blob: a62d6e13d8ced09b333145f2d912d4086c125d94 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for RME Digi9652 audio interfaces
3 *
4 * Copyright (c) 1999 IEM - Winfried Ritsch
5 * Copyright (c) 1999-2001 Paul Davis
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/pcm.h>
32#include <sound/info.h>
33#include <sound/asoundef.h>
34#include <sound/initval.h>
35
36#include <asm/current.h>
37#include <asm/io.h>
38
39static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
40static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103041static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
42static bool precise_ptr[SNDRV_CARDS]; /* Enable precise pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44module_param_array(index, int, NULL, 0444);
45MODULE_PARM_DESC(index, "Index value for RME Digi9652 (Hammerfall) soundcard.");
46module_param_array(id, charp, NULL, 0444);
47MODULE_PARM_DESC(id, "ID string for RME Digi9652 (Hammerfall) soundcard.");
48module_param_array(enable, bool, NULL, 0444);
49MODULE_PARM_DESC(enable, "Enable/disable specific RME96{52,36} soundcards.");
50module_param_array(precise_ptr, bool, NULL, 0444);
51MODULE_PARM_DESC(precise_ptr, "Enable precise pointer (doesn't work reliably).");
52MODULE_AUTHOR("Paul Davis <pbd@op.net>, Winfried Ritsch");
53MODULE_DESCRIPTION("RME Digi9652/Digi9636");
54MODULE_LICENSE("GPL");
55MODULE_SUPPORTED_DEVICE("{{RME,Hammerfall},"
56 "{RME,Hammerfall-Light}}");
57
58/* The Hammerfall has two sets of 24 ADAT + 2 S/PDIF channels, one for
59 capture, one for playback. Both the ADAT and S/PDIF channels appear
60 to the host CPU in the same block of memory. There is no functional
61 difference between them in terms of access.
62
63 The Hammerfall Light is identical to the Hammerfall, except that it
64 has 2 sets 18 channels (16 ADAT + 2 S/PDIF) for capture and playback.
65*/
66
67#define RME9652_NCHANNELS 26
68#define RME9636_NCHANNELS 18
69
70/* Preferred sync source choices - used by "sync_pref" control switch */
71
72#define RME9652_SYNC_FROM_SPDIF 0
73#define RME9652_SYNC_FROM_ADAT1 1
74#define RME9652_SYNC_FROM_ADAT2 2
75#define RME9652_SYNC_FROM_ADAT3 3
76
77/* Possible sources of S/PDIF input */
78
79#define RME9652_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
80#define RME9652_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
81#define RME9652_SPDIFIN_INTERN 2 /* internal (CDROM) */
82
83/* ------------- Status-Register bits --------------------- */
84
85#define RME9652_IRQ (1<<0) /* IRQ is High if not reset by irq_clear */
86#define RME9652_lock_2 (1<<1) /* ADAT 3-PLL: 1=locked, 0=unlocked */
87#define RME9652_lock_1 (1<<2) /* ADAT 2-PLL: 1=locked, 0=unlocked */
88#define RME9652_lock_0 (1<<3) /* ADAT 1-PLL: 1=locked, 0=unlocked */
89#define RME9652_fs48 (1<<4) /* sample rate is 0=44.1/88.2,1=48/96 Khz */
90#define RME9652_wsel_rd (1<<5) /* if Word-Clock is used and valid then 1 */
91 /* bits 6-15 encode h/w buffer pointer position */
92#define RME9652_sync_2 (1<<16) /* if ADAT-IN 3 in sync to system clock */
93#define RME9652_sync_1 (1<<17) /* if ADAT-IN 2 in sync to system clock */
94#define RME9652_sync_0 (1<<18) /* if ADAT-IN 1 in sync to system clock */
95#define RME9652_DS_rd (1<<19) /* 1=Double Speed Mode, 0=Normal Speed */
96#define RME9652_tc_busy (1<<20) /* 1=time-code copy in progress (960ms) */
97#define RME9652_tc_out (1<<21) /* time-code out bit */
98#define RME9652_F_0 (1<<22) /* 000=64kHz, 100=88.2kHz, 011=96kHz */
99#define RME9652_F_1 (1<<23) /* 111=32kHz, 110=44.1kHz, 101=48kHz, */
100#define RME9652_F_2 (1<<24) /* external Crystal Chip if ERF=1 */
101#define RME9652_ERF (1<<25) /* Error-Flag of SDPIF Receiver (1=No Lock) */
102#define RME9652_buffer_id (1<<26) /* toggles by each interrupt on rec/play */
103#define RME9652_tc_valid (1<<27) /* 1 = a signal is detected on time-code input */
104#define RME9652_SPDIF_READ (1<<28) /* byte available from Rev 1.5+ S/PDIF interface */
105
106#define RME9652_sync (RME9652_sync_0|RME9652_sync_1|RME9652_sync_2)
107#define RME9652_lock (RME9652_lock_0|RME9652_lock_1|RME9652_lock_2)
108#define RME9652_F (RME9652_F_0|RME9652_F_1|RME9652_F_2)
109#define rme9652_decode_spdif_rate(x) ((x)>>22)
110
111/* Bit 6..15 : h/w buffer pointer */
112
113#define RME9652_buf_pos 0x000FFC0
114
115/* Bits 31,30,29 are bits 5,4,3 of h/w pointer position on later
116 Rev G EEPROMS and Rev 1.5 cards or later.
117*/
118
119#define RME9652_REV15_buf_pos(x) ((((x)&0xE0000000)>>26)|((x)&RME9652_buf_pos))
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* amount of io space we remap for register access. i'm not sure we
122 even need this much, but 1K is nice round number :)
123*/
124
125#define RME9652_IO_EXTENT 1024
126
127#define RME9652_init_buffer 0
128#define RME9652_play_buffer 32 /* holds ptr to 26x64kBit host RAM */
129#define RME9652_rec_buffer 36 /* holds ptr to 26x64kBit host RAM */
130#define RME9652_control_register 64
131#define RME9652_irq_clear 96
132#define RME9652_time_code 100 /* useful if used with alesis adat */
133#define RME9652_thru_base 128 /* 132...228 Thru for 26 channels */
134
135/* Read-only registers */
136
137/* Writing to any of the register locations writes to the status
138 register. We'll use the first location as our point of access.
139*/
140
141#define RME9652_status_register 0
142
143/* --------- Control-Register Bits ---------------- */
144
145
146#define RME9652_start_bit (1<<0) /* start record/play */
147 /* bits 1-3 encode buffersize/latency */
148#define RME9652_Master (1<<4) /* Clock Mode Master=1,Slave/Auto=0 */
Joe Perches561de312007-12-18 13:13:47 +0100149#define RME9652_IE (1<<5) /* Interrupt Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define RME9652_freq (1<<6) /* samplerate 0=44.1/88.2, 1=48/96 kHz */
151#define RME9652_freq1 (1<<7) /* if 0, 32kHz, else always 1 */
152#define RME9652_DS (1<<8) /* Doule Speed 0=44.1/48, 1=88.2/96 Khz */
153#define RME9652_PRO (1<<9) /* S/PDIF out: 0=consumer, 1=professional */
154#define RME9652_EMP (1<<10) /* Emphasis 0=None, 1=ON */
155#define RME9652_Dolby (1<<11) /* Non-audio bit 1=set, 0=unset */
156#define RME9652_opt_out (1<<12) /* Use 1st optical OUT as SPDIF: 1=yes,0=no */
157#define RME9652_wsel (1<<13) /* use Wordclock as sync (overwrites master) */
158#define RME9652_inp_0 (1<<14) /* SPDIF-IN: 00=optical (ADAT1), */
159#define RME9652_inp_1 (1<<15) /* 01=koaxial (Cinch), 10=Internal CDROM */
160#define RME9652_SyncPref_ADAT2 (1<<16)
161#define RME9652_SyncPref_ADAT3 (1<<17)
162#define RME9652_SPDIF_RESET (1<<18) /* Rev 1.5+: h/w S/PDIF receiver */
163#define RME9652_SPDIF_SELECT (1<<19)
164#define RME9652_SPDIF_CLOCK (1<<20)
165#define RME9652_SPDIF_WRITE (1<<21)
166#define RME9652_ADAT1_INTERNAL (1<<22) /* Rev 1.5+: if set, internal CD connector carries ADAT */
167
168/* buffersize = 512Bytes * 2^n, where n is made from Bit2 ... Bit0 */
169
170#define RME9652_latency 0x0e
171#define rme9652_encode_latency(x) (((x)&0x7)<<1)
172#define rme9652_decode_latency(x) (((x)>>1)&0x7)
173#define rme9652_running_double_speed(s) ((s)->control_register & RME9652_DS)
174#define RME9652_inp (RME9652_inp_0|RME9652_inp_1)
175#define rme9652_encode_spdif_in(x) (((x)&0x3)<<14)
176#define rme9652_decode_spdif_in(x) (((x)>>14)&0x3)
177
178#define RME9652_SyncPref_Mask (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
179#define RME9652_SyncPref_ADAT1 0
180#define RME9652_SyncPref_SPDIF (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
181
182/* the size of a substream (1 mono data stream) */
183
184#define RME9652_CHANNEL_BUFFER_SAMPLES (16*1024)
185#define RME9652_CHANNEL_BUFFER_BYTES (4*RME9652_CHANNEL_BUFFER_SAMPLES)
186
187/* the size of the area we need to allocate for DMA transfers. the
188 size is the same regardless of the number of channels - the
189 9636 still uses the same memory area.
190
191 Note that we allocate 1 more channel than is apparently needed
192 because the h/w seems to write 1 byte beyond the end of the last
193 page. Sigh.
194*/
195
196#define RME9652_DMA_AREA_BYTES ((RME9652_NCHANNELS+1) * RME9652_CHANNEL_BUFFER_BYTES)
197#define RME9652_DMA_AREA_KILOBYTES (RME9652_DMA_AREA_BYTES/1024)
198
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100199struct snd_rme9652 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int dev;
201
202 spinlock_t lock;
203 int irq;
204 unsigned long port;
205 void __iomem *iobase;
206
207 int precise_ptr;
208
209 u32 control_register; /* cached value */
210 u32 thru_bits; /* thru 1=on, 0=off channel 1=Bit1... channel 26= Bit26 */
211
212 u32 creg_spdif;
213 u32 creg_spdif_stream;
214
215 char *card_name; /* hammerfall or hammerfall light names */
216
217 size_t hw_offsetmask; /* &-with status register to get real hw_offset */
218 size_t prev_hw_offset; /* previous hw offset */
219 size_t max_jitter; /* maximum jitter in frames for
220 hw pointer */
221 size_t period_bytes; /* guess what this is */
222
223 unsigned char ds_channels;
224 unsigned char ss_channels; /* different for hammerfall/hammerfall-light */
225
226 struct snd_dma_buffer playback_dma_buf;
227 struct snd_dma_buffer capture_dma_buf;
228
229 unsigned char *capture_buffer; /* suitably aligned address */
230 unsigned char *playback_buffer; /* suitably aligned address */
231
232 pid_t capture_pid;
233 pid_t playback_pid;
234
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100235 struct snd_pcm_substream *capture_substream;
236 struct snd_pcm_substream *playback_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int running;
238
239 int passthru; /* non-zero if doing pass-thru */
240 int hw_rev; /* h/w rev * 10 (i.e. 1.5 has hw_rev = 15) */
241
242 int last_spdif_sample_rate; /* so that we can catch externally ... */
243 int last_adat_sample_rate; /* ... induced rate changes */
244
245 char *channel_map;
246
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100247 struct snd_card *card;
248 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct pci_dev *pci;
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100250 struct snd_kcontrol *spdif_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100252};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254/* These tables map the ALSA channels 1..N to the channels that we
255 need to use in order to find the relevant channel buffer. RME
256 refer to this kind of mapping as between "the ADAT channel and
257 the DMA channel." We index it using the logical audio channel,
258 and the value is the DMA channel (i.e. channel buffer number)
259 where the data for that channel can be read/written from/to.
260*/
261
262static char channel_map_9652_ss[26] = {
263 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
264 18, 19, 20, 21, 22, 23, 24, 25
265};
266
267static char channel_map_9636_ss[26] = {
268 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
269 /* channels 16 and 17 are S/PDIF */
270 24, 25,
271 /* channels 18-25 don't exist */
272 -1, -1, -1, -1, -1, -1, -1, -1
273};
274
275static char channel_map_9652_ds[26] = {
276 /* ADAT channels are remapped */
277 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
278 /* channels 12 and 13 are S/PDIF */
279 24, 25,
280 /* others don't exist */
281 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
282};
283
284static char channel_map_9636_ds[26] = {
285 /* ADAT channels are remapped */
286 1, 3, 5, 7, 9, 11, 13, 15,
287 /* channels 8 and 9 are S/PDIF */
Takashi Iwai770bd4b2013-12-27 00:13:08 +0200288 24, 25,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* others don't exist */
290 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
291};
292
293static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
294{
295 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
296 dmab->dev.dev = snd_dma_pci_data(pci);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200297 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
298 size, dmab) < 0)
299 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
302
303static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
304{
Takashi Iwai47d98c02014-01-08 16:12:25 +0100305 if (dmab->area)
306 snd_dma_free_pages(dmab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200310static DEFINE_PCI_DEVICE_TABLE(snd_rme9652_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 {
312 .vendor = 0x10ee,
313 .device = 0x3fc4,
314 .subvendor = PCI_ANY_ID,
315 .subdevice = PCI_ANY_ID,
316 }, /* RME Digi9652 */
317 { 0, },
318};
319
320MODULE_DEVICE_TABLE(pci, snd_rme9652_ids);
321
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100322static inline void rme9652_write(struct snd_rme9652 *rme9652, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 writel(val, rme9652->iobase + reg);
325}
326
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100327static inline unsigned int rme9652_read(struct snd_rme9652 *rme9652, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 return readl(rme9652->iobase + reg);
330}
331
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100332static inline int snd_rme9652_use_is_exclusive(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 unsigned long flags;
335 int ret = 1;
336
337 spin_lock_irqsave(&rme9652->lock, flags);
338 if ((rme9652->playback_pid != rme9652->capture_pid) &&
339 (rme9652->playback_pid >= 0) && (rme9652->capture_pid >= 0)) {
340 ret = 0;
341 }
342 spin_unlock_irqrestore(&rme9652->lock, flags);
343 return ret;
344}
345
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100346static inline int rme9652_adat_sample_rate(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 if (rme9652_running_double_speed(rme9652)) {
349 return (rme9652_read(rme9652, RME9652_status_register) &
350 RME9652_fs48) ? 96000 : 88200;
351 } else {
352 return (rme9652_read(rme9652, RME9652_status_register) &
353 RME9652_fs48) ? 48000 : 44100;
354 }
355}
356
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100357static inline void rme9652_compute_period_size(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned int i;
360
361 i = rme9652->control_register & RME9652_latency;
362 rme9652->period_bytes = 1 << ((rme9652_decode_latency(i) + 8));
363 rme9652->hw_offsetmask =
364 (rme9652->period_bytes * 2 - 1) & RME9652_buf_pos;
365 rme9652->max_jitter = 80;
366}
367
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100368static snd_pcm_uframes_t rme9652_hw_pointer(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 int status;
371 unsigned int offset, frag;
372 snd_pcm_uframes_t period_size = rme9652->period_bytes / 4;
373 snd_pcm_sframes_t delta;
374
375 status = rme9652_read(rme9652, RME9652_status_register);
376 if (!rme9652->precise_ptr)
377 return (status & RME9652_buffer_id) ? period_size : 0;
378 offset = status & RME9652_buf_pos;
379
380 /* The hardware may give a backward movement for up to 80 frames
381 Martin Kirst <martin.kirst@freenet.de> knows the details.
382 */
383
384 delta = rme9652->prev_hw_offset - offset;
385 delta &= 0xffff;
386 if (delta <= (snd_pcm_sframes_t)rme9652->max_jitter * 4)
387 offset = rme9652->prev_hw_offset;
388 else
389 rme9652->prev_hw_offset = offset;
390 offset &= rme9652->hw_offsetmask;
391 offset /= 4;
392 frag = status & RME9652_buffer_id;
393
394 if (offset < period_size) {
395 if (offset > rme9652->max_jitter) {
396 if (frag)
397 printk(KERN_ERR "Unexpected hw_pointer position (bufid == 0): status: %x offset: %d\n", status, offset);
398 } else if (!frag)
399 return 0;
400 offset -= rme9652->max_jitter;
Takashi Iwai304e3bb2007-05-30 12:46:21 +0200401 if ((int)offset < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 offset += period_size * 2;
403 } else {
404 if (offset > period_size + rme9652->max_jitter) {
405 if (!frag)
406 printk(KERN_ERR "Unexpected hw_pointer position (bufid == 1): status: %x offset: %d\n", status, offset);
407 } else if (frag)
408 return period_size;
409 offset -= rme9652->max_jitter;
410 }
411
412 return offset;
413}
414
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100415static inline void rme9652_reset_hw_pointer(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int i;
418
419 /* reset the FIFO pointer to zero. We do this by writing to 8
420 registers, each of which is a 32bit wide register, and set
421 them all to zero. Note that s->iobase is a pointer to
422 int32, not pointer to char.
423 */
424
425 for (i = 0; i < 8; i++) {
426 rme9652_write(rme9652, i * 4, 0);
427 udelay(10);
428 }
429 rme9652->prev_hw_offset = 0;
430}
431
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100432static inline void rme9652_start(struct snd_rme9652 *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 s->control_register |= (RME9652_IE | RME9652_start_bit);
435 rme9652_write(s, RME9652_control_register, s->control_register);
436}
437
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100438static inline void rme9652_stop(struct snd_rme9652 *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 s->control_register &= ~(RME9652_start_bit | RME9652_IE);
441 rme9652_write(s, RME9652_control_register, s->control_register);
442}
443
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100444static int rme9652_set_interrupt_interval(struct snd_rme9652 *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 unsigned int frames)
446{
447 int restart = 0;
448 int n;
449
450 spin_lock_irq(&s->lock);
451
452 if ((restart = s->running)) {
453 rme9652_stop(s);
454 }
455
456 frames >>= 7;
457 n = 0;
458 while (frames) {
459 n++;
460 frames >>= 1;
461 }
462
463 s->control_register &= ~RME9652_latency;
464 s->control_register |= rme9652_encode_latency(n);
465
466 rme9652_write(s, RME9652_control_register, s->control_register);
467
468 rme9652_compute_period_size(s);
469
470 if (restart)
471 rme9652_start(s);
472
473 spin_unlock_irq(&s->lock);
474
475 return 0;
476}
477
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100478static int rme9652_set_rate(struct snd_rme9652 *rme9652, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 int restart;
481 int reject_if_open = 0;
482 int xrate;
483
484 if (!snd_rme9652_use_is_exclusive (rme9652)) {
485 return -EBUSY;
486 }
487
488 /* Changing from a "single speed" to a "double speed" rate is
489 not allowed if any substreams are open. This is because
490 such a change causes a shift in the location of
491 the DMA buffers and a reduction in the number of available
492 buffers.
493
494 Note that a similar but essentially insoluble problem
495 exists for externally-driven rate changes. All we can do
496 is to flag rate changes in the read/write routines.
497 */
498
499 spin_lock_irq(&rme9652->lock);
500 xrate = rme9652_adat_sample_rate(rme9652);
501
502 switch (rate) {
503 case 44100:
504 if (xrate > 48000) {
505 reject_if_open = 1;
506 }
507 rate = 0;
508 break;
509 case 48000:
510 if (xrate > 48000) {
511 reject_if_open = 1;
512 }
513 rate = RME9652_freq;
514 break;
515 case 88200:
516 if (xrate < 48000) {
517 reject_if_open = 1;
518 }
519 rate = RME9652_DS;
520 break;
521 case 96000:
522 if (xrate < 48000) {
523 reject_if_open = 1;
524 }
525 rate = RME9652_DS | RME9652_freq;
526 break;
527 default:
528 spin_unlock_irq(&rme9652->lock);
529 return -EINVAL;
530 }
531
532 if (reject_if_open && (rme9652->capture_pid >= 0 || rme9652->playback_pid >= 0)) {
533 spin_unlock_irq(&rme9652->lock);
534 return -EBUSY;
535 }
536
537 if ((restart = rme9652->running)) {
538 rme9652_stop(rme9652);
539 }
540 rme9652->control_register &= ~(RME9652_freq | RME9652_DS);
541 rme9652->control_register |= rate;
542 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
543
544 if (restart) {
545 rme9652_start(rme9652);
546 }
547
548 if (rate & RME9652_DS) {
549 if (rme9652->ss_channels == RME9652_NCHANNELS) {
550 rme9652->channel_map = channel_map_9652_ds;
551 } else {
552 rme9652->channel_map = channel_map_9636_ds;
553 }
554 } else {
555 if (rme9652->ss_channels == RME9652_NCHANNELS) {
556 rme9652->channel_map = channel_map_9652_ss;
557 } else {
558 rme9652->channel_map = channel_map_9636_ss;
559 }
560 }
561
562 spin_unlock_irq(&rme9652->lock);
563 return 0;
564}
565
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100566static void rme9652_set_thru(struct snd_rme9652 *rme9652, int channel, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int i;
569
570 rme9652->passthru = 0;
571
572 if (channel < 0) {
573
574 /* set thru for all channels */
575
576 if (enable) {
577 for (i = 0; i < RME9652_NCHANNELS; i++) {
578 rme9652->thru_bits |= (1 << i);
579 rme9652_write(rme9652, RME9652_thru_base + i * 4, 1);
580 }
581 } else {
582 for (i = 0; i < RME9652_NCHANNELS; i++) {
583 rme9652->thru_bits &= ~(1 << i);
584 rme9652_write(rme9652, RME9652_thru_base + i * 4, 0);
585 }
586 }
587
588 } else {
589 int mapped_channel;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 mapped_channel = rme9652->channel_map[channel];
592
593 if (enable) {
594 rme9652->thru_bits |= (1 << mapped_channel);
595 } else {
596 rme9652->thru_bits &= ~(1 << mapped_channel);
597 }
598
599 rme9652_write(rme9652,
600 RME9652_thru_base + mapped_channel * 4,
601 enable ? 1 : 0);
602 }
603}
604
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100605static int rme9652_set_passthru(struct snd_rme9652 *rme9652, int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 if (onoff) {
608 rme9652_set_thru(rme9652, -1, 1);
609
610 /* we don't want interrupts, so do a
611 custom version of rme9652_start().
612 */
613
614 rme9652->control_register =
615 RME9652_inp_0 |
616 rme9652_encode_latency(7) |
617 RME9652_start_bit;
618
619 rme9652_reset_hw_pointer(rme9652);
620
621 rme9652_write(rme9652, RME9652_control_register,
622 rme9652->control_register);
623 rme9652->passthru = 1;
624 } else {
625 rme9652_set_thru(rme9652, -1, 0);
626 rme9652_stop(rme9652);
627 rme9652->passthru = 0;
628 }
629
630 return 0;
631}
632
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100633static void rme9652_spdif_set_bit (struct snd_rme9652 *rme9652, int mask, int onoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 if (onoff)
636 rme9652->control_register |= mask;
637 else
638 rme9652->control_register &= ~mask;
639
640 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
641}
642
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100643static void rme9652_spdif_write_byte (struct snd_rme9652 *rme9652, const int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 long mask;
646 long i;
647
648 for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
649 if (val & mask)
650 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 1);
651 else
652 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 0);
653
654 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
655 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
656 }
657}
658
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100659static int rme9652_spdif_read_byte (struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 long mask;
662 long val;
663 long i;
664
665 val = 0;
666
667 for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
668 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
669 if (rme9652_read (rme9652, RME9652_status_register) & RME9652_SPDIF_READ)
670 val |= mask;
671 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
672 }
673
674 return val;
675}
676
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100677static void rme9652_write_spdif_codec (struct snd_rme9652 *rme9652, const int address, const int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
680 rme9652_spdif_write_byte (rme9652, 0x20);
681 rme9652_spdif_write_byte (rme9652, address);
682 rme9652_spdif_write_byte (rme9652, data);
683 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
684}
685
686
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100687static int rme9652_spdif_read_codec (struct snd_rme9652 *rme9652, const int address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int ret;
690
691 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
692 rme9652_spdif_write_byte (rme9652, 0x20);
693 rme9652_spdif_write_byte (rme9652, address);
694 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
695 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
696
697 rme9652_spdif_write_byte (rme9652, 0x21);
698 ret = rme9652_spdif_read_byte (rme9652);
699 rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
700
701 return ret;
702}
703
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100704static void rme9652_initialize_spdif_receiver (struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 /* XXX what unsets this ? */
707
708 rme9652->control_register |= RME9652_SPDIF_RESET;
709
710 rme9652_write_spdif_codec (rme9652, 4, 0x40);
711 rme9652_write_spdif_codec (rme9652, 17, 0x13);
712 rme9652_write_spdif_codec (rme9652, 6, 0x02);
713}
714
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100715static inline int rme9652_spdif_sample_rate(struct snd_rme9652 *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 unsigned int rate_bits;
718
719 if (rme9652_read(s, RME9652_status_register) & RME9652_ERF) {
720 return -1; /* error condition */
721 }
722
723 if (s->hw_rev == 15) {
724
725 int x, y, ret;
726
727 x = rme9652_spdif_read_codec (s, 30);
728
729 if (x != 0)
730 y = 48000 * 64 / x;
731 else
732 y = 0;
733
734 if (y > 30400 && y < 33600) ret = 32000;
735 else if (y > 41900 && y < 46000) ret = 44100;
736 else if (y > 46000 && y < 50400) ret = 48000;
737 else if (y > 60800 && y < 67200) ret = 64000;
738 else if (y > 83700 && y < 92000) ret = 88200;
739 else if (y > 92000 && y < 100000) ret = 96000;
740 else ret = 0;
741 return ret;
742 }
743
744 rate_bits = rme9652_read(s, RME9652_status_register) & RME9652_F;
745
746 switch (rme9652_decode_spdif_rate(rate_bits)) {
747 case 0x7:
748 return 32000;
749 break;
750
751 case 0x6:
752 return 44100;
753 break;
754
755 case 0x5:
756 return 48000;
757 break;
758
759 case 0x4:
760 return 88200;
761 break;
762
763 case 0x3:
764 return 96000;
765 break;
766
767 case 0x0:
768 return 64000;
769 break;
770
771 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +0200772 snd_printk(KERN_ERR "%s: unknown S/PDIF input rate (bits = 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 s->card_name, rate_bits);
774 return 0;
775 break;
776 }
777}
778
779/*-----------------------------------------------------------------------------
780 Control Interface
781 ----------------------------------------------------------------------------*/
782
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100783static u32 snd_rme9652_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 u32 val = 0;
786 val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME9652_PRO : 0;
787 val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME9652_Dolby : 0;
788 if (val & RME9652_PRO)
789 val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME9652_EMP : 0;
790 else
791 val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME9652_EMP : 0;
792 return val;
793}
794
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100795static void snd_rme9652_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 aes->status[0] = ((val & RME9652_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
798 ((val & RME9652_Dolby) ? IEC958_AES0_NONAUDIO : 0);
799 if (val & RME9652_PRO)
800 aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
801 else
802 aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
803}
804
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100805static int snd_rme9652_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
808 uinfo->count = 1;
809 return 0;
810}
811
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100812static int snd_rme9652_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100814 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif);
817 return 0;
818}
819
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100820static int snd_rme9652_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100822 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int change;
824 u32 val;
825
826 val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
827 spin_lock_irq(&rme9652->lock);
828 change = val != rme9652->creg_spdif;
829 rme9652->creg_spdif = val;
830 spin_unlock_irq(&rme9652->lock);
831 return change;
832}
833
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100834static int snd_rme9652_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
837 uinfo->count = 1;
838 return 0;
839}
840
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100841static int snd_rme9652_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100843 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif_stream);
846 return 0;
847}
848
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100849static int snd_rme9652_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100851 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int change;
853 u32 val;
854
855 val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
856 spin_lock_irq(&rme9652->lock);
857 change = val != rme9652->creg_spdif_stream;
858 rme9652->creg_spdif_stream = val;
859 rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
860 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= val);
861 spin_unlock_irq(&rme9652->lock);
862 return change;
863}
864
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100865static int snd_rme9652_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
868 uinfo->count = 1;
869 return 0;
870}
871
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100872static int snd_rme9652_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 ucontrol->value.iec958.status[0] = kcontrol->private_value;
875 return 0;
876}
877
878#define RME9652_ADAT1_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +0200879{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .info = snd_rme9652_info_adat1_in, \
881 .get = snd_rme9652_get_adat1_in, \
882 .put = snd_rme9652_put_adat1_in }
883
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100884static unsigned int rme9652_adat1_in(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 if (rme9652->control_register & RME9652_ADAT1_INTERNAL)
887 return 1;
888 return 0;
889}
890
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100891static int rme9652_set_adat1_input(struct snd_rme9652 *rme9652, int internal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int restart = 0;
894
895 if (internal) {
896 rme9652->control_register |= RME9652_ADAT1_INTERNAL;
897 } else {
898 rme9652->control_register &= ~RME9652_ADAT1_INTERNAL;
899 }
900
901 /* XXX do we actually need to stop the card when we do this ? */
902
903 if ((restart = rme9652->running)) {
904 rme9652_stop(rme9652);
905 }
906
907 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
908
909 if (restart) {
910 rme9652_start(rme9652);
911 }
912
913 return 0;
914}
915
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100916static int snd_rme9652_info_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 static char *texts[2] = {"ADAT1", "Internal"};
919
920 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
921 uinfo->count = 1;
922 uinfo->value.enumerated.items = 2;
923 if (uinfo->value.enumerated.item > 1)
924 uinfo->value.enumerated.item = 1;
925 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
926 return 0;
927}
928
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100929static int snd_rme9652_get_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100931 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 spin_lock_irq(&rme9652->lock);
934 ucontrol->value.enumerated.item[0] = rme9652_adat1_in(rme9652);
935 spin_unlock_irq(&rme9652->lock);
936 return 0;
937}
938
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100939static int snd_rme9652_put_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100941 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int change;
943 unsigned int val;
944
945 if (!snd_rme9652_use_is_exclusive(rme9652))
946 return -EBUSY;
947 val = ucontrol->value.enumerated.item[0] % 2;
948 spin_lock_irq(&rme9652->lock);
949 change = val != rme9652_adat1_in(rme9652);
950 if (change)
951 rme9652_set_adat1_input(rme9652, val);
952 spin_unlock_irq(&rme9652->lock);
953 return change;
954}
955
956#define RME9652_SPDIF_IN(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +0200957{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .info = snd_rme9652_info_spdif_in, \
959 .get = snd_rme9652_get_spdif_in, .put = snd_rme9652_put_spdif_in }
960
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100961static unsigned int rme9652_spdif_in(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 return rme9652_decode_spdif_in(rme9652->control_register &
964 RME9652_inp);
965}
966
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100967static int rme9652_set_spdif_input(struct snd_rme9652 *rme9652, int in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 int restart = 0;
970
971 rme9652->control_register &= ~RME9652_inp;
972 rme9652->control_register |= rme9652_encode_spdif_in(in);
973
974 if ((restart = rme9652->running)) {
975 rme9652_stop(rme9652);
976 }
977
978 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
979
980 if (restart) {
981 rme9652_start(rme9652);
982 }
983
984 return 0;
985}
986
Takashi Iwaiabfd67b2005-11-17 14:52:53 +0100987static int snd_rme9652_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 static char *texts[3] = {"ADAT1", "Coaxial", "Internal"};
990
991 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
992 uinfo->count = 1;
993 uinfo->value.enumerated.items = 3;
994 if (uinfo->value.enumerated.item > 2)
995 uinfo->value.enumerated.item = 2;
996 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
997 return 0;
998}
999
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001000static int snd_rme9652_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001002 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 spin_lock_irq(&rme9652->lock);
1005 ucontrol->value.enumerated.item[0] = rme9652_spdif_in(rme9652);
1006 spin_unlock_irq(&rme9652->lock);
1007 return 0;
1008}
1009
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001010static int snd_rme9652_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001012 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int change;
1014 unsigned int val;
1015
1016 if (!snd_rme9652_use_is_exclusive(rme9652))
1017 return -EBUSY;
1018 val = ucontrol->value.enumerated.item[0] % 3;
1019 spin_lock_irq(&rme9652->lock);
1020 change = val != rme9652_spdif_in(rme9652);
1021 if (change)
1022 rme9652_set_spdif_input(rme9652, val);
1023 spin_unlock_irq(&rme9652->lock);
1024 return change;
1025}
1026
1027#define RME9652_SPDIF_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001028{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .info = snd_rme9652_info_spdif_out, \
1030 .get = snd_rme9652_get_spdif_out, .put = snd_rme9652_put_spdif_out }
1031
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001032static int rme9652_spdif_out(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 return (rme9652->control_register & RME9652_opt_out) ? 1 : 0;
1035}
1036
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001037static int rme9652_set_spdif_output(struct snd_rme9652 *rme9652, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int restart = 0;
1040
1041 if (out) {
1042 rme9652->control_register |= RME9652_opt_out;
1043 } else {
1044 rme9652->control_register &= ~RME9652_opt_out;
1045 }
1046
1047 if ((restart = rme9652->running)) {
1048 rme9652_stop(rme9652);
1049 }
1050
1051 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1052
1053 if (restart) {
1054 rme9652_start(rme9652);
1055 }
1056
1057 return 0;
1058}
1059
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001060#define snd_rme9652_info_spdif_out snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001062static int snd_rme9652_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001064 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 spin_lock_irq(&rme9652->lock);
1067 ucontrol->value.integer.value[0] = rme9652_spdif_out(rme9652);
1068 spin_unlock_irq(&rme9652->lock);
1069 return 0;
1070}
1071
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001072static int snd_rme9652_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001074 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int change;
1076 unsigned int val;
1077
1078 if (!snd_rme9652_use_is_exclusive(rme9652))
1079 return -EBUSY;
1080 val = ucontrol->value.integer.value[0] & 1;
1081 spin_lock_irq(&rme9652->lock);
1082 change = (int)val != rme9652_spdif_out(rme9652);
1083 rme9652_set_spdif_output(rme9652, val);
1084 spin_unlock_irq(&rme9652->lock);
1085 return change;
1086}
1087
1088#define RME9652_SYNC_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001089{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 .info = snd_rme9652_info_sync_mode, \
1091 .get = snd_rme9652_get_sync_mode, .put = snd_rme9652_put_sync_mode }
1092
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001093static int rme9652_sync_mode(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 if (rme9652->control_register & RME9652_wsel) {
1096 return 2;
1097 } else if (rme9652->control_register & RME9652_Master) {
1098 return 1;
1099 } else {
1100 return 0;
1101 }
1102}
1103
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001104static int rme9652_set_sync_mode(struct snd_rme9652 *rme9652, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 int restart = 0;
1107
1108 switch (mode) {
1109 case 0:
1110 rme9652->control_register &=
1111 ~(RME9652_Master | RME9652_wsel);
1112 break;
1113 case 1:
1114 rme9652->control_register =
1115 (rme9652->control_register & ~RME9652_wsel) | RME9652_Master;
1116 break;
1117 case 2:
1118 rme9652->control_register |=
1119 (RME9652_Master | RME9652_wsel);
1120 break;
1121 }
1122
1123 if ((restart = rme9652->running)) {
1124 rme9652_stop(rme9652);
1125 }
1126
1127 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1128
1129 if (restart) {
1130 rme9652_start(rme9652);
1131 }
1132
1133 return 0;
1134}
1135
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001136static int snd_rme9652_info_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 static char *texts[3] = {"AutoSync", "Master", "Word Clock"};
1139
1140 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1141 uinfo->count = 1;
1142 uinfo->value.enumerated.items = 3;
1143 if (uinfo->value.enumerated.item > 2)
1144 uinfo->value.enumerated.item = 2;
1145 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1146 return 0;
1147}
1148
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001149static int snd_rme9652_get_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001151 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 spin_lock_irq(&rme9652->lock);
1154 ucontrol->value.enumerated.item[0] = rme9652_sync_mode(rme9652);
1155 spin_unlock_irq(&rme9652->lock);
1156 return 0;
1157}
1158
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001159static int snd_rme9652_put_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001161 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int change;
1163 unsigned int val;
1164
1165 val = ucontrol->value.enumerated.item[0] % 3;
1166 spin_lock_irq(&rme9652->lock);
1167 change = (int)val != rme9652_sync_mode(rme9652);
1168 rme9652_set_sync_mode(rme9652, val);
1169 spin_unlock_irq(&rme9652->lock);
1170 return change;
1171}
1172
1173#define RME9652_SYNC_PREF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001174{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 .info = snd_rme9652_info_sync_pref, \
1176 .get = snd_rme9652_get_sync_pref, .put = snd_rme9652_put_sync_pref }
1177
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001178static int rme9652_sync_pref(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1181 case RME9652_SyncPref_ADAT1:
1182 return RME9652_SYNC_FROM_ADAT1;
1183 case RME9652_SyncPref_ADAT2:
1184 return RME9652_SYNC_FROM_ADAT2;
1185 case RME9652_SyncPref_ADAT3:
1186 return RME9652_SYNC_FROM_ADAT3;
1187 case RME9652_SyncPref_SPDIF:
1188 return RME9652_SYNC_FROM_SPDIF;
1189 }
1190 /* Not reachable */
1191 return 0;
1192}
1193
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001194static int rme9652_set_sync_pref(struct snd_rme9652 *rme9652, int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int restart;
1197
1198 rme9652->control_register &= ~RME9652_SyncPref_Mask;
1199 switch (pref) {
1200 case RME9652_SYNC_FROM_ADAT1:
1201 rme9652->control_register |= RME9652_SyncPref_ADAT1;
1202 break;
1203 case RME9652_SYNC_FROM_ADAT2:
1204 rme9652->control_register |= RME9652_SyncPref_ADAT2;
1205 break;
1206 case RME9652_SYNC_FROM_ADAT3:
1207 rme9652->control_register |= RME9652_SyncPref_ADAT3;
1208 break;
1209 case RME9652_SYNC_FROM_SPDIF:
1210 rme9652->control_register |= RME9652_SyncPref_SPDIF;
1211 break;
1212 }
1213
1214 if ((restart = rme9652->running)) {
1215 rme9652_stop(rme9652);
1216 }
1217
1218 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1219
1220 if (restart) {
1221 rme9652_start(rme9652);
1222 }
1223
1224 return 0;
1225}
1226
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001227static int snd_rme9652_info_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 static char *texts[4] = {"IEC958 In", "ADAT1 In", "ADAT2 In", "ADAT3 In"};
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001230 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1233 uinfo->count = 1;
1234 uinfo->value.enumerated.items = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
1235 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1236 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1237 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1238 return 0;
1239}
1240
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001241static int snd_rme9652_get_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001243 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 spin_lock_irq(&rme9652->lock);
1246 ucontrol->value.enumerated.item[0] = rme9652_sync_pref(rme9652);
1247 spin_unlock_irq(&rme9652->lock);
1248 return 0;
1249}
1250
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001251static int snd_rme9652_put_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001253 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int change, max;
1255 unsigned int val;
1256
1257 if (!snd_rme9652_use_is_exclusive(rme9652))
1258 return -EBUSY;
1259 max = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
1260 val = ucontrol->value.enumerated.item[0] % max;
1261 spin_lock_irq(&rme9652->lock);
1262 change = (int)val != rme9652_sync_pref(rme9652);
1263 rme9652_set_sync_pref(rme9652, val);
1264 spin_unlock_irq(&rme9652->lock);
1265 return change;
1266}
1267
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001268static int snd_rme9652_info_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001270 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1272 uinfo->count = rme9652->ss_channels;
1273 uinfo->value.integer.min = 0;
1274 uinfo->value.integer.max = 1;
1275 return 0;
1276}
1277
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001278static int snd_rme9652_get_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001280 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned int k;
1282 u32 thru_bits = rme9652->thru_bits;
1283
1284 for (k = 0; k < rme9652->ss_channels; ++k) {
1285 ucontrol->value.integer.value[k] = !!(thru_bits & (1 << k));
1286 }
1287 return 0;
1288}
1289
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001290static int snd_rme9652_put_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001292 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int change;
1294 unsigned int chn;
1295 u32 thru_bits = 0;
1296
1297 if (!snd_rme9652_use_is_exclusive(rme9652))
1298 return -EBUSY;
1299
1300 for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1301 if (ucontrol->value.integer.value[chn])
1302 thru_bits |= 1 << chn;
1303 }
1304
1305 spin_lock_irq(&rme9652->lock);
1306 change = thru_bits ^ rme9652->thru_bits;
1307 if (change) {
1308 for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1309 if (!(change & (1 << chn)))
1310 continue;
1311 rme9652_set_thru(rme9652,chn,thru_bits&(1<<chn));
1312 }
1313 }
1314 spin_unlock_irq(&rme9652->lock);
1315 return !!change;
1316}
1317
1318#define RME9652_PASSTHRU(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001319{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 .info = snd_rme9652_info_passthru, \
1321 .put = snd_rme9652_put_passthru, \
1322 .get = snd_rme9652_get_passthru }
1323
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001324#define snd_rme9652_info_passthru snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001326static int snd_rme9652_get_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001328 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 spin_lock_irq(&rme9652->lock);
1331 ucontrol->value.integer.value[0] = rme9652->passthru;
1332 spin_unlock_irq(&rme9652->lock);
1333 return 0;
1334}
1335
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001336static int snd_rme9652_put_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001338 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int change;
1340 unsigned int val;
1341 int err = 0;
1342
1343 if (!snd_rme9652_use_is_exclusive(rme9652))
1344 return -EBUSY;
1345
1346 val = ucontrol->value.integer.value[0] & 1;
1347 spin_lock_irq(&rme9652->lock);
1348 change = (ucontrol->value.integer.value[0] != rme9652->passthru);
1349 if (change)
1350 err = rme9652_set_passthru(rme9652, val);
1351 spin_unlock_irq(&rme9652->lock);
1352 return err ? err : change;
1353}
1354
1355/* Read-only switches */
1356
1357#define RME9652_SPDIF_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001358{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1360 .info = snd_rme9652_info_spdif_rate, \
1361 .get = snd_rme9652_get_spdif_rate }
1362
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001363static int snd_rme9652_info_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1366 uinfo->count = 1;
1367 uinfo->value.integer.min = 0;
1368 uinfo->value.integer.max = 96000;
1369 return 0;
1370}
1371
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001372static int snd_rme9652_get_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001374 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 spin_lock_irq(&rme9652->lock);
1377 ucontrol->value.integer.value[0] = rme9652_spdif_sample_rate(rme9652);
1378 spin_unlock_irq(&rme9652->lock);
1379 return 0;
1380}
1381
1382#define RME9652_ADAT_SYNC(xname, xindex, xidx) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001383{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1385 .info = snd_rme9652_info_adat_sync, \
1386 .get = snd_rme9652_get_adat_sync, .private_value = xidx }
1387
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001388static int snd_rme9652_info_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 static char *texts[4] = {"No Lock", "Lock", "No Lock Sync", "Lock Sync"};
1391
1392 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1393 uinfo->count = 1;
1394 uinfo->value.enumerated.items = 4;
1395 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1396 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1397 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1398 return 0;
1399}
1400
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001401static int snd_rme9652_get_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001403 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned int mask1, mask2, val;
1405
1406 switch (kcontrol->private_value) {
1407 case 0: mask1 = RME9652_lock_0; mask2 = RME9652_sync_0; break;
1408 case 1: mask1 = RME9652_lock_1; mask2 = RME9652_sync_1; break;
1409 case 2: mask1 = RME9652_lock_2; mask2 = RME9652_sync_2; break;
1410 default: return -EINVAL;
1411 }
1412 val = rme9652_read(rme9652, RME9652_status_register);
1413 ucontrol->value.enumerated.item[0] = (val & mask1) ? 1 : 0;
1414 ucontrol->value.enumerated.item[0] |= (val & mask2) ? 2 : 0;
1415 return 0;
1416}
1417
1418#define RME9652_TC_VALID(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001419{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1421 .info = snd_rme9652_info_tc_valid, \
1422 .get = snd_rme9652_get_tc_valid }
1423
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001424#define snd_rme9652_info_tc_valid snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001426static int snd_rme9652_get_tc_valid(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001428 struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 ucontrol->value.integer.value[0] =
1431 (rme9652_read(rme9652, RME9652_status_register) & RME9652_tc_valid) ? 1 : 0;
1432 return 0;
1433}
1434
Olaf Hering44456d32005-07-27 11:45:17 -07001435#ifdef ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437/* FIXME: this routine needs a port to the new control API --jk */
1438
1439static int snd_rme9652_get_tc_value(void *private_data,
1440 snd_kswitch_t *kswitch,
1441 snd_switch_t *uswitch)
1442{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001443 struct snd_rme9652 *s = (struct snd_rme9652 *) private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 u32 value;
1445 int i;
1446
1447 uswitch->type = SNDRV_SW_TYPE_DWORD;
1448
1449 if ((rme9652_read(s, RME9652_status_register) &
1450 RME9652_tc_valid) == 0) {
1451 uswitch->value.data32[0] = 0;
1452 return 0;
1453 }
1454
1455 /* timecode request */
1456
1457 rme9652_write(s, RME9652_time_code, 0);
1458
1459 /* XXX bug alert: loop-based timing !!!! */
1460
1461 for (i = 0; i < 50; i++) {
1462 if (!(rme9652_read(s, i * 4) & RME9652_tc_busy))
1463 break;
1464 }
1465
1466 if (!(rme9652_read(s, i * 4) & RME9652_tc_busy)) {
1467 return -EIO;
1468 }
1469
1470 value = 0;
1471
1472 for (i = 0; i < 32; i++) {
1473 value >>= 1;
1474
1475 if (rme9652_read(s, i * 4) & RME9652_tc_out)
1476 value |= 0x80000000;
1477 }
1478
1479 if (value > 2 * 60 * 48000) {
1480 value -= 2 * 60 * 48000;
1481 } else {
1482 value = 0;
1483 }
1484
1485 uswitch->value.data32[0] = value;
1486
1487 return 0;
1488}
1489
1490#endif /* ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE */
1491
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001492static struct snd_kcontrol_new snd_rme9652_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Clemens Ladisch5549d542005-08-03 13:50:30 +02001494 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1496 .info = snd_rme9652_control_spdif_info,
1497 .get = snd_rme9652_control_spdif_get,
1498 .put = snd_rme9652_control_spdif_put,
1499},
1500{
1501 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001502 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1504 .info = snd_rme9652_control_spdif_stream_info,
1505 .get = snd_rme9652_control_spdif_stream_get,
1506 .put = snd_rme9652_control_spdif_stream_put,
1507},
1508{
1509 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001510 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1512 .info = snd_rme9652_control_spdif_mask_info,
1513 .get = snd_rme9652_control_spdif_mask_get,
1514 .private_value = IEC958_AES0_NONAUDIO |
1515 IEC958_AES0_PROFESSIONAL |
1516 IEC958_AES0_CON_EMPHASIS,
1517},
1518{
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001520 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1522 .info = snd_rme9652_control_spdif_mask_info,
1523 .get = snd_rme9652_control_spdif_mask_get,
1524 .private_value = IEC958_AES0_NONAUDIO |
1525 IEC958_AES0_PROFESSIONAL |
1526 IEC958_AES0_PRO_EMPHASIS,
1527},
1528RME9652_SPDIF_IN("IEC958 Input Connector", 0),
1529RME9652_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
1530RME9652_SYNC_MODE("Sync Mode", 0),
1531RME9652_SYNC_PREF("Preferred Sync Source", 0),
1532{
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .name = "Channels Thru",
1535 .index = 0,
1536 .info = snd_rme9652_info_thru,
1537 .get = snd_rme9652_get_thru,
1538 .put = snd_rme9652_put_thru,
1539},
1540RME9652_SPDIF_RATE("IEC958 Sample Rate", 0),
1541RME9652_ADAT_SYNC("ADAT1 Sync Check", 0, 0),
1542RME9652_ADAT_SYNC("ADAT2 Sync Check", 0, 1),
1543RME9652_TC_VALID("Timecode Valid", 0),
1544RME9652_PASSTHRU("Passthru", 0)
1545};
1546
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001547static struct snd_kcontrol_new snd_rme9652_adat3_check =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548RME9652_ADAT_SYNC("ADAT3 Sync Check", 0, 2);
1549
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001550static struct snd_kcontrol_new snd_rme9652_adat1_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551RME9652_ADAT1_IN("ADAT1 Input Source", 0);
1552
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001553static int snd_rme9652_create_controls(struct snd_card *card, struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 unsigned int idx;
1556 int err;
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001557 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 for (idx = 0; idx < ARRAY_SIZE(snd_rme9652_controls); idx++) {
1560 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_controls[idx], rme9652))) < 0)
1561 return err;
1562 if (idx == 1) /* IEC958 (S/PDIF) Stream */
1563 rme9652->spdif_ctl = kctl;
1564 }
1565
1566 if (rme9652->ss_channels == RME9652_NCHANNELS)
1567 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat3_check, rme9652))) < 0)
1568 return err;
1569
1570 if (rme9652->hw_rev >= 15)
1571 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat1_input, rme9652))) < 0)
1572 return err;
1573
1574 return 0;
1575}
1576
1577/*------------------------------------------------------------
1578 /proc interface
1579 ------------------------------------------------------------*/
1580
1581static void
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001582snd_rme9652_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001584 struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 u32 thru_bits = rme9652->thru_bits;
1586 int show_auto_sync_source = 0;
1587 int i;
1588 unsigned int status;
1589 int x;
1590
1591 status = rme9652_read(rme9652, RME9652_status_register);
1592
1593 snd_iprintf(buffer, "%s (Card #%d)\n", rme9652->card_name, rme9652->card->number + 1);
1594 snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
1595 rme9652->capture_buffer, rme9652->playback_buffer);
1596 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
1597 rme9652->irq, rme9652->port, (unsigned long)rme9652->iobase);
1598 snd_iprintf(buffer, "Control register: %x\n", rme9652->control_register);
1599
1600 snd_iprintf(buffer, "\n");
1601
1602 x = 1 << (6 + rme9652_decode_latency(rme9652->control_register &
1603 RME9652_latency));
1604
1605 snd_iprintf(buffer, "Latency: %d samples (2 periods of %lu bytes)\n",
1606 x, (unsigned long) rme9652->period_bytes);
1607 snd_iprintf(buffer, "Hardware pointer (frames): %ld\n",
1608 rme9652_hw_pointer(rme9652));
1609 snd_iprintf(buffer, "Passthru: %s\n",
1610 rme9652->passthru ? "yes" : "no");
1611
1612 if ((rme9652->control_register & (RME9652_Master | RME9652_wsel)) == 0) {
1613 snd_iprintf(buffer, "Clock mode: autosync\n");
1614 show_auto_sync_source = 1;
1615 } else if (rme9652->control_register & RME9652_wsel) {
1616 if (status & RME9652_wsel_rd) {
1617 snd_iprintf(buffer, "Clock mode: word clock\n");
1618 } else {
1619 snd_iprintf(buffer, "Clock mode: word clock (no signal)\n");
1620 }
1621 } else {
1622 snd_iprintf(buffer, "Clock mode: master\n");
1623 }
1624
1625 if (show_auto_sync_source) {
1626 switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1627 case RME9652_SyncPref_ADAT1:
1628 snd_iprintf(buffer, "Pref. sync source: ADAT1\n");
1629 break;
1630 case RME9652_SyncPref_ADAT2:
1631 snd_iprintf(buffer, "Pref. sync source: ADAT2\n");
1632 break;
1633 case RME9652_SyncPref_ADAT3:
1634 snd_iprintf(buffer, "Pref. sync source: ADAT3\n");
1635 break;
1636 case RME9652_SyncPref_SPDIF:
1637 snd_iprintf(buffer, "Pref. sync source: IEC958\n");
1638 break;
1639 default:
1640 snd_iprintf(buffer, "Pref. sync source: ???\n");
1641 }
1642 }
1643
1644 if (rme9652->hw_rev >= 15)
1645 snd_iprintf(buffer, "\nADAT1 Input source: %s\n",
1646 (rme9652->control_register & RME9652_ADAT1_INTERNAL) ?
1647 "Internal" : "ADAT1 optical");
1648
1649 snd_iprintf(buffer, "\n");
1650
1651 switch (rme9652_decode_spdif_in(rme9652->control_register &
1652 RME9652_inp)) {
1653 case RME9652_SPDIFIN_OPTICAL:
1654 snd_iprintf(buffer, "IEC958 input: ADAT1\n");
1655 break;
1656 case RME9652_SPDIFIN_COAXIAL:
1657 snd_iprintf(buffer, "IEC958 input: Coaxial\n");
1658 break;
1659 case RME9652_SPDIFIN_INTERN:
1660 snd_iprintf(buffer, "IEC958 input: Internal\n");
1661 break;
1662 default:
1663 snd_iprintf(buffer, "IEC958 input: ???\n");
1664 break;
1665 }
1666
1667 if (rme9652->control_register & RME9652_opt_out) {
1668 snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
1669 } else {
1670 snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
1671 }
1672
1673 if (rme9652->control_register & RME9652_PRO) {
1674 snd_iprintf(buffer, "IEC958 quality: Professional\n");
1675 } else {
1676 snd_iprintf(buffer, "IEC958 quality: Consumer\n");
1677 }
1678
1679 if (rme9652->control_register & RME9652_EMP) {
1680 snd_iprintf(buffer, "IEC958 emphasis: on\n");
1681 } else {
1682 snd_iprintf(buffer, "IEC958 emphasis: off\n");
1683 }
1684
1685 if (rme9652->control_register & RME9652_Dolby) {
1686 snd_iprintf(buffer, "IEC958 Dolby: on\n");
1687 } else {
1688 snd_iprintf(buffer, "IEC958 Dolby: off\n");
1689 }
1690
1691 i = rme9652_spdif_sample_rate(rme9652);
1692
1693 if (i < 0) {
1694 snd_iprintf(buffer,
1695 "IEC958 sample rate: error flag set\n");
1696 } else if (i == 0) {
1697 snd_iprintf(buffer, "IEC958 sample rate: undetermined\n");
1698 } else {
1699 snd_iprintf(buffer, "IEC958 sample rate: %d\n", i);
1700 }
1701
1702 snd_iprintf(buffer, "\n");
1703
1704 snd_iprintf(buffer, "ADAT Sample rate: %dHz\n",
1705 rme9652_adat_sample_rate(rme9652));
1706
1707 /* Sync Check */
1708
1709 x = status & RME9652_sync_0;
1710 if (status & RME9652_lock_0) {
1711 snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
1712 } else {
1713 snd_iprintf(buffer, "ADAT1: No Lock\n");
1714 }
1715
1716 x = status & RME9652_sync_1;
1717 if (status & RME9652_lock_1) {
1718 snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
1719 } else {
1720 snd_iprintf(buffer, "ADAT2: No Lock\n");
1721 }
1722
1723 x = status & RME9652_sync_2;
1724 if (status & RME9652_lock_2) {
1725 snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
1726 } else {
1727 snd_iprintf(buffer, "ADAT3: No Lock\n");
1728 }
1729
1730 snd_iprintf(buffer, "\n");
1731
1732 snd_iprintf(buffer, "Timecode signal: %s\n",
1733 (status & RME9652_tc_valid) ? "yes" : "no");
1734
1735 /* thru modes */
1736
1737 snd_iprintf(buffer, "Punch Status:\n\n");
1738
1739 for (i = 0; i < rme9652->ss_channels; i++) {
1740 if (thru_bits & (1 << i)) {
1741 snd_iprintf(buffer, "%2d: on ", i + 1);
1742 } else {
1743 snd_iprintf(buffer, "%2d: off ", i + 1);
1744 }
1745
1746 if (((i + 1) % 8) == 0) {
1747 snd_iprintf(buffer, "\n");
1748 }
1749 }
1750
1751 snd_iprintf(buffer, "\n");
1752}
1753
Bill Pembertone23e7a12012-12-06 12:35:10 -05001754static void snd_rme9652_proc_init(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001756 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 if (! snd_card_proc_new(rme9652->card, "rme9652", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001759 snd_info_set_text_ops(entry, rme9652, snd_rme9652_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001762static void snd_rme9652_free_buffers(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 snd_hammerfall_free_buffer(&rme9652->capture_dma_buf, rme9652->pci);
1765 snd_hammerfall_free_buffer(&rme9652->playback_dma_buf, rme9652->pci);
1766}
1767
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001768static int snd_rme9652_free(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 if (rme9652->irq >= 0)
1771 rme9652_stop(rme9652);
1772 snd_rme9652_free_buffers(rme9652);
1773
1774 if (rme9652->irq >= 0)
1775 free_irq(rme9652->irq, (void *)rme9652);
1776 if (rme9652->iobase)
1777 iounmap(rme9652->iobase);
1778 if (rme9652->port)
1779 pci_release_regions(rme9652->pci);
1780
1781 pci_disable_device(rme9652->pci);
1782 return 0;
1783}
1784
Bill Pembertone23e7a12012-12-06 12:35:10 -05001785static int snd_rme9652_initialize_memory(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 unsigned long pb_bus, cb_bus;
1788
1789 if (snd_hammerfall_get_buffer(rme9652->pci, &rme9652->capture_dma_buf, RME9652_DMA_AREA_BYTES) < 0 ||
1790 snd_hammerfall_get_buffer(rme9652->pci, &rme9652->playback_dma_buf, RME9652_DMA_AREA_BYTES) < 0) {
1791 if (rme9652->capture_dma_buf.area)
1792 snd_dma_free_pages(&rme9652->capture_dma_buf);
1793 printk(KERN_ERR "%s: no buffers available\n", rme9652->card_name);
1794 return -ENOMEM;
1795 }
1796
1797 /* Align to bus-space 64K boundary */
1798
Clemens Ladisch7ab39922006-10-09 08:13:32 +02001799 cb_bus = ALIGN(rme9652->capture_dma_buf.addr, 0x10000ul);
1800 pb_bus = ALIGN(rme9652->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Tell the card where it is */
1803
1804 rme9652_write(rme9652, RME9652_rec_buffer, cb_bus);
1805 rme9652_write(rme9652, RME9652_play_buffer, pb_bus);
1806
1807 rme9652->capture_buffer = rme9652->capture_dma_buf.area + (cb_bus - rme9652->capture_dma_buf.addr);
1808 rme9652->playback_buffer = rme9652->playback_dma_buf.area + (pb_bus - rme9652->playback_dma_buf.addr);
1809
1810 return 0;
1811}
1812
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001813static void snd_rme9652_set_defaults(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 unsigned int k;
1816
1817 /* ASSUMPTION: rme9652->lock is either held, or
1818 there is no need to hold it (e.g. during module
Joe Perches561de312007-12-18 13:13:47 +01001819 initialization).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
1821
1822 /* set defaults:
1823
1824 SPDIF Input via Coax
1825 autosync clock mode
1826 maximum latency (7 = 8192 samples, 64Kbyte buffer,
1827 which implies 2 4096 sample, 32Kbyte periods).
1828
1829 if rev 1.5, initialize the S/PDIF receiver.
1830
1831 */
1832
1833 rme9652->control_register =
1834 RME9652_inp_0 | rme9652_encode_latency(7);
1835
1836 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1837
1838 rme9652_reset_hw_pointer(rme9652);
1839 rme9652_compute_period_size(rme9652);
1840
1841 /* default: thru off for all channels */
1842
1843 for (k = 0; k < RME9652_NCHANNELS; ++k)
1844 rme9652_write(rme9652, RME9652_thru_base + k * 4, 0);
1845
1846 rme9652->thru_bits = 0;
1847 rme9652->passthru = 0;
1848
1849 /* set a default rate so that the channel map is set up */
1850
1851 rme9652_set_rate(rme9652, 48000);
1852}
1853
David Howells7d12e782006-10-05 14:55:46 +01001854static irqreturn_t snd_rme9652_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001856 struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (!(rme9652_read(rme9652, RME9652_status_register) & RME9652_IRQ)) {
1859 return IRQ_NONE;
1860 }
1861
1862 rme9652_write(rme9652, RME9652_irq_clear, 0);
1863
1864 if (rme9652->capture_substream) {
1865 snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
1866 }
1867
1868 if (rme9652->playback_substream) {
1869 snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
1870 }
1871 return IRQ_HANDLED;
1872}
1873
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001874static snd_pcm_uframes_t snd_rme9652_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001876 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return rme9652_hw_pointer(rme9652);
1878}
1879
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001880static char *rme9652_channel_buffer_location(struct snd_rme9652 *rme9652,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int stream,
1882 int channel)
1883
1884{
1885 int mapped_channel;
1886
Takashi Iwaida3cec32008-08-08 17:12:14 +02001887 if (snd_BUG_ON(channel < 0 || channel >= RME9652_NCHANNELS))
1888 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 if ((mapped_channel = rme9652->channel_map[channel]) < 0) {
1891 return NULL;
1892 }
1893
1894 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1895 return rme9652->capture_buffer +
1896 (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1897 } else {
1898 return rme9652->playback_buffer +
1899 (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1900 }
1901}
1902
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001903static int snd_rme9652_playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
1905{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001906 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 char *channel_buf;
1908
Takashi Iwaida3cec32008-08-08 17:12:14 +02001909 if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES / 4))
1910 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 channel_buf = rme9652_channel_buffer_location (rme9652,
1913 substream->pstr->stream,
1914 channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001915 if (snd_BUG_ON(!channel_buf))
1916 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (copy_from_user(channel_buf + pos * 4, src, count * 4))
1918 return -EFAULT;
1919 return count;
1920}
1921
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001922static int snd_rme9652_capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
1924{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001925 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 char *channel_buf;
1927
Takashi Iwaida3cec32008-08-08 17:12:14 +02001928 if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES / 4))
1929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 channel_buf = rme9652_channel_buffer_location (rme9652,
1932 substream->pstr->stream,
1933 channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001934 if (snd_BUG_ON(!channel_buf))
1935 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
1937 return -EFAULT;
1938 return count;
1939}
1940
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001941static int snd_rme9652_hw_silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
1943{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001944 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 char *channel_buf;
1946
1947 channel_buf = rme9652_channel_buffer_location (rme9652,
1948 substream->pstr->stream,
1949 channel);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001950 if (snd_BUG_ON(!channel_buf))
1951 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 memset(channel_buf + pos * 4, 0, count * 4);
1953 return count;
1954}
1955
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001956static int snd_rme9652_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001958 struct snd_pcm_runtime *runtime = substream->runtime;
1959 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1960 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1962 other = rme9652->capture_substream;
1963 else
1964 other = rme9652->playback_substream;
1965 if (rme9652->running)
1966 runtime->status->hw_ptr = rme9652_hw_pointer(rme9652);
1967 else
1968 runtime->status->hw_ptr = 0;
1969 if (other) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001970 struct snd_pcm_substream *s;
1971 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001972 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (s == other) {
1974 oruntime->status->hw_ptr = runtime->status->hw_ptr;
1975 break;
1976 }
1977 }
1978 }
1979 return 0;
1980}
1981
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001982static int snd_rme9652_hw_params(struct snd_pcm_substream *substream,
1983 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01001985 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int err;
1987 pid_t this_pid;
1988 pid_t other_pid;
1989
1990 spin_lock_irq(&rme9652->lock);
1991
1992 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1993 rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
1994 rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= rme9652->creg_spdif_stream);
1995 this_pid = rme9652->playback_pid;
1996 other_pid = rme9652->capture_pid;
1997 } else {
1998 this_pid = rme9652->capture_pid;
1999 other_pid = rme9652->playback_pid;
2000 }
2001
2002 if ((other_pid > 0) && (this_pid != other_pid)) {
2003
2004 /* The other stream is open, and not by the same
2005 task as this one. Make sure that the parameters
2006 that matter are the same.
2007 */
2008
2009 if ((int)params_rate(params) !=
2010 rme9652_adat_sample_rate(rme9652)) {
2011 spin_unlock_irq(&rme9652->lock);
2012 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
2013 return -EBUSY;
2014 }
2015
2016 if (params_period_size(params) != rme9652->period_bytes / 4) {
2017 spin_unlock_irq(&rme9652->lock);
2018 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2019 return -EBUSY;
2020 }
2021
2022 /* We're fine. */
2023
2024 spin_unlock_irq(&rme9652->lock);
2025 return 0;
2026
2027 } else {
2028 spin_unlock_irq(&rme9652->lock);
2029 }
2030
2031 /* how to make sure that the rate matches an externally-set one ?
2032 */
2033
2034 if ((err = rme9652_set_rate(rme9652, params_rate(params))) < 0) {
2035 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
2036 return err;
2037 }
2038
2039 if ((err = rme9652_set_interrupt_interval(rme9652, params_period_size(params))) < 0) {
2040 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2041 return err;
2042 }
2043
2044 return 0;
2045}
2046
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002047static int snd_rme9652_channel_info(struct snd_pcm_substream *substream,
2048 struct snd_pcm_channel_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002050 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int chn;
2052
Takashi Iwaida3cec32008-08-08 17:12:14 +02002053 if (snd_BUG_ON(info->channel >= RME9652_NCHANNELS))
2054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if ((chn = rme9652->channel_map[info->channel]) < 0) {
2057 return -EINVAL;
2058 }
2059
2060 info->offset = chn * RME9652_CHANNEL_BUFFER_BYTES;
2061 info->first = 0;
2062 info->step = 32;
2063 return 0;
2064}
2065
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002066static int snd_rme9652_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 unsigned int cmd, void *arg)
2068{
2069 switch (cmd) {
2070 case SNDRV_PCM_IOCTL1_RESET:
2071 {
2072 return snd_rme9652_reset(substream);
2073 }
2074 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
2075 {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002076 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return snd_rme9652_channel_info(substream, info);
2078 }
2079 default:
2080 break;
2081 }
2082
2083 return snd_pcm_lib_ioctl(substream, cmd, arg);
2084}
2085
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002086static void rme9652_silence_playback(struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 memset(rme9652->playback_buffer, 0, RME9652_DMA_AREA_BYTES);
2089}
2090
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002091static int snd_rme9652_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int cmd)
2093{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002094 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2095 struct snd_pcm_substream *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int running;
2097 spin_lock(&rme9652->lock);
2098 running = rme9652->running;
2099 switch (cmd) {
2100 case SNDRV_PCM_TRIGGER_START:
2101 running |= 1 << substream->stream;
2102 break;
2103 case SNDRV_PCM_TRIGGER_STOP:
2104 running &= ~(1 << substream->stream);
2105 break;
2106 default:
2107 snd_BUG();
2108 spin_unlock(&rme9652->lock);
2109 return -EINVAL;
2110 }
2111 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2112 other = rme9652->capture_substream;
2113 else
2114 other = rme9652->playback_substream;
2115
2116 if (other) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002117 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01002118 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (s == other) {
2120 snd_pcm_trigger_done(s, substream);
2121 if (cmd == SNDRV_PCM_TRIGGER_START)
2122 running |= 1 << s->stream;
2123 else
2124 running &= ~(1 << s->stream);
2125 goto _ok;
2126 }
2127 }
2128 if (cmd == SNDRV_PCM_TRIGGER_START) {
2129 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
2130 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2131 rme9652_silence_playback(rme9652);
2132 } else {
2133 if (running &&
2134 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2135 rme9652_silence_playback(rme9652);
2136 }
2137 } else {
2138 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2139 rme9652_silence_playback(rme9652);
2140 }
2141 _ok:
2142 snd_pcm_trigger_done(substream, substream);
2143 if (!rme9652->running && running)
2144 rme9652_start(rme9652);
2145 else if (rme9652->running && !running)
2146 rme9652_stop(rme9652);
2147 rme9652->running = running;
2148 spin_unlock(&rme9652->lock);
2149
2150 return 0;
2151}
2152
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002153static int snd_rme9652_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002155 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unsigned long flags;
2157 int result = 0;
2158
2159 spin_lock_irqsave(&rme9652->lock, flags);
2160 if (!rme9652->running)
2161 rme9652_reset_hw_pointer(rme9652);
2162 spin_unlock_irqrestore(&rme9652->lock, flags);
2163 return result;
2164}
2165
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002166static struct snd_pcm_hardware snd_rme9652_playback_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 .info = (SNDRV_PCM_INFO_MMAP |
2169 SNDRV_PCM_INFO_MMAP_VALID |
2170 SNDRV_PCM_INFO_NONINTERLEAVED |
2171 SNDRV_PCM_INFO_SYNC_START |
2172 SNDRV_PCM_INFO_DOUBLE),
2173 .formats = SNDRV_PCM_FMTBIT_S32_LE,
2174 .rates = (SNDRV_PCM_RATE_44100 |
2175 SNDRV_PCM_RATE_48000 |
2176 SNDRV_PCM_RATE_88200 |
2177 SNDRV_PCM_RATE_96000),
2178 .rate_min = 44100,
2179 .rate_max = 96000,
2180 .channels_min = 10,
2181 .channels_max = 26,
2182 .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES * 26,
2183 .period_bytes_min = (64 * 4) * 10,
2184 .period_bytes_max = (8192 * 4) * 26,
2185 .periods_min = 2,
2186 .periods_max = 2,
2187 .fifo_size = 0,
2188};
2189
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002190static struct snd_pcm_hardware snd_rme9652_capture_subinfo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 .info = (SNDRV_PCM_INFO_MMAP |
2193 SNDRV_PCM_INFO_MMAP_VALID |
2194 SNDRV_PCM_INFO_NONINTERLEAVED |
2195 SNDRV_PCM_INFO_SYNC_START),
2196 .formats = SNDRV_PCM_FMTBIT_S32_LE,
2197 .rates = (SNDRV_PCM_RATE_44100 |
2198 SNDRV_PCM_RATE_48000 |
2199 SNDRV_PCM_RATE_88200 |
2200 SNDRV_PCM_RATE_96000),
2201 .rate_min = 44100,
2202 .rate_max = 96000,
2203 .channels_min = 10,
2204 .channels_max = 26,
2205 .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES *26,
2206 .period_bytes_min = (64 * 4) * 10,
2207 .period_bytes_max = (8192 * 4) * 26,
2208 .periods_min = 2,
2209 .periods_max = 2,
2210 .fifo_size = 0,
2211};
2212
2213static unsigned int period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
2214
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002215static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 .count = ARRAY_SIZE(period_sizes),
2217 .list = period_sizes,
2218 .mask = 0
2219};
2220
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002221static int snd_rme9652_hw_rule_channels(struct snd_pcm_hw_params *params,
2222 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002224 struct snd_rme9652 *rme9652 = rule->private;
2225 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned int list[2] = { rme9652->ds_channels, rme9652->ss_channels };
2227 return snd_interval_list(c, 2, list, 0);
2228}
2229
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002230static int snd_rme9652_hw_rule_channels_rate(struct snd_pcm_hw_params *params,
2231 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002233 struct snd_rme9652 *rme9652 = rule->private;
2234 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2235 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (r->min > 48000) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002237 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 .min = rme9652->ds_channels,
2239 .max = rme9652->ds_channels,
2240 .integer = 1,
2241 };
2242 return snd_interval_refine(c, &t);
2243 } else if (r->max < 88200) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002244 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 .min = rme9652->ss_channels,
2246 .max = rme9652->ss_channels,
2247 .integer = 1,
2248 };
2249 return snd_interval_refine(c, &t);
2250 }
2251 return 0;
2252}
2253
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002254static int snd_rme9652_hw_rule_rate_channels(struct snd_pcm_hw_params *params,
2255 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002257 struct snd_rme9652 *rme9652 = rule->private;
2258 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2259 struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (c->min >= rme9652->ss_channels) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002261 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 .min = 44100,
2263 .max = 48000,
2264 .integer = 1,
2265 };
2266 return snd_interval_refine(r, &t);
2267 } else if (c->max <= rme9652->ds_channels) {
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002268 struct snd_interval t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 .min = 88200,
2270 .max = 96000,
2271 .integer = 1,
2272 };
2273 return snd_interval_refine(r, &t);
2274 }
2275 return 0;
2276}
2277
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002278static int snd_rme9652_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002280 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2281 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 spin_lock_irq(&rme9652->lock);
2284
2285 snd_pcm_set_sync(substream);
2286
2287 runtime->hw = snd_rme9652_playback_subinfo;
2288 runtime->dma_area = rme9652->playback_buffer;
2289 runtime->dma_bytes = RME9652_DMA_AREA_BYTES;
2290
2291 if (rme9652->capture_substream == NULL) {
2292 rme9652_stop(rme9652);
2293 rme9652_set_thru(rme9652, -1, 0);
2294 }
2295
2296 rme9652->playback_pid = current->pid;
2297 rme9652->playback_substream = substream;
2298
2299 spin_unlock_irq(&rme9652->lock);
2300
2301 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2302 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2303 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2304 snd_rme9652_hw_rule_channels, rme9652,
2305 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2306 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2307 snd_rme9652_hw_rule_channels_rate, rme9652,
2308 SNDRV_PCM_HW_PARAM_RATE, -1);
2309 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2310 snd_rme9652_hw_rule_rate_channels, rme9652,
2311 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2312
2313 rme9652->creg_spdif_stream = rme9652->creg_spdif;
2314 rme9652->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2315 snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2316 SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2317 return 0;
2318}
2319
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002320static int snd_rme9652_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002322 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 spin_lock_irq(&rme9652->lock);
2325
2326 rme9652->playback_pid = -1;
2327 rme9652->playback_substream = NULL;
2328
2329 spin_unlock_irq(&rme9652->lock);
2330
2331 rme9652->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2332 snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2333 SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2334 return 0;
2335}
2336
2337
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002338static int snd_rme9652_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002340 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2341 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 spin_lock_irq(&rme9652->lock);
2344
2345 snd_pcm_set_sync(substream);
2346
2347 runtime->hw = snd_rme9652_capture_subinfo;
2348 runtime->dma_area = rme9652->capture_buffer;
2349 runtime->dma_bytes = RME9652_DMA_AREA_BYTES;
2350
2351 if (rme9652->playback_substream == NULL) {
2352 rme9652_stop(rme9652);
2353 rme9652_set_thru(rme9652, -1, 0);
2354 }
2355
2356 rme9652->capture_pid = current->pid;
2357 rme9652->capture_substream = substream;
2358
2359 spin_unlock_irq(&rme9652->lock);
2360
2361 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2362 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2363 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2364 snd_rme9652_hw_rule_channels, rme9652,
2365 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2366 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2367 snd_rme9652_hw_rule_channels_rate, rme9652,
2368 SNDRV_PCM_HW_PARAM_RATE, -1);
2369 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2370 snd_rme9652_hw_rule_rate_channels, rme9652,
2371 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2372 return 0;
2373}
2374
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002375static int snd_rme9652_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002377 struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 spin_lock_irq(&rme9652->lock);
2380
2381 rme9652->capture_pid = -1;
2382 rme9652->capture_substream = NULL;
2383
2384 spin_unlock_irq(&rme9652->lock);
2385 return 0;
2386}
2387
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002388static struct snd_pcm_ops snd_rme9652_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 .open = snd_rme9652_playback_open,
2390 .close = snd_rme9652_playback_release,
2391 .ioctl = snd_rme9652_ioctl,
2392 .hw_params = snd_rme9652_hw_params,
2393 .prepare = snd_rme9652_prepare,
2394 .trigger = snd_rme9652_trigger,
2395 .pointer = snd_rme9652_hw_pointer,
2396 .copy = snd_rme9652_playback_copy,
2397 .silence = snd_rme9652_hw_silence,
2398};
2399
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002400static struct snd_pcm_ops snd_rme9652_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .open = snd_rme9652_capture_open,
2402 .close = snd_rme9652_capture_release,
2403 .ioctl = snd_rme9652_ioctl,
2404 .hw_params = snd_rme9652_hw_params,
2405 .prepare = snd_rme9652_prepare,
2406 .trigger = snd_rme9652_trigger,
2407 .pointer = snd_rme9652_hw_pointer,
2408 .copy = snd_rme9652_capture_copy,
2409};
2410
Bill Pembertone23e7a12012-12-06 12:35:10 -05002411static int snd_rme9652_create_pcm(struct snd_card *card,
2412 struct snd_rme9652 *rme9652)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002414 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int err;
2416
2417 if ((err = snd_pcm_new(card,
2418 rme9652->card_name,
2419 0, 1, 1, &pcm)) < 0) {
2420 return err;
2421 }
2422
2423 rme9652->pcm = pcm;
2424 pcm->private_data = rme9652;
2425 strcpy(pcm->name, rme9652->card_name);
2426
2427 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme9652_playback_ops);
2428 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme9652_capture_ops);
2429
2430 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2431
2432 return 0;
2433}
2434
Bill Pembertone23e7a12012-12-06 12:35:10 -05002435static int snd_rme9652_create(struct snd_card *card,
2436 struct snd_rme9652 *rme9652,
2437 int precise_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 struct pci_dev *pci = rme9652->pci;
2440 int err;
2441 int status;
2442 unsigned short rev;
2443
2444 rme9652->irq = -1;
2445 rme9652->card = card;
2446
2447 pci_read_config_word(rme9652->pci, PCI_CLASS_REVISION, &rev);
2448
2449 switch (rev & 0xff) {
2450 case 3:
2451 case 4:
2452 case 8:
2453 case 9:
2454 break;
2455
2456 default:
2457 /* who knows? */
2458 return -ENODEV;
2459 }
2460
2461 if ((err = pci_enable_device(pci)) < 0)
2462 return err;
2463
2464 spin_lock_init(&rme9652->lock);
2465
2466 if ((err = pci_request_regions(pci, "rme9652")) < 0)
2467 return err;
2468 rme9652->port = pci_resource_start(pci, 0);
2469 rme9652->iobase = ioremap_nocache(rme9652->port, RME9652_IO_EXTENT);
2470 if (rme9652->iobase == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002471 snd_printk(KERN_ERR "unable to remap region 0x%lx-0x%lx\n", rme9652->port, rme9652->port + RME9652_IO_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return -EBUSY;
2473 }
2474
Takashi Iwai437a5a42006-11-21 12:14:23 +01002475 if (request_irq(pci->irq, snd_rme9652_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002476 KBUILD_MODNAME, rme9652)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002477 snd_printk(KERN_ERR "unable to request IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return -EBUSY;
2479 }
2480 rme9652->irq = pci->irq;
2481 rme9652->precise_ptr = precise_ptr;
2482
2483 /* Determine the h/w rev level of the card. This seems like
2484 a particularly kludgy way to encode it, but its what RME
2485 chose to do, so we follow them ...
2486 */
2487
2488 status = rme9652_read(rme9652, RME9652_status_register);
2489 if (rme9652_decode_spdif_rate(status&RME9652_F) == 1) {
2490 rme9652->hw_rev = 15;
2491 } else {
2492 rme9652->hw_rev = 11;
2493 }
2494
2495 /* Differentiate between the standard Hammerfall, and the
2496 "Light", which does not have the expansion board. This
2497 method comes from information received from Mathhias
2498 Clausen at RME. Display the EEPROM and h/w revID where
2499 relevant.
2500 */
2501
2502 switch (rev) {
2503 case 8: /* original eprom */
2504 strcpy(card->driver, "RME9636");
2505 if (rme9652->hw_rev == 15) {
2506 rme9652->card_name = "RME Digi9636 (Rev 1.5)";
2507 } else {
2508 rme9652->card_name = "RME Digi9636";
2509 }
2510 rme9652->ss_channels = RME9636_NCHANNELS;
2511 break;
2512 case 9: /* W36_G EPROM */
2513 strcpy(card->driver, "RME9636");
2514 rme9652->card_name = "RME Digi9636 (Rev G)";
2515 rme9652->ss_channels = RME9636_NCHANNELS;
2516 break;
2517 case 4: /* W52_G EPROM */
2518 strcpy(card->driver, "RME9652");
2519 rme9652->card_name = "RME Digi9652 (Rev G)";
2520 rme9652->ss_channels = RME9652_NCHANNELS;
2521 break;
2522 case 3: /* original eprom */
2523 strcpy(card->driver, "RME9652");
2524 if (rme9652->hw_rev == 15) {
2525 rme9652->card_name = "RME Digi9652 (Rev 1.5)";
2526 } else {
2527 rme9652->card_name = "RME Digi9652";
2528 }
2529 rme9652->ss_channels = RME9652_NCHANNELS;
2530 break;
2531 }
2532
2533 rme9652->ds_channels = (rme9652->ss_channels - 2) / 2 + 2;
2534
2535 pci_set_master(rme9652->pci);
2536
2537 if ((err = snd_rme9652_initialize_memory(rme9652)) < 0) {
2538 return err;
2539 }
2540
2541 if ((err = snd_rme9652_create_pcm(card, rme9652)) < 0) {
2542 return err;
2543 }
2544
2545 if ((err = snd_rme9652_create_controls(card, rme9652)) < 0) {
2546 return err;
2547 }
2548
2549 snd_rme9652_proc_init(rme9652);
2550
2551 rme9652->last_spdif_sample_rate = -1;
2552 rme9652->last_adat_sample_rate = -1;
2553 rme9652->playback_pid = -1;
2554 rme9652->capture_pid = -1;
2555 rme9652->capture_substream = NULL;
2556 rme9652->playback_substream = NULL;
2557
2558 snd_rme9652_set_defaults(rme9652);
2559
2560 if (rme9652->hw_rev == 15) {
2561 rme9652_initialize_spdif_receiver (rme9652);
2562 }
2563
2564 return 0;
2565}
2566
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002567static void snd_rme9652_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002569 struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 if (rme9652)
2572 snd_rme9652_free(rme9652);
2573}
2574
Bill Pembertone23e7a12012-12-06 12:35:10 -05002575static int snd_rme9652_probe(struct pci_dev *pci,
2576 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 static int dev;
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002579 struct snd_rme9652 *rme9652;
2580 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 int err;
2582
2583 if (dev >= SNDRV_CARDS)
2584 return -ENODEV;
2585 if (!enable[dev]) {
2586 dev++;
2587 return -ENOENT;
2588 }
2589
Takashi Iwai60c57722014-01-29 14:20:19 +01002590 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2591 sizeof(struct snd_rme9652), &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002593 if (err < 0)
2594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Takashi Iwaiabfd67b2005-11-17 14:52:53 +01002596 rme9652 = (struct snd_rme9652 *) card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 card->private_free = snd_rme9652_card_free;
2598 rme9652->dev = dev;
2599 rme9652->pci = pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 if ((err = snd_rme9652_create(card, rme9652, precise_ptr[dev])) < 0) {
2602 snd_card_free(card);
2603 return err;
2604 }
2605
2606 strcpy(card->shortname, rme9652->card_name);
2607
2608 sprintf(card->longname, "%s at 0x%lx, irq %d",
2609 card->shortname, rme9652->port, rme9652->irq);
2610
2611
2612 if ((err = snd_card_register(card)) < 0) {
2613 snd_card_free(card);
2614 return err;
2615 }
2616 pci_set_drvdata(pci, card);
2617 dev++;
2618 return 0;
2619}
2620
Bill Pembertone23e7a12012-12-06 12:35:10 -05002621static void snd_rme9652_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002626static struct pci_driver rme9652_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002627 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 .id_table = snd_rme9652_ids,
2629 .probe = snd_rme9652_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002630 .remove = snd_rme9652_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631};
2632
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002633module_pci_driver(rme9652_driver);