xref: /openbmc/linux/sound/pci/rme96.c (revision 7d12e780e003f93433d49ce78cfedf4b4c52adc5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *   ALSA driver for RME Digi96, Digi96/8 and Digi96/8 PRO/PAD/PST audio
31da177e4SLinus Torvalds  *   interfaces
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Copyright (c) 2000, 2001 Anders Torger <torger@ludd.luth.se>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *      Thanks to Henk Hesselink <henk@anda.nl> for the analog volume control
81da177e4SLinus Torvalds  *      code.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  *   (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds  *   GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <sound/driver.h>
271da177e4SLinus Torvalds #include <linux/delay.h>
281da177e4SLinus Torvalds #include <linux/init.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/pci.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/moduleparam.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <sound/core.h>
351da177e4SLinus Torvalds #include <sound/info.h>
361da177e4SLinus Torvalds #include <sound/control.h>
371da177e4SLinus Torvalds #include <sound/pcm.h>
381da177e4SLinus Torvalds #include <sound/pcm_params.h>
391da177e4SLinus Torvalds #include <sound/asoundef.h>
401da177e4SLinus Torvalds #include <sound/initval.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/io.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* note, two last pcis should be equal, it is not a bug */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds MODULE_AUTHOR("Anders Torger <torger@ludd.luth.se>");
471da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Digi96, Digi96/8, Digi96/8 PRO, Digi96/8 PST, "
481da177e4SLinus Torvalds 		   "Digi96/8 PAD");
491da177e4SLinus Torvalds MODULE_LICENSE("GPL");
501da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{RME,Digi96},"
511da177e4SLinus Torvalds 		"{RME,Digi96/8},"
521da177e4SLinus Torvalds 		"{RME,Digi96/8 PRO},"
531da177e4SLinus Torvalds 		"{RME,Digi96/8 PST},"
541da177e4SLinus Torvalds 		"{RME,Digi96/8 PAD}}");
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
571da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
581da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
611da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Digi96 soundcard.");
621da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
631da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Digi96 soundcard.");
641da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
651da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable RME Digi96 soundcard.");
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Defines for RME Digi96 series, from internal RME reference documents
691da177e4SLinus Torvalds  * dated 12.01.00
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define RME96_SPDIF_NCHANNELS 2
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Playback and capture buffer size */
751da177e4SLinus Torvalds #define RME96_BUFFER_SIZE 0x10000
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* IO area size */
781da177e4SLinus Torvalds #define RME96_IO_SIZE 0x60000
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* IO area offsets */
811da177e4SLinus Torvalds #define RME96_IO_PLAY_BUFFER      0x0
821da177e4SLinus Torvalds #define RME96_IO_REC_BUFFER       0x10000
831da177e4SLinus Torvalds #define RME96_IO_CONTROL_REGISTER 0x20000
841da177e4SLinus Torvalds #define RME96_IO_ADDITIONAL_REG   0x20004
851da177e4SLinus Torvalds #define RME96_IO_CONFIRM_PLAY_IRQ 0x20008
861da177e4SLinus Torvalds #define RME96_IO_CONFIRM_REC_IRQ  0x2000C
871da177e4SLinus Torvalds #define RME96_IO_SET_PLAY_POS     0x40000
881da177e4SLinus Torvalds #define RME96_IO_RESET_PLAY_POS   0x4FFFC
891da177e4SLinus Torvalds #define RME96_IO_SET_REC_POS      0x50000
901da177e4SLinus Torvalds #define RME96_IO_RESET_REC_POS    0x5FFFC
911da177e4SLinus Torvalds #define RME96_IO_GET_PLAY_POS     0x20000
921da177e4SLinus Torvalds #define RME96_IO_GET_REC_POS      0x30000
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Write control register bits */
951da177e4SLinus Torvalds #define RME96_WCR_START     (1 << 0)
961da177e4SLinus Torvalds #define RME96_WCR_START_2   (1 << 1)
971da177e4SLinus Torvalds #define RME96_WCR_GAIN_0    (1 << 2)
981da177e4SLinus Torvalds #define RME96_WCR_GAIN_1    (1 << 3)
991da177e4SLinus Torvalds #define RME96_WCR_MODE24    (1 << 4)
1001da177e4SLinus Torvalds #define RME96_WCR_MODE24_2  (1 << 5)
1011da177e4SLinus Torvalds #define RME96_WCR_BM        (1 << 6)
1021da177e4SLinus Torvalds #define RME96_WCR_BM_2      (1 << 7)
1031da177e4SLinus Torvalds #define RME96_WCR_ADAT      (1 << 8)
1041da177e4SLinus Torvalds #define RME96_WCR_FREQ_0    (1 << 9)
1051da177e4SLinus Torvalds #define RME96_WCR_FREQ_1    (1 << 10)
1061da177e4SLinus Torvalds #define RME96_WCR_DS        (1 << 11)
1071da177e4SLinus Torvalds #define RME96_WCR_PRO       (1 << 12)
1081da177e4SLinus Torvalds #define RME96_WCR_EMP       (1 << 13)
1091da177e4SLinus Torvalds #define RME96_WCR_SEL       (1 << 14)
1101da177e4SLinus Torvalds #define RME96_WCR_MASTER    (1 << 15)
1111da177e4SLinus Torvalds #define RME96_WCR_PD        (1 << 16)
1121da177e4SLinus Torvalds #define RME96_WCR_INP_0     (1 << 17)
1131da177e4SLinus Torvalds #define RME96_WCR_INP_1     (1 << 18)
1141da177e4SLinus Torvalds #define RME96_WCR_THRU_0    (1 << 19)
1151da177e4SLinus Torvalds #define RME96_WCR_THRU_1    (1 << 20)
1161da177e4SLinus Torvalds #define RME96_WCR_THRU_2    (1 << 21)
1171da177e4SLinus Torvalds #define RME96_WCR_THRU_3    (1 << 22)
1181da177e4SLinus Torvalds #define RME96_WCR_THRU_4    (1 << 23)
1191da177e4SLinus Torvalds #define RME96_WCR_THRU_5    (1 << 24)
1201da177e4SLinus Torvalds #define RME96_WCR_THRU_6    (1 << 25)
1211da177e4SLinus Torvalds #define RME96_WCR_THRU_7    (1 << 26)
1221da177e4SLinus Torvalds #define RME96_WCR_DOLBY     (1 << 27)
1231da177e4SLinus Torvalds #define RME96_WCR_MONITOR_0 (1 << 28)
1241da177e4SLinus Torvalds #define RME96_WCR_MONITOR_1 (1 << 29)
1251da177e4SLinus Torvalds #define RME96_WCR_ISEL      (1 << 30)
1261da177e4SLinus Torvalds #define RME96_WCR_IDIS      (1 << 31)
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_0 2
1291da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_1 3
1301da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_0 9
1311da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_1 10
1321da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_0 17
1331da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_1 18
1341da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_0 28
1351da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_1 29
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* Read control register bits */
1381da177e4SLinus Torvalds #define RME96_RCR_AUDIO_ADDR_MASK 0xFFFF
1391da177e4SLinus Torvalds #define RME96_RCR_IRQ_2     (1 << 16)
1401da177e4SLinus Torvalds #define RME96_RCR_T_OUT     (1 << 17)
1411da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_0  (1 << 21)
1421da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_1  (1 << 22)
1431da177e4SLinus Torvalds #define RME96_RCR_LOCK      (1 << 23)
1441da177e4SLinus Torvalds #define RME96_RCR_VERF      (1 << 26)
1451da177e4SLinus Torvalds #define RME96_RCR_F0        (1 << 27)
1461da177e4SLinus Torvalds #define RME96_RCR_F1        (1 << 28)
1471da177e4SLinus Torvalds #define RME96_RCR_F2        (1 << 29)
1481da177e4SLinus Torvalds #define RME96_RCR_AUTOSYNC  (1 << 30)
1491da177e4SLinus Torvalds #define RME96_RCR_IRQ       (1 << 31)
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F0 27
1521da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F1 28
1531da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F2 29
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /* Additonal register bits */
1561da177e4SLinus Torvalds #define RME96_AR_WSEL       (1 << 0)
1571da177e4SLinus Torvalds #define RME96_AR_ANALOG     (1 << 1)
1581da177e4SLinus Torvalds #define RME96_AR_FREQPAD_0  (1 << 2)
1591da177e4SLinus Torvalds #define RME96_AR_FREQPAD_1  (1 << 3)
1601da177e4SLinus Torvalds #define RME96_AR_FREQPAD_2  (1 << 4)
1611da177e4SLinus Torvalds #define RME96_AR_PD2        (1 << 5)
1621da177e4SLinus Torvalds #define RME96_AR_DAC_EN     (1 << 6)
1631da177e4SLinus Torvalds #define RME96_AR_CLATCH     (1 << 7)
1641da177e4SLinus Torvalds #define RME96_AR_CCLK       (1 << 8)
1651da177e4SLinus Torvalds #define RME96_AR_CDATA      (1 << 9)
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds #define RME96_AR_BITPOS_F0 2
1681da177e4SLinus Torvalds #define RME96_AR_BITPOS_F1 3
1691da177e4SLinus Torvalds #define RME96_AR_BITPOS_F2 4
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* Monitor tracks */
1721da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_1_2 0
1731da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_3_4 1
1741da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_5_6 2
1751da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_7_8 3
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /* Attenuation */
1781da177e4SLinus Torvalds #define RME96_ATTENUATION_0 0
1791da177e4SLinus Torvalds #define RME96_ATTENUATION_6 1
1801da177e4SLinus Torvalds #define RME96_ATTENUATION_12 2
1811da177e4SLinus Torvalds #define RME96_ATTENUATION_18 3
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* Input types */
1841da177e4SLinus Torvalds #define RME96_INPUT_OPTICAL 0
1851da177e4SLinus Torvalds #define RME96_INPUT_COAXIAL 1
1861da177e4SLinus Torvalds #define RME96_INPUT_INTERNAL 2
1871da177e4SLinus Torvalds #define RME96_INPUT_XLR 3
1881da177e4SLinus Torvalds #define RME96_INPUT_ANALOG 4
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /* Clock modes */
1911da177e4SLinus Torvalds #define RME96_CLOCKMODE_SLAVE 0
1921da177e4SLinus Torvalds #define RME96_CLOCKMODE_MASTER 1
1931da177e4SLinus Torvalds #define RME96_CLOCKMODE_WORDCLOCK 2
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* Block sizes in bytes */
1961da177e4SLinus Torvalds #define RME96_SMALL_BLOCK_SIZE 2048
1971da177e4SLinus Torvalds #define RME96_LARGE_BLOCK_SIZE 8192
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /* Volume control */
2001da177e4SLinus Torvalds #define RME96_AD1852_VOL_BITS 14
2011da177e4SLinus Torvalds #define RME96_AD1855_VOL_BITS 10
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 
204a3aefd88STakashi Iwai struct rme96 {
2051da177e4SLinus Torvalds 	spinlock_t    lock;
2061da177e4SLinus Torvalds 	int irq;
2071da177e4SLinus Torvalds 	unsigned long port;
2081da177e4SLinus Torvalds 	void __iomem *iobase;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	u32 wcreg;    /* cached write control register value */
2111da177e4SLinus Torvalds 	u32 wcreg_spdif;		/* S/PDIF setup */
2121da177e4SLinus Torvalds 	u32 wcreg_spdif_stream;		/* S/PDIF setup (temporary) */
2131da177e4SLinus Torvalds 	u32 rcreg;    /* cached read control register value */
2141da177e4SLinus Torvalds 	u32 areg;     /* cached additional register value */
2151da177e4SLinus Torvalds 	u16 vol[2]; /* cached volume of analog output */
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	u8 rev; /* card revision number */
2181da177e4SLinus Torvalds 
219a3aefd88STakashi Iwai 	struct snd_pcm_substream *playback_substream;
220a3aefd88STakashi Iwai 	struct snd_pcm_substream *capture_substream;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	int playback_frlog; /* log2 of framesize */
2231da177e4SLinus Torvalds 	int capture_frlog;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds         size_t playback_periodsize; /* in bytes, zero if not used */
2261da177e4SLinus Torvalds 	size_t capture_periodsize; /* in bytes, zero if not used */
2271da177e4SLinus Torvalds 
228a3aefd88STakashi Iwai 	struct snd_card *card;
229a3aefd88STakashi Iwai 	struct snd_pcm *spdif_pcm;
230a3aefd88STakashi Iwai 	struct snd_pcm *adat_pcm;
2311da177e4SLinus Torvalds 	struct pci_dev     *pci;
232a3aefd88STakashi Iwai 	struct snd_kcontrol   *spdif_ctl;
233a3aefd88STakashi Iwai };
2341da177e4SLinus Torvalds 
235f40b6890STakashi Iwai static struct pci_device_id snd_rme96_ids[] = {
2368b7fc421SRoland Dreier 	{ PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_RME_DIGI96,
2371da177e4SLinus Torvalds 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2388b7fc421SRoland Dreier 	{ PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_RME_DIGI96_8,
2391da177e4SLinus Torvalds 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2408b7fc421SRoland Dreier 	{ PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PRO,
2411da177e4SLinus Torvalds 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2428b7fc421SRoland Dreier 	{ PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST,
2431da177e4SLinus Torvalds 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2441da177e4SLinus Torvalds 	{ 0, }
2451da177e4SLinus Torvalds };
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_rme96_ids);
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds #define RME96_ISPLAYING(rme96) ((rme96)->wcreg & RME96_WCR_START)
2501da177e4SLinus Torvalds #define RME96_ISRECORDING(rme96) ((rme96)->wcreg & RME96_WCR_START_2)
2518b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_IN(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2528b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_OUT(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO || \
2538b7fc421SRoland Dreier 				     (rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2541da177e4SLinus Torvalds #define	RME96_DAC_IS_1852(rme96) (RME96_HAS_ANALOG_OUT(rme96) && (rme96)->rev >= 4)
2558b7fc421SRoland Dreier #define	RME96_DAC_IS_1855(rme96) (((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && (rme96)->rev < 4) || \
2568b7fc421SRoland Dreier 			          ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO && (rme96)->rev == 2))
2571da177e4SLinus Torvalds #define	RME96_185X_MAX_OUT(rme96) ((1 << (RME96_DAC_IS_1852(rme96) ? RME96_AD1852_VOL_BITS : RME96_AD1855_VOL_BITS)) - 1)
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static int
260a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static int
263a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds static int
266a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
2671da177e4SLinus Torvalds 			   int cmd);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static int
270a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
2711da177e4SLinus Torvalds 			  int cmd);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds static snd_pcm_uframes_t
274a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds static snd_pcm_uframes_t
277a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static void __devinit
280a3aefd88STakashi Iwai snd_rme96_proc_init(struct rme96 *rme96);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static int
283a3aefd88STakashi Iwai snd_rme96_create_switches(struct snd_card *card,
284a3aefd88STakashi Iwai 			  struct rme96 *rme96);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static int
287a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static inline unsigned int
290a3aefd88STakashi Iwai snd_rme96_playback_ptr(struct rme96 *rme96)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
2931da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->playback_frlog;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static inline unsigned int
297a3aefd88STakashi Iwai snd_rme96_capture_ptr(struct rme96 *rme96)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_REC_POS)
3001da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->capture_frlog;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static int
3041da177e4SLinus Torvalds snd_rme96_ratecode(int rate)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds     switch (rate) {
3071da177e4SLinus Torvalds     case 32000: return SNDRV_PCM_RATE_32000;
3081da177e4SLinus Torvalds     case 44100: return SNDRV_PCM_RATE_44100;
3091da177e4SLinus Torvalds     case 48000: return SNDRV_PCM_RATE_48000;
3101da177e4SLinus Torvalds     case 64000: return SNDRV_PCM_RATE_64000;
3111da177e4SLinus Torvalds     case 88200: return SNDRV_PCM_RATE_88200;
3121da177e4SLinus Torvalds     case 96000: return SNDRV_PCM_RATE_96000;
3131da177e4SLinus Torvalds     }
3141da177e4SLinus Torvalds     return 0;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static int
318a3aefd88STakashi Iwai snd_rme96_playback_silence(struct snd_pcm_substream *substream,
3191da177e4SLinus Torvalds 			   int channel, /* not used (interleaved data) */
3201da177e4SLinus Torvalds 			   snd_pcm_uframes_t pos,
3211da177e4SLinus Torvalds 			   snd_pcm_uframes_t count)
3221da177e4SLinus Torvalds {
323a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3241da177e4SLinus Torvalds 	count <<= rme96->playback_frlog;
3251da177e4SLinus Torvalds 	pos <<= rme96->playback_frlog;
3261da177e4SLinus Torvalds 	memset_io(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
3271da177e4SLinus Torvalds 		  0, count);
3281da177e4SLinus Torvalds 	return 0;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static int
332a3aefd88STakashi Iwai snd_rme96_playback_copy(struct snd_pcm_substream *substream,
3331da177e4SLinus Torvalds 			int channel, /* not used (interleaved data) */
3341da177e4SLinus Torvalds 			snd_pcm_uframes_t pos,
3351da177e4SLinus Torvalds 			void __user *src,
3361da177e4SLinus Torvalds 			snd_pcm_uframes_t count)
3371da177e4SLinus Torvalds {
338a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3391da177e4SLinus Torvalds 	count <<= rme96->playback_frlog;
3401da177e4SLinus Torvalds 	pos <<= rme96->playback_frlog;
3411da177e4SLinus Torvalds 	copy_from_user_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos, src,
3421da177e4SLinus Torvalds 			    count);
3431da177e4SLinus Torvalds 	return 0;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static int
347a3aefd88STakashi Iwai snd_rme96_capture_copy(struct snd_pcm_substream *substream,
3481da177e4SLinus Torvalds 		       int channel, /* not used (interleaved data) */
3491da177e4SLinus Torvalds 		       snd_pcm_uframes_t pos,
3501da177e4SLinus Torvalds 		       void __user *dst,
3511da177e4SLinus Torvalds 		       snd_pcm_uframes_t count)
3521da177e4SLinus Torvalds {
353a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3541da177e4SLinus Torvalds 	count <<= rme96->capture_frlog;
3551da177e4SLinus Torvalds 	pos <<= rme96->capture_frlog;
3561da177e4SLinus Torvalds 	copy_to_user_fromio(dst, rme96->iobase + RME96_IO_REC_BUFFER + pos,
3571da177e4SLinus Torvalds 			    count);
3581da177e4SLinus Torvalds         return 0;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /*
3627f927fccSAlexey Dobriyan  * Digital output capabilities (S/PDIF)
3631da177e4SLinus Torvalds  */
364a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_playback_spdif_info =
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
3671da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
3681da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
3691da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
3701da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
3711da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
3721da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
3731da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
3741da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
3751da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
3761da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
3771da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
3781da177e4SLinus Torvalds 	.rate_min =	     32000,
3791da177e4SLinus Torvalds 	.rate_max =	     96000,
3801da177e4SLinus Torvalds 	.channels_min =	     2,
3811da177e4SLinus Torvalds 	.channels_max =	     2,
3821da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
3831da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
3841da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
3851da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
3861da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
3871da177e4SLinus Torvalds 	.fifo_size =	     0,
3881da177e4SLinus Torvalds };
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3917f927fccSAlexey Dobriyan  * Digital input capabilities (S/PDIF)
3921da177e4SLinus Torvalds  */
393a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_capture_spdif_info =
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
3961da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
3971da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
3981da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
3991da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4001da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4011da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
4021da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
4031da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
4041da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
4051da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
4061da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
4071da177e4SLinus Torvalds 	.rate_min =	     32000,
4081da177e4SLinus Torvalds 	.rate_max =	     96000,
4091da177e4SLinus Torvalds 	.channels_min =	     2,
4101da177e4SLinus Torvalds 	.channels_max =	     2,
4111da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4121da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4131da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4141da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4151da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4161da177e4SLinus Torvalds 	.fifo_size =	     0,
4171da177e4SLinus Torvalds };
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds /*
4207f927fccSAlexey Dobriyan  * Digital output capabilities (ADAT)
4211da177e4SLinus Torvalds  */
422a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_playback_adat_info =
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4251da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
4261da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4271da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4281da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4291da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4301da177e4SLinus Torvalds 	.rates =             (SNDRV_PCM_RATE_44100 |
4311da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000),
4321da177e4SLinus Torvalds 	.rate_min =          44100,
4331da177e4SLinus Torvalds 	.rate_max =          48000,
4341da177e4SLinus Torvalds 	.channels_min =      8,
4351da177e4SLinus Torvalds 	.channels_max =	     8,
4361da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4371da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4381da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4391da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4401da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4411da177e4SLinus Torvalds 	.fifo_size =	     0,
4421da177e4SLinus Torvalds };
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /*
4457f927fccSAlexey Dobriyan  * Digital input capabilities (ADAT)
4461da177e4SLinus Torvalds  */
447a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_capture_adat_info =
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4501da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
4511da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4521da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4531da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4541da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4551da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_44100 |
4561da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000),
4571da177e4SLinus Torvalds 	.rate_min =          44100,
4581da177e4SLinus Torvalds 	.rate_max =          48000,
4591da177e4SLinus Torvalds 	.channels_min =      8,
4601da177e4SLinus Torvalds 	.channels_max =	     8,
4611da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4621da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4631da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4641da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4651da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4661da177e4SLinus Torvalds 	.fifo_size =         0,
4671da177e4SLinus Torvalds };
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * The CDATA, CCLK and CLATCH bits can be used to write to the SPI interface
4711da177e4SLinus Torvalds  * of the AD1852 or AD1852 D/A converter on the board.  CDATA must be set up
4721da177e4SLinus Torvalds  * on the falling edge of CCLK and be stable on the rising edge.  The rising
4731da177e4SLinus Torvalds  * edge of CLATCH after the last data bit clocks in the whole data word.
4741da177e4SLinus Torvalds  * A fast processor could probably drive the SPI interface faster than the
4751da177e4SLinus Torvalds  * DAC can handle (3MHz for the 1855, unknown for the 1852).  The udelay(1)
4761da177e4SLinus Torvalds  * limits the data rate to 500KHz and only causes a delay of 33 microsecs.
4771da177e4SLinus Torvalds  *
4781da177e4SLinus Torvalds  * NOTE: increased delay from 1 to 10, since there where problems setting
4791da177e4SLinus Torvalds  * the volume.
4801da177e4SLinus Torvalds  */
4811da177e4SLinus Torvalds static void
482a3aefd88STakashi Iwai snd_rme96_write_SPI(struct rme96 *rme96, u16 val)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	int i;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	for (i = 0; i < 16; i++) {
4871da177e4SLinus Torvalds 		if (val & 0x8000) {
4881da177e4SLinus Torvalds 			rme96->areg |= RME96_AR_CDATA;
4891da177e4SLinus Torvalds 		} else {
4901da177e4SLinus Torvalds 			rme96->areg &= ~RME96_AR_CDATA;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CLATCH);
4931da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4941da177e4SLinus Torvalds 		udelay(10);
4951da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_CCLK;
4961da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4971da177e4SLinus Torvalds 		udelay(10);
4981da177e4SLinus Torvalds 		val <<= 1;
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 	rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CDATA);
5011da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_CLATCH;
5021da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5031da177e4SLinus Torvalds 	udelay(10);
5041da177e4SLinus Torvalds 	rme96->areg &= ~RME96_AR_CLATCH;
5051da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds static void
509a3aefd88STakashi Iwai snd_rme96_apply_dac_volume(struct rme96 *rme96)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	if (RME96_DAC_IS_1852(rme96)) {
5121da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] << 2) | 0x0);
5131da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] << 2) | 0x2);
5141da177e4SLinus Torvalds 	} else if (RME96_DAC_IS_1855(rme96)) {
5151da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] & 0x3FF) | 0x000);
5161da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] & 0x3FF) | 0x400);
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds static void
521a3aefd88STakashi Iwai snd_rme96_reset_dac(struct rme96 *rme96)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	writel(rme96->wcreg | RME96_WCR_PD,
5241da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_CONTROL_REGISTER);
5251da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds static int
529a3aefd88STakashi Iwai snd_rme96_getmontracks(struct rme96 *rme96)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_0) & 1) +
5321da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_1) & 1) << 1);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds static int
536a3aefd88STakashi Iwai snd_rme96_setmontracks(struct rme96 *rme96,
5371da177e4SLinus Torvalds 		       int montracks)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	if (montracks & 1) {
5401da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_0;
5411da177e4SLinus Torvalds 	} else {
5421da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_0;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 	if (montracks & 2) {
5451da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_1;
5461da177e4SLinus Torvalds 	} else {
5471da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_1;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5501da177e4SLinus Torvalds 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds static int
554a3aefd88STakashi Iwai snd_rme96_getattenuation(struct rme96 *rme96)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_0) & 1) +
5571da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_1) & 1) << 1);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static int
561a3aefd88STakashi Iwai snd_rme96_setattenuation(struct rme96 *rme96,
5621da177e4SLinus Torvalds 			 int attenuation)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	switch (attenuation) {
5651da177e4SLinus Torvalds 	case 0:
5661da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) &
5671da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5681da177e4SLinus Torvalds 		break;
5691da177e4SLinus Torvalds 	case 1:
5701da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) &
5711da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5721da177e4SLinus Torvalds 		break;
5731da177e4SLinus Torvalds 	case 2:
5741da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) |
5751da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5761da177e4SLinus Torvalds 		break;
5771da177e4SLinus Torvalds 	case 3:
5781da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) |
5791da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5801da177e4SLinus Torvalds 		break;
5811da177e4SLinus Torvalds 	default:
5821da177e4SLinus Torvalds 		return -EINVAL;
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5851da177e4SLinus Torvalds 	return 0;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds static int
589a3aefd88STakashi Iwai snd_rme96_capture_getrate(struct rme96 *rme96,
5901da177e4SLinus Torvalds 			  int *is_adat)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	int n, rate;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	*is_adat = 0;
5951da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
5961da177e4SLinus Torvalds 		/* Analog input, overrides S/PDIF setting */
5971da177e4SLinus Torvalds 		n = ((rme96->areg >> RME96_AR_BITPOS_F0) & 1) +
5981da177e4SLinus Torvalds 			(((rme96->areg >> RME96_AR_BITPOS_F1) & 1) << 1);
5991da177e4SLinus Torvalds 		switch (n) {
6001da177e4SLinus Torvalds 		case 1:
6011da177e4SLinus Torvalds 			rate = 32000;
6021da177e4SLinus Torvalds 			break;
6031da177e4SLinus Torvalds 		case 2:
6041da177e4SLinus Torvalds 			rate = 44100;
6051da177e4SLinus Torvalds 			break;
6061da177e4SLinus Torvalds 		case 3:
6071da177e4SLinus Torvalds 			rate = 48000;
6081da177e4SLinus Torvalds 			break;
6091da177e4SLinus Torvalds 		default:
6101da177e4SLinus Torvalds 			return -1;
6111da177e4SLinus Torvalds 		}
6121da177e4SLinus Torvalds 		return (rme96->areg & RME96_AR_BITPOS_F2) ? rate << 1 : rate;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
6161da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_LOCK) {
6171da177e4SLinus Torvalds 		/* ADAT rate */
6181da177e4SLinus Torvalds 		*is_adat = 1;
6191da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6201da177e4SLinus Torvalds 			return 48000;
6211da177e4SLinus Torvalds 		}
6221da177e4SLinus Torvalds 		return 44100;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_VERF) {
6261da177e4SLinus Torvalds 		return -1;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/* S/PDIF rate */
6301da177e4SLinus Torvalds 	n = ((rme96->rcreg >> RME96_RCR_BITPOS_F0) & 1) +
6311da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F1) & 1) << 1) +
6321da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F2) & 1) << 2);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	switch (n) {
6351da177e4SLinus Torvalds 	case 0:
6361da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6371da177e4SLinus Torvalds 			return 64000;
6381da177e4SLinus Torvalds 		}
6391da177e4SLinus Torvalds 		return -1;
6401da177e4SLinus Torvalds 	case 3: return 96000;
6411da177e4SLinus Torvalds 	case 4: return 88200;
6421da177e4SLinus Torvalds 	case 5: return 48000;
6431da177e4SLinus Torvalds 	case 6: return 44100;
6441da177e4SLinus Torvalds 	case 7: return 32000;
6451da177e4SLinus Torvalds 	default:
6461da177e4SLinus Torvalds 		break;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 	return -1;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static int
652a3aefd88STakashi Iwai snd_rme96_playback_getrate(struct rme96 *rme96)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	int rate, dummy;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
6571da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
6581da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
6591da177e4SLinus Torvalds 	{
6601da177e4SLinus Torvalds 	        /* slave clock */
6611da177e4SLinus Torvalds 	        return rate;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	rate = ((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_0) & 1) +
6641da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_1) & 1) << 1);
6651da177e4SLinus Torvalds 	switch (rate) {
6661da177e4SLinus Torvalds 	case 1:
6671da177e4SLinus Torvalds 		rate = 32000;
6681da177e4SLinus Torvalds 		break;
6691da177e4SLinus Torvalds 	case 2:
6701da177e4SLinus Torvalds 		rate = 44100;
6711da177e4SLinus Torvalds 		break;
6721da177e4SLinus Torvalds 	case 3:
6731da177e4SLinus Torvalds 		rate = 48000;
6741da177e4SLinus Torvalds 		break;
6751da177e4SLinus Torvalds 	default:
6761da177e4SLinus Torvalds 		return -1;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_DS) ? rate << 1 : rate;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds static int
682a3aefd88STakashi Iwai snd_rme96_playback_setrate(struct rme96 *rme96,
6831da177e4SLinus Torvalds 			   int rate)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	int ds;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	ds = rme96->wcreg & RME96_WCR_DS;
6881da177e4SLinus Torvalds 	switch (rate) {
6891da177e4SLinus Torvalds 	case 32000:
6901da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
6911da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
6921da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
6931da177e4SLinus Torvalds 		break;
6941da177e4SLinus Torvalds 	case 44100:
6951da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
6961da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
6971da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
6981da177e4SLinus Torvalds 		break;
6991da177e4SLinus Torvalds 	case 48000:
7001da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
7011da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
7021da177e4SLinus Torvalds 			RME96_WCR_FREQ_1;
7031da177e4SLinus Torvalds 		break;
7041da177e4SLinus Torvalds 	case 64000:
7051da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7061da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
7071da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
7081da177e4SLinus Torvalds 		break;
7091da177e4SLinus Torvalds 	case 88200:
7101da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7111da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
7121da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
7131da177e4SLinus Torvalds 		break;
7141da177e4SLinus Torvalds 	case 96000:
7151da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7161da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
7171da177e4SLinus Torvalds 			RME96_WCR_FREQ_1;
7181da177e4SLinus Torvalds 		break;
7191da177e4SLinus Torvalds 	default:
7201da177e4SLinus Torvalds 		return -EINVAL;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	if ((!ds && rme96->wcreg & RME96_WCR_DS) ||
7231da177e4SLinus Torvalds 	    (ds && !(rme96->wcreg & RME96_WCR_DS)))
7241da177e4SLinus Torvalds 	{
7251da177e4SLinus Torvalds 		/* change to/from double-speed: reset the DAC (if available) */
7261da177e4SLinus Torvalds 		snd_rme96_reset_dac(rme96);
7271da177e4SLinus Torvalds 	} else {
7281da177e4SLinus Torvalds 		writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 	return 0;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static int
734a3aefd88STakashi Iwai snd_rme96_capture_analog_setrate(struct rme96 *rme96,
7351da177e4SLinus Torvalds 				 int rate)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	switch (rate) {
7381da177e4SLinus Torvalds 	case 32000:
7391da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7401da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	case 44100:
7431da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7441da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7451da177e4SLinus Torvalds 		break;
7461da177e4SLinus Torvalds 	case 48000:
7471da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7481da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7491da177e4SLinus Torvalds 		break;
7501da177e4SLinus Torvalds 	case 64000:
7511da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7521da177e4SLinus Torvalds 			return -EINVAL;
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7551da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7561da177e4SLinus Torvalds 		break;
7571da177e4SLinus Torvalds 	case 88200:
7581da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7591da177e4SLinus Torvalds 			return -EINVAL;
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7621da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7631da177e4SLinus Torvalds 		break;
7641da177e4SLinus Torvalds 	case 96000:
7651da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7661da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7671da177e4SLinus Torvalds 		break;
7681da177e4SLinus Torvalds 	default:
7691da177e4SLinus Torvalds 		return -EINVAL;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
7721da177e4SLinus Torvalds 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static int
776a3aefd88STakashi Iwai snd_rme96_setclockmode(struct rme96 *rme96,
7771da177e4SLinus Torvalds 		       int mode)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	switch (mode) {
7801da177e4SLinus Torvalds 	case RME96_CLOCKMODE_SLAVE:
7811da177e4SLinus Torvalds 	        /* AutoSync */
7821da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MASTER;
7831da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
7841da177e4SLinus Torvalds 		break;
7851da177e4SLinus Torvalds 	case RME96_CLOCKMODE_MASTER:
7861da177e4SLinus Torvalds 	        /* Internal */
7871da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
7881da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
7891da177e4SLinus Torvalds 		break;
7901da177e4SLinus Torvalds 	case RME96_CLOCKMODE_WORDCLOCK:
7911da177e4SLinus Torvalds 		/* Word clock is a master mode */
7921da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
7931da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_WSEL;
7941da177e4SLinus Torvalds 		break;
7951da177e4SLinus Torvalds 	default:
7961da177e4SLinus Torvalds 		return -EINVAL;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
7991da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8001da177e4SLinus Torvalds 	return 0;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds static int
804a3aefd88STakashi Iwai snd_rme96_getclockmode(struct rme96 *rme96)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
8071da177e4SLinus Torvalds 		return RME96_CLOCKMODE_WORDCLOCK;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
8101da177e4SLinus Torvalds 		RME96_CLOCKMODE_SLAVE;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds static int
814a3aefd88STakashi Iwai snd_rme96_setinputtype(struct rme96 *rme96,
8151da177e4SLinus Torvalds 		       int type)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	int n;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	switch (type) {
8201da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
8211da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
8221da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8231da177e4SLinus Torvalds 		break;
8241da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
8251da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
8261da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8271da177e4SLinus Torvalds 		break;
8281da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
8291da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
8301da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8311da177e4SLinus Torvalds 		break;
8321da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
8338b7fc421SRoland Dreier 		if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8348b7fc421SRoland Dreier 		     rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
8358b7fc421SRoland Dreier 		    (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8361da177e4SLinus Torvalds 		     rme96->rev > 4))
8371da177e4SLinus Torvalds 		{
8381da177e4SLinus Torvalds 			/* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
8391da177e4SLinus Torvalds 			return -EINVAL;
8401da177e4SLinus Torvalds 		}
8411da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
8421da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8431da177e4SLinus Torvalds 		break;
8441da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
8451da177e4SLinus Torvalds 		if (!RME96_HAS_ANALOG_IN(rme96)) {
8461da177e4SLinus Torvalds 			return -EINVAL;
8471da177e4SLinus Torvalds 		}
8481da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_ANALOG;
8491da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8501da177e4SLinus Torvalds 		if (rme96->rev < 4) {
8511da177e4SLinus Torvalds 			/*
8521da177e4SLinus Torvalds 			 * Revision less than 004 does not support 64 and
8531da177e4SLinus Torvalds 			 * 88.2 kHz
8541da177e4SLinus Torvalds 			 */
8551da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
8561da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 44100);
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
8591da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 32000);
8601da177e4SLinus Torvalds 			}
8611da177e4SLinus Torvalds 		}
8621da177e4SLinus Torvalds 		return 0;
8631da177e4SLinus Torvalds 	default:
8641da177e4SLinus Torvalds 		return -EINVAL;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
8671da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_ANALOG;
8681da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
8711da177e4SLinus Torvalds 	return 0;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds static int
875a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
8781da177e4SLinus Torvalds 		return RME96_INPUT_ANALOG;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
8811da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds static void
885a3aefd88STakashi Iwai snd_rme96_setframelog(struct rme96 *rme96,
8861da177e4SLinus Torvalds 		      int n_channels,
8871da177e4SLinus Torvalds 		      int is_playback)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	int frlog;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (n_channels == 2) {
8921da177e4SLinus Torvalds 		frlog = 1;
8931da177e4SLinus Torvalds 	} else {
8941da177e4SLinus Torvalds 		/* assume 8 channels */
8951da177e4SLinus Torvalds 		frlog = 3;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 	if (is_playback) {
8981da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
8991da177e4SLinus Torvalds 		rme96->playback_frlog = frlog;
9001da177e4SLinus Torvalds 	} else {
9011da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
9021da177e4SLinus Torvalds 		rme96->capture_frlog = frlog;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds static int
907a3aefd88STakashi Iwai snd_rme96_playback_setformat(struct rme96 *rme96,
9081da177e4SLinus Torvalds 			     int format)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	switch (format) {
9111da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9121da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24;
9131da177e4SLinus Torvalds 		break;
9141da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9151da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24;
9161da177e4SLinus Torvalds 		break;
9171da177e4SLinus Torvalds 	default:
9181da177e4SLinus Torvalds 		return -EINVAL;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds static int
925a3aefd88STakashi Iwai snd_rme96_capture_setformat(struct rme96 *rme96,
9261da177e4SLinus Torvalds 			    int format)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	switch (format) {
9291da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9301da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24_2;
9311da177e4SLinus Torvalds 		break;
9321da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9331da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24_2;
9341da177e4SLinus Torvalds 		break;
9351da177e4SLinus Torvalds 	default:
9361da177e4SLinus Torvalds 		return -EINVAL;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds static void
943a3aefd88STakashi Iwai snd_rme96_set_period_properties(struct rme96 *rme96,
9441da177e4SLinus Torvalds 				size_t period_bytes)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	switch (period_bytes) {
9471da177e4SLinus Torvalds 	case RME96_LARGE_BLOCK_SIZE:
9481da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_ISEL;
9491da177e4SLinus Torvalds 		break;
9501da177e4SLinus Torvalds 	case RME96_SMALL_BLOCK_SIZE:
9511da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_ISEL;
9521da177e4SLinus Torvalds 		break;
9531da177e4SLinus Torvalds 	default:
9541da177e4SLinus Torvalds 		snd_BUG();
9551da177e4SLinus Torvalds 		break;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_IDIS;
9581da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds static int
962a3aefd88STakashi Iwai snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
963a3aefd88STakashi Iwai 			     struct snd_pcm_hw_params *params)
9641da177e4SLinus Torvalds {
965a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
966a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
9671da177e4SLinus Torvalds 	int err, rate, dummy;
9681da177e4SLinus Torvalds 
9694d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
9704d23359bSClemens Ladisch 					     RME96_IO_PLAY_BUFFER);
9711da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
9721da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
9751da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
9761da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
9771da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
9781da177e4SLinus Torvalds 	{
9791da177e4SLinus Torvalds                 /* slave clock */
9801da177e4SLinus Torvalds                 if ((int)params_rate(params) != rate) {
9811da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
9821da177e4SLinus Torvalds 			return -EIO;
9831da177e4SLinus Torvalds                 }
9841da177e4SLinus Torvalds 	} else if ((err = snd_rme96_playback_setrate(rme96, params_rate(params))) < 0) {
9851da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
9861da177e4SLinus Torvalds 		return err;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 	if ((err = snd_rme96_playback_setformat(rme96, params_format(params))) < 0) {
9891da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
9901da177e4SLinus Torvalds 		return err;
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 1);
9931da177e4SLinus Torvalds 	if (rme96->capture_periodsize != 0) {
9941da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->playback_frlog !=
9951da177e4SLinus Torvalds 		    rme96->capture_periodsize)
9961da177e4SLinus Torvalds 		{
9971da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
9981da177e4SLinus Torvalds 			return -EBUSY;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	rme96->playback_periodsize =
10021da177e4SLinus Torvalds 		params_period_size(params) << rme96->playback_frlog;
10031da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
10041da177e4SLinus Torvalds 	/* S/PDIF setup */
10051da177e4SLinus Torvalds 	if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
10061da177e4SLinus Torvalds 		rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
10071da177e4SLinus Torvalds 		writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	return 0;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds static int
1015a3aefd88STakashi Iwai snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
1016a3aefd88STakashi Iwai 			    struct snd_pcm_hw_params *params)
10171da177e4SLinus Torvalds {
1018a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1019a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10201da177e4SLinus Torvalds 	int err, isadat, rate;
10211da177e4SLinus Torvalds 
10224d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
10234d23359bSClemens Ladisch 					     RME96_IO_REC_BUFFER);
10241da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
10251da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
10281da177e4SLinus Torvalds 	if ((err = snd_rme96_capture_setformat(rme96, params_format(params))) < 0) {
10291da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
10301da177e4SLinus Torvalds 		return err;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 	if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
10331da177e4SLinus Torvalds 		if ((err = snd_rme96_capture_analog_setrate(rme96,
10341da177e4SLinus Torvalds 							    params_rate(params))) < 0)
10351da177e4SLinus Torvalds 		{
10361da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10371da177e4SLinus Torvalds 			return err;
10381da177e4SLinus Torvalds 		}
10391da177e4SLinus Torvalds 	} else if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
10401da177e4SLinus Torvalds                 if ((int)params_rate(params) != rate) {
10411da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10421da177e4SLinus Torvalds 			return -EIO;
10431da177e4SLinus Torvalds                 }
10441da177e4SLinus Torvalds                 if ((isadat && runtime->hw.channels_min == 2) ||
10451da177e4SLinus Torvalds                     (!isadat && runtime->hw.channels_min == 8))
10461da177e4SLinus Torvalds                 {
10471da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10481da177e4SLinus Torvalds 			return -EIO;
10491da177e4SLinus Torvalds                 }
10501da177e4SLinus Torvalds         }
10511da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 0);
10521da177e4SLinus Torvalds 	if (rme96->playback_periodsize != 0) {
10531da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->capture_frlog !=
10541da177e4SLinus Torvalds 		    rme96->playback_periodsize)
10551da177e4SLinus Torvalds 		{
10561da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10571da177e4SLinus Torvalds 			return -EBUSY;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 	}
10601da177e4SLinus Torvalds 	rme96->capture_periodsize =
10611da177e4SLinus Torvalds 		params_period_size(params) << rme96->capture_frlog;
10621da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
10631da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	return 0;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds static void
1069a3aefd88STakashi Iwai snd_rme96_playback_start(struct rme96 *rme96,
10701da177e4SLinus Torvalds 			 int from_pause)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	if (!from_pause) {
10731da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	rme96->wcreg |= RME96_WCR_START;
10771da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds static void
1081a3aefd88STakashi Iwai snd_rme96_capture_start(struct rme96 *rme96,
10821da177e4SLinus Torvalds 			int from_pause)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	if (!from_pause) {
10851da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	rme96->wcreg |= RME96_WCR_START_2;
10891da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static void
1093a3aefd88STakashi Iwai snd_rme96_playback_stop(struct rme96 *rme96)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	/*
10961da177e4SLinus Torvalds 	 * Check if there is an unconfirmed IRQ, if so confirm it, or else
10971da177e4SLinus Torvalds 	 * the hardware will not stop generating interrupts
10981da177e4SLinus Torvalds 	 */
10991da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
11001da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ) {
11011da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_START;
11041da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static void
1108a3aefd88STakashi Iwai snd_rme96_capture_stop(struct rme96 *rme96)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
11111da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ_2) {
11121da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_START_2;
11151da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds static irqreturn_t
11191da177e4SLinus Torvalds snd_rme96_interrupt(int irq,
1120*7d12e780SDavid Howells 		    void *dev_id)
11211da177e4SLinus Torvalds {
1122a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)dev_id;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
11251da177e4SLinus Torvalds 	/* fastpath out, to ease interrupt sharing */
11261da177e4SLinus Torvalds 	if (!((rme96->rcreg & RME96_RCR_IRQ) ||
11271da177e4SLinus Torvalds 	      (rme96->rcreg & RME96_RCR_IRQ_2)))
11281da177e4SLinus Torvalds 	{
11291da177e4SLinus Torvalds 		return IRQ_NONE;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ) {
11331da177e4SLinus Torvalds 		/* playback */
11341da177e4SLinus Torvalds                 snd_pcm_period_elapsed(rme96->playback_substream);
11351da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ_2) {
11381da177e4SLinus Torvalds 		/* capture */
11391da177e4SLinus Torvalds 		snd_pcm_period_elapsed(rme96->capture_substream);
11401da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 	return IRQ_HANDLED;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds static unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
11461da177e4SLinus Torvalds 
1147a3aefd88STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
11481da177e4SLinus Torvalds 	.count = ARRAY_SIZE(period_bytes),
11491da177e4SLinus Torvalds 	.list = period_bytes,
11501da177e4SLinus Torvalds 	.mask = 0
11511da177e4SLinus Torvalds };
11521da177e4SLinus Torvalds 
11532ce7fb57STakashi Iwai static void
11542ce7fb57STakashi Iwai rme96_set_buffer_size_constraint(struct rme96 *rme96,
11552ce7fb57STakashi Iwai 				 struct snd_pcm_runtime *runtime)
11562ce7fb57STakashi Iwai {
11572ce7fb57STakashi Iwai 	unsigned int size;
11582ce7fb57STakashi Iwai 
11592ce7fb57STakashi Iwai 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
11602ce7fb57STakashi Iwai 				     RME96_BUFFER_SIZE, RME96_BUFFER_SIZE);
11612ce7fb57STakashi Iwai 	if ((size = rme96->playback_periodsize) != 0 ||
11622ce7fb57STakashi Iwai 	    (size = rme96->capture_periodsize) != 0)
11632ce7fb57STakashi Iwai 		snd_pcm_hw_constraint_minmax(runtime,
11642ce7fb57STakashi Iwai 					     SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
11652ce7fb57STakashi Iwai 					     size, size);
11662ce7fb57STakashi Iwai 	else
11672ce7fb57STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
11682ce7fb57STakashi Iwai 					   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
11692ce7fb57STakashi Iwai 					   &hw_constraints_period_bytes);
11702ce7fb57STakashi Iwai }
11712ce7fb57STakashi Iwai 
11721da177e4SLinus Torvalds static int
1173a3aefd88STakashi Iwai snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds         int rate, dummy;
1176a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1177a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
11821da177e4SLinus Torvalds         if (rme96->playback_substream != NULL) {
11831da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
11841da177e4SLinus Torvalds                 return -EBUSY;
11851da177e4SLinus Torvalds         }
11861da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_ADAT;
11871da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11881da177e4SLinus Torvalds 	rme96->playback_substream = substream;
11891da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_spdif_info;
11921da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
11931da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
11941da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
11951da177e4SLinus Torvalds 	{
11961da177e4SLinus Torvalds                 /* slave clock */
11971da177e4SLinus Torvalds                 runtime->hw.rates = snd_rme96_ratecode(rate);
11981da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
11991da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12001da177e4SLinus Torvalds 	}
12012ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
12041da177e4SLinus Torvalds 	rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
12051da177e4SLinus Torvalds 	snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
12061da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static int
1211a3aefd88STakashi Iwai snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds         int isadat, rate;
1214a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1215a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_spdif_info;
12201da177e4SLinus Torvalds         if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
12211da177e4SLinus Torvalds             (rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0)
12221da177e4SLinus Torvalds         {
12231da177e4SLinus Torvalds                 if (isadat) {
12241da177e4SLinus Torvalds                         return -EIO;
12251da177e4SLinus Torvalds                 }
12261da177e4SLinus Torvalds                 runtime->hw.rates = snd_rme96_ratecode(rate);
12271da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12281da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12291da177e4SLinus Torvalds         }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12321da177e4SLinus Torvalds         if (rme96->capture_substream != NULL) {
12331da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12341da177e4SLinus Torvalds                 return -EBUSY;
12351da177e4SLinus Torvalds         }
12361da177e4SLinus Torvalds 	rme96->capture_substream = substream;
12371da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12381da177e4SLinus Torvalds 
12392ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12401da177e4SLinus Torvalds 	return 0;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static int
1244a3aefd88STakashi Iwai snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds         int rate, dummy;
1247a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1248a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12531da177e4SLinus Torvalds         if (rme96->playback_substream != NULL) {
12541da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12551da177e4SLinus Torvalds                 return -EBUSY;
12561da177e4SLinus Torvalds         }
12571da177e4SLinus Torvalds 	rme96->wcreg |= RME96_WCR_ADAT;
12581da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
12591da177e4SLinus Torvalds 	rme96->playback_substream = substream;
12601da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_adat_info;
12631da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
12641da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
12651da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
12661da177e4SLinus Torvalds 	{
12671da177e4SLinus Torvalds                 /* slave clock */
12681da177e4SLinus Torvalds                 runtime->hw.rates = snd_rme96_ratecode(rate);
12691da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12701da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12711da177e4SLinus Torvalds 	}
12722ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12731da177e4SLinus Torvalds 	return 0;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds static int
1277a3aefd88STakashi Iwai snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds         int isadat, rate;
1280a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1281a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_adat_info;
12861da177e4SLinus Torvalds         if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
12871da177e4SLinus Torvalds                 /* makes no sense to use analog input. Note that analog
12881da177e4SLinus Torvalds                    expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
12891da177e4SLinus Torvalds                 return -EIO;
12901da177e4SLinus Torvalds         }
12911da177e4SLinus Torvalds         if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
12921da177e4SLinus Torvalds                 if (!isadat) {
12931da177e4SLinus Torvalds                         return -EIO;
12941da177e4SLinus Torvalds                 }
12951da177e4SLinus Torvalds                 runtime->hw.rates = snd_rme96_ratecode(rate);
12961da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12971da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12981da177e4SLinus Torvalds         }
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13011da177e4SLinus Torvalds         if (rme96->capture_substream != NULL) {
13021da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
13031da177e4SLinus Torvalds                 return -EBUSY;
13041da177e4SLinus Torvalds         }
13051da177e4SLinus Torvalds 	rme96->capture_substream = substream;
13061da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13071da177e4SLinus Torvalds 
13082ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds static int
1313a3aefd88STakashi Iwai snd_rme96_playback_close(struct snd_pcm_substream *substream)
13141da177e4SLinus Torvalds {
1315a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13161da177e4SLinus Torvalds 	int spdif = 0;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13191da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
13201da177e4SLinus Torvalds 		snd_rme96_playback_stop(rme96);
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 	rme96->playback_substream = NULL;
13231da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
13241da177e4SLinus Torvalds 	spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
13251da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13261da177e4SLinus Torvalds 	if (spdif) {
13271da177e4SLinus Torvalds 		rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
13281da177e4SLinus Torvalds 		snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
13291da177e4SLinus Torvalds 			       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 	return 0;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds static int
1335a3aefd88STakashi Iwai snd_rme96_capture_close(struct snd_pcm_substream *substream)
13361da177e4SLinus Torvalds {
1337a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13401da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
13411da177e4SLinus Torvalds 		snd_rme96_capture_stop(rme96);
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 	rme96->capture_substream = NULL;
13441da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
13451da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13461da177e4SLinus Torvalds 	return 0;
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds static int
1350a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
13511da177e4SLinus Torvalds {
1352a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13551da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
13561da177e4SLinus Torvalds 		snd_rme96_playback_stop(rme96);
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
13591da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13601da177e4SLinus Torvalds 	return 0;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds static int
1364a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
13651da177e4SLinus Torvalds {
1366a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13691da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
13701da177e4SLinus Torvalds 		snd_rme96_capture_stop(rme96);
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
13731da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13741da177e4SLinus Torvalds 	return 0;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds static int
1378a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
13791da177e4SLinus Torvalds 			   int cmd)
13801da177e4SLinus Torvalds {
1381a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	switch (cmd) {
13841da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
13851da177e4SLinus Torvalds 		if (!RME96_ISPLAYING(rme96)) {
13861da177e4SLinus Torvalds 			if (substream != rme96->playback_substream) {
13871da177e4SLinus Torvalds 				return -EBUSY;
13881da177e4SLinus Torvalds 			}
13891da177e4SLinus Torvalds 			snd_rme96_playback_start(rme96, 0);
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 		break;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
13941da177e4SLinus Torvalds 		if (RME96_ISPLAYING(rme96)) {
13951da177e4SLinus Torvalds 			if (substream != rme96->playback_substream) {
13961da177e4SLinus Torvalds 				return -EBUSY;
13971da177e4SLinus Torvalds 			}
13981da177e4SLinus Torvalds 			snd_rme96_playback_stop(rme96);
13991da177e4SLinus Torvalds 		}
14001da177e4SLinus Torvalds 		break;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
14031da177e4SLinus Torvalds 		if (RME96_ISPLAYING(rme96)) {
14041da177e4SLinus Torvalds 			snd_rme96_playback_stop(rme96);
14051da177e4SLinus Torvalds 		}
14061da177e4SLinus Torvalds 		break;
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
14091da177e4SLinus Torvalds 		if (!RME96_ISPLAYING(rme96)) {
14101da177e4SLinus Torvalds 			snd_rme96_playback_start(rme96, 1);
14111da177e4SLinus Torvalds 		}
14121da177e4SLinus Torvalds 		break;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	default:
14151da177e4SLinus Torvalds 		return -EINVAL;
14161da177e4SLinus Torvalds 	}
14171da177e4SLinus Torvalds 	return 0;
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds static int
1421a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
14221da177e4SLinus Torvalds 			  int cmd)
14231da177e4SLinus Torvalds {
1424a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	switch (cmd) {
14271da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
14281da177e4SLinus Torvalds 		if (!RME96_ISRECORDING(rme96)) {
14291da177e4SLinus Torvalds 			if (substream != rme96->capture_substream) {
14301da177e4SLinus Torvalds 				return -EBUSY;
14311da177e4SLinus Torvalds 			}
14321da177e4SLinus Torvalds 			snd_rme96_capture_start(rme96, 0);
14331da177e4SLinus Torvalds 		}
14341da177e4SLinus Torvalds 		break;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
14371da177e4SLinus Torvalds 		if (RME96_ISRECORDING(rme96)) {
14381da177e4SLinus Torvalds 			if (substream != rme96->capture_substream) {
14391da177e4SLinus Torvalds 				return -EBUSY;
14401da177e4SLinus Torvalds 			}
14411da177e4SLinus Torvalds 			snd_rme96_capture_stop(rme96);
14421da177e4SLinus Torvalds 		}
14431da177e4SLinus Torvalds 		break;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
14461da177e4SLinus Torvalds 		if (RME96_ISRECORDING(rme96)) {
14471da177e4SLinus Torvalds 			snd_rme96_capture_stop(rme96);
14481da177e4SLinus Torvalds 		}
14491da177e4SLinus Torvalds 		break;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
14521da177e4SLinus Torvalds 		if (!RME96_ISRECORDING(rme96)) {
14531da177e4SLinus Torvalds 			snd_rme96_capture_start(rme96, 1);
14541da177e4SLinus Torvalds 		}
14551da177e4SLinus Torvalds 		break;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	default:
14581da177e4SLinus Torvalds 		return -EINVAL;
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	return 0;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds static snd_pcm_uframes_t
1465a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
14661da177e4SLinus Torvalds {
1467a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14681da177e4SLinus Torvalds 	return snd_rme96_playback_ptr(rme96);
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds static snd_pcm_uframes_t
1472a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
14731da177e4SLinus Torvalds {
1474a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14751da177e4SLinus Torvalds 	return snd_rme96_capture_ptr(rme96);
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
1478a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
14791da177e4SLinus Torvalds 	.open =		snd_rme96_playback_spdif_open,
14801da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
14811da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
14821da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
14831da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
14841da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
14851da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
14861da177e4SLinus Torvalds 	.copy =		snd_rme96_playback_copy,
14871da177e4SLinus Torvalds 	.silence =	snd_rme96_playback_silence,
14881da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
14891da177e4SLinus Torvalds };
14901da177e4SLinus Torvalds 
1491a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
14921da177e4SLinus Torvalds 	.open =		snd_rme96_capture_spdif_open,
14931da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
14941da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
14951da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
14961da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
14971da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
14981da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
14991da177e4SLinus Torvalds 	.copy =		snd_rme96_capture_copy,
15001da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15011da177e4SLinus Torvalds };
15021da177e4SLinus Torvalds 
1503a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_playback_adat_ops = {
15041da177e4SLinus Torvalds 	.open =		snd_rme96_playback_adat_open,
15051da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
15061da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15071da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
15081da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
15091da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
15101da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
15111da177e4SLinus Torvalds 	.copy =		snd_rme96_playback_copy,
15121da177e4SLinus Torvalds 	.silence =	snd_rme96_playback_silence,
15131da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15141da177e4SLinus Torvalds };
15151da177e4SLinus Torvalds 
1516a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_capture_adat_ops = {
15171da177e4SLinus Torvalds 	.open =		snd_rme96_capture_adat_open,
15181da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
15191da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15201da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
15211da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
15221da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
15231da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
15241da177e4SLinus Torvalds 	.copy =		snd_rme96_capture_copy,
15251da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15261da177e4SLinus Torvalds };
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds static void
15291da177e4SLinus Torvalds snd_rme96_free(void *private_data)
15301da177e4SLinus Torvalds {
1531a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)private_data;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	if (rme96 == NULL) {
15341da177e4SLinus Torvalds 	        return;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 	if (rme96->irq >= 0) {
15371da177e4SLinus Torvalds 		snd_rme96_playback_stop(rme96);
15381da177e4SLinus Torvalds 		snd_rme96_capture_stop(rme96);
15391da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_DAC_EN;
15401da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
15411da177e4SLinus Torvalds 		free_irq(rme96->irq, (void *)rme96);
15421da177e4SLinus Torvalds 		rme96->irq = -1;
15431da177e4SLinus Torvalds 	}
15441da177e4SLinus Torvalds 	if (rme96->iobase) {
15451da177e4SLinus Torvalds 		iounmap(rme96->iobase);
15461da177e4SLinus Torvalds 		rme96->iobase = NULL;
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 	if (rme96->port) {
15491da177e4SLinus Torvalds 		pci_release_regions(rme96->pci);
15501da177e4SLinus Torvalds 		rme96->port = 0;
15511da177e4SLinus Torvalds 	}
15521da177e4SLinus Torvalds 	pci_disable_device(rme96->pci);
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds static void
1556a3aefd88STakashi Iwai snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
15571da177e4SLinus Torvalds {
1558a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *) pcm->private_data;
15591da177e4SLinus Torvalds 	rme96->spdif_pcm = NULL;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds static void
1563a3aefd88STakashi Iwai snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
15641da177e4SLinus Torvalds {
1565a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *) pcm->private_data;
15661da177e4SLinus Torvalds 	rme96->adat_pcm = NULL;
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds static int __devinit
1570a3aefd88STakashi Iwai snd_rme96_create(struct rme96 *rme96)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	struct pci_dev *pci = rme96->pci;
15731da177e4SLinus Torvalds 	int err;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	rme96->irq = -1;
15761da177e4SLinus Torvalds 	spin_lock_init(&rme96->lock);
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
15791da177e4SLinus Torvalds 		return err;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "RME96")) < 0)
15821da177e4SLinus Torvalds 		return err;
15831da177e4SLinus Torvalds 	rme96->port = pci_resource_start(rme96->pci, 0);
15841da177e4SLinus Torvalds 
1585688956f2STakashi Iwai 	if ((rme96->iobase = ioremap_nocache(rme96->port, RME96_IO_SIZE)) == 0) {
1586688956f2STakashi Iwai 		snd_printk(KERN_ERR "unable to remap memory region 0x%lx-0x%lx\n", rme96->port, rme96->port + RME96_IO_SIZE - 1);
1587688956f2STakashi Iwai 		return -ENOMEM;
1588688956f2STakashi Iwai 	}
1589688956f2STakashi Iwai 
159065ca68b3SThomas Gleixner 	if (request_irq(pci->irq, snd_rme96_interrupt, IRQF_DISABLED|IRQF_SHARED, "RME96", (void *)rme96)) {
159199b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
15921da177e4SLinus Torvalds 		return -EBUSY;
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 	rme96->irq = pci->irq;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/* read the card's revision number */
15971da177e4SLinus Torvalds 	pci_read_config_byte(pci, 8, &rme96->rev);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/* set up ALSA pcm device for S/PDIF */
16001da177e4SLinus Torvalds 	if ((err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
16011da177e4SLinus Torvalds 			       1, 1, &rme96->spdif_pcm)) < 0)
16021da177e4SLinus Torvalds 	{
16031da177e4SLinus Torvalds 		return err;
16041da177e4SLinus Torvalds 	}
16051da177e4SLinus Torvalds 	rme96->spdif_pcm->private_data = rme96;
16061da177e4SLinus Torvalds 	rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
16071da177e4SLinus Torvalds 	strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
16081da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
16091da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	rme96->spdif_pcm->info_flags = 0;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	/* set up ALSA pcm device for ADAT */
16148b7fc421SRoland Dreier 	if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
16151da177e4SLinus Torvalds 		/* ADAT is not available on the base model */
16161da177e4SLinus Torvalds 		rme96->adat_pcm = NULL;
16171da177e4SLinus Torvalds 	} else {
16181da177e4SLinus Torvalds 		if ((err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
16191da177e4SLinus Torvalds 				       1, 1, &rme96->adat_pcm)) < 0)
16201da177e4SLinus Torvalds 		{
16211da177e4SLinus Torvalds 			return err;
16221da177e4SLinus Torvalds 		}
16231da177e4SLinus Torvalds 		rme96->adat_pcm->private_data = rme96;
16241da177e4SLinus Torvalds 		rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
16251da177e4SLinus Torvalds 		strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
16261da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
16271da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 		rme96->adat_pcm->info_flags = 0;
16301da177e4SLinus Torvalds 	}
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
16331da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	/* make sure playback/capture is stopped, if by some reason active */
16361da177e4SLinus Torvalds 	snd_rme96_playback_stop(rme96);
16371da177e4SLinus Torvalds 	snd_rme96_capture_stop(rme96);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	/* set default values in registers */
16401da177e4SLinus Torvalds 	rme96->wcreg =
16411da177e4SLinus Torvalds 		RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
16421da177e4SLinus Torvalds 		RME96_WCR_SEL |    /* normal playback */
16431da177e4SLinus Torvalds 		RME96_WCR_MASTER | /* set to master clock mode */
16441da177e4SLinus Torvalds 		RME96_WCR_INP_0;   /* set coaxial input */
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
16491da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	/* reset the ADC */
16521da177e4SLinus Torvalds 	writel(rme96->areg | RME96_AR_PD2,
16531da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_ADDITIONAL_REG);
16541da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/* reset and enable the DAC (order is important). */
16571da177e4SLinus Torvalds 	snd_rme96_reset_dac(rme96);
16581da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_DAC_EN;
16591da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	/* reset playback and record buffer pointers */
16621da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
16631da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	/* reset volume */
16661da177e4SLinus Torvalds 	rme96->vol[0] = rme96->vol[1] = 0;
16671da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_OUT(rme96)) {
16681da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	/* init switch interface */
16721da177e4SLinus Torvalds 	if ((err = snd_rme96_create_switches(rme96->card, rme96)) < 0) {
16731da177e4SLinus Torvalds 		return err;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds         /* init proc interface */
16771da177e4SLinus Torvalds 	snd_rme96_proc_init(rme96);
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	return 0;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds /*
16831da177e4SLinus Torvalds  * proc interface
16841da177e4SLinus Torvalds  */
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds static void
1687a3aefd88STakashi Iwai snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	int n;
1690a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)entry->private_data;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	snd_iprintf(buffer, rme96->card->longname);
16951da177e4SLinus Torvalds 	snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nGeneral settings\n");
16981da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_IDIS) {
16991da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: N/A (interrupts "
17001da177e4SLinus Torvalds 			    "disabled)\n");
17011da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_ISEL) {
17021da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 2048 bytes\n");
17031da177e4SLinus Torvalds 	} else {
17041da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 8192 bytes\n");
17051da177e4SLinus Torvalds 	}
17061da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nInput settings\n");
17071da177e4SLinus Torvalds 	switch (snd_rme96_getinputtype(rme96)) {
17081da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
17091da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: optical");
17101da177e4SLinus Torvalds 		break;
17111da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
17121da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: coaxial");
17131da177e4SLinus Torvalds 		break;
17141da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
17151da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: internal");
17161da177e4SLinus Torvalds 		break;
17171da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
17181da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: XLR");
17191da177e4SLinus Torvalds 		break;
17201da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
17211da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: analog");
17221da177e4SLinus Torvalds 		break;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 	if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17251da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n  sample rate: no valid signal\n");
17261da177e4SLinus Torvalds 	} else {
17271da177e4SLinus Torvalds 		if (n) {
17281da177e4SLinus Torvalds 			snd_iprintf(buffer, " (8 channels)\n");
17291da177e4SLinus Torvalds 		} else {
17301da177e4SLinus Torvalds 			snd_iprintf(buffer, " (2 channels)\n");
17311da177e4SLinus Torvalds 		}
17321da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample rate: %d Hz\n",
17331da177e4SLinus Torvalds 			    snd_rme96_capture_getrate(rme96, &n));
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24_2) {
17361da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17371da177e4SLinus Torvalds 	} else {
17381da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nOutput settings\n");
17421da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_SEL) {
17431da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: normal playback\n");
17441da177e4SLinus Torvalds 	} else {
17451da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: same as input\n");
17461da177e4SLinus Torvalds 	}
17471da177e4SLinus Torvalds 	snd_iprintf(buffer, "  sample rate: %d Hz\n",
17481da177e4SLinus Torvalds 		    snd_rme96_playback_getrate(rme96));
17491da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24) {
17501da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17511da177e4SLinus Torvalds 	} else {
17521da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
17551da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: word clock\n");
17561da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_MASTER) {
17571da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: internal\n");
17581da177e4SLinus Torvalds 	} else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
17591da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to analog input setting)\n");
17601da177e4SLinus Torvalds 	} else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17611da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to no valid signal)\n");
17621da177e4SLinus Torvalds 	} else {
17631da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync\n");
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_PRO) {
17661da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: AES/EBU (professional)\n");
17671da177e4SLinus Torvalds 	} else {
17681da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: IEC958 (consumer)\n");
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_EMP) {
17711da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: on\n");
17721da177e4SLinus Torvalds 	} else {
17731da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: off\n");
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_DOLBY) {
17761da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): on\n");
17771da177e4SLinus Torvalds 	} else {
17781da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): off\n");
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_IN(rme96)) {
17811da177e4SLinus Torvalds 		snd_iprintf(buffer, "\nAnalog output settings\n");
17821da177e4SLinus Torvalds 		switch (snd_rme96_getmontracks(rme96)) {
17831da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_1_2:
17841da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 1+2\n");
17851da177e4SLinus Torvalds 			break;
17861da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_3_4:
17871da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 3+4\n");
17881da177e4SLinus Torvalds 			break;
17891da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_5_6:
17901da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 5+6\n");
17911da177e4SLinus Torvalds 			break;
17921da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_7_8:
17931da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 7+8\n");
17941da177e4SLinus Torvalds 			break;
17951da177e4SLinus Torvalds 		}
17961da177e4SLinus Torvalds 		switch (snd_rme96_getattenuation(rme96)) {
17971da177e4SLinus Torvalds 		case RME96_ATTENUATION_0:
17981da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: 0 dB\n");
17991da177e4SLinus Torvalds 			break;
18001da177e4SLinus Torvalds 		case RME96_ATTENUATION_6:
18011da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -6 dB\n");
18021da177e4SLinus Torvalds 			break;
18031da177e4SLinus Torvalds 		case RME96_ATTENUATION_12:
18041da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -12 dB\n");
18051da177e4SLinus Torvalds 			break;
18061da177e4SLinus Torvalds 		case RME96_ATTENUATION_18:
18071da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -18 dB\n");
18081da177e4SLinus Torvalds 			break;
18091da177e4SLinus Torvalds 		}
18101da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume left: %u\n", rme96->vol[0]);
18111da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume right: %u\n", rme96->vol[1]);
18121da177e4SLinus Torvalds 	}
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds static void __devinit
1816a3aefd88STakashi Iwai snd_rme96_proc_init(struct rme96 *rme96)
18171da177e4SLinus Torvalds {
1818a3aefd88STakashi Iwai 	struct snd_info_entry *entry;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (! snd_card_proc_new(rme96->card, "rme96", &entry))
1821bf850204STakashi Iwai 		snd_info_set_text_ops(entry, rme96, snd_rme96_proc_read);
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  * control interface
18261da177e4SLinus Torvalds  */
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds static int
1829a3aefd88STakashi Iwai snd_rme96_info_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
18321da177e4SLinus Torvalds 	uinfo->count = 1;
18331da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
18341da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
18351da177e4SLinus Torvalds 	return 0;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds static int
1838a3aefd88STakashi Iwai snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18391da177e4SLinus Torvalds {
1840a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18431da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
18441da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18451da177e4SLinus Torvalds 	return 0;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds static int
1848a3aefd88STakashi Iwai snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18491da177e4SLinus Torvalds {
1850a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18511da177e4SLinus Torvalds 	unsigned int val;
18521da177e4SLinus Torvalds 	int change;
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
18551da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18561da177e4SLinus Torvalds 	val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
18571da177e4SLinus Torvalds 	change = val != rme96->wcreg;
18581da177e4SLinus Torvalds 	rme96->wcreg = val;
18591da177e4SLinus Torvalds 	writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
18601da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18611da177e4SLinus Torvalds 	return change;
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds static int
1865a3aefd88STakashi Iwai snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	static char *_texts[5] = { "Optical", "Coaxial", "Internal", "XLR", "Analog" };
1868a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18691da177e4SLinus Torvalds 	char *texts[5] = { _texts[0], _texts[1], _texts[2], _texts[3], _texts[4] };
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
18721da177e4SLinus Torvalds 	uinfo->count = 1;
18731da177e4SLinus Torvalds 	switch (rme96->pci->device) {
18748b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
18758b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
18761da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 3;
18771da177e4SLinus Torvalds 		break;
18788b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
18791da177e4SLinus Torvalds 		uinfo->value.enumerated.items = 4;
18801da177e4SLinus Torvalds 		break;
18818b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
18821da177e4SLinus Torvalds 		if (rme96->rev > 4) {
18831da177e4SLinus Torvalds 			/* PST */
18841da177e4SLinus Torvalds 			uinfo->value.enumerated.items = 4;
18851da177e4SLinus Torvalds 			texts[3] = _texts[4]; /* Analog instead of XLR */
18861da177e4SLinus Torvalds 		} else {
18871da177e4SLinus Torvalds 			/* PAD */
18881da177e4SLinus Torvalds 			uinfo->value.enumerated.items = 5;
18891da177e4SLinus Torvalds 		}
18901da177e4SLinus Torvalds 		break;
18911da177e4SLinus Torvalds 	default:
18921da177e4SLinus Torvalds 		snd_BUG();
18931da177e4SLinus Torvalds 		break;
18941da177e4SLinus Torvalds 	}
18951da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > uinfo->value.enumerated.items - 1) {
18961da177e4SLinus Torvalds 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
18991da177e4SLinus Torvalds 	return 0;
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds static int
1902a3aefd88STakashi Iwai snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19031da177e4SLinus Torvalds {
1904a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19051da177e4SLinus Torvalds 	unsigned int items = 3;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19081da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19118b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19128b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19131da177e4SLinus Torvalds 		items = 3;
19141da177e4SLinus Torvalds 		break;
19158b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19161da177e4SLinus Torvalds 		items = 4;
19171da177e4SLinus Torvalds 		break;
19188b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19191da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19201da177e4SLinus Torvalds 			/* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
19211da177e4SLinus Torvalds 			if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
19221da177e4SLinus Torvalds 				ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
19231da177e4SLinus Torvalds 			}
19241da177e4SLinus Torvalds 			items = 4;
19251da177e4SLinus Torvalds 		} else {
19261da177e4SLinus Torvalds 			items = 5;
19271da177e4SLinus Torvalds 		}
19281da177e4SLinus Torvalds 		break;
19291da177e4SLinus Torvalds 	default:
19301da177e4SLinus Torvalds 		snd_BUG();
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 	}
19331da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0] >= items) {
19341da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = items - 1;
19351da177e4SLinus Torvalds 	}
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19381da177e4SLinus Torvalds 	return 0;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds static int
1941a3aefd88STakashi Iwai snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19421da177e4SLinus Torvalds {
1943a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19441da177e4SLinus Torvalds 	unsigned int val;
19451da177e4SLinus Torvalds 	int change, items = 3;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19488b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19498b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19501da177e4SLinus Torvalds 		items = 3;
19511da177e4SLinus Torvalds 		break;
19528b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19531da177e4SLinus Torvalds 		items = 4;
19541da177e4SLinus Torvalds 		break;
19558b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19561da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19571da177e4SLinus Torvalds 			items = 4;
19581da177e4SLinus Torvalds 		} else {
19591da177e4SLinus Torvalds 			items = 5;
19601da177e4SLinus Torvalds 		}
19611da177e4SLinus Torvalds 		break;
19621da177e4SLinus Torvalds 	default:
19631da177e4SLinus Torvalds 		snd_BUG();
19641da177e4SLinus Torvalds 		break;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % items;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	/* special case for PST */
19698b7fc421SRoland Dreier 	if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
19701da177e4SLinus Torvalds 		if (val == RME96_INPUT_XLR) {
19711da177e4SLinus Torvalds 			val = RME96_INPUT_ANALOG;
19721da177e4SLinus Torvalds 		}
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19761da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getinputtype(rme96);
19771da177e4SLinus Torvalds 	snd_rme96_setinputtype(rme96, val);
19781da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19791da177e4SLinus Torvalds 	return change;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds static int
1983a3aefd88STakashi Iwai snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	static char *texts[3] = { "AutoSync", "Internal", "Word" };
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
19881da177e4SLinus Torvalds 	uinfo->count = 1;
19891da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 3;
19901da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 2) {
19911da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 2;
19921da177e4SLinus Torvalds 	}
19931da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
19941da177e4SLinus Torvalds 	return 0;
19951da177e4SLinus Torvalds }
19961da177e4SLinus Torvalds static int
1997a3aefd88STakashi Iwai snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19981da177e4SLinus Torvalds {
1999a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20021da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
20031da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20041da177e4SLinus Torvalds 	return 0;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds static int
2007a3aefd88STakashi Iwai snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20081da177e4SLinus Torvalds {
2009a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20101da177e4SLinus Torvalds 	unsigned int val;
20111da177e4SLinus Torvalds 	int change;
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 3;
20141da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20151da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getclockmode(rme96);
20161da177e4SLinus Torvalds 	snd_rme96_setclockmode(rme96, val);
20171da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20181da177e4SLinus Torvalds 	return change;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds static int
2022a3aefd88STakashi Iwai snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20231da177e4SLinus Torvalds {
20241da177e4SLinus Torvalds 	static char *texts[4] = { "0 dB", "-6 dB", "-12 dB", "-18 dB" };
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
20271da177e4SLinus Torvalds 	uinfo->count = 1;
20281da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 4;
20291da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 3) {
20301da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 3;
20311da177e4SLinus Torvalds 	}
20321da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
20331da177e4SLinus Torvalds 	return 0;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds static int
2036a3aefd88STakashi Iwai snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20371da177e4SLinus Torvalds {
2038a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20411da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
20421da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20431da177e4SLinus Torvalds 	return 0;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds static int
2046a3aefd88STakashi Iwai snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20471da177e4SLinus Torvalds {
2048a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20491da177e4SLinus Torvalds 	unsigned int val;
20501da177e4SLinus Torvalds 	int change;
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20531da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getattenuation(rme96);
20561da177e4SLinus Torvalds 	snd_rme96_setattenuation(rme96, val);
20571da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20581da177e4SLinus Torvalds 	return change;
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds static int
2062a3aefd88STakashi Iwai snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20631da177e4SLinus Torvalds {
20641da177e4SLinus Torvalds 	static char *texts[4] = { "1+2", "3+4", "5+6", "7+8" };
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
20671da177e4SLinus Torvalds 	uinfo->count = 1;
20681da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 4;
20691da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 3) {
20701da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 3;
20711da177e4SLinus Torvalds 	}
20721da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
20731da177e4SLinus Torvalds 	return 0;
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds static int
2076a3aefd88STakashi Iwai snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20771da177e4SLinus Torvalds {
2078a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20811da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
20821da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20831da177e4SLinus Torvalds 	return 0;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds static int
2086a3aefd88STakashi Iwai snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20871da177e4SLinus Torvalds {
2088a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20891da177e4SLinus Torvalds 	unsigned int val;
20901da177e4SLinus Torvalds 	int change;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20931da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20941da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getmontracks(rme96);
20951da177e4SLinus Torvalds 	snd_rme96_setmontracks(rme96, val);
20961da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20971da177e4SLinus Torvalds 	return change;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
2100a3aefd88STakashi Iwai static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
21011da177e4SLinus Torvalds {
21021da177e4SLinus Torvalds 	u32 val = 0;
21031da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
21041da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
21051da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
21061da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
21071da177e4SLinus Torvalds 	else
21081da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
21091da177e4SLinus Torvalds 	return val;
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds 
2112a3aefd88STakashi Iwai static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
21131da177e4SLinus Torvalds {
21141da177e4SLinus Torvalds 	aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
21151da177e4SLinus Torvalds 			 ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
21161da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
21171da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
21181da177e4SLinus Torvalds 	else
21191da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
2122a3aefd88STakashi Iwai static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21251da177e4SLinus Torvalds 	uinfo->count = 1;
21261da177e4SLinus Torvalds 	return 0;
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
2129a3aefd88STakashi Iwai static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21301da177e4SLinus Torvalds {
2131a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
21341da177e4SLinus Torvalds 	return 0;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
2137a3aefd88STakashi Iwai static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21381da177e4SLinus Torvalds {
2139a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21401da177e4SLinus Torvalds 	int change;
21411da177e4SLinus Torvalds 	u32 val;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21441da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21451da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif;
21461da177e4SLinus Torvalds 	rme96->wcreg_spdif = val;
21471da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21481da177e4SLinus Torvalds 	return change;
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
2151a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21521da177e4SLinus Torvalds {
21531da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21541da177e4SLinus Torvalds 	uinfo->count = 1;
21551da177e4SLinus Torvalds 	return 0;
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
2158a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21591da177e4SLinus Torvalds {
2160a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
21631da177e4SLinus Torvalds 	return 0;
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
2166a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21671da177e4SLinus Torvalds {
2168a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21691da177e4SLinus Torvalds 	int change;
21701da177e4SLinus Torvalds 	u32 val;
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21731da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21741da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif_stream;
21751da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = val;
21761da177e4SLinus Torvalds 	rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
21771da177e4SLinus Torvalds 	rme96->wcreg |= val;
21781da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
21791da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21801da177e4SLinus Torvalds 	return change;
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
2183a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21841da177e4SLinus Torvalds {
21851da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21861da177e4SLinus Torvalds 	uinfo->count = 1;
21871da177e4SLinus Torvalds 	return 0;
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
2190a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
21931da177e4SLinus Torvalds 	return 0;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds static int
2197a3aefd88STakashi Iwai snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21981da177e4SLinus Torvalds {
2199a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
22021da177e4SLinus Torvalds         uinfo->count = 2;
22031da177e4SLinus Torvalds         uinfo->value.integer.min = 0;
22041da177e4SLinus Torvalds 	uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
22051da177e4SLinus Torvalds         return 0;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds static int
2209a3aefd88STakashi Iwai snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
22101da177e4SLinus Torvalds {
2211a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
22141da177e4SLinus Torvalds         u->value.integer.value[0] = rme96->vol[0];
22151da177e4SLinus Torvalds         u->value.integer.value[1] = rme96->vol[1];
22161da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds         return 0;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static int
2222a3aefd88STakashi Iwai snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
22231da177e4SLinus Torvalds {
2224a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
22251da177e4SLinus Torvalds         int change = 0;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	if (!RME96_HAS_ANALOG_OUT(rme96)) {
22281da177e4SLinus Torvalds 		return -EINVAL;
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
22311da177e4SLinus Torvalds         if (u->value.integer.value[0] != rme96->vol[0]) {
22321da177e4SLinus Torvalds 		rme96->vol[0] = u->value.integer.value[0];
22331da177e4SLinus Torvalds                 change = 1;
22341da177e4SLinus Torvalds         }
22351da177e4SLinus Torvalds         if (u->value.integer.value[1] != rme96->vol[1]) {
22361da177e4SLinus Torvalds 		rme96->vol[1] = u->value.integer.value[1];
22371da177e4SLinus Torvalds                 change = 1;
22381da177e4SLinus Torvalds         }
22391da177e4SLinus Torvalds 	if (change) {
22401da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
22411da177e4SLinus Torvalds 	}
22421da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds         return change;
22451da177e4SLinus Torvalds }
22461da177e4SLinus Torvalds 
2247a3aefd88STakashi Iwai static struct snd_kcontrol_new snd_rme96_controls[] = {
22481da177e4SLinus Torvalds {
22491da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22501da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
22511da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_info,
22521da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_get,
22531da177e4SLinus Torvalds 	.put =		snd_rme96_control_spdif_put
22541da177e4SLinus Torvalds },
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
22571da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22581da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
22591da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_stream_info,
22601da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_stream_get,
22611da177e4SLinus Torvalds 	.put =		snd_rme96_control_spdif_stream_put
22621da177e4SLinus Torvalds },
22631da177e4SLinus Torvalds {
22641da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
226567ed4161SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22661da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
22671da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_mask_info,
22681da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_mask_get,
22691da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
22701da177e4SLinus Torvalds 			IEC958_AES0_PROFESSIONAL |
22711da177e4SLinus Torvalds 			IEC958_AES0_CON_EMPHASIS
22721da177e4SLinus Torvalds },
22731da177e4SLinus Torvalds {
22741da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
227567ed4161SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22761da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
22771da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_mask_info,
22781da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_mask_get,
22791da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
22801da177e4SLinus Torvalds 			IEC958_AES0_PROFESSIONAL |
22811da177e4SLinus Torvalds 			IEC958_AES0_PRO_EMPHASIS
22821da177e4SLinus Torvalds },
22831da177e4SLinus Torvalds {
22841da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22851da177e4SLinus Torvalds 	.name =         "Input Connector",
22861da177e4SLinus Torvalds 	.info =         snd_rme96_info_inputtype_control,
22871da177e4SLinus Torvalds 	.get =          snd_rme96_get_inputtype_control,
22881da177e4SLinus Torvalds 	.put =          snd_rme96_put_inputtype_control
22891da177e4SLinus Torvalds },
22901da177e4SLinus Torvalds {
22911da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22921da177e4SLinus Torvalds 	.name =         "Loopback Input",
22931da177e4SLinus Torvalds 	.info =         snd_rme96_info_loopback_control,
22941da177e4SLinus Torvalds 	.get =          snd_rme96_get_loopback_control,
22951da177e4SLinus Torvalds 	.put =          snd_rme96_put_loopback_control
22961da177e4SLinus Torvalds },
22971da177e4SLinus Torvalds {
22981da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22991da177e4SLinus Torvalds 	.name =         "Sample Clock Source",
23001da177e4SLinus Torvalds 	.info =         snd_rme96_info_clockmode_control,
23011da177e4SLinus Torvalds 	.get =          snd_rme96_get_clockmode_control,
23021da177e4SLinus Torvalds 	.put =          snd_rme96_put_clockmode_control
23031da177e4SLinus Torvalds },
23041da177e4SLinus Torvalds {
23051da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
23061da177e4SLinus Torvalds 	.name =         "Monitor Tracks",
23071da177e4SLinus Torvalds 	.info =         snd_rme96_info_montracks_control,
23081da177e4SLinus Torvalds 	.get =          snd_rme96_get_montracks_control,
23091da177e4SLinus Torvalds 	.put =          snd_rme96_put_montracks_control
23101da177e4SLinus Torvalds },
23111da177e4SLinus Torvalds {
23121da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
23131da177e4SLinus Torvalds 	.name =         "Attenuation",
23141da177e4SLinus Torvalds 	.info =         snd_rme96_info_attenuation_control,
23151da177e4SLinus Torvalds 	.get =          snd_rme96_get_attenuation_control,
23161da177e4SLinus Torvalds 	.put =          snd_rme96_put_attenuation_control
23171da177e4SLinus Torvalds },
23181da177e4SLinus Torvalds {
23191da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
23201da177e4SLinus Torvalds 	.name =         "DAC Playback Volume",
23211da177e4SLinus Torvalds 	.info =         snd_rme96_dac_volume_info,
23221da177e4SLinus Torvalds 	.get =          snd_rme96_dac_volume_get,
23231da177e4SLinus Torvalds 	.put =          snd_rme96_dac_volume_put
23241da177e4SLinus Torvalds }
23251da177e4SLinus Torvalds };
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds static int
2328a3aefd88STakashi Iwai snd_rme96_create_switches(struct snd_card *card,
2329a3aefd88STakashi Iwai 			  struct rme96 *rme96)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	int idx, err;
2332a3aefd88STakashi Iwai 	struct snd_kcontrol *kctl;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	for (idx = 0; idx < 7; idx++) {
23351da177e4SLinus Torvalds 		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
23361da177e4SLinus Torvalds 			return err;
23371da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
23381da177e4SLinus Torvalds 			rme96->spdif_ctl = kctl;
23391da177e4SLinus Torvalds 	}
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_OUT(rme96)) {
23421da177e4SLinus Torvalds 		for (idx = 7; idx < 10; idx++)
23431da177e4SLinus Torvalds 			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_rme96_controls[idx], rme96))) < 0)
23441da177e4SLinus Torvalds 				return err;
23451da177e4SLinus Torvalds 	}
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	return 0;
23481da177e4SLinus Torvalds }
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds /*
23511da177e4SLinus Torvalds  * Card initialisation
23521da177e4SLinus Torvalds  */
23531da177e4SLinus Torvalds 
2354a3aefd88STakashi Iwai static void snd_rme96_card_free(struct snd_card *card)
23551da177e4SLinus Torvalds {
23561da177e4SLinus Torvalds 	snd_rme96_free(card->private_data);
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds static int __devinit
23601da177e4SLinus Torvalds snd_rme96_probe(struct pci_dev *pci,
23611da177e4SLinus Torvalds 		const struct pci_device_id *pci_id)
23621da177e4SLinus Torvalds {
23631da177e4SLinus Torvalds 	static int dev;
2364a3aefd88STakashi Iwai 	struct rme96 *rme96;
2365a3aefd88STakashi Iwai 	struct snd_card *card;
23661da177e4SLinus Torvalds 	int err;
23671da177e4SLinus Torvalds 	u8 val;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS) {
23701da177e4SLinus Torvalds 		return -ENODEV;
23711da177e4SLinus Torvalds 	}
23721da177e4SLinus Torvalds 	if (!enable[dev]) {
23731da177e4SLinus Torvalds 		dev++;
23741da177e4SLinus Torvalds 		return -ENOENT;
23751da177e4SLinus Torvalds 	}
23761da177e4SLinus Torvalds 	if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
2377a3aefd88STakashi Iwai 				 sizeof(struct rme96))) == NULL)
23781da177e4SLinus Torvalds 		return -ENOMEM;
23791da177e4SLinus Torvalds 	card->private_free = snd_rme96_card_free;
2380a3aefd88STakashi Iwai 	rme96 = (struct rme96 *)card->private_data;
23811da177e4SLinus Torvalds 	rme96->card = card;
23821da177e4SLinus Torvalds 	rme96->pci = pci;
23831da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
23841da177e4SLinus Torvalds 	if ((err = snd_rme96_create(rme96)) < 0) {
23851da177e4SLinus Torvalds 		snd_card_free(card);
23861da177e4SLinus Torvalds 		return err;
23871da177e4SLinus Torvalds 	}
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	strcpy(card->driver, "Digi96");
23901da177e4SLinus Torvalds 	switch (rme96->pci->device) {
23918b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
23921da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96");
23931da177e4SLinus Torvalds 		break;
23948b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
23951da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8");
23961da177e4SLinus Torvalds 		break;
23978b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
23981da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8 PRO");
23991da177e4SLinus Torvalds 		break;
24008b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
24011da177e4SLinus Torvalds 		pci_read_config_byte(rme96->pci, 8, &val);
24021da177e4SLinus Torvalds 		if (val < 5) {
24031da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PAD");
24041da177e4SLinus Torvalds 		} else {
24051da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PST");
24061da177e4SLinus Torvalds 		}
24071da177e4SLinus Torvalds 		break;
24081da177e4SLinus Torvalds 	}
24091da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
24101da177e4SLinus Torvalds 		rme96->port, rme96->irq);
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
24131da177e4SLinus Torvalds 		snd_card_free(card);
24141da177e4SLinus Torvalds 		return err;
24151da177e4SLinus Torvalds 	}
24161da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
24171da177e4SLinus Torvalds 	dev++;
24181da177e4SLinus Torvalds 	return 0;
24191da177e4SLinus Torvalds }
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds static void __devexit snd_rme96_remove(struct pci_dev *pci)
24221da177e4SLinus Torvalds {
24231da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
24241da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds static struct pci_driver driver = {
24281da177e4SLinus Torvalds 	.name = "RME Digi96",
24291da177e4SLinus Torvalds 	.id_table = snd_rme96_ids,
24301da177e4SLinus Torvalds 	.probe = snd_rme96_probe,
24311da177e4SLinus Torvalds 	.remove = __devexit_p(snd_rme96_remove),
24321da177e4SLinus Torvalds };
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds static int __init alsa_card_rme96_init(void)
24351da177e4SLinus Torvalds {
243601d25d46STakashi Iwai 	return pci_register_driver(&driver);
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds static void __exit alsa_card_rme96_exit(void)
24401da177e4SLinus Torvalds {
24411da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds module_init(alsa_card_rme96_init)
24451da177e4SLinus Torvalds module_exit(alsa_card_rme96_exit)
2446