ALSA: hdspm - Use snd_ctl_enum_info() for texts_autosync
[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
eb0d4dbf 561static const char *const texts_autosync_aes_tco[] = { "Word Clock",
0dca1793
AK
562 "AES1", "AES2", "AES3", "AES4",
563 "AES5", "AES6", "AES7", "AES8",
db2d1a91
AK
564 "TCO", "Sync In"
565};
eb0d4dbf 566static const char *const texts_autosync_aes[] = { "Word Clock",
0dca1793 567 "AES1", "AES2", "AES3", "AES4",
db2d1a91
AK
568 "AES5", "AES6", "AES7", "AES8",
569 "Sync In"
570};
eb0d4dbf 571static const char *const texts_autosync_madi_tco[] = { "Word Clock",
0dca1793 572 "MADI", "TCO", "Sync In" };
eb0d4dbf 573static const char *const texts_autosync_madi[] = { "Word Clock",
0dca1793
AK
574 "MADI", "Sync In" };
575
eb0d4dbf 576static const char *const texts_autosync_raydat_tco[] = {
0dca1793
AK
577 "Word Clock",
578 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
579 "AES", "SPDIF", "TCO", "Sync In"
580};
eb0d4dbf 581static const char *const texts_autosync_raydat[] = {
0dca1793
AK
582 "Word Clock",
583 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
584 "AES", "SPDIF", "Sync In"
585};
eb0d4dbf 586static const char *const texts_autosync_aio_tco[] = {
0dca1793
AK
587 "Word Clock",
588 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
589};
eb0d4dbf 590static const char *const texts_autosync_aio[] = { "Word Clock",
0dca1793
AK
591 "ADAT", "AES", "SPDIF", "Sync In" };
592
38816545 593static const char *const texts_freq[] = {
0dca1793
AK
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
eb0d4dbf 978 const char *const *texts_autosync;
0dca1793 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 2289#define ENUMERATED_CTL_INFO(info, texts) \
38816545 2290 snd_ctl_enum_info(info, 1, ARRAY_SIZE(texts), texts)
8cea5710 2291
0dca1793 2292
2336142f
AK
2293/* Helper function to query the external sample rate and return the
2294 * corresponding enum to be returned to userspace.
2295 */
2296static int hdspm_external_rate_to_enum(struct hdspm *hdspm)
2297{
2298 int rate = hdspm_external_sample_rate(hdspm);
2299 int i, selected_rate = 0;
2300 for (i = 1; i < 10; i++)
2301 if (HDSPM_bit2freq(i) == rate) {
2302 selected_rate = i;
2303 break;
2304 }
2305 return selected_rate;
2306}
2307
0dca1793 2308
763f356c 2309#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
0dca1793
AK
2310{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2311 .name = xname, \
2312 .private_value = xindex, \
2313 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2314 .info = snd_hdspm_info_autosync_sample_rate, \
2315 .get = snd_hdspm_get_autosync_sample_rate \
763f356c
TI
2316}
2317
0dca1793 2318
98274f07
TI
2319static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_info *uinfo)
763f356c 2321{
e5b7b1fe 2322 ENUMERATED_CTL_INFO(uinfo, texts_freq);
763f356c
TI
2323 return 0;
2324}
2325
0dca1793 2326
98274f07
TI
2327static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2328 struct snd_ctl_elem_value *
763f356c
TI
2329 ucontrol)
2330{
98274f07 2331 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2332
0dca1793
AK
2333 switch (hdspm->io_type) {
2334 case RayDAT:
2335 switch (kcontrol->private_value) {
2336 case 0:
2337 ucontrol->value.enumerated.item[0] =
2338 hdspm_get_wc_sample_rate(hdspm);
2339 break;
2340 case 7:
2341 ucontrol->value.enumerated.item[0] =
2342 hdspm_get_tco_sample_rate(hdspm);
2343 break;
2344 case 8:
2345 ucontrol->value.enumerated.item[0] =
2346 hdspm_get_sync_in_sample_rate(hdspm);
2347 break;
2348 default:
2349 ucontrol->value.enumerated.item[0] =
2350 hdspm_get_s1_sample_rate(hdspm,
2351 kcontrol->private_value-1);
2352 }
d681deaa 2353 break;
763f356c 2354
0dca1793
AK
2355 case AIO:
2356 switch (kcontrol->private_value) {
2357 case 0: /* WC */
2358 ucontrol->value.enumerated.item[0] =
2359 hdspm_get_wc_sample_rate(hdspm);
2360 break;
2361 case 4: /* TCO */
2362 ucontrol->value.enumerated.item[0] =
2363 hdspm_get_tco_sample_rate(hdspm);
2364 break;
2365 case 5: /* SYNC_IN */
2366 ucontrol->value.enumerated.item[0] =
2367 hdspm_get_sync_in_sample_rate(hdspm);
2368 break;
2369 default:
2370 ucontrol->value.enumerated.item[0] =
2371 hdspm_get_s1_sample_rate(hdspm,
1cb7dbf4 2372 kcontrol->private_value-1);
0dca1793 2373 }
d681deaa 2374 break;
7c4a95b5
AK
2375
2376 case AES32:
2377
2378 switch (kcontrol->private_value) {
2379 case 0: /* WC */
2380 ucontrol->value.enumerated.item[0] =
2381 hdspm_get_wc_sample_rate(hdspm);
2382 break;
2383 case 9: /* TCO */
2384 ucontrol->value.enumerated.item[0] =
2385 hdspm_get_tco_sample_rate(hdspm);
2386 break;
2387 case 10: /* SYNC_IN */
2388 ucontrol->value.enumerated.item[0] =
2389 hdspm_get_sync_in_sample_rate(hdspm);
2390 break;
2d63ec38
AK
2391 case 11: /* External Rate */
2392 ucontrol->value.enumerated.item[0] =
2393 hdspm_external_rate_to_enum(hdspm);
2394 break;
7c4a95b5
AK
2395 default: /* AES1 to AES8 */
2396 ucontrol->value.enumerated.item[0] =
2d63ec38
AK
2397 hdspm_get_aes_sample_rate(hdspm,
2398 kcontrol->private_value -
2399 HDSPM_AES32_AUTOSYNC_FROM_AES1);
7c4a95b5 2400 break;
7c4a95b5 2401 }
d681deaa 2402 break;
b8812c55
AK
2403
2404 case MADI:
2405 case MADIface:
2336142f
AK
2406 ucontrol->value.enumerated.item[0] =
2407 hdspm_external_rate_to_enum(hdspm);
b8812c55 2408 break;
763f356c 2409 default:
0dca1793 2410 break;
763f356c 2411 }
763f356c 2412
0dca1793 2413 return 0;
763f356c
TI
2414}
2415
2416
0dca1793
AK
2417#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
2418{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2419 .name = xname, \
2420 .index = xindex, \
2421 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2422 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2423 .info = snd_hdspm_info_system_clock_mode, \
2424 .get = snd_hdspm_get_system_clock_mode, \
2425 .put = snd_hdspm_put_system_clock_mode, \
2426}
2427
2428
2429/**
2430 * Returns the system clock mode for the given card.
2431 * @returns 0 - master, 1 - slave
2432 **/
2433static int hdspm_system_clock_mode(struct hdspm *hdspm)
2434{
2435 switch (hdspm->io_type) {
2436 case AIO:
2437 case RayDAT:
2438 if (hdspm->settings_register & HDSPM_c0Master)
2439 return 0;
2440 break;
763f356c 2441
0dca1793
AK
2442 default:
2443 if (hdspm->control_register & HDSPM_ClockModeMaster)
2444 return 0;
2445 }
763f356c 2446
763f356c
TI
2447 return 1;
2448}
2449
0dca1793
AK
2450
2451/**
2452 * Sets the system clock mode.
2453 * @param mode 0 - master, 1 - slave
2454 **/
2455static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2456{
34be7ebb
AK
2457 hdspm_set_toggle_setting(hdspm,
2458 (hdspm_is_raydat_or_aio(hdspm)) ?
2459 HDSPM_c0Master : HDSPM_ClockModeMaster,
2460 (0 == mode));
0dca1793
AK
2461}
2462
2463
2464static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
98274f07 2465 struct snd_ctl_elem_info *uinfo)
763f356c 2466{
38816545 2467 static const char *const texts[] = { "Master", "AutoSync" };
e5b7b1fe 2468 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
2469 return 0;
2470}
2471
98274f07
TI
2472static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_value *ucontrol)
763f356c 2474{
98274f07 2475 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2476
0dca1793 2477 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
763f356c
TI
2478 return 0;
2479}
2480
0dca1793
AK
2481static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_value *ucontrol)
2483{
2484 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2485 int val;
2486
2487 if (!snd_hdspm_use_is_exclusive(hdspm))
2488 return -EBUSY;
2489
2490 val = ucontrol->value.enumerated.item[0];
2491 if (val < 0)
2492 val = 0;
2493 else if (val > 1)
2494 val = 1;
2495
2496 hdspm_set_system_clock_mode(hdspm, val);
2497
2498 return 0;
2499}
2500
2501
2502#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2503{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2504 .name = xname, \
2505 .index = xindex, \
2506 .info = snd_hdspm_info_clock_source, \
2507 .get = snd_hdspm_get_clock_source, \
2508 .put = snd_hdspm_put_clock_source \
763f356c
TI
2509}
2510
0dca1793 2511
98274f07 2512static int hdspm_clock_source(struct hdspm * hdspm)
763f356c 2513{
0dca1793
AK
2514 switch (hdspm->system_sample_rate) {
2515 case 32000: return 0;
2516 case 44100: return 1;
2517 case 48000: return 2;
2518 case 64000: return 3;
2519 case 88200: return 4;
2520 case 96000: return 5;
2521 case 128000: return 6;
2522 case 176400: return 7;
2523 case 192000: return 8;
763f356c 2524 }
0dca1793
AK
2525
2526 return -1;
763f356c
TI
2527}
2528
98274f07 2529static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
763f356c
TI
2530{
2531 int rate;
2532 switch (mode) {
0dca1793
AK
2533 case 0:
2534 rate = 32000; break;
2535 case 1:
2536 rate = 44100; break;
2537 case 2:
2538 rate = 48000; break;
2539 case 3:
2540 rate = 64000; break;
2541 case 4:
2542 rate = 88200; break;
2543 case 5:
2544 rate = 96000; break;
2545 case 6:
2546 rate = 128000; break;
2547 case 7:
2548 rate = 176400; break;
2549 case 8:
2550 rate = 192000; break;
763f356c 2551 default:
0dca1793 2552 rate = 48000;
763f356c 2553 }
763f356c
TI
2554 hdspm_set_rate(hdspm, rate, 1);
2555 return 0;
2556}
2557
98274f07
TI
2558static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_info *uinfo)
763f356c 2560{
763f356c
TI
2561 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2562 uinfo->count = 1;
0dca1793 2563 uinfo->value.enumerated.items = 9;
763f356c
TI
2564
2565 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2566 uinfo->value.enumerated.item =
2567 uinfo->value.enumerated.items - 1;
2568
2569 strcpy(uinfo->value.enumerated.name,
0dca1793 2570 texts_freq[uinfo->value.enumerated.item+1]);
763f356c
TI
2571
2572 return 0;
2573}
2574
98274f07
TI
2575static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_value *ucontrol)
763f356c 2577{
98274f07 2578 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
2579
2580 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2581 return 0;
2582}
2583
98274f07
TI
2584static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
763f356c 2586{
98274f07 2587 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
2588 int change;
2589 int val;
2590
2591 if (!snd_hdspm_use_is_exclusive(hdspm))
2592 return -EBUSY;
2593 val = ucontrol->value.enumerated.item[0];
2594 if (val < 0)
2595 val = 0;
6534599d
RB
2596 if (val > 9)
2597 val = 9;
763f356c
TI
2598 spin_lock_irq(&hdspm->lock);
2599 if (val != hdspm_clock_source(hdspm))
2600 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2601 else
2602 change = 0;
2603 spin_unlock_irq(&hdspm->lock);
2604 return change;
2605}
2606
763f356c 2607
0dca1793 2608#define HDSPM_PREF_SYNC_REF(xname, xindex) \
f27a64f9 2609{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
0dca1793
AK
2610 .name = xname, \
2611 .index = xindex, \
2612 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2613 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2614 .info = snd_hdspm_info_pref_sync_ref, \
2615 .get = snd_hdspm_get_pref_sync_ref, \
2616 .put = snd_hdspm_put_pref_sync_ref \
2617}
2618
2619
2620/**
2621 * Returns the current preferred sync reference setting.
2622 * The semantics of the return value are depending on the
2623 * card, please see the comments for clarification.
2624 **/
98274f07 2625static int hdspm_pref_sync_ref(struct hdspm * hdspm)
763f356c 2626{
0dca1793
AK
2627 switch (hdspm->io_type) {
2628 case AES32:
3cee5a60 2629 switch (hdspm->control_register & HDSPM_SyncRefMask) {
0dca1793
AK
2630 case 0: return 0; /* WC */
2631 case HDSPM_SyncRef0: return 1; /* AES 1 */
2632 case HDSPM_SyncRef1: return 2; /* AES 2 */
2633 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2634 case HDSPM_SyncRef2: return 4; /* AES 4 */
2635 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2636 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2637 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2638 return 7; /* AES 7 */
2639 case HDSPM_SyncRef3: return 8; /* AES 8 */
2640 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
3cee5a60 2641 }
0dca1793
AK
2642 break;
2643
2644 case MADI:
2645 case MADIface:
2646 if (hdspm->tco) {
2647 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2648 case 0: return 0; /* WC */
2649 case HDSPM_SyncRef0: return 1; /* MADI */
2650 case HDSPM_SyncRef1: return 2; /* TCO */
2651 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2652 return 3; /* SYNC_IN */
2653 }
2654 } else {
2655 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2656 case 0: return 0; /* WC */
2657 case HDSPM_SyncRef0: return 1; /* MADI */
2658 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2659 return 2; /* SYNC_IN */
2660 }
2661 }
2662 break;
2663
2664 case RayDAT:
2665 if (hdspm->tco) {
2666 switch ((hdspm->settings_register &
2667 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2668 case 0: return 0; /* WC */
2669 case 3: return 1; /* ADAT 1 */
2670 case 4: return 2; /* ADAT 2 */
2671 case 5: return 3; /* ADAT 3 */
2672 case 6: return 4; /* ADAT 4 */
2673 case 1: return 5; /* AES */
2674 case 2: return 6; /* SPDIF */
2675 case 9: return 7; /* TCO */
2676 case 10: return 8; /* SYNC_IN */
2677 }
2678 } else {
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 10: return 7; /* SYNC_IN */
2689 }
3cee5a60 2690 }
0dca1793
AK
2691
2692 break;
2693
2694 case AIO:
2695 if (hdspm->tco) {
2696 switch ((hdspm->settings_register &
2697 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2698 case 0: return 0; /* WC */
2699 case 3: return 1; /* ADAT */
2700 case 1: return 2; /* AES */
2701 case 2: return 3; /* SPDIF */
2702 case 9: return 4; /* TCO */
2703 case 10: return 5; /* SYNC_IN */
2704 }
2705 } else {
2706 switch ((hdspm->settings_register &
2707 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2708 case 0: return 0; /* WC */
2709 case 3: return 1; /* ADAT */
2710 case 1: return 2; /* AES */
2711 case 2: return 3; /* SPDIF */
2712 case 10: return 4; /* SYNC_IN */
2713 }
2714 }
2715
2716 break;
763f356c
TI
2717 }
2718
0dca1793 2719 return -1;
763f356c
TI
2720}
2721
0dca1793
AK
2722
2723/**
2724 * Set the preferred sync reference to <pref>. The semantics
2725 * of <pref> are depending on the card type, see the comments
2726 * for clarification.
2727 **/
98274f07 2728static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
763f356c 2729{
0dca1793 2730 int p = 0;
763f356c 2731
0dca1793
AK
2732 switch (hdspm->io_type) {
2733 case AES32:
2734 hdspm->control_register &= ~HDSPM_SyncRefMask;
3cee5a60 2735 switch (pref) {
0dca1793
AK
2736 case 0: /* WC */
2737 break;
2738 case 1: /* AES 1 */
2739 hdspm->control_register |= HDSPM_SyncRef0;
2740 break;
2741 case 2: /* AES 2 */
2742 hdspm->control_register |= HDSPM_SyncRef1;
2743 break;
2744 case 3: /* AES 3 */
2745 hdspm->control_register |=
2746 HDSPM_SyncRef1+HDSPM_SyncRef0;
2747 break;
2748 case 4: /* AES 4 */
2749 hdspm->control_register |= HDSPM_SyncRef2;
2750 break;
2751 case 5: /* AES 5 */
2752 hdspm->control_register |=
2753 HDSPM_SyncRef2+HDSPM_SyncRef0;
2754 break;
2755 case 6: /* AES 6 */
2756 hdspm->control_register |=
2757 HDSPM_SyncRef2+HDSPM_SyncRef1;
2758 break;
2759 case 7: /* AES 7 */
2760 hdspm->control_register |=
2761 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
3cee5a60 2762 break;
0dca1793
AK
2763 case 8: /* AES 8 */
2764 hdspm->control_register |= HDSPM_SyncRef3;
2765 break;
2766 case 9: /* TCO */
2767 hdspm->control_register |=
2768 HDSPM_SyncRef3+HDSPM_SyncRef0;
3cee5a60
RB
2769 break;
2770 default:
2771 return -1;
2772 }
0dca1793
AK
2773
2774 break;
2775
2776 case MADI:
2777 case MADIface:
2778 hdspm->control_register &= ~HDSPM_SyncRefMask;
2779 if (hdspm->tco) {
2780 switch (pref) {
2781 case 0: /* WC */
2782 break;
2783 case 1: /* MADI */
2784 hdspm->control_register |= HDSPM_SyncRef0;
2785 break;
2786 case 2: /* TCO */
2787 hdspm->control_register |= HDSPM_SyncRef1;
2788 break;
2789 case 3: /* SYNC_IN */
2790 hdspm->control_register |=
2791 HDSPM_SyncRef0+HDSPM_SyncRef1;
2792 break;
2793 default:
2794 return -1;
2795 }
2796 } else {
2797 switch (pref) {
2798 case 0: /* WC */
2799 break;
2800 case 1: /* MADI */
2801 hdspm->control_register |= HDSPM_SyncRef0;
2802 break;
2803 case 2: /* SYNC_IN */
2804 hdspm->control_register |=
2805 HDSPM_SyncRef0+HDSPM_SyncRef1;
2806 break;
2807 default:
2808 return -1;
2809 }
2810 }
2811
2812 break;
2813
2814 case RayDAT:
2815 if (hdspm->tco) {
2816 switch (pref) {
2817 case 0: p = 0; break; /* WC */
2818 case 1: p = 3; break; /* ADAT 1 */
2819 case 2: p = 4; break; /* ADAT 2 */
2820 case 3: p = 5; break; /* ADAT 3 */
2821 case 4: p = 6; break; /* ADAT 4 */
2822 case 5: p = 1; break; /* AES */
2823 case 6: p = 2; break; /* SPDIF */
2824 case 7: p = 9; break; /* TCO */
2825 case 8: p = 10; break; /* SYNC_IN */
2826 default: return -1;
2827 }
2828 } else {
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 = 10; break; /* SYNC_IN */
2838 default: return -1;
2839 }
2840 }
2841 break;
2842
2843 case AIO:
2844 if (hdspm->tco) {
2845 switch (pref) {
2846 case 0: p = 0; break; /* WC */
2847 case 1: p = 3; break; /* ADAT */
2848 case 2: p = 1; break; /* AES */
2849 case 3: p = 2; break; /* SPDIF */
2850 case 4: p = 9; break; /* TCO */
2851 case 5: p = 10; break; /* SYNC_IN */
2852 default: return -1;
2853 }
2854 } else {
2855 switch (pref) {
2856 case 0: p = 0; break; /* WC */
2857 case 1: p = 3; break; /* ADAT */
2858 case 2: p = 1; break; /* AES */
2859 case 3: p = 2; break; /* SPDIF */
2860 case 4: p = 10; break; /* SYNC_IN */
2861 default: return -1;
2862 }
2863 }
2864 break;
763f356c 2865 }
0dca1793
AK
2866
2867 switch (hdspm->io_type) {
2868 case RayDAT:
2869 case AIO:
2870 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2871 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2872 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2873 break;
2874
2875 case MADI:
2876 case MADIface:
2877 case AES32:
2878 hdspm_write(hdspm, HDSPM_controlRegister,
2879 hdspm->control_register);
2880 }
2881
763f356c
TI
2882 return 0;
2883}
2884
0dca1793 2885
98274f07
TI
2886static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_info *uinfo)
763f356c 2888{
3cee5a60 2889 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2890
eb0d4dbf 2891 snd_ctl_enum_info(uinfo, 1, hdspm->texts_autosync_items, hdspm->texts_autosync);
3cee5a60 2892
763f356c
TI
2893 return 0;
2894}
2895
98274f07
TI
2896static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
763f356c 2898{
98274f07 2899 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
0dca1793 2900 int psf = hdspm_pref_sync_ref(hdspm);
763f356c 2901
0dca1793
AK
2902 if (psf >= 0) {
2903 ucontrol->value.enumerated.item[0] = psf;
2904 return 0;
2905 }
2906
2907 return -1;
763f356c
TI
2908}
2909
98274f07
TI
2910static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
763f356c 2912{
98274f07 2913 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
0dca1793 2914 int val, change = 0;
763f356c
TI
2915
2916 if (!snd_hdspm_use_is_exclusive(hdspm))
2917 return -EBUSY;
2918
0dca1793
AK
2919 val = ucontrol->value.enumerated.item[0];
2920
2921 if (val < 0)
2922 val = 0;
2923 else if (val >= hdspm->texts_autosync_items)
2924 val = hdspm->texts_autosync_items-1;
763f356c
TI
2925
2926 spin_lock_irq(&hdspm->lock);
0dca1793
AK
2927 if (val != hdspm_pref_sync_ref(hdspm))
2928 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2929
763f356c
TI
2930 spin_unlock_irq(&hdspm->lock);
2931 return change;
2932}
2933
0dca1793 2934
763f356c 2935#define HDSPM_AUTOSYNC_REF(xname, xindex) \
f27a64f9
AK
2936{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2937 .name = xname, \
2938 .index = xindex, \
2939 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2940 .info = snd_hdspm_info_autosync_ref, \
2941 .get = snd_hdspm_get_autosync_ref, \
763f356c
TI
2942}
2943
0dca1793 2944static int hdspm_autosync_ref(struct hdspm *hdspm)
763f356c 2945{
2d60fc7f 2946 /* This looks at the autosync selected sync reference */
0dca1793 2947 if (AES32 == hdspm->io_type) {
2d60fc7f 2948
3cee5a60 2949 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
2d60fc7f
AK
2950 unsigned int syncref = (status >> HDSPM_AES32_syncref_bit) & 0xF;
2951 if ((syncref >= HDSPM_AES32_AUTOSYNC_FROM_WORD) &&
2952 (syncref <= HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN)) {
3cee5a60 2953 return syncref;
2d60fc7f 2954 }
3cee5a60 2955 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
2d60fc7f 2956
0dca1793 2957 } else if (MADI == hdspm->io_type) {
3cee5a60 2958
2d60fc7f 2959 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3cee5a60
RB
2960 switch (status2 & HDSPM_SelSyncRefMask) {
2961 case HDSPM_SelSyncRef_WORD:
2962 return HDSPM_AUTOSYNC_FROM_WORD;
2963 case HDSPM_SelSyncRef_MADI:
2964 return HDSPM_AUTOSYNC_FROM_MADI;
0dca1793
AK
2965 case HDSPM_SelSyncRef_TCO:
2966 return HDSPM_AUTOSYNC_FROM_TCO;
2967 case HDSPM_SelSyncRef_SyncIn:
2968 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
3cee5a60
RB
2969 case HDSPM_SelSyncRef_NVALID:
2970 return HDSPM_AUTOSYNC_FROM_NONE;
2971 default:
e71b95ad 2972 return HDSPM_AUTOSYNC_FROM_NONE;
3cee5a60 2973 }
763f356c 2974
763f356c 2975 }
0dca1793 2976 return 0;
763f356c
TI
2977}
2978
0dca1793 2979
98274f07
TI
2980static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_info *uinfo)
763f356c 2982{
3cee5a60 2983 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 2984
0dca1793 2985 if (AES32 == hdspm->io_type) {
3cee5a60 2986 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
db2d1a91 2987 "AES4", "AES5", "AES6", "AES7", "AES8", "TCO", "Sync In", "None"};
3cee5a60
RB
2988
2989 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2990 uinfo->count = 1;
db2d1a91 2991 uinfo->value.enumerated.items = ARRAY_SIZE(texts);
ef5fa1a4
TI
2992 if (uinfo->value.enumerated.item >=
2993 uinfo->value.enumerated.items)
3cee5a60
RB
2994 uinfo->value.enumerated.item =
2995 uinfo->value.enumerated.items - 1;
2996 strcpy(uinfo->value.enumerated.name,
2997 texts[uinfo->value.enumerated.item]);
0dca1793
AK
2998 } else if (MADI == hdspm->io_type) {
2999 static char *texts[] = {"Word Clock", "MADI", "TCO",
3000 "Sync In", "None" };
3cee5a60
RB
3001
3002 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3003 uinfo->count = 1;
0dca1793 3004 uinfo->value.enumerated.items = 5;
ef5fa1a4 3005 if (uinfo->value.enumerated.item >=
0dca1793 3006 uinfo->value.enumerated.items)
3cee5a60
RB
3007 uinfo->value.enumerated.item =
3008 uinfo->value.enumerated.items - 1;
3009 strcpy(uinfo->value.enumerated.name,
3010 texts[uinfo->value.enumerated.item]);
3011 }
763f356c
TI
3012 return 0;
3013}
3014
98274f07
TI
3015static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_value *ucontrol)
763f356c 3017{
98274f07 3018 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3019
6534599d 3020 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
763f356c
TI
3021 return 0;
3022}
3023
f99c7881
AK
3024
3025
3026#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
3027{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3028 .name = xname, \
3029 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3030 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3031 .info = snd_hdspm_info_tco_video_input_format, \
3032 .get = snd_hdspm_get_tco_video_input_format, \
3033}
3034
3035static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
3036 struct snd_ctl_elem_info *uinfo)
3037{
38816545 3038 static const char *const texts[] = {"No video", "NTSC", "PAL"};
f99c7881
AK
3039 ENUMERATED_CTL_INFO(uinfo, texts);
3040 return 0;
3041}
3042
3043static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
3044 struct snd_ctl_elem_value *ucontrol)
3045{
3046 u32 status;
3047 int ret = 0;
3048
3049 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3050 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3051 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3052 HDSPM_TCO1_Video_Input_Format_PAL)) {
3053 case HDSPM_TCO1_Video_Input_Format_NTSC:
3054 /* ntsc */
3055 ret = 1;
3056 break;
3057 case HDSPM_TCO1_Video_Input_Format_PAL:
3058 /* pal */
3059 ret = 2;
3060 break;
3061 default:
3062 /* no video */
3063 ret = 0;
3064 break;
3065 }
3066 ucontrol->value.enumerated.item[0] = ret;
3067 return 0;
3068}
3069
3070
3071
3072#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3073{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3074 .name = xname, \
3075 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3076 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3077 .info = snd_hdspm_info_tco_ltc_frames, \
3078 .get = snd_hdspm_get_tco_ltc_frames, \
3079}
3080
3081static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3082 struct snd_ctl_elem_info *uinfo)
3083{
38816545 3084 static const char *const texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
f99c7881
AK
3085 "30 fps"};
3086 ENUMERATED_CTL_INFO(uinfo, texts);
3087 return 0;
3088}
3089
3090static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3091{
3092 u32 status;
3093 int ret = 0;
3094
3095 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3096 if (status & HDSPM_TCO1_LTC_Input_valid) {
3097 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3098 HDSPM_TCO1_LTC_Format_MSB)) {
3099 case 0:
3100 /* 24 fps */
3101 ret = 1;
3102 break;
3103 case HDSPM_TCO1_LTC_Format_LSB:
3104 /* 25 fps */
3105 ret = 2;
3106 break;
3107 case HDSPM_TCO1_LTC_Format_MSB:
3108 /* 25 fps */
3109 ret = 3;
3110 break;
3111 default:
3112 /* 30 fps */
3113 ret = 4;
3114 break;
3115 }
3116 }
3117
3118 return ret;
3119}
3120
3121static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_value *ucontrol)
3123{
3124 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3125
3126 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3127 return 0;
3128}
3129
bf0ff87b
AK
3130#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3131{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3132 .name = xname, \
3133 .private_value = xindex, \
3134 .info = snd_hdspm_info_toggle_setting, \
3135 .get = snd_hdspm_get_toggle_setting, \
3136 .put = snd_hdspm_put_toggle_setting \
3137}
3138
3139static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3140{
ce13f3f3
AK
3141 u32 reg;
3142
3143 if (hdspm_is_raydat_or_aio(hdspm))
3144 reg = hdspm->settings_register;
3145 else
3146 reg = hdspm->control_register;
3147
3148 return (reg & regmask) ? 1 : 0;
bf0ff87b
AK
3149}
3150
3151static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3152{
ce13f3f3
AK
3153 u32 *reg;
3154 u32 target_reg;
3155
3156 if (hdspm_is_raydat_or_aio(hdspm)) {
3157 reg = &(hdspm->settings_register);
3158 target_reg = HDSPM_WR_SETTINGS;
3159 } else {
3160 reg = &(hdspm->control_register);
3161 target_reg = HDSPM_controlRegister;
3162 }
3163
bf0ff87b 3164 if (out)
ce13f3f3 3165 *reg |= regmask;
bf0ff87b 3166 else
ce13f3f3
AK
3167 *reg &= ~regmask;
3168
3169 hdspm_write(hdspm, target_reg, *reg);
bf0ff87b
AK
3170
3171 return 0;
3172}
3173
3174#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3175
3176static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
3178{
3179 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3180 u32 regmask = kcontrol->private_value;
3181
3182 spin_lock_irq(&hdspm->lock);
3183 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3184 spin_unlock_irq(&hdspm->lock);
3185 return 0;
3186}
3187
3188static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
3190{
3191 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3192 u32 regmask = kcontrol->private_value;
3193 int change;
3194 unsigned int val;
3195
3196 if (!snd_hdspm_use_is_exclusive(hdspm))
3197 return -EBUSY;
3198 val = ucontrol->value.integer.value[0] & 1;
3199 spin_lock_irq(&hdspm->lock);
3200 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3201 hdspm_set_toggle_setting(hdspm, regmask, val);
3202 spin_unlock_irq(&hdspm->lock);
3203 return change;
3204}
3205
3cee5a60 3206#define HDSPM_INPUT_SELECT(xname, xindex) \
f27a64f9
AK
3207{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3208 .name = xname, \
3209 .index = xindex, \
3210 .info = snd_hdspm_info_input_select, \
3211 .get = snd_hdspm_get_input_select, \
3212 .put = snd_hdspm_put_input_select \
3cee5a60
RB
3213}
3214
3215static int hdspm_input_select(struct hdspm * hdspm)
3216{
3217 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3218}
3219
3220static int hdspm_set_input_select(struct hdspm * hdspm, int out)
3221{
3222 if (out)
3223 hdspm->control_register |= HDSPM_InputSelect0;
3224 else
3225 hdspm->control_register &= ~HDSPM_InputSelect0;
3226 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3227
3228 return 0;
3229}
3230
3231static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_info *uinfo)
3233{
38816545 3234 static const char *const texts[] = { "optical", "coaxial" };
e5b7b1fe 3235 ENUMERATED_CTL_INFO(uinfo, texts);
3cee5a60
RB
3236 return 0;
3237}
3238
3239static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3240 struct snd_ctl_elem_value *ucontrol)
3241{
3242 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3243
3244 spin_lock_irq(&hdspm->lock);
3245 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3246 spin_unlock_irq(&hdspm->lock);
3247 return 0;
3248}
3249
3250static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol)
3252{
3253 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3254 int change;
3255 unsigned int val;
3256
3257 if (!snd_hdspm_use_is_exclusive(hdspm))
3258 return -EBUSY;
3259 val = ucontrol->value.integer.value[0] & 1;
3260 spin_lock_irq(&hdspm->lock);
3261 change = (int) val != hdspm_input_select(hdspm);
3262 hdspm_set_input_select(hdspm, val);
3263 spin_unlock_irq(&hdspm->lock);
3264 return change;
3265}
3266
0dca1793 3267
3cee5a60 3268#define HDSPM_DS_WIRE(xname, xindex) \
f27a64f9
AK
3269{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3270 .name = xname, \
3271 .index = xindex, \
3272 .info = snd_hdspm_info_ds_wire, \
3273 .get = snd_hdspm_get_ds_wire, \
3274 .put = snd_hdspm_put_ds_wire \
3cee5a60
RB
3275}
3276
3277static int hdspm_ds_wire(struct hdspm * hdspm)
763f356c 3278{
3cee5a60 3279 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
763f356c
TI
3280}
3281
3cee5a60 3282static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
763f356c 3283{
3cee5a60
RB
3284 if (ds)
3285 hdspm->control_register |= HDSPM_DS_DoubleWire;
763f356c 3286 else
3cee5a60 3287 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
763f356c
TI
3288 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3289
3290 return 0;
3291}
3292
3cee5a60
RB
3293static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3294 struct snd_ctl_elem_info *uinfo)
763f356c 3295{
38816545 3296 static const char *const texts[] = { "Single", "Double" };
e5b7b1fe 3297 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3298 return 0;
3299}
3300
3cee5a60
RB
3301static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_value *ucontrol)
763f356c 3303{
98274f07 3304 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3305
3306 spin_lock_irq(&hdspm->lock);
3cee5a60 3307 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
763f356c
TI
3308 spin_unlock_irq(&hdspm->lock);
3309 return 0;
3310}
3311
3cee5a60
RB
3312static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3313 struct snd_ctl_elem_value *ucontrol)
763f356c 3314{
98274f07 3315 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3316 int change;
3317 unsigned int val;
3318
3319 if (!snd_hdspm_use_is_exclusive(hdspm))
3320 return -EBUSY;
3321 val = ucontrol->value.integer.value[0] & 1;
3322 spin_lock_irq(&hdspm->lock);
3cee5a60
RB
3323 change = (int) val != hdspm_ds_wire(hdspm);
3324 hdspm_set_ds_wire(hdspm, val);
763f356c
TI
3325 spin_unlock_irq(&hdspm->lock);
3326 return change;
3327}
3328
0dca1793 3329
3cee5a60 3330#define HDSPM_QS_WIRE(xname, xindex) \
f27a64f9
AK
3331{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3332 .name = xname, \
3333 .index = xindex, \
3334 .info = snd_hdspm_info_qs_wire, \
3335 .get = snd_hdspm_get_qs_wire, \
3336 .put = snd_hdspm_put_qs_wire \
763f356c
TI
3337}
3338
3cee5a60 3339static int hdspm_qs_wire(struct hdspm * hdspm)
763f356c 3340{
3cee5a60
RB
3341 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3342 return 1;
3343 if (hdspm->control_register & HDSPM_QS_QuadWire)
3344 return 2;
3345 return 0;
763f356c
TI
3346}
3347
3cee5a60 3348static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
763f356c 3349{
3cee5a60
RB
3350 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3351 switch (mode) {
3352 case 0:
3353 break;
3354 case 1:
3355 hdspm->control_register |= HDSPM_QS_DoubleWire;
3356 break;
3357 case 2:
3358 hdspm->control_register |= HDSPM_QS_QuadWire;
3359 break;
3360 }
763f356c
TI
3361 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3362
3363 return 0;
3364}
3365
3cee5a60 3366static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3367 struct snd_ctl_elem_info *uinfo)
763f356c 3368{
38816545 3369 static const char *const texts[] = { "Single", "Double", "Quad" };
e5b7b1fe 3370 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3371 return 0;
3372}
3373
3cee5a60 3374static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3375 struct snd_ctl_elem_value *ucontrol)
763f356c 3376{
98274f07 3377 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3378
3379 spin_lock_irq(&hdspm->lock);
3cee5a60 3380 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
763f356c
TI
3381 spin_unlock_irq(&hdspm->lock);
3382 return 0;
3383}
3384
3cee5a60 3385static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
98274f07 3386 struct snd_ctl_elem_value *ucontrol)
763f356c 3387{
98274f07 3388 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3389 int change;
3cee5a60 3390 int val;
763f356c
TI
3391
3392 if (!snd_hdspm_use_is_exclusive(hdspm))
3393 return -EBUSY;
3cee5a60
RB
3394 val = ucontrol->value.integer.value[0];
3395 if (val < 0)
3396 val = 0;
3397 if (val > 2)
3398 val = 2;
763f356c 3399 spin_lock_irq(&hdspm->lock);
ef5fa1a4 3400 change = val != hdspm_qs_wire(hdspm);
3cee5a60 3401 hdspm_set_qs_wire(hdspm, val);
763f356c
TI
3402 spin_unlock_irq(&hdspm->lock);
3403 return change;
3404}
3405
acf14767
AK
3406#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
3407{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3408 .name = xname, \
3409 .private_value = xindex, \
3410 .info = snd_hdspm_info_tristate, \
3411 .get = snd_hdspm_get_tristate, \
3412 .put = snd_hdspm_put_tristate \
3413}
3414
3415static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
3416{
3417 u32 reg = hdspm->settings_register & (regmask * 3);
3418 return reg / regmask;
3419}
3420
3421static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
3422{
3423 hdspm->settings_register &= ~(regmask * 3);
3424 hdspm->settings_register |= (regmask * mode);
3425 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
3426
3427 return 0;
3428}
3429
3430static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
3431 struct snd_ctl_elem_info *uinfo)
3432{
3433 u32 regmask = kcontrol->private_value;
3434
38816545
AK
3435 static const char *const texts_spdif[] = { "Optical", "Coaxial", "Internal" };
3436 static const char *const texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
acf14767
AK
3437
3438 switch (regmask) {
3439 case HDSPM_c0_Input0:
3440 ENUMERATED_CTL_INFO(uinfo, texts_spdif);
3441 break;
3442 default:
3443 ENUMERATED_CTL_INFO(uinfo, texts_levels);
3444 break;
3445 }
3446 return 0;
3447}
3448
3449static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
3450 struct snd_ctl_elem_value *ucontrol)
3451{
3452 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3453 u32 regmask = kcontrol->private_value;
3454
3455 spin_lock_irq(&hdspm->lock);
3456 ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
3457 spin_unlock_irq(&hdspm->lock);
3458 return 0;
3459}
3460
3461static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol)
3463{
3464 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3465 u32 regmask = kcontrol->private_value;
3466 int change;
3467 int val;
3468
3469 if (!snd_hdspm_use_is_exclusive(hdspm))
3470 return -EBUSY;
3471 val = ucontrol->value.integer.value[0];
3472 if (val < 0)
3473 val = 0;
3474 if (val > 2)
3475 val = 2;
3476
3477 spin_lock_irq(&hdspm->lock);
3478 change = val != hdspm_tristate(hdspm, regmask);
3479 hdspm_set_tristate(hdspm, val, regmask);
3480 spin_unlock_irq(&hdspm->lock);
3481 return change;
3482}
3483
700d1ef3
AK
3484#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3485{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3486 .name = xname, \
3487 .index = xindex, \
3488 .info = snd_hdspm_info_madi_speedmode, \
3489 .get = snd_hdspm_get_madi_speedmode, \
3490 .put = snd_hdspm_put_madi_speedmode \
3491}
3492
3493static int hdspm_madi_speedmode(struct hdspm *hdspm)
3494{
3495 if (hdspm->control_register & HDSPM_QuadSpeed)
3496 return 2;
3497 if (hdspm->control_register & HDSPM_DoubleSpeed)
3498 return 1;
3499 return 0;
3500}
3501
3502static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3503{
3504 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3505 switch (mode) {
3506 case 0:
3507 break;
3508 case 1:
3509 hdspm->control_register |= HDSPM_DoubleSpeed;
3510 break;
3511 case 2:
3512 hdspm->control_register |= HDSPM_QuadSpeed;
3513 break;
3514 }
3515 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3516
3517 return 0;
3518}
3519
3520static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3521 struct snd_ctl_elem_info *uinfo)
3522{
38816545 3523 static const char *const texts[] = { "Single", "Double", "Quad" };
e5b7b1fe 3524 ENUMERATED_CTL_INFO(uinfo, texts);
700d1ef3
AK
3525 return 0;
3526}
3527
3528static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3529 struct snd_ctl_elem_value *ucontrol)
3530{
3531 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3532
3533 spin_lock_irq(&hdspm->lock);
3534 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3535 spin_unlock_irq(&hdspm->lock);
3536 return 0;
3537}
3538
3539static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3540 struct snd_ctl_elem_value *ucontrol)
3541{
3542 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3543 int change;
3544 int val;
3545
3546 if (!snd_hdspm_use_is_exclusive(hdspm))
3547 return -EBUSY;
3548 val = ucontrol->value.integer.value[0];
3549 if (val < 0)
3550 val = 0;
3551 if (val > 2)
3552 val = 2;
3553 spin_lock_irq(&hdspm->lock);
3554 change = val != hdspm_madi_speedmode(hdspm);
3555 hdspm_set_madi_speedmode(hdspm, val);
3556 spin_unlock_irq(&hdspm->lock);
3557 return change;
3558}
763f356c
TI
3559
3560#define HDSPM_MIXER(xname, xindex) \
f27a64f9
AK
3561{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3562 .name = xname, \
3563 .index = xindex, \
3564 .device = 0, \
3565 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3566 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3567 .info = snd_hdspm_info_mixer, \
3568 .get = snd_hdspm_get_mixer, \
3569 .put = snd_hdspm_put_mixer \
763f356c
TI
3570}
3571
98274f07
TI
3572static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3573 struct snd_ctl_elem_info *uinfo)
763f356c
TI
3574{
3575 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3576 uinfo->count = 3;
3577 uinfo->value.integer.min = 0;
3578 uinfo->value.integer.max = 65535;
3579 uinfo->value.integer.step = 1;
3580 return 0;
3581}
3582
98274f07
TI
3583static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3584 struct snd_ctl_elem_value *ucontrol)
763f356c 3585{
98274f07 3586 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3587 int source;
3588 int destination;
3589
3590 source = ucontrol->value.integer.value[0];
3591 if (source < 0)
3592 source = 0;
3593 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3594 source = 2 * HDSPM_MAX_CHANNELS - 1;
3595
3596 destination = ucontrol->value.integer.value[1];
3597 if (destination < 0)
3598 destination = 0;
3599 else if (destination >= HDSPM_MAX_CHANNELS)
3600 destination = HDSPM_MAX_CHANNELS - 1;
3601
3602 spin_lock_irq(&hdspm->lock);
3603 if (source >= HDSPM_MAX_CHANNELS)
3604 ucontrol->value.integer.value[2] =
3605 hdspm_read_pb_gain(hdspm, destination,
3606 source - HDSPM_MAX_CHANNELS);
3607 else
3608 ucontrol->value.integer.value[2] =
3609 hdspm_read_in_gain(hdspm, destination, source);
3610
3611 spin_unlock_irq(&hdspm->lock);
3612
3613 return 0;
3614}
3615
98274f07
TI
3616static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3617 struct snd_ctl_elem_value *ucontrol)
763f356c 3618{
98274f07 3619 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3620 int change;
3621 int source;
3622 int destination;
3623 int gain;
3624
3625 if (!snd_hdspm_use_is_exclusive(hdspm))
3626 return -EBUSY;
3627
3628 source = ucontrol->value.integer.value[0];
3629 destination = ucontrol->value.integer.value[1];
3630
3631 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3632 return -1;
3633 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3634 return -1;
3635
3636 gain = ucontrol->value.integer.value[2];
3637
3638 spin_lock_irq(&hdspm->lock);
3639
3640 if (source >= HDSPM_MAX_CHANNELS)
3641 change = gain != hdspm_read_pb_gain(hdspm, destination,
3642 source -
3643 HDSPM_MAX_CHANNELS);
3644 else
ef5fa1a4
TI
3645 change = gain != hdspm_read_in_gain(hdspm, destination,
3646 source);
763f356c
TI
3647
3648 if (change) {
3649 if (source >= HDSPM_MAX_CHANNELS)
3650 hdspm_write_pb_gain(hdspm, destination,
3651 source - HDSPM_MAX_CHANNELS,
3652 gain);
3653 else
3654 hdspm_write_in_gain(hdspm, destination, source,
3655 gain);
3656 }
3657 spin_unlock_irq(&hdspm->lock);
3658
3659 return change;
3660}
3661
3662/* The simple mixer control(s) provide gain control for the
3663 basic 1:1 mappings of playback streams to output
0dca1793 3664 streams.
763f356c
TI
3665*/
3666
3667#define HDSPM_PLAYBACK_MIXER \
f27a64f9
AK
3668{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3669 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3670 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3671 .info = snd_hdspm_info_playback_mixer, \
3672 .get = snd_hdspm_get_playback_mixer, \
3673 .put = snd_hdspm_put_playback_mixer \
763f356c
TI
3674}
3675
98274f07
TI
3676static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3677 struct snd_ctl_elem_info *uinfo)
763f356c
TI
3678{
3679 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3680 uinfo->count = 1;
3681 uinfo->value.integer.min = 0;
0dca1793 3682 uinfo->value.integer.max = 64;
763f356c
TI
3683 uinfo->value.integer.step = 1;
3684 return 0;
3685}
3686
98274f07
TI
3687static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3688 struct snd_ctl_elem_value *ucontrol)
763f356c 3689{
98274f07 3690 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 3691 int channel;
763f356c
TI
3692
3693 channel = ucontrol->id.index - 1;
3694
da3cec35
TI
3695 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3696 return -EINVAL;
763f356c 3697
763f356c
TI
3698 spin_lock_irq(&hdspm->lock);
3699 ucontrol->value.integer.value[0] =
0dca1793 3700 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
763f356c
TI
3701 spin_unlock_irq(&hdspm->lock);
3702
763f356c
TI
3703 return 0;
3704}
3705
98274f07
TI
3706static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3707 struct snd_ctl_elem_value *ucontrol)
763f356c 3708{
98274f07 3709 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c
TI
3710 int change;
3711 int channel;
763f356c
TI
3712 int gain;
3713
3714 if (!snd_hdspm_use_is_exclusive(hdspm))
3715 return -EBUSY;
3716
3717 channel = ucontrol->id.index - 1;
3718
da3cec35
TI
3719 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3720 return -EINVAL;
763f356c 3721
0dca1793 3722 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
763f356c
TI
3723
3724 spin_lock_irq(&hdspm->lock);
3725 change =
0dca1793
AK
3726 gain != hdspm_read_pb_gain(hdspm, channel,
3727 channel);
763f356c 3728 if (change)
0dca1793 3729 hdspm_write_pb_gain(hdspm, channel, channel,
763f356c
TI
3730 gain);
3731 spin_unlock_irq(&hdspm->lock);
3732 return change;
3733}
3734
0dca1793
AK
3735#define HDSPM_SYNC_CHECK(xname, xindex) \
3736{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3737 .name = xname, \
3738 .private_value = xindex, \
3739 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3740 .info = snd_hdspm_info_sync_check, \
3741 .get = snd_hdspm_get_sync_check \
763f356c
TI
3742}
3743
34542213
AK
3744#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3745{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3746 .name = xname, \
3747 .private_value = xindex, \
3748 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3749 .info = snd_hdspm_tco_info_lock_check, \
3750 .get = snd_hdspm_get_sync_check \
3751}
3752
3753
0dca1793 3754
98274f07
TI
3755static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3756 struct snd_ctl_elem_info *uinfo)
763f356c 3757{
38816545 3758 static const char *const texts[] = { "No Lock", "Lock", "Sync", "N/A" };
e5b7b1fe 3759 ENUMERATED_CTL_INFO(uinfo, texts);
763f356c
TI
3760 return 0;
3761}
3762
34542213
AK
3763static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3764 struct snd_ctl_elem_info *uinfo)
3765{
38816545 3766 static const char *const texts[] = { "No Lock", "Lock" };
34542213
AK
3767 ENUMERATED_CTL_INFO(uinfo, texts);
3768 return 0;
3769}
3770
0dca1793 3771static int hdspm_wc_sync_check(struct hdspm *hdspm)
763f356c 3772{
0dca1793
AK
3773 int status, status2;
3774
3775 switch (hdspm->io_type) {
3776 case AES32:
3777 status = hdspm_read(hdspm, HDSPM_statusRegister);
56bde0f3
AS
3778 if (status & HDSPM_AES32_wcLock) {
3779 if (status & HDSPM_AES32_wcSync)
3780 return 2;
3781 else
3782 return 1;
3783 }
3cee5a60 3784 return 0;
0dca1793
AK
3785 break;
3786
3787 case MADI:
3788 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3cee5a60
RB
3789 if (status2 & HDSPM_wcLock) {
3790 if (status2 & HDSPM_wcSync)
3791 return 2;
3792 else
3793 return 1;
3794 }
3795 return 0;
0dca1793 3796 break;
763f356c 3797
0dca1793
AK
3798 case RayDAT:
3799 case AIO:
3800 status = hdspm_read(hdspm, HDSPM_statusRegister);
763f356c 3801
0dca1793
AK
3802 if (status & 0x2000000)
3803 return 2;
3804 else if (status & 0x1000000)
3805 return 1;
3806 return 0;
763f356c 3807
0dca1793 3808 break;
763f356c 3809
0dca1793
AK
3810 case MADIface:
3811 break;
3812 }
3813
3814
3815 return 3;
763f356c
TI
3816}
3817
0dca1793
AK
3818
3819static int hdspm_madi_sync_check(struct hdspm *hdspm)
763f356c
TI
3820{
3821 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3822 if (status & HDSPM_madiLock) {
3823 if (status & HDSPM_madiSync)
3824 return 2;
3825 else
3826 return 1;
3827 }
3828 return 0;
3829}
3830
763f356c 3831
0dca1793
AK
3832static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3833{
3834 int status, lock, sync;
763f356c 3835
0dca1793 3836 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
763f356c 3837
0dca1793
AK
3838 lock = (status & (0x1<<idx)) ? 1 : 0;
3839 sync = (status & (0x100<<idx)) ? 1 : 0;
3cee5a60 3840
0dca1793 3841 if (lock && sync)
3cee5a60 3842 return 2;
0dca1793
AK
3843 else if (lock)
3844 return 1;
3cee5a60
RB
3845 return 0;
3846}
3847
0dca1793
AK
3848
3849static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3850{
3851 int status, lock = 0, sync = 0;
3852
3853 switch (hdspm->io_type) {
3854 case RayDAT:
3855 case AIO:
3856 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3857 lock = (status & 0x400) ? 1 : 0;
3858 sync = (status & 0x800) ? 1 : 0;
3859 break;
3860
3861 case MADI:
2e0452f5
AK
3862 status = hdspm_read(hdspm, HDSPM_statusRegister);
3863 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3864 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3865 break;
3866
0dca1793
AK
3867 case AES32:
3868 status = hdspm_read(hdspm, HDSPM_statusRegister2);
9a215f47
AK
3869 lock = (status & 0x100000) ? 1 : 0;
3870 sync = (status & 0x200000) ? 1 : 0;
0dca1793
AK
3871 break;
3872
3873 case MADIface:
3874 break;
3875 }
3876
3877 if (lock && sync)
3878 return 2;
3879 else if (lock)
3880 return 1;
3881
3882 return 0;
3883}
3884
3885static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3886{
3887 int status2, lock, sync;
3888 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3889
3890 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3891 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3892
3893 if (sync)
3894 return 2;
3895 else if (lock)
3896 return 1;
3897 return 0;
3898}
3899
34542213
AK
3900static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3901{
3902 u32 status;
3903 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3904
3905 return (status & mask) ? 1 : 0;
3906}
3907
0dca1793
AK
3908
3909static int hdspm_tco_sync_check(struct hdspm *hdspm)
3910{
3911 int status;
3912
3913 if (hdspm->tco) {
3914 switch (hdspm->io_type) {
3915 case MADI:
b0bf5504
AK
3916 status = hdspm_read(hdspm, HDSPM_statusRegister);
3917 if (status & HDSPM_tcoLockMadi) {
3918 if (status & HDSPM_tcoSync)
3919 return 2;
3920 else
3921 return 1;
3922 }
3923 return 0;
3924 break;
0dca1793
AK
3925 case AES32:
3926 status = hdspm_read(hdspm, HDSPM_statusRegister);
b0bf5504 3927 if (status & HDSPM_tcoLockAes) {
0dca1793
AK
3928 if (status & HDSPM_tcoSync)
3929 return 2;
3930 else
3931 return 1;
3932 }
3933 return 0;
3934
3935 break;
3936
3937 case RayDAT:
3938 case AIO:
3939 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3940
3941 if (status & 0x8000000)
3942 return 2; /* Sync */
3943 if (status & 0x4000000)
3944 return 1; /* Lock */
3945 return 0; /* No signal */
3946 break;
3947
3948 default:
3949 break;
3950 }
3951 }
3952
3953 return 3; /* N/A */
3954}
3955
3956
3957static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3958 struct snd_ctl_elem_value *ucontrol)
3959{
3960 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3961 int val = -1;
3962
3963 switch (hdspm->io_type) {
3964 case RayDAT:
3965 switch (kcontrol->private_value) {
3966 case 0: /* WC */
3967 val = hdspm_wc_sync_check(hdspm); break;
3968 case 7: /* TCO */
3969 val = hdspm_tco_sync_check(hdspm); break;
3970 case 8: /* SYNC IN */
3971 val = hdspm_sync_in_sync_check(hdspm); break;
3972 default:
d1a3c98d
AK
3973 val = hdspm_s1_sync_check(hdspm,
3974 kcontrol->private_value-1);
0dca1793 3975 }
fba30fd3 3976 break;
0dca1793
AK
3977
3978 case AIO:
3979 switch (kcontrol->private_value) {
3980 case 0: /* WC */
3981 val = hdspm_wc_sync_check(hdspm); break;
3982 case 4: /* TCO */
3983 val = hdspm_tco_sync_check(hdspm); break;
3984 case 5: /* SYNC IN */
3985 val = hdspm_sync_in_sync_check(hdspm); break;
3986 default:
1cb7dbf4
AK
3987 val = hdspm_s1_sync_check(hdspm,
3988 kcontrol->private_value-1);
0dca1793 3989 }
fba30fd3 3990 break;
0dca1793
AK
3991
3992 case MADI:
3993 switch (kcontrol->private_value) {
3994 case 0: /* WC */
3995 val = hdspm_wc_sync_check(hdspm); break;
3996 case 1: /* MADI */
3997 val = hdspm_madi_sync_check(hdspm); break;
3998 case 2: /* TCO */
3999 val = hdspm_tco_sync_check(hdspm); break;
4000 case 3: /* SYNC_IN */
4001 val = hdspm_sync_in_sync_check(hdspm); break;
4002 }
fba30fd3 4003 break;
0dca1793
AK
4004
4005 case MADIface:
4006 val = hdspm_madi_sync_check(hdspm); /* MADI */
4007 break;
4008
4009 case AES32:
4010 switch (kcontrol->private_value) {
4011 case 0: /* WC */
4012 val = hdspm_wc_sync_check(hdspm); break;
4013 case 9: /* TCO */
4014 val = hdspm_tco_sync_check(hdspm); break;
4015 case 10 /* SYNC IN */:
4016 val = hdspm_sync_in_sync_check(hdspm); break;
7c4a95b5 4017 default: /* AES1 to AES8 */
0dca1793 4018 val = hdspm_aes_sync_check(hdspm,
7c4a95b5 4019 kcontrol->private_value-1);
0dca1793 4020 }
fba30fd3 4021 break;
0dca1793
AK
4022
4023 }
4024
34542213
AK
4025 if (hdspm->tco) {
4026 switch (kcontrol->private_value) {
4027 case 11:
4028 /* Check TCO for lock state of its current input */
4029 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
4030 break;
4031 case 12:
4032 /* Check TCO for valid time code on LTC input. */
4033 val = hdspm_tco_input_check(hdspm,
4034 HDSPM_TCO1_LTC_Input_valid);
4035 break;
4036 default:
4037 break;
4038 }
4039 }
4040
0dca1793
AK
4041 if (-1 == val)
4042 val = 3;
4043
4044 ucontrol->value.enumerated.item[0] = val;
4045 return 0;
4046}
4047
4048
4049
4050/**
4051 * TCO controls
4052 **/
4053static void hdspm_tco_write(struct hdspm *hdspm)
4054{
4055 unsigned int tc[4] = { 0, 0, 0, 0};
4056
4057 switch (hdspm->tco->input) {
4058 case 0:
4059 tc[2] |= HDSPM_TCO2_set_input_MSB;
4060 break;
4061 case 1:
4062 tc[2] |= HDSPM_TCO2_set_input_LSB;
4063 break;
4064 default:
4065 break;
4066 }
4067
4068 switch (hdspm->tco->framerate) {
4069 case 1:
4070 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
4071 break;
4072 case 2:
4073 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
4074 break;
4075 case 3:
4076 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
4077 HDSPM_TCO1_set_drop_frame_flag;
4078 break;
4079 case 4:
4080 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4081 HDSPM_TCO1_LTC_Format_MSB;
4082 break;
4083 case 5:
4084 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4085 HDSPM_TCO1_LTC_Format_MSB +
4086 HDSPM_TCO1_set_drop_frame_flag;
4087 break;
4088 default:
4089 break;
4090 }
4091
4092 switch (hdspm->tco->wordclock) {
4093 case 1:
4094 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
4095 break;
4096 case 2:
4097 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4098 break;
4099 default:
4100 break;
4101 }
4102
4103 switch (hdspm->tco->samplerate) {
4104 case 1:
4105 tc[2] |= HDSPM_TCO2_set_freq;
4106 break;
4107 case 2:
4108 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4109 break;
4110 default:
4111 break;
4112 }
4113
4114 switch (hdspm->tco->pull) {
4115 case 1:
4116 tc[2] |= HDSPM_TCO2_set_pull_up;
4117 break;
4118 case 2:
4119 tc[2] |= HDSPM_TCO2_set_pull_down;
4120 break;
4121 case 3:
4122 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4123 break;
4124 case 4:
4125 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4126 break;
4127 default:
4128 break;
4129 }
4130
4131 if (1 == hdspm->tco->term) {
4132 tc[2] |= HDSPM_TCO2_set_term_75R;
4133 }
4134
4135 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4136 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4137 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4138 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4139}
4140
4141
4142#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4143{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4144 .name = xname, \
4145 .index = xindex, \
4146 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4147 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4148 .info = snd_hdspm_info_tco_sample_rate, \
4149 .get = snd_hdspm_get_tco_sample_rate, \
4150 .put = snd_hdspm_put_tco_sample_rate \
4151}
4152
4153static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4154 struct snd_ctl_elem_info *uinfo)
4155{
38816545 4156 static const char *const texts[] = { "44.1 kHz", "48 kHz" };
e5b7b1fe 4157 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4158 return 0;
4159}
4160
4161static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4162 struct snd_ctl_elem_value *ucontrol)
4163{
4164 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4165
4166 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4167
4168 return 0;
4169}
4170
4171static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4172 struct snd_ctl_elem_value *ucontrol)
4173{
4174 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4175
4176 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4177 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4178
4179 hdspm_tco_write(hdspm);
4180
4181 return 1;
4182 }
4183
4184 return 0;
4185}
4186
4187
4188#define HDSPM_TCO_PULL(xname, xindex) \
4189{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4190 .name = xname, \
4191 .index = xindex, \
4192 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4193 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4194 .info = snd_hdspm_info_tco_pull, \
4195 .get = snd_hdspm_get_tco_pull, \
4196 .put = snd_hdspm_put_tco_pull \
4197}
4198
4199static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4200 struct snd_ctl_elem_info *uinfo)
4201{
38816545
AK
4202 static const char *const texts[] = { "0", "+ 0.1 %", "- 0.1 %",
4203 "+ 4 %", "- 4 %" };
e5b7b1fe 4204 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4205 return 0;
4206}
4207
4208static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4209 struct snd_ctl_elem_value *ucontrol)
4210{
4211 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4212
4213 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4214
4215 return 0;
4216}
4217
4218static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4219 struct snd_ctl_elem_value *ucontrol)
4220{
4221 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4222
4223 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4224 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4225
4226 hdspm_tco_write(hdspm);
4227
4228 return 1;
4229 }
4230
4231 return 0;
4232}
4233
4234#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4235{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4236 .name = xname, \
4237 .index = xindex, \
4238 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4239 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4240 .info = snd_hdspm_info_tco_wck_conversion, \
4241 .get = snd_hdspm_get_tco_wck_conversion, \
4242 .put = snd_hdspm_put_tco_wck_conversion \
4243}
4244
4245static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4246 struct snd_ctl_elem_info *uinfo)
4247{
38816545 4248 static const char *const texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
e5b7b1fe 4249 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4250 return 0;
4251}
4252
4253static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4254 struct snd_ctl_elem_value *ucontrol)
4255{
4256 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4257
4258 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4259
4260 return 0;
4261}
4262
4263static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4264 struct snd_ctl_elem_value *ucontrol)
4265{
4266 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4267
4268 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4269 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4270
4271 hdspm_tco_write(hdspm);
4272
4273 return 1;
4274 }
4275
4276 return 0;
4277}
4278
4279
4280#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4281{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4282 .name = xname, \
4283 .index = xindex, \
4284 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4285 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4286 .info = snd_hdspm_info_tco_frame_rate, \
4287 .get = snd_hdspm_get_tco_frame_rate, \
4288 .put = snd_hdspm_put_tco_frame_rate \
4289}
4290
4291static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4292 struct snd_ctl_elem_info *uinfo)
4293{
38816545 4294 static const char *const texts[] = { "24 fps", "25 fps", "29.97fps",
0dca1793 4295 "29.97 dfps", "30 fps", "30 dfps" };
e5b7b1fe 4296 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4297 return 0;
4298}
4299
4300static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
3cee5a60
RB
4301 struct snd_ctl_elem_value *ucontrol)
4302{
3cee5a60
RB
4303 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4304
0dca1793 4305 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
3cee5a60 4306
3cee5a60
RB
4307 return 0;
4308}
763f356c 4309
0dca1793
AK
4310static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4311 struct snd_ctl_elem_value *ucontrol)
4312{
4313 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
763f356c 4314
0dca1793
AK
4315 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4316 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
763f356c 4317
0dca1793
AK
4318 hdspm_tco_write(hdspm);
4319
4320 return 1;
4321 }
4322
4323 return 0;
4324}
763f356c 4325
0dca1793
AK
4326
4327#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4328{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4329 .name = xname, \
4330 .index = xindex, \
4331 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4332 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4333 .info = snd_hdspm_info_tco_sync_source, \
4334 .get = snd_hdspm_get_tco_sync_source, \
4335 .put = snd_hdspm_put_tco_sync_source \
4336}
4337
4338static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4339 struct snd_ctl_elem_info *uinfo)
4340{
38816545 4341 static const char *const texts[] = { "LTC", "Video", "WCK" };
e5b7b1fe 4342 ENUMERATED_CTL_INFO(uinfo, texts);
0dca1793
AK
4343 return 0;
4344}
4345
4346static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4347 struct snd_ctl_elem_value *ucontrol)
4348{
4349 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4350
4351 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4352
4353 return 0;
4354}
4355
4356static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4357 struct snd_ctl_elem_value *ucontrol)
4358{
4359 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4360
4361 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4362 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4363
4364 hdspm_tco_write(hdspm);
4365
4366 return 1;
4367 }
4368
4369 return 0;
4370}
4371
4372
4373#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4374{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4375 .name = xname, \
4376 .index = xindex, \
4377 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4378 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4379 .info = snd_hdspm_info_tco_word_term, \
4380 .get = snd_hdspm_get_tco_word_term, \
4381 .put = snd_hdspm_put_tco_word_term \
4382}
4383
4384static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4385 struct snd_ctl_elem_info *uinfo)
4386{
4387 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4388 uinfo->count = 1;
4389 uinfo->value.integer.min = 0;
4390 uinfo->value.integer.max = 1;
4391
4392 return 0;
4393}
4394
4395
4396static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4397 struct snd_ctl_elem_value *ucontrol)
4398{
4399 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4400
4401 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4402
4403 return 0;
4404}
4405
4406
4407static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4408 struct snd_ctl_elem_value *ucontrol)
4409{
4410 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4411
4412 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4413 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4414
4415 hdspm_tco_write(hdspm);
4416
4417 return 1;
4418 }
4419
4420 return 0;
4421}
4422
4423
4424
4425
4426static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
4427 HDSPM_MIXER("Mixer", 0),
4428 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
763f356c
TI
4429 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4430 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4431 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4432 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
b8812c55 4433 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
0dca1793
AK
4434 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4435 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
930f4ff0 4436 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
0dca1793 4437 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
c9e1668c
AK
4438 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4439 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
696be0fb 4440 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
c9e1668c
AK
4441 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4442 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
700d1ef3
AK
4443 HDSPM_INPUT_SELECT("Input Select", 0),
4444 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
0dca1793
AK
4445};
4446
4447
4448static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4449 HDSPM_MIXER("Mixer", 0),
4450 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4451 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4452 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4453 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4454 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
c9e1668c
AK
4455 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4456 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4457 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
700d1ef3 4458 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
763f356c
TI
4459};
4460
0dca1793
AK
4461static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
4462 HDSPM_MIXER("Mixer", 0),
4463 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4464 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4465 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
0dca1793
AK
4466 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4467 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4468 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4469 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4470 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4471 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4472 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4473 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4474 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4475 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4476 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4477 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4478 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
fb0f121e 4479 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
42f4c12d 4480 HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
fb0f121e
AK
4481 HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
4482 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4483 HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
4484 HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
42f4c12d
AK
4485 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
4486 HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
4487 HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
4488 HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
0dca1793
AK
4489
4490 /*
4491 HDSPM_INPUT_SELECT("Input Select", 0),
4492 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4493 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4494 HDSPM_SPDIF_IN("SPDIF In", 0);
4495 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4496 HDSPM_INPUT_LEVEL("Input Level", 0);
4497 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4498 HDSPM_PHONES("Phones", 0);
4499 */
4500};
3cee5a60 4501
0dca1793
AK
4502static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4503 HDSPM_MIXER("Mixer", 0),
4504 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4505 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4506 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4507 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4508 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4509 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4510 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4511 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4512 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4513 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4514 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4515 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4516 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4517 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4518 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4519 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4520 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4521 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4522 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4523 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4524 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
11a5cd3c
AK
4525 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
4526 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4527 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
0dca1793
AK
4528};
4529
4530static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
3cee5a60 4531 HDSPM_MIXER("Mixer", 0),
0dca1793 4532 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
3cee5a60
RB
4533 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4534 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4535 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4536 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
2d63ec38 4537 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 11),
0dca1793
AK
4538 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4539 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4540 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4541 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4542 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4543 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4544 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4545 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4546 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4547 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4548 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4549 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4550 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4551 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4552 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4553 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4554 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4555 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4556 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4557 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4558 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4559 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
c9e1668c
AK
4560 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4561 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4562 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4563 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4564 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
3cee5a60
RB
4565 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4566 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4567};
4568
0dca1793
AK
4569
4570
4571/* Control elements for the optional TCO module */
4572static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4573 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4574 HDSPM_TCO_PULL("TCO Pull", 0),
4575 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4576 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4577 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
a817650e
AK
4578 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4579 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4580 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4581 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4582 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
0dca1793
AK
4583};
4584
4585
98274f07 4586static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
763f356c
TI
4587
4588
98274f07 4589static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
763f356c
TI
4590{
4591 int i;
4592
0dca1793 4593 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
763f356c
TI
4594 if (hdspm->system_sample_rate > 48000) {
4595 hdspm->playback_mixer_ctls[i]->vd[0].access =
0dca1793
AK
4596 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4597 SNDRV_CTL_ELEM_ACCESS_READ |
4598 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
763f356c
TI
4599 } else {
4600 hdspm->playback_mixer_ctls[i]->vd[0].access =
0dca1793
AK
4601 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4602 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
763f356c
TI
4603 }
4604 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
0dca1793
AK
4605 SNDRV_CTL_EVENT_MASK_INFO,
4606 &hdspm->playback_mixer_ctls[i]->id);
763f356c
TI
4607 }
4608
4609 return 0;
4610}
4611
4612
0dca1793
AK
4613static int snd_hdspm_create_controls(struct snd_card *card,
4614 struct hdspm *hdspm)
763f356c
TI
4615{
4616 unsigned int idx, limit;
4617 int err;
98274f07 4618 struct snd_kcontrol *kctl;
0dca1793 4619 struct snd_kcontrol_new *list = NULL;
763f356c 4620
0dca1793
AK
4621 switch (hdspm->io_type) {
4622 case MADI:
4623 list = snd_hdspm_controls_madi;
4624 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4625 break;
4626 case MADIface:
4627 list = snd_hdspm_controls_madiface;
4628 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4629 break;
4630 case AIO:
4631 list = snd_hdspm_controls_aio;
4632 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4633 break;
4634 case RayDAT:
4635 list = snd_hdspm_controls_raydat;
4636 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4637 break;
4638 case AES32:
4639 list = snd_hdspm_controls_aes32;
4640 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4641 break;
4642 }
3cee5a60 4643
0dca1793
AK
4644 if (NULL != list) {
4645 for (idx = 0; idx < limit; idx++) {
3cee5a60 4646 err = snd_ctl_add(card,
0dca1793 4647 snd_ctl_new1(&list[idx], hdspm));
3cee5a60
RB
4648 if (err < 0)
4649 return err;
763f356c
TI
4650 }
4651 }
4652
763f356c 4653
0dca1793 4654 /* create simple 1:1 playback mixer controls */
763f356c 4655 snd_hdspm_playback_mixer.name = "Chn";
0dca1793
AK
4656 if (hdspm->system_sample_rate >= 128000) {
4657 limit = hdspm->qs_out_channels;
4658 } else if (hdspm->system_sample_rate >= 64000) {
4659 limit = hdspm->ds_out_channels;
4660 } else {
4661 limit = hdspm->ss_out_channels;
4662 }
763f356c
TI
4663 for (idx = 0; idx < limit; ++idx) {
4664 snd_hdspm_playback_mixer.index = idx + 1;
ef5fa1a4
TI
4665 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4666 err = snd_ctl_add(card, kctl);
4667 if (err < 0)
763f356c 4668 return err;
763f356c
TI
4669 hdspm->playback_mixer_ctls[idx] = kctl;
4670 }
4671
0dca1793
AK
4672
4673 if (hdspm->tco) {
4674 /* add tco control elements */
4675 list = snd_hdspm_controls_tco;
4676 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4677 for (idx = 0; idx < limit; idx++) {
4678 err = snd_ctl_add(card,
4679 snd_ctl_new1(&list[idx], hdspm));
4680 if (err < 0)
4681 return err;
4682 }
4683 }
4684
763f356c
TI
4685 return 0;
4686}
4687
4688/*------------------------------------------------------------
0dca1793 4689 /proc interface
763f356c
TI
4690 ------------------------------------------------------------*/
4691
4692static void
5760107c
AK
4693snd_hdspm_proc_read_tco(struct snd_info_entry *entry,
4694 struct snd_info_buffer *buffer)
763f356c 4695{
ef5fa1a4 4696 struct hdspm *hdspm = entry->private_data;
5760107c 4697 unsigned int status, control;
0dca1793
AK
4698 int a, ltc, frames, seconds, minutes, hours;
4699 unsigned int period;
4700 u64 freq_const = 0;
4701 u32 rate;
4702
5760107c
AK
4703 snd_iprintf(buffer, "--- TCO ---\n");
4704
763f356c 4705 status = hdspm_read(hdspm, HDSPM_statusRegister);
0dca1793 4706 control = hdspm->control_register;
763f356c 4707
763f356c 4708
0dca1793
AK
4709 if (status & HDSPM_tco_detect) {
4710 snd_iprintf(buffer, "TCO module detected.\n");
4711 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4712 if (a & HDSPM_TCO1_LTC_Input_valid) {
4713 snd_iprintf(buffer, " LTC valid, ");
4714 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4715 HDSPM_TCO1_LTC_Format_MSB)) {
4716 case 0:
4717 snd_iprintf(buffer, "24 fps, ");
4718 break;
4719 case HDSPM_TCO1_LTC_Format_LSB:
4720 snd_iprintf(buffer, "25 fps, ");
4721 break;
4722 case HDSPM_TCO1_LTC_Format_MSB:
4723 snd_iprintf(buffer, "29.97 fps, ");
4724 break;
4725 default:
4726 snd_iprintf(buffer, "30 fps, ");
4727 break;
4728 }
4729 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4730 snd_iprintf(buffer, "drop frame\n");
4731 } else {
4732 snd_iprintf(buffer, "full frame\n");
4733 }
4734 } else {
4735 snd_iprintf(buffer, " no LTC\n");
4736 }
4737 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4738 snd_iprintf(buffer, " Video: NTSC\n");
4739 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4740 snd_iprintf(buffer, " Video: PAL\n");
4741 } else {
4742 snd_iprintf(buffer, " No video\n");
4743 }
4744 if (a & HDSPM_TCO1_TCO_lock) {
4745 snd_iprintf(buffer, " Sync: lock\n");
4746 } else {
4747 snd_iprintf(buffer, " Sync: no lock\n");
4748 }
4749
4750 switch (hdspm->io_type) {
4751 case MADI:
4752 case AES32:
4753 freq_const = 110069313433624ULL;
4754 break;
4755 case RayDAT:
4756 case AIO:
4757 freq_const = 104857600000000ULL;
4758 break;
4759 case MADIface:
4760 break; /* no TCO possible */
4761 }
4762
4763 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4764 snd_iprintf(buffer, " period: %u\n", period);
4765
4766
4767 /* rate = freq_const/period; */
4768 rate = div_u64(freq_const, period);
4769
4770 if (control & HDSPM_QuadSpeed) {
4771 rate *= 4;
4772 } else if (control & HDSPM_DoubleSpeed) {
4773 rate *= 2;
4774 }
4775
4776 snd_iprintf(buffer, " Frequency: %u Hz\n",
4777 (unsigned int) rate);
4778
4779 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4780 frames = ltc & 0xF;
4781 ltc >>= 4;
4782 frames += (ltc & 0x3) * 10;
4783 ltc >>= 4;
4784 seconds = ltc & 0xF;
4785 ltc >>= 4;
4786 seconds += (ltc & 0x7) * 10;
4787 ltc >>= 4;
4788 minutes = ltc & 0xF;
4789 ltc >>= 4;
4790 minutes += (ltc & 0x7) * 10;
4791 ltc >>= 4;
4792 hours = ltc & 0xF;
4793 ltc >>= 4;
4794 hours += (ltc & 0x3) * 10;
4795 snd_iprintf(buffer,
4796 " LTC In: %02d:%02d:%02d:%02d\n",
4797 hours, minutes, seconds, frames);
4798
4799 } else {
4800 snd_iprintf(buffer, "No TCO module detected.\n");
4801 }
5760107c
AK
4802}
4803
4804static void
4805snd_hdspm_proc_read_madi(struct snd_info_entry *entry,
4806 struct snd_info_buffer *buffer)
4807{
4808 struct hdspm *hdspm = entry->private_data;
4809 unsigned int status, status2, control, freq;
4810
4811 char *pref_sync_ref;
4812 char *autosync_ref;
4813 char *system_clock_mode;
4814 char *insel;
4815 int x, x2;
4816
4817 status = hdspm_read(hdspm, HDSPM_statusRegister);
4818 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4819 control = hdspm->control_register;
4820 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
4821
4822 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
4823 hdspm->card_name, hdspm->card->number + 1,
4824 hdspm->firmware_rev,
4825 (status2 & HDSPM_version0) |
4826 (status2 & HDSPM_version1) | (status2 &
4827 HDSPM_version2));
4828
4829 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4830 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
4831 hdspm->serial);
4832
4833 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4834 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4835
4836 snd_iprintf(buffer, "--- System ---\n");
4837
4838 snd_iprintf(buffer,
4839 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4840 status & HDSPM_audioIRQPending,
4841 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4842 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4843 hdspm->irq_count);
4844 snd_iprintf(buffer,
4845 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4846 "estimated= %ld (bytes)\n",
4847 ((status & HDSPM_BufferID) ? 1 : 0),
4848 (status & HDSPM_BufferPositionMask),
4849 (status & HDSPM_BufferPositionMask) %
4850 (2 * (int)hdspm->period_bytes),
4851 ((status & HDSPM_BufferPositionMask) - 64) %
4852 (2 * (int)hdspm->period_bytes),
4853 (long) hdspm_hw_pointer(hdspm) * 4);
4854
4855 snd_iprintf(buffer,
4856 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4857 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4858 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4859 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4860 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4861 snd_iprintf(buffer,
4862 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4863 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4864 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4865 snd_iprintf(buffer,
4866 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4867 "status2=0x%x\n",
4868 hdspm->control_register, hdspm->control2_register,
4869 status, status2);
4870
763f356c
TI
4871
4872 snd_iprintf(buffer, "--- Settings ---\n");
4873
7cb155ff 4874 x = hdspm_get_latency(hdspm);
763f356c
TI
4875
4876 snd_iprintf(buffer,
0dca1793
AK
4877 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4878 x, (unsigned long) hdspm->period_bytes);
763f356c 4879
0dca1793
AK
4880 snd_iprintf(buffer, "Line out: %s\n",
4881 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
763f356c
TI
4882
4883 switch (hdspm->control_register & HDSPM_InputMask) {
4884 case HDSPM_InputOptical:
4885 insel = "Optical";
4886 break;
4887 case HDSPM_InputCoaxial:
4888 insel = "Coaxial";
4889 break;
4890 default:
ec8f53fb 4891 insel = "Unknown";
763f356c 4892 }
763f356c
TI
4893
4894 snd_iprintf(buffer,
0dca1793
AK
4895 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4896 "Auto Input %s\n",
4897 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4898 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4899 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
4900
763f356c 4901
3cee5a60 4902 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
0dca1793 4903 system_clock_mode = "AutoSync";
3cee5a60 4904 else
763f356c 4905 system_clock_mode = "Master";
0dca1793 4906 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
763f356c
TI
4907
4908 switch (hdspm_pref_sync_ref(hdspm)) {
4909 case HDSPM_SYNC_FROM_WORD:
4910 pref_sync_ref = "Word Clock";
4911 break;
4912 case HDSPM_SYNC_FROM_MADI:
4913 pref_sync_ref = "MADI Sync";
4914 break;
0dca1793
AK
4915 case HDSPM_SYNC_FROM_TCO:
4916 pref_sync_ref = "TCO";
4917 break;
4918 case HDSPM_SYNC_FROM_SYNC_IN:
4919 pref_sync_ref = "Sync In";
4920 break;
763f356c
TI
4921 default:
4922 pref_sync_ref = "XXXX Clock";
4923 break;
4924 }
4925 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
0dca1793 4926 pref_sync_ref);
763f356c
TI
4927
4928 snd_iprintf(buffer, "System Clock Frequency: %d\n",
0dca1793 4929 hdspm->system_sample_rate);
763f356c
TI
4930
4931
4932 snd_iprintf(buffer, "--- Status:\n");
4933
4934 x = status & HDSPM_madiSync;
4935 x2 = status2 & HDSPM_wcSync;
4936
4937 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
0dca1793
AK
4938 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4939 "NoLock",
4940 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4941 "NoLock");
763f356c
TI
4942
4943 switch (hdspm_autosync_ref(hdspm)) {
0dca1793
AK
4944 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4945 autosync_ref = "Sync In";
4946 break;
4947 case HDSPM_AUTOSYNC_FROM_TCO:
4948 autosync_ref = "TCO";
4949 break;
763f356c
TI
4950 case HDSPM_AUTOSYNC_FROM_WORD:
4951 autosync_ref = "Word Clock";
4952 break;
4953 case HDSPM_AUTOSYNC_FROM_MADI:
4954 autosync_ref = "MADI Sync";
4955 break;
4956 case HDSPM_AUTOSYNC_FROM_NONE:
4957 autosync_ref = "Input not valid";
4958 break;
4959 default:
4960 autosync_ref = "---";
4961 break;
4962 }
4963 snd_iprintf(buffer,
0dca1793
AK
4964 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4965 autosync_ref, hdspm_external_sample_rate(hdspm),
4966 (status & HDSPM_madiFreqMask) >> 22,
4967 (status2 & HDSPM_wcFreqMask) >> 5);
763f356c
TI
4968
4969 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
0dca1793
AK
4970 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4971 (status & HDSPM_RX_64ch) ? "64 channels" :
4972 "56 channels");
763f356c 4973
5760107c
AK
4974 /* call readout function for TCO specific status */
4975 snd_hdspm_proc_read_tco(entry, buffer);
4976
763f356c
TI
4977 snd_iprintf(buffer, "\n");
4978}
4979
3cee5a60
RB
4980static void
4981snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4982 struct snd_info_buffer *buffer)
4983{
ef5fa1a4 4984 struct hdspm *hdspm = entry->private_data;
3cee5a60
RB
4985 unsigned int status;
4986 unsigned int status2;
4987 unsigned int timecode;
56bde0f3 4988 unsigned int wcLock, wcSync;
3cee5a60
RB
4989 int pref_syncref;
4990 char *autosync_ref;
3cee5a60
RB
4991 int x;
4992
4993 status = hdspm_read(hdspm, HDSPM_statusRegister);
4994 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4995 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4996
4997 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4998 hdspm->card_name, hdspm->card->number + 1,
4999 hdspm->firmware_rev);
5000
5001 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
5002 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
5003
5004 snd_iprintf(buffer, "--- System ---\n");
5005
5006 snd_iprintf(buffer,
5007 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
5008 status & HDSPM_audioIRQPending,
5009 (status & HDSPM_midi0IRQPending) ? 1 : 0,
5010 (status & HDSPM_midi1IRQPending) ? 1 : 0,
5011 hdspm->irq_count);
5012 snd_iprintf(buffer,
ef5fa1a4
TI
5013 "HW pointer: id = %d, rawptr = %d (%d->%d) "
5014 "estimated= %ld (bytes)\n",
3cee5a60
RB
5015 ((status & HDSPM_BufferID) ? 1 : 0),
5016 (status & HDSPM_BufferPositionMask),
ef5fa1a4
TI
5017 (status & HDSPM_BufferPositionMask) %
5018 (2 * (int)hdspm->period_bytes),
5019 ((status & HDSPM_BufferPositionMask) - 64) %
5020 (2 * (int)hdspm->period_bytes),
3cee5a60
RB
5021 (long) hdspm_hw_pointer(hdspm) * 4);
5022
5023 snd_iprintf(buffer,
5024 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
5025 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
5026 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
5027 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
5028 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
5029 snd_iprintf(buffer,
0dca1793
AK
5030 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
5031 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
5032 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
5033 snd_iprintf(buffer,
5034 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
5035 "status2=0x%x\n",
5036 hdspm->control_register, hdspm->control2_register,
5037 status, status2);
3cee5a60
RB
5038
5039 snd_iprintf(buffer, "--- Settings ---\n");
5040
7cb155ff 5041 x = hdspm_get_latency(hdspm);
3cee5a60
RB
5042
5043 snd_iprintf(buffer,
5044 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
5045 x, (unsigned long) hdspm->period_bytes);
5046
0dca1793 5047 snd_iprintf(buffer, "Line out: %s\n",
3cee5a60 5048 (hdspm->
0dca1793 5049 control_register & HDSPM_LineOut) ? "on " : "off");
3cee5a60
RB
5050
5051 snd_iprintf(buffer,
5052 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
5053 (hdspm->
5054 control_register & HDSPM_clr_tms) ? "on" : "off",
5055 (hdspm->
5056 control_register & HDSPM_Emphasis) ? "on" : "off",
5057 (hdspm->
5058 control_register & HDSPM_Dolby) ? "on" : "off");
5059
3cee5a60
RB
5060
5061 pref_syncref = hdspm_pref_sync_ref(hdspm);
5062 if (pref_syncref == 0)
5063 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
5064 else
5065 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
5066 pref_syncref);
5067
5068 snd_iprintf(buffer, "System Clock Frequency: %d\n",
5069 hdspm->system_sample_rate);
5070
5071 snd_iprintf(buffer, "Double speed: %s\n",
5072 hdspm->control_register & HDSPM_DS_DoubleWire?
5073 "Double wire" : "Single wire");
5074 snd_iprintf(buffer, "Quad speed: %s\n",
5075 hdspm->control_register & HDSPM_QS_DoubleWire?
5076 "Double wire" :
5077 hdspm->control_register & HDSPM_QS_QuadWire?
5078 "Quad wire" : "Single wire");
5079
5080 snd_iprintf(buffer, "--- Status:\n");
5081
56bde0f3
AS
5082 wcLock = status & HDSPM_AES32_wcLock;
5083 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
5084
3cee5a60 5085 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
56bde0f3 5086 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
ef5fa1a4 5087 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
3cee5a60
RB
5088
5089 for (x = 0; x < 8; x++) {
5090 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
ef5fa1a4
TI
5091 x+1,
5092 (status2 & (HDSPM_LockAES >> x)) ?
0dca1793 5093 "Sync " : "No Lock",
ef5fa1a4 5094 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
3cee5a60
RB
5095 }
5096
5097 switch (hdspm_autosync_ref(hdspm)) {
0dca1793
AK
5098 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5099 autosync_ref = "None"; break;
5100 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5101 autosync_ref = "Word Clock"; break;
5102 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5103 autosync_ref = "AES1"; break;
5104 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5105 autosync_ref = "AES2"; break;
5106 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5107 autosync_ref = "AES3"; break;
5108 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5109 autosync_ref = "AES4"; break;
5110 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5111 autosync_ref = "AES5"; break;
5112 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5113 autosync_ref = "AES6"; break;
5114 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5115 autosync_ref = "AES7"; break;
5116 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5117 autosync_ref = "AES8"; break;
194062da
AK
5118 case HDSPM_AES32_AUTOSYNC_FROM_TCO:
5119 autosync_ref = "TCO"; break;
5120 case HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN:
5121 autosync_ref = "Sync In"; break;
0dca1793
AK
5122 default:
5123 autosync_ref = "---"; break;
3cee5a60
RB
5124 }
5125 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5126
194062da
AK
5127 /* call readout function for TCO specific status */
5128 snd_hdspm_proc_read_tco(entry, buffer);
5129
3cee5a60
RB
5130 snd_iprintf(buffer, "\n");
5131}
5132
0dca1793
AK
5133static void
5134snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5135 struct snd_info_buffer *buffer)
5136{
5137 struct hdspm *hdspm = entry->private_data;
5138 unsigned int status1, status2, status3, control, i;
5139 unsigned int lock, sync;
5140
5141 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5142 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5143 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5144
5145 control = hdspm->control_register;
5146
5147 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5148 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5149 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5150
5151
5152 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5153
5154 snd_iprintf(buffer, "Clock mode : %s\n",
5155 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5156 snd_iprintf(buffer, "System frequency: %d Hz\n",
5157 hdspm_get_system_sample_rate(hdspm));
5158
5159 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5160
5161 lock = 0x1;
5162 sync = 0x100;
5163
5164 for (i = 0; i < 8; i++) {
5165 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5166 i,
5167 (status1 & lock) ? 1 : 0,
5168 (status1 & sync) ? 1 : 0,
5169 texts_freq[(status2 >> (i * 4)) & 0xF]);
5170
5171 lock = lock<<1;
5172 sync = sync<<1;
5173 }
5174
5175 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5176 (status1 & 0x1000000) ? 1 : 0,
5177 (status1 & 0x2000000) ? 1 : 0,
5178 texts_freq[(status1 >> 16) & 0xF]);
5179
5180 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5181 (status1 & 0x4000000) ? 1 : 0,
5182 (status1 & 0x8000000) ? 1 : 0,
5183 texts_freq[(status1 >> 20) & 0xF]);
5184
5185 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5186 (status3 & 0x400) ? 1 : 0,
5187 (status3 & 0x800) ? 1 : 0,
5188 texts_freq[(status2 >> 12) & 0xF]);
5189
5190}
5191
3cee5a60
RB
5192#ifdef CONFIG_SND_DEBUG
5193static void
0dca1793 5194snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
3cee5a60
RB
5195 struct snd_info_buffer *buffer)
5196{
ef5fa1a4 5197 struct hdspm *hdspm = entry->private_data;
3cee5a60
RB
5198
5199 int j,i;
5200
ef5fa1a4 5201 for (i = 0; i < 256 /* 1024*64 */; i += j) {
3cee5a60
RB
5202 snd_iprintf(buffer, "0x%08X: ", i);
5203 for (j = 0; j < 16; j += 4)
5204 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5205 snd_iprintf(buffer, "\n");
5206 }
5207}
5208#endif
5209
5210
0dca1793
AK
5211static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5212 struct snd_info_buffer *buffer)
5213{
5214 struct hdspm *hdspm = entry->private_data;
5215 int i;
5216
5217 snd_iprintf(buffer, "# generated by hdspm\n");
5218
5219 for (i = 0; i < hdspm->max_channels_in; i++) {
5220 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5221 }
5222}
5223
5224static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5225 struct snd_info_buffer *buffer)
5226{
5227 struct hdspm *hdspm = entry->private_data;
5228 int i;
5229
5230 snd_iprintf(buffer, "# generated by hdspm\n");
5231
5232 for (i = 0; i < hdspm->max_channels_out; i++) {
5233 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5234 }
5235}
5236
3cee5a60 5237
e23e7a14 5238static void snd_hdspm_proc_init(struct hdspm *hdspm)
763f356c 5239{
98274f07 5240 struct snd_info_entry *entry;
763f356c 5241
0dca1793
AK
5242 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5243 switch (hdspm->io_type) {
5244 case AES32:
5245 snd_info_set_text_ops(entry, hdspm,
5246 snd_hdspm_proc_read_aes32);
5247 break;
5248 case MADI:
5249 snd_info_set_text_ops(entry, hdspm,
5250 snd_hdspm_proc_read_madi);
5251 break;
5252 case MADIface:
5253 /* snd_info_set_text_ops(entry, hdspm,
5254 snd_hdspm_proc_read_madiface); */
5255 break;
5256 case RayDAT:
5257 snd_info_set_text_ops(entry, hdspm,
5258 snd_hdspm_proc_read_raydat);
5259 break;
5260 case AIO:
5261 break;
5262 }
5263 }
5264
5265 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5266 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5267 }
5268
5269 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5270 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5271 }
5272
3cee5a60
RB
5273#ifdef CONFIG_SND_DEBUG
5274 /* debug file to read all hdspm registers */
5275 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5276 snd_info_set_text_ops(entry, hdspm,
5277 snd_hdspm_proc_read_debug);
5278#endif
763f356c
TI
5279}
5280
5281/*------------------------------------------------------------
0dca1793 5282 hdspm intitialize
763f356c
TI
5283 ------------------------------------------------------------*/
5284
98274f07 5285static int snd_hdspm_set_defaults(struct hdspm * hdspm)
763f356c 5286{
763f356c 5287 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
561de31a 5288 hold it (e.g. during module initialization).
0dca1793 5289 */
763f356c
TI
5290
5291 /* set defaults: */
5292
0dca1793
AK
5293 hdspm->settings_register = 0;
5294
5295 switch (hdspm->io_type) {
5296 case MADI:
5297 case MADIface:
5298 hdspm->control_register =
5299 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5300 break;
5301
5302 case RayDAT:
5303 case AIO:
5304 hdspm->settings_register = 0x1 + 0x1000;
5305 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5306 * line_out */
5307 hdspm->control_register =
5308 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5309 break;
5310
5311 case AES32:
ef5fa1a4 5312 hdspm->control_register =
e71b95ad 5313 HDSPM_ClockModeMaster | /* Master Clock Mode on */
0dca1793 5314 hdspm_encode_latency(7) | /* latency max=8192samples */
3cee5a60
RB
5315 HDSPM_SyncRef0 | /* AES1 is syncclock */
5316 HDSPM_LineOut | /* Analog output in */
5317 HDSPM_Professional; /* Professional mode */
0dca1793
AK
5318 break;
5319 }
763f356c
TI
5320
5321 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5322
0dca1793 5323 if (AES32 == hdspm->io_type) {
ffb2c3c0 5324 /* No control2 register for AES32 */
763f356c 5325#ifdef SNDRV_BIG_ENDIAN
ffb2c3c0 5326 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
763f356c 5327#else
ffb2c3c0 5328 hdspm->control2_register = 0;
763f356c
TI
5329#endif
5330
ffb2c3c0
RB
5331 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5332 }
763f356c
TI
5333 hdspm_compute_period_size(hdspm);
5334
5335 /* silence everything */
5336
5337 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5338
b2ed6326 5339 if (hdspm_is_raydat_or_aio(hdspm))
0dca1793 5340 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
763f356c
TI
5341
5342 /* set a default rate so that the channel map is set up. */
0dca1793 5343 hdspm_set_rate(hdspm, 48000, 1);
763f356c
TI
5344
5345 return 0;
5346}
5347
5348
5349/*------------------------------------------------------------
0dca1793 5350 interrupt
763f356c
TI
5351 ------------------------------------------------------------*/
5352
7d12e780 5353static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
763f356c 5354{
98274f07 5355 struct hdspm *hdspm = (struct hdspm *) dev_id;
763f356c 5356 unsigned int status;
0dca1793
AK
5357 int i, audio, midi, schedule = 0;
5358 /* cycles_t now; */
763f356c
TI
5359
5360 status = hdspm_read(hdspm, HDSPM_statusRegister);
5361
5362 audio = status & HDSPM_audioIRQPending;
0dca1793
AK
5363 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5364 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
5365
5366 /* now = get_cycles(); */
5367 /**
5368 * LAT_2..LAT_0 period counter (win) counter (mac)
5369 * 6 4096 ~256053425 ~514672358
5370 * 5 2048 ~128024983 ~257373821
5371 * 4 1024 ~64023706 ~128718089
5372 * 3 512 ~32005945 ~64385999
5373 * 2 256 ~16003039 ~32260176
5374 * 1 128 ~7998738 ~16194507
5375 * 0 64 ~3998231 ~8191558
5376 **/
5377 /*
5378 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5379 now-hdspm->last_interrupt, status & 0xFFC0);
5380 hdspm->last_interrupt = now;
5381 */
763f356c 5382
0dca1793 5383 if (!audio && !midi)
763f356c
TI
5384 return IRQ_NONE;
5385
5386 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5387 hdspm->irq_count++;
5388
763f356c
TI
5389
5390 if (audio) {
763f356c 5391 if (hdspm->capture_substream)
ef5fa1a4 5392 snd_pcm_period_elapsed(hdspm->capture_substream);
763f356c
TI
5393
5394 if (hdspm->playback_substream)
ef5fa1a4 5395 snd_pcm_period_elapsed(hdspm->playback_substream);
763f356c
TI
5396 }
5397
0dca1793
AK
5398 if (midi) {
5399 i = 0;
5400 while (i < hdspm->midiPorts) {
5401 if ((hdspm_read(hdspm,
5402 hdspm->midi[i].statusIn) & 0xff) &&
5403 (status & hdspm->midi[i].irq)) {
5404 /* we disable interrupts for this input until
5405 * processing is done
5406 */
5407 hdspm->control_register &= ~hdspm->midi[i].ie;
5408 hdspm_write(hdspm, HDSPM_controlRegister,
5409 hdspm->control_register);
5410 hdspm->midi[i].pending = 1;
5411 schedule = 1;
5412 }
5413
5414 i++;
5415 }
5416
5417 if (schedule)
5418 tasklet_hi_schedule(&hdspm->midi_tasklet);
763f356c 5419 }
0dca1793 5420
763f356c
TI
5421 return IRQ_HANDLED;
5422}
5423
5424/*------------------------------------------------------------
0dca1793 5425 pcm interface
763f356c
TI
5426 ------------------------------------------------------------*/
5427
5428
0dca1793
AK
5429static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5430 *substream)
763f356c 5431{
98274f07 5432 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5433 return hdspm_hw_pointer(hdspm);
5434}
5435
763f356c 5436
98274f07 5437static int snd_hdspm_reset(struct snd_pcm_substream *substream)
763f356c 5438{
98274f07
TI
5439 struct snd_pcm_runtime *runtime = substream->runtime;
5440 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5441 struct snd_pcm_substream *other;
763f356c
TI
5442
5443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5444 other = hdspm->capture_substream;
5445 else
5446 other = hdspm->playback_substream;
5447
5448 if (hdspm->running)
5449 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5450 else
5451 runtime->status->hw_ptr = 0;
5452 if (other) {
98274f07
TI
5453 struct snd_pcm_substream *s;
5454 struct snd_pcm_runtime *oruntime = other->runtime;
ef991b95 5455 snd_pcm_group_for_each_entry(s, substream) {
763f356c
TI
5456 if (s == other) {
5457 oruntime->status->hw_ptr =
0dca1793 5458 runtime->status->hw_ptr;
763f356c
TI
5459 break;
5460 }
5461 }
5462 }
5463 return 0;
5464}
5465
98274f07
TI
5466static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5467 struct snd_pcm_hw_params *params)
763f356c 5468{
98274f07 5469 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5470 int err;
5471 int i;
5472 pid_t this_pid;
5473 pid_t other_pid;
763f356c
TI
5474
5475 spin_lock_irq(&hdspm->lock);
5476
5477 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5478 this_pid = hdspm->playback_pid;
5479 other_pid = hdspm->capture_pid;
5480 } else {
5481 this_pid = hdspm->capture_pid;
5482 other_pid = hdspm->playback_pid;
5483 }
5484
ef5fa1a4 5485 if (other_pid > 0 && this_pid != other_pid) {
763f356c
TI
5486
5487 /* The other stream is open, and not by the same
5488 task as this one. Make sure that the parameters
5489 that matter are the same.
0dca1793 5490 */
763f356c
TI
5491
5492 if (params_rate(params) != hdspm->system_sample_rate) {
5493 spin_unlock_irq(&hdspm->lock);
5494 _snd_pcm_hw_param_setempty(params,
0dca1793 5495 SNDRV_PCM_HW_PARAM_RATE);
763f356c
TI
5496 return -EBUSY;
5497 }
5498
5499 if (params_period_size(params) != hdspm->period_bytes / 4) {
5500 spin_unlock_irq(&hdspm->lock);
5501 _snd_pcm_hw_param_setempty(params,
0dca1793 5502 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c
TI
5503 return -EBUSY;
5504 }
5505
5506 }
5507 /* We're fine. */
5508 spin_unlock_irq(&hdspm->lock);
5509
5510 /* how to make sure that the rate matches an externally-set one ? */
5511
5512 spin_lock_irq(&hdspm->lock);
ef5fa1a4
TI
5513 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5514 if (err < 0) {
0dca1793 5515 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
763f356c
TI
5516 spin_unlock_irq(&hdspm->lock);
5517 _snd_pcm_hw_param_setempty(params,
0dca1793 5518 SNDRV_PCM_HW_PARAM_RATE);
763f356c
TI
5519 return err;
5520 }
5521 spin_unlock_irq(&hdspm->lock);
5522
ef5fa1a4 5523 err = hdspm_set_interrupt_interval(hdspm,
0dca1793 5524 params_period_size(params));
ef5fa1a4 5525 if (err < 0) {
0dca1793 5526 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
763f356c 5527 _snd_pcm_hw_param_setempty(params,
0dca1793 5528 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c
TI
5529 return err;
5530 }
5531
ef5fa1a4
TI
5532 /* Memory allocation, takashi's method, dont know if we should
5533 * spinlock
5534 */
763f356c 5535 /* malloc all buffer even if not enabled to get sure */
ffb2c3c0
RB
5536 /* Update for MADI rev 204: we need to allocate for all channels,
5537 * otherwise it doesn't work at 96kHz */
0dca1793 5538
763f356c 5539 err =
0dca1793
AK
5540 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5541 if (err < 0) {
5542 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
763f356c 5543 return err;
0dca1793 5544 }
763f356c 5545
763f356c
TI
5546 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5547
77a23f26 5548 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
763f356c
TI
5549 params_channels(params));
5550
5551 for (i = 0; i < params_channels(params); ++i)
5552 snd_hdspm_enable_out(hdspm, i, 1);
5553
5554 hdspm->playback_buffer =
0dca1793 5555 (unsigned char *) substream->runtime->dma_area;
54bf5dd9 5556 snd_printdd("Allocated sample buffer for playback at %p\n",
3cee5a60 5557 hdspm->playback_buffer);
763f356c 5558 } else {
77a23f26 5559 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
763f356c
TI
5560 params_channels(params));
5561
5562 for (i = 0; i < params_channels(params); ++i)
5563 snd_hdspm_enable_in(hdspm, i, 1);
5564
5565 hdspm->capture_buffer =
0dca1793 5566 (unsigned char *) substream->runtime->dma_area;
54bf5dd9 5567 snd_printdd("Allocated sample buffer for capture at %p\n",
3cee5a60 5568 hdspm->capture_buffer);
763f356c 5569 }
0dca1793 5570
3cee5a60
RB
5571 /*
5572 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5573 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5574 "playback" : "capture",
77a23f26 5575 snd_pcm_sgbuf_get_addr(substream, 0));
0dca1793 5576 */
ffb2c3c0 5577 /*
0dca1793
AK
5578 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5579 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5580 "playback" : "capture",
5581 params_rate(params), params_channels(params),
5582 params_buffer_size(params));
5583 */
5584
5585
3ac9b0ac
AK
5586 /* For AES cards, the float format bit is the same as the
5587 * preferred sync reference. Since we don't want to break
5588 * sync settings, we have to skip the remaining part of this
5589 * function.
5590 */
5591 if (hdspm->io_type == AES32) {
5592 return 0;
5593 }
5594
5595
0dca1793
AK
5596 /* Switch to native float format if requested */
5597 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5598 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5599 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5600
5601 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5602 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5603 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5604 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5605
5606 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5607 }
5608 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5609
763f356c
TI
5610 return 0;
5611}
5612
98274f07 5613static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
763f356c
TI
5614{
5615 int i;
98274f07 5616 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
5617
5618 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5619
0dca1793 5620 /* params_channels(params) should be enough,
763f356c 5621 but to get sure in case of error */
0dca1793 5622 for (i = 0; i < hdspm->max_channels_out; ++i)
763f356c
TI
5623 snd_hdspm_enable_out(hdspm, i, 0);
5624
5625 hdspm->playback_buffer = NULL;
5626 } else {
0dca1793 5627 for (i = 0; i < hdspm->max_channels_in; ++i)
763f356c
TI
5628 snd_hdspm_enable_in(hdspm, i, 0);
5629
5630 hdspm->capture_buffer = NULL;
5631
5632 }
5633
5634 snd_pcm_lib_free_pages(substream);
5635
5636 return 0;
5637}
5638
0dca1793 5639
98274f07 5640static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
0dca1793 5641 struct snd_pcm_channel_info *info)
763f356c 5642{
98274f07 5643 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c 5644
0dca1793
AK
5645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5646 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5647 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5648 return -EINVAL;
5649 }
763f356c 5650
0dca1793
AK
5651 if (hdspm->channel_map_out[info->channel] < 0) {
5652 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5653 return -EINVAL;
5654 }
5655
5656 info->offset = hdspm->channel_map_out[info->channel] *
5657 HDSPM_CHANNEL_BUFFER_BYTES;
5658 } else {
5659 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5660 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5661 return -EINVAL;
5662 }
5663
5664 if (hdspm->channel_map_in[info->channel] < 0) {
5665 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5666 return -EINVAL;
5667 }
5668
5669 info->offset = hdspm->channel_map_in[info->channel] *
5670 HDSPM_CHANNEL_BUFFER_BYTES;
5671 }
763f356c 5672
763f356c
TI
5673 info->first = 0;
5674 info->step = 32;
5675 return 0;
5676}
5677
0dca1793 5678
98274f07 5679static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
0dca1793 5680 unsigned int cmd, void *arg)
763f356c
TI
5681{
5682 switch (cmd) {
5683 case SNDRV_PCM_IOCTL1_RESET:
ef5fa1a4 5684 return snd_hdspm_reset(substream);
763f356c
TI
5685
5686 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
0dca1793
AK
5687 {
5688 struct snd_pcm_channel_info *info = arg;
5689 return snd_hdspm_channel_info(substream, info);
5690 }
763f356c
TI
5691 default:
5692 break;
5693 }
5694
5695 return snd_pcm_lib_ioctl(substream, cmd, arg);
5696}
5697
98274f07 5698static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
763f356c 5699{
98274f07
TI
5700 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5701 struct snd_pcm_substream *other;
763f356c
TI
5702 int running;
5703
5704 spin_lock(&hdspm->lock);
5705 running = hdspm->running;
5706 switch (cmd) {
5707 case SNDRV_PCM_TRIGGER_START:
5708 running |= 1 << substream->stream;
5709 break;
5710 case SNDRV_PCM_TRIGGER_STOP:
5711 running &= ~(1 << substream->stream);
5712 break;
5713 default:
5714 snd_BUG();
5715 spin_unlock(&hdspm->lock);
5716 return -EINVAL;
5717 }
5718 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5719 other = hdspm->capture_substream;
5720 else
5721 other = hdspm->playback_substream;
5722
5723 if (other) {
98274f07 5724 struct snd_pcm_substream *s;
ef991b95 5725 snd_pcm_group_for_each_entry(s, substream) {
763f356c
TI
5726 if (s == other) {
5727 snd_pcm_trigger_done(s, substream);
5728 if (cmd == SNDRV_PCM_TRIGGER_START)
5729 running |= 1 << s->stream;
5730 else
5731 running &= ~(1 << s->stream);
5732 goto _ok;
5733 }
5734 }
5735 if (cmd == SNDRV_PCM_TRIGGER_START) {
5736 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
0dca1793
AK
5737 && substream->stream ==
5738 SNDRV_PCM_STREAM_CAPTURE)
763f356c
TI
5739 hdspm_silence_playback(hdspm);
5740 } else {
5741 if (running &&
0dca1793 5742 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
763f356c
TI
5743 hdspm_silence_playback(hdspm);
5744 }
5745 } else {
5746 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5747 hdspm_silence_playback(hdspm);
5748 }
0dca1793 5749_ok:
763f356c
TI
5750 snd_pcm_trigger_done(substream, substream);
5751 if (!hdspm->running && running)
5752 hdspm_start_audio(hdspm);
5753 else if (hdspm->running && !running)
5754 hdspm_stop_audio(hdspm);
5755 hdspm->running = running;
5756 spin_unlock(&hdspm->lock);
5757
5758 return 0;
5759}
5760
98274f07 5761static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
763f356c
TI
5762{
5763 return 0;
5764}
5765
98274f07 5766static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
763f356c
TI
5767 .info = (SNDRV_PCM_INFO_MMAP |
5768 SNDRV_PCM_INFO_MMAP_VALID |
5769 SNDRV_PCM_INFO_NONINTERLEAVED |
5770 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5771 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5772 .rates = (SNDRV_PCM_RATE_32000 |
5773 SNDRV_PCM_RATE_44100 |
5774 SNDRV_PCM_RATE_48000 |
5775 SNDRV_PCM_RATE_64000 |
3cee5a60
RB
5776 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5777 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
763f356c 5778 .rate_min = 32000,
3cee5a60 5779 .rate_max = 192000,
763f356c
TI
5780 .channels_min = 1,
5781 .channels_max = HDSPM_MAX_CHANNELS,
5782 .buffer_bytes_max =
5783 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
1b6fa108 5784 .period_bytes_min = (32 * 4),
52e6fb48 5785 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
763f356c 5786 .periods_min = 2,
0dca1793 5787 .periods_max = 512,
763f356c
TI
5788 .fifo_size = 0
5789};
5790
98274f07 5791static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
763f356c
TI
5792 .info = (SNDRV_PCM_INFO_MMAP |
5793 SNDRV_PCM_INFO_MMAP_VALID |
5794 SNDRV_PCM_INFO_NONINTERLEAVED |
5795 SNDRV_PCM_INFO_SYNC_START),
5796 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5797 .rates = (SNDRV_PCM_RATE_32000 |
5798 SNDRV_PCM_RATE_44100 |
5799 SNDRV_PCM_RATE_48000 |
5800 SNDRV_PCM_RATE_64000 |
3cee5a60
RB
5801 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5802 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
763f356c 5803 .rate_min = 32000,
3cee5a60 5804 .rate_max = 192000,
763f356c
TI
5805 .channels_min = 1,
5806 .channels_max = HDSPM_MAX_CHANNELS,
5807 .buffer_bytes_max =
5808 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
1b6fa108 5809 .period_bytes_min = (32 * 4),
52e6fb48 5810 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
763f356c 5811 .periods_min = 2,
0dca1793 5812 .periods_max = 512,
763f356c
TI
5813 .fifo_size = 0
5814};
5815
0dca1793
AK
5816static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5817 struct snd_pcm_hw_rule *rule)
5818{
5819 struct hdspm *hdspm = rule->private;
5820 struct snd_interval *c =
5821 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5822 struct snd_interval *r =
5823 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5824
5825 if (r->min > 96000 && r->max <= 192000) {
5826 struct snd_interval t = {
5827 .min = hdspm->qs_in_channels,
5828 .max = hdspm->qs_in_channels,
5829 .integer = 1,
5830 };
5831 return snd_interval_refine(c, &t);
5832 } else if (r->min > 48000 && r->max <= 96000) {
5833 struct snd_interval t = {
5834 .min = hdspm->ds_in_channels,
5835 .max = hdspm->ds_in_channels,
5836 .integer = 1,
5837 };
5838 return snd_interval_refine(c, &t);
5839 } else if (r->max < 64000) {
5840 struct snd_interval t = {
5841 .min = hdspm->ss_in_channels,
5842 .max = hdspm->ss_in_channels,
5843 .integer = 1,
5844 };
5845 return snd_interval_refine(c, &t);
5846 }
5847
5848 return 0;
5849}
763f356c 5850
0dca1793 5851static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
98274f07 5852 struct snd_pcm_hw_rule * rule)
763f356c 5853{
98274f07
TI
5854 struct hdspm *hdspm = rule->private;
5855 struct snd_interval *c =
763f356c 5856 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
98274f07 5857 struct snd_interval *r =
763f356c
TI
5858 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5859
0dca1793
AK
5860 if (r->min > 96000 && r->max <= 192000) {
5861 struct snd_interval t = {
5862 .min = hdspm->qs_out_channels,
5863 .max = hdspm->qs_out_channels,
5864 .integer = 1,
5865 };
5866 return snd_interval_refine(c, &t);
5867 } else if (r->min > 48000 && r->max <= 96000) {
98274f07 5868 struct snd_interval t = {
0dca1793
AK
5869 .min = hdspm->ds_out_channels,
5870 .max = hdspm->ds_out_channels,
763f356c
TI
5871 .integer = 1,
5872 };
5873 return snd_interval_refine(c, &t);
5874 } else if (r->max < 64000) {
98274f07 5875 struct snd_interval t = {
0dca1793
AK
5876 .min = hdspm->ss_out_channels,
5877 .max = hdspm->ss_out_channels,
763f356c
TI
5878 .integer = 1,
5879 };
5880 return snd_interval_refine(c, &t);
0dca1793 5881 } else {
763f356c
TI
5882 }
5883 return 0;
5884}
5885
0dca1793 5886static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
98274f07 5887 struct snd_pcm_hw_rule * rule)
763f356c 5888{
98274f07
TI
5889 struct hdspm *hdspm = rule->private;
5890 struct snd_interval *c =
763f356c 5891 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
98274f07 5892 struct snd_interval *r =
763f356c
TI
5893 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5894
0dca1793 5895 if (c->min >= hdspm->ss_in_channels) {
98274f07 5896 struct snd_interval t = {
763f356c
TI
5897 .min = 32000,
5898 .max = 48000,
5899 .integer = 1,
5900 };
5901 return snd_interval_refine(r, &t);
0dca1793
AK
5902 } else if (c->max <= hdspm->qs_in_channels) {
5903 struct snd_interval t = {
5904 .min = 128000,
5905 .max = 192000,
5906 .integer = 1,
5907 };
5908 return snd_interval_refine(r, &t);
5909 } else if (c->max <= hdspm->ds_in_channels) {
98274f07 5910 struct snd_interval t = {
763f356c
TI
5911 .min = 64000,
5912 .max = 96000,
5913 .integer = 1,
5914 };
0dca1793
AK
5915 return snd_interval_refine(r, &t);
5916 }
5917
5918 return 0;
5919}
5920static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5921 struct snd_pcm_hw_rule *rule)
5922{
5923 struct hdspm *hdspm = rule->private;
5924 struct snd_interval *c =
5925 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5926 struct snd_interval *r =
5927 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
763f356c 5928
0dca1793
AK
5929 if (c->min >= hdspm->ss_out_channels) {
5930 struct snd_interval t = {
5931 .min = 32000,
5932 .max = 48000,
5933 .integer = 1,
5934 };
5935 return snd_interval_refine(r, &t);
5936 } else if (c->max <= hdspm->qs_out_channels) {
5937 struct snd_interval t = {
5938 .min = 128000,
5939 .max = 192000,
5940 .integer = 1,
5941 };
5942 return snd_interval_refine(r, &t);
5943 } else if (c->max <= hdspm->ds_out_channels) {
5944 struct snd_interval t = {
5945 .min = 64000,
5946 .max = 96000,
5947 .integer = 1,
5948 };
763f356c
TI
5949 return snd_interval_refine(r, &t);
5950 }
0dca1793 5951
763f356c
TI
5952 return 0;
5953}
5954
0dca1793 5955static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
ffb2c3c0
RB
5956 struct snd_pcm_hw_rule *rule)
5957{
5958 unsigned int list[3];
5959 struct hdspm *hdspm = rule->private;
5960 struct snd_interval *c = hw_param_interval(params,
5961 SNDRV_PCM_HW_PARAM_CHANNELS);
0dca1793
AK
5962
5963 list[0] = hdspm->qs_in_channels;
5964 list[1] = hdspm->ds_in_channels;
5965 list[2] = hdspm->ss_in_channels;
5966 return snd_interval_list(c, 3, list, 0);
5967}
5968
5969static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5970 struct snd_pcm_hw_rule *rule)
5971{
5972 unsigned int list[3];
5973 struct hdspm *hdspm = rule->private;
5974 struct snd_interval *c = hw_param_interval(params,
5975 SNDRV_PCM_HW_PARAM_CHANNELS);
5976
5977 list[0] = hdspm->qs_out_channels;
5978 list[1] = hdspm->ds_out_channels;
5979 list[2] = hdspm->ss_out_channels;
5980 return snd_interval_list(c, 3, list, 0);
ffb2c3c0
RB
5981}
5982
5983
ef5fa1a4
TI
5984static unsigned int hdspm_aes32_sample_rates[] = {
5985 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5986};
ffb2c3c0 5987
ef5fa1a4
TI
5988static struct snd_pcm_hw_constraint_list
5989hdspm_hw_constraints_aes32_sample_rates = {
ffb2c3c0
RB
5990 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5991 .list = hdspm_aes32_sample_rates,
5992 .mask = 0
5993};
5994
98274f07 5995static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
763f356c 5996{
98274f07
TI
5997 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5998 struct snd_pcm_runtime *runtime = substream->runtime;
763f356c 5999
763f356c
TI
6000 spin_lock_irq(&hdspm->lock);
6001
6002 snd_pcm_set_sync(substream);
6003
0dca1793 6004
763f356c
TI
6005 runtime->hw = snd_hdspm_playback_subinfo;
6006
6007 if (hdspm->capture_substream == NULL)
6008 hdspm_stop_audio(hdspm);
6009
6010 hdspm->playback_pid = current->pid;
6011 hdspm->playback_substream = substream;
6012
6013 spin_unlock_irq(&hdspm->lock);
6014
6015 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
d877681d 6016 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
763f356c 6017
0dca1793
AK
6018 switch (hdspm->io_type) {
6019 case AIO:
6020 case RayDAT:
d877681d
TI
6021 snd_pcm_hw_constraint_minmax(runtime,
6022 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6023 32, 4096);
6024 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
6025 snd_pcm_hw_constraint_minmax(runtime,
6026 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6027 16384, 16384);
0dca1793
AK
6028 break;
6029
6030 default:
d877681d
TI
6031 snd_pcm_hw_constraint_minmax(runtime,
6032 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6033 64, 8192);
6034 break;
0dca1793 6035 }
763f356c 6036
0dca1793 6037 if (AES32 == hdspm->io_type) {
3fa9e3d2 6038 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
ffb2c3c0
RB
6039 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6040 &hdspm_hw_constraints_aes32_sample_rates);
6041 } else {
ffb2c3c0 6042 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0dca1793
AK
6043 snd_hdspm_hw_rule_rate_out_channels, hdspm,
6044 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
ffb2c3c0 6045 }
88fabbfc
AK
6046
6047 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6048 snd_hdspm_hw_rule_out_channels, hdspm,
6049 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6050
6051 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6052 snd_hdspm_hw_rule_out_channels_rate, hdspm,
6053 SNDRV_PCM_HW_PARAM_RATE, -1);
6054
763f356c
TI
6055 return 0;
6056}
6057
98274f07 6058static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
763f356c 6059{
98274f07 6060 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
6061
6062 spin_lock_irq(&hdspm->lock);
6063
6064 hdspm->playback_pid = -1;
6065 hdspm->playback_substream = NULL;
6066
6067 spin_unlock_irq(&hdspm->lock);
6068
6069 return 0;
6070}
6071
6072
98274f07 6073static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
763f356c 6074{
98274f07
TI
6075 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6076 struct snd_pcm_runtime *runtime = substream->runtime;
763f356c
TI
6077
6078 spin_lock_irq(&hdspm->lock);
6079 snd_pcm_set_sync(substream);
6080 runtime->hw = snd_hdspm_capture_subinfo;
6081
6082 if (hdspm->playback_substream == NULL)
6083 hdspm_stop_audio(hdspm);
6084
6085 hdspm->capture_pid = current->pid;
6086 hdspm->capture_substream = substream;
6087
6088 spin_unlock_irq(&hdspm->lock);
6089
6090 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
d877681d
TI
6091 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
6092
0dca1793
AK
6093 switch (hdspm->io_type) {
6094 case AIO:
6095 case RayDAT:
d877681d
TI
6096 snd_pcm_hw_constraint_minmax(runtime,
6097 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6098 32, 4096);
6099 snd_pcm_hw_constraint_minmax(runtime,
6100 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6101 16384, 16384);
6102 break;
0dca1793
AK
6103
6104 default:
d877681d
TI
6105 snd_pcm_hw_constraint_minmax(runtime,
6106 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6107 64, 8192);
6108 break;
0dca1793
AK
6109 }
6110
6111 if (AES32 == hdspm->io_type) {
3fa9e3d2 6112 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
ffb2c3c0
RB
6113 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6114 &hdspm_hw_constraints_aes32_sample_rates);
6115 } else {
ffb2c3c0 6116 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
88fabbfc
AK
6117 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6118 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
ffb2c3c0 6119 }
88fabbfc
AK
6120
6121 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6122 snd_hdspm_hw_rule_in_channels, hdspm,
6123 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6124
6125 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6126 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6127 SNDRV_PCM_HW_PARAM_RATE, -1);
6128
763f356c
TI
6129 return 0;
6130}
6131
98274f07 6132static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
763f356c 6133{
98274f07 6134 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
763f356c
TI
6135
6136 spin_lock_irq(&hdspm->lock);
6137
6138 hdspm->capture_pid = -1;
6139 hdspm->capture_substream = NULL;
6140
6141 spin_unlock_irq(&hdspm->lock);
6142 return 0;
6143}
6144
0dca1793
AK
6145static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
6146{
6147 /* we have nothing to initialize but the call is required */
6148 return 0;
6149}
6150
6151static inline int copy_u32_le(void __user *dest, void __iomem *src)
6152{
6153 u32 val = readl(src);
6154 return copy_to_user(dest, &val, 4);
6155}
6156
6157static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
2ca595ab 6158 unsigned int cmd, unsigned long arg)
763f356c 6159{
0dca1793 6160 void __user *argp = (void __user *)arg;
ef5fa1a4 6161 struct hdspm *hdspm = hw->private_data;
98274f07 6162 struct hdspm_mixer_ioctl mixer;
0dca1793
AK
6163 struct hdspm_config info;
6164 struct hdspm_status status;
98274f07 6165 struct hdspm_version hdspm_version;
730a5865 6166 struct hdspm_peak_rms *levels;
0dca1793
AK
6167 struct hdspm_ltc ltc;
6168 unsigned int statusregister;
6169 long unsigned int s;
6170 int i = 0;
763f356c
TI
6171
6172 switch (cmd) {
6173
763f356c 6174 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
730a5865 6175 levels = &hdspm->peak_rms;
0dca1793 6176 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
730a5865 6177 levels->input_peaks[i] =
0dca1793
AK
6178 readl(hdspm->iobase +
6179 HDSPM_MADI_INPUT_PEAK + i*4);
730a5865 6180 levels->playback_peaks[i] =
0dca1793
AK
6181 readl(hdspm->iobase +
6182 HDSPM_MADI_PLAYBACK_PEAK + i*4);
730a5865 6183 levels->output_peaks[i] =
0dca1793
AK
6184 readl(hdspm->iobase +
6185 HDSPM_MADI_OUTPUT_PEAK + i*4);
6186
730a5865 6187 levels->input_rms[i] =
0dca1793
AK
6188 ((uint64_t) readl(hdspm->iobase +
6189 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6190 (uint64_t) readl(hdspm->iobase +
6191 HDSPM_MADI_INPUT_RMS_L + i*4);
730a5865 6192 levels->playback_rms[i] =
0dca1793
AK
6193 ((uint64_t)readl(hdspm->iobase +
6194 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6195 (uint64_t)readl(hdspm->iobase +
6196 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
730a5865 6197 levels->output_rms[i] =
0dca1793
AK
6198 ((uint64_t)readl(hdspm->iobase +
6199 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6200 (uint64_t)readl(hdspm->iobase +
6201 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6202 }
6203
6204 if (hdspm->system_sample_rate > 96000) {
730a5865 6205 levels->speed = qs;
0dca1793 6206 } else if (hdspm->system_sample_rate > 48000) {
730a5865 6207 levels->speed = ds;
0dca1793 6208 } else {
730a5865 6209 levels->speed = ss;
0dca1793 6210 }
730a5865 6211 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
0dca1793 6212
730a5865 6213 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
0dca1793
AK
6214 if (0 != s) {
6215 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6216 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6217 */
763f356c 6218 return -EFAULT;
0dca1793
AK
6219 }
6220 break;
6221
6222 case SNDRV_HDSPM_IOCTL_GET_LTC:
6223 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6224 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6225 if (i & HDSPM_TCO1_LTC_Input_valid) {
6226 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6227 HDSPM_TCO1_LTC_Format_MSB)) {
6228 case 0:
6229 ltc.format = fps_24;
6230 break;
6231 case HDSPM_TCO1_LTC_Format_LSB:
6232 ltc.format = fps_25;
6233 break;
6234 case HDSPM_TCO1_LTC_Format_MSB:
6235 ltc.format = fps_2997;
6236 break;
6237 default:
6238 ltc.format = 30;
6239 break;
6240 }
6241 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6242 ltc.frame = drop_frame;
6243 } else {
6244 ltc.frame = full_frame;
6245 }
6246 } else {
6247 ltc.format = format_invalid;
6248 ltc.frame = frame_invalid;
6249 }
6250 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6251 ltc.input_format = ntsc;
6252 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6253 ltc.input_format = pal;
6254 } else {
6255 ltc.input_format = no_video;
6256 }
6257
6258 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6259 if (0 != s) {
6260 /*
6261 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
763f356c 6262 return -EFAULT;
0dca1793 6263 }
763f356c
TI
6264
6265 break;
763f356c 6266
0dca1793 6267 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
763f356c 6268
4ab69a2b 6269 memset(&info, 0, sizeof(info));
763f356c 6270 spin_lock_irq(&hdspm->lock);
ef5fa1a4
TI
6271 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6272 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
763f356c
TI
6273
6274 info.system_sample_rate = hdspm->system_sample_rate;
6275 info.autosync_sample_rate =
0dca1793 6276 hdspm_external_sample_rate(hdspm);
ef5fa1a4
TI
6277 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6278 info.clock_source = hdspm_clock_source(hdspm);
6279 info.autosync_ref = hdspm_autosync_ref(hdspm);
c9e1668c 6280 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
763f356c
TI
6281 info.passthru = 0;
6282 spin_unlock_irq(&hdspm->lock);
2ca595ab 6283 if (copy_to_user(argp, &info, sizeof(info)))
763f356c
TI
6284 return -EFAULT;
6285 break;
6286
0dca1793 6287 case SNDRV_HDSPM_IOCTL_GET_STATUS:
643d6bbb
DC
6288 memset(&status, 0, sizeof(status));
6289
0dca1793
AK
6290 status.card_type = hdspm->io_type;
6291
6292 status.autosync_source = hdspm_autosync_ref(hdspm);
6293
6294 status.card_clock = 110069313433624ULL;
6295 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6296
6297 switch (hdspm->io_type) {
6298 case MADI:
6299 case MADIface:
6300 status.card_specific.madi.sync_wc =
6301 hdspm_wc_sync_check(hdspm);
6302 status.card_specific.madi.sync_madi =
6303 hdspm_madi_sync_check(hdspm);
6304 status.card_specific.madi.sync_tco =
6305 hdspm_tco_sync_check(hdspm);
6306 status.card_specific.madi.sync_in =
6307 hdspm_sync_in_sync_check(hdspm);
6308
6309 statusregister =
6310 hdspm_read(hdspm, HDSPM_statusRegister);
6311 status.card_specific.madi.madi_input =
6312 (statusregister & HDSPM_AB_int) ? 1 : 0;
6313 status.card_specific.madi.channel_format =
9e6ff520 6314 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
0dca1793
AK
6315 /* TODO: Mac driver sets it when f_s>48kHz */
6316 status.card_specific.madi.frame_format = 0;
6317
6318 default:
6319 break;
6320 }
6321
2ca595ab 6322 if (copy_to_user(argp, &status, sizeof(status)))
0dca1793
AK
6323 return -EFAULT;
6324
6325
6326 break;
6327
763f356c 6328 case SNDRV_HDSPM_IOCTL_GET_VERSION:
643d6bbb
DC
6329 memset(&hdspm_version, 0, sizeof(hdspm_version));
6330
0dca1793
AK
6331 hdspm_version.card_type = hdspm->io_type;
6332 strncpy(hdspm_version.cardname, hdspm->card_name,
6333 sizeof(hdspm_version.cardname));
7d53a631 6334 hdspm_version.serial = hdspm->serial;
763f356c 6335 hdspm_version.firmware_rev = hdspm->firmware_rev;
0dca1793
AK
6336 hdspm_version.addons = 0;
6337 if (hdspm->tco)
6338 hdspm_version.addons |= HDSPM_ADDON_TCO;
6339
2ca595ab 6340 if (copy_to_user(argp, &hdspm_version,
0dca1793 6341 sizeof(hdspm_version)))
763f356c
TI
6342 return -EFAULT;
6343 break;
6344
6345 case SNDRV_HDSPM_IOCTL_GET_MIXER:
2ca595ab 6346 if (copy_from_user(&mixer, argp, sizeof(mixer)))
763f356c 6347 return -EFAULT;
ef5fa1a4 6348 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
0dca1793 6349 sizeof(struct hdspm_mixer)))
763f356c
TI
6350 return -EFAULT;
6351 break;
6352
6353 default:
6354 return -EINVAL;
6355 }
6356 return 0;
6357}
6358
98274f07 6359static struct snd_pcm_ops snd_hdspm_playback_ops = {
763f356c
TI
6360 .open = snd_hdspm_playback_open,
6361 .close = snd_hdspm_playback_release,
6362 .ioctl = snd_hdspm_ioctl,
6363 .hw_params = snd_hdspm_hw_params,
6364 .hw_free = snd_hdspm_hw_free,
6365 .prepare = snd_hdspm_prepare,
6366 .trigger = snd_hdspm_trigger,
6367 .pointer = snd_hdspm_hw_pointer,
763f356c
TI
6368 .page = snd_pcm_sgbuf_ops_page,
6369};
6370
98274f07 6371static struct snd_pcm_ops snd_hdspm_capture_ops = {
763f356c
TI
6372 .open = snd_hdspm_capture_open,
6373 .close = snd_hdspm_capture_release,
6374 .ioctl = snd_hdspm_ioctl,
6375 .hw_params = snd_hdspm_hw_params,
6376 .hw_free = snd_hdspm_hw_free,
6377 .prepare = snd_hdspm_prepare,
6378 .trigger = snd_hdspm_trigger,
6379 .pointer = snd_hdspm_hw_pointer,
763f356c
TI
6380 .page = snd_pcm_sgbuf_ops_page,
6381};
6382
e23e7a14
BP
6383static int snd_hdspm_create_hwdep(struct snd_card *card,
6384 struct hdspm *hdspm)
763f356c 6385{
98274f07 6386 struct snd_hwdep *hw;
763f356c
TI
6387 int err;
6388
ef5fa1a4
TI
6389 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6390 if (err < 0)
763f356c
TI
6391 return err;
6392
6393 hdspm->hwdep = hw;
6394 hw->private_data = hdspm;
6395 strcpy(hw->name, "HDSPM hwdep interface");
6396
0dca1793 6397 hw->ops.open = snd_hdspm_hwdep_dummy_op;
763f356c 6398 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
8de5d6f1 6399 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
0dca1793 6400 hw->ops.release = snd_hdspm_hwdep_dummy_op;
763f356c
TI
6401
6402 return 0;
6403}
6404
6405
6406/*------------------------------------------------------------
0dca1793 6407 memory interface
763f356c 6408 ------------------------------------------------------------*/
e23e7a14 6409static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
763f356c
TI
6410{
6411 int err;
98274f07 6412 struct snd_pcm *pcm;
763f356c
TI
6413 size_t wanted;
6414
6415 pcm = hdspm->pcm;
6416
3cee5a60 6417 wanted = HDSPM_DMA_AREA_BYTES;
763f356c 6418
ef5fa1a4 6419 err =
763f356c 6420 snd_pcm_lib_preallocate_pages_for_all(pcm,
0dca1793 6421 SNDRV_DMA_TYPE_DEV_SG,
763f356c
TI
6422 snd_dma_pci_data(hdspm->pci),
6423 wanted,
ef5fa1a4
TI
6424 wanted);
6425 if (err < 0) {
e2eba3e7 6426 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
763f356c
TI
6427
6428 return err;
6429 } else
e2eba3e7 6430 snd_printdd(" Preallocated %zd Bytes\n", wanted);
763f356c
TI
6431
6432 return 0;
6433}
6434
0dca1793
AK
6435
6436static void hdspm_set_sgbuf(struct hdspm *hdspm,
77a23f26 6437 struct snd_pcm_substream *substream,
763f356c
TI
6438 unsigned int reg, int channels)
6439{
6440 int i;
0dca1793
AK
6441
6442 /* continuous memory segment */
763f356c
TI
6443 for (i = 0; i < (channels * 16); i++)
6444 hdspm_write(hdspm, reg + 4 * i,
0dca1793 6445 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
763f356c
TI
6446}
6447
0dca1793 6448
763f356c 6449/* ------------- ALSA Devices ---------------------------- */
e23e7a14
BP
6450static int snd_hdspm_create_pcm(struct snd_card *card,
6451 struct hdspm *hdspm)
763f356c 6452{
98274f07 6453 struct snd_pcm *pcm;
763f356c
TI
6454 int err;
6455
ef5fa1a4
TI
6456 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6457 if (err < 0)
763f356c
TI
6458 return err;
6459
6460 hdspm->pcm = pcm;
6461 pcm->private_data = hdspm;
6462 strcpy(pcm->name, hdspm->card_name);
6463
6464 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6465 &snd_hdspm_playback_ops);
6466 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6467 &snd_hdspm_capture_ops);
6468
6469 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6470
ef5fa1a4
TI
6471 err = snd_hdspm_preallocate_memory(hdspm);
6472 if (err < 0)
763f356c
TI
6473 return err;
6474
6475 return 0;
6476}
6477
98274f07 6478static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
763f356c 6479{
7c7102b7
AK
6480 int i;
6481
6482 for (i = 0; i < hdspm->midiPorts; i++)
6483 snd_hdspm_flush_midi_input(hdspm, i);
763f356c
TI
6484}
6485
e23e7a14
BP
6486static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6487 struct hdspm *hdspm)
763f356c 6488{
0dca1793 6489 int err, i;
763f356c
TI
6490
6491 snd_printdd("Create card...\n");
ef5fa1a4
TI
6492 err = snd_hdspm_create_pcm(card, hdspm);
6493 if (err < 0)
763f356c
TI
6494 return err;
6495
0dca1793
AK
6496 i = 0;
6497 while (i < hdspm->midiPorts) {
6498 err = snd_hdspm_create_midi(card, hdspm, i);
6499 if (err < 0) {
6500 return err;
6501 }
6502 i++;
6503 }
763f356c 6504
ef5fa1a4
TI
6505 err = snd_hdspm_create_controls(card, hdspm);
6506 if (err < 0)
763f356c
TI
6507 return err;
6508
ef5fa1a4
TI
6509 err = snd_hdspm_create_hwdep(card, hdspm);
6510 if (err < 0)
763f356c
TI
6511 return err;
6512
6513 snd_printdd("proc init...\n");
6514 snd_hdspm_proc_init(hdspm);
6515
6516 hdspm->system_sample_rate = -1;
6517 hdspm->last_external_sample_rate = -1;
6518 hdspm->last_internal_sample_rate = -1;
6519 hdspm->playback_pid = -1;
6520 hdspm->capture_pid = -1;
6521 hdspm->capture_substream = NULL;
6522 hdspm->playback_substream = NULL;
6523
6524 snd_printdd("Set defaults...\n");
ef5fa1a4
TI
6525 err = snd_hdspm_set_defaults(hdspm);
6526 if (err < 0)
763f356c
TI
6527 return err;
6528
6529 snd_printdd("Update mixer controls...\n");
6530 hdspm_update_simple_mixer_controls(hdspm);
6531
6532 snd_printdd("Initializeing complete ???\n");
6533
ef5fa1a4
TI
6534 err = snd_card_register(card);
6535 if (err < 0) {
763f356c
TI
6536 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6537 return err;
6538 }
6539
6540 snd_printdd("... yes now\n");
6541
6542 return 0;
6543}
6544
e23e7a14
BP
6545static int snd_hdspm_create(struct snd_card *card,
6546 struct hdspm *hdspm)
6547{
0dca1793 6548
763f356c
TI
6549 struct pci_dev *pci = hdspm->pci;
6550 int err;
763f356c
TI
6551 unsigned long io_extent;
6552
6553 hdspm->irq = -1;
763f356c
TI
6554 hdspm->card = card;
6555
6556 spin_lock_init(&hdspm->lock);
6557
763f356c 6558 pci_read_config_word(hdspm->pci,
0dca1793 6559 PCI_CLASS_REVISION, &hdspm->firmware_rev);
3cee5a60 6560
763f356c 6561 strcpy(card->mixername, "Xilinx FPGA");
0dca1793
AK
6562 strcpy(card->driver, "HDSPM");
6563
6564 switch (hdspm->firmware_rev) {
0dca1793
AK
6565 case HDSPM_RAYDAT_REV:
6566 hdspm->io_type = RayDAT;
6567 hdspm->card_name = "RME RayDAT";
6568 hdspm->midiPorts = 2;
6569 break;
6570 case HDSPM_AIO_REV:
6571 hdspm->io_type = AIO;
6572 hdspm->card_name = "RME AIO";
6573 hdspm->midiPorts = 1;
6574 break;
6575 case HDSPM_MADIFACE_REV:
6576 hdspm->io_type = MADIface;
6577 hdspm->card_name = "RME MADIface";
6578 hdspm->midiPorts = 1;
6579 break;
5027f347 6580 default:
c09403dc
AK
6581 if ((hdspm->firmware_rev == 0xf0) ||
6582 ((hdspm->firmware_rev >= 0xe6) &&
6583 (hdspm->firmware_rev <= 0xea))) {
6584 hdspm->io_type = AES32;
6585 hdspm->card_name = "RME AES32";
6586 hdspm->midiPorts = 2;
05c7cc9c 6587 } else if ((hdspm->firmware_rev == 0xd2) ||
c09403dc
AK
6588 ((hdspm->firmware_rev >= 0xc8) &&
6589 (hdspm->firmware_rev <= 0xcf))) {
6590 hdspm->io_type = MADI;
6591 hdspm->card_name = "RME MADI";
6592 hdspm->midiPorts = 3;
6593 } else {
6594 snd_printk(KERN_ERR
6595 "HDSPM: unknown firmware revision %x\n",
5027f347 6596 hdspm->firmware_rev);
c09403dc
AK
6597 return -ENODEV;
6598 }
3cee5a60 6599 }
763f356c 6600
ef5fa1a4
TI
6601 err = pci_enable_device(pci);
6602 if (err < 0)
763f356c
TI
6603 return err;
6604
6605 pci_set_master(hdspm->pci);
6606
ef5fa1a4
TI
6607 err = pci_request_regions(pci, "hdspm");
6608 if (err < 0)
763f356c
TI
6609 return err;
6610
6611 hdspm->port = pci_resource_start(pci, 0);
6612 io_extent = pci_resource_len(pci, 0);
6613
6614 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
0dca1793 6615 hdspm->port, hdspm->port + io_extent - 1);
763f356c 6616
ef5fa1a4
TI
6617 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6618 if (!hdspm->iobase) {
6619 snd_printk(KERN_ERR "HDSPM: "
0dca1793
AK
6620 "unable to remap region 0x%lx-0x%lx\n",
6621 hdspm->port, hdspm->port + io_extent - 1);
763f356c
TI
6622 return -EBUSY;
6623 }
6624 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
0dca1793
AK
6625 (unsigned long)hdspm->iobase, hdspm->port,
6626 hdspm->port + io_extent - 1);
763f356c
TI
6627
6628 if (request_irq(pci->irq, snd_hdspm_interrupt,
934c2b6d 6629 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
763f356c
TI
6630 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6631 return -EBUSY;
6632 }
6633
6634 snd_printdd("use IRQ %d\n", pci->irq);
6635
6636 hdspm->irq = pci->irq;
763f356c 6637
e2eba3e7 6638 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
0dca1793 6639 sizeof(struct hdspm_mixer));
ef5fa1a4
TI
6640 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6641 if (!hdspm->mixer) {
6642 snd_printk(KERN_ERR "HDSPM: "
0dca1793
AK
6643 "unable to kmalloc Mixer memory of %d Bytes\n",
6644 (int)sizeof(struct hdspm_mixer));
b17cbdd8 6645 return -ENOMEM;
763f356c
TI
6646 }
6647
0dca1793
AK
6648 hdspm->port_names_in = NULL;
6649 hdspm->port_names_out = NULL;
6650
6651 switch (hdspm->io_type) {
6652 case AES32:
d2d10a21
AK
6653 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6654 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6655 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
432d2500
AK
6656
6657 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6658 channel_map_aes32;
6659 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6660 channel_map_aes32;
6661 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6662 channel_map_aes32;
6663 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6664 texts_ports_aes32;
6665 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6666 texts_ports_aes32;
6667 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6668 texts_ports_aes32;
6669
d2d10a21
AK
6670 hdspm->max_channels_out = hdspm->max_channels_in =
6671 AES32_CHANNELS;
432d2500
AK
6672 hdspm->port_names_in = hdspm->port_names_out =
6673 texts_ports_aes32;
6674 hdspm->channel_map_in = hdspm->channel_map_out =
6675 channel_map_aes32;
6676
0dca1793
AK
6677 break;
6678
6679 case MADI:
6680 case MADIface:
6681 hdspm->ss_in_channels = hdspm->ss_out_channels =
6682 MADI_SS_CHANNELS;
6683 hdspm->ds_in_channels = hdspm->ds_out_channels =
6684 MADI_DS_CHANNELS;
6685 hdspm->qs_in_channels = hdspm->qs_out_channels =
6686 MADI_QS_CHANNELS;
6687
6688 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6689 channel_map_unity_ss;
01e96078 6690 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
0dca1793 6691 channel_map_unity_ss;
01e96078 6692 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
0dca1793
AK
6693 channel_map_unity_ss;
6694
6695 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6696 texts_ports_madi;
6697 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6698 texts_ports_madi;
6699 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6700 texts_ports_madi;
6701 break;
6702
6703 case AIO:
0dca1793
AK
6704 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6705 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6706 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6707 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6708 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6709 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6710
3de9db26
AK
6711 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6712 snd_printk(KERN_INFO "HDSPM: AEB input board found\n");
6713 hdspm->ss_in_channels += 4;
6714 hdspm->ds_in_channels += 4;
6715 hdspm->qs_in_channels += 4;
6716 }
6717
6718 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6719 snd_printk(KERN_INFO "HDSPM: AEB output board found\n");
6720 hdspm->ss_out_channels += 4;
6721 hdspm->ds_out_channels += 4;
6722 hdspm->qs_out_channels += 4;
6723 }
6724
0dca1793
AK
6725 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6726 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6727 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6728
6729 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6730 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6731 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6732
6733 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6734 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6735 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6736 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6737 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6738 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6739
6740 break;
6741
6742 case RayDAT:
6743 hdspm->ss_in_channels = hdspm->ss_out_channels =
6744 RAYDAT_SS_CHANNELS;
6745 hdspm->ds_in_channels = hdspm->ds_out_channels =
6746 RAYDAT_DS_CHANNELS;
6747 hdspm->qs_in_channels = hdspm->qs_out_channels =
6748 RAYDAT_QS_CHANNELS;
6749
6750 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6751 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6752
6753 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6754 channel_map_raydat_ss;
6755 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6756 channel_map_raydat_ds;
6757 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6758 channel_map_raydat_qs;
6759 hdspm->channel_map_in = hdspm->channel_map_out =
6760 channel_map_raydat_ss;
6761
6762 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6763 texts_ports_raydat_ss;
6764 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6765 texts_ports_raydat_ds;
6766 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6767 texts_ports_raydat_qs;
6768
6769
6770 break;
6771
6772 }
6773
6774 /* TCO detection */
6775 switch (hdspm->io_type) {
6776 case AIO:
6777 case RayDAT:
6778 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6779 HDSPM_s2_tco_detect) {
6780 hdspm->midiPorts++;
6781 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6782 GFP_KERNEL);
6783 if (NULL != hdspm->tco) {
6784 hdspm_tco_write(hdspm);
6785 }
6786 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6787 } else {
6788 hdspm->tco = NULL;
6789 }
6790 break;
6791
6792 case MADI:
0dc831b9 6793 case AES32:
0dca1793
AK
6794 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6795 hdspm->midiPorts++;
6796 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6797 GFP_KERNEL);
6798 if (NULL != hdspm->tco) {
6799 hdspm_tco_write(hdspm);
6800 }
e71b95ad 6801 snd_printk(KERN_INFO "HDSPM: MADI/AES TCO module found\n");
0dca1793
AK
6802 } else {
6803 hdspm->tco = NULL;
6804 }
6805 break;
6806
6807 default:
6808 hdspm->tco = NULL;
6809 }
6810
6811 /* texts */
6812 switch (hdspm->io_type) {
6813 case AES32:
6814 if (hdspm->tco) {
6815 hdspm->texts_autosync = texts_autosync_aes_tco;
e71b95ad
AK
6816 hdspm->texts_autosync_items =
6817 ARRAY_SIZE(texts_autosync_aes_tco);
0dca1793
AK
6818 } else {
6819 hdspm->texts_autosync = texts_autosync_aes;
e71b95ad
AK
6820 hdspm->texts_autosync_items =
6821 ARRAY_SIZE(texts_autosync_aes);
0dca1793
AK
6822 }
6823 break;
6824
6825 case MADI:
6826 if (hdspm->tco) {
6827 hdspm->texts_autosync = texts_autosync_madi_tco;
6828 hdspm->texts_autosync_items = 4;
6829 } else {
6830 hdspm->texts_autosync = texts_autosync_madi;
6831 hdspm->texts_autosync_items = 3;
6832 }
6833 break;
6834
6835 case MADIface:
6836
6837 break;
6838
6839 case RayDAT:
6840 if (hdspm->tco) {
6841 hdspm->texts_autosync = texts_autosync_raydat_tco;
6842 hdspm->texts_autosync_items = 9;
6843 } else {
6844 hdspm->texts_autosync = texts_autosync_raydat;
6845 hdspm->texts_autosync_items = 8;
6846 }
6847 break;
6848
6849 case AIO:
6850 if (hdspm->tco) {
6851 hdspm->texts_autosync = texts_autosync_aio_tco;
6852 hdspm->texts_autosync_items = 6;
6853 } else {
6854 hdspm->texts_autosync = texts_autosync_aio;
6855 hdspm->texts_autosync_items = 5;
6856 }
6857 break;
6858
6859 }
6860
6861 tasklet_init(&hdspm->midi_tasklet,
6862 hdspm_midi_tasklet, (unsigned long) hdspm);
763f356c 6863
f7de8ba3
AK
6864
6865 if (hdspm->io_type != MADIface) {
6866 hdspm->serial = (hdspm_read(hdspm,
6867 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6868 /* id contains either a user-provided value or the default
6869 * NULL. If it's the default, we're safe to
6870 * fill card->id with the serial number.
6871 *
6872 * If the serial number is 0xFFFFFF, then we're dealing with
6873 * an old PCI revision that comes without a sane number. In
6874 * this case, we don't set card->id to avoid collisions
6875 * when running with multiple cards.
6876 */
6877 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6878 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6879 snd_card_set_id(card, card->id);
6880 }
6881 }
6882
763f356c 6883 snd_printdd("create alsa devices.\n");
ef5fa1a4
TI
6884 err = snd_hdspm_create_alsa_devices(card, hdspm);
6885 if (err < 0)
763f356c
TI
6886 return err;
6887
6888 snd_hdspm_initialize_midi_flush(hdspm);
6889
6890 return 0;
6891}
6892
0dca1793 6893
98274f07 6894static int snd_hdspm_free(struct hdspm * hdspm)
763f356c
TI
6895{
6896
6897 if (hdspm->port) {
6898
6899 /* stop th audio, and cancel all interrupts */
6900 hdspm->control_register &=
ef5fa1a4 6901 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
0dca1793
AK
6902 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6903 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
763f356c
TI
6904 hdspm_write(hdspm, HDSPM_controlRegister,
6905 hdspm->control_register);
6906 }
6907
6908 if (hdspm->irq >= 0)
6909 free_irq(hdspm->irq, (void *) hdspm);
6910
fc58422a 6911 kfree(hdspm->mixer);
763f356c
TI
6912
6913 if (hdspm->iobase)
6914 iounmap(hdspm->iobase);
6915
763f356c
TI
6916 if (hdspm->port)
6917 pci_release_regions(hdspm->pci);
6918
6919 pci_disable_device(hdspm->pci);
6920 return 0;
6921}
6922
0dca1793 6923
98274f07 6924static void snd_hdspm_card_free(struct snd_card *card)
763f356c 6925{
ef5fa1a4 6926 struct hdspm *hdspm = card->private_data;
763f356c
TI
6927
6928 if (hdspm)
6929 snd_hdspm_free(hdspm);
6930}
6931
0dca1793 6932
e23e7a14
BP
6933static int snd_hdspm_probe(struct pci_dev *pci,
6934 const struct pci_device_id *pci_id)
763f356c
TI
6935{
6936 static int dev;
98274f07
TI
6937 struct hdspm *hdspm;
6938 struct snd_card *card;
763f356c
TI
6939 int err;
6940
6941 if (dev >= SNDRV_CARDS)
6942 return -ENODEV;
6943 if (!enable[dev]) {
6944 dev++;
6945 return -ENOENT;
6946 }
6947
e58de7ba 6948 err = snd_card_create(index[dev], id[dev],
0dca1793 6949 THIS_MODULE, sizeof(struct hdspm), &card);
e58de7ba
TI
6950 if (err < 0)
6951 return err;
763f356c 6952
ef5fa1a4 6953 hdspm = card->private_data;
763f356c
TI
6954 card->private_free = snd_hdspm_card_free;
6955 hdspm->dev = dev;
6956 hdspm->pci = pci;
6957
c187c041
TI
6958 snd_card_set_dev(card, &pci->dev);
6959
0dca1793 6960 err = snd_hdspm_create(card, hdspm);
ef5fa1a4 6961 if (err < 0) {
763f356c
TI
6962 snd_card_free(card);
6963 return err;
6964 }
6965
0dca1793
AK
6966 if (hdspm->io_type != MADIface) {
6967 sprintf(card->shortname, "%s_%x",
6968 hdspm->card_name,
7d53a631 6969 hdspm->serial);
0dca1793
AK
6970 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6971 hdspm->card_name,
7d53a631 6972 hdspm->serial,
0dca1793
AK
6973 hdspm->port, hdspm->irq);
6974 } else {
6975 sprintf(card->shortname, "%s", hdspm->card_name);
6976 sprintf(card->longname, "%s at 0x%lx, irq %d",
6977 hdspm->card_name, hdspm->port, hdspm->irq);
6978 }
763f356c 6979
ef5fa1a4
TI
6980 err = snd_card_register(card);
6981 if (err < 0) {
763f356c
TI
6982 snd_card_free(card);
6983 return err;
6984 }
6985
6986 pci_set_drvdata(pci, card);
6987
6988 dev++;
6989 return 0;
6990}
6991
e23e7a14 6992static void snd_hdspm_remove(struct pci_dev *pci)
763f356c
TI
6993{
6994 snd_card_free(pci_get_drvdata(pci));
763f356c
TI
6995}
6996
e9f66d9b 6997static struct pci_driver hdspm_driver = {
3733e424 6998 .name = KBUILD_MODNAME,
763f356c
TI
6999 .id_table = snd_hdspm_ids,
7000 .probe = snd_hdspm_probe,
e23e7a14 7001 .remove = snd_hdspm_remove,
763f356c
TI
7002};
7003
e9f66d9b 7004module_pci_driver(hdspm_driver);
This page took 1.09052 seconds and 5 git commands to generate.