blob: 367dd411247d3ec09d1634878b13889d36a1abc4 [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
Adrian Knothb0bf5502013-07-05 11:28:05 +0200363#define HDSPM_tcoLockMadi 0x00000020 /* Optional TCO locked status for HDSPe MADI*/
364#define HDSPM_tcoSync 0x10000000 /* Optional TCO sync status for HDSPe MADI and AES32!*/
Adrian Knoth0dca1792011-01-26 19:32:14 +0100365
Adrian Knothb0bf5502013-07-05 11:28:05 +0200366#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
Adrian Knothb0bf5502013-07-05 11:28:05 +0200385#define HDSPM_tcoLockAes 0x20000000 /* Optional TCO locked status for HDSPe AES */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100386
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
Adrian Knothb0bf5502013-07-05 11:28:05 +0200483#define HDSPM_AES32_AUTOSYNC_FROM_TCO 9
484#define HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN 10
485#define HDSPM_AES32_AUTOSYNC_FROM_NONE 11
Remy Bruno3cee5a62006-10-16 12:46:32 +0200486
487/* status2 */
488/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
489#define HDSPM_LockAES 0x80
490#define HDSPM_LockAES1 0x80
491#define HDSPM_LockAES2 0x40
492#define HDSPM_LockAES3 0x20
493#define HDSPM_LockAES4 0x10
494#define HDSPM_LockAES5 0x8
495#define HDSPM_LockAES6 0x4
496#define HDSPM_LockAES7 0x2
497#define HDSPM_LockAES8 0x1
498/*
499 Timecode
500 After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
501 AES i+1
502 bits 3210
503 0001 32kHz
504 0010 44.1kHz
505 0011 48kHz
506 0100 64kHz
507 0101 88.2kHz
508 0110 96kHz
509 0111 128kHz
510 1000 176.4kHz
511 1001 192kHz
512 NB: Timecode register doesn't seem to work on AES32 card revision 230
513*/
514
Takashi Iwai763f3562005-06-03 11:25:34 +0200515/* Mixer Values */
516#define UNITY_GAIN 32768 /* = 65536/2 */
517#define MINUS_INFINITY_GAIN 0
518
Takashi Iwai763f3562005-06-03 11:25:34 +0200519/* Number of channels for different Speed Modes */
520#define MADI_SS_CHANNELS 64
521#define MADI_DS_CHANNELS 32
522#define MADI_QS_CHANNELS 16
523
Adrian Knoth0dca1792011-01-26 19:32:14 +0100524#define RAYDAT_SS_CHANNELS 36
525#define RAYDAT_DS_CHANNELS 20
526#define RAYDAT_QS_CHANNELS 12
527
528#define AIO_IN_SS_CHANNELS 14
529#define AIO_IN_DS_CHANNELS 10
530#define AIO_IN_QS_CHANNELS 8
531#define AIO_OUT_SS_CHANNELS 16
532#define AIO_OUT_DS_CHANNELS 12
533#define AIO_OUT_QS_CHANNELS 10
534
Adrian Knothd2d10a22011-02-28 15:14:47 +0100535#define AES32_CHANNELS 16
536
Takashi Iwai763f3562005-06-03 11:25:34 +0200537/* the size of a substream (1 mono data stream) */
538#define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024)
539#define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_CHANNEL_BUFFER_SAMPLES)
540
541/* the size of the area we need to allocate for DMA transfers. the
542 size is the same regardless of the number of channels, and
Adrian Knoth0dca1792011-01-26 19:32:14 +0100543 also the latency to use.
Takashi Iwai763f3562005-06-03 11:25:34 +0200544 for one direction !!!
545*/
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100546#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
Takashi Iwai763f3562005-06-03 11:25:34 +0200547#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
548
Adrian Knoth0dca1792011-01-26 19:32:14 +0100549#define HDSPM_RAYDAT_REV 211
550#define HDSPM_AIO_REV 212
551#define HDSPM_MADIFACE_REV 213
Remy Bruno3cee5a62006-10-16 12:46:32 +0200552
Remy Bruno65345992007-08-31 12:21:08 +0200553/* speed factor modes */
554#define HDSPM_SPEED_SINGLE 0
555#define HDSPM_SPEED_DOUBLE 1
556#define HDSPM_SPEED_QUAD 2
Adrian Knoth0dca1792011-01-26 19:32:14 +0100557
Remy Bruno65345992007-08-31 12:21:08 +0200558/* names for speed modes */
559static char *hdspm_speed_names[] = { "single", "double", "quad" };
560
Adrian Knoth0dca1792011-01-26 19:32:14 +0100561static char *texts_autosync_aes_tco[] = { "Word Clock",
562 "AES1", "AES2", "AES3", "AES4",
563 "AES5", "AES6", "AES7", "AES8",
Adrian Knothdb2d1a92013-07-05 11:28:08 +0200564 "TCO", "Sync In"
565};
Adrian Knoth0dca1792011-01-26 19:32:14 +0100566static char *texts_autosync_aes[] = { "Word Clock",
567 "AES1", "AES2", "AES3", "AES4",
Adrian Knothdb2d1a92013-07-05 11:28:08 +0200568 "AES5", "AES6", "AES7", "AES8",
569 "Sync In"
570};
Adrian Knoth0dca1792011-01-26 19:32:14 +0100571static char *texts_autosync_madi_tco[] = { "Word Clock",
572 "MADI", "TCO", "Sync In" };
573static char *texts_autosync_madi[] = { "Word Clock",
574 "MADI", "Sync In" };
575
576static char *texts_autosync_raydat_tco[] = {
577 "Word Clock",
578 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
579 "AES", "SPDIF", "TCO", "Sync In"
580};
581static char *texts_autosync_raydat[] = {
582 "Word Clock",
583 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
584 "AES", "SPDIF", "Sync In"
585};
586static char *texts_autosync_aio_tco[] = {
587 "Word Clock",
588 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
589};
590static char *texts_autosync_aio[] = { "Word Clock",
591 "ADAT", "AES", "SPDIF", "Sync In" };
592
593static char *texts_freq[] = {
594 "No Lock",
595 "32 kHz",
596 "44.1 kHz",
597 "48 kHz",
598 "64 kHz",
599 "88.2 kHz",
600 "96 kHz",
601 "128 kHz",
602 "176.4 kHz",
603 "192 kHz"
604};
605
Adrian Knoth0dca1792011-01-26 19:32:14 +0100606static char *texts_ports_madi[] = {
607 "MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
608 "MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
609 "MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
610 "MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
611 "MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
612 "MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
613 "MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
614 "MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
615 "MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
616 "MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
617 "MADI.61", "MADI.62", "MADI.63", "MADI.64",
618};
619
620
621static char *texts_ports_raydat_ss[] = {
622 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
623 "ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
624 "ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
625 "ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
626 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
627 "ADAT4.7", "ADAT4.8",
628 "AES.L", "AES.R",
629 "SPDIF.L", "SPDIF.R"
630};
631
632static char *texts_ports_raydat_ds[] = {
633 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
634 "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
635 "ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
636 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
637 "AES.L", "AES.R",
638 "SPDIF.L", "SPDIF.R"
639};
640
641static char *texts_ports_raydat_qs[] = {
642 "ADAT1.1", "ADAT1.2",
643 "ADAT2.1", "ADAT2.2",
644 "ADAT3.1", "ADAT3.2",
645 "ADAT4.1", "ADAT4.2",
646 "AES.L", "AES.R",
647 "SPDIF.L", "SPDIF.R"
648};
649
650
651static char *texts_ports_aio_in_ss[] = {
652 "Analogue.L", "Analogue.R",
653 "AES.L", "AES.R",
654 "SPDIF.L", "SPDIF.R",
655 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200656 "ADAT.7", "ADAT.8",
657 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100658};
659
660static char *texts_ports_aio_out_ss[] = {
661 "Analogue.L", "Analogue.R",
662 "AES.L", "AES.R",
663 "SPDIF.L", "SPDIF.R",
664 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
665 "ADAT.7", "ADAT.8",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200666 "Phone.L", "Phone.R",
667 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100668};
669
670static char *texts_ports_aio_in_ds[] = {
671 "Analogue.L", "Analogue.R",
672 "AES.L", "AES.R",
673 "SPDIF.L", "SPDIF.R",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200674 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
675 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100676};
677
678static char *texts_ports_aio_out_ds[] = {
679 "Analogue.L", "Analogue.R",
680 "AES.L", "AES.R",
681 "SPDIF.L", "SPDIF.R",
682 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200683 "Phone.L", "Phone.R",
684 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100685};
686
687static char *texts_ports_aio_in_qs[] = {
688 "Analogue.L", "Analogue.R",
689 "AES.L", "AES.R",
690 "SPDIF.L", "SPDIF.R",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200691 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
692 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100693};
694
695static char *texts_ports_aio_out_qs[] = {
696 "Analogue.L", "Analogue.R",
697 "AES.L", "AES.R",
698 "SPDIF.L", "SPDIF.R",
699 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
Adrian Knoth3de9db22013-07-05 11:28:02 +0200700 "Phone.L", "Phone.R",
701 "AEB.1", "AEB.2", "AEB.3", "AEB.4"
Adrian Knoth0dca1792011-01-26 19:32:14 +0100702};
703
Adrian Knoth432d2502011-02-23 11:43:08 +0100704static char *texts_ports_aes32[] = {
705 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
706 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
707 "AES.15", "AES.16"
708};
709
Adrian Knoth55a57602011-01-27 11:23:15 +0100710/* These tables map the ALSA channels 1..N to the channels that we
711 need to use in order to find the relevant channel buffer. RME
712 refers to this kind of mapping as between "the ADAT channel and
713 the DMA channel." We index it using the logical audio channel,
714 and the value is the DMA channel (i.e. channel buffer number)
715 where the data for that channel can be read/written from/to.
716*/
717
718static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
719 0, 1, 2, 3, 4, 5, 6, 7,
720 8, 9, 10, 11, 12, 13, 14, 15,
721 16, 17, 18, 19, 20, 21, 22, 23,
722 24, 25, 26, 27, 28, 29, 30, 31,
723 32, 33, 34, 35, 36, 37, 38, 39,
724 40, 41, 42, 43, 44, 45, 46, 47,
725 48, 49, 50, 51, 52, 53, 54, 55,
726 56, 57, 58, 59, 60, 61, 62, 63
727};
728
Adrian Knoth55a57602011-01-27 11:23:15 +0100729static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
730 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
731 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
732 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
733 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
734 0, 1, /* AES */
735 2, 3, /* SPDIF */
736 -1, -1, -1, -1,
737 -1, -1, -1, -1, -1, -1, -1, -1,
738 -1, -1, -1, -1, -1, -1, -1, -1,
739 -1, -1, -1, -1, -1, -1, -1, -1,
740};
741
742static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
743 4, 5, 6, 7, /* ADAT 1 */
744 8, 9, 10, 11, /* ADAT 2 */
745 12, 13, 14, 15, /* ADAT 3 */
746 16, 17, 18, 19, /* ADAT 4 */
747 0, 1, /* AES */
748 2, 3, /* SPDIF */
749 -1, -1, -1, -1,
750 -1, -1, -1, -1, -1, -1, -1, -1,
751 -1, -1, -1, -1, -1, -1, -1, -1,
752 -1, -1, -1, -1, -1, -1, -1, -1,
753 -1, -1, -1, -1, -1, -1, -1, -1,
754 -1, -1, -1, -1, -1, -1, -1, -1,
755};
756
757static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
758 4, 5, /* ADAT 1 */
759 6, 7, /* ADAT 2 */
760 8, 9, /* ADAT 3 */
761 10, 11, /* ADAT 4 */
762 0, 1, /* AES */
763 2, 3, /* SPDIF */
764 -1, -1, -1, -1,
765 -1, -1, -1, -1, -1, -1, -1, -1,
766 -1, -1, -1, -1, -1, -1, -1, -1,
767 -1, -1, -1, -1, -1, -1, -1, -1,
768 -1, -1, -1, -1, -1, -1, -1, -1,
769 -1, -1, -1, -1, -1, -1, -1, -1,
770 -1, -1, -1, -1, -1, -1, -1, -1,
771};
772
773static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
774 0, 1, /* line in */
775 8, 9, /* aes in, */
776 10, 11, /* spdif in */
777 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200778 2, 3, 4, 5, /* AEB */
779 -1, -1, -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100780 -1, -1, -1, -1, -1, -1, -1, -1,
781 -1, -1, -1, -1, -1, -1, -1, -1,
782 -1, -1, -1, -1, -1, -1, -1, -1,
783 -1, -1, -1, -1, -1, -1, -1, -1,
784 -1, -1, -1, -1, -1, -1, -1, -1,
785};
786
787static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
788 0, 1, /* line out */
789 8, 9, /* aes out */
790 10, 11, /* spdif out */
791 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
792 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200793 2, 3, 4, 5, /* AEB */
794 -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100795 -1, -1, -1, -1, -1, -1, -1, -1,
796 -1, -1, -1, -1, -1, -1, -1, -1,
797 -1, -1, -1, -1, -1, -1, -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1,
799 -1, -1, -1, -1, -1, -1, -1, -1,
800};
801
802static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
803 0, 1, /* line in */
804 8, 9, /* aes in */
805 10, 11, /* spdif in */
806 12, 14, 16, 18, /* adat in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200807 2, 3, 4, 5, /* AEB */
808 -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100809 -1, -1, -1, -1, -1, -1, -1, -1,
810 -1, -1, -1, -1, -1, -1, -1, -1,
811 -1, -1, -1, -1, -1, -1, -1, -1,
812 -1, -1, -1, -1, -1, -1, -1, -1,
813 -1, -1, -1, -1, -1, -1, -1, -1,
814 -1, -1, -1, -1, -1, -1, -1, -1
815};
816
817static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
818 0, 1, /* line out */
819 8, 9, /* aes out */
820 10, 11, /* spdif out */
821 12, 14, 16, 18, /* adat out */
822 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200823 2, 3, 4, 5, /* AEB */
Adrian Knoth55a57602011-01-27 11:23:15 +0100824 -1, -1, -1, -1, -1, -1, -1, -1,
825 -1, -1, -1, -1, -1, -1, -1, -1,
826 -1, -1, -1, -1, -1, -1, -1, -1,
827 -1, -1, -1, -1, -1, -1, -1, -1,
828 -1, -1, -1, -1, -1, -1, -1, -1,
829 -1, -1, -1, -1, -1, -1, -1, -1
830};
831
832static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
833 0, 1, /* line in */
834 8, 9, /* aes in */
835 10, 11, /* spdif in */
836 12, 16, /* adat in */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200837 2, 3, 4, 5, /* AEB */
838 -1, -1, -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100839 -1, -1, -1, -1, -1, -1, -1, -1,
840 -1, -1, -1, -1, -1, -1, -1, -1,
841 -1, -1, -1, -1, -1, -1, -1, -1,
842 -1, -1, -1, -1, -1, -1, -1, -1,
843 -1, -1, -1, -1, -1, -1, -1, -1,
844 -1, -1, -1, -1, -1, -1, -1, -1
845};
846
847static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
848 0, 1, /* line out */
849 8, 9, /* aes out */
850 10, 11, /* spdif out */
851 12, 16, /* adat out */
852 6, 7, /* phone out */
Adrian Knoth3de9db22013-07-05 11:28:02 +0200853 2, 3, 4, 5, /* AEB */
854 -1, -1,
Adrian Knoth55a57602011-01-27 11:23:15 +0100855 -1, -1, -1, -1, -1, -1, -1, -1,
856 -1, -1, -1, -1, -1, -1, -1, -1,
857 -1, -1, -1, -1, -1, -1, -1, -1,
858 -1, -1, -1, -1, -1, -1, -1, -1,
859 -1, -1, -1, -1, -1, -1, -1, -1,
860 -1, -1, -1, -1, -1, -1, -1, -1
861};
862
Adrian Knoth432d2502011-02-23 11:43:08 +0100863static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
864 0, 1, 2, 3, 4, 5, 6, 7,
865 8, 9, 10, 11, 12, 13, 14, 15,
866 -1, -1, -1, -1, -1, -1, -1, -1,
867 -1, -1, -1, -1, -1, -1, -1, -1,
868 -1, -1, -1, -1, -1, -1, -1, -1,
869 -1, -1, -1, -1, -1, -1, -1, -1,
870 -1, -1, -1, -1, -1, -1, -1, -1,
871 -1, -1, -1, -1, -1, -1, -1, -1
872};
873
Takashi Iwai98274f02005-11-17 14:52:34 +0100874struct hdspm_midi {
875 struct hdspm *hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +0200876 int id;
Takashi Iwai98274f02005-11-17 14:52:34 +0100877 struct snd_rawmidi *rmidi;
878 struct snd_rawmidi_substream *input;
879 struct snd_rawmidi_substream *output;
Takashi Iwai763f3562005-06-03 11:25:34 +0200880 char istimer; /* timer in use */
881 struct timer_list timer;
882 spinlock_t lock;
883 int pending;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100884 int dataIn;
885 int statusIn;
886 int dataOut;
887 int statusOut;
888 int ie;
889 int irq;
890};
891
892struct hdspm_tco {
893 int input;
894 int framerate;
895 int wordclock;
896 int samplerate;
897 int pull;
898 int term; /* 0 = off, 1 = on */
Takashi Iwai763f3562005-06-03 11:25:34 +0200899};
900
Takashi Iwai98274f02005-11-17 14:52:34 +0100901struct hdspm {
Takashi Iwai763f3562005-06-03 11:25:34 +0200902 spinlock_t lock;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200903 /* only one playback and/or capture stream */
904 struct snd_pcm_substream *capture_substream;
905 struct snd_pcm_substream *playback_substream;
Takashi Iwai763f3562005-06-03 11:25:34 +0200906
907 char *card_name; /* for procinfo */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200908 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
909
Adrian Knoth0dca1792011-01-26 19:32:14 +0100910 uint8_t io_type;
Takashi Iwai763f3562005-06-03 11:25:34 +0200911
Takashi Iwai763f3562005-06-03 11:25:34 +0200912 int monitor_outs; /* set up monitoring outs init flag */
913
914 u32 control_register; /* cached value */
915 u32 control2_register; /* cached value */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100916 u32 settings_register;
Takashi Iwai763f3562005-06-03 11:25:34 +0200917
Adrian Knoth0dca1792011-01-26 19:32:14 +0100918 struct hdspm_midi midi[4];
Takashi Iwai763f3562005-06-03 11:25:34 +0200919 struct tasklet_struct midi_tasklet;
920
921 size_t period_bytes;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100922 unsigned char ss_in_channels;
923 unsigned char ds_in_channels;
924 unsigned char qs_in_channels;
925 unsigned char ss_out_channels;
926 unsigned char ds_out_channels;
927 unsigned char qs_out_channels;
928
929 unsigned char max_channels_in;
930 unsigned char max_channels_out;
931
Takashi Iwai286bed02011-06-30 12:45:36 +0200932 signed char *channel_map_in;
933 signed char *channel_map_out;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100934
Takashi Iwai286bed02011-06-30 12:45:36 +0200935 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
936 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100937
938 char **port_names_in;
939 char **port_names_out;
940
941 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
942 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
Takashi Iwai763f3562005-06-03 11:25:34 +0200943
944 unsigned char *playback_buffer; /* suitably aligned address */
945 unsigned char *capture_buffer; /* suitably aligned address */
946
947 pid_t capture_pid; /* process id which uses capture */
948 pid_t playback_pid; /* process id which uses capture */
949 int running; /* running status */
950
951 int last_external_sample_rate; /* samplerate mystic ... */
952 int last_internal_sample_rate;
953 int system_sample_rate;
954
Takashi Iwai763f3562005-06-03 11:25:34 +0200955 int dev; /* Hardware vars... */
956 int irq;
957 unsigned long port;
958 void __iomem *iobase;
959
960 int irq_count; /* for debug */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100961 int midiPorts;
Takashi Iwai763f3562005-06-03 11:25:34 +0200962
Takashi Iwai98274f02005-11-17 14:52:34 +0100963 struct snd_card *card; /* one card */
964 struct snd_pcm *pcm; /* has one pcm */
965 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
Takashi Iwai763f3562005-06-03 11:25:34 +0200966 struct pci_dev *pci; /* and an pci info */
967
968 /* Mixer vars */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200969 /* fast alsa mixer */
970 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
971 /* but input to much, so not used */
972 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300973 /* full mixer accessible over mixer ioctl or hwdep-device */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200974 struct hdspm_mixer *mixer;
Takashi Iwai763f3562005-06-03 11:25:34 +0200975
Adrian Knoth0dca1792011-01-26 19:32:14 +0100976 struct hdspm_tco *tco; /* NULL if no TCO detected */
Takashi Iwai763f3562005-06-03 11:25:34 +0200977
Adrian Knoth0dca1792011-01-26 19:32:14 +0100978 char **texts_autosync;
979 int texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +0200980
Adrian Knoth0dca1792011-01-26 19:32:14 +0100981 cycles_t last_interrupt;
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100982
Adrian Knoth7d53a632012-01-04 14:31:16 +0100983 unsigned int serial;
984
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100985 struct hdspm_peak_rms peak_rms;
Takashi Iwai763f3562005-06-03 11:25:34 +0200986};
987
Takashi Iwai763f3562005-06-03 11:25:34 +0200988
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200989static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
Takashi Iwai763f3562005-06-03 11:25:34 +0200990 {
991 .vendor = PCI_VENDOR_ID_XILINX,
992 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
993 .subvendor = PCI_ANY_ID,
994 .subdevice = PCI_ANY_ID,
995 .class = 0,
996 .class_mask = 0,
997 .driver_data = 0},
998 {0,}
999};
1000
1001MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
1002
1003/* prototypes */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001004static int snd_hdspm_create_alsa_devices(struct snd_card *card,
1005 struct hdspm *hdspm);
1006static int snd_hdspm_create_pcm(struct snd_card *card,
1007 struct hdspm *hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001008
Adrian Knoth0dca1792011-01-26 19:32:14 +01001009static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
Adrian Knoth3f7bf912013-03-10 00:37:21 +01001010static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001011static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
1012static int hdspm_autosync_ref(struct hdspm *hdspm);
Adrian Knoth34be7eb2013-07-05 11:27:56 +02001013static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001014static int snd_hdspm_set_defaults(struct hdspm *hdspm);
Adrian Knoth21a164d2012-10-19 17:42:23 +02001015static int hdspm_system_clock_mode(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001016static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02001017 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02001018 unsigned int reg, int channels);
1019
Adrian Knoth5b266352013-07-05 11:28:10 +02001020static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx);
1021static int hdspm_wc_sync_check(struct hdspm *hdspm);
1022static int hdspm_tco_sync_check(struct hdspm *hdspm);
1023static int hdspm_sync_in_sync_check(struct hdspm *hdspm);
1024
1025static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index);
1026static int hdspm_get_tco_sample_rate(struct hdspm *hdspm);
1027static int hdspm_get_wc_sample_rate(struct hdspm *hdspm);
1028
1029
1030
Remy Bruno3cee5a62006-10-16 12:46:32 +02001031static inline int HDSPM_bit2freq(int n)
1032{
Denys Vlasenko62cef822008-04-14 13:04:18 +02001033 static const int bit2freq_tab[] = {
1034 0, 32000, 44100, 48000, 64000, 88200,
Remy Bruno3cee5a62006-10-16 12:46:32 +02001035 96000, 128000, 176400, 192000 };
1036 if (n < 1 || n > 9)
1037 return 0;
1038 return bit2freq_tab[n];
1039}
1040
Adrian Knothb2ed6322013-07-05 11:27:54 +02001041static bool hdspm_is_raydat_or_aio(struct hdspm *hdspm)
1042{
1043 return ((AIO == hdspm->io_type) || (RayDAT == hdspm->io_type));
1044}
1045
1046
Adrian Knoth0dca1792011-01-26 19:32:14 +01001047/* Write/read to/from HDSPM with Adresses in Bytes
Takashi Iwai763f3562005-06-03 11:25:34 +02001048 not words but only 32Bit writes are allowed */
1049
Takashi Iwai98274f02005-11-17 14:52:34 +01001050static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
Takashi Iwai763f3562005-06-03 11:25:34 +02001051 unsigned int val)
1052{
1053 writel(val, hdspm->iobase + reg);
1054}
1055
Takashi Iwai98274f02005-11-17 14:52:34 +01001056static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
Takashi Iwai763f3562005-06-03 11:25:34 +02001057{
1058 return readl(hdspm->iobase + reg);
1059}
1060
Adrian Knoth0dca1792011-01-26 19:32:14 +01001061/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1062 mixer is write only on hardware so we have to cache him for read
Takashi Iwai763f3562005-06-03 11:25:34 +02001063 each fader is a u32, but uses only the first 16 bit */
1064
Takashi Iwai98274f02005-11-17 14:52:34 +01001065static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001066 unsigned int in)
1067{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001068 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001069 return 0;
1070
1071 return hdspm->mixer->ch[chan].in[in];
1072}
1073
Takashi Iwai98274f02005-11-17 14:52:34 +01001074static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001075 unsigned int pb)
1076{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001077 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001078 return 0;
1079 return hdspm->mixer->ch[chan].pb[pb];
1080}
1081
Denys Vlasenko62cef822008-04-14 13:04:18 +02001082static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001083 unsigned int in, unsigned short data)
1084{
1085 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1086 return -1;
1087
1088 hdspm_write(hdspm,
1089 HDSPM_MADI_mixerBase +
1090 ((in + 128 * chan) * sizeof(u32)),
1091 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1092 return 0;
1093}
1094
Denys Vlasenko62cef822008-04-14 13:04:18 +02001095static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001096 unsigned int pb, unsigned short data)
1097{
1098 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1099 return -1;
1100
1101 hdspm_write(hdspm,
1102 HDSPM_MADI_mixerBase +
1103 ((64 + pb + 128 * chan) * sizeof(u32)),
1104 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1105 return 0;
1106}
1107
1108
1109/* enable DMA for specific channels, now available for DSP-MADI */
Takashi Iwai98274f02005-11-17 14:52:34 +01001110static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001111{
1112 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1113}
1114
Takashi Iwai98274f02005-11-17 14:52:34 +01001115static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001116{
1117 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1118}
1119
1120/* check if same process is writing and reading */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001121static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001122{
1123 unsigned long flags;
1124 int ret = 1;
1125
1126 spin_lock_irqsave(&hdspm->lock, flags);
1127 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1128 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1129 ret = 0;
1130 }
1131 spin_unlock_irqrestore(&hdspm->lock, flags);
1132 return ret;
1133}
1134
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001135/* round arbitary sample rates to commonly known rates */
1136static int hdspm_round_frequency(int rate)
1137{
1138 if (rate < 38050)
1139 return 32000;
1140 if (rate < 46008)
1141 return 44100;
1142 else
1143 return 48000;
1144}
1145
Adrian Knotha8a729f2013-05-31 12:57:10 +02001146/* QS and DS rates normally can not be detected
1147 * automatically by the card. Only exception is MADI
1148 * in 96k frame mode.
1149 *
1150 * So if we read SS values (32 .. 48k), check for
1151 * user-provided DS/QS bits in the control register
1152 * and multiply the base frequency accordingly.
1153 */
1154static int hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
1155{
1156 if (rate <= 48000) {
1157 if (hdspm->control_register & HDSPM_QuadSpeed)
1158 return rate * 4;
1159 else if (hdspm->control_register &
1160 HDSPM_DoubleSpeed)
1161 return rate * 2;
1162 };
1163 return rate;
1164}
1165
Adrian Knoth5b266352013-07-05 11:28:10 +02001166/* check for external sample rate, returns the sample rate in Hz*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001167static int hdspm_external_sample_rate(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001168{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001169 unsigned int status, status2, timecode;
1170 int syncref, rate = 0, rate_bits;
Takashi Iwai763f3562005-06-03 11:25:34 +02001171
Adrian Knoth0dca1792011-01-26 19:32:14 +01001172 switch (hdspm->io_type) {
1173 case AES32:
1174 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1175 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01001176 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001177
1178 syncref = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001179
Remy Bruno3cee5a62006-10-16 12:46:32 +02001180 if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
1181 status & HDSPM_AES32_wcLock)
Adrian Knoth0dca1792011-01-26 19:32:14 +01001182 return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF);
1183
Remy Bruno3cee5a62006-10-16 12:46:32 +02001184 if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001185 syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
1186 status2 & (HDSPM_LockAES >>
1187 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
1188 return HDSPM_bit2freq((timecode >> (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001189 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001190 break;
1191
1192 case MADIface:
1193 status = hdspm_read(hdspm, HDSPM_statusRegister);
1194
1195 if (!(status & HDSPM_madiLock)) {
1196 rate = 0; /* no lock */
1197 } else {
1198 switch (status & (HDSPM_status1_freqMask)) {
1199 case HDSPM_status1_F_0*1:
1200 rate = 32000; break;
1201 case HDSPM_status1_F_0*2:
1202 rate = 44100; break;
1203 case HDSPM_status1_F_0*3:
1204 rate = 48000; break;
1205 case HDSPM_status1_F_0*4:
1206 rate = 64000; break;
1207 case HDSPM_status1_F_0*5:
1208 rate = 88200; break;
1209 case HDSPM_status1_F_0*6:
1210 rate = 96000; break;
1211 case HDSPM_status1_F_0*7:
1212 rate = 128000; break;
1213 case HDSPM_status1_F_0*8:
1214 rate = 176400; break;
1215 case HDSPM_status1_F_0*9:
1216 rate = 192000; break;
1217 default:
1218 rate = 0; break;
1219 }
1220 }
1221
1222 break;
1223
1224 case MADI:
1225 case AIO:
1226 case RayDAT:
1227 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1228 status = hdspm_read(hdspm, HDSPM_statusRegister);
1229 rate = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02001230
Remy Bruno3cee5a62006-10-16 12:46:32 +02001231 /* if wordclock has synced freq and wordclock is valid */
1232 if ((status2 & HDSPM_wcLock) != 0 &&
Adrian Knothfedf1532011-06-12 17:26:18 +02001233 (status2 & HDSPM_SelSyncRef0) == 0) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02001234
1235 rate_bits = status2 & HDSPM_wcFreqMask;
1236
Adrian Knoth0dca1792011-01-26 19:32:14 +01001237
Remy Bruno3cee5a62006-10-16 12:46:32 +02001238 switch (rate_bits) {
1239 case HDSPM_wcFreq32:
1240 rate = 32000;
1241 break;
1242 case HDSPM_wcFreq44_1:
1243 rate = 44100;
1244 break;
1245 case HDSPM_wcFreq48:
1246 rate = 48000;
1247 break;
1248 case HDSPM_wcFreq64:
1249 rate = 64000;
1250 break;
1251 case HDSPM_wcFreq88_2:
1252 rate = 88200;
1253 break;
1254 case HDSPM_wcFreq96:
1255 rate = 96000;
1256 break;
Adrian Knotha8cd7142013-05-31 12:57:09 +02001257 case HDSPM_wcFreq128:
1258 rate = 128000;
1259 break;
1260 case HDSPM_wcFreq176_4:
1261 rate = 176400;
1262 break;
1263 case HDSPM_wcFreq192:
1264 rate = 192000;
1265 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001266 default:
1267 rate = 0;
1268 break;
1269 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001270 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001271
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001272 /* if rate detected and Syncref is Word than have it,
1273 * word has priority to MADI
1274 */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001275 if (rate != 0 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001276 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
Adrian Knoth7b559392013-05-31 12:57:11 +02001277 return hdspm_rate_multiplier(hdspm, rate);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001278
Adrian Knoth0dca1792011-01-26 19:32:14 +01001279 /* maybe a madi input (which is taken if sel sync is madi) */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001280 if (status & HDSPM_madiLock) {
1281 rate_bits = status & HDSPM_madiFreqMask;
1282
1283 switch (rate_bits) {
1284 case HDSPM_madiFreq32:
1285 rate = 32000;
1286 break;
1287 case HDSPM_madiFreq44_1:
1288 rate = 44100;
1289 break;
1290 case HDSPM_madiFreq48:
1291 rate = 48000;
1292 break;
1293 case HDSPM_madiFreq64:
1294 rate = 64000;
1295 break;
1296 case HDSPM_madiFreq88_2:
1297 rate = 88200;
1298 break;
1299 case HDSPM_madiFreq96:
1300 rate = 96000;
1301 break;
1302 case HDSPM_madiFreq128:
1303 rate = 128000;
1304 break;
1305 case HDSPM_madiFreq176_4:
1306 rate = 176400;
1307 break;
1308 case HDSPM_madiFreq192:
1309 rate = 192000;
1310 break;
1311 default:
1312 rate = 0;
1313 break;
1314 }
Adrian Knothd12c51d2011-07-29 03:11:03 +02001315
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001316 } /* endif HDSPM_madiLock */
1317
1318 /* check sample rate from TCO or SYNC_IN */
1319 {
1320 bool is_valid_input = 0;
1321 bool has_sync = 0;
1322
1323 syncref = hdspm_autosync_ref(hdspm);
1324 if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
1325 is_valid_input = 1;
1326 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1327 hdspm_tco_sync_check(hdspm));
1328 } else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
1329 is_valid_input = 1;
1330 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1331 hdspm_sync_in_sync_check(hdspm));
Adrian Knothd12c51d2011-07-29 03:11:03 +02001332 }
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001333
1334 if (is_valid_input && has_sync) {
1335 rate = hdspm_round_frequency(
1336 hdspm_get_pll_freq(hdspm));
1337 }
1338 }
1339
Adrian Knotha8a729f2013-05-31 12:57:10 +02001340 rate = hdspm_rate_multiplier(hdspm, rate);
1341
Adrian Knoth0dca1792011-01-26 19:32:14 +01001342 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001343 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001344
1345 return rate;
Takashi Iwai763f3562005-06-03 11:25:34 +02001346}
1347
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001348/* return latency in samples per period */
1349static int hdspm_get_latency(struct hdspm *hdspm)
1350{
1351 int n;
1352
1353 n = hdspm_decode_latency(hdspm->control_register);
1354
1355 /* Special case for new RME cards with 32 samples period size.
1356 * The three latency bits in the control register
1357 * (HDSP_LatencyMask) encode latency values of 64 samples as
1358 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
1359 * denotes 8192 samples, but on new cards like RayDAT or AIO,
1360 * it corresponds to 32 samples.
1361 */
1362 if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
1363 n = -1;
1364
1365 return 1 << (n + 6);
1366}
1367
Takashi Iwai763f3562005-06-03 11:25:34 +02001368/* Latency function */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001369static inline void hdspm_compute_period_size(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001370{
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001371 hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001372}
1373
Adrian Knoth0dca1792011-01-26 19:32:14 +01001374
1375static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001376{
1377 int position;
1378
1379 position = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth483cee72011-02-23 11:43:09 +01001380
1381 switch (hdspm->io_type) {
1382 case RayDAT:
1383 case AIO:
1384 position &= HDSPM_BufferPositionMask;
1385 position /= 4; /* Bytes per sample */
1386 break;
1387 default:
1388 position = (position & HDSPM_BufferID) ?
1389 (hdspm->period_bytes / 4) : 0;
1390 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001391
1392 return position;
1393}
1394
1395
Takashi Iwai98274f02005-11-17 14:52:34 +01001396static inline void hdspm_start_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001397{
1398 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1399 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1400}
1401
Takashi Iwai98274f02005-11-17 14:52:34 +01001402static inline void hdspm_stop_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001403{
1404 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1405 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1406}
1407
1408/* should I silence all or only opened ones ? doit all for first even is 4MB*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001409static void hdspm_silence_playback(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001410{
1411 int i;
1412 int n = hdspm->period_bytes;
1413 void *buf = hdspm->playback_buffer;
1414
Remy Bruno3cee5a62006-10-16 12:46:32 +02001415 if (buf == NULL)
1416 return;
Takashi Iwai763f3562005-06-03 11:25:34 +02001417
1418 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1419 memset(buf, 0, n);
1420 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1421 }
1422}
1423
Adrian Knoth0dca1792011-01-26 19:32:14 +01001424static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
Takashi Iwai763f3562005-06-03 11:25:34 +02001425{
1426 int n;
1427
1428 spin_lock_irq(&s->lock);
1429
Adrian Knoth2e610272011-08-15 00:22:54 +02001430 if (32 == frames) {
1431 /* Special case for new RME cards like RayDAT/AIO which
1432 * support period sizes of 32 samples. Since latency is
1433 * encoded in the three bits of HDSP_LatencyMask, we can only
1434 * have values from 0 .. 7. While 0 still means 64 samples and
1435 * 6 represents 4096 samples on all cards, 7 represents 8192
1436 * on older cards and 32 samples on new cards.
1437 *
1438 * In other words, period size in samples is calculated by
1439 * 2^(n+6) with n ranging from 0 .. 7.
1440 */
1441 n = 7;
1442 } else {
1443 frames >>= 7;
1444 n = 0;
1445 while (frames) {
1446 n++;
1447 frames >>= 1;
1448 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001449 }
Adrian Knoth2e610272011-08-15 00:22:54 +02001450
Takashi Iwai763f3562005-06-03 11:25:34 +02001451 s->control_register &= ~HDSPM_LatencyMask;
1452 s->control_register |= hdspm_encode_latency(n);
1453
1454 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1455
1456 hdspm_compute_period_size(s);
1457
1458 spin_unlock_irq(&s->lock);
1459
1460 return 0;
1461}
1462
Adrian Knoth0dca1792011-01-26 19:32:14 +01001463static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1464{
1465 u64 freq_const;
1466
1467 if (period == 0)
1468 return 0;
1469
1470 switch (hdspm->io_type) {
1471 case MADI:
1472 case AES32:
1473 freq_const = 110069313433624ULL;
1474 break;
1475 case RayDAT:
1476 case AIO:
1477 freq_const = 104857600000000ULL;
1478 break;
1479 case MADIface:
1480 freq_const = 131072000000000ULL;
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001481 break;
1482 default:
1483 snd_BUG();
1484 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001485 }
1486
1487 return div_u64(freq_const, period);
1488}
1489
1490
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001491static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1492{
1493 u64 n;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001494
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001495 if (rate >= 112000)
1496 rate /= 4;
1497 else if (rate >= 56000)
1498 rate /= 2;
1499
Adrian Knoth0dca1792011-01-26 19:32:14 +01001500 switch (hdspm->io_type) {
1501 case MADIface:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001502 n = 131072000000000ULL; /* 125 MHz */
1503 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001504 case MADI:
1505 case AES32:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001506 n = 110069313433624ULL; /* 105 MHz */
1507 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001508 case RayDAT:
1509 case AIO:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001510 n = 104857600000000ULL; /* 100 MHz */
1511 break;
1512 default:
1513 snd_BUG();
1514 return;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001515 }
1516
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001517 n = div_u64(n, rate);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001518 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001519 snd_BUG_ON(n >> 32);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001520 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1521}
Takashi Iwai763f3562005-06-03 11:25:34 +02001522
1523/* dummy set rate lets see what happens */
Takashi Iwai98274f02005-11-17 14:52:34 +01001524static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
Takashi Iwai763f3562005-06-03 11:25:34 +02001525{
Takashi Iwai763f3562005-06-03 11:25:34 +02001526 int current_rate;
1527 int rate_bits;
1528 int not_set = 0;
Remy Bruno65345992007-08-31 12:21:08 +02001529 int current_speed, target_speed;
Takashi Iwai763f3562005-06-03 11:25:34 +02001530
1531 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1532 it (e.g. during module initialization).
1533 */
1534
1535 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1536
Adrian Knoth0dca1792011-01-26 19:32:14 +01001537 /* SLAVE --- */
Takashi Iwai763f3562005-06-03 11:25:34 +02001538 if (called_internally) {
1539
Adrian Knoth0dca1792011-01-26 19:32:14 +01001540 /* request from ctl or card initialization
1541 just make a warning an remember setting
1542 for future master mode switching */
1543
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001544 snd_printk(KERN_WARNING "HDSPM: "
1545 "Warning: device is not running "
1546 "as a clock master.\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001547 not_set = 1;
1548 } else {
1549
1550 /* hw_param request while in AutoSync mode */
1551 int external_freq =
1552 hdspm_external_sample_rate(hdspm);
1553
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001554 if (hdspm_autosync_ref(hdspm) ==
1555 HDSPM_AUTOSYNC_FROM_NONE) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001556
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001557 snd_printk(KERN_WARNING "HDSPM: "
1558 "Detected no Externel Sync \n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001559 not_set = 1;
1560
1561 } else if (rate != external_freq) {
1562
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001563 snd_printk(KERN_WARNING "HDSPM: "
1564 "Warning: No AutoSync source for "
1565 "requested rate\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001566 not_set = 1;
1567 }
1568 }
1569 }
1570
1571 current_rate = hdspm->system_sample_rate;
1572
1573 /* Changing between Singe, Double and Quad speed is not
1574 allowed if any substreams are open. This is because such a change
1575 causes a shift in the location of the DMA buffers and a reduction
1576 in the number of available buffers.
1577
1578 Note that a similar but essentially insoluble problem exists for
1579 externally-driven rate changes. All we can do is to flag rate
Adrian Knoth0dca1792011-01-26 19:32:14 +01001580 changes in the read/write routines.
Takashi Iwai763f3562005-06-03 11:25:34 +02001581 */
1582
Remy Bruno65345992007-08-31 12:21:08 +02001583 if (current_rate <= 48000)
1584 current_speed = HDSPM_SPEED_SINGLE;
1585 else if (current_rate <= 96000)
1586 current_speed = HDSPM_SPEED_DOUBLE;
1587 else
1588 current_speed = HDSPM_SPEED_QUAD;
1589
1590 if (rate <= 48000)
1591 target_speed = HDSPM_SPEED_SINGLE;
1592 else if (rate <= 96000)
1593 target_speed = HDSPM_SPEED_DOUBLE;
1594 else
1595 target_speed = HDSPM_SPEED_QUAD;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001596
Takashi Iwai763f3562005-06-03 11:25:34 +02001597 switch (rate) {
1598 case 32000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001599 rate_bits = HDSPM_Frequency32KHz;
1600 break;
1601 case 44100:
Takashi Iwai763f3562005-06-03 11:25:34 +02001602 rate_bits = HDSPM_Frequency44_1KHz;
1603 break;
1604 case 48000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001605 rate_bits = HDSPM_Frequency48KHz;
1606 break;
1607 case 64000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001608 rate_bits = HDSPM_Frequency64KHz;
1609 break;
1610 case 88200:
Takashi Iwai763f3562005-06-03 11:25:34 +02001611 rate_bits = HDSPM_Frequency88_2KHz;
1612 break;
1613 case 96000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001614 rate_bits = HDSPM_Frequency96KHz;
1615 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001616 case 128000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001617 rate_bits = HDSPM_Frequency128KHz;
1618 break;
1619 case 176400:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001620 rate_bits = HDSPM_Frequency176_4KHz;
1621 break;
1622 case 192000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001623 rate_bits = HDSPM_Frequency192KHz;
1624 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001625 default:
1626 return -EINVAL;
1627 }
1628
Remy Bruno65345992007-08-31 12:21:08 +02001629 if (current_speed != target_speed
Takashi Iwai763f3562005-06-03 11:25:34 +02001630 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1631 snd_printk
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001632 (KERN_ERR "HDSPM: "
Remy Bruno65345992007-08-31 12:21:08 +02001633 "cannot change from %s speed to %s speed mode "
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001634 "(capture PID = %d, playback PID = %d)\n",
Remy Bruno65345992007-08-31 12:21:08 +02001635 hdspm_speed_names[current_speed],
1636 hdspm_speed_names[target_speed],
Takashi Iwai763f3562005-06-03 11:25:34 +02001637 hdspm->capture_pid, hdspm->playback_pid);
1638 return -EBUSY;
1639 }
1640
1641 hdspm->control_register &= ~HDSPM_FrequencyMask;
1642 hdspm->control_register |= rate_bits;
1643 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1644
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001645 /* For AES32, need to set DDS value in FREQ register
1646 For MADI, also apparently */
1647 hdspm_set_dds_value(hdspm, rate);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001648
1649 if (AES32 == hdspm->io_type && rate != current_rate)
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001650 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
Takashi Iwai763f3562005-06-03 11:25:34 +02001651
1652 hdspm->system_sample_rate = rate;
1653
Adrian Knoth0dca1792011-01-26 19:32:14 +01001654 if (rate <= 48000) {
1655 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1656 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1657 hdspm->max_channels_in = hdspm->ss_in_channels;
1658 hdspm->max_channels_out = hdspm->ss_out_channels;
1659 hdspm->port_names_in = hdspm->port_names_in_ss;
1660 hdspm->port_names_out = hdspm->port_names_out_ss;
1661 } else if (rate <= 96000) {
1662 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1663 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1664 hdspm->max_channels_in = hdspm->ds_in_channels;
1665 hdspm->max_channels_out = hdspm->ds_out_channels;
1666 hdspm->port_names_in = hdspm->port_names_in_ds;
1667 hdspm->port_names_out = hdspm->port_names_out_ds;
1668 } else {
1669 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1670 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1671 hdspm->max_channels_in = hdspm->qs_in_channels;
1672 hdspm->max_channels_out = hdspm->qs_out_channels;
1673 hdspm->port_names_in = hdspm->port_names_in_qs;
1674 hdspm->port_names_out = hdspm->port_names_out_qs;
1675 }
1676
Takashi Iwai763f3562005-06-03 11:25:34 +02001677 if (not_set != 0)
1678 return -1;
1679
1680 return 0;
1681}
1682
1683/* mainly for init to 0 on load */
Takashi Iwai98274f02005-11-17 14:52:34 +01001684static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
Takashi Iwai763f3562005-06-03 11:25:34 +02001685{
1686 int i, j;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001687 unsigned int gain;
1688
1689 if (sgain > UNITY_GAIN)
1690 gain = UNITY_GAIN;
1691 else if (sgain < 0)
1692 gain = 0;
1693 else
1694 gain = sgain;
Takashi Iwai763f3562005-06-03 11:25:34 +02001695
1696 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1697 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1698 hdspm_write_in_gain(hdspm, i, j, gain);
1699 hdspm_write_pb_gain(hdspm, i, j, gain);
1700 }
1701}
1702
1703/*----------------------------------------------------------------------------
1704 MIDI
1705 ----------------------------------------------------------------------------*/
1706
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001707static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1708 int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001709{
1710 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001711 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
Takashi Iwai763f3562005-06-03 11:25:34 +02001712}
1713
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001714static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1715 int val)
Takashi Iwai763f3562005-06-03 11:25:34 +02001716{
1717 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001718 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
Takashi Iwai763f3562005-06-03 11:25:34 +02001719}
1720
Takashi Iwai98274f02005-11-17 14:52:34 +01001721static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001722{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001723 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001724}
1725
Takashi Iwai98274f02005-11-17 14:52:34 +01001726static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001727{
1728 int fifo_bytes_used;
1729
Adrian Knoth0dca1792011-01-26 19:32:14 +01001730 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001731
1732 if (fifo_bytes_used < 128)
1733 return 128 - fifo_bytes_used;
1734 else
1735 return 0;
1736}
1737
Denys Vlasenko62cef822008-04-14 13:04:18 +02001738static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001739{
1740 while (snd_hdspm_midi_input_available (hdspm, id))
1741 snd_hdspm_midi_read_byte (hdspm, id);
1742}
1743
Takashi Iwai98274f02005-11-17 14:52:34 +01001744static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001745{
1746 unsigned long flags;
1747 int n_pending;
1748 int to_write;
1749 int i;
1750 unsigned char buf[128];
1751
1752 /* Output is not interrupt driven */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001753
Takashi Iwai763f3562005-06-03 11:25:34 +02001754 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001755 if (hmidi->output &&
1756 !snd_rawmidi_transmit_empty (hmidi->output)) {
1757 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1758 hmidi->id);
1759 if (n_pending > 0) {
1760 if (n_pending > (int)sizeof (buf))
1761 n_pending = sizeof (buf);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001762
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001763 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1764 n_pending);
1765 if (to_write > 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001766 for (i = 0; i < to_write; ++i)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001767 snd_hdspm_midi_write_byte (hmidi->hdspm,
1768 hmidi->id,
1769 buf[i]);
Takashi Iwai763f3562005-06-03 11:25:34 +02001770 }
1771 }
1772 }
1773 spin_unlock_irqrestore (&hmidi->lock, flags);
1774 return 0;
1775}
1776
Takashi Iwai98274f02005-11-17 14:52:34 +01001777static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001778{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001779 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1780 * input FIFO size
1781 */
Takashi Iwai763f3562005-06-03 11:25:34 +02001782 unsigned long flags;
1783 int n_pending;
1784 int i;
1785
1786 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001787 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1788 if (n_pending > 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001789 if (hmidi->input) {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001790 if (n_pending > (int)sizeof (buf))
Takashi Iwai763f3562005-06-03 11:25:34 +02001791 n_pending = sizeof (buf);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001792 for (i = 0; i < n_pending; ++i)
1793 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1794 hmidi->id);
1795 if (n_pending)
1796 snd_rawmidi_receive (hmidi->input, buf,
1797 n_pending);
Takashi Iwai763f3562005-06-03 11:25:34 +02001798 } else {
1799 /* flush the MIDI input FIFO */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001800 while (n_pending--)
1801 snd_hdspm_midi_read_byte (hmidi->hdspm,
1802 hmidi->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02001803 }
1804 }
1805 hmidi->pending = 0;
Adrian Knothc0da0012011-06-12 17:26:17 +02001806 spin_unlock_irqrestore(&hmidi->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001807
Adrian Knothc0da0012011-06-12 17:26:17 +02001808 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001809 hmidi->hdspm->control_register |= hmidi->ie;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001810 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1811 hmidi->hdspm->control_register);
Adrian Knothc0da0012011-06-12 17:26:17 +02001812 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001813
Takashi Iwai763f3562005-06-03 11:25:34 +02001814 return snd_hdspm_midi_output_write (hmidi);
1815}
1816
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001817static void
1818snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001819{
Takashi Iwai98274f02005-11-17 14:52:34 +01001820 struct hdspm *hdspm;
1821 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001822 unsigned long flags;
Takashi Iwai763f3562005-06-03 11:25:34 +02001823
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001824 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001825 hdspm = hmidi->hdspm;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001826
Takashi Iwai763f3562005-06-03 11:25:34 +02001827 spin_lock_irqsave (&hdspm->lock, flags);
1828 if (up) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001829 if (!(hdspm->control_register & hmidi->ie)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001830 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001831 hdspm->control_register |= hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001832 }
1833 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001834 hdspm->control_register &= ~hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001835 }
1836
1837 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1838 spin_unlock_irqrestore (&hdspm->lock, flags);
1839}
1840
1841static void snd_hdspm_midi_output_timer(unsigned long data)
1842{
Takashi Iwai98274f02005-11-17 14:52:34 +01001843 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001844 unsigned long flags;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001845
Takashi Iwai763f3562005-06-03 11:25:34 +02001846 snd_hdspm_midi_output_write(hmidi);
1847 spin_lock_irqsave (&hmidi->lock, flags);
1848
1849 /* this does not bump hmidi->istimer, because the
1850 kernel automatically removed the timer when it
1851 expired, and we are now adding it back, thus
Adrian Knoth0dca1792011-01-26 19:32:14 +01001852 leaving istimer wherever it was set before.
Takashi Iwai763f3562005-06-03 11:25:34 +02001853 */
1854
1855 if (hmidi->istimer) {
1856 hmidi->timer.expires = 1 + jiffies;
1857 add_timer(&hmidi->timer);
1858 }
1859
1860 spin_unlock_irqrestore (&hmidi->lock, flags);
1861}
1862
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001863static void
1864snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001865{
Takashi Iwai98274f02005-11-17 14:52:34 +01001866 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001867 unsigned long flags;
1868
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001869 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001870 spin_lock_irqsave (&hmidi->lock, flags);
1871 if (up) {
1872 if (!hmidi->istimer) {
1873 init_timer(&hmidi->timer);
1874 hmidi->timer.function = snd_hdspm_midi_output_timer;
1875 hmidi->timer.data = (unsigned long) hmidi;
1876 hmidi->timer.expires = 1 + jiffies;
1877 add_timer(&hmidi->timer);
1878 hmidi->istimer++;
1879 }
1880 } else {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001881 if (hmidi->istimer && --hmidi->istimer <= 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02001882 del_timer (&hmidi->timer);
Takashi Iwai763f3562005-06-03 11:25:34 +02001883 }
1884 spin_unlock_irqrestore (&hmidi->lock, flags);
1885 if (up)
1886 snd_hdspm_midi_output_write(hmidi);
1887}
1888
Takashi Iwai98274f02005-11-17 14:52:34 +01001889static int snd_hdspm_midi_input_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 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1896 hmidi->input = substream;
1897 spin_unlock_irq (&hmidi->lock);
1898
1899 return 0;
1900}
1901
Takashi Iwai98274f02005-11-17 14:52:34 +01001902static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001903{
Takashi Iwai98274f02005-11-17 14:52:34 +01001904 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001905
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001906 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001907 spin_lock_irq (&hmidi->lock);
1908 hmidi->output = substream;
1909 spin_unlock_irq (&hmidi->lock);
1910
1911 return 0;
1912}
1913
Takashi Iwai98274f02005-11-17 14:52:34 +01001914static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001915{
Takashi Iwai98274f02005-11-17 14:52:34 +01001916 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001917
1918 snd_hdspm_midi_input_trigger (substream, 0);
1919
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001920 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001921 spin_lock_irq (&hmidi->lock);
1922 hmidi->input = NULL;
1923 spin_unlock_irq (&hmidi->lock);
1924
1925 return 0;
1926}
1927
Takashi Iwai98274f02005-11-17 14:52:34 +01001928static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001929{
Takashi Iwai98274f02005-11-17 14:52:34 +01001930 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001931
1932 snd_hdspm_midi_output_trigger (substream, 0);
1933
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001934 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001935 spin_lock_irq (&hmidi->lock);
1936 hmidi->output = NULL;
1937 spin_unlock_irq (&hmidi->lock);
1938
1939 return 0;
1940}
1941
Takashi Iwai98274f02005-11-17 14:52:34 +01001942static struct snd_rawmidi_ops snd_hdspm_midi_output =
Takashi Iwai763f3562005-06-03 11:25:34 +02001943{
1944 .open = snd_hdspm_midi_output_open,
1945 .close = snd_hdspm_midi_output_close,
1946 .trigger = snd_hdspm_midi_output_trigger,
1947};
1948
Takashi Iwai98274f02005-11-17 14:52:34 +01001949static struct snd_rawmidi_ops snd_hdspm_midi_input =
Takashi Iwai763f3562005-06-03 11:25:34 +02001950{
1951 .open = snd_hdspm_midi_input_open,
1952 .close = snd_hdspm_midi_input_close,
1953 .trigger = snd_hdspm_midi_input_trigger,
1954};
1955
Bill Pembertone23e7a12012-12-06 12:35:10 -05001956static int snd_hdspm_create_midi(struct snd_card *card,
1957 struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001958{
1959 int err;
1960 char buf[32];
1961
1962 hdspm->midi[id].id = id;
Takashi Iwai763f3562005-06-03 11:25:34 +02001963 hdspm->midi[id].hdspm = hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +02001964 spin_lock_init (&hdspm->midi[id].lock);
1965
Adrian Knoth0dca1792011-01-26 19:32:14 +01001966 if (0 == id) {
1967 if (MADIface == hdspm->io_type) {
1968 /* MIDI-over-MADI on HDSPe MADIface */
1969 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1970 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1971 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1972 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1973 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1974 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1975 } else {
1976 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1977 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1978 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1979 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1980 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
1981 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
1982 }
1983 } else if (1 == id) {
1984 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
1985 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
1986 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
1987 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
1988 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
1989 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
1990 } else if ((2 == id) && (MADI == hdspm->io_type)) {
1991 /* MIDI-over-MADI on HDSPe MADI */
1992 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1993 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1994 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
1995 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
1996 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1997 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
1998 } else if (2 == id) {
1999 /* TCO MTC, read only */
2000 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
2001 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
2002 hdspm->midi[2].dataOut = -1;
2003 hdspm->midi[2].statusOut = -1;
2004 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
2005 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
2006 } else if (3 == id) {
2007 /* TCO MTC on HDSPe MADI */
2008 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
2009 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
2010 hdspm->midi[3].dataOut = -1;
2011 hdspm->midi[3].statusOut = -1;
2012 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
2013 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
2014 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002015
Adrian Knoth0dca1792011-01-26 19:32:14 +01002016 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
2017 (MADIface == hdspm->io_type)))) {
2018 if ((id == 0) && (MADIface == hdspm->io_type)) {
2019 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2020 } else if ((id == 2) && (MADI == hdspm->io_type)) {
2021 sprintf(buf, "%s MIDIoverMADI", card->shortname);
2022 } else {
2023 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
2024 }
2025 err = snd_rawmidi_new(card, buf, id, 1, 1,
2026 &hdspm->midi[id].rmidi);
2027 if (err < 0)
2028 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02002029
Adrian Knoth0dca1792011-01-26 19:32:14 +01002030 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
2031 card->id, id+1);
2032 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
Takashi Iwai763f3562005-06-03 11:25:34 +02002033
Adrian Knoth0dca1792011-01-26 19:32:14 +01002034 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2035 SNDRV_RAWMIDI_STREAM_OUTPUT,
2036 &snd_hdspm_midi_output);
2037 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2038 SNDRV_RAWMIDI_STREAM_INPUT,
2039 &snd_hdspm_midi_input);
2040
2041 hdspm->midi[id].rmidi->info_flags |=
2042 SNDRV_RAWMIDI_INFO_OUTPUT |
2043 SNDRV_RAWMIDI_INFO_INPUT |
2044 SNDRV_RAWMIDI_INFO_DUPLEX;
2045 } else {
2046 /* TCO MTC, read only */
2047 sprintf(buf, "%s MTC %d", card->shortname, id+1);
2048 err = snd_rawmidi_new(card, buf, id, 1, 1,
2049 &hdspm->midi[id].rmidi);
2050 if (err < 0)
2051 return err;
2052
2053 sprintf(hdspm->midi[id].rmidi->name,
2054 "%s MTC %d", card->id, id+1);
2055 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
2056
2057 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2058 SNDRV_RAWMIDI_STREAM_INPUT,
2059 &snd_hdspm_midi_input);
2060
2061 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
2062 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002063
2064 return 0;
2065}
2066
2067
2068static void hdspm_midi_tasklet(unsigned long arg)
2069{
Takashi Iwai98274f02005-11-17 14:52:34 +01002070 struct hdspm *hdspm = (struct hdspm *)arg;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002071 int i = 0;
2072
2073 while (i < hdspm->midiPorts) {
2074 if (hdspm->midi[i].pending)
2075 snd_hdspm_midi_input_read(&hdspm->midi[i]);
2076
2077 i++;
2078 }
2079}
Takashi Iwai763f3562005-06-03 11:25:34 +02002080
2081
2082/*-----------------------------------------------------------------------------
2083 Status Interface
2084 ----------------------------------------------------------------------------*/
2085
2086/* get the system sample rate which is set */
2087
Adrian Knoth0dca1792011-01-26 19:32:14 +01002088
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002089static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
2090{
2091 unsigned int period, rate;
2092
2093 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
2094 rate = hdspm_calc_dds_value(hdspm, period);
2095
2096 return rate;
2097}
2098
Adrian Knoth0dca1792011-01-26 19:32:14 +01002099/**
2100 * Calculate the real sample rate from the
2101 * current DDS value.
2102 **/
2103static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
2104{
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002105 unsigned int rate;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002106
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002107 rate = hdspm_get_pll_freq(hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002108
Adrian Knotha97bda72012-05-30 14:23:18 +02002109 if (rate > 207000) {
Adrian Knoth21a164d2012-10-19 17:42:23 +02002110 /* Unreasonable high sample rate as seen on PCI MADI cards. */
2111 if (0 == hdspm_system_clock_mode(hdspm)) {
2112 /* master mode, return internal sample rate */
2113 rate = hdspm->system_sample_rate;
2114 } else {
2115 /* slave mode, return external sample rate */
2116 rate = hdspm_external_sample_rate(hdspm);
2117 }
Adrian Knotha97bda72012-05-30 14:23:18 +02002118 }
2119
Adrian Knoth0dca1792011-01-26 19:32:14 +01002120 return rate;
2121}
2122
2123
Takashi Iwai763f3562005-06-03 11:25:34 +02002124#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002125{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2126 .name = xname, \
2127 .index = xindex, \
2128 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2129 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2130 .info = snd_hdspm_info_system_sample_rate, \
2131 .put = snd_hdspm_put_system_sample_rate, \
2132 .get = snd_hdspm_get_system_sample_rate \
Takashi Iwai763f3562005-06-03 11:25:34 +02002133}
2134
Takashi Iwai98274f02005-11-17 14:52:34 +01002135static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002137{
2138 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2139 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002140 uinfo->value.integer.min = 27000;
2141 uinfo->value.integer.max = 207000;
2142 uinfo->value.integer.step = 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002143 return 0;
2144}
2145
Adrian Knoth0dca1792011-01-26 19:32:14 +01002146
Takashi Iwai98274f02005-11-17 14:52:34 +01002147static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002149 ucontrol)
2150{
Takashi Iwai98274f02005-11-17 14:52:34 +01002151 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002152
Adrian Knoth0dca1792011-01-26 19:32:14 +01002153 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002154 return 0;
2155}
2156
Adrian Knoth41285a92012-10-19 17:42:22 +02002157static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
2158 struct snd_ctl_elem_value *
2159 ucontrol)
2160{
2161 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2162
2163 hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
2164 return 0;
2165}
2166
Adrian Knoth0dca1792011-01-26 19:32:14 +01002167
2168/**
2169 * Returns the WordClock sample rate class for the given card.
2170 **/
2171static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
2172{
2173 int status;
2174
2175 switch (hdspm->io_type) {
2176 case RayDAT:
2177 case AIO:
2178 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2179 return (status >> 16) & 0xF;
2180 break;
2181 default:
2182 break;
2183 }
2184
2185
2186 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002187}
2188
Adrian Knoth0dca1792011-01-26 19:32:14 +01002189
2190/**
2191 * Returns the TCO sample rate class for the given card.
2192 **/
2193static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2194{
2195 int status;
2196
2197 if (hdspm->tco) {
2198 switch (hdspm->io_type) {
2199 case RayDAT:
2200 case AIO:
2201 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2202 return (status >> 20) & 0xF;
2203 break;
2204 default:
2205 break;
2206 }
2207 }
2208
2209 return 0;
2210}
2211
2212
2213/**
2214 * Returns the SYNC_IN sample rate class for the given card.
2215 **/
2216static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2217{
2218 int status;
2219
2220 if (hdspm->tco) {
2221 switch (hdspm->io_type) {
2222 case RayDAT:
2223 case AIO:
2224 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2225 return (status >> 12) & 0xF;
2226 break;
2227 default:
2228 break;
2229 }
2230 }
2231
2232 return 0;
2233}
2234
Adrian Knothd3c36ed2013-07-05 11:28:09 +02002235/**
2236 * Returns the AES sample rate class for the given card.
2237 **/
2238static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index)
2239{
2240 int timecode;
2241
2242 switch (hdspm->io_type) {
2243 case AES32:
2244 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
2245 return (timecode >> (4*index)) & 0xF;
2246 break;
2247 default:
2248 break;
2249 }
2250 return 0;
2251}
Adrian Knoth0dca1792011-01-26 19:32:14 +01002252
2253/**
2254 * Returns the sample rate class for input source <idx> for
2255 * 'new style' cards like the AIO and RayDAT.
2256 **/
2257static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2258{
2259 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2260
2261 return (status >> (idx*4)) & 0xF;
2262}
2263
Adrian Knoth8cea5712013-07-05 11:27:59 +02002264static void snd_hdspm_set_infotext(struct snd_ctl_elem_info *uinfo,
2265 char **texts, const int count)
2266{
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268 uinfo->count = 1;
2269 uinfo->value.enumerated.items = count;
2270 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2271 uinfo->value.enumerated.item =
2272 uinfo->value.enumerated.items - 1;
2273 strcpy(uinfo->value.enumerated.name,
2274 texts[uinfo->value.enumerated.item]);
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002275}
2276
Adrian Knoth8cea5712013-07-05 11:27:59 +02002277#define ENUMERATED_CTL_INFO(info, texts) \
2278 snd_hdspm_set_infotext(info, texts, ARRAY_SIZE(texts))
2279
Adrian Knoth0dca1792011-01-26 19:32:14 +01002280
2281
2282#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2283{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2284 .name = xname, \
2285 .private_value = xindex, \
2286 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2287 .info = snd_hdspm_info_autosync_sample_rate, \
2288 .get = snd_hdspm_get_autosync_sample_rate \
2289}
2290
2291
Takashi Iwai98274f02005-11-17 14:52:34 +01002292static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002294{
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002295 ENUMERATED_CTL_INFO(uinfo, texts_freq);
Takashi Iwai763f3562005-06-03 11:25:34 +02002296 return 0;
2297}
2298
Adrian Knoth0dca1792011-01-26 19:32:14 +01002299
Takashi Iwai98274f02005-11-17 14:52:34 +01002300static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002302 ucontrol)
2303{
Takashi Iwai98274f02005-11-17 14:52:34 +01002304 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002305
Adrian Knoth0dca1792011-01-26 19:32:14 +01002306 switch (hdspm->io_type) {
2307 case RayDAT:
2308 switch (kcontrol->private_value) {
2309 case 0:
2310 ucontrol->value.enumerated.item[0] =
2311 hdspm_get_wc_sample_rate(hdspm);
2312 break;
2313 case 7:
2314 ucontrol->value.enumerated.item[0] =
2315 hdspm_get_tco_sample_rate(hdspm);
2316 break;
2317 case 8:
2318 ucontrol->value.enumerated.item[0] =
2319 hdspm_get_sync_in_sample_rate(hdspm);
2320 break;
2321 default:
2322 ucontrol->value.enumerated.item[0] =
2323 hdspm_get_s1_sample_rate(hdspm,
2324 kcontrol->private_value-1);
2325 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002326 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002327
Adrian Knoth0dca1792011-01-26 19:32:14 +01002328 case AIO:
2329 switch (kcontrol->private_value) {
2330 case 0: /* WC */
2331 ucontrol->value.enumerated.item[0] =
2332 hdspm_get_wc_sample_rate(hdspm);
2333 break;
2334 case 4: /* TCO */
2335 ucontrol->value.enumerated.item[0] =
2336 hdspm_get_tco_sample_rate(hdspm);
2337 break;
2338 case 5: /* SYNC_IN */
2339 ucontrol->value.enumerated.item[0] =
2340 hdspm_get_sync_in_sample_rate(hdspm);
2341 break;
2342 default:
2343 ucontrol->value.enumerated.item[0] =
2344 hdspm_get_s1_sample_rate(hdspm,
Adrian Knoth1cb7dbf2013-07-05 11:28:03 +02002345 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002346 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002347 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002348
2349 case AES32:
2350
2351 switch (kcontrol->private_value) {
2352 case 0: /* WC */
2353 ucontrol->value.enumerated.item[0] =
2354 hdspm_get_wc_sample_rate(hdspm);
2355 break;
2356 case 9: /* TCO */
2357 ucontrol->value.enumerated.item[0] =
2358 hdspm_get_tco_sample_rate(hdspm);
2359 break;
2360 case 10: /* SYNC_IN */
2361 ucontrol->value.enumerated.item[0] =
2362 hdspm_get_sync_in_sample_rate(hdspm);
2363 break;
2364 default: /* AES1 to AES8 */
2365 ucontrol->value.enumerated.item[0] =
2366 hdspm_get_s1_sample_rate(hdspm,
2367 kcontrol->private_value-1);
2368 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002369 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002370 break;
Adrian Knothb8812c52012-10-19 17:42:26 +02002371
2372 case MADI:
2373 case MADIface:
2374 {
2375 int rate = hdspm_external_sample_rate(hdspm);
2376 int i, selected_rate = 0;
2377 for (i = 1; i < 10; i++)
2378 if (HDSPM_bit2freq(i) == rate) {
2379 selected_rate = i;
2380 break;
2381 }
2382 ucontrol->value.enumerated.item[0] = selected_rate;
2383 }
2384 break;
2385
Takashi Iwai763f3562005-06-03 11:25:34 +02002386 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002387 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002388 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002389
Takashi Iwai763f3562005-06-03 11:25:34 +02002390 return 0;
2391}
2392
Adrian Knoth0dca1792011-01-26 19:32:14 +01002393
Takashi Iwai763f3562005-06-03 11:25:34 +02002394#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002395{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2396 .name = xname, \
2397 .index = xindex, \
2398 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2399 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2400 .info = snd_hdspm_info_system_clock_mode, \
2401 .get = snd_hdspm_get_system_clock_mode, \
2402 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002403}
2404
2405
Adrian Knoth0dca1792011-01-26 19:32:14 +01002406/**
2407 * Returns the system clock mode for the given card.
2408 * @returns 0 - master, 1 - slave
2409 **/
2410static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002411{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002412 switch (hdspm->io_type) {
2413 case AIO:
2414 case RayDAT:
2415 if (hdspm->settings_register & HDSPM_c0Master)
2416 return 0;
2417 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002418
Adrian Knoth0dca1792011-01-26 19:32:14 +01002419 default:
2420 if (hdspm->control_register & HDSPM_ClockModeMaster)
2421 return 0;
2422 }
2423
Takashi Iwai763f3562005-06-03 11:25:34 +02002424 return 1;
2425}
2426
Adrian Knoth0dca1792011-01-26 19:32:14 +01002427
2428/**
2429 * Sets the system clock mode.
2430 * @param mode 0 - master, 1 - slave
2431 **/
2432static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2433{
Adrian Knoth34be7eb2013-07-05 11:27:56 +02002434 hdspm_set_toggle_setting(hdspm,
2435 (hdspm_is_raydat_or_aio(hdspm)) ?
2436 HDSPM_c0Master : HDSPM_ClockModeMaster,
2437 (0 == mode));
Adrian Knoth0dca1792011-01-26 19:32:14 +01002438}
2439
2440
Takashi Iwai98274f02005-11-17 14:52:34 +01002441static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002443{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002444 static char *texts[] = { "Master", "AutoSync" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002445 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02002446 return 0;
2447}
2448
Takashi Iwai98274f02005-11-17 14:52:34 +01002449static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002451{
Takashi Iwai98274f02005-11-17 14:52:34 +01002452 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002453
Adrian Knoth0dca1792011-01-26 19:32:14 +01002454 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002455 return 0;
2456}
2457
Adrian Knoth0dca1792011-01-26 19:32:14 +01002458static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2460{
2461 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2462 int val;
2463
2464 if (!snd_hdspm_use_is_exclusive(hdspm))
2465 return -EBUSY;
2466
2467 val = ucontrol->value.enumerated.item[0];
2468 if (val < 0)
2469 val = 0;
2470 else if (val > 1)
2471 val = 1;
2472
2473 hdspm_set_system_clock_mode(hdspm, val);
2474
2475 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002476}
2477
Adrian Knoth0dca1792011-01-26 19:32:14 +01002478
2479#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2480{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2481 .name = xname, \
2482 .index = xindex, \
2483 .info = snd_hdspm_info_clock_source, \
2484 .get = snd_hdspm_get_clock_source, \
2485 .put = snd_hdspm_put_clock_source \
2486}
2487
2488
Takashi Iwai98274f02005-11-17 14:52:34 +01002489static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002490{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002491 switch (hdspm->system_sample_rate) {
2492 case 32000: return 0;
2493 case 44100: return 1;
2494 case 48000: return 2;
2495 case 64000: return 3;
2496 case 88200: return 4;
2497 case 96000: return 5;
2498 case 128000: return 6;
2499 case 176400: return 7;
2500 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002501 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002502
2503 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002504}
2505
Takashi Iwai98274f02005-11-17 14:52:34 +01002506static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002507{
2508 int rate;
2509 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002510 case 0:
2511 rate = 32000; break;
2512 case 1:
2513 rate = 44100; break;
2514 case 2:
2515 rate = 48000; break;
2516 case 3:
2517 rate = 64000; break;
2518 case 4:
2519 rate = 88200; break;
2520 case 5:
2521 rate = 96000; break;
2522 case 6:
2523 rate = 128000; break;
2524 case 7:
2525 rate = 176400; break;
2526 case 8:
2527 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002528 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002529 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002530 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002531 hdspm_set_rate(hdspm, rate, 1);
2532 return 0;
2533}
2534
Takashi Iwai98274f02005-11-17 14:52:34 +01002535static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002537{
Takashi Iwai763f3562005-06-03 11:25:34 +02002538 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2539 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002540 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002541
2542 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2543 uinfo->value.enumerated.item =
2544 uinfo->value.enumerated.items - 1;
2545
2546 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002547 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002548
2549 return 0;
2550}
2551
Takashi Iwai98274f02005-11-17 14:52:34 +01002552static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002554{
Takashi Iwai98274f02005-11-17 14:52:34 +01002555 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002556
2557 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2558 return 0;
2559}
2560
Takashi Iwai98274f02005-11-17 14:52:34 +01002561static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002563{
Takashi Iwai98274f02005-11-17 14:52:34 +01002564 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002565 int change;
2566 int val;
2567
2568 if (!snd_hdspm_use_is_exclusive(hdspm))
2569 return -EBUSY;
2570 val = ucontrol->value.enumerated.item[0];
2571 if (val < 0)
2572 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002573 if (val > 9)
2574 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002575 spin_lock_irq(&hdspm->lock);
2576 if (val != hdspm_clock_source(hdspm))
2577 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2578 else
2579 change = 0;
2580 spin_unlock_irq(&hdspm->lock);
2581 return change;
2582}
2583
Adrian Knoth0dca1792011-01-26 19:32:14 +01002584
Takashi Iwai763f3562005-06-03 11:25:34 +02002585#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002586{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002587 .name = xname, \
2588 .index = xindex, \
2589 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2590 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2591 .info = snd_hdspm_info_pref_sync_ref, \
2592 .get = snd_hdspm_get_pref_sync_ref, \
2593 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002594}
2595
Adrian Knoth0dca1792011-01-26 19:32:14 +01002596
2597/**
2598 * Returns the current preferred sync reference setting.
2599 * The semantics of the return value are depending on the
2600 * card, please see the comments for clarification.
2601 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002602static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002603{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002604 switch (hdspm->io_type) {
2605 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002606 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002607 case 0: return 0; /* WC */
2608 case HDSPM_SyncRef0: return 1; /* AES 1 */
2609 case HDSPM_SyncRef1: return 2; /* AES 2 */
2610 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2611 case HDSPM_SyncRef2: return 4; /* AES 4 */
2612 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2613 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2614 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2615 return 7; /* AES 7 */
2616 case HDSPM_SyncRef3: return 8; /* AES 8 */
2617 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002618 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002619 break;
2620
2621 case MADI:
2622 case MADIface:
2623 if (hdspm->tco) {
2624 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2625 case 0: return 0; /* WC */
2626 case HDSPM_SyncRef0: return 1; /* MADI */
2627 case HDSPM_SyncRef1: return 2; /* TCO */
2628 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2629 return 3; /* SYNC_IN */
2630 }
2631 } else {
2632 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2633 case 0: return 0; /* WC */
2634 case HDSPM_SyncRef0: return 1; /* MADI */
2635 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2636 return 2; /* SYNC_IN */
2637 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002638 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002639 break;
2640
2641 case RayDAT:
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 1 */
2647 case 4: return 2; /* ADAT 2 */
2648 case 5: return 3; /* ADAT 3 */
2649 case 6: return 4; /* ADAT 4 */
2650 case 1: return 5; /* AES */
2651 case 2: return 6; /* SPDIF */
2652 case 9: return 7; /* TCO */
2653 case 10: return 8; /* SYNC_IN */
2654 }
2655 } else {
2656 switch ((hdspm->settings_register &
2657 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2658 case 0: return 0; /* WC */
2659 case 3: return 1; /* ADAT 1 */
2660 case 4: return 2; /* ADAT 2 */
2661 case 5: return 3; /* ADAT 3 */
2662 case 6: return 4; /* ADAT 4 */
2663 case 1: return 5; /* AES */
2664 case 2: return 6; /* SPDIF */
2665 case 10: return 7; /* SYNC_IN */
2666 }
2667 }
2668
2669 break;
2670
2671 case AIO:
2672 if (hdspm->tco) {
2673 switch ((hdspm->settings_register &
2674 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2675 case 0: return 0; /* WC */
2676 case 3: return 1; /* ADAT */
2677 case 1: return 2; /* AES */
2678 case 2: return 3; /* SPDIF */
2679 case 9: return 4; /* TCO */
2680 case 10: return 5; /* SYNC_IN */
2681 }
2682 } else {
2683 switch ((hdspm->settings_register &
2684 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2685 case 0: return 0; /* WC */
2686 case 3: return 1; /* ADAT */
2687 case 1: return 2; /* AES */
2688 case 2: return 3; /* SPDIF */
2689 case 10: return 4; /* SYNC_IN */
2690 }
2691 }
2692
2693 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002694 }
2695
Adrian Knoth0dca1792011-01-26 19:32:14 +01002696 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002697}
2698
Adrian Knoth0dca1792011-01-26 19:32:14 +01002699
2700/**
2701 * Set the preferred sync reference to <pref>. The semantics
2702 * of <pref> are depending on the card type, see the comments
2703 * for clarification.
2704 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002705static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002706{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002707 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002708
Adrian Knoth0dca1792011-01-26 19:32:14 +01002709 switch (hdspm->io_type) {
2710 case AES32:
2711 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002712 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002713 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002714 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002715 case 1: /* AES 1 */
2716 hdspm->control_register |= HDSPM_SyncRef0;
2717 break;
2718 case 2: /* AES 2 */
2719 hdspm->control_register |= HDSPM_SyncRef1;
2720 break;
2721 case 3: /* AES 3 */
2722 hdspm->control_register |=
2723 HDSPM_SyncRef1+HDSPM_SyncRef0;
2724 break;
2725 case 4: /* AES 4 */
2726 hdspm->control_register |= HDSPM_SyncRef2;
2727 break;
2728 case 5: /* AES 5 */
2729 hdspm->control_register |=
2730 HDSPM_SyncRef2+HDSPM_SyncRef0;
2731 break;
2732 case 6: /* AES 6 */
2733 hdspm->control_register |=
2734 HDSPM_SyncRef2+HDSPM_SyncRef1;
2735 break;
2736 case 7: /* AES 7 */
2737 hdspm->control_register |=
2738 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2739 break;
2740 case 8: /* AES 8 */
2741 hdspm->control_register |= HDSPM_SyncRef3;
2742 break;
2743 case 9: /* TCO */
2744 hdspm->control_register |=
2745 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002746 break;
2747 default:
2748 return -1;
2749 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002750
2751 break;
2752
2753 case MADI:
2754 case MADIface:
2755 hdspm->control_register &= ~HDSPM_SyncRefMask;
2756 if (hdspm->tco) {
2757 switch (pref) {
2758 case 0: /* WC */
2759 break;
2760 case 1: /* MADI */
2761 hdspm->control_register |= HDSPM_SyncRef0;
2762 break;
2763 case 2: /* TCO */
2764 hdspm->control_register |= HDSPM_SyncRef1;
2765 break;
2766 case 3: /* SYNC_IN */
2767 hdspm->control_register |=
2768 HDSPM_SyncRef0+HDSPM_SyncRef1;
2769 break;
2770 default:
2771 return -1;
2772 }
2773 } else {
2774 switch (pref) {
2775 case 0: /* WC */
2776 break;
2777 case 1: /* MADI */
2778 hdspm->control_register |= HDSPM_SyncRef0;
2779 break;
2780 case 2: /* SYNC_IN */
2781 hdspm->control_register |=
2782 HDSPM_SyncRef0+HDSPM_SyncRef1;
2783 break;
2784 default:
2785 return -1;
2786 }
2787 }
2788
2789 break;
2790
2791 case RayDAT:
2792 if (hdspm->tco) {
2793 switch (pref) {
2794 case 0: p = 0; break; /* WC */
2795 case 1: p = 3; break; /* ADAT 1 */
2796 case 2: p = 4; break; /* ADAT 2 */
2797 case 3: p = 5; break; /* ADAT 3 */
2798 case 4: p = 6; break; /* ADAT 4 */
2799 case 5: p = 1; break; /* AES */
2800 case 6: p = 2; break; /* SPDIF */
2801 case 7: p = 9; break; /* TCO */
2802 case 8: p = 10; break; /* SYNC_IN */
2803 default: return -1;
2804 }
2805 } else {
2806 switch (pref) {
2807 case 0: p = 0; break; /* WC */
2808 case 1: p = 3; break; /* ADAT 1 */
2809 case 2: p = 4; break; /* ADAT 2 */
2810 case 3: p = 5; break; /* ADAT 3 */
2811 case 4: p = 6; break; /* ADAT 4 */
2812 case 5: p = 1; break; /* AES */
2813 case 6: p = 2; break; /* SPDIF */
2814 case 7: p = 10; break; /* SYNC_IN */
2815 default: return -1;
2816 }
2817 }
2818 break;
2819
2820 case AIO:
2821 if (hdspm->tco) {
2822 switch (pref) {
2823 case 0: p = 0; break; /* WC */
2824 case 1: p = 3; break; /* ADAT */
2825 case 2: p = 1; break; /* AES */
2826 case 3: p = 2; break; /* SPDIF */
2827 case 4: p = 9; break; /* TCO */
2828 case 5: p = 10; break; /* SYNC_IN */
2829 default: return -1;
2830 }
2831 } else {
2832 switch (pref) {
2833 case 0: p = 0; break; /* WC */
2834 case 1: p = 3; break; /* ADAT */
2835 case 2: p = 1; break; /* AES */
2836 case 3: p = 2; break; /* SPDIF */
2837 case 4: p = 10; break; /* SYNC_IN */
2838 default: return -1;
2839 }
2840 }
2841 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002842 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002843
2844 switch (hdspm->io_type) {
2845 case RayDAT:
2846 case AIO:
2847 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2848 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2849 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2850 break;
2851
2852 case MADI:
2853 case MADIface:
2854 case AES32:
2855 hdspm_write(hdspm, HDSPM_controlRegister,
2856 hdspm->control_register);
2857 }
2858
Takashi Iwai763f3562005-06-03 11:25:34 +02002859 return 0;
2860}
2861
Adrian Knoth0dca1792011-01-26 19:32:14 +01002862
Takashi Iwai98274f02005-11-17 14:52:34 +01002863static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2864 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002865{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002866 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002867
Adrian Knoth0dca1792011-01-26 19:32:14 +01002868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2869 uinfo->count = 1;
2870 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002871
Adrian Knoth0dca1792011-01-26 19:32:14 +01002872 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2873 uinfo->value.enumerated.item =
2874 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002875
Adrian Knoth0dca1792011-01-26 19:32:14 +01002876 strcpy(uinfo->value.enumerated.name,
2877 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002878
Takashi Iwai763f3562005-06-03 11:25:34 +02002879 return 0;
2880}
2881
Takashi Iwai98274f02005-11-17 14:52:34 +01002882static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2883 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002884{
Takashi Iwai98274f02005-11-17 14:52:34 +01002885 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002886 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002887
Adrian Knoth0dca1792011-01-26 19:32:14 +01002888 if (psf >= 0) {
2889 ucontrol->value.enumerated.item[0] = psf;
2890 return 0;
2891 }
2892
2893 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002894}
2895
Takashi Iwai98274f02005-11-17 14:52:34 +01002896static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002898{
Takashi Iwai98274f02005-11-17 14:52:34 +01002899 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002900 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002901
2902 if (!snd_hdspm_use_is_exclusive(hdspm))
2903 return -EBUSY;
2904
Adrian Knoth0dca1792011-01-26 19:32:14 +01002905 val = ucontrol->value.enumerated.item[0];
2906
2907 if (val < 0)
2908 val = 0;
2909 else if (val >= hdspm->texts_autosync_items)
2910 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002911
2912 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002913 if (val != hdspm_pref_sync_ref(hdspm))
2914 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2915
Takashi Iwai763f3562005-06-03 11:25:34 +02002916 spin_unlock_irq(&hdspm->lock);
2917 return change;
2918}
2919
Adrian Knoth0dca1792011-01-26 19:32:14 +01002920
Takashi Iwai763f3562005-06-03 11:25:34 +02002921#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002922{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2923 .name = xname, \
2924 .index = xindex, \
2925 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2926 .info = snd_hdspm_info_autosync_ref, \
2927 .get = snd_hdspm_get_autosync_ref, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002928}
2929
Adrian Knoth0dca1792011-01-26 19:32:14 +01002930static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002931{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002932 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002933 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002934 unsigned int syncref =
2935 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002936 if (syncref == 0)
2937 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2938 if (syncref <= 8)
2939 return syncref;
2940 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002941 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002942 /* This looks at the autosync selected sync reference */
2943 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002944
Remy Bruno3cee5a62006-10-16 12:46:32 +02002945 switch (status2 & HDSPM_SelSyncRefMask) {
2946 case HDSPM_SelSyncRef_WORD:
2947 return HDSPM_AUTOSYNC_FROM_WORD;
2948 case HDSPM_SelSyncRef_MADI:
2949 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002950 case HDSPM_SelSyncRef_TCO:
2951 return HDSPM_AUTOSYNC_FROM_TCO;
2952 case HDSPM_SelSyncRef_SyncIn:
2953 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002954 case HDSPM_SelSyncRef_NVALID:
2955 return HDSPM_AUTOSYNC_FROM_NONE;
2956 default:
Adrian Knothe71b95a2013-07-05 11:28:06 +02002957 return HDSPM_AUTOSYNC_FROM_NONE;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002958 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002959
Takashi Iwai763f3562005-06-03 11:25:34 +02002960 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002961 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002962}
2963
Adrian Knoth0dca1792011-01-26 19:32:14 +01002964
Takashi Iwai98274f02005-11-17 14:52:34 +01002965static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002967{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002968 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002969
Adrian Knoth0dca1792011-01-26 19:32:14 +01002970 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002971 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
Adrian Knothdb2d1a92013-07-05 11:28:08 +02002972 "AES4", "AES5", "AES6", "AES7", "AES8", "TCO", "Sync In", "None"};
Remy Bruno3cee5a62006-10-16 12:46:32 +02002973
2974 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2975 uinfo->count = 1;
Adrian Knothdb2d1a92013-07-05 11:28:08 +02002976 uinfo->value.enumerated.items = ARRAY_SIZE(texts);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002977 if (uinfo->value.enumerated.item >=
2978 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002979 uinfo->value.enumerated.item =
2980 uinfo->value.enumerated.items - 1;
2981 strcpy(uinfo->value.enumerated.name,
2982 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002983 } else if (MADI == hdspm->io_type) {
2984 static char *texts[] = {"Word Clock", "MADI", "TCO",
2985 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002986
2987 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2988 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002989 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002990 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002991 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002992 uinfo->value.enumerated.item =
2993 uinfo->value.enumerated.items - 1;
2994 strcpy(uinfo->value.enumerated.name,
2995 texts[uinfo->value.enumerated.item]);
2996 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002997 return 0;
2998}
2999
Takashi Iwai98274f02005-11-17 14:52:34 +01003000static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003002{
Takashi Iwai98274f02005-11-17 14:52:34 +01003003 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003004
Remy Bruno65345992007-08-31 12:21:08 +02003005 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02003006 return 0;
3007}
3008
Adrian Knothf99c7882013-03-10 00:37:26 +01003009
3010
3011#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
3012{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3013 .name = xname, \
3014 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3015 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3016 .info = snd_hdspm_info_tco_video_input_format, \
3017 .get = snd_hdspm_get_tco_video_input_format, \
3018}
3019
3020static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_info *uinfo)
3022{
3023 static char *texts[] = {"No video", "NTSC", "PAL"};
3024 ENUMERATED_CTL_INFO(uinfo, texts);
3025 return 0;
3026}
3027
3028static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
3029 struct snd_ctl_elem_value *ucontrol)
3030{
3031 u32 status;
3032 int ret = 0;
3033
3034 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3035 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3036 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3037 HDSPM_TCO1_Video_Input_Format_PAL)) {
3038 case HDSPM_TCO1_Video_Input_Format_NTSC:
3039 /* ntsc */
3040 ret = 1;
3041 break;
3042 case HDSPM_TCO1_Video_Input_Format_PAL:
3043 /* pal */
3044 ret = 2;
3045 break;
3046 default:
3047 /* no video */
3048 ret = 0;
3049 break;
3050 }
3051 ucontrol->value.enumerated.item[0] = ret;
3052 return 0;
3053}
3054
3055
3056
3057#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3058{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3059 .name = xname, \
3060 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3061 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3062 .info = snd_hdspm_info_tco_ltc_frames, \
3063 .get = snd_hdspm_get_tco_ltc_frames, \
3064}
3065
3066static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_info *uinfo)
3068{
3069 static char *texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3070 "30 fps"};
3071 ENUMERATED_CTL_INFO(uinfo, texts);
3072 return 0;
3073}
3074
3075static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3076{
3077 u32 status;
3078 int ret = 0;
3079
3080 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3081 if (status & HDSPM_TCO1_LTC_Input_valid) {
3082 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3083 HDSPM_TCO1_LTC_Format_MSB)) {
3084 case 0:
3085 /* 24 fps */
3086 ret = 1;
3087 break;
3088 case HDSPM_TCO1_LTC_Format_LSB:
3089 /* 25 fps */
3090 ret = 2;
3091 break;
3092 case HDSPM_TCO1_LTC_Format_MSB:
3093 /* 25 fps */
3094 ret = 3;
3095 break;
3096 default:
3097 /* 30 fps */
3098 ret = 4;
3099 break;
3100 }
3101 }
3102
3103 return ret;
3104}
3105
3106static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3107 struct snd_ctl_elem_value *ucontrol)
3108{
3109 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3110
3111 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3112 return 0;
3113}
3114
Adrian Knothbf0ff872012-12-03 14:55:49 +01003115#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3116{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3117 .name = xname, \
3118 .private_value = xindex, \
3119 .info = snd_hdspm_info_toggle_setting, \
3120 .get = snd_hdspm_get_toggle_setting, \
3121 .put = snd_hdspm_put_toggle_setting \
3122}
3123
3124static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3125{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003126 u32 reg;
3127
3128 if (hdspm_is_raydat_or_aio(hdspm))
3129 reg = hdspm->settings_register;
3130 else
3131 reg = hdspm->control_register;
3132
3133 return (reg & regmask) ? 1 : 0;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003134}
3135
3136static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3137{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003138 u32 *reg;
3139 u32 target_reg;
3140
3141 if (hdspm_is_raydat_or_aio(hdspm)) {
3142 reg = &(hdspm->settings_register);
3143 target_reg = HDSPM_WR_SETTINGS;
3144 } else {
3145 reg = &(hdspm->control_register);
3146 target_reg = HDSPM_controlRegister;
3147 }
3148
Adrian Knothbf0ff872012-12-03 14:55:49 +01003149 if (out)
Adrian Knothce13f3f2013-07-05 11:27:55 +02003150 *reg |= regmask;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003151 else
Adrian Knothce13f3f2013-07-05 11:27:55 +02003152 *reg &= ~regmask;
3153
3154 hdspm_write(hdspm, target_reg, *reg);
Adrian Knothbf0ff872012-12-03 14:55:49 +01003155
3156 return 0;
3157}
3158
3159#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3160
3161static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_value *ucontrol)
3163{
3164 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3165 u32 regmask = kcontrol->private_value;
3166
3167 spin_lock_irq(&hdspm->lock);
3168 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3169 spin_unlock_irq(&hdspm->lock);
3170 return 0;
3171}
3172
3173static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3174 struct snd_ctl_elem_value *ucontrol)
3175{
3176 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3177 u32 regmask = kcontrol->private_value;
3178 int change;
3179 unsigned int val;
3180
3181 if (!snd_hdspm_use_is_exclusive(hdspm))
3182 return -EBUSY;
3183 val = ucontrol->value.integer.value[0] & 1;
3184 spin_lock_irq(&hdspm->lock);
3185 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3186 hdspm_set_toggle_setting(hdspm, regmask, val);
3187 spin_unlock_irq(&hdspm->lock);
3188 return change;
3189}
3190
Takashi Iwai763f3562005-06-03 11:25:34 +02003191#define HDSPM_INPUT_SELECT(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003192{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3193 .name = xname, \
3194 .index = xindex, \
3195 .info = snd_hdspm_info_input_select, \
3196 .get = snd_hdspm_get_input_select, \
3197 .put = snd_hdspm_put_input_select \
Takashi Iwai763f3562005-06-03 11:25:34 +02003198}
3199
Takashi Iwai98274f02005-11-17 14:52:34 +01003200static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003201{
3202 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3203}
3204
Takashi Iwai98274f02005-11-17 14:52:34 +01003205static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003206{
3207 if (out)
3208 hdspm->control_register |= HDSPM_InputSelect0;
3209 else
3210 hdspm->control_register &= ~HDSPM_InputSelect0;
3211 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3212
3213 return 0;
3214}
3215
Takashi Iwai98274f02005-11-17 14:52:34 +01003216static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3217 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003218{
3219 static char *texts[] = { "optical", "coaxial" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003220 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003221 return 0;
3222}
3223
Takashi Iwai98274f02005-11-17 14:52:34 +01003224static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003226{
Takashi Iwai98274f02005-11-17 14:52:34 +01003227 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003228
3229 spin_lock_irq(&hdspm->lock);
3230 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3231 spin_unlock_irq(&hdspm->lock);
3232 return 0;
3233}
3234
Takashi Iwai98274f02005-11-17 14:52:34 +01003235static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003237{
Takashi Iwai98274f02005-11-17 14:52:34 +01003238 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003239 int change;
3240 unsigned int val;
3241
3242 if (!snd_hdspm_use_is_exclusive(hdspm))
3243 return -EBUSY;
3244 val = ucontrol->value.integer.value[0] & 1;
3245 spin_lock_irq(&hdspm->lock);
3246 change = (int) val != hdspm_input_select(hdspm);
3247 hdspm_set_input_select(hdspm, val);
3248 spin_unlock_irq(&hdspm->lock);
3249 return change;
3250}
3251
Adrian Knoth0dca1792011-01-26 19:32:14 +01003252
Remy Bruno3cee5a62006-10-16 12:46:32 +02003253#define HDSPM_DS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003254{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3255 .name = xname, \
3256 .index = xindex, \
3257 .info = snd_hdspm_info_ds_wire, \
3258 .get = snd_hdspm_get_ds_wire, \
3259 .put = snd_hdspm_put_ds_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003260}
3261
3262static int hdspm_ds_wire(struct hdspm * hdspm)
3263{
3264 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3265}
3266
3267static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3268{
3269 if (ds)
3270 hdspm->control_register |= HDSPM_DS_DoubleWire;
3271 else
3272 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3273 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3274
3275 return 0;
3276}
3277
3278static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_info *uinfo)
3280{
3281 static char *texts[] = { "Single", "Double" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003282 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003283 return 0;
3284}
3285
3286static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3288{
3289 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3290
3291 spin_lock_irq(&hdspm->lock);
3292 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3293 spin_unlock_irq(&hdspm->lock);
3294 return 0;
3295}
3296
3297static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3299{
3300 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3301 int change;
3302 unsigned int val;
3303
3304 if (!snd_hdspm_use_is_exclusive(hdspm))
3305 return -EBUSY;
3306 val = ucontrol->value.integer.value[0] & 1;
3307 spin_lock_irq(&hdspm->lock);
3308 change = (int) val != hdspm_ds_wire(hdspm);
3309 hdspm_set_ds_wire(hdspm, val);
3310 spin_unlock_irq(&hdspm->lock);
3311 return change;
3312}
3313
Adrian Knoth0dca1792011-01-26 19:32:14 +01003314
Remy Bruno3cee5a62006-10-16 12:46:32 +02003315#define HDSPM_QS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003316{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3317 .name = xname, \
3318 .index = xindex, \
3319 .info = snd_hdspm_info_qs_wire, \
3320 .get = snd_hdspm_get_qs_wire, \
3321 .put = snd_hdspm_put_qs_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003322}
3323
3324static int hdspm_qs_wire(struct hdspm * hdspm)
3325{
3326 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3327 return 1;
3328 if (hdspm->control_register & HDSPM_QS_QuadWire)
3329 return 2;
3330 return 0;
3331}
3332
3333static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3334{
3335 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3336 switch (mode) {
3337 case 0:
3338 break;
3339 case 1:
3340 hdspm->control_register |= HDSPM_QS_DoubleWire;
3341 break;
3342 case 2:
3343 hdspm->control_register |= HDSPM_QS_QuadWire;
3344 break;
3345 }
3346 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3347
3348 return 0;
3349}
3350
3351static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3352 struct snd_ctl_elem_info *uinfo)
3353{
3354 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003355 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003356 return 0;
3357}
3358
3359static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3360 struct snd_ctl_elem_value *ucontrol)
3361{
3362 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3363
3364 spin_lock_irq(&hdspm->lock);
3365 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3366 spin_unlock_irq(&hdspm->lock);
3367 return 0;
3368}
3369
3370static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3371 struct snd_ctl_elem_value *ucontrol)
3372{
3373 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3374 int change;
3375 int val;
3376
3377 if (!snd_hdspm_use_is_exclusive(hdspm))
3378 return -EBUSY;
3379 val = ucontrol->value.integer.value[0];
3380 if (val < 0)
3381 val = 0;
3382 if (val > 2)
3383 val = 2;
3384 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003385 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003386 hdspm_set_qs_wire(hdspm, val);
3387 spin_unlock_irq(&hdspm->lock);
3388 return change;
3389}
3390
Adrian Knothacf14762013-07-05 11:28:00 +02003391#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
3392{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3393 .name = xname, \
3394 .private_value = xindex, \
3395 .info = snd_hdspm_info_tristate, \
3396 .get = snd_hdspm_get_tristate, \
3397 .put = snd_hdspm_put_tristate \
3398}
3399
3400static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
3401{
3402 u32 reg = hdspm->settings_register & (regmask * 3);
3403 return reg / regmask;
3404}
3405
3406static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
3407{
3408 hdspm->settings_register &= ~(regmask * 3);
3409 hdspm->settings_register |= (regmask * mode);
3410 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
3411
3412 return 0;
3413}
3414
3415static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
3416 struct snd_ctl_elem_info *uinfo)
3417{
3418 u32 regmask = kcontrol->private_value;
3419
3420 static char *texts_spdif[] = { "Optical", "Coaxial", "Internal" };
3421 static char *texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
3422
3423 switch (regmask) {
3424 case HDSPM_c0_Input0:
3425 ENUMERATED_CTL_INFO(uinfo, texts_spdif);
3426 break;
3427 default:
3428 ENUMERATED_CTL_INFO(uinfo, texts_levels);
3429 break;
3430 }
3431 return 0;
3432}
3433
3434static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
3436{
3437 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3438 u32 regmask = kcontrol->private_value;
3439
3440 spin_lock_irq(&hdspm->lock);
3441 ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
3442 spin_unlock_irq(&hdspm->lock);
3443 return 0;
3444}
3445
3446static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
3447 struct snd_ctl_elem_value *ucontrol)
3448{
3449 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3450 u32 regmask = kcontrol->private_value;
3451 int change;
3452 int val;
3453
3454 if (!snd_hdspm_use_is_exclusive(hdspm))
3455 return -EBUSY;
3456 val = ucontrol->value.integer.value[0];
3457 if (val < 0)
3458 val = 0;
3459 if (val > 2)
3460 val = 2;
3461
3462 spin_lock_irq(&hdspm->lock);
3463 change = val != hdspm_tristate(hdspm, regmask);
3464 hdspm_set_tristate(hdspm, val, regmask);
3465 spin_unlock_irq(&hdspm->lock);
3466 return change;
3467}
3468
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003469#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3470{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3471 .name = xname, \
3472 .index = xindex, \
3473 .info = snd_hdspm_info_madi_speedmode, \
3474 .get = snd_hdspm_get_madi_speedmode, \
3475 .put = snd_hdspm_put_madi_speedmode \
3476}
3477
3478static int hdspm_madi_speedmode(struct hdspm *hdspm)
3479{
3480 if (hdspm->control_register & HDSPM_QuadSpeed)
3481 return 2;
3482 if (hdspm->control_register & HDSPM_DoubleSpeed)
3483 return 1;
3484 return 0;
3485}
3486
3487static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3488{
3489 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3490 switch (mode) {
3491 case 0:
3492 break;
3493 case 1:
3494 hdspm->control_register |= HDSPM_DoubleSpeed;
3495 break;
3496 case 2:
3497 hdspm->control_register |= HDSPM_QuadSpeed;
3498 break;
3499 }
3500 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3501
3502 return 0;
3503}
3504
3505static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3506 struct snd_ctl_elem_info *uinfo)
3507{
3508 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003509 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003510 return 0;
3511}
3512
3513static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3514 struct snd_ctl_elem_value *ucontrol)
3515{
3516 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3517
3518 spin_lock_irq(&hdspm->lock);
3519 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3520 spin_unlock_irq(&hdspm->lock);
3521 return 0;
3522}
3523
3524static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3525 struct snd_ctl_elem_value *ucontrol)
3526{
3527 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3528 int change;
3529 int val;
3530
3531 if (!snd_hdspm_use_is_exclusive(hdspm))
3532 return -EBUSY;
3533 val = ucontrol->value.integer.value[0];
3534 if (val < 0)
3535 val = 0;
3536 if (val > 2)
3537 val = 2;
3538 spin_lock_irq(&hdspm->lock);
3539 change = val != hdspm_madi_speedmode(hdspm);
3540 hdspm_set_madi_speedmode(hdspm, val);
3541 spin_unlock_irq(&hdspm->lock);
3542 return change;
3543}
Takashi Iwai763f3562005-06-03 11:25:34 +02003544
3545#define HDSPM_MIXER(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003546{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3547 .name = xname, \
3548 .index = xindex, \
3549 .device = 0, \
3550 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3551 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3552 .info = snd_hdspm_info_mixer, \
3553 .get = snd_hdspm_get_mixer, \
3554 .put = snd_hdspm_put_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003555}
3556
Takashi Iwai98274f02005-11-17 14:52:34 +01003557static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3558 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003559{
3560 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3561 uinfo->count = 3;
3562 uinfo->value.integer.min = 0;
3563 uinfo->value.integer.max = 65535;
3564 uinfo->value.integer.step = 1;
3565 return 0;
3566}
3567
Takashi Iwai98274f02005-11-17 14:52:34 +01003568static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3569 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003570{
Takashi Iwai98274f02005-11-17 14:52:34 +01003571 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003572 int source;
3573 int destination;
3574
3575 source = ucontrol->value.integer.value[0];
3576 if (source < 0)
3577 source = 0;
3578 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3579 source = 2 * HDSPM_MAX_CHANNELS - 1;
3580
3581 destination = ucontrol->value.integer.value[1];
3582 if (destination < 0)
3583 destination = 0;
3584 else if (destination >= HDSPM_MAX_CHANNELS)
3585 destination = HDSPM_MAX_CHANNELS - 1;
3586
3587 spin_lock_irq(&hdspm->lock);
3588 if (source >= HDSPM_MAX_CHANNELS)
3589 ucontrol->value.integer.value[2] =
3590 hdspm_read_pb_gain(hdspm, destination,
3591 source - HDSPM_MAX_CHANNELS);
3592 else
3593 ucontrol->value.integer.value[2] =
3594 hdspm_read_in_gain(hdspm, destination, source);
3595
3596 spin_unlock_irq(&hdspm->lock);
3597
3598 return 0;
3599}
3600
Takashi Iwai98274f02005-11-17 14:52:34 +01003601static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3602 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003603{
Takashi Iwai98274f02005-11-17 14:52:34 +01003604 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003605 int change;
3606 int source;
3607 int destination;
3608 int gain;
3609
3610 if (!snd_hdspm_use_is_exclusive(hdspm))
3611 return -EBUSY;
3612
3613 source = ucontrol->value.integer.value[0];
3614 destination = ucontrol->value.integer.value[1];
3615
3616 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3617 return -1;
3618 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3619 return -1;
3620
3621 gain = ucontrol->value.integer.value[2];
3622
3623 spin_lock_irq(&hdspm->lock);
3624
3625 if (source >= HDSPM_MAX_CHANNELS)
3626 change = gain != hdspm_read_pb_gain(hdspm, destination,
3627 source -
3628 HDSPM_MAX_CHANNELS);
3629 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003630 change = gain != hdspm_read_in_gain(hdspm, destination,
3631 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003632
3633 if (change) {
3634 if (source >= HDSPM_MAX_CHANNELS)
3635 hdspm_write_pb_gain(hdspm, destination,
3636 source - HDSPM_MAX_CHANNELS,
3637 gain);
3638 else
3639 hdspm_write_in_gain(hdspm, destination, source,
3640 gain);
3641 }
3642 spin_unlock_irq(&hdspm->lock);
3643
3644 return change;
3645}
3646
3647/* The simple mixer control(s) provide gain control for the
3648 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003649 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003650*/
3651
3652#define HDSPM_PLAYBACK_MIXER \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003653{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3654 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3655 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3656 .info = snd_hdspm_info_playback_mixer, \
3657 .get = snd_hdspm_get_playback_mixer, \
3658 .put = snd_hdspm_put_playback_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003659}
3660
Takashi Iwai98274f02005-11-17 14:52:34 +01003661static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3662 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003663{
3664 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3665 uinfo->count = 1;
3666 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003667 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003668 uinfo->value.integer.step = 1;
3669 return 0;
3670}
3671
Takashi Iwai98274f02005-11-17 14:52:34 +01003672static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3673 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003674{
Takashi Iwai98274f02005-11-17 14:52:34 +01003675 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003676 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003677
3678 channel = ucontrol->id.index - 1;
3679
Takashi Iwaida3cec32008-08-08 17:12:14 +02003680 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3681 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003682
Takashi Iwai763f3562005-06-03 11:25:34 +02003683 spin_lock_irq(&hdspm->lock);
3684 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003685 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003686 spin_unlock_irq(&hdspm->lock);
3687
Takashi Iwai763f3562005-06-03 11:25:34 +02003688 return 0;
3689}
3690
Takashi Iwai98274f02005-11-17 14:52:34 +01003691static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3692 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003693{
Takashi Iwai98274f02005-11-17 14:52:34 +01003694 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003695 int change;
3696 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003697 int gain;
3698
3699 if (!snd_hdspm_use_is_exclusive(hdspm))
3700 return -EBUSY;
3701
3702 channel = ucontrol->id.index - 1;
3703
Takashi Iwaida3cec32008-08-08 17:12:14 +02003704 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3705 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003706
Adrian Knoth0dca1792011-01-26 19:32:14 +01003707 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003708
3709 spin_lock_irq(&hdspm->lock);
3710 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003711 gain != hdspm_read_pb_gain(hdspm, channel,
3712 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003713 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003714 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003715 gain);
3716 spin_unlock_irq(&hdspm->lock);
3717 return change;
3718}
3719
Adrian Knoth0dca1792011-01-26 19:32:14 +01003720#define HDSPM_SYNC_CHECK(xname, xindex) \
3721{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3722 .name = xname, \
3723 .private_value = xindex, \
3724 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3725 .info = snd_hdspm_info_sync_check, \
3726 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003727}
3728
Adrian Knoth34542212013-03-10 00:37:25 +01003729#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3730{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3731 .name = xname, \
3732 .private_value = xindex, \
3733 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3734 .info = snd_hdspm_tco_info_lock_check, \
3735 .get = snd_hdspm_get_sync_check \
3736}
3737
3738
Adrian Knoth0dca1792011-01-26 19:32:14 +01003739
Takashi Iwai98274f02005-11-17 14:52:34 +01003740static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3741 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003742{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003743 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003744 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003745 return 0;
3746}
3747
Adrian Knoth34542212013-03-10 00:37:25 +01003748static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3749 struct snd_ctl_elem_info *uinfo)
3750{
3751 static char *texts[] = { "No Lock", "Lock" };
3752 ENUMERATED_CTL_INFO(uinfo, texts);
3753 return 0;
3754}
3755
Adrian Knoth0dca1792011-01-26 19:32:14 +01003756static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003757{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003758 int status, status2;
3759
3760 switch (hdspm->io_type) {
3761 case AES32:
3762 status = hdspm_read(hdspm, HDSPM_statusRegister);
Andre Schramm56bde0f2013-01-09 14:40:18 +01003763 if (status & HDSPM_AES32_wcLock) {
3764 if (status & HDSPM_AES32_wcSync)
3765 return 2;
3766 else
3767 return 1;
3768 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02003769 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003770 break;
3771
3772 case MADI:
3773 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003774 if (status2 & HDSPM_wcLock) {
3775 if (status2 & HDSPM_wcSync)
3776 return 2;
3777 else
3778 return 1;
3779 }
3780 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003781 break;
3782
3783 case RayDAT:
3784 case AIO:
3785 status = hdspm_read(hdspm, HDSPM_statusRegister);
3786
3787 if (status & 0x2000000)
3788 return 2;
3789 else if (status & 0x1000000)
3790 return 1;
3791 return 0;
3792
3793 break;
3794
3795 case MADIface:
3796 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003797 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003798
Takashi Iwai763f3562005-06-03 11:25:34 +02003799
Adrian Knoth0dca1792011-01-26 19:32:14 +01003800 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003801}
3802
3803
Adrian Knoth0dca1792011-01-26 19:32:14 +01003804static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003805{
3806 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3807 if (status & HDSPM_madiLock) {
3808 if (status & HDSPM_madiSync)
3809 return 2;
3810 else
3811 return 1;
3812 }
3813 return 0;
3814}
3815
Adrian Knoth0dca1792011-01-26 19:32:14 +01003816
3817static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3818{
3819 int status, lock, sync;
3820
3821 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3822
3823 lock = (status & (0x1<<idx)) ? 1 : 0;
3824 sync = (status & (0x100<<idx)) ? 1 : 0;
3825
3826 if (lock && sync)
3827 return 2;
3828 else if (lock)
3829 return 1;
3830 return 0;
3831}
3832
3833
3834static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3835{
3836 int status, lock = 0, sync = 0;
3837
3838 switch (hdspm->io_type) {
3839 case RayDAT:
3840 case AIO:
3841 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3842 lock = (status & 0x400) ? 1 : 0;
3843 sync = (status & 0x800) ? 1 : 0;
3844 break;
3845
3846 case MADI:
Adrian Knoth2e0452f2012-10-19 17:42:27 +02003847 status = hdspm_read(hdspm, HDSPM_statusRegister);
3848 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3849 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3850 break;
3851
Adrian Knoth0dca1792011-01-26 19:32:14 +01003852 case AES32:
3853 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth9a215f42012-10-19 17:42:28 +02003854 lock = (status & 0x100000) ? 1 : 0;
3855 sync = (status & 0x200000) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003856 break;
3857
3858 case MADIface:
3859 break;
3860 }
3861
3862 if (lock && sync)
3863 return 2;
3864 else if (lock)
3865 return 1;
3866
3867 return 0;
3868}
3869
3870static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3871{
3872 int status2, lock, sync;
3873 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3874
3875 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3876 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3877
3878 if (sync)
3879 return 2;
3880 else if (lock)
3881 return 1;
3882 return 0;
3883}
3884
Adrian Knoth34542212013-03-10 00:37:25 +01003885static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3886{
3887 u32 status;
3888 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3889
3890 return (status & mask) ? 1 : 0;
3891}
3892
Adrian Knoth0dca1792011-01-26 19:32:14 +01003893
3894static int hdspm_tco_sync_check(struct hdspm *hdspm)
3895{
3896 int status;
3897
3898 if (hdspm->tco) {
3899 switch (hdspm->io_type) {
3900 case MADI:
Adrian Knothb0bf5502013-07-05 11:28:05 +02003901 status = hdspm_read(hdspm, HDSPM_statusRegister);
3902 if (status & HDSPM_tcoLockMadi) {
3903 if (status & HDSPM_tcoSync)
3904 return 2;
3905 else
3906 return 1;
3907 }
3908 return 0;
3909 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003910 case AES32:
3911 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knothb0bf5502013-07-05 11:28:05 +02003912 if (status & HDSPM_tcoLockAes) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01003913 if (status & HDSPM_tcoSync)
3914 return 2;
3915 else
3916 return 1;
3917 }
3918 return 0;
3919
3920 break;
3921
3922 case RayDAT:
3923 case AIO:
3924 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3925
3926 if (status & 0x8000000)
3927 return 2; /* Sync */
3928 if (status & 0x4000000)
3929 return 1; /* Lock */
3930 return 0; /* No signal */
3931 break;
3932
3933 default:
3934 break;
3935 }
3936 }
3937
3938 return 3; /* N/A */
3939}
3940
3941
3942static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3943 struct snd_ctl_elem_value *ucontrol)
3944{
3945 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3946 int val = -1;
3947
3948 switch (hdspm->io_type) {
3949 case RayDAT:
3950 switch (kcontrol->private_value) {
3951 case 0: /* WC */
3952 val = hdspm_wc_sync_check(hdspm); break;
3953 case 7: /* TCO */
3954 val = hdspm_tco_sync_check(hdspm); break;
3955 case 8: /* SYNC IN */
3956 val = hdspm_sync_in_sync_check(hdspm); break;
3957 default:
Adrian Knothd1a3c982012-11-07 18:00:09 +01003958 val = hdspm_s1_sync_check(hdspm,
3959 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003960 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003961 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003962
3963 case AIO:
3964 switch (kcontrol->private_value) {
3965 case 0: /* WC */
3966 val = hdspm_wc_sync_check(hdspm); break;
3967 case 4: /* TCO */
3968 val = hdspm_tco_sync_check(hdspm); break;
3969 case 5: /* SYNC IN */
3970 val = hdspm_sync_in_sync_check(hdspm); break;
3971 default:
Adrian Knoth1cb7dbf2013-07-05 11:28:03 +02003972 val = hdspm_s1_sync_check(hdspm,
3973 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003974 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003975 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003976
3977 case MADI:
3978 switch (kcontrol->private_value) {
3979 case 0: /* WC */
3980 val = hdspm_wc_sync_check(hdspm); break;
3981 case 1: /* MADI */
3982 val = hdspm_madi_sync_check(hdspm); break;
3983 case 2: /* TCO */
3984 val = hdspm_tco_sync_check(hdspm); break;
3985 case 3: /* SYNC_IN */
3986 val = hdspm_sync_in_sync_check(hdspm); break;
3987 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003988 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003989
3990 case MADIface:
3991 val = hdspm_madi_sync_check(hdspm); /* MADI */
3992 break;
3993
3994 case AES32:
3995 switch (kcontrol->private_value) {
3996 case 0: /* WC */
3997 val = hdspm_wc_sync_check(hdspm); break;
3998 case 9: /* TCO */
3999 val = hdspm_tco_sync_check(hdspm); break;
4000 case 10 /* SYNC IN */:
4001 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01004002 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01004003 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01004004 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004005 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02004006 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004007
4008 }
4009
Adrian Knoth34542212013-03-10 00:37:25 +01004010 if (hdspm->tco) {
4011 switch (kcontrol->private_value) {
4012 case 11:
4013 /* Check TCO for lock state of its current input */
4014 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
4015 break;
4016 case 12:
4017 /* Check TCO for valid time code on LTC input. */
4018 val = hdspm_tco_input_check(hdspm,
4019 HDSPM_TCO1_LTC_Input_valid);
4020 break;
4021 default:
4022 break;
4023 }
4024 }
4025
Adrian Knoth0dca1792011-01-26 19:32:14 +01004026 if (-1 == val)
4027 val = 3;
4028
4029 ucontrol->value.enumerated.item[0] = val;
4030 return 0;
4031}
4032
4033
4034
4035/**
4036 * TCO controls
4037 **/
4038static void hdspm_tco_write(struct hdspm *hdspm)
4039{
4040 unsigned int tc[4] = { 0, 0, 0, 0};
4041
4042 switch (hdspm->tco->input) {
4043 case 0:
4044 tc[2] |= HDSPM_TCO2_set_input_MSB;
4045 break;
4046 case 1:
4047 tc[2] |= HDSPM_TCO2_set_input_LSB;
4048 break;
4049 default:
4050 break;
4051 }
4052
4053 switch (hdspm->tco->framerate) {
4054 case 1:
4055 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
4056 break;
4057 case 2:
4058 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
4059 break;
4060 case 3:
4061 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
4062 HDSPM_TCO1_set_drop_frame_flag;
4063 break;
4064 case 4:
4065 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4066 HDSPM_TCO1_LTC_Format_MSB;
4067 break;
4068 case 5:
4069 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4070 HDSPM_TCO1_LTC_Format_MSB +
4071 HDSPM_TCO1_set_drop_frame_flag;
4072 break;
4073 default:
4074 break;
4075 }
4076
4077 switch (hdspm->tco->wordclock) {
4078 case 1:
4079 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
4080 break;
4081 case 2:
4082 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4083 break;
4084 default:
4085 break;
4086 }
4087
4088 switch (hdspm->tco->samplerate) {
4089 case 1:
4090 tc[2] |= HDSPM_TCO2_set_freq;
4091 break;
4092 case 2:
4093 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4094 break;
4095 default:
4096 break;
4097 }
4098
4099 switch (hdspm->tco->pull) {
4100 case 1:
4101 tc[2] |= HDSPM_TCO2_set_pull_up;
4102 break;
4103 case 2:
4104 tc[2] |= HDSPM_TCO2_set_pull_down;
4105 break;
4106 case 3:
4107 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4108 break;
4109 case 4:
4110 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4111 break;
4112 default:
4113 break;
4114 }
4115
4116 if (1 == hdspm->tco->term) {
4117 tc[2] |= HDSPM_TCO2_set_term_75R;
4118 }
4119
4120 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4121 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4122 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4123 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4124}
4125
4126
4127#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4128{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4129 .name = xname, \
4130 .index = xindex, \
4131 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4132 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4133 .info = snd_hdspm_info_tco_sample_rate, \
4134 .get = snd_hdspm_get_tco_sample_rate, \
4135 .put = snd_hdspm_put_tco_sample_rate \
4136}
4137
4138static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4139 struct snd_ctl_elem_info *uinfo)
4140{
4141 static char *texts[] = { "44.1 kHz", "48 kHz" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004142 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004143 return 0;
4144}
4145
4146static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4147 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02004148{
Takashi Iwai98274f02005-11-17 14:52:34 +01004149 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02004150
Adrian Knoth0dca1792011-01-26 19:32:14 +01004151 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4152
Takashi Iwai763f3562005-06-03 11:25:34 +02004153 return 0;
4154}
4155
Adrian Knoth0dca1792011-01-26 19:32:14 +01004156static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4157 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02004158{
Adrian Knoth0dca1792011-01-26 19:32:14 +01004159 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4160
4161 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4162 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4163
4164 hdspm_tco_write(hdspm);
4165
4166 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004167 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01004168
Remy Bruno3cee5a62006-10-16 12:46:32 +02004169 return 0;
4170}
4171
Adrian Knoth0dca1792011-01-26 19:32:14 +01004172
4173#define HDSPM_TCO_PULL(xname, xindex) \
4174{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4175 .name = xname, \
4176 .index = xindex, \
4177 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4178 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4179 .info = snd_hdspm_info_tco_pull, \
4180 .get = snd_hdspm_get_tco_pull, \
4181 .put = snd_hdspm_put_tco_pull \
4182}
4183
4184static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4185 struct snd_ctl_elem_info *uinfo)
4186{
4187 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004188 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004189 return 0;
4190}
4191
4192static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4193 struct snd_ctl_elem_value *ucontrol)
4194{
4195 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4196
4197 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4198
4199 return 0;
4200}
4201
4202static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4203 struct snd_ctl_elem_value *ucontrol)
4204{
4205 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4206
4207 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4208 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4209
4210 hdspm_tco_write(hdspm);
4211
4212 return 1;
4213 }
4214
4215 return 0;
4216}
4217
4218#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4219{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4220 .name = xname, \
4221 .index = xindex, \
4222 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4223 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4224 .info = snd_hdspm_info_tco_wck_conversion, \
4225 .get = snd_hdspm_get_tco_wck_conversion, \
4226 .put = snd_hdspm_put_tco_wck_conversion \
4227}
4228
4229static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4230 struct snd_ctl_elem_info *uinfo)
4231{
4232 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004233 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004234 return 0;
4235}
4236
4237static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4238 struct snd_ctl_elem_value *ucontrol)
4239{
4240 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4241
4242 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4243
4244 return 0;
4245}
4246
4247static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4248 struct snd_ctl_elem_value *ucontrol)
4249{
4250 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4251
4252 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4253 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4254
4255 hdspm_tco_write(hdspm);
4256
4257 return 1;
4258 }
4259
4260 return 0;
4261}
4262
4263
4264#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4265{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4266 .name = xname, \
4267 .index = xindex, \
4268 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4269 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4270 .info = snd_hdspm_info_tco_frame_rate, \
4271 .get = snd_hdspm_get_tco_frame_rate, \
4272 .put = snd_hdspm_put_tco_frame_rate \
4273}
4274
4275static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4276 struct snd_ctl_elem_info *uinfo)
4277{
4278 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4279 "29.97 dfps", "30 fps", "30 dfps" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004280 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004281 return 0;
4282}
4283
4284static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004285 struct snd_ctl_elem_value *ucontrol)
4286{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004287 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4288
Adrian Knoth0dca1792011-01-26 19:32:14 +01004289 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004290
Remy Bruno3cee5a62006-10-16 12:46:32 +02004291 return 0;
4292}
Takashi Iwai763f3562005-06-03 11:25:34 +02004293
Adrian Knoth0dca1792011-01-26 19:32:14 +01004294static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4295 struct snd_ctl_elem_value *ucontrol)
4296{
4297 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4298
4299 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4300 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4301
4302 hdspm_tco_write(hdspm);
4303
4304 return 1;
4305 }
4306
4307 return 0;
4308}
4309
4310
4311#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4312{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4313 .name = xname, \
4314 .index = xindex, \
4315 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4316 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4317 .info = snd_hdspm_info_tco_sync_source, \
4318 .get = snd_hdspm_get_tco_sync_source, \
4319 .put = snd_hdspm_put_tco_sync_source \
4320}
4321
4322static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4323 struct snd_ctl_elem_info *uinfo)
4324{
4325 static char *texts[] = { "LTC", "Video", "WCK" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004326 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004327 return 0;
4328}
4329
4330static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4331 struct snd_ctl_elem_value *ucontrol)
4332{
4333 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4334
4335 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4336
4337 return 0;
4338}
4339
4340static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4341 struct snd_ctl_elem_value *ucontrol)
4342{
4343 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4344
4345 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4346 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4347
4348 hdspm_tco_write(hdspm);
4349
4350 return 1;
4351 }
4352
4353 return 0;
4354}
4355
4356
4357#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4358{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4359 .name = xname, \
4360 .index = xindex, \
4361 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4362 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4363 .info = snd_hdspm_info_tco_word_term, \
4364 .get = snd_hdspm_get_tco_word_term, \
4365 .put = snd_hdspm_put_tco_word_term \
4366}
4367
4368static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4369 struct snd_ctl_elem_info *uinfo)
4370{
4371 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4372 uinfo->count = 1;
4373 uinfo->value.integer.min = 0;
4374 uinfo->value.integer.max = 1;
4375
4376 return 0;
4377}
4378
4379
4380static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4381 struct snd_ctl_elem_value *ucontrol)
4382{
4383 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4384
4385 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4386
4387 return 0;
4388}
4389
4390
4391static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4392 struct snd_ctl_elem_value *ucontrol)
4393{
4394 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4395
4396 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4397 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4398
4399 hdspm_tco_write(hdspm);
4400
4401 return 1;
4402 }
4403
4404 return 0;
4405}
4406
4407
4408
Takashi Iwai763f3562005-06-03 11:25:34 +02004409
Remy Bruno3cee5a62006-10-16 12:46:32 +02004410static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004411 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004412 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004413 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4414 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4415 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4416 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knothb8812c52012-10-19 17:42:26 +02004417 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004418 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4419 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
Adrian Knoth930f4ff2012-10-19 17:42:29 +02004420 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004421 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Adrian Knothc9e16682012-12-03 14:55:50 +01004422 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4423 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
Adrian Knoth696be0f2013-03-10 00:37:23 +01004424 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
Adrian Knothc9e16682012-12-03 14:55:50 +01004425 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4426 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004427 HDSPM_INPUT_SELECT("Input Select", 0),
4428 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004429};
4430
4431
4432static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4433 HDSPM_MIXER("Mixer", 0),
4434 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4435 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4436 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4437 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4438 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
Adrian Knothc9e16682012-12-03 14:55:50 +01004439 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4440 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4441 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004442 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004443};
4444
Adrian Knoth0dca1792011-01-26 19:32:14 +01004445static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004446 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004447 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004448 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4449 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004450 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004451 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004452 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4453 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4454 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4455 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4456 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4457 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4458 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4459 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4460 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4461 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4462 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004463 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004464 HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004465 HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
4466 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4467 HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
4468 HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004469 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
4470 HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
4471 HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
4472 HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004473
4474 /*
4475 HDSPM_INPUT_SELECT("Input Select", 0),
4476 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4477 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4478 HDSPM_SPDIF_IN("SPDIF In", 0);
4479 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4480 HDSPM_INPUT_LEVEL("Input Level", 0);
4481 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4482 HDSPM_PHONES("Phones", 0);
4483 */
4484};
4485
4486static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4487 HDSPM_MIXER("Mixer", 0),
4488 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4489 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4490 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4491 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4492 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4493 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4494 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4495 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4496 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4497 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4498 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4499 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4500 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4501 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4502 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4503 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4504 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4505 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4506 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4507 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4508 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
Adrian Knoth11a5cd32013-07-05 11:27:57 +02004509 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
4510 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4511 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004512};
4513
4514static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4515 HDSPM_MIXER("Mixer", 0),
4516 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4517 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4518 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4519 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4520 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4521 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4522 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4523 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4524 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4525 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4526 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4527 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4528 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4529 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4530 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4531 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4532 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4533 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4534 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4535 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4536 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4537 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4538 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4539 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4540 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4541 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4542 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4543 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Adrian Knothc9e16682012-12-03 14:55:50 +01004544 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4545 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4546 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4547 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4548 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004549 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4550 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4551};
4552
Adrian Knoth0dca1792011-01-26 19:32:14 +01004553
4554
4555/* Control elements for the optional TCO module */
4556static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4557 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4558 HDSPM_TCO_PULL("TCO Pull", 0),
4559 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4560 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4561 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
Adrian Knotha8176502013-03-10 00:37:27 +01004562 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4563 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4564 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4565 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4566 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004567};
4568
4569
Takashi Iwai98274f02005-11-17 14:52:34 +01004570static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004571
4572
Takashi Iwai98274f02005-11-17 14:52:34 +01004573static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004574{
4575 int i;
4576
Adrian Knoth0dca1792011-01-26 19:32:14 +01004577 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004578 if (hdspm->system_sample_rate > 48000) {
4579 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004580 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4581 SNDRV_CTL_ELEM_ACCESS_READ |
4582 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004583 } else {
4584 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004585 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4586 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004587 }
4588 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004589 SNDRV_CTL_EVENT_MASK_INFO,
4590 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004591 }
4592
4593 return 0;
4594}
4595
4596
Adrian Knoth0dca1792011-01-26 19:32:14 +01004597static int snd_hdspm_create_controls(struct snd_card *card,
4598 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004599{
4600 unsigned int idx, limit;
4601 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004602 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004603 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004604
Adrian Knoth0dca1792011-01-26 19:32:14 +01004605 switch (hdspm->io_type) {
4606 case MADI:
4607 list = snd_hdspm_controls_madi;
4608 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4609 break;
4610 case MADIface:
4611 list = snd_hdspm_controls_madiface;
4612 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4613 break;
4614 case AIO:
4615 list = snd_hdspm_controls_aio;
4616 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4617 break;
4618 case RayDAT:
4619 list = snd_hdspm_controls_raydat;
4620 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4621 break;
4622 case AES32:
4623 list = snd_hdspm_controls_aes32;
4624 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4625 break;
4626 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004627
Adrian Knoth0dca1792011-01-26 19:32:14 +01004628 if (NULL != list) {
4629 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004630 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004631 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004632 if (err < 0)
4633 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004634 }
4635 }
4636
Takashi Iwai763f3562005-06-03 11:25:34 +02004637
Adrian Knoth0dca1792011-01-26 19:32:14 +01004638 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004639 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004640 if (hdspm->system_sample_rate >= 128000) {
4641 limit = hdspm->qs_out_channels;
4642 } else if (hdspm->system_sample_rate >= 64000) {
4643 limit = hdspm->ds_out_channels;
4644 } else {
4645 limit = hdspm->ss_out_channels;
4646 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004647 for (idx = 0; idx < limit; ++idx) {
4648 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004649 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4650 err = snd_ctl_add(card, kctl);
4651 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004652 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004653 hdspm->playback_mixer_ctls[idx] = kctl;
4654 }
4655
Adrian Knoth0dca1792011-01-26 19:32:14 +01004656
4657 if (hdspm->tco) {
4658 /* add tco control elements */
4659 list = snd_hdspm_controls_tco;
4660 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4661 for (idx = 0; idx < limit; idx++) {
4662 err = snd_ctl_add(card,
4663 snd_ctl_new1(&list[idx], hdspm));
4664 if (err < 0)
4665 return err;
4666 }
4667 }
4668
Takashi Iwai763f3562005-06-03 11:25:34 +02004669 return 0;
4670}
4671
4672/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004673 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004674 ------------------------------------------------------------*/
4675
4676static void
Adrian Knoth57601072013-07-05 11:28:04 +02004677snd_hdspm_proc_read_tco(struct snd_info_entry *entry,
4678 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004679{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004680 struct hdspm *hdspm = entry->private_data;
Adrian Knoth57601072013-07-05 11:28:04 +02004681 unsigned int status, control;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004682 int a, ltc, frames, seconds, minutes, hours;
4683 unsigned int period;
4684 u64 freq_const = 0;
4685 u32 rate;
4686
Adrian Knoth57601072013-07-05 11:28:04 +02004687 snd_iprintf(buffer, "--- TCO ---\n");
4688
Takashi Iwai763f3562005-06-03 11:25:34 +02004689 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004690 control = hdspm->control_register;
Takashi Iwai763f3562005-06-03 11:25:34 +02004691
Adrian Knoth0dca1792011-01-26 19:32:14 +01004692
Adrian Knoth0dca1792011-01-26 19:32:14 +01004693 if (status & HDSPM_tco_detect) {
4694 snd_iprintf(buffer, "TCO module detected.\n");
4695 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4696 if (a & HDSPM_TCO1_LTC_Input_valid) {
4697 snd_iprintf(buffer, " LTC valid, ");
4698 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4699 HDSPM_TCO1_LTC_Format_MSB)) {
4700 case 0:
4701 snd_iprintf(buffer, "24 fps, ");
4702 break;
4703 case HDSPM_TCO1_LTC_Format_LSB:
4704 snd_iprintf(buffer, "25 fps, ");
4705 break;
4706 case HDSPM_TCO1_LTC_Format_MSB:
4707 snd_iprintf(buffer, "29.97 fps, ");
4708 break;
4709 default:
4710 snd_iprintf(buffer, "30 fps, ");
4711 break;
4712 }
4713 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4714 snd_iprintf(buffer, "drop frame\n");
4715 } else {
4716 snd_iprintf(buffer, "full frame\n");
4717 }
4718 } else {
4719 snd_iprintf(buffer, " no LTC\n");
4720 }
4721 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4722 snd_iprintf(buffer, " Video: NTSC\n");
4723 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4724 snd_iprintf(buffer, " Video: PAL\n");
4725 } else {
4726 snd_iprintf(buffer, " No video\n");
4727 }
4728 if (a & HDSPM_TCO1_TCO_lock) {
4729 snd_iprintf(buffer, " Sync: lock\n");
4730 } else {
4731 snd_iprintf(buffer, " Sync: no lock\n");
4732 }
4733
4734 switch (hdspm->io_type) {
4735 case MADI:
4736 case AES32:
4737 freq_const = 110069313433624ULL;
4738 break;
4739 case RayDAT:
4740 case AIO:
4741 freq_const = 104857600000000ULL;
4742 break;
4743 case MADIface:
4744 break; /* no TCO possible */
4745 }
4746
4747 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4748 snd_iprintf(buffer, " period: %u\n", period);
4749
4750
4751 /* rate = freq_const/period; */
4752 rate = div_u64(freq_const, period);
4753
4754 if (control & HDSPM_QuadSpeed) {
4755 rate *= 4;
4756 } else if (control & HDSPM_DoubleSpeed) {
4757 rate *= 2;
4758 }
4759
4760 snd_iprintf(buffer, " Frequency: %u Hz\n",
4761 (unsigned int) rate);
4762
4763 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4764 frames = ltc & 0xF;
4765 ltc >>= 4;
4766 frames += (ltc & 0x3) * 10;
4767 ltc >>= 4;
4768 seconds = ltc & 0xF;
4769 ltc >>= 4;
4770 seconds += (ltc & 0x7) * 10;
4771 ltc >>= 4;
4772 minutes = ltc & 0xF;
4773 ltc >>= 4;
4774 minutes += (ltc & 0x7) * 10;
4775 ltc >>= 4;
4776 hours = ltc & 0xF;
4777 ltc >>= 4;
4778 hours += (ltc & 0x3) * 10;
4779 snd_iprintf(buffer,
4780 " LTC In: %02d:%02d:%02d:%02d\n",
4781 hours, minutes, seconds, frames);
4782
4783 } else {
4784 snd_iprintf(buffer, "No TCO module detected.\n");
4785 }
Adrian Knoth57601072013-07-05 11:28:04 +02004786}
4787
4788static void
4789snd_hdspm_proc_read_madi(struct snd_info_entry *entry,
4790 struct snd_info_buffer *buffer)
4791{
4792 struct hdspm *hdspm = entry->private_data;
4793 unsigned int status, status2, control, freq;
4794
4795 char *pref_sync_ref;
4796 char *autosync_ref;
4797 char *system_clock_mode;
4798 char *insel;
4799 int x, x2;
4800
4801 status = hdspm_read(hdspm, HDSPM_statusRegister);
4802 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4803 control = hdspm->control_register;
4804 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
4805
4806 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
4807 hdspm->card_name, hdspm->card->number + 1,
4808 hdspm->firmware_rev,
4809 (status2 & HDSPM_version0) |
4810 (status2 & HDSPM_version1) | (status2 &
4811 HDSPM_version2));
4812
4813 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4814 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
4815 hdspm->serial);
4816
4817 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4818 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4819
4820 snd_iprintf(buffer, "--- System ---\n");
4821
4822 snd_iprintf(buffer,
4823 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4824 status & HDSPM_audioIRQPending,
4825 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4826 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4827 hdspm->irq_count);
4828 snd_iprintf(buffer,
4829 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4830 "estimated= %ld (bytes)\n",
4831 ((status & HDSPM_BufferID) ? 1 : 0),
4832 (status & HDSPM_BufferPositionMask),
4833 (status & HDSPM_BufferPositionMask) %
4834 (2 * (int)hdspm->period_bytes),
4835 ((status & HDSPM_BufferPositionMask) - 64) %
4836 (2 * (int)hdspm->period_bytes),
4837 (long) hdspm_hw_pointer(hdspm) * 4);
4838
4839 snd_iprintf(buffer,
4840 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4841 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4842 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4843 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4844 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4845 snd_iprintf(buffer,
4846 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4847 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4848 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4849 snd_iprintf(buffer,
4850 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4851 "status2=0x%x\n",
4852 hdspm->control_register, hdspm->control2_register,
4853 status, status2);
4854
Takashi Iwai763f3562005-06-03 11:25:34 +02004855
4856 snd_iprintf(buffer, "--- Settings ---\n");
4857
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004858 x = hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02004859
4860 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004861 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4862 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004863
Adrian Knoth0dca1792011-01-26 19:32:14 +01004864 snd_iprintf(buffer, "Line out: %s\n",
4865 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004866
4867 switch (hdspm->control_register & HDSPM_InputMask) {
4868 case HDSPM_InputOptical:
4869 insel = "Optical";
4870 break;
4871 case HDSPM_InputCoaxial:
4872 insel = "Coaxial";
4873 break;
4874 default:
Masanari Iidaec8f53f2012-11-02 00:28:50 +09004875 insel = "Unknown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004876 }
4877
Takashi Iwai763f3562005-06-03 11:25:34 +02004878 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004879 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4880 "Auto Input %s\n",
4881 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4882 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4883 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004884
Adrian Knoth0dca1792011-01-26 19:32:14 +01004885
Remy Bruno3cee5a62006-10-16 12:46:32 +02004886 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004887 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004888 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004889 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004890 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004891
4892 switch (hdspm_pref_sync_ref(hdspm)) {
4893 case HDSPM_SYNC_FROM_WORD:
4894 pref_sync_ref = "Word Clock";
4895 break;
4896 case HDSPM_SYNC_FROM_MADI:
4897 pref_sync_ref = "MADI Sync";
4898 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004899 case HDSPM_SYNC_FROM_TCO:
4900 pref_sync_ref = "TCO";
4901 break;
4902 case HDSPM_SYNC_FROM_SYNC_IN:
4903 pref_sync_ref = "Sync In";
4904 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004905 default:
4906 pref_sync_ref = "XXXX Clock";
4907 break;
4908 }
4909 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004910 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004911
4912 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004913 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004914
4915
4916 snd_iprintf(buffer, "--- Status:\n");
4917
4918 x = status & HDSPM_madiSync;
4919 x2 = status2 & HDSPM_wcSync;
4920
4921 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004922 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4923 "NoLock",
4924 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4925 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004926
4927 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004928 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4929 autosync_ref = "Sync In";
4930 break;
4931 case HDSPM_AUTOSYNC_FROM_TCO:
4932 autosync_ref = "TCO";
4933 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004934 case HDSPM_AUTOSYNC_FROM_WORD:
4935 autosync_ref = "Word Clock";
4936 break;
4937 case HDSPM_AUTOSYNC_FROM_MADI:
4938 autosync_ref = "MADI Sync";
4939 break;
4940 case HDSPM_AUTOSYNC_FROM_NONE:
4941 autosync_ref = "Input not valid";
4942 break;
4943 default:
4944 autosync_ref = "---";
4945 break;
4946 }
4947 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004948 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4949 autosync_ref, hdspm_external_sample_rate(hdspm),
4950 (status & HDSPM_madiFreqMask) >> 22,
4951 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004952
4953 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004954 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4955 (status & HDSPM_RX_64ch) ? "64 channels" :
4956 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004957
Adrian Knoth57601072013-07-05 11:28:04 +02004958 /* call readout function for TCO specific status */
4959 snd_hdspm_proc_read_tco(entry, buffer);
4960
Takashi Iwai763f3562005-06-03 11:25:34 +02004961 snd_iprintf(buffer, "\n");
4962}
4963
Remy Bruno3cee5a62006-10-16 12:46:32 +02004964static void
4965snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4966 struct snd_info_buffer *buffer)
4967{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004968 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004969 unsigned int status;
4970 unsigned int status2;
4971 unsigned int timecode;
Andre Schramm56bde0f2013-01-09 14:40:18 +01004972 unsigned int wcLock, wcSync;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004973 int pref_syncref;
4974 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004975 int x;
4976
4977 status = hdspm_read(hdspm, HDSPM_statusRegister);
4978 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4979 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4980
4981 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4982 hdspm->card_name, hdspm->card->number + 1,
4983 hdspm->firmware_rev);
4984
4985 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4986 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4987
4988 snd_iprintf(buffer, "--- System ---\n");
4989
4990 snd_iprintf(buffer,
4991 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4992 status & HDSPM_audioIRQPending,
4993 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4994 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4995 hdspm->irq_count);
4996 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004997 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4998 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004999 ((status & HDSPM_BufferID) ? 1 : 0),
5000 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005001 (status & HDSPM_BufferPositionMask) %
5002 (2 * (int)hdspm->period_bytes),
5003 ((status & HDSPM_BufferPositionMask) - 64) %
5004 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02005005 (long) hdspm_hw_pointer(hdspm) * 4);
5006
5007 snd_iprintf(buffer,
5008 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
5009 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
5010 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
5011 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
5012 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
5013 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005014 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
5015 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
5016 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
5017 snd_iprintf(buffer,
5018 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
5019 "status2=0x%x\n",
5020 hdspm->control_register, hdspm->control2_register,
5021 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02005022
5023 snd_iprintf(buffer, "--- Settings ---\n");
5024
Adrian Knoth7cb155f2011-08-15 00:22:53 +02005025 x = hdspm_get_latency(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02005026
5027 snd_iprintf(buffer,
5028 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
5029 x, (unsigned long) hdspm->period_bytes);
5030
Adrian Knoth0dca1792011-01-26 19:32:14 +01005031 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005032 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01005033 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02005034
5035 snd_iprintf(buffer,
5036 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
5037 (hdspm->
5038 control_register & HDSPM_clr_tms) ? "on" : "off",
5039 (hdspm->
5040 control_register & HDSPM_Emphasis) ? "on" : "off",
5041 (hdspm->
5042 control_register & HDSPM_Dolby) ? "on" : "off");
5043
Remy Bruno3cee5a62006-10-16 12:46:32 +02005044
5045 pref_syncref = hdspm_pref_sync_ref(hdspm);
5046 if (pref_syncref == 0)
5047 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
5048 else
5049 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
5050 pref_syncref);
5051
5052 snd_iprintf(buffer, "System Clock Frequency: %d\n",
5053 hdspm->system_sample_rate);
5054
5055 snd_iprintf(buffer, "Double speed: %s\n",
5056 hdspm->control_register & HDSPM_DS_DoubleWire?
5057 "Double wire" : "Single wire");
5058 snd_iprintf(buffer, "Quad speed: %s\n",
5059 hdspm->control_register & HDSPM_QS_DoubleWire?
5060 "Double wire" :
5061 hdspm->control_register & HDSPM_QS_QuadWire?
5062 "Quad wire" : "Single wire");
5063
5064 snd_iprintf(buffer, "--- Status:\n");
5065
Andre Schramm56bde0f2013-01-09 14:40:18 +01005066 wcLock = status & HDSPM_AES32_wcLock;
5067 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
5068
Remy Bruno3cee5a62006-10-16 12:46:32 +02005069 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Andre Schramm56bde0f2013-01-09 14:40:18 +01005070 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005071 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005072
5073 for (x = 0; x < 8; x++) {
5074 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005075 x+1,
5076 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01005077 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005078 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005079 }
5080
5081 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005082 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5083 autosync_ref = "None"; break;
5084 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5085 autosync_ref = "Word Clock"; break;
5086 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5087 autosync_ref = "AES1"; break;
5088 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5089 autosync_ref = "AES2"; break;
5090 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5091 autosync_ref = "AES3"; break;
5092 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5093 autosync_ref = "AES4"; break;
5094 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5095 autosync_ref = "AES5"; break;
5096 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5097 autosync_ref = "AES6"; break;
5098 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5099 autosync_ref = "AES7"; break;
5100 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5101 autosync_ref = "AES8"; break;
5102 default:
5103 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005104 }
5105 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5106
5107 snd_iprintf(buffer, "\n");
5108}
5109
Adrian Knoth0dca1792011-01-26 19:32:14 +01005110static void
5111snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5112 struct snd_info_buffer *buffer)
5113{
5114 struct hdspm *hdspm = entry->private_data;
5115 unsigned int status1, status2, status3, control, i;
5116 unsigned int lock, sync;
5117
5118 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5119 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5120 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5121
5122 control = hdspm->control_register;
5123
5124 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5125 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5126 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5127
5128
5129 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5130
5131 snd_iprintf(buffer, "Clock mode : %s\n",
5132 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5133 snd_iprintf(buffer, "System frequency: %d Hz\n",
5134 hdspm_get_system_sample_rate(hdspm));
5135
5136 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5137
5138 lock = 0x1;
5139 sync = 0x100;
5140
5141 for (i = 0; i < 8; i++) {
5142 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5143 i,
5144 (status1 & lock) ? 1 : 0,
5145 (status1 & sync) ? 1 : 0,
5146 texts_freq[(status2 >> (i * 4)) & 0xF]);
5147
5148 lock = lock<<1;
5149 sync = sync<<1;
5150 }
5151
5152 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5153 (status1 & 0x1000000) ? 1 : 0,
5154 (status1 & 0x2000000) ? 1 : 0,
5155 texts_freq[(status1 >> 16) & 0xF]);
5156
5157 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5158 (status1 & 0x4000000) ? 1 : 0,
5159 (status1 & 0x8000000) ? 1 : 0,
5160 texts_freq[(status1 >> 20) & 0xF]);
5161
5162 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5163 (status3 & 0x400) ? 1 : 0,
5164 (status3 & 0x800) ? 1 : 0,
5165 texts_freq[(status2 >> 12) & 0xF]);
5166
5167}
5168
Remy Bruno3cee5a62006-10-16 12:46:32 +02005169#ifdef CONFIG_SND_DEBUG
5170static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01005171snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005172 struct snd_info_buffer *buffer)
5173{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005174 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005175
5176 int j,i;
5177
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005178 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02005179 snd_iprintf(buffer, "0x%08X: ", i);
5180 for (j = 0; j < 16; j += 4)
5181 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5182 snd_iprintf(buffer, "\n");
5183 }
5184}
5185#endif
5186
5187
Adrian Knoth0dca1792011-01-26 19:32:14 +01005188static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5189 struct snd_info_buffer *buffer)
5190{
5191 struct hdspm *hdspm = entry->private_data;
5192 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005193
Adrian Knoth0dca1792011-01-26 19:32:14 +01005194 snd_iprintf(buffer, "# generated by hdspm\n");
5195
5196 for (i = 0; i < hdspm->max_channels_in; i++) {
5197 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5198 }
5199}
5200
5201static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5202 struct snd_info_buffer *buffer)
5203{
5204 struct hdspm *hdspm = entry->private_data;
5205 int i;
5206
5207 snd_iprintf(buffer, "# generated by hdspm\n");
5208
5209 for (i = 0; i < hdspm->max_channels_out; i++) {
5210 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5211 }
5212}
5213
5214
Bill Pembertone23e7a12012-12-06 12:35:10 -05005215static void snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005216{
Takashi Iwai98274f02005-11-17 14:52:34 +01005217 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02005218
Adrian Knoth0dca1792011-01-26 19:32:14 +01005219 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5220 switch (hdspm->io_type) {
5221 case AES32:
5222 snd_info_set_text_ops(entry, hdspm,
5223 snd_hdspm_proc_read_aes32);
5224 break;
5225 case MADI:
5226 snd_info_set_text_ops(entry, hdspm,
5227 snd_hdspm_proc_read_madi);
5228 break;
5229 case MADIface:
5230 /* snd_info_set_text_ops(entry, hdspm,
5231 snd_hdspm_proc_read_madiface); */
5232 break;
5233 case RayDAT:
5234 snd_info_set_text_ops(entry, hdspm,
5235 snd_hdspm_proc_read_raydat);
5236 break;
5237 case AIO:
5238 break;
5239 }
5240 }
5241
5242 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5243 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5244 }
5245
5246 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5247 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5248 }
5249
Remy Bruno3cee5a62006-10-16 12:46:32 +02005250#ifdef CONFIG_SND_DEBUG
5251 /* debug file to read all hdspm registers */
5252 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5253 snd_info_set_text_ops(entry, hdspm,
5254 snd_hdspm_proc_read_debug);
5255#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02005256}
5257
5258/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005259 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005260 ------------------------------------------------------------*/
5261
Takashi Iwai98274f02005-11-17 14:52:34 +01005262static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005263{
Takashi Iwai763f3562005-06-03 11:25:34 +02005264 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005265 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005266 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005267
5268 /* set defaults: */
5269
Adrian Knoth0dca1792011-01-26 19:32:14 +01005270 hdspm->settings_register = 0;
5271
5272 switch (hdspm->io_type) {
5273 case MADI:
5274 case MADIface:
5275 hdspm->control_register =
5276 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5277 break;
5278
5279 case RayDAT:
5280 case AIO:
5281 hdspm->settings_register = 0x1 + 0x1000;
5282 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5283 * line_out */
5284 hdspm->control_register =
5285 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5286 break;
5287
5288 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005289 hdspm->control_register =
Adrian Knothe71b95a2013-07-05 11:28:06 +02005290 HDSPM_ClockModeMaster | /* Master Clock Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005291 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005292 HDSPM_SyncRef0 | /* AES1 is syncclock */
5293 HDSPM_LineOut | /* Analog output in */
5294 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005295 break;
5296 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005297
5298 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5299
Adrian Knoth0dca1792011-01-26 19:32:14 +01005300 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005301 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005302#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005303 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005304#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005305 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005306#endif
5307
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005308 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5309 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005310 hdspm_compute_period_size(hdspm);
5311
5312 /* silence everything */
5313
5314 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5315
Adrian Knothb2ed6322013-07-05 11:27:54 +02005316 if (hdspm_is_raydat_or_aio(hdspm))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005317 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005318
5319 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005320 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005321
5322 return 0;
5323}
5324
5325
5326/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005327 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005328 ------------------------------------------------------------*/
5329
David Howells7d12e782006-10-05 14:55:46 +01005330static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005331{
Takashi Iwai98274f02005-11-17 14:52:34 +01005332 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005333 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005334 int i, audio, midi, schedule = 0;
5335 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005336
5337 status = hdspm_read(hdspm, HDSPM_statusRegister);
5338
5339 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005340 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5341 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005342
Adrian Knoth0dca1792011-01-26 19:32:14 +01005343 /* now = get_cycles(); */
5344 /**
5345 * LAT_2..LAT_0 period counter (win) counter (mac)
5346 * 6 4096 ~256053425 ~514672358
5347 * 5 2048 ~128024983 ~257373821
5348 * 4 1024 ~64023706 ~128718089
5349 * 3 512 ~32005945 ~64385999
5350 * 2 256 ~16003039 ~32260176
5351 * 1 128 ~7998738 ~16194507
5352 * 0 64 ~3998231 ~8191558
5353 **/
5354 /*
5355 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5356 now-hdspm->last_interrupt, status & 0xFFC0);
5357 hdspm->last_interrupt = now;
5358 */
5359
5360 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005361 return IRQ_NONE;
5362
5363 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5364 hdspm->irq_count++;
5365
Takashi Iwai763f3562005-06-03 11:25:34 +02005366
5367 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005368 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005369 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005370
5371 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005372 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005373 }
5374
Adrian Knoth0dca1792011-01-26 19:32:14 +01005375 if (midi) {
5376 i = 0;
5377 while (i < hdspm->midiPorts) {
5378 if ((hdspm_read(hdspm,
5379 hdspm->midi[i].statusIn) & 0xff) &&
5380 (status & hdspm->midi[i].irq)) {
5381 /* we disable interrupts for this input until
5382 * processing is done
5383 */
5384 hdspm->control_register &= ~hdspm->midi[i].ie;
5385 hdspm_write(hdspm, HDSPM_controlRegister,
5386 hdspm->control_register);
5387 hdspm->midi[i].pending = 1;
5388 schedule = 1;
5389 }
5390
5391 i++;
5392 }
5393
5394 if (schedule)
5395 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005396 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005397
Takashi Iwai763f3562005-06-03 11:25:34 +02005398 return IRQ_HANDLED;
5399}
5400
5401/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005402 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005403 ------------------------------------------------------------*/
5404
5405
Adrian Knoth0dca1792011-01-26 19:32:14 +01005406static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5407 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005408{
Takashi Iwai98274f02005-11-17 14:52:34 +01005409 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005410 return hdspm_hw_pointer(hdspm);
5411}
5412
Takashi Iwai763f3562005-06-03 11:25:34 +02005413
Takashi Iwai98274f02005-11-17 14:52:34 +01005414static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005415{
Takashi Iwai98274f02005-11-17 14:52:34 +01005416 struct snd_pcm_runtime *runtime = substream->runtime;
5417 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5418 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005419
5420 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5421 other = hdspm->capture_substream;
5422 else
5423 other = hdspm->playback_substream;
5424
5425 if (hdspm->running)
5426 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5427 else
5428 runtime->status->hw_ptr = 0;
5429 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005430 struct snd_pcm_substream *s;
5431 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005432 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005433 if (s == other) {
5434 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005435 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005436 break;
5437 }
5438 }
5439 }
5440 return 0;
5441}
5442
Takashi Iwai98274f02005-11-17 14:52:34 +01005443static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5444 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005445{
Takashi Iwai98274f02005-11-17 14:52:34 +01005446 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005447 int err;
5448 int i;
5449 pid_t this_pid;
5450 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005451
5452 spin_lock_irq(&hdspm->lock);
5453
5454 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5455 this_pid = hdspm->playback_pid;
5456 other_pid = hdspm->capture_pid;
5457 } else {
5458 this_pid = hdspm->capture_pid;
5459 other_pid = hdspm->playback_pid;
5460 }
5461
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005462 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005463
5464 /* The other stream is open, and not by the same
5465 task as this one. Make sure that the parameters
5466 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005467 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005468
5469 if (params_rate(params) != hdspm->system_sample_rate) {
5470 spin_unlock_irq(&hdspm->lock);
5471 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005472 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005473 return -EBUSY;
5474 }
5475
5476 if (params_period_size(params) != hdspm->period_bytes / 4) {
5477 spin_unlock_irq(&hdspm->lock);
5478 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005479 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005480 return -EBUSY;
5481 }
5482
5483 }
5484 /* We're fine. */
5485 spin_unlock_irq(&hdspm->lock);
5486
5487 /* how to make sure that the rate matches an externally-set one ? */
5488
5489 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005490 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5491 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005492 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005493 spin_unlock_irq(&hdspm->lock);
5494 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005495 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005496 return err;
5497 }
5498 spin_unlock_irq(&hdspm->lock);
5499
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005500 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005501 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005502 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005503 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005504 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005505 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005506 return err;
5507 }
5508
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005509 /* Memory allocation, takashi's method, dont know if we should
5510 * spinlock
5511 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005512 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005513 /* Update for MADI rev 204: we need to allocate for all channels,
5514 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005515
Takashi Iwai763f3562005-06-03 11:25:34 +02005516 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005517 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5518 if (err < 0) {
5519 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005520 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005521 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005522
Takashi Iwai763f3562005-06-03 11:25:34 +02005523 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5524
Takashi Iwai77a23f22008-08-21 13:00:13 +02005525 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005526 params_channels(params));
5527
5528 for (i = 0; i < params_channels(params); ++i)
5529 snd_hdspm_enable_out(hdspm, i, 1);
5530
5531 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005532 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005533 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005534 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005535 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005536 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005537 params_channels(params));
5538
5539 for (i = 0; i < params_channels(params); ++i)
5540 snd_hdspm_enable_in(hdspm, i, 1);
5541
5542 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005543 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005544 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005545 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005546 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005547
Remy Bruno3cee5a62006-10-16 12:46:32 +02005548 /*
5549 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5550 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5551 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005552 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005553 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005554 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005555 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5556 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5557 "playback" : "capture",
5558 params_rate(params), params_channels(params),
5559 params_buffer_size(params));
5560 */
5561
5562
5563 /* Switch to native float format if requested */
5564 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5565 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5566 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5567
5568 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5569 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5570 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5571 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5572
5573 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5574 }
5575 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5576
Takashi Iwai763f3562005-06-03 11:25:34 +02005577 return 0;
5578}
5579
Takashi Iwai98274f02005-11-17 14:52:34 +01005580static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005581{
5582 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005583 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005584
5585 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5586
Adrian Knoth0dca1792011-01-26 19:32:14 +01005587 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005588 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005589 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005590 snd_hdspm_enable_out(hdspm, i, 0);
5591
5592 hdspm->playback_buffer = NULL;
5593 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005594 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005595 snd_hdspm_enable_in(hdspm, i, 0);
5596
5597 hdspm->capture_buffer = NULL;
5598
5599 }
5600
5601 snd_pcm_lib_free_pages(substream);
5602
5603 return 0;
5604}
5605
Adrian Knoth0dca1792011-01-26 19:32:14 +01005606
Takashi Iwai98274f02005-11-17 14:52:34 +01005607static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005608 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005609{
Takashi Iwai98274f02005-11-17 14:52:34 +01005610 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005611
Adrian Knoth0dca1792011-01-26 19:32:14 +01005612 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5613 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5614 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5615 return -EINVAL;
5616 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005617
Adrian Knoth0dca1792011-01-26 19:32:14 +01005618 if (hdspm->channel_map_out[info->channel] < 0) {
5619 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5620 return -EINVAL;
5621 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005622
Adrian Knoth0dca1792011-01-26 19:32:14 +01005623 info->offset = hdspm->channel_map_out[info->channel] *
5624 HDSPM_CHANNEL_BUFFER_BYTES;
5625 } else {
5626 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5627 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5628 return -EINVAL;
5629 }
5630
5631 if (hdspm->channel_map_in[info->channel] < 0) {
5632 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5633 return -EINVAL;
5634 }
5635
5636 info->offset = hdspm->channel_map_in[info->channel] *
5637 HDSPM_CHANNEL_BUFFER_BYTES;
5638 }
5639
Takashi Iwai763f3562005-06-03 11:25:34 +02005640 info->first = 0;
5641 info->step = 32;
5642 return 0;
5643}
5644
Adrian Knoth0dca1792011-01-26 19:32:14 +01005645
Takashi Iwai98274f02005-11-17 14:52:34 +01005646static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005647 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005648{
5649 switch (cmd) {
5650 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005651 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005652
5653 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005654 {
5655 struct snd_pcm_channel_info *info = arg;
5656 return snd_hdspm_channel_info(substream, info);
5657 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005658 default:
5659 break;
5660 }
5661
5662 return snd_pcm_lib_ioctl(substream, cmd, arg);
5663}
5664
Takashi Iwai98274f02005-11-17 14:52:34 +01005665static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005666{
Takashi Iwai98274f02005-11-17 14:52:34 +01005667 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5668 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005669 int running;
5670
5671 spin_lock(&hdspm->lock);
5672 running = hdspm->running;
5673 switch (cmd) {
5674 case SNDRV_PCM_TRIGGER_START:
5675 running |= 1 << substream->stream;
5676 break;
5677 case SNDRV_PCM_TRIGGER_STOP:
5678 running &= ~(1 << substream->stream);
5679 break;
5680 default:
5681 snd_BUG();
5682 spin_unlock(&hdspm->lock);
5683 return -EINVAL;
5684 }
5685 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5686 other = hdspm->capture_substream;
5687 else
5688 other = hdspm->playback_substream;
5689
5690 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005691 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005692 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005693 if (s == other) {
5694 snd_pcm_trigger_done(s, substream);
5695 if (cmd == SNDRV_PCM_TRIGGER_START)
5696 running |= 1 << s->stream;
5697 else
5698 running &= ~(1 << s->stream);
5699 goto _ok;
5700 }
5701 }
5702 if (cmd == SNDRV_PCM_TRIGGER_START) {
5703 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005704 && substream->stream ==
5705 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005706 hdspm_silence_playback(hdspm);
5707 } else {
5708 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005709 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005710 hdspm_silence_playback(hdspm);
5711 }
5712 } else {
5713 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5714 hdspm_silence_playback(hdspm);
5715 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005716_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005717 snd_pcm_trigger_done(substream, substream);
5718 if (!hdspm->running && running)
5719 hdspm_start_audio(hdspm);
5720 else if (hdspm->running && !running)
5721 hdspm_stop_audio(hdspm);
5722 hdspm->running = running;
5723 spin_unlock(&hdspm->lock);
5724
5725 return 0;
5726}
5727
Takashi Iwai98274f02005-11-17 14:52:34 +01005728static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005729{
5730 return 0;
5731}
5732
Takashi Iwai98274f02005-11-17 14:52:34 +01005733static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005734 .info = (SNDRV_PCM_INFO_MMAP |
5735 SNDRV_PCM_INFO_MMAP_VALID |
5736 SNDRV_PCM_INFO_NONINTERLEAVED |
5737 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5738 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5739 .rates = (SNDRV_PCM_RATE_32000 |
5740 SNDRV_PCM_RATE_44100 |
5741 SNDRV_PCM_RATE_48000 |
5742 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005743 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5744 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005745 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005746 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005747 .channels_min = 1,
5748 .channels_max = HDSPM_MAX_CHANNELS,
5749 .buffer_bytes_max =
5750 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005751 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005752 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005753 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005754 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005755 .fifo_size = 0
5756};
5757
Takashi Iwai98274f02005-11-17 14:52:34 +01005758static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005759 .info = (SNDRV_PCM_INFO_MMAP |
5760 SNDRV_PCM_INFO_MMAP_VALID |
5761 SNDRV_PCM_INFO_NONINTERLEAVED |
5762 SNDRV_PCM_INFO_SYNC_START),
5763 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5764 .rates = (SNDRV_PCM_RATE_32000 |
5765 SNDRV_PCM_RATE_44100 |
5766 SNDRV_PCM_RATE_48000 |
5767 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005768 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5769 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005770 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005771 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005772 .channels_min = 1,
5773 .channels_max = HDSPM_MAX_CHANNELS,
5774 .buffer_bytes_max =
5775 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005776 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005777 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005778 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005779 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005780 .fifo_size = 0
5781};
5782
Adrian Knoth0dca1792011-01-26 19:32:14 +01005783static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5784 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005785{
Takashi Iwai98274f02005-11-17 14:52:34 +01005786 struct hdspm *hdspm = rule->private;
5787 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005788 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005789 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005790 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5791
Adrian Knoth0dca1792011-01-26 19:32:14 +01005792 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005793 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005794 .min = hdspm->qs_in_channels,
5795 .max = hdspm->qs_in_channels,
5796 .integer = 1,
5797 };
5798 return snd_interval_refine(c, &t);
5799 } else if (r->min > 48000 && r->max <= 96000) {
5800 struct snd_interval t = {
5801 .min = hdspm->ds_in_channels,
5802 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005803 .integer = 1,
5804 };
5805 return snd_interval_refine(c, &t);
5806 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005807 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005808 .min = hdspm->ss_in_channels,
5809 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005810 .integer = 1,
5811 };
5812 return snd_interval_refine(c, &t);
5813 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005814
Takashi Iwai763f3562005-06-03 11:25:34 +02005815 return 0;
5816}
5817
Adrian Knoth0dca1792011-01-26 19:32:14 +01005818static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005819 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005820{
Takashi Iwai98274f02005-11-17 14:52:34 +01005821 struct hdspm *hdspm = rule->private;
5822 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005823 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005824 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005825 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5826
Adrian Knoth0dca1792011-01-26 19:32:14 +01005827 if (r->min > 96000 && r->max <= 192000) {
5828 struct snd_interval t = {
5829 .min = hdspm->qs_out_channels,
5830 .max = hdspm->qs_out_channels,
5831 .integer = 1,
5832 };
5833 return snd_interval_refine(c, &t);
5834 } else if (r->min > 48000 && r->max <= 96000) {
5835 struct snd_interval t = {
5836 .min = hdspm->ds_out_channels,
5837 .max = hdspm->ds_out_channels,
5838 .integer = 1,
5839 };
5840 return snd_interval_refine(c, &t);
5841 } else if (r->max < 64000) {
5842 struct snd_interval t = {
5843 .min = hdspm->ss_out_channels,
5844 .max = hdspm->ss_out_channels,
5845 .integer = 1,
5846 };
5847 return snd_interval_refine(c, &t);
5848 } else {
5849 }
5850 return 0;
5851}
5852
5853static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5854 struct snd_pcm_hw_rule * rule)
5855{
5856 struct hdspm *hdspm = rule->private;
5857 struct snd_interval *c =
5858 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5859 struct snd_interval *r =
5860 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5861
5862 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005863 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005864 .min = 32000,
5865 .max = 48000,
5866 .integer = 1,
5867 };
5868 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005869 } else if (c->max <= hdspm->qs_in_channels) {
5870 struct snd_interval t = {
5871 .min = 128000,
5872 .max = 192000,
5873 .integer = 1,
5874 };
5875 return snd_interval_refine(r, &t);
5876 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005877 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005878 .min = 64000,
5879 .max = 96000,
5880 .integer = 1,
5881 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005882 return snd_interval_refine(r, &t);
5883 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005884
5885 return 0;
5886}
5887static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5888 struct snd_pcm_hw_rule *rule)
5889{
5890 struct hdspm *hdspm = rule->private;
5891 struct snd_interval *c =
5892 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5893 struct snd_interval *r =
5894 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5895
5896 if (c->min >= hdspm->ss_out_channels) {
5897 struct snd_interval t = {
5898 .min = 32000,
5899 .max = 48000,
5900 .integer = 1,
5901 };
5902 return snd_interval_refine(r, &t);
5903 } else if (c->max <= hdspm->qs_out_channels) {
5904 struct snd_interval t = {
5905 .min = 128000,
5906 .max = 192000,
5907 .integer = 1,
5908 };
5909 return snd_interval_refine(r, &t);
5910 } else if (c->max <= hdspm->ds_out_channels) {
5911 struct snd_interval t = {
5912 .min = 64000,
5913 .max = 96000,
5914 .integer = 1,
5915 };
5916 return snd_interval_refine(r, &t);
5917 }
5918
Takashi Iwai763f3562005-06-03 11:25:34 +02005919 return 0;
5920}
5921
Adrian Knoth0dca1792011-01-26 19:32:14 +01005922static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005923 struct snd_pcm_hw_rule *rule)
5924{
5925 unsigned int list[3];
5926 struct hdspm *hdspm = rule->private;
5927 struct snd_interval *c = hw_param_interval(params,
5928 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005929
5930 list[0] = hdspm->qs_in_channels;
5931 list[1] = hdspm->ds_in_channels;
5932 list[2] = hdspm->ss_in_channels;
5933 return snd_interval_list(c, 3, list, 0);
5934}
5935
5936static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5937 struct snd_pcm_hw_rule *rule)
5938{
5939 unsigned int list[3];
5940 struct hdspm *hdspm = rule->private;
5941 struct snd_interval *c = hw_param_interval(params,
5942 SNDRV_PCM_HW_PARAM_CHANNELS);
5943
5944 list[0] = hdspm->qs_out_channels;
5945 list[1] = hdspm->ds_out_channels;
5946 list[2] = hdspm->ss_out_channels;
5947 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005948}
5949
5950
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005951static unsigned int hdspm_aes32_sample_rates[] = {
5952 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5953};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005954
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005955static struct snd_pcm_hw_constraint_list
5956hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005957 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5958 .list = hdspm_aes32_sample_rates,
5959 .mask = 0
5960};
5961
Takashi Iwai98274f02005-11-17 14:52:34 +01005962static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005963{
Takashi Iwai98274f02005-11-17 14:52:34 +01005964 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5965 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005966
Takashi Iwai763f3562005-06-03 11:25:34 +02005967 spin_lock_irq(&hdspm->lock);
5968
5969 snd_pcm_set_sync(substream);
5970
Adrian Knoth0dca1792011-01-26 19:32:14 +01005971
Takashi Iwai763f3562005-06-03 11:25:34 +02005972 runtime->hw = snd_hdspm_playback_subinfo;
5973
5974 if (hdspm->capture_substream == NULL)
5975 hdspm_stop_audio(hdspm);
5976
5977 hdspm->playback_pid = current->pid;
5978 hdspm->playback_substream = substream;
5979
5980 spin_unlock_irq(&hdspm->lock);
5981
5982 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005983 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005984
Adrian Knoth0dca1792011-01-26 19:32:14 +01005985 switch (hdspm->io_type) {
5986 case AIO:
5987 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005988 snd_pcm_hw_constraint_minmax(runtime,
5989 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5990 32, 4096);
5991 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
5992 snd_pcm_hw_constraint_minmax(runtime,
5993 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5994 16384, 16384);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005995 break;
5996
5997 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005998 snd_pcm_hw_constraint_minmax(runtime,
5999 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6000 64, 8192);
6001 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006002 }
6003
6004 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02006005 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006006 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6007 &hdspm_hw_constraints_aes32_sample_rates);
6008 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006009 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006010 snd_hdspm_hw_rule_rate_out_channels, hdspm,
6011 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006012 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006013
6014 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6015 snd_hdspm_hw_rule_out_channels, hdspm,
6016 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6017
6018 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6019 snd_hdspm_hw_rule_out_channels_rate, hdspm,
6020 SNDRV_PCM_HW_PARAM_RATE, -1);
6021
Takashi Iwai763f3562005-06-03 11:25:34 +02006022 return 0;
6023}
6024
Takashi Iwai98274f02005-11-17 14:52:34 +01006025static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006026{
Takashi Iwai98274f02005-11-17 14:52:34 +01006027 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006028
6029 spin_lock_irq(&hdspm->lock);
6030
6031 hdspm->playback_pid = -1;
6032 hdspm->playback_substream = NULL;
6033
6034 spin_unlock_irq(&hdspm->lock);
6035
6036 return 0;
6037}
6038
6039
Takashi Iwai98274f02005-11-17 14:52:34 +01006040static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006041{
Takashi Iwai98274f02005-11-17 14:52:34 +01006042 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6043 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02006044
6045 spin_lock_irq(&hdspm->lock);
6046 snd_pcm_set_sync(substream);
6047 runtime->hw = snd_hdspm_capture_subinfo;
6048
6049 if (hdspm->playback_substream == NULL)
6050 hdspm_stop_audio(hdspm);
6051
6052 hdspm->capture_pid = current->pid;
6053 hdspm->capture_substream = substream;
6054
6055 spin_unlock_irq(&hdspm->lock);
6056
6057 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02006058 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
6059
Adrian Knoth0dca1792011-01-26 19:32:14 +01006060 switch (hdspm->io_type) {
6061 case AIO:
6062 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02006063 snd_pcm_hw_constraint_minmax(runtime,
6064 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6065 32, 4096);
6066 snd_pcm_hw_constraint_minmax(runtime,
6067 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6068 16384, 16384);
6069 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006070
6071 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02006072 snd_pcm_hw_constraint_minmax(runtime,
6073 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6074 64, 8192);
6075 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006076 }
6077
6078 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02006079 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006080 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6081 &hdspm_hw_constraints_aes32_sample_rates);
6082 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006083 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006084 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6085 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006086 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006087
6088 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6089 snd_hdspm_hw_rule_in_channels, hdspm,
6090 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6091
6092 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6093 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6094 SNDRV_PCM_HW_PARAM_RATE, -1);
6095
Takashi Iwai763f3562005-06-03 11:25:34 +02006096 return 0;
6097}
6098
Takashi Iwai98274f02005-11-17 14:52:34 +01006099static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006100{
Takashi Iwai98274f02005-11-17 14:52:34 +01006101 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006102
6103 spin_lock_irq(&hdspm->lock);
6104
6105 hdspm->capture_pid = -1;
6106 hdspm->capture_substream = NULL;
6107
6108 spin_unlock_irq(&hdspm->lock);
6109 return 0;
6110}
6111
Adrian Knoth0dca1792011-01-26 19:32:14 +01006112static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02006113{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006114 /* we have nothing to initialize but the call is required */
6115 return 0;
6116}
6117
6118static inline int copy_u32_le(void __user *dest, void __iomem *src)
6119{
6120 u32 val = readl(src);
6121 return copy_to_user(dest, &val, 4);
6122}
6123
6124static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006125 unsigned int cmd, unsigned long arg)
Adrian Knoth0dca1792011-01-26 19:32:14 +01006126{
6127 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006128 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01006129 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006130 struct hdspm_config info;
6131 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01006132 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006133 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006134 struct hdspm_ltc ltc;
6135 unsigned int statusregister;
6136 long unsigned int s;
6137 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02006138
6139 switch (cmd) {
6140
Takashi Iwai763f3562005-06-03 11:25:34 +02006141 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006142 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006143 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006144 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006145 readl(hdspm->iobase +
6146 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006147 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006148 readl(hdspm->iobase +
6149 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006150 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006151 readl(hdspm->iobase +
6152 HDSPM_MADI_OUTPUT_PEAK + i*4);
6153
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006154 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006155 ((uint64_t) readl(hdspm->iobase +
6156 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6157 (uint64_t) readl(hdspm->iobase +
6158 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006159 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006160 ((uint64_t)readl(hdspm->iobase +
6161 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6162 (uint64_t)readl(hdspm->iobase +
6163 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006164 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006165 ((uint64_t)readl(hdspm->iobase +
6166 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6167 (uint64_t)readl(hdspm->iobase +
6168 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6169 }
6170
6171 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006172 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006173 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006174 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006175 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006176 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006177 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006178 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006179
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006180 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01006181 if (0 != s) {
6182 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6183 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6184 */
Takashi Iwai763f3562005-06-03 11:25:34 +02006185 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006186 }
6187 break;
6188
6189 case SNDRV_HDSPM_IOCTL_GET_LTC:
6190 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6191 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6192 if (i & HDSPM_TCO1_LTC_Input_valid) {
6193 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6194 HDSPM_TCO1_LTC_Format_MSB)) {
6195 case 0:
6196 ltc.format = fps_24;
6197 break;
6198 case HDSPM_TCO1_LTC_Format_LSB:
6199 ltc.format = fps_25;
6200 break;
6201 case HDSPM_TCO1_LTC_Format_MSB:
6202 ltc.format = fps_2997;
6203 break;
6204 default:
6205 ltc.format = 30;
6206 break;
6207 }
6208 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6209 ltc.frame = drop_frame;
6210 } else {
6211 ltc.frame = full_frame;
6212 }
6213 } else {
6214 ltc.format = format_invalid;
6215 ltc.frame = frame_invalid;
6216 }
6217 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6218 ltc.input_format = ntsc;
6219 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6220 ltc.input_format = pal;
6221 } else {
6222 ltc.input_format = no_video;
6223 }
6224
6225 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6226 if (0 != s) {
6227 /*
6228 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02006229 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006230 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006231
6232 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02006233
Adrian Knoth0dca1792011-01-26 19:32:14 +01006234 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02006235
Adrian Knoth4ab69a22011-02-23 11:43:14 +01006236 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02006237 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006238 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6239 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006240
6241 info.system_sample_rate = hdspm->system_sample_rate;
6242 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006243 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006244 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6245 info.clock_source = hdspm_clock_source(hdspm);
6246 info.autosync_ref = hdspm_autosync_ref(hdspm);
Adrian Knothc9e16682012-12-03 14:55:50 +01006247 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
Takashi Iwai763f3562005-06-03 11:25:34 +02006248 info.passthru = 0;
6249 spin_unlock_irq(&hdspm->lock);
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006250 if (copy_to_user(argp, &info, sizeof(info)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006251 return -EFAULT;
6252 break;
6253
Adrian Knoth0dca1792011-01-26 19:32:14 +01006254 case SNDRV_HDSPM_IOCTL_GET_STATUS:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006255 memset(&status, 0, sizeof(status));
6256
Adrian Knoth0dca1792011-01-26 19:32:14 +01006257 status.card_type = hdspm->io_type;
6258
6259 status.autosync_source = hdspm_autosync_ref(hdspm);
6260
6261 status.card_clock = 110069313433624ULL;
6262 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6263
6264 switch (hdspm->io_type) {
6265 case MADI:
6266 case MADIface:
6267 status.card_specific.madi.sync_wc =
6268 hdspm_wc_sync_check(hdspm);
6269 status.card_specific.madi.sync_madi =
6270 hdspm_madi_sync_check(hdspm);
6271 status.card_specific.madi.sync_tco =
6272 hdspm_tco_sync_check(hdspm);
6273 status.card_specific.madi.sync_in =
6274 hdspm_sync_in_sync_check(hdspm);
6275
6276 statusregister =
6277 hdspm_read(hdspm, HDSPM_statusRegister);
6278 status.card_specific.madi.madi_input =
6279 (statusregister & HDSPM_AB_int) ? 1 : 0;
6280 status.card_specific.madi.channel_format =
Adrian Knoth9e6ff522011-10-27 21:57:52 +02006281 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006282 /* TODO: Mac driver sets it when f_s>48kHz */
6283 status.card_specific.madi.frame_format = 0;
6284
6285 default:
6286 break;
6287 }
6288
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006289 if (copy_to_user(argp, &status, sizeof(status)))
Adrian Knoth0dca1792011-01-26 19:32:14 +01006290 return -EFAULT;
6291
6292
6293 break;
6294
Takashi Iwai763f3562005-06-03 11:25:34 +02006295 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006296 memset(&hdspm_version, 0, sizeof(hdspm_version));
6297
Adrian Knoth0dca1792011-01-26 19:32:14 +01006298 hdspm_version.card_type = hdspm->io_type;
6299 strncpy(hdspm_version.cardname, hdspm->card_name,
6300 sizeof(hdspm_version.cardname));
Adrian Knoth7d53a632012-01-04 14:31:16 +01006301 hdspm_version.serial = hdspm->serial;
Takashi Iwai763f3562005-06-03 11:25:34 +02006302 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006303 hdspm_version.addons = 0;
6304 if (hdspm->tco)
6305 hdspm_version.addons |= HDSPM_ADDON_TCO;
6306
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006307 if (copy_to_user(argp, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006308 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006309 return -EFAULT;
6310 break;
6311
6312 case SNDRV_HDSPM_IOCTL_GET_MIXER:
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006313 if (copy_from_user(&mixer, argp, sizeof(mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006314 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006315 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006316 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006317 return -EFAULT;
6318 break;
6319
6320 default:
6321 return -EINVAL;
6322 }
6323 return 0;
6324}
6325
Takashi Iwai98274f02005-11-17 14:52:34 +01006326static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006327 .open = snd_hdspm_playback_open,
6328 .close = snd_hdspm_playback_release,
6329 .ioctl = snd_hdspm_ioctl,
6330 .hw_params = snd_hdspm_hw_params,
6331 .hw_free = snd_hdspm_hw_free,
6332 .prepare = snd_hdspm_prepare,
6333 .trigger = snd_hdspm_trigger,
6334 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006335 .page = snd_pcm_sgbuf_ops_page,
6336};
6337
Takashi Iwai98274f02005-11-17 14:52:34 +01006338static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006339 .open = snd_hdspm_capture_open,
6340 .close = snd_hdspm_capture_release,
6341 .ioctl = snd_hdspm_ioctl,
6342 .hw_params = snd_hdspm_hw_params,
6343 .hw_free = snd_hdspm_hw_free,
6344 .prepare = snd_hdspm_prepare,
6345 .trigger = snd_hdspm_trigger,
6346 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006347 .page = snd_pcm_sgbuf_ops_page,
6348};
6349
Bill Pembertone23e7a12012-12-06 12:35:10 -05006350static int snd_hdspm_create_hwdep(struct snd_card *card,
6351 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006352{
Takashi Iwai98274f02005-11-17 14:52:34 +01006353 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006354 int err;
6355
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006356 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6357 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006358 return err;
6359
6360 hdspm->hwdep = hw;
6361 hw->private_data = hdspm;
6362 strcpy(hw->name, "HDSPM hwdep interface");
6363
Adrian Knoth0dca1792011-01-26 19:32:14 +01006364 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006365 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth8de5d6f2012-03-08 15:38:04 +01006366 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006367 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006368
6369 return 0;
6370}
6371
6372
6373/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006374 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006375 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05006376static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006377{
6378 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006379 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006380 size_t wanted;
6381
6382 pcm = hdspm->pcm;
6383
Remy Bruno3cee5a62006-10-16 12:46:32 +02006384 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006385
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006386 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006387 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006388 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006389 snd_dma_pci_data(hdspm->pci),
6390 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006391 wanted);
6392 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006393 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006394
6395 return err;
6396 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006397 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006398
6399 return 0;
6400}
6401
Adrian Knoth0dca1792011-01-26 19:32:14 +01006402
6403static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006404 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006405 unsigned int reg, int channels)
6406{
6407 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006408
6409 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006410 for (i = 0; i < (channels * 16); i++)
6411 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006412 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006413}
6414
Adrian Knoth0dca1792011-01-26 19:32:14 +01006415
Takashi Iwai763f3562005-06-03 11:25:34 +02006416/* ------------- ALSA Devices ---------------------------- */
Bill Pembertone23e7a12012-12-06 12:35:10 -05006417static int snd_hdspm_create_pcm(struct snd_card *card,
6418 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006419{
Takashi Iwai98274f02005-11-17 14:52:34 +01006420 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006421 int err;
6422
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006423 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6424 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006425 return err;
6426
6427 hdspm->pcm = pcm;
6428 pcm->private_data = hdspm;
6429 strcpy(pcm->name, hdspm->card_name);
6430
6431 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6432 &snd_hdspm_playback_ops);
6433 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6434 &snd_hdspm_capture_ops);
6435
6436 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6437
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006438 err = snd_hdspm_preallocate_memory(hdspm);
6439 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006440 return err;
6441
6442 return 0;
6443}
6444
Takashi Iwai98274f02005-11-17 14:52:34 +01006445static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006446{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006447 int i;
6448
6449 for (i = 0; i < hdspm->midiPorts; i++)
6450 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006451}
6452
Bill Pembertone23e7a12012-12-06 12:35:10 -05006453static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6454 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006455{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006456 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006457
6458 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006459 err = snd_hdspm_create_pcm(card, hdspm);
6460 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006461 return err;
6462
Adrian Knoth0dca1792011-01-26 19:32:14 +01006463 i = 0;
6464 while (i < hdspm->midiPorts) {
6465 err = snd_hdspm_create_midi(card, hdspm, i);
6466 if (err < 0) {
6467 return err;
6468 }
6469 i++;
6470 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006471
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006472 err = snd_hdspm_create_controls(card, hdspm);
6473 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006474 return err;
6475
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006476 err = snd_hdspm_create_hwdep(card, hdspm);
6477 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006478 return err;
6479
6480 snd_printdd("proc init...\n");
6481 snd_hdspm_proc_init(hdspm);
6482
6483 hdspm->system_sample_rate = -1;
6484 hdspm->last_external_sample_rate = -1;
6485 hdspm->last_internal_sample_rate = -1;
6486 hdspm->playback_pid = -1;
6487 hdspm->capture_pid = -1;
6488 hdspm->capture_substream = NULL;
6489 hdspm->playback_substream = NULL;
6490
6491 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006492 err = snd_hdspm_set_defaults(hdspm);
6493 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006494 return err;
6495
6496 snd_printdd("Update mixer controls...\n");
6497 hdspm_update_simple_mixer_controls(hdspm);
6498
6499 snd_printdd("Initializeing complete ???\n");
6500
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006501 err = snd_card_register(card);
6502 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006503 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6504 return err;
6505 }
6506
6507 snd_printdd("... yes now\n");
6508
6509 return 0;
6510}
6511
Bill Pembertone23e7a12012-12-06 12:35:10 -05006512static int snd_hdspm_create(struct snd_card *card,
6513 struct hdspm *hdspm)
6514{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006515
Takashi Iwai763f3562005-06-03 11:25:34 +02006516 struct pci_dev *pci = hdspm->pci;
6517 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006518 unsigned long io_extent;
6519
6520 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006521 hdspm->card = card;
6522
6523 spin_lock_init(&hdspm->lock);
6524
Takashi Iwai763f3562005-06-03 11:25:34 +02006525 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006526 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006527
Takashi Iwai763f3562005-06-03 11:25:34 +02006528 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006529 strcpy(card->driver, "HDSPM");
6530
6531 switch (hdspm->firmware_rev) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01006532 case HDSPM_RAYDAT_REV:
6533 hdspm->io_type = RayDAT;
6534 hdspm->card_name = "RME RayDAT";
6535 hdspm->midiPorts = 2;
6536 break;
6537 case HDSPM_AIO_REV:
6538 hdspm->io_type = AIO;
6539 hdspm->card_name = "RME AIO";
6540 hdspm->midiPorts = 1;
6541 break;
6542 case HDSPM_MADIFACE_REV:
6543 hdspm->io_type = MADIface;
6544 hdspm->card_name = "RME MADIface";
6545 hdspm->midiPorts = 1;
6546 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006547 default:
Adrian Knothc09403d2011-10-27 21:57:54 +02006548 if ((hdspm->firmware_rev == 0xf0) ||
6549 ((hdspm->firmware_rev >= 0xe6) &&
6550 (hdspm->firmware_rev <= 0xea))) {
6551 hdspm->io_type = AES32;
6552 hdspm->card_name = "RME AES32";
6553 hdspm->midiPorts = 2;
Adrian Knoth05c7cc92011-11-21 16:15:36 +01006554 } else if ((hdspm->firmware_rev == 0xd2) ||
Adrian Knothc09403d2011-10-27 21:57:54 +02006555 ((hdspm->firmware_rev >= 0xc8) &&
6556 (hdspm->firmware_rev <= 0xcf))) {
6557 hdspm->io_type = MADI;
6558 hdspm->card_name = "RME MADI";
6559 hdspm->midiPorts = 3;
6560 } else {
6561 snd_printk(KERN_ERR
6562 "HDSPM: unknown firmware revision %x\n",
Adrian Knoth5027f342011-02-28 15:14:49 +01006563 hdspm->firmware_rev);
Adrian Knothc09403d2011-10-27 21:57:54 +02006564 return -ENODEV;
6565 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02006566 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006567
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006568 err = pci_enable_device(pci);
6569 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006570 return err;
6571
6572 pci_set_master(hdspm->pci);
6573
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006574 err = pci_request_regions(pci, "hdspm");
6575 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006576 return err;
6577
6578 hdspm->port = pci_resource_start(pci, 0);
6579 io_extent = pci_resource_len(pci, 0);
6580
6581 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006582 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006583
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006584 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6585 if (!hdspm->iobase) {
6586 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006587 "unable to remap region 0x%lx-0x%lx\n",
6588 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006589 return -EBUSY;
6590 }
6591 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006592 (unsigned long)hdspm->iobase, hdspm->port,
6593 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006594
6595 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006596 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006597 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6598 return -EBUSY;
6599 }
6600
6601 snd_printdd("use IRQ %d\n", pci->irq);
6602
6603 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006604
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006605 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006606 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006607 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6608 if (!hdspm->mixer) {
6609 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006610 "unable to kmalloc Mixer memory of %d Bytes\n",
6611 (int)sizeof(struct hdspm_mixer));
Julia Lawallb17cbdd2012-08-19 09:02:54 +02006612 return -ENOMEM;
Takashi Iwai763f3562005-06-03 11:25:34 +02006613 }
6614
Adrian Knoth0dca1792011-01-26 19:32:14 +01006615 hdspm->port_names_in = NULL;
6616 hdspm->port_names_out = NULL;
6617
6618 switch (hdspm->io_type) {
6619 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006620 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6621 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6622 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006623
6624 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6625 channel_map_aes32;
6626 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6627 channel_map_aes32;
6628 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6629 channel_map_aes32;
6630 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6631 texts_ports_aes32;
6632 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6633 texts_ports_aes32;
6634 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6635 texts_ports_aes32;
6636
Adrian Knothd2d10a22011-02-28 15:14:47 +01006637 hdspm->max_channels_out = hdspm->max_channels_in =
6638 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006639 hdspm->port_names_in = hdspm->port_names_out =
6640 texts_ports_aes32;
6641 hdspm->channel_map_in = hdspm->channel_map_out =
6642 channel_map_aes32;
6643
Adrian Knoth0dca1792011-01-26 19:32:14 +01006644 break;
6645
6646 case MADI:
6647 case MADIface:
6648 hdspm->ss_in_channels = hdspm->ss_out_channels =
6649 MADI_SS_CHANNELS;
6650 hdspm->ds_in_channels = hdspm->ds_out_channels =
6651 MADI_DS_CHANNELS;
6652 hdspm->qs_in_channels = hdspm->qs_out_channels =
6653 MADI_QS_CHANNELS;
6654
6655 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6656 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006657 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006658 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006659 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006660 channel_map_unity_ss;
6661
6662 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6663 texts_ports_madi;
6664 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6665 texts_ports_madi;
6666 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6667 texts_ports_madi;
6668 break;
6669
6670 case AIO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006671 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6672 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6673 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6674 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6675 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6676 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6677
Adrian Knoth3de9db22013-07-05 11:28:02 +02006678 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6679 snd_printk(KERN_INFO "HDSPM: AEB input board found\n");
6680 hdspm->ss_in_channels += 4;
6681 hdspm->ds_in_channels += 4;
6682 hdspm->qs_in_channels += 4;
6683 }
6684
6685 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6686 snd_printk(KERN_INFO "HDSPM: AEB output board found\n");
6687 hdspm->ss_out_channels += 4;
6688 hdspm->ds_out_channels += 4;
6689 hdspm->qs_out_channels += 4;
6690 }
6691
Adrian Knoth0dca1792011-01-26 19:32:14 +01006692 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6693 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6694 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6695
6696 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6697 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6698 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6699
6700 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6701 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6702 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6703 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6704 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6705 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6706
6707 break;
6708
6709 case RayDAT:
6710 hdspm->ss_in_channels = hdspm->ss_out_channels =
6711 RAYDAT_SS_CHANNELS;
6712 hdspm->ds_in_channels = hdspm->ds_out_channels =
6713 RAYDAT_DS_CHANNELS;
6714 hdspm->qs_in_channels = hdspm->qs_out_channels =
6715 RAYDAT_QS_CHANNELS;
6716
6717 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6718 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6719
6720 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6721 channel_map_raydat_ss;
6722 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6723 channel_map_raydat_ds;
6724 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6725 channel_map_raydat_qs;
6726 hdspm->channel_map_in = hdspm->channel_map_out =
6727 channel_map_raydat_ss;
6728
6729 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6730 texts_ports_raydat_ss;
6731 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6732 texts_ports_raydat_ds;
6733 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6734 texts_ports_raydat_qs;
6735
6736
6737 break;
6738
6739 }
6740
6741 /* TCO detection */
6742 switch (hdspm->io_type) {
6743 case AIO:
6744 case RayDAT:
6745 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6746 HDSPM_s2_tco_detect) {
6747 hdspm->midiPorts++;
6748 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6749 GFP_KERNEL);
6750 if (NULL != hdspm->tco) {
6751 hdspm_tco_write(hdspm);
6752 }
6753 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6754 } else {
6755 hdspm->tco = NULL;
6756 }
6757 break;
6758
6759 case MADI:
6760 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6761 hdspm->midiPorts++;
6762 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6763 GFP_KERNEL);
6764 if (NULL != hdspm->tco) {
6765 hdspm_tco_write(hdspm);
6766 }
Adrian Knothe71b95a2013-07-05 11:28:06 +02006767 snd_printk(KERN_INFO "HDSPM: MADI/AES TCO module found\n");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006768 } else {
6769 hdspm->tco = NULL;
6770 }
6771 break;
6772
6773 default:
6774 hdspm->tco = NULL;
6775 }
6776
6777 /* texts */
6778 switch (hdspm->io_type) {
6779 case AES32:
6780 if (hdspm->tco) {
6781 hdspm->texts_autosync = texts_autosync_aes_tco;
Adrian Knothe71b95a2013-07-05 11:28:06 +02006782 hdspm->texts_autosync_items =
6783 ARRAY_SIZE(texts_autosync_aes_tco);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006784 } else {
6785 hdspm->texts_autosync = texts_autosync_aes;
Adrian Knothe71b95a2013-07-05 11:28:06 +02006786 hdspm->texts_autosync_items =
6787 ARRAY_SIZE(texts_autosync_aes);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006788 }
6789 break;
6790
6791 case MADI:
6792 if (hdspm->tco) {
6793 hdspm->texts_autosync = texts_autosync_madi_tco;
6794 hdspm->texts_autosync_items = 4;
6795 } else {
6796 hdspm->texts_autosync = texts_autosync_madi;
6797 hdspm->texts_autosync_items = 3;
6798 }
6799 break;
6800
6801 case MADIface:
6802
6803 break;
6804
6805 case RayDAT:
6806 if (hdspm->tco) {
6807 hdspm->texts_autosync = texts_autosync_raydat_tco;
6808 hdspm->texts_autosync_items = 9;
6809 } else {
6810 hdspm->texts_autosync = texts_autosync_raydat;
6811 hdspm->texts_autosync_items = 8;
6812 }
6813 break;
6814
6815 case AIO:
6816 if (hdspm->tco) {
6817 hdspm->texts_autosync = texts_autosync_aio_tco;
6818 hdspm->texts_autosync_items = 6;
6819 } else {
6820 hdspm->texts_autosync = texts_autosync_aio;
6821 hdspm->texts_autosync_items = 5;
6822 }
6823 break;
6824
6825 }
6826
6827 tasklet_init(&hdspm->midi_tasklet,
6828 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006829
Adrian Knothf7de8ba2012-01-10 20:58:40 +01006830
6831 if (hdspm->io_type != MADIface) {
6832 hdspm->serial = (hdspm_read(hdspm,
6833 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6834 /* id contains either a user-provided value or the default
6835 * NULL. If it's the default, we're safe to
6836 * fill card->id with the serial number.
6837 *
6838 * If the serial number is 0xFFFFFF, then we're dealing with
6839 * an old PCI revision that comes without a sane number. In
6840 * this case, we don't set card->id to avoid collisions
6841 * when running with multiple cards.
6842 */
6843 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6844 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6845 snd_card_set_id(card, card->id);
6846 }
6847 }
6848
Takashi Iwai763f3562005-06-03 11:25:34 +02006849 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006850 err = snd_hdspm_create_alsa_devices(card, hdspm);
6851 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006852 return err;
6853
6854 snd_hdspm_initialize_midi_flush(hdspm);
6855
6856 return 0;
6857}
6858
Adrian Knoth0dca1792011-01-26 19:32:14 +01006859
Takashi Iwai98274f02005-11-17 14:52:34 +01006860static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006861{
6862
6863 if (hdspm->port) {
6864
6865 /* stop th audio, and cancel all interrupts */
6866 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006867 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006868 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6869 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006870 hdspm_write(hdspm, HDSPM_controlRegister,
6871 hdspm->control_register);
6872 }
6873
6874 if (hdspm->irq >= 0)
6875 free_irq(hdspm->irq, (void *) hdspm);
6876
Jesper Juhlfc584222005-10-24 15:11:28 +02006877 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006878
6879 if (hdspm->iobase)
6880 iounmap(hdspm->iobase);
6881
Takashi Iwai763f3562005-06-03 11:25:34 +02006882 if (hdspm->port)
6883 pci_release_regions(hdspm->pci);
6884
6885 pci_disable_device(hdspm->pci);
6886 return 0;
6887}
6888
Adrian Knoth0dca1792011-01-26 19:32:14 +01006889
Takashi Iwai98274f02005-11-17 14:52:34 +01006890static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006891{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006892 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006893
6894 if (hdspm)
6895 snd_hdspm_free(hdspm);
6896}
6897
Adrian Knoth0dca1792011-01-26 19:32:14 +01006898
Bill Pembertone23e7a12012-12-06 12:35:10 -05006899static int snd_hdspm_probe(struct pci_dev *pci,
6900 const struct pci_device_id *pci_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02006901{
6902 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006903 struct hdspm *hdspm;
6904 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006905 int err;
6906
6907 if (dev >= SNDRV_CARDS)
6908 return -ENODEV;
6909 if (!enable[dev]) {
6910 dev++;
6911 return -ENOENT;
6912 }
6913
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006914 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006915 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006916 if (err < 0)
6917 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006918
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006919 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006920 card->private_free = snd_hdspm_card_free;
6921 hdspm->dev = dev;
6922 hdspm->pci = pci;
6923
Takashi Iwaic187c042007-02-19 15:27:33 +01006924 snd_card_set_dev(card, &pci->dev);
6925
Adrian Knoth0dca1792011-01-26 19:32:14 +01006926 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006927 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006928 snd_card_free(card);
6929 return err;
6930 }
6931
Adrian Knoth0dca1792011-01-26 19:32:14 +01006932 if (hdspm->io_type != MADIface) {
6933 sprintf(card->shortname, "%s_%x",
6934 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006935 hdspm->serial);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006936 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6937 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006938 hdspm->serial,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006939 hdspm->port, hdspm->irq);
6940 } else {
6941 sprintf(card->shortname, "%s", hdspm->card_name);
6942 sprintf(card->longname, "%s at 0x%lx, irq %d",
6943 hdspm->card_name, hdspm->port, hdspm->irq);
6944 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006945
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006946 err = snd_card_register(card);
6947 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006948 snd_card_free(card);
6949 return err;
6950 }
6951
6952 pci_set_drvdata(pci, card);
6953
6954 dev++;
6955 return 0;
6956}
6957
Bill Pembertone23e7a12012-12-06 12:35:10 -05006958static void snd_hdspm_remove(struct pci_dev *pci)
Takashi Iwai763f3562005-06-03 11:25:34 +02006959{
6960 snd_card_free(pci_get_drvdata(pci));
Takashi Iwai763f3562005-06-03 11:25:34 +02006961}
6962
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006963static struct pci_driver hdspm_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006964 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006965 .id_table = snd_hdspm_ids,
6966 .probe = snd_hdspm_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05006967 .remove = snd_hdspm_remove,
Takashi Iwai763f3562005-06-03 11:25:34 +02006968};
6969
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006970module_pci_driver(hdspm_driver);