[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / sound / pci / rme9652 / rme9652.c
blob69cd81eaa111bc976932fd2a5838ff81b662add3
1 /*
2 * ALSA driver for RME Digi9652 audio interfaces
4 * Copyright (c) 1999 IEM - Winfried Ritsch
5 * Copyright (c) 1999-2001 Paul Davis
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/info.h>
35 #include <sound/asoundef.h>
36 #include <sound/initval.h>
38 #include <asm/current.h>
39 #include <asm/io.h>
41 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
42 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
43 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
44 static int precise_ptr[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 }; /* Enable precise pointer */
46 module_param_array(index, int, NULL, 0444);
47 MODULE_PARM_DESC(index, "Index value for RME Digi9652 (Hammerfall) soundcard.");
48 module_param_array(id, charp, NULL, 0444);
49 MODULE_PARM_DESC(id, "ID string for RME Digi9652 (Hammerfall) soundcard.");
50 module_param_array(enable, bool, NULL, 0444);
51 MODULE_PARM_DESC(enable, "Enable/disable specific RME96{52,36} soundcards.");
52 module_param_array(precise_ptr, bool, NULL, 0444);
53 MODULE_PARM_DESC(precise_ptr, "Enable precise pointer (doesn't work reliably).");
54 MODULE_AUTHOR("Paul Davis <pbd@op.net>, Winfried Ritsch");
55 MODULE_DESCRIPTION("RME Digi9652/Digi9636");
56 MODULE_LICENSE("GPL");
57 MODULE_SUPPORTED_DEVICE("{{RME,Hammerfall},"
58 "{RME,Hammerfall-Light}}");
60 /* The Hammerfall has two sets of 24 ADAT + 2 S/PDIF channels, one for
61 capture, one for playback. Both the ADAT and S/PDIF channels appear
62 to the host CPU in the same block of memory. There is no functional
63 difference between them in terms of access.
65 The Hammerfall Light is identical to the Hammerfall, except that it
66 has 2 sets 18 channels (16 ADAT + 2 S/PDIF) for capture and playback.
69 #define RME9652_NCHANNELS 26
70 #define RME9636_NCHANNELS 18
72 /* Preferred sync source choices - used by "sync_pref" control switch */
74 #define RME9652_SYNC_FROM_SPDIF 0
75 #define RME9652_SYNC_FROM_ADAT1 1
76 #define RME9652_SYNC_FROM_ADAT2 2
77 #define RME9652_SYNC_FROM_ADAT3 3
79 /* Possible sources of S/PDIF input */
81 #define RME9652_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
82 #define RME9652_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
83 #define RME9652_SPDIFIN_INTERN 2 /* internal (CDROM) */
85 /* ------------- Status-Register bits --------------------- */
87 #define RME9652_IRQ (1<<0) /* IRQ is High if not reset by irq_clear */
88 #define RME9652_lock_2 (1<<1) /* ADAT 3-PLL: 1=locked, 0=unlocked */
89 #define RME9652_lock_1 (1<<2) /* ADAT 2-PLL: 1=locked, 0=unlocked */
90 #define RME9652_lock_0 (1<<3) /* ADAT 1-PLL: 1=locked, 0=unlocked */
91 #define RME9652_fs48 (1<<4) /* sample rate is 0=44.1/88.2,1=48/96 Khz */
92 #define RME9652_wsel_rd (1<<5) /* if Word-Clock is used and valid then 1 */
93 /* bits 6-15 encode h/w buffer pointer position */
94 #define RME9652_sync_2 (1<<16) /* if ADAT-IN 3 in sync to system clock */
95 #define RME9652_sync_1 (1<<17) /* if ADAT-IN 2 in sync to system clock */
96 #define RME9652_sync_0 (1<<18) /* if ADAT-IN 1 in sync to system clock */
97 #define RME9652_DS_rd (1<<19) /* 1=Double Speed Mode, 0=Normal Speed */
98 #define RME9652_tc_busy (1<<20) /* 1=time-code copy in progress (960ms) */
99 #define RME9652_tc_out (1<<21) /* time-code out bit */
100 #define RME9652_F_0 (1<<22) /* 000=64kHz, 100=88.2kHz, 011=96kHz */
101 #define RME9652_F_1 (1<<23) /* 111=32kHz, 110=44.1kHz, 101=48kHz, */
102 #define RME9652_F_2 (1<<24) /* external Crystal Chip if ERF=1 */
103 #define RME9652_ERF (1<<25) /* Error-Flag of SDPIF Receiver (1=No Lock) */
104 #define RME9652_buffer_id (1<<26) /* toggles by each interrupt on rec/play */
105 #define RME9652_tc_valid (1<<27) /* 1 = a signal is detected on time-code input */
106 #define RME9652_SPDIF_READ (1<<28) /* byte available from Rev 1.5+ S/PDIF interface */
108 #define RME9652_sync (RME9652_sync_0|RME9652_sync_1|RME9652_sync_2)
109 #define RME9652_lock (RME9652_lock_0|RME9652_lock_1|RME9652_lock_2)
110 #define RME9652_F (RME9652_F_0|RME9652_F_1|RME9652_F_2)
111 #define rme9652_decode_spdif_rate(x) ((x)>>22)
113 /* Bit 6..15 : h/w buffer pointer */
115 #define RME9652_buf_pos 0x000FFC0
117 /* Bits 31,30,29 are bits 5,4,3 of h/w pointer position on later
118 Rev G EEPROMS and Rev 1.5 cards or later.
121 #define RME9652_REV15_buf_pos(x) ((((x)&0xE0000000)>>26)|((x)&RME9652_buf_pos))
123 #ifndef PCI_VENDOR_ID_XILINX
124 #define PCI_VENDOR_ID_XILINX 0x10ee
125 #endif
126 #ifndef PCI_DEVICE_ID_XILINX_HAMMERFALL
127 #define PCI_DEVICE_ID_XILINX_HAMMERFALL 0x3fc4
128 #endif
130 /* amount of io space we remap for register access. i'm not sure we
131 even need this much, but 1K is nice round number :)
134 #define RME9652_IO_EXTENT 1024
136 #define RME9652_init_buffer 0
137 #define RME9652_play_buffer 32 /* holds ptr to 26x64kBit host RAM */
138 #define RME9652_rec_buffer 36 /* holds ptr to 26x64kBit host RAM */
139 #define RME9652_control_register 64
140 #define RME9652_irq_clear 96
141 #define RME9652_time_code 100 /* useful if used with alesis adat */
142 #define RME9652_thru_base 128 /* 132...228 Thru for 26 channels */
144 /* Read-only registers */
146 /* Writing to any of the register locations writes to the status
147 register. We'll use the first location as our point of access.
150 #define RME9652_status_register 0
152 /* --------- Control-Register Bits ---------------- */
155 #define RME9652_start_bit (1<<0) /* start record/play */
156 /* bits 1-3 encode buffersize/latency */
157 #define RME9652_Master (1<<4) /* Clock Mode Master=1,Slave/Auto=0 */
158 #define RME9652_IE (1<<5) /* Interupt Enable */
159 #define RME9652_freq (1<<6) /* samplerate 0=44.1/88.2, 1=48/96 kHz */
160 #define RME9652_freq1 (1<<7) /* if 0, 32kHz, else always 1 */
161 #define RME9652_DS (1<<8) /* Doule Speed 0=44.1/48, 1=88.2/96 Khz */
162 #define RME9652_PRO (1<<9) /* S/PDIF out: 0=consumer, 1=professional */
163 #define RME9652_EMP (1<<10) /* Emphasis 0=None, 1=ON */
164 #define RME9652_Dolby (1<<11) /* Non-audio bit 1=set, 0=unset */
165 #define RME9652_opt_out (1<<12) /* Use 1st optical OUT as SPDIF: 1=yes,0=no */
166 #define RME9652_wsel (1<<13) /* use Wordclock as sync (overwrites master) */
167 #define RME9652_inp_0 (1<<14) /* SPDIF-IN: 00=optical (ADAT1), */
168 #define RME9652_inp_1 (1<<15) /* 01=koaxial (Cinch), 10=Internal CDROM */
169 #define RME9652_SyncPref_ADAT2 (1<<16)
170 #define RME9652_SyncPref_ADAT3 (1<<17)
171 #define RME9652_SPDIF_RESET (1<<18) /* Rev 1.5+: h/w S/PDIF receiver */
172 #define RME9652_SPDIF_SELECT (1<<19)
173 #define RME9652_SPDIF_CLOCK (1<<20)
174 #define RME9652_SPDIF_WRITE (1<<21)
175 #define RME9652_ADAT1_INTERNAL (1<<22) /* Rev 1.5+: if set, internal CD connector carries ADAT */
177 /* buffersize = 512Bytes * 2^n, where n is made from Bit2 ... Bit0 */
179 #define RME9652_latency 0x0e
180 #define rme9652_encode_latency(x) (((x)&0x7)<<1)
181 #define rme9652_decode_latency(x) (((x)>>1)&0x7)
182 #define rme9652_running_double_speed(s) ((s)->control_register & RME9652_DS)
183 #define RME9652_inp (RME9652_inp_0|RME9652_inp_1)
184 #define rme9652_encode_spdif_in(x) (((x)&0x3)<<14)
185 #define rme9652_decode_spdif_in(x) (((x)>>14)&0x3)
187 #define RME9652_SyncPref_Mask (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
188 #define RME9652_SyncPref_ADAT1 0
189 #define RME9652_SyncPref_SPDIF (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
191 /* the size of a substream (1 mono data stream) */
193 #define RME9652_CHANNEL_BUFFER_SAMPLES (16*1024)
194 #define RME9652_CHANNEL_BUFFER_BYTES (4*RME9652_CHANNEL_BUFFER_SAMPLES)
196 /* the size of the area we need to allocate for DMA transfers. the
197 size is the same regardless of the number of channels - the
198 9636 still uses the same memory area.
200 Note that we allocate 1 more channel than is apparently needed
201 because the h/w seems to write 1 byte beyond the end of the last
202 page. Sigh.
205 #define RME9652_DMA_AREA_BYTES ((RME9652_NCHANNELS+1) * RME9652_CHANNEL_BUFFER_BYTES)
206 #define RME9652_DMA_AREA_KILOBYTES (RME9652_DMA_AREA_BYTES/1024)
208 typedef struct snd_rme9652 {
209 int dev;
211 spinlock_t lock;
212 int irq;
213 unsigned long port;
214 void __iomem *iobase;
216 int precise_ptr;
218 u32 control_register; /* cached value */
219 u32 thru_bits; /* thru 1=on, 0=off channel 1=Bit1... channel 26= Bit26 */
221 u32 creg_spdif;
222 u32 creg_spdif_stream;
224 char *card_name; /* hammerfall or hammerfall light names */
226 size_t hw_offsetmask; /* &-with status register to get real hw_offset */
227 size_t prev_hw_offset; /* previous hw offset */
228 size_t max_jitter; /* maximum jitter in frames for
229 hw pointer */
230 size_t period_bytes; /* guess what this is */
232 unsigned char ds_channels;
233 unsigned char ss_channels; /* different for hammerfall/hammerfall-light */
235 struct snd_dma_buffer playback_dma_buf;
236 struct snd_dma_buffer capture_dma_buf;
238 unsigned char *capture_buffer; /* suitably aligned address */
239 unsigned char *playback_buffer; /* suitably aligned address */
241 pid_t capture_pid;
242 pid_t playback_pid;
244 snd_pcm_substream_t *capture_substream;
245 snd_pcm_substream_t *playback_substream;
246 int running;
248 int passthru; /* non-zero if doing pass-thru */
249 int hw_rev; /* h/w rev * 10 (i.e. 1.5 has hw_rev = 15) */
251 int last_spdif_sample_rate; /* so that we can catch externally ... */
252 int last_adat_sample_rate; /* ... induced rate changes */
254 char *channel_map;
256 snd_card_t *card;
257 snd_pcm_t *pcm;
258 struct pci_dev *pci;
259 snd_kcontrol_t *spdif_ctl;
261 } rme9652_t;
263 /* These tables map the ALSA channels 1..N to the channels that we
264 need to use in order to find the relevant channel buffer. RME
265 refer to this kind of mapping as between "the ADAT channel and
266 the DMA channel." We index it using the logical audio channel,
267 and the value is the DMA channel (i.e. channel buffer number)
268 where the data for that channel can be read/written from/to.
271 static char channel_map_9652_ss[26] = {
272 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
273 18, 19, 20, 21, 22, 23, 24, 25
276 static char channel_map_9636_ss[26] = {
277 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
278 /* channels 16 and 17 are S/PDIF */
279 24, 25,
280 /* channels 18-25 don't exist */
281 -1, -1, -1, -1, -1, -1, -1, -1
284 static char channel_map_9652_ds[26] = {
285 /* ADAT channels are remapped */
286 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
287 /* channels 12 and 13 are S/PDIF */
288 24, 25,
289 /* others don't exist */
290 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
293 static char channel_map_9636_ds[26] = {
294 /* ADAT channels are remapped */
295 1, 3, 5, 7, 9, 11, 13, 15,
296 /* channels 8 and 9 are S/PDIF */
297 24, 25
298 /* others don't exist */
299 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
302 static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
304 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
305 dmab->dev.dev = snd_dma_pci_data(pci);
306 if (! snd_dma_get_reserved_buf(dmab, snd_dma_pci_buf_id(pci))) {
307 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
308 size, dmab) < 0)
309 return -ENOMEM;
311 return 0;
314 static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
316 if (dmab->area)
317 snd_dma_reserve_buf(dmab, snd_dma_pci_buf_id(pci));
321 static struct pci_device_id snd_rme9652_ids[] = {
323 .vendor = 0x10ee,
324 .device = 0x3fc4,
325 .subvendor = PCI_ANY_ID,
326 .subdevice = PCI_ANY_ID,
327 }, /* RME Digi9652 */
328 { 0, },
331 MODULE_DEVICE_TABLE(pci, snd_rme9652_ids);
333 static inline void rme9652_write(rme9652_t *rme9652, int reg, int val)
335 writel(val, rme9652->iobase + reg);
338 static inline unsigned int rme9652_read(rme9652_t *rme9652, int reg)
340 return readl(rme9652->iobase + reg);
343 static inline int snd_rme9652_use_is_exclusive(rme9652_t *rme9652)
345 unsigned long flags;
346 int ret = 1;
348 spin_lock_irqsave(&rme9652->lock, flags);
349 if ((rme9652->playback_pid != rme9652->capture_pid) &&
350 (rme9652->playback_pid >= 0) && (rme9652->capture_pid >= 0)) {
351 ret = 0;
353 spin_unlock_irqrestore(&rme9652->lock, flags);
354 return ret;
357 static inline int rme9652_adat_sample_rate(rme9652_t *rme9652)
359 if (rme9652_running_double_speed(rme9652)) {
360 return (rme9652_read(rme9652, RME9652_status_register) &
361 RME9652_fs48) ? 96000 : 88200;
362 } else {
363 return (rme9652_read(rme9652, RME9652_status_register) &
364 RME9652_fs48) ? 48000 : 44100;
368 static inline void rme9652_compute_period_size(rme9652_t *rme9652)
370 unsigned int i;
372 i = rme9652->control_register & RME9652_latency;
373 rme9652->period_bytes = 1 << ((rme9652_decode_latency(i) + 8));
374 rme9652->hw_offsetmask =
375 (rme9652->period_bytes * 2 - 1) & RME9652_buf_pos;
376 rme9652->max_jitter = 80;
379 static snd_pcm_uframes_t rme9652_hw_pointer(rme9652_t *rme9652)
381 int status;
382 unsigned int offset, frag;
383 snd_pcm_uframes_t period_size = rme9652->period_bytes / 4;
384 snd_pcm_sframes_t delta;
386 status = rme9652_read(rme9652, RME9652_status_register);
387 if (!rme9652->precise_ptr)
388 return (status & RME9652_buffer_id) ? period_size : 0;
389 offset = status & RME9652_buf_pos;
391 /* The hardware may give a backward movement for up to 80 frames
392 Martin Kirst <martin.kirst@freenet.de> knows the details.
395 delta = rme9652->prev_hw_offset - offset;
396 delta &= 0xffff;
397 if (delta <= (snd_pcm_sframes_t)rme9652->max_jitter * 4)
398 offset = rme9652->prev_hw_offset;
399 else
400 rme9652->prev_hw_offset = offset;
401 offset &= rme9652->hw_offsetmask;
402 offset /= 4;
403 frag = status & RME9652_buffer_id;
405 if (offset < period_size) {
406 if (offset > rme9652->max_jitter) {
407 if (frag)
408 printk(KERN_ERR "Unexpected hw_pointer position (bufid == 0): status: %x offset: %d\n", status, offset);
409 } else if (!frag)
410 return 0;
411 offset -= rme9652->max_jitter;
412 if (offset < 0)
413 offset += period_size * 2;
414 } else {
415 if (offset > period_size + rme9652->max_jitter) {
416 if (!frag)
417 printk(KERN_ERR "Unexpected hw_pointer position (bufid == 1): status: %x offset: %d\n", status, offset);
418 } else if (frag)
419 return period_size;
420 offset -= rme9652->max_jitter;
423 return offset;
426 static inline void rme9652_reset_hw_pointer(rme9652_t *rme9652)
428 int i;
430 /* reset the FIFO pointer to zero. We do this by writing to 8
431 registers, each of which is a 32bit wide register, and set
432 them all to zero. Note that s->iobase is a pointer to
433 int32, not pointer to char.
436 for (i = 0; i < 8; i++) {
437 rme9652_write(rme9652, i * 4, 0);
438 udelay(10);
440 rme9652->prev_hw_offset = 0;
443 static inline void rme9652_start(rme9652_t *s)
445 s->control_register |= (RME9652_IE | RME9652_start_bit);
446 rme9652_write(s, RME9652_control_register, s->control_register);
449 static inline void rme9652_stop(rme9652_t *s)
451 s->control_register &= ~(RME9652_start_bit | RME9652_IE);
452 rme9652_write(s, RME9652_control_register, s->control_register);
455 static int rme9652_set_interrupt_interval(rme9652_t *s,
456 unsigned int frames)
458 int restart = 0;
459 int n;
461 spin_lock_irq(&s->lock);
463 if ((restart = s->running)) {
464 rme9652_stop(s);
467 frames >>= 7;
468 n = 0;
469 while (frames) {
470 n++;
471 frames >>= 1;
474 s->control_register &= ~RME9652_latency;
475 s->control_register |= rme9652_encode_latency(n);
477 rme9652_write(s, RME9652_control_register, s->control_register);
479 rme9652_compute_period_size(s);
481 if (restart)
482 rme9652_start(s);
484 spin_unlock_irq(&s->lock);
486 return 0;
489 static int rme9652_set_rate(rme9652_t *rme9652, int rate)
491 int restart;
492 int reject_if_open = 0;
493 int xrate;
495 if (!snd_rme9652_use_is_exclusive (rme9652)) {
496 return -EBUSY;
499 /* Changing from a "single speed" to a "double speed" rate is
500 not allowed if any substreams are open. This is because
501 such a change causes a shift in the location of
502 the DMA buffers and a reduction in the number of available
503 buffers.
505 Note that a similar but essentially insoluble problem
506 exists for externally-driven rate changes. All we can do
507 is to flag rate changes in the read/write routines.
510 spin_lock_irq(&rme9652->lock);
511 xrate = rme9652_adat_sample_rate(rme9652);
513 switch (rate) {
514 case 44100:
515 if (xrate > 48000) {
516 reject_if_open = 1;
518 rate = 0;
519 break;
520 case 48000:
521 if (xrate > 48000) {
522 reject_if_open = 1;
524 rate = RME9652_freq;
525 break;
526 case 88200:
527 if (xrate < 48000) {
528 reject_if_open = 1;
530 rate = RME9652_DS;
531 break;
532 case 96000:
533 if (xrate < 48000) {
534 reject_if_open = 1;
536 rate = RME9652_DS | RME9652_freq;
537 break;
538 default:
539 spin_unlock_irq(&rme9652->lock);
540 return -EINVAL;
543 if (reject_if_open && (rme9652->capture_pid >= 0 || rme9652->playback_pid >= 0)) {
544 spin_unlock_irq(&rme9652->lock);
545 return -EBUSY;
548 if ((restart = rme9652->running)) {
549 rme9652_stop(rme9652);
551 rme9652->control_register &= ~(RME9652_freq | RME9652_DS);
552 rme9652->control_register |= rate;
553 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
555 if (restart) {
556 rme9652_start(rme9652);
559 if (rate & RME9652_DS) {
560 if (rme9652->ss_channels == RME9652_NCHANNELS) {
561 rme9652->channel_map = channel_map_9652_ds;
562 } else {
563 rme9652->channel_map = channel_map_9636_ds;
565 } else {
566 if (rme9652->ss_channels == RME9652_NCHANNELS) {
567 rme9652->channel_map = channel_map_9652_ss;
568 } else {
569 rme9652->channel_map = channel_map_9636_ss;
573 spin_unlock_irq(&rme9652->lock);
574 return 0;
577 static void rme9652_set_thru(rme9652_t *rme9652, int channel, int enable)
579 int i;
581 rme9652->passthru = 0;
583 if (channel < 0) {
585 /* set thru for all channels */
587 if (enable) {
588 for (i = 0; i < RME9652_NCHANNELS; i++) {
589 rme9652->thru_bits |= (1 << i);
590 rme9652_write(rme9652, RME9652_thru_base + i * 4, 1);
592 } else {
593 for (i = 0; i < RME9652_NCHANNELS; i++) {
594 rme9652->thru_bits &= ~(1 << i);
595 rme9652_write(rme9652, RME9652_thru_base + i * 4, 0);
599 } else {
600 int mapped_channel;
602 snd_assert(channel == RME9652_NCHANNELS, return);
604 mapped_channel = rme9652->channel_map[channel];
606 if (enable) {
607 rme9652->thru_bits |= (1 << mapped_channel);
608 } else {
609 rme9652->thru_bits &= ~(1 << mapped_channel);
612 rme9652_write(rme9652,
613 RME9652_thru_base + mapped_channel * 4,
614 enable ? 1 : 0);
618 static int rme9652_set_passthru(rme9652_t *rme9652, int onoff)
620 if (onoff) {
621 rme9652_set_thru(rme9652, -1, 1);
623 /* we don't want interrupts, so do a
624 custom version of rme9652_start().
627 rme9652->control_register =
628 RME9652_inp_0 |
629 rme9652_encode_latency(7) |
630 RME9652_start_bit;
632 rme9652_reset_hw_pointer(rme9652);
634 rme9652_write(rme9652, RME9652_control_register,
635 rme9652->control_register);
636 rme9652->passthru = 1;
637 } else {
638 rme9652_set_thru(rme9652, -1, 0);
639 rme9652_stop(rme9652);
640 rme9652->passthru = 0;
643 return 0;
646 static void rme9652_spdif_set_bit (rme9652_t *rme9652, int mask, int onoff)
648 if (onoff)
649 rme9652->control_register |= mask;
650 else
651 rme9652->control_register &= ~mask;
653 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
656 static void rme9652_spdif_write_byte (rme9652_t *rme9652, const int val)
658 long mask;
659 long i;
661 for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
662 if (val & mask)
663 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 1);
664 else
665 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 0);
667 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
668 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
672 static int rme9652_spdif_read_byte (rme9652_t *rme9652)
674 long mask;
675 long val;
676 long i;
678 val = 0;
680 for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
681 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
682 if (rme9652_read (rme9652, RME9652_status_register) & RME9652_SPDIF_READ)
683 val |= mask;
684 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
687 return val;
690 static void rme9652_write_spdif_codec (rme9652_t *rme9652, const int address, const int data)
692 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
693 rme9652_spdif_write_byte (rme9652, 0x20);
694 rme9652_spdif_write_byte (rme9652, address);
695 rme9652_spdif_write_byte (rme9652, data);
696 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
700 static int rme9652_spdif_read_codec (rme9652_t *rme9652, const int address)
702 int ret;
704 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
705 rme9652_spdif_write_byte (rme9652, 0x20);
706 rme9652_spdif_write_byte (rme9652, address);
707 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
708 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
710 rme9652_spdif_write_byte (rme9652, 0x21);
711 ret = rme9652_spdif_read_byte (rme9652);
712 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
714 return ret;
717 static void rme9652_initialize_spdif_receiver (rme9652_t *rme9652)
719 /* XXX what unsets this ? */
721 rme9652->control_register |= RME9652_SPDIF_RESET;
723 rme9652_write_spdif_codec (rme9652, 4, 0x40);
724 rme9652_write_spdif_codec (rme9652, 17, 0x13);
725 rme9652_write_spdif_codec (rme9652, 6, 0x02);
728 static inline int rme9652_spdif_sample_rate(rme9652_t *s)
730 unsigned int rate_bits;
732 if (rme9652_read(s, RME9652_status_register) & RME9652_ERF) {
733 return -1; /* error condition */
736 if (s->hw_rev == 15) {
738 int x, y, ret;
740 x = rme9652_spdif_read_codec (s, 30);
742 if (x != 0)
743 y = 48000 * 64 / x;
744 else
745 y = 0;
747 if (y > 30400 && y < 33600) ret = 32000;
748 else if (y > 41900 && y < 46000) ret = 44100;
749 else if (y > 46000 && y < 50400) ret = 48000;
750 else if (y > 60800 && y < 67200) ret = 64000;
751 else if (y > 83700 && y < 92000) ret = 88200;
752 else if (y > 92000 && y < 100000) ret = 96000;
753 else ret = 0;
754 return ret;
757 rate_bits = rme9652_read(s, RME9652_status_register) & RME9652_F;
759 switch (rme9652_decode_spdif_rate(rate_bits)) {
760 case 0x7:
761 return 32000;
762 break;
764 case 0x6:
765 return 44100;
766 break;
768 case 0x5:
769 return 48000;
770 break;
772 case 0x4:
773 return 88200;
774 break;
776 case 0x3:
777 return 96000;
778 break;
780 case 0x0:
781 return 64000;
782 break;
784 default:
785 snd_printk("%s: unknown S/PDIF input rate (bits = 0x%x)\n",
786 s->card_name, rate_bits);
787 return 0;
788 break;
792 /*-----------------------------------------------------------------------------
793 Control Interface
794 ----------------------------------------------------------------------------*/
796 static u32 snd_rme9652_convert_from_aes(snd_aes_iec958_t *aes)
798 u32 val = 0;
799 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME9652_PRO : 0;
800 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME9652_Dolby : 0;
801 if (val & RME9652_PRO)
802 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME9652_EMP : 0;
803 else
804 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME9652_EMP : 0;
805 return val;
808 static void snd_rme9652_convert_to_aes(snd_aes_iec958_t *aes, u32 val)
810 aes->status[0] = ((val & RME9652_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
811 ((val & RME9652_Dolby) ? IEC958_AES0_NONAUDIO : 0);
812 if (val & RME9652_PRO)
813 aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
814 else
815 aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
818 static int snd_rme9652_control_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
820 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
821 uinfo->count = 1;
822 return 0;
825 static int snd_rme9652_control_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
827 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
829 snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif);
830 return 0;
833 static int snd_rme9652_control_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
835 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
836 int change;
837 u32 val;
839 val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
840 spin_lock_irq(&rme9652->lock);
841 change = val != rme9652->creg_spdif;
842 rme9652->creg_spdif = val;
843 spin_unlock_irq(&rme9652->lock);
844 return change;
847 static int snd_rme9652_control_spdif_stream_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
849 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
850 uinfo->count = 1;
851 return 0;
854 static int snd_rme9652_control_spdif_stream_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
856 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
858 snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif_stream);
859 return 0;
862 static int snd_rme9652_control_spdif_stream_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
864 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
865 int change;
866 u32 val;
868 val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
869 spin_lock_irq(&rme9652->lock);
870 change = val != rme9652->creg_spdif_stream;
871 rme9652->creg_spdif_stream = val;
872 rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
873 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= val);
874 spin_unlock_irq(&rme9652->lock);
875 return change;
878 static int snd_rme9652_control_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
880 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
881 uinfo->count = 1;
882 return 0;
885 static int snd_rme9652_control_spdif_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
887 ucontrol->value.iec958.status[0] = kcontrol->private_value;
888 return 0;
891 #define RME9652_ADAT1_IN(xname, xindex) \
892 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
893 .info = snd_rme9652_info_adat1_in, \
894 .get = snd_rme9652_get_adat1_in, \
895 .put = snd_rme9652_put_adat1_in }
897 static unsigned int rme9652_adat1_in(rme9652_t *rme9652)
899 if (rme9652->control_register & RME9652_ADAT1_INTERNAL)
900 return 1;
901 return 0;
904 static int rme9652_set_adat1_input(rme9652_t *rme9652, int internal)
906 int restart = 0;
908 if (internal) {
909 rme9652->control_register |= RME9652_ADAT1_INTERNAL;
910 } else {
911 rme9652->control_register &= ~RME9652_ADAT1_INTERNAL;
914 /* XXX do we actually need to stop the card when we do this ? */
916 if ((restart = rme9652->running)) {
917 rme9652_stop(rme9652);
920 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
922 if (restart) {
923 rme9652_start(rme9652);
926 return 0;
929 static int snd_rme9652_info_adat1_in(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
931 static char *texts[2] = {"ADAT1", "Internal"};
933 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
934 uinfo->count = 1;
935 uinfo->value.enumerated.items = 2;
936 if (uinfo->value.enumerated.item > 1)
937 uinfo->value.enumerated.item = 1;
938 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
939 return 0;
942 static int snd_rme9652_get_adat1_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
944 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
946 spin_lock_irq(&rme9652->lock);
947 ucontrol->value.enumerated.item[0] = rme9652_adat1_in(rme9652);
948 spin_unlock_irq(&rme9652->lock);
949 return 0;
952 static int snd_rme9652_put_adat1_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
954 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
955 int change;
956 unsigned int val;
958 if (!snd_rme9652_use_is_exclusive(rme9652))
959 return -EBUSY;
960 val = ucontrol->value.enumerated.item[0] % 2;
961 spin_lock_irq(&rme9652->lock);
962 change = val != rme9652_adat1_in(rme9652);
963 if (change)
964 rme9652_set_adat1_input(rme9652, val);
965 spin_unlock_irq(&rme9652->lock);
966 return change;
969 #define RME9652_SPDIF_IN(xname, xindex) \
970 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
971 .info = snd_rme9652_info_spdif_in, \
972 .get = snd_rme9652_get_spdif_in, .put = snd_rme9652_put_spdif_in }
974 static unsigned int rme9652_spdif_in(rme9652_t *rme9652)
976 return rme9652_decode_spdif_in(rme9652->control_register &
977 RME9652_inp);
980 static int rme9652_set_spdif_input(rme9652_t *rme9652, int in)
982 int restart = 0;
984 rme9652->control_register &= ~RME9652_inp;
985 rme9652->control_register |= rme9652_encode_spdif_in(in);
987 if ((restart = rme9652->running)) {
988 rme9652_stop(rme9652);
991 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
993 if (restart) {
994 rme9652_start(rme9652);
997 return 0;
1000 static int snd_rme9652_info_spdif_in(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1002 static char *texts[3] = {"ADAT1", "Coaxial", "Internal"};
1004 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1005 uinfo->count = 1;
1006 uinfo->value.enumerated.items = 3;
1007 if (uinfo->value.enumerated.item > 2)
1008 uinfo->value.enumerated.item = 2;
1009 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1010 return 0;
1013 static int snd_rme9652_get_spdif_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1015 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1017 spin_lock_irq(&rme9652->lock);
1018 ucontrol->value.enumerated.item[0] = rme9652_spdif_in(rme9652);
1019 spin_unlock_irq(&rme9652->lock);
1020 return 0;
1023 static int snd_rme9652_put_spdif_in(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1025 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1026 int change;
1027 unsigned int val;
1029 if (!snd_rme9652_use_is_exclusive(rme9652))
1030 return -EBUSY;
1031 val = ucontrol->value.enumerated.item[0] % 3;
1032 spin_lock_irq(&rme9652->lock);
1033 change = val != rme9652_spdif_in(rme9652);
1034 if (change)
1035 rme9652_set_spdif_input(rme9652, val);
1036 spin_unlock_irq(&rme9652->lock);
1037 return change;
1040 #define RME9652_SPDIF_OUT(xname, xindex) \
1041 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1042 .info = snd_rme9652_info_spdif_out, \
1043 .get = snd_rme9652_get_spdif_out, .put = snd_rme9652_put_spdif_out }
1045 static int rme9652_spdif_out(rme9652_t *rme9652)
1047 return (rme9652->control_register & RME9652_opt_out) ? 1 : 0;
1050 static int rme9652_set_spdif_output(rme9652_t *rme9652, int out)
1052 int restart = 0;
1054 if (out) {
1055 rme9652->control_register |= RME9652_opt_out;
1056 } else {
1057 rme9652->control_register &= ~RME9652_opt_out;
1060 if ((restart = rme9652->running)) {
1061 rme9652_stop(rme9652);
1064 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1066 if (restart) {
1067 rme9652_start(rme9652);
1070 return 0;
1073 static int snd_rme9652_info_spdif_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1075 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1076 uinfo->count = 1;
1077 uinfo->value.integer.min = 0;
1078 uinfo->value.integer.max = 1;
1079 return 0;
1082 static int snd_rme9652_get_spdif_out(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1084 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1086 spin_lock_irq(&rme9652->lock);
1087 ucontrol->value.integer.value[0] = rme9652_spdif_out(rme9652);
1088 spin_unlock_irq(&rme9652->lock);
1089 return 0;
1092 static int snd_rme9652_put_spdif_out(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1094 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1095 int change;
1096 unsigned int val;
1098 if (!snd_rme9652_use_is_exclusive(rme9652))
1099 return -EBUSY;
1100 val = ucontrol->value.integer.value[0] & 1;
1101 spin_lock_irq(&rme9652->lock);
1102 change = (int)val != rme9652_spdif_out(rme9652);
1103 rme9652_set_spdif_output(rme9652, val);
1104 spin_unlock_irq(&rme9652->lock);
1105 return change;
1108 #define RME9652_SYNC_MODE(xname, xindex) \
1109 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1110 .info = snd_rme9652_info_sync_mode, \
1111 .get = snd_rme9652_get_sync_mode, .put = snd_rme9652_put_sync_mode }
1113 static int rme9652_sync_mode(rme9652_t *rme9652)
1115 if (rme9652->control_register & RME9652_wsel) {
1116 return 2;
1117 } else if (rme9652->control_register & RME9652_Master) {
1118 return 1;
1119 } else {
1120 return 0;
1124 static int rme9652_set_sync_mode(rme9652_t *rme9652, int mode)
1126 int restart = 0;
1128 switch (mode) {
1129 case 0:
1130 rme9652->control_register &=
1131 ~(RME9652_Master | RME9652_wsel);
1132 break;
1133 case 1:
1134 rme9652->control_register =
1135 (rme9652->control_register & ~RME9652_wsel) | RME9652_Master;
1136 break;
1137 case 2:
1138 rme9652->control_register |=
1139 (RME9652_Master | RME9652_wsel);
1140 break;
1143 if ((restart = rme9652->running)) {
1144 rme9652_stop(rme9652);
1147 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1149 if (restart) {
1150 rme9652_start(rme9652);
1153 return 0;
1156 static int snd_rme9652_info_sync_mode(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1158 static char *texts[3] = {"AutoSync", "Master", "Word Clock"};
1160 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1161 uinfo->count = 1;
1162 uinfo->value.enumerated.items = 3;
1163 if (uinfo->value.enumerated.item > 2)
1164 uinfo->value.enumerated.item = 2;
1165 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1166 return 0;
1169 static int snd_rme9652_get_sync_mode(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1171 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1173 spin_lock_irq(&rme9652->lock);
1174 ucontrol->value.enumerated.item[0] = rme9652_sync_mode(rme9652);
1175 spin_unlock_irq(&rme9652->lock);
1176 return 0;
1179 static int snd_rme9652_put_sync_mode(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1181 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1182 int change;
1183 unsigned int val;
1185 val = ucontrol->value.enumerated.item[0] % 3;
1186 spin_lock_irq(&rme9652->lock);
1187 change = (int)val != rme9652_sync_mode(rme9652);
1188 rme9652_set_sync_mode(rme9652, val);
1189 spin_unlock_irq(&rme9652->lock);
1190 return change;
1193 #define RME9652_SYNC_PREF(xname, xindex) \
1194 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1195 .info = snd_rme9652_info_sync_pref, \
1196 .get = snd_rme9652_get_sync_pref, .put = snd_rme9652_put_sync_pref }
1198 static int rme9652_sync_pref(rme9652_t *rme9652)
1200 switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1201 case RME9652_SyncPref_ADAT1:
1202 return RME9652_SYNC_FROM_ADAT1;
1203 case RME9652_SyncPref_ADAT2:
1204 return RME9652_SYNC_FROM_ADAT2;
1205 case RME9652_SyncPref_ADAT3:
1206 return RME9652_SYNC_FROM_ADAT3;
1207 case RME9652_SyncPref_SPDIF:
1208 return RME9652_SYNC_FROM_SPDIF;
1210 /* Not reachable */
1211 return 0;
1214 static int rme9652_set_sync_pref(rme9652_t *rme9652, int pref)
1216 int restart;
1218 rme9652->control_register &= ~RME9652_SyncPref_Mask;
1219 switch (pref) {
1220 case RME9652_SYNC_FROM_ADAT1:
1221 rme9652->control_register |= RME9652_SyncPref_ADAT1;
1222 break;
1223 case RME9652_SYNC_FROM_ADAT2:
1224 rme9652->control_register |= RME9652_SyncPref_ADAT2;
1225 break;
1226 case RME9652_SYNC_FROM_ADAT3:
1227 rme9652->control_register |= RME9652_SyncPref_ADAT3;
1228 break;
1229 case RME9652_SYNC_FROM_SPDIF:
1230 rme9652->control_register |= RME9652_SyncPref_SPDIF;
1231 break;
1234 if ((restart = rme9652->running)) {
1235 rme9652_stop(rme9652);
1238 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1240 if (restart) {
1241 rme9652_start(rme9652);
1244 return 0;
1247 static int snd_rme9652_info_sync_pref(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1249 static char *texts[4] = {"IEC958 In", "ADAT1 In", "ADAT2 In", "ADAT3 In"};
1250 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1252 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1253 uinfo->count = 1;
1254 uinfo->value.enumerated.items = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
1255 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1256 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1257 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1258 return 0;
1261 static int snd_rme9652_get_sync_pref(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1263 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1265 spin_lock_irq(&rme9652->lock);
1266 ucontrol->value.enumerated.item[0] = rme9652_sync_pref(rme9652);
1267 spin_unlock_irq(&rme9652->lock);
1268 return 0;
1271 static int snd_rme9652_put_sync_pref(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1273 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1274 int change, max;
1275 unsigned int val;
1277 if (!snd_rme9652_use_is_exclusive(rme9652))
1278 return -EBUSY;
1279 max = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
1280 val = ucontrol->value.enumerated.item[0] % max;
1281 spin_lock_irq(&rme9652->lock);
1282 change = (int)val != rme9652_sync_pref(rme9652);
1283 rme9652_set_sync_pref(rme9652, val);
1284 spin_unlock_irq(&rme9652->lock);
1285 return change;
1288 static int snd_rme9652_info_thru(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1290 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1291 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1292 uinfo->count = rme9652->ss_channels;
1293 uinfo->value.integer.min = 0;
1294 uinfo->value.integer.max = 1;
1295 return 0;
1298 static int snd_rme9652_get_thru(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1300 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1301 unsigned int k;
1302 u32 thru_bits = rme9652->thru_bits;
1304 for (k = 0; k < rme9652->ss_channels; ++k) {
1305 ucontrol->value.integer.value[k] = !!(thru_bits & (1 << k));
1307 return 0;
1310 static int snd_rme9652_put_thru(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1312 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1313 int change;
1314 unsigned int chn;
1315 u32 thru_bits = 0;
1317 if (!snd_rme9652_use_is_exclusive(rme9652))
1318 return -EBUSY;
1320 for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1321 if (ucontrol->value.integer.value[chn])
1322 thru_bits |= 1 << chn;
1325 spin_lock_irq(&rme9652->lock);
1326 change = thru_bits ^ rme9652->thru_bits;
1327 if (change) {
1328 for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1329 if (!(change & (1 << chn)))
1330 continue;
1331 rme9652_set_thru(rme9652,chn,thru_bits&(1<<chn));
1334 spin_unlock_irq(&rme9652->lock);
1335 return !!change;
1338 #define RME9652_PASSTHRU(xname, xindex) \
1339 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1340 .info = snd_rme9652_info_passthru, \
1341 .put = snd_rme9652_put_passthru, \
1342 .get = snd_rme9652_get_passthru }
1344 static int snd_rme9652_info_passthru(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
1346 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1347 uinfo->count = 1;
1348 uinfo->value.integer.min = 0;
1349 uinfo->value.integer.max = 1;
1350 return 0;
1353 static int snd_rme9652_get_passthru(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1355 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1357 spin_lock_irq(&rme9652->lock);
1358 ucontrol->value.integer.value[0] = rme9652->passthru;
1359 spin_unlock_irq(&rme9652->lock);
1360 return 0;
1363 static int snd_rme9652_put_passthru(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1365 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1366 int change;
1367 unsigned int val;
1368 int err = 0;
1370 if (!snd_rme9652_use_is_exclusive(rme9652))
1371 return -EBUSY;
1373 val = ucontrol->value.integer.value[0] & 1;
1374 spin_lock_irq(&rme9652->lock);
1375 change = (ucontrol->value.integer.value[0] != rme9652->passthru);
1376 if (change)
1377 err = rme9652_set_passthru(rme9652, val);
1378 spin_unlock_irq(&rme9652->lock);
1379 return err ? err : change;
1382 /* Read-only switches */
1384 #define RME9652_SPDIF_RATE(xname, xindex) \
1385 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1386 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1387 .info = snd_rme9652_info_spdif_rate, \
1388 .get = snd_rme9652_get_spdif_rate }
1390 static int snd_rme9652_info_spdif_rate(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1392 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1393 uinfo->count = 1;
1394 uinfo->value.integer.min = 0;
1395 uinfo->value.integer.max = 96000;
1396 return 0;
1399 static int snd_rme9652_get_spdif_rate(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1401 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1403 spin_lock_irq(&rme9652->lock);
1404 ucontrol->value.integer.value[0] = rme9652_spdif_sample_rate(rme9652);
1405 spin_unlock_irq(&rme9652->lock);
1406 return 0;
1409 #define RME9652_ADAT_SYNC(xname, xindex, xidx) \
1410 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1411 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1412 .info = snd_rme9652_info_adat_sync, \
1413 .get = snd_rme9652_get_adat_sync, .private_value = xidx }
1415 static int snd_rme9652_info_adat_sync(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1417 static char *texts[4] = {"No Lock", "Lock", "No Lock Sync", "Lock Sync"};
1419 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1420 uinfo->count = 1;
1421 uinfo->value.enumerated.items = 4;
1422 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1423 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1424 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1425 return 0;
1428 static int snd_rme9652_get_adat_sync(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1430 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1431 unsigned int mask1, mask2, val;
1433 switch (kcontrol->private_value) {
1434 case 0: mask1 = RME9652_lock_0; mask2 = RME9652_sync_0; break;
1435 case 1: mask1 = RME9652_lock_1; mask2 = RME9652_sync_1; break;
1436 case 2: mask1 = RME9652_lock_2; mask2 = RME9652_sync_2; break;
1437 default: return -EINVAL;
1439 val = rme9652_read(rme9652, RME9652_status_register);
1440 ucontrol->value.enumerated.item[0] = (val & mask1) ? 1 : 0;
1441 ucontrol->value.enumerated.item[0] |= (val & mask2) ? 2 : 0;
1442 return 0;
1445 #define RME9652_TC_VALID(xname, xindex) \
1446 { .iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = xname, .index = xindex, \
1447 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1448 .info = snd_rme9652_info_tc_valid, \
1449 .get = snd_rme9652_get_tc_valid }
1451 static int snd_rme9652_info_tc_valid(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1453 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1454 uinfo->count = 1;
1455 uinfo->value.integer.min = 0;
1456 uinfo->value.integer.max = 1;
1457 return 0;
1460 static int snd_rme9652_get_tc_valid(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1462 rme9652_t *rme9652 = snd_kcontrol_chip(kcontrol);
1464 ucontrol->value.integer.value[0] =
1465 (rme9652_read(rme9652, RME9652_status_register) & RME9652_tc_valid) ? 1 : 0;
1466 return 0;
1469 #if ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE
1471 /* FIXME: this routine needs a port to the new control API --jk */
1473 static int snd_rme9652_get_tc_value(void *private_data,
1474 snd_kswitch_t *kswitch,
1475 snd_switch_t *uswitch)
1477 rme9652_t *s = (rme9652_t *) private_data;
1478 u32 value;
1479 int i;
1481 uswitch->type = SNDRV_SW_TYPE_DWORD;
1483 if ((rme9652_read(s, RME9652_status_register) &
1484 RME9652_tc_valid) == 0) {
1485 uswitch->value.data32[0] = 0;
1486 return 0;
1489 /* timecode request */
1491 rme9652_write(s, RME9652_time_code, 0);
1493 /* XXX bug alert: loop-based timing !!!! */
1495 for (i = 0; i < 50; i++) {
1496 if (!(rme9652_read(s, i * 4) & RME9652_tc_busy))
1497 break;
1500 if (!(rme9652_read(s, i * 4) & RME9652_tc_busy)) {
1501 return -EIO;
1504 value = 0;
1506 for (i = 0; i < 32; i++) {
1507 value >>= 1;
1509 if (rme9652_read(s, i * 4) & RME9652_tc_out)
1510 value |= 0x80000000;
1513 if (value > 2 * 60 * 48000) {
1514 value -= 2 * 60 * 48000;
1515 } else {
1516 value = 0;
1519 uswitch->value.data32[0] = value;
1521 return 0;
1524 #endif /* ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE */
1526 static snd_kcontrol_new_t snd_rme9652_controls[] = {
1528 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1529 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1530 .info = snd_rme9652_control_spdif_info,
1531 .get = snd_rme9652_control_spdif_get,
1532 .put = snd_rme9652_control_spdif_put,
1535 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1536 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1537 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1538 .info = snd_rme9652_control_spdif_stream_info,
1539 .get = snd_rme9652_control_spdif_stream_get,
1540 .put = snd_rme9652_control_spdif_stream_put,
1543 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1544 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1545 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1546 .info = snd_rme9652_control_spdif_mask_info,
1547 .get = snd_rme9652_control_spdif_mask_get,
1548 .private_value = IEC958_AES0_NONAUDIO |
1549 IEC958_AES0_PROFESSIONAL |
1550 IEC958_AES0_CON_EMPHASIS,
1553 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1554 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1555 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1556 .info = snd_rme9652_control_spdif_mask_info,
1557 .get = snd_rme9652_control_spdif_mask_get,
1558 .private_value = IEC958_AES0_NONAUDIO |
1559 IEC958_AES0_PROFESSIONAL |
1560 IEC958_AES0_PRO_EMPHASIS,
1562 RME9652_SPDIF_IN("IEC958 Input Connector", 0),
1563 RME9652_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
1564 RME9652_SYNC_MODE("Sync Mode", 0),
1565 RME9652_SYNC_PREF("Preferred Sync Source", 0),
1567 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1568 .name = "Channels Thru",
1569 .index = 0,
1570 .info = snd_rme9652_info_thru,
1571 .get = snd_rme9652_get_thru,
1572 .put = snd_rme9652_put_thru,
1574 RME9652_SPDIF_RATE("IEC958 Sample Rate", 0),
1575 RME9652_ADAT_SYNC("ADAT1 Sync Check", 0, 0),
1576 RME9652_ADAT_SYNC("ADAT2 Sync Check", 0, 1),
1577 RME9652_TC_VALID("Timecode Valid", 0),
1578 RME9652_PASSTHRU("Passthru", 0)
1581 static snd_kcontrol_new_t snd_rme9652_adat3_check =
1582 RME9652_ADAT_SYNC("ADAT3 Sync Check", 0, 2);
1584 static snd_kcontrol_new_t snd_rme9652_adat1_input =
1585 RME9652_ADAT1_IN("ADAT1 Input Source", 0);
1587 static int snd_rme9652_create_controls(snd_card_t *card, rme9652_t *rme9652)
1589 unsigned int idx;
1590 int err;
1591 snd_kcontrol_t *kctl;
1593 for (idx = 0; idx < ARRAY_SIZE(snd_rme9652_controls); idx++) {
1594 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_controls[idx], rme9652))) < 0)
1595 return err;
1596 if (idx == 1) /* IEC958 (S/PDIF) Stream */
1597 rme9652->spdif_ctl = kctl;
1600 if (rme9652->ss_channels == RME9652_NCHANNELS)
1601 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat3_check, rme9652))) < 0)
1602 return err;
1604 if (rme9652->hw_rev >= 15)
1605 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat1_input, rme9652))) < 0)
1606 return err;
1608 return 0;
1611 /*------------------------------------------------------------
1612 /proc interface
1613 ------------------------------------------------------------*/
1615 static void
1616 snd_rme9652_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1618 rme9652_t *rme9652 = (rme9652_t *) entry->private_data;
1619 u32 thru_bits = rme9652->thru_bits;
1620 int show_auto_sync_source = 0;
1621 int i;
1622 unsigned int status;
1623 int x;
1625 status = rme9652_read(rme9652, RME9652_status_register);
1627 snd_iprintf(buffer, "%s (Card #%d)\n", rme9652->card_name, rme9652->card->number + 1);
1628 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
1629 rme9652->capture_buffer, rme9652->playback_buffer);
1630 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
1631 rme9652->irq, rme9652->port, (unsigned long)rme9652->iobase);
1632 snd_iprintf(buffer, "Control register: %x\n", rme9652->control_register);
1634 snd_iprintf(buffer, "\n");
1636 x = 1 << (6 + rme9652_decode_latency(rme9652->control_register &
1637 RME9652_latency));
1639 snd_iprintf(buffer, "Latency: %d samples (2 periods of %lu bytes)\n",
1640 x, (unsigned long) rme9652->period_bytes);
1641 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n",
1642 rme9652_hw_pointer(rme9652));
1643 snd_iprintf(buffer, "Passthru: %s\n",
1644 rme9652->passthru ? "yes" : "no");
1646 if ((rme9652->control_register & (RME9652_Master | RME9652_wsel)) == 0) {
1647 snd_iprintf(buffer, "Clock mode: autosync\n");
1648 show_auto_sync_source = 1;
1649 } else if (rme9652->control_register & RME9652_wsel) {
1650 if (status & RME9652_wsel_rd) {
1651 snd_iprintf(buffer, "Clock mode: word clock\n");
1652 } else {
1653 snd_iprintf(buffer, "Clock mode: word clock (no signal)\n");
1655 } else {
1656 snd_iprintf(buffer, "Clock mode: master\n");
1659 if (show_auto_sync_source) {
1660 switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1661 case RME9652_SyncPref_ADAT1:
1662 snd_iprintf(buffer, "Pref. sync source: ADAT1\n");
1663 break;
1664 case RME9652_SyncPref_ADAT2:
1665 snd_iprintf(buffer, "Pref. sync source: ADAT2\n");
1666 break;
1667 case RME9652_SyncPref_ADAT3:
1668 snd_iprintf(buffer, "Pref. sync source: ADAT3\n");
1669 break;
1670 case RME9652_SyncPref_SPDIF:
1671 snd_iprintf(buffer, "Pref. sync source: IEC958\n");
1672 break;
1673 default:
1674 snd_iprintf(buffer, "Pref. sync source: ???\n");
1678 if (rme9652->hw_rev >= 15)
1679 snd_iprintf(buffer, "\nADAT1 Input source: %s\n",
1680 (rme9652->control_register & RME9652_ADAT1_INTERNAL) ?
1681 "Internal" : "ADAT1 optical");
1683 snd_iprintf(buffer, "\n");
1685 switch (rme9652_decode_spdif_in(rme9652->control_register &
1686 RME9652_inp)) {
1687 case RME9652_SPDIFIN_OPTICAL:
1688 snd_iprintf(buffer, "IEC958 input: ADAT1\n");
1689 break;
1690 case RME9652_SPDIFIN_COAXIAL:
1691 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
1692 break;
1693 case RME9652_SPDIFIN_INTERN:
1694 snd_iprintf(buffer, "IEC958 input: Internal\n");
1695 break;
1696 default:
1697 snd_iprintf(buffer, "IEC958 input: ???\n");
1698 break;
1701 if (rme9652->control_register & RME9652_opt_out) {
1702 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
1703 } else {
1704 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
1707 if (rme9652->control_register & RME9652_PRO) {
1708 snd_iprintf(buffer, "IEC958 quality: Professional\n");
1709 } else {
1710 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
1713 if (rme9652->control_register & RME9652_EMP) {
1714 snd_iprintf(buffer, "IEC958 emphasis: on\n");
1715 } else {
1716 snd_iprintf(buffer, "IEC958 emphasis: off\n");
1719 if (rme9652->control_register & RME9652_Dolby) {
1720 snd_iprintf(buffer, "IEC958 Dolby: on\n");
1721 } else {
1722 snd_iprintf(buffer, "IEC958 Dolby: off\n");
1725 i = rme9652_spdif_sample_rate(rme9652);
1727 if (i < 0) {
1728 snd_iprintf(buffer,
1729 "IEC958 sample rate: error flag set\n");
1730 } else if (i == 0) {
1731 snd_iprintf(buffer, "IEC958 sample rate: undetermined\n");
1732 } else {
1733 snd_iprintf(buffer, "IEC958 sample rate: %d\n", i);
1736 snd_iprintf(buffer, "\n");
1738 snd_iprintf(buffer, "ADAT Sample rate: %dHz\n",
1739 rme9652_adat_sample_rate(rme9652));
1741 /* Sync Check */
1743 x = status & RME9652_sync_0;
1744 if (status & RME9652_lock_0) {
1745 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
1746 } else {
1747 snd_iprintf(buffer, "ADAT1: No Lock\n");
1750 x = status & RME9652_sync_1;
1751 if (status & RME9652_lock_1) {
1752 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
1753 } else {
1754 snd_iprintf(buffer, "ADAT2: No Lock\n");
1757 x = status & RME9652_sync_2;
1758 if (status & RME9652_lock_2) {
1759 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
1760 } else {
1761 snd_iprintf(buffer, "ADAT3: No Lock\n");
1764 snd_iprintf(buffer, "\n");
1766 snd_iprintf(buffer, "Timecode signal: %s\n",
1767 (status & RME9652_tc_valid) ? "yes" : "no");
1769 /* thru modes */
1771 snd_iprintf(buffer, "Punch Status:\n\n");
1773 for (i = 0; i < rme9652->ss_channels; i++) {
1774 if (thru_bits & (1 << i)) {
1775 snd_iprintf(buffer, "%2d: on ", i + 1);
1776 } else {
1777 snd_iprintf(buffer, "%2d: off ", i + 1);
1780 if (((i + 1) % 8) == 0) {
1781 snd_iprintf(buffer, "\n");
1785 snd_iprintf(buffer, "\n");
1788 static void __devinit snd_rme9652_proc_init(rme9652_t *rme9652)
1790 snd_info_entry_t *entry;
1792 if (! snd_card_proc_new(rme9652->card, "rme9652", &entry))
1793 snd_info_set_text_ops(entry, rme9652, 1024, snd_rme9652_proc_read);
1796 static void snd_rme9652_free_buffers(rme9652_t *rme9652)
1798 snd_hammerfall_free_buffer(&rme9652->capture_dma_buf, rme9652->pci);
1799 snd_hammerfall_free_buffer(&rme9652->playback_dma_buf, rme9652->pci);
1802 static int snd_rme9652_free(rme9652_t *rme9652)
1804 if (rme9652->irq >= 0)
1805 rme9652_stop(rme9652);
1806 snd_rme9652_free_buffers(rme9652);
1808 if (rme9652->irq >= 0)
1809 free_irq(rme9652->irq, (void *)rme9652);
1810 if (rme9652->iobase)
1811 iounmap(rme9652->iobase);
1812 if (rme9652->port)
1813 pci_release_regions(rme9652->pci);
1815 pci_disable_device(rme9652->pci);
1816 return 0;
1819 static int __devinit snd_rme9652_initialize_memory(rme9652_t *rme9652)
1821 unsigned long pb_bus, cb_bus;
1823 if (snd_hammerfall_get_buffer(rme9652->pci, &rme9652->capture_dma_buf, RME9652_DMA_AREA_BYTES) < 0 ||
1824 snd_hammerfall_get_buffer(rme9652->pci, &rme9652->playback_dma_buf, RME9652_DMA_AREA_BYTES) < 0) {
1825 if (rme9652->capture_dma_buf.area)
1826 snd_dma_free_pages(&rme9652->capture_dma_buf);
1827 printk(KERN_ERR "%s: no buffers available\n", rme9652->card_name);
1828 return -ENOMEM;
1831 /* Align to bus-space 64K boundary */
1833 cb_bus = (rme9652->capture_dma_buf.addr + 0xFFFF) & ~0xFFFFl;
1834 pb_bus = (rme9652->playback_dma_buf.addr + 0xFFFF) & ~0xFFFFl;
1836 /* Tell the card where it is */
1838 rme9652_write(rme9652, RME9652_rec_buffer, cb_bus);
1839 rme9652_write(rme9652, RME9652_play_buffer, pb_bus);
1841 rme9652->capture_buffer = rme9652->capture_dma_buf.area + (cb_bus - rme9652->capture_dma_buf.addr);
1842 rme9652->playback_buffer = rme9652->playback_dma_buf.area + (pb_bus - rme9652->playback_dma_buf.addr);
1844 return 0;
1847 static void snd_rme9652_set_defaults(rme9652_t *rme9652)
1849 unsigned int k;
1851 /* ASSUMPTION: rme9652->lock is either held, or
1852 there is no need to hold it (e.g. during module
1853 initalization).
1856 /* set defaults:
1858 SPDIF Input via Coax
1859 autosync clock mode
1860 maximum latency (7 = 8192 samples, 64Kbyte buffer,
1861 which implies 2 4096 sample, 32Kbyte periods).
1863 if rev 1.5, initialize the S/PDIF receiver.
1867 rme9652->control_register =
1868 RME9652_inp_0 | rme9652_encode_latency(7);
1870 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1872 rme9652_reset_hw_pointer(rme9652);
1873 rme9652_compute_period_size(rme9652);
1875 /* default: thru off for all channels */
1877 for (k = 0; k < RME9652_NCHANNELS; ++k)
1878 rme9652_write(rme9652, RME9652_thru_base + k * 4, 0);
1880 rme9652->thru_bits = 0;
1881 rme9652->passthru = 0;
1883 /* set a default rate so that the channel map is set up */
1885 rme9652_set_rate(rme9652, 48000);
1888 static irqreturn_t snd_rme9652_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1890 rme9652_t *rme9652 = (rme9652_t *) dev_id;
1892 if (!(rme9652_read(rme9652, RME9652_status_register) & RME9652_IRQ)) {
1893 return IRQ_NONE;
1896 rme9652_write(rme9652, RME9652_irq_clear, 0);
1898 if (rme9652->capture_substream) {
1899 snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
1902 if (rme9652->playback_substream) {
1903 snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
1905 return IRQ_HANDLED;
1908 static snd_pcm_uframes_t snd_rme9652_hw_pointer(snd_pcm_substream_t *substream)
1910 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
1911 return rme9652_hw_pointer(rme9652);
1914 static char *rme9652_channel_buffer_location(rme9652_t *rme9652,
1915 int stream,
1916 int channel)
1919 int mapped_channel;
1921 snd_assert(channel >= 0 || channel < RME9652_NCHANNELS, return NULL);
1923 if ((mapped_channel = rme9652->channel_map[channel]) < 0) {
1924 return NULL;
1927 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1928 return rme9652->capture_buffer +
1929 (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1930 } else {
1931 return rme9652->playback_buffer +
1932 (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1936 static int snd_rme9652_playback_copy(snd_pcm_substream_t *substream, int channel,
1937 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
1939 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
1940 char *channel_buf;
1942 snd_assert(pos + count <= RME9652_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
1944 channel_buf = rme9652_channel_buffer_location (rme9652,
1945 substream->pstr->stream,
1946 channel);
1947 snd_assert(channel_buf != NULL, return -EIO);
1948 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
1949 return -EFAULT;
1950 return count;
1953 static int snd_rme9652_capture_copy(snd_pcm_substream_t *substream, int channel,
1954 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
1956 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
1957 char *channel_buf;
1959 snd_assert(pos + count <= RME9652_CHANNEL_BUFFER_BYTES / 4, return -EINVAL);
1961 channel_buf = rme9652_channel_buffer_location (rme9652,
1962 substream->pstr->stream,
1963 channel);
1964 snd_assert(channel_buf != NULL, return -EIO);
1965 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
1966 return -EFAULT;
1967 return count;
1970 static int snd_rme9652_hw_silence(snd_pcm_substream_t *substream, int channel,
1971 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
1973 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
1974 char *channel_buf;
1976 channel_buf = rme9652_channel_buffer_location (rme9652,
1977 substream->pstr->stream,
1978 channel);
1979 snd_assert(channel_buf != NULL, return -EIO);
1980 memset(channel_buf + pos * 4, 0, count * 4);
1981 return count;
1984 static int snd_rme9652_reset(snd_pcm_substream_t *substream)
1986 snd_pcm_runtime_t *runtime = substream->runtime;
1987 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
1988 snd_pcm_substream_t *other;
1989 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1990 other = rme9652->capture_substream;
1991 else
1992 other = rme9652->playback_substream;
1993 if (rme9652->running)
1994 runtime->status->hw_ptr = rme9652_hw_pointer(rme9652);
1995 else
1996 runtime->status->hw_ptr = 0;
1997 if (other) {
1998 struct list_head *pos;
1999 snd_pcm_substream_t *s;
2000 snd_pcm_runtime_t *oruntime = other->runtime;
2001 snd_pcm_group_for_each(pos, substream) {
2002 s = snd_pcm_group_substream_entry(pos);
2003 if (s == other) {
2004 oruntime->status->hw_ptr = runtime->status->hw_ptr;
2005 break;
2009 return 0;
2012 static int snd_rme9652_hw_params(snd_pcm_substream_t *substream,
2013 snd_pcm_hw_params_t *params)
2015 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2016 int err;
2017 pid_t this_pid;
2018 pid_t other_pid;
2020 spin_lock_irq(&rme9652->lock);
2022 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2023 rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
2024 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= rme9652->creg_spdif_stream);
2025 this_pid = rme9652->playback_pid;
2026 other_pid = rme9652->capture_pid;
2027 } else {
2028 this_pid = rme9652->capture_pid;
2029 other_pid = rme9652->playback_pid;
2032 if ((other_pid > 0) && (this_pid != other_pid)) {
2034 /* The other stream is open, and not by the same
2035 task as this one. Make sure that the parameters
2036 that matter are the same.
2039 if ((int)params_rate(params) !=
2040 rme9652_adat_sample_rate(rme9652)) {
2041 spin_unlock_irq(&rme9652->lock);
2042 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
2043 return -EBUSY;
2046 if (params_period_size(params) != rme9652->period_bytes / 4) {
2047 spin_unlock_irq(&rme9652->lock);
2048 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2049 return -EBUSY;
2052 /* We're fine. */
2054 spin_unlock_irq(&rme9652->lock);
2055 return 0;
2057 } else {
2058 spin_unlock_irq(&rme9652->lock);
2061 /* how to make sure that the rate matches an externally-set one ?
2064 if ((err = rme9652_set_rate(rme9652, params_rate(params))) < 0) {
2065 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
2066 return err;
2069 if ((err = rme9652_set_interrupt_interval(rme9652, params_period_size(params))) < 0) {
2070 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2071 return err;
2074 return 0;
2077 static int snd_rme9652_channel_info(snd_pcm_substream_t *substream,
2078 snd_pcm_channel_info_t *info)
2080 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2081 int chn;
2083 snd_assert(info->channel < RME9652_NCHANNELS, return -EINVAL);
2085 if ((chn = rme9652->channel_map[info->channel]) < 0) {
2086 return -EINVAL;
2089 info->offset = chn * RME9652_CHANNEL_BUFFER_BYTES;
2090 info->first = 0;
2091 info->step = 32;
2092 return 0;
2095 static int snd_rme9652_ioctl(snd_pcm_substream_t *substream,
2096 unsigned int cmd, void *arg)
2098 switch (cmd) {
2099 case SNDRV_PCM_IOCTL1_RESET:
2101 return snd_rme9652_reset(substream);
2103 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
2105 snd_pcm_channel_info_t *info = arg;
2106 return snd_rme9652_channel_info(substream, info);
2108 default:
2109 break;
2112 return snd_pcm_lib_ioctl(substream, cmd, arg);
2115 static void rme9652_silence_playback(rme9652_t *rme9652)
2117 memset(rme9652->playback_buffer, 0, RME9652_DMA_AREA_BYTES);
2120 static int snd_rme9652_trigger(snd_pcm_substream_t *substream,
2121 int cmd)
2123 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2124 snd_pcm_substream_t *other;
2125 int running;
2126 spin_lock(&rme9652->lock);
2127 running = rme9652->running;
2128 switch (cmd) {
2129 case SNDRV_PCM_TRIGGER_START:
2130 running |= 1 << substream->stream;
2131 break;
2132 case SNDRV_PCM_TRIGGER_STOP:
2133 running &= ~(1 << substream->stream);
2134 break;
2135 default:
2136 snd_BUG();
2137 spin_unlock(&rme9652->lock);
2138 return -EINVAL;
2140 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2141 other = rme9652->capture_substream;
2142 else
2143 other = rme9652->playback_substream;
2145 if (other) {
2146 struct list_head *pos;
2147 snd_pcm_substream_t *s;
2148 snd_pcm_group_for_each(pos, substream) {
2149 s = snd_pcm_group_substream_entry(pos);
2150 if (s == other) {
2151 snd_pcm_trigger_done(s, substream);
2152 if (cmd == SNDRV_PCM_TRIGGER_START)
2153 running |= 1 << s->stream;
2154 else
2155 running &= ~(1 << s->stream);
2156 goto _ok;
2159 if (cmd == SNDRV_PCM_TRIGGER_START) {
2160 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
2161 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2162 rme9652_silence_playback(rme9652);
2163 } else {
2164 if (running &&
2165 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2166 rme9652_silence_playback(rme9652);
2168 } else {
2169 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2170 rme9652_silence_playback(rme9652);
2172 _ok:
2173 snd_pcm_trigger_done(substream, substream);
2174 if (!rme9652->running && running)
2175 rme9652_start(rme9652);
2176 else if (rme9652->running && !running)
2177 rme9652_stop(rme9652);
2178 rme9652->running = running;
2179 spin_unlock(&rme9652->lock);
2181 return 0;
2184 static int snd_rme9652_prepare(snd_pcm_substream_t *substream)
2186 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2187 unsigned long flags;
2188 int result = 0;
2190 spin_lock_irqsave(&rme9652->lock, flags);
2191 if (!rme9652->running)
2192 rme9652_reset_hw_pointer(rme9652);
2193 spin_unlock_irqrestore(&rme9652->lock, flags);
2194 return result;
2197 static snd_pcm_hardware_t snd_rme9652_playback_subinfo =
2199 .info = (SNDRV_PCM_INFO_MMAP |
2200 SNDRV_PCM_INFO_MMAP_VALID |
2201 SNDRV_PCM_INFO_NONINTERLEAVED |
2202 SNDRV_PCM_INFO_SYNC_START |
2203 SNDRV_PCM_INFO_DOUBLE),
2204 .formats = SNDRV_PCM_FMTBIT_S32_LE,
2205 .rates = (SNDRV_PCM_RATE_44100 |
2206 SNDRV_PCM_RATE_48000 |
2207 SNDRV_PCM_RATE_88200 |
2208 SNDRV_PCM_RATE_96000),
2209 .rate_min = 44100,
2210 .rate_max = 96000,
2211 .channels_min = 10,
2212 .channels_max = 26,
2213 .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES * 26,
2214 .period_bytes_min = (64 * 4) * 10,
2215 .period_bytes_max = (8192 * 4) * 26,
2216 .periods_min = 2,
2217 .periods_max = 2,
2218 .fifo_size = 0,
2221 static snd_pcm_hardware_t snd_rme9652_capture_subinfo =
2223 .info = (SNDRV_PCM_INFO_MMAP |
2224 SNDRV_PCM_INFO_MMAP_VALID |
2225 SNDRV_PCM_INFO_NONINTERLEAVED |
2226 SNDRV_PCM_INFO_SYNC_START),
2227 .formats = SNDRV_PCM_FMTBIT_S32_LE,
2228 .rates = (SNDRV_PCM_RATE_44100 |
2229 SNDRV_PCM_RATE_48000 |
2230 SNDRV_PCM_RATE_88200 |
2231 SNDRV_PCM_RATE_96000),
2232 .rate_min = 44100,
2233 .rate_max = 96000,
2234 .channels_min = 10,
2235 .channels_max = 26,
2236 .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES *26,
2237 .period_bytes_min = (64 * 4) * 10,
2238 .period_bytes_max = (8192 * 4) * 26,
2239 .periods_min = 2,
2240 .periods_max = 2,
2241 .fifo_size = 0,
2244 static unsigned int period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
2246 static snd_pcm_hw_constraint_list_t hw_constraints_period_sizes = {
2247 .count = ARRAY_SIZE(period_sizes),
2248 .list = period_sizes,
2249 .mask = 0
2252 static int snd_rme9652_hw_rule_channels(snd_pcm_hw_params_t *params,
2253 snd_pcm_hw_rule_t *rule)
2255 rme9652_t *rme9652 = rule->private;
2256 snd_interval_t *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2257 unsigned int list[2] = { rme9652->ds_channels, rme9652->ss_channels };
2258 return snd_interval_list(c, 2, list, 0);
2261 static int snd_rme9652_hw_rule_channels_rate(snd_pcm_hw_params_t *params,
2262 snd_pcm_hw_rule_t *rule)
2264 rme9652_t *rme9652 = rule->private;
2265 snd_interval_t *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2266 snd_interval_t *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
2267 if (r->min > 48000) {
2268 snd_interval_t t = {
2269 .min = rme9652->ds_channels,
2270 .max = rme9652->ds_channels,
2271 .integer = 1,
2273 return snd_interval_refine(c, &t);
2274 } else if (r->max < 88200) {
2275 snd_interval_t t = {
2276 .min = rme9652->ss_channels,
2277 .max = rme9652->ss_channels,
2278 .integer = 1,
2280 return snd_interval_refine(c, &t);
2282 return 0;
2285 static int snd_rme9652_hw_rule_rate_channels(snd_pcm_hw_params_t *params,
2286 snd_pcm_hw_rule_t *rule)
2288 rme9652_t *rme9652 = rule->private;
2289 snd_interval_t *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2290 snd_interval_t *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
2291 if (c->min >= rme9652->ss_channels) {
2292 snd_interval_t t = {
2293 .min = 44100,
2294 .max = 48000,
2295 .integer = 1,
2297 return snd_interval_refine(r, &t);
2298 } else if (c->max <= rme9652->ds_channels) {
2299 snd_interval_t t = {
2300 .min = 88200,
2301 .max = 96000,
2302 .integer = 1,
2304 return snd_interval_refine(r, &t);
2306 return 0;
2309 static int snd_rme9652_playback_open(snd_pcm_substream_t *substream)
2311 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2312 snd_pcm_runtime_t *runtime = substream->runtime;
2314 spin_lock_irq(&rme9652->lock);
2316 snd_pcm_set_sync(substream);
2318 runtime->hw = snd_rme9652_playback_subinfo;
2319 runtime->dma_area = rme9652->playback_buffer;
2320 runtime->dma_bytes = RME9652_DMA_AREA_BYTES;
2322 if (rme9652->capture_substream == NULL) {
2323 rme9652_stop(rme9652);
2324 rme9652_set_thru(rme9652, -1, 0);
2327 rme9652->playback_pid = current->pid;
2328 rme9652->playback_substream = substream;
2330 spin_unlock_irq(&rme9652->lock);
2332 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2333 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2334 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2335 snd_rme9652_hw_rule_channels, rme9652,
2336 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2337 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2338 snd_rme9652_hw_rule_channels_rate, rme9652,
2339 SNDRV_PCM_HW_PARAM_RATE, -1);
2340 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2341 snd_rme9652_hw_rule_rate_channels, rme9652,
2342 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2344 rme9652->creg_spdif_stream = rme9652->creg_spdif;
2345 rme9652->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2346 snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2347 SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2348 return 0;
2351 static int snd_rme9652_playback_release(snd_pcm_substream_t *substream)
2353 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2355 spin_lock_irq(&rme9652->lock);
2357 rme9652->playback_pid = -1;
2358 rme9652->playback_substream = NULL;
2360 spin_unlock_irq(&rme9652->lock);
2362 rme9652->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2363 snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2364 SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2365 return 0;
2369 static int snd_rme9652_capture_open(snd_pcm_substream_t *substream)
2371 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2372 snd_pcm_runtime_t *runtime = substream->runtime;
2374 spin_lock_irq(&rme9652->lock);
2376 snd_pcm_set_sync(substream);
2378 runtime->hw = snd_rme9652_capture_subinfo;
2379 runtime->dma_area = rme9652->capture_buffer;
2380 runtime->dma_bytes = RME9652_DMA_AREA_BYTES;
2382 if (rme9652->playback_substream == NULL) {
2383 rme9652_stop(rme9652);
2384 rme9652_set_thru(rme9652, -1, 0);
2387 rme9652->capture_pid = current->pid;
2388 rme9652->capture_substream = substream;
2390 spin_unlock_irq(&rme9652->lock);
2392 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2393 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2394 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2395 snd_rme9652_hw_rule_channels, rme9652,
2396 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2397 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2398 snd_rme9652_hw_rule_channels_rate, rme9652,
2399 SNDRV_PCM_HW_PARAM_RATE, -1);
2400 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2401 snd_rme9652_hw_rule_rate_channels, rme9652,
2402 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2403 return 0;
2406 static int snd_rme9652_capture_release(snd_pcm_substream_t *substream)
2408 rme9652_t *rme9652 = snd_pcm_substream_chip(substream);
2410 spin_lock_irq(&rme9652->lock);
2412 rme9652->capture_pid = -1;
2413 rme9652->capture_substream = NULL;
2415 spin_unlock_irq(&rme9652->lock);
2416 return 0;
2419 static snd_pcm_ops_t snd_rme9652_playback_ops = {
2420 .open = snd_rme9652_playback_open,
2421 .close = snd_rme9652_playback_release,
2422 .ioctl = snd_rme9652_ioctl,
2423 .hw_params = snd_rme9652_hw_params,
2424 .prepare = snd_rme9652_prepare,
2425 .trigger = snd_rme9652_trigger,
2426 .pointer = snd_rme9652_hw_pointer,
2427 .copy = snd_rme9652_playback_copy,
2428 .silence = snd_rme9652_hw_silence,
2431 static snd_pcm_ops_t snd_rme9652_capture_ops = {
2432 .open = snd_rme9652_capture_open,
2433 .close = snd_rme9652_capture_release,
2434 .ioctl = snd_rme9652_ioctl,
2435 .hw_params = snd_rme9652_hw_params,
2436 .prepare = snd_rme9652_prepare,
2437 .trigger = snd_rme9652_trigger,
2438 .pointer = snd_rme9652_hw_pointer,
2439 .copy = snd_rme9652_capture_copy,
2442 static int __devinit snd_rme9652_create_pcm(snd_card_t *card,
2443 rme9652_t *rme9652)
2445 snd_pcm_t *pcm;
2446 int err;
2448 if ((err = snd_pcm_new(card,
2449 rme9652->card_name,
2450 0, 1, 1, &pcm)) < 0) {
2451 return err;
2454 rme9652->pcm = pcm;
2455 pcm->private_data = rme9652;
2456 strcpy(pcm->name, rme9652->card_name);
2458 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme9652_playback_ops);
2459 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme9652_capture_ops);
2461 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2463 return 0;
2466 static int __devinit snd_rme9652_create(snd_card_t *card,
2467 rme9652_t *rme9652,
2468 int precise_ptr)
2470 struct pci_dev *pci = rme9652->pci;
2471 int err;
2472 int status;
2473 unsigned short rev;
2475 rme9652->irq = -1;
2476 rme9652->card = card;
2478 pci_read_config_word(rme9652->pci, PCI_CLASS_REVISION, &rev);
2480 switch (rev & 0xff) {
2481 case 3:
2482 case 4:
2483 case 8:
2484 case 9:
2485 break;
2487 default:
2488 /* who knows? */
2489 return -ENODEV;
2492 if ((err = pci_enable_device(pci)) < 0)
2493 return err;
2495 spin_lock_init(&rme9652->lock);
2497 if ((err = pci_request_regions(pci, "rme9652")) < 0)
2498 return err;
2499 rme9652->port = pci_resource_start(pci, 0);
2500 rme9652->iobase = ioremap_nocache(rme9652->port, RME9652_IO_EXTENT);
2501 if (rme9652->iobase == NULL) {
2502 snd_printk("unable to remap region 0x%lx-0x%lx\n", rme9652->port, rme9652->port + RME9652_IO_EXTENT - 1);
2503 return -EBUSY;
2506 if (request_irq(pci->irq, snd_rme9652_interrupt, SA_INTERRUPT|SA_SHIRQ, "rme9652", (void *)rme9652)) {
2507 snd_printk("unable to request IRQ %d\n", pci->irq);
2508 return -EBUSY;
2510 rme9652->irq = pci->irq;
2511 rme9652->precise_ptr = precise_ptr;
2513 /* Determine the h/w rev level of the card. This seems like
2514 a particularly kludgy way to encode it, but its what RME
2515 chose to do, so we follow them ...
2518 status = rme9652_read(rme9652, RME9652_status_register);
2519 if (rme9652_decode_spdif_rate(status&RME9652_F) == 1) {
2520 rme9652->hw_rev = 15;
2521 } else {
2522 rme9652->hw_rev = 11;
2525 /* Differentiate between the standard Hammerfall, and the
2526 "Light", which does not have the expansion board. This
2527 method comes from information received from Mathhias
2528 Clausen at RME. Display the EEPROM and h/w revID where
2529 relevant.
2532 switch (rev) {
2533 case 8: /* original eprom */
2534 strcpy(card->driver, "RME9636");
2535 if (rme9652->hw_rev == 15) {
2536 rme9652->card_name = "RME Digi9636 (Rev 1.5)";
2537 } else {
2538 rme9652->card_name = "RME Digi9636";
2540 rme9652->ss_channels = RME9636_NCHANNELS;
2541 break;
2542 case 9: /* W36_G EPROM */
2543 strcpy(card->driver, "RME9636");
2544 rme9652->card_name = "RME Digi9636 (Rev G)";
2545 rme9652->ss_channels = RME9636_NCHANNELS;
2546 break;
2547 case 4: /* W52_G EPROM */
2548 strcpy(card->driver, "RME9652");
2549 rme9652->card_name = "RME Digi9652 (Rev G)";
2550 rme9652->ss_channels = RME9652_NCHANNELS;
2551 break;
2552 case 3: /* original eprom */
2553 strcpy(card->driver, "RME9652");
2554 if (rme9652->hw_rev == 15) {
2555 rme9652->card_name = "RME Digi9652 (Rev 1.5)";
2556 } else {
2557 rme9652->card_name = "RME Digi9652";
2559 rme9652->ss_channels = RME9652_NCHANNELS;
2560 break;
2563 rme9652->ds_channels = (rme9652->ss_channels - 2) / 2 + 2;
2565 pci_set_master(rme9652->pci);
2567 if ((err = snd_rme9652_initialize_memory(rme9652)) < 0) {
2568 return err;
2571 if ((err = snd_rme9652_create_pcm(card, rme9652)) < 0) {
2572 return err;
2575 if ((err = snd_rme9652_create_controls(card, rme9652)) < 0) {
2576 return err;
2579 snd_rme9652_proc_init(rme9652);
2581 rme9652->last_spdif_sample_rate = -1;
2582 rme9652->last_adat_sample_rate = -1;
2583 rme9652->playback_pid = -1;
2584 rme9652->capture_pid = -1;
2585 rme9652->capture_substream = NULL;
2586 rme9652->playback_substream = NULL;
2588 snd_rme9652_set_defaults(rme9652);
2590 if (rme9652->hw_rev == 15) {
2591 rme9652_initialize_spdif_receiver (rme9652);
2594 return 0;
2597 static void snd_rme9652_card_free(snd_card_t *card)
2599 rme9652_t *rme9652 = (rme9652_t *) card->private_data;
2601 if (rme9652)
2602 snd_rme9652_free(rme9652);
2605 static int __devinit snd_rme9652_probe(struct pci_dev *pci,
2606 const struct pci_device_id *pci_id)
2608 static int dev;
2609 rme9652_t *rme9652;
2610 snd_card_t *card;
2611 int err;
2613 if (dev >= SNDRV_CARDS)
2614 return -ENODEV;
2615 if (!enable[dev]) {
2616 dev++;
2617 return -ENOENT;
2620 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
2621 sizeof(rme9652_t));
2623 if (!card)
2624 return -ENOMEM;
2626 rme9652 = (rme9652_t *) card->private_data;
2627 card->private_free = snd_rme9652_card_free;
2628 rme9652->dev = dev;
2629 rme9652->pci = pci;
2630 snd_card_set_dev(card, &pci->dev);
2632 if ((err = snd_rme9652_create(card, rme9652, precise_ptr[dev])) < 0) {
2633 snd_card_free(card);
2634 return err;
2637 strcpy(card->shortname, rme9652->card_name);
2639 sprintf(card->longname, "%s at 0x%lx, irq %d",
2640 card->shortname, rme9652->port, rme9652->irq);
2643 if ((err = snd_card_register(card)) < 0) {
2644 snd_card_free(card);
2645 return err;
2647 pci_set_drvdata(pci, card);
2648 dev++;
2649 return 0;
2652 static void __devexit snd_rme9652_remove(struct pci_dev *pci)
2654 snd_card_free(pci_get_drvdata(pci));
2655 pci_set_drvdata(pci, NULL);
2658 static struct pci_driver driver = {
2659 .name = "RME Digi9652 (Hammerfall)",
2660 .id_table = snd_rme9652_ids,
2661 .probe = snd_rme9652_probe,
2662 .remove = __devexit_p(snd_rme9652_remove),
2665 static int __init alsa_card_hammerfall_init(void)
2667 return pci_module_init(&driver);
2670 static void __exit alsa_card_hammerfall_exit(void)
2672 pci_unregister_driver(&driver);
2675 module_init(alsa_card_hammerfall_init)
2676 module_exit(alsa_card_hammerfall_exit)