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