ALSA: hdspm - AES32: Enable TCO support
[deliverable/linux.git] / sound / pci / rme9652 / hdspm.c
CommitLineData
ef5fa1a4 1/*
763f356c
TI
2 * ALSA driver for RME Hammerfall DSP MADI audio interface(s)
3 *
4 * Copyright (c) 2003 Winfried Ritsch (IEM)
5 * code based on hdsp.c Paul Davis
6 * Marcus Andersson
7 * Thomas Charbonnel
3cee5a60
RB
8 * Modified 2006-06-01 for AES32 support by Remy Bruno
9 * <remy.bruno@trinnov.com>
763f356c 10 *
0dca1793
AK
11 * Modified 2009-04-13 for proper metering by Florian Faber
12 * <faber@faberman.de>
13 *
14 * Modified 2009-04-14 for native float support by Florian Faber
15 * <faber@faberman.de>
16 *
17 * Modified 2009-04-26 fixed bug in rms metering by Florian Faber
18 * <faber@faberman.de>
19 *
20 * Modified 2009-04-30 added hw serial number support by Florian Faber
21 *
22 * Modified 2011-01-14 added S/PDIF input on RayDATs by Adrian Knoth
23 *
24 * Modified 2011-01-25 variable period sizes on RayDAT/AIO by Adrian Knoth
25 *
763f356c
TI
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 *
40 */
763f356c
TI
41#include <linux/init.h>
42#include <linux/delay.h>
43#include <linux/interrupt.h>
65a77217 44#include <linux/module.h>
763f356c
TI
45#include <linux/slab.h>
46#include <linux/pci.h>
3f7440a6 47#include <linux/math64.h>
763f356c
TI
48#include <asm/io.h>
49
50#include <sound/core.h>
51#include <sound/control.h>
52#include <sound/pcm.h>
0dca1793 53#include <sound/pcm_params.h>
763f356c
TI
54#include <sound/info.h>
55#include <sound/asoundef.h>
56#include <sound/rawmidi.h>
57#include <sound/hwdep.h>
58#include <sound/initval.h>
59
60#include <sound/hdspm.h>
61
62static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
63static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
a67ff6a5 64static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
763f356c 65
763f356c
TI
66module_param_array(index, int, NULL, 0444);
67MODULE_PARM_DESC(index, "Index value for RME HDSPM interface.");
68
69module_param_array(id, charp, NULL, 0444);
70MODULE_PARM_DESC(id, "ID string for RME HDSPM interface.");
71
72module_param_array(enable, bool, NULL, 0444);
73MODULE_PARM_DESC(enable, "Enable/disable specific HDSPM soundcards.");
74
763f356c
TI
75
76MODULE_AUTHOR
0dca1793
AK
77(
78 "Winfried Ritsch <ritsch_AT_iem.at>, "
79 "Paul Davis <paul@linuxaudiosystems.com>, "
80 "Marcus Andersson, Thomas Charbonnel <thomas@undata.org>, "
81 "Remy Bruno <remy.bruno@trinnov.com>, "
82 "Florian Faber <faberman@linuxproaudio.org>, "
83 "Adrian Knoth <adi@drcomp.erfurt.thur.de>"
84);
763f356c
TI
85MODULE_DESCRIPTION("RME HDSPM");
86MODULE_LICENSE("GPL");
87MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
88
0dca1793 89/* --- Write registers. ---
763f356c
TI
90 These are defined as byte-offsets from the iobase value. */
91
0dca1793
AK
92#define HDSPM_WR_SETTINGS 0
93#define HDSPM_outputBufferAddress 32
94#define HDSPM_inputBufferAddress 36
763f356c
TI
95#define HDSPM_controlRegister 64
96#define HDSPM_interruptConfirmation 96
97#define HDSPM_control2Reg 256 /* not in specs ???????? */
ffb2c3c0 98#define HDSPM_freqReg 256 /* for AES32 */
0dca1793
AK
99#define HDSPM_midiDataOut0 352 /* just believe in old code */
100#define HDSPM_midiDataOut1 356
ffb2c3c0 101#define HDSPM_eeprom_wr 384 /* for AES32 */
763f356c
TI
102
103/* DMA enable for 64 channels, only Bit 0 is relevant */
0dca1793 104#define HDSPM_outputEnableBase 512 /* 512-767 input DMA */
763f356c
TI
105#define HDSPM_inputEnableBase 768 /* 768-1023 output DMA */
106
0dca1793 107/* 16 page addresses for each of the 64 channels DMA buffer in and out
763f356c
TI
108 (each 64k=16*4k) Buffer must be 4k aligned (which is default i386 ????) */
109#define HDSPM_pageAddressBufferOut 8192
110#define HDSPM_pageAddressBufferIn (HDSPM_pageAddressBufferOut+64*16*4)
111
112#define HDSPM_MADI_mixerBase 32768 /* 32768-65535 for 2x64x64 Fader */
113
114#define HDSPM_MATRIX_MIXER_SIZE 8192 /* = 2*64*64 * 4 Byte => 32kB */
115
116/* --- Read registers. ---
117 These are defined as byte-offsets from the iobase value */
118#define HDSPM_statusRegister 0
3cee5a60
RB
119/*#define HDSPM_statusRegister2 96 */
120/* after RME Windows driver sources, status2 is 4-byte word # 48 = word at
121 * offset 192, for AES32 *and* MADI
122 * => need to check that offset 192 is working on MADI */
123#define HDSPM_statusRegister2 192
124#define HDSPM_timecodeRegister 128
763f356c 125
0dca1793
AK
126/* AIO, RayDAT */
127#define HDSPM_RD_STATUS_0 0
128#define HDSPM_RD_STATUS_1 64
129#define HDSPM_RD_STATUS_2 128
130#define HDSPM_RD_STATUS_3 192
131
132#define HDSPM_RD_TCO 256
133#define HDSPM_RD_PLL_FREQ 512
134#define HDSPM_WR_TCO 128
135
136#define HDSPM_TCO1_TCO_lock 0x00000001
137#define HDSPM_TCO1_WCK_Input_Range_LSB 0x00000002
138#define HDSPM_TCO1_WCK_Input_Range_MSB 0x00000004
139#define HDSPM_TCO1_LTC_Input_valid 0x00000008
140#define HDSPM_TCO1_WCK_Input_valid 0x00000010
141#define HDSPM_TCO1_Video_Input_Format_NTSC 0x00000020
142#define HDSPM_TCO1_Video_Input_Format_PAL 0x00000040
143
144#define HDSPM_TCO1_set_TC 0x00000100
145#define HDSPM_TCO1_set_drop_frame_flag 0x00000200
146#define HDSPM_TCO1_LTC_Format_LSB 0x00000400
147#define HDSPM_TCO1_LTC_Format_MSB 0x00000800
148
149#define HDSPM_TCO2_TC_run 0x00010000
150#define HDSPM_TCO2_WCK_IO_ratio_LSB 0x00020000
151#define HDSPM_TCO2_WCK_IO_ratio_MSB 0x00040000
152#define HDSPM_TCO2_set_num_drop_frames_LSB 0x00080000
153#define HDSPM_TCO2_set_num_drop_frames_MSB 0x00100000
154#define HDSPM_TCO2_set_jam_sync 0x00200000
155#define HDSPM_TCO2_set_flywheel 0x00400000
156
157#define HDSPM_TCO2_set_01_4 0x01000000
158#define HDSPM_TCO2_set_pull_down 0x02000000
159#define HDSPM_TCO2_set_pull_up 0x04000000
160#define HDSPM_TCO2_set_freq 0x08000000
161#define HDSPM_TCO2_set_term_75R 0x10000000
162#define HDSPM_TCO2_set_input_LSB 0x20000000
163#define HDSPM_TCO2_set_input_MSB 0x40000000
164#define HDSPM_TCO2_set_freq_from_app 0x80000000
165
166
167#define HDSPM_midiDataOut0 352
168#define HDSPM_midiDataOut1 356
169#define HDSPM_midiDataOut2 368
170
763f356c
TI
171#define HDSPM_midiDataIn0 360
172#define HDSPM_midiDataIn1 364
0dca1793
AK
173#define HDSPM_midiDataIn2 372
174#define HDSPM_midiDataIn3 376
763f356c
TI
175
176/* status is data bytes in MIDI-FIFO (0-128) */
0dca1793
AK
177#define HDSPM_midiStatusOut0 384
178#define HDSPM_midiStatusOut1 388
179#define HDSPM_midiStatusOut2 400
180
181#define HDSPM_midiStatusIn0 392
182#define HDSPM_midiStatusIn1 396
183#define HDSPM_midiStatusIn2 404
184#define HDSPM_midiStatusIn3 408
763f356c
TI
185
186
187/* the meters are regular i/o-mapped registers, but offset
188 considerably from the rest. the peak registers are reset
0dca1793 189 when read; the least-significant 4 bits are full-scale counters;
763f356c
TI
190 the actual peak value is in the most-significant 24 bits.
191*/
0dca1793
AK
192
193#define HDSPM_MADI_INPUT_PEAK 4096
194#define HDSPM_MADI_PLAYBACK_PEAK 4352
195#define HDSPM_MADI_OUTPUT_PEAK 4608
196
197#define HDSPM_MADI_INPUT_RMS_L 6144
198#define HDSPM_MADI_PLAYBACK_RMS_L 6400
199#define HDSPM_MADI_OUTPUT_RMS_L 6656
200
201#define HDSPM_MADI_INPUT_RMS_H 7168
202#define HDSPM_MADI_PLAYBACK_RMS_H 7424
203#define HDSPM_MADI_OUTPUT_RMS_H 7680
763f356c
TI
204
205/* --- Control Register bits --------- */
206#define HDSPM_Start (1<<0) /* start engine */
207
208#define HDSPM_Latency0 (1<<1) /* buffer size = 2^n */
209#define HDSPM_Latency1 (1<<2) /* where n is defined */
210#define HDSPM_Latency2 (1<<3) /* by Latency{2,1,0} */
211
0dca1793
AK
212#define HDSPM_ClockModeMaster (1<<4) /* 1=Master, 0=Autosync */
213#define HDSPM_c0Master 0x1 /* Master clock bit in settings
214 register [RayDAT, AIO] */
763f356c
TI
215
216#define HDSPM_AudioInterruptEnable (1<<5) /* what do you think ? */
217
218#define HDSPM_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz 1=48kHz/96kHz */
219#define HDSPM_Frequency1 (1<<7) /* 0=32kHz/64kHz */
220#define HDSPM_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
3cee5a60 221#define HDSPM_QuadSpeed (1<<31) /* quad speed bit */
763f356c 222
3cee5a60 223#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
763f356c 224#define HDSPM_TX_64ch (1<<10) /* Output 64channel MODE=1,
3cee5a60
RB
225 56channelMODE=0 */ /* MADI ONLY*/
226#define HDSPM_Emphasis (1<<10) /* Emphasis */ /* AES32 ONLY */
763f356c 227
0dca1793 228#define HDSPM_AutoInp (1<<11) /* Auto Input (takeover) == Safe Mode,
3cee5a60
RB
229 0=off, 1=on */ /* MADI ONLY */
230#define HDSPM_Dolby (1<<11) /* Dolby = "NonAudio" ?? */ /* AES32 ONLY */
763f356c 231
ef5fa1a4
TI
232#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
233 * -- MADI ONLY
234 */
763f356c
TI
235#define HDSPM_InputSelect1 (1<<15) /* should be 0 */
236
3cee5a60
RB
237#define HDSPM_SyncRef2 (1<<13)
238#define HDSPM_SyncRef3 (1<<25)
763f356c 239
3cee5a60 240#define HDSPM_SMUX (1<<18) /* Frame ??? */ /* MADI ONY */
0dca1793 241#define HDSPM_clr_tms (1<<19) /* clear track marker, do not use
763f356c
TI
242 AES additional bits in
243 lower 5 Audiodatabits ??? */
3cee5a60
RB
244#define HDSPM_taxi_reset (1<<20) /* ??? */ /* MADI ONLY ? */
245#define HDSPM_WCK48 (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
763f356c 246
0dca1793
AK
247#define HDSPM_Midi0InterruptEnable 0x0400000
248#define HDSPM_Midi1InterruptEnable 0x0800000
249#define HDSPM_Midi2InterruptEnable 0x0200000
250#define HDSPM_Midi3InterruptEnable 0x4000000
763f356c
TI
251
252#define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */
0dca1793 253#define HDSPe_FLOAT_FORMAT 0x2000000
763f356c 254
3cee5a60
RB
255#define HDSPM_DS_DoubleWire (1<<26) /* AES32 ONLY */
256#define HDSPM_QS_DoubleWire (1<<27) /* AES32 ONLY */
257#define HDSPM_QS_QuadWire (1<<28) /* AES32 ONLY */
258
259#define HDSPM_wclk_sel (1<<30)
763f356c 260
384f778f
AK
261/* additional control register bits for AIO*/
262#define HDSPM_c0_Wck48 0x20 /* also RayDAT */
263#define HDSPM_c0_Input0 0x1000
264#define HDSPM_c0_Input1 0x2000
265#define HDSPM_c0_Spdif_Opt 0x4000
266#define HDSPM_c0_Pro 0x8000
267#define HDSPM_c0_clr_tms 0x10000
268#define HDSPM_c0_AEB1 0x20000
269#define HDSPM_c0_AEB2 0x40000
270#define HDSPM_c0_LineOut 0x80000
271#define HDSPM_c0_AD_GAIN0 0x100000
272#define HDSPM_c0_AD_GAIN1 0x200000
273#define HDSPM_c0_DA_GAIN0 0x400000
274#define HDSPM_c0_DA_GAIN1 0x800000
275#define HDSPM_c0_PH_GAIN0 0x1000000
276#define HDSPM_c0_PH_GAIN1 0x2000000
277#define HDSPM_c0_Sym6db 0x4000000
278
279
763f356c
TI
280/* --- bit helper defines */
281#define HDSPM_LatencyMask (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
ef5fa1a4
TI
282#define HDSPM_FrequencyMask (HDSPM_Frequency0|HDSPM_Frequency1|\
283 HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
763f356c
TI
284#define HDSPM_InputMask (HDSPM_InputSelect0|HDSPM_InputSelect1)
285#define HDSPM_InputOptical 0
286#define HDSPM_InputCoaxial (HDSPM_InputSelect0)
ef5fa1a4
TI
287#define HDSPM_SyncRefMask (HDSPM_SyncRef0|HDSPM_SyncRef1|\
288 HDSPM_SyncRef2|HDSPM_SyncRef3)
763f356c 289
0dca1793
AK
290#define HDSPM_c0_SyncRef0 0x2
291#define HDSPM_c0_SyncRef1 0x4
292#define HDSPM_c0_SyncRef2 0x8
293#define HDSPM_c0_SyncRef3 0x10
294#define HDSPM_c0_SyncRefMask (HDSPM_c0_SyncRef0 | HDSPM_c0_SyncRef1 |\
295 HDSPM_c0_SyncRef2 | HDSPM_c0_SyncRef3)
296
297#define HDSPM_SYNC_FROM_WORD 0 /* Preferred sync reference */
298#define HDSPM_SYNC_FROM_MADI 1 /* choices - used by "pref_sync_ref" */
299#define HDSPM_SYNC_FROM_TCO 2
300#define HDSPM_SYNC_FROM_SYNC_IN 3
763f356c
TI
301
302#define HDSPM_Frequency32KHz HDSPM_Frequency0
303#define HDSPM_Frequency44_1KHz HDSPM_Frequency1
304#define HDSPM_Frequency48KHz (HDSPM_Frequency1|HDSPM_Frequency0)
305#define HDSPM_Frequency64KHz (HDSPM_DoubleSpeed|HDSPM_Frequency0)
306#define HDSPM_Frequency88_2KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1)
ef5fa1a4
TI
307#define HDSPM_Frequency96KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
308 HDSPM_Frequency0)
3cee5a60
RB
309#define HDSPM_Frequency128KHz (HDSPM_QuadSpeed|HDSPM_Frequency0)
310#define HDSPM_Frequency176_4KHz (HDSPM_QuadSpeed|HDSPM_Frequency1)
ef5fa1a4
TI
311#define HDSPM_Frequency192KHz (HDSPM_QuadSpeed|HDSPM_Frequency1|\
312 HDSPM_Frequency0)
763f356c 313
763f356c
TI
314
315/* Synccheck Status */
316#define HDSPM_SYNC_CHECK_NO_LOCK 0
317#define HDSPM_SYNC_CHECK_LOCK 1
318#define HDSPM_SYNC_CHECK_SYNC 2
319
320/* AutoSync References - used by "autosync_ref" control switch */
321#define HDSPM_AUTOSYNC_FROM_WORD 0
322#define HDSPM_AUTOSYNC_FROM_MADI 1
0dca1793
AK
323#define HDSPM_AUTOSYNC_FROM_TCO 2
324#define HDSPM_AUTOSYNC_FROM_SYNC_IN 3
325#define HDSPM_AUTOSYNC_FROM_NONE 4
763f356c
TI
326
327/* Possible sources of MADI input */
328#define HDSPM_OPTICAL 0 /* optical */
329#define HDSPM_COAXIAL 1 /* BNC */
330
331#define hdspm_encode_latency(x) (((x)<<1) & HDSPM_LatencyMask)
0dca1793 332#define hdspm_decode_latency(x) ((((x) & HDSPM_LatencyMask)>>1))
763f356c
TI
333
334#define hdspm_encode_in(x) (((x)&0x3)<<14)
335#define hdspm_decode_in(x) (((x)>>14)&0x3)
336
337/* --- control2 register bits --- */
338#define HDSPM_TMS (1<<0)
339#define HDSPM_TCK (1<<1)
340#define HDSPM_TDI (1<<2)
341#define HDSPM_JTAG (1<<3)
342#define HDSPM_PWDN (1<<4)
343#define HDSPM_PROGRAM (1<<5)
344#define HDSPM_CONFIG_MODE_0 (1<<6)
345#define HDSPM_CONFIG_MODE_1 (1<<7)
346/*#define HDSPM_VERSION_BIT (1<<8) not defined any more*/
347#define HDSPM_BIGENDIAN_MODE (1<<9)
348#define HDSPM_RD_MULTIPLE (1<<10)
349
3cee5a60 350/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
ef5fa1a4
TI
351 that do not conflict with specific bits for AES32 seem to be valid also
352 for the AES32
353 */
763f356c 354#define HDSPM_audioIRQPending (1<<0) /* IRQ is high and pending */
ef5fa1a4
TI
355#define HDSPM_RX_64ch (1<<1) /* Input 64chan. MODE=1, 56chn MODE=0 */
356#define HDSPM_AB_int (1<<2) /* InputChannel Opt=0, Coax=1
357 * (like inp0)
358 */
0dca1793 359
763f356c 360#define HDSPM_madiLock (1<<3) /* MADI Locked =1, no=0 */
0dca1793
AK
361#define HDSPM_madiSync (1<<18) /* MADI is in sync */
362
b0bf5504
AK
363#define HDSPM_tcoLockMadi 0x00000020 /* Optional TCO locked status for HDSPe MADI*/
364#define HDSPM_tcoSync 0x10000000 /* Optional TCO sync status for HDSPe MADI and AES32!*/
0dca1793 365
b0bf5504
AK
366#define HDSPM_syncInLock 0x00010000 /* Sync In lock status for HDSPe MADI! */
367#define HDSPM_syncInSync 0x00020000 /* Sync In sync status for HDSPe MADI! */
763f356c
TI
368
369#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
0dca1793
AK
370 /* since 64byte accurate, last 6 bits are not used */
371
372
763f356c 373
763f356c
TI
374#define HDSPM_DoubleSpeedStatus (1<<19) /* (input) card in double speed */
375
376#define HDSPM_madiFreq0 (1<<22) /* system freq 0=error */
377#define HDSPM_madiFreq1 (1<<23) /* 1=32, 2=44.1 3=48 */
378#define HDSPM_madiFreq2 (1<<24) /* 4=64, 5=88.2 6=96 */
379#define HDSPM_madiFreq3 (1<<25) /* 7=128, 8=176.4 9=192 */
380
ef5fa1a4
TI
381#define HDSPM_BufferID (1<<26) /* (Double)Buffer ID toggles with
382 * Interrupt
383 */
0dca1793 384#define HDSPM_tco_detect 0x08000000
b0bf5504 385#define HDSPM_tcoLockAes 0x20000000 /* Optional TCO locked status for HDSPe AES */
0dca1793
AK
386
387#define HDSPM_s2_tco_detect 0x00000040
388#define HDSPM_s2_AEBO_D 0x00000080
389#define HDSPM_s2_AEBI_D 0x00000100
390
391
392#define HDSPM_midi0IRQPending 0x40000000
393#define HDSPM_midi1IRQPending 0x80000000
394#define HDSPM_midi2IRQPending 0x20000000
395#define HDSPM_midi2IRQPendingAES 0x00000020
396#define HDSPM_midi3IRQPending 0x00200000
763f356c
TI
397
398/* --- status bit helpers */
ef5fa1a4
TI
399#define HDSPM_madiFreqMask (HDSPM_madiFreq0|HDSPM_madiFreq1|\
400 HDSPM_madiFreq2|HDSPM_madiFreq3)
763f356c
TI
401#define HDSPM_madiFreq32 (HDSPM_madiFreq0)
402#define HDSPM_madiFreq44_1 (HDSPM_madiFreq1)
403#define HDSPM_madiFreq48 (HDSPM_madiFreq0|HDSPM_madiFreq1)
404#define HDSPM_madiFreq64 (HDSPM_madiFreq2)
405#define HDSPM_madiFreq88_2 (HDSPM_madiFreq0|HDSPM_madiFreq2)
406#define HDSPM_madiFreq96 (HDSPM_madiFreq1|HDSPM_madiFreq2)
407#define HDSPM_madiFreq128 (HDSPM_madiFreq0|HDSPM_madiFreq1|HDSPM_madiFreq2)
408#define HDSPM_madiFreq176_4 (HDSPM_madiFreq3)
409#define HDSPM_madiFreq192 (HDSPM_madiFreq3|HDSPM_madiFreq0)
410
3cee5a60 411/* Status2 Register bits */ /* MADI ONLY */
763f356c 412
25985edc 413#define HDSPM_version0 (1<<0) /* not really defined but I guess */
763f356c
TI
414#define HDSPM_version1 (1<<1) /* in former cards it was ??? */
415#define HDSPM_version2 (1<<2)
416
417#define HDSPM_wcLock (1<<3) /* Wordclock is detected and locked */
418#define HDSPM_wcSync (1<<4) /* Wordclock is in sync with systemclock */
419
420#define HDSPM_wc_freq0 (1<<5) /* input freq detected via autosync */
421#define HDSPM_wc_freq1 (1<<6) /* 001=32, 010==44.1, 011=48, */
a8cd7148
AK
422#define HDSPM_wc_freq2 (1<<7) /* 100=64, 101=88.2, 110=96, 111=128 */
423#define HDSPM_wc_freq3 0x800 /* 1000=176.4, 1001=192 */
763f356c 424
0dca1793
AK
425#define HDSPM_SyncRef0 0x10000 /* Sync Reference */
426#define HDSPM_SyncRef1 0x20000
427
428#define HDSPM_SelSyncRef0 (1<<8) /* AutoSync Source */
763f356c
TI
429#define HDSPM_SelSyncRef1 (1<<9) /* 000=word, 001=MADI, */
430#define HDSPM_SelSyncRef2 (1<<10) /* 111=no valid signal */
431
432#define HDSPM_wc_valid (HDSPM_wcLock|HDSPM_wcSync)
433
a8cd7148
AK
434#define HDSPM_wcFreqMask (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2|\
435 HDSPM_wc_freq3)
763f356c
TI
436#define HDSPM_wcFreq32 (HDSPM_wc_freq0)
437#define HDSPM_wcFreq44_1 (HDSPM_wc_freq1)
438#define HDSPM_wcFreq48 (HDSPM_wc_freq0|HDSPM_wc_freq1)
439#define HDSPM_wcFreq64 (HDSPM_wc_freq2)
440#define HDSPM_wcFreq88_2 (HDSPM_wc_freq0|HDSPM_wc_freq2)
441#define HDSPM_wcFreq96 (HDSPM_wc_freq1|HDSPM_wc_freq2)
a8cd7148
AK
442#define HDSPM_wcFreq128 (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2)
443#define HDSPM_wcFreq176_4 (HDSPM_wc_freq3)
444#define HDSPM_wcFreq192 (HDSPM_wc_freq0|HDSPM_wc_freq3)
763f356c 445
0dca1793
AK
446#define HDSPM_status1_F_0 0x0400000
447#define HDSPM_status1_F_1 0x0800000
448#define HDSPM_status1_F_2 0x1000000
449#define HDSPM_status1_F_3 0x2000000
450#define HDSPM_status1_freqMask (HDSPM_status1_F_0|HDSPM_status1_F_1|HDSPM_status1_F_2|HDSPM_status1_F_3)
451
763f356c 452
ef5fa1a4
TI
453#define HDSPM_SelSyncRefMask (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
454 HDSPM_SelSyncRef2)
763f356c
TI
455#define HDSPM_SelSyncRef_WORD 0
456#define HDSPM_SelSyncRef_MADI (HDSPM_SelSyncRef0)
0dca1793
AK
457#define HDSPM_SelSyncRef_TCO (HDSPM_SelSyncRef1)
458#define HDSPM_SelSyncRef_SyncIn (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1)
ef5fa1a4
TI
459#define HDSPM_SelSyncRef_NVALID (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
460 HDSPM_SelSyncRef2)
763f356c 461
3cee5a60
RB
462/*
463 For AES32, bits for status, status2 and timecode are different
464*/
465/* status */
466#define HDSPM_AES32_wcLock 0x0200000
56bde0f3 467#define HDSPM_AES32_wcSync 0x0100000
3cee5a60 468#define HDSPM_AES32_wcFreq_bit 22
0dca1793 469/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
3cee5a60
RB
470 HDSPM_bit2freq */
471#define HDSPM_AES32_syncref_bit 16
472/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */
473
474#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
475#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
476#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
477#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
478#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
479#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
480#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
481#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
482#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
b0bf5504
AK
483#define HDSPM_AES32_AUTOSYNC_FROM_TCO 9
484#define HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN 10
485#define HDSPM_AES32_AUTOSYNC_FROM_NONE 11
3cee5a60
RB
486
487/* status2 */
488/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
489#define HDSPM_LockAES 0x80
490#define HDSPM_LockAES1 0x80
491#define HDSPM_LockAES2 0x40
492#define HDSPM_LockAES3 0x20
493#define HDSPM_LockAES4 0x10
494#define HDSPM_LockAES5 0x8
495#define HDSPM_LockAES6 0x4
496#define HDSPM_LockAES7 0x2
497#define HDSPM_LockAES8 0x1
498/*
499 Timecode
500 After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
501 AES i+1
502 bits 3210
503 0001 32kHz
504 0010 44.1kHz
505 0011 48kHz
506 0100 64kHz
507 0101 88.2kHz
508 0110 96kHz
509 0111 128kHz
510 1000 176.4kHz
511 1001 192kHz
512 NB: Timecode register doesn't seem to work on AES32 card revision 230
513*/
514
763f356c
TI
515/* Mixer Values */
516#define UNITY_GAIN 32768 /* = 65536/2 */
517#define MINUS_INFINITY_GAIN 0
518
763f356c
TI
519/* Number of channels for different Speed Modes */
520#define MADI_SS_CHANNELS 64
521#define MADI_DS_CHANNELS 32
522#define MADI_QS_CHANNELS 16
523
0dca1793
AK
524#define RAYDAT_SS_CHANNELS 36
525#define RAYDAT_DS_CHANNELS 20
526#define RAYDAT_QS_CHANNELS 12
527
528#define AIO_IN_SS_CHANNELS 14
529#define AIO_IN_DS_CHANNELS 10
530#define AIO_IN_QS_CHANNELS 8
531#define AIO_OUT_SS_CHANNELS 16
532#define AIO_OUT_DS_CHANNELS 12
533#define AIO_OUT_QS_CHANNELS 10
534
d2d10a21
AK
535#define AES32_CHANNELS 16
536
763f356c
TI
537/* the size of a substream (1 mono data stream) */
538#define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024)
539#define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_CHANNEL_BUFFER_SAMPLES)
540
541/* the size of the area we need to allocate for DMA transfers. the
542 size is the same regardless of the number of channels, and
0dca1793 543 also the latency to use.
763f356c
TI
544 for one direction !!!
545*/
ffb2c3c0 546#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
763f356c
TI
547#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
548
0dca1793
AK
549#define HDSPM_RAYDAT_REV 211
550#define HDSPM_AIO_REV 212
551#define HDSPM_MADIFACE_REV 213
3cee5a60 552
6534599d
RB
553/* speed factor modes */
554#define HDSPM_SPEED_SINGLE 0
555#define HDSPM_SPEED_DOUBLE 1
556#define HDSPM_SPEED_QUAD 2
0dca1793 557
6534599d
RB
558/* names for speed modes */
559static char *hdspm_speed_names[] = { "single", "double", "quad" };
560
0dca1793
AK
561static char *texts_autosync_aes_tco[] = { "Word Clock",
562 "AES1", "AES2", "AES3", "AES4",
563 "AES5", "AES6", "AES7", "AES8",
db2d1a91
AK
564 "TCO", "Sync In"
565};
0dca1793
AK
566static char *texts_autosync_aes[] = { "Word Clock",
567 "AES1", "AES2", "AES3", "AES4",
db2d1a91
AK
568 "AES5", "AES6", "AES7", "AES8",
569 "Sync In"
570};
0dca1793
AK
571static char *texts_autosync_madi_tco[] = { "Word Clock",
572 "MADI", "TCO", "Sync In" };
573static char *texts_autosync_madi[] = { "Word Clock",
574 "MADI", "Sync In" };
575
576static char *texts_autosync_raydat_tco[] = {
577 "Word Clock",
578 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
579 "AES", "SPDIF", "TCO", "Sync In"
580};
581static char *texts_autosync_raydat[] = {
582 "Word Clock",
583 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
584 "AES", "SPDIF", "Sync In"
585};
586static char *texts_autosync_aio_tco[] = {
587 "Word Clock",
588 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
589};
590static char *texts_autosync_aio[] = { "Word Clock",
591 "ADAT", "AES", "SPDIF", "Sync In" };
592
593static char *texts_freq[] = {
594 "No Lock",
595 "32 kHz",
596 "44.1 kHz",
597 "48 kHz",
598 "64 kHz",
599 "88.2 kHz",
600 "96 kHz",
601 "128 kHz",
602 "176.4 kHz",
603 "192 kHz"
604};
605
0dca1793
AK
606static char *texts_ports_madi[] = {
607 "MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
608 "MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
609 "MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
610 "MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
611 "MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
612 "MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
613 "MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
614 "MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
615 "MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
616 "MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
617 "MADI.61", "MADI.62", "MADI.63", "MADI.64",
618};
619
620
621static char *texts_ports_raydat_ss[] = {
622 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
623 "ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
624 "ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
625 "ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
626 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
627 "ADAT4.7", "ADAT4.8",
628 "AES.L", "AES.R",
629 "SPDIF.L", "SPDIF.R"
630};
631
632static char *texts_ports_raydat_ds[] = {
633 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
634 "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
635 "ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
636 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
637 "AES.L", "AES.R",
638 "SPDIF.L", "SPDIF.R"
639};
640
641static char *texts_ports_raydat_qs[] = {
642 "ADAT1.1", "ADAT1.2",
643 "ADAT2.1", "ADAT2.2",
644 "ADAT3.1", "ADAT3.2",
645 "ADAT4.1", "ADAT4.2",
646 "AES.L", "AES.R",
647 "SPDIF.L", "SPDIF.R"
648};
649
650
651static char *texts_ports_aio_in_ss[] = {
652 "Analogue.L", "Analogue.R",
653 "AES.L", "AES.R",
654 "SPDIF.L", "SPDIF.R",
655 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
3de9db26
AK
656 "ADAT.7", "ADAT.8",
657 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
658};
659
660static char *texts_ports_aio_out_ss[] = {
661 "Analogue.L", "Analogue.R",
662 "AES.L", "AES.R",
663 "SPDIF.L", "SPDIF.R",
664 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
665 "ADAT.7", "ADAT.8",
3de9db26
AK
666 "Phone.L", "Phone.R",
667 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
668};
669
670static char *texts_ports_aio_in_ds[] = {
671 "Analogue.L", "Analogue.R",
672 "AES.L", "AES.R",
673 "SPDIF.L", "SPDIF.R",
3de9db26
AK
674 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
675 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
676};
677
678static char *texts_ports_aio_out_ds[] = {
679 "Analogue.L", "Analogue.R",
680 "AES.L", "AES.R",
681 "SPDIF.L", "SPDIF.R",
682 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
3de9db26
AK
683 "Phone.L", "Phone.R",
684 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
685};
686
687static char *texts_ports_aio_in_qs[] = {
688 "Analogue.L", "Analogue.R",
689 "AES.L", "AES.R",
690 "SPDIF.L", "SPDIF.R",
3de9db26
AK
691 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
692 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
693};
694
695static char *texts_ports_aio_out_qs[] = {
696 "Analogue.L", "Analogue.R",
697 "AES.L", "AES.R",
698 "SPDIF.L", "SPDIF.R",
699 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
3de9db26
AK
700 "Phone.L", "Phone.R",
701 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
0dca1793
AK
702};
703
432d2500
AK
704static char *texts_ports_aes32[] = {
705 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
706 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
707 "AES.15", "AES.16"
708};
709
55a57606
AK
710/* These tables map the ALSA channels 1..N to the channels that we
711 need to use in order to find the relevant channel buffer. RME
712 refers to this kind of mapping as between "the ADAT channel and
713 the DMA channel." We index it using the logical audio channel,
714 and the value is the DMA channel (i.e. channel buffer number)
715 where the data for that channel can be read/written from/to.
716*/
717
718static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
719 0, 1, 2, 3, 4, 5, 6, 7,
720 8, 9, 10, 11, 12, 13, 14, 15,
721 16, 17, 18, 19, 20, 21, 22, 23,
722 24, 25, 26, 27, 28, 29, 30, 31,
723 32, 33, 34, 35, 36, 37, 38, 39,
724 40, 41, 42, 43, 44, 45, 46, 47,
725 48, 49, 50, 51, 52, 53, 54, 55,
726 56, 57, 58, 59, 60, 61, 62, 63
727};
728
55a57606
AK
729static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
730 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
731 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
732 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
733 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
734 0, 1, /* AES */
735 2, 3, /* SPDIF */
736 -1, -1, -1, -1,
737 -1, -1, -1, -1, -1, -1, -1, -1,
738 -1, -1, -1, -1, -1, -1, -1, -1,
739 -1, -1, -1, -1, -1, -1, -1, -1,
740};
741
742static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
743 4, 5, 6, 7, /* ADAT 1 */
744 8, 9, 10, 11, /* ADAT 2 */
745 12, 13, 14, 15, /* ADAT 3 */
746 16, 17, 18, 19, /* ADAT 4 */
747 0, 1, /* AES */
748 2, 3, /* SPDIF */
749 -1, -1, -1, -1,
750 -1, -1, -1, -1, -1, -1, -1, -1,
751 -1, -1, -1, -1, -1, -1, -1, -1,
752 -1, -1, -1, -1, -1, -1, -1, -1,
753 -1, -1, -1, -1, -1, -1, -1, -1,
754 -1, -1, -1, -1, -1, -1, -1, -1,
755};
756
757static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
758 4, 5, /* ADAT 1 */
759 6, 7, /* ADAT 2 */
760 8, 9, /* ADAT 3 */
761 10, 11, /* ADAT 4 */
762 0, 1, /* AES */
763 2, 3, /* SPDIF */
764 -1, -1, -1, -1,
765 -1, -1, -1, -1, -1, -1, -1, -1,
766 -1, -1, -1, -1, -1, -1, -1, -1,
767 -1, -1, -1, -1, -1, -1, -1, -1,
768 -1, -1, -1, -1, -1, -1, -1, -1,
769 -1, -1, -1, -1, -1, -1, -1, -1,
770 -1, -1, -1, -1, -1, -1, -1, -1,
771};
772
773static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
774 0, 1, /* line in */
775 8, 9, /* aes in, */
776 10, 11, /* spdif in */
777 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
3de9db26
AK
778 2, 3, 4, 5, /* AEB */
779 -1, -1, -1, -1, -1, -1,
55a57606
AK
780 -1, -1, -1, -1, -1, -1, -1, -1,
781 -1, -1, -1, -1, -1, -1, -1, -1,
782 -1, -1, -1, -1, -1, -1, -1, -1,
783 -1, -1, -1, -1, -1, -1, -1, -1,
784 -1, -1, -1, -1, -1, -1, -1, -1,
785};
786
787static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
788 0, 1, /* line out */
789 8, 9, /* aes out */
790 10, 11, /* spdif out */
791 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
792 6, 7, /* phone out */
3de9db26
AK
793 2, 3, 4, 5, /* AEB */
794 -1, -1, -1, -1,
55a57606
AK
795 -1, -1, -1, -1, -1, -1, -1, -1,
796 -1, -1, -1, -1, -1, -1, -1, -1,
797 -1, -1, -1, -1, -1, -1, -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1,
799 -1, -1, -1, -1, -1, -1, -1, -1,
800};
801
802static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
803 0, 1, /* line in */
804 8, 9, /* aes in */
805 10, 11, /* spdif in */
806 12, 14, 16, 18, /* adat in */
3de9db26
AK
807 2, 3, 4, 5, /* AEB */
808 -1, -1,
55a57606
AK
809 -1, -1, -1, -1, -1, -1, -1, -1,
810 -1, -1, -1, -1, -1, -1, -1, -1,
811 -1, -1, -1, -1, -1, -1, -1, -1,
812 -1, -1, -1, -1, -1, -1, -1, -1,
813 -1, -1, -1, -1, -1, -1, -1, -1,
814 -1, -1, -1, -1, -1, -1, -1, -1
815};
816
817static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
818 0, 1, /* line out */
819 8, 9, /* aes out */
820 10, 11, /* spdif out */
821 12, 14, 16, 18, /* adat out */
822 6, 7, /* phone out */
3de9db26 823 2, 3, 4, 5, /* AEB */
55a57606
AK
824 -1, -1, -1, -1, -1, -1, -1, -1,
825 -1, -1, -1, -1, -1, -1, -1, -1,
826 -1, -1, -1, -1, -1, -1, -1, -1,
827 -1, -1, -1, -1, -1, -1, -1, -1,
828 -1, -1, -1, -1, -1, -1, -1, -1,
829 -1, -1, -1, -1, -1, -1, -1, -1
830};
831
832static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
833 0, 1, /* line in */
834 8, 9, /* aes in */
835 10, 11, /* spdif in */
836 12, 16, /* adat in */
3de9db26
AK
837 2, 3, 4, 5, /* AEB */
838 -1, -1, -1, -1,
55a57606
AK
839 -1, -1, -1, -1, -1, -1, -1, -1,
840 -1, -1, -1, -1, -1, -1, -1, -1,
841 -1, -1, -1, -1, -1, -1, -1, -1,
842 -1, -1, -1, -1, -1, -1, -1, -1,
843 -1, -1, -1, -1, -1, -1, -1, -1,
844 -1, -1, -1, -1, -1, -1, -1, -1
845};
846
847static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
848 0, 1, /* line out */
849 8, 9, /* aes out */
850 10, 11, /* spdif out */
851 12, 16, /* adat out */
852 6, 7, /* phone out */
3de9db26
AK
853 2, 3, 4, 5, /* AEB */
854 -1, -1,
55a57606
AK
855 -1, -1, -1, -1, -1, -1, -1, -1,
856 -1, -1, -1, -1, -1, -1, -1, -1,
857 -1, -1, -1, -1, -1, -1, -1, -1,
858 -1, -1, -1, -1, -1, -1, -1, -1,
859 -1, -1, -1, -1, -1, -1, -1, -1,
860 -1, -1, -1, -1, -1, -1, -1, -1
861};
862
432d2500
AK
863static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
864 0, 1, 2, 3, 4, 5, 6, 7,
865 8, 9, 10, 11, 12, 13, 14, 15,
866 -1, -1, -1, -1, -1, -1, -1, -1,
867 -1, -1, -1, -1, -1, -1, -1, -1,
868 -1, -1, -1, -1, -1, -1, -1, -1,
869 -1, -1, -1, -1, -1, -1, -1, -1,
870 -1, -1, -1, -1, -1, -1, -1, -1,
871 -1, -1, -1, -1, -1, -1, -1, -1
872};
873
98274f07
TI
874struct hdspm_midi {
875 struct hdspm *hdspm;
763f356c 876 int id;
98274f07
TI
877 struct snd_rawmidi *rmidi;
878 struct snd_rawmidi_substream *input;
879 struct snd_rawmidi_substream *output;
763f356c
TI
880 char istimer; /* timer in use */
881 struct timer_list timer;
882 spinlock_t lock;
883 int pending;
0dca1793
AK
884 int dataIn;
885 int statusIn;
886 int dataOut;
887 int statusOut;
888 int ie;
889 int irq;
890};
891
892struct hdspm_tco {
893 int input;
894 int framerate;
895 int wordclock;
896 int samplerate;
897 int pull;
898 int term; /* 0 = off, 1 = on */
763f356c
TI
899};
900
98274f07 901struct hdspm {
763f356c 902 spinlock_t lock;
ef5fa1a4
TI
903 /* only one playback and/or capture stream */
904 struct snd_pcm_substream *capture_substream;
905 struct snd_pcm_substream *playback_substream;
763f356c
TI
906
907 char *card_name; /* for procinfo */
3cee5a60
RB
908 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
909
0dca1793 910 uint8_t io_type;
763f356c 911
763f356c
TI
912 int monitor_outs; /* set up monitoring outs init flag */
913
914 u32 control_register; /* cached value */
915 u32 control2_register; /* cached value */
0dca1793 916 u32 settings_register;
763f356c 917
0dca1793 918 struct hdspm_midi midi[4];
763f356c
TI
919 struct tasklet_struct midi_tasklet;
920
921 size_t period_bytes;
0dca1793
AK
922 unsigned char ss_in_channels;
923 unsigned char ds_in_channels;
924 unsigned char qs_in_channels;
925 unsigned char ss_out_channels;
926 unsigned char ds_out_channels;
927 unsigned char qs_out_channels;
928
929 unsigned char max_channels_in;
930 unsigned char max_channels_out;
931
286bed0f
TI
932 signed char *channel_map_in;
933 signed char *channel_map_out;
0dca1793 934
286bed0f
TI
935 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
936 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
0dca1793
AK
937
938 char **port_names_in;
939 char **port_names_out;
940
941 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
942 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
763f356c
TI
943
944 unsigned char *playback_buffer; /* suitably aligned address */
945 unsigned char *capture_buffer; /* suitably aligned address */
946
947 pid_t capture_pid; /* process id which uses capture */
948 pid_t playback_pid; /* process id which uses capture */
949 int running; /* running status */
950
951 int last_external_sample_rate; /* samplerate mystic ... */
952 int last_internal_sample_rate;
953 int system_sample_rate;
954
763f356c
TI
955 int dev; /* Hardware vars... */
956 int irq;
957 unsigned long port;
958 void __iomem *iobase;
959
960 int irq_count; /* for debug */
0dca1793 961 int midiPorts;
763f356c 962
98274f07
TI
963 struct snd_card *card; /* one card */
964 struct snd_pcm *pcm; /* has one pcm */
965 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
763f356c
TI
966 struct pci_dev *pci; /* and an pci info */
967
968 /* Mixer vars */
ef5fa1a4
TI
969 /* fast alsa mixer */
970 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
971 /* but input to much, so not used */
972 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
25985edc 973 /* full mixer accessible over mixer ioctl or hwdep-device */
ef5fa1a4 974 struct hdspm_mixer *mixer;
763f356c 975
0dca1793 976 struct hdspm_tco *tco; /* NULL if no TCO detected */
763f356c 977
0dca1793
AK
978 char **texts_autosync;
979 int texts_autosync_items;
763f356c 980
0dca1793 981 cycles_t last_interrupt;
730a5865 982
7d53a631
AK
983 unsigned int serial;
984
730a5865 985 struct hdspm_peak_rms peak_rms;
763f356c
TI
986};
987
763f356c 988
cebe41d4 989static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
763f356c
TI
990 {
991 .vendor = PCI_VENDOR_ID_XILINX,
992 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
993 .subvendor = PCI_ANY_ID,
994 .subdevice = PCI_ANY_ID,
995 .class = 0,
996 .class_mask = 0,
997 .driver_data = 0},
998 {0,}
999};
1000
1001MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
1002
1003/* prototypes */
e23e7a14
BP
1004static int snd_hdspm_create_alsa_devices(struct snd_card *card,
1005 struct hdspm *hdspm);
1006static int snd_hdspm_create_pcm(struct snd_card *card,
1007 struct hdspm *hdspm);
98274f07 1008
0dca1793 1009static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
3f7bf918 1010static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
0dca1793
AK
1011static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
1012static int hdspm_autosync_ref(struct hdspm *hdspm);
34be7ebb 1013static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out);
0dca1793 1014static int snd_hdspm_set_defaults(struct hdspm *hdspm);
21a164df 1015static int hdspm_system_clock_mode(struct hdspm *hdspm);
0dca1793 1016static void hdspm_set_sgbuf(struct hdspm *hdspm,
77a23f26 1017 struct snd_pcm_substream *substream,
763f356c
TI
1018 unsigned int reg, int channels);
1019
5b266354
AK
1020static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx);
1021static int hdspm_wc_sync_check(struct hdspm *hdspm);
1022static int hdspm_tco_sync_check(struct hdspm *hdspm);
1023static int hdspm_sync_in_sync_check(struct hdspm *hdspm);
1024
1025static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index);
1026static int hdspm_get_tco_sample_rate(struct hdspm *hdspm);
1027static int hdspm_get_wc_sample_rate(struct hdspm *hdspm);
1028
1029
1030
3cee5a60
RB
1031static inline int HDSPM_bit2freq(int n)
1032{
62cef821
DV
1033 static const int bit2freq_tab[] = {
1034 0, 32000, 44100, 48000, 64000, 88200,
3cee5a60
RB
1035 96000, 128000, 176400, 192000 };
1036 if (n < 1 || n > 9)
1037 return 0;
1038 return bit2freq_tab[n];
1039}
1040
b2ed6326
AK
1041static bool hdspm_is_raydat_or_aio(struct hdspm *hdspm)
1042{
1043 return ((AIO == hdspm->io_type) || (RayDAT == hdspm->io_type));
1044}
1045
1046
0dca1793 1047/* Write/read to/from HDSPM with Adresses in Bytes
763f356c
TI
1048 not words but only 32Bit writes are allowed */
1049
98274f07 1050static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
763f356c
TI
1051 unsigned int val)
1052{
1053 writel(val, hdspm->iobase + reg);
1054}
1055
98274f07 1056static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
763f356c
TI
1057{
1058 return readl(hdspm->iobase + reg);
1059}
1060
0dca1793
AK
1061/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1062 mixer is write only on hardware so we have to cache him for read
763f356c
TI
1063 each fader is a u32, but uses only the first 16 bit */
1064
98274f07 1065static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
763f356c
TI
1066 unsigned int in)
1067{
5bab2482 1068 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
763f356c
TI
1069 return 0;
1070
1071 return hdspm->mixer->ch[chan].in[in];
1072}
1073
98274f07 1074static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
763f356c
TI
1075 unsigned int pb)
1076{
5bab2482 1077 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
763f356c
TI
1078 return 0;
1079 return hdspm->mixer->ch[chan].pb[pb];
1080}
1081
62cef821 1082static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
763f356c
TI
1083 unsigned int in, unsigned short data)
1084{
1085 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1086 return -1;
1087
1088 hdspm_write(hdspm,
1089 HDSPM_MADI_mixerBase +
1090 ((in + 128 * chan) * sizeof(u32)),
1091 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1092 return 0;
1093}
1094
62cef821 1095static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
763f356c
TI
1096 unsigned int pb, unsigned short data)
1097{
1098 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1099 return -1;
1100
1101 hdspm_write(hdspm,
1102 HDSPM_MADI_mixerBase +
1103 ((64 + pb + 128 * chan) * sizeof(u32)),
1104 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1105 return 0;
1106}
1107
1108
1109/* enable DMA for specific channels, now available for DSP-MADI */
98274f07 1110static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
763f356c
TI
1111{
1112 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1113}
1114
98274f07 1115static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
763f356c
TI
1116{
1117 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1118}
1119
1120/* check if same process is writing and reading */
62cef821 1121static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
763f356c
TI
1122{
1123 unsigned long flags;
1124 int ret = 1;
1125
1126 spin_lock_irqsave(&hdspm->lock, flags);
1127 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1128 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1129 ret = 0;
1130 }
1131 spin_unlock_irqrestore(&hdspm->lock, flags);
1132 return ret;
1133}
1134
fcdc4ba1
AK
1135/* round arbitary sample rates to commonly known rates */
1136static int hdspm_round_frequency(int rate)
1137{
1138 if (rate < 38050)
1139 return 32000;
1140 if (rate < 46008)
1141 return 44100;
1142 else
1143 return 48000;
1144}
1145
a8a729fa
AK
1146/* QS and DS rates normally can not be detected
1147 * automatically by the card. Only exception is MADI
1148 * in 96k frame mode.
1149 *
1150 * So if we read SS values (32 .. 48k), check for
1151 * user-provided DS/QS bits in the control register
1152 * and multiply the base frequency accordingly.
1153 */
1154static int hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
1155{
1156 if (rate <= 48000) {
1157 if (hdspm->control_register & HDSPM_QuadSpeed)
1158 return rate * 4;
1159 else if (hdspm->control_register &
1160 HDSPM_DoubleSpeed)
1161 return rate * 2;
1162 };
1163 return rate;
1164}
1165
5b266354 1166/* check for external sample rate, returns the sample rate in Hz*/
62cef821 1167static int hdspm_external_sample_rate(struct hdspm *hdspm)
763f356c 1168{
0dca1793
AK
1169 unsigned int status, status2, timecode;
1170 int syncref, rate = 0, rate_bits;
3cee5a60 1171
0dca1793
AK
1172 switch (hdspm->io_type) {
1173 case AES32:
1174 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1175 status = hdspm_read(hdspm, HDSPM_statusRegister);
7c4a95b5 1176 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
0dca1793
AK
1177
1178 syncref = hdspm_autosync_ref(hdspm);
dbae4a0c
AK
1179 switch (syncref) {
1180 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
1181 /* Check WC sync and get sample rate */
1182 if (hdspm_wc_sync_check(hdspm))
1183 return HDSPM_bit2freq(hdspm_get_wc_sample_rate(hdspm));
1184 break;
1185
1186 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
1187 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
1188 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
1189 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
1190 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
1191 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
1192 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
1193 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
1194 /* Check AES sync and get sample rate */
1195 if (hdspm_aes_sync_check(hdspm, syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1))
1196 return HDSPM_bit2freq(hdspm_get_aes_sample_rate(hdspm,
1197 syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1));
1198 break;
1199
1200
1201 case HDSPM_AES32_AUTOSYNC_FROM_TCO:
1202 /* Check TCO sync and get sample rate */
1203 if (hdspm_tco_sync_check(hdspm))
1204 return HDSPM_bit2freq(hdspm_get_tco_sample_rate(hdspm));
1205 break;
1206 default:
1207 return 0;
1208 } /* end switch(syncref) */
0dca1793
AK
1209 break;
1210
1211 case MADIface:
1212 status = hdspm_read(hdspm, HDSPM_statusRegister);
1213
1214 if (!(status & HDSPM_madiLock)) {
1215 rate = 0; /* no lock */
1216 } else {
1217 switch (status & (HDSPM_status1_freqMask)) {
1218 case HDSPM_status1_F_0*1:
1219 rate = 32000; break;
1220 case HDSPM_status1_F_0*2:
1221 rate = 44100; break;
1222 case HDSPM_status1_F_0*3:
1223 rate = 48000; break;
1224 case HDSPM_status1_F_0*4:
1225 rate = 64000; break;
1226 case HDSPM_status1_F_0*5:
1227 rate = 88200; break;
1228 case HDSPM_status1_F_0*6:
1229 rate = 96000; break;
1230 case HDSPM_status1_F_0*7:
1231 rate = 128000; break;
1232 case HDSPM_status1_F_0*8:
1233 rate = 176400; break;
1234 case HDSPM_status1_F_0*9:
1235 rate = 192000; break;
1236 default:
1237 rate = 0; break;
1238 }
1239 }
1240
1241 break;
1242
1243 case MADI:
1244 case AIO:
1245 case RayDAT:
1246 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1247 status = hdspm_read(hdspm, HDSPM_statusRegister);
1248 rate = 0;
763f356c 1249
3cee5a60
RB
1250 /* if wordclock has synced freq and wordclock is valid */
1251 if ((status2 & HDSPM_wcLock) != 0 &&
fedf1535 1252 (status2 & HDSPM_SelSyncRef0) == 0) {
763f356c 1253
3cee5a60 1254 rate_bits = status2 & HDSPM_wcFreqMask;
763f356c 1255
0dca1793 1256
3cee5a60
RB
1257 switch (rate_bits) {
1258 case HDSPM_wcFreq32:
1259 rate = 32000;
1260 break;
1261 case HDSPM_wcFreq44_1:
1262 rate = 44100;
1263 break;
1264 case HDSPM_wcFreq48:
1265 rate = 48000;
1266 break;
1267 case HDSPM_wcFreq64:
1268 rate = 64000;
1269 break;
1270 case HDSPM_wcFreq88_2:
1271 rate = 88200;
1272 break;
1273 case HDSPM_wcFreq96:
1274 rate = 96000;
1275 break;
a8cd7148
AK
1276 case HDSPM_wcFreq128:
1277 rate = 128000;
1278 break;
1279 case HDSPM_wcFreq176_4:
1280 rate = 176400;
1281 break;
1282 case HDSPM_wcFreq192:
1283 rate = 192000;
1284 break;
3cee5a60
RB
1285 default:
1286 rate = 0;
1287 break;
1288 }
763f356c 1289 }
763f356c 1290
ef5fa1a4
TI
1291 /* if rate detected and Syncref is Word than have it,
1292 * word has priority to MADI
1293 */
3cee5a60 1294 if (rate != 0 &&
0dca1793 1295 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
7b559397 1296 return hdspm_rate_multiplier(hdspm, rate);
763f356c 1297
0dca1793 1298 /* maybe a madi input (which is taken if sel sync is madi) */
3cee5a60
RB
1299 if (status & HDSPM_madiLock) {
1300 rate_bits = status & HDSPM_madiFreqMask;
763f356c 1301
3cee5a60
RB
1302 switch (rate_bits) {
1303 case HDSPM_madiFreq32:
1304 rate = 32000;
1305 break;
1306 case HDSPM_madiFreq44_1:
1307 rate = 44100;
1308 break;
1309 case HDSPM_madiFreq48:
1310 rate = 48000;
1311 break;
1312 case HDSPM_madiFreq64:
1313 rate = 64000;
1314 break;
1315 case HDSPM_madiFreq88_2:
1316 rate = 88200;
1317 break;
1318 case HDSPM_madiFreq96:
1319 rate = 96000;
1320 break;
1321 case HDSPM_madiFreq128:
1322 rate = 128000;
1323 break;
1324 case HDSPM_madiFreq176_4:
1325 rate = 176400;
1326 break;
1327 case HDSPM_madiFreq192:
1328 rate = 192000;
1329 break;
1330 default:
1331 rate = 0;
1332 break;
1333 }
d12c51d8 1334
fcdc4ba1
AK
1335 } /* endif HDSPM_madiLock */
1336
1337 /* check sample rate from TCO or SYNC_IN */
1338 {
1339 bool is_valid_input = 0;
1340 bool has_sync = 0;
1341
1342 syncref = hdspm_autosync_ref(hdspm);
1343 if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
1344 is_valid_input = 1;
1345 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1346 hdspm_tco_sync_check(hdspm));
1347 } else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
1348 is_valid_input = 1;
1349 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1350 hdspm_sync_in_sync_check(hdspm));
d12c51d8 1351 }
fcdc4ba1
AK
1352
1353 if (is_valid_input && has_sync) {
1354 rate = hdspm_round_frequency(
1355 hdspm_get_pll_freq(hdspm));
1356 }
1357 }
1358
a8a729fa
AK
1359 rate = hdspm_rate_multiplier(hdspm, rate);
1360
0dca1793 1361 break;
763f356c 1362 }
0dca1793
AK
1363
1364 return rate;
763f356c
TI
1365}
1366
7cb155ff
AK
1367/* return latency in samples per period */
1368static int hdspm_get_latency(struct hdspm *hdspm)
1369{
1370 int n;
1371
1372 n = hdspm_decode_latency(hdspm->control_register);
1373
1374 /* Special case for new RME cards with 32 samples period size.
1375 * The three latency bits in the control register
1376 * (HDSP_LatencyMask) encode latency values of 64 samples as
1377 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
1378 * denotes 8192 samples, but on new cards like RayDAT or AIO,
1379 * it corresponds to 32 samples.
1380 */
1381 if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
1382 n = -1;
1383
1384 return 1 << (n + 6);
1385}
1386
763f356c 1387/* Latency function */
0dca1793 1388static inline void hdspm_compute_period_size(struct hdspm *hdspm)
763f356c 1389{
7cb155ff 1390 hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
763f356c
TI
1391}
1392
0dca1793
AK
1393
1394static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
763f356c
TI
1395{
1396 int position;
1397
1398 position = hdspm_read(hdspm, HDSPM_statusRegister);
483cee77
AK
1399
1400 switch (hdspm->io_type) {
1401 case RayDAT:
1402 case AIO:
1403 position &= HDSPM_BufferPositionMask;
1404 position /= 4; /* Bytes per sample */
1405 break;
1406 default:
1407 position = (position & HDSPM_BufferID) ?
1408 (hdspm->period_bytes / 4) : 0;
1409 }
763f356c
TI
1410
1411 return position;
1412}
1413
1414
98274f07 1415static inline void hdspm_start_audio(struct hdspm * s)
763f356c
TI
1416{
1417 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1418 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1419}
1420
98274f07 1421static inline void hdspm_stop_audio(struct hdspm * s)
763f356c
TI
1422{
1423 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1424 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1425}
1426
1427/* should I silence all or only opened ones ? doit all for first even is 4MB*/
62cef821 1428static void hdspm_silence_playback(struct hdspm *hdspm)
763f356c
TI
1429{
1430 int i;
1431 int n = hdspm->period_bytes;
1432 void *buf = hdspm->playback_buffer;
1433
3cee5a60
RB
1434 if (buf == NULL)
1435 return;
763f356c
TI
1436
1437 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1438 memset(buf, 0, n);
1439 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1440 }
1441}
1442
0dca1793 1443static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
763f356c
TI
1444{
1445 int n;
1446
1447 spin_lock_irq(&s->lock);
1448
2e610270
AK
1449 if (32 == frames) {
1450 /* Special case for new RME cards like RayDAT/AIO which
1451 * support period sizes of 32 samples. Since latency is
1452 * encoded in the three bits of HDSP_LatencyMask, we can only
1453 * have values from 0 .. 7. While 0 still means 64 samples and
1454 * 6 represents 4096 samples on all cards, 7 represents 8192
1455 * on older cards and 32 samples on new cards.
1456 *
1457 * In other words, period size in samples is calculated by
1458 * 2^(n+6) with n ranging from 0 .. 7.
1459 */
1460 n = 7;
1461 } else {
1462 frames >>= 7;
1463 n = 0;
1464 while (frames) {
1465 n++;
1466 frames >>= 1;
1467 }
763f356c 1468 }
2e610270 1469
763f356c
TI
1470 s->control_register &= ~HDSPM_LatencyMask;
1471 s->control_register |= hdspm_encode_latency(n);
1472
1473 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1474
1475 hdspm_compute_period_size(s);
1476
1477 spin_unlock_irq(&s->lock);
1478
1479 return 0;
1480}
1481
0dca1793
AK
1482static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1483{
1484 u64 freq_const;
1485
1486 if (period == 0)
1487 return 0;
1488
1489 switch (hdspm->io_type) {
1490 case MADI:
1491 case AES32:
1492 freq_const = 110069313433624ULL;
1493 break;
1494 case RayDAT:
1495 case AIO:
1496 freq_const = 104857600000000ULL;
1497 break;
1498 case MADIface:
1499 freq_const = 131072000000000ULL;
3d56c8e6
TI
1500 break;
1501 default:
1502 snd_BUG();
1503 return 0;
0dca1793
AK
1504 }
1505
1506 return div_u64(freq_const, period);
1507}
1508
1509
ffb2c3c0
RB
1510static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1511{
1512 u64 n;
0dca1793 1513
ffb2c3c0
RB
1514 if (rate >= 112000)
1515 rate /= 4;
1516 else if (rate >= 56000)
1517 rate /= 2;
1518
0dca1793
AK
1519 switch (hdspm->io_type) {
1520 case MADIface:
3d56c8e6
TI
1521 n = 131072000000000ULL; /* 125 MHz */
1522 break;
0dca1793
AK
1523 case MADI:
1524 case AES32:
3d56c8e6
TI
1525 n = 110069313433624ULL; /* 105 MHz */
1526 break;
0dca1793
AK
1527 case RayDAT:
1528 case AIO:
3d56c8e6
TI
1529 n = 104857600000000ULL; /* 100 MHz */
1530 break;
1531 default:
1532 snd_BUG();
1533 return;
0dca1793
AK
1534 }
1535
3f7440a6 1536 n = div_u64(n, rate);
ffb2c3c0 1537 /* n should be less than 2^32 for being written to FREQ register */
da3cec35 1538 snd_BUG_ON(n >> 32);
ffb2c3c0
RB
1539 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1540}
763f356c
TI
1541
1542/* dummy set rate lets see what happens */
98274f07 1543static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
763f356c 1544{
763f356c
TI
1545 int current_rate;
1546 int rate_bits;
1547 int not_set = 0;
6534599d 1548 int current_speed, target_speed;
763f356c
TI
1549
1550 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1551 it (e.g. during module initialization).
1552 */
1553
1554 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1555
0dca1793 1556 /* SLAVE --- */
763f356c
TI
1557 if (called_internally) {
1558
0dca1793
AK
1559 /* request from ctl or card initialization
1560 just make a warning an remember setting
1561 for future master mode switching */
1562
ef5fa1a4
TI
1563 snd_printk(KERN_WARNING "HDSPM: "
1564 "Warning: device is not running "
1565 "as a clock master.\n");
763f356c
TI
1566 not_set = 1;
1567 } else {
1568
1569 /* hw_param request while in AutoSync mode */
1570 int external_freq =
1571 hdspm_external_sample_rate(hdspm);
1572
ef5fa1a4
TI
1573 if (hdspm_autosync_ref(hdspm) ==
1574 HDSPM_AUTOSYNC_FROM_NONE) {
763f356c 1575
ef5fa1a4
TI
1576 snd_printk(KERN_WARNING "HDSPM: "
1577 "Detected no Externel Sync \n");
763f356c
TI
1578 not_set = 1;
1579
1580 } else if (rate != external_freq) {
1581
ef5fa1a4
TI
1582 snd_printk(KERN_WARNING "HDSPM: "
1583 "Warning: No AutoSync source for "
1584 "requested rate\n");
763f356c
TI
1585 not_set = 1;
1586 }
1587 }
1588 }
1589
1590 current_rate = hdspm->system_sample_rate;
1591
1592 /* Changing between Singe, Double and Quad speed is not
1593 allowed if any substreams are open. This is because such a change
1594 causes a shift in the location of the DMA buffers and a reduction
1595 in the number of available buffers.
1596
1597 Note that a similar but essentially insoluble problem exists for
1598 externally-driven rate changes. All we can do is to flag rate
0dca1793 1599 changes in the read/write routines.
763f356c
TI
1600 */
1601
6534599d
RB
1602 if (current_rate <= 48000)
1603 current_speed = HDSPM_SPEED_SINGLE;
1604 else if (current_rate <= 96000)
1605 current_speed = HDSPM_SPEED_DOUBLE;
1606 else
1607 current_speed = HDSPM_SPEED_QUAD;
1608
1609 if (rate <= 48000)
1610 target_speed = HDSPM_SPEED_SINGLE;
1611 else if (rate <= 96000)
1612 target_speed = HDSPM_SPEED_DOUBLE;
1613 else
1614 target_speed = HDSPM_SPEED_QUAD;
3cee5a60 1615
763f356c
TI
1616 switch (rate) {
1617 case 32000:
763f356c
TI
1618 rate_bits = HDSPM_Frequency32KHz;
1619 break;
1620 case 44100:
763f356c
TI
1621 rate_bits = HDSPM_Frequency44_1KHz;
1622 break;
1623 case 48000:
763f356c
TI
1624 rate_bits = HDSPM_Frequency48KHz;
1625 break;
1626 case 64000:
763f356c
TI
1627 rate_bits = HDSPM_Frequency64KHz;
1628 break;
1629 case 88200:
763f356c
TI
1630 rate_bits = HDSPM_Frequency88_2KHz;
1631 break;
1632 case 96000:
763f356c
TI
1633 rate_bits = HDSPM_Frequency96KHz;
1634 break;
3cee5a60 1635 case 128000:
3cee5a60
RB
1636 rate_bits = HDSPM_Frequency128KHz;
1637 break;
1638 case 176400:
3cee5a60
RB
1639 rate_bits = HDSPM_Frequency176_4KHz;
1640 break;
1641 case 192000:
3cee5a60
RB
1642 rate_bits = HDSPM_Frequency192KHz;
1643 break;
763f356c
TI
1644 default:
1645 return -EINVAL;
1646 }
1647
6534599d 1648 if (current_speed != target_speed
763f356c
TI
1649 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1650 snd_printk
ef5fa1a4 1651 (KERN_ERR "HDSPM: "
6534599d 1652 "cannot change from %s speed to %s speed mode "
ef5fa1a4 1653 "(capture PID = %d, playback PID = %d)\n",
6534599d
RB
1654 hdspm_speed_names[current_speed],
1655 hdspm_speed_names[target_speed],
763f356c
TI
1656 hdspm->capture_pid, hdspm->playback_pid);
1657 return -EBUSY;
1658 }
1659
1660 hdspm->control_register &= ~HDSPM_FrequencyMask;
1661 hdspm->control_register |= rate_bits;
1662 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1663
ffb2c3c0
RB
1664 /* For AES32, need to set DDS value in FREQ register
1665 For MADI, also apparently */
1666 hdspm_set_dds_value(hdspm, rate);
0dca1793
AK
1667
1668 if (AES32 == hdspm->io_type && rate != current_rate)
ffb2c3c0 1669 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
763f356c
TI
1670
1671 hdspm->system_sample_rate = rate;
1672
0dca1793
AK
1673 if (rate <= 48000) {
1674 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1675 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1676 hdspm->max_channels_in = hdspm->ss_in_channels;
1677 hdspm->max_channels_out = hdspm->ss_out_channels;
1678 hdspm->port_names_in = hdspm->port_names_in_ss;
1679 hdspm->port_names_out = hdspm->port_names_out_ss;
1680 } else if (rate <= 96000) {
1681 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1682 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1683 hdspm->max_channels_in = hdspm->ds_in_channels;
1684 hdspm->max_channels_out = hdspm->ds_out_channels;
1685 hdspm->port_names_in = hdspm->port_names_in_ds;
1686 hdspm->port_names_out = hdspm->port_names_out_ds;
1687 } else {
1688 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1689 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1690 hdspm->max_channels_in = hdspm->qs_in_channels;
1691 hdspm->max_channels_out = hdspm->qs_out_channels;
1692 hdspm->port_names_in = hdspm->port_names_in_qs;
1693 hdspm->port_names_out = hdspm->port_names_out_qs;
1694 }
1695
763f356c
TI
1696 if (not_set != 0)
1697 return -1;
1698
1699 return 0;
1700}
1701
1702/* mainly for init to 0 on load */
98274f07 1703static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
763f356c
TI
1704{
1705 int i, j;
ef5fa1a4
TI
1706 unsigned int gain;
1707
1708 if (sgain > UNITY_GAIN)
1709 gain = UNITY_GAIN;
1710 else if (sgain < 0)
1711 gain = 0;
1712 else
1713 gain = sgain;
763f356c
TI
1714
1715 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1716 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1717 hdspm_write_in_gain(hdspm, i, j, gain);
1718 hdspm_write_pb_gain(hdspm, i, j, gain);
1719 }
1720}
1721
1722/*----------------------------------------------------------------------------
1723 MIDI
1724 ----------------------------------------------------------------------------*/
1725
ef5fa1a4
TI
1726static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1727 int id)
763f356c
TI
1728{
1729 /* the hardware already does the relevant bit-mask with 0xff */
0dca1793 1730 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
763f356c
TI
1731}
1732
ef5fa1a4
TI
1733static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1734 int val)
763f356c
TI
1735{
1736 /* the hardware already does the relevant bit-mask with 0xff */
0dca1793 1737 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
763f356c
TI
1738}
1739
98274f07 1740static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
763f356c 1741{
0dca1793 1742 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
763f356c
TI
1743}
1744
98274f07 1745static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
763f356c
TI
1746{
1747 int fifo_bytes_used;
1748
0dca1793 1749 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
763f356c
TI
1750
1751 if (fifo_bytes_used < 128)
1752 return 128 - fifo_bytes_used;
1753 else
1754 return 0;
1755}
1756
62cef821 1757static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
763f356c
TI
1758{
1759 while (snd_hdspm_midi_input_available (hdspm, id))
1760 snd_hdspm_midi_read_byte (hdspm, id);
1761}
1762
98274f07 1763static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
763f356c
TI
1764{
1765 unsigned long flags;
1766 int n_pending;
1767 int to_write;
1768 int i;
1769 unsigned char buf[128];
1770
1771 /* Output is not interrupt driven */
0dca1793 1772
763f356c 1773 spin_lock_irqsave (&hmidi->lock, flags);
ef5fa1a4
TI
1774 if (hmidi->output &&
1775 !snd_rawmidi_transmit_empty (hmidi->output)) {
1776 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1777 hmidi->id);
1778 if (n_pending > 0) {
1779 if (n_pending > (int)sizeof (buf))
1780 n_pending = sizeof (buf);
0dca1793 1781
ef5fa1a4
TI
1782 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1783 n_pending);
1784 if (to_write > 0) {
0dca1793 1785 for (i = 0; i < to_write; ++i)
ef5fa1a4
TI
1786 snd_hdspm_midi_write_byte (hmidi->hdspm,
1787 hmidi->id,
1788 buf[i]);
763f356c
TI
1789 }
1790 }
1791 }
1792 spin_unlock_irqrestore (&hmidi->lock, flags);
1793 return 0;
1794}
1795
98274f07 1796static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
763f356c 1797{
ef5fa1a4
TI
1798 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1799 * input FIFO size
1800 */
763f356c
TI
1801 unsigned long flags;
1802 int n_pending;
1803 int i;
1804
1805 spin_lock_irqsave (&hmidi->lock, flags);
ef5fa1a4
TI
1806 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1807 if (n_pending > 0) {
763f356c 1808 if (hmidi->input) {
ef5fa1a4 1809 if (n_pending > (int)sizeof (buf))
763f356c 1810 n_pending = sizeof (buf);
ef5fa1a4
TI
1811 for (i = 0; i < n_pending; ++i)
1812 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1813 hmidi->id);
1814 if (n_pending)
1815 snd_rawmidi_receive (hmidi->input, buf,
1816 n_pending);
763f356c
TI
1817 } else {
1818 /* flush the MIDI input FIFO */
ef5fa1a4
TI
1819 while (n_pending--)
1820 snd_hdspm_midi_read_byte (hmidi->hdspm,
1821 hmidi->id);
763f356c
TI
1822 }
1823 }
1824 hmidi->pending = 0;
c0da0014 1825 spin_unlock_irqrestore(&hmidi->lock, flags);
0dca1793 1826
c0da0014 1827 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
0dca1793 1828 hmidi->hdspm->control_register |= hmidi->ie;
ef5fa1a4
TI
1829 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1830 hmidi->hdspm->control_register);
c0da0014 1831 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
0dca1793 1832
763f356c
TI
1833 return snd_hdspm_midi_output_write (hmidi);
1834}
1835
ef5fa1a4
TI
1836static void
1837snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
763f356c 1838{
98274f07
TI
1839 struct hdspm *hdspm;
1840 struct hdspm_midi *hmidi;
763f356c 1841 unsigned long flags;
763f356c 1842
ef5fa1a4 1843 hmidi = substream->rmidi->private_data;
763f356c 1844 hdspm = hmidi->hdspm;
0dca1793 1845
763f356c
TI
1846 spin_lock_irqsave (&hdspm->lock, flags);
1847 if (up) {
0dca1793 1848 if (!(hdspm->control_register & hmidi->ie)) {
763f356c 1849 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
0dca1793 1850 hdspm->control_register |= hmidi->ie;
763f356c
TI
1851 }
1852 } else {
0dca1793 1853 hdspm->control_register &= ~hmidi->ie;
763f356c
TI
1854 }
1855
1856 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1857 spin_unlock_irqrestore (&hdspm->lock, flags);
1858}
1859
1860static void snd_hdspm_midi_output_timer(unsigned long data)
1861{
98274f07 1862 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
763f356c 1863 unsigned long flags;
0dca1793 1864
763f356c
TI
1865 snd_hdspm_midi_output_write(hmidi);
1866 spin_lock_irqsave (&hmidi->lock, flags);
1867
1868 /* this does not bump hmidi->istimer, because the
1869 kernel automatically removed the timer when it
1870 expired, and we are now adding it back, thus
0dca1793 1871 leaving istimer wherever it was set before.
763f356c
TI
1872 */
1873
1874 if (hmidi->istimer) {
1875 hmidi->timer.expires = 1 + jiffies;
1876 add_timer(&hmidi->timer);
1877 }
1878
1879 spin_unlock_irqrestore (&hmidi->lock, flags);
1880}
1881
ef5fa1a4
TI
1882static void
1883snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
763f356c 1884{
98274f07 1885 struct hdspm_midi *hmidi;
763f356c
TI
1886 unsigned long flags;
1887
ef5fa1a4 1888 hmidi = substream->rmidi->private_data;
763f356c
TI
1889 spin_lock_irqsave (&hmidi->lock, flags);
1890 if (up) {
1891 if (!hmidi->istimer) {
1892 init_timer(&hmidi->timer);
1893 hmidi->timer.function = snd_hdspm_midi_output_timer;
1894 hmidi->timer.data = (unsigned long) hmidi;
1895 hmidi->timer.expires = 1 + jiffies;
1896 add_timer(&hmidi->timer);
1897 hmidi->istimer++;
1898 }
1899 } else {
ef5fa1a4 1900 if (hmidi->istimer && --hmidi->istimer <= 0)
763f356c 1901 del_timer (&hmidi->timer);
763f356c
TI
1902 }
1903 spin_unlock_irqrestore (&hmidi->lock, flags);
1904 if (up)
1905 snd_hdspm_midi_output_write(hmidi);
1906}
1907
98274f07 1908static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
763f356c 1909{
98274f07 1910 struct hdspm_midi *hmidi;
763f356c 1911
ef5fa1a4 1912 hmidi = substream->rmidi->private_data;
763f356c
TI
1913 spin_lock_irq (&hmidi->lock);
1914 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1915 hmidi->input = substream;
1916 spin_unlock_irq (&hmidi->lock);
1917
1918 return 0;
1919}
1920
98274f07 1921static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
763f356c 1922{
98274f07 1923 struct hdspm_midi *hmidi;
763f356c 1924
ef5fa1a4 1925 hmidi = substream->rmidi->private_data;
763f356c
TI
1926 spin_lock_irq (&hmidi->lock);
1927 hmidi->output = substream;
1928 spin_unlock_irq (&hmidi->lock);
1929
1930 return 0;
1931}
1932
98274f07 1933static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
763f356c 1934{
98274f07 1935 struct hdspm_midi *hmidi;
763f356c
TI
1936
1937 snd_hdspm_midi_input_trigger (substream, 0);
1938
ef5fa1a4 1939 hmidi = substream->rmidi->private_data;
763f356c
TI
1940 spin_lock_irq (&hmidi->lock);
1941 hmidi->input = NULL;
1942 spin_unlock_irq (&hmidi->lock);
1943
1944 return 0;
1945}
1946
98274f07 1947static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
763f356c 1948{
98274f07 1949 struct hdspm_midi *hmidi;
763f356c
TI
1950
1951 snd_hdspm_midi_output_trigger (substream, 0);
1952
ef5fa1a4 1953 hmidi = substream->rmidi->private_data;
763f356c
TI
1954 spin_lock_irq (&hmidi->lock);
1955 hmidi->output = NULL;
1956 spin_unlock_irq (&hmidi->lock);
1957
1958 return 0;
1959}
1960
98274f07 1961static struct snd_rawmidi_ops snd_hdspm_midi_output =
763f356c
TI
1962{
1963 .open = snd_hdspm_midi_output_open,
1964 .close = snd_hdspm_midi_output_close,
1965 .trigger = snd_hdspm_midi_output_trigger,
1966};
1967
98274f07 1968static struct snd_rawmidi_ops snd_hdspm_midi_input =
763f356c
TI
1969{
1970 .open = snd_hdspm_midi_input_open,
1971 .close = snd_hdspm_midi_input_close,
1972 .trigger = snd_hdspm_midi_input_trigger,
1973};
1974
e23e7a14
BP
1975static int snd_hdspm_create_midi(struct snd_card *card,
1976 struct hdspm *hdspm, int id)
763f356c
TI
1977{
1978 int err;
1979 char buf[32];
1980
1981 hdspm->midi[id].id = id;
763f356c 1982 hdspm->midi[id].hdspm = hdspm;
763f356c
TI
1983 spin_lock_init (&hdspm->midi[id].lock);
1984
0dca1793
AK
1985 if (0 == id) {
1986 if (MADIface == hdspm->io_type) {
1987 /* MIDI-over-MADI on HDSPe MADIface */
1988 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1989 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1990 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1991 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1992 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1993 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1994 } else {
1995 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1996 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1997 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1998 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1999 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
2000 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
2001 }
2002 } else if (1 == id) {
2003 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
2004 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
2005 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
2006 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
2007 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
2008 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
2009 } else if ((2 == id) && (MADI == hdspm->io_type)) {
2010 /* MIDI-over-MADI on HDSPe MADI */
2011 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
2012 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
2013 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
2014 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
2015 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
2016 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
2017 } else if (2 == id) {
2018 /* TCO MTC, read only */
2019 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
2020 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
2021 hdspm->midi[2].dataOut = -1;
2022 hdspm->midi[2].statusOut = -1;
2023 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
2024 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
2025 } else if (3 == id) {
2026 /* TCO MTC on HDSPe MADI */
2027 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
2028 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
2029 hdspm->midi[3].dataOut = -1;
2030 hdspm->midi[3].statusOut = -1;
2031 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
2032 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
2033 }
2034
2035 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
2036 (MADIface == hdspm->io_type)))) {
2037 if ((id == 0) && (MADIface == hdspm->io_type)) {
2038 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2039 } else if ((id == 2) && (MADI == hdspm->io_type)) {
2040 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2041 } else {
2042 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
2043 }
2044 err = snd_rawmidi_new(card, buf, id, 1, 1,
2045 &hdspm->midi[id].rmidi);
2046 if (err < 0)
2047 return err;
763f356c 2048
0dca1793
AK
2049 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
2050 card->id, id+1);
2051 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
2052
2053 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2054 SNDRV_RAWMIDI_STREAM_OUTPUT,
2055 &snd_hdspm_midi_output);
2056 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2057 SNDRV_RAWMIDI_STREAM_INPUT,
2058 &snd_hdspm_midi_input);
2059
2060 hdspm->midi[id].rmidi->info_flags |=
2061 SNDRV_RAWMIDI_INFO_OUTPUT |
2062 SNDRV_RAWMIDI_INFO_INPUT |
2063 SNDRV_RAWMIDI_INFO_DUPLEX;
2064 } else {
2065 /* TCO MTC, read only */
2066 sprintf(buf, "%s MTC %d", card->shortname, id+1);
2067 err = snd_rawmidi_new(card, buf, id, 1, 1,
2068 &hdspm->midi[id].rmidi);
2069 if (err < 0)
2070 return err;
2071
2072 sprintf(hdspm->midi[id].rmidi->name,
2073 "%s MTC %d", card->id, id+1);
2074 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
763f356c 2075
0dca1793
AK
2076 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2077 SNDRV_RAWMIDI_STREAM_INPUT,
2078 &snd_hdspm_midi_input);
763f356c 2079
0dca1793
AK
2080 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
2081 }
763f356c
TI
2082
2083 return 0;
2084}
2085
2086
2087static void hdspm_midi_tasklet(unsigned long arg)
2088{
98274f07 2089 struct hdspm *hdspm = (struct hdspm *)arg;
0dca1793
AK
2090 int i = 0;
2091
2092 while (i < hdspm->midiPorts) {
2093 if (hdspm->midi[i].pending)
2094 snd_hdspm_midi_input_read(&hdspm->midi[i]);
2095
2096 i++;
2097 }
2098}
763f356c
TI
2099
2100
2101/*-----------------------------------------------------------------------------
2102 Status Interface
2103 ----------------------------------------------------------------------------*/
2104
2105/* get the system sample rate which is set */
2106
0dca1793 2107
3f7bf918
AK
2108static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
2109{
2110 unsigned int period, rate;
2111
2112 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
2113 rate = hdspm_calc_dds_value(hdspm, period);
2114
2115 return rate;
2116}
2117
0dca1793
AK
2118/**
2119 * Calculate the real sample rate from the
2120 * current DDS value.
2121 **/
2122static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
2123{
3f7bf918 2124 unsigned int rate;
0dca1793 2125
3f7bf918 2126 rate = hdspm_get_pll_freq(hdspm);
0dca1793 2127
a97bda7d 2128 if (rate > 207000) {
21a164df
AK
2129 /* Unreasonable high sample rate as seen on PCI MADI cards. */
2130 if (0 == hdspm_system_clock_mode(hdspm)) {
2131 /* master mode, return internal sample rate */
2132 rate = hdspm->system_sample_rate;
2133 } else {
2134 /* slave mode, return external sample rate */
2135 rate = hdspm_external_sample_rate(hdspm);
2136 }
a97bda7d
AK
2137 }
2138
0dca1793
AK
2139 return rate;
2140}
2141
2142
763f356c 2143#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
f27a64f9
AK
2144{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2145 .name = xname, \
2146 .index = xindex, \
2147 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2148 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2149 .info = snd_hdspm_info_system_sample_rate, \
2150 .put = snd_hdspm_put_system_sample_rate, \
2151 .get = snd_hdspm_get_system_sample_rate \
763f356c
TI
2152}
2153
98274f07
TI
2154static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_info *uinfo)
763f356c
TI
2156{
2157 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2158 uinfo->count = 1;
0dca1793
AK
2159 uinfo->value.integer.min = 27000;
2160 uinfo->value.integer.max = 207000;
2161 uinfo->value.integer.step = 1;
763f356c
TI
2162 return 0;
2163}
2164
0dca1793 2165
98274f07
TI
2166static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *
763f356c
TI
2168 ucontrol)
2169{
98274f07 2170 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2171
0dca1793
AK
2172 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
2173 return 0;
2174}
2175
41285a98
AK
2176static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *
2178 ucontrol)
2179{
2180 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2181
2182 hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
2183 return 0;
2184}
2185
0dca1793
AK
2186
2187/**
2188 * Returns the WordClock sample rate class for the given card.
2189 **/
2190static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
2191{
2192 int status;
2193
2194 switch (hdspm->io_type) {
2195 case RayDAT:
2196 case AIO:
2197 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2198 return (status >> 16) & 0xF;
2199 break;
a57fea8e
AK
2200 case AES32:
2201 status = hdspm_read(hdspm, HDSPM_statusRegister);
2202 return (status >> HDSPM_AES32_wcFreq_bit) & 0xF;
0dca1793
AK
2203 default:
2204 break;
2205 }
2206
2207
2208 return 0;
2209}
2210
2211
2212/**
2213 * Returns the TCO sample rate class for the given card.
2214 **/
2215static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2216{
2217 int status;
2218
2219 if (hdspm->tco) {
2220 switch (hdspm->io_type) {
2221 case RayDAT:
2222 case AIO:
2223 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2224 return (status >> 20) & 0xF;
2225 break;
051c44fe
AK
2226 case AES32:
2227 status = hdspm_read(hdspm, HDSPM_statusRegister);
2228 return (status >> 1) & 0xF;
0dca1793
AK
2229 default:
2230 break;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
2237
2238/**
2239 * Returns the SYNC_IN sample rate class for the given card.
2240 **/
2241static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2242{
2243 int status;
2244
2245 if (hdspm->tco) {
2246 switch (hdspm->io_type) {
2247 case RayDAT:
2248 case AIO:
2249 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2250 return (status >> 12) & 0xF;
2251 break;
2252 default:
2253 break;
2254 }
2255 }
2256
763f356c
TI
2257 return 0;
2258}
2259
d3c36ed8
AK
2260/**
2261 * Returns the AES sample rate class for the given card.
2262 **/
2263static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index)
2264{
2265 int timecode;
2266
2267 switch (hdspm->io_type) {
2268 case AES32:
2269 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
2270 return (timecode >> (4*index)) & 0xF;
2271 break;
2272 default:
2273 break;
2274 }
2275 return 0;
2276}
0dca1793
AK
2277
2278/**
2279 * Returns the sample rate class for input source <idx> for
2280 * 'new style' cards like the AIO and RayDAT.
2281 **/
2282static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2283{
2284 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2285
2286 return (status >> (idx*4)) & 0xF;
2287}
2288
8cea5710
AK
2289static void snd_hdspm_set_infotext(struct snd_ctl_elem_info *uinfo,
2290 char **texts, const int count)
2291{
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2293 uinfo->count = 1;
2294 uinfo->value.enumerated.items = count;
2295 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2296 uinfo->value.enumerated.item =
2297 uinfo->value.enumerated.items - 1;
2298 strcpy(uinfo->value.enumerated.name,
2299 texts[uinfo->value.enumerated.item]);
e5b7b1fe
AK
2300}
2301
8cea5710
AK
2302#define ENUMERATED_CTL_INFO(info, texts) \
2303 snd_hdspm_set_infotext(info, texts, ARRAY_SIZE(texts))
2304
0dca1793 2305
2336142f
AK
2306/* Helper function to query the external sample rate and return the
2307 * corresponding enum to be returned to userspace.
2308 */
2309static int hdspm_external_rate_to_enum(struct hdspm *hdspm)
2310{
2311 int rate = hdspm_external_sample_rate(hdspm);
2312 int i, selected_rate = 0;
2313 for (i = 1; i < 10; i++)
2314 if (HDSPM_bit2freq(i) == rate) {
2315 selected_rate = i;
2316 break;
2317 }
2318 return selected_rate;
2319}
2320
0dca1793 2321
763f356c 2322#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
0dca1793
AK
2323{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2324 .name = xname, \
2325 .private_value = xindex, \
2326 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2327 .info = snd_hdspm_info_autosync_sample_rate, \
2328 .get = snd_hdspm_get_autosync_sample_rate \
763f356c
TI
2329}
2330
0dca1793 2331
98274f07
TI
2332static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_info *uinfo)
763f356c 2334{
e5b7b1fe 2335 ENUMERATED_CTL_INFO(uinfo, texts_freq);
763f356c
TI
2336 return 0;
2337}
2338
0dca1793 2339
98274f07
TI
2340static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *
763f356c
TI
2342 ucontrol)
2343{
98274f07 2344 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2345
0dca1793
AK
2346 switch (hdspm->io_type) {
2347 case RayDAT:
2348 switch (kcontrol->private_value) {
2349 case 0:
2350 ucontrol->value.enumerated.item[0] =
2351 hdspm_get_wc_sample_rate(hdspm);
2352 break;
2353 case 7:
2354 ucontrol->value.enumerated.item[0] =
2355 hdspm_get_tco_sample_rate(hdspm);
2356 break;
2357 case 8:
2358 ucontrol->value.enumerated.item[0] =
2359 hdspm_get_sync_in_sample_rate(hdspm);
2360 break;
2361 default:
2362 ucontrol->value.enumerated.item[0] =
2363 hdspm_get_s1_sample_rate(hdspm,
2364 kcontrol->private_value-1);
2365 }
d681deaa 2366 break;
763f356c 2367
0dca1793
AK
2368 case AIO:
2369 switch (kcontrol->private_value) {
2370 case 0: /* WC */
2371 ucontrol->value.enumerated.item[0] =
2372 hdspm_get_wc_sample_rate(hdspm);
2373 break;
2374 case 4: /* TCO */
2375 ucontrol->value.enumerated.item[0] =
2376 hdspm_get_tco_sample_rate(hdspm);
2377 break;
2378 case 5: /* SYNC_IN */
2379 ucontrol->value.enumerated.item[0] =
2380 hdspm_get_sync_in_sample_rate(hdspm);
2381 break;
2382 default:
2383 ucontrol->value.enumerated.item[0] =
2384 hdspm_get_s1_sample_rate(hdspm,
1cb7dbf4 2385 kcontrol->private_value-1);
0dca1793 2386 }
d681deaa 2387 break;
7c4a95b5
AK
2388
2389 case AES32:
2390
2391 switch (kcontrol->private_value) {
2392 case 0: /* WC */
2393 ucontrol->value.enumerated.item[0] =
2394 hdspm_get_wc_sample_rate(hdspm);
2395 break;
2396 case 9: /* TCO */
2397 ucontrol->value.enumerated.item[0] =
2398 hdspm_get_tco_sample_rate(hdspm);
2399 break;
2400 case 10: /* SYNC_IN */
2401 ucontrol->value.enumerated.item[0] =
2402 hdspm_get_sync_in_sample_rate(hdspm);
2403 break;
2d63ec38
AK
2404 case 11: /* External Rate */
2405 ucontrol->value.enumerated.item[0] =
2406 hdspm_external_rate_to_enum(hdspm);
2407 break;
7c4a95b5
AK
2408 default: /* AES1 to AES8 */
2409 ucontrol->value.enumerated.item[0] =
2d63ec38
AK
2410 hdspm_get_aes_sample_rate(hdspm,
2411 kcontrol->private_value -
2412 HDSPM_AES32_AUTOSYNC_FROM_AES1);
7c4a95b5 2413 break;
7c4a95b5 2414 }
d681deaa 2415 break;
b8812c55
AK
2416
2417 case MADI:
2418 case MADIface:
2336142f
AK
2419 ucontrol->value.enumerated.item[0] =
2420 hdspm_external_rate_to_enum(hdspm);
b8812c55 2421 break;
763f356c 2422 default:
0dca1793 2423 break;
763f356c 2424 }
763f356c 2425
0dca1793 2426 return 0;
763f356c
TI
2427}
2428
2429
0dca1793
AK
2430#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
2431{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2432 .name = xname, \
2433 .index = xindex, \
2434 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2435 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2436 .info = snd_hdspm_info_system_clock_mode, \
2437 .get = snd_hdspm_get_system_clock_mode, \
2438 .put = snd_hdspm_put_system_clock_mode, \
2439}
2440
2441
2442/**
2443 * Returns the system clock mode for the given card.
2444 * @returns 0 - master, 1 - slave
2445 **/
2446static int hdspm_system_clock_mode(struct hdspm *hdspm)
2447{
2448 switch (hdspm->io_type) {
2449 case AIO:
2450 case RayDAT:
2451 if (hdspm->settings_register & HDSPM_c0Master)
2452 return 0;
2453 break;
763f356c 2454
0dca1793
AK
2455 default:
2456 if (hdspm->control_register & HDSPM_ClockModeMaster)
2457 return 0;
2458 }
763f356c 2459
763f356c
TI
2460 return 1;
2461}
2462
0dca1793
AK
2463
2464/**
2465 * Sets the system clock mode.
2466 * @param mode 0 - master, 1 - slave
2467 **/
2468static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2469{
34be7ebb
AK
2470 hdspm_set_toggle_setting(hdspm,
2471 (hdspm_is_raydat_or_aio(hdspm)) ?
2472 HDSPM_c0Master : HDSPM_ClockModeMaster,
2473 (0 == mode));
0dca1793
AK
2474}
2475
2476
2477static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
98274f07 2478 struct snd_ctl_elem_info *uinfo)
763f356c 2479{
0dca1793 2480 static char *texts[] = { "Master", "AutoSync" };
e5b7b1fe 2481 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
2482 return 0;
2483}
2484
98274f07
TI
2485static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_value *ucontrol)
763f356c 2487{
98274f07 2488 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2489
0dca1793 2490 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
763f356c
TI
2491 return 0;
2492}
2493
0dca1793
AK
2494static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_value *ucontrol)
2496{
2497 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2498 int val;
2499
2500 if (!snd_hdspm_use_is_exclusive(hdspm))
2501 return -EBUSY;
2502
2503 val = ucontrol->value.enumerated.item[0];
2504 if (val < 0)
2505 val = 0;
2506 else if (val > 1)
2507 val = 1;
2508
2509 hdspm_set_system_clock_mode(hdspm, val);
2510
2511 return 0;
2512}
2513
2514
2515#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2516{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2517 .name = xname, \
2518 .index = xindex, \
2519 .info = snd_hdspm_info_clock_source, \
2520 .get = snd_hdspm_get_clock_source, \
2521 .put = snd_hdspm_put_clock_source \
763f356c
TI
2522}
2523
0dca1793 2524
98274f07 2525static int hdspm_clock_source(struct hdspm * hdspm)
763f356c 2526{
0dca1793
AK
2527 switch (hdspm->system_sample_rate) {
2528 case 32000: return 0;
2529 case 44100: return 1;
2530 case 48000: return 2;
2531 case 64000: return 3;
2532 case 88200: return 4;
2533 case 96000: return 5;
2534 case 128000: return 6;
2535 case 176400: return 7;
2536 case 192000: return 8;
763f356c 2537 }
0dca1793
AK
2538
2539 return -1;
763f356c
TI
2540}
2541
98274f07 2542static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
763f356c
TI
2543{
2544 int rate;
2545 switch (mode) {
0dca1793
AK
2546 case 0:
2547 rate = 32000; break;
2548 case 1:
2549 rate = 44100; break;
2550 case 2:
2551 rate = 48000; break;
2552 case 3:
2553 rate = 64000; break;
2554 case 4:
2555 rate = 88200; break;
2556 case 5:
2557 rate = 96000; break;
2558 case 6:
2559 rate = 128000; break;
2560 case 7:
2561 rate = 176400; break;
2562 case 8:
2563 rate = 192000; break;
763f356c 2564 default:
0dca1793 2565 rate = 48000;
763f356c 2566 }
763f356c
TI
2567 hdspm_set_rate(hdspm, rate, 1);
2568 return 0;
2569}
2570
98274f07
TI
2571static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2572 struct snd_ctl_elem_info *uinfo)
763f356c 2573{
763f356c
TI
2574 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2575 uinfo->count = 1;
0dca1793 2576 uinfo->value.enumerated.items = 9;
763f356c
TI
2577
2578 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2579 uinfo->value.enumerated.item =
2580 uinfo->value.enumerated.items - 1;
2581
2582 strcpy(uinfo->value.enumerated.name,
0dca1793 2583 texts_freq[uinfo->value.enumerated.item+1]);
763f356c
TI
2584
2585 return 0;
2586}
2587
98274f07
TI
2588static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2589 struct snd_ctl_elem_value *ucontrol)
763f356c 2590{
98274f07 2591 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
2592
2593 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2594 return 0;
2595}
2596
98274f07
TI
2597static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
763f356c 2599{
98274f07 2600 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
2601 int change;
2602 int val;
2603
2604 if (!snd_hdspm_use_is_exclusive(hdspm))
2605 return -EBUSY;
2606 val = ucontrol->value.enumerated.item[0];
2607 if (val < 0)
2608 val = 0;
6534599d
RB
2609 if (val > 9)
2610 val = 9;
763f356c
TI
2611 spin_lock_irq(&hdspm->lock);
2612 if (val != hdspm_clock_source(hdspm))
2613 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2614 else
2615 change = 0;
2616 spin_unlock_irq(&hdspm->lock);
2617 return change;
2618}
2619
763f356c 2620
0dca1793 2621#define HDSPM_PREF_SYNC_REF(xname, xindex) \
f27a64f9 2622{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
0dca1793
AK
2623 .name = xname, \
2624 .index = xindex, \
2625 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2626 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2627 .info = snd_hdspm_info_pref_sync_ref, \
2628 .get = snd_hdspm_get_pref_sync_ref, \
2629 .put = snd_hdspm_put_pref_sync_ref \
2630}
2631
2632
2633/**
2634 * Returns the current preferred sync reference setting.
2635 * The semantics of the return value are depending on the
2636 * card, please see the comments for clarification.
2637 **/
98274f07 2638static int hdspm_pref_sync_ref(struct hdspm * hdspm)
763f356c 2639{
0dca1793
AK
2640 switch (hdspm->io_type) {
2641 case AES32:
3cee5a60 2642 switch (hdspm->control_register & HDSPM_SyncRefMask) {
0dca1793
AK
2643 case 0: return 0; /* WC */
2644 case HDSPM_SyncRef0: return 1; /* AES 1 */
2645 case HDSPM_SyncRef1: return 2; /* AES 2 */
2646 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2647 case HDSPM_SyncRef2: return 4; /* AES 4 */
2648 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2649 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2650 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2651 return 7; /* AES 7 */
2652 case HDSPM_SyncRef3: return 8; /* AES 8 */
2653 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
3cee5a60 2654 }
0dca1793
AK
2655 break;
2656
2657 case MADI:
2658 case MADIface:
2659 if (hdspm->tco) {
2660 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2661 case 0: return 0; /* WC */
2662 case HDSPM_SyncRef0: return 1; /* MADI */
2663 case HDSPM_SyncRef1: return 2; /* TCO */
2664 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2665 return 3; /* SYNC_IN */
2666 }
2667 } else {
2668 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2669 case 0: return 0; /* WC */
2670 case HDSPM_SyncRef0: return 1; /* MADI */
2671 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2672 return 2; /* SYNC_IN */
2673 }
2674 }
2675 break;
2676
2677 case RayDAT:
2678 if (hdspm->tco) {
2679 switch ((hdspm->settings_register &
2680 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2681 case 0: return 0; /* WC */
2682 case 3: return 1; /* ADAT 1 */
2683 case 4: return 2; /* ADAT 2 */
2684 case 5: return 3; /* ADAT 3 */
2685 case 6: return 4; /* ADAT 4 */
2686 case 1: return 5; /* AES */
2687 case 2: return 6; /* SPDIF */
2688 case 9: return 7; /* TCO */
2689 case 10: return 8; /* SYNC_IN */
2690 }
2691 } else {
2692 switch ((hdspm->settings_register &
2693 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2694 case 0: return 0; /* WC */
2695 case 3: return 1; /* ADAT 1 */
2696 case 4: return 2; /* ADAT 2 */
2697 case 5: return 3; /* ADAT 3 */
2698 case 6: return 4; /* ADAT 4 */
2699 case 1: return 5; /* AES */
2700 case 2: return 6; /* SPDIF */
2701 case 10: return 7; /* SYNC_IN */
2702 }
3cee5a60 2703 }
0dca1793
AK
2704
2705 break;
2706
2707 case AIO:
2708 if (hdspm->tco) {
2709 switch ((hdspm->settings_register &
2710 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2711 case 0: return 0; /* WC */
2712 case 3: return 1; /* ADAT */
2713 case 1: return 2; /* AES */
2714 case 2: return 3; /* SPDIF */
2715 case 9: return 4; /* TCO */
2716 case 10: return 5; /* SYNC_IN */
2717 }
2718 } else {
2719 switch ((hdspm->settings_register &
2720 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2721 case 0: return 0; /* WC */
2722 case 3: return 1; /* ADAT */
2723 case 1: return 2; /* AES */
2724 case 2: return 3; /* SPDIF */
2725 case 10: return 4; /* SYNC_IN */
2726 }
2727 }
2728
2729 break;
763f356c
TI
2730 }
2731
0dca1793 2732 return -1;
763f356c
TI
2733}
2734
0dca1793
AK
2735
2736/**
2737 * Set the preferred sync reference to <pref>. The semantics
2738 * of <pref> are depending on the card type, see the comments
2739 * for clarification.
2740 **/
98274f07 2741static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
763f356c 2742{
0dca1793 2743 int p = 0;
763f356c 2744
0dca1793
AK
2745 switch (hdspm->io_type) {
2746 case AES32:
2747 hdspm->control_register &= ~HDSPM_SyncRefMask;
3cee5a60 2748 switch (pref) {
0dca1793
AK
2749 case 0: /* WC */
2750 break;
2751 case 1: /* AES 1 */
2752 hdspm->control_register |= HDSPM_SyncRef0;
2753 break;
2754 case 2: /* AES 2 */
2755 hdspm->control_register |= HDSPM_SyncRef1;
2756 break;
2757 case 3: /* AES 3 */
2758 hdspm->control_register |=
2759 HDSPM_SyncRef1+HDSPM_SyncRef0;
2760 break;
2761 case 4: /* AES 4 */
2762 hdspm->control_register |= HDSPM_SyncRef2;
2763 break;
2764 case 5: /* AES 5 */
2765 hdspm->control_register |=
2766 HDSPM_SyncRef2+HDSPM_SyncRef0;
2767 break;
2768 case 6: /* AES 6 */
2769 hdspm->control_register |=
2770 HDSPM_SyncRef2+HDSPM_SyncRef1;
2771 break;
2772 case 7: /* AES 7 */
2773 hdspm->control_register |=
2774 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
3cee5a60 2775 break;
0dca1793
AK
2776 case 8: /* AES 8 */
2777 hdspm->control_register |= HDSPM_SyncRef3;
2778 break;
2779 case 9: /* TCO */
2780 hdspm->control_register |=
2781 HDSPM_SyncRef3+HDSPM_SyncRef0;
3cee5a60
RB
2782 break;
2783 default:
2784 return -1;
2785 }
0dca1793
AK
2786
2787 break;
2788
2789 case MADI:
2790 case MADIface:
2791 hdspm->control_register &= ~HDSPM_SyncRefMask;
2792 if (hdspm->tco) {
2793 switch (pref) {
2794 case 0: /* WC */
2795 break;
2796 case 1: /* MADI */
2797 hdspm->control_register |= HDSPM_SyncRef0;
2798 break;
2799 case 2: /* TCO */
2800 hdspm->control_register |= HDSPM_SyncRef1;
2801 break;
2802 case 3: /* SYNC_IN */
2803 hdspm->control_register |=
2804 HDSPM_SyncRef0+HDSPM_SyncRef1;
2805 break;
2806 default:
2807 return -1;
2808 }
2809 } else {
2810 switch (pref) {
2811 case 0: /* WC */
2812 break;
2813 case 1: /* MADI */
2814 hdspm->control_register |= HDSPM_SyncRef0;
2815 break;
2816 case 2: /* SYNC_IN */
2817 hdspm->control_register |=
2818 HDSPM_SyncRef0+HDSPM_SyncRef1;
2819 break;
2820 default:
2821 return -1;
2822 }
2823 }
2824
2825 break;
2826
2827 case RayDAT:
2828 if (hdspm->tco) {
2829 switch (pref) {
2830 case 0: p = 0; break; /* WC */
2831 case 1: p = 3; break; /* ADAT 1 */
2832 case 2: p = 4; break; /* ADAT 2 */
2833 case 3: p = 5; break; /* ADAT 3 */
2834 case 4: p = 6; break; /* ADAT 4 */
2835 case 5: p = 1; break; /* AES */
2836 case 6: p = 2; break; /* SPDIF */
2837 case 7: p = 9; break; /* TCO */
2838 case 8: p = 10; break; /* SYNC_IN */
2839 default: return -1;
2840 }
2841 } else {
2842 switch (pref) {
2843 case 0: p = 0; break; /* WC */
2844 case 1: p = 3; break; /* ADAT 1 */
2845 case 2: p = 4; break; /* ADAT 2 */
2846 case 3: p = 5; break; /* ADAT 3 */
2847 case 4: p = 6; break; /* ADAT 4 */
2848 case 5: p = 1; break; /* AES */
2849 case 6: p = 2; break; /* SPDIF */
2850 case 7: p = 10; break; /* SYNC_IN */
2851 default: return -1;
2852 }
2853 }
2854 break;
2855
2856 case AIO:
2857 if (hdspm->tco) {
2858 switch (pref) {
2859 case 0: p = 0; break; /* WC */
2860 case 1: p = 3; break; /* ADAT */
2861 case 2: p = 1; break; /* AES */
2862 case 3: p = 2; break; /* SPDIF */
2863 case 4: p = 9; break; /* TCO */
2864 case 5: p = 10; break; /* SYNC_IN */
2865 default: return -1;
2866 }
2867 } else {
2868 switch (pref) {
2869 case 0: p = 0; break; /* WC */
2870 case 1: p = 3; break; /* ADAT */
2871 case 2: p = 1; break; /* AES */
2872 case 3: p = 2; break; /* SPDIF */
2873 case 4: p = 10; break; /* SYNC_IN */
2874 default: return -1;
2875 }
2876 }
2877 break;
763f356c 2878 }
0dca1793
AK
2879
2880 switch (hdspm->io_type) {
2881 case RayDAT:
2882 case AIO:
2883 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2884 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2885 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2886 break;
2887
2888 case MADI:
2889 case MADIface:
2890 case AES32:
2891 hdspm_write(hdspm, HDSPM_controlRegister,
2892 hdspm->control_register);
2893 }
2894
763f356c
TI
2895 return 0;
2896}
2897
0dca1793 2898
98274f07
TI
2899static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_info *uinfo)
763f356c 2901{
3cee5a60 2902 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2903
0dca1793
AK
2904 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2905 uinfo->count = 1;
2906 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
3cee5a60 2907
0dca1793
AK
2908 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2909 uinfo->value.enumerated.item =
2910 uinfo->value.enumerated.items - 1;
3cee5a60 2911
0dca1793
AK
2912 strcpy(uinfo->value.enumerated.name,
2913 hdspm->texts_autosync[uinfo->value.enumerated.item]);
3cee5a60 2914
763f356c
TI
2915 return 0;
2916}
2917
98274f07
TI
2918static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
763f356c 2920{
98274f07 2921 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
0dca1793 2922 int psf = hdspm_pref_sync_ref(hdspm);
763f356c 2923
0dca1793
AK
2924 if (psf >= 0) {
2925 ucontrol->value.enumerated.item[0] = psf;
2926 return 0;
2927 }
2928
2929 return -1;
763f356c
TI
2930}
2931
98274f07
TI
2932static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
763f356c 2934{
98274f07 2935 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
0dca1793 2936 int val, change = 0;
763f356c
TI
2937
2938 if (!snd_hdspm_use_is_exclusive(hdspm))
2939 return -EBUSY;
2940
0dca1793
AK
2941 val = ucontrol->value.enumerated.item[0];
2942
2943 if (val < 0)
2944 val = 0;
2945 else if (val >= hdspm->texts_autosync_items)
2946 val = hdspm->texts_autosync_items-1;
763f356c
TI
2947
2948 spin_lock_irq(&hdspm->lock);
0dca1793
AK
2949 if (val != hdspm_pref_sync_ref(hdspm))
2950 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2951
763f356c
TI
2952 spin_unlock_irq(&hdspm->lock);
2953 return change;
2954}
2955
0dca1793 2956
763f356c 2957#define HDSPM_AUTOSYNC_REF(xname, xindex) \
f27a64f9
AK
2958{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2959 .name = xname, \
2960 .index = xindex, \
2961 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2962 .info = snd_hdspm_info_autosync_ref, \
2963 .get = snd_hdspm_get_autosync_ref, \
763f356c
TI
2964}
2965
0dca1793 2966static int hdspm_autosync_ref(struct hdspm *hdspm)
763f356c 2967{
2d60fc7f 2968 /* This looks at the autosync selected sync reference */
0dca1793 2969 if (AES32 == hdspm->io_type) {
2d60fc7f 2970
3cee5a60 2971 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
2d60fc7f
AK
2972 unsigned int syncref = (status >> HDSPM_AES32_syncref_bit) & 0xF;
2973 if ((syncref >= HDSPM_AES32_AUTOSYNC_FROM_WORD) &&
2974 (syncref <= HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN)) {
3cee5a60 2975 return syncref;
2d60fc7f 2976 }
3cee5a60 2977 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
2d60fc7f 2978
0dca1793 2979 } else if (MADI == hdspm->io_type) {
3cee5a60 2980
2d60fc7f 2981 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3cee5a60
RB
2982 switch (status2 & HDSPM_SelSyncRefMask) {
2983 case HDSPM_SelSyncRef_WORD:
2984 return HDSPM_AUTOSYNC_FROM_WORD;
2985 case HDSPM_SelSyncRef_MADI:
2986 return HDSPM_AUTOSYNC_FROM_MADI;
0dca1793
AK
2987 case HDSPM_SelSyncRef_TCO:
2988 return HDSPM_AUTOSYNC_FROM_TCO;
2989 case HDSPM_SelSyncRef_SyncIn:
2990 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
3cee5a60
RB
2991 case HDSPM_SelSyncRef_NVALID:
2992 return HDSPM_AUTOSYNC_FROM_NONE;
2993 default:
e71b95ad 2994 return HDSPM_AUTOSYNC_FROM_NONE;
3cee5a60 2995 }
763f356c 2996
763f356c 2997 }
0dca1793 2998 return 0;
763f356c
TI
2999}
3000
0dca1793 3001
98274f07
TI
3002static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
3003 struct snd_ctl_elem_info *uinfo)
763f356c 3004{
3cee5a60 3005 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3006
0dca1793 3007 if (AES32 == hdspm->io_type) {
3cee5a60 3008 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
db2d1a91 3009 "AES4", "AES5", "AES6", "AES7", "AES8", "TCO", "Sync In", "None"};
3cee5a60
RB
3010
3011 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3012 uinfo->count = 1;
db2d1a91 3013 uinfo->value.enumerated.items = ARRAY_SIZE(texts);
ef5fa1a4
TI
3014 if (uinfo->value.enumerated.item >=
3015 uinfo->value.enumerated.items)
3cee5a60
RB
3016 uinfo->value.enumerated.item =
3017 uinfo->value.enumerated.items - 1;
3018 strcpy(uinfo->value.enumerated.name,
3019 texts[uinfo->value.enumerated.item]);
0dca1793
AK
3020 } else if (MADI == hdspm->io_type) {
3021 static char *texts[] = {"Word Clock", "MADI", "TCO",
3022 "Sync In", "None" };
3cee5a60
RB
3023
3024 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3025 uinfo->count = 1;
0dca1793 3026 uinfo->value.enumerated.items = 5;
ef5fa1a4 3027 if (uinfo->value.enumerated.item >=
0dca1793 3028 uinfo->value.enumerated.items)
3cee5a60
RB
3029 uinfo->value.enumerated.item =
3030 uinfo->value.enumerated.items - 1;
3031 strcpy(uinfo->value.enumerated.name,
3032 texts[uinfo->value.enumerated.item]);
3033 }
763f356c
TI
3034 return 0;
3035}
3036
98274f07
TI
3037static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
3038 struct snd_ctl_elem_value *ucontrol)
763f356c 3039{
98274f07 3040 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3041
6534599d 3042 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
763f356c
TI
3043 return 0;
3044}
3045
f99c7881
AK
3046
3047
3048#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
3049{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3050 .name = xname, \
3051 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3052 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3053 .info = snd_hdspm_info_tco_video_input_format, \
3054 .get = snd_hdspm_get_tco_video_input_format, \
3055}
3056
3057static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_info *uinfo)
3059{
3060 static char *texts[] = {"No video", "NTSC", "PAL"};
3061 ENUMERATED_CTL_INFO(uinfo, texts);
3062 return 0;
3063}
3064
3065static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
3067{
3068 u32 status;
3069 int ret = 0;
3070
3071 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3072 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3073 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3074 HDSPM_TCO1_Video_Input_Format_PAL)) {
3075 case HDSPM_TCO1_Video_Input_Format_NTSC:
3076 /* ntsc */
3077 ret = 1;
3078 break;
3079 case HDSPM_TCO1_Video_Input_Format_PAL:
3080 /* pal */
3081 ret = 2;
3082 break;
3083 default:
3084 /* no video */
3085 ret = 0;
3086 break;
3087 }
3088 ucontrol->value.enumerated.item[0] = ret;
3089 return 0;
3090}
3091
3092
3093
3094#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3095{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3096 .name = xname, \
3097 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3098 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3099 .info = snd_hdspm_info_tco_ltc_frames, \
3100 .get = snd_hdspm_get_tco_ltc_frames, \
3101}
3102
3103static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_info *uinfo)
3105{
3106 static char *texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3107 "30 fps"};
3108 ENUMERATED_CTL_INFO(uinfo, texts);
3109 return 0;
3110}
3111
3112static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3113{
3114 u32 status;
3115 int ret = 0;
3116
3117 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3118 if (status & HDSPM_TCO1_LTC_Input_valid) {
3119 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3120 HDSPM_TCO1_LTC_Format_MSB)) {
3121 case 0:
3122 /* 24 fps */
3123 ret = 1;
3124 break;
3125 case HDSPM_TCO1_LTC_Format_LSB:
3126 /* 25 fps */
3127 ret = 2;
3128 break;
3129 case HDSPM_TCO1_LTC_Format_MSB:
3130 /* 25 fps */
3131 ret = 3;
3132 break;
3133 default:
3134 /* 30 fps */
3135 ret = 4;
3136 break;
3137 }
3138 }
3139
3140 return ret;
3141}
3142
3143static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3144 struct snd_ctl_elem_value *ucontrol)
3145{
3146 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3147
3148 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3149 return 0;
3150}
3151
bf0ff87b
AK
3152#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3153{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3154 .name = xname, \
3155 .private_value = xindex, \
3156 .info = snd_hdspm_info_toggle_setting, \
3157 .get = snd_hdspm_get_toggle_setting, \
3158 .put = snd_hdspm_put_toggle_setting \
3159}
3160
3161static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3162{
ce13f3f3
AK
3163 u32 reg;
3164
3165 if (hdspm_is_raydat_or_aio(hdspm))
3166 reg = hdspm->settings_register;
3167 else
3168 reg = hdspm->control_register;
3169
3170 return (reg & regmask) ? 1 : 0;
bf0ff87b
AK
3171}
3172
3173static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3174{
ce13f3f3
AK
3175 u32 *reg;
3176 u32 target_reg;
3177
3178 if (hdspm_is_raydat_or_aio(hdspm)) {
3179 reg = &(hdspm->settings_register);
3180 target_reg = HDSPM_WR_SETTINGS;
3181 } else {
3182 reg = &(hdspm->control_register);
3183 target_reg = HDSPM_controlRegister;
3184 }
3185
bf0ff87b 3186 if (out)
ce13f3f3 3187 *reg |= regmask;
bf0ff87b 3188 else
ce13f3f3
AK
3189 *reg &= ~regmask;
3190
3191 hdspm_write(hdspm, target_reg, *reg);
bf0ff87b
AK
3192
3193 return 0;
3194}
3195
3196#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3197
3198static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_value *ucontrol)
3200{
3201 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3202 u32 regmask = kcontrol->private_value;
3203
3204 spin_lock_irq(&hdspm->lock);
3205 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3206 spin_unlock_irq(&hdspm->lock);
3207 return 0;
3208}
3209
3210static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3211 struct snd_ctl_elem_value *ucontrol)
3212{
3213 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3214 u32 regmask = kcontrol->private_value;
3215 int change;
3216 unsigned int val;
3217
3218 if (!snd_hdspm_use_is_exclusive(hdspm))
3219 return -EBUSY;
3220 val = ucontrol->value.integer.value[0] & 1;
3221 spin_lock_irq(&hdspm->lock);
3222 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3223 hdspm_set_toggle_setting(hdspm, regmask, val);
3224 spin_unlock_irq(&hdspm->lock);
3225 return change;
3226}
3227
3cee5a60 3228#define HDSPM_INPUT_SELECT(xname, xindex) \
f27a64f9
AK
3229{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3230 .name = xname, \
3231 .index = xindex, \
3232 .info = snd_hdspm_info_input_select, \
3233 .get = snd_hdspm_get_input_select, \
3234 .put = snd_hdspm_put_input_select \
3cee5a60
RB
3235}
3236
3237static int hdspm_input_select(struct hdspm * hdspm)
3238{
3239 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3240}
3241
3242static int hdspm_set_input_select(struct hdspm * hdspm, int out)
3243{
3244 if (out)
3245 hdspm->control_register |= HDSPM_InputSelect0;
3246 else
3247 hdspm->control_register &= ~HDSPM_InputSelect0;
3248 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3249
3250 return 0;
3251}
3252
3253static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3254 struct snd_ctl_elem_info *uinfo)
3255{
3256 static char *texts[] = { "optical", "coaxial" };
e5b7b1fe 3257 ENUMERATED_CTL_INFO(uinfo, texts);
3cee5a60
RB
3258 return 0;
3259}
3260
3261static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3262 struct snd_ctl_elem_value *ucontrol)
3263{
3264 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3265
3266 spin_lock_irq(&hdspm->lock);
3267 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3268 spin_unlock_irq(&hdspm->lock);
3269 return 0;
3270}
3271
3272static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
3274{
3275 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3276 int change;
3277 unsigned int val;
3278
3279 if (!snd_hdspm_use_is_exclusive(hdspm))
3280 return -EBUSY;
3281 val = ucontrol->value.integer.value[0] & 1;
3282 spin_lock_irq(&hdspm->lock);
3283 change = (int) val != hdspm_input_select(hdspm);
3284 hdspm_set_input_select(hdspm, val);
3285 spin_unlock_irq(&hdspm->lock);
3286 return change;
3287}
3288
0dca1793 3289
3cee5a60 3290#define HDSPM_DS_WIRE(xname, xindex) \
f27a64f9
AK
3291{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3292 .name = xname, \
3293 .index = xindex, \
3294 .info = snd_hdspm_info_ds_wire, \
3295 .get = snd_hdspm_get_ds_wire, \
3296 .put = snd_hdspm_put_ds_wire \
3cee5a60
RB
3297}
3298
3299static int hdspm_ds_wire(struct hdspm * hdspm)
763f356c 3300{
3cee5a60 3301 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
763f356c
TI
3302}
3303
3cee5a60 3304static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
763f356c 3305{
3cee5a60
RB
3306 if (ds)
3307 hdspm->control_register |= HDSPM_DS_DoubleWire;
763f356c 3308 else
3cee5a60 3309 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
763f356c
TI
3310 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3311
3312 return 0;
3313}
3314
3cee5a60
RB
3315static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_info *uinfo)
763f356c 3317{
3cee5a60 3318 static char *texts[] = { "Single", "Double" };
e5b7b1fe 3319 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3320 return 0;
3321}
3322
3cee5a60
RB
3323static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
763f356c 3325{
98274f07 3326 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3327
3328 spin_lock_irq(&hdspm->lock);
3cee5a60 3329 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
763f356c
TI
3330 spin_unlock_irq(&hdspm->lock);
3331 return 0;
3332}
3333
3cee5a60
RB
3334static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_value *ucontrol)
763f356c 3336{
98274f07 3337 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3338 int change;
3339 unsigned int val;
3340
3341 if (!snd_hdspm_use_is_exclusive(hdspm))
3342 return -EBUSY;
3343 val = ucontrol->value.integer.value[0] & 1;
3344 spin_lock_irq(&hdspm->lock);
3cee5a60
RB
3345 change = (int) val != hdspm_ds_wire(hdspm);
3346 hdspm_set_ds_wire(hdspm, val);
763f356c
TI
3347 spin_unlock_irq(&hdspm->lock);
3348 return change;
3349}
3350
0dca1793 3351
3cee5a60 3352#define HDSPM_QS_WIRE(xname, xindex) \
f27a64f9
AK
3353{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3354 .name = xname, \
3355 .index = xindex, \
3356 .info = snd_hdspm_info_qs_wire, \
3357 .get = snd_hdspm_get_qs_wire, \
3358 .put = snd_hdspm_put_qs_wire \
763f356c
TI
3359}
3360
3cee5a60 3361static int hdspm_qs_wire(struct hdspm * hdspm)
763f356c 3362{
3cee5a60
RB
3363 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3364 return 1;
3365 if (hdspm->control_register & HDSPM_QS_QuadWire)
3366 return 2;
3367 return 0;
763f356c
TI
3368}
3369
3cee5a60 3370static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
763f356c 3371{
3cee5a60
RB
3372 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3373 switch (mode) {
3374 case 0:
3375 break;
3376 case 1:
3377 hdspm->control_register |= HDSPM_QS_DoubleWire;
3378 break;
3379 case 2:
3380 hdspm->control_register |= HDSPM_QS_QuadWire;
3381 break;
3382 }
763f356c
TI
3383 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3384
3385 return 0;
3386}
3387
3cee5a60 3388static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3389 struct snd_ctl_elem_info *uinfo)
763f356c 3390{
3cee5a60 3391 static char *texts[] = { "Single", "Double", "Quad" };
e5b7b1fe 3392 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3393 return 0;
3394}
3395
3cee5a60 3396static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3397 struct snd_ctl_elem_value *ucontrol)
763f356c 3398{
98274f07 3399 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3400
3401 spin_lock_irq(&hdspm->lock);
3cee5a60 3402 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
763f356c
TI
3403 spin_unlock_irq(&hdspm->lock);
3404 return 0;
3405}
3406
3cee5a60 3407static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3408 struct snd_ctl_elem_value *ucontrol)
763f356c 3409{
98274f07 3410 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3411 int change;
3cee5a60 3412 int val;
763f356c
TI
3413
3414 if (!snd_hdspm_use_is_exclusive(hdspm))
3415 return -EBUSY;
3cee5a60
RB
3416 val = ucontrol->value.integer.value[0];
3417 if (val < 0)
3418 val = 0;
3419 if (val > 2)
3420 val = 2;
763f356c 3421 spin_lock_irq(&hdspm->lock);
ef5fa1a4 3422 change = val != hdspm_qs_wire(hdspm);
3cee5a60 3423 hdspm_set_qs_wire(hdspm, val);
763f356c
TI
3424 spin_unlock_irq(&hdspm->lock);
3425 return change;
3426}
3427
acf14767
AK
3428#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
3429{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3430 .name = xname, \
3431 .private_value = xindex, \
3432 .info = snd_hdspm_info_tristate, \
3433 .get = snd_hdspm_get_tristate, \
3434 .put = snd_hdspm_put_tristate \
3435}
3436
3437static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
3438{
3439 u32 reg = hdspm->settings_register & (regmask * 3);
3440 return reg / regmask;
3441}
3442
3443static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
3444{
3445 hdspm->settings_register &= ~(regmask * 3);
3446 hdspm->settings_register |= (regmask * mode);
3447 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
3448
3449 return 0;
3450}
3451
3452static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
3453 struct snd_ctl_elem_info *uinfo)
3454{
3455 u32 regmask = kcontrol->private_value;
3456
3457 static char *texts_spdif[] = { "Optical", "Coaxial", "Internal" };
3458 static char *texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
3459
3460 switch (regmask) {
3461 case HDSPM_c0_Input0:
3462 ENUMERATED_CTL_INFO(uinfo, texts_spdif);
3463 break;
3464 default:
3465 ENUMERATED_CTL_INFO(uinfo, texts_levels);
3466 break;
3467 }
3468 return 0;
3469}
3470
3471static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
3472 struct snd_ctl_elem_value *ucontrol)
3473{
3474 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3475 u32 regmask = kcontrol->private_value;
3476
3477 spin_lock_irq(&hdspm->lock);
3478 ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
3479 spin_unlock_irq(&hdspm->lock);
3480 return 0;
3481}
3482
3483static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
3484 struct snd_ctl_elem_value *ucontrol)
3485{
3486 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3487 u32 regmask = kcontrol->private_value;
3488 int change;
3489 int val;
3490
3491 if (!snd_hdspm_use_is_exclusive(hdspm))
3492 return -EBUSY;
3493 val = ucontrol->value.integer.value[0];
3494 if (val < 0)
3495 val = 0;
3496 if (val > 2)
3497 val = 2;
3498
3499 spin_lock_irq(&hdspm->lock);
3500 change = val != hdspm_tristate(hdspm, regmask);
3501 hdspm_set_tristate(hdspm, val, regmask);
3502 spin_unlock_irq(&hdspm->lock);
3503 return change;
3504}
3505
700d1ef3
AK
3506#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3507{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3508 .name = xname, \
3509 .index = xindex, \
3510 .info = snd_hdspm_info_madi_speedmode, \
3511 .get = snd_hdspm_get_madi_speedmode, \
3512 .put = snd_hdspm_put_madi_speedmode \
3513}
3514
3515static int hdspm_madi_speedmode(struct hdspm *hdspm)
3516{
3517 if (hdspm->control_register & HDSPM_QuadSpeed)
3518 return 2;
3519 if (hdspm->control_register & HDSPM_DoubleSpeed)
3520 return 1;
3521 return 0;
3522}
3523
3524static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3525{
3526 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3527 switch (mode) {
3528 case 0:
3529 break;
3530 case 1:
3531 hdspm->control_register |= HDSPM_DoubleSpeed;
3532 break;
3533 case 2:
3534 hdspm->control_register |= HDSPM_QuadSpeed;
3535 break;
3536 }
3537 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3538
3539 return 0;
3540}
3541
3542static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_info *uinfo)
3544{
3545 static char *texts[] = { "Single", "Double", "Quad" };
e5b7b1fe 3546 ENUMERATED_CTL_INFO(uinfo, texts);
700d1ef3
AK
3547 return 0;
3548}
3549
3550static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3551 struct snd_ctl_elem_value *ucontrol)
3552{
3553 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3554
3555 spin_lock_irq(&hdspm->lock);
3556 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3557 spin_unlock_irq(&hdspm->lock);
3558 return 0;
3559}
3560
3561static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3562 struct snd_ctl_elem_value *ucontrol)
3563{
3564 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3565 int change;
3566 int val;
3567
3568 if (!snd_hdspm_use_is_exclusive(hdspm))
3569 return -EBUSY;
3570 val = ucontrol->value.integer.value[0];
3571 if (val < 0)
3572 val = 0;
3573 if (val > 2)
3574 val = 2;
3575 spin_lock_irq(&hdspm->lock);
3576 change = val != hdspm_madi_speedmode(hdspm);
3577 hdspm_set_madi_speedmode(hdspm, val);
3578 spin_unlock_irq(&hdspm->lock);
3579 return change;
3580}
763f356c
TI
3581
3582#define HDSPM_MIXER(xname, xindex) \
f27a64f9
AK
3583{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3584 .name = xname, \
3585 .index = xindex, \
3586 .device = 0, \
3587 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3588 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3589 .info = snd_hdspm_info_mixer, \
3590 .get = snd_hdspm_get_mixer, \
3591 .put = snd_hdspm_put_mixer \
763f356c
TI
3592}
3593
98274f07
TI
3594static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3595 struct snd_ctl_elem_info *uinfo)
763f356c
TI
3596{
3597 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3598 uinfo->count = 3;
3599 uinfo->value.integer.min = 0;
3600 uinfo->value.integer.max = 65535;
3601 uinfo->value.integer.step = 1;
3602 return 0;
3603}
3604
98274f07
TI
3605static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3606 struct snd_ctl_elem_value *ucontrol)
763f356c 3607{
98274f07 3608 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3609 int source;
3610 int destination;
3611
3612 source = ucontrol->value.integer.value[0];
3613 if (source < 0)
3614 source = 0;
3615 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3616 source = 2 * HDSPM_MAX_CHANNELS - 1;
3617
3618 destination = ucontrol->value.integer.value[1];
3619 if (destination < 0)
3620 destination = 0;
3621 else if (destination >= HDSPM_MAX_CHANNELS)
3622 destination = HDSPM_MAX_CHANNELS - 1;
3623
3624 spin_lock_irq(&hdspm->lock);
3625 if (source >= HDSPM_MAX_CHANNELS)
3626 ucontrol->value.integer.value[2] =
3627 hdspm_read_pb_gain(hdspm, destination,
3628 source - HDSPM_MAX_CHANNELS);
3629 else
3630 ucontrol->value.integer.value[2] =
3631 hdspm_read_in_gain(hdspm, destination, source);
3632
3633 spin_unlock_irq(&hdspm->lock);
3634
3635 return 0;
3636}
3637
98274f07
TI
3638static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3639 struct snd_ctl_elem_value *ucontrol)
763f356c 3640{
98274f07 3641 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3642 int change;
3643 int source;
3644 int destination;
3645 int gain;
3646
3647 if (!snd_hdspm_use_is_exclusive(hdspm))
3648 return -EBUSY;
3649
3650 source = ucontrol->value.integer.value[0];
3651 destination = ucontrol->value.integer.value[1];
3652
3653 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3654 return -1;
3655 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3656 return -1;
3657
3658 gain = ucontrol->value.integer.value[2];
3659
3660 spin_lock_irq(&hdspm->lock);
3661
3662 if (source >= HDSPM_MAX_CHANNELS)
3663 change = gain != hdspm_read_pb_gain(hdspm, destination,
3664 source -
3665 HDSPM_MAX_CHANNELS);
3666 else
ef5fa1a4
TI
3667 change = gain != hdspm_read_in_gain(hdspm, destination,
3668 source);
763f356c
TI
3669
3670 if (change) {
3671 if (source >= HDSPM_MAX_CHANNELS)
3672 hdspm_write_pb_gain(hdspm, destination,
3673 source - HDSPM_MAX_CHANNELS,
3674 gain);
3675 else
3676 hdspm_write_in_gain(hdspm, destination, source,
3677 gain);
3678 }
3679 spin_unlock_irq(&hdspm->lock);
3680
3681 return change;
3682}
3683
3684/* The simple mixer control(s) provide gain control for the
3685 basic 1:1 mappings of playback streams to output
0dca1793 3686 streams.
763f356c
TI
3687*/
3688
3689#define HDSPM_PLAYBACK_MIXER \
f27a64f9
AK
3690{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3691 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3692 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3693 .info = snd_hdspm_info_playback_mixer, \
3694 .get = snd_hdspm_get_playback_mixer, \
3695 .put = snd_hdspm_put_playback_mixer \
763f356c
TI
3696}
3697
98274f07
TI
3698static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3699 struct snd_ctl_elem_info *uinfo)
763f356c
TI
3700{
3701 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3702 uinfo->count = 1;
3703 uinfo->value.integer.min = 0;
0dca1793 3704 uinfo->value.integer.max = 64;
763f356c
TI
3705 uinfo->value.integer.step = 1;
3706 return 0;
3707}
3708
98274f07
TI
3709static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3710 struct snd_ctl_elem_value *ucontrol)
763f356c 3711{
98274f07 3712 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3713 int channel;
763f356c
TI
3714
3715 channel = ucontrol->id.index - 1;
3716
da3cec35
TI
3717 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3718 return -EINVAL;
763f356c 3719
763f356c
TI
3720 spin_lock_irq(&hdspm->lock);
3721 ucontrol->value.integer.value[0] =
0dca1793 3722 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
763f356c
TI
3723 spin_unlock_irq(&hdspm->lock);
3724
763f356c
TI
3725 return 0;
3726}
3727
98274f07
TI
3728static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3729 struct snd_ctl_elem_value *ucontrol)
763f356c 3730{
98274f07 3731 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3732 int change;
3733 int channel;
763f356c
TI
3734 int gain;
3735
3736 if (!snd_hdspm_use_is_exclusive(hdspm))
3737 return -EBUSY;
3738
3739 channel = ucontrol->id.index - 1;
3740
da3cec35
TI
3741 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3742 return -EINVAL;
763f356c 3743
0dca1793 3744 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
763f356c
TI
3745
3746 spin_lock_irq(&hdspm->lock);
3747 change =
0dca1793
AK
3748 gain != hdspm_read_pb_gain(hdspm, channel,
3749 channel);
763f356c 3750 if (change)
0dca1793 3751 hdspm_write_pb_gain(hdspm, channel, channel,
763f356c
TI
3752 gain);
3753 spin_unlock_irq(&hdspm->lock);
3754 return change;
3755}
3756
0dca1793
AK
3757#define HDSPM_SYNC_CHECK(xname, xindex) \
3758{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3759 .name = xname, \
3760 .private_value = xindex, \
3761 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3762 .info = snd_hdspm_info_sync_check, \
3763 .get = snd_hdspm_get_sync_check \
763f356c
TI
3764}
3765
34542213
AK
3766#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3767{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3768 .name = xname, \
3769 .private_value = xindex, \
3770 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3771 .info = snd_hdspm_tco_info_lock_check, \
3772 .get = snd_hdspm_get_sync_check \
3773}
3774
3775
0dca1793 3776
98274f07
TI
3777static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3778 struct snd_ctl_elem_info *uinfo)
763f356c 3779{
0dca1793 3780 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
e5b7b1fe 3781 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3782 return 0;
3783}
3784
34542213
AK
3785static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3786 struct snd_ctl_elem_info *uinfo)
3787{
3788 static char *texts[] = { "No Lock", "Lock" };
3789 ENUMERATED_CTL_INFO(uinfo, texts);
3790 return 0;
3791}
3792
0dca1793 3793static int hdspm_wc_sync_check(struct hdspm *hdspm)
763f356c 3794{
0dca1793
AK
3795 int status, status2;
3796
3797 switch (hdspm->io_type) {
3798 case AES32:
3799 status = hdspm_read(hdspm, HDSPM_statusRegister);
56bde0f3
AS
3800 if (status & HDSPM_AES32_wcLock) {
3801 if (status & HDSPM_AES32_wcSync)
3802 return 2;
3803 else
3804 return 1;
3805 }
3cee5a60 3806 return 0;
0dca1793
AK
3807 break;
3808
3809 case MADI:
3810 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3cee5a60
RB
3811 if (status2 & HDSPM_wcLock) {
3812 if (status2 & HDSPM_wcSync)
3813 return 2;
3814 else
3815 return 1;
3816 }
3817 return 0;
0dca1793 3818 break;
763f356c 3819
0dca1793
AK
3820 case RayDAT:
3821 case AIO:
3822 status = hdspm_read(hdspm, HDSPM_statusRegister);
763f356c 3823
0dca1793
AK
3824 if (status & 0x2000000)
3825 return 2;
3826 else if (status & 0x1000000)
3827 return 1;
3828 return 0;
763f356c 3829
0dca1793 3830 break;
763f356c 3831
0dca1793
AK
3832 case MADIface:
3833 break;
3834 }
3835
3836
3837 return 3;
763f356c
TI
3838}
3839
0dca1793
AK
3840
3841static int hdspm_madi_sync_check(struct hdspm *hdspm)
763f356c
TI
3842{
3843 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3844 if (status & HDSPM_madiLock) {
3845 if (status & HDSPM_madiSync)
3846 return 2;
3847 else
3848 return 1;
3849 }
3850 return 0;
3851}
3852
763f356c 3853
0dca1793
AK
3854static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3855{
3856 int status, lock, sync;
763f356c 3857
0dca1793 3858 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
763f356c 3859
0dca1793
AK
3860 lock = (status & (0x1<<idx)) ? 1 : 0;
3861 sync = (status & (0x100<<idx)) ? 1 : 0;
3cee5a60 3862
0dca1793 3863 if (lock && sync)
3cee5a60 3864 return 2;
0dca1793
AK
3865 else if (lock)
3866 return 1;
3cee5a60
RB
3867 return 0;
3868}
3869
0dca1793
AK
3870
3871static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3872{
3873 int status, lock = 0, sync = 0;
3874
3875 switch (hdspm->io_type) {
3876 case RayDAT:
3877 case AIO:
3878 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3879 lock = (status & 0x400) ? 1 : 0;
3880 sync = (status & 0x800) ? 1 : 0;
3881 break;
3882
3883 case MADI:
2e0452f5
AK
3884 status = hdspm_read(hdspm, HDSPM_statusRegister);
3885 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3886 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3887 break;
3888
0dca1793
AK
3889 case AES32:
3890 status = hdspm_read(hdspm, HDSPM_statusRegister2);
9a215f47
AK
3891 lock = (status & 0x100000) ? 1 : 0;
3892 sync = (status & 0x200000) ? 1 : 0;
0dca1793
AK
3893 break;
3894
3895 case MADIface:
3896 break;
3897 }
3898
3899 if (lock && sync)
3900 return 2;
3901 else if (lock)
3902 return 1;
3903
3904 return 0;
3905}
3906
3907static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3908{
3909 int status2, lock, sync;
3910 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3911
3912 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3913 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3914
3915 if (sync)
3916 return 2;
3917 else if (lock)
3918 return 1;
3919 return 0;
3920}
3921
34542213
AK
3922static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3923{
3924 u32 status;
3925 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3926
3927 return (status & mask) ? 1 : 0;
3928}
3929
0dca1793
AK
3930
3931static int hdspm_tco_sync_check(struct hdspm *hdspm)
3932{
3933 int status;
3934
3935 if (hdspm->tco) {
3936 switch (hdspm->io_type) {
3937 case MADI:
b0bf5504
AK
3938 status = hdspm_read(hdspm, HDSPM_statusRegister);
3939 if (status & HDSPM_tcoLockMadi) {
3940 if (status & HDSPM_tcoSync)
3941 return 2;
3942 else
3943 return 1;
3944 }
3945 return 0;
3946 break;
0dca1793
AK
3947 case AES32:
3948 status = hdspm_read(hdspm, HDSPM_statusRegister);
b0bf5504 3949 if (status & HDSPM_tcoLockAes) {
0dca1793
AK
3950 if (status & HDSPM_tcoSync)
3951 return 2;
3952 else
3953 return 1;
3954 }
3955 return 0;
3956
3957 break;
3958
3959 case RayDAT:
3960 case AIO:
3961 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3962
3963 if (status & 0x8000000)
3964 return 2; /* Sync */
3965 if (status & 0x4000000)
3966 return 1; /* Lock */
3967 return 0; /* No signal */
3968 break;
3969
3970 default:
3971 break;
3972 }
3973 }
3974
3975 return 3; /* N/A */
3976}
3977
3978
3979static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3980 struct snd_ctl_elem_value *ucontrol)
3981{
3982 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3983 int val = -1;
3984
3985 switch (hdspm->io_type) {
3986 case RayDAT:
3987 switch (kcontrol->private_value) {
3988 case 0: /* WC */
3989 val = hdspm_wc_sync_check(hdspm); break;
3990 case 7: /* TCO */
3991 val = hdspm_tco_sync_check(hdspm); break;
3992 case 8: /* SYNC IN */
3993 val = hdspm_sync_in_sync_check(hdspm); break;
3994 default:
d1a3c98d
AK
3995 val = hdspm_s1_sync_check(hdspm,
3996 kcontrol->private_value-1);
0dca1793 3997 }
fba30fd3 3998 break;
0dca1793
AK
3999
4000 case AIO:
4001 switch (kcontrol->private_value) {
4002 case 0: /* WC */
4003 val = hdspm_wc_sync_check(hdspm); break;
4004 case 4: /* TCO */
4005 val = hdspm_tco_sync_check(hdspm); break;
4006 case 5: /* SYNC IN */
4007 val = hdspm_sync_in_sync_check(hdspm); break;
4008 default:
1cb7dbf4
AK
4009 val = hdspm_s1_sync_check(hdspm,
4010 kcontrol->private_value-1);
0dca1793 4011 }
fba30fd3 4012 break;
0dca1793
AK
4013
4014 case MADI:
4015 switch (kcontrol->private_value) {
4016 case 0: /* WC */
4017 val = hdspm_wc_sync_check(hdspm); break;
4018 case 1: /* MADI */
4019 val = hdspm_madi_sync_check(hdspm); break;
4020 case 2: /* TCO */
4021 val = hdspm_tco_sync_check(hdspm); break;
4022 case 3: /* SYNC_IN */
4023 val = hdspm_sync_in_sync_check(hdspm); break;
4024 }
fba30fd3 4025 break;
0dca1793
AK
4026
4027 case MADIface:
4028 val = hdspm_madi_sync_check(hdspm); /* MADI */
4029 break;
4030
4031 case AES32:
4032 switch (kcontrol->private_value) {
4033 case 0: /* WC */
4034 val = hdspm_wc_sync_check(hdspm); break;
4035 case 9: /* TCO */
4036 val = hdspm_tco_sync_check(hdspm); break;
4037 case 10 /* SYNC IN */:
4038 val = hdspm_sync_in_sync_check(hdspm); break;
7c4a95b5 4039 default: /* AES1 to AES8 */
0dca1793 4040 val = hdspm_aes_sync_check(hdspm,
7c4a95b5 4041 kcontrol->private_value-1);
0dca1793 4042 }
fba30fd3 4043 break;
0dca1793
AK
4044
4045 }
4046
34542213
AK
4047 if (hdspm->tco) {
4048 switch (kcontrol->private_value) {
4049 case 11:
4050 /* Check TCO for lock state of its current input */
4051 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
4052 break;
4053 case 12:
4054 /* Check TCO for valid time code on LTC input. */
4055 val = hdspm_tco_input_check(hdspm,
4056 HDSPM_TCO1_LTC_Input_valid);
4057 break;
4058 default:
4059 break;
4060 }
4061 }
4062
0dca1793
AK
4063 if (-1 == val)
4064 val = 3;
4065
4066 ucontrol->value.enumerated.item[0] = val;
4067 return 0;
4068}
4069
4070
4071
4072/**
4073 * TCO controls
4074 **/
4075static void hdspm_tco_write(struct hdspm *hdspm)
4076{
4077 unsigned int tc[4] = { 0, 0, 0, 0};
4078
4079 switch (hdspm->tco->input) {
4080 case 0:
4081 tc[2] |= HDSPM_TCO2_set_input_MSB;
4082 break;
4083 case 1:
4084 tc[2] |= HDSPM_TCO2_set_input_LSB;
4085 break;
4086 default:
4087 break;
4088 }
4089
4090 switch (hdspm->tco->framerate) {
4091 case 1:
4092 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
4093 break;
4094 case 2:
4095 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
4096 break;
4097 case 3:
4098 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
4099 HDSPM_TCO1_set_drop_frame_flag;
4100 break;
4101 case 4:
4102 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4103 HDSPM_TCO1_LTC_Format_MSB;
4104 break;
4105 case 5:
4106 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4107 HDSPM_TCO1_LTC_Format_MSB +
4108 HDSPM_TCO1_set_drop_frame_flag;
4109 break;
4110 default:
4111 break;
4112 }
4113
4114 switch (hdspm->tco->wordclock) {
4115 case 1:
4116 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
4117 break;
4118 case 2:
4119 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4120 break;
4121 default:
4122 break;
4123 }
4124
4125 switch (hdspm->tco->samplerate) {
4126 case 1:
4127 tc[2] |= HDSPM_TCO2_set_freq;
4128 break;
4129 case 2:
4130 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4131 break;
4132 default:
4133 break;
4134 }
4135
4136 switch (hdspm->tco->pull) {
4137 case 1:
4138 tc[2] |= HDSPM_TCO2_set_pull_up;
4139 break;
4140 case 2:
4141 tc[2] |= HDSPM_TCO2_set_pull_down;
4142 break;
4143 case 3:
4144 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4145 break;
4146 case 4:
4147 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4148 break;
4149 default:
4150 break;
4151 }
4152
4153 if (1 == hdspm->tco->term) {
4154 tc[2] |= HDSPM_TCO2_set_term_75R;
4155 }
4156
4157 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4158 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4159 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4160 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4161}
4162
4163
4164#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4165{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4166 .name = xname, \
4167 .index = xindex, \
4168 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4169 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4170 .info = snd_hdspm_info_tco_sample_rate, \
4171 .get = snd_hdspm_get_tco_sample_rate, \
4172 .put = snd_hdspm_put_tco_sample_rate \
4173}
4174
4175static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4176 struct snd_ctl_elem_info *uinfo)
4177{
4178 static char *texts[] = { "44.1 kHz", "48 kHz" };
e5b7b1fe 4179 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4180 return 0;
4181}
4182
4183static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4184 struct snd_ctl_elem_value *ucontrol)
4185{
4186 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4187
4188 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4189
4190 return 0;
4191}
4192
4193static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4194 struct snd_ctl_elem_value *ucontrol)
4195{
4196 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4197
4198 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4199 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4200
4201 hdspm_tco_write(hdspm);
4202
4203 return 1;
4204 }
4205
4206 return 0;
4207}
4208
4209
4210#define HDSPM_TCO_PULL(xname, xindex) \
4211{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4212 .name = xname, \
4213 .index = xindex, \
4214 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4215 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4216 .info = snd_hdspm_info_tco_pull, \
4217 .get = snd_hdspm_get_tco_pull, \
4218 .put = snd_hdspm_put_tco_pull \
4219}
4220
4221static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4222 struct snd_ctl_elem_info *uinfo)
4223{
4224 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
e5b7b1fe 4225 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4226 return 0;
4227}
4228
4229static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4230 struct snd_ctl_elem_value *ucontrol)
4231{
4232 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4233
4234 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4235
4236 return 0;
4237}
4238
4239static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4240 struct snd_ctl_elem_value *ucontrol)
4241{
4242 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4243
4244 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4245 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4246
4247 hdspm_tco_write(hdspm);
4248
4249 return 1;
4250 }
4251
4252 return 0;
4253}
4254
4255#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4256{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4257 .name = xname, \
4258 .index = xindex, \
4259 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4260 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4261 .info = snd_hdspm_info_tco_wck_conversion, \
4262 .get = snd_hdspm_get_tco_wck_conversion, \
4263 .put = snd_hdspm_put_tco_wck_conversion \
4264}
4265
4266static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4267 struct snd_ctl_elem_info *uinfo)
4268{
4269 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
e5b7b1fe 4270 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4271 return 0;
4272}
4273
4274static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4275 struct snd_ctl_elem_value *ucontrol)
4276{
4277 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4278
4279 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4280
4281 return 0;
4282}
4283
4284static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4285 struct snd_ctl_elem_value *ucontrol)
4286{
4287 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4288
4289 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4290 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4291
4292 hdspm_tco_write(hdspm);
4293
4294 return 1;
4295 }
4296
4297 return 0;
4298}
4299
4300
4301#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4302{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4303 .name = xname, \
4304 .index = xindex, \
4305 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4306 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4307 .info = snd_hdspm_info_tco_frame_rate, \
4308 .get = snd_hdspm_get_tco_frame_rate, \
4309 .put = snd_hdspm_put_tco_frame_rate \
4310}
4311
4312static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4313 struct snd_ctl_elem_info *uinfo)
4314{
4315 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4316 "29.97 dfps", "30 fps", "30 dfps" };
e5b7b1fe 4317 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4318 return 0;
4319}
4320
4321static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
3cee5a60
RB
4322 struct snd_ctl_elem_value *ucontrol)
4323{
3cee5a60
RB
4324 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4325
0dca1793 4326 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
3cee5a60 4327
3cee5a60
RB
4328 return 0;
4329}
763f356c 4330
0dca1793
AK
4331static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4332 struct snd_ctl_elem_value *ucontrol)
4333{
4334 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 4335
0dca1793
AK
4336 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4337 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
763f356c 4338
0dca1793
AK
4339 hdspm_tco_write(hdspm);
4340
4341 return 1;
4342 }
4343
4344 return 0;
4345}
763f356c 4346
0dca1793
AK
4347
4348#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4349{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4350 .name = xname, \
4351 .index = xindex, \
4352 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4353 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4354 .info = snd_hdspm_info_tco_sync_source, \
4355 .get = snd_hdspm_get_tco_sync_source, \
4356 .put = snd_hdspm_put_tco_sync_source \
4357}
4358
4359static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4360 struct snd_ctl_elem_info *uinfo)
4361{
4362 static char *texts[] = { "LTC", "Video", "WCK" };
e5b7b1fe 4363 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4364 return 0;
4365}
4366
4367static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4368 struct snd_ctl_elem_value *ucontrol)
4369{
4370 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4371
4372 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4373
4374 return 0;
4375}
4376
4377static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4378 struct snd_ctl_elem_value *ucontrol)
4379{
4380 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4381
4382 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4383 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4384
4385 hdspm_tco_write(hdspm);
4386
4387 return 1;
4388 }
4389
4390 return 0;
4391}
4392
4393
4394#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4395{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4396 .name = xname, \
4397 .index = xindex, \
4398 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4399 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4400 .info = snd_hdspm_info_tco_word_term, \
4401 .get = snd_hdspm_get_tco_word_term, \
4402 .put = snd_hdspm_put_tco_word_term \
4403}
4404
4405static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4406 struct snd_ctl_elem_info *uinfo)
4407{
4408 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4409 uinfo->count = 1;
4410 uinfo->value.integer.min = 0;
4411 uinfo->value.integer.max = 1;
4412
4413 return 0;
4414}
4415
4416
4417static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4418 struct snd_ctl_elem_value *ucontrol)
4419{
4420 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4421
4422 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4423
4424 return 0;
4425}
4426
4427
4428static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4429 struct snd_ctl_elem_value *ucontrol)
4430{
4431 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4432
4433 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4434 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4435
4436 hdspm_tco_write(hdspm);
4437
4438 return 1;
4439 }
4440
4441 return 0;
4442}
4443
4444
4445
4446
4447static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
4448 HDSPM_MIXER("Mixer", 0),
4449 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
763f356c
TI
4450 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4451 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4452 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4453 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
b8812c55 4454 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
0dca1793
AK
4455 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4456 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
930f4ff0 4457 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
0dca1793 4458 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
c9e1668c
AK
4459 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4460 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
696be0fb 4461 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
c9e1668c
AK
4462 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4463 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
700d1ef3
AK
4464 HDSPM_INPUT_SELECT("Input Select", 0),
4465 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
0dca1793
AK
4466};
4467
4468
4469static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4470 HDSPM_MIXER("Mixer", 0),
4471 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4472 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4473 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4474 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4475 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
c9e1668c
AK
4476 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4477 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4478 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
700d1ef3 4479 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
763f356c
TI
4480};
4481
0dca1793
AK
4482static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
4483 HDSPM_MIXER("Mixer", 0),
4484 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4485 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4486 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
0dca1793
AK
4487 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4488 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4489 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4490 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4491 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4492 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4493 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4494 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4495 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4496 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4497 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4498 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4499 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
fb0f121e 4500 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
42f4c12d 4501 HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
fb0f121e
AK
4502 HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
4503 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4504 HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
4505 HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
42f4c12d
AK
4506 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
4507 HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
4508 HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
4509 HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
0dca1793
AK
4510
4511 /*
4512 HDSPM_INPUT_SELECT("Input Select", 0),
4513 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4514 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4515 HDSPM_SPDIF_IN("SPDIF In", 0);
4516 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4517 HDSPM_INPUT_LEVEL("Input Level", 0);
4518 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4519 HDSPM_PHONES("Phones", 0);
4520 */
4521};
3cee5a60 4522
0dca1793
AK
4523static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4524 HDSPM_MIXER("Mixer", 0),
4525 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4526 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4527 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4528 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4529 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4530 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4531 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4532 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4533 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4534 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4535 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4536 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4537 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4538 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4539 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4540 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4541 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4542 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4543 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4544 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4545 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
11a5cd3c
AK
4546 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
4547 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4548 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
0dca1793
AK
4549};
4550
4551static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
3cee5a60 4552 HDSPM_MIXER("Mixer", 0),
0dca1793 4553 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
3cee5a60
RB
4554 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4555 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4556 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4557 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
2d63ec38 4558 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 11),
0dca1793
AK
4559 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4560 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4561 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4562 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4563 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4564 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4565 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4566 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4567 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4568 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4569 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4570 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4571 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4572 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4573 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4574 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4575 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4576 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4577 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4578 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4579 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4580 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
c9e1668c
AK
4581 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4582 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4583 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4584 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4585 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
3cee5a60
RB
4586 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4587 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4588};
4589
0dca1793
AK
4590
4591
4592/* Control elements for the optional TCO module */
4593static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4594 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4595 HDSPM_TCO_PULL("TCO Pull", 0),
4596 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4597 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4598 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
a817650e
AK
4599 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4600 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4601 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4602 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4603 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
0dca1793
AK
4604};
4605
4606
98274f07 4607static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
763f356c
TI
4608
4609
98274f07 4610static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
763f356c
TI
4611{
4612 int i;
4613
0dca1793 4614 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
763f356c
TI
4615 if (hdspm->system_sample_rate > 48000) {
4616 hdspm->playback_mixer_ctls[i]->vd[0].access =
0dca1793
AK
4617 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4618 SNDRV_CTL_ELEM_ACCESS_READ |
4619 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
763f356c
TI
4620 } else {
4621 hdspm->playback_mixer_ctls[i]->vd[0].access =
0dca1793
AK
4622 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4623 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
763f356c
TI
4624 }
4625 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
0dca1793
AK
4626 SNDRV_CTL_EVENT_MASK_INFO,
4627 &hdspm->playback_mixer_ctls[i]->id);
763f356c
TI
4628 }
4629
4630 return 0;
4631}
4632
4633
0dca1793
AK
4634static int snd_hdspm_create_controls(struct snd_card *card,
4635 struct hdspm *hdspm)
763f356c
TI
4636{
4637 unsigned int idx, limit;
4638 int err;
98274f07 4639 struct snd_kcontrol *kctl;
0dca1793 4640 struct snd_kcontrol_new *list = NULL;
763f356c 4641
0dca1793
AK
4642 switch (hdspm->io_type) {
4643 case MADI:
4644 list = snd_hdspm_controls_madi;
4645 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4646 break;
4647 case MADIface:
4648 list = snd_hdspm_controls_madiface;
4649 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4650 break;
4651 case AIO:
4652 list = snd_hdspm_controls_aio;
4653 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4654 break;
4655 case RayDAT:
4656 list = snd_hdspm_controls_raydat;
4657 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4658 break;
4659 case AES32:
4660 list = snd_hdspm_controls_aes32;
4661 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4662 break;
4663 }
3cee5a60 4664
0dca1793
AK
4665 if (NULL != list) {
4666 for (idx = 0; idx < limit; idx++) {
3cee5a60 4667 err = snd_ctl_add(card,
0dca1793 4668 snd_ctl_new1(&list[idx], hdspm));
3cee5a60
RB
4669 if (err < 0)
4670 return err;
763f356c
TI
4671 }
4672 }
4673
763f356c 4674
0dca1793 4675 /* create simple 1:1 playback mixer controls */
763f356c 4676 snd_hdspm_playback_mixer.name = "Chn";
0dca1793
AK
4677 if (hdspm->system_sample_rate >= 128000) {
4678 limit = hdspm->qs_out_channels;
4679 } else if (hdspm->system_sample_rate >= 64000) {
4680 limit = hdspm->ds_out_channels;
4681 } else {
4682 limit = hdspm->ss_out_channels;
4683 }
763f356c
TI
4684 for (idx = 0; idx < limit; ++idx) {
4685 snd_hdspm_playback_mixer.index = idx + 1;
ef5fa1a4
TI
4686 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4687 err = snd_ctl_add(card, kctl);
4688 if (err < 0)
763f356c 4689 return err;
763f356c
TI
4690 hdspm->playback_mixer_ctls[idx] = kctl;
4691 }
4692
0dca1793
AK
4693
4694 if (hdspm->tco) {
4695 /* add tco control elements */
4696 list = snd_hdspm_controls_tco;
4697 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4698 for (idx = 0; idx < limit; idx++) {
4699 err = snd_ctl_add(card,
4700 snd_ctl_new1(&list[idx], hdspm));
4701 if (err < 0)
4702 return err;
4703 }
4704 }
4705
763f356c
TI
4706 return 0;
4707}
4708
4709/*------------------------------------------------------------
0dca1793 4710 /proc interface
763f356c
TI
4711 ------------------------------------------------------------*/
4712
4713static void
5760107c
AK
4714snd_hdspm_proc_read_tco(struct snd_info_entry *entry,
4715 struct snd_info_buffer *buffer)
763f356c 4716{
ef5fa1a4 4717 struct hdspm *hdspm = entry->private_data;
5760107c 4718 unsigned int status, control;
0dca1793
AK
4719 int a, ltc, frames, seconds, minutes, hours;
4720 unsigned int period;
4721 u64 freq_const = 0;
4722 u32 rate;
4723
5760107c
AK
4724 snd_iprintf(buffer, "--- TCO ---\n");
4725
763f356c 4726 status = hdspm_read(hdspm, HDSPM_statusRegister);
0dca1793 4727 control = hdspm->control_register;
763f356c 4728
763f356c 4729
0dca1793
AK
4730 if (status & HDSPM_tco_detect) {
4731 snd_iprintf(buffer, "TCO module detected.\n");
4732 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4733 if (a & HDSPM_TCO1_LTC_Input_valid) {
4734 snd_iprintf(buffer, " LTC valid, ");
4735 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4736 HDSPM_TCO1_LTC_Format_MSB)) {
4737 case 0:
4738 snd_iprintf(buffer, "24 fps, ");
4739 break;
4740 case HDSPM_TCO1_LTC_Format_LSB:
4741 snd_iprintf(buffer, "25 fps, ");
4742 break;
4743 case HDSPM_TCO1_LTC_Format_MSB:
4744 snd_iprintf(buffer, "29.97 fps, ");
4745 break;
4746 default:
4747 snd_iprintf(buffer, "30 fps, ");
4748 break;
4749 }
4750 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4751 snd_iprintf(buffer, "drop frame\n");
4752 } else {
4753 snd_iprintf(buffer, "full frame\n");
4754 }
4755 } else {
4756 snd_iprintf(buffer, " no LTC\n");
4757 }
4758 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4759 snd_iprintf(buffer, " Video: NTSC\n");
4760 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4761 snd_iprintf(buffer, " Video: PAL\n");
4762 } else {
4763 snd_iprintf(buffer, " No video\n");
4764 }
4765 if (a & HDSPM_TCO1_TCO_lock) {
4766 snd_iprintf(buffer, " Sync: lock\n");
4767 } else {
4768 snd_iprintf(buffer, " Sync: no lock\n");
4769 }
4770
4771 switch (hdspm->io_type) {
4772 case MADI:
4773 case AES32:
4774 freq_const = 110069313433624ULL;
4775 break;
4776 case RayDAT:
4777 case AIO:
4778 freq_const = 104857600000000ULL;
4779 break;
4780 case MADIface:
4781 break; /* no TCO possible */
4782 }
4783
4784 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4785 snd_iprintf(buffer, " period: %u\n", period);
4786
4787
4788 /* rate = freq_const/period; */
4789 rate = div_u64(freq_const, period);
4790
4791 if (control & HDSPM_QuadSpeed) {
4792 rate *= 4;
4793 } else if (control & HDSPM_DoubleSpeed) {
4794 rate *= 2;
4795 }
4796
4797 snd_iprintf(buffer, " Frequency: %u Hz\n",
4798 (unsigned int) rate);
4799
4800 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4801 frames = ltc & 0xF;
4802 ltc >>= 4;
4803 frames += (ltc & 0x3) * 10;
4804 ltc >>= 4;
4805 seconds = ltc & 0xF;
4806 ltc >>= 4;
4807 seconds += (ltc & 0x7) * 10;
4808 ltc >>= 4;
4809 minutes = ltc & 0xF;
4810 ltc >>= 4;
4811 minutes += (ltc & 0x7) * 10;
4812 ltc >>= 4;
4813 hours = ltc & 0xF;
4814 ltc >>= 4;
4815 hours += (ltc & 0x3) * 10;
4816 snd_iprintf(buffer,
4817 " LTC In: %02d:%02d:%02d:%02d\n",
4818 hours, minutes, seconds, frames);
4819
4820 } else {
4821 snd_iprintf(buffer, "No TCO module detected.\n");
4822 }
5760107c
AK
4823}
4824
4825static void
4826snd_hdspm_proc_read_madi(struct snd_info_entry *entry,
4827 struct snd_info_buffer *buffer)
4828{
4829 struct hdspm *hdspm = entry->private_data;
4830 unsigned int status, status2, control, freq;
4831
4832 char *pref_sync_ref;
4833 char *autosync_ref;
4834 char *system_clock_mode;
4835 char *insel;
4836 int x, x2;
4837
4838 status = hdspm_read(hdspm, HDSPM_statusRegister);
4839 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4840 control = hdspm->control_register;
4841 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
4842
4843 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
4844 hdspm->card_name, hdspm->card->number + 1,
4845 hdspm->firmware_rev,
4846 (status2 & HDSPM_version0) |
4847 (status2 & HDSPM_version1) | (status2 &
4848 HDSPM_version2));
4849
4850 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4851 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
4852 hdspm->serial);
4853
4854 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4855 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4856
4857 snd_iprintf(buffer, "--- System ---\n");
4858
4859 snd_iprintf(buffer,
4860 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4861 status & HDSPM_audioIRQPending,
4862 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4863 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4864 hdspm->irq_count);
4865 snd_iprintf(buffer,
4866 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4867 "estimated= %ld (bytes)\n",
4868 ((status & HDSPM_BufferID) ? 1 : 0),
4869 (status & HDSPM_BufferPositionMask),
4870 (status & HDSPM_BufferPositionMask) %
4871 (2 * (int)hdspm->period_bytes),
4872 ((status & HDSPM_BufferPositionMask) - 64) %
4873 (2 * (int)hdspm->period_bytes),
4874 (long) hdspm_hw_pointer(hdspm) * 4);
4875
4876 snd_iprintf(buffer,
4877 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4878 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4879 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4880 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4881 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4882 snd_iprintf(buffer,
4883 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4884 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4885 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4886 snd_iprintf(buffer,
4887 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4888 "status2=0x%x\n",
4889 hdspm->control_register, hdspm->control2_register,
4890 status, status2);
4891
763f356c
TI
4892
4893 snd_iprintf(buffer, "--- Settings ---\n");
4894
7cb155ff 4895 x = hdspm_get_latency(hdspm);
763f356c
TI
4896
4897 snd_iprintf(buffer,
0dca1793
AK
4898 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4899 x, (unsigned long) hdspm->period_bytes);
763f356c 4900
0dca1793
AK
4901 snd_iprintf(buffer, "Line out: %s\n",
4902 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
763f356c
TI
4903
4904 switch (hdspm->control_register & HDSPM_InputMask) {
4905 case HDSPM_InputOptical:
4906 insel = "Optical";
4907 break;
4908 case HDSPM_InputCoaxial:
4909 insel = "Coaxial";
4910 break;
4911 default:
ec8f53fb 4912 insel = "Unknown";
763f356c 4913 }
763f356c
TI
4914
4915 snd_iprintf(buffer,
0dca1793
AK
4916 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4917 "Auto Input %s\n",
4918 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4919 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4920 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
4921
763f356c 4922
3cee5a60 4923 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
0dca1793 4924 system_clock_mode = "AutoSync";
3cee5a60 4925 else
763f356c 4926 system_clock_mode = "Master";
0dca1793 4927 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
763f356c
TI
4928
4929 switch (hdspm_pref_sync_ref(hdspm)) {
4930 case HDSPM_SYNC_FROM_WORD:
4931 pref_sync_ref = "Word Clock";
4932 break;
4933 case HDSPM_SYNC_FROM_MADI:
4934 pref_sync_ref = "MADI Sync";
4935 break;
0dca1793
AK
4936 case HDSPM_SYNC_FROM_TCO:
4937 pref_sync_ref = "TCO";
4938 break;
4939 case HDSPM_SYNC_FROM_SYNC_IN:
4940 pref_sync_ref = "Sync In";
4941 break;
763f356c
TI
4942 default:
4943 pref_sync_ref = "XXXX Clock";
4944 break;
4945 }
4946 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
0dca1793 4947 pref_sync_ref);
763f356c
TI
4948
4949 snd_iprintf(buffer, "System Clock Frequency: %d\n",
0dca1793 4950 hdspm->system_sample_rate);
763f356c
TI
4951
4952
4953 snd_iprintf(buffer, "--- Status:\n");
4954
4955 x = status & HDSPM_madiSync;
4956 x2 = status2 & HDSPM_wcSync;
4957
4958 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
0dca1793
AK
4959 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4960 "NoLock",
4961 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4962 "NoLock");
763f356c
TI
4963
4964 switch (hdspm_autosync_ref(hdspm)) {
0dca1793
AK
4965 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4966 autosync_ref = "Sync In";
4967 break;
4968 case HDSPM_AUTOSYNC_FROM_TCO:
4969 autosync_ref = "TCO";
4970 break;
763f356c
TI
4971 case HDSPM_AUTOSYNC_FROM_WORD:
4972 autosync_ref = "Word Clock";
4973 break;
4974 case HDSPM_AUTOSYNC_FROM_MADI:
4975 autosync_ref = "MADI Sync";
4976 break;
4977 case HDSPM_AUTOSYNC_FROM_NONE:
4978 autosync_ref = "Input not valid";
4979 break;
4980 default:
4981 autosync_ref = "---";
4982 break;
4983 }
4984 snd_iprintf(buffer,
0dca1793
AK
4985 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4986 autosync_ref, hdspm_external_sample_rate(hdspm),
4987 (status & HDSPM_madiFreqMask) >> 22,
4988 (status2 & HDSPM_wcFreqMask) >> 5);
763f356c
TI
4989
4990 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
0dca1793
AK
4991 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4992 (status & HDSPM_RX_64ch) ? "64 channels" :
4993 "56 channels");
763f356c 4994
5760107c
AK
4995 /* call readout function for TCO specific status */
4996 snd_hdspm_proc_read_tco(entry, buffer);
4997
763f356c
TI
4998 snd_iprintf(buffer, "\n");
4999}
5000
3cee5a60
RB
5001static void
5002snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
5003 struct snd_info_buffer *buffer)
5004{
ef5fa1a4 5005 struct hdspm *hdspm = entry->private_data;
3cee5a60
RB
5006 unsigned int status;
5007 unsigned int status2;
5008 unsigned int timecode;
56bde0f3 5009 unsigned int wcLock, wcSync;
3cee5a60
RB
5010 int pref_syncref;
5011 char *autosync_ref;
3cee5a60
RB
5012 int x;
5013
5014 status = hdspm_read(hdspm, HDSPM_statusRegister);
5015 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
5016 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
5017
5018 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
5019 hdspm->card_name, hdspm->card->number + 1,
5020 hdspm->firmware_rev);
5021
5022 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
5023 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
5024
5025 snd_iprintf(buffer, "--- System ---\n");
5026
5027 snd_iprintf(buffer,
5028 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
5029 status & HDSPM_audioIRQPending,
5030 (status & HDSPM_midi0IRQPending) ? 1 : 0,
5031 (status & HDSPM_midi1IRQPending) ? 1 : 0,
5032 hdspm->irq_count);
5033 snd_iprintf(buffer,
ef5fa1a4
TI
5034 "HW pointer: id = %d, rawptr = %d (%d->%d) "
5035 "estimated= %ld (bytes)\n",
3cee5a60
RB
5036 ((status & HDSPM_BufferID) ? 1 : 0),
5037 (status & HDSPM_BufferPositionMask),
ef5fa1a4
TI
5038 (status & HDSPM_BufferPositionMask) %
5039 (2 * (int)hdspm->period_bytes),
5040 ((status & HDSPM_BufferPositionMask) - 64) %
5041 (2 * (int)hdspm->period_bytes),
3cee5a60
RB
5042 (long) hdspm_hw_pointer(hdspm) * 4);
5043
5044 snd_iprintf(buffer,
5045 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
5046 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
5047 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
5048 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
5049 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
5050 snd_iprintf(buffer,
0dca1793
AK
5051 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
5052 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
5053 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
5054 snd_iprintf(buffer,
5055 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
5056 "status2=0x%x\n",
5057 hdspm->control_register, hdspm->control2_register,
5058 status, status2);
3cee5a60
RB
5059
5060 snd_iprintf(buffer, "--- Settings ---\n");
5061
7cb155ff 5062 x = hdspm_get_latency(hdspm);
3cee5a60
RB
5063
5064 snd_iprintf(buffer,
5065 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
5066 x, (unsigned long) hdspm->period_bytes);
5067
0dca1793 5068 snd_iprintf(buffer, "Line out: %s\n",
3cee5a60 5069 (hdspm->
0dca1793 5070 control_register & HDSPM_LineOut) ? "on " : "off");
3cee5a60
RB
5071
5072 snd_iprintf(buffer,
5073 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
5074 (hdspm->
5075 control_register & HDSPM_clr_tms) ? "on" : "off",
5076 (hdspm->
5077 control_register & HDSPM_Emphasis) ? "on" : "off",
5078 (hdspm->
5079 control_register & HDSPM_Dolby) ? "on" : "off");
5080
3cee5a60
RB
5081
5082 pref_syncref = hdspm_pref_sync_ref(hdspm);
5083 if (pref_syncref == 0)
5084 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
5085 else
5086 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
5087 pref_syncref);
5088
5089 snd_iprintf(buffer, "System Clock Frequency: %d\n",
5090 hdspm->system_sample_rate);
5091
5092 snd_iprintf(buffer, "Double speed: %s\n",
5093 hdspm->control_register & HDSPM_DS_DoubleWire?
5094 "Double wire" : "Single wire");
5095 snd_iprintf(buffer, "Quad speed: %s\n",
5096 hdspm->control_register & HDSPM_QS_DoubleWire?
5097 "Double wire" :
5098 hdspm->control_register & HDSPM_QS_QuadWire?
5099 "Quad wire" : "Single wire");
5100
5101 snd_iprintf(buffer, "--- Status:\n");
5102
56bde0f3
AS
5103 wcLock = status & HDSPM_AES32_wcLock;
5104 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
5105
3cee5a60 5106 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
56bde0f3 5107 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
ef5fa1a4 5108 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
3cee5a60
RB
5109
5110 for (x = 0; x < 8; x++) {
5111 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
ef5fa1a4
TI
5112 x+1,
5113 (status2 & (HDSPM_LockAES >> x)) ?
0dca1793 5114 "Sync " : "No Lock",
ef5fa1a4 5115 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
3cee5a60
RB
5116 }
5117
5118 switch (hdspm_autosync_ref(hdspm)) {
0dca1793
AK
5119 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5120 autosync_ref = "None"; break;
5121 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5122 autosync_ref = "Word Clock"; break;
5123 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5124 autosync_ref = "AES1"; break;
5125 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5126 autosync_ref = "AES2"; break;
5127 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5128 autosync_ref = "AES3"; break;
5129 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5130 autosync_ref = "AES4"; break;
5131 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5132 autosync_ref = "AES5"; break;
5133 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5134 autosync_ref = "AES6"; break;
5135 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5136 autosync_ref = "AES7"; break;
5137 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5138 autosync_ref = "AES8"; break;
194062da
AK
5139 case HDSPM_AES32_AUTOSYNC_FROM_TCO:
5140 autosync_ref = "TCO"; break;
5141 case HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN:
5142 autosync_ref = "Sync In"; break;
0dca1793
AK
5143 default:
5144 autosync_ref = "---"; break;
3cee5a60
RB
5145 }
5146 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5147
194062da
AK
5148 /* call readout function for TCO specific status */
5149 snd_hdspm_proc_read_tco(entry, buffer);
5150
3cee5a60
RB
5151 snd_iprintf(buffer, "\n");
5152}
5153
0dca1793
AK
5154static void
5155snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5156 struct snd_info_buffer *buffer)
5157{
5158 struct hdspm *hdspm = entry->private_data;
5159 unsigned int status1, status2, status3, control, i;
5160 unsigned int lock, sync;
5161
5162 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5163 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5164 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5165
5166 control = hdspm->control_register;
5167
5168 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5169 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5170 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5171
5172
5173 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5174
5175 snd_iprintf(buffer, "Clock mode : %s\n",
5176 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5177 snd_iprintf(buffer, "System frequency: %d Hz\n",
5178 hdspm_get_system_sample_rate(hdspm));
5179
5180 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5181
5182 lock = 0x1;
5183 sync = 0x100;
5184
5185 for (i = 0; i < 8; i++) {
5186 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5187 i,
5188 (status1 & lock) ? 1 : 0,
5189 (status1 & sync) ? 1 : 0,
5190 texts_freq[(status2 >> (i * 4)) & 0xF]);
5191
5192 lock = lock<<1;
5193 sync = sync<<1;
5194 }
5195
5196 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5197 (status1 & 0x1000000) ? 1 : 0,
5198 (status1 & 0x2000000) ? 1 : 0,
5199 texts_freq[(status1 >> 16) & 0xF]);
5200
5201 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5202 (status1 & 0x4000000) ? 1 : 0,
5203 (status1 & 0x8000000) ? 1 : 0,
5204 texts_freq[(status1 >> 20) & 0xF]);
5205
5206 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5207 (status3 & 0x400) ? 1 : 0,
5208 (status3 & 0x800) ? 1 : 0,
5209 texts_freq[(status2 >> 12) & 0xF]);
5210
5211}
5212
3cee5a60
RB
5213#ifdef CONFIG_SND_DEBUG
5214static void
0dca1793 5215snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
3cee5a60
RB
5216 struct snd_info_buffer *buffer)
5217{
ef5fa1a4 5218 struct hdspm *hdspm = entry->private_data;
3cee5a60
RB
5219
5220 int j,i;
5221
ef5fa1a4 5222 for (i = 0; i < 256 /* 1024*64 */; i += j) {
3cee5a60
RB
5223 snd_iprintf(buffer, "0x%08X: ", i);
5224 for (j = 0; j < 16; j += 4)
5225 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5226 snd_iprintf(buffer, "\n");
5227 }
5228}
5229#endif
5230
5231
0dca1793
AK
5232static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5233 struct snd_info_buffer *buffer)
5234{
5235 struct hdspm *hdspm = entry->private_data;
5236 int i;
5237
5238 snd_iprintf(buffer, "# generated by hdspm\n");
5239
5240 for (i = 0; i < hdspm->max_channels_in; i++) {
5241 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5242 }
5243}
5244
5245static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5246 struct snd_info_buffer *buffer)
5247{
5248 struct hdspm *hdspm = entry->private_data;
5249 int i;
5250
5251 snd_iprintf(buffer, "# generated by hdspm\n");
5252
5253 for (i = 0; i < hdspm->max_channels_out; i++) {
5254 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5255 }
5256}
5257
3cee5a60 5258
e23e7a14 5259static void snd_hdspm_proc_init(struct hdspm *hdspm)
763f356c 5260{
98274f07 5261 struct snd_info_entry *entry;
763f356c 5262
0dca1793
AK
5263 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5264 switch (hdspm->io_type) {
5265 case AES32:
5266 snd_info_set_text_ops(entry, hdspm,
5267 snd_hdspm_proc_read_aes32);
5268 break;
5269 case MADI:
5270 snd_info_set_text_ops(entry, hdspm,
5271 snd_hdspm_proc_read_madi);
5272 break;
5273 case MADIface:
5274 /* snd_info_set_text_ops(entry, hdspm,
5275 snd_hdspm_proc_read_madiface); */
5276 break;
5277 case RayDAT:
5278 snd_info_set_text_ops(entry, hdspm,
5279 snd_hdspm_proc_read_raydat);
5280 break;
5281 case AIO:
5282 break;
5283 }
5284 }
5285
5286 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5287 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5288 }
5289
5290 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5291 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5292 }
5293
3cee5a60
RB
5294#ifdef CONFIG_SND_DEBUG
5295 /* debug file to read all hdspm registers */
5296 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5297 snd_info_set_text_ops(entry, hdspm,
5298 snd_hdspm_proc_read_debug);
5299#endif
763f356c
TI
5300}
5301
5302/*------------------------------------------------------------
0dca1793 5303 hdspm intitialize
763f356c
TI
5304 ------------------------------------------------------------*/
5305
98274f07 5306static int snd_hdspm_set_defaults(struct hdspm * hdspm)
763f356c 5307{
763f356c 5308 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
561de31a 5309 hold it (e.g. during module initialization).
0dca1793 5310 */
763f356c
TI
5311
5312 /* set defaults: */
5313
0dca1793
AK
5314 hdspm->settings_register = 0;
5315
5316 switch (hdspm->io_type) {
5317 case MADI:
5318 case MADIface:
5319 hdspm->control_register =
5320 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5321 break;
5322
5323 case RayDAT:
5324 case AIO:
5325 hdspm->settings_register = 0x1 + 0x1000;
5326 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5327 * line_out */
5328 hdspm->control_register =
5329 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5330 break;
5331
5332 case AES32:
ef5fa1a4 5333 hdspm->control_register =
e71b95ad 5334 HDSPM_ClockModeMaster | /* Master Clock Mode on */
0dca1793 5335 hdspm_encode_latency(7) | /* latency max=8192samples */
3cee5a60
RB
5336 HDSPM_SyncRef0 | /* AES1 is syncclock */
5337 HDSPM_LineOut | /* Analog output in */
5338 HDSPM_Professional; /* Professional mode */
0dca1793
AK
5339 break;
5340 }
763f356c
TI
5341
5342 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5343
0dca1793 5344 if (AES32 == hdspm->io_type) {
ffb2c3c0 5345 /* No control2 register for AES32 */
763f356c 5346#ifdef SNDRV_BIG_ENDIAN
ffb2c3c0 5347 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
763f356c 5348#else
ffb2c3c0 5349 hdspm->control2_register = 0;
763f356c
TI
5350#endif
5351
ffb2c3c0
RB
5352 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5353 }
763f356c
TI
5354 hdspm_compute_period_size(hdspm);
5355
5356 /* silence everything */
5357
5358 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5359
b2ed6326 5360 if (hdspm_is_raydat_or_aio(hdspm))
0dca1793 5361 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
763f356c
TI
5362
5363 /* set a default rate so that the channel map is set up. */
0dca1793 5364 hdspm_set_rate(hdspm, 48000, 1);
763f356c
TI
5365
5366 return 0;
5367}
5368
5369
5370/*------------------------------------------------------------
0dca1793 5371 interrupt
763f356c
TI
5372 ------------------------------------------------------------*/
5373
7d12e780 5374static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
763f356c 5375{
98274f07 5376 struct hdspm *hdspm = (struct hdspm *) dev_id;
763f356c 5377 unsigned int status;
0dca1793
AK
5378 int i, audio, midi, schedule = 0;
5379 /* cycles_t now; */
763f356c
TI
5380
5381 status = hdspm_read(hdspm, HDSPM_statusRegister);
5382
5383 audio = status & HDSPM_audioIRQPending;
0dca1793
AK
5384 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5385 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
5386
5387 /* now = get_cycles(); */
5388 /**
5389 * LAT_2..LAT_0 period counter (win) counter (mac)
5390 * 6 4096 ~256053425 ~514672358
5391 * 5 2048 ~128024983 ~257373821
5392 * 4 1024 ~64023706 ~128718089
5393 * 3 512 ~32005945 ~64385999
5394 * 2 256 ~16003039 ~32260176
5395 * 1 128 ~7998738 ~16194507
5396 * 0 64 ~3998231 ~8191558
5397 **/
5398 /*
5399 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5400 now-hdspm->last_interrupt, status & 0xFFC0);
5401 hdspm->last_interrupt = now;
5402 */
763f356c 5403
0dca1793 5404 if (!audio && !midi)
763f356c
TI
5405 return IRQ_NONE;
5406
5407 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5408 hdspm->irq_count++;
5409
763f356c
TI
5410
5411 if (audio) {
763f356c 5412 if (hdspm->capture_substream)
ef5fa1a4 5413 snd_pcm_period_elapsed(hdspm->capture_substream);
763f356c
TI
5414
5415 if (hdspm->playback_substream)
ef5fa1a4 5416 snd_pcm_period_elapsed(hdspm->playback_substream);
763f356c
TI
5417 }
5418
0dca1793
AK
5419 if (midi) {
5420 i = 0;
5421 while (i < hdspm->midiPorts) {
5422 if ((hdspm_read(hdspm,
5423 hdspm->midi[i].statusIn) & 0xff) &&
5424 (status & hdspm->midi[i].irq)) {
5425 /* we disable interrupts for this input until
5426 * processing is done
5427 */
5428 hdspm->control_register &= ~hdspm->midi[i].ie;
5429 hdspm_write(hdspm, HDSPM_controlRegister,
5430 hdspm->control_register);
5431 hdspm->midi[i].pending = 1;
5432 schedule = 1;
5433 }
5434
5435 i++;
5436 }
5437
5438 if (schedule)
5439 tasklet_hi_schedule(&hdspm->midi_tasklet);
763f356c 5440 }
0dca1793 5441
763f356c
TI
5442 return IRQ_HANDLED;
5443}
5444
5445/*------------------------------------------------------------
0dca1793 5446 pcm interface
763f356c
TI
5447 ------------------------------------------------------------*/
5448
5449
0dca1793
AK
5450static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5451 *substream)
763f356c 5452{
98274f07 5453 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5454 return hdspm_hw_pointer(hdspm);
5455}
5456
763f356c 5457
98274f07 5458static int snd_hdspm_reset(struct snd_pcm_substream *substream)
763f356c 5459{
98274f07
TI
5460 struct snd_pcm_runtime *runtime = substream->runtime;
5461 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5462 struct snd_pcm_substream *other;
763f356c
TI
5463
5464 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5465 other = hdspm->capture_substream;
5466 else
5467 other = hdspm->playback_substream;
5468
5469 if (hdspm->running)
5470 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5471 else
5472 runtime->status->hw_ptr = 0;
5473 if (other) {
98274f07
TI
5474 struct snd_pcm_substream *s;
5475 struct snd_pcm_runtime *oruntime = other->runtime;
ef991b95 5476 snd_pcm_group_for_each_entry(s, substream) {
763f356c
TI
5477 if (s == other) {
5478 oruntime->status->hw_ptr =
0dca1793 5479 runtime->status->hw_ptr;
763f356c
TI
5480 break;
5481 }
5482 }
5483 }
5484 return 0;
5485}
5486
98274f07
TI
5487static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5488 struct snd_pcm_hw_params *params)
763f356c 5489{
98274f07 5490 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5491 int err;
5492 int i;
5493 pid_t this_pid;
5494 pid_t other_pid;
763f356c
TI
5495
5496 spin_lock_irq(&hdspm->lock);
5497
5498 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5499 this_pid = hdspm->playback_pid;
5500 other_pid = hdspm->capture_pid;
5501 } else {
5502 this_pid = hdspm->capture_pid;
5503 other_pid = hdspm->playback_pid;
5504 }
5505
ef5fa1a4 5506 if (other_pid > 0 && this_pid != other_pid) {
763f356c
TI
5507
5508 /* The other stream is open, and not by the same
5509 task as this one. Make sure that the parameters
5510 that matter are the same.
0dca1793 5511 */
763f356c
TI
5512
5513 if (params_rate(params) != hdspm->system_sample_rate) {
5514 spin_unlock_irq(&hdspm->lock);
5515 _snd_pcm_hw_param_setempty(params,
0dca1793 5516 SNDRV_PCM_HW_PARAM_RATE);
763f356c
TI
5517 return -EBUSY;
5518 }
5519
5520 if (params_period_size(params) != hdspm->period_bytes / 4) {
5521 spin_unlock_irq(&hdspm->lock);
5522 _snd_pcm_hw_param_setempty(params,
0dca1793 5523 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c
TI
5524 return -EBUSY;
5525 }
5526
5527 }
5528 /* We're fine. */
5529 spin_unlock_irq(&hdspm->lock);
5530
5531 /* how to make sure that the rate matches an externally-set one ? */
5532
5533 spin_lock_irq(&hdspm->lock);
ef5fa1a4
TI
5534 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5535 if (err < 0) {
0dca1793 5536 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
763f356c
TI
5537 spin_unlock_irq(&hdspm->lock);
5538 _snd_pcm_hw_param_setempty(params,
0dca1793 5539 SNDRV_PCM_HW_PARAM_RATE);
763f356c
TI
5540 return err;
5541 }
5542 spin_unlock_irq(&hdspm->lock);
5543
ef5fa1a4 5544 err = hdspm_set_interrupt_interval(hdspm,
0dca1793 5545 params_period_size(params));
ef5fa1a4 5546 if (err < 0) {
0dca1793 5547 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
763f356c 5548 _snd_pcm_hw_param_setempty(params,
0dca1793 5549 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c
TI
5550 return err;
5551 }
5552
ef5fa1a4
TI
5553 /* Memory allocation, takashi's method, dont know if we should
5554 * spinlock
5555 */
763f356c 5556 /* malloc all buffer even if not enabled to get sure */
ffb2c3c0
RB
5557 /* Update for MADI rev 204: we need to allocate for all channels,
5558 * otherwise it doesn't work at 96kHz */
0dca1793 5559
763f356c 5560 err =
0dca1793
AK
5561 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5562 if (err < 0) {
5563 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
763f356c 5564 return err;
0dca1793 5565 }
763f356c 5566
763f356c
TI
5567 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5568
77a23f26 5569 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
763f356c
TI
5570 params_channels(params));
5571
5572 for (i = 0; i < params_channels(params); ++i)
5573 snd_hdspm_enable_out(hdspm, i, 1);
5574
5575 hdspm->playback_buffer =
0dca1793 5576 (unsigned char *) substream->runtime->dma_area;
54bf5dd9 5577 snd_printdd("Allocated sample buffer for playback at %p\n",
3cee5a60 5578 hdspm->playback_buffer);
763f356c 5579 } else {
77a23f26 5580 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
763f356c
TI
5581 params_channels(params));
5582
5583 for (i = 0; i < params_channels(params); ++i)
5584 snd_hdspm_enable_in(hdspm, i, 1);
5585
5586 hdspm->capture_buffer =
0dca1793 5587 (unsigned char *) substream->runtime->dma_area;
54bf5dd9 5588 snd_printdd("Allocated sample buffer for capture at %p\n",
3cee5a60 5589 hdspm->capture_buffer);
763f356c 5590 }
0dca1793 5591
3cee5a60
RB
5592 /*
5593 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5594 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5595 "playback" : "capture",
77a23f26 5596 snd_pcm_sgbuf_get_addr(substream, 0));
0dca1793 5597 */
ffb2c3c0 5598 /*
0dca1793
AK
5599 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5600 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5601 "playback" : "capture",
5602 params_rate(params), params_channels(params),
5603 params_buffer_size(params));
5604 */
5605
5606
3ac9b0ac
AK
5607 /* For AES cards, the float format bit is the same as the
5608 * preferred sync reference. Since we don't want to break
5609 * sync settings, we have to skip the remaining part of this
5610 * function.
5611 */
5612 if (hdspm->io_type == AES32) {
5613 return 0;
5614 }
5615
5616
0dca1793
AK
5617 /* Switch to native float format if requested */
5618 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5619 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5620 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5621
5622 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5623 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5624 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5625 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5626
5627 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5628 }
5629 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5630
763f356c
TI
5631 return 0;
5632}
5633
98274f07 5634static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
763f356c
TI
5635{
5636 int i;
98274f07 5637 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5638
5639 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5640
0dca1793 5641 /* params_channels(params) should be enough,
763f356c 5642 but to get sure in case of error */
0dca1793 5643 for (i = 0; i < hdspm->max_channels_out; ++i)
763f356c
TI
5644 snd_hdspm_enable_out(hdspm, i, 0);
5645
5646 hdspm->playback_buffer = NULL;
5647 } else {
0dca1793 5648 for (i = 0; i < hdspm->max_channels_in; ++i)
763f356c
TI
5649 snd_hdspm_enable_in(hdspm, i, 0);
5650
5651 hdspm->capture_buffer = NULL;
5652
5653 }
5654
5655 snd_pcm_lib_free_pages(substream);
5656
5657 return 0;
5658}
5659
0dca1793 5660
98274f07 5661static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
0dca1793 5662 struct snd_pcm_channel_info *info)
763f356c 5663{
98274f07 5664 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c 5665
0dca1793
AK
5666 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5667 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5668 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5669 return -EINVAL;
5670 }
763f356c 5671
0dca1793
AK
5672 if (hdspm->channel_map_out[info->channel] < 0) {
5673 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5674 return -EINVAL;
5675 }
5676
5677 info->offset = hdspm->channel_map_out[info->channel] *
5678 HDSPM_CHANNEL_BUFFER_BYTES;
5679 } else {
5680 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5681 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5682 return -EINVAL;
5683 }
5684
5685 if (hdspm->channel_map_in[info->channel] < 0) {
5686 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5687 return -EINVAL;
5688 }
5689
5690 info->offset = hdspm->channel_map_in[info->channel] *
5691 HDSPM_CHANNEL_BUFFER_BYTES;
5692 }
763f356c 5693
763f356c
TI
5694 info->first = 0;
5695 info->step = 32;
5696 return 0;
5697}
5698
0dca1793 5699
98274f07 5700static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
0dca1793 5701 unsigned int cmd, void *arg)
763f356c
TI
5702{
5703 switch (cmd) {
5704 case SNDRV_PCM_IOCTL1_RESET:
ef5fa1a4 5705 return snd_hdspm_reset(substream);
763f356c
TI
5706
5707 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
0dca1793
AK
5708 {
5709 struct snd_pcm_channel_info *info = arg;
5710 return snd_hdspm_channel_info(substream, info);
5711 }
763f356c
TI
5712 default:
5713 break;
5714 }
5715
5716 return snd_pcm_lib_ioctl(substream, cmd, arg);
5717}
5718
98274f07 5719static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
763f356c 5720{
98274f07
TI
5721 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5722 struct snd_pcm_substream *other;
763f356c
TI
5723 int running;
5724
5725 spin_lock(&hdspm->lock);
5726 running = hdspm->running;
5727 switch (cmd) {
5728 case SNDRV_PCM_TRIGGER_START:
5729 running |= 1 << substream->stream;
5730 break;
5731 case SNDRV_PCM_TRIGGER_STOP:
5732 running &= ~(1 << substream->stream);
5733 break;
5734 default:
5735 snd_BUG();
5736 spin_unlock(&hdspm->lock);
5737 return -EINVAL;
5738 }
5739 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5740 other = hdspm->capture_substream;
5741 else
5742 other = hdspm->playback_substream;
5743
5744 if (other) {
98274f07 5745 struct snd_pcm_substream *s;
ef991b95 5746 snd_pcm_group_for_each_entry(s, substream) {
763f356c
TI
5747 if (s == other) {
5748 snd_pcm_trigger_done(s, substream);
5749 if (cmd == SNDRV_PCM_TRIGGER_START)
5750 running |= 1 << s->stream;
5751 else
5752 running &= ~(1 << s->stream);
5753 goto _ok;
5754 }
5755 }
5756 if (cmd == SNDRV_PCM_TRIGGER_START) {
5757 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
0dca1793
AK
5758 && substream->stream ==
5759 SNDRV_PCM_STREAM_CAPTURE)
763f356c
TI
5760 hdspm_silence_playback(hdspm);
5761 } else {
5762 if (running &&
0dca1793 5763 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
763f356c
TI
5764 hdspm_silence_playback(hdspm);
5765 }
5766 } else {
5767 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5768 hdspm_silence_playback(hdspm);
5769 }
0dca1793 5770_ok:
763f356c
TI
5771 snd_pcm_trigger_done(substream, substream);
5772 if (!hdspm->running && running)
5773 hdspm_start_audio(hdspm);
5774 else if (hdspm->running && !running)
5775 hdspm_stop_audio(hdspm);
5776 hdspm->running = running;
5777 spin_unlock(&hdspm->lock);
5778
5779 return 0;
5780}
5781
98274f07 5782static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
763f356c
TI
5783{
5784 return 0;
5785}
5786
98274f07 5787static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
763f356c
TI
5788 .info = (SNDRV_PCM_INFO_MMAP |
5789 SNDRV_PCM_INFO_MMAP_VALID |
5790 SNDRV_PCM_INFO_NONINTERLEAVED |
5791 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5792 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5793 .rates = (SNDRV_PCM_RATE_32000 |
5794 SNDRV_PCM_RATE_44100 |
5795 SNDRV_PCM_RATE_48000 |
5796 SNDRV_PCM_RATE_64000 |
3cee5a60
RB
5797 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5798 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
763f356c 5799 .rate_min = 32000,
3cee5a60 5800 .rate_max = 192000,
763f356c
TI
5801 .channels_min = 1,
5802 .channels_max = HDSPM_MAX_CHANNELS,
5803 .buffer_bytes_max =
5804 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
1b6fa108 5805 .period_bytes_min = (32 * 4),
52e6fb48 5806 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
763f356c 5807 .periods_min = 2,
0dca1793 5808 .periods_max = 512,
763f356c
TI
5809 .fifo_size = 0
5810};
5811
98274f07 5812static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
763f356c
TI
5813 .info = (SNDRV_PCM_INFO_MMAP |
5814 SNDRV_PCM_INFO_MMAP_VALID |
5815 SNDRV_PCM_INFO_NONINTERLEAVED |
5816 SNDRV_PCM_INFO_SYNC_START),
5817 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5818 .rates = (SNDRV_PCM_RATE_32000 |
5819 SNDRV_PCM_RATE_44100 |
5820 SNDRV_PCM_RATE_48000 |
5821 SNDRV_PCM_RATE_64000 |
3cee5a60
RB
5822 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5823 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
763f356c 5824 .rate_min = 32000,
3cee5a60 5825 .rate_max = 192000,
763f356c
TI
5826 .channels_min = 1,
5827 .channels_max = HDSPM_MAX_CHANNELS,
5828 .buffer_bytes_max =
5829 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
1b6fa108 5830 .period_bytes_min = (32 * 4),
52e6fb48 5831 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
763f356c 5832 .periods_min = 2,
0dca1793 5833 .periods_max = 512,
763f356c
TI
5834 .fifo_size = 0
5835};
5836
0dca1793
AK
5837static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5838 struct snd_pcm_hw_rule *rule)
5839{
5840 struct hdspm *hdspm = rule->private;
5841 struct snd_interval *c =
5842 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5843 struct snd_interval *r =
5844 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5845
5846 if (r->min > 96000 && r->max <= 192000) {
5847 struct snd_interval t = {
5848 .min = hdspm->qs_in_channels,
5849 .max = hdspm->qs_in_channels,
5850 .integer = 1,
5851 };
5852 return snd_interval_refine(c, &t);
5853 } else if (r->min > 48000 && r->max <= 96000) {
5854 struct snd_interval t = {
5855 .min = hdspm->ds_in_channels,
5856 .max = hdspm->ds_in_channels,
5857 .integer = 1,
5858 };
5859 return snd_interval_refine(c, &t);
5860 } else if (r->max < 64000) {
5861 struct snd_interval t = {
5862 .min = hdspm->ss_in_channels,
5863 .max = hdspm->ss_in_channels,
5864 .integer = 1,
5865 };
5866 return snd_interval_refine(c, &t);
5867 }
5868
5869 return 0;
5870}
763f356c 5871
0dca1793 5872static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
98274f07 5873 struct snd_pcm_hw_rule * rule)
763f356c 5874{
98274f07
TI
5875 struct hdspm *hdspm = rule->private;
5876 struct snd_interval *c =
763f356c 5877 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
98274f07 5878 struct snd_interval *r =
763f356c
TI
5879 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5880
0dca1793
AK
5881 if (r->min > 96000 && r->max <= 192000) {
5882 struct snd_interval t = {
5883 .min = hdspm->qs_out_channels,
5884 .max = hdspm->qs_out_channels,
5885 .integer = 1,
5886 };
5887 return snd_interval_refine(c, &t);
5888 } else if (r->min > 48000 && r->max <= 96000) {
98274f07 5889 struct snd_interval t = {
0dca1793
AK
5890 .min = hdspm->ds_out_channels,
5891 .max = hdspm->ds_out_channels,
763f356c
TI
5892 .integer = 1,
5893 };
5894 return snd_interval_refine(c, &t);
5895 } else if (r->max < 64000) {
98274f07 5896 struct snd_interval t = {
0dca1793
AK
5897 .min = hdspm->ss_out_channels,
5898 .max = hdspm->ss_out_channels,
763f356c
TI
5899 .integer = 1,
5900 };
5901 return snd_interval_refine(c, &t);
0dca1793 5902 } else {
763f356c
TI
5903 }
5904 return 0;
5905}
5906
0dca1793 5907static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
98274f07 5908 struct snd_pcm_hw_rule * rule)
763f356c 5909{
98274f07
TI
5910 struct hdspm *hdspm = rule->private;
5911 struct snd_interval *c =
763f356c 5912 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
98274f07 5913 struct snd_interval *r =
763f356c
TI
5914 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5915
0dca1793 5916 if (c->min >= hdspm->ss_in_channels) {
98274f07 5917 struct snd_interval t = {
763f356c
TI
5918 .min = 32000,
5919 .max = 48000,
5920 .integer = 1,
5921 };
5922 return snd_interval_refine(r, &t);
0dca1793
AK
5923 } else if (c->max <= hdspm->qs_in_channels) {
5924 struct snd_interval t = {
5925 .min = 128000,
5926 .max = 192000,
5927 .integer = 1,
5928 };
5929 return snd_interval_refine(r, &t);
5930 } else if (c->max <= hdspm->ds_in_channels) {
98274f07 5931 struct snd_interval t = {
763f356c
TI
5932 .min = 64000,
5933 .max = 96000,
5934 .integer = 1,
5935 };
0dca1793
AK
5936 return snd_interval_refine(r, &t);
5937 }
5938
5939 return 0;
5940}
5941static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5942 struct snd_pcm_hw_rule *rule)
5943{
5944 struct hdspm *hdspm = rule->private;
5945 struct snd_interval *c =
5946 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5947 struct snd_interval *r =
5948 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
763f356c 5949
0dca1793
AK
5950 if (c->min >= hdspm->ss_out_channels) {
5951 struct snd_interval t = {
5952 .min = 32000,
5953 .max = 48000,
5954 .integer = 1,
5955 };
5956 return snd_interval_refine(r, &t);
5957 } else if (c->max <= hdspm->qs_out_channels) {
5958 struct snd_interval t = {
5959 .min = 128000,
5960 .max = 192000,
5961 .integer = 1,
5962 };
5963 return snd_interval_refine(r, &t);
5964 } else if (c->max <= hdspm->ds_out_channels) {
5965 struct snd_interval t = {
5966 .min = 64000,
5967 .max = 96000,
5968 .integer = 1,
5969 };
763f356c
TI
5970 return snd_interval_refine(r, &t);
5971 }
0dca1793 5972
763f356c
TI
5973 return 0;
5974}
5975
0dca1793 5976static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
ffb2c3c0
RB
5977 struct snd_pcm_hw_rule *rule)
5978{
5979 unsigned int list[3];
5980 struct hdspm *hdspm = rule->private;
5981 struct snd_interval *c = hw_param_interval(params,
5982 SNDRV_PCM_HW_PARAM_CHANNELS);
0dca1793
AK
5983
5984 list[0] = hdspm->qs_in_channels;
5985 list[1] = hdspm->ds_in_channels;
5986 list[2] = hdspm->ss_in_channels;
5987 return snd_interval_list(c, 3, list, 0);
5988}
5989
5990static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5991 struct snd_pcm_hw_rule *rule)
5992{
5993 unsigned int list[3];
5994 struct hdspm *hdspm = rule->private;
5995 struct snd_interval *c = hw_param_interval(params,
5996 SNDRV_PCM_HW_PARAM_CHANNELS);
5997
5998 list[0] = hdspm->qs_out_channels;
5999 list[1] = hdspm->ds_out_channels;
6000 list[2] = hdspm->ss_out_channels;
6001 return snd_interval_list(c, 3, list, 0);
ffb2c3c0
RB
6002}
6003
6004
ef5fa1a4
TI
6005static unsigned int hdspm_aes32_sample_rates[] = {
6006 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
6007};
ffb2c3c0 6008
ef5fa1a4
TI
6009static struct snd_pcm_hw_constraint_list
6010hdspm_hw_constraints_aes32_sample_rates = {
ffb2c3c0
RB
6011 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
6012 .list = hdspm_aes32_sample_rates,
6013 .mask = 0
6014};
6015
98274f07 6016static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
763f356c 6017{
98274f07
TI
6018 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6019 struct snd_pcm_runtime *runtime = substream->runtime;
763f356c 6020
763f356c
TI
6021 spin_lock_irq(&hdspm->lock);
6022
6023 snd_pcm_set_sync(substream);
6024
0dca1793 6025
763f356c
TI
6026 runtime->hw = snd_hdspm_playback_subinfo;
6027
6028 if (hdspm->capture_substream == NULL)
6029 hdspm_stop_audio(hdspm);
6030
6031 hdspm->playback_pid = current->pid;
6032 hdspm->playback_substream = substream;
6033
6034 spin_unlock_irq(&hdspm->lock);
6035
6036 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
d877681d 6037 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c 6038
0dca1793
AK
6039 switch (hdspm->io_type) {
6040 case AIO:
6041 case RayDAT:
d877681d
TI
6042 snd_pcm_hw_constraint_minmax(runtime,
6043 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6044 32, 4096);
6045 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
6046 snd_pcm_hw_constraint_minmax(runtime,
6047 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6048 16384, 16384);
0dca1793
AK
6049 break;
6050
6051 default:
d877681d
TI
6052 snd_pcm_hw_constraint_minmax(runtime,
6053 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6054 64, 8192);
6055 break;
0dca1793 6056 }
763f356c 6057
0dca1793 6058 if (AES32 == hdspm->io_type) {
3fa9e3d2 6059 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
ffb2c3c0
RB
6060 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6061 &hdspm_hw_constraints_aes32_sample_rates);
6062 } else {
ffb2c3c0 6063 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0dca1793
AK
6064 snd_hdspm_hw_rule_rate_out_channels, hdspm,
6065 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
ffb2c3c0 6066 }
88fabbfc
AK
6067
6068 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6069 snd_hdspm_hw_rule_out_channels, hdspm,
6070 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6071
6072 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6073 snd_hdspm_hw_rule_out_channels_rate, hdspm,
6074 SNDRV_PCM_HW_PARAM_RATE, -1);
6075
763f356c
TI
6076 return 0;
6077}
6078
98274f07 6079static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
763f356c 6080{
98274f07 6081 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
6082
6083 spin_lock_irq(&hdspm->lock);
6084
6085 hdspm->playback_pid = -1;
6086 hdspm->playback_substream = NULL;
6087
6088 spin_unlock_irq(&hdspm->lock);
6089
6090 return 0;
6091}
6092
6093
98274f07 6094static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
763f356c 6095{
98274f07
TI
6096 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6097 struct snd_pcm_runtime *runtime = substream->runtime;
763f356c
TI
6098
6099 spin_lock_irq(&hdspm->lock);
6100 snd_pcm_set_sync(substream);
6101 runtime->hw = snd_hdspm_capture_subinfo;
6102
6103 if (hdspm->playback_substream == NULL)
6104 hdspm_stop_audio(hdspm);
6105
6106 hdspm->capture_pid = current->pid;
6107 hdspm->capture_substream = substream;
6108
6109 spin_unlock_irq(&hdspm->lock);
6110
6111 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
d877681d
TI
6112 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
6113
0dca1793
AK
6114 switch (hdspm->io_type) {
6115 case AIO:
6116 case RayDAT:
d877681d
TI
6117 snd_pcm_hw_constraint_minmax(runtime,
6118 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6119 32, 4096);
6120 snd_pcm_hw_constraint_minmax(runtime,
6121 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6122 16384, 16384);
6123 break;
0dca1793
AK
6124
6125 default:
d877681d
TI
6126 snd_pcm_hw_constraint_minmax(runtime,
6127 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6128 64, 8192);
6129 break;
0dca1793
AK
6130 }
6131
6132 if (AES32 == hdspm->io_type) {
3fa9e3d2 6133 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
ffb2c3c0
RB
6134 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6135 &hdspm_hw_constraints_aes32_sample_rates);
6136 } else {
ffb2c3c0 6137 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
88fabbfc
AK
6138 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6139 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
ffb2c3c0 6140 }
88fabbfc
AK
6141
6142 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6143 snd_hdspm_hw_rule_in_channels, hdspm,
6144 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6145
6146 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6147 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6148 SNDRV_PCM_HW_PARAM_RATE, -1);
6149
763f356c
TI
6150 return 0;
6151}
6152
98274f07 6153static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
763f356c 6154{
98274f07 6155 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
6156
6157 spin_lock_irq(&hdspm->lock);
6158
6159 hdspm->capture_pid = -1;
6160 hdspm->capture_substream = NULL;
6161
6162 spin_unlock_irq(&hdspm->lock);
6163 return 0;
6164}
6165
0dca1793
AK
6166static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
6167{
6168 /* we have nothing to initialize but the call is required */
6169 return 0;
6170}
6171
6172static inline int copy_u32_le(void __user *dest, void __iomem *src)
6173{
6174 u32 val = readl(src);
6175 return copy_to_user(dest, &val, 4);
6176}
6177
6178static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
2ca595ab 6179 unsigned int cmd, unsigned long arg)
763f356c 6180{
0dca1793 6181 void __user *argp = (void __user *)arg;
ef5fa1a4 6182 struct hdspm *hdspm = hw->private_data;
98274f07 6183 struct hdspm_mixer_ioctl mixer;
0dca1793
AK
6184 struct hdspm_config info;
6185 struct hdspm_status status;
98274f07 6186 struct hdspm_version hdspm_version;
730a5865 6187 struct hdspm_peak_rms *levels;
0dca1793
AK
6188 struct hdspm_ltc ltc;
6189 unsigned int statusregister;
6190 long unsigned int s;
6191 int i = 0;
763f356c
TI
6192
6193 switch (cmd) {
6194
763f356c 6195 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
730a5865 6196 levels = &hdspm->peak_rms;
0dca1793 6197 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
730a5865 6198 levels->input_peaks[i] =
0dca1793
AK
6199 readl(hdspm->iobase +
6200 HDSPM_MADI_INPUT_PEAK + i*4);
730a5865 6201 levels->playback_peaks[i] =
0dca1793
AK
6202 readl(hdspm->iobase +
6203 HDSPM_MADI_PLAYBACK_PEAK + i*4);
730a5865 6204 levels->output_peaks[i] =
0dca1793
AK
6205 readl(hdspm->iobase +
6206 HDSPM_MADI_OUTPUT_PEAK + i*4);
6207
730a5865 6208 levels->input_rms[i] =
0dca1793
AK
6209 ((uint64_t) readl(hdspm->iobase +
6210 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6211 (uint64_t) readl(hdspm->iobase +
6212 HDSPM_MADI_INPUT_RMS_L + i*4);
730a5865 6213 levels->playback_rms[i] =
0dca1793
AK
6214 ((uint64_t)readl(hdspm->iobase +
6215 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6216 (uint64_t)readl(hdspm->iobase +
6217 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
730a5865 6218 levels->output_rms[i] =
0dca1793
AK
6219 ((uint64_t)readl(hdspm->iobase +
6220 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6221 (uint64_t)readl(hdspm->iobase +
6222 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6223 }
6224
6225 if (hdspm->system_sample_rate > 96000) {
730a5865 6226 levels->speed = qs;
0dca1793 6227 } else if (hdspm->system_sample_rate > 48000) {
730a5865 6228 levels->speed = ds;
0dca1793 6229 } else {
730a5865 6230 levels->speed = ss;
0dca1793 6231 }
730a5865 6232 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
0dca1793 6233
730a5865 6234 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
0dca1793
AK
6235 if (0 != s) {
6236 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6237 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6238 */
763f356c 6239 return -EFAULT;
0dca1793
AK
6240 }
6241 break;
6242
6243 case SNDRV_HDSPM_IOCTL_GET_LTC:
6244 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6245 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6246 if (i & HDSPM_TCO1_LTC_Input_valid) {
6247 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6248 HDSPM_TCO1_LTC_Format_MSB)) {
6249 case 0:
6250 ltc.format = fps_24;
6251 break;
6252 case HDSPM_TCO1_LTC_Format_LSB:
6253 ltc.format = fps_25;
6254 break;
6255 case HDSPM_TCO1_LTC_Format_MSB:
6256 ltc.format = fps_2997;
6257 break;
6258 default:
6259 ltc.format = 30;
6260 break;
6261 }
6262 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6263 ltc.frame = drop_frame;
6264 } else {
6265 ltc.frame = full_frame;
6266 }
6267 } else {
6268 ltc.format = format_invalid;
6269 ltc.frame = frame_invalid;
6270 }
6271 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6272 ltc.input_format = ntsc;
6273 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6274 ltc.input_format = pal;
6275 } else {
6276 ltc.input_format = no_video;
6277 }
6278
6279 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6280 if (0 != s) {
6281 /*
6282 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
763f356c 6283 return -EFAULT;
0dca1793 6284 }
763f356c
TI
6285
6286 break;
763f356c 6287
0dca1793 6288 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
763f356c 6289
4ab69a2b 6290 memset(&info, 0, sizeof(info));
763f356c 6291 spin_lock_irq(&hdspm->lock);
ef5fa1a4
TI
6292 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6293 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
763f356c
TI
6294
6295 info.system_sample_rate = hdspm->system_sample_rate;
6296 info.autosync_sample_rate =
0dca1793 6297 hdspm_external_sample_rate(hdspm);
ef5fa1a4
TI
6298 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6299 info.clock_source = hdspm_clock_source(hdspm);
6300 info.autosync_ref = hdspm_autosync_ref(hdspm);
c9e1668c 6301 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
763f356c
TI
6302 info.passthru = 0;
6303 spin_unlock_irq(&hdspm->lock);
2ca595ab 6304 if (copy_to_user(argp, &info, sizeof(info)))
763f356c
TI
6305 return -EFAULT;
6306 break;
6307
0dca1793 6308 case SNDRV_HDSPM_IOCTL_GET_STATUS:
643d6bbb
DC
6309 memset(&status, 0, sizeof(status));
6310
0dca1793
AK
6311 status.card_type = hdspm->io_type;
6312
6313 status.autosync_source = hdspm_autosync_ref(hdspm);
6314
6315 status.card_clock = 110069313433624ULL;
6316 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6317
6318 switch (hdspm->io_type) {
6319 case MADI:
6320 case MADIface:
6321 status.card_specific.madi.sync_wc =
6322 hdspm_wc_sync_check(hdspm);
6323 status.card_specific.madi.sync_madi =
6324 hdspm_madi_sync_check(hdspm);
6325 status.card_specific.madi.sync_tco =
6326 hdspm_tco_sync_check(hdspm);
6327 status.card_specific.madi.sync_in =
6328 hdspm_sync_in_sync_check(hdspm);
6329
6330 statusregister =
6331 hdspm_read(hdspm, HDSPM_statusRegister);
6332 status.card_specific.madi.madi_input =
6333 (statusregister & HDSPM_AB_int) ? 1 : 0;
6334 status.card_specific.madi.channel_format =
9e6ff520 6335 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
0dca1793
AK
6336 /* TODO: Mac driver sets it when f_s>48kHz */
6337 status.card_specific.madi.frame_format = 0;
6338
6339 default:
6340 break;
6341 }
6342
2ca595ab 6343 if (copy_to_user(argp, &status, sizeof(status)))
0dca1793
AK
6344 return -EFAULT;
6345
6346
6347 break;
6348
763f356c 6349 case SNDRV_HDSPM_IOCTL_GET_VERSION:
643d6bbb
DC
6350 memset(&hdspm_version, 0, sizeof(hdspm_version));
6351
0dca1793
AK
6352 hdspm_version.card_type = hdspm->io_type;
6353 strncpy(hdspm_version.cardname, hdspm->card_name,
6354 sizeof(hdspm_version.cardname));
7d53a631 6355 hdspm_version.serial = hdspm->serial;
763f356c 6356 hdspm_version.firmware_rev = hdspm->firmware_rev;
0dca1793
AK
6357 hdspm_version.addons = 0;
6358 if (hdspm->tco)
6359 hdspm_version.addons |= HDSPM_ADDON_TCO;
6360
2ca595ab 6361 if (copy_to_user(argp, &hdspm_version,
0dca1793 6362 sizeof(hdspm_version)))
763f356c
TI
6363 return -EFAULT;
6364 break;
6365
6366 case SNDRV_HDSPM_IOCTL_GET_MIXER:
2ca595ab 6367 if (copy_from_user(&mixer, argp, sizeof(mixer)))
763f356c 6368 return -EFAULT;
ef5fa1a4 6369 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
0dca1793 6370 sizeof(struct hdspm_mixer)))
763f356c
TI
6371 return -EFAULT;
6372 break;
6373
6374 default:
6375 return -EINVAL;
6376 }
6377 return 0;
6378}
6379
98274f07 6380static struct snd_pcm_ops snd_hdspm_playback_ops = {
763f356c
TI
6381 .open = snd_hdspm_playback_open,
6382 .close = snd_hdspm_playback_release,
6383 .ioctl = snd_hdspm_ioctl,
6384 .hw_params = snd_hdspm_hw_params,
6385 .hw_free = snd_hdspm_hw_free,
6386 .prepare = snd_hdspm_prepare,
6387 .trigger = snd_hdspm_trigger,
6388 .pointer = snd_hdspm_hw_pointer,
763f356c
TI
6389 .page = snd_pcm_sgbuf_ops_page,
6390};
6391
98274f07 6392static struct snd_pcm_ops snd_hdspm_capture_ops = {
763f356c
TI
6393 .open = snd_hdspm_capture_open,
6394 .close = snd_hdspm_capture_release,
6395 .ioctl = snd_hdspm_ioctl,
6396 .hw_params = snd_hdspm_hw_params,
6397 .hw_free = snd_hdspm_hw_free,
6398 .prepare = snd_hdspm_prepare,
6399 .trigger = snd_hdspm_trigger,
6400 .pointer = snd_hdspm_hw_pointer,
763f356c
TI
6401 .page = snd_pcm_sgbuf_ops_page,
6402};
6403
e23e7a14
BP
6404static int snd_hdspm_create_hwdep(struct snd_card *card,
6405 struct hdspm *hdspm)
763f356c 6406{
98274f07 6407 struct snd_hwdep *hw;
763f356c
TI
6408 int err;
6409
ef5fa1a4
TI
6410 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6411 if (err < 0)
763f356c
TI
6412 return err;
6413
6414 hdspm->hwdep = hw;
6415 hw->private_data = hdspm;
6416 strcpy(hw->name, "HDSPM hwdep interface");
6417
0dca1793 6418 hw->ops.open = snd_hdspm_hwdep_dummy_op;
763f356c 6419 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
8de5d6f1 6420 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
0dca1793 6421 hw->ops.release = snd_hdspm_hwdep_dummy_op;
763f356c
TI
6422
6423 return 0;
6424}
6425
6426
6427/*------------------------------------------------------------
0dca1793 6428 memory interface
763f356c 6429 ------------------------------------------------------------*/
e23e7a14 6430static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
763f356c
TI
6431{
6432 int err;
98274f07 6433 struct snd_pcm *pcm;
763f356c
TI
6434 size_t wanted;
6435
6436 pcm = hdspm->pcm;
6437
3cee5a60 6438 wanted = HDSPM_DMA_AREA_BYTES;
763f356c 6439
ef5fa1a4 6440 err =
763f356c 6441 snd_pcm_lib_preallocate_pages_for_all(pcm,
0dca1793 6442 SNDRV_DMA_TYPE_DEV_SG,
763f356c
TI
6443 snd_dma_pci_data(hdspm->pci),
6444 wanted,
ef5fa1a4
TI
6445 wanted);
6446 if (err < 0) {
e2eba3e7 6447 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
763f356c
TI
6448
6449 return err;
6450 } else
e2eba3e7 6451 snd_printdd(" Preallocated %zd Bytes\n", wanted);
763f356c
TI
6452
6453 return 0;
6454}
6455
0dca1793
AK
6456
6457static void hdspm_set_sgbuf(struct hdspm *hdspm,
77a23f26 6458 struct snd_pcm_substream *substream,
763f356c
TI
6459 unsigned int reg, int channels)
6460{
6461 int i;
0dca1793
AK
6462
6463 /* continuous memory segment */
763f356c
TI
6464 for (i = 0; i < (channels * 16); i++)
6465 hdspm_write(hdspm, reg + 4 * i,
0dca1793 6466 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
763f356c
TI
6467}
6468
0dca1793 6469
763f356c 6470/* ------------- ALSA Devices ---------------------------- */
e23e7a14
BP
6471static int snd_hdspm_create_pcm(struct snd_card *card,
6472 struct hdspm *hdspm)
763f356c 6473{
98274f07 6474 struct snd_pcm *pcm;
763f356c
TI
6475 int err;
6476
ef5fa1a4
TI
6477 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6478 if (err < 0)
763f356c
TI
6479 return err;
6480
6481 hdspm->pcm = pcm;
6482 pcm->private_data = hdspm;
6483 strcpy(pcm->name, hdspm->card_name);
6484
6485 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6486 &snd_hdspm_playback_ops);
6487 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6488 &snd_hdspm_capture_ops);
6489
6490 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6491
ef5fa1a4
TI
6492 err = snd_hdspm_preallocate_memory(hdspm);
6493 if (err < 0)
763f356c
TI
6494 return err;
6495
6496 return 0;
6497}
6498
98274f07 6499static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
763f356c 6500{
7c7102b7
AK
6501 int i;
6502
6503 for (i = 0; i < hdspm->midiPorts; i++)
6504 snd_hdspm_flush_midi_input(hdspm, i);
763f356c
TI
6505}
6506
e23e7a14
BP
6507static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6508 struct hdspm *hdspm)
763f356c 6509{
0dca1793 6510 int err, i;
763f356c
TI
6511
6512 snd_printdd("Create card...\n");
ef5fa1a4
TI
6513 err = snd_hdspm_create_pcm(card, hdspm);
6514 if (err < 0)
763f356c
TI
6515 return err;
6516
0dca1793
AK
6517 i = 0;
6518 while (i < hdspm->midiPorts) {
6519 err = snd_hdspm_create_midi(card, hdspm, i);
6520 if (err < 0) {
6521 return err;
6522 }
6523 i++;
6524 }
763f356c 6525
ef5fa1a4
TI
6526 err = snd_hdspm_create_controls(card, hdspm);
6527 if (err < 0)
763f356c
TI
6528 return err;
6529
ef5fa1a4
TI
6530 err = snd_hdspm_create_hwdep(card, hdspm);
6531 if (err < 0)
763f356c
TI
6532 return err;
6533
6534 snd_printdd("proc init...\n");
6535 snd_hdspm_proc_init(hdspm);
6536
6537 hdspm->system_sample_rate = -1;
6538 hdspm->last_external_sample_rate = -1;
6539 hdspm->last_internal_sample_rate = -1;
6540 hdspm->playback_pid = -1;
6541 hdspm->capture_pid = -1;
6542 hdspm->capture_substream = NULL;
6543 hdspm->playback_substream = NULL;
6544
6545 snd_printdd("Set defaults...\n");
ef5fa1a4
TI
6546 err = snd_hdspm_set_defaults(hdspm);
6547 if (err < 0)
763f356c
TI
6548 return err;
6549
6550 snd_printdd("Update mixer controls...\n");
6551 hdspm_update_simple_mixer_controls(hdspm);
6552
6553 snd_printdd("Initializeing complete ???\n");
6554
ef5fa1a4
TI
6555 err = snd_card_register(card);
6556 if (err < 0) {
763f356c
TI
6557 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6558 return err;
6559 }
6560
6561 snd_printdd("... yes now\n");
6562
6563 return 0;
6564}
6565
e23e7a14
BP
6566static int snd_hdspm_create(struct snd_card *card,
6567 struct hdspm *hdspm)
6568{
0dca1793 6569
763f356c
TI
6570 struct pci_dev *pci = hdspm->pci;
6571 int err;
763f356c
TI
6572 unsigned long io_extent;
6573
6574 hdspm->irq = -1;
763f356c
TI
6575 hdspm->card = card;
6576
6577 spin_lock_init(&hdspm->lock);
6578
763f356c 6579 pci_read_config_word(hdspm->pci,
0dca1793 6580 PCI_CLASS_REVISION, &hdspm->firmware_rev);
3cee5a60 6581
763f356c 6582 strcpy(card->mixername, "Xilinx FPGA");
0dca1793
AK
6583 strcpy(card->driver, "HDSPM");
6584
6585 switch (hdspm->firmware_rev) {
0dca1793
AK
6586 case HDSPM_RAYDAT_REV:
6587 hdspm->io_type = RayDAT;
6588 hdspm->card_name = "RME RayDAT";
6589 hdspm->midiPorts = 2;
6590 break;
6591 case HDSPM_AIO_REV:
6592 hdspm->io_type = AIO;
6593 hdspm->card_name = "RME AIO";
6594 hdspm->midiPorts = 1;
6595 break;
6596 case HDSPM_MADIFACE_REV:
6597 hdspm->io_type = MADIface;
6598 hdspm->card_name = "RME MADIface";
6599 hdspm->midiPorts = 1;
6600 break;
5027f347 6601 default:
c09403dc
AK
6602 if ((hdspm->firmware_rev == 0xf0) ||
6603 ((hdspm->firmware_rev >= 0xe6) &&
6604 (hdspm->firmware_rev <= 0xea))) {
6605 hdspm->io_type = AES32;
6606 hdspm->card_name = "RME AES32";
6607 hdspm->midiPorts = 2;
05c7cc9c 6608 } else if ((hdspm->firmware_rev == 0xd2) ||
c09403dc
AK
6609 ((hdspm->firmware_rev >= 0xc8) &&
6610 (hdspm->firmware_rev <= 0xcf))) {
6611 hdspm->io_type = MADI;
6612 hdspm->card_name = "RME MADI";
6613 hdspm->midiPorts = 3;
6614 } else {
6615 snd_printk(KERN_ERR
6616 "HDSPM: unknown firmware revision %x\n",
5027f347 6617 hdspm->firmware_rev);
c09403dc
AK
6618 return -ENODEV;
6619 }
3cee5a60 6620 }
763f356c 6621
ef5fa1a4
TI
6622 err = pci_enable_device(pci);
6623 if (err < 0)
763f356c
TI
6624 return err;
6625
6626 pci_set_master(hdspm->pci);
6627
ef5fa1a4
TI
6628 err = pci_request_regions(pci, "hdspm");
6629 if (err < 0)
763f356c
TI
6630 return err;
6631
6632 hdspm->port = pci_resource_start(pci, 0);
6633 io_extent = pci_resource_len(pci, 0);
6634
6635 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
0dca1793 6636 hdspm->port, hdspm->port + io_extent - 1);
763f356c 6637
ef5fa1a4
TI
6638 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6639 if (!hdspm->iobase) {
6640 snd_printk(KERN_ERR "HDSPM: "
0dca1793
AK
6641 "unable to remap region 0x%lx-0x%lx\n",
6642 hdspm->port, hdspm->port + io_extent - 1);
763f356c
TI
6643 return -EBUSY;
6644 }
6645 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
0dca1793
AK
6646 (unsigned long)hdspm->iobase, hdspm->port,
6647 hdspm->port + io_extent - 1);
763f356c
TI
6648
6649 if (request_irq(pci->irq, snd_hdspm_interrupt,
934c2b6d 6650 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
763f356c
TI
6651 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6652 return -EBUSY;
6653 }
6654
6655 snd_printdd("use IRQ %d\n", pci->irq);
6656
6657 hdspm->irq = pci->irq;
763f356c 6658
e2eba3e7 6659 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
0dca1793 6660 sizeof(struct hdspm_mixer));
ef5fa1a4
TI
6661 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6662 if (!hdspm->mixer) {
6663 snd_printk(KERN_ERR "HDSPM: "
0dca1793
AK
6664 "unable to kmalloc Mixer memory of %d Bytes\n",
6665 (int)sizeof(struct hdspm_mixer));
b17cbdd8 6666 return -ENOMEM;
763f356c
TI
6667 }
6668
0dca1793
AK
6669 hdspm->port_names_in = NULL;
6670 hdspm->port_names_out = NULL;
6671
6672 switch (hdspm->io_type) {
6673 case AES32:
d2d10a21
AK
6674 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6675 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6676 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
432d2500
AK
6677
6678 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6679 channel_map_aes32;
6680 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6681 channel_map_aes32;
6682 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6683 channel_map_aes32;
6684 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6685 texts_ports_aes32;
6686 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6687 texts_ports_aes32;
6688 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6689 texts_ports_aes32;
6690
d2d10a21
AK
6691 hdspm->max_channels_out = hdspm->max_channels_in =
6692 AES32_CHANNELS;
432d2500
AK
6693 hdspm->port_names_in = hdspm->port_names_out =
6694 texts_ports_aes32;
6695 hdspm->channel_map_in = hdspm->channel_map_out =
6696 channel_map_aes32;
6697
0dca1793
AK
6698 break;
6699
6700 case MADI:
6701 case MADIface:
6702 hdspm->ss_in_channels = hdspm->ss_out_channels =
6703 MADI_SS_CHANNELS;
6704 hdspm->ds_in_channels = hdspm->ds_out_channels =
6705 MADI_DS_CHANNELS;
6706 hdspm->qs_in_channels = hdspm->qs_out_channels =
6707 MADI_QS_CHANNELS;
6708
6709 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6710 channel_map_unity_ss;
01e96078 6711 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
0dca1793 6712 channel_map_unity_ss;
01e96078 6713 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
0dca1793
AK
6714 channel_map_unity_ss;
6715
6716 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6717 texts_ports_madi;
6718 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6719 texts_ports_madi;
6720 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6721 texts_ports_madi;
6722 break;
6723
6724 case AIO:
0dca1793
AK
6725 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6726 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6727 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6728 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6729 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6730 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6731
3de9db26
AK
6732 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6733 snd_printk(KERN_INFO "HDSPM: AEB input board found\n");
6734 hdspm->ss_in_channels += 4;
6735 hdspm->ds_in_channels += 4;
6736 hdspm->qs_in_channels += 4;
6737 }
6738
6739 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6740 snd_printk(KERN_INFO "HDSPM: AEB output board found\n");
6741 hdspm->ss_out_channels += 4;
6742 hdspm->ds_out_channels += 4;
6743 hdspm->qs_out_channels += 4;
6744 }
6745
0dca1793
AK
6746 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6747 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6748 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6749
6750 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6751 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6752 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6753
6754 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6755 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6756 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6757 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6758 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6759 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6760
6761 break;
6762
6763 case RayDAT:
6764 hdspm->ss_in_channels = hdspm->ss_out_channels =
6765 RAYDAT_SS_CHANNELS;
6766 hdspm->ds_in_channels = hdspm->ds_out_channels =
6767 RAYDAT_DS_CHANNELS;
6768 hdspm->qs_in_channels = hdspm->qs_out_channels =
6769 RAYDAT_QS_CHANNELS;
6770
6771 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6772 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6773
6774 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6775 channel_map_raydat_ss;
6776 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6777 channel_map_raydat_ds;
6778 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6779 channel_map_raydat_qs;
6780 hdspm->channel_map_in = hdspm->channel_map_out =
6781 channel_map_raydat_ss;
6782
6783 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6784 texts_ports_raydat_ss;
6785 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6786 texts_ports_raydat_ds;
6787 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6788 texts_ports_raydat_qs;
6789
6790
6791 break;
6792
6793 }
6794
6795 /* TCO detection */
6796 switch (hdspm->io_type) {
6797 case AIO:
6798 case RayDAT:
6799 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6800 HDSPM_s2_tco_detect) {
6801 hdspm->midiPorts++;
6802 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6803 GFP_KERNEL);
6804 if (NULL != hdspm->tco) {
6805 hdspm_tco_write(hdspm);
6806 }
6807 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6808 } else {
6809 hdspm->tco = NULL;
6810 }
6811 break;
6812
6813 case MADI:
0dc831b9 6814 case AES32:
0dca1793
AK
6815 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6816 hdspm->midiPorts++;
6817 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6818 GFP_KERNEL);
6819 if (NULL != hdspm->tco) {
6820 hdspm_tco_write(hdspm);
6821 }
e71b95ad 6822 snd_printk(KERN_INFO "HDSPM: MADI/AES TCO module found\n");
0dca1793
AK
6823 } else {
6824 hdspm->tco = NULL;
6825 }
6826 break;
6827
6828 default:
6829 hdspm->tco = NULL;
6830 }
6831
6832 /* texts */
6833 switch (hdspm->io_type) {
6834 case AES32:
6835 if (hdspm->tco) {
6836 hdspm->texts_autosync = texts_autosync_aes_tco;
e71b95ad
AK
6837 hdspm->texts_autosync_items =
6838 ARRAY_SIZE(texts_autosync_aes_tco);
0dca1793
AK
6839 } else {
6840 hdspm->texts_autosync = texts_autosync_aes;
e71b95ad
AK
6841 hdspm->texts_autosync_items =
6842 ARRAY_SIZE(texts_autosync_aes);
0dca1793
AK
6843 }
6844 break;
6845
6846 case MADI:
6847 if (hdspm->tco) {
6848 hdspm->texts_autosync = texts_autosync_madi_tco;
6849 hdspm->texts_autosync_items = 4;
6850 } else {
6851 hdspm->texts_autosync = texts_autosync_madi;
6852 hdspm->texts_autosync_items = 3;
6853 }
6854 break;
6855
6856 case MADIface:
6857
6858 break;
6859
6860 case RayDAT:
6861 if (hdspm->tco) {
6862 hdspm->texts_autosync = texts_autosync_raydat_tco;
6863 hdspm->texts_autosync_items = 9;
6864 } else {
6865 hdspm->texts_autosync = texts_autosync_raydat;
6866 hdspm->texts_autosync_items = 8;
6867 }
6868 break;
6869
6870 case AIO:
6871 if (hdspm->tco) {
6872 hdspm->texts_autosync = texts_autosync_aio_tco;
6873 hdspm->texts_autosync_items = 6;
6874 } else {
6875 hdspm->texts_autosync = texts_autosync_aio;
6876 hdspm->texts_autosync_items = 5;
6877 }
6878 break;
6879
6880 }
6881
6882 tasklet_init(&hdspm->midi_tasklet,
6883 hdspm_midi_tasklet, (unsigned long) hdspm);
763f356c 6884
f7de8ba3
AK
6885
6886 if (hdspm->io_type != MADIface) {
6887 hdspm->serial = (hdspm_read(hdspm,
6888 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6889 /* id contains either a user-provided value or the default
6890 * NULL. If it's the default, we're safe to
6891 * fill card->id with the serial number.
6892 *
6893 * If the serial number is 0xFFFFFF, then we're dealing with
6894 * an old PCI revision that comes without a sane number. In
6895 * this case, we don't set card->id to avoid collisions
6896 * when running with multiple cards.
6897 */
6898 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6899 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6900 snd_card_set_id(card, card->id);
6901 }
6902 }
6903
763f356c 6904 snd_printdd("create alsa devices.\n");
ef5fa1a4
TI
6905 err = snd_hdspm_create_alsa_devices(card, hdspm);
6906 if (err < 0)
763f356c
TI
6907 return err;
6908
6909 snd_hdspm_initialize_midi_flush(hdspm);
6910
6911 return 0;
6912}
6913
0dca1793 6914
98274f07 6915static int snd_hdspm_free(struct hdspm * hdspm)
763f356c
TI
6916{
6917
6918 if (hdspm->port) {
6919
6920 /* stop th audio, and cancel all interrupts */
6921 hdspm->control_register &=
ef5fa1a4 6922 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
0dca1793
AK
6923 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6924 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
763f356c
TI
6925 hdspm_write(hdspm, HDSPM_controlRegister,
6926 hdspm->control_register);
6927 }
6928
6929 if (hdspm->irq >= 0)
6930 free_irq(hdspm->irq, (void *) hdspm);
6931
fc58422a 6932 kfree(hdspm->mixer);
763f356c
TI
6933
6934 if (hdspm->iobase)
6935 iounmap(hdspm->iobase);
6936
763f356c
TI
6937 if (hdspm->port)
6938 pci_release_regions(hdspm->pci);
6939
6940 pci_disable_device(hdspm->pci);
6941 return 0;
6942}
6943
0dca1793 6944
98274f07 6945static void snd_hdspm_card_free(struct snd_card *card)
763f356c 6946{
ef5fa1a4 6947 struct hdspm *hdspm = card->private_data;
763f356c
TI
6948
6949 if (hdspm)
6950 snd_hdspm_free(hdspm);
6951}
6952
0dca1793 6953
e23e7a14
BP
6954static int snd_hdspm_probe(struct pci_dev *pci,
6955 const struct pci_device_id *pci_id)
763f356c
TI
6956{
6957 static int dev;
98274f07
TI
6958 struct hdspm *hdspm;
6959 struct snd_card *card;
763f356c
TI
6960 int err;
6961
6962 if (dev >= SNDRV_CARDS)
6963 return -ENODEV;
6964 if (!enable[dev]) {
6965 dev++;
6966 return -ENOENT;
6967 }
6968
e58de7ba 6969 err = snd_card_create(index[dev], id[dev],
0dca1793 6970 THIS_MODULE, sizeof(struct hdspm), &card);
e58de7ba
TI
6971 if (err < 0)
6972 return err;
763f356c 6973
ef5fa1a4 6974 hdspm = card->private_data;
763f356c
TI
6975 card->private_free = snd_hdspm_card_free;
6976 hdspm->dev = dev;
6977 hdspm->pci = pci;
6978
c187c041
TI
6979 snd_card_set_dev(card, &pci->dev);
6980
0dca1793 6981 err = snd_hdspm_create(card, hdspm);
ef5fa1a4 6982 if (err < 0) {
763f356c
TI
6983 snd_card_free(card);
6984 return err;
6985 }
6986
0dca1793
AK
6987 if (hdspm->io_type != MADIface) {
6988 sprintf(card->shortname, "%s_%x",
6989 hdspm->card_name,
7d53a631 6990 hdspm->serial);
0dca1793
AK
6991 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6992 hdspm->card_name,
7d53a631 6993 hdspm->serial,
0dca1793
AK
6994 hdspm->port, hdspm->irq);
6995 } else {
6996 sprintf(card->shortname, "%s", hdspm->card_name);
6997 sprintf(card->longname, "%s at 0x%lx, irq %d",
6998 hdspm->card_name, hdspm->port, hdspm->irq);
6999 }
763f356c 7000
ef5fa1a4
TI
7001 err = snd_card_register(card);
7002 if (err < 0) {
763f356c
TI
7003 snd_card_free(card);
7004 return err;
7005 }
7006
7007 pci_set_drvdata(pci, card);
7008
7009 dev++;
7010 return 0;
7011}
7012
e23e7a14 7013static void snd_hdspm_remove(struct pci_dev *pci)
763f356c
TI
7014{
7015 snd_card_free(pci_get_drvdata(pci));
763f356c
TI
7016}
7017
e9f66d9b 7018static struct pci_driver hdspm_driver = {
3733e424 7019 .name = KBUILD_MODNAME,
763f356c
TI
7020 .id_table = snd_hdspm_ids,
7021 .probe = snd_hdspm_probe,
e23e7a14 7022 .remove = snd_hdspm_remove,
763f356c
TI
7023};
7024
e9f66d9b 7025module_pci_driver(hdspm_driver);
This page took 1.685521 seconds and 5 git commands to generate.