2 * ALSA driver for RME Digi32, Digi32/8 and Digi32 PRO audio interfaces
9 * for writing the digi96-driver
10 * and RME for all informations.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * ****************************************************************************
29 * Note #1 "Sek'd models" ................................... martin 2002-12-07
31 * Identical soundcards by Sek'd were labeled:
32 * RME Digi 32 = Sek'd Prodif 32
33 * RME Digi 32 Pro = Sek'd Prodif 96
34 * RME Digi 32/8 = Sek'd Prodif Gold
36 * ****************************************************************************
38 * Note #2 "full duplex mode" ............................... martin 2002-12-07
40 * Full duplex doesn't work. All cards (32, 32/8, 32Pro) are working identical
41 * in this mode. Rec data and play data are using the same buffer therefore. At
42 * first you have got the playing bits in the buffer and then (after playing
43 * them) they were overwitten by the captured sound of the CS8412/14. Both
44 * modes (play/record) are running harmonically hand in hand in the same buffer
45 * and you have only one start bit plus one interrupt bit to control this
47 * This is opposite to the latter rme96 where playing and capturing is totally
48 * separated and so their full duplex mode is supported by alsa (using two
49 * start bits and two interrupts for two different buffers).
50 * But due to the wrong sequence of playing and capturing ALSA shows no solved
51 * full duplex support for the rme32 at the moment. That's bad, but I'm not
52 * able to solve it. Are you motivated enough to solve this problem now? Your
53 * patch would be welcome!
55 * ****************************************************************************
57 * "The story after the long seeking" -- tiwai
59 * Ok, the situation regarding the full duplex is now improved a bit.
60 * In the fullduplex mode (given by the module parameter), the hardware buffer
61 * is split to halves for read and write directions at the DMA pointer.
62 * That is, the half above the current DMA pointer is used for write, and
63 * the half below is used for read. To mangle this strange behavior, an
64 * software intermediate buffer is introduced. This is, of course, not good
65 * from the viewpoint of the data transfer efficiency. However, this allows
66 * you to use arbitrary buffer sizes, instead of the fixed I/O buffer size.
68 * ****************************************************************************
72 #include <linux/delay.h>
73 #include <linux/gfp.h>
74 #include <linux/init.h>
75 #include <linux/interrupt.h>
76 #include <linux/pci.h>
77 #include <linux/module.h>
80 #include <sound/core.h>
81 #include <sound/info.h>
82 #include <sound/control.h>
83 #include <sound/pcm.h>
84 #include <sound/pcm_params.h>
85 #include <sound/pcm-indirect.h>
86 #include <sound/asoundef.h>
87 #include <sound/initval.h>
89 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
90 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
91 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
92 static bool fullduplex[SNDRV_CARDS]; // = {[0 ... (SNDRV_CARDS - 1)] = 1};
94 module_param_array(index, int, NULL, 0444);
95 MODULE_PARM_DESC(index, "Index value for RME Digi32 soundcard.");
96 module_param_array(id, charp, NULL, 0444);
97 MODULE_PARM_DESC(id, "ID string for RME Digi32 soundcard.");
98 module_param_array(enable, bool, NULL, 0444);
99 MODULE_PARM_DESC(enable, "Enable RME Digi32 soundcard.");
100 module_param_array(fullduplex, bool, NULL, 0444);
101 MODULE_PARM_DESC(fullduplex, "Support full-duplex mode.");
103 MODULE_DESCRIPTION("RME Digi32, Digi32/8, Digi32 PRO");
104 MODULE_LICENSE("GPL");
105 MODULE_SUPPORTED_DEVICE("{{RME,Digi32}," "{RME,Digi32/8}," "{RME,Digi32 PRO}}");
107 /* Defines for RME Digi32 series */
108 #define RME32_SPDIF_NCHANNELS 2
110 /* Playback and capture buffer size */
111 #define RME32_BUFFER_SIZE 0x20000
114 #define RME32_IO_SIZE 0x30000
116 /* IO area offsets */
117 #define RME32_IO_DATA_BUFFER 0x0
118 #define RME32_IO_CONTROL_REGISTER 0x20000
119 #define RME32_IO_GET_POS 0x20000
120 #define RME32_IO_CONFIRM_ACTION_IRQ 0x20004
121 #define RME32_IO_RESET_POS 0x20100
123 /* Write control register bits */
124 #define RME32_WCR_START (1 << 0) /* startbit */
125 #define RME32_WCR_MONO (1 << 1) /* 0=stereo, 1=mono
126 Setting the whole card to mono
127 doesn't seem to be very useful.
128 A software-solution can handle
129 full-duplex with one direction in
130 stereo and the other way in mono.
131 So, the hardware should work all
132 the time in stereo! */
133 #define RME32_WCR_MODE24 (1 << 2) /* 0=16bit, 1=32bit */
134 #define RME32_WCR_SEL (1 << 3) /* 0=input on output, 1=normal playback/capture */
135 #define RME32_WCR_FREQ_0 (1 << 4) /* frequency (play) */
136 #define RME32_WCR_FREQ_1 (1 << 5)
137 #define RME32_WCR_INP_0 (1 << 6) /* input switch */
138 #define RME32_WCR_INP_1 (1 << 7)
139 #define RME32_WCR_RESET (1 << 8) /* Reset address */
140 #define RME32_WCR_MUTE (1 << 9) /* digital mute for output */
141 #define RME32_WCR_PRO (1 << 10) /* 1=professional, 0=consumer */
142 #define RME32_WCR_DS_BM (1 << 11) /* 1=DoubleSpeed (only PRO-Version); 1=BlockMode (only Adat-Version) */
143 #define RME32_WCR_ADAT (1 << 12) /* Adat Mode (only Adat-Version) */
144 #define RME32_WCR_AUTOSYNC (1 << 13) /* AutoSync */
145 #define RME32_WCR_PD (1 << 14) /* DAC Reset (only PRO-Version) */
146 #define RME32_WCR_EMP (1 << 15) /* 1=Emphasis on (only PRO-Version) */
148 #define RME32_WCR_BITPOS_FREQ_0 4
149 #define RME32_WCR_BITPOS_FREQ_1 5
150 #define RME32_WCR_BITPOS_INP_0 6
151 #define RME32_WCR_BITPOS_INP_1 7
153 /* Read control register bits */
154 #define RME32_RCR_AUDIO_ADDR_MASK 0x1ffff
155 #define RME32_RCR_LOCK (1 << 23) /* 1=locked, 0=not locked */
156 #define RME32_RCR_ERF (1 << 26) /* 1=Error, 0=no Error */
157 #define RME32_RCR_FREQ_0 (1 << 27) /* CS841x frequency (record) */
158 #define RME32_RCR_FREQ_1 (1 << 28)
159 #define RME32_RCR_FREQ_2 (1 << 29)
160 #define RME32_RCR_KMODE (1 << 30) /* card mode: 1=PLL, 0=quartz */
161 #define RME32_RCR_IRQ (1 << 31) /* interrupt */
163 #define RME32_RCR_BITPOS_F0 27
164 #define RME32_RCR_BITPOS_F1 28
165 #define RME32_RCR_BITPOS_F2 29
168 #define RME32_INPUT_OPTICAL 0
169 #define RME32_INPUT_COAXIAL 1
170 #define RME32_INPUT_INTERNAL 2
171 #define RME32_INPUT_XLR 3
174 #define RME32_CLOCKMODE_SLAVE 0
175 #define RME32_CLOCKMODE_MASTER_32 1
176 #define RME32_CLOCKMODE_MASTER_44 2
177 #define RME32_CLOCKMODE_MASTER_48 3
179 /* Block sizes in bytes */
180 #define RME32_BLOCK_SIZE 8192
182 /* Software intermediate buffer (max) size */
183 #define RME32_MID_BUFFER_SIZE (1024*1024)
185 /* Hardware revisions */
186 #define RME32_32_REVISION 192
187 #define RME32_328_REVISION_OLD 100
188 #define RME32_328_REVISION_NEW 101
189 #define RME32_PRO_REVISION_WITH_8412 192
190 #define RME32_PRO_REVISION_WITH_8414 150
197 void __iomem *iobase;
199 u32 wcreg; /* cached write control register value */
200 u32 wcreg_spdif; /* S/PDIF setup */
201 u32 wcreg_spdif_stream; /* S/PDIF setup (temporary) */
202 u32 rcreg; /* cached read control register value */
204 u8 rev; /* card revision number */
206 struct snd_pcm_substream *playback_substream;
207 struct snd_pcm_substream *capture_substream;
209 int playback_frlog; /* log2 of framesize */
212 size_t playback_periodsize; /* in bytes, zero if not used */
213 size_t capture_periodsize; /* in bytes, zero if not used */
215 unsigned int fullduplex_mode;
218 struct snd_pcm_indirect playback_pcm;
219 struct snd_pcm_indirect capture_pcm;
221 struct snd_card *card;
222 struct snd_pcm *spdif_pcm;
223 struct snd_pcm *adat_pcm;
225 struct snd_kcontrol *spdif_ctl;
228 static const struct pci_device_id snd_rme32_ids[] = {
229 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32), 0,},
230 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_8), 0,},
231 {PCI_VDEVICE(XILINX_RME, PCI_DEVICE_ID_RME_DIGI32_PRO), 0,},
235 MODULE_DEVICE_TABLE(pci, snd_rme32_ids);
237 #define RME32_ISWORKING(rme32) ((rme32)->wcreg & RME32_WCR_START)
238 #define RME32_PRO_WITH_8414(rme32) ((rme32)->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO && (rme32)->rev == RME32_PRO_REVISION_WITH_8414)
240 static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream);
242 static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream);
244 static int snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd);
246 static void snd_rme32_proc_init(struct rme32 * rme32);
248 static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32);
250 static inline unsigned int snd_rme32_pcm_byteptr(struct rme32 * rme32)
252 return (readl(rme32->iobase + RME32_IO_GET_POS)
253 & RME32_RCR_AUDIO_ADDR_MASK);
256 /* silence callback for halfduplex mode */
257 static int snd_rme32_playback_silence(struct snd_pcm_substream *substream,
258 int channel, unsigned long pos,
261 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
263 memset_io(rme32->iobase + RME32_IO_DATA_BUFFER + pos, 0, count);
267 /* copy callback for halfduplex mode */
268 static int snd_rme32_playback_copy(struct snd_pcm_substream *substream,
269 int channel, unsigned long pos,
270 void __user *src, unsigned long count)
272 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
274 if (copy_from_user_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
280 static int snd_rme32_playback_copy_kernel(struct snd_pcm_substream *substream,
281 int channel, unsigned long pos,
282 void *src, unsigned long count)
284 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
286 memcpy_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos, src, count);
290 /* copy callback for halfduplex mode */
291 static int snd_rme32_capture_copy(struct snd_pcm_substream *substream,
292 int channel, unsigned long pos,
293 void __user *dst, unsigned long count)
295 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
297 if (copy_to_user_fromio(dst,
298 rme32->iobase + RME32_IO_DATA_BUFFER + pos,
304 static int snd_rme32_capture_copy_kernel(struct snd_pcm_substream *substream,
305 int channel, unsigned long pos,
306 void *dst, unsigned long count)
308 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
310 memcpy_fromio(dst, rme32->iobase + RME32_IO_DATA_BUFFER + pos, count);
315 * SPDIF I/O capabilities (half-duplex mode)
317 static const struct snd_pcm_hardware snd_rme32_spdif_info = {
318 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
319 SNDRV_PCM_INFO_MMAP_VALID |
320 SNDRV_PCM_INFO_INTERLEAVED |
321 SNDRV_PCM_INFO_PAUSE |
322 SNDRV_PCM_INFO_SYNC_START |
323 SNDRV_PCM_INFO_SYNC_APPLPTR),
324 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
325 SNDRV_PCM_FMTBIT_S32_LE),
326 .rates = (SNDRV_PCM_RATE_32000 |
327 SNDRV_PCM_RATE_44100 |
328 SNDRV_PCM_RATE_48000),
333 .buffer_bytes_max = RME32_BUFFER_SIZE,
334 .period_bytes_min = RME32_BLOCK_SIZE,
335 .period_bytes_max = RME32_BLOCK_SIZE,
336 .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
337 .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
342 * ADAT I/O capabilities (half-duplex mode)
344 static const struct snd_pcm_hardware snd_rme32_adat_info =
346 .info = (SNDRV_PCM_INFO_MMAP_IOMEM |
347 SNDRV_PCM_INFO_MMAP_VALID |
348 SNDRV_PCM_INFO_INTERLEAVED |
349 SNDRV_PCM_INFO_PAUSE |
350 SNDRV_PCM_INFO_SYNC_START |
351 SNDRV_PCM_INFO_SYNC_APPLPTR),
352 .formats= SNDRV_PCM_FMTBIT_S16_LE,
353 .rates = (SNDRV_PCM_RATE_44100 |
354 SNDRV_PCM_RATE_48000),
359 .buffer_bytes_max = RME32_BUFFER_SIZE,
360 .period_bytes_min = RME32_BLOCK_SIZE,
361 .period_bytes_max = RME32_BLOCK_SIZE,
362 .periods_min = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
363 .periods_max = RME32_BUFFER_SIZE / RME32_BLOCK_SIZE,
368 * SPDIF I/O capabilities (full-duplex mode)
370 static const struct snd_pcm_hardware snd_rme32_spdif_fd_info = {
371 .info = (SNDRV_PCM_INFO_MMAP |
372 SNDRV_PCM_INFO_MMAP_VALID |
373 SNDRV_PCM_INFO_INTERLEAVED |
374 SNDRV_PCM_INFO_PAUSE |
375 SNDRV_PCM_INFO_SYNC_START |
376 SNDRV_PCM_INFO_SYNC_APPLPTR),
377 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
378 SNDRV_PCM_FMTBIT_S32_LE),
379 .rates = (SNDRV_PCM_RATE_32000 |
380 SNDRV_PCM_RATE_44100 |
381 SNDRV_PCM_RATE_48000),
386 .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
387 .period_bytes_min = RME32_BLOCK_SIZE,
388 .period_bytes_max = RME32_BLOCK_SIZE,
390 .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
395 * ADAT I/O capabilities (full-duplex mode)
397 static const struct snd_pcm_hardware snd_rme32_adat_fd_info =
399 .info = (SNDRV_PCM_INFO_MMAP |
400 SNDRV_PCM_INFO_MMAP_VALID |
401 SNDRV_PCM_INFO_INTERLEAVED |
402 SNDRV_PCM_INFO_PAUSE |
403 SNDRV_PCM_INFO_SYNC_START |
404 SNDRV_PCM_INFO_SYNC_APPLPTR),
405 .formats= SNDRV_PCM_FMTBIT_S16_LE,
406 .rates = (SNDRV_PCM_RATE_44100 |
407 SNDRV_PCM_RATE_48000),
412 .buffer_bytes_max = RME32_MID_BUFFER_SIZE,
413 .period_bytes_min = RME32_BLOCK_SIZE,
414 .period_bytes_max = RME32_BLOCK_SIZE,
416 .periods_max = RME32_MID_BUFFER_SIZE / RME32_BLOCK_SIZE,
420 static void snd_rme32_reset_dac(struct rme32 *rme32)
422 writel(rme32->wcreg | RME32_WCR_PD,
423 rme32->iobase + RME32_IO_CONTROL_REGISTER);
424 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
427 static int snd_rme32_playback_getrate(struct rme32 * rme32)
431 rate = ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
432 (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
446 return (rme32->wcreg & RME32_WCR_DS_BM) ? rate << 1 : rate;
449 static int snd_rme32_capture_getrate(struct rme32 * rme32, int *is_adat)
454 if (rme32->rcreg & RME32_RCR_LOCK) {
458 if (rme32->rcreg & RME32_RCR_ERF) {
463 n = ((rme32->rcreg >> RME32_RCR_BITPOS_F0) & 1) +
464 (((rme32->rcreg >> RME32_RCR_BITPOS_F1) & 1) << 1) +
465 (((rme32->rcreg >> RME32_RCR_BITPOS_F2) & 1) << 2);
467 if (RME32_PRO_WITH_8414(rme32))
468 switch (n) { /* supporting the CS8414 */
488 switch (n) { /* supporting the CS8412 */
511 static int snd_rme32_playback_setrate(struct rme32 * rme32, int rate)
515 ds = rme32->wcreg & RME32_WCR_DS_BM;
518 rme32->wcreg &= ~RME32_WCR_DS_BM;
519 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
523 rme32->wcreg &= ~RME32_WCR_DS_BM;
524 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
528 rme32->wcreg &= ~RME32_WCR_DS_BM;
529 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
533 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
535 rme32->wcreg |= RME32_WCR_DS_BM;
536 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
540 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
542 rme32->wcreg |= RME32_WCR_DS_BM;
543 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_1) &
547 if (rme32->pci->device != PCI_DEVICE_ID_RME_DIGI32_PRO)
549 rme32->wcreg |= RME32_WCR_DS_BM;
550 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
556 if ((!ds && rme32->wcreg & RME32_WCR_DS_BM) ||
557 (ds && !(rme32->wcreg & RME32_WCR_DS_BM)))
559 /* change to/from double-speed: reset the DAC (if available) */
560 snd_rme32_reset_dac(rme32);
562 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
567 static int snd_rme32_setclockmode(struct rme32 * rme32, int mode)
570 case RME32_CLOCKMODE_SLAVE:
572 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) &
575 case RME32_CLOCKMODE_MASTER_32:
576 /* Internal 32.0kHz */
577 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) &
580 case RME32_CLOCKMODE_MASTER_44:
581 /* Internal 44.1kHz */
582 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_FREQ_0) |
585 case RME32_CLOCKMODE_MASTER_48:
586 /* Internal 48.0kHz */
587 rme32->wcreg = (rme32->wcreg | RME32_WCR_FREQ_0) |
593 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
597 static int snd_rme32_getclockmode(struct rme32 * rme32)
599 return ((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_0) & 1) +
600 (((rme32->wcreg >> RME32_WCR_BITPOS_FREQ_1) & 1) << 1);
603 static int snd_rme32_setinputtype(struct rme32 * rme32, int type)
606 case RME32_INPUT_OPTICAL:
607 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) &
610 case RME32_INPUT_COAXIAL:
611 rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) &
614 case RME32_INPUT_INTERNAL:
615 rme32->wcreg = (rme32->wcreg & ~RME32_WCR_INP_0) |
618 case RME32_INPUT_XLR:
619 rme32->wcreg = (rme32->wcreg | RME32_WCR_INP_0) |
625 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
629 static int snd_rme32_getinputtype(struct rme32 * rme32)
631 return ((rme32->wcreg >> RME32_WCR_BITPOS_INP_0) & 1) +
632 (((rme32->wcreg >> RME32_WCR_BITPOS_INP_1) & 1) << 1);
636 snd_rme32_setframelog(struct rme32 * rme32, int n_channels, int is_playback)
640 if (n_channels == 2) {
643 /* assume 8 channels */
647 frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
648 rme32->playback_frlog = frlog;
650 frlog += (rme32->wcreg & RME32_WCR_MODE24) ? 2 : 1;
651 rme32->capture_frlog = frlog;
655 static int snd_rme32_setformat(struct rme32 *rme32, snd_pcm_format_t format)
658 case SNDRV_PCM_FORMAT_S16_LE:
659 rme32->wcreg &= ~RME32_WCR_MODE24;
661 case SNDRV_PCM_FORMAT_S32_LE:
662 rme32->wcreg |= RME32_WCR_MODE24;
667 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
672 snd_rme32_playback_hw_params(struct snd_pcm_substream *substream,
673 struct snd_pcm_hw_params *params)
675 int err, rate, dummy;
676 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
677 struct snd_pcm_runtime *runtime = substream->runtime;
679 if (rme32->fullduplex_mode) {
680 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
684 runtime->dma_area = (void __force *)(rme32->iobase +
685 RME32_IO_DATA_BUFFER);
686 runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
687 runtime->dma_bytes = RME32_BUFFER_SIZE;
690 spin_lock_irq(&rme32->lock);
691 if ((rme32->rcreg & RME32_RCR_KMODE) &&
692 (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
694 if ((int)params_rate(params) != rate) {
695 spin_unlock_irq(&rme32->lock);
698 } else if ((err = snd_rme32_playback_setrate(rme32, params_rate(params))) < 0) {
699 spin_unlock_irq(&rme32->lock);
702 if ((err = snd_rme32_setformat(rme32, params_format(params))) < 0) {
703 spin_unlock_irq(&rme32->lock);
707 snd_rme32_setframelog(rme32, params_channels(params), 1);
708 if (rme32->capture_periodsize != 0) {
709 if (params_period_size(params) << rme32->playback_frlog != rme32->capture_periodsize) {
710 spin_unlock_irq(&rme32->lock);
714 rme32->playback_periodsize = params_period_size(params) << rme32->playback_frlog;
716 if ((rme32->wcreg & RME32_WCR_ADAT) == 0) {
717 rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
718 rme32->wcreg |= rme32->wcreg_spdif_stream;
719 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
721 spin_unlock_irq(&rme32->lock);
727 snd_rme32_capture_hw_params(struct snd_pcm_substream *substream,
728 struct snd_pcm_hw_params *params)
730 int err, isadat, rate;
731 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
732 struct snd_pcm_runtime *runtime = substream->runtime;
734 if (rme32->fullduplex_mode) {
735 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
739 runtime->dma_area = (void __force *)rme32->iobase +
740 RME32_IO_DATA_BUFFER;
741 runtime->dma_addr = rme32->port + RME32_IO_DATA_BUFFER;
742 runtime->dma_bytes = RME32_BUFFER_SIZE;
745 spin_lock_irq(&rme32->lock);
746 /* enable AutoSync for record-preparing */
747 rme32->wcreg |= RME32_WCR_AUTOSYNC;
748 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
750 if ((err = snd_rme32_setformat(rme32, params_format(params))) < 0) {
751 spin_unlock_irq(&rme32->lock);
754 if ((err = snd_rme32_playback_setrate(rme32, params_rate(params))) < 0) {
755 spin_unlock_irq(&rme32->lock);
758 if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
759 if ((int)params_rate(params) != rate) {
760 spin_unlock_irq(&rme32->lock);
763 if ((isadat && runtime->hw.channels_min == 2) ||
764 (!isadat && runtime->hw.channels_min == 8)) {
765 spin_unlock_irq(&rme32->lock);
769 /* AutoSync off for recording */
770 rme32->wcreg &= ~RME32_WCR_AUTOSYNC;
771 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
773 snd_rme32_setframelog(rme32, params_channels(params), 0);
774 if (rme32->playback_periodsize != 0) {
775 if (params_period_size(params) << rme32->capture_frlog !=
776 rme32->playback_periodsize) {
777 spin_unlock_irq(&rme32->lock);
781 rme32->capture_periodsize =
782 params_period_size(params) << rme32->capture_frlog;
783 spin_unlock_irq(&rme32->lock);
788 static int snd_rme32_pcm_hw_free(struct snd_pcm_substream *substream)
790 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
791 if (! rme32->fullduplex_mode)
793 return snd_pcm_lib_free_pages(substream);
796 static void snd_rme32_pcm_start(struct rme32 * rme32, int from_pause)
799 writel(0, rme32->iobase + RME32_IO_RESET_POS);
802 rme32->wcreg |= RME32_WCR_START;
803 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
806 static void snd_rme32_pcm_stop(struct rme32 * rme32, int to_pause)
809 * Check if there is an unconfirmed IRQ, if so confirm it, or else
810 * the hardware will not stop generating interrupts
812 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
813 if (rme32->rcreg & RME32_RCR_IRQ) {
814 writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
816 rme32->wcreg &= ~RME32_WCR_START;
817 if (rme32->wcreg & RME32_WCR_SEL)
818 rme32->wcreg |= RME32_WCR_MUTE;
819 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
821 writel(0, rme32->iobase + RME32_IO_RESET_POS);
824 static irqreturn_t snd_rme32_interrupt(int irq, void *dev_id)
826 struct rme32 *rme32 = (struct rme32 *) dev_id;
828 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
829 if (!(rme32->rcreg & RME32_RCR_IRQ)) {
832 if (rme32->capture_substream) {
833 snd_pcm_period_elapsed(rme32->capture_substream);
835 if (rme32->playback_substream) {
836 snd_pcm_period_elapsed(rme32->playback_substream);
838 writel(0, rme32->iobase + RME32_IO_CONFIRM_ACTION_IRQ);
843 static const unsigned int period_bytes[] = { RME32_BLOCK_SIZE };
845 static const struct snd_pcm_hw_constraint_list hw_constraints_period_bytes = {
846 .count = ARRAY_SIZE(period_bytes),
847 .list = period_bytes,
851 static void snd_rme32_set_buffer_constraint(struct rme32 *rme32, struct snd_pcm_runtime *runtime)
853 if (! rme32->fullduplex_mode) {
854 snd_pcm_hw_constraint_single(runtime,
855 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
857 snd_pcm_hw_constraint_list(runtime, 0,
858 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
859 &hw_constraints_period_bytes);
863 static int snd_rme32_playback_spdif_open(struct snd_pcm_substream *substream)
866 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
867 struct snd_pcm_runtime *runtime = substream->runtime;
869 snd_pcm_set_sync(substream);
871 spin_lock_irq(&rme32->lock);
872 if (rme32->playback_substream != NULL) {
873 spin_unlock_irq(&rme32->lock);
876 rme32->wcreg &= ~RME32_WCR_ADAT;
877 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
878 rme32->playback_substream = substream;
879 spin_unlock_irq(&rme32->lock);
881 if (rme32->fullduplex_mode)
882 runtime->hw = snd_rme32_spdif_fd_info;
884 runtime->hw = snd_rme32_spdif_info;
885 if (rme32->pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO) {
886 runtime->hw.rates |= SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
887 runtime->hw.rate_max = 96000;
889 if ((rme32->rcreg & RME32_RCR_KMODE) &&
890 (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
892 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
893 runtime->hw.rate_min = rate;
894 runtime->hw.rate_max = rate;
897 snd_rme32_set_buffer_constraint(rme32, runtime);
899 rme32->wcreg_spdif_stream = rme32->wcreg_spdif;
900 rme32->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
901 snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
902 SNDRV_CTL_EVENT_MASK_INFO, &rme32->spdif_ctl->id);
906 static int snd_rme32_capture_spdif_open(struct snd_pcm_substream *substream)
909 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
910 struct snd_pcm_runtime *runtime = substream->runtime;
912 snd_pcm_set_sync(substream);
914 spin_lock_irq(&rme32->lock);
915 if (rme32->capture_substream != NULL) {
916 spin_unlock_irq(&rme32->lock);
919 rme32->capture_substream = substream;
920 spin_unlock_irq(&rme32->lock);
922 if (rme32->fullduplex_mode)
923 runtime->hw = snd_rme32_spdif_fd_info;
925 runtime->hw = snd_rme32_spdif_info;
926 if (RME32_PRO_WITH_8414(rme32)) {
927 runtime->hw.rates |= SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000;
928 runtime->hw.rate_max = 96000;
930 if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
934 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
935 runtime->hw.rate_min = rate;
936 runtime->hw.rate_max = rate;
939 snd_rme32_set_buffer_constraint(rme32, runtime);
945 snd_rme32_playback_adat_open(struct snd_pcm_substream *substream)
948 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
949 struct snd_pcm_runtime *runtime = substream->runtime;
951 snd_pcm_set_sync(substream);
953 spin_lock_irq(&rme32->lock);
954 if (rme32->playback_substream != NULL) {
955 spin_unlock_irq(&rme32->lock);
958 rme32->wcreg |= RME32_WCR_ADAT;
959 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
960 rme32->playback_substream = substream;
961 spin_unlock_irq(&rme32->lock);
963 if (rme32->fullduplex_mode)
964 runtime->hw = snd_rme32_adat_fd_info;
966 runtime->hw = snd_rme32_adat_info;
967 if ((rme32->rcreg & RME32_RCR_KMODE) &&
968 (rate = snd_rme32_capture_getrate(rme32, &dummy)) > 0) {
970 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
971 runtime->hw.rate_min = rate;
972 runtime->hw.rate_max = rate;
975 snd_rme32_set_buffer_constraint(rme32, runtime);
980 snd_rme32_capture_adat_open(struct snd_pcm_substream *substream)
983 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
984 struct snd_pcm_runtime *runtime = substream->runtime;
986 if (rme32->fullduplex_mode)
987 runtime->hw = snd_rme32_adat_fd_info;
989 runtime->hw = snd_rme32_adat_info;
990 if ((rate = snd_rme32_capture_getrate(rme32, &isadat)) > 0) {
994 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
995 runtime->hw.rate_min = rate;
996 runtime->hw.rate_max = rate;
999 snd_pcm_set_sync(substream);
1001 spin_lock_irq(&rme32->lock);
1002 if (rme32->capture_substream != NULL) {
1003 spin_unlock_irq(&rme32->lock);
1006 rme32->capture_substream = substream;
1007 spin_unlock_irq(&rme32->lock);
1009 snd_rme32_set_buffer_constraint(rme32, runtime);
1013 static int snd_rme32_playback_close(struct snd_pcm_substream *substream)
1015 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1018 spin_lock_irq(&rme32->lock);
1019 rme32->playback_substream = NULL;
1020 rme32->playback_periodsize = 0;
1021 spdif = (rme32->wcreg & RME32_WCR_ADAT) == 0;
1022 spin_unlock_irq(&rme32->lock);
1024 rme32->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1025 snd_ctl_notify(rme32->card, SNDRV_CTL_EVENT_MASK_VALUE |
1026 SNDRV_CTL_EVENT_MASK_INFO,
1027 &rme32->spdif_ctl->id);
1032 static int snd_rme32_capture_close(struct snd_pcm_substream *substream)
1034 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1036 spin_lock_irq(&rme32->lock);
1037 rme32->capture_substream = NULL;
1038 rme32->capture_periodsize = 0;
1039 spin_unlock_irq(&rme32->lock);
1043 static int snd_rme32_playback_prepare(struct snd_pcm_substream *substream)
1045 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1047 spin_lock_irq(&rme32->lock);
1048 if (rme32->fullduplex_mode) {
1049 memset(&rme32->playback_pcm, 0, sizeof(rme32->playback_pcm));
1050 rme32->playback_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
1051 rme32->playback_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1053 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1055 if (rme32->wcreg & RME32_WCR_SEL)
1056 rme32->wcreg &= ~RME32_WCR_MUTE;
1057 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1058 spin_unlock_irq(&rme32->lock);
1062 static int snd_rme32_capture_prepare(struct snd_pcm_substream *substream)
1064 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1066 spin_lock_irq(&rme32->lock);
1067 if (rme32->fullduplex_mode) {
1068 memset(&rme32->capture_pcm, 0, sizeof(rme32->capture_pcm));
1069 rme32->capture_pcm.hw_buffer_size = RME32_BUFFER_SIZE;
1070 rme32->capture_pcm.hw_queue_size = RME32_BUFFER_SIZE / 2;
1071 rme32->capture_pcm.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1073 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1075 spin_unlock_irq(&rme32->lock);
1080 snd_rme32_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1082 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1083 struct snd_pcm_substream *s;
1085 spin_lock(&rme32->lock);
1086 snd_pcm_group_for_each_entry(s, substream) {
1087 if (s != rme32->playback_substream &&
1088 s != rme32->capture_substream)
1091 case SNDRV_PCM_TRIGGER_START:
1092 rme32->running |= (1 << s->stream);
1093 if (rme32->fullduplex_mode) {
1094 /* remember the current DMA position */
1095 if (s == rme32->playback_substream) {
1096 rme32->playback_pcm.hw_io =
1097 rme32->playback_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
1099 rme32->capture_pcm.hw_io =
1100 rme32->capture_pcm.hw_data = snd_rme32_pcm_byteptr(rme32);
1104 case SNDRV_PCM_TRIGGER_STOP:
1105 rme32->running &= ~(1 << s->stream);
1108 snd_pcm_trigger_done(s, substream);
1112 case SNDRV_PCM_TRIGGER_START:
1113 if (rme32->running && ! RME32_ISWORKING(rme32))
1114 snd_rme32_pcm_start(rme32, 0);
1116 case SNDRV_PCM_TRIGGER_STOP:
1117 if (! rme32->running && RME32_ISWORKING(rme32))
1118 snd_rme32_pcm_stop(rme32, 0);
1120 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1121 if (rme32->running && RME32_ISWORKING(rme32))
1122 snd_rme32_pcm_stop(rme32, 1);
1124 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1125 if (rme32->running && ! RME32_ISWORKING(rme32))
1126 snd_rme32_pcm_start(rme32, 1);
1129 spin_unlock(&rme32->lock);
1133 /* pointer callback for halfduplex mode */
1134 static snd_pcm_uframes_t
1135 snd_rme32_playback_pointer(struct snd_pcm_substream *substream)
1137 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1138 return snd_rme32_pcm_byteptr(rme32) >> rme32->playback_frlog;
1141 static snd_pcm_uframes_t
1142 snd_rme32_capture_pointer(struct snd_pcm_substream *substream)
1144 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1145 return snd_rme32_pcm_byteptr(rme32) >> rme32->capture_frlog;
1149 /* ack and pointer callbacks for fullduplex mode */
1150 static void snd_rme32_pb_trans_copy(struct snd_pcm_substream *substream,
1151 struct snd_pcm_indirect *rec, size_t bytes)
1153 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1154 memcpy_toio(rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
1155 substream->runtime->dma_area + rec->sw_data, bytes);
1158 static int snd_rme32_playback_fd_ack(struct snd_pcm_substream *substream)
1160 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1161 struct snd_pcm_indirect *rec, *cprec;
1163 rec = &rme32->playback_pcm;
1164 cprec = &rme32->capture_pcm;
1165 spin_lock(&rme32->lock);
1166 rec->hw_queue_size = RME32_BUFFER_SIZE;
1167 if (rme32->running & (1 << SNDRV_PCM_STREAM_CAPTURE))
1168 rec->hw_queue_size -= cprec->hw_ready;
1169 spin_unlock(&rme32->lock);
1170 return snd_pcm_indirect_playback_transfer(substream, rec,
1171 snd_rme32_pb_trans_copy);
1174 static void snd_rme32_cp_trans_copy(struct snd_pcm_substream *substream,
1175 struct snd_pcm_indirect *rec, size_t bytes)
1177 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1178 memcpy_fromio(substream->runtime->dma_area + rec->sw_data,
1179 rme32->iobase + RME32_IO_DATA_BUFFER + rec->hw_data,
1183 static int snd_rme32_capture_fd_ack(struct snd_pcm_substream *substream)
1185 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1186 return snd_pcm_indirect_capture_transfer(substream, &rme32->capture_pcm,
1187 snd_rme32_cp_trans_copy);
1190 static snd_pcm_uframes_t
1191 snd_rme32_playback_fd_pointer(struct snd_pcm_substream *substream)
1193 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1194 return snd_pcm_indirect_playback_pointer(substream, &rme32->playback_pcm,
1195 snd_rme32_pcm_byteptr(rme32));
1198 static snd_pcm_uframes_t
1199 snd_rme32_capture_fd_pointer(struct snd_pcm_substream *substream)
1201 struct rme32 *rme32 = snd_pcm_substream_chip(substream);
1202 return snd_pcm_indirect_capture_pointer(substream, &rme32->capture_pcm,
1203 snd_rme32_pcm_byteptr(rme32));
1206 /* for halfduplex mode */
1207 static const struct snd_pcm_ops snd_rme32_playback_spdif_ops = {
1208 .open = snd_rme32_playback_spdif_open,
1209 .close = snd_rme32_playback_close,
1210 .ioctl = snd_pcm_lib_ioctl,
1211 .hw_params = snd_rme32_playback_hw_params,
1212 .hw_free = snd_rme32_pcm_hw_free,
1213 .prepare = snd_rme32_playback_prepare,
1214 .trigger = snd_rme32_pcm_trigger,
1215 .pointer = snd_rme32_playback_pointer,
1216 .copy_user = snd_rme32_playback_copy,
1217 .copy_kernel = snd_rme32_playback_copy_kernel,
1218 .fill_silence = snd_rme32_playback_silence,
1219 .mmap = snd_pcm_lib_mmap_iomem,
1222 static const struct snd_pcm_ops snd_rme32_capture_spdif_ops = {
1223 .open = snd_rme32_capture_spdif_open,
1224 .close = snd_rme32_capture_close,
1225 .ioctl = snd_pcm_lib_ioctl,
1226 .hw_params = snd_rme32_capture_hw_params,
1227 .hw_free = snd_rme32_pcm_hw_free,
1228 .prepare = snd_rme32_capture_prepare,
1229 .trigger = snd_rme32_pcm_trigger,
1230 .pointer = snd_rme32_capture_pointer,
1231 .copy_user = snd_rme32_capture_copy,
1232 .copy_kernel = snd_rme32_capture_copy_kernel,
1233 .mmap = snd_pcm_lib_mmap_iomem,
1236 static const struct snd_pcm_ops snd_rme32_playback_adat_ops = {
1237 .open = snd_rme32_playback_adat_open,
1238 .close = snd_rme32_playback_close,
1239 .ioctl = snd_pcm_lib_ioctl,
1240 .hw_params = snd_rme32_playback_hw_params,
1241 .prepare = snd_rme32_playback_prepare,
1242 .trigger = snd_rme32_pcm_trigger,
1243 .pointer = snd_rme32_playback_pointer,
1244 .copy_user = snd_rme32_playback_copy,
1245 .copy_kernel = snd_rme32_playback_copy_kernel,
1246 .fill_silence = snd_rme32_playback_silence,
1247 .mmap = snd_pcm_lib_mmap_iomem,
1250 static const struct snd_pcm_ops snd_rme32_capture_adat_ops = {
1251 .open = snd_rme32_capture_adat_open,
1252 .close = snd_rme32_capture_close,
1253 .ioctl = snd_pcm_lib_ioctl,
1254 .hw_params = snd_rme32_capture_hw_params,
1255 .prepare = snd_rme32_capture_prepare,
1256 .trigger = snd_rme32_pcm_trigger,
1257 .pointer = snd_rme32_capture_pointer,
1258 .copy_user = snd_rme32_capture_copy,
1259 .copy_kernel = snd_rme32_capture_copy_kernel,
1260 .mmap = snd_pcm_lib_mmap_iomem,
1263 /* for fullduplex mode */
1264 static const struct snd_pcm_ops snd_rme32_playback_spdif_fd_ops = {
1265 .open = snd_rme32_playback_spdif_open,
1266 .close = snd_rme32_playback_close,
1267 .ioctl = snd_pcm_lib_ioctl,
1268 .hw_params = snd_rme32_playback_hw_params,
1269 .hw_free = snd_rme32_pcm_hw_free,
1270 .prepare = snd_rme32_playback_prepare,
1271 .trigger = snd_rme32_pcm_trigger,
1272 .pointer = snd_rme32_playback_fd_pointer,
1273 .ack = snd_rme32_playback_fd_ack,
1276 static const struct snd_pcm_ops snd_rme32_capture_spdif_fd_ops = {
1277 .open = snd_rme32_capture_spdif_open,
1278 .close = snd_rme32_capture_close,
1279 .ioctl = snd_pcm_lib_ioctl,
1280 .hw_params = snd_rme32_capture_hw_params,
1281 .hw_free = snd_rme32_pcm_hw_free,
1282 .prepare = snd_rme32_capture_prepare,
1283 .trigger = snd_rme32_pcm_trigger,
1284 .pointer = snd_rme32_capture_fd_pointer,
1285 .ack = snd_rme32_capture_fd_ack,
1288 static const struct snd_pcm_ops snd_rme32_playback_adat_fd_ops = {
1289 .open = snd_rme32_playback_adat_open,
1290 .close = snd_rme32_playback_close,
1291 .ioctl = snd_pcm_lib_ioctl,
1292 .hw_params = snd_rme32_playback_hw_params,
1293 .prepare = snd_rme32_playback_prepare,
1294 .trigger = snd_rme32_pcm_trigger,
1295 .pointer = snd_rme32_playback_fd_pointer,
1296 .ack = snd_rme32_playback_fd_ack,
1299 static const struct snd_pcm_ops snd_rme32_capture_adat_fd_ops = {
1300 .open = snd_rme32_capture_adat_open,
1301 .close = snd_rme32_capture_close,
1302 .ioctl = snd_pcm_lib_ioctl,
1303 .hw_params = snd_rme32_capture_hw_params,
1304 .prepare = snd_rme32_capture_prepare,
1305 .trigger = snd_rme32_pcm_trigger,
1306 .pointer = snd_rme32_capture_fd_pointer,
1307 .ack = snd_rme32_capture_fd_ack,
1310 static void snd_rme32_free(void *private_data)
1312 struct rme32 *rme32 = (struct rme32 *) private_data;
1314 if (rme32 == NULL) {
1317 if (rme32->irq >= 0) {
1318 snd_rme32_pcm_stop(rme32, 0);
1319 free_irq(rme32->irq, (void *) rme32);
1322 if (rme32->iobase) {
1323 iounmap(rme32->iobase);
1324 rme32->iobase = NULL;
1327 pci_release_regions(rme32->pci);
1330 pci_disable_device(rme32->pci);
1333 static void snd_rme32_free_spdif_pcm(struct snd_pcm *pcm)
1335 struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
1336 rme32->spdif_pcm = NULL;
1340 snd_rme32_free_adat_pcm(struct snd_pcm *pcm)
1342 struct rme32 *rme32 = (struct rme32 *) pcm->private_data;
1343 rme32->adat_pcm = NULL;
1346 static int snd_rme32_create(struct rme32 *rme32)
1348 struct pci_dev *pci = rme32->pci;
1352 spin_lock_init(&rme32->lock);
1354 if ((err = pci_enable_device(pci)) < 0)
1357 if ((err = pci_request_regions(pci, "RME32")) < 0)
1359 rme32->port = pci_resource_start(rme32->pci, 0);
1361 rme32->iobase = ioremap_nocache(rme32->port, RME32_IO_SIZE);
1362 if (!rme32->iobase) {
1363 dev_err(rme32->card->dev,
1364 "unable to remap memory region 0x%lx-0x%lx\n",
1365 rme32->port, rme32->port + RME32_IO_SIZE - 1);
1369 if (request_irq(pci->irq, snd_rme32_interrupt, IRQF_SHARED,
1370 KBUILD_MODNAME, rme32)) {
1371 dev_err(rme32->card->dev, "unable to grab IRQ %d\n", pci->irq);
1374 rme32->irq = pci->irq;
1376 /* read the card's revision number */
1377 pci_read_config_byte(pci, 8, &rme32->rev);
1379 /* set up ALSA pcm device for S/PDIF */
1380 if ((err = snd_pcm_new(rme32->card, "Digi32 IEC958", 0, 1, 1, &rme32->spdif_pcm)) < 0) {
1383 rme32->spdif_pcm->private_data = rme32;
1384 rme32->spdif_pcm->private_free = snd_rme32_free_spdif_pcm;
1385 strcpy(rme32->spdif_pcm->name, "Digi32 IEC958");
1386 if (rme32->fullduplex_mode) {
1387 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1388 &snd_rme32_playback_spdif_fd_ops);
1389 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
1390 &snd_rme32_capture_spdif_fd_ops);
1391 snd_pcm_lib_preallocate_pages_for_all(rme32->spdif_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1392 snd_dma_continuous_data(GFP_KERNEL),
1393 0, RME32_MID_BUFFER_SIZE);
1394 rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1396 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1397 &snd_rme32_playback_spdif_ops);
1398 snd_pcm_set_ops(rme32->spdif_pcm, SNDRV_PCM_STREAM_CAPTURE,
1399 &snd_rme32_capture_spdif_ops);
1400 rme32->spdif_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
1403 /* set up ALSA pcm device for ADAT */
1404 if ((pci->device == PCI_DEVICE_ID_RME_DIGI32) ||
1405 (pci->device == PCI_DEVICE_ID_RME_DIGI32_PRO)) {
1406 /* ADAT is not available on DIGI32 and DIGI32 Pro */
1407 rme32->adat_pcm = NULL;
1410 if ((err = snd_pcm_new(rme32->card, "Digi32 ADAT", 1,
1411 1, 1, &rme32->adat_pcm)) < 0)
1415 rme32->adat_pcm->private_data = rme32;
1416 rme32->adat_pcm->private_free = snd_rme32_free_adat_pcm;
1417 strcpy(rme32->adat_pcm->name, "Digi32 ADAT");
1418 if (rme32->fullduplex_mode) {
1419 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1420 &snd_rme32_playback_adat_fd_ops);
1421 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
1422 &snd_rme32_capture_adat_fd_ops);
1423 snd_pcm_lib_preallocate_pages_for_all(rme32->adat_pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1424 snd_dma_continuous_data(GFP_KERNEL),
1425 0, RME32_MID_BUFFER_SIZE);
1426 rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1428 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_PLAYBACK,
1429 &snd_rme32_playback_adat_ops);
1430 snd_pcm_set_ops(rme32->adat_pcm, SNDRV_PCM_STREAM_CAPTURE,
1431 &snd_rme32_capture_adat_ops);
1432 rme32->adat_pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
1437 rme32->playback_periodsize = 0;
1438 rme32->capture_periodsize = 0;
1440 /* make sure playback/capture is stopped, if by some reason active */
1441 snd_rme32_pcm_stop(rme32, 0);
1444 snd_rme32_reset_dac(rme32);
1446 /* reset buffer pointer */
1447 writel(0, rme32->iobase + RME32_IO_RESET_POS);
1449 /* set default values in registers */
1450 rme32->wcreg = RME32_WCR_SEL | /* normal playback */
1451 RME32_WCR_INP_0 | /* input select */
1452 RME32_WCR_MUTE; /* muting on */
1453 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1456 /* init switch interface */
1457 if ((err = snd_rme32_create_switches(rme32->card, rme32)) < 0) {
1461 /* init proc interface */
1462 snd_rme32_proc_init(rme32);
1464 rme32->capture_substream = NULL;
1465 rme32->playback_substream = NULL;
1475 snd_rme32_proc_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer)
1478 struct rme32 *rme32 = (struct rme32 *) entry->private_data;
1480 rme32->rcreg = readl(rme32->iobase + RME32_IO_CONTROL_REGISTER);
1482 snd_iprintf(buffer, rme32->card->longname);
1483 snd_iprintf(buffer, " (index #%d)\n", rme32->card->number + 1);
1485 snd_iprintf(buffer, "\nGeneral settings\n");
1486 if (rme32->fullduplex_mode)
1487 snd_iprintf(buffer, " Full-duplex mode\n");
1489 snd_iprintf(buffer, " Half-duplex mode\n");
1490 if (RME32_PRO_WITH_8414(rme32)) {
1491 snd_iprintf(buffer, " receiver: CS8414\n");
1493 snd_iprintf(buffer, " receiver: CS8412\n");
1495 if (rme32->wcreg & RME32_WCR_MODE24) {
1496 snd_iprintf(buffer, " format: 24 bit");
1498 snd_iprintf(buffer, " format: 16 bit");
1500 if (rme32->wcreg & RME32_WCR_MONO) {
1501 snd_iprintf(buffer, ", Mono\n");
1503 snd_iprintf(buffer, ", Stereo\n");
1506 snd_iprintf(buffer, "\nInput settings\n");
1507 switch (snd_rme32_getinputtype(rme32)) {
1508 case RME32_INPUT_OPTICAL:
1509 snd_iprintf(buffer, " input: optical");
1511 case RME32_INPUT_COAXIAL:
1512 snd_iprintf(buffer, " input: coaxial");
1514 case RME32_INPUT_INTERNAL:
1515 snd_iprintf(buffer, " input: internal");
1517 case RME32_INPUT_XLR:
1518 snd_iprintf(buffer, " input: XLR");
1521 if (snd_rme32_capture_getrate(rme32, &n) < 0) {
1522 snd_iprintf(buffer, "\n sample rate: no valid signal\n");
1525 snd_iprintf(buffer, " (8 channels)\n");
1527 snd_iprintf(buffer, " (2 channels)\n");
1529 snd_iprintf(buffer, " sample rate: %d Hz\n",
1530 snd_rme32_capture_getrate(rme32, &n));
1533 snd_iprintf(buffer, "\nOutput settings\n");
1534 if (rme32->wcreg & RME32_WCR_SEL) {
1535 snd_iprintf(buffer, " output signal: normal playback");
1537 snd_iprintf(buffer, " output signal: same as input");
1539 if (rme32->wcreg & RME32_WCR_MUTE) {
1540 snd_iprintf(buffer, " (muted)\n");
1542 snd_iprintf(buffer, "\n");
1545 /* master output frequency */
1547 ((!(rme32->wcreg & RME32_WCR_FREQ_0))
1548 && (!(rme32->wcreg & RME32_WCR_FREQ_1)))) {
1549 snd_iprintf(buffer, " sample rate: %d Hz\n",
1550 snd_rme32_playback_getrate(rme32));
1552 if (rme32->rcreg & RME32_RCR_KMODE) {
1553 snd_iprintf(buffer, " sample clock source: AutoSync\n");
1555 snd_iprintf(buffer, " sample clock source: Internal\n");
1557 if (rme32->wcreg & RME32_WCR_PRO) {
1558 snd_iprintf(buffer, " format: AES/EBU (professional)\n");
1560 snd_iprintf(buffer, " format: IEC958 (consumer)\n");
1562 if (rme32->wcreg & RME32_WCR_EMP) {
1563 snd_iprintf(buffer, " emphasis: on\n");
1565 snd_iprintf(buffer, " emphasis: off\n");
1569 static void snd_rme32_proc_init(struct rme32 *rme32)
1571 struct snd_info_entry *entry;
1573 if (! snd_card_proc_new(rme32->card, "rme32", &entry))
1574 snd_info_set_text_ops(entry, rme32, snd_rme32_proc_read);
1581 #define snd_rme32_info_loopback_control snd_ctl_boolean_mono_info
1584 snd_rme32_get_loopback_control(struct snd_kcontrol *kcontrol,
1585 struct snd_ctl_elem_value *ucontrol)
1587 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1589 spin_lock_irq(&rme32->lock);
1590 ucontrol->value.integer.value[0] =
1591 rme32->wcreg & RME32_WCR_SEL ? 0 : 1;
1592 spin_unlock_irq(&rme32->lock);
1596 snd_rme32_put_loopback_control(struct snd_kcontrol *kcontrol,
1597 struct snd_ctl_elem_value *ucontrol)
1599 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1603 val = ucontrol->value.integer.value[0] ? 0 : RME32_WCR_SEL;
1604 spin_lock_irq(&rme32->lock);
1605 val = (rme32->wcreg & ~RME32_WCR_SEL) | val;
1606 change = val != rme32->wcreg;
1607 if (ucontrol->value.integer.value[0])
1608 val &= ~RME32_WCR_MUTE;
1610 val |= RME32_WCR_MUTE;
1612 writel(val, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1613 spin_unlock_irq(&rme32->lock);
1618 snd_rme32_info_inputtype_control(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_info *uinfo)
1621 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1622 static const char * const texts[4] = {
1623 "Optical", "Coaxial", "Internal", "XLR"
1627 switch (rme32->pci->device) {
1628 case PCI_DEVICE_ID_RME_DIGI32:
1629 case PCI_DEVICE_ID_RME_DIGI32_8:
1632 case PCI_DEVICE_ID_RME_DIGI32_PRO:
1639 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
1642 snd_rme32_get_inputtype_control(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
1645 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1646 unsigned int items = 3;
1648 spin_lock_irq(&rme32->lock);
1649 ucontrol->value.enumerated.item[0] = snd_rme32_getinputtype(rme32);
1651 switch (rme32->pci->device) {
1652 case PCI_DEVICE_ID_RME_DIGI32:
1653 case PCI_DEVICE_ID_RME_DIGI32_8:
1656 case PCI_DEVICE_ID_RME_DIGI32_PRO:
1663 if (ucontrol->value.enumerated.item[0] >= items) {
1664 ucontrol->value.enumerated.item[0] = items - 1;
1667 spin_unlock_irq(&rme32->lock);
1671 snd_rme32_put_inputtype_control(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol)
1674 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1676 int change, items = 3;
1678 switch (rme32->pci->device) {
1679 case PCI_DEVICE_ID_RME_DIGI32:
1680 case PCI_DEVICE_ID_RME_DIGI32_8:
1683 case PCI_DEVICE_ID_RME_DIGI32_PRO:
1690 val = ucontrol->value.enumerated.item[0] % items;
1692 spin_lock_irq(&rme32->lock);
1693 change = val != (unsigned int)snd_rme32_getinputtype(rme32);
1694 snd_rme32_setinputtype(rme32, val);
1695 spin_unlock_irq(&rme32->lock);
1700 snd_rme32_info_clockmode_control(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_info *uinfo)
1703 static const char * const texts[4] = { "AutoSync",
1706 "Internal 48.0kHz" };
1708 return snd_ctl_enum_info(uinfo, 1, 4, texts);
1711 snd_rme32_get_clockmode_control(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1714 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1716 spin_lock_irq(&rme32->lock);
1717 ucontrol->value.enumerated.item[0] = snd_rme32_getclockmode(rme32);
1718 spin_unlock_irq(&rme32->lock);
1722 snd_rme32_put_clockmode_control(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_value *ucontrol)
1725 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1729 val = ucontrol->value.enumerated.item[0] % 3;
1730 spin_lock_irq(&rme32->lock);
1731 change = val != (unsigned int)snd_rme32_getclockmode(rme32);
1732 snd_rme32_setclockmode(rme32, val);
1733 spin_unlock_irq(&rme32->lock);
1737 static u32 snd_rme32_convert_from_aes(struct snd_aes_iec958 * aes)
1740 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME32_WCR_PRO : 0;
1741 if (val & RME32_WCR_PRO)
1742 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
1744 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME32_WCR_EMP : 0;
1748 static void snd_rme32_convert_to_aes(struct snd_aes_iec958 * aes, u32 val)
1750 aes->status[0] = ((val & RME32_WCR_PRO) ? IEC958_AES0_PROFESSIONAL : 0);
1751 if (val & RME32_WCR_PRO)
1752 aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
1754 aes->status[0] |= (val & RME32_WCR_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
1757 static int snd_rme32_control_spdif_info(struct snd_kcontrol *kcontrol,
1758 struct snd_ctl_elem_info *uinfo)
1760 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1765 static int snd_rme32_control_spdif_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1768 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1770 snd_rme32_convert_to_aes(&ucontrol->value.iec958,
1771 rme32->wcreg_spdif);
1775 static int snd_rme32_control_spdif_put(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
1778 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1782 val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
1783 spin_lock_irq(&rme32->lock);
1784 change = val != rme32->wcreg_spdif;
1785 rme32->wcreg_spdif = val;
1786 spin_unlock_irq(&rme32->lock);
1790 static int snd_rme32_control_spdif_stream_info(struct snd_kcontrol *kcontrol,
1791 struct snd_ctl_elem_info *uinfo)
1793 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1798 static int snd_rme32_control_spdif_stream_get(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *
1802 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1804 snd_rme32_convert_to_aes(&ucontrol->value.iec958,
1805 rme32->wcreg_spdif_stream);
1809 static int snd_rme32_control_spdif_stream_put(struct snd_kcontrol *kcontrol,
1810 struct snd_ctl_elem_value *
1813 struct rme32 *rme32 = snd_kcontrol_chip(kcontrol);
1817 val = snd_rme32_convert_from_aes(&ucontrol->value.iec958);
1818 spin_lock_irq(&rme32->lock);
1819 change = val != rme32->wcreg_spdif_stream;
1820 rme32->wcreg_spdif_stream = val;
1821 rme32->wcreg &= ~(RME32_WCR_PRO | RME32_WCR_EMP);
1822 rme32->wcreg |= val;
1823 writel(rme32->wcreg, rme32->iobase + RME32_IO_CONTROL_REGISTER);
1824 spin_unlock_irq(&rme32->lock);
1828 static int snd_rme32_control_spdif_mask_info(struct snd_kcontrol *kcontrol,
1829 struct snd_ctl_elem_info *uinfo)
1831 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1836 static int snd_rme32_control_spdif_mask_get(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_value *
1840 ucontrol->value.iec958.status[0] = kcontrol->private_value;
1844 static struct snd_kcontrol_new snd_rme32_controls[] = {
1846 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1847 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1848 .info = snd_rme32_control_spdif_info,
1849 .get = snd_rme32_control_spdif_get,
1850 .put = snd_rme32_control_spdif_put
1853 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1854 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1855 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
1856 .info = snd_rme32_control_spdif_stream_info,
1857 .get = snd_rme32_control_spdif_stream_get,
1858 .put = snd_rme32_control_spdif_stream_put
1861 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1862 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1863 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1864 .info = snd_rme32_control_spdif_mask_info,
1865 .get = snd_rme32_control_spdif_mask_get,
1866 .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_CON_EMPHASIS
1869 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1870 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1871 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1872 .info = snd_rme32_control_spdif_mask_info,
1873 .get = snd_rme32_control_spdif_mask_get,
1874 .private_value = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_EMPHASIS
1877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1878 .name = "Input Connector",
1879 .info = snd_rme32_info_inputtype_control,
1880 .get = snd_rme32_get_inputtype_control,
1881 .put = snd_rme32_put_inputtype_control
1884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1885 .name = "Loopback Input",
1886 .info = snd_rme32_info_loopback_control,
1887 .get = snd_rme32_get_loopback_control,
1888 .put = snd_rme32_put_loopback_control
1891 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1892 .name = "Sample Clock Source",
1893 .info = snd_rme32_info_clockmode_control,
1894 .get = snd_rme32_get_clockmode_control,
1895 .put = snd_rme32_put_clockmode_control
1899 static int snd_rme32_create_switches(struct snd_card *card, struct rme32 * rme32)
1902 struct snd_kcontrol *kctl;
1904 for (idx = 0; idx < (int)ARRAY_SIZE(snd_rme32_controls); idx++) {
1905 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme32_controls[idx], rme32))) < 0)
1907 if (idx == 1) /* IEC958 (S/PDIF) Stream */
1908 rme32->spdif_ctl = kctl;
1915 * Card initialisation
1918 static void snd_rme32_card_free(struct snd_card *card)
1920 snd_rme32_free(card->private_data);
1924 snd_rme32_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1927 struct rme32 *rme32;
1928 struct snd_card *card;
1931 if (dev >= SNDRV_CARDS) {
1939 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1940 sizeof(struct rme32), &card);
1943 card->private_free = snd_rme32_card_free;
1944 rme32 = (struct rme32 *) card->private_data;
1947 if (fullduplex[dev])
1948 rme32->fullduplex_mode = 1;
1949 if ((err = snd_rme32_create(rme32)) < 0) {
1950 snd_card_free(card);
1954 strcpy(card->driver, "Digi32");
1955 switch (rme32->pci->device) {
1956 case PCI_DEVICE_ID_RME_DIGI32:
1957 strcpy(card->shortname, "RME Digi32");
1959 case PCI_DEVICE_ID_RME_DIGI32_8:
1960 strcpy(card->shortname, "RME Digi32/8");
1962 case PCI_DEVICE_ID_RME_DIGI32_PRO:
1963 strcpy(card->shortname, "RME Digi32 PRO");
1966 sprintf(card->longname, "%s (Rev. %d) at 0x%lx, irq %d",
1967 card->shortname, rme32->rev, rme32->port, rme32->irq);
1969 if ((err = snd_card_register(card)) < 0) {
1970 snd_card_free(card);
1973 pci_set_drvdata(pci, card);
1978 static void snd_rme32_remove(struct pci_dev *pci)
1980 snd_card_free(pci_get_drvdata(pci));
1983 static struct pci_driver rme32_driver = {
1984 .name = KBUILD_MODNAME,
1985 .id_table = snd_rme32_ids,
1986 .probe = snd_rme32_probe,
1987 .remove = snd_rme32_remove,
1990 module_pci_driver(rme32_driver);