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