2
* Purpose: Driver for RME Digi32 family
6
* This file is part of Open Sound System.
8
* Copyright (C) 4Front Technologies 1996-2008.
10
* This this source file is released under GPL v2 license (no other versions).
11
* See the COPYING file included in the main directory of this source
12
* distribution for the license terms and conditions.
16
#include "oss_digi32_cfg.h"
19
#define RME_VENDOR_ID 0xea60
20
#define RME_DIGI32 0x9896
21
#define RME_DIGI32_PRO 0x9897
22
#define RME_DIGI32_8 0x9898
24
#define RME_VENDOR_ID2 0x10ee
25
#define RME_DIGI96_8_PRO 0x3fc2
27
#define MAX_AUDIO_CHANNEL 4
30
* Control register bits
34
#define C_AUTOSYNC 0x2000
40
#define C_RESET 0x0100
41
#define C_INP_1 0x0080
42
#define C_INP_0 0x0040
43
#define C_FREQ_1 0x0020
44
#define C_FREQ_0 0x0010
46
#define C_MODE24 0x0004
48
#define C_START 0x0001
51
* Status register bits
53
#define S_INTR 0x80000000
54
#define S_KMODE 0x40000000 /* 0=Quartz mode, 1=PLL mode */
55
#define S_FBITS 0x38000000 /* F bits of CS8412/14 */
57
#define S_ERF 0x04000000 /* ERF of the CS8412. 1=error */
58
#define S_REV 0x0x300000 /* Revision bits */
59
#define S_ADATLOCK 0x00080000 /* ADAT PLL locked */
61
typedef struct digi32_portc
83
typedef struct digi32_devc
92
#define MDL_96_8_PRO 3
100
oss_native_word physaddr;
107
digi32_portc portc[MAX_AUDIO_CHANNEL];
112
int active_device_count;
116
static int fbit_tab[2][8] = {
117
{0, 48000, 44100, 32000, 48000, 44100, 44056, 32000},
118
{0, 0, 0, 96000, 88200, 48000, 44100, 32000}
123
write_command (digi32_devc * devc, unsigned int ctrl)
127
PCI_WRITEL (devc->osdev, devc->pCTRL, ctrl);
132
handle_recording (int dev)
134
digi32_portc *portc = audio_engines[dev]->portc;
135
digi32_devc *devc = audio_engines[dev]->devc;
140
p = portc->inptr / 4; /* Dword addressing */
142
(unsigned int *) &audio_engines[dev]->dmap_in->
143
dmabuf[dmap_get_qtail (audio_engines[dev]->dmap_in) *
144
audio_engines[dev]->dmap_in->fragment_size];
146
/* Perform copy with channel swapping */
147
if (portc->bytes_per_sample == 4)
150
for (i = 0; i < (8 * 1024) / 4; i += 2)
152
buf[i] = PCI_READL (devc->osdev, devc->pFIFO + i + 1 + p);
153
buf[i + 1] = PCI_READL (devc->osdev, devc->pFIFO + i + p);
161
for (i = 0; i < (8 * 1024) / 4; i++)
163
tmp = PCI_READL (devc->osdev, devc->pFIFO + i + p);
164
buf[i] = ((tmp >> 16) & 0xffff) | ((tmp & 0xffff) << 16);
168
if (!(portc->trigger_bits & PCM_ENABLE_OUTPUT)) /* Clean the samples */
169
for (i = 0; i < (8 * 1024) / 4; i++)
171
PCI_WRITEL (devc->osdev, devc->pFIFO + i + p, 0);
174
portc->inptr = (portc->inptr + (8 * 1024)) % (128 * 1024); /* Increment the pointer */
178
digi32intr (oss_device_t * osdev)
181
digi32_devc *devc = (digi32_devc *) osdev->devc;
185
status = PCI_READL (devc->osdev, devc->pCTRL);
186
if (!(status & S_INTR))
189
for (i = 0; i < 2; i++)
191
portc = &devc->portc[i];
193
if (portc->trigger_bits & PCM_ENABLE_INPUT)
195
handle_recording (portc->audio_dev);
196
if (status & S_ERF && !(devc->cmd & C_AUTOSYNC))
198
cmn_err (CE_WARN, "External sync dropped\n");
200
devc->cmd |= C_AUTOSYNC;
201
write_command (devc, devc->cmd);
205
oss_audio_inputintr (portc->audio_dev, 0);
208
if (portc->trigger_bits & PCM_ENABLE_OUTPUT)
210
oss_audio_outputintr (portc->audio_dev, 1);
213
write_command (devc, devc->cmd); /* Interrupt acknowledge */
219
/***********************************
221
***********************************/
224
digi32_set_rate (int dev, int arg)
226
digi32_devc *devc = audio_engines[dev]->devc;
227
digi32_portc *portc = audio_engines[dev]->portc;
229
static int speed_table[6] = { 32000, 44100, 48000, 64000, 88200, 96000 };
230
int n, i, best = 2, dif, bestdif = 0x7fffffff;
232
if (arg == 0 || devc->active_device_count > 0 || devc->speed_locked)
235
n = (devc->model == MDL_PRO) ? 6 : 3;
237
for (i = 0; i < n; i++)
239
if (arg == speed_table[i]) /* Exact match */
246
dif = arg - speed_table[i];
257
portc->speed = speed_table[best];
258
portc->speedsel = best;
263
digi32_set_channels (int dev, short arg)
265
digi32_portc *portc = audio_engines[dev]->portc;
268
return portc->channels;
270
portc->channels = arg;
272
return portc->channels;
276
digi32_set_format (int dev, unsigned int arg)
278
digi32_devc *devc = audio_engines[dev]->devc;
279
digi32_portc *portc = audio_engines[dev]->portc;
281
if (arg == 0 || devc->active_device_count > 0)
284
if (arg != AFMT_S16_NE && arg != AFMT_S32_NE && arg != AFMT_AC3)
287
portc->bytes_per_sample = (arg & (AFMT_S32_LE | AFMT_S32_BE)) ? 4 : 2;
295
digi32_ioctl (int dev, unsigned int cmd, ioctl_arg arg)
300
static void digi32_trigger (int dev, int state);
303
digi32_reset (int dev)
305
digi32_trigger (dev, 0);
309
digi32_reset_input (int dev)
311
digi32_portc *portc = audio_engines[dev]->portc;
312
digi32_trigger (dev, portc->trigger_bits & ~PCM_ENABLE_INPUT);
316
digi32_reset_output (int dev)
318
digi32_portc *portc = audio_engines[dev]->portc;
319
digi32_trigger (dev, portc->trigger_bits & ~PCM_ENABLE_OUTPUT);
322
static void digi32_close (int dev, int mode);
326
digi32_open (int dev, int mode, int open_flags)
328
digi32_portc *portc = audio_engines[dev]->portc;
329
digi32_devc *devc = audio_engines[dev]->devc;
331
oss_native_word flags;
333
MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
334
if (portc->open_mode != 0)
336
MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
340
portc->open_mode = mode;
341
portc->audio_dev = dev;
342
devc->speed_locked = 0;
343
MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
345
for (i = 0; i < 32 * 1024; i++)
346
PCI_WRITEL (devc->osdev, devc->pFIFO + i, 0); /* Clean the buffer */
352
digi32_close (int dev, int mode)
354
digi32_portc *portc = audio_engines[dev]->portc;
355
digi32_devc *devc = audio_engines[dev]->devc;
356
oss_native_word flags;
359
MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
360
portc->open_mode = 0;
362
if (portc->tmp_autosync)
364
portc->tmp_autosync = 0;
365
devc->cmd |= C_AUTOSYNC;
366
write_command (devc, devc->cmd);
370
* Inactivate all auxiliary channels allocated for this device
373
if (--devc->open_count <= 0)
375
devc->open_count = 0;
377
MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
382
digi32_output_block (int dev, oss_native_word ptr, int count, int fragsize,
385
digi32_portc *portc = audio_engines[dev]->portc;
386
digi32_devc *devc = audio_engines[dev]->devc;
391
p = portc->outptr / 4; /* Dword addressing */
392
buf = (unsigned int *) &audio_engines[dev]->dmap_out->dmabuf[ptr];
395
/* Perform copy with channel swapping */
396
if (portc->bytes_per_sample == 4)
399
for (i = 0; i < (8 * 1024) / 4; i += 2)
401
PCI_WRITEL (devc->osdev, devc->pFIFO + i + p, buf[i + 1]);
402
PCI_WRITEL (devc->osdev, devc->pFIFO + i + 1 + p, buf[i]);
408
for (i = 0; i < (8 * 1024) / 4; i++)
410
unsigned int tmp = buf[i];
411
tmp = ((tmp >> 16) & 0xffff) | ((tmp & 0xffff) << 16);
412
PCI_WRITEL (devc->osdev, devc->pFIFO + i + p, tmp);
416
PCI_READL (devc->osdev, devc->pFIFO); /* Dummy read to flust cache on some archs */
418
portc->outptr = (portc->outptr + (8 * 1024)) % (128 * 1024); /* Increment the pointer */
422
oss_audio_outputintr (dev, 0);
428
digi32_start_input (int dev, oss_native_word ptr, int count, int fragsize,
434
digi32_trigger (int dev, int state)
436
digi32_portc *portc = audio_engines[dev]->portc;
437
digi32_devc *devc = audio_engines[dev]->devc;
439
oss_native_word flags;
441
MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
442
state &= portc->open_mode;
444
if (portc->active != state)
448
if (devc->active_device_count == 0)
452
write_command (devc, cmd | C_SEL);
453
if ((state & OPEN_WRITE) && (portc->open_mode & OPEN_WRITE))
454
portc->trigger_bits |= PCM_ENABLE_OUTPUT;
455
if ((state & OPEN_READ) && (portc->open_mode & OPEN_READ))
456
portc->trigger_bits |= PCM_ENABLE_INPUT;
457
devc->active_device_count = 1;
462
if (devc->active_device_count != 0)
465
write_command (devc, cmd);
466
if (!(state & OPEN_WRITE) && (portc->open_mode & OPEN_WRITE))
467
portc->trigger_bits &= ~PCM_ENABLE_OUTPUT;
468
if (!(state & OPEN_READ) && (portc->open_mode & OPEN_READ))
469
portc->trigger_bits &= ~PCM_ENABLE_INPUT;
470
devc->active_device_count = 0;
475
portc->active = state;
477
MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
482
digi32_prepare_for_output (int dev, int bsize, int bcount)
484
/* NOTE! This routine is used for input too */
486
digi32_devc *devc = audio_engines[dev]->devc;
487
digi32_portc *portc = audio_engines[dev]->portc;
490
int dblbit = 0, prev_dblbit, speedsel;
492
if (devc->active_device_count > 0)
495
if (bsize != 8 * 1024)
497
cmn_err (CE_CONT, "Illegal fragment size %d\n", bsize);
501
prev_dblbit = cmd & C_DS;
502
cmd &= C_EMP | C_PRO | C_SEL | C_MUTE | C_INP_0 | C_INP_1 | C_AUTOSYNC;
505
if (portc->bits & (AFMT_S32_BE | AFMT_S32_LE))
508
if (portc->speedsel > 2)
511
speedsel = (portc->speedsel % 3) + 1;
512
cmd |= (speedsel << 4) | dblbit;
514
if (dblbit != prev_dblbit)
516
write_command (devc, cmd | C_PD);
519
write_command (devc, cmd);
521
portc->trigger_bits &= ~PCM_ENABLE_OUTPUT;
527
digi32_prepare_for_input (int dev, int bsize, int bcount)
529
int i, status, fbits;
530
digi32_devc *devc = audio_engines[dev]->devc;
531
digi32_portc *portc = audio_engines[dev]->portc;
533
int dblbit = 0, prev_dblbit, speedsel;
535
if (devc->cmd & C_AUTOSYNC)
537
devc->cmd &= ~C_AUTOSYNC; /* Autosync must be used for input */
538
portc->tmp_autosync = 1;
541
write_command (devc, devc->cmd);
544
for (i = 0; i < 1000; i++)
547
status = PCI_READL (devc->osdev, devc->pCTRL);
549
if (!(status & S_ERF))
555
cmn_err (CE_WARN, "No input signal detected\n");
559
devc->speed_locked = 1;
560
fbits = (status & S_FBITS) >> S_FSHIFT;
561
portc->speed = fbit_tab[devc->cs841x_part][fbits];
563
(CE_WARN, "digi32: Measured input sampling rate is %d\n",
566
if (devc->active_device_count > 0)
569
if (bsize != 8 * 1024)
571
cmn_err (CE_CONT, "Illegal fragment size %d\n", bsize);
575
prev_dblbit = cmd & C_DS;
576
cmd &= C_EMP | C_PRO | C_SEL | C_MUTE | C_INP_0 | C_INP_1 | C_AUTOSYNC;
579
if (portc->bits & (AFMT_S32_BE | AFMT_S32_LE))
582
if (portc->speedsel > 2)
585
speedsel = (portc->speedsel % 3) + 1;
586
cmd |= (speedsel << 4) | dblbit;
588
if (dblbit != prev_dblbit)
590
write_command (devc, cmd | C_PD);
593
write_command (devc, cmd);
596
portc->trigger_bits &= ~PCM_ENABLE_INPUT;
602
digi32_alloc_buffer (int dev, dmap_t * dmap, int direction)
604
extern int digi32_buffsize;
606
if (digi32_buffsize < 512) /* Given in kilobytes */
607
digi32_buffsize *= 1024;
609
if (digi32_buffsize < 4096) /* Smaller ones not acceptable */
610
digi32_buffsize = 64 * 1024;
612
if (dmap->dmabuf != NULL)
614
dmap->dmabuf_phys = 0; /* Not mmap() capable */
615
dmap->dmabuf = KERNEL_MALLOC (digi32_buffsize);
616
if (dmap->dmabuf == NULL)
618
dmap->buffsize = digi32_buffsize;
625
digi32_free_buffer (int dev, dmap_t * dmap, int direction)
627
if (dmap->dmabuf == NULL)
629
KERNEL_FREE (dmap->dmabuf);
637
digi32_get_buffer_pointer (int dev, dmap_t * dmap, int direction)
644
digi32_setup_fragments (int dev, dmap_p dmap, int direction)
646
/* Make sure the whole sample RAM is covered by the buffer */
647
dmap->nfrags = dmap->buffsize / dmap->fragment_size;
650
static audiodrv_t digi32_output_driver = {
656
digi32_prepare_for_input,
657
digi32_prepare_for_output,
675
NULL, /* digi32_get_buffer_pointer */
682
digi32_setup_fragments
686
attach_channel (digi32_devc * devc, int chnum, char *name,
687
audiodrv_t * drv, int type)
692
if (chnum < 0 || chnum >= MAX_AUDIO_CHANNEL)
695
portc = &devc->portc[chnum];
697
ADEV_DUPLEX | ADEV_STEREOONLY | ADEV_COLD | ADEV_NOMMAP | ADEV_NOVIRTUAL;
702
if ((adev = oss_install_audiodev (OSS_AUDIO_DRIVER_VERSION,
705
name, drv, sizeof (audiodrv_t),
707
AFMT_S16_NE | AFMT_S32_NE | AFMT_AC3,
713
audio_engines[adev]->mixer_dev = devc->mixer_dev;
714
audio_engines[adev]->portc = portc;
715
audio_engines[adev]->caps |= DSP_CH_STEREO;
716
audio_engines[adev]->min_block = 8 * 1024;
717
audio_engines[adev]->max_block = 8 * 1024;
718
audio_engines[adev]->min_rate = 32000;
719
audio_engines[adev]->max_rate = (devc->model == MDL_PRO) ? 96000 : 48000;
720
portc->open_mode = 0;
721
portc->voice_chn = chnum;
722
portc->speed = 48000;
723
portc->bits = AFMT_S16_NE;
724
portc->bytes_per_sample = 2;
731
digi32_mixer_ioctl (int dev, int audiodev, unsigned int cmd, ioctl_arg arg)
733
if (cmd == SOUND_MIXER_READ_DEVMASK ||
734
cmd == SOUND_MIXER_READ_RECMASK || cmd == SOUND_MIXER_READ_RECSRC)
737
if (cmd == SOUND_MIXER_READ_VOLUME || cmd == SOUND_MIXER_READ_PCM)
738
return *arg = 100 | (100 << 8);
739
if (cmd == SOUND_MIXER_WRITE_VOLUME || cmd == SOUND_MIXER_WRITE_PCM)
740
return *arg = 100 | (100 << 8);
744
static mixer_driver_t digi32_mixer_driver = {
749
digi32_set_control (int dev, int ctrl, unsigned int cmd, int value)
751
digi32_devc *devc = mixer_devs[dev]->devc;
758
offs = (ctrl >> 4) & 0x0f; /* # of bits in the field */
759
nbits = ctrl & 0x0f; /* Shift amount */
761
if (cmd == SNDCTL_MIX_READ)
764
return (data >> offs) & ((1 << nbits) - 1);
767
if (cmd == SNDCTL_MIX_WRITE)
769
if (value < 0 || value >= (1 << nbits))
771
data = devc->cmd & ~(((1 << nbits) - 1) << offs);
772
data |= (value & ((1 << nbits) - 1)) << offs;
781
digi32_mix_init (int dev)
783
digi32_devc *devc = mixer_devs[dev]->devc;
786
if ((group = mixer_ext_create_group (dev, 0, "DIGI32")) < 0)
789
if (devc->model == MDL_PRO)
790
if ((err = mixer_ext_create_control (dev, group,
791
0xf1, digi32_set_control,
794
MIXF_READABLE | MIXF_WRITEABLE)) < 0)
797
if ((err = mixer_ext_create_control (dev, group,
798
0xd1, digi32_set_control,
801
MIXF_READABLE | MIXF_WRITEABLE)) < 0)
804
if (devc->model != MDL_8)
805
if ((err = mixer_ext_create_control (dev, group,
806
0xa1, digi32_set_control,
809
MIXF_READABLE | MIXF_WRITEABLE)) < 0)
812
if ((err = mixer_ext_create_control (dev, group,
813
0x91, digi32_set_control,
816
MIXF_READABLE | MIXF_WRITEABLE)) < 0)
819
if ((err = mixer_ext_create_control (dev, group,
820
0x62, digi32_set_control,
823
MIXF_READABLE | MIXF_WRITEABLE)) < 0)
831
init_digi32 (digi32_devc * devc)
839
if ((my_mixer = oss_install_mixer (OSS_MIXER_DRIVER_VERSION,
842
"RME Digi32 Control panel",
843
&digi32_mixer_driver,
844
sizeof (mixer_driver_t), devc)) < 0)
847
devc->mixer_dev = -1;
852
devc->mixer_dev = my_mixer;
853
mixer_devs[my_mixer]->priority = -1; /* Don't use as the default mixer */
854
mixer_ext_set_init_fn (my_mixer, digi32_mix_init, 20);
858
attach_channel (devc, 0, devc->chip_name, &digi32_output_driver, TY_BOTH);
861
sprintf (tmp, "%s (shadow)", devc->chip_name);
862
ret = attach_channel (devc, 1, tmp, &digi32_output_driver, TY_BOTH);
864
/*setup the command */
865
if (devc->model == MDL_PRO)
866
write_command (devc, C_PD); /* Reset DAC */
868
write_command (devc, C_AUTOSYNC);
874
oss_digi32_attach (oss_device_t * osdev)
877
unsigned char pci_irq_line, pci_revision;
878
unsigned short pci_command, vendor, device;
879
unsigned int pci_ioaddr;
882
DDB (cmn_err (CE_WARN, "Entered Digi32 detect routine\n"));
884
pci_read_config_word (osdev, PCI_VENDOR_ID, &vendor);
885
pci_read_config_word (osdev, PCI_DEVICE_ID, &device);
887
if ((vendor != RME_VENDOR_ID && vendor != RME_VENDOR_ID2) ||
888
(device != RME_DIGI32_PRO &&
889
device != RME_DIGI32 && device != RME_DIGI32_8))
893
pci_read_config_byte (osdev, PCI_REVISION_ID, &pci_revision);
894
pci_read_config_word (osdev, PCI_COMMAND, &pci_command);
895
pci_read_config_irq (osdev, PCI_INTERRUPT_LINE, &pci_irq_line);
896
pci_read_config_dword (osdev, PCI_MEM_BASE_ADDRESS_0, &pci_ioaddr);
900
cmn_err (CE_WARN, "BAR0 not initialized by BIOS\n");
905
if ((devc = PMALLOC (osdev, sizeof (*devc))) == NULL)
907
cmn_err (CE_WARN, "Out of memory\n");
913
devc->physaddr = pci_ioaddr;
918
devc->model = MDL_PRO;
919
if (pci_revision == 150)
921
devc->chip_name = "RME Digi 32 Pro (CS8414)";
922
devc->cs841x_part = CS8414;
926
devc->chip_name = "RME Digi 32 Pro (CS8412)";
927
devc->cs841x_part = CS8412;
932
devc->chip_name = "RME Digi 32/8";
934
devc->cs841x_part = CS8412;
938
devc->chip_name = "RME Digi32";
939
devc->model = MDL_BASIC;
940
devc->cs841x_part = CS8412;
943
case RME_DIGI96_8_PRO:
944
devc->chip_name = "RME Digi 96/8 Pro";
945
devc->model = MDL_96_8_PRO;
946
devc->cs841x_part = CS8414;
950
DDB (cmn_err (CE_WARN, "Found Digi32 at 0x%x\n", pci_ioaddr));
952
pci_command |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
953
pci_write_config_word (osdev, PCI_COMMAND, pci_command);
955
MUTEX_INIT (devc->osdev, devc->mutex, MH_DRV);
957
oss_register_device (osdev, devc->chip_name);
959
if ((err = oss_register_interrupts (devc->osdev, 0, digi32intr, NULL)) < 0)
961
cmn_err (CE_WARN, "Can't register interrupt handler, err=%d\n", err);
965
devc->linaddr = (char *) MAP_PCI_MEM (devc->osdev, 0, devc->physaddr,
967
if (devc->linaddr == NULL)
969
cmn_err (CE_WARN, "Can't ioremap PCI registers\n");
973
devc->pFIFO = (unsigned int *) devc->linaddr;
974
devc->pCTRL = (unsigned int *) (devc->linaddr + 0x20000);
976
return init_digi32 (devc); /* Detected */
980
oss_digi32_detach (oss_device_t * osdev)
982
digi32_devc *devc = (digi32_devc *) osdev->devc;
984
if (oss_disable_device (osdev) < 0)
987
oss_unregister_interrupts (devc->osdev);
989
MUTEX_CLEANUP (devc->mutex);
991
UNMAP_PCI_MEM (devc->osdev, 0, devc->physaddr, devc->linaddr,
994
oss_unregister_device (devc->osdev);