blob: c03ee011bfb65cf69883c1279b69a00085c8310a [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
261/* --- bit helper defines */
262#define HDSPM_LatencyMask (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200263#define HDSPM_FrequencyMask (HDSPM_Frequency0|HDSPM_Frequency1|\
264 HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
Takashi Iwai763f3562005-06-03 11:25:34 +0200265#define HDSPM_InputMask (HDSPM_InputSelect0|HDSPM_InputSelect1)
266#define HDSPM_InputOptical 0
267#define HDSPM_InputCoaxial (HDSPM_InputSelect0)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200268#define HDSPM_SyncRefMask (HDSPM_SyncRef0|HDSPM_SyncRef1|\
269 HDSPM_SyncRef2|HDSPM_SyncRef3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200270
Adrian Knoth0dca1792011-01-26 19:32:14 +0100271#define HDSPM_c0_SyncRef0 0x2
272#define HDSPM_c0_SyncRef1 0x4
273#define HDSPM_c0_SyncRef2 0x8
274#define HDSPM_c0_SyncRef3 0x10
275#define HDSPM_c0_SyncRefMask (HDSPM_c0_SyncRef0 | HDSPM_c0_SyncRef1 |\
276 HDSPM_c0_SyncRef2 | HDSPM_c0_SyncRef3)
277
278#define HDSPM_SYNC_FROM_WORD 0 /* Preferred sync reference */
279#define HDSPM_SYNC_FROM_MADI 1 /* choices - used by "pref_sync_ref" */
280#define HDSPM_SYNC_FROM_TCO 2
281#define HDSPM_SYNC_FROM_SYNC_IN 3
Takashi Iwai763f3562005-06-03 11:25:34 +0200282
283#define HDSPM_Frequency32KHz HDSPM_Frequency0
284#define HDSPM_Frequency44_1KHz HDSPM_Frequency1
285#define HDSPM_Frequency48KHz (HDSPM_Frequency1|HDSPM_Frequency0)
286#define HDSPM_Frequency64KHz (HDSPM_DoubleSpeed|HDSPM_Frequency0)
287#define HDSPM_Frequency88_2KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200288#define HDSPM_Frequency96KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
289 HDSPM_Frequency0)
Remy Bruno3cee5a62006-10-16 12:46:32 +0200290#define HDSPM_Frequency128KHz (HDSPM_QuadSpeed|HDSPM_Frequency0)
291#define HDSPM_Frequency176_4KHz (HDSPM_QuadSpeed|HDSPM_Frequency1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200292#define HDSPM_Frequency192KHz (HDSPM_QuadSpeed|HDSPM_Frequency1|\
293 HDSPM_Frequency0)
Takashi Iwai763f3562005-06-03 11:25:34 +0200294
Takashi Iwai763f3562005-06-03 11:25:34 +0200295
296/* Synccheck Status */
297#define HDSPM_SYNC_CHECK_NO_LOCK 0
298#define HDSPM_SYNC_CHECK_LOCK 1
299#define HDSPM_SYNC_CHECK_SYNC 2
300
301/* AutoSync References - used by "autosync_ref" control switch */
302#define HDSPM_AUTOSYNC_FROM_WORD 0
303#define HDSPM_AUTOSYNC_FROM_MADI 1
Adrian Knoth0dca1792011-01-26 19:32:14 +0100304#define HDSPM_AUTOSYNC_FROM_TCO 2
305#define HDSPM_AUTOSYNC_FROM_SYNC_IN 3
306#define HDSPM_AUTOSYNC_FROM_NONE 4
Takashi Iwai763f3562005-06-03 11:25:34 +0200307
308/* Possible sources of MADI input */
309#define HDSPM_OPTICAL 0 /* optical */
310#define HDSPM_COAXIAL 1 /* BNC */
311
312#define hdspm_encode_latency(x) (((x)<<1) & HDSPM_LatencyMask)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100313#define hdspm_decode_latency(x) ((((x) & HDSPM_LatencyMask)>>1))
Takashi Iwai763f3562005-06-03 11:25:34 +0200314
315#define hdspm_encode_in(x) (((x)&0x3)<<14)
316#define hdspm_decode_in(x) (((x)>>14)&0x3)
317
318/* --- control2 register bits --- */
319#define HDSPM_TMS (1<<0)
320#define HDSPM_TCK (1<<1)
321#define HDSPM_TDI (1<<2)
322#define HDSPM_JTAG (1<<3)
323#define HDSPM_PWDN (1<<4)
324#define HDSPM_PROGRAM (1<<5)
325#define HDSPM_CONFIG_MODE_0 (1<<6)
326#define HDSPM_CONFIG_MODE_1 (1<<7)
327/*#define HDSPM_VERSION_BIT (1<<8) not defined any more*/
328#define HDSPM_BIGENDIAN_MODE (1<<9)
329#define HDSPM_RD_MULTIPLE (1<<10)
330
Remy Bruno3cee5a62006-10-16 12:46:32 +0200331/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200332 that do not conflict with specific bits for AES32 seem to be valid also
333 for the AES32
334 */
Takashi Iwai763f3562005-06-03 11:25:34 +0200335#define HDSPM_audioIRQPending (1<<0) /* IRQ is high and pending */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200336#define HDSPM_RX_64ch (1<<1) /* Input 64chan. MODE=1, 56chn MODE=0 */
337#define HDSPM_AB_int (1<<2) /* InputChannel Opt=0, Coax=1
338 * (like inp0)
339 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100340
Takashi Iwai763f3562005-06-03 11:25:34 +0200341#define HDSPM_madiLock (1<<3) /* MADI Locked =1, no=0 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100342#define HDSPM_madiSync (1<<18) /* MADI is in sync */
343
344#define HDSPM_tcoLock 0x00000020 /* Optional TCO locked status FOR HDSPe MADI! */
345#define HDSPM_tcoSync 0x10000000 /* Optional TCO sync status */
346
347#define HDSPM_syncInLock 0x00010000 /* Sync In lock status FOR HDSPe MADI! */
348#define HDSPM_syncInSync 0x00020000 /* Sync In sync status FOR HDSPe MADI! */
Takashi Iwai763f3562005-06-03 11:25:34 +0200349
350#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100351 /* since 64byte accurate, last 6 bits are not used */
Takashi Iwai763f3562005-06-03 11:25:34 +0200352
Adrian Knoth0dca1792011-01-26 19:32:14 +0100353
354
Takashi Iwai763f3562005-06-03 11:25:34 +0200355#define HDSPM_DoubleSpeedStatus (1<<19) /* (input) card in double speed */
356
357#define HDSPM_madiFreq0 (1<<22) /* system freq 0=error */
358#define HDSPM_madiFreq1 (1<<23) /* 1=32, 2=44.1 3=48 */
359#define HDSPM_madiFreq2 (1<<24) /* 4=64, 5=88.2 6=96 */
360#define HDSPM_madiFreq3 (1<<25) /* 7=128, 8=176.4 9=192 */
361
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200362#define HDSPM_BufferID (1<<26) /* (Double)Buffer ID toggles with
363 * Interrupt
364 */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100365#define HDSPM_tco_detect 0x08000000
366#define HDSPM_tco_lock 0x20000000
367
368#define HDSPM_s2_tco_detect 0x00000040
369#define HDSPM_s2_AEBO_D 0x00000080
370#define HDSPM_s2_AEBI_D 0x00000100
371
372
373#define HDSPM_midi0IRQPending 0x40000000
374#define HDSPM_midi1IRQPending 0x80000000
375#define HDSPM_midi2IRQPending 0x20000000
376#define HDSPM_midi2IRQPendingAES 0x00000020
377#define HDSPM_midi3IRQPending 0x00200000
Takashi Iwai763f3562005-06-03 11:25:34 +0200378
379/* --- status bit helpers */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200380#define HDSPM_madiFreqMask (HDSPM_madiFreq0|HDSPM_madiFreq1|\
381 HDSPM_madiFreq2|HDSPM_madiFreq3)
Takashi Iwai763f3562005-06-03 11:25:34 +0200382#define HDSPM_madiFreq32 (HDSPM_madiFreq0)
383#define HDSPM_madiFreq44_1 (HDSPM_madiFreq1)
384#define HDSPM_madiFreq48 (HDSPM_madiFreq0|HDSPM_madiFreq1)
385#define HDSPM_madiFreq64 (HDSPM_madiFreq2)
386#define HDSPM_madiFreq88_2 (HDSPM_madiFreq0|HDSPM_madiFreq2)
387#define HDSPM_madiFreq96 (HDSPM_madiFreq1|HDSPM_madiFreq2)
388#define HDSPM_madiFreq128 (HDSPM_madiFreq0|HDSPM_madiFreq1|HDSPM_madiFreq2)
389#define HDSPM_madiFreq176_4 (HDSPM_madiFreq3)
390#define HDSPM_madiFreq192 (HDSPM_madiFreq3|HDSPM_madiFreq0)
391
Remy Bruno3cee5a62006-10-16 12:46:32 +0200392/* Status2 Register bits */ /* MADI ONLY */
Takashi Iwai763f3562005-06-03 11:25:34 +0200393
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300394#define HDSPM_version0 (1<<0) /* not really defined but I guess */
Takashi Iwai763f3562005-06-03 11:25:34 +0200395#define HDSPM_version1 (1<<1) /* in former cards it was ??? */
396#define HDSPM_version2 (1<<2)
397
398#define HDSPM_wcLock (1<<3) /* Wordclock is detected and locked */
399#define HDSPM_wcSync (1<<4) /* Wordclock is in sync with systemclock */
400
401#define HDSPM_wc_freq0 (1<<5) /* input freq detected via autosync */
402#define HDSPM_wc_freq1 (1<<6) /* 001=32, 010==44.1, 011=48, */
403#define HDSPM_wc_freq2 (1<<7) /* 100=64, 101=88.2, 110=96, */
404/* missing Bit for 111=128, 1000=176.4, 1001=192 */
405
Adrian Knoth0dca1792011-01-26 19:32:14 +0100406#define HDSPM_SyncRef0 0x10000 /* Sync Reference */
407#define HDSPM_SyncRef1 0x20000
408
409#define HDSPM_SelSyncRef0 (1<<8) /* AutoSync Source */
Takashi Iwai763f3562005-06-03 11:25:34 +0200410#define HDSPM_SelSyncRef1 (1<<9) /* 000=word, 001=MADI, */
411#define HDSPM_SelSyncRef2 (1<<10) /* 111=no valid signal */
412
413#define HDSPM_wc_valid (HDSPM_wcLock|HDSPM_wcSync)
414
415#define HDSPM_wcFreqMask (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2)
416#define HDSPM_wcFreq32 (HDSPM_wc_freq0)
417#define HDSPM_wcFreq44_1 (HDSPM_wc_freq1)
418#define HDSPM_wcFreq48 (HDSPM_wc_freq0|HDSPM_wc_freq1)
419#define HDSPM_wcFreq64 (HDSPM_wc_freq2)
420#define HDSPM_wcFreq88_2 (HDSPM_wc_freq0|HDSPM_wc_freq2)
421#define HDSPM_wcFreq96 (HDSPM_wc_freq1|HDSPM_wc_freq2)
422
Adrian Knoth0dca1792011-01-26 19:32:14 +0100423#define HDSPM_status1_F_0 0x0400000
424#define HDSPM_status1_F_1 0x0800000
425#define HDSPM_status1_F_2 0x1000000
426#define HDSPM_status1_F_3 0x2000000
427#define HDSPM_status1_freqMask (HDSPM_status1_F_0|HDSPM_status1_F_1|HDSPM_status1_F_2|HDSPM_status1_F_3)
428
Takashi Iwai763f3562005-06-03 11:25:34 +0200429
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200430#define HDSPM_SelSyncRefMask (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
431 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200432#define HDSPM_SelSyncRef_WORD 0
433#define HDSPM_SelSyncRef_MADI (HDSPM_SelSyncRef0)
Adrian Knoth0dca1792011-01-26 19:32:14 +0100434#define HDSPM_SelSyncRef_TCO (HDSPM_SelSyncRef1)
435#define HDSPM_SelSyncRef_SyncIn (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200436#define HDSPM_SelSyncRef_NVALID (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
437 HDSPM_SelSyncRef2)
Takashi Iwai763f3562005-06-03 11:25:34 +0200438
Remy Bruno3cee5a62006-10-16 12:46:32 +0200439/*
440 For AES32, bits for status, status2 and timecode are different
441*/
442/* status */
443#define HDSPM_AES32_wcLock 0x0200000
Andre Schramm56bde0f2013-01-09 14:40:18 +0100444#define HDSPM_AES32_wcSync 0x0100000
Remy Bruno3cee5a62006-10-16 12:46:32 +0200445#define HDSPM_AES32_wcFreq_bit 22
Adrian Knoth0dca1792011-01-26 19:32:14 +0100446/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
Remy Bruno3cee5a62006-10-16 12:46:32 +0200447 HDSPM_bit2freq */
448#define HDSPM_AES32_syncref_bit 16
449/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */
450
451#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
452#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
453#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
454#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
455#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
456#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
457#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
458#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
459#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
Remy Bruno65345992007-08-31 12:21:08 +0200460#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
Remy Bruno3cee5a62006-10-16 12:46:32 +0200461
462/* status2 */
463/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
464#define HDSPM_LockAES 0x80
465#define HDSPM_LockAES1 0x80
466#define HDSPM_LockAES2 0x40
467#define HDSPM_LockAES3 0x20
468#define HDSPM_LockAES4 0x10
469#define HDSPM_LockAES5 0x8
470#define HDSPM_LockAES6 0x4
471#define HDSPM_LockAES7 0x2
472#define HDSPM_LockAES8 0x1
473/*
474 Timecode
475 After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
476 AES i+1
477 bits 3210
478 0001 32kHz
479 0010 44.1kHz
480 0011 48kHz
481 0100 64kHz
482 0101 88.2kHz
483 0110 96kHz
484 0111 128kHz
485 1000 176.4kHz
486 1001 192kHz
487 NB: Timecode register doesn't seem to work on AES32 card revision 230
488*/
489
Takashi Iwai763f3562005-06-03 11:25:34 +0200490/* Mixer Values */
491#define UNITY_GAIN 32768 /* = 65536/2 */
492#define MINUS_INFINITY_GAIN 0
493
Takashi Iwai763f3562005-06-03 11:25:34 +0200494/* Number of channels for different Speed Modes */
495#define MADI_SS_CHANNELS 64
496#define MADI_DS_CHANNELS 32
497#define MADI_QS_CHANNELS 16
498
Adrian Knoth0dca1792011-01-26 19:32:14 +0100499#define RAYDAT_SS_CHANNELS 36
500#define RAYDAT_DS_CHANNELS 20
501#define RAYDAT_QS_CHANNELS 12
502
503#define AIO_IN_SS_CHANNELS 14
504#define AIO_IN_DS_CHANNELS 10
505#define AIO_IN_QS_CHANNELS 8
506#define AIO_OUT_SS_CHANNELS 16
507#define AIO_OUT_DS_CHANNELS 12
508#define AIO_OUT_QS_CHANNELS 10
509
Adrian Knothd2d10a22011-02-28 15:14:47 +0100510#define AES32_CHANNELS 16
511
Takashi Iwai763f3562005-06-03 11:25:34 +0200512/* the size of a substream (1 mono data stream) */
513#define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024)
514#define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_CHANNEL_BUFFER_SAMPLES)
515
516/* the size of the area we need to allocate for DMA transfers. the
517 size is the same regardless of the number of channels, and
Adrian Knoth0dca1792011-01-26 19:32:14 +0100518 also the latency to use.
Takashi Iwai763f3562005-06-03 11:25:34 +0200519 for one direction !!!
520*/
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100521#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
Takashi Iwai763f3562005-06-03 11:25:34 +0200522#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
523
Adrian Knoth0dca1792011-01-26 19:32:14 +0100524#define HDSPM_RAYDAT_REV 211
525#define HDSPM_AIO_REV 212
526#define HDSPM_MADIFACE_REV 213
Remy Bruno3cee5a62006-10-16 12:46:32 +0200527
Remy Bruno65345992007-08-31 12:21:08 +0200528/* speed factor modes */
529#define HDSPM_SPEED_SINGLE 0
530#define HDSPM_SPEED_DOUBLE 1
531#define HDSPM_SPEED_QUAD 2
Adrian Knoth0dca1792011-01-26 19:32:14 +0100532
Remy Bruno65345992007-08-31 12:21:08 +0200533/* names for speed modes */
534static char *hdspm_speed_names[] = { "single", "double", "quad" };
535
Adrian Knoth0dca1792011-01-26 19:32:14 +0100536static char *texts_autosync_aes_tco[] = { "Word Clock",
537 "AES1", "AES2", "AES3", "AES4",
538 "AES5", "AES6", "AES7", "AES8",
539 "TCO" };
540static char *texts_autosync_aes[] = { "Word Clock",
541 "AES1", "AES2", "AES3", "AES4",
542 "AES5", "AES6", "AES7", "AES8" };
543static char *texts_autosync_madi_tco[] = { "Word Clock",
544 "MADI", "TCO", "Sync In" };
545static char *texts_autosync_madi[] = { "Word Clock",
546 "MADI", "Sync In" };
547
548static char *texts_autosync_raydat_tco[] = {
549 "Word Clock",
550 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
551 "AES", "SPDIF", "TCO", "Sync In"
552};
553static char *texts_autosync_raydat[] = {
554 "Word Clock",
555 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
556 "AES", "SPDIF", "Sync In"
557};
558static char *texts_autosync_aio_tco[] = {
559 "Word Clock",
560 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
561};
562static char *texts_autosync_aio[] = { "Word Clock",
563 "ADAT", "AES", "SPDIF", "Sync In" };
564
565static char *texts_freq[] = {
566 "No Lock",
567 "32 kHz",
568 "44.1 kHz",
569 "48 kHz",
570 "64 kHz",
571 "88.2 kHz",
572 "96 kHz",
573 "128 kHz",
574 "176.4 kHz",
575 "192 kHz"
576};
577
Adrian Knoth0dca1792011-01-26 19:32:14 +0100578static char *texts_ports_madi[] = {
579 "MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
580 "MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
581 "MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
582 "MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
583 "MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
584 "MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
585 "MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
586 "MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
587 "MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
588 "MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
589 "MADI.61", "MADI.62", "MADI.63", "MADI.64",
590};
591
592
593static char *texts_ports_raydat_ss[] = {
594 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
595 "ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
596 "ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
597 "ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
598 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
599 "ADAT4.7", "ADAT4.8",
600 "AES.L", "AES.R",
601 "SPDIF.L", "SPDIF.R"
602};
603
604static char *texts_ports_raydat_ds[] = {
605 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
606 "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
607 "ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
608 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
609 "AES.L", "AES.R",
610 "SPDIF.L", "SPDIF.R"
611};
612
613static char *texts_ports_raydat_qs[] = {
614 "ADAT1.1", "ADAT1.2",
615 "ADAT2.1", "ADAT2.2",
616 "ADAT3.1", "ADAT3.2",
617 "ADAT4.1", "ADAT4.2",
618 "AES.L", "AES.R",
619 "SPDIF.L", "SPDIF.R"
620};
621
622
623static char *texts_ports_aio_in_ss[] = {
624 "Analogue.L", "Analogue.R",
625 "AES.L", "AES.R",
626 "SPDIF.L", "SPDIF.R",
627 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
628 "ADAT.7", "ADAT.8"
629};
630
631static char *texts_ports_aio_out_ss[] = {
632 "Analogue.L", "Analogue.R",
633 "AES.L", "AES.R",
634 "SPDIF.L", "SPDIF.R",
635 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
636 "ADAT.7", "ADAT.8",
637 "Phone.L", "Phone.R"
638};
639
640static char *texts_ports_aio_in_ds[] = {
641 "Analogue.L", "Analogue.R",
642 "AES.L", "AES.R",
643 "SPDIF.L", "SPDIF.R",
644 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
645};
646
647static char *texts_ports_aio_out_ds[] = {
648 "Analogue.L", "Analogue.R",
649 "AES.L", "AES.R",
650 "SPDIF.L", "SPDIF.R",
651 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
652 "Phone.L", "Phone.R"
653};
654
655static char *texts_ports_aio_in_qs[] = {
656 "Analogue.L", "Analogue.R",
657 "AES.L", "AES.R",
658 "SPDIF.L", "SPDIF.R",
659 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
660};
661
662static char *texts_ports_aio_out_qs[] = {
663 "Analogue.L", "Analogue.R",
664 "AES.L", "AES.R",
665 "SPDIF.L", "SPDIF.R",
666 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
667 "Phone.L", "Phone.R"
668};
669
Adrian Knoth432d2502011-02-23 11:43:08 +0100670static char *texts_ports_aes32[] = {
671 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
672 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
673 "AES.15", "AES.16"
674};
675
Adrian Knoth55a57602011-01-27 11:23:15 +0100676/* These tables map the ALSA channels 1..N to the channels that we
677 need to use in order to find the relevant channel buffer. RME
678 refers to this kind of mapping as between "the ADAT channel and
679 the DMA channel." We index it using the logical audio channel,
680 and the value is the DMA channel (i.e. channel buffer number)
681 where the data for that channel can be read/written from/to.
682*/
683
684static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
685 0, 1, 2, 3, 4, 5, 6, 7,
686 8, 9, 10, 11, 12, 13, 14, 15,
687 16, 17, 18, 19, 20, 21, 22, 23,
688 24, 25, 26, 27, 28, 29, 30, 31,
689 32, 33, 34, 35, 36, 37, 38, 39,
690 40, 41, 42, 43, 44, 45, 46, 47,
691 48, 49, 50, 51, 52, 53, 54, 55,
692 56, 57, 58, 59, 60, 61, 62, 63
693};
694
Adrian Knoth55a57602011-01-27 11:23:15 +0100695static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
696 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
697 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
698 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
699 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
700 0, 1, /* AES */
701 2, 3, /* SPDIF */
702 -1, -1, -1, -1,
703 -1, -1, -1, -1, -1, -1, -1, -1,
704 -1, -1, -1, -1, -1, -1, -1, -1,
705 -1, -1, -1, -1, -1, -1, -1, -1,
706};
707
708static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
709 4, 5, 6, 7, /* ADAT 1 */
710 8, 9, 10, 11, /* ADAT 2 */
711 12, 13, 14, 15, /* ADAT 3 */
712 16, 17, 18, 19, /* ADAT 4 */
713 0, 1, /* AES */
714 2, 3, /* SPDIF */
715 -1, -1, -1, -1,
716 -1, -1, -1, -1, -1, -1, -1, -1,
717 -1, -1, -1, -1, -1, -1, -1, -1,
718 -1, -1, -1, -1, -1, -1, -1, -1,
719 -1, -1, -1, -1, -1, -1, -1, -1,
720 -1, -1, -1, -1, -1, -1, -1, -1,
721};
722
723static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
724 4, 5, /* ADAT 1 */
725 6, 7, /* ADAT 2 */
726 8, 9, /* ADAT 3 */
727 10, 11, /* ADAT 4 */
728 0, 1, /* AES */
729 2, 3, /* SPDIF */
730 -1, -1, -1, -1,
731 -1, -1, -1, -1, -1, -1, -1, -1,
732 -1, -1, -1, -1, -1, -1, -1, -1,
733 -1, -1, -1, -1, -1, -1, -1, -1,
734 -1, -1, -1, -1, -1, -1, -1, -1,
735 -1, -1, -1, -1, -1, -1, -1, -1,
736 -1, -1, -1, -1, -1, -1, -1, -1,
737};
738
739static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
740 0, 1, /* line in */
741 8, 9, /* aes in, */
742 10, 11, /* spdif in */
743 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
744 -1, -1,
745 -1, -1, -1, -1, -1, -1, -1, -1,
746 -1, -1, -1, -1, -1, -1, -1, -1,
747 -1, -1, -1, -1, -1, -1, -1, -1,
748 -1, -1, -1, -1, -1, -1, -1, -1,
749 -1, -1, -1, -1, -1, -1, -1, -1,
750 -1, -1, -1, -1, -1, -1, -1, -1,
751};
752
753static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
754 0, 1, /* line out */
755 8, 9, /* aes out */
756 10, 11, /* spdif out */
757 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
758 6, 7, /* phone out */
759 -1, -1, -1, -1, -1, -1, -1, -1,
760 -1, -1, -1, -1, -1, -1, -1, -1,
761 -1, -1, -1, -1, -1, -1, -1, -1,
762 -1, -1, -1, -1, -1, -1, -1, -1,
763 -1, -1, -1, -1, -1, -1, -1, -1,
764 -1, -1, -1, -1, -1, -1, -1, -1,
765};
766
767static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
768 0, 1, /* line in */
769 8, 9, /* aes in */
770 10, 11, /* spdif in */
771 12, 14, 16, 18, /* adat in */
772 -1, -1, -1, -1, -1, -1,
773 -1, -1, -1, -1, -1, -1, -1, -1,
774 -1, -1, -1, -1, -1, -1, -1, -1,
775 -1, -1, -1, -1, -1, -1, -1, -1,
776 -1, -1, -1, -1, -1, -1, -1, -1,
777 -1, -1, -1, -1, -1, -1, -1, -1,
778 -1, -1, -1, -1, -1, -1, -1, -1
779};
780
781static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
782 0, 1, /* line out */
783 8, 9, /* aes out */
784 10, 11, /* spdif out */
785 12, 14, 16, 18, /* adat out */
786 6, 7, /* phone out */
787 -1, -1, -1, -1,
788 -1, -1, -1, -1, -1, -1, -1, -1,
789 -1, -1, -1, -1, -1, -1, -1, -1,
790 -1, -1, -1, -1, -1, -1, -1, -1,
791 -1, -1, -1, -1, -1, -1, -1, -1,
792 -1, -1, -1, -1, -1, -1, -1, -1,
793 -1, -1, -1, -1, -1, -1, -1, -1
794};
795
796static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
797 0, 1, /* line in */
798 8, 9, /* aes in */
799 10, 11, /* spdif in */
800 12, 16, /* adat in */
801 -1, -1, -1, -1, -1, -1, -1, -1,
802 -1, -1, -1, -1, -1, -1, -1, -1,
803 -1, -1, -1, -1, -1, -1, -1, -1,
804 -1, -1, -1, -1, -1, -1, -1, -1,
805 -1, -1, -1, -1, -1, -1, -1, -1,
806 -1, -1, -1, -1, -1, -1, -1, -1,
807 -1, -1, -1, -1, -1, -1, -1, -1
808};
809
810static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
811 0, 1, /* line out */
812 8, 9, /* aes out */
813 10, 11, /* spdif out */
814 12, 16, /* adat out */
815 6, 7, /* phone out */
816 -1, -1, -1, -1, -1, -1,
817 -1, -1, -1, -1, -1, -1, -1, -1,
818 -1, -1, -1, -1, -1, -1, -1, -1,
819 -1, -1, -1, -1, -1, -1, -1, -1,
820 -1, -1, -1, -1, -1, -1, -1, -1,
821 -1, -1, -1, -1, -1, -1, -1, -1,
822 -1, -1, -1, -1, -1, -1, -1, -1
823};
824
Adrian Knoth432d2502011-02-23 11:43:08 +0100825static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
826 0, 1, 2, 3, 4, 5, 6, 7,
827 8, 9, 10, 11, 12, 13, 14, 15,
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 -1, -1, -1, -1, -1, -1, -1, -1,
832 -1, -1, -1, -1, -1, -1, -1, -1,
833 -1, -1, -1, -1, -1, -1, -1, -1
834};
835
Takashi Iwai98274f02005-11-17 14:52:34 +0100836struct hdspm_midi {
837 struct hdspm *hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +0200838 int id;
Takashi Iwai98274f02005-11-17 14:52:34 +0100839 struct snd_rawmidi *rmidi;
840 struct snd_rawmidi_substream *input;
841 struct snd_rawmidi_substream *output;
Takashi Iwai763f3562005-06-03 11:25:34 +0200842 char istimer; /* timer in use */
843 struct timer_list timer;
844 spinlock_t lock;
845 int pending;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100846 int dataIn;
847 int statusIn;
848 int dataOut;
849 int statusOut;
850 int ie;
851 int irq;
852};
853
854struct hdspm_tco {
855 int input;
856 int framerate;
857 int wordclock;
858 int samplerate;
859 int pull;
860 int term; /* 0 = off, 1 = on */
Takashi Iwai763f3562005-06-03 11:25:34 +0200861};
862
Takashi Iwai98274f02005-11-17 14:52:34 +0100863struct hdspm {
Takashi Iwai763f3562005-06-03 11:25:34 +0200864 spinlock_t lock;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200865 /* only one playback and/or capture stream */
866 struct snd_pcm_substream *capture_substream;
867 struct snd_pcm_substream *playback_substream;
Takashi Iwai763f3562005-06-03 11:25:34 +0200868
869 char *card_name; /* for procinfo */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200870 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
871
Adrian Knoth0dca1792011-01-26 19:32:14 +0100872 uint8_t io_type;
Takashi Iwai763f3562005-06-03 11:25:34 +0200873
Takashi Iwai763f3562005-06-03 11:25:34 +0200874 int monitor_outs; /* set up monitoring outs init flag */
875
876 u32 control_register; /* cached value */
877 u32 control2_register; /* cached value */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100878 u32 settings_register;
Takashi Iwai763f3562005-06-03 11:25:34 +0200879
Adrian Knoth0dca1792011-01-26 19:32:14 +0100880 struct hdspm_midi midi[4];
Takashi Iwai763f3562005-06-03 11:25:34 +0200881 struct tasklet_struct midi_tasklet;
882
883 size_t period_bytes;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100884 unsigned char ss_in_channels;
885 unsigned char ds_in_channels;
886 unsigned char qs_in_channels;
887 unsigned char ss_out_channels;
888 unsigned char ds_out_channels;
889 unsigned char qs_out_channels;
890
891 unsigned char max_channels_in;
892 unsigned char max_channels_out;
893
Takashi Iwai286bed02011-06-30 12:45:36 +0200894 signed char *channel_map_in;
895 signed char *channel_map_out;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100896
Takashi Iwai286bed02011-06-30 12:45:36 +0200897 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
898 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100899
900 char **port_names_in;
901 char **port_names_out;
902
903 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
904 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
Takashi Iwai763f3562005-06-03 11:25:34 +0200905
906 unsigned char *playback_buffer; /* suitably aligned address */
907 unsigned char *capture_buffer; /* suitably aligned address */
908
909 pid_t capture_pid; /* process id which uses capture */
910 pid_t playback_pid; /* process id which uses capture */
911 int running; /* running status */
912
913 int last_external_sample_rate; /* samplerate mystic ... */
914 int last_internal_sample_rate;
915 int system_sample_rate;
916
Takashi Iwai763f3562005-06-03 11:25:34 +0200917 int dev; /* Hardware vars... */
918 int irq;
919 unsigned long port;
920 void __iomem *iobase;
921
922 int irq_count; /* for debug */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100923 int midiPorts;
Takashi Iwai763f3562005-06-03 11:25:34 +0200924
Takashi Iwai98274f02005-11-17 14:52:34 +0100925 struct snd_card *card; /* one card */
926 struct snd_pcm *pcm; /* has one pcm */
927 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
Takashi Iwai763f3562005-06-03 11:25:34 +0200928 struct pci_dev *pci; /* and an pci info */
929
930 /* Mixer vars */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200931 /* fast alsa mixer */
932 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
933 /* but input to much, so not used */
934 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300935 /* full mixer accessible over mixer ioctl or hwdep-device */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200936 struct hdspm_mixer *mixer;
Takashi Iwai763f3562005-06-03 11:25:34 +0200937
Adrian Knoth0dca1792011-01-26 19:32:14 +0100938 struct hdspm_tco *tco; /* NULL if no TCO detected */
Takashi Iwai763f3562005-06-03 11:25:34 +0200939
Adrian Knoth0dca1792011-01-26 19:32:14 +0100940 char **texts_autosync;
941 int texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +0200942
Adrian Knoth0dca1792011-01-26 19:32:14 +0100943 cycles_t last_interrupt;
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100944
Adrian Knoth7d53a632012-01-04 14:31:16 +0100945 unsigned int serial;
946
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100947 struct hdspm_peak_rms peak_rms;
Takashi Iwai763f3562005-06-03 11:25:34 +0200948};
949
Takashi Iwai763f3562005-06-03 11:25:34 +0200950
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200951static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
Takashi Iwai763f3562005-06-03 11:25:34 +0200952 {
953 .vendor = PCI_VENDOR_ID_XILINX,
954 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
955 .subvendor = PCI_ANY_ID,
956 .subdevice = PCI_ANY_ID,
957 .class = 0,
958 .class_mask = 0,
959 .driver_data = 0},
960 {0,}
961};
962
963MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
964
965/* prototypes */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500966static int snd_hdspm_create_alsa_devices(struct snd_card *card,
967 struct hdspm *hdspm);
968static int snd_hdspm_create_pcm(struct snd_card *card,
969 struct hdspm *hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +0200970
Adrian Knoth0dca1792011-01-26 19:32:14 +0100971static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
Adrian Knoth3f7bf912013-03-10 00:37:21 +0100972static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +0100973static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
974static int hdspm_autosync_ref(struct hdspm *hdspm);
975static int snd_hdspm_set_defaults(struct hdspm *hdspm);
Adrian Knoth21a164d2012-10-19 17:42:23 +0200976static int hdspm_system_clock_mode(struct hdspm *hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +0100977static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +0200978 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +0200979 unsigned int reg, int channels);
980
Remy Bruno3cee5a62006-10-16 12:46:32 +0200981static inline int HDSPM_bit2freq(int n)
982{
Denys Vlasenko62cef822008-04-14 13:04:18 +0200983 static const int bit2freq_tab[] = {
984 0, 32000, 44100, 48000, 64000, 88200,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200985 96000, 128000, 176400, 192000 };
986 if (n < 1 || n > 9)
987 return 0;
988 return bit2freq_tab[n];
989}
990
Adrian Knoth0dca1792011-01-26 19:32:14 +0100991/* Write/read to/from HDSPM with Adresses in Bytes
Takashi Iwai763f3562005-06-03 11:25:34 +0200992 not words but only 32Bit writes are allowed */
993
Takashi Iwai98274f02005-11-17 14:52:34 +0100994static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
Takashi Iwai763f3562005-06-03 11:25:34 +0200995 unsigned int val)
996{
997 writel(val, hdspm->iobase + reg);
998}
999
Takashi Iwai98274f02005-11-17 14:52:34 +01001000static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
Takashi Iwai763f3562005-06-03 11:25:34 +02001001{
1002 return readl(hdspm->iobase + reg);
1003}
1004
Adrian Knoth0dca1792011-01-26 19:32:14 +01001005/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1006 mixer is write only on hardware so we have to cache him for read
Takashi Iwai763f3562005-06-03 11:25:34 +02001007 each fader is a u32, but uses only the first 16 bit */
1008
Takashi Iwai98274f02005-11-17 14:52:34 +01001009static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001010 unsigned int in)
1011{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001012 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001013 return 0;
1014
1015 return hdspm->mixer->ch[chan].in[in];
1016}
1017
Takashi Iwai98274f02005-11-17 14:52:34 +01001018static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001019 unsigned int pb)
1020{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001021 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001022 return 0;
1023 return hdspm->mixer->ch[chan].pb[pb];
1024}
1025
Denys Vlasenko62cef822008-04-14 13:04:18 +02001026static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001027 unsigned int in, unsigned short data)
1028{
1029 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1030 return -1;
1031
1032 hdspm_write(hdspm,
1033 HDSPM_MADI_mixerBase +
1034 ((in + 128 * chan) * sizeof(u32)),
1035 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1036 return 0;
1037}
1038
Denys Vlasenko62cef822008-04-14 13:04:18 +02001039static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001040 unsigned int pb, unsigned short data)
1041{
1042 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1043 return -1;
1044
1045 hdspm_write(hdspm,
1046 HDSPM_MADI_mixerBase +
1047 ((64 + pb + 128 * chan) * sizeof(u32)),
1048 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1049 return 0;
1050}
1051
1052
1053/* enable DMA for specific channels, now available for DSP-MADI */
Takashi Iwai98274f02005-11-17 14:52:34 +01001054static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001055{
1056 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1057}
1058
Takashi Iwai98274f02005-11-17 14:52:34 +01001059static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001060{
1061 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1062}
1063
1064/* check if same process is writing and reading */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001065static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001066{
1067 unsigned long flags;
1068 int ret = 1;
1069
1070 spin_lock_irqsave(&hdspm->lock, flags);
1071 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1072 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1073 ret = 0;
1074 }
1075 spin_unlock_irqrestore(&hdspm->lock, flags);
1076 return ret;
1077}
1078
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001079/* round arbitary sample rates to commonly known rates */
1080static int hdspm_round_frequency(int rate)
1081{
1082 if (rate < 38050)
1083 return 32000;
1084 if (rate < 46008)
1085 return 44100;
1086 else
1087 return 48000;
1088}
1089
1090static int hdspm_tco_sync_check(struct hdspm *hdspm);
1091static int hdspm_sync_in_sync_check(struct hdspm *hdspm);
1092
Takashi Iwai763f3562005-06-03 11:25:34 +02001093/* check for external sample rate */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001094static int hdspm_external_sample_rate(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001095{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001096 unsigned int status, status2, timecode;
1097 int syncref, rate = 0, rate_bits;
Takashi Iwai763f3562005-06-03 11:25:34 +02001098
Adrian Knoth0dca1792011-01-26 19:32:14 +01001099 switch (hdspm->io_type) {
1100 case AES32:
1101 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1102 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01001103 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001104
1105 syncref = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001106
Remy Bruno3cee5a62006-10-16 12:46:32 +02001107 if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
1108 status & HDSPM_AES32_wcLock)
Adrian Knoth0dca1792011-01-26 19:32:14 +01001109 return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF);
1110
Remy Bruno3cee5a62006-10-16 12:46:32 +02001111 if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001112 syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
1113 status2 & (HDSPM_LockAES >>
1114 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
1115 return HDSPM_bit2freq((timecode >> (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001116 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001117 break;
1118
1119 case MADIface:
1120 status = hdspm_read(hdspm, HDSPM_statusRegister);
1121
1122 if (!(status & HDSPM_madiLock)) {
1123 rate = 0; /* no lock */
1124 } else {
1125 switch (status & (HDSPM_status1_freqMask)) {
1126 case HDSPM_status1_F_0*1:
1127 rate = 32000; break;
1128 case HDSPM_status1_F_0*2:
1129 rate = 44100; break;
1130 case HDSPM_status1_F_0*3:
1131 rate = 48000; break;
1132 case HDSPM_status1_F_0*4:
1133 rate = 64000; break;
1134 case HDSPM_status1_F_0*5:
1135 rate = 88200; break;
1136 case HDSPM_status1_F_0*6:
1137 rate = 96000; break;
1138 case HDSPM_status1_F_0*7:
1139 rate = 128000; break;
1140 case HDSPM_status1_F_0*8:
1141 rate = 176400; break;
1142 case HDSPM_status1_F_0*9:
1143 rate = 192000; break;
1144 default:
1145 rate = 0; break;
1146 }
1147 }
1148
1149 break;
1150
1151 case MADI:
1152 case AIO:
1153 case RayDAT:
1154 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1155 status = hdspm_read(hdspm, HDSPM_statusRegister);
1156 rate = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02001157
Remy Bruno3cee5a62006-10-16 12:46:32 +02001158 /* if wordclock has synced freq and wordclock is valid */
1159 if ((status2 & HDSPM_wcLock) != 0 &&
Adrian Knothfedf1532011-06-12 17:26:18 +02001160 (status2 & HDSPM_SelSyncRef0) == 0) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02001161
1162 rate_bits = status2 & HDSPM_wcFreqMask;
1163
Adrian Knoth0dca1792011-01-26 19:32:14 +01001164
Remy Bruno3cee5a62006-10-16 12:46:32 +02001165 switch (rate_bits) {
1166 case HDSPM_wcFreq32:
1167 rate = 32000;
1168 break;
1169 case HDSPM_wcFreq44_1:
1170 rate = 44100;
1171 break;
1172 case HDSPM_wcFreq48:
1173 rate = 48000;
1174 break;
1175 case HDSPM_wcFreq64:
1176 rate = 64000;
1177 break;
1178 case HDSPM_wcFreq88_2:
1179 rate = 88200;
1180 break;
1181 case HDSPM_wcFreq96:
1182 rate = 96000;
1183 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001184 default:
1185 rate = 0;
1186 break;
1187 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001188 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001189
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001190 /* if rate detected and Syncref is Word than have it,
1191 * word has priority to MADI
1192 */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001193 if (rate != 0 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001194 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
Remy Bruno3cee5a62006-10-16 12:46:32 +02001195 return rate;
1196
Adrian Knoth0dca1792011-01-26 19:32:14 +01001197 /* maybe a madi input (which is taken if sel sync is madi) */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001198 if (status & HDSPM_madiLock) {
1199 rate_bits = status & HDSPM_madiFreqMask;
1200
1201 switch (rate_bits) {
1202 case HDSPM_madiFreq32:
1203 rate = 32000;
1204 break;
1205 case HDSPM_madiFreq44_1:
1206 rate = 44100;
1207 break;
1208 case HDSPM_madiFreq48:
1209 rate = 48000;
1210 break;
1211 case HDSPM_madiFreq64:
1212 rate = 64000;
1213 break;
1214 case HDSPM_madiFreq88_2:
1215 rate = 88200;
1216 break;
1217 case HDSPM_madiFreq96:
1218 rate = 96000;
1219 break;
1220 case HDSPM_madiFreq128:
1221 rate = 128000;
1222 break;
1223 case HDSPM_madiFreq176_4:
1224 rate = 176400;
1225 break;
1226 case HDSPM_madiFreq192:
1227 rate = 192000;
1228 break;
1229 default:
1230 rate = 0;
1231 break;
1232 }
Adrian Knothd12c51d2011-07-29 03:11:03 +02001233
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001234 } /* endif HDSPM_madiLock */
1235
1236 /* check sample rate from TCO or SYNC_IN */
1237 {
1238 bool is_valid_input = 0;
1239 bool has_sync = 0;
1240
1241 syncref = hdspm_autosync_ref(hdspm);
1242 if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
1243 is_valid_input = 1;
1244 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1245 hdspm_tco_sync_check(hdspm));
1246 } else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
1247 is_valid_input = 1;
1248 has_sync = (HDSPM_SYNC_CHECK_SYNC ==
1249 hdspm_sync_in_sync_check(hdspm));
Adrian Knothd12c51d2011-07-29 03:11:03 +02001250 }
Adrian Knothfcdc4ba2013-03-10 00:37:22 +01001251
1252 if (is_valid_input && has_sync) {
1253 rate = hdspm_round_frequency(
1254 hdspm_get_pll_freq(hdspm));
1255 }
1256 }
1257
1258 /* QS and DS rates normally can not be detected
1259 * automatically by the card. Only exception is MADI
1260 * in 96k frame mode.
1261 *
1262 * So if we read SS values (32 .. 48k), check for
1263 * user-provided DS/QS bits in the control register
1264 * and multiply the base frequency accordingly.
1265 */
1266 if (rate <= 48000) {
1267 if (hdspm->control_register & HDSPM_QuadSpeed)
1268 rate *= 4;
1269 else if (hdspm->control_register &
1270 HDSPM_DoubleSpeed)
1271 rate *= 2;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001272 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001273 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001274 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001275
1276 return rate;
Takashi Iwai763f3562005-06-03 11:25:34 +02001277}
1278
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001279/* return latency in samples per period */
1280static int hdspm_get_latency(struct hdspm *hdspm)
1281{
1282 int n;
1283
1284 n = hdspm_decode_latency(hdspm->control_register);
1285
1286 /* Special case for new RME cards with 32 samples period size.
1287 * The three latency bits in the control register
1288 * (HDSP_LatencyMask) encode latency values of 64 samples as
1289 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
1290 * denotes 8192 samples, but on new cards like RayDAT or AIO,
1291 * it corresponds to 32 samples.
1292 */
1293 if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
1294 n = -1;
1295
1296 return 1 << (n + 6);
1297}
1298
Takashi Iwai763f3562005-06-03 11:25:34 +02001299/* Latency function */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001300static inline void hdspm_compute_period_size(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001301{
Adrian Knoth7cb155f2011-08-15 00:22:53 +02001302 hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001303}
1304
Adrian Knoth0dca1792011-01-26 19:32:14 +01001305
1306static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001307{
1308 int position;
1309
1310 position = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth483cee72011-02-23 11:43:09 +01001311
1312 switch (hdspm->io_type) {
1313 case RayDAT:
1314 case AIO:
1315 position &= HDSPM_BufferPositionMask;
1316 position /= 4; /* Bytes per sample */
1317 break;
1318 default:
1319 position = (position & HDSPM_BufferID) ?
1320 (hdspm->period_bytes / 4) : 0;
1321 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001322
1323 return position;
1324}
1325
1326
Takashi Iwai98274f02005-11-17 14:52:34 +01001327static inline void hdspm_start_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001328{
1329 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1330 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1331}
1332
Takashi Iwai98274f02005-11-17 14:52:34 +01001333static inline void hdspm_stop_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001334{
1335 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1336 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1337}
1338
1339/* should I silence all or only opened ones ? doit all for first even is 4MB*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001340static void hdspm_silence_playback(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001341{
1342 int i;
1343 int n = hdspm->period_bytes;
1344 void *buf = hdspm->playback_buffer;
1345
Remy Bruno3cee5a62006-10-16 12:46:32 +02001346 if (buf == NULL)
1347 return;
Takashi Iwai763f3562005-06-03 11:25:34 +02001348
1349 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1350 memset(buf, 0, n);
1351 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1352 }
1353}
1354
Adrian Knoth0dca1792011-01-26 19:32:14 +01001355static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
Takashi Iwai763f3562005-06-03 11:25:34 +02001356{
1357 int n;
1358
1359 spin_lock_irq(&s->lock);
1360
Adrian Knoth2e610272011-08-15 00:22:54 +02001361 if (32 == frames) {
1362 /* Special case for new RME cards like RayDAT/AIO which
1363 * support period sizes of 32 samples. Since latency is
1364 * encoded in the three bits of HDSP_LatencyMask, we can only
1365 * have values from 0 .. 7. While 0 still means 64 samples and
1366 * 6 represents 4096 samples on all cards, 7 represents 8192
1367 * on older cards and 32 samples on new cards.
1368 *
1369 * In other words, period size in samples is calculated by
1370 * 2^(n+6) with n ranging from 0 .. 7.
1371 */
1372 n = 7;
1373 } else {
1374 frames >>= 7;
1375 n = 0;
1376 while (frames) {
1377 n++;
1378 frames >>= 1;
1379 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001380 }
Adrian Knoth2e610272011-08-15 00:22:54 +02001381
Takashi Iwai763f3562005-06-03 11:25:34 +02001382 s->control_register &= ~HDSPM_LatencyMask;
1383 s->control_register |= hdspm_encode_latency(n);
1384
1385 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1386
1387 hdspm_compute_period_size(s);
1388
1389 spin_unlock_irq(&s->lock);
1390
1391 return 0;
1392}
1393
Adrian Knoth0dca1792011-01-26 19:32:14 +01001394static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1395{
1396 u64 freq_const;
1397
1398 if (period == 0)
1399 return 0;
1400
1401 switch (hdspm->io_type) {
1402 case MADI:
1403 case AES32:
1404 freq_const = 110069313433624ULL;
1405 break;
1406 case RayDAT:
1407 case AIO:
1408 freq_const = 104857600000000ULL;
1409 break;
1410 case MADIface:
1411 freq_const = 131072000000000ULL;
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001412 break;
1413 default:
1414 snd_BUG();
1415 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001416 }
1417
1418 return div_u64(freq_const, period);
1419}
1420
1421
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001422static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1423{
1424 u64 n;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001425
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001426 if (rate >= 112000)
1427 rate /= 4;
1428 else if (rate >= 56000)
1429 rate /= 2;
1430
Adrian Knoth0dca1792011-01-26 19:32:14 +01001431 switch (hdspm->io_type) {
1432 case MADIface:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001433 n = 131072000000000ULL; /* 125 MHz */
1434 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001435 case MADI:
1436 case AES32:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001437 n = 110069313433624ULL; /* 105 MHz */
1438 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001439 case RayDAT:
1440 case AIO:
Takashi Iwai3d56c8e2011-08-05 12:30:12 +02001441 n = 104857600000000ULL; /* 100 MHz */
1442 break;
1443 default:
1444 snd_BUG();
1445 return;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001446 }
1447
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001448 n = div_u64(n, rate);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001449 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001450 snd_BUG_ON(n >> 32);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001451 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1452}
Takashi Iwai763f3562005-06-03 11:25:34 +02001453
1454/* dummy set rate lets see what happens */
Takashi Iwai98274f02005-11-17 14:52:34 +01001455static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
Takashi Iwai763f3562005-06-03 11:25:34 +02001456{
Takashi Iwai763f3562005-06-03 11:25:34 +02001457 int current_rate;
1458 int rate_bits;
1459 int not_set = 0;
Remy Bruno65345992007-08-31 12:21:08 +02001460 int current_speed, target_speed;
Takashi Iwai763f3562005-06-03 11:25:34 +02001461
1462 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1463 it (e.g. during module initialization).
1464 */
1465
1466 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1467
Adrian Knoth0dca1792011-01-26 19:32:14 +01001468 /* SLAVE --- */
Takashi Iwai763f3562005-06-03 11:25:34 +02001469 if (called_internally) {
1470
Adrian Knoth0dca1792011-01-26 19:32:14 +01001471 /* request from ctl or card initialization
1472 just make a warning an remember setting
1473 for future master mode switching */
1474
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001475 snd_printk(KERN_WARNING "HDSPM: "
1476 "Warning: device is not running "
1477 "as a clock master.\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001478 not_set = 1;
1479 } else {
1480
1481 /* hw_param request while in AutoSync mode */
1482 int external_freq =
1483 hdspm_external_sample_rate(hdspm);
1484
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001485 if (hdspm_autosync_ref(hdspm) ==
1486 HDSPM_AUTOSYNC_FROM_NONE) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001487
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001488 snd_printk(KERN_WARNING "HDSPM: "
1489 "Detected no Externel Sync \n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001490 not_set = 1;
1491
1492 } else if (rate != external_freq) {
1493
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001494 snd_printk(KERN_WARNING "HDSPM: "
1495 "Warning: No AutoSync source for "
1496 "requested rate\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001497 not_set = 1;
1498 }
1499 }
1500 }
1501
1502 current_rate = hdspm->system_sample_rate;
1503
1504 /* Changing between Singe, Double and Quad speed is not
1505 allowed if any substreams are open. This is because such a change
1506 causes a shift in the location of the DMA buffers and a reduction
1507 in the number of available buffers.
1508
1509 Note that a similar but essentially insoluble problem exists for
1510 externally-driven rate changes. All we can do is to flag rate
Adrian Knoth0dca1792011-01-26 19:32:14 +01001511 changes in the read/write routines.
Takashi Iwai763f3562005-06-03 11:25:34 +02001512 */
1513
Remy Bruno65345992007-08-31 12:21:08 +02001514 if (current_rate <= 48000)
1515 current_speed = HDSPM_SPEED_SINGLE;
1516 else if (current_rate <= 96000)
1517 current_speed = HDSPM_SPEED_DOUBLE;
1518 else
1519 current_speed = HDSPM_SPEED_QUAD;
1520
1521 if (rate <= 48000)
1522 target_speed = HDSPM_SPEED_SINGLE;
1523 else if (rate <= 96000)
1524 target_speed = HDSPM_SPEED_DOUBLE;
1525 else
1526 target_speed = HDSPM_SPEED_QUAD;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001527
Takashi Iwai763f3562005-06-03 11:25:34 +02001528 switch (rate) {
1529 case 32000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001530 rate_bits = HDSPM_Frequency32KHz;
1531 break;
1532 case 44100:
Takashi Iwai763f3562005-06-03 11:25:34 +02001533 rate_bits = HDSPM_Frequency44_1KHz;
1534 break;
1535 case 48000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001536 rate_bits = HDSPM_Frequency48KHz;
1537 break;
1538 case 64000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001539 rate_bits = HDSPM_Frequency64KHz;
1540 break;
1541 case 88200:
Takashi Iwai763f3562005-06-03 11:25:34 +02001542 rate_bits = HDSPM_Frequency88_2KHz;
1543 break;
1544 case 96000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001545 rate_bits = HDSPM_Frequency96KHz;
1546 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001547 case 128000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001548 rate_bits = HDSPM_Frequency128KHz;
1549 break;
1550 case 176400:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001551 rate_bits = HDSPM_Frequency176_4KHz;
1552 break;
1553 case 192000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001554 rate_bits = HDSPM_Frequency192KHz;
1555 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001556 default:
1557 return -EINVAL;
1558 }
1559
Remy Bruno65345992007-08-31 12:21:08 +02001560 if (current_speed != target_speed
Takashi Iwai763f3562005-06-03 11:25:34 +02001561 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1562 snd_printk
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001563 (KERN_ERR "HDSPM: "
Remy Bruno65345992007-08-31 12:21:08 +02001564 "cannot change from %s speed to %s speed mode "
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001565 "(capture PID = %d, playback PID = %d)\n",
Remy Bruno65345992007-08-31 12:21:08 +02001566 hdspm_speed_names[current_speed],
1567 hdspm_speed_names[target_speed],
Takashi Iwai763f3562005-06-03 11:25:34 +02001568 hdspm->capture_pid, hdspm->playback_pid);
1569 return -EBUSY;
1570 }
1571
1572 hdspm->control_register &= ~HDSPM_FrequencyMask;
1573 hdspm->control_register |= rate_bits;
1574 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1575
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001576 /* For AES32, need to set DDS value in FREQ register
1577 For MADI, also apparently */
1578 hdspm_set_dds_value(hdspm, rate);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001579
1580 if (AES32 == hdspm->io_type && rate != current_rate)
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001581 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
Takashi Iwai763f3562005-06-03 11:25:34 +02001582
1583 hdspm->system_sample_rate = rate;
1584
Adrian Knoth0dca1792011-01-26 19:32:14 +01001585 if (rate <= 48000) {
1586 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1587 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1588 hdspm->max_channels_in = hdspm->ss_in_channels;
1589 hdspm->max_channels_out = hdspm->ss_out_channels;
1590 hdspm->port_names_in = hdspm->port_names_in_ss;
1591 hdspm->port_names_out = hdspm->port_names_out_ss;
1592 } else if (rate <= 96000) {
1593 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1594 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1595 hdspm->max_channels_in = hdspm->ds_in_channels;
1596 hdspm->max_channels_out = hdspm->ds_out_channels;
1597 hdspm->port_names_in = hdspm->port_names_in_ds;
1598 hdspm->port_names_out = hdspm->port_names_out_ds;
1599 } else {
1600 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1601 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1602 hdspm->max_channels_in = hdspm->qs_in_channels;
1603 hdspm->max_channels_out = hdspm->qs_out_channels;
1604 hdspm->port_names_in = hdspm->port_names_in_qs;
1605 hdspm->port_names_out = hdspm->port_names_out_qs;
1606 }
1607
Takashi Iwai763f3562005-06-03 11:25:34 +02001608 if (not_set != 0)
1609 return -1;
1610
1611 return 0;
1612}
1613
1614/* mainly for init to 0 on load */
Takashi Iwai98274f02005-11-17 14:52:34 +01001615static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
Takashi Iwai763f3562005-06-03 11:25:34 +02001616{
1617 int i, j;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001618 unsigned int gain;
1619
1620 if (sgain > UNITY_GAIN)
1621 gain = UNITY_GAIN;
1622 else if (sgain < 0)
1623 gain = 0;
1624 else
1625 gain = sgain;
Takashi Iwai763f3562005-06-03 11:25:34 +02001626
1627 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1628 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1629 hdspm_write_in_gain(hdspm, i, j, gain);
1630 hdspm_write_pb_gain(hdspm, i, j, gain);
1631 }
1632}
1633
1634/*----------------------------------------------------------------------------
1635 MIDI
1636 ----------------------------------------------------------------------------*/
1637
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001638static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1639 int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001640{
1641 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001642 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
Takashi Iwai763f3562005-06-03 11:25:34 +02001643}
1644
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001645static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1646 int val)
Takashi Iwai763f3562005-06-03 11:25:34 +02001647{
1648 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001649 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
Takashi Iwai763f3562005-06-03 11:25:34 +02001650}
1651
Takashi Iwai98274f02005-11-17 14:52:34 +01001652static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001653{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001654 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001655}
1656
Takashi Iwai98274f02005-11-17 14:52:34 +01001657static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001658{
1659 int fifo_bytes_used;
1660
Adrian Knoth0dca1792011-01-26 19:32:14 +01001661 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001662
1663 if (fifo_bytes_used < 128)
1664 return 128 - fifo_bytes_used;
1665 else
1666 return 0;
1667}
1668
Denys Vlasenko62cef822008-04-14 13:04:18 +02001669static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001670{
1671 while (snd_hdspm_midi_input_available (hdspm, id))
1672 snd_hdspm_midi_read_byte (hdspm, id);
1673}
1674
Takashi Iwai98274f02005-11-17 14:52:34 +01001675static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001676{
1677 unsigned long flags;
1678 int n_pending;
1679 int to_write;
1680 int i;
1681 unsigned char buf[128];
1682
1683 /* Output is not interrupt driven */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001684
Takashi Iwai763f3562005-06-03 11:25:34 +02001685 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001686 if (hmidi->output &&
1687 !snd_rawmidi_transmit_empty (hmidi->output)) {
1688 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1689 hmidi->id);
1690 if (n_pending > 0) {
1691 if (n_pending > (int)sizeof (buf))
1692 n_pending = sizeof (buf);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001693
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001694 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1695 n_pending);
1696 if (to_write > 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001697 for (i = 0; i < to_write; ++i)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001698 snd_hdspm_midi_write_byte (hmidi->hdspm,
1699 hmidi->id,
1700 buf[i]);
Takashi Iwai763f3562005-06-03 11:25:34 +02001701 }
1702 }
1703 }
1704 spin_unlock_irqrestore (&hmidi->lock, flags);
1705 return 0;
1706}
1707
Takashi Iwai98274f02005-11-17 14:52:34 +01001708static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001709{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001710 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1711 * input FIFO size
1712 */
Takashi Iwai763f3562005-06-03 11:25:34 +02001713 unsigned long flags;
1714 int n_pending;
1715 int i;
1716
1717 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001718 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1719 if (n_pending > 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001720 if (hmidi->input) {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001721 if (n_pending > (int)sizeof (buf))
Takashi Iwai763f3562005-06-03 11:25:34 +02001722 n_pending = sizeof (buf);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001723 for (i = 0; i < n_pending; ++i)
1724 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1725 hmidi->id);
1726 if (n_pending)
1727 snd_rawmidi_receive (hmidi->input, buf,
1728 n_pending);
Takashi Iwai763f3562005-06-03 11:25:34 +02001729 } else {
1730 /* flush the MIDI input FIFO */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001731 while (n_pending--)
1732 snd_hdspm_midi_read_byte (hmidi->hdspm,
1733 hmidi->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02001734 }
1735 }
1736 hmidi->pending = 0;
Adrian Knothc0da0012011-06-12 17:26:17 +02001737 spin_unlock_irqrestore(&hmidi->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001738
Adrian Knothc0da0012011-06-12 17:26:17 +02001739 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001740 hmidi->hdspm->control_register |= hmidi->ie;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001741 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1742 hmidi->hdspm->control_register);
Adrian Knothc0da0012011-06-12 17:26:17 +02001743 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001744
Takashi Iwai763f3562005-06-03 11:25:34 +02001745 return snd_hdspm_midi_output_write (hmidi);
1746}
1747
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001748static void
1749snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001750{
Takashi Iwai98274f02005-11-17 14:52:34 +01001751 struct hdspm *hdspm;
1752 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001753 unsigned long flags;
Takashi Iwai763f3562005-06-03 11:25:34 +02001754
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001755 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001756 hdspm = hmidi->hdspm;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001757
Takashi Iwai763f3562005-06-03 11:25:34 +02001758 spin_lock_irqsave (&hdspm->lock, flags);
1759 if (up) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001760 if (!(hdspm->control_register & hmidi->ie)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001761 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001762 hdspm->control_register |= hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001763 }
1764 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001765 hdspm->control_register &= ~hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001766 }
1767
1768 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1769 spin_unlock_irqrestore (&hdspm->lock, flags);
1770}
1771
1772static void snd_hdspm_midi_output_timer(unsigned long data)
1773{
Takashi Iwai98274f02005-11-17 14:52:34 +01001774 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001775 unsigned long flags;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001776
Takashi Iwai763f3562005-06-03 11:25:34 +02001777 snd_hdspm_midi_output_write(hmidi);
1778 spin_lock_irqsave (&hmidi->lock, flags);
1779
1780 /* this does not bump hmidi->istimer, because the
1781 kernel automatically removed the timer when it
1782 expired, and we are now adding it back, thus
Adrian Knoth0dca1792011-01-26 19:32:14 +01001783 leaving istimer wherever it was set before.
Takashi Iwai763f3562005-06-03 11:25:34 +02001784 */
1785
1786 if (hmidi->istimer) {
1787 hmidi->timer.expires = 1 + jiffies;
1788 add_timer(&hmidi->timer);
1789 }
1790
1791 spin_unlock_irqrestore (&hmidi->lock, flags);
1792}
1793
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001794static void
1795snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001796{
Takashi Iwai98274f02005-11-17 14:52:34 +01001797 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001798 unsigned long flags;
1799
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001800 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001801 spin_lock_irqsave (&hmidi->lock, flags);
1802 if (up) {
1803 if (!hmidi->istimer) {
1804 init_timer(&hmidi->timer);
1805 hmidi->timer.function = snd_hdspm_midi_output_timer;
1806 hmidi->timer.data = (unsigned long) hmidi;
1807 hmidi->timer.expires = 1 + jiffies;
1808 add_timer(&hmidi->timer);
1809 hmidi->istimer++;
1810 }
1811 } else {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001812 if (hmidi->istimer && --hmidi->istimer <= 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02001813 del_timer (&hmidi->timer);
Takashi Iwai763f3562005-06-03 11:25:34 +02001814 }
1815 spin_unlock_irqrestore (&hmidi->lock, flags);
1816 if (up)
1817 snd_hdspm_midi_output_write(hmidi);
1818}
1819
Takashi Iwai98274f02005-11-17 14:52:34 +01001820static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001821{
Takashi Iwai98274f02005-11-17 14:52:34 +01001822 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001823
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001824 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001825 spin_lock_irq (&hmidi->lock);
1826 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1827 hmidi->input = substream;
1828 spin_unlock_irq (&hmidi->lock);
1829
1830 return 0;
1831}
1832
Takashi Iwai98274f02005-11-17 14:52:34 +01001833static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001834{
Takashi Iwai98274f02005-11-17 14:52:34 +01001835 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001836
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001837 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001838 spin_lock_irq (&hmidi->lock);
1839 hmidi->output = substream;
1840 spin_unlock_irq (&hmidi->lock);
1841
1842 return 0;
1843}
1844
Takashi Iwai98274f02005-11-17 14:52:34 +01001845static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001846{
Takashi Iwai98274f02005-11-17 14:52:34 +01001847 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001848
1849 snd_hdspm_midi_input_trigger (substream, 0);
1850
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001851 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001852 spin_lock_irq (&hmidi->lock);
1853 hmidi->input = NULL;
1854 spin_unlock_irq (&hmidi->lock);
1855
1856 return 0;
1857}
1858
Takashi Iwai98274f02005-11-17 14:52:34 +01001859static int snd_hdspm_midi_output_close(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
1863 snd_hdspm_midi_output_trigger (substream, 0);
1864
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001865 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001866 spin_lock_irq (&hmidi->lock);
1867 hmidi->output = NULL;
1868 spin_unlock_irq (&hmidi->lock);
1869
1870 return 0;
1871}
1872
Takashi Iwai98274f02005-11-17 14:52:34 +01001873static struct snd_rawmidi_ops snd_hdspm_midi_output =
Takashi Iwai763f3562005-06-03 11:25:34 +02001874{
1875 .open = snd_hdspm_midi_output_open,
1876 .close = snd_hdspm_midi_output_close,
1877 .trigger = snd_hdspm_midi_output_trigger,
1878};
1879
Takashi Iwai98274f02005-11-17 14:52:34 +01001880static struct snd_rawmidi_ops snd_hdspm_midi_input =
Takashi Iwai763f3562005-06-03 11:25:34 +02001881{
1882 .open = snd_hdspm_midi_input_open,
1883 .close = snd_hdspm_midi_input_close,
1884 .trigger = snd_hdspm_midi_input_trigger,
1885};
1886
Bill Pembertone23e7a12012-12-06 12:35:10 -05001887static int snd_hdspm_create_midi(struct snd_card *card,
1888 struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001889{
1890 int err;
1891 char buf[32];
1892
1893 hdspm->midi[id].id = id;
Takashi Iwai763f3562005-06-03 11:25:34 +02001894 hdspm->midi[id].hdspm = hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +02001895 spin_lock_init (&hdspm->midi[id].lock);
1896
Adrian Knoth0dca1792011-01-26 19:32:14 +01001897 if (0 == id) {
1898 if (MADIface == hdspm->io_type) {
1899 /* MIDI-over-MADI on HDSPe MADIface */
1900 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1901 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1902 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1903 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1904 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1905 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1906 } else {
1907 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1908 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1909 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1910 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1911 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
1912 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
1913 }
1914 } else if (1 == id) {
1915 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
1916 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
1917 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
1918 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
1919 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
1920 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
1921 } else if ((2 == id) && (MADI == hdspm->io_type)) {
1922 /* MIDI-over-MADI on HDSPe MADI */
1923 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1924 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1925 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
1926 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
1927 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1928 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
1929 } else if (2 == id) {
1930 /* TCO MTC, read only */
1931 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1932 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1933 hdspm->midi[2].dataOut = -1;
1934 hdspm->midi[2].statusOut = -1;
1935 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1936 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
1937 } else if (3 == id) {
1938 /* TCO MTC on HDSPe MADI */
1939 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
1940 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
1941 hdspm->midi[3].dataOut = -1;
1942 hdspm->midi[3].statusOut = -1;
1943 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
1944 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
1945 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001946
Adrian Knoth0dca1792011-01-26 19:32:14 +01001947 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
1948 (MADIface == hdspm->io_type)))) {
1949 if ((id == 0) && (MADIface == hdspm->io_type)) {
1950 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1951 } else if ((id == 2) && (MADI == hdspm->io_type)) {
1952 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1953 } else {
1954 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
1955 }
1956 err = snd_rawmidi_new(card, buf, id, 1, 1,
1957 &hdspm->midi[id].rmidi);
1958 if (err < 0)
1959 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02001960
Adrian Knoth0dca1792011-01-26 19:32:14 +01001961 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
1962 card->id, id+1);
1963 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
Takashi Iwai763f3562005-06-03 11:25:34 +02001964
Adrian Knoth0dca1792011-01-26 19:32:14 +01001965 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1966 SNDRV_RAWMIDI_STREAM_OUTPUT,
1967 &snd_hdspm_midi_output);
1968 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1969 SNDRV_RAWMIDI_STREAM_INPUT,
1970 &snd_hdspm_midi_input);
1971
1972 hdspm->midi[id].rmidi->info_flags |=
1973 SNDRV_RAWMIDI_INFO_OUTPUT |
1974 SNDRV_RAWMIDI_INFO_INPUT |
1975 SNDRV_RAWMIDI_INFO_DUPLEX;
1976 } else {
1977 /* TCO MTC, read only */
1978 sprintf(buf, "%s MTC %d", card->shortname, id+1);
1979 err = snd_rawmidi_new(card, buf, id, 1, 1,
1980 &hdspm->midi[id].rmidi);
1981 if (err < 0)
1982 return err;
1983
1984 sprintf(hdspm->midi[id].rmidi->name,
1985 "%s MTC %d", card->id, id+1);
1986 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
1987
1988 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1989 SNDRV_RAWMIDI_STREAM_INPUT,
1990 &snd_hdspm_midi_input);
1991
1992 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
1993 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001994
1995 return 0;
1996}
1997
1998
1999static void hdspm_midi_tasklet(unsigned long arg)
2000{
Takashi Iwai98274f02005-11-17 14:52:34 +01002001 struct hdspm *hdspm = (struct hdspm *)arg;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002002 int i = 0;
2003
2004 while (i < hdspm->midiPorts) {
2005 if (hdspm->midi[i].pending)
2006 snd_hdspm_midi_input_read(&hdspm->midi[i]);
2007
2008 i++;
2009 }
2010}
Takashi Iwai763f3562005-06-03 11:25:34 +02002011
2012
2013/*-----------------------------------------------------------------------------
2014 Status Interface
2015 ----------------------------------------------------------------------------*/
2016
2017/* get the system sample rate which is set */
2018
Adrian Knoth0dca1792011-01-26 19:32:14 +01002019
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002020static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
2021{
2022 unsigned int period, rate;
2023
2024 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
2025 rate = hdspm_calc_dds_value(hdspm, period);
2026
2027 return rate;
2028}
2029
Adrian Knoth0dca1792011-01-26 19:32:14 +01002030/**
2031 * Calculate the real sample rate from the
2032 * current DDS value.
2033 **/
2034static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
2035{
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002036 unsigned int rate;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002037
Adrian Knoth3f7bf912013-03-10 00:37:21 +01002038 rate = hdspm_get_pll_freq(hdspm);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002039
Adrian Knotha97bda72012-05-30 14:23:18 +02002040 if (rate > 207000) {
Adrian Knoth21a164d2012-10-19 17:42:23 +02002041 /* Unreasonable high sample rate as seen on PCI MADI cards. */
2042 if (0 == hdspm_system_clock_mode(hdspm)) {
2043 /* master mode, return internal sample rate */
2044 rate = hdspm->system_sample_rate;
2045 } else {
2046 /* slave mode, return external sample rate */
2047 rate = hdspm_external_sample_rate(hdspm);
2048 }
Adrian Knotha97bda72012-05-30 14:23:18 +02002049 }
2050
Adrian Knoth0dca1792011-01-26 19:32:14 +01002051 return rate;
2052}
2053
2054
Takashi Iwai763f3562005-06-03 11:25:34 +02002055#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002056{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2057 .name = xname, \
2058 .index = xindex, \
2059 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2060 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2061 .info = snd_hdspm_info_system_sample_rate, \
2062 .put = snd_hdspm_put_system_sample_rate, \
2063 .get = snd_hdspm_get_system_sample_rate \
Takashi Iwai763f3562005-06-03 11:25:34 +02002064}
2065
Takashi Iwai98274f02005-11-17 14:52:34 +01002066static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002068{
2069 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2070 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002071 uinfo->value.integer.min = 27000;
2072 uinfo->value.integer.max = 207000;
2073 uinfo->value.integer.step = 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002074 return 0;
2075}
2076
Adrian Knoth0dca1792011-01-26 19:32:14 +01002077
Takashi Iwai98274f02005-11-17 14:52:34 +01002078static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
2079 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002080 ucontrol)
2081{
Takashi Iwai98274f02005-11-17 14:52:34 +01002082 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002083
Adrian Knoth0dca1792011-01-26 19:32:14 +01002084 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002085 return 0;
2086}
2087
Adrian Knoth41285a92012-10-19 17:42:22 +02002088static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_value *
2090 ucontrol)
2091{
2092 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2093
2094 hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
2095 return 0;
2096}
2097
Adrian Knoth0dca1792011-01-26 19:32:14 +01002098
2099/**
2100 * Returns the WordClock sample rate class for the given card.
2101 **/
2102static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
2103{
2104 int status;
2105
2106 switch (hdspm->io_type) {
2107 case RayDAT:
2108 case AIO:
2109 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2110 return (status >> 16) & 0xF;
2111 break;
2112 default:
2113 break;
2114 }
2115
2116
2117 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002118}
2119
Adrian Knoth0dca1792011-01-26 19:32:14 +01002120
2121/**
2122 * Returns the TCO sample rate class for the given card.
2123 **/
2124static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2125{
2126 int status;
2127
2128 if (hdspm->tco) {
2129 switch (hdspm->io_type) {
2130 case RayDAT:
2131 case AIO:
2132 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2133 return (status >> 20) & 0xF;
2134 break;
2135 default:
2136 break;
2137 }
2138 }
2139
2140 return 0;
2141}
2142
2143
2144/**
2145 * Returns the SYNC_IN sample rate class for the given card.
2146 **/
2147static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2148{
2149 int status;
2150
2151 if (hdspm->tco) {
2152 switch (hdspm->io_type) {
2153 case RayDAT:
2154 case AIO:
2155 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2156 return (status >> 12) & 0xF;
2157 break;
2158 default:
2159 break;
2160 }
2161 }
2162
2163 return 0;
2164}
2165
2166
2167/**
2168 * Returns the sample rate class for input source <idx> for
2169 * 'new style' cards like the AIO and RayDAT.
2170 **/
2171static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2172{
2173 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2174
2175 return (status >> (idx*4)) & 0xF;
2176}
2177
2178
2179
2180#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2181{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2182 .name = xname, \
2183 .private_value = xindex, \
2184 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2185 .info = snd_hdspm_info_autosync_sample_rate, \
2186 .get = snd_hdspm_get_autosync_sample_rate \
2187}
2188
2189
Takashi Iwai98274f02005-11-17 14:52:34 +01002190static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002192{
Takashi Iwai763f3562005-06-03 11:25:34 +02002193 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2194 uinfo->count = 1;
2195 uinfo->value.enumerated.items = 10;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002196
Takashi Iwai763f3562005-06-03 11:25:34 +02002197 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
Adrian Knoth0dca1792011-01-26 19:32:14 +01002198 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002199 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002200 texts_freq[uinfo->value.enumerated.item]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002201 return 0;
2202}
2203
Adrian Knoth0dca1792011-01-26 19:32:14 +01002204
Takashi Iwai98274f02005-11-17 14:52:34 +01002205static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002207 ucontrol)
2208{
Takashi Iwai98274f02005-11-17 14:52:34 +01002209 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002210
Adrian Knoth0dca1792011-01-26 19:32:14 +01002211 switch (hdspm->io_type) {
2212 case RayDAT:
2213 switch (kcontrol->private_value) {
2214 case 0:
2215 ucontrol->value.enumerated.item[0] =
2216 hdspm_get_wc_sample_rate(hdspm);
2217 break;
2218 case 7:
2219 ucontrol->value.enumerated.item[0] =
2220 hdspm_get_tco_sample_rate(hdspm);
2221 break;
2222 case 8:
2223 ucontrol->value.enumerated.item[0] =
2224 hdspm_get_sync_in_sample_rate(hdspm);
2225 break;
2226 default:
2227 ucontrol->value.enumerated.item[0] =
2228 hdspm_get_s1_sample_rate(hdspm,
2229 kcontrol->private_value-1);
2230 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002231 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002232
Adrian Knoth0dca1792011-01-26 19:32:14 +01002233 case AIO:
2234 switch (kcontrol->private_value) {
2235 case 0: /* WC */
2236 ucontrol->value.enumerated.item[0] =
2237 hdspm_get_wc_sample_rate(hdspm);
2238 break;
2239 case 4: /* TCO */
2240 ucontrol->value.enumerated.item[0] =
2241 hdspm_get_tco_sample_rate(hdspm);
2242 break;
2243 case 5: /* SYNC_IN */
2244 ucontrol->value.enumerated.item[0] =
2245 hdspm_get_sync_in_sample_rate(hdspm);
2246 break;
2247 default:
2248 ucontrol->value.enumerated.item[0] =
2249 hdspm_get_s1_sample_rate(hdspm,
2250 ucontrol->id.index-1);
2251 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002252 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002253
2254 case AES32:
2255
2256 switch (kcontrol->private_value) {
2257 case 0: /* WC */
2258 ucontrol->value.enumerated.item[0] =
2259 hdspm_get_wc_sample_rate(hdspm);
2260 break;
2261 case 9: /* TCO */
2262 ucontrol->value.enumerated.item[0] =
2263 hdspm_get_tco_sample_rate(hdspm);
2264 break;
2265 case 10: /* SYNC_IN */
2266 ucontrol->value.enumerated.item[0] =
2267 hdspm_get_sync_in_sample_rate(hdspm);
2268 break;
2269 default: /* AES1 to AES8 */
2270 ucontrol->value.enumerated.item[0] =
2271 hdspm_get_s1_sample_rate(hdspm,
2272 kcontrol->private_value-1);
2273 break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002274 }
Adrian Knothd681dea2012-10-19 17:42:25 +02002275 break;
Adrian Knothb8812c52012-10-19 17:42:26 +02002276
2277 case MADI:
2278 case MADIface:
2279 {
2280 int rate = hdspm_external_sample_rate(hdspm);
2281 int i, selected_rate = 0;
2282 for (i = 1; i < 10; i++)
2283 if (HDSPM_bit2freq(i) == rate) {
2284 selected_rate = i;
2285 break;
2286 }
2287 ucontrol->value.enumerated.item[0] = selected_rate;
2288 }
2289 break;
2290
Takashi Iwai763f3562005-06-03 11:25:34 +02002291 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002292 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002293 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002294
Takashi Iwai763f3562005-06-03 11:25:34 +02002295 return 0;
2296}
2297
Adrian Knoth0dca1792011-01-26 19:32:14 +01002298
Takashi Iwai763f3562005-06-03 11:25:34 +02002299#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002300{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2301 .name = xname, \
2302 .index = xindex, \
2303 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2304 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2305 .info = snd_hdspm_info_system_clock_mode, \
2306 .get = snd_hdspm_get_system_clock_mode, \
2307 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002308}
2309
2310
Adrian Knoth0dca1792011-01-26 19:32:14 +01002311/**
2312 * Returns the system clock mode for the given card.
2313 * @returns 0 - master, 1 - slave
2314 **/
2315static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002316{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002317 switch (hdspm->io_type) {
2318 case AIO:
2319 case RayDAT:
2320 if (hdspm->settings_register & HDSPM_c0Master)
2321 return 0;
2322 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002323
Adrian Knoth0dca1792011-01-26 19:32:14 +01002324 default:
2325 if (hdspm->control_register & HDSPM_ClockModeMaster)
2326 return 0;
2327 }
2328
Takashi Iwai763f3562005-06-03 11:25:34 +02002329 return 1;
2330}
2331
Adrian Knoth0dca1792011-01-26 19:32:14 +01002332
2333/**
2334 * Sets the system clock mode.
2335 * @param mode 0 - master, 1 - slave
2336 **/
2337static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2338{
2339 switch (hdspm->io_type) {
2340 case AIO:
2341 case RayDAT:
2342 if (0 == mode)
2343 hdspm->settings_register |= HDSPM_c0Master;
2344 else
2345 hdspm->settings_register &= ~HDSPM_c0Master;
2346
2347 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2348 break;
2349
2350 default:
2351 if (0 == mode)
2352 hdspm->control_register |= HDSPM_ClockModeMaster;
2353 else
2354 hdspm->control_register &= ~HDSPM_ClockModeMaster;
2355
2356 hdspm_write(hdspm, HDSPM_controlRegister,
2357 hdspm->control_register);
2358 }
2359}
2360
2361
Takashi Iwai98274f02005-11-17 14:52:34 +01002362static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002364{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002365 static char *texts[] = { "Master", "AutoSync" };
Takashi Iwai763f3562005-06-03 11:25:34 +02002366
2367 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2368 uinfo->count = 1;
2369 uinfo->value.enumerated.items = 2;
2370 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2371 uinfo->value.enumerated.item =
2372 uinfo->value.enumerated.items - 1;
2373 strcpy(uinfo->value.enumerated.name,
2374 texts[uinfo->value.enumerated.item]);
2375 return 0;
2376}
2377
Takashi Iwai98274f02005-11-17 14:52:34 +01002378static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002380{
Takashi Iwai98274f02005-11-17 14:52:34 +01002381 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002382
Adrian Knoth0dca1792011-01-26 19:32:14 +01002383 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002384 return 0;
2385}
2386
Adrian Knoth0dca1792011-01-26 19:32:14 +01002387static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2391 int val;
2392
2393 if (!snd_hdspm_use_is_exclusive(hdspm))
2394 return -EBUSY;
2395
2396 val = ucontrol->value.enumerated.item[0];
2397 if (val < 0)
2398 val = 0;
2399 else if (val > 1)
2400 val = 1;
2401
2402 hdspm_set_system_clock_mode(hdspm, val);
2403
2404 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002405}
2406
Adrian Knoth0dca1792011-01-26 19:32:14 +01002407
2408#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2409{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2410 .name = xname, \
2411 .index = xindex, \
2412 .info = snd_hdspm_info_clock_source, \
2413 .get = snd_hdspm_get_clock_source, \
2414 .put = snd_hdspm_put_clock_source \
2415}
2416
2417
Takashi Iwai98274f02005-11-17 14:52:34 +01002418static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002419{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002420 switch (hdspm->system_sample_rate) {
2421 case 32000: return 0;
2422 case 44100: return 1;
2423 case 48000: return 2;
2424 case 64000: return 3;
2425 case 88200: return 4;
2426 case 96000: return 5;
2427 case 128000: return 6;
2428 case 176400: return 7;
2429 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002430 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002431
2432 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002433}
2434
Takashi Iwai98274f02005-11-17 14:52:34 +01002435static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002436{
2437 int rate;
2438 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002439 case 0:
2440 rate = 32000; break;
2441 case 1:
2442 rate = 44100; break;
2443 case 2:
2444 rate = 48000; break;
2445 case 3:
2446 rate = 64000; break;
2447 case 4:
2448 rate = 88200; break;
2449 case 5:
2450 rate = 96000; break;
2451 case 6:
2452 rate = 128000; break;
2453 case 7:
2454 rate = 176400; break;
2455 case 8:
2456 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002457 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002458 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002459 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002460 hdspm_set_rate(hdspm, rate, 1);
2461 return 0;
2462}
2463
Takashi Iwai98274f02005-11-17 14:52:34 +01002464static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002466{
Takashi Iwai763f3562005-06-03 11:25:34 +02002467 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2468 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002469 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002470
2471 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2472 uinfo->value.enumerated.item =
2473 uinfo->value.enumerated.items - 1;
2474
2475 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002476 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002477
2478 return 0;
2479}
2480
Takashi Iwai98274f02005-11-17 14:52:34 +01002481static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2482 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002483{
Takashi Iwai98274f02005-11-17 14:52:34 +01002484 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002485
2486 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2487 return 0;
2488}
2489
Takashi Iwai98274f02005-11-17 14:52:34 +01002490static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002492{
Takashi Iwai98274f02005-11-17 14:52:34 +01002493 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002494 int change;
2495 int val;
2496
2497 if (!snd_hdspm_use_is_exclusive(hdspm))
2498 return -EBUSY;
2499 val = ucontrol->value.enumerated.item[0];
2500 if (val < 0)
2501 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002502 if (val > 9)
2503 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002504 spin_lock_irq(&hdspm->lock);
2505 if (val != hdspm_clock_source(hdspm))
2506 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2507 else
2508 change = 0;
2509 spin_unlock_irq(&hdspm->lock);
2510 return change;
2511}
2512
Adrian Knoth0dca1792011-01-26 19:32:14 +01002513
Takashi Iwai763f3562005-06-03 11:25:34 +02002514#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002515{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002516 .name = xname, \
2517 .index = xindex, \
2518 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2519 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2520 .info = snd_hdspm_info_pref_sync_ref, \
2521 .get = snd_hdspm_get_pref_sync_ref, \
2522 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002523}
2524
Adrian Knoth0dca1792011-01-26 19:32:14 +01002525
2526/**
2527 * Returns the current preferred sync reference setting.
2528 * The semantics of the return value are depending on the
2529 * card, please see the comments for clarification.
2530 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002531static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002532{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002533 switch (hdspm->io_type) {
2534 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002535 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002536 case 0: return 0; /* WC */
2537 case HDSPM_SyncRef0: return 1; /* AES 1 */
2538 case HDSPM_SyncRef1: return 2; /* AES 2 */
2539 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2540 case HDSPM_SyncRef2: return 4; /* AES 4 */
2541 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2542 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2543 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2544 return 7; /* AES 7 */
2545 case HDSPM_SyncRef3: return 8; /* AES 8 */
2546 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002547 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002548 break;
2549
2550 case MADI:
2551 case MADIface:
2552 if (hdspm->tco) {
2553 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2554 case 0: return 0; /* WC */
2555 case HDSPM_SyncRef0: return 1; /* MADI */
2556 case HDSPM_SyncRef1: return 2; /* TCO */
2557 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2558 return 3; /* SYNC_IN */
2559 }
2560 } else {
2561 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2562 case 0: return 0; /* WC */
2563 case HDSPM_SyncRef0: return 1; /* MADI */
2564 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2565 return 2; /* SYNC_IN */
2566 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002567 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002568 break;
2569
2570 case RayDAT:
2571 if (hdspm->tco) {
2572 switch ((hdspm->settings_register &
2573 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2574 case 0: return 0; /* WC */
2575 case 3: return 1; /* ADAT 1 */
2576 case 4: return 2; /* ADAT 2 */
2577 case 5: return 3; /* ADAT 3 */
2578 case 6: return 4; /* ADAT 4 */
2579 case 1: return 5; /* AES */
2580 case 2: return 6; /* SPDIF */
2581 case 9: return 7; /* TCO */
2582 case 10: return 8; /* SYNC_IN */
2583 }
2584 } else {
2585 switch ((hdspm->settings_register &
2586 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2587 case 0: return 0; /* WC */
2588 case 3: return 1; /* ADAT 1 */
2589 case 4: return 2; /* ADAT 2 */
2590 case 5: return 3; /* ADAT 3 */
2591 case 6: return 4; /* ADAT 4 */
2592 case 1: return 5; /* AES */
2593 case 2: return 6; /* SPDIF */
2594 case 10: return 7; /* SYNC_IN */
2595 }
2596 }
2597
2598 break;
2599
2600 case AIO:
2601 if (hdspm->tco) {
2602 switch ((hdspm->settings_register &
2603 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2604 case 0: return 0; /* WC */
2605 case 3: return 1; /* ADAT */
2606 case 1: return 2; /* AES */
2607 case 2: return 3; /* SPDIF */
2608 case 9: return 4; /* TCO */
2609 case 10: return 5; /* SYNC_IN */
2610 }
2611 } else {
2612 switch ((hdspm->settings_register &
2613 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2614 case 0: return 0; /* WC */
2615 case 3: return 1; /* ADAT */
2616 case 1: return 2; /* AES */
2617 case 2: return 3; /* SPDIF */
2618 case 10: return 4; /* SYNC_IN */
2619 }
2620 }
2621
2622 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002623 }
2624
Adrian Knoth0dca1792011-01-26 19:32:14 +01002625 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002626}
2627
Adrian Knoth0dca1792011-01-26 19:32:14 +01002628
2629/**
2630 * Set the preferred sync reference to <pref>. The semantics
2631 * of <pref> are depending on the card type, see the comments
2632 * for clarification.
2633 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002634static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002635{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002636 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002637
Adrian Knoth0dca1792011-01-26 19:32:14 +01002638 switch (hdspm->io_type) {
2639 case AES32:
2640 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002641 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002642 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002643 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002644 case 1: /* AES 1 */
2645 hdspm->control_register |= HDSPM_SyncRef0;
2646 break;
2647 case 2: /* AES 2 */
2648 hdspm->control_register |= HDSPM_SyncRef1;
2649 break;
2650 case 3: /* AES 3 */
2651 hdspm->control_register |=
2652 HDSPM_SyncRef1+HDSPM_SyncRef0;
2653 break;
2654 case 4: /* AES 4 */
2655 hdspm->control_register |= HDSPM_SyncRef2;
2656 break;
2657 case 5: /* AES 5 */
2658 hdspm->control_register |=
2659 HDSPM_SyncRef2+HDSPM_SyncRef0;
2660 break;
2661 case 6: /* AES 6 */
2662 hdspm->control_register |=
2663 HDSPM_SyncRef2+HDSPM_SyncRef1;
2664 break;
2665 case 7: /* AES 7 */
2666 hdspm->control_register |=
2667 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2668 break;
2669 case 8: /* AES 8 */
2670 hdspm->control_register |= HDSPM_SyncRef3;
2671 break;
2672 case 9: /* TCO */
2673 hdspm->control_register |=
2674 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002675 break;
2676 default:
2677 return -1;
2678 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002679
2680 break;
2681
2682 case MADI:
2683 case MADIface:
2684 hdspm->control_register &= ~HDSPM_SyncRefMask;
2685 if (hdspm->tco) {
2686 switch (pref) {
2687 case 0: /* WC */
2688 break;
2689 case 1: /* MADI */
2690 hdspm->control_register |= HDSPM_SyncRef0;
2691 break;
2692 case 2: /* TCO */
2693 hdspm->control_register |= HDSPM_SyncRef1;
2694 break;
2695 case 3: /* SYNC_IN */
2696 hdspm->control_register |=
2697 HDSPM_SyncRef0+HDSPM_SyncRef1;
2698 break;
2699 default:
2700 return -1;
2701 }
2702 } else {
2703 switch (pref) {
2704 case 0: /* WC */
2705 break;
2706 case 1: /* MADI */
2707 hdspm->control_register |= HDSPM_SyncRef0;
2708 break;
2709 case 2: /* SYNC_IN */
2710 hdspm->control_register |=
2711 HDSPM_SyncRef0+HDSPM_SyncRef1;
2712 break;
2713 default:
2714 return -1;
2715 }
2716 }
2717
2718 break;
2719
2720 case RayDAT:
2721 if (hdspm->tco) {
2722 switch (pref) {
2723 case 0: p = 0; break; /* WC */
2724 case 1: p = 3; break; /* ADAT 1 */
2725 case 2: p = 4; break; /* ADAT 2 */
2726 case 3: p = 5; break; /* ADAT 3 */
2727 case 4: p = 6; break; /* ADAT 4 */
2728 case 5: p = 1; break; /* AES */
2729 case 6: p = 2; break; /* SPDIF */
2730 case 7: p = 9; break; /* TCO */
2731 case 8: p = 10; break; /* SYNC_IN */
2732 default: return -1;
2733 }
2734 } else {
2735 switch (pref) {
2736 case 0: p = 0; break; /* WC */
2737 case 1: p = 3; break; /* ADAT 1 */
2738 case 2: p = 4; break; /* ADAT 2 */
2739 case 3: p = 5; break; /* ADAT 3 */
2740 case 4: p = 6; break; /* ADAT 4 */
2741 case 5: p = 1; break; /* AES */
2742 case 6: p = 2; break; /* SPDIF */
2743 case 7: p = 10; break; /* SYNC_IN */
2744 default: return -1;
2745 }
2746 }
2747 break;
2748
2749 case AIO:
2750 if (hdspm->tco) {
2751 switch (pref) {
2752 case 0: p = 0; break; /* WC */
2753 case 1: p = 3; break; /* ADAT */
2754 case 2: p = 1; break; /* AES */
2755 case 3: p = 2; break; /* SPDIF */
2756 case 4: p = 9; break; /* TCO */
2757 case 5: p = 10; break; /* SYNC_IN */
2758 default: return -1;
2759 }
2760 } else {
2761 switch (pref) {
2762 case 0: p = 0; break; /* WC */
2763 case 1: p = 3; break; /* ADAT */
2764 case 2: p = 1; break; /* AES */
2765 case 3: p = 2; break; /* SPDIF */
2766 case 4: p = 10; break; /* SYNC_IN */
2767 default: return -1;
2768 }
2769 }
2770 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002771 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002772
2773 switch (hdspm->io_type) {
2774 case RayDAT:
2775 case AIO:
2776 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2777 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2778 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2779 break;
2780
2781 case MADI:
2782 case MADIface:
2783 case AES32:
2784 hdspm_write(hdspm, HDSPM_controlRegister,
2785 hdspm->control_register);
2786 }
2787
Takashi Iwai763f3562005-06-03 11:25:34 +02002788 return 0;
2789}
2790
Adrian Knoth0dca1792011-01-26 19:32:14 +01002791
Takashi Iwai98274f02005-11-17 14:52:34 +01002792static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002794{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002795 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002796
Adrian Knoth0dca1792011-01-26 19:32:14 +01002797 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2798 uinfo->count = 1;
2799 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002800
Adrian Knoth0dca1792011-01-26 19:32:14 +01002801 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2802 uinfo->value.enumerated.item =
2803 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002804
Adrian Knoth0dca1792011-01-26 19:32:14 +01002805 strcpy(uinfo->value.enumerated.name,
2806 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002807
Takashi Iwai763f3562005-06-03 11:25:34 +02002808 return 0;
2809}
2810
Takashi Iwai98274f02005-11-17 14:52:34 +01002811static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002813{
Takashi Iwai98274f02005-11-17 14:52:34 +01002814 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002815 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002816
Adrian Knoth0dca1792011-01-26 19:32:14 +01002817 if (psf >= 0) {
2818 ucontrol->value.enumerated.item[0] = psf;
2819 return 0;
2820 }
2821
2822 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002823}
2824
Takashi Iwai98274f02005-11-17 14:52:34 +01002825static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002827{
Takashi Iwai98274f02005-11-17 14:52:34 +01002828 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002829 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002830
2831 if (!snd_hdspm_use_is_exclusive(hdspm))
2832 return -EBUSY;
2833
Adrian Knoth0dca1792011-01-26 19:32:14 +01002834 val = ucontrol->value.enumerated.item[0];
2835
2836 if (val < 0)
2837 val = 0;
2838 else if (val >= hdspm->texts_autosync_items)
2839 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002840
2841 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002842 if (val != hdspm_pref_sync_ref(hdspm))
2843 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2844
Takashi Iwai763f3562005-06-03 11:25:34 +02002845 spin_unlock_irq(&hdspm->lock);
2846 return change;
2847}
2848
Adrian Knoth0dca1792011-01-26 19:32:14 +01002849
Takashi Iwai763f3562005-06-03 11:25:34 +02002850#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002851{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2852 .name = xname, \
2853 .index = xindex, \
2854 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2855 .info = snd_hdspm_info_autosync_ref, \
2856 .get = snd_hdspm_get_autosync_ref, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002857}
2858
Adrian Knoth0dca1792011-01-26 19:32:14 +01002859static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002860{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002861 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002862 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002863 unsigned int syncref =
2864 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002865 if (syncref == 0)
2866 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2867 if (syncref <= 8)
2868 return syncref;
2869 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002870 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002871 /* This looks at the autosync selected sync reference */
2872 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002873
Remy Bruno3cee5a62006-10-16 12:46:32 +02002874 switch (status2 & HDSPM_SelSyncRefMask) {
2875 case HDSPM_SelSyncRef_WORD:
2876 return HDSPM_AUTOSYNC_FROM_WORD;
2877 case HDSPM_SelSyncRef_MADI:
2878 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002879 case HDSPM_SelSyncRef_TCO:
2880 return HDSPM_AUTOSYNC_FROM_TCO;
2881 case HDSPM_SelSyncRef_SyncIn:
2882 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002883 case HDSPM_SelSyncRef_NVALID:
2884 return HDSPM_AUTOSYNC_FROM_NONE;
2885 default:
2886 return 0;
2887 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002888
Takashi Iwai763f3562005-06-03 11:25:34 +02002889 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002890 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002891}
2892
Adrian Knoth0dca1792011-01-26 19:32:14 +01002893
Takashi Iwai98274f02005-11-17 14:52:34 +01002894static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002896{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002897 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002898
Adrian Knoth0dca1792011-01-26 19:32:14 +01002899 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002900 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
2901 "AES4", "AES5", "AES6", "AES7", "AES8", "None"};
2902
2903 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2904 uinfo->count = 1;
2905 uinfo->value.enumerated.items = 10;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002906 if (uinfo->value.enumerated.item >=
2907 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002908 uinfo->value.enumerated.item =
2909 uinfo->value.enumerated.items - 1;
2910 strcpy(uinfo->value.enumerated.name,
2911 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002912 } else if (MADI == hdspm->io_type) {
2913 static char *texts[] = {"Word Clock", "MADI", "TCO",
2914 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002915
2916 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2917 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002918 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002919 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002920 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002921 uinfo->value.enumerated.item =
2922 uinfo->value.enumerated.items - 1;
2923 strcpy(uinfo->value.enumerated.name,
2924 texts[uinfo->value.enumerated.item]);
2925 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002926 return 0;
2927}
2928
Takashi Iwai98274f02005-11-17 14:52:34 +01002929static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002931{
Takashi Iwai98274f02005-11-17 14:52:34 +01002932 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002933
Remy Bruno65345992007-08-31 12:21:08 +02002934 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002935 return 0;
2936}
2937
Adrian Knothbf0ff872012-12-03 14:55:49 +01002938#define HDSPM_TOGGLE_SETTING(xname, xindex) \
2939{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2940 .name = xname, \
2941 .private_value = xindex, \
2942 .info = snd_hdspm_info_toggle_setting, \
2943 .get = snd_hdspm_get_toggle_setting, \
2944 .put = snd_hdspm_put_toggle_setting \
2945}
2946
2947static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
2948{
2949 return (hdspm->control_register & regmask) ? 1 : 0;
2950}
2951
2952static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
2953{
2954 if (out)
2955 hdspm->control_register |= regmask;
2956 else
2957 hdspm->control_register &= ~regmask;
2958 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
2959
2960 return 0;
2961}
2962
2963#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info
2964
2965static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2967{
2968 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2969 u32 regmask = kcontrol->private_value;
2970
2971 spin_lock_irq(&hdspm->lock);
2972 ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
2973 spin_unlock_irq(&hdspm->lock);
2974 return 0;
2975}
2976
2977static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
2979{
2980 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2981 u32 regmask = kcontrol->private_value;
2982 int change;
2983 unsigned int val;
2984
2985 if (!snd_hdspm_use_is_exclusive(hdspm))
2986 return -EBUSY;
2987 val = ucontrol->value.integer.value[0] & 1;
2988 spin_lock_irq(&hdspm->lock);
2989 change = (int) val != hdspm_toggle_setting(hdspm, regmask);
2990 hdspm_set_toggle_setting(hdspm, regmask, val);
2991 spin_unlock_irq(&hdspm->lock);
2992 return change;
2993}
2994
Takashi Iwai763f3562005-06-03 11:25:34 +02002995#define HDSPM_INPUT_SELECT(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02002996{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2997 .name = xname, \
2998 .index = xindex, \
2999 .info = snd_hdspm_info_input_select, \
3000 .get = snd_hdspm_get_input_select, \
3001 .put = snd_hdspm_put_input_select \
Takashi Iwai763f3562005-06-03 11:25:34 +02003002}
3003
Takashi Iwai98274f02005-11-17 14:52:34 +01003004static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003005{
3006 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3007}
3008
Takashi Iwai98274f02005-11-17 14:52:34 +01003009static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003010{
3011 if (out)
3012 hdspm->control_register |= HDSPM_InputSelect0;
3013 else
3014 hdspm->control_register &= ~HDSPM_InputSelect0;
3015 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3016
3017 return 0;
3018}
3019
Takashi Iwai98274f02005-11-17 14:52:34 +01003020static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003022{
3023 static char *texts[] = { "optical", "coaxial" };
3024
3025 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3026 uinfo->count = 1;
3027 uinfo->value.enumerated.items = 2;
3028
3029 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3030 uinfo->value.enumerated.item =
3031 uinfo->value.enumerated.items - 1;
3032 strcpy(uinfo->value.enumerated.name,
3033 texts[uinfo->value.enumerated.item]);
3034
3035 return 0;
3036}
3037
Takashi Iwai98274f02005-11-17 14:52:34 +01003038static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3039 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003040{
Takashi Iwai98274f02005-11-17 14:52:34 +01003041 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003042
3043 spin_lock_irq(&hdspm->lock);
3044 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3045 spin_unlock_irq(&hdspm->lock);
3046 return 0;
3047}
3048
Takashi Iwai98274f02005-11-17 14:52:34 +01003049static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3050 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003051{
Takashi Iwai98274f02005-11-17 14:52:34 +01003052 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003053 int change;
3054 unsigned int val;
3055
3056 if (!snd_hdspm_use_is_exclusive(hdspm))
3057 return -EBUSY;
3058 val = ucontrol->value.integer.value[0] & 1;
3059 spin_lock_irq(&hdspm->lock);
3060 change = (int) val != hdspm_input_select(hdspm);
3061 hdspm_set_input_select(hdspm, val);
3062 spin_unlock_irq(&hdspm->lock);
3063 return change;
3064}
3065
Adrian Knoth0dca1792011-01-26 19:32:14 +01003066
Remy Bruno3cee5a62006-10-16 12:46:32 +02003067#define HDSPM_DS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003068{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3069 .name = xname, \
3070 .index = xindex, \
3071 .info = snd_hdspm_info_ds_wire, \
3072 .get = snd_hdspm_get_ds_wire, \
3073 .put = snd_hdspm_put_ds_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003074}
3075
3076static int hdspm_ds_wire(struct hdspm * hdspm)
3077{
3078 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3079}
3080
3081static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3082{
3083 if (ds)
3084 hdspm->control_register |= HDSPM_DS_DoubleWire;
3085 else
3086 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3087 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3088
3089 return 0;
3090}
3091
3092static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3093 struct snd_ctl_elem_info *uinfo)
3094{
3095 static char *texts[] = { "Single", "Double" };
3096
3097 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3098 uinfo->count = 1;
3099 uinfo->value.enumerated.items = 2;
3100
3101 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3102 uinfo->value.enumerated.item =
3103 uinfo->value.enumerated.items - 1;
3104 strcpy(uinfo->value.enumerated.name,
3105 texts[uinfo->value.enumerated.item]);
3106
3107 return 0;
3108}
3109
3110static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3112{
3113 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3114
3115 spin_lock_irq(&hdspm->lock);
3116 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3117 spin_unlock_irq(&hdspm->lock);
3118 return 0;
3119}
3120
3121static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_value *ucontrol)
3123{
3124 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3125 int change;
3126 unsigned int val;
3127
3128 if (!snd_hdspm_use_is_exclusive(hdspm))
3129 return -EBUSY;
3130 val = ucontrol->value.integer.value[0] & 1;
3131 spin_lock_irq(&hdspm->lock);
3132 change = (int) val != hdspm_ds_wire(hdspm);
3133 hdspm_set_ds_wire(hdspm, val);
3134 spin_unlock_irq(&hdspm->lock);
3135 return change;
3136}
3137
Adrian Knoth0dca1792011-01-26 19:32:14 +01003138
Remy Bruno3cee5a62006-10-16 12:46:32 +02003139#define HDSPM_QS_WIRE(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003140{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3141 .name = xname, \
3142 .index = xindex, \
3143 .info = snd_hdspm_info_qs_wire, \
3144 .get = snd_hdspm_get_qs_wire, \
3145 .put = snd_hdspm_put_qs_wire \
Remy Bruno3cee5a62006-10-16 12:46:32 +02003146}
3147
3148static int hdspm_qs_wire(struct hdspm * hdspm)
3149{
3150 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3151 return 1;
3152 if (hdspm->control_register & HDSPM_QS_QuadWire)
3153 return 2;
3154 return 0;
3155}
3156
3157static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3158{
3159 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3160 switch (mode) {
3161 case 0:
3162 break;
3163 case 1:
3164 hdspm->control_register |= HDSPM_QS_DoubleWire;
3165 break;
3166 case 2:
3167 hdspm->control_register |= HDSPM_QS_QuadWire;
3168 break;
3169 }
3170 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3171
3172 return 0;
3173}
3174
3175static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3176 struct snd_ctl_elem_info *uinfo)
3177{
3178 static char *texts[] = { "Single", "Double", "Quad" };
3179
3180 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3181 uinfo->count = 1;
3182 uinfo->value.enumerated.items = 3;
3183
3184 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3185 uinfo->value.enumerated.item =
3186 uinfo->value.enumerated.items - 1;
3187 strcpy(uinfo->value.enumerated.name,
3188 texts[uinfo->value.enumerated.item]);
3189
3190 return 0;
3191}
3192
3193static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3194 struct snd_ctl_elem_value *ucontrol)
3195{
3196 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3197
3198 spin_lock_irq(&hdspm->lock);
3199 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3200 spin_unlock_irq(&hdspm->lock);
3201 return 0;
3202}
3203
3204static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_value *ucontrol)
3206{
3207 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3208 int change;
3209 int val;
3210
3211 if (!snd_hdspm_use_is_exclusive(hdspm))
3212 return -EBUSY;
3213 val = ucontrol->value.integer.value[0];
3214 if (val < 0)
3215 val = 0;
3216 if (val > 2)
3217 val = 2;
3218 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003219 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003220 hdspm_set_qs_wire(hdspm, val);
3221 spin_unlock_irq(&hdspm->lock);
3222 return change;
3223}
3224
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003225#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3226{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3227 .name = xname, \
3228 .index = xindex, \
3229 .info = snd_hdspm_info_madi_speedmode, \
3230 .get = snd_hdspm_get_madi_speedmode, \
3231 .put = snd_hdspm_put_madi_speedmode \
3232}
3233
3234static int hdspm_madi_speedmode(struct hdspm *hdspm)
3235{
3236 if (hdspm->control_register & HDSPM_QuadSpeed)
3237 return 2;
3238 if (hdspm->control_register & HDSPM_DoubleSpeed)
3239 return 1;
3240 return 0;
3241}
3242
3243static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3244{
3245 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3246 switch (mode) {
3247 case 0:
3248 break;
3249 case 1:
3250 hdspm->control_register |= HDSPM_DoubleSpeed;
3251 break;
3252 case 2:
3253 hdspm->control_register |= HDSPM_QuadSpeed;
3254 break;
3255 }
3256 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3257
3258 return 0;
3259}
3260
3261static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3262 struct snd_ctl_elem_info *uinfo)
3263{
3264 static char *texts[] = { "Single", "Double", "Quad" };
3265
3266 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3267 uinfo->count = 1;
3268 uinfo->value.enumerated.items = 3;
3269
3270 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3271 uinfo->value.enumerated.item =
3272 uinfo->value.enumerated.items - 1;
3273 strcpy(uinfo->value.enumerated.name,
3274 texts[uinfo->value.enumerated.item]);
3275
3276 return 0;
3277}
3278
3279static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3280 struct snd_ctl_elem_value *ucontrol)
3281{
3282 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3283
3284 spin_lock_irq(&hdspm->lock);
3285 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3286 spin_unlock_irq(&hdspm->lock);
3287 return 0;
3288}
3289
3290static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3291 struct snd_ctl_elem_value *ucontrol)
3292{
3293 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3294 int change;
3295 int val;
3296
3297 if (!snd_hdspm_use_is_exclusive(hdspm))
3298 return -EBUSY;
3299 val = ucontrol->value.integer.value[0];
3300 if (val < 0)
3301 val = 0;
3302 if (val > 2)
3303 val = 2;
3304 spin_lock_irq(&hdspm->lock);
3305 change = val != hdspm_madi_speedmode(hdspm);
3306 hdspm_set_madi_speedmode(hdspm, val);
3307 spin_unlock_irq(&hdspm->lock);
3308 return change;
3309}
Takashi Iwai763f3562005-06-03 11:25:34 +02003310
3311#define HDSPM_MIXER(xname, xindex) \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003312{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3313 .name = xname, \
3314 .index = xindex, \
3315 .device = 0, \
3316 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3317 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3318 .info = snd_hdspm_info_mixer, \
3319 .get = snd_hdspm_get_mixer, \
3320 .put = snd_hdspm_put_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003321}
3322
Takashi Iwai98274f02005-11-17 14:52:34 +01003323static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003325{
3326 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3327 uinfo->count = 3;
3328 uinfo->value.integer.min = 0;
3329 uinfo->value.integer.max = 65535;
3330 uinfo->value.integer.step = 1;
3331 return 0;
3332}
3333
Takashi Iwai98274f02005-11-17 14:52:34 +01003334static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003336{
Takashi Iwai98274f02005-11-17 14:52:34 +01003337 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003338 int source;
3339 int destination;
3340
3341 source = ucontrol->value.integer.value[0];
3342 if (source < 0)
3343 source = 0;
3344 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3345 source = 2 * HDSPM_MAX_CHANNELS - 1;
3346
3347 destination = ucontrol->value.integer.value[1];
3348 if (destination < 0)
3349 destination = 0;
3350 else if (destination >= HDSPM_MAX_CHANNELS)
3351 destination = HDSPM_MAX_CHANNELS - 1;
3352
3353 spin_lock_irq(&hdspm->lock);
3354 if (source >= HDSPM_MAX_CHANNELS)
3355 ucontrol->value.integer.value[2] =
3356 hdspm_read_pb_gain(hdspm, destination,
3357 source - HDSPM_MAX_CHANNELS);
3358 else
3359 ucontrol->value.integer.value[2] =
3360 hdspm_read_in_gain(hdspm, destination, source);
3361
3362 spin_unlock_irq(&hdspm->lock);
3363
3364 return 0;
3365}
3366
Takashi Iwai98274f02005-11-17 14:52:34 +01003367static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003369{
Takashi Iwai98274f02005-11-17 14:52:34 +01003370 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003371 int change;
3372 int source;
3373 int destination;
3374 int gain;
3375
3376 if (!snd_hdspm_use_is_exclusive(hdspm))
3377 return -EBUSY;
3378
3379 source = ucontrol->value.integer.value[0];
3380 destination = ucontrol->value.integer.value[1];
3381
3382 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3383 return -1;
3384 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3385 return -1;
3386
3387 gain = ucontrol->value.integer.value[2];
3388
3389 spin_lock_irq(&hdspm->lock);
3390
3391 if (source >= HDSPM_MAX_CHANNELS)
3392 change = gain != hdspm_read_pb_gain(hdspm, destination,
3393 source -
3394 HDSPM_MAX_CHANNELS);
3395 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003396 change = gain != hdspm_read_in_gain(hdspm, destination,
3397 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003398
3399 if (change) {
3400 if (source >= HDSPM_MAX_CHANNELS)
3401 hdspm_write_pb_gain(hdspm, destination,
3402 source - HDSPM_MAX_CHANNELS,
3403 gain);
3404 else
3405 hdspm_write_in_gain(hdspm, destination, source,
3406 gain);
3407 }
3408 spin_unlock_irq(&hdspm->lock);
3409
3410 return change;
3411}
3412
3413/* The simple mixer control(s) provide gain control for the
3414 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003415 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003416*/
3417
3418#define HDSPM_PLAYBACK_MIXER \
Adrian Knothf27a64f2012-10-19 17:42:30 +02003419{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3420 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3421 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3422 .info = snd_hdspm_info_playback_mixer, \
3423 .get = snd_hdspm_get_playback_mixer, \
3424 .put = snd_hdspm_put_playback_mixer \
Takashi Iwai763f3562005-06-03 11:25:34 +02003425}
3426
Takashi Iwai98274f02005-11-17 14:52:34 +01003427static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3428 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003429{
3430 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3431 uinfo->count = 1;
3432 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003433 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003434 uinfo->value.integer.step = 1;
3435 return 0;
3436}
3437
Takashi Iwai98274f02005-11-17 14:52:34 +01003438static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3439 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003440{
Takashi Iwai98274f02005-11-17 14:52:34 +01003441 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003442 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003443
3444 channel = ucontrol->id.index - 1;
3445
Takashi Iwaida3cec32008-08-08 17:12:14 +02003446 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3447 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003448
Takashi Iwai763f3562005-06-03 11:25:34 +02003449 spin_lock_irq(&hdspm->lock);
3450 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003451 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003452 spin_unlock_irq(&hdspm->lock);
3453
Takashi Iwai763f3562005-06-03 11:25:34 +02003454 return 0;
3455}
3456
Takashi Iwai98274f02005-11-17 14:52:34 +01003457static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3458 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003459{
Takashi Iwai98274f02005-11-17 14:52:34 +01003460 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003461 int change;
3462 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003463 int gain;
3464
3465 if (!snd_hdspm_use_is_exclusive(hdspm))
3466 return -EBUSY;
3467
3468 channel = ucontrol->id.index - 1;
3469
Takashi Iwaida3cec32008-08-08 17:12:14 +02003470 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3471 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003472
Adrian Knoth0dca1792011-01-26 19:32:14 +01003473 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003474
3475 spin_lock_irq(&hdspm->lock);
3476 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003477 gain != hdspm_read_pb_gain(hdspm, channel,
3478 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003479 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003480 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003481 gain);
3482 spin_unlock_irq(&hdspm->lock);
3483 return change;
3484}
3485
Adrian Knoth0dca1792011-01-26 19:32:14 +01003486#define HDSPM_SYNC_CHECK(xname, xindex) \
3487{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3488 .name = xname, \
3489 .private_value = xindex, \
3490 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3491 .info = snd_hdspm_info_sync_check, \
3492 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003493}
3494
Adrian Knoth0dca1792011-01-26 19:32:14 +01003495
Takashi Iwai98274f02005-11-17 14:52:34 +01003496static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3497 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003498{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003499 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Takashi Iwai763f3562005-06-03 11:25:34 +02003500 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3501 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003502 uinfo->value.enumerated.items = 4;
Takashi Iwai763f3562005-06-03 11:25:34 +02003503 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3504 uinfo->value.enumerated.item =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003505 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02003506 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01003507 texts[uinfo->value.enumerated.item]);
Takashi Iwai763f3562005-06-03 11:25:34 +02003508 return 0;
3509}
3510
Adrian Knoth0dca1792011-01-26 19:32:14 +01003511static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003512{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003513 int status, status2;
3514
3515 switch (hdspm->io_type) {
3516 case AES32:
3517 status = hdspm_read(hdspm, HDSPM_statusRegister);
Andre Schramm56bde0f2013-01-09 14:40:18 +01003518 if (status & HDSPM_AES32_wcLock) {
3519 if (status & HDSPM_AES32_wcSync)
3520 return 2;
3521 else
3522 return 1;
3523 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02003524 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003525 break;
3526
3527 case MADI:
3528 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003529 if (status2 & HDSPM_wcLock) {
3530 if (status2 & HDSPM_wcSync)
3531 return 2;
3532 else
3533 return 1;
3534 }
3535 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003536 break;
3537
3538 case RayDAT:
3539 case AIO:
3540 status = hdspm_read(hdspm, HDSPM_statusRegister);
3541
3542 if (status & 0x2000000)
3543 return 2;
3544 else if (status & 0x1000000)
3545 return 1;
3546 return 0;
3547
3548 break;
3549
3550 case MADIface:
3551 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003552 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003553
Takashi Iwai763f3562005-06-03 11:25:34 +02003554
Adrian Knoth0dca1792011-01-26 19:32:14 +01003555 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003556}
3557
3558
Adrian Knoth0dca1792011-01-26 19:32:14 +01003559static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003560{
3561 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3562 if (status & HDSPM_madiLock) {
3563 if (status & HDSPM_madiSync)
3564 return 2;
3565 else
3566 return 1;
3567 }
3568 return 0;
3569}
3570
Adrian Knoth0dca1792011-01-26 19:32:14 +01003571
3572static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3573{
3574 int status, lock, sync;
3575
3576 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3577
3578 lock = (status & (0x1<<idx)) ? 1 : 0;
3579 sync = (status & (0x100<<idx)) ? 1 : 0;
3580
3581 if (lock && sync)
3582 return 2;
3583 else if (lock)
3584 return 1;
3585 return 0;
3586}
3587
3588
3589static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3590{
3591 int status, lock = 0, sync = 0;
3592
3593 switch (hdspm->io_type) {
3594 case RayDAT:
3595 case AIO:
3596 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3597 lock = (status & 0x400) ? 1 : 0;
3598 sync = (status & 0x800) ? 1 : 0;
3599 break;
3600
3601 case MADI:
Adrian Knoth2e0452f2012-10-19 17:42:27 +02003602 status = hdspm_read(hdspm, HDSPM_statusRegister);
3603 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3604 sync = (status & HDSPM_syncInSync) ? 1 : 0;
3605 break;
3606
Adrian Knoth0dca1792011-01-26 19:32:14 +01003607 case AES32:
3608 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth9a215f42012-10-19 17:42:28 +02003609 lock = (status & 0x100000) ? 1 : 0;
3610 sync = (status & 0x200000) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003611 break;
3612
3613 case MADIface:
3614 break;
3615 }
3616
3617 if (lock && sync)
3618 return 2;
3619 else if (lock)
3620 return 1;
3621
3622 return 0;
3623}
3624
3625static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3626{
3627 int status2, lock, sync;
3628 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3629
3630 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3631 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3632
3633 if (sync)
3634 return 2;
3635 else if (lock)
3636 return 1;
3637 return 0;
3638}
3639
3640
3641static int hdspm_tco_sync_check(struct hdspm *hdspm)
3642{
3643 int status;
3644
3645 if (hdspm->tco) {
3646 switch (hdspm->io_type) {
3647 case MADI:
3648 case AES32:
3649 status = hdspm_read(hdspm, HDSPM_statusRegister);
3650 if (status & HDSPM_tcoLock) {
3651 if (status & HDSPM_tcoSync)
3652 return 2;
3653 else
3654 return 1;
3655 }
3656 return 0;
3657
3658 break;
3659
3660 case RayDAT:
3661 case AIO:
3662 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3663
3664 if (status & 0x8000000)
3665 return 2; /* Sync */
3666 if (status & 0x4000000)
3667 return 1; /* Lock */
3668 return 0; /* No signal */
3669 break;
3670
3671 default:
3672 break;
3673 }
3674 }
3675
3676 return 3; /* N/A */
3677}
3678
3679
3680static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3681 struct snd_ctl_elem_value *ucontrol)
3682{
3683 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3684 int val = -1;
3685
3686 switch (hdspm->io_type) {
3687 case RayDAT:
3688 switch (kcontrol->private_value) {
3689 case 0: /* WC */
3690 val = hdspm_wc_sync_check(hdspm); break;
3691 case 7: /* TCO */
3692 val = hdspm_tco_sync_check(hdspm); break;
3693 case 8: /* SYNC IN */
3694 val = hdspm_sync_in_sync_check(hdspm); break;
3695 default:
Adrian Knothd1a3c982012-11-07 18:00:09 +01003696 val = hdspm_s1_sync_check(hdspm,
3697 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003698 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003699 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003700
3701 case AIO:
3702 switch (kcontrol->private_value) {
3703 case 0: /* WC */
3704 val = hdspm_wc_sync_check(hdspm); break;
3705 case 4: /* TCO */
3706 val = hdspm_tco_sync_check(hdspm); break;
3707 case 5: /* SYNC IN */
3708 val = hdspm_sync_in_sync_check(hdspm); break;
3709 default:
3710 val = hdspm_s1_sync_check(hdspm, ucontrol->id.index-1);
3711 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003712 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003713
3714 case MADI:
3715 switch (kcontrol->private_value) {
3716 case 0: /* WC */
3717 val = hdspm_wc_sync_check(hdspm); break;
3718 case 1: /* MADI */
3719 val = hdspm_madi_sync_check(hdspm); break;
3720 case 2: /* TCO */
3721 val = hdspm_tco_sync_check(hdspm); break;
3722 case 3: /* SYNC_IN */
3723 val = hdspm_sync_in_sync_check(hdspm); break;
3724 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003725 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003726
3727 case MADIface:
3728 val = hdspm_madi_sync_check(hdspm); /* MADI */
3729 break;
3730
3731 case AES32:
3732 switch (kcontrol->private_value) {
3733 case 0: /* WC */
3734 val = hdspm_wc_sync_check(hdspm); break;
3735 case 9: /* TCO */
3736 val = hdspm_tco_sync_check(hdspm); break;
3737 case 10 /* SYNC IN */:
3738 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003739 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01003740 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003741 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003742 }
Adrian Knothfba30fd2012-10-19 17:42:24 +02003743 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003744
3745 }
3746
3747 if (-1 == val)
3748 val = 3;
3749
3750 ucontrol->value.enumerated.item[0] = val;
3751 return 0;
3752}
3753
3754
3755
3756/**
3757 * TCO controls
3758 **/
3759static void hdspm_tco_write(struct hdspm *hdspm)
3760{
3761 unsigned int tc[4] = { 0, 0, 0, 0};
3762
3763 switch (hdspm->tco->input) {
3764 case 0:
3765 tc[2] |= HDSPM_TCO2_set_input_MSB;
3766 break;
3767 case 1:
3768 tc[2] |= HDSPM_TCO2_set_input_LSB;
3769 break;
3770 default:
3771 break;
3772 }
3773
3774 switch (hdspm->tco->framerate) {
3775 case 1:
3776 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
3777 break;
3778 case 2:
3779 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
3780 break;
3781 case 3:
3782 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
3783 HDSPM_TCO1_set_drop_frame_flag;
3784 break;
3785 case 4:
3786 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3787 HDSPM_TCO1_LTC_Format_MSB;
3788 break;
3789 case 5:
3790 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3791 HDSPM_TCO1_LTC_Format_MSB +
3792 HDSPM_TCO1_set_drop_frame_flag;
3793 break;
3794 default:
3795 break;
3796 }
3797
3798 switch (hdspm->tco->wordclock) {
3799 case 1:
3800 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
3801 break;
3802 case 2:
3803 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
3804 break;
3805 default:
3806 break;
3807 }
3808
3809 switch (hdspm->tco->samplerate) {
3810 case 1:
3811 tc[2] |= HDSPM_TCO2_set_freq;
3812 break;
3813 case 2:
3814 tc[2] |= HDSPM_TCO2_set_freq_from_app;
3815 break;
3816 default:
3817 break;
3818 }
3819
3820 switch (hdspm->tco->pull) {
3821 case 1:
3822 tc[2] |= HDSPM_TCO2_set_pull_up;
3823 break;
3824 case 2:
3825 tc[2] |= HDSPM_TCO2_set_pull_down;
3826 break;
3827 case 3:
3828 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
3829 break;
3830 case 4:
3831 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
3832 break;
3833 default:
3834 break;
3835 }
3836
3837 if (1 == hdspm->tco->term) {
3838 tc[2] |= HDSPM_TCO2_set_term_75R;
3839 }
3840
3841 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
3842 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
3843 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
3844 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
3845}
3846
3847
3848#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
3849{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3850 .name = xname, \
3851 .index = xindex, \
3852 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
3853 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3854 .info = snd_hdspm_info_tco_sample_rate, \
3855 .get = snd_hdspm_get_tco_sample_rate, \
3856 .put = snd_hdspm_put_tco_sample_rate \
3857}
3858
3859static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
3860 struct snd_ctl_elem_info *uinfo)
3861{
3862 static char *texts[] = { "44.1 kHz", "48 kHz" };
3863 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3864 uinfo->count = 1;
3865 uinfo->value.enumerated.items = 2;
3866
3867 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3868 uinfo->value.enumerated.item =
3869 uinfo->value.enumerated.items - 1;
3870
3871 strcpy(uinfo->value.enumerated.name,
3872 texts[uinfo->value.enumerated.item]);
3873
3874 return 0;
3875}
3876
3877static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
3878 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003879{
Takashi Iwai98274f02005-11-17 14:52:34 +01003880 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003881
Adrian Knoth0dca1792011-01-26 19:32:14 +01003882 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
3883
Takashi Iwai763f3562005-06-03 11:25:34 +02003884 return 0;
3885}
3886
Adrian Knoth0dca1792011-01-26 19:32:14 +01003887static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
3888 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02003889{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003890 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3891
3892 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
3893 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
3894
3895 hdspm_tco_write(hdspm);
3896
3897 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02003898 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01003899
Remy Bruno3cee5a62006-10-16 12:46:32 +02003900 return 0;
3901}
3902
Adrian Knoth0dca1792011-01-26 19:32:14 +01003903
3904#define HDSPM_TCO_PULL(xname, xindex) \
3905{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3906 .name = xname, \
3907 .index = xindex, \
3908 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
3909 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3910 .info = snd_hdspm_info_tco_pull, \
3911 .get = snd_hdspm_get_tco_pull, \
3912 .put = snd_hdspm_put_tco_pull \
3913}
3914
3915static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
3916 struct snd_ctl_elem_info *uinfo)
3917{
3918 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
3919 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3920 uinfo->count = 1;
3921 uinfo->value.enumerated.items = 5;
3922
3923 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3924 uinfo->value.enumerated.item =
3925 uinfo->value.enumerated.items - 1;
3926
3927 strcpy(uinfo->value.enumerated.name,
3928 texts[uinfo->value.enumerated.item]);
3929
3930 return 0;
3931}
3932
3933static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
3934 struct snd_ctl_elem_value *ucontrol)
3935{
3936 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3937
3938 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
3939
3940 return 0;
3941}
3942
3943static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
3944 struct snd_ctl_elem_value *ucontrol)
3945{
3946 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3947
3948 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
3949 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
3950
3951 hdspm_tco_write(hdspm);
3952
3953 return 1;
3954 }
3955
3956 return 0;
3957}
3958
3959#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
3960{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3961 .name = xname, \
3962 .index = xindex, \
3963 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
3964 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3965 .info = snd_hdspm_info_tco_wck_conversion, \
3966 .get = snd_hdspm_get_tco_wck_conversion, \
3967 .put = snd_hdspm_put_tco_wck_conversion \
3968}
3969
3970static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
3971 struct snd_ctl_elem_info *uinfo)
3972{
3973 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
3974 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3975 uinfo->count = 1;
3976 uinfo->value.enumerated.items = 3;
3977
3978 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3979 uinfo->value.enumerated.item =
3980 uinfo->value.enumerated.items - 1;
3981
3982 strcpy(uinfo->value.enumerated.name,
3983 texts[uinfo->value.enumerated.item]);
3984
3985 return 0;
3986}
3987
3988static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
3989 struct snd_ctl_elem_value *ucontrol)
3990{
3991 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3992
3993 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
3994
3995 return 0;
3996}
3997
3998static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
3999 struct snd_ctl_elem_value *ucontrol)
4000{
4001 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4002
4003 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4004 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4005
4006 hdspm_tco_write(hdspm);
4007
4008 return 1;
4009 }
4010
4011 return 0;
4012}
4013
4014
4015#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4016{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4017 .name = xname, \
4018 .index = xindex, \
4019 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4020 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4021 .info = snd_hdspm_info_tco_frame_rate, \
4022 .get = snd_hdspm_get_tco_frame_rate, \
4023 .put = snd_hdspm_put_tco_frame_rate \
4024}
4025
4026static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4027 struct snd_ctl_elem_info *uinfo)
4028{
4029 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4030 "29.97 dfps", "30 fps", "30 dfps" };
4031 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4032 uinfo->count = 1;
4033 uinfo->value.enumerated.items = 6;
4034
4035 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4036 uinfo->value.enumerated.item =
4037 uinfo->value.enumerated.items - 1;
4038
4039 strcpy(uinfo->value.enumerated.name,
4040 texts[uinfo->value.enumerated.item]);
4041
4042 return 0;
4043}
4044
4045static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004046 struct snd_ctl_elem_value *ucontrol)
4047{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004048 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4049
Adrian Knoth0dca1792011-01-26 19:32:14 +01004050 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004051
Remy Bruno3cee5a62006-10-16 12:46:32 +02004052 return 0;
4053}
Takashi Iwai763f3562005-06-03 11:25:34 +02004054
Adrian Knoth0dca1792011-01-26 19:32:14 +01004055static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4056 struct snd_ctl_elem_value *ucontrol)
4057{
4058 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4059
4060 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4061 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4062
4063 hdspm_tco_write(hdspm);
4064
4065 return 1;
4066 }
4067
4068 return 0;
4069}
4070
4071
4072#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4073{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4074 .name = xname, \
4075 .index = xindex, \
4076 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4077 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4078 .info = snd_hdspm_info_tco_sync_source, \
4079 .get = snd_hdspm_get_tco_sync_source, \
4080 .put = snd_hdspm_put_tco_sync_source \
4081}
4082
4083static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4084 struct snd_ctl_elem_info *uinfo)
4085{
4086 static char *texts[] = { "LTC", "Video", "WCK" };
4087 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4088 uinfo->count = 1;
4089 uinfo->value.enumerated.items = 3;
4090
4091 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4092 uinfo->value.enumerated.item =
4093 uinfo->value.enumerated.items - 1;
4094
4095 strcpy(uinfo->value.enumerated.name,
4096 texts[uinfo->value.enumerated.item]);
4097
4098 return 0;
4099}
4100
4101static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4102 struct snd_ctl_elem_value *ucontrol)
4103{
4104 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4105
4106 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4107
4108 return 0;
4109}
4110
4111static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4112 struct snd_ctl_elem_value *ucontrol)
4113{
4114 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4115
4116 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4117 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4118
4119 hdspm_tco_write(hdspm);
4120
4121 return 1;
4122 }
4123
4124 return 0;
4125}
4126
4127
4128#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4129{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4130 .name = xname, \
4131 .index = xindex, \
4132 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4133 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4134 .info = snd_hdspm_info_tco_word_term, \
4135 .get = snd_hdspm_get_tco_word_term, \
4136 .put = snd_hdspm_put_tco_word_term \
4137}
4138
4139static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4140 struct snd_ctl_elem_info *uinfo)
4141{
4142 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4143 uinfo->count = 1;
4144 uinfo->value.integer.min = 0;
4145 uinfo->value.integer.max = 1;
4146
4147 return 0;
4148}
4149
4150
4151static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4152 struct snd_ctl_elem_value *ucontrol)
4153{
4154 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4155
4156 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4157
4158 return 0;
4159}
4160
4161
4162static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4163 struct snd_ctl_elem_value *ucontrol)
4164{
4165 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4166
4167 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4168 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4169
4170 hdspm_tco_write(hdspm);
4171
4172 return 1;
4173 }
4174
4175 return 0;
4176}
4177
4178
4179
Takashi Iwai763f3562005-06-03 11:25:34 +02004180
Remy Bruno3cee5a62006-10-16 12:46:32 +02004181static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004182 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004183 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004184 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4185 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4186 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4187 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knothb8812c52012-10-19 17:42:26 +02004188 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004189 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4190 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
Adrian Knoth930f4ff2012-10-19 17:42:29 +02004191 HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004192 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Adrian Knothc9e16682012-12-03 14:55:50 +01004193 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4194 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
Adrian Knoth696be0f2013-03-10 00:37:23 +01004195 HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
Adrian Knothc9e16682012-12-03 14:55:50 +01004196 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4197 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004198 HDSPM_INPUT_SELECT("Input Select", 0),
4199 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004200};
4201
4202
4203static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4204 HDSPM_MIXER("Mixer", 0),
4205 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4206 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4207 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4208 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4209 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
Adrian Knothc9e16682012-12-03 14:55:50 +01004210 HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4211 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
4212 HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004213 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004214};
4215
Adrian Knoth0dca1792011-01-26 19:32:14 +01004216static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004217 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004218 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004219 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4220 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4221 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4222 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004223 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004224 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4225 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4226 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4227 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4228 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4229 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4230 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4231 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4232 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4233 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4234 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
4235 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5)
4236
4237 /*
4238 HDSPM_INPUT_SELECT("Input Select", 0),
4239 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4240 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4241 HDSPM_SPDIF_IN("SPDIF In", 0);
4242 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4243 HDSPM_INPUT_LEVEL("Input Level", 0);
4244 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4245 HDSPM_PHONES("Phones", 0);
4246 */
4247};
4248
4249static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4250 HDSPM_MIXER("Mixer", 0),
4251 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4252 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4253 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4254 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4255 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4256 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4257 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4258 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4259 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4260 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4261 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4262 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4263 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4264 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4265 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4266 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4267 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4268 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4269 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4270 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4271 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
4272 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8)
4273};
4274
4275static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4276 HDSPM_MIXER("Mixer", 0),
4277 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4278 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4279 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4280 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4281 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4282 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4283 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4284 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4285 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4286 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4287 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4288 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4289 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4290 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4291 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4292 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4293 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4294 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4295 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4296 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4297 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4298 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4299 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4300 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4301 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4302 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4303 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4304 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Adrian Knothc9e16682012-12-03 14:55:50 +01004305 HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
4306 HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
4307 HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
4308 HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
4309 HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004310 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4311 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4312};
4313
Adrian Knoth0dca1792011-01-26 19:32:14 +01004314
4315
4316/* Control elements for the optional TCO module */
4317static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4318 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4319 HDSPM_TCO_PULL("TCO Pull", 0),
4320 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4321 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4322 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
4323 HDSPM_TCO_WORD_TERM("TCO Word Term", 0)
4324};
4325
4326
Takashi Iwai98274f02005-11-17 14:52:34 +01004327static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004328
4329
Takashi Iwai98274f02005-11-17 14:52:34 +01004330static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004331{
4332 int i;
4333
Adrian Knoth0dca1792011-01-26 19:32:14 +01004334 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004335 if (hdspm->system_sample_rate > 48000) {
4336 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004337 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4338 SNDRV_CTL_ELEM_ACCESS_READ |
4339 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004340 } else {
4341 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004342 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4343 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004344 }
4345 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004346 SNDRV_CTL_EVENT_MASK_INFO,
4347 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004348 }
4349
4350 return 0;
4351}
4352
4353
Adrian Knoth0dca1792011-01-26 19:32:14 +01004354static int snd_hdspm_create_controls(struct snd_card *card,
4355 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004356{
4357 unsigned int idx, limit;
4358 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004359 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004360 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004361
Adrian Knoth0dca1792011-01-26 19:32:14 +01004362 switch (hdspm->io_type) {
4363 case MADI:
4364 list = snd_hdspm_controls_madi;
4365 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4366 break;
4367 case MADIface:
4368 list = snd_hdspm_controls_madiface;
4369 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4370 break;
4371 case AIO:
4372 list = snd_hdspm_controls_aio;
4373 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4374 break;
4375 case RayDAT:
4376 list = snd_hdspm_controls_raydat;
4377 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4378 break;
4379 case AES32:
4380 list = snd_hdspm_controls_aes32;
4381 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4382 break;
4383 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004384
Adrian Knoth0dca1792011-01-26 19:32:14 +01004385 if (NULL != list) {
4386 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004387 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004388 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004389 if (err < 0)
4390 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004391 }
4392 }
4393
Takashi Iwai763f3562005-06-03 11:25:34 +02004394
Adrian Knoth0dca1792011-01-26 19:32:14 +01004395 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004396 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004397 if (hdspm->system_sample_rate >= 128000) {
4398 limit = hdspm->qs_out_channels;
4399 } else if (hdspm->system_sample_rate >= 64000) {
4400 limit = hdspm->ds_out_channels;
4401 } else {
4402 limit = hdspm->ss_out_channels;
4403 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004404 for (idx = 0; idx < limit; ++idx) {
4405 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004406 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4407 err = snd_ctl_add(card, kctl);
4408 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004409 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004410 hdspm->playback_mixer_ctls[idx] = kctl;
4411 }
4412
Adrian Knoth0dca1792011-01-26 19:32:14 +01004413
4414 if (hdspm->tco) {
4415 /* add tco control elements */
4416 list = snd_hdspm_controls_tco;
4417 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4418 for (idx = 0; idx < limit; idx++) {
4419 err = snd_ctl_add(card,
4420 snd_ctl_new1(&list[idx], hdspm));
4421 if (err < 0)
4422 return err;
4423 }
4424 }
4425
Takashi Iwai763f3562005-06-03 11:25:34 +02004426 return 0;
4427}
4428
4429/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004430 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004431 ------------------------------------------------------------*/
4432
4433static void
Remy Bruno3cee5a62006-10-16 12:46:32 +02004434snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
4435 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004436{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004437 struct hdspm *hdspm = entry->private_data;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004438 unsigned int status, status2, control, freq;
4439
Takashi Iwai763f3562005-06-03 11:25:34 +02004440 char *pref_sync_ref;
4441 char *autosync_ref;
4442 char *system_clock_mode;
Takashi Iwai763f3562005-06-03 11:25:34 +02004443 char *insel;
Takashi Iwai763f3562005-06-03 11:25:34 +02004444 int x, x2;
4445
Adrian Knoth0dca1792011-01-26 19:32:14 +01004446 /* TCO stuff */
4447 int a, ltc, frames, seconds, minutes, hours;
4448 unsigned int period;
4449 u64 freq_const = 0;
4450 u32 rate;
4451
Takashi Iwai763f3562005-06-03 11:25:34 +02004452 status = hdspm_read(hdspm, HDSPM_statusRegister);
4453 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004454 control = hdspm->control_register;
4455 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
Takashi Iwai763f3562005-06-03 11:25:34 +02004456
4457 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004458 hdspm->card_name, hdspm->card->number + 1,
4459 hdspm->firmware_rev,
4460 (status2 & HDSPM_version0) |
4461 (status2 & HDSPM_version1) | (status2 &
4462 HDSPM_version2));
4463
4464 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4465 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
Adrian Knoth7d53a632012-01-04 14:31:16 +01004466 hdspm->serial);
Takashi Iwai763f3562005-06-03 11:25:34 +02004467
4468 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004469 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
Takashi Iwai763f3562005-06-03 11:25:34 +02004470
4471 snd_iprintf(buffer, "--- System ---\n");
4472
4473 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004474 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4475 status & HDSPM_audioIRQPending,
4476 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4477 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4478 hdspm->irq_count);
Takashi Iwai763f3562005-06-03 11:25:34 +02004479 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004480 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4481 "estimated= %ld (bytes)\n",
4482 ((status & HDSPM_BufferID) ? 1 : 0),
4483 (status & HDSPM_BufferPositionMask),
4484 (status & HDSPM_BufferPositionMask) %
4485 (2 * (int)hdspm->period_bytes),
4486 ((status & HDSPM_BufferPositionMask) - 64) %
4487 (2 * (int)hdspm->period_bytes),
4488 (long) hdspm_hw_pointer(hdspm) * 4);
Takashi Iwai763f3562005-06-03 11:25:34 +02004489
4490 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004491 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4492 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4493 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4494 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4495 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
Takashi Iwai763f3562005-06-03 11:25:34 +02004496 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004497 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4498 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4499 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4500 snd_iprintf(buffer,
4501 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4502 "status2=0x%x\n",
4503 hdspm->control_register, hdspm->control2_register,
4504 status, status2);
4505 if (status & HDSPM_tco_detect) {
4506 snd_iprintf(buffer, "TCO module detected.\n");
4507 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4508 if (a & HDSPM_TCO1_LTC_Input_valid) {
4509 snd_iprintf(buffer, " LTC valid, ");
4510 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4511 HDSPM_TCO1_LTC_Format_MSB)) {
4512 case 0:
4513 snd_iprintf(buffer, "24 fps, ");
4514 break;
4515 case HDSPM_TCO1_LTC_Format_LSB:
4516 snd_iprintf(buffer, "25 fps, ");
4517 break;
4518 case HDSPM_TCO1_LTC_Format_MSB:
4519 snd_iprintf(buffer, "29.97 fps, ");
4520 break;
4521 default:
4522 snd_iprintf(buffer, "30 fps, ");
4523 break;
4524 }
4525 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4526 snd_iprintf(buffer, "drop frame\n");
4527 } else {
4528 snd_iprintf(buffer, "full frame\n");
4529 }
4530 } else {
4531 snd_iprintf(buffer, " no LTC\n");
4532 }
4533 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4534 snd_iprintf(buffer, " Video: NTSC\n");
4535 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4536 snd_iprintf(buffer, " Video: PAL\n");
4537 } else {
4538 snd_iprintf(buffer, " No video\n");
4539 }
4540 if (a & HDSPM_TCO1_TCO_lock) {
4541 snd_iprintf(buffer, " Sync: lock\n");
4542 } else {
4543 snd_iprintf(buffer, " Sync: no lock\n");
4544 }
4545
4546 switch (hdspm->io_type) {
4547 case MADI:
4548 case AES32:
4549 freq_const = 110069313433624ULL;
4550 break;
4551 case RayDAT:
4552 case AIO:
4553 freq_const = 104857600000000ULL;
4554 break;
4555 case MADIface:
4556 break; /* no TCO possible */
4557 }
4558
4559 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4560 snd_iprintf(buffer, " period: %u\n", period);
4561
4562
4563 /* rate = freq_const/period; */
4564 rate = div_u64(freq_const, period);
4565
4566 if (control & HDSPM_QuadSpeed) {
4567 rate *= 4;
4568 } else if (control & HDSPM_DoubleSpeed) {
4569 rate *= 2;
4570 }
4571
4572 snd_iprintf(buffer, " Frequency: %u Hz\n",
4573 (unsigned int) rate);
4574
4575 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4576 frames = ltc & 0xF;
4577 ltc >>= 4;
4578 frames += (ltc & 0x3) * 10;
4579 ltc >>= 4;
4580 seconds = ltc & 0xF;
4581 ltc >>= 4;
4582 seconds += (ltc & 0x7) * 10;
4583 ltc >>= 4;
4584 minutes = ltc & 0xF;
4585 ltc >>= 4;
4586 minutes += (ltc & 0x7) * 10;
4587 ltc >>= 4;
4588 hours = ltc & 0xF;
4589 ltc >>= 4;
4590 hours += (ltc & 0x3) * 10;
4591 snd_iprintf(buffer,
4592 " LTC In: %02d:%02d:%02d:%02d\n",
4593 hours, minutes, seconds, frames);
4594
4595 } else {
4596 snd_iprintf(buffer, "No TCO module detected.\n");
4597 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004598
4599 snd_iprintf(buffer, "--- Settings ---\n");
4600
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004601 x = hdspm_get_latency(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02004602
4603 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004604 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4605 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004606
Adrian Knoth0dca1792011-01-26 19:32:14 +01004607 snd_iprintf(buffer, "Line out: %s\n",
4608 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004609
4610 switch (hdspm->control_register & HDSPM_InputMask) {
4611 case HDSPM_InputOptical:
4612 insel = "Optical";
4613 break;
4614 case HDSPM_InputCoaxial:
4615 insel = "Coaxial";
4616 break;
4617 default:
Masanari Iidaec8f53f2012-11-02 00:28:50 +09004618 insel = "Unknown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004619 }
4620
Takashi Iwai763f3562005-06-03 11:25:34 +02004621 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004622 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4623 "Auto Input %s\n",
4624 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4625 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4626 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004627
Adrian Knoth0dca1792011-01-26 19:32:14 +01004628
Remy Bruno3cee5a62006-10-16 12:46:32 +02004629 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004630 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004631 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004632 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004633 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004634
4635 switch (hdspm_pref_sync_ref(hdspm)) {
4636 case HDSPM_SYNC_FROM_WORD:
4637 pref_sync_ref = "Word Clock";
4638 break;
4639 case HDSPM_SYNC_FROM_MADI:
4640 pref_sync_ref = "MADI Sync";
4641 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004642 case HDSPM_SYNC_FROM_TCO:
4643 pref_sync_ref = "TCO";
4644 break;
4645 case HDSPM_SYNC_FROM_SYNC_IN:
4646 pref_sync_ref = "Sync In";
4647 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004648 default:
4649 pref_sync_ref = "XXXX Clock";
4650 break;
4651 }
4652 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004653 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004654
4655 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004656 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004657
4658
4659 snd_iprintf(buffer, "--- Status:\n");
4660
4661 x = status & HDSPM_madiSync;
4662 x2 = status2 & HDSPM_wcSync;
4663
4664 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004665 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4666 "NoLock",
4667 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4668 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004669
4670 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004671 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4672 autosync_ref = "Sync In";
4673 break;
4674 case HDSPM_AUTOSYNC_FROM_TCO:
4675 autosync_ref = "TCO";
4676 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004677 case HDSPM_AUTOSYNC_FROM_WORD:
4678 autosync_ref = "Word Clock";
4679 break;
4680 case HDSPM_AUTOSYNC_FROM_MADI:
4681 autosync_ref = "MADI Sync";
4682 break;
4683 case HDSPM_AUTOSYNC_FROM_NONE:
4684 autosync_ref = "Input not valid";
4685 break;
4686 default:
4687 autosync_ref = "---";
4688 break;
4689 }
4690 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004691 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4692 autosync_ref, hdspm_external_sample_rate(hdspm),
4693 (status & HDSPM_madiFreqMask) >> 22,
4694 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004695
4696 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004697 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4698 (status & HDSPM_RX_64ch) ? "64 channels" :
4699 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004700
4701 snd_iprintf(buffer, "\n");
4702}
4703
Remy Bruno3cee5a62006-10-16 12:46:32 +02004704static void
4705snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4706 struct snd_info_buffer *buffer)
4707{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004708 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004709 unsigned int status;
4710 unsigned int status2;
4711 unsigned int timecode;
Andre Schramm56bde0f2013-01-09 14:40:18 +01004712 unsigned int wcLock, wcSync;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004713 int pref_syncref;
4714 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004715 int x;
4716
4717 status = hdspm_read(hdspm, HDSPM_statusRegister);
4718 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4719 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4720
4721 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4722 hdspm->card_name, hdspm->card->number + 1,
4723 hdspm->firmware_rev);
4724
4725 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4726 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4727
4728 snd_iprintf(buffer, "--- System ---\n");
4729
4730 snd_iprintf(buffer,
4731 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4732 status & HDSPM_audioIRQPending,
4733 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4734 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4735 hdspm->irq_count);
4736 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004737 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4738 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004739 ((status & HDSPM_BufferID) ? 1 : 0),
4740 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004741 (status & HDSPM_BufferPositionMask) %
4742 (2 * (int)hdspm->period_bytes),
4743 ((status & HDSPM_BufferPositionMask) - 64) %
4744 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004745 (long) hdspm_hw_pointer(hdspm) * 4);
4746
4747 snd_iprintf(buffer,
4748 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4749 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4750 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4751 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4752 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4753 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004754 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4755 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4756 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4757 snd_iprintf(buffer,
4758 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4759 "status2=0x%x\n",
4760 hdspm->control_register, hdspm->control2_register,
4761 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004762
4763 snd_iprintf(buffer, "--- Settings ---\n");
4764
Adrian Knoth7cb155f2011-08-15 00:22:53 +02004765 x = hdspm_get_latency(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004766
4767 snd_iprintf(buffer,
4768 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4769 x, (unsigned long) hdspm->period_bytes);
4770
Adrian Knoth0dca1792011-01-26 19:32:14 +01004771 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004772 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01004773 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02004774
4775 snd_iprintf(buffer,
4776 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
4777 (hdspm->
4778 control_register & HDSPM_clr_tms) ? "on" : "off",
4779 (hdspm->
4780 control_register & HDSPM_Emphasis) ? "on" : "off",
4781 (hdspm->
4782 control_register & HDSPM_Dolby) ? "on" : "off");
4783
Remy Bruno3cee5a62006-10-16 12:46:32 +02004784
4785 pref_syncref = hdspm_pref_sync_ref(hdspm);
4786 if (pref_syncref == 0)
4787 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
4788 else
4789 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
4790 pref_syncref);
4791
4792 snd_iprintf(buffer, "System Clock Frequency: %d\n",
4793 hdspm->system_sample_rate);
4794
4795 snd_iprintf(buffer, "Double speed: %s\n",
4796 hdspm->control_register & HDSPM_DS_DoubleWire?
4797 "Double wire" : "Single wire");
4798 snd_iprintf(buffer, "Quad speed: %s\n",
4799 hdspm->control_register & HDSPM_QS_DoubleWire?
4800 "Double wire" :
4801 hdspm->control_register & HDSPM_QS_QuadWire?
4802 "Quad wire" : "Single wire");
4803
4804 snd_iprintf(buffer, "--- Status:\n");
4805
Andre Schramm56bde0f2013-01-09 14:40:18 +01004806 wcLock = status & HDSPM_AES32_wcLock;
4807 wcSync = wcLock && (status & HDSPM_AES32_wcSync);
4808
Remy Bruno3cee5a62006-10-16 12:46:32 +02004809 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Andre Schramm56bde0f2013-01-09 14:40:18 +01004810 (wcLock) ? (wcSync ? "Sync " : "Lock ") : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004811 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004812
4813 for (x = 0; x < 8; x++) {
4814 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004815 x+1,
4816 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01004817 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004818 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004819 }
4820
4821 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004822 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
4823 autosync_ref = "None"; break;
4824 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
4825 autosync_ref = "Word Clock"; break;
4826 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
4827 autosync_ref = "AES1"; break;
4828 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
4829 autosync_ref = "AES2"; break;
4830 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
4831 autosync_ref = "AES3"; break;
4832 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
4833 autosync_ref = "AES4"; break;
4834 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
4835 autosync_ref = "AES5"; break;
4836 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
4837 autosync_ref = "AES6"; break;
4838 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
4839 autosync_ref = "AES7"; break;
4840 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
4841 autosync_ref = "AES8"; break;
4842 default:
4843 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004844 }
4845 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
4846
4847 snd_iprintf(buffer, "\n");
4848}
4849
Adrian Knoth0dca1792011-01-26 19:32:14 +01004850static void
4851snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
4852 struct snd_info_buffer *buffer)
4853{
4854 struct hdspm *hdspm = entry->private_data;
4855 unsigned int status1, status2, status3, control, i;
4856 unsigned int lock, sync;
4857
4858 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
4859 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
4860 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
4861
4862 control = hdspm->control_register;
4863
4864 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
4865 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
4866 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
4867
4868
4869 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
4870
4871 snd_iprintf(buffer, "Clock mode : %s\n",
4872 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
4873 snd_iprintf(buffer, "System frequency: %d Hz\n",
4874 hdspm_get_system_sample_rate(hdspm));
4875
4876 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
4877
4878 lock = 0x1;
4879 sync = 0x100;
4880
4881 for (i = 0; i < 8; i++) {
4882 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
4883 i,
4884 (status1 & lock) ? 1 : 0,
4885 (status1 & sync) ? 1 : 0,
4886 texts_freq[(status2 >> (i * 4)) & 0xF]);
4887
4888 lock = lock<<1;
4889 sync = sync<<1;
4890 }
4891
4892 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
4893 (status1 & 0x1000000) ? 1 : 0,
4894 (status1 & 0x2000000) ? 1 : 0,
4895 texts_freq[(status1 >> 16) & 0xF]);
4896
4897 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
4898 (status1 & 0x4000000) ? 1 : 0,
4899 (status1 & 0x8000000) ? 1 : 0,
4900 texts_freq[(status1 >> 20) & 0xF]);
4901
4902 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
4903 (status3 & 0x400) ? 1 : 0,
4904 (status3 & 0x800) ? 1 : 0,
4905 texts_freq[(status2 >> 12) & 0xF]);
4906
4907}
4908
Remy Bruno3cee5a62006-10-16 12:46:32 +02004909#ifdef CONFIG_SND_DEBUG
4910static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01004911snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004912 struct snd_info_buffer *buffer)
4913{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004914 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004915
4916 int j,i;
4917
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004918 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004919 snd_iprintf(buffer, "0x%08X: ", i);
4920 for (j = 0; j < 16; j += 4)
4921 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
4922 snd_iprintf(buffer, "\n");
4923 }
4924}
4925#endif
4926
4927
Adrian Knoth0dca1792011-01-26 19:32:14 +01004928static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
4929 struct snd_info_buffer *buffer)
4930{
4931 struct hdspm *hdspm = entry->private_data;
4932 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004933
Adrian Knoth0dca1792011-01-26 19:32:14 +01004934 snd_iprintf(buffer, "# generated by hdspm\n");
4935
4936 for (i = 0; i < hdspm->max_channels_in; i++) {
4937 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
4938 }
4939}
4940
4941static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
4942 struct snd_info_buffer *buffer)
4943{
4944 struct hdspm *hdspm = entry->private_data;
4945 int i;
4946
4947 snd_iprintf(buffer, "# generated by hdspm\n");
4948
4949 for (i = 0; i < hdspm->max_channels_out; i++) {
4950 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
4951 }
4952}
4953
4954
Bill Pembertone23e7a12012-12-06 12:35:10 -05004955static void snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004956{
Takashi Iwai98274f02005-11-17 14:52:34 +01004957 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02004958
Adrian Knoth0dca1792011-01-26 19:32:14 +01004959 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
4960 switch (hdspm->io_type) {
4961 case AES32:
4962 snd_info_set_text_ops(entry, hdspm,
4963 snd_hdspm_proc_read_aes32);
4964 break;
4965 case MADI:
4966 snd_info_set_text_ops(entry, hdspm,
4967 snd_hdspm_proc_read_madi);
4968 break;
4969 case MADIface:
4970 /* snd_info_set_text_ops(entry, hdspm,
4971 snd_hdspm_proc_read_madiface); */
4972 break;
4973 case RayDAT:
4974 snd_info_set_text_ops(entry, hdspm,
4975 snd_hdspm_proc_read_raydat);
4976 break;
4977 case AIO:
4978 break;
4979 }
4980 }
4981
4982 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
4983 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
4984 }
4985
4986 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
4987 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
4988 }
4989
Remy Bruno3cee5a62006-10-16 12:46:32 +02004990#ifdef CONFIG_SND_DEBUG
4991 /* debug file to read all hdspm registers */
4992 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
4993 snd_info_set_text_ops(entry, hdspm,
4994 snd_hdspm_proc_read_debug);
4995#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02004996}
4997
4998/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004999 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005000 ------------------------------------------------------------*/
5001
Takashi Iwai98274f02005-11-17 14:52:34 +01005002static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005003{
Takashi Iwai763f3562005-06-03 11:25:34 +02005004 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005005 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005006 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005007
5008 /* set defaults: */
5009
Adrian Knoth0dca1792011-01-26 19:32:14 +01005010 hdspm->settings_register = 0;
5011
5012 switch (hdspm->io_type) {
5013 case MADI:
5014 case MADIface:
5015 hdspm->control_register =
5016 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5017 break;
5018
5019 case RayDAT:
5020 case AIO:
5021 hdspm->settings_register = 0x1 + 0x1000;
5022 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5023 * line_out */
5024 hdspm->control_register =
5025 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5026 break;
5027
5028 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005029 hdspm->control_register =
5030 HDSPM_ClockModeMaster | /* Master Cloack Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005031 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005032 HDSPM_SyncRef0 | /* AES1 is syncclock */
5033 HDSPM_LineOut | /* Analog output in */
5034 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005035 break;
5036 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005037
5038 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5039
Adrian Knoth0dca1792011-01-26 19:32:14 +01005040 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005041 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005042#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005043 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005044#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005045 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005046#endif
5047
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005048 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5049 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005050 hdspm_compute_period_size(hdspm);
5051
5052 /* silence everything */
5053
5054 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5055
Adrian Knoth0dca1792011-01-26 19:32:14 +01005056 if (hdspm->io_type == AIO || hdspm->io_type == RayDAT) {
5057 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005058 }
5059
5060 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005061 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005062
5063 return 0;
5064}
5065
5066
5067/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005068 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005069 ------------------------------------------------------------*/
5070
David Howells7d12e782006-10-05 14:55:46 +01005071static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005072{
Takashi Iwai98274f02005-11-17 14:52:34 +01005073 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005074 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005075 int i, audio, midi, schedule = 0;
5076 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005077
5078 status = hdspm_read(hdspm, HDSPM_statusRegister);
5079
5080 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005081 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5082 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005083
Adrian Knoth0dca1792011-01-26 19:32:14 +01005084 /* now = get_cycles(); */
5085 /**
5086 * LAT_2..LAT_0 period counter (win) counter (mac)
5087 * 6 4096 ~256053425 ~514672358
5088 * 5 2048 ~128024983 ~257373821
5089 * 4 1024 ~64023706 ~128718089
5090 * 3 512 ~32005945 ~64385999
5091 * 2 256 ~16003039 ~32260176
5092 * 1 128 ~7998738 ~16194507
5093 * 0 64 ~3998231 ~8191558
5094 **/
5095 /*
5096 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5097 now-hdspm->last_interrupt, status & 0xFFC0);
5098 hdspm->last_interrupt = now;
5099 */
5100
5101 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005102 return IRQ_NONE;
5103
5104 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5105 hdspm->irq_count++;
5106
Takashi Iwai763f3562005-06-03 11:25:34 +02005107
5108 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005109 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005110 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005111
5112 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005113 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005114 }
5115
Adrian Knoth0dca1792011-01-26 19:32:14 +01005116 if (midi) {
5117 i = 0;
5118 while (i < hdspm->midiPorts) {
5119 if ((hdspm_read(hdspm,
5120 hdspm->midi[i].statusIn) & 0xff) &&
5121 (status & hdspm->midi[i].irq)) {
5122 /* we disable interrupts for this input until
5123 * processing is done
5124 */
5125 hdspm->control_register &= ~hdspm->midi[i].ie;
5126 hdspm_write(hdspm, HDSPM_controlRegister,
5127 hdspm->control_register);
5128 hdspm->midi[i].pending = 1;
5129 schedule = 1;
5130 }
5131
5132 i++;
5133 }
5134
5135 if (schedule)
5136 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005137 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005138
Takashi Iwai763f3562005-06-03 11:25:34 +02005139 return IRQ_HANDLED;
5140}
5141
5142/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005143 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005144 ------------------------------------------------------------*/
5145
5146
Adrian Knoth0dca1792011-01-26 19:32:14 +01005147static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5148 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005149{
Takashi Iwai98274f02005-11-17 14:52:34 +01005150 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005151 return hdspm_hw_pointer(hdspm);
5152}
5153
Takashi Iwai763f3562005-06-03 11:25:34 +02005154
Takashi Iwai98274f02005-11-17 14:52:34 +01005155static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005156{
Takashi Iwai98274f02005-11-17 14:52:34 +01005157 struct snd_pcm_runtime *runtime = substream->runtime;
5158 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5159 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005160
5161 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5162 other = hdspm->capture_substream;
5163 else
5164 other = hdspm->playback_substream;
5165
5166 if (hdspm->running)
5167 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5168 else
5169 runtime->status->hw_ptr = 0;
5170 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005171 struct snd_pcm_substream *s;
5172 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005173 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005174 if (s == other) {
5175 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005176 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005177 break;
5178 }
5179 }
5180 }
5181 return 0;
5182}
5183
Takashi Iwai98274f02005-11-17 14:52:34 +01005184static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5185 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005186{
Takashi Iwai98274f02005-11-17 14:52:34 +01005187 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005188 int err;
5189 int i;
5190 pid_t this_pid;
5191 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005192
5193 spin_lock_irq(&hdspm->lock);
5194
5195 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5196 this_pid = hdspm->playback_pid;
5197 other_pid = hdspm->capture_pid;
5198 } else {
5199 this_pid = hdspm->capture_pid;
5200 other_pid = hdspm->playback_pid;
5201 }
5202
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005203 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005204
5205 /* The other stream is open, and not by the same
5206 task as this one. Make sure that the parameters
5207 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005208 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005209
5210 if (params_rate(params) != hdspm->system_sample_rate) {
5211 spin_unlock_irq(&hdspm->lock);
5212 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005213 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005214 return -EBUSY;
5215 }
5216
5217 if (params_period_size(params) != hdspm->period_bytes / 4) {
5218 spin_unlock_irq(&hdspm->lock);
5219 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005220 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005221 return -EBUSY;
5222 }
5223
5224 }
5225 /* We're fine. */
5226 spin_unlock_irq(&hdspm->lock);
5227
5228 /* how to make sure that the rate matches an externally-set one ? */
5229
5230 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005231 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5232 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005233 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005234 spin_unlock_irq(&hdspm->lock);
5235 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005236 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005237 return err;
5238 }
5239 spin_unlock_irq(&hdspm->lock);
5240
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005241 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005242 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005243 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005244 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005245 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005246 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005247 return err;
5248 }
5249
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005250 /* Memory allocation, takashi's method, dont know if we should
5251 * spinlock
5252 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005253 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005254 /* Update for MADI rev 204: we need to allocate for all channels,
5255 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005256
Takashi Iwai763f3562005-06-03 11:25:34 +02005257 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005258 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5259 if (err < 0) {
5260 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005261 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005262 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005263
Takashi Iwai763f3562005-06-03 11:25:34 +02005264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5265
Takashi Iwai77a23f22008-08-21 13:00:13 +02005266 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005267 params_channels(params));
5268
5269 for (i = 0; i < params_channels(params); ++i)
5270 snd_hdspm_enable_out(hdspm, i, 1);
5271
5272 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005273 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005274 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005275 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005276 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005277 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005278 params_channels(params));
5279
5280 for (i = 0; i < params_channels(params); ++i)
5281 snd_hdspm_enable_in(hdspm, i, 1);
5282
5283 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005284 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005285 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005286 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005287 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005288
Remy Bruno3cee5a62006-10-16 12:46:32 +02005289 /*
5290 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5291 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5292 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005293 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005294 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005295 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005296 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5297 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5298 "playback" : "capture",
5299 params_rate(params), params_channels(params),
5300 params_buffer_size(params));
5301 */
5302
5303
5304 /* Switch to native float format if requested */
5305 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5306 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5307 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5308
5309 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5310 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5311 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5312 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5313
5314 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5315 }
5316 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5317
Takashi Iwai763f3562005-06-03 11:25:34 +02005318 return 0;
5319}
5320
Takashi Iwai98274f02005-11-17 14:52:34 +01005321static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005322{
5323 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005324 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005325
5326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5327
Adrian Knoth0dca1792011-01-26 19:32:14 +01005328 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005329 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005330 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005331 snd_hdspm_enable_out(hdspm, i, 0);
5332
5333 hdspm->playback_buffer = NULL;
5334 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005335 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005336 snd_hdspm_enable_in(hdspm, i, 0);
5337
5338 hdspm->capture_buffer = NULL;
5339
5340 }
5341
5342 snd_pcm_lib_free_pages(substream);
5343
5344 return 0;
5345}
5346
Adrian Knoth0dca1792011-01-26 19:32:14 +01005347
Takashi Iwai98274f02005-11-17 14:52:34 +01005348static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005349 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005350{
Takashi Iwai98274f02005-11-17 14:52:34 +01005351 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005352
Adrian Knoth0dca1792011-01-26 19:32:14 +01005353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5354 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5355 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5356 return -EINVAL;
5357 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005358
Adrian Knoth0dca1792011-01-26 19:32:14 +01005359 if (hdspm->channel_map_out[info->channel] < 0) {
5360 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5361 return -EINVAL;
5362 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005363
Adrian Knoth0dca1792011-01-26 19:32:14 +01005364 info->offset = hdspm->channel_map_out[info->channel] *
5365 HDSPM_CHANNEL_BUFFER_BYTES;
5366 } else {
5367 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5368 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5369 return -EINVAL;
5370 }
5371
5372 if (hdspm->channel_map_in[info->channel] < 0) {
5373 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5374 return -EINVAL;
5375 }
5376
5377 info->offset = hdspm->channel_map_in[info->channel] *
5378 HDSPM_CHANNEL_BUFFER_BYTES;
5379 }
5380
Takashi Iwai763f3562005-06-03 11:25:34 +02005381 info->first = 0;
5382 info->step = 32;
5383 return 0;
5384}
5385
Adrian Knoth0dca1792011-01-26 19:32:14 +01005386
Takashi Iwai98274f02005-11-17 14:52:34 +01005387static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005388 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005389{
5390 switch (cmd) {
5391 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005392 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005393
5394 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005395 {
5396 struct snd_pcm_channel_info *info = arg;
5397 return snd_hdspm_channel_info(substream, info);
5398 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005399 default:
5400 break;
5401 }
5402
5403 return snd_pcm_lib_ioctl(substream, cmd, arg);
5404}
5405
Takashi Iwai98274f02005-11-17 14:52:34 +01005406static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005407{
Takashi Iwai98274f02005-11-17 14:52:34 +01005408 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5409 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005410 int running;
5411
5412 spin_lock(&hdspm->lock);
5413 running = hdspm->running;
5414 switch (cmd) {
5415 case SNDRV_PCM_TRIGGER_START:
5416 running |= 1 << substream->stream;
5417 break;
5418 case SNDRV_PCM_TRIGGER_STOP:
5419 running &= ~(1 << substream->stream);
5420 break;
5421 default:
5422 snd_BUG();
5423 spin_unlock(&hdspm->lock);
5424 return -EINVAL;
5425 }
5426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5427 other = hdspm->capture_substream;
5428 else
5429 other = hdspm->playback_substream;
5430
5431 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005432 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005433 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005434 if (s == other) {
5435 snd_pcm_trigger_done(s, substream);
5436 if (cmd == SNDRV_PCM_TRIGGER_START)
5437 running |= 1 << s->stream;
5438 else
5439 running &= ~(1 << s->stream);
5440 goto _ok;
5441 }
5442 }
5443 if (cmd == SNDRV_PCM_TRIGGER_START) {
5444 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005445 && substream->stream ==
5446 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005447 hdspm_silence_playback(hdspm);
5448 } else {
5449 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005450 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005451 hdspm_silence_playback(hdspm);
5452 }
5453 } else {
5454 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5455 hdspm_silence_playback(hdspm);
5456 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005457_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005458 snd_pcm_trigger_done(substream, substream);
5459 if (!hdspm->running && running)
5460 hdspm_start_audio(hdspm);
5461 else if (hdspm->running && !running)
5462 hdspm_stop_audio(hdspm);
5463 hdspm->running = running;
5464 spin_unlock(&hdspm->lock);
5465
5466 return 0;
5467}
5468
Takashi Iwai98274f02005-11-17 14:52:34 +01005469static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005470{
5471 return 0;
5472}
5473
Takashi Iwai98274f02005-11-17 14:52:34 +01005474static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005475 .info = (SNDRV_PCM_INFO_MMAP |
5476 SNDRV_PCM_INFO_MMAP_VALID |
5477 SNDRV_PCM_INFO_NONINTERLEAVED |
5478 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5479 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5480 .rates = (SNDRV_PCM_RATE_32000 |
5481 SNDRV_PCM_RATE_44100 |
5482 SNDRV_PCM_RATE_48000 |
5483 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005484 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5485 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005486 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005487 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005488 .channels_min = 1,
5489 .channels_max = HDSPM_MAX_CHANNELS,
5490 .buffer_bytes_max =
5491 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005492 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005493 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005494 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005495 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005496 .fifo_size = 0
5497};
5498
Takashi Iwai98274f02005-11-17 14:52:34 +01005499static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005500 .info = (SNDRV_PCM_INFO_MMAP |
5501 SNDRV_PCM_INFO_MMAP_VALID |
5502 SNDRV_PCM_INFO_NONINTERLEAVED |
5503 SNDRV_PCM_INFO_SYNC_START),
5504 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5505 .rates = (SNDRV_PCM_RATE_32000 |
5506 SNDRV_PCM_RATE_44100 |
5507 SNDRV_PCM_RATE_48000 |
5508 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005509 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5510 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005511 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005512 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005513 .channels_min = 1,
5514 .channels_max = HDSPM_MAX_CHANNELS,
5515 .buffer_bytes_max =
5516 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
Adrian Knoth1b6fa102011-08-15 00:22:51 +02005517 .period_bytes_min = (32 * 4),
Takashi Iwai52e6fb42011-08-15 10:40:59 +02005518 .period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005519 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005520 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005521 .fifo_size = 0
5522};
5523
Adrian Knoth0dca1792011-01-26 19:32:14 +01005524static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5525 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005526{
Takashi Iwai98274f02005-11-17 14:52:34 +01005527 struct hdspm *hdspm = rule->private;
5528 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005529 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005530 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005531 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5532
Adrian Knoth0dca1792011-01-26 19:32:14 +01005533 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005534 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005535 .min = hdspm->qs_in_channels,
5536 .max = hdspm->qs_in_channels,
5537 .integer = 1,
5538 };
5539 return snd_interval_refine(c, &t);
5540 } else if (r->min > 48000 && r->max <= 96000) {
5541 struct snd_interval t = {
5542 .min = hdspm->ds_in_channels,
5543 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005544 .integer = 1,
5545 };
5546 return snd_interval_refine(c, &t);
5547 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005548 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005549 .min = hdspm->ss_in_channels,
5550 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005551 .integer = 1,
5552 };
5553 return snd_interval_refine(c, &t);
5554 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005555
Takashi Iwai763f3562005-06-03 11:25:34 +02005556 return 0;
5557}
5558
Adrian Knoth0dca1792011-01-26 19:32:14 +01005559static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005560 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005561{
Takashi Iwai98274f02005-11-17 14:52:34 +01005562 struct hdspm *hdspm = rule->private;
5563 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005564 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005565 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005566 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5567
Adrian Knoth0dca1792011-01-26 19:32:14 +01005568 if (r->min > 96000 && r->max <= 192000) {
5569 struct snd_interval t = {
5570 .min = hdspm->qs_out_channels,
5571 .max = hdspm->qs_out_channels,
5572 .integer = 1,
5573 };
5574 return snd_interval_refine(c, &t);
5575 } else if (r->min > 48000 && r->max <= 96000) {
5576 struct snd_interval t = {
5577 .min = hdspm->ds_out_channels,
5578 .max = hdspm->ds_out_channels,
5579 .integer = 1,
5580 };
5581 return snd_interval_refine(c, &t);
5582 } else if (r->max < 64000) {
5583 struct snd_interval t = {
5584 .min = hdspm->ss_out_channels,
5585 .max = hdspm->ss_out_channels,
5586 .integer = 1,
5587 };
5588 return snd_interval_refine(c, &t);
5589 } else {
5590 }
5591 return 0;
5592}
5593
5594static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5595 struct snd_pcm_hw_rule * rule)
5596{
5597 struct hdspm *hdspm = rule->private;
5598 struct snd_interval *c =
5599 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5600 struct snd_interval *r =
5601 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5602
5603 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005604 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005605 .min = 32000,
5606 .max = 48000,
5607 .integer = 1,
5608 };
5609 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005610 } else if (c->max <= hdspm->qs_in_channels) {
5611 struct snd_interval t = {
5612 .min = 128000,
5613 .max = 192000,
5614 .integer = 1,
5615 };
5616 return snd_interval_refine(r, &t);
5617 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005618 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005619 .min = 64000,
5620 .max = 96000,
5621 .integer = 1,
5622 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005623 return snd_interval_refine(r, &t);
5624 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005625
5626 return 0;
5627}
5628static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5629 struct snd_pcm_hw_rule *rule)
5630{
5631 struct hdspm *hdspm = rule->private;
5632 struct snd_interval *c =
5633 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5634 struct snd_interval *r =
5635 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5636
5637 if (c->min >= hdspm->ss_out_channels) {
5638 struct snd_interval t = {
5639 .min = 32000,
5640 .max = 48000,
5641 .integer = 1,
5642 };
5643 return snd_interval_refine(r, &t);
5644 } else if (c->max <= hdspm->qs_out_channels) {
5645 struct snd_interval t = {
5646 .min = 128000,
5647 .max = 192000,
5648 .integer = 1,
5649 };
5650 return snd_interval_refine(r, &t);
5651 } else if (c->max <= hdspm->ds_out_channels) {
5652 struct snd_interval t = {
5653 .min = 64000,
5654 .max = 96000,
5655 .integer = 1,
5656 };
5657 return snd_interval_refine(r, &t);
5658 }
5659
Takashi Iwai763f3562005-06-03 11:25:34 +02005660 return 0;
5661}
5662
Adrian Knoth0dca1792011-01-26 19:32:14 +01005663static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005664 struct snd_pcm_hw_rule *rule)
5665{
5666 unsigned int list[3];
5667 struct hdspm *hdspm = rule->private;
5668 struct snd_interval *c = hw_param_interval(params,
5669 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005670
5671 list[0] = hdspm->qs_in_channels;
5672 list[1] = hdspm->ds_in_channels;
5673 list[2] = hdspm->ss_in_channels;
5674 return snd_interval_list(c, 3, list, 0);
5675}
5676
5677static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5678 struct snd_pcm_hw_rule *rule)
5679{
5680 unsigned int list[3];
5681 struct hdspm *hdspm = rule->private;
5682 struct snd_interval *c = hw_param_interval(params,
5683 SNDRV_PCM_HW_PARAM_CHANNELS);
5684
5685 list[0] = hdspm->qs_out_channels;
5686 list[1] = hdspm->ds_out_channels;
5687 list[2] = hdspm->ss_out_channels;
5688 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005689}
5690
5691
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005692static unsigned int hdspm_aes32_sample_rates[] = {
5693 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5694};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005695
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005696static struct snd_pcm_hw_constraint_list
5697hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005698 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5699 .list = hdspm_aes32_sample_rates,
5700 .mask = 0
5701};
5702
Takashi Iwai98274f02005-11-17 14:52:34 +01005703static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005704{
Takashi Iwai98274f02005-11-17 14:52:34 +01005705 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5706 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005707
Takashi Iwai763f3562005-06-03 11:25:34 +02005708 spin_lock_irq(&hdspm->lock);
5709
5710 snd_pcm_set_sync(substream);
5711
Adrian Knoth0dca1792011-01-26 19:32:14 +01005712
Takashi Iwai763f3562005-06-03 11:25:34 +02005713 runtime->hw = snd_hdspm_playback_subinfo;
5714
5715 if (hdspm->capture_substream == NULL)
5716 hdspm_stop_audio(hdspm);
5717
5718 hdspm->playback_pid = current->pid;
5719 hdspm->playback_substream = substream;
5720
5721 spin_unlock_irq(&hdspm->lock);
5722
5723 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005724 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005725
Adrian Knoth0dca1792011-01-26 19:32:14 +01005726 switch (hdspm->io_type) {
5727 case AIO:
5728 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005729 snd_pcm_hw_constraint_minmax(runtime,
5730 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5731 32, 4096);
5732 /* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
5733 snd_pcm_hw_constraint_minmax(runtime,
5734 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5735 16384, 16384);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005736 break;
5737
5738 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005739 snd_pcm_hw_constraint_minmax(runtime,
5740 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5741 64, 8192);
5742 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005743 }
5744
5745 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02005746 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005747 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5748 &hdspm_hw_constraints_aes32_sample_rates);
5749 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005750 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005751 snd_hdspm_hw_rule_rate_out_channels, hdspm,
5752 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005753 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005754
5755 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5756 snd_hdspm_hw_rule_out_channels, hdspm,
5757 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5758
5759 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5760 snd_hdspm_hw_rule_out_channels_rate, hdspm,
5761 SNDRV_PCM_HW_PARAM_RATE, -1);
5762
Takashi Iwai763f3562005-06-03 11:25:34 +02005763 return 0;
5764}
5765
Takashi Iwai98274f02005-11-17 14:52:34 +01005766static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005767{
Takashi Iwai98274f02005-11-17 14:52:34 +01005768 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005769
5770 spin_lock_irq(&hdspm->lock);
5771
5772 hdspm->playback_pid = -1;
5773 hdspm->playback_substream = NULL;
5774
5775 spin_unlock_irq(&hdspm->lock);
5776
5777 return 0;
5778}
5779
5780
Takashi Iwai98274f02005-11-17 14:52:34 +01005781static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005782{
Takashi Iwai98274f02005-11-17 14:52:34 +01005783 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5784 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005785
5786 spin_lock_irq(&hdspm->lock);
5787 snd_pcm_set_sync(substream);
5788 runtime->hw = snd_hdspm_capture_subinfo;
5789
5790 if (hdspm->playback_substream == NULL)
5791 hdspm_stop_audio(hdspm);
5792
5793 hdspm->capture_pid = current->pid;
5794 hdspm->capture_substream = substream;
5795
5796 spin_unlock_irq(&hdspm->lock);
5797
5798 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Takashi Iwaid8776812011-08-15 10:45:42 +02005799 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
5800
Adrian Knoth0dca1792011-01-26 19:32:14 +01005801 switch (hdspm->io_type) {
5802 case AIO:
5803 case RayDAT:
Takashi Iwaid8776812011-08-15 10:45:42 +02005804 snd_pcm_hw_constraint_minmax(runtime,
5805 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5806 32, 4096);
5807 snd_pcm_hw_constraint_minmax(runtime,
5808 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5809 16384, 16384);
5810 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005811
5812 default:
Takashi Iwaid8776812011-08-15 10:45:42 +02005813 snd_pcm_hw_constraint_minmax(runtime,
5814 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5815 64, 8192);
5816 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005817 }
5818
5819 if (AES32 == hdspm->io_type) {
Takashi Iwai3fa9e3d2011-08-15 10:42:23 +02005820 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005821 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5822 &hdspm_hw_constraints_aes32_sample_rates);
5823 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005824 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005825 snd_hdspm_hw_rule_rate_in_channels, hdspm,
5826 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005827 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005828
5829 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5830 snd_hdspm_hw_rule_in_channels, hdspm,
5831 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5832
5833 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5834 snd_hdspm_hw_rule_in_channels_rate, hdspm,
5835 SNDRV_PCM_HW_PARAM_RATE, -1);
5836
Takashi Iwai763f3562005-06-03 11:25:34 +02005837 return 0;
5838}
5839
Takashi Iwai98274f02005-11-17 14:52:34 +01005840static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005841{
Takashi Iwai98274f02005-11-17 14:52:34 +01005842 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005843
5844 spin_lock_irq(&hdspm->lock);
5845
5846 hdspm->capture_pid = -1;
5847 hdspm->capture_substream = NULL;
5848
5849 spin_unlock_irq(&hdspm->lock);
5850 return 0;
5851}
5852
Adrian Knoth0dca1792011-01-26 19:32:14 +01005853static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02005854{
Adrian Knoth0dca1792011-01-26 19:32:14 +01005855 /* we have nothing to initialize but the call is required */
5856 return 0;
5857}
5858
5859static inline int copy_u32_le(void __user *dest, void __iomem *src)
5860{
5861 u32 val = readl(src);
5862 return copy_to_user(dest, &val, 4);
5863}
5864
5865static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
Dan Carpenter2ca595a2011-09-23 09:25:05 +03005866 unsigned int cmd, unsigned long arg)
Adrian Knoth0dca1792011-01-26 19:32:14 +01005867{
5868 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005869 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01005870 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005871 struct hdspm_config info;
5872 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01005873 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005874 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005875 struct hdspm_ltc ltc;
5876 unsigned int statusregister;
5877 long unsigned int s;
5878 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005879
5880 switch (cmd) {
5881
Takashi Iwai763f3562005-06-03 11:25:34 +02005882 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005883 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005884 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005885 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005886 readl(hdspm->iobase +
5887 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005888 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005889 readl(hdspm->iobase +
5890 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005891 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005892 readl(hdspm->iobase +
5893 HDSPM_MADI_OUTPUT_PEAK + i*4);
5894
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005895 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005896 ((uint64_t) readl(hdspm->iobase +
5897 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
5898 (uint64_t) readl(hdspm->iobase +
5899 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005900 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005901 ((uint64_t)readl(hdspm->iobase +
5902 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
5903 (uint64_t)readl(hdspm->iobase +
5904 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005905 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005906 ((uint64_t)readl(hdspm->iobase +
5907 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
5908 (uint64_t)readl(hdspm->iobase +
5909 HDSPM_MADI_OUTPUT_RMS_L + i*4);
5910 }
5911
5912 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005913 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005914 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005915 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005916 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005917 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005918 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005919 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005920
Jaroslav Kysela730a5862011-01-27 13:03:15 +01005921 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005922 if (0 != s) {
5923 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
5924 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
5925 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005926 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005927 }
5928 break;
5929
5930 case SNDRV_HDSPM_IOCTL_GET_LTC:
5931 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
5932 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
5933 if (i & HDSPM_TCO1_LTC_Input_valid) {
5934 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
5935 HDSPM_TCO1_LTC_Format_MSB)) {
5936 case 0:
5937 ltc.format = fps_24;
5938 break;
5939 case HDSPM_TCO1_LTC_Format_LSB:
5940 ltc.format = fps_25;
5941 break;
5942 case HDSPM_TCO1_LTC_Format_MSB:
5943 ltc.format = fps_2997;
5944 break;
5945 default:
5946 ltc.format = 30;
5947 break;
5948 }
5949 if (i & HDSPM_TCO1_set_drop_frame_flag) {
5950 ltc.frame = drop_frame;
5951 } else {
5952 ltc.frame = full_frame;
5953 }
5954 } else {
5955 ltc.format = format_invalid;
5956 ltc.frame = frame_invalid;
5957 }
5958 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
5959 ltc.input_format = ntsc;
5960 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
5961 ltc.input_format = pal;
5962 } else {
5963 ltc.input_format = no_video;
5964 }
5965
5966 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
5967 if (0 != s) {
5968 /*
5969 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02005970 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005971 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005972
5973 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02005974
Adrian Knoth0dca1792011-01-26 19:32:14 +01005975 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02005976
Adrian Knoth4ab69a22011-02-23 11:43:14 +01005977 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02005978 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005979 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
5980 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02005981
5982 info.system_sample_rate = hdspm->system_sample_rate;
5983 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005984 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005985 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
5986 info.clock_source = hdspm_clock_source(hdspm);
5987 info.autosync_ref = hdspm_autosync_ref(hdspm);
Adrian Knothc9e16682012-12-03 14:55:50 +01005988 info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
Takashi Iwai763f3562005-06-03 11:25:34 +02005989 info.passthru = 0;
5990 spin_unlock_irq(&hdspm->lock);
Dan Carpenter2ca595a2011-09-23 09:25:05 +03005991 if (copy_to_user(argp, &info, sizeof(info)))
Takashi Iwai763f3562005-06-03 11:25:34 +02005992 return -EFAULT;
5993 break;
5994
Adrian Knoth0dca1792011-01-26 19:32:14 +01005995 case SNDRV_HDSPM_IOCTL_GET_STATUS:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03005996 memset(&status, 0, sizeof(status));
5997
Adrian Knoth0dca1792011-01-26 19:32:14 +01005998 status.card_type = hdspm->io_type;
5999
6000 status.autosync_source = hdspm_autosync_ref(hdspm);
6001
6002 status.card_clock = 110069313433624ULL;
6003 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6004
6005 switch (hdspm->io_type) {
6006 case MADI:
6007 case MADIface:
6008 status.card_specific.madi.sync_wc =
6009 hdspm_wc_sync_check(hdspm);
6010 status.card_specific.madi.sync_madi =
6011 hdspm_madi_sync_check(hdspm);
6012 status.card_specific.madi.sync_tco =
6013 hdspm_tco_sync_check(hdspm);
6014 status.card_specific.madi.sync_in =
6015 hdspm_sync_in_sync_check(hdspm);
6016
6017 statusregister =
6018 hdspm_read(hdspm, HDSPM_statusRegister);
6019 status.card_specific.madi.madi_input =
6020 (statusregister & HDSPM_AB_int) ? 1 : 0;
6021 status.card_specific.madi.channel_format =
Adrian Knoth9e6ff522011-10-27 21:57:52 +02006022 (statusregister & HDSPM_RX_64ch) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006023 /* TODO: Mac driver sets it when f_s>48kHz */
6024 status.card_specific.madi.frame_format = 0;
6025
6026 default:
6027 break;
6028 }
6029
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006030 if (copy_to_user(argp, &status, sizeof(status)))
Adrian Knoth0dca1792011-01-26 19:32:14 +01006031 return -EFAULT;
6032
6033
6034 break;
6035
Takashi Iwai763f3562005-06-03 11:25:34 +02006036 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Dan Carpenter643d6bb2011-09-23 09:24:21 +03006037 memset(&hdspm_version, 0, sizeof(hdspm_version));
6038
Adrian Knoth0dca1792011-01-26 19:32:14 +01006039 hdspm_version.card_type = hdspm->io_type;
6040 strncpy(hdspm_version.cardname, hdspm->card_name,
6041 sizeof(hdspm_version.cardname));
Adrian Knoth7d53a632012-01-04 14:31:16 +01006042 hdspm_version.serial = hdspm->serial;
Takashi Iwai763f3562005-06-03 11:25:34 +02006043 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006044 hdspm_version.addons = 0;
6045 if (hdspm->tco)
6046 hdspm_version.addons |= HDSPM_ADDON_TCO;
6047
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006048 if (copy_to_user(argp, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006049 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006050 return -EFAULT;
6051 break;
6052
6053 case SNDRV_HDSPM_IOCTL_GET_MIXER:
Dan Carpenter2ca595a2011-09-23 09:25:05 +03006054 if (copy_from_user(&mixer, argp, sizeof(mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006055 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006056 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006057 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006058 return -EFAULT;
6059 break;
6060
6061 default:
6062 return -EINVAL;
6063 }
6064 return 0;
6065}
6066
Takashi Iwai98274f02005-11-17 14:52:34 +01006067static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006068 .open = snd_hdspm_playback_open,
6069 .close = snd_hdspm_playback_release,
6070 .ioctl = snd_hdspm_ioctl,
6071 .hw_params = snd_hdspm_hw_params,
6072 .hw_free = snd_hdspm_hw_free,
6073 .prepare = snd_hdspm_prepare,
6074 .trigger = snd_hdspm_trigger,
6075 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006076 .page = snd_pcm_sgbuf_ops_page,
6077};
6078
Takashi Iwai98274f02005-11-17 14:52:34 +01006079static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006080 .open = snd_hdspm_capture_open,
6081 .close = snd_hdspm_capture_release,
6082 .ioctl = snd_hdspm_ioctl,
6083 .hw_params = snd_hdspm_hw_params,
6084 .hw_free = snd_hdspm_hw_free,
6085 .prepare = snd_hdspm_prepare,
6086 .trigger = snd_hdspm_trigger,
6087 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006088 .page = snd_pcm_sgbuf_ops_page,
6089};
6090
Bill Pembertone23e7a12012-12-06 12:35:10 -05006091static int snd_hdspm_create_hwdep(struct snd_card *card,
6092 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006093{
Takashi Iwai98274f02005-11-17 14:52:34 +01006094 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006095 int err;
6096
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006097 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6098 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006099 return err;
6100
6101 hdspm->hwdep = hw;
6102 hw->private_data = hdspm;
6103 strcpy(hw->name, "HDSPM hwdep interface");
6104
Adrian Knoth0dca1792011-01-26 19:32:14 +01006105 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006106 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth8de5d6f2012-03-08 15:38:04 +01006107 hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006108 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006109
6110 return 0;
6111}
6112
6113
6114/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006115 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006116 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05006117static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006118{
6119 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006120 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006121 size_t wanted;
6122
6123 pcm = hdspm->pcm;
6124
Remy Bruno3cee5a62006-10-16 12:46:32 +02006125 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006126
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006127 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006128 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006129 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006130 snd_dma_pci_data(hdspm->pci),
6131 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006132 wanted);
6133 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006134 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006135
6136 return err;
6137 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006138 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006139
6140 return 0;
6141}
6142
Adrian Knoth0dca1792011-01-26 19:32:14 +01006143
6144static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006145 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006146 unsigned int reg, int channels)
6147{
6148 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006149
6150 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006151 for (i = 0; i < (channels * 16); i++)
6152 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006153 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006154}
6155
Adrian Knoth0dca1792011-01-26 19:32:14 +01006156
Takashi Iwai763f3562005-06-03 11:25:34 +02006157/* ------------- ALSA Devices ---------------------------- */
Bill Pembertone23e7a12012-12-06 12:35:10 -05006158static int snd_hdspm_create_pcm(struct snd_card *card,
6159 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006160{
Takashi Iwai98274f02005-11-17 14:52:34 +01006161 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006162 int err;
6163
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006164 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6165 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006166 return err;
6167
6168 hdspm->pcm = pcm;
6169 pcm->private_data = hdspm;
6170 strcpy(pcm->name, hdspm->card_name);
6171
6172 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6173 &snd_hdspm_playback_ops);
6174 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6175 &snd_hdspm_capture_ops);
6176
6177 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6178
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006179 err = snd_hdspm_preallocate_memory(hdspm);
6180 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006181 return err;
6182
6183 return 0;
6184}
6185
Takashi Iwai98274f02005-11-17 14:52:34 +01006186static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006187{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006188 int i;
6189
6190 for (i = 0; i < hdspm->midiPorts; i++)
6191 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006192}
6193
Bill Pembertone23e7a12012-12-06 12:35:10 -05006194static int snd_hdspm_create_alsa_devices(struct snd_card *card,
6195 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006196{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006197 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006198
6199 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006200 err = snd_hdspm_create_pcm(card, hdspm);
6201 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006202 return err;
6203
Adrian Knoth0dca1792011-01-26 19:32:14 +01006204 i = 0;
6205 while (i < hdspm->midiPorts) {
6206 err = snd_hdspm_create_midi(card, hdspm, i);
6207 if (err < 0) {
6208 return err;
6209 }
6210 i++;
6211 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006212
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006213 err = snd_hdspm_create_controls(card, hdspm);
6214 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006215 return err;
6216
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006217 err = snd_hdspm_create_hwdep(card, hdspm);
6218 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006219 return err;
6220
6221 snd_printdd("proc init...\n");
6222 snd_hdspm_proc_init(hdspm);
6223
6224 hdspm->system_sample_rate = -1;
6225 hdspm->last_external_sample_rate = -1;
6226 hdspm->last_internal_sample_rate = -1;
6227 hdspm->playback_pid = -1;
6228 hdspm->capture_pid = -1;
6229 hdspm->capture_substream = NULL;
6230 hdspm->playback_substream = NULL;
6231
6232 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006233 err = snd_hdspm_set_defaults(hdspm);
6234 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006235 return err;
6236
6237 snd_printdd("Update mixer controls...\n");
6238 hdspm_update_simple_mixer_controls(hdspm);
6239
6240 snd_printdd("Initializeing complete ???\n");
6241
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006242 err = snd_card_register(card);
6243 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006244 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6245 return err;
6246 }
6247
6248 snd_printdd("... yes now\n");
6249
6250 return 0;
6251}
6252
Bill Pembertone23e7a12012-12-06 12:35:10 -05006253static int snd_hdspm_create(struct snd_card *card,
6254 struct hdspm *hdspm)
6255{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006256
Takashi Iwai763f3562005-06-03 11:25:34 +02006257 struct pci_dev *pci = hdspm->pci;
6258 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006259 unsigned long io_extent;
6260
6261 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006262 hdspm->card = card;
6263
6264 spin_lock_init(&hdspm->lock);
6265
Takashi Iwai763f3562005-06-03 11:25:34 +02006266 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006267 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006268
Takashi Iwai763f3562005-06-03 11:25:34 +02006269 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006270 strcpy(card->driver, "HDSPM");
6271
6272 switch (hdspm->firmware_rev) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01006273 case HDSPM_RAYDAT_REV:
6274 hdspm->io_type = RayDAT;
6275 hdspm->card_name = "RME RayDAT";
6276 hdspm->midiPorts = 2;
6277 break;
6278 case HDSPM_AIO_REV:
6279 hdspm->io_type = AIO;
6280 hdspm->card_name = "RME AIO";
6281 hdspm->midiPorts = 1;
6282 break;
6283 case HDSPM_MADIFACE_REV:
6284 hdspm->io_type = MADIface;
6285 hdspm->card_name = "RME MADIface";
6286 hdspm->midiPorts = 1;
6287 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006288 default:
Adrian Knothc09403d2011-10-27 21:57:54 +02006289 if ((hdspm->firmware_rev == 0xf0) ||
6290 ((hdspm->firmware_rev >= 0xe6) &&
6291 (hdspm->firmware_rev <= 0xea))) {
6292 hdspm->io_type = AES32;
6293 hdspm->card_name = "RME AES32";
6294 hdspm->midiPorts = 2;
Adrian Knoth05c7cc92011-11-21 16:15:36 +01006295 } else if ((hdspm->firmware_rev == 0xd2) ||
Adrian Knothc09403d2011-10-27 21:57:54 +02006296 ((hdspm->firmware_rev >= 0xc8) &&
6297 (hdspm->firmware_rev <= 0xcf))) {
6298 hdspm->io_type = MADI;
6299 hdspm->card_name = "RME MADI";
6300 hdspm->midiPorts = 3;
6301 } else {
6302 snd_printk(KERN_ERR
6303 "HDSPM: unknown firmware revision %x\n",
Adrian Knoth5027f342011-02-28 15:14:49 +01006304 hdspm->firmware_rev);
Adrian Knothc09403d2011-10-27 21:57:54 +02006305 return -ENODEV;
6306 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02006307 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006308
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006309 err = pci_enable_device(pci);
6310 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006311 return err;
6312
6313 pci_set_master(hdspm->pci);
6314
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006315 err = pci_request_regions(pci, "hdspm");
6316 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006317 return err;
6318
6319 hdspm->port = pci_resource_start(pci, 0);
6320 io_extent = pci_resource_len(pci, 0);
6321
6322 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006323 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006324
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006325 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6326 if (!hdspm->iobase) {
6327 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006328 "unable to remap region 0x%lx-0x%lx\n",
6329 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006330 return -EBUSY;
6331 }
6332 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006333 (unsigned long)hdspm->iobase, hdspm->port,
6334 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006335
6336 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006337 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006338 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6339 return -EBUSY;
6340 }
6341
6342 snd_printdd("use IRQ %d\n", pci->irq);
6343
6344 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006345
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006346 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006347 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006348 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6349 if (!hdspm->mixer) {
6350 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006351 "unable to kmalloc Mixer memory of %d Bytes\n",
6352 (int)sizeof(struct hdspm_mixer));
Julia Lawallb17cbdd2012-08-19 09:02:54 +02006353 return -ENOMEM;
Takashi Iwai763f3562005-06-03 11:25:34 +02006354 }
6355
Adrian Knoth0dca1792011-01-26 19:32:14 +01006356 hdspm->port_names_in = NULL;
6357 hdspm->port_names_out = NULL;
6358
6359 switch (hdspm->io_type) {
6360 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006361 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6362 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6363 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006364
6365 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6366 channel_map_aes32;
6367 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6368 channel_map_aes32;
6369 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6370 channel_map_aes32;
6371 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6372 texts_ports_aes32;
6373 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6374 texts_ports_aes32;
6375 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6376 texts_ports_aes32;
6377
Adrian Knothd2d10a22011-02-28 15:14:47 +01006378 hdspm->max_channels_out = hdspm->max_channels_in =
6379 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006380 hdspm->port_names_in = hdspm->port_names_out =
6381 texts_ports_aes32;
6382 hdspm->channel_map_in = hdspm->channel_map_out =
6383 channel_map_aes32;
6384
Adrian Knoth0dca1792011-01-26 19:32:14 +01006385 break;
6386
6387 case MADI:
6388 case MADIface:
6389 hdspm->ss_in_channels = hdspm->ss_out_channels =
6390 MADI_SS_CHANNELS;
6391 hdspm->ds_in_channels = hdspm->ds_out_channels =
6392 MADI_DS_CHANNELS;
6393 hdspm->qs_in_channels = hdspm->qs_out_channels =
6394 MADI_QS_CHANNELS;
6395
6396 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6397 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006398 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006399 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006400 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006401 channel_map_unity_ss;
6402
6403 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6404 texts_ports_madi;
6405 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6406 texts_ports_madi;
6407 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6408 texts_ports_madi;
6409 break;
6410
6411 case AIO:
6412 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6413 snd_printk(KERN_INFO "HDSPM: AEB input board found, but not supported\n");
6414 }
6415
6416 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6417 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6418 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6419 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6420 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6421 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6422
6423 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6424 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6425 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6426
6427 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6428 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6429 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6430
6431 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6432 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6433 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6434 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6435 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6436 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6437
6438 break;
6439
6440 case RayDAT:
6441 hdspm->ss_in_channels = hdspm->ss_out_channels =
6442 RAYDAT_SS_CHANNELS;
6443 hdspm->ds_in_channels = hdspm->ds_out_channels =
6444 RAYDAT_DS_CHANNELS;
6445 hdspm->qs_in_channels = hdspm->qs_out_channels =
6446 RAYDAT_QS_CHANNELS;
6447
6448 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6449 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6450
6451 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6452 channel_map_raydat_ss;
6453 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6454 channel_map_raydat_ds;
6455 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6456 channel_map_raydat_qs;
6457 hdspm->channel_map_in = hdspm->channel_map_out =
6458 channel_map_raydat_ss;
6459
6460 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6461 texts_ports_raydat_ss;
6462 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6463 texts_ports_raydat_ds;
6464 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6465 texts_ports_raydat_qs;
6466
6467
6468 break;
6469
6470 }
6471
6472 /* TCO detection */
6473 switch (hdspm->io_type) {
6474 case AIO:
6475 case RayDAT:
6476 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6477 HDSPM_s2_tco_detect) {
6478 hdspm->midiPorts++;
6479 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6480 GFP_KERNEL);
6481 if (NULL != hdspm->tco) {
6482 hdspm_tco_write(hdspm);
6483 }
6484 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6485 } else {
6486 hdspm->tco = NULL;
6487 }
6488 break;
6489
6490 case MADI:
6491 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6492 hdspm->midiPorts++;
6493 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6494 GFP_KERNEL);
6495 if (NULL != hdspm->tco) {
6496 hdspm_tco_write(hdspm);
6497 }
6498 snd_printk(KERN_INFO "HDSPM: MADI TCO module found\n");
6499 } else {
6500 hdspm->tco = NULL;
6501 }
6502 break;
6503
6504 default:
6505 hdspm->tco = NULL;
6506 }
6507
6508 /* texts */
6509 switch (hdspm->io_type) {
6510 case AES32:
6511 if (hdspm->tco) {
6512 hdspm->texts_autosync = texts_autosync_aes_tco;
6513 hdspm->texts_autosync_items = 10;
6514 } else {
6515 hdspm->texts_autosync = texts_autosync_aes;
6516 hdspm->texts_autosync_items = 9;
6517 }
6518 break;
6519
6520 case MADI:
6521 if (hdspm->tco) {
6522 hdspm->texts_autosync = texts_autosync_madi_tco;
6523 hdspm->texts_autosync_items = 4;
6524 } else {
6525 hdspm->texts_autosync = texts_autosync_madi;
6526 hdspm->texts_autosync_items = 3;
6527 }
6528 break;
6529
6530 case MADIface:
6531
6532 break;
6533
6534 case RayDAT:
6535 if (hdspm->tco) {
6536 hdspm->texts_autosync = texts_autosync_raydat_tco;
6537 hdspm->texts_autosync_items = 9;
6538 } else {
6539 hdspm->texts_autosync = texts_autosync_raydat;
6540 hdspm->texts_autosync_items = 8;
6541 }
6542 break;
6543
6544 case AIO:
6545 if (hdspm->tco) {
6546 hdspm->texts_autosync = texts_autosync_aio_tco;
6547 hdspm->texts_autosync_items = 6;
6548 } else {
6549 hdspm->texts_autosync = texts_autosync_aio;
6550 hdspm->texts_autosync_items = 5;
6551 }
6552 break;
6553
6554 }
6555
6556 tasklet_init(&hdspm->midi_tasklet,
6557 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006558
Adrian Knothf7de8ba2012-01-10 20:58:40 +01006559
6560 if (hdspm->io_type != MADIface) {
6561 hdspm->serial = (hdspm_read(hdspm,
6562 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
6563 /* id contains either a user-provided value or the default
6564 * NULL. If it's the default, we're safe to
6565 * fill card->id with the serial number.
6566 *
6567 * If the serial number is 0xFFFFFF, then we're dealing with
6568 * an old PCI revision that comes without a sane number. In
6569 * this case, we don't set card->id to avoid collisions
6570 * when running with multiple cards.
6571 */
6572 if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
6573 sprintf(card->id, "HDSPMx%06x", hdspm->serial);
6574 snd_card_set_id(card, card->id);
6575 }
6576 }
6577
Takashi Iwai763f3562005-06-03 11:25:34 +02006578 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006579 err = snd_hdspm_create_alsa_devices(card, hdspm);
6580 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006581 return err;
6582
6583 snd_hdspm_initialize_midi_flush(hdspm);
6584
6585 return 0;
6586}
6587
Adrian Knoth0dca1792011-01-26 19:32:14 +01006588
Takashi Iwai98274f02005-11-17 14:52:34 +01006589static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006590{
6591
6592 if (hdspm->port) {
6593
6594 /* stop th audio, and cancel all interrupts */
6595 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006596 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006597 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6598 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006599 hdspm_write(hdspm, HDSPM_controlRegister,
6600 hdspm->control_register);
6601 }
6602
6603 if (hdspm->irq >= 0)
6604 free_irq(hdspm->irq, (void *) hdspm);
6605
Jesper Juhlfc584222005-10-24 15:11:28 +02006606 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006607
6608 if (hdspm->iobase)
6609 iounmap(hdspm->iobase);
6610
Takashi Iwai763f3562005-06-03 11:25:34 +02006611 if (hdspm->port)
6612 pci_release_regions(hdspm->pci);
6613
6614 pci_disable_device(hdspm->pci);
6615 return 0;
6616}
6617
Adrian Knoth0dca1792011-01-26 19:32:14 +01006618
Takashi Iwai98274f02005-11-17 14:52:34 +01006619static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006620{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006621 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006622
6623 if (hdspm)
6624 snd_hdspm_free(hdspm);
6625}
6626
Adrian Knoth0dca1792011-01-26 19:32:14 +01006627
Bill Pembertone23e7a12012-12-06 12:35:10 -05006628static int snd_hdspm_probe(struct pci_dev *pci,
6629 const struct pci_device_id *pci_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02006630{
6631 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006632 struct hdspm *hdspm;
6633 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006634 int err;
6635
6636 if (dev >= SNDRV_CARDS)
6637 return -ENODEV;
6638 if (!enable[dev]) {
6639 dev++;
6640 return -ENOENT;
6641 }
6642
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006643 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006644 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006645 if (err < 0)
6646 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006647
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006648 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006649 card->private_free = snd_hdspm_card_free;
6650 hdspm->dev = dev;
6651 hdspm->pci = pci;
6652
Takashi Iwaic187c042007-02-19 15:27:33 +01006653 snd_card_set_dev(card, &pci->dev);
6654
Adrian Knoth0dca1792011-01-26 19:32:14 +01006655 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006656 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006657 snd_card_free(card);
6658 return err;
6659 }
6660
Adrian Knoth0dca1792011-01-26 19:32:14 +01006661 if (hdspm->io_type != MADIface) {
6662 sprintf(card->shortname, "%s_%x",
6663 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006664 hdspm->serial);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006665 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6666 hdspm->card_name,
Adrian Knoth7d53a632012-01-04 14:31:16 +01006667 hdspm->serial,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006668 hdspm->port, hdspm->irq);
6669 } else {
6670 sprintf(card->shortname, "%s", hdspm->card_name);
6671 sprintf(card->longname, "%s at 0x%lx, irq %d",
6672 hdspm->card_name, hdspm->port, hdspm->irq);
6673 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006674
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006675 err = snd_card_register(card);
6676 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006677 snd_card_free(card);
6678 return err;
6679 }
6680
6681 pci_set_drvdata(pci, card);
6682
6683 dev++;
6684 return 0;
6685}
6686
Bill Pembertone23e7a12012-12-06 12:35:10 -05006687static void snd_hdspm_remove(struct pci_dev *pci)
Takashi Iwai763f3562005-06-03 11:25:34 +02006688{
6689 snd_card_free(pci_get_drvdata(pci));
6690 pci_set_drvdata(pci, NULL);
6691}
6692
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006693static struct pci_driver hdspm_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006694 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006695 .id_table = snd_hdspm_ids,
6696 .probe = snd_hdspm_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05006697 .remove = snd_hdspm_remove,
Takashi Iwai763f3562005-06-03 11:25:34 +02006698};
6699
Takashi Iwaie9f66d92012-04-24 12:25:00 +02006700module_pci_driver(hdspm_driver);