hdspm.c 186.8 KB
Newer Older
T
Takashi Iwai 已提交
1
/*
T
Takashi Iwai 已提交
2 3 4 5 6 7
 *   ALSA driver for RME Hammerfall DSP MADI audio interface(s)
 *
 *      Copyright (c) 2003 Winfried Ritsch (IEM)
 *      code based on hdsp.c   Paul Davis
 *                             Marcus Andersson
 *                             Thomas Charbonnel
R
Remy Bruno 已提交
8 9
 *      Modified 2006-06-01 for AES32 support by Remy Bruno
 *                                               <remy.bruno@trinnov.com>
T
Takashi Iwai 已提交
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *      Modified 2009-04-13 for proper metering by Florian Faber
 *                                               <faber@faberman.de>
 *
 *      Modified 2009-04-14 for native float support by Florian Faber
 *                                               <faber@faberman.de>
 *
 *      Modified 2009-04-26 fixed bug in rms metering by Florian Faber
 *                                               <faber@faberman.de>
 *
 *      Modified 2009-04-30 added hw serial number support by Florian Faber
 *
 *      Modified 2011-01-14 added S/PDIF input on RayDATs by Adrian Knoth
 *
 *	Modified 2011-01-25 variable period sizes on RayDAT/AIO by Adrian Knoth
 *
T
Takashi Iwai 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/* *************    Register Documentation   *******************************************************
 *
 * Work in progress! Documentation is based on the code in this file.
 *
 * --------- HDSPM_controlRegister ---------
 * :7654.3210:7654.3210:7654.3210:7654.3210: bit number per byte
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :3322.2222:2222.1111:1111.1100:0000.0000: bit number
 * :1098.7654:3210.9876:5432.1098:7654.3210: 0..31
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :8421.8421:8421.8421:8421.8421:8421.8421: hex digit
 * :    .    :    .    :    .    :  x .    :  HDSPM_AudioInterruptEnable \_ setting both bits
 * :    .    :    .    :    .    :    .   x:  HDSPM_Start                /  enables audio IO
 * :    .    :    .    :    .    :   x.    :  HDSPM_ClockModeMaster - 1: Master, 0: Slave
 * :    .    :    .    :    .    :    .210 :  HDSPM_LatencyMask - 3 Bit value for latency
 * :    .    :    .    :    .    :    .    :      0:64, 1:128, 2:256, 3:512,
 * :    .    :    .    :    .    :    .    :      4:1024, 5:2048, 6:4096, 7:8192
 * :x   .    :    .    :    .   x:xx  .    :  HDSPM_FrequencyMask
 * :    .    :    .    :    .    :10  .    :  HDSPM_Frequency1|HDSPM_Frequency0: 1=32K,2=44.1K,3=48K,0=??
 * :    .    :    .    :    .   x:    .    :  <MADI> HDSPM_DoubleSpeed
 * :x   .    :    .    :    .    :    .    :  <MADI> HDSPM_QuadSpeed
 * :    .  3 :    .  10:  2 .    :    .    :  HDSPM_SyncRefMask :
 * :    .    :    .   x:    .    :    .    :  HDSPM_SyncRef0
 * :    .    :    .  x :    .    :    .    :  HDSPM_SyncRef1
 * :    .    :    .    :  x .    :    .    :  <AES32> HDSPM_SyncRef2
 * :    .  x :    .    :    .    :    .    :  <AES32> HDSPM_SyncRef3
 * :    .    :    .  10:    .    :    .    :  <MADI> sync ref: 0:WC, 1:Madi, 2:TCO, 3:SyncIn
 * :    .  3 :    .  10:  2 .    :    .    :  <AES32>  0:WC, 1:AES1 ... 8:AES8, 9: TCO, 10:SyncIn?
 * :    .  x :    .    :    .    :    .    :  <MADIe> HDSPe_FLOAT_FORMAT
 * :    .    :    .    : x  .    :    .    :  <MADI> HDSPM_InputSelect0 : 0=optical,1=coax
 * :    .    :    .    :x   .    :    .    :  <MADI> HDSPM_InputSelect1
 * :    .    :    .x   :    .    :    .    :  <MADI> HDSPM_clr_tms
 * :    .    :    .    :    . x  :    .    :  <MADI> HDSPM_TX_64ch
 * :    .    :    .    :    . x  :    .    :  <AES32> HDSPM_Emphasis
 * :    .    :    .    :    .x   :    .    :  <MADI> HDSPM_AutoInp
 * :    .    :    . x  :    .    :    .    :  <MADI> HDSPM_SMUX
 * :    .    :    .x   :    .    :    .    :  <MADI> HDSPM_clr_tms
 * :    .    :   x.    :    .    :    .    :  <MADI> HDSPM_taxi_reset
 * :    .   x:    .    :    .    :    .    :  <MADI> HDSPM_LineOut
 * :    .   x:    .    :    .    :    .    :  <AES32> ??????????????????
 * :    .    :   x.    :    .    :    .    :  <AES32> HDSPM_WCK48
 * :    .    :    .    :    .x   :    .    :  <AES32> HDSPM_Dolby
 * :    .    : x  .    :    .    :    .    :  HDSPM_Midi0InterruptEnable
 * :    .    :x   .    :    .    :    .    :  HDSPM_Midi1InterruptEnable
 * :    .    :  x .    :    .    :    .    :  HDSPM_Midi2InterruptEnable
 * :    . x  :    .    :    .    :    .    :  <MADI> HDSPM_Midi3InterruptEnable
 * :    . x  :    .    :    .    :    .    :  <AES32> HDSPM_DS_DoubleWire
 * :    .x   :    .    :    .    :    .    :  <AES32> HDSPM_QS_DoubleWire
 * :   x.    :    .    :    .    :    .    :  <AES32> HDSPM_QS_QuadWire
 * :    .    :    .    :    .  x :    .    :  <AES32> HDSPM_Professional
 * : x  .    :    .    :    .    :    .    :  HDSPM_wclk_sel
 * :    .    :    .    :    .    :    .    :
 * :7654.3210:7654.3210:7654.3210:7654.3210: bit number per byte
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :3322.2222:2222.1111:1111.1100:0000.0000: bit number
 * :1098.7654:3210.9876:5432.1098:7654.3210: 0..31
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :8421.8421:8421.8421:8421.8421:8421.8421:hex digit
 *
 *
 *
 * AIO / RayDAT only
 *
 * ------------ HDSPM_WR_SETTINGS ----------
 * :3322.2222:2222.1111:1111.1100:0000.0000: bit number per byte
 * :1098.7654:3210.9876:5432.1098:7654.3210:
 * :||||.||||:||||.||||:||||.||||:||||.||||: bit number
 * :7654.3210:7654.3210:7654.3210:7654.3210: 0..31
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :8421.8421:8421.8421:8421.8421:8421.8421: hex digit
 * :    .    :    .    :    .    :    .   x: HDSPM_c0Master 1: Master, 0: Slave
 * :    .    :    .    :    .    :    .  x : HDSPM_c0_SyncRef0
 * :    .    :    .    :    .    :    . x  : HDSPM_c0_SyncRef1
 * :    .    :    .    :    .    :    .x   : HDSPM_c0_SyncRef2
 * :    .    :    .    :    .    :   x.    : HDSPM_c0_SyncRef3
 * :    .    :    .    :    .    :   3.210 : HDSPM_c0_SyncRefMask:
 * :    .    :    .    :    .    :    .    :  RayDat: 0:WC, 1:AES, 2:SPDIF, 3..6: ADAT1..4,
 * :    .    :    .    :    .    :    .    :          9:TCO, 10:SyncIn
 * :    .    :    .    :    .    :    .    :  AIO: 0:WC, 1:AES, 2: SPDIF, 3: ATAT,
 * :    .    :    .    :    .    :    .    :          9:TCO, 10:SyncIn
 * :    .    :    .    :    .    :    .    :
 * :    .    :    .    :    .    :    .    :
 * :3322.2222:2222.1111:1111.1100:0000.0000: bit number per byte
 * :1098.7654:3210.9876:5432.1098:7654.3210:
 * :||||.||||:||||.||||:||||.||||:||||.||||: bit number
 * :7654.3210:7654.3210:7654.3210:7654.3210: 0..31
 * :||||.||||:||||.||||:||||.||||:||||.||||:
 * :8421.8421:8421.8421:8421.8421:8421.8421: hex digit
 *
 */
T
Takashi Iwai 已提交
132 133 134
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
135
#include <linux/module.h>
T
Takashi Iwai 已提交
136 137
#include <linux/slab.h>
#include <linux/pci.h>
138
#include <linux/math64.h>
T
Takashi Iwai 已提交
139 140 141 142 143
#include <asm/io.h>

#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
144
#include <sound/pcm_params.h>
T
Takashi Iwai 已提交
145 146 147 148 149 150 151 152 153 154
#include <sound/info.h>
#include <sound/asoundef.h>
#include <sound/rawmidi.h>
#include <sound/hwdep.h>
#include <sound/initval.h>

#include <sound/hdspm.h>

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	  /* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	  /* ID for this card */
155
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
T
Takashi Iwai 已提交
156 157 158 159 160 161 162 163 164 165 166 167

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for RME HDSPM interface.");

module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for RME HDSPM interface.");

module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable/disable specific HDSPM soundcards.");


MODULE_AUTHOR
168 169 170 171 172 173 174 175
(
	"Winfried Ritsch <ritsch_AT_iem.at>, "
	"Paul Davis <paul@linuxaudiosystems.com>, "
	"Marcus Andersson, Thomas Charbonnel <thomas@undata.org>, "
	"Remy Bruno <remy.bruno@trinnov.com>, "
	"Florian Faber <faberman@linuxproaudio.org>, "
	"Adrian Knoth <adi@drcomp.erfurt.thur.de>"
);
T
Takashi Iwai 已提交
176 177 178 179
MODULE_DESCRIPTION("RME HDSPM");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");

180
/* --- Write registers. ---
T
Takashi Iwai 已提交
181 182
  These are defined as byte-offsets from the iobase value.  */

183 184 185
#define HDSPM_WR_SETTINGS             0
#define HDSPM_outputBufferAddress    32
#define HDSPM_inputBufferAddress     36
T
Takashi Iwai 已提交
186 187 188
#define HDSPM_controlRegister	     64
#define HDSPM_interruptConfirmation  96
#define HDSPM_control2Reg	     256  /* not in specs ???????? */
189
#define HDSPM_freqReg                256  /* for setting arbitrary clock values (DDS feature) */
190 191
#define HDSPM_midiDataOut0	     352  /* just believe in old code */
#define HDSPM_midiDataOut1	     356
192
#define HDSPM_eeprom_wr		     384  /* for AES32 */
T
Takashi Iwai 已提交
193 194

/* DMA enable for 64 channels, only Bit 0 is relevant */
195
#define HDSPM_outputEnableBase       512  /* 512-767  input  DMA */
T
Takashi Iwai 已提交
196 197
#define HDSPM_inputEnableBase        768  /* 768-1023 output DMA */

198
/* 16 page addresses for each of the 64 channels DMA buffer in and out
T
Takashi Iwai 已提交
199 200 201 202 203 204 205 206 207 208 209
   (each 64k=16*4k) Buffer must be 4k aligned (which is default i386 ????) */
#define HDSPM_pageAddressBufferOut       8192
#define HDSPM_pageAddressBufferIn        (HDSPM_pageAddressBufferOut+64*16*4)

#define HDSPM_MADI_mixerBase    32768	/* 32768-65535 for 2x64x64 Fader */

#define HDSPM_MATRIX_MIXER_SIZE  8192	/* = 2*64*64 * 4 Byte => 32kB */

/* --- Read registers. ---
   These are defined as byte-offsets from the iobase value */
#define HDSPM_statusRegister    0
R
Remy Bruno 已提交
210 211 212 213 214 215
/*#define HDSPM_statusRegister2  96 */
/* after RME Windows driver sources, status2 is 4-byte word # 48 = word at
 * offset 192, for AES32 *and* MADI
 * => need to check that offset 192 is working on MADI */
#define HDSPM_statusRegister2  192
#define HDSPM_timecodeRegister 128
T
Takashi Iwai 已提交
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/* AIO, RayDAT */
#define HDSPM_RD_STATUS_0 0
#define HDSPM_RD_STATUS_1 64
#define HDSPM_RD_STATUS_2 128
#define HDSPM_RD_STATUS_3 192

#define HDSPM_RD_TCO           256
#define HDSPM_RD_PLL_FREQ      512
#define HDSPM_WR_TCO           128

#define HDSPM_TCO1_TCO_lock			0x00000001
#define HDSPM_TCO1_WCK_Input_Range_LSB		0x00000002
#define HDSPM_TCO1_WCK_Input_Range_MSB		0x00000004
#define HDSPM_TCO1_LTC_Input_valid		0x00000008
#define HDSPM_TCO1_WCK_Input_valid		0x00000010
#define HDSPM_TCO1_Video_Input_Format_NTSC	0x00000020
#define HDSPM_TCO1_Video_Input_Format_PAL	0x00000040

#define HDSPM_TCO1_set_TC			0x00000100
#define HDSPM_TCO1_set_drop_frame_flag		0x00000200
#define HDSPM_TCO1_LTC_Format_LSB		0x00000400
#define HDSPM_TCO1_LTC_Format_MSB		0x00000800

#define HDSPM_TCO2_TC_run			0x00010000
#define HDSPM_TCO2_WCK_IO_ratio_LSB		0x00020000
#define HDSPM_TCO2_WCK_IO_ratio_MSB		0x00040000
#define HDSPM_TCO2_set_num_drop_frames_LSB	0x00080000
#define HDSPM_TCO2_set_num_drop_frames_MSB	0x00100000
#define HDSPM_TCO2_set_jam_sync			0x00200000
#define HDSPM_TCO2_set_flywheel			0x00400000

#define HDSPM_TCO2_set_01_4			0x01000000
#define HDSPM_TCO2_set_pull_down		0x02000000
#define HDSPM_TCO2_set_pull_up			0x04000000
#define HDSPM_TCO2_set_freq			0x08000000
#define HDSPM_TCO2_set_term_75R			0x10000000
#define HDSPM_TCO2_set_input_LSB		0x20000000
#define HDSPM_TCO2_set_input_MSB		0x40000000
#define HDSPM_TCO2_set_freq_from_app		0x80000000


#define HDSPM_midiDataOut0    352
#define HDSPM_midiDataOut1    356
#define HDSPM_midiDataOut2    368

T
Takashi Iwai 已提交
262 263
#define HDSPM_midiDataIn0     360
#define HDSPM_midiDataIn1     364
264 265
#define HDSPM_midiDataIn2     372
#define HDSPM_midiDataIn3     376
T
Takashi Iwai 已提交
266 267

/* status is data bytes in MIDI-FIFO (0-128) */
268 269 270 271 272 273 274 275
#define HDSPM_midiStatusOut0  384
#define HDSPM_midiStatusOut1  388
#define HDSPM_midiStatusOut2  400

#define HDSPM_midiStatusIn0   392
#define HDSPM_midiStatusIn1   396
#define HDSPM_midiStatusIn2   404
#define HDSPM_midiStatusIn3   408
T
Takashi Iwai 已提交
276 277 278 279


/* the meters are regular i/o-mapped registers, but offset
   considerably from the rest. the peak registers are reset
280
   when read; the least-significant 4 bits are full-scale counters;
T
Takashi Iwai 已提交
281 282
   the actual peak value is in the most-significant 24 bits.
*/
283 284 285 286 287 288 289 290 291 292 293 294

#define HDSPM_MADI_INPUT_PEAK		4096
#define HDSPM_MADI_PLAYBACK_PEAK	4352
#define HDSPM_MADI_OUTPUT_PEAK		4608

#define HDSPM_MADI_INPUT_RMS_L		6144
#define HDSPM_MADI_PLAYBACK_RMS_L	6400
#define HDSPM_MADI_OUTPUT_RMS_L		6656

#define HDSPM_MADI_INPUT_RMS_H		7168
#define HDSPM_MADI_PLAYBACK_RMS_H	7424
#define HDSPM_MADI_OUTPUT_RMS_H		7680
T
Takashi Iwai 已提交
295 296 297 298 299 300 301 302

/* --- Control Register bits --------- */
#define HDSPM_Start                (1<<0) /* start engine */

#define HDSPM_Latency0             (1<<1) /* buffer size = 2^n */
#define HDSPM_Latency1             (1<<2) /* where n is defined */
#define HDSPM_Latency2             (1<<3) /* by Latency{2,1,0} */

303 304 305
#define HDSPM_ClockModeMaster      (1<<4) /* 1=Master, 0=Autosync */
#define HDSPM_c0Master		0x1    /* Master clock bit in settings
					  register [RayDAT, AIO] */
T
Takashi Iwai 已提交
306 307 308 309 310 311

#define HDSPM_AudioInterruptEnable (1<<5) /* what do you think ? */

#define HDSPM_Frequency0  (1<<6)  /* 0=44.1kHz/88.2kHz 1=48kHz/96kHz */
#define HDSPM_Frequency1  (1<<7)  /* 0=32kHz/64kHz */
#define HDSPM_DoubleSpeed (1<<8)  /* 0=normal speed, 1=double speed */
R
Remy Bruno 已提交
312
#define HDSPM_QuadSpeed   (1<<31) /* quad speed bit */
T
Takashi Iwai 已提交
313

R
Remy Bruno 已提交
314
#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
T
Takashi Iwai 已提交
315
#define HDSPM_TX_64ch     (1<<10) /* Output 64channel MODE=1,
R
Remy Bruno 已提交
316 317
				     56channelMODE=0 */ /* MADI ONLY*/
#define HDSPM_Emphasis    (1<<10) /* Emphasis */ /* AES32 ONLY */
T
Takashi Iwai 已提交
318

319
#define HDSPM_AutoInp     (1<<11) /* Auto Input (takeover) == Safe Mode,
R
Remy Bruno 已提交
320 321
                                     0=off, 1=on  */ /* MADI ONLY */
#define HDSPM_Dolby       (1<<11) /* Dolby = "NonAudio" ?? */ /* AES32 ONLY */
T
Takashi Iwai 已提交
322

T
Takashi Iwai 已提交
323 324 325
#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
				    * -- MADI ONLY
				    */
T
Takashi Iwai 已提交
326 327
#define HDSPM_InputSelect1 (1<<15) /* should be 0 */

R
Remy Bruno 已提交
328 329
#define HDSPM_SyncRef2     (1<<13)
#define HDSPM_SyncRef3     (1<<25)
T
Takashi Iwai 已提交
330

R
Remy Bruno 已提交
331
#define HDSPM_SMUX         (1<<18) /* Frame ??? */ /* MADI ONY */
332
#define HDSPM_clr_tms      (1<<19) /* clear track marker, do not use
T
Takashi Iwai 已提交
333 334
                                      AES additional bits in
				      lower 5 Audiodatabits ??? */
R
Remy Bruno 已提交
335 336
#define HDSPM_taxi_reset   (1<<20) /* ??? */ /* MADI ONLY ? */
#define HDSPM_WCK48        (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
T
Takashi Iwai 已提交
337

338 339 340 341
#define HDSPM_Midi0InterruptEnable 0x0400000
#define HDSPM_Midi1InterruptEnable 0x0800000
#define HDSPM_Midi2InterruptEnable 0x0200000
#define HDSPM_Midi3InterruptEnable 0x4000000
T
Takashi Iwai 已提交
342 343

#define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */
344
#define HDSPe_FLOAT_FORMAT         0x2000000
T
Takashi Iwai 已提交
345

R
Remy Bruno 已提交
346 347 348 349 350
#define HDSPM_DS_DoubleWire (1<<26) /* AES32 ONLY */
#define HDSPM_QS_DoubleWire (1<<27) /* AES32 ONLY */
#define HDSPM_QS_QuadWire   (1<<28) /* AES32 ONLY */

#define HDSPM_wclk_sel (1<<30)
T
Takashi Iwai 已提交
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/* additional control register bits for AIO*/
#define HDSPM_c0_Wck48				0x20 /* also RayDAT */
#define HDSPM_c0_Input0				0x1000
#define HDSPM_c0_Input1				0x2000
#define HDSPM_c0_Spdif_Opt			0x4000
#define HDSPM_c0_Pro				0x8000
#define HDSPM_c0_clr_tms			0x10000
#define HDSPM_c0_AEB1				0x20000
#define HDSPM_c0_AEB2				0x40000
#define HDSPM_c0_LineOut			0x80000
#define HDSPM_c0_AD_GAIN0			0x100000
#define HDSPM_c0_AD_GAIN1			0x200000
#define HDSPM_c0_DA_GAIN0			0x400000
#define HDSPM_c0_DA_GAIN1			0x800000
#define HDSPM_c0_PH_GAIN0			0x1000000
#define HDSPM_c0_PH_GAIN1			0x2000000
#define HDSPM_c0_Sym6db				0x4000000


T
Takashi Iwai 已提交
371 372
/* --- bit helper defines */
#define HDSPM_LatencyMask    (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
T
Takashi Iwai 已提交
373 374
#define HDSPM_FrequencyMask  (HDSPM_Frequency0|HDSPM_Frequency1|\
			      HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
T
Takashi Iwai 已提交
375 376 377
#define HDSPM_InputMask      (HDSPM_InputSelect0|HDSPM_InputSelect1)
#define HDSPM_InputOptical   0
#define HDSPM_InputCoaxial   (HDSPM_InputSelect0)
T
Takashi Iwai 已提交
378 379
#define HDSPM_SyncRefMask    (HDSPM_SyncRef0|HDSPM_SyncRef1|\
			      HDSPM_SyncRef2|HDSPM_SyncRef3)
T
Takashi Iwai 已提交
380

381 382 383 384 385 386 387 388 389 390 391
#define HDSPM_c0_SyncRef0      0x2
#define HDSPM_c0_SyncRef1      0x4
#define HDSPM_c0_SyncRef2      0x8
#define HDSPM_c0_SyncRef3      0x10
#define HDSPM_c0_SyncRefMask   (HDSPM_c0_SyncRef0 | HDSPM_c0_SyncRef1 |\
				HDSPM_c0_SyncRef2 | HDSPM_c0_SyncRef3)

#define HDSPM_SYNC_FROM_WORD    0	/* Preferred sync reference */
#define HDSPM_SYNC_FROM_MADI    1	/* choices - used by "pref_sync_ref" */
#define HDSPM_SYNC_FROM_TCO     2
#define HDSPM_SYNC_FROM_SYNC_IN 3
T
Takashi Iwai 已提交
392 393 394 395 396 397

#define HDSPM_Frequency32KHz    HDSPM_Frequency0
#define HDSPM_Frequency44_1KHz  HDSPM_Frequency1
#define HDSPM_Frequency48KHz   (HDSPM_Frequency1|HDSPM_Frequency0)
#define HDSPM_Frequency64KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency0)
#define HDSPM_Frequency88_2KHz (HDSPM_DoubleSpeed|HDSPM_Frequency1)
T
Takashi Iwai 已提交
398 399
#define HDSPM_Frequency96KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
				HDSPM_Frequency0)
R
Remy Bruno 已提交
400 401
#define HDSPM_Frequency128KHz   (HDSPM_QuadSpeed|HDSPM_Frequency0)
#define HDSPM_Frequency176_4KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1)
T
Takashi Iwai 已提交
402 403
#define HDSPM_Frequency192KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1|\
				 HDSPM_Frequency0)
T
Takashi Iwai 已提交
404 405 406 407 408 409 410 411 412 413


/* Synccheck Status */
#define HDSPM_SYNC_CHECK_NO_LOCK 0
#define HDSPM_SYNC_CHECK_LOCK    1
#define HDSPM_SYNC_CHECK_SYNC	 2

/* AutoSync References - used by "autosync_ref" control switch */
#define HDSPM_AUTOSYNC_FROM_WORD      0
#define HDSPM_AUTOSYNC_FROM_MADI      1
414 415 416
#define HDSPM_AUTOSYNC_FROM_TCO       2
#define HDSPM_AUTOSYNC_FROM_SYNC_IN   3
#define HDSPM_AUTOSYNC_FROM_NONE      4
T
Takashi Iwai 已提交
417 418 419 420 421 422

/* Possible sources of MADI input */
#define HDSPM_OPTICAL 0		/* optical   */
#define HDSPM_COAXIAL 1		/* BNC */

#define hdspm_encode_latency(x)       (((x)<<1) & HDSPM_LatencyMask)
423
#define hdspm_decode_latency(x)       ((((x) & HDSPM_LatencyMask)>>1))
T
Takashi Iwai 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

#define hdspm_encode_in(x) (((x)&0x3)<<14)
#define hdspm_decode_in(x) (((x)>>14)&0x3)

/* --- control2 register bits --- */
#define HDSPM_TMS             (1<<0)
#define HDSPM_TCK             (1<<1)
#define HDSPM_TDI             (1<<2)
#define HDSPM_JTAG            (1<<3)
#define HDSPM_PWDN            (1<<4)
#define HDSPM_PROGRAM	      (1<<5)
#define HDSPM_CONFIG_MODE_0   (1<<6)
#define HDSPM_CONFIG_MODE_1   (1<<7)
/*#define HDSPM_VERSION_BIT     (1<<8) not defined any more*/
#define HDSPM_BIGENDIAN_MODE  (1<<9)
#define HDSPM_RD_MULTIPLE     (1<<10)

R
Remy Bruno 已提交
441
/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
T
Takashi Iwai 已提交
442 443 444
     that do not conflict with specific bits for AES32 seem to be valid also
     for the AES32
 */
T
Takashi Iwai 已提交
445
#define HDSPM_audioIRQPending    (1<<0)	/* IRQ is high and pending */
T
Takashi Iwai 已提交
446 447 448 449
#define HDSPM_RX_64ch            (1<<1)	/* Input 64chan. MODE=1, 56chn MODE=0 */
#define HDSPM_AB_int             (1<<2)	/* InputChannel Opt=0, Coax=1
					 * (like inp0)
					 */
450

T
Takashi Iwai 已提交
451
#define HDSPM_madiLock           (1<<3)	/* MADI Locked =1, no=0 */
452 453
#define HDSPM_madiSync          (1<<18) /* MADI is in sync */

454 455
#define HDSPM_tcoLockMadi    0x00000020 /* Optional TCO locked status for HDSPe MADI*/
#define HDSPM_tcoSync    0x10000000 /* Optional TCO sync status for HDSPe MADI and AES32!*/
456

457 458
#define HDSPM_syncInLock 0x00010000 /* Sync In lock status for HDSPe MADI! */
#define HDSPM_syncInSync 0x00020000 /* Sync In sync status for HDSPe MADI! */
T
Takashi Iwai 已提交
459 460

#define HDSPM_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
461 462 463
			/* since 64byte accurate, last 6 bits are not used */


T
Takashi Iwai 已提交
464 465 466 467 468 469 470 471

#define HDSPM_DoubleSpeedStatus (1<<19) /* (input) card in double speed */

#define HDSPM_madiFreq0         (1<<22)	/* system freq 0=error */
#define HDSPM_madiFreq1         (1<<23)	/* 1=32, 2=44.1 3=48 */
#define HDSPM_madiFreq2         (1<<24)	/* 4=64, 5=88.2 6=96 */
#define HDSPM_madiFreq3         (1<<25)	/* 7=128, 8=176.4 9=192 */

T
Takashi Iwai 已提交
472 473 474
#define HDSPM_BufferID          (1<<26)	/* (Double)Buffer ID toggles with
					 * Interrupt
					 */
475
#define HDSPM_tco_detect         0x08000000
476
#define HDSPM_tcoLockAes         0x20000000 /* Optional TCO locked status for HDSPe AES */
477 478 479 480 481 482 483 484 485 486 487

#define HDSPM_s2_tco_detect      0x00000040
#define HDSPM_s2_AEBO_D          0x00000080
#define HDSPM_s2_AEBI_D          0x00000100


#define HDSPM_midi0IRQPending    0x40000000
#define HDSPM_midi1IRQPending    0x80000000
#define HDSPM_midi2IRQPending    0x20000000
#define HDSPM_midi2IRQPendingAES 0x00000020
#define HDSPM_midi3IRQPending    0x00200000
T
Takashi Iwai 已提交
488 489

/* --- status bit helpers */
T
Takashi Iwai 已提交
490 491
#define HDSPM_madiFreqMask  (HDSPM_madiFreq0|HDSPM_madiFreq1|\
			     HDSPM_madiFreq2|HDSPM_madiFreq3)
T
Takashi Iwai 已提交
492 493 494 495 496 497 498 499 500 501
#define HDSPM_madiFreq32    (HDSPM_madiFreq0)
#define HDSPM_madiFreq44_1  (HDSPM_madiFreq1)
#define HDSPM_madiFreq48    (HDSPM_madiFreq0|HDSPM_madiFreq1)
#define HDSPM_madiFreq64    (HDSPM_madiFreq2)
#define HDSPM_madiFreq88_2  (HDSPM_madiFreq0|HDSPM_madiFreq2)
#define HDSPM_madiFreq96    (HDSPM_madiFreq1|HDSPM_madiFreq2)
#define HDSPM_madiFreq128   (HDSPM_madiFreq0|HDSPM_madiFreq1|HDSPM_madiFreq2)
#define HDSPM_madiFreq176_4 (HDSPM_madiFreq3)
#define HDSPM_madiFreq192   (HDSPM_madiFreq3|HDSPM_madiFreq0)

R
Remy Bruno 已提交
502
/* Status2 Register bits */ /* MADI ONLY */
T
Takashi Iwai 已提交
503

L
Lucas De Marchi 已提交
504
#define HDSPM_version0 (1<<0)	/* not really defined but I guess */
T
Takashi Iwai 已提交
505 506 507 508 509 510 511 512
#define HDSPM_version1 (1<<1)	/* in former cards it was ??? */
#define HDSPM_version2 (1<<2)

#define HDSPM_wcLock (1<<3)	/* Wordclock is detected and locked */
#define HDSPM_wcSync (1<<4)	/* Wordclock is in sync with systemclock */

#define HDSPM_wc_freq0 (1<<5)	/* input freq detected via autosync  */
#define HDSPM_wc_freq1 (1<<6)	/* 001=32, 010==44.1, 011=48, */
513 514
#define HDSPM_wc_freq2 (1<<7)	/* 100=64, 101=88.2, 110=96, 111=128 */
#define HDSPM_wc_freq3 0x800	/* 1000=176.4, 1001=192 */
T
Takashi Iwai 已提交
515

516 517 518 519
#define HDSPM_SyncRef0 0x10000  /* Sync Reference */
#define HDSPM_SyncRef1 0x20000

#define HDSPM_SelSyncRef0 (1<<8)	/* AutoSync Source */
T
Takashi Iwai 已提交
520 521 522 523 524
#define HDSPM_SelSyncRef1 (1<<9)	/* 000=word, 001=MADI, */
#define HDSPM_SelSyncRef2 (1<<10)	/* 111=no valid signal */

#define HDSPM_wc_valid (HDSPM_wcLock|HDSPM_wcSync)

525 526
#define HDSPM_wcFreqMask  (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2|\
			    HDSPM_wc_freq3)
T
Takashi Iwai 已提交
527 528 529 530 531 532
#define HDSPM_wcFreq32    (HDSPM_wc_freq0)
#define HDSPM_wcFreq44_1  (HDSPM_wc_freq1)
#define HDSPM_wcFreq48    (HDSPM_wc_freq0|HDSPM_wc_freq1)
#define HDSPM_wcFreq64    (HDSPM_wc_freq2)
#define HDSPM_wcFreq88_2  (HDSPM_wc_freq0|HDSPM_wc_freq2)
#define HDSPM_wcFreq96    (HDSPM_wc_freq1|HDSPM_wc_freq2)
533 534 535
#define HDSPM_wcFreq128   (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2)
#define HDSPM_wcFreq176_4 (HDSPM_wc_freq3)
#define HDSPM_wcFreq192   (HDSPM_wc_freq0|HDSPM_wc_freq3)
T
Takashi Iwai 已提交
536

537 538 539 540 541 542
#define HDSPM_status1_F_0 0x0400000
#define HDSPM_status1_F_1 0x0800000
#define HDSPM_status1_F_2 0x1000000
#define HDSPM_status1_F_3 0x2000000
#define HDSPM_status1_freqMask (HDSPM_status1_F_0|HDSPM_status1_F_1|HDSPM_status1_F_2|HDSPM_status1_F_3)

T
Takashi Iwai 已提交
543

T
Takashi Iwai 已提交
544 545
#define HDSPM_SelSyncRefMask       (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
				    HDSPM_SelSyncRef2)
T
Takashi Iwai 已提交
546 547
#define HDSPM_SelSyncRef_WORD      0
#define HDSPM_SelSyncRef_MADI      (HDSPM_SelSyncRef0)
548 549
#define HDSPM_SelSyncRef_TCO       (HDSPM_SelSyncRef1)
#define HDSPM_SelSyncRef_SyncIn    (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1)
T
Takashi Iwai 已提交
550 551
#define HDSPM_SelSyncRef_NVALID    (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
				    HDSPM_SelSyncRef2)
T
Takashi Iwai 已提交
552

R
Remy Bruno 已提交
553 554 555 556 557
/*
   For AES32, bits for status, status2 and timecode are different
*/
/* status */
#define HDSPM_AES32_wcLock	0x0200000
558
#define HDSPM_AES32_wcSync	0x0100000
R
Remy Bruno 已提交
559
#define HDSPM_AES32_wcFreq_bit  22
560
/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function
R
Remy Bruno 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573
  HDSPM_bit2freq */
#define HDSPM_AES32_syncref_bit  16
/* (status >> HDSPM_AES32_syncref_bit) & 0xF gives sync source */

#define HDSPM_AES32_AUTOSYNC_FROM_WORD 0
#define HDSPM_AES32_AUTOSYNC_FROM_AES1 1
#define HDSPM_AES32_AUTOSYNC_FROM_AES2 2
#define HDSPM_AES32_AUTOSYNC_FROM_AES3 3
#define HDSPM_AES32_AUTOSYNC_FROM_AES4 4
#define HDSPM_AES32_AUTOSYNC_FROM_AES5 5
#define HDSPM_AES32_AUTOSYNC_FROM_AES6 6
#define HDSPM_AES32_AUTOSYNC_FROM_AES7 7
#define HDSPM_AES32_AUTOSYNC_FROM_AES8 8
574 575 576
#define HDSPM_AES32_AUTOSYNC_FROM_TCO 9
#define HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN 10
#define HDSPM_AES32_AUTOSYNC_FROM_NONE 11
R
Remy Bruno 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

/*  status2 */
/* HDSPM_LockAES_bit is given by HDSPM_LockAES >> (AES# - 1) */
#define HDSPM_LockAES   0x80
#define HDSPM_LockAES1  0x80
#define HDSPM_LockAES2  0x40
#define HDSPM_LockAES3  0x20
#define HDSPM_LockAES4  0x10
#define HDSPM_LockAES5  0x8
#define HDSPM_LockAES6  0x4
#define HDSPM_LockAES7  0x2
#define HDSPM_LockAES8  0x1
/*
   Timecode
   After windows driver sources, bits 4*i to 4*i+3 give the input frequency on
   AES i+1
 bits 3210
      0001  32kHz
      0010  44.1kHz
      0011  48kHz
      0100  64kHz
      0101  88.2kHz
      0110  96kHz
      0111  128kHz
      1000  176.4kHz
      1001  192kHz
  NB: Timecode register doesn't seem to work on AES32 card revision 230
*/

T
Takashi Iwai 已提交
606 607 608 609 610 611 612 613 614
/* Mixer Values */
#define UNITY_GAIN          32768	/* = 65536/2 */
#define MINUS_INFINITY_GAIN 0

/* Number of channels for different Speed Modes */
#define MADI_SS_CHANNELS       64
#define MADI_DS_CHANNELS       32
#define MADI_QS_CHANNELS       16

615 616 617 618 619 620 621 622 623 624 625
#define RAYDAT_SS_CHANNELS     36
#define RAYDAT_DS_CHANNELS     20
#define RAYDAT_QS_CHANNELS     12

#define AIO_IN_SS_CHANNELS        14
#define AIO_IN_DS_CHANNELS        10
#define AIO_IN_QS_CHANNELS        8
#define AIO_OUT_SS_CHANNELS        16
#define AIO_OUT_DS_CHANNELS        12
#define AIO_OUT_QS_CHANNELS        10

626 627
#define AES32_CHANNELS		16

T
Takashi Iwai 已提交
628 629 630 631 632 633
/* the size of a substream (1 mono data stream) */
#define HDSPM_CHANNEL_BUFFER_SAMPLES  (16*1024)
#define HDSPM_CHANNEL_BUFFER_BYTES    (4*HDSPM_CHANNEL_BUFFER_SAMPLES)

/* the size of the area we need to allocate for DMA transfers. the
   size is the same regardless of the number of channels, and
634
   also the latency to use.
T
Takashi Iwai 已提交
635 636
   for one direction !!!
*/
637
#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
T
Takashi Iwai 已提交
638 639
#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)

640 641 642
#define HDSPM_RAYDAT_REV	211
#define HDSPM_AIO_REV		212
#define HDSPM_MADIFACE_REV	213
R
Remy Bruno 已提交
643

R
Remy Bruno 已提交
644 645 646 647
/* speed factor modes */
#define HDSPM_SPEED_SINGLE 0
#define HDSPM_SPEED_DOUBLE 1
#define HDSPM_SPEED_QUAD   2
648

R
Remy Bruno 已提交
649 650 651
/* names for speed modes */
static char *hdspm_speed_names[] = { "single", "double", "quad" };

652
static const char *const texts_autosync_aes_tco[] = { "Word Clock",
653 654
					  "AES1", "AES2", "AES3", "AES4",
					  "AES5", "AES6", "AES7", "AES8",
655 656
					  "TCO", "Sync In"
};
657
static const char *const texts_autosync_aes[] = { "Word Clock",
658
				      "AES1", "AES2", "AES3", "AES4",
659 660 661
				      "AES5", "AES6", "AES7", "AES8",
				      "Sync In"
};
662
static const char *const texts_autosync_madi_tco[] = { "Word Clock",
663
					   "MADI", "TCO", "Sync In" };
664
static const char *const texts_autosync_madi[] = { "Word Clock",
665 666
				       "MADI", "Sync In" };

667
static const char *const texts_autosync_raydat_tco[] = {
668 669 670 671
	"Word Clock",
	"ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
	"AES", "SPDIF", "TCO", "Sync In"
};
672
static const char *const texts_autosync_raydat[] = {
673 674 675 676
	"Word Clock",
	"ADAT 1", "ADAT 2", "ADAT 3", "ADAT 4",
	"AES", "SPDIF", "Sync In"
};
677
static const char *const texts_autosync_aio_tco[] = {
678 679 680
	"Word Clock",
	"ADAT", "AES", "SPDIF", "TCO", "Sync In"
};
681
static const char *const texts_autosync_aio[] = { "Word Clock",
682 683
				      "ADAT", "AES", "SPDIF", "Sync In" };

684
static const char *const texts_freq[] = {
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	"No Lock",
	"32 kHz",
	"44.1 kHz",
	"48 kHz",
	"64 kHz",
	"88.2 kHz",
	"96 kHz",
	"128 kHz",
	"176.4 kHz",
	"192 kHz"
};

static char *texts_ports_madi[] = {
	"MADI.1", "MADI.2", "MADI.3", "MADI.4", "MADI.5", "MADI.6",
	"MADI.7", "MADI.8", "MADI.9", "MADI.10", "MADI.11", "MADI.12",
	"MADI.13", "MADI.14", "MADI.15", "MADI.16", "MADI.17", "MADI.18",
	"MADI.19", "MADI.20", "MADI.21", "MADI.22", "MADI.23", "MADI.24",
	"MADI.25", "MADI.26", "MADI.27", "MADI.28", "MADI.29", "MADI.30",
	"MADI.31", "MADI.32", "MADI.33", "MADI.34", "MADI.35", "MADI.36",
	"MADI.37", "MADI.38", "MADI.39", "MADI.40", "MADI.41", "MADI.42",
	"MADI.43", "MADI.44", "MADI.45", "MADI.46", "MADI.47", "MADI.48",
	"MADI.49", "MADI.50", "MADI.51", "MADI.52", "MADI.53", "MADI.54",
	"MADI.55", "MADI.56", "MADI.57", "MADI.58", "MADI.59", "MADI.60",
	"MADI.61", "MADI.62", "MADI.63", "MADI.64",
};


static char *texts_ports_raydat_ss[] = {
	"ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4", "ADAT1.5", "ADAT1.6",
	"ADAT1.7", "ADAT1.8", "ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
	"ADAT2.5", "ADAT2.6", "ADAT2.7", "ADAT2.8", "ADAT3.1", "ADAT3.2",
	"ADAT3.3", "ADAT3.4", "ADAT3.5", "ADAT3.6", "ADAT3.7", "ADAT3.8",
	"ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4", "ADAT4.5", "ADAT4.6",
	"ADAT4.7", "ADAT4.8",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R"
};

static char *texts_ports_raydat_ds[] = {
	"ADAT1.1", "ADAT1.2", "ADAT1.3", "ADAT1.4",
	"ADAT2.1", "ADAT2.2", "ADAT2.3", "ADAT2.4",
	"ADAT3.1", "ADAT3.2", "ADAT3.3", "ADAT3.4",
	"ADAT4.1", "ADAT4.2", "ADAT4.3", "ADAT4.4",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R"
};

static char *texts_ports_raydat_qs[] = {
	"ADAT1.1", "ADAT1.2",
	"ADAT2.1", "ADAT2.2",
	"ADAT3.1", "ADAT3.2",
	"ADAT4.1", "ADAT4.2",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R"
};


static char *texts_ports_aio_in_ss[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
747 748
	"ADAT.7", "ADAT.8",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
749 750 751 752 753 754 755 756
};

static char *texts_ports_aio_out_ss[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4", "ADAT.5", "ADAT.6",
	"ADAT.7", "ADAT.8",
757 758
	"Phone.L", "Phone.R",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
759 760 761 762 763 764
};

static char *texts_ports_aio_in_ds[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
765 766
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
767 768 769 770 771 772 773
};

static char *texts_ports_aio_out_ds[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
774 775
	"Phone.L", "Phone.R",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
776 777 778 779 780 781
};

static char *texts_ports_aio_in_qs[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
782 783
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
784 785 786 787 788 789 790
};

static char *texts_ports_aio_out_qs[] = {
	"Analogue.L", "Analogue.R",
	"AES.L", "AES.R",
	"SPDIF.L", "SPDIF.R",
	"ADAT.1", "ADAT.2", "ADAT.3", "ADAT.4",
791 792
	"Phone.L", "Phone.R",
	"AEB.1", "AEB.2", "AEB.3", "AEB.4"
793 794
};

795 796 797 798 799 800
static char *texts_ports_aes32[] = {
	"AES.1", "AES.2", "AES.3", "AES.4", "AES.5", "AES.6", "AES.7",
	"AES.8", "AES.9.", "AES.10", "AES.11", "AES.12", "AES.13", "AES.14",
	"AES.15", "AES.16"
};

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
/* These tables map the ALSA channels 1..N to the channels that we
   need to use in order to find the relevant channel buffer. RME
   refers to this kind of mapping as between "the ADAT channel and
   the DMA channel." We index it using the logical audio channel,
   and the value is the DMA channel (i.e. channel buffer number)
   where the data for that channel can be read/written from/to.
*/

static char channel_map_unity_ss[HDSPM_MAX_CHANNELS] = {
	0, 1, 2, 3, 4, 5, 6, 7,
	8, 9, 10, 11, 12, 13, 14, 15,
	16, 17, 18, 19, 20, 21, 22, 23,
	24, 25, 26, 27, 28, 29, 30, 31,
	32, 33, 34, 35, 36, 37, 38, 39,
	40, 41, 42, 43, 44, 45, 46, 47,
	48, 49, 50, 51, 52, 53, 54, 55,
	56, 57, 58, 59, 60, 61, 62, 63
};

static char channel_map_raydat_ss[HDSPM_MAX_CHANNELS] = {
	4, 5, 6, 7, 8, 9, 10, 11,	/* ADAT 1 */
	12, 13, 14, 15, 16, 17, 18, 19,	/* ADAT 2 */
	20, 21, 22, 23, 24, 25, 26, 27,	/* ADAT 3 */
	28, 29, 30, 31, 32, 33, 34, 35,	/* ADAT 4 */
	0, 1,			/* AES */
	2, 3,			/* SPDIF */
	-1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
};

static char channel_map_raydat_ds[HDSPM_MAX_CHANNELS] = {
	4, 5, 6, 7,		/* ADAT 1 */
	8, 9, 10, 11,		/* ADAT 2 */
	12, 13, 14, 15,		/* ADAT 3 */
	16, 17, 18, 19,		/* ADAT 4 */
	0, 1,			/* AES */
	2, 3,			/* SPDIF */
	-1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
};

static char channel_map_raydat_qs[HDSPM_MAX_CHANNELS] = {
	4, 5,			/* ADAT 1 */
	6, 7,			/* ADAT 2 */
	8, 9,			/* ADAT 3 */
	10, 11,			/* ADAT 4 */
	0, 1,			/* AES */
	2, 3,			/* SPDIF */
	-1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
};

static char channel_map_aio_in_ss[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line in */
	8, 9,			/* aes in, */
	10, 11,			/* spdif in */
	12, 13, 14, 15, 16, 17, 18, 19,	/* ADAT in */
869 870
	2, 3, 4, 5,		/* AEB */
	-1, -1, -1, -1, -1, -1,
871 872 873 874 875 876 877 878 879 880 881 882 883
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
};

static char channel_map_aio_out_ss[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line out */
	8, 9,			/* aes out */
	10, 11,			/* spdif out */
	12, 13, 14, 15, 16, 17, 18, 19,	/* ADAT out */
	6, 7,			/* phone out */
884 885
	2, 3, 4, 5,		/* AEB */
	-1, -1, -1, -1,
886 887 888 889 890 891 892 893 894 895 896 897
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
};

static char channel_map_aio_in_ds[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line in */
	8, 9,			/* aes in */
	10, 11,			/* spdif in */
	12, 14, 16, 18,		/* adat in */
898 899
	2, 3, 4, 5,		/* AEB */
	-1, -1,
900 901 902 903 904 905 906 907 908 909 910 911 912 913
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};

static char channel_map_aio_out_ds[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line out */
	8, 9,			/* aes out */
	10, 11,			/* spdif out */
	12, 14, 16, 18,		/* adat out */
	6, 7,			/* phone out */
914
	2, 3, 4, 5,		/* AEB */
915 916 917 918 919 920 921 922 923 924 925 926 927
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};

static char channel_map_aio_in_qs[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line in */
	8, 9,			/* aes in */
	10, 11,			/* spdif in */
	12, 16,			/* adat in */
928 929
	2, 3, 4, 5,		/* AEB */
	-1, -1, -1, -1,
930 931 932 933 934 935 936 937 938 939 940 941 942 943
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};

static char channel_map_aio_out_qs[HDSPM_MAX_CHANNELS] = {
	0, 1,			/* line out */
	8, 9,			/* aes out */
	10, 11,			/* spdif out */
	12, 16,			/* adat out */
	6, 7,			/* phone out */
944 945
	2, 3, 4, 5,		/* AEB */
	-1, -1,
946 947 948 949 950 951 952 953
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};

954 955 956 957 958 959 960 961 962 963 964
static char channel_map_aes32[HDSPM_MAX_CHANNELS] = {
	0, 1, 2, 3, 4, 5, 6, 7,
	8, 9, 10, 11, 12, 13, 14, 15,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};

965 966
struct hdspm_midi {
	struct hdspm *hdspm;
T
Takashi Iwai 已提交
967
	int id;
968 969 970
	struct snd_rawmidi *rmidi;
	struct snd_rawmidi_substream *input;
	struct snd_rawmidi_substream *output;
T
Takashi Iwai 已提交
971 972 973 974
	char istimer;		/* timer in use */
	struct timer_list timer;
	spinlock_t lock;
	int pending;
975 976 977 978 979 980 981 982 983
	int dataIn;
	int statusIn;
	int dataOut;
	int statusOut;
	int ie;
	int irq;
};

struct hdspm_tco {
984 985 986 987 988
	int input; /* 0: LTC, 1:Video, 2: WC*/
	int framerate; /* 0=24, 1=25, 2=29.97, 3=29.97d, 4=30, 5=30d */
	int wordclock; /* 0=1:1, 1=44.1->48, 2=48->44.1 */
	int samplerate; /* 0=44.1, 1=48, 2= freq from app */
	int pull; /*   0=0, 1=+0.1%, 2=-0.1%, 3=+4%, 4=-4%*/
989
	int term; /* 0 = off, 1 = on */
T
Takashi Iwai 已提交
990 991
};

992
struct hdspm {
T
Takashi Iwai 已提交
993
        spinlock_t lock;
T
Takashi Iwai 已提交
994 995 996
	/* only one playback and/or capture stream */
        struct snd_pcm_substream *capture_substream;
        struct snd_pcm_substream *playback_substream;
T
Takashi Iwai 已提交
997 998

	char *card_name;	     /* for procinfo */
R
Remy Bruno 已提交
999 1000
	unsigned short firmware_rev; /* dont know if relevant (yes if AES32)*/

1001
	uint8_t io_type;
T
Takashi Iwai 已提交
1002 1003 1004 1005 1006

	int monitor_outs;	/* set up monitoring outs init flag */

	u32 control_register;	/* cached value */
	u32 control2_register;	/* cached value */
1007
	u32 settings_register;  /* cached value for AIO / RayDat (sync reference, master/slave) */
T
Takashi Iwai 已提交
1008

1009
	struct hdspm_midi midi[4];
T
Takashi Iwai 已提交
1010 1011 1012
	struct tasklet_struct midi_tasklet;

	size_t period_bytes;
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	unsigned char ss_in_channels;
	unsigned char ds_in_channels;
	unsigned char qs_in_channels;
	unsigned char ss_out_channels;
	unsigned char ds_out_channels;
	unsigned char qs_out_channels;

	unsigned char max_channels_in;
	unsigned char max_channels_out;

1023 1024
	signed char *channel_map_in;
	signed char *channel_map_out;
1025

1026 1027
	signed char *channel_map_in_ss, *channel_map_in_ds, *channel_map_in_qs;
	signed char *channel_map_out_ss, *channel_map_out_ds, *channel_map_out_qs;
1028 1029 1030 1031 1032 1033

	char **port_names_in;
	char **port_names_out;

	char **port_names_in_ss, **port_names_in_ds, **port_names_in_qs;
	char **port_names_out_ss, **port_names_out_ds, **port_names_out_qs;
T
Takashi Iwai 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

	unsigned char *playback_buffer;	/* suitably aligned address */
	unsigned char *capture_buffer;	/* suitably aligned address */

	pid_t capture_pid;	/* process id which uses capture */
	pid_t playback_pid;	/* process id which uses capture */
	int running;		/* running status */

	int last_external_sample_rate;	/* samplerate mystic ... */
	int last_internal_sample_rate;
	int system_sample_rate;

	int dev;		/* Hardware vars... */
	int irq;
	unsigned long port;
	void __iomem *iobase;

	int irq_count;		/* for debug */
1052
	int midiPorts;
T
Takashi Iwai 已提交
1053

1054 1055 1056
	struct snd_card *card;	/* one card */
	struct snd_pcm *pcm;		/* has one pcm */
	struct snd_hwdep *hwdep;	/* and a hwdep for additional ioctl */
T
Takashi Iwai 已提交
1057 1058 1059
	struct pci_dev *pci;	/* and an pci info */

	/* Mixer vars */
T
Takashi Iwai 已提交
1060 1061 1062 1063
	/* fast alsa mixer */
	struct snd_kcontrol *playback_mixer_ctls[HDSPM_MAX_CHANNELS];
	/* but input to much, so not used */
	struct snd_kcontrol *input_mixer_ctls[HDSPM_MAX_CHANNELS];
L
Lucas De Marchi 已提交
1064
	/* full mixer accessible over mixer ioctl or hwdep-device */
T
Takashi Iwai 已提交
1065
	struct hdspm_mixer *mixer;
T
Takashi Iwai 已提交
1066

1067
	struct hdspm_tco *tco;  /* NULL if no TCO detected */
T
Takashi Iwai 已提交
1068

1069
	const char *const *texts_autosync;
1070
	int texts_autosync_items;
T
Takashi Iwai 已提交
1071

1072
	cycles_t last_interrupt;
1073

1074 1075
	unsigned int serial;

1076
	struct hdspm_peak_rms peak_rms;
T
Takashi Iwai 已提交
1077 1078 1079
};


1080
static const struct pci_device_id snd_hdspm_ids[] = {
T
Takashi Iwai 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	{
	 .vendor = PCI_VENDOR_ID_XILINX,
	 .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP_MADI,
	 .subvendor = PCI_ANY_ID,
	 .subdevice = PCI_ANY_ID,
	 .class = 0,
	 .class_mask = 0,
	 .driver_data = 0},
	{0,}
};

MODULE_DEVICE_TABLE(pci, snd_hdspm_ids);

/* prototypes */
1095 1096 1097 1098
static int snd_hdspm_create_alsa_devices(struct snd_card *card,
					 struct hdspm *hdspm);
static int snd_hdspm_create_pcm(struct snd_card *card,
				struct hdspm *hdspm);
1099

1100
static inline void snd_hdspm_initialize_midi_flush(struct hdspm *hdspm);
1101
static inline int hdspm_get_pll_freq(struct hdspm *hdspm);
1102 1103
static int hdspm_update_simple_mixer_controls(struct hdspm *hdspm);
static int hdspm_autosync_ref(struct hdspm *hdspm);
1104
static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out);
1105
static int snd_hdspm_set_defaults(struct hdspm *hdspm);
1106
static int hdspm_system_clock_mode(struct hdspm *hdspm);
1107
static void hdspm_set_sgbuf(struct hdspm *hdspm,
1108
			    struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
1109 1110
			     unsigned int reg, int channels);

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx);
static int hdspm_wc_sync_check(struct hdspm *hdspm);
static int hdspm_tco_sync_check(struct hdspm *hdspm);
static int hdspm_sync_in_sync_check(struct hdspm *hdspm);

static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index);
static int hdspm_get_tco_sample_rate(struct hdspm *hdspm);
static int hdspm_get_wc_sample_rate(struct hdspm *hdspm);



R
Remy Bruno 已提交
1122 1123
static inline int HDSPM_bit2freq(int n)
{
1124 1125
	static const int bit2freq_tab[] = {
		0, 32000, 44100, 48000, 64000, 88200,
R
Remy Bruno 已提交
1126 1127 1128 1129 1130 1131
		96000, 128000, 176400, 192000 };
	if (n < 1 || n > 9)
		return 0;
	return bit2freq_tab[n];
}

1132 1133 1134 1135 1136 1137
static bool hdspm_is_raydat_or_aio(struct hdspm *hdspm)
{
	return ((AIO == hdspm->io_type) || (RayDAT == hdspm->io_type));
}


1138
/* Write/read to/from HDSPM with Adresses in Bytes
T
Takashi Iwai 已提交
1139 1140
   not words but only 32Bit writes are allowed */

1141
static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
T
Takashi Iwai 已提交
1142 1143 1144 1145 1146
			       unsigned int val)
{
	writel(val, hdspm->iobase + reg);
}

1147
static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
T
Takashi Iwai 已提交
1148 1149 1150 1151
{
	return readl(hdspm->iobase + reg);
}

1152 1153
/* for each output channel (chan) I have an Input (in) and Playback (pb) Fader
   mixer is write only on hardware so we have to cache him for read
T
Takashi Iwai 已提交
1154 1155
   each fader is a u32, but uses only the first 16 bit */

1156
static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
T
Takashi Iwai 已提交
1157 1158
				     unsigned int in)
{
1159
	if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
T
Takashi Iwai 已提交
1160 1161 1162 1163 1164
		return 0;

	return hdspm->mixer->ch[chan].in[in];
}

1165
static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
T
Takashi Iwai 已提交
1166 1167
				     unsigned int pb)
{
1168
	if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
T
Takashi Iwai 已提交
1169 1170 1171 1172
		return 0;
	return hdspm->mixer->ch[chan].pb[pb];
}

1173
static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
T
Takashi Iwai 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
				      unsigned int in, unsigned short data)
{
	if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
		return -1;

	hdspm_write(hdspm,
		    HDSPM_MADI_mixerBase +
		    ((in + 128 * chan) * sizeof(u32)),
		    (hdspm->mixer->ch[chan].in[in] = data & 0xFFFF));
	return 0;
}

1186
static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
T
Takashi Iwai 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
				      unsigned int pb, unsigned short data)
{
	if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
		return -1;

	hdspm_write(hdspm,
		    HDSPM_MADI_mixerBase +
		    ((64 + pb + 128 * chan) * sizeof(u32)),
		    (hdspm->mixer->ch[chan].pb[pb] = data & 0xFFFF));
	return 0;
}


/* enable DMA for specific channels, now available for DSP-MADI */
1201
static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
T
Takashi Iwai 已提交
1202 1203 1204 1205
{
	hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
}

1206
static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
T
Takashi Iwai 已提交
1207 1208 1209 1210 1211
{
	hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
}

/* check if same process is writing and reading */
1212
static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
T
Takashi Iwai 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
{
	unsigned long flags;
	int ret = 1;

	spin_lock_irqsave(&hdspm->lock, flags);
	if ((hdspm->playback_pid != hdspm->capture_pid) &&
	    (hdspm->playback_pid >= 0) && (hdspm->capture_pid >= 0)) {
		ret = 0;
	}
	spin_unlock_irqrestore(&hdspm->lock, flags);
	return ret;
}

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
/* round arbitary sample rates to commonly known rates */
static int hdspm_round_frequency(int rate)
{
	if (rate < 38050)
		return 32000;
	if (rate < 46008)
		return 44100;
	else
		return 48000;
}

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
/* QS and DS rates normally can not be detected
 * automatically by the card. Only exception is MADI
 * in 96k frame mode.
 *
 * So if we read SS values (32 .. 48k), check for
 * user-provided DS/QS bits in the control register
 * and multiply the base frequency accordingly.
 */
static int hdspm_rate_multiplier(struct hdspm *hdspm, int rate)
{
	if (rate <= 48000) {
		if (hdspm->control_register & HDSPM_QuadSpeed)
			return rate * 4;
		else if (hdspm->control_register &
				HDSPM_DoubleSpeed)
			return rate * 2;
1253
	}
1254 1255 1256
	return rate;
}

1257
/* check for external sample rate, returns the sample rate in Hz*/
1258
static int hdspm_external_sample_rate(struct hdspm *hdspm)
T
Takashi Iwai 已提交
1259
{
1260
	unsigned int status, status2;
1261
	int syncref, rate = 0, rate_bits;
R
Remy Bruno 已提交
1262

1263 1264 1265 1266 1267 1268
	switch (hdspm->io_type) {
	case AES32:
		status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
		status = hdspm_read(hdspm, HDSPM_statusRegister);

		syncref = hdspm_autosync_ref(hdspm);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
		switch (syncref) {
		case HDSPM_AES32_AUTOSYNC_FROM_WORD:
		/* Check WC sync and get sample rate */
			if (hdspm_wc_sync_check(hdspm))
				return HDSPM_bit2freq(hdspm_get_wc_sample_rate(hdspm));
			break;

		case HDSPM_AES32_AUTOSYNC_FROM_AES1:
		case HDSPM_AES32_AUTOSYNC_FROM_AES2:
		case HDSPM_AES32_AUTOSYNC_FROM_AES3:
		case HDSPM_AES32_AUTOSYNC_FROM_AES4:
		case HDSPM_AES32_AUTOSYNC_FROM_AES5:
		case HDSPM_AES32_AUTOSYNC_FROM_AES6:
		case HDSPM_AES32_AUTOSYNC_FROM_AES7:
		case HDSPM_AES32_AUTOSYNC_FROM_AES8:
		/* Check AES sync and get sample rate */
			if (hdspm_aes_sync_check(hdspm, syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1))
				return HDSPM_bit2freq(hdspm_get_aes_sample_rate(hdspm,
							syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1));
			break;


		case HDSPM_AES32_AUTOSYNC_FROM_TCO:
		/* Check TCO sync and get sample rate */
			if (hdspm_tco_sync_check(hdspm))
				return HDSPM_bit2freq(hdspm_get_tco_sample_rate(hdspm));
			break;
		default:
			return 0;
		} /* end switch(syncref) */
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
		break;

	case MADIface:
		status = hdspm_read(hdspm, HDSPM_statusRegister);

		if (!(status & HDSPM_madiLock)) {
			rate = 0;  /* no lock */
		} else {
			switch (status & (HDSPM_status1_freqMask)) {
			case HDSPM_status1_F_0*1:
				rate = 32000; break;
			case HDSPM_status1_F_0*2:
				rate = 44100; break;
			case HDSPM_status1_F_0*3:
				rate = 48000; break;
			case HDSPM_status1_F_0*4:
				rate = 64000; break;
			case HDSPM_status1_F_0*5:
				rate = 88200; break;
			case HDSPM_status1_F_0*6:
				rate = 96000; break;
			case HDSPM_status1_F_0*7:
				rate = 128000; break;
			case HDSPM_status1_F_0*8:
				rate = 176400; break;
			case HDSPM_status1_F_0*9:
				rate = 192000; break;
			default:
				rate = 0; break;
			}
		}

		break;

	case MADI:
	case AIO:
	case RayDAT:
		status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
		status = hdspm_read(hdspm, HDSPM_statusRegister);
		rate = 0;
T
Takashi Iwai 已提交
1339

R
Remy Bruno 已提交
1340 1341
		/* if wordclock has synced freq and wordclock is valid */
		if ((status2 & HDSPM_wcLock) != 0 &&
1342
				(status2 & HDSPM_SelSyncRef0) == 0) {
T
Takashi Iwai 已提交
1343

R
Remy Bruno 已提交
1344
			rate_bits = status2 & HDSPM_wcFreqMask;
T
Takashi Iwai 已提交
1345

1346

R
Remy Bruno 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
			switch (rate_bits) {
			case HDSPM_wcFreq32:
				rate = 32000;
				break;
			case HDSPM_wcFreq44_1:
				rate = 44100;
				break;
			case HDSPM_wcFreq48:
				rate = 48000;
				break;
			case HDSPM_wcFreq64:
				rate = 64000;
				break;
			case HDSPM_wcFreq88_2:
				rate = 88200;
				break;
			case HDSPM_wcFreq96:
				rate = 96000;
				break;
1366 1367 1368 1369 1370 1371 1372 1373 1374
			case HDSPM_wcFreq128:
				rate = 128000;
				break;
			case HDSPM_wcFreq176_4:
				rate = 176400;
				break;
			case HDSPM_wcFreq192:
				rate = 192000;
				break;
R
Remy Bruno 已提交
1375 1376 1377 1378
			default:
				rate = 0;
				break;
			}
T
Takashi Iwai 已提交
1379 1380
		}

T
Takashi Iwai 已提交
1381 1382 1383
		/* if rate detected and Syncref is Word than have it,
		 * word has priority to MADI
		 */
R
Remy Bruno 已提交
1384
		if (rate != 0 &&
1385
		(status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
1386
			return hdspm_rate_multiplier(hdspm, rate);
T
Takashi Iwai 已提交
1387

1388
		/* maybe a madi input (which is taken if sel sync is madi) */
R
Remy Bruno 已提交
1389 1390
		if (status & HDSPM_madiLock) {
			rate_bits = status & HDSPM_madiFreqMask;
T
Takashi Iwai 已提交
1391

R
Remy Bruno 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
			switch (rate_bits) {
			case HDSPM_madiFreq32:
				rate = 32000;
				break;
			case HDSPM_madiFreq44_1:
				rate = 44100;
				break;
			case HDSPM_madiFreq48:
				rate = 48000;
				break;
			case HDSPM_madiFreq64:
				rate = 64000;
				break;
			case HDSPM_madiFreq88_2:
				rate = 88200;
				break;
			case HDSPM_madiFreq96:
				rate = 96000;
				break;
			case HDSPM_madiFreq128:
				rate = 128000;
				break;
			case HDSPM_madiFreq176_4:
				rate = 176400;
				break;
			case HDSPM_madiFreq192:
				rate = 192000;
				break;
			default:
				rate = 0;
				break;
			}
1424

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		} /* endif HDSPM_madiLock */

		/* check sample rate from TCO or SYNC_IN */
		{
			bool is_valid_input = 0;
			bool has_sync = 0;

			syncref = hdspm_autosync_ref(hdspm);
			if (HDSPM_AUTOSYNC_FROM_TCO == syncref) {
				is_valid_input = 1;
				has_sync = (HDSPM_SYNC_CHECK_SYNC ==
					hdspm_tco_sync_check(hdspm));
			} else if (HDSPM_AUTOSYNC_FROM_SYNC_IN == syncref) {
				is_valid_input = 1;
				has_sync = (HDSPM_SYNC_CHECK_SYNC ==
					hdspm_sync_in_sync_check(hdspm));
1441
			}
1442 1443 1444 1445 1446 1447 1448

			if (is_valid_input && has_sync) {
				rate = hdspm_round_frequency(
					hdspm_get_pll_freq(hdspm));
			}
		}

1449 1450
		rate = hdspm_rate_multiplier(hdspm, rate);

1451
		break;
T
Takashi Iwai 已提交
1452
	}
1453 1454

	return rate;
T
Takashi Iwai 已提交
1455 1456
}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
/* return latency in samples per period */
static int hdspm_get_latency(struct hdspm *hdspm)
{
	int n;

	n = hdspm_decode_latency(hdspm->control_register);

	/* Special case for new RME cards with 32 samples period size.
	 * The three latency bits in the control register
	 * (HDSP_LatencyMask) encode latency values of 64 samples as
	 * 0, 128 samples as 1 ... 4096 samples as 6. For old cards, 7
	 * denotes 8192 samples, but on new cards like RayDAT or AIO,
	 * it corresponds to 32 samples.
	 */
	if ((7 == n) && (RayDAT == hdspm->io_type || AIO == hdspm->io_type))
		n = -1;

	return 1 << (n + 6);
}

T
Takashi Iwai 已提交
1477
/* Latency function */
1478
static inline void hdspm_compute_period_size(struct hdspm *hdspm)
T
Takashi Iwai 已提交
1479
{
1480
	hdspm->period_bytes = 4 * hdspm_get_latency(hdspm);
T
Takashi Iwai 已提交
1481 1482
}

1483 1484

static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm *hdspm)
T
Takashi Iwai 已提交
1485 1486 1487 1488
{
	int position;

	position = hdspm_read(hdspm, HDSPM_statusRegister);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

	switch (hdspm->io_type) {
	case RayDAT:
	case AIO:
		position &= HDSPM_BufferPositionMask;
		position /= 4; /* Bytes per sample */
		break;
	default:
		position = (position & HDSPM_BufferID) ?
			(hdspm->period_bytes / 4) : 0;
	}
T
Takashi Iwai 已提交
1500 1501 1502 1503 1504

	return position;
}


1505
static inline void hdspm_start_audio(struct hdspm * s)
T
Takashi Iwai 已提交
1506 1507 1508 1509 1510
{
	s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
	hdspm_write(s, HDSPM_controlRegister, s->control_register);
}

1511
static inline void hdspm_stop_audio(struct hdspm * s)
T
Takashi Iwai 已提交
1512 1513 1514 1515 1516 1517
{
	s->control_register &= ~(HDSPM_Start | HDSPM_AudioInterruptEnable);
	hdspm_write(s, HDSPM_controlRegister, s->control_register);
}

/* should I silence all or only opened ones ? doit all for first even is 4MB*/
1518
static void hdspm_silence_playback(struct hdspm *hdspm)
T
Takashi Iwai 已提交
1519 1520 1521 1522 1523
{
	int i;
	int n = hdspm->period_bytes;
	void *buf = hdspm->playback_buffer;

R
Remy Bruno 已提交
1524 1525
	if (buf == NULL)
		return;
T
Takashi Iwai 已提交
1526 1527 1528 1529 1530 1531 1532

	for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
		memset(buf, 0, n);
		buf += HDSPM_CHANNEL_BUFFER_BYTES;
	}
}

1533
static int hdspm_set_interrupt_interval(struct hdspm *s, unsigned int frames)
T
Takashi Iwai 已提交
1534 1535 1536 1537 1538
{
	int n;

	spin_lock_irq(&s->lock);

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
	if (32 == frames) {
		/* Special case for new RME cards like RayDAT/AIO which
		 * support period sizes of 32 samples. Since latency is
		 * encoded in the three bits of HDSP_LatencyMask, we can only
		 * have values from 0 .. 7. While 0 still means 64 samples and
		 * 6 represents 4096 samples on all cards, 7 represents 8192
		 * on older cards and 32 samples on new cards.
		 *
		 * In other words, period size in samples is calculated by
		 * 2^(n+6) with n ranging from 0 .. 7.
		 */
		n = 7;
	} else {
		frames >>= 7;
		n = 0;
		while (frames) {
			n++;
			frames >>= 1;
		}
T
Takashi Iwai 已提交
1558
	}
1559

T
Takashi Iwai 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	s->control_register &= ~HDSPM_LatencyMask;
	s->control_register |= hdspm_encode_latency(n);

	hdspm_write(s, HDSPM_controlRegister, s->control_register);

	hdspm_compute_period_size(s);

	spin_unlock_irq(&s->lock);

	return 0;
}

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
static u64 hdspm_calc_dds_value(struct hdspm *hdspm, u64 period)
{
	u64 freq_const;

	if (period == 0)
		return 0;

	switch (hdspm->io_type) {
	case MADI:
	case AES32:
		freq_const = 110069313433624ULL;
		break;
	case RayDAT:
	case AIO:
		freq_const = 104857600000000ULL;
		break;
	case MADIface:
		freq_const = 131072000000000ULL;
1590 1591 1592 1593
		break;
	default:
		snd_BUG();
		return 0;
1594 1595 1596 1597 1598 1599
	}

	return div_u64(freq_const, period);
}


1600 1601 1602
static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
{
	u64 n;
1603

1604 1605 1606 1607 1608
	if (rate >= 112000)
		rate /= 4;
	else if (rate >= 56000)
		rate /= 2;

1609 1610
	switch (hdspm->io_type) {
	case MADIface:
1611 1612
		n = 131072000000000ULL;  /* 125 MHz */
		break;
1613 1614
	case MADI:
	case AES32:
1615 1616
		n = 110069313433624ULL;  /* 105 MHz */
		break;
1617 1618
	case RayDAT:
	case AIO:
1619 1620 1621 1622 1623
		n = 104857600000000ULL;  /* 100 MHz */
		break;
	default:
		snd_BUG();
		return;
1624 1625
	}

1626
	n = div_u64(n, rate);
1627
	/* n should be less than 2^32 for being written to FREQ register */
1628
	snd_BUG_ON(n >> 32);
1629 1630
	hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
}
T
Takashi Iwai 已提交
1631 1632

/* dummy set rate lets see what happens */
1633
static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
T
Takashi Iwai 已提交
1634 1635 1636 1637
{
	int current_rate;
	int rate_bits;
	int not_set = 0;
R
Remy Bruno 已提交
1638
	int current_speed, target_speed;
T
Takashi Iwai 已提交
1639 1640 1641 1642 1643 1644 1645

	/* ASSUMPTION: hdspm->lock is either set, or there is no need for
	   it (e.g. during module initialization).
	 */

	if (!(hdspm->control_register & HDSPM_ClockModeMaster)) {

1646
		/* SLAVE --- */
T
Takashi Iwai 已提交
1647 1648
		if (called_internally) {

1649 1650 1651 1652
			/* request from ctl or card initialization
			   just make a warning an remember setting
			   for future master mode switching */

1653 1654
			dev_warn(hdspm->card->dev,
				 "Warning: device is not running as a clock master.\n");
T
Takashi Iwai 已提交
1655 1656 1657 1658 1659 1660 1661
			not_set = 1;
		} else {

			/* hw_param request while in AutoSync mode */
			int external_freq =
			    hdspm_external_sample_rate(hdspm);

T
Takashi Iwai 已提交
1662 1663
			if (hdspm_autosync_ref(hdspm) ==
			    HDSPM_AUTOSYNC_FROM_NONE) {
T
Takashi Iwai 已提交
1664

1665 1666
				dev_warn(hdspm->card->dev,
					 "Detected no Externel Sync\n");
T
Takashi Iwai 已提交
1667 1668 1669 1670
				not_set = 1;

			} else if (rate != external_freq) {

1671 1672
				dev_warn(hdspm->card->dev,
					 "Warning: No AutoSync source for requested rate\n");
T
Takashi Iwai 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
				not_set = 1;
			}
		}
	}

	current_rate = hdspm->system_sample_rate;

	/* Changing between Singe, Double and Quad speed is not
	   allowed if any substreams are open. This is because such a change
	   causes a shift in the location of the DMA buffers and a reduction
	   in the number of available buffers.

	   Note that a similar but essentially insoluble problem exists for
	   externally-driven rate changes. All we can do is to flag rate
1687
	   changes in the read/write routines.
T
Takashi Iwai 已提交
1688 1689
	 */

R
Remy Bruno 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	if (current_rate <= 48000)
		current_speed = HDSPM_SPEED_SINGLE;
	else if (current_rate <= 96000)
		current_speed = HDSPM_SPEED_DOUBLE;
	else
		current_speed = HDSPM_SPEED_QUAD;

	if (rate <= 48000)
		target_speed = HDSPM_SPEED_SINGLE;
	else if (rate <= 96000)
		target_speed = HDSPM_SPEED_DOUBLE;
	else
		target_speed = HDSPM_SPEED_QUAD;
R
Remy Bruno 已提交
1703

T
Takashi Iwai 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
	switch (rate) {
	case 32000:
		rate_bits = HDSPM_Frequency32KHz;
		break;
	case 44100:
		rate_bits = HDSPM_Frequency44_1KHz;
		break;
	case 48000:
		rate_bits = HDSPM_Frequency48KHz;
		break;
	case 64000:
		rate_bits = HDSPM_Frequency64KHz;
		break;
	case 88200:
		rate_bits = HDSPM_Frequency88_2KHz;
		break;
	case 96000:
		rate_bits = HDSPM_Frequency96KHz;
		break;
R
Remy Bruno 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731
	case 128000:
		rate_bits = HDSPM_Frequency128KHz;
		break;
	case 176400:
		rate_bits = HDSPM_Frequency176_4KHz;
		break;
	case 192000:
		rate_bits = HDSPM_Frequency192KHz;
		break;
T
Takashi Iwai 已提交
1732 1733 1734 1735
	default:
		return -EINVAL;
	}

R
Remy Bruno 已提交
1736
	if (current_speed != target_speed
T
Takashi Iwai 已提交
1737
	    && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
1738 1739 1740 1741 1742
		dev_err(hdspm->card->dev,
			"cannot change from %s speed to %s speed mode (capture PID = %d, playback PID = %d)\n",
			hdspm_speed_names[current_speed],
			hdspm_speed_names[target_speed],
			hdspm->capture_pid, hdspm->playback_pid);
T
Takashi Iwai 已提交
1743 1744 1745 1746 1747 1748 1749
		return -EBUSY;
	}

	hdspm->control_register &= ~HDSPM_FrequencyMask;
	hdspm->control_register |= rate_bits;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

1750 1751 1752
	/* For AES32, need to set DDS value in FREQ register
	   For MADI, also apparently */
	hdspm_set_dds_value(hdspm, rate);
1753 1754

	if (AES32 == hdspm->io_type && rate != current_rate)
1755
		hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
T
Takashi Iwai 已提交
1756 1757 1758

	hdspm->system_sample_rate = rate;

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
	if (rate <= 48000) {
		hdspm->channel_map_in = hdspm->channel_map_in_ss;
		hdspm->channel_map_out = hdspm->channel_map_out_ss;
		hdspm->max_channels_in = hdspm->ss_in_channels;
		hdspm->max_channels_out = hdspm->ss_out_channels;
		hdspm->port_names_in = hdspm->port_names_in_ss;
		hdspm->port_names_out = hdspm->port_names_out_ss;
	} else if (rate <= 96000) {
		hdspm->channel_map_in = hdspm->channel_map_in_ds;
		hdspm->channel_map_out = hdspm->channel_map_out_ds;
		hdspm->max_channels_in = hdspm->ds_in_channels;
		hdspm->max_channels_out = hdspm->ds_out_channels;
		hdspm->port_names_in = hdspm->port_names_in_ds;
		hdspm->port_names_out = hdspm->port_names_out_ds;
	} else {
		hdspm->channel_map_in = hdspm->channel_map_in_qs;
		hdspm->channel_map_out = hdspm->channel_map_out_qs;
		hdspm->max_channels_in = hdspm->qs_in_channels;
		hdspm->max_channels_out = hdspm->qs_out_channels;
		hdspm->port_names_in = hdspm->port_names_in_qs;
		hdspm->port_names_out = hdspm->port_names_out_qs;
	}

T
Takashi Iwai 已提交
1782 1783 1784 1785 1786 1787 1788
	if (not_set != 0)
		return -1;

	return 0;
}

/* mainly for init to 0 on load */
1789
static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
T
Takashi Iwai 已提交
1790 1791
{
	int i, j;
T
Takashi Iwai 已提交
1792 1793 1794 1795 1796 1797 1798 1799
	unsigned int gain;

	if (sgain > UNITY_GAIN)
		gain = UNITY_GAIN;
	else if (sgain < 0)
		gain = 0;
	else
		gain = sgain;
T
Takashi Iwai 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811

	for (i = 0; i < HDSPM_MIXER_CHANNELS; i++)
		for (j = 0; j < HDSPM_MIXER_CHANNELS; j++) {
			hdspm_write_in_gain(hdspm, i, j, gain);
			hdspm_write_pb_gain(hdspm, i, j, gain);
		}
}

/*----------------------------------------------------------------------------
   MIDI
  ----------------------------------------------------------------------------*/

T
Takashi Iwai 已提交
1812 1813
static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
						      int id)
T
Takashi Iwai 已提交
1814 1815
{
	/* the hardware already does the relevant bit-mask with 0xff */
1816
	return hdspm_read(hdspm, hdspm->midi[id].dataIn);
T
Takashi Iwai 已提交
1817 1818
}

T
Takashi Iwai 已提交
1819 1820
static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
					      int val)
T
Takashi Iwai 已提交
1821 1822
{
	/* the hardware already does the relevant bit-mask with 0xff */
1823
	return hdspm_write(hdspm, hdspm->midi[id].dataOut, val);
T
Takashi Iwai 已提交
1824 1825
}

1826
static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1827
{
1828
	return hdspm_read(hdspm, hdspm->midi[id].statusIn) & 0xFF;
T
Takashi Iwai 已提交
1829 1830
}

1831
static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1832 1833 1834
{
	int fifo_bytes_used;

1835
	fifo_bytes_used = hdspm_read(hdspm, hdspm->midi[id].statusOut) & 0xFF;
T
Takashi Iwai 已提交
1836 1837 1838 1839 1840 1841 1842

	if (fifo_bytes_used < 128)
		return  128 - fifo_bytes_used;
	else
		return 0;
}

1843
static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1844 1845 1846 1847 1848
{
	while (snd_hdspm_midi_input_available (hdspm, id))
		snd_hdspm_midi_read_byte (hdspm, id);
}

1849
static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
T
Takashi Iwai 已提交
1850 1851 1852 1853 1854 1855 1856 1857
{
	unsigned long flags;
	int n_pending;
	int to_write;
	int i;
	unsigned char buf[128];

	/* Output is not interrupt driven */
1858

T
Takashi Iwai 已提交
1859
	spin_lock_irqsave (&hmidi->lock, flags);
T
Takashi Iwai 已提交
1860 1861 1862 1863 1864 1865 1866
	if (hmidi->output &&
	    !snd_rawmidi_transmit_empty (hmidi->output)) {
		n_pending = snd_hdspm_midi_output_possible (hmidi->hdspm,
							    hmidi->id);
		if (n_pending > 0) {
			if (n_pending > (int)sizeof (buf))
				n_pending = sizeof (buf);
1867

T
Takashi Iwai 已提交
1868 1869 1870
			to_write = snd_rawmidi_transmit (hmidi->output, buf,
							 n_pending);
			if (to_write > 0) {
1871
				for (i = 0; i < to_write; ++i)
T
Takashi Iwai 已提交
1872 1873 1874
					snd_hdspm_midi_write_byte (hmidi->hdspm,
								   hmidi->id,
								   buf[i]);
T
Takashi Iwai 已提交
1875 1876 1877 1878 1879 1880 1881
			}
		}
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return 0;
}

1882
static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
T
Takashi Iwai 已提交
1883
{
T
Takashi Iwai 已提交
1884 1885 1886
	unsigned char buf[128]; /* this buffer is designed to match the MIDI
				 * input FIFO size
				 */
T
Takashi Iwai 已提交
1887 1888 1889 1890 1891
	unsigned long flags;
	int n_pending;
	int i;

	spin_lock_irqsave (&hmidi->lock, flags);
T
Takashi Iwai 已提交
1892 1893
	n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
	if (n_pending > 0) {
T
Takashi Iwai 已提交
1894
		if (hmidi->input) {
T
Takashi Iwai 已提交
1895
			if (n_pending > (int)sizeof (buf))
T
Takashi Iwai 已提交
1896
				n_pending = sizeof (buf);
T
Takashi Iwai 已提交
1897 1898 1899 1900 1901 1902
			for (i = 0; i < n_pending; ++i)
				buf[i] = snd_hdspm_midi_read_byte (hmidi->hdspm,
								   hmidi->id);
			if (n_pending)
				snd_rawmidi_receive (hmidi->input, buf,
						     n_pending);
T
Takashi Iwai 已提交
1903 1904
		} else {
			/* flush the MIDI input FIFO */
T
Takashi Iwai 已提交
1905 1906 1907
			while (n_pending--)
				snd_hdspm_midi_read_byte (hmidi->hdspm,
							  hmidi->id);
T
Takashi Iwai 已提交
1908 1909 1910
		}
	}
	hmidi->pending = 0;
1911
	spin_unlock_irqrestore(&hmidi->lock, flags);
1912

1913
	spin_lock_irqsave(&hmidi->hdspm->lock, flags);
1914
	hmidi->hdspm->control_register |= hmidi->ie;
T
Takashi Iwai 已提交
1915 1916
	hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
		    hmidi->hdspm->control_register);
1917
	spin_unlock_irqrestore(&hmidi->hdspm->lock, flags);
1918

T
Takashi Iwai 已提交
1919 1920 1921
	return snd_hdspm_midi_output_write (hmidi);
}

T
Takashi Iwai 已提交
1922 1923
static void
snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
T
Takashi Iwai 已提交
1924
{
1925 1926
	struct hdspm *hdspm;
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1927 1928
	unsigned long flags;

T
Takashi Iwai 已提交
1929
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1930
	hdspm = hmidi->hdspm;
1931

T
Takashi Iwai 已提交
1932 1933
	spin_lock_irqsave (&hdspm->lock, flags);
	if (up) {
1934
		if (!(hdspm->control_register & hmidi->ie)) {
T
Takashi Iwai 已提交
1935
			snd_hdspm_flush_midi_input (hdspm, hmidi->id);
1936
			hdspm->control_register |= hmidi->ie;
T
Takashi Iwai 已提交
1937 1938
		}
	} else {
1939
		hdspm->control_register &= ~hmidi->ie;
T
Takashi Iwai 已提交
1940 1941 1942 1943 1944 1945 1946 1947
	}

	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
	spin_unlock_irqrestore (&hdspm->lock, flags);
}

static void snd_hdspm_midi_output_timer(unsigned long data)
{
1948
	struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
T
Takashi Iwai 已提交
1949
	unsigned long flags;
1950

T
Takashi Iwai 已提交
1951 1952 1953 1954 1955 1956
	snd_hdspm_midi_output_write(hmidi);
	spin_lock_irqsave (&hmidi->lock, flags);

	/* this does not bump hmidi->istimer, because the
	   kernel automatically removed the timer when it
	   expired, and we are now adding it back, thus
1957
	   leaving istimer wherever it was set before.
T
Takashi Iwai 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
	*/

	if (hmidi->istimer) {
		hmidi->timer.expires = 1 + jiffies;
		add_timer(&hmidi->timer);
	}

	spin_unlock_irqrestore (&hmidi->lock, flags);
}

T
Takashi Iwai 已提交
1968 1969
static void
snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
T
Takashi Iwai 已提交
1970
{
1971
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1972 1973
	unsigned long flags;

T
Takashi Iwai 已提交
1974
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
	spin_lock_irqsave (&hmidi->lock, flags);
	if (up) {
		if (!hmidi->istimer) {
			init_timer(&hmidi->timer);
			hmidi->timer.function = snd_hdspm_midi_output_timer;
			hmidi->timer.data = (unsigned long) hmidi;
			hmidi->timer.expires = 1 + jiffies;
			add_timer(&hmidi->timer);
			hmidi->istimer++;
		}
	} else {
T
Takashi Iwai 已提交
1986
		if (hmidi->istimer && --hmidi->istimer <= 0)
T
Takashi Iwai 已提交
1987 1988 1989 1990 1991 1992 1993
			del_timer (&hmidi->timer);
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	if (up)
		snd_hdspm_midi_output_write(hmidi);
}

1994
static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
1995
{
1996
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1997

T
Takashi Iwai 已提交
1998
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1999 2000 2001 2002 2003 2004 2005 2006
	spin_lock_irq (&hmidi->lock);
	snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
	hmidi->input = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

2007
static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
2008
{
2009
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
2010

T
Takashi Iwai 已提交
2011
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
2012 2013 2014 2015 2016 2017 2018
	spin_lock_irq (&hmidi->lock);
	hmidi->output = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

2019
static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
2020
{
2021
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
2022 2023 2024

	snd_hdspm_midi_input_trigger (substream, 0);

T
Takashi Iwai 已提交
2025
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
2026 2027 2028 2029 2030 2031 2032
	spin_lock_irq (&hmidi->lock);
	hmidi->input = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

2033
static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
2034
{
2035
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
2036 2037 2038

	snd_hdspm_midi_output_trigger (substream, 0);

T
Takashi Iwai 已提交
2039
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
2040 2041 2042 2043 2044 2045 2046
	spin_lock_irq (&hmidi->lock);
	hmidi->output = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

2047
static struct snd_rawmidi_ops snd_hdspm_midi_output =
T
Takashi Iwai 已提交
2048 2049 2050 2051 2052 2053
{
	.open =		snd_hdspm_midi_output_open,
	.close =	snd_hdspm_midi_output_close,
	.trigger =	snd_hdspm_midi_output_trigger,
};

2054
static struct snd_rawmidi_ops snd_hdspm_midi_input =
T
Takashi Iwai 已提交
2055 2056 2057 2058 2059 2060
{
	.open =		snd_hdspm_midi_input_open,
	.close =	snd_hdspm_midi_input_close,
	.trigger =	snd_hdspm_midi_input_trigger,
};

2061 2062
static int snd_hdspm_create_midi(struct snd_card *card,
				 struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
2063 2064 2065 2066 2067 2068 2069 2070
{
	int err;
	char buf[32];

	hdspm->midi[id].id = id;
	hdspm->midi[id].hdspm = hdspm;
	spin_lock_init (&hdspm->midi[id].lock);

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	if (0 == id) {
		if (MADIface == hdspm->io_type) {
			/* MIDI-over-MADI on HDSPe MADIface */
			hdspm->midi[0].dataIn = HDSPM_midiDataIn2;
			hdspm->midi[0].statusIn = HDSPM_midiStatusIn2;
			hdspm->midi[0].dataOut = HDSPM_midiDataOut2;
			hdspm->midi[0].statusOut = HDSPM_midiStatusOut2;
			hdspm->midi[0].ie = HDSPM_Midi2InterruptEnable;
			hdspm->midi[0].irq = HDSPM_midi2IRQPending;
		} else {
			hdspm->midi[0].dataIn = HDSPM_midiDataIn0;
			hdspm->midi[0].statusIn = HDSPM_midiStatusIn0;
			hdspm->midi[0].dataOut = HDSPM_midiDataOut0;
			hdspm->midi[0].statusOut = HDSPM_midiStatusOut0;
			hdspm->midi[0].ie = HDSPM_Midi0InterruptEnable;
			hdspm->midi[0].irq = HDSPM_midi0IRQPending;
		}
	} else if (1 == id) {
		hdspm->midi[1].dataIn = HDSPM_midiDataIn1;
		hdspm->midi[1].statusIn = HDSPM_midiStatusIn1;
		hdspm->midi[1].dataOut = HDSPM_midiDataOut1;
		hdspm->midi[1].statusOut = HDSPM_midiStatusOut1;
		hdspm->midi[1].ie = HDSPM_Midi1InterruptEnable;
		hdspm->midi[1].irq = HDSPM_midi1IRQPending;
	} else if ((2 == id) && (MADI == hdspm->io_type)) {
		/* MIDI-over-MADI on HDSPe MADI */
		hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
		hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
		hdspm->midi[2].dataOut = HDSPM_midiDataOut2;
		hdspm->midi[2].statusOut = HDSPM_midiStatusOut2;
		hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
		hdspm->midi[2].irq = HDSPM_midi2IRQPending;
	} else if (2 == id) {
		/* TCO MTC, read only */
		hdspm->midi[2].dataIn = HDSPM_midiDataIn2;
		hdspm->midi[2].statusIn = HDSPM_midiStatusIn2;
		hdspm->midi[2].dataOut = -1;
		hdspm->midi[2].statusOut = -1;
		hdspm->midi[2].ie = HDSPM_Midi2InterruptEnable;
		hdspm->midi[2].irq = HDSPM_midi2IRQPendingAES;
	} else if (3 == id) {
		/* TCO MTC on HDSPe MADI */
		hdspm->midi[3].dataIn = HDSPM_midiDataIn3;
		hdspm->midi[3].statusIn = HDSPM_midiStatusIn3;
		hdspm->midi[3].dataOut = -1;
		hdspm->midi[3].statusOut = -1;
		hdspm->midi[3].ie = HDSPM_Midi3InterruptEnable;
		hdspm->midi[3].irq = HDSPM_midi3IRQPending;
	}

	if ((id < 2) || ((2 == id) && ((MADI == hdspm->io_type) ||
					(MADIface == hdspm->io_type)))) {
		if ((id == 0) && (MADIface == hdspm->io_type)) {
			sprintf(buf, "%s MIDIoverMADI", card->shortname);
		} else if ((id == 2) && (MADI == hdspm->io_type)) {
			sprintf(buf, "%s MIDIoverMADI", card->shortname);
		} else {
			sprintf(buf, "%s MIDI %d", card->shortname, id+1);
		}
		err = snd_rawmidi_new(card, buf, id, 1, 1,
				&hdspm->midi[id].rmidi);
		if (err < 0)
			return err;
T
Takashi Iwai 已提交
2134

2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
		sprintf(hdspm->midi[id].rmidi->name, "%s MIDI %d",
				card->id, id+1);
		hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];

		snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
				SNDRV_RAWMIDI_STREAM_OUTPUT,
				&snd_hdspm_midi_output);
		snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
				SNDRV_RAWMIDI_STREAM_INPUT,
				&snd_hdspm_midi_input);

		hdspm->midi[id].rmidi->info_flags |=
			SNDRV_RAWMIDI_INFO_OUTPUT |
			SNDRV_RAWMIDI_INFO_INPUT |
			SNDRV_RAWMIDI_INFO_DUPLEX;
	} else {
		/* TCO MTC, read only */
		sprintf(buf, "%s MTC %d", card->shortname, id+1);
		err = snd_rawmidi_new(card, buf, id, 1, 1,
				&hdspm->midi[id].rmidi);
		if (err < 0)
			return err;

		sprintf(hdspm->midi[id].rmidi->name,
				"%s MTC %d", card->id, id+1);
		hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];
T
Takashi Iwai 已提交
2161

2162 2163 2164
		snd_rawmidi_set_ops(hdspm->midi[id].rmidi,
				SNDRV_RAWMIDI_STREAM_INPUT,
				&snd_hdspm_midi_input);
T
Takashi Iwai 已提交
2165

2166 2167
		hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
	}
T
Takashi Iwai 已提交
2168 2169 2170 2171 2172 2173 2174

	return 0;
}


static void hdspm_midi_tasklet(unsigned long arg)
{
2175
	struct hdspm *hdspm = (struct hdspm *)arg;
2176 2177 2178 2179 2180 2181 2182 2183 2184
	int i = 0;

	while (i < hdspm->midiPorts) {
		if (hdspm->midi[i].pending)
			snd_hdspm_midi_input_read(&hdspm->midi[i]);

		i++;
	}
}
T
Takashi Iwai 已提交
2185 2186 2187 2188 2189 2190 2191 2192


/*-----------------------------------------------------------------------------
  Status Interface
  ----------------------------------------------------------------------------*/

/* get the system sample rate which is set */

2193

2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
static inline int hdspm_get_pll_freq(struct hdspm *hdspm)
{
	unsigned int period, rate;

	period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
	rate = hdspm_calc_dds_value(hdspm, period);

	return rate;
}

2204 2205 2206 2207 2208 2209
/**
 * Calculate the real sample rate from the
 * current DDS value.
 **/
static int hdspm_get_system_sample_rate(struct hdspm *hdspm)
{
2210
	unsigned int rate;
2211

2212
	rate = hdspm_get_pll_freq(hdspm);
2213

2214
	if (rate > 207000) {
2215 2216 2217 2218 2219 2220 2221 2222
		/* Unreasonable high sample rate as seen on PCI MADI cards. */
		if (0 == hdspm_system_clock_mode(hdspm)) {
			/* master mode, return internal sample rate */
			rate = hdspm->system_sample_rate;
		} else {
			/* slave mode, return external sample rate */
			rate = hdspm_external_sample_rate(hdspm);
		}
2223 2224
	}

2225 2226 2227 2228
	return rate;
}


T
Takashi Iwai 已提交
2229
#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
2230 2231 2232 2233 2234 2235 2236 2237
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_system_sample_rate, \
	.put = snd_hdspm_put_system_sample_rate, \
	.get = snd_hdspm_get_system_sample_rate \
T
Takashi Iwai 已提交
2238 2239
}

2240 2241
static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2242 2243 2244
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
2245 2246 2247
	uinfo->value.integer.min = 27000;
	uinfo->value.integer.max = 207000;
	uinfo->value.integer.step = 1;
T
Takashi Iwai 已提交
2248 2249 2250
	return 0;
}

2251

2252 2253
static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *
T
Takashi Iwai 已提交
2254 2255
					    ucontrol)
{
2256
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2257

2258 2259 2260 2261
	ucontrol->value.integer.value[0] = hdspm_get_system_sample_rate(hdspm);
	return 0;
}

2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
static int snd_hdspm_put_system_sample_rate(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *
					    ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	hdspm_set_dds_value(hdspm, ucontrol->value.enumerated.item[0]);
	return 0;
}

2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285

/**
 * Returns the WordClock sample rate class for the given card.
 **/
static int hdspm_get_wc_sample_rate(struct hdspm *hdspm)
{
	int status;

	switch (hdspm->io_type) {
	case RayDAT:
	case AIO:
		status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
		return (status >> 16) & 0xF;
		break;
2286 2287 2288
	case AES32:
		status = hdspm_read(hdspm, HDSPM_statusRegister);
		return (status >> HDSPM_AES32_wcFreq_bit) & 0xF;
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
	default:
		break;
	}


	return 0;
}


/**
 * Returns the TCO sample rate class for the given card.
 **/
static int hdspm_get_tco_sample_rate(struct hdspm *hdspm)
{
	int status;

	if (hdspm->tco) {
		switch (hdspm->io_type) {
		case RayDAT:
		case AIO:
			status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
			return (status >> 20) & 0xF;
			break;
2312 2313 2314
		case AES32:
			status = hdspm_read(hdspm, HDSPM_statusRegister);
			return (status >> 1) & 0xF;
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
		default:
			break;
		}
	}

	return 0;
}


/**
 * Returns the SYNC_IN sample rate class for the given card.
 **/
static int hdspm_get_sync_in_sample_rate(struct hdspm *hdspm)
{
	int status;

	if (hdspm->tco) {
		switch (hdspm->io_type) {
		case RayDAT:
		case AIO:
			status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);
			return (status >> 12) & 0xF;
			break;
		default:
			break;
		}
	}

T
Takashi Iwai 已提交
2343 2344 2345
	return 0;
}

2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
/**
 * Returns the AES sample rate class for the given card.
 **/
static int hdspm_get_aes_sample_rate(struct hdspm *hdspm, int index)
{
	int timecode;

	switch (hdspm->io_type) {
	case AES32:
		timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);
		return (timecode >> (4*index)) & 0xF;
		break;
	default:
		break;
	}
	return 0;
}
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374

/**
 * Returns the sample rate class for input source <idx> for
 * 'new style' cards like the AIO and RayDAT.
 **/
static int hdspm_get_s1_sample_rate(struct hdspm *hdspm, unsigned int idx)
{
	int status = hdspm_read(hdspm, HDSPM_RD_STATUS_2);

	return (status >> (idx*4)) & 0xF;
}

2375
#define ENUMERATED_CTL_INFO(info, texts) \
2376
	snd_ctl_enum_info(info, 1, ARRAY_SIZE(texts), texts)
2377

2378

2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
/* Helper function to query the external sample rate and return the
 * corresponding enum to be returned to userspace.
 */
static int hdspm_external_rate_to_enum(struct hdspm *hdspm)
{
	int rate = hdspm_external_sample_rate(hdspm);
	int i, selected_rate = 0;
	for (i = 1; i < 10; i++)
		if (HDSPM_bit2freq(i) == rate) {
			selected_rate = i;
			break;
		}
	return selected_rate;
}

2394

T
Takashi Iwai 已提交
2395
#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
2396 2397 2398 2399 2400 2401
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ, \
	.info = snd_hdspm_info_autosync_sample_rate, \
	.get = snd_hdspm_get_autosync_sample_rate \
T
Takashi Iwai 已提交
2402 2403
}

2404

2405 2406
static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2407
{
2408
	ENUMERATED_CTL_INFO(uinfo, texts_freq);
T
Takashi Iwai 已提交
2409 2410 2411
	return 0;
}

2412

2413 2414
static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
					      struct snd_ctl_elem_value *
T
Takashi Iwai 已提交
2415 2416
					      ucontrol)
{
2417
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2418

2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
	switch (hdspm->io_type) {
	case RayDAT:
		switch (kcontrol->private_value) {
		case 0:
			ucontrol->value.enumerated.item[0] =
				hdspm_get_wc_sample_rate(hdspm);
			break;
		case 7:
			ucontrol->value.enumerated.item[0] =
				hdspm_get_tco_sample_rate(hdspm);
			break;
		case 8:
			ucontrol->value.enumerated.item[0] =
				hdspm_get_sync_in_sample_rate(hdspm);
			break;
		default:
			ucontrol->value.enumerated.item[0] =
				hdspm_get_s1_sample_rate(hdspm,
						kcontrol->private_value-1);
		}
2439
		break;
T
Takashi Iwai 已提交
2440

2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
	case AIO:
		switch (kcontrol->private_value) {
		case 0: /* WC */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_wc_sample_rate(hdspm);
			break;
		case 4: /* TCO */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_tco_sample_rate(hdspm);
			break;
		case 5: /* SYNC_IN */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_sync_in_sample_rate(hdspm);
			break;
		default:
			ucontrol->value.enumerated.item[0] =
				hdspm_get_s1_sample_rate(hdspm,
2458
						kcontrol->private_value-1);
2459
		}
2460
		break;
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476

	case AES32:

		switch (kcontrol->private_value) {
		case 0: /* WC */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_wc_sample_rate(hdspm);
			break;
		case 9: /* TCO */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_tco_sample_rate(hdspm);
			break;
		case 10: /* SYNC_IN */
			ucontrol->value.enumerated.item[0] =
				hdspm_get_sync_in_sample_rate(hdspm);
			break;
2477 2478 2479 2480
		case 11: /* External Rate */
			ucontrol->value.enumerated.item[0] =
				hdspm_external_rate_to_enum(hdspm);
			break;
2481 2482
		default: /* AES1 to AES8 */
			ucontrol->value.enumerated.item[0] =
2483 2484 2485
				hdspm_get_aes_sample_rate(hdspm,
						kcontrol->private_value -
						HDSPM_AES32_AUTOSYNC_FROM_AES1);
2486 2487
			break;
		}
2488
		break;
2489 2490 2491

	case MADI:
	case MADIface:
2492 2493
		ucontrol->value.enumerated.item[0] =
			hdspm_external_rate_to_enum(hdspm);
2494
		break;
T
Takashi Iwai 已提交
2495
	default:
2496
		break;
T
Takashi Iwai 已提交
2497 2498
	}

2499
	return 0;
T
Takashi Iwai 已提交
2500 2501 2502
}


2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_system_clock_mode, \
	.get = snd_hdspm_get_system_clock_mode, \
	.put = snd_hdspm_put_system_clock_mode, \
}


/**
 * Returns the system clock mode for the given card.
 * @returns 0 - master, 1 - slave
 **/
static int hdspm_system_clock_mode(struct hdspm *hdspm)
{
	switch (hdspm->io_type) {
	case AIO:
	case RayDAT:
		if (hdspm->settings_register & HDSPM_c0Master)
			return 0;
		break;
T
Takashi Iwai 已提交
2527

2528 2529 2530 2531
	default:
		if (hdspm->control_register & HDSPM_ClockModeMaster)
			return 0;
	}
T
Takashi Iwai 已提交
2532 2533 2534 2535

	return 1;
}

2536 2537 2538 2539 2540 2541 2542

/**
 * Sets the system clock mode.
 * @param mode 0 - master, 1 - slave
 **/
static void hdspm_set_system_clock_mode(struct hdspm *hdspm, int mode)
{
2543 2544 2545 2546
	hdspm_set_toggle_setting(hdspm,
			(hdspm_is_raydat_or_aio(hdspm)) ?
			HDSPM_c0Master : HDSPM_ClockModeMaster,
			(0 == mode));
2547 2548 2549 2550
}


static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
2551
					    struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2552
{
2553
	static const char *const texts[] = { "Master", "AutoSync" };
2554
	ENUMERATED_CTL_INFO(uinfo, texts);
T
Takashi Iwai 已提交
2555 2556 2557
	return 0;
}

2558 2559
static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2560
{
2561
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2562

2563
	ucontrol->value.enumerated.item[0] = hdspm_system_clock_mode(hdspm);
T
Takashi Iwai 已提交
2564 2565 2566
	return 0;
}

2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
static int snd_hdspm_put_system_clock_mode(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;

	val = ucontrol->value.enumerated.item[0];
	if (val < 0)
		val = 0;
	else if (val > 1)
		val = 1;

	hdspm_set_system_clock_mode(hdspm, val);

	return 0;
}


#define HDSPM_INTERNAL_CLOCK(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.info = snd_hdspm_info_clock_source, \
	.get = snd_hdspm_get_clock_source, \
	.put = snd_hdspm_put_clock_source \
T
Takashi Iwai 已提交
2595 2596
}

2597

2598
static int hdspm_clock_source(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2599
{
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
	switch (hdspm->system_sample_rate) {
	case 32000: return 0;
	case 44100: return 1;
	case 48000: return 2;
	case 64000: return 3;
	case 88200: return 4;
	case 96000: return 5;
	case 128000: return 6;
	case 176400: return 7;
	case 192000: return 8;
T
Takashi Iwai 已提交
2610
	}
2611 2612

	return -1;
T
Takashi Iwai 已提交
2613 2614
}

2615
static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
T
Takashi Iwai 已提交
2616 2617 2618
{
	int rate;
	switch (mode) {
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
	case 0:
		rate = 32000; break;
	case 1:
		rate = 44100; break;
	case 2:
		rate = 48000; break;
	case 3:
		rate = 64000; break;
	case 4:
		rate = 88200; break;
	case 5:
		rate = 96000; break;
	case 6:
		rate = 128000; break;
	case 7:
		rate = 176400; break;
	case 8:
		rate = 192000; break;
T
Takashi Iwai 已提交
2637
	default:
2638
		rate = 48000;
T
Takashi Iwai 已提交
2639 2640 2641 2642 2643
	}
	hdspm_set_rate(hdspm, rate, 1);
	return 0;
}

2644 2645
static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2646
{
2647
	return snd_ctl_enum_info(uinfo, 1, 9, texts_freq + 1);
T
Takashi Iwai 已提交
2648 2649
}

2650 2651
static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2652
{
2653
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2654 2655 2656 2657 2658

	ucontrol->value.enumerated.item[0] = hdspm_clock_source(hdspm);
	return 0;
}

2659 2660
static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2661
{
2662
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2663 2664 2665 2666 2667 2668 2669 2670
	int change;
	int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0];
	if (val < 0)
		val = 0;
R
Remy Bruno 已提交
2671 2672
	if (val > 9)
		val = 9;
T
Takashi Iwai 已提交
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
	spin_lock_irq(&hdspm->lock);
	if (val != hdspm_clock_source(hdspm))
		change = (hdspm_set_clock_source(hdspm, val) == 0) ? 1 : 0;
	else
		change = 0;
	spin_unlock_irq(&hdspm->lock);
	return change;
}


2683
#define HDSPM_PREF_SYNC_REF(xname, xindex) \
2684
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
			SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_pref_sync_ref, \
	.get = snd_hdspm_get_pref_sync_ref, \
	.put = snd_hdspm_put_pref_sync_ref \
}


/**
 * Returns the current preferred sync reference setting.
 * The semantics of the return value are depending on the
 * card, please see the comments for clarification.
 **/
2700
static int hdspm_pref_sync_ref(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2701
{
2702 2703
	switch (hdspm->io_type) {
	case AES32:
R
Remy Bruno 已提交
2704
		switch (hdspm->control_register & HDSPM_SyncRefMask) {
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
		case 0: return 0;  /* WC */
		case HDSPM_SyncRef0: return 1; /* AES 1 */
		case HDSPM_SyncRef1: return 2; /* AES 2 */
		case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3; /* AES 3 */
		case HDSPM_SyncRef2: return 4; /* AES 4 */
		case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5; /* AES 5 */
		case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6; /* AES 6 */
		case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0:
						    return 7; /* AES 7 */
		case HDSPM_SyncRef3: return 8; /* AES 8 */
		case HDSPM_SyncRef3+HDSPM_SyncRef0: return 9; /* TCO */
R
Remy Bruno 已提交
2716
		}
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
		break;

	case MADI:
	case MADIface:
		if (hdspm->tco) {
			switch (hdspm->control_register & HDSPM_SyncRefMask) {
			case 0: return 0;  /* WC */
			case HDSPM_SyncRef0: return 1;  /* MADI */
			case HDSPM_SyncRef1: return 2;  /* TCO */
			case HDSPM_SyncRef1+HDSPM_SyncRef0:
					     return 3;  /* SYNC_IN */
			}
		} else {
			switch (hdspm->control_register & HDSPM_SyncRefMask) {
			case 0: return 0;  /* WC */
			case HDSPM_SyncRef0: return 1;  /* MADI */
			case HDSPM_SyncRef1+HDSPM_SyncRef0:
					     return 2;  /* SYNC_IN */
			}
		}
		break;

	case RayDAT:
		if (hdspm->tco) {
			switch ((hdspm->settings_register &
				HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
			case 0: return 0;  /* WC */
			case 3: return 1;  /* ADAT 1 */
			case 4: return 2;  /* ADAT 2 */
			case 5: return 3;  /* ADAT 3 */
			case 6: return 4;  /* ADAT 4 */
			case 1: return 5;  /* AES */
			case 2: return 6;  /* SPDIF */
			case 9: return 7;  /* TCO */
			case 10: return 8; /* SYNC_IN */
			}
		} else {
			switch ((hdspm->settings_register &
				HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
			case 0: return 0;  /* WC */
			case 3: return 1;  /* ADAT 1 */
			case 4: return 2;  /* ADAT 2 */
			case 5: return 3;  /* ADAT 3 */
			case 6: return 4;  /* ADAT 4 */
			case 1: return 5;  /* AES */
			case 2: return 6;  /* SPDIF */
			case 10: return 7; /* SYNC_IN */
			}
R
Remy Bruno 已提交
2765
		}
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791

		break;

	case AIO:
		if (hdspm->tco) {
			switch ((hdspm->settings_register &
				HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
			case 0: return 0;  /* WC */
			case 3: return 1;  /* ADAT */
			case 1: return 2;  /* AES */
			case 2: return 3;  /* SPDIF */
			case 9: return 4;  /* TCO */
			case 10: return 5; /* SYNC_IN */
			}
		} else {
			switch ((hdspm->settings_register &
				HDSPM_c0_SyncRefMask) / HDSPM_c0_SyncRef0) {
			case 0: return 0;  /* WC */
			case 3: return 1;  /* ADAT */
			case 1: return 2;  /* AES */
			case 2: return 3;  /* SPDIF */
			case 10: return 4; /* SYNC_IN */
			}
		}

		break;
T
Takashi Iwai 已提交
2792 2793
	}

2794
	return -1;
T
Takashi Iwai 已提交
2795 2796
}

2797 2798 2799 2800 2801 2802

/**
 * Set the preferred sync reference to <pref>. The semantics
 * of <pref> are depending on the card type, see the comments
 * for clarification.
 **/
2803
static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
T
Takashi Iwai 已提交
2804
{
2805
	int p = 0;
T
Takashi Iwai 已提交
2806

2807 2808 2809
	switch (hdspm->io_type) {
	case AES32:
		hdspm->control_register &= ~HDSPM_SyncRefMask;
R
Remy Bruno 已提交
2810
		switch (pref) {
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
		case 0: /* WC  */
			break;
		case 1: /* AES 1 */
			hdspm->control_register |= HDSPM_SyncRef0;
			break;
		case 2: /* AES 2 */
			hdspm->control_register |= HDSPM_SyncRef1;
			break;
		case 3: /* AES 3 */
			hdspm->control_register |=
				HDSPM_SyncRef1+HDSPM_SyncRef0;
			break;
		case 4: /* AES 4 */
			hdspm->control_register |= HDSPM_SyncRef2;
			break;
		case 5: /* AES 5 */
			hdspm->control_register |=
				HDSPM_SyncRef2+HDSPM_SyncRef0;
			break;
		case 6: /* AES 6 */
			hdspm->control_register |=
				HDSPM_SyncRef2+HDSPM_SyncRef1;
			break;
		case 7: /* AES 7 */
			hdspm->control_register |=
				HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
R
Remy Bruno 已提交
2837
			break;
2838 2839 2840 2841 2842 2843
		case 8: /* AES 8 */
			hdspm->control_register |= HDSPM_SyncRef3;
			break;
		case 9: /* TCO */
			hdspm->control_register |=
				HDSPM_SyncRef3+HDSPM_SyncRef0;
R
Remy Bruno 已提交
2844 2845 2846 2847
			break;
		default:
			return -1;
		}
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939

		break;

	case MADI:
	case MADIface:
		hdspm->control_register &= ~HDSPM_SyncRefMask;
		if (hdspm->tco) {
			switch (pref) {
			case 0: /* WC */
				break;
			case 1: /* MADI */
				hdspm->control_register |= HDSPM_SyncRef0;
				break;
			case 2: /* TCO */
				hdspm->control_register |= HDSPM_SyncRef1;
				break;
			case 3: /* SYNC_IN */
				hdspm->control_register |=
					HDSPM_SyncRef0+HDSPM_SyncRef1;
				break;
			default:
				return -1;
			}
		} else {
			switch (pref) {
			case 0: /* WC */
				break;
			case 1: /* MADI */
				hdspm->control_register |= HDSPM_SyncRef0;
				break;
			case 2: /* SYNC_IN */
				hdspm->control_register |=
					HDSPM_SyncRef0+HDSPM_SyncRef1;
				break;
			default:
				return -1;
			}
		}

		break;

	case RayDAT:
		if (hdspm->tco) {
			switch (pref) {
			case 0: p = 0; break;  /* WC */
			case 1: p = 3; break;  /* ADAT 1 */
			case 2: p = 4; break;  /* ADAT 2 */
			case 3: p = 5; break;  /* ADAT 3 */
			case 4: p = 6; break;  /* ADAT 4 */
			case 5: p = 1; break;  /* AES */
			case 6: p = 2; break;  /* SPDIF */
			case 7: p = 9; break;  /* TCO */
			case 8: p = 10; break; /* SYNC_IN */
			default: return -1;
			}
		} else {
			switch (pref) {
			case 0: p = 0; break;  /* WC */
			case 1: p = 3; break;  /* ADAT 1 */
			case 2: p = 4; break;  /* ADAT 2 */
			case 3: p = 5; break;  /* ADAT 3 */
			case 4: p = 6; break;  /* ADAT 4 */
			case 5: p = 1; break;  /* AES */
			case 6: p = 2; break;  /* SPDIF */
			case 7: p = 10; break; /* SYNC_IN */
			default: return -1;
			}
		}
		break;

	case AIO:
		if (hdspm->tco) {
			switch (pref) {
			case 0: p = 0; break;  /* WC */
			case 1: p = 3; break;  /* ADAT */
			case 2: p = 1; break;  /* AES */
			case 3: p = 2; break;  /* SPDIF */
			case 4: p = 9; break;  /* TCO */
			case 5: p = 10; break; /* SYNC_IN */
			default: return -1;
			}
		} else {
			switch (pref) {
			case 0: p = 0; break;  /* WC */
			case 1: p = 3; break;  /* ADAT */
			case 2: p = 1; break;  /* AES */
			case 3: p = 2; break;  /* SPDIF */
			case 4: p = 10; break; /* SYNC_IN */
			default: return -1;
			}
		}
		break;
T
Takashi Iwai 已提交
2940
	}
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956

	switch (hdspm->io_type) {
	case RayDAT:
	case AIO:
		hdspm->settings_register &= ~HDSPM_c0_SyncRefMask;
		hdspm->settings_register |= HDSPM_c0_SyncRef0 * p;
		hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
		break;

	case MADI:
	case MADIface:
	case AES32:
		hdspm_write(hdspm, HDSPM_controlRegister,
				hdspm->control_register);
	}

T
Takashi Iwai 已提交
2957 2958 2959
	return 0;
}

2960

2961 2962
static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2963
{
R
Remy Bruno 已提交
2964
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2965

2966
	snd_ctl_enum_info(uinfo, 1, hdspm->texts_autosync_items, hdspm->texts_autosync);
R
Remy Bruno 已提交
2967

T
Takashi Iwai 已提交
2968 2969 2970
	return 0;
}

2971 2972
static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2973
{
2974
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2975
	int psf = hdspm_pref_sync_ref(hdspm);
T
Takashi Iwai 已提交
2976

2977 2978 2979 2980 2981 2982
	if (psf >= 0) {
		ucontrol->value.enumerated.item[0] = psf;
		return 0;
	}

	return -1;
T
Takashi Iwai 已提交
2983 2984
}

2985 2986
static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2987
{
2988
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
2989
	int val, change = 0;
T
Takashi Iwai 已提交
2990 2991 2992 2993

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;

2994 2995 2996 2997 2998 2999
	val = ucontrol->value.enumerated.item[0];

	if (val < 0)
		val = 0;
	else if (val >= hdspm->texts_autosync_items)
		val = hdspm->texts_autosync_items-1;
T
Takashi Iwai 已提交
3000 3001

	spin_lock_irq(&hdspm->lock);
3002 3003 3004
	if (val != hdspm_pref_sync_ref(hdspm))
		change = (0 == hdspm_set_pref_sync_ref(hdspm, val)) ? 1 : 0;

T
Takashi Iwai 已提交
3005 3006 3007 3008
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3009

T
Takashi Iwai 已提交
3010
#define HDSPM_AUTOSYNC_REF(xname, xindex) \
3011 3012 3013 3014 3015 3016
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ, \
	.info = snd_hdspm_info_autosync_ref, \
	.get = snd_hdspm_get_autosync_ref, \
T
Takashi Iwai 已提交
3017 3018
}

3019
static int hdspm_autosync_ref(struct hdspm *hdspm)
T
Takashi Iwai 已提交
3020
{
3021
	/* This looks at the autosync selected sync reference */
3022
	if (AES32 == hdspm->io_type) {
3023

R
Remy Bruno 已提交
3024
		unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
3025 3026 3027
		unsigned int syncref = (status >> HDSPM_AES32_syncref_bit) & 0xF;
		if ((syncref >= HDSPM_AES32_AUTOSYNC_FROM_WORD) &&
				(syncref <= HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN)) {
R
Remy Bruno 已提交
3028
			return syncref;
3029
		}
R
Remy Bruno 已提交
3030
		return HDSPM_AES32_AUTOSYNC_FROM_NONE;
3031

3032
	} else if (MADI == hdspm->io_type) {
R
Remy Bruno 已提交
3033

3034
		unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
R
Remy Bruno 已提交
3035 3036 3037 3038 3039
		switch (status2 & HDSPM_SelSyncRefMask) {
		case HDSPM_SelSyncRef_WORD:
			return HDSPM_AUTOSYNC_FROM_WORD;
		case HDSPM_SelSyncRef_MADI:
			return HDSPM_AUTOSYNC_FROM_MADI;
3040 3041 3042 3043
		case HDSPM_SelSyncRef_TCO:
			return HDSPM_AUTOSYNC_FROM_TCO;
		case HDSPM_SelSyncRef_SyncIn:
			return HDSPM_AUTOSYNC_FROM_SYNC_IN;
R
Remy Bruno 已提交
3044 3045 3046
		case HDSPM_SelSyncRef_NVALID:
			return HDSPM_AUTOSYNC_FROM_NONE;
		default:
3047
			return HDSPM_AUTOSYNC_FROM_NONE;
R
Remy Bruno 已提交
3048
		}
T
Takashi Iwai 已提交
3049 3050

	}
3051
	return 0;
T
Takashi Iwai 已提交
3052 3053
}

3054

3055 3056
static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3057
{
R
Remy Bruno 已提交
3058
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3059

3060
	if (AES32 == hdspm->io_type) {
3061
		static const char *const texts[] = { "WordClock", "AES1", "AES2", "AES3",
3062
			"AES4",	"AES5", "AES6", "AES7", "AES8", "TCO", "Sync In", "None"};
R
Remy Bruno 已提交
3063

3064
		ENUMERATED_CTL_INFO(uinfo, texts);
3065
	} else if (MADI == hdspm->io_type) {
3066
		static const char *const texts[] = {"Word Clock", "MADI", "TCO",
3067
			"Sync In", "None" };
R
Remy Bruno 已提交
3068

3069
		ENUMERATED_CTL_INFO(uinfo, texts);
R
Remy Bruno 已提交
3070
	}
T
Takashi Iwai 已提交
3071 3072 3073
	return 0;
}

3074 3075
static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3076
{
3077
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3078

R
Remy Bruno 已提交
3079
	ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
T
Takashi Iwai 已提交
3080 3081 3082
	return 0;
}

3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096


#define HDSPM_TCO_VIDEO_INPUT_FORMAT(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_video_input_format, \
	.get = snd_hdspm_get_tco_video_input_format, \
}

static int snd_hdspm_info_tco_video_input_format(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
3097
	static const char *const texts[] = {"No video", "NTSC", "PAL"};
3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
	ENUMERATED_CTL_INFO(uinfo, texts);
	return 0;
}

static int snd_hdspm_get_tco_video_input_format(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	u32 status;
	int ret = 0;

	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
	switch (status & (HDSPM_TCO1_Video_Input_Format_NTSC |
			HDSPM_TCO1_Video_Input_Format_PAL)) {
	case HDSPM_TCO1_Video_Input_Format_NTSC:
		/* ntsc */
		ret = 1;
		break;
	case HDSPM_TCO1_Video_Input_Format_PAL:
		/* pal */
		ret = 2;
		break;
	default:
		/* no video */
		ret = 0;
		break;
	}
	ucontrol->value.enumerated.item[0] = ret;
	return 0;
}



#define HDSPM_TCO_LTC_FRAMES(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_ltc_frames, \
	.get = snd_hdspm_get_tco_ltc_frames, \
}

static int snd_hdspm_info_tco_ltc_frames(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
3143
	static const char *const texts[] = {"No lock", "24 fps", "25 fps", "29.97 fps",
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
				"30 fps"};
	ENUMERATED_CTL_INFO(uinfo, texts);
	return 0;
}

static int hdspm_tco_ltc_frames(struct hdspm *hdspm)
{
	u32 status;
	int ret = 0;

	status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
	if (status & HDSPM_TCO1_LTC_Input_valid) {
		switch (status & (HDSPM_TCO1_LTC_Format_LSB |
					HDSPM_TCO1_LTC_Format_MSB)) {
		case 0:
			/* 24 fps */
3160
			ret = fps_24;
3161 3162 3163
			break;
		case HDSPM_TCO1_LTC_Format_LSB:
			/* 25 fps */
3164
			ret = fps_25;
3165 3166
			break;
		case HDSPM_TCO1_LTC_Format_MSB:
3167 3168
			/* 29.97 fps */
			ret = fps_2997;
3169 3170 3171
			break;
		default:
			/* 30 fps */
3172
			ret = fps_30;
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
			break;
		}
	}

	return ret;
}

static int snd_hdspm_get_tco_ltc_frames(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm_tco_ltc_frames(hdspm);
	return 0;
}

3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
#define HDSPM_TOGGLE_SETTING(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.info = snd_hdspm_info_toggle_setting, \
	.get = snd_hdspm_get_toggle_setting, \
	.put = snd_hdspm_put_toggle_setting \
}

static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask)
{
3200 3201 3202 3203 3204 3205 3206 3207
	u32 reg;

	if (hdspm_is_raydat_or_aio(hdspm))
		reg = hdspm->settings_register;
	else
		reg = hdspm->control_register;

	return (reg & regmask) ? 1 : 0;
3208 3209 3210 3211
}

static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out)
{
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
	u32 *reg;
	u32 target_reg;

	if (hdspm_is_raydat_or_aio(hdspm)) {
		reg = &(hdspm->settings_register);
		target_reg = HDSPM_WR_SETTINGS;
	} else {
		reg = &(hdspm->control_register);
		target_reg = HDSPM_controlRegister;
	}

3223
	if (out)
3224
		*reg |= regmask;
3225
	else
3226 3227 3228
		*reg &= ~regmask;

	hdspm_write(hdspm, target_reg, *reg);
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264

	return 0;
}

#define snd_hdspm_info_toggle_setting		snd_ctl_boolean_mono_info

static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;
	int change;
	unsigned int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdspm->lock);
	change = (int) val != hdspm_toggle_setting(hdspm, regmask);
	hdspm_set_toggle_setting(hdspm, regmask, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

R
Remy Bruno 已提交
3265
#define HDSPM_INPUT_SELECT(xname, xindex) \
3266 3267 3268 3269 3270 3271
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.info = snd_hdspm_info_input_select, \
	.get = snd_hdspm_get_input_select, \
	.put = snd_hdspm_put_input_select \
R
Remy Bruno 已提交
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
}

static int hdspm_input_select(struct hdspm * hdspm)
{
	return (hdspm->control_register & HDSPM_InputSelect0) ? 1 : 0;
}

static int hdspm_set_input_select(struct hdspm * hdspm, int out)
{
	if (out)
		hdspm->control_register |= HDSPM_InputSelect0;
	else
		hdspm->control_register &= ~HDSPM_InputSelect0;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

static int snd_hdspm_info_input_select(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
3293
	static const char *const texts[] = { "optical", "coaxial" };
3294
	ENUMERATED_CTL_INFO(uinfo, texts);
R
Remy Bruno 已提交
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
	return 0;
}

static int snd_hdspm_get_input_select(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.enumerated.item[0] = hdspm_input_select(hdspm);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_input_select(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	int change;
	unsigned int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdspm->lock);
	change = (int) val != hdspm_input_select(hdspm);
	hdspm_set_input_select(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3326

R
Remy Bruno 已提交
3327
#define HDSPM_DS_WIRE(xname, xindex) \
3328 3329 3330 3331 3332 3333
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.info = snd_hdspm_info_ds_wire, \
	.get = snd_hdspm_get_ds_wire, \
	.put = snd_hdspm_put_ds_wire \
R
Remy Bruno 已提交
3334 3335 3336
}

static int hdspm_ds_wire(struct hdspm * hdspm)
T
Takashi Iwai 已提交
3337
{
R
Remy Bruno 已提交
3338
	return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
T
Takashi Iwai 已提交
3339 3340
}

R
Remy Bruno 已提交
3341
static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
T
Takashi Iwai 已提交
3342
{
R
Remy Bruno 已提交
3343 3344
	if (ds)
		hdspm->control_register |= HDSPM_DS_DoubleWire;
T
Takashi Iwai 已提交
3345
	else
R
Remy Bruno 已提交
3346
		hdspm->control_register &= ~HDSPM_DS_DoubleWire;
T
Takashi Iwai 已提交
3347 3348 3349 3350 3351
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

R
Remy Bruno 已提交
3352 3353
static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3354
{
3355
	static const char *const texts[] = { "Single", "Double" };
3356
	ENUMERATED_CTL_INFO(uinfo, texts);
T
Takashi Iwai 已提交
3357 3358 3359
	return 0;
}

R
Remy Bruno 已提交
3360 3361
static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3362
{
3363
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3364 3365

	spin_lock_irq(&hdspm->lock);
R
Remy Bruno 已提交
3366
	ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
T
Takashi Iwai 已提交
3367 3368 3369 3370
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

R
Remy Bruno 已提交
3371 3372
static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3373
{
3374
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3375 3376 3377 3378 3379 3380 3381
	int change;
	unsigned int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&hdspm->lock);
R
Remy Bruno 已提交
3382 3383
	change = (int) val != hdspm_ds_wire(hdspm);
	hdspm_set_ds_wire(hdspm, val);
T
Takashi Iwai 已提交
3384 3385 3386 3387
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3388

R
Remy Bruno 已提交
3389
#define HDSPM_QS_WIRE(xname, xindex) \
3390 3391 3392 3393 3394 3395
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.info = snd_hdspm_info_qs_wire, \
	.get = snd_hdspm_get_qs_wire, \
	.put = snd_hdspm_put_qs_wire \
T
Takashi Iwai 已提交
3396 3397
}

R
Remy Bruno 已提交
3398
static int hdspm_qs_wire(struct hdspm * hdspm)
T
Takashi Iwai 已提交
3399
{
R
Remy Bruno 已提交
3400 3401 3402 3403 3404
	if (hdspm->control_register & HDSPM_QS_DoubleWire)
		return 1;
	if (hdspm->control_register & HDSPM_QS_QuadWire)
		return 2;
	return 0;
T
Takashi Iwai 已提交
3405 3406
}

R
Remy Bruno 已提交
3407
static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
T
Takashi Iwai 已提交
3408
{
R
Remy Bruno 已提交
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
	hdspm->control_register &= ~(HDSPM_QS_DoubleWire | HDSPM_QS_QuadWire);
	switch (mode) {
	case 0:
		break;
	case 1:
		hdspm->control_register |= HDSPM_QS_DoubleWire;
		break;
	case 2:
		hdspm->control_register |= HDSPM_QS_QuadWire;
		break;
	}
T
Takashi Iwai 已提交
3420 3421 3422 3423 3424
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

R
Remy Bruno 已提交
3425
static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
3426
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3427
{
3428
	static const char *const texts[] = { "Single", "Double", "Quad" };
3429
	ENUMERATED_CTL_INFO(uinfo, texts);
T
Takashi Iwai 已提交
3430 3431 3432
	return 0;
}

R
Remy Bruno 已提交
3433
static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
3434
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3435
{
3436
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3437 3438

	spin_lock_irq(&hdspm->lock);
R
Remy Bruno 已提交
3439
	ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
T
Takashi Iwai 已提交
3440 3441 3442 3443
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

R
Remy Bruno 已提交
3444
static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
3445
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3446
{
3447
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3448
	int change;
R
Remy Bruno 已提交
3449
	int val;
T
Takashi Iwai 已提交
3450 3451 3452

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
R
Remy Bruno 已提交
3453 3454 3455 3456 3457
	val = ucontrol->value.integer.value[0];
	if (val < 0)
		val = 0;
	if (val > 2)
		val = 2;
T
Takashi Iwai 已提交
3458
	spin_lock_irq(&hdspm->lock);
T
Takashi Iwai 已提交
3459
	change = val != hdspm_qs_wire(hdspm);
R
Remy Bruno 已提交
3460
	hdspm_set_qs_wire(hdspm, val);
T
Takashi Iwai 已提交
3461 3462 3463 3464
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493
#define HDSPM_CONTROL_TRISTATE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.info = snd_hdspm_info_tristate, \
	.get = snd_hdspm_get_tristate, \
	.put = snd_hdspm_put_tristate \
}

static int hdspm_tristate(struct hdspm *hdspm, u32 regmask)
{
	u32 reg = hdspm->settings_register & (regmask * 3);
	return reg / regmask;
}

static int hdspm_set_tristate(struct hdspm *hdspm, int mode, u32 regmask)
{
	hdspm->settings_register &= ~(regmask * 3);
	hdspm->settings_register |= (regmask * mode);
	hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);

	return 0;
}

static int snd_hdspm_info_tristate(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
	u32 regmask = kcontrol->private_value;

3494 3495
	static const char *const texts_spdif[] = { "Optical", "Coaxial", "Internal" };
	static const char *const texts_levels[] = { "Hi Gain", "+4 dBu", "-10 dBV" };
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542

	switch (regmask) {
	case HDSPM_c0_Input0:
		ENUMERATED_CTL_INFO(uinfo, texts_spdif);
		break;
	default:
		ENUMERATED_CTL_INFO(uinfo, texts_levels);
		break;
	}
	return 0;
}

static int snd_hdspm_get_tristate(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.enumerated.item[0] = hdspm_tristate(hdspm, regmask);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_tristate(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	u32 regmask = kcontrol->private_value;
	int change;
	int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.integer.value[0];
	if (val < 0)
		val = 0;
	if (val > 2)
		val = 2;

	spin_lock_irq(&hdspm->lock);
	change = val != hdspm_tristate(hdspm, regmask);
	hdspm_set_tristate(hdspm, val, regmask);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
#define HDSPM_MADI_SPEEDMODE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.info = snd_hdspm_info_madi_speedmode, \
	.get = snd_hdspm_get_madi_speedmode, \
	.put = snd_hdspm_put_madi_speedmode \
}

static int hdspm_madi_speedmode(struct hdspm *hdspm)
{
	if (hdspm->control_register & HDSPM_QuadSpeed)
		return 2;
	if (hdspm->control_register & HDSPM_DoubleSpeed)
		return 1;
	return 0;
}

static int hdspm_set_madi_speedmode(struct hdspm *hdspm, int mode)
{
	hdspm->control_register &= ~(HDSPM_DoubleSpeed | HDSPM_QuadSpeed);
	switch (mode) {
	case 0:
		break;
	case 1:
		hdspm->control_register |= HDSPM_DoubleSpeed;
		break;
	case 2:
		hdspm->control_register |= HDSPM_QuadSpeed;
		break;
	}
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

static int snd_hdspm_info_madi_speedmode(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
{
3582
	static const char *const texts[] = { "Single", "Double", "Quad" };
3583
	ENUMERATED_CTL_INFO(uinfo, texts);
3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
	return 0;
}

static int snd_hdspm_get_madi_speedmode(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.enumerated.item[0] = hdspm_madi_speedmode(hdspm);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_madi_speedmode(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	int change;
	int val;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
	val = ucontrol->value.integer.value[0];
	if (val < 0)
		val = 0;
	if (val > 2)
		val = 2;
	spin_lock_irq(&hdspm->lock);
	change = val != hdspm_madi_speedmode(hdspm);
	hdspm_set_madi_speedmode(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}
T
Takashi Iwai 已提交
3618 3619

#define HDSPM_MIXER(xname, xindex) \
3620 3621 3622 3623 3624 3625 3626 3627 3628
{	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
	.name = xname, \
	.index = xindex, \
	.device = 0, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_mixer, \
	.get = snd_hdspm_get_mixer, \
	.put = snd_hdspm_put_mixer \
T
Takashi Iwai 已提交
3629 3630
}

3631 3632
static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3633 3634 3635 3636 3637 3638 3639 3640 3641
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 3;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 65535;
	uinfo->value.integer.step = 1;
	return 0;
}

3642 3643
static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3644
{
3645
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674
	int source;
	int destination;

	source = ucontrol->value.integer.value[0];
	if (source < 0)
		source = 0;
	else if (source >= 2 * HDSPM_MAX_CHANNELS)
		source = 2 * HDSPM_MAX_CHANNELS - 1;

	destination = ucontrol->value.integer.value[1];
	if (destination < 0)
		destination = 0;
	else if (destination >= HDSPM_MAX_CHANNELS)
		destination = HDSPM_MAX_CHANNELS - 1;

	spin_lock_irq(&hdspm->lock);
	if (source >= HDSPM_MAX_CHANNELS)
		ucontrol->value.integer.value[2] =
		    hdspm_read_pb_gain(hdspm, destination,
				       source - HDSPM_MAX_CHANNELS);
	else
		ucontrol->value.integer.value[2] =
		    hdspm_read_in_gain(hdspm, destination, source);

	spin_unlock_irq(&hdspm->lock);

	return 0;
}

3675 3676
static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3677
{
3678
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703
	int change;
	int source;
	int destination;
	int gain;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;

	source = ucontrol->value.integer.value[0];
	destination = ucontrol->value.integer.value[1];

	if (source < 0 || source >= 2 * HDSPM_MAX_CHANNELS)
		return -1;
	if (destination < 0 || destination >= HDSPM_MAX_CHANNELS)
		return -1;

	gain = ucontrol->value.integer.value[2];

	spin_lock_irq(&hdspm->lock);

	if (source >= HDSPM_MAX_CHANNELS)
		change = gain != hdspm_read_pb_gain(hdspm, destination,
						    source -
						    HDSPM_MAX_CHANNELS);
	else
T
Takashi Iwai 已提交
3704 3705
		change = gain != hdspm_read_in_gain(hdspm, destination,
						    source);
T
Takashi Iwai 已提交
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722

	if (change) {
		if (source >= HDSPM_MAX_CHANNELS)
			hdspm_write_pb_gain(hdspm, destination,
					    source - HDSPM_MAX_CHANNELS,
					    gain);
		else
			hdspm_write_in_gain(hdspm, destination, source,
					    gain);
	}
	spin_unlock_irq(&hdspm->lock);

	return change;
}

/* The simple mixer control(s) provide gain control for the
   basic 1:1 mappings of playback streams to output
3723
   streams.
T
Takashi Iwai 已提交
3724 3725 3726
*/

#define HDSPM_PLAYBACK_MIXER \
3727 3728 3729 3730 3731 3732
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE | \
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_playback_mixer, \
	.get = snd_hdspm_get_playback_mixer, \
	.put = snd_hdspm_put_playback_mixer \
T
Takashi Iwai 已提交
3733 3734
}

3735 3736
static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3737 3738 3739 3740
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
3741
	uinfo->value.integer.max = 64;
T
Takashi Iwai 已提交
3742 3743 3744 3745
	uinfo->value.integer.step = 1;
	return 0;
}

3746 3747
static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3748
{
3749
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3750 3751 3752 3753
	int channel;

	channel = ucontrol->id.index - 1;

3754 3755
	if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
		return -EINVAL;
T
Takashi Iwai 已提交
3756 3757 3758

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.integer.value[0] =
3759
	  (hdspm_read_pb_gain(hdspm, channel, channel)*64)/UNITY_GAIN;
T
Takashi Iwai 已提交
3760 3761 3762 3763 3764
	spin_unlock_irq(&hdspm->lock);

	return 0;
}

3765 3766
static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
3767
{
3768
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
3769 3770 3771 3772 3773 3774 3775 3776 3777
	int change;
	int channel;
	int gain;

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;

	channel = ucontrol->id.index - 1;

3778 3779
	if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
		return -EINVAL;
T
Takashi Iwai 已提交
3780

3781
	gain = ucontrol->value.integer.value[0]*UNITY_GAIN/64;
T
Takashi Iwai 已提交
3782 3783 3784

	spin_lock_irq(&hdspm->lock);
	change =
3785 3786
	    gain != hdspm_read_pb_gain(hdspm, channel,
				       channel);
T
Takashi Iwai 已提交
3787
	if (change)
3788
		hdspm_write_pb_gain(hdspm, channel, channel,
T
Takashi Iwai 已提交
3789 3790 3791 3792 3793
				    gain);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

3794 3795 3796 3797 3798 3799 3800
#define HDSPM_SYNC_CHECK(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_sync_check, \
	.get = snd_hdspm_get_sync_check \
T
Takashi Iwai 已提交
3801 3802
}

3803 3804 3805 3806 3807 3808 3809 3810 3811 3812
#define HDSPM_TCO_LOCK_CHECK(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_tco_info_lock_check, \
	.get = snd_hdspm_get_sync_check \
}


3813

3814 3815
static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
3816
{
3817
	static const char *const texts[] = { "No Lock", "Lock", "Sync", "N/A" };
3818
	ENUMERATED_CTL_INFO(uinfo, texts);
T
Takashi Iwai 已提交
3819 3820 3821
	return 0;
}

3822 3823 3824
static int snd_hdspm_tco_info_lock_check(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_info *uinfo)
{
3825
	static const char *const texts[] = { "No Lock", "Lock" };
3826 3827 3828 3829
	ENUMERATED_CTL_INFO(uinfo, texts);
	return 0;
}

3830
static int hdspm_wc_sync_check(struct hdspm *hdspm)
T
Takashi Iwai 已提交
3831
{
3832 3833 3834 3835 3836
	int status, status2;

	switch (hdspm->io_type) {
	case AES32:
		status = hdspm_read(hdspm, HDSPM_statusRegister);
3837 3838 3839 3840 3841 3842
		if (status & HDSPM_AES32_wcLock) {
			if (status & HDSPM_AES32_wcSync)
				return 2;
			else
				return 1;
		}
R
Remy Bruno 已提交
3843
		return 0;
3844 3845 3846 3847
		break;

	case MADI:
		status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
R
Remy Bruno 已提交
3848 3849 3850 3851 3852 3853 3854
		if (status2 & HDSPM_wcLock) {
			if (status2 & HDSPM_wcSync)
				return 2;
			else
				return 1;
		}
		return 0;
3855
		break;
T
Takashi Iwai 已提交
3856

3857 3858 3859
	case RayDAT:
	case AIO:
		status = hdspm_read(hdspm, HDSPM_statusRegister);
T
Takashi Iwai 已提交
3860

3861 3862 3863 3864 3865
		if (status & 0x2000000)
			return 2;
		else if (status & 0x1000000)
			return 1;
		return 0;
T
Takashi Iwai 已提交
3866

3867
		break;
T
Takashi Iwai 已提交
3868

3869 3870 3871 3872 3873 3874
	case MADIface:
		break;
	}


	return 3;
T
Takashi Iwai 已提交
3875 3876
}

3877 3878

static int hdspm_madi_sync_check(struct hdspm *hdspm)
T
Takashi Iwai 已提交
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
{
	int status = hdspm_read(hdspm, HDSPM_statusRegister);
	if (status & HDSPM_madiLock) {
		if (status & HDSPM_madiSync)
			return 2;
		else
			return 1;
	}
	return 0;
}


3891 3892 3893
static int hdspm_s1_sync_check(struct hdspm *hdspm, int idx)
{
	int status, lock, sync;
T
Takashi Iwai 已提交
3894

3895
	status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);
T
Takashi Iwai 已提交
3896

3897 3898
	lock = (status & (0x1<<idx)) ? 1 : 0;
	sync = (status & (0x100<<idx)) ? 1 : 0;
R
Remy Bruno 已提交
3899

3900
	if (lock && sync)
R
Remy Bruno 已提交
3901
		return 2;
3902 3903
	else if (lock)
		return 1;
R
Remy Bruno 已提交
3904 3905 3906
	return 0;
}

3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920

static int hdspm_sync_in_sync_check(struct hdspm *hdspm)
{
	int status, lock = 0, sync = 0;

	switch (hdspm->io_type) {
	case RayDAT:
	case AIO:
		status = hdspm_read(hdspm, HDSPM_RD_STATUS_3);
		lock = (status & 0x400) ? 1 : 0;
		sync = (status & 0x800) ? 1 : 0;
		break;

	case MADI:
3921 3922 3923 3924 3925
		status = hdspm_read(hdspm, HDSPM_statusRegister);
		lock = (status & HDSPM_syncInLock) ? 1 : 0;
		sync = (status & HDSPM_syncInSync) ? 1 : 0;
		break;

3926 3927
	case AES32:
		status = hdspm_read(hdspm, HDSPM_statusRegister2);
3928 3929
		lock = (status & 0x100000) ? 1 : 0;
		sync = (status & 0x200000) ? 1 : 0;
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
		break;

	case MADIface:
		break;
	}

	if (lock && sync)
		return 2;
	else if (lock)
		return 1;

	return 0;
}

static int hdspm_aes_sync_check(struct hdspm *hdspm, int idx)
{
	int status2, lock, sync;
	status2 = hdspm_read(hdspm, HDSPM_statusRegister2);

	lock = (status2 & (0x0080 >> idx)) ? 1 : 0;
	sync = (status2 & (0x8000 >> idx)) ? 1 : 0;

	if (sync)
		return 2;
	else if (lock)
		return 1;
	return 0;
}

3959 3960 3961 3962 3963 3964 3965 3966
static int hdspm_tco_input_check(struct hdspm *hdspm, u32 mask)
{
	u32 status;
	status = hdspm_read(hdspm, HDSPM_RD_TCO + 4);

	return (status & mask) ? 1 : 0;
}

3967 3968 3969 3970 3971 3972 3973 3974

static int hdspm_tco_sync_check(struct hdspm *hdspm)
{
	int status;

	if (hdspm->tco) {
		switch (hdspm->io_type) {
		case MADI:
3975 3976 3977 3978 3979 3980 3981 3982
			status = hdspm_read(hdspm, HDSPM_statusRegister);
			if (status & HDSPM_tcoLockMadi) {
				if (status & HDSPM_tcoSync)
					return 2;
				else
					return 1;
			}
			return 0;
3983 3984
		case AES32:
			status = hdspm_read(hdspm, HDSPM_statusRegister);
3985
			if (status & HDSPM_tcoLockAes) {
3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
				if (status & HDSPM_tcoSync)
					return 2;
				else
					return 1;
			}
			return 0;
		case RayDAT:
		case AIO:
			status = hdspm_read(hdspm, HDSPM_RD_STATUS_1);

			if (status & 0x8000000)
				return 2; /* Sync */
			if (status & 0x4000000)
				return 1; /* Lock */
			return 0; /* No signal */

		default:
			break;
		}
	}

	return 3; /* N/A */
}


static int snd_hdspm_get_sync_check(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
	int val = -1;

	switch (hdspm->io_type) {
	case RayDAT:
		switch (kcontrol->private_value) {
		case 0: /* WC */
			val = hdspm_wc_sync_check(hdspm); break;
		case 7: /* TCO */
			val = hdspm_tco_sync_check(hdspm); break;
		case 8: /* SYNC IN */
			val = hdspm_sync_in_sync_check(hdspm); break;
		default:
4027 4028
			val = hdspm_s1_sync_check(hdspm,
					kcontrol->private_value-1);
4029
		}
4030
		break;
4031 4032 4033 4034 4035 4036 4037 4038 4039 4040

	case AIO:
		switch (kcontrol->private_value) {
		case 0: /* WC */
			val = hdspm_wc_sync_check(hdspm); break;
		case 4: /* TCO */
			val = hdspm_tco_sync_check(hdspm); break;
		case 5: /* SYNC IN */
			val = hdspm_sync_in_sync_check(hdspm); break;
		default:
4041 4042
			val = hdspm_s1_sync_check(hdspm,
					kcontrol->private_value-1);
4043
		}
4044
		break;
4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056

	case MADI:
		switch (kcontrol->private_value) {
		case 0: /* WC */
			val = hdspm_wc_sync_check(hdspm); break;
		case 1: /* MADI */
			val = hdspm_madi_sync_check(hdspm); break;
		case 2: /* TCO */
			val = hdspm_tco_sync_check(hdspm); break;
		case 3: /* SYNC_IN */
			val = hdspm_sync_in_sync_check(hdspm); break;
		}
4057
		break;
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070

	case MADIface:
		val = hdspm_madi_sync_check(hdspm); /* MADI */
		break;

	case AES32:
		switch (kcontrol->private_value) {
		case 0: /* WC */
			val = hdspm_wc_sync_check(hdspm); break;
		case 9: /* TCO */
			val = hdspm_tco_sync_check(hdspm); break;
		case 10 /* SYNC IN */:
			val = hdspm_sync_in_sync_check(hdspm); break;
4071
		default: /* AES1 to AES8 */
4072
			 val = hdspm_aes_sync_check(hdspm,
4073
					 kcontrol->private_value-1);
4074
		}
4075
		break;
4076 4077 4078

	}

4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094
	if (hdspm->tco) {
		switch (kcontrol->private_value) {
		case 11:
			/* Check TCO for lock state of its current input */
			val = hdspm_tco_input_check(hdspm, HDSPM_TCO1_TCO_lock);
			break;
		case 12:
			/* Check TCO for valid time code on LTC input. */
			val = hdspm_tco_input_check(hdspm,
				HDSPM_TCO1_LTC_Input_valid);
			break;
		default:
			break;
		}
	}

4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
	if (-1 == val)
		val = 3;

	ucontrol->value.enumerated.item[0] = val;
	return 0;
}



/**
 * TCO controls
 **/
static void hdspm_tco_write(struct hdspm *hdspm)
{
	unsigned int tc[4] = { 0, 0, 0, 0};

	switch (hdspm->tco->input) {
	case 0:
		tc[2] |= HDSPM_TCO2_set_input_MSB;
		break;
	case 1:
		tc[2] |= HDSPM_TCO2_set_input_LSB;
		break;
	default:
		break;
	}

	switch (hdspm->tco->framerate) {
	case 1:
		tc[1] |= HDSPM_TCO1_LTC_Format_LSB;
		break;
	case 2:
		tc[1] |= HDSPM_TCO1_LTC_Format_MSB;
		break;
	case 3:
		tc[1] |= HDSPM_TCO1_LTC_Format_MSB +
			HDSPM_TCO1_set_drop_frame_flag;
		break;
	case 4:
		tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
			HDSPM_TCO1_LTC_Format_MSB;
		break;
	case 5:
		tc[1] |= HDSPM_TCO1_LTC_Format_LSB +
			HDSPM_TCO1_LTC_Format_MSB +
			HDSPM_TCO1_set_drop_frame_flag;
		break;
	default:
		break;
	}

	switch (hdspm->tco->wordclock) {
	case 1:
		tc[2] |= HDSPM_TCO2_WCK_IO_ratio_LSB;
		break;
	case 2:
		tc[2] |= HDSPM_TCO2_WCK_IO_ratio_MSB;
		break;
	default:
		break;
	}

	switch (hdspm->tco->samplerate) {
	case 1:
		tc[2] |= HDSPM_TCO2_set_freq;
		break;
	case 2:
		tc[2] |= HDSPM_TCO2_set_freq_from_app;
		break;
	default:
		break;
	}

	switch (hdspm->tco->pull) {
	case 1:
		tc[2] |= HDSPM_TCO2_set_pull_up;
		break;
	case 2:
		tc[2] |= HDSPM_TCO2_set_pull_down;
		break;
	case 3:
		tc[2] |= HDSPM_TCO2_set_pull_up + HDSPM_TCO2_set_01_4;
		break;
	case 4:
		tc[2] |= HDSPM_TCO2_set_pull_down + HDSPM_TCO2_set_01_4;
		break;
	default:
		break;
	}

	if (1 == hdspm->tco->term) {
		tc[2] |= HDSPM_TCO2_set_term_75R;
	}

	hdspm_write(hdspm, HDSPM_WR_TCO, tc[0]);
	hdspm_write(hdspm, HDSPM_WR_TCO+4, tc[1]);
	hdspm_write(hdspm, HDSPM_WR_TCO+8, tc[2]);
	hdspm_write(hdspm, HDSPM_WR_TCO+12, tc[3]);
}


#define HDSPM_TCO_SAMPLE_RATE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_sample_rate, \
	.get = snd_hdspm_get_tco_sample_rate, \
	.put = snd_hdspm_put_tco_sample_rate \
}

static int snd_hdspm_info_tco_sample_rate(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
4210
	/* TODO freq from app could be supported here, see tco->samplerate */
4211
	static const char *const texts[] = { "44.1 kHz", "48 kHz" };
4212
	ENUMERATED_CTL_INFO(uinfo, texts);
4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
	return 0;
}

static int snd_hdspm_get_tco_sample_rate(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm->tco->samplerate;

	return 0;
}

static int snd_hdspm_put_tco_sample_rate(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	if (hdspm->tco->samplerate != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->samplerate = ucontrol->value.enumerated.item[0];

		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}


#define HDSPM_TCO_PULL(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
		SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_pull, \
	.get = snd_hdspm_get_tco_pull, \
	.put = snd_hdspm_put_tco_pull \
}

static int snd_hdspm_info_tco_pull(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_info *uinfo)
{
4257 4258
	static const char *const texts[] = { "0", "+ 0.1 %", "- 0.1 %",
		"+ 4 %", "- 4 %" };
4259
	ENUMERATED_CTL_INFO(uinfo, texts);
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
	return 0;
}

static int snd_hdspm_get_tco_pull(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm->tco->pull;

	return 0;
}

static int snd_hdspm_put_tco_pull(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	if (hdspm->tco->pull != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->pull = ucontrol->value.enumerated.item[0];

		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}

#define HDSPM_TCO_WCK_CONVERSION(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
			SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_wck_conversion, \
	.get = snd_hdspm_get_tco_wck_conversion, \
	.put = snd_hdspm_put_tco_wck_conversion \
}

static int snd_hdspm_info_tco_wck_conversion(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_info *uinfo)
{
4303
	static const char *const texts[] = { "1:1", "44.1 -> 48", "48 -> 44.1" };
4304
	ENUMERATED_CTL_INFO(uinfo, texts);
4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
	return 0;
}

static int snd_hdspm_get_tco_wck_conversion(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm->tco->wordclock;

	return 0;
}

static int snd_hdspm_put_tco_wck_conversion(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	if (hdspm->tco->wordclock != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->wordclock = ucontrol->value.enumerated.item[0];

		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}


#define HDSPM_TCO_FRAME_RATE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
			SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_frame_rate, \
	.get = snd_hdspm_get_tco_frame_rate, \
	.put = snd_hdspm_put_tco_frame_rate \
}

static int snd_hdspm_info_tco_frame_rate(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
4349
	static const char *const texts[] = { "24 fps", "25 fps", "29.97fps",
4350
		"29.97 dfps", "30 fps", "30 dfps" };
4351
	ENUMERATED_CTL_INFO(uinfo, texts);
4352 4353 4354 4355
	return 0;
}

static int snd_hdspm_get_tco_frame_rate(struct snd_kcontrol *kcontrol,
R
Remy Bruno 已提交
4356 4357 4358 4359
					struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

4360
	ucontrol->value.enumerated.item[0] = hdspm->tco->framerate;
R
Remy Bruno 已提交
4361 4362 4363

	return 0;
}
T
Takashi Iwai 已提交
4364

4365 4366 4367 4368
static int snd_hdspm_put_tco_frame_rate(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
4369

4370 4371
	if (hdspm->tco->framerate != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->framerate = ucontrol->value.enumerated.item[0];
T
Takashi Iwai 已提交
4372

4373 4374 4375 4376 4377 4378 4379
		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}
T
Takashi Iwai 已提交
4380

4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395

#define HDSPM_TCO_SYNC_SOURCE(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
			SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_sync_source, \
	.get = snd_hdspm_get_tco_sync_source, \
	.put = snd_hdspm_put_tco_sync_source \
}

static int snd_hdspm_info_tco_sync_source(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
4396
	static const char *const texts[] = { "LTC", "Video", "WCK" };
4397
	ENUMERATED_CTL_INFO(uinfo, texts);
4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
	return 0;
}

static int snd_hdspm_get_tco_sync_source(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm->tco->input;

	return 0;
}

static int snd_hdspm_put_tco_sync_source(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	if (hdspm->tco->input != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->input = ucontrol->value.enumerated.item[0];

		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}


#define HDSPM_TCO_WORD_TERM(xname, xindex) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.index = xindex, \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |\
			SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
	.info = snd_hdspm_info_tco_word_term, \
	.get = snd_hdspm_get_tco_word_term, \
	.put = snd_hdspm_put_tco_word_term \
}

static int snd_hdspm_info_tco_word_term(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;

	return 0;
}


static int snd_hdspm_get_tco_word_term(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = hdspm->tco->term;

	return 0;
}


static int snd_hdspm_put_tco_word_term(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
{
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);

	if (hdspm->tco->term != ucontrol->value.enumerated.item[0]) {
		hdspm->tco->term = ucontrol->value.enumerated.item[0];

		hdspm_tco_write(hdspm);

		return 1;
	}

	return 0;
}




static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
	HDSPM_MIXER("Mixer", 0),
	HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
T
Takashi Iwai 已提交
4484 4485 4486 4487
	HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
	HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
	HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
	HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4488
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
4489 4490
	HDSPM_SYNC_CHECK("WC SyncCheck", 0),
	HDSPM_SYNC_CHECK("MADI SyncCheck", 1),
4491
	HDSPM_SYNC_CHECK("TCO SyncCheck", 2),
4492
	HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3),
4493 4494
	HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
	HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
4495
	HDSPM_TOGGLE_SETTING("Disable 96K frames", HDSPM_SMUX),
4496 4497
	HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
	HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
4498 4499
	HDSPM_INPUT_SELECT("Input Select", 0),
	HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
};


static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = {
	HDSPM_MIXER("Mixer", 0),
	HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
	HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
	HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
	HDSPM_SYNC_CHECK("MADI SyncCheck", 0),
4510 4511 4512
	HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch),
	HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
	HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp),
4513
	HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0)
T
Takashi Iwai 已提交
4514 4515
};

4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
static struct snd_kcontrol_new snd_hdspm_controls_aio[] = {
	HDSPM_MIXER("Mixer", 0),
	HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
	HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
	HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
	HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
	HDSPM_SYNC_CHECK("WC SyncCheck", 0),
	HDSPM_SYNC_CHECK("AES SyncCheck", 1),
	HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
	HDSPM_SYNC_CHECK("ADAT SyncCheck", 3),
	HDSPM_SYNC_CHECK("TCO SyncCheck", 4),
	HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 5),
	HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
	HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
	HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT Frequency", 3),
	HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 4),
4534
	HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 5),
4535
	HDSPM_CONTROL_TRISTATE("S/PDIF Input", HDSPM_c0_Input0),
4536 4537 4538 4539
	HDSPM_TOGGLE_SETTING("S/PDIF Out Optical", HDSPM_c0_Spdif_Opt),
	HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
	HDSPM_TOGGLE_SETTING("ADAT internal (AEB/TEB)", HDSPM_c0_AEB1),
	HDSPM_TOGGLE_SETTING("XLR Breakout Cable", HDSPM_c0_Sym6db),
4540 4541 4542 4543
	HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48),
	HDSPM_CONTROL_TRISTATE("Input Level", HDSPM_c0_AD_GAIN0),
	HDSPM_CONTROL_TRISTATE("Output Level", HDSPM_c0_DA_GAIN0),
	HDSPM_CONTROL_TRISTATE("Phones Level", HDSPM_c0_PH_GAIN0)
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555

		/*
		   HDSPM_INPUT_SELECT("Input Select", 0),
		   HDSPM_SPDIF_OPTICAL("SPDIF Out Optical", 0),
		   HDSPM_PROFESSIONAL("SPDIF Out Professional", 0);
		   HDSPM_SPDIF_IN("SPDIF In", 0);
		   HDSPM_BREAKOUT_CABLE("Breakout Cable", 0);
		   HDSPM_INPUT_LEVEL("Input Level", 0);
		   HDSPM_OUTPUT_LEVEL("Output Level", 0);
		   HDSPM_PHONES("Phones", 0);
		   */
};
R
Remy Bruno 已提交
4556

4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
static struct snd_kcontrol_new snd_hdspm_controls_raydat[] = {
	HDSPM_MIXER("Mixer", 0),
	HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
	HDSPM_SYSTEM_CLOCK_MODE("Clock Mode", 0),
	HDSPM_PREF_SYNC_REF("Pref Sync Ref", 0),
	HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
	HDSPM_SYNC_CHECK("WC SyncCheck", 0),
	HDSPM_SYNC_CHECK("AES SyncCheck", 1),
	HDSPM_SYNC_CHECK("SPDIF SyncCheck", 2),
	HDSPM_SYNC_CHECK("ADAT1 SyncCheck", 3),
	HDSPM_SYNC_CHECK("ADAT2 SyncCheck", 4),
	HDSPM_SYNC_CHECK("ADAT3 SyncCheck", 5),
	HDSPM_SYNC_CHECK("ADAT4 SyncCheck", 6),
	HDSPM_SYNC_CHECK("TCO SyncCheck", 7),
	HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 8),
	HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES Frequency", 1),
	HDSPM_AUTOSYNC_SAMPLE_RATE("SPDIF Frequency", 2),
	HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT1 Frequency", 3),
	HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT2 Frequency", 4),
	HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT3 Frequency", 5),
	HDSPM_AUTOSYNC_SAMPLE_RATE("ADAT4 Frequency", 6),
	HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 7),
4580 4581 4582
	HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 8),
	HDSPM_TOGGLE_SETTING("S/PDIF Out Professional", HDSPM_c0_Pro),
	HDSPM_TOGGLE_SETTING("Single Speed WordClock Out", HDSPM_c0_Wck48)
4583 4584 4585
};

static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {
R
Remy Bruno 已提交
4586
	HDSPM_MIXER("Mixer", 0),
4587
	HDSPM_INTERNAL_CLOCK("Internal Clock", 0),
R
Remy Bruno 已提交
4588 4589 4590 4591
	HDSPM_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
	HDSPM_PREF_SYNC_REF("Preferred Sync Reference", 0),
	HDSPM_AUTOSYNC_REF("AutoSync Reference", 0),
	HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
4592
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 11),
4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614
	HDSPM_SYNC_CHECK("WC Sync Check", 0),
	HDSPM_SYNC_CHECK("AES1 Sync Check", 1),
	HDSPM_SYNC_CHECK("AES2 Sync Check", 2),
	HDSPM_SYNC_CHECK("AES3 Sync Check", 3),
	HDSPM_SYNC_CHECK("AES4 Sync Check", 4),
	HDSPM_SYNC_CHECK("AES5 Sync Check", 5),
	HDSPM_SYNC_CHECK("AES6 Sync Check", 6),
	HDSPM_SYNC_CHECK("AES7 Sync Check", 7),
	HDSPM_SYNC_CHECK("AES8 Sync Check", 8),
	HDSPM_SYNC_CHECK("TCO Sync Check", 9),
	HDSPM_SYNC_CHECK("SYNC IN Sync Check", 10),
	HDSPM_AUTOSYNC_SAMPLE_RATE("WC Frequency", 0),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES1 Frequency", 1),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES2 Frequency", 2),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES3 Frequency", 3),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES4 Frequency", 4),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES5 Frequency", 5),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES6 Frequency", 6),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES7 Frequency", 7),
	HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8),
	HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9),
	HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10),
4615 4616 4617 4618 4619
	HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut),
	HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis),
	HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby),
	HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional),
	HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms),
R
Remy Bruno 已提交
4620 4621 4622 4623
	HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
	HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
};

4624 4625 4626 4627 4628 4629 4630 4631 4632


/* Control elements for the optional TCO module */
static struct snd_kcontrol_new snd_hdspm_controls_tco[] = {
	HDSPM_TCO_SAMPLE_RATE("TCO Sample Rate", 0),
	HDSPM_TCO_PULL("TCO Pull", 0),
	HDSPM_TCO_WCK_CONVERSION("TCO WCK Conversion", 0),
	HDSPM_TCO_FRAME_RATE("TCO Frame Rate", 0),
	HDSPM_TCO_SYNC_SOURCE("TCO Sync Source", 0),
4633 4634 4635 4636 4637
	HDSPM_TCO_WORD_TERM("TCO Word Term", 0),
	HDSPM_TCO_LOCK_CHECK("TCO Input Check", 11),
	HDSPM_TCO_LOCK_CHECK("TCO LTC Valid", 12),
	HDSPM_TCO_LTC_FRAMES("TCO Detected Frame Rate", 0),
	HDSPM_TCO_VIDEO_INPUT_FORMAT("Video Input Format", 0)
4638 4639 4640
};


4641
static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
T
Takashi Iwai 已提交
4642 4643


4644
static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
T
Takashi Iwai 已提交
4645 4646 4647
{
	int i;

4648
	for (i = hdspm->ds_out_channels; i < hdspm->ss_out_channels; ++i) {
T
Takashi Iwai 已提交
4649 4650
		if (hdspm->system_sample_rate > 48000) {
			hdspm->playback_mixer_ctls[i]->vd[0].access =
4651 4652 4653
				SNDRV_CTL_ELEM_ACCESS_INACTIVE |
				SNDRV_CTL_ELEM_ACCESS_READ |
				SNDRV_CTL_ELEM_ACCESS_VOLATILE;
T
Takashi Iwai 已提交
4654 4655
		} else {
			hdspm->playback_mixer_ctls[i]->vd[0].access =
4656 4657
				SNDRV_CTL_ELEM_ACCESS_READWRITE |
				SNDRV_CTL_ELEM_ACCESS_VOLATILE;
T
Takashi Iwai 已提交
4658 4659
		}
		snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
4660 4661
				SNDRV_CTL_EVENT_MASK_INFO,
				&hdspm->playback_mixer_ctls[i]->id);
T
Takashi Iwai 已提交
4662 4663 4664 4665 4666 4667
	}

	return 0;
}


4668 4669
static int snd_hdspm_create_controls(struct snd_card *card,
					struct hdspm *hdspm)
T
Takashi Iwai 已提交
4670 4671 4672
{
	unsigned int idx, limit;
	int err;
4673
	struct snd_kcontrol *kctl;
4674
	struct snd_kcontrol_new *list = NULL;
T
Takashi Iwai 已提交
4675

4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697
	switch (hdspm->io_type) {
	case MADI:
		list = snd_hdspm_controls_madi;
		limit = ARRAY_SIZE(snd_hdspm_controls_madi);
		break;
	case MADIface:
		list = snd_hdspm_controls_madiface;
		limit = ARRAY_SIZE(snd_hdspm_controls_madiface);
		break;
	case AIO:
		list = snd_hdspm_controls_aio;
		limit = ARRAY_SIZE(snd_hdspm_controls_aio);
		break;
	case RayDAT:
		list = snd_hdspm_controls_raydat;
		limit = ARRAY_SIZE(snd_hdspm_controls_raydat);
		break;
	case AES32:
		list = snd_hdspm_controls_aes32;
		limit = ARRAY_SIZE(snd_hdspm_controls_aes32);
		break;
	}
R
Remy Bruno 已提交
4698

4699 4700
	if (NULL != list) {
		for (idx = 0; idx < limit; idx++) {
R
Remy Bruno 已提交
4701
			err = snd_ctl_add(card,
4702
					snd_ctl_new1(&list[idx], hdspm));
R
Remy Bruno 已提交
4703 4704
			if (err < 0)
				return err;
T
Takashi Iwai 已提交
4705 4706 4707 4708
		}
	}


4709
	/* create simple 1:1 playback mixer controls */
T
Takashi Iwai 已提交
4710
	snd_hdspm_playback_mixer.name = "Chn";
4711 4712 4713 4714 4715 4716 4717
	if (hdspm->system_sample_rate >= 128000) {
		limit = hdspm->qs_out_channels;
	} else if (hdspm->system_sample_rate >= 64000) {
		limit = hdspm->ds_out_channels;
	} else {
		limit = hdspm->ss_out_channels;
	}
T
Takashi Iwai 已提交
4718 4719
	for (idx = 0; idx < limit; ++idx) {
		snd_hdspm_playback_mixer.index = idx + 1;
T
Takashi Iwai 已提交
4720 4721 4722
		kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
		err = snd_ctl_add(card, kctl);
		if (err < 0)
T
Takashi Iwai 已提交
4723 4724 4725 4726
			return err;
		hdspm->playback_mixer_ctls[idx] = kctl;
	}

4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739

	if (hdspm->tco) {
		/* add tco control elements */
		list = snd_hdspm_controls_tco;
		limit = ARRAY_SIZE(snd_hdspm_controls_tco);
		for (idx = 0; idx < limit; idx++) {
			err = snd_ctl_add(card,
					snd_ctl_new1(&list[idx], hdspm));
			if (err < 0)
				return err;
		}
	}

T
Takashi Iwai 已提交
4740 4741 4742 4743
	return 0;
}

/*------------------------------------------------------------
4744
   /proc interface
T
Takashi Iwai 已提交
4745 4746 4747
 ------------------------------------------------------------*/

static void
4748 4749
snd_hdspm_proc_read_tco(struct snd_info_entry *entry,
					struct snd_info_buffer *buffer)
T
Takashi Iwai 已提交
4750
{
T
Takashi Iwai 已提交
4751
	struct hdspm *hdspm = entry->private_data;
4752
	unsigned int status, control;
4753 4754 4755 4756 4757
	int a, ltc, frames, seconds, minutes, hours;
	unsigned int period;
	u64 freq_const = 0;
	u32 rate;

4758 4759
	snd_iprintf(buffer, "--- TCO ---\n");

T
Takashi Iwai 已提交
4760
	status = hdspm_read(hdspm, HDSPM_statusRegister);
4761
	control = hdspm->control_register;
T
Takashi Iwai 已提交
4762 4763


4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856
	if (status & HDSPM_tco_detect) {
		snd_iprintf(buffer, "TCO module detected.\n");
		a = hdspm_read(hdspm, HDSPM_RD_TCO+4);
		if (a & HDSPM_TCO1_LTC_Input_valid) {
			snd_iprintf(buffer, "  LTC valid, ");
			switch (a & (HDSPM_TCO1_LTC_Format_LSB |
						HDSPM_TCO1_LTC_Format_MSB)) {
			case 0:
				snd_iprintf(buffer, "24 fps, ");
				break;
			case HDSPM_TCO1_LTC_Format_LSB:
				snd_iprintf(buffer, "25 fps, ");
				break;
			case HDSPM_TCO1_LTC_Format_MSB:
				snd_iprintf(buffer, "29.97 fps, ");
				break;
			default:
				snd_iprintf(buffer, "30 fps, ");
				break;
			}
			if (a & HDSPM_TCO1_set_drop_frame_flag) {
				snd_iprintf(buffer, "drop frame\n");
			} else {
				snd_iprintf(buffer, "full frame\n");
			}
		} else {
			snd_iprintf(buffer, "  no LTC\n");
		}
		if (a & HDSPM_TCO1_Video_Input_Format_NTSC) {
			snd_iprintf(buffer, "  Video: NTSC\n");
		} else if (a & HDSPM_TCO1_Video_Input_Format_PAL) {
			snd_iprintf(buffer, "  Video: PAL\n");
		} else {
			snd_iprintf(buffer, "  No video\n");
		}
		if (a & HDSPM_TCO1_TCO_lock) {
			snd_iprintf(buffer, "  Sync: lock\n");
		} else {
			snd_iprintf(buffer, "  Sync: no lock\n");
		}

		switch (hdspm->io_type) {
		case MADI:
		case AES32:
			freq_const = 110069313433624ULL;
			break;
		case RayDAT:
		case AIO:
			freq_const = 104857600000000ULL;
			break;
		case MADIface:
			break; /* no TCO possible */
		}

		period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);
		snd_iprintf(buffer, "    period: %u\n", period);


		/* rate = freq_const/period; */
		rate = div_u64(freq_const, period);

		if (control & HDSPM_QuadSpeed) {
			rate *= 4;
		} else if (control & HDSPM_DoubleSpeed) {
			rate *= 2;
		}

		snd_iprintf(buffer, "  Frequency: %u Hz\n",
				(unsigned int) rate);

		ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
		frames = ltc & 0xF;
		ltc >>= 4;
		frames += (ltc & 0x3) * 10;
		ltc >>= 4;
		seconds = ltc & 0xF;
		ltc >>= 4;
		seconds += (ltc & 0x7) * 10;
		ltc >>= 4;
		minutes = ltc & 0xF;
		ltc >>= 4;
		minutes += (ltc & 0x7) * 10;
		ltc >>= 4;
		hours = ltc & 0xF;
		ltc >>= 4;
		hours += (ltc & 0x3) * 10;
		snd_iprintf(buffer,
			"  LTC In: %02d:%02d:%02d:%02d\n",
			hours, minutes, seconds, frames);

	} else {
		snd_iprintf(buffer, "No TCO module detected.\n");
	}
4857 4858 4859 4860 4861 4862 4863
}

static void
snd_hdspm_proc_read_madi(struct snd_info_entry *entry,
			 struct snd_info_buffer *buffer)
{
	struct hdspm *hdspm = entry->private_data;
4864
	unsigned int status, status2;
4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922

	char *pref_sync_ref;
	char *autosync_ref;
	char *system_clock_mode;
	int x, x2;

	status = hdspm_read(hdspm, HDSPM_statusRegister);
	status2 = hdspm_read(hdspm, HDSPM_statusRegister2);

	snd_iprintf(buffer, "%s (Card #%d) Rev.%x Status2first3bits: %x\n",
			hdspm->card_name, hdspm->card->number + 1,
			hdspm->firmware_rev,
			(status2 & HDSPM_version0) |
			(status2 & HDSPM_version1) | (status2 &
				HDSPM_version2));

	snd_iprintf(buffer, "HW Serial: 0x%06x%06x\n",
			(hdspm_read(hdspm, HDSPM_midiStatusIn1)>>8) & 0xFFFFFF,
			hdspm->serial);

	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
			hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);

	snd_iprintf(buffer, "--- System ---\n");

	snd_iprintf(buffer,
		"IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
		status & HDSPM_audioIRQPending,
		(status & HDSPM_midi0IRQPending) ? 1 : 0,
		(status & HDSPM_midi1IRQPending) ? 1 : 0,
		hdspm->irq_count);
	snd_iprintf(buffer,
		"HW pointer: id = %d, rawptr = %d (%d->%d) "
		"estimated= %ld (bytes)\n",
		((status & HDSPM_BufferID) ? 1 : 0),
		(status & HDSPM_BufferPositionMask),
		(status & HDSPM_BufferPositionMask) %
		(2 * (int)hdspm->period_bytes),
		((status & HDSPM_BufferPositionMask) - 64) %
		(2 * (int)hdspm->period_bytes),
		(long) hdspm_hw_pointer(hdspm) * 4);

	snd_iprintf(buffer,
		"MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
		hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
		hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
		hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
		hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
	snd_iprintf(buffer,
		"MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
		hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
		hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
	snd_iprintf(buffer,
		"Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
		"status2=0x%x\n",
		hdspm->control_register, hdspm->control2_register,
		status, status2);

T
Takashi Iwai 已提交
4923 4924 4925

	snd_iprintf(buffer, "--- Settings ---\n");

4926
	x = hdspm_get_latency(hdspm);
T
Takashi Iwai 已提交
4927 4928

	snd_iprintf(buffer,
4929 4930
		"Size (Latency): %d samples (2 periods of %lu bytes)\n",
		x, (unsigned long) hdspm->period_bytes);
T
Takashi Iwai 已提交
4931

4932 4933
	snd_iprintf(buffer, "Line out: %s\n",
		(hdspm->control_register & HDSPM_LineOut) ? "on " : "off");
T
Takashi Iwai 已提交
4934 4935

	snd_iprintf(buffer,
4936 4937 4938 4939 4940 4941
		"ClearTrackMarker = %s, Transmit in %s Channel Mode, "
		"Auto Input %s\n",
		(hdspm->control_register & HDSPM_clr_tms) ? "on" : "off",
		(hdspm->control_register & HDSPM_TX_64ch) ? "64" : "56",
		(hdspm->control_register & HDSPM_AutoInp) ? "on" : "off");

T
Takashi Iwai 已提交
4942

R
Remy Bruno 已提交
4943
	if (!(hdspm->control_register & HDSPM_ClockModeMaster))
4944
		system_clock_mode = "AutoSync";
R
Remy Bruno 已提交
4945
	else
T
Takashi Iwai 已提交
4946
		system_clock_mode = "Master";
4947
	snd_iprintf(buffer, "AutoSync Reference: %s\n", system_clock_mode);
T
Takashi Iwai 已提交
4948 4949 4950 4951 4952 4953 4954 4955

	switch (hdspm_pref_sync_ref(hdspm)) {
	case HDSPM_SYNC_FROM_WORD:
		pref_sync_ref = "Word Clock";
		break;
	case HDSPM_SYNC_FROM_MADI:
		pref_sync_ref = "MADI Sync";
		break;
4956 4957 4958 4959 4960 4961
	case HDSPM_SYNC_FROM_TCO:
		pref_sync_ref = "TCO";
		break;
	case HDSPM_SYNC_FROM_SYNC_IN:
		pref_sync_ref = "Sync In";
		break;
T
Takashi Iwai 已提交
4962 4963 4964 4965 4966
	default:
		pref_sync_ref = "XXXX Clock";
		break;
	}
	snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
4967
			pref_sync_ref);
T
Takashi Iwai 已提交
4968 4969

	snd_iprintf(buffer, "System Clock Frequency: %d\n",
4970
			hdspm->system_sample_rate);
T
Takashi Iwai 已提交
4971 4972 4973 4974 4975 4976 4977 4978


	snd_iprintf(buffer, "--- Status:\n");

	x = status & HDSPM_madiSync;
	x2 = status2 & HDSPM_wcSync;

	snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
4979 4980 4981 4982
			(status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
			"NoLock",
			(status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
			"NoLock");
T
Takashi Iwai 已提交
4983 4984

	switch (hdspm_autosync_ref(hdspm)) {
4985 4986 4987 4988 4989 4990
	case HDSPM_AUTOSYNC_FROM_SYNC_IN:
		autosync_ref = "Sync In";
		break;
	case HDSPM_AUTOSYNC_FROM_TCO:
		autosync_ref = "TCO";
		break;
T
Takashi Iwai 已提交
4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004
	case HDSPM_AUTOSYNC_FROM_WORD:
		autosync_ref = "Word Clock";
		break;
	case HDSPM_AUTOSYNC_FROM_MADI:
		autosync_ref = "MADI Sync";
		break;
	case HDSPM_AUTOSYNC_FROM_NONE:
		autosync_ref = "Input not valid";
		break;
	default:
		autosync_ref = "---";
		break;
	}
	snd_iprintf(buffer,
5005 5006 5007 5008
		"AutoSync: Reference= %s, Freq=%d (MADI = %d, Word = %d)\n",
		autosync_ref, hdspm_external_sample_rate(hdspm),
		(status & HDSPM_madiFreqMask) >> 22,
		(status2 & HDSPM_wcFreqMask) >> 5);
T
Takashi Iwai 已提交
5009 5010

	snd_iprintf(buffer, "Input: %s, Mode=%s\n",
5011 5012 5013
		(status & HDSPM_AB_int) ? "Coax" : "Optical",
		(status & HDSPM_RX_64ch) ? "64 channels" :
		"56 channels");
T
Takashi Iwai 已提交
5014

5015 5016 5017
	/* call readout function for TCO specific status */
	snd_hdspm_proc_read_tco(entry, buffer);

T
Takashi Iwai 已提交
5018 5019 5020
	snd_iprintf(buffer, "\n");
}

R
Remy Bruno 已提交
5021 5022 5023 5024
static void
snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
			  struct snd_info_buffer *buffer)
{
T
Takashi Iwai 已提交
5025
	struct hdspm *hdspm = entry->private_data;
R
Remy Bruno 已提交
5026 5027 5028
	unsigned int status;
	unsigned int status2;
	unsigned int timecode;
5029
	unsigned int wcLock, wcSync;
R
Remy Bruno 已提交
5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053
	int pref_syncref;
	char *autosync_ref;
	int x;

	status = hdspm_read(hdspm, HDSPM_statusRegister);
	status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
	timecode = hdspm_read(hdspm, HDSPM_timecodeRegister);

	snd_iprintf(buffer, "%s (Card #%d) Rev.%x\n",
		    hdspm->card_name, hdspm->card->number + 1,
		    hdspm->firmware_rev);

	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
		    hdspm->irq, hdspm->port, (unsigned long)hdspm->iobase);

	snd_iprintf(buffer, "--- System ---\n");

	snd_iprintf(buffer,
		    "IRQ Pending: Audio=%d, MIDI0=%d, MIDI1=%d, IRQcount=%d\n",
		    status & HDSPM_audioIRQPending,
		    (status & HDSPM_midi0IRQPending) ? 1 : 0,
		    (status & HDSPM_midi1IRQPending) ? 1 : 0,
		    hdspm->irq_count);
	snd_iprintf(buffer,
T
Takashi Iwai 已提交
5054 5055
		    "HW pointer: id = %d, rawptr = %d (%d->%d) "
		    "estimated= %ld (bytes)\n",
R
Remy Bruno 已提交
5056 5057
		    ((status & HDSPM_BufferID) ? 1 : 0),
		    (status & HDSPM_BufferPositionMask),
T
Takashi Iwai 已提交
5058 5059 5060 5061
		    (status & HDSPM_BufferPositionMask) %
		    (2 * (int)hdspm->period_bytes),
		    ((status & HDSPM_BufferPositionMask) - 64) %
		    (2 * (int)hdspm->period_bytes),
R
Remy Bruno 已提交
5062 5063 5064 5065 5066 5067 5068 5069 5070
		    (long) hdspm_hw_pointer(hdspm) * 4);

	snd_iprintf(buffer,
		    "MIDI FIFO: Out1=0x%x, Out2=0x%x, In1=0x%x, In2=0x%x \n",
		    hdspm_read(hdspm, HDSPM_midiStatusOut0) & 0xFF,
		    hdspm_read(hdspm, HDSPM_midiStatusOut1) & 0xFF,
		    hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xFF,
		    hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xFF);
	snd_iprintf(buffer,
5071 5072 5073 5074 5075 5076 5077 5078
		    "MIDIoverMADI FIFO: In=0x%x, Out=0x%x \n",
		    hdspm_read(hdspm, HDSPM_midiStatusIn2) & 0xFF,
		    hdspm_read(hdspm, HDSPM_midiStatusOut2) & 0xFF);
	snd_iprintf(buffer,
		    "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
		    "status2=0x%x\n",
		    hdspm->control_register, hdspm->control2_register,
		    status, status2);
R
Remy Bruno 已提交
5079 5080 5081

	snd_iprintf(buffer, "--- Settings ---\n");

5082
	x = hdspm_get_latency(hdspm);
R
Remy Bruno 已提交
5083 5084 5085 5086 5087

	snd_iprintf(buffer,
		    "Size (Latency): %d samples (2 periods of %lu bytes)\n",
		    x, (unsigned long) hdspm->period_bytes);

5088
	snd_iprintf(buffer, "Line out: %s\n",
R
Remy Bruno 已提交
5089
		    (hdspm->
5090
		     control_register & HDSPM_LineOut) ? "on " : "off");
R
Remy Bruno 已提交
5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122

	snd_iprintf(buffer,
		    "ClearTrackMarker %s, Emphasis %s, Dolby %s\n",
		    (hdspm->
		     control_register & HDSPM_clr_tms) ? "on" : "off",
		    (hdspm->
		     control_register & HDSPM_Emphasis) ? "on" : "off",
		    (hdspm->
		     control_register & HDSPM_Dolby) ? "on" : "off");


	pref_syncref = hdspm_pref_sync_ref(hdspm);
	if (pref_syncref == 0)
		snd_iprintf(buffer, "Preferred Sync Reference: Word Clock\n");
	else
		snd_iprintf(buffer, "Preferred Sync Reference: AES%d\n",
				pref_syncref);

	snd_iprintf(buffer, "System Clock Frequency: %d\n",
		    hdspm->system_sample_rate);

	snd_iprintf(buffer, "Double speed: %s\n",
			hdspm->control_register & HDSPM_DS_DoubleWire?
			"Double wire" : "Single wire");
	snd_iprintf(buffer, "Quad speed: %s\n",
			hdspm->control_register & HDSPM_QS_DoubleWire?
			"Double wire" :
			hdspm->control_register & HDSPM_QS_QuadWire?
			"Quad wire" : "Single wire");

	snd_iprintf(buffer, "--- Status:\n");

5123 5124 5125
	wcLock = status & HDSPM_AES32_wcLock;
	wcSync = wcLock && (status & HDSPM_AES32_wcSync);

R
Remy Bruno 已提交
5126
	snd_iprintf(buffer, "Word: %s  Frequency: %d\n",
5127
		    (wcLock) ? (wcSync ? "Sync   " : "Lock   ") : "No Lock",
T
Takashi Iwai 已提交
5128
		    HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
R
Remy Bruno 已提交
5129 5130 5131

	for (x = 0; x < 8; x++) {
		snd_iprintf(buffer, "AES%d: %s  Frequency: %d\n",
T
Takashi Iwai 已提交
5132 5133
			    x+1,
			    (status2 & (HDSPM_LockAES >> x)) ?
5134
			    "Sync   " : "No Lock",
T
Takashi Iwai 已提交
5135
			    HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
R
Remy Bruno 已提交
5136 5137 5138
	}

	switch (hdspm_autosync_ref(hdspm)) {
5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158
	case HDSPM_AES32_AUTOSYNC_FROM_NONE:
		autosync_ref = "None"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_WORD:
		autosync_ref = "Word Clock"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES1:
		autosync_ref = "AES1"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES2:
		autosync_ref = "AES2"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES3:
		autosync_ref = "AES3"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES4:
		autosync_ref = "AES4"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES5:
		autosync_ref = "AES5"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES6:
		autosync_ref = "AES6"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES7:
		autosync_ref = "AES7"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_AES8:
		autosync_ref = "AES8"; break;
5159 5160 5161 5162
	case HDSPM_AES32_AUTOSYNC_FROM_TCO:
		autosync_ref = "TCO"; break;
	case HDSPM_AES32_AUTOSYNC_FROM_SYNC_IN:
		autosync_ref = "Sync In"; break;
5163 5164
	default:
		autosync_ref = "---"; break;
R
Remy Bruno 已提交
5165 5166 5167
	}
	snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);

5168 5169 5170
	/* call readout function for TCO specific status */
	snd_hdspm_proc_read_tco(entry, buffer);

R
Remy Bruno 已提交
5171 5172 5173
	snd_iprintf(buffer, "\n");
}

5174 5175 5176 5177 5178
static void
snd_hdspm_proc_read_raydat(struct snd_info_entry *entry,
			 struct snd_info_buffer *buffer)
{
	struct hdspm *hdspm = entry->private_data;
5179
	unsigned int status1, status2, status3, i;
5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230
	unsigned int lock, sync;

	status1 = hdspm_read(hdspm, HDSPM_RD_STATUS_1); /* s1 */
	status2 = hdspm_read(hdspm, HDSPM_RD_STATUS_2); /* freq */
	status3 = hdspm_read(hdspm, HDSPM_RD_STATUS_3); /* s2 */

	snd_iprintf(buffer, "STATUS1: 0x%08x\n", status1);
	snd_iprintf(buffer, "STATUS2: 0x%08x\n", status2);
	snd_iprintf(buffer, "STATUS3: 0x%08x\n", status3);


	snd_iprintf(buffer, "\n*** CLOCK MODE\n\n");

	snd_iprintf(buffer, "Clock mode      : %s\n",
		(hdspm_system_clock_mode(hdspm) == 0) ? "master" : "slave");
	snd_iprintf(buffer, "System frequency: %d Hz\n",
		hdspm_get_system_sample_rate(hdspm));

	snd_iprintf(buffer, "\n*** INPUT STATUS\n\n");

	lock = 0x1;
	sync = 0x100;

	for (i = 0; i < 8; i++) {
		snd_iprintf(buffer, "s1_input %d: Lock %d, Sync %d, Freq %s\n",
				i,
				(status1 & lock) ? 1 : 0,
				(status1 & sync) ? 1 : 0,
				texts_freq[(status2 >> (i * 4)) & 0xF]);

		lock = lock<<1;
		sync = sync<<1;
	}

	snd_iprintf(buffer, "WC input: Lock %d, Sync %d, Freq %s\n",
			(status1 & 0x1000000) ? 1 : 0,
			(status1 & 0x2000000) ? 1 : 0,
			texts_freq[(status1 >> 16) & 0xF]);

	snd_iprintf(buffer, "TCO input: Lock %d, Sync %d, Freq %s\n",
			(status1 & 0x4000000) ? 1 : 0,
			(status1 & 0x8000000) ? 1 : 0,
			texts_freq[(status1 >> 20) & 0xF]);

	snd_iprintf(buffer, "SYNC IN: Lock %d, Sync %d, Freq %s\n",
			(status3 & 0x400) ? 1 : 0,
			(status3 & 0x800) ? 1 : 0,
			texts_freq[(status2 >> 12) & 0xF]);

}

R
Remy Bruno 已提交
5231 5232
#ifdef CONFIG_SND_DEBUG
static void
5233
snd_hdspm_proc_read_debug(struct snd_info_entry *entry,
R
Remy Bruno 已提交
5234 5235
			  struct snd_info_buffer *buffer)
{
T
Takashi Iwai 已提交
5236
	struct hdspm *hdspm = entry->private_data;
R
Remy Bruno 已提交
5237 5238 5239

	int j,i;

T
Takashi Iwai 已提交
5240
	for (i = 0; i < 256 /* 1024*64 */; i += j) {
R
Remy Bruno 已提交
5241 5242 5243 5244 5245 5246 5247 5248 5249
		snd_iprintf(buffer, "0x%08X: ", i);
		for (j = 0; j < 16; j += 4)
			snd_iprintf(buffer, "%08X ", hdspm_read(hdspm, i + j));
		snd_iprintf(buffer, "\n");
	}
}
#endif


5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
static void snd_hdspm_proc_ports_in(struct snd_info_entry *entry,
			  struct snd_info_buffer *buffer)
{
	struct hdspm *hdspm = entry->private_data;
	int i;

	snd_iprintf(buffer, "# generated by hdspm\n");

	for (i = 0; i < hdspm->max_channels_in; i++) {
		snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_in[i]);
	}
}

static void snd_hdspm_proc_ports_out(struct snd_info_entry *entry,
			  struct snd_info_buffer *buffer)
{
	struct hdspm *hdspm = entry->private_data;
	int i;

	snd_iprintf(buffer, "# generated by hdspm\n");

	for (i = 0; i < hdspm->max_channels_out; i++) {
		snd_iprintf(buffer, "%d=%s\n", i+1, hdspm->port_names_out[i]);
	}
}

R
Remy Bruno 已提交
5276

5277
static void snd_hdspm_proc_init(struct hdspm *hdspm)
T
Takashi Iwai 已提交
5278
{
5279
	struct snd_info_entry *entry;
T
Takashi Iwai 已提交
5280

5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311
	if (!snd_card_proc_new(hdspm->card, "hdspm", &entry)) {
		switch (hdspm->io_type) {
		case AES32:
			snd_info_set_text_ops(entry, hdspm,
					snd_hdspm_proc_read_aes32);
			break;
		case MADI:
			snd_info_set_text_ops(entry, hdspm,
					snd_hdspm_proc_read_madi);
			break;
		case MADIface:
			/* snd_info_set_text_ops(entry, hdspm,
			 snd_hdspm_proc_read_madiface); */
			break;
		case RayDAT:
			snd_info_set_text_ops(entry, hdspm,
					snd_hdspm_proc_read_raydat);
			break;
		case AIO:
			break;
		}
	}

	if (!snd_card_proc_new(hdspm->card, "ports.in", &entry)) {
		snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_in);
	}

	if (!snd_card_proc_new(hdspm->card, "ports.out", &entry)) {
		snd_info_set_text_ops(entry, hdspm, snd_hdspm_proc_ports_out);
	}

R
Remy Bruno 已提交
5312 5313 5314 5315 5316 5317
#ifdef CONFIG_SND_DEBUG
	/* debug file to read all hdspm registers */
	if (!snd_card_proc_new(hdspm->card, "debug", &entry))
		snd_info_set_text_ops(entry, hdspm,
				snd_hdspm_proc_read_debug);
#endif
T
Takashi Iwai 已提交
5318 5319 5320
}

/*------------------------------------------------------------
5321
   hdspm intitialize
T
Takashi Iwai 已提交
5322 5323
 ------------------------------------------------------------*/

5324
static int snd_hdspm_set_defaults(struct hdspm * hdspm)
T
Takashi Iwai 已提交
5325 5326
{
	/* ASSUMPTION: hdspm->lock is either held, or there is no need to
J
Joe Perches 已提交
5327
	   hold it (e.g. during module initialization).
5328
	   */
T
Takashi Iwai 已提交
5329 5330 5331

	/* set defaults:       */

5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350
	hdspm->settings_register = 0;

	switch (hdspm->io_type) {
	case MADI:
	case MADIface:
		hdspm->control_register =
			0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
		break;

	case RayDAT:
	case AIO:
		hdspm->settings_register = 0x1 + 0x1000;
		/* Magic values are: LAT_0, LAT_2, Master, freq1, tx64ch, inp_0,
		 * line_out */
		hdspm->control_register =
			0x2 + 0x8 + 0x10 + 0x80 + 0x400 + 0x4000 + 0x1000000;
		break;

	case AES32:
T
Takashi Iwai 已提交
5351
		hdspm->control_register =
5352
			HDSPM_ClockModeMaster |	/* Master Clock Mode on */
5353
			hdspm_encode_latency(7) | /* latency max=8192samples */
R
Remy Bruno 已提交
5354 5355 5356
			HDSPM_SyncRef0 |	/* AES1 is syncclock */
			HDSPM_LineOut |	/* Analog output in */
			HDSPM_Professional;  /* Professional mode */
5357 5358
		break;
	}
T
Takashi Iwai 已提交
5359 5360 5361

	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

5362
	if (AES32 == hdspm->io_type) {
5363
		/* No control2 register for AES32 */
T
Takashi Iwai 已提交
5364
#ifdef SNDRV_BIG_ENDIAN
5365
		hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
T
Takashi Iwai 已提交
5366
#else
5367
		hdspm->control2_register = 0;
T
Takashi Iwai 已提交
5368 5369
#endif

5370 5371
		hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
	}
T
Takashi Iwai 已提交
5372 5373 5374 5375 5376 5377
	hdspm_compute_period_size(hdspm);

	/* silence everything */

	all_in_all_mixer(hdspm, 0 * UNITY_GAIN);

5378
	if (hdspm_is_raydat_or_aio(hdspm))
5379
		hdspm_write(hdspm, HDSPM_WR_SETTINGS, hdspm->settings_register);
T
Takashi Iwai 已提交
5380 5381

	/* set a default rate so that the channel map is set up. */
5382
	hdspm_set_rate(hdspm, 48000, 1);
T
Takashi Iwai 已提交
5383 5384 5385 5386 5387 5388

	return 0;
}


/*------------------------------------------------------------
5389
   interrupt
T
Takashi Iwai 已提交
5390 5391
 ------------------------------------------------------------*/

5392
static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
T
Takashi Iwai 已提交
5393
{
5394
	struct hdspm *hdspm = (struct hdspm *) dev_id;
T
Takashi Iwai 已提交
5395
	unsigned int status;
5396 5397
	int i, audio, midi, schedule = 0;
	/* cycles_t now; */
T
Takashi Iwai 已提交
5398 5399 5400 5401

	status = hdspm_read(hdspm, HDSPM_statusRegister);

	audio = status & HDSPM_audioIRQPending;
5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416
	midi = status & (HDSPM_midi0IRQPending | HDSPM_midi1IRQPending |
			HDSPM_midi2IRQPending | HDSPM_midi3IRQPending);

	/* now = get_cycles(); */
	/**
	 *   LAT_2..LAT_0 period  counter (win)  counter (mac)
	 *          6       4096   ~256053425     ~514672358
	 *          5       2048   ~128024983     ~257373821
	 *          4       1024    ~64023706     ~128718089
	 *          3        512    ~32005945      ~64385999
	 *          2        256    ~16003039      ~32260176
	 *          1        128     ~7998738      ~16194507
	 *          0         64     ~3998231       ~8191558
	 **/
	/*
5417
	  dev_info(hdspm->card->dev, "snd_hdspm_interrupt %llu @ %llx\n",
5418 5419 5420
	   now-hdspm->last_interrupt, status & 0xFFC0);
	   hdspm->last_interrupt = now;
	*/
T
Takashi Iwai 已提交
5421

5422
	if (!audio && !midi)
T
Takashi Iwai 已提交
5423 5424 5425 5426 5427 5428 5429 5430
		return IRQ_NONE;

	hdspm_write(hdspm, HDSPM_interruptConfirmation, 0);
	hdspm->irq_count++;


	if (audio) {
		if (hdspm->capture_substream)
T
Takashi Iwai 已提交
5431
			snd_pcm_period_elapsed(hdspm->capture_substream);
T
Takashi Iwai 已提交
5432 5433

		if (hdspm->playback_substream)
T
Takashi Iwai 已提交
5434
			snd_pcm_period_elapsed(hdspm->playback_substream);
T
Takashi Iwai 已提交
5435 5436
	}

5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457
	if (midi) {
		i = 0;
		while (i < hdspm->midiPorts) {
			if ((hdspm_read(hdspm,
				hdspm->midi[i].statusIn) & 0xff) &&
					(status & hdspm->midi[i].irq)) {
				/* we disable interrupts for this input until
				 * processing is done
				 */
				hdspm->control_register &= ~hdspm->midi[i].ie;
				hdspm_write(hdspm, HDSPM_controlRegister,
						hdspm->control_register);
				hdspm->midi[i].pending = 1;
				schedule = 1;
			}

			i++;
		}

		if (schedule)
			tasklet_hi_schedule(&hdspm->midi_tasklet);
T
Takashi Iwai 已提交
5458
	}
5459

T
Takashi Iwai 已提交
5460 5461 5462 5463
	return IRQ_HANDLED;
}

/*------------------------------------------------------------
5464
   pcm interface
T
Takashi Iwai 已提交
5465 5466 5467
  ------------------------------------------------------------*/


5468 5469
static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream
					      *substream)
T
Takashi Iwai 已提交
5470
{
5471
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
5472 5473 5474 5475
	return hdspm_hw_pointer(hdspm);
}


5476
static int snd_hdspm_reset(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
5477
{
5478 5479 5480
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
T
Takashi Iwai 已提交
5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = hdspm->capture_substream;
	else
		other = hdspm->playback_substream;

	if (hdspm->running)
		runtime->status->hw_ptr = hdspm_hw_pointer(hdspm);
	else
		runtime->status->hw_ptr = 0;
	if (other) {
5492 5493
		struct snd_pcm_substream *s;
		struct snd_pcm_runtime *oruntime = other->runtime;
5494
		snd_pcm_group_for_each_entry(s, substream) {
T
Takashi Iwai 已提交
5495 5496
			if (s == other) {
				oruntime->status->hw_ptr =
5497
					runtime->status->hw_ptr;
T
Takashi Iwai 已提交
5498 5499 5500 5501 5502 5503 5504
				break;
			}
		}
	}
	return 0;
}

5505 5506
static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
			       struct snd_pcm_hw_params *params)
T
Takashi Iwai 已提交
5507
{
5508
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523
	int err;
	int i;
	pid_t this_pid;
	pid_t other_pid;

	spin_lock_irq(&hdspm->lock);

	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		this_pid = hdspm->playback_pid;
		other_pid = hdspm->capture_pid;
	} else {
		this_pid = hdspm->capture_pid;
		other_pid = hdspm->playback_pid;
	}

T
Takashi Iwai 已提交
5524
	if (other_pid > 0 && this_pid != other_pid) {
T
Takashi Iwai 已提交
5525 5526 5527 5528

		/* The other stream is open, and not by the same
		   task as this one. Make sure that the parameters
		   that matter are the same.
5529
		   */
T
Takashi Iwai 已提交
5530 5531 5532 5533

		if (params_rate(params) != hdspm->system_sample_rate) {
			spin_unlock_irq(&hdspm->lock);
			_snd_pcm_hw_param_setempty(params,
5534
					SNDRV_PCM_HW_PARAM_RATE);
T
Takashi Iwai 已提交
5535 5536 5537 5538 5539 5540
			return -EBUSY;
		}

		if (params_period_size(params) != hdspm->period_bytes / 4) {
			spin_unlock_irq(&hdspm->lock);
			_snd_pcm_hw_param_setempty(params,
5541
					SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
T
Takashi Iwai 已提交
5542 5543 5544 5545 5546 5547 5548 5549 5550 5551
			return -EBUSY;
		}

	}
	/* We're fine. */
	spin_unlock_irq(&hdspm->lock);

	/* how to make sure that the rate matches an externally-set one ?   */

	spin_lock_irq(&hdspm->lock);
T
Takashi Iwai 已提交
5552 5553
	err = hdspm_set_rate(hdspm, params_rate(params), 0);
	if (err < 0) {
5554
		dev_info(hdspm->card->dev, "err on hdspm_set_rate: %d\n", err);
T
Takashi Iwai 已提交
5555 5556
		spin_unlock_irq(&hdspm->lock);
		_snd_pcm_hw_param_setempty(params,
5557
				SNDRV_PCM_HW_PARAM_RATE);
T
Takashi Iwai 已提交
5558 5559 5560 5561
		return err;
	}
	spin_unlock_irq(&hdspm->lock);

T
Takashi Iwai 已提交
5562
	err = hdspm_set_interrupt_interval(hdspm,
5563
			params_period_size(params));
T
Takashi Iwai 已提交
5564
	if (err < 0) {
5565 5566
		dev_info(hdspm->card->dev,
			 "err on hdspm_set_interrupt_interval: %d\n", err);
T
Takashi Iwai 已提交
5567
		_snd_pcm_hw_param_setempty(params,
5568
				SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
T
Takashi Iwai 已提交
5569 5570 5571
		return err;
	}

T
Takashi Iwai 已提交
5572 5573 5574
	/* Memory allocation, takashi's method, dont know if we should
	 * spinlock
	 */
T
Takashi Iwai 已提交
5575
	/* malloc all buffer even if not enabled to get sure */
5576 5577
	/* Update for MADI rev 204: we need to allocate for all channels,
	 * otherwise it doesn't work at 96kHz */
5578

T
Takashi Iwai 已提交
5579
	err =
5580 5581
		snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
	if (err < 0) {
5582 5583
		dev_info(hdspm->card->dev,
			 "err on snd_pcm_lib_malloc_pages: %d\n", err);
T
Takashi Iwai 已提交
5584
		return err;
5585
	}
T
Takashi Iwai 已提交
5586 5587 5588

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {

5589
		hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
T
Takashi Iwai 已提交
5590 5591 5592 5593 5594 5595
				params_channels(params));

		for (i = 0; i < params_channels(params); ++i)
			snd_hdspm_enable_out(hdspm, i, 1);

		hdspm->playback_buffer =
5596
			(unsigned char *) substream->runtime->dma_area;
5597 5598
		dev_dbg(hdspm->card->dev,
			"Allocated sample buffer for playback at %p\n",
R
Remy Bruno 已提交
5599
				hdspm->playback_buffer);
T
Takashi Iwai 已提交
5600
	} else {
5601
		hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
T
Takashi Iwai 已提交
5602 5603 5604 5605 5606 5607
				params_channels(params));

		for (i = 0; i < params_channels(params); ++i)
			snd_hdspm_enable_in(hdspm, i, 1);

		hdspm->capture_buffer =
5608
			(unsigned char *) substream->runtime->dma_area;
5609 5610
		dev_dbg(hdspm->card->dev,
			"Allocated sample buffer for capture at %p\n",
R
Remy Bruno 已提交
5611
				hdspm->capture_buffer);
T
Takashi Iwai 已提交
5612
	}
5613

R
Remy Bruno 已提交
5614
	/*
5615 5616
	   dev_dbg(hdspm->card->dev,
	   "Allocated sample buffer for %s at 0x%08X\n",
R
Remy Bruno 已提交
5617 5618
	   substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
	   "playback" : "capture",
5619
	   snd_pcm_sgbuf_get_addr(substream, 0));
5620
	   */
5621
	/*
5622 5623
	   dev_dbg(hdspm->card->dev,
	   "set_hwparams: %s %d Hz, %d channels, bs = %d\n",
5624 5625 5626 5627 5628 5629 5630
	   substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
	   "playback" : "capture",
	   params_rate(params), params_channels(params),
	   params_buffer_size(params));
	   */


5631 5632 5633 5634 5635 5636 5637 5638 5639 5640
	/*  For AES cards, the float format bit is the same as the
	 *  preferred sync reference. Since we don't want to break
	 *  sync settings, we have to skip the remaining part of this
	 *  function.
	 */
	if (hdspm->io_type == AES32) {
		return 0;
	}


5641 5642 5643
	/* Switch to native float format if requested */
	if (SNDRV_PCM_FORMAT_FLOAT_LE == params_format(params)) {
		if (!(hdspm->control_register & HDSPe_FLOAT_FORMAT))
5644 5645
			dev_info(hdspm->card->dev,
				 "Switching to native 32bit LE float format.\n");
5646 5647 5648 5649

		hdspm->control_register |= HDSPe_FLOAT_FORMAT;
	} else if (SNDRV_PCM_FORMAT_S32_LE == params_format(params)) {
		if (hdspm->control_register & HDSPe_FLOAT_FORMAT)
5650 5651
			dev_info(hdspm->card->dev,
				 "Switching to native 32bit LE integer format.\n");
5652 5653 5654 5655 5656

		hdspm->control_register &= ~HDSPe_FLOAT_FORMAT;
	}
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

T
Takashi Iwai 已提交
5657 5658 5659
	return 0;
}

5660
static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
5661 5662
{
	int i;
5663
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
5664 5665 5666

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {

5667
		/* params_channels(params) should be enough,
T
Takashi Iwai 已提交
5668
		   but to get sure in case of error */
5669
		for (i = 0; i < hdspm->max_channels_out; ++i)
T
Takashi Iwai 已提交
5670 5671 5672 5673
			snd_hdspm_enable_out(hdspm, i, 0);

		hdspm->playback_buffer = NULL;
	} else {
5674
		for (i = 0; i < hdspm->max_channels_in; ++i)
T
Takashi Iwai 已提交
5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685
			snd_hdspm_enable_in(hdspm, i, 0);

		hdspm->capture_buffer = NULL;

	}

	snd_pcm_lib_free_pages(substream);

	return 0;
}

5686

5687
static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
5688
		struct snd_pcm_channel_info *info)
T
Takashi Iwai 已提交
5689
{
5690
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
5691

5692 5693
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		if (snd_BUG_ON(info->channel >= hdspm->max_channels_out)) {
5694 5695 5696
			dev_info(hdspm->card->dev,
				 "snd_hdspm_channel_info: output channel out of range (%d)\n",
				 info->channel);
5697 5698
			return -EINVAL;
		}
T
Takashi Iwai 已提交
5699

5700
		if (hdspm->channel_map_out[info->channel] < 0) {
5701 5702 5703
			dev_info(hdspm->card->dev,
				 "snd_hdspm_channel_info: output channel %d mapped out\n",
				 info->channel);
5704 5705 5706 5707 5708 5709 5710
			return -EINVAL;
		}

		info->offset = hdspm->channel_map_out[info->channel] *
			HDSPM_CHANNEL_BUFFER_BYTES;
	} else {
		if (snd_BUG_ON(info->channel >= hdspm->max_channels_in)) {
5711 5712 5713
			dev_info(hdspm->card->dev,
				 "snd_hdspm_channel_info: input channel out of range (%d)\n",
				 info->channel);
5714 5715 5716 5717
			return -EINVAL;
		}

		if (hdspm->channel_map_in[info->channel] < 0) {
5718 5719 5720
			dev_info(hdspm->card->dev,
				 "snd_hdspm_channel_info: input channel %d mapped out\n",
				 info->channel);
5721 5722 5723 5724 5725 5726
			return -EINVAL;
		}

		info->offset = hdspm->channel_map_in[info->channel] *
			HDSPM_CHANNEL_BUFFER_BYTES;
	}
T
Takashi Iwai 已提交
5727 5728 5729 5730 5731 5732

	info->first = 0;
	info->step = 32;
	return 0;
}

5733

5734
static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
5735
		unsigned int cmd, void *arg)
T
Takashi Iwai 已提交
5736 5737 5738
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_RESET:
T
Takashi Iwai 已提交
5739
		return snd_hdspm_reset(substream);
T
Takashi Iwai 已提交
5740 5741

	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
5742 5743 5744 5745
		{
			struct snd_pcm_channel_info *info = arg;
			return snd_hdspm_channel_info(substream, info);
		}
T
Takashi Iwai 已提交
5746 5747 5748 5749 5750 5751 5752
	default:
		break;
	}

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

5753
static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
T
Takashi Iwai 已提交
5754
{
5755 5756
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
T
Takashi Iwai 已提交
5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778
	int running;

	spin_lock(&hdspm->lock);
	running = hdspm->running;
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		running |= 1 << substream->stream;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		running &= ~(1 << substream->stream);
		break;
	default:
		snd_BUG();
		spin_unlock(&hdspm->lock);
		return -EINVAL;
	}
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = hdspm->capture_substream;
	else
		other = hdspm->playback_substream;

	if (other) {
5779
		struct snd_pcm_substream *s;
5780
		snd_pcm_group_for_each_entry(s, substream) {
T
Takashi Iwai 已提交
5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791
			if (s == other) {
				snd_pcm_trigger_done(s, substream);
				if (cmd == SNDRV_PCM_TRIGGER_START)
					running |= 1 << s->stream;
				else
					running &= ~(1 << s->stream);
				goto _ok;
			}
		}
		if (cmd == SNDRV_PCM_TRIGGER_START) {
			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK))
5792 5793
					&& substream->stream ==
					SNDRV_PCM_STREAM_CAPTURE)
T
Takashi Iwai 已提交
5794 5795 5796
				hdspm_silence_playback(hdspm);
		} else {
			if (running &&
5797
				substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
T
Takashi Iwai 已提交
5798 5799 5800 5801 5802 5803
				hdspm_silence_playback(hdspm);
		}
	} else {
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
			hdspm_silence_playback(hdspm);
	}
5804
_ok:
T
Takashi Iwai 已提交
5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815
	snd_pcm_trigger_done(substream, substream);
	if (!hdspm->running && running)
		hdspm_start_audio(hdspm);
	else if (hdspm->running && !running)
		hdspm_stop_audio(hdspm);
	hdspm->running = running;
	spin_unlock(&hdspm->lock);

	return 0;
}

5816
static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
5817 5818 5819 5820
{
	return 0;
}

5821
static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
T
Takashi Iwai 已提交
5822 5823 5824 5825 5826 5827 5828 5829 5830
	.info = (SNDRV_PCM_INFO_MMAP |
		 SNDRV_PCM_INFO_MMAP_VALID |
		 SNDRV_PCM_INFO_NONINTERLEAVED |
		 SNDRV_PCM_INFO_SYNC_START | SNDRV_PCM_INFO_DOUBLE),
	.formats = SNDRV_PCM_FMTBIT_S32_LE,
	.rates = (SNDRV_PCM_RATE_32000 |
		  SNDRV_PCM_RATE_44100 |
		  SNDRV_PCM_RATE_48000 |
		  SNDRV_PCM_RATE_64000 |
R
Remy Bruno 已提交
5831 5832
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
T
Takashi Iwai 已提交
5833
	.rate_min = 32000,
R
Remy Bruno 已提交
5834
	.rate_max = 192000,
T
Takashi Iwai 已提交
5835 5836 5837 5838
	.channels_min = 1,
	.channels_max = HDSPM_MAX_CHANNELS,
	.buffer_bytes_max =
	    HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
5839
	.period_bytes_min = (32 * 4),
5840
	.period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
T
Takashi Iwai 已提交
5841
	.periods_min = 2,
5842
	.periods_max = 512,
T
Takashi Iwai 已提交
5843 5844 5845
	.fifo_size = 0
};

5846
static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
T
Takashi Iwai 已提交
5847 5848 5849 5850 5851 5852 5853 5854 5855
	.info = (SNDRV_PCM_INFO_MMAP |
		 SNDRV_PCM_INFO_MMAP_VALID |
		 SNDRV_PCM_INFO_NONINTERLEAVED |
		 SNDRV_PCM_INFO_SYNC_START),
	.formats = SNDRV_PCM_FMTBIT_S32_LE,
	.rates = (SNDRV_PCM_RATE_32000 |
		  SNDRV_PCM_RATE_44100 |
		  SNDRV_PCM_RATE_48000 |
		  SNDRV_PCM_RATE_64000 |
R
Remy Bruno 已提交
5856 5857
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
T
Takashi Iwai 已提交
5858
	.rate_min = 32000,
R
Remy Bruno 已提交
5859
	.rate_max = 192000,
T
Takashi Iwai 已提交
5860 5861 5862 5863
	.channels_min = 1,
	.channels_max = HDSPM_MAX_CHANNELS,
	.buffer_bytes_max =
	    HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
5864
	.period_bytes_min = (32 * 4),
5865
	.period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
T
Takashi Iwai 已提交
5866
	.periods_min = 2,
5867
	.periods_max = 512,
T
Takashi Iwai 已提交
5868 5869 5870
	.fifo_size = 0
};

5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904
static int snd_hdspm_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule *rule)
{
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r =
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);

	if (r->min > 96000 && r->max <= 192000) {
		struct snd_interval t = {
			.min = hdspm->qs_in_channels,
			.max = hdspm->qs_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->min > 48000 && r->max <= 96000) {
		struct snd_interval t = {
			.min = hdspm->ds_in_channels,
			.max = hdspm->ds_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
		struct snd_interval t = {
			.min = hdspm->ss_in_channels,
			.max = hdspm->ss_in_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}

	return 0;
}
T
Takashi Iwai 已提交
5905

5906
static int snd_hdspm_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
5907
					   struct snd_pcm_hw_rule * rule)
T
Takashi Iwai 已提交
5908
{
5909 5910
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
T
Takashi Iwai 已提交
5911
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5912
	struct snd_interval *r =
T
Takashi Iwai 已提交
5913 5914
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);

5915 5916 5917 5918 5919 5920 5921 5922
	if (r->min > 96000 && r->max <= 192000) {
		struct snd_interval t = {
			.min = hdspm->qs_out_channels,
			.max = hdspm->qs_out_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->min > 48000 && r->max <= 96000) {
5923
		struct snd_interval t = {
5924 5925
			.min = hdspm->ds_out_channels,
			.max = hdspm->ds_out_channels,
T
Takashi Iwai 已提交
5926 5927 5928 5929
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
5930
		struct snd_interval t = {
5931 5932
			.min = hdspm->ss_out_channels,
			.max = hdspm->ss_out_channels,
T
Takashi Iwai 已提交
5933 5934 5935
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
5936
	} else {
T
Takashi Iwai 已提交
5937 5938 5939 5940
	}
	return 0;
}

5941
static int snd_hdspm_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
5942
					   struct snd_pcm_hw_rule * rule)
T
Takashi Iwai 已提交
5943
{
5944 5945
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
T
Takashi Iwai 已提交
5946
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
5947
	struct snd_interval *r =
T
Takashi Iwai 已提交
5948 5949
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);

5950
	if (c->min >= hdspm->ss_in_channels) {
5951
		struct snd_interval t = {
T
Takashi Iwai 已提交
5952 5953 5954 5955 5956
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
5957 5958 5959 5960 5961 5962 5963 5964
	} else if (c->max <= hdspm->qs_in_channels) {
		struct snd_interval t = {
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdspm->ds_in_channels) {
5965
		struct snd_interval t = {
T
Takashi Iwai 已提交
5966 5967 5968 5969
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982
		return snd_interval_refine(r, &t);
	}

	return 0;
}
static int snd_hdspm_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule *rule)
{
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r =
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
T
Takashi Iwai 已提交
5983

5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003
	if (c->min >= hdspm->ss_out_channels) {
		struct snd_interval t = {
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdspm->qs_out_channels) {
		struct snd_interval t = {
			.min = 128000,
			.max = 192000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= hdspm->ds_out_channels) {
		struct snd_interval t = {
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};
T
Takashi Iwai 已提交
6004 6005
		return snd_interval_refine(r, &t);
	}
6006

T
Takashi Iwai 已提交
6007 6008 6009
	return 0;
}

6010
static int snd_hdspm_hw_rule_in_channels(struct snd_pcm_hw_params *params,
6011 6012 6013 6014 6015 6016
				      struct snd_pcm_hw_rule *rule)
{
	unsigned int list[3];
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c = hw_param_interval(params,
			SNDRV_PCM_HW_PARAM_CHANNELS);
6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035

	list[0] = hdspm->qs_in_channels;
	list[1] = hdspm->ds_in_channels;
	list[2] = hdspm->ss_in_channels;
	return snd_interval_list(c, 3, list, 0);
}

static int snd_hdspm_hw_rule_out_channels(struct snd_pcm_hw_params *params,
				      struct snd_pcm_hw_rule *rule)
{
	unsigned int list[3];
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c = hw_param_interval(params,
			SNDRV_PCM_HW_PARAM_CHANNELS);

	list[0] = hdspm->qs_out_channels;
	list[1] = hdspm->ds_out_channels;
	list[2] = hdspm->ss_out_channels;
	return snd_interval_list(c, 3, list, 0);
6036 6037 6038
}


T
Takashi Iwai 已提交
6039 6040 6041
static unsigned int hdspm_aes32_sample_rates[] = {
	32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
};
6042

T
Takashi Iwai 已提交
6043 6044
static struct snd_pcm_hw_constraint_list
hdspm_hw_constraints_aes32_sample_rates = {
6045 6046 6047 6048 6049
	.count = ARRAY_SIZE(hdspm_aes32_sample_rates),
	.list = hdspm_aes32_sample_rates,
	.mask = 0
};

6050
static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
6051
{
6052 6053
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
6054 6055 6056 6057 6058

	spin_lock_irq(&hdspm->lock);

	snd_pcm_set_sync(substream);

6059

T
Takashi Iwai 已提交
6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070
	runtime->hw = snd_hdspm_playback_subinfo;

	if (hdspm->capture_substream == NULL)
		hdspm_stop_audio(hdspm);

	hdspm->playback_pid = current->pid;
	hdspm->playback_substream = substream;

	spin_unlock_irq(&hdspm->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
6071
	snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
T
Takashi Iwai 已提交
6072

6073 6074 6075
	switch (hdspm->io_type) {
	case AIO:
	case RayDAT:
6076 6077 6078 6079 6080 6081 6082
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
					     32, 4096);
		/* RayDAT & AIO have a fixed buffer of 16384 samples per channel */
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
					     16384, 16384);
6083 6084 6085
		break;

	default:
6086 6087 6088 6089
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
					     64, 8192);
		break;
6090
	}
T
Takashi Iwai 已提交
6091

6092
	if (AES32 == hdspm->io_type) {
6093
		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
6094 6095 6096 6097
		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				&hdspm_hw_constraints_aes32_sample_rates);
	} else {
		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6098 6099
				snd_hdspm_hw_rule_rate_out_channels, hdspm,
				SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6100
	}
6101 6102 6103 6104 6105 6106 6107 6108 6109

	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			snd_hdspm_hw_rule_out_channels, hdspm,
			SNDRV_PCM_HW_PARAM_CHANNELS, -1);

	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			snd_hdspm_hw_rule_out_channels_rate, hdspm,
			SNDRV_PCM_HW_PARAM_RATE, -1);

T
Takashi Iwai 已提交
6110 6111 6112
	return 0;
}

6113
static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
6114
{
6115
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127

	spin_lock_irq(&hdspm->lock);

	hdspm->playback_pid = -1;
	hdspm->playback_substream = NULL;

	spin_unlock_irq(&hdspm->lock);

	return 0;
}


6128
static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
6129
{
6130 6131
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145

	spin_lock_irq(&hdspm->lock);
	snd_pcm_set_sync(substream);
	runtime->hw = snd_hdspm_capture_subinfo;

	if (hdspm->playback_substream == NULL)
		hdspm_stop_audio(hdspm);

	hdspm->capture_pid = current->pid;
	hdspm->capture_substream = substream;

	spin_unlock_irq(&hdspm->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
6146 6147
	snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);

6148 6149 6150
	switch (hdspm->io_type) {
	case AIO:
	case RayDAT:
6151 6152 6153 6154 6155 6156 6157
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
					     32, 4096);
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
					     16384, 16384);
		break;
6158 6159

	default:
6160 6161 6162 6163
		snd_pcm_hw_constraint_minmax(runtime,
					     SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
					     64, 8192);
		break;
6164 6165 6166
	}

	if (AES32 == hdspm->io_type) {
6167
		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
6168 6169 6170 6171
		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				&hdspm_hw_constraints_aes32_sample_rates);
	} else {
		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
6172 6173
				snd_hdspm_hw_rule_rate_in_channels, hdspm,
				SNDRV_PCM_HW_PARAM_CHANNELS, -1);
6174
	}
6175 6176 6177 6178 6179 6180 6181 6182 6183

	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			snd_hdspm_hw_rule_in_channels, hdspm,
			SNDRV_PCM_HW_PARAM_CHANNELS, -1);

	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			snd_hdspm_hw_rule_in_channels_rate, hdspm,
			SNDRV_PCM_HW_PARAM_RATE, -1);

T
Takashi Iwai 已提交
6184 6185 6186
	return 0;
}

6187
static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
6188
{
6189
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
6190 6191 6192 6193 6194 6195 6196 6197 6198 6199

	spin_lock_irq(&hdspm->lock);

	hdspm->capture_pid = -1;
	hdspm->capture_substream = NULL;

	spin_unlock_irq(&hdspm->lock);
	return 0;
}

6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212
static int snd_hdspm_hwdep_dummy_op(struct snd_hwdep *hw, struct file *file)
{
	/* we have nothing to initialize but the call is required */
	return 0;
}

static inline int copy_u32_le(void __user *dest, void __iomem *src)
{
	u32 val = readl(src);
	return copy_to_user(dest, &val, 4);
}

static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
6213
		unsigned int cmd, unsigned long arg)
T
Takashi Iwai 已提交
6214
{
6215
	void __user *argp = (void __user *)arg;
T
Takashi Iwai 已提交
6216
	struct hdspm *hdspm = hw->private_data;
6217
	struct hdspm_mixer_ioctl mixer;
6218 6219
	struct hdspm_config info;
	struct hdspm_status status;
6220
	struct hdspm_version hdspm_version;
6221
	struct hdspm_peak_rms *levels;
6222 6223 6224 6225
	struct hdspm_ltc ltc;
	unsigned int statusregister;
	long unsigned int s;
	int i = 0;
T
Takashi Iwai 已提交
6226 6227 6228 6229

	switch (cmd) {

	case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
6230
		levels = &hdspm->peak_rms;
6231
		for (i = 0; i < HDSPM_MAX_CHANNELS; i++) {
6232
			levels->input_peaks[i] =
6233 6234
				readl(hdspm->iobase +
						HDSPM_MADI_INPUT_PEAK + i*4);
6235
			levels->playback_peaks[i] =
6236 6237
				readl(hdspm->iobase +
						HDSPM_MADI_PLAYBACK_PEAK + i*4);
6238
			levels->output_peaks[i] =
6239 6240 6241
				readl(hdspm->iobase +
						HDSPM_MADI_OUTPUT_PEAK + i*4);

6242
			levels->input_rms[i] =
6243 6244 6245 6246
				((uint64_t) readl(hdspm->iobase +
					HDSPM_MADI_INPUT_RMS_H + i*4) << 32) |
				(uint64_t) readl(hdspm->iobase +
						HDSPM_MADI_INPUT_RMS_L + i*4);
6247
			levels->playback_rms[i] =
6248 6249 6250 6251
				((uint64_t)readl(hdspm->iobase +
					HDSPM_MADI_PLAYBACK_RMS_H+i*4) << 32) |
				(uint64_t)readl(hdspm->iobase +
					HDSPM_MADI_PLAYBACK_RMS_L + i*4);
6252
			levels->output_rms[i] =
6253 6254 6255 6256 6257 6258 6259
				((uint64_t)readl(hdspm->iobase +
					HDSPM_MADI_OUTPUT_RMS_H + i*4) << 32) |
				(uint64_t)readl(hdspm->iobase +
						HDSPM_MADI_OUTPUT_RMS_L + i*4);
		}

		if (hdspm->system_sample_rate > 96000) {
6260
			levels->speed = qs;
6261
		} else if (hdspm->system_sample_rate > 48000) {
6262
			levels->speed = ds;
6263
		} else {
6264
			levels->speed = ss;
6265
		}
6266
		levels->status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
6267

6268
		s = copy_to_user(argp, levels, sizeof(struct hdspm_peak_rms));
6269
		if (0 != s) {
6270
			/* dev_err(hdspm->card->dev, "copy_to_user(.., .., %lu): %lu
6271 6272
			 [Levels]\n", sizeof(struct hdspm_peak_rms), s);
			 */
T
Takashi Iwai 已提交
6273
			return -EFAULT;
6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292
		}
		break;

	case SNDRV_HDSPM_IOCTL_GET_LTC:
		ltc.ltc = hdspm_read(hdspm, HDSPM_RD_TCO);
		i = hdspm_read(hdspm, HDSPM_RD_TCO + 4);
		if (i & HDSPM_TCO1_LTC_Input_valid) {
			switch (i & (HDSPM_TCO1_LTC_Format_LSB |
				HDSPM_TCO1_LTC_Format_MSB)) {
			case 0:
				ltc.format = fps_24;
				break;
			case HDSPM_TCO1_LTC_Format_LSB:
				ltc.format = fps_25;
				break;
			case HDSPM_TCO1_LTC_Format_MSB:
				ltc.format = fps_2997;
				break;
			default:
6293
				ltc.format = fps_30;
6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315
				break;
			}
			if (i & HDSPM_TCO1_set_drop_frame_flag) {
				ltc.frame = drop_frame;
			} else {
				ltc.frame = full_frame;
			}
		} else {
			ltc.format = format_invalid;
			ltc.frame = frame_invalid;
		}
		if (i & HDSPM_TCO1_Video_Input_Format_NTSC) {
			ltc.input_format = ntsc;
		} else if (i & HDSPM_TCO1_Video_Input_Format_PAL) {
			ltc.input_format = pal;
		} else {
			ltc.input_format = no_video;
		}

		s = copy_to_user(argp, &ltc, sizeof(struct hdspm_ltc));
		if (0 != s) {
			/*
6316
			  dev_err(hdspm->card->dev, "copy_to_user(.., .., %lu): %lu [LTC]\n", sizeof(struct hdspm_ltc), s); */
T
Takashi Iwai 已提交
6317
			return -EFAULT;
6318
		}
T
Takashi Iwai 已提交
6319 6320 6321

		break;

6322
	case SNDRV_HDSPM_IOCTL_GET_CONFIG:
T
Takashi Iwai 已提交
6323

6324
		memset(&info, 0, sizeof(info));
T
Takashi Iwai 已提交
6325
		spin_lock_irq(&hdspm->lock);
T
Takashi Iwai 已提交
6326 6327
		info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
		info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
T
Takashi Iwai 已提交
6328 6329 6330

		info.system_sample_rate = hdspm->system_sample_rate;
		info.autosync_sample_rate =
6331
			hdspm_external_sample_rate(hdspm);
T
Takashi Iwai 已提交
6332 6333 6334
		info.system_clock_mode = hdspm_system_clock_mode(hdspm);
		info.clock_source = hdspm_clock_source(hdspm);
		info.autosync_ref = hdspm_autosync_ref(hdspm);
6335
		info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut);
T
Takashi Iwai 已提交
6336 6337
		info.passthru = 0;
		spin_unlock_irq(&hdspm->lock);
6338
		if (copy_to_user(argp, &info, sizeof(info)))
T
Takashi Iwai 已提交
6339 6340 6341
			return -EFAULT;
		break;

6342
	case SNDRV_HDSPM_IOCTL_GET_STATUS:
6343 6344
		memset(&status, 0, sizeof(status));

6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368
		status.card_type = hdspm->io_type;

		status.autosync_source = hdspm_autosync_ref(hdspm);

		status.card_clock = 110069313433624ULL;
		status.master_period = hdspm_read(hdspm, HDSPM_RD_PLL_FREQ);

		switch (hdspm->io_type) {
		case MADI:
		case MADIface:
			status.card_specific.madi.sync_wc =
				hdspm_wc_sync_check(hdspm);
			status.card_specific.madi.sync_madi =
				hdspm_madi_sync_check(hdspm);
			status.card_specific.madi.sync_tco =
				hdspm_tco_sync_check(hdspm);
			status.card_specific.madi.sync_in =
				hdspm_sync_in_sync_check(hdspm);

			statusregister =
				hdspm_read(hdspm, HDSPM_statusRegister);
			status.card_specific.madi.madi_input =
				(statusregister & HDSPM_AB_int) ? 1 : 0;
			status.card_specific.madi.channel_format =
6369
				(statusregister & HDSPM_RX_64ch) ? 1 : 0;
6370 6371 6372 6373 6374 6375 6376
			/* TODO: Mac driver sets it when f_s>48kHz */
			status.card_specific.madi.frame_format = 0;

		default:
			break;
		}

6377
		if (copy_to_user(argp, &status, sizeof(status)))
6378 6379 6380 6381 6382
			return -EFAULT;


		break;

T
Takashi Iwai 已提交
6383
	case SNDRV_HDSPM_IOCTL_GET_VERSION:
6384 6385
		memset(&hdspm_version, 0, sizeof(hdspm_version));

6386
		hdspm_version.card_type = hdspm->io_type;
6387
		strlcpy(hdspm_version.cardname, hdspm->card_name,
6388
				sizeof(hdspm_version.cardname));
6389
		hdspm_version.serial = hdspm->serial;
T
Takashi Iwai 已提交
6390
		hdspm_version.firmware_rev = hdspm->firmware_rev;
6391 6392 6393 6394
		hdspm_version.addons = 0;
		if (hdspm->tco)
			hdspm_version.addons |= HDSPM_ADDON_TCO;

6395
		if (copy_to_user(argp, &hdspm_version,
6396
					sizeof(hdspm_version)))
T
Takashi Iwai 已提交
6397 6398 6399 6400
			return -EFAULT;
		break;

	case SNDRV_HDSPM_IOCTL_GET_MIXER:
6401
		if (copy_from_user(&mixer, argp, sizeof(mixer)))
T
Takashi Iwai 已提交
6402
			return -EFAULT;
T
Takashi Iwai 已提交
6403
		if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
6404
					sizeof(struct hdspm_mixer)))
T
Takashi Iwai 已提交
6405 6406 6407 6408 6409 6410 6411 6412 6413
			return -EFAULT;
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

6414
static struct snd_pcm_ops snd_hdspm_playback_ops = {
T
Takashi Iwai 已提交
6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425
	.open = snd_hdspm_playback_open,
	.close = snd_hdspm_playback_release,
	.ioctl = snd_hdspm_ioctl,
	.hw_params = snd_hdspm_hw_params,
	.hw_free = snd_hdspm_hw_free,
	.prepare = snd_hdspm_prepare,
	.trigger = snd_hdspm_trigger,
	.pointer = snd_hdspm_hw_pointer,
	.page = snd_pcm_sgbuf_ops_page,
};

6426
static struct snd_pcm_ops snd_hdspm_capture_ops = {
T
Takashi Iwai 已提交
6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437
	.open = snd_hdspm_capture_open,
	.close = snd_hdspm_capture_release,
	.ioctl = snd_hdspm_ioctl,
	.hw_params = snd_hdspm_hw_params,
	.hw_free = snd_hdspm_hw_free,
	.prepare = snd_hdspm_prepare,
	.trigger = snd_hdspm_trigger,
	.pointer = snd_hdspm_hw_pointer,
	.page = snd_pcm_sgbuf_ops_page,
};

6438 6439
static int snd_hdspm_create_hwdep(struct snd_card *card,
				  struct hdspm *hdspm)
T
Takashi Iwai 已提交
6440
{
6441
	struct snd_hwdep *hw;
T
Takashi Iwai 已提交
6442 6443
	int err;

T
Takashi Iwai 已提交
6444 6445
	err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
	if (err < 0)
T
Takashi Iwai 已提交
6446 6447 6448 6449 6450 6451
		return err;

	hdspm->hwdep = hw;
	hw->private_data = hdspm;
	strcpy(hw->name, "HDSPM hwdep interface");

6452
	hw->ops.open = snd_hdspm_hwdep_dummy_op;
T
Takashi Iwai 已提交
6453
	hw->ops.ioctl = snd_hdspm_hwdep_ioctl;
6454
	hw->ops.ioctl_compat = snd_hdspm_hwdep_ioctl;
6455
	hw->ops.release = snd_hdspm_hwdep_dummy_op;
T
Takashi Iwai 已提交
6456 6457 6458 6459 6460 6461

	return 0;
}


/*------------------------------------------------------------
6462
   memory interface
T
Takashi Iwai 已提交
6463
 ------------------------------------------------------------*/
6464
static int snd_hdspm_preallocate_memory(struct hdspm *hdspm)
T
Takashi Iwai 已提交
6465 6466
{
	int err;
6467
	struct snd_pcm *pcm;
T
Takashi Iwai 已提交
6468 6469 6470 6471
	size_t wanted;

	pcm = hdspm->pcm;

R
Remy Bruno 已提交
6472
	wanted = HDSPM_DMA_AREA_BYTES;
T
Takashi Iwai 已提交
6473

T
Takashi Iwai 已提交
6474
	err =
T
Takashi Iwai 已提交
6475
	     snd_pcm_lib_preallocate_pages_for_all(pcm,
6476
						   SNDRV_DMA_TYPE_DEV_SG,
T
Takashi Iwai 已提交
6477 6478
						   snd_dma_pci_data(hdspm->pci),
						   wanted,
T
Takashi Iwai 已提交
6479 6480
						   wanted);
	if (err < 0) {
6481 6482
		dev_dbg(hdspm->card->dev,
			"Could not preallocate %zd Bytes\n", wanted);
T
Takashi Iwai 已提交
6483 6484 6485

		return err;
	} else
6486 6487
		dev_dbg(hdspm->card->dev,
			" Preallocated %zd Bytes\n", wanted);
T
Takashi Iwai 已提交
6488 6489 6490 6491

	return 0;
}

6492 6493

static void hdspm_set_sgbuf(struct hdspm *hdspm,
6494
			    struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
6495 6496 6497
			     unsigned int reg, int channels)
{
	int i;
6498 6499

	/* continuous memory segment */
T
Takashi Iwai 已提交
6500 6501
	for (i = 0; i < (channels * 16); i++)
		hdspm_write(hdspm, reg + 4 * i,
6502
				snd_pcm_sgbuf_get_addr(substream, 4096 * i));
T
Takashi Iwai 已提交
6503 6504
}

6505

T
Takashi Iwai 已提交
6506
/* ------------- ALSA Devices ---------------------------- */
6507 6508
static int snd_hdspm_create_pcm(struct snd_card *card,
				struct hdspm *hdspm)
T
Takashi Iwai 已提交
6509
{
6510
	struct snd_pcm *pcm;
T
Takashi Iwai 已提交
6511 6512
	int err;

T
Takashi Iwai 已提交
6513 6514
	err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
	if (err < 0)
T
Takashi Iwai 已提交
6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527
		return err;

	hdspm->pcm = pcm;
	pcm->private_data = hdspm;
	strcpy(pcm->name, hdspm->card_name);

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
			&snd_hdspm_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
			&snd_hdspm_capture_ops);

	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;

T
Takashi Iwai 已提交
6528 6529
	err = snd_hdspm_preallocate_memory(hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6530 6531 6532 6533 6534
		return err;

	return 0;
}

6535
static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
T
Takashi Iwai 已提交
6536
{
6537 6538 6539 6540
	int i;

	for (i = 0; i < hdspm->midiPorts; i++)
		snd_hdspm_flush_midi_input(hdspm, i);
T
Takashi Iwai 已提交
6541 6542
}

6543 6544
static int snd_hdspm_create_alsa_devices(struct snd_card *card,
					 struct hdspm *hdspm)
T
Takashi Iwai 已提交
6545
{
6546
	int err, i;
T
Takashi Iwai 已提交
6547

6548
	dev_dbg(card->dev, "Create card...\n");
T
Takashi Iwai 已提交
6549 6550
	err = snd_hdspm_create_pcm(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6551 6552
		return err;

6553 6554 6555 6556 6557 6558 6559 6560
	i = 0;
	while (i < hdspm->midiPorts) {
		err = snd_hdspm_create_midi(card, hdspm, i);
		if (err < 0) {
			return err;
		}
		i++;
	}
T
Takashi Iwai 已提交
6561

T
Takashi Iwai 已提交
6562 6563
	err = snd_hdspm_create_controls(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6564 6565
		return err;

T
Takashi Iwai 已提交
6566 6567
	err = snd_hdspm_create_hwdep(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6568 6569
		return err;

6570
	dev_dbg(card->dev, "proc init...\n");
T
Takashi Iwai 已提交
6571 6572 6573 6574 6575 6576 6577 6578 6579 6580
	snd_hdspm_proc_init(hdspm);

	hdspm->system_sample_rate = -1;
	hdspm->last_external_sample_rate = -1;
	hdspm->last_internal_sample_rate = -1;
	hdspm->playback_pid = -1;
	hdspm->capture_pid = -1;
	hdspm->capture_substream = NULL;
	hdspm->playback_substream = NULL;

6581
	dev_dbg(card->dev, "Set defaults...\n");
T
Takashi Iwai 已提交
6582 6583
	err = snd_hdspm_set_defaults(hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6584 6585
		return err;

6586
	dev_dbg(card->dev, "Update mixer controls...\n");
T
Takashi Iwai 已提交
6587 6588
	hdspm_update_simple_mixer_controls(hdspm);

6589
	dev_dbg(card->dev, "Initializeing complete ???\n");
T
Takashi Iwai 已提交
6590

T
Takashi Iwai 已提交
6591 6592
	err = snd_card_register(card);
	if (err < 0) {
6593
		dev_err(card->dev, "error registering card\n");
T
Takashi Iwai 已提交
6594 6595 6596
		return err;
	}

6597
	dev_dbg(card->dev, "... yes now\n");
T
Takashi Iwai 已提交
6598 6599 6600 6601

	return 0;
}

6602 6603 6604
static int snd_hdspm_create(struct snd_card *card,
			    struct hdspm *hdspm)
{
6605

T
Takashi Iwai 已提交
6606 6607 6608 6609 6610 6611 6612 6613 6614 6615
	struct pci_dev *pci = hdspm->pci;
	int err;
	unsigned long io_extent;

	hdspm->irq = -1;
	hdspm->card = card;

	spin_lock_init(&hdspm->lock);

	pci_read_config_word(hdspm->pci,
6616
			PCI_CLASS_REVISION, &hdspm->firmware_rev);
R
Remy Bruno 已提交
6617

T
Takashi Iwai 已提交
6618
	strcpy(card->mixername, "Xilinx FPGA");
6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636
	strcpy(card->driver, "HDSPM");

	switch (hdspm->firmware_rev) {
	case HDSPM_RAYDAT_REV:
		hdspm->io_type = RayDAT;
		hdspm->card_name = "RME RayDAT";
		hdspm->midiPorts = 2;
		break;
	case HDSPM_AIO_REV:
		hdspm->io_type = AIO;
		hdspm->card_name = "RME AIO";
		hdspm->midiPorts = 1;
		break;
	case HDSPM_MADIFACE_REV:
		hdspm->io_type = MADIface;
		hdspm->card_name = "RME MADIface";
		hdspm->midiPorts = 1;
		break;
6637
	default:
6638 6639 6640 6641 6642 6643
		if ((hdspm->firmware_rev == 0xf0) ||
			((hdspm->firmware_rev >= 0xe6) &&
					(hdspm->firmware_rev <= 0xea))) {
			hdspm->io_type = AES32;
			hdspm->card_name = "RME AES32";
			hdspm->midiPorts = 2;
6644
		} else if ((hdspm->firmware_rev == 0xd2) ||
6645 6646 6647 6648 6649 6650
			((hdspm->firmware_rev >= 0xc8)  &&
				(hdspm->firmware_rev <= 0xcf))) {
			hdspm->io_type = MADI;
			hdspm->card_name = "RME MADI";
			hdspm->midiPorts = 3;
		} else {
6651 6652
			dev_err(card->dev,
				"unknown firmware revision %x\n",
6653
				hdspm->firmware_rev);
6654 6655
			return -ENODEV;
		}
R
Remy Bruno 已提交
6656
	}
T
Takashi Iwai 已提交
6657

T
Takashi Iwai 已提交
6658 6659
	err = pci_enable_device(pci);
	if (err < 0)
T
Takashi Iwai 已提交
6660 6661 6662 6663
		return err;

	pci_set_master(hdspm->pci);

T
Takashi Iwai 已提交
6664 6665
	err = pci_request_regions(pci, "hdspm");
	if (err < 0)
T
Takashi Iwai 已提交
6666 6667 6668 6669 6670
		return err;

	hdspm->port = pci_resource_start(pci, 0);
	io_extent = pci_resource_len(pci, 0);

6671
	dev_dbg(card->dev, "grabbed memory region 0x%lx-0x%lx\n",
6672
			hdspm->port, hdspm->port + io_extent - 1);
T
Takashi Iwai 已提交
6673

T
Takashi Iwai 已提交
6674 6675
	hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
	if (!hdspm->iobase) {
6676
		dev_err(card->dev, "unable to remap region 0x%lx-0x%lx\n",
6677
				hdspm->port, hdspm->port + io_extent - 1);
T
Takashi Iwai 已提交
6678 6679
		return -EBUSY;
	}
6680
	dev_dbg(card->dev, "remapped region (0x%lx) 0x%lx-0x%lx\n",
6681 6682
			(unsigned long)hdspm->iobase, hdspm->port,
			hdspm->port + io_extent - 1);
T
Takashi Iwai 已提交
6683 6684

	if (request_irq(pci->irq, snd_hdspm_interrupt,
6685
			IRQF_SHARED, KBUILD_MODNAME, hdspm)) {
6686
		dev_err(card->dev, "unable to use IRQ %d\n", pci->irq);
T
Takashi Iwai 已提交
6687 6688 6689
		return -EBUSY;
	}

6690
	dev_dbg(card->dev, "use IRQ %d\n", pci->irq);
T
Takashi Iwai 已提交
6691 6692 6693

	hdspm->irq = pci->irq;

6694
	dev_dbg(card->dev, "kmalloc Mixer memory of %zd Bytes\n",
6695
			sizeof(struct hdspm_mixer));
T
Takashi Iwai 已提交
6696 6697
	hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
	if (!hdspm->mixer) {
6698 6699
		dev_err(card->dev,
			"unable to kmalloc Mixer memory of %d Bytes\n",
6700
				(int)sizeof(struct hdspm_mixer));
6701
		return -ENOMEM;
T
Takashi Iwai 已提交
6702 6703
	}

6704 6705 6706 6707 6708
	hdspm->port_names_in = NULL;
	hdspm->port_names_out = NULL;

	switch (hdspm->io_type) {
	case AES32:
6709 6710 6711
		hdspm->ss_in_channels = hdspm->ss_out_channels = AES32_CHANNELS;
		hdspm->ds_in_channels = hdspm->ds_out_channels = AES32_CHANNELS;
		hdspm->qs_in_channels = hdspm->qs_out_channels = AES32_CHANNELS;
6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725

		hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
			channel_map_aes32;
		hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
			channel_map_aes32;
		hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
			channel_map_aes32;
		hdspm->port_names_in_ss = hdspm->port_names_out_ss =
			texts_ports_aes32;
		hdspm->port_names_in_ds = hdspm->port_names_out_ds =
			texts_ports_aes32;
		hdspm->port_names_in_qs = hdspm->port_names_out_qs =
			texts_ports_aes32;

6726 6727
		hdspm->max_channels_out = hdspm->max_channels_in =
			AES32_CHANNELS;
6728 6729 6730 6731 6732
		hdspm->port_names_in = hdspm->port_names_out =
			texts_ports_aes32;
		hdspm->channel_map_in = hdspm->channel_map_out =
			channel_map_aes32;

6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745
		break;

	case MADI:
	case MADIface:
		hdspm->ss_in_channels = hdspm->ss_out_channels =
			MADI_SS_CHANNELS;
		hdspm->ds_in_channels = hdspm->ds_out_channels =
			MADI_DS_CHANNELS;
		hdspm->qs_in_channels = hdspm->qs_out_channels =
			MADI_QS_CHANNELS;

		hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
			channel_map_unity_ss;
6746
		hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
6747
			channel_map_unity_ss;
6748
		hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766
			channel_map_unity_ss;

		hdspm->port_names_in_ss = hdspm->port_names_out_ss =
			texts_ports_madi;
		hdspm->port_names_in_ds = hdspm->port_names_out_ds =
			texts_ports_madi;
		hdspm->port_names_in_qs = hdspm->port_names_out_qs =
			texts_ports_madi;
		break;

	case AIO:
		hdspm->ss_in_channels = AIO_IN_SS_CHANNELS;
		hdspm->ds_in_channels = AIO_IN_DS_CHANNELS;
		hdspm->qs_in_channels = AIO_IN_QS_CHANNELS;
		hdspm->ss_out_channels = AIO_OUT_SS_CHANNELS;
		hdspm->ds_out_channels = AIO_OUT_DS_CHANNELS;
		hdspm->qs_out_channels = AIO_OUT_QS_CHANNELS;

6767
		if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBI_D)) {
6768
			dev_info(card->dev, "AEB input board found\n");
6769 6770 6771 6772 6773 6774
			hdspm->ss_in_channels += 4;
			hdspm->ds_in_channels += 4;
			hdspm->qs_in_channels += 4;
		}

		if (0 == (hdspm_read(hdspm, HDSPM_statusRegister2) & HDSPM_s2_AEBO_D)) {
6775
			dev_info(card->dev, "AEB output board found\n");
6776 6777 6778 6779 6780
			hdspm->ss_out_channels += 4;
			hdspm->ds_out_channels += 4;
			hdspm->qs_out_channels += 4;
		}

6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841
		hdspm->channel_map_out_ss = channel_map_aio_out_ss;
		hdspm->channel_map_out_ds = channel_map_aio_out_ds;
		hdspm->channel_map_out_qs = channel_map_aio_out_qs;

		hdspm->channel_map_in_ss = channel_map_aio_in_ss;
		hdspm->channel_map_in_ds = channel_map_aio_in_ds;
		hdspm->channel_map_in_qs = channel_map_aio_in_qs;

		hdspm->port_names_in_ss = texts_ports_aio_in_ss;
		hdspm->port_names_out_ss = texts_ports_aio_out_ss;
		hdspm->port_names_in_ds = texts_ports_aio_in_ds;
		hdspm->port_names_out_ds = texts_ports_aio_out_ds;
		hdspm->port_names_in_qs = texts_ports_aio_in_qs;
		hdspm->port_names_out_qs = texts_ports_aio_out_qs;

		break;

	case RayDAT:
		hdspm->ss_in_channels = hdspm->ss_out_channels =
			RAYDAT_SS_CHANNELS;
		hdspm->ds_in_channels = hdspm->ds_out_channels =
			RAYDAT_DS_CHANNELS;
		hdspm->qs_in_channels = hdspm->qs_out_channels =
			RAYDAT_QS_CHANNELS;

		hdspm->max_channels_in = RAYDAT_SS_CHANNELS;
		hdspm->max_channels_out = RAYDAT_SS_CHANNELS;

		hdspm->channel_map_in_ss = hdspm->channel_map_out_ss =
			channel_map_raydat_ss;
		hdspm->channel_map_in_ds = hdspm->channel_map_out_ds =
			channel_map_raydat_ds;
		hdspm->channel_map_in_qs = hdspm->channel_map_out_qs =
			channel_map_raydat_qs;
		hdspm->channel_map_in = hdspm->channel_map_out =
			channel_map_raydat_ss;

		hdspm->port_names_in_ss = hdspm->port_names_out_ss =
			texts_ports_raydat_ss;
		hdspm->port_names_in_ds = hdspm->port_names_out_ds =
			texts_ports_raydat_ds;
		hdspm->port_names_in_qs = hdspm->port_names_out_qs =
			texts_ports_raydat_qs;


		break;

	}

	/* TCO detection */
	switch (hdspm->io_type) {
	case AIO:
	case RayDAT:
		if (hdspm_read(hdspm, HDSPM_statusRegister2) &
				HDSPM_s2_tco_detect) {
			hdspm->midiPorts++;
			hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
					GFP_KERNEL);
			if (NULL != hdspm->tco) {
				hdspm_tco_write(hdspm);
			}
6842
			dev_info(card->dev, "AIO/RayDAT TCO module found\n");
6843 6844 6845 6846 6847 6848
		} else {
			hdspm->tco = NULL;
		}
		break;

	case MADI:
6849
	case AES32:
6850 6851 6852 6853 6854 6855 6856
		if (hdspm_read(hdspm, HDSPM_statusRegister) & HDSPM_tco_detect) {
			hdspm->midiPorts++;
			hdspm->tco = kzalloc(sizeof(struct hdspm_tco),
					GFP_KERNEL);
			if (NULL != hdspm->tco) {
				hdspm_tco_write(hdspm);
			}
6857
			dev_info(card->dev, "MADI/AES TCO module found\n");
6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871
		} else {
			hdspm->tco = NULL;
		}
		break;

	default:
		hdspm->tco = NULL;
	}

	/* texts */
	switch (hdspm->io_type) {
	case AES32:
		if (hdspm->tco) {
			hdspm->texts_autosync = texts_autosync_aes_tco;
6872 6873
			hdspm->texts_autosync_items =
				ARRAY_SIZE(texts_autosync_aes_tco);
6874 6875
		} else {
			hdspm->texts_autosync = texts_autosync_aes;
6876 6877
			hdspm->texts_autosync_items =
				ARRAY_SIZE(texts_autosync_aes);
6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918
		}
		break;

	case MADI:
		if (hdspm->tco) {
			hdspm->texts_autosync = texts_autosync_madi_tco;
			hdspm->texts_autosync_items = 4;
		} else {
			hdspm->texts_autosync = texts_autosync_madi;
			hdspm->texts_autosync_items = 3;
		}
		break;

	case MADIface:

		break;

	case RayDAT:
		if (hdspm->tco) {
			hdspm->texts_autosync = texts_autosync_raydat_tco;
			hdspm->texts_autosync_items = 9;
		} else {
			hdspm->texts_autosync = texts_autosync_raydat;
			hdspm->texts_autosync_items = 8;
		}
		break;

	case AIO:
		if (hdspm->tco) {
			hdspm->texts_autosync = texts_autosync_aio_tco;
			hdspm->texts_autosync_items = 6;
		} else {
			hdspm->texts_autosync = texts_autosync_aio;
			hdspm->texts_autosync_items = 5;
		}
		break;

	}

	tasklet_init(&hdspm->midi_tasklet,
			hdspm_midi_tasklet, (unsigned long) hdspm);
T
Takashi Iwai 已提交
6919

6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938

	if (hdspm->io_type != MADIface) {
		hdspm->serial = (hdspm_read(hdspm,
				HDSPM_midiStatusIn0)>>8) & 0xFFFFFF;
		/* id contains either a user-provided value or the default
		 * NULL. If it's the default, we're safe to
		 * fill card->id with the serial number.
		 *
		 * If the serial number is 0xFFFFFF, then we're dealing with
		 * an old PCI revision that comes without a sane number. In
		 * this case, we don't set card->id to avoid collisions
		 * when running with multiple cards.
		 */
		if (NULL == id[hdspm->dev] && hdspm->serial != 0xFFFFFF) {
			sprintf(card->id, "HDSPMx%06x", hdspm->serial);
			snd_card_set_id(card, card->id);
		}
	}

6939
	dev_dbg(card->dev, "create alsa devices.\n");
T
Takashi Iwai 已提交
6940 6941
	err = snd_hdspm_create_alsa_devices(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
6942 6943 6944 6945 6946 6947 6948
		return err;

	snd_hdspm_initialize_midi_flush(hdspm);

	return 0;
}

6949

6950
static int snd_hdspm_free(struct hdspm * hdspm)
T
Takashi Iwai 已提交
6951 6952 6953 6954 6955 6956
{

	if (hdspm->port) {

		/* stop th audio, and cancel all interrupts */
		hdspm->control_register &=
T
Takashi Iwai 已提交
6957
		    ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
6958 6959
		      HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable |
		      HDSPM_Midi2InterruptEnable | HDSPM_Midi3InterruptEnable);
T
Takashi Iwai 已提交
6960 6961 6962 6963 6964 6965 6966
		hdspm_write(hdspm, HDSPM_controlRegister,
			    hdspm->control_register);
	}

	if (hdspm->irq >= 0)
		free_irq(hdspm->irq, (void *) hdspm);

6967
	kfree(hdspm->mixer);
T
Takashi Iwai 已提交
6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978

	if (hdspm->iobase)
		iounmap(hdspm->iobase);

	if (hdspm->port)
		pci_release_regions(hdspm->pci);

	pci_disable_device(hdspm->pci);
	return 0;
}

6979

6980
static void snd_hdspm_card_free(struct snd_card *card)
T
Takashi Iwai 已提交
6981
{
T
Takashi Iwai 已提交
6982
	struct hdspm *hdspm = card->private_data;
T
Takashi Iwai 已提交
6983 6984 6985 6986 6987

	if (hdspm)
		snd_hdspm_free(hdspm);
}

6988

6989 6990
static int snd_hdspm_probe(struct pci_dev *pci,
			   const struct pci_device_id *pci_id)
T
Takashi Iwai 已提交
6991 6992
{
	static int dev;
6993 6994
	struct hdspm *hdspm;
	struct snd_card *card;
T
Takashi Iwai 已提交
6995 6996 6997 6998 6999 7000 7001 7002 7003
	int err;

	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

7004 7005
	err = snd_card_new(&pci->dev, index[dev], id[dev],
			   THIS_MODULE, sizeof(struct hdspm), &card);
7006 7007
	if (err < 0)
		return err;
T
Takashi Iwai 已提交
7008

T
Takashi Iwai 已提交
7009
	hdspm = card->private_data;
T
Takashi Iwai 已提交
7010 7011 7012 7013
	card->private_free = snd_hdspm_card_free;
	hdspm->dev = dev;
	hdspm->pci = pci;

7014
	err = snd_hdspm_create(card, hdspm);
T
Takashi Iwai 已提交
7015
	if (err < 0) {
T
Takashi Iwai 已提交
7016 7017 7018 7019
		snd_card_free(card);
		return err;
	}

7020 7021 7022
	if (hdspm->io_type != MADIface) {
		sprintf(card->shortname, "%s_%x",
			hdspm->card_name,
7023
			hdspm->serial);
7024 7025
		sprintf(card->longname, "%s S/N 0x%x at 0x%lx, irq %d",
			hdspm->card_name,
7026
			hdspm->serial,
7027 7028 7029 7030 7031 7032
			hdspm->port, hdspm->irq);
	} else {
		sprintf(card->shortname, "%s", hdspm->card_name);
		sprintf(card->longname, "%s at 0x%lx, irq %d",
				hdspm->card_name, hdspm->port, hdspm->irq);
	}
T
Takashi Iwai 已提交
7033

T
Takashi Iwai 已提交
7034 7035
	err = snd_card_register(card);
	if (err < 0) {
T
Takashi Iwai 已提交
7036 7037 7038 7039 7040 7041 7042 7043 7044 7045
		snd_card_free(card);
		return err;
	}

	pci_set_drvdata(pci, card);

	dev++;
	return 0;
}

7046
static void snd_hdspm_remove(struct pci_dev *pci)
T
Takashi Iwai 已提交
7047 7048 7049 7050
{
	snd_card_free(pci_get_drvdata(pci));
}

7051
static struct pci_driver hdspm_driver = {
7052
	.name = KBUILD_MODNAME,
T
Takashi Iwai 已提交
7053 7054
	.id_table = snd_hdspm_ids,
	.probe = snd_hdspm_probe,
7055
	.remove = snd_hdspm_remove,
T
Takashi Iwai 已提交
7056 7057
};

7058
module_pci_driver(hdspm_driver);