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