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;
43650895a55SJason 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;
48350895a55SJason 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
50550895a55SJason 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
52050895a55SJason 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
52950895a55SJason 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
54350895a55SJason 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
55750895a55SJason 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 *
snd_hammerfall_get_buffer(struct pci_dev * pci,size_t size)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
hdsp_playback_to_output_key(struct hdsp * hdsp,int in,int out)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
hdsp_input_to_output_key(struct hdsp * hdsp,int in,int out)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
hdsp_write(struct hdsp * hdsp,int reg,int val)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
hdsp_read(struct hdsp * hdsp,int reg)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
hdsp_check_for_iobox(struct hdsp * hdsp)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
hdsp_wait_for_iobox(struct hdsp * hdsp,unsigned int loops,unsigned int delay)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
snd_hdsp_load_firmware_from_cache(struct hdsp * hdsp)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
hdsp_get_iobox_version(struct hdsp * hdsp)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
hdsp_check_for_firmware(struct hdsp * hdsp,int load_on_demand)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
hdsp_fifo_wait(struct hdsp * hdsp,int count,int timeout)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
hdsp_read_gain(struct hdsp * hdsp,unsigned int addr)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
hdsp_write_gain(struct hdsp * hdsp,unsigned int addr,unsigned short data)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
snd_hdsp_use_is_exclusive(struct hdsp * hdsp)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
hdsp_spdif_sample_rate(struct hdsp * hdsp)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
hdsp_external_sample_rate(struct hdsp * hdsp)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
hdsp_compute_period_size(struct hdsp * hdsp)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
hdsp_hw_pointer(struct hdsp * hdsp)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
hdsp_reset_hw_pointer(struct hdsp * hdsp)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
hdsp_start_audio(struct hdsp * s)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
hdsp_stop_audio(struct hdsp * s)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
hdsp_silence_playback(struct hdsp * hdsp)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
hdsp_set_interrupt_interval(struct hdsp * s,unsigned int frames)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
hdsp_set_dds_value(struct hdsp * hdsp,int rate)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
hdsp_set_rate(struct hdsp * hdsp,int rate,int called_internally)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
snd_hdsp_midi_read_byte(struct hdsp * hdsp,int id)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
snd_hdsp_midi_write_byte(struct hdsp * hdsp,int id,int val)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
snd_hdsp_midi_input_available(struct hdsp * hdsp,int id)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
snd_hdsp_midi_output_possible(struct hdsp * hdsp,int id)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
snd_hdsp_flush_midi_input(struct hdsp * hdsp,int id)129955e957d8STakashi Iwai static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
13001da177e4SLinus Torvalds {
1301*6cc4e5eaSTakashi Iwai int count = 256;
1302*6cc4e5eaSTakashi Iwai
1303*6cc4e5eaSTakashi Iwai while (snd_hdsp_midi_input_available(hdsp, id) && --count)
13041da177e4SLinus Torvalds snd_hdsp_midi_read_byte(hdsp, id);
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds
snd_hdsp_midi_output_write(struct hdsp_midi * hmidi)130755e957d8STakashi Iwai static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds unsigned long flags;
13101da177e4SLinus Torvalds int n_pending;
13111da177e4SLinus Torvalds int to_write;
13121da177e4SLinus Torvalds int i;
13131da177e4SLinus Torvalds unsigned char buf[128];
13141da177e4SLinus Torvalds
13151da177e4SLinus Torvalds /* Output is not interrupt driven */
13161da177e4SLinus Torvalds
13171da177e4SLinus Torvalds spin_lock_irqsave (&hmidi->lock, flags);
13181da177e4SLinus Torvalds if (hmidi->output) {
13191da177e4SLinus Torvalds if (!snd_rawmidi_transmit_empty (hmidi->output)) {
132066c8f759STakashi Iwai n_pending = snd_hdsp_midi_output_possible(hmidi->hdsp, hmidi->id);
132166c8f759STakashi Iwai if (n_pending > 0) {
13221da177e4SLinus Torvalds if (n_pending > (int)sizeof (buf))
13231da177e4SLinus Torvalds n_pending = sizeof (buf);
13241da177e4SLinus Torvalds
132566c8f759STakashi Iwai to_write = snd_rawmidi_transmit(hmidi->output, buf, n_pending);
132666c8f759STakashi Iwai if (to_write > 0) {
13271da177e4SLinus Torvalds for (i = 0; i < to_write; ++i)
13281da177e4SLinus Torvalds snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds spin_unlock_irqrestore (&hmidi->lock, flags);
13341da177e4SLinus Torvalds return 0;
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds
snd_hdsp_midi_input_read(struct hdsp_midi * hmidi)133755e957d8STakashi Iwai static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
13401da177e4SLinus Torvalds unsigned long flags;
13411da177e4SLinus Torvalds int n_pending;
13421da177e4SLinus Torvalds int i;
13431da177e4SLinus Torvalds
13441da177e4SLinus Torvalds spin_lock_irqsave (&hmidi->lock, flags);
134566c8f759STakashi Iwai n_pending = snd_hdsp_midi_input_available(hmidi->hdsp, hmidi->id);
134666c8f759STakashi Iwai if (n_pending > 0) {
13471da177e4SLinus Torvalds if (hmidi->input) {
1348b0b98119STakashi Iwai if (n_pending > (int)sizeof (buf))
13491da177e4SLinus Torvalds n_pending = sizeof (buf);
1350b0b98119STakashi Iwai for (i = 0; i < n_pending; ++i)
13511da177e4SLinus Torvalds buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
1352b0b98119STakashi Iwai if (n_pending)
13531da177e4SLinus Torvalds snd_rawmidi_receive (hmidi->input, buf, n_pending);
13541da177e4SLinus Torvalds } else {
13551da177e4SLinus Torvalds /* flush the MIDI input FIFO */
1356b0b98119STakashi Iwai while (--n_pending)
13571da177e4SLinus Torvalds snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds }
13601da177e4SLinus Torvalds hmidi->pending = 0;
1361b0b98119STakashi Iwai if (hmidi->id)
13621da177e4SLinus Torvalds hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
1363b0b98119STakashi Iwai else
13641da177e4SLinus Torvalds hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
13651da177e4SLinus Torvalds hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
13661da177e4SLinus Torvalds spin_unlock_irqrestore (&hmidi->lock, flags);
13671da177e4SLinus Torvalds return snd_hdsp_midi_output_write (hmidi);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds
snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream * substream,int up)137055e957d8STakashi Iwai static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
13711da177e4SLinus Torvalds {
137255e957d8STakashi Iwai struct hdsp *hdsp;
137355e957d8STakashi Iwai struct hdsp_midi *hmidi;
13741da177e4SLinus Torvalds unsigned long flags;
13751da177e4SLinus Torvalds u32 ie;
13761da177e4SLinus Torvalds
137755e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
13781da177e4SLinus Torvalds hdsp = hmidi->hdsp;
13791da177e4SLinus Torvalds ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
13801da177e4SLinus Torvalds spin_lock_irqsave (&hdsp->lock, flags);
13811da177e4SLinus Torvalds if (up) {
13821da177e4SLinus Torvalds if (!(hdsp->control_register & ie)) {
13831da177e4SLinus Torvalds snd_hdsp_flush_midi_input (hdsp, hmidi->id);
13841da177e4SLinus Torvalds hdsp->control_register |= ie;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds } else {
13871da177e4SLinus Torvalds hdsp->control_register &= ~ie;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds
13901da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
13911da177e4SLinus Torvalds spin_unlock_irqrestore (&hdsp->lock, flags);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds
snd_hdsp_midi_output_timer(struct timer_list * t)13947211ec63SKees Cook static void snd_hdsp_midi_output_timer(struct timer_list *t)
13951da177e4SLinus Torvalds {
13967211ec63SKees Cook struct hdsp_midi *hmidi = from_timer(hmidi, t, timer);
13971da177e4SLinus Torvalds unsigned long flags;
13981da177e4SLinus Torvalds
13991da177e4SLinus Torvalds snd_hdsp_midi_output_write(hmidi);
14001da177e4SLinus Torvalds spin_lock_irqsave (&hmidi->lock, flags);
14011da177e4SLinus Torvalds
14021da177e4SLinus Torvalds /* this does not bump hmidi->istimer, because the
14031da177e4SLinus Torvalds kernel automatically removed the timer when it
14041da177e4SLinus Torvalds expired, and we are now adding it back, thus
14051da177e4SLinus Torvalds leaving istimer wherever it was set before.
14061da177e4SLinus Torvalds */
14071da177e4SLinus Torvalds
1408c41c009eSTakashi Iwai if (hmidi->istimer)
1409c41c009eSTakashi Iwai mod_timer(&hmidi->timer, 1 + jiffies);
14101da177e4SLinus Torvalds
14111da177e4SLinus Torvalds spin_unlock_irqrestore (&hmidi->lock, flags);
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds
snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream * substream,int up)141455e957d8STakashi Iwai static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
14151da177e4SLinus Torvalds {
141655e957d8STakashi Iwai struct hdsp_midi *hmidi;
14171da177e4SLinus Torvalds unsigned long flags;
14181da177e4SLinus Torvalds
141955e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14201da177e4SLinus Torvalds spin_lock_irqsave (&hmidi->lock, flags);
14211da177e4SLinus Torvalds if (up) {
14221da177e4SLinus Torvalds if (!hmidi->istimer) {
14237211ec63SKees Cook timer_setup(&hmidi->timer, snd_hdsp_midi_output_timer,
14247211ec63SKees Cook 0);
1425c41c009eSTakashi Iwai mod_timer(&hmidi->timer, 1 + jiffies);
14261da177e4SLinus Torvalds hmidi->istimer++;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds } else {
1429b0b98119STakashi Iwai if (hmidi->istimer && --hmidi->istimer <= 0)
14301da177e4SLinus Torvalds del_timer (&hmidi->timer);
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds spin_unlock_irqrestore (&hmidi->lock, flags);
14331da177e4SLinus Torvalds if (up)
14341da177e4SLinus Torvalds snd_hdsp_midi_output_write(hmidi);
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds
snd_hdsp_midi_input_open(struct snd_rawmidi_substream * substream)143755e957d8STakashi Iwai static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
14381da177e4SLinus Torvalds {
143955e957d8STakashi Iwai struct hdsp_midi *hmidi;
14401da177e4SLinus Torvalds
144155e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14421da177e4SLinus Torvalds spin_lock_irq (&hmidi->lock);
14431da177e4SLinus Torvalds snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
14441da177e4SLinus Torvalds hmidi->input = substream;
14451da177e4SLinus Torvalds spin_unlock_irq (&hmidi->lock);
14461da177e4SLinus Torvalds
14471da177e4SLinus Torvalds return 0;
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds
snd_hdsp_midi_output_open(struct snd_rawmidi_substream * substream)145055e957d8STakashi Iwai static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
14511da177e4SLinus Torvalds {
145255e957d8STakashi Iwai struct hdsp_midi *hmidi;
14531da177e4SLinus Torvalds
145455e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14551da177e4SLinus Torvalds spin_lock_irq (&hmidi->lock);
14561da177e4SLinus Torvalds hmidi->output = substream;
14571da177e4SLinus Torvalds spin_unlock_irq (&hmidi->lock);
14581da177e4SLinus Torvalds
14591da177e4SLinus Torvalds return 0;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds
snd_hdsp_midi_input_close(struct snd_rawmidi_substream * substream)146255e957d8STakashi Iwai static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
14631da177e4SLinus Torvalds {
146455e957d8STakashi Iwai struct hdsp_midi *hmidi;
14651da177e4SLinus Torvalds
14661da177e4SLinus Torvalds snd_hdsp_midi_input_trigger (substream, 0);
14671da177e4SLinus Torvalds
146855e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14691da177e4SLinus Torvalds spin_lock_irq (&hmidi->lock);
14701da177e4SLinus Torvalds hmidi->input = NULL;
14711da177e4SLinus Torvalds spin_unlock_irq (&hmidi->lock);
14721da177e4SLinus Torvalds
14731da177e4SLinus Torvalds return 0;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds
snd_hdsp_midi_output_close(struct snd_rawmidi_substream * substream)147655e957d8STakashi Iwai static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
14771da177e4SLinus Torvalds {
147855e957d8STakashi Iwai struct hdsp_midi *hmidi;
14791da177e4SLinus Torvalds
14801da177e4SLinus Torvalds snd_hdsp_midi_output_trigger (substream, 0);
14811da177e4SLinus Torvalds
148255e957d8STakashi Iwai hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
14831da177e4SLinus Torvalds spin_lock_irq (&hmidi->lock);
14841da177e4SLinus Torvalds hmidi->output = NULL;
14851da177e4SLinus Torvalds spin_unlock_irq (&hmidi->lock);
14861da177e4SLinus Torvalds
14871da177e4SLinus Torvalds return 0;
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds
1490485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_hdsp_midi_output =
14911da177e4SLinus Torvalds {
14921da177e4SLinus Torvalds .open = snd_hdsp_midi_output_open,
14931da177e4SLinus Torvalds .close = snd_hdsp_midi_output_close,
14941da177e4SLinus Torvalds .trigger = snd_hdsp_midi_output_trigger,
14951da177e4SLinus Torvalds };
14961da177e4SLinus Torvalds
1497485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_hdsp_midi_input =
14981da177e4SLinus Torvalds {
14991da177e4SLinus Torvalds .open = snd_hdsp_midi_input_open,
15001da177e4SLinus Torvalds .close = snd_hdsp_midi_input_close,
15011da177e4SLinus Torvalds .trigger = snd_hdsp_midi_input_trigger,
15021da177e4SLinus Torvalds };
15031da177e4SLinus Torvalds
snd_hdsp_create_midi(struct snd_card * card,struct hdsp * hdsp,int id)1504f40b6890STakashi Iwai static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
15051da177e4SLinus Torvalds {
1506a3b3a9dbSDan Carpenter char buf[40];
15071da177e4SLinus Torvalds
15081da177e4SLinus Torvalds hdsp->midi[id].id = id;
15091da177e4SLinus Torvalds hdsp->midi[id].rmidi = NULL;
15101da177e4SLinus Torvalds hdsp->midi[id].input = NULL;
15111da177e4SLinus Torvalds hdsp->midi[id].output = NULL;
15121da177e4SLinus Torvalds hdsp->midi[id].hdsp = hdsp;
15131da177e4SLinus Torvalds hdsp->midi[id].istimer = 0;
15141da177e4SLinus Torvalds hdsp->midi[id].pending = 0;
15151da177e4SLinus Torvalds spin_lock_init (&hdsp->midi[id].lock);
15161da177e4SLinus Torvalds
1517a3b3a9dbSDan Carpenter snprintf(buf, sizeof(buf), "%s MIDI %d", card->shortname, id + 1);
1518b0b98119STakashi Iwai if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
15191da177e4SLinus Torvalds return -1;
15201da177e4SLinus Torvalds
1521972d4c50SJaroslav Kysela sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
15221da177e4SLinus Torvalds hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
15231da177e4SLinus Torvalds
15241da177e4SLinus Torvalds snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
15251da177e4SLinus Torvalds snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
15261da177e4SLinus Torvalds
15271da177e4SLinus Torvalds hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
15281da177e4SLinus Torvalds SNDRV_RAWMIDI_INFO_INPUT |
15291da177e4SLinus Torvalds SNDRV_RAWMIDI_INFO_DUPLEX;
15301da177e4SLinus Torvalds
15311da177e4SLinus Torvalds return 0;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds
15341da177e4SLinus Torvalds /*-----------------------------------------------------------------------------
15351da177e4SLinus Torvalds Control Interface
15361da177e4SLinus Torvalds ----------------------------------------------------------------------------*/
15371da177e4SLinus Torvalds
snd_hdsp_convert_from_aes(struct snd_aes_iec958 * aes)153855e957d8STakashi Iwai static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds u32 val = 0;
15411da177e4SLinus Torvalds val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
15421da177e4SLinus Torvalds val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
15431da177e4SLinus Torvalds if (val & HDSP_SPDIFProfessional)
15441da177e4SLinus Torvalds val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
15451da177e4SLinus Torvalds else
15461da177e4SLinus Torvalds val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
15471da177e4SLinus Torvalds return val;
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds
snd_hdsp_convert_to_aes(struct snd_aes_iec958 * aes,u32 val)155055e957d8STakashi Iwai static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
15531da177e4SLinus Torvalds ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
15541da177e4SLinus Torvalds if (val & HDSP_SPDIFProfessional)
15551da177e4SLinus Torvalds aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
15561da177e4SLinus Torvalds else
15571da177e4SLinus Torvalds aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds
snd_hdsp_control_spdif_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)156055e957d8STakashi Iwai static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15611da177e4SLinus Torvalds {
15621da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15631da177e4SLinus Torvalds uinfo->count = 1;
15641da177e4SLinus Torvalds return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds
snd_hdsp_control_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)156755e957d8STakashi Iwai static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15681da177e4SLinus Torvalds {
156955e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15701da177e4SLinus Torvalds
15711da177e4SLinus Torvalds snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
15721da177e4SLinus Torvalds return 0;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds
snd_hdsp_control_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)157555e957d8STakashi Iwai static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15761da177e4SLinus Torvalds {
157755e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15781da177e4SLinus Torvalds int change;
15791da177e4SLinus Torvalds u32 val;
15801da177e4SLinus Torvalds
15811da177e4SLinus Torvalds val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
15821da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
15831da177e4SLinus Torvalds change = val != hdsp->creg_spdif;
15841da177e4SLinus Torvalds hdsp->creg_spdif = val;
15851da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
15861da177e4SLinus Torvalds return change;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds
snd_hdsp_control_spdif_stream_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)158955e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
15921da177e4SLinus Torvalds uinfo->count = 1;
15931da177e4SLinus Torvalds return 0;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds
snd_hdsp_control_spdif_stream_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)159655e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15971da177e4SLinus Torvalds {
159855e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
15991da177e4SLinus Torvalds
16001da177e4SLinus Torvalds snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
16011da177e4SLinus Torvalds return 0;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds
snd_hdsp_control_spdif_stream_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)160455e957d8STakashi Iwai static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16051da177e4SLinus Torvalds {
160655e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16071da177e4SLinus Torvalds int change;
16081da177e4SLinus Torvalds u32 val;
16091da177e4SLinus Torvalds
16101da177e4SLinus Torvalds val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
16111da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
16121da177e4SLinus Torvalds change = val != hdsp->creg_spdif_stream;
16131da177e4SLinus Torvalds hdsp->creg_spdif_stream = val;
16141da177e4SLinus Torvalds hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
16151da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
16161da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
16171da177e4SLinus Torvalds return change;
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds
snd_hdsp_control_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)162055e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
16211da177e4SLinus Torvalds {
16221da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
16231da177e4SLinus Torvalds uinfo->count = 1;
16241da177e4SLinus Torvalds return 0;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds
snd_hdsp_control_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)162755e957d8STakashi Iwai static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16281da177e4SLinus Torvalds {
16291da177e4SLinus Torvalds ucontrol->value.iec958.status[0] = kcontrol->private_value;
16301da177e4SLinus Torvalds return 0;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds
16331da177e4SLinus Torvalds #define HDSP_SPDIF_IN(xname, xindex) \
163467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
16351da177e4SLinus Torvalds .name = xname, \
16361da177e4SLinus Torvalds .index = xindex, \
16371da177e4SLinus Torvalds .info = snd_hdsp_info_spdif_in, \
16381da177e4SLinus Torvalds .get = snd_hdsp_get_spdif_in, \
16391da177e4SLinus Torvalds .put = snd_hdsp_put_spdif_in }
16401da177e4SLinus Torvalds
hdsp_spdif_in(struct hdsp * hdsp)164155e957d8STakashi Iwai static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds
hdsp_set_spdif_input(struct hdsp * hdsp,int in)164655e957d8STakashi Iwai static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
16471da177e4SLinus Torvalds {
16481da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_SPDIFInputMask;
16491da177e4SLinus Torvalds hdsp->control_register |= hdsp_encode_spdif_in(in);
16501da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
16511da177e4SLinus Torvalds return 0;
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds
snd_hdsp_info_spdif_in(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)165455e957d8STakashi Iwai static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
16551da177e4SLinus Torvalds {
16568d678da9STakashi Iwai static const char * const texts[4] = {
16578d678da9STakashi Iwai "Optical", "Coaxial", "Internal", "AES"
16588d678da9STakashi Iwai };
165955e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16601da177e4SLinus Torvalds
16618d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
16628d678da9STakashi Iwai texts);
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds
snd_hdsp_get_spdif_in(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)166555e957d8STakashi Iwai static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16661da177e4SLinus Torvalds {
166755e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16681da177e4SLinus Torvalds
16691da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
16701da177e4SLinus Torvalds return 0;
16711da177e4SLinus Torvalds }
16721da177e4SLinus Torvalds
snd_hdsp_put_spdif_in(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)167355e957d8STakashi Iwai static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16741da177e4SLinus Torvalds {
167555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
16761da177e4SLinus Torvalds int change;
16771da177e4SLinus Torvalds unsigned int val;
16781da177e4SLinus Torvalds
16791da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
16801da177e4SLinus Torvalds return -EBUSY;
16811da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
16821da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
16831da177e4SLinus Torvalds change = val != hdsp_spdif_in(hdsp);
16841da177e4SLinus Torvalds if (change)
16851da177e4SLinus Torvalds hdsp_set_spdif_input(hdsp, val);
16861da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
16871da177e4SLinus Torvalds return change;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds
169066d9244eSAdrian Knoth #define HDSP_TOGGLE_SETTING(xname, xindex) \
169166d9244eSAdrian Knoth { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
169266d9244eSAdrian Knoth .name = xname, \
169366d9244eSAdrian Knoth .private_value = xindex, \
169466d9244eSAdrian Knoth .info = snd_hdsp_info_toggle_setting, \
169566d9244eSAdrian Knoth .get = snd_hdsp_get_toggle_setting, \
169666d9244eSAdrian Knoth .put = snd_hdsp_put_toggle_setting \
169766d9244eSAdrian Knoth }
169866d9244eSAdrian Knoth
hdsp_toggle_setting(struct hdsp * hdsp,u32 regmask)169966d9244eSAdrian Knoth static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
170066d9244eSAdrian Knoth {
170166d9244eSAdrian Knoth return (hdsp->control_register & regmask) ? 1 : 0;
170266d9244eSAdrian Knoth }
170366d9244eSAdrian Knoth
hdsp_set_toggle_setting(struct hdsp * hdsp,u32 regmask,int out)170466d9244eSAdrian Knoth static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
170566d9244eSAdrian Knoth {
170666d9244eSAdrian Knoth if (out)
170766d9244eSAdrian Knoth hdsp->control_register |= regmask;
170866d9244eSAdrian Knoth else
170966d9244eSAdrian Knoth hdsp->control_register &= ~regmask;
171066d9244eSAdrian Knoth hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
171166d9244eSAdrian Knoth
171266d9244eSAdrian Knoth return 0;
171366d9244eSAdrian Knoth }
171466d9244eSAdrian Knoth
171566d9244eSAdrian Knoth #define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
171666d9244eSAdrian Knoth
snd_hdsp_get_toggle_setting(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)171766d9244eSAdrian Knoth static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
171866d9244eSAdrian Knoth struct snd_ctl_elem_value *ucontrol)
171966d9244eSAdrian Knoth {
172066d9244eSAdrian Knoth struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
172166d9244eSAdrian Knoth u32 regmask = kcontrol->private_value;
172266d9244eSAdrian Knoth
172366d9244eSAdrian Knoth spin_lock_irq(&hdsp->lock);
172466d9244eSAdrian Knoth ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
172566d9244eSAdrian Knoth spin_unlock_irq(&hdsp->lock);
172666d9244eSAdrian Knoth return 0;
172766d9244eSAdrian Knoth }
172866d9244eSAdrian Knoth
snd_hdsp_put_toggle_setting(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)172966d9244eSAdrian Knoth static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
173066d9244eSAdrian Knoth struct snd_ctl_elem_value *ucontrol)
173166d9244eSAdrian Knoth {
173266d9244eSAdrian Knoth struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
173366d9244eSAdrian Knoth u32 regmask = kcontrol->private_value;
173466d9244eSAdrian Knoth int change;
173566d9244eSAdrian Knoth unsigned int val;
173666d9244eSAdrian Knoth
173766d9244eSAdrian Knoth if (!snd_hdsp_use_is_exclusive(hdsp))
173866d9244eSAdrian Knoth return -EBUSY;
173966d9244eSAdrian Knoth val = ucontrol->value.integer.value[0] & 1;
174066d9244eSAdrian Knoth spin_lock_irq(&hdsp->lock);
174166d9244eSAdrian Knoth change = (int) val != hdsp_toggle_setting(hdsp, regmask);
174266d9244eSAdrian Knoth if (change)
174366d9244eSAdrian Knoth hdsp_set_toggle_setting(hdsp, regmask, val);
174466d9244eSAdrian Knoth spin_unlock_irq(&hdsp->lock);
174566d9244eSAdrian Knoth return change;
174666d9244eSAdrian Knoth }
174766d9244eSAdrian Knoth
17481da177e4SLinus Torvalds #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
174967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
17501da177e4SLinus Torvalds .name = xname, \
17511da177e4SLinus Torvalds .index = xindex, \
17521da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, \
17531da177e4SLinus Torvalds .info = snd_hdsp_info_spdif_sample_rate, \
17541da177e4SLinus Torvalds .get = snd_hdsp_get_spdif_sample_rate \
17551da177e4SLinus Torvalds }
17561da177e4SLinus Torvalds
snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)175755e957d8STakashi Iwai static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
17581da177e4SLinus Torvalds {
17598d678da9STakashi Iwai static const char * const texts[] = {
17608d678da9STakashi Iwai "32000", "44100", "48000", "64000", "88200", "96000",
17618d678da9STakashi Iwai "None", "128000", "176400", "192000"
17628d678da9STakashi Iwai };
176355e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17641da177e4SLinus Torvalds
17658d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
17668d678da9STakashi Iwai texts);
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds
snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)176955e957d8STakashi Iwai static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17701da177e4SLinus Torvalds {
177155e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
17721da177e4SLinus Torvalds
17731da177e4SLinus Torvalds switch (hdsp_spdif_sample_rate(hdsp)) {
17741da177e4SLinus Torvalds case 32000:
17751da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 0;
17761da177e4SLinus Torvalds break;
17771da177e4SLinus Torvalds case 44100:
17781da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 1;
17791da177e4SLinus Torvalds break;
17801da177e4SLinus Torvalds case 48000:
17811da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 2;
17821da177e4SLinus Torvalds break;
17831da177e4SLinus Torvalds case 64000:
17841da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 3;
17851da177e4SLinus Torvalds break;
17861da177e4SLinus Torvalds case 88200:
17871da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 4;
17881da177e4SLinus Torvalds break;
17891da177e4SLinus Torvalds case 96000:
17901da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 5;
17911da177e4SLinus Torvalds break;
17921da177e4SLinus Torvalds case 128000:
17931da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 7;
17941da177e4SLinus Torvalds break;
17951da177e4SLinus Torvalds case 176400:
17961da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 8;
17971da177e4SLinus Torvalds break;
17981da177e4SLinus Torvalds case 192000:
17991da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 9;
18001da177e4SLinus Torvalds break;
18011da177e4SLinus Torvalds default:
18021da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 6;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds return 0;
18051da177e4SLinus Torvalds }
18061da177e4SLinus Torvalds
18071da177e4SLinus Torvalds #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
180867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18091da177e4SLinus Torvalds .name = xname, \
18101da177e4SLinus Torvalds .index = xindex, \
18111da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18121da177e4SLinus Torvalds .info = snd_hdsp_info_system_sample_rate, \
18131da177e4SLinus Torvalds .get = snd_hdsp_get_system_sample_rate \
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds
snd_hdsp_info_system_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)181655e957d8STakashi Iwai static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18191da177e4SLinus Torvalds uinfo->count = 1;
18201da177e4SLinus Torvalds return 0;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds
snd_hdsp_get_system_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)182355e957d8STakashi Iwai static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18241da177e4SLinus Torvalds {
182555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18261da177e4SLinus Torvalds
18271da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
18281da177e4SLinus Torvalds return 0;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds
18311da177e4SLinus Torvalds #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
183267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18331da177e4SLinus Torvalds .name = xname, \
18341da177e4SLinus Torvalds .index = xindex, \
18351da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18361da177e4SLinus Torvalds .info = snd_hdsp_info_autosync_sample_rate, \
18371da177e4SLinus Torvalds .get = snd_hdsp_get_autosync_sample_rate \
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds
snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)184055e957d8STakashi Iwai static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
18411da177e4SLinus Torvalds {
184255e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18438d678da9STakashi Iwai static const char * const texts[] = {
18448d678da9STakashi Iwai "32000", "44100", "48000", "64000", "88200", "96000",
18458d678da9STakashi Iwai "None", "128000", "176400", "192000"
18468d678da9STakashi Iwai };
18478d678da9STakashi Iwai
18488d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
18498d678da9STakashi Iwai texts);
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds
snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)185255e957d8STakashi Iwai static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
18531da177e4SLinus Torvalds {
185455e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
18551da177e4SLinus Torvalds
18561da177e4SLinus Torvalds switch (hdsp_external_sample_rate(hdsp)) {
18571da177e4SLinus Torvalds case 32000:
18581da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 0;
18591da177e4SLinus Torvalds break;
18601da177e4SLinus Torvalds case 44100:
18611da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 1;
18621da177e4SLinus Torvalds break;
18631da177e4SLinus Torvalds case 48000:
18641da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 2;
18651da177e4SLinus Torvalds break;
18661da177e4SLinus Torvalds case 64000:
18671da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 3;
18681da177e4SLinus Torvalds break;
18691da177e4SLinus Torvalds case 88200:
18701da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 4;
18711da177e4SLinus Torvalds break;
18721da177e4SLinus Torvalds case 96000:
18731da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 5;
18741da177e4SLinus Torvalds break;
18751da177e4SLinus Torvalds case 128000:
18761da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 7;
18771da177e4SLinus Torvalds break;
18781da177e4SLinus Torvalds case 176400:
18791da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 8;
18801da177e4SLinus Torvalds break;
18811da177e4SLinus Torvalds case 192000:
18821da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 9;
18831da177e4SLinus Torvalds break;
18841da177e4SLinus Torvalds default:
18851da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = 6;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds return 0;
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds
18901da177e4SLinus Torvalds #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
189167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
18921da177e4SLinus Torvalds .name = xname, \
18931da177e4SLinus Torvalds .index = xindex, \
18941da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, \
18951da177e4SLinus Torvalds .info = snd_hdsp_info_system_clock_mode, \
18961da177e4SLinus Torvalds .get = snd_hdsp_get_system_clock_mode \
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds
hdsp_system_clock_mode(struct hdsp * hdsp)189955e957d8STakashi Iwai static int hdsp_system_clock_mode(struct hdsp *hdsp)
19001da177e4SLinus Torvalds {
1901b0b98119STakashi Iwai if (hdsp->control_register & HDSP_ClockModeMaster)
19021da177e4SLinus Torvalds return 0;
1903b0b98119STakashi Iwai else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
19041da177e4SLinus Torvalds return 0;
19051da177e4SLinus Torvalds return 1;
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds
snd_hdsp_info_system_clock_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)190855e957d8STakashi Iwai static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19091da177e4SLinus Torvalds {
19108d678da9STakashi Iwai static const char * const texts[] = {"Master", "Slave" };
19111da177e4SLinus Torvalds
19128d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 2, texts);
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds
snd_hdsp_get_system_clock_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)191555e957d8STakashi Iwai static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
19161da177e4SLinus Torvalds {
191755e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
19181da177e4SLinus Torvalds
19191da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
19201da177e4SLinus Torvalds return 0;
19211da177e4SLinus Torvalds }
19221da177e4SLinus Torvalds
19231da177e4SLinus Torvalds #define HDSP_CLOCK_SOURCE(xname, xindex) \
192467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
19251da177e4SLinus Torvalds .name = xname, \
19261da177e4SLinus Torvalds .index = xindex, \
19271da177e4SLinus Torvalds .info = snd_hdsp_info_clock_source, \
19281da177e4SLinus Torvalds .get = snd_hdsp_get_clock_source, \
19291da177e4SLinus Torvalds .put = snd_hdsp_put_clock_source \
19301da177e4SLinus Torvalds }
19311da177e4SLinus Torvalds
hdsp_clock_source(struct hdsp * hdsp)193255e957d8STakashi Iwai static int hdsp_clock_source(struct hdsp *hdsp)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds if (hdsp->control_register & HDSP_ClockModeMaster) {
19351da177e4SLinus Torvalds switch (hdsp->system_sample_rate) {
19361da177e4SLinus Torvalds case 32000:
19371da177e4SLinus Torvalds return 1;
19381da177e4SLinus Torvalds case 44100:
19391da177e4SLinus Torvalds return 2;
19401da177e4SLinus Torvalds case 48000:
19411da177e4SLinus Torvalds return 3;
19421da177e4SLinus Torvalds case 64000:
19431da177e4SLinus Torvalds return 4;
19441da177e4SLinus Torvalds case 88200:
19451da177e4SLinus Torvalds return 5;
19461da177e4SLinus Torvalds case 96000:
19471da177e4SLinus Torvalds return 6;
19481da177e4SLinus Torvalds case 128000:
19491da177e4SLinus Torvalds return 7;
19501da177e4SLinus Torvalds case 176400:
19511da177e4SLinus Torvalds return 8;
19521da177e4SLinus Torvalds case 192000:
19531da177e4SLinus Torvalds return 9;
19541da177e4SLinus Torvalds default:
19551da177e4SLinus Torvalds return 3;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds } else {
19581da177e4SLinus Torvalds return 0;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds
hdsp_set_clock_source(struct hdsp * hdsp,int mode)196255e957d8STakashi Iwai static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
19631da177e4SLinus Torvalds {
19641da177e4SLinus Torvalds int rate;
19651da177e4SLinus Torvalds switch (mode) {
19661da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_AUTOSYNC:
19671da177e4SLinus Torvalds if (hdsp_external_sample_rate(hdsp) != 0) {
19681da177e4SLinus Torvalds if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
19691da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_ClockModeMaster;
19701da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
19711da177e4SLinus Torvalds return 0;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds return -1;
19751da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
19761da177e4SLinus Torvalds rate = 32000;
19771da177e4SLinus Torvalds break;
19781da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
19791da177e4SLinus Torvalds rate = 44100;
19801da177e4SLinus Torvalds break;
19811da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
19821da177e4SLinus Torvalds rate = 48000;
19831da177e4SLinus Torvalds break;
19841da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
19851da177e4SLinus Torvalds rate = 64000;
19861da177e4SLinus Torvalds break;
19871da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
19881da177e4SLinus Torvalds rate = 88200;
19891da177e4SLinus Torvalds break;
19901da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
19911da177e4SLinus Torvalds rate = 96000;
19921da177e4SLinus Torvalds break;
19931da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
19941da177e4SLinus Torvalds rate = 128000;
19951da177e4SLinus Torvalds break;
19961da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
19971da177e4SLinus Torvalds rate = 176400;
19981da177e4SLinus Torvalds break;
19991da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
20001da177e4SLinus Torvalds rate = 192000;
20011da177e4SLinus Torvalds break;
20021da177e4SLinus Torvalds default:
20031da177e4SLinus Torvalds rate = 48000;
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds hdsp->control_register |= HDSP_ClockModeMaster;
20061da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
20071da177e4SLinus Torvalds hdsp_set_rate(hdsp, rate, 1);
20081da177e4SLinus Torvalds return 0;
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds
snd_hdsp_info_clock_source(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)201155e957d8STakashi Iwai static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
20121da177e4SLinus Torvalds {
20138d678da9STakashi Iwai static const char * const texts[] = {
20148d678da9STakashi Iwai "AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
20158d678da9STakashi Iwai "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
20168d678da9STakashi Iwai "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
20178d678da9STakashi Iwai "Internal 192.0 KHz"
20188d678da9STakashi Iwai };
201955e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20201da177e4SLinus Torvalds
20218d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
20228d678da9STakashi Iwai texts);
20231da177e4SLinus Torvalds }
20241da177e4SLinus Torvalds
snd_hdsp_get_clock_source(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)202555e957d8STakashi Iwai static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20261da177e4SLinus Torvalds {
202755e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20281da177e4SLinus Torvalds
20291da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
20301da177e4SLinus Torvalds return 0;
20311da177e4SLinus Torvalds }
20321da177e4SLinus Torvalds
snd_hdsp_put_clock_source(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)203355e957d8STakashi Iwai static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
20341da177e4SLinus Torvalds {
203555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
20361da177e4SLinus Torvalds int change;
20371da177e4SLinus Torvalds int val;
20381da177e4SLinus Torvalds
20391da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
20401da177e4SLinus Torvalds return -EBUSY;
20411da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0];
20421da177e4SLinus Torvalds if (val < 0) val = 0;
20431da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
2044b0b98119STakashi Iwai if (val > 9)
2045b0b98119STakashi Iwai val = 9;
20461da177e4SLinus Torvalds } else {
2047b0b98119STakashi Iwai if (val > 6)
2048b0b98119STakashi Iwai val = 6;
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
2051b0b98119STakashi Iwai if (val != hdsp_clock_source(hdsp))
20521da177e4SLinus Torvalds change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
2053b0b98119STakashi Iwai else
20541da177e4SLinus Torvalds change = 0;
20551da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
20561da177e4SLinus Torvalds return change;
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds
2059a5ce8890STakashi Iwai #define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
2060e3ea4d89STakashi Iwai
snd_hdsp_get_clock_source_lock(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)206155e957d8STakashi Iwai static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2062e3ea4d89STakashi Iwai {
206355e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2064e3ea4d89STakashi Iwai
2065e3ea4d89STakashi Iwai ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
2066e3ea4d89STakashi Iwai return 0;
2067e3ea4d89STakashi Iwai }
2068e3ea4d89STakashi Iwai
snd_hdsp_put_clock_source_lock(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)206955e957d8STakashi Iwai static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2070e3ea4d89STakashi Iwai {
207155e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2072e3ea4d89STakashi Iwai int change;
2073e3ea4d89STakashi Iwai
2074e3ea4d89STakashi Iwai change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
2075e3ea4d89STakashi Iwai if (change)
20764e98d6a7STakashi Iwai hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
2077e3ea4d89STakashi Iwai return change;
2078e3ea4d89STakashi Iwai }
2079e3ea4d89STakashi Iwai
20801da177e4SLinus Torvalds #define HDSP_DA_GAIN(xname, xindex) \
208167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
20821da177e4SLinus Torvalds .name = xname, \
20831da177e4SLinus Torvalds .index = xindex, \
20841da177e4SLinus Torvalds .info = snd_hdsp_info_da_gain, \
20851da177e4SLinus Torvalds .get = snd_hdsp_get_da_gain, \
20861da177e4SLinus Torvalds .put = snd_hdsp_put_da_gain \
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds
hdsp_da_gain(struct hdsp * hdsp)208955e957d8STakashi Iwai static int hdsp_da_gain(struct hdsp *hdsp)
20901da177e4SLinus Torvalds {
20911da177e4SLinus Torvalds switch (hdsp->control_register & HDSP_DAGainMask) {
20921da177e4SLinus Torvalds case HDSP_DAGainHighGain:
20931da177e4SLinus Torvalds return 0;
20941da177e4SLinus Torvalds case HDSP_DAGainPlus4dBu:
20951da177e4SLinus Torvalds return 1;
20961da177e4SLinus Torvalds case HDSP_DAGainMinus10dBV:
20971da177e4SLinus Torvalds return 2;
20981da177e4SLinus Torvalds default:
20991da177e4SLinus Torvalds return 1;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds
hdsp_set_da_gain(struct hdsp * hdsp,int mode)210355e957d8STakashi Iwai static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_DAGainMask;
21061da177e4SLinus Torvalds switch (mode) {
21071da177e4SLinus Torvalds case 0:
21081da177e4SLinus Torvalds hdsp->control_register |= HDSP_DAGainHighGain;
21091da177e4SLinus Torvalds break;
21101da177e4SLinus Torvalds case 1:
21111da177e4SLinus Torvalds hdsp->control_register |= HDSP_DAGainPlus4dBu;
21121da177e4SLinus Torvalds break;
21131da177e4SLinus Torvalds case 2:
21141da177e4SLinus Torvalds hdsp->control_register |= HDSP_DAGainMinus10dBV;
21151da177e4SLinus Torvalds break;
21161da177e4SLinus Torvalds default:
21171da177e4SLinus Torvalds return -1;
21181da177e4SLinus Torvalds
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
21211da177e4SLinus Torvalds return 0;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds
snd_hdsp_info_da_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)212455e957d8STakashi Iwai static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
21251da177e4SLinus Torvalds {
21268d678da9STakashi Iwai static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
21271da177e4SLinus Torvalds
21288d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 3, texts);
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds
snd_hdsp_get_da_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)213155e957d8STakashi Iwai static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21321da177e4SLinus Torvalds {
213355e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21341da177e4SLinus Torvalds
21351da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
21361da177e4SLinus Torvalds return 0;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds
snd_hdsp_put_da_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)213955e957d8STakashi Iwai static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
21401da177e4SLinus Torvalds {
214155e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
21421da177e4SLinus Torvalds int change;
21431da177e4SLinus Torvalds int val;
21441da177e4SLinus Torvalds
21451da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
21461da177e4SLinus Torvalds return -EBUSY;
21471da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0];
21481da177e4SLinus Torvalds if (val < 0) val = 0;
21491da177e4SLinus Torvalds if (val > 2) val = 2;
21501da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
2151b0b98119STakashi Iwai if (val != hdsp_da_gain(hdsp))
21521da177e4SLinus Torvalds change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
2153b0b98119STakashi Iwai else
21541da177e4SLinus Torvalds change = 0;
21551da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
21561da177e4SLinus Torvalds return change;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds
21591da177e4SLinus Torvalds #define HDSP_AD_GAIN(xname, xindex) \
216067ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
21611da177e4SLinus Torvalds .name = xname, \
21621da177e4SLinus Torvalds .index = xindex, \
21631da177e4SLinus Torvalds .info = snd_hdsp_info_ad_gain, \
21641da177e4SLinus Torvalds .get = snd_hdsp_get_ad_gain, \
21651da177e4SLinus Torvalds .put = snd_hdsp_put_ad_gain \
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds
hdsp_ad_gain(struct hdsp * hdsp)216855e957d8STakashi Iwai static int hdsp_ad_gain(struct hdsp *hdsp)
21691da177e4SLinus Torvalds {
21701da177e4SLinus Torvalds switch (hdsp->control_register & HDSP_ADGainMask) {
21711da177e4SLinus Torvalds case HDSP_ADGainMinus10dBV:
21721da177e4SLinus Torvalds return 0;
21731da177e4SLinus Torvalds case HDSP_ADGainPlus4dBu:
21741da177e4SLinus Torvalds return 1;
21751da177e4SLinus Torvalds case HDSP_ADGainLowGain:
21761da177e4SLinus Torvalds return 2;
21771da177e4SLinus Torvalds default:
21781da177e4SLinus Torvalds return 1;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds
hdsp_set_ad_gain(struct hdsp * hdsp,int mode)218255e957d8STakashi Iwai static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
21831da177e4SLinus Torvalds {
21841da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_ADGainMask;
21851da177e4SLinus Torvalds switch (mode) {
21861da177e4SLinus Torvalds case 0:
21871da177e4SLinus Torvalds hdsp->control_register |= HDSP_ADGainMinus10dBV;
21881da177e4SLinus Torvalds break;
21891da177e4SLinus Torvalds case 1:
21901da177e4SLinus Torvalds hdsp->control_register |= HDSP_ADGainPlus4dBu;
21911da177e4SLinus Torvalds break;
21921da177e4SLinus Torvalds case 2:
21931da177e4SLinus Torvalds hdsp->control_register |= HDSP_ADGainLowGain;
21941da177e4SLinus Torvalds break;
21951da177e4SLinus Torvalds default:
21961da177e4SLinus Torvalds return -1;
21971da177e4SLinus Torvalds
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22001da177e4SLinus Torvalds return 0;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds
snd_hdsp_info_ad_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)220355e957d8STakashi Iwai static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22041da177e4SLinus Torvalds {
22058d678da9STakashi Iwai static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
22061da177e4SLinus Torvalds
22078d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 3, texts);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds
snd_hdsp_get_ad_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)221055e957d8STakashi Iwai static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22111da177e4SLinus Torvalds {
221255e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22131da177e4SLinus Torvalds
22141da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
22151da177e4SLinus Torvalds return 0;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds
snd_hdsp_put_ad_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)221855e957d8STakashi Iwai static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22191da177e4SLinus Torvalds {
222055e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22211da177e4SLinus Torvalds int change;
22221da177e4SLinus Torvalds int val;
22231da177e4SLinus Torvalds
22241da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
22251da177e4SLinus Torvalds return -EBUSY;
22261da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0];
22271da177e4SLinus Torvalds if (val < 0) val = 0;
22281da177e4SLinus Torvalds if (val > 2) val = 2;
22291da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
2230b0b98119STakashi Iwai if (val != hdsp_ad_gain(hdsp))
22311da177e4SLinus Torvalds change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
2232b0b98119STakashi Iwai else
22331da177e4SLinus Torvalds change = 0;
22341da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
22351da177e4SLinus Torvalds return change;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds
22381da177e4SLinus Torvalds #define HDSP_PHONE_GAIN(xname, xindex) \
223967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
22401da177e4SLinus Torvalds .name = xname, \
22411da177e4SLinus Torvalds .index = xindex, \
22421da177e4SLinus Torvalds .info = snd_hdsp_info_phone_gain, \
22431da177e4SLinus Torvalds .get = snd_hdsp_get_phone_gain, \
22441da177e4SLinus Torvalds .put = snd_hdsp_put_phone_gain \
22451da177e4SLinus Torvalds }
22461da177e4SLinus Torvalds
hdsp_phone_gain(struct hdsp * hdsp)224755e957d8STakashi Iwai static int hdsp_phone_gain(struct hdsp *hdsp)
22481da177e4SLinus Torvalds {
22491da177e4SLinus Torvalds switch (hdsp->control_register & HDSP_PhoneGainMask) {
22501da177e4SLinus Torvalds case HDSP_PhoneGain0dB:
22511da177e4SLinus Torvalds return 0;
22521da177e4SLinus Torvalds case HDSP_PhoneGainMinus6dB:
22531da177e4SLinus Torvalds return 1;
22541da177e4SLinus Torvalds case HDSP_PhoneGainMinus12dB:
22551da177e4SLinus Torvalds return 2;
22561da177e4SLinus Torvalds default:
22571da177e4SLinus Torvalds return 0;
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds
hdsp_set_phone_gain(struct hdsp * hdsp,int mode)226155e957d8STakashi Iwai static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_PhoneGainMask;
22641da177e4SLinus Torvalds switch (mode) {
22651da177e4SLinus Torvalds case 0:
22661da177e4SLinus Torvalds hdsp->control_register |= HDSP_PhoneGain0dB;
22671da177e4SLinus Torvalds break;
22681da177e4SLinus Torvalds case 1:
22691da177e4SLinus Torvalds hdsp->control_register |= HDSP_PhoneGainMinus6dB;
22701da177e4SLinus Torvalds break;
22711da177e4SLinus Torvalds case 2:
22721da177e4SLinus Torvalds hdsp->control_register |= HDSP_PhoneGainMinus12dB;
22731da177e4SLinus Torvalds break;
22741da177e4SLinus Torvalds default:
22751da177e4SLinus Torvalds return -1;
22761da177e4SLinus Torvalds
22771da177e4SLinus Torvalds }
22781da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
22791da177e4SLinus Torvalds return 0;
22801da177e4SLinus Torvalds }
22811da177e4SLinus Torvalds
snd_hdsp_info_phone_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)228255e957d8STakashi Iwai static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
22831da177e4SLinus Torvalds {
22848d678da9STakashi Iwai static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
22851da177e4SLinus Torvalds
22868d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 3, texts);
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds
snd_hdsp_get_phone_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)228955e957d8STakashi Iwai static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22901da177e4SLinus Torvalds {
229155e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
22921da177e4SLinus Torvalds
22931da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
22941da177e4SLinus Torvalds return 0;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds
snd_hdsp_put_phone_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)229755e957d8STakashi Iwai static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
22981da177e4SLinus Torvalds {
229955e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23001da177e4SLinus Torvalds int change;
23011da177e4SLinus Torvalds int val;
23021da177e4SLinus Torvalds
23031da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
23041da177e4SLinus Torvalds return -EBUSY;
23051da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0];
23061da177e4SLinus Torvalds if (val < 0) val = 0;
23071da177e4SLinus Torvalds if (val > 2) val = 2;
23081da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
2309b0b98119STakashi Iwai if (val != hdsp_phone_gain(hdsp))
23101da177e4SLinus Torvalds change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
2311b0b98119STakashi Iwai else
23121da177e4SLinus Torvalds change = 0;
23131da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
23141da177e4SLinus Torvalds return change;
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds
23171da177e4SLinus Torvalds #define HDSP_PREF_SYNC_REF(xname, xindex) \
231867ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
23191da177e4SLinus Torvalds .name = xname, \
23201da177e4SLinus Torvalds .index = xindex, \
23211da177e4SLinus Torvalds .info = snd_hdsp_info_pref_sync_ref, \
23221da177e4SLinus Torvalds .get = snd_hdsp_get_pref_sync_ref, \
23231da177e4SLinus Torvalds .put = snd_hdsp_put_pref_sync_ref \
23241da177e4SLinus Torvalds }
23251da177e4SLinus Torvalds
hdsp_pref_sync_ref(struct hdsp * hdsp)232655e957d8STakashi Iwai static int hdsp_pref_sync_ref(struct hdsp *hdsp)
23271da177e4SLinus Torvalds {
23281da177e4SLinus Torvalds /* Notice that this looks at the requested sync source,
23291da177e4SLinus Torvalds not the one actually in use.
23301da177e4SLinus Torvalds */
23311da177e4SLinus Torvalds
23321da177e4SLinus Torvalds switch (hdsp->control_register & HDSP_SyncRefMask) {
23331da177e4SLinus Torvalds case HDSP_SyncRef_ADAT1:
23341da177e4SLinus Torvalds return HDSP_SYNC_FROM_ADAT1;
23351da177e4SLinus Torvalds case HDSP_SyncRef_ADAT2:
23361da177e4SLinus Torvalds return HDSP_SYNC_FROM_ADAT2;
23371da177e4SLinus Torvalds case HDSP_SyncRef_ADAT3:
23381da177e4SLinus Torvalds return HDSP_SYNC_FROM_ADAT3;
23391da177e4SLinus Torvalds case HDSP_SyncRef_SPDIF:
23401da177e4SLinus Torvalds return HDSP_SYNC_FROM_SPDIF;
23411da177e4SLinus Torvalds case HDSP_SyncRef_WORD:
23421da177e4SLinus Torvalds return HDSP_SYNC_FROM_WORD;
23431da177e4SLinus Torvalds case HDSP_SyncRef_ADAT_SYNC:
23441da177e4SLinus Torvalds return HDSP_SYNC_FROM_ADAT_SYNC;
23451da177e4SLinus Torvalds default:
23461da177e4SLinus Torvalds return HDSP_SYNC_FROM_WORD;
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds return 0;
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds
hdsp_set_pref_sync_ref(struct hdsp * hdsp,int pref)235155e957d8STakashi Iwai static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
23521da177e4SLinus Torvalds {
23531da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_SyncRefMask;
23541da177e4SLinus Torvalds switch (pref) {
23551da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT1:
23561da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
23571da177e4SLinus Torvalds break;
23581da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT2:
23591da177e4SLinus Torvalds hdsp->control_register |= HDSP_SyncRef_ADAT2;
23601da177e4SLinus Torvalds break;
23611da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT3:
23621da177e4SLinus Torvalds hdsp->control_register |= HDSP_SyncRef_ADAT3;
23631da177e4SLinus Torvalds break;
23641da177e4SLinus Torvalds case HDSP_SYNC_FROM_SPDIF:
23651da177e4SLinus Torvalds hdsp->control_register |= HDSP_SyncRef_SPDIF;
23661da177e4SLinus Torvalds break;
23671da177e4SLinus Torvalds case HDSP_SYNC_FROM_WORD:
23681da177e4SLinus Torvalds hdsp->control_register |= HDSP_SyncRef_WORD;
23691da177e4SLinus Torvalds break;
23701da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT_SYNC:
23711da177e4SLinus Torvalds hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
23721da177e4SLinus Torvalds break;
23731da177e4SLinus Torvalds default:
23741da177e4SLinus Torvalds return -1;
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
23771da177e4SLinus Torvalds return 0;
23781da177e4SLinus Torvalds }
23791da177e4SLinus Torvalds
snd_hdsp_info_pref_sync_ref(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)238055e957d8STakashi Iwai static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
23811da177e4SLinus Torvalds {
23828d678da9STakashi Iwai static const char * const texts[] = {
23838d678da9STakashi Iwai "Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
23848d678da9STakashi Iwai };
238555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
23868d678da9STakashi Iwai int num_items;
23871da177e4SLinus Torvalds
23881da177e4SLinus Torvalds switch (hdsp->io_type) {
23891da177e4SLinus Torvalds case Digiface:
23901da177e4SLinus Torvalds case H9652:
23918d678da9STakashi Iwai num_items = 6;
23921da177e4SLinus Torvalds break;
23931da177e4SLinus Torvalds case Multiface:
23948d678da9STakashi Iwai num_items = 4;
23951da177e4SLinus Torvalds break;
23961da177e4SLinus Torvalds case H9632:
23978d678da9STakashi Iwai num_items = 3;
23981da177e4SLinus Torvalds break;
23991da177e4SLinus Torvalds default:
24009badda0aSTakashi Iwai return -EINVAL;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds
24038d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, num_items, texts);
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds
snd_hdsp_get_pref_sync_ref(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)240655e957d8STakashi Iwai static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24071da177e4SLinus Torvalds {
240855e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24091da177e4SLinus Torvalds
24101da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
24111da177e4SLinus Torvalds return 0;
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds
snd_hdsp_put_pref_sync_ref(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)241455e957d8STakashi Iwai static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24151da177e4SLinus Torvalds {
241655e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24171da177e4SLinus Torvalds int change, max;
24181da177e4SLinus Torvalds unsigned int val;
24191da177e4SLinus Torvalds
24201da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
24211da177e4SLinus Torvalds return -EBUSY;
24221da177e4SLinus Torvalds
24231da177e4SLinus Torvalds switch (hdsp->io_type) {
24241da177e4SLinus Torvalds case Digiface:
24251da177e4SLinus Torvalds case H9652:
24261da177e4SLinus Torvalds max = 6;
24271da177e4SLinus Torvalds break;
24281da177e4SLinus Torvalds case Multiface:
24291da177e4SLinus Torvalds max = 4;
24301da177e4SLinus Torvalds break;
24311da177e4SLinus Torvalds case H9632:
24321da177e4SLinus Torvalds max = 3;
24331da177e4SLinus Torvalds break;
24341da177e4SLinus Torvalds default:
24351da177e4SLinus Torvalds return -EIO;
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds
24381da177e4SLinus Torvalds val = ucontrol->value.enumerated.item[0] % max;
24391da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
24401da177e4SLinus Torvalds change = (int)val != hdsp_pref_sync_ref(hdsp);
24411da177e4SLinus Torvalds hdsp_set_pref_sync_ref(hdsp, val);
24421da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
24431da177e4SLinus Torvalds return change;
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds
24461da177e4SLinus Torvalds #define HDSP_AUTOSYNC_REF(xname, xindex) \
244767ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
24481da177e4SLinus Torvalds .name = xname, \
24491da177e4SLinus Torvalds .index = xindex, \
24501da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ, \
24511da177e4SLinus Torvalds .info = snd_hdsp_info_autosync_ref, \
24521da177e4SLinus Torvalds .get = snd_hdsp_get_autosync_ref, \
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds
hdsp_autosync_ref(struct hdsp * hdsp)245555e957d8STakashi Iwai static int hdsp_autosync_ref(struct hdsp *hdsp)
24561da177e4SLinus Torvalds {
24571da177e4SLinus Torvalds /* This looks at the autosync selected sync reference */
24581da177e4SLinus Torvalds unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
24591da177e4SLinus Torvalds
24601da177e4SLinus Torvalds switch (status2 & HDSP_SelSyncRefMask) {
24611da177e4SLinus Torvalds case HDSP_SelSyncRef_WORD:
24621da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_WORD;
24631da177e4SLinus Torvalds case HDSP_SelSyncRef_ADAT_SYNC:
24641da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
24651da177e4SLinus Torvalds case HDSP_SelSyncRef_SPDIF:
24661da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_SPDIF;
24671da177e4SLinus Torvalds case HDSP_SelSyncRefMask:
24681da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_NONE;
24691da177e4SLinus Torvalds case HDSP_SelSyncRef_ADAT1:
24701da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_ADAT1;
24711da177e4SLinus Torvalds case HDSP_SelSyncRef_ADAT2:
24721da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_ADAT2;
24731da177e4SLinus Torvalds case HDSP_SelSyncRef_ADAT3:
24741da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_ADAT3;
24751da177e4SLinus Torvalds default:
24761da177e4SLinus Torvalds return HDSP_AUTOSYNC_FROM_WORD;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds return 0;
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds
snd_hdsp_info_autosync_ref(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)248155e957d8STakashi Iwai static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
24821da177e4SLinus Torvalds {
24838d678da9STakashi Iwai static const char * const texts[] = {
24848d678da9STakashi Iwai "Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
24858d678da9STakashi Iwai };
24861da177e4SLinus Torvalds
24878d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 7, texts);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds
snd_hdsp_get_autosync_ref(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)249055e957d8STakashi Iwai static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
24911da177e4SLinus Torvalds {
249255e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
24931da177e4SLinus Torvalds
24941da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
24951da177e4SLinus Torvalds return 0;
24961da177e4SLinus Torvalds }
24971da177e4SLinus Torvalds
24981da177e4SLinus Torvalds #define HDSP_PRECISE_POINTER(xname, xindex) \
249967ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
25001da177e4SLinus Torvalds .name = xname, \
25011da177e4SLinus Torvalds .index = xindex, \
25021da177e4SLinus Torvalds .info = snd_hdsp_info_precise_pointer, \
25031da177e4SLinus Torvalds .get = snd_hdsp_get_precise_pointer, \
25041da177e4SLinus Torvalds .put = snd_hdsp_put_precise_pointer \
25051da177e4SLinus Torvalds }
25061da177e4SLinus Torvalds
hdsp_set_precise_pointer(struct hdsp * hdsp,int precise)250755e957d8STakashi Iwai static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
25081da177e4SLinus Torvalds {
2509b0b98119STakashi Iwai if (precise)
25101da177e4SLinus Torvalds hdsp->precise_ptr = 1;
2511b0b98119STakashi Iwai else
25121da177e4SLinus Torvalds hdsp->precise_ptr = 0;
25131da177e4SLinus Torvalds return 0;
25141da177e4SLinus Torvalds }
25151da177e4SLinus Torvalds
2516a5ce8890STakashi Iwai #define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
25171da177e4SLinus Torvalds
snd_hdsp_get_precise_pointer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)251855e957d8STakashi Iwai static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25191da177e4SLinus Torvalds {
252055e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25211da177e4SLinus Torvalds
25221da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
25231da177e4SLinus Torvalds ucontrol->value.integer.value[0] = hdsp->precise_ptr;
25241da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
25251da177e4SLinus Torvalds return 0;
25261da177e4SLinus Torvalds }
25271da177e4SLinus Torvalds
snd_hdsp_put_precise_pointer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)252855e957d8STakashi Iwai static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25291da177e4SLinus Torvalds {
253055e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25311da177e4SLinus Torvalds int change;
25321da177e4SLinus Torvalds unsigned int val;
25331da177e4SLinus Torvalds
25341da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
25351da177e4SLinus Torvalds return -EBUSY;
25361da177e4SLinus Torvalds val = ucontrol->value.integer.value[0] & 1;
25371da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
25381da177e4SLinus Torvalds change = (int)val != hdsp->precise_ptr;
25391da177e4SLinus Torvalds hdsp_set_precise_pointer(hdsp, val);
25401da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
25411da177e4SLinus Torvalds return change;
25421da177e4SLinus Torvalds }
25431da177e4SLinus Torvalds
25444adab848STakashi Iwai #define HDSP_USE_MIDI_WORK(xname, xindex) \
254567ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
25461da177e4SLinus Torvalds .name = xname, \
25471da177e4SLinus Torvalds .index = xindex, \
25484adab848STakashi Iwai .info = snd_hdsp_info_use_midi_work, \
25494adab848STakashi Iwai .get = snd_hdsp_get_use_midi_work, \
25504adab848STakashi Iwai .put = snd_hdsp_put_use_midi_work \
25511da177e4SLinus Torvalds }
25521da177e4SLinus Torvalds
hdsp_set_use_midi_work(struct hdsp * hdsp,int use_work)25534adab848STakashi Iwai static int hdsp_set_use_midi_work(struct hdsp *hdsp, int use_work)
25541da177e4SLinus Torvalds {
25554adab848STakashi Iwai if (use_work)
25564adab848STakashi Iwai hdsp->use_midi_work = 1;
2557b0b98119STakashi Iwai else
25584adab848STakashi Iwai hdsp->use_midi_work = 0;
25591da177e4SLinus Torvalds return 0;
25601da177e4SLinus Torvalds }
25611da177e4SLinus Torvalds
25624adab848STakashi Iwai #define snd_hdsp_info_use_midi_work snd_ctl_boolean_mono_info
25631da177e4SLinus Torvalds
snd_hdsp_get_use_midi_work(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)25644adab848STakashi Iwai static int snd_hdsp_get_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25651da177e4SLinus Torvalds {
256655e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25671da177e4SLinus Torvalds
25681da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
25694adab848STakashi Iwai ucontrol->value.integer.value[0] = hdsp->use_midi_work;
25701da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
25711da177e4SLinus Torvalds return 0;
25721da177e4SLinus Torvalds }
25731da177e4SLinus Torvalds
snd_hdsp_put_use_midi_work(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)25744adab848STakashi Iwai static int snd_hdsp_put_use_midi_work(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
25751da177e4SLinus Torvalds {
257655e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
25771da177e4SLinus Torvalds int change;
25781da177e4SLinus Torvalds unsigned int val;
25791da177e4SLinus Torvalds
25801da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
25811da177e4SLinus Torvalds return -EBUSY;
25821da177e4SLinus Torvalds val = ucontrol->value.integer.value[0] & 1;
25831da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
25844adab848STakashi Iwai change = (int)val != hdsp->use_midi_work;
25854adab848STakashi Iwai hdsp_set_use_midi_work(hdsp, val);
25861da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
25871da177e4SLinus Torvalds return change;
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds
25901da177e4SLinus Torvalds #define HDSP_MIXER(xname, xindex) \
25911da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
25921da177e4SLinus Torvalds .name = xname, \
25931da177e4SLinus Torvalds .index = xindex, \
259467ed4161SClemens Ladisch .device = 0, \
25951da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
25961da177e4SLinus Torvalds SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
25971da177e4SLinus Torvalds .info = snd_hdsp_info_mixer, \
25981da177e4SLinus Torvalds .get = snd_hdsp_get_mixer, \
25991da177e4SLinus Torvalds .put = snd_hdsp_put_mixer \
26001da177e4SLinus Torvalds }
26011da177e4SLinus Torvalds
snd_hdsp_info_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)260255e957d8STakashi Iwai static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26031da177e4SLinus Torvalds {
26041da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
26051da177e4SLinus Torvalds uinfo->count = 3;
26061da177e4SLinus Torvalds uinfo->value.integer.min = 0;
26071da177e4SLinus Torvalds uinfo->value.integer.max = 65536;
26081da177e4SLinus Torvalds uinfo->value.integer.step = 1;
26091da177e4SLinus Torvalds return 0;
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds
snd_hdsp_get_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)261255e957d8STakashi Iwai static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26131da177e4SLinus Torvalds {
261455e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26151da177e4SLinus Torvalds int source;
26161da177e4SLinus Torvalds int destination;
26171da177e4SLinus Torvalds int addr;
26181da177e4SLinus Torvalds
26191da177e4SLinus Torvalds source = ucontrol->value.integer.value[0];
26201da177e4SLinus Torvalds destination = ucontrol->value.integer.value[1];
26211da177e4SLinus Torvalds
2622b0b98119STakashi Iwai if (source >= hdsp->max_channels)
26231da177e4SLinus Torvalds addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
2624b0b98119STakashi Iwai else
26251da177e4SLinus Torvalds addr = hdsp_input_to_output_key(hdsp,source, destination);
26261da177e4SLinus Torvalds
26271da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
26281da177e4SLinus Torvalds ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
26291da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
26301da177e4SLinus Torvalds return 0;
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds
snd_hdsp_put_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)263355e957d8STakashi Iwai static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26341da177e4SLinus Torvalds {
263555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26361da177e4SLinus Torvalds int change;
26371da177e4SLinus Torvalds int source;
26381da177e4SLinus Torvalds int destination;
26391da177e4SLinus Torvalds int gain;
26401da177e4SLinus Torvalds int addr;
26411da177e4SLinus Torvalds
26421da177e4SLinus Torvalds if (!snd_hdsp_use_is_exclusive(hdsp))
26431da177e4SLinus Torvalds return -EBUSY;
26441da177e4SLinus Torvalds
26451da177e4SLinus Torvalds source = ucontrol->value.integer.value[0];
26461da177e4SLinus Torvalds destination = ucontrol->value.integer.value[1];
26471da177e4SLinus Torvalds
2648b0b98119STakashi Iwai if (source >= hdsp->max_channels)
26491da177e4SLinus Torvalds addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
2650b0b98119STakashi Iwai else
26511da177e4SLinus Torvalds addr = hdsp_input_to_output_key(hdsp,source, destination);
26521da177e4SLinus Torvalds
26531da177e4SLinus Torvalds gain = ucontrol->value.integer.value[2];
26541da177e4SLinus Torvalds
26551da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
26561da177e4SLinus Torvalds change = gain != hdsp_read_gain(hdsp, addr);
26571da177e4SLinus Torvalds if (change)
26581da177e4SLinus Torvalds hdsp_write_gain(hdsp, addr, gain);
26591da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
26601da177e4SLinus Torvalds return change;
26611da177e4SLinus Torvalds }
26621da177e4SLinus Torvalds
26631da177e4SLinus Torvalds #define HDSP_WC_SYNC_CHECK(xname, xindex) \
266467ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
26651da177e4SLinus Torvalds .name = xname, \
26661da177e4SLinus Torvalds .index = xindex, \
26671da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
26681da177e4SLinus Torvalds .info = snd_hdsp_info_sync_check, \
26691da177e4SLinus Torvalds .get = snd_hdsp_get_wc_sync_check \
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds
snd_hdsp_info_sync_check(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)267255e957d8STakashi Iwai static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
26731da177e4SLinus Torvalds {
26748d678da9STakashi Iwai static const char * const texts[] = {"No Lock", "Lock", "Sync" };
26758d678da9STakashi Iwai
26768d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 3, texts);
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds
hdsp_wc_sync_check(struct hdsp * hdsp)267955e957d8STakashi Iwai static int hdsp_wc_sync_check(struct hdsp *hdsp)
26801da177e4SLinus Torvalds {
26811da177e4SLinus Torvalds int status2 = hdsp_read(hdsp, HDSP_status2Register);
26821da177e4SLinus Torvalds if (status2 & HDSP_wc_lock) {
2683b0b98119STakashi Iwai if (status2 & HDSP_wc_sync)
26841da177e4SLinus Torvalds return 2;
2685b0b98119STakashi Iwai else
26861da177e4SLinus Torvalds return 1;
2687b0b98119STakashi Iwai } else
26881da177e4SLinus Torvalds return 0;
26891da177e4SLinus Torvalds return 0;
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds
snd_hdsp_get_wc_sync_check(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)269255e957d8STakashi Iwai static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
26931da177e4SLinus Torvalds {
269455e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
26951da177e4SLinus Torvalds
26961da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
26971da177e4SLinus Torvalds return 0;
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds
27001da177e4SLinus Torvalds #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
270167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27021da177e4SLinus Torvalds .name = xname, \
27031da177e4SLinus Torvalds .index = xindex, \
27041da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27051da177e4SLinus Torvalds .info = snd_hdsp_info_sync_check, \
27061da177e4SLinus Torvalds .get = snd_hdsp_get_spdif_sync_check \
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds
hdsp_spdif_sync_check(struct hdsp * hdsp)270955e957d8STakashi Iwai static int hdsp_spdif_sync_check(struct hdsp *hdsp)
27101da177e4SLinus Torvalds {
27111da177e4SLinus Torvalds int status = hdsp_read(hdsp, HDSP_statusRegister);
2712b0b98119STakashi Iwai if (status & HDSP_SPDIFErrorFlag)
27131da177e4SLinus Torvalds return 0;
2714b0b98119STakashi Iwai else {
2715b0b98119STakashi Iwai if (status & HDSP_SPDIFSync)
27161da177e4SLinus Torvalds return 2;
2717b0b98119STakashi Iwai else
27181da177e4SLinus Torvalds return 1;
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds return 0;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds
snd_hdsp_get_spdif_sync_check(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)272355e957d8STakashi Iwai static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27241da177e4SLinus Torvalds {
272555e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27261da177e4SLinus Torvalds
27271da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
27281da177e4SLinus Torvalds return 0;
27291da177e4SLinus Torvalds }
27301da177e4SLinus Torvalds
27311da177e4SLinus Torvalds #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
273267ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27331da177e4SLinus Torvalds .name = xname, \
27341da177e4SLinus Torvalds .index = xindex, \
27351da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27361da177e4SLinus Torvalds .info = snd_hdsp_info_sync_check, \
27371da177e4SLinus Torvalds .get = snd_hdsp_get_adatsync_sync_check \
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds
hdsp_adatsync_sync_check(struct hdsp * hdsp)274055e957d8STakashi Iwai static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
27411da177e4SLinus Torvalds {
27421da177e4SLinus Torvalds int status = hdsp_read(hdsp, HDSP_statusRegister);
27431da177e4SLinus Torvalds if (status & HDSP_TimecodeLock) {
2744b0b98119STakashi Iwai if (status & HDSP_TimecodeSync)
27451da177e4SLinus Torvalds return 2;
2746b0b98119STakashi Iwai else
27471da177e4SLinus Torvalds return 1;
2748b0b98119STakashi Iwai } else
27491da177e4SLinus Torvalds return 0;
27501da177e4SLinus Torvalds }
27511da177e4SLinus Torvalds
snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)275255e957d8STakashi Iwai static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27531da177e4SLinus Torvalds {
275455e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27551da177e4SLinus Torvalds
27561da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
27571da177e4SLinus Torvalds return 0;
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds
27601da177e4SLinus Torvalds #define HDSP_ADAT_SYNC_CHECK \
276167ed4161SClemens Ladisch { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
27621da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
27631da177e4SLinus Torvalds .info = snd_hdsp_info_sync_check, \
27641da177e4SLinus Torvalds .get = snd_hdsp_get_adat_sync_check \
27651da177e4SLinus Torvalds }
27661da177e4SLinus Torvalds
hdsp_adat_sync_check(struct hdsp * hdsp,int idx)276755e957d8STakashi Iwai static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
27681da177e4SLinus Torvalds {
27691da177e4SLinus Torvalds int status = hdsp_read(hdsp, HDSP_statusRegister);
27701da177e4SLinus Torvalds
27711da177e4SLinus Torvalds if (status & (HDSP_Lock0>>idx)) {
2772b0b98119STakashi Iwai if (status & (HDSP_Sync0>>idx))
27731da177e4SLinus Torvalds return 2;
2774b0b98119STakashi Iwai else
27751da177e4SLinus Torvalds return 1;
2776b0b98119STakashi Iwai } else
27771da177e4SLinus Torvalds return 0;
27781da177e4SLinus Torvalds }
27791da177e4SLinus Torvalds
snd_hdsp_get_adat_sync_check(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)278055e957d8STakashi Iwai static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
27811da177e4SLinus Torvalds {
27821da177e4SLinus Torvalds int offset;
278355e957d8STakashi Iwai struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
27841da177e4SLinus Torvalds
27851da177e4SLinus Torvalds offset = ucontrol->id.index - 1;
27869e46aedaSDan Carpenter if (snd_BUG_ON(offset < 0))
27879e46aedaSDan Carpenter return -EINVAL;
27881da177e4SLinus Torvalds
27891da177e4SLinus Torvalds switch (hdsp->io_type) {
27901da177e4SLinus Torvalds case Digiface:
27911da177e4SLinus Torvalds case H9652:
27921da177e4SLinus Torvalds if (offset >= 3)
27931da177e4SLinus Torvalds return -EINVAL;
27941da177e4SLinus Torvalds break;
27951da177e4SLinus Torvalds case Multiface:
27961da177e4SLinus Torvalds case H9632:
27971da177e4SLinus Torvalds if (offset >= 1)
27981da177e4SLinus Torvalds return -EINVAL;
27991da177e4SLinus Torvalds break;
28001da177e4SLinus Torvalds default:
28011da177e4SLinus Torvalds return -EIO;
28021da177e4SLinus Torvalds }
28031da177e4SLinus Torvalds
28041da177e4SLinus Torvalds ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
28051da177e4SLinus Torvalds return 0;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds
2808e4b6088cSJulian Cable #define HDSP_DDS_OFFSET(xname, xindex) \
2809e4b6088cSJulian Cable { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2810e4b6088cSJulian Cable .name = xname, \
2811e4b6088cSJulian Cable .index = xindex, \
2812e4b6088cSJulian Cable .info = snd_hdsp_info_dds_offset, \
2813e4b6088cSJulian Cable .get = snd_hdsp_get_dds_offset, \
2814e4b6088cSJulian Cable .put = snd_hdsp_put_dds_offset \
2815e4b6088cSJulian Cable }
2816e4b6088cSJulian Cable
hdsp_dds_offset(struct hdsp * hdsp)2817e4b6088cSJulian Cable static int hdsp_dds_offset(struct hdsp *hdsp)
2818e4b6088cSJulian Cable {
2819e4b6088cSJulian Cable u64 n;
2820e4b6088cSJulian Cable unsigned int dds_value = hdsp->dds_value;
2821e4b6088cSJulian Cable int system_sample_rate = hdsp->system_sample_rate;
2822e4b6088cSJulian Cable
28232a3988f6STakashi Iwai if (!dds_value)
28242a3988f6STakashi Iwai return 0;
28252a3988f6STakashi Iwai
2826e4b6088cSJulian Cable n = DDS_NUMERATOR;
2827e4b6088cSJulian Cable /*
2828e4b6088cSJulian Cable * dds_value = n / rate
2829e4b6088cSJulian Cable * rate = n / dds_value
2830e4b6088cSJulian Cable */
28313f7440a6STakashi Iwai n = div_u64(n, dds_value);
2832e4b6088cSJulian Cable if (system_sample_rate >= 112000)
2833e4b6088cSJulian Cable n *= 4;
2834e4b6088cSJulian Cable else if (system_sample_rate >= 56000)
2835e4b6088cSJulian Cable n *= 2;
2836e4b6088cSJulian Cable return ((int)n) - system_sample_rate;
2837e4b6088cSJulian Cable }
2838e4b6088cSJulian Cable
hdsp_set_dds_offset(struct hdsp * hdsp,int offset_hz)2839e4b6088cSJulian Cable static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
2840e4b6088cSJulian Cable {
2841e4b6088cSJulian Cable int rate = hdsp->system_sample_rate + offset_hz;
2842e4b6088cSJulian Cable hdsp_set_dds_value(hdsp, rate);
2843e4b6088cSJulian Cable return 0;
2844e4b6088cSJulian Cable }
2845e4b6088cSJulian Cable
snd_hdsp_info_dds_offset(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2846e4b6088cSJulian Cable static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2847e4b6088cSJulian Cable {
2848e4b6088cSJulian Cable uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2849e4b6088cSJulian Cable uinfo->count = 1;
2850e4b6088cSJulian Cable uinfo->value.integer.min = -5000;
2851e4b6088cSJulian Cable uinfo->value.integer.max = 5000;
2852e4b6088cSJulian Cable return 0;
2853e4b6088cSJulian Cable }
2854e4b6088cSJulian Cable
snd_hdsp_get_dds_offset(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2855e4b6088cSJulian Cable static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2856e4b6088cSJulian Cable {
2857e4b6088cSJulian Cable struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2858e4b6088cSJulian Cable
2859eab3c4dbSTakashi Iwai ucontrol->value.integer.value[0] = hdsp_dds_offset(hdsp);
2860e4b6088cSJulian Cable return 0;
2861e4b6088cSJulian Cable }
2862e4b6088cSJulian Cable
snd_hdsp_put_dds_offset(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2863e4b6088cSJulian Cable static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2864e4b6088cSJulian Cable {
2865e4b6088cSJulian Cable struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
2866e4b6088cSJulian Cable int change;
2867e4b6088cSJulian Cable int val;
2868e4b6088cSJulian Cable
2869e4b6088cSJulian Cable if (!snd_hdsp_use_is_exclusive(hdsp))
2870e4b6088cSJulian Cable return -EBUSY;
2871eab3c4dbSTakashi Iwai val = ucontrol->value.integer.value[0];
2872e4b6088cSJulian Cable spin_lock_irq(&hdsp->lock);
2873e4b6088cSJulian Cable if (val != hdsp_dds_offset(hdsp))
2874e4b6088cSJulian Cable change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
2875e4b6088cSJulian Cable else
2876e4b6088cSJulian Cable change = 0;
2877e4b6088cSJulian Cable spin_unlock_irq(&hdsp->lock);
2878e4b6088cSJulian Cable return change;
2879e4b6088cSJulian Cable }
2880e4b6088cSJulian Cable
2881b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
28821da177e4SLinus Torvalds HDSP_DA_GAIN("DA Gain", 0),
28831da177e4SLinus Torvalds HDSP_AD_GAIN("AD Gain", 0),
28841da177e4SLinus Torvalds HDSP_PHONE_GAIN("Phones Gain", 0),
28854833c673SAdrian Knoth HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
2886e4b6088cSJulian Cable HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
28871da177e4SLinus Torvalds };
28881da177e4SLinus Torvalds
2889b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_controls[] = {
28901da177e4SLinus Torvalds {
28915549d549SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_PCM,
28921da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
28931da177e4SLinus Torvalds .info = snd_hdsp_control_spdif_info,
28941da177e4SLinus Torvalds .get = snd_hdsp_control_spdif_get,
28951da177e4SLinus Torvalds .put = snd_hdsp_control_spdif_put,
28961da177e4SLinus Torvalds },
28971da177e4SLinus Torvalds {
28981da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
28995549d549SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_PCM,
29001da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
29011da177e4SLinus Torvalds .info = snd_hdsp_control_spdif_stream_info,
29021da177e4SLinus Torvalds .get = snd_hdsp_control_spdif_stream_get,
29031da177e4SLinus Torvalds .put = snd_hdsp_control_spdif_stream_put,
29041da177e4SLinus Torvalds },
29051da177e4SLinus Torvalds {
29061da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ,
29075549d549SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_PCM,
29081da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
29091da177e4SLinus Torvalds .info = snd_hdsp_control_spdif_mask_info,
29101da177e4SLinus Torvalds .get = snd_hdsp_control_spdif_mask_get,
29111da177e4SLinus Torvalds .private_value = IEC958_AES0_NONAUDIO |
29121da177e4SLinus Torvalds IEC958_AES0_PROFESSIONAL |
29131da177e4SLinus Torvalds IEC958_AES0_CON_EMPHASIS,
29141da177e4SLinus Torvalds },
29151da177e4SLinus Torvalds {
29161da177e4SLinus Torvalds .access = SNDRV_CTL_ELEM_ACCESS_READ,
29175549d549SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_PCM,
29181da177e4SLinus Torvalds .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
29191da177e4SLinus Torvalds .info = snd_hdsp_control_spdif_mask_info,
29201da177e4SLinus Torvalds .get = snd_hdsp_control_spdif_mask_get,
29211da177e4SLinus Torvalds .private_value = IEC958_AES0_NONAUDIO |
29221da177e4SLinus Torvalds IEC958_AES0_PROFESSIONAL |
29231da177e4SLinus Torvalds IEC958_AES0_PRO_EMPHASIS,
29241da177e4SLinus Torvalds },
29251da177e4SLinus Torvalds HDSP_MIXER("Mixer", 0),
29261da177e4SLinus Torvalds HDSP_SPDIF_IN("IEC958 Input Connector", 0),
29274833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
29284833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
29294833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
29304833c673SAdrian Knoth HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
29311da177e4SLinus Torvalds /* 'Sample Clock Source' complies with the alsa control naming scheme */
29321da177e4SLinus Torvalds HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
2933e3ea4d89STakashi Iwai {
2934e3ea4d89STakashi Iwai .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2935e3ea4d89STakashi Iwai .name = "Sample Clock Source Locking",
2936e3ea4d89STakashi Iwai .info = snd_hdsp_info_clock_source_lock,
2937e3ea4d89STakashi Iwai .get = snd_hdsp_get_clock_source_lock,
2938e3ea4d89STakashi Iwai .put = snd_hdsp_put_clock_source_lock,
2939e3ea4d89STakashi Iwai },
29401da177e4SLinus Torvalds HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
29411da177e4SLinus Torvalds HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
29421da177e4SLinus Torvalds HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
29431da177e4SLinus Torvalds HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
29441da177e4SLinus Torvalds HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
29451da177e4SLinus Torvalds /* 'External Rate' complies with the alsa control naming scheme */
29461da177e4SLinus Torvalds HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
29471da177e4SLinus Torvalds HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
29481da177e4SLinus Torvalds HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
29491da177e4SLinus Torvalds HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
29504833c673SAdrian Knoth HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
29511da177e4SLinus Torvalds HDSP_PRECISE_POINTER("Precise Pointer", 0),
29524adab848STakashi Iwai HDSP_USE_MIDI_WORK("Use Midi Tasklet", 0),
29531da177e4SLinus Torvalds };
29541da177e4SLinus Torvalds
295528b26e15SFlorian Faber
hdsp_rpm_input12(struct hdsp * hdsp)295628b26e15SFlorian Faber static int hdsp_rpm_input12(struct hdsp *hdsp)
295728b26e15SFlorian Faber {
295828b26e15SFlorian Faber switch (hdsp->control_register & HDSP_RPM_Inp12) {
295928b26e15SFlorian Faber case HDSP_RPM_Inp12_Phon_6dB:
296028b26e15SFlorian Faber return 0;
296128b26e15SFlorian Faber case HDSP_RPM_Inp12_Phon_n6dB:
296228b26e15SFlorian Faber return 2;
296328b26e15SFlorian Faber case HDSP_RPM_Inp12_Line_0dB:
296428b26e15SFlorian Faber return 3;
296528b26e15SFlorian Faber case HDSP_RPM_Inp12_Line_n6dB:
296628b26e15SFlorian Faber return 4;
296728b26e15SFlorian Faber }
296828b26e15SFlorian Faber return 1;
296928b26e15SFlorian Faber }
297028b26e15SFlorian Faber
297128b26e15SFlorian Faber
snd_hdsp_get_rpm_input12(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)297228b26e15SFlorian Faber static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
297328b26e15SFlorian Faber {
297428b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
297528b26e15SFlorian Faber
297628b26e15SFlorian Faber ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
297728b26e15SFlorian Faber return 0;
297828b26e15SFlorian Faber }
297928b26e15SFlorian Faber
298028b26e15SFlorian Faber
hdsp_set_rpm_input12(struct hdsp * hdsp,int mode)298128b26e15SFlorian Faber static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
298228b26e15SFlorian Faber {
298328b26e15SFlorian Faber hdsp->control_register &= ~HDSP_RPM_Inp12;
298428b26e15SFlorian Faber switch (mode) {
298528b26e15SFlorian Faber case 0:
298628b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
298728b26e15SFlorian Faber break;
298828b26e15SFlorian Faber case 1:
298928b26e15SFlorian Faber break;
299028b26e15SFlorian Faber case 2:
299128b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
299228b26e15SFlorian Faber break;
299328b26e15SFlorian Faber case 3:
299428b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
299528b26e15SFlorian Faber break;
299628b26e15SFlorian Faber case 4:
299728b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
299828b26e15SFlorian Faber break;
299928b26e15SFlorian Faber default:
300028b26e15SFlorian Faber return -1;
300128b26e15SFlorian Faber }
300228b26e15SFlorian Faber
300328b26e15SFlorian Faber hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
300428b26e15SFlorian Faber return 0;
300528b26e15SFlorian Faber }
300628b26e15SFlorian Faber
300728b26e15SFlorian Faber
snd_hdsp_put_rpm_input12(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)300828b26e15SFlorian Faber static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
300928b26e15SFlorian Faber {
301028b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
301128b26e15SFlorian Faber int change;
301228b26e15SFlorian Faber int val;
301328b26e15SFlorian Faber
301428b26e15SFlorian Faber if (!snd_hdsp_use_is_exclusive(hdsp))
301528b26e15SFlorian Faber return -EBUSY;
301628b26e15SFlorian Faber val = ucontrol->value.enumerated.item[0];
301728b26e15SFlorian Faber if (val < 0)
301828b26e15SFlorian Faber val = 0;
301928b26e15SFlorian Faber if (val > 4)
302028b26e15SFlorian Faber val = 4;
302128b26e15SFlorian Faber spin_lock_irq(&hdsp->lock);
302228b26e15SFlorian Faber if (val != hdsp_rpm_input12(hdsp))
302328b26e15SFlorian Faber change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
302428b26e15SFlorian Faber else
302528b26e15SFlorian Faber change = 0;
302628b26e15SFlorian Faber spin_unlock_irq(&hdsp->lock);
302728b26e15SFlorian Faber return change;
302828b26e15SFlorian Faber }
302928b26e15SFlorian Faber
303028b26e15SFlorian Faber
snd_hdsp_info_rpm_input(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)303128b26e15SFlorian Faber static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
303228b26e15SFlorian Faber {
30338d678da9STakashi Iwai static const char * const texts[] = {
30348d678da9STakashi Iwai "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
30358d678da9STakashi Iwai };
303628b26e15SFlorian Faber
30378d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 5, texts);
303828b26e15SFlorian Faber }
303928b26e15SFlorian Faber
304028b26e15SFlorian Faber
hdsp_rpm_input34(struct hdsp * hdsp)304128b26e15SFlorian Faber static int hdsp_rpm_input34(struct hdsp *hdsp)
304228b26e15SFlorian Faber {
304328b26e15SFlorian Faber switch (hdsp->control_register & HDSP_RPM_Inp34) {
304428b26e15SFlorian Faber case HDSP_RPM_Inp34_Phon_6dB:
304528b26e15SFlorian Faber return 0;
304628b26e15SFlorian Faber case HDSP_RPM_Inp34_Phon_n6dB:
304728b26e15SFlorian Faber return 2;
304828b26e15SFlorian Faber case HDSP_RPM_Inp34_Line_0dB:
304928b26e15SFlorian Faber return 3;
305028b26e15SFlorian Faber case HDSP_RPM_Inp34_Line_n6dB:
305128b26e15SFlorian Faber return 4;
305228b26e15SFlorian Faber }
305328b26e15SFlorian Faber return 1;
305428b26e15SFlorian Faber }
305528b26e15SFlorian Faber
305628b26e15SFlorian Faber
snd_hdsp_get_rpm_input34(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)305728b26e15SFlorian Faber static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
305828b26e15SFlorian Faber {
305928b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
306028b26e15SFlorian Faber
306128b26e15SFlorian Faber ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
306228b26e15SFlorian Faber return 0;
306328b26e15SFlorian Faber }
306428b26e15SFlorian Faber
306528b26e15SFlorian Faber
hdsp_set_rpm_input34(struct hdsp * hdsp,int mode)306628b26e15SFlorian Faber static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
306728b26e15SFlorian Faber {
306828b26e15SFlorian Faber hdsp->control_register &= ~HDSP_RPM_Inp34;
306928b26e15SFlorian Faber switch (mode) {
307028b26e15SFlorian Faber case 0:
307128b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
307228b26e15SFlorian Faber break;
307328b26e15SFlorian Faber case 1:
307428b26e15SFlorian Faber break;
307528b26e15SFlorian Faber case 2:
307628b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
307728b26e15SFlorian Faber break;
307828b26e15SFlorian Faber case 3:
307928b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
308028b26e15SFlorian Faber break;
308128b26e15SFlorian Faber case 4:
308228b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
308328b26e15SFlorian Faber break;
308428b26e15SFlorian Faber default:
308528b26e15SFlorian Faber return -1;
308628b26e15SFlorian Faber }
308728b26e15SFlorian Faber
308828b26e15SFlorian Faber hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
308928b26e15SFlorian Faber return 0;
309028b26e15SFlorian Faber }
309128b26e15SFlorian Faber
309228b26e15SFlorian Faber
snd_hdsp_put_rpm_input34(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)309328b26e15SFlorian Faber static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
309428b26e15SFlorian Faber {
309528b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
309628b26e15SFlorian Faber int change;
309728b26e15SFlorian Faber int val;
309828b26e15SFlorian Faber
309928b26e15SFlorian Faber if (!snd_hdsp_use_is_exclusive(hdsp))
310028b26e15SFlorian Faber return -EBUSY;
310128b26e15SFlorian Faber val = ucontrol->value.enumerated.item[0];
310228b26e15SFlorian Faber if (val < 0)
310328b26e15SFlorian Faber val = 0;
310428b26e15SFlorian Faber if (val > 4)
310528b26e15SFlorian Faber val = 4;
310628b26e15SFlorian Faber spin_lock_irq(&hdsp->lock);
310728b26e15SFlorian Faber if (val != hdsp_rpm_input34(hdsp))
310828b26e15SFlorian Faber change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
310928b26e15SFlorian Faber else
311028b26e15SFlorian Faber change = 0;
311128b26e15SFlorian Faber spin_unlock_irq(&hdsp->lock);
311228b26e15SFlorian Faber return change;
311328b26e15SFlorian Faber }
311428b26e15SFlorian Faber
311528b26e15SFlorian Faber
311628b26e15SFlorian Faber /* RPM Bypass switch */
hdsp_rpm_bypass(struct hdsp * hdsp)311728b26e15SFlorian Faber static int hdsp_rpm_bypass(struct hdsp *hdsp)
311828b26e15SFlorian Faber {
311928b26e15SFlorian Faber return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
312028b26e15SFlorian Faber }
312128b26e15SFlorian Faber
312228b26e15SFlorian Faber
snd_hdsp_get_rpm_bypass(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)312328b26e15SFlorian Faber static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
312428b26e15SFlorian Faber {
312528b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
312628b26e15SFlorian Faber
312728b26e15SFlorian Faber ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
312828b26e15SFlorian Faber return 0;
312928b26e15SFlorian Faber }
313028b26e15SFlorian Faber
313128b26e15SFlorian Faber
hdsp_set_rpm_bypass(struct hdsp * hdsp,int on)313228b26e15SFlorian Faber static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
313328b26e15SFlorian Faber {
313428b26e15SFlorian Faber if (on)
313528b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Bypass;
313628b26e15SFlorian Faber else
313728b26e15SFlorian Faber hdsp->control_register &= ~HDSP_RPM_Bypass;
313828b26e15SFlorian Faber hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
313928b26e15SFlorian Faber return 0;
314028b26e15SFlorian Faber }
314128b26e15SFlorian Faber
314228b26e15SFlorian Faber
snd_hdsp_put_rpm_bypass(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)314328b26e15SFlorian Faber static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
314428b26e15SFlorian Faber {
314528b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
314628b26e15SFlorian Faber int change;
314728b26e15SFlorian Faber unsigned int val;
314828b26e15SFlorian Faber
314928b26e15SFlorian Faber if (!snd_hdsp_use_is_exclusive(hdsp))
315028b26e15SFlorian Faber return -EBUSY;
315128b26e15SFlorian Faber val = ucontrol->value.integer.value[0] & 1;
315228b26e15SFlorian Faber spin_lock_irq(&hdsp->lock);
315328b26e15SFlorian Faber change = (int)val != hdsp_rpm_bypass(hdsp);
315428b26e15SFlorian Faber hdsp_set_rpm_bypass(hdsp, val);
315528b26e15SFlorian Faber spin_unlock_irq(&hdsp->lock);
315628b26e15SFlorian Faber return change;
315728b26e15SFlorian Faber }
315828b26e15SFlorian Faber
315928b26e15SFlorian Faber
snd_hdsp_info_rpm_bypass(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)316028b26e15SFlorian Faber static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
316128b26e15SFlorian Faber {
31628d678da9STakashi Iwai static const char * const texts[] = {"On", "Off"};
316328b26e15SFlorian Faber
31648d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 2, texts);
316528b26e15SFlorian Faber }
316628b26e15SFlorian Faber
316728b26e15SFlorian Faber
316828b26e15SFlorian Faber /* RPM Disconnect switch */
hdsp_rpm_disconnect(struct hdsp * hdsp)316928b26e15SFlorian Faber static int hdsp_rpm_disconnect(struct hdsp *hdsp)
317028b26e15SFlorian Faber {
317128b26e15SFlorian Faber return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
317228b26e15SFlorian Faber }
317328b26e15SFlorian Faber
317428b26e15SFlorian Faber
snd_hdsp_get_rpm_disconnect(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)317528b26e15SFlorian Faber static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
317628b26e15SFlorian Faber {
317728b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
317828b26e15SFlorian Faber
317928b26e15SFlorian Faber ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
318028b26e15SFlorian Faber return 0;
318128b26e15SFlorian Faber }
318228b26e15SFlorian Faber
318328b26e15SFlorian Faber
hdsp_set_rpm_disconnect(struct hdsp * hdsp,int on)318428b26e15SFlorian Faber static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
318528b26e15SFlorian Faber {
318628b26e15SFlorian Faber if (on)
318728b26e15SFlorian Faber hdsp->control_register |= HDSP_RPM_Disconnect;
318828b26e15SFlorian Faber else
318928b26e15SFlorian Faber hdsp->control_register &= ~HDSP_RPM_Disconnect;
319028b26e15SFlorian Faber hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
319128b26e15SFlorian Faber return 0;
319228b26e15SFlorian Faber }
319328b26e15SFlorian Faber
319428b26e15SFlorian Faber
snd_hdsp_put_rpm_disconnect(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)319528b26e15SFlorian Faber static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
319628b26e15SFlorian Faber {
319728b26e15SFlorian Faber struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
319828b26e15SFlorian Faber int change;
319928b26e15SFlorian Faber unsigned int val;
320028b26e15SFlorian Faber
320128b26e15SFlorian Faber if (!snd_hdsp_use_is_exclusive(hdsp))
320228b26e15SFlorian Faber return -EBUSY;
320328b26e15SFlorian Faber val = ucontrol->value.integer.value[0] & 1;
320428b26e15SFlorian Faber spin_lock_irq(&hdsp->lock);
320528b26e15SFlorian Faber change = (int)val != hdsp_rpm_disconnect(hdsp);
320628b26e15SFlorian Faber hdsp_set_rpm_disconnect(hdsp, val);
320728b26e15SFlorian Faber spin_unlock_irq(&hdsp->lock);
320828b26e15SFlorian Faber return change;
320928b26e15SFlorian Faber }
321028b26e15SFlorian Faber
snd_hdsp_info_rpm_disconnect(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)321128b26e15SFlorian Faber static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
321228b26e15SFlorian Faber {
32138d678da9STakashi Iwai static const char * const texts[] = {"On", "Off"};
321428b26e15SFlorian Faber
32158d678da9STakashi Iwai return snd_ctl_enum_info(uinfo, 1, 2, texts);
321628b26e15SFlorian Faber }
321728b26e15SFlorian Faber
3218b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
321928b26e15SFlorian Faber {
322028b26e15SFlorian Faber .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
322128b26e15SFlorian Faber .name = "RPM Bypass",
322228b26e15SFlorian Faber .get = snd_hdsp_get_rpm_bypass,
322328b26e15SFlorian Faber .put = snd_hdsp_put_rpm_bypass,
322428b26e15SFlorian Faber .info = snd_hdsp_info_rpm_bypass
322528b26e15SFlorian Faber },
322628b26e15SFlorian Faber {
322728b26e15SFlorian Faber .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
322828b26e15SFlorian Faber .name = "RPM Disconnect",
322928b26e15SFlorian Faber .get = snd_hdsp_get_rpm_disconnect,
323028b26e15SFlorian Faber .put = snd_hdsp_put_rpm_disconnect,
323128b26e15SFlorian Faber .info = snd_hdsp_info_rpm_disconnect
323228b26e15SFlorian Faber },
323328b26e15SFlorian Faber {
323428b26e15SFlorian Faber .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
323528b26e15SFlorian Faber .name = "Input 1/2",
323628b26e15SFlorian Faber .get = snd_hdsp_get_rpm_input12,
323728b26e15SFlorian Faber .put = snd_hdsp_put_rpm_input12,
323828b26e15SFlorian Faber .info = snd_hdsp_info_rpm_input
323928b26e15SFlorian Faber },
324028b26e15SFlorian Faber {
324128b26e15SFlorian Faber .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
324228b26e15SFlorian Faber .name = "Input 3/4",
324328b26e15SFlorian Faber .get = snd_hdsp_get_rpm_input34,
324428b26e15SFlorian Faber .put = snd_hdsp_put_rpm_input34,
324528b26e15SFlorian Faber .info = snd_hdsp_info_rpm_input
324628b26e15SFlorian Faber },
324728b26e15SFlorian Faber HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
324828b26e15SFlorian Faber HDSP_MIXER("Mixer", 0)
324928b26e15SFlorian Faber };
325028b26e15SFlorian Faber
3251b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_hdsp_96xx_aeb =
32524833c673SAdrian Knoth HDSP_TOGGLE_SETTING("Analog Extension Board",
32534833c673SAdrian Knoth HDSP_AnalogExtensionBoard);
325455e957d8STakashi Iwai static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
32551da177e4SLinus Torvalds
3256da2a040eSJasmin Fazlic
hdsp_loopback_get(struct hdsp * const hdsp,const u8 channel)3257da2a040eSJasmin Fazlic static bool hdsp_loopback_get(struct hdsp *const hdsp, const u8 channel)
3258da2a040eSJasmin Fazlic {
3259da2a040eSJasmin Fazlic return hdsp->io_loopback & (1 << channel);
3260da2a040eSJasmin Fazlic }
3261da2a040eSJasmin Fazlic
hdsp_loopback_set(struct hdsp * const hdsp,const u8 channel,const bool enable)3262da2a040eSJasmin Fazlic static int hdsp_loopback_set(struct hdsp *const hdsp, const u8 channel, const bool enable)
3263da2a040eSJasmin Fazlic {
3264da2a040eSJasmin Fazlic if (hdsp_loopback_get(hdsp, channel) == enable)
3265da2a040eSJasmin Fazlic return 0;
3266da2a040eSJasmin Fazlic
3267da2a040eSJasmin Fazlic hdsp->io_loopback ^= (1 << channel);
3268da2a040eSJasmin Fazlic
3269da2a040eSJasmin Fazlic hdsp_write(hdsp, HDSP_inputEnable + (4 * (hdsp->max_channels + channel)), enable);
3270da2a040eSJasmin Fazlic
3271da2a040eSJasmin Fazlic return 1;
3272da2a040eSJasmin Fazlic }
3273da2a040eSJasmin Fazlic
snd_hdsp_loopback_get(struct snd_kcontrol * const kcontrol,struct snd_ctl_elem_value * const ucontrol)3274da2a040eSJasmin Fazlic static int snd_hdsp_loopback_get(struct snd_kcontrol *const kcontrol,
3275da2a040eSJasmin Fazlic struct snd_ctl_elem_value *const ucontrol)
3276da2a040eSJasmin Fazlic {
3277da2a040eSJasmin Fazlic struct hdsp *const hdsp = snd_kcontrol_chip(kcontrol);
3278da2a040eSJasmin Fazlic const u8 channel = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
3279da2a040eSJasmin Fazlic
3280da2a040eSJasmin Fazlic if (channel >= hdsp->max_channels)
3281da2a040eSJasmin Fazlic return -ENOENT;
3282da2a040eSJasmin Fazlic
3283da2a040eSJasmin Fazlic ucontrol->value.integer.value[0] = hdsp_loopback_get(hdsp, channel);
3284da2a040eSJasmin Fazlic
3285da2a040eSJasmin Fazlic return 0;
3286da2a040eSJasmin Fazlic }
3287da2a040eSJasmin Fazlic
snd_hdsp_loopback_put(struct snd_kcontrol * const kcontrol,struct snd_ctl_elem_value * const ucontrol)3288da2a040eSJasmin Fazlic static int snd_hdsp_loopback_put(struct snd_kcontrol *const kcontrol,
3289da2a040eSJasmin Fazlic struct snd_ctl_elem_value *const ucontrol)
3290da2a040eSJasmin Fazlic {
3291da2a040eSJasmin Fazlic struct hdsp *const hdsp = snd_kcontrol_chip(kcontrol);
3292da2a040eSJasmin Fazlic const u8 channel = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
3293da2a040eSJasmin Fazlic const bool enable = ucontrol->value.integer.value[0] & 1;
3294da2a040eSJasmin Fazlic
3295da2a040eSJasmin Fazlic if (channel >= hdsp->max_channels)
3296da2a040eSJasmin Fazlic return -ENOENT;
3297da2a040eSJasmin Fazlic
3298da2a040eSJasmin Fazlic return hdsp_loopback_set(hdsp, channel, enable);
3299da2a040eSJasmin Fazlic }
3300da2a040eSJasmin Fazlic
3301da2a040eSJasmin Fazlic static struct snd_kcontrol_new snd_hdsp_loopback_control = {
3302da2a040eSJasmin Fazlic .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
3303da2a040eSJasmin Fazlic .name = "Output Loopback",
3304da2a040eSJasmin Fazlic .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3305da2a040eSJasmin Fazlic .info = snd_ctl_boolean_mono_info,
3306da2a040eSJasmin Fazlic .get = snd_hdsp_loopback_get,
3307da2a040eSJasmin Fazlic .put = snd_hdsp_loopback_put
3308da2a040eSJasmin Fazlic };
3309da2a040eSJasmin Fazlic
snd_hdsp_create_controls(struct snd_card * card,struct hdsp * hdsp)331055e957d8STakashi Iwai static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
33111da177e4SLinus Torvalds {
33121da177e4SLinus Torvalds unsigned int idx;
33131da177e4SLinus Torvalds int err;
331455e957d8STakashi Iwai struct snd_kcontrol *kctl;
33151da177e4SLinus Torvalds
331628b26e15SFlorian Faber if (hdsp->io_type == RPM) {
331728b26e15SFlorian Faber /* RPM Bypass, Disconnect and Input switches */
331828b26e15SFlorian Faber for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
3319d06130dcSColin Ian King err = snd_ctl_add(card, snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
332028b26e15SFlorian Faber if (err < 0)
332128b26e15SFlorian Faber return err;
332228b26e15SFlorian Faber }
332328b26e15SFlorian Faber return 0;
332428b26e15SFlorian Faber }
332528b26e15SFlorian Faber
33261da177e4SLinus Torvalds for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
332766c8f759STakashi Iwai kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp);
332866c8f759STakashi Iwai err = snd_ctl_add(card, kctl);
332966c8f759STakashi Iwai if (err < 0)
33301da177e4SLinus Torvalds return err;
33311da177e4SLinus Torvalds if (idx == 1) /* IEC958 (S/PDIF) Stream */
33321da177e4SLinus Torvalds hdsp->spdif_ctl = kctl;
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds
33351da177e4SLinus Torvalds /* ADAT SyncCheck status */
33361da177e4SLinus Torvalds snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
33371da177e4SLinus Torvalds snd_hdsp_adat_sync_check.index = 1;
333866c8f759STakashi Iwai kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp);
333966c8f759STakashi Iwai err = snd_ctl_add(card, kctl);
334066c8f759STakashi Iwai if (err < 0)
33411da177e4SLinus Torvalds return err;
33421da177e4SLinus Torvalds if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
33431da177e4SLinus Torvalds for (idx = 1; idx < 3; ++idx) {
33441da177e4SLinus Torvalds snd_hdsp_adat_sync_check.index = idx+1;
334566c8f759STakashi Iwai kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp);
334666c8f759STakashi Iwai err = snd_ctl_add(card, kctl);
334766c8f759STakashi Iwai if (err < 0)
33481da177e4SLinus Torvalds return err;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds
33521da177e4SLinus Torvalds /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
33531da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
33541da177e4SLinus Torvalds for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
335566c8f759STakashi Iwai kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp);
335666c8f759STakashi Iwai err = snd_ctl_add(card, kctl);
335766c8f759STakashi Iwai if (err < 0)
33581da177e4SLinus Torvalds return err;
33591da177e4SLinus Torvalds }
33601da177e4SLinus Torvalds }
33611da177e4SLinus Torvalds
3362da2a040eSJasmin Fazlic /* Output loopback controls for H9632 cards */
3363da2a040eSJasmin Fazlic if (hdsp->io_type == H9632) {
3364da2a040eSJasmin Fazlic snd_hdsp_loopback_control.count = hdsp->max_channels;
3365da2a040eSJasmin Fazlic kctl = snd_ctl_new1(&snd_hdsp_loopback_control, hdsp);
3366da2a040eSJasmin Fazlic if (kctl == NULL)
3367da2a040eSJasmin Fazlic return -ENOMEM;
3368da2a040eSJasmin Fazlic err = snd_ctl_add(card, kctl);
3369da2a040eSJasmin Fazlic if (err < 0)
3370da2a040eSJasmin Fazlic return err;
3371da2a040eSJasmin Fazlic }
3372da2a040eSJasmin Fazlic
33731da177e4SLinus Torvalds /* AEB control for H96xx card */
33741da177e4SLinus Torvalds if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
337566c8f759STakashi Iwai kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp);
337666c8f759STakashi Iwai err = snd_ctl_add(card, kctl);
337766c8f759STakashi Iwai if (err < 0)
33781da177e4SLinus Torvalds return err;
33791da177e4SLinus Torvalds }
33801da177e4SLinus Torvalds
33811da177e4SLinus Torvalds return 0;
33821da177e4SLinus Torvalds }
33831da177e4SLinus Torvalds
33841da177e4SLinus Torvalds /*------------------------------------------------------------
33851da177e4SLinus Torvalds /proc interface
33861da177e4SLinus Torvalds ------------------------------------------------------------*/
33871da177e4SLinus Torvalds
33881da177e4SLinus Torvalds static void
snd_hdsp_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)338955e957d8STakashi Iwai snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
33901da177e4SLinus Torvalds {
33919fe856e4SJoe Perches struct hdsp *hdsp = entry->private_data;
33921da177e4SLinus Torvalds unsigned int status;
33931da177e4SLinus Torvalds unsigned int status2;
33941da177e4SLinus Torvalds char *pref_sync_ref;
33951da177e4SLinus Torvalds char *autosync_ref;
33961da177e4SLinus Torvalds char *system_clock_mode;
33971da177e4SLinus Torvalds char *clock_source;
33981da177e4SLinus Torvalds int x;
33991da177e4SLinus Torvalds
3400c18bc9b9STim Blechmann status = hdsp_read(hdsp, HDSP_statusRegister);
3401c18bc9b9STim Blechmann status2 = hdsp_read(hdsp, HDSP_status2Register);
3402c18bc9b9STim Blechmann
3403c18bc9b9STim Blechmann snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
3404c18bc9b9STim Blechmann hdsp->card->number + 1);
3405c18bc9b9STim Blechmann snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
3406c18bc9b9STim Blechmann hdsp->capture_buffer, hdsp->playback_buffer);
3407c18bc9b9STim Blechmann snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
3408c18bc9b9STim Blechmann hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
3409c18bc9b9STim Blechmann snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
3410c18bc9b9STim Blechmann snd_iprintf(buffer, "Control2 register: 0x%x\n",
3411c18bc9b9STim Blechmann hdsp->control2_register);
3412c18bc9b9STim Blechmann snd_iprintf(buffer, "Status register: 0x%x\n", status);
3413c18bc9b9STim Blechmann snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
3414c18bc9b9STim Blechmann
3415ecb594e6SRemy Bruno if (hdsp_check_for_iobox(hdsp)) {
3416c18bc9b9STim Blechmann snd_iprintf(buffer, "No I/O box connected.\n"
3417c18bc9b9STim Blechmann "Please connect one and upload firmware.\n");
34181da177e4SLinus Torvalds return;
3419ecb594e6SRemy Bruno }
34201da177e4SLinus Torvalds
3421b0b98119STakashi Iwai if (hdsp_check_for_firmware(hdsp, 0)) {
34221da177e4SLinus Torvalds if (hdsp->state & HDSP_FirmwareCached) {
34231da177e4SLinus Torvalds if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
3424c18bc9b9STim Blechmann snd_iprintf(buffer, "Firmware loading from "
3425c18bc9b9STim Blechmann "cache failed, "
3426c18bc9b9STim Blechmann "please upload manually.\n");
34271da177e4SLinus Torvalds return;
34281da177e4SLinus Torvalds }
34291da177e4SLinus Torvalds } else {
34300cc62972SColin Ian King int err;
34310cc62972SColin Ian King
3432311e70a4STakashi Iwai err = hdsp_request_fw_loader(hdsp);
3433311e70a4STakashi Iwai if (err < 0) {
3434311e70a4STakashi Iwai snd_iprintf(buffer,
3435311e70a4STakashi Iwai "No firmware loaded nor cached, "
3436311e70a4STakashi Iwai "please upload firmware.\n");
34371da177e4SLinus Torvalds return;
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds }
3440311e70a4STakashi Iwai }
34411da177e4SLinus Torvalds
34421da177e4SLinus Torvalds snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
34431da177e4SLinus Torvalds snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
34441da177e4SLinus Torvalds snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
34451da177e4SLinus Torvalds snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
34461da177e4SLinus Torvalds snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
34474adab848STakashi Iwai snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_work ? "on" : "off");
34481da177e4SLinus Torvalds
34491da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
34501da177e4SLinus Torvalds
34511da177e4SLinus Torvalds x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
34521da177e4SLinus Torvalds
34531da177e4SLinus Torvalds snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
34541da177e4SLinus Torvalds snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
34551da177e4SLinus Torvalds snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
34561da177e4SLinus Torvalds snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
34571da177e4SLinus Torvalds
34581da177e4SLinus Torvalds snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
34591da177e4SLinus Torvalds
34601da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
34611da177e4SLinus Torvalds
34621da177e4SLinus Torvalds switch (hdsp_clock_source(hdsp)) {
34631da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_AUTOSYNC:
34641da177e4SLinus Torvalds clock_source = "AutoSync";
34651da177e4SLinus Torvalds break;
34661da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
34671da177e4SLinus Torvalds clock_source = "Internal 32 kHz";
34681da177e4SLinus Torvalds break;
34691da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
34701da177e4SLinus Torvalds clock_source = "Internal 44.1 kHz";
34711da177e4SLinus Torvalds break;
34721da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
34731da177e4SLinus Torvalds clock_source = "Internal 48 kHz";
34741da177e4SLinus Torvalds break;
34751da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
34761da177e4SLinus Torvalds clock_source = "Internal 64 kHz";
34771da177e4SLinus Torvalds break;
34781da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
34791da177e4SLinus Torvalds clock_source = "Internal 88.2 kHz";
34801da177e4SLinus Torvalds break;
34811da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
34821da177e4SLinus Torvalds clock_source = "Internal 96 kHz";
34831da177e4SLinus Torvalds break;
34841da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
34851da177e4SLinus Torvalds clock_source = "Internal 128 kHz";
34861da177e4SLinus Torvalds break;
34871da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
34881da177e4SLinus Torvalds clock_source = "Internal 176.4 kHz";
34891da177e4SLinus Torvalds break;
34901da177e4SLinus Torvalds case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
34911da177e4SLinus Torvalds clock_source = "Internal 192 kHz";
34921da177e4SLinus Torvalds break;
34931da177e4SLinus Torvalds default:
34941da177e4SLinus Torvalds clock_source = "Error";
34951da177e4SLinus Torvalds }
34961da177e4SLinus Torvalds snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
34971da177e4SLinus Torvalds
3498b0b98119STakashi Iwai if (hdsp_system_clock_mode(hdsp))
34991da177e4SLinus Torvalds system_clock_mode = "Slave";
3500b0b98119STakashi Iwai else
35011da177e4SLinus Torvalds system_clock_mode = "Master";
35021da177e4SLinus Torvalds
35031da177e4SLinus Torvalds switch (hdsp_pref_sync_ref (hdsp)) {
35041da177e4SLinus Torvalds case HDSP_SYNC_FROM_WORD:
35051da177e4SLinus Torvalds pref_sync_ref = "Word Clock";
35061da177e4SLinus Torvalds break;
35071da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT_SYNC:
35081da177e4SLinus Torvalds pref_sync_ref = "ADAT Sync";
35091da177e4SLinus Torvalds break;
35101da177e4SLinus Torvalds case HDSP_SYNC_FROM_SPDIF:
35111da177e4SLinus Torvalds pref_sync_ref = "SPDIF";
35121da177e4SLinus Torvalds break;
35131da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT1:
35141da177e4SLinus Torvalds pref_sync_ref = "ADAT1";
35151da177e4SLinus Torvalds break;
35161da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT2:
35171da177e4SLinus Torvalds pref_sync_ref = "ADAT2";
35181da177e4SLinus Torvalds break;
35191da177e4SLinus Torvalds case HDSP_SYNC_FROM_ADAT3:
35201da177e4SLinus Torvalds pref_sync_ref = "ADAT3";
35211da177e4SLinus Torvalds break;
35221da177e4SLinus Torvalds default:
35231da177e4SLinus Torvalds pref_sync_ref = "Word Clock";
35241da177e4SLinus Torvalds break;
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
35271da177e4SLinus Torvalds
35281da177e4SLinus Torvalds switch (hdsp_autosync_ref (hdsp)) {
35291da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_WORD:
35301da177e4SLinus Torvalds autosync_ref = "Word Clock";
35311da177e4SLinus Torvalds break;
35321da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
35331da177e4SLinus Torvalds autosync_ref = "ADAT Sync";
35341da177e4SLinus Torvalds break;
35351da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_SPDIF:
35361da177e4SLinus Torvalds autosync_ref = "SPDIF";
35371da177e4SLinus Torvalds break;
35381da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_NONE:
35391da177e4SLinus Torvalds autosync_ref = "None";
35401da177e4SLinus Torvalds break;
35411da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_ADAT1:
35421da177e4SLinus Torvalds autosync_ref = "ADAT1";
35431da177e4SLinus Torvalds break;
35441da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_ADAT2:
35451da177e4SLinus Torvalds autosync_ref = "ADAT2";
35461da177e4SLinus Torvalds break;
35471da177e4SLinus Torvalds case HDSP_AUTOSYNC_FROM_ADAT3:
35481da177e4SLinus Torvalds autosync_ref = "ADAT3";
35491da177e4SLinus Torvalds break;
35501da177e4SLinus Torvalds default:
35511da177e4SLinus Torvalds autosync_ref = "---";
35521da177e4SLinus Torvalds break;
35531da177e4SLinus Torvalds }
35541da177e4SLinus Torvalds snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
35551da177e4SLinus Torvalds
35561da177e4SLinus Torvalds snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
35571da177e4SLinus Torvalds
35581da177e4SLinus Torvalds snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
35591da177e4SLinus Torvalds
35601da177e4SLinus Torvalds snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
3561e3ea4d89STakashi Iwai snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
35621da177e4SLinus Torvalds
35631da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
35641da177e4SLinus Torvalds
356528b26e15SFlorian Faber if (hdsp->io_type != RPM) {
35661da177e4SLinus Torvalds switch (hdsp_spdif_in(hdsp)) {
35671da177e4SLinus Torvalds case HDSP_SPDIFIN_OPTICAL:
35681da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 input: Optical\n");
35691da177e4SLinus Torvalds break;
35701da177e4SLinus Torvalds case HDSP_SPDIFIN_COAXIAL:
35711da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 input: Coaxial\n");
35721da177e4SLinus Torvalds break;
35731da177e4SLinus Torvalds case HDSP_SPDIFIN_INTERNAL:
35741da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 input: Internal\n");
35751da177e4SLinus Torvalds break;
35761da177e4SLinus Torvalds case HDSP_SPDIFIN_AES:
35771da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 input: AES\n");
35781da177e4SLinus Torvalds break;
35791da177e4SLinus Torvalds default:
35801da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 input: ???\n");
35811da177e4SLinus Torvalds break;
35821da177e4SLinus Torvalds }
358328b26e15SFlorian Faber }
35841da177e4SLinus Torvalds
358528b26e15SFlorian Faber if (RPM == hdsp->io_type) {
358628b26e15SFlorian Faber if (hdsp->control_register & HDSP_RPM_Bypass)
358728b26e15SFlorian Faber snd_iprintf(buffer, "RPM Bypass: disabled\n");
358828b26e15SFlorian Faber else
358928b26e15SFlorian Faber snd_iprintf(buffer, "RPM Bypass: enabled\n");
359028b26e15SFlorian Faber if (hdsp->control_register & HDSP_RPM_Disconnect)
359128b26e15SFlorian Faber snd_iprintf(buffer, "RPM disconnected\n");
359228b26e15SFlorian Faber else
359328b26e15SFlorian Faber snd_iprintf(buffer, "RPM connected\n");
359428b26e15SFlorian Faber
359528b26e15SFlorian Faber switch (hdsp->control_register & HDSP_RPM_Inp12) {
359628b26e15SFlorian Faber case HDSP_RPM_Inp12_Phon_6dB:
359728b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
359828b26e15SFlorian Faber break;
359928b26e15SFlorian Faber case HDSP_RPM_Inp12_Phon_0dB:
360028b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
360128b26e15SFlorian Faber break;
360228b26e15SFlorian Faber case HDSP_RPM_Inp12_Phon_n6dB:
360328b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
360428b26e15SFlorian Faber break;
360528b26e15SFlorian Faber case HDSP_RPM_Inp12_Line_0dB:
360628b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
360728b26e15SFlorian Faber break;
360828b26e15SFlorian Faber case HDSP_RPM_Inp12_Line_n6dB:
360928b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
361028b26e15SFlorian Faber break;
361128b26e15SFlorian Faber default:
361228b26e15SFlorian Faber snd_iprintf(buffer, "Input 1/2: ???\n");
361328b26e15SFlorian Faber }
361428b26e15SFlorian Faber
361528b26e15SFlorian Faber switch (hdsp->control_register & HDSP_RPM_Inp34) {
361628b26e15SFlorian Faber case HDSP_RPM_Inp34_Phon_6dB:
361728b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
361828b26e15SFlorian Faber break;
361928b26e15SFlorian Faber case HDSP_RPM_Inp34_Phon_0dB:
362028b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
362128b26e15SFlorian Faber break;
362228b26e15SFlorian Faber case HDSP_RPM_Inp34_Phon_n6dB:
362328b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
362428b26e15SFlorian Faber break;
362528b26e15SFlorian Faber case HDSP_RPM_Inp34_Line_0dB:
362628b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
362728b26e15SFlorian Faber break;
362828b26e15SFlorian Faber case HDSP_RPM_Inp34_Line_n6dB:
362928b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
363028b26e15SFlorian Faber break;
363128b26e15SFlorian Faber default:
363228b26e15SFlorian Faber snd_iprintf(buffer, "Input 3/4: ???\n");
363328b26e15SFlorian Faber }
363428b26e15SFlorian Faber
363528b26e15SFlorian Faber } else {
3636b0b98119STakashi Iwai if (hdsp->control_register & HDSP_SPDIFOpticalOut)
36371da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
3638b0b98119STakashi Iwai else
36391da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
36401da177e4SLinus Torvalds
3641b0b98119STakashi Iwai if (hdsp->control_register & HDSP_SPDIFProfessional)
36421da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 quality: Professional\n");
3643b0b98119STakashi Iwai else
36441da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 quality: Consumer\n");
36451da177e4SLinus Torvalds
3646b0b98119STakashi Iwai if (hdsp->control_register & HDSP_SPDIFEmphasis)
36471da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 emphasis: on\n");
3648b0b98119STakashi Iwai else
36491da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 emphasis: off\n");
36501da177e4SLinus Torvalds
3651b0b98119STakashi Iwai if (hdsp->control_register & HDSP_SPDIFNonAudio)
36521da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 NonAudio: on\n");
3653b0b98119STakashi Iwai else
36541da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 NonAudio: off\n");
365528b26e15SFlorian Faber x = hdsp_spdif_sample_rate(hdsp);
365628b26e15SFlorian Faber if (x != 0)
36571da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
3658b0b98119STakashi Iwai else
36591da177e4SLinus Torvalds snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
366028b26e15SFlorian Faber }
36611da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
36621da177e4SLinus Torvalds
36631da177e4SLinus Torvalds /* Sync Check */
36641da177e4SLinus Torvalds x = status & HDSP_Sync0;
3665b0b98119STakashi Iwai if (status & HDSP_Lock0)
36661da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
3667b0b98119STakashi Iwai else
36681da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT1: No Lock\n");
36691da177e4SLinus Torvalds
36701da177e4SLinus Torvalds switch (hdsp->io_type) {
36711da177e4SLinus Torvalds case Digiface:
36721da177e4SLinus Torvalds case H9652:
36731da177e4SLinus Torvalds x = status & HDSP_Sync1;
3674b0b98119STakashi Iwai if (status & HDSP_Lock1)
36751da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
3676b0b98119STakashi Iwai else
36771da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT2: No Lock\n");
36781da177e4SLinus Torvalds x = status & HDSP_Sync2;
3679b0b98119STakashi Iwai if (status & HDSP_Lock2)
36801da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
3681b0b98119STakashi Iwai else
36821da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT3: No Lock\n");
3683b0b98119STakashi Iwai break;
36841da177e4SLinus Torvalds default:
36851da177e4SLinus Torvalds /* relax */
36861da177e4SLinus Torvalds break;
36871da177e4SLinus Torvalds }
36881da177e4SLinus Torvalds
36891da177e4SLinus Torvalds x = status & HDSP_SPDIFSync;
3690b0b98119STakashi Iwai if (status & HDSP_SPDIFErrorFlag)
36911da177e4SLinus Torvalds snd_iprintf (buffer, "SPDIF: No Lock\n");
3692b0b98119STakashi Iwai else
36931da177e4SLinus Torvalds snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
36941da177e4SLinus Torvalds
36951da177e4SLinus Torvalds x = status2 & HDSP_wc_sync;
3696b0b98119STakashi Iwai if (status2 & HDSP_wc_lock)
36971da177e4SLinus Torvalds snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
3698b0b98119STakashi Iwai else
36991da177e4SLinus Torvalds snd_iprintf (buffer, "Word Clock: No Lock\n");
37001da177e4SLinus Torvalds
37011da177e4SLinus Torvalds x = status & HDSP_TimecodeSync;
3702b0b98119STakashi Iwai if (status & HDSP_TimecodeLock)
37031da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
3704b0b98119STakashi Iwai else
37051da177e4SLinus Torvalds snd_iprintf(buffer, "ADAT Sync: No Lock\n");
37061da177e4SLinus Torvalds
37071da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
37081da177e4SLinus Torvalds
37091da177e4SLinus Torvalds /* Informations about H9632 specific controls */
37101da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
37111da177e4SLinus Torvalds char *tmp;
37121da177e4SLinus Torvalds
37131da177e4SLinus Torvalds switch (hdsp_ad_gain(hdsp)) {
37141da177e4SLinus Torvalds case 0:
37151da177e4SLinus Torvalds tmp = "-10 dBV";
37161da177e4SLinus Torvalds break;
37171da177e4SLinus Torvalds case 1:
37181da177e4SLinus Torvalds tmp = "+4 dBu";
37191da177e4SLinus Torvalds break;
37201da177e4SLinus Torvalds default:
37211da177e4SLinus Torvalds tmp = "Lo Gain";
37221da177e4SLinus Torvalds break;
37231da177e4SLinus Torvalds }
37241da177e4SLinus Torvalds snd_iprintf(buffer, "AD Gain : %s\n", tmp);
37251da177e4SLinus Torvalds
37261da177e4SLinus Torvalds switch (hdsp_da_gain(hdsp)) {
37271da177e4SLinus Torvalds case 0:
37281da177e4SLinus Torvalds tmp = "Hi Gain";
37291da177e4SLinus Torvalds break;
37301da177e4SLinus Torvalds case 1:
37311da177e4SLinus Torvalds tmp = "+4 dBu";
37321da177e4SLinus Torvalds break;
37331da177e4SLinus Torvalds default:
37341da177e4SLinus Torvalds tmp = "-10 dBV";
37351da177e4SLinus Torvalds break;
37361da177e4SLinus Torvalds }
37371da177e4SLinus Torvalds snd_iprintf(buffer, "DA Gain : %s\n", tmp);
37381da177e4SLinus Torvalds
37391da177e4SLinus Torvalds switch (hdsp_phone_gain(hdsp)) {
37401da177e4SLinus Torvalds case 0:
37411da177e4SLinus Torvalds tmp = "0 dB";
37421da177e4SLinus Torvalds break;
37431da177e4SLinus Torvalds case 1:
37441da177e4SLinus Torvalds tmp = "-6 dB";
37451da177e4SLinus Torvalds break;
37461da177e4SLinus Torvalds default:
37471da177e4SLinus Torvalds tmp = "-12 dB";
37481da177e4SLinus Torvalds break;
37491da177e4SLinus Torvalds }
37501da177e4SLinus Torvalds snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
37511da177e4SLinus Torvalds
37524833c673SAdrian Knoth snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
37534833c673SAdrian Knoth hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
37544833c673SAdrian Knoth "yes" : "no");
37551da177e4SLinus Torvalds
3756b0b98119STakashi Iwai if (hdsp->control_register & HDSP_AnalogExtensionBoard)
37571da177e4SLinus Torvalds snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
3758b0b98119STakashi Iwai else
37591da177e4SLinus Torvalds snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
37601da177e4SLinus Torvalds snd_iprintf(buffer, "\n");
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds
37631da177e4SLinus Torvalds }
37641da177e4SLinus Torvalds
snd_hdsp_proc_init(struct hdsp * hdsp)37651374f8ceSRandy Dunlap static void snd_hdsp_proc_init(struct hdsp *hdsp)
37661da177e4SLinus Torvalds {
376747f2769bSTakashi Iwai snd_card_ro_proc_new(hdsp->card, "hdsp", hdsp, snd_hdsp_proc_read);
37681da177e4SLinus Torvalds }
37691da177e4SLinus Torvalds
snd_hdsp_initialize_memory(struct hdsp * hdsp)3770e23e7a14SBill Pemberton static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
37711da177e4SLinus Torvalds {
377243d35cccSTakashi Iwai struct snd_dma_buffer *capture_dma, *playback_dma;
37731da177e4SLinus Torvalds
377443d35cccSTakashi Iwai capture_dma = snd_hammerfall_get_buffer(hdsp->pci, HDSP_DMA_AREA_BYTES);
377543d35cccSTakashi Iwai playback_dma = snd_hammerfall_get_buffer(hdsp->pci, HDSP_DMA_AREA_BYTES);
377643d35cccSTakashi Iwai if (!capture_dma || !playback_dma) {
3777a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
3778a54ba0feSTakashi Iwai "%s: no buffers available\n", hdsp->card_name);
37791da177e4SLinus Torvalds return -ENOMEM;
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds
378243d35cccSTakashi Iwai /* copy to the own data for alignment */
378343d35cccSTakashi Iwai hdsp->capture_dma_buf = *capture_dma;
378443d35cccSTakashi Iwai hdsp->playback_dma_buf = *playback_dma;
37851da177e4SLinus Torvalds
378643d35cccSTakashi Iwai /* Align to bus-space 64K boundary */
378743d35cccSTakashi Iwai hdsp->capture_dma_buf.addr = ALIGN(capture_dma->addr, 0x10000ul);
378843d35cccSTakashi Iwai hdsp->playback_dma_buf.addr = ALIGN(playback_dma->addr, 0x10000ul);
37891da177e4SLinus Torvalds
37901da177e4SLinus Torvalds /* Tell the card where it is */
379143d35cccSTakashi Iwai hdsp_write(hdsp, HDSP_inputBufferAddress, hdsp->capture_dma_buf.addr);
379243d35cccSTakashi Iwai hdsp_write(hdsp, HDSP_outputBufferAddress, hdsp->playback_dma_buf.addr);
37931da177e4SLinus Torvalds
379443d35cccSTakashi Iwai hdsp->capture_dma_buf.area += hdsp->capture_dma_buf.addr - capture_dma->addr;
379543d35cccSTakashi Iwai hdsp->playback_dma_buf.area += hdsp->playback_dma_buf.addr - playback_dma->addr;
379643d35cccSTakashi Iwai hdsp->capture_buffer = hdsp->capture_dma_buf.area;
379743d35cccSTakashi Iwai hdsp->playback_buffer = hdsp->playback_dma_buf.area;
37981da177e4SLinus Torvalds
37991da177e4SLinus Torvalds return 0;
38001da177e4SLinus Torvalds }
38011da177e4SLinus Torvalds
snd_hdsp_set_defaults(struct hdsp * hdsp)380255e957d8STakashi Iwai static int snd_hdsp_set_defaults(struct hdsp *hdsp)
38031da177e4SLinus Torvalds {
38041da177e4SLinus Torvalds unsigned int i;
38051da177e4SLinus Torvalds
38061da177e4SLinus Torvalds /* ASSUMPTION: hdsp->lock is either held, or
38071da177e4SLinus Torvalds there is no need to hold it (e.g. during module
3808561de31aSJoe Perches initialization).
38091da177e4SLinus Torvalds */
38101da177e4SLinus Torvalds
38111da177e4SLinus Torvalds /* set defaults:
38121da177e4SLinus Torvalds
38131da177e4SLinus Torvalds SPDIF Input via Coax
38141da177e4SLinus Torvalds Master clock mode
38151da177e4SLinus Torvalds maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
38161da177e4SLinus Torvalds which implies 2 4096 sample, 32Kbyte periods).
38171da177e4SLinus Torvalds Enable line out.
38181da177e4SLinus Torvalds */
38191da177e4SLinus Torvalds
38201da177e4SLinus Torvalds hdsp->control_register = HDSP_ClockModeMaster |
38211da177e4SLinus Torvalds HDSP_SPDIFInputCoaxial |
38221da177e4SLinus Torvalds hdsp_encode_latency(7) |
38231da177e4SLinus Torvalds HDSP_LineOut;
38241da177e4SLinus Torvalds
38251da177e4SLinus Torvalds
38261da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
38271da177e4SLinus Torvalds
38281da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
38291da177e4SLinus Torvalds hdsp->control2_register = HDSP_BIGENDIAN_MODE;
38301da177e4SLinus Torvalds #else
38311da177e4SLinus Torvalds hdsp->control2_register = 0;
38321da177e4SLinus Torvalds #endif
3833b0b98119STakashi Iwai if (hdsp->io_type == H9652)
38341da177e4SLinus Torvalds snd_hdsp_9652_enable_mixer (hdsp);
3835b0b98119STakashi Iwai else
38361da177e4SLinus Torvalds hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
38371da177e4SLinus Torvalds
38381da177e4SLinus Torvalds hdsp_reset_hw_pointer(hdsp);
38391da177e4SLinus Torvalds hdsp_compute_period_size(hdsp);
38401da177e4SLinus Torvalds
38411da177e4SLinus Torvalds /* silence everything */
38421da177e4SLinus Torvalds
3843b0b98119STakashi Iwai for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
38441da177e4SLinus Torvalds hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
38451da177e4SLinus Torvalds
38461da177e4SLinus Torvalds for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
3847b0b98119STakashi Iwai if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
38481da177e4SLinus Torvalds return -EIO;
38491da177e4SLinus Torvalds }
38501da177e4SLinus Torvalds
38511da177e4SLinus Torvalds /* H9632 specific defaults */
38521da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
38531da177e4SLinus Torvalds hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
38541da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
38551da177e4SLinus Torvalds }
38561da177e4SLinus Torvalds
38571da177e4SLinus Torvalds /* set a default rate so that the channel map is set up.
38581da177e4SLinus Torvalds */
38591da177e4SLinus Torvalds
38601da177e4SLinus Torvalds hdsp_set_rate(hdsp, 48000, 1);
38611da177e4SLinus Torvalds
38621da177e4SLinus Torvalds return 0;
38631da177e4SLinus Torvalds }
38641da177e4SLinus Torvalds
hdsp_midi_work(struct work_struct * work)38654adab848STakashi Iwai static void hdsp_midi_work(struct work_struct *work)
38661da177e4SLinus Torvalds {
38674adab848STakashi Iwai struct hdsp *hdsp = container_of(work, struct hdsp, midi_work);
38681da177e4SLinus Torvalds
3869b0b98119STakashi Iwai if (hdsp->midi[0].pending)
38701da177e4SLinus Torvalds snd_hdsp_midi_input_read (&hdsp->midi[0]);
3871b0b98119STakashi Iwai if (hdsp->midi[1].pending)
38721da177e4SLinus Torvalds snd_hdsp_midi_input_read (&hdsp->midi[1]);
38731da177e4SLinus Torvalds }
38741da177e4SLinus Torvalds
snd_hdsp_interrupt(int irq,void * dev_id)38757d12e780SDavid Howells static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
38761da177e4SLinus Torvalds {
387755e957d8STakashi Iwai struct hdsp *hdsp = (struct hdsp *) dev_id;
38781da177e4SLinus Torvalds unsigned int status;
38791da177e4SLinus Torvalds int audio;
38801da177e4SLinus Torvalds int midi0;
38811da177e4SLinus Torvalds int midi1;
38821da177e4SLinus Torvalds unsigned int midi0status;
38831da177e4SLinus Torvalds unsigned int midi1status;
38841da177e4SLinus Torvalds int schedule = 0;
38851da177e4SLinus Torvalds
38861da177e4SLinus Torvalds status = hdsp_read(hdsp, HDSP_statusRegister);
38871da177e4SLinus Torvalds
38881da177e4SLinus Torvalds audio = status & HDSP_audioIRQPending;
38891da177e4SLinus Torvalds midi0 = status & HDSP_midi0IRQPending;
38901da177e4SLinus Torvalds midi1 = status & HDSP_midi1IRQPending;
38911da177e4SLinus Torvalds
3892b0b98119STakashi Iwai if (!audio && !midi0 && !midi1)
38931da177e4SLinus Torvalds return IRQ_NONE;
38941da177e4SLinus Torvalds
38951da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
38961da177e4SLinus Torvalds
38971da177e4SLinus Torvalds midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
38981da177e4SLinus Torvalds midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
38991da177e4SLinus Torvalds
3900c2503cd3STakashi Iwai if (!(hdsp->state & HDSP_InitializationComplete))
3901c2503cd3STakashi Iwai return IRQ_HANDLED;
3902c2503cd3STakashi Iwai
39031da177e4SLinus Torvalds if (audio) {
3904b0b98119STakashi Iwai if (hdsp->capture_substream)
39051da177e4SLinus Torvalds snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
39061da177e4SLinus Torvalds
3907b0b98119STakashi Iwai if (hdsp->playback_substream)
39081da177e4SLinus Torvalds snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
39091da177e4SLinus Torvalds }
39101da177e4SLinus Torvalds
39111da177e4SLinus Torvalds if (midi0 && midi0status) {
39124adab848STakashi Iwai if (hdsp->use_midi_work) {
39131da177e4SLinus Torvalds /* we disable interrupts for this input until processing is done */
39141da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
39151da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
39161da177e4SLinus Torvalds hdsp->midi[0].pending = 1;
39171da177e4SLinus Torvalds schedule = 1;
39181da177e4SLinus Torvalds } else {
39191da177e4SLinus Torvalds snd_hdsp_midi_input_read (&hdsp->midi[0]);
39201da177e4SLinus Torvalds }
39211da177e4SLinus Torvalds }
392228b26e15SFlorian Faber if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
39234adab848STakashi Iwai if (hdsp->use_midi_work) {
39241da177e4SLinus Torvalds /* we disable interrupts for this input until processing is done */
39251da177e4SLinus Torvalds hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
39261da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
39271da177e4SLinus Torvalds hdsp->midi[1].pending = 1;
39281da177e4SLinus Torvalds schedule = 1;
39291da177e4SLinus Torvalds } else {
39301da177e4SLinus Torvalds snd_hdsp_midi_input_read (&hdsp->midi[1]);
39311da177e4SLinus Torvalds }
39321da177e4SLinus Torvalds }
39334adab848STakashi Iwai if (hdsp->use_midi_work && schedule)
39344adab848STakashi Iwai queue_work(system_highpri_wq, &hdsp->midi_work);
39351da177e4SLinus Torvalds return IRQ_HANDLED;
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds
snd_hdsp_hw_pointer(struct snd_pcm_substream * substream)393855e957d8STakashi Iwai static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
39391da177e4SLinus Torvalds {
394055e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
39411da177e4SLinus Torvalds return hdsp_hw_pointer(hdsp);
39421da177e4SLinus Torvalds }
39431da177e4SLinus Torvalds
hdsp_channel_buffer_location(struct hdsp * hdsp,int stream,int channel)394450895a55SJason A. Donenfeld static signed char *hdsp_channel_buffer_location(struct hdsp *hdsp,
39451da177e4SLinus Torvalds int stream,
39461da177e4SLinus Torvalds int channel)
39471da177e4SLinus Torvalds
39481da177e4SLinus Torvalds {
39491da177e4SLinus Torvalds int mapped_channel;
39501da177e4SLinus Torvalds
3951da3cec35STakashi Iwai if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
3952da3cec35STakashi Iwai return NULL;
39531da177e4SLinus Torvalds
395466c8f759STakashi Iwai mapped_channel = hdsp->channel_map[channel];
395566c8f759STakashi Iwai if (mapped_channel < 0)
39561da177e4SLinus Torvalds return NULL;
39571da177e4SLinus Torvalds
3958b0b98119STakashi Iwai if (stream == SNDRV_PCM_STREAM_CAPTURE)
39591da177e4SLinus Torvalds return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
3960b0b98119STakashi Iwai else
39611da177e4SLinus Torvalds return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
39621da177e4SLinus Torvalds }
39631da177e4SLinus Torvalds
snd_hdsp_playback_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * src,unsigned long count)39640925e6b2STakashi Iwai static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream,
39650925e6b2STakashi Iwai int channel, unsigned long pos,
396690ed2311STakashi Iwai struct iov_iter *src, unsigned long count)
39671da177e4SLinus Torvalds {
396855e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
396950895a55SJason A. Donenfeld signed char *channel_buf;
39701da177e4SLinus Torvalds
39710925e6b2STakashi Iwai if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3972da3cec35STakashi Iwai return -EINVAL;
39731da177e4SLinus Torvalds
39741da177e4SLinus Torvalds channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3975da3cec35STakashi Iwai if (snd_BUG_ON(!channel_buf))
3976da3cec35STakashi Iwai return -EIO;
397790ed2311STakashi Iwai if (copy_from_iter(channel_buf + pos, count, src) != count)
39781da177e4SLinus Torvalds return -EFAULT;
39790925e6b2STakashi Iwai return 0;
39801da177e4SLinus Torvalds }
39811da177e4SLinus Torvalds
snd_hdsp_capture_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * dst,unsigned long count)39820925e6b2STakashi Iwai static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream,
39830925e6b2STakashi Iwai int channel, unsigned long pos,
398490ed2311STakashi Iwai struct iov_iter *dst, unsigned long count)
39850925e6b2STakashi Iwai {
39860925e6b2STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
398750895a55SJason A. Donenfeld signed char *channel_buf;
39880925e6b2STakashi Iwai
39890925e6b2STakashi Iwai if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES))
3990da3cec35STakashi Iwai return -EINVAL;
39911da177e4SLinus Torvalds
39921da177e4SLinus Torvalds channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
3993da3cec35STakashi Iwai if (snd_BUG_ON(!channel_buf))
3994da3cec35STakashi Iwai return -EIO;
399590ed2311STakashi Iwai if (copy_to_iter(channel_buf + pos, count, dst) != count)
39961da177e4SLinus Torvalds return -EFAULT;
39970925e6b2STakashi Iwai return 0;
39981da177e4SLinus Torvalds }
39991da177e4SLinus Torvalds
snd_hdsp_hw_silence(struct snd_pcm_substream * substream,int channel,unsigned long pos,unsigned long count)40000925e6b2STakashi Iwai static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream,
40010925e6b2STakashi Iwai int channel, unsigned long pos,
40020925e6b2STakashi Iwai unsigned long count)
40030925e6b2STakashi Iwai {
40040925e6b2STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
400550895a55SJason A. Donenfeld signed char *channel_buf;
40060925e6b2STakashi Iwai
40070925e6b2STakashi Iwai channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
40080925e6b2STakashi Iwai if (snd_BUG_ON(!channel_buf))
40090925e6b2STakashi Iwai return -EIO;
40100925e6b2STakashi Iwai memset(channel_buf + pos, 0, count);
40110925e6b2STakashi Iwai return 0;
40121da177e4SLinus Torvalds }
40131da177e4SLinus Torvalds
snd_hdsp_reset(struct snd_pcm_substream * substream)401455e957d8STakashi Iwai static int snd_hdsp_reset(struct snd_pcm_substream *substream)
40151da177e4SLinus Torvalds {
401655e957d8STakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime;
401755e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
401855e957d8STakashi Iwai struct snd_pcm_substream *other;
40191da177e4SLinus Torvalds if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
40201da177e4SLinus Torvalds other = hdsp->capture_substream;
40211da177e4SLinus Torvalds else
40221da177e4SLinus Torvalds other = hdsp->playback_substream;
40231da177e4SLinus Torvalds if (hdsp->running)
40241da177e4SLinus Torvalds runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
40251da177e4SLinus Torvalds else
40261da177e4SLinus Torvalds runtime->status->hw_ptr = 0;
40271da177e4SLinus Torvalds if (other) {
402855e957d8STakashi Iwai struct snd_pcm_substream *s;
402955e957d8STakashi Iwai struct snd_pcm_runtime *oruntime = other->runtime;
4030ef991b95STakashi Iwai snd_pcm_group_for_each_entry(s, substream) {
40311da177e4SLinus Torvalds if (s == other) {
40321da177e4SLinus Torvalds oruntime->status->hw_ptr = runtime->status->hw_ptr;
40331da177e4SLinus Torvalds break;
40341da177e4SLinus Torvalds }
40351da177e4SLinus Torvalds }
40361da177e4SLinus Torvalds }
40371da177e4SLinus Torvalds return 0;
40381da177e4SLinus Torvalds }
40391da177e4SLinus Torvalds
snd_hdsp_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)404055e957d8STakashi Iwai static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
404155e957d8STakashi Iwai struct snd_pcm_hw_params *params)
40421da177e4SLinus Torvalds {
404355e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
40441da177e4SLinus Torvalds int err;
40451da177e4SLinus Torvalds pid_t this_pid;
40461da177e4SLinus Torvalds pid_t other_pid;
40471da177e4SLinus Torvalds
4048b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
40491da177e4SLinus Torvalds return -EIO;
40501da177e4SLinus Torvalds
4051b0b98119STakashi Iwai if (hdsp_check_for_firmware(hdsp, 1))
40521da177e4SLinus Torvalds return -EIO;
40531da177e4SLinus Torvalds
40541da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
40551da177e4SLinus Torvalds
40561da177e4SLinus Torvalds if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
40571da177e4SLinus Torvalds hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
40581da177e4SLinus Torvalds hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
40591da177e4SLinus Torvalds this_pid = hdsp->playback_pid;
40601da177e4SLinus Torvalds other_pid = hdsp->capture_pid;
40611da177e4SLinus Torvalds } else {
40621da177e4SLinus Torvalds this_pid = hdsp->capture_pid;
40631da177e4SLinus Torvalds other_pid = hdsp->playback_pid;
40641da177e4SLinus Torvalds }
40651da177e4SLinus Torvalds
40661da177e4SLinus Torvalds if ((other_pid > 0) && (this_pid != other_pid)) {
40671da177e4SLinus Torvalds
40681da177e4SLinus Torvalds /* The other stream is open, and not by the same
40691da177e4SLinus Torvalds task as this one. Make sure that the parameters
40701da177e4SLinus Torvalds that matter are the same.
40711da177e4SLinus Torvalds */
40721da177e4SLinus Torvalds
40731da177e4SLinus Torvalds if (params_rate(params) != hdsp->system_sample_rate) {
40741da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
40751da177e4SLinus Torvalds _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
40761da177e4SLinus Torvalds return -EBUSY;
40771da177e4SLinus Torvalds }
40781da177e4SLinus Torvalds
40791da177e4SLinus Torvalds if (params_period_size(params) != hdsp->period_bytes / 4) {
40801da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
40811da177e4SLinus Torvalds _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
40821da177e4SLinus Torvalds return -EBUSY;
40831da177e4SLinus Torvalds }
40841da177e4SLinus Torvalds
40851da177e4SLinus Torvalds /* We're fine. */
40861da177e4SLinus Torvalds
40871da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
40881da177e4SLinus Torvalds return 0;
40891da177e4SLinus Torvalds
40901da177e4SLinus Torvalds } else {
40911da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
40921da177e4SLinus Torvalds }
40931da177e4SLinus Torvalds
40941da177e4SLinus Torvalds /* how to make sure that the rate matches an externally-set one ?
40951da177e4SLinus Torvalds */
40961da177e4SLinus Torvalds
40971da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
4098e3ea4d89STakashi Iwai if (! hdsp->clock_source_locked) {
409966c8f759STakashi Iwai err = hdsp_set_rate(hdsp, params_rate(params), 0);
410066c8f759STakashi Iwai if (err < 0) {
41011da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
41021da177e4SLinus Torvalds _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
41031da177e4SLinus Torvalds return err;
41041da177e4SLinus Torvalds }
4105e3ea4d89STakashi Iwai }
4106e3ea4d89STakashi Iwai spin_unlock_irq(&hdsp->lock);
41071da177e4SLinus Torvalds
410866c8f759STakashi Iwai err = hdsp_set_interrupt_interval(hdsp, params_period_size(params));
410966c8f759STakashi Iwai if (err < 0) {
41101da177e4SLinus Torvalds _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
41111da177e4SLinus Torvalds return err;
41121da177e4SLinus Torvalds }
41131da177e4SLinus Torvalds
41141da177e4SLinus Torvalds return 0;
41151da177e4SLinus Torvalds }
41161da177e4SLinus Torvalds
snd_hdsp_channel_info(struct snd_pcm_substream * substream,struct snd_pcm_channel_info * info)411755e957d8STakashi Iwai static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
411855e957d8STakashi Iwai struct snd_pcm_channel_info *info)
41191da177e4SLinus Torvalds {
412055e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
41210b84304eSGustavo A. R. Silva unsigned int channel = info->channel;
41221da177e4SLinus Torvalds
41230b84304eSGustavo A. R. Silva if (snd_BUG_ON(channel >= hdsp->max_channels))
41240b84304eSGustavo A. R. Silva return -EINVAL;
41250b84304eSGustavo A. R. Silva channel = array_index_nospec(channel, hdsp->max_channels);
41260b84304eSGustavo A. R. Silva
41270b84304eSGustavo A. R. Silva if (hdsp->channel_map[channel] < 0)
4128da3cec35STakashi Iwai return -EINVAL;
41291da177e4SLinus Torvalds
41300b84304eSGustavo A. R. Silva info->offset = hdsp->channel_map[channel] * HDSP_CHANNEL_BUFFER_BYTES;
41311da177e4SLinus Torvalds info->first = 0;
41321da177e4SLinus Torvalds info->step = 32;
41331da177e4SLinus Torvalds return 0;
41341da177e4SLinus Torvalds }
41351da177e4SLinus Torvalds
snd_hdsp_ioctl(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)413655e957d8STakashi Iwai static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
41371da177e4SLinus Torvalds unsigned int cmd, void *arg)
41381da177e4SLinus Torvalds {
41391da177e4SLinus Torvalds switch (cmd) {
41401da177e4SLinus Torvalds case SNDRV_PCM_IOCTL1_RESET:
41411da177e4SLinus Torvalds return snd_hdsp_reset(substream);
41421da177e4SLinus Torvalds case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
4143b0b98119STakashi Iwai return snd_hdsp_channel_info(substream, arg);
41441da177e4SLinus Torvalds default:
41451da177e4SLinus Torvalds break;
41461da177e4SLinus Torvalds }
41471da177e4SLinus Torvalds
41481da177e4SLinus Torvalds return snd_pcm_lib_ioctl(substream, cmd, arg);
41491da177e4SLinus Torvalds }
41501da177e4SLinus Torvalds
snd_hdsp_trigger(struct snd_pcm_substream * substream,int cmd)415155e957d8STakashi Iwai static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
41521da177e4SLinus Torvalds {
415355e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
415455e957d8STakashi Iwai struct snd_pcm_substream *other;
41551da177e4SLinus Torvalds int running;
41561da177e4SLinus Torvalds
4157b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
41581da177e4SLinus Torvalds return -EIO;
41591da177e4SLinus Torvalds
4160311e70a4STakashi Iwai if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
41611da177e4SLinus Torvalds return -EIO;
41621da177e4SLinus Torvalds
41631da177e4SLinus Torvalds spin_lock(&hdsp->lock);
41641da177e4SLinus Torvalds running = hdsp->running;
41651da177e4SLinus Torvalds switch (cmd) {
41661da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_START:
41671da177e4SLinus Torvalds running |= 1 << substream->stream;
41681da177e4SLinus Torvalds break;
41691da177e4SLinus Torvalds case SNDRV_PCM_TRIGGER_STOP:
41701da177e4SLinus Torvalds running &= ~(1 << substream->stream);
41711da177e4SLinus Torvalds break;
41721da177e4SLinus Torvalds default:
41731da177e4SLinus Torvalds snd_BUG();
41741da177e4SLinus Torvalds spin_unlock(&hdsp->lock);
41751da177e4SLinus Torvalds return -EINVAL;
41761da177e4SLinus Torvalds }
41771da177e4SLinus Torvalds if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
41781da177e4SLinus Torvalds other = hdsp->capture_substream;
41791da177e4SLinus Torvalds else
41801da177e4SLinus Torvalds other = hdsp->playback_substream;
41811da177e4SLinus Torvalds
41821da177e4SLinus Torvalds if (other) {
418355e957d8STakashi Iwai struct snd_pcm_substream *s;
4184ef991b95STakashi Iwai snd_pcm_group_for_each_entry(s, substream) {
41851da177e4SLinus Torvalds if (s == other) {
41861da177e4SLinus Torvalds snd_pcm_trigger_done(s, substream);
41871da177e4SLinus Torvalds if (cmd == SNDRV_PCM_TRIGGER_START)
41881da177e4SLinus Torvalds running |= 1 << s->stream;
41891da177e4SLinus Torvalds else
41901da177e4SLinus Torvalds running &= ~(1 << s->stream);
41911da177e4SLinus Torvalds goto _ok;
41921da177e4SLinus Torvalds }
41931da177e4SLinus Torvalds }
41941da177e4SLinus Torvalds if (cmd == SNDRV_PCM_TRIGGER_START) {
41951da177e4SLinus Torvalds if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
41961da177e4SLinus Torvalds substream->stream == SNDRV_PCM_STREAM_CAPTURE)
41971da177e4SLinus Torvalds hdsp_silence_playback(hdsp);
41981da177e4SLinus Torvalds } else {
41991da177e4SLinus Torvalds if (running &&
42001da177e4SLinus Torvalds substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
42011da177e4SLinus Torvalds hdsp_silence_playback(hdsp);
42021da177e4SLinus Torvalds }
42031da177e4SLinus Torvalds } else {
42041da177e4SLinus Torvalds if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
42051da177e4SLinus Torvalds hdsp_silence_playback(hdsp);
42061da177e4SLinus Torvalds }
42071da177e4SLinus Torvalds _ok:
42081da177e4SLinus Torvalds snd_pcm_trigger_done(substream, substream);
42091da177e4SLinus Torvalds if (!hdsp->running && running)
42101da177e4SLinus Torvalds hdsp_start_audio(hdsp);
42111da177e4SLinus Torvalds else if (hdsp->running && !running)
42121da177e4SLinus Torvalds hdsp_stop_audio(hdsp);
42131da177e4SLinus Torvalds hdsp->running = running;
42141da177e4SLinus Torvalds spin_unlock(&hdsp->lock);
42151da177e4SLinus Torvalds
42161da177e4SLinus Torvalds return 0;
42171da177e4SLinus Torvalds }
42181da177e4SLinus Torvalds
snd_hdsp_prepare(struct snd_pcm_substream * substream)421955e957d8STakashi Iwai static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
42201da177e4SLinus Torvalds {
422155e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
42221da177e4SLinus Torvalds int result = 0;
42231da177e4SLinus Torvalds
4224b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
42251da177e4SLinus Torvalds return -EIO;
42261da177e4SLinus Torvalds
4227b0b98119STakashi Iwai if (hdsp_check_for_firmware(hdsp, 1))
42281da177e4SLinus Torvalds return -EIO;
42291da177e4SLinus Torvalds
42301da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
42311da177e4SLinus Torvalds if (!hdsp->running)
42321da177e4SLinus Torvalds hdsp_reset_hw_pointer(hdsp);
42331da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
42341da177e4SLinus Torvalds return result;
42351da177e4SLinus Torvalds }
42361da177e4SLinus Torvalds
423718ed1cf0SBhumika Goyal static const struct snd_pcm_hardware snd_hdsp_playback_subinfo =
42381da177e4SLinus Torvalds {
42391da177e4SLinus Torvalds .info = (SNDRV_PCM_INFO_MMAP |
42401da177e4SLinus Torvalds SNDRV_PCM_INFO_MMAP_VALID |
42411da177e4SLinus Torvalds SNDRV_PCM_INFO_NONINTERLEAVED |
42421da177e4SLinus Torvalds SNDRV_PCM_INFO_SYNC_START |
42431da177e4SLinus Torvalds SNDRV_PCM_INFO_DOUBLE),
42441da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
42451da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_S32_BE,
42461da177e4SLinus Torvalds #else
42471da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_S32_LE,
42481da177e4SLinus Torvalds #endif
42491da177e4SLinus Torvalds .rates = (SNDRV_PCM_RATE_32000 |
42501da177e4SLinus Torvalds SNDRV_PCM_RATE_44100 |
42511da177e4SLinus Torvalds SNDRV_PCM_RATE_48000 |
42521da177e4SLinus Torvalds SNDRV_PCM_RATE_64000 |
42531da177e4SLinus Torvalds SNDRV_PCM_RATE_88200 |
42541da177e4SLinus Torvalds SNDRV_PCM_RATE_96000),
42551da177e4SLinus Torvalds .rate_min = 32000,
42561da177e4SLinus Torvalds .rate_max = 96000,
425728b26e15SFlorian Faber .channels_min = 6,
42581da177e4SLinus Torvalds .channels_max = HDSP_MAX_CHANNELS,
42591da177e4SLinus Torvalds .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
42601da177e4SLinus Torvalds .period_bytes_min = (64 * 4) * 10,
42611da177e4SLinus Torvalds .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
42621da177e4SLinus Torvalds .periods_min = 2,
42631da177e4SLinus Torvalds .periods_max = 2,
42641da177e4SLinus Torvalds .fifo_size = 0
42651da177e4SLinus Torvalds };
42661da177e4SLinus Torvalds
426718ed1cf0SBhumika Goyal static const struct snd_pcm_hardware snd_hdsp_capture_subinfo =
42681da177e4SLinus Torvalds {
42691da177e4SLinus Torvalds .info = (SNDRV_PCM_INFO_MMAP |
42701da177e4SLinus Torvalds SNDRV_PCM_INFO_MMAP_VALID |
42711da177e4SLinus Torvalds SNDRV_PCM_INFO_NONINTERLEAVED |
42721da177e4SLinus Torvalds SNDRV_PCM_INFO_SYNC_START),
42731da177e4SLinus Torvalds #ifdef SNDRV_BIG_ENDIAN
42741da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_S32_BE,
42751da177e4SLinus Torvalds #else
42761da177e4SLinus Torvalds .formats = SNDRV_PCM_FMTBIT_S32_LE,
42771da177e4SLinus Torvalds #endif
42781da177e4SLinus Torvalds .rates = (SNDRV_PCM_RATE_32000 |
42791da177e4SLinus Torvalds SNDRV_PCM_RATE_44100 |
42801da177e4SLinus Torvalds SNDRV_PCM_RATE_48000 |
42811da177e4SLinus Torvalds SNDRV_PCM_RATE_64000 |
42821da177e4SLinus Torvalds SNDRV_PCM_RATE_88200 |
42831da177e4SLinus Torvalds SNDRV_PCM_RATE_96000),
42841da177e4SLinus Torvalds .rate_min = 32000,
42851da177e4SLinus Torvalds .rate_max = 96000,
428628b26e15SFlorian Faber .channels_min = 5,
42871da177e4SLinus Torvalds .channels_max = HDSP_MAX_CHANNELS,
42881da177e4SLinus Torvalds .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
42891da177e4SLinus Torvalds .period_bytes_min = (64 * 4) * 10,
42901da177e4SLinus Torvalds .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
42911da177e4SLinus Torvalds .periods_min = 2,
42921da177e4SLinus Torvalds .periods_max = 2,
42931da177e4SLinus Torvalds .fifo_size = 0
42941da177e4SLinus Torvalds };
42951da177e4SLinus Torvalds
42964640c034STakashi Iwai static const unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
42971da177e4SLinus Torvalds
42984640c034STakashi Iwai static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
42991da177e4SLinus Torvalds .count = ARRAY_SIZE(hdsp_period_sizes),
43001da177e4SLinus Torvalds .list = hdsp_period_sizes,
43011da177e4SLinus Torvalds .mask = 0
43021da177e4SLinus Torvalds };
43031da177e4SLinus Torvalds
43044640c034STakashi Iwai static const unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
43051da177e4SLinus Torvalds
43064640c034STakashi Iwai static const struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
43071da177e4SLinus Torvalds .count = ARRAY_SIZE(hdsp_9632_sample_rates),
43081da177e4SLinus Torvalds .list = hdsp_9632_sample_rates,
43091da177e4SLinus Torvalds .mask = 0
43101da177e4SLinus Torvalds };
43111da177e4SLinus Torvalds
snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)431255e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
431355e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
43141da177e4SLinus Torvalds {
431555e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
431655e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
43171da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
43181da177e4SLinus Torvalds unsigned int list[3];
43191da177e4SLinus Torvalds list[0] = hdsp->qs_in_channels;
43201da177e4SLinus Torvalds list[1] = hdsp->ds_in_channels;
43211da177e4SLinus Torvalds list[2] = hdsp->ss_in_channels;
43221da177e4SLinus Torvalds return snd_interval_list(c, 3, list, 0);
43231da177e4SLinus Torvalds } else {
43241da177e4SLinus Torvalds unsigned int list[2];
43251da177e4SLinus Torvalds list[0] = hdsp->ds_in_channels;
43261da177e4SLinus Torvalds list[1] = hdsp->ss_in_channels;
43271da177e4SLinus Torvalds return snd_interval_list(c, 2, list, 0);
43281da177e4SLinus Torvalds }
43291da177e4SLinus Torvalds }
43301da177e4SLinus Torvalds
snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)433155e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
433255e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
43331da177e4SLinus Torvalds {
43341da177e4SLinus Torvalds unsigned int list[3];
433555e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
433655e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
43371da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
43381da177e4SLinus Torvalds list[0] = hdsp->qs_out_channels;
43391da177e4SLinus Torvalds list[1] = hdsp->ds_out_channels;
43401da177e4SLinus Torvalds list[2] = hdsp->ss_out_channels;
43411da177e4SLinus Torvalds return snd_interval_list(c, 3, list, 0);
43421da177e4SLinus Torvalds } else {
43431da177e4SLinus Torvalds list[0] = hdsp->ds_out_channels;
43441da177e4SLinus Torvalds list[1] = hdsp->ss_out_channels;
43451da177e4SLinus Torvalds }
43461da177e4SLinus Torvalds return snd_interval_list(c, 2, list, 0);
43471da177e4SLinus Torvalds }
43481da177e4SLinus Torvalds
snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)434955e957d8STakashi Iwai static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
435055e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
43511da177e4SLinus Torvalds {
435255e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
435355e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
435455e957d8STakashi Iwai struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
43551da177e4SLinus Torvalds if (r->min > 96000 && hdsp->io_type == H9632) {
435655e957d8STakashi Iwai struct snd_interval t = {
43571da177e4SLinus Torvalds .min = hdsp->qs_in_channels,
43581da177e4SLinus Torvalds .max = hdsp->qs_in_channels,
43591da177e4SLinus Torvalds .integer = 1,
43601da177e4SLinus Torvalds };
43611da177e4SLinus Torvalds return snd_interval_refine(c, &t);
43621da177e4SLinus Torvalds } else if (r->min > 48000 && r->max <= 96000) {
436355e957d8STakashi Iwai struct snd_interval t = {
43641da177e4SLinus Torvalds .min = hdsp->ds_in_channels,
43651da177e4SLinus Torvalds .max = hdsp->ds_in_channels,
43661da177e4SLinus Torvalds .integer = 1,
43671da177e4SLinus Torvalds };
43681da177e4SLinus Torvalds return snd_interval_refine(c, &t);
43691da177e4SLinus Torvalds } else if (r->max < 64000) {
437055e957d8STakashi Iwai struct snd_interval t = {
43711da177e4SLinus Torvalds .min = hdsp->ss_in_channels,
43721da177e4SLinus Torvalds .max = hdsp->ss_in_channels,
43731da177e4SLinus Torvalds .integer = 1,
43741da177e4SLinus Torvalds };
43751da177e4SLinus Torvalds return snd_interval_refine(c, &t);
43761da177e4SLinus Torvalds }
43771da177e4SLinus Torvalds return 0;
43781da177e4SLinus Torvalds }
43791da177e4SLinus Torvalds
snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)438055e957d8STakashi Iwai static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
438155e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
43821da177e4SLinus Torvalds {
438355e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
438455e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
438555e957d8STakashi Iwai struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
43861da177e4SLinus Torvalds if (r->min > 96000 && hdsp->io_type == H9632) {
438755e957d8STakashi Iwai struct snd_interval t = {
43881da177e4SLinus Torvalds .min = hdsp->qs_out_channels,
43891da177e4SLinus Torvalds .max = hdsp->qs_out_channels,
43901da177e4SLinus Torvalds .integer = 1,
43911da177e4SLinus Torvalds };
43921da177e4SLinus Torvalds return snd_interval_refine(c, &t);
43931da177e4SLinus Torvalds } else if (r->min > 48000 && r->max <= 96000) {
439455e957d8STakashi Iwai struct snd_interval t = {
43951da177e4SLinus Torvalds .min = hdsp->ds_out_channels,
43961da177e4SLinus Torvalds .max = hdsp->ds_out_channels,
43971da177e4SLinus Torvalds .integer = 1,
43981da177e4SLinus Torvalds };
43991da177e4SLinus Torvalds return snd_interval_refine(c, &t);
44001da177e4SLinus Torvalds } else if (r->max < 64000) {
440155e957d8STakashi Iwai struct snd_interval t = {
44021da177e4SLinus Torvalds .min = hdsp->ss_out_channels,
44031da177e4SLinus Torvalds .max = hdsp->ss_out_channels,
44041da177e4SLinus Torvalds .integer = 1,
44051da177e4SLinus Torvalds };
44061da177e4SLinus Torvalds return snd_interval_refine(c, &t);
44071da177e4SLinus Torvalds }
44081da177e4SLinus Torvalds return 0;
44091da177e4SLinus Torvalds }
44101da177e4SLinus Torvalds
snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)441155e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
441255e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
44131da177e4SLinus Torvalds {
441455e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
441555e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
441655e957d8STakashi Iwai struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
44171da177e4SLinus Torvalds if (c->min >= hdsp->ss_out_channels) {
441855e957d8STakashi Iwai struct snd_interval t = {
44191da177e4SLinus Torvalds .min = 32000,
44201da177e4SLinus Torvalds .max = 48000,
44211da177e4SLinus Torvalds .integer = 1,
44221da177e4SLinus Torvalds };
44231da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44241da177e4SLinus Torvalds } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
442555e957d8STakashi Iwai struct snd_interval t = {
44261da177e4SLinus Torvalds .min = 128000,
44271da177e4SLinus Torvalds .max = 192000,
44281da177e4SLinus Torvalds .integer = 1,
44291da177e4SLinus Torvalds };
44301da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44311da177e4SLinus Torvalds } else if (c->max <= hdsp->ds_out_channels) {
443255e957d8STakashi Iwai struct snd_interval t = {
44331da177e4SLinus Torvalds .min = 64000,
44341da177e4SLinus Torvalds .max = 96000,
44351da177e4SLinus Torvalds .integer = 1,
44361da177e4SLinus Torvalds };
44371da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds return 0;
44401da177e4SLinus Torvalds }
44411da177e4SLinus Torvalds
snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)444255e957d8STakashi Iwai static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
444355e957d8STakashi Iwai struct snd_pcm_hw_rule *rule)
44441da177e4SLinus Torvalds {
444555e957d8STakashi Iwai struct hdsp *hdsp = rule->private;
444655e957d8STakashi Iwai struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
444755e957d8STakashi Iwai struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
44481da177e4SLinus Torvalds if (c->min >= hdsp->ss_in_channels) {
444955e957d8STakashi Iwai struct snd_interval t = {
44501da177e4SLinus Torvalds .min = 32000,
44511da177e4SLinus Torvalds .max = 48000,
44521da177e4SLinus Torvalds .integer = 1,
44531da177e4SLinus Torvalds };
44541da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44551da177e4SLinus Torvalds } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
445655e957d8STakashi Iwai struct snd_interval t = {
44571da177e4SLinus Torvalds .min = 128000,
44581da177e4SLinus Torvalds .max = 192000,
44591da177e4SLinus Torvalds .integer = 1,
44601da177e4SLinus Torvalds };
44611da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44621da177e4SLinus Torvalds } else if (c->max <= hdsp->ds_in_channels) {
446355e957d8STakashi Iwai struct snd_interval t = {
44641da177e4SLinus Torvalds .min = 64000,
44651da177e4SLinus Torvalds .max = 96000,
44661da177e4SLinus Torvalds .integer = 1,
44671da177e4SLinus Torvalds };
44681da177e4SLinus Torvalds return snd_interval_refine(r, &t);
44691da177e4SLinus Torvalds }
44701da177e4SLinus Torvalds return 0;
44711da177e4SLinus Torvalds }
44721da177e4SLinus Torvalds
snd_hdsp_playback_open(struct snd_pcm_substream * substream)447355e957d8STakashi Iwai static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
44741da177e4SLinus Torvalds {
447555e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
447655e957d8STakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime;
44771da177e4SLinus Torvalds
4478b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
44791da177e4SLinus Torvalds return -EIO;
44801da177e4SLinus Torvalds
4481b0b98119STakashi Iwai if (hdsp_check_for_firmware(hdsp, 1))
44821da177e4SLinus Torvalds return -EIO;
44831da177e4SLinus Torvalds
44841da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
44851da177e4SLinus Torvalds
44861da177e4SLinus Torvalds snd_pcm_set_sync(substream);
44871da177e4SLinus Torvalds
44881da177e4SLinus Torvalds runtime->hw = snd_hdsp_playback_subinfo;
448943d35cccSTakashi Iwai snd_pcm_set_runtime_buffer(substream, &hdsp->playback_dma_buf);
44901da177e4SLinus Torvalds
44911da177e4SLinus Torvalds hdsp->playback_pid = current->pid;
44921da177e4SLinus Torvalds hdsp->playback_substream = substream;
44931da177e4SLinus Torvalds
44941da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
44951da177e4SLinus Torvalds
44961da177e4SLinus Torvalds snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
44971da177e4SLinus Torvalds snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
4498e3ea4d89STakashi Iwai if (hdsp->clock_source_locked) {
4499e3ea4d89STakashi Iwai runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
4500e3ea4d89STakashi Iwai } else if (hdsp->io_type == H9632) {
45011da177e4SLinus Torvalds runtime->hw.rate_max = 192000;
45021da177e4SLinus Torvalds runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
45031da177e4SLinus Torvalds snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
45041da177e4SLinus Torvalds }
4505e3ea4d89STakashi Iwai if (hdsp->io_type == H9632) {
4506e3ea4d89STakashi Iwai runtime->hw.channels_min = hdsp->qs_out_channels;
4507e3ea4d89STakashi Iwai runtime->hw.channels_max = hdsp->ss_out_channels;
4508e3ea4d89STakashi Iwai }
45091da177e4SLinus Torvalds
45101da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45111da177e4SLinus Torvalds snd_hdsp_hw_rule_out_channels, hdsp,
45121da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45131da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45141da177e4SLinus Torvalds snd_hdsp_hw_rule_out_channels_rate, hdsp,
45151da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_RATE, -1);
45161da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
45171da177e4SLinus Torvalds snd_hdsp_hw_rule_rate_out_channels, hdsp,
45181da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45191da177e4SLinus Torvalds
452028b26e15SFlorian Faber if (RPM != hdsp->io_type) {
45211da177e4SLinus Torvalds hdsp->creg_spdif_stream = hdsp->creg_spdif;
45221da177e4SLinus Torvalds hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
45231da177e4SLinus Torvalds snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
45241da177e4SLinus Torvalds SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
452528b26e15SFlorian Faber }
45261da177e4SLinus Torvalds return 0;
45271da177e4SLinus Torvalds }
45281da177e4SLinus Torvalds
snd_hdsp_playback_release(struct snd_pcm_substream * substream)452955e957d8STakashi Iwai static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
45301da177e4SLinus Torvalds {
453155e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
45321da177e4SLinus Torvalds
45331da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
45341da177e4SLinus Torvalds
45351da177e4SLinus Torvalds hdsp->playback_pid = -1;
45361da177e4SLinus Torvalds hdsp->playback_substream = NULL;
45371da177e4SLinus Torvalds
45381da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
45391da177e4SLinus Torvalds
454028b26e15SFlorian Faber if (RPM != hdsp->io_type) {
45411da177e4SLinus Torvalds hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
45421da177e4SLinus Torvalds snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
45431da177e4SLinus Torvalds SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
454428b26e15SFlorian Faber }
45451da177e4SLinus Torvalds return 0;
45461da177e4SLinus Torvalds }
45471da177e4SLinus Torvalds
45481da177e4SLinus Torvalds
snd_hdsp_capture_open(struct snd_pcm_substream * substream)454955e957d8STakashi Iwai static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
45501da177e4SLinus Torvalds {
455155e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
455255e957d8STakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime;
45531da177e4SLinus Torvalds
4554b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
45551da177e4SLinus Torvalds return -EIO;
45561da177e4SLinus Torvalds
4557b0b98119STakashi Iwai if (hdsp_check_for_firmware(hdsp, 1))
45581da177e4SLinus Torvalds return -EIO;
45591da177e4SLinus Torvalds
45601da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
45611da177e4SLinus Torvalds
45621da177e4SLinus Torvalds snd_pcm_set_sync(substream);
45631da177e4SLinus Torvalds
45641da177e4SLinus Torvalds runtime->hw = snd_hdsp_capture_subinfo;
456543d35cccSTakashi Iwai snd_pcm_set_runtime_buffer(substream, &hdsp->capture_dma_buf);
45661da177e4SLinus Torvalds
45671da177e4SLinus Torvalds hdsp->capture_pid = current->pid;
45681da177e4SLinus Torvalds hdsp->capture_substream = substream;
45691da177e4SLinus Torvalds
45701da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
45711da177e4SLinus Torvalds
45721da177e4SLinus Torvalds snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
45731da177e4SLinus Torvalds snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
45741da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
45751da177e4SLinus Torvalds runtime->hw.channels_min = hdsp->qs_in_channels;
45761da177e4SLinus Torvalds runtime->hw.channels_max = hdsp->ss_in_channels;
45771da177e4SLinus Torvalds runtime->hw.rate_max = 192000;
45781da177e4SLinus Torvalds runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
45791da177e4SLinus Torvalds snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
45801da177e4SLinus Torvalds }
45811da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45821da177e4SLinus Torvalds snd_hdsp_hw_rule_in_channels, hdsp,
45831da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45841da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45851da177e4SLinus Torvalds snd_hdsp_hw_rule_in_channels_rate, hdsp,
45861da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_RATE, -1);
45871da177e4SLinus Torvalds snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
45881da177e4SLinus Torvalds snd_hdsp_hw_rule_rate_in_channels, hdsp,
45891da177e4SLinus Torvalds SNDRV_PCM_HW_PARAM_CHANNELS, -1);
45901da177e4SLinus Torvalds return 0;
45911da177e4SLinus Torvalds }
45921da177e4SLinus Torvalds
snd_hdsp_capture_release(struct snd_pcm_substream * substream)459355e957d8STakashi Iwai static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
45941da177e4SLinus Torvalds {
459555e957d8STakashi Iwai struct hdsp *hdsp = snd_pcm_substream_chip(substream);
45961da177e4SLinus Torvalds
45971da177e4SLinus Torvalds spin_lock_irq(&hdsp->lock);
45981da177e4SLinus Torvalds
45991da177e4SLinus Torvalds hdsp->capture_pid = -1;
46001da177e4SLinus Torvalds hdsp->capture_substream = NULL;
46011da177e4SLinus Torvalds
46021da177e4SLinus Torvalds spin_unlock_irq(&hdsp->lock);
46031da177e4SLinus Torvalds return 0;
46041da177e4SLinus Torvalds }
46051da177e4SLinus Torvalds
46061da177e4SLinus Torvalds /* helper functions for copying meter values */
copy_u32_le(void __user * dest,void __iomem * src)46071da177e4SLinus Torvalds static inline int copy_u32_le(void __user *dest, void __iomem *src)
46081da177e4SLinus Torvalds {
46091da177e4SLinus Torvalds u32 val = readl(src);
46101da177e4SLinus Torvalds return copy_to_user(dest, &val, 4);
46111da177e4SLinus Torvalds }
46121da177e4SLinus Torvalds
copy_u64_le(void __user * dest,void __iomem * src_low,void __iomem * src_high)46131da177e4SLinus Torvalds static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
46141da177e4SLinus Torvalds {
46151da177e4SLinus Torvalds u32 rms_low, rms_high;
46161da177e4SLinus Torvalds u64 rms;
46171da177e4SLinus Torvalds rms_low = readl(src_low);
46181da177e4SLinus Torvalds rms_high = readl(src_high);
46191da177e4SLinus Torvalds rms = ((u64)rms_high << 32) | rms_low;
46201da177e4SLinus Torvalds return copy_to_user(dest, &rms, 8);
46211da177e4SLinus Torvalds }
46221da177e4SLinus Torvalds
copy_u48_le(void __user * dest,void __iomem * src_low,void __iomem * src_high)46231da177e4SLinus Torvalds static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
46241da177e4SLinus Torvalds {
46251da177e4SLinus Torvalds u32 rms_low, rms_high;
46261da177e4SLinus Torvalds u64 rms;
46271da177e4SLinus Torvalds rms_low = readl(src_low) & 0xffffff00;
46281da177e4SLinus Torvalds rms_high = readl(src_high) & 0xffffff00;
46291da177e4SLinus Torvalds rms = ((u64)rms_high << 32) | rms_low;
46301da177e4SLinus Torvalds return copy_to_user(dest, &rms, 8);
46311da177e4SLinus Torvalds }
46321da177e4SLinus Torvalds
hdsp_9652_get_peak(struct hdsp * hdsp,struct hdsp_peak_rms __user * peak_rms)463355e957d8STakashi Iwai static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
46341da177e4SLinus Torvalds {
46351da177e4SLinus Torvalds int doublespeed = 0;
46361da177e4SLinus Torvalds int i, j, channels, ofs;
46371da177e4SLinus Torvalds
46381da177e4SLinus Torvalds if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
46391da177e4SLinus Torvalds doublespeed = 1;
46401da177e4SLinus Torvalds channels = doublespeed ? 14 : 26;
46411da177e4SLinus Torvalds for (i = 0, j = 0; i < 26; ++i) {
46421da177e4SLinus Torvalds if (doublespeed && (i & 4))
46431da177e4SLinus Torvalds continue;
46441da177e4SLinus Torvalds ofs = HDSP_9652_peakBase - j * 4;
46451da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
46461da177e4SLinus Torvalds return -EFAULT;
46471da177e4SLinus Torvalds ofs -= channels * 4;
46481da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
46491da177e4SLinus Torvalds return -EFAULT;
46501da177e4SLinus Torvalds ofs -= channels * 4;
46511da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
46521da177e4SLinus Torvalds return -EFAULT;
46531da177e4SLinus Torvalds ofs = HDSP_9652_rmsBase + j * 8;
46541da177e4SLinus Torvalds if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
46551da177e4SLinus Torvalds hdsp->iobase + ofs + 4))
46561da177e4SLinus Torvalds return -EFAULT;
46571da177e4SLinus Torvalds ofs += channels * 8;
46581da177e4SLinus Torvalds if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
46591da177e4SLinus Torvalds hdsp->iobase + ofs + 4))
46601da177e4SLinus Torvalds return -EFAULT;
46611da177e4SLinus Torvalds ofs += channels * 8;
46621da177e4SLinus Torvalds if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
46631da177e4SLinus Torvalds hdsp->iobase + ofs + 4))
46641da177e4SLinus Torvalds return -EFAULT;
46651da177e4SLinus Torvalds j++;
46661da177e4SLinus Torvalds }
46671da177e4SLinus Torvalds return 0;
46681da177e4SLinus Torvalds }
46691da177e4SLinus Torvalds
hdsp_9632_get_peak(struct hdsp * hdsp,struct hdsp_peak_rms __user * peak_rms)467055e957d8STakashi Iwai static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
46711da177e4SLinus Torvalds {
46721da177e4SLinus Torvalds int i, j;
467355e957d8STakashi Iwai struct hdsp_9632_meters __iomem *m;
46741da177e4SLinus Torvalds int doublespeed = 0;
46751da177e4SLinus Torvalds
46761da177e4SLinus Torvalds if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
46771da177e4SLinus Torvalds doublespeed = 1;
467855e957d8STakashi Iwai m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
46791da177e4SLinus Torvalds for (i = 0, j = 0; i < 16; ++i, ++j) {
46801da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
46811da177e4SLinus Torvalds return -EFAULT;
46821da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
46831da177e4SLinus Torvalds return -EFAULT;
46841da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
46851da177e4SLinus Torvalds return -EFAULT;
46861da177e4SLinus Torvalds if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
46871da177e4SLinus Torvalds &m->input_rms_high[j]))
46881da177e4SLinus Torvalds return -EFAULT;
46891da177e4SLinus Torvalds if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
46901da177e4SLinus Torvalds &m->playback_rms_high[j]))
46911da177e4SLinus Torvalds return -EFAULT;
46921da177e4SLinus Torvalds if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
46931da177e4SLinus Torvalds &m->output_rms_high[j]))
46941da177e4SLinus Torvalds return -EFAULT;
46951da177e4SLinus Torvalds if (doublespeed && i == 3) i += 4;
46961da177e4SLinus Torvalds }
46971da177e4SLinus Torvalds return 0;
46981da177e4SLinus Torvalds }
46991da177e4SLinus Torvalds
hdsp_get_peak(struct hdsp * hdsp,struct hdsp_peak_rms __user * peak_rms)470055e957d8STakashi Iwai static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
47011da177e4SLinus Torvalds {
47021da177e4SLinus Torvalds int i;
47031da177e4SLinus Torvalds
47041da177e4SLinus Torvalds for (i = 0; i < 26; i++) {
47051da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->playback_peaks[i],
47061da177e4SLinus Torvalds hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
47071da177e4SLinus Torvalds return -EFAULT;
47081da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->input_peaks[i],
47091da177e4SLinus Torvalds hdsp->iobase + HDSP_inputPeakLevel + i * 4))
47101da177e4SLinus Torvalds return -EFAULT;
47111da177e4SLinus Torvalds }
47121da177e4SLinus Torvalds for (i = 0; i < 28; i++) {
47131da177e4SLinus Torvalds if (copy_u32_le(&peak_rms->output_peaks[i],
47141da177e4SLinus Torvalds hdsp->iobase + HDSP_outputPeakLevel + i * 4))
47151da177e4SLinus Torvalds return -EFAULT;
47161da177e4SLinus Torvalds }
47171da177e4SLinus Torvalds for (i = 0; i < 26; ++i) {
47181da177e4SLinus Torvalds if (copy_u64_le(&peak_rms->playback_rms[i],
47191da177e4SLinus Torvalds hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
47201da177e4SLinus Torvalds hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
47211da177e4SLinus Torvalds return -EFAULT;
47221da177e4SLinus Torvalds if (copy_u64_le(&peak_rms->input_rms[i],
47231da177e4SLinus Torvalds hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
47241da177e4SLinus Torvalds hdsp->iobase + HDSP_inputRmsLevel + i * 8))
47251da177e4SLinus Torvalds return -EFAULT;
47261da177e4SLinus Torvalds }
47271da177e4SLinus Torvalds return 0;
47281da177e4SLinus Torvalds }
47291da177e4SLinus Torvalds
snd_hdsp_hwdep_ioctl(struct snd_hwdep * hw,struct file * file,unsigned int cmd,unsigned long arg)473055e957d8STakashi Iwai static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
47311da177e4SLinus Torvalds {
47329fe856e4SJoe Perches struct hdsp *hdsp = hw->private_data;
47331da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
47343ae7e2e2STim Blechmann int err;
47351da177e4SLinus Torvalds
47361da177e4SLinus Torvalds switch (cmd) {
47371da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
473855e957d8STakashi Iwai struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
47391da177e4SLinus Torvalds
47403ae7e2e2STim Blechmann err = hdsp_check_for_iobox(hdsp);
47413ae7e2e2STim Blechmann if (err < 0)
47423ae7e2e2STim Blechmann return err;
47433ae7e2e2STim Blechmann
47443ae7e2e2STim Blechmann err = hdsp_check_for_firmware(hdsp, 1);
47453ae7e2e2STim Blechmann if (err < 0)
47463ae7e2e2STim Blechmann return err;
47473ae7e2e2STim Blechmann
47481da177e4SLinus Torvalds if (!(hdsp->state & HDSP_FirmwareLoaded)) {
4749a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
4750a54ba0feSTakashi Iwai "firmware needs to be uploaded to the card.\n");
47511da177e4SLinus Torvalds return -EINVAL;
47521da177e4SLinus Torvalds }
47531da177e4SLinus Torvalds
47541da177e4SLinus Torvalds switch (hdsp->io_type) {
47551da177e4SLinus Torvalds case H9652:
47561da177e4SLinus Torvalds return hdsp_9652_get_peak(hdsp, peak_rms);
47571da177e4SLinus Torvalds case H9632:
47581da177e4SLinus Torvalds return hdsp_9632_get_peak(hdsp, peak_rms);
47591da177e4SLinus Torvalds default:
47601da177e4SLinus Torvalds return hdsp_get_peak(hdsp, peak_rms);
47611da177e4SLinus Torvalds }
47621da177e4SLinus Torvalds }
47631da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
476455e957d8STakashi Iwai struct hdsp_config_info info;
47651da177e4SLinus Torvalds unsigned long flags;
47661da177e4SLinus Torvalds int i;
47671da177e4SLinus Torvalds
47683ae7e2e2STim Blechmann err = hdsp_check_for_iobox(hdsp);
47693ae7e2e2STim Blechmann if (err < 0)
47703ae7e2e2STim Blechmann return err;
47713ae7e2e2STim Blechmann
47723ae7e2e2STim Blechmann err = hdsp_check_for_firmware(hdsp, 1);
47733ae7e2e2STim Blechmann if (err < 0)
47743ae7e2e2STim Blechmann return err;
47753ae7e2e2STim Blechmann
4776e68d3b31SDan Rosenberg memset(&info, 0, sizeof(info));
47771da177e4SLinus Torvalds spin_lock_irqsave(&hdsp->lock, flags);
47781da177e4SLinus Torvalds info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
47791da177e4SLinus Torvalds info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
4780b0b98119STakashi Iwai if (hdsp->io_type != H9632)
47811da177e4SLinus Torvalds info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
47821da177e4SLinus Torvalds info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
478328b26e15SFlorian Faber for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
47841da177e4SLinus Torvalds info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
47851da177e4SLinus Torvalds info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
47864833c673SAdrian Knoth info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
47874833c673SAdrian Knoth HDSP_SPDIFOpticalOut);
47884833c673SAdrian Knoth info.spdif_professional = (unsigned char)
47894833c673SAdrian Knoth hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
47904833c673SAdrian Knoth info.spdif_emphasis = (unsigned char)
47914833c673SAdrian Knoth hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
47924833c673SAdrian Knoth info.spdif_nonaudio = (unsigned char)
47934833c673SAdrian Knoth hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
47941da177e4SLinus Torvalds info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
47951da177e4SLinus Torvalds info.system_sample_rate = hdsp->system_sample_rate;
47961da177e4SLinus Torvalds info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
47971da177e4SLinus Torvalds info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
47981da177e4SLinus Torvalds info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
47991da177e4SLinus Torvalds info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
48004833c673SAdrian Knoth info.line_out = (unsigned char)
48014833c673SAdrian Knoth hdsp_toggle_setting(hdsp, HDSP_LineOut);
48021da177e4SLinus Torvalds if (hdsp->io_type == H9632) {
48031da177e4SLinus Torvalds info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
48041da177e4SLinus Torvalds info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
48051da177e4SLinus Torvalds info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
48064833c673SAdrian Knoth info.xlr_breakout_cable =
48074833c673SAdrian Knoth (unsigned char)hdsp_toggle_setting(hdsp,
48084833c673SAdrian Knoth HDSP_XLRBreakoutCable);
48091da177e4SLinus Torvalds
481028b26e15SFlorian Faber } else if (hdsp->io_type == RPM) {
481128b26e15SFlorian Faber info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
481228b26e15SFlorian Faber info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
48131da177e4SLinus Torvalds }
4814b0b98119STakashi Iwai if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
48154833c673SAdrian Knoth info.analog_extension_board =
48164833c673SAdrian Knoth (unsigned char)hdsp_toggle_setting(hdsp,
48174833c673SAdrian Knoth HDSP_AnalogExtensionBoard);
48181da177e4SLinus Torvalds spin_unlock_irqrestore(&hdsp->lock, flags);
48191da177e4SLinus Torvalds if (copy_to_user(argp, &info, sizeof(info)))
48201da177e4SLinus Torvalds return -EFAULT;
48211da177e4SLinus Torvalds break;
48221da177e4SLinus Torvalds }
48231da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
482455e957d8STakashi Iwai struct hdsp_9632_aeb h9632_aeb;
48251da177e4SLinus Torvalds
48261da177e4SLinus Torvalds if (hdsp->io_type != H9632) return -EINVAL;
48271da177e4SLinus Torvalds h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
48281da177e4SLinus Torvalds h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
48291da177e4SLinus Torvalds if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
48301da177e4SLinus Torvalds return -EFAULT;
48311da177e4SLinus Torvalds break;
48321da177e4SLinus Torvalds }
48331da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_GET_VERSION: {
483455e957d8STakashi Iwai struct hdsp_version hdsp_version;
48351da177e4SLinus Torvalds int err;
48361da177e4SLinus Torvalds
48371da177e4SLinus Torvalds if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
48381da177e4SLinus Torvalds if (hdsp->io_type == Undefined) {
483966c8f759STakashi Iwai err = hdsp_get_iobox_version(hdsp);
484066c8f759STakashi Iwai if (err < 0)
48411da177e4SLinus Torvalds return err;
48421da177e4SLinus Torvalds }
4843d14df339SDan Carpenter memset(&hdsp_version, 0, sizeof(hdsp_version));
48441da177e4SLinus Torvalds hdsp_version.io_type = hdsp->io_type;
48451da177e4SLinus Torvalds hdsp_version.firmware_rev = hdsp->firmware_rev;
48460f7c9565SDan Carpenter if (copy_to_user(argp, &hdsp_version, sizeof(hdsp_version)))
48471da177e4SLinus Torvalds return -EFAULT;
48481da177e4SLinus Torvalds break;
48491da177e4SLinus Torvalds }
48501da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
4851112e3f5aSTakashi Iwai struct hdsp_firmware firmware;
48521da177e4SLinus Torvalds u32 __user *firmware_data;
48531da177e4SLinus Torvalds int err;
48541da177e4SLinus Torvalds
48551da177e4SLinus Torvalds if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
48561da177e4SLinus Torvalds /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
48571da177e4SLinus Torvalds if (hdsp->io_type == Undefined) return -EINVAL;
48581da177e4SLinus Torvalds
48591da177e4SLinus Torvalds if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
48601da177e4SLinus Torvalds return -EBUSY;
48611da177e4SLinus Torvalds
4862a54ba0feSTakashi Iwai dev_info(hdsp->card->dev,
4863a54ba0feSTakashi Iwai "initializing firmware upload\n");
4864112e3f5aSTakashi Iwai if (copy_from_user(&firmware, argp, sizeof(firmware)))
48651da177e4SLinus Torvalds return -EFAULT;
4866112e3f5aSTakashi Iwai firmware_data = (u32 __user *)firmware.firmware_data;
48671da177e4SLinus Torvalds
4868b0b98119STakashi Iwai if (hdsp_check_for_iobox (hdsp))
48691da177e4SLinus Torvalds return -EIO;
48701da177e4SLinus Torvalds
487190caaef6STakashi Iwai if (!hdsp->fw_uploaded) {
487290caaef6STakashi Iwai hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
487390caaef6STakashi Iwai if (!hdsp->fw_uploaded)
487490caaef6STakashi Iwai return -ENOMEM;
487590caaef6STakashi Iwai }
487690caaef6STakashi Iwai
487790caaef6STakashi Iwai if (copy_from_user(hdsp->fw_uploaded, firmware_data,
487890caaef6STakashi Iwai HDSP_FIRMWARE_SIZE)) {
487990caaef6STakashi Iwai vfree(hdsp->fw_uploaded);
488090caaef6STakashi Iwai hdsp->fw_uploaded = NULL;
48811da177e4SLinus Torvalds return -EFAULT;
488290caaef6STakashi Iwai }
48831da177e4SLinus Torvalds
48841da177e4SLinus Torvalds hdsp->state |= HDSP_FirmwareCached;
48851da177e4SLinus Torvalds
488666c8f759STakashi Iwai err = snd_hdsp_load_firmware_from_cache(hdsp);
488766c8f759STakashi Iwai if (err < 0)
48881da177e4SLinus Torvalds return err;
48891da177e4SLinus Torvalds
48901da177e4SLinus Torvalds if (!(hdsp->state & HDSP_InitializationComplete)) {
489166c8f759STakashi Iwai err = snd_hdsp_enable_io(hdsp);
489266c8f759STakashi Iwai if (err < 0)
48931da177e4SLinus Torvalds return err;
48941da177e4SLinus Torvalds
48951da177e4SLinus Torvalds snd_hdsp_initialize_channels(hdsp);
48961da177e4SLinus Torvalds snd_hdsp_initialize_midi_flush(hdsp);
48971da177e4SLinus Torvalds
489866c8f759STakashi Iwai err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp);
489966c8f759STakashi Iwai if (err < 0) {
4900a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
4901a54ba0feSTakashi Iwai "error creating alsa devices\n");
49021da177e4SLinus Torvalds return err;
49031da177e4SLinus Torvalds }
49041da177e4SLinus Torvalds }
49051da177e4SLinus Torvalds break;
49061da177e4SLinus Torvalds }
49071da177e4SLinus Torvalds case SNDRV_HDSP_IOCTL_GET_MIXER: {
490855e957d8STakashi Iwai struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
49091da177e4SLinus Torvalds if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
49101da177e4SLinus Torvalds return -EFAULT;
49111da177e4SLinus Torvalds break;
49121da177e4SLinus Torvalds }
49131da177e4SLinus Torvalds default:
49141da177e4SLinus Torvalds return -EINVAL;
49151da177e4SLinus Torvalds }
49161da177e4SLinus Torvalds return 0;
49171da177e4SLinus Torvalds }
49181da177e4SLinus Torvalds
49196769e988SJulia Lawall static const struct snd_pcm_ops snd_hdsp_playback_ops = {
49201da177e4SLinus Torvalds .open = snd_hdsp_playback_open,
49211da177e4SLinus Torvalds .close = snd_hdsp_playback_release,
49221da177e4SLinus Torvalds .ioctl = snd_hdsp_ioctl,
49231da177e4SLinus Torvalds .hw_params = snd_hdsp_hw_params,
49241da177e4SLinus Torvalds .prepare = snd_hdsp_prepare,
49251da177e4SLinus Torvalds .trigger = snd_hdsp_trigger,
49261da177e4SLinus Torvalds .pointer = snd_hdsp_hw_pointer,
492790ed2311STakashi Iwai .copy = snd_hdsp_playback_copy,
49280925e6b2STakashi Iwai .fill_silence = snd_hdsp_hw_silence,
49291da177e4SLinus Torvalds };
49301da177e4SLinus Torvalds
49316769e988SJulia Lawall static const struct snd_pcm_ops snd_hdsp_capture_ops = {
49321da177e4SLinus Torvalds .open = snd_hdsp_capture_open,
49331da177e4SLinus Torvalds .close = snd_hdsp_capture_release,
49341da177e4SLinus Torvalds .ioctl = snd_hdsp_ioctl,
49351da177e4SLinus Torvalds .hw_params = snd_hdsp_hw_params,
49361da177e4SLinus Torvalds .prepare = snd_hdsp_prepare,
49371da177e4SLinus Torvalds .trigger = snd_hdsp_trigger,
49381da177e4SLinus Torvalds .pointer = snd_hdsp_hw_pointer,
493990ed2311STakashi Iwai .copy = snd_hdsp_capture_copy,
49401da177e4SLinus Torvalds };
49411da177e4SLinus Torvalds
snd_hdsp_create_hwdep(struct snd_card * card,struct hdsp * hdsp)494292eed66dSTakashi Iwai static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
49431da177e4SLinus Torvalds {
494455e957d8STakashi Iwai struct snd_hwdep *hw;
49451da177e4SLinus Torvalds int err;
49461da177e4SLinus Torvalds
494766c8f759STakashi Iwai err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw);
494866c8f759STakashi Iwai if (err < 0)
49491da177e4SLinus Torvalds return err;
49501da177e4SLinus Torvalds
49511da177e4SLinus Torvalds hdsp->hwdep = hw;
49521da177e4SLinus Torvalds hw->private_data = hdsp;
49531da177e4SLinus Torvalds strcpy(hw->name, "HDSP hwdep interface");
49541da177e4SLinus Torvalds
49551da177e4SLinus Torvalds hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
495642eb9238SAndre Schramm hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
49571da177e4SLinus Torvalds
49581da177e4SLinus Torvalds return 0;
49591da177e4SLinus Torvalds }
49601da177e4SLinus Torvalds
snd_hdsp_create_pcm(struct snd_card * card,struct hdsp * hdsp)496155e957d8STakashi Iwai static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
49621da177e4SLinus Torvalds {
496355e957d8STakashi Iwai struct snd_pcm *pcm;
49641da177e4SLinus Torvalds int err;
49651da177e4SLinus Torvalds
496666c8f759STakashi Iwai err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm);
496766c8f759STakashi Iwai if (err < 0)
49681da177e4SLinus Torvalds return err;
49691da177e4SLinus Torvalds
49701da177e4SLinus Torvalds hdsp->pcm = pcm;
49711da177e4SLinus Torvalds pcm->private_data = hdsp;
49721da177e4SLinus Torvalds strcpy(pcm->name, hdsp->card_name);
49731da177e4SLinus Torvalds
49741da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
49751da177e4SLinus Torvalds snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
49761da177e4SLinus Torvalds
49771da177e4SLinus Torvalds pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
49781da177e4SLinus Torvalds
49791da177e4SLinus Torvalds return 0;
49801da177e4SLinus Torvalds }
49811da177e4SLinus Torvalds
snd_hdsp_9652_enable_mixer(struct hdsp * hdsp)498255e957d8STakashi Iwai static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
49831da177e4SLinus Torvalds {
49841da177e4SLinus Torvalds hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
49851da177e4SLinus Torvalds hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
49861da177e4SLinus Torvalds }
49871da177e4SLinus Torvalds
snd_hdsp_enable_io(struct hdsp * hdsp)498855e957d8STakashi Iwai static int snd_hdsp_enable_io (struct hdsp *hdsp)
49891da177e4SLinus Torvalds {
49901da177e4SLinus Torvalds int i;
49911da177e4SLinus Torvalds
49921da177e4SLinus Torvalds if (hdsp_fifo_wait (hdsp, 0, 100)) {
4993a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
4994a54ba0feSTakashi Iwai "enable_io fifo_wait failed\n");
49951da177e4SLinus Torvalds return -EIO;
49961da177e4SLinus Torvalds }
49971da177e4SLinus Torvalds
49981da177e4SLinus Torvalds for (i = 0; i < hdsp->max_channels; ++i) {
49991da177e4SLinus Torvalds hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
50001da177e4SLinus Torvalds hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
50011da177e4SLinus Torvalds }
50021da177e4SLinus Torvalds
50031da177e4SLinus Torvalds return 0;
50041da177e4SLinus Torvalds }
50051da177e4SLinus Torvalds
snd_hdsp_initialize_channels(struct hdsp * hdsp)500655e957d8STakashi Iwai static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
50071da177e4SLinus Torvalds {
5008da2a040eSJasmin Fazlic int status, aebi_channels, aebo_channels, i;
50091da177e4SLinus Torvalds
50101da177e4SLinus Torvalds switch (hdsp->io_type) {
50111da177e4SLinus Torvalds case Digiface:
50121da177e4SLinus Torvalds hdsp->card_name = "RME Hammerfall DSP + Digiface";
50131da177e4SLinus Torvalds hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
50141da177e4SLinus Torvalds hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
50151da177e4SLinus Torvalds break;
50161da177e4SLinus Torvalds
50171da177e4SLinus Torvalds case H9652:
50181da177e4SLinus Torvalds hdsp->card_name = "RME Hammerfall HDSP 9652";
50191da177e4SLinus Torvalds hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
50201da177e4SLinus Torvalds hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
50211da177e4SLinus Torvalds break;
50221da177e4SLinus Torvalds
50231da177e4SLinus Torvalds case H9632:
50241da177e4SLinus Torvalds status = hdsp_read(hdsp, HDSP_statusRegister);
50251da177e4SLinus Torvalds /* HDSP_AEBx bits are low when AEB are connected */
50261da177e4SLinus Torvalds aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
50271da177e4SLinus Torvalds aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
50281da177e4SLinus Torvalds hdsp->card_name = "RME Hammerfall HDSP 9632";
50291da177e4SLinus Torvalds hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
50301da177e4SLinus Torvalds hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
50311da177e4SLinus Torvalds hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
50321da177e4SLinus Torvalds hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
50331da177e4SLinus Torvalds hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
50341da177e4SLinus Torvalds hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
5035da2a040eSJasmin Fazlic /* Disable loopback of output channels, as the set function
5036da2a040eSJasmin Fazlic * only sets on a change we fake all bits (channels) as enabled.
5037da2a040eSJasmin Fazlic */
5038da2a040eSJasmin Fazlic hdsp->io_loopback = 0xffffffff;
5039da2a040eSJasmin Fazlic for (i = 0; i < hdsp->max_channels; ++i)
5040da2a040eSJasmin Fazlic hdsp_loopback_set(hdsp, i, false);
50411da177e4SLinus Torvalds break;
50421da177e4SLinus Torvalds
50431da177e4SLinus Torvalds case Multiface:
50441da177e4SLinus Torvalds hdsp->card_name = "RME Hammerfall DSP + Multiface";
50451da177e4SLinus Torvalds hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
50461da177e4SLinus Torvalds hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
50471da177e4SLinus Torvalds break;
50481da177e4SLinus Torvalds
504928b26e15SFlorian Faber case RPM:
505028b26e15SFlorian Faber hdsp->card_name = "RME Hammerfall DSP + RPM";
505128b26e15SFlorian Faber hdsp->ss_in_channels = RPM_CHANNELS-1;
505228b26e15SFlorian Faber hdsp->ss_out_channels = RPM_CHANNELS;
505328b26e15SFlorian Faber hdsp->ds_in_channels = RPM_CHANNELS-1;
505428b26e15SFlorian Faber hdsp->ds_out_channels = RPM_CHANNELS;
505528b26e15SFlorian Faber break;
505628b26e15SFlorian Faber
50571da177e4SLinus Torvalds default:
50581da177e4SLinus Torvalds /* should never get here */
50591da177e4SLinus Torvalds break;
50601da177e4SLinus Torvalds }
50611da177e4SLinus Torvalds }
50621da177e4SLinus Torvalds
snd_hdsp_initialize_midi_flush(struct hdsp * hdsp)506355e957d8STakashi Iwai static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
50641da177e4SLinus Torvalds {
50651da177e4SLinus Torvalds snd_hdsp_flush_midi_input (hdsp, 0);
50661da177e4SLinus Torvalds snd_hdsp_flush_midi_input (hdsp, 1);
50671da177e4SLinus Torvalds }
50681da177e4SLinus Torvalds
snd_hdsp_create_alsa_devices(struct snd_card * card,struct hdsp * hdsp)506955e957d8STakashi Iwai static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
50701da177e4SLinus Torvalds {
50711da177e4SLinus Torvalds int err;
50721da177e4SLinus Torvalds
507366c8f759STakashi Iwai err = snd_hdsp_create_pcm(card, hdsp);
507466c8f759STakashi Iwai if (err < 0) {
5075a54ba0feSTakashi Iwai dev_err(card->dev,
5076a54ba0feSTakashi Iwai "Error creating pcm interface\n");
50771da177e4SLinus Torvalds return err;
50781da177e4SLinus Torvalds }
50791da177e4SLinus Torvalds
50801da177e4SLinus Torvalds
508166c8f759STakashi Iwai err = snd_hdsp_create_midi(card, hdsp, 0);
508266c8f759STakashi Iwai if (err < 0) {
5083a54ba0feSTakashi Iwai dev_err(card->dev,
5084a54ba0feSTakashi Iwai "Error creating first midi interface\n");
50851da177e4SLinus Torvalds return err;
50861da177e4SLinus Torvalds }
50871da177e4SLinus Torvalds
50881da177e4SLinus Torvalds if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
508966c8f759STakashi Iwai err = snd_hdsp_create_midi(card, hdsp, 1);
509066c8f759STakashi Iwai if (err < 0) {
5091a54ba0feSTakashi Iwai dev_err(card->dev,
5092a54ba0feSTakashi Iwai "Error creating second midi interface\n");
50931da177e4SLinus Torvalds return err;
50941da177e4SLinus Torvalds }
50951da177e4SLinus Torvalds }
50961da177e4SLinus Torvalds
509766c8f759STakashi Iwai err = snd_hdsp_create_controls(card, hdsp);
509866c8f759STakashi Iwai if (err < 0) {
5099a54ba0feSTakashi Iwai dev_err(card->dev,
5100a54ba0feSTakashi Iwai "Error creating ctl interface\n");
51011da177e4SLinus Torvalds return err;
51021da177e4SLinus Torvalds }
51031da177e4SLinus Torvalds
51041da177e4SLinus Torvalds snd_hdsp_proc_init(hdsp);
51051da177e4SLinus Torvalds
51061da177e4SLinus Torvalds hdsp->system_sample_rate = -1;
51071da177e4SLinus Torvalds hdsp->playback_pid = -1;
51081da177e4SLinus Torvalds hdsp->capture_pid = -1;
51091da177e4SLinus Torvalds hdsp->capture_substream = NULL;
51101da177e4SLinus Torvalds hdsp->playback_substream = NULL;
51111da177e4SLinus Torvalds
511266c8f759STakashi Iwai err = snd_hdsp_set_defaults(hdsp);
511366c8f759STakashi Iwai if (err < 0) {
5114a54ba0feSTakashi Iwai dev_err(card->dev,
5115a54ba0feSTakashi Iwai "Error setting default values\n");
51161da177e4SLinus Torvalds return err;
51171da177e4SLinus Torvalds }
51181da177e4SLinus Torvalds
51191da177e4SLinus Torvalds if (!(hdsp->state & HDSP_InitializationComplete)) {
5120b73c1c12SClemens Ladisch strcpy(card->shortname, "Hammerfall DSP");
51211da177e4SLinus Torvalds sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
51221da177e4SLinus Torvalds hdsp->port, hdsp->irq);
51231da177e4SLinus Torvalds
512466c8f759STakashi Iwai err = snd_card_register(card);
512566c8f759STakashi Iwai if (err < 0) {
5126a54ba0feSTakashi Iwai dev_err(card->dev,
5127a54ba0feSTakashi Iwai "error registering card\n");
51281da177e4SLinus Torvalds return err;
51291da177e4SLinus Torvalds }
51301da177e4SLinus Torvalds hdsp->state |= HDSP_InitializationComplete;
51311da177e4SLinus Torvalds }
51321da177e4SLinus Torvalds
51331da177e4SLinus Torvalds return 0;
51341da177e4SLinus Torvalds }
51351da177e4SLinus Torvalds
51361da177e4SLinus Torvalds /* load firmware via hotplug fw loader */
hdsp_request_fw_loader(struct hdsp * hdsp)513792eed66dSTakashi Iwai static int hdsp_request_fw_loader(struct hdsp *hdsp)
51381da177e4SLinus Torvalds {
51391da177e4SLinus Torvalds const char *fwfile;
51401da177e4SLinus Torvalds const struct firmware *fw;
51411da177e4SLinus Torvalds int err;
51421da177e4SLinus Torvalds
51431da177e4SLinus Torvalds if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
51441da177e4SLinus Torvalds return 0;
51451da177e4SLinus Torvalds if (hdsp->io_type == Undefined) {
514666c8f759STakashi Iwai err = hdsp_get_iobox_version(hdsp);
514766c8f759STakashi Iwai if (err < 0)
51481da177e4SLinus Torvalds return err;
51491da177e4SLinus Torvalds if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
51501da177e4SLinus Torvalds return 0;
51511da177e4SLinus Torvalds }
51521da177e4SLinus Torvalds
51531da177e4SLinus Torvalds /* caution: max length of firmware filename is 30! */
51541da177e4SLinus Torvalds switch (hdsp->io_type) {
515528b26e15SFlorian Faber case RPM:
515628b26e15SFlorian Faber fwfile = "rpm_firmware.bin";
515728b26e15SFlorian Faber break;
51581da177e4SLinus Torvalds case Multiface:
51591da177e4SLinus Torvalds if (hdsp->firmware_rev == 0xa)
51601da177e4SLinus Torvalds fwfile = "multiface_firmware.bin";
51611da177e4SLinus Torvalds else
51621da177e4SLinus Torvalds fwfile = "multiface_firmware_rev11.bin";
51631da177e4SLinus Torvalds break;
51641da177e4SLinus Torvalds case Digiface:
51651da177e4SLinus Torvalds if (hdsp->firmware_rev == 0xa)
51661da177e4SLinus Torvalds fwfile = "digiface_firmware.bin";
51671da177e4SLinus Torvalds else
51681da177e4SLinus Torvalds fwfile = "digiface_firmware_rev11.bin";
51691da177e4SLinus Torvalds break;
51701da177e4SLinus Torvalds default:
5171a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5172a54ba0feSTakashi Iwai "invalid io_type %d\n", hdsp->io_type);
51731da177e4SLinus Torvalds return -EINVAL;
51741da177e4SLinus Torvalds }
51751da177e4SLinus Torvalds
51761da177e4SLinus Torvalds if (request_firmware(&fw, fwfile, &hdsp->pci->dev)) {
5177a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5178a54ba0feSTakashi Iwai "cannot load firmware %s\n", fwfile);
51791da177e4SLinus Torvalds return -ENOENT;
51801da177e4SLinus Torvalds }
518190caaef6STakashi Iwai if (fw->size < HDSP_FIRMWARE_SIZE) {
5182a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5183a54ba0feSTakashi Iwai "too short firmware size %d (expected %d)\n",
518490caaef6STakashi Iwai (int)fw->size, HDSP_FIRMWARE_SIZE);
518529d28c50SSudip Mukherjee release_firmware(fw);
51861da177e4SLinus Torvalds return -EINVAL;
51871da177e4SLinus Torvalds }
51887679a030SThomas Charbonnel
518990caaef6STakashi Iwai hdsp->firmware = fw;
51901da177e4SLinus Torvalds
51911da177e4SLinus Torvalds hdsp->state |= HDSP_FirmwareCached;
51921da177e4SLinus Torvalds
519366c8f759STakashi Iwai err = snd_hdsp_load_firmware_from_cache(hdsp);
519466c8f759STakashi Iwai if (err < 0)
51951da177e4SLinus Torvalds return err;
51961da177e4SLinus Torvalds
51971da177e4SLinus Torvalds if (!(hdsp->state & HDSP_InitializationComplete)) {
519866c8f759STakashi Iwai err = snd_hdsp_enable_io(hdsp);
519966c8f759STakashi Iwai if (err < 0)
52001da177e4SLinus Torvalds return err;
52011da177e4SLinus Torvalds
520266c8f759STakashi Iwai err = snd_hdsp_create_hwdep(hdsp->card, hdsp);
520366c8f759STakashi Iwai if (err < 0) {
5204a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5205a54ba0feSTakashi Iwai "error creating hwdep device\n");
52061da177e4SLinus Torvalds return err;
52071da177e4SLinus Torvalds }
52081da177e4SLinus Torvalds snd_hdsp_initialize_channels(hdsp);
52091da177e4SLinus Torvalds snd_hdsp_initialize_midi_flush(hdsp);
521066c8f759STakashi Iwai err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp);
521166c8f759STakashi Iwai if (err < 0) {
5212a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5213a54ba0feSTakashi Iwai "error creating alsa devices\n");
52141da177e4SLinus Torvalds return err;
52151da177e4SLinus Torvalds }
52161da177e4SLinus Torvalds }
52171da177e4SLinus Torvalds return 0;
52181da177e4SLinus Torvalds }
52191da177e4SLinus Torvalds
snd_hdsp_create(struct snd_card * card,struct hdsp * hdsp)5220e23e7a14SBill Pemberton static int snd_hdsp_create(struct snd_card *card,
522155e957d8STakashi Iwai struct hdsp *hdsp)
52221da177e4SLinus Torvalds {
52231da177e4SLinus Torvalds struct pci_dev *pci = hdsp->pci;
52241da177e4SLinus Torvalds int err;
52251da177e4SLinus Torvalds int is_9652 = 0;
52261da177e4SLinus Torvalds int is_9632 = 0;
52271da177e4SLinus Torvalds
52281da177e4SLinus Torvalds hdsp->irq = -1;
52291da177e4SLinus Torvalds hdsp->state = 0;
52301da177e4SLinus Torvalds hdsp->midi[0].rmidi = NULL;
52311da177e4SLinus Torvalds hdsp->midi[1].rmidi = NULL;
52321da177e4SLinus Torvalds hdsp->midi[0].input = NULL;
52331da177e4SLinus Torvalds hdsp->midi[1].input = NULL;
52341da177e4SLinus Torvalds hdsp->midi[0].output = NULL;
52351da177e4SLinus Torvalds hdsp->midi[1].output = NULL;
52361da177e4SLinus Torvalds hdsp->midi[0].pending = 0;
52371da177e4SLinus Torvalds hdsp->midi[1].pending = 0;
52381da177e4SLinus Torvalds spin_lock_init(&hdsp->midi[0].lock);
52391da177e4SLinus Torvalds spin_lock_init(&hdsp->midi[1].lock);
52401da177e4SLinus Torvalds hdsp->iobase = NULL;
52411da177e4SLinus Torvalds hdsp->control_register = 0;
52421da177e4SLinus Torvalds hdsp->control2_register = 0;
52431da177e4SLinus Torvalds hdsp->io_type = Undefined;
52441da177e4SLinus Torvalds hdsp->max_channels = 26;
52451da177e4SLinus Torvalds
52461da177e4SLinus Torvalds hdsp->card = card;
52471da177e4SLinus Torvalds
52481da177e4SLinus Torvalds spin_lock_init(&hdsp->lock);
52491da177e4SLinus Torvalds
52504adab848STakashi Iwai INIT_WORK(&hdsp->midi_work, hdsp_midi_work);
52511da177e4SLinus Torvalds
52521da177e4SLinus Torvalds pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
52531da177e4SLinus Torvalds hdsp->firmware_rev &= 0xff;
52541da177e4SLinus Torvalds
52551da177e4SLinus Torvalds /* From Martin Bjoernsen :
52561da177e4SLinus Torvalds "It is important that the card's latency timer register in
52571da177e4SLinus Torvalds the PCI configuration space is set to a value much larger
52581da177e4SLinus Torvalds than 0 by the computer's BIOS or the driver.
52591da177e4SLinus Torvalds The windows driver always sets this 8 bit register [...]
52601da177e4SLinus Torvalds to its maximum 255 to avoid problems with some computers."
52611da177e4SLinus Torvalds */
52621da177e4SLinus Torvalds pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
52631da177e4SLinus Torvalds
52641da177e4SLinus Torvalds strcpy(card->driver, "H-DSP");
52651da177e4SLinus Torvalds strcpy(card->mixername, "Xilinx FPGA");
52661da177e4SLinus Torvalds
5267b0b98119STakashi Iwai if (hdsp->firmware_rev < 0xa)
52681da177e4SLinus Torvalds return -ENODEV;
5269b0b98119STakashi Iwai else if (hdsp->firmware_rev < 0x64)
52701da177e4SLinus Torvalds hdsp->card_name = "RME Hammerfall DSP";
5271b0b98119STakashi Iwai else if (hdsp->firmware_rev < 0x96) {
52721da177e4SLinus Torvalds hdsp->card_name = "RME HDSP 9652";
52731da177e4SLinus Torvalds is_9652 = 1;
52741da177e4SLinus Torvalds } else {
52751da177e4SLinus Torvalds hdsp->card_name = "RME HDSP 9632";
52761da177e4SLinus Torvalds hdsp->max_channels = 16;
52771da177e4SLinus Torvalds is_9632 = 1;
52781da177e4SLinus Torvalds }
52791da177e4SLinus Torvalds
5280d136b8e5STakashi Iwai err = pcim_enable_device(pci);
528166c8f759STakashi Iwai if (err < 0)
52821da177e4SLinus Torvalds return err;
52831da177e4SLinus Torvalds
52841da177e4SLinus Torvalds pci_set_master(hdsp->pci);
52851da177e4SLinus Torvalds
528666c8f759STakashi Iwai err = pci_request_regions(pci, "hdsp");
528766c8f759STakashi Iwai if (err < 0)
52881da177e4SLinus Torvalds return err;
52891da177e4SLinus Torvalds hdsp->port = pci_resource_start(pci, 0);
5290d136b8e5STakashi Iwai hdsp->iobase = devm_ioremap(&pci->dev, hdsp->port, HDSP_IO_EXTENT);
529166c8f759STakashi Iwai if (!hdsp->iobase) {
5292a54ba0feSTakashi Iwai dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
5293a54ba0feSTakashi Iwai hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
52941da177e4SLinus Torvalds return -EBUSY;
52951da177e4SLinus Torvalds }
52961da177e4SLinus Torvalds
5297d136b8e5STakashi Iwai if (devm_request_irq(&pci->dev, pci->irq, snd_hdsp_interrupt,
5298d136b8e5STakashi Iwai IRQF_SHARED, KBUILD_MODNAME, hdsp)) {
5299a54ba0feSTakashi Iwai dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
53001da177e4SLinus Torvalds return -EBUSY;
53011da177e4SLinus Torvalds }
53021da177e4SLinus Torvalds
53031da177e4SLinus Torvalds hdsp->irq = pci->irq;
530439cccf45STakashi Iwai card->sync_irq = hdsp->irq;
5305176546abSRemy Bruno hdsp->precise_ptr = 0;
53064adab848STakashi Iwai hdsp->use_midi_work = 1;
5307d7923b2aSRemy Bruno hdsp->dds_value = 0;
53081da177e4SLinus Torvalds
530966c8f759STakashi Iwai err = snd_hdsp_initialize_memory(hdsp);
531066c8f759STakashi Iwai if (err < 0)
53111da177e4SLinus Torvalds return err;
53121da177e4SLinus Torvalds
53131da177e4SLinus Torvalds if (!is_9652 && !is_9632) {
5314e588ed83STim Blechmann /* we wait a maximum of 10 seconds to let freshly
5315e588ed83STim Blechmann * inserted cardbus cards do their hardware init */
5316e588ed83STim Blechmann err = hdsp_wait_for_iobox(hdsp, 1000, 10);
53171da177e4SLinus Torvalds
531800c9ddd1STim Blechmann if (err < 0)
531900c9ddd1STim Blechmann return err;
532000c9ddd1STim Blechmann
53211da177e4SLinus Torvalds if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
532266c8f759STakashi Iwai err = hdsp_request_fw_loader(hdsp);
532366c8f759STakashi Iwai if (err < 0)
53241da177e4SLinus Torvalds /* we don't fail as this can happen
53251da177e4SLinus Torvalds if userspace is not ready for
53261da177e4SLinus Torvalds firmware upload
53271da177e4SLinus Torvalds */
5328a54ba0feSTakashi Iwai dev_err(hdsp->card->dev,
5329a54ba0feSTakashi Iwai "couldn't get firmware from userspace. try using hdsploader\n");
5330b0b98119STakashi Iwai else
53311da177e4SLinus Torvalds /* init is complete, we return */
53321da177e4SLinus Torvalds return 0;
533300c9ddd1STim Blechmann /* we defer initialization */
5334a54ba0feSTakashi Iwai dev_info(hdsp->card->dev,
5335a54ba0feSTakashi Iwai "card initialization pending : waiting for firmware\n");
533666c8f759STakashi Iwai err = snd_hdsp_create_hwdep(card, hdsp);
533766c8f759STakashi Iwai if (err < 0)
53381da177e4SLinus Torvalds return err;
53391da177e4SLinus Torvalds return 0;
53401da177e4SLinus Torvalds } else {
5341a54ba0feSTakashi Iwai dev_info(hdsp->card->dev,
5342a54ba0feSTakashi Iwai "Firmware already present, initializing card.\n");
534328b26e15SFlorian Faber if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
534428b26e15SFlorian Faber hdsp->io_type = RPM;
534528b26e15SFlorian Faber else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
53461da177e4SLinus Torvalds hdsp->io_type = Multiface;
5347b0b98119STakashi Iwai else
53481da177e4SLinus Torvalds hdsp->io_type = Digiface;
53491da177e4SLinus Torvalds }
53501da177e4SLinus Torvalds }
53511da177e4SLinus Torvalds
535266c8f759STakashi Iwai err = snd_hdsp_enable_io(hdsp);
535366c8f759STakashi Iwai if (err)
53541da177e4SLinus Torvalds return err;
53551da177e4SLinus Torvalds
5356b0b98119STakashi Iwai if (is_9652)
53571da177e4SLinus Torvalds hdsp->io_type = H9652;
53581da177e4SLinus Torvalds
5359b0b98119STakashi Iwai if (is_9632)
53601da177e4SLinus Torvalds hdsp->io_type = H9632;
53611da177e4SLinus Torvalds
536266c8f759STakashi Iwai err = snd_hdsp_create_hwdep(card, hdsp);
536366c8f759STakashi Iwai if (err < 0)
53641da177e4SLinus Torvalds return err;
53651da177e4SLinus Torvalds
53661da177e4SLinus Torvalds snd_hdsp_initialize_channels(hdsp);
53671da177e4SLinus Torvalds snd_hdsp_initialize_midi_flush(hdsp);
53681da177e4SLinus Torvalds
53691da177e4SLinus Torvalds hdsp->state |= HDSP_FirmwareLoaded;
53701da177e4SLinus Torvalds
537166c8f759STakashi Iwai err = snd_hdsp_create_alsa_devices(card, hdsp);
537266c8f759STakashi Iwai if (err < 0)
53731da177e4SLinus Torvalds return err;
53741da177e4SLinus Torvalds
53751da177e4SLinus Torvalds return 0;
53761da177e4SLinus Torvalds }
53771da177e4SLinus Torvalds
snd_hdsp_card_free(struct snd_card * card)5378d136b8e5STakashi Iwai static void snd_hdsp_card_free(struct snd_card *card)
53791da177e4SLinus Torvalds {
5380d136b8e5STakashi Iwai struct hdsp *hdsp = card->private_data;
5381d136b8e5STakashi Iwai
53821da177e4SLinus Torvalds if (hdsp->port) {
53831da177e4SLinus Torvalds /* stop the audio, and cancel all interrupts */
53844adab848STakashi Iwai cancel_work_sync(&hdsp->midi_work);
53851da177e4SLinus Torvalds hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
53861da177e4SLinus Torvalds hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
53871da177e4SLinus Torvalds }
53881da177e4SLinus Torvalds
538990caaef6STakashi Iwai release_firmware(hdsp->firmware);
539090caaef6STakashi Iwai vfree(hdsp->fw_uploaded);
53911da177e4SLinus Torvalds }
53921da177e4SLinus Torvalds
snd_hdsp_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)5393e23e7a14SBill Pemberton static int snd_hdsp_probe(struct pci_dev *pci,
53941da177e4SLinus Torvalds const struct pci_device_id *pci_id)
53951da177e4SLinus Torvalds {
53961da177e4SLinus Torvalds static int dev;
539755e957d8STakashi Iwai struct hdsp *hdsp;
539855e957d8STakashi Iwai struct snd_card *card;
53991da177e4SLinus Torvalds int err;
54001da177e4SLinus Torvalds
54011da177e4SLinus Torvalds if (dev >= SNDRV_CARDS)
54021da177e4SLinus Torvalds return -ENODEV;
54031da177e4SLinus Torvalds if (!enable[dev]) {
54041da177e4SLinus Torvalds dev++;
54051da177e4SLinus Torvalds return -ENOENT;
54061da177e4SLinus Torvalds }
54071da177e4SLinus Torvalds
5408d136b8e5STakashi Iwai err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
5409e58de7baSTakashi Iwai sizeof(struct hdsp), &card);
5410e58de7baSTakashi Iwai if (err < 0)
5411e58de7baSTakashi Iwai return err;
54121da177e4SLinus Torvalds
54139fe856e4SJoe Perches hdsp = card->private_data;
54141da177e4SLinus Torvalds card->private_free = snd_hdsp_card_free;
54151da177e4SLinus Torvalds hdsp->dev = dev;
54161da177e4SLinus Torvalds hdsp->pci = pci;
54172675be5aSMarkus Elfring err = snd_hdsp_create(card, hdsp);
54182675be5aSMarkus Elfring if (err)
5419e2263f0bSTakashi Iwai goto error;
54201da177e4SLinus Torvalds
54211da177e4SLinus Torvalds strcpy(card->shortname, "Hammerfall DSP");
54221da177e4SLinus Torvalds sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
54231da177e4SLinus Torvalds hdsp->port, hdsp->irq);
54242675be5aSMarkus Elfring err = snd_card_register(card);
5425d136b8e5STakashi Iwai if (err)
5426e2263f0bSTakashi Iwai goto error;
54271da177e4SLinus Torvalds pci_set_drvdata(pci, card);
54281da177e4SLinus Torvalds dev++;
54291da177e4SLinus Torvalds return 0;
5430e2263f0bSTakashi Iwai
5431e2263f0bSTakashi Iwai error:
5432e2263f0bSTakashi Iwai snd_card_free(card);
5433e2263f0bSTakashi Iwai return err;
54341da177e4SLinus Torvalds }
54351da177e4SLinus Torvalds
5436e9f66d9bSTakashi Iwai static struct pci_driver hdsp_driver = {
54373733e424STakashi Iwai .name = KBUILD_MODNAME,
54381da177e4SLinus Torvalds .id_table = snd_hdsp_ids,
54391da177e4SLinus Torvalds .probe = snd_hdsp_probe,
54401da177e4SLinus Torvalds };
54411da177e4SLinus Torvalds
5442e9f66d9bSTakashi Iwai module_pci_driver(hdsp_driver);
5443