xref: /openbmc/linux/sound/pci/rme9652/hdsp.c (revision 50895a55bcfde8ac6f22a37c6bc8cff506b3c7c6)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *   ALSA driver for RME Hammerfall DSP audio interface(s)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *      Copyright (c) 2002  Paul Davis
61da177e4SLinus Torvalds  *                          Marcus Andersson
71da177e4SLinus Torvalds  *                          Thomas Charbonnel
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/delay.h>
121da177e4SLinus Torvalds #include <linux/interrupt.h>
131da177e4SLinus Torvalds #include <linux/pci.h>
141da177e4SLinus Torvalds #include <linux/firmware.h>
1565a77217SPaul Gortmaker #include <linux/module.h>
163f7440a6STakashi Iwai #include <linux/math64.h>
178232932dSTakashi Iwai #include <linux/vmalloc.h>
186cbbfe1cSTakashi Iwai #include <linux/io.h>
190b84304eSGustavo A. R. Silva #include <linux/nospec.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <sound/core.h>
221da177e4SLinus Torvalds #include <sound/control.h>
231da177e4SLinus Torvalds #include <sound/pcm.h>
241da177e4SLinus Torvalds #include <sound/info.h>
251da177e4SLinus Torvalds #include <sound/asoundef.h>
261da177e4SLinus Torvalds #include <sound/rawmidi.h>
271da177e4SLinus Torvalds #include <sound/hwdep.h>
281da177e4SLinus Torvalds #include <sound/initval.h>
291da177e4SLinus Torvalds #include <sound/hdsp.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/byteorder.h>
321da177e4SLinus Torvalds #include <asm/current.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
351da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
36a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
391da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
401da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
411da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
421da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
431da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
441da177e4SLinus Torvalds MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
451da177e4SLinus Torvalds MODULE_DESCRIPTION("RME Hammerfall DSP");
461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4728b26e15SFlorian Faber MODULE_FIRMWARE("rpm_firmware.bin");
487e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware.bin");
497e0af29dSClemens Ladisch MODULE_FIRMWARE("multiface_firmware_rev11.bin");
507e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware.bin");
517e0af29dSClemens Ladisch MODULE_FIRMWARE("digiface_firmware_rev11.bin");
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define HDSP_MAX_CHANNELS        26
541da177e4SLinus Torvalds #define HDSP_MAX_DS_CHANNELS     14
551da177e4SLinus Torvalds #define HDSP_MAX_QS_CHANNELS     8
561da177e4SLinus Torvalds #define DIGIFACE_SS_CHANNELS     26
571da177e4SLinus Torvalds #define DIGIFACE_DS_CHANNELS     14
581da177e4SLinus Torvalds #define MULTIFACE_SS_CHANNELS    18
591da177e4SLinus Torvalds #define MULTIFACE_DS_CHANNELS    14
601da177e4SLinus Torvalds #define H9652_SS_CHANNELS        26
611da177e4SLinus Torvalds #define H9652_DS_CHANNELS        14
621da177e4SLinus Torvalds /* This does not include possible Analog Extension Boards
631da177e4SLinus Torvalds    AEBs are detected at card initialization
641da177e4SLinus Torvalds */
651da177e4SLinus Torvalds #define H9632_SS_CHANNELS	 12
661da177e4SLinus Torvalds #define H9632_DS_CHANNELS	 8
671da177e4SLinus Torvalds #define H9632_QS_CHANNELS	 4
6828b26e15SFlorian Faber #define RPM_CHANNELS             6
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* Write registers. These are defined as byte-offsets from the iobase value.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds #define HDSP_resetPointer               0
73d7923b2aSRemy Bruno #define HDSP_freqReg			0
741da177e4SLinus Torvalds #define HDSP_outputBufferAddress	32
751da177e4SLinus Torvalds #define HDSP_inputBufferAddress		36
761da177e4SLinus Torvalds #define HDSP_controlRegister		64
771da177e4SLinus Torvalds #define HDSP_interruptConfirmation	96
781da177e4SLinus Torvalds #define HDSP_outputEnable	  	128
791da177e4SLinus Torvalds #define HDSP_control2Reg		256
801da177e4SLinus Torvalds #define HDSP_midiDataOut0  		352
811da177e4SLinus Torvalds #define HDSP_midiDataOut1  		356
821da177e4SLinus Torvalds #define HDSP_fifoData  			368
831da177e4SLinus Torvalds #define HDSP_inputEnable	 	384
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Read registers. These are defined as byte-offsets from the iobase value
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #define HDSP_statusRegister    0
891da177e4SLinus Torvalds #define HDSP_timecode        128
901da177e4SLinus Torvalds #define HDSP_status2Register 192
911da177e4SLinus Torvalds #define HDSP_midiDataIn0     360
921da177e4SLinus Torvalds #define HDSP_midiDataIn1     364
931da177e4SLinus Torvalds #define HDSP_midiStatusOut0  384
941da177e4SLinus Torvalds #define HDSP_midiStatusOut1  388
951da177e4SLinus Torvalds #define HDSP_midiStatusIn0   392
961da177e4SLinus Torvalds #define HDSP_midiStatusIn1   396
971da177e4SLinus Torvalds #define HDSP_fifoStatus      400
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* the meters are regular i/o-mapped registers, but offset
1001da177e4SLinus Torvalds    considerably from the rest. the peak registers are reset
1011da177e4SLinus Torvalds    when read; the least-significant 4 bits are full-scale counters;
1021da177e4SLinus Torvalds    the actual peak value is in the most-significant 24 bits.
1031da177e4SLinus Torvalds */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define HDSP_playbackPeakLevel  4096  /* 26 * 32 bit values */
1061da177e4SLinus Torvalds #define HDSP_inputPeakLevel     4224  /* 26 * 32 bit values */
1071da177e4SLinus Torvalds #define HDSP_outputPeakLevel    4352  /* (26+2) * 32 bit values */
1081da177e4SLinus Torvalds #define HDSP_playbackRmsLevel   4612  /* 26 * 64 bit values */
1091da177e4SLinus Torvalds #define HDSP_inputRmsLevel      4868  /* 26 * 64 bit values */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* This is for H9652 cards
1131da177e4SLinus Torvalds    Peak values are read downward from the base
1141da177e4SLinus Torvalds    Rms values are read upward
1151da177e4SLinus Torvalds    There are rms values for the outputs too
1161da177e4SLinus Torvalds    26*3 values are read in ss mode
1171da177e4SLinus Torvalds    14*3 in ds mode, with no gap between values
1181da177e4SLinus Torvalds */
1191da177e4SLinus Torvalds #define HDSP_9652_peakBase	7164
1201da177e4SLinus Torvalds #define HDSP_9652_rmsBase	4096
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* c.f. the hdsp_9632_meters_t struct */
1231da177e4SLinus Torvalds #define HDSP_9632_metersBase	4096
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #define HDSP_IO_EXTENT     7168
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* control2 register bits */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #define HDSP_TMS                0x01
1301da177e4SLinus Torvalds #define HDSP_TCK                0x02
1311da177e4SLinus Torvalds #define HDSP_TDI                0x04
1321da177e4SLinus Torvalds #define HDSP_JTAG               0x08
1331da177e4SLinus Torvalds #define HDSP_PWDN               0x10
1341da177e4SLinus Torvalds #define HDSP_PROGRAM	        0x020
1351da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_0	0x040
1361da177e4SLinus Torvalds #define HDSP_CONFIG_MODE_1	0x080
137a3466865SAdrian Knoth #define HDSP_VERSION_BIT	(0x100 | HDSP_S_LOAD)
1381da177e4SLinus Torvalds #define HDSP_BIGENDIAN_MODE     0x200
1391da177e4SLinus Torvalds #define HDSP_RD_MULTIPLE        0x400
1401da177e4SLinus Torvalds #define HDSP_9652_ENABLE_MIXER  0x800
1410c2bc7c7SAdrian Knoth #define HDSP_S200		0x800
1420c2bc7c7SAdrian Knoth #define HDSP_S300		(0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
1430c2bc7c7SAdrian Knoth #define HDSP_CYCLIC_MODE	0x1000
1441da177e4SLinus Torvalds #define HDSP_TDO                0x10000000
1451da177e4SLinus Torvalds 
1460c2bc7c7SAdrian Knoth #define HDSP_S_PROGRAM	    (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
1470c2bc7c7SAdrian Knoth #define HDSP_S_LOAD	    (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /* Control Register bits */
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define HDSP_Start                (1<<0)  /* start engine */
1521da177e4SLinus Torvalds #define HDSP_Latency0             (1<<1)  /* buffer size = 2^n where n is defined by Latency{2,1,0} */
1531da177e4SLinus Torvalds #define HDSP_Latency1             (1<<2)  /* [ see above ] */
1541da177e4SLinus Torvalds #define HDSP_Latency2             (1<<3)  /* [ see above ] */
1551da177e4SLinus Torvalds #define HDSP_ClockModeMaster      (1<<4)  /* 1=Master, 0=Slave/Autosync */
1561da177e4SLinus Torvalds #define HDSP_AudioInterruptEnable (1<<5)  /* what do you think ? */
1571da177e4SLinus Torvalds #define HDSP_Frequency0           (1<<6)  /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
1581da177e4SLinus Torvalds #define HDSP_Frequency1           (1<<7)  /* 0=32kHz/64kHz/128kHz */
1591da177e4SLinus Torvalds #define HDSP_DoubleSpeed          (1<<8)  /* 0=normal speed, 1=double speed */
1601da177e4SLinus Torvalds #define HDSP_SPDIFProfessional    (1<<9)  /* 0=consumer, 1=professional */
1611da177e4SLinus Torvalds #define HDSP_SPDIFEmphasis        (1<<10) /* 0=none, 1=on */
1621da177e4SLinus Torvalds #define HDSP_SPDIFNonAudio        (1<<11) /* 0=off, 1=on */
1631da177e4SLinus Torvalds #define HDSP_SPDIFOpticalOut      (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
1641da177e4SLinus Torvalds #define HDSP_SyncRef2             (1<<13)
1651da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect0    (1<<14)
1661da177e4SLinus Torvalds #define HDSP_SPDIFInputSelect1    (1<<15)
1671da177e4SLinus Torvalds #define HDSP_SyncRef0             (1<<16)
1681da177e4SLinus Torvalds #define HDSP_SyncRef1             (1<<17)
1691da177e4SLinus Torvalds #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
1701da177e4SLinus Torvalds #define HDSP_XLRBreakoutCable     (1<<20) /* For H9632 cards */
1711da177e4SLinus Torvalds #define HDSP_Midi0InterruptEnable (1<<22)
1721da177e4SLinus Torvalds #define HDSP_Midi1InterruptEnable (1<<23)
1731da177e4SLinus Torvalds #define HDSP_LineOut              (1<<24)
1741da177e4SLinus Torvalds #define HDSP_ADGain0		  (1<<25) /* From here : H9632 specific */
1751da177e4SLinus Torvalds #define HDSP_ADGain1		  (1<<26)
1761da177e4SLinus Torvalds #define HDSP_DAGain0		  (1<<27)
1771da177e4SLinus Torvalds #define HDSP_DAGain1		  (1<<28)
1781da177e4SLinus Torvalds #define HDSP_PhoneGain0		  (1<<29)
1791da177e4SLinus Torvalds #define HDSP_PhoneGain1		  (1<<30)
1801da177e4SLinus Torvalds #define HDSP_QuadSpeed	  	  (1<<31)
1811da177e4SLinus Torvalds 
18228b26e15SFlorian Faber /* RPM uses some of the registers for special purposes */
18328b26e15SFlorian Faber #define HDSP_RPM_Inp12            0x04A00
18428b26e15SFlorian Faber #define HDSP_RPM_Inp12_Phon_6dB   0x00800  /* Dolby */
18528b26e15SFlorian Faber #define HDSP_RPM_Inp12_Phon_0dB   0x00000  /* .. */
18628b26e15SFlorian Faber #define HDSP_RPM_Inp12_Phon_n6dB  0x04000  /* inp_0 */
18728b26e15SFlorian Faber #define HDSP_RPM_Inp12_Line_0dB   0x04200  /* Dolby+PRO */
18828b26e15SFlorian Faber #define HDSP_RPM_Inp12_Line_n6dB  0x00200  /* PRO */
18928b26e15SFlorian Faber 
19028b26e15SFlorian Faber #define HDSP_RPM_Inp34            0x32000
19128b26e15SFlorian Faber #define HDSP_RPM_Inp34_Phon_6dB   0x20000  /* SyncRef1 */
19228b26e15SFlorian Faber #define HDSP_RPM_Inp34_Phon_0dB   0x00000  /* .. */
19328b26e15SFlorian Faber #define HDSP_RPM_Inp34_Phon_n6dB  0x02000  /* SyncRef2 */
19428b26e15SFlorian Faber #define HDSP_RPM_Inp34_Line_0dB   0x30000  /* SyncRef1+SyncRef0 */
19528b26e15SFlorian Faber #define HDSP_RPM_Inp34_Line_n6dB  0x10000  /* SyncRef0 */
19628b26e15SFlorian Faber 
19728b26e15SFlorian Faber #define HDSP_RPM_Bypass           0x01000
19828b26e15SFlorian Faber 
19928b26e15SFlorian Faber #define HDSP_RPM_Disconnect       0x00001
20028b26e15SFlorian Faber 
2011da177e4SLinus Torvalds #define HDSP_ADGainMask       (HDSP_ADGain0|HDSP_ADGain1)
2021da177e4SLinus Torvalds #define HDSP_ADGainMinus10dBV  HDSP_ADGainMask
2031da177e4SLinus Torvalds #define HDSP_ADGainPlus4dBu   (HDSP_ADGain0)
2041da177e4SLinus Torvalds #define HDSP_ADGainLowGain     0
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds #define HDSP_DAGainMask         (HDSP_DAGain0|HDSP_DAGain1)
2071da177e4SLinus Torvalds #define HDSP_DAGainHighGain      HDSP_DAGainMask
2081da177e4SLinus Torvalds #define HDSP_DAGainPlus4dBu     (HDSP_DAGain0)
2091da177e4SLinus Torvalds #define HDSP_DAGainMinus10dBV    0
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds #define HDSP_PhoneGainMask      (HDSP_PhoneGain0|HDSP_PhoneGain1)
2121da177e4SLinus Torvalds #define HDSP_PhoneGain0dB        HDSP_PhoneGainMask
2131da177e4SLinus Torvalds #define HDSP_PhoneGainMinus6dB  (HDSP_PhoneGain0)
2141da177e4SLinus Torvalds #define HDSP_PhoneGainMinus12dB  0
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds #define HDSP_LatencyMask    (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
2171da177e4SLinus Torvalds #define HDSP_FrequencyMask  (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds #define HDSP_SPDIFInputMask    (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2201da177e4SLinus Torvalds #define HDSP_SPDIFInputADAT1    0
2211da177e4SLinus Torvalds #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
2221da177e4SLinus Torvalds #define HDSP_SPDIFInputCdrom   (HDSP_SPDIFInputSelect1)
2231da177e4SLinus Torvalds #define HDSP_SPDIFInputAES     (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds #define HDSP_SyncRefMask        (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
2261da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT1       0
2271da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT2      (HDSP_SyncRef0)
2281da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT3      (HDSP_SyncRef1)
2291da177e4SLinus Torvalds #define HDSP_SyncRef_SPDIF      (HDSP_SyncRef0|HDSP_SyncRef1)
2301da177e4SLinus Torvalds #define HDSP_SyncRef_WORD       (HDSP_SyncRef2)
2311da177e4SLinus Torvalds #define HDSP_SyncRef_ADAT_SYNC  (HDSP_SyncRef0|HDSP_SyncRef2)
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /* Sample Clock Sources */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_AUTOSYNC           0
2361da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ     1
2371da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ   2
2381da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ     3
2391da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ     4
2401da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ   5
2411da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ     6
2421da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ    7
2431da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ  8
2441da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ    9
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds #define HDSP_SYNC_FROM_WORD      0
2491da177e4SLinus Torvalds #define HDSP_SYNC_FROM_SPDIF     1
2501da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT1     2
2511da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT_SYNC 3
2521da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT2     4
2531da177e4SLinus Torvalds #define HDSP_SYNC_FROM_ADAT3     5
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /* SyncCheck status */
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_NO_LOCK 0
2581da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_LOCK    1
2591da177e4SLinus Torvalds #define HDSP_SYNC_CHECK_SYNC	2
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /* AutoSync references - used by "autosync_ref" control switch */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_WORD      0
2641da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
2651da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_SPDIF     2
2661da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_NONE	     3
2671da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT1     4
2681da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT2     5
2691da177e4SLinus Torvalds #define HDSP_AUTOSYNC_FROM_ADAT3     6
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /* Possible sources of S/PDIF input */
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds #define HDSP_SPDIFIN_OPTICAL  0	/* optical  (ADAT1) */
2741da177e4SLinus Torvalds #define HDSP_SPDIFIN_COAXIAL  1	/* coaxial (RCA) */
2751da177e4SLinus Torvalds #define HDSP_SPDIFIN_INTERNAL 2	/* internal (CDROM) */
2761da177e4SLinus Torvalds #define HDSP_SPDIFIN_AES      3 /* xlr for H9632 (AES)*/
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds #define HDSP_Frequency32KHz    HDSP_Frequency0
2791da177e4SLinus Torvalds #define HDSP_Frequency44_1KHz  HDSP_Frequency1
2801da177e4SLinus Torvalds #define HDSP_Frequency48KHz    (HDSP_Frequency1|HDSP_Frequency0)
2811da177e4SLinus Torvalds #define HDSP_Frequency64KHz    (HDSP_DoubleSpeed|HDSP_Frequency0)
2821da177e4SLinus Torvalds #define HDSP_Frequency88_2KHz  (HDSP_DoubleSpeed|HDSP_Frequency1)
2831da177e4SLinus Torvalds #define HDSP_Frequency96KHz    (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
2841da177e4SLinus Torvalds /* For H9632 cards */
2851da177e4SLinus Torvalds #define HDSP_Frequency128KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
2861da177e4SLinus Torvalds #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
2871da177e4SLinus Torvalds #define HDSP_Frequency192KHz   (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
288e4b6088cSJulian Cable /* RME says n = 104857600000000, but in the windows MADI driver, I see:
289e4b6088cSJulian Cable 	return 104857600000000 / rate; // 100 MHz
290e4b6088cSJulian Cable 	return 110100480000000 / rate; // 105 MHz
291e4b6088cSJulian Cable */
292d0f5137bSHuilong Deng #define DDS_NUMERATOR 104857600000000ULL  /*  =  2^20 * 10^8 */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds #define hdsp_encode_latency(x)       (((x)<<1) & HDSP_LatencyMask)
2951da177e4SLinus Torvalds #define hdsp_decode_latency(x)       (((x) & HDSP_LatencyMask)>>1)
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
2981da177e4SLinus Torvalds #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds /* Status Register bits */
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds #define HDSP_audioIRQPending    (1<<0)
3031da177e4SLinus Torvalds #define HDSP_Lock2              (1<<1)     /* this is for Digiface and H9652 */
3041da177e4SLinus Torvalds #define HDSP_spdifFrequency3	HDSP_Lock2 /* this is for H9632 only */
3051da177e4SLinus Torvalds #define HDSP_Lock1              (1<<2)
3061da177e4SLinus Torvalds #define HDSP_Lock0              (1<<3)
3071da177e4SLinus Torvalds #define HDSP_SPDIFSync          (1<<4)
3081da177e4SLinus Torvalds #define HDSP_TimecodeLock       (1<<5)
3091da177e4SLinus Torvalds #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
3101da177e4SLinus Torvalds #define HDSP_Sync2              (1<<16)
3111da177e4SLinus Torvalds #define HDSP_Sync1              (1<<17)
3121da177e4SLinus Torvalds #define HDSP_Sync0              (1<<18)
3131da177e4SLinus Torvalds #define HDSP_DoubleSpeedStatus  (1<<19)
3141da177e4SLinus Torvalds #define HDSP_ConfigError        (1<<20)
3151da177e4SLinus Torvalds #define HDSP_DllError           (1<<21)
3161da177e4SLinus Torvalds #define HDSP_spdifFrequency0    (1<<22)
3171da177e4SLinus Torvalds #define HDSP_spdifFrequency1    (1<<23)
3181da177e4SLinus Torvalds #define HDSP_spdifFrequency2    (1<<24)
3191da177e4SLinus Torvalds #define HDSP_SPDIFErrorFlag     (1<<25)
3201da177e4SLinus Torvalds #define HDSP_BufferID           (1<<26)
3211da177e4SLinus Torvalds #define HDSP_TimecodeSync       (1<<27)
3221da177e4SLinus Torvalds #define HDSP_AEBO          	(1<<28) /* H9632 specific Analog Extension Boards */
3231da177e4SLinus Torvalds #define HDSP_AEBI		(1<<29) /* 0 = present, 1 = absent */
3241da177e4SLinus Torvalds #define HDSP_midi0IRQPending    (1<<30)
3251da177e4SLinus Torvalds #define HDSP_midi1IRQPending    (1<<31)
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds #define HDSP_spdifFrequencyMask    (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
32847ba97f8SRemy Bruno #define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
32947ba97f8SRemy Bruno 				      HDSP_spdifFrequency1|\
33047ba97f8SRemy Bruno 				      HDSP_spdifFrequency2|\
33147ba97f8SRemy Bruno 				      HDSP_spdifFrequency3)
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds #define HDSP_spdifFrequency32KHz   (HDSP_spdifFrequency0)
3341da177e4SLinus Torvalds #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
3351da177e4SLinus Torvalds #define HDSP_spdifFrequency48KHz   (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds #define HDSP_spdifFrequency64KHz   (HDSP_spdifFrequency2)
3381da177e4SLinus Torvalds #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
3391da177e4SLinus Torvalds #define HDSP_spdifFrequency96KHz   (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* This is for H9632 cards */
34247ba97f8SRemy Bruno #define HDSP_spdifFrequency128KHz   (HDSP_spdifFrequency0|\
34347ba97f8SRemy Bruno 				     HDSP_spdifFrequency1|\
34447ba97f8SRemy Bruno 				     HDSP_spdifFrequency2)
3451da177e4SLinus Torvalds #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
3461da177e4SLinus Torvalds #define HDSP_spdifFrequency192KHz   (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /* Status2 Register bits */
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds #define HDSP_version0     (1<<0)
3511da177e4SLinus Torvalds #define HDSP_version1     (1<<1)
3521da177e4SLinus Torvalds #define HDSP_version2     (1<<2)
3531da177e4SLinus Torvalds #define HDSP_wc_lock      (1<<3)
3541da177e4SLinus Torvalds #define HDSP_wc_sync      (1<<4)
3551da177e4SLinus Torvalds #define HDSP_inp_freq0    (1<<5)
3561da177e4SLinus Torvalds #define HDSP_inp_freq1    (1<<6)
3571da177e4SLinus Torvalds #define HDSP_inp_freq2    (1<<7)
3581da177e4SLinus Torvalds #define HDSP_SelSyncRef0  (1<<8)
3591da177e4SLinus Torvalds #define HDSP_SelSyncRef1  (1<<9)
3601da177e4SLinus Torvalds #define HDSP_SelSyncRef2  (1<<10)
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
3651da177e4SLinus Torvalds #define HDSP_systemFrequency32   (HDSP_inp_freq0)
3661da177e4SLinus Torvalds #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
3671da177e4SLinus Torvalds #define HDSP_systemFrequency48   (HDSP_inp_freq0|HDSP_inp_freq1)
3681da177e4SLinus Torvalds #define HDSP_systemFrequency64   (HDSP_inp_freq2)
3691da177e4SLinus Torvalds #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
3701da177e4SLinus Torvalds #define HDSP_systemFrequency96   (HDSP_inp_freq1|HDSP_inp_freq2)
3711da177e4SLinus Torvalds /* FIXME : more values for 9632 cards ? */
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds #define HDSP_SelSyncRefMask        (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
3741da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT1      0
3751da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT2      (HDSP_SelSyncRef0)
3761da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT3      (HDSP_SelSyncRef1)
3771da177e4SLinus Torvalds #define HDSP_SelSyncRef_SPDIF      (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
3781da177e4SLinus Torvalds #define HDSP_SelSyncRef_WORD       (HDSP_SelSyncRef2)
3791da177e4SLinus Torvalds #define HDSP_SelSyncRef_ADAT_SYNC  (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds /* Card state flags */
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds #define HDSP_InitializationComplete  (1<<0)
3841da177e4SLinus Torvalds #define HDSP_FirmwareLoaded	     (1<<1)
3851da177e4SLinus Torvalds #define HDSP_FirmwareCached	     (1<<2)
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /* FIFO wait times, defined in terms of 1/10ths of msecs */
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds #define HDSP_LONG_WAIT	 5000
3901da177e4SLinus Torvalds #define HDSP_SHORT_WAIT  30
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds #define UNITY_GAIN                       32768
3931da177e4SLinus Torvalds #define MINUS_INFINITY_GAIN              0
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /* the size of a substream (1 mono data stream) */
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_SAMPLES  (16*1024)
3981da177e4SLinus Torvalds #define HDSP_CHANNEL_BUFFER_BYTES    (4*HDSP_CHANNEL_BUFFER_SAMPLES)
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /* the size of the area we need to allocate for DMA transfers. the
4011da177e4SLinus Torvalds    size is the same regardless of the number of channels - the
4021da177e4SLinus Torvalds    Multiface still uses the same memory area.
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds    Note that we allocate 1 more channel than is apparently needed
4051da177e4SLinus Torvalds    because the h/w seems to write 1 byte beyond the end of the last
4061da177e4SLinus Torvalds    page. Sigh.
4071da177e4SLinus Torvalds */
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
4101da177e4SLinus Torvalds #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
4111da177e4SLinus Torvalds 
41290caaef6STakashi Iwai #define HDSP_FIRMWARE_SIZE	(24413 * 4)
4131da177e4SLinus Torvalds 
41455e957d8STakashi Iwai struct hdsp_9632_meters {
4151da177e4SLinus Torvalds     u32 input_peak[16];
4161da177e4SLinus Torvalds     u32 playback_peak[16];
4171da177e4SLinus Torvalds     u32 output_peak[16];
4181da177e4SLinus Torvalds     u32 xxx_peak[16];
4191da177e4SLinus Torvalds     u32 padding[64];
4201da177e4SLinus Torvalds     u32 input_rms_low[16];
4211da177e4SLinus Torvalds     u32 playback_rms_low[16];
4221da177e4SLinus Torvalds     u32 output_rms_low[16];
4231da177e4SLinus Torvalds     u32 xxx_rms_low[16];
4241da177e4SLinus Torvalds     u32 input_rms_high[16];
4251da177e4SLinus Torvalds     u32 playback_rms_high[16];
4261da177e4SLinus Torvalds     u32 output_rms_high[16];
4271da177e4SLinus Torvalds     u32 xxx_rms_high[16];
4281da177e4SLinus Torvalds };
4291da177e4SLinus Torvalds 
43055e957d8STakashi Iwai struct hdsp_midi {
43155e957d8STakashi Iwai     struct hdsp             *hdsp;
4321da177e4SLinus Torvalds     int                      id;
43355e957d8STakashi Iwai     struct snd_rawmidi           *rmidi;
43455e957d8STakashi Iwai     struct snd_rawmidi_substream *input;
43555e957d8STakashi Iwai     struct snd_rawmidi_substream *output;
436*50895a55SJason A. Donenfeld     signed char		     istimer; /* timer in use */
4371da177e4SLinus Torvalds     struct timer_list	     timer;
4381da177e4SLinus Torvalds     spinlock_t               lock;
4391da177e4SLinus Torvalds     int			     pending;
4401da177e4SLinus Torvalds };
4411da177e4SLinus Torvalds 
44255e957d8STakashi Iwai struct hdsp {
4431da177e4SLinus Torvalds 	spinlock_t            lock;
44455e957d8STakashi Iwai 	struct snd_pcm_substream *capture_substream;
44555e957d8STakashi Iwai 	struct snd_pcm_substream *playback_substream;
44655e957d8STakashi Iwai         struct hdsp_midi      midi[2];
4474adab848STakashi Iwai 	struct work_struct    midi_work;
4484adab848STakashi Iwai 	int		      use_midi_work;
4491da177e4SLinus Torvalds 	int                   precise_ptr;
4501da177e4SLinus Torvalds 	u32                   control_register;	     /* cached value */
4511da177e4SLinus Torvalds 	u32                   control2_register;     /* cached value */
4521da177e4SLinus Torvalds 	u32                   creg_spdif;
4531da177e4SLinus Torvalds 	u32                   creg_spdif_stream;
454e3ea4d89STakashi Iwai 	int                   clock_source_locked;
45528b26e15SFlorian Faber 	char                 *card_name;	 /* digiface/multiface/rpm */
45655e957d8STakashi Iwai 	enum HDSP_IO_Type     io_type;               /* ditto, but for code use */
4571da177e4SLinus Torvalds         unsigned short        firmware_rev;
4581da177e4SLinus Torvalds 	unsigned short	      state;		     /* stores state bits */
45990caaef6STakashi Iwai 	const struct firmware *firmware;
46090caaef6STakashi Iwai 	u32                  *fw_uploaded;
4611da177e4SLinus Torvalds 	size_t                period_bytes; 	     /* guess what this is */
4621da177e4SLinus Torvalds 	unsigned char	      max_channels;
4631da177e4SLinus Torvalds 	unsigned char	      qs_in_channels;	     /* quad speed mode for H9632 */
4641da177e4SLinus Torvalds 	unsigned char         ds_in_channels;
4651da177e4SLinus Torvalds 	unsigned char         ss_in_channels;	    /* different for multiface/digiface */
4661da177e4SLinus Torvalds 	unsigned char	      qs_out_channels;
4671da177e4SLinus Torvalds 	unsigned char         ds_out_channels;
4681da177e4SLinus Torvalds 	unsigned char         ss_out_channels;
469da2a040eSJasmin Fazlic 	u32                   io_loopback;          /* output loopback channel states*/
4701da177e4SLinus Torvalds 
47143d35cccSTakashi Iwai 	/* DMA buffers; those are copied instances from the original snd_dma_buf
47243d35cccSTakashi Iwai 	 * objects (which are managed via devres) for the address alignments
47343d35cccSTakashi Iwai 	 */
47443d35cccSTakashi Iwai 	struct snd_dma_buffer capture_dma_buf;
47543d35cccSTakashi Iwai 	struct snd_dma_buffer playback_dma_buf;
4761da177e4SLinus Torvalds 	unsigned char        *capture_buffer;	    /* suitably aligned address */
4771da177e4SLinus Torvalds 	unsigned char        *playback_buffer;	    /* suitably aligned address */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	pid_t                 capture_pid;
4801da177e4SLinus Torvalds 	pid_t                 playback_pid;
4811da177e4SLinus Torvalds 	int                   running;
4821da177e4SLinus Torvalds 	int                   system_sample_rate;
483*50895a55SJason A. Donenfeld 	const signed char    *channel_map;
4841da177e4SLinus Torvalds 	int                   dev;
4851da177e4SLinus Torvalds 	int                   irq;
4861da177e4SLinus Torvalds 	unsigned long         port;
4871da177e4SLinus Torvalds         void __iomem         *iobase;
48855e957d8STakashi Iwai 	struct snd_card *card;
48955e957d8STakashi Iwai 	struct snd_pcm *pcm;
49055e957d8STakashi Iwai 	struct snd_hwdep          *hwdep;
4911da177e4SLinus Torvalds 	struct pci_dev       *pci;
49255e957d8STakashi Iwai 	struct snd_kcontrol *spdif_ctl;
4931da177e4SLinus Torvalds         unsigned short        mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
494d7923b2aSRemy Bruno 	unsigned int          dds_value; /* last value written to freq register */
4951da177e4SLinus Torvalds };
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds /* These tables map the ALSA channels 1..N to the channels that we
4981da177e4SLinus Torvalds    need to use in order to find the relevant channel buffer. RME
4991da177e4SLinus Torvalds    refer to this kind of mapping as between "the ADAT channel and
5001da177e4SLinus Torvalds    the DMA channel." We index it using the logical audio channel,
5011da177e4SLinus Torvalds    and the value is the DMA channel (i.e. channel buffer number)
5021da177e4SLinus Torvalds    where the data for that channel can be read/written from/to.
5031da177e4SLinus Torvalds */
5041da177e4SLinus Torvalds 
505*50895a55SJason A. Donenfeld static const signed char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
5061da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5071da177e4SLinus Torvalds 	18, 19, 20, 21, 22, 23, 24, 25
5081da177e4SLinus Torvalds };
5091da177e4SLinus Torvalds 
510d9259640STakashi Iwai static const char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
5111da177e4SLinus Torvalds 	/* Analog */
5121da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5131da177e4SLinus Torvalds 	/* ADAT 2 */
5141da177e4SLinus Torvalds 	16, 17, 18, 19, 20, 21, 22, 23,
5151da177e4SLinus Torvalds 	/* SPDIF */
5161da177e4SLinus Torvalds 	24, 25,
5171da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1
5181da177e4SLinus Torvalds };
5191da177e4SLinus Torvalds 
520*50895a55SJason A. Donenfeld static const signed char channel_map_ds[HDSP_MAX_CHANNELS] = {
5211da177e4SLinus Torvalds 	/* ADAT channels are remapped */
5221da177e4SLinus Torvalds 	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
5231da177e4SLinus Torvalds 	/* channels 12 and 13 are S/PDIF */
5241da177e4SLinus Torvalds 	24, 25,
5251da177e4SLinus Torvalds 	/* others don't exist */
5261da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
5271da177e4SLinus Torvalds };
5281da177e4SLinus Torvalds 
529*50895a55SJason A. Donenfeld static const signed char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
5301da177e4SLinus Torvalds 	/* ADAT channels */
5311da177e4SLinus Torvalds 	0, 1, 2, 3, 4, 5, 6, 7,
5321da177e4SLinus Torvalds 	/* SPDIF */
5331da177e4SLinus Torvalds 	8, 9,
5341da177e4SLinus Torvalds 	/* Analog */
5351da177e4SLinus Torvalds 	10, 11,
5361da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5371da177e4SLinus Torvalds 	12, 13, 14, 15,
5381da177e4SLinus Torvalds 	/* others don't exist */
5391da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5401da177e4SLinus Torvalds 	-1, -1
5411da177e4SLinus Torvalds };
5421da177e4SLinus Torvalds 
543*50895a55SJason A. Donenfeld static const signed char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
5441da177e4SLinus Torvalds 	/* ADAT */
5451da177e4SLinus Torvalds 	1, 3, 5, 7,
5461da177e4SLinus Torvalds 	/* SPDIF */
5471da177e4SLinus Torvalds 	8, 9,
5481da177e4SLinus Torvalds 	/* Analog */
5491da177e4SLinus Torvalds 	10, 11,
5501da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5511da177e4SLinus Torvalds 	12, 13, 14, 15,
5521da177e4SLinus Torvalds 	/* others don't exist */
5531da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5541da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1
5551da177e4SLinus Torvalds };
5561da177e4SLinus Torvalds 
557*50895a55SJason A. Donenfeld static const signed char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
5581da177e4SLinus Torvalds 	/* ADAT is disabled in this mode */
5591da177e4SLinus Torvalds 	/* SPDIF */
5601da177e4SLinus Torvalds 	8, 9,
5611da177e4SLinus Torvalds 	/* Analog */
5621da177e4SLinus Torvalds 	10, 11,
5631da177e4SLinus Torvalds 	/* AO4S-192 and AI4S-192 extension boards */
5641da177e4SLinus Torvalds 	12, 13, 14, 15,
5651da177e4SLinus Torvalds 	/* others don't exist */
5661da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5671da177e4SLinus Torvalds 	-1, -1, -1, -1, -1, -1, -1, -1,
5681da177e4SLinus Torvalds 	-1, -1
5691da177e4SLinus Torvalds };
5701da177e4SLinus Torvalds 
571d136b8e5STakashi Iwai static struct snd_dma_buffer *
572d136b8e5STakashi Iwai snd_hammerfall_get_buffer(struct pci_dev *pci, size_t size)
5731da177e4SLinus Torvalds {
574d136b8e5STakashi Iwai 	return snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV, size);
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5779baa3c34SBenoit Taine static const struct pci_device_id snd_hdsp_ids[] = {
5781da177e4SLinus Torvalds 	{
5791da177e4SLinus Torvalds 		.vendor = PCI_VENDOR_ID_XILINX,
5801da177e4SLinus Torvalds 		.device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
5811da177e4SLinus Torvalds 		.subvendor = PCI_ANY_ID,
5821da177e4SLinus Torvalds 		.subdevice = PCI_ANY_ID,
5831da177e4SLinus Torvalds 	}, /* RME Hammerfall-DSP */
5841da177e4SLinus Torvalds 	{ 0, },
5851da177e4SLinus Torvalds };
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds /* prototypes */
59055e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
59155e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
59255e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp);
59355e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
59455e957d8STakashi Iwai static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
59555e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
59655e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp);
59755e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp);
59855e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
5991da177e4SLinus Torvalds 
60055e957d8STakashi Iwai static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
6011da177e4SLinus Torvalds {
602a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
603a3a68c85SRemy Bruno 	case Multiface:
604a3a68c85SRemy Bruno 	case Digiface:
60528b26e15SFlorian Faber 	case RPM:
6061da177e4SLinus Torvalds 	default:
607192b8e39SAndreas Degert 		if (hdsp->firmware_rev == 0xa)
608a3a68c85SRemy Bruno 			return (64 * out) + (32 + (in));
609192b8e39SAndreas Degert 		else
610192b8e39SAndreas Degert 			return (52 * out) + (26 + (in));
611a3a68c85SRemy Bruno 	case H9632:
612a3a68c85SRemy Bruno 		return (32 * out) + (16 + (in));
613a3a68c85SRemy Bruno 	case H9652:
6141da177e4SLinus Torvalds 		return (52 * out) + (26 + (in));
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
61855e957d8STakashi Iwai static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
6191da177e4SLinus Torvalds {
620a3a68c85SRemy Bruno 	switch (hdsp->io_type) {
621a3a68c85SRemy Bruno 	case Multiface:
622a3a68c85SRemy Bruno 	case Digiface:
62328b26e15SFlorian Faber 	case RPM:
6241da177e4SLinus Torvalds 	default:
625192b8e39SAndreas Degert 		if (hdsp->firmware_rev == 0xa)
626a3a68c85SRemy Bruno 			return (64 * out) + in;
627192b8e39SAndreas Degert 		else
628192b8e39SAndreas Degert 			return (52 * out) + in;
629a3a68c85SRemy Bruno 	case H9632:
630a3a68c85SRemy Bruno 		return (32 * out) + in;
631a3a68c85SRemy Bruno 	case H9652:
6321da177e4SLinus Torvalds 		return (52 * out) + in;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
63655e957d8STakashi Iwai static void hdsp_write(struct hdsp *hdsp, int reg, int val)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	writel(val, hdsp->iobase + reg);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
64155e957d8STakashi Iwai static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	return readl (hdsp->iobase + reg);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
64655e957d8STakashi Iwai static int hdsp_check_for_iobox (struct hdsp *hdsp)
6471da177e4SLinus Torvalds {
6480c2bc7c7SAdrian Knoth 	int i;
6490c2bc7c7SAdrian Knoth 
6501da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
6510c2bc7c7SAdrian Knoth 	for (i = 0; i < 500; i++) {
6520c2bc7c7SAdrian Knoth 		if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
6530c2bc7c7SAdrian Knoth 					HDSP_ConfigError)) {
6540c2bc7c7SAdrian Knoth 			if (i) {
655a54ba0feSTakashi Iwai 				dev_dbg(hdsp->card->dev,
656a54ba0feSTakashi Iwai 					"IO box found after %d ms\n",
6570c2bc7c7SAdrian Knoth 						(20 * i));
6581da177e4SLinus Torvalds 			}
6591da177e4SLinus Torvalds 			return 0;
660e588ed83STim Blechmann 		}
6610c2bc7c7SAdrian Knoth 		msleep(20);
6620c2bc7c7SAdrian Knoth 	}
663a54ba0feSTakashi Iwai 	dev_err(hdsp->card->dev, "no IO box connected!\n");
6640c2bc7c7SAdrian Knoth 	hdsp->state &= ~HDSP_FirmwareLoaded;
6650c2bc7c7SAdrian Knoth 	return -EIO;
6660c2bc7c7SAdrian Knoth }
6671da177e4SLinus Torvalds 
668e588ed83STim Blechmann static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
669e588ed83STim Blechmann 			       unsigned int delay)
670e588ed83STim Blechmann {
671e588ed83STim Blechmann 	unsigned int i;
672e588ed83STim Blechmann 
673e588ed83STim Blechmann 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
674e588ed83STim Blechmann 		return 0;
675e588ed83STim Blechmann 
676e588ed83STim Blechmann 	for (i = 0; i != loops; ++i) {
677e588ed83STim Blechmann 		if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
678e588ed83STim Blechmann 			msleep(delay);
679e588ed83STim Blechmann 		else {
680a54ba0feSTakashi Iwai 			dev_dbg(hdsp->card->dev, "iobox found after %ums!\n",
681e588ed83STim Blechmann 				   i * delay);
682e588ed83STim Blechmann 			return 0;
683e588ed83STim Blechmann 		}
684e588ed83STim Blechmann 	}
685e588ed83STim Blechmann 
686a54ba0feSTakashi Iwai 	dev_info(hdsp->card->dev, "no IO box connected!\n");
687e588ed83STim Blechmann 	hdsp->state &= ~HDSP_FirmwareLoaded;
688e588ed83STim Blechmann 	return -EIO;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
69155e957d8STakashi Iwai static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	int i;
6941da177e4SLinus Torvalds 	unsigned long flags;
69590caaef6STakashi Iwai 	const u32 *cache;
69690caaef6STakashi Iwai 
69790caaef6STakashi Iwai 	if (hdsp->fw_uploaded)
69890caaef6STakashi Iwai 		cache = hdsp->fw_uploaded;
69990caaef6STakashi Iwai 	else {
70090caaef6STakashi Iwai 		if (!hdsp->firmware)
70190caaef6STakashi Iwai 			return -ENODEV;
70290caaef6STakashi Iwai 		cache = (u32 *)hdsp->firmware->data;
70390caaef6STakashi Iwai 		if (!cache)
70490caaef6STakashi Iwai 			return -ENODEV;
70590caaef6STakashi Iwai 	}
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7081da177e4SLinus Torvalds 
709a54ba0feSTakashi Iwai 		dev_info(hdsp->card->dev, "loading firmware\n");
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
7121da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, 0);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 		if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
715a54ba0feSTakashi Iwai 			dev_info(hdsp->card->dev,
716a54ba0feSTakashi Iwai 				 "timeout waiting for download preparation\n");
7170c2bc7c7SAdrian Knoth 			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
7181da177e4SLinus Torvalds 			return -EIO;
7191da177e4SLinus Torvalds 		}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7221da177e4SLinus Torvalds 
72390caaef6STakashi Iwai 		for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
72490caaef6STakashi Iwai 			hdsp_write(hdsp, HDSP_fifoData, cache[i]);
7251da177e4SLinus Torvalds 			if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
726a54ba0feSTakashi Iwai 				dev_info(hdsp->card->dev,
727a54ba0feSTakashi Iwai 					 "timeout during firmware loading\n");
7280c2bc7c7SAdrian Knoth 				hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
7291da177e4SLinus Torvalds 				return -EIO;
7301da177e4SLinus Torvalds 			}
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 
7330c2bc7c7SAdrian Knoth 		hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
7340c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
7350c2bc7c7SAdrian Knoth 
736ef21ca24SNishanth Aravamudan 		ssleep(3);
7371da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
7381da177e4SLinus Torvalds 		hdsp->control2_register = HDSP_BIGENDIAN_MODE;
7391da177e4SLinus Torvalds #else
7401da177e4SLinus Torvalds 		hdsp->control2_register = 0;
7411da177e4SLinus Torvalds #endif
7421da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
743a54ba0feSTakashi Iwai 		dev_info(hdsp->card->dev, "finished firmware loading\n");
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 	if (hdsp->state & HDSP_InitializationComplete) {
747a54ba0feSTakashi Iwai 		dev_info(hdsp->card->dev,
748a54ba0feSTakashi Iwai 			 "firmware loaded from cache, restoring defaults\n");
7491da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
7501da177e4SLinus Torvalds 		snd_hdsp_set_defaults(hdsp);
7511da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
75955e957d8STakashi Iwai static int hdsp_get_iobox_version (struct hdsp *hdsp)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7641da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_fifoData, 0);
7651da177e4SLinus Torvalds 
7660c2bc7c7SAdrian Knoth 		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
7670c2bc7c7SAdrian Knoth 			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
7681da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7691da177e4SLinus Torvalds 		}
7700c2bc7c7SAdrian Knoth 
7710c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
7720c2bc7c7SAdrian Knoth 		hdsp_write (hdsp, HDSP_fifoData, 0);
77399dcad32SMarkus Elfring 		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
77499dcad32SMarkus Elfring 			goto set_multi;
7750c2bc7c7SAdrian Knoth 
7760c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7770c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_fifoData, 0);
7780c2bc7c7SAdrian Knoth 		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
7790c2bc7c7SAdrian Knoth 			hdsp->io_type = Digiface;
780a54ba0feSTakashi Iwai 			dev_info(hdsp->card->dev, "Digiface found\n");
7810c2bc7c7SAdrian Knoth 			return 0;
7820c2bc7c7SAdrian Knoth 		}
7830c2bc7c7SAdrian Knoth 
7840c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
7850c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7860c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_fifoData, 0);
78799dcad32SMarkus Elfring 		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0)
78899dcad32SMarkus Elfring 			goto set_multi;
7890c2bc7c7SAdrian Knoth 
7900c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
7910c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
7920c2bc7c7SAdrian Knoth 		hdsp_write(hdsp, HDSP_fifoData, 0);
79399dcad32SMarkus Elfring 		if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0)
79499dcad32SMarkus Elfring 			goto set_multi;
7950c2bc7c7SAdrian Knoth 
7960c2bc7c7SAdrian Knoth 		hdsp->io_type = RPM;
797a54ba0feSTakashi Iwai 		dev_info(hdsp->card->dev, "RPM found\n");
7980c2bc7c7SAdrian Knoth 		return 0;
7991da177e4SLinus Torvalds 	} else {
8001da177e4SLinus Torvalds 		/* firmware was already loaded, get iobox type */
80128b26e15SFlorian Faber 		if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
80228b26e15SFlorian Faber 			hdsp->io_type = RPM;
80328b26e15SFlorian Faber 		else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
8041da177e4SLinus Torvalds 			hdsp->io_type = Multiface;
805b0b98119STakashi Iwai 		else
8061da177e4SLinus Torvalds 			hdsp->io_type = Digiface;
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds 	return 0;
80999dcad32SMarkus Elfring 
81099dcad32SMarkus Elfring set_multi:
81199dcad32SMarkus Elfring 	hdsp->io_type = Multiface;
81299dcad32SMarkus Elfring 	dev_info(hdsp->card->dev, "Multiface found\n");
81399dcad32SMarkus Elfring 	return 0;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 
81792eed66dSTakashi Iwai static int hdsp_request_fw_loader(struct hdsp *hdsp);
818311e70a4STakashi Iwai 
819311e70a4STakashi Iwai static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
8201da177e4SLinus Torvalds {
821311e70a4STakashi Iwai 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
822311e70a4STakashi Iwai 		return 0;
8231da177e4SLinus Torvalds 	if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
8241da177e4SLinus Torvalds 		hdsp->state &= ~HDSP_FirmwareLoaded;
825311e70a4STakashi Iwai 		if (! load_on_demand)
826b0b98119STakashi Iwai 			return -EIO;
827a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev, "firmware not present.\n");
828b0b98119STakashi Iwai 		/* try to load firmware */
829311e70a4STakashi Iwai 		if (! (hdsp->state & HDSP_FirmwareCached)) {
830311e70a4STakashi Iwai 			if (! hdsp_request_fw_loader(hdsp))
831311e70a4STakashi Iwai 				return 0;
832a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
833a54ba0feSTakashi Iwai 				   "No firmware loaded nor cached, please upload firmware.\n");
8341da177e4SLinus Torvalds 			return -EIO;
8351da177e4SLinus Torvalds 		}
836311e70a4STakashi Iwai 		if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
837a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
838a54ba0feSTakashi Iwai 				   "Firmware loading from cache failed, please upload manually.\n");
839311e70a4STakashi Iwai 			return -EIO;
840311e70a4STakashi Iwai 		}
841311e70a4STakashi Iwai 	}
8421da177e4SLinus Torvalds 	return 0;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 
84655e957d8STakashi Iwai static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	int i;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	/* the fifoStatus registers reports on how many words
8511da177e4SLinus Torvalds 	   are available in the command FIFO.
8521da177e4SLinus Torvalds 	*/
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	for (i = 0; i < timeout; i++) {
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
8571da177e4SLinus Torvalds 			return 0;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		/* not very friendly, but we only do this during a firmware
8601da177e4SLinus Torvalds 		   load and changing the mixer, so we just put up with it.
8611da177e4SLinus Torvalds 		*/
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		udelay (100);
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 
866a54ba0feSTakashi Iwai 	dev_warn(hdsp->card->dev,
867a54ba0feSTakashi Iwai 		 "wait for FIFO status <= %d failed after %d iterations\n",
8681da177e4SLinus Torvalds 		    count, timeout);
8691da177e4SLinus Torvalds 	return -1;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
87255e957d8STakashi Iwai static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
8731da177e4SLinus Torvalds {
874b0b98119STakashi Iwai 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8751da177e4SLinus Torvalds 		return 0;
876b0b98119STakashi Iwai 
8771da177e4SLinus Torvalds 	return hdsp->mixer_matrix[addr];
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
88055e957d8STakashi Iwai static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	unsigned int ad;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (addr >= HDSP_MATRIX_MIXER_SIZE)
8851da177e4SLinus Torvalds 		return -1;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		/* from martin bjornsen:
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 		   "You can only write dwords to the
8921da177e4SLinus Torvalds 		   mixer memory which contain two
8931da177e4SLinus Torvalds 		   mixer values in the low and high
8941da177e4SLinus Torvalds 		   word. So if you want to change
8951da177e4SLinus Torvalds 		   value 0 you have to read value 1
8961da177e4SLinus Torvalds 		   from the cache and write both to
8971da177e4SLinus Torvalds 		   the first dword in the mixer
8981da177e4SLinus Torvalds 		   memory."
8991da177e4SLinus Torvalds 		*/
9001da177e4SLinus Torvalds 
901b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 && addr >= 512)
9021da177e4SLinus Torvalds 			return 0;
9031da177e4SLinus Torvalds 
904b0b98119STakashi Iwai 		if (hdsp->io_type == H9652 && addr >= 1352)
9051da177e4SLinus Torvalds 			return 0;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 		/* `addr' addresses a 16-bit wide address, but
9111da177e4SLinus Torvalds 		   the address space accessed via hdsp_write
9121da177e4SLinus Torvalds 		   uses byte offsets. put another way, addr
9131da177e4SLinus Torvalds 		   varies from 0 to 1351, but to access the
9141da177e4SLinus Torvalds 		   corresponding memory location, we need
9151da177e4SLinus Torvalds 		   to access 0 to 2703 ...
9161da177e4SLinus Torvalds 		*/
9171da177e4SLinus Torvalds 		ad = addr/2;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 		hdsp_write (hdsp, 4096 + (ad*4),
9201da177e4SLinus Torvalds 			    (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
9211da177e4SLinus Torvalds 			    hdsp->mixer_matrix[addr&0x7fe]);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 		return 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	} else {
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 		ad = (addr << 16) + data;
9281da177e4SLinus Torvalds 
929b0b98119STakashi Iwai 		if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
9301da177e4SLinus Torvalds 			return -1;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_fifoData, ad);
9331da177e4SLinus Torvalds 		hdsp->mixer_matrix[addr] = data;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	return 0;
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
94055e957d8STakashi Iwai static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	unsigned long flags;
9431da177e4SLinus Torvalds 	int ret = 1;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	spin_lock_irqsave(&hdsp->lock, flags);
9461da177e4SLinus Torvalds 	if ((hdsp->playback_pid != hdsp->capture_pid) &&
947b0b98119STakashi Iwai 	    (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
9481da177e4SLinus Torvalds 		ret = 0;
9491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hdsp->lock, flags);
9501da177e4SLinus Torvalds 	return ret;
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
95355e957d8STakashi Iwai static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
9561da177e4SLinus Torvalds 	unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
9571da177e4SLinus Torvalds 
95847ba97f8SRemy Bruno 	/* For the 9632, the mask is different */
95947ba97f8SRemy Bruno 	if (hdsp->io_type == H9632)
96047ba97f8SRemy Bruno 		 rate_bits = (status & HDSP_spdifFrequencyMask_9632);
96147ba97f8SRemy Bruno 
962b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
9631da177e4SLinus Torvalds 		return 0;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	switch (rate_bits) {
9661da177e4SLinus Torvalds 	case HDSP_spdifFrequency32KHz: return 32000;
9671da177e4SLinus Torvalds 	case HDSP_spdifFrequency44_1KHz: return 44100;
9681da177e4SLinus Torvalds 	case HDSP_spdifFrequency48KHz: return 48000;
9691da177e4SLinus Torvalds 	case HDSP_spdifFrequency64KHz: return 64000;
9701da177e4SLinus Torvalds 	case HDSP_spdifFrequency88_2KHz: return 88200;
9711da177e4SLinus Torvalds 	case HDSP_spdifFrequency96KHz: return 96000;
9721da177e4SLinus Torvalds 	case HDSP_spdifFrequency128KHz:
9731da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 128000;
9741da177e4SLinus Torvalds 		break;
9751da177e4SLinus Torvalds 	case HDSP_spdifFrequency176_4KHz:
9761da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 176400;
9771da177e4SLinus Torvalds 		break;
9781da177e4SLinus Torvalds 	case HDSP_spdifFrequency192KHz:
9791da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) return 192000;
9801da177e4SLinus Torvalds 		break;
9811da177e4SLinus Torvalds 	default:
9821da177e4SLinus Torvalds 		break;
9831da177e4SLinus Torvalds 	}
984a54ba0feSTakashi Iwai 	dev_warn(hdsp->card->dev,
985a54ba0feSTakashi Iwai 		 "unknown spdif frequency status; bits = 0x%x, status = 0x%x\n",
986a54ba0feSTakashi Iwai 		 rate_bits, status);
9871da177e4SLinus Torvalds 	return 0;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99047ba97f8SRemy Bruno static int hdsp_external_sample_rate(struct hdsp *hdsp)
99147ba97f8SRemy Bruno {
99247ba97f8SRemy Bruno 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
99347ba97f8SRemy Bruno 	unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
99447ba97f8SRemy Bruno 
99547ba97f8SRemy Bruno 	/* For the 9632 card, there seems to be no bit for indicating external
99647ba97f8SRemy Bruno 	 * sample rate greater than 96kHz. The card reports the corresponding
99747ba97f8SRemy Bruno 	 * single speed. So the best means seems to get spdif rate when
99847ba97f8SRemy Bruno 	 * autosync reference is spdif */
99947ba97f8SRemy Bruno 	if (hdsp->io_type == H9632 &&
100047ba97f8SRemy Bruno 	    hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
100147ba97f8SRemy Bruno 		 return hdsp_spdif_sample_rate(hdsp);
100247ba97f8SRemy Bruno 
100347ba97f8SRemy Bruno 	switch (rate_bits) {
100447ba97f8SRemy Bruno 	case HDSP_systemFrequency32:   return 32000;
100547ba97f8SRemy Bruno 	case HDSP_systemFrequency44_1: return 44100;
100647ba97f8SRemy Bruno 	case HDSP_systemFrequency48:   return 48000;
100747ba97f8SRemy Bruno 	case HDSP_systemFrequency64:   return 64000;
100847ba97f8SRemy Bruno 	case HDSP_systemFrequency88_2: return 88200;
100947ba97f8SRemy Bruno 	case HDSP_systemFrequency96:   return 96000;
101047ba97f8SRemy Bruno 	default:
101147ba97f8SRemy Bruno 		return 0;
101247ba97f8SRemy Bruno 	}
101347ba97f8SRemy Bruno }
101447ba97f8SRemy Bruno 
101555e957d8STakashi Iwai static void hdsp_compute_period_size(struct hdsp *hdsp)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds 	hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
102055e957d8STakashi Iwai static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
10211da177e4SLinus Torvalds {
10221da177e4SLinus Torvalds 	int position;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	position = hdsp_read(hdsp, HDSP_statusRegister);
10251da177e4SLinus Torvalds 
1026b0b98119STakashi Iwai 	if (!hdsp->precise_ptr)
10271da177e4SLinus Torvalds 		return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	position &= HDSP_BufferPositionMask;
10301da177e4SLinus Torvalds 	position /= 4;
10311da177e4SLinus Torvalds 	position &= (hdsp->period_bytes/2) - 1;
10321da177e4SLinus Torvalds 	return position;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
103555e957d8STakashi Iwai static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_resetPointer, 0);
1038d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1039d7923b2aSRemy Bruno 		/* HDSP_resetPointer = HDSP_freqReg, which is strange and
1040d7923b2aSRemy Bruno 		 * requires (?) to write again DDS value after a reset pointer
1041d7923b2aSRemy Bruno 		 * (at least, it works like this) */
1042d7923b2aSRemy Bruno 		hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
104555e957d8STakashi Iwai static void hdsp_start_audio(struct hdsp *s)
10461da177e4SLinus Torvalds {
10471da177e4SLinus Torvalds 	s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
10481da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
105155e957d8STakashi Iwai static void hdsp_stop_audio(struct hdsp *s)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
10541da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
105755e957d8STakashi Iwai static void hdsp_silence_playback(struct hdsp *hdsp)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
106255e957d8STakashi Iwai static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	int n;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	spin_lock_irq(&s->lock);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	frames >>= 7;
10691da177e4SLinus Torvalds 	n = 0;
10701da177e4SLinus Torvalds 	while (frames) {
10711da177e4SLinus Torvalds 		n++;
10721da177e4SLinus Torvalds 		frames >>= 1;
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	s->control_register &= ~HDSP_LatencyMask;
10761da177e4SLinus Torvalds 	s->control_register |= hdsp_encode_latency(n);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	hdsp_write(s, HDSP_controlRegister, s->control_register);
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	hdsp_compute_period_size(s);
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	spin_unlock_irq(&s->lock);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return 0;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
1087d7923b2aSRemy Bruno static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
1088d7923b2aSRemy Bruno {
1089d7923b2aSRemy Bruno 	u64 n;
1090d7923b2aSRemy Bruno 
1091d7923b2aSRemy Bruno 	if (rate >= 112000)
1092d7923b2aSRemy Bruno 		rate /= 4;
1093d7923b2aSRemy Bruno 	else if (rate >= 56000)
1094d7923b2aSRemy Bruno 		rate /= 2;
1095d7923b2aSRemy Bruno 
1096e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
10973f7440a6STakashi Iwai 	n = div_u64(n, rate);
1098d7923b2aSRemy Bruno 	/* n should be less than 2^32 for being written to FREQ register */
1099da3cec35STakashi Iwai 	snd_BUG_ON(n >> 32);
1100d7923b2aSRemy Bruno 	/* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
1101d7923b2aSRemy Bruno 	   value to write it after a reset */
1102d7923b2aSRemy Bruno 	hdsp->dds_value = n;
1103d7923b2aSRemy Bruno 	hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
1104d7923b2aSRemy Bruno }
1105d7923b2aSRemy Bruno 
110655e957d8STakashi Iwai static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
11071da177e4SLinus Torvalds {
11081da177e4SLinus Torvalds 	int reject_if_open = 0;
11091da177e4SLinus Torvalds 	int current_rate;
11101da177e4SLinus Torvalds 	int rate_bits;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
11131da177e4SLinus Torvalds 	   there is no need for it (e.g. during module
11141da177e4SLinus Torvalds 	   initialization).
11151da177e4SLinus Torvalds 	*/
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
11181da177e4SLinus Torvalds 		if (called_internally) {
11191da177e4SLinus Torvalds 			/* request from ctl or card initialization */
1120a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
1121a54ba0feSTakashi Iwai 				"device is not running as a clock master: cannot set sample rate.\n");
11221da177e4SLinus Torvalds 			return -1;
11231da177e4SLinus Torvalds 		} else {
11241da177e4SLinus Torvalds 			/* hw_param request while in AutoSync mode */
11251da177e4SLinus Torvalds 			int external_freq = hdsp_external_sample_rate(hdsp);
11261da177e4SLinus Torvalds 			int spdif_freq = hdsp_spdif_sample_rate(hdsp);
11271da177e4SLinus Torvalds 
1128b0b98119STakashi Iwai 			if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1129a54ba0feSTakashi Iwai 				dev_info(hdsp->card->dev,
1130a54ba0feSTakashi Iwai 					 "Detected ADAT in double speed mode\n");
1131b0b98119STakashi Iwai 			else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
1132a54ba0feSTakashi Iwai 				dev_info(hdsp->card->dev,
1133a54ba0feSTakashi Iwai 					 "Detected ADAT in quad speed mode\n");
1134b0b98119STakashi Iwai 			else if (rate != external_freq) {
1135a54ba0feSTakashi Iwai 				dev_info(hdsp->card->dev,
1136a54ba0feSTakashi Iwai 					 "No AutoSync source for requested rate\n");
11371da177e4SLinus Torvalds 				return -1;
11381da177e4SLinus Torvalds 			}
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	current_rate = hdsp->system_sample_rate;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	/* Changing from a "single speed" to a "double speed" rate is
11451da177e4SLinus Torvalds 	   not allowed if any substreams are open. This is because
11461da177e4SLinus Torvalds 	   such a change causes a shift in the location of
11471da177e4SLinus Torvalds 	   the DMA buffers and a reduction in the number of available
11481da177e4SLinus Torvalds 	   buffers.
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	   Note that a similar but essentially insoluble problem
11511da177e4SLinus Torvalds 	   exists for externally-driven rate changes. All we can do
11521da177e4SLinus Torvalds 	   is to flag rate changes in the read/write routines.  */
11531da177e4SLinus Torvalds 
1154b0b98119STakashi Iwai 	if (rate > 96000 && hdsp->io_type != H9632)
11551da177e4SLinus Torvalds 		return -EINVAL;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	switch (rate) {
11581da177e4SLinus Torvalds 	case 32000:
1159b0b98119STakashi Iwai 		if (current_rate > 48000)
11601da177e4SLinus Torvalds 			reject_if_open = 1;
11611da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency32KHz;
11621da177e4SLinus Torvalds 		break;
11631da177e4SLinus Torvalds 	case 44100:
1164b0b98119STakashi Iwai 		if (current_rate > 48000)
11651da177e4SLinus Torvalds 			reject_if_open = 1;
11661da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency44_1KHz;
11671da177e4SLinus Torvalds 		break;
11681da177e4SLinus Torvalds 	case 48000:
1169b0b98119STakashi Iwai 		if (current_rate > 48000)
11701da177e4SLinus Torvalds 			reject_if_open = 1;
11711da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency48KHz;
11721da177e4SLinus Torvalds 		break;
11731da177e4SLinus Torvalds 	case 64000:
1174b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11751da177e4SLinus Torvalds 			reject_if_open = 1;
11761da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency64KHz;
11771da177e4SLinus Torvalds 		break;
11781da177e4SLinus Torvalds 	case 88200:
1179b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11801da177e4SLinus Torvalds 			reject_if_open = 1;
11811da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency88_2KHz;
11821da177e4SLinus Torvalds 		break;
11831da177e4SLinus Torvalds 	case 96000:
1184b0b98119STakashi Iwai 		if (current_rate <= 48000 || current_rate > 96000)
11851da177e4SLinus Torvalds 			reject_if_open = 1;
11861da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency96KHz;
11871da177e4SLinus Torvalds 		break;
11881da177e4SLinus Torvalds 	case 128000:
1189b0b98119STakashi Iwai 		if (current_rate < 128000)
11901da177e4SLinus Torvalds 			reject_if_open = 1;
11911da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency128KHz;
11921da177e4SLinus Torvalds 		break;
11931da177e4SLinus Torvalds 	case 176400:
1194b0b98119STakashi Iwai 		if (current_rate < 128000)
11951da177e4SLinus Torvalds 			reject_if_open = 1;
11961da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency176_4KHz;
11971da177e4SLinus Torvalds 		break;
11981da177e4SLinus Torvalds 	case 192000:
1199b0b98119STakashi Iwai 		if (current_rate < 128000)
12001da177e4SLinus Torvalds 			reject_if_open = 1;
12011da177e4SLinus Torvalds 		rate_bits = HDSP_Frequency192KHz;
12021da177e4SLinus Torvalds 		break;
12031da177e4SLinus Torvalds 	default:
12041da177e4SLinus Torvalds 		return -EINVAL;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
1208a54ba0feSTakashi Iwai 		dev_warn(hdsp->card->dev,
1209a54ba0feSTakashi Iwai 			 "cannot change speed mode (capture PID = %d, playback PID = %d)\n",
12101da177e4SLinus Torvalds 			    hdsp->capture_pid,
12111da177e4SLinus Torvalds 			    hdsp->playback_pid);
12121da177e4SLinus Torvalds 		return -EBUSY;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_FrequencyMask;
12161da177e4SLinus Torvalds 	hdsp->control_register |= rate_bits;
12171da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
12181da177e4SLinus Torvalds 
1219d7923b2aSRemy Bruno 	/* For HDSP9632 rev 152, need to set DDS value in FREQ register */
1220d7923b2aSRemy Bruno 	if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
1221d7923b2aSRemy Bruno 		hdsp_set_dds_value(hdsp, rate);
1222d7923b2aSRemy Bruno 
12231da177e4SLinus Torvalds 	if (rate >= 128000) {
12241da177e4SLinus Torvalds 		hdsp->channel_map = channel_map_H9632_qs;
12251da177e4SLinus Torvalds 	} else if (rate > 48000) {
1226b0b98119STakashi Iwai 		if (hdsp->io_type == H9632)
12271da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ds;
1228b0b98119STakashi Iwai 		else
12291da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_ds;
12301da177e4SLinus Torvalds 	} else {
12311da177e4SLinus Torvalds 		switch (hdsp->io_type) {
123228b26e15SFlorian Faber 		case RPM:
12331da177e4SLinus Torvalds 		case Multiface:
12341da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_mf_ss;
12351da177e4SLinus Torvalds 			break;
12361da177e4SLinus Torvalds 		case Digiface:
12371da177e4SLinus Torvalds 		case H9652:
12381da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_df_ss;
12391da177e4SLinus Torvalds 			break;
12401da177e4SLinus Torvalds 		case H9632:
12411da177e4SLinus Torvalds 			hdsp->channel_map = channel_map_H9632_ss;
12421da177e4SLinus Torvalds 			break;
12431da177e4SLinus Torvalds 		default:
12441da177e4SLinus Torvalds 			/* should never happen */
12451da177e4SLinus Torvalds 			break;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	hdsp->system_sample_rate = rate;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	return 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /*----------------------------------------------------------------------------
12551da177e4SLinus Torvalds    MIDI
12561da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
12571da177e4SLinus Torvalds 
125855e957d8STakashi Iwai static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1261b0b98119STakashi Iwai 	if (id)
12621da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn1);
1263b0b98119STakashi Iwai 	else
12641da177e4SLinus Torvalds 		return hdsp_read(hdsp, HDSP_midiDataIn0);
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
126755e957d8STakashi Iwai static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	/* the hardware already does the relevant bit-mask with 0xff */
1270b0b98119STakashi Iwai 	if (id)
12711da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut1, val);
1272b0b98119STakashi Iwai 	else
12731da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_midiDataOut0, val);
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
127655e957d8STakashi Iwai static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
12771da177e4SLinus Torvalds {
1278b0b98119STakashi Iwai 	if (id)
12791da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
1280b0b98119STakashi Iwai 	else
12811da177e4SLinus Torvalds 		return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
128455e957d8STakashi Iwai static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
12851da177e4SLinus Torvalds {
12861da177e4SLinus Torvalds 	int fifo_bytes_used;
12871da177e4SLinus Torvalds 
1288b0b98119STakashi Iwai 	if (id)
12891da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
1290b0b98119STakashi Iwai 	else
12911da177e4SLinus Torvalds 		fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
12921da177e4SLinus Torvalds 
1293b0b98119STakashi Iwai 	if (fifo_bytes_used < 128)
12941da177e4SLinus Torvalds 		return  128 - fifo_bytes_used;
1295b0b98119STakashi Iwai 	else
12961da177e4SLinus Torvalds 		return 0;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
129955e957d8STakashi Iwai static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
13001da177e4SLinus Torvalds {
1301b0b98119STakashi Iwai 	while (snd_hdsp_midi_input_available (hdsp, id))
13021da177e4SLinus Torvalds 		snd_hdsp_midi_read_byte (hdsp, id);
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
130555e957d8STakashi Iwai static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	unsigned long flags;
13081da177e4SLinus Torvalds 	int n_pending;
13091da177e4SLinus Torvalds 	int to_write;
13101da177e4SLinus Torvalds 	int i;
13111da177e4SLinus Torvalds 	unsigned char buf[128];
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	/* Output is not interrupt driven */
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13161da177e4SLinus Torvalds 	if (hmidi->output) {
13171da177e4SLinus Torvalds 		if (!snd_rawmidi_transmit_empty (hmidi->output)) {
131866c8f759STakashi Iwai 			n_pending = snd_hdsp_midi_output_possible(hmidi->hdsp, hmidi->id);
131966c8f759STakashi Iwai 			if (n_pending > 0) {
13201da177e4SLinus Torvalds 				if (n_pending > (int)sizeof (buf))
13211da177e4SLinus Torvalds 					n_pending = sizeof (buf);
13221da177e4SLinus Torvalds 
132366c8f759STakashi Iwai 				to_write = snd_rawmidi_transmit(hmidi->output, buf, n_pending);
132466c8f759STakashi Iwai 				if (to_write > 0) {
13251da177e4SLinus Torvalds 					for (i = 0; i < to_write; ++i)
13261da177e4SLinus Torvalds 						snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
13271da177e4SLinus Torvalds 				}
13281da177e4SLinus Torvalds 			}
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13321da177e4SLinus Torvalds 	return 0;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
133555e957d8STakashi Iwai static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds 	unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
13381da177e4SLinus Torvalds 	unsigned long flags;
13391da177e4SLinus Torvalds 	int n_pending;
13401da177e4SLinus Torvalds 	int i;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
134366c8f759STakashi Iwai 	n_pending = snd_hdsp_midi_input_available(hmidi->hdsp, hmidi->id);
134466c8f759STakashi Iwai 	if (n_pending > 0) {
13451da177e4SLinus Torvalds 		if (hmidi->input) {
1346b0b98119STakashi Iwai 			if (n_pending > (int)sizeof (buf))
13471da177e4SLinus Torvalds 				n_pending = sizeof (buf);
1348b0b98119STakashi Iwai 			for (i = 0; i < n_pending; ++i)
13491da177e4SLinus Torvalds 				buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1350b0b98119STakashi Iwai 			if (n_pending)
13511da177e4SLinus Torvalds 				snd_rawmidi_receive (hmidi->input, buf, n_pending);
13521da177e4SLinus Torvalds 		} else {
13531da177e4SLinus Torvalds 			/* flush the MIDI input FIFO */
1354b0b98119STakashi Iwai 			while (--n_pending)
13551da177e4SLinus Torvalds 				snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 	hmidi->pending = 0;
1359b0b98119STakashi Iwai 	if (hmidi->id)
13601da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1361b0b98119STakashi Iwai 	else
13621da177e4SLinus Torvalds 		hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
13631da177e4SLinus Torvalds 	hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
13641da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
13651da177e4SLinus Torvalds 	return snd_hdsp_midi_output_write (hmidi);
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
136855e957d8STakashi Iwai static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
13691da177e4SLinus Torvalds {
137055e957d8STakashi Iwai 	struct hdsp *hdsp;
137155e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
13721da177e4SLinus Torvalds 	unsigned long flags;
13731da177e4SLinus Torvalds 	u32 ie;
13741da177e4SLinus Torvalds 
137555e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13761da177e4SLinus Torvalds 	hdsp = hmidi->hdsp;
13771da177e4SLinus Torvalds 	ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
13781da177e4SLinus Torvalds 	spin_lock_irqsave (&hdsp->lock, flags);
13791da177e4SLinus Torvalds 	if (up) {
13801da177e4SLinus Torvalds 		if (!(hdsp->control_register & ie)) {
13811da177e4SLinus Torvalds 			snd_hdsp_flush_midi_input (hdsp, hmidi->id);
13821da177e4SLinus Torvalds 			hdsp->control_register |= ie;
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 	} else {
13851da177e4SLinus Torvalds 		hdsp->control_register &= ~ie;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
13891da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hdsp->lock, flags);
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds 
13927211ec63SKees Cook static void snd_hdsp_midi_output_timer(struct timer_list *t)
13931da177e4SLinus Torvalds {
13947211ec63SKees Cook 	struct hdsp_midi *hmidi = from_timer(hmidi, t, timer);
13951da177e4SLinus Torvalds 	unsigned long flags;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	snd_hdsp_midi_output_write(hmidi);
13981da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	/* this does not bump hmidi->istimer, because the
14011da177e4SLinus Torvalds 	   kernel automatically removed the timer when it
14021da177e4SLinus Torvalds 	   expired, and we are now adding it back, thus
14031da177e4SLinus Torvalds 	   leaving istimer wherever it was set before.
14041da177e4SLinus Torvalds 	*/
14051da177e4SLinus Torvalds 
1406c41c009eSTakashi Iwai 	if (hmidi->istimer)
1407c41c009eSTakashi Iwai 		mod_timer(&hmidi->timer, 1 + jiffies);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
141255e957d8STakashi Iwai static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
14131da177e4SLinus Torvalds {
141455e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14151da177e4SLinus Torvalds 	unsigned long flags;
14161da177e4SLinus Torvalds 
141755e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14181da177e4SLinus Torvalds 	spin_lock_irqsave (&hmidi->lock, flags);
14191da177e4SLinus Torvalds 	if (up) {
14201da177e4SLinus Torvalds 		if (!hmidi->istimer) {
14217211ec63SKees Cook 			timer_setup(&hmidi->timer, snd_hdsp_midi_output_timer,
14227211ec63SKees Cook 				    0);
1423c41c009eSTakashi Iwai 			mod_timer(&hmidi->timer, 1 + jiffies);
14241da177e4SLinus Torvalds 			hmidi->istimer++;
14251da177e4SLinus Torvalds 		}
14261da177e4SLinus Torvalds 	} else {
1427b0b98119STakashi Iwai 		if (hmidi->istimer && --hmidi->istimer <= 0)
14281da177e4SLinus Torvalds 			del_timer (&hmidi->timer);
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hmidi->lock, flags);
14311da177e4SLinus Torvalds 	if (up)
14321da177e4SLinus Torvalds 		snd_hdsp_midi_output_write(hmidi);
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
143555e957d8STakashi Iwai static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
14361da177e4SLinus Torvalds {
143755e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14381da177e4SLinus Torvalds 
143955e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14401da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14411da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
14421da177e4SLinus Torvalds 	hmidi->input = substream;
14431da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	return 0;
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds 
144855e957d8STakashi Iwai static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
14491da177e4SLinus Torvalds {
145055e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14511da177e4SLinus Torvalds 
145255e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14531da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14541da177e4SLinus Torvalds 	hmidi->output = substream;
14551da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	return 0;
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
146055e957d8STakashi Iwai static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
14611da177e4SLinus Torvalds {
146255e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	snd_hdsp_midi_input_trigger (substream, 0);
14651da177e4SLinus Torvalds 
146655e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14671da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14681da177e4SLinus Torvalds 	hmidi->input = NULL;
14691da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	return 0;
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
147455e957d8STakashi Iwai static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
14751da177e4SLinus Torvalds {
147655e957d8STakashi Iwai 	struct hdsp_midi *hmidi;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	snd_hdsp_midi_output_trigger (substream, 0);
14791da177e4SLinus Torvalds 
148055e957d8STakashi Iwai 	hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14811da177e4SLinus Torvalds 	spin_lock_irq (&hmidi->lock);
14821da177e4SLinus Torvalds 	hmidi->output = NULL;
14831da177e4SLinus Torvalds 	spin_unlock_irq (&hmidi->lock);
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	return 0;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
1488485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_hdsp_midi_output =
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_output_open,
14911da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_output_close,
14921da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_output_trigger,
14931da177e4SLinus Torvalds };
14941da177e4SLinus Torvalds 
1495485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_hdsp_midi_input =
14961da177e4SLinus Torvalds {
14971da177e4SLinus Torvalds 	.open =		snd_hdsp_midi_input_open,
14981da177e4SLinus Torvalds 	.close =	snd_hdsp_midi_input_close,
14991da177e4SLinus Torvalds 	.trigger =	snd_hdsp_midi_input_trigger,
15001da177e4SLinus Torvalds };
15011da177e4SLinus Torvalds 
1502f40b6890STakashi Iwai static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
15031da177e4SLinus Torvalds {
1504a3b3a9dbSDan Carpenter 	char buf[40];
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	hdsp->midi[id].id = id;
15071da177e4SLinus Torvalds 	hdsp->midi[id].rmidi = NULL;
15081da177e4SLinus Torvalds 	hdsp->midi[id].input = NULL;
15091da177e4SLinus Torvalds 	hdsp->midi[id].output = NULL;
15101da177e4SLinus Torvalds 	hdsp->midi[id].hdsp = hdsp;
15111da177e4SLinus Torvalds 	hdsp->midi[id].istimer = 0;
15121da177e4SLinus Torvalds 	hdsp->midi[id].pending = 0;
15131da177e4SLinus Torvalds 	spin_lock_init (&hdsp->midi[id].lock);
15141da177e4SLinus Torvalds 
1515a3b3a9dbSDan Carpenter 	snprintf(buf, sizeof(buf), "%s MIDI %d", card->shortname, id + 1);
1516b0b98119STakashi Iwai 	if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
15171da177e4SLinus Torvalds 		return -1;
15181da177e4SLinus Torvalds 
1519972d4c50SJaroslav Kysela 	sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
15201da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
15231da177e4SLinus Torvalds 	snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
15261da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_INPUT |
15271da177e4SLinus Torvalds 		SNDRV_RAWMIDI_INFO_DUPLEX;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	return 0;
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /*-----------------------------------------------------------------------------
15331da177e4SLinus Torvalds   Control Interface
15341da177e4SLinus Torvalds   ----------------------------------------------------------------------------*/
15351da177e4SLinus Torvalds 
153655e957d8STakashi Iwai static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	u32 val = 0;
15391da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
15401da177e4SLinus Torvalds 	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
15411da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
15421da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
15431da177e4SLinus Torvalds 	else
15441da177e4SLinus Torvalds 		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
15451da177e4SLinus Torvalds 	return val;
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
154855e957d8STakashi Iwai static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
15511da177e4SLinus Torvalds 			 ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
15521da177e4SLinus Torvalds 	if (val & HDSP_SPDIFProfessional)
15531da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
15541da177e4SLinus Torvalds 	else
15551da177e4SLinus Torvalds 		aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
155855e957d8STakashi Iwai static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15591da177e4SLinus Torvalds {
15601da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15611da177e4SLinus Torvalds 	uinfo->count = 1;
15621da177e4SLinus Torvalds 	return 0;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
156555e957d8STakashi Iwai static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15661da177e4SLinus Torvalds {
156755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
15701da177e4SLinus Torvalds 	return 0;
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
157355e957d8STakashi Iwai static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15741da177e4SLinus Torvalds {
157555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15761da177e4SLinus Torvalds 	int change;
15771da177e4SLinus Torvalds 	u32 val;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
15801da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
15811da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif;
15821da177e4SLinus Torvalds 	hdsp->creg_spdif = val;
15831da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
15841da177e4SLinus Torvalds 	return change;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
158755e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15901da177e4SLinus Torvalds 	uinfo->count = 1;
15911da177e4SLinus Torvalds 	return 0;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
159455e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15951da177e4SLinus Torvalds {
159655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
160255e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16031da177e4SLinus Torvalds {
160455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16051da177e4SLinus Torvalds 	int change;
16061da177e4SLinus Torvalds 	u32 val;
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
16091da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16101da177e4SLinus Torvalds 	change = val != hdsp->creg_spdif_stream;
16111da177e4SLinus Torvalds 	hdsp->creg_spdif_stream = val;
16121da177e4SLinus Torvalds 	hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
16131da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
16141da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16151da177e4SLinus Torvalds 	return change;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
161855e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
16211da177e4SLinus Torvalds 	uinfo->count = 1;
16221da177e4SLinus Torvalds 	return 0;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
162555e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = kcontrol->private_value;
16281da177e4SLinus Torvalds 	return 0;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds #define HDSP_SPDIF_IN(xname, xindex) \
163267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
16331da177e4SLinus Torvalds   .name = xname, \
16341da177e4SLinus Torvalds   .index = xindex, \
16351da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_in, \
16361da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_in, \
16371da177e4SLinus Torvalds   .put = snd_hdsp_put_spdif_in }
16381da177e4SLinus Torvalds 
163955e957d8STakashi Iwai static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
164455e957d8STakashi Iwai static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SPDIFInputMask;
16471da177e4SLinus Torvalds 	hdsp->control_register |= hdsp_encode_spdif_in(in);
16481da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16491da177e4SLinus Torvalds 	return 0;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
165255e957d8STakashi Iwai static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
16531da177e4SLinus Torvalds {
16548d678da9STakashi Iwai 	static const char * const texts[4] = {
16558d678da9STakashi Iwai 		"Optical", "Coaxial", "Internal", "AES"
16568d678da9STakashi Iwai 	};
165755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16581da177e4SLinus Torvalds 
16598d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
16608d678da9STakashi Iwai 				 texts);
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
166355e957d8STakashi Iwai static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16641da177e4SLinus Torvalds {
166555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
16681da177e4SLinus Torvalds 	return 0;
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
167155e957d8STakashi Iwai static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16721da177e4SLinus Torvalds {
167355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16741da177e4SLinus Torvalds 	int change;
16751da177e4SLinus Torvalds 	unsigned int val;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
16781da177e4SLinus Torvalds 		return -EBUSY;
16791da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
16801da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
16811da177e4SLinus Torvalds 	change = val != hdsp_spdif_in(hdsp);
16821da177e4SLinus Torvalds 	if (change)
16831da177e4SLinus Torvalds 		hdsp_set_spdif_input(hdsp, val);
16841da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
16851da177e4SLinus Torvalds 	return change;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
168866d9244eSAdrian Knoth #define HDSP_TOGGLE_SETTING(xname, xindex) \
168966d9244eSAdrian Knoth {   .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
169066d9244eSAdrian Knoth 	.name = xname, \
169166d9244eSAdrian Knoth 	.private_value = xindex, \
169266d9244eSAdrian Knoth 	.info = snd_hdsp_info_toggle_setting, \
169366d9244eSAdrian Knoth 	.get = snd_hdsp_get_toggle_setting, \
169466d9244eSAdrian Knoth 	.put = snd_hdsp_put_toggle_setting \
169566d9244eSAdrian Knoth }
169666d9244eSAdrian Knoth 
169766d9244eSAdrian Knoth static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
169866d9244eSAdrian Knoth {
169966d9244eSAdrian Knoth 	return (hdsp->control_register & regmask) ? 1 : 0;
170066d9244eSAdrian Knoth }
170166d9244eSAdrian Knoth 
170266d9244eSAdrian Knoth static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
170366d9244eSAdrian Knoth {
170466d9244eSAdrian Knoth 	if (out)
170566d9244eSAdrian Knoth 		hdsp->control_register |= regmask;
170666d9244eSAdrian Knoth 	else
170766d9244eSAdrian Knoth 		hdsp->control_register &= ~regmask;
170866d9244eSAdrian Knoth 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
170966d9244eSAdrian Knoth 
171066d9244eSAdrian Knoth 	return 0;
171166d9244eSAdrian Knoth }
171266d9244eSAdrian Knoth 
171366d9244eSAdrian Knoth #define snd_hdsp_info_toggle_setting		   snd_ctl_boolean_mono_info
171466d9244eSAdrian Knoth 
171566d9244eSAdrian Knoth static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
171666d9244eSAdrian Knoth 		struct snd_ctl_elem_value *ucontrol)
171766d9244eSAdrian Knoth {
171866d9244eSAdrian Knoth 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
171966d9244eSAdrian Knoth 	u32 regmask = kcontrol->private_value;
172066d9244eSAdrian Knoth 
172166d9244eSAdrian Knoth 	spin_lock_irq(&hdsp->lock);
172266d9244eSAdrian Knoth 	ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
172366d9244eSAdrian Knoth 	spin_unlock_irq(&hdsp->lock);
172466d9244eSAdrian Knoth 	return 0;
172566d9244eSAdrian Knoth }
172666d9244eSAdrian Knoth 
172766d9244eSAdrian Knoth static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
172866d9244eSAdrian Knoth 		struct snd_ctl_elem_value *ucontrol)
172966d9244eSAdrian Knoth {
173066d9244eSAdrian Knoth 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
173166d9244eSAdrian Knoth 	u32 regmask = kcontrol->private_value;
173266d9244eSAdrian Knoth 	int change;
173366d9244eSAdrian Knoth 	unsigned int val;
173466d9244eSAdrian Knoth 
173566d9244eSAdrian Knoth 	if (!snd_hdsp_use_is_exclusive(hdsp))
173666d9244eSAdrian Knoth 		return -EBUSY;
173766d9244eSAdrian Knoth 	val = ucontrol->value.integer.value[0] & 1;
173866d9244eSAdrian Knoth 	spin_lock_irq(&hdsp->lock);
173966d9244eSAdrian Knoth 	change = (int) val != hdsp_toggle_setting(hdsp, regmask);
174066d9244eSAdrian Knoth 	if (change)
174166d9244eSAdrian Knoth 		hdsp_set_toggle_setting(hdsp, regmask, val);
174266d9244eSAdrian Knoth 	spin_unlock_irq(&hdsp->lock);
174366d9244eSAdrian Knoth 	return change;
174466d9244eSAdrian Knoth }
174566d9244eSAdrian Knoth 
17461da177e4SLinus Torvalds #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
174767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
17481da177e4SLinus Torvalds   .name = xname, \
17491da177e4SLinus Torvalds   .index = xindex, \
17501da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
17511da177e4SLinus Torvalds   .info = snd_hdsp_info_spdif_sample_rate, \
17521da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sample_rate \
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
175555e957d8STakashi Iwai static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
17561da177e4SLinus Torvalds {
17578d678da9STakashi Iwai 	static const char * const texts[] = {
17588d678da9STakashi Iwai 		"32000", "44100", "48000", "64000", "88200", "96000",
17598d678da9STakashi Iwai 		"None", "128000", "176400", "192000"
17608d678da9STakashi Iwai 	};
176155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17621da177e4SLinus Torvalds 
17638d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
17648d678da9STakashi Iwai 				 texts);
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
176755e957d8STakashi Iwai static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17681da177e4SLinus Torvalds {
176955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	switch (hdsp_spdif_sample_rate(hdsp)) {
17721da177e4SLinus Torvalds 	case 32000:
17731da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
17741da177e4SLinus Torvalds 		break;
17751da177e4SLinus Torvalds 	case 44100:
17761da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
17771da177e4SLinus Torvalds 		break;
17781da177e4SLinus Torvalds 	case 48000:
17791da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
17801da177e4SLinus Torvalds 		break;
17811da177e4SLinus Torvalds 	case 64000:
17821da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
17831da177e4SLinus Torvalds 		break;
17841da177e4SLinus Torvalds 	case 88200:
17851da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
17861da177e4SLinus Torvalds 		break;
17871da177e4SLinus Torvalds 	case 96000:
17881da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
17891da177e4SLinus Torvalds 		break;
17901da177e4SLinus Torvalds 	case 128000:
17911da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
17921da177e4SLinus Torvalds 		break;
17931da177e4SLinus Torvalds 	case 176400:
17941da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
17951da177e4SLinus Torvalds 		break;
17961da177e4SLinus Torvalds 	case 192000:
17971da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
17981da177e4SLinus Torvalds 		break;
17991da177e4SLinus Torvalds 	default:
18001da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 	return 0;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
180667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18071da177e4SLinus Torvalds   .name = xname, \
18081da177e4SLinus Torvalds   .index = xindex, \
18091da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18101da177e4SLinus Torvalds   .info = snd_hdsp_info_system_sample_rate, \
18111da177e4SLinus Torvalds   .get = snd_hdsp_get_system_sample_rate \
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
181455e957d8STakashi Iwai static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18171da177e4SLinus Torvalds 	uinfo->count = 1;
18181da177e4SLinus Torvalds 	return 0;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
182155e957d8STakashi Iwai static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18221da177e4SLinus Torvalds {
182355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
18261da177e4SLinus Torvalds 	return 0;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
183067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18311da177e4SLinus Torvalds   .name = xname, \
18321da177e4SLinus Torvalds   .index = xindex, \
18331da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18341da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_sample_rate, \
18351da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_sample_rate \
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds 
183855e957d8STakashi Iwai static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18391da177e4SLinus Torvalds {
184055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18418d678da9STakashi Iwai 	static const char * const texts[] = {
18428d678da9STakashi Iwai 		"32000", "44100", "48000", "64000", "88200", "96000",
18438d678da9STakashi Iwai 		"None", "128000", "176400", "192000"
18448d678da9STakashi Iwai 	};
18458d678da9STakashi Iwai 
18468d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
18478d678da9STakashi Iwai 				 texts);
18481da177e4SLinus Torvalds }
18491da177e4SLinus Torvalds 
185055e957d8STakashi Iwai static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18511da177e4SLinus Torvalds {
185255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	switch (hdsp_external_sample_rate(hdsp)) {
18551da177e4SLinus Torvalds 	case 32000:
18561da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 0;
18571da177e4SLinus Torvalds 		break;
18581da177e4SLinus Torvalds 	case 44100:
18591da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 1;
18601da177e4SLinus Torvalds 		break;
18611da177e4SLinus Torvalds 	case 48000:
18621da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 2;
18631da177e4SLinus Torvalds 		break;
18641da177e4SLinus Torvalds 	case 64000:
18651da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 3;
18661da177e4SLinus Torvalds 		break;
18671da177e4SLinus Torvalds 	case 88200:
18681da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 4;
18691da177e4SLinus Torvalds 		break;
18701da177e4SLinus Torvalds 	case 96000:
18711da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 5;
18721da177e4SLinus Torvalds 		break;
18731da177e4SLinus Torvalds 	case 128000:
18741da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 7;
18751da177e4SLinus Torvalds 		break;
18761da177e4SLinus Torvalds 	case 176400:
18771da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 8;
18781da177e4SLinus Torvalds 		break;
18791da177e4SLinus Torvalds 	case 192000:
18801da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 9;
18811da177e4SLinus Torvalds 		break;
18821da177e4SLinus Torvalds 	default:
18831da177e4SLinus Torvalds 		ucontrol->value.enumerated.item[0] = 6;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 	return 0;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
188967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18901da177e4SLinus Torvalds   .name = xname, \
18911da177e4SLinus Torvalds   .index = xindex, \
18921da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18931da177e4SLinus Torvalds   .info = snd_hdsp_info_system_clock_mode, \
18941da177e4SLinus Torvalds   .get = snd_hdsp_get_system_clock_mode \
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
189755e957d8STakashi Iwai static int hdsp_system_clock_mode(struct hdsp *hdsp)
18981da177e4SLinus Torvalds {
1899b0b98119STakashi Iwai 	if (hdsp->control_register & HDSP_ClockModeMaster)
19001da177e4SLinus Torvalds 		return 0;
1901b0b98119STakashi Iwai 	else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
19021da177e4SLinus Torvalds 			return 0;
19031da177e4SLinus Torvalds 	return 1;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
190655e957d8STakashi Iwai static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19071da177e4SLinus Torvalds {
19088d678da9STakashi Iwai 	static const char * const texts[] = {"Master", "Slave" };
19091da177e4SLinus Torvalds 
19108d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
191355e957d8STakashi Iwai static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19141da177e4SLinus Torvalds {
191555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE(xname, xindex) \
192267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19231da177e4SLinus Torvalds   .name = xname, \
19241da177e4SLinus Torvalds   .index = xindex, \
19251da177e4SLinus Torvalds   .info = snd_hdsp_info_clock_source, \
19261da177e4SLinus Torvalds   .get = snd_hdsp_get_clock_source, \
19271da177e4SLinus Torvalds   .put = snd_hdsp_put_clock_source \
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
193055e957d8STakashi Iwai static int hdsp_clock_source(struct hdsp *hdsp)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	if (hdsp->control_register & HDSP_ClockModeMaster) {
19331da177e4SLinus Torvalds 		switch (hdsp->system_sample_rate) {
19341da177e4SLinus Torvalds 		case 32000:
19351da177e4SLinus Torvalds 			return 1;
19361da177e4SLinus Torvalds 		case 44100:
19371da177e4SLinus Torvalds 			return 2;
19381da177e4SLinus Torvalds 		case 48000:
19391da177e4SLinus Torvalds 			return 3;
19401da177e4SLinus Torvalds 		case 64000:
19411da177e4SLinus Torvalds 			return 4;
19421da177e4SLinus Torvalds 		case 88200:
19431da177e4SLinus Torvalds 			return 5;
19441da177e4SLinus Torvalds 		case 96000:
19451da177e4SLinus Torvalds 			return 6;
19461da177e4SLinus Torvalds 		case 128000:
19471da177e4SLinus Torvalds 			return 7;
19481da177e4SLinus Torvalds 		case 176400:
19491da177e4SLinus Torvalds 			return 8;
19501da177e4SLinus Torvalds 		case 192000:
19511da177e4SLinus Torvalds 			return 9;
19521da177e4SLinus Torvalds 		default:
19531da177e4SLinus Torvalds 			return 3;
19541da177e4SLinus Torvalds 		}
19551da177e4SLinus Torvalds 	} else {
19561da177e4SLinus Torvalds 		return 0;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
196055e957d8STakashi Iwai static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
19611da177e4SLinus Torvalds {
19621da177e4SLinus Torvalds 	int rate;
19631da177e4SLinus Torvalds 	switch (mode) {
19641da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
19651da177e4SLinus Torvalds 		if (hdsp_external_sample_rate(hdsp) != 0) {
19661da177e4SLinus Torvalds 		    if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
19671da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_ClockModeMaster;
19681da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
19691da177e4SLinus Torvalds 			return 0;
19701da177e4SLinus Torvalds 		    }
19711da177e4SLinus Torvalds 		}
19721da177e4SLinus Torvalds 		return -1;
19731da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
19741da177e4SLinus Torvalds 		rate = 32000;
19751da177e4SLinus Torvalds 		break;
19761da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
19771da177e4SLinus Torvalds 		rate = 44100;
19781da177e4SLinus Torvalds 		break;
19791da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
19801da177e4SLinus Torvalds 		rate = 48000;
19811da177e4SLinus Torvalds 		break;
19821da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
19831da177e4SLinus Torvalds 		rate = 64000;
19841da177e4SLinus Torvalds 		break;
19851da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
19861da177e4SLinus Torvalds 		rate = 88200;
19871da177e4SLinus Torvalds 		break;
19881da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
19891da177e4SLinus Torvalds 		rate = 96000;
19901da177e4SLinus Torvalds 		break;
19911da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
19921da177e4SLinus Torvalds 		rate = 128000;
19931da177e4SLinus Torvalds 		break;
19941da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
19951da177e4SLinus Torvalds 		rate = 176400;
19961da177e4SLinus Torvalds 		break;
19971da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
19981da177e4SLinus Torvalds 		rate = 192000;
19991da177e4SLinus Torvalds 		break;
20001da177e4SLinus Torvalds 	default:
20011da177e4SLinus Torvalds 		rate = 48000;
20021da177e4SLinus Torvalds 	}
20031da177e4SLinus Torvalds 	hdsp->control_register |= HDSP_ClockModeMaster;
20041da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20051da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, rate, 1);
20061da177e4SLinus Torvalds 	return 0;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
200955e957d8STakashi Iwai static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20101da177e4SLinus Torvalds {
20118d678da9STakashi Iwai 	static const char * const texts[] = {
20128d678da9STakashi Iwai 		"AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
20138d678da9STakashi Iwai 		"Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
20148d678da9STakashi Iwai 		"Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
20158d678da9STakashi Iwai 		"Internal 192.0 KHz"
20168d678da9STakashi Iwai 	};
201755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20181da177e4SLinus Torvalds 
20198d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
20208d678da9STakashi Iwai 				 texts);
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
202355e957d8STakashi Iwai static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20241da177e4SLinus Torvalds {
202555e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
20281da177e4SLinus Torvalds 	return 0;
20291da177e4SLinus Torvalds }
20301da177e4SLinus Torvalds 
203155e957d8STakashi Iwai static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20321da177e4SLinus Torvalds {
203355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20341da177e4SLinus Torvalds 	int change;
20351da177e4SLinus Torvalds 	int val;
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
20381da177e4SLinus Torvalds 		return -EBUSY;
20391da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
20401da177e4SLinus Torvalds 	if (val < 0) val = 0;
20411da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
2042b0b98119STakashi Iwai 		if (val > 9)
2043b0b98119STakashi Iwai 			val = 9;
20441da177e4SLinus Torvalds 	} else {
2045b0b98119STakashi Iwai 		if (val > 6)
2046b0b98119STakashi Iwai 			val = 6;
20471da177e4SLinus Torvalds 	}
20481da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2049b0b98119STakashi Iwai 	if (val != hdsp_clock_source(hdsp))
20501da177e4SLinus Torvalds 		change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2051b0b98119STakashi Iwai 	else
20521da177e4SLinus Torvalds 		change = 0;
20531da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
20541da177e4SLinus Torvalds 	return change;
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
2057a5ce8890STakashi Iwai #define snd_hdsp_info_clock_source_lock		snd_ctl_boolean_mono_info
2058e3ea4d89STakashi Iwai 
205955e957d8STakashi Iwai static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2060e3ea4d89STakashi Iwai {
206155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2062e3ea4d89STakashi Iwai 
2063e3ea4d89STakashi Iwai 	ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2064e3ea4d89STakashi Iwai 	return 0;
2065e3ea4d89STakashi Iwai }
2066e3ea4d89STakashi Iwai 
206755e957d8STakashi Iwai static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2068e3ea4d89STakashi Iwai {
206955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2070e3ea4d89STakashi Iwai 	int change;
2071e3ea4d89STakashi Iwai 
2072e3ea4d89STakashi Iwai 	change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2073e3ea4d89STakashi Iwai 	if (change)
20744e98d6a7STakashi Iwai 		hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2075e3ea4d89STakashi Iwai 	return change;
2076e3ea4d89STakashi Iwai }
2077e3ea4d89STakashi Iwai 
20781da177e4SLinus Torvalds #define HDSP_DA_GAIN(xname, xindex) \
207967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
20801da177e4SLinus Torvalds   .name = xname, \
20811da177e4SLinus Torvalds   .index = xindex, \
20821da177e4SLinus Torvalds   .info = snd_hdsp_info_da_gain, \
20831da177e4SLinus Torvalds   .get = snd_hdsp_get_da_gain, \
20841da177e4SLinus Torvalds   .put = snd_hdsp_put_da_gain \
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
208755e957d8STakashi Iwai static int hdsp_da_gain(struct hdsp *hdsp)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_DAGainMask) {
20901da177e4SLinus Torvalds 	case HDSP_DAGainHighGain:
20911da177e4SLinus Torvalds 		return 0;
20921da177e4SLinus Torvalds 	case HDSP_DAGainPlus4dBu:
20931da177e4SLinus Torvalds 		return 1;
20941da177e4SLinus Torvalds 	case HDSP_DAGainMinus10dBV:
20951da177e4SLinus Torvalds 		return 2;
20961da177e4SLinus Torvalds 	default:
20971da177e4SLinus Torvalds 		return 1;
20981da177e4SLinus Torvalds 	}
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
210155e957d8STakashi Iwai static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_DAGainMask;
21041da177e4SLinus Torvalds 	switch (mode) {
21051da177e4SLinus Torvalds 	case 0:
21061da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainHighGain;
21071da177e4SLinus Torvalds 		break;
21081da177e4SLinus Torvalds 	case 1:
21091da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainPlus4dBu;
21101da177e4SLinus Torvalds 		break;
21111da177e4SLinus Torvalds 	case 2:
21121da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_DAGainMinus10dBV;
21131da177e4SLinus Torvalds 		break;
21141da177e4SLinus Torvalds 	default:
21151da177e4SLinus Torvalds 		return -1;
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	}
21181da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21191da177e4SLinus Torvalds 	return 0;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
212255e957d8STakashi Iwai static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21231da177e4SLinus Torvalds {
21248d678da9STakashi Iwai 	static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
21251da177e4SLinus Torvalds 
21268d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
212955e957d8STakashi Iwai static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21301da177e4SLinus Torvalds {
213155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
21341da177e4SLinus Torvalds 	return 0;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
213755e957d8STakashi Iwai static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21381da177e4SLinus Torvalds {
213955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21401da177e4SLinus Torvalds 	int change;
21411da177e4SLinus Torvalds 	int val;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
21441da177e4SLinus Torvalds 		return -EBUSY;
21451da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
21461da177e4SLinus Torvalds 	if (val < 0) val = 0;
21471da177e4SLinus Torvalds 	if (val > 2) val = 2;
21481da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2149b0b98119STakashi Iwai 	if (val != hdsp_da_gain(hdsp))
21501da177e4SLinus Torvalds 		change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2151b0b98119STakashi Iwai 	else
21521da177e4SLinus Torvalds 		change = 0;
21531da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
21541da177e4SLinus Torvalds 	return change;
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds #define HDSP_AD_GAIN(xname, xindex) \
215867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21591da177e4SLinus Torvalds   .name = xname, \
21601da177e4SLinus Torvalds   .index = xindex, \
21611da177e4SLinus Torvalds   .info = snd_hdsp_info_ad_gain, \
21621da177e4SLinus Torvalds   .get = snd_hdsp_get_ad_gain, \
21631da177e4SLinus Torvalds   .put = snd_hdsp_put_ad_gain \
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
216655e957d8STakashi Iwai static int hdsp_ad_gain(struct hdsp *hdsp)
21671da177e4SLinus Torvalds {
21681da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_ADGainMask) {
21691da177e4SLinus Torvalds 	case HDSP_ADGainMinus10dBV:
21701da177e4SLinus Torvalds 		return 0;
21711da177e4SLinus Torvalds 	case HDSP_ADGainPlus4dBu:
21721da177e4SLinus Torvalds 		return 1;
21731da177e4SLinus Torvalds 	case HDSP_ADGainLowGain:
21741da177e4SLinus Torvalds 		return 2;
21751da177e4SLinus Torvalds 	default:
21761da177e4SLinus Torvalds 		return 1;
21771da177e4SLinus Torvalds 	}
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
218055e957d8STakashi Iwai static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
21811da177e4SLinus Torvalds {
21821da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_ADGainMask;
21831da177e4SLinus Torvalds 	switch (mode) {
21841da177e4SLinus Torvalds 	case 0:
21851da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainMinus10dBV;
21861da177e4SLinus Torvalds 		break;
21871da177e4SLinus Torvalds 	case 1:
21881da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainPlus4dBu;
21891da177e4SLinus Torvalds 		break;
21901da177e4SLinus Torvalds 	case 2:
21911da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_ADGainLowGain;
21921da177e4SLinus Torvalds 		break;
21931da177e4SLinus Torvalds 	default:
21941da177e4SLinus Torvalds 		return -1;
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21981da177e4SLinus Torvalds 	return 0;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
220155e957d8STakashi Iwai static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22021da177e4SLinus Torvalds {
22038d678da9STakashi Iwai 	static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
22041da177e4SLinus Torvalds 
22058d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
220855e957d8STakashi Iwai static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22091da177e4SLinus Torvalds {
221055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
22131da177e4SLinus Torvalds 	return 0;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
221655e957d8STakashi Iwai static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22171da177e4SLinus Torvalds {
221855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22191da177e4SLinus Torvalds 	int change;
22201da177e4SLinus Torvalds 	int val;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
22231da177e4SLinus Torvalds 		return -EBUSY;
22241da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
22251da177e4SLinus Torvalds 	if (val < 0) val = 0;
22261da177e4SLinus Torvalds 	if (val > 2) val = 2;
22271da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2228b0b98119STakashi Iwai 	if (val != hdsp_ad_gain(hdsp))
22291da177e4SLinus Torvalds 		change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2230b0b98119STakashi Iwai 	else
22311da177e4SLinus Torvalds 		change = 0;
22321da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
22331da177e4SLinus Torvalds 	return change;
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds #define HDSP_PHONE_GAIN(xname, xindex) \
223767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
22381da177e4SLinus Torvalds   .name = xname, \
22391da177e4SLinus Torvalds   .index = xindex, \
22401da177e4SLinus Torvalds   .info = snd_hdsp_info_phone_gain, \
22411da177e4SLinus Torvalds   .get = snd_hdsp_get_phone_gain, \
22421da177e4SLinus Torvalds   .put = snd_hdsp_put_phone_gain \
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
224555e957d8STakashi Iwai static int hdsp_phone_gain(struct hdsp *hdsp)
22461da177e4SLinus Torvalds {
22471da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_PhoneGainMask) {
22481da177e4SLinus Torvalds 	case HDSP_PhoneGain0dB:
22491da177e4SLinus Torvalds 		return 0;
22501da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus6dB:
22511da177e4SLinus Torvalds 		return 1;
22521da177e4SLinus Torvalds 	case HDSP_PhoneGainMinus12dB:
22531da177e4SLinus Torvalds 		return 2;
22541da177e4SLinus Torvalds 	default:
22551da177e4SLinus Torvalds 		return 0;
22561da177e4SLinus Torvalds 	}
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
225955e957d8STakashi Iwai static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
22601da177e4SLinus Torvalds {
22611da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_PhoneGainMask;
22621da177e4SLinus Torvalds 	switch (mode) {
22631da177e4SLinus Torvalds 	case 0:
22641da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGain0dB;
22651da177e4SLinus Torvalds 		break;
22661da177e4SLinus Torvalds 	case 1:
22671da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus6dB;
22681da177e4SLinus Torvalds 		break;
22691da177e4SLinus Torvalds 	case 2:
22701da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_PhoneGainMinus12dB;
22711da177e4SLinus Torvalds 		break;
22721da177e4SLinus Torvalds 	default:
22731da177e4SLinus Torvalds 		return -1;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	}
22761da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22771da177e4SLinus Torvalds 	return 0;
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
228055e957d8STakashi Iwai static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22811da177e4SLinus Torvalds {
22828d678da9STakashi Iwai 	static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
22831da177e4SLinus Torvalds 
22848d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
228755e957d8STakashi Iwai static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22881da177e4SLinus Torvalds {
228955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
22921da177e4SLinus Torvalds 	return 0;
22931da177e4SLinus Torvalds }
22941da177e4SLinus Torvalds 
229555e957d8STakashi Iwai static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22961da177e4SLinus Torvalds {
229755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22981da177e4SLinus Torvalds 	int change;
22991da177e4SLinus Torvalds 	int val;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
23021da177e4SLinus Torvalds 		return -EBUSY;
23031da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0];
23041da177e4SLinus Torvalds 	if (val < 0) val = 0;
23051da177e4SLinus Torvalds 	if (val > 2) val = 2;
23061da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
2307b0b98119STakashi Iwai 	if (val != hdsp_phone_gain(hdsp))
23081da177e4SLinus Torvalds 		change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2309b0b98119STakashi Iwai 	else
23101da177e4SLinus Torvalds 		change = 0;
23111da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
23121da177e4SLinus Torvalds 	return change;
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds #define HDSP_PREF_SYNC_REF(xname, xindex) \
231667ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23171da177e4SLinus Torvalds   .name = xname, \
23181da177e4SLinus Torvalds   .index = xindex, \
23191da177e4SLinus Torvalds   .info = snd_hdsp_info_pref_sync_ref, \
23201da177e4SLinus Torvalds   .get = snd_hdsp_get_pref_sync_ref, \
23211da177e4SLinus Torvalds   .put = snd_hdsp_put_pref_sync_ref \
23221da177e4SLinus Torvalds }
23231da177e4SLinus Torvalds 
232455e957d8STakashi Iwai static int hdsp_pref_sync_ref(struct hdsp *hdsp)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	/* Notice that this looks at the requested sync source,
23271da177e4SLinus Torvalds 	   not the one actually in use.
23281da177e4SLinus Torvalds 	*/
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds 	switch (hdsp->control_register & HDSP_SyncRefMask) {
23311da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT1:
23321da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT1;
23331da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT2:
23341da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT2;
23351da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT3:
23361da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT3;
23371da177e4SLinus Torvalds 	case HDSP_SyncRef_SPDIF:
23381da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_SPDIF;
23391da177e4SLinus Torvalds 	case HDSP_SyncRef_WORD:
23401da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
23411da177e4SLinus Torvalds 	case HDSP_SyncRef_ADAT_SYNC:
23421da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_ADAT_SYNC;
23431da177e4SLinus Torvalds 	default:
23441da177e4SLinus Torvalds 		return HDSP_SYNC_FROM_WORD;
23451da177e4SLinus Torvalds 	}
23461da177e4SLinus Torvalds 	return 0;
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
234955e957d8STakashi Iwai static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
23501da177e4SLinus Torvalds {
23511da177e4SLinus Torvalds 	hdsp->control_register &= ~HDSP_SyncRefMask;
23521da177e4SLinus Torvalds 	switch (pref) {
23531da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
23541da177e4SLinus Torvalds 		hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
23551da177e4SLinus Torvalds 		break;
23561da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
23571da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT2;
23581da177e4SLinus Torvalds 		break;
23591da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
23601da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT3;
23611da177e4SLinus Torvalds 		break;
23621da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
23631da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_SPDIF;
23641da177e4SLinus Torvalds 		break;
23651da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
23661da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_WORD;
23671da177e4SLinus Torvalds 		break;
23681da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
23691da177e4SLinus Torvalds 		hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
23701da177e4SLinus Torvalds 		break;
23711da177e4SLinus Torvalds 	default:
23721da177e4SLinus Torvalds 		return -1;
23731da177e4SLinus Torvalds 	}
23741da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23751da177e4SLinus Torvalds 	return 0;
23761da177e4SLinus Torvalds }
23771da177e4SLinus Torvalds 
237855e957d8STakashi Iwai static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23791da177e4SLinus Torvalds {
23808d678da9STakashi Iwai 	static const char * const texts[] = {
23818d678da9STakashi Iwai 		"Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
23828d678da9STakashi Iwai 	};
238355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23848d678da9STakashi Iwai 	int num_items;
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	switch (hdsp->io_type) {
23871da177e4SLinus Torvalds 	case Digiface:
23881da177e4SLinus Torvalds 	case H9652:
23898d678da9STakashi Iwai 		num_items = 6;
23901da177e4SLinus Torvalds 		break;
23911da177e4SLinus Torvalds 	case Multiface:
23928d678da9STakashi Iwai 		num_items = 4;
23931da177e4SLinus Torvalds 		break;
23941da177e4SLinus Torvalds 	case H9632:
23958d678da9STakashi Iwai 		num_items = 3;
23961da177e4SLinus Torvalds 		break;
23971da177e4SLinus Torvalds 	default:
23989badda0aSTakashi Iwai 		return -EINVAL;
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 
24018d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, num_items, texts);
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
240455e957d8STakashi Iwai static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24051da177e4SLinus Torvalds {
240655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
24091da177e4SLinus Torvalds 	return 0;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
241255e957d8STakashi Iwai static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24131da177e4SLinus Torvalds {
241455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24151da177e4SLinus Torvalds 	int change, max;
24161da177e4SLinus Torvalds 	unsigned int val;
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
24191da177e4SLinus Torvalds 		return -EBUSY;
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	switch (hdsp->io_type) {
24221da177e4SLinus Torvalds 	case Digiface:
24231da177e4SLinus Torvalds 	case H9652:
24241da177e4SLinus Torvalds 		max = 6;
24251da177e4SLinus Torvalds 		break;
24261da177e4SLinus Torvalds 	case Multiface:
24271da177e4SLinus Torvalds 		max = 4;
24281da177e4SLinus Torvalds 		break;
24291da177e4SLinus Torvalds 	case H9632:
24301da177e4SLinus Torvalds 		max = 3;
24311da177e4SLinus Torvalds 		break;
24321da177e4SLinus Torvalds 	default:
24331da177e4SLinus Torvalds 		return -EIO;
24341da177e4SLinus Torvalds 	}
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	val = ucontrol->value.enumerated.item[0] % max;
24371da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
24381da177e4SLinus Torvalds 	change = (int)val != hdsp_pref_sync_ref(hdsp);
24391da177e4SLinus Torvalds 	hdsp_set_pref_sync_ref(hdsp, val);
24401da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
24411da177e4SLinus Torvalds 	return change;
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds #define HDSP_AUTOSYNC_REF(xname, xindex) \
244567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24461da177e4SLinus Torvalds   .name = xname, \
24471da177e4SLinus Torvalds   .index = xindex, \
24481da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ, \
24491da177e4SLinus Torvalds   .info = snd_hdsp_info_autosync_ref, \
24501da177e4SLinus Torvalds   .get = snd_hdsp_get_autosync_ref, \
24511da177e4SLinus Torvalds }
24521da177e4SLinus Torvalds 
245355e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp)
24541da177e4SLinus Torvalds {
24551da177e4SLinus Torvalds 	/* This looks at the autosync selected sync reference */
24561da177e4SLinus Torvalds 	unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	switch (status2 & HDSP_SelSyncRefMask) {
24591da177e4SLinus Torvalds 	case HDSP_SelSyncRef_WORD:
24601da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
24611da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT_SYNC:
24621da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
24631da177e4SLinus Torvalds 	case HDSP_SelSyncRef_SPDIF:
24641da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_SPDIF;
24651da177e4SLinus Torvalds 	case HDSP_SelSyncRefMask:
24661da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_NONE;
24671da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT1:
24681da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT1;
24691da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT2:
24701da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT2;
24711da177e4SLinus Torvalds 	case HDSP_SelSyncRef_ADAT3:
24721da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_ADAT3;
24731da177e4SLinus Torvalds 	default:
24741da177e4SLinus Torvalds 		return HDSP_AUTOSYNC_FROM_WORD;
24751da177e4SLinus Torvalds 	}
24761da177e4SLinus Torvalds 	return 0;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds 
247955e957d8STakashi Iwai static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
24801da177e4SLinus Torvalds {
24818d678da9STakashi Iwai 	static const char * const texts[] = {
24828d678da9STakashi Iwai 		"Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
24838d678da9STakashi Iwai 	};
24841da177e4SLinus Torvalds 
24858d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 7, texts);
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds 
248855e957d8STakashi Iwai static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24891da177e4SLinus Torvalds {
249055e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
24931da177e4SLinus Torvalds 	return 0;
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds #define HDSP_PRECISE_POINTER(xname, xindex) \
249767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
24981da177e4SLinus Torvalds   .name = xname, \
24991da177e4SLinus Torvalds   .index = xindex, \
25001da177e4SLinus Torvalds   .info = snd_hdsp_info_precise_pointer, \
25011da177e4SLinus Torvalds   .get = snd_hdsp_get_precise_pointer, \
25021da177e4SLinus Torvalds   .put = snd_hdsp_put_precise_pointer \
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
250555e957d8STakashi Iwai static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
25061da177e4SLinus Torvalds {
2507b0b98119STakashi Iwai 	if (precise)
25081da177e4SLinus Torvalds 		hdsp->precise_ptr = 1;
2509b0b98119STakashi Iwai 	else
25101da177e4SLinus Torvalds 		hdsp->precise_ptr = 0;
25111da177e4SLinus Torvalds 	return 0;
25121da177e4SLinus Torvalds }
25131da177e4SLinus Torvalds 
2514a5ce8890STakashi Iwai #define snd_hdsp_info_precise_pointer		snd_ctl_boolean_mono_info
25151da177e4SLinus Torvalds 
251655e957d8STakashi Iwai static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25171da177e4SLinus Torvalds {
251855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
25211da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = hdsp->precise_ptr;
25221da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
25231da177e4SLinus Torvalds 	return 0;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
252655e957d8STakashi Iwai static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25271da177e4SLinus Torvalds {
252855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25291da177e4SLinus Torvalds 	int change;
25301da177e4SLinus Torvalds 	unsigned int val;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
25331da177e4SLinus Torvalds 		return -EBUSY;
25341da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
25351da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
25361da177e4SLinus Torvalds 	change = (int)val != hdsp->precise_ptr;
25371da177e4SLinus Torvalds 	hdsp_set_precise_pointer(hdsp, val);
25381da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
25391da177e4SLinus Torvalds 	return change;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
25424adab848STakashi Iwai #define HDSP_USE_MIDI_WORK(xname, xindex) \
254367ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
25441da177e4SLinus Torvalds   .name = xname, \
25451da177e4SLinus Torvalds   .index = xindex, \
25464adab848STakashi Iwai   .info = snd_hdsp_info_use_midi_work, \
25474adab848STakashi Iwai   .get = snd_hdsp_get_use_midi_work, \
25484adab848STakashi Iwai   .put = snd_hdsp_put_use_midi_work \
25491da177e4SLinus Torvalds }
25501da177e4SLinus Torvalds 
25514adab848STakashi Iwai static int hdsp_set_use_midi_work(struct hdsp *hdsp, int use_work)
25521da177e4SLinus Torvalds {
25534adab848STakashi Iwai 	if (use_work)
25544adab848STakashi Iwai 		hdsp->use_midi_work = 1;
2555b0b98119STakashi Iwai 	else
25564adab848STakashi Iwai 		hdsp->use_midi_work = 0;
25571da177e4SLinus Torvalds 	return 0;
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25604adab848STakashi Iwai #define snd_hdsp_info_use_midi_work		snd_ctl_boolean_mono_info
25611da177e4SLinus Torvalds 
25624adab848STakashi Iwai static int snd_hdsp_get_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25631da177e4SLinus Torvalds {
256455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
25674adab848STakashi Iwai 	ucontrol->value.integer.value[0] = hdsp->use_midi_work;
25681da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
25691da177e4SLinus Torvalds 	return 0;
25701da177e4SLinus Torvalds }
25711da177e4SLinus Torvalds 
25724adab848STakashi Iwai static int snd_hdsp_put_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25731da177e4SLinus Torvalds {
257455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25751da177e4SLinus Torvalds 	int change;
25761da177e4SLinus Torvalds 	unsigned int val;
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
25791da177e4SLinus Torvalds 		return -EBUSY;
25801da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & 1;
25811da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
25824adab848STakashi Iwai 	change = (int)val != hdsp->use_midi_work;
25834adab848STakashi Iwai 	hdsp_set_use_midi_work(hdsp, val);
25841da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
25851da177e4SLinus Torvalds 	return change;
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds #define HDSP_MIXER(xname, xindex) \
25891da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
25901da177e4SLinus Torvalds   .name = xname, \
25911da177e4SLinus Torvalds   .index = xindex, \
259267ed4161SClemens Ladisch   .device = 0, \
25931da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
25941da177e4SLinus Torvalds 		 SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
25951da177e4SLinus Torvalds   .info = snd_hdsp_info_mixer, \
25961da177e4SLinus Torvalds   .get = snd_hdsp_get_mixer, \
25971da177e4SLinus Torvalds   .put = snd_hdsp_put_mixer \
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
260055e957d8STakashi Iwai static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26011da177e4SLinus Torvalds {
26021da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
26031da177e4SLinus Torvalds 	uinfo->count = 3;
26041da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
26051da177e4SLinus Torvalds 	uinfo->value.integer.max = 65536;
26061da177e4SLinus Torvalds 	uinfo->value.integer.step = 1;
26071da177e4SLinus Torvalds 	return 0;
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
261055e957d8STakashi Iwai static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26111da177e4SLinus Torvalds {
261255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26131da177e4SLinus Torvalds 	int source;
26141da177e4SLinus Torvalds 	int destination;
26151da177e4SLinus Torvalds 	int addr;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
26181da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
26191da177e4SLinus Torvalds 
2620b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
26211da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2622b0b98119STakashi Iwai 	else
26231da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
26261da177e4SLinus Torvalds 	ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
26271da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
26281da177e4SLinus Torvalds 	return 0;
26291da177e4SLinus Torvalds }
26301da177e4SLinus Torvalds 
263155e957d8STakashi Iwai static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26321da177e4SLinus Torvalds {
263355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26341da177e4SLinus Torvalds 	int change;
26351da177e4SLinus Torvalds 	int source;
26361da177e4SLinus Torvalds 	int destination;
26371da177e4SLinus Torvalds 	int gain;
26381da177e4SLinus Torvalds 	int addr;
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	if (!snd_hdsp_use_is_exclusive(hdsp))
26411da177e4SLinus Torvalds 		return -EBUSY;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	source = ucontrol->value.integer.value[0];
26441da177e4SLinus Torvalds 	destination = ucontrol->value.integer.value[1];
26451da177e4SLinus Torvalds 
2646b0b98119STakashi Iwai 	if (source >= hdsp->max_channels)
26471da177e4SLinus Torvalds 		addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2648b0b98119STakashi Iwai 	else
26491da177e4SLinus Torvalds 		addr = hdsp_input_to_output_key(hdsp,source, destination);
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	gain = ucontrol->value.integer.value[2];
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
26541da177e4SLinus Torvalds 	change = gain != hdsp_read_gain(hdsp, addr);
26551da177e4SLinus Torvalds 	if (change)
26561da177e4SLinus Torvalds 		hdsp_write_gain(hdsp, addr, gain);
26571da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
26581da177e4SLinus Torvalds 	return change;
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds #define HDSP_WC_SYNC_CHECK(xname, xindex) \
266267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26631da177e4SLinus Torvalds   .name = xname, \
26641da177e4SLinus Torvalds   .index = xindex, \
26651da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
26661da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
26671da177e4SLinus Torvalds   .get = snd_hdsp_get_wc_sync_check \
26681da177e4SLinus Torvalds }
26691da177e4SLinus Torvalds 
267055e957d8STakashi Iwai static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26711da177e4SLinus Torvalds {
26728d678da9STakashi Iwai 	static const char * const texts[] = {"No Lock", "Lock", "Sync" };
26738d678da9STakashi Iwai 
26748d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 3, texts);
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
267755e957d8STakashi Iwai static int hdsp_wc_sync_check(struct hdsp *hdsp)
26781da177e4SLinus Torvalds {
26791da177e4SLinus Torvalds 	int status2 = hdsp_read(hdsp, HDSP_status2Register);
26801da177e4SLinus Torvalds 	if (status2 & HDSP_wc_lock) {
2681b0b98119STakashi Iwai 		if (status2 & HDSP_wc_sync)
26821da177e4SLinus Torvalds 			return 2;
2683b0b98119STakashi Iwai 		else
26841da177e4SLinus Torvalds 			 return 1;
2685b0b98119STakashi Iwai 	} else
26861da177e4SLinus Torvalds 		return 0;
26871da177e4SLinus Torvalds 	return 0;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
269055e957d8STakashi Iwai static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26911da177e4SLinus Torvalds {
269255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
26951da177e4SLinus Torvalds 	return 0;
26961da177e4SLinus Torvalds }
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
269967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27001da177e4SLinus Torvalds   .name = xname, \
27011da177e4SLinus Torvalds   .index = xindex, \
27021da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27031da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
27041da177e4SLinus Torvalds   .get = snd_hdsp_get_spdif_sync_check \
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
270755e957d8STakashi Iwai static int hdsp_spdif_sync_check(struct hdsp *hdsp)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
2710b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
27111da177e4SLinus Torvalds 		return 0;
2712b0b98119STakashi Iwai 	else {
2713b0b98119STakashi Iwai 		if (status & HDSP_SPDIFSync)
27141da177e4SLinus Torvalds 			return 2;
2715b0b98119STakashi Iwai 		else
27161da177e4SLinus Torvalds 			return 1;
27171da177e4SLinus Torvalds 	}
27181da177e4SLinus Torvalds 	return 0;
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
272155e957d8STakashi Iwai static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27221da177e4SLinus Torvalds {
272355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
27261da177e4SLinus Torvalds 	return 0;
27271da177e4SLinus Torvalds }
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
273067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27311da177e4SLinus Torvalds   .name = xname, \
27321da177e4SLinus Torvalds   .index = xindex, \
27331da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27341da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
27351da177e4SLinus Torvalds   .get = snd_hdsp_get_adatsync_sync_check \
27361da177e4SLinus Torvalds }
27371da177e4SLinus Torvalds 
273855e957d8STakashi Iwai static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
27391da177e4SLinus Torvalds {
27401da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
27411da177e4SLinus Torvalds 	if (status & HDSP_TimecodeLock) {
2742b0b98119STakashi Iwai 		if (status & HDSP_TimecodeSync)
27431da177e4SLinus Torvalds 			return 2;
2744b0b98119STakashi Iwai 		else
27451da177e4SLinus Torvalds 			return 1;
2746b0b98119STakashi Iwai 	} else
27471da177e4SLinus Torvalds 		return 0;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
275055e957d8STakashi Iwai static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27511da177e4SLinus Torvalds {
275255e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
27551da177e4SLinus Torvalds 	return 0;
27561da177e4SLinus Torvalds }
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds #define HDSP_ADAT_SYNC_CHECK \
275967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27601da177e4SLinus Torvalds   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27611da177e4SLinus Torvalds   .info = snd_hdsp_info_sync_check, \
27621da177e4SLinus Torvalds   .get = snd_hdsp_get_adat_sync_check \
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
276555e957d8STakashi Iwai static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
27661da177e4SLinus Torvalds {
27671da177e4SLinus Torvalds 	int status = hdsp_read(hdsp, HDSP_statusRegister);
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds 	if (status & (HDSP_Lock0>>idx)) {
2770b0b98119STakashi Iwai 		if (status & (HDSP_Sync0>>idx))
27711da177e4SLinus Torvalds 			return 2;
2772b0b98119STakashi Iwai 		else
27731da177e4SLinus Torvalds 			return 1;
2774b0b98119STakashi Iwai 	} else
27751da177e4SLinus Torvalds 		return 0;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
277855e957d8STakashi Iwai static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27791da177e4SLinus Torvalds {
27801da177e4SLinus Torvalds 	int offset;
278155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	offset = ucontrol->id.index - 1;
27849e46aedaSDan Carpenter 	if (snd_BUG_ON(offset < 0))
27859e46aedaSDan Carpenter 		return -EINVAL;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	switch (hdsp->io_type) {
27881da177e4SLinus Torvalds 	case Digiface:
27891da177e4SLinus Torvalds 	case H9652:
27901da177e4SLinus Torvalds 		if (offset >= 3)
27911da177e4SLinus Torvalds 			return -EINVAL;
27921da177e4SLinus Torvalds 		break;
27931da177e4SLinus Torvalds 	case Multiface:
27941da177e4SLinus Torvalds 	case H9632:
27951da177e4SLinus Torvalds 		if (offset >= 1)
27961da177e4SLinus Torvalds 			return -EINVAL;
27971da177e4SLinus Torvalds 		break;
27981da177e4SLinus Torvalds 	default:
27991da177e4SLinus Torvalds 		return -EIO;
28001da177e4SLinus Torvalds 	}
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
28031da177e4SLinus Torvalds 	return 0;
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
2806e4b6088cSJulian Cable #define HDSP_DDS_OFFSET(xname, xindex) \
2807e4b6088cSJulian Cable { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2808e4b6088cSJulian Cable   .name = xname, \
2809e4b6088cSJulian Cable   .index = xindex, \
2810e4b6088cSJulian Cable   .info = snd_hdsp_info_dds_offset, \
2811e4b6088cSJulian Cable   .get = snd_hdsp_get_dds_offset, \
2812e4b6088cSJulian Cable   .put = snd_hdsp_put_dds_offset \
2813e4b6088cSJulian Cable }
2814e4b6088cSJulian Cable 
2815e4b6088cSJulian Cable static int hdsp_dds_offset(struct hdsp *hdsp)
2816e4b6088cSJulian Cable {
2817e4b6088cSJulian Cable 	u64 n;
2818e4b6088cSJulian Cable 	unsigned int dds_value = hdsp->dds_value;
2819e4b6088cSJulian Cable 	int system_sample_rate = hdsp->system_sample_rate;
2820e4b6088cSJulian Cable 
28212a3988f6STakashi Iwai 	if (!dds_value)
28222a3988f6STakashi Iwai 		return 0;
28232a3988f6STakashi Iwai 
2824e4b6088cSJulian Cable 	n = DDS_NUMERATOR;
2825e4b6088cSJulian Cable 	/*
2826e4b6088cSJulian Cable 	 * dds_value = n / rate
2827e4b6088cSJulian Cable 	 * rate = n / dds_value
2828e4b6088cSJulian Cable 	 */
28293f7440a6STakashi Iwai 	n = div_u64(n, dds_value);
2830e4b6088cSJulian Cable 	if (system_sample_rate >= 112000)
2831e4b6088cSJulian Cable 		n *= 4;
2832e4b6088cSJulian Cable 	else if (system_sample_rate >= 56000)
2833e4b6088cSJulian Cable 		n *= 2;
2834e4b6088cSJulian Cable 	return ((int)n) - system_sample_rate;
2835e4b6088cSJulian Cable }
2836e4b6088cSJulian Cable 
2837e4b6088cSJulian Cable static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2838e4b6088cSJulian Cable {
2839e4b6088cSJulian Cable 	int rate = hdsp->system_sample_rate + offset_hz;
2840e4b6088cSJulian Cable 	hdsp_set_dds_value(hdsp, rate);
2841e4b6088cSJulian Cable 	return 0;
2842e4b6088cSJulian Cable }
2843e4b6088cSJulian Cable 
2844e4b6088cSJulian Cable static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2845e4b6088cSJulian Cable {
2846e4b6088cSJulian Cable 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2847e4b6088cSJulian Cable 	uinfo->count = 1;
2848e4b6088cSJulian Cable 	uinfo->value.integer.min = -5000;
2849e4b6088cSJulian Cable 	uinfo->value.integer.max = 5000;
2850e4b6088cSJulian Cable 	return 0;
2851e4b6088cSJulian Cable }
2852e4b6088cSJulian Cable 
2853e4b6088cSJulian Cable static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2854e4b6088cSJulian Cable {
2855e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2856e4b6088cSJulian Cable 
2857eab3c4dbSTakashi Iwai 	ucontrol->value.integer.value[0] = hdsp_dds_offset(hdsp);
2858e4b6088cSJulian Cable 	return 0;
2859e4b6088cSJulian Cable }
2860e4b6088cSJulian Cable 
2861e4b6088cSJulian Cable static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2862e4b6088cSJulian Cable {
2863e4b6088cSJulian Cable 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2864e4b6088cSJulian Cable 	int change;
2865e4b6088cSJulian Cable 	int val;
2866e4b6088cSJulian Cable 
2867e4b6088cSJulian Cable 	if (!snd_hdsp_use_is_exclusive(hdsp))
2868e4b6088cSJulian Cable 		return -EBUSY;
2869eab3c4dbSTakashi Iwai 	val = ucontrol->value.integer.value[0];
2870e4b6088cSJulian Cable 	spin_lock_irq(&hdsp->lock);
2871e4b6088cSJulian Cable 	if (val != hdsp_dds_offset(hdsp))
2872e4b6088cSJulian Cable 		change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2873e4b6088cSJulian Cable 	else
2874e4b6088cSJulian Cable 		change = 0;
2875e4b6088cSJulian Cable 	spin_unlock_irq(&hdsp->lock);
2876e4b6088cSJulian Cable 	return change;
2877e4b6088cSJulian Cable }
2878e4b6088cSJulian Cable 
2879b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
28801da177e4SLinus Torvalds HDSP_DA_GAIN("DA Gain", 0),
28811da177e4SLinus Torvalds HDSP_AD_GAIN("AD Gain", 0),
28821da177e4SLinus Torvalds HDSP_PHONE_GAIN("Phones Gain", 0),
28834833c673SAdrian Knoth HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
2884e4b6088cSJulian Cable HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
28851da177e4SLinus Torvalds };
28861da177e4SLinus Torvalds 
2887b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_controls[] = {
28881da177e4SLinus Torvalds {
28895549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
28901da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
28911da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_info,
28921da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_get,
28931da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_put,
28941da177e4SLinus Torvalds },
28951da177e4SLinus Torvalds {
28961da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
28975549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
28981da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
28991da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_stream_info,
29001da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_stream_get,
29011da177e4SLinus Torvalds 	.put =		snd_hdsp_control_spdif_stream_put,
29021da177e4SLinus Torvalds },
29031da177e4SLinus Torvalds {
29041da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
29055549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
29061da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
29071da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
29081da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
29091da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
29101da177e4SLinus Torvalds   			 IEC958_AES0_PROFESSIONAL |
29111da177e4SLinus Torvalds 			 IEC958_AES0_CON_EMPHASIS,
29121da177e4SLinus Torvalds },
29131da177e4SLinus Torvalds {
29141da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
29155549d549SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
29161da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
29171da177e4SLinus Torvalds 	.info =		snd_hdsp_control_spdif_mask_info,
29181da177e4SLinus Torvalds 	.get =		snd_hdsp_control_spdif_mask_get,
29191da177e4SLinus Torvalds 	.private_value = IEC958_AES0_NONAUDIO |
29201da177e4SLinus Torvalds 			 IEC958_AES0_PROFESSIONAL |
29211da177e4SLinus Torvalds 			 IEC958_AES0_PRO_EMPHASIS,
29221da177e4SLinus Torvalds },
29231da177e4SLinus Torvalds HDSP_MIXER("Mixer", 0),
29241da177e4SLinus Torvalds HDSP_SPDIF_IN("IEC958 Input Connector", 0),
29254833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
29264833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
29274833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
29284833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
29291da177e4SLinus Torvalds /* 'Sample Clock Source' complies with the alsa control naming scheme */
29301da177e4SLinus Torvalds HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
2931e3ea4d89STakashi Iwai {
2932e3ea4d89STakashi Iwai 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2933e3ea4d89STakashi Iwai 	.name = "Sample Clock Source Locking",
2934e3ea4d89STakashi Iwai 	.info = snd_hdsp_info_clock_source_lock,
2935e3ea4d89STakashi Iwai 	.get = snd_hdsp_get_clock_source_lock,
2936e3ea4d89STakashi Iwai 	.put = snd_hdsp_put_clock_source_lock,
2937e3ea4d89STakashi Iwai },
29381da177e4SLinus Torvalds HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
29391da177e4SLinus Torvalds HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
29401da177e4SLinus Torvalds HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
29411da177e4SLinus Torvalds HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
29421da177e4SLinus Torvalds HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
29431da177e4SLinus Torvalds /* 'External Rate' complies with the alsa control naming scheme */
29441da177e4SLinus Torvalds HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
29451da177e4SLinus Torvalds HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
29461da177e4SLinus Torvalds HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
29471da177e4SLinus Torvalds HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
29484833c673SAdrian Knoth HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
29491da177e4SLinus Torvalds HDSP_PRECISE_POINTER("Precise Pointer", 0),
29504adab848STakashi Iwai HDSP_USE_MIDI_WORK("Use Midi Tasklet", 0),
29511da177e4SLinus Torvalds };
29521da177e4SLinus Torvalds 
295328b26e15SFlorian Faber 
295428b26e15SFlorian Faber static int hdsp_rpm_input12(struct hdsp *hdsp)
295528b26e15SFlorian Faber {
295628b26e15SFlorian Faber 	switch (hdsp->control_register & HDSP_RPM_Inp12) {
295728b26e15SFlorian Faber 	case HDSP_RPM_Inp12_Phon_6dB:
295828b26e15SFlorian Faber 		return 0;
295928b26e15SFlorian Faber 	case HDSP_RPM_Inp12_Phon_n6dB:
296028b26e15SFlorian Faber 		return 2;
296128b26e15SFlorian Faber 	case HDSP_RPM_Inp12_Line_0dB:
296228b26e15SFlorian Faber 		return 3;
296328b26e15SFlorian Faber 	case HDSP_RPM_Inp12_Line_n6dB:
296428b26e15SFlorian Faber 		return 4;
296528b26e15SFlorian Faber 	}
296628b26e15SFlorian Faber 	return 1;
296728b26e15SFlorian Faber }
296828b26e15SFlorian Faber 
296928b26e15SFlorian Faber 
297028b26e15SFlorian Faber static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
297128b26e15SFlorian Faber {
297228b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
297328b26e15SFlorian Faber 
297428b26e15SFlorian Faber 	ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
297528b26e15SFlorian Faber 	return 0;
297628b26e15SFlorian Faber }
297728b26e15SFlorian Faber 
297828b26e15SFlorian Faber 
297928b26e15SFlorian Faber static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
298028b26e15SFlorian Faber {
298128b26e15SFlorian Faber 	hdsp->control_register &= ~HDSP_RPM_Inp12;
298228b26e15SFlorian Faber 	switch (mode) {
298328b26e15SFlorian Faber 	case 0:
298428b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
298528b26e15SFlorian Faber 		break;
298628b26e15SFlorian Faber 	case 1:
298728b26e15SFlorian Faber 		break;
298828b26e15SFlorian Faber 	case 2:
298928b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
299028b26e15SFlorian Faber 		break;
299128b26e15SFlorian Faber 	case 3:
299228b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
299328b26e15SFlorian Faber 		break;
299428b26e15SFlorian Faber 	case 4:
299528b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
299628b26e15SFlorian Faber 		break;
299728b26e15SFlorian Faber 	default:
299828b26e15SFlorian Faber 		return -1;
299928b26e15SFlorian Faber 	}
300028b26e15SFlorian Faber 
300128b26e15SFlorian Faber 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
300228b26e15SFlorian Faber 	return 0;
300328b26e15SFlorian Faber }
300428b26e15SFlorian Faber 
300528b26e15SFlorian Faber 
300628b26e15SFlorian Faber static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
300728b26e15SFlorian Faber {
300828b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
300928b26e15SFlorian Faber 	int change;
301028b26e15SFlorian Faber 	int val;
301128b26e15SFlorian Faber 
301228b26e15SFlorian Faber 	if (!snd_hdsp_use_is_exclusive(hdsp))
301328b26e15SFlorian Faber 		return -EBUSY;
301428b26e15SFlorian Faber 	val = ucontrol->value.enumerated.item[0];
301528b26e15SFlorian Faber 	if (val < 0)
301628b26e15SFlorian Faber 		val = 0;
301728b26e15SFlorian Faber 	if (val > 4)
301828b26e15SFlorian Faber 		val = 4;
301928b26e15SFlorian Faber 	spin_lock_irq(&hdsp->lock);
302028b26e15SFlorian Faber 	if (val != hdsp_rpm_input12(hdsp))
302128b26e15SFlorian Faber 		change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
302228b26e15SFlorian Faber 	else
302328b26e15SFlorian Faber 		change = 0;
302428b26e15SFlorian Faber 	spin_unlock_irq(&hdsp->lock);
302528b26e15SFlorian Faber 	return change;
302628b26e15SFlorian Faber }
302728b26e15SFlorian Faber 
302828b26e15SFlorian Faber 
302928b26e15SFlorian Faber static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
303028b26e15SFlorian Faber {
30318d678da9STakashi Iwai 	static const char * const texts[] = {
30328d678da9STakashi Iwai 		"Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
30338d678da9STakashi Iwai 	};
303428b26e15SFlorian Faber 
30358d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 5, texts);
303628b26e15SFlorian Faber }
303728b26e15SFlorian Faber 
303828b26e15SFlorian Faber 
303928b26e15SFlorian Faber static int hdsp_rpm_input34(struct hdsp *hdsp)
304028b26e15SFlorian Faber {
304128b26e15SFlorian Faber 	switch (hdsp->control_register & HDSP_RPM_Inp34) {
304228b26e15SFlorian Faber 	case HDSP_RPM_Inp34_Phon_6dB:
304328b26e15SFlorian Faber 		return 0;
304428b26e15SFlorian Faber 	case HDSP_RPM_Inp34_Phon_n6dB:
304528b26e15SFlorian Faber 		return 2;
304628b26e15SFlorian Faber 	case HDSP_RPM_Inp34_Line_0dB:
304728b26e15SFlorian Faber 		return 3;
304828b26e15SFlorian Faber 	case HDSP_RPM_Inp34_Line_n6dB:
304928b26e15SFlorian Faber 		return 4;
305028b26e15SFlorian Faber 	}
305128b26e15SFlorian Faber 	return 1;
305228b26e15SFlorian Faber }
305328b26e15SFlorian Faber 
305428b26e15SFlorian Faber 
305528b26e15SFlorian Faber static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
305628b26e15SFlorian Faber {
305728b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
305828b26e15SFlorian Faber 
305928b26e15SFlorian Faber 	ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
306028b26e15SFlorian Faber 	return 0;
306128b26e15SFlorian Faber }
306228b26e15SFlorian Faber 
306328b26e15SFlorian Faber 
306428b26e15SFlorian Faber static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
306528b26e15SFlorian Faber {
306628b26e15SFlorian Faber 	hdsp->control_register &= ~HDSP_RPM_Inp34;
306728b26e15SFlorian Faber 	switch (mode) {
306828b26e15SFlorian Faber 	case 0:
306928b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
307028b26e15SFlorian Faber 		break;
307128b26e15SFlorian Faber 	case 1:
307228b26e15SFlorian Faber 		break;
307328b26e15SFlorian Faber 	case 2:
307428b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
307528b26e15SFlorian Faber 		break;
307628b26e15SFlorian Faber 	case 3:
307728b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
307828b26e15SFlorian Faber 		break;
307928b26e15SFlorian Faber 	case 4:
308028b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
308128b26e15SFlorian Faber 		break;
308228b26e15SFlorian Faber 	default:
308328b26e15SFlorian Faber 		return -1;
308428b26e15SFlorian Faber 	}
308528b26e15SFlorian Faber 
308628b26e15SFlorian Faber 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
308728b26e15SFlorian Faber 	return 0;
308828b26e15SFlorian Faber }
308928b26e15SFlorian Faber 
309028b26e15SFlorian Faber 
309128b26e15SFlorian Faber static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
309228b26e15SFlorian Faber {
309328b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
309428b26e15SFlorian Faber 	int change;
309528b26e15SFlorian Faber 	int val;
309628b26e15SFlorian Faber 
309728b26e15SFlorian Faber 	if (!snd_hdsp_use_is_exclusive(hdsp))
309828b26e15SFlorian Faber 		return -EBUSY;
309928b26e15SFlorian Faber 	val = ucontrol->value.enumerated.item[0];
310028b26e15SFlorian Faber 	if (val < 0)
310128b26e15SFlorian Faber 		val = 0;
310228b26e15SFlorian Faber 	if (val > 4)
310328b26e15SFlorian Faber 		val = 4;
310428b26e15SFlorian Faber 	spin_lock_irq(&hdsp->lock);
310528b26e15SFlorian Faber 	if (val != hdsp_rpm_input34(hdsp))
310628b26e15SFlorian Faber 		change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
310728b26e15SFlorian Faber 	else
310828b26e15SFlorian Faber 		change = 0;
310928b26e15SFlorian Faber 	spin_unlock_irq(&hdsp->lock);
311028b26e15SFlorian Faber 	return change;
311128b26e15SFlorian Faber }
311228b26e15SFlorian Faber 
311328b26e15SFlorian Faber 
311428b26e15SFlorian Faber /* RPM Bypass switch */
311528b26e15SFlorian Faber static int hdsp_rpm_bypass(struct hdsp *hdsp)
311628b26e15SFlorian Faber {
311728b26e15SFlorian Faber 	return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
311828b26e15SFlorian Faber }
311928b26e15SFlorian Faber 
312028b26e15SFlorian Faber 
312128b26e15SFlorian Faber static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
312228b26e15SFlorian Faber {
312328b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
312428b26e15SFlorian Faber 
312528b26e15SFlorian Faber 	ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
312628b26e15SFlorian Faber 	return 0;
312728b26e15SFlorian Faber }
312828b26e15SFlorian Faber 
312928b26e15SFlorian Faber 
313028b26e15SFlorian Faber static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
313128b26e15SFlorian Faber {
313228b26e15SFlorian Faber 	if (on)
313328b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Bypass;
313428b26e15SFlorian Faber 	else
313528b26e15SFlorian Faber 		hdsp->control_register &= ~HDSP_RPM_Bypass;
313628b26e15SFlorian Faber 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
313728b26e15SFlorian Faber 	return 0;
313828b26e15SFlorian Faber }
313928b26e15SFlorian Faber 
314028b26e15SFlorian Faber 
314128b26e15SFlorian Faber static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
314228b26e15SFlorian Faber {
314328b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
314428b26e15SFlorian Faber 	int change;
314528b26e15SFlorian Faber 	unsigned int val;
314628b26e15SFlorian Faber 
314728b26e15SFlorian Faber 	if (!snd_hdsp_use_is_exclusive(hdsp))
314828b26e15SFlorian Faber 		return -EBUSY;
314928b26e15SFlorian Faber 	val = ucontrol->value.integer.value[0] & 1;
315028b26e15SFlorian Faber 	spin_lock_irq(&hdsp->lock);
315128b26e15SFlorian Faber 	change = (int)val != hdsp_rpm_bypass(hdsp);
315228b26e15SFlorian Faber 	hdsp_set_rpm_bypass(hdsp, val);
315328b26e15SFlorian Faber 	spin_unlock_irq(&hdsp->lock);
315428b26e15SFlorian Faber 	return change;
315528b26e15SFlorian Faber }
315628b26e15SFlorian Faber 
315728b26e15SFlorian Faber 
315828b26e15SFlorian Faber static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
315928b26e15SFlorian Faber {
31608d678da9STakashi Iwai 	static const char * const texts[] = {"On", "Off"};
316128b26e15SFlorian Faber 
31628d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
316328b26e15SFlorian Faber }
316428b26e15SFlorian Faber 
316528b26e15SFlorian Faber 
316628b26e15SFlorian Faber /* RPM Disconnect switch */
316728b26e15SFlorian Faber static int hdsp_rpm_disconnect(struct hdsp *hdsp)
316828b26e15SFlorian Faber {
316928b26e15SFlorian Faber 	return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
317028b26e15SFlorian Faber }
317128b26e15SFlorian Faber 
317228b26e15SFlorian Faber 
317328b26e15SFlorian Faber static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
317428b26e15SFlorian Faber {
317528b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
317628b26e15SFlorian Faber 
317728b26e15SFlorian Faber 	ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
317828b26e15SFlorian Faber 	return 0;
317928b26e15SFlorian Faber }
318028b26e15SFlorian Faber 
318128b26e15SFlorian Faber 
318228b26e15SFlorian Faber static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
318328b26e15SFlorian Faber {
318428b26e15SFlorian Faber 	if (on)
318528b26e15SFlorian Faber 		hdsp->control_register |= HDSP_RPM_Disconnect;
318628b26e15SFlorian Faber 	else
318728b26e15SFlorian Faber 		hdsp->control_register &= ~HDSP_RPM_Disconnect;
318828b26e15SFlorian Faber 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
318928b26e15SFlorian Faber 	return 0;
319028b26e15SFlorian Faber }
319128b26e15SFlorian Faber 
319228b26e15SFlorian Faber 
319328b26e15SFlorian Faber static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
319428b26e15SFlorian Faber {
319528b26e15SFlorian Faber 	struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
319628b26e15SFlorian Faber 	int change;
319728b26e15SFlorian Faber 	unsigned int val;
319828b26e15SFlorian Faber 
319928b26e15SFlorian Faber 	if (!snd_hdsp_use_is_exclusive(hdsp))
320028b26e15SFlorian Faber 		return -EBUSY;
320128b26e15SFlorian Faber 	val = ucontrol->value.integer.value[0] & 1;
320228b26e15SFlorian Faber 	spin_lock_irq(&hdsp->lock);
320328b26e15SFlorian Faber 	change = (int)val != hdsp_rpm_disconnect(hdsp);
320428b26e15SFlorian Faber 	hdsp_set_rpm_disconnect(hdsp, val);
320528b26e15SFlorian Faber 	spin_unlock_irq(&hdsp->lock);
320628b26e15SFlorian Faber 	return change;
320728b26e15SFlorian Faber }
320828b26e15SFlorian Faber 
320928b26e15SFlorian Faber static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
321028b26e15SFlorian Faber {
32118d678da9STakashi Iwai 	static const char * const texts[] = {"On", "Off"};
321228b26e15SFlorian Faber 
32138d678da9STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
321428b26e15SFlorian Faber }
321528b26e15SFlorian Faber 
3216b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
321728b26e15SFlorian Faber 	{
321828b26e15SFlorian Faber 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
321928b26e15SFlorian Faber 		.name = "RPM Bypass",
322028b26e15SFlorian Faber 		.get = snd_hdsp_get_rpm_bypass,
322128b26e15SFlorian Faber 		.put = snd_hdsp_put_rpm_bypass,
322228b26e15SFlorian Faber 		.info = snd_hdsp_info_rpm_bypass
322328b26e15SFlorian Faber 	},
322428b26e15SFlorian Faber 	{
322528b26e15SFlorian Faber 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
322628b26e15SFlorian Faber 		.name = "RPM Disconnect",
322728b26e15SFlorian Faber 		.get = snd_hdsp_get_rpm_disconnect,
322828b26e15SFlorian Faber 		.put = snd_hdsp_put_rpm_disconnect,
322928b26e15SFlorian Faber 		.info = snd_hdsp_info_rpm_disconnect
323028b26e15SFlorian Faber 	},
323128b26e15SFlorian Faber 	{
323228b26e15SFlorian Faber 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
323328b26e15SFlorian Faber 		.name = "Input 1/2",
323428b26e15SFlorian Faber 		.get = snd_hdsp_get_rpm_input12,
323528b26e15SFlorian Faber 		.put = snd_hdsp_put_rpm_input12,
323628b26e15SFlorian Faber 		.info = snd_hdsp_info_rpm_input
323728b26e15SFlorian Faber 	},
323828b26e15SFlorian Faber 	{
323928b26e15SFlorian Faber 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
324028b26e15SFlorian Faber 		.name = "Input 3/4",
324128b26e15SFlorian Faber 		.get = snd_hdsp_get_rpm_input34,
324228b26e15SFlorian Faber 		.put = snd_hdsp_put_rpm_input34,
324328b26e15SFlorian Faber 		.info = snd_hdsp_info_rpm_input
324428b26e15SFlorian Faber 	},
324528b26e15SFlorian Faber 	HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
324628b26e15SFlorian Faber 	HDSP_MIXER("Mixer", 0)
324728b26e15SFlorian Faber };
324828b26e15SFlorian Faber 
3249b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_96xx_aeb =
32504833c673SAdrian Knoth 	HDSP_TOGGLE_SETTING("Analog Extension Board",
32514833c673SAdrian Knoth 			HDSP_AnalogExtensionBoard);
325255e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
32531da177e4SLinus Torvalds 
3254da2a040eSJasmin Fazlic 
3255da2a040eSJasmin Fazlic static bool hdsp_loopback_get(struct hdsp *const hdsp, const u8 channel)
3256da2a040eSJasmin Fazlic {
3257da2a040eSJasmin Fazlic 	return hdsp->io_loopback & (1 << channel);
3258da2a040eSJasmin Fazlic }
3259da2a040eSJasmin Fazlic 
3260da2a040eSJasmin Fazlic static int hdsp_loopback_set(struct hdsp *const hdsp, const u8 channel, const bool enable)
3261da2a040eSJasmin Fazlic {
3262da2a040eSJasmin Fazlic 	if (hdsp_loopback_get(hdsp, channel) == enable)
3263da2a040eSJasmin Fazlic 		return 0;
3264da2a040eSJasmin Fazlic 
3265da2a040eSJasmin Fazlic 	hdsp->io_loopback ^= (1 << channel);
3266da2a040eSJasmin Fazlic 
3267da2a040eSJasmin Fazlic 	hdsp_write(hdsp, HDSP_inputEnable + (4 * (hdsp->max_channels + channel)), enable);
3268da2a040eSJasmin Fazlic 
3269da2a040eSJasmin Fazlic 	return 1;
3270da2a040eSJasmin Fazlic }
3271da2a040eSJasmin Fazlic 
3272da2a040eSJasmin Fazlic static int snd_hdsp_loopback_get(struct snd_kcontrol *const kcontrol,
3273da2a040eSJasmin Fazlic 				 struct snd_ctl_elem_value *const ucontrol)
3274da2a040eSJasmin Fazlic {
3275da2a040eSJasmin Fazlic 	struct hdsp *const hdsp = snd_kcontrol_chip(kcontrol);
3276da2a040eSJasmin Fazlic 	const u8 channel = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
3277da2a040eSJasmin Fazlic 
3278da2a040eSJasmin Fazlic 	if (channel >= hdsp->max_channels)
3279da2a040eSJasmin Fazlic 		return -ENOENT;
3280da2a040eSJasmin Fazlic 
3281da2a040eSJasmin Fazlic 	ucontrol->value.integer.value[0] = hdsp_loopback_get(hdsp, channel);
3282da2a040eSJasmin Fazlic 
3283da2a040eSJasmin Fazlic 	return 0;
3284da2a040eSJasmin Fazlic }
3285da2a040eSJasmin Fazlic 
3286da2a040eSJasmin Fazlic static int snd_hdsp_loopback_put(struct snd_kcontrol *const kcontrol,
3287da2a040eSJasmin Fazlic 				 struct snd_ctl_elem_value *const ucontrol)
3288da2a040eSJasmin Fazlic {
3289da2a040eSJasmin Fazlic 	struct hdsp *const hdsp = snd_kcontrol_chip(kcontrol);
3290da2a040eSJasmin Fazlic 	const u8 channel = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
3291da2a040eSJasmin Fazlic 	const bool enable = ucontrol->value.integer.value[0] & 1;
3292da2a040eSJasmin Fazlic 
3293da2a040eSJasmin Fazlic 	if (channel >= hdsp->max_channels)
3294da2a040eSJasmin Fazlic 		return -ENOENT;
3295da2a040eSJasmin Fazlic 
3296da2a040eSJasmin Fazlic 	return hdsp_loopback_set(hdsp, channel, enable);
3297da2a040eSJasmin Fazlic }
3298da2a040eSJasmin Fazlic 
3299da2a040eSJasmin Fazlic static struct snd_kcontrol_new snd_hdsp_loopback_control = {
3300da2a040eSJasmin Fazlic 	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
3301da2a040eSJasmin Fazlic 	.name = "Output Loopback",
3302da2a040eSJasmin Fazlic 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3303da2a040eSJasmin Fazlic 	.info = snd_ctl_boolean_mono_info,
3304da2a040eSJasmin Fazlic 	.get = snd_hdsp_loopback_get,
3305da2a040eSJasmin Fazlic 	.put = snd_hdsp_loopback_put
3306da2a040eSJasmin Fazlic };
3307da2a040eSJasmin Fazlic 
330855e957d8STakashi Iwai static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
33091da177e4SLinus Torvalds {
33101da177e4SLinus Torvalds 	unsigned int idx;
33111da177e4SLinus Torvalds 	int err;
331255e957d8STakashi Iwai 	struct snd_kcontrol *kctl;
33131da177e4SLinus Torvalds 
331428b26e15SFlorian Faber 	if (hdsp->io_type == RPM) {
331528b26e15SFlorian Faber 		/* RPM Bypass, Disconnect and Input switches */
331628b26e15SFlorian Faber 		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3317d06130dcSColin Ian King 			err = snd_ctl_add(card, snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
331828b26e15SFlorian Faber 			if (err < 0)
331928b26e15SFlorian Faber 				return err;
332028b26e15SFlorian Faber 		}
332128b26e15SFlorian Faber 		return 0;
332228b26e15SFlorian Faber 	}
332328b26e15SFlorian Faber 
33241da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
332566c8f759STakashi Iwai 		kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp);
332666c8f759STakashi Iwai 		err = snd_ctl_add(card, kctl);
332766c8f759STakashi Iwai 		if (err < 0)
33281da177e4SLinus Torvalds 			return err;
33291da177e4SLinus Torvalds 		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
33301da177e4SLinus Torvalds 			hdsp->spdif_ctl = kctl;
33311da177e4SLinus Torvalds 	}
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds 	/* ADAT SyncCheck status */
33341da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
33351da177e4SLinus Torvalds 	snd_hdsp_adat_sync_check.index = 1;
333666c8f759STakashi Iwai 	kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp);
333766c8f759STakashi Iwai 	err = snd_ctl_add(card, kctl);
333866c8f759STakashi Iwai 	if (err < 0)
33391da177e4SLinus Torvalds 		return err;
33401da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
33411da177e4SLinus Torvalds 		for (idx = 1; idx < 3; ++idx) {
33421da177e4SLinus Torvalds 			snd_hdsp_adat_sync_check.index = idx+1;
334366c8f759STakashi Iwai 			kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp);
334466c8f759STakashi Iwai 			err = snd_ctl_add(card, kctl);
334566c8f759STakashi Iwai 			if (err < 0)
33461da177e4SLinus Torvalds 				return err;
33471da177e4SLinus Torvalds 		}
33481da177e4SLinus Torvalds 	}
33491da177e4SLinus Torvalds 
33501da177e4SLinus Torvalds 	/* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
33511da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
33521da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
335366c8f759STakashi Iwai 			kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp);
335466c8f759STakashi Iwai 			err = snd_ctl_add(card, kctl);
335566c8f759STakashi Iwai 			if (err < 0)
33561da177e4SLinus Torvalds 				return err;
33571da177e4SLinus Torvalds 		}
33581da177e4SLinus Torvalds 	}
33591da177e4SLinus Torvalds 
3360da2a040eSJasmin Fazlic 	/* Output loopback controls for H9632 cards */
3361da2a040eSJasmin Fazlic 	if (hdsp->io_type == H9632) {
3362da2a040eSJasmin Fazlic 		snd_hdsp_loopback_control.count = hdsp->max_channels;
3363da2a040eSJasmin Fazlic 		kctl = snd_ctl_new1(&snd_hdsp_loopback_control, hdsp);
3364da2a040eSJasmin Fazlic 		if (kctl == NULL)
3365da2a040eSJasmin Fazlic 			return -ENOMEM;
3366da2a040eSJasmin Fazlic 		err = snd_ctl_add(card, kctl);
3367da2a040eSJasmin Fazlic 		if (err < 0)
3368da2a040eSJasmin Fazlic 			return err;
3369da2a040eSJasmin Fazlic 	}
3370da2a040eSJasmin Fazlic 
33711da177e4SLinus Torvalds 	/* AEB control for H96xx card */
33721da177e4SLinus Torvalds 	if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
337366c8f759STakashi Iwai 		kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp);
337466c8f759STakashi Iwai 		err = snd_ctl_add(card, kctl);
337566c8f759STakashi Iwai 		if (err < 0)
33761da177e4SLinus Torvalds 			return err;
33771da177e4SLinus Torvalds 	}
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 	return 0;
33801da177e4SLinus Torvalds }
33811da177e4SLinus Torvalds 
33821da177e4SLinus Torvalds /*------------------------------------------------------------
33831da177e4SLinus Torvalds    /proc interface
33841da177e4SLinus Torvalds  ------------------------------------------------------------*/
33851da177e4SLinus Torvalds 
33861da177e4SLinus Torvalds static void
338755e957d8STakashi Iwai snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
33881da177e4SLinus Torvalds {
33899fe856e4SJoe Perches 	struct hdsp *hdsp = entry->private_data;
33901da177e4SLinus Torvalds 	unsigned int status;
33911da177e4SLinus Torvalds 	unsigned int status2;
33921da177e4SLinus Torvalds 	char *pref_sync_ref;
33931da177e4SLinus Torvalds 	char *autosync_ref;
33941da177e4SLinus Torvalds 	char *system_clock_mode;
33951da177e4SLinus Torvalds 	char *clock_source;
33961da177e4SLinus Torvalds 	int x;
33971da177e4SLinus Torvalds 
3398c18bc9b9STim Blechmann 	status = hdsp_read(hdsp, HDSP_statusRegister);
3399c18bc9b9STim Blechmann 	status2 = hdsp_read(hdsp, HDSP_status2Register);
3400c18bc9b9STim Blechmann 
3401c18bc9b9STim Blechmann 	snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3402c18bc9b9STim Blechmann 		    hdsp->card->number + 1);
3403c18bc9b9STim Blechmann 	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3404c18bc9b9STim Blechmann 		    hdsp->capture_buffer, hdsp->playback_buffer);
3405c18bc9b9STim Blechmann 	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3406c18bc9b9STim Blechmann 		    hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3407c18bc9b9STim Blechmann 	snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3408c18bc9b9STim Blechmann 	snd_iprintf(buffer, "Control2 register: 0x%x\n",
3409c18bc9b9STim Blechmann 		    hdsp->control2_register);
3410c18bc9b9STim Blechmann 	snd_iprintf(buffer, "Status register: 0x%x\n", status);
3411c18bc9b9STim Blechmann 	snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3412c18bc9b9STim Blechmann 
3413ecb594e6SRemy Bruno 	if (hdsp_check_for_iobox(hdsp)) {
3414c18bc9b9STim Blechmann 		snd_iprintf(buffer, "No I/O box connected.\n"
3415c18bc9b9STim Blechmann 			    "Please connect one and upload firmware.\n");
34161da177e4SLinus Torvalds 		return;
3417ecb594e6SRemy Bruno 	}
34181da177e4SLinus Torvalds 
3419b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) {
34201da177e4SLinus Torvalds 		if (hdsp->state & HDSP_FirmwareCached) {
34211da177e4SLinus Torvalds 			if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3422c18bc9b9STim Blechmann 				snd_iprintf(buffer, "Firmware loading from "
3423c18bc9b9STim Blechmann 					    "cache failed, "
3424c18bc9b9STim Blechmann 					    "please upload manually.\n");
34251da177e4SLinus Torvalds 				return;
34261da177e4SLinus Torvalds 			}
34271da177e4SLinus Torvalds 		} else {
34280cc62972SColin Ian King 			int err;
34290cc62972SColin Ian King 
3430311e70a4STakashi Iwai 			err = hdsp_request_fw_loader(hdsp);
3431311e70a4STakashi Iwai 			if (err < 0) {
3432311e70a4STakashi Iwai 				snd_iprintf(buffer,
3433311e70a4STakashi Iwai 					    "No firmware loaded nor cached, "
3434311e70a4STakashi Iwai 					    "please upload firmware.\n");
34351da177e4SLinus Torvalds 				return;
34361da177e4SLinus Torvalds 			}
34371da177e4SLinus Torvalds 		}
3438311e70a4STakashi Iwai 	}
34391da177e4SLinus Torvalds 
34401da177e4SLinus Torvalds 	snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
34411da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
34421da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
34431da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
34441da177e4SLinus Torvalds 	snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
34454adab848STakashi Iwai 	snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_work ? "on" : "off");
34461da177e4SLinus Torvalds 
34471da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34481da177e4SLinus Torvalds 
34491da177e4SLinus Torvalds 	x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
34501da177e4SLinus Torvalds 
34511da177e4SLinus Torvalds 	snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
34521da177e4SLinus Torvalds 	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
34531da177e4SLinus Torvalds 	snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
34541da177e4SLinus Torvalds 	snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
34551da177e4SLinus Torvalds 
34561da177e4SLinus Torvalds 	snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds 	switch (hdsp_clock_source(hdsp)) {
34611da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_AUTOSYNC:
34621da177e4SLinus Torvalds 		clock_source = "AutoSync";
34631da177e4SLinus Torvalds 		break;
34641da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
34651da177e4SLinus Torvalds 		clock_source = "Internal 32 kHz";
34661da177e4SLinus Torvalds 		break;
34671da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
34681da177e4SLinus Torvalds 		clock_source = "Internal 44.1 kHz";
34691da177e4SLinus Torvalds 		break;
34701da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
34711da177e4SLinus Torvalds 		clock_source = "Internal 48 kHz";
34721da177e4SLinus Torvalds 		break;
34731da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
34741da177e4SLinus Torvalds 		clock_source = "Internal 64 kHz";
34751da177e4SLinus Torvalds 		break;
34761da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
34771da177e4SLinus Torvalds 		clock_source = "Internal 88.2 kHz";
34781da177e4SLinus Torvalds 		break;
34791da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
34801da177e4SLinus Torvalds 		clock_source = "Internal 96 kHz";
34811da177e4SLinus Torvalds 		break;
34821da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
34831da177e4SLinus Torvalds 		clock_source = "Internal 128 kHz";
34841da177e4SLinus Torvalds 		break;
34851da177e4SLinus Torvalds 	case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
34861da177e4SLinus Torvalds 		clock_source = "Internal 176.4 kHz";
34871da177e4SLinus Torvalds 		break;
34881da177e4SLinus Torvalds 		case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
34891da177e4SLinus Torvalds 		clock_source = "Internal 192 kHz";
34901da177e4SLinus Torvalds 		break;
34911da177e4SLinus Torvalds 	default:
34921da177e4SLinus Torvalds 		clock_source = "Error";
34931da177e4SLinus Torvalds 	}
34941da177e4SLinus Torvalds 	snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
34951da177e4SLinus Torvalds 
3496b0b98119STakashi Iwai 	if (hdsp_system_clock_mode(hdsp))
34971da177e4SLinus Torvalds 		system_clock_mode = "Slave";
3498b0b98119STakashi Iwai 	else
34991da177e4SLinus Torvalds 		system_clock_mode = "Master";
35001da177e4SLinus Torvalds 
35011da177e4SLinus Torvalds 	switch (hdsp_pref_sync_ref (hdsp)) {
35021da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_WORD:
35031da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
35041da177e4SLinus Torvalds 		break;
35051da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT_SYNC:
35061da177e4SLinus Torvalds 		pref_sync_ref = "ADAT Sync";
35071da177e4SLinus Torvalds 		break;
35081da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_SPDIF:
35091da177e4SLinus Torvalds 		pref_sync_ref = "SPDIF";
35101da177e4SLinus Torvalds 		break;
35111da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT1:
35121da177e4SLinus Torvalds 		pref_sync_ref = "ADAT1";
35131da177e4SLinus Torvalds 		break;
35141da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT2:
35151da177e4SLinus Torvalds 		pref_sync_ref = "ADAT2";
35161da177e4SLinus Torvalds 		break;
35171da177e4SLinus Torvalds 	case HDSP_SYNC_FROM_ADAT3:
35181da177e4SLinus Torvalds 		pref_sync_ref = "ADAT3";
35191da177e4SLinus Torvalds 		break;
35201da177e4SLinus Torvalds 	default:
35211da177e4SLinus Torvalds 		pref_sync_ref = "Word Clock";
35221da177e4SLinus Torvalds 		break;
35231da177e4SLinus Torvalds 	}
35241da177e4SLinus Torvalds 	snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 	switch (hdsp_autosync_ref (hdsp)) {
35271da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_WORD:
35281da177e4SLinus Torvalds 		autosync_ref = "Word Clock";
35291da177e4SLinus Torvalds 		break;
35301da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
35311da177e4SLinus Torvalds 		autosync_ref = "ADAT Sync";
35321da177e4SLinus Torvalds 		break;
35331da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_SPDIF:
35341da177e4SLinus Torvalds 		autosync_ref = "SPDIF";
35351da177e4SLinus Torvalds 		break;
35361da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_NONE:
35371da177e4SLinus Torvalds 		autosync_ref = "None";
35381da177e4SLinus Torvalds 		break;
35391da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT1:
35401da177e4SLinus Torvalds 		autosync_ref = "ADAT1";
35411da177e4SLinus Torvalds 		break;
35421da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT2:
35431da177e4SLinus Torvalds 		autosync_ref = "ADAT2";
35441da177e4SLinus Torvalds 		break;
35451da177e4SLinus Torvalds 	case HDSP_AUTOSYNC_FROM_ADAT3:
35461da177e4SLinus Torvalds 		autosync_ref = "ADAT3";
35471da177e4SLinus Torvalds 		break;
35481da177e4SLinus Torvalds 	default:
35491da177e4SLinus Torvalds 		autosync_ref = "---";
35501da177e4SLinus Torvalds 		break;
35511da177e4SLinus Torvalds 	}
35521da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
35531da177e4SLinus Torvalds 
35541da177e4SLinus Torvalds 	snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
35551da177e4SLinus Torvalds 
35561da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 	snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3559e3ea4d89STakashi Iwai 	snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
35601da177e4SLinus Torvalds 
35611da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
35621da177e4SLinus Torvalds 
356328b26e15SFlorian Faber 	if (hdsp->io_type != RPM) {
35641da177e4SLinus Torvalds 		switch (hdsp_spdif_in(hdsp)) {
35651da177e4SLinus Torvalds 		case HDSP_SPDIFIN_OPTICAL:
35661da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 input: Optical\n");
35671da177e4SLinus Torvalds 			break;
35681da177e4SLinus Torvalds 		case HDSP_SPDIFIN_COAXIAL:
35691da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 input: Coaxial\n");
35701da177e4SLinus Torvalds 			break;
35711da177e4SLinus Torvalds 		case HDSP_SPDIFIN_INTERNAL:
35721da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 input: Internal\n");
35731da177e4SLinus Torvalds 			break;
35741da177e4SLinus Torvalds 		case HDSP_SPDIFIN_AES:
35751da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 input: AES\n");
35761da177e4SLinus Torvalds 			break;
35771da177e4SLinus Torvalds 		default:
35781da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 input: ???\n");
35791da177e4SLinus Torvalds 			break;
35801da177e4SLinus Torvalds 		}
358128b26e15SFlorian Faber 	}
35821da177e4SLinus Torvalds 
358328b26e15SFlorian Faber 	if (RPM == hdsp->io_type) {
358428b26e15SFlorian Faber 		if (hdsp->control_register & HDSP_RPM_Bypass)
358528b26e15SFlorian Faber 			snd_iprintf(buffer, "RPM Bypass: disabled\n");
358628b26e15SFlorian Faber 		else
358728b26e15SFlorian Faber 			snd_iprintf(buffer, "RPM Bypass: enabled\n");
358828b26e15SFlorian Faber 		if (hdsp->control_register & HDSP_RPM_Disconnect)
358928b26e15SFlorian Faber 			snd_iprintf(buffer, "RPM disconnected\n");
359028b26e15SFlorian Faber 		else
359128b26e15SFlorian Faber 			snd_iprintf(buffer, "RPM connected\n");
359228b26e15SFlorian Faber 
359328b26e15SFlorian Faber 		switch (hdsp->control_register & HDSP_RPM_Inp12) {
359428b26e15SFlorian Faber 		case HDSP_RPM_Inp12_Phon_6dB:
359528b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
359628b26e15SFlorian Faber 			break;
359728b26e15SFlorian Faber 		case HDSP_RPM_Inp12_Phon_0dB:
359828b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
359928b26e15SFlorian Faber 			break;
360028b26e15SFlorian Faber 		case HDSP_RPM_Inp12_Phon_n6dB:
360128b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
360228b26e15SFlorian Faber 			break;
360328b26e15SFlorian Faber 		case HDSP_RPM_Inp12_Line_0dB:
360428b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
360528b26e15SFlorian Faber 			break;
360628b26e15SFlorian Faber 		case HDSP_RPM_Inp12_Line_n6dB:
360728b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
360828b26e15SFlorian Faber 			break;
360928b26e15SFlorian Faber 		default:
361028b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 1/2: ???\n");
361128b26e15SFlorian Faber 		}
361228b26e15SFlorian Faber 
361328b26e15SFlorian Faber 		switch (hdsp->control_register & HDSP_RPM_Inp34) {
361428b26e15SFlorian Faber 		case HDSP_RPM_Inp34_Phon_6dB:
361528b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
361628b26e15SFlorian Faber 			break;
361728b26e15SFlorian Faber 		case HDSP_RPM_Inp34_Phon_0dB:
361828b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
361928b26e15SFlorian Faber 			break;
362028b26e15SFlorian Faber 		case HDSP_RPM_Inp34_Phon_n6dB:
362128b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
362228b26e15SFlorian Faber 			break;
362328b26e15SFlorian Faber 		case HDSP_RPM_Inp34_Line_0dB:
362428b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
362528b26e15SFlorian Faber 			break;
362628b26e15SFlorian Faber 		case HDSP_RPM_Inp34_Line_n6dB:
362728b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
362828b26e15SFlorian Faber 			break;
362928b26e15SFlorian Faber 		default:
363028b26e15SFlorian Faber 			snd_iprintf(buffer, "Input 3/4: ???\n");
363128b26e15SFlorian Faber 		}
363228b26e15SFlorian Faber 
363328b26e15SFlorian Faber 	} else {
3634b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_SPDIFOpticalOut)
36351da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3636b0b98119STakashi Iwai 		else
36371da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
36381da177e4SLinus Torvalds 
3639b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_SPDIFProfessional)
36401da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 quality: Professional\n");
3641b0b98119STakashi Iwai 		else
36421da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 quality: Consumer\n");
36431da177e4SLinus Torvalds 
3644b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_SPDIFEmphasis)
36451da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 emphasis: on\n");
3646b0b98119STakashi Iwai 		else
36471da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 emphasis: off\n");
36481da177e4SLinus Torvalds 
3649b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_SPDIFNonAudio)
36501da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3651b0b98119STakashi Iwai 		else
36521da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 NonAudio: off\n");
365328b26e15SFlorian Faber 		x = hdsp_spdif_sample_rate(hdsp);
365428b26e15SFlorian Faber 		if (x != 0)
36551da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3656b0b98119STakashi Iwai 		else
36571da177e4SLinus Torvalds 			snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
365828b26e15SFlorian Faber 	}
36591da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	/* Sync Check */
36621da177e4SLinus Torvalds 	x = status & HDSP_Sync0;
3663b0b98119STakashi Iwai 	if (status & HDSP_Lock0)
36641da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3665b0b98119STakashi Iwai 	else
36661da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT1: No Lock\n");
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds 	switch (hdsp->io_type) {
36691da177e4SLinus Torvalds 	case Digiface:
36701da177e4SLinus Torvalds 	case H9652:
36711da177e4SLinus Torvalds 		x = status & HDSP_Sync1;
3672b0b98119STakashi Iwai 		if (status & HDSP_Lock1)
36731da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3674b0b98119STakashi Iwai 		else
36751da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT2: No Lock\n");
36761da177e4SLinus Torvalds 		x = status & HDSP_Sync2;
3677b0b98119STakashi Iwai 		if (status & HDSP_Lock2)
36781da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3679b0b98119STakashi Iwai 		else
36801da177e4SLinus Torvalds 			snd_iprintf(buffer, "ADAT3: No Lock\n");
3681b0b98119STakashi Iwai 		break;
36821da177e4SLinus Torvalds 	default:
36831da177e4SLinus Torvalds 		/* relax */
36841da177e4SLinus Torvalds 		break;
36851da177e4SLinus Torvalds 	}
36861da177e4SLinus Torvalds 
36871da177e4SLinus Torvalds 	x = status & HDSP_SPDIFSync;
3688b0b98119STakashi Iwai 	if (status & HDSP_SPDIFErrorFlag)
36891da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: No Lock\n");
3690b0b98119STakashi Iwai 	else
36911da177e4SLinus Torvalds 		snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
36921da177e4SLinus Torvalds 
36931da177e4SLinus Torvalds 	x = status2 & HDSP_wc_sync;
3694b0b98119STakashi Iwai 	if (status2 & HDSP_wc_lock)
36951da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3696b0b98119STakashi Iwai 	else
36971da177e4SLinus Torvalds 		snd_iprintf (buffer, "Word Clock: No Lock\n");
36981da177e4SLinus Torvalds 
36991da177e4SLinus Torvalds 	x = status & HDSP_TimecodeSync;
3700b0b98119STakashi Iwai 	if (status & HDSP_TimecodeLock)
37011da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3702b0b98119STakashi Iwai 	else
37031da177e4SLinus Torvalds 		snd_iprintf(buffer, "ADAT Sync: No Lock\n");
37041da177e4SLinus Torvalds 
37051da177e4SLinus Torvalds 	snd_iprintf(buffer, "\n");
37061da177e4SLinus Torvalds 
37071da177e4SLinus Torvalds 	/* Informations about H9632 specific controls */
37081da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
37091da177e4SLinus Torvalds 		char *tmp;
37101da177e4SLinus Torvalds 
37111da177e4SLinus Torvalds 		switch (hdsp_ad_gain(hdsp)) {
37121da177e4SLinus Torvalds 		case 0:
37131da177e4SLinus Torvalds 			tmp = "-10 dBV";
37141da177e4SLinus Torvalds 			break;
37151da177e4SLinus Torvalds 		case 1:
37161da177e4SLinus Torvalds 			tmp = "+4 dBu";
37171da177e4SLinus Torvalds 			break;
37181da177e4SLinus Torvalds 		default:
37191da177e4SLinus Torvalds 			tmp = "Lo Gain";
37201da177e4SLinus Torvalds 			break;
37211da177e4SLinus Torvalds 		}
37221da177e4SLinus Torvalds 		snd_iprintf(buffer, "AD Gain : %s\n", tmp);
37231da177e4SLinus Torvalds 
37241da177e4SLinus Torvalds 		switch (hdsp_da_gain(hdsp)) {
37251da177e4SLinus Torvalds 		case 0:
37261da177e4SLinus Torvalds 			tmp = "Hi Gain";
37271da177e4SLinus Torvalds 			break;
37281da177e4SLinus Torvalds 		case 1:
37291da177e4SLinus Torvalds 			tmp = "+4 dBu";
37301da177e4SLinus Torvalds 			break;
37311da177e4SLinus Torvalds 		default:
37321da177e4SLinus Torvalds 			tmp = "-10 dBV";
37331da177e4SLinus Torvalds 			break;
37341da177e4SLinus Torvalds 		}
37351da177e4SLinus Torvalds 		snd_iprintf(buffer, "DA Gain : %s\n", tmp);
37361da177e4SLinus Torvalds 
37371da177e4SLinus Torvalds 		switch (hdsp_phone_gain(hdsp)) {
37381da177e4SLinus Torvalds 		case 0:
37391da177e4SLinus Torvalds 			tmp = "0 dB";
37401da177e4SLinus Torvalds 			break;
37411da177e4SLinus Torvalds 		case 1:
37421da177e4SLinus Torvalds 			tmp = "-6 dB";
37431da177e4SLinus Torvalds 			break;
37441da177e4SLinus Torvalds 		default:
37451da177e4SLinus Torvalds 			tmp = "-12 dB";
37461da177e4SLinus Torvalds 			break;
37471da177e4SLinus Torvalds 		}
37481da177e4SLinus Torvalds 		snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
37491da177e4SLinus Torvalds 
37504833c673SAdrian Knoth 		snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
37514833c673SAdrian Knoth 			hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
37524833c673SAdrian Knoth 			"yes" : "no");
37531da177e4SLinus Torvalds 
3754b0b98119STakashi Iwai 		if (hdsp->control_register & HDSP_AnalogExtensionBoard)
37551da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3756b0b98119STakashi Iwai 		else
37571da177e4SLinus Torvalds 			snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
37581da177e4SLinus Torvalds 		snd_iprintf(buffer, "\n");
37591da177e4SLinus Torvalds 	}
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds 
37631374f8ceSRandy Dunlap static void snd_hdsp_proc_init(struct hdsp *hdsp)
37641da177e4SLinus Torvalds {
376547f2769bSTakashi Iwai 	snd_card_ro_proc_new(hdsp->card, "hdsp", hdsp, snd_hdsp_proc_read);
37661da177e4SLinus Torvalds }
37671da177e4SLinus Torvalds 
3768e23e7a14SBill Pemberton static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
37691da177e4SLinus Torvalds {
377043d35cccSTakashi Iwai 	struct snd_dma_buffer *capture_dma, *playback_dma;
37711da177e4SLinus Torvalds 
377243d35cccSTakashi Iwai 	capture_dma = snd_hammerfall_get_buffer(hdsp->pci, HDSP_DMA_AREA_BYTES);
377343d35cccSTakashi Iwai 	playback_dma = snd_hammerfall_get_buffer(hdsp->pci, HDSP_DMA_AREA_BYTES);
377443d35cccSTakashi Iwai 	if (!capture_dma || !playback_dma) {
3775a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev,
3776a54ba0feSTakashi Iwai 			"%s: no buffers available\n", hdsp->card_name);
37771da177e4SLinus Torvalds 		return -ENOMEM;
37781da177e4SLinus Torvalds 	}
37791da177e4SLinus Torvalds 
378043d35cccSTakashi Iwai 	/* copy to the own data for alignment */
378143d35cccSTakashi Iwai 	hdsp->capture_dma_buf = *capture_dma;
378243d35cccSTakashi Iwai 	hdsp->playback_dma_buf = *playback_dma;
37831da177e4SLinus Torvalds 
378443d35cccSTakashi Iwai 	/* Align to bus-space 64K boundary */
378543d35cccSTakashi Iwai 	hdsp->capture_dma_buf.addr = ALIGN(capture_dma->addr, 0x10000ul);
378643d35cccSTakashi Iwai 	hdsp->playback_dma_buf.addr = ALIGN(playback_dma->addr, 0x10000ul);
37871da177e4SLinus Torvalds 
37881da177e4SLinus Torvalds 	/* Tell the card where it is */
378943d35cccSTakashi Iwai 	hdsp_write(hdsp, HDSP_inputBufferAddress, hdsp->capture_dma_buf.addr);
379043d35cccSTakashi Iwai 	hdsp_write(hdsp, HDSP_outputBufferAddress, hdsp->playback_dma_buf.addr);
37911da177e4SLinus Torvalds 
379243d35cccSTakashi Iwai 	hdsp->capture_dma_buf.area += hdsp->capture_dma_buf.addr - capture_dma->addr;
379343d35cccSTakashi Iwai 	hdsp->playback_dma_buf.area += hdsp->playback_dma_buf.addr - playback_dma->addr;
379443d35cccSTakashi Iwai 	hdsp->capture_buffer = hdsp->capture_dma_buf.area;
379543d35cccSTakashi Iwai 	hdsp->playback_buffer = hdsp->playback_dma_buf.area;
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	return 0;
37981da177e4SLinus Torvalds }
37991da177e4SLinus Torvalds 
380055e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp)
38011da177e4SLinus Torvalds {
38021da177e4SLinus Torvalds 	unsigned int i;
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	/* ASSUMPTION: hdsp->lock is either held, or
38051da177e4SLinus Torvalds 	   there is no need to hold it (e.g. during module
3806561de31aSJoe Perches 	   initialization).
38071da177e4SLinus Torvalds 	 */
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 	/* set defaults:
38101da177e4SLinus Torvalds 
38111da177e4SLinus Torvalds 	   SPDIF Input via Coax
38121da177e4SLinus Torvalds 	   Master clock mode
38131da177e4SLinus Torvalds 	   maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
38141da177e4SLinus Torvalds 	                    which implies 2 4096 sample, 32Kbyte periods).
38151da177e4SLinus Torvalds            Enable line out.
38161da177e4SLinus Torvalds 	 */
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 	hdsp->control_register = HDSP_ClockModeMaster |
38191da177e4SLinus Torvalds 		                 HDSP_SPDIFInputCoaxial |
38201da177e4SLinus Torvalds 		                 hdsp_encode_latency(7) |
38211da177e4SLinus Torvalds 		                 HDSP_LineOut;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
38271da177e4SLinus Torvalds 	hdsp->control2_register = HDSP_BIGENDIAN_MODE;
38281da177e4SLinus Torvalds #else
38291da177e4SLinus Torvalds 	hdsp->control2_register = 0;
38301da177e4SLinus Torvalds #endif
3831b0b98119STakashi Iwai 	if (hdsp->io_type == H9652)
38321da177e4SLinus Torvalds 	        snd_hdsp_9652_enable_mixer (hdsp);
3833b0b98119STakashi Iwai 	else
38341da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	hdsp_reset_hw_pointer(hdsp);
38371da177e4SLinus Torvalds 	hdsp_compute_period_size(hdsp);
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds 	/* silence everything */
38401da177e4SLinus Torvalds 
3841b0b98119STakashi Iwai 	for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
38421da177e4SLinus Torvalds 		hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3845b0b98119STakashi Iwai 		if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
38461da177e4SLinus Torvalds 			return -EIO;
38471da177e4SLinus Torvalds 	}
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	/* H9632 specific defaults */
38501da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
38511da177e4SLinus Torvalds 		hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
38521da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
38531da177e4SLinus Torvalds 	}
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds 	/* set a default rate so that the channel map is set up.
38561da177e4SLinus Torvalds 	 */
38571da177e4SLinus Torvalds 
38581da177e4SLinus Torvalds 	hdsp_set_rate(hdsp, 48000, 1);
38591da177e4SLinus Torvalds 
38601da177e4SLinus Torvalds 	return 0;
38611da177e4SLinus Torvalds }
38621da177e4SLinus Torvalds 
38634adab848STakashi Iwai static void hdsp_midi_work(struct work_struct *work)
38641da177e4SLinus Torvalds {
38654adab848STakashi Iwai 	struct hdsp *hdsp = container_of(work, struct hdsp, midi_work);
38661da177e4SLinus Torvalds 
3867b0b98119STakashi Iwai 	if (hdsp->midi[0].pending)
38681da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[0]);
3869b0b98119STakashi Iwai 	if (hdsp->midi[1].pending)
38701da177e4SLinus Torvalds 		snd_hdsp_midi_input_read (&hdsp->midi[1]);
38711da177e4SLinus Torvalds }
38721da177e4SLinus Torvalds 
38737d12e780SDavid Howells static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
38741da177e4SLinus Torvalds {
387555e957d8STakashi Iwai 	struct hdsp *hdsp = (struct hdsp *) dev_id;
38761da177e4SLinus Torvalds 	unsigned int status;
38771da177e4SLinus Torvalds 	int audio;
38781da177e4SLinus Torvalds 	int midi0;
38791da177e4SLinus Torvalds 	int midi1;
38801da177e4SLinus Torvalds 	unsigned int midi0status;
38811da177e4SLinus Torvalds 	unsigned int midi1status;
38821da177e4SLinus Torvalds 	int schedule = 0;
38831da177e4SLinus Torvalds 
38841da177e4SLinus Torvalds 	status = hdsp_read(hdsp, HDSP_statusRegister);
38851da177e4SLinus Torvalds 
38861da177e4SLinus Torvalds 	audio = status & HDSP_audioIRQPending;
38871da177e4SLinus Torvalds 	midi0 = status & HDSP_midi0IRQPending;
38881da177e4SLinus Torvalds 	midi1 = status & HDSP_midi1IRQPending;
38891da177e4SLinus Torvalds 
3890b0b98119STakashi Iwai 	if (!audio && !midi0 && !midi1)
38911da177e4SLinus Torvalds 		return IRQ_NONE;
38921da177e4SLinus Torvalds 
38931da177e4SLinus Torvalds 	hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 	midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
38961da177e4SLinus Torvalds 	midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
38971da177e4SLinus Torvalds 
3898c2503cd3STakashi Iwai 	if (!(hdsp->state & HDSP_InitializationComplete))
3899c2503cd3STakashi Iwai 		return IRQ_HANDLED;
3900c2503cd3STakashi Iwai 
39011da177e4SLinus Torvalds 	if (audio) {
3902b0b98119STakashi Iwai 		if (hdsp->capture_substream)
39031da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
39041da177e4SLinus Torvalds 
3905b0b98119STakashi Iwai 		if (hdsp->playback_substream)
39061da177e4SLinus Torvalds 			snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
39071da177e4SLinus Torvalds 	}
39081da177e4SLinus Torvalds 
39091da177e4SLinus Torvalds 	if (midi0 && midi0status) {
39104adab848STakashi Iwai 		if (hdsp->use_midi_work) {
39111da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
39121da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
39131da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
39141da177e4SLinus Torvalds 			hdsp->midi[0].pending = 1;
39151da177e4SLinus Torvalds 			schedule = 1;
39161da177e4SLinus Torvalds 		} else {
39171da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[0]);
39181da177e4SLinus Torvalds 		}
39191da177e4SLinus Torvalds 	}
392028b26e15SFlorian Faber 	if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
39214adab848STakashi Iwai 		if (hdsp->use_midi_work) {
39221da177e4SLinus Torvalds 			/* we disable interrupts for this input until processing is done */
39231da177e4SLinus Torvalds 			hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
39241da177e4SLinus Torvalds 			hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
39251da177e4SLinus Torvalds 			hdsp->midi[1].pending = 1;
39261da177e4SLinus Torvalds 			schedule = 1;
39271da177e4SLinus Torvalds 		} else {
39281da177e4SLinus Torvalds 			snd_hdsp_midi_input_read (&hdsp->midi[1]);
39291da177e4SLinus Torvalds 		}
39301da177e4SLinus Torvalds 	}
39314adab848STakashi Iwai 	if (hdsp->use_midi_work && schedule)
39324adab848STakashi Iwai 		queue_work(system_highpri_wq, &hdsp->midi_work);
39331da177e4SLinus Torvalds 	return IRQ_HANDLED;
39341da177e4SLinus Torvalds }
39351da177e4SLinus Torvalds 
393655e957d8STakashi Iwai static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
39371da177e4SLinus Torvalds {
393855e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
39391da177e4SLinus Torvalds 	return hdsp_hw_pointer(hdsp);
39401da177e4SLinus Torvalds }
39411da177e4SLinus Torvalds 
3942*50895a55SJason A. Donenfeld static signed char *hdsp_channel_buffer_location(struct hdsp *hdsp,
39431da177e4SLinus Torvalds 					     int stream,
39441da177e4SLinus Torvalds 					     int channel)
39451da177e4SLinus Torvalds 
39461da177e4SLinus Torvalds {
39471da177e4SLinus Torvalds 	int mapped_channel;
39481da177e4SLinus Torvalds 
3949da3cec35STakashi Iwai         if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3950da3cec35STakashi Iwai 		return NULL;
39511da177e4SLinus Torvalds 
395266c8f759STakashi Iwai 	mapped_channel = hdsp->channel_map[channel];
395366c8f759STakashi Iwai 	if (mapped_channel < 0)
39541da177e4SLinus Torvalds 		return NULL;
39551da177e4SLinus Torvalds 
3956b0b98119STakashi Iwai 	if (stream == SNDRV_PCM_STREAM_CAPTURE)
39571da177e4SLinus Torvalds 		return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3958b0b98119STakashi Iwai 	else
39591da177e4SLinus Torvalds 		return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
39601da177e4SLinus Torvalds }
39611da177e4SLinus Torvalds 
39620925e6b2STakashi Iwai static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream,
39630925e6b2STakashi Iwai 				  int channel, unsigned long pos,
39640925e6b2STakashi Iwai 				  void __user *src, unsigned long count)
39651da177e4SLinus Torvalds {
396655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3967*50895a55SJason A. Donenfeld 	signed char *channel_buf;
39681da177e4SLinus Torvalds 
39690925e6b2STakashi Iwai 	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3970da3cec35STakashi Iwai 		return -EINVAL;
39711da177e4SLinus Torvalds 
39721da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3973da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
3974da3cec35STakashi Iwai 		return -EIO;
39750925e6b2STakashi Iwai 	if (copy_from_user(channel_buf + pos, src, count))
39761da177e4SLinus Torvalds 		return -EFAULT;
39770925e6b2STakashi Iwai 	return 0;
39781da177e4SLinus Torvalds }
39791da177e4SLinus Torvalds 
39800925e6b2STakashi Iwai static int snd_hdsp_playback_copy_kernel(struct snd_pcm_substream *substream,
39810925e6b2STakashi Iwai 					 int channel, unsigned long pos,
39820925e6b2STakashi Iwai 					 void *src, unsigned long count)
39831da177e4SLinus Torvalds {
398455e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3985*50895a55SJason A. Donenfeld 	signed char *channel_buf;
39861da177e4SLinus Torvalds 
39870925e6b2STakashi Iwai 	channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
39880925e6b2STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
39890925e6b2STakashi Iwai 		return -EIO;
39900925e6b2STakashi Iwai 	memcpy(channel_buf + pos, src, count);
39910925e6b2STakashi Iwai 	return 0;
39920925e6b2STakashi Iwai }
39930925e6b2STakashi Iwai 
39940925e6b2STakashi Iwai static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream,
39950925e6b2STakashi Iwai 				 int channel, unsigned long pos,
39960925e6b2STakashi Iwai 				 void __user *dst, unsigned long count)
39970925e6b2STakashi Iwai {
39980925e6b2STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
3999*50895a55SJason A. Donenfeld 	signed char *channel_buf;
40000925e6b2STakashi Iwai 
40010925e6b2STakashi Iwai 	if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
4002da3cec35STakashi Iwai 		return -EINVAL;
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
4005da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
4006da3cec35STakashi Iwai 		return -EIO;
40070925e6b2STakashi Iwai 	if (copy_to_user(dst, channel_buf + pos, count))
40081da177e4SLinus Torvalds 		return -EFAULT;
40090925e6b2STakashi Iwai 	return 0;
40101da177e4SLinus Torvalds }
40111da177e4SLinus Torvalds 
40120925e6b2STakashi Iwai static int snd_hdsp_capture_copy_kernel(struct snd_pcm_substream *substream,
40130925e6b2STakashi Iwai 					int channel, unsigned long pos,
40140925e6b2STakashi Iwai 					void *dst, unsigned long count)
40151da177e4SLinus Torvalds {
401655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4017*50895a55SJason A. Donenfeld 	signed char *channel_buf;
40181da177e4SLinus Torvalds 
40191da177e4SLinus Torvalds 	channel_buf = hdsp_channel_buffer_location(hdsp, substream->pstr->stream, channel);
4020da3cec35STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
4021da3cec35STakashi Iwai 		return -EIO;
40220925e6b2STakashi Iwai 	memcpy(dst, channel_buf + pos, count);
40230925e6b2STakashi Iwai 	return 0;
40240925e6b2STakashi Iwai }
40250925e6b2STakashi Iwai 
40260925e6b2STakashi Iwai static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream,
40270925e6b2STakashi Iwai 			       int channel, unsigned long pos,
40280925e6b2STakashi Iwai 			       unsigned long count)
40290925e6b2STakashi Iwai {
40300925e6b2STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
4031*50895a55SJason A. Donenfeld 	signed char *channel_buf;
40320925e6b2STakashi Iwai 
40330925e6b2STakashi Iwai 	channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
40340925e6b2STakashi Iwai 	if (snd_BUG_ON(!channel_buf))
40350925e6b2STakashi Iwai 		return -EIO;
40360925e6b2STakashi Iwai 	memset(channel_buf + pos, 0, count);
40370925e6b2STakashi Iwai 	return 0;
40381da177e4SLinus Torvalds }
40391da177e4SLinus Torvalds 
404055e957d8STakashi Iwai static int snd_hdsp_reset(struct snd_pcm_substream *substream)
40411da177e4SLinus Torvalds {
404255e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
404355e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
404455e957d8STakashi Iwai 	struct snd_pcm_substream *other;
40451da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
40461da177e4SLinus Torvalds 		other = hdsp->capture_substream;
40471da177e4SLinus Torvalds 	else
40481da177e4SLinus Torvalds 		other = hdsp->playback_substream;
40491da177e4SLinus Torvalds 	if (hdsp->running)
40501da177e4SLinus Torvalds 		runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
40511da177e4SLinus Torvalds 	else
40521da177e4SLinus Torvalds 		runtime->status->hw_ptr = 0;
40531da177e4SLinus Torvalds 	if (other) {
405455e957d8STakashi Iwai 		struct snd_pcm_substream *s;
405555e957d8STakashi Iwai 		struct snd_pcm_runtime *oruntime = other->runtime;
4056ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
40571da177e4SLinus Torvalds 			if (s == other) {
40581da177e4SLinus Torvalds 				oruntime->status->hw_ptr = runtime->status->hw_ptr;
40591da177e4SLinus Torvalds 				break;
40601da177e4SLinus Torvalds 			}
40611da177e4SLinus Torvalds 		}
40621da177e4SLinus Torvalds 	}
40631da177e4SLinus Torvalds 	return 0;
40641da177e4SLinus Torvalds }
40651da177e4SLinus Torvalds 
406655e957d8STakashi Iwai static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
406755e957d8STakashi Iwai 				 struct snd_pcm_hw_params *params)
40681da177e4SLinus Torvalds {
406955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
40701da177e4SLinus Torvalds 	int err;
40711da177e4SLinus Torvalds 	pid_t this_pid;
40721da177e4SLinus Torvalds 	pid_t other_pid;
40731da177e4SLinus Torvalds 
4074b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
40751da177e4SLinus Torvalds 		return -EIO;
40761da177e4SLinus Torvalds 
4077b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
40781da177e4SLinus Torvalds 		return -EIO;
40791da177e4SLinus Torvalds 
40801da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
40811da177e4SLinus Torvalds 
40821da177e4SLinus Torvalds 	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
40831da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
40841da177e4SLinus Torvalds 		hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
40851da177e4SLinus Torvalds 		this_pid = hdsp->playback_pid;
40861da177e4SLinus Torvalds 		other_pid = hdsp->capture_pid;
40871da177e4SLinus Torvalds 	} else {
40881da177e4SLinus Torvalds 		this_pid = hdsp->capture_pid;
40891da177e4SLinus Torvalds 		other_pid = hdsp->playback_pid;
40901da177e4SLinus Torvalds 	}
40911da177e4SLinus Torvalds 
40921da177e4SLinus Torvalds 	if ((other_pid > 0) && (this_pid != other_pid)) {
40931da177e4SLinus Torvalds 
40941da177e4SLinus Torvalds 		/* The other stream is open, and not by the same
40951da177e4SLinus Torvalds 		   task as this one. Make sure that the parameters
40961da177e4SLinus Torvalds 		   that matter are the same.
40971da177e4SLinus Torvalds 		 */
40981da177e4SLinus Torvalds 
40991da177e4SLinus Torvalds 		if (params_rate(params) != hdsp->system_sample_rate) {
41001da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
41011da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
41021da177e4SLinus Torvalds 			return -EBUSY;
41031da177e4SLinus Torvalds 		}
41041da177e4SLinus Torvalds 
41051da177e4SLinus Torvalds 		if (params_period_size(params) != hdsp->period_bytes / 4) {
41061da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
41071da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
41081da177e4SLinus Torvalds 			return -EBUSY;
41091da177e4SLinus Torvalds 		}
41101da177e4SLinus Torvalds 
41111da177e4SLinus Torvalds 		/* We're fine. */
41121da177e4SLinus Torvalds 
41131da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
41141da177e4SLinus Torvalds  		return 0;
41151da177e4SLinus Torvalds 
41161da177e4SLinus Torvalds 	} else {
41171da177e4SLinus Torvalds 		spin_unlock_irq(&hdsp->lock);
41181da177e4SLinus Torvalds 	}
41191da177e4SLinus Torvalds 
41201da177e4SLinus Torvalds 	/* how to make sure that the rate matches an externally-set one ?
41211da177e4SLinus Torvalds 	 */
41221da177e4SLinus Torvalds 
41231da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
4124e3ea4d89STakashi Iwai 	if (! hdsp->clock_source_locked) {
412566c8f759STakashi Iwai 		err = hdsp_set_rate(hdsp, params_rate(params), 0);
412666c8f759STakashi Iwai 		if (err < 0) {
41271da177e4SLinus Torvalds 			spin_unlock_irq(&hdsp->lock);
41281da177e4SLinus Torvalds 			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
41291da177e4SLinus Torvalds 			return err;
41301da177e4SLinus Torvalds 		}
4131e3ea4d89STakashi Iwai 	}
4132e3ea4d89STakashi Iwai 	spin_unlock_irq(&hdsp->lock);
41331da177e4SLinus Torvalds 
413466c8f759STakashi Iwai 	err = hdsp_set_interrupt_interval(hdsp, params_period_size(params));
413566c8f759STakashi Iwai 	if (err < 0) {
41361da177e4SLinus Torvalds 		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
41371da177e4SLinus Torvalds 		return err;
41381da177e4SLinus Torvalds 	}
41391da177e4SLinus Torvalds 
41401da177e4SLinus Torvalds 	return 0;
41411da177e4SLinus Torvalds }
41421da177e4SLinus Torvalds 
414355e957d8STakashi Iwai static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
414455e957d8STakashi Iwai 				    struct snd_pcm_channel_info *info)
41451da177e4SLinus Torvalds {
414655e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
41470b84304eSGustavo A. R. Silva 	unsigned int channel = info->channel;
41481da177e4SLinus Torvalds 
41490b84304eSGustavo A. R. Silva 	if (snd_BUG_ON(channel >= hdsp->max_channels))
41500b84304eSGustavo A. R. Silva 		return -EINVAL;
41510b84304eSGustavo A. R. Silva 	channel = array_index_nospec(channel, hdsp->max_channels);
41520b84304eSGustavo A. R. Silva 
41530b84304eSGustavo A. R. Silva 	if (hdsp->channel_map[channel] < 0)
4154da3cec35STakashi Iwai 		return -EINVAL;
41551da177e4SLinus Torvalds 
41560b84304eSGustavo A. R. Silva 	info->offset = hdsp->channel_map[channel] * HDSP_CHANNEL_BUFFER_BYTES;
41571da177e4SLinus Torvalds 	info->first = 0;
41581da177e4SLinus Torvalds 	info->step = 32;
41591da177e4SLinus Torvalds 	return 0;
41601da177e4SLinus Torvalds }
41611da177e4SLinus Torvalds 
416255e957d8STakashi Iwai static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
41631da177e4SLinus Torvalds 			     unsigned int cmd, void *arg)
41641da177e4SLinus Torvalds {
41651da177e4SLinus Torvalds 	switch (cmd) {
41661da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_RESET:
41671da177e4SLinus Torvalds 		return snd_hdsp_reset(substream);
41681da177e4SLinus Torvalds 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
4169b0b98119STakashi Iwai 		return snd_hdsp_channel_info(substream, arg);
41701da177e4SLinus Torvalds 	default:
41711da177e4SLinus Torvalds 		break;
41721da177e4SLinus Torvalds 	}
41731da177e4SLinus Torvalds 
41741da177e4SLinus Torvalds 	return snd_pcm_lib_ioctl(substream, cmd, arg);
41751da177e4SLinus Torvalds }
41761da177e4SLinus Torvalds 
417755e957d8STakashi Iwai static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
41781da177e4SLinus Torvalds {
417955e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
418055e957d8STakashi Iwai 	struct snd_pcm_substream *other;
41811da177e4SLinus Torvalds 	int running;
41821da177e4SLinus Torvalds 
4183b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
41841da177e4SLinus Torvalds 		return -EIO;
41851da177e4SLinus Torvalds 
4186311e70a4STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
41871da177e4SLinus Torvalds 		return -EIO;
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 	spin_lock(&hdsp->lock);
41901da177e4SLinus Torvalds 	running = hdsp->running;
41911da177e4SLinus Torvalds 	switch (cmd) {
41921da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
41931da177e4SLinus Torvalds 		running |= 1 << substream->stream;
41941da177e4SLinus Torvalds 		break;
41951da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
41961da177e4SLinus Torvalds 		running &= ~(1 << substream->stream);
41971da177e4SLinus Torvalds 		break;
41981da177e4SLinus Torvalds 	default:
41991da177e4SLinus Torvalds 		snd_BUG();
42001da177e4SLinus Torvalds 		spin_unlock(&hdsp->lock);
42011da177e4SLinus Torvalds 		return -EINVAL;
42021da177e4SLinus Torvalds 	}
42031da177e4SLinus Torvalds 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
42041da177e4SLinus Torvalds 		other = hdsp->capture_substream;
42051da177e4SLinus Torvalds 	else
42061da177e4SLinus Torvalds 		other = hdsp->playback_substream;
42071da177e4SLinus Torvalds 
42081da177e4SLinus Torvalds 	if (other) {
420955e957d8STakashi Iwai 		struct snd_pcm_substream *s;
4210ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
42111da177e4SLinus Torvalds 			if (s == other) {
42121da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
42131da177e4SLinus Torvalds 				if (cmd == SNDRV_PCM_TRIGGER_START)
42141da177e4SLinus Torvalds 					running |= 1 << s->stream;
42151da177e4SLinus Torvalds 				else
42161da177e4SLinus Torvalds 					running &= ~(1 << s->stream);
42171da177e4SLinus Torvalds 				goto _ok;
42181da177e4SLinus Torvalds 			}
42191da177e4SLinus Torvalds 		}
42201da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
42211da177e4SLinus Torvalds 			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
42221da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
42231da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
42241da177e4SLinus Torvalds 		} else {
42251da177e4SLinus Torvalds 			if (running &&
42261da177e4SLinus Torvalds 			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
42271da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
42281da177e4SLinus Torvalds 		}
42291da177e4SLinus Torvalds 	} else {
42301da177e4SLinus Torvalds 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
42311da177e4SLinus Torvalds 				hdsp_silence_playback(hdsp);
42321da177e4SLinus Torvalds 	}
42331da177e4SLinus Torvalds  _ok:
42341da177e4SLinus Torvalds 	snd_pcm_trigger_done(substream, substream);
42351da177e4SLinus Torvalds 	if (!hdsp->running && running)
42361da177e4SLinus Torvalds 		hdsp_start_audio(hdsp);
42371da177e4SLinus Torvalds 	else if (hdsp->running && !running)
42381da177e4SLinus Torvalds 		hdsp_stop_audio(hdsp);
42391da177e4SLinus Torvalds 	hdsp->running = running;
42401da177e4SLinus Torvalds 	spin_unlock(&hdsp->lock);
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds 	return 0;
42431da177e4SLinus Torvalds }
42441da177e4SLinus Torvalds 
424555e957d8STakashi Iwai static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
42461da177e4SLinus Torvalds {
424755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
42481da177e4SLinus Torvalds 	int result = 0;
42491da177e4SLinus Torvalds 
4250b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
42511da177e4SLinus Torvalds 		return -EIO;
42521da177e4SLinus Torvalds 
4253b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
42541da177e4SLinus Torvalds 		return -EIO;
42551da177e4SLinus Torvalds 
42561da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
42571da177e4SLinus Torvalds 	if (!hdsp->running)
42581da177e4SLinus Torvalds 		hdsp_reset_hw_pointer(hdsp);
42591da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
42601da177e4SLinus Torvalds 	return result;
42611da177e4SLinus Torvalds }
42621da177e4SLinus Torvalds 
426318ed1cf0SBhumika Goyal static const struct snd_pcm_hardware snd_hdsp_playback_subinfo =
42641da177e4SLinus Torvalds {
42651da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
42661da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
42671da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
42681da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START |
42691da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_DOUBLE),
42701da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
42711da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
42721da177e4SLinus Torvalds #else
42731da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
42741da177e4SLinus Torvalds #endif
42751da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
42761da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
42771da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
42781da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
42791da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
42801da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
42811da177e4SLinus Torvalds 	.rate_min =		32000,
42821da177e4SLinus Torvalds 	.rate_max =		96000,
428328b26e15SFlorian Faber 	.channels_min =		6,
42841da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
42851da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
42861da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
42871da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
42881da177e4SLinus Torvalds 	.periods_min =		2,
42891da177e4SLinus Torvalds 	.periods_max =		2,
42901da177e4SLinus Torvalds 	.fifo_size =		0
42911da177e4SLinus Torvalds };
42921da177e4SLinus Torvalds 
429318ed1cf0SBhumika Goyal static const struct snd_pcm_hardware snd_hdsp_capture_subinfo =
42941da177e4SLinus Torvalds {
42951da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
42961da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
42971da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_NONINTERLEAVED |
42981da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_SYNC_START),
42991da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
43001da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_BE,
43011da177e4SLinus Torvalds #else
43021da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
43031da177e4SLinus Torvalds #endif
43041da177e4SLinus Torvalds 	.rates =		(SNDRV_PCM_RATE_32000 |
43051da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_44100 |
43061da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_48000 |
43071da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_64000 |
43081da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_88200 |
43091da177e4SLinus Torvalds 				 SNDRV_PCM_RATE_96000),
43101da177e4SLinus Torvalds 	.rate_min =		32000,
43111da177e4SLinus Torvalds 	.rate_max =		96000,
431228b26e15SFlorian Faber 	.channels_min =		5,
43131da177e4SLinus Torvalds 	.channels_max =		HDSP_MAX_CHANNELS,
43141da177e4SLinus Torvalds 	.buffer_bytes_max =	HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
43151da177e4SLinus Torvalds 	.period_bytes_min =	(64 * 4) * 10,
43161da177e4SLinus Torvalds 	.period_bytes_max =	(8192 * 4) * HDSP_MAX_CHANNELS,
43171da177e4SLinus Torvalds 	.periods_min =		2,
43181da177e4SLinus Torvalds 	.periods_max =		2,
43191da177e4SLinus Torvalds 	.fifo_size =		0
43201da177e4SLinus Torvalds };
43211da177e4SLinus Torvalds 
43224640c034STakashi Iwai static const unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
43231da177e4SLinus Torvalds 
43244640c034STakashi Iwai static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
43251da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_period_sizes),
43261da177e4SLinus Torvalds 	.list = hdsp_period_sizes,
43271da177e4SLinus Torvalds 	.mask = 0
43281da177e4SLinus Torvalds };
43291da177e4SLinus Torvalds 
43304640c034STakashi Iwai static const unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
43311da177e4SLinus Torvalds 
43324640c034STakashi Iwai static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
43331da177e4SLinus Torvalds 	.count = ARRAY_SIZE(hdsp_9632_sample_rates),
43341da177e4SLinus Torvalds 	.list = hdsp_9632_sample_rates,
43351da177e4SLinus Torvalds 	.mask = 0
43361da177e4SLinus Torvalds };
43371da177e4SLinus Torvalds 
433855e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
433955e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
43401da177e4SLinus Torvalds {
434155e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
434255e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
43431da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
43441da177e4SLinus Torvalds 		unsigned int list[3];
43451da177e4SLinus Torvalds 		list[0] = hdsp->qs_in_channels;
43461da177e4SLinus Torvalds 		list[1] = hdsp->ds_in_channels;
43471da177e4SLinus Torvalds 		list[2] = hdsp->ss_in_channels;
43481da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
43491da177e4SLinus Torvalds 	} else {
43501da177e4SLinus Torvalds 		unsigned int list[2];
43511da177e4SLinus Torvalds 		list[0] = hdsp->ds_in_channels;
43521da177e4SLinus Torvalds 		list[1] = hdsp->ss_in_channels;
43531da177e4SLinus Torvalds 		return snd_interval_list(c, 2, list, 0);
43541da177e4SLinus Torvalds 	}
43551da177e4SLinus Torvalds }
43561da177e4SLinus Torvalds 
435755e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
435855e957d8STakashi Iwai 					struct snd_pcm_hw_rule *rule)
43591da177e4SLinus Torvalds {
43601da177e4SLinus Torvalds 	unsigned int list[3];
436155e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
436255e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
43631da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
43641da177e4SLinus Torvalds 		list[0] = hdsp->qs_out_channels;
43651da177e4SLinus Torvalds 		list[1] = hdsp->ds_out_channels;
43661da177e4SLinus Torvalds 		list[2] = hdsp->ss_out_channels;
43671da177e4SLinus Torvalds 		return snd_interval_list(c, 3, list, 0);
43681da177e4SLinus Torvalds 	} else {
43691da177e4SLinus Torvalds 		list[0] = hdsp->ds_out_channels;
43701da177e4SLinus Torvalds 		list[1] = hdsp->ss_out_channels;
43711da177e4SLinus Torvalds 	}
43721da177e4SLinus Torvalds 	return snd_interval_list(c, 2, list, 0);
43731da177e4SLinus Torvalds }
43741da177e4SLinus Torvalds 
437555e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
437655e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
43771da177e4SLinus Torvalds {
437855e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
437955e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
438055e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
43811da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
438255e957d8STakashi Iwai 		struct snd_interval t = {
43831da177e4SLinus Torvalds 			.min = hdsp->qs_in_channels,
43841da177e4SLinus Torvalds 			.max = hdsp->qs_in_channels,
43851da177e4SLinus Torvalds 			.integer = 1,
43861da177e4SLinus Torvalds 		};
43871da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
43881da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
438955e957d8STakashi Iwai 		struct snd_interval t = {
43901da177e4SLinus Torvalds 			.min = hdsp->ds_in_channels,
43911da177e4SLinus Torvalds 			.max = hdsp->ds_in_channels,
43921da177e4SLinus Torvalds 			.integer = 1,
43931da177e4SLinus Torvalds 		};
43941da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
43951da177e4SLinus Torvalds 	} else if (r->max < 64000) {
439655e957d8STakashi Iwai 		struct snd_interval t = {
43971da177e4SLinus Torvalds 			.min = hdsp->ss_in_channels,
43981da177e4SLinus Torvalds 			.max = hdsp->ss_in_channels,
43991da177e4SLinus Torvalds 			.integer = 1,
44001da177e4SLinus Torvalds 		};
44011da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
44021da177e4SLinus Torvalds 	}
44031da177e4SLinus Torvalds 	return 0;
44041da177e4SLinus Torvalds }
44051da177e4SLinus Torvalds 
440655e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
440755e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
44081da177e4SLinus Torvalds {
440955e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
441055e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
441155e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
44121da177e4SLinus Torvalds 	if (r->min > 96000 && hdsp->io_type == H9632) {
441355e957d8STakashi Iwai 		struct snd_interval t = {
44141da177e4SLinus Torvalds 			.min = hdsp->qs_out_channels,
44151da177e4SLinus Torvalds 			.max = hdsp->qs_out_channels,
44161da177e4SLinus Torvalds 			.integer = 1,
44171da177e4SLinus Torvalds 		};
44181da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
44191da177e4SLinus Torvalds 	} else if (r->min > 48000 && r->max <= 96000) {
442055e957d8STakashi Iwai 		struct snd_interval t = {
44211da177e4SLinus Torvalds 			.min = hdsp->ds_out_channels,
44221da177e4SLinus Torvalds 			.max = hdsp->ds_out_channels,
44231da177e4SLinus Torvalds 			.integer = 1,
44241da177e4SLinus Torvalds 		};
44251da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
44261da177e4SLinus Torvalds 	} else if (r->max < 64000) {
442755e957d8STakashi Iwai 		struct snd_interval t = {
44281da177e4SLinus Torvalds 			.min = hdsp->ss_out_channels,
44291da177e4SLinus Torvalds 			.max = hdsp->ss_out_channels,
44301da177e4SLinus Torvalds 			.integer = 1,
44311da177e4SLinus Torvalds 		};
44321da177e4SLinus Torvalds 		return snd_interval_refine(c, &t);
44331da177e4SLinus Torvalds 	}
44341da177e4SLinus Torvalds 	return 0;
44351da177e4SLinus Torvalds }
44361da177e4SLinus Torvalds 
443755e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
443855e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
44391da177e4SLinus Torvalds {
444055e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
444155e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
444255e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
44431da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_out_channels) {
444455e957d8STakashi Iwai 		struct snd_interval t = {
44451da177e4SLinus Torvalds 			.min = 32000,
44461da177e4SLinus Torvalds 			.max = 48000,
44471da177e4SLinus Torvalds 			.integer = 1,
44481da177e4SLinus Torvalds 		};
44491da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44501da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
445155e957d8STakashi Iwai 		struct snd_interval t = {
44521da177e4SLinus Torvalds 			.min = 128000,
44531da177e4SLinus Torvalds 			.max = 192000,
44541da177e4SLinus Torvalds 			.integer = 1,
44551da177e4SLinus Torvalds 		};
44561da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44571da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_out_channels) {
445855e957d8STakashi Iwai 		struct snd_interval t = {
44591da177e4SLinus Torvalds 			.min = 64000,
44601da177e4SLinus Torvalds 			.max = 96000,
44611da177e4SLinus Torvalds 			.integer = 1,
44621da177e4SLinus Torvalds 		};
44631da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44641da177e4SLinus Torvalds 	}
44651da177e4SLinus Torvalds 	return 0;
44661da177e4SLinus Torvalds }
44671da177e4SLinus Torvalds 
446855e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
446955e957d8STakashi Iwai 					     struct snd_pcm_hw_rule *rule)
44701da177e4SLinus Torvalds {
447155e957d8STakashi Iwai 	struct hdsp *hdsp = rule->private;
447255e957d8STakashi Iwai 	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
447355e957d8STakashi Iwai 	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
44741da177e4SLinus Torvalds 	if (c->min >= hdsp->ss_in_channels) {
447555e957d8STakashi Iwai 		struct snd_interval t = {
44761da177e4SLinus Torvalds 			.min = 32000,
44771da177e4SLinus Torvalds 			.max = 48000,
44781da177e4SLinus Torvalds 			.integer = 1,
44791da177e4SLinus Torvalds 		};
44801da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44811da177e4SLinus Torvalds 	} else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
448255e957d8STakashi Iwai 		struct snd_interval t = {
44831da177e4SLinus Torvalds 			.min = 128000,
44841da177e4SLinus Torvalds 			.max = 192000,
44851da177e4SLinus Torvalds 			.integer = 1,
44861da177e4SLinus Torvalds 		};
44871da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44881da177e4SLinus Torvalds 	} else if (c->max <= hdsp->ds_in_channels) {
448955e957d8STakashi Iwai 		struct snd_interval t = {
44901da177e4SLinus Torvalds 			.min = 64000,
44911da177e4SLinus Torvalds 			.max = 96000,
44921da177e4SLinus Torvalds 			.integer = 1,
44931da177e4SLinus Torvalds 		};
44941da177e4SLinus Torvalds 		return snd_interval_refine(r, &t);
44951da177e4SLinus Torvalds 	}
44961da177e4SLinus Torvalds 	return 0;
44971da177e4SLinus Torvalds }
44981da177e4SLinus Torvalds 
449955e957d8STakashi Iwai static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
45001da177e4SLinus Torvalds {
450155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
450255e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
45031da177e4SLinus Torvalds 
4504b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
45051da177e4SLinus Torvalds 		return -EIO;
45061da177e4SLinus Torvalds 
4507b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
45081da177e4SLinus Torvalds 		return -EIO;
45091da177e4SLinus Torvalds 
45101da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
45111da177e4SLinus Torvalds 
45121da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
45131da177e4SLinus Torvalds 
45141da177e4SLinus Torvalds         runtime->hw = snd_hdsp_playback_subinfo;
451543d35cccSTakashi Iwai 	snd_pcm_set_runtime_buffer(substream, &hdsp->playback_dma_buf);
45161da177e4SLinus Torvalds 
45171da177e4SLinus Torvalds 	hdsp->playback_pid = current->pid;
45181da177e4SLinus Torvalds 	hdsp->playback_substream = substream;
45191da177e4SLinus Torvalds 
45201da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
45211da177e4SLinus Torvalds 
45221da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
45231da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4524e3ea4d89STakashi Iwai 	if (hdsp->clock_source_locked) {
4525e3ea4d89STakashi Iwai 		runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4526e3ea4d89STakashi Iwai 	} else if (hdsp->io_type == H9632) {
45271da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
45281da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
45291da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
45301da177e4SLinus Torvalds 	}
4531e3ea4d89STakashi Iwai 	if (hdsp->io_type == H9632) {
4532e3ea4d89STakashi Iwai 		runtime->hw.channels_min = hdsp->qs_out_channels;
4533e3ea4d89STakashi Iwai 		runtime->hw.channels_max = hdsp->ss_out_channels;
4534e3ea4d89STakashi Iwai 	}
45351da177e4SLinus Torvalds 
45361da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45371da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels, hdsp,
45381da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45391da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45401da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_out_channels_rate, hdsp,
45411da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
45421da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
45431da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_out_channels, hdsp,
45441da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45451da177e4SLinus Torvalds 
454628b26e15SFlorian Faber 	if (RPM != hdsp->io_type) {
45471da177e4SLinus Torvalds 		hdsp->creg_spdif_stream = hdsp->creg_spdif;
45481da177e4SLinus Torvalds 		hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
45491da177e4SLinus Torvalds 		snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
45501da177e4SLinus Torvalds 			SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
455128b26e15SFlorian Faber 	}
45521da177e4SLinus Torvalds 	return 0;
45531da177e4SLinus Torvalds }
45541da177e4SLinus Torvalds 
455555e957d8STakashi Iwai static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
45561da177e4SLinus Torvalds {
455755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
45581da177e4SLinus Torvalds 
45591da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
45601da177e4SLinus Torvalds 
45611da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
45621da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
45631da177e4SLinus Torvalds 
45641da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
45651da177e4SLinus Torvalds 
456628b26e15SFlorian Faber 	if (RPM != hdsp->io_type) {
45671da177e4SLinus Torvalds 		hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
45681da177e4SLinus Torvalds 		snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
45691da177e4SLinus Torvalds 			SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
457028b26e15SFlorian Faber 	}
45711da177e4SLinus Torvalds 	return 0;
45721da177e4SLinus Torvalds }
45731da177e4SLinus Torvalds 
45741da177e4SLinus Torvalds 
457555e957d8STakashi Iwai static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
45761da177e4SLinus Torvalds {
457755e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
457855e957d8STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
45791da177e4SLinus Torvalds 
4580b0b98119STakashi Iwai 	if (hdsp_check_for_iobox (hdsp))
45811da177e4SLinus Torvalds 		return -EIO;
45821da177e4SLinus Torvalds 
4583b0b98119STakashi Iwai 	if (hdsp_check_for_firmware(hdsp, 1))
45841da177e4SLinus Torvalds 		return -EIO;
45851da177e4SLinus Torvalds 
45861da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
45871da177e4SLinus Torvalds 
45881da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
45891da177e4SLinus Torvalds 
45901da177e4SLinus Torvalds 	runtime->hw = snd_hdsp_capture_subinfo;
459143d35cccSTakashi Iwai 	snd_pcm_set_runtime_buffer(substream, &hdsp->capture_dma_buf);
45921da177e4SLinus Torvalds 
45931da177e4SLinus Torvalds 	hdsp->capture_pid = current->pid;
45941da177e4SLinus Torvalds 	hdsp->capture_substream = substream;
45951da177e4SLinus Torvalds 
45961da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
45971da177e4SLinus Torvalds 
45981da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
45991da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
46001da177e4SLinus Torvalds 	if (hdsp->io_type == H9632) {
46011da177e4SLinus Torvalds 		runtime->hw.channels_min = hdsp->qs_in_channels;
46021da177e4SLinus Torvalds 		runtime->hw.channels_max = hdsp->ss_in_channels;
46031da177e4SLinus Torvalds 		runtime->hw.rate_max = 192000;
46041da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
46051da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
46061da177e4SLinus Torvalds 	}
46071da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
46081da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels, hdsp,
46091da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
46101da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
46111da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_in_channels_rate, hdsp,
46121da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_RATE, -1);
46131da177e4SLinus Torvalds 	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
46141da177e4SLinus Torvalds 			     snd_hdsp_hw_rule_rate_in_channels, hdsp,
46151da177e4SLinus Torvalds 			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
46161da177e4SLinus Torvalds 	return 0;
46171da177e4SLinus Torvalds }
46181da177e4SLinus Torvalds 
461955e957d8STakashi Iwai static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
46201da177e4SLinus Torvalds {
462155e957d8STakashi Iwai 	struct hdsp *hdsp = snd_pcm_substream_chip(substream);
46221da177e4SLinus Torvalds 
46231da177e4SLinus Torvalds 	spin_lock_irq(&hdsp->lock);
46241da177e4SLinus Torvalds 
46251da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
46261da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
46271da177e4SLinus Torvalds 
46281da177e4SLinus Torvalds 	spin_unlock_irq(&hdsp->lock);
46291da177e4SLinus Torvalds 	return 0;
46301da177e4SLinus Torvalds }
46311da177e4SLinus Torvalds 
46321da177e4SLinus Torvalds /* helper functions for copying meter values */
46331da177e4SLinus Torvalds static inline int copy_u32_le(void __user *dest, void __iomem *src)
46341da177e4SLinus Torvalds {
46351da177e4SLinus Torvalds 	u32 val = readl(src);
46361da177e4SLinus Torvalds 	return copy_to_user(dest, &val, 4);
46371da177e4SLinus Torvalds }
46381da177e4SLinus Torvalds 
46391da177e4SLinus Torvalds static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
46401da177e4SLinus Torvalds {
46411da177e4SLinus Torvalds 	u32 rms_low, rms_high;
46421da177e4SLinus Torvalds 	u64 rms;
46431da177e4SLinus Torvalds 	rms_low = readl(src_low);
46441da177e4SLinus Torvalds 	rms_high = readl(src_high);
46451da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
46461da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
46471da177e4SLinus Torvalds }
46481da177e4SLinus Torvalds 
46491da177e4SLinus Torvalds static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
46501da177e4SLinus Torvalds {
46511da177e4SLinus Torvalds 	u32 rms_low, rms_high;
46521da177e4SLinus Torvalds 	u64 rms;
46531da177e4SLinus Torvalds 	rms_low = readl(src_low) & 0xffffff00;
46541da177e4SLinus Torvalds 	rms_high = readl(src_high) & 0xffffff00;
46551da177e4SLinus Torvalds 	rms = ((u64)rms_high << 32) | rms_low;
46561da177e4SLinus Torvalds 	return copy_to_user(dest, &rms, 8);
46571da177e4SLinus Torvalds }
46581da177e4SLinus Torvalds 
465955e957d8STakashi Iwai static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
46601da177e4SLinus Torvalds {
46611da177e4SLinus Torvalds 	int doublespeed = 0;
46621da177e4SLinus Torvalds 	int i, j, channels, ofs;
46631da177e4SLinus Torvalds 
46641da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
46651da177e4SLinus Torvalds 		doublespeed = 1;
46661da177e4SLinus Torvalds 	channels = doublespeed ? 14 : 26;
46671da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 26; ++i) {
46681da177e4SLinus Torvalds 		if (doublespeed && (i & 4))
46691da177e4SLinus Torvalds 			continue;
46701da177e4SLinus Torvalds 		ofs = HDSP_9652_peakBase - j * 4;
46711da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
46721da177e4SLinus Torvalds 			return -EFAULT;
46731da177e4SLinus Torvalds 		ofs -= channels * 4;
46741da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
46751da177e4SLinus Torvalds 			return -EFAULT;
46761da177e4SLinus Torvalds 		ofs -= channels * 4;
46771da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
46781da177e4SLinus Torvalds 			return -EFAULT;
46791da177e4SLinus Torvalds 		ofs = HDSP_9652_rmsBase + j * 8;
46801da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
46811da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
46821da177e4SLinus Torvalds 			return -EFAULT;
46831da177e4SLinus Torvalds 		ofs += channels * 8;
46841da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
46851da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
46861da177e4SLinus Torvalds 			return -EFAULT;
46871da177e4SLinus Torvalds 		ofs += channels * 8;
46881da177e4SLinus Torvalds 		if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
46891da177e4SLinus Torvalds 				hdsp->iobase + ofs + 4))
46901da177e4SLinus Torvalds 			return -EFAULT;
46911da177e4SLinus Torvalds 		j++;
46921da177e4SLinus Torvalds 	}
46931da177e4SLinus Torvalds 	return 0;
46941da177e4SLinus Torvalds }
46951da177e4SLinus Torvalds 
469655e957d8STakashi Iwai static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
46971da177e4SLinus Torvalds {
46981da177e4SLinus Torvalds 	int i, j;
469955e957d8STakashi Iwai 	struct hdsp_9632_meters __iomem *m;
47001da177e4SLinus Torvalds 	int doublespeed = 0;
47011da177e4SLinus Torvalds 
47021da177e4SLinus Torvalds 	if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
47031da177e4SLinus Torvalds 		doublespeed = 1;
470455e957d8STakashi Iwai 	m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
47051da177e4SLinus Torvalds 	for (i = 0, j = 0; i < 16; ++i, ++j) {
47061da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
47071da177e4SLinus Torvalds 			return -EFAULT;
47081da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
47091da177e4SLinus Torvalds 			return -EFAULT;
47101da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
47111da177e4SLinus Torvalds 			return -EFAULT;
47121da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
47131da177e4SLinus Torvalds 				&m->input_rms_high[j]))
47141da177e4SLinus Torvalds 			return -EFAULT;
47151da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
47161da177e4SLinus Torvalds 				&m->playback_rms_high[j]))
47171da177e4SLinus Torvalds 			return -EFAULT;
47181da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
47191da177e4SLinus Torvalds 				&m->output_rms_high[j]))
47201da177e4SLinus Torvalds 			return -EFAULT;
47211da177e4SLinus Torvalds 		if (doublespeed && i == 3) i += 4;
47221da177e4SLinus Torvalds 	}
47231da177e4SLinus Torvalds 	return 0;
47241da177e4SLinus Torvalds }
47251da177e4SLinus Torvalds 
472655e957d8STakashi Iwai static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
47271da177e4SLinus Torvalds {
47281da177e4SLinus Torvalds 	int i;
47291da177e4SLinus Torvalds 
47301da177e4SLinus Torvalds 	for (i = 0; i < 26; i++) {
47311da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->playback_peaks[i],
47321da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
47331da177e4SLinus Torvalds 			return -EFAULT;
47341da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->input_peaks[i],
47351da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputPeakLevel + i * 4))
47361da177e4SLinus Torvalds 			return -EFAULT;
47371da177e4SLinus Torvalds 	}
47381da177e4SLinus Torvalds 	for (i = 0; i < 28; i++) {
47391da177e4SLinus Torvalds 		if (copy_u32_le(&peak_rms->output_peaks[i],
47401da177e4SLinus Torvalds 				hdsp->iobase + HDSP_outputPeakLevel + i * 4))
47411da177e4SLinus Torvalds 			return -EFAULT;
47421da177e4SLinus Torvalds 	}
47431da177e4SLinus Torvalds 	for (i = 0; i < 26; ++i) {
47441da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->playback_rms[i],
47451da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
47461da177e4SLinus Torvalds 				hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
47471da177e4SLinus Torvalds 			return -EFAULT;
47481da177e4SLinus Torvalds 		if (copy_u64_le(&peak_rms->input_rms[i],
47491da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
47501da177e4SLinus Torvalds 				hdsp->iobase + HDSP_inputRmsLevel + i * 8))
47511da177e4SLinus Torvalds 			return -EFAULT;
47521da177e4SLinus Torvalds 	}
47531da177e4SLinus Torvalds 	return 0;
47541da177e4SLinus Torvalds }
47551da177e4SLinus Torvalds 
475655e957d8STakashi Iwai static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
47571da177e4SLinus Torvalds {
47589fe856e4SJoe Perches 	struct hdsp *hdsp = hw->private_data;
47591da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
47603ae7e2e2STim Blechmann 	int err;
47611da177e4SLinus Torvalds 
47621da177e4SLinus Torvalds 	switch (cmd) {
47631da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
476455e957d8STakashi Iwai 		struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
47651da177e4SLinus Torvalds 
47663ae7e2e2STim Blechmann 		err = hdsp_check_for_iobox(hdsp);
47673ae7e2e2STim Blechmann 		if (err < 0)
47683ae7e2e2STim Blechmann 			return err;
47693ae7e2e2STim Blechmann 
47703ae7e2e2STim Blechmann 		err = hdsp_check_for_firmware(hdsp, 1);
47713ae7e2e2STim Blechmann 		if (err < 0)
47723ae7e2e2STim Blechmann 			return err;
47733ae7e2e2STim Blechmann 
47741da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4775a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
4776a54ba0feSTakashi Iwai 				"firmware needs to be uploaded to the card.\n");
47771da177e4SLinus Torvalds 			return -EINVAL;
47781da177e4SLinus Torvalds 		}
47791da177e4SLinus Torvalds 
47801da177e4SLinus Torvalds 		switch (hdsp->io_type) {
47811da177e4SLinus Torvalds 		case H9652:
47821da177e4SLinus Torvalds 			return hdsp_9652_get_peak(hdsp, peak_rms);
47831da177e4SLinus Torvalds 		case H9632:
47841da177e4SLinus Torvalds 			return hdsp_9632_get_peak(hdsp, peak_rms);
47851da177e4SLinus Torvalds 		default:
47861da177e4SLinus Torvalds 			return hdsp_get_peak(hdsp, peak_rms);
47871da177e4SLinus Torvalds 		}
47881da177e4SLinus Torvalds 	}
47891da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
479055e957d8STakashi Iwai 		struct hdsp_config_info info;
47911da177e4SLinus Torvalds 		unsigned long flags;
47921da177e4SLinus Torvalds 		int i;
47931da177e4SLinus Torvalds 
47943ae7e2e2STim Blechmann 		err = hdsp_check_for_iobox(hdsp);
47953ae7e2e2STim Blechmann 		if (err < 0)
47963ae7e2e2STim Blechmann 			return err;
47973ae7e2e2STim Blechmann 
47983ae7e2e2STim Blechmann 		err = hdsp_check_for_firmware(hdsp, 1);
47993ae7e2e2STim Blechmann 		if (err < 0)
48003ae7e2e2STim Blechmann 			return err;
48013ae7e2e2STim Blechmann 
4802e68d3b31SDan Rosenberg 		memset(&info, 0, sizeof(info));
48031da177e4SLinus Torvalds 		spin_lock_irqsave(&hdsp->lock, flags);
48041da177e4SLinus Torvalds 		info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
48051da177e4SLinus Torvalds 		info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4806b0b98119STakashi Iwai 		if (hdsp->io_type != H9632)
48071da177e4SLinus Torvalds 		    info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
48081da177e4SLinus Torvalds 		info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
480928b26e15SFlorian Faber 		for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
48101da177e4SLinus Torvalds 			info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
48111da177e4SLinus Torvalds 		info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
48124833c673SAdrian Knoth 		info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
48134833c673SAdrian Knoth 				HDSP_SPDIFOpticalOut);
48144833c673SAdrian Knoth 		info.spdif_professional = (unsigned char)
48154833c673SAdrian Knoth 			hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
48164833c673SAdrian Knoth 		info.spdif_emphasis = (unsigned char)
48174833c673SAdrian Knoth 			hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
48184833c673SAdrian Knoth 		info.spdif_nonaudio = (unsigned char)
48194833c673SAdrian Knoth 			hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
48201da177e4SLinus Torvalds 		info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
48211da177e4SLinus Torvalds 		info.system_sample_rate = hdsp->system_sample_rate;
48221da177e4SLinus Torvalds 		info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
48231da177e4SLinus Torvalds 		info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
48241da177e4SLinus Torvalds 		info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
48251da177e4SLinus Torvalds 		info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
48264833c673SAdrian Knoth 		info.line_out = (unsigned char)
48274833c673SAdrian Knoth 			hdsp_toggle_setting(hdsp, HDSP_LineOut);
48281da177e4SLinus Torvalds 		if (hdsp->io_type == H9632) {
48291da177e4SLinus Torvalds 			info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
48301da177e4SLinus Torvalds 			info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
48311da177e4SLinus Torvalds 			info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
48324833c673SAdrian Knoth 			info.xlr_breakout_cable =
48334833c673SAdrian Knoth 				(unsigned char)hdsp_toggle_setting(hdsp,
48344833c673SAdrian Knoth 					HDSP_XLRBreakoutCable);
48351da177e4SLinus Torvalds 
483628b26e15SFlorian Faber 		} else if (hdsp->io_type == RPM) {
483728b26e15SFlorian Faber 			info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
483828b26e15SFlorian Faber 			info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
48391da177e4SLinus Torvalds 		}
4840b0b98119STakashi Iwai 		if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
48414833c673SAdrian Knoth 			info.analog_extension_board =
48424833c673SAdrian Knoth 				(unsigned char)hdsp_toggle_setting(hdsp,
48434833c673SAdrian Knoth 					    HDSP_AnalogExtensionBoard);
48441da177e4SLinus Torvalds 		spin_unlock_irqrestore(&hdsp->lock, flags);
48451da177e4SLinus Torvalds 		if (copy_to_user(argp, &info, sizeof(info)))
48461da177e4SLinus Torvalds 			return -EFAULT;
48471da177e4SLinus Torvalds 		break;
48481da177e4SLinus Torvalds 	}
48491da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
485055e957d8STakashi Iwai 		struct hdsp_9632_aeb h9632_aeb;
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds 		if (hdsp->io_type != H9632) return -EINVAL;
48531da177e4SLinus Torvalds 		h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
48541da177e4SLinus Torvalds 		h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
48551da177e4SLinus Torvalds 		if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
48561da177e4SLinus Torvalds 			return -EFAULT;
48571da177e4SLinus Torvalds 		break;
48581da177e4SLinus Torvalds 	}
48591da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_VERSION: {
486055e957d8STakashi Iwai 		struct hdsp_version hdsp_version;
48611da177e4SLinus Torvalds 		int err;
48621da177e4SLinus Torvalds 
48631da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
48641da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) {
486566c8f759STakashi Iwai 			err = hdsp_get_iobox_version(hdsp);
486666c8f759STakashi Iwai 			if (err < 0)
48671da177e4SLinus Torvalds 				return err;
48681da177e4SLinus Torvalds 		}
4869d14df339SDan Carpenter 		memset(&hdsp_version, 0, sizeof(hdsp_version));
48701da177e4SLinus Torvalds 		hdsp_version.io_type = hdsp->io_type;
48711da177e4SLinus Torvalds 		hdsp_version.firmware_rev = hdsp->firmware_rev;
48720f7c9565SDan Carpenter 		if (copy_to_user(argp, &hdsp_version, sizeof(hdsp_version)))
48731da177e4SLinus Torvalds 			return -EFAULT;
48741da177e4SLinus Torvalds 		break;
48751da177e4SLinus Torvalds 	}
48761da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4877112e3f5aSTakashi Iwai 		struct hdsp_firmware firmware;
48781da177e4SLinus Torvalds 		u32 __user *firmware_data;
48791da177e4SLinus Torvalds 		int err;
48801da177e4SLinus Torvalds 
48811da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
48821da177e4SLinus Torvalds 		/* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
48831da177e4SLinus Torvalds 		if (hdsp->io_type == Undefined) return -EINVAL;
48841da177e4SLinus Torvalds 
48851da177e4SLinus Torvalds 		if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
48861da177e4SLinus Torvalds 			return -EBUSY;
48871da177e4SLinus Torvalds 
4888a54ba0feSTakashi Iwai 		dev_info(hdsp->card->dev,
4889a54ba0feSTakashi Iwai 			 "initializing firmware upload\n");
4890112e3f5aSTakashi Iwai 		if (copy_from_user(&firmware, argp, sizeof(firmware)))
48911da177e4SLinus Torvalds 			return -EFAULT;
4892112e3f5aSTakashi Iwai 		firmware_data = (u32 __user *)firmware.firmware_data;
48931da177e4SLinus Torvalds 
4894b0b98119STakashi Iwai 		if (hdsp_check_for_iobox (hdsp))
48951da177e4SLinus Torvalds 			return -EIO;
48961da177e4SLinus Torvalds 
489790caaef6STakashi Iwai 		if (!hdsp->fw_uploaded) {
489890caaef6STakashi Iwai 			hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
489990caaef6STakashi Iwai 			if (!hdsp->fw_uploaded)
490090caaef6STakashi Iwai 				return -ENOMEM;
490190caaef6STakashi Iwai 		}
490290caaef6STakashi Iwai 
490390caaef6STakashi Iwai 		if (copy_from_user(hdsp->fw_uploaded, firmware_data,
490490caaef6STakashi Iwai 				   HDSP_FIRMWARE_SIZE)) {
490590caaef6STakashi Iwai 			vfree(hdsp->fw_uploaded);
490690caaef6STakashi Iwai 			hdsp->fw_uploaded = NULL;
49071da177e4SLinus Torvalds 			return -EFAULT;
490890caaef6STakashi Iwai 		}
49091da177e4SLinus Torvalds 
49101da177e4SLinus Torvalds 		hdsp->state |= HDSP_FirmwareCached;
49111da177e4SLinus Torvalds 
491266c8f759STakashi Iwai 		err = snd_hdsp_load_firmware_from_cache(hdsp);
491366c8f759STakashi Iwai 		if (err < 0)
49141da177e4SLinus Torvalds 			return err;
49151da177e4SLinus Torvalds 
49161da177e4SLinus Torvalds 		if (!(hdsp->state & HDSP_InitializationComplete)) {
491766c8f759STakashi Iwai 			err = snd_hdsp_enable_io(hdsp);
491866c8f759STakashi Iwai 			if (err < 0)
49191da177e4SLinus Torvalds 				return err;
49201da177e4SLinus Torvalds 
49211da177e4SLinus Torvalds 			snd_hdsp_initialize_channels(hdsp);
49221da177e4SLinus Torvalds 			snd_hdsp_initialize_midi_flush(hdsp);
49231da177e4SLinus Torvalds 
492466c8f759STakashi Iwai 			err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp);
492566c8f759STakashi Iwai 			if (err < 0) {
4926a54ba0feSTakashi Iwai 				dev_err(hdsp->card->dev,
4927a54ba0feSTakashi Iwai 					"error creating alsa devices\n");
49281da177e4SLinus Torvalds 				return err;
49291da177e4SLinus Torvalds 			}
49301da177e4SLinus Torvalds 		}
49311da177e4SLinus Torvalds 		break;
49321da177e4SLinus Torvalds 	}
49331da177e4SLinus Torvalds 	case SNDRV_HDSP_IOCTL_GET_MIXER: {
493455e957d8STakashi Iwai 		struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
49351da177e4SLinus Torvalds 		if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
49361da177e4SLinus Torvalds 			return -EFAULT;
49371da177e4SLinus Torvalds 		break;
49381da177e4SLinus Torvalds 	}
49391da177e4SLinus Torvalds 	default:
49401da177e4SLinus Torvalds 		return -EINVAL;
49411da177e4SLinus Torvalds 	}
49421da177e4SLinus Torvalds 	return 0;
49431da177e4SLinus Torvalds }
49441da177e4SLinus Torvalds 
49456769e988SJulia Lawall static const struct snd_pcm_ops snd_hdsp_playback_ops = {
49461da177e4SLinus Torvalds 	.open =		snd_hdsp_playback_open,
49471da177e4SLinus Torvalds 	.close =	snd_hdsp_playback_release,
49481da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
49491da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
49501da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
49511da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
49521da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
49530925e6b2STakashi Iwai 	.copy_user =	snd_hdsp_playback_copy,
49540925e6b2STakashi Iwai 	.copy_kernel =	snd_hdsp_playback_copy_kernel,
49550925e6b2STakashi Iwai 	.fill_silence =	snd_hdsp_hw_silence,
49561da177e4SLinus Torvalds };
49571da177e4SLinus Torvalds 
49586769e988SJulia Lawall static const struct snd_pcm_ops snd_hdsp_capture_ops = {
49591da177e4SLinus Torvalds 	.open =		snd_hdsp_capture_open,
49601da177e4SLinus Torvalds 	.close =	snd_hdsp_capture_release,
49611da177e4SLinus Torvalds 	.ioctl =	snd_hdsp_ioctl,
49621da177e4SLinus Torvalds 	.hw_params =	snd_hdsp_hw_params,
49631da177e4SLinus Torvalds 	.prepare =	snd_hdsp_prepare,
49641da177e4SLinus Torvalds 	.trigger =	snd_hdsp_trigger,
49651da177e4SLinus Torvalds 	.pointer =	snd_hdsp_hw_pointer,
49660925e6b2STakashi Iwai 	.copy_user =	snd_hdsp_capture_copy,
49670925e6b2STakashi Iwai 	.copy_kernel =	snd_hdsp_capture_copy_kernel,
49681da177e4SLinus Torvalds };
49691da177e4SLinus Torvalds 
497092eed66dSTakashi Iwai static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
49711da177e4SLinus Torvalds {
497255e957d8STakashi Iwai 	struct snd_hwdep *hw;
49731da177e4SLinus Torvalds 	int err;
49741da177e4SLinus Torvalds 
497566c8f759STakashi Iwai 	err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw);
497666c8f759STakashi Iwai 	if (err < 0)
49771da177e4SLinus Torvalds 		return err;
49781da177e4SLinus Torvalds 
49791da177e4SLinus Torvalds 	hdsp->hwdep = hw;
49801da177e4SLinus Torvalds 	hw->private_data = hdsp;
49811da177e4SLinus Torvalds 	strcpy(hw->name, "HDSP hwdep interface");
49821da177e4SLinus Torvalds 
49831da177e4SLinus Torvalds 	hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
498442eb9238SAndre Schramm 	hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
49851da177e4SLinus Torvalds 
49861da177e4SLinus Torvalds 	return 0;
49871da177e4SLinus Torvalds }
49881da177e4SLinus Torvalds 
498955e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
49901da177e4SLinus Torvalds {
499155e957d8STakashi Iwai 	struct snd_pcm *pcm;
49921da177e4SLinus Torvalds 	int err;
49931da177e4SLinus Torvalds 
499466c8f759STakashi Iwai 	err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm);
499566c8f759STakashi Iwai 	if (err < 0)
49961da177e4SLinus Torvalds 		return err;
49971da177e4SLinus Torvalds 
49981da177e4SLinus Torvalds 	hdsp->pcm = pcm;
49991da177e4SLinus Torvalds 	pcm->private_data = hdsp;
50001da177e4SLinus Torvalds 	strcpy(pcm->name, hdsp->card_name);
50011da177e4SLinus Torvalds 
50021da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
50031da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
50041da177e4SLinus Torvalds 
50051da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
50061da177e4SLinus Torvalds 
50071da177e4SLinus Torvalds 	return 0;
50081da177e4SLinus Torvalds }
50091da177e4SLinus Torvalds 
501055e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
50111da177e4SLinus Torvalds {
50121da177e4SLinus Torvalds         hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
50131da177e4SLinus Torvalds 	hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
50141da177e4SLinus Torvalds }
50151da177e4SLinus Torvalds 
501655e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp)
50171da177e4SLinus Torvalds {
50181da177e4SLinus Torvalds 	int i;
50191da177e4SLinus Torvalds 
50201da177e4SLinus Torvalds 	if (hdsp_fifo_wait (hdsp, 0, 100)) {
5021a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev,
5022a54ba0feSTakashi Iwai 			"enable_io fifo_wait failed\n");
50231da177e4SLinus Torvalds 		return -EIO;
50241da177e4SLinus Torvalds 	}
50251da177e4SLinus Torvalds 
50261da177e4SLinus Torvalds 	for (i = 0; i < hdsp->max_channels; ++i) {
50271da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
50281da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
50291da177e4SLinus Torvalds 	}
50301da177e4SLinus Torvalds 
50311da177e4SLinus Torvalds 	return 0;
50321da177e4SLinus Torvalds }
50331da177e4SLinus Torvalds 
503455e957d8STakashi Iwai static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
50351da177e4SLinus Torvalds {
5036da2a040eSJasmin Fazlic 	int status, aebi_channels, aebo_channels, i;
50371da177e4SLinus Torvalds 
50381da177e4SLinus Torvalds 	switch (hdsp->io_type) {
50391da177e4SLinus Torvalds 	case Digiface:
50401da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Digiface";
50411da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
50421da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
50431da177e4SLinus Torvalds 		break;
50441da177e4SLinus Torvalds 
50451da177e4SLinus Torvalds 	case H9652:
50461da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9652";
50471da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
50481da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
50491da177e4SLinus Torvalds 		break;
50501da177e4SLinus Torvalds 
50511da177e4SLinus Torvalds 	case H9632:
50521da177e4SLinus Torvalds 		status = hdsp_read(hdsp, HDSP_statusRegister);
50531da177e4SLinus Torvalds 		/* HDSP_AEBx bits are low when AEB are connected */
50541da177e4SLinus Torvalds 		aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
50551da177e4SLinus Torvalds 		aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
50561da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall HDSP 9632";
50571da177e4SLinus Torvalds 		hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
50581da177e4SLinus Torvalds 		hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
50591da177e4SLinus Torvalds 		hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
50601da177e4SLinus Torvalds 		hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
50611da177e4SLinus Torvalds 		hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
50621da177e4SLinus Torvalds 		hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
5063da2a040eSJasmin Fazlic 		/* Disable loopback of output channels, as the set function
5064da2a040eSJasmin Fazlic 		 * only sets on a change we fake all bits (channels) as enabled.
5065da2a040eSJasmin Fazlic 		 */
5066da2a040eSJasmin Fazlic 		hdsp->io_loopback = 0xffffffff;
5067da2a040eSJasmin Fazlic 		for (i = 0; i < hdsp->max_channels; ++i)
5068da2a040eSJasmin Fazlic 			hdsp_loopback_set(hdsp, i, false);
50691da177e4SLinus Torvalds 		break;
50701da177e4SLinus Torvalds 
50711da177e4SLinus Torvalds 	case Multiface:
50721da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP + Multiface";
50731da177e4SLinus Torvalds 		hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
50741da177e4SLinus Torvalds 		hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
50751da177e4SLinus Torvalds 		break;
50761da177e4SLinus Torvalds 
507728b26e15SFlorian Faber 	case RPM:
507828b26e15SFlorian Faber 		hdsp->card_name = "RME Hammerfall DSP + RPM";
507928b26e15SFlorian Faber 		hdsp->ss_in_channels = RPM_CHANNELS-1;
508028b26e15SFlorian Faber 		hdsp->ss_out_channels = RPM_CHANNELS;
508128b26e15SFlorian Faber 		hdsp->ds_in_channels = RPM_CHANNELS-1;
508228b26e15SFlorian Faber 		hdsp->ds_out_channels = RPM_CHANNELS;
508328b26e15SFlorian Faber 		break;
508428b26e15SFlorian Faber 
50851da177e4SLinus Torvalds 	default:
50861da177e4SLinus Torvalds  		/* should never get here */
50871da177e4SLinus Torvalds 		break;
50881da177e4SLinus Torvalds 	}
50891da177e4SLinus Torvalds }
50901da177e4SLinus Torvalds 
509155e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
50921da177e4SLinus Torvalds {
50931da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 0);
50941da177e4SLinus Torvalds 	snd_hdsp_flush_midi_input (hdsp, 1);
50951da177e4SLinus Torvalds }
50961da177e4SLinus Torvalds 
509755e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
50981da177e4SLinus Torvalds {
50991da177e4SLinus Torvalds 	int err;
51001da177e4SLinus Torvalds 
510166c8f759STakashi Iwai 	err = snd_hdsp_create_pcm(card, hdsp);
510266c8f759STakashi Iwai 	if (err < 0) {
5103a54ba0feSTakashi Iwai 		dev_err(card->dev,
5104a54ba0feSTakashi Iwai 			"Error creating pcm interface\n");
51051da177e4SLinus Torvalds 		return err;
51061da177e4SLinus Torvalds 	}
51071da177e4SLinus Torvalds 
51081da177e4SLinus Torvalds 
510966c8f759STakashi Iwai 	err = snd_hdsp_create_midi(card, hdsp, 0);
511066c8f759STakashi Iwai 	if (err < 0) {
5111a54ba0feSTakashi Iwai 		dev_err(card->dev,
5112a54ba0feSTakashi Iwai 			"Error creating first midi interface\n");
51131da177e4SLinus Torvalds 		return err;
51141da177e4SLinus Torvalds 	}
51151da177e4SLinus Torvalds 
51161da177e4SLinus Torvalds 	if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
511766c8f759STakashi Iwai 		err = snd_hdsp_create_midi(card, hdsp, 1);
511866c8f759STakashi Iwai 		if (err < 0) {
5119a54ba0feSTakashi Iwai 			dev_err(card->dev,
5120a54ba0feSTakashi Iwai 				"Error creating second midi interface\n");
51211da177e4SLinus Torvalds 			return err;
51221da177e4SLinus Torvalds 		}
51231da177e4SLinus Torvalds 	}
51241da177e4SLinus Torvalds 
512566c8f759STakashi Iwai 	err = snd_hdsp_create_controls(card, hdsp);
512666c8f759STakashi Iwai 	if (err < 0) {
5127a54ba0feSTakashi Iwai 		dev_err(card->dev,
5128a54ba0feSTakashi Iwai 			"Error creating ctl interface\n");
51291da177e4SLinus Torvalds 		return err;
51301da177e4SLinus Torvalds 	}
51311da177e4SLinus Torvalds 
51321da177e4SLinus Torvalds 	snd_hdsp_proc_init(hdsp);
51331da177e4SLinus Torvalds 
51341da177e4SLinus Torvalds 	hdsp->system_sample_rate = -1;
51351da177e4SLinus Torvalds 	hdsp->playback_pid = -1;
51361da177e4SLinus Torvalds 	hdsp->capture_pid = -1;
51371da177e4SLinus Torvalds 	hdsp->capture_substream = NULL;
51381da177e4SLinus Torvalds 	hdsp->playback_substream = NULL;
51391da177e4SLinus Torvalds 
514066c8f759STakashi Iwai 	err = snd_hdsp_set_defaults(hdsp);
514166c8f759STakashi Iwai 	if (err < 0) {
5142a54ba0feSTakashi Iwai 		dev_err(card->dev,
5143a54ba0feSTakashi Iwai 			"Error setting default values\n");
51441da177e4SLinus Torvalds 		return err;
51451da177e4SLinus Torvalds 	}
51461da177e4SLinus Torvalds 
51471da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
5148b73c1c12SClemens Ladisch 		strcpy(card->shortname, "Hammerfall DSP");
51491da177e4SLinus Torvalds 		sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
51501da177e4SLinus Torvalds 			hdsp->port, hdsp->irq);
51511da177e4SLinus Torvalds 
515266c8f759STakashi Iwai 		err = snd_card_register(card);
515366c8f759STakashi Iwai 		if (err < 0) {
5154a54ba0feSTakashi Iwai 			dev_err(card->dev,
5155a54ba0feSTakashi Iwai 				"error registering card\n");
51561da177e4SLinus Torvalds 			return err;
51571da177e4SLinus Torvalds 		}
51581da177e4SLinus Torvalds 		hdsp->state |= HDSP_InitializationComplete;
51591da177e4SLinus Torvalds 	}
51601da177e4SLinus Torvalds 
51611da177e4SLinus Torvalds 	return 0;
51621da177e4SLinus Torvalds }
51631da177e4SLinus Torvalds 
51641da177e4SLinus Torvalds /* load firmware via hotplug fw loader */
516592eed66dSTakashi Iwai static int hdsp_request_fw_loader(struct hdsp *hdsp)
51661da177e4SLinus Torvalds {
51671da177e4SLinus Torvalds 	const char *fwfile;
51681da177e4SLinus Torvalds 	const struct firmware *fw;
51691da177e4SLinus Torvalds 	int err;
51701da177e4SLinus Torvalds 
51711da177e4SLinus Torvalds 	if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
51721da177e4SLinus Torvalds 		return 0;
51731da177e4SLinus Torvalds 	if (hdsp->io_type == Undefined) {
517466c8f759STakashi Iwai 		err = hdsp_get_iobox_version(hdsp);
517566c8f759STakashi Iwai 		if (err < 0)
51761da177e4SLinus Torvalds 			return err;
51771da177e4SLinus Torvalds 		if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
51781da177e4SLinus Torvalds 			return 0;
51791da177e4SLinus Torvalds 	}
51801da177e4SLinus Torvalds 
51811da177e4SLinus Torvalds 	/* caution: max length of firmware filename is 30! */
51821da177e4SLinus Torvalds 	switch (hdsp->io_type) {
518328b26e15SFlorian Faber 	case RPM:
518428b26e15SFlorian Faber 		fwfile = "rpm_firmware.bin";
518528b26e15SFlorian Faber 		break;
51861da177e4SLinus Torvalds 	case Multiface:
51871da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
51881da177e4SLinus Torvalds 			fwfile = "multiface_firmware.bin";
51891da177e4SLinus Torvalds 		else
51901da177e4SLinus Torvalds 			fwfile = "multiface_firmware_rev11.bin";
51911da177e4SLinus Torvalds 		break;
51921da177e4SLinus Torvalds 	case Digiface:
51931da177e4SLinus Torvalds 		if (hdsp->firmware_rev == 0xa)
51941da177e4SLinus Torvalds 			fwfile = "digiface_firmware.bin";
51951da177e4SLinus Torvalds 		else
51961da177e4SLinus Torvalds 			fwfile = "digiface_firmware_rev11.bin";
51971da177e4SLinus Torvalds 		break;
51981da177e4SLinus Torvalds 	default:
5199a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev,
5200a54ba0feSTakashi Iwai 			"invalid io_type %d\n", hdsp->io_type);
52011da177e4SLinus Torvalds 		return -EINVAL;
52021da177e4SLinus Torvalds 	}
52031da177e4SLinus Torvalds 
52041da177e4SLinus Torvalds 	if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5205a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev,
5206a54ba0feSTakashi Iwai 			"cannot load firmware %s\n", fwfile);
52071da177e4SLinus Torvalds 		return -ENOENT;
52081da177e4SLinus Torvalds 	}
520990caaef6STakashi Iwai 	if (fw->size < HDSP_FIRMWARE_SIZE) {
5210a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev,
5211a54ba0feSTakashi Iwai 			"too short firmware size %d (expected %d)\n",
521290caaef6STakashi Iwai 			   (int)fw->size, HDSP_FIRMWARE_SIZE);
521329d28c50SSudip Mukherjee 		release_firmware(fw);
52141da177e4SLinus Torvalds 		return -EINVAL;
52151da177e4SLinus Torvalds 	}
52167679a030SThomas Charbonnel 
521790caaef6STakashi Iwai 	hdsp->firmware = fw;
52181da177e4SLinus Torvalds 
52191da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareCached;
52201da177e4SLinus Torvalds 
522166c8f759STakashi Iwai 	err = snd_hdsp_load_firmware_from_cache(hdsp);
522266c8f759STakashi Iwai 	if (err < 0)
52231da177e4SLinus Torvalds 		return err;
52241da177e4SLinus Torvalds 
52251da177e4SLinus Torvalds 	if (!(hdsp->state & HDSP_InitializationComplete)) {
522666c8f759STakashi Iwai 		err = snd_hdsp_enable_io(hdsp);
522766c8f759STakashi Iwai 		if (err < 0)
52281da177e4SLinus Torvalds 			return err;
52291da177e4SLinus Torvalds 
523066c8f759STakashi Iwai 		err = snd_hdsp_create_hwdep(hdsp->card, hdsp);
523166c8f759STakashi Iwai 		if (err < 0) {
5232a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
5233a54ba0feSTakashi Iwai 				"error creating hwdep device\n");
52341da177e4SLinus Torvalds 			return err;
52351da177e4SLinus Torvalds 		}
52361da177e4SLinus Torvalds 		snd_hdsp_initialize_channels(hdsp);
52371da177e4SLinus Torvalds 		snd_hdsp_initialize_midi_flush(hdsp);
523866c8f759STakashi Iwai 		err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp);
523966c8f759STakashi Iwai 		if (err < 0) {
5240a54ba0feSTakashi Iwai 			dev_err(hdsp->card->dev,
5241a54ba0feSTakashi Iwai 				"error creating alsa devices\n");
52421da177e4SLinus Torvalds 			return err;
52431da177e4SLinus Torvalds 		}
52441da177e4SLinus Torvalds 	}
52451da177e4SLinus Torvalds 	return 0;
52461da177e4SLinus Torvalds }
52471da177e4SLinus Torvalds 
5248e23e7a14SBill Pemberton static int snd_hdsp_create(struct snd_card *card,
524955e957d8STakashi Iwai 			   struct hdsp *hdsp)
52501da177e4SLinus Torvalds {
52511da177e4SLinus Torvalds 	struct pci_dev *pci = hdsp->pci;
52521da177e4SLinus Torvalds 	int err;
52531da177e4SLinus Torvalds 	int is_9652 = 0;
52541da177e4SLinus Torvalds 	int is_9632 = 0;
52551da177e4SLinus Torvalds 
52561da177e4SLinus Torvalds 	hdsp->irq = -1;
52571da177e4SLinus Torvalds 	hdsp->state = 0;
52581da177e4SLinus Torvalds 	hdsp->midi[0].rmidi = NULL;
52591da177e4SLinus Torvalds 	hdsp->midi[1].rmidi = NULL;
52601da177e4SLinus Torvalds 	hdsp->midi[0].input = NULL;
52611da177e4SLinus Torvalds 	hdsp->midi[1].input = NULL;
52621da177e4SLinus Torvalds 	hdsp->midi[0].output = NULL;
52631da177e4SLinus Torvalds 	hdsp->midi[1].output = NULL;
52641da177e4SLinus Torvalds 	hdsp->midi[0].pending = 0;
52651da177e4SLinus Torvalds 	hdsp->midi[1].pending = 0;
52661da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[0].lock);
52671da177e4SLinus Torvalds 	spin_lock_init(&hdsp->midi[1].lock);
52681da177e4SLinus Torvalds 	hdsp->iobase = NULL;
52691da177e4SLinus Torvalds 	hdsp->control_register = 0;
52701da177e4SLinus Torvalds 	hdsp->control2_register = 0;
52711da177e4SLinus Torvalds 	hdsp->io_type = Undefined;
52721da177e4SLinus Torvalds 	hdsp->max_channels = 26;
52731da177e4SLinus Torvalds 
52741da177e4SLinus Torvalds 	hdsp->card = card;
52751da177e4SLinus Torvalds 
52761da177e4SLinus Torvalds 	spin_lock_init(&hdsp->lock);
52771da177e4SLinus Torvalds 
52784adab848STakashi Iwai 	INIT_WORK(&hdsp->midi_work, hdsp_midi_work);
52791da177e4SLinus Torvalds 
52801da177e4SLinus Torvalds 	pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
52811da177e4SLinus Torvalds 	hdsp->firmware_rev &= 0xff;
52821da177e4SLinus Torvalds 
52831da177e4SLinus Torvalds 	/* From Martin Bjoernsen :
52841da177e4SLinus Torvalds 	    "It is important that the card's latency timer register in
52851da177e4SLinus Torvalds 	    the PCI configuration space is set to a value much larger
52861da177e4SLinus Torvalds 	    than 0 by the computer's BIOS or the driver.
52871da177e4SLinus Torvalds 	    The windows driver always sets this 8 bit register [...]
52881da177e4SLinus Torvalds 	    to its maximum 255 to avoid problems with some computers."
52891da177e4SLinus Torvalds 	*/
52901da177e4SLinus Torvalds 	pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
52911da177e4SLinus Torvalds 
52921da177e4SLinus Torvalds 	strcpy(card->driver, "H-DSP");
52931da177e4SLinus Torvalds 	strcpy(card->mixername, "Xilinx FPGA");
52941da177e4SLinus Torvalds 
5295b0b98119STakashi Iwai 	if (hdsp->firmware_rev < 0xa)
52961da177e4SLinus Torvalds 		return -ENODEV;
5297b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x64)
52981da177e4SLinus Torvalds 		hdsp->card_name = "RME Hammerfall DSP";
5299b0b98119STakashi Iwai 	else if (hdsp->firmware_rev < 0x96) {
53001da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9652";
53011da177e4SLinus Torvalds 		is_9652 = 1;
53021da177e4SLinus Torvalds 	} else {
53031da177e4SLinus Torvalds 		hdsp->card_name = "RME HDSP 9632";
53041da177e4SLinus Torvalds 		hdsp->max_channels = 16;
53051da177e4SLinus Torvalds 		is_9632 = 1;
53061da177e4SLinus Torvalds 	}
53071da177e4SLinus Torvalds 
5308d136b8e5STakashi Iwai 	err = pcim_enable_device(pci);
530966c8f759STakashi Iwai 	if (err < 0)
53101da177e4SLinus Torvalds 		return err;
53111da177e4SLinus Torvalds 
53121da177e4SLinus Torvalds 	pci_set_master(hdsp->pci);
53131da177e4SLinus Torvalds 
531466c8f759STakashi Iwai 	err = pci_request_regions(pci, "hdsp");
531566c8f759STakashi Iwai 	if (err < 0)
53161da177e4SLinus Torvalds 		return err;
53171da177e4SLinus Torvalds 	hdsp->port = pci_resource_start(pci, 0);
5318d136b8e5STakashi Iwai 	hdsp->iobase = devm_ioremap(&pci->dev, hdsp->port, HDSP_IO_EXTENT);
531966c8f759STakashi Iwai 	if (!hdsp->iobase) {
5320a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
5321a54ba0feSTakashi Iwai 			hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
53221da177e4SLinus Torvalds 		return -EBUSY;
53231da177e4SLinus Torvalds 	}
53241da177e4SLinus Torvalds 
5325d136b8e5STakashi Iwai 	if (devm_request_irq(&pci->dev, pci->irq, snd_hdsp_interrupt,
5326d136b8e5STakashi Iwai 			     IRQF_SHARED, KBUILD_MODNAME, hdsp)) {
5327a54ba0feSTakashi Iwai 		dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
53281da177e4SLinus Torvalds 		return -EBUSY;
53291da177e4SLinus Torvalds 	}
53301da177e4SLinus Torvalds 
53311da177e4SLinus Torvalds 	hdsp->irq = pci->irq;
533239cccf45STakashi Iwai 	card->sync_irq = hdsp->irq;
5333176546abSRemy Bruno 	hdsp->precise_ptr = 0;
53344adab848STakashi Iwai 	hdsp->use_midi_work = 1;
5335d7923b2aSRemy Bruno 	hdsp->dds_value = 0;
53361da177e4SLinus Torvalds 
533766c8f759STakashi Iwai 	err = snd_hdsp_initialize_memory(hdsp);
533866c8f759STakashi Iwai 	if (err < 0)
53391da177e4SLinus Torvalds 		return err;
53401da177e4SLinus Torvalds 
53411da177e4SLinus Torvalds 	if (!is_9652 && !is_9632) {
5342e588ed83STim Blechmann 		/* we wait a maximum of 10 seconds to let freshly
5343e588ed83STim Blechmann 		 * inserted cardbus cards do their hardware init */
5344e588ed83STim Blechmann 		err = hdsp_wait_for_iobox(hdsp, 1000, 10);
53451da177e4SLinus Torvalds 
534600c9ddd1STim Blechmann 		if (err < 0)
534700c9ddd1STim Blechmann 			return err;
534800c9ddd1STim Blechmann 
53491da177e4SLinus Torvalds 		if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
535066c8f759STakashi Iwai 			err = hdsp_request_fw_loader(hdsp);
535166c8f759STakashi Iwai 			if (err < 0)
53521da177e4SLinus Torvalds 				/* we don't fail as this can happen
53531da177e4SLinus Torvalds 				   if userspace is not ready for
53541da177e4SLinus Torvalds 				   firmware upload
53551da177e4SLinus Torvalds 				*/
5356a54ba0feSTakashi Iwai 				dev_err(hdsp->card->dev,
5357a54ba0feSTakashi Iwai 					"couldn't get firmware from userspace. try using hdsploader\n");
5358b0b98119STakashi Iwai 			else
53591da177e4SLinus Torvalds 				/* init is complete, we return */
53601da177e4SLinus Torvalds 				return 0;
536100c9ddd1STim Blechmann 			/* we defer initialization */
5362a54ba0feSTakashi Iwai 			dev_info(hdsp->card->dev,
5363a54ba0feSTakashi Iwai 				 "card initialization pending : waiting for firmware\n");
536466c8f759STakashi Iwai 			err = snd_hdsp_create_hwdep(card, hdsp);
536566c8f759STakashi Iwai 			if (err < 0)
53661da177e4SLinus Torvalds 				return err;
53671da177e4SLinus Torvalds 			return 0;
53681da177e4SLinus Torvalds 		} else {
5369a54ba0feSTakashi Iwai 			dev_info(hdsp->card->dev,
5370a54ba0feSTakashi Iwai 				 "Firmware already present, initializing card.\n");
537128b26e15SFlorian Faber 			if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
537228b26e15SFlorian Faber 				hdsp->io_type = RPM;
537328b26e15SFlorian Faber 			else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
53741da177e4SLinus Torvalds 				hdsp->io_type = Multiface;
5375b0b98119STakashi Iwai 			else
53761da177e4SLinus Torvalds 				hdsp->io_type = Digiface;
53771da177e4SLinus Torvalds 		}
53781da177e4SLinus Torvalds 	}
53791da177e4SLinus Torvalds 
538066c8f759STakashi Iwai 	err = snd_hdsp_enable_io(hdsp);
538166c8f759STakashi Iwai 	if (err)
53821da177e4SLinus Torvalds 		return err;
53831da177e4SLinus Torvalds 
5384b0b98119STakashi Iwai 	if (is_9652)
53851da177e4SLinus Torvalds 	        hdsp->io_type = H9652;
53861da177e4SLinus Torvalds 
5387b0b98119STakashi Iwai 	if (is_9632)
53881da177e4SLinus Torvalds 		hdsp->io_type = H9632;
53891da177e4SLinus Torvalds 
539066c8f759STakashi Iwai 	err = snd_hdsp_create_hwdep(card, hdsp);
539166c8f759STakashi Iwai 	if (err < 0)
53921da177e4SLinus Torvalds 		return err;
53931da177e4SLinus Torvalds 
53941da177e4SLinus Torvalds 	snd_hdsp_initialize_channels(hdsp);
53951da177e4SLinus Torvalds 	snd_hdsp_initialize_midi_flush(hdsp);
53961da177e4SLinus Torvalds 
53971da177e4SLinus Torvalds 	hdsp->state |= HDSP_FirmwareLoaded;
53981da177e4SLinus Torvalds 
539966c8f759STakashi Iwai 	err = snd_hdsp_create_alsa_devices(card, hdsp);
540066c8f759STakashi Iwai 	if (err < 0)
54011da177e4SLinus Torvalds 		return err;
54021da177e4SLinus Torvalds 
54031da177e4SLinus Torvalds 	return 0;
54041da177e4SLinus Torvalds }
54051da177e4SLinus Torvalds 
5406d136b8e5STakashi Iwai static void snd_hdsp_card_free(struct snd_card *card)
54071da177e4SLinus Torvalds {
5408d136b8e5STakashi Iwai 	struct hdsp *hdsp = card->private_data;
5409d136b8e5STakashi Iwai 
54101da177e4SLinus Torvalds 	if (hdsp->port) {
54111da177e4SLinus Torvalds 		/* stop the audio, and cancel all interrupts */
54124adab848STakashi Iwai 		cancel_work_sync(&hdsp->midi_work);
54131da177e4SLinus Torvalds 		hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
54141da177e4SLinus Torvalds 		hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
54151da177e4SLinus Torvalds 	}
54161da177e4SLinus Torvalds 
541790caaef6STakashi Iwai 	release_firmware(hdsp->firmware);
541890caaef6STakashi Iwai 	vfree(hdsp->fw_uploaded);
54191da177e4SLinus Torvalds }
54201da177e4SLinus Torvalds 
5421e23e7a14SBill Pemberton static int snd_hdsp_probe(struct pci_dev *pci,
54221da177e4SLinus Torvalds 			  const struct pci_device_id *pci_id)
54231da177e4SLinus Torvalds {
54241da177e4SLinus Torvalds 	static int dev;
542555e957d8STakashi Iwai 	struct hdsp *hdsp;
542655e957d8STakashi Iwai 	struct snd_card *card;
54271da177e4SLinus Torvalds 	int err;
54281da177e4SLinus Torvalds 
54291da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
54301da177e4SLinus Torvalds 		return -ENODEV;
54311da177e4SLinus Torvalds 	if (!enable[dev]) {
54321da177e4SLinus Torvalds 		dev++;
54331da177e4SLinus Torvalds 		return -ENOENT;
54341da177e4SLinus Torvalds 	}
54351da177e4SLinus Torvalds 
5436d136b8e5STakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5437e58de7baSTakashi Iwai 				sizeof(struct hdsp), &card);
5438e58de7baSTakashi Iwai 	if (err < 0)
5439e58de7baSTakashi Iwai 		return err;
54401da177e4SLinus Torvalds 
54419fe856e4SJoe Perches 	hdsp = card->private_data;
54421da177e4SLinus Torvalds 	card->private_free = snd_hdsp_card_free;
54431da177e4SLinus Torvalds 	hdsp->dev = dev;
54441da177e4SLinus Torvalds 	hdsp->pci = pci;
54452675be5aSMarkus Elfring 	err = snd_hdsp_create(card, hdsp);
54462675be5aSMarkus Elfring 	if (err)
5447e2263f0bSTakashi Iwai 		goto error;
54481da177e4SLinus Torvalds 
54491da177e4SLinus Torvalds 	strcpy(card->shortname, "Hammerfall DSP");
54501da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
54511da177e4SLinus Torvalds 		hdsp->port, hdsp->irq);
54522675be5aSMarkus Elfring 	err = snd_card_register(card);
5453d136b8e5STakashi Iwai 	if (err)
5454e2263f0bSTakashi Iwai 		goto error;
54551da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
54561da177e4SLinus Torvalds 	dev++;
54571da177e4SLinus Torvalds 	return 0;
5458e2263f0bSTakashi Iwai 
5459e2263f0bSTakashi Iwai  error:
5460e2263f0bSTakashi Iwai 	snd_card_free(card);
5461e2263f0bSTakashi Iwai 	return err;
54621da177e4SLinus Torvalds }
54631da177e4SLinus Torvalds 
5464e9f66d9bSTakashi Iwai static struct pci_driver hdsp_driver = {
54653733e424STakashi Iwai 	.name =     KBUILD_MODNAME,
54661da177e4SLinus Torvalds 	.id_table = snd_hdsp_ids,
54671da177e4SLinus Torvalds 	.probe =    snd_hdsp_probe,
54681da177e4SLinus Torvalds };
54691da177e4SLinus Torvalds 
5470e9f66d9bSTakashi Iwai module_pci_driver(hdsp_driver);
5471