blob: 88ae27431cb21030b13934372e16a46b1ac4898b [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>
44#include <linux/moduleparam.h>
45#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 */
64static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
65
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
444#define HDSPM_AES32_wcFreq_bit 22
Adrian Knoth0dca1792011-01-26 19:32:14 +0100445/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
Remy Bruno3cee5a62006-10-16 12:46:32 +0200446 HDSPM_bit2freq */
447#define HDSPM_AES32_syncref_bit 16
448/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */
449
450#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
451#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
452#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
453#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
454#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
455#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
456#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
457#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
458#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
Remy Bruno65345992007-08-31 12:21:08 +0200459#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
Remy Bruno3cee5a62006-10-16 12:46:32 +0200460
461/* status2 */
462/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
463#define HDSPM_LockAES 0x80
464#define HDSPM_LockAES1 0x80
465#define HDSPM_LockAES2 0x40
466#define HDSPM_LockAES3 0x20
467#define HDSPM_LockAES4 0x10
468#define HDSPM_LockAES5 0x8
469#define HDSPM_LockAES6 0x4
470#define HDSPM_LockAES7 0x2
471#define HDSPM_LockAES8 0x1
472/*
473 Timecode
474 After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
475 AES i+1
476 bits 3210
477 0001 32kHz
478 0010 44.1kHz
479 0011 48kHz
480 0100 64kHz
481 0101 88.2kHz
482 0110 96kHz
483 0111 128kHz
484 1000 176.4kHz
485 1001 192kHz
486 NB: Timecode register doesn't seem to work on AES32 card revision 230
487*/
488
Takashi Iwai763f3562005-06-03 11:25:34 +0200489/* Mixer Values */
490#define UNITY_GAIN 32768 /* = 65536/2 */
491#define MINUS_INFINITY_GAIN 0
492
Takashi Iwai763f3562005-06-03 11:25:34 +0200493/* Number of channels for different Speed Modes */
494#define MADI_SS_CHANNELS 64
495#define MADI_DS_CHANNELS 32
496#define MADI_QS_CHANNELS 16
497
Adrian Knoth0dca1792011-01-26 19:32:14 +0100498#define RAYDAT_SS_CHANNELS 36
499#define RAYDAT_DS_CHANNELS 20
500#define RAYDAT_QS_CHANNELS 12
501
502#define AIO_IN_SS_CHANNELS 14
503#define AIO_IN_DS_CHANNELS 10
504#define AIO_IN_QS_CHANNELS 8
505#define AIO_OUT_SS_CHANNELS 16
506#define AIO_OUT_DS_CHANNELS 12
507#define AIO_OUT_QS_CHANNELS 10
508
Adrian Knothd2d10a22011-02-28 15:14:47 +0100509#define AES32_CHANNELS 16
510
Takashi Iwai763f3562005-06-03 11:25:34 +0200511/* the size of a substream (1 mono data stream) */
512#define HDSPM_CHANNEL_BUFFER_SAMPLES (16*1024)
513#define HDSPM_CHANNEL_BUFFER_BYTES (4*HDSPM_CHANNEL_BUFFER_SAMPLES)
514
515/* the size of the area we need to allocate for DMA transfers. the
516 size is the same regardless of the number of channels, and
Adrian Knoth0dca1792011-01-26 19:32:14 +0100517 also the latency to use.
Takashi Iwai763f3562005-06-03 11:25:34 +0200518 for one direction !!!
519*/
Remy Brunoffb2c3c2007-03-07 19:08:46 +0100520#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
Takashi Iwai763f3562005-06-03 11:25:34 +0200521#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)
522
Remy Bruno3cee5a62006-10-16 12:46:32 +0200523/* revisions >= 230 indicate AES32 card */
Adrian Knothefef0542011-06-12 17:26:19 +0200524#define HDSPM_MADI_OLD_REV 207
Adrian Knoth0dca1792011-01-26 19:32:14 +0100525#define HDSPM_MADI_REV 210
526#define HDSPM_RAYDAT_REV 211
527#define HDSPM_AIO_REV 212
528#define HDSPM_MADIFACE_REV 213
529#define HDSPM_AES_REV 240
Adrian Knoth526ea862011-02-28 15:14:48 +0100530#define HDSPM_AES32_REV 234
Adrian Knothbdd32552011-03-07 19:10:11 +0100531#define HDSPM_AES32_OLD_REV 233
Remy Bruno3cee5a62006-10-16 12:46:32 +0200532
Remy Bruno65345992007-08-31 12:21:08 +0200533/* speed factor modes */
534#define HDSPM_SPEED_SINGLE 0
535#define HDSPM_SPEED_DOUBLE 1
536#define HDSPM_SPEED_QUAD 2
Adrian Knoth0dca1792011-01-26 19:32:14 +0100537
Remy Bruno65345992007-08-31 12:21:08 +0200538/* names for speed modes */
539static char *hdspm_speed_names[] = { "single", "double", "quad" };
540
Adrian Knoth0dca1792011-01-26 19:32:14 +0100541static char *texts_autosync_aes_tco[] = { "Word Clock",
542 "AES1", "AES2", "AES3", "AES4",
543 "AES5", "AES6", "AES7", "AES8",
544 "TCO" };
545static char *texts_autosync_aes[] = { "Word Clock",
546 "AES1", "AES2", "AES3", "AES4",
547 "AES5", "AES6", "AES7", "AES8" };
548static char *texts_autosync_madi_tco[] = { "Word Clock",
549 "MADI", "TCO", "Sync In" };
550static char *texts_autosync_madi[] = { "Word Clock",
551 "MADI", "Sync In" };
552
553static char *texts_autosync_raydat_tco[] = {
554 "Word Clock",
555 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
556 "AES", "SPDIF", "TCO", "Sync In"
557};
558static char *texts_autosync_raydat[] = {
559 "Word Clock",
560 "ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
561 "AES", "SPDIF", "Sync In"
562};
563static char *texts_autosync_aio_tco[] = {
564 "Word Clock",
565 "ADAT", "AES", "SPDIF", "TCO", "Sync In"
566};
567static char *texts_autosync_aio[] = { "Word Clock",
568 "ADAT", "AES", "SPDIF", "Sync In" };
569
570static char *texts_freq[] = {
571 "No Lock",
572 "32 kHz",
573 "44.1 kHz",
574 "48 kHz",
575 "64 kHz",
576 "88.2 kHz",
577 "96 kHz",
578 "128 kHz",
579 "176.4 kHz",
580 "192 kHz"
581};
582
Adrian Knoth0dca1792011-01-26 19:32:14 +0100583static char *texts_ports_madi[] = {
584 "MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
585 "MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
586 "MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
587 "MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
588 "MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
589 "MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
590 "MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
591 "MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
592 "MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
593 "MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
594 "MADI.61", "MADI.62", "MADI.63", "MADI.64",
595};
596
597
598static char *texts_ports_raydat_ss[] = {
599 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
600 "ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
601 "ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
602 "ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
603 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
604 "ADAT4.7", "ADAT4.8",
605 "AES.L", "AES.R",
606 "SPDIF.L", "SPDIF.R"
607};
608
609static char *texts_ports_raydat_ds[] = {
610 "ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
611 "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
612 "ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
613 "ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
614 "AES.L", "AES.R",
615 "SPDIF.L", "SPDIF.R"
616};
617
618static char *texts_ports_raydat_qs[] = {
619 "ADAT1.1", "ADAT1.2",
620 "ADAT2.1", "ADAT2.2",
621 "ADAT3.1", "ADAT3.2",
622 "ADAT4.1", "ADAT4.2",
623 "AES.L", "AES.R",
624 "SPDIF.L", "SPDIF.R"
625};
626
627
628static char *texts_ports_aio_in_ss[] = {
629 "Analogue.L", "Analogue.R",
630 "AES.L", "AES.R",
631 "SPDIF.L", "SPDIF.R",
632 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
633 "ADAT.7", "ADAT.8"
634};
635
636static char *texts_ports_aio_out_ss[] = {
637 "Analogue.L", "Analogue.R",
638 "AES.L", "AES.R",
639 "SPDIF.L", "SPDIF.R",
640 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
641 "ADAT.7", "ADAT.8",
642 "Phone.L", "Phone.R"
643};
644
645static char *texts_ports_aio_in_ds[] = {
646 "Analogue.L", "Analogue.R",
647 "AES.L", "AES.R",
648 "SPDIF.L", "SPDIF.R",
649 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
650};
651
652static char *texts_ports_aio_out_ds[] = {
653 "Analogue.L", "Analogue.R",
654 "AES.L", "AES.R",
655 "SPDIF.L", "SPDIF.R",
656 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
657 "Phone.L", "Phone.R"
658};
659
660static char *texts_ports_aio_in_qs[] = {
661 "Analogue.L", "Analogue.R",
662 "AES.L", "AES.R",
663 "SPDIF.L", "SPDIF.R",
664 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4"
665};
666
667static char *texts_ports_aio_out_qs[] = {
668 "Analogue.L", "Analogue.R",
669 "AES.L", "AES.R",
670 "SPDIF.L", "SPDIF.R",
671 "ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
672 "Phone.L", "Phone.R"
673};
674
Adrian Knoth432d2502011-02-23 11:43:08 +0100675static char *texts_ports_aes32[] = {
676 "AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
677 "AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
678 "AES.15", "AES.16"
679};
680
Adrian Knoth55a57602011-01-27 11:23:15 +0100681/* These tables map the ALSA channels 1..N to the channels that we
682 need to use in order to find the relevant channel buffer. RME
683 refers to this kind of mapping as between "the ADAT channel and
684 the DMA channel." We index it using the logical audio channel,
685 and the value is the DMA channel (i.e. channel buffer number)
686 where the data for that channel can be read/written from/to.
687*/
688
689static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
690 0, 1, 2, 3, 4, 5, 6, 7,
691 8, 9, 10, 11, 12, 13, 14, 15,
692 16, 17, 18, 19, 20, 21, 22, 23,
693 24, 25, 26, 27, 28, 29, 30, 31,
694 32, 33, 34, 35, 36, 37, 38, 39,
695 40, 41, 42, 43, 44, 45, 46, 47,
696 48, 49, 50, 51, 52, 53, 54, 55,
697 56, 57, 58, 59, 60, 61, 62, 63
698};
699
Adrian Knoth55a57602011-01-27 11:23:15 +0100700static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
701 4, 5, 6, 7, 8, 9, 10, 11, /* ADAT 1 */
702 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT 2 */
703 20, 21, 22, 23, 24, 25, 26, 27, /* ADAT 3 */
704 28, 29, 30, 31, 32, 33, 34, 35, /* ADAT 4 */
705 0, 1, /* AES */
706 2, 3, /* SPDIF */
707 -1, -1, -1, -1,
708 -1, -1, -1, -1, -1, -1, -1, -1,
709 -1, -1, -1, -1, -1, -1, -1, -1,
710 -1, -1, -1, -1, -1, -1, -1, -1,
711};
712
713static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
714 4, 5, 6, 7, /* ADAT 1 */
715 8, 9, 10, 11, /* ADAT 2 */
716 12, 13, 14, 15, /* ADAT 3 */
717 16, 17, 18, 19, /* ADAT 4 */
718 0, 1, /* AES */
719 2, 3, /* SPDIF */
720 -1, -1, -1, -1,
721 -1, -1, -1, -1, -1, -1, -1, -1,
722 -1, -1, -1, -1, -1, -1, -1, -1,
723 -1, -1, -1, -1, -1, -1, -1, -1,
724 -1, -1, -1, -1, -1, -1, -1, -1,
725 -1, -1, -1, -1, -1, -1, -1, -1,
726};
727
728static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
729 4, 5, /* ADAT 1 */
730 6, 7, /* ADAT 2 */
731 8, 9, /* ADAT 3 */
732 10, 11, /* ADAT 4 */
733 0, 1, /* AES */
734 2, 3, /* SPDIF */
735 -1, -1, -1, -1,
736 -1, -1, -1, -1, -1, -1, -1, -1,
737 -1, -1, -1, -1, -1, -1, -1, -1,
738 -1, -1, -1, -1, -1, -1, -1, -1,
739 -1, -1, -1, -1, -1, -1, -1, -1,
740 -1, -1, -1, -1, -1, -1, -1, -1,
741 -1, -1, -1, -1, -1, -1, -1, -1,
742};
743
744static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
745 0, 1, /* line in */
746 8, 9, /* aes in, */
747 10, 11, /* spdif in */
748 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT in */
749 -1, -1,
750 -1, -1, -1, -1, -1, -1, -1, -1,
751 -1, -1, -1, -1, -1, -1, -1, -1,
752 -1, -1, -1, -1, -1, -1, -1, -1,
753 -1, -1, -1, -1, -1, -1, -1, -1,
754 -1, -1, -1, -1, -1, -1, -1, -1,
755 -1, -1, -1, -1, -1, -1, -1, -1,
756};
757
758static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
759 0, 1, /* line out */
760 8, 9, /* aes out */
761 10, 11, /* spdif out */
762 12, 13, 14, 15, 16, 17, 18, 19, /* ADAT out */
763 6, 7, /* phone out */
764 -1, -1, -1, -1, -1, -1, -1, -1,
765 -1, -1, -1, -1, -1, -1, -1, -1,
766 -1, -1, -1, -1, -1, -1, -1, -1,
767 -1, -1, -1, -1, -1, -1, -1, -1,
768 -1, -1, -1, -1, -1, -1, -1, -1,
769 -1, -1, -1, -1, -1, -1, -1, -1,
770};
771
772static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
773 0, 1, /* line in */
774 8, 9, /* aes in */
775 10, 11, /* spdif in */
776 12, 14, 16, 18, /* adat in */
777 -1, -1, -1, -1, -1, -1,
778 -1, -1, -1, -1, -1, -1, -1, -1,
779 -1, -1, -1, -1, -1, -1, -1, -1,
780 -1, -1, -1, -1, -1, -1, -1, -1,
781 -1, -1, -1, -1, -1, -1, -1, -1,
782 -1, -1, -1, -1, -1, -1, -1, -1,
783 -1, -1, -1, -1, -1, -1, -1, -1
784};
785
786static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
787 0, 1, /* line out */
788 8, 9, /* aes out */
789 10, 11, /* spdif out */
790 12, 14, 16, 18, /* adat out */
791 6, 7, /* phone out */
792 -1, -1, -1, -1,
793 -1, -1, -1, -1, -1, -1, -1, -1,
794 -1, -1, -1, -1, -1, -1, -1, -1,
795 -1, -1, -1, -1, -1, -1, -1, -1,
796 -1, -1, -1, -1, -1, -1, -1, -1,
797 -1, -1, -1, -1, -1, -1, -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1
799};
800
801static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
802 0, 1, /* line in */
803 8, 9, /* aes in */
804 10, 11, /* spdif in */
805 12, 16, /* adat in */
806 -1, -1, -1, -1, -1, -1, -1, -1,
807 -1, -1, -1, -1, -1, -1, -1, -1,
808 -1, -1, -1, -1, -1, -1, -1, -1,
809 -1, -1, -1, -1, -1, -1, -1, -1,
810 -1, -1, -1, -1, -1, -1, -1, -1,
811 -1, -1, -1, -1, -1, -1, -1, -1,
812 -1, -1, -1, -1, -1, -1, -1, -1
813};
814
815static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
816 0, 1, /* line out */
817 8, 9, /* aes out */
818 10, 11, /* spdif out */
819 12, 16, /* adat out */
820 6, 7, /* phone out */
821 -1, -1, -1, -1, -1, -1,
822 -1, -1, -1, -1, -1, -1, -1, -1,
823 -1, -1, -1, -1, -1, -1, -1, -1,
824 -1, -1, -1, -1, -1, -1, -1, -1,
825 -1, -1, -1, -1, -1, -1, -1, -1,
826 -1, -1, -1, -1, -1, -1, -1, -1,
827 -1, -1, -1, -1, -1, -1, -1, -1
828};
829
Adrian Knoth432d2502011-02-23 11:43:08 +0100830static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
831 0, 1, 2, 3, 4, 5, 6, 7,
832 8, 9, 10, 11, 12, 13, 14, 15,
833 -1, -1, -1, -1, -1, -1, -1, -1,
834 -1, -1, -1, -1, -1, -1, -1, -1,
835 -1, -1, -1, -1, -1, -1, -1, -1,
836 -1, -1, -1, -1, -1, -1, -1, -1,
837 -1, -1, -1, -1, -1, -1, -1, -1,
838 -1, -1, -1, -1, -1, -1, -1, -1
839};
840
Takashi Iwai98274f02005-11-17 14:52:34 +0100841struct hdspm_midi {
842 struct hdspm *hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +0200843 int id;
Takashi Iwai98274f02005-11-17 14:52:34 +0100844 struct snd_rawmidi *rmidi;
845 struct snd_rawmidi_substream *input;
846 struct snd_rawmidi_substream *output;
Takashi Iwai763f3562005-06-03 11:25:34 +0200847 char istimer; /* timer in use */
848 struct timer_list timer;
849 spinlock_t lock;
850 int pending;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100851 int dataIn;
852 int statusIn;
853 int dataOut;
854 int statusOut;
855 int ie;
856 int irq;
857};
858
859struct hdspm_tco {
860 int input;
861 int framerate;
862 int wordclock;
863 int samplerate;
864 int pull;
865 int term; /* 0 = off, 1 = on */
Takashi Iwai763f3562005-06-03 11:25:34 +0200866};
867
Takashi Iwai98274f02005-11-17 14:52:34 +0100868struct hdspm {
Takashi Iwai763f3562005-06-03 11:25:34 +0200869 spinlock_t lock;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200870 /* only one playback and/or capture stream */
871 struct snd_pcm_substream *capture_substream;
872 struct snd_pcm_substream *playback_substream;
Takashi Iwai763f3562005-06-03 11:25:34 +0200873
874 char *card_name; /* for procinfo */
Remy Bruno3cee5a62006-10-16 12:46:32 +0200875 unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/
876
Adrian Knoth0dca1792011-01-26 19:32:14 +0100877 uint8_t io_type;
Takashi Iwai763f3562005-06-03 11:25:34 +0200878
Takashi Iwai763f3562005-06-03 11:25:34 +0200879 int monitor_outs; /* set up monitoring outs init flag */
880
881 u32 control_register; /* cached value */
882 u32 control2_register; /* cached value */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100883 u32 settings_register;
Takashi Iwai763f3562005-06-03 11:25:34 +0200884
Adrian Knoth0dca1792011-01-26 19:32:14 +0100885 struct hdspm_midi midi[4];
Takashi Iwai763f3562005-06-03 11:25:34 +0200886 struct tasklet_struct midi_tasklet;
887
888 size_t period_bytes;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100889 unsigned char ss_in_channels;
890 unsigned char ds_in_channels;
891 unsigned char qs_in_channels;
892 unsigned char ss_out_channels;
893 unsigned char ds_out_channels;
894 unsigned char qs_out_channels;
895
896 unsigned char max_channels_in;
897 unsigned char max_channels_out;
898
Takashi Iwai286bed02011-06-30 12:45:36 +0200899 signed char *channel_map_in;
900 signed char *channel_map_out;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100901
Takashi Iwai286bed02011-06-30 12:45:36 +0200902 signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
903 signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +0100904
905 char **port_names_in;
906 char **port_names_out;
907
908 char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
909 char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
Takashi Iwai763f3562005-06-03 11:25:34 +0200910
911 unsigned char *playback_buffer; /* suitably aligned address */
912 unsigned char *capture_buffer; /* suitably aligned address */
913
914 pid_t capture_pid; /* process id which uses capture */
915 pid_t playback_pid; /* process id which uses capture */
916 int running; /* running status */
917
918 int last_external_sample_rate; /* samplerate mystic ... */
919 int last_internal_sample_rate;
920 int system_sample_rate;
921
Takashi Iwai763f3562005-06-03 11:25:34 +0200922 int dev; /* Hardware vars... */
923 int irq;
924 unsigned long port;
925 void __iomem *iobase;
926
927 int irq_count; /* for debug */
Adrian Knoth0dca1792011-01-26 19:32:14 +0100928 int midiPorts;
Takashi Iwai763f3562005-06-03 11:25:34 +0200929
Takashi Iwai98274f02005-11-17 14:52:34 +0100930 struct snd_card *card; /* one card */
931 struct snd_pcm *pcm; /* has one pcm */
932 struct snd_hwdep *hwdep; /* and a hwdep for additional ioctl */
Takashi Iwai763f3562005-06-03 11:25:34 +0200933 struct pci_dev *pci; /* and an pci info */
934
935 /* Mixer vars */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200936 /* fast alsa mixer */
937 struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
938 /* but input to much, so not used */
939 struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300940 /* full mixer accessible over mixer ioctl or hwdep-device */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +0200941 struct hdspm_mixer *mixer;
Takashi Iwai763f3562005-06-03 11:25:34 +0200942
Adrian Knoth0dca1792011-01-26 19:32:14 +0100943 struct hdspm_tco *tco; /* NULL if no TCO detected */
Takashi Iwai763f3562005-06-03 11:25:34 +0200944
Adrian Knoth0dca1792011-01-26 19:32:14 +0100945 char **texts_autosync;
946 int texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +0200947
Adrian Knoth0dca1792011-01-26 19:32:14 +0100948 cycles_t last_interrupt;
Jaroslav Kysela730a5862011-01-27 13:03:15 +0100949
950 struct hdspm_peak_rms peak_rms;
Takashi Iwai763f3562005-06-03 11:25:34 +0200951};
952
Takashi Iwai763f3562005-06-03 11:25:34 +0200953
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200954static DEFINE_PCI_DEVICE_TABLE(snd_hdspm_ids) = {
Takashi Iwai763f3562005-06-03 11:25:34 +0200955 {
956 .vendor = PCI_VENDOR_ID_XILINX,
957 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
958 .subvendor = PCI_ANY_ID,
959 .subdevice = PCI_ANY_ID,
960 .class = 0,
961 .class_mask = 0,
962 .driver_data = 0},
963 {0,}
964};
965
966MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);
967
968/* prototypes */
Takashi Iwai98274f02005-11-17 14:52:34 +0100969static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
970 struct hdspm * hdspm);
971static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
972 struct hdspm * hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +0200973
Adrian Knoth0dca1792011-01-26 19:32:14 +0100974static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
975static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
976static int hdspm_autosync_ref(struct hdspm *hdspm);
977static int snd_hdspm_set_defaults(struct hdspm *hdspm);
978static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +0200979 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +0200980 unsigned int reg, int channels);
981
Remy Bruno3cee5a62006-10-16 12:46:32 +0200982static inline int HDSPM_bit2freq(int n)
983{
Denys Vlasenko62cef822008-04-14 13:04:18 +0200984 static const int bit2freq_tab[] = {
985 0, 32000, 44100, 48000, 64000, 88200,
Remy Bruno3cee5a62006-10-16 12:46:32 +0200986 96000, 128000, 176400, 192000 };
987 if (n < 1 || n > 9)
988 return 0;
989 return bit2freq_tab[n];
990}
991
Adrian Knoth0dca1792011-01-26 19:32:14 +0100992/* Write/read to/from HDSPM with Adresses in Bytes
Takashi Iwai763f3562005-06-03 11:25:34 +0200993 not words but only 32Bit writes are allowed */
994
Takashi Iwai98274f02005-11-17 14:52:34 +0100995static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
Takashi Iwai763f3562005-06-03 11:25:34 +0200996 unsigned int val)
997{
998 writel(val, hdspm->iobase + reg);
999}
1000
Takashi Iwai98274f02005-11-17 14:52:34 +01001001static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
Takashi Iwai763f3562005-06-03 11:25:34 +02001002{
1003 return readl(hdspm->iobase + reg);
1004}
1005
Adrian Knoth0dca1792011-01-26 19:32:14 +01001006/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
1007 mixer is write only on hardware so we have to cache him for read
Takashi Iwai763f3562005-06-03 11:25:34 +02001008 each fader is a u32, but uses only the first 16 bit */
1009
Takashi Iwai98274f02005-11-17 14:52:34 +01001010static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001011 unsigned int in)
1012{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001013 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001014 return 0;
1015
1016 return hdspm->mixer->ch[chan].in[in];
1017}
1018
Takashi Iwai98274f02005-11-17 14:52:34 +01001019static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001020 unsigned int pb)
1021{
Adrian Bunk5bab24822006-03-13 14:15:04 +01001022 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
Takashi Iwai763f3562005-06-03 11:25:34 +02001023 return 0;
1024 return hdspm->mixer->ch[chan].pb[pb];
1025}
1026
Denys Vlasenko62cef822008-04-14 13:04:18 +02001027static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001028 unsigned int in, unsigned short data)
1029{
1030 if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
1031 return -1;
1032
1033 hdspm_write(hdspm,
1034 HDSPM_MADI_mixerBase +
1035 ((in + 128 * chan) * sizeof(u32)),
1036 (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
1037 return 0;
1038}
1039
Denys Vlasenko62cef822008-04-14 13:04:18 +02001040static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
Takashi Iwai763f3562005-06-03 11:25:34 +02001041 unsigned int pb, unsigned short data)
1042{
1043 if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
1044 return -1;
1045
1046 hdspm_write(hdspm,
1047 HDSPM_MADI_mixerBase +
1048 ((64 + pb + 128 * chan) * sizeof(u32)),
1049 (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
1050 return 0;
1051}
1052
1053
1054/* enable DMA for specific channels, now available for DSP-MADI */
Takashi Iwai98274f02005-11-17 14:52:34 +01001055static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001056{
1057 hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
1058}
1059
Takashi Iwai98274f02005-11-17 14:52:34 +01001060static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
Takashi Iwai763f3562005-06-03 11:25:34 +02001061{
1062 hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
1063}
1064
1065/* check if same process is writing and reading */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001066static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001067{
1068 unsigned long flags;
1069 int ret = 1;
1070
1071 spin_lock_irqsave(&hdspm->lock, flags);
1072 if ((hdspm->playback_pid != hdspm->capture_pid) &&
1073 (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
1074 ret = 0;
1075 }
1076 spin_unlock_irqrestore(&hdspm->lock, flags);
1077 return ret;
1078}
1079
1080/* check for external sample rate */
Denys Vlasenko62cef822008-04-14 13:04:18 +02001081static int hdspm_external_sample_rate(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001082{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001083 unsigned int status, status2, timecode;
1084 int syncref, rate = 0, rate_bits;
Takashi Iwai763f3562005-06-03 11:25:34 +02001085
Adrian Knoth0dca1792011-01-26 19:32:14 +01001086 switch (hdspm->io_type) {
1087 case AES32:
1088 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1089 status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01001090 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001091
1092 syncref = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001093
Remy Bruno3cee5a62006-10-16 12:46:32 +02001094 if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
1095 status & HDSPM_AES32_wcLock)
Adrian Knoth0dca1792011-01-26 19:32:14 +01001096 return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF);
1097
Remy Bruno3cee5a62006-10-16 12:46:32 +02001098 if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001099 syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
1100 status2 & (HDSPM_LockAES >>
1101 (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
1102 return HDSPM_bit2freq((timecode >> (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
Remy Bruno3cee5a62006-10-16 12:46:32 +02001103 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001104 break;
1105
1106 case MADIface:
1107 status = hdspm_read(hdspm, HDSPM_statusRegister);
1108
1109 if (!(status & HDSPM_madiLock)) {
1110 rate = 0; /* no lock */
1111 } else {
1112 switch (status & (HDSPM_status1_freqMask)) {
1113 case HDSPM_status1_F_0*1:
1114 rate = 32000; break;
1115 case HDSPM_status1_F_0*2:
1116 rate = 44100; break;
1117 case HDSPM_status1_F_0*3:
1118 rate = 48000; break;
1119 case HDSPM_status1_F_0*4:
1120 rate = 64000; break;
1121 case HDSPM_status1_F_0*5:
1122 rate = 88200; break;
1123 case HDSPM_status1_F_0*6:
1124 rate = 96000; break;
1125 case HDSPM_status1_F_0*7:
1126 rate = 128000; break;
1127 case HDSPM_status1_F_0*8:
1128 rate = 176400; break;
1129 case HDSPM_status1_F_0*9:
1130 rate = 192000; break;
1131 default:
1132 rate = 0; break;
1133 }
1134 }
1135
1136 break;
1137
1138 case MADI:
1139 case AIO:
1140 case RayDAT:
1141 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
1142 status = hdspm_read(hdspm, HDSPM_statusRegister);
1143 rate = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02001144
Remy Bruno3cee5a62006-10-16 12:46:32 +02001145 /* if wordclock has synced freq and wordclock is valid */
1146 if ((status2 & HDSPM_wcLock) != 0 &&
Adrian Knothfedf1532011-06-12 17:26:18 +02001147 (status2 & HDSPM_SelSyncRef0) == 0) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02001148
1149 rate_bits = status2 & HDSPM_wcFreqMask;
1150
Adrian Knoth0dca1792011-01-26 19:32:14 +01001151
Remy Bruno3cee5a62006-10-16 12:46:32 +02001152 switch (rate_bits) {
1153 case HDSPM_wcFreq32:
1154 rate = 32000;
1155 break;
1156 case HDSPM_wcFreq44_1:
1157 rate = 44100;
1158 break;
1159 case HDSPM_wcFreq48:
1160 rate = 48000;
1161 break;
1162 case HDSPM_wcFreq64:
1163 rate = 64000;
1164 break;
1165 case HDSPM_wcFreq88_2:
1166 rate = 88200;
1167 break;
1168 case HDSPM_wcFreq96:
1169 rate = 96000;
1170 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001171 default:
1172 rate = 0;
1173 break;
1174 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001175 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001176
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001177 /* if rate detected and Syncref is Word than have it,
1178 * word has priority to MADI
1179 */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001180 if (rate != 0 &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01001181 (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
Remy Bruno3cee5a62006-10-16 12:46:32 +02001182 return rate;
1183
Adrian Knoth0dca1792011-01-26 19:32:14 +01001184 /* maybe a madi input (which is taken if sel sync is madi) */
Remy Bruno3cee5a62006-10-16 12:46:32 +02001185 if (status & HDSPM_madiLock) {
1186 rate_bits = status & HDSPM_madiFreqMask;
1187
1188 switch (rate_bits) {
1189 case HDSPM_madiFreq32:
1190 rate = 32000;
1191 break;
1192 case HDSPM_madiFreq44_1:
1193 rate = 44100;
1194 break;
1195 case HDSPM_madiFreq48:
1196 rate = 48000;
1197 break;
1198 case HDSPM_madiFreq64:
1199 rate = 64000;
1200 break;
1201 case HDSPM_madiFreq88_2:
1202 rate = 88200;
1203 break;
1204 case HDSPM_madiFreq96:
1205 rate = 96000;
1206 break;
1207 case HDSPM_madiFreq128:
1208 rate = 128000;
1209 break;
1210 case HDSPM_madiFreq176_4:
1211 rate = 176400;
1212 break;
1213 case HDSPM_madiFreq192:
1214 rate = 192000;
1215 break;
1216 default:
1217 rate = 0;
1218 break;
1219 }
Adrian Knothd12c51d2011-07-29 03:11:03 +02001220
1221 /* QS and DS rates normally can not be detected
1222 * automatically by the card. Only exception is MADI
1223 * in 96k frame mode.
1224 *
1225 * So if we read SS values (32 .. 48k), check for
1226 * user-provided DS/QS bits in the control register
1227 * and multiply the base frequency accordingly.
1228 */
1229 if (rate <= 48000) {
1230 if (hdspm->control_register & HDSPM_QuadSpeed)
1231 rate *= 4;
1232 else if (hdspm->control_register &
1233 HDSPM_DoubleSpeed)
1234 rate *= 2;
1235 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02001236 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001237 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001238 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01001239
1240 return rate;
Takashi Iwai763f3562005-06-03 11:25:34 +02001241}
1242
1243/* Latency function */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001244static inline void hdspm_compute_period_size(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001245{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001246 hdspm->period_bytes = 1 << ((hdspm_decode_latency(hdspm->control_register) + 8));
Takashi Iwai763f3562005-06-03 11:25:34 +02001247}
1248
Adrian Knoth0dca1792011-01-26 19:32:14 +01001249
1250static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001251{
1252 int position;
1253
1254 position = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth483cee72011-02-23 11:43:09 +01001255
1256 switch (hdspm->io_type) {
1257 case RayDAT:
1258 case AIO:
1259 position &= HDSPM_BufferPositionMask;
1260 position /= 4; /* Bytes per sample */
1261 break;
1262 default:
1263 position = (position & HDSPM_BufferID) ?
1264 (hdspm->period_bytes / 4) : 0;
1265 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001266
1267 return position;
1268}
1269
1270
Takashi Iwai98274f02005-11-17 14:52:34 +01001271static inline void hdspm_start_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001272{
1273 s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
1274 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1275}
1276
Takashi Iwai98274f02005-11-17 14:52:34 +01001277static inline void hdspm_stop_audio(struct hdspm * s)
Takashi Iwai763f3562005-06-03 11:25:34 +02001278{
1279 s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
1280 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1281}
1282
1283/* should I silence all or only opened ones ? doit all for first even is 4MB*/
Denys Vlasenko62cef822008-04-14 13:04:18 +02001284static void hdspm_silence_playback(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02001285{
1286 int i;
1287 int n = hdspm->period_bytes;
1288 void *buf = hdspm->playback_buffer;
1289
Remy Bruno3cee5a62006-10-16 12:46:32 +02001290 if (buf == NULL)
1291 return;
Takashi Iwai763f3562005-06-03 11:25:34 +02001292
1293 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
1294 memset(buf, 0, n);
1295 buf += HDSPM_CHANNEL_BUFFER_BYTES;
1296 }
1297}
1298
Adrian Knoth0dca1792011-01-26 19:32:14 +01001299static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
Takashi Iwai763f3562005-06-03 11:25:34 +02001300{
1301 int n;
1302
1303 spin_lock_irq(&s->lock);
1304
1305 frames >>= 7;
1306 n = 0;
1307 while (frames) {
1308 n++;
1309 frames >>= 1;
1310 }
1311 s->control_register &= ~HDSPM_LatencyMask;
1312 s->control_register |= hdspm_encode_latency(n);
1313
1314 hdspm_write(s, HDSPM_controlRegister, s->control_register);
1315
1316 hdspm_compute_period_size(s);
1317
1318 spin_unlock_irq(&s->lock);
1319
1320 return 0;
1321}
1322
Adrian Knoth0dca1792011-01-26 19:32:14 +01001323static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
1324{
1325 u64 freq_const;
1326
1327 if (period == 0)
1328 return 0;
1329
1330 switch (hdspm->io_type) {
1331 case MADI:
1332 case AES32:
1333 freq_const = 110069313433624ULL;
1334 break;
1335 case RayDAT:
1336 case AIO:
1337 freq_const = 104857600000000ULL;
1338 break;
1339 case MADIface:
1340 freq_const = 131072000000000ULL;
1341 }
1342
1343 return div_u64(freq_const, period);
1344}
1345
1346
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001347static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
1348{
1349 u64 n;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001350
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001351 if (rate >= 112000)
1352 rate /= 4;
1353 else if (rate >= 56000)
1354 rate /= 2;
1355
Adrian Knoth0dca1792011-01-26 19:32:14 +01001356 switch (hdspm->io_type) {
1357 case MADIface:
1358 n = 131072000000000ULL; /* 125 MHz */
1359 break;
1360 case MADI:
1361 case AES32:
1362 n = 110069313433624ULL; /* 105 MHz */
1363 break;
1364 case RayDAT:
1365 case AIO:
1366 n = 104857600000000ULL; /* 100 MHz */
1367 break;
1368 }
1369
Takashi Iwai3f7440a2009-06-05 17:40:04 +02001370 n = div_u64(n, rate);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001371 /* n should be less than 2^32 for being written to FREQ register */
Takashi Iwaida3cec32008-08-08 17:12:14 +02001372 snd_BUG_ON(n >> 32);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001373 hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
1374}
Takashi Iwai763f3562005-06-03 11:25:34 +02001375
1376/* dummy set rate lets see what happens */
Takashi Iwai98274f02005-11-17 14:52:34 +01001377static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
Takashi Iwai763f3562005-06-03 11:25:34 +02001378{
Takashi Iwai763f3562005-06-03 11:25:34 +02001379 int current_rate;
1380 int rate_bits;
1381 int not_set = 0;
Remy Bruno65345992007-08-31 12:21:08 +02001382 int current_speed, target_speed;
Takashi Iwai763f3562005-06-03 11:25:34 +02001383
1384 /* ASSUMPTION: hdspm->lock is either set, or there is no need for
1385 it (e.g. during module initialization).
1386 */
1387
1388 if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {
1389
Adrian Knoth0dca1792011-01-26 19:32:14 +01001390 /* SLAVE --- */
Takashi Iwai763f3562005-06-03 11:25:34 +02001391 if (called_internally) {
1392
Adrian Knoth0dca1792011-01-26 19:32:14 +01001393 /* request from ctl or card initialization
1394 just make a warning an remember setting
1395 for future master mode switching */
1396
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001397 snd_printk(KERN_WARNING "HDSPM: "
1398 "Warning: device is not running "
1399 "as a clock master.\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001400 not_set = 1;
1401 } else {
1402
1403 /* hw_param request while in AutoSync mode */
1404 int external_freq =
1405 hdspm_external_sample_rate(hdspm);
1406
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001407 if (hdspm_autosync_ref(hdspm) ==
1408 HDSPM_AUTOSYNC_FROM_NONE) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001409
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001410 snd_printk(KERN_WARNING "HDSPM: "
1411 "Detected no Externel Sync \n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001412 not_set = 1;
1413
1414 } else if (rate != external_freq) {
1415
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001416 snd_printk(KERN_WARNING "HDSPM: "
1417 "Warning: No AutoSync source for "
1418 "requested rate\n");
Takashi Iwai763f3562005-06-03 11:25:34 +02001419 not_set = 1;
1420 }
1421 }
1422 }
1423
1424 current_rate = hdspm->system_sample_rate;
1425
1426 /* Changing between Singe, Double and Quad speed is not
1427 allowed if any substreams are open. This is because such a change
1428 causes a shift in the location of the DMA buffers and a reduction
1429 in the number of available buffers.
1430
1431 Note that a similar but essentially insoluble problem exists for
1432 externally-driven rate changes. All we can do is to flag rate
Adrian Knoth0dca1792011-01-26 19:32:14 +01001433 changes in the read/write routines.
Takashi Iwai763f3562005-06-03 11:25:34 +02001434 */
1435
Remy Bruno65345992007-08-31 12:21:08 +02001436 if (current_rate <= 48000)
1437 current_speed = HDSPM_SPEED_SINGLE;
1438 else if (current_rate <= 96000)
1439 current_speed = HDSPM_SPEED_DOUBLE;
1440 else
1441 current_speed = HDSPM_SPEED_QUAD;
1442
1443 if (rate <= 48000)
1444 target_speed = HDSPM_SPEED_SINGLE;
1445 else if (rate <= 96000)
1446 target_speed = HDSPM_SPEED_DOUBLE;
1447 else
1448 target_speed = HDSPM_SPEED_QUAD;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001449
Takashi Iwai763f3562005-06-03 11:25:34 +02001450 switch (rate) {
1451 case 32000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001452 rate_bits = HDSPM_Frequency32KHz;
1453 break;
1454 case 44100:
Takashi Iwai763f3562005-06-03 11:25:34 +02001455 rate_bits = HDSPM_Frequency44_1KHz;
1456 break;
1457 case 48000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001458 rate_bits = HDSPM_Frequency48KHz;
1459 break;
1460 case 64000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001461 rate_bits = HDSPM_Frequency64KHz;
1462 break;
1463 case 88200:
Takashi Iwai763f3562005-06-03 11:25:34 +02001464 rate_bits = HDSPM_Frequency88_2KHz;
1465 break;
1466 case 96000:
Takashi Iwai763f3562005-06-03 11:25:34 +02001467 rate_bits = HDSPM_Frequency96KHz;
1468 break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02001469 case 128000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001470 rate_bits = HDSPM_Frequency128KHz;
1471 break;
1472 case 176400:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001473 rate_bits = HDSPM_Frequency176_4KHz;
1474 break;
1475 case 192000:
Remy Bruno3cee5a62006-10-16 12:46:32 +02001476 rate_bits = HDSPM_Frequency192KHz;
1477 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02001478 default:
1479 return -EINVAL;
1480 }
1481
Remy Bruno65345992007-08-31 12:21:08 +02001482 if (current_speed != target_speed
Takashi Iwai763f3562005-06-03 11:25:34 +02001483 && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1484 snd_printk
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001485 (KERN_ERR "HDSPM: "
Remy Bruno65345992007-08-31 12:21:08 +02001486 "cannot change from %s speed to %s speed mode "
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001487 "(capture PID = %d, playback PID = %d)\n",
Remy Bruno65345992007-08-31 12:21:08 +02001488 hdspm_speed_names[current_speed],
1489 hdspm_speed_names[target_speed],
Takashi Iwai763f3562005-06-03 11:25:34 +02001490 hdspm->capture_pid, hdspm->playback_pid);
1491 return -EBUSY;
1492 }
1493
1494 hdspm->control_register &= ~HDSPM_FrequencyMask;
1495 hdspm->control_register |= rate_bits;
1496 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1497
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001498 /* For AES32, need to set DDS value in FREQ register
1499 For MADI, also apparently */
1500 hdspm_set_dds_value(hdspm, rate);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001501
1502 if (AES32 == hdspm->io_type && rate != current_rate)
Remy Brunoffb2c3c2007-03-07 19:08:46 +01001503 hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
Takashi Iwai763f3562005-06-03 11:25:34 +02001504
1505 hdspm->system_sample_rate = rate;
1506
Adrian Knoth0dca1792011-01-26 19:32:14 +01001507 if (rate <= 48000) {
1508 hdspm->channel_map_in = hdspm->channel_map_in_ss;
1509 hdspm->channel_map_out = hdspm->channel_map_out_ss;
1510 hdspm->max_channels_in = hdspm->ss_in_channels;
1511 hdspm->max_channels_out = hdspm->ss_out_channels;
1512 hdspm->port_names_in = hdspm->port_names_in_ss;
1513 hdspm->port_names_out = hdspm->port_names_out_ss;
1514 } else if (rate <= 96000) {
1515 hdspm->channel_map_in = hdspm->channel_map_in_ds;
1516 hdspm->channel_map_out = hdspm->channel_map_out_ds;
1517 hdspm->max_channels_in = hdspm->ds_in_channels;
1518 hdspm->max_channels_out = hdspm->ds_out_channels;
1519 hdspm->port_names_in = hdspm->port_names_in_ds;
1520 hdspm->port_names_out = hdspm->port_names_out_ds;
1521 } else {
1522 hdspm->channel_map_in = hdspm->channel_map_in_qs;
1523 hdspm->channel_map_out = hdspm->channel_map_out_qs;
1524 hdspm->max_channels_in = hdspm->qs_in_channels;
1525 hdspm->max_channels_out = hdspm->qs_out_channels;
1526 hdspm->port_names_in = hdspm->port_names_in_qs;
1527 hdspm->port_names_out = hdspm->port_names_out_qs;
1528 }
1529
Takashi Iwai763f3562005-06-03 11:25:34 +02001530 if (not_set != 0)
1531 return -1;
1532
1533 return 0;
1534}
1535
1536/* mainly for init to 0 on load */
Takashi Iwai98274f02005-11-17 14:52:34 +01001537static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
Takashi Iwai763f3562005-06-03 11:25:34 +02001538{
1539 int i, j;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001540 unsigned int gain;
1541
1542 if (sgain > UNITY_GAIN)
1543 gain = UNITY_GAIN;
1544 else if (sgain < 0)
1545 gain = 0;
1546 else
1547 gain = sgain;
Takashi Iwai763f3562005-06-03 11:25:34 +02001548
1549 for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
1550 for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
1551 hdspm_write_in_gain(hdspm, i, j, gain);
1552 hdspm_write_pb_gain(hdspm, i, j, gain);
1553 }
1554}
1555
1556/*----------------------------------------------------------------------------
1557 MIDI
1558 ----------------------------------------------------------------------------*/
1559
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001560static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
1561 int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001562{
1563 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001564 return hdspm_read(hdspm, hdspm->midi[id].dataIn);
Takashi Iwai763f3562005-06-03 11:25:34 +02001565}
1566
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001567static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
1568 int val)
Takashi Iwai763f3562005-06-03 11:25:34 +02001569{
1570 /* the hardware already does the relevant bit-mask with 0xff */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001571 return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
Takashi Iwai763f3562005-06-03 11:25:34 +02001572}
1573
Takashi Iwai98274f02005-11-17 14:52:34 +01001574static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001575{
Adrian Knoth0dca1792011-01-26 19:32:14 +01001576 return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001577}
1578
Takashi Iwai98274f02005-11-17 14:52:34 +01001579static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001580{
1581 int fifo_bytes_used;
1582
Adrian Knoth0dca1792011-01-26 19:32:14 +01001583 fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02001584
1585 if (fifo_bytes_used < 128)
1586 return 128 - fifo_bytes_used;
1587 else
1588 return 0;
1589}
1590
Denys Vlasenko62cef822008-04-14 13:04:18 +02001591static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001592{
1593 while (snd_hdspm_midi_input_available (hdspm, id))
1594 snd_hdspm_midi_read_byte (hdspm, id);
1595}
1596
Takashi Iwai98274f02005-11-17 14:52:34 +01001597static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001598{
1599 unsigned long flags;
1600 int n_pending;
1601 int to_write;
1602 int i;
1603 unsigned char buf[128];
1604
1605 /* Output is not interrupt driven */
Adrian Knoth0dca1792011-01-26 19:32:14 +01001606
Takashi Iwai763f3562005-06-03 11:25:34 +02001607 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001608 if (hmidi->output &&
1609 !snd_rawmidi_transmit_empty (hmidi->output)) {
1610 n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
1611 hmidi->id);
1612 if (n_pending > 0) {
1613 if (n_pending > (int)sizeof (buf))
1614 n_pending = sizeof (buf);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001615
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001616 to_write = snd_rawmidi_transmit (hmidi->output, buf,
1617 n_pending);
1618 if (to_write > 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001619 for (i = 0; i < to_write; ++i)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001620 snd_hdspm_midi_write_byte (hmidi->hdspm,
1621 hmidi->id,
1622 buf[i]);
Takashi Iwai763f3562005-06-03 11:25:34 +02001623 }
1624 }
1625 }
1626 spin_unlock_irqrestore (&hmidi->lock, flags);
1627 return 0;
1628}
1629
Takashi Iwai98274f02005-11-17 14:52:34 +01001630static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
Takashi Iwai763f3562005-06-03 11:25:34 +02001631{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001632 unsigned char buf[128]; /* this buffer is designed to match the MIDI
1633 * input FIFO size
1634 */
Takashi Iwai763f3562005-06-03 11:25:34 +02001635 unsigned long flags;
1636 int n_pending;
1637 int i;
1638
1639 spin_lock_irqsave (&hmidi->lock, flags);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001640 n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
1641 if (n_pending > 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001642 if (hmidi->input) {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001643 if (n_pending > (int)sizeof (buf))
Takashi Iwai763f3562005-06-03 11:25:34 +02001644 n_pending = sizeof (buf);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001645 for (i = 0; i < n_pending; ++i)
1646 buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
1647 hmidi->id);
1648 if (n_pending)
1649 snd_rawmidi_receive (hmidi->input, buf,
1650 n_pending);
Takashi Iwai763f3562005-06-03 11:25:34 +02001651 } else {
1652 /* flush the MIDI input FIFO */
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001653 while (n_pending--)
1654 snd_hdspm_midi_read_byte (hmidi->hdspm,
1655 hmidi->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02001656 }
1657 }
1658 hmidi->pending = 0;
Adrian Knothc0da0012011-06-12 17:26:17 +02001659 spin_unlock_irqrestore(&hmidi->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001660
Adrian Knothc0da0012011-06-12 17:26:17 +02001661 spin_lock_irqsave(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001662 hmidi->hdspm->control_register |= hmidi->ie;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001663 hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
1664 hmidi->hdspm->control_register);
Adrian Knothc0da0012011-06-12 17:26:17 +02001665 spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001666
Takashi Iwai763f3562005-06-03 11:25:34 +02001667 return snd_hdspm_midi_output_write (hmidi);
1668}
1669
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001670static void
1671snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001672{
Takashi Iwai98274f02005-11-17 14:52:34 +01001673 struct hdspm *hdspm;
1674 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001675 unsigned long flags;
Takashi Iwai763f3562005-06-03 11:25:34 +02001676
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001677 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001678 hdspm = hmidi->hdspm;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001679
Takashi Iwai763f3562005-06-03 11:25:34 +02001680 spin_lock_irqsave (&hdspm->lock, flags);
1681 if (up) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001682 if (!(hdspm->control_register & hmidi->ie)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02001683 snd_hdspm_flush_midi_input (hdspm, hmidi->id);
Adrian Knoth0dca1792011-01-26 19:32:14 +01001684 hdspm->control_register |= hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001685 }
1686 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01001687 hdspm->control_register &= ~hmidi->ie;
Takashi Iwai763f3562005-06-03 11:25:34 +02001688 }
1689
1690 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
1691 spin_unlock_irqrestore (&hdspm->lock, flags);
1692}
1693
1694static void snd_hdspm_midi_output_timer(unsigned long data)
1695{
Takashi Iwai98274f02005-11-17 14:52:34 +01001696 struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001697 unsigned long flags;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001698
Takashi Iwai763f3562005-06-03 11:25:34 +02001699 snd_hdspm_midi_output_write(hmidi);
1700 spin_lock_irqsave (&hmidi->lock, flags);
1701
1702 /* this does not bump hmidi->istimer, because the
1703 kernel automatically removed the timer when it
1704 expired, and we are now adding it back, thus
Adrian Knoth0dca1792011-01-26 19:32:14 +01001705 leaving istimer wherever it was set before.
Takashi Iwai763f3562005-06-03 11:25:34 +02001706 */
1707
1708 if (hmidi->istimer) {
1709 hmidi->timer.expires = 1 + jiffies;
1710 add_timer(&hmidi->timer);
1711 }
1712
1713 spin_unlock_irqrestore (&hmidi->lock, flags);
1714}
1715
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001716static void
1717snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Takashi Iwai763f3562005-06-03 11:25:34 +02001718{
Takashi Iwai98274f02005-11-17 14:52:34 +01001719 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001720 unsigned long flags;
1721
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001722 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001723 spin_lock_irqsave (&hmidi->lock, flags);
1724 if (up) {
1725 if (!hmidi->istimer) {
1726 init_timer(&hmidi->timer);
1727 hmidi->timer.function = snd_hdspm_midi_output_timer;
1728 hmidi->timer.data = (unsigned long) hmidi;
1729 hmidi->timer.expires = 1 + jiffies;
1730 add_timer(&hmidi->timer);
1731 hmidi->istimer++;
1732 }
1733 } else {
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001734 if (hmidi->istimer && --hmidi->istimer <= 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02001735 del_timer (&hmidi->timer);
Takashi Iwai763f3562005-06-03 11:25:34 +02001736 }
1737 spin_unlock_irqrestore (&hmidi->lock, flags);
1738 if (up)
1739 snd_hdspm_midi_output_write(hmidi);
1740}
1741
Takashi Iwai98274f02005-11-17 14:52:34 +01001742static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001743{
Takashi Iwai98274f02005-11-17 14:52:34 +01001744 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001745
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001746 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001747 spin_lock_irq (&hmidi->lock);
1748 snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
1749 hmidi->input = substream;
1750 spin_unlock_irq (&hmidi->lock);
1751
1752 return 0;
1753}
1754
Takashi Iwai98274f02005-11-17 14:52:34 +01001755static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001756{
Takashi Iwai98274f02005-11-17 14:52:34 +01001757 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001758
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001759 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001760 spin_lock_irq (&hmidi->lock);
1761 hmidi->output = substream;
1762 spin_unlock_irq (&hmidi->lock);
1763
1764 return 0;
1765}
1766
Takashi Iwai98274f02005-11-17 14:52:34 +01001767static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001768{
Takashi Iwai98274f02005-11-17 14:52:34 +01001769 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001770
1771 snd_hdspm_midi_input_trigger (substream, 0);
1772
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001773 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001774 spin_lock_irq (&hmidi->lock);
1775 hmidi->input = NULL;
1776 spin_unlock_irq (&hmidi->lock);
1777
1778 return 0;
1779}
1780
Takashi Iwai98274f02005-11-17 14:52:34 +01001781static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02001782{
Takashi Iwai98274f02005-11-17 14:52:34 +01001783 struct hdspm_midi *hmidi;
Takashi Iwai763f3562005-06-03 11:25:34 +02001784
1785 snd_hdspm_midi_output_trigger (substream, 0);
1786
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001787 hmidi = substream->rmidi->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02001788 spin_lock_irq (&hmidi->lock);
1789 hmidi->output = NULL;
1790 spin_unlock_irq (&hmidi->lock);
1791
1792 return 0;
1793}
1794
Takashi Iwai98274f02005-11-17 14:52:34 +01001795static struct snd_rawmidi_ops snd_hdspm_midi_output =
Takashi Iwai763f3562005-06-03 11:25:34 +02001796{
1797 .open = snd_hdspm_midi_output_open,
1798 .close = snd_hdspm_midi_output_close,
1799 .trigger = snd_hdspm_midi_output_trigger,
1800};
1801
Takashi Iwai98274f02005-11-17 14:52:34 +01001802static struct snd_rawmidi_ops snd_hdspm_midi_input =
Takashi Iwai763f3562005-06-03 11:25:34 +02001803{
1804 .open = snd_hdspm_midi_input_open,
1805 .close = snd_hdspm_midi_input_close,
1806 .trigger = snd_hdspm_midi_input_trigger,
1807};
1808
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02001809static int __devinit snd_hdspm_create_midi (struct snd_card *card,
1810 struct hdspm *hdspm, int id)
Takashi Iwai763f3562005-06-03 11:25:34 +02001811{
1812 int err;
1813 char buf[32];
1814
1815 hdspm->midi[id].id = id;
Takashi Iwai763f3562005-06-03 11:25:34 +02001816 hdspm->midi[id].hdspm = hdspm;
Takashi Iwai763f3562005-06-03 11:25:34 +02001817 spin_lock_init (&hdspm->midi[id].lock);
1818
Adrian Knoth0dca1792011-01-26 19:32:14 +01001819 if (0 == id) {
1820 if (MADIface == hdspm->io_type) {
1821 /* MIDI-over-MADI on HDSPe MADIface */
1822 hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
1823 hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
1824 hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
1825 hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
1826 hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
1827 hdspm->midi[0].irq = HDSPM_midi2IRQPending;
1828 } else {
1829 hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
1830 hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
1831 hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
1832 hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
1833 hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
1834 hdspm->midi[0].irq = HDSPM_midi0IRQPending;
1835 }
1836 } else if (1 == id) {
1837 hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
1838 hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
1839 hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
1840 hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
1841 hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
1842 hdspm->midi[1].irq = HDSPM_midi1IRQPending;
1843 } else if ((2 == id) && (MADI == hdspm->io_type)) {
1844 /* MIDI-over-MADI on HDSPe MADI */
1845 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1846 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1847 hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
1848 hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
1849 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1850 hdspm->midi[2].irq = HDSPM_midi2IRQPending;
1851 } else if (2 == id) {
1852 /* TCO MTC, read only */
1853 hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
1854 hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
1855 hdspm->midi[2].dataOut = -1;
1856 hdspm->midi[2].statusOut = -1;
1857 hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
1858 hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
1859 } else if (3 == id) {
1860 /* TCO MTC on HDSPe MADI */
1861 hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
1862 hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
1863 hdspm->midi[3].dataOut = -1;
1864 hdspm->midi[3].statusOut = -1;
1865 hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
1866 hdspm->midi[3].irq = HDSPM_midi3IRQPending;
1867 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001868
Adrian Knoth0dca1792011-01-26 19:32:14 +01001869 if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
1870 (MADIface == hdspm->io_type)))) {
1871 if ((id == 0) && (MADIface == hdspm->io_type)) {
1872 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1873 } else if ((id == 2) && (MADI == hdspm->io_type)) {
1874 sprintf(buf, "%s MIDIoverMADI", card->shortname);
1875 } else {
1876 sprintf(buf, "%s MIDI %d", card->shortname, id+1);
1877 }
1878 err = snd_rawmidi_new(card, buf, id, 1, 1,
1879 &hdspm->midi[id].rmidi);
1880 if (err < 0)
1881 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02001882
Adrian Knoth0dca1792011-01-26 19:32:14 +01001883 sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
1884 card->id, id+1);
1885 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
Takashi Iwai763f3562005-06-03 11:25:34 +02001886
Adrian Knoth0dca1792011-01-26 19:32:14 +01001887 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1888 SNDRV_RAWMIDI_STREAM_OUTPUT,
1889 &snd_hdspm_midi_output);
1890 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1891 SNDRV_RAWMIDI_STREAM_INPUT,
1892 &snd_hdspm_midi_input);
1893
1894 hdspm->midi[id].rmidi->info_flags |=
1895 SNDRV_RAWMIDI_INFO_OUTPUT |
1896 SNDRV_RAWMIDI_INFO_INPUT |
1897 SNDRV_RAWMIDI_INFO_DUPLEX;
1898 } else {
1899 /* TCO MTC, read only */
1900 sprintf(buf, "%s MTC %d", card->shortname, id+1);
1901 err = snd_rawmidi_new(card, buf, id, 1, 1,
1902 &hdspm->midi[id].rmidi);
1903 if (err < 0)
1904 return err;
1905
1906 sprintf(hdspm->midi[id].rmidi->name,
1907 "%s MTC %d", card->id, id+1);
1908 hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
1909
1910 snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
1911 SNDRV_RAWMIDI_STREAM_INPUT,
1912 &snd_hdspm_midi_input);
1913
1914 hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
1915 }
Takashi Iwai763f3562005-06-03 11:25:34 +02001916
1917 return 0;
1918}
1919
1920
1921static void hdspm_midi_tasklet(unsigned long arg)
1922{
Takashi Iwai98274f02005-11-17 14:52:34 +01001923 struct hdspm *hdspm = (struct hdspm *)arg;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001924 int i = 0;
1925
1926 while (i < hdspm->midiPorts) {
1927 if (hdspm->midi[i].pending)
1928 snd_hdspm_midi_input_read(&hdspm->midi[i]);
1929
1930 i++;
1931 }
1932}
Takashi Iwai763f3562005-06-03 11:25:34 +02001933
1934
1935/*-----------------------------------------------------------------------------
1936 Status Interface
1937 ----------------------------------------------------------------------------*/
1938
1939/* get the system sample rate which is set */
1940
Adrian Knoth0dca1792011-01-26 19:32:14 +01001941
1942/**
1943 * Calculate the real sample rate from the
1944 * current DDS value.
1945 **/
1946static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
1947{
1948 unsigned int period, rate;
1949
1950 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
1951 rate = hdspm_calc_dds_value(hdspm, period);
1952
1953 return rate;
1954}
1955
1956
Takashi Iwai763f3562005-06-03 11:25:34 +02001957#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001958{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02001959 .name = xname, \
1960 .index = xindex, \
1961 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
1962 .info = snd_hdspm_info_system_sample_rate, \
1963 .get = snd_hdspm_get_system_sample_rate \
1964}
1965
Takashi Iwai98274f02005-11-17 14:52:34 +01001966static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02001968{
1969 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1970 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01001971 uinfo->value.integer.min = 27000;
1972 uinfo->value.integer.max = 207000;
1973 uinfo->value.integer.step = 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02001974 return 0;
1975}
1976
Adrian Knoth0dca1792011-01-26 19:32:14 +01001977
Takashi Iwai98274f02005-11-17 14:52:34 +01001978static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02001980 ucontrol)
1981{
Takashi Iwai98274f02005-11-17 14:52:34 +01001982 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02001983
Adrian Knoth0dca1792011-01-26 19:32:14 +01001984 ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02001985 return 0;
1986}
1987
Adrian Knoth0dca1792011-01-26 19:32:14 +01001988
1989/**
1990 * Returns the WordClock sample rate class for the given card.
1991 **/
1992static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
1993{
1994 int status;
1995
1996 switch (hdspm->io_type) {
1997 case RayDAT:
1998 case AIO:
1999 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2000 return (status >> 16) & 0xF;
2001 break;
2002 default:
2003 break;
2004 }
2005
2006
2007 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002008}
2009
Adrian Knoth0dca1792011-01-26 19:32:14 +01002010
2011/**
2012 * Returns the TCO sample rate class for the given card.
2013 **/
2014static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
2015{
2016 int status;
2017
2018 if (hdspm->tco) {
2019 switch (hdspm->io_type) {
2020 case RayDAT:
2021 case AIO:
2022 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
2023 return (status >> 20) & 0xF;
2024 break;
2025 default:
2026 break;
2027 }
2028 }
2029
2030 return 0;
2031}
2032
2033
2034/**
2035 * Returns the SYNC_IN sample rate class for the given card.
2036 **/
2037static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
2038{
2039 int status;
2040
2041 if (hdspm->tco) {
2042 switch (hdspm->io_type) {
2043 case RayDAT:
2044 case AIO:
2045 status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2046 return (status >> 12) & 0xF;
2047 break;
2048 default:
2049 break;
2050 }
2051 }
2052
2053 return 0;
2054}
2055
2056
2057/**
2058 * Returns the sample rate class for input source <idx> for
2059 * 'new style' cards like the AIO and RayDAT.
2060 **/
2061static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
2062{
2063 int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
2064
2065 return (status >> (idx*4)) & 0xF;
2066}
2067
2068
2069
2070#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2071{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2072 .name = xname, \
2073 .private_value = xindex, \
2074 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2075 .info = snd_hdspm_info_autosync_sample_rate, \
2076 .get = snd_hdspm_get_autosync_sample_rate \
2077}
2078
2079
Takashi Iwai98274f02005-11-17 14:52:34 +01002080static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2081 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002082{
Takashi Iwai763f3562005-06-03 11:25:34 +02002083 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2084 uinfo->count = 1;
2085 uinfo->value.enumerated.items = 10;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002086
Takashi Iwai763f3562005-06-03 11:25:34 +02002087 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
Adrian Knoth0dca1792011-01-26 19:32:14 +01002088 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002089 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002090 texts_freq[uinfo->value.enumerated.item]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002091 return 0;
2092}
2093
Adrian Knoth0dca1792011-01-26 19:32:14 +01002094
Takashi Iwai98274f02005-11-17 14:52:34 +01002095static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *
Takashi Iwai763f3562005-06-03 11:25:34 +02002097 ucontrol)
2098{
Takashi Iwai98274f02005-11-17 14:52:34 +01002099 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002100
Adrian Knoth0dca1792011-01-26 19:32:14 +01002101 switch (hdspm->io_type) {
2102 case RayDAT:
2103 switch (kcontrol->private_value) {
2104 case 0:
2105 ucontrol->value.enumerated.item[0] =
2106 hdspm_get_wc_sample_rate(hdspm);
2107 break;
2108 case 7:
2109 ucontrol->value.enumerated.item[0] =
2110 hdspm_get_tco_sample_rate(hdspm);
2111 break;
2112 case 8:
2113 ucontrol->value.enumerated.item[0] =
2114 hdspm_get_sync_in_sample_rate(hdspm);
2115 break;
2116 default:
2117 ucontrol->value.enumerated.item[0] =
2118 hdspm_get_s1_sample_rate(hdspm,
2119 kcontrol->private_value-1);
2120 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002121
Adrian Knoth0dca1792011-01-26 19:32:14 +01002122 case AIO:
2123 switch (kcontrol->private_value) {
2124 case 0: /* WC */
2125 ucontrol->value.enumerated.item[0] =
2126 hdspm_get_wc_sample_rate(hdspm);
2127 break;
2128 case 4: /* TCO */
2129 ucontrol->value.enumerated.item[0] =
2130 hdspm_get_tco_sample_rate(hdspm);
2131 break;
2132 case 5: /* SYNC_IN */
2133 ucontrol->value.enumerated.item[0] =
2134 hdspm_get_sync_in_sample_rate(hdspm);
2135 break;
2136 default:
2137 ucontrol->value.enumerated.item[0] =
2138 hdspm_get_s1_sample_rate(hdspm,
2139 ucontrol->id.index-1);
2140 }
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01002141
2142 case AES32:
2143
2144 switch (kcontrol->private_value) {
2145 case 0: /* WC */
2146 ucontrol->value.enumerated.item[0] =
2147 hdspm_get_wc_sample_rate(hdspm);
2148 break;
2149 case 9: /* TCO */
2150 ucontrol->value.enumerated.item[0] =
2151 hdspm_get_tco_sample_rate(hdspm);
2152 break;
2153 case 10: /* SYNC_IN */
2154 ucontrol->value.enumerated.item[0] =
2155 hdspm_get_sync_in_sample_rate(hdspm);
2156 break;
2157 default: /* AES1 to AES8 */
2158 ucontrol->value.enumerated.item[0] =
2159 hdspm_get_s1_sample_rate(hdspm,
2160 kcontrol->private_value-1);
2161 break;
2162
2163 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002164 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002165 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002166 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002167
Takashi Iwai763f3562005-06-03 11:25:34 +02002168 return 0;
2169}
2170
Adrian Knoth0dca1792011-01-26 19:32:14 +01002171
Takashi Iwai763f3562005-06-03 11:25:34 +02002172#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002173{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2174 .name = xname, \
2175 .index = xindex, \
2176 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2177 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2178 .info = snd_hdspm_info_system_clock_mode, \
2179 .get = snd_hdspm_get_system_clock_mode, \
2180 .put = snd_hdspm_put_system_clock_mode, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002181}
2182
2183
Adrian Knoth0dca1792011-01-26 19:32:14 +01002184/**
2185 * Returns the system clock mode for the given card.
2186 * @returns 0 - master, 1 - slave
2187 **/
2188static int hdspm_system_clock_mode(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002189{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002190 switch (hdspm->io_type) {
2191 case AIO:
2192 case RayDAT:
2193 if (hdspm->settings_register & HDSPM_c0Master)
2194 return 0;
2195 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002196
Adrian Knoth0dca1792011-01-26 19:32:14 +01002197 default:
2198 if (hdspm->control_register & HDSPM_ClockModeMaster)
2199 return 0;
2200 }
2201
Takashi Iwai763f3562005-06-03 11:25:34 +02002202 return 1;
2203}
2204
Adrian Knoth0dca1792011-01-26 19:32:14 +01002205
2206/**
2207 * Sets the system clock mode.
2208 * @param mode 0 - master, 1 - slave
2209 **/
2210static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
2211{
2212 switch (hdspm->io_type) {
2213 case AIO:
2214 case RayDAT:
2215 if (0 == mode)
2216 hdspm->settings_register |= HDSPM_c0Master;
2217 else
2218 hdspm->settings_register &= ~HDSPM_c0Master;
2219
2220 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2221 break;
2222
2223 default:
2224 if (0 == mode)
2225 hdspm->control_register |= HDSPM_ClockModeMaster;
2226 else
2227 hdspm->control_register &= ~HDSPM_ClockModeMaster;
2228
2229 hdspm_write(hdspm, HDSPM_controlRegister,
2230 hdspm->control_register);
2231 }
2232}
2233
2234
Takashi Iwai98274f02005-11-17 14:52:34 +01002235static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2236 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002237{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002238 static char *texts[] = { "Master", "AutoSync" };
Takashi Iwai763f3562005-06-03 11:25:34 +02002239
2240 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2241 uinfo->count = 1;
2242 uinfo->value.enumerated.items = 2;
2243 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2244 uinfo->value.enumerated.item =
2245 uinfo->value.enumerated.items - 1;
2246 strcpy(uinfo->value.enumerated.name,
2247 texts[uinfo->value.enumerated.item]);
2248 return 0;
2249}
2250
Takashi Iwai98274f02005-11-17 14:52:34 +01002251static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
2252 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002253{
Takashi Iwai98274f02005-11-17 14:52:34 +01002254 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002255
Adrian Knoth0dca1792011-01-26 19:32:14 +01002256 ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002257 return 0;
2258}
2259
Adrian Knoth0dca1792011-01-26 19:32:14 +01002260static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_value *ucontrol)
2262{
2263 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2264 int val;
2265
2266 if (!snd_hdspm_use_is_exclusive(hdspm))
2267 return -EBUSY;
2268
2269 val = ucontrol->value.enumerated.item[0];
2270 if (val < 0)
2271 val = 0;
2272 else if (val > 1)
2273 val = 1;
2274
2275 hdspm_set_system_clock_mode(hdspm, val);
2276
2277 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002278}
2279
Adrian Knoth0dca1792011-01-26 19:32:14 +01002280
2281#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
2282{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2283 .name = xname, \
2284 .index = xindex, \
2285 .info = snd_hdspm_info_clock_source, \
2286 .get = snd_hdspm_get_clock_source, \
2287 .put = snd_hdspm_put_clock_source \
2288}
2289
2290
Takashi Iwai98274f02005-11-17 14:52:34 +01002291static int hdspm_clock_source(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002292{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002293 switch (hdspm->system_sample_rate) {
2294 case 32000: return 0;
2295 case 44100: return 1;
2296 case 48000: return 2;
2297 case 64000: return 3;
2298 case 88200: return 4;
2299 case 96000: return 5;
2300 case 128000: return 6;
2301 case 176400: return 7;
2302 case 192000: return 8;
Takashi Iwai763f3562005-06-03 11:25:34 +02002303 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002304
2305 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002306}
2307
Takashi Iwai98274f02005-11-17 14:52:34 +01002308static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
Takashi Iwai763f3562005-06-03 11:25:34 +02002309{
2310 int rate;
2311 switch (mode) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002312 case 0:
2313 rate = 32000; break;
2314 case 1:
2315 rate = 44100; break;
2316 case 2:
2317 rate = 48000; break;
2318 case 3:
2319 rate = 64000; break;
2320 case 4:
2321 rate = 88200; break;
2322 case 5:
2323 rate = 96000; break;
2324 case 6:
2325 rate = 128000; break;
2326 case 7:
2327 rate = 176400; break;
2328 case 8:
2329 rate = 192000; break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002330 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01002331 rate = 48000;
Takashi Iwai763f3562005-06-03 11:25:34 +02002332 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002333 hdspm_set_rate(hdspm, rate, 1);
2334 return 0;
2335}
2336
Takashi Iwai98274f02005-11-17 14:52:34 +01002337static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002339{
Takashi Iwai763f3562005-06-03 11:25:34 +02002340 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2341 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002342 uinfo->value.enumerated.items = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002343
2344 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2345 uinfo->value.enumerated.item =
2346 uinfo->value.enumerated.items - 1;
2347
2348 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01002349 texts_freq[uinfo->value.enumerated.item+1]);
Takashi Iwai763f3562005-06-03 11:25:34 +02002350
2351 return 0;
2352}
2353
Takashi Iwai98274f02005-11-17 14:52:34 +01002354static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002356{
Takashi Iwai98274f02005-11-17 14:52:34 +01002357 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002358
2359 ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
2360 return 0;
2361}
2362
Takashi Iwai98274f02005-11-17 14:52:34 +01002363static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
2364 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002365{
Takashi Iwai98274f02005-11-17 14:52:34 +01002366 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002367 int change;
2368 int val;
2369
2370 if (!snd_hdspm_use_is_exclusive(hdspm))
2371 return -EBUSY;
2372 val = ucontrol->value.enumerated.item[0];
2373 if (val < 0)
2374 val = 0;
Remy Bruno65345992007-08-31 12:21:08 +02002375 if (val > 9)
2376 val = 9;
Takashi Iwai763f3562005-06-03 11:25:34 +02002377 spin_lock_irq(&hdspm->lock);
2378 if (val != hdspm_clock_source(hdspm))
2379 change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
2380 else
2381 change = 0;
2382 spin_unlock_irq(&hdspm->lock);
2383 return change;
2384}
2385
Adrian Knoth0dca1792011-01-26 19:32:14 +01002386
Takashi Iwai763f3562005-06-03 11:25:34 +02002387#define HDSPM_PREF_SYNC_REF(xname, xindex) \
Adrian Knoth0dca1792011-01-26 19:32:14 +01002388{.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2389 .name = xname, \
2390 .index = xindex, \
2391 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
2392 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
2393 .info = snd_hdspm_info_pref_sync_ref, \
2394 .get = snd_hdspm_get_pref_sync_ref, \
2395 .put = snd_hdspm_put_pref_sync_ref \
Takashi Iwai763f3562005-06-03 11:25:34 +02002396}
2397
Adrian Knoth0dca1792011-01-26 19:32:14 +01002398
2399/**
2400 * Returns the current preferred sync reference setting.
2401 * The semantics of the return value are depending on the
2402 * card, please see the comments for clarification.
2403 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002404static int hdspm_pref_sync_ref(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002405{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002406 switch (hdspm->io_type) {
2407 case AES32:
Remy Bruno3cee5a62006-10-16 12:46:32 +02002408 switch (hdspm->control_register & HDSPM_SyncRefMask) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002409 case 0: return 0; /* WC */
2410 case HDSPM_SyncRef0: return 1; /* AES 1 */
2411 case HDSPM_SyncRef1: return 2; /* AES 2 */
2412 case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
2413 case HDSPM_SyncRef2: return 4; /* AES 4 */
2414 case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
2415 case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
2416 case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
2417 return 7; /* AES 7 */
2418 case HDSPM_SyncRef3: return 8; /* AES 8 */
2419 case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002420 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002421 break;
2422
2423 case MADI:
2424 case MADIface:
2425 if (hdspm->tco) {
2426 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2427 case 0: return 0; /* WC */
2428 case HDSPM_SyncRef0: return 1; /* MADI */
2429 case HDSPM_SyncRef1: return 2; /* TCO */
2430 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2431 return 3; /* SYNC_IN */
2432 }
2433 } else {
2434 switch (hdspm->control_register & HDSPM_SyncRefMask) {
2435 case 0: return 0; /* WC */
2436 case HDSPM_SyncRef0: return 1; /* MADI */
2437 case HDSPM_SyncRef1+HDSPM_SyncRef0:
2438 return 2; /* SYNC_IN */
2439 }
Remy Bruno3cee5a62006-10-16 12:46:32 +02002440 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002441 break;
2442
2443 case RayDAT:
2444 if (hdspm->tco) {
2445 switch ((hdspm->settings_register &
2446 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2447 case 0: return 0; /* WC */
2448 case 3: return 1; /* ADAT 1 */
2449 case 4: return 2; /* ADAT 2 */
2450 case 5: return 3; /* ADAT 3 */
2451 case 6: return 4; /* ADAT 4 */
2452 case 1: return 5; /* AES */
2453 case 2: return 6; /* SPDIF */
2454 case 9: return 7; /* TCO */
2455 case 10: return 8; /* SYNC_IN */
2456 }
2457 } else {
2458 switch ((hdspm->settings_register &
2459 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2460 case 0: return 0; /* WC */
2461 case 3: return 1; /* ADAT 1 */
2462 case 4: return 2; /* ADAT 2 */
2463 case 5: return 3; /* ADAT 3 */
2464 case 6: return 4; /* ADAT 4 */
2465 case 1: return 5; /* AES */
2466 case 2: return 6; /* SPDIF */
2467 case 10: return 7; /* SYNC_IN */
2468 }
2469 }
2470
2471 break;
2472
2473 case AIO:
2474 if (hdspm->tco) {
2475 switch ((hdspm->settings_register &
2476 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2477 case 0: return 0; /* WC */
2478 case 3: return 1; /* ADAT */
2479 case 1: return 2; /* AES */
2480 case 2: return 3; /* SPDIF */
2481 case 9: return 4; /* TCO */
2482 case 10: return 5; /* SYNC_IN */
2483 }
2484 } else {
2485 switch ((hdspm->settings_register &
2486 HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
2487 case 0: return 0; /* WC */
2488 case 3: return 1; /* ADAT */
2489 case 1: return 2; /* AES */
2490 case 2: return 3; /* SPDIF */
2491 case 10: return 4; /* SYNC_IN */
2492 }
2493 }
2494
2495 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002496 }
2497
Adrian Knoth0dca1792011-01-26 19:32:14 +01002498 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002499}
2500
Adrian Knoth0dca1792011-01-26 19:32:14 +01002501
2502/**
2503 * Set the preferred sync reference to <pref>. The semantics
2504 * of <pref> are depending on the card type, see the comments
2505 * for clarification.
2506 **/
Takashi Iwai98274f02005-11-17 14:52:34 +01002507static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
Takashi Iwai763f3562005-06-03 11:25:34 +02002508{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002509 int p = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002510
Adrian Knoth0dca1792011-01-26 19:32:14 +01002511 switch (hdspm->io_type) {
2512 case AES32:
2513 hdspm->control_register &= ~HDSPM_SyncRefMask;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002514 switch (pref) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01002515 case 0: /* WC */
Remy Bruno3cee5a62006-10-16 12:46:32 +02002516 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002517 case 1: /* AES 1 */
2518 hdspm->control_register |= HDSPM_SyncRef0;
2519 break;
2520 case 2: /* AES 2 */
2521 hdspm->control_register |= HDSPM_SyncRef1;
2522 break;
2523 case 3: /* AES 3 */
2524 hdspm->control_register |=
2525 HDSPM_SyncRef1+HDSPM_SyncRef0;
2526 break;
2527 case 4: /* AES 4 */
2528 hdspm->control_register |= HDSPM_SyncRef2;
2529 break;
2530 case 5: /* AES 5 */
2531 hdspm->control_register |=
2532 HDSPM_SyncRef2+HDSPM_SyncRef0;
2533 break;
2534 case 6: /* AES 6 */
2535 hdspm->control_register |=
2536 HDSPM_SyncRef2+HDSPM_SyncRef1;
2537 break;
2538 case 7: /* AES 7 */
2539 hdspm->control_register |=
2540 HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
2541 break;
2542 case 8: /* AES 8 */
2543 hdspm->control_register |= HDSPM_SyncRef3;
2544 break;
2545 case 9: /* TCO */
2546 hdspm->control_register |=
2547 HDSPM_SyncRef3+HDSPM_SyncRef0;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002548 break;
2549 default:
2550 return -1;
2551 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002552
2553 break;
2554
2555 case MADI:
2556 case MADIface:
2557 hdspm->control_register &= ~HDSPM_SyncRefMask;
2558 if (hdspm->tco) {
2559 switch (pref) {
2560 case 0: /* WC */
2561 break;
2562 case 1: /* MADI */
2563 hdspm->control_register |= HDSPM_SyncRef0;
2564 break;
2565 case 2: /* TCO */
2566 hdspm->control_register |= HDSPM_SyncRef1;
2567 break;
2568 case 3: /* SYNC_IN */
2569 hdspm->control_register |=
2570 HDSPM_SyncRef0+HDSPM_SyncRef1;
2571 break;
2572 default:
2573 return -1;
2574 }
2575 } else {
2576 switch (pref) {
2577 case 0: /* WC */
2578 break;
2579 case 1: /* MADI */
2580 hdspm->control_register |= HDSPM_SyncRef0;
2581 break;
2582 case 2: /* SYNC_IN */
2583 hdspm->control_register |=
2584 HDSPM_SyncRef0+HDSPM_SyncRef1;
2585 break;
2586 default:
2587 return -1;
2588 }
2589 }
2590
2591 break;
2592
2593 case RayDAT:
2594 if (hdspm->tco) {
2595 switch (pref) {
2596 case 0: p = 0; break; /* WC */
2597 case 1: p = 3; break; /* ADAT 1 */
2598 case 2: p = 4; break; /* ADAT 2 */
2599 case 3: p = 5; break; /* ADAT 3 */
2600 case 4: p = 6; break; /* ADAT 4 */
2601 case 5: p = 1; break; /* AES */
2602 case 6: p = 2; break; /* SPDIF */
2603 case 7: p = 9; break; /* TCO */
2604 case 8: p = 10; break; /* SYNC_IN */
2605 default: return -1;
2606 }
2607 } else {
2608 switch (pref) {
2609 case 0: p = 0; break; /* WC */
2610 case 1: p = 3; break; /* ADAT 1 */
2611 case 2: p = 4; break; /* ADAT 2 */
2612 case 3: p = 5; break; /* ADAT 3 */
2613 case 4: p = 6; break; /* ADAT 4 */
2614 case 5: p = 1; break; /* AES */
2615 case 6: p = 2; break; /* SPDIF */
2616 case 7: p = 10; break; /* SYNC_IN */
2617 default: return -1;
2618 }
2619 }
2620 break;
2621
2622 case AIO:
2623 if (hdspm->tco) {
2624 switch (pref) {
2625 case 0: p = 0; break; /* WC */
2626 case 1: p = 3; break; /* ADAT */
2627 case 2: p = 1; break; /* AES */
2628 case 3: p = 2; break; /* SPDIF */
2629 case 4: p = 9; break; /* TCO */
2630 case 5: p = 10; break; /* SYNC_IN */
2631 default: return -1;
2632 }
2633 } else {
2634 switch (pref) {
2635 case 0: p = 0; break; /* WC */
2636 case 1: p = 3; break; /* ADAT */
2637 case 2: p = 1; break; /* AES */
2638 case 3: p = 2; break; /* SPDIF */
2639 case 4: p = 10; break; /* SYNC_IN */
2640 default: return -1;
2641 }
2642 }
2643 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02002644 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002645
2646 switch (hdspm->io_type) {
2647 case RayDAT:
2648 case AIO:
2649 hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
2650 hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
2651 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
2652 break;
2653
2654 case MADI:
2655 case MADIface:
2656 case AES32:
2657 hdspm_write(hdspm, HDSPM_controlRegister,
2658 hdspm->control_register);
2659 }
2660
Takashi Iwai763f3562005-06-03 11:25:34 +02002661 return 0;
2662}
2663
Adrian Knoth0dca1792011-01-26 19:32:14 +01002664
Takashi Iwai98274f02005-11-17 14:52:34 +01002665static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002667{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002668 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002669
Adrian Knoth0dca1792011-01-26 19:32:14 +01002670 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2671 uinfo->count = 1;
2672 uinfo->value.enumerated.items = hdspm->texts_autosync_items;
Takashi Iwai763f3562005-06-03 11:25:34 +02002673
Adrian Knoth0dca1792011-01-26 19:32:14 +01002674 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2675 uinfo->value.enumerated.item =
2676 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002677
Adrian Knoth0dca1792011-01-26 19:32:14 +01002678 strcpy(uinfo->value.enumerated.name,
2679 hdspm->texts_autosync[uinfo->value.enumerated.item]);
Remy Bruno3cee5a62006-10-16 12:46:32 +02002680
Takashi Iwai763f3562005-06-03 11:25:34 +02002681 return 0;
2682}
2683
Takashi Iwai98274f02005-11-17 14:52:34 +01002684static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002686{
Takashi Iwai98274f02005-11-17 14:52:34 +01002687 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002688 int psf = hdspm_pref_sync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002689
Adrian Knoth0dca1792011-01-26 19:32:14 +01002690 if (psf >= 0) {
2691 ucontrol->value.enumerated.item[0] = psf;
2692 return 0;
2693 }
2694
2695 return -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002696}
2697
Takashi Iwai98274f02005-11-17 14:52:34 +01002698static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
2699 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002700{
Takashi Iwai98274f02005-11-17 14:52:34 +01002701 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002702 int val, change = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002703
2704 if (!snd_hdspm_use_is_exclusive(hdspm))
2705 return -EBUSY;
2706
Adrian Knoth0dca1792011-01-26 19:32:14 +01002707 val = ucontrol->value.enumerated.item[0];
2708
2709 if (val < 0)
2710 val = 0;
2711 else if (val >= hdspm->texts_autosync_items)
2712 val = hdspm->texts_autosync_items-1;
Takashi Iwai763f3562005-06-03 11:25:34 +02002713
2714 spin_lock_irq(&hdspm->lock);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002715 if (val != hdspm_pref_sync_ref(hdspm))
2716 change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;
2717
Takashi Iwai763f3562005-06-03 11:25:34 +02002718 spin_unlock_irq(&hdspm->lock);
2719 return change;
2720}
2721
Adrian Knoth0dca1792011-01-26 19:32:14 +01002722
Takashi Iwai763f3562005-06-03 11:25:34 +02002723#define HDSPM_AUTOSYNC_REF(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002724{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002725 .name = xname, \
2726 .index = xindex, \
2727 .access = SNDRV_CTL_ELEM_ACCESS_READ, \
2728 .info = snd_hdspm_info_autosync_ref, \
2729 .get = snd_hdspm_get_autosync_ref, \
2730}
2731
Adrian Knoth0dca1792011-01-26 19:32:14 +01002732static int hdspm_autosync_ref(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002733{
Adrian Knoth0dca1792011-01-26 19:32:14 +01002734 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002735 unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002736 unsigned int syncref =
2737 (status >> HDSPM_AES32_syncref_bit) & 0xF;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002738 if (syncref == 0)
2739 return HDSPM_AES32_AUTOSYNC_FROM_WORD;
2740 if (syncref <= 8)
2741 return syncref;
2742 return HDSPM_AES32_AUTOSYNC_FROM_NONE;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002743 } else if (MADI == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002744 /* This looks at the autosync selected sync reference */
2745 unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Takashi Iwai763f3562005-06-03 11:25:34 +02002746
Remy Bruno3cee5a62006-10-16 12:46:32 +02002747 switch (status2 & HDSPM_SelSyncRefMask) {
2748 case HDSPM_SelSyncRef_WORD:
2749 return HDSPM_AUTOSYNC_FROM_WORD;
2750 case HDSPM_SelSyncRef_MADI:
2751 return HDSPM_AUTOSYNC_FROM_MADI;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002752 case HDSPM_SelSyncRef_TCO:
2753 return HDSPM_AUTOSYNC_FROM_TCO;
2754 case HDSPM_SelSyncRef_SyncIn:
2755 return HDSPM_AUTOSYNC_FROM_SYNC_IN;
Remy Bruno3cee5a62006-10-16 12:46:32 +02002756 case HDSPM_SelSyncRef_NVALID:
2757 return HDSPM_AUTOSYNC_FROM_NONE;
2758 default:
2759 return 0;
2760 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002761
Takashi Iwai763f3562005-06-03 11:25:34 +02002762 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01002763 return 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02002764}
2765
Adrian Knoth0dca1792011-01-26 19:32:14 +01002766
Takashi Iwai98274f02005-11-17 14:52:34 +01002767static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02002769{
Remy Bruno3cee5a62006-10-16 12:46:32 +02002770 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002771
Adrian Knoth0dca1792011-01-26 19:32:14 +01002772 if (AES32 == hdspm->io_type) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02002773 static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
2774 "AES4", "AES5", "AES6", "AES7", "AES8", "None"};
2775
2776 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2777 uinfo->count = 1;
2778 uinfo->value.enumerated.items = 10;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002779 if (uinfo->value.enumerated.item >=
2780 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002781 uinfo->value.enumerated.item =
2782 uinfo->value.enumerated.items - 1;
2783 strcpy(uinfo->value.enumerated.name,
2784 texts[uinfo->value.enumerated.item]);
Adrian Knoth0dca1792011-01-26 19:32:14 +01002785 } else if (MADI == hdspm->io_type) {
2786 static char *texts[] = {"Word Clock", "MADI", "TCO",
2787 "Sync In", "None" };
Remy Bruno3cee5a62006-10-16 12:46:32 +02002788
2789 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2790 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01002791 uinfo->value.enumerated.items = 5;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02002792 if (uinfo->value.enumerated.item >=
Adrian Knoth0dca1792011-01-26 19:32:14 +01002793 uinfo->value.enumerated.items)
Remy Bruno3cee5a62006-10-16 12:46:32 +02002794 uinfo->value.enumerated.item =
2795 uinfo->value.enumerated.items - 1;
2796 strcpy(uinfo->value.enumerated.name,
2797 texts[uinfo->value.enumerated.item]);
2798 }
Takashi Iwai763f3562005-06-03 11:25:34 +02002799 return 0;
2800}
2801
Takashi Iwai98274f02005-11-17 14:52:34 +01002802static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002804{
Takashi Iwai98274f02005-11-17 14:52:34 +01002805 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002806
Remy Bruno65345992007-08-31 12:21:08 +02002807 ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02002808 return 0;
2809}
2810
Adrian Knoth0dca1792011-01-26 19:32:14 +01002811
Takashi Iwai763f3562005-06-03 11:25:34 +02002812#define HDSPM_LINE_OUT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002813{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002814 .name = xname, \
2815 .index = xindex, \
2816 .info = snd_hdspm_info_line_out, \
2817 .get = snd_hdspm_get_line_out, \
2818 .put = snd_hdspm_put_line_out \
2819}
2820
Takashi Iwai98274f02005-11-17 14:52:34 +01002821static int hdspm_line_out(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002822{
2823 return (hdspm->control_register & HDSPM_LineOut) ? 1 : 0;
2824}
2825
2826
Takashi Iwai98274f02005-11-17 14:52:34 +01002827static int hdspm_set_line_output(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02002828{
2829 if (out)
2830 hdspm->control_register |= HDSPM_LineOut;
2831 else
2832 hdspm->control_register &= ~HDSPM_LineOut;
2833 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
2834
2835 return 0;
2836}
2837
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002838#define snd_hdspm_info_line_out snd_ctl_boolean_mono_info
Takashi Iwai763f3562005-06-03 11:25:34 +02002839
Takashi Iwai98274f02005-11-17 14:52:34 +01002840static int snd_hdspm_get_line_out(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002842{
Takashi Iwai98274f02005-11-17 14:52:34 +01002843 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002844
2845 spin_lock_irq(&hdspm->lock);
2846 ucontrol->value.integer.value[0] = hdspm_line_out(hdspm);
2847 spin_unlock_irq(&hdspm->lock);
2848 return 0;
2849}
2850
Takashi Iwai98274f02005-11-17 14:52:34 +01002851static int snd_hdspm_put_line_out(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002853{
Takashi Iwai98274f02005-11-17 14:52:34 +01002854 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002855 int change;
2856 unsigned int val;
2857
2858 if (!snd_hdspm_use_is_exclusive(hdspm))
2859 return -EBUSY;
2860 val = ucontrol->value.integer.value[0] & 1;
2861 spin_lock_irq(&hdspm->lock);
2862 change = (int) val != hdspm_line_out(hdspm);
2863 hdspm_set_line_output(hdspm, val);
2864 spin_unlock_irq(&hdspm->lock);
2865 return change;
2866}
2867
Adrian Knoth0dca1792011-01-26 19:32:14 +01002868
Takashi Iwai763f3562005-06-03 11:25:34 +02002869#define HDSPM_TX_64(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002870{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002871 .name = xname, \
2872 .index = xindex, \
2873 .info = snd_hdspm_info_tx_64, \
2874 .get = snd_hdspm_get_tx_64, \
2875 .put = snd_hdspm_put_tx_64 \
2876}
2877
Takashi Iwai98274f02005-11-17 14:52:34 +01002878static int hdspm_tx_64(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002879{
2880 return (hdspm->control_register & HDSPM_TX_64ch) ? 1 : 0;
2881}
2882
Takashi Iwai98274f02005-11-17 14:52:34 +01002883static int hdspm_set_tx_64(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02002884{
2885 if (out)
2886 hdspm->control_register |= HDSPM_TX_64ch;
2887 else
2888 hdspm->control_register &= ~HDSPM_TX_64ch;
2889 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
2890
2891 return 0;
2892}
2893
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002894#define snd_hdspm_info_tx_64 snd_ctl_boolean_mono_info
Takashi Iwai763f3562005-06-03 11:25:34 +02002895
Takashi Iwai98274f02005-11-17 14:52:34 +01002896static int snd_hdspm_get_tx_64(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002898{
Takashi Iwai98274f02005-11-17 14:52:34 +01002899 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002900
2901 spin_lock_irq(&hdspm->lock);
2902 ucontrol->value.integer.value[0] = hdspm_tx_64(hdspm);
2903 spin_unlock_irq(&hdspm->lock);
2904 return 0;
2905}
2906
Takashi Iwai98274f02005-11-17 14:52:34 +01002907static int snd_hdspm_put_tx_64(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002909{
Takashi Iwai98274f02005-11-17 14:52:34 +01002910 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002911 int change;
2912 unsigned int val;
2913
2914 if (!snd_hdspm_use_is_exclusive(hdspm))
2915 return -EBUSY;
2916 val = ucontrol->value.integer.value[0] & 1;
2917 spin_lock_irq(&hdspm->lock);
2918 change = (int) val != hdspm_tx_64(hdspm);
2919 hdspm_set_tx_64(hdspm, val);
2920 spin_unlock_irq(&hdspm->lock);
2921 return change;
2922}
2923
Adrian Knoth0dca1792011-01-26 19:32:14 +01002924
Takashi Iwai763f3562005-06-03 11:25:34 +02002925#define HDSPM_C_TMS(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002926{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002927 .name = xname, \
2928 .index = xindex, \
2929 .info = snd_hdspm_info_c_tms, \
2930 .get = snd_hdspm_get_c_tms, \
2931 .put = snd_hdspm_put_c_tms \
2932}
2933
Takashi Iwai98274f02005-11-17 14:52:34 +01002934static int hdspm_c_tms(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002935{
2936 return (hdspm->control_register & HDSPM_clr_tms) ? 1 : 0;
2937}
2938
Takashi Iwai98274f02005-11-17 14:52:34 +01002939static int hdspm_set_c_tms(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02002940{
2941 if (out)
2942 hdspm->control_register |= HDSPM_clr_tms;
2943 else
2944 hdspm->control_register &= ~HDSPM_clr_tms;
2945 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
2946
2947 return 0;
2948}
2949
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002950#define snd_hdspm_info_c_tms snd_ctl_boolean_mono_info
Takashi Iwai763f3562005-06-03 11:25:34 +02002951
Takashi Iwai98274f02005-11-17 14:52:34 +01002952static int snd_hdspm_get_c_tms(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002954{
Takashi Iwai98274f02005-11-17 14:52:34 +01002955 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002956
2957 spin_lock_irq(&hdspm->lock);
2958 ucontrol->value.integer.value[0] = hdspm_c_tms(hdspm);
2959 spin_unlock_irq(&hdspm->lock);
2960 return 0;
2961}
2962
Takashi Iwai98274f02005-11-17 14:52:34 +01002963static int snd_hdspm_put_c_tms(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02002965{
Takashi Iwai98274f02005-11-17 14:52:34 +01002966 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02002967 int change;
2968 unsigned int val;
2969
2970 if (!snd_hdspm_use_is_exclusive(hdspm))
2971 return -EBUSY;
2972 val = ucontrol->value.integer.value[0] & 1;
2973 spin_lock_irq(&hdspm->lock);
2974 change = (int) val != hdspm_c_tms(hdspm);
2975 hdspm_set_c_tms(hdspm, val);
2976 spin_unlock_irq(&hdspm->lock);
2977 return change;
2978}
2979
Adrian Knoth0dca1792011-01-26 19:32:14 +01002980
Takashi Iwai763f3562005-06-03 11:25:34 +02002981#define HDSPM_SAFE_MODE(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002982{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02002983 .name = xname, \
2984 .index = xindex, \
2985 .info = snd_hdspm_info_safe_mode, \
2986 .get = snd_hdspm_get_safe_mode, \
2987 .put = snd_hdspm_put_safe_mode \
2988}
2989
Takashi Iwai98274f02005-11-17 14:52:34 +01002990static int hdspm_safe_mode(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02002991{
2992 return (hdspm->control_register & HDSPM_AutoInp) ? 1 : 0;
2993}
2994
Takashi Iwai98274f02005-11-17 14:52:34 +01002995static int hdspm_set_safe_mode(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02002996{
2997 if (out)
2998 hdspm->control_register |= HDSPM_AutoInp;
2999 else
3000 hdspm->control_register &= ~HDSPM_AutoInp;
3001 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3002
3003 return 0;
3004}
3005
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003006#define snd_hdspm_info_safe_mode snd_ctl_boolean_mono_info
Takashi Iwai763f3562005-06-03 11:25:34 +02003007
Takashi Iwai98274f02005-11-17 14:52:34 +01003008static int snd_hdspm_get_safe_mode(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003010{
Takashi Iwai98274f02005-11-17 14:52:34 +01003011 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003012
3013 spin_lock_irq(&hdspm->lock);
3014 ucontrol->value.integer.value[0] = hdspm_safe_mode(hdspm);
3015 spin_unlock_irq(&hdspm->lock);
3016 return 0;
3017}
3018
Takashi Iwai98274f02005-11-17 14:52:34 +01003019static int snd_hdspm_put_safe_mode(struct snd_kcontrol *kcontrol,
3020 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003021{
Takashi Iwai98274f02005-11-17 14:52:34 +01003022 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003023 int change;
3024 unsigned int val;
3025
3026 if (!snd_hdspm_use_is_exclusive(hdspm))
3027 return -EBUSY;
3028 val = ucontrol->value.integer.value[0] & 1;
3029 spin_lock_irq(&hdspm->lock);
3030 change = (int) val != hdspm_safe_mode(hdspm);
3031 hdspm_set_safe_mode(hdspm, val);
3032 spin_unlock_irq(&hdspm->lock);
3033 return change;
3034}
3035
Adrian Knoth0dca1792011-01-26 19:32:14 +01003036
Remy Bruno3cee5a62006-10-16 12:46:32 +02003037#define HDSPM_EMPHASIS(xname, xindex) \
3038{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3039 .name = xname, \
3040 .index = xindex, \
3041 .info = snd_hdspm_info_emphasis, \
3042 .get = snd_hdspm_get_emphasis, \
3043 .put = snd_hdspm_put_emphasis \
3044}
3045
3046static int hdspm_emphasis(struct hdspm * hdspm)
3047{
3048 return (hdspm->control_register & HDSPM_Emphasis) ? 1 : 0;
3049}
3050
3051static int hdspm_set_emphasis(struct hdspm * hdspm, int emp)
3052{
3053 if (emp)
3054 hdspm->control_register |= HDSPM_Emphasis;
3055 else
3056 hdspm->control_register &= ~HDSPM_Emphasis;
3057 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3058
3059 return 0;
3060}
3061
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003062#define snd_hdspm_info_emphasis snd_ctl_boolean_mono_info
Remy Bruno3cee5a62006-10-16 12:46:32 +02003063
3064static int snd_hdspm_get_emphasis(struct snd_kcontrol *kcontrol,
3065 struct snd_ctl_elem_value *ucontrol)
3066{
3067 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3068
3069 spin_lock_irq(&hdspm->lock);
3070 ucontrol->value.enumerated.item[0] = hdspm_emphasis(hdspm);
3071 spin_unlock_irq(&hdspm->lock);
3072 return 0;
3073}
3074
3075static int snd_hdspm_put_emphasis(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_value *ucontrol)
3077{
3078 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3079 int change;
3080 unsigned int val;
3081
3082 if (!snd_hdspm_use_is_exclusive(hdspm))
3083 return -EBUSY;
3084 val = ucontrol->value.integer.value[0] & 1;
3085 spin_lock_irq(&hdspm->lock);
3086 change = (int) val != hdspm_emphasis(hdspm);
3087 hdspm_set_emphasis(hdspm, val);
3088 spin_unlock_irq(&hdspm->lock);
3089 return change;
3090}
3091
Adrian Knoth0dca1792011-01-26 19:32:14 +01003092
Remy Bruno3cee5a62006-10-16 12:46:32 +02003093#define HDSPM_DOLBY(xname, xindex) \
3094{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3095 .name = xname, \
3096 .index = xindex, \
3097 .info = snd_hdspm_info_dolby, \
3098 .get = snd_hdspm_get_dolby, \
3099 .put = snd_hdspm_put_dolby \
3100}
3101
3102static int hdspm_dolby(struct hdspm * hdspm)
3103{
3104 return (hdspm->control_register & HDSPM_Dolby) ? 1 : 0;
3105}
3106
3107static int hdspm_set_dolby(struct hdspm * hdspm, int dol)
3108{
3109 if (dol)
3110 hdspm->control_register |= HDSPM_Dolby;
3111 else
3112 hdspm->control_register &= ~HDSPM_Dolby;
3113 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3114
3115 return 0;
3116}
3117
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003118#define snd_hdspm_info_dolby snd_ctl_boolean_mono_info
Remy Bruno3cee5a62006-10-16 12:46:32 +02003119
3120static int snd_hdspm_get_dolby(struct snd_kcontrol *kcontrol,
3121 struct snd_ctl_elem_value *ucontrol)
3122{
3123 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3124
3125 spin_lock_irq(&hdspm->lock);
3126 ucontrol->value.enumerated.item[0] = hdspm_dolby(hdspm);
3127 spin_unlock_irq(&hdspm->lock);
3128 return 0;
3129}
3130
3131static int snd_hdspm_put_dolby(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_value *ucontrol)
3133{
3134 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3135 int change;
3136 unsigned int val;
3137
3138 if (!snd_hdspm_use_is_exclusive(hdspm))
3139 return -EBUSY;
3140 val = ucontrol->value.integer.value[0] & 1;
3141 spin_lock_irq(&hdspm->lock);
3142 change = (int) val != hdspm_dolby(hdspm);
3143 hdspm_set_dolby(hdspm, val);
3144 spin_unlock_irq(&hdspm->lock);
3145 return change;
3146}
3147
Adrian Knoth0dca1792011-01-26 19:32:14 +01003148
Remy Bruno3cee5a62006-10-16 12:46:32 +02003149#define HDSPM_PROFESSIONAL(xname, xindex) \
3150{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3151 .name = xname, \
3152 .index = xindex, \
3153 .info = snd_hdspm_info_professional, \
3154 .get = snd_hdspm_get_professional, \
3155 .put = snd_hdspm_put_professional \
3156}
3157
3158static int hdspm_professional(struct hdspm * hdspm)
3159{
3160 return (hdspm->control_register & HDSPM_Professional) ? 1 : 0;
3161}
3162
3163static int hdspm_set_professional(struct hdspm * hdspm, int dol)
3164{
3165 if (dol)
3166 hdspm->control_register |= HDSPM_Professional;
3167 else
3168 hdspm->control_register &= ~HDSPM_Professional;
3169 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3170
3171 return 0;
3172}
3173
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003174#define snd_hdspm_info_professional snd_ctl_boolean_mono_info
Remy Bruno3cee5a62006-10-16 12:46:32 +02003175
3176static int snd_hdspm_get_professional(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
3178{
3179 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3180
3181 spin_lock_irq(&hdspm->lock);
3182 ucontrol->value.enumerated.item[0] = hdspm_professional(hdspm);
3183 spin_unlock_irq(&hdspm->lock);
3184 return 0;
3185}
3186
3187static int snd_hdspm_put_professional(struct snd_kcontrol *kcontrol,
3188 struct snd_ctl_elem_value *ucontrol)
3189{
3190 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3191 int change;
3192 unsigned int val;
3193
3194 if (!snd_hdspm_use_is_exclusive(hdspm))
3195 return -EBUSY;
3196 val = ucontrol->value.integer.value[0] & 1;
3197 spin_lock_irq(&hdspm->lock);
3198 change = (int) val != hdspm_professional(hdspm);
3199 hdspm_set_professional(hdspm, val);
3200 spin_unlock_irq(&hdspm->lock);
3201 return change;
3202}
3203
Takashi Iwai763f3562005-06-03 11:25:34 +02003204#define HDSPM_INPUT_SELECT(xname, xindex) \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003205{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Takashi Iwai763f3562005-06-03 11:25:34 +02003206 .name = xname, \
3207 .index = xindex, \
3208 .info = snd_hdspm_info_input_select, \
3209 .get = snd_hdspm_get_input_select, \
3210 .put = snd_hdspm_put_input_select \
3211}
3212
Takashi Iwai98274f02005-11-17 14:52:34 +01003213static int hdspm_input_select(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003214{
3215 return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
3216}
3217
Takashi Iwai98274f02005-11-17 14:52:34 +01003218static int hdspm_set_input_select(struct hdspm * hdspm, int out)
Takashi Iwai763f3562005-06-03 11:25:34 +02003219{
3220 if (out)
3221 hdspm->control_register |= HDSPM_InputSelect0;
3222 else
3223 hdspm->control_register &= ~HDSPM_InputSelect0;
3224 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3225
3226 return 0;
3227}
3228
Takashi Iwai98274f02005-11-17 14:52:34 +01003229static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003231{
3232 static char *texts[] = { "optical", "coaxial" };
3233
3234 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3235 uinfo->count = 1;
3236 uinfo->value.enumerated.items = 2;
3237
3238 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3239 uinfo->value.enumerated.item =
3240 uinfo->value.enumerated.items - 1;
3241 strcpy(uinfo->value.enumerated.name,
3242 texts[uinfo->value.enumerated.item]);
3243
3244 return 0;
3245}
3246
Takashi Iwai98274f02005-11-17 14:52:34 +01003247static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
3248 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003249{
Takashi Iwai98274f02005-11-17 14:52:34 +01003250 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003251
3252 spin_lock_irq(&hdspm->lock);
3253 ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
3254 spin_unlock_irq(&hdspm->lock);
3255 return 0;
3256}
3257
Takashi Iwai98274f02005-11-17 14:52:34 +01003258static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
3259 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003260{
Takashi Iwai98274f02005-11-17 14:52:34 +01003261 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003262 int change;
3263 unsigned int val;
3264
3265 if (!snd_hdspm_use_is_exclusive(hdspm))
3266 return -EBUSY;
3267 val = ucontrol->value.integer.value[0] & 1;
3268 spin_lock_irq(&hdspm->lock);
3269 change = (int) val != hdspm_input_select(hdspm);
3270 hdspm_set_input_select(hdspm, val);
3271 spin_unlock_irq(&hdspm->lock);
3272 return change;
3273}
3274
Adrian Knoth0dca1792011-01-26 19:32:14 +01003275
Remy Bruno3cee5a62006-10-16 12:46:32 +02003276#define HDSPM_DS_WIRE(xname, xindex) \
3277{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3278 .name = xname, \
3279 .index = xindex, \
3280 .info = snd_hdspm_info_ds_wire, \
3281 .get = snd_hdspm_get_ds_wire, \
3282 .put = snd_hdspm_put_ds_wire \
3283}
3284
3285static int hdspm_ds_wire(struct hdspm * hdspm)
3286{
3287 return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
3288}
3289
3290static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
3291{
3292 if (ds)
3293 hdspm->control_register |= HDSPM_DS_DoubleWire;
3294 else
3295 hdspm->control_register &= ~HDSPM_DS_DoubleWire;
3296 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3297
3298 return 0;
3299}
3300
3301static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_info *uinfo)
3303{
3304 static char *texts[] = { "Single", "Double" };
3305
3306 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3307 uinfo->count = 1;
3308 uinfo->value.enumerated.items = 2;
3309
3310 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3311 uinfo->value.enumerated.item =
3312 uinfo->value.enumerated.items - 1;
3313 strcpy(uinfo->value.enumerated.name,
3314 texts[uinfo->value.enumerated.item]);
3315
3316 return 0;
3317}
3318
3319static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
3320 struct snd_ctl_elem_value *ucontrol)
3321{
3322 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3323
3324 spin_lock_irq(&hdspm->lock);
3325 ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
3326 spin_unlock_irq(&hdspm->lock);
3327 return 0;
3328}
3329
3330static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
3333 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3334 int change;
3335 unsigned int val;
3336
3337 if (!snd_hdspm_use_is_exclusive(hdspm))
3338 return -EBUSY;
3339 val = ucontrol->value.integer.value[0] & 1;
3340 spin_lock_irq(&hdspm->lock);
3341 change = (int) val != hdspm_ds_wire(hdspm);
3342 hdspm_set_ds_wire(hdspm, val);
3343 spin_unlock_irq(&hdspm->lock);
3344 return change;
3345}
3346
Adrian Knoth0dca1792011-01-26 19:32:14 +01003347
Remy Bruno3cee5a62006-10-16 12:46:32 +02003348#define HDSPM_QS_WIRE(xname, xindex) \
3349{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3350 .name = xname, \
3351 .index = xindex, \
3352 .info = snd_hdspm_info_qs_wire, \
3353 .get = snd_hdspm_get_qs_wire, \
3354 .put = snd_hdspm_put_qs_wire \
3355}
3356
3357static int hdspm_qs_wire(struct hdspm * hdspm)
3358{
3359 if (hdspm->control_register & HDSPM_QS_DoubleWire)
3360 return 1;
3361 if (hdspm->control_register & HDSPM_QS_QuadWire)
3362 return 2;
3363 return 0;
3364}
3365
3366static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
3367{
3368 hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
3369 switch (mode) {
3370 case 0:
3371 break;
3372 case 1:
3373 hdspm->control_register |= HDSPM_QS_DoubleWire;
3374 break;
3375 case 2:
3376 hdspm->control_register |= HDSPM_QS_QuadWire;
3377 break;
3378 }
3379 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3380
3381 return 0;
3382}
3383
3384static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_info *uinfo)
3386{
3387 static char *texts[] = { "Single", "Double", "Quad" };
3388
3389 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3390 uinfo->count = 1;
3391 uinfo->value.enumerated.items = 3;
3392
3393 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3394 uinfo->value.enumerated.item =
3395 uinfo->value.enumerated.items - 1;
3396 strcpy(uinfo->value.enumerated.name,
3397 texts[uinfo->value.enumerated.item]);
3398
3399 return 0;
3400}
3401
3402static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3403 struct snd_ctl_elem_value *ucontrol)
3404{
3405 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3406
3407 spin_lock_irq(&hdspm->lock);
3408 ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
3409 spin_unlock_irq(&hdspm->lock);
3410 return 0;
3411}
3412
3413static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_value *ucontrol)
3415{
3416 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3417 int change;
3418 int val;
3419
3420 if (!snd_hdspm_use_is_exclusive(hdspm))
3421 return -EBUSY;
3422 val = ucontrol->value.integer.value[0];
3423 if (val < 0)
3424 val = 0;
3425 if (val > 2)
3426 val = 2;
3427 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003428 change = val != hdspm_qs_wire(hdspm);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003429 hdspm_set_qs_wire(hdspm, val);
3430 spin_unlock_irq(&hdspm->lock);
3431 return change;
3432}
3433
Adrian Knoth700d1ef2011-07-29 03:11:02 +02003434#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
3435{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3436 .name = xname, \
3437 .index = xindex, \
3438 .info = snd_hdspm_info_madi_speedmode, \
3439 .get = snd_hdspm_get_madi_speedmode, \
3440 .put = snd_hdspm_put_madi_speedmode \
3441}
3442
3443static int hdspm_madi_speedmode(struct hdspm *hdspm)
3444{
3445 if (hdspm->control_register & HDSPM_QuadSpeed)
3446 return 2;
3447 if (hdspm->control_register & HDSPM_DoubleSpeed)
3448 return 1;
3449 return 0;
3450}
3451
3452static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
3453{
3454 hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
3455 switch (mode) {
3456 case 0:
3457 break;
3458 case 1:
3459 hdspm->control_register |= HDSPM_DoubleSpeed;
3460 break;
3461 case 2:
3462 hdspm->control_register |= HDSPM_QuadSpeed;
3463 break;
3464 }
3465 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
3466
3467 return 0;
3468}
3469
3470static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
3471 struct snd_ctl_elem_info *uinfo)
3472{
3473 static char *texts[] = { "Single", "Double", "Quad" };
3474
3475 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3476 uinfo->count = 1;
3477 uinfo->value.enumerated.items = 3;
3478
3479 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3480 uinfo->value.enumerated.item =
3481 uinfo->value.enumerated.items - 1;
3482 strcpy(uinfo->value.enumerated.name,
3483 texts[uinfo->value.enumerated.item]);
3484
3485 return 0;
3486}
3487
3488static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
3489 struct snd_ctl_elem_value *ucontrol)
3490{
3491 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3492
3493 spin_lock_irq(&hdspm->lock);
3494 ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
3495 spin_unlock_irq(&hdspm->lock);
3496 return 0;
3497}
3498
3499static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
3500 struct snd_ctl_elem_value *ucontrol)
3501{
3502 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3503 int change;
3504 int val;
3505
3506 if (!snd_hdspm_use_is_exclusive(hdspm))
3507 return -EBUSY;
3508 val = ucontrol->value.integer.value[0];
3509 if (val < 0)
3510 val = 0;
3511 if (val > 2)
3512 val = 2;
3513 spin_lock_irq(&hdspm->lock);
3514 change = val != hdspm_madi_speedmode(hdspm);
3515 hdspm_set_madi_speedmode(hdspm, val);
3516 spin_unlock_irq(&hdspm->lock);
3517 return change;
3518}
Takashi Iwai763f3562005-06-03 11:25:34 +02003519
3520#define HDSPM_MIXER(xname, xindex) \
3521{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
3522 .name = xname, \
3523 .index = xindex, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003524 .device = 0, \
Takashi Iwai763f3562005-06-03 11:25:34 +02003525 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3526 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3527 .info = snd_hdspm_info_mixer, \
3528 .get = snd_hdspm_get_mixer, \
3529 .put = snd_hdspm_put_mixer \
3530}
3531
Takashi Iwai98274f02005-11-17 14:52:34 +01003532static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
3533 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003534{
3535 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3536 uinfo->count = 3;
3537 uinfo->value.integer.min = 0;
3538 uinfo->value.integer.max = 65535;
3539 uinfo->value.integer.step = 1;
3540 return 0;
3541}
3542
Takashi Iwai98274f02005-11-17 14:52:34 +01003543static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
3544 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003545{
Takashi Iwai98274f02005-11-17 14:52:34 +01003546 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003547 int source;
3548 int destination;
3549
3550 source = ucontrol->value.integer.value[0];
3551 if (source < 0)
3552 source = 0;
3553 else if (source >= 2 * HDSPM_MAX_CHANNELS)
3554 source = 2 * HDSPM_MAX_CHANNELS - 1;
3555
3556 destination = ucontrol->value.integer.value[1];
3557 if (destination < 0)
3558 destination = 0;
3559 else if (destination >= HDSPM_MAX_CHANNELS)
3560 destination = HDSPM_MAX_CHANNELS - 1;
3561
3562 spin_lock_irq(&hdspm->lock);
3563 if (source >= HDSPM_MAX_CHANNELS)
3564 ucontrol->value.integer.value[2] =
3565 hdspm_read_pb_gain(hdspm, destination,
3566 source - HDSPM_MAX_CHANNELS);
3567 else
3568 ucontrol->value.integer.value[2] =
3569 hdspm_read_in_gain(hdspm, destination, source);
3570
3571 spin_unlock_irq(&hdspm->lock);
3572
3573 return 0;
3574}
3575
Takashi Iwai98274f02005-11-17 14:52:34 +01003576static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
3577 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003578{
Takashi Iwai98274f02005-11-17 14:52:34 +01003579 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003580 int change;
3581 int source;
3582 int destination;
3583 int gain;
3584
3585 if (!snd_hdspm_use_is_exclusive(hdspm))
3586 return -EBUSY;
3587
3588 source = ucontrol->value.integer.value[0];
3589 destination = ucontrol->value.integer.value[1];
3590
3591 if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
3592 return -1;
3593 if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
3594 return -1;
3595
3596 gain = ucontrol->value.integer.value[2];
3597
3598 spin_lock_irq(&hdspm->lock);
3599
3600 if (source >= HDSPM_MAX_CHANNELS)
3601 change = gain != hdspm_read_pb_gain(hdspm, destination,
3602 source -
3603 HDSPM_MAX_CHANNELS);
3604 else
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02003605 change = gain != hdspm_read_in_gain(hdspm, destination,
3606 source);
Takashi Iwai763f3562005-06-03 11:25:34 +02003607
3608 if (change) {
3609 if (source >= HDSPM_MAX_CHANNELS)
3610 hdspm_write_pb_gain(hdspm, destination,
3611 source - HDSPM_MAX_CHANNELS,
3612 gain);
3613 else
3614 hdspm_write_in_gain(hdspm, destination, source,
3615 gain);
3616 }
3617 spin_unlock_irq(&hdspm->lock);
3618
3619 return change;
3620}
3621
3622/* The simple mixer control(s) provide gain control for the
3623 basic 1:1 mappings of playback streams to output
Adrian Knoth0dca1792011-01-26 19:32:14 +01003624 streams.
Takashi Iwai763f3562005-06-03 11:25:34 +02003625*/
3626
3627#define HDSPM_PLAYBACK_MIXER \
3628{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3629 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
3630 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3631 .info = snd_hdspm_info_playback_mixer, \
3632 .get = snd_hdspm_get_playback_mixer, \
3633 .put = snd_hdspm_put_playback_mixer \
3634}
3635
Takashi Iwai98274f02005-11-17 14:52:34 +01003636static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
3637 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003638{
3639 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3640 uinfo->count = 1;
3641 uinfo->value.integer.min = 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003642 uinfo->value.integer.max = 64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003643 uinfo->value.integer.step = 1;
3644 return 0;
3645}
3646
Takashi Iwai98274f02005-11-17 14:52:34 +01003647static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
3648 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003649{
Takashi Iwai98274f02005-11-17 14:52:34 +01003650 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003651 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003652
3653 channel = ucontrol->id.index - 1;
3654
Takashi Iwaida3cec32008-08-08 17:12:14 +02003655 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3656 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003657
Takashi Iwai763f3562005-06-03 11:25:34 +02003658 spin_lock_irq(&hdspm->lock);
3659 ucontrol->value.integer.value[0] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003660 (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
Takashi Iwai763f3562005-06-03 11:25:34 +02003661 spin_unlock_irq(&hdspm->lock);
3662
Takashi Iwai763f3562005-06-03 11:25:34 +02003663 return 0;
3664}
3665
Takashi Iwai98274f02005-11-17 14:52:34 +01003666static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
3667 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02003668{
Takashi Iwai98274f02005-11-17 14:52:34 +01003669 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02003670 int change;
3671 int channel;
Takashi Iwai763f3562005-06-03 11:25:34 +02003672 int gain;
3673
3674 if (!snd_hdspm_use_is_exclusive(hdspm))
3675 return -EBUSY;
3676
3677 channel = ucontrol->id.index - 1;
3678
Takashi Iwaida3cec32008-08-08 17:12:14 +02003679 if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
3680 return -EINVAL;
Takashi Iwai763f3562005-06-03 11:25:34 +02003681
Adrian Knoth0dca1792011-01-26 19:32:14 +01003682 gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
Takashi Iwai763f3562005-06-03 11:25:34 +02003683
3684 spin_lock_irq(&hdspm->lock);
3685 change =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003686 gain != hdspm_read_pb_gain(hdspm, channel,
3687 channel);
Takashi Iwai763f3562005-06-03 11:25:34 +02003688 if (change)
Adrian Knoth0dca1792011-01-26 19:32:14 +01003689 hdspm_write_pb_gain(hdspm, channel, channel,
Takashi Iwai763f3562005-06-03 11:25:34 +02003690 gain);
3691 spin_unlock_irq(&hdspm->lock);
3692 return change;
3693}
3694
Adrian Knoth0dca1792011-01-26 19:32:14 +01003695#define HDSPM_SYNC_CHECK(xname, xindex) \
3696{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3697 .name = xname, \
3698 .private_value = xindex, \
3699 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
3700 .info = snd_hdspm_info_sync_check, \
3701 .get = snd_hdspm_get_sync_check \
Takashi Iwai763f3562005-06-03 11:25:34 +02003702}
3703
Adrian Knoth0dca1792011-01-26 19:32:14 +01003704
Takashi Iwai98274f02005-11-17 14:52:34 +01003705static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
3706 struct snd_ctl_elem_info *uinfo)
Takashi Iwai763f3562005-06-03 11:25:34 +02003707{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003708 static char *texts[] = { "No Lock", "Lock", "Sync", "N/A" };
Takashi Iwai763f3562005-06-03 11:25:34 +02003709 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3710 uinfo->count = 1;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003711 uinfo->value.enumerated.items = 4;
Takashi Iwai763f3562005-06-03 11:25:34 +02003712 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
3713 uinfo->value.enumerated.item =
Adrian Knoth0dca1792011-01-26 19:32:14 +01003714 uinfo->value.enumerated.items - 1;
Takashi Iwai763f3562005-06-03 11:25:34 +02003715 strcpy(uinfo->value.enumerated.name,
Adrian Knoth0dca1792011-01-26 19:32:14 +01003716 texts[uinfo->value.enumerated.item]);
Takashi Iwai763f3562005-06-03 11:25:34 +02003717 return 0;
3718}
3719
Adrian Knoth0dca1792011-01-26 19:32:14 +01003720static int hdspm_wc_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003721{
Adrian Knoth0dca1792011-01-26 19:32:14 +01003722 int status, status2;
3723
3724 switch (hdspm->io_type) {
3725 case AES32:
3726 status = hdspm_read(hdspm, HDSPM_statusRegister);
3727 if (status & HDSPM_wcSync)
Takashi Iwai763f3562005-06-03 11:25:34 +02003728 return 2;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003729 else if (status & HDSPM_wcLock)
3730 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02003731 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003732 break;
3733
3734 case MADI:
3735 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02003736 if (status2 & HDSPM_wcLock) {
3737 if (status2 & HDSPM_wcSync)
3738 return 2;
3739 else
3740 return 1;
3741 }
3742 return 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003743 break;
3744
3745 case RayDAT:
3746 case AIO:
3747 status = hdspm_read(hdspm, HDSPM_statusRegister);
3748
3749 if (status & 0x2000000)
3750 return 2;
3751 else if (status & 0x1000000)
3752 return 1;
3753 return 0;
3754
3755 break;
3756
3757 case MADIface:
3758 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02003759 }
Takashi Iwai763f3562005-06-03 11:25:34 +02003760
Takashi Iwai763f3562005-06-03 11:25:34 +02003761
Adrian Knoth0dca1792011-01-26 19:32:14 +01003762 return 3;
Takashi Iwai763f3562005-06-03 11:25:34 +02003763}
3764
3765
Adrian Knoth0dca1792011-01-26 19:32:14 +01003766static int hdspm_madi_sync_check(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02003767{
3768 int status = hdspm_read(hdspm, HDSPM_statusRegister);
3769 if (status & HDSPM_madiLock) {
3770 if (status & HDSPM_madiSync)
3771 return 2;
3772 else
3773 return 1;
3774 }
3775 return 0;
3776}
3777
Adrian Knoth0dca1792011-01-26 19:32:14 +01003778
3779static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
3780{
3781 int status, lock, sync;
3782
3783 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3784
3785 lock = (status & (0x1<<idx)) ? 1 : 0;
3786 sync = (status & (0x100<<idx)) ? 1 : 0;
3787
3788 if (lock && sync)
3789 return 2;
3790 else if (lock)
3791 return 1;
3792 return 0;
3793}
3794
3795
3796static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
3797{
3798 int status, lock = 0, sync = 0;
3799
3800 switch (hdspm->io_type) {
3801 case RayDAT:
3802 case AIO:
3803 status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
3804 lock = (status & 0x400) ? 1 : 0;
3805 sync = (status & 0x800) ? 1 : 0;
3806 break;
3807
3808 case MADI:
3809 case AES32:
3810 status = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knotha7edbd52011-02-23 11:43:15 +01003811 lock = (status & HDSPM_syncInLock) ? 1 : 0;
3812 sync = (status & HDSPM_syncInSync) ? 1 : 0;
Adrian Knoth0dca1792011-01-26 19:32:14 +01003813 break;
3814
3815 case MADIface:
3816 break;
3817 }
3818
3819 if (lock && sync)
3820 return 2;
3821 else if (lock)
3822 return 1;
3823
3824 return 0;
3825}
3826
3827static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
3828{
3829 int status2, lock, sync;
3830 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
3831
3832 lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
3833 sync = (status2 & (0x8000 >> idx)) ? 1 : 0;
3834
3835 if (sync)
3836 return 2;
3837 else if (lock)
3838 return 1;
3839 return 0;
3840}
3841
3842
3843static int hdspm_tco_sync_check(struct hdspm *hdspm)
3844{
3845 int status;
3846
3847 if (hdspm->tco) {
3848 switch (hdspm->io_type) {
3849 case MADI:
3850 case AES32:
3851 status = hdspm_read(hdspm, HDSPM_statusRegister);
3852 if (status & HDSPM_tcoLock) {
3853 if (status & HDSPM_tcoSync)
3854 return 2;
3855 else
3856 return 1;
3857 }
3858 return 0;
3859
3860 break;
3861
3862 case RayDAT:
3863 case AIO:
3864 status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
3865
3866 if (status & 0x8000000)
3867 return 2; /* Sync */
3868 if (status & 0x4000000)
3869 return 1; /* Lock */
3870 return 0; /* No signal */
3871 break;
3872
3873 default:
3874 break;
3875 }
3876 }
3877
3878 return 3; /* N/A */
3879}
3880
3881
3882static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
3883 struct snd_ctl_elem_value *ucontrol)
3884{
3885 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
3886 int val = -1;
3887
3888 switch (hdspm->io_type) {
3889 case RayDAT:
3890 switch (kcontrol->private_value) {
3891 case 0: /* WC */
3892 val = hdspm_wc_sync_check(hdspm); break;
3893 case 7: /* TCO */
3894 val = hdspm_tco_sync_check(hdspm); break;
3895 case 8: /* SYNC IN */
3896 val = hdspm_sync_in_sync_check(hdspm); break;
3897 default:
3898 val = hdspm_s1_sync_check(hdspm, ucontrol->id.index-1);
3899 }
3900
3901 case AIO:
3902 switch (kcontrol->private_value) {
3903 case 0: /* WC */
3904 val = hdspm_wc_sync_check(hdspm); break;
3905 case 4: /* TCO */
3906 val = hdspm_tco_sync_check(hdspm); break;
3907 case 5: /* SYNC IN */
3908 val = hdspm_sync_in_sync_check(hdspm); break;
3909 default:
3910 val = hdspm_s1_sync_check(hdspm, ucontrol->id.index-1);
3911 }
3912
3913 case MADI:
3914 switch (kcontrol->private_value) {
3915 case 0: /* WC */
3916 val = hdspm_wc_sync_check(hdspm); break;
3917 case 1: /* MADI */
3918 val = hdspm_madi_sync_check(hdspm); break;
3919 case 2: /* TCO */
3920 val = hdspm_tco_sync_check(hdspm); break;
3921 case 3: /* SYNC_IN */
3922 val = hdspm_sync_in_sync_check(hdspm); break;
3923 }
3924
3925 case MADIface:
3926 val = hdspm_madi_sync_check(hdspm); /* MADI */
3927 break;
3928
3929 case AES32:
3930 switch (kcontrol->private_value) {
3931 case 0: /* WC */
3932 val = hdspm_wc_sync_check(hdspm); break;
3933 case 9: /* TCO */
3934 val = hdspm_tco_sync_check(hdspm); break;
3935 case 10 /* SYNC IN */:
3936 val = hdspm_sync_in_sync_check(hdspm); break;
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003937 default: /* AES1 to AES8 */
Adrian Knoth0dca1792011-01-26 19:32:14 +01003938 val = hdspm_aes_sync_check(hdspm,
Adrian Knoth7c4a95b2011-02-23 11:43:13 +01003939 kcontrol->private_value-1);
Adrian Knoth0dca1792011-01-26 19:32:14 +01003940 }
3941
3942 }
3943
3944 if (-1 == val)
3945 val = 3;
3946
3947 ucontrol->value.enumerated.item[0] = val;
3948 return 0;
3949}
3950
3951
3952
3953/**
3954 * TCO controls
3955 **/
3956static void hdspm_tco_write(struct hdspm *hdspm)
3957{
3958 unsigned int tc[4] = { 0, 0, 0, 0};
3959
3960 switch (hdspm->tco->input) {
3961 case 0:
3962 tc[2] |= HDSPM_TCO2_set_input_MSB;
3963 break;
3964 case 1:
3965 tc[2] |= HDSPM_TCO2_set_input_LSB;
3966 break;
3967 default:
3968 break;
3969 }
3970
3971 switch (hdspm->tco->framerate) {
3972 case 1:
3973 tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
3974 break;
3975 case 2:
3976 tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
3977 break;
3978 case 3:
3979 tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
3980 HDSPM_TCO1_set_drop_frame_flag;
3981 break;
3982 case 4:
3983 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3984 HDSPM_TCO1_LTC_Format_MSB;
3985 break;
3986 case 5:
3987 tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
3988 HDSPM_TCO1_LTC_Format_MSB +
3989 HDSPM_TCO1_set_drop_frame_flag;
3990 break;
3991 default:
3992 break;
3993 }
3994
3995 switch (hdspm->tco->wordclock) {
3996 case 1:
3997 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
3998 break;
3999 case 2:
4000 tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
4001 break;
4002 default:
4003 break;
4004 }
4005
4006 switch (hdspm->tco->samplerate) {
4007 case 1:
4008 tc[2] |= HDSPM_TCO2_set_freq;
4009 break;
4010 case 2:
4011 tc[2] |= HDSPM_TCO2_set_freq_from_app;
4012 break;
4013 default:
4014 break;
4015 }
4016
4017 switch (hdspm->tco->pull) {
4018 case 1:
4019 tc[2] |= HDSPM_TCO2_set_pull_up;
4020 break;
4021 case 2:
4022 tc[2] |= HDSPM_TCO2_set_pull_down;
4023 break;
4024 case 3:
4025 tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
4026 break;
4027 case 4:
4028 tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
4029 break;
4030 default:
4031 break;
4032 }
4033
4034 if (1 == hdspm->tco->term) {
4035 tc[2] |= HDSPM_TCO2_set_term_75R;
4036 }
4037
4038 hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
4039 hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
4040 hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
4041 hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
4042}
4043
4044
4045#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
4046{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4047 .name = xname, \
4048 .index = xindex, \
4049 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4050 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4051 .info = snd_hdspm_info_tco_sample_rate, \
4052 .get = snd_hdspm_get_tco_sample_rate, \
4053 .put = snd_hdspm_put_tco_sample_rate \
4054}
4055
4056static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
4057 struct snd_ctl_elem_info *uinfo)
4058{
4059 static char *texts[] = { "44.1 kHz", "48 kHz" };
4060 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4061 uinfo->count = 1;
4062 uinfo->value.enumerated.items = 2;
4063
4064 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4065 uinfo->value.enumerated.item =
4066 uinfo->value.enumerated.items - 1;
4067
4068 strcpy(uinfo->value.enumerated.name,
4069 texts[uinfo->value.enumerated.item]);
4070
4071 return 0;
4072}
4073
4074static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
4075 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai763f3562005-06-03 11:25:34 +02004076{
Takashi Iwai98274f02005-11-17 14:52:34 +01004077 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
Takashi Iwai763f3562005-06-03 11:25:34 +02004078
Adrian Knoth0dca1792011-01-26 19:32:14 +01004079 ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;
4080
Takashi Iwai763f3562005-06-03 11:25:34 +02004081 return 0;
4082}
4083
Adrian Knoth0dca1792011-01-26 19:32:14 +01004084static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
4085 struct snd_ctl_elem_value *ucontrol)
Remy Bruno3cee5a62006-10-16 12:46:32 +02004086{
Adrian Knoth0dca1792011-01-26 19:32:14 +01004087 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4088
4089 if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
4090 hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];
4091
4092 hdspm_tco_write(hdspm);
4093
4094 return 1;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004095 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01004096
Remy Bruno3cee5a62006-10-16 12:46:32 +02004097 return 0;
4098}
4099
Adrian Knoth0dca1792011-01-26 19:32:14 +01004100
4101#define HDSPM_TCO_PULL(xname, xindex) \
4102{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4103 .name = xname, \
4104 .index = xindex, \
4105 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4106 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4107 .info = snd_hdspm_info_tco_pull, \
4108 .get = snd_hdspm_get_tco_pull, \
4109 .put = snd_hdspm_put_tco_pull \
4110}
4111
4112static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
4113 struct snd_ctl_elem_info *uinfo)
4114{
4115 static char *texts[] = { "0", "+ 0.1 %", "- 0.1 %", "+ 4 %", "- 4 %" };
4116 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4117 uinfo->count = 1;
4118 uinfo->value.enumerated.items = 5;
4119
4120 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4121 uinfo->value.enumerated.item =
4122 uinfo->value.enumerated.items - 1;
4123
4124 strcpy(uinfo->value.enumerated.name,
4125 texts[uinfo->value.enumerated.item]);
4126
4127 return 0;
4128}
4129
4130static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
4131 struct snd_ctl_elem_value *ucontrol)
4132{
4133 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4134
4135 ucontrol->value.enumerated.item[0] = hdspm->tco->pull;
4136
4137 return 0;
4138}
4139
4140static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
4141 struct snd_ctl_elem_value *ucontrol)
4142{
4143 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4144
4145 if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
4146 hdspm->tco->pull = ucontrol->value.enumerated.item[0];
4147
4148 hdspm_tco_write(hdspm);
4149
4150 return 1;
4151 }
4152
4153 return 0;
4154}
4155
4156#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
4157{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4158 .name = xname, \
4159 .index = xindex, \
4160 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4161 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4162 .info = snd_hdspm_info_tco_wck_conversion, \
4163 .get = snd_hdspm_get_tco_wck_conversion, \
4164 .put = snd_hdspm_put_tco_wck_conversion \
4165}
4166
4167static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4168 struct snd_ctl_elem_info *uinfo)
4169{
4170 static char *texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
4171 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4172 uinfo->count = 1;
4173 uinfo->value.enumerated.items = 3;
4174
4175 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4176 uinfo->value.enumerated.item =
4177 uinfo->value.enumerated.items - 1;
4178
4179 strcpy(uinfo->value.enumerated.name,
4180 texts[uinfo->value.enumerated.item]);
4181
4182 return 0;
4183}
4184
4185static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4186 struct snd_ctl_elem_value *ucontrol)
4187{
4188 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4189
4190 ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;
4191
4192 return 0;
4193}
4194
4195static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
4196 struct snd_ctl_elem_value *ucontrol)
4197{
4198 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4199
4200 if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
4201 hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];
4202
4203 hdspm_tco_write(hdspm);
4204
4205 return 1;
4206 }
4207
4208 return 0;
4209}
4210
4211
4212#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
4213{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4214 .name = xname, \
4215 .index = xindex, \
4216 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4217 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4218 .info = snd_hdspm_info_tco_frame_rate, \
4219 .get = snd_hdspm_get_tco_frame_rate, \
4220 .put = snd_hdspm_put_tco_frame_rate \
4221}
4222
4223static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
4224 struct snd_ctl_elem_info *uinfo)
4225{
4226 static char *texts[] = { "24 fps", "25 fps", "29.97fps",
4227 "29.97 dfps", "30 fps", "30 dfps" };
4228 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4229 uinfo->count = 1;
4230 uinfo->value.enumerated.items = 6;
4231
4232 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4233 uinfo->value.enumerated.item =
4234 uinfo->value.enumerated.items - 1;
4235
4236 strcpy(uinfo->value.enumerated.name,
4237 texts[uinfo->value.enumerated.item]);
4238
4239 return 0;
4240}
4241
4242static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
Remy Bruno3cee5a62006-10-16 12:46:32 +02004243 struct snd_ctl_elem_value *ucontrol)
4244{
Remy Bruno3cee5a62006-10-16 12:46:32 +02004245 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4246
Adrian Knoth0dca1792011-01-26 19:32:14 +01004247 ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004248
Remy Bruno3cee5a62006-10-16 12:46:32 +02004249 return 0;
4250}
Takashi Iwai763f3562005-06-03 11:25:34 +02004251
Adrian Knoth0dca1792011-01-26 19:32:14 +01004252static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
4253 struct snd_ctl_elem_value *ucontrol)
4254{
4255 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4256
4257 if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
4258 hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
4259
4260 hdspm_tco_write(hdspm);
4261
4262 return 1;
4263 }
4264
4265 return 0;
4266}
4267
4268
4269#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
4270{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4271 .name = xname, \
4272 .index = xindex, \
4273 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4274 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4275 .info = snd_hdspm_info_tco_sync_source, \
4276 .get = snd_hdspm_get_tco_sync_source, \
4277 .put = snd_hdspm_put_tco_sync_source \
4278}
4279
4280static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
4281 struct snd_ctl_elem_info *uinfo)
4282{
4283 static char *texts[] = { "LTC", "Video", "WCK" };
4284 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4285 uinfo->count = 1;
4286 uinfo->value.enumerated.items = 3;
4287
4288 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4289 uinfo->value.enumerated.item =
4290 uinfo->value.enumerated.items - 1;
4291
4292 strcpy(uinfo->value.enumerated.name,
4293 texts[uinfo->value.enumerated.item]);
4294
4295 return 0;
4296}
4297
4298static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
4299 struct snd_ctl_elem_value *ucontrol)
4300{
4301 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4302
4303 ucontrol->value.enumerated.item[0] = hdspm->tco->input;
4304
4305 return 0;
4306}
4307
4308static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
4309 struct snd_ctl_elem_value *ucontrol)
4310{
4311 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4312
4313 if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
4314 hdspm->tco->input = ucontrol->value.enumerated.item[0];
4315
4316 hdspm_tco_write(hdspm);
4317
4318 return 1;
4319 }
4320
4321 return 0;
4322}
4323
4324
4325#define HDSPM_TCO_WORD_TERM(xname, xindex) \
4326{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4327 .name = xname, \
4328 .index = xindex, \
4329 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
4330 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
4331 .info = snd_hdspm_info_tco_word_term, \
4332 .get = snd_hdspm_get_tco_word_term, \
4333 .put = snd_hdspm_put_tco_word_term \
4334}
4335
4336static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
4337 struct snd_ctl_elem_info *uinfo)
4338{
4339 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
4340 uinfo->count = 1;
4341 uinfo->value.integer.min = 0;
4342 uinfo->value.integer.max = 1;
4343
4344 return 0;
4345}
4346
4347
4348static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
4349 struct snd_ctl_elem_value *ucontrol)
4350{
4351 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4352
4353 ucontrol->value.enumerated.item[0] = hdspm->tco->term;
4354
4355 return 0;
4356}
4357
4358
4359static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
4360 struct snd_ctl_elem_value *ucontrol)
4361{
4362 struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
4363
4364 if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
4365 hdspm->tco->term = ucontrol->value.enumerated.item[0];
4366
4367 hdspm_tco_write(hdspm);
4368
4369 return 1;
4370 }
4371
4372 return 0;
4373}
4374
4375
4376
Takashi Iwai763f3562005-06-03 11:25:34 +02004377
Remy Bruno3cee5a62006-10-16 12:46:32 +02004378static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
Takashi Iwai763f3562005-06-03 11:25:34 +02004379 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004380 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Takashi Iwai763f3562005-06-03 11:25:34 +02004381 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4382 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4383 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4384 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004385 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4386 HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
4387 HDSPM_SYNC_CHECK("TCO SyncCHeck", 2),
4388 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
Takashi Iwai763f3562005-06-03 11:25:34 +02004389 HDSPM_LINE_OUT("Line Out", 0),
4390 HDSPM_TX_64("TX 64 channels mode", 0),
4391 HDSPM_C_TMS("Clear Track Marker", 0),
4392 HDSPM_SAFE_MODE("Safe Mode", 0),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004393 HDSPM_INPUT_SELECT("Input Select", 0),
4394 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Adrian Knoth0dca1792011-01-26 19:32:14 +01004395};
4396
4397
4398static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
4399 HDSPM_MIXER("Mixer", 0),
4400 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4401 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4402 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4403 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4404 HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
4405 HDSPM_TX_64("TX 64 channels mode", 0),
4406 HDSPM_C_TMS("Clear Track Marker", 0),
Adrian Knoth700d1ef2011-07-29 03:11:02 +02004407 HDSPM_SAFE_MODE("Safe Mode", 0),
4408 HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004409};
4410
Adrian Knoth0dca1792011-01-26 19:32:14 +01004411static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004412 HDSPM_MIXER("Mixer", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004413 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004414 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4415 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4416 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4417 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004418 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
Adrian Knoth0dca1792011-01-26 19:32:14 +01004419 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4420 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4421 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4422 HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
4423 HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
4424 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
4425 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4426 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4427 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4428 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
4429 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
4430 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5)
4431
4432 /*
4433 HDSPM_INPUT_SELECT("Input Select", 0),
4434 HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
4435 HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
4436 HDSPM_SPDIF_IN("SPDIF In", 0);
4437 HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
4438 HDSPM_INPUT_LEVEL("Input Level", 0);
4439 HDSPM_OUTPUT_LEVEL("Output Level", 0);
4440 HDSPM_PHONES("Phones", 0);
4441 */
4442};
4443
4444static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
4445 HDSPM_MIXER("Mixer", 0),
4446 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4447 HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
4448 HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
4449 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4450 HDSPM_SYNC_CHECK("WC SyncCheck", 0),
4451 HDSPM_SYNC_CHECK("AES SyncCheck", 1),
4452 HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
4453 HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
4454 HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
4455 HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
4456 HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
4457 HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
4458 HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
4459 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4460 HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
4461 HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
4462 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
4463 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
4464 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
4465 HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
4466 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
4467 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8)
4468};
4469
4470static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
4471 HDSPM_MIXER("Mixer", 0),
4472 HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
4473 HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
4474 HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
4475 HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
4476 HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4477 HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4478 HDSPM_SYNC_CHECK("WC Sync Check", 0),
4479 HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
4480 HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
4481 HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
4482 HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
4483 HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
4484 HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
4485 HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
4486 HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
4487 HDSPM_SYNC_CHECK("TCO Sync Check", 9),
4488 HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
4489 HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
4490 HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
4491 HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
4492 HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
4493 HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
4494 HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
4495 HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
4496 HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
4497 HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
4498 HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
4499 HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004500 HDSPM_LINE_OUT("Line Out", 0),
4501 HDSPM_EMPHASIS("Emphasis", 0),
4502 HDSPM_DOLBY("Non Audio", 0),
4503 HDSPM_PROFESSIONAL("Professional", 0),
4504 HDSPM_C_TMS("Clear Track Marker", 0),
4505 HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
4506 HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
4507};
4508
Adrian Knoth0dca1792011-01-26 19:32:14 +01004509
4510
4511/* Control elements for the optional TCO module */
4512static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
4513 HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
4514 HDSPM_TCO_PULL("TCO Pull", 0),
4515 HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
4516 HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
4517 HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
4518 HDSPM_TCO_WORD_TERM("TCO Word Term", 0)
4519};
4520
4521
Takashi Iwai98274f02005-11-17 14:52:34 +01004522static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
Takashi Iwai763f3562005-06-03 11:25:34 +02004523
4524
Takashi Iwai98274f02005-11-17 14:52:34 +01004525static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004526{
4527 int i;
4528
Adrian Knoth0dca1792011-01-26 19:32:14 +01004529 for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
Takashi Iwai763f3562005-06-03 11:25:34 +02004530 if (hdspm->system_sample_rate > 48000) {
4531 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004532 SNDRV_CTL_ELEM_ACCESS_INACTIVE |
4533 SNDRV_CTL_ELEM_ACCESS_READ |
4534 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004535 } else {
4536 hdspm->playback_mixer_ctls[i]->vd[0].access =
Adrian Knoth0dca1792011-01-26 19:32:14 +01004537 SNDRV_CTL_ELEM_ACCESS_READWRITE |
4538 SNDRV_CTL_ELEM_ACCESS_VOLATILE;
Takashi Iwai763f3562005-06-03 11:25:34 +02004539 }
4540 snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
Adrian Knoth0dca1792011-01-26 19:32:14 +01004541 SNDRV_CTL_EVENT_MASK_INFO,
4542 &hdspm->playback_mixer_ctls[i]->id);
Takashi Iwai763f3562005-06-03 11:25:34 +02004543 }
4544
4545 return 0;
4546}
4547
4548
Adrian Knoth0dca1792011-01-26 19:32:14 +01004549static int snd_hdspm_create_controls(struct snd_card *card,
4550 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02004551{
4552 unsigned int idx, limit;
4553 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01004554 struct snd_kcontrol *kctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004555 struct snd_kcontrol_new *list = NULL;
Takashi Iwai763f3562005-06-03 11:25:34 +02004556
Adrian Knoth0dca1792011-01-26 19:32:14 +01004557 switch (hdspm->io_type) {
4558 case MADI:
4559 list = snd_hdspm_controls_madi;
4560 limit = ARRAY_SIZE(snd_hdspm_controls_madi);
4561 break;
4562 case MADIface:
4563 list = snd_hdspm_controls_madiface;
4564 limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
4565 break;
4566 case AIO:
4567 list = snd_hdspm_controls_aio;
4568 limit = ARRAY_SIZE(snd_hdspm_controls_aio);
4569 break;
4570 case RayDAT:
4571 list = snd_hdspm_controls_raydat;
4572 limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
4573 break;
4574 case AES32:
4575 list = snd_hdspm_controls_aes32;
4576 limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
4577 break;
4578 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004579
Adrian Knoth0dca1792011-01-26 19:32:14 +01004580 if (NULL != list) {
4581 for (idx = 0; idx < limit; idx++) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02004582 err = snd_ctl_add(card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004583 snd_ctl_new1(&list[idx], hdspm));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004584 if (err < 0)
4585 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004586 }
4587 }
4588
Takashi Iwai763f3562005-06-03 11:25:34 +02004589
Adrian Knoth0dca1792011-01-26 19:32:14 +01004590 /* create simple 1:1 playback mixer controls */
Takashi Iwai763f3562005-06-03 11:25:34 +02004591 snd_hdspm_playback_mixer.name = "Chn";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004592 if (hdspm->system_sample_rate >= 128000) {
4593 limit = hdspm->qs_out_channels;
4594 } else if (hdspm->system_sample_rate >= 64000) {
4595 limit = hdspm->ds_out_channels;
4596 } else {
4597 limit = hdspm->ss_out_channels;
4598 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004599 for (idx = 0; idx < limit; ++idx) {
4600 snd_hdspm_playback_mixer.index = idx + 1;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004601 kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
4602 err = snd_ctl_add(card, kctl);
4603 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02004604 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02004605 hdspm->playback_mixer_ctls[idx] = kctl;
4606 }
4607
Adrian Knoth0dca1792011-01-26 19:32:14 +01004608
4609 if (hdspm->tco) {
4610 /* add tco control elements */
4611 list = snd_hdspm_controls_tco;
4612 limit = ARRAY_SIZE(snd_hdspm_controls_tco);
4613 for (idx = 0; idx < limit; idx++) {
4614 err = snd_ctl_add(card,
4615 snd_ctl_new1(&list[idx], hdspm));
4616 if (err < 0)
4617 return err;
4618 }
4619 }
4620
Takashi Iwai763f3562005-06-03 11:25:34 +02004621 return 0;
4622}
4623
4624/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01004625 /proc interface
Takashi Iwai763f3562005-06-03 11:25:34 +02004626 ------------------------------------------------------------*/
4627
4628static void
Remy Bruno3cee5a62006-10-16 12:46:32 +02004629snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
4630 struct snd_info_buffer *buffer)
Takashi Iwai763f3562005-06-03 11:25:34 +02004631{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004632 struct hdspm *hdspm = entry->private_data;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004633 unsigned int status, status2, control, freq;
4634
Takashi Iwai763f3562005-06-03 11:25:34 +02004635 char *pref_sync_ref;
4636 char *autosync_ref;
4637 char *system_clock_mode;
Takashi Iwai763f3562005-06-03 11:25:34 +02004638 char *insel;
Takashi Iwai763f3562005-06-03 11:25:34 +02004639 int x, x2;
4640
Adrian Knoth0dca1792011-01-26 19:32:14 +01004641 /* TCO stuff */
4642 int a, ltc, frames, seconds, minutes, hours;
4643 unsigned int period;
4644 u64 freq_const = 0;
4645 u32 rate;
4646
Takashi Iwai763f3562005-06-03 11:25:34 +02004647 status = hdspm_read(hdspm, HDSPM_statusRegister);
4648 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01004649 control = hdspm->control_register;
4650 freq = hdspm_read(hdspm, HDSPM_timecodeRegister);
Takashi Iwai763f3562005-06-03 11:25:34 +02004651
4652 snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004653 hdspm->card_name, hdspm->card->number + 1,
4654 hdspm->firmware_rev,
4655 (status2 & HDSPM_version0) |
4656 (status2 & HDSPM_version1) | (status2 &
4657 HDSPM_version2));
4658
4659 snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
4660 (hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
4661 (hdspm_read(hdspm, HDSPM_midiStatusIn0)>>8) & 0xFFFFFF);
Takashi Iwai763f3562005-06-03 11:25:34 +02004662
4663 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004664 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
Takashi Iwai763f3562005-06-03 11:25:34 +02004665
4666 snd_iprintf(buffer, "--- System ---\n");
4667
4668 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004669 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4670 status & HDSPM_audioIRQPending,
4671 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4672 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4673 hdspm->irq_count);
Takashi Iwai763f3562005-06-03 11:25:34 +02004674 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004675 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4676 "estimated= %ld (bytes)\n",
4677 ((status & HDSPM_BufferID) ? 1 : 0),
4678 (status & HDSPM_BufferPositionMask),
4679 (status & HDSPM_BufferPositionMask) %
4680 (2 * (int)hdspm->period_bytes),
4681 ((status & HDSPM_BufferPositionMask) - 64) %
4682 (2 * (int)hdspm->period_bytes),
4683 (long) hdspm_hw_pointer(hdspm) * 4);
Takashi Iwai763f3562005-06-03 11:25:34 +02004684
4685 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004686 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4687 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4688 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4689 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4690 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
Takashi Iwai763f3562005-06-03 11:25:34 +02004691 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004692 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4693 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4694 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4695 snd_iprintf(buffer,
4696 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4697 "status2=0x%x\n",
4698 hdspm->control_register, hdspm->control2_register,
4699 status, status2);
4700 if (status & HDSPM_tco_detect) {
4701 snd_iprintf(buffer, "TCO module detected.\n");
4702 a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
4703 if (a & HDSPM_TCO1_LTC_Input_valid) {
4704 snd_iprintf(buffer, " LTC valid, ");
4705 switch (a & (HDSPM_TCO1_LTC_Format_LSB |
4706 HDSPM_TCO1_LTC_Format_MSB)) {
4707 case 0:
4708 snd_iprintf(buffer, "24 fps, ");
4709 break;
4710 case HDSPM_TCO1_LTC_Format_LSB:
4711 snd_iprintf(buffer, "25 fps, ");
4712 break;
4713 case HDSPM_TCO1_LTC_Format_MSB:
4714 snd_iprintf(buffer, "29.97 fps, ");
4715 break;
4716 default:
4717 snd_iprintf(buffer, "30 fps, ");
4718 break;
4719 }
4720 if (a & HDSPM_TCO1_set_drop_frame_flag) {
4721 snd_iprintf(buffer, "drop frame\n");
4722 } else {
4723 snd_iprintf(buffer, "full frame\n");
4724 }
4725 } else {
4726 snd_iprintf(buffer, " no LTC\n");
4727 }
4728 if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
4729 snd_iprintf(buffer, " Video: NTSC\n");
4730 } else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
4731 snd_iprintf(buffer, " Video: PAL\n");
4732 } else {
4733 snd_iprintf(buffer, " No video\n");
4734 }
4735 if (a & HDSPM_TCO1_TCO_lock) {
4736 snd_iprintf(buffer, " Sync: lock\n");
4737 } else {
4738 snd_iprintf(buffer, " Sync: no lock\n");
4739 }
4740
4741 switch (hdspm->io_type) {
4742 case MADI:
4743 case AES32:
4744 freq_const = 110069313433624ULL;
4745 break;
4746 case RayDAT:
4747 case AIO:
4748 freq_const = 104857600000000ULL;
4749 break;
4750 case MADIface:
4751 break; /* no TCO possible */
4752 }
4753
4754 period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
4755 snd_iprintf(buffer, " period: %u\n", period);
4756
4757
4758 /* rate = freq_const/period; */
4759 rate = div_u64(freq_const, period);
4760
4761 if (control & HDSPM_QuadSpeed) {
4762 rate *= 4;
4763 } else if (control & HDSPM_DoubleSpeed) {
4764 rate *= 2;
4765 }
4766
4767 snd_iprintf(buffer, " Frequency: %u Hz\n",
4768 (unsigned int) rate);
4769
4770 ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
4771 frames = ltc & 0xF;
4772 ltc >>= 4;
4773 frames += (ltc & 0x3) * 10;
4774 ltc >>= 4;
4775 seconds = ltc & 0xF;
4776 ltc >>= 4;
4777 seconds += (ltc & 0x7) * 10;
4778 ltc >>= 4;
4779 minutes = ltc & 0xF;
4780 ltc >>= 4;
4781 minutes += (ltc & 0x7) * 10;
4782 ltc >>= 4;
4783 hours = ltc & 0xF;
4784 ltc >>= 4;
4785 hours += (ltc & 0x3) * 10;
4786 snd_iprintf(buffer,
4787 " LTC In: %02d:%02d:%02d:%02d\n",
4788 hours, minutes, seconds, frames);
4789
4790 } else {
4791 snd_iprintf(buffer, "No TCO module detected.\n");
4792 }
Takashi Iwai763f3562005-06-03 11:25:34 +02004793
4794 snd_iprintf(buffer, "--- Settings ---\n");
4795
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004796 x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
Adrian Knoth0dca1792011-01-26 19:32:14 +01004797 HDSPM_LatencyMask));
Takashi Iwai763f3562005-06-03 11:25:34 +02004798
4799 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004800 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4801 x, (unsigned long) hdspm->period_bytes);
Takashi Iwai763f3562005-06-03 11:25:34 +02004802
Adrian Knoth0dca1792011-01-26 19:32:14 +01004803 snd_iprintf(buffer, "Line out: %s\n",
4804 (hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004805
4806 switch (hdspm->control_register & HDSPM_InputMask) {
4807 case HDSPM_InputOptical:
4808 insel = "Optical";
4809 break;
4810 case HDSPM_InputCoaxial:
4811 insel = "Coaxial";
4812 break;
4813 default:
Adrian Knoth0dca1792011-01-26 19:32:14 +01004814 insel = "Unkown";
Takashi Iwai763f3562005-06-03 11:25:34 +02004815 }
4816
Takashi Iwai763f3562005-06-03 11:25:34 +02004817 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004818 "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
4819 "Auto Input %s\n",
4820 (hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
4821 (hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
4822 (hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");
Takashi Iwai763f3562005-06-03 11:25:34 +02004823
Adrian Knoth0dca1792011-01-26 19:32:14 +01004824
Remy Bruno3cee5a62006-10-16 12:46:32 +02004825 if (!(hdspm->control_register & HDSPM_ClockModeMaster))
Adrian Knoth0dca1792011-01-26 19:32:14 +01004826 system_clock_mode = "AutoSync";
Remy Bruno3cee5a62006-10-16 12:46:32 +02004827 else
Takashi Iwai763f3562005-06-03 11:25:34 +02004828 system_clock_mode = "Master";
Adrian Knoth0dca1792011-01-26 19:32:14 +01004829 snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
Takashi Iwai763f3562005-06-03 11:25:34 +02004830
4831 switch (hdspm_pref_sync_ref(hdspm)) {
4832 case HDSPM_SYNC_FROM_WORD:
4833 pref_sync_ref = "Word Clock";
4834 break;
4835 case HDSPM_SYNC_FROM_MADI:
4836 pref_sync_ref = "MADI Sync";
4837 break;
Adrian Knoth0dca1792011-01-26 19:32:14 +01004838 case HDSPM_SYNC_FROM_TCO:
4839 pref_sync_ref = "TCO";
4840 break;
4841 case HDSPM_SYNC_FROM_SYNC_IN:
4842 pref_sync_ref = "Sync In";
4843 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004844 default:
4845 pref_sync_ref = "XXXX Clock";
4846 break;
4847 }
4848 snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004849 pref_sync_ref);
Takashi Iwai763f3562005-06-03 11:25:34 +02004850
4851 snd_iprintf(buffer, "System Clock Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004852 hdspm->system_sample_rate);
Takashi Iwai763f3562005-06-03 11:25:34 +02004853
4854
4855 snd_iprintf(buffer, "--- Status:\n");
4856
4857 x = status & HDSPM_madiSync;
4858 x2 = status2 & HDSPM_wcSync;
4859
4860 snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004861 (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
4862 "NoLock",
4863 (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
4864 "NoLock");
Takashi Iwai763f3562005-06-03 11:25:34 +02004865
4866 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01004867 case HDSPM_AUTOSYNC_FROM_SYNC_IN:
4868 autosync_ref = "Sync In";
4869 break;
4870 case HDSPM_AUTOSYNC_FROM_TCO:
4871 autosync_ref = "TCO";
4872 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02004873 case HDSPM_AUTOSYNC_FROM_WORD:
4874 autosync_ref = "Word Clock";
4875 break;
4876 case HDSPM_AUTOSYNC_FROM_MADI:
4877 autosync_ref = "MADI Sync";
4878 break;
4879 case HDSPM_AUTOSYNC_FROM_NONE:
4880 autosync_ref = "Input not valid";
4881 break;
4882 default:
4883 autosync_ref = "---";
4884 break;
4885 }
4886 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004887 "AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
4888 autosync_ref, hdspm_external_sample_rate(hdspm),
4889 (status & HDSPM_madiFreqMask) >> 22,
4890 (status2 & HDSPM_wcFreqMask) >> 5);
Takashi Iwai763f3562005-06-03 11:25:34 +02004891
4892 snd_iprintf(buffer, "Input: %s, Mode=%s\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01004893 (status & HDSPM_AB_int) ? "Coax" : "Optical",
4894 (status & HDSPM_RX_64ch) ? "64 channels" :
4895 "56 channels");
Takashi Iwai763f3562005-06-03 11:25:34 +02004896
4897 snd_iprintf(buffer, "\n");
4898}
4899
Remy Bruno3cee5a62006-10-16 12:46:32 +02004900static void
4901snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
4902 struct snd_info_buffer *buffer)
4903{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004904 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004905 unsigned int status;
4906 unsigned int status2;
4907 unsigned int timecode;
4908 int pref_syncref;
4909 char *autosync_ref;
Remy Bruno3cee5a62006-10-16 12:46:32 +02004910 int x;
4911
4912 status = hdspm_read(hdspm, HDSPM_statusRegister);
4913 status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
4914 timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
4915
4916 snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
4917 hdspm->card_name, hdspm->card->number + 1,
4918 hdspm->firmware_rev);
4919
4920 snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
4921 hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);
4922
4923 snd_iprintf(buffer, "--- System ---\n");
4924
4925 snd_iprintf(buffer,
4926 "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
4927 status & HDSPM_audioIRQPending,
4928 (status & HDSPM_midi0IRQPending) ? 1 : 0,
4929 (status & HDSPM_midi1IRQPending) ? 1 : 0,
4930 hdspm->irq_count);
4931 snd_iprintf(buffer,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004932 "HW pointer: id = %d, rawptr = %d (%d->%d) "
4933 "estimated= %ld (bytes)\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004934 ((status & HDSPM_BufferID) ? 1 : 0),
4935 (status & HDSPM_BufferPositionMask),
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004936 (status & HDSPM_BufferPositionMask) %
4937 (2 * (int)hdspm->period_bytes),
4938 ((status & HDSPM_BufferPositionMask) - 64) %
4939 (2 * (int)hdspm->period_bytes),
Remy Bruno3cee5a62006-10-16 12:46:32 +02004940 (long) hdspm_hw_pointer(hdspm) * 4);
4941
4942 snd_iprintf(buffer,
4943 "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
4944 hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
4945 hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
4946 hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
4947 hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
4948 snd_iprintf(buffer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01004949 "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
4950 hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
4951 hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
4952 snd_iprintf(buffer,
4953 "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
4954 "status2=0x%x\n",
4955 hdspm->control_register, hdspm->control2_register,
4956 status, status2);
Remy Bruno3cee5a62006-10-16 12:46:32 +02004957
4958 snd_iprintf(buffer, "--- Settings ---\n");
4959
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02004960 x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
Adrian Knoth0dca1792011-01-26 19:32:14 +01004961 HDSPM_LatencyMask));
Remy Bruno3cee5a62006-10-16 12:46:32 +02004962
4963 snd_iprintf(buffer,
4964 "Size (Latency): %d samples (2 periods of %lu bytes)\n",
4965 x, (unsigned long) hdspm->period_bytes);
4966
Adrian Knoth0dca1792011-01-26 19:32:14 +01004967 snd_iprintf(buffer, "Line out: %s\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02004968 (hdspm->
Adrian Knoth0dca1792011-01-26 19:32:14 +01004969 control_register & HDSPM_LineOut) ? "on " : "off");
Remy Bruno3cee5a62006-10-16 12:46:32 +02004970
4971 snd_iprintf(buffer,
4972 "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
4973 (hdspm->
4974 control_register & HDSPM_clr_tms) ? "on" : "off",
4975 (hdspm->
4976 control_register & HDSPM_Emphasis) ? "on" : "off",
4977 (hdspm->
4978 control_register & HDSPM_Dolby) ? "on" : "off");
4979
Remy Bruno3cee5a62006-10-16 12:46:32 +02004980
4981 pref_syncref = hdspm_pref_sync_ref(hdspm);
4982 if (pref_syncref == 0)
4983 snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
4984 else
4985 snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
4986 pref_syncref);
4987
4988 snd_iprintf(buffer, "System Clock Frequency: %d\n",
4989 hdspm->system_sample_rate);
4990
4991 snd_iprintf(buffer, "Double speed: %s\n",
4992 hdspm->control_register & HDSPM_DS_DoubleWire?
4993 "Double wire" : "Single wire");
4994 snd_iprintf(buffer, "Quad speed: %s\n",
4995 hdspm->control_register & HDSPM_QS_DoubleWire?
4996 "Double wire" :
4997 hdspm->control_register & HDSPM_QS_QuadWire?
4998 "Quad wire" : "Single wire");
4999
5000 snd_iprintf(buffer, "--- Status:\n");
5001
5002 snd_iprintf(buffer, "Word: %s Frequency: %d\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01005003 (status & HDSPM_AES32_wcLock) ? "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005004 HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005005
5006 for (x = 0; x < 8; x++) {
5007 snd_iprintf(buffer, "AES%d: %s Frequency: %d\n",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005008 x+1,
5009 (status2 & (HDSPM_LockAES >> x)) ?
Adrian Knoth0dca1792011-01-26 19:32:14 +01005010 "Sync " : "No Lock",
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005011 HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
Remy Bruno3cee5a62006-10-16 12:46:32 +02005012 }
5013
5014 switch (hdspm_autosync_ref(hdspm)) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005015 case HDSPM_AES32_AUTOSYNC_FROM_NONE:
5016 autosync_ref = "None"; break;
5017 case HDSPM_AES32_AUTOSYNC_FROM_WORD:
5018 autosync_ref = "Word Clock"; break;
5019 case HDSPM_AES32_AUTOSYNC_FROM_AES1:
5020 autosync_ref = "AES1"; break;
5021 case HDSPM_AES32_AUTOSYNC_FROM_AES2:
5022 autosync_ref = "AES2"; break;
5023 case HDSPM_AES32_AUTOSYNC_FROM_AES3:
5024 autosync_ref = "AES3"; break;
5025 case HDSPM_AES32_AUTOSYNC_FROM_AES4:
5026 autosync_ref = "AES4"; break;
5027 case HDSPM_AES32_AUTOSYNC_FROM_AES5:
5028 autosync_ref = "AES5"; break;
5029 case HDSPM_AES32_AUTOSYNC_FROM_AES6:
5030 autosync_ref = "AES6"; break;
5031 case HDSPM_AES32_AUTOSYNC_FROM_AES7:
5032 autosync_ref = "AES7"; break;
5033 case HDSPM_AES32_AUTOSYNC_FROM_AES8:
5034 autosync_ref = "AES8"; break;
5035 default:
5036 autosync_ref = "---"; break;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005037 }
5038 snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);
5039
5040 snd_iprintf(buffer, "\n");
5041}
5042
Adrian Knoth0dca1792011-01-26 19:32:14 +01005043static void
5044snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
5045 struct snd_info_buffer *buffer)
5046{
5047 struct hdspm *hdspm = entry->private_data;
5048 unsigned int status1, status2, status3, control, i;
5049 unsigned int lock, sync;
5050
5051 status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
5052 status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
5053 status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */
5054
5055 control = hdspm->control_register;
5056
5057 snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
5058 snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
5059 snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);
5060
5061
5062 snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");
5063
5064 snd_iprintf(buffer, "Clock mode : %s\n",
5065 (hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
5066 snd_iprintf(buffer, "System frequency: %d Hz\n",
5067 hdspm_get_system_sample_rate(hdspm));
5068
5069 snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");
5070
5071 lock = 0x1;
5072 sync = 0x100;
5073
5074 for (i = 0; i < 8; i++) {
5075 snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
5076 i,
5077 (status1 & lock) ? 1 : 0,
5078 (status1 & sync) ? 1 : 0,
5079 texts_freq[(status2 >> (i * 4)) & 0xF]);
5080
5081 lock = lock<<1;
5082 sync = sync<<1;
5083 }
5084
5085 snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
5086 (status1 & 0x1000000) ? 1 : 0,
5087 (status1 & 0x2000000) ? 1 : 0,
5088 texts_freq[(status1 >> 16) & 0xF]);
5089
5090 snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
5091 (status1 & 0x4000000) ? 1 : 0,
5092 (status1 & 0x8000000) ? 1 : 0,
5093 texts_freq[(status1 >> 20) & 0xF]);
5094
5095 snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
5096 (status3 & 0x400) ? 1 : 0,
5097 (status3 & 0x800) ? 1 : 0,
5098 texts_freq[(status2 >> 12) & 0xF]);
5099
5100}
5101
Remy Bruno3cee5a62006-10-16 12:46:32 +02005102#ifdef CONFIG_SND_DEBUG
5103static void
Adrian Knoth0dca1792011-01-26 19:32:14 +01005104snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005105 struct snd_info_buffer *buffer)
5106{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005107 struct hdspm *hdspm = entry->private_data;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005108
5109 int j,i;
5110
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005111 for (i = 0; i < 256 /* 1024*64 */; i += j) {
Remy Bruno3cee5a62006-10-16 12:46:32 +02005112 snd_iprintf(buffer, "0x%08X: ", i);
5113 for (j = 0; j < 16; j += 4)
5114 snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
5115 snd_iprintf(buffer, "\n");
5116 }
5117}
5118#endif
5119
5120
Adrian Knoth0dca1792011-01-26 19:32:14 +01005121static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
5122 struct snd_info_buffer *buffer)
5123{
5124 struct hdspm *hdspm = entry->private_data;
5125 int i;
Remy Bruno3cee5a62006-10-16 12:46:32 +02005126
Adrian Knoth0dca1792011-01-26 19:32:14 +01005127 snd_iprintf(buffer, "# generated by hdspm\n");
5128
5129 for (i = 0; i < hdspm->max_channels_in; i++) {
5130 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
5131 }
5132}
5133
5134static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
5135 struct snd_info_buffer *buffer)
5136{
5137 struct hdspm *hdspm = entry->private_data;
5138 int i;
5139
5140 snd_iprintf(buffer, "# generated by hdspm\n");
5141
5142 for (i = 0; i < hdspm->max_channels_out; i++) {
5143 snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
5144 }
5145}
5146
5147
5148static void __devinit snd_hdspm_proc_init(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005149{
Takashi Iwai98274f02005-11-17 14:52:34 +01005150 struct snd_info_entry *entry;
Takashi Iwai763f3562005-06-03 11:25:34 +02005151
Adrian Knoth0dca1792011-01-26 19:32:14 +01005152 if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
5153 switch (hdspm->io_type) {
5154 case AES32:
5155 snd_info_set_text_ops(entry, hdspm,
5156 snd_hdspm_proc_read_aes32);
5157 break;
5158 case MADI:
5159 snd_info_set_text_ops(entry, hdspm,
5160 snd_hdspm_proc_read_madi);
5161 break;
5162 case MADIface:
5163 /* snd_info_set_text_ops(entry, hdspm,
5164 snd_hdspm_proc_read_madiface); */
5165 break;
5166 case RayDAT:
5167 snd_info_set_text_ops(entry, hdspm,
5168 snd_hdspm_proc_read_raydat);
5169 break;
5170 case AIO:
5171 break;
5172 }
5173 }
5174
5175 if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
5176 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
5177 }
5178
5179 if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
5180 snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
5181 }
5182
Remy Bruno3cee5a62006-10-16 12:46:32 +02005183#ifdef CONFIG_SND_DEBUG
5184 /* debug file to read all hdspm registers */
5185 if (!snd_card_proc_new(hdspm->card, "debug", &entry))
5186 snd_info_set_text_ops(entry, hdspm,
5187 snd_hdspm_proc_read_debug);
5188#endif
Takashi Iwai763f3562005-06-03 11:25:34 +02005189}
5190
5191/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005192 hdspm intitialize
Takashi Iwai763f3562005-06-03 11:25:34 +02005193 ------------------------------------------------------------*/
5194
Takashi Iwai98274f02005-11-17 14:52:34 +01005195static int snd_hdspm_set_defaults(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02005196{
Takashi Iwai763f3562005-06-03 11:25:34 +02005197 /* ASSUMPTION: hdspm->lock is either held, or there is no need to
Joe Perches561de312007-12-18 13:13:47 +01005198 hold it (e.g. during module initialization).
Adrian Knoth0dca1792011-01-26 19:32:14 +01005199 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005200
5201 /* set defaults: */
5202
Adrian Knoth0dca1792011-01-26 19:32:14 +01005203 hdspm->settings_register = 0;
5204
5205 switch (hdspm->io_type) {
5206 case MADI:
5207 case MADIface:
5208 hdspm->control_register =
5209 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5210 break;
5211
5212 case RayDAT:
5213 case AIO:
5214 hdspm->settings_register = 0x1 + 0x1000;
5215 /* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
5216 * line_out */
5217 hdspm->control_register =
5218 0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
5219 break;
5220
5221 case AES32:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005222 hdspm->control_register =
5223 HDSPM_ClockModeMaster | /* Master Cloack Mode on */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005224 hdspm_encode_latency(7) | /* latency max=8192samples */
Remy Bruno3cee5a62006-10-16 12:46:32 +02005225 HDSPM_SyncRef0 | /* AES1 is syncclock */
5226 HDSPM_LineOut | /* Analog output in */
5227 HDSPM_Professional; /* Professional mode */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005228 break;
5229 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005230
5231 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5232
Adrian Knoth0dca1792011-01-26 19:32:14 +01005233 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005234 /* No control2 register for AES32 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005235#ifdef SNDRV_BIG_ENDIAN
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005236 hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
Takashi Iwai763f3562005-06-03 11:25:34 +02005237#else
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005238 hdspm->control2_register = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02005239#endif
5240
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005241 hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
5242 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005243 hdspm_compute_period_size(hdspm);
5244
5245 /* silence everything */
5246
5247 all_in_all_mixer(hdspm, 0 * UNITY_GAIN);
5248
Adrian Knoth0dca1792011-01-26 19:32:14 +01005249 if (hdspm->io_type == AIO || hdspm->io_type == RayDAT) {
5250 hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
Takashi Iwai763f3562005-06-03 11:25:34 +02005251 }
5252
5253 /* set a default rate so that the channel map is set up. */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005254 hdspm_set_rate(hdspm, 48000, 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02005255
5256 return 0;
5257}
5258
5259
5260/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005261 interrupt
Takashi Iwai763f3562005-06-03 11:25:34 +02005262 ------------------------------------------------------------*/
5263
David Howells7d12e782006-10-05 14:55:46 +01005264static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
Takashi Iwai763f3562005-06-03 11:25:34 +02005265{
Takashi Iwai98274f02005-11-17 14:52:34 +01005266 struct hdspm *hdspm = (struct hdspm *) dev_id;
Takashi Iwai763f3562005-06-03 11:25:34 +02005267 unsigned int status;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005268 int i, audio, midi, schedule = 0;
5269 /* cycles_t now; */
Takashi Iwai763f3562005-06-03 11:25:34 +02005270
5271 status = hdspm_read(hdspm, HDSPM_statusRegister);
5272
5273 audio = status & HDSPM_audioIRQPending;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005274 midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
5275 HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);
Takashi Iwai763f3562005-06-03 11:25:34 +02005276
Adrian Knoth0dca1792011-01-26 19:32:14 +01005277 /* now = get_cycles(); */
5278 /**
5279 * LAT_2..LAT_0 period counter (win) counter (mac)
5280 * 6 4096 ~256053425 ~514672358
5281 * 5 2048 ~128024983 ~257373821
5282 * 4 1024 ~64023706 ~128718089
5283 * 3 512 ~32005945 ~64385999
5284 * 2 256 ~16003039 ~32260176
5285 * 1 128 ~7998738 ~16194507
5286 * 0 64 ~3998231 ~8191558
5287 **/
5288 /*
5289 snd_printk(KERN_INFO "snd_hdspm_interrupt %llu @ %llx\n",
5290 now-hdspm->last_interrupt, status & 0xFFC0);
5291 hdspm->last_interrupt = now;
5292 */
5293
5294 if (!audio && !midi)
Takashi Iwai763f3562005-06-03 11:25:34 +02005295 return IRQ_NONE;
5296
5297 hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
5298 hdspm->irq_count++;
5299
Takashi Iwai763f3562005-06-03 11:25:34 +02005300
5301 if (audio) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005302 if (hdspm->capture_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005303 snd_pcm_period_elapsed(hdspm->capture_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005304
5305 if (hdspm->playback_substream)
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005306 snd_pcm_period_elapsed(hdspm->playback_substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005307 }
5308
Adrian Knoth0dca1792011-01-26 19:32:14 +01005309 if (midi) {
5310 i = 0;
5311 while (i < hdspm->midiPorts) {
5312 if ((hdspm_read(hdspm,
5313 hdspm->midi[i].statusIn) & 0xff) &&
5314 (status & hdspm->midi[i].irq)) {
5315 /* we disable interrupts for this input until
5316 * processing is done
5317 */
5318 hdspm->control_register &= ~hdspm->midi[i].ie;
5319 hdspm_write(hdspm, HDSPM_controlRegister,
5320 hdspm->control_register);
5321 hdspm->midi[i].pending = 1;
5322 schedule = 1;
5323 }
5324
5325 i++;
5326 }
5327
5328 if (schedule)
5329 tasklet_hi_schedule(&hdspm->midi_tasklet);
Takashi Iwai763f3562005-06-03 11:25:34 +02005330 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005331
Takashi Iwai763f3562005-06-03 11:25:34 +02005332 return IRQ_HANDLED;
5333}
5334
5335/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01005336 pcm interface
Takashi Iwai763f3562005-06-03 11:25:34 +02005337 ------------------------------------------------------------*/
5338
5339
Adrian Knoth0dca1792011-01-26 19:32:14 +01005340static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
5341 *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005342{
Takashi Iwai98274f02005-11-17 14:52:34 +01005343 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005344 return hdspm_hw_pointer(hdspm);
5345}
5346
Takashi Iwai763f3562005-06-03 11:25:34 +02005347
Takashi Iwai98274f02005-11-17 14:52:34 +01005348static int snd_hdspm_reset(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005349{
Takashi Iwai98274f02005-11-17 14:52:34 +01005350 struct snd_pcm_runtime *runtime = substream->runtime;
5351 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5352 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005353
5354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5355 other = hdspm->capture_substream;
5356 else
5357 other = hdspm->playback_substream;
5358
5359 if (hdspm->running)
5360 runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
5361 else
5362 runtime->status->hw_ptr = 0;
5363 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005364 struct snd_pcm_substream *s;
5365 struct snd_pcm_runtime *oruntime = other->runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01005366 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005367 if (s == other) {
5368 oruntime->status->hw_ptr =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005369 runtime->status->hw_ptr;
Takashi Iwai763f3562005-06-03 11:25:34 +02005370 break;
5371 }
5372 }
5373 }
5374 return 0;
5375}
5376
Takashi Iwai98274f02005-11-17 14:52:34 +01005377static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
5378 struct snd_pcm_hw_params *params)
Takashi Iwai763f3562005-06-03 11:25:34 +02005379{
Takashi Iwai98274f02005-11-17 14:52:34 +01005380 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005381 int err;
5382 int i;
5383 pid_t this_pid;
5384 pid_t other_pid;
Takashi Iwai763f3562005-06-03 11:25:34 +02005385
5386 spin_lock_irq(&hdspm->lock);
5387
5388 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5389 this_pid = hdspm->playback_pid;
5390 other_pid = hdspm->capture_pid;
5391 } else {
5392 this_pid = hdspm->capture_pid;
5393 other_pid = hdspm->playback_pid;
5394 }
5395
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005396 if (other_pid > 0 && this_pid != other_pid) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005397
5398 /* The other stream is open, and not by the same
5399 task as this one. Make sure that the parameters
5400 that matter are the same.
Adrian Knoth0dca1792011-01-26 19:32:14 +01005401 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005402
5403 if (params_rate(params) != hdspm->system_sample_rate) {
5404 spin_unlock_irq(&hdspm->lock);
5405 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005406 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005407 return -EBUSY;
5408 }
5409
5410 if (params_period_size(params) != hdspm->period_bytes / 4) {
5411 spin_unlock_irq(&hdspm->lock);
5412 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005413 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005414 return -EBUSY;
5415 }
5416
5417 }
5418 /* We're fine. */
5419 spin_unlock_irq(&hdspm->lock);
5420
5421 /* how to make sure that the rate matches an externally-set one ? */
5422
5423 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005424 err = hdspm_set_rate(hdspm, params_rate(params), 0);
5425 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005426 snd_printk(KERN_INFO "err on hdspm_set_rate: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005427 spin_unlock_irq(&hdspm->lock);
5428 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005429 SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005430 return err;
5431 }
5432 spin_unlock_irq(&hdspm->lock);
5433
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005434 err = hdspm_set_interrupt_interval(hdspm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005435 params_period_size(params));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005436 if (err < 0) {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005437 snd_printk(KERN_INFO "err on hdspm_set_interrupt_interval: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005438 _snd_pcm_hw_param_setempty(params,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005439 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
Takashi Iwai763f3562005-06-03 11:25:34 +02005440 return err;
5441 }
5442
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005443 /* Memory allocation, takashi's method, dont know if we should
5444 * spinlock
5445 */
Takashi Iwai763f3562005-06-03 11:25:34 +02005446 /* malloc all buffer even if not enabled to get sure */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005447 /* Update for MADI rev 204: we need to allocate for all channels,
5448 * otherwise it doesn't work at 96kHz */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005449
Takashi Iwai763f3562005-06-03 11:25:34 +02005450 err =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005451 snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
5452 if (err < 0) {
5453 snd_printk(KERN_INFO "err on snd_pcm_lib_malloc_pages: %d\n", err);
Takashi Iwai763f3562005-06-03 11:25:34 +02005454 return err;
Adrian Knoth0dca1792011-01-26 19:32:14 +01005455 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005456
Takashi Iwai763f3562005-06-03 11:25:34 +02005457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5458
Takashi Iwai77a23f22008-08-21 13:00:13 +02005459 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
Takashi Iwai763f3562005-06-03 11:25:34 +02005460 params_channels(params));
5461
5462 for (i = 0; i < params_channels(params); ++i)
5463 snd_hdspm_enable_out(hdspm, i, 1);
5464
5465 hdspm->playback_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005466 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005467 snd_printdd("Allocated sample buffer for playback at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005468 hdspm->playback_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005469 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +02005470 hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
Takashi Iwai763f3562005-06-03 11:25:34 +02005471 params_channels(params));
5472
5473 for (i = 0; i < params_channels(params); ++i)
5474 snd_hdspm_enable_in(hdspm, i, 1);
5475
5476 hdspm->capture_buffer =
Adrian Knoth0dca1792011-01-26 19:32:14 +01005477 (unsigned char *) substream->runtime->dma_area;
Takashi Iwai54bf5dd2006-11-06 15:38:55 +01005478 snd_printdd("Allocated sample buffer for capture at %p\n",
Remy Bruno3cee5a62006-10-16 12:46:32 +02005479 hdspm->capture_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005480 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005481
Remy Bruno3cee5a62006-10-16 12:46:32 +02005482 /*
5483 snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
5484 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5485 "playback" : "capture",
Takashi Iwai77a23f22008-08-21 13:00:13 +02005486 snd_pcm_sgbuf_get_addr(substream, 0));
Adrian Knoth0dca1792011-01-26 19:32:14 +01005487 */
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005488 /*
Adrian Knoth0dca1792011-01-26 19:32:14 +01005489 snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5490 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
5491 "playback" : "capture",
5492 params_rate(params), params_channels(params),
5493 params_buffer_size(params));
5494 */
5495
5496
5497 /* Switch to native float format if requested */
5498 if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
5499 if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5500 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE float format.\n");
5501
5502 hdspm->control_register |= HDSPe_FLOAT_FORMAT;
5503 } else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
5504 if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5505 snd_printk(KERN_INFO "hdspm: Switching to native 32bit LE integer format.\n");
5506
5507 hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
5508 }
5509 hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
5510
Takashi Iwai763f3562005-06-03 11:25:34 +02005511 return 0;
5512}
5513
Takashi Iwai98274f02005-11-17 14:52:34 +01005514static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005515{
5516 int i;
Takashi Iwai98274f02005-11-17 14:52:34 +01005517 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005518
5519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5520
Adrian Knoth0dca1792011-01-26 19:32:14 +01005521 /* params_channels(params) should be enough,
Takashi Iwai763f3562005-06-03 11:25:34 +02005522 but to get sure in case of error */
Adrian Knoth0dca1792011-01-26 19:32:14 +01005523 for (i = 0; i < hdspm->max_channels_out; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005524 snd_hdspm_enable_out(hdspm, i, 0);
5525
5526 hdspm->playback_buffer = NULL;
5527 } else {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005528 for (i = 0; i < hdspm->max_channels_in; ++i)
Takashi Iwai763f3562005-06-03 11:25:34 +02005529 snd_hdspm_enable_in(hdspm, i, 0);
5530
5531 hdspm->capture_buffer = NULL;
5532
5533 }
5534
5535 snd_pcm_lib_free_pages(substream);
5536
5537 return 0;
5538}
5539
Adrian Knoth0dca1792011-01-26 19:32:14 +01005540
Takashi Iwai98274f02005-11-17 14:52:34 +01005541static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005542 struct snd_pcm_channel_info *info)
Takashi Iwai763f3562005-06-03 11:25:34 +02005543{
Takashi Iwai98274f02005-11-17 14:52:34 +01005544 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005545
Adrian Knoth0dca1792011-01-26 19:32:14 +01005546 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
5547 if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5548 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel out of range (%d)\n", info->channel);
5549 return -EINVAL;
5550 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005551
Adrian Knoth0dca1792011-01-26 19:32:14 +01005552 if (hdspm->channel_map_out[info->channel] < 0) {
5553 snd_printk(KERN_INFO "snd_hdspm_channel_info: output channel %d mapped out\n", info->channel);
5554 return -EINVAL;
5555 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005556
Adrian Knoth0dca1792011-01-26 19:32:14 +01005557 info->offset = hdspm->channel_map_out[info->channel] *
5558 HDSPM_CHANNEL_BUFFER_BYTES;
5559 } else {
5560 if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5561 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel out of range (%d)\n", info->channel);
5562 return -EINVAL;
5563 }
5564
5565 if (hdspm->channel_map_in[info->channel] < 0) {
5566 snd_printk(KERN_INFO "snd_hdspm_channel_info: input channel %d mapped out\n", info->channel);
5567 return -EINVAL;
5568 }
5569
5570 info->offset = hdspm->channel_map_in[info->channel] *
5571 HDSPM_CHANNEL_BUFFER_BYTES;
5572 }
5573
Takashi Iwai763f3562005-06-03 11:25:34 +02005574 info->first = 0;
5575 info->step = 32;
5576 return 0;
5577}
5578
Adrian Knoth0dca1792011-01-26 19:32:14 +01005579
Takashi Iwai98274f02005-11-17 14:52:34 +01005580static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005581 unsigned int cmd, void *arg)
Takashi Iwai763f3562005-06-03 11:25:34 +02005582{
5583 switch (cmd) {
5584 case SNDRV_PCM_IOCTL1_RESET:
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005585 return snd_hdspm_reset(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005586
5587 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
Adrian Knoth0dca1792011-01-26 19:32:14 +01005588 {
5589 struct snd_pcm_channel_info *info = arg;
5590 return snd_hdspm_channel_info(substream, info);
5591 }
Takashi Iwai763f3562005-06-03 11:25:34 +02005592 default:
5593 break;
5594 }
5595
5596 return snd_pcm_lib_ioctl(substream, cmd, arg);
5597}
5598
Takashi Iwai98274f02005-11-17 14:52:34 +01005599static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai763f3562005-06-03 11:25:34 +02005600{
Takashi Iwai98274f02005-11-17 14:52:34 +01005601 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5602 struct snd_pcm_substream *other;
Takashi Iwai763f3562005-06-03 11:25:34 +02005603 int running;
5604
5605 spin_lock(&hdspm->lock);
5606 running = hdspm->running;
5607 switch (cmd) {
5608 case SNDRV_PCM_TRIGGER_START:
5609 running |= 1 << substream->stream;
5610 break;
5611 case SNDRV_PCM_TRIGGER_STOP:
5612 running &= ~(1 << substream->stream);
5613 break;
5614 default:
5615 snd_BUG();
5616 spin_unlock(&hdspm->lock);
5617 return -EINVAL;
5618 }
5619 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5620 other = hdspm->capture_substream;
5621 else
5622 other = hdspm->playback_substream;
5623
5624 if (other) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005625 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +01005626 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai763f3562005-06-03 11:25:34 +02005627 if (s == other) {
5628 snd_pcm_trigger_done(s, substream);
5629 if (cmd == SNDRV_PCM_TRIGGER_START)
5630 running |= 1 << s->stream;
5631 else
5632 running &= ~(1 << s->stream);
5633 goto _ok;
5634 }
5635 }
5636 if (cmd == SNDRV_PCM_TRIGGER_START) {
5637 if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
Adrian Knoth0dca1792011-01-26 19:32:14 +01005638 && substream->stream ==
5639 SNDRV_PCM_STREAM_CAPTURE)
Takashi Iwai763f3562005-06-03 11:25:34 +02005640 hdspm_silence_playback(hdspm);
5641 } else {
5642 if (running &&
Adrian Knoth0dca1792011-01-26 19:32:14 +01005643 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwai763f3562005-06-03 11:25:34 +02005644 hdspm_silence_playback(hdspm);
5645 }
5646 } else {
5647 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
5648 hdspm_silence_playback(hdspm);
5649 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005650_ok:
Takashi Iwai763f3562005-06-03 11:25:34 +02005651 snd_pcm_trigger_done(substream, substream);
5652 if (!hdspm->running && running)
5653 hdspm_start_audio(hdspm);
5654 else if (hdspm->running && !running)
5655 hdspm_stop_audio(hdspm);
5656 hdspm->running = running;
5657 spin_unlock(&hdspm->lock);
5658
5659 return 0;
5660}
5661
Takashi Iwai98274f02005-11-17 14:52:34 +01005662static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005663{
5664 return 0;
5665}
5666
Adrian Knoth0dca1792011-01-26 19:32:14 +01005667static unsigned int period_sizes_old[] = {
5668 64, 128, 256, 512, 1024, 2048, 4096
5669};
5670
5671static unsigned int period_sizes_new[] = {
5672 32, 64, 128, 256, 512, 1024, 2048, 4096
5673};
5674
5675/* RayDAT and AIO always have a buffer of 16384 samples per channel */
5676static unsigned int raydat_aio_buffer_sizes[] = {
5677 16384
5678};
Takashi Iwai763f3562005-06-03 11:25:34 +02005679
Takashi Iwai98274f02005-11-17 14:52:34 +01005680static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005681 .info = (SNDRV_PCM_INFO_MMAP |
5682 SNDRV_PCM_INFO_MMAP_VALID |
5683 SNDRV_PCM_INFO_NONINTERLEAVED |
5684 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
5685 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5686 .rates = (SNDRV_PCM_RATE_32000 |
5687 SNDRV_PCM_RATE_44100 |
5688 SNDRV_PCM_RATE_48000 |
5689 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005690 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5691 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
Takashi Iwai763f3562005-06-03 11:25:34 +02005692 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005693 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005694 .channels_min = 1,
5695 .channels_max = HDSPM_MAX_CHANNELS,
5696 .buffer_bytes_max =
5697 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
5698 .period_bytes_min = (64 * 4),
Adrian Knoth0dca1792011-01-26 19:32:14 +01005699 .period_bytes_max = (4096 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005700 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005701 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005702 .fifo_size = 0
5703};
5704
Takashi Iwai98274f02005-11-17 14:52:34 +01005705static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005706 .info = (SNDRV_PCM_INFO_MMAP |
5707 SNDRV_PCM_INFO_MMAP_VALID |
5708 SNDRV_PCM_INFO_NONINTERLEAVED |
5709 SNDRV_PCM_INFO_SYNC_START),
5710 .formats = SNDRV_PCM_FMTBIT_S32_LE,
5711 .rates = (SNDRV_PCM_RATE_32000 |
5712 SNDRV_PCM_RATE_44100 |
5713 SNDRV_PCM_RATE_48000 |
5714 SNDRV_PCM_RATE_64000 |
Remy Bruno3cee5a62006-10-16 12:46:32 +02005715 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
5716 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
Takashi Iwai763f3562005-06-03 11:25:34 +02005717 .rate_min = 32000,
Remy Bruno3cee5a62006-10-16 12:46:32 +02005718 .rate_max = 192000,
Takashi Iwai763f3562005-06-03 11:25:34 +02005719 .channels_min = 1,
5720 .channels_max = HDSPM_MAX_CHANNELS,
5721 .buffer_bytes_max =
5722 HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
5723 .period_bytes_min = (64 * 4),
Adrian Knoth0dca1792011-01-26 19:32:14 +01005724 .period_bytes_max = (4096 * 4) * HDSPM_MAX_CHANNELS,
Takashi Iwai763f3562005-06-03 11:25:34 +02005725 .periods_min = 2,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005726 .periods_max = 512,
Takashi Iwai763f3562005-06-03 11:25:34 +02005727 .fifo_size = 0
5728};
5729
Adrian Knoth0dca1792011-01-26 19:32:14 +01005730static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes_old = {
5731 .count = ARRAY_SIZE(period_sizes_old),
5732 .list = period_sizes_old,
Takashi Iwai763f3562005-06-03 11:25:34 +02005733 .mask = 0
5734};
5735
Adrian Knoth0dca1792011-01-26 19:32:14 +01005736static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes_new = {
5737 .count = ARRAY_SIZE(period_sizes_new),
5738 .list = period_sizes_new,
5739 .mask = 0
5740};
Takashi Iwai763f3562005-06-03 11:25:34 +02005741
Adrian Knoth0dca1792011-01-26 19:32:14 +01005742static struct snd_pcm_hw_constraint_list hw_constraints_raydat_io_buffer = {
5743 .count = ARRAY_SIZE(raydat_aio_buffer_sizes),
5744 .list = raydat_aio_buffer_sizes,
5745 .mask = 0
5746};
5747
5748static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
5749 struct snd_pcm_hw_rule *rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005750{
Takashi Iwai98274f02005-11-17 14:52:34 +01005751 struct hdspm *hdspm = rule->private;
5752 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005753 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005754 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005755 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5756
Adrian Knoth0dca1792011-01-26 19:32:14 +01005757 if (r->min > 96000 && r->max <= 192000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005758 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005759 .min = hdspm->qs_in_channels,
5760 .max = hdspm->qs_in_channels,
5761 .integer = 1,
5762 };
5763 return snd_interval_refine(c, &t);
5764 } else if (r->min > 48000 && r->max <= 96000) {
5765 struct snd_interval t = {
5766 .min = hdspm->ds_in_channels,
5767 .max = hdspm->ds_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005768 .integer = 1,
5769 };
5770 return snd_interval_refine(c, &t);
5771 } else if (r->max < 64000) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005772 struct snd_interval t = {
Adrian Knoth0dca1792011-01-26 19:32:14 +01005773 .min = hdspm->ss_in_channels,
5774 .max = hdspm->ss_in_channels,
Takashi Iwai763f3562005-06-03 11:25:34 +02005775 .integer = 1,
5776 };
5777 return snd_interval_refine(c, &t);
5778 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005779
Takashi Iwai763f3562005-06-03 11:25:34 +02005780 return 0;
5781}
5782
Adrian Knoth0dca1792011-01-26 19:32:14 +01005783static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
Takashi Iwai98274f02005-11-17 14:52:34 +01005784 struct snd_pcm_hw_rule * rule)
Takashi Iwai763f3562005-06-03 11:25:34 +02005785{
Takashi Iwai98274f02005-11-17 14:52:34 +01005786 struct hdspm *hdspm = rule->private;
5787 struct snd_interval *c =
Takashi Iwai763f3562005-06-03 11:25:34 +02005788 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Takashi Iwai98274f02005-11-17 14:52:34 +01005789 struct snd_interval *r =
Takashi Iwai763f3562005-06-03 11:25:34 +02005790 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5791
Adrian Knoth0dca1792011-01-26 19:32:14 +01005792 if (r->min > 96000 && r->max <= 192000) {
5793 struct snd_interval t = {
5794 .min = hdspm->qs_out_channels,
5795 .max = hdspm->qs_out_channels,
5796 .integer = 1,
5797 };
5798 return snd_interval_refine(c, &t);
5799 } else if (r->min > 48000 && r->max <= 96000) {
5800 struct snd_interval t = {
5801 .min = hdspm->ds_out_channels,
5802 .max = hdspm->ds_out_channels,
5803 .integer = 1,
5804 };
5805 return snd_interval_refine(c, &t);
5806 } else if (r->max < 64000) {
5807 struct snd_interval t = {
5808 .min = hdspm->ss_out_channels,
5809 .max = hdspm->ss_out_channels,
5810 .integer = 1,
5811 };
5812 return snd_interval_refine(c, &t);
5813 } else {
5814 }
5815 return 0;
5816}
5817
5818static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5819 struct snd_pcm_hw_rule * rule)
5820{
5821 struct hdspm *hdspm = rule->private;
5822 struct snd_interval *c =
5823 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5824 struct snd_interval *r =
5825 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5826
5827 if (c->min >= hdspm->ss_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005828 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005829 .min = 32000,
5830 .max = 48000,
5831 .integer = 1,
5832 };
5833 return snd_interval_refine(r, &t);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005834 } else if (c->max <= hdspm->qs_in_channels) {
5835 struct snd_interval t = {
5836 .min = 128000,
5837 .max = 192000,
5838 .integer = 1,
5839 };
5840 return snd_interval_refine(r, &t);
5841 } else if (c->max <= hdspm->ds_in_channels) {
Takashi Iwai98274f02005-11-17 14:52:34 +01005842 struct snd_interval t = {
Takashi Iwai763f3562005-06-03 11:25:34 +02005843 .min = 64000,
5844 .max = 96000,
5845 .integer = 1,
5846 };
Takashi Iwai763f3562005-06-03 11:25:34 +02005847 return snd_interval_refine(r, &t);
5848 }
Adrian Knoth0dca1792011-01-26 19:32:14 +01005849
5850 return 0;
5851}
5852static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
5853 struct snd_pcm_hw_rule *rule)
5854{
5855 struct hdspm *hdspm = rule->private;
5856 struct snd_interval *c =
5857 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5858 struct snd_interval *r =
5859 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
5860
5861 if (c->min >= hdspm->ss_out_channels) {
5862 struct snd_interval t = {
5863 .min = 32000,
5864 .max = 48000,
5865 .integer = 1,
5866 };
5867 return snd_interval_refine(r, &t);
5868 } else if (c->max <= hdspm->qs_out_channels) {
5869 struct snd_interval t = {
5870 .min = 128000,
5871 .max = 192000,
5872 .integer = 1,
5873 };
5874 return snd_interval_refine(r, &t);
5875 } else if (c->max <= hdspm->ds_out_channels) {
5876 struct snd_interval t = {
5877 .min = 64000,
5878 .max = 96000,
5879 .integer = 1,
5880 };
5881 return snd_interval_refine(r, &t);
5882 }
5883
Takashi Iwai763f3562005-06-03 11:25:34 +02005884 return 0;
5885}
5886
Adrian Knoth0dca1792011-01-26 19:32:14 +01005887static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005888 struct snd_pcm_hw_rule *rule)
5889{
5890 unsigned int list[3];
5891 struct hdspm *hdspm = rule->private;
5892 struct snd_interval *c = hw_param_interval(params,
5893 SNDRV_PCM_HW_PARAM_CHANNELS);
Adrian Knoth0dca1792011-01-26 19:32:14 +01005894
5895 list[0] = hdspm->qs_in_channels;
5896 list[1] = hdspm->ds_in_channels;
5897 list[2] = hdspm->ss_in_channels;
5898 return snd_interval_list(c, 3, list, 0);
5899}
5900
5901static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
5902 struct snd_pcm_hw_rule *rule)
5903{
5904 unsigned int list[3];
5905 struct hdspm *hdspm = rule->private;
5906 struct snd_interval *c = hw_param_interval(params,
5907 SNDRV_PCM_HW_PARAM_CHANNELS);
5908
5909 list[0] = hdspm->qs_out_channels;
5910 list[1] = hdspm->ds_out_channels;
5911 list[2] = hdspm->ss_out_channels;
5912 return snd_interval_list(c, 3, list, 0);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005913}
5914
5915
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005916static unsigned int hdspm_aes32_sample_rates[] = {
5917 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
5918};
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005919
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02005920static struct snd_pcm_hw_constraint_list
5921hdspm_hw_constraints_aes32_sample_rates = {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005922 .count = ARRAY_SIZE(hdspm_aes32_sample_rates),
5923 .list = hdspm_aes32_sample_rates,
5924 .mask = 0
5925};
5926
Takashi Iwai98274f02005-11-17 14:52:34 +01005927static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005928{
Takashi Iwai98274f02005-11-17 14:52:34 +01005929 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
5930 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02005931
Takashi Iwai763f3562005-06-03 11:25:34 +02005932 spin_lock_irq(&hdspm->lock);
5933
5934 snd_pcm_set_sync(substream);
5935
Adrian Knoth0dca1792011-01-26 19:32:14 +01005936
Takashi Iwai763f3562005-06-03 11:25:34 +02005937 runtime->hw = snd_hdspm_playback_subinfo;
5938
5939 if (hdspm->capture_substream == NULL)
5940 hdspm_stop_audio(hdspm);
5941
5942 hdspm->playback_pid = current->pid;
5943 hdspm->playback_substream = substream;
5944
5945 spin_unlock_irq(&hdspm->lock);
5946
5947 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
5948
Adrian Knoth0dca1792011-01-26 19:32:14 +01005949 switch (hdspm->io_type) {
5950 case AIO:
5951 case RayDAT:
5952 snd_pcm_hw_constraint_list(runtime, 0,
5953 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5954 &hw_constraints_period_sizes_new);
5955 snd_pcm_hw_constraint_list(runtime, 0,
5956 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
5957 &hw_constraints_raydat_io_buffer);
Takashi Iwai763f3562005-06-03 11:25:34 +02005958
Adrian Knoth0dca1792011-01-26 19:32:14 +01005959 break;
5960
5961 default:
5962 snd_pcm_hw_constraint_list(runtime, 0,
5963 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
5964 &hw_constraints_period_sizes_old);
5965 }
5966
5967 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005968 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
5969 &hdspm_hw_constraints_aes32_sample_rates);
5970 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005971 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth0dca1792011-01-26 19:32:14 +01005972 snd_hdspm_hw_rule_rate_out_channels, hdspm,
5973 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01005974 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01005975
5976 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5977 snd_hdspm_hw_rule_out_channels, hdspm,
5978 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
5979
5980 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
5981 snd_hdspm_hw_rule_out_channels_rate, hdspm,
5982 SNDRV_PCM_HW_PARAM_RATE, -1);
5983
Takashi Iwai763f3562005-06-03 11:25:34 +02005984 return 0;
5985}
5986
Takashi Iwai98274f02005-11-17 14:52:34 +01005987static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02005988{
Takashi Iwai98274f02005-11-17 14:52:34 +01005989 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02005990
5991 spin_lock_irq(&hdspm->lock);
5992
5993 hdspm->playback_pid = -1;
5994 hdspm->playback_substream = NULL;
5995
5996 spin_unlock_irq(&hdspm->lock);
5997
5998 return 0;
5999}
6000
6001
Takashi Iwai98274f02005-11-17 14:52:34 +01006002static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006003{
Takashi Iwai98274f02005-11-17 14:52:34 +01006004 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
6005 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai763f3562005-06-03 11:25:34 +02006006
6007 spin_lock_irq(&hdspm->lock);
6008 snd_pcm_set_sync(substream);
6009 runtime->hw = snd_hdspm_capture_subinfo;
6010
6011 if (hdspm->playback_substream == NULL)
6012 hdspm_stop_audio(hdspm);
6013
6014 hdspm->capture_pid = current->pid;
6015 hdspm->capture_substream = substream;
6016
6017 spin_unlock_irq(&hdspm->lock);
6018
6019 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006020 switch (hdspm->io_type) {
6021 case AIO:
6022 case RayDAT:
6023 snd_pcm_hw_constraint_list(runtime, 0,
6024 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6025 &hw_constraints_period_sizes_new);
6026 snd_pcm_hw_constraint_list(runtime, 0,
6027 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
6028 &hw_constraints_raydat_io_buffer);
6029 break;
6030
6031 default:
6032 snd_pcm_hw_constraint_list(runtime, 0,
6033 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
6034 &hw_constraints_period_sizes_old);
6035 }
6036
6037 if (AES32 == hdspm->io_type) {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006038 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6039 &hdspm_hw_constraints_aes32_sample_rates);
6040 } else {
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006041 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006042 snd_hdspm_hw_rule_rate_in_channels, hdspm,
6043 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Remy Brunoffb2c3c2007-03-07 19:08:46 +01006044 }
Adrian Knoth88fabbf2011-02-23 11:43:10 +01006045
6046 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6047 snd_hdspm_hw_rule_in_channels, hdspm,
6048 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6049
6050 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
6051 snd_hdspm_hw_rule_in_channels_rate, hdspm,
6052 SNDRV_PCM_HW_PARAM_RATE, -1);
6053
Takashi Iwai763f3562005-06-03 11:25:34 +02006054 return 0;
6055}
6056
Takashi Iwai98274f02005-11-17 14:52:34 +01006057static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
Takashi Iwai763f3562005-06-03 11:25:34 +02006058{
Takashi Iwai98274f02005-11-17 14:52:34 +01006059 struct hdspm *hdspm = snd_pcm_substream_chip(substream);
Takashi Iwai763f3562005-06-03 11:25:34 +02006060
6061 spin_lock_irq(&hdspm->lock);
6062
6063 hdspm->capture_pid = -1;
6064 hdspm->capture_substream = NULL;
6065
6066 spin_unlock_irq(&hdspm->lock);
6067 return 0;
6068}
6069
Adrian Knoth0dca1792011-01-26 19:32:14 +01006070static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
Takashi Iwai763f3562005-06-03 11:25:34 +02006071{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006072 /* we have nothing to initialize but the call is required */
6073 return 0;
6074}
6075
6076static inline int copy_u32_le(void __user *dest, void __iomem *src)
6077{
6078 u32 val = readl(src);
6079 return copy_to_user(dest, &val, 4);
6080}
6081
6082static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
6083 unsigned int cmd, unsigned long __user arg)
6084{
6085 void __user *argp = (void __user *)arg;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006086 struct hdspm *hdspm = hw->private_data;
Takashi Iwai98274f02005-11-17 14:52:34 +01006087 struct hdspm_mixer_ioctl mixer;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006088 struct hdspm_config info;
6089 struct hdspm_status status;
Takashi Iwai98274f02005-11-17 14:52:34 +01006090 struct hdspm_version hdspm_version;
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006091 struct hdspm_peak_rms *levels;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006092 struct hdspm_ltc ltc;
6093 unsigned int statusregister;
6094 long unsigned int s;
6095 int i = 0;
Takashi Iwai763f3562005-06-03 11:25:34 +02006096
6097 switch (cmd) {
6098
Takashi Iwai763f3562005-06-03 11:25:34 +02006099 case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006100 levels = &hdspm->peak_rms;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006101 for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006102 levels->input_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006103 readl(hdspm->iobase +
6104 HDSPM_MADI_INPUT_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006105 levels->playback_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006106 readl(hdspm->iobase +
6107 HDSPM_MADI_PLAYBACK_PEAK + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006108 levels->output_peaks[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006109 readl(hdspm->iobase +
6110 HDSPM_MADI_OUTPUT_PEAK + i*4);
6111
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006112 levels->input_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006113 ((uint64_t) readl(hdspm->iobase +
6114 HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
6115 (uint64_t) readl(hdspm->iobase +
6116 HDSPM_MADI_INPUT_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006117 levels->playback_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006118 ((uint64_t)readl(hdspm->iobase +
6119 HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
6120 (uint64_t)readl(hdspm->iobase +
6121 HDSPM_MADI_PLAYBACK_RMS_L + i*4);
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006122 levels->output_rms[i] =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006123 ((uint64_t)readl(hdspm->iobase +
6124 HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
6125 (uint64_t)readl(hdspm->iobase +
6126 HDSPM_MADI_OUTPUT_RMS_L + i*4);
6127 }
6128
6129 if (hdspm->system_sample_rate > 96000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006130 levels->speed = qs;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006131 } else if (hdspm->system_sample_rate > 48000) {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006132 levels->speed = ds;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006133 } else {
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006134 levels->speed = ss;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006135 }
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006136 levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
Adrian Knoth0dca1792011-01-26 19:32:14 +01006137
Jaroslav Kysela730a5862011-01-27 13:03:15 +01006138 s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
Adrian Knoth0dca1792011-01-26 19:32:14 +01006139 if (0 != s) {
6140 /* snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu
6141 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
6142 */
Takashi Iwai763f3562005-06-03 11:25:34 +02006143 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006144 }
6145 break;
6146
6147 case SNDRV_HDSPM_IOCTL_GET_LTC:
6148 ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
6149 i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
6150 if (i & HDSPM_TCO1_LTC_Input_valid) {
6151 switch (i & (HDSPM_TCO1_LTC_Format_LSB |
6152 HDSPM_TCO1_LTC_Format_MSB)) {
6153 case 0:
6154 ltc.format = fps_24;
6155 break;
6156 case HDSPM_TCO1_LTC_Format_LSB:
6157 ltc.format = fps_25;
6158 break;
6159 case HDSPM_TCO1_LTC_Format_MSB:
6160 ltc.format = fps_2997;
6161 break;
6162 default:
6163 ltc.format = 30;
6164 break;
6165 }
6166 if (i & HDSPM_TCO1_set_drop_frame_flag) {
6167 ltc.frame = drop_frame;
6168 } else {
6169 ltc.frame = full_frame;
6170 }
6171 } else {
6172 ltc.format = format_invalid;
6173 ltc.frame = frame_invalid;
6174 }
6175 if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
6176 ltc.input_format = ntsc;
6177 } else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
6178 ltc.input_format = pal;
6179 } else {
6180 ltc.input_format = no_video;
6181 }
6182
6183 s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
6184 if (0 != s) {
6185 /*
6186 snd_printk(KERN_ERR "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
Takashi Iwai763f3562005-06-03 11:25:34 +02006187 return -EFAULT;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006188 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006189
6190 break;
Takashi Iwai763f3562005-06-03 11:25:34 +02006191
Adrian Knoth0dca1792011-01-26 19:32:14 +01006192 case SNDRV_HDSPM_IOCTL_GET_CONFIG:
Takashi Iwai763f3562005-06-03 11:25:34 +02006193
Adrian Knoth4ab69a22011-02-23 11:43:14 +01006194 memset(&info, 0, sizeof(info));
Takashi Iwai763f3562005-06-03 11:25:34 +02006195 spin_lock_irq(&hdspm->lock);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006196 info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
6197 info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006198
6199 info.system_sample_rate = hdspm->system_sample_rate;
6200 info.autosync_sample_rate =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006201 hdspm_external_sample_rate(hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006202 info.system_clock_mode = hdspm_system_clock_mode(hdspm);
6203 info.clock_source = hdspm_clock_source(hdspm);
6204 info.autosync_ref = hdspm_autosync_ref(hdspm);
6205 info.line_out = hdspm_line_out(hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006206 info.passthru = 0;
6207 spin_unlock_irq(&hdspm->lock);
6208 if (copy_to_user((void __user *) arg, &info, sizeof(info)))
6209 return -EFAULT;
6210 break;
6211
Adrian Knoth0dca1792011-01-26 19:32:14 +01006212 case SNDRV_HDSPM_IOCTL_GET_STATUS:
6213 status.card_type = hdspm->io_type;
6214
6215 status.autosync_source = hdspm_autosync_ref(hdspm);
6216
6217 status.card_clock = 110069313433624ULL;
6218 status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
6219
6220 switch (hdspm->io_type) {
6221 case MADI:
6222 case MADIface:
6223 status.card_specific.madi.sync_wc =
6224 hdspm_wc_sync_check(hdspm);
6225 status.card_specific.madi.sync_madi =
6226 hdspm_madi_sync_check(hdspm);
6227 status.card_specific.madi.sync_tco =
6228 hdspm_tco_sync_check(hdspm);
6229 status.card_specific.madi.sync_in =
6230 hdspm_sync_in_sync_check(hdspm);
6231
6232 statusregister =
6233 hdspm_read(hdspm, HDSPM_statusRegister);
6234 status.card_specific.madi.madi_input =
6235 (statusregister & HDSPM_AB_int) ? 1 : 0;
6236 status.card_specific.madi.channel_format =
6237 (statusregister & HDSPM_TX_64ch) ? 1 : 0;
6238 /* TODO: Mac driver sets it when f_s>48kHz */
6239 status.card_specific.madi.frame_format = 0;
6240
6241 default:
6242 break;
6243 }
6244
6245 if (copy_to_user((void __user *) arg, &status, sizeof(status)))
6246 return -EFAULT;
6247
6248
6249 break;
6250
Takashi Iwai763f3562005-06-03 11:25:34 +02006251 case SNDRV_HDSPM_IOCTL_GET_VERSION:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006252 hdspm_version.card_type = hdspm->io_type;
6253 strncpy(hdspm_version.cardname, hdspm->card_name,
6254 sizeof(hdspm_version.cardname));
6255 hdspm_version.serial = (hdspm_read(hdspm,
6256 HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
Takashi Iwai763f3562005-06-03 11:25:34 +02006257 hdspm_version.firmware_rev = hdspm->firmware_rev;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006258 hdspm_version.addons = 0;
6259 if (hdspm->tco)
6260 hdspm_version.addons |= HDSPM_ADDON_TCO;
6261
Takashi Iwai763f3562005-06-03 11:25:34 +02006262 if (copy_to_user((void __user *) arg, &hdspm_version,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006263 sizeof(hdspm_version)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006264 return -EFAULT;
6265 break;
6266
6267 case SNDRV_HDSPM_IOCTL_GET_MIXER:
6268 if (copy_from_user(&mixer, (void __user *)arg, sizeof(mixer)))
6269 return -EFAULT;
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006270 if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006271 sizeof(struct hdspm_mixer)))
Takashi Iwai763f3562005-06-03 11:25:34 +02006272 return -EFAULT;
6273 break;
6274
6275 default:
6276 return -EINVAL;
6277 }
6278 return 0;
6279}
6280
Takashi Iwai98274f02005-11-17 14:52:34 +01006281static struct snd_pcm_ops snd_hdspm_playback_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006282 .open = snd_hdspm_playback_open,
6283 .close = snd_hdspm_playback_release,
6284 .ioctl = snd_hdspm_ioctl,
6285 .hw_params = snd_hdspm_hw_params,
6286 .hw_free = snd_hdspm_hw_free,
6287 .prepare = snd_hdspm_prepare,
6288 .trigger = snd_hdspm_trigger,
6289 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006290 .page = snd_pcm_sgbuf_ops_page,
6291};
6292
Takashi Iwai98274f02005-11-17 14:52:34 +01006293static struct snd_pcm_ops snd_hdspm_capture_ops = {
Takashi Iwai763f3562005-06-03 11:25:34 +02006294 .open = snd_hdspm_capture_open,
6295 .close = snd_hdspm_capture_release,
6296 .ioctl = snd_hdspm_ioctl,
6297 .hw_params = snd_hdspm_hw_params,
6298 .hw_free = snd_hdspm_hw_free,
6299 .prepare = snd_hdspm_prepare,
6300 .trigger = snd_hdspm_trigger,
6301 .pointer = snd_hdspm_hw_pointer,
Takashi Iwai763f3562005-06-03 11:25:34 +02006302 .page = snd_pcm_sgbuf_ops_page,
6303};
6304
Takashi Iwai98274f02005-11-17 14:52:34 +01006305static int __devinit snd_hdspm_create_hwdep(struct snd_card *card,
6306 struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006307{
Takashi Iwai98274f02005-11-17 14:52:34 +01006308 struct snd_hwdep *hw;
Takashi Iwai763f3562005-06-03 11:25:34 +02006309 int err;
6310
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006311 err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
6312 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006313 return err;
6314
6315 hdspm->hwdep = hw;
6316 hw->private_data = hdspm;
6317 strcpy(hw->name, "HDSPM hwdep interface");
6318
Adrian Knoth0dca1792011-01-26 19:32:14 +01006319 hw->ops.open = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006320 hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006321 hw->ops.release = snd_hdspm_hwdep_dummy_op;
Takashi Iwai763f3562005-06-03 11:25:34 +02006322
6323 return 0;
6324}
6325
6326
6327/*------------------------------------------------------------
Adrian Knoth0dca1792011-01-26 19:32:14 +01006328 memory interface
Takashi Iwai763f3562005-06-03 11:25:34 +02006329 ------------------------------------------------------------*/
Adrian Knoth0dca1792011-01-26 19:32:14 +01006330static int __devinit snd_hdspm_preallocate_memory(struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006331{
6332 int err;
Takashi Iwai98274f02005-11-17 14:52:34 +01006333 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006334 size_t wanted;
6335
6336 pcm = hdspm->pcm;
6337
Remy Bruno3cee5a62006-10-16 12:46:32 +02006338 wanted = HDSPM_DMA_AREA_BYTES;
Takashi Iwai763f3562005-06-03 11:25:34 +02006339
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006340 err =
Takashi Iwai763f3562005-06-03 11:25:34 +02006341 snd_pcm_lib_preallocate_pages_for_all(pcm,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006342 SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwai763f3562005-06-03 11:25:34 +02006343 snd_dma_pci_data(hdspm->pci),
6344 wanted,
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006345 wanted);
6346 if (err < 0) {
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006347 snd_printdd("Could not preallocate %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006348
6349 return err;
6350 } else
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006351 snd_printdd(" Preallocated %zd Bytes\n", wanted);
Takashi Iwai763f3562005-06-03 11:25:34 +02006352
6353 return 0;
6354}
6355
Adrian Knoth0dca1792011-01-26 19:32:14 +01006356
6357static void hdspm_set_sgbuf(struct hdspm *hdspm,
Takashi Iwai77a23f22008-08-21 13:00:13 +02006358 struct snd_pcm_substream *substream,
Takashi Iwai763f3562005-06-03 11:25:34 +02006359 unsigned int reg, int channels)
6360{
6361 int i;
Adrian Knoth0dca1792011-01-26 19:32:14 +01006362
6363 /* continuous memory segment */
Takashi Iwai763f3562005-06-03 11:25:34 +02006364 for (i = 0; i < (channels * 16); i++)
6365 hdspm_write(hdspm, reg + 4 * i,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006366 snd_pcm_sgbuf_get_addr(substream, 4096 * i));
Takashi Iwai763f3562005-06-03 11:25:34 +02006367}
6368
Adrian Knoth0dca1792011-01-26 19:32:14 +01006369
Takashi Iwai763f3562005-06-03 11:25:34 +02006370/* ------------- ALSA Devices ---------------------------- */
Takashi Iwai98274f02005-11-17 14:52:34 +01006371static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006372 struct hdspm *hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006373{
Takashi Iwai98274f02005-11-17 14:52:34 +01006374 struct snd_pcm *pcm;
Takashi Iwai763f3562005-06-03 11:25:34 +02006375 int err;
6376
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006377 err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
6378 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006379 return err;
6380
6381 hdspm->pcm = pcm;
6382 pcm->private_data = hdspm;
6383 strcpy(pcm->name, hdspm->card_name);
6384
6385 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
6386 &snd_hdspm_playback_ops);
6387 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
6388 &snd_hdspm_capture_ops);
6389
6390 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
6391
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006392 err = snd_hdspm_preallocate_memory(hdspm);
6393 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006394 return err;
6395
6396 return 0;
6397}
6398
Takashi Iwai98274f02005-11-17 14:52:34 +01006399static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006400{
Adrian Knoth7c7102b2011-02-28 15:14:50 +01006401 int i;
6402
6403 for (i = 0; i < hdspm->midiPorts; i++)
6404 snd_hdspm_flush_midi_input(hdspm, i);
Takashi Iwai763f3562005-06-03 11:25:34 +02006405}
6406
Takashi Iwai98274f02005-11-17 14:52:34 +01006407static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
6408 struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006409{
Adrian Knoth0dca1792011-01-26 19:32:14 +01006410 int err, i;
Takashi Iwai763f3562005-06-03 11:25:34 +02006411
6412 snd_printdd("Create card...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006413 err = snd_hdspm_create_pcm(card, hdspm);
6414 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006415 return err;
6416
Adrian Knoth0dca1792011-01-26 19:32:14 +01006417 i = 0;
6418 while (i < hdspm->midiPorts) {
6419 err = snd_hdspm_create_midi(card, hdspm, i);
6420 if (err < 0) {
6421 return err;
6422 }
6423 i++;
6424 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006425
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006426 err = snd_hdspm_create_controls(card, hdspm);
6427 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006428 return err;
6429
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006430 err = snd_hdspm_create_hwdep(card, hdspm);
6431 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006432 return err;
6433
6434 snd_printdd("proc init...\n");
6435 snd_hdspm_proc_init(hdspm);
6436
6437 hdspm->system_sample_rate = -1;
6438 hdspm->last_external_sample_rate = -1;
6439 hdspm->last_internal_sample_rate = -1;
6440 hdspm->playback_pid = -1;
6441 hdspm->capture_pid = -1;
6442 hdspm->capture_substream = NULL;
6443 hdspm->playback_substream = NULL;
6444
6445 snd_printdd("Set defaults...\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006446 err = snd_hdspm_set_defaults(hdspm);
6447 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006448 return err;
6449
6450 snd_printdd("Update mixer controls...\n");
6451 hdspm_update_simple_mixer_controls(hdspm);
6452
6453 snd_printdd("Initializeing complete ???\n");
6454
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006455 err = snd_card_register(card);
6456 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006457 snd_printk(KERN_ERR "HDSPM: error registering card\n");
6458 return err;
6459 }
6460
6461 snd_printdd("... yes now\n");
6462
6463 return 0;
6464}
6465
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006466static int __devinit snd_hdspm_create(struct snd_card *card,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006467 struct hdspm *hdspm) {
6468
Takashi Iwai763f3562005-06-03 11:25:34 +02006469 struct pci_dev *pci = hdspm->pci;
6470 int err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006471 unsigned long io_extent;
6472
6473 hdspm->irq = -1;
Takashi Iwai763f3562005-06-03 11:25:34 +02006474 hdspm->card = card;
6475
6476 spin_lock_init(&hdspm->lock);
6477
Takashi Iwai763f3562005-06-03 11:25:34 +02006478 pci_read_config_word(hdspm->pci,
Adrian Knoth0dca1792011-01-26 19:32:14 +01006479 PCI_CLASS_REVISION, &hdspm->firmware_rev);
Remy Bruno3cee5a62006-10-16 12:46:32 +02006480
Takashi Iwai763f3562005-06-03 11:25:34 +02006481 strcpy(card->mixername, "Xilinx FPGA");
Adrian Knoth0dca1792011-01-26 19:32:14 +01006482 strcpy(card->driver, "HDSPM");
6483
6484 switch (hdspm->firmware_rev) {
6485 case HDSPM_MADI_REV:
Adrian Knothefef0542011-06-12 17:26:19 +02006486 case HDSPM_MADI_OLD_REV:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006487 hdspm->io_type = MADI;
6488 hdspm->card_name = "RME MADI";
6489 hdspm->midiPorts = 3;
6490 break;
6491 case HDSPM_RAYDAT_REV:
6492 hdspm->io_type = RayDAT;
6493 hdspm->card_name = "RME RayDAT";
6494 hdspm->midiPorts = 2;
6495 break;
6496 case HDSPM_AIO_REV:
6497 hdspm->io_type = AIO;
6498 hdspm->card_name = "RME AIO";
6499 hdspm->midiPorts = 1;
6500 break;
6501 case HDSPM_MADIFACE_REV:
6502 hdspm->io_type = MADIface;
6503 hdspm->card_name = "RME MADIface";
6504 hdspm->midiPorts = 1;
6505 break;
6506 case HDSPM_AES_REV:
Adrian Knoth526ea862011-02-28 15:14:48 +01006507 case HDSPM_AES32_REV:
Adrian Knothbdd32552011-03-07 19:10:11 +01006508 case HDSPM_AES32_OLD_REV:
Adrian Knoth0dca1792011-01-26 19:32:14 +01006509 hdspm->io_type = AES32;
6510 hdspm->card_name = "RME AES32";
6511 hdspm->midiPorts = 2;
6512 break;
Adrian Knoth5027f342011-02-28 15:14:49 +01006513 default:
6514 snd_printk(KERN_ERR "HDSPM: unknown firmware revision %x\n",
6515 hdspm->firmware_rev);
6516 return -ENODEV;
Remy Bruno3cee5a62006-10-16 12:46:32 +02006517 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006518
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006519 err = pci_enable_device(pci);
6520 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006521 return err;
6522
6523 pci_set_master(hdspm->pci);
6524
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006525 err = pci_request_regions(pci, "hdspm");
6526 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006527 return err;
6528
6529 hdspm->port = pci_resource_start(pci, 0);
6530 io_extent = pci_resource_len(pci, 0);
6531
6532 snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006533 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006534
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006535 hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
6536 if (!hdspm->iobase) {
6537 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006538 "unable to remap region 0x%lx-0x%lx\n",
6539 hdspm->port, hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006540 return -EBUSY;
6541 }
6542 snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006543 (unsigned long)hdspm->iobase, hdspm->port,
6544 hdspm->port + io_extent - 1);
Takashi Iwai763f3562005-06-03 11:25:34 +02006545
6546 if (request_irq(pci->irq, snd_hdspm_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02006547 IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006548 snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
6549 return -EBUSY;
6550 }
6551
6552 snd_printdd("use IRQ %d\n", pci->irq);
6553
6554 hdspm->irq = pci->irq;
Takashi Iwai763f3562005-06-03 11:25:34 +02006555
Andrew Mortone2eba3e2006-01-20 14:07:13 +01006556 snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
Adrian Knoth0dca1792011-01-26 19:32:14 +01006557 sizeof(struct hdspm_mixer));
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006558 hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
6559 if (!hdspm->mixer) {
6560 snd_printk(KERN_ERR "HDSPM: "
Adrian Knoth0dca1792011-01-26 19:32:14 +01006561 "unable to kmalloc Mixer memory of %d Bytes\n",
6562 (int)sizeof(struct hdspm_mixer));
Takashi Iwai763f3562005-06-03 11:25:34 +02006563 return err;
6564 }
6565
Adrian Knoth0dca1792011-01-26 19:32:14 +01006566 hdspm->port_names_in = NULL;
6567 hdspm->port_names_out = NULL;
6568
6569 switch (hdspm->io_type) {
6570 case AES32:
Adrian Knothd2d10a22011-02-28 15:14:47 +01006571 hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
6572 hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
6573 hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006574
6575 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6576 channel_map_aes32;
6577 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6578 channel_map_aes32;
6579 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6580 channel_map_aes32;
6581 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6582 texts_ports_aes32;
6583 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6584 texts_ports_aes32;
6585 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6586 texts_ports_aes32;
6587
Adrian Knothd2d10a22011-02-28 15:14:47 +01006588 hdspm->max_channels_out = hdspm->max_channels_in =
6589 AES32_CHANNELS;
Adrian Knoth432d2502011-02-23 11:43:08 +01006590 hdspm->port_names_in = hdspm->port_names_out =
6591 texts_ports_aes32;
6592 hdspm->channel_map_in = hdspm->channel_map_out =
6593 channel_map_aes32;
6594
Adrian Knoth0dca1792011-01-26 19:32:14 +01006595 break;
6596
6597 case MADI:
6598 case MADIface:
6599 hdspm->ss_in_channels = hdspm->ss_out_channels =
6600 MADI_SS_CHANNELS;
6601 hdspm->ds_in_channels = hdspm->ds_out_channels =
6602 MADI_DS_CHANNELS;
6603 hdspm->qs_in_channels = hdspm->qs_out_channels =
6604 MADI_QS_CHANNELS;
6605
6606 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6607 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006608 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006609 channel_map_unity_ss;
Adrian Knoth01e96072011-02-23 11:43:11 +01006610 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
Adrian Knoth0dca1792011-01-26 19:32:14 +01006611 channel_map_unity_ss;
6612
6613 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6614 texts_ports_madi;
6615 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6616 texts_ports_madi;
6617 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6618 texts_ports_madi;
6619 break;
6620
6621 case AIO:
6622 if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6623 snd_printk(KERN_INFO "HDSPM: AEB input board found, but not supported\n");
6624 }
6625
6626 hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
6627 hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
6628 hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
6629 hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
6630 hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
6631 hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;
6632
6633 hdspm->channel_map_out_ss = channel_map_aio_out_ss;
6634 hdspm->channel_map_out_ds = channel_map_aio_out_ds;
6635 hdspm->channel_map_out_qs = channel_map_aio_out_qs;
6636
6637 hdspm->channel_map_in_ss = channel_map_aio_in_ss;
6638 hdspm->channel_map_in_ds = channel_map_aio_in_ds;
6639 hdspm->channel_map_in_qs = channel_map_aio_in_qs;
6640
6641 hdspm->port_names_in_ss = texts_ports_aio_in_ss;
6642 hdspm->port_names_out_ss = texts_ports_aio_out_ss;
6643 hdspm->port_names_in_ds = texts_ports_aio_in_ds;
6644 hdspm->port_names_out_ds = texts_ports_aio_out_ds;
6645 hdspm->port_names_in_qs = texts_ports_aio_in_qs;
6646 hdspm->port_names_out_qs = texts_ports_aio_out_qs;
6647
6648 break;
6649
6650 case RayDAT:
6651 hdspm->ss_in_channels = hdspm->ss_out_channels =
6652 RAYDAT_SS_CHANNELS;
6653 hdspm->ds_in_channels = hdspm->ds_out_channels =
6654 RAYDAT_DS_CHANNELS;
6655 hdspm->qs_in_channels = hdspm->qs_out_channels =
6656 RAYDAT_QS_CHANNELS;
6657
6658 hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
6659 hdspm->max_channels_out = RAYDAT_SS_CHANNELS;
6660
6661 hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
6662 channel_map_raydat_ss;
6663 hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6664 channel_map_raydat_ds;
6665 hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6666 channel_map_raydat_qs;
6667 hdspm->channel_map_in = hdspm->channel_map_out =
6668 channel_map_raydat_ss;
6669
6670 hdspm->port_names_in_ss = hdspm->port_names_out_ss =
6671 texts_ports_raydat_ss;
6672 hdspm->port_names_in_ds = hdspm->port_names_out_ds =
6673 texts_ports_raydat_ds;
6674 hdspm->port_names_in_qs = hdspm->port_names_out_qs =
6675 texts_ports_raydat_qs;
6676
6677
6678 break;
6679
6680 }
6681
6682 /* TCO detection */
6683 switch (hdspm->io_type) {
6684 case AIO:
6685 case RayDAT:
6686 if (hdspm_read(hdspm, HDSPM_statusRegister2) &
6687 HDSPM_s2_tco_detect) {
6688 hdspm->midiPorts++;
6689 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6690 GFP_KERNEL);
6691 if (NULL != hdspm->tco) {
6692 hdspm_tco_write(hdspm);
6693 }
6694 snd_printk(KERN_INFO "HDSPM: AIO/RayDAT TCO module found\n");
6695 } else {
6696 hdspm->tco = NULL;
6697 }
6698 break;
6699
6700 case MADI:
6701 if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
6702 hdspm->midiPorts++;
6703 hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
6704 GFP_KERNEL);
6705 if (NULL != hdspm->tco) {
6706 hdspm_tco_write(hdspm);
6707 }
6708 snd_printk(KERN_INFO "HDSPM: MADI TCO module found\n");
6709 } else {
6710 hdspm->tco = NULL;
6711 }
6712 break;
6713
6714 default:
6715 hdspm->tco = NULL;
6716 }
6717
6718 /* texts */
6719 switch (hdspm->io_type) {
6720 case AES32:
6721 if (hdspm->tco) {
6722 hdspm->texts_autosync = texts_autosync_aes_tco;
6723 hdspm->texts_autosync_items = 10;
6724 } else {
6725 hdspm->texts_autosync = texts_autosync_aes;
6726 hdspm->texts_autosync_items = 9;
6727 }
6728 break;
6729
6730 case MADI:
6731 if (hdspm->tco) {
6732 hdspm->texts_autosync = texts_autosync_madi_tco;
6733 hdspm->texts_autosync_items = 4;
6734 } else {
6735 hdspm->texts_autosync = texts_autosync_madi;
6736 hdspm->texts_autosync_items = 3;
6737 }
6738 break;
6739
6740 case MADIface:
6741
6742 break;
6743
6744 case RayDAT:
6745 if (hdspm->tco) {
6746 hdspm->texts_autosync = texts_autosync_raydat_tco;
6747 hdspm->texts_autosync_items = 9;
6748 } else {
6749 hdspm->texts_autosync = texts_autosync_raydat;
6750 hdspm->texts_autosync_items = 8;
6751 }
6752 break;
6753
6754 case AIO:
6755 if (hdspm->tco) {
6756 hdspm->texts_autosync = texts_autosync_aio_tco;
6757 hdspm->texts_autosync_items = 6;
6758 } else {
6759 hdspm->texts_autosync = texts_autosync_aio;
6760 hdspm->texts_autosync_items = 5;
6761 }
6762 break;
6763
6764 }
6765
6766 tasklet_init(&hdspm->midi_tasklet,
6767 hdspm_midi_tasklet, (unsigned long) hdspm);
Takashi Iwai763f3562005-06-03 11:25:34 +02006768
6769 snd_printdd("create alsa devices.\n");
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006770 err = snd_hdspm_create_alsa_devices(card, hdspm);
6771 if (err < 0)
Takashi Iwai763f3562005-06-03 11:25:34 +02006772 return err;
6773
6774 snd_hdspm_initialize_midi_flush(hdspm);
6775
6776 return 0;
6777}
6778
Adrian Knoth0dca1792011-01-26 19:32:14 +01006779
Takashi Iwai98274f02005-11-17 14:52:34 +01006780static int snd_hdspm_free(struct hdspm * hdspm)
Takashi Iwai763f3562005-06-03 11:25:34 +02006781{
6782
6783 if (hdspm->port) {
6784
6785 /* stop th audio, and cancel all interrupts */
6786 hdspm->control_register &=
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006787 ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
Adrian Knoth0dca1792011-01-26 19:32:14 +01006788 HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
6789 HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
Takashi Iwai763f3562005-06-03 11:25:34 +02006790 hdspm_write(hdspm, HDSPM_controlRegister,
6791 hdspm->control_register);
6792 }
6793
6794 if (hdspm->irq >= 0)
6795 free_irq(hdspm->irq, (void *) hdspm);
6796
Jesper Juhlfc584222005-10-24 15:11:28 +02006797 kfree(hdspm->mixer);
Takashi Iwai763f3562005-06-03 11:25:34 +02006798
6799 if (hdspm->iobase)
6800 iounmap(hdspm->iobase);
6801
Takashi Iwai763f3562005-06-03 11:25:34 +02006802 if (hdspm->port)
6803 pci_release_regions(hdspm->pci);
6804
6805 pci_disable_device(hdspm->pci);
6806 return 0;
6807}
6808
Adrian Knoth0dca1792011-01-26 19:32:14 +01006809
Takashi Iwai98274f02005-11-17 14:52:34 +01006810static void snd_hdspm_card_free(struct snd_card *card)
Takashi Iwai763f3562005-06-03 11:25:34 +02006811{
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006812 struct hdspm *hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006813
6814 if (hdspm)
6815 snd_hdspm_free(hdspm);
6816}
6817
Adrian Knoth0dca1792011-01-26 19:32:14 +01006818
Takashi Iwai763f3562005-06-03 11:25:34 +02006819static int __devinit snd_hdspm_probe(struct pci_dev *pci,
6820 const struct pci_device_id *pci_id)
6821{
6822 static int dev;
Takashi Iwai98274f02005-11-17 14:52:34 +01006823 struct hdspm *hdspm;
6824 struct snd_card *card;
Takashi Iwai763f3562005-06-03 11:25:34 +02006825 int err;
6826
6827 if (dev >= SNDRV_CARDS)
6828 return -ENODEV;
6829 if (!enable[dev]) {
6830 dev++;
6831 return -ENOENT;
6832 }
6833
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006834 err = snd_card_create(index[dev], id[dev],
Adrian Knoth0dca1792011-01-26 19:32:14 +01006835 THIS_MODULE, sizeof(struct hdspm), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01006836 if (err < 0)
6837 return err;
Takashi Iwai763f3562005-06-03 11:25:34 +02006838
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006839 hdspm = card->private_data;
Takashi Iwai763f3562005-06-03 11:25:34 +02006840 card->private_free = snd_hdspm_card_free;
6841 hdspm->dev = dev;
6842 hdspm->pci = pci;
6843
Takashi Iwaic187c042007-02-19 15:27:33 +01006844 snd_card_set_dev(card, &pci->dev);
6845
Adrian Knoth0dca1792011-01-26 19:32:14 +01006846 err = snd_hdspm_create(card, hdspm);
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006847 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006848 snd_card_free(card);
6849 return err;
6850 }
6851
Adrian Knoth0dca1792011-01-26 19:32:14 +01006852 if (hdspm->io_type != MADIface) {
6853 sprintf(card->shortname, "%s_%x",
6854 hdspm->card_name,
6855 (hdspm_read(hdspm, HDSPM_midiStatusIn0)>>8) & 0xFFFFFF);
6856 sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
6857 hdspm->card_name,
6858 (hdspm_read(hdspm, HDSPM_midiStatusIn0)>>8) & 0xFFFFFF,
6859 hdspm->port, hdspm->irq);
6860 } else {
6861 sprintf(card->shortname, "%s", hdspm->card_name);
6862 sprintf(card->longname, "%s at 0x%lx, irq %d",
6863 hdspm->card_name, hdspm->port, hdspm->irq);
6864 }
Takashi Iwai763f3562005-06-03 11:25:34 +02006865
Takashi Iwaief5fa1a2007-07-27 16:52:46 +02006866 err = snd_card_register(card);
6867 if (err < 0) {
Takashi Iwai763f3562005-06-03 11:25:34 +02006868 snd_card_free(card);
6869 return err;
6870 }
6871
6872 pci_set_drvdata(pci, card);
6873
6874 dev++;
6875 return 0;
6876}
6877
6878static void __devexit snd_hdspm_remove(struct pci_dev *pci)
6879{
6880 snd_card_free(pci_get_drvdata(pci));
6881 pci_set_drvdata(pci, NULL);
6882}
6883
6884static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02006885 .name = KBUILD_MODNAME,
Takashi Iwai763f3562005-06-03 11:25:34 +02006886 .id_table = snd_hdspm_ids,
6887 .probe = snd_hdspm_probe,
6888 .remove = __devexit_p(snd_hdspm_remove),
6889};
6890
6891
6892static int __init alsa_card_hdspm_init(void)
6893{
6894 return pci_register_driver(&driver);
6895}
6896
6897static void __exit alsa_card_hdspm_exit(void)
6898{
6899 pci_unregister_driver(&driver);
6900}
6901
6902module_init(alsa_card_hdspm_init)
6903module_exit(alsa_card_hdspm_exit)