blob: 5b6bd9f0b2f77df56084e213f282f69020d351df [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ALSA driver for RME Digi32, Digi32/8 and Digi32 PRO audio interfaces
4 *
5 * Copyright (c) 2002-2004 Martin Langer <martin-langer@gmx.de>,
6 * Pilo Chambert <pilo.c@wanadoo.fr>
7 *
8 * Thanks to : Anders Torger <torger@ludd.luth.se>,
9 * Henk Hesselink <henk@anda.nl>
10 * for writing the digi96-driver
11 * and RME for all informations.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * ****************************************************************************
14 *
15 * Note #1 "Sek'd models" ................................... martin 2002-12-07
16 *
17 * Identical soundcards by Sek'd were labeled:
18 * RME Digi 32 = Sek'd Prodif 32
19 * RME Digi 32 Pro = Sek'd Prodif 96
20 * RME Digi 32/8 = Sek'd Prodif Gold
21 *
22 * ****************************************************************************
23 *
24 * Note #2 "full duplex mode" ............................... martin 2002-12-07
25 *
26 * Full duplex doesn't work. All cards (32, 32/8, 32Pro) are working identical
27 * in this mode. Rec data and play data are using the same buffer therefore. At
28 * first you have got the playing bits in the buffer and then (after playing
29 * them) they were overwitten by the captured sound of the CS8412/14. Both
30 * modes (play/record) are running harmonically hand in hand in the same buffer
31 * and you have only one start bit plus one interrupt bit to control this
32 * paired action.
33 * This is opposite to the latter rme96 where playing and capturing is totally
34 * separated and so their full duplex mode is supported by alsa (using two
35 * start bits and two interrupts for two different buffers).
36 * But due to the wrong sequence of playing and capturing ALSA shows no solved
37 * full duplex support for the rme32 at the moment. That's bad, but I'm not
38 * able to solve it. Are you motivated enough to solve this problem now? Your
39 * patch would be welcome!
40 *
41 * ****************************************************************************
42 *
43 * "The story after the long seeking" -- tiwai
44 *
45 * Ok, the situation regarding the full duplex is now improved a bit.
46 * In the fullduplex mode (given by the module parameter), the hardware buffer
47 * is split to halves for read and write directions at the DMA pointer.
48 * That is, the half above the current DMA pointer is used for write, and
49 * the half below is used for read. To mangle this strange behavior, an
50 * software intermediate buffer is introduced. This is, of course, not good
51 * from the viewpoint of the data transfer efficiency. However, this allows
52 * you to use arbitrary buffer sizes, instead of the fixed I/O buffer size.
53 *
54 * ****************************************************************************
55 */
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/init.h>
61#include <linux/interrupt.h>
62#include <linux/pci.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040063#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010064#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#include <sound/core.h>
67#include <sound/info.h>
68#include <sound/control.h>
69#include <sound/pcm.h>
70#include <sound/pcm_params.h>
71#include <sound/pcm-indirect.h>
72#include <sound/asoundef.h>
73#include <sound/initval.h>
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
76static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103077static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
78static bool fullduplex[SNDRV_CARDS]; // = {[0 ... (SNDRV_CARDS - 1)] = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80module_param_array(index, int, NULL, 0444);
81MODULE_PARM_DESC(index, "Index value for RME Digi32 soundcard.");
82module_param_array(id, charp, NULL, 0444);
83MODULE_PARM_DESC(id, "ID string for RME Digi32 soundcard.");
84module_param_array(enable, bool, NULL, 0444);
85MODULE_PARM_DESC(enable, "Enable RME Digi32 soundcard.");
86module_param_array(fullduplex, bool, NULL, 0444);
87MODULE_PARM_DESC(fullduplex, "Support full-duplex mode.");
88MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>, Pilo Chambert <pilo.c@wanadoo.fr>");
89MODULE_DESCRIPTION("RME Digi32, Digi32/8, Digi32 PRO");
90MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Defines for RME Digi32 series */
93#define RME32_SPDIF_NCHANNELS 2
94
95/* Playback and capture buffer size */
96#define RME32_BUFFER_SIZE 0x20000
97
98/* IO area size */
99#define RME32_IO_SIZE 0x30000
100
101/* IO area offsets */
102#define RME32_IO_DATA_BUFFER 0x0
103#define RME32_IO_CONTROL_REGISTER 0x20000
104#define RME32_IO_GET_POS 0x20000
105#define RME32_IO_CONFIRM_ACTION_IRQ 0x20004
106#define RME32_IO_RESET_POS 0x20100
107
108/* Write control register bits */
109#define RME32_WCR_START (1 << 0) /* startbit */
110#define RME32_WCR_MONO (1 << 1) /* 0=stereo, 1=mono
111 Setting the whole card to mono
112 doesn't seem to be very useful.
113 A software-solution can handle
114 full-duplex with one direction in
115 stereo and the other way in mono.
116 So, the hardware should work all
117 the time in stereo! */
118#define RME32_WCR_MODE24 (1 << 2) /* 0=16bit, 1=32bit */
119#define RME32_WCR_SEL (1 << 3) /* 0=input on output, 1=normal playback/capture */
120#define RME32_WCR_FREQ_0 (1 << 4) /* frequency (play) */
121#define RME32_WCR_FREQ_1 (1 << 5)
122#define RME32_WCR_INP_0 (1 << 6) /* input switch */
123#define RME32_WCR_INP_1 (1 << 7)
124#define RME32_WCR_RESET (1 << 8) /* Reset address */
125#define RME32_WCR_MUTE (1 << 9) /* digital mute for output */
126#define RME32_WCR_PRO (1 << 10) /* 1=professional, 0=consumer */
127#define RME32_WCR_DS_BM (1 << 11) /* 1=DoubleSpeed (only PRO-Version); 1=BlockMode (only Adat-Version) */
128#define RME32_WCR_ADAT (1 << 12) /* Adat Mode (only Adat-Version) */
129#define RME32_WCR_AUTOSYNC (1 << 13) /* AutoSync */
130#define RME32_WCR_PD (1 << 14) /* DAC Reset (only PRO-Version) */
131#define RME32_WCR_EMP (1 << 15) /* 1=Emphasis on (only PRO-Version) */
132
133#define RME32_WCR_BITPOS_FREQ_0 4
134#define RME32_WCR_BITPOS_FREQ_1 5
135#define RME32_WCR_BITPOS_INP_0 6
136#define RME32_WCR_BITPOS_INP_1 7
137
138/* Read control register bits */
139#define RME32_RCR_AUDIO_ADDR_MASK 0x1ffff
140#define RME32_RCR_LOCK (1 << 23) /* 1=locked, 0=not locked */
141#define RME32_RCR_ERF (1 << 26) /* 1=Error, 0=no Error */
142#define RME32_RCR_FREQ_0 (1 << 27) /* CS841x frequency (record) */
143#define RME32_RCR_FREQ_1 (1 << 28)
144#define RME32_RCR_FREQ_2 (1 << 29)
145#define RME32_RCR_KMODE (1 << 30) /* card mode: 1=PLL, 0=quartz */
146#define RME32_RCR_IRQ (1 << 31) /* interrupt */
147
148#define RME32_RCR_BITPOS_F0 27
149#define RME32_RCR_BITPOS_F1 28
150#define RME32_RCR_BITPOS_F2 29
151
152/* Input types */
153#define RME32_INPUT_OPTICAL 0
154#define RME32_INPUT_COAXIAL 1
155#define RME32_INPUT_INTERNAL 2
156#define RME32_INPUT_XLR 3
157
158/* Clock modes */
159#define RME32_CLOCKMODE_SLAVE 0
160#define RME32_CLOCKMODE_MASTER_32 1
161#define RME32_CLOCKMODE_MASTER_44 2
162#define RME32_CLOCKMODE_MASTER_48 3
163
164/* Block sizes in bytes */
165#define RME32_BLOCK_SIZE 8192
166
167/* Software intermediate buffer (max) size */
168#define RME32_MID_BUFFER_SIZE (1024*1024)
169
170/* Hardware revisions */
171#define RME32_32_REVISION 192
172#define RME32_328_REVISION_OLD 100
173#define RME32_328_REVISION_NEW 101
174#define RME32_PRO_REVISION_WITH_8412 192
175#define RME32_PRO_REVISION_WITH_8414 150
176
177
Takashi Iwai017ce802005-11-17 15:05:25 +0100178struct rme32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 spinlock_t lock;
180 int irq;
181 unsigned long port;
182 void __iomem *iobase;
183
184 u32 wcreg; /* cached write control register value */
185 u32 wcreg_spdif; /* S/PDIF setup */
186 u32 wcreg_spdif_stream; /* S/PDIF setup (temporary) */
187 u32 rcreg; /* cached read control register value */
188
189 u8 rev; /* card revision number */
190
Takashi Iwai017ce802005-11-17 15:05:25 +0100191 struct snd_pcm_substream *playback_substream;
192 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 int playback_frlog; /* log2 of framesize */
195 int capture_frlog;
196
197 size_t playback_periodsize; /* in bytes, zero if not used */
198 size_t capture_periodsize; /* in bytes, zero if not used */
199
200 unsigned int fullduplex_mode;
201 int running;
202
Takashi Iwai017ce802005-11-17 15:05:25 +0100203 struct snd_pcm_indirect playback_pcm;
204 struct snd_pcm_indirect capture_pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Takashi Iwai017ce802005-11-17 15:05:25 +0100206 struct snd_card *card;
207 struct snd_pcm *spdif_pcm;
208 struct snd_pcm *adat_pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct pci_dev *pci;
Takashi Iwai017ce802005-11-17 15:05:25 +0100210 struct snd_kcontrol *spdif_ctl;
211};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Benoit Taine9baa3c32014-08-08 15:56:03 +0200213static const struct pci_device_id snd_rme32_ids[] = {
Joe Perches28d27aa2009-06-24 22:13:35 -0700214 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32), 0,},
215 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_8), 0,},
216 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_PRO), 0,},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {0,}
218};
219
220MODULE_DEVICE_TABLE(pci, snd_rme32_ids);
221
222#define RME32_ISWORKING(rme32) ((rme32)->wcreg & RME32_WCR_START)
Roland Dreier8b7fc422005-09-14 14:19:17 -0700223#define RME32_PRO_WITH_8414(rme32) ((rme32)->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO && (rme32)->rev == RME32_PRO_REVISION_WITH_8414)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Takashi Iwai017ce802005-11-17 15:05:25 +0100225static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Takashi Iwai017ce802005-11-17 15:05:25 +0100227static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Takashi Iwai017ce802005-11-17 15:05:25 +0100229static int snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Takashi Iwai017ce802005-11-17 15:05:25 +0100231static void snd_rme32_proc_init(struct rme32 * rme32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Takashi Iwai017ce802005-11-17 15:05:25 +0100233static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Takashi Iwai017ce802005-11-17 15:05:25 +0100235static inline unsigned int snd_rme32_pcm_byteptr(struct rme32 * rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return (readl(rme32->iobase + RME32_IO_GET_POS)
238 & RME32_RCR_AUDIO_ADDR_MASK);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* silence callback for halfduplex mode */
Takashi Iwai032db752017-05-10 20:29:25 +0200242static int snd_rme32_playback_silence(struct snd_pcm_substream *substream,
243 int channel, unsigned long pos,
244 unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Takashi Iwai017ce802005-11-17 15:05:25 +0100246 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Takashi Iwai032db752017-05-10 20:29:25 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 memset_io(rme32->iobase + RME32_IO_DATA_BUFFER + pos, 0, count);
249 return 0;
250}
251
252/* copy callback for halfduplex mode */
Takashi Iwai032db752017-05-10 20:29:25 +0200253static int snd_rme32_playback_copy(struct snd_pcm_substream *substream,
254 int channel, unsigned long pos,
255 void __user *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Takashi Iwai017ce802005-11-17 15:05:25 +0100257 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Takashi Iwai032db752017-05-10 20:29:25 +0200258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (copy_from_user_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
Takashi Iwai032db752017-05-10 20:29:25 +0200260 src, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EFAULT;
262 return 0;
263}
264
Takashi Iwai032db752017-05-10 20:29:25 +0200265static int snd_rme32_playback_copy_kernel(struct snd_pcm_substream *substream,
266 int channel, unsigned long pos,
267 void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Takashi Iwai017ce802005-11-17 15:05:25 +0100269 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Takashi Iwai032db752017-05-10 20:29:25 +0200270
271 memcpy_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos, src, count);
272 return 0;
273}
274
275/* copy callback for halfduplex mode */
276static int snd_rme32_capture_copy(struct snd_pcm_substream *substream,
277 int channel, unsigned long pos,
278 void __user *dst, unsigned long count)
279{
280 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (copy_to_user_fromio(dst,
283 rme32->iobase + RME32_IO_DATA_BUFFER + pos,
284 count))
285 return -EFAULT;
286 return 0;
287}
288
Takashi Iwai032db752017-05-10 20:29:25 +0200289static int snd_rme32_capture_copy_kernel(struct snd_pcm_substream *substream,
290 int channel, unsigned long pos,
291 void *dst, unsigned long count)
292{
293 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
294
295 memcpy_fromio(dst, rme32->iobase + RME32_IO_DATA_BUFFER + pos, count);
296 return 0;
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800300 * SPDIF I/O capabilities (half-duplex mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Bhumika Goyaldee49892017-08-12 21:01:28 +0530302static const struct snd_pcm_hardware snd_rme32_spdif_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
304 SNDRV_PCM_INFO_MMAP_VALID |
305 SNDRV_PCM_INFO_INTERLEAVED |
306 SNDRV_PCM_INFO_PAUSE |
Takashi Iwai38ce57a2018-09-02 10:22:13 +0200307 SNDRV_PCM_INFO_SYNC_START |
308 SNDRV_PCM_INFO_SYNC_APPLPTR),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
310 SNDRV_PCM_FMTBIT_S32_LE),
311 .rates = (SNDRV_PCM_RATE_32000 |
312 SNDRV_PCM_RATE_44100 |
313 SNDRV_PCM_RATE_48000),
314 .rate_min = 32000,
315 .rate_max = 48000,
316 .channels_min = 2,
317 .channels_max = 2,
318 .buffer_bytes_max = RME32_BUFFER_SIZE,
319 .period_bytes_min = RME32_BLOCK_SIZE,
320 .period_bytes_max = RME32_BLOCK_SIZE,
321 .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
322 .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
323 .fifo_size = 0,
324};
325
326/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800327 * ADAT I/O capabilities (half-duplex mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Bhumika Goyaldee49892017-08-12 21:01:28 +0530329static const struct snd_pcm_hardware snd_rme32_adat_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
332 SNDRV_PCM_INFO_MMAP_VALID |
333 SNDRV_PCM_INFO_INTERLEAVED |
334 SNDRV_PCM_INFO_PAUSE |
Takashi Iwai38ce57a2018-09-02 10:22:13 +0200335 SNDRV_PCM_INFO_SYNC_START |
336 SNDRV_PCM_INFO_SYNC_APPLPTR),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .formats= SNDRV_PCM_FMTBIT_S16_LE,
338 .rates = (SNDRV_PCM_RATE_44100 |
339 SNDRV_PCM_RATE_48000),
340 .rate_min = 44100,
341 .rate_max = 48000,
342 .channels_min = 8,
343 .channels_max = 8,
344 .buffer_bytes_max = RME32_BUFFER_SIZE,
345 .period_bytes_min = RME32_BLOCK_SIZE,
346 .period_bytes_max = RME32_BLOCK_SIZE,
347 .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
348 .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
349 .fifo_size = 0,
350};
351
352/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800353 * SPDIF I/O capabilities (full-duplex mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Bhumika Goyaldee49892017-08-12 21:01:28 +0530355static const struct snd_pcm_hardware snd_rme32_spdif_fd_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 .info = (SNDRV_PCM_INFO_MMAP |
357 SNDRV_PCM_INFO_MMAP_VALID |
358 SNDRV_PCM_INFO_INTERLEAVED |
359 SNDRV_PCM_INFO_PAUSE |
Takashi Iwai38ce57a2018-09-02 10:22:13 +0200360 SNDRV_PCM_INFO_SYNC_START |
361 SNDRV_PCM_INFO_SYNC_APPLPTR),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
363 SNDRV_PCM_FMTBIT_S32_LE),
364 .rates = (SNDRV_PCM_RATE_32000 |
365 SNDRV_PCM_RATE_44100 |
366 SNDRV_PCM_RATE_48000),
367 .rate_min = 32000,
368 .rate_max = 48000,
369 .channels_min = 2,
370 .channels_max = 2,
371 .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
372 .period_bytes_min = RME32_BLOCK_SIZE,
373 .period_bytes_max = RME32_BLOCK_SIZE,
374 .periods_min = 2,
375 .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
376 .fifo_size = 0,
377};
378
379/*
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -0800380 * ADAT I/O capabilities (full-duplex mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Bhumika Goyaldee49892017-08-12 21:01:28 +0530382static const struct snd_pcm_hardware snd_rme32_adat_fd_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 .info = (SNDRV_PCM_INFO_MMAP |
385 SNDRV_PCM_INFO_MMAP_VALID |
386 SNDRV_PCM_INFO_INTERLEAVED |
387 SNDRV_PCM_INFO_PAUSE |
Takashi Iwai38ce57a2018-09-02 10:22:13 +0200388 SNDRV_PCM_INFO_SYNC_START |
389 SNDRV_PCM_INFO_SYNC_APPLPTR),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .formats= SNDRV_PCM_FMTBIT_S16_LE,
391 .rates = (SNDRV_PCM_RATE_44100 |
392 SNDRV_PCM_RATE_48000),
393 .rate_min = 44100,
394 .rate_max = 48000,
395 .channels_min = 8,
396 .channels_max = 8,
397 .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
398 .period_bytes_min = RME32_BLOCK_SIZE,
399 .period_bytes_max = RME32_BLOCK_SIZE,
400 .periods_min = 2,
401 .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
402 .fifo_size = 0,
403};
404
Takashi Iwai017ce802005-11-17 15:05:25 +0100405static void snd_rme32_reset_dac(struct rme32 *rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 writel(rme32->wcreg | RME32_WCR_PD,
408 rme32->iobase + RME32_IO_CONTROL_REGISTER);
409 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
410}
411
Takashi Iwai017ce802005-11-17 15:05:25 +0100412static int snd_rme32_playback_getrate(struct rme32 * rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int rate;
415
416 rate = ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
417 (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
418 switch (rate) {
419 case 1:
420 rate = 32000;
421 break;
422 case 2:
423 rate = 44100;
424 break;
425 case 3:
426 rate = 48000;
427 break;
428 default:
429 return -1;
430 }
431 return (rme32->wcreg & RME32_WCR_DS_BM) ? rate << 1 : rate;
432}
433
Takashi Iwai017ce802005-11-17 15:05:25 +0100434static int snd_rme32_capture_getrate(struct rme32 * rme32, int *is_adat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int n;
437
438 *is_adat = 0;
439 if (rme32->rcreg & RME32_RCR_LOCK) {
440 /* ADAT rate */
441 *is_adat = 1;
442 }
443 if (rme32->rcreg & RME32_RCR_ERF) {
444 return -1;
445 }
446
447 /* S/PDIF rate */
448 n = ((rme32->rcreg >> RME32_RCR_BITPOS_F0) & 1) +
449 (((rme32->rcreg >> RME32_RCR_BITPOS_F1) & 1) << 1) +
450 (((rme32->rcreg >> RME32_RCR_BITPOS_F2) & 1) << 2);
451
452 if (RME32_PRO_WITH_8414(rme32))
453 switch (n) { /* supporting the CS8414 */
454 case 0:
455 case 1:
456 case 2:
457 return -1;
458 case 3:
459 return 96000;
460 case 4:
461 return 88200;
462 case 5:
463 return 48000;
464 case 6:
465 return 44100;
466 case 7:
467 return 32000;
468 default:
469 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 else
472 switch (n) { /* supporting the CS8412 */
473 case 0:
474 return -1;
475 case 1:
476 return 48000;
477 case 2:
478 return 44100;
479 case 3:
480 return 32000;
481 case 4:
482 return 48000;
483 case 5:
484 return 44100;
485 case 6:
486 return 44056;
487 case 7:
488 return 32000;
489 default:
490 break;
491 }
492 return -1;
493}
494
Takashi Iwai017ce802005-11-17 15:05:25 +0100495static int snd_rme32_playback_setrate(struct rme32 * rme32, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 int ds;
498
499 ds = rme32->wcreg & RME32_WCR_DS_BM;
500 switch (rate) {
501 case 32000:
502 rme32->wcreg &= ~RME32_WCR_DS_BM;
503 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
504 ~RME32_WCR_FREQ_1;
505 break;
506 case 44100:
507 rme32->wcreg &= ~RME32_WCR_DS_BM;
508 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
509 ~RME32_WCR_FREQ_0;
510 break;
511 case 48000:
512 rme32->wcreg &= ~RME32_WCR_DS_BM;
513 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
514 RME32_WCR_FREQ_1;
515 break;
516 case 64000:
Roland Dreier8b7fc422005-09-14 14:19:17 -0700517 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EINVAL;
519 rme32->wcreg |= RME32_WCR_DS_BM;
520 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
521 ~RME32_WCR_FREQ_1;
522 break;
523 case 88200:
Roland Dreier8b7fc422005-09-14 14:19:17 -0700524 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EINVAL;
526 rme32->wcreg |= RME32_WCR_DS_BM;
527 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
528 ~RME32_WCR_FREQ_0;
529 break;
530 case 96000:
Roland Dreier8b7fc422005-09-14 14:19:17 -0700531 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EINVAL;
533 rme32->wcreg |= RME32_WCR_DS_BM;
534 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
535 RME32_WCR_FREQ_1;
536 break;
537 default:
538 return -EINVAL;
539 }
540 if ((!ds && rme32->wcreg & RME32_WCR_DS_BM) ||
541 (ds && !(rme32->wcreg & RME32_WCR_DS_BM)))
542 {
543 /* change to/from double-speed: reset the DAC (if available) */
544 snd_rme32_reset_dac(rme32);
545 } else {
546 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
547 }
548 return 0;
549}
550
Takashi Iwai017ce802005-11-17 15:05:25 +0100551static int snd_rme32_setclockmode(struct rme32 * rme32, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 switch (mode) {
554 case RME32_CLOCKMODE_SLAVE:
555 /* AutoSync */
556 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) &
557 ~RME32_WCR_FREQ_1;
558 break;
559 case RME32_CLOCKMODE_MASTER_32:
560 /* Internal 32.0kHz */
561 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
562 ~RME32_WCR_FREQ_1;
563 break;
564 case RME32_CLOCKMODE_MASTER_44:
565 /* Internal 44.1kHz */
566 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) |
567 RME32_WCR_FREQ_1;
568 break;
569 case RME32_CLOCKMODE_MASTER_48:
570 /* Internal 48.0kHz */
571 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
572 RME32_WCR_FREQ_1;
573 break;
574 default:
575 return -EINVAL;
576 }
577 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
578 return 0;
579}
580
Takashi Iwai017ce802005-11-17 15:05:25 +0100581static int snd_rme32_getclockmode(struct rme32 * rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 return ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
584 (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
585}
586
Takashi Iwai017ce802005-11-17 15:05:25 +0100587static int snd_rme32_setinputtype(struct rme32 * rme32, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 switch (type) {
590 case RME32_INPUT_OPTICAL:
591 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) &
592 ~RME32_WCR_INP_1;
593 break;
594 case RME32_INPUT_COAXIAL:
595 rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) &
596 ~RME32_WCR_INP_1;
597 break;
598 case RME32_INPUT_INTERNAL:
599 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) |
600 RME32_WCR_INP_1;
601 break;
602 case RME32_INPUT_XLR:
603 rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) |
604 RME32_WCR_INP_1;
605 break;
606 default:
607 return -EINVAL;
608 }
609 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
610 return 0;
611}
612
Takashi Iwai017ce802005-11-17 15:05:25 +0100613static int snd_rme32_getinputtype(struct rme32 * rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 return ((rme32->wcreg >> RME32_WCR_BITPOS_INP_0) & 1) +
616 (((rme32->wcreg >> RME32_WCR_BITPOS_INP_1) & 1) << 1);
617}
618
619static void
Takashi Iwai017ce802005-11-17 15:05:25 +0100620snd_rme32_setframelog(struct rme32 * rme32, int n_channels, int is_playback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 int frlog;
623
624 if (n_channels == 2) {
625 frlog = 1;
626 } else {
627 /* assume 8 channels */
628 frlog = 3;
629 }
630 if (is_playback) {
631 frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
632 rme32->playback_frlog = frlog;
633 } else {
634 frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
635 rme32->capture_frlog = frlog;
636 }
637}
638
Takashi Iwai6c869d32015-01-12 16:56:51 +0100639static int snd_rme32_setformat(struct rme32 *rme32, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 switch (format) {
642 case SNDRV_PCM_FORMAT_S16_LE:
643 rme32->wcreg &= ~RME32_WCR_MODE24;
644 break;
645 case SNDRV_PCM_FORMAT_S32_LE:
646 rme32->wcreg |= RME32_WCR_MODE24;
647 break;
648 default:
649 return -EINVAL;
650 }
651 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
652 return 0;
653}
654
655static int
Takashi Iwai017ce802005-11-17 15:05:25 +0100656snd_rme32_playback_hw_params(struct snd_pcm_substream *substream,
657 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int err, rate, dummy;
Takashi Iwai017ce802005-11-17 15:05:25 +0100660 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Takashi Iwai7ceebdc82019-12-09 10:49:23 +0100663 if (!rme32->fullduplex_mode) {
Clemens Ladisch4d233592005-09-05 10:35:20 +0200664 runtime->dma_area = (void __force *)(rme32->iobase +
665 RME32_IO_DATA_BUFFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
667 runtime->dma_bytes = RME32_BUFFER_SIZE;
668 }
669
670 spin_lock_irq(&rme32->lock);
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200671 rate = 0;
672 if (rme32->rcreg & RME32_RCR_KMODE)
673 rate = snd_rme32_capture_getrate(rme32, &dummy);
674 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* AutoSync */
676 if ((int)params_rate(params) != rate) {
677 spin_unlock_irq(&rme32->lock);
678 return -EIO;
679 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200680 } else {
681 err = snd_rme32_playback_setrate(rme32, params_rate(params));
682 if (err < 0) {
683 spin_unlock_irq(&rme32->lock);
684 return err;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200687 err = snd_rme32_setformat(rme32, params_format(params));
688 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 spin_unlock_irq(&rme32->lock);
690 return err;
691 }
692
693 snd_rme32_setframelog(rme32, params_channels(params), 1);
694 if (rme32->capture_periodsize != 0) {
695 if (params_period_size(params) << rme32->playback_frlog != rme32->capture_periodsize) {
696 spin_unlock_irq(&rme32->lock);
697 return -EBUSY;
698 }
699 }
700 rme32->playback_periodsize = params_period_size(params) << rme32->playback_frlog;
701 /* S/PDIF setup */
702 if ((rme32->wcreg & RME32_WCR_ADAT) == 0) {
703 rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
704 rme32->wcreg |= rme32->wcreg_spdif_stream;
705 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
706 }
707 spin_unlock_irq(&rme32->lock);
708
709 return 0;
710}
711
712static int
Takashi Iwai017ce802005-11-17 15:05:25 +0100713snd_rme32_capture_hw_params(struct snd_pcm_substream *substream,
714 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 int err, isadat, rate;
Takashi Iwai017ce802005-11-17 15:05:25 +0100717 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
718 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Takashi Iwai7ceebdc82019-12-09 10:49:23 +0100720 if (!rme32->fullduplex_mode) {
Clemens Ladisch4d233592005-09-05 10:35:20 +0200721 runtime->dma_area = (void __force *)rme32->iobase +
722 RME32_IO_DATA_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
724 runtime->dma_bytes = RME32_BUFFER_SIZE;
725 }
726
727 spin_lock_irq(&rme32->lock);
728 /* enable AutoSync for record-preparing */
729 rme32->wcreg |= RME32_WCR_AUTOSYNC;
730 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
731
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200732 err = snd_rme32_setformat(rme32, params_format(params));
733 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_unlock_irq(&rme32->lock);
735 return err;
736 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200737 err = snd_rme32_playback_setrate(rme32, params_rate(params));
738 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_unlock_irq(&rme32->lock);
740 return err;
741 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200742 rate = snd_rme32_capture_getrate(rme32, &isadat);
743 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if ((int)params_rate(params) != rate) {
745 spin_unlock_irq(&rme32->lock);
746 return -EIO;
747 }
748 if ((isadat && runtime->hw.channels_min == 2) ||
749 (!isadat && runtime->hw.channels_min == 8)) {
750 spin_unlock_irq(&rme32->lock);
751 return -EIO;
752 }
753 }
754 /* AutoSync off for recording */
755 rme32->wcreg &= ~RME32_WCR_AUTOSYNC;
756 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
757
758 snd_rme32_setframelog(rme32, params_channels(params), 0);
759 if (rme32->playback_periodsize != 0) {
760 if (params_period_size(params) << rme32->capture_frlog !=
761 rme32->playback_periodsize) {
762 spin_unlock_irq(&rme32->lock);
763 return -EBUSY;
764 }
765 }
766 rme32->capture_periodsize =
767 params_period_size(params) << rme32->capture_frlog;
768 spin_unlock_irq(&rme32->lock);
769
770 return 0;
771}
772
Takashi Iwai017ce802005-11-17 15:05:25 +0100773static void snd_rme32_pcm_start(struct rme32 * rme32, int from_pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 if (!from_pause) {
776 writel(0, rme32->iobase + RME32_IO_RESET_POS);
777 }
778
779 rme32->wcreg |= RME32_WCR_START;
780 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
781}
782
Takashi Iwai017ce802005-11-17 15:05:25 +0100783static void snd_rme32_pcm_stop(struct rme32 * rme32, int to_pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 /*
786 * Check if there is an unconfirmed IRQ, if so confirm it, or else
787 * the hardware will not stop generating interrupts
788 */
789 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
790 if (rme32->rcreg & RME32_RCR_IRQ) {
791 writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
792 }
793 rme32->wcreg &= ~RME32_WCR_START;
794 if (rme32->wcreg & RME32_WCR_SEL)
795 rme32->wcreg |= RME32_WCR_MUTE;
796 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
797 if (! to_pause)
798 writel(0, rme32->iobase + RME32_IO_RESET_POS);
799}
800
David Howells7d12e782006-10-05 14:55:46 +0100801static irqreturn_t snd_rme32_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Takashi Iwai017ce802005-11-17 15:05:25 +0100803 struct rme32 *rme32 = (struct rme32 *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
806 if (!(rme32->rcreg & RME32_RCR_IRQ)) {
807 return IRQ_NONE;
808 } else {
809 if (rme32->capture_substream) {
810 snd_pcm_period_elapsed(rme32->capture_substream);
811 }
812 if (rme32->playback_substream) {
813 snd_pcm_period_elapsed(rme32->playback_substream);
814 }
815 writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
816 }
817 return IRQ_HANDLED;
818}
819
Takashi Iwaia7f80092017-06-07 14:22:02 +0200820static const unsigned int period_bytes[] = { RME32_BLOCK_SIZE };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Takashi Iwaia7f80092017-06-07 14:22:02 +0200822static const struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .count = ARRAY_SIZE(period_bytes),
824 .list = period_bytes,
825 .mask = 0
826};
827
Takashi Iwai017ce802005-11-17 15:05:25 +0100828static void snd_rme32_set_buffer_constraint(struct rme32 *rme32, struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 if (! rme32->fullduplex_mode) {
Lars-Peter Clausen80ec8892015-10-18 15:39:19 +0200831 snd_pcm_hw_constraint_single(runtime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
Lars-Peter Clausen80ec8892015-10-18 15:39:19 +0200833 RME32_BUFFER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 snd_pcm_hw_constraint_list(runtime, 0,
835 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
836 &hw_constraints_period_bytes);
837 }
838}
839
Takashi Iwai017ce802005-11-17 15:05:25 +0100840static int snd_rme32_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 int rate, dummy;
Takashi Iwai017ce802005-11-17 15:05:25 +0100843 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
844 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 snd_pcm_set_sync(substream);
847
848 spin_lock_irq(&rme32->lock);
849 if (rme32->playback_substream != NULL) {
850 spin_unlock_irq(&rme32->lock);
851 return -EBUSY;
852 }
853 rme32->wcreg &= ~RME32_WCR_ADAT;
854 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
855 rme32->playback_substream = substream;
856 spin_unlock_irq(&rme32->lock);
857
858 if (rme32->fullduplex_mode)
859 runtime->hw = snd_rme32_spdif_fd_info;
860 else
861 runtime->hw = snd_rme32_spdif_info;
Roland Dreier8b7fc422005-09-14 14:19:17 -0700862 if (rme32->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 runtime->hw.rates |= SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
864 runtime->hw.rate_max = 96000;
865 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200866 rate = 0;
867 if (rme32->rcreg & RME32_RCR_KMODE)
868 rate = snd_rme32_capture_getrate(rme32, &dummy);
869 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* AutoSync */
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200871 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 runtime->hw.rate_min = rate;
873 runtime->hw.rate_max = rate;
874 }
875
876 snd_rme32_set_buffer_constraint(rme32, runtime);
877
878 rme32->wcreg_spdif_stream = rme32->wcreg_spdif;
879 rme32->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
880 snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
881 SNDRV_CTL_EVENT_MASK_INFO, &rme32->spdif_ctl->id);
882 return 0;
883}
884
Takashi Iwai017ce802005-11-17 15:05:25 +0100885static int snd_rme32_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 int isadat, rate;
Takashi Iwai017ce802005-11-17 15:05:25 +0100888 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
889 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 snd_pcm_set_sync(substream);
892
893 spin_lock_irq(&rme32->lock);
894 if (rme32->capture_substream != NULL) {
895 spin_unlock_irq(&rme32->lock);
896 return -EBUSY;
897 }
898 rme32->capture_substream = substream;
899 spin_unlock_irq(&rme32->lock);
900
901 if (rme32->fullduplex_mode)
902 runtime->hw = snd_rme32_spdif_fd_info;
903 else
904 runtime->hw = snd_rme32_spdif_info;
905 if (RME32_PRO_WITH_8414(rme32)) {
906 runtime->hw.rates |= SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
907 runtime->hw.rate_max = 96000;
908 }
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200909 rate = snd_rme32_capture_getrate(rme32, &isadat);
910 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (isadat) {
912 return -EIO;
913 }
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200914 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 runtime->hw.rate_min = rate;
916 runtime->hw.rate_max = rate;
917 }
918
919 snd_rme32_set_buffer_constraint(rme32, runtime);
920
921 return 0;
922}
923
924static int
Takashi Iwai017ce802005-11-17 15:05:25 +0100925snd_rme32_playback_adat_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 int rate, dummy;
Takashi Iwai017ce802005-11-17 15:05:25 +0100928 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
929 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 snd_pcm_set_sync(substream);
932
933 spin_lock_irq(&rme32->lock);
934 if (rme32->playback_substream != NULL) {
935 spin_unlock_irq(&rme32->lock);
936 return -EBUSY;
937 }
938 rme32->wcreg |= RME32_WCR_ADAT;
939 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
940 rme32->playback_substream = substream;
941 spin_unlock_irq(&rme32->lock);
942
943 if (rme32->fullduplex_mode)
944 runtime->hw = snd_rme32_adat_fd_info;
945 else
946 runtime->hw = snd_rme32_adat_info;
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200947 rate = 0;
948 if (rme32->rcreg & RME32_RCR_KMODE)
949 rate = snd_rme32_capture_getrate(rme32, &dummy);
950 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* AutoSync */
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200952 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 runtime->hw.rate_min = rate;
954 runtime->hw.rate_max = rate;
955 }
956
957 snd_rme32_set_buffer_constraint(rme32, runtime);
958 return 0;
959}
960
961static int
Takashi Iwai017ce802005-11-17 15:05:25 +0100962snd_rme32_capture_adat_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 int isadat, rate;
Takashi Iwai017ce802005-11-17 15:05:25 +0100965 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
966 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (rme32->fullduplex_mode)
969 runtime->hw = snd_rme32_adat_fd_info;
970 else
971 runtime->hw = snd_rme32_adat_info;
Takashi Iwai1c9140b2021-06-08 16:05:04 +0200972 rate = snd_rme32_capture_getrate(rme32, &isadat);
973 if (rate > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!isadat) {
975 return -EIO;
976 }
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200977 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 runtime->hw.rate_min = rate;
979 runtime->hw.rate_max = rate;
980 }
981
982 snd_pcm_set_sync(substream);
983
984 spin_lock_irq(&rme32->lock);
985 if (rme32->capture_substream != NULL) {
986 spin_unlock_irq(&rme32->lock);
987 return -EBUSY;
988 }
989 rme32->capture_substream = substream;
990 spin_unlock_irq(&rme32->lock);
991
992 snd_rme32_set_buffer_constraint(rme32, runtime);
993 return 0;
994}
995
Takashi Iwai017ce802005-11-17 15:05:25 +0100996static int snd_rme32_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Takashi Iwai017ce802005-11-17 15:05:25 +0100998 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int spdif = 0;
1000
1001 spin_lock_irq(&rme32->lock);
1002 rme32->playback_substream = NULL;
1003 rme32->playback_periodsize = 0;
1004 spdif = (rme32->wcreg & RME32_WCR_ADAT) == 0;
1005 spin_unlock_irq(&rme32->lock);
1006 if (spdif) {
1007 rme32->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1008 snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
1009 SNDRV_CTL_EVENT_MASK_INFO,
1010 &rme32->spdif_ctl->id);
1011 }
1012 return 0;
1013}
1014
Takashi Iwai017ce802005-11-17 15:05:25 +01001015static int snd_rme32_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Takashi Iwai017ce802005-11-17 15:05:25 +01001017 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 spin_lock_irq(&rme32->lock);
1020 rme32->capture_substream = NULL;
1021 rme32->capture_periodsize = 0;
Denis Efremovf49a59c2013-02-11 19:04:06 +04001022 spin_unlock_irq(&rme32->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024}
1025
Takashi Iwai017ce802005-11-17 15:05:25 +01001026static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Takashi Iwai017ce802005-11-17 15:05:25 +01001028 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 spin_lock_irq(&rme32->lock);
1031 if (rme32->fullduplex_mode) {
1032 memset(&rme32->playback_pcm, 0, sizeof(rme32->playback_pcm));
1033 rme32->playback_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
1034 rme32->playback_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1035 } else {
1036 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1037 }
1038 if (rme32->wcreg & RME32_WCR_SEL)
1039 rme32->wcreg &= ~RME32_WCR_MUTE;
1040 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1041 spin_unlock_irq(&rme32->lock);
1042 return 0;
1043}
1044
Takashi Iwai017ce802005-11-17 15:05:25 +01001045static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai017ce802005-11-17 15:05:25 +01001047 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 spin_lock_irq(&rme32->lock);
1050 if (rme32->fullduplex_mode) {
1051 memset(&rme32->capture_pcm, 0, sizeof(rme32->capture_pcm));
1052 rme32->capture_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
1053 rme32->capture_pcm.hw_queue_size = RME32_BUFFER_SIZE / 2;
1054 rme32->capture_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1055 } else {
1056 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1057 }
1058 spin_unlock_irq(&rme32->lock);
1059 return 0;
1060}
1061
1062static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001063snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Takashi Iwai017ce802005-11-17 15:05:25 +01001065 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Takashi Iwai017ce802005-11-17 15:05:25 +01001066 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 spin_lock(&rme32->lock);
Takashi Iwaief991b92007-02-22 12:52:53 +01001069 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (s != rme32->playback_substream &&
1071 s != rme32->capture_substream)
1072 continue;
1073 switch (cmd) {
1074 case SNDRV_PCM_TRIGGER_START:
1075 rme32->running |= (1 << s->stream);
1076 if (rme32->fullduplex_mode) {
1077 /* remember the current DMA position */
1078 if (s == rme32->playback_substream) {
1079 rme32->playback_pcm.hw_io =
1080 rme32->playback_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
1081 } else {
1082 rme32->capture_pcm.hw_io =
1083 rme32->capture_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
1084 }
1085 }
1086 break;
1087 case SNDRV_PCM_TRIGGER_STOP:
1088 rme32->running &= ~(1 << s->stream);
1089 break;
1090 }
1091 snd_pcm_trigger_done(s, substream);
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 switch (cmd) {
1095 case SNDRV_PCM_TRIGGER_START:
1096 if (rme32->running && ! RME32_ISWORKING(rme32))
1097 snd_rme32_pcm_start(rme32, 0);
1098 break;
1099 case SNDRV_PCM_TRIGGER_STOP:
1100 if (! rme32->running && RME32_ISWORKING(rme32))
1101 snd_rme32_pcm_stop(rme32, 0);
1102 break;
1103 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1104 if (rme32->running && RME32_ISWORKING(rme32))
1105 snd_rme32_pcm_stop(rme32, 1);
1106 break;
1107 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1108 if (rme32->running && ! RME32_ISWORKING(rme32))
1109 snd_rme32_pcm_start(rme32, 1);
1110 break;
1111 }
1112 spin_unlock(&rme32->lock);
1113 return 0;
1114}
1115
1116/* pointer callback for halfduplex mode */
1117static snd_pcm_uframes_t
Takashi Iwai017ce802005-11-17 15:05:25 +01001118snd_rme32_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Takashi Iwai017ce802005-11-17 15:05:25 +01001120 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return snd_rme32_pcm_byteptr(rme32) >> rme32->playback_frlog;
1122}
1123
1124static snd_pcm_uframes_t
Takashi Iwai017ce802005-11-17 15:05:25 +01001125snd_rme32_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwai017ce802005-11-17 15:05:25 +01001127 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return snd_rme32_pcm_byteptr(rme32) >> rme32->capture_frlog;
1129}
1130
1131
1132/* ack and pointer callbacks for fullduplex mode */
Takashi Iwai017ce802005-11-17 15:05:25 +01001133static void snd_rme32_pb_trans_copy(struct snd_pcm_substream *substream,
1134 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwai017ce802005-11-17 15:05:25 +01001136 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 memcpy_toio(rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
1138 substream->runtime->dma_area + rec->sw_data, bytes);
1139}
1140
Takashi Iwai017ce802005-11-17 15:05:25 +01001141static int snd_rme32_playback_fd_ack(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Takashi Iwai017ce802005-11-17 15:05:25 +01001143 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1144 struct snd_pcm_indirect *rec, *cprec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 rec = &rme32->playback_pcm;
1147 cprec = &rme32->capture_pcm;
1148 spin_lock(&rme32->lock);
1149 rec->hw_queue_size = RME32_BUFFER_SIZE;
1150 if (rme32->running & (1 << SNDRV_PCM_STREAM_CAPTURE))
1151 rec->hw_queue_size -= cprec->hw_ready;
1152 spin_unlock(&rme32->lock);
Takashi Iwai3e02c5a2017-05-19 18:49:45 +02001153 return snd_pcm_indirect_playback_transfer(substream, rec,
1154 snd_rme32_pb_trans_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Takashi Iwai017ce802005-11-17 15:05:25 +01001157static void snd_rme32_cp_trans_copy(struct snd_pcm_substream *substream,
1158 struct snd_pcm_indirect *rec, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Takashi Iwai017ce802005-11-17 15:05:25 +01001160 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 memcpy_fromio(substream->runtime->dma_area + rec->sw_data,
1162 rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
1163 bytes);
1164}
1165
Takashi Iwai017ce802005-11-17 15:05:25 +01001166static int snd_rme32_capture_fd_ack(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Takashi Iwai017ce802005-11-17 15:05:25 +01001168 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Takashi Iwai3e02c5a2017-05-19 18:49:45 +02001169 return snd_pcm_indirect_capture_transfer(substream, &rme32->capture_pcm,
1170 snd_rme32_cp_trans_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173static snd_pcm_uframes_t
Takashi Iwai017ce802005-11-17 15:05:25 +01001174snd_rme32_playback_fd_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwai017ce802005-11-17 15:05:25 +01001176 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return snd_pcm_indirect_playback_pointer(substream, &rme32->playback_pcm,
1178 snd_rme32_pcm_byteptr(rme32));
1179}
1180
1181static snd_pcm_uframes_t
Takashi Iwai017ce802005-11-17 15:05:25 +01001182snd_rme32_capture_fd_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Takashi Iwai017ce802005-11-17 15:05:25 +01001184 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return snd_pcm_indirect_capture_pointer(substream, &rme32->capture_pcm,
1186 snd_rme32_pcm_byteptr(rme32));
1187}
1188
1189/* for halfduplex mode */
Julia Lawall6769e9882016-09-02 00:13:10 +02001190static const struct snd_pcm_ops snd_rme32_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 .open = snd_rme32_playback_spdif_open,
1192 .close = snd_rme32_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 .hw_params = snd_rme32_playback_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 .prepare = snd_rme32_playback_prepare,
1195 .trigger = snd_rme32_pcm_trigger,
1196 .pointer = snd_rme32_playback_pointer,
Takashi Iwai032db752017-05-10 20:29:25 +02001197 .copy_user = snd_rme32_playback_copy,
1198 .copy_kernel = snd_rme32_playback_copy_kernel,
1199 .fill_silence = snd_rme32_playback_silence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .mmap = snd_pcm_lib_mmap_iomem,
1201};
1202
Julia Lawall6769e9882016-09-02 00:13:10 +02001203static const struct snd_pcm_ops snd_rme32_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 .open = snd_rme32_capture_spdif_open,
1205 .close = snd_rme32_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .hw_params = snd_rme32_capture_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .prepare = snd_rme32_capture_prepare,
1208 .trigger = snd_rme32_pcm_trigger,
1209 .pointer = snd_rme32_capture_pointer,
Takashi Iwai032db752017-05-10 20:29:25 +02001210 .copy_user = snd_rme32_capture_copy,
1211 .copy_kernel = snd_rme32_capture_copy_kernel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 .mmap = snd_pcm_lib_mmap_iomem,
1213};
1214
Julia Lawall6769e9882016-09-02 00:13:10 +02001215static const struct snd_pcm_ops snd_rme32_playback_adat_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .open = snd_rme32_playback_adat_open,
1217 .close = snd_rme32_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 .hw_params = snd_rme32_playback_hw_params,
1219 .prepare = snd_rme32_playback_prepare,
1220 .trigger = snd_rme32_pcm_trigger,
1221 .pointer = snd_rme32_playback_pointer,
Takashi Iwai032db752017-05-10 20:29:25 +02001222 .copy_user = snd_rme32_playback_copy,
1223 .copy_kernel = snd_rme32_playback_copy_kernel,
1224 .fill_silence = snd_rme32_playback_silence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 .mmap = snd_pcm_lib_mmap_iomem,
1226};
1227
Julia Lawall6769e9882016-09-02 00:13:10 +02001228static const struct snd_pcm_ops snd_rme32_capture_adat_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 .open = snd_rme32_capture_adat_open,
1230 .close = snd_rme32_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .hw_params = snd_rme32_capture_hw_params,
1232 .prepare = snd_rme32_capture_prepare,
1233 .trigger = snd_rme32_pcm_trigger,
1234 .pointer = snd_rme32_capture_pointer,
Takashi Iwai032db752017-05-10 20:29:25 +02001235 .copy_user = snd_rme32_capture_copy,
1236 .copy_kernel = snd_rme32_capture_copy_kernel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .mmap = snd_pcm_lib_mmap_iomem,
1238};
1239
1240/* for fullduplex mode */
Julia Lawall6769e9882016-09-02 00:13:10 +02001241static const struct snd_pcm_ops snd_rme32_playback_spdif_fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .open = snd_rme32_playback_spdif_open,
1243 .close = snd_rme32_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 .hw_params = snd_rme32_playback_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 .prepare = snd_rme32_playback_prepare,
1246 .trigger = snd_rme32_pcm_trigger,
1247 .pointer = snd_rme32_playback_fd_pointer,
1248 .ack = snd_rme32_playback_fd_ack,
1249};
1250
Julia Lawall6769e9882016-09-02 00:13:10 +02001251static const struct snd_pcm_ops snd_rme32_capture_spdif_fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .open = snd_rme32_capture_spdif_open,
1253 .close = snd_rme32_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 .hw_params = snd_rme32_capture_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 .prepare = snd_rme32_capture_prepare,
1256 .trigger = snd_rme32_pcm_trigger,
1257 .pointer = snd_rme32_capture_fd_pointer,
1258 .ack = snd_rme32_capture_fd_ack,
1259};
1260
Julia Lawall6769e9882016-09-02 00:13:10 +02001261static const struct snd_pcm_ops snd_rme32_playback_adat_fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .open = snd_rme32_playback_adat_open,
1263 .close = snd_rme32_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 .hw_params = snd_rme32_playback_hw_params,
1265 .prepare = snd_rme32_playback_prepare,
1266 .trigger = snd_rme32_pcm_trigger,
1267 .pointer = snd_rme32_playback_fd_pointer,
1268 .ack = snd_rme32_playback_fd_ack,
1269};
1270
Julia Lawall6769e9882016-09-02 00:13:10 +02001271static const struct snd_pcm_ops snd_rme32_capture_adat_fd_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 .open = snd_rme32_capture_adat_open,
1273 .close = snd_rme32_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 .hw_params = snd_rme32_capture_hw_params,
1275 .prepare = snd_rme32_capture_prepare,
1276 .trigger = snd_rme32_pcm_trigger,
1277 .pointer = snd_rme32_capture_fd_pointer,
1278 .ack = snd_rme32_capture_fd_ack,
1279};
1280
Takashi Iwai102e6152021-07-15 09:58:43 +02001281static void snd_rme32_free(struct rme32 *rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai102e6152021-07-15 09:58:43 +02001283 if (rme32->irq >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 snd_rme32_pcm_stop(rme32, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Takashi Iwai017ce802005-11-17 15:05:25 +01001287static void snd_rme32_free_spdif_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai017ce802005-11-17 15:05:25 +01001289 struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 rme32->spdif_pcm = NULL;
1291}
1292
1293static void
Takashi Iwai017ce802005-11-17 15:05:25 +01001294snd_rme32_free_adat_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwai017ce802005-11-17 15:05:25 +01001296 struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 rme32->adat_pcm = NULL;
1298}
1299
Bill Pembertone23e7a12012-12-06 12:35:10 -05001300static int snd_rme32_create(struct rme32 *rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct pci_dev *pci = rme32->pci;
1303 int err;
1304
1305 rme32->irq = -1;
1306 spin_lock_init(&rme32->lock);
1307
Takashi Iwai102e6152021-07-15 09:58:43 +02001308 err = pcim_enable_device(pci);
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001309 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return err;
1311
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001312 err = pci_request_regions(pci, "RME32");
1313 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return err;
1315 rme32->port = pci_resource_start(rme32->pci, 0);
1316
Takashi Iwai102e6152021-07-15 09:58:43 +02001317 rme32->iobase = devm_ioremap(&pci->dev, rme32->port, RME32_IO_SIZE);
Harvey Harrison4db9e4f2008-02-28 11:57:23 +01001318 if (!rme32->iobase) {
Takashi Iwai03952a32014-02-25 14:57:03 +01001319 dev_err(rme32->card->dev,
1320 "unable to remap memory region 0x%lx-0x%lx\n",
Takashi Iwai102e6152021-07-15 09:58:43 +02001321 rme32->port, rme32->port + RME32_IO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -ENOMEM;
1323 }
1324
Takashi Iwai102e6152021-07-15 09:58:43 +02001325 if (devm_request_irq(&pci->dev, pci->irq, snd_rme32_interrupt,
1326 IRQF_SHARED, KBUILD_MODNAME, rme32)) {
Takashi Iwai03952a32014-02-25 14:57:03 +01001327 dev_err(rme32->card->dev, "unable to grab IRQ %d\n", pci->irq);
Takashi Iwai688956f22006-06-06 15:44:34 +02001328 return -EBUSY;
1329 }
1330 rme32->irq = pci->irq;
Takashi Iwai1dad75b2019-12-10 07:34:30 +01001331 rme32->card->sync_irq = rme32->irq;
Takashi Iwai688956f22006-06-06 15:44:34 +02001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /* read the card's revision number */
1334 pci_read_config_byte(pci, 8, &rme32->rev);
1335
1336 /* set up ALSA pcm device for S/PDIF */
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001337 err = snd_pcm_new(rme32->card, "Digi32 IEC958", 0, 1, 1, &rme32->spdif_pcm);
1338 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rme32->spdif_pcm->private_data = rme32;
1341 rme32->spdif_pcm->private_free = snd_rme32_free_spdif_pcm;
1342 strcpy(rme32->spdif_pcm->name, "Digi32 IEC958");
1343 if (rme32->fullduplex_mode) {
1344 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1345 &snd_rme32_playback_spdif_fd_ops);
1346 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
1347 &snd_rme32_capture_spdif_fd_ops);
Takashi Iwai7ceebdc82019-12-09 10:49:23 +01001348 snd_pcm_set_managed_buffer_all(rme32->spdif_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1349 NULL, 0, RME32_MID_BUFFER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1351 } else {
1352 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1353 &snd_rme32_playback_spdif_ops);
1354 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
1355 &snd_rme32_capture_spdif_ops);
1356 rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
1357 }
1358
1359 /* set up ALSA pcm device for ADAT */
Roland Dreier8b7fc422005-09-14 14:19:17 -07001360 if ((pci->device == PCI_DEVICE_ID_RME_DIGI32) ||
1361 (pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* ADAT is not available on DIGI32 and DIGI32 Pro */
1363 rme32->adat_pcm = NULL;
1364 }
1365 else {
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001366 err = snd_pcm_new(rme32->card, "Digi32 ADAT", 1,
1367 1, 1, &rme32->adat_pcm);
1368 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 rme32->adat_pcm->private_data = rme32;
1371 rme32->adat_pcm->private_free = snd_rme32_free_adat_pcm;
1372 strcpy(rme32->adat_pcm->name, "Digi32 ADAT");
1373 if (rme32->fullduplex_mode) {
1374 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1375 &snd_rme32_playback_adat_fd_ops);
1376 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
1377 &snd_rme32_capture_adat_fd_ops);
Takashi Iwai7ceebdc82019-12-09 10:49:23 +01001378 snd_pcm_set_managed_buffer_all(rme32->adat_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1379 NULL,
1380 0, RME32_MID_BUFFER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1382 } else {
1383 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1384 &snd_rme32_playback_adat_ops);
1385 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
1386 &snd_rme32_capture_adat_ops);
1387 rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
1388 }
1389 }
1390
1391
1392 rme32->playback_periodsize = 0;
1393 rme32->capture_periodsize = 0;
1394
1395 /* make sure playback/capture is stopped, if by some reason active */
1396 snd_rme32_pcm_stop(rme32, 0);
1397
1398 /* reset DAC */
1399 snd_rme32_reset_dac(rme32);
1400
1401 /* reset buffer pointer */
1402 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1403
1404 /* set default values in registers */
1405 rme32->wcreg = RME32_WCR_SEL | /* normal playback */
1406 RME32_WCR_INP_0 | /* input select */
1407 RME32_WCR_MUTE; /* muting on */
1408 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1409
1410
1411 /* init switch interface */
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001412 err = snd_rme32_create_switches(rme32->card, rme32);
1413 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* init proc interface */
1417 snd_rme32_proc_init(rme32);
1418
1419 rme32->capture_substream = NULL;
1420 rme32->playback_substream = NULL;
1421
1422 return 0;
1423}
1424
1425/*
1426 * proc interface
1427 */
1428
1429static void
Takashi Iwai017ce802005-11-17 15:05:25 +01001430snd_rme32_proc_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 int n;
Takashi Iwai017ce802005-11-17 15:05:25 +01001433 struct rme32 *rme32 = (struct rme32 *) entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
1436
1437 snd_iprintf(buffer, rme32->card->longname);
1438 snd_iprintf(buffer, " (index #%d)\n", rme32->card->number + 1);
1439
1440 snd_iprintf(buffer, "\nGeneral settings\n");
1441 if (rme32->fullduplex_mode)
1442 snd_iprintf(buffer, " Full-duplex mode\n");
1443 else
1444 snd_iprintf(buffer, " Half-duplex mode\n");
1445 if (RME32_PRO_WITH_8414(rme32)) {
1446 snd_iprintf(buffer, " receiver: CS8414\n");
1447 } else {
1448 snd_iprintf(buffer, " receiver: CS8412\n");
1449 }
1450 if (rme32->wcreg & RME32_WCR_MODE24) {
1451 snd_iprintf(buffer, " format: 24 bit");
1452 } else {
1453 snd_iprintf(buffer, " format: 16 bit");
1454 }
1455 if (rme32->wcreg & RME32_WCR_MONO) {
1456 snd_iprintf(buffer, ", Mono\n");
1457 } else {
1458 snd_iprintf(buffer, ", Stereo\n");
1459 }
1460
1461 snd_iprintf(buffer, "\nInput settings\n");
1462 switch (snd_rme32_getinputtype(rme32)) {
1463 case RME32_INPUT_OPTICAL:
1464 snd_iprintf(buffer, " input: optical");
1465 break;
1466 case RME32_INPUT_COAXIAL:
1467 snd_iprintf(buffer, " input: coaxial");
1468 break;
1469 case RME32_INPUT_INTERNAL:
1470 snd_iprintf(buffer, " input: internal");
1471 break;
1472 case RME32_INPUT_XLR:
1473 snd_iprintf(buffer, " input: XLR");
1474 break;
1475 }
1476 if (snd_rme32_capture_getrate(rme32, &n) < 0) {
1477 snd_iprintf(buffer, "\n sample rate: no valid signal\n");
1478 } else {
1479 if (n) {
1480 snd_iprintf(buffer, " (8 channels)\n");
1481 } else {
1482 snd_iprintf(buffer, " (2 channels)\n");
1483 }
1484 snd_iprintf(buffer, " sample rate: %d Hz\n",
1485 snd_rme32_capture_getrate(rme32, &n));
1486 }
1487
1488 snd_iprintf(buffer, "\nOutput settings\n");
1489 if (rme32->wcreg & RME32_WCR_SEL) {
1490 snd_iprintf(buffer, " output signal: normal playback");
1491 } else {
1492 snd_iprintf(buffer, " output signal: same as input");
1493 }
1494 if (rme32->wcreg & RME32_WCR_MUTE) {
1495 snd_iprintf(buffer, " (muted)\n");
1496 } else {
1497 snd_iprintf(buffer, "\n");
1498 }
1499
1500 /* master output frequency */
1501 if (!
1502 ((!(rme32->wcreg & RME32_WCR_FREQ_0))
1503 && (!(rme32->wcreg & RME32_WCR_FREQ_1)))) {
1504 snd_iprintf(buffer, " sample rate: %d Hz\n",
1505 snd_rme32_playback_getrate(rme32));
1506 }
1507 if (rme32->rcreg & RME32_RCR_KMODE) {
1508 snd_iprintf(buffer, " sample clock source: AutoSync\n");
1509 } else {
1510 snd_iprintf(buffer, " sample clock source: Internal\n");
1511 }
1512 if (rme32->wcreg & RME32_WCR_PRO) {
1513 snd_iprintf(buffer, " format: AES/EBU (professional)\n");
1514 } else {
1515 snd_iprintf(buffer, " format: IEC958 (consumer)\n");
1516 }
1517 if (rme32->wcreg & RME32_WCR_EMP) {
1518 snd_iprintf(buffer, " emphasis: on\n");
1519 } else {
1520 snd_iprintf(buffer, " emphasis: off\n");
1521 }
1522}
1523
Bill Pembertone23e7a12012-12-06 12:35:10 -05001524static void snd_rme32_proc_init(struct rme32 *rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Takashi Iwai47f27692019-02-04 16:01:39 +01001526 snd_card_ro_proc_new(rme32->card, "rme32", rme32, snd_rme32_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529/*
1530 * control interface
1531 */
1532
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001533#define snd_rme32_info_loopback_control snd_ctl_boolean_mono_info
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001536snd_rme32_get_loopback_control(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Takashi Iwai017ce802005-11-17 15:05:25 +01001539 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 spin_lock_irq(&rme32->lock);
1542 ucontrol->value.integer.value[0] =
1543 rme32->wcreg & RME32_WCR_SEL ? 0 : 1;
1544 spin_unlock_irq(&rme32->lock);
1545 return 0;
1546}
1547static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001548snd_rme32_put_loopback_control(struct snd_kcontrol *kcontrol,
1549 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Takashi Iwai017ce802005-11-17 15:05:25 +01001551 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned int val;
1553 int change;
1554
1555 val = ucontrol->value.integer.value[0] ? 0 : RME32_WCR_SEL;
1556 spin_lock_irq(&rme32->lock);
1557 val = (rme32->wcreg & ~RME32_WCR_SEL) | val;
1558 change = val != rme32->wcreg;
1559 if (ucontrol->value.integer.value[0])
1560 val &= ~RME32_WCR_MUTE;
1561 else
1562 val |= RME32_WCR_MUTE;
1563 rme32->wcreg = val;
1564 writel(val, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1565 spin_unlock_irq(&rme32->lock);
1566 return change;
1567}
1568
1569static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001570snd_rme32_info_inputtype_control(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Takashi Iwai017ce802005-11-17 15:05:25 +01001573 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001574 static const char * const texts[4] = {
1575 "Optical", "Coaxial", "Internal", "XLR"
1576 };
1577 int num_items;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 switch (rme32->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001580 case PCI_DEVICE_ID_RME_DIGI32:
1581 case PCI_DEVICE_ID_RME_DIGI32_8:
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001582 num_items = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001584 case PCI_DEVICE_ID_RME_DIGI32_PRO:
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001585 num_items = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587 default:
1588 snd_BUG();
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001591 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001594snd_rme32_get_inputtype_control(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwai017ce802005-11-17 15:05:25 +01001597 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 unsigned int items = 3;
1599
1600 spin_lock_irq(&rme32->lock);
1601 ucontrol->value.enumerated.item[0] = snd_rme32_getinputtype(rme32);
1602
1603 switch (rme32->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001604 case PCI_DEVICE_ID_RME_DIGI32:
1605 case PCI_DEVICE_ID_RME_DIGI32_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 items = 3;
1607 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001608 case PCI_DEVICE_ID_RME_DIGI32_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 items = 4;
1610 break;
1611 default:
1612 snd_BUG();
1613 break;
1614 }
1615 if (ucontrol->value.enumerated.item[0] >= items) {
1616 ucontrol->value.enumerated.item[0] = items - 1;
1617 }
1618
1619 spin_unlock_irq(&rme32->lock);
1620 return 0;
1621}
1622static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001623snd_rme32_put_inputtype_control(struct snd_kcontrol *kcontrol,
1624 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwai017ce802005-11-17 15:05:25 +01001626 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 unsigned int val;
1628 int change, items = 3;
1629
1630 switch (rme32->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001631 case PCI_DEVICE_ID_RME_DIGI32:
1632 case PCI_DEVICE_ID_RME_DIGI32_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 items = 3;
1634 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001635 case PCI_DEVICE_ID_RME_DIGI32_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 items = 4;
1637 break;
1638 default:
1639 snd_BUG();
1640 break;
1641 }
1642 val = ucontrol->value.enumerated.item[0] % items;
1643
1644 spin_lock_irq(&rme32->lock);
1645 change = val != (unsigned int)snd_rme32_getinputtype(rme32);
1646 snd_rme32_setinputtype(rme32, val);
1647 spin_unlock_irq(&rme32->lock);
1648 return change;
1649}
1650
1651static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001652snd_rme32_info_clockmode_control(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001655 static const char * const texts[4] = { "AutoSync",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 "Internal 32.0kHz",
1657 "Internal 44.1kHz",
1658 "Internal 48.0kHz" };
1659
Takashi Iwai11c6ef72014-10-20 18:19:12 +02001660 return snd_ctl_enum_info(uinfo, 1, 4, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001663snd_rme32_get_clockmode_control(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Takashi Iwai017ce802005-11-17 15:05:25 +01001666 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 spin_lock_irq(&rme32->lock);
1669 ucontrol->value.enumerated.item[0] = snd_rme32_getclockmode(rme32);
1670 spin_unlock_irq(&rme32->lock);
1671 return 0;
1672}
1673static int
Takashi Iwai017ce802005-11-17 15:05:25 +01001674snd_rme32_put_clockmode_control(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Takashi Iwai017ce802005-11-17 15:05:25 +01001677 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 unsigned int val;
1679 int change;
1680
1681 val = ucontrol->value.enumerated.item[0] % 3;
1682 spin_lock_irq(&rme32->lock);
1683 change = val != (unsigned int)snd_rme32_getclockmode(rme32);
1684 snd_rme32_setclockmode(rme32, val);
1685 spin_unlock_irq(&rme32->lock);
1686 return change;
1687}
1688
Takashi Iwai017ce802005-11-17 15:05:25 +01001689static u32 snd_rme32_convert_from_aes(struct snd_aes_iec958 * aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 u32 val = 0;
1692 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME32_WCR_PRO : 0;
1693 if (val & RME32_WCR_PRO)
1694 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
1695 else
1696 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
1697 return val;
1698}
1699
Takashi Iwai017ce802005-11-17 15:05:25 +01001700static void snd_rme32_convert_to_aes(struct snd_aes_iec958 * aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 aes->status[0] = ((val & RME32_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0);
1703 if (val & RME32_WCR_PRO)
1704 aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1705 else
1706 aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1707}
1708
Takashi Iwai017ce802005-11-17 15:05:25 +01001709static int snd_rme32_control_spdif_info(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1713 uinfo->count = 1;
1714 return 0;
1715}
1716
Takashi Iwai017ce802005-11-17 15:05:25 +01001717static int snd_rme32_control_spdif_get(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai017ce802005-11-17 15:05:25 +01001720 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 snd_rme32_convert_to_aes(&ucontrol->value.iec958,
1723 rme32->wcreg_spdif);
1724 return 0;
1725}
1726
Takashi Iwai017ce802005-11-17 15:05:25 +01001727static int snd_rme32_control_spdif_put(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Takashi Iwai017ce802005-11-17 15:05:25 +01001730 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int change;
1732 u32 val;
1733
1734 val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
1735 spin_lock_irq(&rme32->lock);
1736 change = val != rme32->wcreg_spdif;
1737 rme32->wcreg_spdif = val;
1738 spin_unlock_irq(&rme32->lock);
1739 return change;
1740}
1741
Takashi Iwai017ce802005-11-17 15:05:25 +01001742static int snd_rme32_control_spdif_stream_info(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1746 uinfo->count = 1;
1747 return 0;
1748}
1749
Takashi Iwai017ce802005-11-17 15:05:25 +01001750static int snd_rme32_control_spdif_stream_get(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ucontrol)
1753{
Takashi Iwai017ce802005-11-17 15:05:25 +01001754 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 snd_rme32_convert_to_aes(&ucontrol->value.iec958,
1757 rme32->wcreg_spdif_stream);
1758 return 0;
1759}
1760
Takashi Iwai017ce802005-11-17 15:05:25 +01001761static int snd_rme32_control_spdif_stream_put(struct snd_kcontrol *kcontrol,
1762 struct snd_ctl_elem_value *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ucontrol)
1764{
Takashi Iwai017ce802005-11-17 15:05:25 +01001765 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int change;
1767 u32 val;
1768
1769 val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
1770 spin_lock_irq(&rme32->lock);
1771 change = val != rme32->wcreg_spdif_stream;
1772 rme32->wcreg_spdif_stream = val;
1773 rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
1774 rme32->wcreg |= val;
1775 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1776 spin_unlock_irq(&rme32->lock);
1777 return change;
1778}
1779
Takashi Iwai017ce802005-11-17 15:05:25 +01001780static int snd_rme32_control_spdif_mask_info(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1784 uinfo->count = 1;
1785 return 0;
1786}
1787
Takashi Iwai017ce802005-11-17 15:05:25 +01001788static int snd_rme32_control_spdif_mask_get(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ucontrol)
1791{
1792 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1793 return 0;
1794}
1795
Takashi Iwaib4e5e702020-01-03 09:16:53 +01001796static const struct snd_kcontrol_new snd_rme32_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 {
1798 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1799 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1800 .info = snd_rme32_control_spdif_info,
1801 .get = snd_rme32_control_spdif_get,
1802 .put = snd_rme32_control_spdif_put
1803 },
1804 {
1805 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1806 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1807 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
1808 .info = snd_rme32_control_spdif_stream_info,
1809 .get = snd_rme32_control_spdif_stream_get,
1810 .put = snd_rme32_control_spdif_stream_put
1811 },
1812 {
1813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001814 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1816 .info = snd_rme32_control_spdif_mask_info,
1817 .get = snd_rme32_control_spdif_mask_get,
1818 .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_CON_EMPHASIS
1819 },
1820 {
1821 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001822 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1824 .info = snd_rme32_control_spdif_mask_info,
1825 .get = snd_rme32_control_spdif_mask_get,
1826 .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_EMPHASIS
1827 },
1828 {
1829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1830 .name = "Input Connector",
1831 .info = snd_rme32_info_inputtype_control,
1832 .get = snd_rme32_get_inputtype_control,
1833 .put = snd_rme32_put_inputtype_control
1834 },
1835 {
1836 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1837 .name = "Loopback Input",
1838 .info = snd_rme32_info_loopback_control,
1839 .get = snd_rme32_get_loopback_control,
1840 .put = snd_rme32_put_loopback_control
1841 },
1842 {
1843 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1844 .name = "Sample Clock Source",
1845 .info = snd_rme32_info_clockmode_control,
1846 .get = snd_rme32_get_clockmode_control,
1847 .put = snd_rme32_put_clockmode_control
1848 }
1849};
1850
Takashi Iwai017ce802005-11-17 15:05:25 +01001851static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 int idx, err;
Takashi Iwai017ce802005-11-17 15:05:25 +01001854 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 for (idx = 0; idx < (int)ARRAY_SIZE(snd_rme32_controls); idx++) {
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001857 kctl = snd_ctl_new1(&snd_rme32_controls[idx], rme32);
1858 err = snd_ctl_add(card, kctl);
1859 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return err;
1861 if (idx == 1) /* IEC958 (S/PDIF) Stream */
1862 rme32->spdif_ctl = kctl;
1863 }
1864
1865 return 0;
1866}
1867
1868/*
1869 * Card initialisation
1870 */
1871
Takashi Iwai017ce802005-11-17 15:05:25 +01001872static void snd_rme32_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 snd_rme32_free(card->private_data);
1875}
1876
Bill Pembertone23e7a12012-12-06 12:35:10 -05001877static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878snd_rme32_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1879{
1880 static int dev;
Takashi Iwai017ce802005-11-17 15:05:25 +01001881 struct rme32 *rme32;
1882 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int err;
1884
1885 if (dev >= SNDRV_CARDS) {
1886 return -ENODEV;
1887 }
1888 if (!enable[dev]) {
1889 dev++;
1890 return -ENOENT;
1891 }
1892
Takashi Iwai102e6152021-07-15 09:58:43 +02001893 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1894 sizeof(*rme32), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01001895 if (err < 0)
1896 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 card->private_free = snd_rme32_card_free;
Takashi Iwai017ce802005-11-17 15:05:25 +01001898 rme32 = (struct rme32 *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rme32->card = card;
1900 rme32->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (fullduplex[dev])
1902 rme32->fullduplex_mode = 1;
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001903 err = snd_rme32_create(rme32);
Takashi Iwai102e6152021-07-15 09:58:43 +02001904 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 strcpy(card->driver, "Digi32");
1908 switch (rme32->pci->device) {
Roland Dreier8b7fc422005-09-14 14:19:17 -07001909 case PCI_DEVICE_ID_RME_DIGI32:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 strcpy(card->shortname, "RME Digi32");
1911 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001912 case PCI_DEVICE_ID_RME_DIGI32_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 strcpy(card->shortname, "RME Digi32/8");
1914 break;
Roland Dreier8b7fc422005-09-14 14:19:17 -07001915 case PCI_DEVICE_ID_RME_DIGI32_PRO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 strcpy(card->shortname, "RME Digi32 PRO");
1917 break;
1918 }
1919 sprintf(card->longname, "%s (Rev. %d) at 0x%lx, irq %d",
1920 card->shortname, rme32->rev, rme32->port, rme32->irq);
1921
Takashi Iwai1c9140b2021-06-08 16:05:04 +02001922 err = snd_card_register(card);
Takashi Iwai102e6152021-07-15 09:58:43 +02001923 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 pci_set_drvdata(pci, card);
1926 dev++;
1927 return 0;
1928}
1929
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001930static struct pci_driver rme32_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02001931 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 .id_table = snd_rme32_ids,
1933 .probe = snd_rme32_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934};
1935
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001936module_pci_driver(rme32_driver);