xref: /openbmc/linux/sound/pci/rme96.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *   ALSA driver for RME Digi96, Digi96/8 and Digi96/8 PRO/PAD/PST audio
41da177e4SLinus Torvalds  *   interfaces
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Copyright (c) 2000, 2001 Anders Torger <torger@ludd.luth.se>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *      Thanks to Henk Hesselink <henk@anda.nl> for the analog volume control
91da177e4SLinus Torvalds  *      code.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/delay.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/interrupt.h>
151da177e4SLinus Torvalds #include <linux/pci.h>
1665a77217SPaul Gortmaker #include <linux/module.h>
17a8cc2099SKnut Petersen #include <linux/vmalloc.h>
186cbbfe1cSTakashi Iwai #include <linux/io.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <sound/core.h>
211da177e4SLinus Torvalds #include <sound/info.h>
221da177e4SLinus Torvalds #include <sound/control.h>
231da177e4SLinus Torvalds #include <sound/pcm.h>
241da177e4SLinus Torvalds #include <sound/pcm_params.h>
251da177e4SLinus Torvalds #include <sound/asoundef.h>
261da177e4SLinus Torvalds #include <sound/initval.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* note, two last pcis should be equal, it is not a bug */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds MODULE_AUTHOR("Anders Torger <torger@ludd.luth.se>");
311da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Digi96, Digi96/8, Digi96/8 PRO, Digi96/8 PST, "
321da177e4SLinus Torvalds 		   "Digi96/8 PAD");
331da177e4SLinus Torvalds MODULE_LICENSE("GPL");
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
361da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
37a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
401da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Digi96 soundcard.");
411da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
421da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Digi96 soundcard.");
431da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
441da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable RME Digi96 soundcard.");
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * Defines for RME Digi96 series, from internal RME reference documents
481da177e4SLinus Torvalds  * dated 12.01.00
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define RME96_SPDIF_NCHANNELS 2
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* Playback and capture buffer size */
541da177e4SLinus Torvalds #define RME96_BUFFER_SIZE 0x10000
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* IO area size */
571da177e4SLinus Torvalds #define RME96_IO_SIZE 0x60000
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* IO area offsets */
601da177e4SLinus Torvalds #define RME96_IO_PLAY_BUFFER      0x0
611da177e4SLinus Torvalds #define RME96_IO_REC_BUFFER       0x10000
621da177e4SLinus Torvalds #define RME96_IO_CONTROL_REGISTER 0x20000
631da177e4SLinus Torvalds #define RME96_IO_ADDITIONAL_REG   0x20004
641da177e4SLinus Torvalds #define RME96_IO_CONFIRM_PLAY_IRQ 0x20008
651da177e4SLinus Torvalds #define RME96_IO_CONFIRM_REC_IRQ  0x2000C
661da177e4SLinus Torvalds #define RME96_IO_SET_PLAY_POS     0x40000
671da177e4SLinus Torvalds #define RME96_IO_RESET_PLAY_POS   0x4FFFC
681da177e4SLinus Torvalds #define RME96_IO_SET_REC_POS      0x50000
691da177e4SLinus Torvalds #define RME96_IO_RESET_REC_POS    0x5FFFC
701da177e4SLinus Torvalds #define RME96_IO_GET_PLAY_POS     0x20000
711da177e4SLinus Torvalds #define RME96_IO_GET_REC_POS      0x30000
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Write control register bits */
741da177e4SLinus Torvalds #define RME96_WCR_START     (1 << 0)
751da177e4SLinus Torvalds #define RME96_WCR_START_2   (1 << 1)
761da177e4SLinus Torvalds #define RME96_WCR_GAIN_0    (1 << 2)
771da177e4SLinus Torvalds #define RME96_WCR_GAIN_1    (1 << 3)
781da177e4SLinus Torvalds #define RME96_WCR_MODE24    (1 << 4)
791da177e4SLinus Torvalds #define RME96_WCR_MODE24_2  (1 << 5)
801da177e4SLinus Torvalds #define RME96_WCR_BM        (1 << 6)
811da177e4SLinus Torvalds #define RME96_WCR_BM_2      (1 << 7)
821da177e4SLinus Torvalds #define RME96_WCR_ADAT      (1 << 8)
831da177e4SLinus Torvalds #define RME96_WCR_FREQ_0    (1 << 9)
841da177e4SLinus Torvalds #define RME96_WCR_FREQ_1    (1 << 10)
851da177e4SLinus Torvalds #define RME96_WCR_DS        (1 << 11)
861da177e4SLinus Torvalds #define RME96_WCR_PRO       (1 << 12)
871da177e4SLinus Torvalds #define RME96_WCR_EMP       (1 << 13)
881da177e4SLinus Torvalds #define RME96_WCR_SEL       (1 << 14)
891da177e4SLinus Torvalds #define RME96_WCR_MASTER    (1 << 15)
901da177e4SLinus Torvalds #define RME96_WCR_PD        (1 << 16)
911da177e4SLinus Torvalds #define RME96_WCR_INP_0     (1 << 17)
921da177e4SLinus Torvalds #define RME96_WCR_INP_1     (1 << 18)
931da177e4SLinus Torvalds #define RME96_WCR_THRU_0    (1 << 19)
941da177e4SLinus Torvalds #define RME96_WCR_THRU_1    (1 << 20)
951da177e4SLinus Torvalds #define RME96_WCR_THRU_2    (1 << 21)
961da177e4SLinus Torvalds #define RME96_WCR_THRU_3    (1 << 22)
971da177e4SLinus Torvalds #define RME96_WCR_THRU_4    (1 << 23)
981da177e4SLinus Torvalds #define RME96_WCR_THRU_5    (1 << 24)
991da177e4SLinus Torvalds #define RME96_WCR_THRU_6    (1 << 25)
1001da177e4SLinus Torvalds #define RME96_WCR_THRU_7    (1 << 26)
1011da177e4SLinus Torvalds #define RME96_WCR_DOLBY     (1 << 27)
1021da177e4SLinus Torvalds #define RME96_WCR_MONITOR_0 (1 << 28)
1031da177e4SLinus Torvalds #define RME96_WCR_MONITOR_1 (1 << 29)
1041da177e4SLinus Torvalds #define RME96_WCR_ISEL      (1 << 30)
1051da177e4SLinus Torvalds #define RME96_WCR_IDIS      (1 << 31)
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_0 2
1081da177e4SLinus Torvalds #define RME96_WCR_BITPOS_GAIN_1 3
1091da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_0 9
1101da177e4SLinus Torvalds #define RME96_WCR_BITPOS_FREQ_1 10
1111da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_0 17
1121da177e4SLinus Torvalds #define RME96_WCR_BITPOS_INP_1 18
1131da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_0 28
1141da177e4SLinus Torvalds #define RME96_WCR_BITPOS_MONITOR_1 29
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /* Read control register bits */
1171da177e4SLinus Torvalds #define RME96_RCR_AUDIO_ADDR_MASK 0xFFFF
1181da177e4SLinus Torvalds #define RME96_RCR_IRQ_2     (1 << 16)
1191da177e4SLinus Torvalds #define RME96_RCR_T_OUT     (1 << 17)
1201da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_0  (1 << 21)
1211da177e4SLinus Torvalds #define RME96_RCR_DEV_ID_1  (1 << 22)
1221da177e4SLinus Torvalds #define RME96_RCR_LOCK      (1 << 23)
1231da177e4SLinus Torvalds #define RME96_RCR_VERF      (1 << 26)
1241da177e4SLinus Torvalds #define RME96_RCR_F0        (1 << 27)
1251da177e4SLinus Torvalds #define RME96_RCR_F1        (1 << 28)
1261da177e4SLinus Torvalds #define RME96_RCR_F2        (1 << 29)
1271da177e4SLinus Torvalds #define RME96_RCR_AUTOSYNC  (1 << 30)
1281da177e4SLinus Torvalds #define RME96_RCR_IRQ       (1 << 31)
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F0 27
1311da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F1 28
1321da177e4SLinus Torvalds #define RME96_RCR_BITPOS_F2 29
1331da177e4SLinus Torvalds 
13425985edcSLucas De Marchi /* Additional register bits */
1351da177e4SLinus Torvalds #define RME96_AR_WSEL       (1 << 0)
1361da177e4SLinus Torvalds #define RME96_AR_ANALOG     (1 << 1)
1371da177e4SLinus Torvalds #define RME96_AR_FREQPAD_0  (1 << 2)
1381da177e4SLinus Torvalds #define RME96_AR_FREQPAD_1  (1 << 3)
1391da177e4SLinus Torvalds #define RME96_AR_FREQPAD_2  (1 << 4)
1401da177e4SLinus Torvalds #define RME96_AR_PD2        (1 << 5)
1411da177e4SLinus Torvalds #define RME96_AR_DAC_EN     (1 << 6)
1421da177e4SLinus Torvalds #define RME96_AR_CLATCH     (1 << 7)
1431da177e4SLinus Torvalds #define RME96_AR_CCLK       (1 << 8)
1441da177e4SLinus Torvalds #define RME96_AR_CDATA      (1 << 9)
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds #define RME96_AR_BITPOS_F0 2
1471da177e4SLinus Torvalds #define RME96_AR_BITPOS_F1 3
1481da177e4SLinus Torvalds #define RME96_AR_BITPOS_F2 4
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* Monitor tracks */
1511da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_1_2 0
1521da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_3_4 1
1531da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_5_6 2
1541da177e4SLinus Torvalds #define RME96_MONITOR_TRACKS_7_8 3
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /* Attenuation */
1571da177e4SLinus Torvalds #define RME96_ATTENUATION_0 0
1581da177e4SLinus Torvalds #define RME96_ATTENUATION_6 1
1591da177e4SLinus Torvalds #define RME96_ATTENUATION_12 2
1601da177e4SLinus Torvalds #define RME96_ATTENUATION_18 3
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Input types */
1631da177e4SLinus Torvalds #define RME96_INPUT_OPTICAL 0
1641da177e4SLinus Torvalds #define RME96_INPUT_COAXIAL 1
1651da177e4SLinus Torvalds #define RME96_INPUT_INTERNAL 2
1661da177e4SLinus Torvalds #define RME96_INPUT_XLR 3
1671da177e4SLinus Torvalds #define RME96_INPUT_ANALOG 4
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /* Clock modes */
1701da177e4SLinus Torvalds #define RME96_CLOCKMODE_SLAVE 0
1711da177e4SLinus Torvalds #define RME96_CLOCKMODE_MASTER 1
1721da177e4SLinus Torvalds #define RME96_CLOCKMODE_WORDCLOCK 2
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /* Block sizes in bytes */
1751da177e4SLinus Torvalds #define RME96_SMALL_BLOCK_SIZE 2048
1761da177e4SLinus Torvalds #define RME96_LARGE_BLOCK_SIZE 8192
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Volume control */
1791da177e4SLinus Torvalds #define RME96_AD1852_VOL_BITS 14
1801da177e4SLinus Torvalds #define RME96_AD1855_VOL_BITS 10
1811da177e4SLinus Torvalds 
182b892ca1cSKnut Petersen /* Defines for snd_rme96_trigger */
183b892ca1cSKnut Petersen #define RME96_TB_START_PLAYBACK 1
184b892ca1cSKnut Petersen #define RME96_TB_START_CAPTURE 2
185b892ca1cSKnut Petersen #define RME96_TB_STOP_PLAYBACK 4
186b892ca1cSKnut Petersen #define RME96_TB_STOP_CAPTURE 8
187b892ca1cSKnut Petersen #define RME96_TB_RESET_PLAYPOS 16
188b892ca1cSKnut Petersen #define RME96_TB_RESET_CAPTUREPOS 32
189b892ca1cSKnut Petersen #define RME96_TB_CLEAR_PLAYBACK_IRQ 64
190b892ca1cSKnut Petersen #define RME96_TB_CLEAR_CAPTURE_IRQ 128
191b892ca1cSKnut Petersen #define RME96_RESUME_PLAYBACK	(RME96_TB_START_PLAYBACK)
192b892ca1cSKnut Petersen #define RME96_RESUME_CAPTURE	(RME96_TB_START_CAPTURE)
193b892ca1cSKnut Petersen #define RME96_RESUME_BOTH	(RME96_RESUME_PLAYBACK \
194b892ca1cSKnut Petersen 				| RME96_RESUME_CAPTURE)
195b892ca1cSKnut Petersen #define RME96_START_PLAYBACK	(RME96_TB_START_PLAYBACK \
196b892ca1cSKnut Petersen 				| RME96_TB_RESET_PLAYPOS)
197b892ca1cSKnut Petersen #define RME96_START_CAPTURE	(RME96_TB_START_CAPTURE \
198b892ca1cSKnut Petersen 				| RME96_TB_RESET_CAPTUREPOS)
199b892ca1cSKnut Petersen #define RME96_START_BOTH	(RME96_START_PLAYBACK \
200b892ca1cSKnut Petersen 				| RME96_START_CAPTURE)
201b892ca1cSKnut Petersen #define RME96_STOP_PLAYBACK	(RME96_TB_STOP_PLAYBACK \
202b892ca1cSKnut Petersen 				| RME96_TB_CLEAR_PLAYBACK_IRQ)
203b892ca1cSKnut Petersen #define RME96_STOP_CAPTURE	(RME96_TB_STOP_CAPTURE \
204b892ca1cSKnut Petersen 				| RME96_TB_CLEAR_CAPTURE_IRQ)
205b892ca1cSKnut Petersen #define RME96_STOP_BOTH		(RME96_STOP_PLAYBACK \
206b892ca1cSKnut Petersen 				| RME96_STOP_CAPTURE)
2071da177e4SLinus Torvalds 
208a3aefd88STakashi Iwai struct rme96 {
2091da177e4SLinus Torvalds 	spinlock_t    lock;
2101da177e4SLinus Torvalds 	int irq;
2111da177e4SLinus Torvalds 	unsigned long port;
2121da177e4SLinus Torvalds 	void __iomem *iobase;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	u32 wcreg;    /* cached write control register value */
2151da177e4SLinus Torvalds 	u32 wcreg_spdif;		/* S/PDIF setup */
2161da177e4SLinus Torvalds 	u32 wcreg_spdif_stream;		/* S/PDIF setup (temporary) */
2171da177e4SLinus Torvalds 	u32 rcreg;    /* cached read control register value */
2181da177e4SLinus Torvalds 	u32 areg;     /* cached additional register value */
2191da177e4SLinus Torvalds 	u16 vol[2]; /* cached volume of analog output */
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	u8 rev; /* card revision number */
2221da177e4SLinus Torvalds 
223a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
224528ba522SKnut Petersen 	u32 playback_pointer;
225528ba522SKnut Petersen 	u32 capture_pointer;
226528ba522SKnut Petersen 	void *playback_suspend_buffer;
227528ba522SKnut Petersen 	void *capture_suspend_buffer;
228528ba522SKnut Petersen #endif
229528ba522SKnut Petersen 
230a3aefd88STakashi Iwai 	struct snd_pcm_substream *playback_substream;
231a3aefd88STakashi Iwai 	struct snd_pcm_substream *capture_substream;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	int playback_frlog; /* log2 of framesize */
2341da177e4SLinus Torvalds 	int capture_frlog;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds         size_t playback_periodsize; /* in bytes, zero if not used */
2371da177e4SLinus Torvalds 	size_t capture_periodsize; /* in bytes, zero if not used */
2381da177e4SLinus Torvalds 
239a3aefd88STakashi Iwai 	struct snd_card *card;
240a3aefd88STakashi Iwai 	struct snd_pcm *spdif_pcm;
241a3aefd88STakashi Iwai 	struct snd_pcm *adat_pcm;
2421da177e4SLinus Torvalds 	struct pci_dev     *pci;
243a3aefd88STakashi Iwai 	struct snd_kcontrol   *spdif_ctl;
244a3aefd88STakashi Iwai };
2451da177e4SLinus Torvalds 
2469baa3c34SBenoit Taine static const struct pci_device_id snd_rme96_ids[] = {
24728d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96), 0, },
24828d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8), 0, },
24928d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PRO), 0, },
25028d27aaeSJoe Perches 	{ PCI_VDEVICE(XILINX, PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST), 0, },
2511da177e4SLinus Torvalds 	{ 0, }
2521da177e4SLinus Torvalds };
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_rme96_ids);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds #define RME96_ISPLAYING(rme96) ((rme96)->wcreg & RME96_WCR_START)
2571da177e4SLinus Torvalds #define RME96_ISRECORDING(rme96) ((rme96)->wcreg & RME96_WCR_START_2)
2588b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_IN(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2598b7fc421SRoland Dreier #define	RME96_HAS_ANALOG_OUT(rme96) ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO || \
2608b7fc421SRoland Dreier 				     (rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST)
2611da177e4SLinus Torvalds #define	RME96_DAC_IS_1852(rme96) (RME96_HAS_ANALOG_OUT(rme96) && (rme96)->rev >= 4)
2628b7fc421SRoland Dreier #define	RME96_DAC_IS_1855(rme96) (((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && (rme96)->rev < 4) || \
2638b7fc421SRoland Dreier 			          ((rme96)->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PRO && (rme96)->rev == 2))
2641da177e4SLinus Torvalds #define	RME96_185X_MAX_OUT(rme96) ((1 << (RME96_DAC_IS_1852(rme96) ? RME96_AD1852_VOL_BITS : RME96_AD1855_VOL_BITS)) - 1)
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static int
267a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static int
270a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int
273a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
2741da177e4SLinus Torvalds 			   int cmd);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds static int
277a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
2781da177e4SLinus Torvalds 			  int cmd);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static snd_pcm_uframes_t
281a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static snd_pcm_uframes_t
284a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream);
2851da177e4SLinus Torvalds 
286e23e7a14SBill Pemberton static void snd_rme96_proc_init(struct rme96 *rme96);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static int
289a3aefd88STakashi Iwai snd_rme96_create_switches(struct snd_card *card,
290a3aefd88STakashi Iwai 			  struct rme96 *rme96);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static int
293a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static inline unsigned int
snd_rme96_playback_ptr(struct rme96 * rme96)296a3aefd88STakashi Iwai snd_rme96_playback_ptr(struct rme96 *rme96)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
2991da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->playback_frlog;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static inline unsigned int
snd_rme96_capture_ptr(struct rme96 * rme96)303a3aefd88STakashi Iwai snd_rme96_capture_ptr(struct rme96 *rme96)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	return (readl(rme96->iobase + RME96_IO_GET_REC_POS)
3061da177e4SLinus Torvalds 		& RME96_RCR_AUDIO_ADDR_MASK) >> rme96->capture_frlog;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static int
snd_rme96_playback_silence(struct snd_pcm_substream * substream,int channel,unsigned long pos,unsigned long count)310a3aefd88STakashi Iwai snd_rme96_playback_silence(struct snd_pcm_substream *substream,
31153945f5bSTakashi Iwai 			   int channel, unsigned long pos, unsigned long count)
3121da177e4SLinus Torvalds {
313a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
31453945f5bSTakashi Iwai 
3151da177e4SLinus Torvalds 	memset_io(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
3161da177e4SLinus Torvalds 		  0, count);
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static int
snd_rme96_playback_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * src,unsigned long count)321a3aefd88STakashi Iwai snd_rme96_playback_copy(struct snd_pcm_substream *substream,
32253945f5bSTakashi Iwai 			int channel, unsigned long pos,
323*c3abdf06STakashi Iwai 			struct iov_iter *src, unsigned long count)
3241da177e4SLinus Torvalds {
325a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
32653945f5bSTakashi Iwai 
327*c3abdf06STakashi Iwai 	return copy_from_iter_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
32853945f5bSTakashi Iwai 				   src, count);
32953945f5bSTakashi Iwai }
33053945f5bSTakashi Iwai 
33153945f5bSTakashi Iwai static int
snd_rme96_capture_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * dst,unsigned long count)332a3aefd88STakashi Iwai snd_rme96_capture_copy(struct snd_pcm_substream *substream,
33353945f5bSTakashi Iwai 		       int channel, unsigned long pos,
334*c3abdf06STakashi Iwai 		       struct iov_iter *dst, unsigned long count)
3351da177e4SLinus Torvalds {
336a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
33753945f5bSTakashi Iwai 
338*c3abdf06STakashi Iwai 	return copy_to_iter_fromio(dst,
33953945f5bSTakashi Iwai 				   rme96->iobase + RME96_IO_REC_BUFFER + pos,
3401da177e4SLinus Torvalds 				   count);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds /*
3447f927fccSAlexey Dobriyan  * Digital output capabilities (S/PDIF)
3451da177e4SLinus Torvalds  */
346dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_rme96_playback_spdif_info =
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
3491da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
350b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
351528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
3521da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
3531da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
3541da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
3551da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
3561da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
3571da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
3581da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
3591da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
3601da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
3611da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
3621da177e4SLinus Torvalds 	.rate_min =	     32000,
3631da177e4SLinus Torvalds 	.rate_max =	     96000,
3641da177e4SLinus Torvalds 	.channels_min =	     2,
3651da177e4SLinus Torvalds 	.channels_max =	     2,
3661da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
3671da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
3681da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
3691da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
3701da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
3711da177e4SLinus Torvalds 	.fifo_size =	     0,
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3757f927fccSAlexey Dobriyan  * Digital input capabilities (S/PDIF)
3761da177e4SLinus Torvalds  */
377dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_rme96_capture_spdif_info =
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
3801da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
381b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
382528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
3831da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
3841da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
3851da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
3861da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
3871da177e4SLinus Torvalds 	.rates =	     (SNDRV_PCM_RATE_32000 |
3881da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_44100 |
3891da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000 |
3901da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_64000 |
3911da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_88200 |
3921da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_96000),
3931da177e4SLinus Torvalds 	.rate_min =	     32000,
3941da177e4SLinus Torvalds 	.rate_max =	     96000,
3951da177e4SLinus Torvalds 	.channels_min =	     2,
3961da177e4SLinus Torvalds 	.channels_max =	     2,
3971da177e4SLinus Torvalds 	.buffer_bytes_max =  RME96_BUFFER_SIZE,
3981da177e4SLinus Torvalds 	.period_bytes_min =  RME96_SMALL_BLOCK_SIZE,
3991da177e4SLinus Torvalds 	.period_bytes_max =  RME96_LARGE_BLOCK_SIZE,
4001da177e4SLinus Torvalds 	.periods_min =	     RME96_BUFFER_SIZE / RME96_LARGE_BLOCK_SIZE,
4011da177e4SLinus Torvalds 	.periods_max =	     RME96_BUFFER_SIZE / RME96_SMALL_BLOCK_SIZE,
4021da177e4SLinus Torvalds 	.fifo_size =	     0,
4031da177e4SLinus Torvalds };
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4067f927fccSAlexey Dobriyan  * Digital output capabilities (ADAT)
4071da177e4SLinus Torvalds  */
408dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_rme96_playback_adat_info =
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	.info =		     (SNDRV_PCM_INFO_MMAP_IOMEM |
4111da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_MMAP_VALID |
412b892ca1cSKnut Petersen 			      SNDRV_PCM_INFO_SYNC_START |
413528ba522SKnut Petersen 			      SNDRV_PCM_INFO_RESUME |
4141da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_INTERLEAVED |
4151da177e4SLinus Torvalds 			      SNDRV_PCM_INFO_PAUSE),
4161da177e4SLinus Torvalds 	.formats =	     (SNDRV_PCM_FMTBIT_S16_LE |
4171da177e4SLinus Torvalds 			      SNDRV_PCM_FMTBIT_S32_LE),
4181da177e4SLinus Torvalds 	.rates =             (SNDRV_PCM_RATE_44100 |
4191da177e4SLinus Torvalds 			      SNDRV_PCM_RATE_48000),
4201da177e4SLinus Torvalds 	.rate_min =          44100,
4211da177e4SLinus Torvalds 	.rate_max =          48000,
4221da177e4SLinus Torvalds 	.channels_min =      8,
4231da177e4SLinus Torvalds 	.channels_max =	     8,
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 input capabilities (ADAT)
4341da177e4SLinus Torvalds  */
435dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_rme96_capture_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 /*
4601da177e4SLinus Torvalds  * The CDATA, CCLK and CLATCH bits can be used to write to the SPI interface
4611da177e4SLinus Torvalds  * of the AD1852 or AD1852 D/A converter on the board.  CDATA must be set up
4621da177e4SLinus Torvalds  * on the falling edge of CCLK and be stable on the rising edge.  The rising
4631da177e4SLinus Torvalds  * edge of CLATCH after the last data bit clocks in the whole data word.
4641da177e4SLinus Torvalds  * A fast processor could probably drive the SPI interface faster than the
4651da177e4SLinus Torvalds  * DAC can handle (3MHz for the 1855, unknown for the 1852).  The udelay(1)
4661da177e4SLinus Torvalds  * limits the data rate to 500KHz and only causes a delay of 33 microsecs.
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  * NOTE: increased delay from 1 to 10, since there where problems setting
4691da177e4SLinus Torvalds  * the volume.
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds static void
snd_rme96_write_SPI(struct rme96 * rme96,u16 val)472a3aefd88STakashi Iwai snd_rme96_write_SPI(struct rme96 *rme96, u16 val)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	int i;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	for (i = 0; i < 16; i++) {
4771da177e4SLinus Torvalds 		if (val & 0x8000) {
4781da177e4SLinus Torvalds 			rme96->areg |= RME96_AR_CDATA;
4791da177e4SLinus Torvalds 		} else {
4801da177e4SLinus Torvalds 			rme96->areg &= ~RME96_AR_CDATA;
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 		rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CLATCH);
4831da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4841da177e4SLinus Torvalds 		udelay(10);
4851da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_CCLK;
4861da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4871da177e4SLinus Torvalds 		udelay(10);
4881da177e4SLinus Torvalds 		val <<= 1;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 	rme96->areg &= ~(RME96_AR_CCLK | RME96_AR_CDATA);
4911da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_CLATCH;
4921da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4931da177e4SLinus Torvalds 	udelay(10);
4941da177e4SLinus Torvalds 	rme96->areg &= ~RME96_AR_CLATCH;
4951da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds static void
snd_rme96_apply_dac_volume(struct rme96 * rme96)499a3aefd88STakashi Iwai snd_rme96_apply_dac_volume(struct rme96 *rme96)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	if (RME96_DAC_IS_1852(rme96)) {
5021da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] << 2) | 0x0);
5031da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] << 2) | 0x2);
5041da177e4SLinus Torvalds 	} else if (RME96_DAC_IS_1855(rme96)) {
5051da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[0] & 0x3FF) | 0x000);
5061da177e4SLinus Torvalds 		snd_rme96_write_SPI(rme96, (rme96->vol[1] & 0x3FF) | 0x400);
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static void
snd_rme96_reset_dac(struct rme96 * rme96)511a3aefd88STakashi Iwai snd_rme96_reset_dac(struct rme96 *rme96)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	writel(rme96->wcreg | RME96_WCR_PD,
5141da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_CONTROL_REGISTER);
5151da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static int
snd_rme96_getmontracks(struct rme96 * rme96)519a3aefd88STakashi Iwai snd_rme96_getmontracks(struct rme96 *rme96)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_0) & 1) +
5221da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_MONITOR_1) & 1) << 1);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds static int
snd_rme96_setmontracks(struct rme96 * rme96,int montracks)526a3aefd88STakashi Iwai snd_rme96_setmontracks(struct rme96 *rme96,
5271da177e4SLinus Torvalds 		       int montracks)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	if (montracks & 1) {
5301da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_0;
5311da177e4SLinus Torvalds 	} else {
5321da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_0;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	if (montracks & 2) {
5351da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MONITOR_1;
5361da177e4SLinus Torvalds 	} else {
5371da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MONITOR_1;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5401da177e4SLinus Torvalds 	return 0;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds static int
snd_rme96_getattenuation(struct rme96 * rme96)544a3aefd88STakashi Iwai snd_rme96_getattenuation(struct rme96 *rme96)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_0) & 1) +
5471da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_GAIN_1) & 1) << 1);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static int
snd_rme96_setattenuation(struct rme96 * rme96,int attenuation)551a3aefd88STakashi Iwai snd_rme96_setattenuation(struct rme96 *rme96,
5521da177e4SLinus Torvalds 			 int attenuation)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	switch (attenuation) {
5551da177e4SLinus Torvalds 	case 0:
5561da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) &
5571da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5581da177e4SLinus Torvalds 		break;
5591da177e4SLinus Torvalds 	case 1:
5601da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) &
5611da177e4SLinus Torvalds 			~RME96_WCR_GAIN_1;
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 	case 2:
5641da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_GAIN_0) |
5651da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5661da177e4SLinus Torvalds 		break;
5671da177e4SLinus Torvalds 	case 3:
5681da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_GAIN_0) |
5691da177e4SLinus Torvalds 			RME96_WCR_GAIN_1;
5701da177e4SLinus Torvalds 		break;
5711da177e4SLinus Torvalds 	default:
5721da177e4SLinus Torvalds 		return -EINVAL;
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static int
snd_rme96_capture_getrate(struct rme96 * rme96,int * is_adat)579a3aefd88STakashi Iwai snd_rme96_capture_getrate(struct rme96 *rme96,
5801da177e4SLinus Torvalds 			  int *is_adat)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	int n, rate;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	*is_adat = 0;
5851da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
5861da177e4SLinus Torvalds 		/* Analog input, overrides S/PDIF setting */
5871da177e4SLinus Torvalds 		n = ((rme96->areg >> RME96_AR_BITPOS_F0) & 1) +
5881da177e4SLinus Torvalds 			(((rme96->areg >> RME96_AR_BITPOS_F1) & 1) << 1);
5891da177e4SLinus Torvalds 		switch (n) {
5901da177e4SLinus Torvalds 		case 1:
5911da177e4SLinus Torvalds 			rate = 32000;
5921da177e4SLinus Torvalds 			break;
5931da177e4SLinus Torvalds 		case 2:
5941da177e4SLinus Torvalds 			rate = 44100;
5951da177e4SLinus Torvalds 			break;
5961da177e4SLinus Torvalds 		case 3:
5971da177e4SLinus Torvalds 			rate = 48000;
5981da177e4SLinus Torvalds 			break;
5991da177e4SLinus Torvalds 		default:
6001da177e4SLinus Torvalds 			return -1;
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 		return (rme96->areg & RME96_AR_BITPOS_F2) ? rate << 1 : rate;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
6061da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_LOCK) {
6071da177e4SLinus Torvalds 		/* ADAT rate */
6081da177e4SLinus Torvalds 		*is_adat = 1;
6091da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6101da177e4SLinus Torvalds 			return 48000;
6111da177e4SLinus Torvalds 		}
6121da177e4SLinus Torvalds 		return 44100;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_VERF) {
6161da177e4SLinus Torvalds 		return -1;
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/* S/PDIF rate */
6201da177e4SLinus Torvalds 	n = ((rme96->rcreg >> RME96_RCR_BITPOS_F0) & 1) +
6211da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F1) & 1) << 1) +
6221da177e4SLinus Torvalds 		(((rme96->rcreg >> RME96_RCR_BITPOS_F2) & 1) << 2);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	switch (n) {
6251da177e4SLinus Torvalds 	case 0:
6261da177e4SLinus Torvalds 		if (rme96->rcreg & RME96_RCR_T_OUT) {
6271da177e4SLinus Torvalds 			return 64000;
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 		return -1;
6301da177e4SLinus Torvalds 	case 3: return 96000;
6311da177e4SLinus Torvalds 	case 4: return 88200;
6321da177e4SLinus Torvalds 	case 5: return 48000;
6331da177e4SLinus Torvalds 	case 6: return 44100;
6341da177e4SLinus Torvalds 	case 7: return 32000;
6351da177e4SLinus Torvalds 	default:
6361da177e4SLinus Torvalds 		break;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 	return -1;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds static int
snd_rme96_playback_getrate(struct rme96 * rme96)642a3aefd88STakashi Iwai snd_rme96_playback_getrate(struct rme96 *rme96)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	int rate, dummy;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
647da8eedb1STakashi Iwai 	    snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG) {
648da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &dummy);
649da8eedb1STakashi Iwai 		if (rate > 0) {
6501da177e4SLinus Torvalds 			/* slave clock */
6511da177e4SLinus Torvalds 			return rate;
6521da177e4SLinus Torvalds 		}
653da8eedb1STakashi Iwai 	}
654da8eedb1STakashi Iwai 
6551da177e4SLinus Torvalds 	rate = ((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_0) & 1) +
6561da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_FREQ_1) & 1) << 1);
6571da177e4SLinus Torvalds 	switch (rate) {
6581da177e4SLinus Torvalds 	case 1:
6591da177e4SLinus Torvalds 		rate = 32000;
6601da177e4SLinus Torvalds 		break;
6611da177e4SLinus Torvalds 	case 2:
6621da177e4SLinus Torvalds 		rate = 44100;
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	case 3:
6651da177e4SLinus Torvalds 		rate = 48000;
6661da177e4SLinus Torvalds 		break;
6671da177e4SLinus Torvalds 	default:
6681da177e4SLinus Torvalds 		return -1;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_DS) ? rate << 1 : rate;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds static int
snd_rme96_playback_setrate(struct rme96 * rme96,int rate)674a3aefd88STakashi Iwai snd_rme96_playback_setrate(struct rme96 *rme96,
6751da177e4SLinus Torvalds 			   int rate)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	int ds;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	ds = rme96->wcreg & RME96_WCR_DS;
6801da177e4SLinus Torvalds 	switch (rate) {
6811da177e4SLinus Torvalds 	case 32000:
6821da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
6831da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
6841da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
6851da177e4SLinus Torvalds 		break;
6861da177e4SLinus Torvalds 	case 44100:
6871da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
6881da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
6891da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
6901da177e4SLinus Torvalds 		break;
6911da177e4SLinus Torvalds 	case 48000:
6921da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_DS;
6931da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) |
6941da177e4SLinus Torvalds 			RME96_WCR_FREQ_1;
6951da177e4SLinus Torvalds 		break;
6961da177e4SLinus Torvalds 	case 64000:
6971da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
6981da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_0) &
6991da177e4SLinus Torvalds 			~RME96_WCR_FREQ_1;
7001da177e4SLinus Torvalds 		break;
7011da177e4SLinus Torvalds 	case 88200:
7021da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_DS;
7031da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_FREQ_1) &
7041da177e4SLinus Torvalds 			~RME96_WCR_FREQ_0;
7051da177e4SLinus Torvalds 		break;
7061da177e4SLinus Torvalds 	case 96000:
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 	default:
7121da177e4SLinus Torvalds 		return -EINVAL;
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	if ((!ds && rme96->wcreg & RME96_WCR_DS) ||
7151da177e4SLinus Torvalds 	    (ds && !(rme96->wcreg & RME96_WCR_DS)))
7161da177e4SLinus Torvalds 	{
7171da177e4SLinus Torvalds 		/* change to/from double-speed: reset the DAC (if available) */
7181da177e4SLinus Torvalds 		snd_rme96_reset_dac(rme96);
719a74a8216STakashi Iwai 		return 1; /* need to restore volume */
7201da177e4SLinus Torvalds 	} else {
7211da177e4SLinus Torvalds 		writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
7221da177e4SLinus Torvalds 		return 0;
7231da177e4SLinus Torvalds 	}
724a74a8216STakashi Iwai }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds static int
snd_rme96_capture_analog_setrate(struct rme96 * rme96,int rate)727a3aefd88STakashi Iwai snd_rme96_capture_analog_setrate(struct rme96 *rme96,
7281da177e4SLinus Torvalds 				 int rate)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	switch (rate) {
7311da177e4SLinus Torvalds 	case 32000:
7321da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7331da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7341da177e4SLinus Torvalds 		break;
7351da177e4SLinus Torvalds 	case 44100:
7361da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7371da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7381da177e4SLinus Torvalds 		break;
7391da177e4SLinus Torvalds 	case 48000:
7401da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7411da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) & ~RME96_AR_FREQPAD_2;
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 	case 64000:
7441da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7451da177e4SLinus Torvalds 			return -EINVAL;
7461da177e4SLinus Torvalds 		}
7471da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) &
7481da177e4SLinus Torvalds 			       ~RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7491da177e4SLinus Torvalds 		break;
7501da177e4SLinus Torvalds 	case 88200:
7511da177e4SLinus Torvalds 		if (rme96->rev < 4) {
7521da177e4SLinus Torvalds 			return -EINVAL;
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg & ~RME96_AR_FREQPAD_0) |
7551da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7561da177e4SLinus Torvalds 		break;
7571da177e4SLinus Torvalds 	case 96000:
7581da177e4SLinus Torvalds 		rme96->areg = ((rme96->areg | RME96_AR_FREQPAD_0) |
7591da177e4SLinus Torvalds 			       RME96_AR_FREQPAD_1) | RME96_AR_FREQPAD_2;
7601da177e4SLinus Torvalds 		break;
7611da177e4SLinus Torvalds 	default:
7621da177e4SLinus Torvalds 		return -EINVAL;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
7651da177e4SLinus Torvalds 	return 0;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds static int
snd_rme96_setclockmode(struct rme96 * rme96,int mode)769a3aefd88STakashi Iwai snd_rme96_setclockmode(struct rme96 *rme96,
7701da177e4SLinus Torvalds 		       int mode)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	switch (mode) {
7731da177e4SLinus Torvalds 	case RME96_CLOCKMODE_SLAVE:
7741da177e4SLinus Torvalds 	        /* AutoSync */
7751da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MASTER;
7761da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
7771da177e4SLinus Torvalds 		break;
7781da177e4SLinus Torvalds 	case RME96_CLOCKMODE_MASTER:
7791da177e4SLinus Torvalds 	        /* Internal */
7801da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
7811da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_WSEL;
7821da177e4SLinus Torvalds 		break;
7831da177e4SLinus Torvalds 	case RME96_CLOCKMODE_WORDCLOCK:
7841da177e4SLinus Torvalds 		/* Word clock is a master mode */
7851da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MASTER;
7861da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_WSEL;
7871da177e4SLinus Torvalds 		break;
7881da177e4SLinus Torvalds 	default:
7891da177e4SLinus Torvalds 		return -EINVAL;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
7921da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
7931da177e4SLinus Torvalds 	return 0;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static int
snd_rme96_getclockmode(struct rme96 * rme96)797a3aefd88STakashi Iwai snd_rme96_getclockmode(struct rme96 *rme96)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
8001da177e4SLinus Torvalds 		return RME96_CLOCKMODE_WORDCLOCK;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 	return (rme96->wcreg & RME96_WCR_MASTER) ? RME96_CLOCKMODE_MASTER :
8031da177e4SLinus Torvalds 		RME96_CLOCKMODE_SLAVE;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds static int
snd_rme96_setinputtype(struct rme96 * rme96,int type)807a3aefd88STakashi Iwai snd_rme96_setinputtype(struct rme96 *rme96,
8081da177e4SLinus Torvalds 		       int type)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	int n;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	switch (type) {
8131da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
8141da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) &
8151da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8161da177e4SLinus Torvalds 		break;
8171da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
8181da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) &
8191da177e4SLinus Torvalds 			~RME96_WCR_INP_1;
8201da177e4SLinus Torvalds 		break;
8211da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
8221da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg & ~RME96_WCR_INP_0) |
8231da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8241da177e4SLinus Torvalds 		break;
8251da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
8268b7fc421SRoland Dreier 		if ((rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8278b7fc421SRoland Dreier 		     rme96->pci->device != PCI_DEVICE_ID_RME_DIGI96_8_PRO) ||
8288b7fc421SRoland Dreier 		    (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST &&
8291da177e4SLinus Torvalds 		     rme96->rev > 4))
8301da177e4SLinus Torvalds 		{
8311da177e4SLinus Torvalds 			/* Only Digi96/8 PRO and Digi96/8 PAD supports XLR */
8321da177e4SLinus Torvalds 			return -EINVAL;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 		rme96->wcreg = (rme96->wcreg | RME96_WCR_INP_0) |
8351da177e4SLinus Torvalds 			RME96_WCR_INP_1;
8361da177e4SLinus Torvalds 		break;
8371da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
8381da177e4SLinus Torvalds 		if (!RME96_HAS_ANALOG_IN(rme96)) {
8391da177e4SLinus Torvalds 			return -EINVAL;
8401da177e4SLinus Torvalds 		}
8411da177e4SLinus Torvalds 		rme96->areg |= RME96_AR_ANALOG;
8421da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8431da177e4SLinus Torvalds 		if (rme96->rev < 4) {
8441da177e4SLinus Torvalds 			/*
8451da177e4SLinus Torvalds 			 * Revision less than 004 does not support 64 and
8461da177e4SLinus Torvalds 			 * 88.2 kHz
8471da177e4SLinus Torvalds 			 */
8481da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 88200) {
8491da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 44100);
8501da177e4SLinus Torvalds 			}
8511da177e4SLinus Torvalds 			if (snd_rme96_capture_getrate(rme96, &n) == 64000) {
8521da177e4SLinus Torvalds 				snd_rme96_capture_analog_setrate(rme96, 32000);
8531da177e4SLinus Torvalds 			}
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 		return 0;
8561da177e4SLinus Torvalds 	default:
8571da177e4SLinus Torvalds 		return -EINVAL;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 	if (type != RME96_INPUT_ANALOG && RME96_HAS_ANALOG_IN(rme96)) {
8601da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_ANALOG;
8611da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
8641da177e4SLinus Torvalds 	return 0;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds static int
snd_rme96_getinputtype(struct rme96 * rme96)868a3aefd88STakashi Iwai snd_rme96_getinputtype(struct rme96 *rme96)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_ANALOG) {
8711da177e4SLinus Torvalds 		return RME96_INPUT_ANALOG;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 	return ((rme96->wcreg >> RME96_WCR_BITPOS_INP_0) & 1) +
8741da177e4SLinus Torvalds 		(((rme96->wcreg >> RME96_WCR_BITPOS_INP_1) & 1) << 1);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds static void
snd_rme96_setframelog(struct rme96 * rme96,int n_channels,int is_playback)878a3aefd88STakashi Iwai snd_rme96_setframelog(struct rme96 *rme96,
8791da177e4SLinus Torvalds 		      int n_channels,
8801da177e4SLinus Torvalds 		      int is_playback)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	int frlog;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (n_channels == 2) {
8851da177e4SLinus Torvalds 		frlog = 1;
8861da177e4SLinus Torvalds 	} else {
8871da177e4SLinus Torvalds 		/* assume 8 channels */
8881da177e4SLinus Torvalds 		frlog = 3;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 	if (is_playback) {
8911da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24) ? 2 : 1;
8921da177e4SLinus Torvalds 		rme96->playback_frlog = frlog;
8931da177e4SLinus Torvalds 	} else {
8941da177e4SLinus Torvalds 		frlog += (rme96->wcreg & RME96_WCR_MODE24_2) ? 2 : 1;
8951da177e4SLinus Torvalds 		rme96->capture_frlog = frlog;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds static int
snd_rme96_playback_setformat(struct rme96 * rme96,snd_pcm_format_t format)9006c869d30STakashi Iwai snd_rme96_playback_setformat(struct rme96 *rme96, snd_pcm_format_t format)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	switch (format) {
9031da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9041da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24;
9051da177e4SLinus Torvalds 		break;
9061da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9071da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24;
9081da177e4SLinus Torvalds 		break;
9091da177e4SLinus Torvalds 	default:
9101da177e4SLinus Torvalds 		return -EINVAL;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9131da177e4SLinus Torvalds 	return 0;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds static int
snd_rme96_capture_setformat(struct rme96 * rme96,snd_pcm_format_t format)9176c869d30STakashi Iwai snd_rme96_capture_setformat(struct rme96 *rme96, snd_pcm_format_t format)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	switch (format) {
9201da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
9211da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_MODE24_2;
9221da177e4SLinus Torvalds 		break;
9231da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S32_LE:
9241da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_MODE24_2;
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	default:
9271da177e4SLinus Torvalds 		return -EINVAL;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9301da177e4SLinus Torvalds 	return 0;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds static void
snd_rme96_set_period_properties(struct rme96 * rme96,size_t period_bytes)934a3aefd88STakashi Iwai snd_rme96_set_period_properties(struct rme96 *rme96,
9351da177e4SLinus Torvalds 				size_t period_bytes)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	switch (period_bytes) {
9381da177e4SLinus Torvalds 	case RME96_LARGE_BLOCK_SIZE:
9391da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_ISEL;
9401da177e4SLinus Torvalds 		break;
9411da177e4SLinus Torvalds 	case RME96_SMALL_BLOCK_SIZE:
9421da177e4SLinus Torvalds 		rme96->wcreg |= RME96_WCR_ISEL;
9431da177e4SLinus Torvalds 		break;
9441da177e4SLinus Torvalds 	default:
9451da177e4SLinus Torvalds 		snd_BUG();
9461da177e4SLinus Torvalds 		break;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_IDIS;
9491da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static int
snd_rme96_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)953a3aefd88STakashi Iwai snd_rme96_playback_hw_params(struct snd_pcm_substream *substream,
954a3aefd88STakashi Iwai 			     struct snd_pcm_hw_params *params)
9551da177e4SLinus Torvalds {
956a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
957a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
9581da177e4SLinus Torvalds 	int err, rate, dummy;
959a74a8216STakashi Iwai 	bool apply_dac_volume = false;
9601da177e4SLinus Torvalds 
9614d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
9624d23359bSClemens Ladisch 					     RME96_IO_PLAY_BUFFER);
9631da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_PLAY_BUFFER;
9641da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
967da8eedb1STakashi Iwai 	rate = 0;
9681da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
969da8eedb1STakashi Iwai 	    snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG)
970da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &dummy);
971da8eedb1STakashi Iwai 	if (rate > 0) {
9721da177e4SLinus Torvalds                 /* slave clock */
9731da177e4SLinus Torvalds                 if ((int)params_rate(params) != rate) {
974a74a8216STakashi Iwai 			err = -EIO;
975a74a8216STakashi Iwai 			goto error;
9761da177e4SLinus Torvalds 		}
977a74a8216STakashi Iwai 	} else {
978a74a8216STakashi Iwai 		err = snd_rme96_playback_setrate(rme96, params_rate(params));
979a74a8216STakashi Iwai 		if (err < 0)
980a74a8216STakashi Iwai 			goto error;
981a74a8216STakashi Iwai 		apply_dac_volume = err > 0; /* need to restore volume later? */
9821da177e4SLinus Torvalds 	}
983a74a8216STakashi Iwai 
984a74a8216STakashi Iwai 	err = snd_rme96_playback_setformat(rme96, params_format(params));
985a74a8216STakashi Iwai 	if (err < 0)
986a74a8216STakashi Iwai 		goto error;
9871da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 1);
9881da177e4SLinus Torvalds 	if (rme96->capture_periodsize != 0) {
9891da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->playback_frlog !=
9901da177e4SLinus Torvalds 		    rme96->capture_periodsize)
9911da177e4SLinus Torvalds 		{
992a74a8216STakashi Iwai 			err = -EBUSY;
993a74a8216STakashi Iwai 			goto error;
9941da177e4SLinus Torvalds 		}
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	rme96->playback_periodsize =
9971da177e4SLinus Torvalds 		params_period_size(params) << rme96->playback_frlog;
9981da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->playback_periodsize);
9991da177e4SLinus Torvalds 	/* S/PDIF setup */
10001da177e4SLinus Torvalds 	if ((rme96->wcreg & RME96_WCR_ADAT) == 0) {
10011da177e4SLinus Torvalds 		rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
10021da177e4SLinus Torvalds 		writel(rme96->wcreg |= rme96->wcreg_spdif_stream, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
1005a74a8216STakashi Iwai 	err = 0;
1006a74a8216STakashi Iwai  error:
1007a74a8216STakashi Iwai 	spin_unlock_irq(&rme96->lock);
1008a74a8216STakashi Iwai 	if (apply_dac_volume) {
1009a74a8216STakashi Iwai 		usleep_range(3000, 10000);
1010a74a8216STakashi Iwai 		snd_rme96_apply_dac_volume(rme96);
1011a74a8216STakashi Iwai 	}
1012a74a8216STakashi Iwai 
1013a74a8216STakashi Iwai 	return err;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds static int
snd_rme96_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1017a3aefd88STakashi Iwai snd_rme96_capture_hw_params(struct snd_pcm_substream *substream,
1018a3aefd88STakashi Iwai 			    struct snd_pcm_hw_params *params)
10191da177e4SLinus Torvalds {
1020a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1021a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10221da177e4SLinus Torvalds 	int err, isadat, rate;
10231da177e4SLinus Torvalds 
10244d23359bSClemens Ladisch 	runtime->dma_area = (void __force *)(rme96->iobase +
10254d23359bSClemens Ladisch 					     RME96_IO_REC_BUFFER);
10261da177e4SLinus Torvalds 	runtime->dma_addr = rme96->port + RME96_IO_REC_BUFFER;
10271da177e4SLinus Torvalds 	runtime->dma_bytes = RME96_BUFFER_SIZE;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
1030da8eedb1STakashi Iwai 	err = snd_rme96_capture_setformat(rme96, params_format(params));
1031da8eedb1STakashi Iwai 	if (err < 0) {
10321da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
10331da177e4SLinus Torvalds 		return err;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 	if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
1036da8eedb1STakashi Iwai 		err = snd_rme96_capture_analog_setrate(rme96, params_rate(params));
1037da8eedb1STakashi Iwai 		if (err < 0) {
10381da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10391da177e4SLinus Torvalds 			return err;
10401da177e4SLinus Torvalds 		}
1041da8eedb1STakashi Iwai 	} else {
1042da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &isadat);
1043da8eedb1STakashi Iwai 		if (rate > 0) {
10441da177e4SLinus Torvalds 			if ((int)params_rate(params) != rate) {
10451da177e4SLinus Torvalds 				spin_unlock_irq(&rme96->lock);
10461da177e4SLinus Torvalds 				return -EIO;
10471da177e4SLinus Torvalds 			}
10481da177e4SLinus Torvalds 			if ((isadat && runtime->hw.channels_min == 2) ||
1049da8eedb1STakashi Iwai 			    (!isadat && runtime->hw.channels_min == 8)) {
10501da177e4SLinus Torvalds 				spin_unlock_irq(&rme96->lock);
10511da177e4SLinus Torvalds 				return -EIO;
10521da177e4SLinus Torvalds 			}
10531da177e4SLinus Torvalds 		}
1054da8eedb1STakashi Iwai         }
10551da177e4SLinus Torvalds 	snd_rme96_setframelog(rme96, params_channels(params), 0);
10561da177e4SLinus Torvalds 	if (rme96->playback_periodsize != 0) {
10571da177e4SLinus Torvalds 		if (params_period_size(params) << rme96->capture_frlog !=
10581da177e4SLinus Torvalds 		    rme96->playback_periodsize)
10591da177e4SLinus Torvalds 		{
10601da177e4SLinus Torvalds 			spin_unlock_irq(&rme96->lock);
10611da177e4SLinus Torvalds 			return -EBUSY;
10621da177e4SLinus Torvalds 		}
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 	rme96->capture_periodsize =
10651da177e4SLinus Torvalds 		params_period_size(params) << rme96->capture_frlog;
10661da177e4SLinus Torvalds 	snd_rme96_set_period_properties(rme96, rme96->capture_periodsize);
10671da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	return 0;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds static void
snd_rme96_trigger(struct rme96 * rme96,int op)1073b892ca1cSKnut Petersen snd_rme96_trigger(struct rme96 *rme96,
1074b892ca1cSKnut Petersen 		  int op)
10751da177e4SLinus Torvalds {
1076b892ca1cSKnut Petersen 	if (op & RME96_TB_RESET_PLAYPOS)
10771da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
1078b892ca1cSKnut Petersen 	if (op & RME96_TB_RESET_CAPTUREPOS)
10791da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
1080b892ca1cSKnut Petersen 	if (op & RME96_TB_CLEAR_PLAYBACK_IRQ) {
10811da177e4SLinus Torvalds 		rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1082b892ca1cSKnut Petersen 		if (rme96->rcreg & RME96_RCR_IRQ)
10831da177e4SLinus Torvalds 			writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
10841da177e4SLinus Torvalds 	}
1085b892ca1cSKnut Petersen 	if (op & RME96_TB_CLEAR_CAPTURE_IRQ) {
10861da177e4SLinus Torvalds 		rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
1087b892ca1cSKnut Petersen 		if (rme96->rcreg & RME96_RCR_IRQ_2)
10881da177e4SLinus Torvalds 			writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
10891da177e4SLinus Torvalds 	}
1090b892ca1cSKnut Petersen 	if (op & RME96_TB_START_PLAYBACK)
1091b892ca1cSKnut Petersen 		rme96->wcreg |= RME96_WCR_START;
1092b892ca1cSKnut Petersen 	if (op & RME96_TB_STOP_PLAYBACK)
1093b892ca1cSKnut Petersen 		rme96->wcreg &= ~RME96_WCR_START;
1094b892ca1cSKnut Petersen 	if (op & RME96_TB_START_CAPTURE)
1095b892ca1cSKnut Petersen 		rme96->wcreg |= RME96_WCR_START_2;
1096b892ca1cSKnut Petersen 	if (op & RME96_TB_STOP_CAPTURE)
10971da177e4SLinus Torvalds 		rme96->wcreg &= ~RME96_WCR_START_2;
10981da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101b892ca1cSKnut Petersen 
1102b892ca1cSKnut Petersen 
11031da177e4SLinus Torvalds static irqreturn_t
snd_rme96_interrupt(int irq,void * dev_id)11041da177e4SLinus Torvalds snd_rme96_interrupt(int irq,
11057d12e780SDavid Howells 		    void *dev_id)
11061da177e4SLinus Torvalds {
1107a3aefd88STakashi Iwai 	struct rme96 *rme96 = (struct rme96 *)dev_id;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
11101da177e4SLinus Torvalds 	/* fastpath out, to ease interrupt sharing */
11111da177e4SLinus Torvalds 	if (!((rme96->rcreg & RME96_RCR_IRQ) ||
11121da177e4SLinus Torvalds 	      (rme96->rcreg & RME96_RCR_IRQ_2)))
11131da177e4SLinus Torvalds 	{
11141da177e4SLinus Torvalds 		return IRQ_NONE;
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ) {
11181da177e4SLinus Torvalds 		/* playback */
11191da177e4SLinus Torvalds                 snd_pcm_period_elapsed(rme96->playback_substream);
11201da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_PLAY_IRQ);
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 	if (rme96->rcreg & RME96_RCR_IRQ_2) {
11231da177e4SLinus Torvalds 		/* capture */
11241da177e4SLinus Torvalds 		snd_pcm_period_elapsed(rme96->capture_substream);
11251da177e4SLinus Torvalds 		writel(0, rme96->iobase + RME96_IO_CONFIRM_REC_IRQ);
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds 	return IRQ_HANDLED;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
113084e7b998STakashi Iwai static const unsigned int period_bytes[] = { RME96_SMALL_BLOCK_SIZE, RME96_LARGE_BLOCK_SIZE };
11311da177e4SLinus Torvalds 
113284e7b998STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
11331da177e4SLinus Torvalds 	.count = ARRAY_SIZE(period_bytes),
11341da177e4SLinus Torvalds 	.list = period_bytes,
11351da177e4SLinus Torvalds 	.mask = 0
11361da177e4SLinus Torvalds };
11371da177e4SLinus Torvalds 
11382ce7fb57STakashi Iwai static void
rme96_set_buffer_size_constraint(struct rme96 * rme96,struct snd_pcm_runtime * runtime)11392ce7fb57STakashi Iwai rme96_set_buffer_size_constraint(struct rme96 *rme96,
11402ce7fb57STakashi Iwai 				 struct snd_pcm_runtime *runtime)
11412ce7fb57STakashi Iwai {
11422ce7fb57STakashi Iwai 	unsigned int size;
11432ce7fb57STakashi Iwai 
11441a8e41efSLars-Peter Clausen 	snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
11451a8e41efSLars-Peter Clausen 				     RME96_BUFFER_SIZE);
1146da8eedb1STakashi Iwai 	size = rme96->playback_periodsize;
1147da8eedb1STakashi Iwai 	if (!size)
1148da8eedb1STakashi Iwai 		size = rme96->capture_periodsize;
1149da8eedb1STakashi Iwai 	if (size)
11501a8e41efSLars-Peter Clausen 		snd_pcm_hw_constraint_single(runtime,
11512ce7fb57STakashi Iwai 					     SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
11521a8e41efSLars-Peter Clausen 					     size);
11532ce7fb57STakashi Iwai 	else
11542ce7fb57STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
11552ce7fb57STakashi Iwai 					   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
11562ce7fb57STakashi Iwai 					   &hw_constraints_period_bytes);
11572ce7fb57STakashi Iwai }
11582ce7fb57STakashi Iwai 
11591da177e4SLinus Torvalds static int
snd_rme96_playback_spdif_open(struct snd_pcm_substream * substream)1160a3aefd88STakashi Iwai snd_rme96_playback_spdif_open(struct snd_pcm_substream *substream)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds         int rate, dummy;
1163a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1164a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11651da177e4SLinus Torvalds 
1166b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
11671da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
11682cb98d01SMarkus Elfring 	if (rme96->playback_substream) {
11691da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
11701da177e4SLinus Torvalds                 return -EBUSY;
11711da177e4SLinus Torvalds         }
11721da177e4SLinus Torvalds 	rme96->wcreg &= ~RME96_WCR_ADAT;
11731da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
11741da177e4SLinus Torvalds 	rme96->playback_substream = substream;
11751da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_spdif_info;
11781da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
1179da8eedb1STakashi Iwai 	    snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG) {
1180da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &dummy);
1181da8eedb1STakashi Iwai 		if (rate > 0) {
11821da177e4SLinus Torvalds 			/* slave clock */
1183918f3a0eSClemens Ladisch 			runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
11841da177e4SLinus Torvalds 			runtime->hw.rate_min = rate;
11851da177e4SLinus Torvalds 			runtime->hw.rate_max = rate;
11861da177e4SLinus Torvalds 		}
1187da8eedb1STakashi Iwai 	}
11882ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = rme96->wcreg_spdif;
11911da177e4SLinus Torvalds 	rme96->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
11921da177e4SLinus Torvalds 	snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
11931da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
11941da177e4SLinus Torvalds 	return 0;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds static int
snd_rme96_capture_spdif_open(struct snd_pcm_substream * substream)1198a3aefd88STakashi Iwai snd_rme96_capture_spdif_open(struct snd_pcm_substream *substream)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds         int isadat, rate;
1201a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1202a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12031da177e4SLinus Torvalds 
1204b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12051da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_spdif_info;
1206da8eedb1STakashi Iwai 	if (snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG) {
1207da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &isadat);
1208da8eedb1STakashi Iwai 		if (rate > 0) {
1209da8eedb1STakashi Iwai 			if (isadat)
12101da177e4SLinus Torvalds 				return -EIO;
1211918f3a0eSClemens Ladisch 			runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12121da177e4SLinus Torvalds 			runtime->hw.rate_min = rate;
12131da177e4SLinus Torvalds 			runtime->hw.rate_max = rate;
12141da177e4SLinus Torvalds 		}
1215da8eedb1STakashi Iwai 	}
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12182cb98d01SMarkus Elfring 	if (rme96->capture_substream) {
12191da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12201da177e4SLinus Torvalds                 return -EBUSY;
12211da177e4SLinus Torvalds         }
12221da177e4SLinus Torvalds 	rme96->capture_substream = substream;
12231da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12241da177e4SLinus Torvalds 
12252ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12261da177e4SLinus Torvalds 	return 0;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds static int
snd_rme96_playback_adat_open(struct snd_pcm_substream * substream)1230a3aefd88STakashi Iwai snd_rme96_playback_adat_open(struct snd_pcm_substream *substream)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds         int rate, dummy;
1233a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1234a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12351da177e4SLinus Torvalds 
1236b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12371da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12382cb98d01SMarkus Elfring 	if (rme96->playback_substream) {
12391da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12401da177e4SLinus Torvalds                 return -EBUSY;
12411da177e4SLinus Torvalds         }
12421da177e4SLinus Torvalds 	rme96->wcreg |= RME96_WCR_ADAT;
12431da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
12441da177e4SLinus Torvalds 	rme96->playback_substream = substream;
12451da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	runtime->hw = snd_rme96_playback_adat_info;
12481da177e4SLinus Torvalds 	if (!(rme96->wcreg & RME96_WCR_MASTER) &&
1249da8eedb1STakashi Iwai 	    snd_rme96_getinputtype(rme96) != RME96_INPUT_ANALOG) {
1250da8eedb1STakashi Iwai 		rate = snd_rme96_capture_getrate(rme96, &dummy);
1251da8eedb1STakashi Iwai 		if (rate > 0) {
12521da177e4SLinus Torvalds 			/* slave clock */
1253918f3a0eSClemens Ladisch 			runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12541da177e4SLinus Torvalds 			runtime->hw.rate_min = rate;
12551da177e4SLinus Torvalds 			runtime->hw.rate_max = rate;
12561da177e4SLinus Torvalds 		}
1257da8eedb1STakashi Iwai 	}
1258da8eedb1STakashi Iwai 
12592ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12601da177e4SLinus Torvalds 	return 0;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds static int
snd_rme96_capture_adat_open(struct snd_pcm_substream * substream)1264a3aefd88STakashi Iwai snd_rme96_capture_adat_open(struct snd_pcm_substream *substream)
12651da177e4SLinus Torvalds {
12661da177e4SLinus Torvalds         int isadat, rate;
1267a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1268a3aefd88STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
12691da177e4SLinus Torvalds 
1270b892ca1cSKnut Petersen 	snd_pcm_set_sync(substream);
12711da177e4SLinus Torvalds 	runtime->hw = snd_rme96_capture_adat_info;
12721da177e4SLinus Torvalds         if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
12731da177e4SLinus Torvalds                 /* makes no sense to use analog input. Note that analog
12741da177e4SLinus Torvalds                    expension cards AEB4/8-I are RME96_INPUT_INTERNAL */
12751da177e4SLinus Torvalds                 return -EIO;
12761da177e4SLinus Torvalds         }
1277da8eedb1STakashi Iwai 	rate = snd_rme96_capture_getrate(rme96, &isadat);
1278da8eedb1STakashi Iwai 	if (rate > 0) {
12791da177e4SLinus Torvalds                 if (!isadat) {
12801da177e4SLinus Torvalds                         return -EIO;
12811da177e4SLinus Torvalds                 }
1282918f3a0eSClemens Ladisch                 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
12831da177e4SLinus Torvalds                 runtime->hw.rate_min = rate;
12841da177e4SLinus Torvalds                 runtime->hw.rate_max = rate;
12851da177e4SLinus Torvalds         }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
12882cb98d01SMarkus Elfring 	if (rme96->capture_substream) {
12891da177e4SLinus Torvalds 		spin_unlock_irq(&rme96->lock);
12901da177e4SLinus Torvalds                 return -EBUSY;
12911da177e4SLinus Torvalds         }
12921da177e4SLinus Torvalds 	rme96->capture_substream = substream;
12931da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
12941da177e4SLinus Torvalds 
12952ce7fb57STakashi Iwai 	rme96_set_buffer_size_constraint(rme96, runtime);
12961da177e4SLinus Torvalds 	return 0;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds static int
snd_rme96_playback_close(struct snd_pcm_substream * substream)1300a3aefd88STakashi Iwai snd_rme96_playback_close(struct snd_pcm_substream *substream)
13011da177e4SLinus Torvalds {
1302a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13031da177e4SLinus Torvalds 	int spdif = 0;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13061da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
1307b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 	rme96->playback_substream = NULL;
13101da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
13111da177e4SLinus Torvalds 	spdif = (rme96->wcreg & RME96_WCR_ADAT) == 0;
13121da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13131da177e4SLinus Torvalds 	if (spdif) {
13141da177e4SLinus Torvalds 		rme96->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
13151da177e4SLinus Torvalds 		snd_ctl_notify(rme96->card, SNDRV_CTL_EVENT_MASK_VALUE |
13161da177e4SLinus Torvalds 			       SNDRV_CTL_EVENT_MASK_INFO, &rme96->spdif_ctl->id);
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 	return 0;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds static int
snd_rme96_capture_close(struct snd_pcm_substream * substream)1322a3aefd88STakashi Iwai snd_rme96_capture_close(struct snd_pcm_substream *substream)
13231da177e4SLinus Torvalds {
1324a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13271da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
1328b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
13291da177e4SLinus Torvalds 	}
13301da177e4SLinus Torvalds 	rme96->capture_substream = NULL;
13311da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
13321da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13331da177e4SLinus Torvalds 	return 0;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds static int
snd_rme96_playback_prepare(struct snd_pcm_substream * substream)1337a3aefd88STakashi Iwai snd_rme96_playback_prepare(struct snd_pcm_substream *substream)
13381da177e4SLinus Torvalds {
1339a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13421da177e4SLinus Torvalds 	if (RME96_ISPLAYING(rme96)) {
1343b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_PLAYBACK);
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
13461da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13471da177e4SLinus Torvalds 	return 0;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds static int
snd_rme96_capture_prepare(struct snd_pcm_substream * substream)1351a3aefd88STakashi Iwai snd_rme96_capture_prepare(struct snd_pcm_substream *substream)
13521da177e4SLinus Torvalds {
1353a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
13561da177e4SLinus Torvalds 	if (RME96_ISRECORDING(rme96)) {
1357b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_CAPTURE);
13581da177e4SLinus Torvalds 	}
13591da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
13601da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
13611da177e4SLinus Torvalds 	return 0;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds static int
snd_rme96_playback_trigger(struct snd_pcm_substream * substream,int cmd)1365a3aefd88STakashi Iwai snd_rme96_playback_trigger(struct snd_pcm_substream *substream,
13661da177e4SLinus Torvalds 			   int cmd)
13671da177e4SLinus Torvalds {
1368a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1369b892ca1cSKnut Petersen 	struct snd_pcm_substream *s;
1370b892ca1cSKnut Petersen 	bool sync;
1371b892ca1cSKnut Petersen 
1372b892ca1cSKnut Petersen 	snd_pcm_group_for_each_entry(s, substream) {
1373b892ca1cSKnut Petersen 		if (snd_pcm_substream_chip(s) == rme96)
1374b892ca1cSKnut Petersen 			snd_pcm_trigger_done(s, substream);
1375b892ca1cSKnut Petersen 	}
1376b892ca1cSKnut Petersen 
1377b892ca1cSKnut Petersen 	sync = (rme96->playback_substream && rme96->capture_substream) &&
1378b892ca1cSKnut Petersen 	       (rme96->playback_substream->group ==
1379b892ca1cSKnut Petersen 		rme96->capture_substream->group);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	switch (cmd) {
13821da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
13831da177e4SLinus Torvalds 		if (!RME96_ISPLAYING(rme96)) {
1384b892ca1cSKnut Petersen 			if (substream != rme96->playback_substream)
13851da177e4SLinus Torvalds 				return -EBUSY;
1386b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1387b892ca1cSKnut Petersen 						 : RME96_START_PLAYBACK);
13881da177e4SLinus Torvalds 		}
13891da177e4SLinus Torvalds 		break;
13901da177e4SLinus Torvalds 
1391528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_SUSPEND:
13921da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
13931da177e4SLinus Torvalds 		if (RME96_ISPLAYING(rme96)) {
1394b892ca1cSKnut Petersen 			if (substream != rme96->playback_substream)
13951da177e4SLinus Torvalds 				return -EBUSY;
1396b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1397b892ca1cSKnut Petersen 						 :  RME96_STOP_PLAYBACK);
13981da177e4SLinus Torvalds 		}
13991da177e4SLinus Torvalds 		break;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1402b892ca1cSKnut Petersen 		if (RME96_ISPLAYING(rme96))
1403b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1404b892ca1cSKnut Petersen 						 : RME96_STOP_PLAYBACK);
14051da177e4SLinus Torvalds 		break;
14061da177e4SLinus Torvalds 
1407528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_RESUME:
14081da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1409b892ca1cSKnut Petersen 		if (!RME96_ISPLAYING(rme96))
1410b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1411b892ca1cSKnut Petersen 						 : RME96_RESUME_PLAYBACK);
14121da177e4SLinus Torvalds 		break;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	default:
14151da177e4SLinus Torvalds 		return -EINVAL;
14161da177e4SLinus Torvalds 	}
1417b892ca1cSKnut Petersen 
14181da177e4SLinus Torvalds 	return 0;
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds static int
snd_rme96_capture_trigger(struct snd_pcm_substream * substream,int cmd)1422a3aefd88STakashi Iwai snd_rme96_capture_trigger(struct snd_pcm_substream *substream,
14231da177e4SLinus Torvalds 			  int cmd)
14241da177e4SLinus Torvalds {
1425a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
1426b892ca1cSKnut Petersen 	struct snd_pcm_substream *s;
1427b892ca1cSKnut Petersen 	bool sync;
1428b892ca1cSKnut Petersen 
1429b892ca1cSKnut Petersen 	snd_pcm_group_for_each_entry(s, substream) {
1430b892ca1cSKnut Petersen 		if (snd_pcm_substream_chip(s) == rme96)
1431b892ca1cSKnut Petersen 			snd_pcm_trigger_done(s, substream);
1432b892ca1cSKnut Petersen 	}
1433b892ca1cSKnut Petersen 
1434b892ca1cSKnut Petersen 	sync = (rme96->playback_substream && rme96->capture_substream) &&
1435b892ca1cSKnut Petersen 	       (rme96->playback_substream->group ==
1436b892ca1cSKnut Petersen 		rme96->capture_substream->group);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	switch (cmd) {
14391da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
14401da177e4SLinus Torvalds 		if (!RME96_ISRECORDING(rme96)) {
1441b892ca1cSKnut Petersen 			if (substream != rme96->capture_substream)
14421da177e4SLinus Torvalds 				return -EBUSY;
1443b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_START_BOTH
1444b892ca1cSKnut Petersen 						 : RME96_START_CAPTURE);
14451da177e4SLinus Torvalds 		}
14461da177e4SLinus Torvalds 		break;
14471da177e4SLinus Torvalds 
1448528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_SUSPEND:
14491da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
14501da177e4SLinus Torvalds 		if (RME96_ISRECORDING(rme96)) {
1451b892ca1cSKnut Petersen 			if (substream != rme96->capture_substream)
14521da177e4SLinus Torvalds 				return -EBUSY;
1453b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1454b892ca1cSKnut Petersen 						 : RME96_STOP_CAPTURE);
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 		break;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1459b892ca1cSKnut Petersen 		if (RME96_ISRECORDING(rme96))
1460b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_STOP_BOTH
1461b892ca1cSKnut Petersen 						 : RME96_STOP_CAPTURE);
14621da177e4SLinus Torvalds 		break;
14631da177e4SLinus Torvalds 
1464528ba522SKnut Petersen 	case SNDRV_PCM_TRIGGER_RESUME:
14651da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1466b892ca1cSKnut Petersen 		if (!RME96_ISRECORDING(rme96))
1467b892ca1cSKnut Petersen 			snd_rme96_trigger(rme96, sync ? RME96_RESUME_BOTH
1468b892ca1cSKnut Petersen 						 : RME96_RESUME_CAPTURE);
14691da177e4SLinus Torvalds 		break;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	default:
14721da177e4SLinus Torvalds 		return -EINVAL;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	return 0;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds static snd_pcm_uframes_t
snd_rme96_playback_pointer(struct snd_pcm_substream * substream)1479a3aefd88STakashi Iwai snd_rme96_playback_pointer(struct snd_pcm_substream *substream)
14801da177e4SLinus Torvalds {
1481a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14821da177e4SLinus Torvalds 	return snd_rme96_playback_ptr(rme96);
14831da177e4SLinus Torvalds }
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds static snd_pcm_uframes_t
snd_rme96_capture_pointer(struct snd_pcm_substream * substream)1486a3aefd88STakashi Iwai snd_rme96_capture_pointer(struct snd_pcm_substream *substream)
14871da177e4SLinus Torvalds {
1488a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_pcm_substream_chip(substream);
14891da177e4SLinus Torvalds 	return snd_rme96_capture_ptr(rme96);
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14926769e988SJulia Lawall static const struct snd_pcm_ops snd_rme96_playback_spdif_ops = {
14931da177e4SLinus Torvalds 	.open =		snd_rme96_playback_spdif_open,
14941da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
14951da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
14961da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
14971da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
14981da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
1499*c3abdf06STakashi Iwai 	.copy =		snd_rme96_playback_copy,
150053945f5bSTakashi Iwai 	.fill_silence =	snd_rme96_playback_silence,
15011da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15021da177e4SLinus Torvalds };
15031da177e4SLinus Torvalds 
15046769e988SJulia Lawall static const struct snd_pcm_ops snd_rme96_capture_spdif_ops = {
15051da177e4SLinus Torvalds 	.open =		snd_rme96_capture_spdif_open,
15061da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
15071da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
15081da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
15091da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
15101da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
1511*c3abdf06STakashi Iwai 	.copy =		snd_rme96_capture_copy,
15121da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15131da177e4SLinus Torvalds };
15141da177e4SLinus Torvalds 
15156769e988SJulia Lawall static const struct snd_pcm_ops snd_rme96_playback_adat_ops = {
15161da177e4SLinus Torvalds 	.open =		snd_rme96_playback_adat_open,
15171da177e4SLinus Torvalds 	.close =	snd_rme96_playback_close,
15181da177e4SLinus Torvalds 	.hw_params =	snd_rme96_playback_hw_params,
15191da177e4SLinus Torvalds 	.prepare =	snd_rme96_playback_prepare,
15201da177e4SLinus Torvalds 	.trigger =	snd_rme96_playback_trigger,
15211da177e4SLinus Torvalds 	.pointer =	snd_rme96_playback_pointer,
1522*c3abdf06STakashi Iwai 	.copy =		snd_rme96_playback_copy,
152353945f5bSTakashi Iwai 	.fill_silence =	snd_rme96_playback_silence,
15241da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15251da177e4SLinus Torvalds };
15261da177e4SLinus Torvalds 
15276769e988SJulia Lawall static const struct snd_pcm_ops snd_rme96_capture_adat_ops = {
15281da177e4SLinus Torvalds 	.open =		snd_rme96_capture_adat_open,
15291da177e4SLinus Torvalds 	.close =	snd_rme96_capture_close,
15301da177e4SLinus Torvalds 	.hw_params =	snd_rme96_capture_hw_params,
15311da177e4SLinus Torvalds 	.prepare =	snd_rme96_capture_prepare,
15321da177e4SLinus Torvalds 	.trigger =	snd_rme96_capture_trigger,
15331da177e4SLinus Torvalds 	.pointer =	snd_rme96_capture_pointer,
1534*c3abdf06STakashi Iwai 	.copy =		snd_rme96_capture_copy,
15351da177e4SLinus Torvalds 	.mmap =		snd_pcm_lib_mmap_iomem,
15361da177e4SLinus Torvalds };
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds static void
snd_rme96_free(struct rme96 * rme96)1539df06df7cSTakashi Iwai snd_rme96_free(struct rme96 *rme96)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	if (rme96->irq >= 0) {
1542b892ca1cSKnut Petersen 		snd_rme96_trigger(rme96, RME96_STOP_BOTH);
15431da177e4SLinus Torvalds 		rme96->areg &= ~RME96_AR_DAC_EN;
15441da177e4SLinus Torvalds 		writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
15451da177e4SLinus Torvalds 	}
1546a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
1547528ba522SKnut Petersen 	vfree(rme96->playback_suspend_buffer);
1548528ba522SKnut Petersen 	vfree(rme96->capture_suspend_buffer);
1549528ba522SKnut Petersen #endif
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds static void
snd_rme96_free_spdif_pcm(struct snd_pcm * pcm)1553a3aefd88STakashi Iwai snd_rme96_free_spdif_pcm(struct snd_pcm *pcm)
15541da177e4SLinus Torvalds {
15559fe856e4SJoe Perches 	struct rme96 *rme96 = pcm->private_data;
15561da177e4SLinus Torvalds 	rme96->spdif_pcm = NULL;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds static void
snd_rme96_free_adat_pcm(struct snd_pcm * pcm)1560a3aefd88STakashi Iwai snd_rme96_free_adat_pcm(struct snd_pcm *pcm)
15611da177e4SLinus Torvalds {
15629fe856e4SJoe Perches 	struct rme96 *rme96 = pcm->private_data;
15631da177e4SLinus Torvalds 	rme96->adat_pcm = NULL;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
1566e23e7a14SBill Pemberton static int
snd_rme96_create(struct rme96 * rme96)1567a3aefd88STakashi Iwai snd_rme96_create(struct rme96 *rme96)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	struct pci_dev *pci = rme96->pci;
15701da177e4SLinus Torvalds 	int err;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	rme96->irq = -1;
15731da177e4SLinus Torvalds 	spin_lock_init(&rme96->lock);
15741da177e4SLinus Torvalds 
1575df06df7cSTakashi Iwai 	err = pcim_enable_device(pci);
1576da8eedb1STakashi Iwai 	if (err < 0)
15771da177e4SLinus Torvalds 		return err;
15781da177e4SLinus Torvalds 
1579da8eedb1STakashi Iwai 	err = pci_request_regions(pci, "RME96");
1580da8eedb1STakashi Iwai 	if (err < 0)
15811da177e4SLinus Torvalds 		return err;
15821da177e4SLinus Torvalds 	rme96->port = pci_resource_start(rme96->pci, 0);
15831da177e4SLinus Torvalds 
1584df06df7cSTakashi Iwai 	rme96->iobase = devm_ioremap(&pci->dev, rme96->port, RME96_IO_SIZE);
158544977b71SHarvey Harrison 	if (!rme96->iobase) {
1586342cd934STakashi Iwai 		dev_err(rme96->card->dev,
1587342cd934STakashi Iwai 			"unable to remap memory region 0x%lx-0x%lx\n",
1588342cd934STakashi Iwai 			rme96->port, rme96->port + RME96_IO_SIZE - 1);
1589df06df7cSTakashi Iwai 		return -EBUSY;
1590688956f2STakashi Iwai 	}
1591688956f2STakashi Iwai 
1592df06df7cSTakashi Iwai 	if (devm_request_irq(&pci->dev, pci->irq, snd_rme96_interrupt,
1593df06df7cSTakashi Iwai 			     IRQF_SHARED, KBUILD_MODNAME, rme96)) {
1594342cd934STakashi Iwai 		dev_err(rme96->card->dev, "unable to grab IRQ %d\n", pci->irq);
15951da177e4SLinus Torvalds 		return -EBUSY;
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 	rme96->irq = pci->irq;
15985fcf46bcSTakashi Iwai 	rme96->card->sync_irq = rme96->irq;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	/* read the card's revision number */
16011da177e4SLinus Torvalds 	pci_read_config_byte(pci, 8, &rme96->rev);
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	/* set up ALSA pcm device for S/PDIF */
1604da8eedb1STakashi Iwai 	err = snd_pcm_new(rme96->card, "Digi96 IEC958", 0,
1605da8eedb1STakashi Iwai 			  1, 1, &rme96->spdif_pcm);
1606da8eedb1STakashi Iwai 	if (err < 0)
16071da177e4SLinus Torvalds 		return err;
1608da8eedb1STakashi Iwai 
16091da177e4SLinus Torvalds 	rme96->spdif_pcm->private_data = rme96;
16101da177e4SLinus Torvalds 	rme96->spdif_pcm->private_free = snd_rme96_free_spdif_pcm;
16111da177e4SLinus Torvalds 	strcpy(rme96->spdif_pcm->name, "Digi96 IEC958");
16121da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_spdif_ops);
16131da177e4SLinus Torvalds 	snd_pcm_set_ops(rme96->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_spdif_ops);
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	rme96->spdif_pcm->info_flags = 0;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	/* set up ALSA pcm device for ADAT */
16188b7fc421SRoland Dreier 	if (pci->device == PCI_DEVICE_ID_RME_DIGI96) {
16191da177e4SLinus Torvalds 		/* ADAT is not available on the base model */
16201da177e4SLinus Torvalds 		rme96->adat_pcm = NULL;
16211da177e4SLinus Torvalds 	} else {
1622da8eedb1STakashi Iwai 		err = snd_pcm_new(rme96->card, "Digi96 ADAT", 1,
1623da8eedb1STakashi Iwai 				  1, 1, &rme96->adat_pcm);
1624da8eedb1STakashi Iwai 		if (err < 0)
16251da177e4SLinus Torvalds 			return err;
16261da177e4SLinus Torvalds 		rme96->adat_pcm->private_data = rme96;
16271da177e4SLinus Torvalds 		rme96->adat_pcm->private_free = snd_rme96_free_adat_pcm;
16281da177e4SLinus Torvalds 		strcpy(rme96->adat_pcm->name, "Digi96 ADAT");
16291da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme96_playback_adat_ops);
16301da177e4SLinus Torvalds 		snd_pcm_set_ops(rme96->adat_pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme96_capture_adat_ops);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		rme96->adat_pcm->info_flags = 0;
16331da177e4SLinus Torvalds 	}
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	rme96->playback_periodsize = 0;
16361da177e4SLinus Torvalds 	rme96->capture_periodsize = 0;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	/* make sure playback/capture is stopped, if by some reason active */
1639b892ca1cSKnut Petersen 	snd_rme96_trigger(rme96, RME96_STOP_BOTH);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/* set default values in registers */
16421da177e4SLinus Torvalds 	rme96->wcreg =
16431da177e4SLinus Torvalds 		RME96_WCR_FREQ_1 | /* set 44.1 kHz playback */
16441da177e4SLinus Torvalds 		RME96_WCR_SEL |    /* normal playback */
16451da177e4SLinus Torvalds 		RME96_WCR_MASTER | /* set to master clock mode */
16461da177e4SLinus Torvalds 		RME96_WCR_INP_0;   /* set coaxial input */
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	rme96->areg = RME96_AR_FREQPAD_1; /* set 44.1 kHz analog capture */
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
16511da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	/* reset the ADC */
16541da177e4SLinus Torvalds 	writel(rme96->areg | RME96_AR_PD2,
16551da177e4SLinus Torvalds 	       rme96->iobase + RME96_IO_ADDITIONAL_REG);
16561da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	/* reset and enable the DAC (order is important). */
16591da177e4SLinus Torvalds 	snd_rme96_reset_dac(rme96);
16601da177e4SLinus Torvalds 	rme96->areg |= RME96_AR_DAC_EN;
16611da177e4SLinus Torvalds 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	/* reset playback and record buffer pointers */
16641da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_PLAY_POS);
16651da177e4SLinus Torvalds 	writel(0, rme96->iobase + RME96_IO_RESET_REC_POS);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	/* reset volume */
16681da177e4SLinus Torvalds 	rme96->vol[0] = rme96->vol[1] = 0;
16691da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_OUT(rme96)) {
16701da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	/* init switch interface */
1674da8eedb1STakashi Iwai 	err = snd_rme96_create_switches(rme96->card, rme96);
1675da8eedb1STakashi Iwai 	if (err < 0)
16761da177e4SLinus Torvalds 		return err;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds         /* init proc interface */
16791da177e4SLinus Torvalds 	snd_rme96_proc_init(rme96);
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	return 0;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds /*
16851da177e4SLinus Torvalds  * proc interface
16861da177e4SLinus Torvalds  */
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds static void
snd_rme96_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1689a3aefd88STakashi Iwai snd_rme96_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	int n;
16929fe856e4SJoe Perches 	struct rme96 *rme96 = entry->private_data;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	rme96->rcreg = readl(rme96->iobase + RME96_IO_CONTROL_REGISTER);
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	snd_iprintf(buffer, rme96->card->longname);
16971da177e4SLinus Torvalds 	snd_iprintf(buffer, " (index #%d)\n", rme96->card->number + 1);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nGeneral settings\n");
17001da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_IDIS) {
17011da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: N/A (interrupts "
17021da177e4SLinus Torvalds 			    "disabled)\n");
17031da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_ISEL) {
17041da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 2048 bytes\n");
17051da177e4SLinus Torvalds 	} else {
17061da177e4SLinus Torvalds 		snd_iprintf(buffer, "  period size: 8192 bytes\n");
17071da177e4SLinus Torvalds 	}
17081da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nInput settings\n");
17091da177e4SLinus Torvalds 	switch (snd_rme96_getinputtype(rme96)) {
17101da177e4SLinus Torvalds 	case RME96_INPUT_OPTICAL:
17111da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: optical");
17121da177e4SLinus Torvalds 		break;
17131da177e4SLinus Torvalds 	case RME96_INPUT_COAXIAL:
17141da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: coaxial");
17151da177e4SLinus Torvalds 		break;
17161da177e4SLinus Torvalds 	case RME96_INPUT_INTERNAL:
17171da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: internal");
17181da177e4SLinus Torvalds 		break;
17191da177e4SLinus Torvalds 	case RME96_INPUT_XLR:
17201da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: XLR");
17211da177e4SLinus Torvalds 		break;
17221da177e4SLinus Torvalds 	case RME96_INPUT_ANALOG:
17231da177e4SLinus Torvalds 		snd_iprintf(buffer, "  input: analog");
17241da177e4SLinus Torvalds 		break;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 	if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17271da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n  sample rate: no valid signal\n");
17281da177e4SLinus Torvalds 	} else {
17291da177e4SLinus Torvalds 		if (n) {
17301da177e4SLinus Torvalds 			snd_iprintf(buffer, " (8 channels)\n");
17311da177e4SLinus Torvalds 		} else {
17321da177e4SLinus Torvalds 			snd_iprintf(buffer, " (2 channels)\n");
17331da177e4SLinus Torvalds 		}
17341da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample rate: %d Hz\n",
17351da177e4SLinus Torvalds 			    snd_rme96_capture_getrate(rme96, &n));
17361da177e4SLinus Torvalds 	}
17371da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24_2) {
17381da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17391da177e4SLinus Torvalds 	} else {
17401da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	snd_iprintf(buffer, "\nOutput settings\n");
17441da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_SEL) {
17451da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: normal playback\n");
17461da177e4SLinus Torvalds 	} else {
17471da177e4SLinus Torvalds 		snd_iprintf(buffer, "  output signal: same as input\n");
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds 	snd_iprintf(buffer, "  sample rate: %d Hz\n",
17501da177e4SLinus Torvalds 		    snd_rme96_playback_getrate(rme96));
17511da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_MODE24) {
17521da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 24 bit\n");
17531da177e4SLinus Torvalds 	} else {
17541da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample format: 16 bit\n");
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 	if (rme96->areg & RME96_AR_WSEL) {
17571da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: word clock\n");
17581da177e4SLinus Torvalds 	} else if (rme96->wcreg & RME96_WCR_MASTER) {
17591da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: internal\n");
17601da177e4SLinus Torvalds 	} else if (snd_rme96_getinputtype(rme96) == RME96_INPUT_ANALOG) {
17611da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to analog input setting)\n");
17621da177e4SLinus Torvalds 	} else if (snd_rme96_capture_getrate(rme96, &n) < 0) {
17631da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync (internal anyway due to no valid signal)\n");
17641da177e4SLinus Torvalds 	} else {
17651da177e4SLinus Torvalds 		snd_iprintf(buffer, "  sample clock source: autosync\n");
17661da177e4SLinus Torvalds 	}
17671da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_PRO) {
17681da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: AES/EBU (professional)\n");
17691da177e4SLinus Torvalds 	} else {
17701da177e4SLinus Torvalds 		snd_iprintf(buffer, "  format: IEC958 (consumer)\n");
17711da177e4SLinus Torvalds 	}
17721da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_EMP) {
17731da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: on\n");
17741da177e4SLinus Torvalds 	} else {
17751da177e4SLinus Torvalds 		snd_iprintf(buffer, "  emphasis: off\n");
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 	if (rme96->wcreg & RME96_WCR_DOLBY) {
17781da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): on\n");
17791da177e4SLinus Torvalds 	} else {
17801da177e4SLinus Torvalds 		snd_iprintf(buffer, "  non-audio (dolby): off\n");
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_IN(rme96)) {
17831da177e4SLinus Torvalds 		snd_iprintf(buffer, "\nAnalog output settings\n");
17841da177e4SLinus Torvalds 		switch (snd_rme96_getmontracks(rme96)) {
17851da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_1_2:
17861da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 1+2\n");
17871da177e4SLinus Torvalds 			break;
17881da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_3_4:
17891da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 3+4\n");
17901da177e4SLinus Torvalds 			break;
17911da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_5_6:
17921da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 5+6\n");
17931da177e4SLinus Torvalds 			break;
17941da177e4SLinus Torvalds 		case RME96_MONITOR_TRACKS_7_8:
17951da177e4SLinus Torvalds 			snd_iprintf(buffer, "  monitored ADAT tracks: 7+8\n");
17961da177e4SLinus Torvalds 			break;
17971da177e4SLinus Torvalds 		}
17981da177e4SLinus Torvalds 		switch (snd_rme96_getattenuation(rme96)) {
17991da177e4SLinus Torvalds 		case RME96_ATTENUATION_0:
18001da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: 0 dB\n");
18011da177e4SLinus Torvalds 			break;
18021da177e4SLinus Torvalds 		case RME96_ATTENUATION_6:
18031da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -6 dB\n");
18041da177e4SLinus Torvalds 			break;
18051da177e4SLinus Torvalds 		case RME96_ATTENUATION_12:
18061da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -12 dB\n");
18071da177e4SLinus Torvalds 			break;
18081da177e4SLinus Torvalds 		case RME96_ATTENUATION_18:
18091da177e4SLinus Torvalds 			snd_iprintf(buffer, "  attenuation: -18 dB\n");
18101da177e4SLinus Torvalds 			break;
18111da177e4SLinus Torvalds 		}
18121da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume left: %u\n", rme96->vol[0]);
18131da177e4SLinus Torvalds 		snd_iprintf(buffer, "  volume right: %u\n", rme96->vol[1]);
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
snd_rme96_proc_init(struct rme96 * rme96)1817e23e7a14SBill Pemberton static void snd_rme96_proc_init(struct rme96 *rme96)
18181da177e4SLinus Torvalds {
181947f2769bSTakashi Iwai 	snd_card_ro_proc_new(rme96->card, "rme96", rme96, snd_rme96_proc_read);
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds /*
18231da177e4SLinus Torvalds  * control interface
18241da177e4SLinus Torvalds  */
18251da177e4SLinus Torvalds 
1826a5ce8890STakashi Iwai #define snd_rme96_info_loopback_control		snd_ctl_boolean_mono_info
1827a5ce8890STakashi Iwai 
18281da177e4SLinus Torvalds static int
snd_rme96_get_loopback_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1829a3aefd88STakashi Iwai snd_rme96_get_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18301da177e4SLinus Torvalds {
1831a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18341da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = rme96->wcreg & RME96_WCR_SEL ? 0 : 1;
18351da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18361da177e4SLinus Torvalds 	return 0;
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds static int
snd_rme96_put_loopback_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1839a3aefd88STakashi Iwai snd_rme96_put_loopback_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18401da177e4SLinus Torvalds {
1841a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18421da177e4SLinus Torvalds 	unsigned int val;
18431da177e4SLinus Torvalds 	int change;
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] ? 0 : RME96_WCR_SEL;
18461da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18471da177e4SLinus Torvalds 	val = (rme96->wcreg & ~RME96_WCR_SEL) | val;
18481da177e4SLinus Torvalds 	change = val != rme96->wcreg;
18491da177e4SLinus Torvalds 	rme96->wcreg = val;
18501da177e4SLinus Torvalds 	writel(val, rme96->iobase + RME96_IO_CONTROL_REGISTER);
18511da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
18521da177e4SLinus Torvalds 	return change;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds static int
snd_rme96_info_inputtype_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1856a3aefd88STakashi Iwai snd_rme96_info_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18571da177e4SLinus Torvalds {
18589c30d46aSTakashi Iwai 	static const char * const _texts[5] = {
18599c30d46aSTakashi Iwai 		"Optical", "Coaxial", "Internal", "XLR", "Analog"
18609c30d46aSTakashi Iwai 	};
1861a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18629c30d46aSTakashi Iwai 	const char *texts[5] = {
18639c30d46aSTakashi Iwai 		_texts[0], _texts[1], _texts[2], _texts[3], _texts[4]
18649c30d46aSTakashi Iwai 	};
18659c30d46aSTakashi Iwai 	int num_items;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	switch (rme96->pci->device) {
18688b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
18698b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
18709c30d46aSTakashi Iwai 		num_items = 3;
18711da177e4SLinus Torvalds 		break;
18728b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
18739c30d46aSTakashi Iwai 		num_items = 4;
18741da177e4SLinus Torvalds 		break;
18758b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
18761da177e4SLinus Torvalds 		if (rme96->rev > 4) {
18771da177e4SLinus Torvalds 			/* PST */
18789c30d46aSTakashi Iwai 			num_items = 4;
18791da177e4SLinus Torvalds 			texts[3] = _texts[4]; /* Analog instead of XLR */
18801da177e4SLinus Torvalds 		} else {
18811da177e4SLinus Torvalds 			/* PAD */
18829c30d46aSTakashi Iwai 			num_items = 5;
18831da177e4SLinus Torvalds 		}
18841da177e4SLinus Torvalds 		break;
18851da177e4SLinus Torvalds 	default:
18861da177e4SLinus Torvalds 		snd_BUG();
18879c30d46aSTakashi Iwai 		return -EINVAL;
18881da177e4SLinus Torvalds 	}
18899c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, num_items, texts);
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds static int
snd_rme96_get_inputtype_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1892a3aefd88STakashi Iwai snd_rme96_get_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18931da177e4SLinus Torvalds {
1894a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
18951da177e4SLinus Torvalds 	unsigned int items = 3;
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
18981da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getinputtype(rme96);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19018b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19028b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19031da177e4SLinus Torvalds 		items = 3;
19041da177e4SLinus Torvalds 		break;
19058b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19061da177e4SLinus Torvalds 		items = 4;
19071da177e4SLinus Torvalds 		break;
19088b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19091da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19101da177e4SLinus Torvalds 			/* for handling PST case, (INPUT_ANALOG is moved to INPUT_XLR */
19111da177e4SLinus Torvalds 			if (ucontrol->value.enumerated.item[0] == RME96_INPUT_ANALOG) {
19121da177e4SLinus Torvalds 				ucontrol->value.enumerated.item[0] = RME96_INPUT_XLR;
19131da177e4SLinus Torvalds 			}
19141da177e4SLinus Torvalds 			items = 4;
19151da177e4SLinus Torvalds 		} else {
19161da177e4SLinus Torvalds 			items = 5;
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 		break;
19191da177e4SLinus Torvalds 	default:
19201da177e4SLinus Torvalds 		snd_BUG();
19211da177e4SLinus Torvalds 		break;
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0] >= items) {
19241da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = items - 1;
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19281da177e4SLinus Torvalds 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds static int
snd_rme96_put_inputtype_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1931a3aefd88STakashi Iwai snd_rme96_put_inputtype_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19321da177e4SLinus Torvalds {
1933a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19341da177e4SLinus Torvalds 	unsigned int val;
19351da177e4SLinus Torvalds 	int change, items = 3;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	switch (rme96->pci->device) {
19388b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
19398b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
19401da177e4SLinus Torvalds 		items = 3;
19411da177e4SLinus Torvalds 		break;
19428b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
19431da177e4SLinus Torvalds 		items = 4;
19441da177e4SLinus Torvalds 		break;
19458b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
19461da177e4SLinus Torvalds 		if (rme96->rev > 4) {
19471da177e4SLinus Torvalds 			items = 4;
19481da177e4SLinus Torvalds 		} else {
19491da177e4SLinus Torvalds 			items = 5;
19501da177e4SLinus Torvalds 		}
19511da177e4SLinus Torvalds 		break;
19521da177e4SLinus Torvalds 	default:
19531da177e4SLinus Torvalds 		snd_BUG();
19541da177e4SLinus Torvalds 		break;
19551da177e4SLinus Torvalds 	}
19561da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % items;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	/* special case for PST */
19598b7fc421SRoland Dreier 	if (rme96->pci->device == PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST && rme96->rev > 4) {
19601da177e4SLinus Torvalds 		if (val == RME96_INPUT_XLR) {
19611da177e4SLinus Torvalds 			val = RME96_INPUT_ANALOG;
19621da177e4SLinus Torvalds 		}
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19661da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getinputtype(rme96);
19671da177e4SLinus Torvalds 	snd_rme96_setinputtype(rme96, val);
19681da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19691da177e4SLinus Torvalds 	return change;
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds static int
snd_rme96_info_clockmode_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1973a3aefd88STakashi Iwai snd_rme96_info_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19741da177e4SLinus Torvalds {
19759c30d46aSTakashi Iwai 	static const char * const texts[3] = { "AutoSync", "Internal", "Word" };
19761da177e4SLinus Torvalds 
19779c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds static int
snd_rme96_get_clockmode_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1980a3aefd88STakashi Iwai snd_rme96_get_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19811da177e4SLinus Torvalds {
1982a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19851da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getclockmode(rme96);
19861da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
19871da177e4SLinus Torvalds 	return 0;
19881da177e4SLinus Torvalds }
19891da177e4SLinus Torvalds static int
snd_rme96_put_clockmode_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1990a3aefd88STakashi Iwai snd_rme96_put_clockmode_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19911da177e4SLinus Torvalds {
1992a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
19931da177e4SLinus Torvalds 	unsigned int val;
19941da177e4SLinus Torvalds 	int change;
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 3;
19971da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
19981da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getclockmode(rme96);
19991da177e4SLinus Torvalds 	snd_rme96_setclockmode(rme96, val);
20001da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20011da177e4SLinus Torvalds 	return change;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds static int
snd_rme96_info_attenuation_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2005a3aefd88STakashi Iwai snd_rme96_info_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20061da177e4SLinus Torvalds {
20079c30d46aSTakashi Iwai 	static const char * const texts[4] = {
20089c30d46aSTakashi Iwai 		"0 dB", "-6 dB", "-12 dB", "-18 dB"
20099c30d46aSTakashi Iwai 	};
20101da177e4SLinus Torvalds 
20119c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 4, texts);
20121da177e4SLinus Torvalds }
20131da177e4SLinus Torvalds static int
snd_rme96_get_attenuation_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2014a3aefd88STakashi Iwai snd_rme96_get_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20151da177e4SLinus Torvalds {
2016a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20191da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getattenuation(rme96);
20201da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20211da177e4SLinus Torvalds 	return 0;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds static int
snd_rme96_put_attenuation_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2024a3aefd88STakashi Iwai snd_rme96_put_attenuation_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20251da177e4SLinus Torvalds {
2026a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20271da177e4SLinus Torvalds 	unsigned int val;
20281da177e4SLinus Torvalds 	int change;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20311da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getattenuation(rme96);
20341da177e4SLinus Torvalds 	snd_rme96_setattenuation(rme96, val);
20351da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20361da177e4SLinus Torvalds 	return change;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds static int
snd_rme96_info_montracks_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2040a3aefd88STakashi Iwai snd_rme96_info_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20411da177e4SLinus Torvalds {
20429c30d46aSTakashi Iwai 	static const char * const texts[4] = { "1+2", "3+4", "5+6", "7+8" };
20431da177e4SLinus Torvalds 
20449c30d46aSTakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 4, texts);
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds static int
snd_rme96_get_montracks_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2047a3aefd88STakashi Iwai snd_rme96_get_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20481da177e4SLinus Torvalds {
2049a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20521da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = snd_rme96_getmontracks(rme96);
20531da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20541da177e4SLinus Torvalds 	return 0;
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds static int
snd_rme96_put_montracks_control(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2057a3aefd88STakashi Iwai snd_rme96_put_montracks_control(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20581da177e4SLinus Torvalds {
2059a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
20601da177e4SLinus Torvalds 	unsigned int val;
20611da177e4SLinus Torvalds 	int change;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % 4;
20641da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
20651da177e4SLinus Torvalds 	change = (int)val != snd_rme96_getmontracks(rme96);
20661da177e4SLinus Torvalds 	snd_rme96_setmontracks(rme96, val);
20671da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
20681da177e4SLinus Torvalds 	return change;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
snd_rme96_convert_from_aes(struct snd_aes_iec958 * aes)2071a3aefd88STakashi Iwai static u32 snd_rme96_convert_from_aes(struct snd_aes_iec958 *aes)
20721da177e4SLinus Torvalds {
20731da177e4SLinus Torvalds 	u32 val = 0;
20741da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME96_WCR_PRO : 0;
20751da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME96_WCR_DOLBY : 0;
20761da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
20771da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
20781da177e4SLinus Torvalds 	else
20791da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME96_WCR_EMP : 0;
20801da177e4SLinus Torvalds 	return val;
20811da177e4SLinus Torvalds }
20821da177e4SLinus Torvalds 
snd_rme96_convert_to_aes(struct snd_aes_iec958 * aes,u32 val)2083a3aefd88STakashi Iwai static void snd_rme96_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
20841da177e4SLinus Torvalds {
20851da177e4SLinus Torvalds 	aes->status[0] = ((val & RME96_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
20861da177e4SLinus Torvalds 			 ((val & RME96_WCR_DOLBY) ? IEC958_AES0_NONAUDIO : 0);
20871da177e4SLinus Torvalds 	if (val & RME96_WCR_PRO)
20881da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
20891da177e4SLinus Torvalds 	else
20901da177e4SLinus Torvalds 		aes->status[0] |= (val & RME96_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
snd_rme96_control_spdif_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2093a3aefd88STakashi Iwai static int snd_rme96_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20941da177e4SLinus Torvalds {
20951da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
20961da177e4SLinus Torvalds 	uinfo->count = 1;
20971da177e4SLinus Torvalds 	return 0;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
snd_rme96_control_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2100a3aefd88STakashi Iwai static int snd_rme96_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21011da177e4SLinus Torvalds {
2102a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif);
21051da177e4SLinus Torvalds 	return 0;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
snd_rme96_control_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2108a3aefd88STakashi Iwai static int snd_rme96_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21091da177e4SLinus Torvalds {
2110a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21111da177e4SLinus Torvalds 	int change;
21121da177e4SLinus Torvalds 	u32 val;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21151da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21161da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif;
21171da177e4SLinus Torvalds 	rme96->wcreg_spdif = val;
21181da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21191da177e4SLinus Torvalds 	return change;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
snd_rme96_control_spdif_stream_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2122a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21251da177e4SLinus Torvalds 	uinfo->count = 1;
21261da177e4SLinus Torvalds 	return 0;
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
snd_rme96_control_spdif_stream_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2129a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21301da177e4SLinus Torvalds {
2131a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	snd_rme96_convert_to_aes(&ucontrol->value.iec958, rme96->wcreg_spdif_stream);
21341da177e4SLinus Torvalds 	return 0;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
snd_rme96_control_spdif_stream_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2137a3aefd88STakashi Iwai static int snd_rme96_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21381da177e4SLinus Torvalds {
2139a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21401da177e4SLinus Torvalds 	int change;
21411da177e4SLinus Torvalds 	u32 val;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	val = snd_rme96_convert_from_aes(&ucontrol->value.iec958);
21441da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21451da177e4SLinus Torvalds 	change = val != rme96->wcreg_spdif_stream;
21461da177e4SLinus Torvalds 	rme96->wcreg_spdif_stream = val;
21471da177e4SLinus Torvalds 	rme96->wcreg &= ~(RME96_WCR_PRO | RME96_WCR_DOLBY | RME96_WCR_EMP);
21481da177e4SLinus Torvalds 	rme96->wcreg |= val;
21491da177e4SLinus Torvalds 	writel(rme96->wcreg, rme96->iobase + RME96_IO_CONTROL_REGISTER);
21501da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21511da177e4SLinus Torvalds 	return change;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
snd_rme96_control_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2154a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21551da177e4SLinus Torvalds {
21561da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
21571da177e4SLinus Torvalds 	uinfo->count = 1;
21581da177e4SLinus Torvalds 	return 0;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
snd_rme96_control_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2161a3aefd88STakashi Iwai static int snd_rme96_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
21641da177e4SLinus Torvalds 	return 0;
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds static int
snd_rme96_dac_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2168a3aefd88STakashi Iwai snd_rme96_dac_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21691da177e4SLinus Torvalds {
2170a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
21731da177e4SLinus Torvalds         uinfo->count = 2;
21741da177e4SLinus Torvalds         uinfo->value.integer.min = 0;
21751da177e4SLinus Torvalds 	uinfo->value.integer.max = RME96_185X_MAX_OUT(rme96);
21761da177e4SLinus Torvalds         return 0;
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds static int
snd_rme96_dac_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * u)2180a3aefd88STakashi Iwai snd_rme96_dac_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
21811da177e4SLinus Torvalds {
2182a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
21851da177e4SLinus Torvalds         u->value.integer.value[0] = rme96->vol[0];
21861da177e4SLinus Torvalds         u->value.integer.value[1] = rme96->vol[1];
21871da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds         return 0;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds static int
snd_rme96_dac_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * u)2193a3aefd88STakashi Iwai snd_rme96_dac_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u)
21941da177e4SLinus Torvalds {
2195a3aefd88STakashi Iwai 	struct rme96 *rme96 = snd_kcontrol_chip(kcontrol);
21961da177e4SLinus Torvalds         int change = 0;
21974e98d6a7STakashi Iwai 	unsigned int vol, maxvol;
21981da177e4SLinus Torvalds 
21994e98d6a7STakashi Iwai 
22004e98d6a7STakashi Iwai 	if (!RME96_HAS_ANALOG_OUT(rme96))
22011da177e4SLinus Torvalds 		return -EINVAL;
22024e98d6a7STakashi Iwai 	maxvol = RME96_185X_MAX_OUT(rme96);
22031da177e4SLinus Torvalds 	spin_lock_irq(&rme96->lock);
22044e98d6a7STakashi Iwai 	vol = u->value.integer.value[0];
22054e98d6a7STakashi Iwai 	if (vol != rme96->vol[0] && vol <= maxvol) {
22064e98d6a7STakashi Iwai 		rme96->vol[0] = vol;
22071da177e4SLinus Torvalds 		change = 1;
22081da177e4SLinus Torvalds 	}
22094e98d6a7STakashi Iwai 	vol = u->value.integer.value[1];
22104e98d6a7STakashi Iwai 	if (vol != rme96->vol[1] && vol <= maxvol) {
22114e98d6a7STakashi Iwai 		rme96->vol[1] = vol;
22121da177e4SLinus Torvalds 		change = 1;
22131da177e4SLinus Torvalds 	}
22144e98d6a7STakashi Iwai 	if (change)
22151da177e4SLinus Torvalds 		snd_rme96_apply_dac_volume(rme96);
22161da177e4SLinus Torvalds 	spin_unlock_irq(&rme96->lock);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds         return change;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
2221b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_rme96_controls[] = {
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22241da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
22251da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_info,
22261da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_get,
22271da177e4SLinus Torvalds 	.put =		snd_rme96_control_spdif_put
22281da177e4SLinus Torvalds },
22291da177e4SLinus Torvalds {
22301da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
22311da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22321da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
22331da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_stream_info,
22341da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_stream_get,
22351da177e4SLinus Torvalds 	.put =		snd_rme96_control_spdif_stream_put
22361da177e4SLinus Torvalds },
22371da177e4SLinus Torvalds {
22381da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
223967ed4161SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22401da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
22411da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_mask_info,
22421da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_mask_get,
22431da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
22441da177e4SLinus Torvalds 			IEC958_AES0_PROFESSIONAL |
22451da177e4SLinus Torvalds 			IEC958_AES0_CON_EMPHASIS
22461da177e4SLinus Torvalds },
22471da177e4SLinus Torvalds {
22481da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
224967ed4161SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
22501da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
22511da177e4SLinus Torvalds 	.info =		snd_rme96_control_spdif_mask_info,
22521da177e4SLinus Torvalds 	.get =		snd_rme96_control_spdif_mask_get,
22531da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
22541da177e4SLinus Torvalds 			IEC958_AES0_PROFESSIONAL |
22551da177e4SLinus Torvalds 			IEC958_AES0_PRO_EMPHASIS
22561da177e4SLinus Torvalds },
22571da177e4SLinus Torvalds {
22581da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22591da177e4SLinus Torvalds 	.name =         "Input Connector",
22601da177e4SLinus Torvalds 	.info =         snd_rme96_info_inputtype_control,
22611da177e4SLinus Torvalds 	.get =          snd_rme96_get_inputtype_control,
22621da177e4SLinus Torvalds 	.put =          snd_rme96_put_inputtype_control
22631da177e4SLinus Torvalds },
22641da177e4SLinus Torvalds {
22651da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22661da177e4SLinus Torvalds 	.name =         "Loopback Input",
22671da177e4SLinus Torvalds 	.info =         snd_rme96_info_loopback_control,
22681da177e4SLinus Torvalds 	.get =          snd_rme96_get_loopback_control,
22691da177e4SLinus Torvalds 	.put =          snd_rme96_put_loopback_control
22701da177e4SLinus Torvalds },
22711da177e4SLinus Torvalds {
22721da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22731da177e4SLinus Torvalds 	.name =         "Sample Clock Source",
22741da177e4SLinus Torvalds 	.info =         snd_rme96_info_clockmode_control,
22751da177e4SLinus Torvalds 	.get =          snd_rme96_get_clockmode_control,
22761da177e4SLinus Torvalds 	.put =          snd_rme96_put_clockmode_control
22771da177e4SLinus Torvalds },
22781da177e4SLinus Torvalds {
22791da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22801da177e4SLinus Torvalds 	.name =         "Monitor Tracks",
22811da177e4SLinus Torvalds 	.info =         snd_rme96_info_montracks_control,
22821da177e4SLinus Torvalds 	.get =          snd_rme96_get_montracks_control,
22831da177e4SLinus Torvalds 	.put =          snd_rme96_put_montracks_control
22841da177e4SLinus Torvalds },
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22871da177e4SLinus Torvalds 	.name =         "Attenuation",
22881da177e4SLinus Torvalds 	.info =         snd_rme96_info_attenuation_control,
22891da177e4SLinus Torvalds 	.get =          snd_rme96_get_attenuation_control,
22901da177e4SLinus Torvalds 	.put =          snd_rme96_put_attenuation_control
22911da177e4SLinus Torvalds },
22921da177e4SLinus Torvalds {
22931da177e4SLinus Torvalds         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
22941da177e4SLinus Torvalds 	.name =         "DAC Playback Volume",
22951da177e4SLinus Torvalds 	.info =         snd_rme96_dac_volume_info,
22961da177e4SLinus Torvalds 	.get =          snd_rme96_dac_volume_get,
22971da177e4SLinus Torvalds 	.put =          snd_rme96_dac_volume_put
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds };
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds static int
snd_rme96_create_switches(struct snd_card * card,struct rme96 * rme96)2302a3aefd88STakashi Iwai snd_rme96_create_switches(struct snd_card *card,
2303a3aefd88STakashi Iwai 			  struct rme96 *rme96)
23041da177e4SLinus Torvalds {
23051da177e4SLinus Torvalds 	int idx, err;
2306a3aefd88STakashi Iwai 	struct snd_kcontrol *kctl;
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	for (idx = 0; idx < 7; idx++) {
2309da8eedb1STakashi Iwai 		kctl = snd_ctl_new1(&snd_rme96_controls[idx], rme96);
2310da8eedb1STakashi Iwai 		err = snd_ctl_add(card, kctl);
2311da8eedb1STakashi Iwai 		if (err < 0)
23121da177e4SLinus Torvalds 			return err;
23131da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
23141da177e4SLinus Torvalds 			rme96->spdif_ctl = kctl;
23151da177e4SLinus Torvalds 	}
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	if (RME96_HAS_ANALOG_OUT(rme96)) {
2318da8eedb1STakashi Iwai 		for (idx = 7; idx < 10; idx++) {
2319da8eedb1STakashi Iwai 			err = snd_ctl_add(card, snd_ctl_new1(&snd_rme96_controls[idx], rme96));
2320da8eedb1STakashi Iwai 			if (err < 0)
23211da177e4SLinus Torvalds 				return err;
23221da177e4SLinus Torvalds 		}
2323da8eedb1STakashi Iwai 	}
23241da177e4SLinus Torvalds 
23251da177e4SLinus Torvalds 	return 0;
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds /*
23291da177e4SLinus Torvalds  * Card initialisation
23301da177e4SLinus Torvalds  */
23311da177e4SLinus Torvalds 
2332a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
2333528ba522SKnut Petersen 
rme96_suspend(struct device * dev)2334a932be91STakashi Iwai static int rme96_suspend(struct device *dev)
2335528ba522SKnut Petersen {
2336a932be91STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2337528ba522SKnut Petersen 	struct rme96 *rme96 = card->private_data;
2338528ba522SKnut Petersen 
2339528ba522SKnut Petersen 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2340528ba522SKnut Petersen 
2341528ba522SKnut Petersen 	/* save capture & playback pointers */
2342528ba522SKnut Petersen 	rme96->playback_pointer = readl(rme96->iobase + RME96_IO_GET_PLAY_POS)
2343528ba522SKnut Petersen 				  & RME96_RCR_AUDIO_ADDR_MASK;
2344528ba522SKnut Petersen 	rme96->capture_pointer = readl(rme96->iobase + RME96_IO_GET_REC_POS)
2345528ba522SKnut Petersen 				 & RME96_RCR_AUDIO_ADDR_MASK;
2346528ba522SKnut Petersen 
2347528ba522SKnut Petersen 	/* save playback and capture buffers */
2348528ba522SKnut Petersen 	memcpy_fromio(rme96->playback_suspend_buffer,
2349528ba522SKnut Petersen 		      rme96->iobase + RME96_IO_PLAY_BUFFER, RME96_BUFFER_SIZE);
2350528ba522SKnut Petersen 	memcpy_fromio(rme96->capture_suspend_buffer,
2351528ba522SKnut Petersen 		      rme96->iobase + RME96_IO_REC_BUFFER, RME96_BUFFER_SIZE);
2352528ba522SKnut Petersen 
2353528ba522SKnut Petersen 	/* disable the DAC  */
2354528ba522SKnut Petersen 	rme96->areg &= ~RME96_AR_DAC_EN;
2355528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2356528ba522SKnut Petersen 	return 0;
2357528ba522SKnut Petersen }
2358528ba522SKnut Petersen 
rme96_resume(struct device * dev)2359a932be91STakashi Iwai static int rme96_resume(struct device *dev)
2360528ba522SKnut Petersen {
2361a932be91STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2362528ba522SKnut Petersen 	struct rme96 *rme96 = card->private_data;
2363528ba522SKnut Petersen 
2364528ba522SKnut Petersen 	/* reset playback and record buffer pointers */
2365528ba522SKnut Petersen 	writel(0, rme96->iobase + RME96_IO_SET_PLAY_POS
2366528ba522SKnut Petersen 		  + rme96->playback_pointer);
2367528ba522SKnut Petersen 	writel(0, rme96->iobase + RME96_IO_SET_REC_POS
2368528ba522SKnut Petersen 		  + rme96->capture_pointer);
2369528ba522SKnut Petersen 
2370528ba522SKnut Petersen 	/* restore playback and capture buffers */
2371528ba522SKnut Petersen 	memcpy_toio(rme96->iobase + RME96_IO_PLAY_BUFFER,
2372528ba522SKnut Petersen 		    rme96->playback_suspend_buffer, RME96_BUFFER_SIZE);
2373528ba522SKnut Petersen 	memcpy_toio(rme96->iobase + RME96_IO_REC_BUFFER,
2374528ba522SKnut Petersen 		    rme96->capture_suspend_buffer, RME96_BUFFER_SIZE);
2375528ba522SKnut Petersen 
2376528ba522SKnut Petersen 	/* reset the ADC */
2377528ba522SKnut Petersen 	writel(rme96->areg | RME96_AR_PD2,
2378528ba522SKnut Petersen 	       rme96->iobase + RME96_IO_ADDITIONAL_REG);
2379528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2380528ba522SKnut Petersen 
2381528ba522SKnut Petersen 	/* reset and enable DAC, restore analog volume */
2382528ba522SKnut Petersen 	snd_rme96_reset_dac(rme96);
2383528ba522SKnut Petersen 	rme96->areg |= RME96_AR_DAC_EN;
2384528ba522SKnut Petersen 	writel(rme96->areg, rme96->iobase + RME96_IO_ADDITIONAL_REG);
2385528ba522SKnut Petersen 	if (RME96_HAS_ANALOG_OUT(rme96)) {
2386528ba522SKnut Petersen 		usleep_range(3000, 10000);
2387528ba522SKnut Petersen 		snd_rme96_apply_dac_volume(rme96);
2388528ba522SKnut Petersen 	}
2389528ba522SKnut Petersen 
2390528ba522SKnut Petersen 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2391528ba522SKnut Petersen 
2392528ba522SKnut Petersen 	return 0;
2393528ba522SKnut Petersen }
2394528ba522SKnut Petersen 
2395a932be91STakashi Iwai static SIMPLE_DEV_PM_OPS(rme96_pm, rme96_suspend, rme96_resume);
2396a932be91STakashi Iwai #define RME96_PM_OPS	&rme96_pm
2397a932be91STakashi Iwai #else
2398a932be91STakashi Iwai #define RME96_PM_OPS	NULL
2399a932be91STakashi Iwai #endif /* CONFIG_PM_SLEEP */
2400528ba522SKnut Petersen 
snd_rme96_card_free(struct snd_card * card)2401a3aefd88STakashi Iwai static void snd_rme96_card_free(struct snd_card *card)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	snd_rme96_free(card->private_data);
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds 
2406e23e7a14SBill Pemberton static int
__snd_rme96_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)240793b884f8STakashi Iwai __snd_rme96_probe(struct pci_dev *pci,
24081da177e4SLinus Torvalds 		  const struct pci_device_id *pci_id)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	static int dev;
2411a3aefd88STakashi Iwai 	struct rme96 *rme96;
2412a3aefd88STakashi Iwai 	struct snd_card *card;
24131da177e4SLinus Torvalds 	int err;
24141da177e4SLinus Torvalds 	u8 val;
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS) {
24171da177e4SLinus Torvalds 		return -ENODEV;
24181da177e4SLinus Torvalds 	}
24191da177e4SLinus Torvalds 	if (!enable[dev]) {
24201da177e4SLinus Torvalds 		dev++;
24211da177e4SLinus Torvalds 		return -ENOENT;
24221da177e4SLinus Torvalds 	}
2423df06df7cSTakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2424df06df7cSTakashi Iwai 				sizeof(*rme96), &card);
2425e58de7baSTakashi Iwai 	if (err < 0)
2426e58de7baSTakashi Iwai 		return err;
24271da177e4SLinus Torvalds 	card->private_free = snd_rme96_card_free;
24289fe856e4SJoe Perches 	rme96 = card->private_data;
24291da177e4SLinus Torvalds 	rme96->card = card;
24301da177e4SLinus Torvalds 	rme96->pci = pci;
2431d72a8010SMarkus Elfring 	err = snd_rme96_create(rme96);
2432d72a8010SMarkus Elfring 	if (err)
2433df06df7cSTakashi Iwai 		return err;
24341da177e4SLinus Torvalds 
2435a932be91STakashi Iwai #ifdef CONFIG_PM_SLEEP
2436528ba522SKnut Petersen 	rme96->playback_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2437df06df7cSTakashi Iwai 	if (!rme96->playback_suspend_buffer)
2438df06df7cSTakashi Iwai 		return -ENOMEM;
2439528ba522SKnut Petersen 	rme96->capture_suspend_buffer = vmalloc(RME96_BUFFER_SIZE);
2440df06df7cSTakashi Iwai 	if (!rme96->capture_suspend_buffer)
2441df06df7cSTakashi Iwai 		return -ENOMEM;
2442528ba522SKnut Petersen #endif
2443528ba522SKnut Petersen 
24441da177e4SLinus Torvalds 	strcpy(card->driver, "Digi96");
24451da177e4SLinus Torvalds 	switch (rme96->pci->device) {
24468b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96:
24471da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96");
24481da177e4SLinus Torvalds 		break;
24498b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8:
24501da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8");
24511da177e4SLinus Torvalds 		break;
24528b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PRO:
24531da177e4SLinus Torvalds 		strcpy(card->shortname, "RME Digi96/8 PRO");
24541da177e4SLinus Torvalds 		break;
24558b7fc421SRoland Dreier 	case PCI_DEVICE_ID_RME_DIGI96_8_PAD_OR_PST:
24561da177e4SLinus Torvalds 		pci_read_config_byte(rme96->pci, 8, &val);
24571da177e4SLinus Torvalds 		if (val < 5) {
24581da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PAD");
24591da177e4SLinus Torvalds 		} else {
24601da177e4SLinus Torvalds 			strcpy(card->shortname, "RME Digi96/8 PST");
24611da177e4SLinus Torvalds 		}
24621da177e4SLinus Torvalds 		break;
24631da177e4SLinus Torvalds 	}
24641da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
24651da177e4SLinus Torvalds 		rme96->port, rme96->irq);
2466d72a8010SMarkus Elfring 	err = snd_card_register(card);
2467d72a8010SMarkus Elfring 	if (err)
2468df06df7cSTakashi Iwai 		return err;
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
24711da177e4SLinus Torvalds 	dev++;
24721da177e4SLinus Torvalds 	return 0;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
snd_rme96_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)247593b884f8STakashi Iwai static int snd_rme96_probe(struct pci_dev *pci,
247693b884f8STakashi Iwai 			   const struct pci_device_id *pci_id)
247793b884f8STakashi Iwai {
247893b884f8STakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_rme96_probe(pci, pci_id));
247993b884f8STakashi Iwai }
248093b884f8STakashi Iwai 
2481e9f66d9bSTakashi Iwai static struct pci_driver rme96_driver = {
24823733e424STakashi Iwai 	.name = KBUILD_MODNAME,
24831da177e4SLinus Torvalds 	.id_table = snd_rme96_ids,
24841da177e4SLinus Torvalds 	.probe = snd_rme96_probe,
2485a932be91STakashi Iwai 	.driver = {
2486a932be91STakashi Iwai 		.pm = RME96_PM_OPS,
2487a932be91STakashi Iwai 	},
24881da177e4SLinus Torvalds };
24891da177e4SLinus Torvalds 
2490e9f66d9bSTakashi Iwai module_pci_driver(rme96_driver);
2491