xref: /openbmc/linux/sound/pci/rme96.c (revision 1a8e41efe3f403678cd6099fea8a05af7391de4c)
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 <linux/delay.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/interrupt.h>
291da177e4SLinus Torvalds #include <linux/pci.h>
3065a77217SPaul Gortmaker #include <linux/module.h>
31a8cc2099SKnut Petersen #include <linux/vmalloc.h>
326cbbfe1cSTakashi Iwai #include <linux/io.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 /* note, two last pcis should be equal, it is not a bug */
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds MODULE_AUTHOR("Anders Torger <torger@ludd.luth.se>");
451da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Digi96, Digi96/8, Digi96/8 PRO, Digi96/8 PST, "
461da177e4SLinus Torvalds 		   "Digi96/8 PAD");
471da177e4SLinus Torvalds MODULE_LICENSE("GPL");
481da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{RME,Digi96},"
491da177e4SLinus Torvalds 		"{RME,Digi96/8},"
501da177e4SLinus Torvalds 		"{RME,Digi96/8 PRO},"
511da177e4SLinus Torvalds 		"{RME,Digi96/8 PST},"
521da177e4SLinus Torvalds 		"{RME,Digi96/8 PAD}}");
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
551da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
56a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
591da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Digi96 soundcard.");
601da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
611da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Digi96 soundcard.");
621da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
631da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable RME Digi96 soundcard.");
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Defines for RME Digi96 series, from internal RME reference documents
671da177e4SLinus Torvalds  * dated 12.01.00
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define RME96_SPDIF_NCHANNELS 2
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Playback and capture buffer size */
731da177e4SLinus Torvalds #define RME96_BUFFER_SIZE 0x10000
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /* IO area size */
761da177e4SLinus Torvalds #define RME96_IO_SIZE 0x60000
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /* IO area offsets */
791da177e4SLinus Torvalds #define RME96_IO_PLAY_BUFFER      0x0
801da177e4SLinus Torvalds #define RME96_IO_REC_BUFFER       0x10000
811da177e4SLinus Torvalds #define RME96_IO_CONTROL_REGISTER 0x20000
821da177e4SLinus Torvalds #define RME96_IO_ADDITIONAL_REG   0x20004
831da177e4SLinus Torvalds #define RME96_IO_CONFIRM_PLAY_IRQ 0x20008
841da177e4SLinus Torvalds #define RME96_IO_CONFIRM_REC_IRQ  0x2000C
851da177e4SLinus Torvalds #define RME96_IO_SET_PLAY_POS     0x40000
861da177e4SLinus Torvalds #define RME96_IO_RESET_PLAY_POS   0x4FFFC
871da177e4SLinus Torvalds #define RME96_IO_SET_REC_POS      0x50000
881da177e4SLinus Torvalds #define RME96_IO_RESET_REC_POS    0x5FFFC
891da177e4SLinus Torvalds #define RME96_IO_GET_PLAY_POS     0x20000
901da177e4SLinus Torvalds #define RME96_IO_GET_REC_POS      0x30000
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* Write control register bits */
931da177e4SLinus Torvalds #define RME96_WCR_START     (1 << 0)
941da177e4SLinus Torvalds #define RME96_WCR_START_2   (1 << 1)
951da177e4SLinus Torvalds #define RME96_WCR_GAIN_0    (1 << 2)
961da177e4SLinus Torvalds #define RME96_WCR_GAIN_1    (1 << 3)
971da177e4SLinus Torvalds #define RME96_WCR_MODE24    (1 << 4)
981da177e4SLinus Torvalds #define RME96_WCR_MODE24_2  (1 << 5)
991da177e4SLinus Torvalds #define RME96_WCR_BM        (1 << 6)
1001da177e4SLinus Torvalds #define RME96_WCR_BM_2      (1 << 7)
1011da177e4SLinus Torvalds #define RME96_WCR_ADAT      (1 << 8)
1021da177e4SLinus Torvalds #define RME96_WCR_FREQ_0    (1 << 9)
1031da177e4SLinus Torvalds #define RME96_WCR_FREQ_1    (1 << 10)
1041da177e4SLinus Torvalds #define RME96_WCR_DS        (1 << 11)
1051da177e4SLinus Torvalds #define RME96_WCR_PRO       (1 << 12)
1061da177e4SLinus Torvalds #define RME96_WCR_EMP       (1 << 13)
1071da177e4SLinus Torvalds #define RME96_WCR_SEL       (1 << 14)
1081da177e4SLinus Torvalds #define RME96_WCR_MASTER    (1 << 15)
1091da177e4SLinus Torvalds #define RME96_WCR_PD        (1 << 16)
1101da177e4SLinus Torvalds #define RME96_WCR_INP_0     (1 << 17)
1111da177e4SLinus Torvalds #define RME96_WCR_INP_1     (1 << 18)
1121da177e4SLinus Torvalds #define RME96_WCR_THRU_0    (1 << 19)
1131da177e4SLinus Torvalds #define RME96_WCR_THRU_1    (1 << 20)
1141da177e4SLinus Torvalds #define RME96_WCR_THRU_2    (1 << 21)
1151da177e4SLinus Torvalds #define RME96_WCR_THRU_3    (1 << 22)
1161da177e4SLinus Torvalds #define RME96_WCR_THRU_4    (1 << 23)
1171da177e4SLinus Torvalds #define RME96_WCR_THRU_5    (1 << 24)
1181da177e4SLinus Torvalds #define RME96_WCR_THRU_6    (1 << 25)
1191da177e4SLinus Torvalds #define RME96_WCR_THRU_7    (1 << 26)
1201da177e4SLinus Torvalds #define RME96_WCR_DOLBY     (1 << 27)
1211da177e4SLinus Torvalds #define RME96_WCR_MONITOR_0 (1 << 28)
1221da177e4SLinus Torvalds #define RME96_WCR_MONITOR_1 (1 << 29)
1231da177e4SLinus Torvalds #define RME96_WCR_ISEL      (1 << 30)
1241da177e4SLinus Torvalds #define RME96_WCR_IDIS      (1 << 31)
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_0 2
1271da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_1 3
1281da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_0 9
1291da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_1 10
1301da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_0 17
1311da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_1 18
1321da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_0 28
1331da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_1 29
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /* Read control register bits */
1361da177e4SLinus Torvalds #define RME96_RCR_AUDIO_ADDR_MASK 0xFFFF
1371da177e4SLinus Torvalds #define RME96_RCR_IRQ_2     (1 << 16)
1381da177e4SLinus Torvalds #define RME96_RCR_T_OUT     (1 << 17)
1391da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_0  (1 << 21)
1401da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_1  (1 << 22)
1411da177e4SLinus Torvalds #define RME96_RCR_LOCK      (1 << 23)
1421da177e4SLinus Torvalds #define RME96_RCR_VERF      (1 << 26)
1431da177e4SLinus Torvalds #define RME96_RCR_F0        (1 << 27)
1441da177e4SLinus Torvalds #define RME96_RCR_F1        (1 << 28)
1451da177e4SLinus Torvalds #define RME96_RCR_F2        (1 << 29)
1461da177e4SLinus Torvalds #define RME96_RCR_AUTOSYNC  (1 << 30)
1471da177e4SLinus Torvalds #define RME96_RCR_IRQ       (1 << 31)
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F0 27
1501da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F1 28
1511da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F2 29
1521da177e4SLinus Torvalds 
15325985edcSLucas De Marchi /* Additional register bits */
1541da177e4SLinus Torvalds #define RME96_AR_WSEL       (1 << 0)
1551da177e4SLinus Torvalds #define RME96_AR_ANALOG     (1 << 1)
1561da177e4SLinus Torvalds #define RME96_AR_FREQPAD_0  (1 << 2)
1571da177e4SLinus Torvalds #define RME96_AR_FREQPAD_1  (1 << 3)
1581da177e4SLinus Torvalds #define RME96_AR_FREQPAD_2  (1 << 4)
1591da177e4SLinus Torvalds #define RME96_AR_PD2        (1 << 5)
1601da177e4SLinus Torvalds #define RME96_AR_DAC_EN     (1 << 6)
1611da177e4SLinus Torvalds #define RME96_AR_CLATCH     (1 << 7)
1621da177e4SLinus Torvalds #define RME96_AR_CCLK       (1 << 8)
1631da177e4SLinus Torvalds #define RME96_AR_CDATA      (1 << 9)
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds #define RME96_AR_BITPOS_F0 2
1661da177e4SLinus Torvalds #define RME96_AR_BITPOS_F1 3
1671da177e4SLinus Torvalds #define RME96_AR_BITPOS_F2 4
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /* Monitor tracks */
1701da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_1_2 0
1711da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_3_4 1
1721da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_5_6 2
1731da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_7_8 3
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* Attenuation */
1761da177e4SLinus Torvalds #define RME96_ATTENUATION_0 0
1771da177e4SLinus Torvalds #define RME96_ATTENUATION_6 1
1781da177e4SLinus Torvalds #define RME96_ATTENUATION_12 2
1791da177e4SLinus Torvalds #define RME96_ATTENUATION_18 3
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /* Input types */
1821da177e4SLinus Torvalds #define RME96_INPUT_OPTICAL 0
1831da177e4SLinus Torvalds #define RME96_INPUT_COAXIAL 1
1841da177e4SLinus Torvalds #define RME96_INPUT_INTERNAL 2
1851da177e4SLinus Torvalds #define RME96_INPUT_XLR 3
1861da177e4SLinus Torvalds #define RME96_INPUT_ANALOG 4
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /* Clock modes */
1891da177e4SLinus Torvalds #define RME96_CLOCKMODE_SLAVE 0
1901da177e4SLinus Torvalds #define RME96_CLOCKMODE_MASTER 1
1911da177e4SLinus Torvalds #define RME96_CLOCKMODE_WORDCLOCK 2
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /* Block sizes in bytes */
1941da177e4SLinus Torvalds #define RME96_SMALL_BLOCK_SIZE 2048
1951da177e4SLinus Torvalds #define RME96_LARGE_BLOCK_SIZE 8192
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /* Volume control */
1981da177e4SLinus Torvalds #define RME96_AD1852_VOL_BITS 14
1991da177e4SLinus Torvalds #define RME96_AD1855_VOL_BITS 10
2001da177e4SLinus Torvalds 
201b892ca1cSKnut Petersen /* Defines for snd_rme96_trigger */
202b892ca1cSKnut Petersen #define RME96_TB_START_PLAYBACK 1
203b892ca1cSKnut Petersen #define RME96_TB_START_CAPTURE 2
204b892ca1cSKnut Petersen #define RME96_TB_STOP_PLAYBACK 4
205b892ca1cSKnut Petersen #define RME96_TB_STOP_CAPTURE 8
206b892ca1cSKnut Petersen #define RME96_TB_RESET_PLAYPOS 16
207b892ca1cSKnut Petersen #define RME96_TB_RESET_CAPTUREPOS 32
208b892ca1cSKnut Petersen #define RME96_TB_CLEAR_PLAYBACK_IRQ 64
209b892ca1cSKnut Petersen #define RME96_TB_CLEAR_CAPTURE_IRQ 128
210b892ca1cSKnut Petersen #define RME96_RESUME_PLAYBACK	(RME96_TB_START_PLAYBACK)
211b892ca1cSKnut Petersen #define RME96_RESUME_CAPTURE	(RME96_TB_START_CAPTURE)
212b892ca1cSKnut Petersen #define RME96_RESUME_BOTH	(RME96_RESUME_PLAYBACK \
213b892ca1cSKnut Petersen 				| RME96_RESUME_CAPTURE)
214b892ca1cSKnut Petersen #define RME96_START_PLAYBACK	(RME96_TB_START_PLAYBACK \
215b892ca1cSKnut Petersen 				| RME96_TB_RESET_PLAYPOS)
216b892ca1cSKnut Petersen #define RME96_START_CAPTURE	(RME96_TB_START_CAPTURE \
217b892ca1cSKnut Petersen 				| RME96_TB_RESET_CAPTUREPOS)
218b892ca1cSKnut Petersen #define RME96_START_BOTH	(RME96_START_PLAYBACK \
219b892ca1cSKnut Petersen 				| RME96_START_CAPTURE)
220b892ca1cSKnut Petersen #define RME96_STOP_PLAYBACK	(RME96_TB_STOP_PLAYBACK \
221b892ca1cSKnut Petersen 				| RME96_TB_CLEAR_PLAYBACK_IRQ)
222b892ca1cSKnut Petersen #define RME96_STOP_CAPTURE	(RME96_TB_STOP_CAPTURE \
223b892ca1cSKnut Petersen 				| RME96_TB_CLEAR_CAPTURE_IRQ)
224b892ca1cSKnut Petersen #define RME96_STOP_BOTH		(RME96_STOP_PLAYBACK \
225b892ca1cSKnut Petersen 				| RME96_STOP_CAPTURE)
2261da177e4SLinus Torvalds 
227a3aefd88STakashi Iwai struct rme96 {
2281da177e4SLinus Torvalds 	spinlock_t    lock;
2291da177e4SLinus Torvalds 	int irq;
2301da177e4SLinus Torvalds 	unsigned long port;
2311da177e4SLinus Torvalds 	void __iomem *iobase;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	u32 wcreg;    /* cached write control register value */
2341da177e4SLinus Torvalds 	u32 wcreg_spdif;		/* S/PDIF setup */
2351da177e4SLinus Torvalds 	u32 wcreg_spdif_stream;		/* S/PDIF setup (temporary) */
2361da177e4SLinus Torvalds 	u32 rcreg;    /* cached read control register value */
2371da177e4SLinus Torvalds 	u32 areg;     /* cached additional register value */
2381da177e4SLinus Torvalds 	u16 vol[2]; /* cached volume of analog output */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	u8 rev; /* card revision number */
2411da177e4SLinus Torvalds 
242a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
243528ba522SKnut Petersen 	u32 playback_pointer;
244528ba522SKnut Petersen 	u32 capture_pointer;
245528ba522SKnut Petersen 	void *playback_suspend_buffer;
246528ba522SKnut Petersen 	void *capture_suspend_buffer;
247528ba522SKnut Petersen #endif
248528ba522SKnut Petersen 
249a3aefd88STakashi Iwai 	struct snd_pcm_substream *playback_substream;
250a3aefd88STakashi Iwai 	struct snd_pcm_substream *capture_substream;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	int playback_frlog; /* log2 of framesize */
2531da177e4SLinus Torvalds 	int capture_frlog;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds         size_t playback_periodsize; /* in bytes, zero if not used */
2561da177e4SLinus Torvalds 	size_t capture_periodsize; /* in bytes, zero if not used */
2571da177e4SLinus Torvalds 
258a3aefd88STakashi Iwai 	struct snd_card *card;
259a3aefd88STakashi Iwai 	struct snd_pcm *spdif_pcm;
260a3aefd88STakashi Iwai 	struct snd_pcm *adat_pcm;
2611da177e4SLinus Torvalds 	struct pci_dev     *pci;
262a3aefd88STakashi Iwai 	struct snd_kcontrol   *spdif_ctl;
263a3aefd88STakashi Iwai };
2641da177e4SLinus Torvalds 
2659baa3c34SBenoit Taine static const struct pci_device_id snd_rme96_ids[] = {
26628d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96), 0, },
26728d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8), 0, },
26828d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PRO), 0, },
26928d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST), 0, },
2701da177e4SLinus Torvalds 	{ 0, }
2711da177e4SLinus Torvalds };
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_rme96_ids);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds #define RME96_ISPLAYING(rme96) ((rme96)->wcreg & RME96_WCR_START)
2761da177e4SLinus Torvalds #define RME96_ISRECORDING(rme96) ((rme96)->wcreg & RME96_WCR_START_2)
2778b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_IN(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2788b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_OUT(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO || \
2798b7fc421SRoland Dreier 				     (rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2801da177e4SLinus Torvalds #define	RME96_DAC_IS_1852(rme96) (RME96_HAS_ANALOG_OUT(rme96) && (rme96)->rev >= 4)
2818b7fc421SRoland Dreier #define	RME96_DAC_IS_1855(rme96) (((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && (rme96)->rev < 4) || \
2828b7fc421SRoland Dreier 			          ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO && (rme96)->rev == 2))
2831da177e4SLinus Torvalds #define	RME96_185X_MAX_OUT(rme96) ((1 << (RME96_DAC_IS_1852(rme96) ? RME96_AD1852_VOL_BITS : RME96_AD1855_VOL_BITS)) - 1)
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds static int
286a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static int
289a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static int
292a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
2931da177e4SLinus Torvalds 			   int cmd);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int
296a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
2971da177e4SLinus Torvalds 			  int cmd);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static snd_pcm_uframes_t
300a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static snd_pcm_uframes_t
303a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream);
3041da177e4SLinus Torvalds 
305e23e7a14SBill Pemberton static void snd_rme96_proc_init(struct rme96 *rme96);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds static int
308a3aefd88STakashi Iwai snd_rme96_create_switches(struct snd_card *card,
309a3aefd88STakashi Iwai 			  struct rme96 *rme96);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static int
312a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static inline unsigned int
315a3aefd88STakashi Iwai snd_rme96_playback_ptr(struct rme96 *rme96)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
3181da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->playback_frlog;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds static inline unsigned int
322a3aefd88STakashi Iwai snd_rme96_capture_ptr(struct rme96 *rme96)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_REC_POS)
3251da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->capture_frlog;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds static int
329a3aefd88STakashi Iwai snd_rme96_playback_silence(struct snd_pcm_substream *substream,
3301da177e4SLinus Torvalds 			   int channel, /* not used (interleaved data) */
3311da177e4SLinus Torvalds 			   snd_pcm_uframes_t pos,
3321da177e4SLinus Torvalds 			   snd_pcm_uframes_t count)
3331da177e4SLinus Torvalds {
334a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3351da177e4SLinus Torvalds 	count <<= rme96->playback_frlog;
3361da177e4SLinus Torvalds 	pos <<= rme96->playback_frlog;
3371da177e4SLinus Torvalds 	memset_io(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
3381da177e4SLinus Torvalds 		  0, count);
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds static int
343a3aefd88STakashi Iwai snd_rme96_playback_copy(struct snd_pcm_substream *substream,
3441da177e4SLinus Torvalds 			int channel, /* not used (interleaved data) */
3451da177e4SLinus Torvalds 			snd_pcm_uframes_t pos,
3461da177e4SLinus Torvalds 			void __user *src,
3471da177e4SLinus Torvalds 			snd_pcm_uframes_t count)
3481da177e4SLinus Torvalds {
349a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3501da177e4SLinus Torvalds 	count <<= rme96->playback_frlog;
3511da177e4SLinus Torvalds 	pos <<= rme96->playback_frlog;
3522026d24eSTakashi Iwai 	return copy_from_user_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos, src,
3531da177e4SLinus Torvalds 				   count);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds static int
357a3aefd88STakashi Iwai snd_rme96_capture_copy(struct snd_pcm_substream *substream,
3581da177e4SLinus Torvalds 		       int channel, /* not used (interleaved data) */
3591da177e4SLinus Torvalds 		       snd_pcm_uframes_t pos,
3601da177e4SLinus Torvalds 		       void __user *dst,
3611da177e4SLinus Torvalds 		       snd_pcm_uframes_t count)
3621da177e4SLinus Torvalds {
363a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
3641da177e4SLinus Torvalds 	count <<= rme96->capture_frlog;
3651da177e4SLinus Torvalds 	pos <<= rme96->capture_frlog;
3662026d24eSTakashi Iwai 	return copy_to_user_fromio(dst, rme96->iobase + RME96_IO_REC_BUFFER + pos,
3671da177e4SLinus Torvalds 				   count);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*
3717f927fccSAlexey Dobriyan  * Digital output capabilities (S/PDIF)
3721da177e4SLinus Torvalds  */
373a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_playback_spdif_info =
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
3761da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
377b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
378528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
3791da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
3801da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
3811da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
3821da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
3831da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
3841da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
3851da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
3861da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
3871da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
3881da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
3891da177e4SLinus Torvalds 	.rate_min =	     32000,
3901da177e4SLinus Torvalds 	.rate_max =	     96000,
3911da177e4SLinus Torvalds 	.channels_min =	     2,
3921da177e4SLinus Torvalds 	.channels_max =	     2,
3931da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
3941da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
3951da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
3961da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
3971da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
3981da177e4SLinus Torvalds 	.fifo_size =	     0,
3991da177e4SLinus Torvalds };
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
4027f927fccSAlexey Dobriyan  * Digital input capabilities (S/PDIF)
4031da177e4SLinus Torvalds  */
404a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_capture_spdif_info =
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4071da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
408b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
409528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
4101da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4111da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4121da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4131da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4141da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
4151da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
4161da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
4171da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
4181da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
4191da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
4201da177e4SLinus Torvalds 	.rate_min =	     32000,
4211da177e4SLinus Torvalds 	.rate_max =	     96000,
4221da177e4SLinus Torvalds 	.channels_min =	     2,
4231da177e4SLinus Torvalds 	.channels_max =	     2,
4241da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4251da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4261da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4271da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4281da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4291da177e4SLinus Torvalds 	.fifo_size =	     0,
4301da177e4SLinus Torvalds };
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /*
4337f927fccSAlexey Dobriyan  * Digital output capabilities (ADAT)
4341da177e4SLinus Torvalds  */
435a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_playback_adat_info =
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4381da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
439b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
440528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
4411da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4421da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4431da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4441da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4451da177e4SLinus Torvalds 	.rates =             (SNDRV_PCM_RATE_44100 |
4461da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000),
4471da177e4SLinus Torvalds 	.rate_min =          44100,
4481da177e4SLinus Torvalds 	.rate_max =          48000,
4491da177e4SLinus Torvalds 	.channels_min =      8,
4501da177e4SLinus Torvalds 	.channels_max =	     8,
4511da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4521da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4531da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4541da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4551da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4561da177e4SLinus Torvalds 	.fifo_size =	     0,
4571da177e4SLinus Torvalds };
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /*
4607f927fccSAlexey Dobriyan  * Digital input capabilities (ADAT)
4611da177e4SLinus Torvalds  */
462a3aefd88STakashi Iwai static struct snd_pcm_hardware snd_rme96_capture_adat_info =
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4651da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
466b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
467528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
4681da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4691da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4701da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4711da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4721da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_44100 |
4731da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000),
4741da177e4SLinus Torvalds 	.rate_min =          44100,
4751da177e4SLinus Torvalds 	.rate_max =          48000,
4761da177e4SLinus Torvalds 	.channels_min =      8,
4771da177e4SLinus Torvalds 	.channels_max =	     8,
4781da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
4791da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
4801da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4811da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4821da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4831da177e4SLinus Torvalds 	.fifo_size =         0,
4841da177e4SLinus Torvalds };
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  * The CDATA, CCLK and CLATCH bits can be used to write to the SPI interface
4881da177e4SLinus Torvalds  * of the AD1852 or AD1852 D/A converter on the board.  CDATA must be set up
4891da177e4SLinus Torvalds  * on the falling edge of CCLK and be stable on the rising edge.  The rising
4901da177e4SLinus Torvalds  * edge of CLATCH after the last data bit clocks in the whole data word.
4911da177e4SLinus Torvalds  * A fast processor could probably drive the SPI interface faster than the
4921da177e4SLinus Torvalds  * DAC can handle (3MHz for the 1855, unknown for the 1852).  The udelay(1)
4931da177e4SLinus Torvalds  * limits the data rate to 500KHz and only causes a delay of 33 microsecs.
4941da177e4SLinus Torvalds  *
4951da177e4SLinus Torvalds  * NOTE: increased delay from 1 to 10, since there where problems setting
4961da177e4SLinus Torvalds  * the volume.
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds static void
499a3aefd88STakashi Iwai snd_rme96_write_SPI(struct rme96 *rme96, u16 val)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	int i;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	for (i = 0; i < 16; i++) {
5041da177e4SLinus Torvalds 		if (val & 0x8000) {
5051da177e4SLinus Torvalds 			rme96->areg |= RME96_AR_CDATA;
5061da177e4SLinus Torvalds 		} else {
5071da177e4SLinus Torvalds 			rme96->areg &= ~RME96_AR_CDATA;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 		rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CLATCH);
5101da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5111da177e4SLinus Torvalds 		udelay(10);
5121da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_CCLK;
5131da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5141da177e4SLinus Torvalds 		udelay(10);
5151da177e4SLinus Torvalds 		val <<= 1;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 	rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CDATA);
5181da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_CLATCH;
5191da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5201da177e4SLinus Torvalds 	udelay(10);
5211da177e4SLinus Torvalds 	rme96->areg &= ~RME96_AR_CLATCH;
5221da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds static void
526a3aefd88STakashi Iwai snd_rme96_apply_dac_volume(struct rme96 *rme96)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	if (RME96_DAC_IS_1852(rme96)) {
5291da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] << 2) | 0x0);
5301da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] << 2) | 0x2);
5311da177e4SLinus Torvalds 	} else if (RME96_DAC_IS_1855(rme96)) {
5321da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] & 0x3FF) | 0x000);
5331da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] & 0x3FF) | 0x400);
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static void
538a3aefd88STakashi Iwai snd_rme96_reset_dac(struct rme96 *rme96)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	writel(rme96->wcreg | RME96_WCR_PD,
5411da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_CONTROL_REGISTER);
5421da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static int
546a3aefd88STakashi Iwai snd_rme96_getmontracks(struct rme96 *rme96)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_0) & 1) +
5491da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_1) & 1) << 1);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds static int
553a3aefd88STakashi Iwai snd_rme96_setmontracks(struct rme96 *rme96,
5541da177e4SLinus Torvalds 		       int montracks)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	if (montracks & 1) {
5571da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_0;
5581da177e4SLinus Torvalds 	} else {
5591da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_0;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 	if (montracks & 2) {
5621da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_1;
5631da177e4SLinus Torvalds 	} else {
5641da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_1;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5671da177e4SLinus Torvalds 	return 0;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds static int
571a3aefd88STakashi Iwai snd_rme96_getattenuation(struct rme96 *rme96)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_0) & 1) +
5741da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_1) & 1) << 1);
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds static int
578a3aefd88STakashi Iwai snd_rme96_setattenuation(struct rme96 *rme96,
5791da177e4SLinus Torvalds 			 int attenuation)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	switch (attenuation) {
5821da177e4SLinus Torvalds 	case 0:
5831da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) &
5841da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5851da177e4SLinus Torvalds 		break;
5861da177e4SLinus Torvalds 	case 1:
5871da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) &
5881da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5891da177e4SLinus Torvalds 		break;
5901da177e4SLinus Torvalds 	case 2:
5911da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) |
5921da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 	case 3:
5951da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) |
5961da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5971da177e4SLinus Torvalds 		break;
5981da177e4SLinus Torvalds 	default:
5991da177e4SLinus Torvalds 		return -EINVAL;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
6021da177e4SLinus Torvalds 	return 0;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds static int
606a3aefd88STakashi Iwai snd_rme96_capture_getrate(struct rme96 *rme96,
6071da177e4SLinus Torvalds 			  int *is_adat)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	int n, rate;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	*is_adat = 0;
6121da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
6131da177e4SLinus Torvalds 		/* Analog input, overrides S/PDIF setting */
6141da177e4SLinus Torvalds 		n = ((rme96->areg >> RME96_AR_BITPOS_F0) & 1) +
6151da177e4SLinus Torvalds 			(((rme96->areg >> RME96_AR_BITPOS_F1) & 1) << 1);
6161da177e4SLinus Torvalds 		switch (n) {
6171da177e4SLinus Torvalds 		case 1:
6181da177e4SLinus Torvalds 			rate = 32000;
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 		case 2:
6211da177e4SLinus Torvalds 			rate = 44100;
6221da177e4SLinus Torvalds 			break;
6231da177e4SLinus Torvalds 		case 3:
6241da177e4SLinus Torvalds 			rate = 48000;
6251da177e4SLinus Torvalds 			break;
6261da177e4SLinus Torvalds 		default:
6271da177e4SLinus Torvalds 			return -1;
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 		return (rme96->areg & RME96_AR_BITPOS_F2) ? rate << 1 : rate;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
6331da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_LOCK) {
6341da177e4SLinus Torvalds 		/* ADAT rate */
6351da177e4SLinus Torvalds 		*is_adat = 1;
6361da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6371da177e4SLinus Torvalds 			return 48000;
6381da177e4SLinus Torvalds 		}
6391da177e4SLinus Torvalds 		return 44100;
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_VERF) {
6431da177e4SLinus Torvalds 		return -1;
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	/* S/PDIF rate */
6471da177e4SLinus Torvalds 	n = ((rme96->rcreg >> RME96_RCR_BITPOS_F0) & 1) +
6481da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F1) & 1) << 1) +
6491da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F2) & 1) << 2);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	switch (n) {
6521da177e4SLinus Torvalds 	case 0:
6531da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6541da177e4SLinus Torvalds 			return 64000;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 		return -1;
6571da177e4SLinus Torvalds 	case 3: return 96000;
6581da177e4SLinus Torvalds 	case 4: return 88200;
6591da177e4SLinus Torvalds 	case 5: return 48000;
6601da177e4SLinus Torvalds 	case 6: return 44100;
6611da177e4SLinus Torvalds 	case 7: return 32000;
6621da177e4SLinus Torvalds 	default:
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 	return -1;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds static int
669a3aefd88STakashi Iwai snd_rme96_playback_getrate(struct rme96 *rme96)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	int rate, dummy;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
6741da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
6751da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
6761da177e4SLinus Torvalds 	{
6771da177e4SLinus Torvalds 	        /* slave clock */
6781da177e4SLinus Torvalds 	        return rate;
6791da177e4SLinus Torvalds 	}
6801da177e4SLinus Torvalds 	rate = ((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_0) & 1) +
6811da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_1) & 1) << 1);
6821da177e4SLinus Torvalds 	switch (rate) {
6831da177e4SLinus Torvalds 	case 1:
6841da177e4SLinus Torvalds 		rate = 32000;
6851da177e4SLinus Torvalds 		break;
6861da177e4SLinus Torvalds 	case 2:
6871da177e4SLinus Torvalds 		rate = 44100;
6881da177e4SLinus Torvalds 		break;
6891da177e4SLinus Torvalds 	case 3:
6901da177e4SLinus Torvalds 		rate = 48000;
6911da177e4SLinus Torvalds 		break;
6921da177e4SLinus Torvalds 	default:
6931da177e4SLinus Torvalds 		return -1;
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_DS) ? rate << 1 : rate;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static int
699a3aefd88STakashi Iwai snd_rme96_playback_setrate(struct rme96 *rme96,
7001da177e4SLinus Torvalds 			   int rate)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	int ds;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	ds = rme96->wcreg & RME96_WCR_DS;
7051da177e4SLinus Torvalds 	switch (rate) {
7061da177e4SLinus Torvalds 	case 32000:
7071da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
7081da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
7091da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
7101da177e4SLinus Torvalds 		break;
7111da177e4SLinus Torvalds 	case 44100:
7121da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
7131da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
7141da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
7151da177e4SLinus Torvalds 		break;
7161da177e4SLinus Torvalds 	case 48000:
7171da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
7181da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
7191da177e4SLinus Torvalds 			RME96_WCR_FREQ_1;
7201da177e4SLinus Torvalds 		break;
7211da177e4SLinus Torvalds 	case 64000:
7221da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7231da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
7241da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
7251da177e4SLinus Torvalds 		break;
7261da177e4SLinus Torvalds 	case 88200:
7271da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7281da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
7291da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
7301da177e4SLinus Torvalds 		break;
7311da177e4SLinus Torvalds 	case 96000:
7321da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7331da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
7341da177e4SLinus Torvalds 			RME96_WCR_FREQ_1;
7351da177e4SLinus Torvalds 		break;
7361da177e4SLinus Torvalds 	default:
7371da177e4SLinus Torvalds 		return -EINVAL;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	if ((!ds && rme96->wcreg & RME96_WCR_DS) ||
7401da177e4SLinus Torvalds 	    (ds && !(rme96->wcreg & RME96_WCR_DS)))
7411da177e4SLinus Torvalds 	{
7421da177e4SLinus Torvalds 		/* change to/from double-speed: reset the DAC (if available) */
7431da177e4SLinus Torvalds 		snd_rme96_reset_dac(rme96);
7441da177e4SLinus Torvalds 	} else {
7451da177e4SLinus Torvalds 		writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	return 0;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds static int
751a3aefd88STakashi Iwai snd_rme96_capture_analog_setrate(struct rme96 *rme96,
7521da177e4SLinus Torvalds 				 int rate)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	switch (rate) {
7551da177e4SLinus Torvalds 	case 32000:
7561da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7571da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7581da177e4SLinus Torvalds 		break;
7591da177e4SLinus Torvalds 	case 44100:
7601da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7611da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7621da177e4SLinus Torvalds 		break;
7631da177e4SLinus Torvalds 	case 48000:
7641da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7651da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7661da177e4SLinus Torvalds 		break;
7671da177e4SLinus Torvalds 	case 64000:
7681da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7691da177e4SLinus Torvalds 			return -EINVAL;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7721da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7731da177e4SLinus Torvalds 		break;
7741da177e4SLinus Torvalds 	case 88200:
7751da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7761da177e4SLinus Torvalds 			return -EINVAL;
7771da177e4SLinus Torvalds 		}
7781da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7791da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7801da177e4SLinus Torvalds 		break;
7811da177e4SLinus Torvalds 	case 96000:
7821da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7831da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7841da177e4SLinus Torvalds 		break;
7851da177e4SLinus Torvalds 	default:
7861da177e4SLinus Torvalds 		return -EINVAL;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
7891da177e4SLinus Torvalds 	return 0;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds static int
793a3aefd88STakashi Iwai snd_rme96_setclockmode(struct rme96 *rme96,
7941da177e4SLinus Torvalds 		       int mode)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	switch (mode) {
7971da177e4SLinus Torvalds 	case RME96_CLOCKMODE_SLAVE:
7981da177e4SLinus Torvalds 	        /* AutoSync */
7991da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MASTER;
8001da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
8011da177e4SLinus Torvalds 		break;
8021da177e4SLinus Torvalds 	case RME96_CLOCKMODE_MASTER:
8031da177e4SLinus Torvalds 	        /* Internal */
8041da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
8051da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
8061da177e4SLinus Torvalds 		break;
8071da177e4SLinus Torvalds 	case RME96_CLOCKMODE_WORDCLOCK:
8081da177e4SLinus Torvalds 		/* Word clock is a master mode */
8091da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
8101da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_WSEL;
8111da177e4SLinus Torvalds 		break;
8121da177e4SLinus Torvalds 	default:
8131da177e4SLinus Torvalds 		return -EINVAL;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
8161da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8171da177e4SLinus Torvalds 	return 0;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds static int
821a3aefd88STakashi Iwai snd_rme96_getclockmode(struct rme96 *rme96)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
8241da177e4SLinus Torvalds 		return RME96_CLOCKMODE_WORDCLOCK;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
8271da177e4SLinus Torvalds 		RME96_CLOCKMODE_SLAVE;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static int
831a3aefd88STakashi Iwai snd_rme96_setinputtype(struct rme96 *rme96,
8321da177e4SLinus Torvalds 		       int type)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	int n;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	switch (type) {
8371da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
8381da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
8391da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8401da177e4SLinus Torvalds 		break;
8411da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
8421da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
8431da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8441da177e4SLinus Torvalds 		break;
8451da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
8461da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
8471da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8481da177e4SLinus Torvalds 		break;
8491da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
8508b7fc421SRoland Dreier 		if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8518b7fc421SRoland Dreier 		     rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
8528b7fc421SRoland Dreier 		    (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8531da177e4SLinus Torvalds 		     rme96->rev > 4))
8541da177e4SLinus Torvalds 		{
8551da177e4SLinus Torvalds 			/* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
8561da177e4SLinus Torvalds 			return -EINVAL;
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
8591da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8601da177e4SLinus Torvalds 		break;
8611da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
8621da177e4SLinus Torvalds 		if (!RME96_HAS_ANALOG_IN(rme96)) {
8631da177e4SLinus Torvalds 			return -EINVAL;
8641da177e4SLinus Torvalds 		}
8651da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_ANALOG;
8661da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8671da177e4SLinus Torvalds 		if (rme96->rev < 4) {
8681da177e4SLinus Torvalds 			/*
8691da177e4SLinus Torvalds 			 * Revision less than 004 does not support 64 and
8701da177e4SLinus Torvalds 			 * 88.2 kHz
8711da177e4SLinus Torvalds 			 */
8721da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
8731da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 44100);
8741da177e4SLinus Torvalds 			}
8751da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
8761da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 32000);
8771da177e4SLinus Torvalds 			}
8781da177e4SLinus Torvalds 		}
8791da177e4SLinus Torvalds 		return 0;
8801da177e4SLinus Torvalds 	default:
8811da177e4SLinus Torvalds 		return -EINVAL;
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 	if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
8841da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_ANALOG;
8851da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
8881da177e4SLinus Torvalds 	return 0;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds static int
892a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
8951da177e4SLinus Torvalds 		return RME96_INPUT_ANALOG;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
8981da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds static void
902a3aefd88STakashi Iwai snd_rme96_setframelog(struct rme96 *rme96,
9031da177e4SLinus Torvalds 		      int n_channels,
9041da177e4SLinus Torvalds 		      int is_playback)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	int frlog;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (n_channels == 2) {
9091da177e4SLinus Torvalds 		frlog = 1;
9101da177e4SLinus Torvalds 	} else {
9111da177e4SLinus Torvalds 		/* assume 8 channels */
9121da177e4SLinus Torvalds 		frlog = 3;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 	if (is_playback) {
9151da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
9161da177e4SLinus Torvalds 		rme96->playback_frlog = frlog;
9171da177e4SLinus Torvalds 	} else {
9181da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
9191da177e4SLinus Torvalds 		rme96->capture_frlog = frlog;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds static int
9246c869d30STakashi Iwai snd_rme96_playback_setformat(struct rme96 *rme96, snd_pcm_format_t format)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	switch (format) {
9271da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9281da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24;
9291da177e4SLinus Torvalds 		break;
9301da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9311da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24;
9321da177e4SLinus Torvalds 		break;
9331da177e4SLinus Torvalds 	default:
9341da177e4SLinus Torvalds 		return -EINVAL;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9371da177e4SLinus Torvalds 	return 0;
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds static int
9416c869d30STakashi Iwai snd_rme96_capture_setformat(struct rme96 *rme96, snd_pcm_format_t format)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	switch (format) {
9441da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9451da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24_2;
9461da177e4SLinus Torvalds 		break;
9471da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9481da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24_2;
9491da177e4SLinus Torvalds 		break;
9501da177e4SLinus Torvalds 	default:
9511da177e4SLinus Torvalds 		return -EINVAL;
9521da177e4SLinus Torvalds 	}
9531da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9541da177e4SLinus Torvalds 	return 0;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds static void
958a3aefd88STakashi Iwai snd_rme96_set_period_properties(struct rme96 *rme96,
9591da177e4SLinus Torvalds 				size_t period_bytes)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	switch (period_bytes) {
9621da177e4SLinus Torvalds 	case RME96_LARGE_BLOCK_SIZE:
9631da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_ISEL;
9641da177e4SLinus Torvalds 		break;
9651da177e4SLinus Torvalds 	case RME96_SMALL_BLOCK_SIZE:
9661da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_ISEL;
9671da177e4SLinus Torvalds 		break;
9681da177e4SLinus Torvalds 	default:
9691da177e4SLinus Torvalds 		snd_BUG();
9701da177e4SLinus Torvalds 		break;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_IDIS;
9731da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds static int
977a3aefd88STakashi Iwai snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
978a3aefd88STakashi Iwai 			     struct snd_pcm_hw_params *params)
9791da177e4SLinus Torvalds {
980a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
981a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
9821da177e4SLinus Torvalds 	int err, rate, dummy;
9831da177e4SLinus Torvalds 
9844d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
9854d23359bSClemens Ladisch 					     RME96_IO_PLAY_BUFFER);
9861da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
9871da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
9901da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
9911da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
9921da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
9931da177e4SLinus Torvalds 	{
9941da177e4SLinus Torvalds                 /* slave clock */
9951da177e4SLinus Torvalds                 if ((int)params_rate(params) != rate) {
9961da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
9971da177e4SLinus Torvalds 			return -EIO;
9981da177e4SLinus Torvalds                 }
9991da177e4SLinus Torvalds 	} else if ((err = snd_rme96_playback_setrate(rme96, params_rate(params))) < 0) {
10001da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
10011da177e4SLinus Torvalds 		return err;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 	if ((err = snd_rme96_playback_setformat(rme96, params_format(params))) < 0) {
10041da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
10051da177e4SLinus Torvalds 		return err;
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 1);
10081da177e4SLinus Torvalds 	if (rme96->capture_periodsize != 0) {
10091da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->playback_frlog !=
10101da177e4SLinus Torvalds 		    rme96->capture_periodsize)
10111da177e4SLinus Torvalds 		{
10121da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10131da177e4SLinus Torvalds 			return -EBUSY;
10141da177e4SLinus Torvalds 		}
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	rme96->playback_periodsize =
10171da177e4SLinus Torvalds 		params_period_size(params) << rme96->playback_frlog;
10181da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
10191da177e4SLinus Torvalds 	/* S/PDIF setup */
10201da177e4SLinus Torvalds 	if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
10211da177e4SLinus Torvalds 		rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
10221da177e4SLinus Torvalds 		writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	return 0;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds static int
1030a3aefd88STakashi Iwai snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
1031a3aefd88STakashi Iwai 			    struct snd_pcm_hw_params *params)
10321da177e4SLinus Torvalds {
1033a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1034a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10351da177e4SLinus Torvalds 	int err, isadat, rate;
10361da177e4SLinus Torvalds 
10374d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
10384d23359bSClemens Ladisch 					     RME96_IO_REC_BUFFER);
10391da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
10401da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
10431da177e4SLinus Torvalds 	if ((err = snd_rme96_capture_setformat(rme96, params_format(params))) < 0) {
10441da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
10451da177e4SLinus Torvalds 		return err;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 	if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
10481da177e4SLinus Torvalds 		if ((err = snd_rme96_capture_analog_setrate(rme96,
10491da177e4SLinus Torvalds 							    params_rate(params))) < 0)
10501da177e4SLinus Torvalds 		{
10511da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10521da177e4SLinus Torvalds 			return err;
10531da177e4SLinus Torvalds 		}
10541da177e4SLinus Torvalds 	} else if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
10551da177e4SLinus Torvalds                 if ((int)params_rate(params) != rate) {
10561da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10571da177e4SLinus Torvalds 			return -EIO;
10581da177e4SLinus Torvalds                 }
10591da177e4SLinus Torvalds                 if ((isadat && runtime->hw.channels_min == 2) ||
10601da177e4SLinus Torvalds                     (!isadat && runtime->hw.channels_min == 8))
10611da177e4SLinus Torvalds                 {
10621da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10631da177e4SLinus Torvalds 			return -EIO;
10641da177e4SLinus Torvalds                 }
10651da177e4SLinus Torvalds         }
10661da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 0);
10671da177e4SLinus Torvalds 	if (rme96->playback_periodsize != 0) {
10681da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->capture_frlog !=
10691da177e4SLinus Torvalds 		    rme96->playback_periodsize)
10701da177e4SLinus Torvalds 		{
10711da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10721da177e4SLinus Torvalds 			return -EBUSY;
10731da177e4SLinus Torvalds 		}
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 	rme96->capture_periodsize =
10761da177e4SLinus Torvalds 		params_period_size(params) << rme96->capture_frlog;
10771da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
10781da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	return 0;
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds static void
1084b892ca1cSKnut Petersen snd_rme96_trigger(struct rme96 *rme96,
1085b892ca1cSKnut Petersen 		  int op)
10861da177e4SLinus Torvalds {
1087b892ca1cSKnut Petersen 	if (op & RME96_TB_RESET_PLAYPOS)
10881da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
1089b892ca1cSKnut Petersen 	if (op & RME96_TB_RESET_CAPTUREPOS)
10901da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
1091b892ca1cSKnut Petersen 	if (op & RME96_TB_CLEAR_PLAYBACK_IRQ) {
10921da177e4SLinus Torvalds 		rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1093b892ca1cSKnut Petersen 		if (rme96->rcreg & RME96_RCR_IRQ)
10941da177e4SLinus Torvalds 			writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
10951da177e4SLinus Torvalds 	}
1096b892ca1cSKnut Petersen 	if (op & RME96_TB_CLEAR_CAPTURE_IRQ) {
10971da177e4SLinus Torvalds 		rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1098b892ca1cSKnut Petersen 		if (rme96->rcreg & RME96_RCR_IRQ_2)
10991da177e4SLinus Torvalds 			writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
11001da177e4SLinus Torvalds 	}
1101b892ca1cSKnut Petersen 	if (op & RME96_TB_START_PLAYBACK)
1102b892ca1cSKnut Petersen 		rme96->wcreg |= RME96_WCR_START;
1103b892ca1cSKnut Petersen 	if (op & RME96_TB_STOP_PLAYBACK)
1104b892ca1cSKnut Petersen 		rme96->wcreg &= ~RME96_WCR_START;
1105b892ca1cSKnut Petersen 	if (op & RME96_TB_START_CAPTURE)
1106b892ca1cSKnut Petersen 		rme96->wcreg |= RME96_WCR_START_2;
1107b892ca1cSKnut Petersen 	if (op & RME96_TB_STOP_CAPTURE)
11081da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_START_2;
11091da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
1112b892ca1cSKnut Petersen 
1113b892ca1cSKnut Petersen 
11141da177e4SLinus Torvalds static irqreturn_t
11151da177e4SLinus Torvalds snd_rme96_interrupt(int irq,
11167d12e780SDavid Howells 		    void *dev_id)
11171da177e4SLinus Torvalds {
1118a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)dev_id;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
11211da177e4SLinus Torvalds 	/* fastpath out, to ease interrupt sharing */
11221da177e4SLinus Torvalds 	if (!((rme96->rcreg & RME96_RCR_IRQ) ||
11231da177e4SLinus Torvalds 	      (rme96->rcreg & RME96_RCR_IRQ_2)))
11241da177e4SLinus Torvalds 	{
11251da177e4SLinus Torvalds 		return IRQ_NONE;
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ) {
11291da177e4SLinus Torvalds 		/* playback */
11301da177e4SLinus Torvalds                 snd_pcm_period_elapsed(rme96->playback_substream);
11311da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ_2) {
11341da177e4SLinus Torvalds 		/* capture */
11351da177e4SLinus Torvalds 		snd_pcm_period_elapsed(rme96->capture_substream);
11361da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 	return IRQ_HANDLED;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds static unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
11421da177e4SLinus Torvalds 
1143a3aefd88STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
11441da177e4SLinus Torvalds 	.count = ARRAY_SIZE(period_bytes),
11451da177e4SLinus Torvalds 	.list = period_bytes,
11461da177e4SLinus Torvalds 	.mask = 0
11471da177e4SLinus Torvalds };
11481da177e4SLinus Torvalds 
11492ce7fb57STakashi Iwai static void
11502ce7fb57STakashi Iwai rme96_set_buffer_size_constraint(struct rme96 *rme96,
11512ce7fb57STakashi Iwai 				 struct snd_pcm_runtime *runtime)
11522ce7fb57STakashi Iwai {
11532ce7fb57STakashi Iwai 	unsigned int size;
11542ce7fb57STakashi Iwai 
1155*1a8e41efSLars-Peter Clausen 	snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1156*1a8e41efSLars-Peter Clausen 				     RME96_BUFFER_SIZE);
11572ce7fb57STakashi Iwai 	if ((size = rme96->playback_periodsize) != 0 ||
11582ce7fb57STakashi Iwai 	    (size = rme96->capture_periodsize) != 0)
1159*1a8e41efSLars-Peter Clausen 		snd_pcm_hw_constraint_single(runtime,
11602ce7fb57STakashi Iwai 					     SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1161*1a8e41efSLars-Peter Clausen 					     size);
11622ce7fb57STakashi Iwai 	else
11632ce7fb57STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
11642ce7fb57STakashi Iwai 					   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
11652ce7fb57STakashi Iwai 					   &hw_constraints_period_bytes);
11662ce7fb57STakashi Iwai }
11672ce7fb57STakashi Iwai 
11681da177e4SLinus Torvalds static int
1169a3aefd88STakashi Iwai snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds         int rate, dummy;
1172a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1173a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11741da177e4SLinus Torvalds 
1175b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
11761da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
11771da177e4SLinus Torvalds         if (rme96->playback_substream != NULL) {
11781da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
11791da177e4SLinus Torvalds                 return -EBUSY;
11801da177e4SLinus Torvalds         }
11811da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_ADAT;
11821da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11831da177e4SLinus Torvalds 	rme96->playback_substream = substream;
11841da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_spdif_info;
11871da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
11881da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
11891da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
11901da177e4SLinus Torvalds 	{
11911da177e4SLinus Torvalds                 /* slave clock */
1192918f3a0eSClemens Ladisch                 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
11931da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
11941da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
11951da177e4SLinus Torvalds 	}
11962ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
11991da177e4SLinus Torvalds 	rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
12001da177e4SLinus Torvalds 	snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
12011da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
12021da177e4SLinus Torvalds 	return 0;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds static int
1206a3aefd88STakashi Iwai snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds         int isadat, rate;
1209a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1210a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12111da177e4SLinus Torvalds 
1212b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12131da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_spdif_info;
12141da177e4SLinus Torvalds         if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
12151da177e4SLinus Torvalds             (rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0)
12161da177e4SLinus Torvalds         {
12171da177e4SLinus Torvalds                 if (isadat) {
12181da177e4SLinus Torvalds                         return -EIO;
12191da177e4SLinus Torvalds                 }
1220918f3a0eSClemens Ladisch                 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12211da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12221da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12231da177e4SLinus Torvalds         }
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12261da177e4SLinus Torvalds         if (rme96->capture_substream != NULL) {
12271da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12281da177e4SLinus Torvalds                 return -EBUSY;
12291da177e4SLinus Torvalds         }
12301da177e4SLinus Torvalds 	rme96->capture_substream = substream;
12311da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12321da177e4SLinus Torvalds 
12332ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12341da177e4SLinus Torvalds 	return 0;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds static int
1238a3aefd88STakashi Iwai snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
12391da177e4SLinus Torvalds {
12401da177e4SLinus Torvalds         int rate, dummy;
1241a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1242a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12431da177e4SLinus Torvalds 
1244b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12451da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12461da177e4SLinus Torvalds         if (rme96->playback_substream != NULL) {
12471da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12481da177e4SLinus Torvalds                 return -EBUSY;
12491da177e4SLinus Torvalds         }
12501da177e4SLinus Torvalds 	rme96->wcreg |= RME96_WCR_ADAT;
12511da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
12521da177e4SLinus Torvalds 	rme96->playback_substream = substream;
12531da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_adat_info;
12561da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
12571da177e4SLinus Torvalds             snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG &&
12581da177e4SLinus Torvalds 	    (rate = snd_rme96_capture_getrate(rme96, &dummy)) > 0)
12591da177e4SLinus Torvalds 	{
12601da177e4SLinus Torvalds                 /* slave clock */
1261918f3a0eSClemens Ladisch                 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12621da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12631da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12641da177e4SLinus Torvalds 	}
12652ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12661da177e4SLinus Torvalds 	return 0;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds static int
1270a3aefd88STakashi Iwai snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds         int isadat, rate;
1273a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1274a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12751da177e4SLinus Torvalds 
1276b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12771da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_adat_info;
12781da177e4SLinus Torvalds         if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
12791da177e4SLinus Torvalds                 /* makes no sense to use analog input. Note that analog
12801da177e4SLinus Torvalds                    expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
12811da177e4SLinus Torvalds                 return -EIO;
12821da177e4SLinus Torvalds         }
12831da177e4SLinus Torvalds         if ((rate = snd_rme96_capture_getrate(rme96, &isadat)) > 0) {
12841da177e4SLinus Torvalds                 if (!isadat) {
12851da177e4SLinus Torvalds                         return -EIO;
12861da177e4SLinus Torvalds                 }
1287918f3a0eSClemens Ladisch                 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12881da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12891da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12901da177e4SLinus Torvalds         }
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12931da177e4SLinus Torvalds         if (rme96->capture_substream != NULL) {
12941da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12951da177e4SLinus Torvalds                 return -EBUSY;
12961da177e4SLinus Torvalds         }
12971da177e4SLinus Torvalds 	rme96->capture_substream = substream;
12981da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12991da177e4SLinus Torvalds 
13002ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
13011da177e4SLinus Torvalds 	return 0;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds static int
1305a3aefd88STakashi Iwai snd_rme96_playback_close(struct snd_pcm_substream *substream)
13061da177e4SLinus Torvalds {
1307a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13081da177e4SLinus Torvalds 	int spdif = 0;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13111da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
1312b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 	rme96->playback_substream = NULL;
13151da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
13161da177e4SLinus Torvalds 	spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
13171da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13181da177e4SLinus Torvalds 	if (spdif) {
13191da177e4SLinus Torvalds 		rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
13201da177e4SLinus Torvalds 		snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
13211da177e4SLinus Torvalds 			       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 	return 0;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds static int
1327a3aefd88STakashi Iwai snd_rme96_capture_close(struct snd_pcm_substream *substream)
13281da177e4SLinus Torvalds {
1329a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13321da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
1333b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
13341da177e4SLinus Torvalds 	}
13351da177e4SLinus Torvalds 	rme96->capture_substream = NULL;
13361da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
13371da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13381da177e4SLinus Torvalds 	return 0;
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds static int
1342a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
13431da177e4SLinus Torvalds {
1344a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13471da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
1348b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
13511da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13521da177e4SLinus Torvalds 	return 0;
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds static int
1356a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
13571da177e4SLinus Torvalds {
1358a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13611da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
1362b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
13651da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13661da177e4SLinus Torvalds 	return 0;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds static int
1370a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
13711da177e4SLinus Torvalds 			   int cmd)
13721da177e4SLinus Torvalds {
1373a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1374b892ca1cSKnut Petersen 	struct snd_pcm_substream *s;
1375b892ca1cSKnut Petersen 	bool sync;
1376b892ca1cSKnut Petersen 
1377b892ca1cSKnut Petersen 	snd_pcm_group_for_each_entry(s, substream) {
1378b892ca1cSKnut Petersen 		if (snd_pcm_substream_chip(s) == rme96)
1379b892ca1cSKnut Petersen 			snd_pcm_trigger_done(s, substream);
1380b892ca1cSKnut Petersen 	}
1381b892ca1cSKnut Petersen 
1382b892ca1cSKnut Petersen 	sync = (rme96->playback_substream && rme96->capture_substream) &&
1383b892ca1cSKnut Petersen 	       (rme96->playback_substream->group ==
1384b892ca1cSKnut Petersen 		rme96->capture_substream->group);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	switch (cmd) {
13871da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
13881da177e4SLinus Torvalds 		if (!RME96_ISPLAYING(rme96)) {
1389b892ca1cSKnut Petersen 			if (substream != rme96->playback_substream)
13901da177e4SLinus Torvalds 				return -EBUSY;
1391b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1392b892ca1cSKnut Petersen 						 : RME96_START_PLAYBACK);
13931da177e4SLinus Torvalds 		}
13941da177e4SLinus Torvalds 		break;
13951da177e4SLinus Torvalds 
1396528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_SUSPEND:
13971da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
13981da177e4SLinus Torvalds 		if (RME96_ISPLAYING(rme96)) {
1399b892ca1cSKnut Petersen 			if (substream != rme96->playback_substream)
14001da177e4SLinus Torvalds 				return -EBUSY;
1401b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1402b892ca1cSKnut Petersen 						 :  RME96_STOP_PLAYBACK);
14031da177e4SLinus Torvalds 		}
14041da177e4SLinus Torvalds 		break;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1407b892ca1cSKnut Petersen 		if (RME96_ISPLAYING(rme96))
1408b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1409b892ca1cSKnut Petersen 						 : RME96_STOP_PLAYBACK);
14101da177e4SLinus Torvalds 		break;
14111da177e4SLinus Torvalds 
1412528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_RESUME:
14131da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1414b892ca1cSKnut Petersen 		if (!RME96_ISPLAYING(rme96))
1415b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1416b892ca1cSKnut Petersen 						 : RME96_RESUME_PLAYBACK);
14171da177e4SLinus Torvalds 		break;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	default:
14201da177e4SLinus Torvalds 		return -EINVAL;
14211da177e4SLinus Torvalds 	}
1422b892ca1cSKnut Petersen 
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds static int
1427a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
14281da177e4SLinus Torvalds 			  int cmd)
14291da177e4SLinus Torvalds {
1430a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1431b892ca1cSKnut Petersen 	struct snd_pcm_substream *s;
1432b892ca1cSKnut Petersen 	bool sync;
1433b892ca1cSKnut Petersen 
1434b892ca1cSKnut Petersen 	snd_pcm_group_for_each_entry(s, substream) {
1435b892ca1cSKnut Petersen 		if (snd_pcm_substream_chip(s) == rme96)
1436b892ca1cSKnut Petersen 			snd_pcm_trigger_done(s, substream);
1437b892ca1cSKnut Petersen 	}
1438b892ca1cSKnut Petersen 
1439b892ca1cSKnut Petersen 	sync = (rme96->playback_substream && rme96->capture_substream) &&
1440b892ca1cSKnut Petersen 	       (rme96->playback_substream->group ==
1441b892ca1cSKnut Petersen 		rme96->capture_substream->group);
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	switch (cmd) {
14441da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
14451da177e4SLinus Torvalds 		if (!RME96_ISRECORDING(rme96)) {
1446b892ca1cSKnut Petersen 			if (substream != rme96->capture_substream)
14471da177e4SLinus Torvalds 				return -EBUSY;
1448b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1449b892ca1cSKnut Petersen 						 : RME96_START_CAPTURE);
14501da177e4SLinus Torvalds 		}
14511da177e4SLinus Torvalds 		break;
14521da177e4SLinus Torvalds 
1453528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_SUSPEND:
14541da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
14551da177e4SLinus Torvalds 		if (RME96_ISRECORDING(rme96)) {
1456b892ca1cSKnut Petersen 			if (substream != rme96->capture_substream)
14571da177e4SLinus Torvalds 				return -EBUSY;
1458b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1459b892ca1cSKnut Petersen 						 : RME96_STOP_CAPTURE);
14601da177e4SLinus Torvalds 		}
14611da177e4SLinus Torvalds 		break;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1464b892ca1cSKnut Petersen 		if (RME96_ISRECORDING(rme96))
1465b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1466b892ca1cSKnut Petersen 						 : RME96_STOP_CAPTURE);
14671da177e4SLinus Torvalds 		break;
14681da177e4SLinus Torvalds 
1469528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_RESUME:
14701da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1471b892ca1cSKnut Petersen 		if (!RME96_ISRECORDING(rme96))
1472b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1473b892ca1cSKnut Petersen 						 : RME96_RESUME_CAPTURE);
14741da177e4SLinus Torvalds 		break;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	default:
14771da177e4SLinus Torvalds 		return -EINVAL;
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	return 0;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds static snd_pcm_uframes_t
1484a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
14851da177e4SLinus Torvalds {
1486a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14871da177e4SLinus Torvalds 	return snd_rme96_playback_ptr(rme96);
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds static snd_pcm_uframes_t
1491a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
14921da177e4SLinus Torvalds {
1493a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14941da177e4SLinus Torvalds 	return snd_rme96_capture_ptr(rme96);
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
1497a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
14981da177e4SLinus Torvalds 	.open =		snd_rme96_playback_spdif_open,
14991da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
15001da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15011da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
15021da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
15031da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
15041da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
15051da177e4SLinus Torvalds 	.copy =		snd_rme96_playback_copy,
15061da177e4SLinus Torvalds 	.silence =	snd_rme96_playback_silence,
15071da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15081da177e4SLinus Torvalds };
15091da177e4SLinus Torvalds 
1510a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
15111da177e4SLinus Torvalds 	.open =		snd_rme96_capture_spdif_open,
15121da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
15131da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15141da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
15151da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
15161da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
15171da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
15181da177e4SLinus Torvalds 	.copy =		snd_rme96_capture_copy,
15191da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15201da177e4SLinus Torvalds };
15211da177e4SLinus Torvalds 
1522a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_playback_adat_ops = {
15231da177e4SLinus Torvalds 	.open =		snd_rme96_playback_adat_open,
15241da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
15251da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15261da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
15271da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
15281da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
15291da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
15301da177e4SLinus Torvalds 	.copy =		snd_rme96_playback_copy,
15311da177e4SLinus Torvalds 	.silence =	snd_rme96_playback_silence,
15321da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15331da177e4SLinus Torvalds };
15341da177e4SLinus Torvalds 
1535a3aefd88STakashi Iwai static struct snd_pcm_ops snd_rme96_capture_adat_ops = {
15361da177e4SLinus Torvalds 	.open =		snd_rme96_capture_adat_open,
15371da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
15381da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
15391da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
15401da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
15411da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
15421da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
15431da177e4SLinus Torvalds 	.copy =		snd_rme96_capture_copy,
15441da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15451da177e4SLinus Torvalds };
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static void
15481da177e4SLinus Torvalds snd_rme96_free(void *private_data)
15491da177e4SLinus Torvalds {
1550a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)private_data;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (rme96 == NULL) {
15531da177e4SLinus Torvalds 	        return;
15541da177e4SLinus Torvalds 	}
15551da177e4SLinus Torvalds 	if (rme96->irq >= 0) {
1556b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_BOTH);
15571da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_DAC_EN;
15581da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
15591da177e4SLinus Torvalds 		free_irq(rme96->irq, (void *)rme96);
15601da177e4SLinus Torvalds 		rme96->irq = -1;
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	if (rme96->iobase) {
15631da177e4SLinus Torvalds 		iounmap(rme96->iobase);
15641da177e4SLinus Torvalds 		rme96->iobase = NULL;
15651da177e4SLinus Torvalds 	}
15661da177e4SLinus Torvalds 	if (rme96->port) {
15671da177e4SLinus Torvalds 		pci_release_regions(rme96->pci);
15681da177e4SLinus Torvalds 		rme96->port = 0;
15691da177e4SLinus Torvalds 	}
1570a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
1571528ba522SKnut Petersen 	vfree(rme96->playback_suspend_buffer);
1572528ba522SKnut Petersen 	vfree(rme96->capture_suspend_buffer);
1573528ba522SKnut Petersen #endif
15741da177e4SLinus Torvalds 	pci_disable_device(rme96->pci);
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds static void
1578a3aefd88STakashi Iwai snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
15791da177e4SLinus Torvalds {
15809fe856e4SJoe Perches 	struct rme96 *rme96 = pcm->private_data;
15811da177e4SLinus Torvalds 	rme96->spdif_pcm = NULL;
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds static void
1585a3aefd88STakashi Iwai snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
15861da177e4SLinus Torvalds {
15879fe856e4SJoe Perches 	struct rme96 *rme96 = pcm->private_data;
15881da177e4SLinus Torvalds 	rme96->adat_pcm = NULL;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
1591e23e7a14SBill Pemberton static int
1592a3aefd88STakashi Iwai snd_rme96_create(struct rme96 *rme96)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	struct pci_dev *pci = rme96->pci;
15951da177e4SLinus Torvalds 	int err;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	rme96->irq = -1;
15981da177e4SLinus Torvalds 	spin_lock_init(&rme96->lock);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
16011da177e4SLinus Torvalds 		return err;
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "RME96")) < 0)
16041da177e4SLinus Torvalds 		return err;
16051da177e4SLinus Torvalds 	rme96->port = pci_resource_start(rme96->pci, 0);
16061da177e4SLinus Torvalds 
160744977b71SHarvey Harrison 	rme96->iobase = ioremap_nocache(rme96->port, RME96_IO_SIZE);
160844977b71SHarvey Harrison 	if (!rme96->iobase) {
1609342cd934STakashi Iwai 		dev_err(rme96->card->dev,
1610342cd934STakashi Iwai 			"unable to remap memory region 0x%lx-0x%lx\n",
1611342cd934STakashi Iwai 			rme96->port, rme96->port + RME96_IO_SIZE - 1);
1612688956f2STakashi Iwai 		return -ENOMEM;
1613688956f2STakashi Iwai 	}
1614688956f2STakashi Iwai 
1615437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_rme96_interrupt, IRQF_SHARED,
1616934c2b6dSTakashi Iwai 			KBUILD_MODNAME, rme96)) {
1617342cd934STakashi Iwai 		dev_err(rme96->card->dev, "unable to grab IRQ %d\n", pci->irq);
16181da177e4SLinus Torvalds 		return -EBUSY;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 	rme96->irq = pci->irq;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	/* read the card's revision number */
16231da177e4SLinus Torvalds 	pci_read_config_byte(pci, 8, &rme96->rev);
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	/* set up ALSA pcm device for S/PDIF */
16261da177e4SLinus Torvalds 	if ((err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
16271da177e4SLinus Torvalds 			       1, 1, &rme96->spdif_pcm)) < 0)
16281da177e4SLinus Torvalds 	{
16291da177e4SLinus Torvalds 		return err;
16301da177e4SLinus Torvalds 	}
16311da177e4SLinus Torvalds 	rme96->spdif_pcm->private_data = rme96;
16321da177e4SLinus Torvalds 	rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
16331da177e4SLinus Torvalds 	strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
16341da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
16351da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	rme96->spdif_pcm->info_flags = 0;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	/* set up ALSA pcm device for ADAT */
16408b7fc421SRoland Dreier 	if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
16411da177e4SLinus Torvalds 		/* ADAT is not available on the base model */
16421da177e4SLinus Torvalds 		rme96->adat_pcm = NULL;
16431da177e4SLinus Torvalds 	} else {
16441da177e4SLinus Torvalds 		if ((err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
16451da177e4SLinus Torvalds 				       1, 1, &rme96->adat_pcm)) < 0)
16461da177e4SLinus Torvalds 		{
16471da177e4SLinus Torvalds 			return err;
16481da177e4SLinus Torvalds 		}
16491da177e4SLinus Torvalds 		rme96->adat_pcm->private_data = rme96;
16501da177e4SLinus Torvalds 		rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
16511da177e4SLinus Torvalds 		strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
16521da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
16531da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 		rme96->adat_pcm->info_flags = 0;
16561da177e4SLinus Torvalds 	}
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
16591da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	/* make sure playback/capture is stopped, if by some reason active */
1662b892ca1cSKnut Petersen 	snd_rme96_trigger(rme96, RME96_STOP_BOTH);
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	/* set default values in registers */
16651da177e4SLinus Torvalds 	rme96->wcreg =
16661da177e4SLinus Torvalds 		RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
16671da177e4SLinus Torvalds 		RME96_WCR_SEL |    /* normal playback */
16681da177e4SLinus Torvalds 		RME96_WCR_MASTER | /* set to master clock mode */
16691da177e4SLinus Torvalds 		RME96_WCR_INP_0;   /* set coaxial input */
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
16741da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	/* reset the ADC */
16771da177e4SLinus Torvalds 	writel(rme96->areg | RME96_AR_PD2,
16781da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_ADDITIONAL_REG);
16791da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	/* reset and enable the DAC (order is important). */
16821da177e4SLinus Torvalds 	snd_rme96_reset_dac(rme96);
16831da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_DAC_EN;
16841da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	/* reset playback and record buffer pointers */
16871da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
16881da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	/* reset volume */
16911da177e4SLinus Torvalds 	rme96->vol[0] = rme96->vol[1] = 0;
16921da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_OUT(rme96)) {
16931da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	/* init switch interface */
16971da177e4SLinus Torvalds 	if ((err = snd_rme96_create_switches(rme96->card, rme96)) < 0) {
16981da177e4SLinus Torvalds 		return err;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds         /* init proc interface */
17021da177e4SLinus Torvalds 	snd_rme96_proc_init(rme96);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	return 0;
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds /*
17081da177e4SLinus Torvalds  * proc interface
17091da177e4SLinus Torvalds  */
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds static void
1712a3aefd88STakashi Iwai snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
17131da177e4SLinus Torvalds {
17141da177e4SLinus Torvalds 	int n;
17159fe856e4SJoe Perches 	struct rme96 *rme96 = entry->private_data;
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	snd_iprintf(buffer, rme96->card->longname);
17201da177e4SLinus Torvalds 	snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nGeneral settings\n");
17231da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_IDIS) {
17241da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: N/A (interrupts "
17251da177e4SLinus Torvalds 			    "disabled)\n");
17261da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_ISEL) {
17271da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 2048 bytes\n");
17281da177e4SLinus Torvalds 	} else {
17291da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 8192 bytes\n");
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nInput settings\n");
17321da177e4SLinus Torvalds 	switch (snd_rme96_getinputtype(rme96)) {
17331da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
17341da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: optical");
17351da177e4SLinus Torvalds 		break;
17361da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
17371da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: coaxial");
17381da177e4SLinus Torvalds 		break;
17391da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
17401da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: internal");
17411da177e4SLinus Torvalds 		break;
17421da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
17431da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: XLR");
17441da177e4SLinus Torvalds 		break;
17451da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
17461da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: analog");
17471da177e4SLinus Torvalds 		break;
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds 	if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17501da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n  sample rate: no valid signal\n");
17511da177e4SLinus Torvalds 	} else {
17521da177e4SLinus Torvalds 		if (n) {
17531da177e4SLinus Torvalds 			snd_iprintf(buffer, " (8 channels)\n");
17541da177e4SLinus Torvalds 		} else {
17551da177e4SLinus Torvalds 			snd_iprintf(buffer, " (2 channels)\n");
17561da177e4SLinus Torvalds 		}
17571da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample rate: %d Hz\n",
17581da177e4SLinus Torvalds 			    snd_rme96_capture_getrate(rme96, &n));
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24_2) {
17611da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17621da177e4SLinus Torvalds 	} else {
17631da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nOutput settings\n");
17671da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_SEL) {
17681da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: normal playback\n");
17691da177e4SLinus Torvalds 	} else {
17701da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: same as input\n");
17711da177e4SLinus Torvalds 	}
17721da177e4SLinus Torvalds 	snd_iprintf(buffer, "  sample rate: %d Hz\n",
17731da177e4SLinus Torvalds 		    snd_rme96_playback_getrate(rme96));
17741da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24) {
17751da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17761da177e4SLinus Torvalds 	} else {
17771da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
17801da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: word clock\n");
17811da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_MASTER) {
17821da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: internal\n");
17831da177e4SLinus Torvalds 	} else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
17841da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to analog input setting)\n");
17851da177e4SLinus Torvalds 	} else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17861da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to no valid signal)\n");
17871da177e4SLinus Torvalds 	} else {
17881da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync\n");
17891da177e4SLinus Torvalds 	}
17901da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_PRO) {
17911da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: AES/EBU (professional)\n");
17921da177e4SLinus Torvalds 	} else {
17931da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: IEC958 (consumer)\n");
17941da177e4SLinus Torvalds 	}
17951da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_EMP) {
17961da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: on\n");
17971da177e4SLinus Torvalds 	} else {
17981da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: off\n");
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_DOLBY) {
18011da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): on\n");
18021da177e4SLinus Torvalds 	} else {
18031da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): off\n");
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_IN(rme96)) {
18061da177e4SLinus Torvalds 		snd_iprintf(buffer, "\nAnalog output settings\n");
18071da177e4SLinus Torvalds 		switch (snd_rme96_getmontracks(rme96)) {
18081da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_1_2:
18091da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 1+2\n");
18101da177e4SLinus Torvalds 			break;
18111da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_3_4:
18121da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 3+4\n");
18131da177e4SLinus Torvalds 			break;
18141da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_5_6:
18151da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 5+6\n");
18161da177e4SLinus Torvalds 			break;
18171da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_7_8:
18181da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 7+8\n");
18191da177e4SLinus Torvalds 			break;
18201da177e4SLinus Torvalds 		}
18211da177e4SLinus Torvalds 		switch (snd_rme96_getattenuation(rme96)) {
18221da177e4SLinus Torvalds 		case RME96_ATTENUATION_0:
18231da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: 0 dB\n");
18241da177e4SLinus Torvalds 			break;
18251da177e4SLinus Torvalds 		case RME96_ATTENUATION_6:
18261da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -6 dB\n");
18271da177e4SLinus Torvalds 			break;
18281da177e4SLinus Torvalds 		case RME96_ATTENUATION_12:
18291da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -12 dB\n");
18301da177e4SLinus Torvalds 			break;
18311da177e4SLinus Torvalds 		case RME96_ATTENUATION_18:
18321da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -18 dB\n");
18331da177e4SLinus Torvalds 			break;
18341da177e4SLinus Torvalds 		}
18351da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume left: %u\n", rme96->vol[0]);
18361da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume right: %u\n", rme96->vol[1]);
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
1840e23e7a14SBill Pemberton static void snd_rme96_proc_init(struct rme96 *rme96)
18411da177e4SLinus Torvalds {
1842a3aefd88STakashi Iwai 	struct snd_info_entry *entry;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	if (! snd_card_proc_new(rme96->card, "rme96", &entry))
1845bf850204STakashi Iwai 		snd_info_set_text_ops(entry, rme96, snd_rme96_proc_read);
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds /*
18491da177e4SLinus Torvalds  * control interface
18501da177e4SLinus Torvalds  */
18511da177e4SLinus Torvalds 
1852a5ce8890STakashi Iwai #define snd_rme96_info_loopback_control		snd_ctl_boolean_mono_info
1853a5ce8890STakashi Iwai 
18541da177e4SLinus Torvalds static int
1855a3aefd88STakashi Iwai snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18561da177e4SLinus Torvalds {
1857a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18601da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
18611da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18621da177e4SLinus Torvalds 	return 0;
18631da177e4SLinus Torvalds }
18641da177e4SLinus Torvalds static int
1865a3aefd88STakashi Iwai snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18661da177e4SLinus Torvalds {
1867a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18681da177e4SLinus Torvalds 	unsigned int val;
18691da177e4SLinus Torvalds 	int change;
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
18721da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18731da177e4SLinus Torvalds 	val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
18741da177e4SLinus Torvalds 	change = val != rme96->wcreg;
18751da177e4SLinus Torvalds 	rme96->wcreg = val;
18761da177e4SLinus Torvalds 	writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
18771da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18781da177e4SLinus Torvalds 	return change;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds static int
1882a3aefd88STakashi Iwai snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18831da177e4SLinus Torvalds {
18849c30d46aSTakashi Iwai 	static const char * const _texts[5] = {
18859c30d46aSTakashi Iwai 		"Optical", "Coaxial", "Internal", "XLR", "Analog"
18869c30d46aSTakashi Iwai 	};
1887a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18889c30d46aSTakashi Iwai 	const char *texts[5] = {
18899c30d46aSTakashi Iwai 		_texts[0], _texts[1], _texts[2], _texts[3], _texts[4]
18909c30d46aSTakashi Iwai 	};
18919c30d46aSTakashi Iwai 	int num_items;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	switch (rme96->pci->device) {
18948b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
18958b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
18969c30d46aSTakashi Iwai 		num_items = 3;
18971da177e4SLinus Torvalds 		break;
18988b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
18999c30d46aSTakashi Iwai 		num_items = 4;
19001da177e4SLinus Torvalds 		break;
19018b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19021da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19031da177e4SLinus Torvalds 			/* PST */
19049c30d46aSTakashi Iwai 			num_items = 4;
19051da177e4SLinus Torvalds 			texts[3] = _texts[4]; /* Analog instead of XLR */
19061da177e4SLinus Torvalds 		} else {
19071da177e4SLinus Torvalds 			/* PAD */
19089c30d46aSTakashi Iwai 			num_items = 5;
19091da177e4SLinus Torvalds 		}
19101da177e4SLinus Torvalds 		break;
19111da177e4SLinus Torvalds 	default:
19121da177e4SLinus Torvalds 		snd_BUG();
19139c30d46aSTakashi Iwai 		return -EINVAL;
19141da177e4SLinus Torvalds 	}
19159c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, num_items, texts);
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds static int
1918a3aefd88STakashi Iwai snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19191da177e4SLinus Torvalds {
1920a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19211da177e4SLinus Torvalds 	unsigned int items = 3;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19241da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19278b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19288b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19291da177e4SLinus Torvalds 		items = 3;
19301da177e4SLinus Torvalds 		break;
19318b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19321da177e4SLinus Torvalds 		items = 4;
19331da177e4SLinus Torvalds 		break;
19348b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19351da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19361da177e4SLinus Torvalds 			/* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
19371da177e4SLinus Torvalds 			if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
19381da177e4SLinus Torvalds 				ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
19391da177e4SLinus Torvalds 			}
19401da177e4SLinus Torvalds 			items = 4;
19411da177e4SLinus Torvalds 		} else {
19421da177e4SLinus Torvalds 			items = 5;
19431da177e4SLinus Torvalds 		}
19441da177e4SLinus Torvalds 		break;
19451da177e4SLinus Torvalds 	default:
19461da177e4SLinus Torvalds 		snd_BUG();
19471da177e4SLinus Torvalds 		break;
19481da177e4SLinus Torvalds 	}
19491da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0] >= items) {
19501da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = items - 1;
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19541da177e4SLinus Torvalds 	return 0;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds static int
1957a3aefd88STakashi Iwai snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19581da177e4SLinus Torvalds {
1959a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19601da177e4SLinus Torvalds 	unsigned int val;
19611da177e4SLinus Torvalds 	int change, items = 3;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19648b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19658b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19661da177e4SLinus Torvalds 		items = 3;
19671da177e4SLinus Torvalds 		break;
19688b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19691da177e4SLinus Torvalds 		items = 4;
19701da177e4SLinus Torvalds 		break;
19718b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19721da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19731da177e4SLinus Torvalds 			items = 4;
19741da177e4SLinus Torvalds 		} else {
19751da177e4SLinus Torvalds 			items = 5;
19761da177e4SLinus Torvalds 		}
19771da177e4SLinus Torvalds 		break;
19781da177e4SLinus Torvalds 	default:
19791da177e4SLinus Torvalds 		snd_BUG();
19801da177e4SLinus Torvalds 		break;
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % items;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	/* special case for PST */
19858b7fc421SRoland Dreier 	if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
19861da177e4SLinus Torvalds 		if (val == RME96_INPUT_XLR) {
19871da177e4SLinus Torvalds 			val = RME96_INPUT_ANALOG;
19881da177e4SLinus Torvalds 		}
19891da177e4SLinus Torvalds 	}
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19921da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getinputtype(rme96);
19931da177e4SLinus Torvalds 	snd_rme96_setinputtype(rme96, val);
19941da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19951da177e4SLinus Torvalds 	return change;
19961da177e4SLinus Torvalds }
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds static int
1999a3aefd88STakashi Iwai snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20001da177e4SLinus Torvalds {
20019c30d46aSTakashi Iwai 	static const char * const texts[3] = { "AutoSync", "Internal", "Word" };
20021da177e4SLinus Torvalds 
20039c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds static int
2006a3aefd88STakashi Iwai snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20071da177e4SLinus Torvalds {
2008a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20111da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
20121da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20131da177e4SLinus Torvalds 	return 0;
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds static int
2016a3aefd88STakashi Iwai snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20171da177e4SLinus Torvalds {
2018a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20191da177e4SLinus Torvalds 	unsigned int val;
20201da177e4SLinus Torvalds 	int change;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 3;
20231da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20241da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getclockmode(rme96);
20251da177e4SLinus Torvalds 	snd_rme96_setclockmode(rme96, val);
20261da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20271da177e4SLinus Torvalds 	return change;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds static int
2031a3aefd88STakashi Iwai snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20321da177e4SLinus Torvalds {
20339c30d46aSTakashi Iwai 	static const char * const texts[4] = {
20349c30d46aSTakashi Iwai 		"0 dB", "-6 dB", "-12 dB", "-18 dB"
20359c30d46aSTakashi Iwai 	};
20361da177e4SLinus Torvalds 
20379c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 4, texts);
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds static int
2040a3aefd88STakashi Iwai snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20411da177e4SLinus Torvalds {
2042a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20451da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
20461da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20471da177e4SLinus Torvalds 	return 0;
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds static int
2050a3aefd88STakashi Iwai snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20511da177e4SLinus Torvalds {
2052a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20531da177e4SLinus Torvalds 	unsigned int val;
20541da177e4SLinus Torvalds 	int change;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20571da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getattenuation(rme96);
20601da177e4SLinus Torvalds 	snd_rme96_setattenuation(rme96, val);
20611da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20621da177e4SLinus Torvalds 	return change;
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds static int
2066a3aefd88STakashi Iwai snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20671da177e4SLinus Torvalds {
20689c30d46aSTakashi Iwai 	static const char * const texts[4] = { "1+2", "3+4", "5+6", "7+8" };
20691da177e4SLinus Torvalds 
20709c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 4, texts);
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds static int
2073a3aefd88STakashi Iwai snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20741da177e4SLinus Torvalds {
2075a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20781da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
20791da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20801da177e4SLinus Torvalds 	return 0;
20811da177e4SLinus Torvalds }
20821da177e4SLinus Torvalds static int
2083a3aefd88STakashi Iwai snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20841da177e4SLinus Torvalds {
2085a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20861da177e4SLinus Torvalds 	unsigned int val;
20871da177e4SLinus Torvalds 	int change;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20901da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20911da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getmontracks(rme96);
20921da177e4SLinus Torvalds 	snd_rme96_setmontracks(rme96, val);
20931da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20941da177e4SLinus Torvalds 	return change;
20951da177e4SLinus Torvalds }
20961da177e4SLinus Torvalds 
2097a3aefd88STakashi Iwai static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
20981da177e4SLinus Torvalds {
20991da177e4SLinus Torvalds 	u32 val = 0;
21001da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
21011da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
21021da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
21031da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
21041da177e4SLinus Torvalds 	else
21051da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
21061da177e4SLinus Torvalds 	return val;
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
2109a3aefd88STakashi Iwai static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
21101da177e4SLinus Torvalds {
21111da177e4SLinus Torvalds 	aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
21121da177e4SLinus Torvalds 			 ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
21131da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
21141da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
21151da177e4SLinus Torvalds 	else
21161da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
2119a3aefd88STakashi Iwai static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21221da177e4SLinus Torvalds 	uinfo->count = 1;
21231da177e4SLinus Torvalds 	return 0;
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
2126a3aefd88STakashi Iwai static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21271da177e4SLinus Torvalds {
2128a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
21311da177e4SLinus Torvalds 	return 0;
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
2134a3aefd88STakashi Iwai static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21351da177e4SLinus Torvalds {
2136a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21371da177e4SLinus Torvalds 	int change;
21381da177e4SLinus Torvalds 	u32 val;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21411da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21421da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif;
21431da177e4SLinus Torvalds 	rme96->wcreg_spdif = val;
21441da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21451da177e4SLinus Torvalds 	return change;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
2148a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21491da177e4SLinus Torvalds {
21501da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21511da177e4SLinus Torvalds 	uinfo->count = 1;
21521da177e4SLinus Torvalds 	return 0;
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
2155a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21561da177e4SLinus Torvalds {
2157a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
21601da177e4SLinus Torvalds 	return 0;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
2163a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21641da177e4SLinus Torvalds {
2165a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21661da177e4SLinus Torvalds 	int change;
21671da177e4SLinus Torvalds 	u32 val;
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21701da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21711da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif_stream;
21721da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = val;
21731da177e4SLinus Torvalds 	rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
21741da177e4SLinus Torvalds 	rme96->wcreg |= val;
21751da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
21761da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21771da177e4SLinus Torvalds 	return change;
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
2180a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21811da177e4SLinus Torvalds {
21821da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21831da177e4SLinus Torvalds 	uinfo->count = 1;
21841da177e4SLinus Torvalds 	return 0;
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
2187a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21881da177e4SLinus Torvalds {
21891da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
21901da177e4SLinus Torvalds 	return 0;
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds static int
2194a3aefd88STakashi Iwai snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21951da177e4SLinus Torvalds {
2196a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
21991da177e4SLinus Torvalds         uinfo->count = 2;
22001da177e4SLinus Torvalds         uinfo->value.integer.min = 0;
22011da177e4SLinus Torvalds 	uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
22021da177e4SLinus Torvalds         return 0;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds static int
2206a3aefd88STakashi Iwai snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
22071da177e4SLinus Torvalds {
2208a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
22111da177e4SLinus Torvalds         u->value.integer.value[0] = rme96->vol[0];
22121da177e4SLinus Torvalds         u->value.integer.value[1] = rme96->vol[1];
22131da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds         return 0;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds static int
2219a3aefd88STakashi Iwai snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
22201da177e4SLinus Torvalds {
2221a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
22221da177e4SLinus Torvalds         int change = 0;
22234e98d6a7STakashi Iwai 	unsigned int vol, maxvol;
22241da177e4SLinus Torvalds 
22254e98d6a7STakashi Iwai 
22264e98d6a7STakashi Iwai 	if (!RME96_HAS_ANALOG_OUT(rme96))
22271da177e4SLinus Torvalds 		return -EINVAL;
22284e98d6a7STakashi Iwai 	maxvol = RME96_185X_MAX_OUT(rme96);
22291da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
22304e98d6a7STakashi Iwai 	vol = u->value.integer.value[0];
22314e98d6a7STakashi Iwai 	if (vol != rme96->vol[0] && vol <= maxvol) {
22324e98d6a7STakashi Iwai 		rme96->vol[0] = vol;
22331da177e4SLinus Torvalds 		change = 1;
22341da177e4SLinus Torvalds 	}
22354e98d6a7STakashi Iwai 	vol = u->value.integer.value[1];
22364e98d6a7STakashi Iwai 	if (vol != rme96->vol[1] && vol <= maxvol) {
22374e98d6a7STakashi Iwai 		rme96->vol[1] = vol;
22381da177e4SLinus Torvalds 		change = 1;
22391da177e4SLinus Torvalds 	}
22404e98d6a7STakashi Iwai 	if (change)
22411da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
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 
2354a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
2355528ba522SKnut Petersen 
2356a932be91STakashi Iwai static int rme96_suspend(struct device *dev)
2357528ba522SKnut Petersen {
2358a932be91STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2359528ba522SKnut Petersen 	struct rme96 *rme96 = card->private_data;
2360528ba522SKnut Petersen 
2361528ba522SKnut Petersen 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2362528ba522SKnut Petersen 	snd_pcm_suspend(rme96->playback_substream);
2363528ba522SKnut Petersen 	snd_pcm_suspend(rme96->capture_substream);
2364528ba522SKnut Petersen 
2365528ba522SKnut Petersen 	/* save capture & playback pointers */
2366528ba522SKnut Petersen 	rme96->playback_pointer = readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
2367528ba522SKnut Petersen 				  & RME96_RCR_AUDIO_ADDR_MASK;
2368528ba522SKnut Petersen 	rme96->capture_pointer = readl(rme96->iobase + RME96_IO_GET_REC_POS)
2369528ba522SKnut Petersen 				 & RME96_RCR_AUDIO_ADDR_MASK;
2370528ba522SKnut Petersen 
2371528ba522SKnut Petersen 	/* save playback and capture buffers */
2372528ba522SKnut Petersen 	memcpy_fromio(rme96->playback_suspend_buffer,
2373528ba522SKnut Petersen 		      rme96->iobase + RME96_IO_PLAY_BUFFER, RME96_BUFFER_SIZE);
2374528ba522SKnut Petersen 	memcpy_fromio(rme96->capture_suspend_buffer,
2375528ba522SKnut Petersen 		      rme96->iobase + RME96_IO_REC_BUFFER, RME96_BUFFER_SIZE);
2376528ba522SKnut Petersen 
2377528ba522SKnut Petersen 	/* disable the DAC  */
2378528ba522SKnut Petersen 	rme96->areg &= ~RME96_AR_DAC_EN;
2379528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2380528ba522SKnut Petersen 	return 0;
2381528ba522SKnut Petersen }
2382528ba522SKnut Petersen 
2383a932be91STakashi Iwai static int rme96_resume(struct device *dev)
2384528ba522SKnut Petersen {
2385a932be91STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2386528ba522SKnut Petersen 	struct rme96 *rme96 = card->private_data;
2387528ba522SKnut Petersen 
2388528ba522SKnut Petersen 	/* reset playback and record buffer pointers */
2389528ba522SKnut Petersen 	writel(0, rme96->iobase + RME96_IO_SET_PLAY_POS
2390528ba522SKnut Petersen 		  + rme96->playback_pointer);
2391528ba522SKnut Petersen 	writel(0, rme96->iobase + RME96_IO_SET_REC_POS
2392528ba522SKnut Petersen 		  + rme96->capture_pointer);
2393528ba522SKnut Petersen 
2394528ba522SKnut Petersen 	/* restore playback and capture buffers */
2395528ba522SKnut Petersen 	memcpy_toio(rme96->iobase + RME96_IO_PLAY_BUFFER,
2396528ba522SKnut Petersen 		    rme96->playback_suspend_buffer, RME96_BUFFER_SIZE);
2397528ba522SKnut Petersen 	memcpy_toio(rme96->iobase + RME96_IO_REC_BUFFER,
2398528ba522SKnut Petersen 		    rme96->capture_suspend_buffer, RME96_BUFFER_SIZE);
2399528ba522SKnut Petersen 
2400528ba522SKnut Petersen 	/* reset the ADC */
2401528ba522SKnut Petersen 	writel(rme96->areg | RME96_AR_PD2,
2402528ba522SKnut Petersen 	       rme96->iobase + RME96_IO_ADDITIONAL_REG);
2403528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2404528ba522SKnut Petersen 
2405528ba522SKnut Petersen 	/* reset and enable DAC, restore analog volume */
2406528ba522SKnut Petersen 	snd_rme96_reset_dac(rme96);
2407528ba522SKnut Petersen 	rme96->areg |= RME96_AR_DAC_EN;
2408528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2409528ba522SKnut Petersen 	if (RME96_HAS_ANALOG_OUT(rme96)) {
2410528ba522SKnut Petersen 		usleep_range(3000, 10000);
2411528ba522SKnut Petersen 		snd_rme96_apply_dac_volume(rme96);
2412528ba522SKnut Petersen 	}
2413528ba522SKnut Petersen 
2414528ba522SKnut Petersen 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2415528ba522SKnut Petersen 
2416528ba522SKnut Petersen 	return 0;
2417528ba522SKnut Petersen }
2418528ba522SKnut Petersen 
2419a932be91STakashi Iwai static SIMPLE_DEV_PM_OPS(rme96_pm, rme96_suspend, rme96_resume);
2420a932be91STakashi Iwai #define RME96_PM_OPS	&rme96_pm
2421a932be91STakashi Iwai #else
2422a932be91STakashi Iwai #define RME96_PM_OPS	NULL
2423a932be91STakashi Iwai #endif /* CONFIG_PM_SLEEP */
2424528ba522SKnut Petersen 
2425a3aefd88STakashi Iwai static void snd_rme96_card_free(struct snd_card *card)
24261da177e4SLinus Torvalds {
24271da177e4SLinus Torvalds 	snd_rme96_free(card->private_data);
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
2430e23e7a14SBill Pemberton static int
24311da177e4SLinus Torvalds snd_rme96_probe(struct pci_dev *pci,
24321da177e4SLinus Torvalds 		const struct pci_device_id *pci_id)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	static int dev;
2435a3aefd88STakashi Iwai 	struct rme96 *rme96;
2436a3aefd88STakashi Iwai 	struct snd_card *card;
24371da177e4SLinus Torvalds 	int err;
24381da177e4SLinus Torvalds 	u8 val;
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS) {
24411da177e4SLinus Torvalds 		return -ENODEV;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 	if (!enable[dev]) {
24441da177e4SLinus Torvalds 		dev++;
24451da177e4SLinus Torvalds 		return -ENOENT;
24461da177e4SLinus Torvalds 	}
244760c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2448e58de7baSTakashi Iwai 			   sizeof(struct rme96), &card);
2449e58de7baSTakashi Iwai 	if (err < 0)
2450e58de7baSTakashi Iwai 		return err;
24511da177e4SLinus Torvalds 	card->private_free = snd_rme96_card_free;
24529fe856e4SJoe Perches 	rme96 = card->private_data;
24531da177e4SLinus Torvalds 	rme96->card = card;
24541da177e4SLinus Torvalds 	rme96->pci = pci;
24551da177e4SLinus Torvalds 	if ((err = snd_rme96_create(rme96)) < 0) {
24561da177e4SLinus Torvalds 		snd_card_free(card);
24571da177e4SLinus Torvalds 		return err;
24581da177e4SLinus Torvalds 	}
24591da177e4SLinus Torvalds 
2460a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
2461528ba522SKnut Petersen 	rme96->playback_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2462528ba522SKnut Petersen 	if (!rme96->playback_suspend_buffer) {
2463342cd934STakashi Iwai 		dev_err(card->dev,
2464528ba522SKnut Petersen 			   "Failed to allocate playback suspend buffer!\n");
2465528ba522SKnut Petersen 		snd_card_free(card);
2466528ba522SKnut Petersen 		return -ENOMEM;
2467528ba522SKnut Petersen 	}
2468528ba522SKnut Petersen 	rme96->capture_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2469528ba522SKnut Petersen 	if (!rme96->capture_suspend_buffer) {
2470342cd934STakashi Iwai 		dev_err(card->dev,
2471528ba522SKnut Petersen 			   "Failed to allocate capture suspend buffer!\n");
2472528ba522SKnut Petersen 		snd_card_free(card);
2473528ba522SKnut Petersen 		return -ENOMEM;
2474528ba522SKnut Petersen 	}
2475528ba522SKnut Petersen #endif
2476528ba522SKnut Petersen 
24771da177e4SLinus Torvalds 	strcpy(card->driver, "Digi96");
24781da177e4SLinus Torvalds 	switch (rme96->pci->device) {
24798b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
24801da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96");
24811da177e4SLinus Torvalds 		break;
24828b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
24831da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8");
24841da177e4SLinus Torvalds 		break;
24858b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
24861da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8 PRO");
24871da177e4SLinus Torvalds 		break;
24888b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
24891da177e4SLinus Torvalds 		pci_read_config_byte(rme96->pci, 8, &val);
24901da177e4SLinus Torvalds 		if (val < 5) {
24911da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PAD");
24921da177e4SLinus Torvalds 		} else {
24931da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PST");
24941da177e4SLinus Torvalds 		}
24951da177e4SLinus Torvalds 		break;
24961da177e4SLinus Torvalds 	}
24971da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
24981da177e4SLinus Torvalds 		rme96->port, rme96->irq);
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
25011da177e4SLinus Torvalds 		snd_card_free(card);
25021da177e4SLinus Torvalds 		return err;
25031da177e4SLinus Torvalds 	}
25041da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
25051da177e4SLinus Torvalds 	dev++;
25061da177e4SLinus Torvalds 	return 0;
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
2509e23e7a14SBill Pemberton static void snd_rme96_remove(struct pci_dev *pci)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
25121da177e4SLinus Torvalds }
25131da177e4SLinus Torvalds 
2514e9f66d9bSTakashi Iwai static struct pci_driver rme96_driver = {
25153733e424STakashi Iwai 	.name = KBUILD_MODNAME,
25161da177e4SLinus Torvalds 	.id_table = snd_rme96_ids,
25171da177e4SLinus Torvalds 	.probe = snd_rme96_probe,
2518e23e7a14SBill Pemberton 	.remove = snd_rme96_remove,
2519a932be91STakashi Iwai 	.driver = {
2520a932be91STakashi Iwai 		.pm = RME96_PM_OPS,
2521a932be91STakashi Iwai 	},
25221da177e4SLinus Torvalds };
25231da177e4SLinus Torvalds 
2524e9f66d9bSTakashi Iwai module_pci_driver(rme96_driver);
2525