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