blob: ad416365dc68025c17458d08da1dd89aec084d8c [file] [log] [blame]
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001/*
Takashi Iwai763f3562005-06-03 11:25:34 +02002 * 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
Remy Bruno3cee5a62006-10-16 12:46:32 +02008 * Modified 2006-06-01 for AES32 support by Remy Bruno
9 * <remy.bruno@trinnov.com>
Takashi Iwai763f3562005-06-03 11:25:34 +020010 *
Adrian Knoth0dca1792011-01-26 19:32:14 +010011 * 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 *
Takashi Iwai763f3562005-06-03 11:25:34 +020026 * 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 */
Takashi Iwai763f3562005-06-03 11:25:34 +020041#include <linux/init.h>
42#include <linux/delay.h>
43#include <linux/interrupt.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040044#include <linux/module.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020045#include <linux/slab.h>
46#include <linux/pci.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020047#include <linux/math64.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020048#include <asm/io.h>
49
50#include <sound/core.h>
51#include <sound/control.h>
52#include <sound/pcm.h>
Adrian Knoth0dca1792011-01-26 19:32:14 +010053#include <sound/pcm_params.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020054#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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103064static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Takashi Iwai763f3562005-06-03 11:25:34 +020065
Takashi Iwai763f3562005-06-03 11:25:34 +020066module_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
Takashi Iwai763f3562005-06-03 11:25:34 +020075
76MODULE_AUTHOR
Adrian Knoth0dca1792011-01-26 19:32:14 +010077(
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);
Takashi Iwai763f3562005-06-03 11:25:34 +020085MODULE_DESCRIPTION("RME HDSPM");
86MODULE_LICENSE("GPL");
87MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
88
Adrian Knoth0dca1792011-01-26 19:32:14 +010089/* --- Write registers. ---
Takashi Iwai763f3562005-06-03 11:25:34 +020090 These are defined as byte-offsets from the iobase value. */
91
Adrian Knoth0dca1792011-01-26 19:32:14 +010092#define HDSPM_WR_SETTINGS 0
93#define HDSPM_outputBufferAddress 32
94#define HDSPM_inputBufferAddress 36
Takashi Iwai763f3562005-06-03 11:25:34 +020095#define HDSPM_controlRegister 64
96#define HDSPM_interruptConfirmation 96
97#define HDSPM_control2Reg 256 /* not in specs ???????? */
Remy Brunoffb2c3c2007-03-07 19:08:46 +010098#define HDSPM_freqReg 256 /* for AES32 */
Adrian Knoth0dca1792011-01-26 19:32:14 +010099#define HDSPM_midiDataOut0 352 /* just believe in old code */
100#define HDSPM_midiDataOut1 356
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100101#define HDSPM_eeprom_wr 384 /* for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200102
103/* DMA enable for 64 channels, only Bit 0 is relevant */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100104#define HDSPM_outputEnableBase 512 /* 512-767 input DMA */
Takashi Iwai763f3562005-06-03 11:25:34 +0200105#define HDSPM_inputEnableBase 768 /* 768-1023 output DMA */
106
Adrian Knoth0dca1792011-01-26 19:32:14 +0100107/* 16 page addresses for each of the 64 channels DMA buffer in and out
Takashi Iwai763f3562005-06-03 11:25:34 +0200108 (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
Remy Bruno3cee5a62006-10-16 12:46:32 +0200119/*#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
Takashi Iwai763f3562005-06-03 11:25:34 +0200125
Adrian Knoth0dca1792011-01-26 19:32:14 +0100126/* 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
Takashi Iwai763f3562005-06-03 11:25:34 +0200171#define HDSPM_midiDataIn0 360
172#define HDSPM_midiDataIn1 364
Adrian Knoth0dca1792011-01-26 19:32:14 +0100173#define HDSPM_midiDataIn2 372
174#define HDSPM_midiDataIn3 376
Takashi Iwai763f3562005-06-03 11:25:34 +0200175
176/* status is data bytes in MIDI-FIFO (0-128) */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100177#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
Takashi Iwai763f3562005-06-03 11:25:34 +0200185
186
187/* the meters are regular i/o-mapped registers, but offset
188 considerably from the rest. the peak registers are reset
Adrian Knoth0dca1792011-01-26 19:32:14 +0100189 when read; the least-significant 4 bits are full-scale counters;
Takashi Iwai763f3562005-06-03 11:25:34 +0200190 the actual peak value is in the most-significant 24 bits.
191*/
Adrian Knoth0dca1792011-01-26 19:32:14 +0100192
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
Takashi Iwai763f3562005-06-03 11:25:34 +0200204
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
Adrian Knoth0dca1792011-01-26 19:32:14 +0100212#define HDSPM_ClockModeMaster (1<<4) /* 1=Master, 0=Autosync */
213#define HDSPM_c0Master 0x1 /* Master clock bit in settings
214 register [RayDAT, AIO] */
Takashi Iwai763f3562005-06-03 11:25:34 +0200215
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 */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200221#define HDSPM_QuadSpeed (1<<31) /* quad speed bit */
Takashi Iwai763f3562005-06-03 11:25:34 +0200222
Remy Bruno3cee5a62006-10-16 12:46:32 +0200223#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200224#define HDSPM_TX_64ch (1<<10) /* Output 64channel MODE=1,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200225 56channelMODE=0 */ /* MADI ONLY*/
226#define HDSPM_Emphasis (1<<10) /* Emphasis */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200227
Adrian Knoth0dca1792011-01-26 19:32:14 +0100228#define HDSPM_AutoInp (1<<11) /* Auto Input (takeover) == Safe Mode,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200229 0=off, 1=on */ /* MADI ONLY */
230#define HDSPM_Dolby (1<<11) /* Dolby = "NonAudio" ?? */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200231
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200232#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
233 * -- MADI ONLY
234 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200235#define HDSPM_InputSelect1 (1<<15) /* should be 0 */
236
Remy Bruno3cee5a62006-10-16 12:46:32 +0200237#define HDSPM_SyncRef2 (1<<13)
238#define HDSPM_SyncRef3 (1<<25)
Takashi Iwai763f3562005-06-03 11:25:34 +0200239
Remy Bruno3cee5a62006-10-16 12:46:32 +0200240#define HDSPM_SMUX (1<<18) /* Frame ??? */ /* MADI ONY */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100241#define HDSPM_clr_tms (1<<19) /* clear track marker, do not use
Takashi Iwai763f3562005-06-03 11:25:34 +0200242 AES additional bits in
243 lower 5 Audiodatabits ??? */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200244#define HDSPM_taxi_reset (1<<20) /* ??? */ /* MADI ONLY ? */
245#define HDSPM_WCK48 (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200246
Adrian Knoth0dca1792011-01-26 19:32:14 +0100247#define HDSPM_Midi0InterruptEnable 0x0400000
248#define HDSPM_Midi1InterruptEnable 0x0800000
249#define HDSPM_Midi2InterruptEnable 0x0200000
250#define HDSPM_Midi3InterruptEnable 0x4000000
Takashi Iwai763f3562005-06-03 11:25:34 +0200251
252#define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100253#define HDSPe_FLOAT_FORMAT 0x2000000
Takashi Iwai763f3562005-06-03 11:25:34 +0200254
Remy Bruno3cee5a62006-10-16 12:46:32 +0200255#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)
Takashi Iwai763f3562005-06-03 11:25:34 +0200260
Adrian Knoth384f7782013-07-05 11:27:53 +0200261/* 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
Takashi Iwai763f3562005-06-03 11:25:34 +0200280/* --- bit helper defines */
281#define HDSPM_LatencyMask (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200282#define HDSPM_FrequencyMask (HDSPM_Frequency0|HDSPM_Frequency1|\
283 HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
Takashi Iwai763f3562005-06-03 11:25:34 +0200284#define HDSPM_InputMask (HDSPM_InputSelect0|HDSPM_InputSelect1)
285#define HDSPM_InputOptical 0
286#define HDSPM_InputCoaxial (HDSPM_InputSelect0)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200287#define HDSPM_SyncRefMask (HDSPM_SyncRef0|HDSPM_SyncRef1|\
288 HDSPM_SyncRef2|HDSPM_SyncRef3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200289
Adrian Knoth0dca1792011-01-26 19:32:14 +0100290#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
Takashi Iwai763f3562005-06-03 11:25:34 +0200301
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)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200307#define HDSPM_Frequency96KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
308 HDSPM_Frequency0)
Remy Bruno3cee5a62006-10-16 12:46:32 +0200309#define HDSPM_Frequency128KHz (HDSPM_QuadSpeed|HDSPM_Frequency0)
310#define HDSPM_Frequency176_4KHz (HDSPM_QuadSpeed|HDSPM_Frequency1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200311#define HDSPM_Frequency192KHz (HDSPM_QuadSpeed|HDSPM_Frequency1|\
312 HDSPM_Frequency0)
Takashi Iwai763f3562005-06-03 11:25:34 +0200313
Takashi Iwai763f3562005-06-03 11:25:34 +0200314
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
Adrian Knoth0dca1792011-01-26 19:32:14 +0100323#define HDSPM_AUTOSYNC_FROM_TCO 2
324#define HDSPM_AUTOSYNC_FROM_SYNC_IN 3
325#define HDSPM_AUTOSYNC_FROM_NONE 4
Takashi Iwai763f3562005-06-03 11:25:34 +0200326
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)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100332#define hdspm_decode_latency(x) ((((x) & HDSPM_LatencyMask)>>1))
Takashi Iwai763f3562005-06-03 11:25:34 +0200333
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
Remy Bruno3cee5a62006-10-16 12:46:32 +0200350/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200351 that do not conflict with specific bits for AES32 seem to be valid also
352 for the AES32
353 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200354#define HDSPM_audioIRQPending (1<<0) /* IRQ is high and pending */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200355#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 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100359
Takashi Iwai763f3562005-06-03 11:25:34 +0200360#define HDSPM_madiLock (1<<3) /* MADI Locked =1, no=0 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100361#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! */
Takashi Iwai763f3562005-06-03 11:25:34 +0200368
369#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100370 /* since 64byte accurate, last 6 bits are not used */
Takashi Iwai763f3562005-06-03 11:25:34 +0200371
Adrian Knoth0dca1792011-01-26 19:32:14 +0100372
373
Takashi Iwai763f3562005-06-03 11:25:34 +0200374#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
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200381#define HDSPM_BufferID (1<<26) /* (Double)Buffer ID toggles with
382 * Interrupt
383 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100384#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
Takashi Iwai763f3562005-06-03 11:25:34 +0200397
398/* --- status bit helpers */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200399#define HDSPM_madiFreqMask (HDSPM_madiFreq0|HDSPM_madiFreq1|\
400 HDSPM_madiFreq2|HDSPM_madiFreq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200401#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
Remy Bruno3cee5a62006-10-16 12:46:32 +0200411/* Status2 Register bits */ /* MADI ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200412
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300413#define HDSPM_version0 (1<<0) /* not really defined but I guess */
Takashi Iwai763f3562005-06-03 11:25:34 +0200414#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, */
Adrian Knotha8cd7142013-05-31 12:57:09 +0200422#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 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200424
Adrian Knoth0dca1792011-01-26 19:32:14 +0100425#define HDSPM_SyncRef0 0x10000 /* Sync Reference */
426#define HDSPM_SyncRef1 0x20000
427
428#define HDSPM_SelSyncRef0 (1<<8) /* AutoSync Source */
Takashi Iwai763f3562005-06-03 11:25:34 +0200429#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
Adrian Knotha8cd7142013-05-31 12:57:09 +0200434#define HDSPM_wcFreqMask (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2|\
435 HDSPM_wc_freq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200436#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)
Adrian Knotha8cd7142013-05-31 12:57:09 +0200442#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)
Takashi Iwai763f3562005-06-03 11:25:34 +0200445
Adrian Knoth0dca1792011-01-26 19:32:14 +0100446#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
Takashi Iwai763f3562005-06-03 11:25:34 +0200452
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200453#define HDSPM_SelSyncRefMask (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
454 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200455#define HDSPM_SelSyncRef_WORD 0
456#define HDSPM_SelSyncRef_MADI (HDSPM_SelSyncRef0)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100457#define HDSPM_SelSyncRef_TCO (HDSPM_SelSyncRef1)
458#define HDSPM_SelSyncRef_SyncIn (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200459#define HDSPM_SelSyncRef_NVALID (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
460 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200461
Remy Bruno3cee5a62006-10-16 12:46:32 +0200462/*
463 For AES32, bits for status, status2 and timecode are different
464*/
465/* status */
466#define HDSPM_AES32_wcLock 0x0200000
Andre Schramm56bde0f2013-01-09 14:40:18 +0100467#define HDSPM_AES32_wcSync 0x0100000
Remy Bruno3cee5a62006-10-16 12:46:32 +0200468#define HDSPM_AES32_wcFreq_bit 22
Adrian Knoth0dca1792011-01-26 19:32:14 +0100469/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
Remy Bruno3cee5a62006-10-16 12:46:32 +0200470 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
Remy Bruno65345992007-08-31 12:21:08 +0200483#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
Remy Bruno3cee5a62006-10-16 12:46:32 +0200484
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
Takashi Iwai763f3562005-06-03 11:25:34 +0200513/* Mixer Values */
514#define UNITY_GAIN 32768 /* = 65536/2 */
515#define MINUS_INFINITY_GAIN 0
516
Takashi Iwai763f3562005-06-03 11:25:34 +0200517/* 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
Adrian Knoth0dca1792011-01-26 19:32:14 +0100522#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
Adrian Knothd2d10a22011-02-28 15:14:47 +0100533#define AES32_CHANNELS 16
534
Takashi Iwai763f3562005-06-03 11:25:34 +0200535/* 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
Adrian Knoth0dca1792011-01-26 19:32:14 +0100541 also the latency to use.
Takashi Iwai763f3562005-06-03 11:25:34 +0200542 for one direction !!!
543*/
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100544#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
Takashi Iwai763f3562005-06-03 11:25:34 +0200545#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
546
Adrian Knoth0dca1792011-01-26 19:32:14 +0100547#define HDSPM_RAYDAT_REV 211
548#define HDSPM_AIO_REV 212
549#define HDSPM_MADIFACE_REV 213
Remy Bruno3cee5a62006-10-16 12:46:32 +0200550
Remy Bruno65345992007-08-31 12:21:08 +0200551/* speed factor modes */
552#define HDSPM_SPEED_SINGLE 0
553#define HDSPM_SPEED_DOUBLE 1
554#define HDSPM_SPEED_QUAD 2
Adrian Knoth0dca1792011-01-26 19:32:14 +0100555
Remy Bruno65345992007-08-31 12:21:08 +0200556/* names for speed modes */
557static char *hdspm_speed_names[] = { "single", "double", "quad" };
558
Adrian Knoth0dca1792011-01-26 19:32:14 +0100559static 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
Adrian Knoth0dca1792011-01-26 19:32:14 +0100601static 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",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200651 "ADAT.7", "ADAT.8",
652 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100653};
654
655static char *texts_ports_aio_out_ss[] = {
656 "Analogue.L", "Analogue.R",
657 "AES.L", "AES.R",
658 "SPDIF.L", "SPDIF.R",
659 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
660 "ADAT.7", "ADAT.8",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200661 "Phone.L", "Phone.R",
662 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100663};
664
665static char *texts_ports_aio_in_ds[] = {
666 "Analogue.L", "Analogue.R",
667 "AES.L", "AES.R",
668 "SPDIF.L", "SPDIF.R",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200669 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
670 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100671};
672
673static char *texts_ports_aio_out_ds[] = {
674 "Analogue.L", "Analogue.R",
675 "AES.L", "AES.R",
676 "SPDIF.L", "SPDIF.R",
677 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200678 "Phone.L", "Phone.R",
679 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100680};
681
682static char *texts_ports_aio_in_qs[] = {
683 "Analogue.L", "Analogue.R",
684 "AES.L", "AES.R",
685 "SPDIF.L", "SPDIF.R",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200686 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
687 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100688};
689
690static char *texts_ports_aio_out_qs[] = {
691 "Analogue.L", "Analogue.R",
692 "AES.L", "AES.R",
693 "SPDIF.L", "SPDIF.R",
694 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200695 "Phone.L", "Phone.R",
696 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100697};
698
Adrian Knoth432d2502011-02-23 11:43:08 +0100699static char *texts_ports_aes32[] = {
700 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
701 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
702 "AES.15", "AES.16"
703};
704
Adrian Knoth55a57602011-01-27 11:23:15 +0100705/* These tables map the ALSA channels 1..N to the channels that we
706 need to use in order to find the relevant channel buffer. RME
707 refers to this kind of mapping as between "the ADAT channel and
708 the DMA channel." We index it using the logical audio channel,
709 and the value is the DMA channel (i.e. channel buffer number)
710 where the data for that channel can be read/written from/to.
711*/
712
713static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
714 0, 1, 2, 3, 4, 5, 6, 7,
715 8, 9, 10, 11, 12, 13, 14, 15,
716 16, 17, 18, 19, 20, 21, 22, 23,
717 24, 25, 26, 27, 28, 29, 30, 31,
718 32, 33, 34, 35, 36, 37, 38, 39,
719 40, 41, 42, 43, 44, 45, 46, 47,
720 48, 49, 50, 51, 52, 53, 54, 55,
721 56, 57, 58, 59, 60, 61, 62, 63
722};
723
Adrian Knoth55a57602011-01-27 11:23:15 +0100724static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
725 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
726 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
727 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
728 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
729 0, 1, /* AES */
730 2, 3, /* SPDIF */
731 -1, -1, -1, -1,
732 -1, -1, -1, -1, -1, -1, -1, -1,
733 -1, -1, -1, -1, -1, -1, -1, -1,
734 -1, -1, -1, -1, -1, -1, -1, -1,
735};
736
737static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
738 4, 5, 6, 7, /* ADAT 1 */
739 8, 9, 10, 11, /* ADAT 2 */
740 12, 13, 14, 15, /* ADAT 3 */
741 16, 17, 18, 19, /* ADAT 4 */
742 0, 1, /* AES */
743 2, 3, /* SPDIF */
744 -1, -1, -1, -1,
745 -1, -1, -1, -1, -1, -1, -1, -1,
746 -1, -1, -1, -1, -1, -1, -1, -1,
747 -1, -1, -1, -1, -1, -1, -1, -1,
748 -1, -1, -1, -1, -1, -1, -1, -1,
749 -1, -1, -1, -1, -1, -1, -1, -1,
750};
751
752static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
753 4, 5, /* ADAT 1 */
754 6, 7, /* ADAT 2 */
755 8, 9, /* ADAT 3 */
756 10, 11, /* ADAT 4 */
757 0, 1, /* AES */
758 2, 3, /* SPDIF */
759 -1, -1, -1, -1,
760 -1, -1, -1, -1, -1, -1, -1, -1,
761 -1, -1, -1, -1, -1, -1, -1, -1,
762 -1, -1, -1, -1, -1, -1, -1, -1,
763 -1, -1, -1, -1, -1, -1, -1, -1,
764 -1, -1, -1, -1, -1, -1, -1, -1,
765 -1, -1, -1, -1, -1, -1, -1, -1,
766};
767
768static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
769 0, 1, /* line in */
770 8, 9, /* aes in, */
771 10, 11, /* spdif in */
772 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200773 2, 3, 4, 5, /* AEB */
774 -1, -1, -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100775 -1, -1, -1, -1, -1, -1, -1, -1,
776 -1, -1, -1, -1, -1, -1, -1, -1,
777 -1, -1, -1, -1, -1, -1, -1, -1,
778 -1, -1, -1, -1, -1, -1, -1, -1,
779 -1, -1, -1, -1, -1, -1, -1, -1,
780};
781
782static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
783 0, 1, /* line out */
784 8, 9, /* aes out */
785 10, 11, /* spdif out */
786 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
787 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200788 2, 3, 4, 5, /* AEB */
789 -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100790 -1, -1, -1, -1, -1, -1, -1, -1,
791 -1, -1, -1, -1, -1, -1, -1, -1,
792 -1, -1, -1, -1, -1, -1, -1, -1,
793 -1, -1, -1, -1, -1, -1, -1, -1,
794 -1, -1, -1, -1, -1, -1, -1, -1,
795};
796
797static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
798 0, 1, /* line in */
799 8, 9, /* aes in */
800 10, 11, /* spdif in */
801 12, 14, 16, 18, /* adat in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200802 2, 3, 4, 5, /* AEB */
803 -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100804 -1, -1, -1, -1, -1, -1, -1, -1,
805 -1, -1, -1, -1, -1, -1, -1, -1,
806 -1, -1, -1, -1, -1, -1, -1, -1,
807 -1, -1, -1, -1, -1, -1, -1, -1,
808 -1, -1, -1, -1, -1, -1, -1, -1,
809 -1, -1, -1, -1, -1, -1, -1, -1
810};
811
812static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
813 0, 1, /* line out */
814 8, 9, /* aes out */
815 10, 11, /* spdif out */
816 12, 14, 16, 18, /* adat out */
817 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200818 2, 3, 4, 5, /* AEB */
Adrian Knoth55a57602011-01-27 11:23:15 +0100819 -1, -1, -1, -1, -1, -1, -1, -1,
820 -1, -1, -1, -1, -1, -1, -1, -1,
821 -1, -1, -1, -1, -1, -1, -1, -1,
822 -1, -1, -1, -1, -1, -1, -1, -1,
823 -1, -1, -1, -1, -1, -1, -1, -1,
824 -1, -1, -1, -1, -1, -1, -1, -1
825};
826
827static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
828 0, 1, /* line in */
829 8, 9, /* aes in */
830 10, 11, /* spdif in */
831 12, 16, /* adat in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200832 2, 3, 4, 5, /* AEB */
833 -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100834 -1, -1, -1, -1, -1, -1, -1, -1,
835 -1, -1, -1, -1, -1, -1, -1, -1,
836 -1, -1, -1, -1, -1, -1, -1, -1,
837 -1, -1, -1, -1, -1, -1, -1, -1,
838 -1, -1, -1, -1, -1, -1, -1, -1,
839 -1, -1, -1, -1, -1, -1, -1, -1
840};
841
842static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
843 0, 1, /* line out */
844 8, 9, /* aes out */
845 10, 11, /* spdif out */
846 12, 16, /* adat out */
847 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200848 2, 3, 4, 5, /* AEB */
849 -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100850 -1, -1, -1, -1, -1, -1, -1, -1,
851 -1, -1, -1, -1, -1, -1, -1, -1,
852 -1, -1, -1, -1, -1, -1, -1, -1,
853 -1, -1, -1, -1, -1, -1, -1, -1,
854 -1, -1, -1, -1, -1, -1, -1, -1,
855 -1, -1, -1, -1, -1, -1, -1, -1
856};
857
Adrian Knoth432d2502011-02-23 11:43:08 +0100858static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
859 0, 1, 2, 3, 4, 5, 6, 7,
860 8, 9, 10, 11, 12, 13, 14, 15,
861 -1, -1, -1, -1, -1, -1, -1, -1,
862 -1, -1, -1, -1, -1, -1, -1, -1,
863 -1, -1, -1, -1, -1, -1, -1, -1,
864 -1, -1, -1, -1, -1, -1, -1, -1,
865 -1, -1, -1, -1, -1, -1, -1, -1,
866 -1, -1, -1, -1, -1, -1, -1, -1
867};
868
Takashi Iwai98274f02005-11-17 14:52:34 +0100869struct hdspm_midi {
870 struct hdspm *hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +0200871 int id;
Takashi Iwai98274f02005-11-17 14:52:34 +0100872 struct snd_rawmidi *rmidi;
873 struct snd_rawmidi_substream *input;
874 struct snd_rawmidi_substream *output;
Takashi Iwai763f3562005-06-03 11:25:34 +0200875 char istimer; /* timer in use */
876 struct timer_list timer;
877 spinlock_t lock;
878 int pending;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100879 int dataIn;
880 int statusIn;
881 int dataOut;
882 int statusOut;
883 int ie;
884 int irq;
885};
886
887struct hdspm_tco {
888 int input;
889 int framerate;
890 int wordclock;
891 int samplerate;
892 int pull;
893 int term; /* 0 = off, 1 = on */
Takashi Iwai763f3562005-06-03 11:25:34 +0200894};
895
Takashi Iwai98274f02005-11-17 14:52:34 +0100896struct hdspm {
Takashi Iwai763f3562005-06-03 11:25:34 +0200897 spinlock_t lock;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200898 /* only one playback and/or capture stream */
899 struct snd_pcm_substream *capture_substream;
900 struct snd_pcm_substream *playback_substream;
Takashi Iwai763f3562005-06-03 11:25:34 +0200901
902 char *card_name; /* for procinfo */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200903 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
904
Adrian Knoth0dca1792011-01-26 19:32:14 +0100905 uint8_t io_type;
Takashi Iwai763f3562005-06-03 11:25:34 +0200906
Takashi Iwai763f3562005-06-03 11:25:34 +0200907 int monitor_outs; /* set up monitoring outs init flag */
908
909 u32 control_register; /* cached value */
910 u32 control2_register; /* cached value */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100911 u32 settings_register;
Takashi Iwai763f3562005-06-03 11:25:34 +0200912
Adrian Knoth0dca1792011-01-26 19:32:14 +0100913 struct hdspm_midi midi[4];
Takashi Iwai763f3562005-06-03 11:25:34 +0200914 struct tasklet_struct midi_tasklet;
915
916 size_t period_bytes;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100917 unsigned char ss_in_channels;
918 unsigned char ds_in_channels;
919 unsigned char qs_in_channels;
920 unsigned char ss_out_channels;
921 unsigned char ds_out_channels;
922 unsigned char qs_out_channels;
923
924 unsigned char max_channels_in;
925 unsigned char max_channels_out;
926
Takashi Iwai286bed02011-06-30 12:45:36 +0200927 signed char *channel_map_in;
928 signed char *channel_map_out;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100929
Takashi Iwai286bed02011-06-30 12:45:36 +0200930 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
931 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100932
933 char **port_names_in;
934 char **port_names_out;
935
936 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
937 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
Takashi Iwai763f3562005-06-03 11:25:34 +0200938
939 unsigned char *playback_buffer; /* suitably aligned address */
940 unsigned char *capture_buffer; /* suitably aligned address */
941
942 pid_t capture_pid; /* process id which uses capture */
943 pid_t playback_pid; /* process id which uses capture */
944 int running; /* running status */
945
946 int last_external_sample_rate; /* samplerate mystic ... */
947 int last_internal_sample_rate;
948 int system_sample_rate;
949
Takashi Iwai763f3562005-06-03 11:25:34 +0200950 int dev; /* Hardware vars... */
951 int irq;
952 unsigned long port;
953 void __iomem *iobase;
954
955 int irq_count; /* for debug */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100956 int midiPorts;
Takashi Iwai763f3562005-06-03 11:25:34 +0200957
Takashi Iwai98274f02005-11-17 14:52:34 +0100958 struct snd_card *card; /* one card */
959 struct snd_pcm *pcm; /* has one pcm */
960 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
Takashi Iwai763f3562005-06-03 11:25:34 +0200961 struct pci_dev *pci; /* and an pci info */
962
963 /* Mixer vars */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200964 /* fast alsa mixer */
965 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
966 /* but input to much, so not used */
967 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300968 /* full mixer accessible over mixer ioctl or hwdep-device */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200969 struct hdspm_mixer *mixer;
Takashi Iwai763f3562005-06-03 11:25:34 +0200970
Adrian Knoth0dca1792011-01-26 19:32:14 +0100971 struct hdspm_tco *tco; /* NULL if no TCO detected */
Takashi Iwai763f3562005-06-03 11:25:34 +0200972
Adrian Knoth0dca1792011-01-26 19:32:14 +0100973 char **texts_autosync;
974 int texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +0200975
Adrian Knoth0dca1792011-01-26 19:32:14 +0100976 cycles_t last_interrupt;
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100977
Adrian Knoth7d53a632012-01-04 14:31:16 +0100978 unsigned int serial;
979
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100980 struct hdspm_peak_rms peak_rms;
Takashi Iwai763f3562005-06-03 11:25:34 +0200981};
982
Takashi Iwai763f3562005-06-03 11:25:34 +0200983
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200984static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
Takashi Iwai763f3562005-06-03 11:25:34 +0200985 {
986 .vendor = PCI_VENDOR_ID_XILINX,
987 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
988 .subvendor = PCI_ANY_ID,
989 .subdevice = PCI_ANY_ID,
990 .class = 0,
991 .class_mask = 0,
992 .driver_data = 0},
993 {0,}
994};
995
996MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
997
998/* prototypes */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500999static int snd_hdspm_create_alsa_devices(struct snd_card *card,
1000 struct hdspm *hdspm);
1001static int snd_hdspm_create_pcm(struct snd_card *card,
1002 struct hdspm *hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001003
Adrian Knoth0dca1792011-01-26 19:32:14 +01001004static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
Adrian Knoth3f7bf912013-03-10 00:37:21 +01001005static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001006static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
1007static int hdspm_autosync_ref(struct hdspm *hdspm);
Adrian Knoth34be7eb2013-07-05 11:27:56 +02001008static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001009static int snd_hdspm_set_defaults(struct hdspm *hdspm);
Adrian Knoth21a164d2012-10-19 17:42:23 +02001010static int hdspm_system_clock_mode(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001011static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02001012 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02001013 unsigned int reg, int channels);
1014
Remy Bruno3cee5a62006-10-16 12:46:32 +02001015static inline int HDSPM_bit2freq(int n)
1016{
Denys Vlasenko62cef822008-04-14 13:04:18 +02001017 static const int bit2freq_tab[] = {
1018 0, 32000, 44100, 48000, 64000, 88200,
Remy Bruno3cee5a62006-10-16 12:46:32 +02001019 96000, 128000, 176400, 192000 };
1020 if (n < 1 || n > 9)
1021 return 0;
1022 return bit2freq_tab[n];
1023}
1024
Adrian Knothb2ed6322013-07-05 11:27:54 +02001025static bool hdspm_is_raydat_or_aio(struct hdspm *hdspm)
1026{
1027 return ((AIO == hdspm->io_type) || (RayDAT == hdspm->io_type));
1028}
1029
1030
Adrian Knoth0dca1792011-01-26 19:32:14 +01001031/* Write/read to/from HDSPM with Adresses in Bytes
Takashi Iwai763f3562005-06-03 11:25:34 +02001032 not words but only 32Bit writes are allowed */
1033
Takashi Iwai98274f02005-11-17 14:52:34 +01001034static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
Takashi Iwai763f3562005-06-03 11:25:34 +02001035 unsigned int val)
1036{
1037 writel(val, hdspm->iobase + reg);
1038}
1039
Takashi Iwai98274f02005-11-17 14:52:34 +01001040static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
Takashi Iwai763f3562005-06-03 11:25:34 +02001041{
1042 return readl(hdspm->iobase + reg);
1043}
1044
Adrian Knoth0dca1792011-01-26 19:32:14 +01001045/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1046 mixer is write only on hardware so we have to cache him for read
Takashi Iwai763f3562005-06-03 11:25:34 +02001047 each fader is a u32, but uses only the first 16 bit */
1048
Takashi Iwai98274f02005-11-17 14:52:34 +01001049static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001050 unsigned int in)
1051{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001052 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001053 return 0;
1054
1055 return hdspm->mixer->ch[chan].in[in];
1056}
1057
Takashi Iwai98274f02005-11-17 14:52:34 +01001058static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001059 unsigned int pb)
1060{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001061 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001062 return 0;
1063 return hdspm->mixer->ch[chan].pb[pb];
1064}
1065
Denys Vlasenko62cef822008-04-14 13:04:18 +02001066static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001067 unsigned int in, unsigned short data)
1068{
1069 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1070 return -1;
1071
1072 hdspm_write(hdspm,
1073 HDSPM_MADI_mixerBase +
1074 ((in + 128 * chan) * sizeof(u32)),
1075 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1076 return 0;
1077}
1078
Denys Vlasenko62cef822008-04-14 13:04:18 +02001079static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001080 unsigned int pb, unsigned short data)
1081{
1082 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1083 return -1;
1084
1085 hdspm_write(hdspm,
1086 HDSPM_MADI_mixerBase +
1087 ((64 + pb + 128 * chan) * sizeof(u32)),
1088 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1089 return 0;
1090}
1091
1092
1093/* enable DMA for specific channels, now available for DSP-MADI */
Takashi Iwai98274f02005-11-17 14:52:34 +01001094static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001095{
1096 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1097}
1098
Takashi Iwai98274f02005-11-17 14:52:34 +01001099static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001100{
1101 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1102}
1103
1104/* check if same process is writing and reading */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001105static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001106{
1107 unsigned long flags;
1108 int ret = 1;
1109
1110 spin_lock_irqsave(&hdspm->lock, flags);
1111 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1112 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1113 ret = 0;
1114 }
1115 spin_unlock_irqrestore(&hdspm->lock, flags);
1116 return ret;
1117}
1118
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001119/* round arbitary sample rates to commonly known rates */
1120static int hdspm_round_frequency(int rate)
1121{
1122 if (rate < 38050)
1123 return 32000;
1124 if (rate < 46008)
1125 return 44100;
1126 else
1127 return 48000;
1128}
1129
Adrian Knotha8a729f2013-05-31 12:57:10 +02001130/* QS and DS rates normally can not be detected
1131 * automatically by the card. Only exception is MADI
1132 * in 96k frame mode.
1133 *
1134 * So if we read SS values (32 .. 48k), check for
1135 * user-provided DS/QS bits in the control register
1136 * and multiply the base frequency accordingly.
1137 */
1138static int hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
1139{
1140 if (rate <= 48000) {
1141 if (hdspm->control_register & HDSPM_QuadSpeed)
1142 return rate * 4;
1143 else if (hdspm->control_register &
1144 HDSPM_DoubleSpeed)
1145 return rate * 2;
1146 };
1147 return rate;
1148}
1149
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001150static int hdspm_tco_sync_check(struct hdspm *hdspm);
1151static int hdspm_sync_in_sync_check(struct hdspm *hdspm);
1152
Takashi Iwai763f3562005-06-03 11:25:34 +02001153/* check for external sample rate */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001154static int hdspm_external_sample_rate(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001155{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001156 unsigned int status, status2, timecode;
1157 int syncref, rate = 0, rate_bits;
Takashi Iwai763f3562005-06-03 11:25:34 +02001158
Adrian Knoth0dca1792011-01-26 19:32:14 +01001159 switch (hdspm->io_type) {
1160 case AES32:
1161 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1162 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01001163 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001164
1165 syncref = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001166
Remy Bruno3cee5a62006-10-16 12:46:32 +02001167 if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
1168 status & HDSPM_AES32_wcLock)
Adrian Knoth0dca1792011-01-26 19:32:14 +01001169 return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF);
1170
Remy Bruno3cee5a62006-10-16 12:46:32 +02001171 if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001172 syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
1173 status2 & (HDSPM_LockAES >>
1174 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
1175 return HDSPM_bit2freq((timecode >> (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001176 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001177 break;
1178
1179 case MADIface:
1180 status = hdspm_read(hdspm, HDSPM_statusRegister);
1181
1182 if (!(status & HDSPM_madiLock)) {
1183 rate = 0; /* no lock */
1184 } else {
1185 switch (status & (HDSPM_status1_freqMask)) {
1186 case HDSPM_status1_F_0*1:
1187 rate = 32000; break;
1188 case HDSPM_status1_F_0*2:
1189 rate = 44100; break;
1190 case HDSPM_status1_F_0*3:
1191 rate = 48000; break;
1192 case HDSPM_status1_F_0*4:
1193 rate = 64000; break;
1194 case HDSPM_status1_F_0*5:
1195 rate = 88200; break;
1196 case HDSPM_status1_F_0*6:
1197 rate = 96000; break;
1198 case HDSPM_status1_F_0*7:
1199 rate = 128000; break;
1200 case HDSPM_status1_F_0*8:
1201 rate = 176400; break;
1202 case HDSPM_status1_F_0*9:
1203 rate = 192000; break;
1204 default:
1205 rate = 0; break;
1206 }
1207 }
1208
1209 break;
1210
1211 case MADI:
1212 case AIO:
1213 case RayDAT:
1214 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1215 status = hdspm_read(hdspm, HDSPM_statusRegister);
1216 rate = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02001217
Remy Bruno3cee5a62006-10-16 12:46:32 +02001218 /* if wordclock has synced freq and wordclock is valid */
1219 if ((status2 & HDSPM_wcLock) != 0 &&
Adrian Knothfedf1532011-06-12 17:26:18 +02001220 (status2 & HDSPM_SelSyncRef0) == 0) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02001221
1222 rate_bits = status2 & HDSPM_wcFreqMask;
1223
Adrian Knoth0dca1792011-01-26 19:32:14 +01001224
Remy Bruno3cee5a62006-10-16 12:46:32 +02001225 switch (rate_bits) {
1226 case HDSPM_wcFreq32:
1227 rate = 32000;
1228 break;
1229 case HDSPM_wcFreq44_1:
1230 rate = 44100;
1231 break;
1232 case HDSPM_wcFreq48:
1233 rate = 48000;
1234 break;
1235 case HDSPM_wcFreq64:
1236 rate = 64000;
1237 break;
1238 case HDSPM_wcFreq88_2:
1239 rate = 88200;
1240 break;
1241 case HDSPM_wcFreq96:
1242 rate = 96000;
1243 break;
Adrian Knotha8cd7142013-05-31 12:57:09 +02001244 case HDSPM_wcFreq128:
1245 rate = 128000;
1246 break;
1247 case HDSPM_wcFreq176_4:
1248 rate = 176400;
1249 break;
1250 case HDSPM_wcFreq192:
1251 rate = 192000;
1252 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001253 default:
1254 rate = 0;
1255 break;
1256 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001257 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001258
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001259 /* if rate detected and Syncref is Word than have it,
1260 * word has priority to MADI
1261 */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001262 if (rate != 0 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001263 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
Adrian Knoth7b559392013-05-31 12:57:11 +02001264 return hdspm_rate_multiplier(hdspm, rate);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001265
Adrian Knoth0dca1792011-01-26 19:32:14 +01001266 /* maybe a madi input (which is taken if sel sync is madi) */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001267 if (status & HDSPM_madiLock) {
1268 rate_bits = status & HDSPM_madiFreqMask;
1269
1270 switch (rate_bits) {
1271 case HDSPM_madiFreq32:
1272 rate = 32000;
1273 break;
1274 case HDSPM_madiFreq44_1:
1275 rate = 44100;
1276 break;
1277 case HDSPM_madiFreq48:
1278 rate = 48000;
1279 break;
1280 case HDSPM_madiFreq64:
1281 rate = 64000;
1282 break;
1283 case HDSPM_madiFreq88_2:
1284 rate = 88200;
1285 break;
1286 case HDSPM_madiFreq96:
1287 rate = 96000;
1288 break;
1289 case HDSPM_madiFreq128:
1290 rate = 128000;
1291 break;
1292 case HDSPM_madiFreq176_4:
1293 rate = 176400;
1294 break;
1295 case HDSPM_madiFreq192:
1296 rate = 192000;
1297 break;
1298 default:
1299 rate = 0;
1300 break;
1301 }
Adrian Knothd12c51d2011-07-29 03:11:03 +02001302
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001303 } /* endif HDSPM_madiLock */
1304
1305 /* check sample rate from TCO or SYNC_IN */
1306 {
1307 bool is_valid_input = 0;
1308 bool has_sync = 0;
1309
1310 syncref = hdspm_autosync_ref(hdspm);
1311 if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
1312 is_valid_input = 1;
1313 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1314 hdspm_tco_sync_check(hdspm));
1315 } else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
1316 is_valid_input = 1;
1317 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1318 hdspm_sync_in_sync_check(hdspm));
Adrian Knothd12c51d2011-07-29 03:11:03 +02001319 }
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001320
1321 if (is_valid_input && has_sync) {
1322 rate = hdspm_round_frequency(
1323 hdspm_get_pll_freq(hdspm));
1324 }
1325 }
1326
Adrian Knotha8a729f2013-05-31 12:57:10 +02001327 rate = hdspm_rate_multiplier(hdspm, rate);
1328
Adrian Knoth0dca1792011-01-26 19:32:14 +01001329 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001330 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001331
1332 return rate;
Takashi Iwai763f3562005-06-03 11:25:34 +02001333}
1334
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001335/* return latency in samples per period */
1336static int hdspm_get_latency(struct hdspm *hdspm)
1337{
1338 int n;
1339
1340 n = hdspm_decode_latency(hdspm->control_register);
1341
1342 /* Special case for new RME cards with 32 samples period size.
1343 * The three latency bits in the control register
1344 * (HDSP_LatencyMask) encode latency values of 64 samples as
1345 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
1346 * denotes 8192 samples, but on new cards like RayDAT or AIO,
1347 * it corresponds to 32 samples.
1348 */
1349 if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
1350 n = -1;
1351
1352 return 1 << (n + 6);
1353}
1354
Takashi Iwai763f3562005-06-03 11:25:34 +02001355/* Latency function */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001356static inline void hdspm_compute_period_size(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001357{
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001358 hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001359}
1360
Adrian Knoth0dca1792011-01-26 19:32:14 +01001361
1362static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001363{
1364 int position;
1365
1366 position = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth483cee72011-02-23 11:43:09 +01001367
1368 switch (hdspm->io_type) {
1369 case RayDAT:
1370 case AIO:
1371 position &= HDSPM_BufferPositionMask;
1372 position /= 4; /* Bytes per sample */
1373 break;
1374 default:
1375 position = (position & HDSPM_BufferID) ?
1376 (hdspm->period_bytes / 4) : 0;
1377 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001378
1379 return position;
1380}
1381
1382
Takashi Iwai98274f02005-11-17 14:52:34 +01001383static inline void hdspm_start_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001384{
1385 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1386 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1387}
1388
Takashi Iwai98274f02005-11-17 14:52:34 +01001389static inline void hdspm_stop_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001390{
1391 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1392 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1393}
1394
1395/* should I silence all or only opened ones ? doit all for first even is 4MB*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001396static void hdspm_silence_playback(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001397{
1398 int i;
1399 int n = hdspm->period_bytes;
1400 void *buf = hdspm->playback_buffer;
1401
Remy Bruno3cee5a62006-10-16 12:46:32 +02001402 if (buf == NULL)
1403 return;
Takashi Iwai763f3562005-06-03 11:25:34 +02001404
1405 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1406 memset(buf, 0, n);
1407 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1408 }
1409}
1410
Adrian Knoth0dca1792011-01-26 19:32:14 +01001411static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
Takashi Iwai763f3562005-06-03 11:25:34 +02001412{
1413 int n;
1414
1415 spin_lock_irq(&s->lock);
1416
Adrian Knoth2e610272011-08-15 00:22:54 +02001417 if (32 == frames) {
1418 /* Special case for new RME cards like RayDAT/AIO which
1419 * support period sizes of 32 samples. Since latency is
1420 * encoded in the three bits of HDSP_LatencyMask, we can only
1421 * have values from 0 .. 7. While 0 still means 64 samples and
1422 * 6 represents 4096 samples on all cards, 7 represents 8192
1423 * on older cards and 32 samples on new cards.
1424 *
1425 * In other words, period size in samples is calculated by
1426 * 2^(n+6) with n ranging from 0 .. 7.
1427 */
1428 n = 7;
1429 } else {
1430 frames >>= 7;
1431 n = 0;
1432 while (frames) {
1433 n++;
1434 frames >>= 1;
1435 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001436 }
Adrian Knoth2e610272011-08-15 00:22:54 +02001437
Takashi Iwai763f3562005-06-03 11:25:34 +02001438 s->control_register &= ~HDSPM_LatencyMask;
1439 s->control_register |= hdspm_encode_latency(n);
1440
1441 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1442
1443 hdspm_compute_period_size(s);
1444
1445 spin_unlock_irq(&s->lock);
1446
1447 return 0;
1448}
1449
Adrian Knoth0dca1792011-01-26 19:32:14 +01001450static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1451{
1452 u64 freq_const;
1453
1454 if (period == 0)
1455 return 0;
1456
1457 switch (hdspm->io_type) {
1458 case MADI:
1459 case AES32:
1460 freq_const = 110069313433624ULL;
1461 break;
1462 case RayDAT:
1463 case AIO:
1464 freq_const = 104857600000000ULL;
1465 break;
1466 case MADIface:
1467 freq_const = 131072000000000ULL;
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001468 break;
1469 default:
1470 snd_BUG();
1471 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001472 }
1473
1474 return div_u64(freq_const, period);
1475}
1476
1477
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001478static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1479{
1480 u64 n;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001481
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001482 if (rate >= 112000)
1483 rate /= 4;
1484 else if (rate >= 56000)
1485 rate /= 2;
1486
Adrian Knoth0dca1792011-01-26 19:32:14 +01001487 switch (hdspm->io_type) {
1488 case MADIface:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001489 n = 131072000000000ULL; /* 125 MHz */
1490 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001491 case MADI:
1492 case AES32:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001493 n = 110069313433624ULL; /* 105 MHz */
1494 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001495 case RayDAT:
1496 case AIO:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001497 n = 104857600000000ULL; /* 100 MHz */
1498 break;
1499 default:
1500 snd_BUG();
1501 return;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001502 }
1503
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001504 n = div_u64(n, rate);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001505 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001506 snd_BUG_ON(n >> 32);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001507 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1508}
Takashi Iwai763f3562005-06-03 11:25:34 +02001509
1510/* dummy set rate lets see what happens */
Takashi Iwai98274f02005-11-17 14:52:34 +01001511static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
Takashi Iwai763f3562005-06-03 11:25:34 +02001512{
Takashi Iwai763f3562005-06-03 11:25:34 +02001513 int current_rate;
1514 int rate_bits;
1515 int not_set = 0;
Remy Bruno65345992007-08-31 12:21:08 +02001516 int current_speed, target_speed;
Takashi Iwai763f3562005-06-03 11:25:34 +02001517
1518 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1519 it (e.g. during module initialization).
1520 */
1521
1522 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1523
Adrian Knoth0dca1792011-01-26 19:32:14 +01001524 /* SLAVE --- */
Takashi Iwai763f3562005-06-03 11:25:34 +02001525 if (called_internally) {
1526
Adrian Knoth0dca1792011-01-26 19:32:14 +01001527 /* request from ctl or card initialization
1528 just make a warning an remember setting
1529 for future master mode switching */
1530
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001531 snd_printk(KERN_WARNING "HDSPM: "
1532 "Warning: device is not running "
1533 "as a clock master.\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001534 not_set = 1;
1535 } else {
1536
1537 /* hw_param request while in AutoSync mode */
1538 int external_freq =
1539 hdspm_external_sample_rate(hdspm);
1540
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001541 if (hdspm_autosync_ref(hdspm) ==
1542 HDSPM_AUTOSYNC_FROM_NONE) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001543
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001544 snd_printk(KERN_WARNING "HDSPM: "
1545 "Detected no Externel Sync \n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001546 not_set = 1;
1547
1548 } else if (rate != external_freq) {
1549
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001550 snd_printk(KERN_WARNING "HDSPM: "
1551 "Warning: No AutoSync source for "
1552 "requested rate\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001553 not_set = 1;
1554 }
1555 }
1556 }
1557
1558 current_rate = hdspm->system_sample_rate;
1559
1560 /* Changing between Singe, Double and Quad speed is not
1561 allowed if any substreams are open. This is because such a change
1562 causes a shift in the location of the DMA buffers and a reduction
1563 in the number of available buffers.
1564
1565 Note that a similar but essentially insoluble problem exists for
1566 externally-driven rate changes. All we can do is to flag rate
Adrian Knoth0dca1792011-01-26 19:32:14 +01001567 changes in the read/write routines.
Takashi Iwai763f3562005-06-03 11:25:34 +02001568 */
1569
Remy Bruno65345992007-08-31 12:21:08 +02001570 if (current_rate <= 48000)
1571 current_speed = HDSPM_SPEED_SINGLE;
1572 else if (current_rate <= 96000)
1573 current_speed = HDSPM_SPEED_DOUBLE;
1574 else
1575 current_speed = HDSPM_SPEED_QUAD;
1576
1577 if (rate <= 48000)
1578 target_speed = HDSPM_SPEED_SINGLE;
1579 else if (rate <= 96000)
1580 target_speed = HDSPM_SPEED_DOUBLE;
1581 else
1582 target_speed = HDSPM_SPEED_QUAD;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001583
Takashi Iwai763f3562005-06-03 11:25:34 +02001584 switch (rate) {
1585 case 32000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001586 rate_bits = HDSPM_Frequency32KHz;
1587 break;
1588 case 44100:
Takashi Iwai763f3562005-06-03 11:25:34 +02001589 rate_bits = HDSPM_Frequency44_1KHz;
1590 break;
1591 case 48000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001592 rate_bits = HDSPM_Frequency48KHz;
1593 break;
1594 case 64000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001595 rate_bits = HDSPM_Frequency64KHz;
1596 break;
1597 case 88200:
Takashi Iwai763f3562005-06-03 11:25:34 +02001598 rate_bits = HDSPM_Frequency88_2KHz;
1599 break;
1600 case 96000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001601 rate_bits = HDSPM_Frequency96KHz;
1602 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001603 case 128000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001604 rate_bits = HDSPM_Frequency128KHz;
1605 break;
1606 case 176400:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001607 rate_bits = HDSPM_Frequency176_4KHz;
1608 break;
1609 case 192000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001610 rate_bits = HDSPM_Frequency192KHz;
1611 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001612 default:
1613 return -EINVAL;
1614 }
1615
Remy Bruno65345992007-08-31 12:21:08 +02001616 if (current_speed != target_speed
Takashi Iwai763f3562005-06-03 11:25:34 +02001617 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1618 snd_printk
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001619 (KERN_ERR "HDSPM: "
Remy Bruno65345992007-08-31 12:21:08 +02001620 "cannot change from %s speed to %s speed mode "
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001621 "(capture PID = %d, playback PID = %d)\n",
Remy Bruno65345992007-08-31 12:21:08 +02001622 hdspm_speed_names[current_speed],
1623 hdspm_speed_names[target_speed],
Takashi Iwai763f3562005-06-03 11:25:34 +02001624 hdspm->capture_pid, hdspm->playback_pid);
1625 return -EBUSY;
1626 }
1627
1628 hdspm->control_register &= ~HDSPM_FrequencyMask;
1629 hdspm->control_register |= rate_bits;
1630 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1631
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001632 /* For AES32, need to set DDS value in FREQ register
1633 For MADI, also apparently */
1634 hdspm_set_dds_value(hdspm, rate);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001635
1636 if (AES32 == hdspm->io_type && rate != current_rate)
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001637 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
Takashi Iwai763f3562005-06-03 11:25:34 +02001638
1639 hdspm->system_sample_rate = rate;
1640
Adrian Knoth0dca1792011-01-26 19:32:14 +01001641 if (rate <= 48000) {
1642 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1643 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1644 hdspm->max_channels_in = hdspm->ss_in_channels;
1645 hdspm->max_channels_out = hdspm->ss_out_channels;
1646 hdspm->port_names_in = hdspm->port_names_in_ss;
1647 hdspm->port_names_out = hdspm->port_names_out_ss;
1648 } else if (rate <= 96000) {
1649 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1650 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1651 hdspm->max_channels_in = hdspm->ds_in_channels;
1652 hdspm->max_channels_out = hdspm->ds_out_channels;
1653 hdspm->port_names_in = hdspm->port_names_in_ds;
1654 hdspm->port_names_out = hdspm->port_names_out_ds;
1655 } else {
1656 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1657 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1658 hdspm->max_channels_in = hdspm->qs_in_channels;
1659 hdspm->max_channels_out = hdspm->qs_out_channels;
1660 hdspm->port_names_in = hdspm->port_names_in_qs;
1661 hdspm->port_names_out = hdspm->port_names_out_qs;
1662 }
1663
Takashi Iwai763f3562005-06-03 11:25:34 +02001664 if (not_set != 0)
1665 return -1;
1666
1667 return 0;
1668}
1669
1670/* mainly for init to 0 on load */
Takashi Iwai98274f02005-11-17 14:52:34 +01001671static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
Takashi Iwai763f3562005-06-03 11:25:34 +02001672{
1673 int i, j;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001674 unsigned int gain;
1675
1676 if (sgain > UNITY_GAIN)
1677 gain = UNITY_GAIN;
1678 else if (sgain < 0)
1679 gain = 0;
1680 else
1681 gain = sgain;
Takashi Iwai763f3562005-06-03 11:25:34 +02001682
1683 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1684 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1685 hdspm_write_in_gain(hdspm, i, j, gain);
1686 hdspm_write_pb_gain(hdspm, i, j, gain);
1687 }
1688}
1689
1690/*----------------------------------------------------------------------------
1691 MIDI
1692 ----------------------------------------------------------------------------*/
1693
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001694static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1695 int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001696{
1697 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001698 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
Takashi Iwai763f3562005-06-03 11:25:34 +02001699}
1700
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001701static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1702 int val)
Takashi Iwai763f3562005-06-03 11:25:34 +02001703{
1704 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001705 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
Takashi Iwai763f3562005-06-03 11:25:34 +02001706}
1707
Takashi Iwai98274f02005-11-17 14:52:34 +01001708static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001709{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001710 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001711}
1712
Takashi Iwai98274f02005-11-17 14:52:34 +01001713static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001714{
1715 int fifo_bytes_used;
1716
Adrian Knoth0dca1792011-01-26 19:32:14 +01001717 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001718
1719 if (fifo_bytes_used < 128)
1720 return 128 - fifo_bytes_used;
1721 else
1722 return 0;
1723}
1724
Denys Vlasenko62cef822008-04-14 13:04:18 +02001725static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001726{
1727 while (snd_hdspm_midi_input_available (hdspm, id))
1728 snd_hdspm_midi_read_byte (hdspm, id);
1729}
1730
Takashi Iwai98274f02005-11-17 14:52:34 +01001731static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001732{
1733 unsigned long flags;
1734 int n_pending;
1735 int to_write;
1736 int i;
1737 unsigned char buf[128];
1738
1739 /* Output is not interrupt driven */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001740
Takashi Iwai763f3562005-06-03 11:25:34 +02001741 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001742 if (hmidi->output &&
1743 !snd_rawmidi_transmit_empty (hmidi->output)) {
1744 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1745 hmidi->id);
1746 if (n_pending > 0) {
1747 if (n_pending > (int)sizeof (buf))
1748 n_pending = sizeof (buf);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001749
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001750 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1751 n_pending);
1752 if (to_write > 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001753 for (i = 0; i < to_write; ++i)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001754 snd_hdspm_midi_write_byte (hmidi->hdspm,
1755 hmidi->id,
1756 buf[i]);
Takashi Iwai763f3562005-06-03 11:25:34 +02001757 }
1758 }
1759 }
1760 spin_unlock_irqrestore (&hmidi->lock, flags);
1761 return 0;
1762}
1763
Takashi Iwai98274f02005-11-17 14:52:34 +01001764static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001765{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001766 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1767 * input FIFO size
1768 */
Takashi Iwai763f3562005-06-03 11:25:34 +02001769 unsigned long flags;
1770 int n_pending;
1771 int i;
1772
1773 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001774 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1775 if (n_pending > 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001776 if (hmidi->input) {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001777 if (n_pending > (int)sizeof (buf))
Takashi Iwai763f3562005-06-03 11:25:34 +02001778 n_pending = sizeof (buf);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001779 for (i = 0; i < n_pending; ++i)
1780 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1781 hmidi->id);
1782 if (n_pending)
1783 snd_rawmidi_receive (hmidi->input, buf,
1784 n_pending);
Takashi Iwai763f3562005-06-03 11:25:34 +02001785 } else {
1786 /* flush the MIDI input FIFO */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001787 while (n_pending--)
1788 snd_hdspm_midi_read_byte (hmidi->hdspm,
1789 hmidi->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02001790 }
1791 }
1792 hmidi->pending = 0;
Adrian Knothc0da0012011-06-12 17:26:17 +02001793 spin_unlock_irqrestore(&hmidi->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001794
Adrian Knothc0da0012011-06-12 17:26:17 +02001795 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001796 hmidi->hdspm->control_register |= hmidi->ie;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001797 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1798 hmidi->hdspm->control_register);
Adrian Knothc0da0012011-06-12 17:26:17 +02001799 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001800
Takashi Iwai763f3562005-06-03 11:25:34 +02001801 return snd_hdspm_midi_output_write (hmidi);
1802}
1803
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001804static void
1805snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001806{
Takashi Iwai98274f02005-11-17 14:52:34 +01001807 struct hdspm *hdspm;
1808 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001809 unsigned long flags;
Takashi Iwai763f3562005-06-03 11:25:34 +02001810
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001811 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001812 hdspm = hmidi->hdspm;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001813
Takashi Iwai763f3562005-06-03 11:25:34 +02001814 spin_lock_irqsave (&hdspm->lock, flags);
1815 if (up) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001816 if (!(hdspm->control_register & hmidi->ie)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001817 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001818 hdspm->control_register |= hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001819 }
1820 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001821 hdspm->control_register &= ~hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001822 }
1823
1824 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1825 spin_unlock_irqrestore (&hdspm->lock, flags);
1826}
1827
1828static void snd_hdspm_midi_output_timer(unsigned long data)
1829{
Takashi Iwai98274f02005-11-17 14:52:34 +01001830 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001831 unsigned long flags;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001832
Takashi Iwai763f3562005-06-03 11:25:34 +02001833 snd_hdspm_midi_output_write(hmidi);
1834 spin_lock_irqsave (&hmidi->lock, flags);
1835
1836 /* this does not bump hmidi->istimer, because the
1837 kernel automatically removed the timer when it
1838 expired, and we are now adding it back, thus
Adrian Knoth0dca1792011-01-26 19:32:14 +01001839 leaving istimer wherever it was set before.
Takashi Iwai763f3562005-06-03 11:25:34 +02001840 */
1841
1842 if (hmidi->istimer) {
1843 hmidi->timer.expires = 1 + jiffies;
1844 add_timer(&hmidi->timer);
1845 }
1846
1847 spin_unlock_irqrestore (&hmidi->lock, flags);
1848}
1849
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001850static void
1851snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001852{
Takashi Iwai98274f02005-11-17 14:52:34 +01001853 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001854 unsigned long flags;
1855
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001856 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001857 spin_lock_irqsave (&hmidi->lock, flags);
1858 if (up) {
1859 if (!hmidi->istimer) {
1860 init_timer(&hmidi->timer);
1861 hmidi->timer.function = snd_hdspm_midi_output_timer;
1862 hmidi->timer.data = (unsigned long) hmidi;
1863 hmidi->timer.expires = 1 + jiffies;
1864 add_timer(&hmidi->timer);
1865 hmidi->istimer++;
1866 }
1867 } else {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001868 if (hmidi->istimer && --hmidi->istimer <= 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02001869 del_timer (&hmidi->timer);
Takashi Iwai763f3562005-06-03 11:25:34 +02001870 }
1871 spin_unlock_irqrestore (&hmidi->lock, flags);
1872 if (up)
1873 snd_hdspm_midi_output_write(hmidi);
1874}
1875
Takashi Iwai98274f02005-11-17 14:52:34 +01001876static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001877{
Takashi Iwai98274f02005-11-17 14:52:34 +01001878 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001879
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001880 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001881 spin_lock_irq (&hmidi->lock);
1882 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1883 hmidi->input = substream;
1884 spin_unlock_irq (&hmidi->lock);
1885
1886 return 0;
1887}
1888
Takashi Iwai98274f02005-11-17 14:52:34 +01001889static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001890{
Takashi Iwai98274f02005-11-17 14:52:34 +01001891 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001892
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001893 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001894 spin_lock_irq (&hmidi->lock);
1895 hmidi->output = substream;
1896 spin_unlock_irq (&hmidi->lock);
1897
1898 return 0;
1899}
1900
Takashi Iwai98274f02005-11-17 14:52:34 +01001901static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001902{
Takashi Iwai98274f02005-11-17 14:52:34 +01001903 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001904
1905 snd_hdspm_midi_input_trigger (substream, 0);
1906
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001907 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001908 spin_lock_irq (&hmidi->lock);
1909 hmidi->input = NULL;
1910 spin_unlock_irq (&hmidi->lock);
1911
1912 return 0;
1913}
1914
Takashi Iwai98274f02005-11-17 14:52:34 +01001915static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001916{
Takashi Iwai98274f02005-11-17 14:52:34 +01001917 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001918
1919 snd_hdspm_midi_output_trigger (substream, 0);
1920
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001921 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001922 spin_lock_irq (&hmidi->lock);
1923 hmidi->output = NULL;
1924 spin_unlock_irq (&hmidi->lock);
1925
1926 return 0;
1927}
1928
Takashi Iwai98274f02005-11-17 14:52:34 +01001929static struct snd_rawmidi_ops snd_hdspm_midi_output =
Takashi Iwai763f3562005-06-03 11:25:34 +02001930{
1931 .open = snd_hdspm_midi_output_open,
1932 .close = snd_hdspm_midi_output_close,
1933 .trigger = snd_hdspm_midi_output_trigger,
1934};
1935
Takashi Iwai98274f02005-11-17 14:52:34 +01001936static struct snd_rawmidi_ops snd_hdspm_midi_input =
Takashi Iwai763f3562005-06-03 11:25:34 +02001937{
1938 .open = snd_hdspm_midi_input_open,
1939 .close = snd_hdspm_midi_input_close,
1940 .trigger = snd_hdspm_midi_input_trigger,
1941};
1942
Bill Pembertone23e7a12012-12-06 12:35:10 -05001943static int snd_hdspm_create_midi(struct snd_card *card,
1944 struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001945{
1946 int err;
1947 char buf[32];
1948
1949 hdspm->midi[id].id = id;
Takashi Iwai763f3562005-06-03 11:25:34 +02001950 hdspm->midi[id].hdspm = hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +02001951 spin_lock_init (&hdspm->midi[id].lock);
1952
Adrian Knoth0dca1792011-01-26 19:32:14 +01001953 if (0 == id) {
1954 if (MADIface == hdspm->io_type) {
1955 /* MIDI-over-MADI on HDSPe MADIface */
1956 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1957 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1958 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1959 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1960 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1961 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1962 } else {
1963 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1964 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1965 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1966 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1967 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
1968 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
1969 }
1970 } else if (1 == id) {
1971 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
1972 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
1973 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
1974 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
1975 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
1976 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
1977 } else if ((2 == id) && (MADI == hdspm->io_type)) {
1978 /* MIDI-over-MADI on HDSPe MADI */
1979 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1980 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1981 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
1982 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
1983 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1984 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
1985 } else if (2 == id) {
1986 /* TCO MTC, read only */
1987 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1988 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1989 hdspm->midi[2].dataOut = -1;
1990 hdspm->midi[2].statusOut = -1;
1991 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1992 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
1993 } else if (3 == id) {
1994 /* TCO MTC on HDSPe MADI */
1995 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
1996 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
1997 hdspm->midi[3].dataOut = -1;
1998 hdspm->midi[3].statusOut = -1;
1999 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
2000 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
2001 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002002
Adrian Knoth0dca1792011-01-26 19:32:14 +01002003 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
2004 (MADIface == hdspm->io_type)))) {
2005 if ((id == 0) && (MADIface == hdspm->io_type)) {
2006 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2007 } else if ((id == 2) && (MADI == hdspm->io_type)) {
2008 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2009 } else {
2010 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
2011 }
2012 err = snd_rawmidi_new(card, buf, id, 1, 1,
2013 &hdspm->midi[id].rmidi);
2014 if (err < 0)
2015 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02002016
Adrian Knoth0dca1792011-01-26 19:32:14 +01002017 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
2018 card->id, id+1);
2019 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
Takashi Iwai763f3562005-06-03 11:25:34 +02002020
Adrian Knoth0dca1792011-01-26 19:32:14 +01002021 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2022 SNDRV_RAWMIDI_STREAM_OUTPUT,
2023 &snd_hdspm_midi_output);
2024 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2025 SNDRV_RAWMIDI_STREAM_INPUT,
2026 &snd_hdspm_midi_input);
2027
2028 hdspm->midi[id].rmidi->info_flags |=
2029 SNDRV_RAWMIDI_INFO_OUTPUT |
2030 SNDRV_RAWMIDI_INFO_INPUT |
2031 SNDRV_RAWMIDI_INFO_DUPLEX;
2032 } else {
2033 /* TCO MTC, read only */
2034 sprintf(buf, "%s MTC %d", card->shortname, id+1);
2035 err = snd_rawmidi_new(card, buf, id, 1, 1,
2036 &hdspm->midi[id].rmidi);
2037 if (err < 0)
2038 return err;
2039
2040 sprintf(hdspm->midi[id].rmidi->name,
2041 "%s MTC %d", card->id, id+1);
2042 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
2043
2044 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2045 SNDRV_RAWMIDI_STREAM_INPUT,
2046 &snd_hdspm_midi_input);
2047
2048 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
2049 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002050
2051 return 0;
2052}
2053
2054
2055static void hdspm_midi_tasklet(unsigned long arg)
2056{
Takashi Iwai98274f02005-11-17 14:52:34 +01002057 struct hdspm *hdspm = (struct hdspm *)arg;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002058 int i = 0;
2059
2060 while (i < hdspm->midiPorts) {
2061 if (hdspm->midi[i].pending)
2062 snd_hdspm_midi_input_read(&hdspm->midi[i]);
2063
2064 i++;
2065 }
2066}
Takashi Iwai763f3562005-06-03 11:25:34 +02002067
2068
2069/*-----------------------------------------------------------------------------
2070 Status Interface
2071 ----------------------------------------------------------------------------*/
2072
2073/* get the system sample rate which is set */
2074
Adrian Knoth0dca1792011-01-26 19:32:14 +01002075
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002076static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
2077{
2078 unsigned int period, rate;
2079
2080 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
2081 rate = hdspm_calc_dds_value(hdspm, period);
2082
2083 return rate;
2084}
2085
Adrian Knoth0dca1792011-01-26 19:32:14 +01002086/**
2087 * Calculate the real sample rate from the
2088 * current DDS value.
2089 **/
2090static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
2091{
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002092 unsigned int rate;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002093
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002094 rate = hdspm_get_pll_freq(hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002095
Adrian Knotha97bda72012-05-30 14:23:18 +02002096 if (rate > 207000) {
Adrian Knoth21a164d2012-10-19 17:42:23 +02002097 /* Unreasonable high sample rate as seen on PCI MADI cards. */
2098 if (0 == hdspm_system_clock_mode(hdspm)) {
2099 /* master mode, return internal sample rate */
2100 rate = hdspm->system_sample_rate;
2101 } else {
2102 /* slave mode, return external sample rate */
2103 rate = hdspm_external_sample_rate(hdspm);
2104 }
Adrian Knotha97bda72012-05-30 14:23:18 +02002105 }
2106
Adrian Knoth0dca1792011-01-26 19:32:14 +01002107 return rate;
2108}
2109
2110
Takashi Iwai763f3562005-06-03 11:25:34 +02002111#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002112{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2113 .name = xname, \
2114 .index = xindex, \
2115 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2116 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2117 .info = snd_hdspm_info_system_sample_rate, \
2118 .put = snd_hdspm_put_system_sample_rate, \
2119 .get = snd_hdspm_get_system_sample_rate \
Takashi Iwai763f3562005-06-03 11:25:34 +02002120}
2121
Takashi Iwai98274f02005-11-17 14:52:34 +01002122static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002124{
2125 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2126 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002127 uinfo->value.integer.min = 27000;
2128 uinfo->value.integer.max = 207000;
2129 uinfo->value.integer.step = 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002130 return 0;
2131}
2132
Adrian Knoth0dca1792011-01-26 19:32:14 +01002133
Takashi Iwai98274f02005-11-17 14:52:34 +01002134static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002136 ucontrol)
2137{
Takashi Iwai98274f02005-11-17 14:52:34 +01002138 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002139
Adrian Knoth0dca1792011-01-26 19:32:14 +01002140 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002141 return 0;
2142}
2143
Adrian Knoth41285a92012-10-19 17:42:22 +02002144static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *
2146 ucontrol)
2147{
2148 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2149
2150 hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
2151 return 0;
2152}
2153
Adrian Knoth0dca1792011-01-26 19:32:14 +01002154
2155/**
2156 * Returns the WordClock sample rate class for the given card.
2157 **/
2158static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
2159{
2160 int status;
2161
2162 switch (hdspm->io_type) {
2163 case RayDAT:
2164 case AIO:
2165 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2166 return (status >> 16) & 0xF;
2167 break;
2168 default:
2169 break;
2170 }
2171
2172
2173 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002174}
2175
Adrian Knoth0dca1792011-01-26 19:32:14 +01002176
2177/**
2178 * Returns the TCO sample rate class for the given card.
2179 **/
2180static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2181{
2182 int status;
2183
2184 if (hdspm->tco) {
2185 switch (hdspm->io_type) {
2186 case RayDAT:
2187 case AIO:
2188 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2189 return (status >> 20) & 0xF;
2190 break;
2191 default:
2192 break;
2193 }
2194 }
2195
2196 return 0;
2197}
2198
2199
2200/**
2201 * Returns the SYNC_IN sample rate class for the given card.
2202 **/
2203static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2204{
2205 int status;
2206
2207 if (hdspm->tco) {
2208 switch (hdspm->io_type) {
2209 case RayDAT:
2210 case AIO:
2211 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2212 return (status >> 12) & 0xF;
2213 break;
2214 default:
2215 break;
2216 }
2217 }
2218
2219 return 0;
2220}
2221
2222
2223/**
2224 * Returns the sample rate class for input source <idx> for
2225 * 'new style' cards like the AIO and RayDAT.
2226 **/
2227static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2228{
2229 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2230
2231 return (status >> (idx*4)) & 0xF;
2232}
2233
Adrian Knoth8cea5712013-07-05 11:27:59 +02002234static void snd_hdspm_set_infotext(struct snd_ctl_elem_info *uinfo,
2235 char **texts, const int count)
2236{
2237 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2238 uinfo->count = 1;
2239 uinfo->value.enumerated.items = count;
2240 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2241 uinfo->value.enumerated.item =
2242 uinfo->value.enumerated.items - 1;
2243 strcpy(uinfo->value.enumerated.name,
2244 texts[uinfo->value.enumerated.item]);
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002245}
2246
Adrian Knoth8cea5712013-07-05 11:27:59 +02002247#define ENUMERATED_CTL_INFO(info, texts) \
2248 snd_hdspm_set_infotext(info, texts, ARRAY_SIZE(texts))
2249
Adrian Knoth0dca1792011-01-26 19:32:14 +01002250
2251
2252#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2253{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2254 .name = xname, \
2255 .private_value = xindex, \
2256 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2257 .info = snd_hdspm_info_autosync_sample_rate, \
2258 .get = snd_hdspm_get_autosync_sample_rate \
2259}
2260
2261
Takashi Iwai98274f02005-11-17 14:52:34 +01002262static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002264{
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002265 ENUMERATED_CTL_INFO(uinfo, texts_freq);
Takashi Iwai763f3562005-06-03 11:25:34 +02002266 return 0;
2267}
2268
Adrian Knoth0dca1792011-01-26 19:32:14 +01002269
Takashi Iwai98274f02005-11-17 14:52:34 +01002270static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002272 ucontrol)
2273{
Takashi Iwai98274f02005-11-17 14:52:34 +01002274 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002275
Adrian Knoth0dca1792011-01-26 19:32:14 +01002276 switch (hdspm->io_type) {
2277 case RayDAT:
2278 switch (kcontrol->private_value) {
2279 case 0:
2280 ucontrol->value.enumerated.item[0] =
2281 hdspm_get_wc_sample_rate(hdspm);
2282 break;
2283 case 7:
2284 ucontrol->value.enumerated.item[0] =
2285 hdspm_get_tco_sample_rate(hdspm);
2286 break;
2287 case 8:
2288 ucontrol->value.enumerated.item[0] =
2289 hdspm_get_sync_in_sample_rate(hdspm);
2290 break;
2291 default:
2292 ucontrol->value.enumerated.item[0] =
2293 hdspm_get_s1_sample_rate(hdspm,
2294 kcontrol->private_value-1);
2295 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002296 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002297
Adrian Knoth0dca1792011-01-26 19:32:14 +01002298 case AIO:
2299 switch (kcontrol->private_value) {
2300 case 0: /* WC */
2301 ucontrol->value.enumerated.item[0] =
2302 hdspm_get_wc_sample_rate(hdspm);
2303 break;
2304 case 4: /* TCO */
2305 ucontrol->value.enumerated.item[0] =
2306 hdspm_get_tco_sample_rate(hdspm);
2307 break;
2308 case 5: /* SYNC_IN */
2309 ucontrol->value.enumerated.item[0] =
2310 hdspm_get_sync_in_sample_rate(hdspm);
2311 break;
2312 default:
2313 ucontrol->value.enumerated.item[0] =
2314 hdspm_get_s1_sample_rate(hdspm,
2315 ucontrol->id.index-1);
2316 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002317 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002318
2319 case AES32:
2320
2321 switch (kcontrol->private_value) {
2322 case 0: /* WC */
2323 ucontrol->value.enumerated.item[0] =
2324 hdspm_get_wc_sample_rate(hdspm);
2325 break;
2326 case 9: /* TCO */
2327 ucontrol->value.enumerated.item[0] =
2328 hdspm_get_tco_sample_rate(hdspm);
2329 break;
2330 case 10: /* SYNC_IN */
2331 ucontrol->value.enumerated.item[0] =
2332 hdspm_get_sync_in_sample_rate(hdspm);
2333 break;
2334 default: /* AES1 to AES8 */
2335 ucontrol->value.enumerated.item[0] =
2336 hdspm_get_s1_sample_rate(hdspm,
2337 kcontrol->private_value-1);
2338 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002339 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002340 break;
Adrian Knothb8812c52012-10-19 17:42:26 +02002341
2342 case MADI:
2343 case MADIface:
2344 {
2345 int rate = hdspm_external_sample_rate(hdspm);
2346 int i, selected_rate = 0;
2347 for (i = 1; i < 10; i++)
2348 if (HDSPM_bit2freq(i) == rate) {
2349 selected_rate = i;
2350 break;
2351 }
2352 ucontrol->value.enumerated.item[0] = selected_rate;
2353 }
2354 break;
2355
Takashi Iwai763f3562005-06-03 11:25:34 +02002356 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002357 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002358 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002359
Takashi Iwai763f3562005-06-03 11:25:34 +02002360 return 0;
2361}
2362
Adrian Knoth0dca1792011-01-26 19:32:14 +01002363
Takashi Iwai763f3562005-06-03 11:25:34 +02002364#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002365{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2366 .name = xname, \
2367 .index = xindex, \
2368 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2369 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2370 .info = snd_hdspm_info_system_clock_mode, \
2371 .get = snd_hdspm_get_system_clock_mode, \
2372 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002373}
2374
2375
Adrian Knoth0dca1792011-01-26 19:32:14 +01002376/**
2377 * Returns the system clock mode for the given card.
2378 * @returns 0 - master, 1 - slave
2379 **/
2380static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002381{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002382 switch (hdspm->io_type) {
2383 case AIO:
2384 case RayDAT:
2385 if (hdspm->settings_register & HDSPM_c0Master)
2386 return 0;
2387 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002388
Adrian Knoth0dca1792011-01-26 19:32:14 +01002389 default:
2390 if (hdspm->control_register & HDSPM_ClockModeMaster)
2391 return 0;
2392 }
2393
Takashi Iwai763f3562005-06-03 11:25:34 +02002394 return 1;
2395}
2396
Adrian Knoth0dca1792011-01-26 19:32:14 +01002397
2398/**
2399 * Sets the system clock mode.
2400 * @param mode 0 - master, 1 - slave
2401 **/
2402static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2403{
Adrian Knoth34be7eb2013-07-05 11:27:56 +02002404 hdspm_set_toggle_setting(hdspm,
2405 (hdspm_is_raydat_or_aio(hdspm)) ?
2406 HDSPM_c0Master : HDSPM_ClockModeMaster,
2407 (0 == mode));
Adrian Knoth0dca1792011-01-26 19:32:14 +01002408}
2409
2410
Takashi Iwai98274f02005-11-17 14:52:34 +01002411static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002413{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002414 static char *texts[] = { "Master", "AutoSync" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002415 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02002416 return 0;
2417}
2418
Takashi Iwai98274f02005-11-17 14:52:34 +01002419static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002421{
Takashi Iwai98274f02005-11-17 14:52:34 +01002422 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002423
Adrian Knoth0dca1792011-01-26 19:32:14 +01002424 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002425 return 0;
2426}
2427
Adrian Knoth0dca1792011-01-26 19:32:14 +01002428static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2432 int val;
2433
2434 if (!snd_hdspm_use_is_exclusive(hdspm))
2435 return -EBUSY;
2436
2437 val = ucontrol->value.enumerated.item[0];
2438 if (val < 0)
2439 val = 0;
2440 else if (val > 1)
2441 val = 1;
2442
2443 hdspm_set_system_clock_mode(hdspm, val);
2444
2445 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002446}
2447
Adrian Knoth0dca1792011-01-26 19:32:14 +01002448
2449#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2450{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2451 .name = xname, \
2452 .index = xindex, \
2453 .info = snd_hdspm_info_clock_source, \
2454 .get = snd_hdspm_get_clock_source, \
2455 .put = snd_hdspm_put_clock_source \
2456}
2457
2458
Takashi Iwai98274f02005-11-17 14:52:34 +01002459static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002460{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002461 switch (hdspm->system_sample_rate) {
2462 case 32000: return 0;
2463 case 44100: return 1;
2464 case 48000: return 2;
2465 case 64000: return 3;
2466 case 88200: return 4;
2467 case 96000: return 5;
2468 case 128000: return 6;
2469 case 176400: return 7;
2470 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002471 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002472
2473 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002474}
2475
Takashi Iwai98274f02005-11-17 14:52:34 +01002476static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002477{
2478 int rate;
2479 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002480 case 0:
2481 rate = 32000; break;
2482 case 1:
2483 rate = 44100; break;
2484 case 2:
2485 rate = 48000; break;
2486 case 3:
2487 rate = 64000; break;
2488 case 4:
2489 rate = 88200; break;
2490 case 5:
2491 rate = 96000; break;
2492 case 6:
2493 rate = 128000; break;
2494 case 7:
2495 rate = 176400; break;
2496 case 8:
2497 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002498 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002499 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002500 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002501 hdspm_set_rate(hdspm, rate, 1);
2502 return 0;
2503}
2504
Takashi Iwai98274f02005-11-17 14:52:34 +01002505static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2506 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002507{
Takashi Iwai763f3562005-06-03 11:25:34 +02002508 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2509 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002510 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002511
2512 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2513 uinfo->value.enumerated.item =
2514 uinfo->value.enumerated.items - 1;
2515
2516 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002517 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002518
2519 return 0;
2520}
2521
Takashi Iwai98274f02005-11-17 14:52:34 +01002522static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002524{
Takashi Iwai98274f02005-11-17 14:52:34 +01002525 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002526
2527 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2528 return 0;
2529}
2530
Takashi Iwai98274f02005-11-17 14:52:34 +01002531static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002533{
Takashi Iwai98274f02005-11-17 14:52:34 +01002534 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002535 int change;
2536 int val;
2537
2538 if (!snd_hdspm_use_is_exclusive(hdspm))
2539 return -EBUSY;
2540 val = ucontrol->value.enumerated.item[0];
2541 if (val < 0)
2542 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002543 if (val > 9)
2544 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002545 spin_lock_irq(&hdspm->lock);
2546 if (val != hdspm_clock_source(hdspm))
2547 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2548 else
2549 change = 0;
2550 spin_unlock_irq(&hdspm->lock);
2551 return change;
2552}
2553
Adrian Knoth0dca1792011-01-26 19:32:14 +01002554
Takashi Iwai763f3562005-06-03 11:25:34 +02002555#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002556{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002557 .name = xname, \
2558 .index = xindex, \
2559 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2560 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2561 .info = snd_hdspm_info_pref_sync_ref, \
2562 .get = snd_hdspm_get_pref_sync_ref, \
2563 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002564}
2565
Adrian Knoth0dca1792011-01-26 19:32:14 +01002566
2567/**
2568 * Returns the current preferred sync reference setting.
2569 * The semantics of the return value are depending on the
2570 * card, please see the comments for clarification.
2571 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002572static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002573{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002574 switch (hdspm->io_type) {
2575 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002576 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002577 case 0: return 0; /* WC */
2578 case HDSPM_SyncRef0: return 1; /* AES 1 */
2579 case HDSPM_SyncRef1: return 2; /* AES 2 */
2580 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2581 case HDSPM_SyncRef2: return 4; /* AES 4 */
2582 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2583 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2584 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2585 return 7; /* AES 7 */
2586 case HDSPM_SyncRef3: return 8; /* AES 8 */
2587 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002588 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002589 break;
2590
2591 case MADI:
2592 case MADIface:
2593 if (hdspm->tco) {
2594 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2595 case 0: return 0; /* WC */
2596 case HDSPM_SyncRef0: return 1; /* MADI */
2597 case HDSPM_SyncRef1: return 2; /* TCO */
2598 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2599 return 3; /* SYNC_IN */
2600 }
2601 } else {
2602 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2603 case 0: return 0; /* WC */
2604 case HDSPM_SyncRef0: return 1; /* MADI */
2605 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2606 return 2; /* SYNC_IN */
2607 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002608 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002609 break;
2610
2611 case RayDAT:
2612 if (hdspm->tco) {
2613 switch ((hdspm->settings_register &
2614 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2615 case 0: return 0; /* WC */
2616 case 3: return 1; /* ADAT 1 */
2617 case 4: return 2; /* ADAT 2 */
2618 case 5: return 3; /* ADAT 3 */
2619 case 6: return 4; /* ADAT 4 */
2620 case 1: return 5; /* AES */
2621 case 2: return 6; /* SPDIF */
2622 case 9: return 7; /* TCO */
2623 case 10: return 8; /* SYNC_IN */
2624 }
2625 } else {
2626 switch ((hdspm->settings_register &
2627 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2628 case 0: return 0; /* WC */
2629 case 3: return 1; /* ADAT 1 */
2630 case 4: return 2; /* ADAT 2 */
2631 case 5: return 3; /* ADAT 3 */
2632 case 6: return 4; /* ADAT 4 */
2633 case 1: return 5; /* AES */
2634 case 2: return 6; /* SPDIF */
2635 case 10: return 7; /* SYNC_IN */
2636 }
2637 }
2638
2639 break;
2640
2641 case AIO:
2642 if (hdspm->tco) {
2643 switch ((hdspm->settings_register &
2644 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2645 case 0: return 0; /* WC */
2646 case 3: return 1; /* ADAT */
2647 case 1: return 2; /* AES */
2648 case 2: return 3; /* SPDIF */
2649 case 9: return 4; /* TCO */
2650 case 10: return 5; /* SYNC_IN */
2651 }
2652 } else {
2653 switch ((hdspm->settings_register &
2654 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2655 case 0: return 0; /* WC */
2656 case 3: return 1; /* ADAT */
2657 case 1: return 2; /* AES */
2658 case 2: return 3; /* SPDIF */
2659 case 10: return 4; /* SYNC_IN */
2660 }
2661 }
2662
2663 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002664 }
2665
Adrian Knoth0dca1792011-01-26 19:32:14 +01002666 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002667}
2668
Adrian Knoth0dca1792011-01-26 19:32:14 +01002669
2670/**
2671 * Set the preferred sync reference to <pref>. The semantics
2672 * of <pref> are depending on the card type, see the comments
2673 * for clarification.
2674 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002675static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002676{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002677 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002678
Adrian Knoth0dca1792011-01-26 19:32:14 +01002679 switch (hdspm->io_type) {
2680 case AES32:
2681 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002682 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002683 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002684 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002685 case 1: /* AES 1 */
2686 hdspm->control_register |= HDSPM_SyncRef0;
2687 break;
2688 case 2: /* AES 2 */
2689 hdspm->control_register |= HDSPM_SyncRef1;
2690 break;
2691 case 3: /* AES 3 */
2692 hdspm->control_register |=
2693 HDSPM_SyncRef1+HDSPM_SyncRef0;
2694 break;
2695 case 4: /* AES 4 */
2696 hdspm->control_register |= HDSPM_SyncRef2;
2697 break;
2698 case 5: /* AES 5 */
2699 hdspm->control_register |=
2700 HDSPM_SyncRef2+HDSPM_SyncRef0;
2701 break;
2702 case 6: /* AES 6 */
2703 hdspm->control_register |=
2704 HDSPM_SyncRef2+HDSPM_SyncRef1;
2705 break;
2706 case 7: /* AES 7 */
2707 hdspm->control_register |=
2708 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2709 break;
2710 case 8: /* AES 8 */
2711 hdspm->control_register |= HDSPM_SyncRef3;
2712 break;
2713 case 9: /* TCO */
2714 hdspm->control_register |=
2715 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002716 break;
2717 default:
2718 return -1;
2719 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002720
2721 break;
2722
2723 case MADI:
2724 case MADIface:
2725 hdspm->control_register &= ~HDSPM_SyncRefMask;
2726 if (hdspm->tco) {
2727 switch (pref) {
2728 case 0: /* WC */
2729 break;
2730 case 1: /* MADI */
2731 hdspm->control_register |= HDSPM_SyncRef0;
2732 break;
2733 case 2: /* TCO */
2734 hdspm->control_register |= HDSPM_SyncRef1;
2735 break;
2736 case 3: /* SYNC_IN */
2737 hdspm->control_register |=
2738 HDSPM_SyncRef0+HDSPM_SyncRef1;
2739 break;
2740 default:
2741 return -1;
2742 }
2743 } else {
2744 switch (pref) {
2745 case 0: /* WC */
2746 break;
2747 case 1: /* MADI */
2748 hdspm->control_register |= HDSPM_SyncRef0;
2749 break;
2750 case 2: /* SYNC_IN */
2751 hdspm->control_register |=
2752 HDSPM_SyncRef0+HDSPM_SyncRef1;
2753 break;
2754 default:
2755 return -1;
2756 }
2757 }
2758
2759 break;
2760
2761 case RayDAT:
2762 if (hdspm->tco) {
2763 switch (pref) {
2764 case 0: p = 0; break; /* WC */
2765 case 1: p = 3; break; /* ADAT 1 */
2766 case 2: p = 4; break; /* ADAT 2 */
2767 case 3: p = 5; break; /* ADAT 3 */
2768 case 4: p = 6; break; /* ADAT 4 */
2769 case 5: p = 1; break; /* AES */
2770 case 6: p = 2; break; /* SPDIF */
2771 case 7: p = 9; break; /* TCO */
2772 case 8: p = 10; break; /* SYNC_IN */
2773 default: return -1;
2774 }
2775 } else {
2776 switch (pref) {
2777 case 0: p = 0; break; /* WC */
2778 case 1: p = 3; break; /* ADAT 1 */
2779 case 2: p = 4; break; /* ADAT 2 */
2780 case 3: p = 5; break; /* ADAT 3 */
2781 case 4: p = 6; break; /* ADAT 4 */
2782 case 5: p = 1; break; /* AES */
2783 case 6: p = 2; break; /* SPDIF */
2784 case 7: p = 10; break; /* SYNC_IN */
2785 default: return -1;
2786 }
2787 }
2788 break;
2789
2790 case AIO:
2791 if (hdspm->tco) {
2792 switch (pref) {
2793 case 0: p = 0; break; /* WC */
2794 case 1: p = 3; break; /* ADAT */
2795 case 2: p = 1; break; /* AES */
2796 case 3: p = 2; break; /* SPDIF */
2797 case 4: p = 9; break; /* TCO */
2798 case 5: p = 10; break; /* SYNC_IN */
2799 default: return -1;
2800 }
2801 } else {
2802 switch (pref) {
2803 case 0: p = 0; break; /* WC */
2804 case 1: p = 3; break; /* ADAT */
2805 case 2: p = 1; break; /* AES */
2806 case 3: p = 2; break; /* SPDIF */
2807 case 4: p = 10; break; /* SYNC_IN */
2808 default: return -1;
2809 }
2810 }
2811 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002812 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002813
2814 switch (hdspm->io_type) {
2815 case RayDAT:
2816 case AIO:
2817 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2818 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2819 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2820 break;
2821
2822 case MADI:
2823 case MADIface:
2824 case AES32:
2825 hdspm_write(hdspm, HDSPM_controlRegister,
2826 hdspm->control_register);
2827 }
2828
Takashi Iwai763f3562005-06-03 11:25:34 +02002829 return 0;
2830}
2831
Adrian Knoth0dca1792011-01-26 19:32:14 +01002832
Takashi Iwai98274f02005-11-17 14:52:34 +01002833static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002835{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002836 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002837
Adrian Knoth0dca1792011-01-26 19:32:14 +01002838 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2839 uinfo->count = 1;
2840 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002841
Adrian Knoth0dca1792011-01-26 19:32:14 +01002842 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2843 uinfo->value.enumerated.item =
2844 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002845
Adrian Knoth0dca1792011-01-26 19:32:14 +01002846 strcpy(uinfo->value.enumerated.name,
2847 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002848
Takashi Iwai763f3562005-06-03 11:25:34 +02002849 return 0;
2850}
2851
Takashi Iwai98274f02005-11-17 14:52:34 +01002852static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002854{
Takashi Iwai98274f02005-11-17 14:52:34 +01002855 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002856 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002857
Adrian Knoth0dca1792011-01-26 19:32:14 +01002858 if (psf >= 0) {
2859 ucontrol->value.enumerated.item[0] = psf;
2860 return 0;
2861 }
2862
2863 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002864}
2865
Takashi Iwai98274f02005-11-17 14:52:34 +01002866static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002868{
Takashi Iwai98274f02005-11-17 14:52:34 +01002869 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002870 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002871
2872 if (!snd_hdspm_use_is_exclusive(hdspm))
2873 return -EBUSY;
2874
Adrian Knoth0dca1792011-01-26 19:32:14 +01002875 val = ucontrol->value.enumerated.item[0];
2876
2877 if (val < 0)
2878 val = 0;
2879 else if (val >= hdspm->texts_autosync_items)
2880 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002881
2882 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002883 if (val != hdspm_pref_sync_ref(hdspm))
2884 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2885
Takashi Iwai763f3562005-06-03 11:25:34 +02002886 spin_unlock_irq(&hdspm->lock);
2887 return change;
2888}
2889
Adrian Knoth0dca1792011-01-26 19:32:14 +01002890
Takashi Iwai763f3562005-06-03 11:25:34 +02002891#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002892{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2893 .name = xname, \
2894 .index = xindex, \
2895 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2896 .info = snd_hdspm_info_autosync_ref, \
2897 .get = snd_hdspm_get_autosync_ref, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002898}
2899
Adrian Knoth0dca1792011-01-26 19:32:14 +01002900static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002901{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002902 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002903 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002904 unsigned int syncref =
2905 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002906 if (syncref == 0)
2907 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2908 if (syncref <= 8)
2909 return syncref;
2910 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002911 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002912 /* This looks at the autosync selected sync reference */
2913 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002914
Remy Bruno3cee5a62006-10-16 12:46:32 +02002915 switch (status2 & HDSPM_SelSyncRefMask) {
2916 case HDSPM_SelSyncRef_WORD:
2917 return HDSPM_AUTOSYNC_FROM_WORD;
2918 case HDSPM_SelSyncRef_MADI:
2919 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002920 case HDSPM_SelSyncRef_TCO:
2921 return HDSPM_AUTOSYNC_FROM_TCO;
2922 case HDSPM_SelSyncRef_SyncIn:
2923 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002924 case HDSPM_SelSyncRef_NVALID:
2925 return HDSPM_AUTOSYNC_FROM_NONE;
2926 default:
2927 return 0;
2928 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002929
Takashi Iwai763f3562005-06-03 11:25:34 +02002930 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002931 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002932}
2933
Adrian Knoth0dca1792011-01-26 19:32:14 +01002934
Takashi Iwai98274f02005-11-17 14:52:34 +01002935static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2936 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002937{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002938 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002939
Adrian Knoth0dca1792011-01-26 19:32:14 +01002940 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002941 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
2942 "AES4", "AES5", "AES6", "AES7", "AES8", "None"};
2943
2944 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2945 uinfo->count = 1;
2946 uinfo->value.enumerated.items = 10;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002947 if (uinfo->value.enumerated.item >=
2948 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002949 uinfo->value.enumerated.item =
2950 uinfo->value.enumerated.items - 1;
2951 strcpy(uinfo->value.enumerated.name,
2952 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002953 } else if (MADI == hdspm->io_type) {
2954 static char *texts[] = {"Word Clock", "MADI", "TCO",
2955 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002956
2957 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2958 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002959 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002960 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002961 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002962 uinfo->value.enumerated.item =
2963 uinfo->value.enumerated.items - 1;
2964 strcpy(uinfo->value.enumerated.name,
2965 texts[uinfo->value.enumerated.item]);
2966 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002967 return 0;
2968}
2969
Takashi Iwai98274f02005-11-17 14:52:34 +01002970static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
2971 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002972{
Takashi Iwai98274f02005-11-17 14:52:34 +01002973 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002974
Remy Bruno65345992007-08-31 12:21:08 +02002975 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002976 return 0;
2977}
2978
Adrian Knothf99c7882013-03-10 00:37:26 +01002979
2980
2981#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
2982{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2983 .name = xname, \
2984 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
2985 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2986 .info = snd_hdspm_info_tco_video_input_format, \
2987 .get = snd_hdspm_get_tco_video_input_format, \
2988}
2989
2990static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_info *uinfo)
2992{
2993 static char *texts[] = {"No video", "NTSC", "PAL"};
2994 ENUMERATED_CTL_INFO(uinfo, texts);
2995 return 0;
2996}
2997
2998static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
2999 struct snd_ctl_elem_value *ucontrol)
3000{
3001 u32 status;
3002 int ret = 0;
3003
3004 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3005 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3006 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3007 HDSPM_TCO1_Video_Input_Format_PAL)) {
3008 case HDSPM_TCO1_Video_Input_Format_NTSC:
3009 /* ntsc */
3010 ret = 1;
3011 break;
3012 case HDSPM_TCO1_Video_Input_Format_PAL:
3013 /* pal */
3014 ret = 2;
3015 break;
3016 default:
3017 /* no video */
3018 ret = 0;
3019 break;
3020 }
3021 ucontrol->value.enumerated.item[0] = ret;
3022 return 0;
3023}
3024
3025
3026
3027#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3028{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3029 .name = xname, \
3030 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3031 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3032 .info = snd_hdspm_info_tco_ltc_frames, \
3033 .get = snd_hdspm_get_tco_ltc_frames, \
3034}
3035
3036static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3037 struct snd_ctl_elem_info *uinfo)
3038{
3039 static char *texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3040 "30 fps"};
3041 ENUMERATED_CTL_INFO(uinfo, texts);
3042 return 0;
3043}
3044
3045static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3046{
3047 u32 status;
3048 int ret = 0;
3049
3050 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3051 if (status & HDSPM_TCO1_LTC_Input_valid) {
3052 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3053 HDSPM_TCO1_LTC_Format_MSB)) {
3054 case 0:
3055 /* 24 fps */
3056 ret = 1;
3057 break;
3058 case HDSPM_TCO1_LTC_Format_LSB:
3059 /* 25 fps */
3060 ret = 2;
3061 break;
3062 case HDSPM_TCO1_LTC_Format_MSB:
3063 /* 25 fps */
3064 ret = 3;
3065 break;
3066 default:
3067 /* 30 fps */
3068 ret = 4;
3069 break;
3070 }
3071 }
3072
3073 return ret;
3074}
3075
3076static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3077 struct snd_ctl_elem_value *ucontrol)
3078{
3079 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3080
3081 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3082 return 0;
3083}
3084
Adrian Knothbf0ff872012-12-03 14:55:49 +01003085#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3086{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3087 .name = xname, \
3088 .private_value = xindex, \
3089 .info = snd_hdspm_info_toggle_setting, \
3090 .get = snd_hdspm_get_toggle_setting, \
3091 .put = snd_hdspm_put_toggle_setting \
3092}
3093
3094static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3095{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003096 u32 reg;
3097
3098 if (hdspm_is_raydat_or_aio(hdspm))
3099 reg = hdspm->settings_register;
3100 else
3101 reg = hdspm->control_register;
3102
3103 return (reg & regmask) ? 1 : 0;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003104}
3105
3106static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3107{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003108 u32 *reg;
3109 u32 target_reg;
3110
3111 if (hdspm_is_raydat_or_aio(hdspm)) {
3112 reg = &(hdspm->settings_register);
3113 target_reg = HDSPM_WR_SETTINGS;
3114 } else {
3115 reg = &(hdspm->control_register);
3116 target_reg = HDSPM_controlRegister;
3117 }
3118
Adrian Knothbf0ff872012-12-03 14:55:49 +01003119 if (out)
Adrian Knothce13f3f2013-07-05 11:27:55 +02003120 *reg |= regmask;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003121 else
Adrian Knothce13f3f2013-07-05 11:27:55 +02003122 *reg &= ~regmask;
3123
3124 hdspm_write(hdspm, target_reg, *reg);
Adrian Knothbf0ff872012-12-03 14:55:49 +01003125
3126 return 0;
3127}
3128
3129#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3130
3131static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_value *ucontrol)
3133{
3134 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3135 u32 regmask = kcontrol->private_value;
3136
3137 spin_lock_irq(&hdspm->lock);
3138 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3139 spin_unlock_irq(&hdspm->lock);
3140 return 0;
3141}
3142
3143static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3144 struct snd_ctl_elem_value *ucontrol)
3145{
3146 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3147 u32 regmask = kcontrol->private_value;
3148 int change;
3149 unsigned int val;
3150
3151 if (!snd_hdspm_use_is_exclusive(hdspm))
3152 return -EBUSY;
3153 val = ucontrol->value.integer.value[0] & 1;
3154 spin_lock_irq(&hdspm->lock);
3155 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3156 hdspm_set_toggle_setting(hdspm, regmask, val);
3157 spin_unlock_irq(&hdspm->lock);
3158 return change;
3159}
3160
Takashi Iwai763f3562005-06-03 11:25:34 +02003161#define HDSPM_INPUT_SELECT(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003162{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3163 .name = xname, \
3164 .index = xindex, \
3165 .info = snd_hdspm_info_input_select, \
3166 .get = snd_hdspm_get_input_select, \
3167 .put = snd_hdspm_put_input_select \
Takashi Iwai763f3562005-06-03 11:25:34 +02003168}
3169
Takashi Iwai98274f02005-11-17 14:52:34 +01003170static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003171{
3172 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3173}
3174
Takashi Iwai98274f02005-11-17 14:52:34 +01003175static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003176{
3177 if (out)
3178 hdspm->control_register |= HDSPM_InputSelect0;
3179 else
3180 hdspm->control_register &= ~HDSPM_InputSelect0;
3181 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3182
3183 return 0;
3184}
3185
Takashi Iwai98274f02005-11-17 14:52:34 +01003186static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003188{
3189 static char *texts[] = { "optical", "coaxial" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003190 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003191 return 0;
3192}
3193
Takashi Iwai98274f02005-11-17 14:52:34 +01003194static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003196{
Takashi Iwai98274f02005-11-17 14:52:34 +01003197 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003198
3199 spin_lock_irq(&hdspm->lock);
3200 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3201 spin_unlock_irq(&hdspm->lock);
3202 return 0;
3203}
3204
Takashi Iwai98274f02005-11-17 14:52:34 +01003205static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003207{
Takashi Iwai98274f02005-11-17 14:52:34 +01003208 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003209 int change;
3210 unsigned int val;
3211
3212 if (!snd_hdspm_use_is_exclusive(hdspm))
3213 return -EBUSY;
3214 val = ucontrol->value.integer.value[0] & 1;
3215 spin_lock_irq(&hdspm->lock);
3216 change = (int) val != hdspm_input_select(hdspm);
3217 hdspm_set_input_select(hdspm, val);
3218 spin_unlock_irq(&hdspm->lock);
3219 return change;
3220}
3221
Adrian Knoth0dca1792011-01-26 19:32:14 +01003222
Remy Bruno3cee5a62006-10-16 12:46:32 +02003223#define HDSPM_DS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003224{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3225 .name = xname, \
3226 .index = xindex, \
3227 .info = snd_hdspm_info_ds_wire, \
3228 .get = snd_hdspm_get_ds_wire, \
3229 .put = snd_hdspm_put_ds_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003230}
3231
3232static int hdspm_ds_wire(struct hdspm * hdspm)
3233{
3234 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3235}
3236
3237static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3238{
3239 if (ds)
3240 hdspm->control_register |= HDSPM_DS_DoubleWire;
3241 else
3242 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3243 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3244
3245 return 0;
3246}
3247
3248static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3249 struct snd_ctl_elem_info *uinfo)
3250{
3251 static char *texts[] = { "Single", "Double" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003252 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003253 return 0;
3254}
3255
3256static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3257 struct snd_ctl_elem_value *ucontrol)
3258{
3259 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3260
3261 spin_lock_irq(&hdspm->lock);
3262 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3263 spin_unlock_irq(&hdspm->lock);
3264 return 0;
3265}
3266
3267static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3268 struct snd_ctl_elem_value *ucontrol)
3269{
3270 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3271 int change;
3272 unsigned int val;
3273
3274 if (!snd_hdspm_use_is_exclusive(hdspm))
3275 return -EBUSY;
3276 val = ucontrol->value.integer.value[0] & 1;
3277 spin_lock_irq(&hdspm->lock);
3278 change = (int) val != hdspm_ds_wire(hdspm);
3279 hdspm_set_ds_wire(hdspm, val);
3280 spin_unlock_irq(&hdspm->lock);
3281 return change;
3282}
3283
Adrian Knoth0dca1792011-01-26 19:32:14 +01003284
Remy Bruno3cee5a62006-10-16 12:46:32 +02003285#define HDSPM_QS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003286{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3287 .name = xname, \
3288 .index = xindex, \
3289 .info = snd_hdspm_info_qs_wire, \
3290 .get = snd_hdspm_get_qs_wire, \
3291 .put = snd_hdspm_put_qs_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003292}
3293
3294static int hdspm_qs_wire(struct hdspm * hdspm)
3295{
3296 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3297 return 1;
3298 if (hdspm->control_register & HDSPM_QS_QuadWire)
3299 return 2;
3300 return 0;
3301}
3302
3303static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3304{
3305 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3306 switch (mode) {
3307 case 0:
3308 break;
3309 case 1:
3310 hdspm->control_register |= HDSPM_QS_DoubleWire;
3311 break;
3312 case 2:
3313 hdspm->control_register |= HDSPM_QS_QuadWire;
3314 break;
3315 }
3316 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3317
3318 return 0;
3319}
3320
3321static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3322 struct snd_ctl_elem_info *uinfo)
3323{
3324 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003325 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003326 return 0;
3327}
3328
3329static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3330 struct snd_ctl_elem_value *ucontrol)
3331{
3332 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3333
3334 spin_lock_irq(&hdspm->lock);
3335 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3336 spin_unlock_irq(&hdspm->lock);
3337 return 0;
3338}
3339
3340static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3341 struct snd_ctl_elem_value *ucontrol)
3342{
3343 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3344 int change;
3345 int val;
3346
3347 if (!snd_hdspm_use_is_exclusive(hdspm))
3348 return -EBUSY;
3349 val = ucontrol->value.integer.value[0];
3350 if (val < 0)
3351 val = 0;
3352 if (val > 2)
3353 val = 2;
3354 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003355 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003356 hdspm_set_qs_wire(hdspm, val);
3357 spin_unlock_irq(&hdspm->lock);
3358 return change;
3359}
3360
Adrian Knothacf14762013-07-05 11:28:00 +02003361#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
3362{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3363 .name = xname, \
3364 .private_value = xindex, \
3365 .info = snd_hdspm_info_tristate, \
3366 .get = snd_hdspm_get_tristate, \
3367 .put = snd_hdspm_put_tristate \
3368}
3369
3370static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
3371{
3372 u32 reg = hdspm->settings_register & (regmask * 3);
3373 return reg / regmask;
3374}
3375
3376static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
3377{
3378 hdspm->settings_register &= ~(regmask * 3);
3379 hdspm->settings_register |= (regmask * mode);
3380 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
3381
3382 return 0;
3383}
3384
3385static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_info *uinfo)
3387{
3388 u32 regmask = kcontrol->private_value;
3389
3390 static char *texts_spdif[] = { "Optical", "Coaxial", "Internal" };
3391 static char *texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
3392
3393 switch (regmask) {
3394 case HDSPM_c0_Input0:
3395 ENUMERATED_CTL_INFO(uinfo, texts_spdif);
3396 break;
3397 default:
3398 ENUMERATED_CTL_INFO(uinfo, texts_levels);
3399 break;
3400 }
3401 return 0;
3402}
3403
3404static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3406{
3407 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3408 u32 regmask = kcontrol->private_value;
3409
3410 spin_lock_irq(&hdspm->lock);
3411 ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
3412 spin_unlock_irq(&hdspm->lock);
3413 return 0;
3414}
3415
3416static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3420 u32 regmask = kcontrol->private_value;
3421 int change;
3422 int val;
3423
3424 if (!snd_hdspm_use_is_exclusive(hdspm))
3425 return -EBUSY;
3426 val = ucontrol->value.integer.value[0];
3427 if (val < 0)
3428 val = 0;
3429 if (val > 2)
3430 val = 2;
3431
3432 spin_lock_irq(&hdspm->lock);
3433 change = val != hdspm_tristate(hdspm, regmask);
3434 hdspm_set_tristate(hdspm, val, regmask);
3435 spin_unlock_irq(&hdspm->lock);
3436 return change;
3437}
3438
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003439#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3440{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3441 .name = xname, \
3442 .index = xindex, \
3443 .info = snd_hdspm_info_madi_speedmode, \
3444 .get = snd_hdspm_get_madi_speedmode, \
3445 .put = snd_hdspm_put_madi_speedmode \
3446}
3447
3448static int hdspm_madi_speedmode(struct hdspm *hdspm)
3449{
3450 if (hdspm->control_register & HDSPM_QuadSpeed)
3451 return 2;
3452 if (hdspm->control_register & HDSPM_DoubleSpeed)
3453 return 1;
3454 return 0;
3455}
3456
3457static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3458{
3459 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3460 switch (mode) {
3461 case 0:
3462 break;
3463 case 1:
3464 hdspm->control_register |= HDSPM_DoubleSpeed;
3465 break;
3466 case 2:
3467 hdspm->control_register |= HDSPM_QuadSpeed;
3468 break;
3469 }
3470 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3471
3472 return 0;
3473}
3474
3475static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3476 struct snd_ctl_elem_info *uinfo)
3477{
3478 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003479 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003480 return 0;
3481}
3482
3483static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3484 struct snd_ctl_elem_value *ucontrol)
3485{
3486 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3487
3488 spin_lock_irq(&hdspm->lock);
3489 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3490 spin_unlock_irq(&hdspm->lock);
3491 return 0;
3492}
3493
3494static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3495 struct snd_ctl_elem_value *ucontrol)
3496{
3497 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3498 int change;
3499 int val;
3500
3501 if (!snd_hdspm_use_is_exclusive(hdspm))
3502 return -EBUSY;
3503 val = ucontrol->value.integer.value[0];
3504 if (val < 0)
3505 val = 0;
3506 if (val > 2)
3507 val = 2;
3508 spin_lock_irq(&hdspm->lock);
3509 change = val != hdspm_madi_speedmode(hdspm);
3510 hdspm_set_madi_speedmode(hdspm, val);
3511 spin_unlock_irq(&hdspm->lock);
3512 return change;
3513}
Takashi Iwai763f3562005-06-03 11:25:34 +02003514
3515#define HDSPM_MIXER(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003516{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3517 .name = xname, \
3518 .index = xindex, \
3519 .device = 0, \
3520 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3521 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3522 .info = snd_hdspm_info_mixer, \
3523 .get = snd_hdspm_get_mixer, \
3524 .put = snd_hdspm_put_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003525}
3526
Takashi Iwai98274f02005-11-17 14:52:34 +01003527static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3528 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003529{
3530 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3531 uinfo->count = 3;
3532 uinfo->value.integer.min = 0;
3533 uinfo->value.integer.max = 65535;
3534 uinfo->value.integer.step = 1;
3535 return 0;
3536}
3537
Takashi Iwai98274f02005-11-17 14:52:34 +01003538static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3539 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003540{
Takashi Iwai98274f02005-11-17 14:52:34 +01003541 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003542 int source;
3543 int destination;
3544
3545 source = ucontrol->value.integer.value[0];
3546 if (source < 0)
3547 source = 0;
3548 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3549 source = 2 * HDSPM_MAX_CHANNELS - 1;
3550
3551 destination = ucontrol->value.integer.value[1];
3552 if (destination < 0)
3553 destination = 0;
3554 else if (destination >= HDSPM_MAX_CHANNELS)
3555 destination = HDSPM_MAX_CHANNELS - 1;
3556
3557 spin_lock_irq(&hdspm->lock);
3558 if (source >= HDSPM_MAX_CHANNELS)
3559 ucontrol->value.integer.value[2] =
3560 hdspm_read_pb_gain(hdspm, destination,
3561 source - HDSPM_MAX_CHANNELS);
3562 else
3563 ucontrol->value.integer.value[2] =
3564 hdspm_read_in_gain(hdspm, destination, source);
3565
3566 spin_unlock_irq(&hdspm->lock);
3567
3568 return 0;
3569}
3570
Takashi Iwai98274f02005-11-17 14:52:34 +01003571static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3572 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003573{
Takashi Iwai98274f02005-11-17 14:52:34 +01003574 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003575 int change;
3576 int source;
3577 int destination;
3578 int gain;
3579
3580 if (!snd_hdspm_use_is_exclusive(hdspm))
3581 return -EBUSY;
3582
3583 source = ucontrol->value.integer.value[0];
3584 destination = ucontrol->value.integer.value[1];
3585
3586 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3587 return -1;
3588 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3589 return -1;
3590
3591 gain = ucontrol->value.integer.value[2];
3592
3593 spin_lock_irq(&hdspm->lock);
3594
3595 if (source >= HDSPM_MAX_CHANNELS)
3596 change = gain != hdspm_read_pb_gain(hdspm, destination,
3597 source -
3598 HDSPM_MAX_CHANNELS);
3599 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003600 change = gain != hdspm_read_in_gain(hdspm, destination,
3601 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003602
3603 if (change) {
3604 if (source >= HDSPM_MAX_CHANNELS)
3605 hdspm_write_pb_gain(hdspm, destination,
3606 source - HDSPM_MAX_CHANNELS,
3607 gain);
3608 else
3609 hdspm_write_in_gain(hdspm, destination, source,
3610 gain);
3611 }
3612 spin_unlock_irq(&hdspm->lock);
3613
3614 return change;
3615}
3616
3617/* The simple mixer control(s) provide gain control for the
3618 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003619 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003620*/
3621
3622#define HDSPM_PLAYBACK_MIXER \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003623{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3624 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3625 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3626 .info = snd_hdspm_info_playback_mixer, \
3627 .get = snd_hdspm_get_playback_mixer, \
3628 .put = snd_hdspm_put_playback_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003629}
3630
Takashi Iwai98274f02005-11-17 14:52:34 +01003631static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3632 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003633{
3634 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3635 uinfo->count = 1;
3636 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003637 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003638 uinfo->value.integer.step = 1;
3639 return 0;
3640}
3641
Takashi Iwai98274f02005-11-17 14:52:34 +01003642static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3643 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003644{
Takashi Iwai98274f02005-11-17 14:52:34 +01003645 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003646 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003647
3648 channel = ucontrol->id.index - 1;
3649
Takashi Iwaida3cec32008-08-08 17:12:14 +02003650 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3651 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003652
Takashi Iwai763f3562005-06-03 11:25:34 +02003653 spin_lock_irq(&hdspm->lock);
3654 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003655 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003656 spin_unlock_irq(&hdspm->lock);
3657
Takashi Iwai763f3562005-06-03 11:25:34 +02003658 return 0;
3659}
3660
Takashi Iwai98274f02005-11-17 14:52:34 +01003661static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3662 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003663{
Takashi Iwai98274f02005-11-17 14:52:34 +01003664 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003665 int change;
3666 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003667 int gain;
3668
3669 if (!snd_hdspm_use_is_exclusive(hdspm))
3670 return -EBUSY;
3671
3672 channel = ucontrol->id.index - 1;
3673
Takashi Iwaida3cec32008-08-08 17:12:14 +02003674 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3675 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003676
Adrian Knoth0dca1792011-01-26 19:32:14 +01003677 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003678
3679 spin_lock_irq(&hdspm->lock);
3680 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003681 gain != hdspm_read_pb_gain(hdspm, channel,
3682 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003683 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003684 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003685 gain);
3686 spin_unlock_irq(&hdspm->lock);
3687 return change;
3688}
3689
Adrian Knoth0dca1792011-01-26 19:32:14 +01003690#define HDSPM_SYNC_CHECK(xname, xindex) \
3691{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3692 .name = xname, \
3693 .private_value = xindex, \
3694 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3695 .info = snd_hdspm_info_sync_check, \
3696 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003697}
3698
Adrian Knoth34542212013-03-10 00:37:25 +01003699#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3700{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3701 .name = xname, \
3702 .private_value = xindex, \
3703 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3704 .info = snd_hdspm_tco_info_lock_check, \
3705 .get = snd_hdspm_get_sync_check \
3706}
3707
3708
Adrian Knoth0dca1792011-01-26 19:32:14 +01003709
Takashi Iwai98274f02005-11-17 14:52:34 +01003710static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3711 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003712{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003713 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003714 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003715 return 0;
3716}
3717
Adrian Knoth34542212013-03-10 00:37:25 +01003718static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3719 struct snd_ctl_elem_info *uinfo)
3720{
3721 static char *texts[] = { "No Lock", "Lock" };
3722 ENUMERATED_CTL_INFO(uinfo, texts);
3723 return 0;
3724}
3725
Adrian Knoth0dca1792011-01-26 19:32:14 +01003726static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003727{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003728 int status, status2;
3729
3730 switch (hdspm->io_type) {
3731 case AES32:
3732 status = hdspm_read(hdspm, HDSPM_statusRegister);
Andre Schramm56bde0f2013-01-09 14:40:18 +01003733 if (status & HDSPM_AES32_wcLock) {
3734 if (status & HDSPM_AES32_wcSync)
3735 return 2;
3736 else
3737 return 1;
3738 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02003739 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003740 break;
3741
3742 case MADI:
3743 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003744 if (status2 & HDSPM_wcLock) {
3745 if (status2 & HDSPM_wcSync)
3746 return 2;
3747 else
3748 return 1;
3749 }
3750 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003751 break;
3752
3753 case RayDAT:
3754 case AIO:
3755 status = hdspm_read(hdspm, HDSPM_statusRegister);
3756
3757 if (status & 0x2000000)
3758 return 2;
3759 else if (status & 0x1000000)
3760 return 1;
3761 return 0;
3762
3763 break;
3764
3765 case MADIface:
3766 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003767 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003768
Takashi Iwai763f3562005-06-03 11:25:34 +02003769
Adrian Knoth0dca1792011-01-26 19:32:14 +01003770 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003771}
3772
3773
Adrian Knoth0dca1792011-01-26 19:32:14 +01003774static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003775{
3776 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3777 if (status & HDSPM_madiLock) {
3778 if (status & HDSPM_madiSync)
3779 return 2;
3780 else
3781 return 1;
3782 }
3783 return 0;
3784}
3785
Adrian Knoth0dca1792011-01-26 19:32:14 +01003786
3787static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3788{
3789 int status, lock, sync;
3790
3791 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3792
3793 lock = (status & (0x1<<idx)) ? 1 : 0;
3794 sync = (status & (0x100<<idx)) ? 1 : 0;
3795
3796 if (lock && sync)
3797 return 2;
3798 else if (lock)
3799 return 1;
3800 return 0;
3801}
3802
3803
3804static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3805{
3806 int status, lock = 0, sync = 0;
3807
3808 switch (hdspm->io_type) {
3809 case RayDAT:
3810 case AIO:
3811 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3812 lock = (status & 0x400) ? 1 : 0;
3813 sync = (status & 0x800) ? 1 : 0;
3814 break;
3815
3816 case MADI:
Adrian Knoth2e0452f2012-10-19 17:42:27 +02003817 status = hdspm_read(hdspm, HDSPM_statusRegister);
3818 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3819 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3820 break;
3821
Adrian Knoth0dca1792011-01-26 19:32:14 +01003822 case AES32:
3823 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth9a215f42012-10-19 17:42:28 +02003824 lock = (status & 0x100000) ? 1 : 0;
3825 sync = (status & 0x200000) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003826 break;
3827
3828 case MADIface:
3829 break;
3830 }
3831
3832 if (lock && sync)
3833 return 2;
3834 else if (lock)
3835 return 1;
3836
3837 return 0;
3838}
3839
3840static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3841{
3842 int status2, lock, sync;
3843 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3844
3845 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3846 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3847
3848 if (sync)
3849 return 2;
3850 else if (lock)
3851 return 1;
3852 return 0;
3853}
3854
Adrian Knoth34542212013-03-10 00:37:25 +01003855static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3856{
3857 u32 status;
3858 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3859
3860 return (status & mask) ? 1 : 0;
3861}
3862
Adrian Knoth0dca1792011-01-26 19:32:14 +01003863
3864static int hdspm_tco_sync_check(struct hdspm *hdspm)
3865{
3866 int status;
3867
3868 if (hdspm->tco) {
3869 switch (hdspm->io_type) {
3870 case MADI:
3871 case AES32:
3872 status = hdspm_read(hdspm, HDSPM_statusRegister);
3873 if (status & HDSPM_tcoLock) {
3874 if (status & HDSPM_tcoSync)
3875 return 2;
3876 else
3877 return 1;
3878 }
3879 return 0;
3880
3881 break;
3882
3883 case RayDAT:
3884 case AIO:
3885 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3886
3887 if (status & 0x8000000)
3888 return 2; /* Sync */
3889 if (status & 0x4000000)
3890 return 1; /* Lock */
3891 return 0; /* No signal */
3892 break;
3893
3894 default:
3895 break;
3896 }
3897 }
3898
3899 return 3; /* N/A */
3900}
3901
3902
3903static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3904 struct snd_ctl_elem_value *ucontrol)
3905{
3906 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3907 int val = -1;
3908
3909 switch (hdspm->io_type) {
3910 case RayDAT:
3911 switch (kcontrol->private_value) {
3912 case 0: /* WC */
3913 val = hdspm_wc_sync_check(hdspm); break;
3914 case 7: /* TCO */
3915 val = hdspm_tco_sync_check(hdspm); break;
3916 case 8: /* SYNC IN */
3917 val = hdspm_sync_in_sync_check(hdspm); break;
3918 default:
Adrian Knothd1a3c982012-11-07 18:00:09 +01003919 val = hdspm_s1_sync_check(hdspm,
3920 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003921 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003922 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003923
3924 case AIO:
3925 switch (kcontrol->private_value) {
3926 case 0: /* WC */
3927 val = hdspm_wc_sync_check(hdspm); break;
3928 case 4: /* TCO */
3929 val = hdspm_tco_sync_check(hdspm); break;
3930 case 5: /* SYNC IN */
3931 val = hdspm_sync_in_sync_check(hdspm); break;
3932 default:
3933 val = hdspm_s1_sync_check(hdspm, ucontrol->id.index-1);
3934 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003935 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003936
3937 case MADI:
3938 switch (kcontrol->private_value) {
3939 case 0: /* WC */
3940 val = hdspm_wc_sync_check(hdspm); break;
3941 case 1: /* MADI */
3942 val = hdspm_madi_sync_check(hdspm); break;
3943 case 2: /* TCO */
3944 val = hdspm_tco_sync_check(hdspm); break;
3945 case 3: /* SYNC_IN */
3946 val = hdspm_sync_in_sync_check(hdspm); break;
3947 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003948 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003949
3950 case MADIface:
3951 val = hdspm_madi_sync_check(hdspm); /* MADI */
3952 break;
3953
3954 case AES32:
3955 switch (kcontrol->private_value) {
3956 case 0: /* WC */
3957 val = hdspm_wc_sync_check(hdspm); break;
3958 case 9: /* TCO */
3959 val = hdspm_tco_sync_check(hdspm); break;
3960 case 10 /* SYNC IN */:
3961 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003962 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01003963 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003964 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003965 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003966 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003967
3968 }
3969
Adrian Knoth34542212013-03-10 00:37:25 +01003970 if (hdspm->tco) {
3971 switch (kcontrol->private_value) {
3972 case 11:
3973 /* Check TCO for lock state of its current input */
3974 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
3975 break;
3976 case 12:
3977 /* Check TCO for valid time code on LTC input. */
3978 val = hdspm_tco_input_check(hdspm,
3979 HDSPM_TCO1_LTC_Input_valid);
3980 break;
3981 default:
3982 break;
3983 }
3984 }
3985
Adrian Knoth0dca1792011-01-26 19:32:14 +01003986 if (-1 == val)
3987 val = 3;
3988
3989 ucontrol->value.enumerated.item[0] = val;
3990 return 0;
3991}
3992
3993
3994
3995/**
3996 * TCO controls
3997 **/
3998static void hdspm_tco_write(struct hdspm *hdspm)
3999{
4000 unsigned int tc[4] = { 0, 0, 0, 0};
4001
4002 switch (hdspm->tco->input) {
4003 case 0:
4004 tc[2] |= HDSPM_TCO2_set_input_MSB;
4005 break;
4006 case 1:
4007 tc[2] |= HDSPM_TCO2_set_input_LSB;
4008 break;
4009 default:
4010 break;
4011 }
4012
4013 switch (hdspm->tco->framerate) {
4014 case 1:
4015 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
4016 break;
4017 case 2:
4018 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
4019 break;
4020 case 3:
4021 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
4022 HDSPM_TCO1_set_drop_frame_flag;
4023 break;
4024 case 4:
4025 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4026 HDSPM_TCO1_LTC_Format_MSB;
4027 break;
4028 case 5:
4029 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4030 HDSPM_TCO1_LTC_Format_MSB +
4031 HDSPM_TCO1_set_drop_frame_flag;
4032 break;
4033 default:
4034 break;
4035 }
4036
4037 switch (hdspm->tco->wordclock) {
4038 case 1:
4039 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
4040 break;
4041 case 2:
4042 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4043 break;
4044 default:
4045 break;
4046 }
4047
4048 switch (hdspm->tco->samplerate) {
4049 case 1:
4050 tc[2] |= HDSPM_TCO2_set_freq;
4051 break;
4052 case 2:
4053 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4054 break;
4055 default:
4056 break;
4057 }
4058
4059 switch (hdspm->tco->pull) {
4060 case 1:
4061 tc[2] |= HDSPM_TCO2_set_pull_up;
4062 break;
4063 case 2:
4064 tc[2] |= HDSPM_TCO2_set_pull_down;
4065 break;
4066 case 3:
4067 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4068 break;
4069 case 4:
4070 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4071 break;
4072 default:
4073 break;
4074 }
4075
4076 if (1 == hdspm->tco->term) {
4077 tc[2] |= HDSPM_TCO2_set_term_75R;
4078 }
4079
4080 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4081 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4082 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4083 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4084}
4085
4086
4087#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4088{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4089 .name = xname, \
4090 .index = xindex, \
4091 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4092 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4093 .info = snd_hdspm_info_tco_sample_rate, \
4094 .get = snd_hdspm_get_tco_sample_rate, \
4095 .put = snd_hdspm_put_tco_sample_rate \
4096}
4097
4098static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4099 struct snd_ctl_elem_info *uinfo)
4100{
4101 static char *texts[] = { "44.1 kHz", "48 kHz" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004102 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004103 return 0;
4104}
4105
4106static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4107 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02004108{
Takashi Iwai98274f02005-11-17 14:52:34 +01004109 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02004110
Adrian Knoth0dca1792011-01-26 19:32:14 +01004111 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4112
Takashi Iwai763f3562005-06-03 11:25:34 +02004113 return 0;
4114}
4115
Adrian Knoth0dca1792011-01-26 19:32:14 +01004116static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4117 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02004118{
Adrian Knoth0dca1792011-01-26 19:32:14 +01004119 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4120
4121 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4122 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4123
4124 hdspm_tco_write(hdspm);
4125
4126 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004127 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01004128
Remy Bruno3cee5a62006-10-16 12:46:32 +02004129 return 0;
4130}
4131
Adrian Knoth0dca1792011-01-26 19:32:14 +01004132
4133#define HDSPM_TCO_PULL(xname, xindex) \
4134{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4135 .name = xname, \
4136 .index = xindex, \
4137 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4138 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4139 .info = snd_hdspm_info_tco_pull, \
4140 .get = snd_hdspm_get_tco_pull, \
4141 .put = snd_hdspm_put_tco_pull \
4142}
4143
4144static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4145 struct snd_ctl_elem_info *uinfo)
4146{
4147 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004148 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004149 return 0;
4150}
4151
4152static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4153 struct snd_ctl_elem_value *ucontrol)
4154{
4155 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4156
4157 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4158
4159 return 0;
4160}
4161
4162static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4163 struct snd_ctl_elem_value *ucontrol)
4164{
4165 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4166
4167 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4168 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4169
4170 hdspm_tco_write(hdspm);
4171
4172 return 1;
4173 }
4174
4175 return 0;
4176}
4177
4178#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4179{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4180 .name = xname, \
4181 .index = xindex, \
4182 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4183 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4184 .info = snd_hdspm_info_tco_wck_conversion, \
4185 .get = snd_hdspm_get_tco_wck_conversion, \
4186 .put = snd_hdspm_put_tco_wck_conversion \
4187}
4188
4189static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4190 struct snd_ctl_elem_info *uinfo)
4191{
4192 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004193 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004194 return 0;
4195}
4196
4197static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4198 struct snd_ctl_elem_value *ucontrol)
4199{
4200 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4201
4202 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4203
4204 return 0;
4205}
4206
4207static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4208 struct snd_ctl_elem_value *ucontrol)
4209{
4210 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4211
4212 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4213 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4214
4215 hdspm_tco_write(hdspm);
4216
4217 return 1;
4218 }
4219
4220 return 0;
4221}
4222
4223
4224#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4225{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4226 .name = xname, \
4227 .index = xindex, \
4228 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4229 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4230 .info = snd_hdspm_info_tco_frame_rate, \
4231 .get = snd_hdspm_get_tco_frame_rate, \
4232 .put = snd_hdspm_put_tco_frame_rate \
4233}
4234
4235static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4236 struct snd_ctl_elem_info *uinfo)
4237{
4238 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4239 "29.97 dfps", "30 fps", "30 dfps" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004240 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004241 return 0;
4242}
4243
4244static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004245 struct snd_ctl_elem_value *ucontrol)
4246{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004247 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4248
Adrian Knoth0dca1792011-01-26 19:32:14 +01004249 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004250
Remy Bruno3cee5a62006-10-16 12:46:32 +02004251 return 0;
4252}
Takashi Iwai763f3562005-06-03 11:25:34 +02004253
Adrian Knoth0dca1792011-01-26 19:32:14 +01004254static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4255 struct snd_ctl_elem_value *ucontrol)
4256{
4257 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4258
4259 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4260 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4261
4262 hdspm_tco_write(hdspm);
4263
4264 return 1;
4265 }
4266
4267 return 0;
4268}
4269
4270
4271#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4272{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4273 .name = xname, \
4274 .index = xindex, \
4275 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4276 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4277 .info = snd_hdspm_info_tco_sync_source, \
4278 .get = snd_hdspm_get_tco_sync_source, \
4279 .put = snd_hdspm_put_tco_sync_source \
4280}
4281
4282static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4283 struct snd_ctl_elem_info *uinfo)
4284{
4285 static char *texts[] = { "LTC", "Video", "WCK" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004286 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004287 return 0;
4288}
4289
4290static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4291 struct snd_ctl_elem_value *ucontrol)
4292{
4293 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4294
4295 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4296
4297 return 0;
4298}
4299
4300static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4301 struct snd_ctl_elem_value *ucontrol)
4302{
4303 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4304
4305 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4306 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4307
4308 hdspm_tco_write(hdspm);
4309
4310 return 1;
4311 }
4312
4313 return 0;
4314}
4315
4316
4317#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4318{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4319 .name = xname, \
4320 .index = xindex, \
4321 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4322 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4323 .info = snd_hdspm_info_tco_word_term, \
4324 .get = snd_hdspm_get_tco_word_term, \
4325 .put = snd_hdspm_put_tco_word_term \
4326}
4327
4328static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4329 struct snd_ctl_elem_info *uinfo)
4330{
4331 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4332 uinfo->count = 1;
4333 uinfo->value.integer.min = 0;
4334 uinfo->value.integer.max = 1;
4335
4336 return 0;
4337}
4338
4339
4340static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4341 struct snd_ctl_elem_value *ucontrol)
4342{
4343 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4344
4345 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4346
4347 return 0;
4348}
4349
4350
4351static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4352 struct snd_ctl_elem_value *ucontrol)
4353{
4354 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4355
4356 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4357 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4358
4359 hdspm_tco_write(hdspm);
4360
4361 return 1;
4362 }
4363
4364 return 0;
4365}
4366
4367
4368
Takashi Iwai763f3562005-06-03 11:25:34 +02004369
Remy Bruno3cee5a62006-10-16 12:46:32 +02004370static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004371 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004372 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004373 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4374 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4375 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4376 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knothb8812c52012-10-19 17:42:26 +02004377 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004378 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4379 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
Adrian Knoth930f4ff2012-10-19 17:42:29 +02004380 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004381 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Adrian Knothc9e16682012-12-03 14:55:50 +01004382 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4383 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
Adrian Knoth696be0f2013-03-10 00:37:23 +01004384 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
Adrian Knothc9e16682012-12-03 14:55:50 +01004385 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4386 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004387 HDSPM_INPUT_SELECT("Input Select", 0),
4388 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004389};
4390
4391
4392static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4393 HDSPM_MIXER("Mixer", 0),
4394 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4395 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4396 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4397 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4398 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
Adrian Knothc9e16682012-12-03 14:55:50 +01004399 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4400 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4401 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004402 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004403};
4404
Adrian Knoth0dca1792011-01-26 19:32:14 +01004405static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004406 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004407 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004408 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4409 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4410 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4411 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004412 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004413 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4414 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4415 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4416 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4417 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4418 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4419 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4420 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4421 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4422 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4423 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004424 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004425 HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004426 HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
4427 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4428 HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
4429 HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004430 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
4431 HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
4432 HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
4433 HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004434
4435 /*
4436 HDSPM_INPUT_SELECT("Input Select", 0),
4437 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4438 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4439 HDSPM_SPDIF_IN("SPDIF In", 0);
4440 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4441 HDSPM_INPUT_LEVEL("Input Level", 0);
4442 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4443 HDSPM_PHONES("Phones", 0);
4444 */
4445};
4446
4447static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4448 HDSPM_MIXER("Mixer", 0),
4449 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4450 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4451 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4452 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4453 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4454 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4455 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4456 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4457 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4458 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4459 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4460 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4461 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4462 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4463 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4464 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4465 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4466 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4467 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4468 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4469 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
Adrian Knoth11a5cd32013-07-05 11:27:57 +02004470 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
4471 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4472 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004473};
4474
4475static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4476 HDSPM_MIXER("Mixer", 0),
4477 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4478 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4479 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4480 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4481 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4482 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4483 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4484 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4485 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4486 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4487 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4488 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4489 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4490 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4491 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4492 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4493 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4494 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4495 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4496 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4497 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4498 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4499 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4500 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4501 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4502 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4503 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4504 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Adrian Knothc9e16682012-12-03 14:55:50 +01004505 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4506 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4507 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4508 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4509 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004510 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4511 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4512};
4513
Adrian Knoth0dca1792011-01-26 19:32:14 +01004514
4515
4516/* Control elements for the optional TCO module */
4517static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4518 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4519 HDSPM_TCO_PULL("TCO Pull", 0),
4520 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4521 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4522 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
Adrian Knotha8176502013-03-10 00:37:27 +01004523 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4524 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4525 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4526 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4527 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004528};
4529
4530
Takashi Iwai98274f02005-11-17 14:52:34 +01004531static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004532
4533
Takashi Iwai98274f02005-11-17 14:52:34 +01004534static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004535{
4536 int i;
4537
Adrian Knoth0dca1792011-01-26 19:32:14 +01004538 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004539 if (hdspm->system_sample_rate > 48000) {
4540 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004541 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4542 SNDRV_CTL_ELEM_ACCESS_READ |
4543 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004544 } else {
4545 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004546 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4547 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004548 }
4549 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004550 SNDRV_CTL_EVENT_MASK_INFO,
4551 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004552 }
4553
4554 return 0;
4555}
4556
4557
Adrian Knoth0dca1792011-01-26 19:32:14 +01004558static int snd_hdspm_create_controls(struct snd_card *card,
4559 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004560{
4561 unsigned int idx, limit;
4562 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004563 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004564 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004565
Adrian Knoth0dca1792011-01-26 19:32:14 +01004566 switch (hdspm->io_type) {
4567 case MADI:
4568 list = snd_hdspm_controls_madi;
4569 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4570 break;
4571 case MADIface:
4572 list = snd_hdspm_controls_madiface;
4573 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4574 break;
4575 case AIO:
4576 list = snd_hdspm_controls_aio;
4577 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4578 break;
4579 case RayDAT:
4580 list = snd_hdspm_controls_raydat;
4581 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4582 break;
4583 case AES32:
4584 list = snd_hdspm_controls_aes32;
4585 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4586 break;
4587 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004588
Adrian Knoth0dca1792011-01-26 19:32:14 +01004589 if (NULL != list) {
4590 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004591 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004592 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004593 if (err < 0)
4594 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004595 }
4596 }
4597
Takashi Iwai763f3562005-06-03 11:25:34 +02004598
Adrian Knoth0dca1792011-01-26 19:32:14 +01004599 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004600 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004601 if (hdspm->system_sample_rate >= 128000) {
4602 limit = hdspm->qs_out_channels;
4603 } else if (hdspm->system_sample_rate >= 64000) {
4604 limit = hdspm->ds_out_channels;
4605 } else {
4606 limit = hdspm->ss_out_channels;
4607 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004608 for (idx = 0; idx < limit; ++idx) {
4609 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004610 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4611 err = snd_ctl_add(card, kctl);
4612 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004613 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004614 hdspm->playback_mixer_ctls[idx] = kctl;
4615 }
4616
Adrian Knoth0dca1792011-01-26 19:32:14 +01004617
4618 if (hdspm->tco) {
4619 /* add tco control elements */
4620 list = snd_hdspm_controls_tco;
4621 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4622 for (idx = 0; idx < limit; idx++) {
4623 err = snd_ctl_add(card,
4624 snd_ctl_new1(&list[idx], hdspm));
4625 if (err < 0)
4626 return err;
4627 }
4628 }
4629
Takashi Iwai763f3562005-06-03 11:25:34 +02004630 return 0;
4631}
4632
4633/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004634 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004635 ------------------------------------------------------------*/
4636
4637static void
Remy Bruno3cee5a62006-10-16 12:46:32 +02004638snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
4639 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004640{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004641 struct hdspm *hdspm = entry->private_data;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004642 unsigned int status, status2, control, freq;
4643
Takashi Iwai763f3562005-06-03 11:25:34 +02004644 char *pref_sync_ref;
4645 char *autosync_ref;
4646 char *system_clock_mode;
Takashi Iwai763f3562005-06-03 11:25:34 +02004647 char *insel;
Takashi Iwai763f3562005-06-03 11:25:34 +02004648 int x, x2;
4649
Adrian Knoth0dca1792011-01-26 19:32:14 +01004650 /* TCO stuff */
4651 int a, ltc, frames, seconds, minutes, hours;
4652 unsigned int period;
4653 u64 freq_const = 0;
4654 u32 rate;
4655
Takashi Iwai763f3562005-06-03 11:25:34 +02004656 status = hdspm_read(hdspm, HDSPM_statusRegister);
4657 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004658 control = hdspm->control_register;
4659 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
Takashi Iwai763f3562005-06-03 11:25:34 +02004660
4661 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004662 hdspm->card_name, hdspm->card->number + 1,
4663 hdspm->firmware_rev,
4664 (status2 & HDSPM_version0) |
4665 (status2 & HDSPM_version1) | (status2 &
4666 HDSPM_version2));
4667
4668 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4669 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
Adrian Knoth7d53a632012-01-04 14:31:16 +01004670 hdspm->serial);
Takashi Iwai763f3562005-06-03 11:25:34 +02004671
4672 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004673 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
Takashi Iwai763f3562005-06-03 11:25:34 +02004674
4675 snd_iprintf(buffer, "--- System ---\n");
4676
4677 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004678 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4679 status & HDSPM_audioIRQPending,
4680 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4681 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4682 hdspm->irq_count);
Takashi Iwai763f3562005-06-03 11:25:34 +02004683 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004684 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4685 "estimated= %ld (bytes)\n",
4686 ((status & HDSPM_BufferID) ? 1 : 0),
4687 (status & HDSPM_BufferPositionMask),
4688 (status & HDSPM_BufferPositionMask) %
4689 (2 * (int)hdspm->period_bytes),
4690 ((status & HDSPM_BufferPositionMask) - 64) %
4691 (2 * (int)hdspm->period_bytes),
4692 (long) hdspm_hw_pointer(hdspm) * 4);
Takashi Iwai763f3562005-06-03 11:25:34 +02004693
4694 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004695 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4696 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4697 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4698 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4699 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
Takashi Iwai763f3562005-06-03 11:25:34 +02004700 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004701 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4702 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4703 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4704 snd_iprintf(buffer,
4705 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4706 "status2=0x%x\n",
4707 hdspm->control_register, hdspm->control2_register,
4708 status, status2);
4709 if (status & HDSPM_tco_detect) {
4710 snd_iprintf(buffer, "TCO module detected.\n");
4711 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4712 if (a & HDSPM_TCO1_LTC_Input_valid) {
4713 snd_iprintf(buffer, " LTC valid, ");
4714 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4715 HDSPM_TCO1_LTC_Format_MSB)) {
4716 case 0:
4717 snd_iprintf(buffer, "24 fps, ");
4718 break;
4719 case HDSPM_TCO1_LTC_Format_LSB:
4720 snd_iprintf(buffer, "25 fps, ");
4721 break;
4722 case HDSPM_TCO1_LTC_Format_MSB:
4723 snd_iprintf(buffer, "29.97 fps, ");
4724 break;
4725 default:
4726 snd_iprintf(buffer, "30 fps, ");
4727 break;
4728 }
4729 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4730 snd_iprintf(buffer, "drop frame\n");
4731 } else {
4732 snd_iprintf(buffer, "full frame\n");
4733 }
4734 } else {
4735 snd_iprintf(buffer, " no LTC\n");
4736 }
4737 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4738 snd_iprintf(buffer, " Video: NTSC\n");
4739 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4740 snd_iprintf(buffer, " Video: PAL\n");
4741 } else {
4742 snd_iprintf(buffer, " No video\n");
4743 }
4744 if (a & HDSPM_TCO1_TCO_lock) {
4745 snd_iprintf(buffer, " Sync: lock\n");
4746 } else {
4747 snd_iprintf(buffer, " Sync: no lock\n");
4748 }
4749
4750 switch (hdspm->io_type) {
4751 case MADI:
4752 case AES32:
4753 freq_const = 110069313433624ULL;
4754 break;
4755 case RayDAT:
4756 case AIO:
4757 freq_const = 104857600000000ULL;
4758 break;
4759 case MADIface:
4760 break; /* no TCO possible */
4761 }
4762
4763 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4764 snd_iprintf(buffer, " period: %u\n", period);
4765
4766
4767 /* rate = freq_const/period; */
4768 rate = div_u64(freq_const, period);
4769
4770 if (control & HDSPM_QuadSpeed) {
4771 rate *= 4;
4772 } else if (control & HDSPM_DoubleSpeed) {
4773 rate *= 2;
4774 }
4775
4776 snd_iprintf(buffer, " Frequency: %u Hz\n",
4777 (unsigned int) rate);
4778
4779 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4780 frames = ltc & 0xF;
4781 ltc >>= 4;
4782 frames += (ltc & 0x3) * 10;
4783 ltc >>= 4;
4784 seconds = ltc & 0xF;
4785 ltc >>= 4;
4786 seconds += (ltc & 0x7) * 10;
4787 ltc >>= 4;
4788 minutes = ltc & 0xF;
4789 ltc >>= 4;
4790 minutes += (ltc & 0x7) * 10;
4791 ltc >>= 4;
4792 hours = ltc & 0xF;
4793 ltc >>= 4;
4794 hours += (ltc & 0x3) * 10;
4795 snd_iprintf(buffer,
4796 " LTC In: %02d:%02d:%02d:%02d\n",
4797 hours, minutes, seconds, frames);
4798
4799 } else {
4800 snd_iprintf(buffer, "No TCO module detected.\n");
4801 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004802
4803 snd_iprintf(buffer, "--- Settings ---\n");
4804
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004805 x = hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02004806
4807 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004808 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4809 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004810
Adrian Knoth0dca1792011-01-26 19:32:14 +01004811 snd_iprintf(buffer, "Line out: %s\n",
4812 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004813
4814 switch (hdspm->control_register & HDSPM_InputMask) {
4815 case HDSPM_InputOptical:
4816 insel = "Optical";
4817 break;
4818 case HDSPM_InputCoaxial:
4819 insel = "Coaxial";
4820 break;
4821 default:
Masanari Iidaec8f53f2012-11-02 00:28:50 +09004822 insel = "Unknown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004823 }
4824
Takashi Iwai763f3562005-06-03 11:25:34 +02004825 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004826 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4827 "Auto Input %s\n",
4828 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4829 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4830 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004831
Adrian Knoth0dca1792011-01-26 19:32:14 +01004832
Remy Bruno3cee5a62006-10-16 12:46:32 +02004833 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004834 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004835 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004836 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004837 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004838
4839 switch (hdspm_pref_sync_ref(hdspm)) {
4840 case HDSPM_SYNC_FROM_WORD:
4841 pref_sync_ref = "Word Clock";
4842 break;
4843 case HDSPM_SYNC_FROM_MADI:
4844 pref_sync_ref = "MADI Sync";
4845 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004846 case HDSPM_SYNC_FROM_TCO:
4847 pref_sync_ref = "TCO";
4848 break;
4849 case HDSPM_SYNC_FROM_SYNC_IN:
4850 pref_sync_ref = "Sync In";
4851 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004852 default:
4853 pref_sync_ref = "XXXX Clock";
4854 break;
4855 }
4856 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004857 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004858
4859 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004860 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004861
4862
4863 snd_iprintf(buffer, "--- Status:\n");
4864
4865 x = status & HDSPM_madiSync;
4866 x2 = status2 & HDSPM_wcSync;
4867
4868 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004869 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4870 "NoLock",
4871 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4872 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004873
4874 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004875 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4876 autosync_ref = "Sync In";
4877 break;
4878 case HDSPM_AUTOSYNC_FROM_TCO:
4879 autosync_ref = "TCO";
4880 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004881 case HDSPM_AUTOSYNC_FROM_WORD:
4882 autosync_ref = "Word Clock";
4883 break;
4884 case HDSPM_AUTOSYNC_FROM_MADI:
4885 autosync_ref = "MADI Sync";
4886 break;
4887 case HDSPM_AUTOSYNC_FROM_NONE:
4888 autosync_ref = "Input not valid";
4889 break;
4890 default:
4891 autosync_ref = "---";
4892 break;
4893 }
4894 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004895 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4896 autosync_ref, hdspm_external_sample_rate(hdspm),
4897 (status & HDSPM_madiFreqMask) >> 22,
4898 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004899
4900 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004901 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4902 (status & HDSPM_RX_64ch) ? "64 channels" :
4903 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004904
4905 snd_iprintf(buffer, "\n");
4906}
4907
Remy Bruno3cee5a62006-10-16 12:46:32 +02004908static void
4909snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4910 struct snd_info_buffer *buffer)
4911{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004912 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004913 unsigned int status;
4914 unsigned int status2;
4915 unsigned int timecode;
Andre Schramm56bde0f2013-01-09 14:40:18 +01004916 unsigned int wcLock, wcSync;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004917 int pref_syncref;
4918 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004919 int x;
4920
4921 status = hdspm_read(hdspm, HDSPM_statusRegister);
4922 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4923 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4924
4925 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4926 hdspm->card_name, hdspm->card->number + 1,
4927 hdspm->firmware_rev);
4928
4929 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4930 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4931
4932 snd_iprintf(buffer, "--- System ---\n");
4933
4934 snd_iprintf(buffer,
4935 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4936 status & HDSPM_audioIRQPending,
4937 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4938 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4939 hdspm->irq_count);
4940 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004941 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4942 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004943 ((status & HDSPM_BufferID) ? 1 : 0),
4944 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004945 (status & HDSPM_BufferPositionMask) %
4946 (2 * (int)hdspm->period_bytes),
4947 ((status & HDSPM_BufferPositionMask) - 64) %
4948 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004949 (long) hdspm_hw_pointer(hdspm) * 4);
4950
4951 snd_iprintf(buffer,
4952 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4953 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4954 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4955 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4956 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4957 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004958 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4959 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4960 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4961 snd_iprintf(buffer,
4962 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4963 "status2=0x%x\n",
4964 hdspm->control_register, hdspm->control2_register,
4965 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004966
4967 snd_iprintf(buffer, "--- Settings ---\n");
4968
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004969 x = hdspm_get_latency(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004970
4971 snd_iprintf(buffer,
4972 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4973 x, (unsigned long) hdspm->period_bytes);
4974
Adrian Knoth0dca1792011-01-26 19:32:14 +01004975 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004976 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01004977 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02004978
4979 snd_iprintf(buffer,
4980 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
4981 (hdspm->
4982 control_register & HDSPM_clr_tms) ? "on" : "off",
4983 (hdspm->
4984 control_register & HDSPM_Emphasis) ? "on" : "off",
4985 (hdspm->
4986 control_register & HDSPM_Dolby) ? "on" : "off");
4987
Remy Bruno3cee5a62006-10-16 12:46:32 +02004988
4989 pref_syncref = hdspm_pref_sync_ref(hdspm);
4990 if (pref_syncref == 0)
4991 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
4992 else
4993 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
4994 pref_syncref);
4995
4996 snd_iprintf(buffer, "System Clock Frequency: %d\n",
4997 hdspm->system_sample_rate);
4998
4999 snd_iprintf(buffer, "Double speed: %s\n",
5000 hdspm->control_register & HDSPM_DS_DoubleWire?
5001 "Double wire" : "Single wire");
5002 snd_iprintf(buffer, "Quad speed: %s\n",
5003 hdspm->control_register & HDSPM_QS_DoubleWire?
5004 "Double wire" :
5005 hdspm->control_register & HDSPM_QS_QuadWire?
5006 "Quad wire" : "Single wire");
5007
5008 snd_iprintf(buffer, "--- Status:\n");
5009
Andre Schramm56bde0f2013-01-09 14:40:18 +01005010 wcLock = status & HDSPM_AES32_wcLock;
5011 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
5012
Remy Bruno3cee5a62006-10-16 12:46:32 +02005013 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Andre Schramm56bde0f2013-01-09 14:40:18 +01005014 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005015 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005016
5017 for (x = 0; x < 8; x++) {
5018 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005019 x+1,
5020 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01005021 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005022 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005023 }
5024
5025 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005026 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5027 autosync_ref = "None"; break;
5028 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5029 autosync_ref = "Word Clock"; break;
5030 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5031 autosync_ref = "AES1"; break;
5032 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5033 autosync_ref = "AES2"; break;
5034 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5035 autosync_ref = "AES3"; break;
5036 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5037 autosync_ref = "AES4"; break;
5038 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5039 autosync_ref = "AES5"; break;
5040 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5041 autosync_ref = "AES6"; break;
5042 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5043 autosync_ref = "AES7"; break;
5044 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5045 autosync_ref = "AES8"; break;
5046 default:
5047 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005048 }
5049 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5050
5051 snd_iprintf(buffer, "\n");
5052}
5053
Adrian Knoth0dca1792011-01-26 19:32:14 +01005054static void
5055snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5056 struct snd_info_buffer *buffer)
5057{
5058 struct hdspm *hdspm = entry->private_data;
5059 unsigned int status1, status2, status3, control, i;
5060 unsigned int lock, sync;
5061
5062 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5063 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5064 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5065
5066 control = hdspm->control_register;
5067
5068 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5069 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5070 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5071
5072
5073 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5074
5075 snd_iprintf(buffer, "Clock mode : %s\n",
5076 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5077 snd_iprintf(buffer, "System frequency: %d Hz\n",
5078 hdspm_get_system_sample_rate(hdspm));
5079
5080 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5081
5082 lock = 0x1;
5083 sync = 0x100;
5084
5085 for (i = 0; i < 8; i++) {
5086 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5087 i,
5088 (status1 & lock) ? 1 : 0,
5089 (status1 & sync) ? 1 : 0,
5090 texts_freq[(status2 >> (i * 4)) & 0xF]);
5091
5092 lock = lock<<1;
5093 sync = sync<<1;
5094 }
5095
5096 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5097 (status1 & 0x1000000) ? 1 : 0,
5098 (status1 & 0x2000000) ? 1 : 0,
5099 texts_freq[(status1 >> 16) & 0xF]);
5100
5101 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5102 (status1 & 0x4000000) ? 1 : 0,
5103 (status1 & 0x8000000) ? 1 : 0,
5104 texts_freq[(status1 >> 20) & 0xF]);
5105
5106 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5107 (status3 & 0x400) ? 1 : 0,
5108 (status3 & 0x800) ? 1 : 0,
5109 texts_freq[(status2 >> 12) & 0xF]);
5110
5111}
5112
Remy Bruno3cee5a62006-10-16 12:46:32 +02005113#ifdef CONFIG_SND_DEBUG
5114static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01005115snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005116 struct snd_info_buffer *buffer)
5117{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005118 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005119
5120 int j,i;
5121
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005122 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02005123 snd_iprintf(buffer, "0x%08X: ", i);
5124 for (j = 0; j < 16; j += 4)
5125 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5126 snd_iprintf(buffer, "\n");
5127 }
5128}
5129#endif
5130
5131
Adrian Knoth0dca1792011-01-26 19:32:14 +01005132static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5133 struct snd_info_buffer *buffer)
5134{
5135 struct hdspm *hdspm = entry->private_data;
5136 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005137
Adrian Knoth0dca1792011-01-26 19:32:14 +01005138 snd_iprintf(buffer, "# generated by hdspm\n");
5139
5140 for (i = 0; i < hdspm->max_channels_in; i++) {
5141 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5142 }
5143}
5144
5145static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5146 struct snd_info_buffer *buffer)
5147{
5148 struct hdspm *hdspm = entry->private_data;
5149 int i;
5150
5151 snd_iprintf(buffer, "# generated by hdspm\n");
5152
5153 for (i = 0; i < hdspm->max_channels_out; i++) {
5154 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5155 }
5156}
5157
5158
Bill Pembertone23e7a12012-12-06 12:35:10 -05005159static void snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005160{
Takashi Iwai98274f02005-11-17 14:52:34 +01005161 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02005162
Adrian Knoth0dca1792011-01-26 19:32:14 +01005163 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5164 switch (hdspm->io_type) {
5165 case AES32:
5166 snd_info_set_text_ops(entry, hdspm,
5167 snd_hdspm_proc_read_aes32);
5168 break;
5169 case MADI:
5170 snd_info_set_text_ops(entry, hdspm,
5171 snd_hdspm_proc_read_madi);
5172 break;
5173 case MADIface:
5174 /* snd_info_set_text_ops(entry, hdspm,
5175 snd_hdspm_proc_read_madiface); */
5176 break;
5177 case RayDAT:
5178 snd_info_set_text_ops(entry, hdspm,
5179 snd_hdspm_proc_read_raydat);
5180 break;
5181 case AIO:
5182 break;
5183 }
5184 }
5185
5186 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5187 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5188 }
5189
5190 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5191 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5192 }
5193
Remy Bruno3cee5a62006-10-16 12:46:32 +02005194#ifdef CONFIG_SND_DEBUG
5195 /* debug file to read all hdspm registers */
5196 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5197 snd_info_set_text_ops(entry, hdspm,
5198 snd_hdspm_proc_read_debug);
5199#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02005200}
5201
5202/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005203 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005204 ------------------------------------------------------------*/
5205
Takashi Iwai98274f02005-11-17 14:52:34 +01005206static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005207{
Takashi Iwai763f3562005-06-03 11:25:34 +02005208 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005209 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005210 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005211
5212 /* set defaults: */
5213
Adrian Knoth0dca1792011-01-26 19:32:14 +01005214 hdspm->settings_register = 0;
5215
5216 switch (hdspm->io_type) {
5217 case MADI:
5218 case MADIface:
5219 hdspm->control_register =
5220 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5221 break;
5222
5223 case RayDAT:
5224 case AIO:
5225 hdspm->settings_register = 0x1 + 0x1000;
5226 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5227 * line_out */
5228 hdspm->control_register =
5229 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5230 break;
5231
5232 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005233 hdspm->control_register =
5234 HDSPM_ClockModeMaster | /* Master Cloack Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005235 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005236 HDSPM_SyncRef0 | /* AES1 is syncclock */
5237 HDSPM_LineOut | /* Analog output in */
5238 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005239 break;
5240 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005241
5242 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5243
Adrian Knoth0dca1792011-01-26 19:32:14 +01005244 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005245 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005246#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005247 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005248#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005249 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005250#endif
5251
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005252 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5253 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005254 hdspm_compute_period_size(hdspm);
5255
5256 /* silence everything */
5257
5258 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5259
Adrian Knothb2ed6322013-07-05 11:27:54 +02005260 if (hdspm_is_raydat_or_aio(hdspm))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005261 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005262
5263 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005264 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005265
5266 return 0;
5267}
5268
5269
5270/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005271 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005272 ------------------------------------------------------------*/
5273
David Howells7d12e782006-10-05 14:55:46 +01005274static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005275{
Takashi Iwai98274f02005-11-17 14:52:34 +01005276 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005277 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005278 int i, audio, midi, schedule = 0;
5279 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005280
5281 status = hdspm_read(hdspm, HDSPM_statusRegister);
5282
5283 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005284 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5285 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005286
Adrian Knoth0dca1792011-01-26 19:32:14 +01005287 /* now = get_cycles(); */
5288 /**
5289 * LAT_2..LAT_0 period counter (win) counter (mac)
5290 * 6 4096 ~256053425 ~514672358
5291 * 5 2048 ~128024983 ~257373821
5292 * 4 1024 ~64023706 ~128718089
5293 * 3 512 ~32005945 ~64385999
5294 * 2 256 ~16003039 ~32260176
5295 * 1 128 ~7998738 ~16194507
5296 * 0 64 ~3998231 ~8191558
5297 **/
5298 /*
5299 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5300 now-hdspm->last_interrupt, status & 0xFFC0);
5301 hdspm->last_interrupt = now;
5302 */
5303
5304 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005305 return IRQ_NONE;
5306
5307 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5308 hdspm->irq_count++;
5309
Takashi Iwai763f3562005-06-03 11:25:34 +02005310
5311 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005312 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005313 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005314
5315 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005316 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005317 }
5318
Adrian Knoth0dca1792011-01-26 19:32:14 +01005319 if (midi) {
5320 i = 0;
5321 while (i < hdspm->midiPorts) {
5322 if ((hdspm_read(hdspm,
5323 hdspm->midi[i].statusIn) & 0xff) &&
5324 (status & hdspm->midi[i].irq)) {
5325 /* we disable interrupts for this input until
5326 * processing is done
5327 */
5328 hdspm->control_register &= ~hdspm->midi[i].ie;
5329 hdspm_write(hdspm, HDSPM_controlRegister,
5330 hdspm->control_register);
5331 hdspm->midi[i].pending = 1;
5332 schedule = 1;
5333 }
5334
5335 i++;
5336 }
5337
5338 if (schedule)
5339 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005340 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005341
Takashi Iwai763f3562005-06-03 11:25:34 +02005342 return IRQ_HANDLED;
5343}
5344
5345/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005346 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005347 ------------------------------------------------------------*/
5348
5349
Adrian Knoth0dca1792011-01-26 19:32:14 +01005350static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5351 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005352{
Takashi Iwai98274f02005-11-17 14:52:34 +01005353 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005354 return hdspm_hw_pointer(hdspm);
5355}
5356
Takashi Iwai763f3562005-06-03 11:25:34 +02005357
Takashi Iwai98274f02005-11-17 14:52:34 +01005358static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005359{
Takashi Iwai98274f02005-11-17 14:52:34 +01005360 struct snd_pcm_runtime *runtime = substream->runtime;
5361 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5362 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005363
5364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5365 other = hdspm->capture_substream;
5366 else
5367 other = hdspm->playback_substream;
5368
5369 if (hdspm->running)
5370 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5371 else
5372 runtime->status->hw_ptr = 0;
5373 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005374 struct snd_pcm_substream *s;
5375 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005376 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005377 if (s == other) {
5378 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005379 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005380 break;
5381 }
5382 }
5383 }
5384 return 0;
5385}
5386
Takashi Iwai98274f02005-11-17 14:52:34 +01005387static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5388 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005389{
Takashi Iwai98274f02005-11-17 14:52:34 +01005390 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005391 int err;
5392 int i;
5393 pid_t this_pid;
5394 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005395
5396 spin_lock_irq(&hdspm->lock);
5397
5398 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5399 this_pid = hdspm->playback_pid;
5400 other_pid = hdspm->capture_pid;
5401 } else {
5402 this_pid = hdspm->capture_pid;
5403 other_pid = hdspm->playback_pid;
5404 }
5405
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005406 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005407
5408 /* The other stream is open, and not by the same
5409 task as this one. Make sure that the parameters
5410 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005411 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005412
5413 if (params_rate(params) != hdspm->system_sample_rate) {
5414 spin_unlock_irq(&hdspm->lock);
5415 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005416 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005417 return -EBUSY;
5418 }
5419
5420 if (params_period_size(params) != hdspm->period_bytes / 4) {
5421 spin_unlock_irq(&hdspm->lock);
5422 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005423 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005424 return -EBUSY;
5425 }
5426
5427 }
5428 /* We're fine. */
5429 spin_unlock_irq(&hdspm->lock);
5430
5431 /* how to make sure that the rate matches an externally-set one ? */
5432
5433 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005434 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5435 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005436 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005437 spin_unlock_irq(&hdspm->lock);
5438 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005439 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005440 return err;
5441 }
5442 spin_unlock_irq(&hdspm->lock);
5443
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005444 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005445 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005446 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005447 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005448 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005449 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005450 return err;
5451 }
5452
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005453 /* Memory allocation, takashi's method, dont know if we should
5454 * spinlock
5455 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005456 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005457 /* Update for MADI rev 204: we need to allocate for all channels,
5458 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005459
Takashi Iwai763f3562005-06-03 11:25:34 +02005460 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005461 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5462 if (err < 0) {
5463 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005464 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005465 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005466
Takashi Iwai763f3562005-06-03 11:25:34 +02005467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5468
Takashi Iwai77a23f22008-08-21 13:00:13 +02005469 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005470 params_channels(params));
5471
5472 for (i = 0; i < params_channels(params); ++i)
5473 snd_hdspm_enable_out(hdspm, i, 1);
5474
5475 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005476 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005477 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005478 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005479 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005480 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005481 params_channels(params));
5482
5483 for (i = 0; i < params_channels(params); ++i)
5484 snd_hdspm_enable_in(hdspm, i, 1);
5485
5486 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005487 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005488 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005489 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005490 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005491
Remy Bruno3cee5a62006-10-16 12:46:32 +02005492 /*
5493 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5494 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5495 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005496 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005497 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005498 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005499 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5500 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5501 "playback" : "capture",
5502 params_rate(params), params_channels(params),
5503 params_buffer_size(params));
5504 */
5505
5506
5507 /* Switch to native float format if requested */
5508 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5509 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5510 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5511
5512 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5513 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5514 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5515 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5516
5517 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5518 }
5519 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5520
Takashi Iwai763f3562005-06-03 11:25:34 +02005521 return 0;
5522}
5523
Takashi Iwai98274f02005-11-17 14:52:34 +01005524static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005525{
5526 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005527 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005528
5529 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5530
Adrian Knoth0dca1792011-01-26 19:32:14 +01005531 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005532 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005533 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005534 snd_hdspm_enable_out(hdspm, i, 0);
5535
5536 hdspm->playback_buffer = NULL;
5537 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005538 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005539 snd_hdspm_enable_in(hdspm, i, 0);
5540
5541 hdspm->capture_buffer = NULL;
5542
5543 }
5544
5545 snd_pcm_lib_free_pages(substream);
5546
5547 return 0;
5548}
5549
Adrian Knoth0dca1792011-01-26 19:32:14 +01005550
Takashi Iwai98274f02005-11-17 14:52:34 +01005551static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005552 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005553{
Takashi Iwai98274f02005-11-17 14:52:34 +01005554 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005555
Adrian Knoth0dca1792011-01-26 19:32:14 +01005556 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5557 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5558 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5559 return -EINVAL;
5560 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005561
Adrian Knoth0dca1792011-01-26 19:32:14 +01005562 if (hdspm->channel_map_out[info->channel] < 0) {
5563 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5564 return -EINVAL;
5565 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005566
Adrian Knoth0dca1792011-01-26 19:32:14 +01005567 info->offset = hdspm->channel_map_out[info->channel] *
5568 HDSPM_CHANNEL_BUFFER_BYTES;
5569 } else {
5570 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5571 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5572 return -EINVAL;
5573 }
5574
5575 if (hdspm->channel_map_in[info->channel] < 0) {
5576 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5577 return -EINVAL;
5578 }
5579
5580 info->offset = hdspm->channel_map_in[info->channel] *
5581 HDSPM_CHANNEL_BUFFER_BYTES;
5582 }
5583
Takashi Iwai763f3562005-06-03 11:25:34 +02005584 info->first = 0;
5585 info->step = 32;
5586 return 0;
5587}
5588
Adrian Knoth0dca1792011-01-26 19:32:14 +01005589
Takashi Iwai98274f02005-11-17 14:52:34 +01005590static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005591 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005592{
5593 switch (cmd) {
5594 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005595 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005596
5597 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005598 {
5599 struct snd_pcm_channel_info *info = arg;
5600 return snd_hdspm_channel_info(substream, info);
5601 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005602 default:
5603 break;
5604 }
5605
5606 return snd_pcm_lib_ioctl(substream, cmd, arg);
5607}
5608
Takashi Iwai98274f02005-11-17 14:52:34 +01005609static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005610{
Takashi Iwai98274f02005-11-17 14:52:34 +01005611 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5612 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005613 int running;
5614
5615 spin_lock(&hdspm->lock);
5616 running = hdspm->running;
5617 switch (cmd) {
5618 case SNDRV_PCM_TRIGGER_START:
5619 running |= 1 << substream->stream;
5620 break;
5621 case SNDRV_PCM_TRIGGER_STOP:
5622 running &= ~(1 << substream->stream);
5623 break;
5624 default:
5625 snd_BUG();
5626 spin_unlock(&hdspm->lock);
5627 return -EINVAL;
5628 }
5629 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5630 other = hdspm->capture_substream;
5631 else
5632 other = hdspm->playback_substream;
5633
5634 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005635 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005636 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005637 if (s == other) {
5638 snd_pcm_trigger_done(s, substream);
5639 if (cmd == SNDRV_PCM_TRIGGER_START)
5640 running |= 1 << s->stream;
5641 else
5642 running &= ~(1 << s->stream);
5643 goto _ok;
5644 }
5645 }
5646 if (cmd == SNDRV_PCM_TRIGGER_START) {
5647 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005648 && substream->stream ==
5649 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005650 hdspm_silence_playback(hdspm);
5651 } else {
5652 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005653 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005654 hdspm_silence_playback(hdspm);
5655 }
5656 } else {
5657 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5658 hdspm_silence_playback(hdspm);
5659 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005660_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005661 snd_pcm_trigger_done(substream, substream);
5662 if (!hdspm->running && running)
5663 hdspm_start_audio(hdspm);
5664 else if (hdspm->running && !running)
5665 hdspm_stop_audio(hdspm);
5666 hdspm->running = running;
5667 spin_unlock(&hdspm->lock);
5668
5669 return 0;
5670}
5671
Takashi Iwai98274f02005-11-17 14:52:34 +01005672static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005673{
5674 return 0;
5675}
5676
Takashi Iwai98274f02005-11-17 14:52:34 +01005677static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005678 .info = (SNDRV_PCM_INFO_MMAP |
5679 SNDRV_PCM_INFO_MMAP_VALID |
5680 SNDRV_PCM_INFO_NONINTERLEAVED |
5681 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5682 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5683 .rates = (SNDRV_PCM_RATE_32000 |
5684 SNDRV_PCM_RATE_44100 |
5685 SNDRV_PCM_RATE_48000 |
5686 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005687 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5688 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005689 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005690 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005691 .channels_min = 1,
5692 .channels_max = HDSPM_MAX_CHANNELS,
5693 .buffer_bytes_max =
5694 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005695 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005696 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005697 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005698 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005699 .fifo_size = 0
5700};
5701
Takashi Iwai98274f02005-11-17 14:52:34 +01005702static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005703 .info = (SNDRV_PCM_INFO_MMAP |
5704 SNDRV_PCM_INFO_MMAP_VALID |
5705 SNDRV_PCM_INFO_NONINTERLEAVED |
5706 SNDRV_PCM_INFO_SYNC_START),
5707 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5708 .rates = (SNDRV_PCM_RATE_32000 |
5709 SNDRV_PCM_RATE_44100 |
5710 SNDRV_PCM_RATE_48000 |
5711 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005712 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5713 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005714 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005715 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005716 .channels_min = 1,
5717 .channels_max = HDSPM_MAX_CHANNELS,
5718 .buffer_bytes_max =
5719 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005720 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005721 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005722 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005723 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005724 .fifo_size = 0
5725};
5726
Adrian Knoth0dca1792011-01-26 19:32:14 +01005727static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5728 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005729{
Takashi Iwai98274f02005-11-17 14:52:34 +01005730 struct hdspm *hdspm = rule->private;
5731 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005732 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005733 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005734 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5735
Adrian Knoth0dca1792011-01-26 19:32:14 +01005736 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005737 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005738 .min = hdspm->qs_in_channels,
5739 .max = hdspm->qs_in_channels,
5740 .integer = 1,
5741 };
5742 return snd_interval_refine(c, &t);
5743 } else if (r->min > 48000 && r->max <= 96000) {
5744 struct snd_interval t = {
5745 .min = hdspm->ds_in_channels,
5746 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005747 .integer = 1,
5748 };
5749 return snd_interval_refine(c, &t);
5750 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005751 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005752 .min = hdspm->ss_in_channels,
5753 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005754 .integer = 1,
5755 };
5756 return snd_interval_refine(c, &t);
5757 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005758
Takashi Iwai763f3562005-06-03 11:25:34 +02005759 return 0;
5760}
5761
Adrian Knoth0dca1792011-01-26 19:32:14 +01005762static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005763 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005764{
Takashi Iwai98274f02005-11-17 14:52:34 +01005765 struct hdspm *hdspm = rule->private;
5766 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005767 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005768 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005769 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5770
Adrian Knoth0dca1792011-01-26 19:32:14 +01005771 if (r->min > 96000 && r->max <= 192000) {
5772 struct snd_interval t = {
5773 .min = hdspm->qs_out_channels,
5774 .max = hdspm->qs_out_channels,
5775 .integer = 1,
5776 };
5777 return snd_interval_refine(c, &t);
5778 } else if (r->min > 48000 && r->max <= 96000) {
5779 struct snd_interval t = {
5780 .min = hdspm->ds_out_channels,
5781 .max = hdspm->ds_out_channels,
5782 .integer = 1,
5783 };
5784 return snd_interval_refine(c, &t);
5785 } else if (r->max < 64000) {
5786 struct snd_interval t = {
5787 .min = hdspm->ss_out_channels,
5788 .max = hdspm->ss_out_channels,
5789 .integer = 1,
5790 };
5791 return snd_interval_refine(c, &t);
5792 } else {
5793 }
5794 return 0;
5795}
5796
5797static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5798 struct snd_pcm_hw_rule * rule)
5799{
5800 struct hdspm *hdspm = rule->private;
5801 struct snd_interval *c =
5802 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5803 struct snd_interval *r =
5804 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5805
5806 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005807 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005808 .min = 32000,
5809 .max = 48000,
5810 .integer = 1,
5811 };
5812 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005813 } else if (c->max <= hdspm->qs_in_channels) {
5814 struct snd_interval t = {
5815 .min = 128000,
5816 .max = 192000,
5817 .integer = 1,
5818 };
5819 return snd_interval_refine(r, &t);
5820 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005821 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005822 .min = 64000,
5823 .max = 96000,
5824 .integer = 1,
5825 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005826 return snd_interval_refine(r, &t);
5827 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005828
5829 return 0;
5830}
5831static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5832 struct snd_pcm_hw_rule *rule)
5833{
5834 struct hdspm *hdspm = rule->private;
5835 struct snd_interval *c =
5836 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5837 struct snd_interval *r =
5838 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5839
5840 if (c->min >= hdspm->ss_out_channels) {
5841 struct snd_interval t = {
5842 .min = 32000,
5843 .max = 48000,
5844 .integer = 1,
5845 };
5846 return snd_interval_refine(r, &t);
5847 } else if (c->max <= hdspm->qs_out_channels) {
5848 struct snd_interval t = {
5849 .min = 128000,
5850 .max = 192000,
5851 .integer = 1,
5852 };
5853 return snd_interval_refine(r, &t);
5854 } else if (c->max <= hdspm->ds_out_channels) {
5855 struct snd_interval t = {
5856 .min = 64000,
5857 .max = 96000,
5858 .integer = 1,
5859 };
5860 return snd_interval_refine(r, &t);
5861 }
5862
Takashi Iwai763f3562005-06-03 11:25:34 +02005863 return 0;
5864}
5865
Adrian Knoth0dca1792011-01-26 19:32:14 +01005866static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005867 struct snd_pcm_hw_rule *rule)
5868{
5869 unsigned int list[3];
5870 struct hdspm *hdspm = rule->private;
5871 struct snd_interval *c = hw_param_interval(params,
5872 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005873
5874 list[0] = hdspm->qs_in_channels;
5875 list[1] = hdspm->ds_in_channels;
5876 list[2] = hdspm->ss_in_channels;
5877 return snd_interval_list(c, 3, list, 0);
5878}
5879
5880static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5881 struct snd_pcm_hw_rule *rule)
5882{
5883 unsigned int list[3];
5884 struct hdspm *hdspm = rule->private;
5885 struct snd_interval *c = hw_param_interval(params,
5886 SNDRV_PCM_HW_PARAM_CHANNELS);
5887
5888 list[0] = hdspm->qs_out_channels;
5889 list[1] = hdspm->ds_out_channels;
5890 list[2] = hdspm->ss_out_channels;
5891 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005892}
5893
5894
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005895static unsigned int hdspm_aes32_sample_rates[] = {
5896 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5897};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005898
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005899static struct snd_pcm_hw_constraint_list
5900hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005901 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5902 .list = hdspm_aes32_sample_rates,
5903 .mask = 0
5904};
5905
Takashi Iwai98274f02005-11-17 14:52:34 +01005906static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005907{
Takashi Iwai98274f02005-11-17 14:52:34 +01005908 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5909 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005910
Takashi Iwai763f3562005-06-03 11:25:34 +02005911 spin_lock_irq(&hdspm->lock);
5912
5913 snd_pcm_set_sync(substream);
5914
Adrian Knoth0dca1792011-01-26 19:32:14 +01005915
Takashi Iwai763f3562005-06-03 11:25:34 +02005916 runtime->hw = snd_hdspm_playback_subinfo;
5917
5918 if (hdspm->capture_substream == NULL)
5919 hdspm_stop_audio(hdspm);
5920
5921 hdspm->playback_pid = current->pid;
5922 hdspm->playback_substream = substream;
5923
5924 spin_unlock_irq(&hdspm->lock);
5925
5926 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005927 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005928
Adrian Knoth0dca1792011-01-26 19:32:14 +01005929 switch (hdspm->io_type) {
5930 case AIO:
5931 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005932 snd_pcm_hw_constraint_minmax(runtime,
5933 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5934 32, 4096);
5935 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
5936 snd_pcm_hw_constraint_minmax(runtime,
5937 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5938 16384, 16384);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005939 break;
5940
5941 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005942 snd_pcm_hw_constraint_minmax(runtime,
5943 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5944 64, 8192);
5945 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005946 }
5947
5948 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02005949 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005950 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5951 &hdspm_hw_constraints_aes32_sample_rates);
5952 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005953 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005954 snd_hdspm_hw_rule_rate_out_channels, hdspm,
5955 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005956 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005957
5958 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5959 snd_hdspm_hw_rule_out_channels, hdspm,
5960 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5961
5962 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5963 snd_hdspm_hw_rule_out_channels_rate, hdspm,
5964 SNDRV_PCM_HW_PARAM_RATE, -1);
5965
Takashi Iwai763f3562005-06-03 11:25:34 +02005966 return 0;
5967}
5968
Takashi Iwai98274f02005-11-17 14:52:34 +01005969static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005970{
Takashi Iwai98274f02005-11-17 14:52:34 +01005971 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005972
5973 spin_lock_irq(&hdspm->lock);
5974
5975 hdspm->playback_pid = -1;
5976 hdspm->playback_substream = NULL;
5977
5978 spin_unlock_irq(&hdspm->lock);
5979
5980 return 0;
5981}
5982
5983
Takashi Iwai98274f02005-11-17 14:52:34 +01005984static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005985{
Takashi Iwai98274f02005-11-17 14:52:34 +01005986 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5987 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005988
5989 spin_lock_irq(&hdspm->lock);
5990 snd_pcm_set_sync(substream);
5991 runtime->hw = snd_hdspm_capture_subinfo;
5992
5993 if (hdspm->playback_substream == NULL)
5994 hdspm_stop_audio(hdspm);
5995
5996 hdspm->capture_pid = current->pid;
5997 hdspm->capture_substream = substream;
5998
5999 spin_unlock_irq(&hdspm->lock);
6000
6001 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02006002 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
6003
Adrian Knoth0dca1792011-01-26 19:32:14 +01006004 switch (hdspm->io_type) {
6005 case AIO:
6006 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02006007 snd_pcm_hw_constraint_minmax(runtime,
6008 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6009 32, 4096);
6010 snd_pcm_hw_constraint_minmax(runtime,
6011 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6012 16384, 16384);
6013 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006014
6015 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02006016 snd_pcm_hw_constraint_minmax(runtime,
6017 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6018 64, 8192);
6019 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006020 }
6021
6022 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02006023 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006024 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6025 &hdspm_hw_constraints_aes32_sample_rates);
6026 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006027 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006028 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6029 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006030 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006031
6032 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6033 snd_hdspm_hw_rule_in_channels, hdspm,
6034 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6035
6036 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6037 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6038 SNDRV_PCM_HW_PARAM_RATE, -1);
6039
Takashi Iwai763f3562005-06-03 11:25:34 +02006040 return 0;
6041}
6042
Takashi Iwai98274f02005-11-17 14:52:34 +01006043static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006044{
Takashi Iwai98274f02005-11-17 14:52:34 +01006045 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006046
6047 spin_lock_irq(&hdspm->lock);
6048
6049 hdspm->capture_pid = -1;
6050 hdspm->capture_substream = NULL;
6051
6052 spin_unlock_irq(&hdspm->lock);
6053 return 0;
6054}
6055
Adrian Knoth0dca1792011-01-26 19:32:14 +01006056static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02006057{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006058 /* we have nothing to initialize but the call is required */
6059 return 0;
6060}
6061
6062static inline int copy_u32_le(void __user *dest, void __iomem *src)
6063{
6064 u32 val = readl(src);
6065 return copy_to_user(dest, &val, 4);
6066}
6067
6068static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006069 unsigned int cmd, unsigned long arg)
Adrian Knoth0dca1792011-01-26 19:32:14 +01006070{
6071 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006072 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01006073 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006074 struct hdspm_config info;
6075 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01006076 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006077 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006078 struct hdspm_ltc ltc;
6079 unsigned int statusregister;
6080 long unsigned int s;
6081 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02006082
6083 switch (cmd) {
6084
Takashi Iwai763f3562005-06-03 11:25:34 +02006085 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006086 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006087 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006088 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006089 readl(hdspm->iobase +
6090 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006091 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006092 readl(hdspm->iobase +
6093 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006094 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006095 readl(hdspm->iobase +
6096 HDSPM_MADI_OUTPUT_PEAK + i*4);
6097
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006098 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006099 ((uint64_t) readl(hdspm->iobase +
6100 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6101 (uint64_t) readl(hdspm->iobase +
6102 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006103 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006104 ((uint64_t)readl(hdspm->iobase +
6105 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6106 (uint64_t)readl(hdspm->iobase +
6107 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006108 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006109 ((uint64_t)readl(hdspm->iobase +
6110 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6111 (uint64_t)readl(hdspm->iobase +
6112 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6113 }
6114
6115 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006116 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006117 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006118 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006119 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006120 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006121 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006122 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006123
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006124 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01006125 if (0 != s) {
6126 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6127 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6128 */
Takashi Iwai763f3562005-06-03 11:25:34 +02006129 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006130 }
6131 break;
6132
6133 case SNDRV_HDSPM_IOCTL_GET_LTC:
6134 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6135 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6136 if (i & HDSPM_TCO1_LTC_Input_valid) {
6137 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6138 HDSPM_TCO1_LTC_Format_MSB)) {
6139 case 0:
6140 ltc.format = fps_24;
6141 break;
6142 case HDSPM_TCO1_LTC_Format_LSB:
6143 ltc.format = fps_25;
6144 break;
6145 case HDSPM_TCO1_LTC_Format_MSB:
6146 ltc.format = fps_2997;
6147 break;
6148 default:
6149 ltc.format = 30;
6150 break;
6151 }
6152 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6153 ltc.frame = drop_frame;
6154 } else {
6155 ltc.frame = full_frame;
6156 }
6157 } else {
6158 ltc.format = format_invalid;
6159 ltc.frame = frame_invalid;
6160 }
6161 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6162 ltc.input_format = ntsc;
6163 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6164 ltc.input_format = pal;
6165 } else {
6166 ltc.input_format = no_video;
6167 }
6168
6169 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6170 if (0 != s) {
6171 /*
6172 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02006173 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006174 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006175
6176 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02006177
Adrian Knoth0dca1792011-01-26 19:32:14 +01006178 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02006179
Adrian Knoth4ab69a22011-02-23 11:43:14 +01006180 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02006181 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006182 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6183 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006184
6185 info.system_sample_rate = hdspm->system_sample_rate;
6186 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006187 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006188 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6189 info.clock_source = hdspm_clock_source(hdspm);
6190 info.autosync_ref = hdspm_autosync_ref(hdspm);
Adrian Knothc9e16682012-12-03 14:55:50 +01006191 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
Takashi Iwai763f3562005-06-03 11:25:34 +02006192 info.passthru = 0;
6193 spin_unlock_irq(&hdspm->lock);
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006194 if (copy_to_user(argp, &info, sizeof(info)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006195 return -EFAULT;
6196 break;
6197
Adrian Knoth0dca1792011-01-26 19:32:14 +01006198 case SNDRV_HDSPM_IOCTL_GET_STATUS:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006199 memset(&status, 0, sizeof(status));
6200
Adrian Knoth0dca1792011-01-26 19:32:14 +01006201 status.card_type = hdspm->io_type;
6202
6203 status.autosync_source = hdspm_autosync_ref(hdspm);
6204
6205 status.card_clock = 110069313433624ULL;
6206 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6207
6208 switch (hdspm->io_type) {
6209 case MADI:
6210 case MADIface:
6211 status.card_specific.madi.sync_wc =
6212 hdspm_wc_sync_check(hdspm);
6213 status.card_specific.madi.sync_madi =
6214 hdspm_madi_sync_check(hdspm);
6215 status.card_specific.madi.sync_tco =
6216 hdspm_tco_sync_check(hdspm);
6217 status.card_specific.madi.sync_in =
6218 hdspm_sync_in_sync_check(hdspm);
6219
6220 statusregister =
6221 hdspm_read(hdspm, HDSPM_statusRegister);
6222 status.card_specific.madi.madi_input =
6223 (statusregister & HDSPM_AB_int) ? 1 : 0;
6224 status.card_specific.madi.channel_format =
Adrian Knoth9e6ff522011-10-27 21:57:52 +02006225 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006226 /* TODO: Mac driver sets it when f_s>48kHz */
6227 status.card_specific.madi.frame_format = 0;
6228
6229 default:
6230 break;
6231 }
6232
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006233 if (copy_to_user(argp, &status, sizeof(status)))
Adrian Knoth0dca1792011-01-26 19:32:14 +01006234 return -EFAULT;
6235
6236
6237 break;
6238
Takashi Iwai763f3562005-06-03 11:25:34 +02006239 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006240 memset(&hdspm_version, 0, sizeof(hdspm_version));
6241
Adrian Knoth0dca1792011-01-26 19:32:14 +01006242 hdspm_version.card_type = hdspm->io_type;
6243 strncpy(hdspm_version.cardname, hdspm->card_name,
6244 sizeof(hdspm_version.cardname));
Adrian Knoth7d53a632012-01-04 14:31:16 +01006245 hdspm_version.serial = hdspm->serial;
Takashi Iwai763f3562005-06-03 11:25:34 +02006246 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006247 hdspm_version.addons = 0;
6248 if (hdspm->tco)
6249 hdspm_version.addons |= HDSPM_ADDON_TCO;
6250
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006251 if (copy_to_user(argp, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006252 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006253 return -EFAULT;
6254 break;
6255
6256 case SNDRV_HDSPM_IOCTL_GET_MIXER:
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006257 if (copy_from_user(&mixer, argp, sizeof(mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006258 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006259 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006260 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006261 return -EFAULT;
6262 break;
6263
6264 default:
6265 return -EINVAL;
6266 }
6267 return 0;
6268}
6269
Takashi Iwai98274f02005-11-17 14:52:34 +01006270static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006271 .open = snd_hdspm_playback_open,
6272 .close = snd_hdspm_playback_release,
6273 .ioctl = snd_hdspm_ioctl,
6274 .hw_params = snd_hdspm_hw_params,
6275 .hw_free = snd_hdspm_hw_free,
6276 .prepare = snd_hdspm_prepare,
6277 .trigger = snd_hdspm_trigger,
6278 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006279 .page = snd_pcm_sgbuf_ops_page,
6280};
6281
Takashi Iwai98274f02005-11-17 14:52:34 +01006282static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006283 .open = snd_hdspm_capture_open,
6284 .close = snd_hdspm_capture_release,
6285 .ioctl = snd_hdspm_ioctl,
6286 .hw_params = snd_hdspm_hw_params,
6287 .hw_free = snd_hdspm_hw_free,
6288 .prepare = snd_hdspm_prepare,
6289 .trigger = snd_hdspm_trigger,
6290 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006291 .page = snd_pcm_sgbuf_ops_page,
6292};
6293
Bill Pembertone23e7a12012-12-06 12:35:10 -05006294static int snd_hdspm_create_hwdep(struct snd_card *card,
6295 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006296{
Takashi Iwai98274f02005-11-17 14:52:34 +01006297 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006298 int err;
6299
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006300 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6301 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006302 return err;
6303
6304 hdspm->hwdep = hw;
6305 hw->private_data = hdspm;
6306 strcpy(hw->name, "HDSPM hwdep interface");
6307
Adrian Knoth0dca1792011-01-26 19:32:14 +01006308 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006309 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth8de5d6f2012-03-08 15:38:04 +01006310 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006311 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006312
6313 return 0;
6314}
6315
6316
6317/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006318 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006319 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05006320static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006321{
6322 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006323 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006324 size_t wanted;
6325
6326 pcm = hdspm->pcm;
6327
Remy Bruno3cee5a62006-10-16 12:46:32 +02006328 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006329
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006330 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006331 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006332 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006333 snd_dma_pci_data(hdspm->pci),
6334 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006335 wanted);
6336 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006337 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006338
6339 return err;
6340 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006341 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006342
6343 return 0;
6344}
6345
Adrian Knoth0dca1792011-01-26 19:32:14 +01006346
6347static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006348 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006349 unsigned int reg, int channels)
6350{
6351 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006352
6353 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006354 for (i = 0; i < (channels * 16); i++)
6355 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006356 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006357}
6358
Adrian Knoth0dca1792011-01-26 19:32:14 +01006359
Takashi Iwai763f3562005-06-03 11:25:34 +02006360/* ------------- ALSA Devices ---------------------------- */
Bill Pembertone23e7a12012-12-06 12:35:10 -05006361static int snd_hdspm_create_pcm(struct snd_card *card,
6362 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006363{
Takashi Iwai98274f02005-11-17 14:52:34 +01006364 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006365 int err;
6366
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006367 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6368 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006369 return err;
6370
6371 hdspm->pcm = pcm;
6372 pcm->private_data = hdspm;
6373 strcpy(pcm->name, hdspm->card_name);
6374
6375 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6376 &snd_hdspm_playback_ops);
6377 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6378 &snd_hdspm_capture_ops);
6379
6380 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6381
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006382 err = snd_hdspm_preallocate_memory(hdspm);
6383 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006384 return err;
6385
6386 return 0;
6387}
6388
Takashi Iwai98274f02005-11-17 14:52:34 +01006389static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006390{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006391 int i;
6392
6393 for (i = 0; i < hdspm->midiPorts; i++)
6394 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006395}
6396
Bill Pembertone23e7a12012-12-06 12:35:10 -05006397static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6398 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006399{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006400 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006401
6402 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006403 err = snd_hdspm_create_pcm(card, hdspm);
6404 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006405 return err;
6406
Adrian Knoth0dca1792011-01-26 19:32:14 +01006407 i = 0;
6408 while (i < hdspm->midiPorts) {
6409 err = snd_hdspm_create_midi(card, hdspm, i);
6410 if (err < 0) {
6411 return err;
6412 }
6413 i++;
6414 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006415
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006416 err = snd_hdspm_create_controls(card, hdspm);
6417 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006418 return err;
6419
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006420 err = snd_hdspm_create_hwdep(card, hdspm);
6421 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006422 return err;
6423
6424 snd_printdd("proc init...\n");
6425 snd_hdspm_proc_init(hdspm);
6426
6427 hdspm->system_sample_rate = -1;
6428 hdspm->last_external_sample_rate = -1;
6429 hdspm->last_internal_sample_rate = -1;
6430 hdspm->playback_pid = -1;
6431 hdspm->capture_pid = -1;
6432 hdspm->capture_substream = NULL;
6433 hdspm->playback_substream = NULL;
6434
6435 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006436 err = snd_hdspm_set_defaults(hdspm);
6437 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006438 return err;
6439
6440 snd_printdd("Update mixer controls...\n");
6441 hdspm_update_simple_mixer_controls(hdspm);
6442
6443 snd_printdd("Initializeing complete ???\n");
6444
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006445 err = snd_card_register(card);
6446 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006447 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6448 return err;
6449 }
6450
6451 snd_printdd("... yes now\n");
6452
6453 return 0;
6454}
6455
Bill Pembertone23e7a12012-12-06 12:35:10 -05006456static int snd_hdspm_create(struct snd_card *card,
6457 struct hdspm *hdspm)
6458{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006459
Takashi Iwai763f3562005-06-03 11:25:34 +02006460 struct pci_dev *pci = hdspm->pci;
6461 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006462 unsigned long io_extent;
6463
6464 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006465 hdspm->card = card;
6466
6467 spin_lock_init(&hdspm->lock);
6468
Takashi Iwai763f3562005-06-03 11:25:34 +02006469 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006470 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006471
Takashi Iwai763f3562005-06-03 11:25:34 +02006472 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006473 strcpy(card->driver, "HDSPM");
6474
6475 switch (hdspm->firmware_rev) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01006476 case HDSPM_RAYDAT_REV:
6477 hdspm->io_type = RayDAT;
6478 hdspm->card_name = "RME RayDAT";
6479 hdspm->midiPorts = 2;
6480 break;
6481 case HDSPM_AIO_REV:
6482 hdspm->io_type = AIO;
6483 hdspm->card_name = "RME AIO";
6484 hdspm->midiPorts = 1;
6485 break;
6486 case HDSPM_MADIFACE_REV:
6487 hdspm->io_type = MADIface;
6488 hdspm->card_name = "RME MADIface";
6489 hdspm->midiPorts = 1;
6490 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006491 default:
Adrian Knothc09403d2011-10-27 21:57:54 +02006492 if ((hdspm->firmware_rev == 0xf0) ||
6493 ((hdspm->firmware_rev >= 0xe6) &&
6494 (hdspm->firmware_rev <= 0xea))) {
6495 hdspm->io_type = AES32;
6496 hdspm->card_name = "RME AES32";
6497 hdspm->midiPorts = 2;
Adrian Knoth05c7cc92011-11-21 16:15:36 +01006498 } else if ((hdspm->firmware_rev == 0xd2) ||
Adrian Knothc09403d2011-10-27 21:57:54 +02006499 ((hdspm->firmware_rev >= 0xc8) &&
6500 (hdspm->firmware_rev <= 0xcf))) {
6501 hdspm->io_type = MADI;
6502 hdspm->card_name = "RME MADI";
6503 hdspm->midiPorts = 3;
6504 } else {
6505 snd_printk(KERN_ERR
6506 "HDSPM: unknown firmware revision %x\n",
Adrian Knoth5027f342011-02-28 15:14:49 +01006507 hdspm->firmware_rev);
Adrian Knothc09403d2011-10-27 21:57:54 +02006508 return -ENODEV;
6509 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02006510 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006511
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006512 err = pci_enable_device(pci);
6513 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006514 return err;
6515
6516 pci_set_master(hdspm->pci);
6517
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006518 err = pci_request_regions(pci, "hdspm");
6519 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006520 return err;
6521
6522 hdspm->port = pci_resource_start(pci, 0);
6523 io_extent = pci_resource_len(pci, 0);
6524
6525 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006526 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006527
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006528 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6529 if (!hdspm->iobase) {
6530 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006531 "unable to remap region 0x%lx-0x%lx\n",
6532 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006533 return -EBUSY;
6534 }
6535 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006536 (unsigned long)hdspm->iobase, hdspm->port,
6537 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006538
6539 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006540 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006541 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6542 return -EBUSY;
6543 }
6544
6545 snd_printdd("use IRQ %d\n", pci->irq);
6546
6547 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006548
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006549 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006550 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006551 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6552 if (!hdspm->mixer) {
6553 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006554 "unable to kmalloc Mixer memory of %d Bytes\n",
6555 (int)sizeof(struct hdspm_mixer));
Julia Lawallb17cbdd2012-08-19 09:02:54 +02006556 return -ENOMEM;
Takashi Iwai763f3562005-06-03 11:25:34 +02006557 }
6558
Adrian Knoth0dca1792011-01-26 19:32:14 +01006559 hdspm->port_names_in = NULL;
6560 hdspm->port_names_out = NULL;
6561
6562 switch (hdspm->io_type) {
6563 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006564 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6565 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6566 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006567
6568 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6569 channel_map_aes32;
6570 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6571 channel_map_aes32;
6572 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6573 channel_map_aes32;
6574 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6575 texts_ports_aes32;
6576 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6577 texts_ports_aes32;
6578 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6579 texts_ports_aes32;
6580
Adrian Knothd2d10a22011-02-28 15:14:47 +01006581 hdspm->max_channels_out = hdspm->max_channels_in =
6582 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006583 hdspm->port_names_in = hdspm->port_names_out =
6584 texts_ports_aes32;
6585 hdspm->channel_map_in = hdspm->channel_map_out =
6586 channel_map_aes32;
6587
Adrian Knoth0dca1792011-01-26 19:32:14 +01006588 break;
6589
6590 case MADI:
6591 case MADIface:
6592 hdspm->ss_in_channels = hdspm->ss_out_channels =
6593 MADI_SS_CHANNELS;
6594 hdspm->ds_in_channels = hdspm->ds_out_channels =
6595 MADI_DS_CHANNELS;
6596 hdspm->qs_in_channels = hdspm->qs_out_channels =
6597 MADI_QS_CHANNELS;
6598
6599 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6600 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006601 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006602 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006603 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006604 channel_map_unity_ss;
6605
6606 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6607 texts_ports_madi;
6608 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6609 texts_ports_madi;
6610 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6611 texts_ports_madi;
6612 break;
6613
6614 case AIO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006615 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6616 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6617 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6618 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6619 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6620 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6621
Adrian Knoth3de9db22013-07-05 11:28:02 +02006622 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6623 snd_printk(KERN_INFO "HDSPM: AEB input board found\n");
6624 hdspm->ss_in_channels += 4;
6625 hdspm->ds_in_channels += 4;
6626 hdspm->qs_in_channels += 4;
6627 }
6628
6629 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6630 snd_printk(KERN_INFO "HDSPM: AEB output board found\n");
6631 hdspm->ss_out_channels += 4;
6632 hdspm->ds_out_channels += 4;
6633 hdspm->qs_out_channels += 4;
6634 }
6635
Adrian Knoth0dca1792011-01-26 19:32:14 +01006636 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6637 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6638 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6639
6640 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6641 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6642 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6643
6644 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6645 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6646 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6647 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6648 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6649 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6650
6651 break;
6652
6653 case RayDAT:
6654 hdspm->ss_in_channels = hdspm->ss_out_channels =
6655 RAYDAT_SS_CHANNELS;
6656 hdspm->ds_in_channels = hdspm->ds_out_channels =
6657 RAYDAT_DS_CHANNELS;
6658 hdspm->qs_in_channels = hdspm->qs_out_channels =
6659 RAYDAT_QS_CHANNELS;
6660
6661 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6662 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6663
6664 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6665 channel_map_raydat_ss;
6666 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6667 channel_map_raydat_ds;
6668 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6669 channel_map_raydat_qs;
6670 hdspm->channel_map_in = hdspm->channel_map_out =
6671 channel_map_raydat_ss;
6672
6673 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6674 texts_ports_raydat_ss;
6675 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6676 texts_ports_raydat_ds;
6677 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6678 texts_ports_raydat_qs;
6679
6680
6681 break;
6682
6683 }
6684
6685 /* TCO detection */
6686 switch (hdspm->io_type) {
6687 case AIO:
6688 case RayDAT:
6689 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6690 HDSPM_s2_tco_detect) {
6691 hdspm->midiPorts++;
6692 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6693 GFP_KERNEL);
6694 if (NULL != hdspm->tco) {
6695 hdspm_tco_write(hdspm);
6696 }
6697 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6698 } else {
6699 hdspm->tco = NULL;
6700 }
6701 break;
6702
6703 case MADI:
6704 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6705 hdspm->midiPorts++;
6706 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6707 GFP_KERNEL);
6708 if (NULL != hdspm->tco) {
6709 hdspm_tco_write(hdspm);
6710 }
6711 snd_printk(KERN_INFO "HDSPM: MADI TCO module found\n");
6712 } else {
6713 hdspm->tco = NULL;
6714 }
6715 break;
6716
6717 default:
6718 hdspm->tco = NULL;
6719 }
6720
6721 /* texts */
6722 switch (hdspm->io_type) {
6723 case AES32:
6724 if (hdspm->tco) {
6725 hdspm->texts_autosync = texts_autosync_aes_tco;
6726 hdspm->texts_autosync_items = 10;
6727 } else {
6728 hdspm->texts_autosync = texts_autosync_aes;
6729 hdspm->texts_autosync_items = 9;
6730 }
6731 break;
6732
6733 case MADI:
6734 if (hdspm->tco) {
6735 hdspm->texts_autosync = texts_autosync_madi_tco;
6736 hdspm->texts_autosync_items = 4;
6737 } else {
6738 hdspm->texts_autosync = texts_autosync_madi;
6739 hdspm->texts_autosync_items = 3;
6740 }
6741 break;
6742
6743 case MADIface:
6744
6745 break;
6746
6747 case RayDAT:
6748 if (hdspm->tco) {
6749 hdspm->texts_autosync = texts_autosync_raydat_tco;
6750 hdspm->texts_autosync_items = 9;
6751 } else {
6752 hdspm->texts_autosync = texts_autosync_raydat;
6753 hdspm->texts_autosync_items = 8;
6754 }
6755 break;
6756
6757 case AIO:
6758 if (hdspm->tco) {
6759 hdspm->texts_autosync = texts_autosync_aio_tco;
6760 hdspm->texts_autosync_items = 6;
6761 } else {
6762 hdspm->texts_autosync = texts_autosync_aio;
6763 hdspm->texts_autosync_items = 5;
6764 }
6765 break;
6766
6767 }
6768
6769 tasklet_init(&hdspm->midi_tasklet,
6770 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006771
Adrian Knothf7de8ba2012-01-10 20:58:40 +01006772
6773 if (hdspm->io_type != MADIface) {
6774 hdspm->serial = (hdspm_read(hdspm,
6775 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6776 /* id contains either a user-provided value or the default
6777 * NULL. If it's the default, we're safe to
6778 * fill card->id with the serial number.
6779 *
6780 * If the serial number is 0xFFFFFF, then we're dealing with
6781 * an old PCI revision that comes without a sane number. In
6782 * this case, we don't set card->id to avoid collisions
6783 * when running with multiple cards.
6784 */
6785 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6786 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6787 snd_card_set_id(card, card->id);
6788 }
6789 }
6790
Takashi Iwai763f3562005-06-03 11:25:34 +02006791 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006792 err = snd_hdspm_create_alsa_devices(card, hdspm);
6793 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006794 return err;
6795
6796 snd_hdspm_initialize_midi_flush(hdspm);
6797
6798 return 0;
6799}
6800
Adrian Knoth0dca1792011-01-26 19:32:14 +01006801
Takashi Iwai98274f02005-11-17 14:52:34 +01006802static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006803{
6804
6805 if (hdspm->port) {
6806
6807 /* stop th audio, and cancel all interrupts */
6808 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006809 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006810 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6811 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006812 hdspm_write(hdspm, HDSPM_controlRegister,
6813 hdspm->control_register);
6814 }
6815
6816 if (hdspm->irq >= 0)
6817 free_irq(hdspm->irq, (void *) hdspm);
6818
Jesper Juhlfc584222005-10-24 15:11:28 +02006819 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006820
6821 if (hdspm->iobase)
6822 iounmap(hdspm->iobase);
6823
Takashi Iwai763f3562005-06-03 11:25:34 +02006824 if (hdspm->port)
6825 pci_release_regions(hdspm->pci);
6826
6827 pci_disable_device(hdspm->pci);
6828 return 0;
6829}
6830
Adrian Knoth0dca1792011-01-26 19:32:14 +01006831
Takashi Iwai98274f02005-11-17 14:52:34 +01006832static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006833{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006834 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006835
6836 if (hdspm)
6837 snd_hdspm_free(hdspm);
6838}
6839
Adrian Knoth0dca1792011-01-26 19:32:14 +01006840
Bill Pembertone23e7a12012-12-06 12:35:10 -05006841static int snd_hdspm_probe(struct pci_dev *pci,
6842 const struct pci_device_id *pci_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02006843{
6844 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006845 struct hdspm *hdspm;
6846 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006847 int err;
6848
6849 if (dev >= SNDRV_CARDS)
6850 return -ENODEV;
6851 if (!enable[dev]) {
6852 dev++;
6853 return -ENOENT;
6854 }
6855
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006856 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006857 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006858 if (err < 0)
6859 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006860
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006861 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006862 card->private_free = snd_hdspm_card_free;
6863 hdspm->dev = dev;
6864 hdspm->pci = pci;
6865
Takashi Iwaic187c042007-02-19 15:27:33 +01006866 snd_card_set_dev(card, &pci->dev);
6867
Adrian Knoth0dca1792011-01-26 19:32:14 +01006868 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006869 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006870 snd_card_free(card);
6871 return err;
6872 }
6873
Adrian Knoth0dca1792011-01-26 19:32:14 +01006874 if (hdspm->io_type != MADIface) {
6875 sprintf(card->shortname, "%s_%x",
6876 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006877 hdspm->serial);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006878 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6879 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006880 hdspm->serial,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006881 hdspm->port, hdspm->irq);
6882 } else {
6883 sprintf(card->shortname, "%s", hdspm->card_name);
6884 sprintf(card->longname, "%s at 0x%lx, irq %d",
6885 hdspm->card_name, hdspm->port, hdspm->irq);
6886 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006887
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006888 err = snd_card_register(card);
6889 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006890 snd_card_free(card);
6891 return err;
6892 }
6893
6894 pci_set_drvdata(pci, card);
6895
6896 dev++;
6897 return 0;
6898}
6899
Bill Pembertone23e7a12012-12-06 12:35:10 -05006900static void snd_hdspm_remove(struct pci_dev *pci)
Takashi Iwai763f3562005-06-03 11:25:34 +02006901{
6902 snd_card_free(pci_get_drvdata(pci));
Takashi Iwai763f3562005-06-03 11:25:34 +02006903}
6904
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006905static struct pci_driver hdspm_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006906 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006907 .id_table = snd_hdspm_ids,
6908 .probe = snd_hdspm_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05006909 .remove = snd_hdspm_remove,
Takashi Iwai763f3562005-06-03 11:25:34 +02006910};
6911
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006912module_pci_driver(hdspm_driver);