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