xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision 51055da51d1e92bb089c9b9e0ecb1be69396f808)
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 
34208a1b4cSTakashi Iwai static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	return readb(chip->reg_area_virt + offset);
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
39208a1b4cSTakashi Iwai static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	writeb(val, chip->reg_area_virt + offset);
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
44208a1b4cSTakashi Iwai static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	return readw(chip->reg_area_virt + offset);
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
49208a1b4cSTakashi Iwai static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	writew(val, chip->reg_area_virt + offset);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
54208a1b4cSTakashi Iwai static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	return readl(chip->reg_area_virt + offset);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59208a1b4cSTakashi Iwai static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	writel(val, chip->reg_area_virt + offset);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64208a1b4cSTakashi Iwai static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
651da177e4SLinus Torvalds {
66ef21ca24SNishanth Aravamudan 	unsigned long end_time;
671da177e4SLinus Torvalds 	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
681da177e4SLinus Torvalds 
69ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
701da177e4SLinus Torvalds 	do {
711da177e4SLinus Torvalds 		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
721da177e4SLinus Torvalds 			return 0;
738433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
74ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
756436bcf6STakashi Iwai 	dev_err(chip->card->dev,
766436bcf6STakashi Iwai 		"codec_ready: codec %i is not ready [0x%x]\n",
776436bcf6STakashi Iwai 		secondary, snd_ymfpci_readw(chip, reg));
781da177e4SLinus Torvalds 	return -EBUSY;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
81208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
821da177e4SLinus Torvalds {
83208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
841da177e4SLinus Torvalds 	u32 cmd;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
871da177e4SLinus Torvalds 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
881da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
91208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
921da177e4SLinus Torvalds {
93208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
961da177e4SLinus Torvalds 		return ~0;
971da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
981da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
991da177e4SLinus Torvalds 		return ~0;
1001da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
1011da177e4SLinus Torvalds 		int i;
1021da177e4SLinus Torvalds 		for (i = 0; i < 600; i++)
1031da177e4SLinus Torvalds 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  *  Misc routines
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	switch (rate) {
1151da177e4SLinus Torvalds 	case 8000:	return 0x02aaab00;
1161da177e4SLinus Torvalds 	case 11025:	return 0x03accd00;
1171da177e4SLinus Torvalds 	case 16000:	return 0x05555500;
1181da177e4SLinus Torvalds 	case 22050:	return 0x07599a00;
1191da177e4SLinus Torvalds 	case 32000:	return 0x0aaaab00;
1201da177e4SLinus Torvalds 	case 44100:	return 0x0eb33300;
1211da177e4SLinus Torvalds 	default:	return ((rate << 16) / 375) << 5;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static u32 def_rate[8] = {
1261da177e4SLinus Torvalds 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	u32 i;
1321da177e4SLinus Torvalds 	static u32 val[8] = {
1331da177e4SLinus Torvalds 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
1341da177e4SLinus Torvalds 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
1351da177e4SLinus Torvalds 	};
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	if (rate == 44100)
1381da177e4SLinus Torvalds 		return 0x40000000;	/* FIXME: What's the right value? */
1391da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1401da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1411da177e4SLinus Torvalds 			return val[i];
1421da177e4SLinus Torvalds 	return val[0];
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	u32 i;
1481da177e4SLinus Torvalds 	static u32 val[8] = {
1491da177e4SLinus Torvalds 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
1501da177e4SLinus Torvalds 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
1511da177e4SLinus Torvalds 	};
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (rate == 44100)
1541da177e4SLinus Torvalds 		return 0x370A0000;
1551da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1561da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1571da177e4SLinus Torvalds 			return val[i];
1581da177e4SLinus Torvalds 	return val[0];
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds  *  Hardware start management
1631da177e4SLinus Torvalds  */
1641da177e4SLinus Torvalds 
165208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	unsigned long flags;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1701da177e4SLinus Torvalds 	if (chip->start_count++ > 0)
1711da177e4SLinus Torvalds 		goto __end;
1721da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1731da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
1741da177e4SLinus Torvalds 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
1751da177e4SLinus Torvalds       __end:
1761da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unsigned long flags;
1821da177e4SLinus Torvalds 	long timeout = 1000;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1851da177e4SLinus Torvalds 	if (--chip->start_count > 0)
1861da177e4SLinus Torvalds 		goto __end;
1871da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1881da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
1891da177e4SLinus Torvalds 	while (timeout-- > 0) {
1901da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
1911da177e4SLinus Torvalds 			break;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	if (atomic_read(&chip->interrupt_sleep_count)) {
1941da177e4SLinus Torvalds 		atomic_set(&chip->interrupt_sleep_count, 0);
1951da177e4SLinus Torvalds 		wake_up(&chip->interrupt_sleep);
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds       __end:
1981da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
2021da177e4SLinus Torvalds  *  Playback voice management
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds 
205208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip,
206208a1b4cSTakashi Iwai 		       enum snd_ymfpci_voice_type type, int pair,
207208a1b4cSTakashi Iwai 		       struct snd_ymfpci_voice **rvoice)
2081da177e4SLinus Torvalds {
209208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice, *voice2;
2101da177e4SLinus Torvalds 	int idx;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	*rvoice = NULL;
2131da177e4SLinus Torvalds 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
2141da177e4SLinus Torvalds 		voice = &chip->voices[idx];
2151da177e4SLinus Torvalds 		voice2 = pair ? &chip->voices[idx+1] : NULL;
2161da177e4SLinus Torvalds 		if (voice->use || (voice2 && voice2->use))
2171da177e4SLinus Torvalds 			continue;
2181da177e4SLinus Torvalds 		voice->use = 1;
2191da177e4SLinus Torvalds 		if (voice2)
2201da177e4SLinus Torvalds 			voice2->use = 1;
2211da177e4SLinus Torvalds 		switch (type) {
2221da177e4SLinus Torvalds 		case YMFPCI_PCM:
2231da177e4SLinus Torvalds 			voice->pcm = 1;
2241da177e4SLinus Torvalds 			if (voice2)
2251da177e4SLinus Torvalds 				voice2->pcm = 1;
2261da177e4SLinus Torvalds 			break;
2271da177e4SLinus Torvalds 		case YMFPCI_SYNTH:
2281da177e4SLinus Torvalds 			voice->synth = 1;
2291da177e4SLinus Torvalds 			break;
2301da177e4SLinus Torvalds 		case YMFPCI_MIDI:
2311da177e4SLinus Torvalds 			voice->midi = 1;
2321da177e4SLinus Torvalds 			break;
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 		snd_ymfpci_hw_start(chip);
2351da177e4SLinus Torvalds 		if (voice2)
2361da177e4SLinus Torvalds 			snd_ymfpci_hw_start(chip);
2371da177e4SLinus Torvalds 		*rvoice = voice;
2381da177e4SLinus Torvalds 		return 0;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	return -ENOMEM;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
243208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
244208a1b4cSTakashi Iwai 				  enum snd_ymfpci_voice_type type, int pair,
245208a1b4cSTakashi Iwai 				  struct snd_ymfpci_voice **rvoice)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	unsigned long flags;
2481da177e4SLinus Torvalds 	int result;
2491da177e4SLinus Torvalds 
250da3cec35STakashi Iwai 	if (snd_BUG_ON(!rvoice))
251da3cec35STakashi Iwai 		return -EINVAL;
252da3cec35STakashi Iwai 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
253da3cec35STakashi Iwai 		return -EINVAL;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2561da177e4SLinus Torvalds 	for (;;) {
2571da177e4SLinus Torvalds 		result = voice_alloc(chip, type, pair, rvoice);
2581da177e4SLinus Torvalds 		if (result == 0 || type != YMFPCI_PCM)
2591da177e4SLinus Torvalds 			break;
2601da177e4SLinus Torvalds 		/* TODO: synth/midi voice deallocation */
2611da177e4SLinus Torvalds 		break;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2641da177e4SLinus Torvalds 	return result;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	unsigned long flags;
2701da177e4SLinus Torvalds 
271da3cec35STakashi Iwai 	if (snd_BUG_ON(!pvoice))
272da3cec35STakashi Iwai 		return -EINVAL;
2731da177e4SLinus Torvalds 	snd_ymfpci_hw_stop(chip);
2741da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2759ed1261eSTeru KAMOGASHIRA 	if (pvoice->number == chip->src441_used) {
2769ed1261eSTeru KAMOGASHIRA 		chip->src441_used = -1;
2779ed1261eSTeru KAMOGASHIRA 		pvoice->ypcm->use_441_slot = 0;
2789ed1261eSTeru KAMOGASHIRA 	}
2791da177e4SLinus Torvalds 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
2801da177e4SLinus Torvalds 	pvoice->ypcm = NULL;
2811da177e4SLinus Torvalds 	pvoice->interrupt = NULL;
2821da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /*
2871da177e4SLinus Torvalds  *  PCM part
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds 
290208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
2911da177e4SLinus Torvalds {
292208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
2931da177e4SLinus Torvalds 	u32 pos, delta;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if ((ypcm = voice->ypcm) == NULL)
2961da177e4SLinus Torvalds 		return;
2971da177e4SLinus Torvalds 	if (ypcm->substream == NULL)
2981da177e4SLinus Torvalds 		return;
2991da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3001da177e4SLinus Torvalds 	if (ypcm->running) {
3011da177e4SLinus Torvalds 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
3021da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3031da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3041da177e4SLinus Torvalds 		else
3051da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3061da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3071da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3081da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
309ee419653STakashi Iwai 			/*
3106436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
311ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
312ee419653STakashi Iwai 			       chip->active_bank,
313ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
314ee419653STakashi Iwai 			*/
3151da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
3161da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3171da177e4SLinus Torvalds 			snd_pcm_period_elapsed(ypcm->substream);
3181da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3191da177e4SLinus Torvalds 		}
3209bcf6551SClemens Ladisch 
3219bcf6551SClemens Ladisch 		if (unlikely(ypcm->update_pcm_vol)) {
3229bcf6551SClemens Ladisch 			unsigned int subs = ypcm->substream->number;
3239bcf6551SClemens Ladisch 			unsigned int next_bank = 1 - chip->active_bank;
324208a1b4cSTakashi Iwai 			struct snd_ymfpci_playback_bank *bank;
325d3c63763STakashi Iwai 			__le32 volume;
3269bcf6551SClemens Ladisch 
3279bcf6551SClemens Ladisch 			bank = &voice->bank[next_bank];
3289bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
3299bcf6551SClemens Ladisch 			bank->left_gain_end = volume;
3309bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3319bcf6551SClemens Ladisch 				bank->eff2_gain_end = volume;
3329bcf6551SClemens Ladisch 			if (ypcm->voices[1])
3339bcf6551SClemens Ladisch 				bank = &ypcm->voices[1]->bank[next_bank];
3349bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
3359bcf6551SClemens Ladisch 			bank->right_gain_end = volume;
3369bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3379bcf6551SClemens Ladisch 				bank->eff3_gain_end = volume;
3389bcf6551SClemens Ladisch 			ypcm->update_pcm_vol--;
3399bcf6551SClemens Ladisch 		}
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
344208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
3451da177e4SLinus Torvalds {
346208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
347208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
348208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ypcm->chip;
3491da177e4SLinus Torvalds 	u32 pos, delta;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3521da177e4SLinus Torvalds 	if (ypcm->running) {
3531da177e4SLinus Torvalds 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
3541da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3551da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3561da177e4SLinus Torvalds 		else
3571da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3581da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3591da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3601da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
3611da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
362ee419653STakashi Iwai 			/*
3636436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
364ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
365ee419653STakashi Iwai 			       chip->active_bank,
366ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
367ee419653STakashi Iwai 			*/
3681da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3691da177e4SLinus Torvalds 			snd_pcm_period_elapsed(substream);
3701da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
3771da177e4SLinus Torvalds 				       int cmd)
3781da177e4SLinus Torvalds {
379208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
380208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
381177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl = NULL;
3821da177e4SLinus Torvalds 	int result = 0;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3851da177e4SLinus Torvalds 	if (ypcm->voices[0] == NULL) {
3861da177e4SLinus Torvalds 		result = -EINVAL;
3871da177e4SLinus Torvalds 		goto __unlock;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 	switch (cmd) {
3901da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3911da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
3921da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
3931da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
3949ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
3951da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
3961da177e4SLinus Torvalds 		ypcm->running = 1;
3971da177e4SLinus Torvalds 		break;
3981da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
399177a7cdbSClemens Ladisch 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
400177a7cdbSClemens Ladisch 			kctl = chip->pcm_mixer[substream->number].ctl;
401177a7cdbSClemens Ladisch 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
402177a7cdbSClemens Ladisch 		}
403177a7cdbSClemens Ladisch 		/* fall through */
4041da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4051da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4061da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
4079ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4081da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
4091da177e4SLinus Torvalds 		ypcm->running = 0;
4101da177e4SLinus Torvalds 		break;
4111da177e4SLinus Torvalds 	default:
4121da177e4SLinus Torvalds 		result = -EINVAL;
4131da177e4SLinus Torvalds 		break;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds       __unlock:
4161da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
417177a7cdbSClemens Ladisch 	if (kctl)
418177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
4191da177e4SLinus Torvalds 	return result;
4201da177e4SLinus Torvalds }
421208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
4221da177e4SLinus Torvalds 				      int cmd)
4231da177e4SLinus Torvalds {
424208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
425208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
4261da177e4SLinus Torvalds 	int result = 0;
4271da177e4SLinus Torvalds 	u32 tmp;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4301da177e4SLinus Torvalds 	switch (cmd) {
4311da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4321da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4331da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4341da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
4351da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4361da177e4SLinus Torvalds 		ypcm->running = 1;
4371da177e4SLinus Torvalds 		break;
4381da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4391da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4401da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4411da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
4421da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4431da177e4SLinus Torvalds 		ypcm->running = 0;
4441da177e4SLinus Torvalds 		break;
4451da177e4SLinus Torvalds 	default:
4461da177e4SLinus Torvalds 		result = -EINVAL;
4471da177e4SLinus Torvalds 		break;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4501da177e4SLinus Torvalds 	return result;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
453208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	int err;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	if (ypcm->voices[1] != NULL && voices < 2) {
4581da177e4SLinus Torvalds 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
4591da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 	if (voices == 1 && ypcm->voices[0] != NULL)
4621da177e4SLinus Torvalds 		return 0;		/* already allocated */
4631da177e4SLinus Torvalds 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
4641da177e4SLinus Torvalds 		return 0;		/* already allocated */
4651da177e4SLinus Torvalds 	if (voices > 1) {
4661da177e4SLinus Torvalds 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
4671da177e4SLinus Torvalds 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
4681da177e4SLinus Torvalds 			ypcm->voices[0] = NULL;
4691da177e4SLinus Torvalds 		}
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
4721da177e4SLinus Torvalds 	if (err < 0)
4731da177e4SLinus Torvalds 		return err;
4741da177e4SLinus Torvalds 	ypcm->voices[0]->ypcm = ypcm;
4751da177e4SLinus Torvalds 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
4761da177e4SLinus Torvalds 	if (voices > 1) {
4771da177e4SLinus Torvalds 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
4781da177e4SLinus Torvalds 		ypcm->voices[1]->ypcm = ypcm;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	return 0;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
483208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
484208a1b4cSTakashi Iwai 				      struct snd_pcm_runtime *runtime,
4859bcf6551SClemens Ladisch 				      int has_pcm_volume)
4861da177e4SLinus Torvalds {
487208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
4881da177e4SLinus Torvalds 	u32 format;
4899bcf6551SClemens Ladisch 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
4909bcf6551SClemens Ladisch 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
4919bcf6551SClemens Ladisch 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
492208a1b4cSTakashi Iwai 	struct snd_ymfpci_playback_bank *bank;
4931da177e4SLinus Torvalds 	unsigned int nbank;
494d3c63763STakashi Iwai 	__le32 vol_left, vol_right;
4959bcf6551SClemens Ladisch 	u8 use_left, use_right;
4969ed1261eSTeru KAMOGASHIRA 	unsigned long flags;
4971da177e4SLinus Torvalds 
498da3cec35STakashi Iwai 	if (snd_BUG_ON(!voice))
499da3cec35STakashi Iwai 		return;
5009bcf6551SClemens Ladisch 	if (runtime->channels == 1) {
5019bcf6551SClemens Ladisch 		use_left = 1;
5029bcf6551SClemens Ladisch 		use_right = 1;
5039bcf6551SClemens Ladisch 	} else {
5049bcf6551SClemens Ladisch 		use_left = (voiceidx & 1) == 0;
5059bcf6551SClemens Ladisch 		use_right = !use_left;
5069bcf6551SClemens Ladisch 	}
5079bcf6551SClemens Ladisch 	if (has_pcm_volume) {
5089bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
5099bcf6551SClemens Ladisch 				       [ypcm->substream->number].left << 15);
5109bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
5119bcf6551SClemens Ladisch 					[ypcm->substream->number].right << 15);
5129bcf6551SClemens Ladisch 	} else {
5139bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(0x40000000);
5149bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(0x40000000);
5159bcf6551SClemens Ladisch 	}
5169ed1261eSTeru KAMOGASHIRA 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
5179bcf6551SClemens Ladisch 	format = runtime->channels == 2 ? 0x00010000 : 0;
5189bcf6551SClemens Ladisch 	if (snd_pcm_format_width(runtime->format) == 8)
5199bcf6551SClemens Ladisch 		format |= 0x80000000;
5209ed1261eSTeru KAMOGASHIRA 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
5219ed1261eSTeru KAMOGASHIRA 		 runtime->rate == 44100 && runtime->channels == 2 &&
5229ed1261eSTeru KAMOGASHIRA 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
5239ed1261eSTeru KAMOGASHIRA 				   ypcm->chip->src441_used == voice->number)) {
5249ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = voice->number;
5259ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 1;
5269ed1261eSTeru KAMOGASHIRA 		format |= 0x10000000;
5279ed1261eSTeru KAMOGASHIRA 	}
5289ed1261eSTeru KAMOGASHIRA 	if (ypcm->chip->src441_used == voice->number &&
5299ed1261eSTeru KAMOGASHIRA 	    (format & 0x10000000) == 0) {
5309ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = -1;
5319ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 0;
5329ed1261eSTeru KAMOGASHIRA 	}
5339bcf6551SClemens Ladisch 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
5349bcf6551SClemens Ladisch 		format |= 1;
5359ed1261eSTeru KAMOGASHIRA 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
5361da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
5371da177e4SLinus Torvalds 		bank = &voice->bank[nbank];
5389bcf6551SClemens Ladisch 		memset(bank, 0, sizeof(*bank));
5391da177e4SLinus Torvalds 		bank->format = cpu_to_le32(format);
5409bcf6551SClemens Ladisch 		bank->base = cpu_to_le32(runtime->dma_addr);
5419bcf6551SClemens Ladisch 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
5421da177e4SLinus Torvalds 		bank->lpfQ = cpu_to_le32(lpfQ);
5431da177e4SLinus Torvalds 		bank->delta =
5441da177e4SLinus Torvalds 		bank->delta_end = cpu_to_le32(delta);
5451da177e4SLinus Torvalds 		bank->lpfK =
5461da177e4SLinus Torvalds 		bank->lpfK_end = cpu_to_le32(lpfK);
5479bcf6551SClemens Ladisch 		bank->eg_gain =
5489bcf6551SClemens Ladisch 		bank->eg_gain_end = cpu_to_le32(0x40000000);
5491da177e4SLinus Torvalds 
5509bcf6551SClemens Ladisch 		if (ypcm->output_front) {
5519bcf6551SClemens Ladisch 			if (use_left) {
5521da177e4SLinus Torvalds 				bank->left_gain =
5539bcf6551SClemens Ladisch 				bank->left_gain_end = vol_left;
5549bcf6551SClemens Ladisch 			}
5559bcf6551SClemens Ladisch 			if (use_right) {
5561da177e4SLinus Torvalds 				bank->right_gain =
5579bcf6551SClemens Ladisch 				bank->right_gain_end = vol_right;
5589bcf6551SClemens Ladisch 			}
5599bcf6551SClemens Ladisch 		}
5609bcf6551SClemens Ladisch 		if (ypcm->output_rear) {
5615a25c5cfSJaroslav Kysela 		        if (!ypcm->swap_rear) {
5625a25c5cfSJaroslav Kysela         			if (use_left) {
5635a25c5cfSJaroslav Kysela         				bank->eff2_gain =
5645a25c5cfSJaroslav Kysela         				bank->eff2_gain_end = vol_left;
5655a25c5cfSJaroslav Kysela         			}
5665a25c5cfSJaroslav Kysela         			if (use_right) {
5675a25c5cfSJaroslav Kysela         				bank->eff3_gain =
5685a25c5cfSJaroslav Kysela         				bank->eff3_gain_end = vol_right;
5695a25c5cfSJaroslav Kysela         			}
5705a25c5cfSJaroslav Kysela 		        } else {
571153abaa4SClemens Ladisch         			/* The SPDIF out channels seem to be swapped, so we have
572153abaa4SClemens Ladisch         			 * to swap them here, too.  The rear analog out channels
573153abaa4SClemens Ladisch         			 * will be wrong, but otherwise AC3 would not work.
574153abaa4SClemens Ladisch         			 */
5759bcf6551SClemens Ladisch         			if (use_left) {
576153abaa4SClemens Ladisch         				bank->eff3_gain =
577153abaa4SClemens Ladisch         				bank->eff3_gain_end = vol_left;
5789bcf6551SClemens Ladisch         			}
5799bcf6551SClemens Ladisch         			if (use_right) {
580153abaa4SClemens Ladisch         				bank->eff2_gain =
581153abaa4SClemens Ladisch         				bank->eff2_gain_end = vol_right;
5821da177e4SLinus Torvalds         			}
5831da177e4SLinus Torvalds         		}
5841da177e4SLinus Torvalds                 }
5851da177e4SLinus Torvalds 	}
5865a25c5cfSJaroslav Kysela }
5871da177e4SLinus Torvalds 
588e23e7a14SBill Pemberton static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
5891da177e4SLinus Torvalds {
5906974f8adSTakashi Iwai 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
5911da177e4SLinus Torvalds 				4096, &chip->ac3_tmp_base) < 0)
5921da177e4SLinus Torvalds 		return -ENOMEM;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	chip->bank_effect[3][0]->base =
5951da177e4SLinus Torvalds 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
5961da177e4SLinus Torvalds 	chip->bank_effect[3][0]->loop_end =
5971da177e4SLinus Torvalds 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
5981da177e4SLinus Torvalds 	chip->bank_effect[4][0]->base =
5991da177e4SLinus Torvalds 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
6001da177e4SLinus Torvalds 	chip->bank_effect[4][0]->loop_end =
6011da177e4SLinus Torvalds 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6041da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6051da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
6061da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6071da177e4SLinus Torvalds 	return 0;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6131da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6141da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
6151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6161da177e4SLinus Torvalds 	// snd_ymfpci_irq_wait(chip);
6171da177e4SLinus Torvalds 	if (chip->ac3_tmp_base.area) {
6181da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->ac3_tmp_base);
6191da177e4SLinus Torvalds 		chip->ac3_tmp_base.area = NULL;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	return 0;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
624208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
625208a1b4cSTakashi Iwai 					 struct snd_pcm_hw_params *hw_params)
6261da177e4SLinus Torvalds {
627208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
628208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
6291da177e4SLinus Torvalds 	int err;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
6321da177e4SLinus Torvalds 		return err;
6331da177e4SLinus Torvalds 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
6371da177e4SLinus Torvalds {
638208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
639208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
640208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (runtime->private_data == NULL)
6431da177e4SLinus Torvalds 		return 0;
6441da177e4SLinus Torvalds 	ypcm = runtime->private_data;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6471da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
6481da177e4SLinus Torvalds 	if (ypcm->voices[1]) {
6491da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
6501da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 	if (ypcm->voices[0]) {
6531da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
6541da177e4SLinus Torvalds 		ypcm->voices[0] = NULL;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 	return 0;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
659208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
6601da177e4SLinus Torvalds {
661208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
662208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
663208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
664177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl;
6651da177e4SLinus Torvalds 	unsigned int nvoice;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6681da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
6691da177e4SLinus Torvalds 	ypcm->period_pos = 0;
6701da177e4SLinus Torvalds 	ypcm->last_pos = 0;
6711da177e4SLinus Torvalds 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
6729bcf6551SClemens Ladisch 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
6739bcf6551SClemens Ladisch 					  substream->pcm == chip->pcm);
674177a7cdbSClemens Ladisch 
675177a7cdbSClemens Ladisch 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
676177a7cdbSClemens Ladisch 		kctl = chip->pcm_mixer[substream->number].ctl;
677177a7cdbSClemens Ladisch 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
679177a7cdbSClemens Ladisch 	}
6801da177e4SLinus Torvalds 	return 0;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
6841da177e4SLinus Torvalds {
685208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6881da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
689b6ed90c0STakashi Iwai 	return 0;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
692208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
6931da177e4SLinus Torvalds {
694208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
695208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
696208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
697208a1b4cSTakashi Iwai 	struct snd_ymfpci_capture_bank * bank;
6981da177e4SLinus Torvalds 	int nbank;
6991da177e4SLinus Torvalds 	u32 rate, format;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
7021da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
7031da177e4SLinus Torvalds 	ypcm->period_pos = 0;
7041da177e4SLinus Torvalds 	ypcm->last_pos = 0;
7051da177e4SLinus Torvalds 	ypcm->shift = 0;
7061da177e4SLinus Torvalds 	rate = ((48000 * 4096) / runtime->rate) - 1;
7071da177e4SLinus Torvalds 	format = 0;
7081da177e4SLinus Torvalds 	if (runtime->channels == 2) {
7091da177e4SLinus Torvalds 		format |= 2;
7101da177e4SLinus Torvalds 		ypcm->shift++;
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 8)
7131da177e4SLinus Torvalds 		format |= 1;
7141da177e4SLinus Torvalds 	else
7151da177e4SLinus Torvalds 		ypcm->shift++;
7161da177e4SLinus Torvalds 	switch (ypcm->capture_bank_number) {
7171da177e4SLinus Torvalds 	case 0:
7181da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
7191da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
7201da177e4SLinus Torvalds 		break;
7211da177e4SLinus Torvalds 	case 1:
7221da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
7231da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
7241da177e4SLinus Torvalds 		break;
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
7271da177e4SLinus Torvalds 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
7281da177e4SLinus Torvalds 		bank->base = cpu_to_le32(runtime->dma_addr);
7291da177e4SLinus Torvalds 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
7301da177e4SLinus Torvalds 		bank->start = 0;
7311da177e4SLinus Torvalds 		bank->num_of_loops = 0;
7321da177e4SLinus Torvalds 	}
7331da177e4SLinus Torvalds 	return 0;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
736208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
7371da177e4SLinus Torvalds {
738208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
739208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
740208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
741208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	if (!(ypcm->running && voice))
7441da177e4SLinus Torvalds 		return 0;
7451da177e4SLinus Torvalds 	return le32_to_cpu(voice->bank[chip->active_bank].start);
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
7491da177e4SLinus Torvalds {
750208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
751208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
752208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	if (!ypcm->running)
7551da177e4SLinus Torvalds 		return 0;
7561da177e4SLinus Torvalds 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
7601da177e4SLinus Torvalds {
761ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
7621da177e4SLinus Torvalds 	int loops = 4;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	while (loops-- > 0) {
7651da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
7661da177e4SLinus Torvalds 		 	continue;
7671da177e4SLinus Torvalds 		init_waitqueue_entry(&wait, current);
7681da177e4SLinus Torvalds 		add_wait_queue(&chip->interrupt_sleep, &wait);
7691da177e4SLinus Torvalds 		atomic_inc(&chip->interrupt_sleep_count);
7708433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
7711da177e4SLinus Torvalds 		remove_wait_queue(&chip->interrupt_sleep, &wait);
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7757d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
7761da177e4SLinus Torvalds {
777208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = dev_id;
7781da177e4SLinus Torvalds 	u32 status, nvoice, mode;
779208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
7821da177e4SLinus Torvalds 	if (status & 0x80000000) {
7831da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
7841da177e4SLinus Torvalds 		spin_lock(&chip->voice_lock);
7851da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
7861da177e4SLinus Torvalds 			voice = &chip->voices[nvoice];
7871da177e4SLinus Torvalds 			if (voice->interrupt)
7881da177e4SLinus Torvalds 				voice->interrupt(chip, voice);
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
7911da177e4SLinus Torvalds 			if (chip->capture_substream[nvoice])
7921da177e4SLinus Torvalds 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds #if 0
7951da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
7961da177e4SLinus Torvalds 			if (chip->effect_substream[nvoice])
7971da177e4SLinus Torvalds 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
7981da177e4SLinus Torvalds 		}
7991da177e4SLinus Torvalds #endif
8001da177e4SLinus Torvalds 		spin_unlock(&chip->voice_lock);
8011da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
8021da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
8031da177e4SLinus Torvalds 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
8041da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
8051da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		if (atomic_read(&chip->interrupt_sleep_count)) {
8081da177e4SLinus Torvalds 			atomic_set(&chip->interrupt_sleep_count, 0);
8091da177e4SLinus Torvalds 			wake_up(&chip->interrupt_sleep);
8101da177e4SLinus Torvalds 		}
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
8141da177e4SLinus Torvalds 	if (status & 1) {
8151da177e4SLinus Torvalds 		if (chip->timer)
8166e2efaacSClemens Ladisch 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (chip->rawmidi)
8217d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
8221da177e4SLinus Torvalds 	return IRQ_HANDLED;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
825420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_playback =
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8281da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8291da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8301da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8311da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8321da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8331da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8341da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8351da177e4SLinus Torvalds 	.rate_min =		8000,
8361da177e4SLinus Torvalds 	.rate_max =		48000,
8371da177e4SLinus Torvalds 	.channels_min =		1,
8381da177e4SLinus Torvalds 	.channels_max =		2,
8391da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8401da177e4SLinus Torvalds 	.period_bytes_min =	64,
8411da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8421da177e4SLinus Torvalds 	.periods_min =		3,
8431da177e4SLinus Torvalds 	.periods_max =		1024,
8441da177e4SLinus Torvalds 	.fifo_size =		0,
8451da177e4SLinus Torvalds };
8461da177e4SLinus Torvalds 
847420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_capture =
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8501da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8541da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8551da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8561da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8571da177e4SLinus Torvalds 	.rate_min =		8000,
8581da177e4SLinus Torvalds 	.rate_max =		48000,
8591da177e4SLinus Torvalds 	.channels_min =		1,
8601da177e4SLinus Torvalds 	.channels_max =		2,
8611da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8621da177e4SLinus Torvalds 	.period_bytes_min =	64,
8631da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8641da177e4SLinus Torvalds 	.periods_min =		3,
8651da177e4SLinus Torvalds 	.periods_max =		1024,
8661da177e4SLinus Torvalds 	.fifo_size =		0,
8671da177e4SLinus Torvalds };
8681da177e4SLinus Torvalds 
869208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
8701da177e4SLinus Torvalds {
8714d572776SJesper Juhl 	kfree(runtime->private_data);
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
874208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
8751da177e4SLinus Torvalds {
876208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
877208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
878208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
87984f9df15SClemens Ladisch 	int err;
88084f9df15SClemens Ladisch 
88184f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_playback;
88284f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
88384f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
88484f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
88584f9df15SClemens Ladisch 					   5334, UINT_MAX);
88684f9df15SClemens Ladisch 	if (err < 0)
88784f9df15SClemens Ladisch 		return err;
8885b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
8895b0416a3SClemens Ladisch 	if (err < 0)
8905b0416a3SClemens Ladisch 		return err;
8911da177e4SLinus Torvalds 
892e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
8931da177e4SLinus Torvalds 	if (ypcm == NULL)
8941da177e4SLinus Torvalds 		return -ENOMEM;
8951da177e4SLinus Torvalds 	ypcm->chip = chip;
8961da177e4SLinus Torvalds 	ypcm->type = PLAYBACK_VOICE;
8971da177e4SLinus Torvalds 	ypcm->substream = substream;
8981da177e4SLinus Torvalds 	runtime->private_data = ypcm;
8991da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
9001da177e4SLinus Torvalds 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /* call with spinlock held */
904208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9071da177e4SLinus Torvalds 		if (! chip->spdif_opened) /* set AC3 */
9081da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9091da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
9101da177e4SLinus Torvalds 		/* enable second codec (4CHEN) */
9111da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9121da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds /* call with spinlock held */
917208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9201da177e4SLinus Torvalds 		if (! chip->spdif_opened)
9211da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9221da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
9231da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9241da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
928208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
9291da177e4SLinus Torvalds {
930208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
931208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
932208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9331da177e4SLinus Torvalds 	int err;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9361da177e4SLinus Torvalds 		return err;
9371da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9381da177e4SLinus Torvalds 	ypcm->output_front = 1;
9391da177e4SLinus Torvalds 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
940d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9411da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9421da177e4SLinus Torvalds 	if (ypcm->output_rear) {
9431da177e4SLinus Torvalds 		ymfpci_open_extension(chip);
9441da177e4SLinus Torvalds 		chip->rear_opened++;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9471da177e4SLinus Torvalds 	return 0;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
950208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
9511da177e4SLinus Torvalds {
952208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
953208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
954208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9551da177e4SLinus Torvalds 	int err;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9581da177e4SLinus Torvalds 		return err;
9591da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9601da177e4SLinus Torvalds 	ypcm->output_front = 0;
9611da177e4SLinus Torvalds 	ypcm->output_rear = 1;
962d9301263SGlen Masgai 	ypcm->swap_rear = 1;
9631da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9641da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
9651da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
9661da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9671da177e4SLinus Torvalds 	chip->spdif_pcm_bits = chip->spdif_bits;
9681da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
9691da177e4SLinus Torvalds 	chip->spdif_opened++;
9701da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
9731da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
9741da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
9751da177e4SLinus Torvalds 	return 0;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
9791da177e4SLinus Torvalds {
980208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
981208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
982208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9831da177e4SLinus Torvalds 	int err;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 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 
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 
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 
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 
1041208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	return 0;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
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 
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 
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 
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 
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 
11271da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
11281da177e4SLinus Torvalds 		return err;
11291da177e4SLinus Torvalds 	pcm->private_data = chip;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
11321da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/* global setup */
11351da177e4SLinus Torvalds 	pcm->info_flags = 0;
11361da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI");
11371da177e4SLinus Torvalds 	chip->pcm = pcm;
11381da177e4SLinus Torvalds 
1139b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1140b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
11411da177e4SLinus Torvalds 
114238c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
11430afdb8f2STakashi Iwai 				     snd_pcm_std_chmaps, 2, 0, NULL);
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11466769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
11471da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_ac97_open,
11481da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11491da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11501da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11511da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11521da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11531da177e4SLinus Torvalds };
11541da177e4SLinus Torvalds 
115538c47181SLars-Peter Clausen int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
11561da177e4SLinus Torvalds {
1157208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11581da177e4SLinus Torvalds 	int err;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
11611da177e4SLinus Torvalds 		return err;
11621da177e4SLinus Torvalds 	pcm->private_data = chip;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	/* global setup */
11671da177e4SLinus Torvalds 	pcm->info_flags = 0;
11681da177e4SLinus Torvalds 	sprintf(pcm->name, "YMFPCI - %s",
11691da177e4SLinus Torvalds 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
11701da177e4SLinus Torvalds 	chip->pcm2 = pcm;
11711da177e4SLinus Torvalds 
1172b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1173b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	return 0;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11786769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
11791da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_spdif_open,
11801da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_spdif_close,
11811da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11821da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11831da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11841da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11851da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11861da177e4SLinus Torvalds };
11871da177e4SLinus Torvalds 
118838c47181SLars-Peter Clausen int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
11891da177e4SLinus Torvalds {
1190208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11911da177e4SLinus Torvalds 	int err;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
11941da177e4SLinus Torvalds 		return err;
11951da177e4SLinus Torvalds 	pcm->private_data = chip;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	/* global setup */
12001da177e4SLinus Torvalds 	pcm->info_flags = 0;
12011da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - IEC958");
12021da177e4SLinus Torvalds 	chip->pcm_spdif = pcm;
12031da177e4SLinus Torvalds 
1204b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1205b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12106769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
12111da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_4ch_open,
12121da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_4ch_close,
12131da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12141da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12151da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12161da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12171da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12181da177e4SLinus Torvalds };
12191da177e4SLinus Torvalds 
12200afdb8f2STakashi Iwai static const struct snd_pcm_chmap_elem surround_map[] = {
12210afdb8f2STakashi Iwai 	{ .channels = 1,
12225efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
12230afdb8f2STakashi Iwai 	{ .channels = 2,
12240afdb8f2STakashi Iwai 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12250afdb8f2STakashi Iwai 	{ }
12260afdb8f2STakashi Iwai };
12270afdb8f2STakashi Iwai 
122838c47181SLars-Peter Clausen int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
12291da177e4SLinus Torvalds {
1230208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12311da177e4SLinus Torvalds 	int err;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
12341da177e4SLinus Torvalds 		return err;
12351da177e4SLinus Torvalds 	pcm->private_data = chip;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	/* global setup */
12401da177e4SLinus Torvalds 	pcm->info_flags = 0;
12411da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - Rear PCM");
12421da177e4SLinus Torvalds 	chip->pcm_4ch = pcm;
12431da177e4SLinus Torvalds 
1244b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1245b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
12461da177e4SLinus Torvalds 
124738c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12480afdb8f2STakashi Iwai 				     surround_map, 2, 0, NULL);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
1251208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
12541da177e4SLinus Torvalds 	uinfo->count = 1;
12551da177e4SLinus Torvalds 	return 0;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
1258208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1259208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
12601da177e4SLinus Torvalds {
1261208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12641da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
12651da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1266fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
12671da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
12681da177e4SLinus Torvalds 	return 0;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
1271208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1272208a1b4cSTakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
12731da177e4SLinus Torvalds {
1274208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12751da177e4SLinus Torvalds 	unsigned int val;
12761da177e4SLinus Torvalds 	int change;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
12791da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
12801da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12811da177e4SLinus Torvalds 	change = chip->spdif_bits != val;
12821da177e4SLinus Torvalds 	chip->spdif_bits = val;
12831da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
12841da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
12851da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
12861da177e4SLinus Torvalds 	return change;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
1289f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
12921da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
12931da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_default_info,
12941da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_default_get,
12951da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_default_put
12961da177e4SLinus Torvalds };
12971da177e4SLinus Torvalds 
1298208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13011da177e4SLinus Torvalds 	uinfo->count = 1;
13021da177e4SLinus Torvalds 	return 0;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
1305208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1306208a1b4cSTakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
13071da177e4SLinus Torvalds {
1308208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13111da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0x3e;
13121da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
13131da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13141da177e4SLinus Torvalds 	return 0;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
1317f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
13201da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13211da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
13221da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_mask_info,
13231da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_mask_get,
13241da177e4SLinus Torvalds };
13251da177e4SLinus Torvalds 
1326208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13291da177e4SLinus Torvalds 	uinfo->count = 1;
13301da177e4SLinus Torvalds 	return 0;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
1333208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1334208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13351da177e4SLinus Torvalds {
1336208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13391da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
13401da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1341fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13421da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13431da177e4SLinus Torvalds 	return 0;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
1346208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1347208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13481da177e4SLinus Torvalds {
1349208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13501da177e4SLinus Torvalds 	unsigned int val;
13511da177e4SLinus Torvalds 	int change;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13541da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13551da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13561da177e4SLinus Torvalds 	change = chip->spdif_pcm_bits != val;
13571da177e4SLinus Torvalds 	chip->spdif_pcm_bits = val;
13581da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
13591da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
13601da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13611da177e4SLinus Torvalds 	return change;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
1364f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
13671da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13681da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
13691da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_stream_info,
13701da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_stream_get,
13711da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_stream_put
13721da177e4SLinus Torvalds };
13731da177e4SLinus Torvalds 
1374208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
13751da177e4SLinus Torvalds {
1376bed6896dSClemens Ladisch 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
13771da177e4SLinus Torvalds 
1378bed6896dSClemens Ladisch 	return snd_ctl_enum_info(info, 1, 3, texts);
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
1381208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
13821da177e4SLinus Torvalds {
1383208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13841da177e4SLinus Torvalds 	u16 reg;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13871da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
13881da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13891da177e4SLinus Torvalds 	if (!(reg & 0x100))
13901da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 0;
13911da177e4SLinus Torvalds 	else
13921da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
13931da177e4SLinus Torvalds 	return 0;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
1396208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
13971da177e4SLinus Torvalds {
1398208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13991da177e4SLinus Torvalds 	u16 reg, old_reg;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14021da177e4SLinus Torvalds 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14031da177e4SLinus Torvalds 	if (value->value.enumerated.item[0] == 0)
14041da177e4SLinus Torvalds 		reg = old_reg & ~0x100;
14051da177e4SLinus Torvalds 	else
14061da177e4SLinus Torvalds 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
14071da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
14081da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14091da177e4SLinus Torvalds 	return reg != old_reg;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
1412f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
14131da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
14141da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
14151da177e4SLinus Torvalds 	.name =		"Direct Recording Source",
14161da177e4SLinus Torvalds 	.info =		snd_ymfpci_drec_source_info,
14171da177e4SLinus Torvalds 	.get =		snd_ymfpci_drec_source_get,
14181da177e4SLinus Torvalds 	.put =		snd_ymfpci_drec_source_put
14191da177e4SLinus Torvalds };
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds /*
14221da177e4SLinus Torvalds  *  Mixer controls
14231da177e4SLinus Torvalds  */
14241da177e4SLinus Torvalds 
1425d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
14261da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14271da177e4SLinus Torvalds   .info = snd_ymfpci_info_single, \
14281da177e4SLinus Torvalds   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1429d602c885SGlen Masgai   .private_value = ((reg) | ((shift) << 16)) }
14301da177e4SLinus Torvalds 
1431a5ce8890STakashi Iwai #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
14321da177e4SLinus Torvalds 
1433208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1434208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14351da177e4SLinus Torvalds {
1436208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1437d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1438d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1439d602c885SGlen Masgai 	unsigned int mask = 1;
14401da177e4SLinus Torvalds 
1441d602c885SGlen Masgai 	switch (reg) {
14421da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14431da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14441da177e4SLinus Torvalds 	default: return -EINVAL;
14451da177e4SLinus Torvalds 	}
1446d602c885SGlen Masgai 	ucontrol->value.integer.value[0] =
1447d602c885SGlen Masgai 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
14481da177e4SLinus Torvalds 	return 0;
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
1451208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1452208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14531da177e4SLinus Torvalds {
1454208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1455d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1456d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1457d602c885SGlen Masgai  	unsigned int mask = 1;
14581da177e4SLinus Torvalds 	int change;
14591da177e4SLinus Torvalds 	unsigned int val, oval;
14601da177e4SLinus Torvalds 
1461d602c885SGlen Masgai 	switch (reg) {
14621da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14631da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14641da177e4SLinus Torvalds 	default: return -EINVAL;
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
14671da177e4SLinus Torvalds 	val <<= shift;
14681da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14691da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
14701da177e4SLinus Torvalds 	val = (oval & ~(mask << shift)) | val;
14711da177e4SLinus Torvalds 	change = val != oval;
14721da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val);
14731da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14741da177e4SLinus Torvalds 	return change;
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14770cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
147833925186STakashi Iwai 
14791da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \
14801da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
148133925186STakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
14821da177e4SLinus Torvalds   .info = snd_ymfpci_info_double, \
14831da177e4SLinus Torvalds   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
148433925186STakashi Iwai   .private_value = reg, \
148533925186STakashi Iwai   .tlv = { .p = db_scale_native } }
14861da177e4SLinus Torvalds 
1487208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
14921da177e4SLinus Torvalds 		return -EINVAL;
1493467a8c2fSAdrian Bunk 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
14941da177e4SLinus Torvalds 	uinfo->count = 2;
14951da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
1496467a8c2fSAdrian Bunk 	uinfo->value.integer.max = 16383;
14971da177e4SLinus Torvalds 	return 0;
14981da177e4SLinus Torvalds }
14991da177e4SLinus Torvalds 
1500208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15011da177e4SLinus Torvalds {
1502208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15031da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1504467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15051da177e4SLinus Torvalds 	unsigned int val;
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15081da177e4SLinus Torvalds 		return -EINVAL;
15091da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15101da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, reg);
15111da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15121da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
15131da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
15141da177e4SLinus Torvalds 	return 0;
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
1517208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15181da177e4SLinus Torvalds {
1519208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15201da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1521467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15221da177e4SLinus Torvalds 	int change;
15231da177e4SLinus Torvalds 	unsigned int val1, val2, oval;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15261da177e4SLinus Torvalds 		return -EINVAL;
15271da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15281da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15291da177e4SLinus Torvalds 	val1 <<= shift_left;
15301da177e4SLinus Torvalds 	val2 <<= shift_right;
15311da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15321da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15331da177e4SLinus Torvalds 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
15341da177e4SLinus Torvalds 	change = val1 != oval;
15351da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val1);
15361da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15371da177e4SLinus Torvalds 	return change;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
1540177a7cdbSClemens Ladisch static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1541177a7cdbSClemens Ladisch 				       struct snd_ctl_elem_value *ucontrol)
1542177a7cdbSClemens Ladisch {
1543177a7cdbSClemens Ladisch 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1544177a7cdbSClemens Ladisch 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1545177a7cdbSClemens Ladisch 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1546177a7cdbSClemens Ladisch 	int change;
1547177a7cdbSClemens Ladisch 	unsigned int value, oval;
1548177a7cdbSClemens Ladisch 
1549177a7cdbSClemens Ladisch 	value = ucontrol->value.integer.value[0] & 0x3fff;
1550177a7cdbSClemens Ladisch 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1551177a7cdbSClemens Ladisch 	spin_lock_irq(&chip->reg_lock);
1552177a7cdbSClemens Ladisch 	oval = snd_ymfpci_readl(chip, reg);
1553177a7cdbSClemens Ladisch 	change = value != oval;
1554177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg, value);
1555177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg2, value);
1556177a7cdbSClemens Ladisch 	spin_unlock_irq(&chip->reg_lock);
1557177a7cdbSClemens Ladisch 	return change;
1558177a7cdbSClemens Ladisch }
1559177a7cdbSClemens Ladisch 
15601da177e4SLinus Torvalds /*
15611da177e4SLinus Torvalds  * 4ch duplication
15621da177e4SLinus Torvalds  */
1563a5ce8890STakashi Iwai #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
15641da177e4SLinus Torvalds 
1565208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15661da177e4SLinus Torvalds {
1567208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15681da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
15691da177e4SLinus Torvalds 	return 0;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
1572208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15731da177e4SLinus Torvalds {
1574208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15751da177e4SLinus Torvalds 	int change;
15761da177e4SLinus Torvalds 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
15771da177e4SLinus Torvalds 	if (change)
15781da177e4SLinus Torvalds 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
15791da177e4SLinus Torvalds 	return change;
15801da177e4SLinus Torvalds }
15811da177e4SLinus Torvalds 
1582f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
15834d20bb1dSRaymond Yau 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15844d20bb1dSRaymond Yau 	.name = "4ch Duplication",
15854d20bb1dSRaymond Yau 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
15864d20bb1dSRaymond Yau 	.info = snd_ymfpci_info_dup4ch,
15874d20bb1dSRaymond Yau 	.get = snd_ymfpci_get_dup4ch,
15884d20bb1dSRaymond Yau 	.put = snd_ymfpci_put_dup4ch,
15894d20bb1dSRaymond Yau };
15901da177e4SLinus Torvalds 
1591e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_controls[] = {
1592177a7cdbSClemens Ladisch {
1593177a7cdbSClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1594177a7cdbSClemens Ladisch 	.name = "Wave Playback Volume",
1595177a7cdbSClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1596177a7cdbSClemens Ladisch 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1597177a7cdbSClemens Ladisch 	.info = snd_ymfpci_info_double,
1598177a7cdbSClemens Ladisch 	.get = snd_ymfpci_get_double,
1599177a7cdbSClemens Ladisch 	.put = snd_ymfpci_put_nativedacvol,
1600177a7cdbSClemens Ladisch 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1601177a7cdbSClemens Ladisch 	.tlv = { .p = db_scale_native },
1602177a7cdbSClemens Ladisch },
16031da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
16041da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
16051da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
16061da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
16071da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
16081da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
16091da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
161089f3325aSRaymond Yau YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
16111da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
16121da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
16131da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
16141da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1615d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1616d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1617d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
16181da177e4SLinus Torvalds };
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds /*
16221da177e4SLinus Torvalds  * GPIO
16231da177e4SLinus Torvalds  */
16241da177e4SLinus Torvalds 
1625208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	u16 reg, mode;
16281da177e4SLinus Torvalds 	unsigned long flags;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16311da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16321da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16331da177e4SLinus Torvalds 	reg |= (1 << pin);
16341da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16351da177e4SLinus Torvalds 	/* set the level mode for input line */
16361da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
16371da177e4SLinus Torvalds 	mode &= ~(3 << (pin * 2));
16381da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
16391da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16401da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
16411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16421da177e4SLinus Torvalds 	return (mode >> pin) & 1;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
1645208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds 	u16 reg;
16481da177e4SLinus Torvalds 	unsigned long flags;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16511da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16521da177e4SLinus Torvalds 	reg &= ~(1 << pin);
16531da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16541da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16551da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
16561da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	return 0;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
1662a5ce8890STakashi Iwai #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
16631da177e4SLinus Torvalds 
1664208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16651da177e4SLinus Torvalds {
1666208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16671da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
16681da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
16691da177e4SLinus Torvalds 	return 0;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
1672208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16731da177e4SLinus Torvalds {
1674208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16751da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
16781da177e4SLinus Torvalds 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
16791da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
16801da177e4SLinus Torvalds 		return 1;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 	return 0;
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
1685f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
16861da177e4SLinus Torvalds 	.name = "Shared Rear/Line-In Switch",
16871da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16881da177e4SLinus Torvalds 	.info = snd_ymfpci_gpio_sw_info,
16891da177e4SLinus Torvalds 	.get = snd_ymfpci_gpio_sw_get,
16901da177e4SLinus Torvalds 	.put = snd_ymfpci_gpio_sw_put,
16911da177e4SLinus Torvalds 	.private_value = 2,
16921da177e4SLinus Torvalds };
16931da177e4SLinus Torvalds 
16949bcf6551SClemens Ladisch /*
16959bcf6551SClemens Ladisch  * PCM voice volume
16969bcf6551SClemens Ladisch  */
16979bcf6551SClemens Ladisch 
1698208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1699208a1b4cSTakashi Iwai 				   struct snd_ctl_elem_info *uinfo)
17009bcf6551SClemens Ladisch {
17019bcf6551SClemens Ladisch 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
17029bcf6551SClemens Ladisch 	uinfo->count = 2;
17039bcf6551SClemens Ladisch 	uinfo->value.integer.min = 0;
17049bcf6551SClemens Ladisch 	uinfo->value.integer.max = 0x8000;
17059bcf6551SClemens Ladisch 	return 0;
17069bcf6551SClemens Ladisch }
17079bcf6551SClemens Ladisch 
1708208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1709208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17109bcf6551SClemens Ladisch {
1711208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17129bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
17139bcf6551SClemens Ladisch 
17149bcf6551SClemens Ladisch 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
17159bcf6551SClemens Ladisch 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
17169bcf6551SClemens Ladisch 	return 0;
17179bcf6551SClemens Ladisch }
17189bcf6551SClemens Ladisch 
1719208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1720208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17219bcf6551SClemens Ladisch {
1722208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17239bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
1724208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17259bcf6551SClemens Ladisch 	unsigned long flags;
17269bcf6551SClemens Ladisch 
17279bcf6551SClemens Ladisch 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
17289bcf6551SClemens Ladisch 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
17299bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
17309bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
17314e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].left > 0x8000)
17324e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].left = 0x8000;
17334e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].right > 0x8000)
17344e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].right = 0x8000;
17359bcf6551SClemens Ladisch 
1736208a1b4cSTakashi Iwai 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
17379bcf6551SClemens Ladisch 		spin_lock_irqsave(&chip->voice_lock, flags);
17389bcf6551SClemens Ladisch 		if (substream->runtime && substream->runtime->private_data) {
1739208a1b4cSTakashi Iwai 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
17409ed1261eSTeru KAMOGASHIRA 			if (!ypcm->use_441_slot)
17419bcf6551SClemens Ladisch 				ypcm->update_pcm_vol = 2;
17429bcf6551SClemens Ladisch 		}
17439bcf6551SClemens Ladisch 		spin_unlock_irqrestore(&chip->voice_lock, flags);
17449bcf6551SClemens Ladisch 		return 1;
17459bcf6551SClemens Ladisch 	}
17469bcf6551SClemens Ladisch 	return 0;
17479bcf6551SClemens Ladisch }
17489bcf6551SClemens Ladisch 
1749f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
17509bcf6551SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
17519bcf6551SClemens Ladisch 	.name = "PCM Playback Volume",
17529bcf6551SClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
17539bcf6551SClemens Ladisch 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
17549bcf6551SClemens Ladisch 	.info = snd_ymfpci_pcm_vol_info,
17559bcf6551SClemens Ladisch 	.get = snd_ymfpci_pcm_vol_get,
17569bcf6551SClemens Ladisch 	.put = snd_ymfpci_pcm_vol_put,
17579bcf6551SClemens Ladisch };
17589bcf6551SClemens Ladisch 
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds /*
17611da177e4SLinus Torvalds  *  Mixer routines
17621da177e4SLinus Torvalds  */
17631da177e4SLinus Torvalds 
1764208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
17651da177e4SLinus Torvalds {
1766208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = bus->private_data;
17671da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
1770208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
17711da177e4SLinus Torvalds {
1772208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
17731da177e4SLinus Torvalds 	chip->ac97 = NULL;
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
1776e23e7a14SBill Pemberton int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
17771da177e4SLinus Torvalds {
1778208a1b4cSTakashi Iwai 	struct snd_ac97_template ac97;
1779208a1b4cSTakashi Iwai 	struct snd_kcontrol *kctl;
1780208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17811da177e4SLinus Torvalds 	unsigned int idx;
17821da177e4SLinus Torvalds 	int err;
1783*51055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
17841da177e4SLinus Torvalds 		.write = snd_ymfpci_codec_write,
17851da177e4SLinus Torvalds 		.read = snd_ymfpci_codec_read,
17861da177e4SLinus Torvalds 	};
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
17891da177e4SLinus Torvalds 		return err;
17901da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
17911da177e4SLinus Torvalds 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
17941da177e4SLinus Torvalds 	ac97.private_data = chip;
17951da177e4SLinus Torvalds 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
17961da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
17971da177e4SLinus Torvalds 		return err;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	/* to be sure */
18001da177e4SLinus Torvalds 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
18011da177e4SLinus Torvalds 			     AC97_EA_VRA|AC97_EA_VRM, 0);
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
18041da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
18051da177e4SLinus Torvalds 			return err;
18061da177e4SLinus Torvalds 	}
18074d20bb1dSRaymond Yau 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
18084d20bb1dSRaymond Yau 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
18094d20bb1dSRaymond Yau 		err = snd_ctl_add(chip->card, kctl);
18104d20bb1dSRaymond Yau 		if (err < 0)
18114d20bb1dSRaymond Yau 			return err;
18124d20bb1dSRaymond Yau 	}
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	/* add S/PDIF control */
1815da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip->pcm_spdif))
1816da3cec35STakashi Iwai 		return -ENXIO;
18171da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
18181da177e4SLinus Torvalds 		return err;
18191da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18201da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
18211da177e4SLinus Torvalds 		return err;
18221da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18231da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
18241da177e4SLinus Torvalds 		return err;
18251da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18261da177e4SLinus Torvalds 	chip->spdif_pcm_ctl = kctl;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	/* direct recording source */
18291da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
18301da177e4SLinus Torvalds 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
18311da177e4SLinus Torvalds 		return err;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	/*
18341da177e4SLinus Torvalds 	 * shared rear/line-in
18351da177e4SLinus Torvalds 	 */
18361da177e4SLinus Torvalds 	if (rear_switch) {
18371da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
18381da177e4SLinus Torvalds 			return err;
18391da177e4SLinus Torvalds 	}
18401da177e4SLinus Torvalds 
18419bcf6551SClemens Ladisch 	/* per-voice volume */
18429bcf6551SClemens Ladisch 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
18439bcf6551SClemens Ladisch 	for (idx = 0; idx < 32; ++idx) {
18449bcf6551SClemens Ladisch 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
18459bcf6551SClemens Ladisch 		if (!kctl)
18469bcf6551SClemens Ladisch 			return -ENOMEM;
18479bcf6551SClemens Ladisch 		kctl->id.device = chip->pcm->device;
18489bcf6551SClemens Ladisch 		kctl->id.subdevice = idx;
18499bcf6551SClemens Ladisch 		kctl->private_value = (unsigned long)substream;
18509bcf6551SClemens Ladisch 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
18519bcf6551SClemens Ladisch 			return err;
18529bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].left = 0x8000;
18539bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].right = 0x8000;
18549bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].ctl = kctl;
18559bcf6551SClemens Ladisch 		substream = substream->next;
18569bcf6551SClemens Ladisch 	}
18579bcf6551SClemens Ladisch 
18581da177e4SLinus Torvalds 	return 0;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds /*
18631da177e4SLinus Torvalds  * timer
18641da177e4SLinus Torvalds  */
18651da177e4SLinus Torvalds 
1866208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer)
18671da177e4SLinus Torvalds {
1868208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
18691da177e4SLinus Torvalds 	unsigned long flags;
18701da177e4SLinus Torvalds 	unsigned int count;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
18731da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
18746e2efaacSClemens Ladisch 	if (timer->sticks > 1) {
18756e2efaacSClemens Ladisch 		chip->timer_ticks = timer->sticks;
18766e2efaacSClemens Ladisch 		count = timer->sticks - 1;
18776e2efaacSClemens Ladisch 	} else {
18786e2efaacSClemens Ladisch 		/*
18796e2efaacSClemens Ladisch 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
18806e2efaacSClemens Ladisch 		 * counting two ticks for each interrupt.
18816e2efaacSClemens Ladisch 		 */
18826e2efaacSClemens Ladisch 		chip->timer_ticks = 2;
18836e2efaacSClemens Ladisch 		count = 2 - 1;
18846e2efaacSClemens Ladisch 	}
18851da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
18861da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
18871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
18881da177e4SLinus Torvalds 	return 0;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
1891208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer)
18921da177e4SLinus Torvalds {
1893208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
18941da177e4SLinus Torvalds 	unsigned long flags;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
18971da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
18981da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
18991da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19001da177e4SLinus Torvalds 	return 0;
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds 
1903208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
19041da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
19051da177e4SLinus Torvalds {
19061da177e4SLinus Torvalds 	*num = 1;
19076e2efaacSClemens Ladisch 	*den = 96000;
19081da177e4SLinus Torvalds 	return 0;
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
19115ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_ymfpci_timer_hw = {
19121da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
19136e2efaacSClemens Ladisch 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
19146e2efaacSClemens Ladisch 	.ticks = 0x10000,
19151da177e4SLinus Torvalds 	.start = snd_ymfpci_timer_start,
19161da177e4SLinus Torvalds 	.stop = snd_ymfpci_timer_stop,
19171da177e4SLinus Torvalds 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
19181da177e4SLinus Torvalds };
19191da177e4SLinus Torvalds 
1920e23e7a14SBill Pemberton int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
19211da177e4SLinus Torvalds {
1922208a1b4cSTakashi Iwai 	struct snd_timer *timer = NULL;
1923208a1b4cSTakashi Iwai 	struct snd_timer_id tid;
19241da177e4SLinus Torvalds 	int err;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19271da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19281da177e4SLinus Torvalds 	tid.card = chip->card->number;
19291da177e4SLinus Torvalds 	tid.device = device;
19301da177e4SLinus Torvalds 	tid.subdevice = 0;
19311da177e4SLinus Torvalds 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
19321da177e4SLinus Torvalds 		strcpy(timer->name, "YMFPCI timer");
19331da177e4SLinus Torvalds 		timer->private_data = chip;
19341da177e4SLinus Torvalds 		timer->hw = snd_ymfpci_timer_hw;
19351da177e4SLinus Torvalds 	}
19361da177e4SLinus Torvalds 	chip->timer = timer;
19371da177e4SLinus Torvalds 	return err;
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds /*
19421da177e4SLinus Torvalds  *  proc interface
19431da177e4SLinus Torvalds  */
19441da177e4SLinus Torvalds 
1945208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1946208a1b4cSTakashi Iwai 				 struct snd_info_buffer *buffer)
19471da177e4SLinus Torvalds {
1948208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = entry->private_data;
19491da177e4SLinus Torvalds 	int i;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	snd_iprintf(buffer, "YMFPCI\n\n");
19521da177e4SLinus Torvalds 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
19531da177e4SLinus Torvalds 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds 
1956e23e7a14SBill Pemberton static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
19571da177e4SLinus Torvalds {
195847f2769bSTakashi Iwai 	return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds /*
19621da177e4SLinus Torvalds  *  initialization routines
19631da177e4SLinus Torvalds  */
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
19661da177e4SLinus Torvalds {
19671da177e4SLinus Torvalds 	u8 cmd;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
19701da177e4SLinus Torvalds #if 0 // force to reset
19711da177e4SLinus Torvalds 	if (cmd & 0x03) {
19721da177e4SLinus Torvalds #endif
19731da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
19741da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
19751da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
19761da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
19771da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
19781da177e4SLinus Torvalds #if 0
19791da177e4SLinus Torvalds 	}
19801da177e4SLinus Torvalds #endif
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
1983208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
1988208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
19891da177e4SLinus Torvalds {
19901da177e4SLinus Torvalds 	u32 val;
19911da177e4SLinus Torvalds 	int timeout = 1000;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
19941da177e4SLinus Torvalds 	if (val)
19951da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
19961da177e4SLinus Torvalds 	while (timeout-- > 0) {
19971da177e4SLinus Torvalds 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
19981da177e4SLinus Torvalds 		if ((val & 0x00000002) == 0)
19991da177e4SLinus Torvalds 			break;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds }
20021da177e4SLinus Torvalds 
2003102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2004102fa906SClemens Ladisch {
2005102fa906SClemens Ladisch 	int err, is_1e;
2006102fa906SClemens Ladisch 	const char *name;
2007102fa906SClemens Ladisch 
2008102fa906SClemens Ladisch 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2009102fa906SClemens Ladisch 			       &chip->pci->dev);
2010102fa906SClemens Ladisch 	if (err >= 0) {
2011b82a82d0SDavid Woodhouse 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
20126436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20136436bcf6STakashi Iwai 				"DSP microcode has wrong size\n");
2014102fa906SClemens Ladisch 			err = -EINVAL;
2015102fa906SClemens Ladisch 		}
2016102fa906SClemens Ladisch 	}
2017b7dd2b34STakashi Iwai 	if (err < 0)
2018102fa906SClemens Ladisch 		return err;
2019102fa906SClemens Ladisch 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2020102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2021102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2022102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2023102fa906SClemens Ladisch 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2024102fa906SClemens Ladisch 	err = request_firmware(&chip->controller_microcode, name,
2025102fa906SClemens Ladisch 			       &chip->pci->dev);
2026102fa906SClemens Ladisch 	if (err >= 0) {
2027b82a82d0SDavid Woodhouse 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
20286436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20296436bcf6STakashi Iwai 				"controller microcode has wrong size\n");
2030102fa906SClemens Ladisch 			err = -EINVAL;
2031102fa906SClemens Ladisch 		}
2032102fa906SClemens Ladisch 	}
2033b7dd2b34STakashi Iwai 	if (err < 0)
2034102fa906SClemens Ladisch 		return err;
2035102fa906SClemens Ladisch 	return 0;
2036102fa906SClemens Ladisch }
20377e0af29dSClemens Ladisch 
20387e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
20397e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
20407e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
20417e0af29dSClemens Ladisch 
2042208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
20431da177e4SLinus Torvalds {
20441da177e4SLinus Torvalds 	int i;
20451da177e4SLinus Torvalds 	u16 ctrl;
2046b82a82d0SDavid Woodhouse 	const __le32 *inst;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
20491da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
20501da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
20511da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
20521da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
20531da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
20541da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
20551da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
20561da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
20571da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
20581da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	/* setup DSP instruction code */
2061b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->dsp_microcode->data;
20621da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2063b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2064b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	/* setup control instruction code */
2067b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->controller_microcode->data;
20681da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2069b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2070b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	snd_ymfpci_enable_dsp(chip);
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds 
2075e23e7a14SBill Pemberton static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
20761da177e4SLinus Torvalds {
20771da177e4SLinus Torvalds 	long size, playback_ctrl_size;
20781da177e4SLinus Torvalds 	int voice, bank, reg;
20791da177e4SLinus Torvalds 	u8 *ptr;
20801da177e4SLinus Torvalds 	dma_addr_t ptr_addr;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
20831da177e4SLinus Torvalds 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
20841da177e4SLinus Torvalds 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
20851da177e4SLinus Torvalds 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
20861da177e4SLinus Torvalds 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
20871da177e4SLinus Torvalds 
20887ab39926SClemens Ladisch 	size = ALIGN(playback_ctrl_size, 0x100) +
20897ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
20907ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
20917ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
20921da177e4SLinus Torvalds 	       chip->work_size;
20931da177e4SLinus Torvalds 	/* work_ptr must be aligned to 256 bytes, but it's already
20941da177e4SLinus Torvalds 	   covered with the kernel page allocation mechanism */
20956974f8adSTakashi Iwai 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
20961da177e4SLinus Torvalds 				size, &chip->work_ptr) < 0)
20971da177e4SLinus Torvalds 		return -ENOMEM;
20981da177e4SLinus Torvalds 	ptr = chip->work_ptr.area;
20991da177e4SLinus Torvalds 	ptr_addr = chip->work_ptr.addr;
21001da177e4SLinus Torvalds 	memset(ptr, 0, size);	/* for sure */
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	chip->bank_base_playback = ptr;
21031da177e4SLinus Torvalds 	chip->bank_base_playback_addr = ptr_addr;
2104d3c63763STakashi Iwai 	chip->ctrl_playback = (__le32 *)ptr;
21051da177e4SLinus Torvalds 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
21067ab39926SClemens Ladisch 	ptr += ALIGN(playback_ctrl_size, 0x100);
21077ab39926SClemens Ladisch 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
21081da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
21091da177e4SLinus Torvalds 		chip->voices[voice].number = voice;
2110208a1b4cSTakashi Iwai 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
21111da177e4SLinus Torvalds 		chip->voices[voice].bank_addr = ptr_addr;
21121da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2113208a1b4cSTakashi Iwai 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
21141da177e4SLinus Torvalds 			ptr += chip->bank_size_playback;
21151da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_playback;
21161da177e4SLinus Torvalds 		}
21171da177e4SLinus Torvalds 	}
21187ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21197ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21201da177e4SLinus Torvalds 	chip->bank_base_capture = ptr;
21211da177e4SLinus Torvalds 	chip->bank_base_capture_addr = ptr_addr;
21221da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
21231da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2124208a1b4cSTakashi Iwai 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
21251da177e4SLinus Torvalds 			ptr += chip->bank_size_capture;
21261da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_capture;
21271da177e4SLinus Torvalds 		}
21287ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21297ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21301da177e4SLinus Torvalds 	chip->bank_base_effect = ptr;
21311da177e4SLinus Torvalds 	chip->bank_base_effect_addr = ptr_addr;
21321da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
21331da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2134208a1b4cSTakashi Iwai 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
21351da177e4SLinus Torvalds 			ptr += chip->bank_size_effect;
21361da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_effect;
21371da177e4SLinus Torvalds 		}
21387ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21397ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21401da177e4SLinus Torvalds 	chip->work_base = ptr;
21411da177e4SLinus Torvalds 	chip->work_base_addr = ptr_addr;
21421da177e4SLinus Torvalds 
2143da3cec35STakashi Iwai 	snd_BUG_ON(ptr + chip->work_size !=
2144da3cec35STakashi Iwai 		   chip->work_ptr.area + chip->work_ptr.bytes);
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
21471da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
21481da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
21491da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
21501da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	/* S/PDIF output initialization */
21531da177e4SLinus Torvalds 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
21541da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
21551da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	/* S/PDIF input initialization */
21581da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	/* digital mixer setup */
21611da177e4SLinus Torvalds 	for (reg = 0x80; reg < 0xc0; reg += 4)
21621da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, reg, 0);
21631da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
21644a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
21651da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
21661da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
21671da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
21681da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
21691da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
21701da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	return 0;
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
2175208a1b4cSTakashi Iwai static int snd_ymfpci_free(struct snd_ymfpci *chip)
21761da177e4SLinus Torvalds {
21771da177e4SLinus Torvalds 	u16 ctrl;
21781da177e4SLinus Torvalds 
2179da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
2180da3cec35STakashi Iwai 		return -EINVAL;
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	if (chip->res_reg_area) {	/* don't touch busy hardware */
21831da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
21841da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
21851da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
21861da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
21871da177e4SLinus Torvalds 		snd_ymfpci_disable_dsp(chip);
21881da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
21891da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
21901da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
21911da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
21921da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
21931da177e4SLinus Torvalds 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
21941da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
21951da177e4SLinus Torvalds 	}
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	snd_ymfpci_ac3_done(chip);
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	/* Set PCI device to D3 state */
22001da177e4SLinus Torvalds #if 0
22011da177e4SLinus Torvalds 	/* FIXME: temporarily disabled, otherwise we cannot fire up
22021da177e4SLinus Torvalds 	 * the chip again unless reboot.  ACPI bug?
22031da177e4SLinus Torvalds 	 */
2204db10e7fbSYijing Wang 	pci_set_power_state(chip->pci, PCI_D3hot);
22051da177e4SLinus Torvalds #endif
22061da177e4SLinus Torvalds 
2207c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22087009fa56STakashi Iwai 	kfree(chip->saved_regs);
22091da177e4SLinus Torvalds #endif
221095866d38STakashi Iwai 	if (chip->irq >= 0)
221195866d38STakashi Iwai 		free_irq(chip->irq, chip);
2212b1d5776dSTakashi Iwai 	release_and_free_resource(chip->mpu_res);
2213b1d5776dSTakashi Iwai 	release_and_free_resource(chip->fm_res);
22141da177e4SLinus Torvalds 	snd_ymfpci_free_gameport(chip);
22151da177e4SLinus Torvalds 	iounmap(chip->reg_area_virt);
22161da177e4SLinus Torvalds 	if (chip->work_ptr.area)
22171da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->work_ptr);
22181da177e4SLinus Torvalds 
2219b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_reg_area);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
2224102fa906SClemens Ladisch 	release_firmware(chip->dsp_microcode);
2225102fa906SClemens Ladisch 	release_firmware(chip->controller_microcode);
22261da177e4SLinus Torvalds 	kfree(chip);
22271da177e4SLinus Torvalds 	return 0;
22281da177e4SLinus Torvalds }
22291da177e4SLinus Torvalds 
2230208a1b4cSTakashi Iwai static int snd_ymfpci_dev_free(struct snd_device *device)
22311da177e4SLinus Torvalds {
2232208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = device->device_data;
22331da177e4SLinus Torvalds 	return snd_ymfpci_free(chip);
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
2236c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22371da177e4SLinus Torvalds static int saved_regs_index[] = {
22381da177e4SLinus Torvalds 	/* spdif */
22391da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTCTRL,
22401da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTSTATUS,
22411da177e4SLinus Torvalds 	YDSXGR_SPDIFINCTRL,
22421da177e4SLinus Torvalds 	/* volumes */
22431da177e4SLinus Torvalds 	YDSXGR_PRIADCLOOPVOL,
22441da177e4SLinus Torvalds 	YDSXGR_NATIVEDACINVOL,
22451da177e4SLinus Torvalds 	YDSXGR_NATIVEDACOUTVOL,
22469ed1261eSTeru KAMOGASHIRA 	YDSXGR_BUF441OUTVOL,
22471da177e4SLinus Torvalds 	YDSXGR_NATIVEADCINVOL,
22481da177e4SLinus Torvalds 	YDSXGR_SPDIFLOOPVOL,
22491da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTVOL,
22501da177e4SLinus Torvalds 	YDSXGR_ZVOUTVOL,
22511da177e4SLinus Torvalds 	YDSXGR_LEGACYOUTVOL,
22521da177e4SLinus Torvalds 	/* address bases */
22531da177e4SLinus Torvalds 	YDSXGR_PLAYCTRLBASE,
22541da177e4SLinus Torvalds 	YDSXGR_RECCTRLBASE,
22551da177e4SLinus Torvalds 	YDSXGR_EFFCTRLBASE,
22561da177e4SLinus Torvalds 	YDSXGR_WORKBASE,
22571da177e4SLinus Torvalds 	/* capture set up */
22581da177e4SLinus Torvalds 	YDSXGR_MAPOFREC,
22591da177e4SLinus Torvalds 	YDSXGR_RECFORMAT,
22601da177e4SLinus Torvalds 	YDSXGR_RECSLOTSR,
22611da177e4SLinus Torvalds 	YDSXGR_ADCFORMAT,
22621da177e4SLinus Torvalds 	YDSXGR_ADCSLOTSR,
22631da177e4SLinus Torvalds };
22641da177e4SLinus Torvalds #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
22651da177e4SLinus Torvalds 
226668cb2b55STakashi Iwai static int snd_ymfpci_suspend(struct device *dev)
22671da177e4SLinus Torvalds {
226868cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2269ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
22701da177e4SLinus Torvalds 	unsigned int i;
22711da177e4SLinus Torvalds 
2272ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
22731da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
22741da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
22751da177e4SLinus Torvalds 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
22761da177e4SLinus Torvalds 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
227728aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
227828aa165cSTakashi Iwai 			     &chip->saved_dsxg_legacy);
227928aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
228028aa165cSTakashi Iwai 			     &chip->saved_dsxg_elegacy);
22811da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22824a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22831da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
22841da177e4SLinus Torvalds 	return 0;
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
228768cb2b55STakashi Iwai static int snd_ymfpci_resume(struct device *dev)
22881da177e4SLinus Torvalds {
228968cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
229068cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2291ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
22921da177e4SLinus Torvalds 	unsigned int i;
22931da177e4SLinus Torvalds 
2294ded46235STakashi Iwai 	snd_ymfpci_aclink_reset(pci);
22951da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
22961da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
22971da177e4SLinus Torvalds 	udelay(100);
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23001da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
23031da177e4SLinus Torvalds 
230428aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
230528aa165cSTakashi Iwai 			      chip->saved_dsxg_legacy);
230628aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
230728aa165cSTakashi Iwai 			      chip->saved_dsxg_elegacy);
230828aa165cSTakashi Iwai 
23091da177e4SLinus Torvalds 	/* start hw again */
23101da177e4SLinus Torvalds 	if (chip->start_count > 0) {
23111da177e4SLinus Torvalds 		spin_lock_irq(&chip->reg_lock);
23121da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
23131da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
23141da177e4SLinus Torvalds 		spin_unlock_irq(&chip->reg_lock);
23151da177e4SLinus Torvalds 	}
2316ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
23171da177e4SLinus Torvalds 	return 0;
23181da177e4SLinus Torvalds }
231968cb2b55STakashi Iwai 
232068cb2b55STakashi Iwai SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2321c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
23221da177e4SLinus Torvalds 
2323e23e7a14SBill Pemberton int snd_ymfpci_create(struct snd_card *card,
23241da177e4SLinus Torvalds 		      struct pci_dev *pci,
23251da177e4SLinus Torvalds 		      unsigned short old_legacy_ctrl,
2326208a1b4cSTakashi Iwai 		      struct snd_ymfpci **rchip)
23271da177e4SLinus Torvalds {
2328208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
23291da177e4SLinus Torvalds 	int err;
2330efb0ad25STakashi Iwai 	static const struct snd_device_ops ops = {
23311da177e4SLinus Torvalds 		.dev_free =	snd_ymfpci_dev_free,
23321da177e4SLinus Torvalds 	};
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	*rchip = NULL;
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	/* enable PCI device */
23371da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
23381da177e4SLinus Torvalds 		return err;
23391da177e4SLinus Torvalds 
2340e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
23411da177e4SLinus Torvalds 	if (chip == NULL) {
23421da177e4SLinus Torvalds 		pci_disable_device(pci);
23431da177e4SLinus Torvalds 		return -ENOMEM;
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 	chip->old_legacy_ctrl = old_legacy_ctrl;
23461da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
23471da177e4SLinus Torvalds 	spin_lock_init(&chip->voice_lock);
23481da177e4SLinus Torvalds 	init_waitqueue_head(&chip->interrupt_sleep);
23491da177e4SLinus Torvalds 	atomic_set(&chip->interrupt_sleep_count, 0);
23501da177e4SLinus Torvalds 	chip->card = card;
23511da177e4SLinus Torvalds 	chip->pci = pci;
23521da177e4SLinus Torvalds 	chip->irq = -1;
23531da177e4SLinus Torvalds 	chip->device_id = pci->device;
235444c10138SAuke Kok 	chip->rev = pci->revision;
23551da177e4SLinus Torvalds 	chip->reg_area_phys = pci_resource_start(pci, 0);
23561da177e4SLinus Torvalds 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
23571da177e4SLinus Torvalds 	pci_set_master(pci);
23589ed1261eSTeru KAMOGASHIRA 	chip->src441_used = -1;
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
23616436bcf6STakashi Iwai 		dev_err(chip->card->dev,
23626436bcf6STakashi Iwai 			"unable to grab memory region 0x%lx-0x%lx\n",
23636436bcf6STakashi Iwai 			chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2364ba2186e4SMarkus Elfring 		err = -EBUSY;
2365ba2186e4SMarkus Elfring 		goto free_chip;
23661da177e4SLinus Torvalds 	}
2367437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2368934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
23696436bcf6STakashi Iwai 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2370ba2186e4SMarkus Elfring 		err = -EBUSY;
2371ba2186e4SMarkus Elfring 		goto free_chip;
23721da177e4SLinus Torvalds 	}
23731da177e4SLinus Torvalds 	chip->irq = pci->irq;
2374b7a03a1eSTakashi Iwai 	card->sync_irq = chip->irq;
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds 	snd_ymfpci_aclink_reset(pci);
23771da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2378ba2186e4SMarkus Elfring 		err = -EIO;
2379ba2186e4SMarkus Elfring 		goto free_chip;
23801da177e4SLinus Torvalds 	}
23811da177e4SLinus Torvalds 
2382102fa906SClemens Ladisch 	err = snd_ymfpci_request_firmware(chip);
2383102fa906SClemens Ladisch 	if (err < 0) {
23846436bcf6STakashi Iwai 		dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2385ba2186e4SMarkus Elfring 		goto free_chip;
2386102fa906SClemens Ladisch 	}
23871da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	udelay(100); /* seems we need a delay after downloading image.. */
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	if (snd_ymfpci_memalloc(chip) < 0) {
2392ba2186e4SMarkus Elfring 		err = -EIO;
2393ba2186e4SMarkus Elfring 		goto free_chip;
23941da177e4SLinus Torvalds 	}
23951da177e4SLinus Torvalds 
2396ba2186e4SMarkus Elfring 	err = snd_ymfpci_ac3_init(chip);
2397ba2186e4SMarkus Elfring 	if (err < 0)
2398ba2186e4SMarkus Elfring 		goto free_chip;
23991da177e4SLinus Torvalds 
2400c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
24016da2ec56SKees Cook 	chip->saved_regs = kmalloc_array(YDSXGR_NUM_SAVED_REGS, sizeof(u32),
24027009fa56STakashi Iwai 					 GFP_KERNEL);
24031da177e4SLinus Torvalds 	if (chip->saved_regs == NULL) {
2404ba2186e4SMarkus Elfring 		err = -ENOMEM;
2405ba2186e4SMarkus Elfring 		goto free_chip;
24061da177e4SLinus Torvalds 	}
24071da177e4SLinus Torvalds #endif
24081da177e4SLinus Torvalds 
2409ba2186e4SMarkus Elfring 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2410ba2186e4SMarkus Elfring 	if (err < 0)
2411ba2186e4SMarkus Elfring 		goto free_chip;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	snd_ymfpci_proc_init(card, chip);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	*rchip = chip;
24161da177e4SLinus Torvalds 	return 0;
2417ba2186e4SMarkus Elfring 
2418ba2186e4SMarkus Elfring free_chip:
2419ba2186e4SMarkus Elfring 	snd_ymfpci_free(chip);
2420ba2186e4SMarkus Elfring 	return err;
24211da177e4SLinus Torvalds }
2422