hdspm.c 122.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 26 27 28 29 30 31
 *
 *   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
 *
 */
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/pci.h>
32
#include <linux/math64.h>
T
Takashi Iwai 已提交
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 64 65 66 67 68
#include <asm/io.h>

#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#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 */
static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */

/* Disable precise pointer at start */
static int precise_ptr[SNDRV_CARDS];

/* Send all playback to line outs */
static int line_outs_monitor[SNDRV_CARDS];

/* Enable Analog Outs on Channel 63/64 by default */
static int enable_monitor[SNDRV_CARDS];

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_param_array(precise_ptr, bool, NULL, 0444);
69
MODULE_PARM_DESC(precise_ptr, "Enable or disable precise pointer.");
T
Takashi Iwai 已提交
70 71 72 73 74 75 76 77 78 79

module_param_array(line_outs_monitor, bool, NULL, 0444);
MODULE_PARM_DESC(line_outs_monitor,
		 "Send playback streams to analog outs by default.");

module_param_array(enable_monitor, bool, NULL, 0444);
MODULE_PARM_DESC(enable_monitor,
		 "Enable Analog Out on Channel 63/64 by default.");

MODULE_AUTHOR
T
Takashi Iwai 已提交
80 81
      ("Winfried Ritsch <ritsch_AT_iem.at>, "
       "Paul Davis <paul@linuxaudiosystems.com>, "
R
Remy Bruno 已提交
82 83
       "Marcus Andersson, Thomas Charbonnel <thomas@undata.org>, "
       "Remy Bruno <remy.bruno@trinnov.com>");
T
Takashi Iwai 已提交
84 85 86 87 88 89 90 91 92 93
MODULE_DESCRIPTION("RME HDSPM");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{RME HDSPM-MADI}}");

/* --- Write registers. --- 
  These are defined as byte-offsets from the iobase value.  */

#define HDSPM_controlRegister	     64
#define HDSPM_interruptConfirmation  96
#define HDSPM_control2Reg	     256  /* not in specs ???????? */
94
#define HDSPM_freqReg                256  /* for AES32 */
T
Takashi Iwai 已提交
95 96
#define HDSPM_midiDataOut0  	     352  /* just believe in old code */
#define HDSPM_midiDataOut1  	     356
97
#define HDSPM_eeprom_wr		     384  /* for AES32 */
T
Takashi Iwai 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/* DMA enable for 64 channels, only Bit 0 is relevant */
#define HDSPM_outputEnableBase       512  /* 512-767  input  DMA */ 
#define HDSPM_inputEnableBase        768  /* 768-1023 output DMA */

/* 16 page addresses for each of the 64 channels DMA buffer in and out 
   (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 已提交
115 116 117 118 119 120
/*#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 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

#define HDSPM_midiDataIn0     360
#define HDSPM_midiDataIn1     364

/* status is data bytes in MIDI-FIFO (0-128) */
#define HDSPM_midiStatusOut0  384	
#define HDSPM_midiStatusOut1  388	
#define HDSPM_midiStatusIn0   392	
#define HDSPM_midiStatusIn1   396	


/* the meters are regular i/o-mapped registers, but offset
   considerably from the rest. the peak registers are reset
   when read; the least-significant 4 bits are full-scale counters; 
   the actual peak value is in the most-significant 24 bits.
*/
#define HDSPM_MADI_peakrmsbase 	4096	/* 4096-8191 2x64x32Bit Meters */

/* --- 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} */

#define HDSPM_ClockModeMaster      (1<<4) /* 1=Master, 0=Slave/Autosync */

#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 已提交
153
#define HDSPM_QuadSpeed   (1<<31) /* quad speed bit */
T
Takashi Iwai 已提交
154

R
Remy Bruno 已提交
155
#define HDSPM_Professional (1<<9) /* Professional */ /* AES32 ONLY */
T
Takashi Iwai 已提交
156
#define HDSPM_TX_64ch     (1<<10) /* Output 64channel MODE=1,
R
Remy Bruno 已提交
157 158
				     56channelMODE=0 */ /* MADI ONLY*/
#define HDSPM_Emphasis    (1<<10) /* Emphasis */ /* AES32 ONLY */
T
Takashi Iwai 已提交
159 160

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

T
Takashi Iwai 已提交
164 165 166
#define HDSPM_InputSelect0 (1<<14) /* Input select 0= optical, 1=coax
				    * -- MADI ONLY
				    */
T
Takashi Iwai 已提交
167 168 169
#define HDSPM_InputSelect1 (1<<15) /* should be 0 */

#define HDSPM_SyncRef0     (1<<16) /* 0=WOrd, 1=MADI */
R
Remy Bruno 已提交
170 171 172
#define HDSPM_SyncRef1     (1<<17) /* for AES32: SyncRefN codes the AES # */
#define HDSPM_SyncRef2     (1<<13)
#define HDSPM_SyncRef3     (1<<25)
T
Takashi Iwai 已提交
173

R
Remy Bruno 已提交
174
#define HDSPM_SMUX         (1<<18) /* Frame ??? */ /* MADI ONY */
T
Takashi Iwai 已提交
175 176 177
#define HDSPM_clr_tms      (1<<19) /* clear track marker, do not use 
                                      AES additional bits in
				      lower 5 Audiodatabits ??? */
R
Remy Bruno 已提交
178 179
#define HDSPM_taxi_reset   (1<<20) /* ??? */ /* MADI ONLY ? */
#define HDSPM_WCK48        (1<<20) /* Frame ??? = HDSPM_SMUX */ /* AES32 ONLY */
T
Takashi Iwai 已提交
180 181 182 183 184 185

#define HDSPM_Midi0InterruptEnable (1<<22)
#define HDSPM_Midi1InterruptEnable (1<<23)

#define HDSPM_LineOut (1<<24) /* Analog Out on channel 63/64 on=1, mute=0 */

R
Remy Bruno 已提交
186 187 188 189 190
#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 已提交
191 192 193

/* --- bit helper defines */
#define HDSPM_LatencyMask    (HDSPM_Latency0|HDSPM_Latency1|HDSPM_Latency2)
T
Takashi Iwai 已提交
194 195
#define HDSPM_FrequencyMask  (HDSPM_Frequency0|HDSPM_Frequency1|\
			      HDSPM_DoubleSpeed|HDSPM_QuadSpeed)
T
Takashi Iwai 已提交
196 197 198
#define HDSPM_InputMask      (HDSPM_InputSelect0|HDSPM_InputSelect1)
#define HDSPM_InputOptical   0
#define HDSPM_InputCoaxial   (HDSPM_InputSelect0)
T
Takashi Iwai 已提交
199 200
#define HDSPM_SyncRefMask    (HDSPM_SyncRef0|HDSPM_SyncRef1|\
			      HDSPM_SyncRef2|HDSPM_SyncRef3)
T
Takashi Iwai 已提交
201 202 203 204 205 206 207 208 209 210 211
#define HDSPM_SyncRef_Word   0
#define HDSPM_SyncRef_MADI   (HDSPM_SyncRef0)

#define HDSPM_SYNC_FROM_WORD 0	/* Preferred sync reference */
#define HDSPM_SYNC_FROM_MADI 1	/* choices - used by "pref_sync_ref" */

#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 已提交
212 213
#define HDSPM_Frequency96KHz   (HDSPM_DoubleSpeed|HDSPM_Frequency1|\
				HDSPM_Frequency0)
R
Remy Bruno 已提交
214 215
#define HDSPM_Frequency128KHz   (HDSPM_QuadSpeed|HDSPM_Frequency0)
#define HDSPM_Frequency176_4KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1)
T
Takashi Iwai 已提交
216 217
#define HDSPM_Frequency192KHz   (HDSPM_QuadSpeed|HDSPM_Frequency1|\
				 HDSPM_Frequency0)
T
Takashi Iwai 已提交
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 262 263

/* --- for internal discrimination */
#define HDSPM_CLOCK_SOURCE_AUTOSYNC          0	/* Sample Clock Sources */
#define HDSPM_CLOCK_SOURCE_INTERNAL_32KHZ    1
#define HDSPM_CLOCK_SOURCE_INTERNAL_44_1KHZ  2
#define HDSPM_CLOCK_SOURCE_INTERNAL_48KHZ    3
#define HDSPM_CLOCK_SOURCE_INTERNAL_64KHZ    4
#define HDSPM_CLOCK_SOURCE_INTERNAL_88_2KHZ  5
#define HDSPM_CLOCK_SOURCE_INTERNAL_96KHZ    6
#define HDSPM_CLOCK_SOURCE_INTERNAL_128KHZ   7
#define HDSPM_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
#define HDSPM_CLOCK_SOURCE_INTERNAL_192KHZ   9

/* 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
#define HDSPM_AUTOSYNC_FROM_NONE      2

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

#define hdspm_encode_latency(x)       (((x)<<1) & HDSPM_LatencyMask)
#define hdspm_decode_latency(x)       (((x) & HDSPM_LatencyMask)>>1)

#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 已提交
264
/* --- Status Register bits --- */ /* MADI ONLY */ /* Bits defined here and
T
Takashi Iwai 已提交
265 266 267
     that do not conflict with specific bits for AES32 seem to be valid also
     for the AES32
 */
T
Takashi Iwai 已提交
268
#define HDSPM_audioIRQPending    (1<<0)	/* IRQ is high and pending */
T
Takashi Iwai 已提交
269 270 271 272
#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)
					 */
T
Takashi Iwai 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286
#define HDSPM_madiLock           (1<<3)	/* MADI Locked =1, no=0 */

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

#define HDSPM_madiSync          (1<<18) /* MADI is in sync */
#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 已提交
287 288 289
#define HDSPM_BufferID          (1<<26)	/* (Double)Buffer ID toggles with
					 * Interrupt
					 */
T
Takashi Iwai 已提交
290 291 292 293
#define HDSPM_midi0IRQPending   (1<<30)	/* MIDI IRQ is pending  */
#define HDSPM_midi1IRQPending   (1<<31)	/* and aktiv */

/* --- status bit helpers */
T
Takashi Iwai 已提交
294 295
#define HDSPM_madiFreqMask  (HDSPM_madiFreq0|HDSPM_madiFreq1|\
			     HDSPM_madiFreq2|HDSPM_madiFreq3)
T
Takashi Iwai 已提交
296 297 298 299 300 301 302 303 304 305
#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 已提交
306
/* Status2 Register bits */ /* MADI ONLY */
T
Takashi Iwai 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

#define HDSPM_version0 (1<<0)	/* not realy defined but I guess */
#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, */
#define HDSPM_wc_freq2 (1<<7)	/* 100=64, 101=88.2, 110=96, */
/* missing Bit   for               111=128, 1000=176.4, 1001=192 */

#define HDSPM_SelSyncRef0 (1<<8)	/* Sync Source in slave mode */
#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)

#define HDSPM_wcFreqMask  (HDSPM_wc_freq0|HDSPM_wc_freq1|HDSPM_wc_freq2)
#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)


T
Takashi Iwai 已提交
335 336
#define HDSPM_SelSyncRefMask       (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
				    HDSPM_SelSyncRef2)
T
Takashi Iwai 已提交
337 338
#define HDSPM_SelSyncRef_WORD      0
#define HDSPM_SelSyncRef_MADI      (HDSPM_SelSyncRef0)
T
Takashi Iwai 已提交
339 340
#define HDSPM_SelSyncRef_NVALID    (HDSPM_SelSyncRef0|HDSPM_SelSyncRef1|\
				    HDSPM_SelSyncRef2)
T
Takashi Iwai 已提交
341

R
Remy Bruno 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
/*
   For AES32, bits for status, status2 and timecode are different
*/
/* status */
#define HDSPM_AES32_wcLock	0x0200000
#define HDSPM_AES32_wcFreq_bit  22
/* (status >> HDSPM_AES32_wcFreq_bit) & 0xF gives WC frequency (cf function 
  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
R
Remy Bruno 已提交
362
#define HDSPM_AES32_AUTOSYNC_FROM_NONE 9
R
Remy Bruno 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

/*  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 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
/* 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

/* 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
   also the latency to use. 
   for one direction !!!
*/
410
#define HDSPM_DMA_AREA_BYTES (HDSPM_MAX_CHANNELS * HDSPM_CHANNEL_BUFFER_BYTES)
T
Takashi Iwai 已提交
411 412
#define HDSPM_DMA_AREA_KILOBYTES (HDSPM_DMA_AREA_BYTES/1024)

R
Remy Bruno 已提交
413 414 415
/* revisions >= 230 indicate AES32 card */
#define HDSPM_AESREVISION 230

R
Remy Bruno 已提交
416 417 418 419 420 421 422
/* speed factor modes */
#define HDSPM_SPEED_SINGLE 0
#define HDSPM_SPEED_DOUBLE 1
#define HDSPM_SPEED_QUAD   2
/* names for speed modes */
static char *hdspm_speed_names[] = { "single", "double", "quad" };

423 424
struct hdspm_midi {
	struct hdspm *hdspm;
T
Takashi Iwai 已提交
425
	int id;
426 427 428
	struct snd_rawmidi *rmidi;
	struct snd_rawmidi_substream *input;
	struct snd_rawmidi_substream *output;
T
Takashi Iwai 已提交
429 430 431 432 433 434
	char istimer;		/* timer in use */
	struct timer_list timer;
	spinlock_t lock;
	int pending;
};

435
struct hdspm {
T
Takashi Iwai 已提交
436
        spinlock_t lock;
T
Takashi Iwai 已提交
437 438 439
	/* only one playback and/or capture stream */
        struct snd_pcm_substream *capture_substream;
        struct snd_pcm_substream *playback_substream;
T
Takashi Iwai 已提交
440 441

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

	unsigned char is_aes32;    /* indicates if card is AES32 */
T
Takashi Iwai 已提交
445 446 447 448 449 450 451

	int precise_ptr;	/* use precise pointers, to be tested */
	int monitor_outs;	/* set up monitoring outs init flag */

	u32 control_register;	/* cached value */
	u32 control2_register;	/* cached value */

452
	struct hdspm_midi midi[2];
T
Takashi Iwai 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	struct tasklet_struct midi_tasklet;

	size_t period_bytes;
	unsigned char ss_channels;	/* channels of card in single speed */
	unsigned char ds_channels;	/* Double Speed */
	unsigned char qs_channels;	/* Quad Speed */

	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;

	char *channel_map;	/* channel map for DS and Quadspeed */

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

	int irq_count;		/* for debug */

480 481 482
	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 已提交
483 484 485
	struct pci_dev *pci;	/* and an pci info */

	/* Mixer vars */
T
Takashi Iwai 已提交
486 487 488 489 490 491
	/* 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];
	/* full mixer accessable over mixer ioctl or hwdep-device */
	struct hdspm_mixer *mixer;
T
Takashi Iwai 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

};

/* 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
   refer 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_madi_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
};


515
static struct pci_device_id snd_hdspm_ids[] __devinitdata = {
T
Takashi Iwai 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529
	{
	 .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 */
530 531 532 533 534 535 536 537 538
static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
						   struct hdspm * hdspm);
static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
					  struct hdspm * hdspm);

static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm);
static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm);
static int hdspm_autosync_ref(struct hdspm * hdspm);
static int snd_hdspm_set_defaults(struct hdspm * hdspm);
539 540
static void hdspm_set_sgbuf(struct hdspm * hdspm,
			    struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
541 542
			     unsigned int reg, int channels);

R
Remy Bruno 已提交
543 544
static inline int HDSPM_bit2freq(int n)
{
545 546
	static const int bit2freq_tab[] = {
		0, 32000, 44100, 48000, 64000, 88200,
R
Remy Bruno 已提交
547 548 549 550 551 552
		96000, 128000, 176400, 192000 };
	if (n < 1 || n > 9)
		return 0;
	return bit2freq_tab[n];
}

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

556
static inline void hdspm_write(struct hdspm * hdspm, unsigned int reg,
T
Takashi Iwai 已提交
557 558 559 560 561
			       unsigned int val)
{
	writel(val, hdspm->iobase + reg);
}

562
static inline unsigned int hdspm_read(struct hdspm * hdspm, unsigned int reg)
T
Takashi Iwai 已提交
563 564 565 566 567 568 569 570
{
	return readl(hdspm->iobase + reg);
}

/* 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 
   each fader is a u32, but uses only the first 16 bit */

571
static inline int hdspm_read_in_gain(struct hdspm * hdspm, unsigned int chan,
T
Takashi Iwai 已提交
572 573
				     unsigned int in)
{
574
	if (chan >= HDSPM_MIXER_CHANNELS || in >= HDSPM_MIXER_CHANNELS)
T
Takashi Iwai 已提交
575 576 577 578 579
		return 0;

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

580
static inline int hdspm_read_pb_gain(struct hdspm * hdspm, unsigned int chan,
T
Takashi Iwai 已提交
581 582
				     unsigned int pb)
{
583
	if (chan >= HDSPM_MIXER_CHANNELS || pb >= HDSPM_MIXER_CHANNELS)
T
Takashi Iwai 已提交
584 585 586 587
		return 0;
	return hdspm->mixer->ch[chan].pb[pb];
}

588
static int hdspm_write_in_gain(struct hdspm *hdspm, unsigned int chan,
T
Takashi Iwai 已提交
589 590 591 592 593 594 595 596 597 598 599 600
				      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;
}

601
static int hdspm_write_pb_gain(struct hdspm *hdspm, unsigned int chan,
T
Takashi Iwai 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615
				      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 */
616
static inline void snd_hdspm_enable_in(struct hdspm * hdspm, int i, int v)
T
Takashi Iwai 已提交
617 618 619 620
{
	hdspm_write(hdspm, HDSPM_inputEnableBase + (4 * i), v);
}

621
static inline void snd_hdspm_enable_out(struct hdspm * hdspm, int i, int v)
T
Takashi Iwai 已提交
622 623 624 625 626
{
	hdspm_write(hdspm, HDSPM_outputEnableBase + (4 * i), v);
}

/* check if same process is writing and reading */
627
static int snd_hdspm_use_is_exclusive(struct hdspm *hdspm)
T
Takashi Iwai 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
	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;
}

/* check for external sample rate */
642
static int hdspm_external_sample_rate(struct hdspm *hdspm)
T
Takashi Iwai 已提交
643
{
R
Remy Bruno 已提交
644 645 646
	if (hdspm->is_aes32) {
		unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
		unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
T
Takashi Iwai 已提交
647 648
		unsigned int timecode =
			hdspm_read(hdspm, HDSPM_timecodeRegister);
R
Remy Bruno 已提交
649 650 651 652 653

		int syncref = hdspm_autosync_ref(hdspm);

		if (syncref == HDSPM_AES32_AUTOSYNC_FROM_WORD &&
				status & HDSPM_AES32_wcLock)
T
Takashi Iwai 已提交
654 655
			return HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit)
					      & 0xF);
R
Remy Bruno 已提交
656 657 658 659 660 661 662 663 664 665 666 667
		if (syncref >= HDSPM_AES32_AUTOSYNC_FROM_AES1 &&
			syncref <= HDSPM_AES32_AUTOSYNC_FROM_AES8 &&
			status2 & (HDSPM_LockAES >>
			          (syncref - HDSPM_AES32_AUTOSYNC_FROM_AES1)))
			return HDSPM_bit2freq((timecode >>
			  (4*(syncref-HDSPM_AES32_AUTOSYNC_FROM_AES1))) & 0xF);
		return 0;
	} else {
		unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
		unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
		unsigned int rate_bits;
		int rate = 0;
T
Takashi Iwai 已提交
668

R
Remy Bruno 已提交
669 670 671
		/* if wordclock has synced freq and wordclock is valid */
		if ((status2 & HDSPM_wcLock) != 0 &&
				(status & HDSPM_SelSyncRef0) == 0) {
T
Takashi Iwai 已提交
672

R
Remy Bruno 已提交
673
			rate_bits = status2 & HDSPM_wcFreqMask;
T
Takashi Iwai 已提交
674

R
Remy Bruno 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
			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;
				/* Quadspeed Bit missing ???? */
			default:
				rate = 0;
				break;
			}
T
Takashi Iwai 已提交
699 700
		}

T
Takashi Iwai 已提交
701 702 703
		/* if rate detected and Syncref is Word than have it,
		 * word has priority to MADI
		 */
R
Remy Bruno 已提交
704 705 706
		if (rate != 0 &&
	            (status2 & HDSPM_SelSyncRefMask) == HDSPM_SelSyncRef_WORD)
			return rate;
T
Takashi Iwai 已提交
707

R
Remy Bruno 已提交
708 709 710
		/* maby a madi input (which is taken if sel sync is madi) */
		if (status & HDSPM_madiLock) {
			rate_bits = status & HDSPM_madiFreqMask;
T
Takashi Iwai 已提交
711

R
Remy Bruno 已提交
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
			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;
			}
T
Takashi Iwai 已提交
744
		}
R
Remy Bruno 已提交
745
		return rate;
T
Takashi Iwai 已提交
746 747 748 749
	}
}

/* Latency function */
750
static inline void hdspm_compute_period_size(struct hdspm * hdspm)
T
Takashi Iwai 已提交
751 752 753 754 755
{
	hdspm->period_bytes =
	    1 << ((hdspm_decode_latency(hdspm->control_register) + 8));
}

756
static snd_pcm_uframes_t hdspm_hw_pointer(struct hdspm * hdspm)
T
Takashi Iwai 已提交
757 758 759 760 761
{
	int position;

	position = hdspm_read(hdspm, HDSPM_statusRegister);

T
Takashi Iwai 已提交
762 763 764
	if (!hdspm->precise_ptr)
		return (position & HDSPM_BufferID) ?
			(hdspm->period_bytes / 4) : 0;
T
Takashi Iwai 已提交
765

T
Takashi Iwai 已提交
766 767
	/* hwpointer comes in bytes and is 64Bytes accurate (by docu since
	   PCI Burst)
T
Takashi Iwai 已提交
768 769 770 771 772 773 774 775 776 777 778 779
	   i have experimented that it is at most 64 Byte to much for playing 
	   so substraction of 64 byte should be ok for ALSA, but use it only
	   for application where you know what you do since if you come to
	   near with record pointer it can be a disaster */

	position &= HDSPM_BufferPositionMask;
	position = ((position - 64) % (2 * hdspm->period_bytes)) / 4;

	return position;
}


780
static inline void hdspm_start_audio(struct hdspm * s)
T
Takashi Iwai 已提交
781 782 783 784 785
{
	s->control_register |= (HDSPM_AudioInterruptEnable | HDSPM_Start);
	hdspm_write(s, HDSPM_controlRegister, s->control_register);
}

786
static inline void hdspm_stop_audio(struct hdspm * s)
T
Takashi Iwai 已提交
787 788 789 790 791 792
{
	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*/
793
static void hdspm_silence_playback(struct hdspm *hdspm)
T
Takashi Iwai 已提交
794 795 796 797 798
{
	int i;
	int n = hdspm->period_bytes;
	void *buf = hdspm->playback_buffer;

R
Remy Bruno 已提交
799 800
	if (buf == NULL)
		return;
T
Takashi Iwai 已提交
801 802 803 804 805 806 807

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

808
static int hdspm_set_interrupt_interval(struct hdspm * s, unsigned int frames)
T
Takashi Iwai 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
{
	int n;

	spin_lock_irq(&s->lock);

	frames >>= 7;
	n = 0;
	while (frames) {
		n++;
		frames >>= 1;
	}
	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;
}

832 833 834 835 836 837 838 839 840 841
static void hdspm_set_dds_value(struct hdspm *hdspm, int rate)
{
	u64 n;
	
	if (rate >= 112000)
		rate /= 4;
	else if (rate >= 56000)
		rate /= 2;

	/* RME says n = 104857600000000, but in the windows MADI driver, I see:
R
Remy Bruno 已提交
842
//	return 104857600000000 / rate; // 100 MHz
843 844
	return 110100480000000 / rate; // 105 MHz
        */	   
T
Takashi Iwai 已提交
845
	/* n = 104857600000000ULL; */ /*  =  2^20 * 10^8 */
846
	n = 110100480000000ULL;    /* Value checked for AES32 and MADI */
847
	n = div_u64(n, rate);
848
	/* n should be less than 2^32 for being written to FREQ register */
849
	snd_BUG_ON(n >> 32);
850 851
	hdspm_write(hdspm, HDSPM_freqReg, (u32)n);
}
T
Takashi Iwai 已提交
852 853

/* dummy set rate lets see what happens */
854
static int hdspm_set_rate(struct hdspm * hdspm, int rate, int called_internally)
T
Takashi Iwai 已提交
855 856 857 858
{
	int current_rate;
	int rate_bits;
	int not_set = 0;
R
Remy Bruno 已提交
859
	int current_speed, target_speed;
T
Takashi Iwai 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873

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

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

	        /* SLAVE --- */ 
		if (called_internally) {

        	  /* request from ctl or card initialization 
	             just make a warning an remember setting 
		     for future master mode switching */
    
T
Takashi Iwai 已提交
874 875 876
			snd_printk(KERN_WARNING "HDSPM: "
				   "Warning: device is not running "
				   "as a clock master.\n");
T
Takashi Iwai 已提交
877 878 879 880 881 882 883
			not_set = 1;
		} else {

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

T
Takashi Iwai 已提交
884 885
			if (hdspm_autosync_ref(hdspm) ==
			    HDSPM_AUTOSYNC_FROM_NONE) {
T
Takashi Iwai 已提交
886

T
Takashi Iwai 已提交
887 888
				snd_printk(KERN_WARNING "HDSPM: "
					   "Detected no Externel Sync \n");
T
Takashi Iwai 已提交
889 890 891 892
				not_set = 1;

			} else if (rate != external_freq) {

T
Takashi Iwai 已提交
893 894 895
				snd_printk(KERN_WARNING "HDSPM: "
					   "Warning: No AutoSync source for "
					   "requested rate\n");
T
Takashi Iwai 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
				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
	   changes in the read/write routines.  
	 */

R
Remy Bruno 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925
	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 已提交
926

T
Takashi Iwai 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	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 已提交
946 947 948 949 950 951 952 953 954
	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 已提交
955 956 957 958
	default:
		return -EINVAL;
	}

R
Remy Bruno 已提交
959
	if (current_speed != target_speed
T
Takashi Iwai 已提交
960 961
	    && (hdspm->capture_pid >= 0 || hdspm->playback_pid >= 0)) {
		snd_printk
T
Takashi Iwai 已提交
962
		    (KERN_ERR "HDSPM: "
R
Remy Bruno 已提交
963
		     "cannot change from %s speed to %s speed mode "
T
Takashi Iwai 已提交
964
		     "(capture PID = %d, playback PID = %d)\n",
R
Remy Bruno 已提交
965 966
		     hdspm_speed_names[current_speed],
		     hdspm_speed_names[target_speed],
T
Takashi Iwai 已提交
967 968 969 970 971 972 973 974
		     hdspm->capture_pid, hdspm->playback_pid);
		return -EBUSY;
	}

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

975 976 977 978 979 980 981 982 983 984
	/* For AES32, need to set DDS value in FREQ register
	   For MADI, also apparently */
	hdspm_set_dds_value(hdspm, rate);
	
	if (hdspm->is_aes32 && rate != current_rate)
		hdspm_write(hdspm, HDSPM_eeprom_wr, 0);
	
	/* For AES32 and for MADI (at least rev 204), channel_map needs to
	 * always be channel_map_madi_ss, whatever the sample rate */
	hdspm->channel_map = channel_map_madi_ss;
T
Takashi Iwai 已提交
985 986 987 988 989 990 991 992 993 994

	hdspm->system_sample_rate = rate;

	if (not_set != 0)
		return -1;

	return 0;
}

/* mainly for init to 0 on load */
995
static void all_in_all_mixer(struct hdspm * hdspm, int sgain)
T
Takashi Iwai 已提交
996 997
{
	int i, j;
T
Takashi Iwai 已提交
998 999 1000 1001 1002 1003 1004 1005
	unsigned int gain;

	if (sgain > UNITY_GAIN)
		gain = UNITY_GAIN;
	else if (sgain < 0)
		gain = 0;
	else
		gain = sgain;
T
Takashi Iwai 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

	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 已提交
1018 1019
static inline unsigned char snd_hdspm_midi_read_byte (struct hdspm *hdspm,
						      int id)
T
Takashi Iwai 已提交
1020 1021 1022 1023 1024 1025 1026 1027
{
	/* the hardware already does the relevant bit-mask with 0xff */
	if (id)
		return hdspm_read(hdspm, HDSPM_midiDataIn1);
	else
		return hdspm_read(hdspm, HDSPM_midiDataIn0);
}

T
Takashi Iwai 已提交
1028 1029
static inline void snd_hdspm_midi_write_byte (struct hdspm *hdspm, int id,
					      int val)
T
Takashi Iwai 已提交
1030 1031 1032
{
	/* the hardware already does the relevant bit-mask with 0xff */
	if (id)
1033
		hdspm_write(hdspm, HDSPM_midiDataOut1, val);
T
Takashi Iwai 已提交
1034
	else
1035
		hdspm_write(hdspm, HDSPM_midiDataOut0, val);
T
Takashi Iwai 已提交
1036 1037
}

1038
static inline int snd_hdspm_midi_input_available (struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1039 1040 1041 1042 1043 1044 1045
{
	if (id)
		return (hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xff);
	else
		return (hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xff);
}

1046
static inline int snd_hdspm_midi_output_possible (struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1047 1048 1049 1050
{
	int fifo_bytes_used;

	if (id)
T
Takashi Iwai 已提交
1051
		fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut1);
T
Takashi Iwai 已提交
1052
	else
T
Takashi Iwai 已提交
1053 1054
		fifo_bytes_used = hdspm_read(hdspm, HDSPM_midiStatusOut0);
	fifo_bytes_used &= 0xff;
T
Takashi Iwai 已提交
1055 1056 1057 1058 1059 1060 1061

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

1062
static void snd_hdspm_flush_midi_input(struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1063 1064 1065 1066 1067
{
	while (snd_hdspm_midi_input_available (hdspm, id))
		snd_hdspm_midi_read_byte (hdspm, id);
}

1068
static int snd_hdspm_midi_output_write (struct hdspm_midi *hmidi)
T
Takashi Iwai 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
{
	unsigned long flags;
	int n_pending;
	int to_write;
	int i;
	unsigned char buf[128];

	/* Output is not interrupt driven */
		
	spin_lock_irqsave (&hmidi->lock, flags);
T
Takashi Iwai 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	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);
		
			to_write = snd_rawmidi_transmit (hmidi->output, buf,
							 n_pending);
			if (to_write > 0) {
				for (i = 0; i < to_write; ++i) 
					snd_hdspm_midi_write_byte (hmidi->hdspm,
								   hmidi->id,
								   buf[i]);
T
Takashi Iwai 已提交
1094 1095 1096 1097 1098 1099 1100
			}
		}
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return 0;
}

1101
static int snd_hdspm_midi_input_read (struct hdspm_midi *hmidi)
T
Takashi Iwai 已提交
1102
{
T
Takashi Iwai 已提交
1103 1104 1105
	unsigned char buf[128]; /* this buffer is designed to match the MIDI
				 * input FIFO size
				 */
T
Takashi Iwai 已提交
1106 1107 1108 1109 1110
	unsigned long flags;
	int n_pending;
	int i;

	spin_lock_irqsave (&hmidi->lock, flags);
T
Takashi Iwai 已提交
1111 1112
	n_pending = snd_hdspm_midi_input_available (hmidi->hdspm, hmidi->id);
	if (n_pending > 0) {
T
Takashi Iwai 已提交
1113
		if (hmidi->input) {
T
Takashi Iwai 已提交
1114
			if (n_pending > (int)sizeof (buf))
T
Takashi Iwai 已提交
1115
				n_pending = sizeof (buf);
T
Takashi Iwai 已提交
1116 1117 1118 1119 1120 1121
			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 已提交
1122 1123
		} else {
			/* flush the MIDI input FIFO */
T
Takashi Iwai 已提交
1124 1125 1126
			while (n_pending--)
				snd_hdspm_midi_read_byte (hmidi->hdspm,
							  hmidi->id);
T
Takashi Iwai 已提交
1127 1128 1129
		}
	}
	hmidi->pending = 0;
T
Takashi Iwai 已提交
1130
	if (hmidi->id)
T
Takashi Iwai 已提交
1131
		hmidi->hdspm->control_register |= HDSPM_Midi1InterruptEnable;
T
Takashi Iwai 已提交
1132
	else
T
Takashi Iwai 已提交
1133
		hmidi->hdspm->control_register |= HDSPM_Midi0InterruptEnable;
T
Takashi Iwai 已提交
1134 1135
	hdspm_write(hmidi->hdspm, HDSPM_controlRegister,
		    hmidi->hdspm->control_register);
T
Takashi Iwai 已提交
1136 1137 1138 1139
	spin_unlock_irqrestore (&hmidi->lock, flags);
	return snd_hdspm_midi_output_write (hmidi);
}

T
Takashi Iwai 已提交
1140 1141
static void
snd_hdspm_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
T
Takashi Iwai 已提交
1142
{
1143 1144
	struct hdspm *hdspm;
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1145 1146 1147
	unsigned long flags;
	u32 ie;

T
Takashi Iwai 已提交
1148
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1149
	hdspm = hmidi->hdspm;
T
Takashi Iwai 已提交
1150 1151
	ie = hmidi->id ?
		HDSPM_Midi1InterruptEnable : HDSPM_Midi0InterruptEnable;
T
Takashi Iwai 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	spin_lock_irqsave (&hdspm->lock, flags);
	if (up) {
		if (!(hdspm->control_register & ie)) {
			snd_hdspm_flush_midi_input (hdspm, hmidi->id);
			hdspm->control_register |= ie;
		}
	} else {
		hdspm->control_register &= ~ie;
	}

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

static void snd_hdspm_midi_output_timer(unsigned long data)
{
1168
	struct hdspm_midi *hmidi = (struct hdspm_midi *) data;
T
Takashi Iwai 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	unsigned long flags;
	
	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
	   leaving istimer wherever it was set before.  
	*/

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

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

T
Takashi Iwai 已提交
1188 1189
static void
snd_hdspm_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
T
Takashi Iwai 已提交
1190
{
1191
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1192 1193
	unsigned long flags;

T
Takashi Iwai 已提交
1194
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	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 已提交
1206
		if (hmidi->istimer && --hmidi->istimer <= 0)
T
Takashi Iwai 已提交
1207 1208 1209 1210 1211 1212 1213
			del_timer (&hmidi->timer);
	}
	spin_unlock_irqrestore (&hmidi->lock, flags);
	if (up)
		snd_hdspm_midi_output_write(hmidi);
}

1214
static int snd_hdspm_midi_input_open(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
1215
{
1216
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1217

T
Takashi Iwai 已提交
1218
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1219 1220 1221 1222 1223 1224 1225 1226
	spin_lock_irq (&hmidi->lock);
	snd_hdspm_flush_midi_input (hmidi->hdspm, hmidi->id);
	hmidi->input = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1227
static int snd_hdspm_midi_output_open(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
1228
{
1229
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1230

T
Takashi Iwai 已提交
1231
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1232 1233 1234 1235 1236 1237 1238
	spin_lock_irq (&hmidi->lock);
	hmidi->output = substream;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1239
static int snd_hdspm_midi_input_close(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
1240
{
1241
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1242 1243 1244

	snd_hdspm_midi_input_trigger (substream, 0);

T
Takashi Iwai 已提交
1245
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1246 1247 1248 1249 1250 1251 1252
	spin_lock_irq (&hmidi->lock);
	hmidi->input = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1253
static int snd_hdspm_midi_output_close(struct snd_rawmidi_substream *substream)
T
Takashi Iwai 已提交
1254
{
1255
	struct hdspm_midi *hmidi;
T
Takashi Iwai 已提交
1256 1257 1258

	snd_hdspm_midi_output_trigger (substream, 0);

T
Takashi Iwai 已提交
1259
	hmidi = substream->rmidi->private_data;
T
Takashi Iwai 已提交
1260 1261 1262 1263 1264 1265 1266
	spin_lock_irq (&hmidi->lock);
	hmidi->output = NULL;
	spin_unlock_irq (&hmidi->lock);

	return 0;
}

1267
static struct snd_rawmidi_ops snd_hdspm_midi_output =
T
Takashi Iwai 已提交
1268 1269 1270 1271 1272 1273
{
	.open =		snd_hdspm_midi_output_open,
	.close =	snd_hdspm_midi_output_close,
	.trigger =	snd_hdspm_midi_output_trigger,
};

1274
static struct snd_rawmidi_ops snd_hdspm_midi_input =
T
Takashi Iwai 已提交
1275 1276 1277 1278 1279 1280
{
	.open =		snd_hdspm_midi_input_open,
	.close =	snd_hdspm_midi_input_close,
	.trigger =	snd_hdspm_midi_input_trigger,
};

T
Takashi Iwai 已提交
1281 1282
static int __devinit snd_hdspm_create_midi (struct snd_card *card,
					    struct hdspm *hdspm, int id)
T
Takashi Iwai 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291
{
	int err;
	char buf[32];

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

	sprintf (buf, "%s MIDI %d", card->shortname, id+1);
T
Takashi Iwai 已提交
1292 1293
	err = snd_rawmidi_new (card, buf, id, 1, 1, &hdspm->midi[id].rmidi);
	if (err < 0)
T
Takashi Iwai 已提交
1294 1295
		return err;

1296
	sprintf(hdspm->midi[id].rmidi->name, "HDSPM MIDI %d", id+1);
T
Takashi Iwai 已提交
1297 1298
	hdspm->midi[id].rmidi->private_data = &hdspm->midi[id];

T
Takashi Iwai 已提交
1299 1300 1301 1302
	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);
T
Takashi Iwai 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

	hdspm->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
		SNDRV_RAWMIDI_INFO_INPUT |
		SNDRV_RAWMIDI_INFO_DUPLEX;

	return 0;
}


static void hdspm_midi_tasklet(unsigned long arg)
{
1314
	struct hdspm *hdspm = (struct hdspm *)arg;
T
Takashi Iwai 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	
	if (hdspm->midi[0].pending)
		snd_hdspm_midi_input_read (&hdspm->midi[0]);
	if (hdspm->midi[1].pending)
		snd_hdspm_midi_input_read (&hdspm->midi[1]);
} 


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

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

#define HDSPM_SYSTEM_SAMPLE_RATE(xname, xindex) \
1330
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1331 1332 1333 1334 1335 1336 1337
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdspm_info_system_sample_rate, \
  .get = snd_hdspm_get_system_sample_rate \
}

1338 1339
static int snd_hdspm_info_system_sample_rate(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1340 1341 1342 1343 1344 1345
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	return 0;
}

1346 1347
static int snd_hdspm_get_system_sample_rate(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_value *
T
Takashi Iwai 已提交
1348 1349
					    ucontrol)
{
1350
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1351 1352 1353 1354 1355 1356

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

#define HDSPM_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
1357
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1358 1359 1360 1361 1362 1363 1364
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdspm_info_autosync_sample_rate, \
  .get = snd_hdspm_get_autosync_sample_rate \
}

1365 1366
static int snd_hdspm_info_autosync_sample_rate(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
{
	static char *texts[] = { "32000", "44100", "48000",
		"64000", "88200", "96000",
		"128000", "176400", "192000",
		"None"
	};
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 10;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);
	return 0;
}

1384 1385
static int snd_hdspm_get_autosync_sample_rate(struct snd_kcontrol *kcontrol,
					      struct snd_ctl_elem_value *
T
Takashi Iwai 已提交
1386 1387
					      ucontrol)
{
1388
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1389 1390 1391 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 1424 1425

	switch (hdspm_external_sample_rate(hdspm)) {
	case 32000:
		ucontrol->value.enumerated.item[0] = 0;
		break;
	case 44100:
		ucontrol->value.enumerated.item[0] = 1;
		break;
	case 48000:
		ucontrol->value.enumerated.item[0] = 2;
		break;
	case 64000:
		ucontrol->value.enumerated.item[0] = 3;
		break;
	case 88200:
		ucontrol->value.enumerated.item[0] = 4;
		break;
	case 96000:
		ucontrol->value.enumerated.item[0] = 5;
		break;
	case 128000:
		ucontrol->value.enumerated.item[0] = 6;
		break;
	case 176400:
		ucontrol->value.enumerated.item[0] = 7;
		break;
	case 192000:
		ucontrol->value.enumerated.item[0] = 8;
		break;

	default:
		ucontrol->value.enumerated.item[0] = 9;
	}
	return 0;
}

#define HDSPM_SYSTEM_CLOCK_MODE(xname, xindex) \
1426
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdspm_info_system_clock_mode, \
  .get = snd_hdspm_get_system_clock_mode, \
}



1436
static int hdspm_system_clock_mode(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1437 1438 1439 1440 1441 1442 1443 1444
{
        /* Always reflect the hardware info, rme is never wrong !!!! */

	if (hdspm->control_register & HDSPM_ClockModeMaster)
		return 0;
	return 1;
}

1445 1446
static int snd_hdspm_info_system_clock_mode(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
{
	static char *texts[] = { "Master", "Slave" };

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);
	return 0;
}

1461 1462
static int snd_hdspm_get_system_clock_mode(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1463
{
1464
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1465 1466 1467 1468 1469 1470 1471

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

#define HDSPM_CLOCK_SOURCE(xname, xindex) \
1472
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1473 1474 1475 1476 1477 1478 1479
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_clock_source, \
  .get = snd_hdspm_get_clock_source, \
  .put = snd_hdspm_put_clock_source \
}

1480
static int hdspm_clock_source(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
{
	if (hdspm->control_register & HDSPM_ClockModeMaster) {
		switch (hdspm->system_sample_rate) {
		case 32000:
			return 1;
		case 44100:
			return 2;
		case 48000:
			return 3;
		case 64000:
			return 4;
		case 88200:
			return 5;
		case 96000:
			return 6;
		case 128000:
			return 7;
		case 176400:
			return 8;
		case 192000:
			return 9;
		default:
			return 3;
		}
	} else {
		return 0;
	}
}

1510
static int hdspm_set_clock_source(struct hdspm * hdspm, int mode)
T
Takashi Iwai 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
{
	int rate;
	switch (mode) {

	case HDSPM_CLOCK_SOURCE_AUTOSYNC:
		if (hdspm_external_sample_rate(hdspm) != 0) {
			hdspm->control_register &= ~HDSPM_ClockModeMaster;
			hdspm_write(hdspm, HDSPM_controlRegister,
				    hdspm->control_register);
			return 0;
		}
		return -1;
	case HDSPM_CLOCK_SOURCE_INTERNAL_32KHZ:
		rate = 32000;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_44_1KHZ:
		rate = 44100;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_48KHZ:
		rate = 48000;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_64KHZ:
		rate = 64000;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_88_2KHZ:
		rate = 88200;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_96KHZ:
		rate = 96000;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_128KHZ:
		rate = 128000;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_176_4KHZ:
		rate = 176400;
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_192KHZ:
		rate = 192000;
		break;

	default:
		rate = 44100;
	}
	hdspm->control_register |= HDSPM_ClockModeMaster;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
	hdspm_set_rate(hdspm, rate, 1);
	return 0;
}

1560 1561
static int snd_hdspm_info_clock_source(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
{
	static char *texts[] = { "AutoSync",
		"Internal 32.0 kHz", "Internal 44.1 kHz",
		    "Internal 48.0 kHz",
		"Internal 64.0 kHz", "Internal 88.2 kHz",
		    "Internal 96.0 kHz",
		"Internal 128.0 kHz", "Internal 176.4 kHz",
		    "Internal 192.0 kHz"
	};

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 10;

	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;

	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);

	return 0;
}

1586 1587
static int snd_hdspm_get_clock_source(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1588
{
1589
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1590 1591 1592 1593 1594

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

1595 1596
static int snd_hdspm_put_clock_source(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1597
{
1598
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1599 1600 1601 1602 1603 1604 1605 1606
	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 已提交
1607 1608
	if (val > 9)
		val = 9;
T
Takashi Iwai 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	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;
}

#define HDSPM_PREF_SYNC_REF(xname, xindex) \
1619
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1620 1621 1622 1623 1624 1625 1626
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_pref_sync_ref, \
  .get = snd_hdspm_get_pref_sync_ref, \
  .put = snd_hdspm_put_pref_sync_ref \
}

1627
static int hdspm_pref_sync_ref(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1628 1629 1630 1631
{
	/* Notice that this looks at the requested sync source,
	   not the one actually in use.
	 */
R
Remy Bruno 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	if (hdspm->is_aes32) {
		switch (hdspm->control_register & HDSPM_SyncRefMask) {
		/* number gives AES index, except for 0 which
		   corresponds to WordClock */
		case 0: return 0;
		case HDSPM_SyncRef0: return 1;
		case HDSPM_SyncRef1: return 2;
		case HDSPM_SyncRef1+HDSPM_SyncRef0: return 3;
		case HDSPM_SyncRef2: return 4;
		case HDSPM_SyncRef2+HDSPM_SyncRef0: return 5;
		case HDSPM_SyncRef2+HDSPM_SyncRef1: return 6;
		case HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0: return 7;
		case HDSPM_SyncRef3: return 8;
		}
	} else {
		switch (hdspm->control_register & HDSPM_SyncRefMask) {
		case HDSPM_SyncRef_Word:
			return HDSPM_SYNC_FROM_WORD;
		case HDSPM_SyncRef_MADI:
			return HDSPM_SYNC_FROM_MADI;
		}
T
Takashi Iwai 已提交
1653 1654 1655 1656 1657
	}

	return HDSPM_SYNC_FROM_WORD;
}

1658
static int hdspm_set_pref_sync_ref(struct hdspm * hdspm, int pref)
T
Takashi Iwai 已提交
1659 1660 1661
{
	hdspm->control_register &= ~HDSPM_SyncRefMask;

R
Remy Bruno 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
	if (hdspm->is_aes32) {
		switch (pref) {
		case 0:
		       hdspm->control_register |= 0;
		       break;
		case 1:
		       hdspm->control_register |= HDSPM_SyncRef0;
		       break;
		case 2:
		       hdspm->control_register |= HDSPM_SyncRef1;
		       break;
		case 3:
		       hdspm->control_register |= HDSPM_SyncRef1+HDSPM_SyncRef0;
		       break;
		case 4:
		       hdspm->control_register |= HDSPM_SyncRef2;
		       break;
		case 5:
		       hdspm->control_register |= HDSPM_SyncRef2+HDSPM_SyncRef0;
		       break;
		case 6:
		       hdspm->control_register |= HDSPM_SyncRef2+HDSPM_SyncRef1;
		       break;
		case 7:
T
Takashi Iwai 已提交
1686 1687
		       hdspm->control_register |=
			       HDSPM_SyncRef2+HDSPM_SyncRef1+HDSPM_SyncRef0;
R
Remy Bruno 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
		       break;
		case 8:
		       hdspm->control_register |= HDSPM_SyncRef3;
		       break;
		default:
		       return -1;
		}
	} else {
		switch (pref) {
		case HDSPM_SYNC_FROM_MADI:
			hdspm->control_register |= HDSPM_SyncRef_MADI;
			break;
		case HDSPM_SYNC_FROM_WORD:
			hdspm->control_register |= HDSPM_SyncRef_Word;
			break;
		default:
			return -1;
		}
T
Takashi Iwai 已提交
1706 1707 1708 1709 1710
	}
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);
	return 0;
}

1711 1712
static int snd_hdspm_info_pref_sync_ref(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1713
{
R
Remy Bruno 已提交
1714
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1715

R
Remy Bruno 已提交
1716 1717 1718
	if (hdspm->is_aes32) {
		static char *texts[] = { "Word", "AES1", "AES2", "AES3",
			"AES4", "AES5",	"AES6", "AES7", "AES8" };
T
Takashi Iwai 已提交
1719

R
Remy Bruno 已提交
1720 1721
		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
		uinfo->count = 1;
T
Takashi Iwai 已提交
1722

R
Remy Bruno 已提交
1723 1724
		uinfo->value.enumerated.items = 9;

T
Takashi Iwai 已提交
1725 1726
		if (uinfo->value.enumerated.item >=
		    uinfo->value.enumerated.items)
R
Remy Bruno 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
			uinfo->value.enumerated.item =
				uinfo->value.enumerated.items - 1;
		strcpy(uinfo->value.enumerated.name,
				texts[uinfo->value.enumerated.item]);
	} else {
		static char *texts[] = { "Word", "MADI" };

		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
		uinfo->count = 1;

		uinfo->value.enumerated.items = 2;

T
Takashi Iwai 已提交
1739 1740
		if (uinfo->value.enumerated.item >=
		    uinfo->value.enumerated.items)
R
Remy Bruno 已提交
1741 1742 1743 1744 1745
			uinfo->value.enumerated.item =
				uinfo->value.enumerated.items - 1;
		strcpy(uinfo->value.enumerated.name,
				texts[uinfo->value.enumerated.item]);
	}
T
Takashi Iwai 已提交
1746 1747 1748
	return 0;
}

1749 1750
static int snd_hdspm_get_pref_sync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1751
{
1752
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1753 1754 1755 1756 1757

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

1758 1759
static int snd_hdspm_put_pref_sync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1760
{
1761
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1762 1763 1764
	int change, max;
	unsigned int val;

R
Remy Bruno 已提交
1765
	max = hdspm->is_aes32 ? 9 : 2;
T
Takashi Iwai 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779

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

	val = ucontrol->value.enumerated.item[0] % max;

	spin_lock_irq(&hdspm->lock);
	change = (int) val != hdspm_pref_sync_ref(hdspm);
	hdspm_set_pref_sync_ref(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_AUTOSYNC_REF(xname, xindex) \
1780
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1781 1782 1783 1784 1785 1786 1787
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  .info = snd_hdspm_info_autosync_ref, \
  .get = snd_hdspm_get_autosync_ref, \
}

1788
static int hdspm_autosync_ref(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1789
{
R
Remy Bruno 已提交
1790 1791
	if (hdspm->is_aes32) {
		unsigned int status = hdspm_read(hdspm, HDSPM_statusRegister);
T
Takashi Iwai 已提交
1792 1793
		unsigned int syncref = (status >> HDSPM_AES32_syncref_bit) &
			0xF;
R
Remy Bruno 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
		if (syncref == 0)
			return HDSPM_AES32_AUTOSYNC_FROM_WORD;
		if (syncref <= 8)
			return syncref;
		return HDSPM_AES32_AUTOSYNC_FROM_NONE;
	} else {
		/* This looks at the autosync selected sync reference */
		unsigned int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);

		switch (status2 & HDSPM_SelSyncRefMask) {
		case HDSPM_SelSyncRef_WORD:
			return HDSPM_AUTOSYNC_FROM_WORD;
		case HDSPM_SelSyncRef_MADI:
			return HDSPM_AUTOSYNC_FROM_MADI;
		case HDSPM_SelSyncRef_NVALID:
			return HDSPM_AUTOSYNC_FROM_NONE;
		default:
			return 0;
		}
T
Takashi Iwai 已提交
1813 1814 1815 1816 1817

		return 0;
	}
}

1818 1819
static int snd_hdspm_info_autosync_ref(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
1820
{
R
Remy Bruno 已提交
1821
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1822

R
Remy Bruno 已提交
1823 1824 1825 1826 1827 1828 1829
	if (hdspm->is_aes32) {
		static char *texts[] = { "WordClock", "AES1", "AES2", "AES3",
			"AES4",	"AES5", "AES6", "AES7", "AES8", "None"};

		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
		uinfo->count = 1;
		uinfo->value.enumerated.items = 10;
T
Takashi Iwai 已提交
1830 1831
		if (uinfo->value.enumerated.item >=
		    uinfo->value.enumerated.items)
R
Remy Bruno 已提交
1832 1833 1834 1835
			uinfo->value.enumerated.item =
				uinfo->value.enumerated.items - 1;
		strcpy(uinfo->value.enumerated.name,
				texts[uinfo->value.enumerated.item]);
T
Takashi Iwai 已提交
1836
	} else {
R
Remy Bruno 已提交
1837 1838 1839 1840 1841
		static char *texts[] = { "WordClock", "MADI", "None" };

		uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
		uinfo->count = 1;
		uinfo->value.enumerated.items = 3;
T
Takashi Iwai 已提交
1842 1843
		if (uinfo->value.enumerated.item >=
		    uinfo->value.enumerated.items)
R
Remy Bruno 已提交
1844 1845 1846 1847 1848
			uinfo->value.enumerated.item =
				uinfo->value.enumerated.items - 1;
		strcpy(uinfo->value.enumerated.name,
				texts[uinfo->value.enumerated.item]);
	}
T
Takashi Iwai 已提交
1849 1850 1851
	return 0;
}

1852 1853
static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1854
{
1855
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1856

R
Remy Bruno 已提交
1857
	ucontrol->value.enumerated.item[0] = hdspm_autosync_ref(hdspm);
T
Takashi Iwai 已提交
1858 1859 1860 1861
	return 0;
}

#define HDSPM_LINE_OUT(xname, xindex) \
1862
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1863 1864 1865 1866 1867 1868 1869
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_line_out, \
  .get = snd_hdspm_get_line_out, \
  .put = snd_hdspm_put_line_out \
}

1870
static int hdspm_line_out(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1871 1872 1873 1874 1875
{
	return (hdspm->control_register & HDSPM_LineOut) ? 1 : 0;
}


1876
static int hdspm_set_line_output(struct hdspm * hdspm, int out)
T
Takashi Iwai 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
{
	if (out)
		hdspm->control_register |= HDSPM_LineOut;
	else
		hdspm->control_register &= ~HDSPM_LineOut;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

1887
#define snd_hdspm_info_line_out		snd_ctl_boolean_mono_info
T
Takashi Iwai 已提交
1888

1889 1890
static int snd_hdspm_get_line_out(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1891
{
1892
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1893 1894 1895 1896 1897 1898 1899

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

1900 1901
static int snd_hdspm_put_line_out(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1902
{
1903
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
	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_line_out(hdspm);
	hdspm_set_line_output(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_TX_64(xname, xindex) \
1918
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1919 1920 1921 1922 1923 1924 1925
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_tx_64, \
  .get = snd_hdspm_get_tx_64, \
  .put = snd_hdspm_put_tx_64 \
}

1926
static int hdspm_tx_64(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1927 1928 1929 1930
{
	return (hdspm->control_register & HDSPM_TX_64ch) ? 1 : 0;
}

1931
static int hdspm_set_tx_64(struct hdspm * hdspm, int out)
T
Takashi Iwai 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
{
	if (out)
		hdspm->control_register |= HDSPM_TX_64ch;
	else
		hdspm->control_register &= ~HDSPM_TX_64ch;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

1942
#define snd_hdspm_info_tx_64		snd_ctl_boolean_mono_info
T
Takashi Iwai 已提交
1943

1944 1945
static int snd_hdspm_get_tx_64(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1946
{
1947
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1948 1949 1950 1951 1952 1953 1954

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

1955 1956
static int snd_hdspm_put_tx_64(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
1957
{
1958
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
	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_tx_64(hdspm);
	hdspm_set_tx_64(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_C_TMS(xname, xindex) \
1973
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
1974 1975 1976 1977 1978 1979 1980
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_c_tms, \
  .get = snd_hdspm_get_c_tms, \
  .put = snd_hdspm_put_c_tms \
}

1981
static int hdspm_c_tms(struct hdspm * hdspm)
T
Takashi Iwai 已提交
1982 1983 1984 1985
{
	return (hdspm->control_register & HDSPM_clr_tms) ? 1 : 0;
}

1986
static int hdspm_set_c_tms(struct hdspm * hdspm, int out)
T
Takashi Iwai 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
{
	if (out)
		hdspm->control_register |= HDSPM_clr_tms;
	else
		hdspm->control_register &= ~HDSPM_clr_tms;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

1997
#define snd_hdspm_info_c_tms		snd_ctl_boolean_mono_info
T
Takashi Iwai 已提交
1998

1999 2000
static int snd_hdspm_get_c_tms(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2001
{
2002
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2003 2004 2005 2006 2007 2008 2009

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

2010 2011
static int snd_hdspm_put_c_tms(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2012
{
2013
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
	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_c_tms(hdspm);
	hdspm_set_c_tms(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_SAFE_MODE(xname, xindex) \
2028
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
2029 2030 2031 2032 2033 2034 2035
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_safe_mode, \
  .get = snd_hdspm_get_safe_mode, \
  .put = snd_hdspm_put_safe_mode \
}

R
Remy Bruno 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
static int hdspm_safe_mode(struct hdspm * hdspm)
{
	return (hdspm->control_register & HDSPM_AutoInp) ? 1 : 0;
}

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

	return 0;
}

2052
#define snd_hdspm_info_safe_mode	snd_ctl_boolean_mono_info
R
Remy Bruno 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 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

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

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

static int snd_hdspm_put_safe_mode(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_safe_mode(hdspm);
	hdspm_set_safe_mode(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_EMPHASIS(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_emphasis, \
  .get = snd_hdspm_get_emphasis, \
  .put = snd_hdspm_put_emphasis \
}

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

static int hdspm_set_emphasis(struct hdspm * hdspm, int emp)
{
	if (emp)
		hdspm->control_register |= HDSPM_Emphasis;
	else
		hdspm->control_register &= ~HDSPM_Emphasis;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

2107
#define snd_hdspm_info_emphasis		snd_ctl_boolean_mono_info
R
Remy Bruno 已提交
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 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 2161

static int snd_hdspm_get_emphasis(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_emphasis(hdspm);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_emphasis(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_emphasis(hdspm);
	hdspm_set_emphasis(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_DOLBY(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_dolby, \
  .get = snd_hdspm_get_dolby, \
  .put = snd_hdspm_put_dolby \
}

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

static int hdspm_set_dolby(struct hdspm * hdspm, int dol)
{
	if (dol)
		hdspm->control_register |= HDSPM_Dolby;
	else
		hdspm->control_register &= ~HDSPM_Dolby;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

2162
#define snd_hdspm_info_dolby		snd_ctl_boolean_mono_info
R
Remy Bruno 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216

static int snd_hdspm_get_dolby(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_dolby(hdspm);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_dolby(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_dolby(hdspm);
	hdspm_set_dolby(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_PROFESSIONAL(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = xname, \
  .index = xindex, \
  .info = snd_hdspm_info_professional, \
  .get = snd_hdspm_get_professional, \
  .put = snd_hdspm_put_professional \
}

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

static int hdspm_set_professional(struct hdspm * hdspm, int dol)
{
	if (dol)
		hdspm->control_register |= HDSPM_Professional;
	else
		hdspm->control_register &= ~HDSPM_Professional;
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

2217
#define snd_hdspm_info_professional	snd_ctl_boolean_mono_info
R
Remy Bruno 已提交
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327

static int snd_hdspm_get_professional(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_professional(hdspm);
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

static int snd_hdspm_put_professional(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_professional(hdspm);
	hdspm_set_professional(hdspm, val);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_INPUT_SELECT(xname, xindex) \
{ .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 \
}

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)
{
	static char *texts[] = { "optical", "coaxial" };

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;

	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);

	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;
}

#define HDSPM_DS_WIRE(xname, xindex) \
{ .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 \
}

static int hdspm_ds_wire(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2328
{
R
Remy Bruno 已提交
2329
	return (hdspm->control_register & HDSPM_DS_DoubleWire) ? 1 : 0;
T
Takashi Iwai 已提交
2330 2331
}

R
Remy Bruno 已提交
2332
static int hdspm_set_ds_wire(struct hdspm * hdspm, int ds)
T
Takashi Iwai 已提交
2333
{
R
Remy Bruno 已提交
2334 2335
	if (ds)
		hdspm->control_register |= HDSPM_DS_DoubleWire;
T
Takashi Iwai 已提交
2336
	else
R
Remy Bruno 已提交
2337
		hdspm->control_register &= ~HDSPM_DS_DoubleWire;
T
Takashi Iwai 已提交
2338 2339 2340 2341 2342
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

R
Remy Bruno 已提交
2343 2344
static int snd_hdspm_info_ds_wire(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2345
{
R
Remy Bruno 已提交
2346 2347 2348
	static char *texts[] = { "Single", "Double" };

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
T
Takashi Iwai 已提交
2349
	uinfo->count = 1;
R
Remy Bruno 已提交
2350 2351 2352 2353 2354 2355 2356 2357
	uinfo->value.enumerated.items = 2;

	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);

T
Takashi Iwai 已提交
2358 2359 2360
	return 0;
}

R
Remy Bruno 已提交
2361 2362
static int snd_hdspm_get_ds_wire(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2363
{
2364
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2365 2366

	spin_lock_irq(&hdspm->lock);
R
Remy Bruno 已提交
2367
	ucontrol->value.enumerated.item[0] = hdspm_ds_wire(hdspm);
T
Takashi Iwai 已提交
2368 2369 2370 2371
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

R
Remy Bruno 已提交
2372 2373
static int snd_hdspm_put_ds_wire(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2374
{
2375
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2376 2377 2378 2379 2380 2381 2382
	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 已提交
2383 2384
	change = (int) val != hdspm_ds_wire(hdspm);
	hdspm_set_ds_wire(hdspm, val);
T
Takashi Iwai 已提交
2385 2386 2387 2388
	spin_unlock_irq(&hdspm->lock);
	return change;
}

R
Remy Bruno 已提交
2389
#define HDSPM_QS_WIRE(xname, xindex) \
2390
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
2391 2392
  .name = xname, \
  .index = xindex, \
R
Remy Bruno 已提交
2393 2394 2395
  .info = snd_hdspm_info_qs_wire, \
  .get = snd_hdspm_get_qs_wire, \
  .put = snd_hdspm_put_qs_wire \
T
Takashi Iwai 已提交
2396 2397
}

R
Remy Bruno 已提交
2398
static int hdspm_qs_wire(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2399
{
R
Remy Bruno 已提交
2400 2401 2402 2403 2404
	if (hdspm->control_register & HDSPM_QS_DoubleWire)
		return 1;
	if (hdspm->control_register & HDSPM_QS_QuadWire)
		return 2;
	return 0;
T
Takashi Iwai 已提交
2405 2406
}

R
Remy Bruno 已提交
2407
static int hdspm_set_qs_wire(struct hdspm * hdspm, int mode)
T
Takashi Iwai 已提交
2408
{
R
Remy Bruno 已提交
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
	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 已提交
2420 2421 2422 2423 2424
	hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register);

	return 0;
}

R
Remy Bruno 已提交
2425
static int snd_hdspm_info_qs_wire(struct snd_kcontrol *kcontrol,
2426
				       struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2427
{
R
Remy Bruno 已提交
2428
	static char *texts[] = { "Single", "Double", "Quad" };
T
Takashi Iwai 已提交
2429 2430 2431

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
R
Remy Bruno 已提交
2432
	uinfo->value.enumerated.items = 3;
T
Takashi Iwai 已提交
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442

	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);

	return 0;
}

R
Remy Bruno 已提交
2443
static int snd_hdspm_get_qs_wire(struct snd_kcontrol *kcontrol,
2444
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2445
{
2446
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2447 2448

	spin_lock_irq(&hdspm->lock);
R
Remy Bruno 已提交
2449
	ucontrol->value.enumerated.item[0] = hdspm_qs_wire(hdspm);
T
Takashi Iwai 已提交
2450 2451 2452 2453
	spin_unlock_irq(&hdspm->lock);
	return 0;
}

R
Remy Bruno 已提交
2454
static int snd_hdspm_put_qs_wire(struct snd_kcontrol *kcontrol,
2455
				      struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2456
{
2457
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2458
	int change;
R
Remy Bruno 已提交
2459
	int val;
T
Takashi Iwai 已提交
2460 2461 2462

	if (!snd_hdspm_use_is_exclusive(hdspm))
		return -EBUSY;
R
Remy Bruno 已提交
2463 2464 2465 2466 2467
	val = ucontrol->value.integer.value[0];
	if (val < 0)
		val = 0;
	if (val > 2)
		val = 2;
T
Takashi Iwai 已提交
2468
	spin_lock_irq(&hdspm->lock);
T
Takashi Iwai 已提交
2469
	change = val != hdspm_qs_wire(hdspm);
R
Remy Bruno 已提交
2470
	hdspm_set_qs_wire(hdspm, val);
T
Takashi Iwai 已提交
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
	spin_unlock_irq(&hdspm->lock);
	return change;
}

/*           Simple Mixer
  deprecated since to much faders ???
  MIXER interface says output (source, destination, value)
   where source > MAX_channels are playback channels 
   on MADICARD 
  - playback mixer matrix: [channelout+64] [output] [value]
  - input(thru) mixer matrix: [channelin] [output] [value]
  (better do 2 kontrols for seperation ?)
*/

#define HDSPM_MIXER(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
  .name = xname, \
  .index = xindex, \
2489
  .device = 0, \
T
Takashi Iwai 已提交
2490 2491 2492 2493 2494 2495 2496
  .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 \
}

2497 2498
static int snd_hdspm_info_mixer(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507
{
	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;
}

2508 2509
static int snd_hdspm_get_mixer(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2510
{
2511
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
	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;
}

2541 2542
static int snd_hdspm_put_mixer(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2543
{
2544
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
	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 已提交
2570 2571
		change = gain != hdspm_read_in_gain(hdspm, destination,
						    source);
T
Takashi Iwai 已提交
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600

	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
   streams. 
*/

#define HDSPM_PLAYBACK_MIXER \
{ .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 \
}

2601 2602
static int snd_hdspm_info_playback_mixer(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2603 2604 2605 2606 2607 2608 2609 2610 2611
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 65536;
	uinfo->value.integer.step = 1;
	return 0;
}

2612 2613
static int snd_hdspm_get_playback_mixer(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2614
{
2615
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2616 2617 2618 2619 2620
	int channel;
	int mapped_channel;

	channel = ucontrol->id.index - 1;

2621 2622
	if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
		return -EINVAL;
T
Takashi Iwai 已提交
2623

T
Takashi Iwai 已提交
2624 2625
	mapped_channel = hdspm->channel_map[channel];
	if (mapped_channel < 0)
T
Takashi Iwai 已提交
2626 2627 2628 2629 2630 2631 2632
		return -EINVAL;

	spin_lock_irq(&hdspm->lock);
	ucontrol->value.integer.value[0] =
	    hdspm_read_pb_gain(hdspm, mapped_channel, mapped_channel);
	spin_unlock_irq(&hdspm->lock);

T
Takashi Iwai 已提交
2633 2634 2635 2636 2637 2638
	/*
	snd_printdd("get pb mixer index %d, channel %d, mapped_channel %d, "
		    "value %d\n",
		    ucontrol->id.index, channel, mapped_channel,
		    ucontrol->value.integer.value[0]); 
	*/
T
Takashi Iwai 已提交
2639 2640 2641
	return 0;
}

2642 2643
static int snd_hdspm_put_playback_mixer(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2644
{
2645
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
	int change;
	int channel;
	int mapped_channel;
	int gain;

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

	channel = ucontrol->id.index - 1;

2656 2657
	if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
		return -EINVAL;
T
Takashi Iwai 已提交
2658

T
Takashi Iwai 已提交
2659 2660
	mapped_channel = hdspm->channel_map[channel];
	if (mapped_channel < 0)
T
Takashi Iwai 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
		return -EINVAL;

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

	spin_lock_irq(&hdspm->lock);
	change =
	    gain != hdspm_read_pb_gain(hdspm, mapped_channel,
				       mapped_channel);
	if (change)
		hdspm_write_pb_gain(hdspm, mapped_channel, mapped_channel,
				    gain);
	spin_unlock_irq(&hdspm->lock);
	return change;
}

#define HDSPM_WC_SYNC_CHECK(xname, xindex) \
2677
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
2678 2679 2680 2681 2682 2683 2684
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdspm_info_sync_check, \
  .get = snd_hdspm_get_wc_sync_check \
}

2685 2686
static int snd_hdspm_info_sync_check(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_info *uinfo)
T
Takashi Iwai 已提交
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
{
	static char *texts[] = { "No Lock", "Lock", "Sync" };
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item =
		    uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name,
	       texts[uinfo->value.enumerated.item]);
	return 0;
}

2700
static int hdspm_wc_sync_check(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2701
{
R
Remy Bruno 已提交
2702 2703 2704 2705 2706
	if (hdspm->is_aes32) {
		int status = hdspm_read(hdspm, HDSPM_statusRegister);
		if (status & HDSPM_AES32_wcLock) {
			/* I don't know how to differenciate sync from lock.
			   Doing as if sync for now */
T
Takashi Iwai 已提交
2707
			return 2;
R
Remy Bruno 已提交
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
		}
		return 0;
	} else {
		int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
		if (status2 & HDSPM_wcLock) {
			if (status2 & HDSPM_wcSync)
				return 2;
			else
				return 1;
		}
		return 0;
T
Takashi Iwai 已提交
2719 2720 2721
	}
}

2722 2723
static int snd_hdspm_get_wc_sync_check(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
T
Takashi Iwai 已提交
2724
{
2725
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2726 2727 2728 2729 2730 2731 2732

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


#define HDSPM_MADI_SYNC_CHECK(xname, xindex) \
2733
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
T
Takashi Iwai 已提交
2734 2735 2736 2737 2738 2739 2740
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdspm_info_sync_check, \
  .get = snd_hdspm_get_madisync_sync_check \
}

2741
static int hdspm_madisync_sync_check(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
{
	int status = hdspm_read(hdspm, HDSPM_statusRegister);
	if (status & HDSPM_madiLock) {
		if (status & HDSPM_madiSync)
			return 2;
		else
			return 1;
	}
	return 0;
}

2753 2754
static int snd_hdspm_get_madisync_sync_check(struct snd_kcontrol *kcontrol,
					     struct snd_ctl_elem_value *
T
Takashi Iwai 已提交
2755 2756
					     ucontrol)
{
2757
	struct hdspm *hdspm = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
2758 2759 2760 2761 2762 2763 2764

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


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 2792 2793 2794 2795 2796 2797 2798
#define HDSPM_AES_SYNC_CHECK(xname, xindex) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = xname, \
  .index = xindex, \
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_hdspm_info_sync_check, \
  .get = snd_hdspm_get_aes_sync_check \
}

static int hdspm_aes_sync_check(struct hdspm * hdspm, int idx)
{
	int status2 = hdspm_read(hdspm, HDSPM_statusRegister2);
	if (status2 & (HDSPM_LockAES >> idx)) {
		/* I don't know how to differenciate sync from lock.
		   Doing as if sync for now */
		return 2;
	}
	return 0;
}

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

	offset = ucontrol->id.index - 1;
	if (offset < 0 || offset >= 8)
		return -EINVAL;

	ucontrol->value.enumerated.item[0] =
		hdspm_aes_sync_check(hdspm, offset);
	return 0;
}
T
Takashi Iwai 已提交
2799 2800


R
Remy Bruno 已提交
2801
static struct snd_kcontrol_new snd_hdspm_controls_madi[] = {
T
Takashi Iwai 已提交
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821

	HDSPM_MIXER("Mixer", 0),
/* 'Sample Clock Source' complies with the alsa control naming scheme */
	HDSPM_CLOCK_SOURCE("Sample Clock Source", 0),

	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),
/* 'External Rate' complies with the alsa control naming scheme */
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
	HDSPM_WC_SYNC_CHECK("Word Clock Lock Status", 0),
	HDSPM_MADI_SYNC_CHECK("MADI Sync Lock Status", 0),
	HDSPM_LINE_OUT("Line Out", 0),
	HDSPM_TX_64("TX 64 channels mode", 0),
	HDSPM_C_TMS("Clear Track Marker", 0),
	HDSPM_SAFE_MODE("Safe Mode", 0),
	HDSPM_INPUT_SELECT("Input Select", 0),
};

R
Remy Bruno 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = {

	HDSPM_MIXER("Mixer", 0),
/* 'Sample Clock Source' complies with the alsa control naming scheme */
	HDSPM_CLOCK_SOURCE("Sample Clock Source", 0),

	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),
/* 'External Rate' complies with the alsa control naming scheme */
	HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
	HDSPM_WC_SYNC_CHECK("Word Clock Lock Status", 0),
/*	HDSPM_AES_SYNC_CHECK("AES Lock Status", 0),*/ /* created in snd_hdspm_create_controls() */
	HDSPM_LINE_OUT("Line Out", 0),
	HDSPM_EMPHASIS("Emphasis", 0),
	HDSPM_DOLBY("Non Audio", 0),
	HDSPM_PROFESSIONAL("Professional", 0),
	HDSPM_C_TMS("Clear Track Marker", 0),
	HDSPM_DS_WIRE("Double Speed Wire Mode", 0),
	HDSPM_QS_WIRE("Quad Speed Wire Mode", 0),
};

2845
static struct snd_kcontrol_new snd_hdspm_playback_mixer = HDSPM_PLAYBACK_MIXER;
T
Takashi Iwai 已提交
2846 2847


2848
static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm)
T
Takashi Iwai 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
{
	int i;

	for (i = hdspm->ds_channels; i < hdspm->ss_channels; ++i) {
		if (hdspm->system_sample_rate > 48000) {
			hdspm->playback_mixer_ctls[i]->vd[0].access =
			    SNDRV_CTL_ELEM_ACCESS_INACTIVE |
			    SNDRV_CTL_ELEM_ACCESS_READ |
			    SNDRV_CTL_ELEM_ACCESS_VOLATILE;
		} else {
			hdspm->playback_mixer_ctls[i]->vd[0].access =
			    SNDRV_CTL_ELEM_ACCESS_READWRITE |
			    SNDRV_CTL_ELEM_ACCESS_VOLATILE;
		}
		snd_ctl_notify(hdspm->card, SNDRV_CTL_EVENT_MASK_VALUE |
			       SNDRV_CTL_EVENT_MASK_INFO,
			       &hdspm->playback_mixer_ctls[i]->id);
	}

	return 0;
}


2872
static int snd_hdspm_create_controls(struct snd_card *card, struct hdspm * hdspm)
T
Takashi Iwai 已提交
2873 2874 2875
{
	unsigned int idx, limit;
	int err;
2876
	struct snd_kcontrol *kctl;
T
Takashi Iwai 已提交
2877 2878

	/* add control list first */
R
Remy Bruno 已提交
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
	if (hdspm->is_aes32) {
		struct snd_kcontrol_new aes_sync_ctl =
			HDSPM_AES_SYNC_CHECK("AES Lock Status", 0);

		for (idx = 0; idx < ARRAY_SIZE(snd_hdspm_controls_aes32);
		     idx++) {
			err = snd_ctl_add(card,
					  snd_ctl_new1(&snd_hdspm_controls_aes32[idx],
						       hdspm));
			if (err < 0)
				return err;
		}
		for (idx = 1; idx <= 8; idx++) {
			aes_sync_ctl.index = idx;
			err = snd_ctl_add(card,
					  snd_ctl_new1(&aes_sync_ctl, hdspm));
			if (err < 0)
				return err;
		}
	} else {
		for (idx = 0; idx < ARRAY_SIZE(snd_hdspm_controls_madi);
		     idx++) {
			err = snd_ctl_add(card,
					  snd_ctl_new1(&snd_hdspm_controls_madi[idx],
						       hdspm));
			if (err < 0)
				return err;
T
Takashi Iwai 已提交
2906 2907 2908 2909
		}
	}

	/* Channel playback mixer as default control 
T
Takashi Iwai 已提交
2910 2911 2912 2913
	   Note: the whole matrix would be 128*HDSPM_MIXER_CHANNELS Faders,
	   thats too * big for any alsamixer they are accesible via special
	   IOCTL on hwdep and the mixer 2dimensional mixer control
	*/
T
Takashi Iwai 已提交
2914 2915 2916 2917

	snd_hdspm_playback_mixer.name = "Chn";
	limit = HDSPM_MAX_CHANNELS;

T
Takashi Iwai 已提交
2918 2919 2920 2921
	/* The index values are one greater than the channel ID so that
	 * alsamixer will display them correctly. We want to use the index
	 * for fast lookup of the relevant channel, but if we use it at all,
	 * most ALSA software does the wrong thing with it ...
T
Takashi Iwai 已提交
2922 2923 2924 2925
	 */

	for (idx = 0; idx < limit; ++idx) {
		snd_hdspm_playback_mixer.index = idx + 1;
T
Takashi Iwai 已提交
2926 2927 2928
		kctl = snd_ctl_new1(&snd_hdspm_playback_mixer, hdspm);
		err = snd_ctl_add(card, kctl);
		if (err < 0)
T
Takashi Iwai 已提交
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
			return err;
		hdspm->playback_mixer_ctls[idx] = kctl;
	}

	return 0;
}

/*------------------------------------------------------------
   /proc interface 
 ------------------------------------------------------------*/

static void
R
Remy Bruno 已提交
2941 2942
snd_hdspm_proc_read_madi(struct snd_info_entry * entry,
			 struct snd_info_buffer *buffer)
T
Takashi Iwai 已提交
2943
{
T
Takashi Iwai 已提交
2944
	struct hdspm *hdspm = entry->private_data;
T
Takashi Iwai 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
	unsigned int status;
	unsigned int status2;
	char *pref_sync_ref;
	char *autosync_ref;
	char *system_clock_mode;
	char *clock_source;
	char *insel;
	char *syncref;
	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, "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 已提交
2977 2978
		    "HW pointer: id = %d, rawptr = %d (%d->%d) "
		    "estimated= %ld (bytes)\n",
T
Takashi Iwai 已提交
2979 2980
		    ((status & HDSPM_BufferID) ? 1 : 0),
		    (status & HDSPM_BufferPositionMask),
T
Takashi Iwai 已提交
2981 2982 2983 2984
		    (status & HDSPM_BufferPositionMask) %
		    (2 * (int)hdspm->period_bytes),
		    ((status & HDSPM_BufferPositionMask) - 64) %
		    (2 * (int)hdspm->period_bytes),
T
Takashi Iwai 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993
		    (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,
T
Takashi Iwai 已提交
2994 2995
		    "Register: ctrl1=0x%x, ctrl2=0x%x, status1=0x%x, "
		    "status2=0x%x\n",
T
Takashi Iwai 已提交
2996 2997 2998 2999 3000
		    hdspm->control_register, hdspm->control2_register,
		    status, status2);

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

T
Takashi Iwai 已提交
3001 3002
	x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
					   HDSPM_LatencyMask));
T
Takashi Iwai 已提交
3003 3004 3005 3006 3007 3008

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

	snd_iprintf(buffer, "Line out: %s,   Precise Pointer: %s\n",
T
Takashi Iwai 已提交
3009
		    (hdspm->control_register & HDSPM_LineOut) ? "on " : "off",
T
Takashi Iwai 已提交
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
		    (hdspm->precise_ptr) ? "on" : "off");

	switch (hdspm->control_register & HDSPM_InputMask) {
	case HDSPM_InputOptical:
		insel = "Optical";
		break;
	case HDSPM_InputCoaxial:
		insel = "Coaxial";
		break;
	default:
3020
		insel = "Unknown";
T
Takashi Iwai 已提交
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030
	}

	switch (hdspm->control_register & HDSPM_SyncRefMask) {
	case HDSPM_SyncRef_Word:
		syncref = "WordClock";
		break;
	case HDSPM_SyncRef_MADI:
		syncref = "MADI";
		break;
	default:
3031
		syncref = "Unknown";
T
Takashi Iwai 已提交
3032 3033 3034 3035 3036
	}
	snd_iprintf(buffer, "Inputsel = %s, SyncRef = %s\n", insel,
		    syncref);

	snd_iprintf(buffer,
T
Takashi Iwai 已提交
3037 3038
		    "ClearTrackMarker = %s, Transmit in %s Channel Mode, "
		    "Auto Input %s\n",
T
Takashi Iwai 已提交
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
		    (hdspm->
		     control_register & HDSPM_clr_tms) ? "on" : "off",
		    (hdspm->
		     control_register & HDSPM_TX_64ch) ? "64" : "56",
		    (hdspm->
		     control_register & HDSPM_AutoInp) ? "on" : "off");

	switch (hdspm_clock_source(hdspm)) {
	case HDSPM_CLOCK_SOURCE_AUTOSYNC:
		clock_source = "AutoSync";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_32KHZ:
		clock_source = "Internal 32 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_44_1KHZ:
		clock_source = "Internal 44.1 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_48KHZ:
		clock_source = "Internal 48 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_64KHZ:
		clock_source = "Internal 64 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_88_2KHZ:
		clock_source = "Internal 88.2 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_96KHZ:
		clock_source = "Internal 96 kHz";
		break;
	default:
		clock_source = "Error";
	}
	snd_iprintf(buffer, "Sample Clock Source: %s\n", clock_source);
R
Remy Bruno 已提交
3072
	if (!(hdspm->control_register & HDSPM_ClockModeMaster))
T
Takashi Iwai 已提交
3073
		system_clock_mode = "Slave";
R
Remy Bruno 已提交
3074
	else
T
Takashi Iwai 已提交
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 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
		system_clock_mode = "Master";
	snd_iprintf(buffer, "System Clock Mode: %s\n", system_clock_mode);

	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;
	default:
		pref_sync_ref = "XXXX Clock";
		break;
	}
	snd_iprintf(buffer, "Preferred Sync Reference: %s\n",
		    pref_sync_ref);

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


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

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

	snd_iprintf(buffer, "Inputs MADI=%s, WordClock=%s\n",
		    (status & HDSPM_madiLock) ? (x ? "Sync" : "Lock") :
		    "NoLock",
		    (status2 & HDSPM_wcLock) ? (x2 ? "Sync" : "Lock") :
		    "NoLock");

	switch (hdspm_autosync_ref(hdspm)) {
	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,
		    "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);

	snd_iprintf(buffer, "Input: %s, Mode=%s\n",
		    (status & HDSPM_AB_int) ? "Coax" : "Optical",
		    (status & HDSPM_RX_64ch) ? "64 channels" :
		    "56 channels");

	snd_iprintf(buffer, "\n");
}

R
Remy Bruno 已提交
3135 3136 3137 3138
static void
snd_hdspm_proc_read_aes32(struct snd_info_entry * entry,
			  struct snd_info_buffer *buffer)
{
T
Takashi Iwai 已提交
3139
	struct hdspm *hdspm = entry->private_data;
R
Remy Bruno 已提交
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	unsigned int status;
	unsigned int status2;
	unsigned int timecode;
	int pref_syncref;
	char *autosync_ref;
	char *system_clock_mode;
	char *clock_source;
	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 已提交
3169 3170
		    "HW pointer: id = %d, rawptr = %d (%d->%d) "
		    "estimated= %ld (bytes)\n",
R
Remy Bruno 已提交
3171 3172
		    ((status & HDSPM_BufferID) ? 1 : 0),
		    (status & HDSPM_BufferPositionMask),
T
Takashi Iwai 已提交
3173 3174 3175 3176
		    (status & HDSPM_BufferPositionMask) %
		    (2 * (int)hdspm->period_bytes),
		    ((status & HDSPM_BufferPositionMask) - 64) %
		    (2 * (int)hdspm->period_bytes),
R
Remy Bruno 已提交
3177 3178 3179 3180 3181 3182 3183 3184 3185
		    (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,
T
Takashi Iwai 已提交
3186 3187
		    "Register: ctrl1=0x%x, status1=0x%x, status2=0x%x, "
		    "timecode=0x%x\n",
3188
		    hdspm->control_register,
R
Remy Bruno 已提交
3189 3190 3191 3192
		    status, status2, timecode);

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

T
Takashi Iwai 已提交
3193 3194
	x = 1 << (6 + hdspm_decode_latency(hdspm->control_register &
					   HDSPM_LatencyMask));
R
Remy Bruno 已提交
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 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 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276

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

	snd_iprintf(buffer, "Line out: %s,   Precise Pointer: %s\n",
		    (hdspm->
		     control_register & HDSPM_LineOut) ? "on " : "off",
		    (hdspm->precise_ptr) ? "on" : "off");

	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");

	switch (hdspm_clock_source(hdspm)) {
	case HDSPM_CLOCK_SOURCE_AUTOSYNC:
		clock_source = "AutoSync";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_32KHZ:
		clock_source = "Internal 32 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_44_1KHZ:
		clock_source = "Internal 44.1 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_48KHZ:
		clock_source = "Internal 48 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_64KHZ:
		clock_source = "Internal 64 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_88_2KHZ:
		clock_source = "Internal 88.2 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_96KHZ:
		clock_source = "Internal 96 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_128KHZ:
		clock_source = "Internal 128 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_176_4KHZ:
		clock_source = "Internal 176.4 kHz";
		break;
	case HDSPM_CLOCK_SOURCE_INTERNAL_192KHZ:
		clock_source = "Internal 192 kHz";
		break;
	default:
		clock_source = "Error";
	}
	snd_iprintf(buffer, "Sample Clock Source: %s\n", clock_source);
	if (!(hdspm->control_register & HDSPM_ClockModeMaster))
		system_clock_mode = "Slave";
	else
		system_clock_mode = "Master";
	snd_iprintf(buffer, "System Clock Mode: %s\n", system_clock_mode);

	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");

	snd_iprintf(buffer, "Word: %s  Frequency: %d\n",
T
Takashi Iwai 已提交
3277 3278
		    (status & HDSPM_AES32_wcLock)? "Sync   " : "No Lock",
		    HDSPM_bit2freq((status >> HDSPM_AES32_wcFreq_bit) & 0xF));
R
Remy Bruno 已提交
3279 3280 3281

	for (x = 0; x < 8; x++) {
		snd_iprintf(buffer, "AES%d: %s  Frequency: %d\n",
T
Takashi Iwai 已提交
3282 3283 3284 3285
			    x+1,
			    (status2 & (HDSPM_LockAES >> x)) ?
			    "Sync   ": "No Lock",
			    HDSPM_bit2freq((timecode >> (4*x)) & 0xF));
R
Remy Bruno 已提交
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
	}

	switch (hdspm_autosync_ref(hdspm)) {
	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;
	default: autosync_ref = "---"; break;
	}
	snd_iprintf(buffer, "AutoSync ref = %s\n", autosync_ref);

	snd_iprintf(buffer, "\n");
}

#ifdef CONFIG_SND_DEBUG
static void
snd_hdspm_proc_read_debug(struct snd_info_entry * entry,
			  struct snd_info_buffer *buffer)
{
T
Takashi Iwai 已提交
3311
	struct hdspm *hdspm = entry->private_data;
R
Remy Bruno 已提交
3312 3313 3314

	int j,i;

T
Takashi Iwai 已提交
3315
	for (i = 0; i < 256 /* 1024*64 */; i += j) {
R
Remy Bruno 已提交
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
		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



3326
static void __devinit snd_hdspm_proc_init(struct hdspm * hdspm)
T
Takashi Iwai 已提交
3327
{
3328
	struct snd_info_entry *entry;
T
Takashi Iwai 已提交
3329 3330

	if (!snd_card_proc_new(hdspm->card, "hdspm", &entry))
3331
		snd_info_set_text_ops(entry, hdspm,
R
Remy Bruno 已提交
3332 3333 3334 3335 3336 3337 3338 3339 3340
				      hdspm->is_aes32 ?
				      snd_hdspm_proc_read_aes32 :
				      snd_hdspm_proc_read_madi);
#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 已提交
3341 3342 3343 3344 3345 3346
}

/*------------------------------------------------------------
   hdspm intitialize 
 ------------------------------------------------------------*/

3347
static int snd_hdspm_set_defaults(struct hdspm * hdspm)
T
Takashi Iwai 已提交
3348 3349 3350 3351
{
	unsigned int i;

	/* ASSUMPTION: hdspm->lock is either held, or there is no need to
J
Joe Perches 已提交
3352
	   hold it (e.g. during module initialization).
T
Takashi Iwai 已提交
3353 3354 3355 3356
	 */

	/* set defaults:       */

R
Remy Bruno 已提交
3357
	if (hdspm->is_aes32)
T
Takashi Iwai 已提交
3358 3359 3360 3361 3362
		hdspm->control_register =
			HDSPM_ClockModeMaster |	/* Master Cloack Mode on */
			hdspm_encode_latency(7) | /* latency maximum =
						   * 8192 samples
						   */
R
Remy Bruno 已提交
3363 3364 3365 3366
			HDSPM_SyncRef0 |	/* AES1 is syncclock */
			HDSPM_LineOut |	/* Analog output in */
			HDSPM_Professional;  /* Professional mode */
	else
T
Takashi Iwai 已提交
3367 3368 3369 3370 3371
		hdspm->control_register =
			HDSPM_ClockModeMaster |	/* Master Cloack Mode on */
			hdspm_encode_latency(7) | /* latency maximum =
						   * 8192 samples
						   */
R
Remy Bruno 已提交
3372 3373 3374 3375 3376
			HDSPM_InputCoaxial |	/* Input Coax not Optical */
			HDSPM_SyncRef_MADI |	/* Madi is syncclock */
			HDSPM_LineOut |	/* Analog output in */
			HDSPM_TX_64ch |	/* transmit in 64ch mode */
			HDSPM_AutoInp;	/* AutoInput chossing (takeover) */
T
Takashi Iwai 已提交
3377 3378 3379 3380 3381 3382 3383

	/* ! HDSPM_Frequency0|HDSPM_Frequency1 = 44.1khz */
	/* !  HDSPM_DoubleSpeed HDSPM_QuadSpeed = normal speed */
	/* ! HDSPM_clr_tms = do not clear bits in track marks */

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

3384 3385
        if (!hdspm->is_aes32) {
		/* No control2 register for AES32 */
T
Takashi Iwai 已提交
3386
#ifdef SNDRV_BIG_ENDIAN
3387
		hdspm->control2_register = HDSPM_BIGENDIAN_MODE;
T
Takashi Iwai 已提交
3388
#else
3389
		hdspm->control2_register = 0;
T
Takashi Iwai 已提交
3390 3391
#endif

3392 3393
		hdspm_write(hdspm, HDSPM_control2Reg, hdspm->control2_register);
	}
T
Takashi Iwai 已提交
3394 3395 3396 3397 3398 3399 3400 3401
	hdspm_compute_period_size(hdspm);

	/* silence everything */

	all_in_all_mixer(hdspm, 0 * UNITY_GAIN);

	if (line_outs_monitor[hdspm->dev]) {

T
Takashi Iwai 已提交
3402 3403
		snd_printk(KERN_INFO "HDSPM: "
			   "sending all playback streams to line outs.\n");
T
Takashi Iwai 已提交
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419

		for (i = 0; i < HDSPM_MIXER_CHANNELS; i++) {
			if (hdspm_write_pb_gain(hdspm, i, i, UNITY_GAIN))
				return -EIO;
		}
	}

	/* set a default rate so that the channel map is set up. */
	hdspm->channel_map = channel_map_madi_ss;
	hdspm_set_rate(hdspm, 44100, 1);

	return 0;
}


/*------------------------------------------------------------
J
Joe Perches 已提交
3420
   interrupt 
T
Takashi Iwai 已提交
3421 3422
 ------------------------------------------------------------*/

3423
static irqreturn_t snd_hdspm_interrupt(int irq, void *dev_id)
T
Takashi Iwai 已提交
3424
{
3425
	struct hdspm *hdspm = (struct hdspm *) dev_id;
T
Takashi Iwai 已提交
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
	unsigned int status;
	int audio;
	int midi0;
	int midi1;
	unsigned int midi0status;
	unsigned int midi1status;
	int schedule = 0;

	status = hdspm_read(hdspm, HDSPM_statusRegister);

	audio = status & HDSPM_audioIRQPending;
	midi0 = status & HDSPM_midi0IRQPending;
	midi1 = status & HDSPM_midi1IRQPending;

	if (!audio && !midi0 && !midi1)
		return IRQ_NONE;

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

	midi0status = hdspm_read(hdspm, HDSPM_midiStatusIn0) & 0xff;
	midi1status = hdspm_read(hdspm, HDSPM_midiStatusIn1) & 0xff;

	if (audio) {

		if (hdspm->capture_substream)
T
Takashi Iwai 已提交
3452
			snd_pcm_period_elapsed(hdspm->capture_substream);
T
Takashi Iwai 已提交
3453 3454

		if (hdspm->playback_substream)
T
Takashi Iwai 已提交
3455
			snd_pcm_period_elapsed(hdspm->playback_substream);
T
Takashi Iwai 已提交
3456 3457 3458
	}

	if (midi0 && midi0status) {
T
Takashi Iwai 已提交
3459 3460 3461
		/* we disable interrupts for this input until processing
		 * is done
		 */
T
Takashi Iwai 已提交
3462 3463 3464 3465 3466 3467 3468
		hdspm->control_register &= ~HDSPM_Midi0InterruptEnable;
		hdspm_write(hdspm, HDSPM_controlRegister,
			    hdspm->control_register);
		hdspm->midi[0].pending = 1;
		schedule = 1;
	}
	if (midi1 && midi1status) {
T
Takashi Iwai 已提交
3469 3470 3471
		/* we disable interrupts for this input until processing
		 * is done
		 */
T
Takashi Iwai 已提交
3472 3473 3474 3475 3476 3477 3478
		hdspm->control_register &= ~HDSPM_Midi1InterruptEnable;
		hdspm_write(hdspm, HDSPM_controlRegister,
			    hdspm->control_register);
		hdspm->midi[1].pending = 1;
		schedule = 1;
	}
	if (schedule)
3479
		tasklet_schedule(&hdspm->midi_tasklet);
T
Takashi Iwai 已提交
3480 3481 3482 3483 3484 3485 3486 3487
	return IRQ_HANDLED;
}

/*------------------------------------------------------------
   pcm interface 
  ------------------------------------------------------------*/


3488
static snd_pcm_uframes_t snd_hdspm_hw_pointer(struct snd_pcm_substream *
T
Takashi Iwai 已提交
3489 3490
					      substream)
{
3491
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3492 3493 3494
	return hdspm_hw_pointer(hdspm);
}

3495
static char *hdspm_channel_buffer_location(struct hdspm * hdspm,
T
Takashi Iwai 已提交
3496 3497 3498 3499
					   int stream, int channel)
{
	int mapped_channel;

3500 3501
	if (snd_BUG_ON(channel < 0 || channel >= HDSPM_MAX_CHANNELS))
		return NULL;
T
Takashi Iwai 已提交
3502

T
Takashi Iwai 已提交
3503 3504
	mapped_channel = hdspm->channel_map[channel];
	if (mapped_channel < 0)
T
Takashi Iwai 已提交
3505 3506
		return NULL;

T
Takashi Iwai 已提交
3507
	if (stream == SNDRV_PCM_STREAM_CAPTURE)
T
Takashi Iwai 已提交
3508 3509
		return hdspm->capture_buffer +
		    mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
T
Takashi Iwai 已提交
3510
	else
T
Takashi Iwai 已提交
3511 3512 3513 3514 3515 3516
		return hdspm->playback_buffer +
		    mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
}


/* dont know why need it ??? */
3517
static int snd_hdspm_playback_copy(struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
3518 3519 3520
				   int channel, snd_pcm_uframes_t pos,
				   void __user *src, snd_pcm_uframes_t count)
{
3521
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3522 3523
	char *channel_buf;

3524 3525
	if (snd_BUG_ON(pos + count > HDSPM_CHANNEL_BUFFER_BYTES / 4))
		return -EINVAL;
T
Takashi Iwai 已提交
3526

T
Takashi Iwai 已提交
3527 3528 3529
	channel_buf =
		hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
					      channel);
T
Takashi Iwai 已提交
3530

3531 3532
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
T
Takashi Iwai 已提交
3533 3534 3535 3536

	return copy_from_user(channel_buf + pos * 4, src, count * 4);
}

3537
static int snd_hdspm_capture_copy(struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
3538 3539 3540
				  int channel, snd_pcm_uframes_t pos,
				  void __user *dst, snd_pcm_uframes_t count)
{
3541
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3542 3543
	char *channel_buf;

3544 3545
	if (snd_BUG_ON(pos + count > HDSPM_CHANNEL_BUFFER_BYTES / 4))
		return -EINVAL;
T
Takashi Iwai 已提交
3546

T
Takashi Iwai 已提交
3547 3548 3549
	channel_buf =
		hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
					      channel);
3550 3551
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
T
Takashi Iwai 已提交
3552 3553 3554
	return copy_to_user(dst, channel_buf + pos * 4, count * 4);
}

3555
static int snd_hdspm_hw_silence(struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
3556 3557 3558
				int channel, snd_pcm_uframes_t pos,
				snd_pcm_uframes_t count)
{
3559
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3560 3561 3562
	char *channel_buf;

	channel_buf =
T
Takashi Iwai 已提交
3563 3564
		hdspm_channel_buffer_location(hdspm, substream->pstr->stream,
					      channel);
3565 3566
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
T
Takashi Iwai 已提交
3567 3568 3569 3570
	memset(channel_buf + pos * 4, 0, count * 4);
	return 0;
}

3571
static int snd_hdspm_reset(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
3572
{
3573 3574 3575
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
T
Takashi Iwai 已提交
3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586

	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) {
3587 3588
		struct snd_pcm_substream *s;
		struct snd_pcm_runtime *oruntime = other->runtime;
3589
		snd_pcm_group_for_each_entry(s, substream) {
T
Takashi Iwai 已提交
3590 3591 3592 3593 3594 3595 3596 3597 3598 3599
			if (s == other) {
				oruntime->status->hw_ptr =
				    runtime->status->hw_ptr;
				break;
			}
		}
	}
	return 0;
}

3600 3601
static int snd_hdspm_hw_params(struct snd_pcm_substream *substream,
			       struct snd_pcm_hw_params *params)
T
Takashi Iwai 已提交
3602
{
3603
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
	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 已提交
3619
	if (other_pid > 0 && this_pid != other_pid) {
T
Takashi Iwai 已提交
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635

		/* The other stream is open, and not by the same
		   task as this one. Make sure that the parameters
		   that matter are the same.
		 */

		if (params_rate(params) != hdspm->system_sample_rate) {
			spin_unlock_irq(&hdspm->lock);
			_snd_pcm_hw_param_setempty(params,
						   SNDRV_PCM_HW_PARAM_RATE);
			return -EBUSY;
		}

		if (params_period_size(params) != hdspm->period_bytes / 4) {
			spin_unlock_irq(&hdspm->lock);
			_snd_pcm_hw_param_setempty(params,
T
Takashi Iwai 已提交
3636
					   SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
T
Takashi Iwai 已提交
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
			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 已提交
3647 3648
	err = hdspm_set_rate(hdspm, params_rate(params), 0);
	if (err < 0) {
T
Takashi Iwai 已提交
3649 3650 3651 3652 3653 3654 3655
		spin_unlock_irq(&hdspm->lock);
		_snd_pcm_hw_param_setempty(params,
					   SNDRV_PCM_HW_PARAM_RATE);
		return err;
	}
	spin_unlock_irq(&hdspm->lock);

T
Takashi Iwai 已提交
3656 3657 3658
	err = hdspm_set_interrupt_interval(hdspm,
					   params_period_size(params));
	if (err < 0) {
T
Takashi Iwai 已提交
3659 3660 3661 3662 3663
		_snd_pcm_hw_param_setempty(params,
					   SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
		return err;
	}

T
Takashi Iwai 已提交
3664 3665 3666
	/* Memory allocation, takashi's method, dont know if we should
	 * spinlock
	 */
T
Takashi Iwai 已提交
3667
	/* malloc all buffer even if not enabled to get sure */
3668 3669
	/* Update for MADI rev 204: we need to allocate for all channels,
	 * otherwise it doesn't work at 96kHz */
T
Takashi Iwai 已提交
3670
	err =
3671
	    snd_pcm_lib_malloc_pages(substream, HDSPM_DMA_AREA_BYTES);
T
Takashi Iwai 已提交
3672 3673 3674 3675 3676
	if (err < 0)
		return err;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {

3677
		hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
T
Takashi Iwai 已提交
3678 3679 3680 3681 3682 3683 3684
				params_channels(params));

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

		hdspm->playback_buffer =
		    (unsigned char *) substream->runtime->dma_area;
3685
		snd_printdd("Allocated sample buffer for playback at %p\n",
R
Remy Bruno 已提交
3686
				hdspm->playback_buffer);
T
Takashi Iwai 已提交
3687
	} else {
3688
		hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
T
Takashi Iwai 已提交
3689 3690 3691 3692 3693 3694 3695
				params_channels(params));

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

		hdspm->capture_buffer =
		    (unsigned char *) substream->runtime->dma_area;
3696
		snd_printdd("Allocated sample buffer for capture at %p\n",
R
Remy Bruno 已提交
3697
				hdspm->capture_buffer);
T
Takashi Iwai 已提交
3698
	}
R
Remy Bruno 已提交
3699 3700 3701 3702
	/*
	   snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
	   substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
	   "playback" : "capture",
3703
	   snd_pcm_sgbuf_get_addr(substream, 0));
R
Remy Bruno 已提交
3704
	 */
3705 3706 3707 3708 3709 3710 3711
	/*
	snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
			substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
			  "playback" : "capture",
			params_rate(params), params_channels(params),
			params_buffer_size(params));
	*/
T
Takashi Iwai 已提交
3712 3713 3714
	return 0;
}

3715
static int snd_hdspm_hw_free(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
3716 3717
{
	int i;
3718
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {

		/* params_channels(params) should be enough, 
		   but to get sure in case of error */
		for (i = 0; i < HDSPM_MAX_CHANNELS; ++i)
			snd_hdspm_enable_out(hdspm, i, 0);

		hdspm->playback_buffer = NULL;
	} else {
		for (i = 0; i < HDSPM_MAX_CHANNELS; ++i)
			snd_hdspm_enable_in(hdspm, i, 0);

		hdspm->capture_buffer = NULL;

	}

	snd_pcm_lib_free_pages(substream);

	return 0;
}

3741 3742
static int snd_hdspm_channel_info(struct snd_pcm_substream *substream,
				  struct snd_pcm_channel_info * info)
T
Takashi Iwai 已提交
3743
{
3744
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
3745 3746
	int mapped_channel;

3747 3748
	if (snd_BUG_ON(info->channel >= HDSPM_MAX_CHANNELS))
		return -EINVAL;
T
Takashi Iwai 已提交
3749

T
Takashi Iwai 已提交
3750 3751
	mapped_channel = hdspm->channel_map[info->channel];
	if (mapped_channel < 0)
T
Takashi Iwai 已提交
3752 3753 3754 3755 3756 3757 3758 3759
		return -EINVAL;

	info->offset = mapped_channel * HDSPM_CHANNEL_BUFFER_BYTES;
	info->first = 0;
	info->step = 32;
	return 0;
}

3760
static int snd_hdspm_ioctl(struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
3761 3762 3763 3764
			   unsigned int cmd, void *arg)
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_RESET:
T
Takashi Iwai 已提交
3765
		return snd_hdspm_reset(substream);
T
Takashi Iwai 已提交
3766 3767

	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
T
Takashi Iwai 已提交
3768 3769 3770 3771
	{
		struct snd_pcm_channel_info *info = arg;
		return snd_hdspm_channel_info(substream, info);
	}
T
Takashi Iwai 已提交
3772 3773 3774 3775 3776 3777 3778
	default:
		break;
	}

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

3779
static int snd_hdspm_trigger(struct snd_pcm_substream *substream, int cmd)
T
Takashi Iwai 已提交
3780
{
3781 3782
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
T
Takashi Iwai 已提交
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
	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) {
3805
		struct snd_pcm_substream *s;
3806
		snd_pcm_group_for_each_entry(s, substream) {
T
Takashi Iwai 已提交
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
			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))
			    && substream->stream ==
			    SNDRV_PCM_STREAM_CAPTURE)
				hdspm_silence_playback(hdspm);
		} else {
			if (running &&
			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
				hdspm_silence_playback(hdspm);
		}
	} else {
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
			hdspm_silence_playback(hdspm);
	}
      _ok:
	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;
}

3842
static int snd_hdspm_prepare(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
3843 3844 3845 3846 3847 3848 3849
{
	return 0;
}

static unsigned int period_sizes[] =
    { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };

3850
static struct snd_pcm_hardware snd_hdspm_playback_subinfo = {
T
Takashi Iwai 已提交
3851 3852 3853 3854 3855 3856 3857 3858 3859
	.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 已提交
3860 3861
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 ),
T
Takashi Iwai 已提交
3862
	.rate_min = 32000,
R
Remy Bruno 已提交
3863
	.rate_max = 192000,
T
Takashi Iwai 已提交
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
	.channels_min = 1,
	.channels_max = HDSPM_MAX_CHANNELS,
	.buffer_bytes_max =
	    HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
	.period_bytes_min = (64 * 4),
	.period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
	.periods_min = 2,
	.periods_max = 2,
	.fifo_size = 0
};

3875
static struct snd_pcm_hardware snd_hdspm_capture_subinfo = {
T
Takashi Iwai 已提交
3876 3877 3878 3879 3880 3881 3882 3883 3884
	.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 已提交
3885 3886
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000),
T
Takashi Iwai 已提交
3887
	.rate_min = 32000,
R
Remy Bruno 已提交
3888
	.rate_max = 192000,
T
Takashi Iwai 已提交
3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899
	.channels_min = 1,
	.channels_max = HDSPM_MAX_CHANNELS,
	.buffer_bytes_max =
	    HDSPM_CHANNEL_BUFFER_BYTES * HDSPM_MAX_CHANNELS,
	.period_bytes_min = (64 * 4),
	.period_bytes_max = (8192 * 4) * HDSPM_MAX_CHANNELS,
	.periods_min = 2,
	.periods_max = 2,
	.fifo_size = 0
};

3900
static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
T
Takashi Iwai 已提交
3901 3902 3903 3904 3905 3906
	.count = ARRAY_SIZE(period_sizes),
	.list = period_sizes,
	.mask = 0
};


3907 3908
static int snd_hdspm_hw_rule_channels_rate(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule * rule)
T
Takashi Iwai 已提交
3909
{
3910 3911
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
T
Takashi Iwai 已提交
3912
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
3913
	struct snd_interval *r =
T
Takashi Iwai 已提交
3914 3915
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);

3916
	if (r->min > 48000 && r->max <= 96000) {
3917
		struct snd_interval t = {
3918
			.min = hdspm->ds_channels,
T
Takashi Iwai 已提交
3919 3920 3921 3922 3923
			.max = hdspm->ds_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 64000) {
3924
		struct snd_interval t = {
3925
			.min = hdspm->ss_channels,
T
Takashi Iwai 已提交
3926 3927 3928 3929 3930 3931 3932 3933
			.max = hdspm->ss_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

3934 3935
static int snd_hdspm_hw_rule_rate_channels(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule * rule)
T
Takashi Iwai 已提交
3936
{
3937 3938
	struct hdspm *hdspm = rule->private;
	struct snd_interval *c =
T
Takashi Iwai 已提交
3939
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
3940
	struct snd_interval *r =
T
Takashi Iwai 已提交
3941 3942
	    hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);

3943
	if (c->min >= hdspm->ss_channels) {
3944
		struct snd_interval t = {
T
Takashi Iwai 已提交
3945 3946 3947 3948 3949
			.min = 32000,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
3950
	} else if (c->max <= hdspm->ds_channels) {
3951
		struct snd_interval t = {
T
Takashi Iwai 已提交
3952 3953 3954 3955 3956 3957 3958 3959 3960 3961
			.min = 64000,
			.max = 96000,
			.integer = 1,
		};

		return snd_interval_refine(r, &t);
	}
	return 0;
}

3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981
static int snd_hdspm_hw_rule_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);
	if (hdspm->is_aes32) {
		list[0] = hdspm->qs_channels;
		list[1] = hdspm->ds_channels;
		list[2] = hdspm->ss_channels;
		return snd_interval_list(c, 3, list, 0);
	} else {
		list[0] = hdspm->ds_channels;
		list[1] = hdspm->ss_channels;
		return snd_interval_list(c, 2, list, 0);
	}
}


T
Takashi Iwai 已提交
3982 3983 3984
static unsigned int hdspm_aes32_sample_rates[] = {
	32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000
};
3985

T
Takashi Iwai 已提交
3986 3987
static struct snd_pcm_hw_constraint_list
hdspm_hw_constraints_aes32_sample_rates = {
3988 3989 3990 3991 3992
	.count = ARRAY_SIZE(hdspm_aes32_sample_rates),
	.list = hdspm_aes32_sample_rates,
	.mask = 0
};

3993
static int snd_hdspm_playback_open(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
3994
{
3995 3996
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017

	spin_lock_irq(&hdspm->lock);

	snd_pcm_set_sync(substream);

	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);

	snd_pcm_hw_constraint_list(runtime, 0,
				   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
				   &hw_constraints_period_sizes);

4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032
	if (hdspm->is_aes32) {
		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_CHANNELS,
				     snd_hdspm_hw_rule_channels, hdspm,
				     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
				    snd_hdspm_hw_rule_channels_rate, hdspm,
				    SNDRV_PCM_HW_PARAM_RATE, -1);

		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				    snd_hdspm_hw_rule_rate_channels, hdspm,
				    SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	}
T
Takashi Iwai 已提交
4033 4034 4035
	return 0;
}

4036
static int snd_hdspm_playback_release(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
4037
{
4038
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050

	spin_lock_irq(&hdspm->lock);

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

	spin_unlock_irq(&hdspm->lock);

	return 0;
}


4051
static int snd_hdspm_capture_open(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
4052
{
4053 4054
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
T
Takashi Iwai 已提交
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071

	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);
	snd_pcm_hw_constraint_list(runtime, 0,
				   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
				   &hw_constraints_period_sizes);
4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086
	if (hdspm->is_aes32) {
		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_CHANNELS,
				     snd_hdspm_hw_rule_channels, hdspm,
				     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
				    snd_hdspm_hw_rule_channels_rate, hdspm,
				    SNDRV_PCM_HW_PARAM_RATE, -1);

		snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				    snd_hdspm_hw_rule_rate_channels, hdspm,
				    SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	}
T
Takashi Iwai 已提交
4087 4088 4089
	return 0;
}

4090
static int snd_hdspm_capture_release(struct snd_pcm_substream *substream)
T
Takashi Iwai 已提交
4091
{
4092
	struct hdspm *hdspm = snd_pcm_substream_chip(substream);
T
Takashi Iwai 已提交
4093 4094 4095 4096 4097 4098 4099 4100 4101 4102

	spin_lock_irq(&hdspm->lock);

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

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

4103
static int snd_hdspm_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
T
Takashi Iwai 已提交
4104 4105
				 unsigned int cmd, unsigned long arg)
{
T
Takashi Iwai 已提交
4106
	struct hdspm *hdspm = hw->private_data;
4107 4108 4109 4110
	struct hdspm_mixer_ioctl mixer;
	struct hdspm_config_info info;
	struct hdspm_version hdspm_version;
	struct hdspm_peak_rms_ioctl rms;
T
Takashi Iwai 已提交
4111 4112 4113 4114 4115 4116

	switch (cmd) {

	case SNDRV_HDSPM_IOCTL_GET_PEAK_RMS:
		if (copy_from_user(&rms, (void __user *)arg, sizeof(rms)))
			return -EFAULT;
T
Takashi Iwai 已提交
4117 4118 4119
		/* maybe there is a chance to memorymap in future
		 * so dont touch just copy
		 */
T
Takashi Iwai 已提交
4120 4121
		if(copy_to_user_fromio((void __user *)rms.peak,
				       hdspm->iobase+HDSPM_MADI_peakrmsbase,
4122
				       sizeof(struct hdspm_peak_rms)) != 0 )
T
Takashi Iwai 已提交
4123 4124 4125 4126 4127 4128 4129 4130
			return -EFAULT;

		break;
		

	case SNDRV_HDSPM_IOCTL_GET_CONFIG_INFO:

		spin_lock_irq(&hdspm->lock);
T
Takashi Iwai 已提交
4131 4132
		info.pref_sync_ref = hdspm_pref_sync_ref(hdspm);
		info.wordclock_sync_check = hdspm_wc_sync_check(hdspm);
T
Takashi Iwai 已提交
4133 4134 4135 4136

		info.system_sample_rate = hdspm->system_sample_rate;
		info.autosync_sample_rate =
		    hdspm_external_sample_rate(hdspm);
T
Takashi Iwai 已提交
4137 4138 4139 4140
		info.system_clock_mode = hdspm_system_clock_mode(hdspm);
		info.clock_source = hdspm_clock_source(hdspm);
		info.autosync_ref = hdspm_autosync_ref(hdspm);
		info.line_out = hdspm_line_out(hdspm);
T
Takashi Iwai 已提交
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
		info.passthru = 0;
		spin_unlock_irq(&hdspm->lock);
		if (copy_to_user((void __user *) arg, &info, sizeof(info)))
			return -EFAULT;
		break;

	case SNDRV_HDSPM_IOCTL_GET_VERSION:
		hdspm_version.firmware_rev = hdspm->firmware_rev;
		if (copy_to_user((void __user *) arg, &hdspm_version,
				 sizeof(hdspm_version)))
			return -EFAULT;
		break;

	case SNDRV_HDSPM_IOCTL_GET_MIXER:
		if (copy_from_user(&mixer, (void __user *)arg, sizeof(mixer)))
			return -EFAULT;
T
Takashi Iwai 已提交
4157 4158
		if (copy_to_user((void __user *)mixer.mixer, hdspm->mixer,
				 sizeof(struct hdspm_mixer)))
T
Takashi Iwai 已提交
4159 4160 4161 4162 4163 4164 4165 4166 4167
			return -EFAULT;
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

4168
static struct snd_pcm_ops snd_hdspm_playback_ops = {
T
Takashi Iwai 已提交
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181
	.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,
	.copy = snd_hdspm_playback_copy,
	.silence = snd_hdspm_hw_silence,
	.page = snd_pcm_sgbuf_ops_page,
};

4182
static struct snd_pcm_ops snd_hdspm_capture_ops = {
T
Takashi Iwai 已提交
4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194
	.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,
	.copy = snd_hdspm_capture_copy,
	.page = snd_pcm_sgbuf_ops_page,
};

4195 4196
static int __devinit snd_hdspm_create_hwdep(struct snd_card *card,
					    struct hdspm * hdspm)
T
Takashi Iwai 已提交
4197
{
4198
	struct snd_hwdep *hw;
T
Takashi Iwai 已提交
4199 4200
	int err;

T
Takashi Iwai 已提交
4201 4202
	err = snd_hwdep_new(card, "HDSPM hwdep", 0, &hw);
	if (err < 0)
T
Takashi Iwai 已提交
4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217
		return err;

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

	hw->ops.ioctl = snd_hdspm_hwdep_ioctl;

	return 0;
}


/*------------------------------------------------------------
   memory interface 
 ------------------------------------------------------------*/
4218
static int __devinit snd_hdspm_preallocate_memory(struct hdspm * hdspm)
T
Takashi Iwai 已提交
4219 4220
{
	int err;
4221
	struct snd_pcm *pcm;
T
Takashi Iwai 已提交
4222 4223 4224 4225
	size_t wanted;

	pcm = hdspm->pcm;

R
Remy Bruno 已提交
4226
	wanted = HDSPM_DMA_AREA_BYTES;
T
Takashi Iwai 已提交
4227

T
Takashi Iwai 已提交
4228
	err =
T
Takashi Iwai 已提交
4229 4230 4231 4232
	     snd_pcm_lib_preallocate_pages_for_all(pcm,
	     					   SNDRV_DMA_TYPE_DEV_SG,
						   snd_dma_pci_data(hdspm->pci),
						   wanted,
T
Takashi Iwai 已提交
4233 4234
						   wanted);
	if (err < 0) {
4235
		snd_printdd("Could not preallocate %zd Bytes\n", wanted);
T
Takashi Iwai 已提交
4236 4237 4238

		return err;
	} else
4239
		snd_printdd(" Preallocated %zd Bytes\n", wanted);
T
Takashi Iwai 已提交
4240 4241 4242 4243

	return 0;
}

4244 4245
static void hdspm_set_sgbuf(struct hdspm * hdspm,
			    struct snd_pcm_substream *substream,
T
Takashi Iwai 已提交
4246 4247 4248 4249 4250
			     unsigned int reg, int channels)
{
	int i;
	for (i = 0; i < (channels * 16); i++)
		hdspm_write(hdspm, reg + 4 * i,
4251
			    snd_pcm_sgbuf_get_addr(substream, 4096 * i));
T
Takashi Iwai 已提交
4252 4253 4254
}

/* ------------- ALSA Devices ---------------------------- */
4255 4256
static int __devinit snd_hdspm_create_pcm(struct snd_card *card,
					  struct hdspm * hdspm)
T
Takashi Iwai 已提交
4257
{
4258
	struct snd_pcm *pcm;
T
Takashi Iwai 已提交
4259 4260
	int err;

T
Takashi Iwai 已提交
4261 4262
	err = snd_pcm_new(card, hdspm->card_name, 0, 1, 1, &pcm);
	if (err < 0)
T
Takashi Iwai 已提交
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275
		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 已提交
4276 4277
	err = snd_hdspm_preallocate_memory(hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4278 4279 4280 4281 4282
		return err;

	return 0;
}

4283
static inline void snd_hdspm_initialize_midi_flush(struct hdspm * hdspm)
T
Takashi Iwai 已提交
4284 4285 4286 4287 4288
{
	snd_hdspm_flush_midi_input(hdspm, 0);
	snd_hdspm_flush_midi_input(hdspm, 1);
}

4289 4290
static int __devinit snd_hdspm_create_alsa_devices(struct snd_card *card,
						   struct hdspm * hdspm)
T
Takashi Iwai 已提交
4291 4292 4293 4294
{
	int err;

	snd_printdd("Create card...\n");
T
Takashi Iwai 已提交
4295 4296
	err = snd_hdspm_create_pcm(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4297 4298
		return err;

T
Takashi Iwai 已提交
4299 4300
	err = snd_hdspm_create_midi(card, hdspm, 0);
	if (err < 0)
T
Takashi Iwai 已提交
4301 4302
		return err;

T
Takashi Iwai 已提交
4303 4304
	err = snd_hdspm_create_midi(card, hdspm, 1);
	if (err < 0)
T
Takashi Iwai 已提交
4305 4306
		return err;

T
Takashi Iwai 已提交
4307 4308
	err = snd_hdspm_create_controls(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4309 4310
		return err;

T
Takashi Iwai 已提交
4311 4312
	err = snd_hdspm_create_hwdep(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326
		return err;

	snd_printdd("proc init...\n");
	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;

	snd_printdd("Set defaults...\n");
T
Takashi Iwai 已提交
4327 4328
	err = snd_hdspm_set_defaults(hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4329 4330 4331 4332 4333 4334 4335
		return err;

	snd_printdd("Update mixer controls...\n");
	hdspm_update_simple_mixer_controls(hdspm);

	snd_printdd("Initializeing complete ???\n");

T
Takashi Iwai 已提交
4336 4337
	err = snd_card_register(card);
	if (err < 0) {
T
Takashi Iwai 已提交
4338 4339 4340 4341 4342 4343 4344 4345 4346
		snd_printk(KERN_ERR "HDSPM: error registering card\n");
		return err;
	}

	snd_printdd("... yes now\n");

	return 0;
}

T
Takashi Iwai 已提交
4347 4348
static int __devinit snd_hdspm_create(struct snd_card *card,
				      struct hdspm *hdspm,
T
Takashi Iwai 已提交
4349 4350 4351 4352 4353 4354 4355
				      int precise_ptr, int enable_monitor)
{
	struct pci_dev *pci = hdspm->pci;
	int err;
	unsigned long io_extent;

	hdspm->irq = -1;
T
Takashi Iwai 已提交
4356

T
Takashi Iwai 已提交
4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
	spin_lock_init(&hdspm->midi[0].lock);
	spin_lock_init(&hdspm->midi[1].lock);

	hdspm->card = card;

	spin_lock_init(&hdspm->lock);

	tasklet_init(&hdspm->midi_tasklet,
		     hdspm_midi_tasklet, (unsigned long) hdspm);

	pci_read_config_word(hdspm->pci,
			     PCI_CLASS_REVISION, &hdspm->firmware_rev);

R
Remy Bruno 已提交
4370 4371
	hdspm->is_aes32 = (hdspm->firmware_rev >= HDSPM_AESREVISION);

T
Takashi Iwai 已提交
4372
	strcpy(card->mixername, "Xilinx FPGA");
R
Remy Bruno 已提交
4373 4374 4375 4376 4377 4378 4379
	if (hdspm->is_aes32) {
		strcpy(card->driver, "HDSPAES32");
		hdspm->card_name = "RME HDSPM AES32";
	} else {
		strcpy(card->driver, "HDSPM");
		hdspm->card_name = "RME HDSPM MADI";
	}
T
Takashi Iwai 已提交
4380

T
Takashi Iwai 已提交
4381 4382
	err = pci_enable_device(pci);
	if (err < 0)
T
Takashi Iwai 已提交
4383 4384 4385 4386
		return err;

	pci_set_master(hdspm->pci);

T
Takashi Iwai 已提交
4387 4388
	err = pci_request_regions(pci, "hdspm");
	if (err < 0)
T
Takashi Iwai 已提交
4389 4390 4391 4392 4393 4394 4395 4396 4397
		return err;

	hdspm->port = pci_resource_start(pci, 0);
	io_extent = pci_resource_len(pci, 0);

	snd_printdd("grabbed memory region 0x%lx-0x%lx\n",
		   hdspm->port, hdspm->port + io_extent - 1);


T
Takashi Iwai 已提交
4398 4399 4400 4401
	hdspm->iobase = ioremap_nocache(hdspm->port, io_extent);
	if (!hdspm->iobase) {
		snd_printk(KERN_ERR "HDSPM: "
			   "unable to remap region 0x%lx-0x%lx\n",
T
Takashi Iwai 已提交
4402 4403 4404 4405 4406 4407 4408 4409
			   hdspm->port, hdspm->port + io_extent - 1);
		return -EBUSY;
	}
	snd_printdd("remapped region (0x%lx) 0x%lx-0x%lx\n",
		   (unsigned long)hdspm->iobase, hdspm->port,
		   hdspm->port + io_extent - 1);

	if (request_irq(pci->irq, snd_hdspm_interrupt,
4410
			IRQF_SHARED, "hdspm", hdspm)) {
T
Takashi Iwai 已提交
4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421
		snd_printk(KERN_ERR "HDSPM: unable to use IRQ %d\n", pci->irq);
		return -EBUSY;
	}

	snd_printdd("use IRQ %d\n", pci->irq);

	hdspm->irq = pci->irq;
	hdspm->precise_ptr = precise_ptr;

	hdspm->monitor_outs = enable_monitor;

4422
	snd_printdd("kmalloc Mixer memory of %zd Bytes\n",
4423
		   sizeof(struct hdspm_mixer));
T
Takashi Iwai 已提交
4424 4425 4426 4427
	hdspm->mixer = kzalloc(sizeof(struct hdspm_mixer), GFP_KERNEL);
	if (!hdspm->mixer) {
		snd_printk(KERN_ERR "HDSPM: "
			   "unable to kmalloc Mixer memory of %d Bytes\n",
4428
			   (int)sizeof(struct hdspm_mixer));
T
Takashi Iwai 已提交
4429 4430 4431 4432 4433 4434 4435 4436
		return err;
	}

	hdspm->ss_channels = MADI_SS_CHANNELS;
	hdspm->ds_channels = MADI_DS_CHANNELS;
	hdspm->qs_channels = MADI_QS_CHANNELS;

	snd_printdd("create alsa devices.\n");
T
Takashi Iwai 已提交
4437 4438
	err = snd_hdspm_create_alsa_devices(card, hdspm);
	if (err < 0)
T
Takashi Iwai 已提交
4439 4440 4441 4442 4443 4444 4445
		return err;

	snd_hdspm_initialize_midi_flush(hdspm);

	return 0;
}

4446
static int snd_hdspm_free(struct hdspm * hdspm)
T
Takashi Iwai 已提交
4447 4448 4449 4450 4451 4452
{

	if (hdspm->port) {

		/* stop th audio, and cancel all interrupts */
		hdspm->control_register &=
T
Takashi Iwai 已提交
4453 4454
		    ~(HDSPM_Start | HDSPM_AudioInterruptEnable |
		      HDSPM_Midi0InterruptEnable | HDSPM_Midi1InterruptEnable);
T
Takashi Iwai 已提交
4455 4456 4457 4458 4459 4460 4461
		hdspm_write(hdspm, HDSPM_controlRegister,
			    hdspm->control_register);
	}

	if (hdspm->irq >= 0)
		free_irq(hdspm->irq, (void *) hdspm);

4462
	kfree(hdspm->mixer);
T
Takashi Iwai 已提交
4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473

	if (hdspm->iobase)
		iounmap(hdspm->iobase);

	if (hdspm->port)
		pci_release_regions(hdspm->pci);

	pci_disable_device(hdspm->pci);
	return 0;
}

4474
static void snd_hdspm_card_free(struct snd_card *card)
T
Takashi Iwai 已提交
4475
{
T
Takashi Iwai 已提交
4476
	struct hdspm *hdspm = card->private_data;
T
Takashi Iwai 已提交
4477 4478 4479 4480 4481 4482 4483 4484 4485

	if (hdspm)
		snd_hdspm_free(hdspm);
}

static int __devinit snd_hdspm_probe(struct pci_dev *pci,
				     const struct pci_device_id *pci_id)
{
	static int dev;
4486 4487
	struct hdspm *hdspm;
	struct snd_card *card;
T
Takashi Iwai 已提交
4488 4489 4490 4491 4492 4493 4494 4495 4496
	int err;

	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

4497 4498 4499 4500
	err = snd_card_create(index[dev], id[dev],
			      THIS_MODULE, sizeof(struct hdspm), &card);
	if (err < 0)
		return err;
T
Takashi Iwai 已提交
4501

T
Takashi Iwai 已提交
4502
	hdspm = card->private_data;
T
Takashi Iwai 已提交
4503 4504 4505 4506
	card->private_free = snd_hdspm_card_free;
	hdspm->dev = dev;
	hdspm->pci = pci;

4507 4508
	snd_card_set_dev(card, &pci->dev);

T
Takashi Iwai 已提交
4509 4510 4511
	err = snd_hdspm_create(card, hdspm, precise_ptr[dev],
			       enable_monitor[dev]);
	if (err < 0) {
T
Takashi Iwai 已提交
4512 4513 4514 4515 4516 4517 4518 4519
		snd_card_free(card);
		return err;
	}

	strcpy(card->shortname, "HDSPM MADI");
	sprintf(card->longname, "%s at 0x%lx, irq %d", hdspm->card_name,
		hdspm->port, hdspm->irq);

T
Takashi Iwai 已提交
4520 4521
	err = snd_card_register(card);
	if (err < 0) {
T
Takashi Iwai 已提交
4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
		snd_card_free(card);
		return err;
	}

	pci_set_drvdata(pci, card);

	dev++;
	return 0;
}

static void __devexit snd_hdspm_remove(struct pci_dev *pci)
{
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
}

static struct pci_driver driver = {
	.name = "RME Hammerfall DSP MADI",
	.id_table = snd_hdspm_ids,
	.probe = snd_hdspm_probe,
	.remove = __devexit_p(snd_hdspm_remove),
};


static int __init alsa_card_hdspm_init(void)
{
	return pci_register_driver(&driver);
}

static void __exit alsa_card_hdspm_exit(void)
{
	pci_unregister_driver(&driver);
}

module_init(alsa_card_hdspm_init)
module_exit(alsa_card_hdspm_exit)