blob: a0fc961bc34727fd49ae8156aa4aeeafd7b8221d [file] [log] [blame]
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001/*
Takashi Iwai763f3562005-06-03 11:25:34 +02002 * ALSA driver for RME Hammerfall DSP MADI audio interface(s)
3 *
4 * Copyright (c) 2003 Winfried Ritsch (IEM)
5 * code based on hdsp.c Paul Davis
6 * Marcus Andersson
7 * Thomas Charbonnel
Remy Bruno3cee5a62006-10-16 12:46:32 +02008 * Modified 2006-06-01 for AES32 support by Remy Bruno
9 * <remy.bruno@trinnov.com>
Takashi Iwai763f3562005-06-03 11:25:34 +020010 *
Adrian Knoth0dca1792011-01-26 19:32:14 +010011 * Modified 2009-04-13 for proper metering by Florian Faber
12 * <faber@faberman.de>
13 *
14 * Modified 2009-04-14 for native float support by Florian Faber
15 * <faber@faberman.de>
16 *
17 * Modified 2009-04-26 fixed bug in rms metering by Florian Faber
18 * <faber@faberman.de>
19 *
20 * Modified 2009-04-30 added hw serial number support by Florian Faber
21 *
22 * Modified 2011-01-14 added S/PDIF input on RayDATs by Adrian Knoth
23 *
24 * Modified 2011-01-25 variable period sizes on RayDAT/AIO by Adrian Knoth
25 *
Takashi Iwai763f3562005-06-03 11:25:34 +020026 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 *
40 */
Takashi Iwai763f3562005-06-03 11:25:34 +020041#include <linux/init.h>
42#include <linux/delay.h>
43#include <linux/interrupt.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040044#include <linux/module.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020045#include <linux/slab.h>
46#include <linux/pci.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020047#include <linux/math64.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020048#include <asm/io.h>
49
50#include <sound/core.h>
51#include <sound/control.h>
52#include <sound/pcm.h>
Adrian Knoth0dca1792011-01-26 19:32:14 +010053#include <sound/pcm_params.h>
Takashi Iwai763f3562005-06-03 11:25:34 +020054#include <sound/info.h>
55#include <sound/asoundef.h>
56#include <sound/rawmidi.h>
57#include <sound/hwdep.h>
58#include <sound/initval.h>
59
60#include <sound/hdspm.h>
61
62static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
63static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103064static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Takashi Iwai763f3562005-06-03 11:25:34 +020065
Takashi Iwai763f3562005-06-03 11:25:34 +020066module_param_array(index, int, NULL, 0444);
67MODULE_PARM_DESC(index, "Index value for RME HDSPM interface.");
68
69module_param_array(id, charp, NULL, 0444);
70MODULE_PARM_DESC(id, "ID string for RME HDSPM interface.");
71
72module_param_array(enable, bool, NULL, 0444);
73MODULE_PARM_DESC(enable, "Enable/disable specific HDSPM soundcards.");
74
Takashi Iwai763f3562005-06-03 11:25:34 +020075
76MODULE_AUTHOR
Adrian Knoth0dca1792011-01-26 19:32:14 +010077(
78 "Winfried Ritsch <ritsch_AT_iem.at>, "
79 "Paul Davis <paul@linuxaudiosystems.com>, "
80 "Marcus Andersson, Thomas Charbonnel <thomas@undata.org>, "
81 "Remy Bruno <remy.bruno@trinnov.com>, "
82 "Florian Faber <faberman@linuxproaudio.org>, "
83 "Adrian Knoth <adi@drcomp.erfurt.thur.de>"
84);
Takashi Iwai763f3562005-06-03 11:25:34 +020085MODULE_DESCRIPTION("RME HDSPM");
86MODULE_LICENSE("GPL");
87MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");
88
Adrian Knoth0dca1792011-01-26 19:32:14 +010089/* --- Write registers. ---
Takashi Iwai763f3562005-06-03 11:25:34 +020090 These are defined as byte-offsets from the iobase value. */
91
Adrian Knoth0dca1792011-01-26 19:32:14 +010092#define HDSPM_WR_SETTINGS 0
93#define HDSPM_outputBufferAddress 32
94#define HDSPM_inputBufferAddress 36
Takashi Iwai763f3562005-06-03 11:25:34 +020095#define HDSPM_controlRegister 64
96#define HDSPM_interruptConfirmation 96
97#define HDSPM_control2Reg 256 /* not in specs ???????? */
Remy Brunoffb2c3c2007-03-07 19:08:46 +010098#define HDSPM_freqReg 256 /* for AES32 */
Adrian Knoth0dca1792011-01-26 19:32:14 +010099#define HDSPM_midiDataOut0 352 /* just believe in old code */
100#define HDSPM_midiDataOut1 356
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100101#define HDSPM_eeprom_wr 384 /* for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200102
103/* DMA enable for 64 channels, only Bit 0 is relevant */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100104#define HDSPM_outputEnableBase 512 /* 512-767 input DMA */
Takashi Iwai763f3562005-06-03 11:25:34 +0200105#define HDSPM_inputEnableBase 768 /* 768-1023 output DMA */
106
Adrian Knoth0dca1792011-01-26 19:32:14 +0100107/* 16 page addresses for each of the 64 channels DMA buffer in and out
Takashi Iwai763f3562005-06-03 11:25:34 +0200108 (each 64k=16*4k) Buffer must be 4k aligned (which is default i386 ????) */
109#define HDSPM_pageAddressBufferOut 8192
110#define HDSPM_pageAddressBufferIn (HDSPM_pageAddressBufferOut+64*16*4)
111
112#define HDSPM_MADI_mixerBase 32768 /* 32768-65535 for 2x64x64 Fader */
113
114#define HDSPM_MATRIX_MIXER_SIZE 8192 /* = 2*64*64 * 4 Byte => 32kB */
115
116/* --- Read registers. ---
117 These are defined as byte-offsets from the iobase value */
118#define HDSPM_statusRegister 0
Remy Bruno3cee5a62006-10-16 12:46:32 +0200119/*#define HDSPM_statusRegister2 96 */
120/* after RME Windows driver sources, status2 is 4-byte word # 48 = word at
121 * offset 192, for AES32 *and* MADI
122 * => need to check that offset 192 is working on MADI */
123#define HDSPM_statusRegister2 192
124#define HDSPM_timecodeRegister 128
Takashi Iwai763f3562005-06-03 11:25:34 +0200125
Adrian Knoth0dca1792011-01-26 19:32:14 +0100126/* AIO, RayDAT */
127#define HDSPM_RD_STATUS_0 0
128#define HDSPM_RD_STATUS_1 64
129#define HDSPM_RD_STATUS_2 128
130#define HDSPM_RD_STATUS_3 192
131
132#define HDSPM_RD_TCO 256
133#define HDSPM_RD_PLL_FREQ 512
134#define HDSPM_WR_TCO 128
135
136#define HDSPM_TCO1_TCO_lock 0x00000001
137#define HDSPM_TCO1_WCK_Input_Range_LSB 0x00000002
138#define HDSPM_TCO1_WCK_Input_Range_MSB 0x00000004
139#define HDSPM_TCO1_LTC_Input_valid 0x00000008
140#define HDSPM_TCO1_WCK_Input_valid 0x00000010
141#define HDSPM_TCO1_Video_Input_Format_NTSC 0x00000020
142#define HDSPM_TCO1_Video_Input_Format_PAL 0x00000040
143
144#define HDSPM_TCO1_set_TC 0x00000100
145#define HDSPM_TCO1_set_drop_frame_flag 0x00000200
146#define HDSPM_TCO1_LTC_Format_LSB 0x00000400
147#define HDSPM_TCO1_LTC_Format_MSB 0x00000800
148
149#define HDSPM_TCO2_TC_run 0x00010000
150#define HDSPM_TCO2_WCK_IO_ratio_LSB 0x00020000
151#define HDSPM_TCO2_WCK_IO_ratio_MSB 0x00040000
152#define HDSPM_TCO2_set_num_drop_frames_LSB 0x00080000
153#define HDSPM_TCO2_set_num_drop_frames_MSB 0x00100000
154#define HDSPM_TCO2_set_jam_sync 0x00200000
155#define HDSPM_TCO2_set_flywheel 0x00400000
156
157#define HDSPM_TCO2_set_01_4 0x01000000
158#define HDSPM_TCO2_set_pull_down 0x02000000
159#define HDSPM_TCO2_set_pull_up 0x04000000
160#define HDSPM_TCO2_set_freq 0x08000000
161#define HDSPM_TCO2_set_term_75R 0x10000000
162#define HDSPM_TCO2_set_input_LSB 0x20000000
163#define HDSPM_TCO2_set_input_MSB 0x40000000
164#define HDSPM_TCO2_set_freq_from_app 0x80000000
165
166
167#define HDSPM_midiDataOut0 352
168#define HDSPM_midiDataOut1 356
169#define HDSPM_midiDataOut2 368
170
Takashi Iwai763f3562005-06-03 11:25:34 +0200171#define HDSPM_midiDataIn0 360
172#define HDSPM_midiDataIn1 364
Adrian Knoth0dca1792011-01-26 19:32:14 +0100173#define HDSPM_midiDataIn2 372
174#define HDSPM_midiDataIn3 376
Takashi Iwai763f3562005-06-03 11:25:34 +0200175
176/* status is data bytes in MIDI-FIFO (0-128) */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100177#define HDSPM_midiStatusOut0 384
178#define HDSPM_midiStatusOut1 388
179#define HDSPM_midiStatusOut2 400
180
181#define HDSPM_midiStatusIn0 392
182#define HDSPM_midiStatusIn1 396
183#define HDSPM_midiStatusIn2 404
184#define HDSPM_midiStatusIn3 408
Takashi Iwai763f3562005-06-03 11:25:34 +0200185
186
187/* the meters are regular i/o-mapped registers, but offset
188 considerably from the rest. the peak registers are reset
Adrian Knoth0dca1792011-01-26 19:32:14 +0100189 when read; the least-significant 4 bits are full-scale counters;
Takashi Iwai763f3562005-06-03 11:25:34 +0200190 the actual peak value is in the most-significant 24 bits.
191*/
Adrian Knoth0dca1792011-01-26 19:32:14 +0100192
193#define HDSPM_MADI_INPUT_PEAK 4096
194#define HDSPM_MADI_PLAYBACK_PEAK 4352
195#define HDSPM_MADI_OUTPUT_PEAK 4608
196
197#define HDSPM_MADI_INPUT_RMS_L 6144
198#define HDSPM_MADI_PLAYBACK_RMS_L 6400
199#define HDSPM_MADI_OUTPUT_RMS_L 6656
200
201#define HDSPM_MADI_INPUT_RMS_H 7168
202#define HDSPM_MADI_PLAYBACK_RMS_H 7424
203#define HDSPM_MADI_OUTPUT_RMS_H 7680
Takashi Iwai763f3562005-06-03 11:25:34 +0200204
205/* --- Control Register bits --------- */
206#define HDSPM_Start (1<<0) /* start engine */
207
208#define HDSPM_Latency0 (1<<1) /* buffer size = 2^n */
209#define HDSPM_Latency1 (1<<2) /* where n is defined */
210#define HDSPM_Latency2 (1<<3) /* by Latency{2,1,0} */
211
Adrian Knoth0dca1792011-01-26 19:32:14 +0100212#define HDSPM_ClockModeMaster (1<<4) /* 1=Master, 0=Autosync */
213#define HDSPM_c0Master 0x1 /* Master clock bit in settings
214 register [RayDAT, AIO] */
Takashi Iwai763f3562005-06-03 11:25:34 +0200215
216#define HDSPM_AudioInterruptEnable (1<<5) /* what do you think ? */
217
218#define HDSPM_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz 1=48kHz/96kHz */
219#define HDSPM_Frequency1 (1<<7) /* 0=32kHz/64kHz */
220#define HDSPM_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200221#define HDSPM_QuadSpeed (1<<31) /* quad speed bit */
Takashi Iwai763f3562005-06-03 11:25:34 +0200222
Remy Bruno3cee5a62006-10-16 12:46:32 +0200223#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200224#define HDSPM_TX_64ch (1<<10) /* Output 64channel MODE=1,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200225 56channelMODE=0 */ /* MADI ONLY*/
226#define HDSPM_Emphasis (1<<10) /* Emphasis */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200227
Adrian Knoth0dca1792011-01-26 19:32:14 +0100228#define HDSPM_AutoInp (1<<11) /* Auto Input (takeover) == Safe Mode,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200229 0=off, 1=on */ /* MADI ONLY */
230#define HDSPM_Dolby (1<<11) /* Dolby = "NonAudio" ?? */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200231
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200232#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
233 * -- MADI ONLY
234 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200235#define HDSPM_InputSelect1 (1<<15) /* should be 0 */
236
Remy Bruno3cee5a62006-10-16 12:46:32 +0200237#define HDSPM_SyncRef2 (1<<13)
238#define HDSPM_SyncRef3 (1<<25)
Takashi Iwai763f3562005-06-03 11:25:34 +0200239
Remy Bruno3cee5a62006-10-16 12:46:32 +0200240#define HDSPM_SMUX (1<<18) /* Frame ??? */ /* MADI ONY */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100241#define HDSPM_clr_tms (1<<19) /* clear track marker, do not use
Takashi Iwai763f3562005-06-03 11:25:34 +0200242 AES additional bits in
243 lower 5 Audiodatabits ??? */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200244#define HDSPM_taxi_reset (1<<20) /* ??? */ /* MADI ONLY ? */
245#define HDSPM_WCK48 (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200246
Adrian Knoth0dca1792011-01-26 19:32:14 +0100247#define HDSPM_Midi0InterruptEnable 0x0400000
248#define HDSPM_Midi1InterruptEnable 0x0800000
249#define HDSPM_Midi2InterruptEnable 0x0200000
250#define HDSPM_Midi3InterruptEnable 0x4000000
Takashi Iwai763f3562005-06-03 11:25:34 +0200251
252#define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100253#define HDSPe_FLOAT_FORMAT 0x2000000
Takashi Iwai763f3562005-06-03 11:25:34 +0200254
Remy Bruno3cee5a62006-10-16 12:46:32 +0200255#define HDSPM_DS_DoubleWire (1<<26) /* AES32 ONLY */
256#define HDSPM_QS_DoubleWire (1<<27) /* AES32 ONLY */
257#define HDSPM_QS_QuadWire (1<<28) /* AES32 ONLY */
258
259#define HDSPM_wclk_sel (1<<30)
Takashi Iwai763f3562005-06-03 11:25:34 +0200260
Adrian Knoth384f7782013-07-05 11:27:53 +0200261/* additional control register bits for AIO*/
262#define HDSPM_c0_Wck48 0x20 /* also RayDAT */
263#define HDSPM_c0_Input0 0x1000
264#define HDSPM_c0_Input1 0x2000
265#define HDSPM_c0_Spdif_Opt 0x4000
266#define HDSPM_c0_Pro 0x8000
267#define HDSPM_c0_clr_tms 0x10000
268#define HDSPM_c0_AEB1 0x20000
269#define HDSPM_c0_AEB2 0x40000
270#define HDSPM_c0_LineOut 0x80000
271#define HDSPM_c0_AD_GAIN0 0x100000
272#define HDSPM_c0_AD_GAIN1 0x200000
273#define HDSPM_c0_DA_GAIN0 0x400000
274#define HDSPM_c0_DA_GAIN1 0x800000
275#define HDSPM_c0_PH_GAIN0 0x1000000
276#define HDSPM_c0_PH_GAIN1 0x2000000
277#define HDSPM_c0_Sym6db 0x4000000
278
279
Takashi Iwai763f3562005-06-03 11:25:34 +0200280/* --- bit helper defines */
281#define HDSPM_LatencyMask (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200282#define HDSPM_FrequencyMask (HDSPM_Frequency0|HDSPM_Frequency1|\
283 HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
Takashi Iwai763f3562005-06-03 11:25:34 +0200284#define HDSPM_InputMask (HDSPM_InputSelect0|HDSPM_InputSelect1)
285#define HDSPM_InputOptical 0
286#define HDSPM_InputCoaxial (HDSPM_InputSelect0)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200287#define HDSPM_SyncRefMask (HDSPM_SyncRef0|HDSPM_SyncRef1|\
288 HDSPM_SyncRef2|HDSPM_SyncRef3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200289
Adrian Knoth0dca1792011-01-26 19:32:14 +0100290#define HDSPM_c0_SyncRef0 0x2
291#define HDSPM_c0_SyncRef1 0x4
292#define HDSPM_c0_SyncRef2 0x8
293#define HDSPM_c0_SyncRef3 0x10
294#define HDSPM_c0_SyncRefMask (HDSPM_c0_SyncRef0 | HDSPM_c0_SyncRef1 |\
295 HDSPM_c0_SyncRef2 | HDSPM_c0_SyncRef3)
296
297#define HDSPM_SYNC_FROM_WORD 0 /* Preferred sync reference */
298#define HDSPM_SYNC_FROM_MADI 1 /* choices - used by "pref_sync_ref" */
299#define HDSPM_SYNC_FROM_TCO 2
300#define HDSPM_SYNC_FROM_SYNC_IN 3
Takashi Iwai763f3562005-06-03 11:25:34 +0200301
302#define HDSPM_Frequency32KHz HDSPM_Frequency0
303#define HDSPM_Frequency44_1KHz HDSPM_Frequency1
304#define HDSPM_Frequency48KHz (HDSPM_Frequency1|HDSPM_Frequency0)
305#define HDSPM_Frequency64KHz (HDSPM_DoubleSpeed|HDSPM_Frequency0)
306#define HDSPM_Frequency88_2KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200307#define HDSPM_Frequency96KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
308 HDSPM_Frequency0)
Remy Bruno3cee5a62006-10-16 12:46:32 +0200309#define HDSPM_Frequency128KHz (HDSPM_QuadSpeed|HDSPM_Frequency0)
310#define HDSPM_Frequency176_4KHz (HDSPM_QuadSpeed|HDSPM_Frequency1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200311#define HDSPM_Frequency192KHz (HDSPM_QuadSpeed|HDSPM_Frequency1|\
312 HDSPM_Frequency0)
Takashi Iwai763f3562005-06-03 11:25:34 +0200313
Takashi Iwai763f3562005-06-03 11:25:34 +0200314
315/* Synccheck Status */
316#define HDSPM_SYNC_CHECK_NO_LOCK 0
317#define HDSPM_SYNC_CHECK_LOCK 1
318#define HDSPM_SYNC_CHECK_SYNC 2
319
320/* AutoSync References - used by "autosync_ref" control switch */
321#define HDSPM_AUTOSYNC_FROM_WORD 0
322#define HDSPM_AUTOSYNC_FROM_MADI 1
Adrian Knoth0dca1792011-01-26 19:32:14 +0100323#define HDSPM_AUTOSYNC_FROM_TCO 2
324#define HDSPM_AUTOSYNC_FROM_SYNC_IN 3
325#define HDSPM_AUTOSYNC_FROM_NONE 4
Takashi Iwai763f3562005-06-03 11:25:34 +0200326
327/* Possible sources of MADI input */
328#define HDSPM_OPTICAL 0 /* optical */
329#define HDSPM_COAXIAL 1 /* BNC */
330
331#define hdspm_encode_latency(x) (((x)<<1) & HDSPM_LatencyMask)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100332#define hdspm_decode_latency(x) ((((x) & HDSPM_LatencyMask)>>1))
Takashi Iwai763f3562005-06-03 11:25:34 +0200333
334#define hdspm_encode_in(x) (((x)&0x3)<<14)
335#define hdspm_decode_in(x) (((x)>>14)&0x3)
336
337/* --- control2 register bits --- */
338#define HDSPM_TMS (1<<0)
339#define HDSPM_TCK (1<<1)
340#define HDSPM_TDI (1<<2)
341#define HDSPM_JTAG (1<<3)
342#define HDSPM_PWDN (1<<4)
343#define HDSPM_PROGRAM (1<<5)
344#define HDSPM_CONFIG_MODE_0 (1<<6)
345#define HDSPM_CONFIG_MODE_1 (1<<7)
346/*#define HDSPM_VERSION_BIT (1<<8) not defined any more*/
347#define HDSPM_BIGENDIAN_MODE (1<<9)
348#define HDSPM_RD_MULTIPLE (1<<10)
349
Remy Bruno3cee5a62006-10-16 12:46:32 +0200350/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200351 that do not conflict with specific bits for AES32 seem to be valid also
352 for the AES32
353 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200354#define HDSPM_audioIRQPending (1<<0) /* IRQ is high and pending */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200355#define HDSPM_RX_64ch (1<<1) /* Input 64chan. MODE=1, 56chn MODE=0 */
356#define HDSPM_AB_int (1<<2) /* InputChannel Opt=0, Coax=1
357 * (like inp0)
358 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100359
Takashi Iwai763f3562005-06-03 11:25:34 +0200360#define HDSPM_madiLock (1<<3) /* MADI Locked =1, no=0 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100361#define HDSPM_madiSync (1<<18) /* MADI is in sync */
362
363#define HDSPM_tcoLock 0x00000020 /* Optional TCO locked status FOR HDSPe MADI! */
364#define HDSPM_tcoSync 0x10000000 /* Optional TCO sync status */
365
366#define HDSPM_syncInLock 0x00010000 /* Sync In lock status FOR HDSPe MADI! */
367#define HDSPM_syncInSync 0x00020000 /* Sync In sync status FOR HDSPe MADI! */
Takashi Iwai763f3562005-06-03 11:25:34 +0200368
369#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100370 /* since 64byte accurate, last 6 bits are not used */
Takashi Iwai763f3562005-06-03 11:25:34 +0200371
Adrian Knoth0dca1792011-01-26 19:32:14 +0100372
373
Takashi Iwai763f3562005-06-03 11:25:34 +0200374#define HDSPM_DoubleSpeedStatus (1<<19) /* (input) card in double speed */
375
376#define HDSPM_madiFreq0 (1<<22) /* system freq 0=error */
377#define HDSPM_madiFreq1 (1<<23) /* 1=32, 2=44.1 3=48 */
378#define HDSPM_madiFreq2 (1<<24) /* 4=64, 5=88.2 6=96 */
379#define HDSPM_madiFreq3 (1<<25) /* 7=128, 8=176.4 9=192 */
380
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200381#define HDSPM_BufferID (1<<26) /* (Double)Buffer ID toggles with
382 * Interrupt
383 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100384#define HDSPM_tco_detect 0x08000000
385#define HDSPM_tco_lock 0x20000000
386
387#define HDSPM_s2_tco_detect 0x00000040
388#define HDSPM_s2_AEBO_D 0x00000080
389#define HDSPM_s2_AEBI_D 0x00000100
390
391
392#define HDSPM_midi0IRQPending 0x40000000
393#define HDSPM_midi1IRQPending 0x80000000
394#define HDSPM_midi2IRQPending 0x20000000
395#define HDSPM_midi2IRQPendingAES 0x00000020
396#define HDSPM_midi3IRQPending 0x00200000
Takashi Iwai763f3562005-06-03 11:25:34 +0200397
398/* --- status bit helpers */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200399#define HDSPM_madiFreqMask (HDSPM_madiFreq0|HDSPM_madiFreq1|\
400 HDSPM_madiFreq2|HDSPM_madiFreq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200401#define HDSPM_madiFreq32 (HDSPM_madiFreq0)
402#define HDSPM_madiFreq44_1 (HDSPM_madiFreq1)
403#define HDSPM_madiFreq48 (HDSPM_madiFreq0|HDSPM_madiFreq1)
404#define HDSPM_madiFreq64 (HDSPM_madiFreq2)
405#define HDSPM_madiFreq88_2 (HDSPM_madiFreq0|HDSPM_madiFreq2)
406#define HDSPM_madiFreq96 (HDSPM_madiFreq1|HDSPM_madiFreq2)
407#define HDSPM_madiFreq128 (HDSPM_madiFreq0|HDSPM_madiFreq1|HDSPM_madiFreq2)
408#define HDSPM_madiFreq176_4 (HDSPM_madiFreq3)
409#define HDSPM_madiFreq192 (HDSPM_madiFreq3|HDSPM_madiFreq0)
410
Remy Bruno3cee5a62006-10-16 12:46:32 +0200411/* Status2 Register bits */ /* MADI ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200412
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300413#define HDSPM_version0 (1<<0) /* not really defined but I guess */
Takashi Iwai763f3562005-06-03 11:25:34 +0200414#define HDSPM_version1 (1<<1) /* in former cards it was ??? */
415#define HDSPM_version2 (1<<2)
416
417#define HDSPM_wcLock (1<<3) /* Wordclock is detected and locked */
418#define HDSPM_wcSync (1<<4) /* Wordclock is in sync with systemclock */
419
420#define HDSPM_wc_freq0 (1<<5) /* input freq detected via autosync */
421#define HDSPM_wc_freq1 (1<<6) /* 001=32, 010==44.1, 011=48, */
Adrian Knotha8cd7142013-05-31 12:57:09 +0200422#define HDSPM_wc_freq2 (1<<7) /* 100=64, 101=88.2, 110=96, 111=128 */
423#define HDSPM_wc_freq3 0x800 /* 1000=176.4, 1001=192 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200424
Adrian Knoth0dca1792011-01-26 19:32:14 +0100425#define HDSPM_SyncRef0 0x10000 /* Sync Reference */
426#define HDSPM_SyncRef1 0x20000
427
428#define HDSPM_SelSyncRef0 (1<<8) /* AutoSync Source */
Takashi Iwai763f3562005-06-03 11:25:34 +0200429#define HDSPM_SelSyncRef1 (1<<9) /* 000=word, 001=MADI, */
430#define HDSPM_SelSyncRef2 (1<<10) /* 111=no valid signal */
431
432#define HDSPM_wc_valid (HDSPM_wcLock|HDSPM_wcSync)
433
Adrian Knotha8cd7142013-05-31 12:57:09 +0200434#define HDSPM_wcFreqMask (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2|\
435 HDSPM_wc_freq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200436#define HDSPM_wcFreq32 (HDSPM_wc_freq0)
437#define HDSPM_wcFreq44_1 (HDSPM_wc_freq1)
438#define HDSPM_wcFreq48 (HDSPM_wc_freq0|HDSPM_wc_freq1)
439#define HDSPM_wcFreq64 (HDSPM_wc_freq2)
440#define HDSPM_wcFreq88_2 (HDSPM_wc_freq0|HDSPM_wc_freq2)
441#define HDSPM_wcFreq96 (HDSPM_wc_freq1|HDSPM_wc_freq2)
Adrian Knotha8cd7142013-05-31 12:57:09 +0200442#define HDSPM_wcFreq128 (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2)
443#define HDSPM_wcFreq176_4 (HDSPM_wc_freq3)
444#define HDSPM_wcFreq192 (HDSPM_wc_freq0|HDSPM_wc_freq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200445
Adrian Knoth0dca1792011-01-26 19:32:14 +0100446#define HDSPM_status1_F_0 0x0400000
447#define HDSPM_status1_F_1 0x0800000
448#define HDSPM_status1_F_2 0x1000000
449#define HDSPM_status1_F_3 0x2000000
450#define HDSPM_status1_freqMask (HDSPM_status1_F_0|HDSPM_status1_F_1|HDSPM_status1_F_2|HDSPM_status1_F_3)
451
Takashi Iwai763f3562005-06-03 11:25:34 +0200452
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200453#define HDSPM_SelSyncRefMask (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
454 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200455#define HDSPM_SelSyncRef_WORD 0
456#define HDSPM_SelSyncRef_MADI (HDSPM_SelSyncRef0)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100457#define HDSPM_SelSyncRef_TCO (HDSPM_SelSyncRef1)
458#define HDSPM_SelSyncRef_SyncIn (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200459#define HDSPM_SelSyncRef_NVALID (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
460 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200461
Remy Bruno3cee5a62006-10-16 12:46:32 +0200462/*
463 For AES32, bits for status, status2 and timecode are different
464*/
465/* status */
466#define HDSPM_AES32_wcLock 0x0200000
Andre Schramm56bde0f2013-01-09 14:40:18 +0100467#define HDSPM_AES32_wcSync 0x0100000
Remy Bruno3cee5a62006-10-16 12:46:32 +0200468#define HDSPM_AES32_wcFreq_bit 22
Adrian Knoth0dca1792011-01-26 19:32:14 +0100469/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
Remy Bruno3cee5a62006-10-16 12:46:32 +0200470 HDSPM_bit2freq */
471#define HDSPM_AES32_syncref_bit 16
472/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */
473
474#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
475#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
476#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
477#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
478#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
479#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
480#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
481#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
482#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
Remy Bruno65345992007-08-31 12:21:08 +0200483#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
Remy Bruno3cee5a62006-10-16 12:46:32 +0200484
485/* status2 */
486/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
487#define HDSPM_LockAES 0x80
488#define HDSPM_LockAES1 0x80
489#define HDSPM_LockAES2 0x40
490#define HDSPM_LockAES3 0x20
491#define HDSPM_LockAES4 0x10
492#define HDSPM_LockAES5 0x8
493#define HDSPM_LockAES6 0x4
494#define HDSPM_LockAES7 0x2
495#define HDSPM_LockAES8 0x1
496/*
497 Timecode
498 After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
499 AES i+1
500 bits 3210
501 0001 32kHz
502 0010 44.1kHz
503 0011 48kHz
504 0100 64kHz
505 0101 88.2kHz
506 0110 96kHz
507 0111 128kHz
508 1000 176.4kHz
509 1001 192kHz
510 NB: Timecode register doesn't seem to work on AES32 card revision 230
511*/
512
Takashi Iwai763f3562005-06-03 11:25:34 +0200513/* Mixer Values */
514#define UNITY_GAIN 32768 /* = 65536/2 */
515#define MINUS_INFINITY_GAIN 0
516
Takashi Iwai763f3562005-06-03 11:25:34 +0200517/* Number of channels for different Speed Modes */
518#define MADI_SS_CHANNELS 64
519#define MADI_DS_CHANNELS 32
520#define MADI_QS_CHANNELS 16
521
Adrian Knoth0dca1792011-01-26 19:32:14 +0100522#define RAYDAT_SS_CHANNELS 36
523#define RAYDAT_DS_CHANNELS 20
524#define RAYDAT_QS_CHANNELS 12
525
526#define AIO_IN_SS_CHANNELS 14
527#define AIO_IN_DS_CHANNELS 10
528#define AIO_IN_QS_CHANNELS 8
529#define AIO_OUT_SS_CHANNELS 16
530#define AIO_OUT_DS_CHANNELS 12
531#define AIO_OUT_QS_CHANNELS 10
532
Adrian Knothd2d10a22011-02-28 15:14:47 +0100533#define AES32_CHANNELS 16
534
Takashi Iwai763f3562005-06-03 11:25:34 +0200535/* the size of a substream (1 mono data stream) */
536#define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024)
537#define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_CHANNEL_BUFFER_SAMPLES)
538
539/* the size of the area we need to allocate for DMA transfers. the
540 size is the same regardless of the number of channels, and
Adrian Knoth0dca1792011-01-26 19:32:14 +0100541 also the latency to use.
Takashi Iwai763f3562005-06-03 11:25:34 +0200542 for one direction !!!
543*/
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100544#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
Takashi Iwai763f3562005-06-03 11:25:34 +0200545#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
546
Adrian Knoth0dca1792011-01-26 19:32:14 +0100547#define HDSPM_RAYDAT_REV 211
548#define HDSPM_AIO_REV 212
549#define HDSPM_MADIFACE_REV 213
Remy Bruno3cee5a62006-10-16 12:46:32 +0200550
Remy Bruno65345992007-08-31 12:21:08 +0200551/* speed factor modes */
552#define HDSPM_SPEED_SINGLE 0
553#define HDSPM_SPEED_DOUBLE 1
554#define HDSPM_SPEED_QUAD 2
Adrian Knoth0dca1792011-01-26 19:32:14 +0100555
Remy Bruno65345992007-08-31 12:21:08 +0200556/* names for speed modes */
557static char *hdspm_speed_names[] = { "single", "double", "quad" };
558
Adrian Knoth0dca1792011-01-26 19:32:14 +0100559static char *texts_autosync_aes_tco[] = { "Word Clock",
560 "AES1", "AES2", "AES3", "AES4",
561 "AES5", "AES6", "AES7", "AES8",
562 "TCO" };
563static char *texts_autosync_aes[] = { "Word Clock",
564 "AES1", "AES2", "AES3", "AES4",
565 "AES5", "AES6", "AES7", "AES8" };
566static char *texts_autosync_madi_tco[] = { "Word Clock",
567 "MADI", "TCO", "Sync In" };
568static char *texts_autosync_madi[] = { "Word Clock",
569 "MADI", "Sync In" };
570
571static char *texts_autosync_raydat_tco[] = {
572 "Word Clock",
573 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
574 "AES", "SPDIF", "TCO", "Sync In"
575};
576static char *texts_autosync_raydat[] = {
577 "Word Clock",
578 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
579 "AES", "SPDIF", "Sync In"
580};
581static char *texts_autosync_aio_tco[] = {
582 "Word Clock",
583 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
584};
585static char *texts_autosync_aio[] = { "Word Clock",
586 "ADAT", "AES", "SPDIF", "Sync In" };
587
588static char *texts_freq[] = {
589 "No Lock",
590 "32 kHz",
591 "44.1 kHz",
592 "48 kHz",
593 "64 kHz",
594 "88.2 kHz",
595 "96 kHz",
596 "128 kHz",
597 "176.4 kHz",
598 "192 kHz"
599};
600
Adrian Knoth0dca1792011-01-26 19:32:14 +0100601static char *texts_ports_madi[] = {
602 "MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
603 "MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
604 "MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
605 "MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
606 "MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
607 "MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
608 "MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
609 "MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
610 "MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
611 "MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
612 "MADI.61", "MADI.62", "MADI.63", "MADI.64",
613};
614
615
616static char *texts_ports_raydat_ss[] = {
617 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
618 "ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
619 "ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
620 "ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
621 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
622 "ADAT4.7", "ADAT4.8",
623 "AES.L", "AES.R",
624 "SPDIF.L", "SPDIF.R"
625};
626
627static char *texts_ports_raydat_ds[] = {
628 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
629 "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
630 "ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
631 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
632 "AES.L", "AES.R",
633 "SPDIF.L", "SPDIF.R"
634};
635
636static char *texts_ports_raydat_qs[] = {
637 "ADAT1.1", "ADAT1.2",
638 "ADAT2.1", "ADAT2.2",
639 "ADAT3.1", "ADAT3.2",
640 "ADAT4.1", "ADAT4.2",
641 "AES.L", "AES.R",
642 "SPDIF.L", "SPDIF.R"
643};
644
645
646static char *texts_ports_aio_in_ss[] = {
647 "Analogue.L", "Analogue.R",
648 "AES.L", "AES.R",
649 "SPDIF.L", "SPDIF.R",
650 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
651 "ADAT.7", "ADAT.8"
652};
653
654static char *texts_ports_aio_out_ss[] = {
655 "Analogue.L", "Analogue.R",
656 "AES.L", "AES.R",
657 "SPDIF.L", "SPDIF.R",
658 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
659 "ADAT.7", "ADAT.8",
660 "Phone.L", "Phone.R"
661};
662
663static char *texts_ports_aio_in_ds[] = {
664 "Analogue.L", "Analogue.R",
665 "AES.L", "AES.R",
666 "SPDIF.L", "SPDIF.R",
667 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
668};
669
670static char *texts_ports_aio_out_ds[] = {
671 "Analogue.L", "Analogue.R",
672 "AES.L", "AES.R",
673 "SPDIF.L", "SPDIF.R",
674 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
675 "Phone.L", "Phone.R"
676};
677
678static char *texts_ports_aio_in_qs[] = {
679 "Analogue.L", "Analogue.R",
680 "AES.L", "AES.R",
681 "SPDIF.L", "SPDIF.R",
682 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
683};
684
685static char *texts_ports_aio_out_qs[] = {
686 "Analogue.L", "Analogue.R",
687 "AES.L", "AES.R",
688 "SPDIF.L", "SPDIF.R",
689 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
690 "Phone.L", "Phone.R"
691};
692
Adrian Knoth432d2502011-02-23 11:43:08 +0100693static char *texts_ports_aes32[] = {
694 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
695 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
696 "AES.15", "AES.16"
697};
698
Adrian Knoth55a57602011-01-27 11:23:15 +0100699/* These tables map the ALSA channels 1..N to the channels that we
700 need to use in order to find the relevant channel buffer. RME
701 refers to this kind of mapping as between "the ADAT channel and
702 the DMA channel." We index it using the logical audio channel,
703 and the value is the DMA channel (i.e. channel buffer number)
704 where the data for that channel can be read/written from/to.
705*/
706
707static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
708 0, 1, 2, 3, 4, 5, 6, 7,
709 8, 9, 10, 11, 12, 13, 14, 15,
710 16, 17, 18, 19, 20, 21, 22, 23,
711 24, 25, 26, 27, 28, 29, 30, 31,
712 32, 33, 34, 35, 36, 37, 38, 39,
713 40, 41, 42, 43, 44, 45, 46, 47,
714 48, 49, 50, 51, 52, 53, 54, 55,
715 56, 57, 58, 59, 60, 61, 62, 63
716};
717
Adrian Knoth55a57602011-01-27 11:23:15 +0100718static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
719 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
720 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
721 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
722 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
723 0, 1, /* AES */
724 2, 3, /* SPDIF */
725 -1, -1, -1, -1,
726 -1, -1, -1, -1, -1, -1, -1, -1,
727 -1, -1, -1, -1, -1, -1, -1, -1,
728 -1, -1, -1, -1, -1, -1, -1, -1,
729};
730
731static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
732 4, 5, 6, 7, /* ADAT 1 */
733 8, 9, 10, 11, /* ADAT 2 */
734 12, 13, 14, 15, /* ADAT 3 */
735 16, 17, 18, 19, /* ADAT 4 */
736 0, 1, /* AES */
737 2, 3, /* SPDIF */
738 -1, -1, -1, -1,
739 -1, -1, -1, -1, -1, -1, -1, -1,
740 -1, -1, -1, -1, -1, -1, -1, -1,
741 -1, -1, -1, -1, -1, -1, -1, -1,
742 -1, -1, -1, -1, -1, -1, -1, -1,
743 -1, -1, -1, -1, -1, -1, -1, -1,
744};
745
746static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
747 4, 5, /* ADAT 1 */
748 6, 7, /* ADAT 2 */
749 8, 9, /* ADAT 3 */
750 10, 11, /* ADAT 4 */
751 0, 1, /* AES */
752 2, 3, /* SPDIF */
753 -1, -1, -1, -1,
754 -1, -1, -1, -1, -1, -1, -1, -1,
755 -1, -1, -1, -1, -1, -1, -1, -1,
756 -1, -1, -1, -1, -1, -1, -1, -1,
757 -1, -1, -1, -1, -1, -1, -1, -1,
758 -1, -1, -1, -1, -1, -1, -1, -1,
759 -1, -1, -1, -1, -1, -1, -1, -1,
760};
761
762static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
763 0, 1, /* line in */
764 8, 9, /* aes in, */
765 10, 11, /* spdif in */
766 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
767 -1, -1,
768 -1, -1, -1, -1, -1, -1, -1, -1,
769 -1, -1, -1, -1, -1, -1, -1, -1,
770 -1, -1, -1, -1, -1, -1, -1, -1,
771 -1, -1, -1, -1, -1, -1, -1, -1,
772 -1, -1, -1, -1, -1, -1, -1, -1,
773 -1, -1, -1, -1, -1, -1, -1, -1,
774};
775
776static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
777 0, 1, /* line out */
778 8, 9, /* aes out */
779 10, 11, /* spdif out */
780 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
781 6, 7, /* phone out */
782 -1, -1, -1, -1, -1, -1, -1, -1,
783 -1, -1, -1, -1, -1, -1, -1, -1,
784 -1, -1, -1, -1, -1, -1, -1, -1,
785 -1, -1, -1, -1, -1, -1, -1, -1,
786 -1, -1, -1, -1, -1, -1, -1, -1,
787 -1, -1, -1, -1, -1, -1, -1, -1,
788};
789
790static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
791 0, 1, /* line in */
792 8, 9, /* aes in */
793 10, 11, /* spdif in */
794 12, 14, 16, 18, /* adat in */
795 -1, -1, -1, -1, -1, -1,
796 -1, -1, -1, -1, -1, -1, -1, -1,
797 -1, -1, -1, -1, -1, -1, -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1,
799 -1, -1, -1, -1, -1, -1, -1, -1,
800 -1, -1, -1, -1, -1, -1, -1, -1,
801 -1, -1, -1, -1, -1, -1, -1, -1
802};
803
804static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
805 0, 1, /* line out */
806 8, 9, /* aes out */
807 10, 11, /* spdif out */
808 12, 14, 16, 18, /* adat out */
809 6, 7, /* phone out */
810 -1, -1, -1, -1,
811 -1, -1, -1, -1, -1, -1, -1, -1,
812 -1, -1, -1, -1, -1, -1, -1, -1,
813 -1, -1, -1, -1, -1, -1, -1, -1,
814 -1, -1, -1, -1, -1, -1, -1, -1,
815 -1, -1, -1, -1, -1, -1, -1, -1,
816 -1, -1, -1, -1, -1, -1, -1, -1
817};
818
819static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
820 0, 1, /* line in */
821 8, 9, /* aes in */
822 10, 11, /* spdif in */
823 12, 16, /* adat in */
824 -1, -1, -1, -1, -1, -1, -1, -1,
825 -1, -1, -1, -1, -1, -1, -1, -1,
826 -1, -1, -1, -1, -1, -1, -1, -1,
827 -1, -1, -1, -1, -1, -1, -1, -1,
828 -1, -1, -1, -1, -1, -1, -1, -1,
829 -1, -1, -1, -1, -1, -1, -1, -1,
830 -1, -1, -1, -1, -1, -1, -1, -1
831};
832
833static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
834 0, 1, /* line out */
835 8, 9, /* aes out */
836 10, 11, /* spdif out */
837 12, 16, /* adat out */
838 6, 7, /* phone out */
839 -1, -1, -1, -1, -1, -1,
840 -1, -1, -1, -1, -1, -1, -1, -1,
841 -1, -1, -1, -1, -1, -1, -1, -1,
842 -1, -1, -1, -1, -1, -1, -1, -1,
843 -1, -1, -1, -1, -1, -1, -1, -1,
844 -1, -1, -1, -1, -1, -1, -1, -1,
845 -1, -1, -1, -1, -1, -1, -1, -1
846};
847
Adrian Knoth432d2502011-02-23 11:43:08 +0100848static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
849 0, 1, 2, 3, 4, 5, 6, 7,
850 8, 9, 10, 11, 12, 13, 14, 15,
851 -1, -1, -1, -1, -1, -1, -1, -1,
852 -1, -1, -1, -1, -1, -1, -1, -1,
853 -1, -1, -1, -1, -1, -1, -1, -1,
854 -1, -1, -1, -1, -1, -1, -1, -1,
855 -1, -1, -1, -1, -1, -1, -1, -1,
856 -1, -1, -1, -1, -1, -1, -1, -1
857};
858
Takashi Iwai98274f02005-11-17 14:52:34 +0100859struct hdspm_midi {
860 struct hdspm *hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +0200861 int id;
Takashi Iwai98274f02005-11-17 14:52:34 +0100862 struct snd_rawmidi *rmidi;
863 struct snd_rawmidi_substream *input;
864 struct snd_rawmidi_substream *output;
Takashi Iwai763f3562005-06-03 11:25:34 +0200865 char istimer; /* timer in use */
866 struct timer_list timer;
867 spinlock_t lock;
868 int pending;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100869 int dataIn;
870 int statusIn;
871 int dataOut;
872 int statusOut;
873 int ie;
874 int irq;
875};
876
877struct hdspm_tco {
878 int input;
879 int framerate;
880 int wordclock;
881 int samplerate;
882 int pull;
883 int term; /* 0 = off, 1 = on */
Takashi Iwai763f3562005-06-03 11:25:34 +0200884};
885
Takashi Iwai98274f02005-11-17 14:52:34 +0100886struct hdspm {
Takashi Iwai763f3562005-06-03 11:25:34 +0200887 spinlock_t lock;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200888 /* only one playback and/or capture stream */
889 struct snd_pcm_substream *capture_substream;
890 struct snd_pcm_substream *playback_substream;
Takashi Iwai763f3562005-06-03 11:25:34 +0200891
892 char *card_name; /* for procinfo */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200893 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
894
Adrian Knoth0dca1792011-01-26 19:32:14 +0100895 uint8_t io_type;
Takashi Iwai763f3562005-06-03 11:25:34 +0200896
Takashi Iwai763f3562005-06-03 11:25:34 +0200897 int monitor_outs; /* set up monitoring outs init flag */
898
899 u32 control_register; /* cached value */
900 u32 control2_register; /* cached value */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100901 u32 settings_register;
Takashi Iwai763f3562005-06-03 11:25:34 +0200902
Adrian Knoth0dca1792011-01-26 19:32:14 +0100903 struct hdspm_midi midi[4];
Takashi Iwai763f3562005-06-03 11:25:34 +0200904 struct tasklet_struct midi_tasklet;
905
906 size_t period_bytes;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100907 unsigned char ss_in_channels;
908 unsigned char ds_in_channels;
909 unsigned char qs_in_channels;
910 unsigned char ss_out_channels;
911 unsigned char ds_out_channels;
912 unsigned char qs_out_channels;
913
914 unsigned char max_channels_in;
915 unsigned char max_channels_out;
916
Takashi Iwai286bed02011-06-30 12:45:36 +0200917 signed char *channel_map_in;
918 signed char *channel_map_out;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100919
Takashi Iwai286bed02011-06-30 12:45:36 +0200920 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
921 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100922
923 char **port_names_in;
924 char **port_names_out;
925
926 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
927 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
Takashi Iwai763f3562005-06-03 11:25:34 +0200928
929 unsigned char *playback_buffer; /* suitably aligned address */
930 unsigned char *capture_buffer; /* suitably aligned address */
931
932 pid_t capture_pid; /* process id which uses capture */
933 pid_t playback_pid; /* process id which uses capture */
934 int running; /* running status */
935
936 int last_external_sample_rate; /* samplerate mystic ... */
937 int last_internal_sample_rate;
938 int system_sample_rate;
939
Takashi Iwai763f3562005-06-03 11:25:34 +0200940 int dev; /* Hardware vars... */
941 int irq;
942 unsigned long port;
943 void __iomem *iobase;
944
945 int irq_count; /* for debug */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100946 int midiPorts;
Takashi Iwai763f3562005-06-03 11:25:34 +0200947
Takashi Iwai98274f02005-11-17 14:52:34 +0100948 struct snd_card *card; /* one card */
949 struct snd_pcm *pcm; /* has one pcm */
950 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
Takashi Iwai763f3562005-06-03 11:25:34 +0200951 struct pci_dev *pci; /* and an pci info */
952
953 /* Mixer vars */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200954 /* fast alsa mixer */
955 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
956 /* but input to much, so not used */
957 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300958 /* full mixer accessible over mixer ioctl or hwdep-device */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200959 struct hdspm_mixer *mixer;
Takashi Iwai763f3562005-06-03 11:25:34 +0200960
Adrian Knoth0dca1792011-01-26 19:32:14 +0100961 struct hdspm_tco *tco; /* NULL if no TCO detected */
Takashi Iwai763f3562005-06-03 11:25:34 +0200962
Adrian Knoth0dca1792011-01-26 19:32:14 +0100963 char **texts_autosync;
964 int texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +0200965
Adrian Knoth0dca1792011-01-26 19:32:14 +0100966 cycles_t last_interrupt;
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100967
Adrian Knoth7d53a632012-01-04 14:31:16 +0100968 unsigned int serial;
969
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100970 struct hdspm_peak_rms peak_rms;
Takashi Iwai763f3562005-06-03 11:25:34 +0200971};
972
Takashi Iwai763f3562005-06-03 11:25:34 +0200973
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200974static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
Takashi Iwai763f3562005-06-03 11:25:34 +0200975 {
976 .vendor = PCI_VENDOR_ID_XILINX,
977 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
978 .subvendor = PCI_ANY_ID,
979 .subdevice = PCI_ANY_ID,
980 .class = 0,
981 .class_mask = 0,
982 .driver_data = 0},
983 {0,}
984};
985
986MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
987
988/* prototypes */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500989static int snd_hdspm_create_alsa_devices(struct snd_card *card,
990 struct hdspm *hdspm);
991static int snd_hdspm_create_pcm(struct snd_card *card,
992 struct hdspm *hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +0200993
Adrian Knoth0dca1792011-01-26 19:32:14 +0100994static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
Adrian Knoth3f7bf912013-03-10 00:37:21 +0100995static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +0100996static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
997static int hdspm_autosync_ref(struct hdspm *hdspm);
998static int snd_hdspm_set_defaults(struct hdspm *hdspm);
Adrian Knoth21a164d2012-10-19 17:42:23 +0200999static int hdspm_system_clock_mode(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001000static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02001001 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02001002 unsigned int reg, int channels);
1003
Remy Bruno3cee5a62006-10-16 12:46:32 +02001004static inline int HDSPM_bit2freq(int n)
1005{
Denys Vlasenko62cef822008-04-14 13:04:18 +02001006 static const int bit2freq_tab[] = {
1007 0, 32000, 44100, 48000, 64000, 88200,
Remy Bruno3cee5a62006-10-16 12:46:32 +02001008 96000, 128000, 176400, 192000 };
1009 if (n < 1 || n > 9)
1010 return 0;
1011 return bit2freq_tab[n];
1012}
1013
Adrian Knoth0dca1792011-01-26 19:32:14 +01001014/* Write/read to/from HDSPM with Adresses in Bytes
Takashi Iwai763f3562005-06-03 11:25:34 +02001015 not words but only 32Bit writes are allowed */
1016
Takashi Iwai98274f02005-11-17 14:52:34 +01001017static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
Takashi Iwai763f3562005-06-03 11:25:34 +02001018 unsigned int val)
1019{
1020 writel(val, hdspm->iobase + reg);
1021}
1022
Takashi Iwai98274f02005-11-17 14:52:34 +01001023static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
Takashi Iwai763f3562005-06-03 11:25:34 +02001024{
1025 return readl(hdspm->iobase + reg);
1026}
1027
Adrian Knoth0dca1792011-01-26 19:32:14 +01001028/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1029 mixer is write only on hardware so we have to cache him for read
Takashi Iwai763f3562005-06-03 11:25:34 +02001030 each fader is a u32, but uses only the first 16 bit */
1031
Takashi Iwai98274f02005-11-17 14:52:34 +01001032static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001033 unsigned int in)
1034{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001035 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001036 return 0;
1037
1038 return hdspm->mixer->ch[chan].in[in];
1039}
1040
Takashi Iwai98274f02005-11-17 14:52:34 +01001041static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001042 unsigned int pb)
1043{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001044 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001045 return 0;
1046 return hdspm->mixer->ch[chan].pb[pb];
1047}
1048
Denys Vlasenko62cef822008-04-14 13:04:18 +02001049static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001050 unsigned int in, unsigned short data)
1051{
1052 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1053 return -1;
1054
1055 hdspm_write(hdspm,
1056 HDSPM_MADI_mixerBase +
1057 ((in + 128 * chan) * sizeof(u32)),
1058 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1059 return 0;
1060}
1061
Denys Vlasenko62cef822008-04-14 13:04:18 +02001062static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001063 unsigned int pb, unsigned short data)
1064{
1065 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1066 return -1;
1067
1068 hdspm_write(hdspm,
1069 HDSPM_MADI_mixerBase +
1070 ((64 + pb + 128 * chan) * sizeof(u32)),
1071 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1072 return 0;
1073}
1074
1075
1076/* enable DMA for specific channels, now available for DSP-MADI */
Takashi Iwai98274f02005-11-17 14:52:34 +01001077static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001078{
1079 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1080}
1081
Takashi Iwai98274f02005-11-17 14:52:34 +01001082static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001083{
1084 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1085}
1086
1087/* check if same process is writing and reading */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001088static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001089{
1090 unsigned long flags;
1091 int ret = 1;
1092
1093 spin_lock_irqsave(&hdspm->lock, flags);
1094 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1095 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1096 ret = 0;
1097 }
1098 spin_unlock_irqrestore(&hdspm->lock, flags);
1099 return ret;
1100}
1101
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001102/* round arbitary sample rates to commonly known rates */
1103static int hdspm_round_frequency(int rate)
1104{
1105 if (rate < 38050)
1106 return 32000;
1107 if (rate < 46008)
1108 return 44100;
1109 else
1110 return 48000;
1111}
1112
Adrian Knotha8a729f2013-05-31 12:57:10 +02001113/* QS and DS rates normally can not be detected
1114 * automatically by the card. Only exception is MADI
1115 * in 96k frame mode.
1116 *
1117 * So if we read SS values (32 .. 48k), check for
1118 * user-provided DS/QS bits in the control register
1119 * and multiply the base frequency accordingly.
1120 */
1121static int hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
1122{
1123 if (rate <= 48000) {
1124 if (hdspm->control_register & HDSPM_QuadSpeed)
1125 return rate * 4;
1126 else if (hdspm->control_register &
1127 HDSPM_DoubleSpeed)
1128 return rate * 2;
1129 };
1130 return rate;
1131}
1132
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001133static int hdspm_tco_sync_check(struct hdspm *hdspm);
1134static int hdspm_sync_in_sync_check(struct hdspm *hdspm);
1135
Takashi Iwai763f3562005-06-03 11:25:34 +02001136/* check for external sample rate */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001137static int hdspm_external_sample_rate(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001138{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001139 unsigned int status, status2, timecode;
1140 int syncref, rate = 0, rate_bits;
Takashi Iwai763f3562005-06-03 11:25:34 +02001141
Adrian Knoth0dca1792011-01-26 19:32:14 +01001142 switch (hdspm->io_type) {
1143 case AES32:
1144 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1145 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01001146 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001147
1148 syncref = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001149
Remy Bruno3cee5a62006-10-16 12:46:32 +02001150 if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
1151 status & HDSPM_AES32_wcLock)
Adrian Knoth0dca1792011-01-26 19:32:14 +01001152 return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF);
1153
Remy Bruno3cee5a62006-10-16 12:46:32 +02001154 if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001155 syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
1156 status2 & (HDSPM_LockAES >>
1157 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
1158 return HDSPM_bit2freq((timecode >> (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001159 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001160 break;
1161
1162 case MADIface:
1163 status = hdspm_read(hdspm, HDSPM_statusRegister);
1164
1165 if (!(status & HDSPM_madiLock)) {
1166 rate = 0; /* no lock */
1167 } else {
1168 switch (status & (HDSPM_status1_freqMask)) {
1169 case HDSPM_status1_F_0*1:
1170 rate = 32000; break;
1171 case HDSPM_status1_F_0*2:
1172 rate = 44100; break;
1173 case HDSPM_status1_F_0*3:
1174 rate = 48000; break;
1175 case HDSPM_status1_F_0*4:
1176 rate = 64000; break;
1177 case HDSPM_status1_F_0*5:
1178 rate = 88200; break;
1179 case HDSPM_status1_F_0*6:
1180 rate = 96000; break;
1181 case HDSPM_status1_F_0*7:
1182 rate = 128000; break;
1183 case HDSPM_status1_F_0*8:
1184 rate = 176400; break;
1185 case HDSPM_status1_F_0*9:
1186 rate = 192000; break;
1187 default:
1188 rate = 0; break;
1189 }
1190 }
1191
1192 break;
1193
1194 case MADI:
1195 case AIO:
1196 case RayDAT:
1197 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1198 status = hdspm_read(hdspm, HDSPM_statusRegister);
1199 rate = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02001200
Remy Bruno3cee5a62006-10-16 12:46:32 +02001201 /* if wordclock has synced freq and wordclock is valid */
1202 if ((status2 & HDSPM_wcLock) != 0 &&
Adrian Knothfedf1532011-06-12 17:26:18 +02001203 (status2 & HDSPM_SelSyncRef0) == 0) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02001204
1205 rate_bits = status2 & HDSPM_wcFreqMask;
1206
Adrian Knoth0dca1792011-01-26 19:32:14 +01001207
Remy Bruno3cee5a62006-10-16 12:46:32 +02001208 switch (rate_bits) {
1209 case HDSPM_wcFreq32:
1210 rate = 32000;
1211 break;
1212 case HDSPM_wcFreq44_1:
1213 rate = 44100;
1214 break;
1215 case HDSPM_wcFreq48:
1216 rate = 48000;
1217 break;
1218 case HDSPM_wcFreq64:
1219 rate = 64000;
1220 break;
1221 case HDSPM_wcFreq88_2:
1222 rate = 88200;
1223 break;
1224 case HDSPM_wcFreq96:
1225 rate = 96000;
1226 break;
Adrian Knotha8cd7142013-05-31 12:57:09 +02001227 case HDSPM_wcFreq128:
1228 rate = 128000;
1229 break;
1230 case HDSPM_wcFreq176_4:
1231 rate = 176400;
1232 break;
1233 case HDSPM_wcFreq192:
1234 rate = 192000;
1235 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001236 default:
1237 rate = 0;
1238 break;
1239 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001240 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001241
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001242 /* if rate detected and Syncref is Word than have it,
1243 * word has priority to MADI
1244 */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001245 if (rate != 0 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001246 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
Adrian Knoth7b559392013-05-31 12:57:11 +02001247 return hdspm_rate_multiplier(hdspm, rate);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001248
Adrian Knoth0dca1792011-01-26 19:32:14 +01001249 /* maybe a madi input (which is taken if sel sync is madi) */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001250 if (status & HDSPM_madiLock) {
1251 rate_bits = status & HDSPM_madiFreqMask;
1252
1253 switch (rate_bits) {
1254 case HDSPM_madiFreq32:
1255 rate = 32000;
1256 break;
1257 case HDSPM_madiFreq44_1:
1258 rate = 44100;
1259 break;
1260 case HDSPM_madiFreq48:
1261 rate = 48000;
1262 break;
1263 case HDSPM_madiFreq64:
1264 rate = 64000;
1265 break;
1266 case HDSPM_madiFreq88_2:
1267 rate = 88200;
1268 break;
1269 case HDSPM_madiFreq96:
1270 rate = 96000;
1271 break;
1272 case HDSPM_madiFreq128:
1273 rate = 128000;
1274 break;
1275 case HDSPM_madiFreq176_4:
1276 rate = 176400;
1277 break;
1278 case HDSPM_madiFreq192:
1279 rate = 192000;
1280 break;
1281 default:
1282 rate = 0;
1283 break;
1284 }
Adrian Knothd12c51d2011-07-29 03:11:03 +02001285
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001286 } /* endif HDSPM_madiLock */
1287
1288 /* check sample rate from TCO or SYNC_IN */
1289 {
1290 bool is_valid_input = 0;
1291 bool has_sync = 0;
1292
1293 syncref = hdspm_autosync_ref(hdspm);
1294 if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
1295 is_valid_input = 1;
1296 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1297 hdspm_tco_sync_check(hdspm));
1298 } else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
1299 is_valid_input = 1;
1300 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1301 hdspm_sync_in_sync_check(hdspm));
Adrian Knothd12c51d2011-07-29 03:11:03 +02001302 }
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001303
1304 if (is_valid_input && has_sync) {
1305 rate = hdspm_round_frequency(
1306 hdspm_get_pll_freq(hdspm));
1307 }
1308 }
1309
Adrian Knotha8a729f2013-05-31 12:57:10 +02001310 rate = hdspm_rate_multiplier(hdspm, rate);
1311
Adrian Knoth0dca1792011-01-26 19:32:14 +01001312 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001313 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001314
1315 return rate;
Takashi Iwai763f3562005-06-03 11:25:34 +02001316}
1317
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001318/* return latency in samples per period */
1319static int hdspm_get_latency(struct hdspm *hdspm)
1320{
1321 int n;
1322
1323 n = hdspm_decode_latency(hdspm->control_register);
1324
1325 /* Special case for new RME cards with 32 samples period size.
1326 * The three latency bits in the control register
1327 * (HDSP_LatencyMask) encode latency values of 64 samples as
1328 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
1329 * denotes 8192 samples, but on new cards like RayDAT or AIO,
1330 * it corresponds to 32 samples.
1331 */
1332 if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
1333 n = -1;
1334
1335 return 1 << (n + 6);
1336}
1337
Takashi Iwai763f3562005-06-03 11:25:34 +02001338/* Latency function */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001339static inline void hdspm_compute_period_size(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001340{
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001341 hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001342}
1343
Adrian Knoth0dca1792011-01-26 19:32:14 +01001344
1345static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001346{
1347 int position;
1348
1349 position = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth483cee72011-02-23 11:43:09 +01001350
1351 switch (hdspm->io_type) {
1352 case RayDAT:
1353 case AIO:
1354 position &= HDSPM_BufferPositionMask;
1355 position /= 4; /* Bytes per sample */
1356 break;
1357 default:
1358 position = (position & HDSPM_BufferID) ?
1359 (hdspm->period_bytes / 4) : 0;
1360 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001361
1362 return position;
1363}
1364
1365
Takashi Iwai98274f02005-11-17 14:52:34 +01001366static inline void hdspm_start_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001367{
1368 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1369 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1370}
1371
Takashi Iwai98274f02005-11-17 14:52:34 +01001372static inline void hdspm_stop_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001373{
1374 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1375 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1376}
1377
1378/* should I silence all or only opened ones ? doit all for first even is 4MB*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001379static void hdspm_silence_playback(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001380{
1381 int i;
1382 int n = hdspm->period_bytes;
1383 void *buf = hdspm->playback_buffer;
1384
Remy Bruno3cee5a62006-10-16 12:46:32 +02001385 if (buf == NULL)
1386 return;
Takashi Iwai763f3562005-06-03 11:25:34 +02001387
1388 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1389 memset(buf, 0, n);
1390 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1391 }
1392}
1393
Adrian Knoth0dca1792011-01-26 19:32:14 +01001394static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
Takashi Iwai763f3562005-06-03 11:25:34 +02001395{
1396 int n;
1397
1398 spin_lock_irq(&s->lock);
1399
Adrian Knoth2e610272011-08-15 00:22:54 +02001400 if (32 == frames) {
1401 /* Special case for new RME cards like RayDAT/AIO which
1402 * support period sizes of 32 samples. Since latency is
1403 * encoded in the three bits of HDSP_LatencyMask, we can only
1404 * have values from 0 .. 7. While 0 still means 64 samples and
1405 * 6 represents 4096 samples on all cards, 7 represents 8192
1406 * on older cards and 32 samples on new cards.
1407 *
1408 * In other words, period size in samples is calculated by
1409 * 2^(n+6) with n ranging from 0 .. 7.
1410 */
1411 n = 7;
1412 } else {
1413 frames >>= 7;
1414 n = 0;
1415 while (frames) {
1416 n++;
1417 frames >>= 1;
1418 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001419 }
Adrian Knoth2e610272011-08-15 00:22:54 +02001420
Takashi Iwai763f3562005-06-03 11:25:34 +02001421 s->control_register &= ~HDSPM_LatencyMask;
1422 s->control_register |= hdspm_encode_latency(n);
1423
1424 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1425
1426 hdspm_compute_period_size(s);
1427
1428 spin_unlock_irq(&s->lock);
1429
1430 return 0;
1431}
1432
Adrian Knoth0dca1792011-01-26 19:32:14 +01001433static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1434{
1435 u64 freq_const;
1436
1437 if (period == 0)
1438 return 0;
1439
1440 switch (hdspm->io_type) {
1441 case MADI:
1442 case AES32:
1443 freq_const = 110069313433624ULL;
1444 break;
1445 case RayDAT:
1446 case AIO:
1447 freq_const = 104857600000000ULL;
1448 break;
1449 case MADIface:
1450 freq_const = 131072000000000ULL;
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001451 break;
1452 default:
1453 snd_BUG();
1454 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001455 }
1456
1457 return div_u64(freq_const, period);
1458}
1459
1460
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001461static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1462{
1463 u64 n;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001464
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001465 if (rate >= 112000)
1466 rate /= 4;
1467 else if (rate >= 56000)
1468 rate /= 2;
1469
Adrian Knoth0dca1792011-01-26 19:32:14 +01001470 switch (hdspm->io_type) {
1471 case MADIface:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001472 n = 131072000000000ULL; /* 125 MHz */
1473 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001474 case MADI:
1475 case AES32:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001476 n = 110069313433624ULL; /* 105 MHz */
1477 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001478 case RayDAT:
1479 case AIO:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001480 n = 104857600000000ULL; /* 100 MHz */
1481 break;
1482 default:
1483 snd_BUG();
1484 return;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001485 }
1486
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001487 n = div_u64(n, rate);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001488 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001489 snd_BUG_ON(n >> 32);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001490 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1491}
Takashi Iwai763f3562005-06-03 11:25:34 +02001492
1493/* dummy set rate lets see what happens */
Takashi Iwai98274f02005-11-17 14:52:34 +01001494static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
Takashi Iwai763f3562005-06-03 11:25:34 +02001495{
Takashi Iwai763f3562005-06-03 11:25:34 +02001496 int current_rate;
1497 int rate_bits;
1498 int not_set = 0;
Remy Bruno65345992007-08-31 12:21:08 +02001499 int current_speed, target_speed;
Takashi Iwai763f3562005-06-03 11:25:34 +02001500
1501 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1502 it (e.g. during module initialization).
1503 */
1504
1505 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1506
Adrian Knoth0dca1792011-01-26 19:32:14 +01001507 /* SLAVE --- */
Takashi Iwai763f3562005-06-03 11:25:34 +02001508 if (called_internally) {
1509
Adrian Knoth0dca1792011-01-26 19:32:14 +01001510 /* request from ctl or card initialization
1511 just make a warning an remember setting
1512 for future master mode switching */
1513
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001514 snd_printk(KERN_WARNING "HDSPM: "
1515 "Warning: device is not running "
1516 "as a clock master.\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001517 not_set = 1;
1518 } else {
1519
1520 /* hw_param request while in AutoSync mode */
1521 int external_freq =
1522 hdspm_external_sample_rate(hdspm);
1523
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001524 if (hdspm_autosync_ref(hdspm) ==
1525 HDSPM_AUTOSYNC_FROM_NONE) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001526
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001527 snd_printk(KERN_WARNING "HDSPM: "
1528 "Detected no Externel Sync \n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001529 not_set = 1;
1530
1531 } else if (rate != external_freq) {
1532
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001533 snd_printk(KERN_WARNING "HDSPM: "
1534 "Warning: No AutoSync source for "
1535 "requested rate\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001536 not_set = 1;
1537 }
1538 }
1539 }
1540
1541 current_rate = hdspm->system_sample_rate;
1542
1543 /* Changing between Singe, Double and Quad speed is not
1544 allowed if any substreams are open. This is because such a change
1545 causes a shift in the location of the DMA buffers and a reduction
1546 in the number of available buffers.
1547
1548 Note that a similar but essentially insoluble problem exists for
1549 externally-driven rate changes. All we can do is to flag rate
Adrian Knoth0dca1792011-01-26 19:32:14 +01001550 changes in the read/write routines.
Takashi Iwai763f3562005-06-03 11:25:34 +02001551 */
1552
Remy Bruno65345992007-08-31 12:21:08 +02001553 if (current_rate <= 48000)
1554 current_speed = HDSPM_SPEED_SINGLE;
1555 else if (current_rate <= 96000)
1556 current_speed = HDSPM_SPEED_DOUBLE;
1557 else
1558 current_speed = HDSPM_SPEED_QUAD;
1559
1560 if (rate <= 48000)
1561 target_speed = HDSPM_SPEED_SINGLE;
1562 else if (rate <= 96000)
1563 target_speed = HDSPM_SPEED_DOUBLE;
1564 else
1565 target_speed = HDSPM_SPEED_QUAD;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001566
Takashi Iwai763f3562005-06-03 11:25:34 +02001567 switch (rate) {
1568 case 32000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001569 rate_bits = HDSPM_Frequency32KHz;
1570 break;
1571 case 44100:
Takashi Iwai763f3562005-06-03 11:25:34 +02001572 rate_bits = HDSPM_Frequency44_1KHz;
1573 break;
1574 case 48000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001575 rate_bits = HDSPM_Frequency48KHz;
1576 break;
1577 case 64000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001578 rate_bits = HDSPM_Frequency64KHz;
1579 break;
1580 case 88200:
Takashi Iwai763f3562005-06-03 11:25:34 +02001581 rate_bits = HDSPM_Frequency88_2KHz;
1582 break;
1583 case 96000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001584 rate_bits = HDSPM_Frequency96KHz;
1585 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001586 case 128000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001587 rate_bits = HDSPM_Frequency128KHz;
1588 break;
1589 case 176400:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001590 rate_bits = HDSPM_Frequency176_4KHz;
1591 break;
1592 case 192000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001593 rate_bits = HDSPM_Frequency192KHz;
1594 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001595 default:
1596 return -EINVAL;
1597 }
1598
Remy Bruno65345992007-08-31 12:21:08 +02001599 if (current_speed != target_speed
Takashi Iwai763f3562005-06-03 11:25:34 +02001600 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1601 snd_printk
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001602 (KERN_ERR "HDSPM: "
Remy Bruno65345992007-08-31 12:21:08 +02001603 "cannot change from %s speed to %s speed mode "
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001604 "(capture PID = %d, playback PID = %d)\n",
Remy Bruno65345992007-08-31 12:21:08 +02001605 hdspm_speed_names[current_speed],
1606 hdspm_speed_names[target_speed],
Takashi Iwai763f3562005-06-03 11:25:34 +02001607 hdspm->capture_pid, hdspm->playback_pid);
1608 return -EBUSY;
1609 }
1610
1611 hdspm->control_register &= ~HDSPM_FrequencyMask;
1612 hdspm->control_register |= rate_bits;
1613 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1614
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001615 /* For AES32, need to set DDS value in FREQ register
1616 For MADI, also apparently */
1617 hdspm_set_dds_value(hdspm, rate);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001618
1619 if (AES32 == hdspm->io_type && rate != current_rate)
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001620 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
Takashi Iwai763f3562005-06-03 11:25:34 +02001621
1622 hdspm->system_sample_rate = rate;
1623
Adrian Knoth0dca1792011-01-26 19:32:14 +01001624 if (rate <= 48000) {
1625 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1626 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1627 hdspm->max_channels_in = hdspm->ss_in_channels;
1628 hdspm->max_channels_out = hdspm->ss_out_channels;
1629 hdspm->port_names_in = hdspm->port_names_in_ss;
1630 hdspm->port_names_out = hdspm->port_names_out_ss;
1631 } else if (rate <= 96000) {
1632 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1633 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1634 hdspm->max_channels_in = hdspm->ds_in_channels;
1635 hdspm->max_channels_out = hdspm->ds_out_channels;
1636 hdspm->port_names_in = hdspm->port_names_in_ds;
1637 hdspm->port_names_out = hdspm->port_names_out_ds;
1638 } else {
1639 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1640 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1641 hdspm->max_channels_in = hdspm->qs_in_channels;
1642 hdspm->max_channels_out = hdspm->qs_out_channels;
1643 hdspm->port_names_in = hdspm->port_names_in_qs;
1644 hdspm->port_names_out = hdspm->port_names_out_qs;
1645 }
1646
Takashi Iwai763f3562005-06-03 11:25:34 +02001647 if (not_set != 0)
1648 return -1;
1649
1650 return 0;
1651}
1652
1653/* mainly for init to 0 on load */
Takashi Iwai98274f02005-11-17 14:52:34 +01001654static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
Takashi Iwai763f3562005-06-03 11:25:34 +02001655{
1656 int i, j;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001657 unsigned int gain;
1658
1659 if (sgain > UNITY_GAIN)
1660 gain = UNITY_GAIN;
1661 else if (sgain < 0)
1662 gain = 0;
1663 else
1664 gain = sgain;
Takashi Iwai763f3562005-06-03 11:25:34 +02001665
1666 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1667 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1668 hdspm_write_in_gain(hdspm, i, j, gain);
1669 hdspm_write_pb_gain(hdspm, i, j, gain);
1670 }
1671}
1672
1673/*----------------------------------------------------------------------------
1674 MIDI
1675 ----------------------------------------------------------------------------*/
1676
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001677static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1678 int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001679{
1680 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001681 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
Takashi Iwai763f3562005-06-03 11:25:34 +02001682}
1683
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001684static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1685 int val)
Takashi Iwai763f3562005-06-03 11:25:34 +02001686{
1687 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001688 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
Takashi Iwai763f3562005-06-03 11:25:34 +02001689}
1690
Takashi Iwai98274f02005-11-17 14:52:34 +01001691static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001692{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001693 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001694}
1695
Takashi Iwai98274f02005-11-17 14:52:34 +01001696static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001697{
1698 int fifo_bytes_used;
1699
Adrian Knoth0dca1792011-01-26 19:32:14 +01001700 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001701
1702 if (fifo_bytes_used < 128)
1703 return 128 - fifo_bytes_used;
1704 else
1705 return 0;
1706}
1707
Denys Vlasenko62cef822008-04-14 13:04:18 +02001708static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001709{
1710 while (snd_hdspm_midi_input_available (hdspm, id))
1711 snd_hdspm_midi_read_byte (hdspm, id);
1712}
1713
Takashi Iwai98274f02005-11-17 14:52:34 +01001714static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001715{
1716 unsigned long flags;
1717 int n_pending;
1718 int to_write;
1719 int i;
1720 unsigned char buf[128];
1721
1722 /* Output is not interrupt driven */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001723
Takashi Iwai763f3562005-06-03 11:25:34 +02001724 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001725 if (hmidi->output &&
1726 !snd_rawmidi_transmit_empty (hmidi->output)) {
1727 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1728 hmidi->id);
1729 if (n_pending > 0) {
1730 if (n_pending > (int)sizeof (buf))
1731 n_pending = sizeof (buf);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001732
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001733 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1734 n_pending);
1735 if (to_write > 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001736 for (i = 0; i < to_write; ++i)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001737 snd_hdspm_midi_write_byte (hmidi->hdspm,
1738 hmidi->id,
1739 buf[i]);
Takashi Iwai763f3562005-06-03 11:25:34 +02001740 }
1741 }
1742 }
1743 spin_unlock_irqrestore (&hmidi->lock, flags);
1744 return 0;
1745}
1746
Takashi Iwai98274f02005-11-17 14:52:34 +01001747static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001748{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001749 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1750 * input FIFO size
1751 */
Takashi Iwai763f3562005-06-03 11:25:34 +02001752 unsigned long flags;
1753 int n_pending;
1754 int i;
1755
1756 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001757 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1758 if (n_pending > 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001759 if (hmidi->input) {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001760 if (n_pending > (int)sizeof (buf))
Takashi Iwai763f3562005-06-03 11:25:34 +02001761 n_pending = sizeof (buf);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001762 for (i = 0; i < n_pending; ++i)
1763 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1764 hmidi->id);
1765 if (n_pending)
1766 snd_rawmidi_receive (hmidi->input, buf,
1767 n_pending);
Takashi Iwai763f3562005-06-03 11:25:34 +02001768 } else {
1769 /* flush the MIDI input FIFO */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001770 while (n_pending--)
1771 snd_hdspm_midi_read_byte (hmidi->hdspm,
1772 hmidi->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02001773 }
1774 }
1775 hmidi->pending = 0;
Adrian Knothc0da0012011-06-12 17:26:17 +02001776 spin_unlock_irqrestore(&hmidi->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001777
Adrian Knothc0da0012011-06-12 17:26:17 +02001778 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001779 hmidi->hdspm->control_register |= hmidi->ie;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001780 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1781 hmidi->hdspm->control_register);
Adrian Knothc0da0012011-06-12 17:26:17 +02001782 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001783
Takashi Iwai763f3562005-06-03 11:25:34 +02001784 return snd_hdspm_midi_output_write (hmidi);
1785}
1786
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001787static void
1788snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001789{
Takashi Iwai98274f02005-11-17 14:52:34 +01001790 struct hdspm *hdspm;
1791 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001792 unsigned long flags;
Takashi Iwai763f3562005-06-03 11:25:34 +02001793
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001794 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001795 hdspm = hmidi->hdspm;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001796
Takashi Iwai763f3562005-06-03 11:25:34 +02001797 spin_lock_irqsave (&hdspm->lock, flags);
1798 if (up) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001799 if (!(hdspm->control_register & hmidi->ie)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001800 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001801 hdspm->control_register |= hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001802 }
1803 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001804 hdspm->control_register &= ~hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001805 }
1806
1807 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1808 spin_unlock_irqrestore (&hdspm->lock, flags);
1809}
1810
1811static void snd_hdspm_midi_output_timer(unsigned long data)
1812{
Takashi Iwai98274f02005-11-17 14:52:34 +01001813 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001814 unsigned long flags;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001815
Takashi Iwai763f3562005-06-03 11:25:34 +02001816 snd_hdspm_midi_output_write(hmidi);
1817 spin_lock_irqsave (&hmidi->lock, flags);
1818
1819 /* this does not bump hmidi->istimer, because the
1820 kernel automatically removed the timer when it
1821 expired, and we are now adding it back, thus
Adrian Knoth0dca1792011-01-26 19:32:14 +01001822 leaving istimer wherever it was set before.
Takashi Iwai763f3562005-06-03 11:25:34 +02001823 */
1824
1825 if (hmidi->istimer) {
1826 hmidi->timer.expires = 1 + jiffies;
1827 add_timer(&hmidi->timer);
1828 }
1829
1830 spin_unlock_irqrestore (&hmidi->lock, flags);
1831}
1832
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001833static void
1834snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001835{
Takashi Iwai98274f02005-11-17 14:52:34 +01001836 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001837 unsigned long flags;
1838
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001839 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001840 spin_lock_irqsave (&hmidi->lock, flags);
1841 if (up) {
1842 if (!hmidi->istimer) {
1843 init_timer(&hmidi->timer);
1844 hmidi->timer.function = snd_hdspm_midi_output_timer;
1845 hmidi->timer.data = (unsigned long) hmidi;
1846 hmidi->timer.expires = 1 + jiffies;
1847 add_timer(&hmidi->timer);
1848 hmidi->istimer++;
1849 }
1850 } else {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001851 if (hmidi->istimer && --hmidi->istimer <= 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02001852 del_timer (&hmidi->timer);
Takashi Iwai763f3562005-06-03 11:25:34 +02001853 }
1854 spin_unlock_irqrestore (&hmidi->lock, flags);
1855 if (up)
1856 snd_hdspm_midi_output_write(hmidi);
1857}
1858
Takashi Iwai98274f02005-11-17 14:52:34 +01001859static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001860{
Takashi Iwai98274f02005-11-17 14:52:34 +01001861 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001862
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001863 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001864 spin_lock_irq (&hmidi->lock);
1865 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1866 hmidi->input = substream;
1867 spin_unlock_irq (&hmidi->lock);
1868
1869 return 0;
1870}
1871
Takashi Iwai98274f02005-11-17 14:52:34 +01001872static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001873{
Takashi Iwai98274f02005-11-17 14:52:34 +01001874 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001875
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001876 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001877 spin_lock_irq (&hmidi->lock);
1878 hmidi->output = substream;
1879 spin_unlock_irq (&hmidi->lock);
1880
1881 return 0;
1882}
1883
Takashi Iwai98274f02005-11-17 14:52:34 +01001884static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001885{
Takashi Iwai98274f02005-11-17 14:52:34 +01001886 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001887
1888 snd_hdspm_midi_input_trigger (substream, 0);
1889
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001890 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001891 spin_lock_irq (&hmidi->lock);
1892 hmidi->input = NULL;
1893 spin_unlock_irq (&hmidi->lock);
1894
1895 return 0;
1896}
1897
Takashi Iwai98274f02005-11-17 14:52:34 +01001898static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001899{
Takashi Iwai98274f02005-11-17 14:52:34 +01001900 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001901
1902 snd_hdspm_midi_output_trigger (substream, 0);
1903
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001904 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001905 spin_lock_irq (&hmidi->lock);
1906 hmidi->output = NULL;
1907 spin_unlock_irq (&hmidi->lock);
1908
1909 return 0;
1910}
1911
Takashi Iwai98274f02005-11-17 14:52:34 +01001912static struct snd_rawmidi_ops snd_hdspm_midi_output =
Takashi Iwai763f3562005-06-03 11:25:34 +02001913{
1914 .open = snd_hdspm_midi_output_open,
1915 .close = snd_hdspm_midi_output_close,
1916 .trigger = snd_hdspm_midi_output_trigger,
1917};
1918
Takashi Iwai98274f02005-11-17 14:52:34 +01001919static struct snd_rawmidi_ops snd_hdspm_midi_input =
Takashi Iwai763f3562005-06-03 11:25:34 +02001920{
1921 .open = snd_hdspm_midi_input_open,
1922 .close = snd_hdspm_midi_input_close,
1923 .trigger = snd_hdspm_midi_input_trigger,
1924};
1925
Bill Pembertone23e7a12012-12-06 12:35:10 -05001926static int snd_hdspm_create_midi(struct snd_card *card,
1927 struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001928{
1929 int err;
1930 char buf[32];
1931
1932 hdspm->midi[id].id = id;
Takashi Iwai763f3562005-06-03 11:25:34 +02001933 hdspm->midi[id].hdspm = hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +02001934 spin_lock_init (&hdspm->midi[id].lock);
1935
Adrian Knoth0dca1792011-01-26 19:32:14 +01001936 if (0 == id) {
1937 if (MADIface == hdspm->io_type) {
1938 /* MIDI-over-MADI on HDSPe MADIface */
1939 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1940 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1941 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1942 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1943 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1944 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1945 } else {
1946 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1947 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1948 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1949 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1950 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
1951 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
1952 }
1953 } else if (1 == id) {
1954 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
1955 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
1956 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
1957 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
1958 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
1959 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
1960 } else if ((2 == id) && (MADI == hdspm->io_type)) {
1961 /* MIDI-over-MADI on HDSPe MADI */
1962 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1963 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1964 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
1965 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
1966 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1967 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
1968 } else if (2 == id) {
1969 /* TCO MTC, read only */
1970 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1971 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1972 hdspm->midi[2].dataOut = -1;
1973 hdspm->midi[2].statusOut = -1;
1974 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1975 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
1976 } else if (3 == id) {
1977 /* TCO MTC on HDSPe MADI */
1978 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
1979 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
1980 hdspm->midi[3].dataOut = -1;
1981 hdspm->midi[3].statusOut = -1;
1982 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
1983 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
1984 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001985
Adrian Knoth0dca1792011-01-26 19:32:14 +01001986 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
1987 (MADIface == hdspm->io_type)))) {
1988 if ((id == 0) && (MADIface == hdspm->io_type)) {
1989 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1990 } else if ((id == 2) && (MADI == hdspm->io_type)) {
1991 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1992 } else {
1993 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
1994 }
1995 err = snd_rawmidi_new(card, buf, id, 1, 1,
1996 &hdspm->midi[id].rmidi);
1997 if (err < 0)
1998 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02001999
Adrian Knoth0dca1792011-01-26 19:32:14 +01002000 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
2001 card->id, id+1);
2002 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
Takashi Iwai763f3562005-06-03 11:25:34 +02002003
Adrian Knoth0dca1792011-01-26 19:32:14 +01002004 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2005 SNDRV_RAWMIDI_STREAM_OUTPUT,
2006 &snd_hdspm_midi_output);
2007 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2008 SNDRV_RAWMIDI_STREAM_INPUT,
2009 &snd_hdspm_midi_input);
2010
2011 hdspm->midi[id].rmidi->info_flags |=
2012 SNDRV_RAWMIDI_INFO_OUTPUT |
2013 SNDRV_RAWMIDI_INFO_INPUT |
2014 SNDRV_RAWMIDI_INFO_DUPLEX;
2015 } else {
2016 /* TCO MTC, read only */
2017 sprintf(buf, "%s MTC %d", card->shortname, id+1);
2018 err = snd_rawmidi_new(card, buf, id, 1, 1,
2019 &hdspm->midi[id].rmidi);
2020 if (err < 0)
2021 return err;
2022
2023 sprintf(hdspm->midi[id].rmidi->name,
2024 "%s MTC %d", card->id, id+1);
2025 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
2026
2027 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
2028 SNDRV_RAWMIDI_STREAM_INPUT,
2029 &snd_hdspm_midi_input);
2030
2031 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
2032 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002033
2034 return 0;
2035}
2036
2037
2038static void hdspm_midi_tasklet(unsigned long arg)
2039{
Takashi Iwai98274f02005-11-17 14:52:34 +01002040 struct hdspm *hdspm = (struct hdspm *)arg;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002041 int i = 0;
2042
2043 while (i < hdspm->midiPorts) {
2044 if (hdspm->midi[i].pending)
2045 snd_hdspm_midi_input_read(&hdspm->midi[i]);
2046
2047 i++;
2048 }
2049}
Takashi Iwai763f3562005-06-03 11:25:34 +02002050
2051
2052/*-----------------------------------------------------------------------------
2053 Status Interface
2054 ----------------------------------------------------------------------------*/
2055
2056/* get the system sample rate which is set */
2057
Adrian Knoth0dca1792011-01-26 19:32:14 +01002058
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002059static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
2060{
2061 unsigned int period, rate;
2062
2063 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
2064 rate = hdspm_calc_dds_value(hdspm, period);
2065
2066 return rate;
2067}
2068
Adrian Knoth0dca1792011-01-26 19:32:14 +01002069/**
2070 * Calculate the real sample rate from the
2071 * current DDS value.
2072 **/
2073static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
2074{
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002075 unsigned int rate;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002076
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002077 rate = hdspm_get_pll_freq(hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002078
Adrian Knotha97bda72012-05-30 14:23:18 +02002079 if (rate > 207000) {
Adrian Knoth21a164d2012-10-19 17:42:23 +02002080 /* Unreasonable high sample rate as seen on PCI MADI cards. */
2081 if (0 == hdspm_system_clock_mode(hdspm)) {
2082 /* master mode, return internal sample rate */
2083 rate = hdspm->system_sample_rate;
2084 } else {
2085 /* slave mode, return external sample rate */
2086 rate = hdspm_external_sample_rate(hdspm);
2087 }
Adrian Knotha97bda72012-05-30 14:23:18 +02002088 }
2089
Adrian Knoth0dca1792011-01-26 19:32:14 +01002090 return rate;
2091}
2092
2093
Takashi Iwai763f3562005-06-03 11:25:34 +02002094#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002095{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2096 .name = xname, \
2097 .index = xindex, \
2098 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2099 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2100 .info = snd_hdspm_info_system_sample_rate, \
2101 .put = snd_hdspm_put_system_sample_rate, \
2102 .get = snd_hdspm_get_system_sample_rate \
Takashi Iwai763f3562005-06-03 11:25:34 +02002103}
2104
Takashi Iwai98274f02005-11-17 14:52:34 +01002105static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002107{
2108 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2109 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002110 uinfo->value.integer.min = 27000;
2111 uinfo->value.integer.max = 207000;
2112 uinfo->value.integer.step = 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002113 return 0;
2114}
2115
Adrian Knoth0dca1792011-01-26 19:32:14 +01002116
Takashi Iwai98274f02005-11-17 14:52:34 +01002117static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
2118 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002119 ucontrol)
2120{
Takashi Iwai98274f02005-11-17 14:52:34 +01002121 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002122
Adrian Knoth0dca1792011-01-26 19:32:14 +01002123 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002124 return 0;
2125}
2126
Adrian Knoth41285a92012-10-19 17:42:22 +02002127static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
2128 struct snd_ctl_elem_value *
2129 ucontrol)
2130{
2131 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2132
2133 hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
2134 return 0;
2135}
2136
Adrian Knoth0dca1792011-01-26 19:32:14 +01002137
2138/**
2139 * Returns the WordClock sample rate class for the given card.
2140 **/
2141static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
2142{
2143 int status;
2144
2145 switch (hdspm->io_type) {
2146 case RayDAT:
2147 case AIO:
2148 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2149 return (status >> 16) & 0xF;
2150 break;
2151 default:
2152 break;
2153 }
2154
2155
2156 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002157}
2158
Adrian Knoth0dca1792011-01-26 19:32:14 +01002159
2160/**
2161 * Returns the TCO sample rate class for the given card.
2162 **/
2163static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2164{
2165 int status;
2166
2167 if (hdspm->tco) {
2168 switch (hdspm->io_type) {
2169 case RayDAT:
2170 case AIO:
2171 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2172 return (status >> 20) & 0xF;
2173 break;
2174 default:
2175 break;
2176 }
2177 }
2178
2179 return 0;
2180}
2181
2182
2183/**
2184 * Returns the SYNC_IN sample rate class for the given card.
2185 **/
2186static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2187{
2188 int status;
2189
2190 if (hdspm->tco) {
2191 switch (hdspm->io_type) {
2192 case RayDAT:
2193 case AIO:
2194 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2195 return (status >> 12) & 0xF;
2196 break;
2197 default:
2198 break;
2199 }
2200 }
2201
2202 return 0;
2203}
2204
2205
2206/**
2207 * Returns the sample rate class for input source <idx> for
2208 * 'new style' cards like the AIO and RayDAT.
2209 **/
2210static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2211{
2212 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2213
2214 return (status >> (idx*4)) & 0xF;
2215}
2216
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002217#define ENUMERATED_CTL_INFO(info, texts) \
2218{ \
2219 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; \
2220 uinfo->count = 1; \
2221 uinfo->value.enumerated.items = ARRAY_SIZE(texts); \
2222 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) \
2223 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; \
2224 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); \
2225}
2226
Adrian Knoth0dca1792011-01-26 19:32:14 +01002227
2228
2229#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2230{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2231 .name = xname, \
2232 .private_value = xindex, \
2233 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2234 .info = snd_hdspm_info_autosync_sample_rate, \
2235 .get = snd_hdspm_get_autosync_sample_rate \
2236}
2237
2238
Takashi Iwai98274f02005-11-17 14:52:34 +01002239static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002241{
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002242 ENUMERATED_CTL_INFO(uinfo, texts_freq);
Takashi Iwai763f3562005-06-03 11:25:34 +02002243 return 0;
2244}
2245
Adrian Knoth0dca1792011-01-26 19:32:14 +01002246
Takashi Iwai98274f02005-11-17 14:52:34 +01002247static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002249 ucontrol)
2250{
Takashi Iwai98274f02005-11-17 14:52:34 +01002251 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002252
Adrian Knoth0dca1792011-01-26 19:32:14 +01002253 switch (hdspm->io_type) {
2254 case RayDAT:
2255 switch (kcontrol->private_value) {
2256 case 0:
2257 ucontrol->value.enumerated.item[0] =
2258 hdspm_get_wc_sample_rate(hdspm);
2259 break;
2260 case 7:
2261 ucontrol->value.enumerated.item[0] =
2262 hdspm_get_tco_sample_rate(hdspm);
2263 break;
2264 case 8:
2265 ucontrol->value.enumerated.item[0] =
2266 hdspm_get_sync_in_sample_rate(hdspm);
2267 break;
2268 default:
2269 ucontrol->value.enumerated.item[0] =
2270 hdspm_get_s1_sample_rate(hdspm,
2271 kcontrol->private_value-1);
2272 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002273 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002274
Adrian Knoth0dca1792011-01-26 19:32:14 +01002275 case AIO:
2276 switch (kcontrol->private_value) {
2277 case 0: /* WC */
2278 ucontrol->value.enumerated.item[0] =
2279 hdspm_get_wc_sample_rate(hdspm);
2280 break;
2281 case 4: /* TCO */
2282 ucontrol->value.enumerated.item[0] =
2283 hdspm_get_tco_sample_rate(hdspm);
2284 break;
2285 case 5: /* SYNC_IN */
2286 ucontrol->value.enumerated.item[0] =
2287 hdspm_get_sync_in_sample_rate(hdspm);
2288 break;
2289 default:
2290 ucontrol->value.enumerated.item[0] =
2291 hdspm_get_s1_sample_rate(hdspm,
2292 ucontrol->id.index-1);
2293 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002294 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002295
2296 case AES32:
2297
2298 switch (kcontrol->private_value) {
2299 case 0: /* WC */
2300 ucontrol->value.enumerated.item[0] =
2301 hdspm_get_wc_sample_rate(hdspm);
2302 break;
2303 case 9: /* TCO */
2304 ucontrol->value.enumerated.item[0] =
2305 hdspm_get_tco_sample_rate(hdspm);
2306 break;
2307 case 10: /* SYNC_IN */
2308 ucontrol->value.enumerated.item[0] =
2309 hdspm_get_sync_in_sample_rate(hdspm);
2310 break;
2311 default: /* AES1 to AES8 */
2312 ucontrol->value.enumerated.item[0] =
2313 hdspm_get_s1_sample_rate(hdspm,
2314 kcontrol->private_value-1);
2315 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002316 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002317 break;
Adrian Knothb8812c52012-10-19 17:42:26 +02002318
2319 case MADI:
2320 case MADIface:
2321 {
2322 int rate = hdspm_external_sample_rate(hdspm);
2323 int i, selected_rate = 0;
2324 for (i = 1; i < 10; i++)
2325 if (HDSPM_bit2freq(i) == rate) {
2326 selected_rate = i;
2327 break;
2328 }
2329 ucontrol->value.enumerated.item[0] = selected_rate;
2330 }
2331 break;
2332
Takashi Iwai763f3562005-06-03 11:25:34 +02002333 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002334 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002335 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002336
Takashi Iwai763f3562005-06-03 11:25:34 +02002337 return 0;
2338}
2339
Adrian Knoth0dca1792011-01-26 19:32:14 +01002340
Takashi Iwai763f3562005-06-03 11:25:34 +02002341#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002342{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2343 .name = xname, \
2344 .index = xindex, \
2345 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2346 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2347 .info = snd_hdspm_info_system_clock_mode, \
2348 .get = snd_hdspm_get_system_clock_mode, \
2349 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002350}
2351
2352
Adrian Knoth0dca1792011-01-26 19:32:14 +01002353/**
2354 * Returns the system clock mode for the given card.
2355 * @returns 0 - master, 1 - slave
2356 **/
2357static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002358{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002359 switch (hdspm->io_type) {
2360 case AIO:
2361 case RayDAT:
2362 if (hdspm->settings_register & HDSPM_c0Master)
2363 return 0;
2364 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002365
Adrian Knoth0dca1792011-01-26 19:32:14 +01002366 default:
2367 if (hdspm->control_register & HDSPM_ClockModeMaster)
2368 return 0;
2369 }
2370
Takashi Iwai763f3562005-06-03 11:25:34 +02002371 return 1;
2372}
2373
Adrian Knoth0dca1792011-01-26 19:32:14 +01002374
2375/**
2376 * Sets the system clock mode.
2377 * @param mode 0 - master, 1 - slave
2378 **/
2379static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2380{
2381 switch (hdspm->io_type) {
2382 case AIO:
2383 case RayDAT:
2384 if (0 == mode)
2385 hdspm->settings_register |= HDSPM_c0Master;
2386 else
2387 hdspm->settings_register &= ~HDSPM_c0Master;
2388
2389 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2390 break;
2391
2392 default:
2393 if (0 == mode)
2394 hdspm->control_register |= HDSPM_ClockModeMaster;
2395 else
2396 hdspm->control_register &= ~HDSPM_ClockModeMaster;
2397
2398 hdspm_write(hdspm, HDSPM_controlRegister,
2399 hdspm->control_register);
2400 }
2401}
2402
2403
Takashi Iwai98274f02005-11-17 14:52:34 +01002404static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002406{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002407 static char *texts[] = { "Master", "AutoSync" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01002408 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02002409 return 0;
2410}
2411
Takashi Iwai98274f02005-11-17 14:52:34 +01002412static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002414{
Takashi Iwai98274f02005-11-17 14:52:34 +01002415 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002416
Adrian Knoth0dca1792011-01-26 19:32:14 +01002417 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002418 return 0;
2419}
2420
Adrian Knoth0dca1792011-01-26 19:32:14 +01002421static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2425 int val;
2426
2427 if (!snd_hdspm_use_is_exclusive(hdspm))
2428 return -EBUSY;
2429
2430 val = ucontrol->value.enumerated.item[0];
2431 if (val < 0)
2432 val = 0;
2433 else if (val > 1)
2434 val = 1;
2435
2436 hdspm_set_system_clock_mode(hdspm, val);
2437
2438 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002439}
2440
Adrian Knoth0dca1792011-01-26 19:32:14 +01002441
2442#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2443{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2444 .name = xname, \
2445 .index = xindex, \
2446 .info = snd_hdspm_info_clock_source, \
2447 .get = snd_hdspm_get_clock_source, \
2448 .put = snd_hdspm_put_clock_source \
2449}
2450
2451
Takashi Iwai98274f02005-11-17 14:52:34 +01002452static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002453{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002454 switch (hdspm->system_sample_rate) {
2455 case 32000: return 0;
2456 case 44100: return 1;
2457 case 48000: return 2;
2458 case 64000: return 3;
2459 case 88200: return 4;
2460 case 96000: return 5;
2461 case 128000: return 6;
2462 case 176400: return 7;
2463 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002464 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002465
2466 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002467}
2468
Takashi Iwai98274f02005-11-17 14:52:34 +01002469static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002470{
2471 int rate;
2472 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002473 case 0:
2474 rate = 32000; break;
2475 case 1:
2476 rate = 44100; break;
2477 case 2:
2478 rate = 48000; break;
2479 case 3:
2480 rate = 64000; break;
2481 case 4:
2482 rate = 88200; break;
2483 case 5:
2484 rate = 96000; break;
2485 case 6:
2486 rate = 128000; break;
2487 case 7:
2488 rate = 176400; break;
2489 case 8:
2490 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002491 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002492 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002493 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002494 hdspm_set_rate(hdspm, rate, 1);
2495 return 0;
2496}
2497
Takashi Iwai98274f02005-11-17 14:52:34 +01002498static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2499 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002500{
Takashi Iwai763f3562005-06-03 11:25:34 +02002501 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2502 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002503 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002504
2505 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2506 uinfo->value.enumerated.item =
2507 uinfo->value.enumerated.items - 1;
2508
2509 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002510 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002511
2512 return 0;
2513}
2514
Takashi Iwai98274f02005-11-17 14:52:34 +01002515static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002517{
Takashi Iwai98274f02005-11-17 14:52:34 +01002518 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002519
2520 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2521 return 0;
2522}
2523
Takashi Iwai98274f02005-11-17 14:52:34 +01002524static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002526{
Takashi Iwai98274f02005-11-17 14:52:34 +01002527 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002528 int change;
2529 int val;
2530
2531 if (!snd_hdspm_use_is_exclusive(hdspm))
2532 return -EBUSY;
2533 val = ucontrol->value.enumerated.item[0];
2534 if (val < 0)
2535 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002536 if (val > 9)
2537 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002538 spin_lock_irq(&hdspm->lock);
2539 if (val != hdspm_clock_source(hdspm))
2540 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2541 else
2542 change = 0;
2543 spin_unlock_irq(&hdspm->lock);
2544 return change;
2545}
2546
Adrian Knoth0dca1792011-01-26 19:32:14 +01002547
Takashi Iwai763f3562005-06-03 11:25:34 +02002548#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002549{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002550 .name = xname, \
2551 .index = xindex, \
2552 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2553 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2554 .info = snd_hdspm_info_pref_sync_ref, \
2555 .get = snd_hdspm_get_pref_sync_ref, \
2556 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002557}
2558
Adrian Knoth0dca1792011-01-26 19:32:14 +01002559
2560/**
2561 * Returns the current preferred sync reference setting.
2562 * The semantics of the return value are depending on the
2563 * card, please see the comments for clarification.
2564 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002565static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002566{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002567 switch (hdspm->io_type) {
2568 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002569 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002570 case 0: return 0; /* WC */
2571 case HDSPM_SyncRef0: return 1; /* AES 1 */
2572 case HDSPM_SyncRef1: return 2; /* AES 2 */
2573 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2574 case HDSPM_SyncRef2: return 4; /* AES 4 */
2575 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2576 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2577 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2578 return 7; /* AES 7 */
2579 case HDSPM_SyncRef3: return 8; /* AES 8 */
2580 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002581 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002582 break;
2583
2584 case MADI:
2585 case MADIface:
2586 if (hdspm->tco) {
2587 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2588 case 0: return 0; /* WC */
2589 case HDSPM_SyncRef0: return 1; /* MADI */
2590 case HDSPM_SyncRef1: return 2; /* TCO */
2591 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2592 return 3; /* SYNC_IN */
2593 }
2594 } else {
2595 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2596 case 0: return 0; /* WC */
2597 case HDSPM_SyncRef0: return 1; /* MADI */
2598 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2599 return 2; /* SYNC_IN */
2600 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002601 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002602 break;
2603
2604 case RayDAT:
2605 if (hdspm->tco) {
2606 switch ((hdspm->settings_register &
2607 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2608 case 0: return 0; /* WC */
2609 case 3: return 1; /* ADAT 1 */
2610 case 4: return 2; /* ADAT 2 */
2611 case 5: return 3; /* ADAT 3 */
2612 case 6: return 4; /* ADAT 4 */
2613 case 1: return 5; /* AES */
2614 case 2: return 6; /* SPDIF */
2615 case 9: return 7; /* TCO */
2616 case 10: return 8; /* SYNC_IN */
2617 }
2618 } else {
2619 switch ((hdspm->settings_register &
2620 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2621 case 0: return 0; /* WC */
2622 case 3: return 1; /* ADAT 1 */
2623 case 4: return 2; /* ADAT 2 */
2624 case 5: return 3; /* ADAT 3 */
2625 case 6: return 4; /* ADAT 4 */
2626 case 1: return 5; /* AES */
2627 case 2: return 6; /* SPDIF */
2628 case 10: return 7; /* SYNC_IN */
2629 }
2630 }
2631
2632 break;
2633
2634 case AIO:
2635 if (hdspm->tco) {
2636 switch ((hdspm->settings_register &
2637 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2638 case 0: return 0; /* WC */
2639 case 3: return 1; /* ADAT */
2640 case 1: return 2; /* AES */
2641 case 2: return 3; /* SPDIF */
2642 case 9: return 4; /* TCO */
2643 case 10: return 5; /* SYNC_IN */
2644 }
2645 } else {
2646 switch ((hdspm->settings_register &
2647 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2648 case 0: return 0; /* WC */
2649 case 3: return 1; /* ADAT */
2650 case 1: return 2; /* AES */
2651 case 2: return 3; /* SPDIF */
2652 case 10: return 4; /* SYNC_IN */
2653 }
2654 }
2655
2656 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002657 }
2658
Adrian Knoth0dca1792011-01-26 19:32:14 +01002659 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002660}
2661
Adrian Knoth0dca1792011-01-26 19:32:14 +01002662
2663/**
2664 * Set the preferred sync reference to <pref>. The semantics
2665 * of <pref> are depending on the card type, see the comments
2666 * for clarification.
2667 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002668static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002669{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002670 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002671
Adrian Knoth0dca1792011-01-26 19:32:14 +01002672 switch (hdspm->io_type) {
2673 case AES32:
2674 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002675 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002676 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002677 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002678 case 1: /* AES 1 */
2679 hdspm->control_register |= HDSPM_SyncRef0;
2680 break;
2681 case 2: /* AES 2 */
2682 hdspm->control_register |= HDSPM_SyncRef1;
2683 break;
2684 case 3: /* AES 3 */
2685 hdspm->control_register |=
2686 HDSPM_SyncRef1+HDSPM_SyncRef0;
2687 break;
2688 case 4: /* AES 4 */
2689 hdspm->control_register |= HDSPM_SyncRef2;
2690 break;
2691 case 5: /* AES 5 */
2692 hdspm->control_register |=
2693 HDSPM_SyncRef2+HDSPM_SyncRef0;
2694 break;
2695 case 6: /* AES 6 */
2696 hdspm->control_register |=
2697 HDSPM_SyncRef2+HDSPM_SyncRef1;
2698 break;
2699 case 7: /* AES 7 */
2700 hdspm->control_register |=
2701 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2702 break;
2703 case 8: /* AES 8 */
2704 hdspm->control_register |= HDSPM_SyncRef3;
2705 break;
2706 case 9: /* TCO */
2707 hdspm->control_register |=
2708 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002709 break;
2710 default:
2711 return -1;
2712 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002713
2714 break;
2715
2716 case MADI:
2717 case MADIface:
2718 hdspm->control_register &= ~HDSPM_SyncRefMask;
2719 if (hdspm->tco) {
2720 switch (pref) {
2721 case 0: /* WC */
2722 break;
2723 case 1: /* MADI */
2724 hdspm->control_register |= HDSPM_SyncRef0;
2725 break;
2726 case 2: /* TCO */
2727 hdspm->control_register |= HDSPM_SyncRef1;
2728 break;
2729 case 3: /* SYNC_IN */
2730 hdspm->control_register |=
2731 HDSPM_SyncRef0+HDSPM_SyncRef1;
2732 break;
2733 default:
2734 return -1;
2735 }
2736 } else {
2737 switch (pref) {
2738 case 0: /* WC */
2739 break;
2740 case 1: /* MADI */
2741 hdspm->control_register |= HDSPM_SyncRef0;
2742 break;
2743 case 2: /* SYNC_IN */
2744 hdspm->control_register |=
2745 HDSPM_SyncRef0+HDSPM_SyncRef1;
2746 break;
2747 default:
2748 return -1;
2749 }
2750 }
2751
2752 break;
2753
2754 case RayDAT:
2755 if (hdspm->tco) {
2756 switch (pref) {
2757 case 0: p = 0; break; /* WC */
2758 case 1: p = 3; break; /* ADAT 1 */
2759 case 2: p = 4; break; /* ADAT 2 */
2760 case 3: p = 5; break; /* ADAT 3 */
2761 case 4: p = 6; break; /* ADAT 4 */
2762 case 5: p = 1; break; /* AES */
2763 case 6: p = 2; break; /* SPDIF */
2764 case 7: p = 9; break; /* TCO */
2765 case 8: p = 10; break; /* SYNC_IN */
2766 default: return -1;
2767 }
2768 } else {
2769 switch (pref) {
2770 case 0: p = 0; break; /* WC */
2771 case 1: p = 3; break; /* ADAT 1 */
2772 case 2: p = 4; break; /* ADAT 2 */
2773 case 3: p = 5; break; /* ADAT 3 */
2774 case 4: p = 6; break; /* ADAT 4 */
2775 case 5: p = 1; break; /* AES */
2776 case 6: p = 2; break; /* SPDIF */
2777 case 7: p = 10; break; /* SYNC_IN */
2778 default: return -1;
2779 }
2780 }
2781 break;
2782
2783 case AIO:
2784 if (hdspm->tco) {
2785 switch (pref) {
2786 case 0: p = 0; break; /* WC */
2787 case 1: p = 3; break; /* ADAT */
2788 case 2: p = 1; break; /* AES */
2789 case 3: p = 2; break; /* SPDIF */
2790 case 4: p = 9; break; /* TCO */
2791 case 5: p = 10; break; /* SYNC_IN */
2792 default: return -1;
2793 }
2794 } else {
2795 switch (pref) {
2796 case 0: p = 0; break; /* WC */
2797 case 1: p = 3; break; /* ADAT */
2798 case 2: p = 1; break; /* AES */
2799 case 3: p = 2; break; /* SPDIF */
2800 case 4: p = 10; break; /* SYNC_IN */
2801 default: return -1;
2802 }
2803 }
2804 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002805 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002806
2807 switch (hdspm->io_type) {
2808 case RayDAT:
2809 case AIO:
2810 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2811 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2812 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2813 break;
2814
2815 case MADI:
2816 case MADIface:
2817 case AES32:
2818 hdspm_write(hdspm, HDSPM_controlRegister,
2819 hdspm->control_register);
2820 }
2821
Takashi Iwai763f3562005-06-03 11:25:34 +02002822 return 0;
2823}
2824
Adrian Knoth0dca1792011-01-26 19:32:14 +01002825
Takashi Iwai98274f02005-11-17 14:52:34 +01002826static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002828{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002829 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002830
Adrian Knoth0dca1792011-01-26 19:32:14 +01002831 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2832 uinfo->count = 1;
2833 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002834
Adrian Knoth0dca1792011-01-26 19:32:14 +01002835 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2836 uinfo->value.enumerated.item =
2837 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002838
Adrian Knoth0dca1792011-01-26 19:32:14 +01002839 strcpy(uinfo->value.enumerated.name,
2840 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002841
Takashi Iwai763f3562005-06-03 11:25:34 +02002842 return 0;
2843}
2844
Takashi Iwai98274f02005-11-17 14:52:34 +01002845static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002847{
Takashi Iwai98274f02005-11-17 14:52:34 +01002848 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002849 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002850
Adrian Knoth0dca1792011-01-26 19:32:14 +01002851 if (psf >= 0) {
2852 ucontrol->value.enumerated.item[0] = psf;
2853 return 0;
2854 }
2855
2856 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002857}
2858
Takashi Iwai98274f02005-11-17 14:52:34 +01002859static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002861{
Takashi Iwai98274f02005-11-17 14:52:34 +01002862 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002863 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002864
2865 if (!snd_hdspm_use_is_exclusive(hdspm))
2866 return -EBUSY;
2867
Adrian Knoth0dca1792011-01-26 19:32:14 +01002868 val = ucontrol->value.enumerated.item[0];
2869
2870 if (val < 0)
2871 val = 0;
2872 else if (val >= hdspm->texts_autosync_items)
2873 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002874
2875 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002876 if (val != hdspm_pref_sync_ref(hdspm))
2877 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2878
Takashi Iwai763f3562005-06-03 11:25:34 +02002879 spin_unlock_irq(&hdspm->lock);
2880 return change;
2881}
2882
Adrian Knoth0dca1792011-01-26 19:32:14 +01002883
Takashi Iwai763f3562005-06-03 11:25:34 +02002884#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002885{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2886 .name = xname, \
2887 .index = xindex, \
2888 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2889 .info = snd_hdspm_info_autosync_ref, \
2890 .get = snd_hdspm_get_autosync_ref, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002891}
2892
Adrian Knoth0dca1792011-01-26 19:32:14 +01002893static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002894{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002895 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002896 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002897 unsigned int syncref =
2898 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002899 if (syncref == 0)
2900 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2901 if (syncref <= 8)
2902 return syncref;
2903 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002904 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002905 /* This looks at the autosync selected sync reference */
2906 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002907
Remy Bruno3cee5a62006-10-16 12:46:32 +02002908 switch (status2 & HDSPM_SelSyncRefMask) {
2909 case HDSPM_SelSyncRef_WORD:
2910 return HDSPM_AUTOSYNC_FROM_WORD;
2911 case HDSPM_SelSyncRef_MADI:
2912 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002913 case HDSPM_SelSyncRef_TCO:
2914 return HDSPM_AUTOSYNC_FROM_TCO;
2915 case HDSPM_SelSyncRef_SyncIn:
2916 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002917 case HDSPM_SelSyncRef_NVALID:
2918 return HDSPM_AUTOSYNC_FROM_NONE;
2919 default:
2920 return 0;
2921 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002922
Takashi Iwai763f3562005-06-03 11:25:34 +02002923 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002924 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002925}
2926
Adrian Knoth0dca1792011-01-26 19:32:14 +01002927
Takashi Iwai98274f02005-11-17 14:52:34 +01002928static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2929 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002930{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002931 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002932
Adrian Knoth0dca1792011-01-26 19:32:14 +01002933 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002934 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
2935 "AES4", "AES5", "AES6", "AES7", "AES8", "None"};
2936
2937 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2938 uinfo->count = 1;
2939 uinfo->value.enumerated.items = 10;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002940 if (uinfo->value.enumerated.item >=
2941 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002942 uinfo->value.enumerated.item =
2943 uinfo->value.enumerated.items - 1;
2944 strcpy(uinfo->value.enumerated.name,
2945 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002946 } else if (MADI == hdspm->io_type) {
2947 static char *texts[] = {"Word Clock", "MADI", "TCO",
2948 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002949
2950 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2951 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002952 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002953 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002954 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002955 uinfo->value.enumerated.item =
2956 uinfo->value.enumerated.items - 1;
2957 strcpy(uinfo->value.enumerated.name,
2958 texts[uinfo->value.enumerated.item]);
2959 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002960 return 0;
2961}
2962
Takashi Iwai98274f02005-11-17 14:52:34 +01002963static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002965{
Takashi Iwai98274f02005-11-17 14:52:34 +01002966 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002967
Remy Bruno65345992007-08-31 12:21:08 +02002968 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002969 return 0;
2970}
2971
Adrian Knothf99c7882013-03-10 00:37:26 +01002972
2973
2974#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
2975{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2976 .name = xname, \
2977 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
2978 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2979 .info = snd_hdspm_info_tco_video_input_format, \
2980 .get = snd_hdspm_get_tco_video_input_format, \
2981}
2982
2983static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_info *uinfo)
2985{
2986 static char *texts[] = {"No video", "NTSC", "PAL"};
2987 ENUMERATED_CTL_INFO(uinfo, texts);
2988 return 0;
2989}
2990
2991static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
2992 struct snd_ctl_elem_value *ucontrol)
2993{
2994 u32 status;
2995 int ret = 0;
2996
2997 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2998 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
2999 switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
3000 HDSPM_TCO1_Video_Input_Format_PAL)) {
3001 case HDSPM_TCO1_Video_Input_Format_NTSC:
3002 /* ntsc */
3003 ret = 1;
3004 break;
3005 case HDSPM_TCO1_Video_Input_Format_PAL:
3006 /* pal */
3007 ret = 2;
3008 break;
3009 default:
3010 /* no video */
3011 ret = 0;
3012 break;
3013 }
3014 ucontrol->value.enumerated.item[0] = ret;
3015 return 0;
3016}
3017
3018
3019
3020#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
3021{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3022 .name = xname, \
3023 .access = SNDRV_CTL_ELEM_ACCESS_READ |\
3024 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3025 .info = snd_hdspm_info_tco_ltc_frames, \
3026 .get = snd_hdspm_get_tco_ltc_frames, \
3027}
3028
3029static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_info *uinfo)
3031{
3032 static char *texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3033 "30 fps"};
3034 ENUMERATED_CTL_INFO(uinfo, texts);
3035 return 0;
3036}
3037
3038static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
3039{
3040 u32 status;
3041 int ret = 0;
3042
3043 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3044 if (status & HDSPM_TCO1_LTC_Input_valid) {
3045 switch (status & (HDSPM_TCO1_LTC_Format_LSB |
3046 HDSPM_TCO1_LTC_Format_MSB)) {
3047 case 0:
3048 /* 24 fps */
3049 ret = 1;
3050 break;
3051 case HDSPM_TCO1_LTC_Format_LSB:
3052 /* 25 fps */
3053 ret = 2;
3054 break;
3055 case HDSPM_TCO1_LTC_Format_MSB:
3056 /* 25 fps */
3057 ret = 3;
3058 break;
3059 default:
3060 /* 30 fps */
3061 ret = 4;
3062 break;
3063 }
3064 }
3065
3066 return ret;
3067}
3068
3069static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_value *ucontrol)
3071{
3072 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3073
3074 ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
3075 return 0;
3076}
3077
Adrian Knothbf0ff872012-12-03 14:55:49 +01003078#define HDSPM_TOGGLE_SETTING(xname, xindex) \
3079{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3080 .name = xname, \
3081 .private_value = xindex, \
3082 .info = snd_hdspm_info_toggle_setting, \
3083 .get = snd_hdspm_get_toggle_setting, \
3084 .put = snd_hdspm_put_toggle_setting \
3085}
3086
3087static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
3088{
3089 return (hdspm->control_register & regmask) ? 1 : 0;
3090}
3091
3092static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
3093{
3094 if (out)
3095 hdspm->control_register |= regmask;
3096 else
3097 hdspm->control_register &= ~regmask;
3098 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3099
3100 return 0;
3101}
3102
3103#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
3104
3105static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
3106 struct snd_ctl_elem_value *ucontrol)
3107{
3108 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3109 u32 regmask = kcontrol->private_value;
3110
3111 spin_lock_irq(&hdspm->lock);
3112 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
3113 spin_unlock_irq(&hdspm->lock);
3114 return 0;
3115}
3116
3117static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_value *ucontrol)
3119{
3120 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3121 u32 regmask = kcontrol->private_value;
3122 int change;
3123 unsigned int val;
3124
3125 if (!snd_hdspm_use_is_exclusive(hdspm))
3126 return -EBUSY;
3127 val = ucontrol->value.integer.value[0] & 1;
3128 spin_lock_irq(&hdspm->lock);
3129 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
3130 hdspm_set_toggle_setting(hdspm, regmask, val);
3131 spin_unlock_irq(&hdspm->lock);
3132 return change;
3133}
3134
Takashi Iwai763f3562005-06-03 11:25:34 +02003135#define HDSPM_INPUT_SELECT(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003136{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3137 .name = xname, \
3138 .index = xindex, \
3139 .info = snd_hdspm_info_input_select, \
3140 .get = snd_hdspm_get_input_select, \
3141 .put = snd_hdspm_put_input_select \
Takashi Iwai763f3562005-06-03 11:25:34 +02003142}
3143
Takashi Iwai98274f02005-11-17 14:52:34 +01003144static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003145{
3146 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3147}
3148
Takashi Iwai98274f02005-11-17 14:52:34 +01003149static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003150{
3151 if (out)
3152 hdspm->control_register |= HDSPM_InputSelect0;
3153 else
3154 hdspm->control_register &= ~HDSPM_InputSelect0;
3155 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3156
3157 return 0;
3158}
3159
Takashi Iwai98274f02005-11-17 14:52:34 +01003160static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3161 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003162{
3163 static char *texts[] = { "optical", "coaxial" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003164 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003165 return 0;
3166}
3167
Takashi Iwai98274f02005-11-17 14:52:34 +01003168static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003170{
Takashi Iwai98274f02005-11-17 14:52:34 +01003171 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003172
3173 spin_lock_irq(&hdspm->lock);
3174 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3175 spin_unlock_irq(&hdspm->lock);
3176 return 0;
3177}
3178
Takashi Iwai98274f02005-11-17 14:52:34 +01003179static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003181{
Takashi Iwai98274f02005-11-17 14:52:34 +01003182 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003183 int change;
3184 unsigned int val;
3185
3186 if (!snd_hdspm_use_is_exclusive(hdspm))
3187 return -EBUSY;
3188 val = ucontrol->value.integer.value[0] & 1;
3189 spin_lock_irq(&hdspm->lock);
3190 change = (int) val != hdspm_input_select(hdspm);
3191 hdspm_set_input_select(hdspm, val);
3192 spin_unlock_irq(&hdspm->lock);
3193 return change;
3194}
3195
Adrian Knoth0dca1792011-01-26 19:32:14 +01003196
Remy Bruno3cee5a62006-10-16 12:46:32 +02003197#define HDSPM_DS_WIRE(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_ds_wire, \
3202 .get = snd_hdspm_get_ds_wire, \
3203 .put = snd_hdspm_put_ds_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003204}
3205
3206static int hdspm_ds_wire(struct hdspm * hdspm)
3207{
3208 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3209}
3210
3211static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3212{
3213 if (ds)
3214 hdspm->control_register |= HDSPM_DS_DoubleWire;
3215 else
3216 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3217 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3218
3219 return 0;
3220}
3221
3222static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_info *uinfo)
3224{
3225 static char *texts[] = { "Single", "Double" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003226 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003227 return 0;
3228}
3229
3230static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3231 struct snd_ctl_elem_value *ucontrol)
3232{
3233 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3234
3235 spin_lock_irq(&hdspm->lock);
3236 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3237 spin_unlock_irq(&hdspm->lock);
3238 return 0;
3239}
3240
3241static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3242 struct snd_ctl_elem_value *ucontrol)
3243{
3244 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3245 int change;
3246 unsigned int val;
3247
3248 if (!snd_hdspm_use_is_exclusive(hdspm))
3249 return -EBUSY;
3250 val = ucontrol->value.integer.value[0] & 1;
3251 spin_lock_irq(&hdspm->lock);
3252 change = (int) val != hdspm_ds_wire(hdspm);
3253 hdspm_set_ds_wire(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_QS_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_qs_wire, \
3264 .get = snd_hdspm_get_qs_wire, \
3265 .put = snd_hdspm_put_qs_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003266}
3267
3268static int hdspm_qs_wire(struct hdspm * hdspm)
3269{
3270 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3271 return 1;
3272 if (hdspm->control_register & HDSPM_QS_QuadWire)
3273 return 2;
3274 return 0;
3275}
3276
3277static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3278{
3279 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3280 switch (mode) {
3281 case 0:
3282 break;
3283 case 1:
3284 hdspm->control_register |= HDSPM_QS_DoubleWire;
3285 break;
3286 case 2:
3287 hdspm->control_register |= HDSPM_QS_QuadWire;
3288 break;
3289 }
3290 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3291
3292 return 0;
3293}
3294
3295static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3296 struct snd_ctl_elem_info *uinfo)
3297{
3298 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003299 ENUMERATED_CTL_INFO(uinfo, texts);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003300 return 0;
3301}
3302
3303static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
3306 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3307
3308 spin_lock_irq(&hdspm->lock);
3309 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3310 spin_unlock_irq(&hdspm->lock);
3311 return 0;
3312}
3313
3314static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3315 struct snd_ctl_elem_value *ucontrol)
3316{
3317 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3318 int change;
3319 int val;
3320
3321 if (!snd_hdspm_use_is_exclusive(hdspm))
3322 return -EBUSY;
3323 val = ucontrol->value.integer.value[0];
3324 if (val < 0)
3325 val = 0;
3326 if (val > 2)
3327 val = 2;
3328 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003329 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003330 hdspm_set_qs_wire(hdspm, val);
3331 spin_unlock_irq(&hdspm->lock);
3332 return change;
3333}
3334
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003335#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3336{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3337 .name = xname, \
3338 .index = xindex, \
3339 .info = snd_hdspm_info_madi_speedmode, \
3340 .get = snd_hdspm_get_madi_speedmode, \
3341 .put = snd_hdspm_put_madi_speedmode \
3342}
3343
3344static int hdspm_madi_speedmode(struct hdspm *hdspm)
3345{
3346 if (hdspm->control_register & HDSPM_QuadSpeed)
3347 return 2;
3348 if (hdspm->control_register & HDSPM_DoubleSpeed)
3349 return 1;
3350 return 0;
3351}
3352
3353static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3354{
3355 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3356 switch (mode) {
3357 case 0:
3358 break;
3359 case 1:
3360 hdspm->control_register |= HDSPM_DoubleSpeed;
3361 break;
3362 case 2:
3363 hdspm->control_register |= HDSPM_QuadSpeed;
3364 break;
3365 }
3366 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3367
3368 return 0;
3369}
3370
3371static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3372 struct snd_ctl_elem_info *uinfo)
3373{
3374 static char *texts[] = { "Single", "Double", "Quad" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003375 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003376 return 0;
3377}
3378
3379static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3380 struct snd_ctl_elem_value *ucontrol)
3381{
3382 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3383
3384 spin_lock_irq(&hdspm->lock);
3385 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3386 spin_unlock_irq(&hdspm->lock);
3387 return 0;
3388}
3389
3390static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3391 struct snd_ctl_elem_value *ucontrol)
3392{
3393 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3394 int change;
3395 int val;
3396
3397 if (!snd_hdspm_use_is_exclusive(hdspm))
3398 return -EBUSY;
3399 val = ucontrol->value.integer.value[0];
3400 if (val < 0)
3401 val = 0;
3402 if (val > 2)
3403 val = 2;
3404 spin_lock_irq(&hdspm->lock);
3405 change = val != hdspm_madi_speedmode(hdspm);
3406 hdspm_set_madi_speedmode(hdspm, val);
3407 spin_unlock_irq(&hdspm->lock);
3408 return change;
3409}
Takashi Iwai763f3562005-06-03 11:25:34 +02003410
3411#define HDSPM_MIXER(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003412{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3413 .name = xname, \
3414 .index = xindex, \
3415 .device = 0, \
3416 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3417 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3418 .info = snd_hdspm_info_mixer, \
3419 .get = snd_hdspm_get_mixer, \
3420 .put = snd_hdspm_put_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003421}
3422
Takashi Iwai98274f02005-11-17 14:52:34 +01003423static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3424 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003425{
3426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3427 uinfo->count = 3;
3428 uinfo->value.integer.min = 0;
3429 uinfo->value.integer.max = 65535;
3430 uinfo->value.integer.step = 1;
3431 return 0;
3432}
3433
Takashi Iwai98274f02005-11-17 14:52:34 +01003434static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003436{
Takashi Iwai98274f02005-11-17 14:52:34 +01003437 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003438 int source;
3439 int destination;
3440
3441 source = ucontrol->value.integer.value[0];
3442 if (source < 0)
3443 source = 0;
3444 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3445 source = 2 * HDSPM_MAX_CHANNELS - 1;
3446
3447 destination = ucontrol->value.integer.value[1];
3448 if (destination < 0)
3449 destination = 0;
3450 else if (destination >= HDSPM_MAX_CHANNELS)
3451 destination = HDSPM_MAX_CHANNELS - 1;
3452
3453 spin_lock_irq(&hdspm->lock);
3454 if (source >= HDSPM_MAX_CHANNELS)
3455 ucontrol->value.integer.value[2] =
3456 hdspm_read_pb_gain(hdspm, destination,
3457 source - HDSPM_MAX_CHANNELS);
3458 else
3459 ucontrol->value.integer.value[2] =
3460 hdspm_read_in_gain(hdspm, destination, source);
3461
3462 spin_unlock_irq(&hdspm->lock);
3463
3464 return 0;
3465}
3466
Takashi Iwai98274f02005-11-17 14:52:34 +01003467static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003469{
Takashi Iwai98274f02005-11-17 14:52:34 +01003470 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003471 int change;
3472 int source;
3473 int destination;
3474 int gain;
3475
3476 if (!snd_hdspm_use_is_exclusive(hdspm))
3477 return -EBUSY;
3478
3479 source = ucontrol->value.integer.value[0];
3480 destination = ucontrol->value.integer.value[1];
3481
3482 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3483 return -1;
3484 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3485 return -1;
3486
3487 gain = ucontrol->value.integer.value[2];
3488
3489 spin_lock_irq(&hdspm->lock);
3490
3491 if (source >= HDSPM_MAX_CHANNELS)
3492 change = gain != hdspm_read_pb_gain(hdspm, destination,
3493 source -
3494 HDSPM_MAX_CHANNELS);
3495 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003496 change = gain != hdspm_read_in_gain(hdspm, destination,
3497 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003498
3499 if (change) {
3500 if (source >= HDSPM_MAX_CHANNELS)
3501 hdspm_write_pb_gain(hdspm, destination,
3502 source - HDSPM_MAX_CHANNELS,
3503 gain);
3504 else
3505 hdspm_write_in_gain(hdspm, destination, source,
3506 gain);
3507 }
3508 spin_unlock_irq(&hdspm->lock);
3509
3510 return change;
3511}
3512
3513/* The simple mixer control(s) provide gain control for the
3514 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003515 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003516*/
3517
3518#define HDSPM_PLAYBACK_MIXER \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003519{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3520 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3521 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3522 .info = snd_hdspm_info_playback_mixer, \
3523 .get = snd_hdspm_get_playback_mixer, \
3524 .put = snd_hdspm_put_playback_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003525}
3526
Takashi Iwai98274f02005-11-17 14:52:34 +01003527static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3528 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003529{
3530 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3531 uinfo->count = 1;
3532 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003533 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003534 uinfo->value.integer.step = 1;
3535 return 0;
3536}
3537
Takashi Iwai98274f02005-11-17 14:52:34 +01003538static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3539 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003540{
Takashi Iwai98274f02005-11-17 14:52:34 +01003541 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003542 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003543
3544 channel = ucontrol->id.index - 1;
3545
Takashi Iwaida3cec32008-08-08 17:12:14 +02003546 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3547 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003548
Takashi Iwai763f3562005-06-03 11:25:34 +02003549 spin_lock_irq(&hdspm->lock);
3550 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003551 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003552 spin_unlock_irq(&hdspm->lock);
3553
Takashi Iwai763f3562005-06-03 11:25:34 +02003554 return 0;
3555}
3556
Takashi Iwai98274f02005-11-17 14:52:34 +01003557static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3558 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003559{
Takashi Iwai98274f02005-11-17 14:52:34 +01003560 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003561 int change;
3562 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003563 int gain;
3564
3565 if (!snd_hdspm_use_is_exclusive(hdspm))
3566 return -EBUSY;
3567
3568 channel = ucontrol->id.index - 1;
3569
Takashi Iwaida3cec32008-08-08 17:12:14 +02003570 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3571 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003572
Adrian Knoth0dca1792011-01-26 19:32:14 +01003573 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003574
3575 spin_lock_irq(&hdspm->lock);
3576 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003577 gain != hdspm_read_pb_gain(hdspm, channel,
3578 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003579 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003580 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003581 gain);
3582 spin_unlock_irq(&hdspm->lock);
3583 return change;
3584}
3585
Adrian Knoth0dca1792011-01-26 19:32:14 +01003586#define HDSPM_SYNC_CHECK(xname, xindex) \
3587{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3588 .name = xname, \
3589 .private_value = xindex, \
3590 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3591 .info = snd_hdspm_info_sync_check, \
3592 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003593}
3594
Adrian Knoth34542212013-03-10 00:37:25 +01003595#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
3596{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3597 .name = xname, \
3598 .private_value = xindex, \
3599 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3600 .info = snd_hdspm_tco_info_lock_check, \
3601 .get = snd_hdspm_get_sync_check \
3602}
3603
3604
Adrian Knoth0dca1792011-01-26 19:32:14 +01003605
Takashi Iwai98274f02005-11-17 14:52:34 +01003606static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3607 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003608{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003609 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003610 ENUMERATED_CTL_INFO(uinfo, texts);
Takashi Iwai763f3562005-06-03 11:25:34 +02003611 return 0;
3612}
3613
Adrian Knoth34542212013-03-10 00:37:25 +01003614static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
3615 struct snd_ctl_elem_info *uinfo)
3616{
3617 static char *texts[] = { "No Lock", "Lock" };
3618 ENUMERATED_CTL_INFO(uinfo, texts);
3619 return 0;
3620}
3621
Adrian Knoth0dca1792011-01-26 19:32:14 +01003622static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003623{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003624 int status, status2;
3625
3626 switch (hdspm->io_type) {
3627 case AES32:
3628 status = hdspm_read(hdspm, HDSPM_statusRegister);
Andre Schramm56bde0f2013-01-09 14:40:18 +01003629 if (status & HDSPM_AES32_wcLock) {
3630 if (status & HDSPM_AES32_wcSync)
3631 return 2;
3632 else
3633 return 1;
3634 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02003635 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003636 break;
3637
3638 case MADI:
3639 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003640 if (status2 & HDSPM_wcLock) {
3641 if (status2 & HDSPM_wcSync)
3642 return 2;
3643 else
3644 return 1;
3645 }
3646 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003647 break;
3648
3649 case RayDAT:
3650 case AIO:
3651 status = hdspm_read(hdspm, HDSPM_statusRegister);
3652
3653 if (status & 0x2000000)
3654 return 2;
3655 else if (status & 0x1000000)
3656 return 1;
3657 return 0;
3658
3659 break;
3660
3661 case MADIface:
3662 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003663 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003664
Takashi Iwai763f3562005-06-03 11:25:34 +02003665
Adrian Knoth0dca1792011-01-26 19:32:14 +01003666 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003667}
3668
3669
Adrian Knoth0dca1792011-01-26 19:32:14 +01003670static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003671{
3672 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3673 if (status & HDSPM_madiLock) {
3674 if (status & HDSPM_madiSync)
3675 return 2;
3676 else
3677 return 1;
3678 }
3679 return 0;
3680}
3681
Adrian Knoth0dca1792011-01-26 19:32:14 +01003682
3683static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3684{
3685 int status, lock, sync;
3686
3687 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3688
3689 lock = (status & (0x1<<idx)) ? 1 : 0;
3690 sync = (status & (0x100<<idx)) ? 1 : 0;
3691
3692 if (lock && sync)
3693 return 2;
3694 else if (lock)
3695 return 1;
3696 return 0;
3697}
3698
3699
3700static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3701{
3702 int status, lock = 0, sync = 0;
3703
3704 switch (hdspm->io_type) {
3705 case RayDAT:
3706 case AIO:
3707 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3708 lock = (status & 0x400) ? 1 : 0;
3709 sync = (status & 0x800) ? 1 : 0;
3710 break;
3711
3712 case MADI:
Adrian Knoth2e0452f2012-10-19 17:42:27 +02003713 status = hdspm_read(hdspm, HDSPM_statusRegister);
3714 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3715 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3716 break;
3717
Adrian Knoth0dca1792011-01-26 19:32:14 +01003718 case AES32:
3719 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth9a215f42012-10-19 17:42:28 +02003720 lock = (status & 0x100000) ? 1 : 0;
3721 sync = (status & 0x200000) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003722 break;
3723
3724 case MADIface:
3725 break;
3726 }
3727
3728 if (lock && sync)
3729 return 2;
3730 else if (lock)
3731 return 1;
3732
3733 return 0;
3734}
3735
3736static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3737{
3738 int status2, lock, sync;
3739 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3740
3741 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3742 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3743
3744 if (sync)
3745 return 2;
3746 else if (lock)
3747 return 1;
3748 return 0;
3749}
3750
Adrian Knoth34542212013-03-10 00:37:25 +01003751static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
3752{
3753 u32 status;
3754 status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
3755
3756 return (status & mask) ? 1 : 0;
3757}
3758
Adrian Knoth0dca1792011-01-26 19:32:14 +01003759
3760static int hdspm_tco_sync_check(struct hdspm *hdspm)
3761{
3762 int status;
3763
3764 if (hdspm->tco) {
3765 switch (hdspm->io_type) {
3766 case MADI:
3767 case AES32:
3768 status = hdspm_read(hdspm, HDSPM_statusRegister);
3769 if (status & HDSPM_tcoLock) {
3770 if (status & HDSPM_tcoSync)
3771 return 2;
3772 else
3773 return 1;
3774 }
3775 return 0;
3776
3777 break;
3778
3779 case RayDAT:
3780 case AIO:
3781 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3782
3783 if (status & 0x8000000)
3784 return 2; /* Sync */
3785 if (status & 0x4000000)
3786 return 1; /* Lock */
3787 return 0; /* No signal */
3788 break;
3789
3790 default:
3791 break;
3792 }
3793 }
3794
3795 return 3; /* N/A */
3796}
3797
3798
3799static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3800 struct snd_ctl_elem_value *ucontrol)
3801{
3802 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3803 int val = -1;
3804
3805 switch (hdspm->io_type) {
3806 case RayDAT:
3807 switch (kcontrol->private_value) {
3808 case 0: /* WC */
3809 val = hdspm_wc_sync_check(hdspm); break;
3810 case 7: /* TCO */
3811 val = hdspm_tco_sync_check(hdspm); break;
3812 case 8: /* SYNC IN */
3813 val = hdspm_sync_in_sync_check(hdspm); break;
3814 default:
Adrian Knothd1a3c982012-11-07 18:00:09 +01003815 val = hdspm_s1_sync_check(hdspm,
3816 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003817 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003818 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003819
3820 case AIO:
3821 switch (kcontrol->private_value) {
3822 case 0: /* WC */
3823 val = hdspm_wc_sync_check(hdspm); break;
3824 case 4: /* TCO */
3825 val = hdspm_tco_sync_check(hdspm); break;
3826 case 5: /* SYNC IN */
3827 val = hdspm_sync_in_sync_check(hdspm); break;
3828 default:
3829 val = hdspm_s1_sync_check(hdspm, ucontrol->id.index-1);
3830 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003831 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003832
3833 case MADI:
3834 switch (kcontrol->private_value) {
3835 case 0: /* WC */
3836 val = hdspm_wc_sync_check(hdspm); break;
3837 case 1: /* MADI */
3838 val = hdspm_madi_sync_check(hdspm); break;
3839 case 2: /* TCO */
3840 val = hdspm_tco_sync_check(hdspm); break;
3841 case 3: /* SYNC_IN */
3842 val = hdspm_sync_in_sync_check(hdspm); break;
3843 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003844 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003845
3846 case MADIface:
3847 val = hdspm_madi_sync_check(hdspm); /* MADI */
3848 break;
3849
3850 case AES32:
3851 switch (kcontrol->private_value) {
3852 case 0: /* WC */
3853 val = hdspm_wc_sync_check(hdspm); break;
3854 case 9: /* TCO */
3855 val = hdspm_tco_sync_check(hdspm); break;
3856 case 10 /* SYNC IN */:
3857 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003858 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01003859 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003860 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003861 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003862 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003863
3864 }
3865
Adrian Knoth34542212013-03-10 00:37:25 +01003866 if (hdspm->tco) {
3867 switch (kcontrol->private_value) {
3868 case 11:
3869 /* Check TCO for lock state of its current input */
3870 val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
3871 break;
3872 case 12:
3873 /* Check TCO for valid time code on LTC input. */
3874 val = hdspm_tco_input_check(hdspm,
3875 HDSPM_TCO1_LTC_Input_valid);
3876 break;
3877 default:
3878 break;
3879 }
3880 }
3881
Adrian Knoth0dca1792011-01-26 19:32:14 +01003882 if (-1 == val)
3883 val = 3;
3884
3885 ucontrol->value.enumerated.item[0] = val;
3886 return 0;
3887}
3888
3889
3890
3891/**
3892 * TCO controls
3893 **/
3894static void hdspm_tco_write(struct hdspm *hdspm)
3895{
3896 unsigned int tc[4] = { 0, 0, 0, 0};
3897
3898 switch (hdspm->tco->input) {
3899 case 0:
3900 tc[2] |= HDSPM_TCO2_set_input_MSB;
3901 break;
3902 case 1:
3903 tc[2] |= HDSPM_TCO2_set_input_LSB;
3904 break;
3905 default:
3906 break;
3907 }
3908
3909 switch (hdspm->tco->framerate) {
3910 case 1:
3911 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
3912 break;
3913 case 2:
3914 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
3915 break;
3916 case 3:
3917 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
3918 HDSPM_TCO1_set_drop_frame_flag;
3919 break;
3920 case 4:
3921 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3922 HDSPM_TCO1_LTC_Format_MSB;
3923 break;
3924 case 5:
3925 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3926 HDSPM_TCO1_LTC_Format_MSB +
3927 HDSPM_TCO1_set_drop_frame_flag;
3928 break;
3929 default:
3930 break;
3931 }
3932
3933 switch (hdspm->tco->wordclock) {
3934 case 1:
3935 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
3936 break;
3937 case 2:
3938 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
3939 break;
3940 default:
3941 break;
3942 }
3943
3944 switch (hdspm->tco->samplerate) {
3945 case 1:
3946 tc[2] |= HDSPM_TCO2_set_freq;
3947 break;
3948 case 2:
3949 tc[2] |= HDSPM_TCO2_set_freq_from_app;
3950 break;
3951 default:
3952 break;
3953 }
3954
3955 switch (hdspm->tco->pull) {
3956 case 1:
3957 tc[2] |= HDSPM_TCO2_set_pull_up;
3958 break;
3959 case 2:
3960 tc[2] |= HDSPM_TCO2_set_pull_down;
3961 break;
3962 case 3:
3963 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
3964 break;
3965 case 4:
3966 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
3967 break;
3968 default:
3969 break;
3970 }
3971
3972 if (1 == hdspm->tco->term) {
3973 tc[2] |= HDSPM_TCO2_set_term_75R;
3974 }
3975
3976 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
3977 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
3978 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
3979 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
3980}
3981
3982
3983#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
3984{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3985 .name = xname, \
3986 .index = xindex, \
3987 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
3988 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3989 .info = snd_hdspm_info_tco_sample_rate, \
3990 .get = snd_hdspm_get_tco_sample_rate, \
3991 .put = snd_hdspm_put_tco_sample_rate \
3992}
3993
3994static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
3995 struct snd_ctl_elem_info *uinfo)
3996{
3997 static char *texts[] = { "44.1 kHz", "48 kHz" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01003998 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003999 return 0;
4000}
4001
4002static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4003 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02004004{
Takashi Iwai98274f02005-11-17 14:52:34 +01004005 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02004006
Adrian Knoth0dca1792011-01-26 19:32:14 +01004007 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4008
Takashi Iwai763f3562005-06-03 11:25:34 +02004009 return 0;
4010}
4011
Adrian Knoth0dca1792011-01-26 19:32:14 +01004012static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4013 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02004014{
Adrian Knoth0dca1792011-01-26 19:32:14 +01004015 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4016
4017 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4018 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4019
4020 hdspm_tco_write(hdspm);
4021
4022 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004023 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01004024
Remy Bruno3cee5a62006-10-16 12:46:32 +02004025 return 0;
4026}
4027
Adrian Knoth0dca1792011-01-26 19:32:14 +01004028
4029#define HDSPM_TCO_PULL(xname, xindex) \
4030{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4031 .name = xname, \
4032 .index = xindex, \
4033 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4034 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4035 .info = snd_hdspm_info_tco_pull, \
4036 .get = snd_hdspm_get_tco_pull, \
4037 .put = snd_hdspm_put_tco_pull \
4038}
4039
4040static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4041 struct snd_ctl_elem_info *uinfo)
4042{
4043 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004044 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004045 return 0;
4046}
4047
4048static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4049 struct snd_ctl_elem_value *ucontrol)
4050{
4051 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4052
4053 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4054
4055 return 0;
4056}
4057
4058static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4059 struct snd_ctl_elem_value *ucontrol)
4060{
4061 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4062
4063 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4064 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4065
4066 hdspm_tco_write(hdspm);
4067
4068 return 1;
4069 }
4070
4071 return 0;
4072}
4073
4074#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4075{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4076 .name = xname, \
4077 .index = xindex, \
4078 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4079 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4080 .info = snd_hdspm_info_tco_wck_conversion, \
4081 .get = snd_hdspm_get_tco_wck_conversion, \
4082 .put = snd_hdspm_put_tco_wck_conversion \
4083}
4084
4085static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4086 struct snd_ctl_elem_info *uinfo)
4087{
4088 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004089 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004090 return 0;
4091}
4092
4093static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4094 struct snd_ctl_elem_value *ucontrol)
4095{
4096 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4097
4098 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4099
4100 return 0;
4101}
4102
4103static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4104 struct snd_ctl_elem_value *ucontrol)
4105{
4106 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4107
4108 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4109 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4110
4111 hdspm_tco_write(hdspm);
4112
4113 return 1;
4114 }
4115
4116 return 0;
4117}
4118
4119
4120#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4121{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4122 .name = xname, \
4123 .index = xindex, \
4124 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4125 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4126 .info = snd_hdspm_info_tco_frame_rate, \
4127 .get = snd_hdspm_get_tco_frame_rate, \
4128 .put = snd_hdspm_put_tco_frame_rate \
4129}
4130
4131static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4132 struct snd_ctl_elem_info *uinfo)
4133{
4134 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4135 "29.97 dfps", "30 fps", "30 dfps" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004136 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004137 return 0;
4138}
4139
4140static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004141 struct snd_ctl_elem_value *ucontrol)
4142{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004143 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4144
Adrian Knoth0dca1792011-01-26 19:32:14 +01004145 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004146
Remy Bruno3cee5a62006-10-16 12:46:32 +02004147 return 0;
4148}
Takashi Iwai763f3562005-06-03 11:25:34 +02004149
Adrian Knoth0dca1792011-01-26 19:32:14 +01004150static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4151 struct snd_ctl_elem_value *ucontrol)
4152{
4153 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4154
4155 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4156 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4157
4158 hdspm_tco_write(hdspm);
4159
4160 return 1;
4161 }
4162
4163 return 0;
4164}
4165
4166
4167#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4168{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4169 .name = xname, \
4170 .index = xindex, \
4171 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4172 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4173 .info = snd_hdspm_info_tco_sync_source, \
4174 .get = snd_hdspm_get_tco_sync_source, \
4175 .put = snd_hdspm_put_tco_sync_source \
4176}
4177
4178static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4179 struct snd_ctl_elem_info *uinfo)
4180{
4181 static char *texts[] = { "LTC", "Video", "WCK" };
Adrian Knothe5b7b1f2013-03-10 00:37:24 +01004182 ENUMERATED_CTL_INFO(uinfo, texts);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004183 return 0;
4184}
4185
4186static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4187 struct snd_ctl_elem_value *ucontrol)
4188{
4189 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4190
4191 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4192
4193 return 0;
4194}
4195
4196static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4197 struct snd_ctl_elem_value *ucontrol)
4198{
4199 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4200
4201 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4202 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4203
4204 hdspm_tco_write(hdspm);
4205
4206 return 1;
4207 }
4208
4209 return 0;
4210}
4211
4212
4213#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4214{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4215 .name = xname, \
4216 .index = xindex, \
4217 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4218 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4219 .info = snd_hdspm_info_tco_word_term, \
4220 .get = snd_hdspm_get_tco_word_term, \
4221 .put = snd_hdspm_put_tco_word_term \
4222}
4223
4224static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4225 struct snd_ctl_elem_info *uinfo)
4226{
4227 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4228 uinfo->count = 1;
4229 uinfo->value.integer.min = 0;
4230 uinfo->value.integer.max = 1;
4231
4232 return 0;
4233}
4234
4235
4236static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4237 struct snd_ctl_elem_value *ucontrol)
4238{
4239 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4240
4241 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4242
4243 return 0;
4244}
4245
4246
4247static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4248 struct snd_ctl_elem_value *ucontrol)
4249{
4250 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4251
4252 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4253 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4254
4255 hdspm_tco_write(hdspm);
4256
4257 return 1;
4258 }
4259
4260 return 0;
4261}
4262
4263
4264
Takashi Iwai763f3562005-06-03 11:25:34 +02004265
Remy Bruno3cee5a62006-10-16 12:46:32 +02004266static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004267 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004268 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004269 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4270 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4271 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4272 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knothb8812c52012-10-19 17:42:26 +02004273 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004274 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4275 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
Adrian Knoth930f4ff2012-10-19 17:42:29 +02004276 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004277 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Adrian Knothc9e16682012-12-03 14:55:50 +01004278 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4279 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
Adrian Knoth696be0f2013-03-10 00:37:23 +01004280 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
Adrian Knothc9e16682012-12-03 14:55:50 +01004281 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4282 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004283 HDSPM_INPUT_SELECT("Input Select", 0),
4284 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004285};
4286
4287
4288static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4289 HDSPM_MIXER("Mixer", 0),
4290 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4291 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4292 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4293 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4294 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
Adrian Knothc9e16682012-12-03 14:55:50 +01004295 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4296 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4297 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004298 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004299};
4300
Adrian Knoth0dca1792011-01-26 19:32:14 +01004301static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004302 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004303 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004304 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4305 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4306 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4307 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004308 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004309 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4310 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4311 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4312 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4313 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4314 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4315 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4316 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4317 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4318 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4319 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
4320 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5)
4321
4322 /*
4323 HDSPM_INPUT_SELECT("Input Select", 0),
4324 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4325 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4326 HDSPM_SPDIF_IN("SPDIF In", 0);
4327 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4328 HDSPM_INPUT_LEVEL("Input Level", 0);
4329 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4330 HDSPM_PHONES("Phones", 0);
4331 */
4332};
4333
4334static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4335 HDSPM_MIXER("Mixer", 0),
4336 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4337 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4338 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4339 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4340 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4341 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4342 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4343 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4344 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4345 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4346 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4347 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4348 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4349 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4350 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4351 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4352 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4353 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4354 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4355 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4356 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
4357 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8)
4358};
4359
4360static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4361 HDSPM_MIXER("Mixer", 0),
4362 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4363 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4364 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4365 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4366 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4367 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4368 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4369 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4370 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4371 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4372 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4373 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4374 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4375 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4376 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4377 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4378 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4379 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4380 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4381 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4382 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4383 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4384 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4385 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4386 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4387 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4388 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4389 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Adrian Knothc9e16682012-12-03 14:55:50 +01004390 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4391 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4392 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4393 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4394 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004395 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4396 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4397};
4398
Adrian Knoth0dca1792011-01-26 19:32:14 +01004399
4400
4401/* Control elements for the optional TCO module */
4402static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4403 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4404 HDSPM_TCO_PULL("TCO Pull", 0),
4405 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4406 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4407 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
Adrian Knotha8176502013-03-10 00:37:27 +01004408 HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
4409 HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
4410 HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
4411 HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
4412 HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004413};
4414
4415
Takashi Iwai98274f02005-11-17 14:52:34 +01004416static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004417
4418
Takashi Iwai98274f02005-11-17 14:52:34 +01004419static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004420{
4421 int i;
4422
Adrian Knoth0dca1792011-01-26 19:32:14 +01004423 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004424 if (hdspm->system_sample_rate > 48000) {
4425 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004426 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4427 SNDRV_CTL_ELEM_ACCESS_READ |
4428 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004429 } else {
4430 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004431 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4432 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004433 }
4434 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004435 SNDRV_CTL_EVENT_MASK_INFO,
4436 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004437 }
4438
4439 return 0;
4440}
4441
4442
Adrian Knoth0dca1792011-01-26 19:32:14 +01004443static int snd_hdspm_create_controls(struct snd_card *card,
4444 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004445{
4446 unsigned int idx, limit;
4447 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004448 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004449 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004450
Adrian Knoth0dca1792011-01-26 19:32:14 +01004451 switch (hdspm->io_type) {
4452 case MADI:
4453 list = snd_hdspm_controls_madi;
4454 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4455 break;
4456 case MADIface:
4457 list = snd_hdspm_controls_madiface;
4458 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4459 break;
4460 case AIO:
4461 list = snd_hdspm_controls_aio;
4462 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4463 break;
4464 case RayDAT:
4465 list = snd_hdspm_controls_raydat;
4466 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4467 break;
4468 case AES32:
4469 list = snd_hdspm_controls_aes32;
4470 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4471 break;
4472 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004473
Adrian Knoth0dca1792011-01-26 19:32:14 +01004474 if (NULL != list) {
4475 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004476 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004477 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004478 if (err < 0)
4479 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004480 }
4481 }
4482
Takashi Iwai763f3562005-06-03 11:25:34 +02004483
Adrian Knoth0dca1792011-01-26 19:32:14 +01004484 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004485 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004486 if (hdspm->system_sample_rate >= 128000) {
4487 limit = hdspm->qs_out_channels;
4488 } else if (hdspm->system_sample_rate >= 64000) {
4489 limit = hdspm->ds_out_channels;
4490 } else {
4491 limit = hdspm->ss_out_channels;
4492 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004493 for (idx = 0; idx < limit; ++idx) {
4494 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004495 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4496 err = snd_ctl_add(card, kctl);
4497 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004498 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004499 hdspm->playback_mixer_ctls[idx] = kctl;
4500 }
4501
Adrian Knoth0dca1792011-01-26 19:32:14 +01004502
4503 if (hdspm->tco) {
4504 /* add tco control elements */
4505 list = snd_hdspm_controls_tco;
4506 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4507 for (idx = 0; idx < limit; idx++) {
4508 err = snd_ctl_add(card,
4509 snd_ctl_new1(&list[idx], hdspm));
4510 if (err < 0)
4511 return err;
4512 }
4513 }
4514
Takashi Iwai763f3562005-06-03 11:25:34 +02004515 return 0;
4516}
4517
4518/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004519 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004520 ------------------------------------------------------------*/
4521
4522static void
Remy Bruno3cee5a62006-10-16 12:46:32 +02004523snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
4524 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004525{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004526 struct hdspm *hdspm = entry->private_data;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004527 unsigned int status, status2, control, freq;
4528
Takashi Iwai763f3562005-06-03 11:25:34 +02004529 char *pref_sync_ref;
4530 char *autosync_ref;
4531 char *system_clock_mode;
Takashi Iwai763f3562005-06-03 11:25:34 +02004532 char *insel;
Takashi Iwai763f3562005-06-03 11:25:34 +02004533 int x, x2;
4534
Adrian Knoth0dca1792011-01-26 19:32:14 +01004535 /* TCO stuff */
4536 int a, ltc, frames, seconds, minutes, hours;
4537 unsigned int period;
4538 u64 freq_const = 0;
4539 u32 rate;
4540
Takashi Iwai763f3562005-06-03 11:25:34 +02004541 status = hdspm_read(hdspm, HDSPM_statusRegister);
4542 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004543 control = hdspm->control_register;
4544 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
Takashi Iwai763f3562005-06-03 11:25:34 +02004545
4546 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004547 hdspm->card_name, hdspm->card->number + 1,
4548 hdspm->firmware_rev,
4549 (status2 & HDSPM_version0) |
4550 (status2 & HDSPM_version1) | (status2 &
4551 HDSPM_version2));
4552
4553 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4554 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
Adrian Knoth7d53a632012-01-04 14:31:16 +01004555 hdspm->serial);
Takashi Iwai763f3562005-06-03 11:25:34 +02004556
4557 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004558 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
Takashi Iwai763f3562005-06-03 11:25:34 +02004559
4560 snd_iprintf(buffer, "--- System ---\n");
4561
4562 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004563 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4564 status & HDSPM_audioIRQPending,
4565 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4566 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4567 hdspm->irq_count);
Takashi Iwai763f3562005-06-03 11:25:34 +02004568 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004569 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4570 "estimated= %ld (bytes)\n",
4571 ((status & HDSPM_BufferID) ? 1 : 0),
4572 (status & HDSPM_BufferPositionMask),
4573 (status & HDSPM_BufferPositionMask) %
4574 (2 * (int)hdspm->period_bytes),
4575 ((status & HDSPM_BufferPositionMask) - 64) %
4576 (2 * (int)hdspm->period_bytes),
4577 (long) hdspm_hw_pointer(hdspm) * 4);
Takashi Iwai763f3562005-06-03 11:25:34 +02004578
4579 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004580 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4581 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4582 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4583 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4584 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
Takashi Iwai763f3562005-06-03 11:25:34 +02004585 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004586 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4587 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4588 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4589 snd_iprintf(buffer,
4590 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4591 "status2=0x%x\n",
4592 hdspm->control_register, hdspm->control2_register,
4593 status, status2);
4594 if (status & HDSPM_tco_detect) {
4595 snd_iprintf(buffer, "TCO module detected.\n");
4596 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4597 if (a & HDSPM_TCO1_LTC_Input_valid) {
4598 snd_iprintf(buffer, " LTC valid, ");
4599 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4600 HDSPM_TCO1_LTC_Format_MSB)) {
4601 case 0:
4602 snd_iprintf(buffer, "24 fps, ");
4603 break;
4604 case HDSPM_TCO1_LTC_Format_LSB:
4605 snd_iprintf(buffer, "25 fps, ");
4606 break;
4607 case HDSPM_TCO1_LTC_Format_MSB:
4608 snd_iprintf(buffer, "29.97 fps, ");
4609 break;
4610 default:
4611 snd_iprintf(buffer, "30 fps, ");
4612 break;
4613 }
4614 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4615 snd_iprintf(buffer, "drop frame\n");
4616 } else {
4617 snd_iprintf(buffer, "full frame\n");
4618 }
4619 } else {
4620 snd_iprintf(buffer, " no LTC\n");
4621 }
4622 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4623 snd_iprintf(buffer, " Video: NTSC\n");
4624 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4625 snd_iprintf(buffer, " Video: PAL\n");
4626 } else {
4627 snd_iprintf(buffer, " No video\n");
4628 }
4629 if (a & HDSPM_TCO1_TCO_lock) {
4630 snd_iprintf(buffer, " Sync: lock\n");
4631 } else {
4632 snd_iprintf(buffer, " Sync: no lock\n");
4633 }
4634
4635 switch (hdspm->io_type) {
4636 case MADI:
4637 case AES32:
4638 freq_const = 110069313433624ULL;
4639 break;
4640 case RayDAT:
4641 case AIO:
4642 freq_const = 104857600000000ULL;
4643 break;
4644 case MADIface:
4645 break; /* no TCO possible */
4646 }
4647
4648 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4649 snd_iprintf(buffer, " period: %u\n", period);
4650
4651
4652 /* rate = freq_const/period; */
4653 rate = div_u64(freq_const, period);
4654
4655 if (control & HDSPM_QuadSpeed) {
4656 rate *= 4;
4657 } else if (control & HDSPM_DoubleSpeed) {
4658 rate *= 2;
4659 }
4660
4661 snd_iprintf(buffer, " Frequency: %u Hz\n",
4662 (unsigned int) rate);
4663
4664 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4665 frames = ltc & 0xF;
4666 ltc >>= 4;
4667 frames += (ltc & 0x3) * 10;
4668 ltc >>= 4;
4669 seconds = ltc & 0xF;
4670 ltc >>= 4;
4671 seconds += (ltc & 0x7) * 10;
4672 ltc >>= 4;
4673 minutes = ltc & 0xF;
4674 ltc >>= 4;
4675 minutes += (ltc & 0x7) * 10;
4676 ltc >>= 4;
4677 hours = ltc & 0xF;
4678 ltc >>= 4;
4679 hours += (ltc & 0x3) * 10;
4680 snd_iprintf(buffer,
4681 " LTC In: %02d:%02d:%02d:%02d\n",
4682 hours, minutes, seconds, frames);
4683
4684 } else {
4685 snd_iprintf(buffer, "No TCO module detected.\n");
4686 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004687
4688 snd_iprintf(buffer, "--- Settings ---\n");
4689
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004690 x = hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02004691
4692 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004693 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4694 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004695
Adrian Knoth0dca1792011-01-26 19:32:14 +01004696 snd_iprintf(buffer, "Line out: %s\n",
4697 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004698
4699 switch (hdspm->control_register & HDSPM_InputMask) {
4700 case HDSPM_InputOptical:
4701 insel = "Optical";
4702 break;
4703 case HDSPM_InputCoaxial:
4704 insel = "Coaxial";
4705 break;
4706 default:
Masanari Iidaec8f53f2012-11-02 00:28:50 +09004707 insel = "Unknown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004708 }
4709
Takashi Iwai763f3562005-06-03 11:25:34 +02004710 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004711 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4712 "Auto Input %s\n",
4713 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4714 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4715 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004716
Adrian Knoth0dca1792011-01-26 19:32:14 +01004717
Remy Bruno3cee5a62006-10-16 12:46:32 +02004718 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004719 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004720 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004721 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004722 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004723
4724 switch (hdspm_pref_sync_ref(hdspm)) {
4725 case HDSPM_SYNC_FROM_WORD:
4726 pref_sync_ref = "Word Clock";
4727 break;
4728 case HDSPM_SYNC_FROM_MADI:
4729 pref_sync_ref = "MADI Sync";
4730 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004731 case HDSPM_SYNC_FROM_TCO:
4732 pref_sync_ref = "TCO";
4733 break;
4734 case HDSPM_SYNC_FROM_SYNC_IN:
4735 pref_sync_ref = "Sync In";
4736 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004737 default:
4738 pref_sync_ref = "XXXX Clock";
4739 break;
4740 }
4741 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004742 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004743
4744 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004745 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004746
4747
4748 snd_iprintf(buffer, "--- Status:\n");
4749
4750 x = status & HDSPM_madiSync;
4751 x2 = status2 & HDSPM_wcSync;
4752
4753 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004754 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4755 "NoLock",
4756 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4757 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004758
4759 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004760 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4761 autosync_ref = "Sync In";
4762 break;
4763 case HDSPM_AUTOSYNC_FROM_TCO:
4764 autosync_ref = "TCO";
4765 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004766 case HDSPM_AUTOSYNC_FROM_WORD:
4767 autosync_ref = "Word Clock";
4768 break;
4769 case HDSPM_AUTOSYNC_FROM_MADI:
4770 autosync_ref = "MADI Sync";
4771 break;
4772 case HDSPM_AUTOSYNC_FROM_NONE:
4773 autosync_ref = "Input not valid";
4774 break;
4775 default:
4776 autosync_ref = "---";
4777 break;
4778 }
4779 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004780 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4781 autosync_ref, hdspm_external_sample_rate(hdspm),
4782 (status & HDSPM_madiFreqMask) >> 22,
4783 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004784
4785 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004786 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4787 (status & HDSPM_RX_64ch) ? "64 channels" :
4788 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004789
4790 snd_iprintf(buffer, "\n");
4791}
4792
Remy Bruno3cee5a62006-10-16 12:46:32 +02004793static void
4794snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4795 struct snd_info_buffer *buffer)
4796{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004797 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004798 unsigned int status;
4799 unsigned int status2;
4800 unsigned int timecode;
Andre Schramm56bde0f2013-01-09 14:40:18 +01004801 unsigned int wcLock, wcSync;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004802 int pref_syncref;
4803 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004804 int x;
4805
4806 status = hdspm_read(hdspm, HDSPM_statusRegister);
4807 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4808 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4809
4810 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4811 hdspm->card_name, hdspm->card->number + 1,
4812 hdspm->firmware_rev);
4813
4814 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4815 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4816
4817 snd_iprintf(buffer, "--- System ---\n");
4818
4819 snd_iprintf(buffer,
4820 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4821 status & HDSPM_audioIRQPending,
4822 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4823 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4824 hdspm->irq_count);
4825 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004826 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4827 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004828 ((status & HDSPM_BufferID) ? 1 : 0),
4829 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004830 (status & HDSPM_BufferPositionMask) %
4831 (2 * (int)hdspm->period_bytes),
4832 ((status & HDSPM_BufferPositionMask) - 64) %
4833 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004834 (long) hdspm_hw_pointer(hdspm) * 4);
4835
4836 snd_iprintf(buffer,
4837 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4838 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4839 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4840 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4841 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4842 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004843 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4844 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4845 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4846 snd_iprintf(buffer,
4847 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4848 "status2=0x%x\n",
4849 hdspm->control_register, hdspm->control2_register,
4850 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004851
4852 snd_iprintf(buffer, "--- Settings ---\n");
4853
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004854 x = hdspm_get_latency(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004855
4856 snd_iprintf(buffer,
4857 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4858 x, (unsigned long) hdspm->period_bytes);
4859
Adrian Knoth0dca1792011-01-26 19:32:14 +01004860 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004861 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01004862 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02004863
4864 snd_iprintf(buffer,
4865 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
4866 (hdspm->
4867 control_register & HDSPM_clr_tms) ? "on" : "off",
4868 (hdspm->
4869 control_register & HDSPM_Emphasis) ? "on" : "off",
4870 (hdspm->
4871 control_register & HDSPM_Dolby) ? "on" : "off");
4872
Remy Bruno3cee5a62006-10-16 12:46:32 +02004873
4874 pref_syncref = hdspm_pref_sync_ref(hdspm);
4875 if (pref_syncref == 0)
4876 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
4877 else
4878 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
4879 pref_syncref);
4880
4881 snd_iprintf(buffer, "System Clock Frequency: %d\n",
4882 hdspm->system_sample_rate);
4883
4884 snd_iprintf(buffer, "Double speed: %s\n",
4885 hdspm->control_register & HDSPM_DS_DoubleWire?
4886 "Double wire" : "Single wire");
4887 snd_iprintf(buffer, "Quad speed: %s\n",
4888 hdspm->control_register & HDSPM_QS_DoubleWire?
4889 "Double wire" :
4890 hdspm->control_register & HDSPM_QS_QuadWire?
4891 "Quad wire" : "Single wire");
4892
4893 snd_iprintf(buffer, "--- Status:\n");
4894
Andre Schramm56bde0f2013-01-09 14:40:18 +01004895 wcLock = status & HDSPM_AES32_wcLock;
4896 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
4897
Remy Bruno3cee5a62006-10-16 12:46:32 +02004898 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Andre Schramm56bde0f2013-01-09 14:40:18 +01004899 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004900 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004901
4902 for (x = 0; x < 8; x++) {
4903 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004904 x+1,
4905 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01004906 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004907 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004908 }
4909
4910 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004911 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
4912 autosync_ref = "None"; break;
4913 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
4914 autosync_ref = "Word Clock"; break;
4915 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
4916 autosync_ref = "AES1"; break;
4917 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
4918 autosync_ref = "AES2"; break;
4919 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
4920 autosync_ref = "AES3"; break;
4921 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
4922 autosync_ref = "AES4"; break;
4923 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
4924 autosync_ref = "AES5"; break;
4925 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
4926 autosync_ref = "AES6"; break;
4927 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
4928 autosync_ref = "AES7"; break;
4929 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
4930 autosync_ref = "AES8"; break;
4931 default:
4932 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004933 }
4934 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
4935
4936 snd_iprintf(buffer, "\n");
4937}
4938
Adrian Knoth0dca1792011-01-26 19:32:14 +01004939static void
4940snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
4941 struct snd_info_buffer *buffer)
4942{
4943 struct hdspm *hdspm = entry->private_data;
4944 unsigned int status1, status2, status3, control, i;
4945 unsigned int lock, sync;
4946
4947 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
4948 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
4949 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
4950
4951 control = hdspm->control_register;
4952
4953 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
4954 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
4955 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
4956
4957
4958 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
4959
4960 snd_iprintf(buffer, "Clock mode : %s\n",
4961 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
4962 snd_iprintf(buffer, "System frequency: %d Hz\n",
4963 hdspm_get_system_sample_rate(hdspm));
4964
4965 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
4966
4967 lock = 0x1;
4968 sync = 0x100;
4969
4970 for (i = 0; i < 8; i++) {
4971 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
4972 i,
4973 (status1 & lock) ? 1 : 0,
4974 (status1 & sync) ? 1 : 0,
4975 texts_freq[(status2 >> (i * 4)) & 0xF]);
4976
4977 lock = lock<<1;
4978 sync = sync<<1;
4979 }
4980
4981 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
4982 (status1 & 0x1000000) ? 1 : 0,
4983 (status1 & 0x2000000) ? 1 : 0,
4984 texts_freq[(status1 >> 16) & 0xF]);
4985
4986 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
4987 (status1 & 0x4000000) ? 1 : 0,
4988 (status1 & 0x8000000) ? 1 : 0,
4989 texts_freq[(status1 >> 20) & 0xF]);
4990
4991 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
4992 (status3 & 0x400) ? 1 : 0,
4993 (status3 & 0x800) ? 1 : 0,
4994 texts_freq[(status2 >> 12) & 0xF]);
4995
4996}
4997
Remy Bruno3cee5a62006-10-16 12:46:32 +02004998#ifdef CONFIG_SND_DEBUG
4999static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01005000snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005001 struct snd_info_buffer *buffer)
5002{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005003 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005004
5005 int j,i;
5006
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005007 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02005008 snd_iprintf(buffer, "0x%08X: ", i);
5009 for (j = 0; j < 16; j += 4)
5010 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5011 snd_iprintf(buffer, "\n");
5012 }
5013}
5014#endif
5015
5016
Adrian Knoth0dca1792011-01-26 19:32:14 +01005017static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5018 struct snd_info_buffer *buffer)
5019{
5020 struct hdspm *hdspm = entry->private_data;
5021 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005022
Adrian Knoth0dca1792011-01-26 19:32:14 +01005023 snd_iprintf(buffer, "# generated by hdspm\n");
5024
5025 for (i = 0; i < hdspm->max_channels_in; i++) {
5026 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5027 }
5028}
5029
5030static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5031 struct snd_info_buffer *buffer)
5032{
5033 struct hdspm *hdspm = entry->private_data;
5034 int i;
5035
5036 snd_iprintf(buffer, "# generated by hdspm\n");
5037
5038 for (i = 0; i < hdspm->max_channels_out; i++) {
5039 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5040 }
5041}
5042
5043
Bill Pembertone23e7a12012-12-06 12:35:10 -05005044static void snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005045{
Takashi Iwai98274f02005-11-17 14:52:34 +01005046 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02005047
Adrian Knoth0dca1792011-01-26 19:32:14 +01005048 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5049 switch (hdspm->io_type) {
5050 case AES32:
5051 snd_info_set_text_ops(entry, hdspm,
5052 snd_hdspm_proc_read_aes32);
5053 break;
5054 case MADI:
5055 snd_info_set_text_ops(entry, hdspm,
5056 snd_hdspm_proc_read_madi);
5057 break;
5058 case MADIface:
5059 /* snd_info_set_text_ops(entry, hdspm,
5060 snd_hdspm_proc_read_madiface); */
5061 break;
5062 case RayDAT:
5063 snd_info_set_text_ops(entry, hdspm,
5064 snd_hdspm_proc_read_raydat);
5065 break;
5066 case AIO:
5067 break;
5068 }
5069 }
5070
5071 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5072 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5073 }
5074
5075 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5076 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5077 }
5078
Remy Bruno3cee5a62006-10-16 12:46:32 +02005079#ifdef CONFIG_SND_DEBUG
5080 /* debug file to read all hdspm registers */
5081 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5082 snd_info_set_text_ops(entry, hdspm,
5083 snd_hdspm_proc_read_debug);
5084#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02005085}
5086
5087/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005088 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005089 ------------------------------------------------------------*/
5090
Takashi Iwai98274f02005-11-17 14:52:34 +01005091static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005092{
Takashi Iwai763f3562005-06-03 11:25:34 +02005093 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005094 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005095 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005096
5097 /* set defaults: */
5098
Adrian Knoth0dca1792011-01-26 19:32:14 +01005099 hdspm->settings_register = 0;
5100
5101 switch (hdspm->io_type) {
5102 case MADI:
5103 case MADIface:
5104 hdspm->control_register =
5105 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5106 break;
5107
5108 case RayDAT:
5109 case AIO:
5110 hdspm->settings_register = 0x1 + 0x1000;
5111 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5112 * line_out */
5113 hdspm->control_register =
5114 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5115 break;
5116
5117 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005118 hdspm->control_register =
5119 HDSPM_ClockModeMaster | /* Master Cloack Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005120 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005121 HDSPM_SyncRef0 | /* AES1 is syncclock */
5122 HDSPM_LineOut | /* Analog output in */
5123 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005124 break;
5125 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005126
5127 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5128
Adrian Knoth0dca1792011-01-26 19:32:14 +01005129 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005130 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005131#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005132 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005133#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005134 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005135#endif
5136
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005137 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5138 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005139 hdspm_compute_period_size(hdspm);
5140
5141 /* silence everything */
5142
5143 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5144
Adrian Knoth0dca1792011-01-26 19:32:14 +01005145 if (hdspm->io_type == AIO || hdspm->io_type == RayDAT) {
5146 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005147 }
5148
5149 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005150 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005151
5152 return 0;
5153}
5154
5155
5156/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005157 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005158 ------------------------------------------------------------*/
5159
David Howells7d12e782006-10-05 14:55:46 +01005160static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005161{
Takashi Iwai98274f02005-11-17 14:52:34 +01005162 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005163 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005164 int i, audio, midi, schedule = 0;
5165 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005166
5167 status = hdspm_read(hdspm, HDSPM_statusRegister);
5168
5169 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005170 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5171 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005172
Adrian Knoth0dca1792011-01-26 19:32:14 +01005173 /* now = get_cycles(); */
5174 /**
5175 * LAT_2..LAT_0 period counter (win) counter (mac)
5176 * 6 4096 ~256053425 ~514672358
5177 * 5 2048 ~128024983 ~257373821
5178 * 4 1024 ~64023706 ~128718089
5179 * 3 512 ~32005945 ~64385999
5180 * 2 256 ~16003039 ~32260176
5181 * 1 128 ~7998738 ~16194507
5182 * 0 64 ~3998231 ~8191558
5183 **/
5184 /*
5185 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5186 now-hdspm->last_interrupt, status & 0xFFC0);
5187 hdspm->last_interrupt = now;
5188 */
5189
5190 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005191 return IRQ_NONE;
5192
5193 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5194 hdspm->irq_count++;
5195
Takashi Iwai763f3562005-06-03 11:25:34 +02005196
5197 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005198 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005199 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005200
5201 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005202 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005203 }
5204
Adrian Knoth0dca1792011-01-26 19:32:14 +01005205 if (midi) {
5206 i = 0;
5207 while (i < hdspm->midiPorts) {
5208 if ((hdspm_read(hdspm,
5209 hdspm->midi[i].statusIn) & 0xff) &&
5210 (status & hdspm->midi[i].irq)) {
5211 /* we disable interrupts for this input until
5212 * processing is done
5213 */
5214 hdspm->control_register &= ~hdspm->midi[i].ie;
5215 hdspm_write(hdspm, HDSPM_controlRegister,
5216 hdspm->control_register);
5217 hdspm->midi[i].pending = 1;
5218 schedule = 1;
5219 }
5220
5221 i++;
5222 }
5223
5224 if (schedule)
5225 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005226 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005227
Takashi Iwai763f3562005-06-03 11:25:34 +02005228 return IRQ_HANDLED;
5229}
5230
5231/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005232 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005233 ------------------------------------------------------------*/
5234
5235
Adrian Knoth0dca1792011-01-26 19:32:14 +01005236static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5237 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005238{
Takashi Iwai98274f02005-11-17 14:52:34 +01005239 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005240 return hdspm_hw_pointer(hdspm);
5241}
5242
Takashi Iwai763f3562005-06-03 11:25:34 +02005243
Takashi Iwai98274f02005-11-17 14:52:34 +01005244static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005245{
Takashi Iwai98274f02005-11-17 14:52:34 +01005246 struct snd_pcm_runtime *runtime = substream->runtime;
5247 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5248 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005249
5250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5251 other = hdspm->capture_substream;
5252 else
5253 other = hdspm->playback_substream;
5254
5255 if (hdspm->running)
5256 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5257 else
5258 runtime->status->hw_ptr = 0;
5259 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005260 struct snd_pcm_substream *s;
5261 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005262 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005263 if (s == other) {
5264 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005265 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005266 break;
5267 }
5268 }
5269 }
5270 return 0;
5271}
5272
Takashi Iwai98274f02005-11-17 14:52:34 +01005273static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5274 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005275{
Takashi Iwai98274f02005-11-17 14:52:34 +01005276 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005277 int err;
5278 int i;
5279 pid_t this_pid;
5280 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005281
5282 spin_lock_irq(&hdspm->lock);
5283
5284 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5285 this_pid = hdspm->playback_pid;
5286 other_pid = hdspm->capture_pid;
5287 } else {
5288 this_pid = hdspm->capture_pid;
5289 other_pid = hdspm->playback_pid;
5290 }
5291
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005292 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005293
5294 /* The other stream is open, and not by the same
5295 task as this one. Make sure that the parameters
5296 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005297 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005298
5299 if (params_rate(params) != hdspm->system_sample_rate) {
5300 spin_unlock_irq(&hdspm->lock);
5301 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005302 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005303 return -EBUSY;
5304 }
5305
5306 if (params_period_size(params) != hdspm->period_bytes / 4) {
5307 spin_unlock_irq(&hdspm->lock);
5308 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005309 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005310 return -EBUSY;
5311 }
5312
5313 }
5314 /* We're fine. */
5315 spin_unlock_irq(&hdspm->lock);
5316
5317 /* how to make sure that the rate matches an externally-set one ? */
5318
5319 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005320 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5321 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005322 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005323 spin_unlock_irq(&hdspm->lock);
5324 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005325 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005326 return err;
5327 }
5328 spin_unlock_irq(&hdspm->lock);
5329
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005330 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005331 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005332 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005333 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005334 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005335 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005336 return err;
5337 }
5338
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005339 /* Memory allocation, takashi's method, dont know if we should
5340 * spinlock
5341 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005342 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005343 /* Update for MADI rev 204: we need to allocate for all channels,
5344 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005345
Takashi Iwai763f3562005-06-03 11:25:34 +02005346 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005347 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5348 if (err < 0) {
5349 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005350 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005351 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005352
Takashi Iwai763f3562005-06-03 11:25:34 +02005353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5354
Takashi Iwai77a23f22008-08-21 13:00:13 +02005355 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005356 params_channels(params));
5357
5358 for (i = 0; i < params_channels(params); ++i)
5359 snd_hdspm_enable_out(hdspm, i, 1);
5360
5361 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005362 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005363 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005364 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005365 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005366 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005367 params_channels(params));
5368
5369 for (i = 0; i < params_channels(params); ++i)
5370 snd_hdspm_enable_in(hdspm, i, 1);
5371
5372 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005373 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005374 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005375 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005376 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005377
Remy Bruno3cee5a62006-10-16 12:46:32 +02005378 /*
5379 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5380 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5381 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005382 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005383 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005384 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005385 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5386 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5387 "playback" : "capture",
5388 params_rate(params), params_channels(params),
5389 params_buffer_size(params));
5390 */
5391
5392
5393 /* Switch to native float format if requested */
5394 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5395 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5396 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5397
5398 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5399 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5400 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5401 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5402
5403 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5404 }
5405 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5406
Takashi Iwai763f3562005-06-03 11:25:34 +02005407 return 0;
5408}
5409
Takashi Iwai98274f02005-11-17 14:52:34 +01005410static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005411{
5412 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005413 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005414
5415 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5416
Adrian Knoth0dca1792011-01-26 19:32:14 +01005417 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005418 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005419 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005420 snd_hdspm_enable_out(hdspm, i, 0);
5421
5422 hdspm->playback_buffer = NULL;
5423 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005424 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005425 snd_hdspm_enable_in(hdspm, i, 0);
5426
5427 hdspm->capture_buffer = NULL;
5428
5429 }
5430
5431 snd_pcm_lib_free_pages(substream);
5432
5433 return 0;
5434}
5435
Adrian Knoth0dca1792011-01-26 19:32:14 +01005436
Takashi Iwai98274f02005-11-17 14:52:34 +01005437static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005438 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005439{
Takashi Iwai98274f02005-11-17 14:52:34 +01005440 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005441
Adrian Knoth0dca1792011-01-26 19:32:14 +01005442 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5443 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5444 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5445 return -EINVAL;
5446 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005447
Adrian Knoth0dca1792011-01-26 19:32:14 +01005448 if (hdspm->channel_map_out[info->channel] < 0) {
5449 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5450 return -EINVAL;
5451 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005452
Adrian Knoth0dca1792011-01-26 19:32:14 +01005453 info->offset = hdspm->channel_map_out[info->channel] *
5454 HDSPM_CHANNEL_BUFFER_BYTES;
5455 } else {
5456 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5457 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5458 return -EINVAL;
5459 }
5460
5461 if (hdspm->channel_map_in[info->channel] < 0) {
5462 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5463 return -EINVAL;
5464 }
5465
5466 info->offset = hdspm->channel_map_in[info->channel] *
5467 HDSPM_CHANNEL_BUFFER_BYTES;
5468 }
5469
Takashi Iwai763f3562005-06-03 11:25:34 +02005470 info->first = 0;
5471 info->step = 32;
5472 return 0;
5473}
5474
Adrian Knoth0dca1792011-01-26 19:32:14 +01005475
Takashi Iwai98274f02005-11-17 14:52:34 +01005476static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005477 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005478{
5479 switch (cmd) {
5480 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005481 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005482
5483 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005484 {
5485 struct snd_pcm_channel_info *info = arg;
5486 return snd_hdspm_channel_info(substream, info);
5487 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005488 default:
5489 break;
5490 }
5491
5492 return snd_pcm_lib_ioctl(substream, cmd, arg);
5493}
5494
Takashi Iwai98274f02005-11-17 14:52:34 +01005495static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005496{
Takashi Iwai98274f02005-11-17 14:52:34 +01005497 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5498 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005499 int running;
5500
5501 spin_lock(&hdspm->lock);
5502 running = hdspm->running;
5503 switch (cmd) {
5504 case SNDRV_PCM_TRIGGER_START:
5505 running |= 1 << substream->stream;
5506 break;
5507 case SNDRV_PCM_TRIGGER_STOP:
5508 running &= ~(1 << substream->stream);
5509 break;
5510 default:
5511 snd_BUG();
5512 spin_unlock(&hdspm->lock);
5513 return -EINVAL;
5514 }
5515 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5516 other = hdspm->capture_substream;
5517 else
5518 other = hdspm->playback_substream;
5519
5520 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005521 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005522 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005523 if (s == other) {
5524 snd_pcm_trigger_done(s, substream);
5525 if (cmd == SNDRV_PCM_TRIGGER_START)
5526 running |= 1 << s->stream;
5527 else
5528 running &= ~(1 << s->stream);
5529 goto _ok;
5530 }
5531 }
5532 if (cmd == SNDRV_PCM_TRIGGER_START) {
5533 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005534 && substream->stream ==
5535 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005536 hdspm_silence_playback(hdspm);
5537 } else {
5538 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005539 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005540 hdspm_silence_playback(hdspm);
5541 }
5542 } else {
5543 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5544 hdspm_silence_playback(hdspm);
5545 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005546_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005547 snd_pcm_trigger_done(substream, substream);
5548 if (!hdspm->running && running)
5549 hdspm_start_audio(hdspm);
5550 else if (hdspm->running && !running)
5551 hdspm_stop_audio(hdspm);
5552 hdspm->running = running;
5553 spin_unlock(&hdspm->lock);
5554
5555 return 0;
5556}
5557
Takashi Iwai98274f02005-11-17 14:52:34 +01005558static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005559{
5560 return 0;
5561}
5562
Takashi Iwai98274f02005-11-17 14:52:34 +01005563static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005564 .info = (SNDRV_PCM_INFO_MMAP |
5565 SNDRV_PCM_INFO_MMAP_VALID |
5566 SNDRV_PCM_INFO_NONINTERLEAVED |
5567 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5568 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5569 .rates = (SNDRV_PCM_RATE_32000 |
5570 SNDRV_PCM_RATE_44100 |
5571 SNDRV_PCM_RATE_48000 |
5572 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005573 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5574 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005575 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005576 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005577 .channels_min = 1,
5578 .channels_max = HDSPM_MAX_CHANNELS,
5579 .buffer_bytes_max =
5580 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005581 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005582 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005583 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005584 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005585 .fifo_size = 0
5586};
5587
Takashi Iwai98274f02005-11-17 14:52:34 +01005588static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005589 .info = (SNDRV_PCM_INFO_MMAP |
5590 SNDRV_PCM_INFO_MMAP_VALID |
5591 SNDRV_PCM_INFO_NONINTERLEAVED |
5592 SNDRV_PCM_INFO_SYNC_START),
5593 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5594 .rates = (SNDRV_PCM_RATE_32000 |
5595 SNDRV_PCM_RATE_44100 |
5596 SNDRV_PCM_RATE_48000 |
5597 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005598 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5599 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005600 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005601 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005602 .channels_min = 1,
5603 .channels_max = HDSPM_MAX_CHANNELS,
5604 .buffer_bytes_max =
5605 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005606 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005607 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005608 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005609 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005610 .fifo_size = 0
5611};
5612
Adrian Knoth0dca1792011-01-26 19:32:14 +01005613static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5614 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005615{
Takashi Iwai98274f02005-11-17 14:52:34 +01005616 struct hdspm *hdspm = rule->private;
5617 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005618 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005619 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005620 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5621
Adrian Knoth0dca1792011-01-26 19:32:14 +01005622 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005623 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005624 .min = hdspm->qs_in_channels,
5625 .max = hdspm->qs_in_channels,
5626 .integer = 1,
5627 };
5628 return snd_interval_refine(c, &t);
5629 } else if (r->min > 48000 && r->max <= 96000) {
5630 struct snd_interval t = {
5631 .min = hdspm->ds_in_channels,
5632 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005633 .integer = 1,
5634 };
5635 return snd_interval_refine(c, &t);
5636 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005637 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005638 .min = hdspm->ss_in_channels,
5639 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005640 .integer = 1,
5641 };
5642 return snd_interval_refine(c, &t);
5643 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005644
Takashi Iwai763f3562005-06-03 11:25:34 +02005645 return 0;
5646}
5647
Adrian Knoth0dca1792011-01-26 19:32:14 +01005648static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005649 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005650{
Takashi Iwai98274f02005-11-17 14:52:34 +01005651 struct hdspm *hdspm = rule->private;
5652 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005653 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005654 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005655 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5656
Adrian Knoth0dca1792011-01-26 19:32:14 +01005657 if (r->min > 96000 && r->max <= 192000) {
5658 struct snd_interval t = {
5659 .min = hdspm->qs_out_channels,
5660 .max = hdspm->qs_out_channels,
5661 .integer = 1,
5662 };
5663 return snd_interval_refine(c, &t);
5664 } else if (r->min > 48000 && r->max <= 96000) {
5665 struct snd_interval t = {
5666 .min = hdspm->ds_out_channels,
5667 .max = hdspm->ds_out_channels,
5668 .integer = 1,
5669 };
5670 return snd_interval_refine(c, &t);
5671 } else if (r->max < 64000) {
5672 struct snd_interval t = {
5673 .min = hdspm->ss_out_channels,
5674 .max = hdspm->ss_out_channels,
5675 .integer = 1,
5676 };
5677 return snd_interval_refine(c, &t);
5678 } else {
5679 }
5680 return 0;
5681}
5682
5683static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5684 struct snd_pcm_hw_rule * rule)
5685{
5686 struct hdspm *hdspm = rule->private;
5687 struct snd_interval *c =
5688 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5689 struct snd_interval *r =
5690 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5691
5692 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005693 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005694 .min = 32000,
5695 .max = 48000,
5696 .integer = 1,
5697 };
5698 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005699 } else if (c->max <= hdspm->qs_in_channels) {
5700 struct snd_interval t = {
5701 .min = 128000,
5702 .max = 192000,
5703 .integer = 1,
5704 };
5705 return snd_interval_refine(r, &t);
5706 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005707 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005708 .min = 64000,
5709 .max = 96000,
5710 .integer = 1,
5711 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005712 return snd_interval_refine(r, &t);
5713 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005714
5715 return 0;
5716}
5717static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5718 struct snd_pcm_hw_rule *rule)
5719{
5720 struct hdspm *hdspm = rule->private;
5721 struct snd_interval *c =
5722 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5723 struct snd_interval *r =
5724 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5725
5726 if (c->min >= hdspm->ss_out_channels) {
5727 struct snd_interval t = {
5728 .min = 32000,
5729 .max = 48000,
5730 .integer = 1,
5731 };
5732 return snd_interval_refine(r, &t);
5733 } else if (c->max <= hdspm->qs_out_channels) {
5734 struct snd_interval t = {
5735 .min = 128000,
5736 .max = 192000,
5737 .integer = 1,
5738 };
5739 return snd_interval_refine(r, &t);
5740 } else if (c->max <= hdspm->ds_out_channels) {
5741 struct snd_interval t = {
5742 .min = 64000,
5743 .max = 96000,
5744 .integer = 1,
5745 };
5746 return snd_interval_refine(r, &t);
5747 }
5748
Takashi Iwai763f3562005-06-03 11:25:34 +02005749 return 0;
5750}
5751
Adrian Knoth0dca1792011-01-26 19:32:14 +01005752static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005753 struct snd_pcm_hw_rule *rule)
5754{
5755 unsigned int list[3];
5756 struct hdspm *hdspm = rule->private;
5757 struct snd_interval *c = hw_param_interval(params,
5758 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005759
5760 list[0] = hdspm->qs_in_channels;
5761 list[1] = hdspm->ds_in_channels;
5762 list[2] = hdspm->ss_in_channels;
5763 return snd_interval_list(c, 3, list, 0);
5764}
5765
5766static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5767 struct snd_pcm_hw_rule *rule)
5768{
5769 unsigned int list[3];
5770 struct hdspm *hdspm = rule->private;
5771 struct snd_interval *c = hw_param_interval(params,
5772 SNDRV_PCM_HW_PARAM_CHANNELS);
5773
5774 list[0] = hdspm->qs_out_channels;
5775 list[1] = hdspm->ds_out_channels;
5776 list[2] = hdspm->ss_out_channels;
5777 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005778}
5779
5780
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005781static unsigned int hdspm_aes32_sample_rates[] = {
5782 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5783};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005784
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005785static struct snd_pcm_hw_constraint_list
5786hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005787 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5788 .list = hdspm_aes32_sample_rates,
5789 .mask = 0
5790};
5791
Takashi Iwai98274f02005-11-17 14:52:34 +01005792static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005793{
Takashi Iwai98274f02005-11-17 14:52:34 +01005794 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5795 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005796
Takashi Iwai763f3562005-06-03 11:25:34 +02005797 spin_lock_irq(&hdspm->lock);
5798
5799 snd_pcm_set_sync(substream);
5800
Adrian Knoth0dca1792011-01-26 19:32:14 +01005801
Takashi Iwai763f3562005-06-03 11:25:34 +02005802 runtime->hw = snd_hdspm_playback_subinfo;
5803
5804 if (hdspm->capture_substream == NULL)
5805 hdspm_stop_audio(hdspm);
5806
5807 hdspm->playback_pid = current->pid;
5808 hdspm->playback_substream = substream;
5809
5810 spin_unlock_irq(&hdspm->lock);
5811
5812 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005813 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005814
Adrian Knoth0dca1792011-01-26 19:32:14 +01005815 switch (hdspm->io_type) {
5816 case AIO:
5817 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005818 snd_pcm_hw_constraint_minmax(runtime,
5819 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5820 32, 4096);
5821 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
5822 snd_pcm_hw_constraint_minmax(runtime,
5823 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5824 16384, 16384);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005825 break;
5826
5827 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005828 snd_pcm_hw_constraint_minmax(runtime,
5829 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5830 64, 8192);
5831 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005832 }
5833
5834 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02005835 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005836 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5837 &hdspm_hw_constraints_aes32_sample_rates);
5838 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005839 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005840 snd_hdspm_hw_rule_rate_out_channels, hdspm,
5841 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005842 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005843
5844 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5845 snd_hdspm_hw_rule_out_channels, hdspm,
5846 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5847
5848 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5849 snd_hdspm_hw_rule_out_channels_rate, hdspm,
5850 SNDRV_PCM_HW_PARAM_RATE, -1);
5851
Takashi Iwai763f3562005-06-03 11:25:34 +02005852 return 0;
5853}
5854
Takashi Iwai98274f02005-11-17 14:52:34 +01005855static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005856{
Takashi Iwai98274f02005-11-17 14:52:34 +01005857 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005858
5859 spin_lock_irq(&hdspm->lock);
5860
5861 hdspm->playback_pid = -1;
5862 hdspm->playback_substream = NULL;
5863
5864 spin_unlock_irq(&hdspm->lock);
5865
5866 return 0;
5867}
5868
5869
Takashi Iwai98274f02005-11-17 14:52:34 +01005870static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005871{
Takashi Iwai98274f02005-11-17 14:52:34 +01005872 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5873 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005874
5875 spin_lock_irq(&hdspm->lock);
5876 snd_pcm_set_sync(substream);
5877 runtime->hw = snd_hdspm_capture_subinfo;
5878
5879 if (hdspm->playback_substream == NULL)
5880 hdspm_stop_audio(hdspm);
5881
5882 hdspm->capture_pid = current->pid;
5883 hdspm->capture_substream = substream;
5884
5885 spin_unlock_irq(&hdspm->lock);
5886
5887 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005888 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
5889
Adrian Knoth0dca1792011-01-26 19:32:14 +01005890 switch (hdspm->io_type) {
5891 case AIO:
5892 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005893 snd_pcm_hw_constraint_minmax(runtime,
5894 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5895 32, 4096);
5896 snd_pcm_hw_constraint_minmax(runtime,
5897 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5898 16384, 16384);
5899 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005900
5901 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005902 snd_pcm_hw_constraint_minmax(runtime,
5903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5904 64, 8192);
5905 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005906 }
5907
5908 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02005909 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005910 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5911 &hdspm_hw_constraints_aes32_sample_rates);
5912 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005913 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005914 snd_hdspm_hw_rule_rate_in_channels, hdspm,
5915 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005916 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005917
5918 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5919 snd_hdspm_hw_rule_in_channels, hdspm,
5920 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5921
5922 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5923 snd_hdspm_hw_rule_in_channels_rate, hdspm,
5924 SNDRV_PCM_HW_PARAM_RATE, -1);
5925
Takashi Iwai763f3562005-06-03 11:25:34 +02005926 return 0;
5927}
5928
Takashi Iwai98274f02005-11-17 14:52:34 +01005929static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005930{
Takashi Iwai98274f02005-11-17 14:52:34 +01005931 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005932
5933 spin_lock_irq(&hdspm->lock);
5934
5935 hdspm->capture_pid = -1;
5936 hdspm->capture_substream = NULL;
5937
5938 spin_unlock_irq(&hdspm->lock);
5939 return 0;
5940}
5941
Adrian Knoth0dca1792011-01-26 19:32:14 +01005942static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02005943{
Adrian Knoth0dca1792011-01-26 19:32:14 +01005944 /* we have nothing to initialize but the call is required */
5945 return 0;
5946}
5947
5948static inline int copy_u32_le(void __user *dest, void __iomem *src)
5949{
5950 u32 val = readl(src);
5951 return copy_to_user(dest, &val, 4);
5952}
5953
5954static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
Dan Carpenter2ca595a2011-09-23 09:25:05 +03005955 unsigned int cmd, unsigned long arg)
Adrian Knoth0dca1792011-01-26 19:32:14 +01005956{
5957 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005958 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01005959 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005960 struct hdspm_config info;
5961 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01005962 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005963 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005964 struct hdspm_ltc ltc;
5965 unsigned int statusregister;
5966 long unsigned int s;
5967 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005968
5969 switch (cmd) {
5970
Takashi Iwai763f3562005-06-03 11:25:34 +02005971 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005972 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005973 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005974 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005975 readl(hdspm->iobase +
5976 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005977 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005978 readl(hdspm->iobase +
5979 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005980 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005981 readl(hdspm->iobase +
5982 HDSPM_MADI_OUTPUT_PEAK + i*4);
5983
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005984 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005985 ((uint64_t) readl(hdspm->iobase +
5986 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
5987 (uint64_t) readl(hdspm->iobase +
5988 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005989 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005990 ((uint64_t)readl(hdspm->iobase +
5991 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
5992 (uint64_t)readl(hdspm->iobase +
5993 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005994 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005995 ((uint64_t)readl(hdspm->iobase +
5996 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
5997 (uint64_t)readl(hdspm->iobase +
5998 HDSPM_MADI_OUTPUT_RMS_L + i*4);
5999 }
6000
6001 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006002 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006003 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006004 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006005 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006006 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006007 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006008 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006009
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006010 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01006011 if (0 != s) {
6012 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6013 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6014 */
Takashi Iwai763f3562005-06-03 11:25:34 +02006015 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006016 }
6017 break;
6018
6019 case SNDRV_HDSPM_IOCTL_GET_LTC:
6020 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6021 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6022 if (i & HDSPM_TCO1_LTC_Input_valid) {
6023 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6024 HDSPM_TCO1_LTC_Format_MSB)) {
6025 case 0:
6026 ltc.format = fps_24;
6027 break;
6028 case HDSPM_TCO1_LTC_Format_LSB:
6029 ltc.format = fps_25;
6030 break;
6031 case HDSPM_TCO1_LTC_Format_MSB:
6032 ltc.format = fps_2997;
6033 break;
6034 default:
6035 ltc.format = 30;
6036 break;
6037 }
6038 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6039 ltc.frame = drop_frame;
6040 } else {
6041 ltc.frame = full_frame;
6042 }
6043 } else {
6044 ltc.format = format_invalid;
6045 ltc.frame = frame_invalid;
6046 }
6047 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6048 ltc.input_format = ntsc;
6049 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6050 ltc.input_format = pal;
6051 } else {
6052 ltc.input_format = no_video;
6053 }
6054
6055 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6056 if (0 != s) {
6057 /*
6058 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02006059 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006060 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006061
6062 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02006063
Adrian Knoth0dca1792011-01-26 19:32:14 +01006064 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02006065
Adrian Knoth4ab69a22011-02-23 11:43:14 +01006066 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02006067 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006068 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6069 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006070
6071 info.system_sample_rate = hdspm->system_sample_rate;
6072 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006073 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006074 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6075 info.clock_source = hdspm_clock_source(hdspm);
6076 info.autosync_ref = hdspm_autosync_ref(hdspm);
Adrian Knothc9e16682012-12-03 14:55:50 +01006077 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
Takashi Iwai763f3562005-06-03 11:25:34 +02006078 info.passthru = 0;
6079 spin_unlock_irq(&hdspm->lock);
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006080 if (copy_to_user(argp, &info, sizeof(info)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006081 return -EFAULT;
6082 break;
6083
Adrian Knoth0dca1792011-01-26 19:32:14 +01006084 case SNDRV_HDSPM_IOCTL_GET_STATUS:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006085 memset(&status, 0, sizeof(status));
6086
Adrian Knoth0dca1792011-01-26 19:32:14 +01006087 status.card_type = hdspm->io_type;
6088
6089 status.autosync_source = hdspm_autosync_ref(hdspm);
6090
6091 status.card_clock = 110069313433624ULL;
6092 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6093
6094 switch (hdspm->io_type) {
6095 case MADI:
6096 case MADIface:
6097 status.card_specific.madi.sync_wc =
6098 hdspm_wc_sync_check(hdspm);
6099 status.card_specific.madi.sync_madi =
6100 hdspm_madi_sync_check(hdspm);
6101 status.card_specific.madi.sync_tco =
6102 hdspm_tco_sync_check(hdspm);
6103 status.card_specific.madi.sync_in =
6104 hdspm_sync_in_sync_check(hdspm);
6105
6106 statusregister =
6107 hdspm_read(hdspm, HDSPM_statusRegister);
6108 status.card_specific.madi.madi_input =
6109 (statusregister & HDSPM_AB_int) ? 1 : 0;
6110 status.card_specific.madi.channel_format =
Adrian Knoth9e6ff522011-10-27 21:57:52 +02006111 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006112 /* TODO: Mac driver sets it when f_s>48kHz */
6113 status.card_specific.madi.frame_format = 0;
6114
6115 default:
6116 break;
6117 }
6118
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006119 if (copy_to_user(argp, &status, sizeof(status)))
Adrian Knoth0dca1792011-01-26 19:32:14 +01006120 return -EFAULT;
6121
6122
6123 break;
6124
Takashi Iwai763f3562005-06-03 11:25:34 +02006125 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006126 memset(&hdspm_version, 0, sizeof(hdspm_version));
6127
Adrian Knoth0dca1792011-01-26 19:32:14 +01006128 hdspm_version.card_type = hdspm->io_type;
6129 strncpy(hdspm_version.cardname, hdspm->card_name,
6130 sizeof(hdspm_version.cardname));
Adrian Knoth7d53a632012-01-04 14:31:16 +01006131 hdspm_version.serial = hdspm->serial;
Takashi Iwai763f3562005-06-03 11:25:34 +02006132 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006133 hdspm_version.addons = 0;
6134 if (hdspm->tco)
6135 hdspm_version.addons |= HDSPM_ADDON_TCO;
6136
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006137 if (copy_to_user(argp, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006138 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006139 return -EFAULT;
6140 break;
6141
6142 case SNDRV_HDSPM_IOCTL_GET_MIXER:
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006143 if (copy_from_user(&mixer, argp, sizeof(mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006144 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006145 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006146 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006147 return -EFAULT;
6148 break;
6149
6150 default:
6151 return -EINVAL;
6152 }
6153 return 0;
6154}
6155
Takashi Iwai98274f02005-11-17 14:52:34 +01006156static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006157 .open = snd_hdspm_playback_open,
6158 .close = snd_hdspm_playback_release,
6159 .ioctl = snd_hdspm_ioctl,
6160 .hw_params = snd_hdspm_hw_params,
6161 .hw_free = snd_hdspm_hw_free,
6162 .prepare = snd_hdspm_prepare,
6163 .trigger = snd_hdspm_trigger,
6164 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006165 .page = snd_pcm_sgbuf_ops_page,
6166};
6167
Takashi Iwai98274f02005-11-17 14:52:34 +01006168static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006169 .open = snd_hdspm_capture_open,
6170 .close = snd_hdspm_capture_release,
6171 .ioctl = snd_hdspm_ioctl,
6172 .hw_params = snd_hdspm_hw_params,
6173 .hw_free = snd_hdspm_hw_free,
6174 .prepare = snd_hdspm_prepare,
6175 .trigger = snd_hdspm_trigger,
6176 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006177 .page = snd_pcm_sgbuf_ops_page,
6178};
6179
Bill Pembertone23e7a12012-12-06 12:35:10 -05006180static int snd_hdspm_create_hwdep(struct snd_card *card,
6181 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006182{
Takashi Iwai98274f02005-11-17 14:52:34 +01006183 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006184 int err;
6185
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006186 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6187 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006188 return err;
6189
6190 hdspm->hwdep = hw;
6191 hw->private_data = hdspm;
6192 strcpy(hw->name, "HDSPM hwdep interface");
6193
Adrian Knoth0dca1792011-01-26 19:32:14 +01006194 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006195 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth8de5d6f2012-03-08 15:38:04 +01006196 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006197 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006198
6199 return 0;
6200}
6201
6202
6203/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006204 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006205 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05006206static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006207{
6208 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006209 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006210 size_t wanted;
6211
6212 pcm = hdspm->pcm;
6213
Remy Bruno3cee5a62006-10-16 12:46:32 +02006214 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006215
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006216 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006217 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006218 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006219 snd_dma_pci_data(hdspm->pci),
6220 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006221 wanted);
6222 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006223 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006224
6225 return err;
6226 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006227 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006228
6229 return 0;
6230}
6231
Adrian Knoth0dca1792011-01-26 19:32:14 +01006232
6233static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006234 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006235 unsigned int reg, int channels)
6236{
6237 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006238
6239 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006240 for (i = 0; i < (channels * 16); i++)
6241 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006242 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006243}
6244
Adrian Knoth0dca1792011-01-26 19:32:14 +01006245
Takashi Iwai763f3562005-06-03 11:25:34 +02006246/* ------------- ALSA Devices ---------------------------- */
Bill Pembertone23e7a12012-12-06 12:35:10 -05006247static int snd_hdspm_create_pcm(struct snd_card *card,
6248 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006249{
Takashi Iwai98274f02005-11-17 14:52:34 +01006250 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006251 int err;
6252
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006253 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6254 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006255 return err;
6256
6257 hdspm->pcm = pcm;
6258 pcm->private_data = hdspm;
6259 strcpy(pcm->name, hdspm->card_name);
6260
6261 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6262 &snd_hdspm_playback_ops);
6263 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6264 &snd_hdspm_capture_ops);
6265
6266 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6267
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006268 err = snd_hdspm_preallocate_memory(hdspm);
6269 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006270 return err;
6271
6272 return 0;
6273}
6274
Takashi Iwai98274f02005-11-17 14:52:34 +01006275static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006276{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006277 int i;
6278
6279 for (i = 0; i < hdspm->midiPorts; i++)
6280 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006281}
6282
Bill Pembertone23e7a12012-12-06 12:35:10 -05006283static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6284 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006285{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006286 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006287
6288 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006289 err = snd_hdspm_create_pcm(card, hdspm);
6290 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006291 return err;
6292
Adrian Knoth0dca1792011-01-26 19:32:14 +01006293 i = 0;
6294 while (i < hdspm->midiPorts) {
6295 err = snd_hdspm_create_midi(card, hdspm, i);
6296 if (err < 0) {
6297 return err;
6298 }
6299 i++;
6300 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006301
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006302 err = snd_hdspm_create_controls(card, hdspm);
6303 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006304 return err;
6305
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006306 err = snd_hdspm_create_hwdep(card, hdspm);
6307 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006308 return err;
6309
6310 snd_printdd("proc init...\n");
6311 snd_hdspm_proc_init(hdspm);
6312
6313 hdspm->system_sample_rate = -1;
6314 hdspm->last_external_sample_rate = -1;
6315 hdspm->last_internal_sample_rate = -1;
6316 hdspm->playback_pid = -1;
6317 hdspm->capture_pid = -1;
6318 hdspm->capture_substream = NULL;
6319 hdspm->playback_substream = NULL;
6320
6321 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006322 err = snd_hdspm_set_defaults(hdspm);
6323 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006324 return err;
6325
6326 snd_printdd("Update mixer controls...\n");
6327 hdspm_update_simple_mixer_controls(hdspm);
6328
6329 snd_printdd("Initializeing complete ???\n");
6330
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006331 err = snd_card_register(card);
6332 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006333 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6334 return err;
6335 }
6336
6337 snd_printdd("... yes now\n");
6338
6339 return 0;
6340}
6341
Bill Pembertone23e7a12012-12-06 12:35:10 -05006342static int snd_hdspm_create(struct snd_card *card,
6343 struct hdspm *hdspm)
6344{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006345
Takashi Iwai763f3562005-06-03 11:25:34 +02006346 struct pci_dev *pci = hdspm->pci;
6347 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006348 unsigned long io_extent;
6349
6350 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006351 hdspm->card = card;
6352
6353 spin_lock_init(&hdspm->lock);
6354
Takashi Iwai763f3562005-06-03 11:25:34 +02006355 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006356 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006357
Takashi Iwai763f3562005-06-03 11:25:34 +02006358 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006359 strcpy(card->driver, "HDSPM");
6360
6361 switch (hdspm->firmware_rev) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01006362 case HDSPM_RAYDAT_REV:
6363 hdspm->io_type = RayDAT;
6364 hdspm->card_name = "RME RayDAT";
6365 hdspm->midiPorts = 2;
6366 break;
6367 case HDSPM_AIO_REV:
6368 hdspm->io_type = AIO;
6369 hdspm->card_name = "RME AIO";
6370 hdspm->midiPorts = 1;
6371 break;
6372 case HDSPM_MADIFACE_REV:
6373 hdspm->io_type = MADIface;
6374 hdspm->card_name = "RME MADIface";
6375 hdspm->midiPorts = 1;
6376 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006377 default:
Adrian Knothc09403d2011-10-27 21:57:54 +02006378 if ((hdspm->firmware_rev == 0xf0) ||
6379 ((hdspm->firmware_rev >= 0xe6) &&
6380 (hdspm->firmware_rev <= 0xea))) {
6381 hdspm->io_type = AES32;
6382 hdspm->card_name = "RME AES32";
6383 hdspm->midiPorts = 2;
Adrian Knoth05c7cc92011-11-21 16:15:36 +01006384 } else if ((hdspm->firmware_rev == 0xd2) ||
Adrian Knothc09403d2011-10-27 21:57:54 +02006385 ((hdspm->firmware_rev >= 0xc8) &&
6386 (hdspm->firmware_rev <= 0xcf))) {
6387 hdspm->io_type = MADI;
6388 hdspm->card_name = "RME MADI";
6389 hdspm->midiPorts = 3;
6390 } else {
6391 snd_printk(KERN_ERR
6392 "HDSPM: unknown firmware revision %x\n",
Adrian Knoth5027f342011-02-28 15:14:49 +01006393 hdspm->firmware_rev);
Adrian Knothc09403d2011-10-27 21:57:54 +02006394 return -ENODEV;
6395 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02006396 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006397
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006398 err = pci_enable_device(pci);
6399 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006400 return err;
6401
6402 pci_set_master(hdspm->pci);
6403
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006404 err = pci_request_regions(pci, "hdspm");
6405 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006406 return err;
6407
6408 hdspm->port = pci_resource_start(pci, 0);
6409 io_extent = pci_resource_len(pci, 0);
6410
6411 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006412 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006413
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006414 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6415 if (!hdspm->iobase) {
6416 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006417 "unable to remap region 0x%lx-0x%lx\n",
6418 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006419 return -EBUSY;
6420 }
6421 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006422 (unsigned long)hdspm->iobase, hdspm->port,
6423 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006424
6425 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006426 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006427 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6428 return -EBUSY;
6429 }
6430
6431 snd_printdd("use IRQ %d\n", pci->irq);
6432
6433 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006434
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006435 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006436 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006437 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6438 if (!hdspm->mixer) {
6439 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006440 "unable to kmalloc Mixer memory of %d Bytes\n",
6441 (int)sizeof(struct hdspm_mixer));
Julia Lawallb17cbdd2012-08-19 09:02:54 +02006442 return -ENOMEM;
Takashi Iwai763f3562005-06-03 11:25:34 +02006443 }
6444
Adrian Knoth0dca1792011-01-26 19:32:14 +01006445 hdspm->port_names_in = NULL;
6446 hdspm->port_names_out = NULL;
6447
6448 switch (hdspm->io_type) {
6449 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006450 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6451 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6452 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006453
6454 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6455 channel_map_aes32;
6456 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6457 channel_map_aes32;
6458 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6459 channel_map_aes32;
6460 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6461 texts_ports_aes32;
6462 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6463 texts_ports_aes32;
6464 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6465 texts_ports_aes32;
6466
Adrian Knothd2d10a22011-02-28 15:14:47 +01006467 hdspm->max_channels_out = hdspm->max_channels_in =
6468 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006469 hdspm->port_names_in = hdspm->port_names_out =
6470 texts_ports_aes32;
6471 hdspm->channel_map_in = hdspm->channel_map_out =
6472 channel_map_aes32;
6473
Adrian Knoth0dca1792011-01-26 19:32:14 +01006474 break;
6475
6476 case MADI:
6477 case MADIface:
6478 hdspm->ss_in_channels = hdspm->ss_out_channels =
6479 MADI_SS_CHANNELS;
6480 hdspm->ds_in_channels = hdspm->ds_out_channels =
6481 MADI_DS_CHANNELS;
6482 hdspm->qs_in_channels = hdspm->qs_out_channels =
6483 MADI_QS_CHANNELS;
6484
6485 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6486 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006487 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006488 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006489 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006490 channel_map_unity_ss;
6491
6492 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6493 texts_ports_madi;
6494 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6495 texts_ports_madi;
6496 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6497 texts_ports_madi;
6498 break;
6499
6500 case AIO:
6501 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6502 snd_printk(KERN_INFO "HDSPM: AEB input board found, but not supported\n");
6503 }
6504
6505 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6506 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6507 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6508 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6509 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6510 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6511
6512 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6513 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6514 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6515
6516 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6517 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6518 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6519
6520 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6521 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6522 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6523 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6524 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6525 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6526
6527 break;
6528
6529 case RayDAT:
6530 hdspm->ss_in_channels = hdspm->ss_out_channels =
6531 RAYDAT_SS_CHANNELS;
6532 hdspm->ds_in_channels = hdspm->ds_out_channels =
6533 RAYDAT_DS_CHANNELS;
6534 hdspm->qs_in_channels = hdspm->qs_out_channels =
6535 RAYDAT_QS_CHANNELS;
6536
6537 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6538 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6539
6540 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6541 channel_map_raydat_ss;
6542 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6543 channel_map_raydat_ds;
6544 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6545 channel_map_raydat_qs;
6546 hdspm->channel_map_in = hdspm->channel_map_out =
6547 channel_map_raydat_ss;
6548
6549 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6550 texts_ports_raydat_ss;
6551 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6552 texts_ports_raydat_ds;
6553 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6554 texts_ports_raydat_qs;
6555
6556
6557 break;
6558
6559 }
6560
6561 /* TCO detection */
6562 switch (hdspm->io_type) {
6563 case AIO:
6564 case RayDAT:
6565 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6566 HDSPM_s2_tco_detect) {
6567 hdspm->midiPorts++;
6568 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6569 GFP_KERNEL);
6570 if (NULL != hdspm->tco) {
6571 hdspm_tco_write(hdspm);
6572 }
6573 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6574 } else {
6575 hdspm->tco = NULL;
6576 }
6577 break;
6578
6579 case MADI:
6580 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6581 hdspm->midiPorts++;
6582 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6583 GFP_KERNEL);
6584 if (NULL != hdspm->tco) {
6585 hdspm_tco_write(hdspm);
6586 }
6587 snd_printk(KERN_INFO "HDSPM: MADI TCO module found\n");
6588 } else {
6589 hdspm->tco = NULL;
6590 }
6591 break;
6592
6593 default:
6594 hdspm->tco = NULL;
6595 }
6596
6597 /* texts */
6598 switch (hdspm->io_type) {
6599 case AES32:
6600 if (hdspm->tco) {
6601 hdspm->texts_autosync = texts_autosync_aes_tco;
6602 hdspm->texts_autosync_items = 10;
6603 } else {
6604 hdspm->texts_autosync = texts_autosync_aes;
6605 hdspm->texts_autosync_items = 9;
6606 }
6607 break;
6608
6609 case MADI:
6610 if (hdspm->tco) {
6611 hdspm->texts_autosync = texts_autosync_madi_tco;
6612 hdspm->texts_autosync_items = 4;
6613 } else {
6614 hdspm->texts_autosync = texts_autosync_madi;
6615 hdspm->texts_autosync_items = 3;
6616 }
6617 break;
6618
6619 case MADIface:
6620
6621 break;
6622
6623 case RayDAT:
6624 if (hdspm->tco) {
6625 hdspm->texts_autosync = texts_autosync_raydat_tco;
6626 hdspm->texts_autosync_items = 9;
6627 } else {
6628 hdspm->texts_autosync = texts_autosync_raydat;
6629 hdspm->texts_autosync_items = 8;
6630 }
6631 break;
6632
6633 case AIO:
6634 if (hdspm->tco) {
6635 hdspm->texts_autosync = texts_autosync_aio_tco;
6636 hdspm->texts_autosync_items = 6;
6637 } else {
6638 hdspm->texts_autosync = texts_autosync_aio;
6639 hdspm->texts_autosync_items = 5;
6640 }
6641 break;
6642
6643 }
6644
6645 tasklet_init(&hdspm->midi_tasklet,
6646 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006647
Adrian Knothf7de8ba2012-01-10 20:58:40 +01006648
6649 if (hdspm->io_type != MADIface) {
6650 hdspm->serial = (hdspm_read(hdspm,
6651 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6652 /* id contains either a user-provided value or the default
6653 * NULL. If it's the default, we're safe to
6654 * fill card->id with the serial number.
6655 *
6656 * If the serial number is 0xFFFFFF, then we're dealing with
6657 * an old PCI revision that comes without a sane number. In
6658 * this case, we don't set card->id to avoid collisions
6659 * when running with multiple cards.
6660 */
6661 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6662 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6663 snd_card_set_id(card, card->id);
6664 }
6665 }
6666
Takashi Iwai763f3562005-06-03 11:25:34 +02006667 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006668 err = snd_hdspm_create_alsa_devices(card, hdspm);
6669 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006670 return err;
6671
6672 snd_hdspm_initialize_midi_flush(hdspm);
6673
6674 return 0;
6675}
6676
Adrian Knoth0dca1792011-01-26 19:32:14 +01006677
Takashi Iwai98274f02005-11-17 14:52:34 +01006678static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006679{
6680
6681 if (hdspm->port) {
6682
6683 /* stop th audio, and cancel all interrupts */
6684 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006685 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006686 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6687 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006688 hdspm_write(hdspm, HDSPM_controlRegister,
6689 hdspm->control_register);
6690 }
6691
6692 if (hdspm->irq >= 0)
6693 free_irq(hdspm->irq, (void *) hdspm);
6694
Jesper Juhlfc584222005-10-24 15:11:28 +02006695 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006696
6697 if (hdspm->iobase)
6698 iounmap(hdspm->iobase);
6699
Takashi Iwai763f3562005-06-03 11:25:34 +02006700 if (hdspm->port)
6701 pci_release_regions(hdspm->pci);
6702
6703 pci_disable_device(hdspm->pci);
6704 return 0;
6705}
6706
Adrian Knoth0dca1792011-01-26 19:32:14 +01006707
Takashi Iwai98274f02005-11-17 14:52:34 +01006708static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006709{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006710 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006711
6712 if (hdspm)
6713 snd_hdspm_free(hdspm);
6714}
6715
Adrian Knoth0dca1792011-01-26 19:32:14 +01006716
Bill Pembertone23e7a12012-12-06 12:35:10 -05006717static int snd_hdspm_probe(struct pci_dev *pci,
6718 const struct pci_device_id *pci_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02006719{
6720 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006721 struct hdspm *hdspm;
6722 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006723 int err;
6724
6725 if (dev >= SNDRV_CARDS)
6726 return -ENODEV;
6727 if (!enable[dev]) {
6728 dev++;
6729 return -ENOENT;
6730 }
6731
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006732 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006733 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006734 if (err < 0)
6735 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006736
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006737 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006738 card->private_free = snd_hdspm_card_free;
6739 hdspm->dev = dev;
6740 hdspm->pci = pci;
6741
Takashi Iwaic187c042007-02-19 15:27:33 +01006742 snd_card_set_dev(card, &pci->dev);
6743
Adrian Knoth0dca1792011-01-26 19:32:14 +01006744 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006745 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006746 snd_card_free(card);
6747 return err;
6748 }
6749
Adrian Knoth0dca1792011-01-26 19:32:14 +01006750 if (hdspm->io_type != MADIface) {
6751 sprintf(card->shortname, "%s_%x",
6752 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006753 hdspm->serial);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006754 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6755 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006756 hdspm->serial,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006757 hdspm->port, hdspm->irq);
6758 } else {
6759 sprintf(card->shortname, "%s", hdspm->card_name);
6760 sprintf(card->longname, "%s at 0x%lx, irq %d",
6761 hdspm->card_name, hdspm->port, hdspm->irq);
6762 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006763
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006764 err = snd_card_register(card);
6765 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006766 snd_card_free(card);
6767 return err;
6768 }
6769
6770 pci_set_drvdata(pci, card);
6771
6772 dev++;
6773 return 0;
6774}
6775
Bill Pembertone23e7a12012-12-06 12:35:10 -05006776static void snd_hdspm_remove(struct pci_dev *pci)
Takashi Iwai763f3562005-06-03 11:25:34 +02006777{
6778 snd_card_free(pci_get_drvdata(pci));
Takashi Iwai763f3562005-06-03 11:25:34 +02006779}
6780
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006781static struct pci_driver hdspm_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006782 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006783 .id_table = snd_hdspm_ids,
6784 .probe = snd_hdspm_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05006785 .remove = snd_hdspm_remove,
Takashi Iwai763f3562005-06-03 11:25:34 +02006786};
6787
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006788module_pci_driver(hdspm_driver);