blob: c0143cf83cf35ba871c3a3474c15d75bb1390aea [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;
Adrian Knotha57fea82013-07-05 11:28:11 +02002181 case AES32:
2182 status = hdspm_read(hdspm, HDSPM_statusRegister);
2183 return (status >> HDSPM_AES32_wcFreq_bit) & 0xF;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002184 default:
2185 break;
2186 }
2187
2188
2189 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002190}
2191
Adrian Knoth0dca1792011-01-26 19:32:14 +01002192
2193/**
2194 * Returns the TCO sample rate class for the given card.
2195 **/
2196static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2197{
2198 int status;
2199
2200 if (hdspm->tco) {
2201 switch (hdspm->io_type) {
2202 case RayDAT:
2203 case AIO:
2204 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2205 return (status >> 20) & 0xF;
2206 break;
Adrian Knoth051c44f2013-07-05 11:28:12 +02002207 case AES32:
2208 status = hdspm_read(hdspm, HDSPM_statusRegister);
2209 return (status >> 1) & 0xF;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002210 default:
2211 break;
2212 }
2213 }
2214
2215 return 0;
2216}
2217
2218
2219/**
2220 * Returns the SYNC_IN sample rate class for the given card.
2221 **/
2222static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2223{
2224 int status;
2225
2226 if (hdspm->tco) {
2227 switch (hdspm->io_type) {
2228 case RayDAT:
2229 case AIO:
2230 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2231 return (status >> 12) & 0xF;
2232 break;
2233 default:
2234 break;
2235 }
2236 }
2237
2238 return 0;
2239}
2240
Adrian Knothd3c36ed2013-07-05 11:28:09 +02002241/**
2242 * Returns the AES sample rate class for the given card.
2243 **/
2244static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index)
2245{
2246 int timecode;
2247
2248 switch (hdspm->io_type) {
2249 case AES32:
2250 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
2251 return (timecode >> (4*index)) & 0xF;
2252 break;
2253 default:
2254 break;
2255 }
2256 return 0;
2257}
Adrian Knoth0dca1792011-01-26 19:32:14 +01002258
2259/**
2260 * Returns the sample rate class for input source <idx> for
2261 * 'new style' cards like the AIO and RayDAT.
2262 **/
2263static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2264{
2265 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2266
2267 return (status >> (idx*4)) & 0xF;
2268}
2269
Adrian Knoth8cea5712013-07-05 11:27:59 +02002270static void snd_hdspm_set_infotext(struct snd_ctl_elem_info *uinfo,
2271 char **texts, const int count)
2272{
2273 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2274 uinfo->count = 1;
2275 uinfo->value.enumerated.items = count;
2276 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2277 uinfo->value.enumerated.item =
2278 uinfo->value.enumerated.items - 1;
2279 strcpy(uinfo->value.enumerated.name,
2280 texts[uinfo->value.enumerated.item]);
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002281}
2282
Adrian Knoth8cea5712013-07-05 11:27:59 +02002283#define ENUMERATED_CTL_INFO(info, texts) \
2284 snd_hdspm_set_infotext(info, texts, ARRAY_SIZE(texts))
2285
Adrian Knoth0dca1792011-01-26 19:32:14 +01002286
2287
2288#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2289{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2290 .name = xname, \
2291 .private_value = xindex, \
2292 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2293 .info = snd_hdspm_info_autosync_sample_rate, \
2294 .get = snd_hdspm_get_autosync_sample_rate \
2295}
2296
2297
Takashi Iwai98274f02005-11-17 14:52:34 +01002298static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002300{
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002301 ENUMERATED_CTL_INFO(uinfo, texts_freq);
Takashi Iwai763f3562005-06-03 11:25:34 +02002302 return 0;
2303}
2304
Adrian Knoth0dca1792011-01-26 19:32:14 +01002305
Takashi Iwai98274f02005-11-17 14:52:34 +01002306static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002308 ucontrol)
2309{
Takashi Iwai98274f02005-11-17 14:52:34 +01002310 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002311
Adrian Knoth0dca1792011-01-26 19:32:14 +01002312 switch (hdspm->io_type) {
2313 case RayDAT:
2314 switch (kcontrol->private_value) {
2315 case 0:
2316 ucontrol->value.enumerated.item[0] =
2317 hdspm_get_wc_sample_rate(hdspm);
2318 break;
2319 case 7:
2320 ucontrol->value.enumerated.item[0] =
2321 hdspm_get_tco_sample_rate(hdspm);
2322 break;
2323 case 8:
2324 ucontrol->value.enumerated.item[0] =
2325 hdspm_get_sync_in_sample_rate(hdspm);
2326 break;
2327 default:
2328 ucontrol->value.enumerated.item[0] =
2329 hdspm_get_s1_sample_rate(hdspm,
2330 kcontrol->private_value-1);
2331 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002332 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002333
Adrian Knoth0dca1792011-01-26 19:32:14 +01002334 case AIO:
2335 switch (kcontrol->private_value) {
2336 case 0: /* WC */
2337 ucontrol->value.enumerated.item[0] =
2338 hdspm_get_wc_sample_rate(hdspm);
2339 break;
2340 case 4: /* TCO */
2341 ucontrol->value.enumerated.item[0] =
2342 hdspm_get_tco_sample_rate(hdspm);
2343 break;
2344 case 5: /* SYNC_IN */
2345 ucontrol->value.enumerated.item[0] =
2346 hdspm_get_sync_in_sample_rate(hdspm);
2347 break;
2348 default:
2349 ucontrol->value.enumerated.item[0] =
2350 hdspm_get_s1_sample_rate(hdspm,
Adrian Knoth1cb7dbf2013-07-05 11:28:03 +02002351 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002352 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002353 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002354
2355 case AES32:
2356
2357 switch (kcontrol->private_value) {
2358 case 0: /* WC */
2359 ucontrol->value.enumerated.item[0] =
2360 hdspm_get_wc_sample_rate(hdspm);
2361 break;
2362 case 9: /* TCO */
2363 ucontrol->value.enumerated.item[0] =
2364 hdspm_get_tco_sample_rate(hdspm);
2365 break;
2366 case 10: /* SYNC_IN */
2367 ucontrol->value.enumerated.item[0] =
2368 hdspm_get_sync_in_sample_rate(hdspm);
2369 break;
2370 default: /* AES1 to AES8 */
2371 ucontrol->value.enumerated.item[0] =
2372 hdspm_get_s1_sample_rate(hdspm,
2373 kcontrol->private_value-1);
2374 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002375 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002376 break;
Adrian Knothb8812c52012-10-19 17:42:26 +02002377
2378 case MADI:
2379 case MADIface:
2380 {
2381 int rate = hdspm_external_sample_rate(hdspm);
2382 int i, selected_rate = 0;
2383 for (i = 1; i < 10; i++)
2384 if (HDSPM_bit2freq(i) == rate) {
2385 selected_rate = i;
2386 break;
2387 }
2388 ucontrol->value.enumerated.item[0] = selected_rate;
2389 }
2390 break;
2391
Takashi Iwai763f3562005-06-03 11:25:34 +02002392 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002393 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002394 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002395
Takashi Iwai763f3562005-06-03 11:25:34 +02002396 return 0;
2397}
2398
Adrian Knoth0dca1792011-01-26 19:32:14 +01002399
Takashi Iwai763f3562005-06-03 11:25:34 +02002400#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002401{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2402 .name = xname, \
2403 .index = xindex, \
2404 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2405 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2406 .info = snd_hdspm_info_system_clock_mode, \
2407 .get = snd_hdspm_get_system_clock_mode, \
2408 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002409}
2410
2411
Adrian Knoth0dca1792011-01-26 19:32:14 +01002412/**
2413 * Returns the system clock mode for the given card.
2414 * @returns 0 - master, 1 - slave
2415 **/
2416static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002417{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002418 switch (hdspm->io_type) {
2419 case AIO:
2420 case RayDAT:
2421 if (hdspm->settings_register & HDSPM_c0Master)
2422 return 0;
2423 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002424
Adrian Knoth0dca1792011-01-26 19:32:14 +01002425 default:
2426 if (hdspm->control_register & HDSPM_ClockModeMaster)
2427 return 0;
2428 }
2429
Takashi Iwai763f3562005-06-03 11:25:34 +02002430 return 1;
2431}
2432
Adrian Knoth0dca1792011-01-26 19:32:14 +01002433
2434/**
2435 * Sets the system clock mode.
2436 * @param mode 0 - master, 1 - slave
2437 **/
2438static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2439{
Adrian Knoth34be7eb2013-07-05 11:27:56 +02002440 hdspm_set_toggle_setting(hdspm,
2441 (hdspm_is_raydat_or_aio(hdspm)) ?
2442 HDSPM_c0Master : HDSPM_ClockModeMaster,
2443 (0 == mode));
Adrian Knoth0dca1792011-01-26 19:32:14 +01002444}
2445
2446
Takashi Iwai98274f02005-11-17 14:52:34 +01002447static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002449{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002450 static char *texts[] = { "Master", "AutoSync" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002451 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02002452 return 0;
2453}
2454
Takashi Iwai98274f02005-11-17 14:52:34 +01002455static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002457{
Takashi Iwai98274f02005-11-17 14:52:34 +01002458 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002459
Adrian Knoth0dca1792011-01-26 19:32:14 +01002460 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002461 return 0;
2462}
2463
Adrian Knoth0dca1792011-01-26 19:32:14 +01002464static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
2466{
2467 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2468 int val;
2469
2470 if (!snd_hdspm_use_is_exclusive(hdspm))
2471 return -EBUSY;
2472
2473 val = ucontrol->value.enumerated.item[0];
2474 if (val < 0)
2475 val = 0;
2476 else if (val > 1)
2477 val = 1;
2478
2479 hdspm_set_system_clock_mode(hdspm, val);
2480
2481 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002482}
2483
Adrian Knoth0dca1792011-01-26 19:32:14 +01002484
2485#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2486{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2487 .name = xname, \
2488 .index = xindex, \
2489 .info = snd_hdspm_info_clock_source, \
2490 .get = snd_hdspm_get_clock_source, \
2491 .put = snd_hdspm_put_clock_source \
2492}
2493
2494
Takashi Iwai98274f02005-11-17 14:52:34 +01002495static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002496{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002497 switch (hdspm->system_sample_rate) {
2498 case 32000: return 0;
2499 case 44100: return 1;
2500 case 48000: return 2;
2501 case 64000: return 3;
2502 case 88200: return 4;
2503 case 96000: return 5;
2504 case 128000: return 6;
2505 case 176400: return 7;
2506 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002507 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002508
2509 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002510}
2511
Takashi Iwai98274f02005-11-17 14:52:34 +01002512static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002513{
2514 int rate;
2515 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002516 case 0:
2517 rate = 32000; break;
2518 case 1:
2519 rate = 44100; break;
2520 case 2:
2521 rate = 48000; break;
2522 case 3:
2523 rate = 64000; break;
2524 case 4:
2525 rate = 88200; break;
2526 case 5:
2527 rate = 96000; break;
2528 case 6:
2529 rate = 128000; break;
2530 case 7:
2531 rate = 176400; break;
2532 case 8:
2533 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002534 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002535 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002536 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002537 hdspm_set_rate(hdspm, rate, 1);
2538 return 0;
2539}
2540
Takashi Iwai98274f02005-11-17 14:52:34 +01002541static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002543{
Takashi Iwai763f3562005-06-03 11:25:34 +02002544 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2545 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002546 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002547
2548 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2549 uinfo->value.enumerated.item =
2550 uinfo->value.enumerated.items - 1;
2551
2552 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002553 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002554
2555 return 0;
2556}
2557
Takashi Iwai98274f02005-11-17 14:52:34 +01002558static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002560{
Takashi Iwai98274f02005-11-17 14:52:34 +01002561 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002562
2563 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2564 return 0;
2565}
2566
Takashi Iwai98274f02005-11-17 14:52:34 +01002567static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002569{
Takashi Iwai98274f02005-11-17 14:52:34 +01002570 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002571 int change;
2572 int val;
2573
2574 if (!snd_hdspm_use_is_exclusive(hdspm))
2575 return -EBUSY;
2576 val = ucontrol->value.enumerated.item[0];
2577 if (val < 0)
2578 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002579 if (val > 9)
2580 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002581 spin_lock_irq(&hdspm->lock);
2582 if (val != hdspm_clock_source(hdspm))
2583 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2584 else
2585 change = 0;
2586 spin_unlock_irq(&hdspm->lock);
2587 return change;
2588}
2589
Adrian Knoth0dca1792011-01-26 19:32:14 +01002590
Takashi Iwai763f3562005-06-03 11:25:34 +02002591#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002592{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002593 .name = xname, \
2594 .index = xindex, \
2595 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2596 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2597 .info = snd_hdspm_info_pref_sync_ref, \
2598 .get = snd_hdspm_get_pref_sync_ref, \
2599 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002600}
2601
Adrian Knoth0dca1792011-01-26 19:32:14 +01002602
2603/**
2604 * Returns the current preferred sync reference setting.
2605 * The semantics of the return value are depending on the
2606 * card, please see the comments for clarification.
2607 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002608static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002609{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002610 switch (hdspm->io_type) {
2611 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002612 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002613 case 0: return 0; /* WC */
2614 case HDSPM_SyncRef0: return 1; /* AES 1 */
2615 case HDSPM_SyncRef1: return 2; /* AES 2 */
2616 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2617 case HDSPM_SyncRef2: return 4; /* AES 4 */
2618 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2619 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2620 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2621 return 7; /* AES 7 */
2622 case HDSPM_SyncRef3: return 8; /* AES 8 */
2623 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002624 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002625 break;
2626
2627 case MADI:
2628 case MADIface:
2629 if (hdspm->tco) {
2630 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2631 case 0: return 0; /* WC */
2632 case HDSPM_SyncRef0: return 1; /* MADI */
2633 case HDSPM_SyncRef1: return 2; /* TCO */
2634 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2635 return 3; /* SYNC_IN */
2636 }
2637 } else {
2638 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2639 case 0: return 0; /* WC */
2640 case HDSPM_SyncRef0: return 1; /* MADI */
2641 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2642 return 2; /* SYNC_IN */
2643 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002644 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002645 break;
2646
2647 case RayDAT:
2648 if (hdspm->tco) {
2649 switch ((hdspm->settings_register &
2650 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2651 case 0: return 0; /* WC */
2652 case 3: return 1; /* ADAT 1 */
2653 case 4: return 2; /* ADAT 2 */
2654 case 5: return 3; /* ADAT 3 */
2655 case 6: return 4; /* ADAT 4 */
2656 case 1: return 5; /* AES */
2657 case 2: return 6; /* SPDIF */
2658 case 9: return 7; /* TCO */
2659 case 10: return 8; /* SYNC_IN */
2660 }
2661 } else {
2662 switch ((hdspm->settings_register &
2663 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2664 case 0: return 0; /* WC */
2665 case 3: return 1; /* ADAT 1 */
2666 case 4: return 2; /* ADAT 2 */
2667 case 5: return 3; /* ADAT 3 */
2668 case 6: return 4; /* ADAT 4 */
2669 case 1: return 5; /* AES */
2670 case 2: return 6; /* SPDIF */
2671 case 10: return 7; /* SYNC_IN */
2672 }
2673 }
2674
2675 break;
2676
2677 case AIO:
2678 if (hdspm->tco) {
2679 switch ((hdspm->settings_register &
2680 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2681 case 0: return 0; /* WC */
2682 case 3: return 1; /* ADAT */
2683 case 1: return 2; /* AES */
2684 case 2: return 3; /* SPDIF */
2685 case 9: return 4; /* TCO */
2686 case 10: return 5; /* SYNC_IN */
2687 }
2688 } else {
2689 switch ((hdspm->settings_register &
2690 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2691 case 0: return 0; /* WC */
2692 case 3: return 1; /* ADAT */
2693 case 1: return 2; /* AES */
2694 case 2: return 3; /* SPDIF */
2695 case 10: return 4; /* SYNC_IN */
2696 }
2697 }
2698
2699 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002700 }
2701
Adrian Knoth0dca1792011-01-26 19:32:14 +01002702 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002703}
2704
Adrian Knoth0dca1792011-01-26 19:32:14 +01002705
2706/**
2707 * Set the preferred sync reference to <pref>. The semantics
2708 * of <pref> are depending on the card type, see the comments
2709 * for clarification.
2710 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002711static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002712{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002713 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002714
Adrian Knoth0dca1792011-01-26 19:32:14 +01002715 switch (hdspm->io_type) {
2716 case AES32:
2717 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002718 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002719 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002720 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002721 case 1: /* AES 1 */
2722 hdspm->control_register |= HDSPM_SyncRef0;
2723 break;
2724 case 2: /* AES 2 */
2725 hdspm->control_register |= HDSPM_SyncRef1;
2726 break;
2727 case 3: /* AES 3 */
2728 hdspm->control_register |=
2729 HDSPM_SyncRef1+HDSPM_SyncRef0;
2730 break;
2731 case 4: /* AES 4 */
2732 hdspm->control_register |= HDSPM_SyncRef2;
2733 break;
2734 case 5: /* AES 5 */
2735 hdspm->control_register |=
2736 HDSPM_SyncRef2+HDSPM_SyncRef0;
2737 break;
2738 case 6: /* AES 6 */
2739 hdspm->control_register |=
2740 HDSPM_SyncRef2+HDSPM_SyncRef1;
2741 break;
2742 case 7: /* AES 7 */
2743 hdspm->control_register |=
2744 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2745 break;
2746 case 8: /* AES 8 */
2747 hdspm->control_register |= HDSPM_SyncRef3;
2748 break;
2749 case 9: /* TCO */
2750 hdspm->control_register |=
2751 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002752 break;
2753 default:
2754 return -1;
2755 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002756
2757 break;
2758
2759 case MADI:
2760 case MADIface:
2761 hdspm->control_register &= ~HDSPM_SyncRefMask;
2762 if (hdspm->tco) {
2763 switch (pref) {
2764 case 0: /* WC */
2765 break;
2766 case 1: /* MADI */
2767 hdspm->control_register |= HDSPM_SyncRef0;
2768 break;
2769 case 2: /* TCO */
2770 hdspm->control_register |= HDSPM_SyncRef1;
2771 break;
2772 case 3: /* SYNC_IN */
2773 hdspm->control_register |=
2774 HDSPM_SyncRef0+HDSPM_SyncRef1;
2775 break;
2776 default:
2777 return -1;
2778 }
2779 } else {
2780 switch (pref) {
2781 case 0: /* WC */
2782 break;
2783 case 1: /* MADI */
2784 hdspm->control_register |= HDSPM_SyncRef0;
2785 break;
2786 case 2: /* SYNC_IN */
2787 hdspm->control_register |=
2788 HDSPM_SyncRef0+HDSPM_SyncRef1;
2789 break;
2790 default:
2791 return -1;
2792 }
2793 }
2794
2795 break;
2796
2797 case RayDAT:
2798 if (hdspm->tco) {
2799 switch (pref) {
2800 case 0: p = 0; break; /* WC */
2801 case 1: p = 3; break; /* ADAT 1 */
2802 case 2: p = 4; break; /* ADAT 2 */
2803 case 3: p = 5; break; /* ADAT 3 */
2804 case 4: p = 6; break; /* ADAT 4 */
2805 case 5: p = 1; break; /* AES */
2806 case 6: p = 2; break; /* SPDIF */
2807 case 7: p = 9; break; /* TCO */
2808 case 8: p = 10; break; /* SYNC_IN */
2809 default: return -1;
2810 }
2811 } else {
2812 switch (pref) {
2813 case 0: p = 0; break; /* WC */
2814 case 1: p = 3; break; /* ADAT 1 */
2815 case 2: p = 4; break; /* ADAT 2 */
2816 case 3: p = 5; break; /* ADAT 3 */
2817 case 4: p = 6; break; /* ADAT 4 */
2818 case 5: p = 1; break; /* AES */
2819 case 6: p = 2; break; /* SPDIF */
2820 case 7: p = 10; break; /* SYNC_IN */
2821 default: return -1;
2822 }
2823 }
2824 break;
2825
2826 case AIO:
2827 if (hdspm->tco) {
2828 switch (pref) {
2829 case 0: p = 0; break; /* WC */
2830 case 1: p = 3; break; /* ADAT */
2831 case 2: p = 1; break; /* AES */
2832 case 3: p = 2; break; /* SPDIF */
2833 case 4: p = 9; break; /* TCO */
2834 case 5: p = 10; break; /* SYNC_IN */
2835 default: return -1;
2836 }
2837 } else {
2838 switch (pref) {
2839 case 0: p = 0; break; /* WC */
2840 case 1: p = 3; break; /* ADAT */
2841 case 2: p = 1; break; /* AES */
2842 case 3: p = 2; break; /* SPDIF */
2843 case 4: p = 10; break; /* SYNC_IN */
2844 default: return -1;
2845 }
2846 }
2847 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002848 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002849
2850 switch (hdspm->io_type) {
2851 case RayDAT:
2852 case AIO:
2853 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2854 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2855 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2856 break;
2857
2858 case MADI:
2859 case MADIface:
2860 case AES32:
2861 hdspm_write(hdspm, HDSPM_controlRegister,
2862 hdspm->control_register);
2863 }
2864
Takashi Iwai763f3562005-06-03 11:25:34 +02002865 return 0;
2866}
2867
Adrian Knoth0dca1792011-01-26 19:32:14 +01002868
Takashi Iwai98274f02005-11-17 14:52:34 +01002869static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2870 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002871{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002872 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002873
Adrian Knoth0dca1792011-01-26 19:32:14 +01002874 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2875 uinfo->count = 1;
2876 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002877
Adrian Knoth0dca1792011-01-26 19:32:14 +01002878 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2879 uinfo->value.enumerated.item =
2880 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002881
Adrian Knoth0dca1792011-01-26 19:32:14 +01002882 strcpy(uinfo->value.enumerated.name,
2883 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002884
Takashi Iwai763f3562005-06-03 11:25:34 +02002885 return 0;
2886}
2887
Takashi Iwai98274f02005-11-17 14:52:34 +01002888static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002890{
Takashi Iwai98274f02005-11-17 14:52:34 +01002891 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002892 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002893
Adrian Knoth0dca1792011-01-26 19:32:14 +01002894 if (psf >= 0) {
2895 ucontrol->value.enumerated.item[0] = psf;
2896 return 0;
2897 }
2898
2899 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002900}
2901
Takashi Iwai98274f02005-11-17 14:52:34 +01002902static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002904{
Takashi Iwai98274f02005-11-17 14:52:34 +01002905 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002906 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002907
2908 if (!snd_hdspm_use_is_exclusive(hdspm))
2909 return -EBUSY;
2910
Adrian Knoth0dca1792011-01-26 19:32:14 +01002911 val = ucontrol->value.enumerated.item[0];
2912
2913 if (val < 0)
2914 val = 0;
2915 else if (val >= hdspm->texts_autosync_items)
2916 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002917
2918 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002919 if (val != hdspm_pref_sync_ref(hdspm))
2920 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2921
Takashi Iwai763f3562005-06-03 11:25:34 +02002922 spin_unlock_irq(&hdspm->lock);
2923 return change;
2924}
2925
Adrian Knoth0dca1792011-01-26 19:32:14 +01002926
Takashi Iwai763f3562005-06-03 11:25:34 +02002927#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002928{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2929 .name = xname, \
2930 .index = xindex, \
2931 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2932 .info = snd_hdspm_info_autosync_ref, \
2933 .get = snd_hdspm_get_autosync_ref, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002934}
2935
Adrian Knoth0dca1792011-01-26 19:32:14 +01002936static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002937{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002938 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002939 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002940 unsigned int syncref =
2941 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002942 if (syncref == 0)
2943 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2944 if (syncref <= 8)
2945 return syncref;
2946 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002947 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002948 /* This looks at the autosync selected sync reference */
2949 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002950
Remy Bruno3cee5a62006-10-16 12:46:32 +02002951 switch (status2 & HDSPM_SelSyncRefMask) {
2952 case HDSPM_SelSyncRef_WORD:
2953 return HDSPM_AUTOSYNC_FROM_WORD;
2954 case HDSPM_SelSyncRef_MADI:
2955 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002956 case HDSPM_SelSyncRef_TCO:
2957 return HDSPM_AUTOSYNC_FROM_TCO;
2958 case HDSPM_SelSyncRef_SyncIn:
2959 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002960 case HDSPM_SelSyncRef_NVALID:
2961 return HDSPM_AUTOSYNC_FROM_NONE;
2962 default:
Adrian Knothe71b95a2013-07-05 11:28:06 +02002963 return HDSPM_AUTOSYNC_FROM_NONE;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002964 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002965
Takashi Iwai763f3562005-06-03 11:25:34 +02002966 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002967 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002968}
2969
Adrian Knoth0dca1792011-01-26 19:32:14 +01002970
Takashi Iwai98274f02005-11-17 14:52:34 +01002971static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2972 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002973{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002974 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002975
Adrian Knoth0dca1792011-01-26 19:32:14 +01002976 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002977 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
Adrian Knothdb2d1a92013-07-05 11:28:08 +02002978 "AES4", "AES5", "AES6", "AES7", "AES8", "TCO", "Sync In", "None"};
Remy Bruno3cee5a62006-10-16 12:46:32 +02002979
2980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2981 uinfo->count = 1;
Adrian Knothdb2d1a92013-07-05 11:28:08 +02002982 uinfo->value.enumerated.items = ARRAY_SIZE(texts);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002983 if (uinfo->value.enumerated.item >=
2984 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002985 uinfo->value.enumerated.item =
2986 uinfo->value.enumerated.items - 1;
2987 strcpy(uinfo->value.enumerated.name,
2988 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002989 } else if (MADI == hdspm->io_type) {
2990 static char *texts[] = {"Word Clock", "MADI", "TCO",
2991 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002992
2993 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2994 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002995 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002996 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002997 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002998 uinfo->value.enumerated.item =
2999 uinfo->value.enumerated.items - 1;
3000 strcpy(uinfo->value.enumerated.name,
3001 texts[uinfo->value.enumerated.item]);
3002 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003003 return 0;
3004}
3005
Takashi Iwai98274f02005-11-17 14:52:34 +01003006static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
3007 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003008{
Takashi Iwai98274f02005-11-17 14:52:34 +01003009 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003010
Remy Bruno65345992007-08-31 12:21:08 +02003011 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02003012 return 0;
3013}
3014
Adrian Knothf99c7882013-03-10 00:37:26 +01003015
3016
3017#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
3018{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3019 .name = xname, \
3020 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3021 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3022 .info = snd_hdspm_info_tco_video_input_format, \
3023 .get = snd_hdspm_get_tco_video_input_format, \
3024}
3025
3026static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
3027 struct snd_ctl_elem_info *uinfo)
3028{
3029 static char *texts[] = {"No video", "NTSC", "PAL"};
3030 ENUMERATED_CTL_INFO(uinfo, texts);
3031 return 0;
3032}
3033
3034static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
3036{
3037 u32 status;
3038 int ret = 0;
3039
3040 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3041 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3042 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3043 HDSPM_TCO1_Video_Input_Format_PAL)) {
3044 case HDSPM_TCO1_Video_Input_Format_NTSC:
3045 /* ntsc */
3046 ret = 1;
3047 break;
3048 case HDSPM_TCO1_Video_Input_Format_PAL:
3049 /* pal */
3050 ret = 2;
3051 break;
3052 default:
3053 /* no video */
3054 ret = 0;
3055 break;
3056 }
3057 ucontrol->value.enumerated.item[0] = ret;
3058 return 0;
3059}
3060
3061
3062
3063#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3064{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3065 .name = xname, \
3066 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3067 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3068 .info = snd_hdspm_info_tco_ltc_frames, \
3069 .get = snd_hdspm_get_tco_ltc_frames, \
3070}
3071
3072static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_info *uinfo)
3074{
3075 static char *texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3076 "30 fps"};
3077 ENUMERATED_CTL_INFO(uinfo, texts);
3078 return 0;
3079}
3080
3081static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3082{
3083 u32 status;
3084 int ret = 0;
3085
3086 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3087 if (status & HDSPM_TCO1_LTC_Input_valid) {
3088 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3089 HDSPM_TCO1_LTC_Format_MSB)) {
3090 case 0:
3091 /* 24 fps */
3092 ret = 1;
3093 break;
3094 case HDSPM_TCO1_LTC_Format_LSB:
3095 /* 25 fps */
3096 ret = 2;
3097 break;
3098 case HDSPM_TCO1_LTC_Format_MSB:
3099 /* 25 fps */
3100 ret = 3;
3101 break;
3102 default:
3103 /* 30 fps */
3104 ret = 4;
3105 break;
3106 }
3107 }
3108
3109 return ret;
3110}
3111
3112static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3114{
3115 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3116
3117 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3118 return 0;
3119}
3120
Adrian Knothbf0ff872012-12-03 14:55:49 +01003121#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3122{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3123 .name = xname, \
3124 .private_value = xindex, \
3125 .info = snd_hdspm_info_toggle_setting, \
3126 .get = snd_hdspm_get_toggle_setting, \
3127 .put = snd_hdspm_put_toggle_setting \
3128}
3129
3130static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3131{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003132 u32 reg;
3133
3134 if (hdspm_is_raydat_or_aio(hdspm))
3135 reg = hdspm->settings_register;
3136 else
3137 reg = hdspm->control_register;
3138
3139 return (reg & regmask) ? 1 : 0;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003140}
3141
3142static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3143{
Adrian Knothce13f3f2013-07-05 11:27:55 +02003144 u32 *reg;
3145 u32 target_reg;
3146
3147 if (hdspm_is_raydat_or_aio(hdspm)) {
3148 reg = &(hdspm->settings_register);
3149 target_reg = HDSPM_WR_SETTINGS;
3150 } else {
3151 reg = &(hdspm->control_register);
3152 target_reg = HDSPM_controlRegister;
3153 }
3154
Adrian Knothbf0ff872012-12-03 14:55:49 +01003155 if (out)
Adrian Knothce13f3f2013-07-05 11:27:55 +02003156 *reg |= regmask;
Adrian Knothbf0ff872012-12-03 14:55:49 +01003157 else
Adrian Knothce13f3f2013-07-05 11:27:55 +02003158 *reg &= ~regmask;
3159
3160 hdspm_write(hdspm, target_reg, *reg);
Adrian Knothbf0ff872012-12-03 14:55:49 +01003161
3162 return 0;
3163}
3164
3165#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3166
3167static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3168 struct snd_ctl_elem_value *ucontrol)
3169{
3170 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3171 u32 regmask = kcontrol->private_value;
3172
3173 spin_lock_irq(&hdspm->lock);
3174 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3175 spin_unlock_irq(&hdspm->lock);
3176 return 0;
3177}
3178
3179static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
3181{
3182 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3183 u32 regmask = kcontrol->private_value;
3184 int change;
3185 unsigned int val;
3186
3187 if (!snd_hdspm_use_is_exclusive(hdspm))
3188 return -EBUSY;
3189 val = ucontrol->value.integer.value[0] & 1;
3190 spin_lock_irq(&hdspm->lock);
3191 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3192 hdspm_set_toggle_setting(hdspm, regmask, val);
3193 spin_unlock_irq(&hdspm->lock);
3194 return change;
3195}
3196
Takashi Iwai763f3562005-06-03 11:25:34 +02003197#define HDSPM_INPUT_SELECT(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003198{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3199 .name = xname, \
3200 .index = xindex, \
3201 .info = snd_hdspm_info_input_select, \
3202 .get = snd_hdspm_get_input_select, \
3203 .put = snd_hdspm_put_input_select \
Takashi Iwai763f3562005-06-03 11:25:34 +02003204}
3205
Takashi Iwai98274f02005-11-17 14:52:34 +01003206static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003207{
3208 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3209}
3210
Takashi Iwai98274f02005-11-17 14:52:34 +01003211static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003212{
3213 if (out)
3214 hdspm->control_register |= HDSPM_InputSelect0;
3215 else
3216 hdspm->control_register &= ~HDSPM_InputSelect0;
3217 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3218
3219 return 0;
3220}
3221
Takashi Iwai98274f02005-11-17 14:52:34 +01003222static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003224{
3225 static char *texts[] = { "optical", "coaxial" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003226 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003227 return 0;
3228}
3229
Takashi Iwai98274f02005-11-17 14:52:34 +01003230static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3231 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003232{
Takashi Iwai98274f02005-11-17 14:52:34 +01003233 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003234
3235 spin_lock_irq(&hdspm->lock);
3236 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3237 spin_unlock_irq(&hdspm->lock);
3238 return 0;
3239}
3240
Takashi Iwai98274f02005-11-17 14:52:34 +01003241static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3242 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003243{
Takashi Iwai98274f02005-11-17 14:52:34 +01003244 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003245 int change;
3246 unsigned int val;
3247
3248 if (!snd_hdspm_use_is_exclusive(hdspm))
3249 return -EBUSY;
3250 val = ucontrol->value.integer.value[0] & 1;
3251 spin_lock_irq(&hdspm->lock);
3252 change = (int) val != hdspm_input_select(hdspm);
3253 hdspm_set_input_select(hdspm, val);
3254 spin_unlock_irq(&hdspm->lock);
3255 return change;
3256}
3257
Adrian Knoth0dca1792011-01-26 19:32:14 +01003258
Remy Bruno3cee5a62006-10-16 12:46:32 +02003259#define HDSPM_DS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003260{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3261 .name = xname, \
3262 .index = xindex, \
3263 .info = snd_hdspm_info_ds_wire, \
3264 .get = snd_hdspm_get_ds_wire, \
3265 .put = snd_hdspm_put_ds_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003266}
3267
3268static int hdspm_ds_wire(struct hdspm * hdspm)
3269{
3270 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3271}
3272
3273static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3274{
3275 if (ds)
3276 hdspm->control_register |= HDSPM_DS_DoubleWire;
3277 else
3278 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3279 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3280
3281 return 0;
3282}
3283
3284static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3285 struct snd_ctl_elem_info *uinfo)
3286{
3287 static char *texts[] = { "Single", "Double" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003288 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003289 return 0;
3290}
3291
3292static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3293 struct snd_ctl_elem_value *ucontrol)
3294{
3295 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3296
3297 spin_lock_irq(&hdspm->lock);
3298 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3299 spin_unlock_irq(&hdspm->lock);
3300 return 0;
3301}
3302
3303static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
3306 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3307 int change;
3308 unsigned int val;
3309
3310 if (!snd_hdspm_use_is_exclusive(hdspm))
3311 return -EBUSY;
3312 val = ucontrol->value.integer.value[0] & 1;
3313 spin_lock_irq(&hdspm->lock);
3314 change = (int) val != hdspm_ds_wire(hdspm);
3315 hdspm_set_ds_wire(hdspm, val);
3316 spin_unlock_irq(&hdspm->lock);
3317 return change;
3318}
3319
Adrian Knoth0dca1792011-01-26 19:32:14 +01003320
Remy Bruno3cee5a62006-10-16 12:46:32 +02003321#define HDSPM_QS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003322{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3323 .name = xname, \
3324 .index = xindex, \
3325 .info = snd_hdspm_info_qs_wire, \
3326 .get = snd_hdspm_get_qs_wire, \
3327 .put = snd_hdspm_put_qs_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003328}
3329
3330static int hdspm_qs_wire(struct hdspm * hdspm)
3331{
3332 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3333 return 1;
3334 if (hdspm->control_register & HDSPM_QS_QuadWire)
3335 return 2;
3336 return 0;
3337}
3338
3339static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3340{
3341 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3342 switch (mode) {
3343 case 0:
3344 break;
3345 case 1:
3346 hdspm->control_register |= HDSPM_QS_DoubleWire;
3347 break;
3348 case 2:
3349 hdspm->control_register |= HDSPM_QS_QuadWire;
3350 break;
3351 }
3352 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3353
3354 return 0;
3355}
3356
3357static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3358 struct snd_ctl_elem_info *uinfo)
3359{
3360 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003361 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003362 return 0;
3363}
3364
3365static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_value *ucontrol)
3367{
3368 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3369
3370 spin_lock_irq(&hdspm->lock);
3371 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3372 spin_unlock_irq(&hdspm->lock);
3373 return 0;
3374}
3375
3376static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3377 struct snd_ctl_elem_value *ucontrol)
3378{
3379 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3380 int change;
3381 int val;
3382
3383 if (!snd_hdspm_use_is_exclusive(hdspm))
3384 return -EBUSY;
3385 val = ucontrol->value.integer.value[0];
3386 if (val < 0)
3387 val = 0;
3388 if (val > 2)
3389 val = 2;
3390 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003391 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003392 hdspm_set_qs_wire(hdspm, val);
3393 spin_unlock_irq(&hdspm->lock);
3394 return change;
3395}
3396
Adrian Knothacf14762013-07-05 11:28:00 +02003397#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
3398{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3399 .name = xname, \
3400 .private_value = xindex, \
3401 .info = snd_hdspm_info_tristate, \
3402 .get = snd_hdspm_get_tristate, \
3403 .put = snd_hdspm_put_tristate \
3404}
3405
3406static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
3407{
3408 u32 reg = hdspm->settings_register & (regmask * 3);
3409 return reg / regmask;
3410}
3411
3412static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
3413{
3414 hdspm->settings_register &= ~(regmask * 3);
3415 hdspm->settings_register |= (regmask * mode);
3416 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
3417
3418 return 0;
3419}
3420
3421static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_info *uinfo)
3423{
3424 u32 regmask = kcontrol->private_value;
3425
3426 static char *texts_spdif[] = { "Optical", "Coaxial", "Internal" };
3427 static char *texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
3428
3429 switch (regmask) {
3430 case HDSPM_c0_Input0:
3431 ENUMERATED_CTL_INFO(uinfo, texts_spdif);
3432 break;
3433 default:
3434 ENUMERATED_CTL_INFO(uinfo, texts_levels);
3435 break;
3436 }
3437 return 0;
3438}
3439
3440static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
3441 struct snd_ctl_elem_value *ucontrol)
3442{
3443 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3444 u32 regmask = kcontrol->private_value;
3445
3446 spin_lock_irq(&hdspm->lock);
3447 ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
3448 spin_unlock_irq(&hdspm->lock);
3449 return 0;
3450}
3451
3452static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
3453 struct snd_ctl_elem_value *ucontrol)
3454{
3455 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3456 u32 regmask = kcontrol->private_value;
3457 int change;
3458 int val;
3459
3460 if (!snd_hdspm_use_is_exclusive(hdspm))
3461 return -EBUSY;
3462 val = ucontrol->value.integer.value[0];
3463 if (val < 0)
3464 val = 0;
3465 if (val > 2)
3466 val = 2;
3467
3468 spin_lock_irq(&hdspm->lock);
3469 change = val != hdspm_tristate(hdspm, regmask);
3470 hdspm_set_tristate(hdspm, val, regmask);
3471 spin_unlock_irq(&hdspm->lock);
3472 return change;
3473}
3474
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003475#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3476{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3477 .name = xname, \
3478 .index = xindex, \
3479 .info = snd_hdspm_info_madi_speedmode, \
3480 .get = snd_hdspm_get_madi_speedmode, \
3481 .put = snd_hdspm_put_madi_speedmode \
3482}
3483
3484static int hdspm_madi_speedmode(struct hdspm *hdspm)
3485{
3486 if (hdspm->control_register & HDSPM_QuadSpeed)
3487 return 2;
3488 if (hdspm->control_register & HDSPM_DoubleSpeed)
3489 return 1;
3490 return 0;
3491}
3492
3493static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3494{
3495 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3496 switch (mode) {
3497 case 0:
3498 break;
3499 case 1:
3500 hdspm->control_register |= HDSPM_DoubleSpeed;
3501 break;
3502 case 2:
3503 hdspm->control_register |= HDSPM_QuadSpeed;
3504 break;
3505 }
3506 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3507
3508 return 0;
3509}
3510
3511static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3512 struct snd_ctl_elem_info *uinfo)
3513{
3514 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003515 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003516 return 0;
3517}
3518
3519static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3520 struct snd_ctl_elem_value *ucontrol)
3521{
3522 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3523
3524 spin_lock_irq(&hdspm->lock);
3525 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3526 spin_unlock_irq(&hdspm->lock);
3527 return 0;
3528}
3529
3530static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3531 struct snd_ctl_elem_value *ucontrol)
3532{
3533 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3534 int change;
3535 int val;
3536
3537 if (!snd_hdspm_use_is_exclusive(hdspm))
3538 return -EBUSY;
3539 val = ucontrol->value.integer.value[0];
3540 if (val < 0)
3541 val = 0;
3542 if (val > 2)
3543 val = 2;
3544 spin_lock_irq(&hdspm->lock);
3545 change = val != hdspm_madi_speedmode(hdspm);
3546 hdspm_set_madi_speedmode(hdspm, val);
3547 spin_unlock_irq(&hdspm->lock);
3548 return change;
3549}
Takashi Iwai763f3562005-06-03 11:25:34 +02003550
3551#define HDSPM_MIXER(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003552{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3553 .name = xname, \
3554 .index = xindex, \
3555 .device = 0, \
3556 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3557 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3558 .info = snd_hdspm_info_mixer, \
3559 .get = snd_hdspm_get_mixer, \
3560 .put = snd_hdspm_put_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003561}
3562
Takashi Iwai98274f02005-11-17 14:52:34 +01003563static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3564 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003565{
3566 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3567 uinfo->count = 3;
3568 uinfo->value.integer.min = 0;
3569 uinfo->value.integer.max = 65535;
3570 uinfo->value.integer.step = 1;
3571 return 0;
3572}
3573
Takashi Iwai98274f02005-11-17 14:52:34 +01003574static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3575 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003576{
Takashi Iwai98274f02005-11-17 14:52:34 +01003577 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003578 int source;
3579 int destination;
3580
3581 source = ucontrol->value.integer.value[0];
3582 if (source < 0)
3583 source = 0;
3584 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3585 source = 2 * HDSPM_MAX_CHANNELS - 1;
3586
3587 destination = ucontrol->value.integer.value[1];
3588 if (destination < 0)
3589 destination = 0;
3590 else if (destination >= HDSPM_MAX_CHANNELS)
3591 destination = HDSPM_MAX_CHANNELS - 1;
3592
3593 spin_lock_irq(&hdspm->lock);
3594 if (source >= HDSPM_MAX_CHANNELS)
3595 ucontrol->value.integer.value[2] =
3596 hdspm_read_pb_gain(hdspm, destination,
3597 source - HDSPM_MAX_CHANNELS);
3598 else
3599 ucontrol->value.integer.value[2] =
3600 hdspm_read_in_gain(hdspm, destination, source);
3601
3602 spin_unlock_irq(&hdspm->lock);
3603
3604 return 0;
3605}
3606
Takashi Iwai98274f02005-11-17 14:52:34 +01003607static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3608 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003609{
Takashi Iwai98274f02005-11-17 14:52:34 +01003610 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003611 int change;
3612 int source;
3613 int destination;
3614 int gain;
3615
3616 if (!snd_hdspm_use_is_exclusive(hdspm))
3617 return -EBUSY;
3618
3619 source = ucontrol->value.integer.value[0];
3620 destination = ucontrol->value.integer.value[1];
3621
3622 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3623 return -1;
3624 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3625 return -1;
3626
3627 gain = ucontrol->value.integer.value[2];
3628
3629 spin_lock_irq(&hdspm->lock);
3630
3631 if (source >= HDSPM_MAX_CHANNELS)
3632 change = gain != hdspm_read_pb_gain(hdspm, destination,
3633 source -
3634 HDSPM_MAX_CHANNELS);
3635 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003636 change = gain != hdspm_read_in_gain(hdspm, destination,
3637 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003638
3639 if (change) {
3640 if (source >= HDSPM_MAX_CHANNELS)
3641 hdspm_write_pb_gain(hdspm, destination,
3642 source - HDSPM_MAX_CHANNELS,
3643 gain);
3644 else
3645 hdspm_write_in_gain(hdspm, destination, source,
3646 gain);
3647 }
3648 spin_unlock_irq(&hdspm->lock);
3649
3650 return change;
3651}
3652
3653/* The simple mixer control(s) provide gain control for the
3654 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003655 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003656*/
3657
3658#define HDSPM_PLAYBACK_MIXER \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003659{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3660 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3661 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3662 .info = snd_hdspm_info_playback_mixer, \
3663 .get = snd_hdspm_get_playback_mixer, \
3664 .put = snd_hdspm_put_playback_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003665}
3666
Takashi Iwai98274f02005-11-17 14:52:34 +01003667static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3668 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003669{
3670 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3671 uinfo->count = 1;
3672 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003673 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003674 uinfo->value.integer.step = 1;
3675 return 0;
3676}
3677
Takashi Iwai98274f02005-11-17 14:52:34 +01003678static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3679 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003680{
Takashi Iwai98274f02005-11-17 14:52:34 +01003681 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003682 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003683
3684 channel = ucontrol->id.index - 1;
3685
Takashi Iwaida3cec32008-08-08 17:12:14 +02003686 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3687 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003688
Takashi Iwai763f3562005-06-03 11:25:34 +02003689 spin_lock_irq(&hdspm->lock);
3690 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003691 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003692 spin_unlock_irq(&hdspm->lock);
3693
Takashi Iwai763f3562005-06-03 11:25:34 +02003694 return 0;
3695}
3696
Takashi Iwai98274f02005-11-17 14:52:34 +01003697static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3698 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003699{
Takashi Iwai98274f02005-11-17 14:52:34 +01003700 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003701 int change;
3702 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003703 int gain;
3704
3705 if (!snd_hdspm_use_is_exclusive(hdspm))
3706 return -EBUSY;
3707
3708 channel = ucontrol->id.index - 1;
3709
Takashi Iwaida3cec32008-08-08 17:12:14 +02003710 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3711 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003712
Adrian Knoth0dca1792011-01-26 19:32:14 +01003713 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003714
3715 spin_lock_irq(&hdspm->lock);
3716 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003717 gain != hdspm_read_pb_gain(hdspm, channel,
3718 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003719 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003720 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003721 gain);
3722 spin_unlock_irq(&hdspm->lock);
3723 return change;
3724}
3725
Adrian Knoth0dca1792011-01-26 19:32:14 +01003726#define HDSPM_SYNC_CHECK(xname, xindex) \
3727{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3728 .name = xname, \
3729 .private_value = xindex, \
3730 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3731 .info = snd_hdspm_info_sync_check, \
3732 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003733}
3734
Adrian Knoth34542212013-03-10 00:37:25 +01003735#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3736{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3737 .name = xname, \
3738 .private_value = xindex, \
3739 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3740 .info = snd_hdspm_tco_info_lock_check, \
3741 .get = snd_hdspm_get_sync_check \
3742}
3743
3744
Adrian Knoth0dca1792011-01-26 19:32:14 +01003745
Takashi Iwai98274f02005-11-17 14:52:34 +01003746static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3747 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003748{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003749 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003750 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003751 return 0;
3752}
3753
Adrian Knoth34542212013-03-10 00:37:25 +01003754static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3755 struct snd_ctl_elem_info *uinfo)
3756{
3757 static char *texts[] = { "No Lock", "Lock" };
3758 ENUMERATED_CTL_INFO(uinfo, texts);
3759 return 0;
3760}
3761
Adrian Knoth0dca1792011-01-26 19:32:14 +01003762static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003763{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003764 int status, status2;
3765
3766 switch (hdspm->io_type) {
3767 case AES32:
3768 status = hdspm_read(hdspm, HDSPM_statusRegister);
Andre Schramm56bde0f2013-01-09 14:40:18 +01003769 if (status & HDSPM_AES32_wcLock) {
3770 if (status & HDSPM_AES32_wcSync)
3771 return 2;
3772 else
3773 return 1;
3774 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02003775 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003776 break;
3777
3778 case MADI:
3779 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003780 if (status2 & HDSPM_wcLock) {
3781 if (status2 & HDSPM_wcSync)
3782 return 2;
3783 else
3784 return 1;
3785 }
3786 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003787 break;
3788
3789 case RayDAT:
3790 case AIO:
3791 status = hdspm_read(hdspm, HDSPM_statusRegister);
3792
3793 if (status & 0x2000000)
3794 return 2;
3795 else if (status & 0x1000000)
3796 return 1;
3797 return 0;
3798
3799 break;
3800
3801 case MADIface:
3802 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003803 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003804
Takashi Iwai763f3562005-06-03 11:25:34 +02003805
Adrian Knoth0dca1792011-01-26 19:32:14 +01003806 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003807}
3808
3809
Adrian Knoth0dca1792011-01-26 19:32:14 +01003810static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003811{
3812 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3813 if (status & HDSPM_madiLock) {
3814 if (status & HDSPM_madiSync)
3815 return 2;
3816 else
3817 return 1;
3818 }
3819 return 0;
3820}
3821
Adrian Knoth0dca1792011-01-26 19:32:14 +01003822
3823static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3824{
3825 int status, lock, sync;
3826
3827 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3828
3829 lock = (status & (0x1<<idx)) ? 1 : 0;
3830 sync = (status & (0x100<<idx)) ? 1 : 0;
3831
3832 if (lock && sync)
3833 return 2;
3834 else if (lock)
3835 return 1;
3836 return 0;
3837}
3838
3839
3840static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3841{
3842 int status, lock = 0, sync = 0;
3843
3844 switch (hdspm->io_type) {
3845 case RayDAT:
3846 case AIO:
3847 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3848 lock = (status & 0x400) ? 1 : 0;
3849 sync = (status & 0x800) ? 1 : 0;
3850 break;
3851
3852 case MADI:
Adrian Knoth2e0452f2012-10-19 17:42:27 +02003853 status = hdspm_read(hdspm, HDSPM_statusRegister);
3854 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3855 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3856 break;
3857
Adrian Knoth0dca1792011-01-26 19:32:14 +01003858 case AES32:
3859 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth9a215f42012-10-19 17:42:28 +02003860 lock = (status & 0x100000) ? 1 : 0;
3861 sync = (status & 0x200000) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003862 break;
3863
3864 case MADIface:
3865 break;
3866 }
3867
3868 if (lock && sync)
3869 return 2;
3870 else if (lock)
3871 return 1;
3872
3873 return 0;
3874}
3875
3876static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3877{
3878 int status2, lock, sync;
3879 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3880
3881 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3882 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3883
3884 if (sync)
3885 return 2;
3886 else if (lock)
3887 return 1;
3888 return 0;
3889}
3890
Adrian Knoth34542212013-03-10 00:37:25 +01003891static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3892{
3893 u32 status;
3894 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3895
3896 return (status & mask) ? 1 : 0;
3897}
3898
Adrian Knoth0dca1792011-01-26 19:32:14 +01003899
3900static int hdspm_tco_sync_check(struct hdspm *hdspm)
3901{
3902 int status;
3903
3904 if (hdspm->tco) {
3905 switch (hdspm->io_type) {
3906 case MADI:
Adrian Knothb0bf5502013-07-05 11:28:05 +02003907 status = hdspm_read(hdspm, HDSPM_statusRegister);
3908 if (status & HDSPM_tcoLockMadi) {
3909 if (status & HDSPM_tcoSync)
3910 return 2;
3911 else
3912 return 1;
3913 }
3914 return 0;
3915 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003916 case AES32:
3917 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knothb0bf5502013-07-05 11:28:05 +02003918 if (status & HDSPM_tcoLockAes) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01003919 if (status & HDSPM_tcoSync)
3920 return 2;
3921 else
3922 return 1;
3923 }
3924 return 0;
3925
3926 break;
3927
3928 case RayDAT:
3929 case AIO:
3930 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3931
3932 if (status & 0x8000000)
3933 return 2; /* Sync */
3934 if (status & 0x4000000)
3935 return 1; /* Lock */
3936 return 0; /* No signal */
3937 break;
3938
3939 default:
3940 break;
3941 }
3942 }
3943
3944 return 3; /* N/A */
3945}
3946
3947
3948static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3949 struct snd_ctl_elem_value *ucontrol)
3950{
3951 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3952 int val = -1;
3953
3954 switch (hdspm->io_type) {
3955 case RayDAT:
3956 switch (kcontrol->private_value) {
3957 case 0: /* WC */
3958 val = hdspm_wc_sync_check(hdspm); break;
3959 case 7: /* TCO */
3960 val = hdspm_tco_sync_check(hdspm); break;
3961 case 8: /* SYNC IN */
3962 val = hdspm_sync_in_sync_check(hdspm); break;
3963 default:
Adrian Knothd1a3c982012-11-07 18:00:09 +01003964 val = hdspm_s1_sync_check(hdspm,
3965 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003966 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003967 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003968
3969 case AIO:
3970 switch (kcontrol->private_value) {
3971 case 0: /* WC */
3972 val = hdspm_wc_sync_check(hdspm); break;
3973 case 4: /* TCO */
3974 val = hdspm_tco_sync_check(hdspm); break;
3975 case 5: /* SYNC IN */
3976 val = hdspm_sync_in_sync_check(hdspm); break;
3977 default:
Adrian Knoth1cb7dbf2013-07-05 11:28:03 +02003978 val = hdspm_s1_sync_check(hdspm,
3979 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003980 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003981 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003982
3983 case MADI:
3984 switch (kcontrol->private_value) {
3985 case 0: /* WC */
3986 val = hdspm_wc_sync_check(hdspm); break;
3987 case 1: /* MADI */
3988 val = hdspm_madi_sync_check(hdspm); break;
3989 case 2: /* TCO */
3990 val = hdspm_tco_sync_check(hdspm); break;
3991 case 3: /* SYNC_IN */
3992 val = hdspm_sync_in_sync_check(hdspm); break;
3993 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003994 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003995
3996 case MADIface:
3997 val = hdspm_madi_sync_check(hdspm); /* MADI */
3998 break;
3999
4000 case AES32:
4001 switch (kcontrol->private_value) {
4002 case 0: /* WC */
4003 val = hdspm_wc_sync_check(hdspm); break;
4004 case 9: /* TCO */
4005 val = hdspm_tco_sync_check(hdspm); break;
4006 case 10 /* SYNC IN */:
4007 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01004008 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01004009 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01004010 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004011 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02004012 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004013
4014 }
4015
Adrian Knoth34542212013-03-10 00:37:25 +01004016 if (hdspm->tco) {
4017 switch (kcontrol->private_value) {
4018 case 11:
4019 /* Check TCO for lock state of its current input */
4020 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
4021 break;
4022 case 12:
4023 /* Check TCO for valid time code on LTC input. */
4024 val = hdspm_tco_input_check(hdspm,
4025 HDSPM_TCO1_LTC_Input_valid);
4026 break;
4027 default:
4028 break;
4029 }
4030 }
4031
Adrian Knoth0dca1792011-01-26 19:32:14 +01004032 if (-1 == val)
4033 val = 3;
4034
4035 ucontrol->value.enumerated.item[0] = val;
4036 return 0;
4037}
4038
4039
4040
4041/**
4042 * TCO controls
4043 **/
4044static void hdspm_tco_write(struct hdspm *hdspm)
4045{
4046 unsigned int tc[4] = { 0, 0, 0, 0};
4047
4048 switch (hdspm->tco->input) {
4049 case 0:
4050 tc[2] |= HDSPM_TCO2_set_input_MSB;
4051 break;
4052 case 1:
4053 tc[2] |= HDSPM_TCO2_set_input_LSB;
4054 break;
4055 default:
4056 break;
4057 }
4058
4059 switch (hdspm->tco->framerate) {
4060 case 1:
4061 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
4062 break;
4063 case 2:
4064 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
4065 break;
4066 case 3:
4067 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
4068 HDSPM_TCO1_set_drop_frame_flag;
4069 break;
4070 case 4:
4071 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4072 HDSPM_TCO1_LTC_Format_MSB;
4073 break;
4074 case 5:
4075 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
4076 HDSPM_TCO1_LTC_Format_MSB +
4077 HDSPM_TCO1_set_drop_frame_flag;
4078 break;
4079 default:
4080 break;
4081 }
4082
4083 switch (hdspm->tco->wordclock) {
4084 case 1:
4085 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
4086 break;
4087 case 2:
4088 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4089 break;
4090 default:
4091 break;
4092 }
4093
4094 switch (hdspm->tco->samplerate) {
4095 case 1:
4096 tc[2] |= HDSPM_TCO2_set_freq;
4097 break;
4098 case 2:
4099 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4100 break;
4101 default:
4102 break;
4103 }
4104
4105 switch (hdspm->tco->pull) {
4106 case 1:
4107 tc[2] |= HDSPM_TCO2_set_pull_up;
4108 break;
4109 case 2:
4110 tc[2] |= HDSPM_TCO2_set_pull_down;
4111 break;
4112 case 3:
4113 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4114 break;
4115 case 4:
4116 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4117 break;
4118 default:
4119 break;
4120 }
4121
4122 if (1 == hdspm->tco->term) {
4123 tc[2] |= HDSPM_TCO2_set_term_75R;
4124 }
4125
4126 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4127 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4128 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4129 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4130}
4131
4132
4133#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4134{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4135 .name = xname, \
4136 .index = xindex, \
4137 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4138 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4139 .info = snd_hdspm_info_tco_sample_rate, \
4140 .get = snd_hdspm_get_tco_sample_rate, \
4141 .put = snd_hdspm_put_tco_sample_rate \
4142}
4143
4144static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4145 struct snd_ctl_elem_info *uinfo)
4146{
4147 static char *texts[] = { "44.1 kHz", "48 kHz" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004148 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004149 return 0;
4150}
4151
4152static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4153 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02004154{
Takashi Iwai98274f02005-11-17 14:52:34 +01004155 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02004156
Adrian Knoth0dca1792011-01-26 19:32:14 +01004157 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4158
Takashi Iwai763f3562005-06-03 11:25:34 +02004159 return 0;
4160}
4161
Adrian Knoth0dca1792011-01-26 19:32:14 +01004162static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4163 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02004164{
Adrian Knoth0dca1792011-01-26 19:32:14 +01004165 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4166
4167 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4168 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4169
4170 hdspm_tco_write(hdspm);
4171
4172 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004173 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01004174
Remy Bruno3cee5a62006-10-16 12:46:32 +02004175 return 0;
4176}
4177
Adrian Knoth0dca1792011-01-26 19:32:14 +01004178
4179#define HDSPM_TCO_PULL(xname, xindex) \
4180{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4181 .name = xname, \
4182 .index = xindex, \
4183 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4184 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4185 .info = snd_hdspm_info_tco_pull, \
4186 .get = snd_hdspm_get_tco_pull, \
4187 .put = snd_hdspm_put_tco_pull \
4188}
4189
4190static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4191 struct snd_ctl_elem_info *uinfo)
4192{
4193 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004194 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004195 return 0;
4196}
4197
4198static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4199 struct snd_ctl_elem_value *ucontrol)
4200{
4201 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4202
4203 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4204
4205 return 0;
4206}
4207
4208static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4209 struct snd_ctl_elem_value *ucontrol)
4210{
4211 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4212
4213 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4214 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4215
4216 hdspm_tco_write(hdspm);
4217
4218 return 1;
4219 }
4220
4221 return 0;
4222}
4223
4224#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4225{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4226 .name = xname, \
4227 .index = xindex, \
4228 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4229 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4230 .info = snd_hdspm_info_tco_wck_conversion, \
4231 .get = snd_hdspm_get_tco_wck_conversion, \
4232 .put = snd_hdspm_put_tco_wck_conversion \
4233}
4234
4235static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4236 struct snd_ctl_elem_info *uinfo)
4237{
4238 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004239 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004240 return 0;
4241}
4242
4243static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4244 struct snd_ctl_elem_value *ucontrol)
4245{
4246 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4247
4248 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4249
4250 return 0;
4251}
4252
4253static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4254 struct snd_ctl_elem_value *ucontrol)
4255{
4256 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4257
4258 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4259 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4260
4261 hdspm_tco_write(hdspm);
4262
4263 return 1;
4264 }
4265
4266 return 0;
4267}
4268
4269
4270#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4271{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4272 .name = xname, \
4273 .index = xindex, \
4274 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4275 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4276 .info = snd_hdspm_info_tco_frame_rate, \
4277 .get = snd_hdspm_get_tco_frame_rate, \
4278 .put = snd_hdspm_put_tco_frame_rate \
4279}
4280
4281static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4282 struct snd_ctl_elem_info *uinfo)
4283{
4284 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4285 "29.97 dfps", "30 fps", "30 dfps" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004286 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004287 return 0;
4288}
4289
4290static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004291 struct snd_ctl_elem_value *ucontrol)
4292{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004293 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4294
Adrian Knoth0dca1792011-01-26 19:32:14 +01004295 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004296
Remy Bruno3cee5a62006-10-16 12:46:32 +02004297 return 0;
4298}
Takashi Iwai763f3562005-06-03 11:25:34 +02004299
Adrian Knoth0dca1792011-01-26 19:32:14 +01004300static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4301 struct snd_ctl_elem_value *ucontrol)
4302{
4303 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4304
4305 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4306 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4307
4308 hdspm_tco_write(hdspm);
4309
4310 return 1;
4311 }
4312
4313 return 0;
4314}
4315
4316
4317#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4318{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4319 .name = xname, \
4320 .index = xindex, \
4321 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4322 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4323 .info = snd_hdspm_info_tco_sync_source, \
4324 .get = snd_hdspm_get_tco_sync_source, \
4325 .put = snd_hdspm_put_tco_sync_source \
4326}
4327
4328static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4329 struct snd_ctl_elem_info *uinfo)
4330{
4331 static char *texts[] = { "LTC", "Video", "WCK" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004332 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004333 return 0;
4334}
4335
4336static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4337 struct snd_ctl_elem_value *ucontrol)
4338{
4339 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4340
4341 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4342
4343 return 0;
4344}
4345
4346static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4347 struct snd_ctl_elem_value *ucontrol)
4348{
4349 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4350
4351 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4352 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4353
4354 hdspm_tco_write(hdspm);
4355
4356 return 1;
4357 }
4358
4359 return 0;
4360}
4361
4362
4363#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4364{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4365 .name = xname, \
4366 .index = xindex, \
4367 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4368 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4369 .info = snd_hdspm_info_tco_word_term, \
4370 .get = snd_hdspm_get_tco_word_term, \
4371 .put = snd_hdspm_put_tco_word_term \
4372}
4373
4374static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4375 struct snd_ctl_elem_info *uinfo)
4376{
4377 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4378 uinfo->count = 1;
4379 uinfo->value.integer.min = 0;
4380 uinfo->value.integer.max = 1;
4381
4382 return 0;
4383}
4384
4385
4386static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4387 struct snd_ctl_elem_value *ucontrol)
4388{
4389 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4390
4391 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4392
4393 return 0;
4394}
4395
4396
4397static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4398 struct snd_ctl_elem_value *ucontrol)
4399{
4400 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4401
4402 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4403 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4404
4405 hdspm_tco_write(hdspm);
4406
4407 return 1;
4408 }
4409
4410 return 0;
4411}
4412
4413
4414
Takashi Iwai763f3562005-06-03 11:25:34 +02004415
Remy Bruno3cee5a62006-10-16 12:46:32 +02004416static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004417 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004418 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004419 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4420 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4421 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4422 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knothb8812c52012-10-19 17:42:26 +02004423 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004424 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4425 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
Adrian Knoth930f4ff2012-10-19 17:42:29 +02004426 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004427 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Adrian Knothc9e16682012-12-03 14:55:50 +01004428 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4429 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
Adrian Knoth696be0f2013-03-10 00:37:23 +01004430 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
Adrian Knothc9e16682012-12-03 14:55:50 +01004431 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4432 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004433 HDSPM_INPUT_SELECT("Input Select", 0),
4434 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004435};
4436
4437
4438static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4439 HDSPM_MIXER("Mixer", 0),
4440 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4441 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4442 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4443 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4444 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
Adrian Knothc9e16682012-12-03 14:55:50 +01004445 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4446 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4447 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004448 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004449};
4450
Adrian Knoth0dca1792011-01-26 19:32:14 +01004451static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004452 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004453 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004454 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4455 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004456 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004457 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004458 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4459 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4460 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4461 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4462 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4463 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4464 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4465 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4466 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4467 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4468 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004469 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004470 HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
Adrian Knothfb0f1212013-07-05 11:27:58 +02004471 HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
4472 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4473 HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
4474 HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
Adrian Knoth42f4c122013-07-05 11:28:01 +02004475 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
4476 HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
4477 HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
4478 HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004479
4480 /*
4481 HDSPM_INPUT_SELECT("Input Select", 0),
4482 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4483 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4484 HDSPM_SPDIF_IN("SPDIF In", 0);
4485 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4486 HDSPM_INPUT_LEVEL("Input Level", 0);
4487 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4488 HDSPM_PHONES("Phones", 0);
4489 */
4490};
4491
4492static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4493 HDSPM_MIXER("Mixer", 0),
4494 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4495 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4496 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4497 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4498 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4499 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4500 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4501 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4502 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4503 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4504 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4505 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4506 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4507 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4508 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4509 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4510 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4511 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4512 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4513 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4514 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
Adrian Knoth11a5cd32013-07-05 11:27:57 +02004515 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
4516 HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
4517 HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004518};
4519
4520static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4521 HDSPM_MIXER("Mixer", 0),
4522 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4523 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4524 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4525 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4526 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4527 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4528 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4529 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4530 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4531 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4532 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4533 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4534 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4535 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4536 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4537 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4538 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4539 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4540 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4541 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4542 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4543 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4544 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4545 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4546 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4547 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4548 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4549 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Adrian Knothc9e16682012-12-03 14:55:50 +01004550 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4551 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4552 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4553 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4554 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004555 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4556 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4557};
4558
Adrian Knoth0dca1792011-01-26 19:32:14 +01004559
4560
4561/* Control elements for the optional TCO module */
4562static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4563 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4564 HDSPM_TCO_PULL("TCO Pull", 0),
4565 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4566 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4567 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
Adrian Knotha8176502013-03-10 00:37:27 +01004568 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4569 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4570 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4571 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4572 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004573};
4574
4575
Takashi Iwai98274f02005-11-17 14:52:34 +01004576static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004577
4578
Takashi Iwai98274f02005-11-17 14:52:34 +01004579static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004580{
4581 int i;
4582
Adrian Knoth0dca1792011-01-26 19:32:14 +01004583 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004584 if (hdspm->system_sample_rate > 48000) {
4585 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004586 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4587 SNDRV_CTL_ELEM_ACCESS_READ |
4588 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004589 } else {
4590 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004591 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4592 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004593 }
4594 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004595 SNDRV_CTL_EVENT_MASK_INFO,
4596 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004597 }
4598
4599 return 0;
4600}
4601
4602
Adrian Knoth0dca1792011-01-26 19:32:14 +01004603static int snd_hdspm_create_controls(struct snd_card *card,
4604 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004605{
4606 unsigned int idx, limit;
4607 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004608 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004609 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004610
Adrian Knoth0dca1792011-01-26 19:32:14 +01004611 switch (hdspm->io_type) {
4612 case MADI:
4613 list = snd_hdspm_controls_madi;
4614 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4615 break;
4616 case MADIface:
4617 list = snd_hdspm_controls_madiface;
4618 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4619 break;
4620 case AIO:
4621 list = snd_hdspm_controls_aio;
4622 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4623 break;
4624 case RayDAT:
4625 list = snd_hdspm_controls_raydat;
4626 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4627 break;
4628 case AES32:
4629 list = snd_hdspm_controls_aes32;
4630 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4631 break;
4632 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004633
Adrian Knoth0dca1792011-01-26 19:32:14 +01004634 if (NULL != list) {
4635 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004636 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004637 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004638 if (err < 0)
4639 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004640 }
4641 }
4642
Takashi Iwai763f3562005-06-03 11:25:34 +02004643
Adrian Knoth0dca1792011-01-26 19:32:14 +01004644 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004645 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004646 if (hdspm->system_sample_rate >= 128000) {
4647 limit = hdspm->qs_out_channels;
4648 } else if (hdspm->system_sample_rate >= 64000) {
4649 limit = hdspm->ds_out_channels;
4650 } else {
4651 limit = hdspm->ss_out_channels;
4652 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004653 for (idx = 0; idx < limit; ++idx) {
4654 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004655 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4656 err = snd_ctl_add(card, kctl);
4657 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004658 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004659 hdspm->playback_mixer_ctls[idx] = kctl;
4660 }
4661
Adrian Knoth0dca1792011-01-26 19:32:14 +01004662
4663 if (hdspm->tco) {
4664 /* add tco control elements */
4665 list = snd_hdspm_controls_tco;
4666 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4667 for (idx = 0; idx < limit; idx++) {
4668 err = snd_ctl_add(card,
4669 snd_ctl_new1(&list[idx], hdspm));
4670 if (err < 0)
4671 return err;
4672 }
4673 }
4674
Takashi Iwai763f3562005-06-03 11:25:34 +02004675 return 0;
4676}
4677
4678/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004679 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004680 ------------------------------------------------------------*/
4681
4682static void
Adrian Knoth57601072013-07-05 11:28:04 +02004683snd_hdspm_proc_read_tco(struct snd_info_entry *entry,
4684 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004685{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004686 struct hdspm *hdspm = entry->private_data;
Adrian Knoth57601072013-07-05 11:28:04 +02004687 unsigned int status, control;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004688 int a, ltc, frames, seconds, minutes, hours;
4689 unsigned int period;
4690 u64 freq_const = 0;
4691 u32 rate;
4692
Adrian Knoth57601072013-07-05 11:28:04 +02004693 snd_iprintf(buffer, "--- TCO ---\n");
4694
Takashi Iwai763f3562005-06-03 11:25:34 +02004695 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004696 control = hdspm->control_register;
Takashi Iwai763f3562005-06-03 11:25:34 +02004697
Adrian Knoth0dca1792011-01-26 19:32:14 +01004698
Adrian Knoth0dca1792011-01-26 19:32:14 +01004699 if (status & HDSPM_tco_detect) {
4700 snd_iprintf(buffer, "TCO module detected.\n");
4701 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4702 if (a & HDSPM_TCO1_LTC_Input_valid) {
4703 snd_iprintf(buffer, " LTC valid, ");
4704 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4705 HDSPM_TCO1_LTC_Format_MSB)) {
4706 case 0:
4707 snd_iprintf(buffer, "24 fps, ");
4708 break;
4709 case HDSPM_TCO1_LTC_Format_LSB:
4710 snd_iprintf(buffer, "25 fps, ");
4711 break;
4712 case HDSPM_TCO1_LTC_Format_MSB:
4713 snd_iprintf(buffer, "29.97 fps, ");
4714 break;
4715 default:
4716 snd_iprintf(buffer, "30 fps, ");
4717 break;
4718 }
4719 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4720 snd_iprintf(buffer, "drop frame\n");
4721 } else {
4722 snd_iprintf(buffer, "full frame\n");
4723 }
4724 } else {
4725 snd_iprintf(buffer, " no LTC\n");
4726 }
4727 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4728 snd_iprintf(buffer, " Video: NTSC\n");
4729 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4730 snd_iprintf(buffer, " Video: PAL\n");
4731 } else {
4732 snd_iprintf(buffer, " No video\n");
4733 }
4734 if (a & HDSPM_TCO1_TCO_lock) {
4735 snd_iprintf(buffer, " Sync: lock\n");
4736 } else {
4737 snd_iprintf(buffer, " Sync: no lock\n");
4738 }
4739
4740 switch (hdspm->io_type) {
4741 case MADI:
4742 case AES32:
4743 freq_const = 110069313433624ULL;
4744 break;
4745 case RayDAT:
4746 case AIO:
4747 freq_const = 104857600000000ULL;
4748 break;
4749 case MADIface:
4750 break; /* no TCO possible */
4751 }
4752
4753 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4754 snd_iprintf(buffer, " period: %u\n", period);
4755
4756
4757 /* rate = freq_const/period; */
4758 rate = div_u64(freq_const, period);
4759
4760 if (control & HDSPM_QuadSpeed) {
4761 rate *= 4;
4762 } else if (control & HDSPM_DoubleSpeed) {
4763 rate *= 2;
4764 }
4765
4766 snd_iprintf(buffer, " Frequency: %u Hz\n",
4767 (unsigned int) rate);
4768
4769 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4770 frames = ltc & 0xF;
4771 ltc >>= 4;
4772 frames += (ltc & 0x3) * 10;
4773 ltc >>= 4;
4774 seconds = ltc & 0xF;
4775 ltc >>= 4;
4776 seconds += (ltc & 0x7) * 10;
4777 ltc >>= 4;
4778 minutes = ltc & 0xF;
4779 ltc >>= 4;
4780 minutes += (ltc & 0x7) * 10;
4781 ltc >>= 4;
4782 hours = ltc & 0xF;
4783 ltc >>= 4;
4784 hours += (ltc & 0x3) * 10;
4785 snd_iprintf(buffer,
4786 " LTC In: %02d:%02d:%02d:%02d\n",
4787 hours, minutes, seconds, frames);
4788
4789 } else {
4790 snd_iprintf(buffer, "No TCO module detected.\n");
4791 }
Adrian Knoth57601072013-07-05 11:28:04 +02004792}
4793
4794static void
4795snd_hdspm_proc_read_madi(struct snd_info_entry *entry,
4796 struct snd_info_buffer *buffer)
4797{
4798 struct hdspm *hdspm = entry->private_data;
4799 unsigned int status, status2, control, freq;
4800
4801 char *pref_sync_ref;
4802 char *autosync_ref;
4803 char *system_clock_mode;
4804 char *insel;
4805 int x, x2;
4806
4807 status = hdspm_read(hdspm, HDSPM_statusRegister);
4808 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4809 control = hdspm->control_register;
4810 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
4811
4812 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
4813 hdspm->card_name, hdspm->card->number + 1,
4814 hdspm->firmware_rev,
4815 (status2 & HDSPM_version0) |
4816 (status2 & HDSPM_version1) | (status2 &
4817 HDSPM_version2));
4818
4819 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4820 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
4821 hdspm->serial);
4822
4823 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4824 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4825
4826 snd_iprintf(buffer, "--- System ---\n");
4827
4828 snd_iprintf(buffer,
4829 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4830 status & HDSPM_audioIRQPending,
4831 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4832 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4833 hdspm->irq_count);
4834 snd_iprintf(buffer,
4835 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4836 "estimated= %ld (bytes)\n",
4837 ((status & HDSPM_BufferID) ? 1 : 0),
4838 (status & HDSPM_BufferPositionMask),
4839 (status & HDSPM_BufferPositionMask) %
4840 (2 * (int)hdspm->period_bytes),
4841 ((status & HDSPM_BufferPositionMask) - 64) %
4842 (2 * (int)hdspm->period_bytes),
4843 (long) hdspm_hw_pointer(hdspm) * 4);
4844
4845 snd_iprintf(buffer,
4846 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4847 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4848 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4849 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4850 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4851 snd_iprintf(buffer,
4852 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4853 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4854 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4855 snd_iprintf(buffer,
4856 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4857 "status2=0x%x\n",
4858 hdspm->control_register, hdspm->control2_register,
4859 status, status2);
4860
Takashi Iwai763f3562005-06-03 11:25:34 +02004861
4862 snd_iprintf(buffer, "--- Settings ---\n");
4863
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004864 x = hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02004865
4866 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004867 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4868 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004869
Adrian Knoth0dca1792011-01-26 19:32:14 +01004870 snd_iprintf(buffer, "Line out: %s\n",
4871 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004872
4873 switch (hdspm->control_register & HDSPM_InputMask) {
4874 case HDSPM_InputOptical:
4875 insel = "Optical";
4876 break;
4877 case HDSPM_InputCoaxial:
4878 insel = "Coaxial";
4879 break;
4880 default:
Masanari Iidaec8f53f2012-11-02 00:28:50 +09004881 insel = "Unknown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004882 }
4883
Takashi Iwai763f3562005-06-03 11:25:34 +02004884 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004885 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4886 "Auto Input %s\n",
4887 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4888 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4889 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004890
Adrian Knoth0dca1792011-01-26 19:32:14 +01004891
Remy Bruno3cee5a62006-10-16 12:46:32 +02004892 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004893 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004894 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004895 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004896 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004897
4898 switch (hdspm_pref_sync_ref(hdspm)) {
4899 case HDSPM_SYNC_FROM_WORD:
4900 pref_sync_ref = "Word Clock";
4901 break;
4902 case HDSPM_SYNC_FROM_MADI:
4903 pref_sync_ref = "MADI Sync";
4904 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004905 case HDSPM_SYNC_FROM_TCO:
4906 pref_sync_ref = "TCO";
4907 break;
4908 case HDSPM_SYNC_FROM_SYNC_IN:
4909 pref_sync_ref = "Sync In";
4910 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004911 default:
4912 pref_sync_ref = "XXXX Clock";
4913 break;
4914 }
4915 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004916 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004917
4918 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004919 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004920
4921
4922 snd_iprintf(buffer, "--- Status:\n");
4923
4924 x = status & HDSPM_madiSync;
4925 x2 = status2 & HDSPM_wcSync;
4926
4927 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004928 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4929 "NoLock",
4930 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4931 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004932
4933 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004934 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4935 autosync_ref = "Sync In";
4936 break;
4937 case HDSPM_AUTOSYNC_FROM_TCO:
4938 autosync_ref = "TCO";
4939 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004940 case HDSPM_AUTOSYNC_FROM_WORD:
4941 autosync_ref = "Word Clock";
4942 break;
4943 case HDSPM_AUTOSYNC_FROM_MADI:
4944 autosync_ref = "MADI Sync";
4945 break;
4946 case HDSPM_AUTOSYNC_FROM_NONE:
4947 autosync_ref = "Input not valid";
4948 break;
4949 default:
4950 autosync_ref = "---";
4951 break;
4952 }
4953 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004954 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4955 autosync_ref, hdspm_external_sample_rate(hdspm),
4956 (status & HDSPM_madiFreqMask) >> 22,
4957 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004958
4959 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004960 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4961 (status & HDSPM_RX_64ch) ? "64 channels" :
4962 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004963
Adrian Knoth57601072013-07-05 11:28:04 +02004964 /* call readout function for TCO specific status */
4965 snd_hdspm_proc_read_tco(entry, buffer);
4966
Takashi Iwai763f3562005-06-03 11:25:34 +02004967 snd_iprintf(buffer, "\n");
4968}
4969
Remy Bruno3cee5a62006-10-16 12:46:32 +02004970static void
4971snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4972 struct snd_info_buffer *buffer)
4973{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004974 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004975 unsigned int status;
4976 unsigned int status2;
4977 unsigned int timecode;
Andre Schramm56bde0f2013-01-09 14:40:18 +01004978 unsigned int wcLock, wcSync;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004979 int pref_syncref;
4980 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004981 int x;
4982
4983 status = hdspm_read(hdspm, HDSPM_statusRegister);
4984 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4985 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4986
4987 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4988 hdspm->card_name, hdspm->card->number + 1,
4989 hdspm->firmware_rev);
4990
4991 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4992 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4993
4994 snd_iprintf(buffer, "--- System ---\n");
4995
4996 snd_iprintf(buffer,
4997 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4998 status & HDSPM_audioIRQPending,
4999 (status & HDSPM_midi0IRQPending) ? 1 : 0,
5000 (status & HDSPM_midi1IRQPending) ? 1 : 0,
5001 hdspm->irq_count);
5002 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005003 "HW pointer: id = %d, rawptr = %d (%d->%d) "
5004 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005005 ((status & HDSPM_BufferID) ? 1 : 0),
5006 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005007 (status & HDSPM_BufferPositionMask) %
5008 (2 * (int)hdspm->period_bytes),
5009 ((status & HDSPM_BufferPositionMask) - 64) %
5010 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02005011 (long) hdspm_hw_pointer(hdspm) * 4);
5012
5013 snd_iprintf(buffer,
5014 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
5015 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
5016 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
5017 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
5018 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
5019 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005020 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
5021 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
5022 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
5023 snd_iprintf(buffer,
5024 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
5025 "status2=0x%x\n",
5026 hdspm->control_register, hdspm->control2_register,
5027 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02005028
5029 snd_iprintf(buffer, "--- Settings ---\n");
5030
Adrian Knoth7cb155f2011-08-15 00:22:53 +02005031 x = hdspm_get_latency(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02005032
5033 snd_iprintf(buffer,
5034 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
5035 x, (unsigned long) hdspm->period_bytes);
5036
Adrian Knoth0dca1792011-01-26 19:32:14 +01005037 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005038 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01005039 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02005040
5041 snd_iprintf(buffer,
5042 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
5043 (hdspm->
5044 control_register & HDSPM_clr_tms) ? "on" : "off",
5045 (hdspm->
5046 control_register & HDSPM_Emphasis) ? "on" : "off",
5047 (hdspm->
5048 control_register & HDSPM_Dolby) ? "on" : "off");
5049
Remy Bruno3cee5a62006-10-16 12:46:32 +02005050
5051 pref_syncref = hdspm_pref_sync_ref(hdspm);
5052 if (pref_syncref == 0)
5053 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
5054 else
5055 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
5056 pref_syncref);
5057
5058 snd_iprintf(buffer, "System Clock Frequency: %d\n",
5059 hdspm->system_sample_rate);
5060
5061 snd_iprintf(buffer, "Double speed: %s\n",
5062 hdspm->control_register & HDSPM_DS_DoubleWire?
5063 "Double wire" : "Single wire");
5064 snd_iprintf(buffer, "Quad speed: %s\n",
5065 hdspm->control_register & HDSPM_QS_DoubleWire?
5066 "Double wire" :
5067 hdspm->control_register & HDSPM_QS_QuadWire?
5068 "Quad wire" : "Single wire");
5069
5070 snd_iprintf(buffer, "--- Status:\n");
5071
Andre Schramm56bde0f2013-01-09 14:40:18 +01005072 wcLock = status & HDSPM_AES32_wcLock;
5073 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
5074
Remy Bruno3cee5a62006-10-16 12:46:32 +02005075 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Andre Schramm56bde0f2013-01-09 14:40:18 +01005076 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005077 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005078
5079 for (x = 0; x < 8; x++) {
5080 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005081 x+1,
5082 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01005083 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005084 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005085 }
5086
5087 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005088 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5089 autosync_ref = "None"; break;
5090 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5091 autosync_ref = "Word Clock"; break;
5092 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5093 autosync_ref = "AES1"; break;
5094 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5095 autosync_ref = "AES2"; break;
5096 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5097 autosync_ref = "AES3"; break;
5098 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5099 autosync_ref = "AES4"; break;
5100 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5101 autosync_ref = "AES5"; break;
5102 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5103 autosync_ref = "AES6"; break;
5104 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5105 autosync_ref = "AES7"; break;
5106 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5107 autosync_ref = "AES8"; break;
5108 default:
5109 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005110 }
5111 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5112
5113 snd_iprintf(buffer, "\n");
5114}
5115
Adrian Knoth0dca1792011-01-26 19:32:14 +01005116static void
5117snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5118 struct snd_info_buffer *buffer)
5119{
5120 struct hdspm *hdspm = entry->private_data;
5121 unsigned int status1, status2, status3, control, i;
5122 unsigned int lock, sync;
5123
5124 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5125 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5126 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5127
5128 control = hdspm->control_register;
5129
5130 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5131 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5132 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5133
5134
5135 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5136
5137 snd_iprintf(buffer, "Clock mode : %s\n",
5138 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5139 snd_iprintf(buffer, "System frequency: %d Hz\n",
5140 hdspm_get_system_sample_rate(hdspm));
5141
5142 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5143
5144 lock = 0x1;
5145 sync = 0x100;
5146
5147 for (i = 0; i < 8; i++) {
5148 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5149 i,
5150 (status1 & lock) ? 1 : 0,
5151 (status1 & sync) ? 1 : 0,
5152 texts_freq[(status2 >> (i * 4)) & 0xF]);
5153
5154 lock = lock<<1;
5155 sync = sync<<1;
5156 }
5157
5158 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5159 (status1 & 0x1000000) ? 1 : 0,
5160 (status1 & 0x2000000) ? 1 : 0,
5161 texts_freq[(status1 >> 16) & 0xF]);
5162
5163 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5164 (status1 & 0x4000000) ? 1 : 0,
5165 (status1 & 0x8000000) ? 1 : 0,
5166 texts_freq[(status1 >> 20) & 0xF]);
5167
5168 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5169 (status3 & 0x400) ? 1 : 0,
5170 (status3 & 0x800) ? 1 : 0,
5171 texts_freq[(status2 >> 12) & 0xF]);
5172
5173}
5174
Remy Bruno3cee5a62006-10-16 12:46:32 +02005175#ifdef CONFIG_SND_DEBUG
5176static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01005177snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005178 struct snd_info_buffer *buffer)
5179{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005180 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005181
5182 int j,i;
5183
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005184 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02005185 snd_iprintf(buffer, "0x%08X: ", i);
5186 for (j = 0; j < 16; j += 4)
5187 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5188 snd_iprintf(buffer, "\n");
5189 }
5190}
5191#endif
5192
5193
Adrian Knoth0dca1792011-01-26 19:32:14 +01005194static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5195 struct snd_info_buffer *buffer)
5196{
5197 struct hdspm *hdspm = entry->private_data;
5198 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005199
Adrian Knoth0dca1792011-01-26 19:32:14 +01005200 snd_iprintf(buffer, "# generated by hdspm\n");
5201
5202 for (i = 0; i < hdspm->max_channels_in; i++) {
5203 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5204 }
5205}
5206
5207static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5208 struct snd_info_buffer *buffer)
5209{
5210 struct hdspm *hdspm = entry->private_data;
5211 int i;
5212
5213 snd_iprintf(buffer, "# generated by hdspm\n");
5214
5215 for (i = 0; i < hdspm->max_channels_out; i++) {
5216 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5217 }
5218}
5219
5220
Bill Pembertone23e7a12012-12-06 12:35:10 -05005221static void snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005222{
Takashi Iwai98274f02005-11-17 14:52:34 +01005223 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02005224
Adrian Knoth0dca1792011-01-26 19:32:14 +01005225 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5226 switch (hdspm->io_type) {
5227 case AES32:
5228 snd_info_set_text_ops(entry, hdspm,
5229 snd_hdspm_proc_read_aes32);
5230 break;
5231 case MADI:
5232 snd_info_set_text_ops(entry, hdspm,
5233 snd_hdspm_proc_read_madi);
5234 break;
5235 case MADIface:
5236 /* snd_info_set_text_ops(entry, hdspm,
5237 snd_hdspm_proc_read_madiface); */
5238 break;
5239 case RayDAT:
5240 snd_info_set_text_ops(entry, hdspm,
5241 snd_hdspm_proc_read_raydat);
5242 break;
5243 case AIO:
5244 break;
5245 }
5246 }
5247
5248 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5249 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5250 }
5251
5252 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5253 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5254 }
5255
Remy Bruno3cee5a62006-10-16 12:46:32 +02005256#ifdef CONFIG_SND_DEBUG
5257 /* debug file to read all hdspm registers */
5258 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5259 snd_info_set_text_ops(entry, hdspm,
5260 snd_hdspm_proc_read_debug);
5261#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02005262}
5263
5264/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005265 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005266 ------------------------------------------------------------*/
5267
Takashi Iwai98274f02005-11-17 14:52:34 +01005268static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005269{
Takashi Iwai763f3562005-06-03 11:25:34 +02005270 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005271 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005272 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005273
5274 /* set defaults: */
5275
Adrian Knoth0dca1792011-01-26 19:32:14 +01005276 hdspm->settings_register = 0;
5277
5278 switch (hdspm->io_type) {
5279 case MADI:
5280 case MADIface:
5281 hdspm->control_register =
5282 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5283 break;
5284
5285 case RayDAT:
5286 case AIO:
5287 hdspm->settings_register = 0x1 + 0x1000;
5288 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5289 * line_out */
5290 hdspm->control_register =
5291 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5292 break;
5293
5294 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005295 hdspm->control_register =
Adrian Knothe71b95a2013-07-05 11:28:06 +02005296 HDSPM_ClockModeMaster | /* Master Clock Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005297 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005298 HDSPM_SyncRef0 | /* AES1 is syncclock */
5299 HDSPM_LineOut | /* Analog output in */
5300 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005301 break;
5302 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005303
5304 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5305
Adrian Knoth0dca1792011-01-26 19:32:14 +01005306 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005307 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005308#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005309 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005310#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005311 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005312#endif
5313
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005314 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5315 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005316 hdspm_compute_period_size(hdspm);
5317
5318 /* silence everything */
5319
5320 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5321
Adrian Knothb2ed6322013-07-05 11:27:54 +02005322 if (hdspm_is_raydat_or_aio(hdspm))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005323 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005324
5325 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005326 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005327
5328 return 0;
5329}
5330
5331
5332/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005333 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005334 ------------------------------------------------------------*/
5335
David Howells7d12e782006-10-05 14:55:46 +01005336static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005337{
Takashi Iwai98274f02005-11-17 14:52:34 +01005338 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005339 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005340 int i, audio, midi, schedule = 0;
5341 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005342
5343 status = hdspm_read(hdspm, HDSPM_statusRegister);
5344
5345 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005346 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5347 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005348
Adrian Knoth0dca1792011-01-26 19:32:14 +01005349 /* now = get_cycles(); */
5350 /**
5351 * LAT_2..LAT_0 period counter (win) counter (mac)
5352 * 6 4096 ~256053425 ~514672358
5353 * 5 2048 ~128024983 ~257373821
5354 * 4 1024 ~64023706 ~128718089
5355 * 3 512 ~32005945 ~64385999
5356 * 2 256 ~16003039 ~32260176
5357 * 1 128 ~7998738 ~16194507
5358 * 0 64 ~3998231 ~8191558
5359 **/
5360 /*
5361 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5362 now-hdspm->last_interrupt, status & 0xFFC0);
5363 hdspm->last_interrupt = now;
5364 */
5365
5366 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005367 return IRQ_NONE;
5368
5369 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5370 hdspm->irq_count++;
5371
Takashi Iwai763f3562005-06-03 11:25:34 +02005372
5373 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005374 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005375 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005376
5377 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005378 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005379 }
5380
Adrian Knoth0dca1792011-01-26 19:32:14 +01005381 if (midi) {
5382 i = 0;
5383 while (i < hdspm->midiPorts) {
5384 if ((hdspm_read(hdspm,
5385 hdspm->midi[i].statusIn) & 0xff) &&
5386 (status & hdspm->midi[i].irq)) {
5387 /* we disable interrupts for this input until
5388 * processing is done
5389 */
5390 hdspm->control_register &= ~hdspm->midi[i].ie;
5391 hdspm_write(hdspm, HDSPM_controlRegister,
5392 hdspm->control_register);
5393 hdspm->midi[i].pending = 1;
5394 schedule = 1;
5395 }
5396
5397 i++;
5398 }
5399
5400 if (schedule)
5401 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005402 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005403
Takashi Iwai763f3562005-06-03 11:25:34 +02005404 return IRQ_HANDLED;
5405}
5406
5407/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005408 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005409 ------------------------------------------------------------*/
5410
5411
Adrian Knoth0dca1792011-01-26 19:32:14 +01005412static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5413 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005414{
Takashi Iwai98274f02005-11-17 14:52:34 +01005415 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005416 return hdspm_hw_pointer(hdspm);
5417}
5418
Takashi Iwai763f3562005-06-03 11:25:34 +02005419
Takashi Iwai98274f02005-11-17 14:52:34 +01005420static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005421{
Takashi Iwai98274f02005-11-17 14:52:34 +01005422 struct snd_pcm_runtime *runtime = substream->runtime;
5423 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5424 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005425
5426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5427 other = hdspm->capture_substream;
5428 else
5429 other = hdspm->playback_substream;
5430
5431 if (hdspm->running)
5432 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5433 else
5434 runtime->status->hw_ptr = 0;
5435 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005436 struct snd_pcm_substream *s;
5437 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005438 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005439 if (s == other) {
5440 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005441 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005442 break;
5443 }
5444 }
5445 }
5446 return 0;
5447}
5448
Takashi Iwai98274f02005-11-17 14:52:34 +01005449static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5450 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005451{
Takashi Iwai98274f02005-11-17 14:52:34 +01005452 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005453 int err;
5454 int i;
5455 pid_t this_pid;
5456 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005457
5458 spin_lock_irq(&hdspm->lock);
5459
5460 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5461 this_pid = hdspm->playback_pid;
5462 other_pid = hdspm->capture_pid;
5463 } else {
5464 this_pid = hdspm->capture_pid;
5465 other_pid = hdspm->playback_pid;
5466 }
5467
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005468 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005469
5470 /* The other stream is open, and not by the same
5471 task as this one. Make sure that the parameters
5472 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005473 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005474
5475 if (params_rate(params) != hdspm->system_sample_rate) {
5476 spin_unlock_irq(&hdspm->lock);
5477 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005478 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005479 return -EBUSY;
5480 }
5481
5482 if (params_period_size(params) != hdspm->period_bytes / 4) {
5483 spin_unlock_irq(&hdspm->lock);
5484 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005485 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005486 return -EBUSY;
5487 }
5488
5489 }
5490 /* We're fine. */
5491 spin_unlock_irq(&hdspm->lock);
5492
5493 /* how to make sure that the rate matches an externally-set one ? */
5494
5495 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005496 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5497 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005498 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005499 spin_unlock_irq(&hdspm->lock);
5500 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005501 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005502 return err;
5503 }
5504 spin_unlock_irq(&hdspm->lock);
5505
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005506 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005507 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005508 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005509 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005510 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005511 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005512 return err;
5513 }
5514
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005515 /* Memory allocation, takashi's method, dont know if we should
5516 * spinlock
5517 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005518 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005519 /* Update for MADI rev 204: we need to allocate for all channels,
5520 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005521
Takashi Iwai763f3562005-06-03 11:25:34 +02005522 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005523 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5524 if (err < 0) {
5525 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005526 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005527 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005528
Takashi Iwai763f3562005-06-03 11:25:34 +02005529 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5530
Takashi Iwai77a23f22008-08-21 13:00:13 +02005531 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005532 params_channels(params));
5533
5534 for (i = 0; i < params_channels(params); ++i)
5535 snd_hdspm_enable_out(hdspm, i, 1);
5536
5537 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005538 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005539 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005540 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005541 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005542 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005543 params_channels(params));
5544
5545 for (i = 0; i < params_channels(params); ++i)
5546 snd_hdspm_enable_in(hdspm, i, 1);
5547
5548 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005549 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005550 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005551 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005552 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005553
Remy Bruno3cee5a62006-10-16 12:46:32 +02005554 /*
5555 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5556 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5557 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005558 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005559 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005560 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005561 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5562 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5563 "playback" : "capture",
5564 params_rate(params), params_channels(params),
5565 params_buffer_size(params));
5566 */
5567
5568
5569 /* Switch to native float format if requested */
5570 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5571 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5572 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5573
5574 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5575 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5576 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5577 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5578
5579 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5580 }
5581 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5582
Takashi Iwai763f3562005-06-03 11:25:34 +02005583 return 0;
5584}
5585
Takashi Iwai98274f02005-11-17 14:52:34 +01005586static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005587{
5588 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005589 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005590
5591 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5592
Adrian Knoth0dca1792011-01-26 19:32:14 +01005593 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005594 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005595 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005596 snd_hdspm_enable_out(hdspm, i, 0);
5597
5598 hdspm->playback_buffer = NULL;
5599 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005600 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005601 snd_hdspm_enable_in(hdspm, i, 0);
5602
5603 hdspm->capture_buffer = NULL;
5604
5605 }
5606
5607 snd_pcm_lib_free_pages(substream);
5608
5609 return 0;
5610}
5611
Adrian Knoth0dca1792011-01-26 19:32:14 +01005612
Takashi Iwai98274f02005-11-17 14:52:34 +01005613static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005614 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005615{
Takashi Iwai98274f02005-11-17 14:52:34 +01005616 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005617
Adrian Knoth0dca1792011-01-26 19:32:14 +01005618 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5619 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5620 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5621 return -EINVAL;
5622 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005623
Adrian Knoth0dca1792011-01-26 19:32:14 +01005624 if (hdspm->channel_map_out[info->channel] < 0) {
5625 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5626 return -EINVAL;
5627 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005628
Adrian Knoth0dca1792011-01-26 19:32:14 +01005629 info->offset = hdspm->channel_map_out[info->channel] *
5630 HDSPM_CHANNEL_BUFFER_BYTES;
5631 } else {
5632 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5633 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5634 return -EINVAL;
5635 }
5636
5637 if (hdspm->channel_map_in[info->channel] < 0) {
5638 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5639 return -EINVAL;
5640 }
5641
5642 info->offset = hdspm->channel_map_in[info->channel] *
5643 HDSPM_CHANNEL_BUFFER_BYTES;
5644 }
5645
Takashi Iwai763f3562005-06-03 11:25:34 +02005646 info->first = 0;
5647 info->step = 32;
5648 return 0;
5649}
5650
Adrian Knoth0dca1792011-01-26 19:32:14 +01005651
Takashi Iwai98274f02005-11-17 14:52:34 +01005652static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005653 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005654{
5655 switch (cmd) {
5656 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005657 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005658
5659 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005660 {
5661 struct snd_pcm_channel_info *info = arg;
5662 return snd_hdspm_channel_info(substream, info);
5663 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005664 default:
5665 break;
5666 }
5667
5668 return snd_pcm_lib_ioctl(substream, cmd, arg);
5669}
5670
Takashi Iwai98274f02005-11-17 14:52:34 +01005671static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005672{
Takashi Iwai98274f02005-11-17 14:52:34 +01005673 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5674 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005675 int running;
5676
5677 spin_lock(&hdspm->lock);
5678 running = hdspm->running;
5679 switch (cmd) {
5680 case SNDRV_PCM_TRIGGER_START:
5681 running |= 1 << substream->stream;
5682 break;
5683 case SNDRV_PCM_TRIGGER_STOP:
5684 running &= ~(1 << substream->stream);
5685 break;
5686 default:
5687 snd_BUG();
5688 spin_unlock(&hdspm->lock);
5689 return -EINVAL;
5690 }
5691 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5692 other = hdspm->capture_substream;
5693 else
5694 other = hdspm->playback_substream;
5695
5696 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005697 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005698 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005699 if (s == other) {
5700 snd_pcm_trigger_done(s, substream);
5701 if (cmd == SNDRV_PCM_TRIGGER_START)
5702 running |= 1 << s->stream;
5703 else
5704 running &= ~(1 << s->stream);
5705 goto _ok;
5706 }
5707 }
5708 if (cmd == SNDRV_PCM_TRIGGER_START) {
5709 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005710 && substream->stream ==
5711 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005712 hdspm_silence_playback(hdspm);
5713 } else {
5714 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005715 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005716 hdspm_silence_playback(hdspm);
5717 }
5718 } else {
5719 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5720 hdspm_silence_playback(hdspm);
5721 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005722_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005723 snd_pcm_trigger_done(substream, substream);
5724 if (!hdspm->running && running)
5725 hdspm_start_audio(hdspm);
5726 else if (hdspm->running && !running)
5727 hdspm_stop_audio(hdspm);
5728 hdspm->running = running;
5729 spin_unlock(&hdspm->lock);
5730
5731 return 0;
5732}
5733
Takashi Iwai98274f02005-11-17 14:52:34 +01005734static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005735{
5736 return 0;
5737}
5738
Takashi Iwai98274f02005-11-17 14:52:34 +01005739static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005740 .info = (SNDRV_PCM_INFO_MMAP |
5741 SNDRV_PCM_INFO_MMAP_VALID |
5742 SNDRV_PCM_INFO_NONINTERLEAVED |
5743 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5744 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5745 .rates = (SNDRV_PCM_RATE_32000 |
5746 SNDRV_PCM_RATE_44100 |
5747 SNDRV_PCM_RATE_48000 |
5748 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005749 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5750 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005751 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005752 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005753 .channels_min = 1,
5754 .channels_max = HDSPM_MAX_CHANNELS,
5755 .buffer_bytes_max =
5756 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005757 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005758 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005759 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005760 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005761 .fifo_size = 0
5762};
5763
Takashi Iwai98274f02005-11-17 14:52:34 +01005764static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005765 .info = (SNDRV_PCM_INFO_MMAP |
5766 SNDRV_PCM_INFO_MMAP_VALID |
5767 SNDRV_PCM_INFO_NONINTERLEAVED |
5768 SNDRV_PCM_INFO_SYNC_START),
5769 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5770 .rates = (SNDRV_PCM_RATE_32000 |
5771 SNDRV_PCM_RATE_44100 |
5772 SNDRV_PCM_RATE_48000 |
5773 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005774 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5775 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005776 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005777 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005778 .channels_min = 1,
5779 .channels_max = HDSPM_MAX_CHANNELS,
5780 .buffer_bytes_max =
5781 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005782 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005783 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005784 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005785 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005786 .fifo_size = 0
5787};
5788
Adrian Knoth0dca1792011-01-26 19:32:14 +01005789static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5790 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005791{
Takashi Iwai98274f02005-11-17 14:52:34 +01005792 struct hdspm *hdspm = rule->private;
5793 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005794 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005795 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005796 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5797
Adrian Knoth0dca1792011-01-26 19:32:14 +01005798 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005799 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005800 .min = hdspm->qs_in_channels,
5801 .max = hdspm->qs_in_channels,
5802 .integer = 1,
5803 };
5804 return snd_interval_refine(c, &t);
5805 } else if (r->min > 48000 && r->max <= 96000) {
5806 struct snd_interval t = {
5807 .min = hdspm->ds_in_channels,
5808 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005809 .integer = 1,
5810 };
5811 return snd_interval_refine(c, &t);
5812 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005813 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005814 .min = hdspm->ss_in_channels,
5815 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005816 .integer = 1,
5817 };
5818 return snd_interval_refine(c, &t);
5819 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005820
Takashi Iwai763f3562005-06-03 11:25:34 +02005821 return 0;
5822}
5823
Adrian Knoth0dca1792011-01-26 19:32:14 +01005824static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005825 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005826{
Takashi Iwai98274f02005-11-17 14:52:34 +01005827 struct hdspm *hdspm = rule->private;
5828 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005829 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005830 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005831 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5832
Adrian Knoth0dca1792011-01-26 19:32:14 +01005833 if (r->min > 96000 && r->max <= 192000) {
5834 struct snd_interval t = {
5835 .min = hdspm->qs_out_channels,
5836 .max = hdspm->qs_out_channels,
5837 .integer = 1,
5838 };
5839 return snd_interval_refine(c, &t);
5840 } else if (r->min > 48000 && r->max <= 96000) {
5841 struct snd_interval t = {
5842 .min = hdspm->ds_out_channels,
5843 .max = hdspm->ds_out_channels,
5844 .integer = 1,
5845 };
5846 return snd_interval_refine(c, &t);
5847 } else if (r->max < 64000) {
5848 struct snd_interval t = {
5849 .min = hdspm->ss_out_channels,
5850 .max = hdspm->ss_out_channels,
5851 .integer = 1,
5852 };
5853 return snd_interval_refine(c, &t);
5854 } else {
5855 }
5856 return 0;
5857}
5858
5859static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5860 struct snd_pcm_hw_rule * rule)
5861{
5862 struct hdspm *hdspm = rule->private;
5863 struct snd_interval *c =
5864 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5865 struct snd_interval *r =
5866 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5867
5868 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005869 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005870 .min = 32000,
5871 .max = 48000,
5872 .integer = 1,
5873 };
5874 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005875 } else if (c->max <= hdspm->qs_in_channels) {
5876 struct snd_interval t = {
5877 .min = 128000,
5878 .max = 192000,
5879 .integer = 1,
5880 };
5881 return snd_interval_refine(r, &t);
5882 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005883 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005884 .min = 64000,
5885 .max = 96000,
5886 .integer = 1,
5887 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005888 return snd_interval_refine(r, &t);
5889 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005890
5891 return 0;
5892}
5893static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5894 struct snd_pcm_hw_rule *rule)
5895{
5896 struct hdspm *hdspm = rule->private;
5897 struct snd_interval *c =
5898 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5899 struct snd_interval *r =
5900 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5901
5902 if (c->min >= hdspm->ss_out_channels) {
5903 struct snd_interval t = {
5904 .min = 32000,
5905 .max = 48000,
5906 .integer = 1,
5907 };
5908 return snd_interval_refine(r, &t);
5909 } else if (c->max <= hdspm->qs_out_channels) {
5910 struct snd_interval t = {
5911 .min = 128000,
5912 .max = 192000,
5913 .integer = 1,
5914 };
5915 return snd_interval_refine(r, &t);
5916 } else if (c->max <= hdspm->ds_out_channels) {
5917 struct snd_interval t = {
5918 .min = 64000,
5919 .max = 96000,
5920 .integer = 1,
5921 };
5922 return snd_interval_refine(r, &t);
5923 }
5924
Takashi Iwai763f3562005-06-03 11:25:34 +02005925 return 0;
5926}
5927
Adrian Knoth0dca1792011-01-26 19:32:14 +01005928static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005929 struct snd_pcm_hw_rule *rule)
5930{
5931 unsigned int list[3];
5932 struct hdspm *hdspm = rule->private;
5933 struct snd_interval *c = hw_param_interval(params,
5934 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005935
5936 list[0] = hdspm->qs_in_channels;
5937 list[1] = hdspm->ds_in_channels;
5938 list[2] = hdspm->ss_in_channels;
5939 return snd_interval_list(c, 3, list, 0);
5940}
5941
5942static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5943 struct snd_pcm_hw_rule *rule)
5944{
5945 unsigned int list[3];
5946 struct hdspm *hdspm = rule->private;
5947 struct snd_interval *c = hw_param_interval(params,
5948 SNDRV_PCM_HW_PARAM_CHANNELS);
5949
5950 list[0] = hdspm->qs_out_channels;
5951 list[1] = hdspm->ds_out_channels;
5952 list[2] = hdspm->ss_out_channels;
5953 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005954}
5955
5956
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005957static unsigned int hdspm_aes32_sample_rates[] = {
5958 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5959};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005960
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005961static struct snd_pcm_hw_constraint_list
5962hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005963 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5964 .list = hdspm_aes32_sample_rates,
5965 .mask = 0
5966};
5967
Takashi Iwai98274f02005-11-17 14:52:34 +01005968static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005969{
Takashi Iwai98274f02005-11-17 14:52:34 +01005970 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5971 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005972
Takashi Iwai763f3562005-06-03 11:25:34 +02005973 spin_lock_irq(&hdspm->lock);
5974
5975 snd_pcm_set_sync(substream);
5976
Adrian Knoth0dca1792011-01-26 19:32:14 +01005977
Takashi Iwai763f3562005-06-03 11:25:34 +02005978 runtime->hw = snd_hdspm_playback_subinfo;
5979
5980 if (hdspm->capture_substream == NULL)
5981 hdspm_stop_audio(hdspm);
5982
5983 hdspm->playback_pid = current->pid;
5984 hdspm->playback_substream = substream;
5985
5986 spin_unlock_irq(&hdspm->lock);
5987
5988 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005989 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005990
Adrian Knoth0dca1792011-01-26 19:32:14 +01005991 switch (hdspm->io_type) {
5992 case AIO:
5993 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005994 snd_pcm_hw_constraint_minmax(runtime,
5995 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5996 32, 4096);
5997 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
5998 snd_pcm_hw_constraint_minmax(runtime,
5999 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6000 16384, 16384);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006001 break;
6002
6003 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02006004 snd_pcm_hw_constraint_minmax(runtime,
6005 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6006 64, 8192);
6007 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006008 }
6009
6010 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02006011 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006012 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6013 &hdspm_hw_constraints_aes32_sample_rates);
6014 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006015 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006016 snd_hdspm_hw_rule_rate_out_channels, hdspm,
6017 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006018 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006019
6020 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6021 snd_hdspm_hw_rule_out_channels, hdspm,
6022 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6023
6024 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6025 snd_hdspm_hw_rule_out_channels_rate, hdspm,
6026 SNDRV_PCM_HW_PARAM_RATE, -1);
6027
Takashi Iwai763f3562005-06-03 11:25:34 +02006028 return 0;
6029}
6030
Takashi Iwai98274f02005-11-17 14:52:34 +01006031static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006032{
Takashi Iwai98274f02005-11-17 14:52:34 +01006033 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006034
6035 spin_lock_irq(&hdspm->lock);
6036
6037 hdspm->playback_pid = -1;
6038 hdspm->playback_substream = NULL;
6039
6040 spin_unlock_irq(&hdspm->lock);
6041
6042 return 0;
6043}
6044
6045
Takashi Iwai98274f02005-11-17 14:52:34 +01006046static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006047{
Takashi Iwai98274f02005-11-17 14:52:34 +01006048 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6049 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02006050
6051 spin_lock_irq(&hdspm->lock);
6052 snd_pcm_set_sync(substream);
6053 runtime->hw = snd_hdspm_capture_subinfo;
6054
6055 if (hdspm->playback_substream == NULL)
6056 hdspm_stop_audio(hdspm);
6057
6058 hdspm->capture_pid = current->pid;
6059 hdspm->capture_substream = substream;
6060
6061 spin_unlock_irq(&hdspm->lock);
6062
6063 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02006064 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
6065
Adrian Knoth0dca1792011-01-26 19:32:14 +01006066 switch (hdspm->io_type) {
6067 case AIO:
6068 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02006069 snd_pcm_hw_constraint_minmax(runtime,
6070 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6071 32, 4096);
6072 snd_pcm_hw_constraint_minmax(runtime,
6073 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6074 16384, 16384);
6075 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006076
6077 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02006078 snd_pcm_hw_constraint_minmax(runtime,
6079 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6080 64, 8192);
6081 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006082 }
6083
6084 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02006085 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006086 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6087 &hdspm_hw_constraints_aes32_sample_rates);
6088 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006089 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006090 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6091 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006092 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006093
6094 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6095 snd_hdspm_hw_rule_in_channels, hdspm,
6096 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6097
6098 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6099 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6100 SNDRV_PCM_HW_PARAM_RATE, -1);
6101
Takashi Iwai763f3562005-06-03 11:25:34 +02006102 return 0;
6103}
6104
Takashi Iwai98274f02005-11-17 14:52:34 +01006105static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006106{
Takashi Iwai98274f02005-11-17 14:52:34 +01006107 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006108
6109 spin_lock_irq(&hdspm->lock);
6110
6111 hdspm->capture_pid = -1;
6112 hdspm->capture_substream = NULL;
6113
6114 spin_unlock_irq(&hdspm->lock);
6115 return 0;
6116}
6117
Adrian Knoth0dca1792011-01-26 19:32:14 +01006118static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02006119{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006120 /* we have nothing to initialize but the call is required */
6121 return 0;
6122}
6123
6124static inline int copy_u32_le(void __user *dest, void __iomem *src)
6125{
6126 u32 val = readl(src);
6127 return copy_to_user(dest, &val, 4);
6128}
6129
6130static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006131 unsigned int cmd, unsigned long arg)
Adrian Knoth0dca1792011-01-26 19:32:14 +01006132{
6133 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006134 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01006135 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006136 struct hdspm_config info;
6137 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01006138 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006139 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006140 struct hdspm_ltc ltc;
6141 unsigned int statusregister;
6142 long unsigned int s;
6143 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02006144
6145 switch (cmd) {
6146
Takashi Iwai763f3562005-06-03 11:25:34 +02006147 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006148 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006149 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006150 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006151 readl(hdspm->iobase +
6152 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006153 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006154 readl(hdspm->iobase +
6155 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006156 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006157 readl(hdspm->iobase +
6158 HDSPM_MADI_OUTPUT_PEAK + i*4);
6159
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006160 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006161 ((uint64_t) readl(hdspm->iobase +
6162 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6163 (uint64_t) readl(hdspm->iobase +
6164 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006165 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006166 ((uint64_t)readl(hdspm->iobase +
6167 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6168 (uint64_t)readl(hdspm->iobase +
6169 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006170 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006171 ((uint64_t)readl(hdspm->iobase +
6172 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6173 (uint64_t)readl(hdspm->iobase +
6174 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6175 }
6176
6177 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006178 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006179 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006180 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006181 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006182 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006183 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006184 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006185
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006186 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01006187 if (0 != s) {
6188 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6189 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6190 */
Takashi Iwai763f3562005-06-03 11:25:34 +02006191 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006192 }
6193 break;
6194
6195 case SNDRV_HDSPM_IOCTL_GET_LTC:
6196 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6197 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6198 if (i & HDSPM_TCO1_LTC_Input_valid) {
6199 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6200 HDSPM_TCO1_LTC_Format_MSB)) {
6201 case 0:
6202 ltc.format = fps_24;
6203 break;
6204 case HDSPM_TCO1_LTC_Format_LSB:
6205 ltc.format = fps_25;
6206 break;
6207 case HDSPM_TCO1_LTC_Format_MSB:
6208 ltc.format = fps_2997;
6209 break;
6210 default:
6211 ltc.format = 30;
6212 break;
6213 }
6214 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6215 ltc.frame = drop_frame;
6216 } else {
6217 ltc.frame = full_frame;
6218 }
6219 } else {
6220 ltc.format = format_invalid;
6221 ltc.frame = frame_invalid;
6222 }
6223 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6224 ltc.input_format = ntsc;
6225 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6226 ltc.input_format = pal;
6227 } else {
6228 ltc.input_format = no_video;
6229 }
6230
6231 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6232 if (0 != s) {
6233 /*
6234 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02006235 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006236 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006237
6238 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02006239
Adrian Knoth0dca1792011-01-26 19:32:14 +01006240 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02006241
Adrian Knoth4ab69a22011-02-23 11:43:14 +01006242 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02006243 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006244 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6245 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006246
6247 info.system_sample_rate = hdspm->system_sample_rate;
6248 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006249 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006250 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6251 info.clock_source = hdspm_clock_source(hdspm);
6252 info.autosync_ref = hdspm_autosync_ref(hdspm);
Adrian Knothc9e16682012-12-03 14:55:50 +01006253 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
Takashi Iwai763f3562005-06-03 11:25:34 +02006254 info.passthru = 0;
6255 spin_unlock_irq(&hdspm->lock);
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006256 if (copy_to_user(argp, &info, sizeof(info)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006257 return -EFAULT;
6258 break;
6259
Adrian Knoth0dca1792011-01-26 19:32:14 +01006260 case SNDRV_HDSPM_IOCTL_GET_STATUS:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006261 memset(&status, 0, sizeof(status));
6262
Adrian Knoth0dca1792011-01-26 19:32:14 +01006263 status.card_type = hdspm->io_type;
6264
6265 status.autosync_source = hdspm_autosync_ref(hdspm);
6266
6267 status.card_clock = 110069313433624ULL;
6268 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6269
6270 switch (hdspm->io_type) {
6271 case MADI:
6272 case MADIface:
6273 status.card_specific.madi.sync_wc =
6274 hdspm_wc_sync_check(hdspm);
6275 status.card_specific.madi.sync_madi =
6276 hdspm_madi_sync_check(hdspm);
6277 status.card_specific.madi.sync_tco =
6278 hdspm_tco_sync_check(hdspm);
6279 status.card_specific.madi.sync_in =
6280 hdspm_sync_in_sync_check(hdspm);
6281
6282 statusregister =
6283 hdspm_read(hdspm, HDSPM_statusRegister);
6284 status.card_specific.madi.madi_input =
6285 (statusregister & HDSPM_AB_int) ? 1 : 0;
6286 status.card_specific.madi.channel_format =
Adrian Knoth9e6ff522011-10-27 21:57:52 +02006287 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006288 /* TODO: Mac driver sets it when f_s>48kHz */
6289 status.card_specific.madi.frame_format = 0;
6290
6291 default:
6292 break;
6293 }
6294
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006295 if (copy_to_user(argp, &status, sizeof(status)))
Adrian Knoth0dca1792011-01-26 19:32:14 +01006296 return -EFAULT;
6297
6298
6299 break;
6300
Takashi Iwai763f3562005-06-03 11:25:34 +02006301 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006302 memset(&hdspm_version, 0, sizeof(hdspm_version));
6303
Adrian Knoth0dca1792011-01-26 19:32:14 +01006304 hdspm_version.card_type = hdspm->io_type;
6305 strncpy(hdspm_version.cardname, hdspm->card_name,
6306 sizeof(hdspm_version.cardname));
Adrian Knoth7d53a632012-01-04 14:31:16 +01006307 hdspm_version.serial = hdspm->serial;
Takashi Iwai763f3562005-06-03 11:25:34 +02006308 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006309 hdspm_version.addons = 0;
6310 if (hdspm->tco)
6311 hdspm_version.addons |= HDSPM_ADDON_TCO;
6312
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006313 if (copy_to_user(argp, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006314 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006315 return -EFAULT;
6316 break;
6317
6318 case SNDRV_HDSPM_IOCTL_GET_MIXER:
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006319 if (copy_from_user(&mixer, argp, sizeof(mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006320 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006321 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006322 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006323 return -EFAULT;
6324 break;
6325
6326 default:
6327 return -EINVAL;
6328 }
6329 return 0;
6330}
6331
Takashi Iwai98274f02005-11-17 14:52:34 +01006332static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006333 .open = snd_hdspm_playback_open,
6334 .close = snd_hdspm_playback_release,
6335 .ioctl = snd_hdspm_ioctl,
6336 .hw_params = snd_hdspm_hw_params,
6337 .hw_free = snd_hdspm_hw_free,
6338 .prepare = snd_hdspm_prepare,
6339 .trigger = snd_hdspm_trigger,
6340 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006341 .page = snd_pcm_sgbuf_ops_page,
6342};
6343
Takashi Iwai98274f02005-11-17 14:52:34 +01006344static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006345 .open = snd_hdspm_capture_open,
6346 .close = snd_hdspm_capture_release,
6347 .ioctl = snd_hdspm_ioctl,
6348 .hw_params = snd_hdspm_hw_params,
6349 .hw_free = snd_hdspm_hw_free,
6350 .prepare = snd_hdspm_prepare,
6351 .trigger = snd_hdspm_trigger,
6352 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006353 .page = snd_pcm_sgbuf_ops_page,
6354};
6355
Bill Pembertone23e7a12012-12-06 12:35:10 -05006356static int snd_hdspm_create_hwdep(struct snd_card *card,
6357 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006358{
Takashi Iwai98274f02005-11-17 14:52:34 +01006359 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006360 int err;
6361
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006362 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6363 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006364 return err;
6365
6366 hdspm->hwdep = hw;
6367 hw->private_data = hdspm;
6368 strcpy(hw->name, "HDSPM hwdep interface");
6369
Adrian Knoth0dca1792011-01-26 19:32:14 +01006370 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006371 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth8de5d6f2012-03-08 15:38:04 +01006372 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006373 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006374
6375 return 0;
6376}
6377
6378
6379/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006380 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006381 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05006382static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006383{
6384 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006385 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006386 size_t wanted;
6387
6388 pcm = hdspm->pcm;
6389
Remy Bruno3cee5a62006-10-16 12:46:32 +02006390 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006391
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006392 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006393 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006394 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006395 snd_dma_pci_data(hdspm->pci),
6396 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006397 wanted);
6398 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006399 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006400
6401 return err;
6402 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006403 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006404
6405 return 0;
6406}
6407
Adrian Knoth0dca1792011-01-26 19:32:14 +01006408
6409static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006410 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006411 unsigned int reg, int channels)
6412{
6413 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006414
6415 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006416 for (i = 0; i < (channels * 16); i++)
6417 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006418 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006419}
6420
Adrian Knoth0dca1792011-01-26 19:32:14 +01006421
Takashi Iwai763f3562005-06-03 11:25:34 +02006422/* ------------- ALSA Devices ---------------------------- */
Bill Pembertone23e7a12012-12-06 12:35:10 -05006423static int snd_hdspm_create_pcm(struct snd_card *card,
6424 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006425{
Takashi Iwai98274f02005-11-17 14:52:34 +01006426 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006427 int err;
6428
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006429 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6430 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006431 return err;
6432
6433 hdspm->pcm = pcm;
6434 pcm->private_data = hdspm;
6435 strcpy(pcm->name, hdspm->card_name);
6436
6437 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6438 &snd_hdspm_playback_ops);
6439 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6440 &snd_hdspm_capture_ops);
6441
6442 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6443
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006444 err = snd_hdspm_preallocate_memory(hdspm);
6445 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006446 return err;
6447
6448 return 0;
6449}
6450
Takashi Iwai98274f02005-11-17 14:52:34 +01006451static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006452{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006453 int i;
6454
6455 for (i = 0; i < hdspm->midiPorts; i++)
6456 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006457}
6458
Bill Pembertone23e7a12012-12-06 12:35:10 -05006459static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6460 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006461{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006462 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006463
6464 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006465 err = snd_hdspm_create_pcm(card, hdspm);
6466 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006467 return err;
6468
Adrian Knoth0dca1792011-01-26 19:32:14 +01006469 i = 0;
6470 while (i < hdspm->midiPorts) {
6471 err = snd_hdspm_create_midi(card, hdspm, i);
6472 if (err < 0) {
6473 return err;
6474 }
6475 i++;
6476 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006477
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006478 err = snd_hdspm_create_controls(card, hdspm);
6479 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006480 return err;
6481
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006482 err = snd_hdspm_create_hwdep(card, hdspm);
6483 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006484 return err;
6485
6486 snd_printdd("proc init...\n");
6487 snd_hdspm_proc_init(hdspm);
6488
6489 hdspm->system_sample_rate = -1;
6490 hdspm->last_external_sample_rate = -1;
6491 hdspm->last_internal_sample_rate = -1;
6492 hdspm->playback_pid = -1;
6493 hdspm->capture_pid = -1;
6494 hdspm->capture_substream = NULL;
6495 hdspm->playback_substream = NULL;
6496
6497 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006498 err = snd_hdspm_set_defaults(hdspm);
6499 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006500 return err;
6501
6502 snd_printdd("Update mixer controls...\n");
6503 hdspm_update_simple_mixer_controls(hdspm);
6504
6505 snd_printdd("Initializeing complete ???\n");
6506
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006507 err = snd_card_register(card);
6508 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006509 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6510 return err;
6511 }
6512
6513 snd_printdd("... yes now\n");
6514
6515 return 0;
6516}
6517
Bill Pembertone23e7a12012-12-06 12:35:10 -05006518static int snd_hdspm_create(struct snd_card *card,
6519 struct hdspm *hdspm)
6520{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006521
Takashi Iwai763f3562005-06-03 11:25:34 +02006522 struct pci_dev *pci = hdspm->pci;
6523 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006524 unsigned long io_extent;
6525
6526 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006527 hdspm->card = card;
6528
6529 spin_lock_init(&hdspm->lock);
6530
Takashi Iwai763f3562005-06-03 11:25:34 +02006531 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006532 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006533
Takashi Iwai763f3562005-06-03 11:25:34 +02006534 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006535 strcpy(card->driver, "HDSPM");
6536
6537 switch (hdspm->firmware_rev) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01006538 case HDSPM_RAYDAT_REV:
6539 hdspm->io_type = RayDAT;
6540 hdspm->card_name = "RME RayDAT";
6541 hdspm->midiPorts = 2;
6542 break;
6543 case HDSPM_AIO_REV:
6544 hdspm->io_type = AIO;
6545 hdspm->card_name = "RME AIO";
6546 hdspm->midiPorts = 1;
6547 break;
6548 case HDSPM_MADIFACE_REV:
6549 hdspm->io_type = MADIface;
6550 hdspm->card_name = "RME MADIface";
6551 hdspm->midiPorts = 1;
6552 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006553 default:
Adrian Knothc09403d2011-10-27 21:57:54 +02006554 if ((hdspm->firmware_rev == 0xf0) ||
6555 ((hdspm->firmware_rev >= 0xe6) &&
6556 (hdspm->firmware_rev <= 0xea))) {
6557 hdspm->io_type = AES32;
6558 hdspm->card_name = "RME AES32";
6559 hdspm->midiPorts = 2;
Adrian Knoth05c7cc92011-11-21 16:15:36 +01006560 } else if ((hdspm->firmware_rev == 0xd2) ||
Adrian Knothc09403d2011-10-27 21:57:54 +02006561 ((hdspm->firmware_rev >= 0xc8) &&
6562 (hdspm->firmware_rev <= 0xcf))) {
6563 hdspm->io_type = MADI;
6564 hdspm->card_name = "RME MADI";
6565 hdspm->midiPorts = 3;
6566 } else {
6567 snd_printk(KERN_ERR
6568 "HDSPM: unknown firmware revision %x\n",
Adrian Knoth5027f342011-02-28 15:14:49 +01006569 hdspm->firmware_rev);
Adrian Knothc09403d2011-10-27 21:57:54 +02006570 return -ENODEV;
6571 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02006572 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006573
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006574 err = pci_enable_device(pci);
6575 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006576 return err;
6577
6578 pci_set_master(hdspm->pci);
6579
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006580 err = pci_request_regions(pci, "hdspm");
6581 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006582 return err;
6583
6584 hdspm->port = pci_resource_start(pci, 0);
6585 io_extent = pci_resource_len(pci, 0);
6586
6587 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006588 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006589
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006590 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6591 if (!hdspm->iobase) {
6592 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006593 "unable to remap region 0x%lx-0x%lx\n",
6594 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006595 return -EBUSY;
6596 }
6597 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006598 (unsigned long)hdspm->iobase, hdspm->port,
6599 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006600
6601 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006602 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006603 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6604 return -EBUSY;
6605 }
6606
6607 snd_printdd("use IRQ %d\n", pci->irq);
6608
6609 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006610
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006611 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006612 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006613 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6614 if (!hdspm->mixer) {
6615 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006616 "unable to kmalloc Mixer memory of %d Bytes\n",
6617 (int)sizeof(struct hdspm_mixer));
Julia Lawallb17cbdd2012-08-19 09:02:54 +02006618 return -ENOMEM;
Takashi Iwai763f3562005-06-03 11:25:34 +02006619 }
6620
Adrian Knoth0dca1792011-01-26 19:32:14 +01006621 hdspm->port_names_in = NULL;
6622 hdspm->port_names_out = NULL;
6623
6624 switch (hdspm->io_type) {
6625 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006626 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6627 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6628 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006629
6630 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6631 channel_map_aes32;
6632 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6633 channel_map_aes32;
6634 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6635 channel_map_aes32;
6636 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6637 texts_ports_aes32;
6638 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6639 texts_ports_aes32;
6640 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6641 texts_ports_aes32;
6642
Adrian Knothd2d10a22011-02-28 15:14:47 +01006643 hdspm->max_channels_out = hdspm->max_channels_in =
6644 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006645 hdspm->port_names_in = hdspm->port_names_out =
6646 texts_ports_aes32;
6647 hdspm->channel_map_in = hdspm->channel_map_out =
6648 channel_map_aes32;
6649
Adrian Knoth0dca1792011-01-26 19:32:14 +01006650 break;
6651
6652 case MADI:
6653 case MADIface:
6654 hdspm->ss_in_channels = hdspm->ss_out_channels =
6655 MADI_SS_CHANNELS;
6656 hdspm->ds_in_channels = hdspm->ds_out_channels =
6657 MADI_DS_CHANNELS;
6658 hdspm->qs_in_channels = hdspm->qs_out_channels =
6659 MADI_QS_CHANNELS;
6660
6661 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6662 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006663 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006664 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006665 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006666 channel_map_unity_ss;
6667
6668 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6669 texts_ports_madi;
6670 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6671 texts_ports_madi;
6672 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6673 texts_ports_madi;
6674 break;
6675
6676 case AIO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006677 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6678 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6679 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6680 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6681 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6682 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6683
Adrian Knoth3de9db22013-07-05 11:28:02 +02006684 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6685 snd_printk(KERN_INFO "HDSPM: AEB input board found\n");
6686 hdspm->ss_in_channels += 4;
6687 hdspm->ds_in_channels += 4;
6688 hdspm->qs_in_channels += 4;
6689 }
6690
6691 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6692 snd_printk(KERN_INFO "HDSPM: AEB output board found\n");
6693 hdspm->ss_out_channels += 4;
6694 hdspm->ds_out_channels += 4;
6695 hdspm->qs_out_channels += 4;
6696 }
6697
Adrian Knoth0dca1792011-01-26 19:32:14 +01006698 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6699 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6700 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6701
6702 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6703 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6704 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6705
6706 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6707 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6708 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6709 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6710 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6711 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6712
6713 break;
6714
6715 case RayDAT:
6716 hdspm->ss_in_channels = hdspm->ss_out_channels =
6717 RAYDAT_SS_CHANNELS;
6718 hdspm->ds_in_channels = hdspm->ds_out_channels =
6719 RAYDAT_DS_CHANNELS;
6720 hdspm->qs_in_channels = hdspm->qs_out_channels =
6721 RAYDAT_QS_CHANNELS;
6722
6723 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6724 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6725
6726 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6727 channel_map_raydat_ss;
6728 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6729 channel_map_raydat_ds;
6730 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6731 channel_map_raydat_qs;
6732 hdspm->channel_map_in = hdspm->channel_map_out =
6733 channel_map_raydat_ss;
6734
6735 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6736 texts_ports_raydat_ss;
6737 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6738 texts_ports_raydat_ds;
6739 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6740 texts_ports_raydat_qs;
6741
6742
6743 break;
6744
6745 }
6746
6747 /* TCO detection */
6748 switch (hdspm->io_type) {
6749 case AIO:
6750 case RayDAT:
6751 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6752 HDSPM_s2_tco_detect) {
6753 hdspm->midiPorts++;
6754 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6755 GFP_KERNEL);
6756 if (NULL != hdspm->tco) {
6757 hdspm_tco_write(hdspm);
6758 }
6759 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6760 } else {
6761 hdspm->tco = NULL;
6762 }
6763 break;
6764
6765 case MADI:
6766 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6767 hdspm->midiPorts++;
6768 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6769 GFP_KERNEL);
6770 if (NULL != hdspm->tco) {
6771 hdspm_tco_write(hdspm);
6772 }
Adrian Knothe71b95a2013-07-05 11:28:06 +02006773 snd_printk(KERN_INFO "HDSPM: MADI/AES TCO module found\n");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006774 } else {
6775 hdspm->tco = NULL;
6776 }
6777 break;
6778
6779 default:
6780 hdspm->tco = NULL;
6781 }
6782
6783 /* texts */
6784 switch (hdspm->io_type) {
6785 case AES32:
6786 if (hdspm->tco) {
6787 hdspm->texts_autosync = texts_autosync_aes_tco;
Adrian Knothe71b95a2013-07-05 11:28:06 +02006788 hdspm->texts_autosync_items =
6789 ARRAY_SIZE(texts_autosync_aes_tco);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006790 } else {
6791 hdspm->texts_autosync = texts_autosync_aes;
Adrian Knothe71b95a2013-07-05 11:28:06 +02006792 hdspm->texts_autosync_items =
6793 ARRAY_SIZE(texts_autosync_aes);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006794 }
6795 break;
6796
6797 case MADI:
6798 if (hdspm->tco) {
6799 hdspm->texts_autosync = texts_autosync_madi_tco;
6800 hdspm->texts_autosync_items = 4;
6801 } else {
6802 hdspm->texts_autosync = texts_autosync_madi;
6803 hdspm->texts_autosync_items = 3;
6804 }
6805 break;
6806
6807 case MADIface:
6808
6809 break;
6810
6811 case RayDAT:
6812 if (hdspm->tco) {
6813 hdspm->texts_autosync = texts_autosync_raydat_tco;
6814 hdspm->texts_autosync_items = 9;
6815 } else {
6816 hdspm->texts_autosync = texts_autosync_raydat;
6817 hdspm->texts_autosync_items = 8;
6818 }
6819 break;
6820
6821 case AIO:
6822 if (hdspm->tco) {
6823 hdspm->texts_autosync = texts_autosync_aio_tco;
6824 hdspm->texts_autosync_items = 6;
6825 } else {
6826 hdspm->texts_autosync = texts_autosync_aio;
6827 hdspm->texts_autosync_items = 5;
6828 }
6829 break;
6830
6831 }
6832
6833 tasklet_init(&hdspm->midi_tasklet,
6834 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006835
Adrian Knothf7de8ba2012-01-10 20:58:40 +01006836
6837 if (hdspm->io_type != MADIface) {
6838 hdspm->serial = (hdspm_read(hdspm,
6839 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6840 /* id contains either a user-provided value or the default
6841 * NULL. If it's the default, we're safe to
6842 * fill card->id with the serial number.
6843 *
6844 * If the serial number is 0xFFFFFF, then we're dealing with
6845 * an old PCI revision that comes without a sane number. In
6846 * this case, we don't set card->id to avoid collisions
6847 * when running with multiple cards.
6848 */
6849 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6850 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6851 snd_card_set_id(card, card->id);
6852 }
6853 }
6854
Takashi Iwai763f3562005-06-03 11:25:34 +02006855 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006856 err = snd_hdspm_create_alsa_devices(card, hdspm);
6857 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006858 return err;
6859
6860 snd_hdspm_initialize_midi_flush(hdspm);
6861
6862 return 0;
6863}
6864
Adrian Knoth0dca1792011-01-26 19:32:14 +01006865
Takashi Iwai98274f02005-11-17 14:52:34 +01006866static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006867{
6868
6869 if (hdspm->port) {
6870
6871 /* stop th audio, and cancel all interrupts */
6872 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006873 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006874 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6875 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006876 hdspm_write(hdspm, HDSPM_controlRegister,
6877 hdspm->control_register);
6878 }
6879
6880 if (hdspm->irq >= 0)
6881 free_irq(hdspm->irq, (void *) hdspm);
6882
Jesper Juhlfc584222005-10-24 15:11:28 +02006883 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006884
6885 if (hdspm->iobase)
6886 iounmap(hdspm->iobase);
6887
Takashi Iwai763f3562005-06-03 11:25:34 +02006888 if (hdspm->port)
6889 pci_release_regions(hdspm->pci);
6890
6891 pci_disable_device(hdspm->pci);
6892 return 0;
6893}
6894
Adrian Knoth0dca1792011-01-26 19:32:14 +01006895
Takashi Iwai98274f02005-11-17 14:52:34 +01006896static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006897{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006898 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006899
6900 if (hdspm)
6901 snd_hdspm_free(hdspm);
6902}
6903
Adrian Knoth0dca1792011-01-26 19:32:14 +01006904
Bill Pembertone23e7a12012-12-06 12:35:10 -05006905static int snd_hdspm_probe(struct pci_dev *pci,
6906 const struct pci_device_id *pci_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02006907{
6908 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006909 struct hdspm *hdspm;
6910 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006911 int err;
6912
6913 if (dev >= SNDRV_CARDS)
6914 return -ENODEV;
6915 if (!enable[dev]) {
6916 dev++;
6917 return -ENOENT;
6918 }
6919
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006920 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006921 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006922 if (err < 0)
6923 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006924
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006925 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006926 card->private_free = snd_hdspm_card_free;
6927 hdspm->dev = dev;
6928 hdspm->pci = pci;
6929
Takashi Iwaic187c042007-02-19 15:27:33 +01006930 snd_card_set_dev(card, &pci->dev);
6931
Adrian Knoth0dca1792011-01-26 19:32:14 +01006932 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006933 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006934 snd_card_free(card);
6935 return err;
6936 }
6937
Adrian Knoth0dca1792011-01-26 19:32:14 +01006938 if (hdspm->io_type != MADIface) {
6939 sprintf(card->shortname, "%s_%x",
6940 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006941 hdspm->serial);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006942 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6943 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006944 hdspm->serial,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006945 hdspm->port, hdspm->irq);
6946 } else {
6947 sprintf(card->shortname, "%s", hdspm->card_name);
6948 sprintf(card->longname, "%s at 0x%lx, irq %d",
6949 hdspm->card_name, hdspm->port, hdspm->irq);
6950 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006951
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006952 err = snd_card_register(card);
6953 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006954 snd_card_free(card);
6955 return err;
6956 }
6957
6958 pci_set_drvdata(pci, card);
6959
6960 dev++;
6961 return 0;
6962}
6963
Bill Pembertone23e7a12012-12-06 12:35:10 -05006964static void snd_hdspm_remove(struct pci_dev *pci)
Takashi Iwai763f3562005-06-03 11:25:34 +02006965{
6966 snd_card_free(pci_get_drvdata(pci));
Takashi Iwai763f3562005-06-03 11:25:34 +02006967}
6968
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006969static struct pci_driver hdspm_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006970 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006971 .id_table = snd_hdspm_ids,
6972 .probe = snd_hdspm_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05006973 .remove = snd_hdspm_remove,
Takashi Iwai763f3562005-06-03 11:25:34 +02006974};
6975
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006976module_pci_driver(hdspm_driver);