source: GPL/trunk/alsa-kernel/pci/cmipci.c

Last change on this file was 777, checked in by David Azarewicz, 6 months ago

Merge from uniaud32-exp branch

File size: 102.0 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Driver for C-Media CMI8338 and 8738 PCI soundcards.
4 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
5 */
6
7/* Does not work. Warning may block system in capture mode */
8/* #define USE_VAR48KRATE */
9
10#include <linux/io.h>
11#include <linux/delay.h>
12#include <linux/interrupt.h>
13#include <linux/init.h>
14#include <linux/pci.h>
15#include <linux/slab.h>
16#include <linux/gameport.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19#include <sound/core.h>
20#include <sound/info.h>
21#include <sound/control.h>
22#include <sound/pcm.h>
23#include <sound/rawmidi.h>
24#include <sound/mpu401.h>
25#include <sound/opl3.h>
26#include <sound/sb.h>
27#include <sound/asoundef.h>
28#include <sound/initval.h>
29
30#ifdef TARGET_OS2
31#define KBUILD_MODNAME "cmipci"
32#endif
33MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
34MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
35MODULE_LICENSE("GPL");
36
37#if IS_REACHABLE(CONFIG_GAMEPORT)
38#define SUPPORT_JOYSTICK 1
39#endif
40
41static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
42static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
43static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
44#ifndef TARGET_OS2
45static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
47static bool soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
48#else
49static long mpu_port[SNDRV_CARDS] = {0,1,1,1,1,1,1,1};
50static long fm_port[SNDRV_CARDS] = {0,1,1,1,1,1,1,1};
51static bool soft_ac3[SNDRV_CARDS] = {0,1,1,1,1,1,1,1};
52#endif
53#ifdef SUPPORT_JOYSTICK
54static int joystick_port[SNDRV_CARDS];
55#endif
56
57module_param_array(index, int, NULL, 0444);
58MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
59module_param_array(id, charp, NULL, 0444);
60MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
61module_param_array(enable, bool, NULL, 0444);
62MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
63module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
64MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
65module_param_hw_array(fm_port, long, ioport, NULL, 0444);
66MODULE_PARM_DESC(fm_port, "FM port.");
67module_param_array(soft_ac3, bool, NULL, 0444);
68MODULE_PARM_DESC(soft_ac3, "Software-conversion of raw SPDIF packets (model 033 only).");
69#ifdef SUPPORT_JOYSTICK
70module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
71MODULE_PARM_DESC(joystick_port, "Joystick port address.");
72#endif
73
74/*
75 * CM8x38 registers definition
76 */
77
78#define CM_REG_FUNCTRL0 0x00
79#define CM_RST_CH1 0x00080000
80#define CM_RST_CH0 0x00040000
81#define CM_CHEN1 0x00020000 /* ch1: enable */
82#define CM_CHEN0 0x00010000 /* ch0: enable */
83#define CM_PAUSE1 0x00000008 /* ch1: pause */
84#define CM_PAUSE0 0x00000004 /* ch0: pause */
85#define CM_CHADC1 0x00000002 /* ch1, 0:playback, 1:record */
86#define CM_CHADC0 0x00000001 /* ch0, 0:playback, 1:record */
87
88#define CM_REG_FUNCTRL1 0x04
89#define CM_DSFC_MASK 0x0000E000 /* channel 1 (DAC?) sampling frequency */
90#define CM_DSFC_SHIFT 13
91#define CM_ASFC_MASK 0x00001C00 /* channel 0 (ADC?) sampling frequency */
92#define CM_ASFC_SHIFT 10
93#define CM_SPDF_1 0x00000200 /* SPDIF IN/OUT at channel B */
94#define CM_SPDF_0 0x00000100 /* SPDIF OUT only channel A */
95#define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/IN -> OUT loopback */
96#define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
97#define CM_INTRM 0x00000020 /* master control block (MCB) interrupt enabled */
98#define CM_BREQ 0x00000010 /* bus master enabled */
99#define CM_VOICE_EN 0x00000008 /* legacy voice (SB16,FM) */
100#define CM_UART_EN 0x00000004 /* legacy UART */
101#define CM_JYSTK_EN 0x00000002 /* legacy joystick */
102#define CM_ZVPORT 0x00000001 /* ZVPORT */
103
104#define CM_REG_CHFORMAT 0x08
105
106#define CM_CHB3D5C 0x80000000 /* 5,6 channels */
107#define CM_FMOFFSET2 0x40000000 /* initial FM PCM offset 2 when Fmute=1 */
108#define CM_CHB3D 0x20000000 /* 4 channels */
109
110#define CM_CHIP_MASK1 0x1f000000
111#define CM_CHIP_037 0x01000000
112#define CM_SETLAT48 0x00800000 /* set latency timer 48h */
113#define CM_EDGEIRQ 0x00400000 /* emulated edge trigger legacy IRQ */
114#define CM_SPD24SEL39 0x00200000 /* 24-bit spdif: model 039 */
115#define CM_AC3EN1 0x00100000 /* enable AC3: model 037 */
116#define CM_SPDIF_SELECT1 0x00080000 /* for model <= 037 ? */
117#define CM_SPD24SEL 0x00020000 /* 24bit spdif: model 037 */
118/* #define CM_SPDIF_INVERSE 0x00010000 */ /* ??? */
119
120#define CM_ADCBITLEN_MASK 0x0000C000
121#define CM_ADCBITLEN_16 0x00000000
122#define CM_ADCBITLEN_15 0x00004000
123#define CM_ADCBITLEN_14 0x00008000
124#define CM_ADCBITLEN_13 0x0000C000
125
126#define CM_ADCDACLEN_MASK 0x00003000 /* model 037 */
127#define CM_ADCDACLEN_060 0x00000000
128#define CM_ADCDACLEN_066 0x00001000
129#define CM_ADCDACLEN_130 0x00002000
130#define CM_ADCDACLEN_280 0x00003000
131
132#define CM_ADCDLEN_MASK 0x00003000 /* model 039 */
133#define CM_ADCDLEN_ORIGINAL 0x00000000
134#define CM_ADCDLEN_EXTRA 0x00001000
135#define CM_ADCDLEN_24K 0x00002000
136#define CM_ADCDLEN_WEIGHT 0x00003000
137
138#define CM_CH1_SRATE_176K 0x00000800
139#define CM_CH1_SRATE_96K 0x00000800 /* model 055? */
140#define CM_CH1_SRATE_88K 0x00000400
141#define CM_CH0_SRATE_176K 0x00000200
142#define CM_CH0_SRATE_96K 0x00000200 /* model 055? */
143#define CM_CH0_SRATE_88K 0x00000100
144#define CM_CH0_SRATE_128K 0x00000300
145#define CM_CH0_SRATE_MASK 0x00000300
146
147#define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
148#define CM_DBLSPDS 0x00000040 /* double SPDIF sample rate 88.2/96 */
149#define CM_POLVALID 0x00000020 /* inverse SPDIF/IN valid bit */
150#define CM_SPDLOCKED 0x00000010
151
152#define CM_CH1FMT_MASK 0x0000000C /* bit 3: 16 bits, bit 2: stereo */
153#define CM_CH1FMT_SHIFT 2
154#define CM_CH0FMT_MASK 0x00000003 /* bit 1: 16 bits, bit 0: stereo */
155#define CM_CH0FMT_SHIFT 0
156
157#define CM_REG_INT_HLDCLR 0x0C
158#define CM_CHIP_MASK2 0xff000000
159#define CM_CHIP_8768 0x20000000
160#define CM_CHIP_055 0x08000000
161#define CM_CHIP_039 0x04000000
162#define CM_CHIP_039_6CH 0x01000000
163#define CM_UNKNOWN_INT_EN 0x00080000 /* ? */
164#define CM_TDMA_INT_EN 0x00040000
165#define CM_CH1_INT_EN 0x00020000
166#define CM_CH0_INT_EN 0x00010000
167
168#define CM_REG_INT_STATUS 0x10
169#define CM_INTR 0x80000000
170#define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
171#define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
172#define CM_UARTINT 0x00010000
173#define CM_LTDMAINT 0x00008000
174#define CM_HTDMAINT 0x00004000
175#define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
176#define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
177#define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
178#define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
179#define CM_CH1BUSY 0x00000008
180#define CM_CH0BUSY 0x00000004
181#define CM_CHINT1 0x00000002
182#define CM_CHINT0 0x00000001
183
184#define CM_REG_LEGACY_CTRL 0x14
185#define CM_NXCHG 0x80000000 /* don't map base reg dword->sample */
186#define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
187#define CM_VMPU_330 0x00000000
188#define CM_VMPU_320 0x20000000
189#define CM_VMPU_310 0x40000000
190#define CM_VMPU_300 0x60000000
191#define CM_ENWR8237 0x10000000 /* enable bus master to write 8237 base reg */
192#define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
193#define CM_VSBSEL_220 0x00000000
194#define CM_VSBSEL_240 0x04000000
195#define CM_VSBSEL_260 0x08000000
196#define CM_VSBSEL_280 0x0C000000
197#define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
198#define CM_FMSEL_388 0x00000000
199#define CM_FMSEL_3C8 0x01000000
200#define CM_FMSEL_3E0 0x02000000
201#define CM_FMSEL_3E8 0x03000000
202#define CM_ENSPDOUT 0x00800000 /* enable XSPDIF/OUT to I/O interface */
203#define CM_SPDCOPYRHT 0x00400000 /* spdif in/out copyright bit */
204#define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
205#define CM_INVIDWEN 0x00100000 /* internal vendor ID write enable, model 039? */
206#define CM_SETRETRY 0x00100000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
207#define CM_C_EEACCESS 0x00080000 /* direct programming eeprom regs */
208#define CM_C_EECS 0x00040000
209#define CM_C_EEDI46 0x00020000
210#define CM_C_EECK46 0x00010000
211#define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
212#define CM_CENTR2LIN 0x00004000 /* line-in as center out */
213#define CM_BASE2LIN 0x00002000 /* line-in as bass out */
214#define CM_EXBASEN 0x00001000 /* external bass input enable */
215
216#define CM_REG_MISC_CTRL 0x18
217#define CM_PWD 0x80000000 /* power down */
218#define CM_RESET 0x40000000
219#define CM_SFIL_MASK 0x30000000 /* filter control at front end DAC, model 037? */
220#define CM_VMGAIN 0x10000000 /* analog master amp +6dB, model 039? */
221#define CM_TXVX 0x08000000 /* model 037? */
222#define CM_N4SPK3D 0x04000000 /* copy front to rear */
223#define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
224#define CM_SPDIF48K 0x01000000 /* write */
225#define CM_SPATUS48K 0x01000000 /* read */
226#define CM_ENDBDAC 0x00800000 /* enable double dac */
227#define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
228#define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
229#define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
230#define CM_FM_EN 0x00080000 /* enable legacy FM */
231#define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
232#define CM_ENWRASID 0x00010000 /* choose writable internal SUBID (audio) */
233#define CM_VIDWPDSB 0x00010000 /* model 037? */
234#define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
235#define CM_MASK_EN 0x00004000 /* activate channel mask on legacy DMA */
236#define CM_ENWRMSID 0x00002000 /* choose writable internal SUBID (modem) */
237#define CM_VIDWPPRT 0x00002000 /* model 037? */
238#define CM_SFILENB 0x00001000 /* filter stepping at front end DAC, model 037? */
239#define CM_MMODE_MASK 0x00000E00 /* model DAA interface mode */
240#define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
241#define CM_ENCENTER 0x00000080
242#define CM_FLINKON 0x00000040 /* force modem link detection on, model 037 */
243#define CM_MUTECH1 0x00000040 /* mute PCI ch1 to DAC */
244#define CM_FLINKOFF 0x00000020 /* force modem link detection off, model 037 */
245#define CM_MIDSMP 0x00000010 /* 1/2 interpolation at front end DAC */
246#define CM_UPDDMA_MASK 0x0000000C /* TDMA position update notification */
247#define CM_UPDDMA_2048 0x00000000
248#define CM_UPDDMA_1024 0x00000004
249#define CM_UPDDMA_512 0x00000008
250#define CM_UPDDMA_256 0x0000000C
251#define CM_TWAIT_MASK 0x00000003 /* model 037 */
252#define CM_TWAIT1 0x00000002 /* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
253#define CM_TWAIT0 0x00000001 /* i/o cycle, 0: 4, 1: 6 PCICLKs */
254
255#define CM_REG_TDMA_POSITION 0x1C
256#define CM_TDMA_CNT_MASK 0xFFFF0000 /* current byte/word count */
257#define CM_TDMA_ADR_MASK 0x0000FFFF /* current address */
258
259 /* byte */
260#define CM_REG_MIXER0 0x20
261#define CM_REG_SBVR 0x20 /* write: sb16 version */
262#define CM_REG_DEV 0x20 /* read: hardware device version */
263
264#define CM_REG_MIXER21 0x21
265#define CM_UNKNOWN_21_MASK 0x78 /* ? */
266#define CM_X_ADPCM 0x04 /* SB16 ADPCM enable */
267#define CM_PROINV 0x02 /* SBPro left/right channel switching */
268#define CM_X_SB16 0x01 /* SB16 compatible */
269
270#define CM_REG_SB16_DATA 0x22
271#define CM_REG_SB16_ADDR 0x23
272
273#define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
274#define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
275#define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
276#define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
277
278#define CM_REG_MIXER1 0x24
279#define CM_FMMUTE 0x80 /* mute FM */
280#define CM_FMMUTE_SHIFT 7
281#define CM_WSMUTE 0x40 /* mute PCM */
282#define CM_WSMUTE_SHIFT 6
283#define CM_REAR2LIN 0x20 /* lin-in -> rear line out */
284#define CM_REAR2LIN_SHIFT 5
285#define CM_REAR2FRONT 0x10 /* exchange rear/front */
286#define CM_REAR2FRONT_SHIFT 4
287#define CM_WAVEINL 0x08 /* digital wave rec. left chan */
288#define CM_WAVEINL_SHIFT 3
289#define CM_WAVEINR 0x04 /* digical wave rec. right */
290#define CM_WAVEINR_SHIFT 2
291#define CM_X3DEN 0x02 /* 3D surround enable */
292#define CM_X3DEN_SHIFT 1
293#define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
294#define CM_CDPLAY_SHIFT 0
295
296#define CM_REG_MIXER2 0x25
297#define CM_RAUXREN 0x80 /* AUX right capture */
298#define CM_RAUXREN_SHIFT 7
299#define CM_RAUXLEN 0x40 /* AUX left capture */
300#define CM_RAUXLEN_SHIFT 6
301#define CM_VAUXRM 0x20 /* AUX right mute */
302#define CM_VAUXRM_SHIFT 5
303#define CM_VAUXLM 0x10 /* AUX left mute */
304#define CM_VAUXLM_SHIFT 4
305#define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
306#define CM_VADMIC_SHIFT 1
307#define CM_MICGAINZ 0x01 /* mic boost */
308#define CM_MICGAINZ_SHIFT 0
309
310#define CM_REG_AUX_VOL 0x26
311#define CM_VAUXL_MASK 0xf0
312#define CM_VAUXR_MASK 0x0f
313
314#define CM_REG_MISC 0x27
315#define CM_UNKNOWN_27_MASK 0xd8 /* ? */
316#define CM_XGPO1 0x20
317// #define CM_XGPBIO 0x04
318#define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
319#define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
320#define CM_SPDVALID 0x02 /* spdif input valid check */
321#define CM_DMAUTO 0x01 /* SB16 DMA auto detect */
322
323#define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
324/*
325 * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
326 * or identical with AC97 codec?
327 */
328#define CM_REG_EXTERN_CODEC CM_REG_AC97
329
330/*
331 * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
332 */
333#define CM_REG_MPU_PCI 0x40
334
335/*
336 * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
337 */
338#define CM_REG_FM_PCI 0x50
339
340/*
341 * access from SB-mixer port
342 */
343#define CM_REG_EXTENT_IND 0xf0
344#define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
345#define CM_VPHONE_SHIFT 5
346#define CM_VPHOM 0x10 /* Phone mute control */
347#define CM_VSPKM 0x08 /* Speaker mute control, default high */
348#define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
349#define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
350#define CM_VADMIC3 0x01 /* Mic record boost */
351
352/*
353 * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
354 * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
355 * unit (readonly?).
356 */
357#define CM_REG_PLL 0xf8
358
359/*
360 * extended registers
361 */
362#define CM_REG_CH0_FRAME1 0x80 /* write: base address */
363#define CM_REG_CH0_FRAME2 0x84 /* read: current address */
364#define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
365#define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
366
367#define CM_REG_EXT_MISC 0x90
368#define CM_ADC48K44K 0x10000000 /* ADC parameters group, 0: 44k, 1: 48k */
369#define CM_CHB3D8C 0x00200000 /* 7.1 channels support */
370#define CM_SPD32FMT 0x00100000 /* SPDIF/IN 32k sample rate */
371#define CM_ADC2SPDIF 0x00080000 /* ADC output to SPDIF/OUT */
372#define CM_SHAREADC 0x00040000 /* DAC in ADC as Center/LFE */
373#define CM_REALTCMP 0x00020000 /* monitor the CMPL/CMPR of ADC */
374#define CM_INVLRCK 0x00010000 /* invert ZVPORT's LRCK */
375#define CM_UNKNOWN_90_MASK 0x0000FFFF /* ? */
376
377/*
378 * size of i/o region
379 */
380#define CM_EXTENT_CODEC 0x100
381#define CM_EXTENT_MIDI 0x2
382#define CM_EXTENT_SYNTH 0x4
383
384
385/*
386 * channels for playback / capture
387 */
388#define CM_CH_PLAY 0
389#define CM_CH_CAPT 1
390
391/*
392 * flags to check device open/close
393 */
394#define CM_OPEN_NONE 0
395#define CM_OPEN_CH_MASK 0x01
396#define CM_OPEN_DAC 0x10
397#define CM_OPEN_ADC 0x20
398#define CM_OPEN_SPDIF 0x40
399#define CM_OPEN_MCHAN 0x80
400#define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
401#define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
402#define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
403#define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
404#define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
405#define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
406
407
408#if CM_CH_PLAY == 1
409#define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
410#define CM_PLAYBACK_SPDF CM_SPDF_1
411#define CM_CAPTURE_SPDF CM_SPDF_0
412#else
413#define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
414#define CM_PLAYBACK_SPDF CM_SPDF_0
415#define CM_CAPTURE_SPDF CM_SPDF_1
416#endif
417
418
419/*
420 * driver data
421 */
422
423struct cmipci_pcm {
424 struct snd_pcm_substream *substream;
425 u8 running; /* dac/adc running? */
426 u8 fmt; /* format bits */
427 u8 is_dac;
428 u8 needs_silencing;
429 unsigned int dma_size; /* in frames */
430 unsigned int shift;
431 unsigned int ch; /* channel (0/1) */
432 unsigned int offset; /* physical address of the buffer */
433};
434
435/* mixer elements toggled/resumed during ac3 playback */
436struct cmipci_mixer_auto_switches {
437 const char *name; /* switch to toggle */
438 int toggle_on; /* value to change when ac3 mode */
439};
440static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
441 {"PCM Playback Switch", 0},
442 {"IEC958 Output Switch", 1},
443 {"IEC958 Mix Analog", 0},
444 // {"IEC958 Out To DAC", 1}, // no longer used
445 {"IEC958 Loop", 0},
446};
447#define CM_SAVED_MIXERS ARRAY_SIZE(cm_saved_mixer)
448
449struct cmipci {
450 struct snd_card *card;
451
452 struct pci_dev *pci;
453 unsigned int device; /* device ID */
454 int irq;
455
456 unsigned long iobase;
457 unsigned int ctrl; /* FUNCTRL0 current value */
458
459 struct snd_pcm *pcm; /* DAC/ADC PCM */
460 struct snd_pcm *pcm2; /* 2nd DAC */
461 struct snd_pcm *pcm_spdif; /* SPDIF */
462
463 int chip_version;
464 int max_channels;
465 unsigned int can_ac3_sw: 1;
466 unsigned int can_ac3_hw: 1;
467 unsigned int can_multi_ch: 1;
468 unsigned int can_96k: 1; /* samplerate above 48k */
469 unsigned int do_soft_ac3: 1;
470
471 unsigned int spdif_playback_avail: 1; /* spdif ready? */
472 unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
473 int spdif_counter; /* for software AC3 */
474
475 unsigned int dig_status;
476 unsigned int dig_pcm_status;
477
478 struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
479
480 int opened[2]; /* open mode */
481 struct mutex open_mutex;
482
483 unsigned int mixer_insensitive: 1;
484 struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
485 int mixer_res_status[CM_SAVED_MIXERS];
486
487 struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
488
489 /* external MIDI */
490 struct snd_rawmidi *rmidi;
491
492#ifdef SUPPORT_JOYSTICK
493 struct gameport *gameport;
494#endif
495
496 spinlock_t reg_lock;
497
498 unsigned int saved_regs[0x20];
499 unsigned char saved_mixers[0x20];
500};
501
502
503/* read/write operations for dword register */
504static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
505{
506 outl(data, cm->iobase + cmd);
507}
508
509static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
510{
511 return inl(cm->iobase + cmd);
512}
513
514/* read/write operations for word register */
515static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
516{
517 outw(data, cm->iobase + cmd);
518}
519
520static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
521{
522 return inw(cm->iobase + cmd);
523}
524
525/* read/write operations for byte register */
526static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
527{
528 outb(data, cm->iobase + cmd);
529}
530
531static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
532{
533 return inb(cm->iobase + cmd);
534}
535
536/* bit operations for dword register */
537static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
538{
539 unsigned int val, oval;
540 val = oval = inl(cm->iobase + cmd);
541 val |= flag;
542 if (val == oval)
543 return 0;
544 outl(val, cm->iobase + cmd);
545 return 1;
546}
547
548static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
549{
550 unsigned int val, oval;
551 val = oval = inl(cm->iobase + cmd);
552 val &= ~flag;
553 if (val == oval)
554 return 0;
555 outl(val, cm->iobase + cmd);
556 return 1;
557}
558
559/* bit operations for byte register */
560static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
561{
562 unsigned char val, oval;
563 val = oval = inb(cm->iobase + cmd);
564 val |= flag;
565 if (val == oval)
566 return 0;
567 outb(val, cm->iobase + cmd);
568 return 1;
569}
570
571static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
572{
573 unsigned char val, oval;
574 val = oval = inb(cm->iobase + cmd);
575 val &= ~flag;
576 if (val == oval)
577 return 0;
578 outb(val, cm->iobase + cmd);
579 return 1;
580}
581
582
583/*
584 * PCM interface
585 */
586
587/*
588 * calculate frequency
589 */
590
591static const unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
592
593static unsigned int snd_cmipci_rate_freq(unsigned int rate)
594{
595 unsigned int i;
596
597 for (i = 0; i < ARRAY_SIZE(rates); i++) {
598 if (rates[i] == rate)
599 return i;
600 }
601 snd_BUG();
602 return 0;
603}
604
605#ifdef USE_VAR48KRATE
606/*
607 * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
608 * does it this way .. maybe not. Never get any information from C-Media about
609 * that <werner@suse.de>.
610 */
611static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
612{
613 unsigned int delta, tolerance;
614 int xm, xn, xr;
615
616 for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
617 rate <<= 1;
618 *n = -1;
619 if (*r > 0xff)
620 goto out;
621 tolerance = rate*CM_TOLERANCE_RATE;
622
623 for (xn = (1+2); xn < (0x1f+2); xn++) {
624 for (xm = (1+2); xm < (0xff+2); xm++) {
625 xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
626
627 if (xr < rate)
628 delta = rate - xr;
629 else
630 delta = xr - rate;
631
632 /*
633 * If we found one, remember this,
634 * and try to find a closer one
635 */
636 if (delta < tolerance) {
637 tolerance = delta;
638 *m = xm - 2;
639 *n = xn - 2;
640 }
641 }
642 }
643out:
644 return (*n > -1);
645}
646
647/*
648 * Program pll register bits, I assume that the 8 registers 0xf8 up to 0xff
649 * are mapped onto the 8 ADC/DAC sampling frequency which can be chosen
650 * at the register CM_REG_FUNCTRL1 (0x04).
651 * Problem: other ways are also possible (any information about that?)
652 */
653static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
654{
655 unsigned int reg = CM_REG_PLL + slot;
656 /*
657 * Guess that this programs at reg. 0x04 the pos 15:13/12:10
658 * for DSFC/ASFC (000 up to 111).
659 */
660
661 /* FIXME: Init (Do we've to set an other register first before programming?) */
662
663 /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
664 snd_cmipci_write_b(cm, reg, rate>>8);
665 snd_cmipci_write_b(cm, reg, rate&0xff);
666
667 /* FIXME: Setup (Do we've to set an other register first to enable this?) */
668}
669#endif /* USE_VAR48KRATE */
670
671static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
672 struct snd_pcm_hw_params *hw_params)
673{
674 struct cmipci *cm = snd_pcm_substream_chip(substream);
675 if (params_channels(hw_params) > 2) {
676 mutex_lock(&cm->open_mutex);
677 if (cm->opened[CM_CH_PLAY]) {
678 mutex_unlock(&cm->open_mutex);
679 return -EBUSY;
680 }
681 /* reserve the channel A */
682 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
683 mutex_unlock(&cm->open_mutex);
684 }
685 return 0;
686}
687
688static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
689{
690 int reset = CM_RST_CH0 << (cm->channel[ch].ch);
691 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
692 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
693 udelay(10);
694}
695
696
697/*
698 */
699
700static const unsigned int hw_channels[] = {1, 2, 4, 6, 8};
701static const struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
702 .count = 3,
703 .list = hw_channels,
704 .mask = 0,
705};
706static const struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
707 .count = 4,
708 .list = hw_channels,
709 .mask = 0,
710};
711static const struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
712 .count = 5,
713 .list = hw_channels,
714 .mask = 0,
715};
716
717static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
718{
719 if (channels > 2) {
720 if (!cm->can_multi_ch || !rec->ch)
721 return -EINVAL;
722 if (rec->fmt != 0x03) /* stereo 16bit only */
723 return -EINVAL;
724 }
725
726 if (cm->can_multi_ch) {
727 spin_lock_irq(&cm->reg_lock);
728 if (channels > 2) {
729 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
730 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
731 } else {
732 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
733 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
734 }
735 if (channels == 8)
736 snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
737 else
738 snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
739 if (channels == 6) {
740 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
741 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
742 } else {
743 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
744 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
745 }
746 if (channels == 4)
747 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
748 else
749 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
750 spin_unlock_irq(&cm->reg_lock);
751 }
752 return 0;
753}
754
755
756/*
757 * prepare playback/capture channel
758 * channel to be used must have been set in rec->ch.
759 */
760static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
761 struct snd_pcm_substream *substream)
762{
763 unsigned int reg, freq, freq_ext, val;
764 unsigned int period_size;
765 struct snd_pcm_runtime *runtime = substream->runtime;
766
767 rec->fmt = 0;
768 rec->shift = 0;
769 if (snd_pcm_format_width(runtime->format) >= 16) {
770 rec->fmt |= 0x02;
771 if (snd_pcm_format_width(runtime->format) > 16)
772 rec->shift++; /* 24/32bit */
773 }
774 if (runtime->channels > 1)
775 rec->fmt |= 0x01;
776 if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
777 dev_dbg(cm->card->dev, "cannot set dac channels\n");
778 return -EINVAL;
779 }
780
781 rec->offset = runtime->dma_addr;
782 /* buffer and period sizes in frame */
783 rec->dma_size = runtime->buffer_size << rec->shift;
784 period_size = runtime->period_size << rec->shift;
785 if (runtime->channels > 2) {
786 /* multi-channels */
787 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
788 period_size = (period_size * runtime->channels) / 2;
789 }
790
791 spin_lock_irq(&cm->reg_lock);
792
793 /* set buffer address */
794 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
795 snd_cmipci_write(cm, reg, rec->offset);
796 /* program sample counts */
797 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
798 snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
799 snd_cmipci_write_w(cm, reg + 2, period_size - 1);
800
801 /* set adc/dac flag */
802 val = rec->ch ? CM_CHADC1 : CM_CHADC0;
803 if (rec->is_dac)
804 cm->ctrl &= ~val;
805 else
806 cm->ctrl |= val;
807 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
808 /* dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl); */
809
810 /* set sample rate */
811 freq = 0;
812 freq_ext = 0;
813 if (runtime->rate > 48000)
814 switch (runtime->rate) {
815 case 88200: freq_ext = CM_CH0_SRATE_88K; break;
816 case 96000: freq_ext = CM_CH0_SRATE_96K; break;
817 case 128000: freq_ext = CM_CH0_SRATE_128K; break;
818 default: snd_BUG(); break;
819 }
820 else
821 freq = snd_cmipci_rate_freq(runtime->rate);
822 val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
823 if (rec->ch) {
824 val &= ~CM_DSFC_MASK;
825 val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
826 } else {
827 val &= ~CM_ASFC_MASK;
828 val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
829 }
830 snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
831 dev_dbg(cm->card->dev, "functrl1 = %08x\n", val);
832
833 /* set format */
834 val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
835 if (rec->ch) {
836 val &= ~CM_CH1FMT_MASK;
837 val |= rec->fmt << CM_CH1FMT_SHIFT;
838 } else {
839 val &= ~CM_CH0FMT_MASK;
840 val |= rec->fmt << CM_CH0FMT_SHIFT;
841 }
842 if (cm->can_96k) {
843 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
844 val |= freq_ext << (rec->ch * 2);
845 }
846 snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
847 dev_dbg(cm->card->dev, "chformat = %08x\n", val);
848
849 if (!rec->is_dac && cm->chip_version) {
850 if (runtime->rate > 44100)
851 snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
852 else
853 snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
854 }
855
856 rec->running = 0;
857 spin_unlock_irq(&cm->reg_lock);
858
859 return 0;
860}
861
862/*
863 * PCM trigger/stop
864 */
865static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
866 int cmd)
867{
868 unsigned int inthld, chen, reset, pause;
869 int result = 0;
870
871 inthld = CM_CH0_INT_EN << rec->ch;
872 chen = CM_CHEN0 << rec->ch;
873 reset = CM_RST_CH0 << rec->ch;
874 pause = CM_PAUSE0 << rec->ch;
875
876 spin_lock(&cm->reg_lock);
877 switch (cmd) {
878 case SNDRV_PCM_TRIGGER_START:
879 rec->running = 1;
880 /* set interrupt */
881 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
882 cm->ctrl |= chen;
883 /* enable channel */
884 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
885 dev_dbg(cm->card->dev, "functrl0 = %08x\n", cm->ctrl);
886 break;
887 case SNDRV_PCM_TRIGGER_STOP:
888 rec->running = 0;
889 /* disable interrupt */
890 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
891 /* reset */
892 cm->ctrl &= ~chen;
893 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
894 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
895 rec->needs_silencing = rec->is_dac;
896 break;
897 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
898 case SNDRV_PCM_TRIGGER_SUSPEND:
899 cm->ctrl |= pause;
900 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
901 break;
902 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
903 case SNDRV_PCM_TRIGGER_RESUME:
904 cm->ctrl &= ~pause;
905 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
906 break;
907 default:
908 result = -EINVAL;
909 break;
910 }
911 spin_unlock(&cm->reg_lock);
912 return result;
913}
914
915/*
916 * return the current pointer
917 */
918static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
919 struct snd_pcm_substream *substream)
920{
921 size_t ptr;
922 unsigned int reg, rem, tries;
923
924 if (!rec->running)
925 return 0;
926#if 1 // this seems better..
927 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
928 for (tries = 0; tries < 3; tries++) {
929 rem = snd_cmipci_read_w(cm, reg);
930 if (rem < rec->dma_size)
931 goto ok;
932 }
933 dev_err(cm->card->dev, "invalid PCM pointer: %#x\n", rem);
934 return SNDRV_PCM_POS_XRUN;
935ok:
936 ptr = (rec->dma_size - (rem + 1)) >> rec->shift;
937#else
938 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
939 ptr = snd_cmipci_read(cm, reg) - rec->offset;
940 ptr = bytes_to_frames(substream->runtime, ptr);
941#endif
942 if (substream->runtime->channels > 2)
943 ptr = (ptr * 2) / substream->runtime->channels;
944 return ptr;
945}
946
947/*
948 * playback
949 */
950
951static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
952 int cmd)
953{
954 struct cmipci *cm = snd_pcm_substream_chip(substream);
955 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
956}
957
958static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
959{
960 struct cmipci *cm = snd_pcm_substream_chip(substream);
961 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
962}
963
964
965
966/*
967 * capture
968 */
969
970static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
971 int cmd)
972{
973 struct cmipci *cm = snd_pcm_substream_chip(substream);
974 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
975}
976
977static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
978{
979 struct cmipci *cm = snd_pcm_substream_chip(substream);
980 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
981}
982
983
984/*
985 * hw preparation for spdif
986 */
987
988static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
989 struct snd_ctl_elem_info *uinfo)
990{
991 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
992 uinfo->count = 1;
993 return 0;
994}
995
996static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
997 struct snd_ctl_elem_value *ucontrol)
998{
999 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1000 int i;
1001
1002 spin_lock_irq(&chip->reg_lock);
1003 for (i = 0; i < 4; i++)
1004 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
1005 spin_unlock_irq(&chip->reg_lock);
1006 return 0;
1007}
1008
1009static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
1010 struct snd_ctl_elem_value *ucontrol)
1011{
1012 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1013 int i, change;
1014 unsigned int val;
1015
1016 val = 0;
1017 spin_lock_irq(&chip->reg_lock);
1018 for (i = 0; i < 4; i++)
1019 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1020 change = val != chip->dig_status;
1021 chip->dig_status = val;
1022 spin_unlock_irq(&chip->reg_lock);
1023 return change;
1024}
1025
1026static const struct snd_kcontrol_new snd_cmipci_spdif_default =
1027{
1028 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1029 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1030 .info = snd_cmipci_spdif_default_info,
1031 .get = snd_cmipci_spdif_default_get,
1032 .put = snd_cmipci_spdif_default_put
1033};
1034
1035static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
1036 struct snd_ctl_elem_info *uinfo)
1037{
1038 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1039 uinfo->count = 1;
1040 return 0;
1041}
1042
1043static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
1045{
1046 ucontrol->value.iec958.status[0] = 0xff;
1047 ucontrol->value.iec958.status[1] = 0xff;
1048 ucontrol->value.iec958.status[2] = 0xff;
1049 ucontrol->value.iec958.status[3] = 0xff;
1050 return 0;
1051}
1052
1053static const struct snd_kcontrol_new snd_cmipci_spdif_mask =
1054{
1055 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1056 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1057 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1058 .info = snd_cmipci_spdif_mask_info,
1059 .get = snd_cmipci_spdif_mask_get,
1060};
1061
1062static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
1063 struct snd_ctl_elem_info *uinfo)
1064{
1065 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1066 uinfo->count = 1;
1067 return 0;
1068}
1069
1070static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1071 struct snd_ctl_elem_value *ucontrol)
1072{
1073 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1074 int i;
1075
1076 spin_lock_irq(&chip->reg_lock);
1077 for (i = 0; i < 4; i++)
1078 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1079 spin_unlock_irq(&chip->reg_lock);
1080 return 0;
1081}
1082
1083static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1085{
1086 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
1087 int i, change;
1088 unsigned int val;
1089
1090 val = 0;
1091 spin_lock_irq(&chip->reg_lock);
1092 for (i = 0; i < 4; i++)
1093 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1094 change = val != chip->dig_pcm_status;
1095 chip->dig_pcm_status = val;
1096 spin_unlock_irq(&chip->reg_lock);
1097 return change;
1098}
1099
1100static const struct snd_kcontrol_new snd_cmipci_spdif_stream =
1101{
1102 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1103 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1104 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1105 .info = snd_cmipci_spdif_stream_info,
1106 .get = snd_cmipci_spdif_stream_get,
1107 .put = snd_cmipci_spdif_stream_put
1108};
1109
1110/*
1111 */
1112
1113/* save mixer setting and mute for AC3 playback */
1114static int save_mixer_state(struct cmipci *cm)
1115{
1116 if (! cm->mixer_insensitive) {
1117 struct snd_ctl_elem_value *val;
1118 unsigned int i;
1119
1120 val = kmalloc(sizeof(*val), GFP_KERNEL);
1121 if (!val)
1122 return -ENOMEM;
1123 for (i = 0; i < CM_SAVED_MIXERS; i++) {
1124 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1125 if (ctl) {
1126 int event;
1127 memset(val, 0, sizeof(*val));
1128 ctl->get(ctl, val);
1129 cm->mixer_res_status[i] = val->value.integer.value[0];
1130 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1131 event = SNDRV_CTL_EVENT_MASK_INFO;
1132 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1133 ctl->put(ctl, val); /* toggle */
1134 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1135 }
1136 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1137 snd_ctl_notify(cm->card, event, &ctl->id);
1138 }
1139 }
1140 kfree(val);
1141 cm->mixer_insensitive = 1;
1142 }
1143 return 0;
1144}
1145
1146
1147/* restore the previously saved mixer status */
1148static void restore_mixer_state(struct cmipci *cm)
1149{
1150 if (cm->mixer_insensitive) {
1151 struct snd_ctl_elem_value *val;
1152 unsigned int i;
1153
1154 val = kmalloc(sizeof(*val), GFP_KERNEL);
1155 if (!val)
1156 return;
1157 cm->mixer_insensitive = 0; /* at first clear this;
1158 otherwise the changes will be ignored */
1159 for (i = 0; i < CM_SAVED_MIXERS; i++) {
1160 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
1161 if (ctl) {
1162 int event;
1163
1164 memset(val, 0, sizeof(*val));
1165 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1166 ctl->get(ctl, val);
1167 event = SNDRV_CTL_EVENT_MASK_INFO;
1168 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1169 val->value.integer.value[0] = cm->mixer_res_status[i];
1170 ctl->put(ctl, val);
1171 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1172 }
1173 snd_ctl_notify(cm->card, event, &ctl->id);
1174 }
1175 }
1176 kfree(val);
1177 }
1178}
1179
1180/* spinlock held! */
1181static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
1182{
1183 if (do_ac3) {
1184 /* AC3EN for 037 */
1185 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1186 /* AC3EN for 039 */
1187 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1188
1189 if (cm->can_ac3_hw) {
1190 /* SPD24SEL for 037, 0x02 */
1191 /* SPD24SEL for 039, 0x20, but cannot be set */
1192 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1193 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1194 } else { /* can_ac3_sw */
1195 /* SPD32SEL for 037 & 039, 0x20 */
1196 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1197 /* set 176K sample rate to fix 033 HW bug */
1198 if (cm->chip_version == 33) {
1199 if (rate >= 48000) {
1200 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1201 } else {
1202 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1203 }
1204 }
1205 }
1206
1207 } else {
1208 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1209 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1210
1211 if (cm->can_ac3_hw) {
1212 /* chip model >= 37 */
1213 if (snd_pcm_format_width(subs->runtime->format) > 16) {
1214 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1215 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1216 } else {
1217 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1218 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1219 }
1220 } else {
1221 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1222 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1223 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1224 }
1225 }
1226}
1227
1228static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
1229{
1230 int rate, err;
1231
1232 rate = subs->runtime->rate;
1233
1234 if (up && do_ac3) {
1235 err = save_mixer_state(cm);
1236 if (err < 0)
1237 return err;
1238 }
1239
1240 spin_lock_irq(&cm->reg_lock);
1241 cm->spdif_playback_avail = up;
1242 if (up) {
1243 /* they are controlled via "IEC958 Output Switch" */
1244 /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1245 /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1246 if (cm->spdif_playback_enabled)
1247 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1248 setup_ac3(cm, subs, do_ac3, rate);
1249
1250 if (rate == 48000 || rate == 96000)
1251 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1252 else
1253 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1254 if (rate > 48000)
1255 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1256 else
1257 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1258 } else {
1259 /* they are controlled via "IEC958 Output Switch" */
1260 /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1261 /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1262 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1263 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1264 setup_ac3(cm, subs, 0, 0);
1265 }
1266 spin_unlock_irq(&cm->reg_lock);
1267 return 0;
1268}
1269
1270
1271/*
1272 * preparation
1273 */
1274
1275/* playback - enable spdif only on the certain condition */
1276static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
1277{
1278 struct cmipci *cm = snd_pcm_substream_chip(substream);
1279 int rate = substream->runtime->rate;
1280 int err, do_spdif, do_ac3 = 0;
1281
1282 do_spdif = (rate >= 44100 && rate <= 96000 &&
1283 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1284 substream->runtime->channels == 2);
1285 if (do_spdif && cm->can_ac3_hw)
1286 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1287 err = setup_spdif_playback(cm, substream, do_spdif, do_ac3);
1288 if (err < 0)
1289 return err;
1290 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1291}
1292
1293/* playback (via device #2) - enable spdif always */
1294static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
1295{
1296 struct cmipci *cm = snd_pcm_substream_chip(substream);
1297 int err, do_ac3;
1298
1299 if (cm->can_ac3_hw)
1300 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1301 else
1302 do_ac3 = 1; /* doesn't matter */
1303 err = setup_spdif_playback(cm, substream, 1, do_ac3);
1304 if (err < 0)
1305 return err;
1306 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1307}
1308
1309/*
1310 * Apparently, the samples last played on channel A stay in some buffer, even
1311 * after the channel is reset, and get added to the data for the rear DACs when
1312 * playing a multichannel stream on channel B. This is likely to generate
1313 * wraparounds and thus distortions.
1314 * To avoid this, we play at least one zero sample after the actual stream has
1315 * stopped.
1316 */
1317static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
1318{
1319 struct snd_pcm_runtime *runtime = rec->substream->runtime;
1320 unsigned int reg, val;
1321
1322 if (rec->needs_silencing && runtime && runtime->dma_area) {
1323 /* set up a small silence buffer */
1324 memset(runtime->dma_area, 0, PAGE_SIZE);
1325 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1326 val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
1327 snd_cmipci_write(cm, reg, val);
1328
1329 /* configure for 16 bits, 2 channels, 8 kHz */
1330 if (runtime->channels > 2)
1331 set_dac_channels(cm, rec, 2);
1332 spin_lock_irq(&cm->reg_lock);
1333 val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
1334 val &= ~(CM_ASFC_MASK << (rec->ch * 3));
1335 val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
1336 snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
1337 val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
1338 val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
1339 val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
1340 if (cm->can_96k)
1341 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
1342 snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
1343
1344 /* start stream (we don't need interrupts) */
1345 cm->ctrl |= CM_CHEN0 << rec->ch;
1346 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
1347 spin_unlock_irq(&cm->reg_lock);
1348
1349 msleep(1);
1350
1351 /* stop and reset stream */
1352 spin_lock_irq(&cm->reg_lock);
1353 cm->ctrl &= ~(CM_CHEN0 << rec->ch);
1354 val = CM_RST_CH0 << rec->ch;
1355 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
1356 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
1357 spin_unlock_irq(&cm->reg_lock);
1358
1359 rec->needs_silencing = 0;
1360 }
1361}
1362
1363static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
1364{
1365 struct cmipci *cm = snd_pcm_substream_chip(substream);
1366 setup_spdif_playback(cm, substream, 0, 0);
1367 restore_mixer_state(cm);
1368 snd_cmipci_silence_hack(cm, &cm->channel[0]);
1369 return 0;
1370}
1371
1372static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
1373{
1374 struct cmipci *cm = snd_pcm_substream_chip(substream);
1375 snd_cmipci_silence_hack(cm, &cm->channel[1]);
1376 return 0;
1377}
1378
1379/* capture */
1380static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
1381{
1382 struct cmipci *cm = snd_pcm_substream_chip(substream);
1383 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1384}
1385
1386/* capture with spdif (via device #2) */
1387static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
1388{
1389 struct cmipci *cm = snd_pcm_substream_chip(substream);
1390
1391 spin_lock_irq(&cm->reg_lock);
1392 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1393 if (cm->can_96k) {
1394 if (substream->runtime->rate > 48000)
1395 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1396 else
1397 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1398 }
1399 if (snd_pcm_format_width(substream->runtime->format) > 16)
1400 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1401 else
1402 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1403
1404 spin_unlock_irq(&cm->reg_lock);
1405
1406 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1407}
1408
1409static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
1410{
1411 struct cmipci *cm = snd_pcm_substream_chip(subs);
1412
1413 spin_lock_irq(&cm->reg_lock);
1414 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1415 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1416 spin_unlock_irq(&cm->reg_lock);
1417
1418 return 0;
1419}
1420
1421
1422/*
1423 * interrupt handler
1424 */
1425static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
1426{
1427 struct cmipci *cm = dev_id;
1428 unsigned int status, mask = 0;
1429
1430 /* fastpath out, to ease interrupt sharing */
1431 status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
1432 if (!(status & CM_INTR))
1433 return IRQ_NONE;
1434
1435 /* acknowledge interrupt */
1436 spin_lock(&cm->reg_lock);
1437 if (status & CM_CHINT0)
1438 mask |= CM_CH0_INT_EN;
1439 if (status & CM_CHINT1)
1440 mask |= CM_CH1_INT_EN;
1441 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
1442 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
1443 spin_unlock(&cm->reg_lock);
1444
1445 if (cm->rmidi && (status & CM_UARTINT))
1446 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
1447
1448 if (cm->pcm) {
1449 if ((status & CM_CHINT0) && cm->channel[0].running)
1450 snd_pcm_period_elapsed(cm->channel[0].substream);
1451 if ((status & CM_CHINT1) && cm->channel[1].running)
1452 snd_pcm_period_elapsed(cm->channel[1].substream);
1453 }
1454 return IRQ_HANDLED;
1455}
1456
1457/*
1458 * h/w infos
1459 */
1460
1461/* playback on channel A */
1462static const struct snd_pcm_hardware snd_cmipci_playback =
1463{
1464 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1465 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1466 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1467 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1468 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1469 .rate_min = 5512,
1470 .rate_max = 48000,
1471 .channels_min = 1,
1472 .channels_max = 2,
1473 .buffer_bytes_max = (128*1024),
1474 .period_bytes_min = 64,
1475 .period_bytes_max = (128*1024),
1476 .periods_min = 2,
1477 .periods_max = 1024,
1478 .fifo_size = 0,
1479};
1480
1481/* capture on channel B */
1482static const struct snd_pcm_hardware snd_cmipci_capture =
1483{
1484 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1485 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1486 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1487 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1488 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1489 .rate_min = 5512,
1490 .rate_max = 48000,
1491 .channels_min = 1,
1492 .channels_max = 2,
1493 .buffer_bytes_max = (128*1024),
1494 .period_bytes_min = 64,
1495 .period_bytes_max = (128*1024),
1496 .periods_min = 2,
1497 .periods_max = 1024,
1498 .fifo_size = 0,
1499};
1500
1501/* playback on channel B - stereo 16bit only? */
1502static const struct snd_pcm_hardware snd_cmipci_playback2 =
1503{
1504 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1505 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1506 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1507 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1508 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1509 .rate_min = 5512,
1510 .rate_max = 48000,
1511 .channels_min = 2,
1512 .channels_max = 2,
1513 .buffer_bytes_max = (128*1024),
1514 .period_bytes_min = 64,
1515 .period_bytes_max = (128*1024),
1516 .periods_min = 2,
1517 .periods_max = 1024,
1518 .fifo_size = 0,
1519};
1520
1521/* spdif playback on channel A */
1522static const struct snd_pcm_hardware snd_cmipci_playback_spdif =
1523{
1524 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1525 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1526 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1527 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1528 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1529 .rate_min = 44100,
1530 .rate_max = 48000,
1531 .channels_min = 2,
1532 .channels_max = 2,
1533 .buffer_bytes_max = (128*1024),
1534 .period_bytes_min = 64,
1535 .period_bytes_max = (128*1024),
1536 .periods_min = 2,
1537 .periods_max = 1024,
1538 .fifo_size = 0,
1539};
1540
1541/* spdif playback on channel A (32bit, IEC958 subframes) */
1542static const struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
1543{
1544 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1545 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1546 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1547 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1548 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1549 .rate_min = 44100,
1550 .rate_max = 48000,
1551 .channels_min = 2,
1552 .channels_max = 2,
1553 .buffer_bytes_max = (128*1024),
1554 .period_bytes_min = 64,
1555 .period_bytes_max = (128*1024),
1556 .periods_min = 2,
1557 .periods_max = 1024,
1558 .fifo_size = 0,
1559};
1560
1561/* spdif capture on channel B */
1562static const struct snd_pcm_hardware snd_cmipci_capture_spdif =
1563{
1564 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1565 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1566 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
1567 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1568 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1569 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1570 .rate_min = 44100,
1571 .rate_max = 48000,
1572 .channels_min = 2,
1573 .channels_max = 2,
1574 .buffer_bytes_max = (128*1024),
1575 .period_bytes_min = 64,
1576 .period_bytes_max = (128*1024),
1577 .periods_min = 2,
1578 .periods_max = 1024,
1579 .fifo_size = 0,
1580};
1581
1582/*
1583 * check device open/close
1584 */
1585static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
1586{
1587 int ch = mode & CM_OPEN_CH_MASK;
1588
1589 /* FIXME: a file should wait until the device becomes free
1590 * when it's opened on blocking mode. however, since the current
1591 * pcm framework doesn't pass file pointer before actually opened,
1592 * we can't know whether blocking mode or not in open callback..
1593 */
1594 mutex_lock(&cm->open_mutex);
1595 if (cm->opened[ch]) {
1596 mutex_unlock(&cm->open_mutex);
1597 return -EBUSY;
1598 }
1599 cm->opened[ch] = mode;
1600 cm->channel[ch].substream = subs;
1601 if (! (mode & CM_OPEN_DAC)) {
1602 /* disable dual DAC mode */
1603 cm->channel[ch].is_dac = 0;
1604 spin_lock_irq(&cm->reg_lock);
1605 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1606 spin_unlock_irq(&cm->reg_lock);
1607 }
1608 mutex_unlock(&cm->open_mutex);
1609 return 0;
1610}
1611
1612static void close_device_check(struct cmipci *cm, int mode)
1613{
1614 int ch = mode & CM_OPEN_CH_MASK;
1615
1616 mutex_lock(&cm->open_mutex);
1617 if (cm->opened[ch] == mode) {
1618 if (cm->channel[ch].substream) {
1619 snd_cmipci_ch_reset(cm, ch);
1620 cm->channel[ch].running = 0;
1621 cm->channel[ch].substream = NULL;
1622 }
1623 cm->opened[ch] = 0;
1624 if (! cm->channel[ch].is_dac) {
1625 /* enable dual DAC mode again */
1626 cm->channel[ch].is_dac = 1;
1627 spin_lock_irq(&cm->reg_lock);
1628 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1629 spin_unlock_irq(&cm->reg_lock);
1630 }
1631 }
1632 mutex_unlock(&cm->open_mutex);
1633}
1634
1635/*
1636 */
1637
1638static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
1639{
1640 struct cmipci *cm = snd_pcm_substream_chip(substream);
1641 struct snd_pcm_runtime *runtime = substream->runtime;
1642 int err;
1643
1644 err = open_device_check(cm, CM_OPEN_PLAYBACK, substream);
1645 if (err < 0)
1646 return err;
1647 runtime->hw = snd_cmipci_playback;
1648 if (cm->chip_version == 68) {
1649 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1650 SNDRV_PCM_RATE_96000;
1651 runtime->hw.rate_max = 96000;
1652 } else if (cm->chip_version == 55) {
1653 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1654 SNDRV_PCM_RATE_96000 |
1655 SNDRV_PCM_RATE_128000;
1656 runtime->hw.rate_max = 128000;
1657 }
1658 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1659 cm->dig_pcm_status = cm->dig_status;
1660 return 0;
1661}
1662
1663static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
1664{
1665 struct cmipci *cm = snd_pcm_substream_chip(substream);
1666 struct snd_pcm_runtime *runtime = substream->runtime;
1667 int err;
1668
1669 err = open_device_check(cm, CM_OPEN_CAPTURE, substream);
1670 if (err < 0)
1671 return err;
1672 runtime->hw = snd_cmipci_capture;
1673 if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
1674 runtime->hw.rate_min = 41000;
1675 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1676 } else if (cm->chip_version == 55) {
1677 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1678 SNDRV_PCM_RATE_96000 |
1679 SNDRV_PCM_RATE_128000;
1680 runtime->hw.rate_max = 128000;
1681 }
1682 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1683 return 0;
1684}
1685
1686static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
1687{
1688 struct cmipci *cm = snd_pcm_substream_chip(substream);
1689 struct snd_pcm_runtime *runtime = substream->runtime;
1690 int err;
1691
1692 /* use channel B */
1693 err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream);
1694 if (err < 0)
1695 return err;
1696 runtime->hw = snd_cmipci_playback2;
1697 mutex_lock(&cm->open_mutex);
1698 if (! cm->opened[CM_CH_PLAY]) {
1699 if (cm->can_multi_ch) {
1700 runtime->hw.channels_max = cm->max_channels;
1701 if (cm->max_channels == 4)
1702 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
1703 else if (cm->max_channels == 6)
1704 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
1705 else if (cm->max_channels == 8)
1706 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
1707 }
1708 }
1709 mutex_unlock(&cm->open_mutex);
1710 if (cm->chip_version == 68) {
1711 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1712 SNDRV_PCM_RATE_96000;
1713 runtime->hw.rate_max = 96000;
1714 } else if (cm->chip_version == 55) {
1715 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1716 SNDRV_PCM_RATE_96000 |
1717 SNDRV_PCM_RATE_128000;
1718 runtime->hw.rate_max = 128000;
1719 }
1720 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1721 return 0;
1722}
1723
1724static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
1725{
1726 struct cmipci *cm = snd_pcm_substream_chip(substream);
1727 struct snd_pcm_runtime *runtime = substream->runtime;
1728 int err;
1729
1730 /* use channel A */
1731 err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream);
1732 if (err < 0)
1733 return err;
1734 if (cm->can_ac3_hw) {
1735 runtime->hw = snd_cmipci_playback_spdif;
1736 if (cm->chip_version >= 37) {
1737 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1738 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1739 }
1740 if (cm->can_96k) {
1741 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1742 SNDRV_PCM_RATE_96000;
1743 runtime->hw.rate_max = 96000;
1744 }
1745 } else {
1746 runtime->hw = snd_cmipci_playback_iec958_subframe;
1747 }
1748 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1749 cm->dig_pcm_status = cm->dig_status;
1750 return 0;
1751}
1752
1753static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
1754{
1755 struct cmipci *cm = snd_pcm_substream_chip(substream);
1756 struct snd_pcm_runtime *runtime = substream->runtime;
1757 int err;
1758
1759 /* use channel B */
1760 err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream);
1761 if (err < 0)
1762 return err;
1763 runtime->hw = snd_cmipci_capture_spdif;
1764 if (cm->can_96k && !(cm->chip_version == 68)) {
1765 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1766 SNDRV_PCM_RATE_96000;
1767 runtime->hw.rate_max = 96000;
1768 }
1769 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1770 return 0;
1771}
1772
1773
1774/*
1775 */
1776
1777static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
1778{
1779 struct cmipci *cm = snd_pcm_substream_chip(substream);
1780 close_device_check(cm, CM_OPEN_PLAYBACK);
1781 return 0;
1782}
1783
1784static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
1785{
1786 struct cmipci *cm = snd_pcm_substream_chip(substream);
1787 close_device_check(cm, CM_OPEN_CAPTURE);
1788 return 0;
1789}
1790
1791static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
1792{
1793 struct cmipci *cm = snd_pcm_substream_chip(substream);
1794 close_device_check(cm, CM_OPEN_PLAYBACK2);
1795 close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
1796 return 0;
1797}
1798
1799static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
1800{
1801 struct cmipci *cm = snd_pcm_substream_chip(substream);
1802 close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
1803 return 0;
1804}
1805
1806static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
1807{
1808 struct cmipci *cm = snd_pcm_substream_chip(substream);
1809 close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
1810 return 0;
1811}
1812
1813
1814/*
1815 */
1816
1817static const struct snd_pcm_ops snd_cmipci_playback_ops = {
1818 .open = snd_cmipci_playback_open,
1819 .close = snd_cmipci_playback_close,
1820 .hw_free = snd_cmipci_playback_hw_free,
1821 .prepare = snd_cmipci_playback_prepare,
1822 .trigger = snd_cmipci_playback_trigger,
1823 .pointer = snd_cmipci_playback_pointer,
1824};
1825
1826static const struct snd_pcm_ops snd_cmipci_capture_ops = {
1827 .open = snd_cmipci_capture_open,
1828 .close = snd_cmipci_capture_close,
1829 .prepare = snd_cmipci_capture_prepare,
1830 .trigger = snd_cmipci_capture_trigger,
1831 .pointer = snd_cmipci_capture_pointer,
1832};
1833
1834static const struct snd_pcm_ops snd_cmipci_playback2_ops = {
1835 .open = snd_cmipci_playback2_open,
1836 .close = snd_cmipci_playback2_close,
1837 .hw_params = snd_cmipci_playback2_hw_params,
1838 .hw_free = snd_cmipci_playback2_hw_free,
1839 .prepare = snd_cmipci_capture_prepare, /* channel B */
1840 .trigger = snd_cmipci_capture_trigger, /* channel B */
1841 .pointer = snd_cmipci_capture_pointer, /* channel B */
1842};
1843
1844static const struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
1845 .open = snd_cmipci_playback_spdif_open,
1846 .close = snd_cmipci_playback_spdif_close,
1847 .hw_free = snd_cmipci_playback_hw_free,
1848 .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
1849 .trigger = snd_cmipci_playback_trigger,
1850 .pointer = snd_cmipci_playback_pointer,
1851};
1852
1853static const struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
1854 .open = snd_cmipci_capture_spdif_open,
1855 .close = snd_cmipci_capture_spdif_close,
1856 .hw_free = snd_cmipci_capture_spdif_hw_free,
1857 .prepare = snd_cmipci_capture_spdif_prepare,
1858 .trigger = snd_cmipci_capture_trigger,
1859 .pointer = snd_cmipci_capture_pointer,
1860};
1861
1862
1863/*
1864 */
1865
1866static int snd_cmipci_pcm_new(struct cmipci *cm, int device)
1867{
1868 struct snd_pcm *pcm;
1869 int err;
1870
1871 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1872 if (err < 0)
1873 return err;
1874
1875 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
1876 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
1877
1878 pcm->private_data = cm;
1879 pcm->info_flags = 0;
1880 strcpy(pcm->name, "C-Media PCI DAC/ADC");
1881 cm->pcm = pcm;
1882
1883 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1884 &cm->pci->dev, 64*1024, 128*1024);
1885
1886 return 0;
1887}
1888
1889static int snd_cmipci_pcm2_new(struct cmipci *cm, int device)
1890{
1891 struct snd_pcm *pcm;
1892 int err;
1893
1894 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1895 if (err < 0)
1896 return err;
1897
1898 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
1899
1900 pcm->private_data = cm;
1901 pcm->info_flags = 0;
1902 strcpy(pcm->name, "C-Media PCI 2nd DAC");
1903 cm->pcm2 = pcm;
1904
1905 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1906 &cm->pci->dev, 64*1024, 128*1024);
1907
1908 return 0;
1909}
1910
1911static int snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
1912{
1913 struct snd_pcm *pcm;
1914 int err;
1915
1916 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1917 if (err < 0)
1918 return err;
1919
1920 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
1921 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
1922
1923 pcm->private_data = cm;
1924 pcm->info_flags = 0;
1925 strcpy(pcm->name, "C-Media PCI IEC958");
1926 cm->pcm_spdif = pcm;
1927
1928 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1929 &cm->pci->dev, 64*1024, 128*1024);
1930
1931 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1932 snd_pcm_alt_chmaps, cm->max_channels, 0,
1933 NULL);
1934 if (err < 0)
1935 return err;
1936
1937 return 0;
1938}
1939
1940/*
1941 * mixer interface:
1942 * - CM8338/8738 has a compatible mixer interface with SB16, but
1943 * lack of some elements like tone control, i/o gain and AGC.
1944 * - Access to native registers:
1945 * - A 3D switch
1946 * - Output mute switches
1947 */
1948
1949static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
1950{
1951 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1952 outb(data, s->iobase + CM_REG_SB16_DATA);
1953}
1954
1955static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
1956{
1957 unsigned char v;
1958
1959 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1960 v = inb(s->iobase + CM_REG_SB16_DATA);
1961 return v;
1962}
1963
1964/*
1965 * general mixer element
1966 */
1967struct cmipci_sb_reg {
1968 unsigned int left_reg, right_reg;
1969 unsigned int left_shift, right_shift;
1970 unsigned int mask;
1971 unsigned int invert: 1;
1972 unsigned int stereo: 1;
1973};
1974
1975#define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1976 ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1977
1978#define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1979{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1980 .info = snd_cmipci_info_volume, \
1981 .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
1982 .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1983}
1984
1985#define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
1986#define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
1987#define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
1988#define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
1989
1990static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
1991{
1992 r->left_reg = val & 0xff;
1993 r->right_reg = (val >> 8) & 0xff;
1994 r->left_shift = (val >> 16) & 0x07;
1995 r->right_shift = (val >> 19) & 0x07;
1996 r->invert = (val >> 22) & 1;
1997 r->stereo = (val >> 23) & 1;
1998 r->mask = (val >> 24) & 0xff;
1999}
2000
2001static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_info *uinfo)
2003{
2004 struct cmipci_sb_reg reg;
2005
2006 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2007 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2008 uinfo->count = reg.stereo + 1;
2009 uinfo->value.integer.min = 0;
2010 uinfo->value.integer.max = reg.mask;
2011 return 0;
2012}
2013
2014static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
2015 struct snd_ctl_elem_value *ucontrol)
2016{
2017 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2018 struct cmipci_sb_reg reg;
2019 int val;
2020
2021 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2022 spin_lock_irq(&cm->reg_lock);
2023 val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
2024 if (reg.invert)
2025 val = reg.mask - val;
2026 ucontrol->value.integer.value[0] = val;
2027 if (reg.stereo) {
2028 val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
2029 if (reg.invert)
2030 val = reg.mask - val;
2031 ucontrol->value.integer.value[1] = val;
2032 }
2033 spin_unlock_irq(&cm->reg_lock);
2034 return 0;
2035}
2036
2037static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_value *ucontrol)
2039{
2040 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2041 struct cmipci_sb_reg reg;
2042 int change;
2043 int left, right, oleft, oright;
2044
2045 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2046 left = ucontrol->value.integer.value[0] & reg.mask;
2047 if (reg.invert)
2048 left = reg.mask - left;
2049 left <<= reg.left_shift;
2050 if (reg.stereo) {
2051 right = ucontrol->value.integer.value[1] & reg.mask;
2052 if (reg.invert)
2053 right = reg.mask - right;
2054 right <<= reg.right_shift;
2055 } else
2056 right = 0;
2057 spin_lock_irq(&cm->reg_lock);
2058 oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
2059 left |= oleft & ~(reg.mask << reg.left_shift);
2060 change = left != oleft;
2061 if (reg.stereo) {
2062 if (reg.left_reg != reg.right_reg) {
2063 snd_cmipci_mixer_write(cm, reg.left_reg, left);
2064 oright = snd_cmipci_mixer_read(cm, reg.right_reg);
2065 } else
2066 oright = left;
2067 right |= oright & ~(reg.mask << reg.right_shift);
2068 change |= right != oright;
2069 snd_cmipci_mixer_write(cm, reg.right_reg, right);
2070 } else
2071 snd_cmipci_mixer_write(cm, reg.left_reg, left);
2072 spin_unlock_irq(&cm->reg_lock);
2073 return change;
2074}
2075
2076/*
2077 * input route (left,right) -> (left,right)
2078 */
2079#define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
2080{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2081 .info = snd_cmipci_info_input_sw, \
2082 .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
2083 .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
2084}
2085
2086static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_info *uinfo)
2088{
2089 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2090 uinfo->count = 4;
2091 uinfo->value.integer.min = 0;
2092 uinfo->value.integer.max = 1;
2093 return 0;
2094}
2095
2096static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_value *ucontrol)
2098{
2099 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2100 struct cmipci_sb_reg reg;
2101 int val1, val2;
2102
2103 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2104 spin_lock_irq(&cm->reg_lock);
2105 val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2106 val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2107 spin_unlock_irq(&cm->reg_lock);
2108 ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2109 ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2110 ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2111 ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2112 return 0;
2113}
2114
2115static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
2117{
2118 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2119 struct cmipci_sb_reg reg;
2120 int change;
2121 int val1, val2, oval1, oval2;
2122
2123 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2124 spin_lock_irq(&cm->reg_lock);
2125 oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2126 oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2127 val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2128 val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2129 val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2130 val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2131 val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2132 val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2133 change = val1 != oval1 || val2 != oval2;
2134 snd_cmipci_mixer_write(cm, reg.left_reg, val1);
2135 snd_cmipci_mixer_write(cm, reg.right_reg, val2);
2136 spin_unlock_irq(&cm->reg_lock);
2137 return change;
2138}
2139
2140/*
2141 * native mixer switches/volumes
2142 */
2143
2144#define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
2145{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2146 .info = snd_cmipci_info_native_mixer, \
2147 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2148 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
2149}
2150
2151#define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
2152{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2153 .info = snd_cmipci_info_native_mixer, \
2154 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2155 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
2156}
2157
2158#define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
2159{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2160 .info = snd_cmipci_info_native_mixer, \
2161 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2162 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
2163}
2164
2165#define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
2166{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2167 .info = snd_cmipci_info_native_mixer, \
2168 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2169 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
2170}
2171
2172static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_info *uinfo)
2174{
2175 struct cmipci_sb_reg reg;
2176
2177 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2178 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2179 uinfo->count = reg.stereo + 1;
2180 uinfo->value.integer.min = 0;
2181 uinfo->value.integer.max = reg.mask;
2182 return 0;
2183
2184}
2185
2186static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
2187 struct snd_ctl_elem_value *ucontrol)
2188{
2189 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2190 struct cmipci_sb_reg reg;
2191 unsigned char oreg, val;
2192
2193 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2194 spin_lock_irq(&cm->reg_lock);
2195 oreg = inb(cm->iobase + reg.left_reg);
2196 val = (oreg >> reg.left_shift) & reg.mask;
2197 if (reg.invert)
2198 val = reg.mask - val;
2199 ucontrol->value.integer.value[0] = val;
2200 if (reg.stereo) {
2201 val = (oreg >> reg.right_shift) & reg.mask;
2202 if (reg.invert)
2203 val = reg.mask - val;
2204 ucontrol->value.integer.value[1] = val;
2205 }
2206 spin_unlock_irq(&cm->reg_lock);
2207 return 0;
2208}
2209
2210static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
2213 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2214 struct cmipci_sb_reg reg;
2215 unsigned char oreg, nreg, val;
2216
2217 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2218 spin_lock_irq(&cm->reg_lock);
2219 oreg = inb(cm->iobase + reg.left_reg);
2220 val = ucontrol->value.integer.value[0] & reg.mask;
2221 if (reg.invert)
2222 val = reg.mask - val;
2223 nreg = oreg & ~(reg.mask << reg.left_shift);
2224 nreg |= (val << reg.left_shift);
2225 if (reg.stereo) {
2226 val = ucontrol->value.integer.value[1] & reg.mask;
2227 if (reg.invert)
2228 val = reg.mask - val;
2229 nreg &= ~(reg.mask << reg.right_shift);
2230 nreg |= (val << reg.right_shift);
2231 }
2232 outb(nreg, cm->iobase + reg.left_reg);
2233 spin_unlock_irq(&cm->reg_lock);
2234 return (nreg != oreg);
2235}
2236
2237/*
2238 * special case - check mixer sensitivity
2239 */
2240static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2244 return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
2245}
2246
2247static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2249{
2250 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2251 if (cm->mixer_insensitive) {
2252 /* ignored */
2253 return 0;
2254 }
2255 return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
2256}
2257
2258
2259static const struct snd_kcontrol_new snd_cmipci_mixers[] = {
2260 CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
2261 CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2262 CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
2263 //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
2264 { /* switch with sensitivity */
2265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2266 .name = "PCM Playback Switch",
2267 .info = snd_cmipci_info_native_mixer,
2268 .get = snd_cmipci_get_native_mixer_sensitive,
2269 .put = snd_cmipci_put_native_mixer_sensitive,
2270 .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
2271 },
2272 CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
2273 CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
2274 CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
2275 CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
2276 CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
2277 CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
2278 CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
2279 CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
2280 CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
2281 CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
2282 CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
2283 CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
2284 CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
2285 CMIPCI_SB_VOL_MONO("Beep Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
2286 CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
2287 CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
2288 CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
2289 CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
2290 CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
2291 CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
2292 CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
2293 CMIPCI_DOUBLE("Beep Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
2294 CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
2295};
2296
2297/*
2298 * other switches
2299 */
2300
2301struct cmipci_switch_args {
2302 int reg; /* register index */
2303 unsigned int mask; /* mask bits */
2304 unsigned int mask_on; /* mask bits to turn on */
2305 unsigned int is_byte: 1; /* byte access? */
2306 unsigned int ac3_sensitive: 1; /* access forbidden during
2307 * non-audio operation?
2308 */
2309};
2310
2311#define snd_cmipci_uswitch_info snd_ctl_boolean_mono_info
2312
2313static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol,
2315 struct cmipci_switch_args *args)
2316{
2317 unsigned int val;
2318 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2319
2320 spin_lock_irq(&cm->reg_lock);
2321 if (args->ac3_sensitive && cm->mixer_insensitive) {
2322 ucontrol->value.integer.value[0] = 0;
2323 spin_unlock_irq(&cm->reg_lock);
2324 return 0;
2325 }
2326 if (args->is_byte)
2327 val = inb(cm->iobase + args->reg);
2328 else
2329 val = snd_cmipci_read(cm, args->reg);
2330 ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2331 spin_unlock_irq(&cm->reg_lock);
2332 return 0;
2333}
2334
2335static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
2338 struct cmipci_switch_args *args;
2339 args = (struct cmipci_switch_args *)kcontrol->private_value;
2340 if (snd_BUG_ON(!args))
2341 return -EINVAL;
2342 return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
2343}
2344
2345static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol,
2347 struct cmipci_switch_args *args)
2348{
2349 unsigned int val;
2350 int change;
2351 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2352
2353 spin_lock_irq(&cm->reg_lock);
2354 if (args->ac3_sensitive && cm->mixer_insensitive) {
2355 /* ignored */
2356 spin_unlock_irq(&cm->reg_lock);
2357 return 0;
2358 }
2359 if (args->is_byte)
2360 val = inb(cm->iobase + args->reg);
2361 else
2362 val = snd_cmipci_read(cm, args->reg);
2363 change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
2364 args->mask_on : (args->mask & ~args->mask_on));
2365 if (change) {
2366 val &= ~args->mask;
2367 if (ucontrol->value.integer.value[0])
2368 val |= args->mask_on;
2369 else
2370 val |= (args->mask & ~args->mask_on);
2371 if (args->is_byte)
2372 outb((unsigned char)val, cm->iobase + args->reg);
2373 else
2374 snd_cmipci_write(cm, args->reg, val);
2375 }
2376 spin_unlock_irq(&cm->reg_lock);
2377 return change;
2378}
2379
2380static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct cmipci_switch_args *args;
2384 args = (struct cmipci_switch_args *)kcontrol->private_value;
2385 if (snd_BUG_ON(!args))
2386 return -EINVAL;
2387 return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
2388}
2389
2390#ifndef TARGET_OS2
2391#define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
2392static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
2393 .reg = xreg, \
2394 .mask = xmask, \
2395 .mask_on = xmask_on, \
2396 .is_byte = xis_byte, \
2397 .ac3_sensitive = xac3, \
2398}
2399#else
2400#define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
2401 static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
2402 xreg, \
2403 xmask, \
2404 xmask_on, \
2405 xis_byte, \
2406 xac3, \
2407 }
2408#endif
2409
2410#define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
2411 DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
2412
2413#if 0 /* these will be controlled in pcm device */
2414DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
2415DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
2416#endif
2417DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
2418DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
2419DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
2420DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
2421DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
2422DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
2423DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
2424DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
2425// DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
2426DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
2427DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
2428/* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
2429DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
2430DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
2431#if CM_CH_PLAY == 1
2432DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
2433#else
2434DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
2435#endif
2436DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
2437// DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
2438// DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
2439// DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
2440DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
2441
2442#define DEFINE_SWITCH(sname, stype, sarg) \
2443{ .name = sname, \
2444 .iface = stype, \
2445 .info = snd_cmipci_uswitch_info, \
2446 .get = snd_cmipci_uswitch_get, \
2447 .put = snd_cmipci_uswitch_put, \
2448 .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
2449}
2450
2451#define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
2452#define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
2453
2454
2455/*
2456 * callbacks for spdif output switch
2457 * needs toggle two registers..
2458 */
2459static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2461{
2462 int changed;
2463 changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2464 changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2465 return changed;
2466}
2467
2468static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
2472 int changed;
2473 changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2474 changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2475 if (changed) {
2476 if (ucontrol->value.integer.value[0]) {
2477 if (chip->spdif_playback_avail)
2478 snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2479 } else {
2480 if (chip->spdif_playback_avail)
2481 snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2482 }
2483 }
2484 chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2485 return changed;
2486}
2487
2488
2489static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
2490 struct snd_ctl_elem_info *uinfo)
2491{
2492 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2493 static const char *const texts[3] = {
2494 "Line-In", "Rear Output", "Bass Output"
2495 };
2496
2497 return snd_ctl_enum_info(uinfo, 1,
2498 cm->chip_version >= 39 ? 3 : 2, texts);
2499}
2500
2501static inline unsigned int get_line_in_mode(struct cmipci *cm)
2502{
2503 unsigned int val;
2504 if (cm->chip_version >= 39) {
2505 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
2506 if (val & (CM_CENTR2LIN | CM_BASE2LIN))
2507 return 2;
2508 }
2509 val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
2510 if (val & CM_REAR2LIN)
2511 return 1;
2512 return 0;
2513}
2514
2515static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
2516 struct snd_ctl_elem_value *ucontrol)
2517{
2518 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2519
2520 spin_lock_irq(&cm->reg_lock);
2521 ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2522 spin_unlock_irq(&cm->reg_lock);
2523 return 0;
2524}
2525
2526static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_value *ucontrol)
2528{
2529 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2530 int change;
2531
2532 spin_lock_irq(&cm->reg_lock);
2533 if (ucontrol->value.enumerated.item[0] == 2)
2534 change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
2535 else
2536 change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
2537 if (ucontrol->value.enumerated.item[0] == 1)
2538 change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
2539 else
2540 change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
2541 spin_unlock_irq(&cm->reg_lock);
2542 return change;
2543}
2544
2545static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_info *uinfo)
2547{
2548 static const char *const texts[2] = { "Mic-In", "Center/LFE Output" };
2549
2550 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2551}
2552
2553static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_value *ucontrol)
2555{
2556 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2557 /* same bit as spdi_phase */
2558 spin_lock_irq(&cm->reg_lock);
2559 ucontrol->value.enumerated.item[0] =
2560 (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
2561 spin_unlock_irq(&cm->reg_lock);
2562 return 0;
2563}
2564
2565static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
2566 struct snd_ctl_elem_value *ucontrol)
2567{
2568 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2569 int change;
2570
2571 spin_lock_irq(&cm->reg_lock);
2572 if (ucontrol->value.enumerated.item[0])
2573 change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2574 else
2575 change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2576 spin_unlock_irq(&cm->reg_lock);
2577 return change;
2578}
2579
2580/* both for CM8338/8738 */
2581static const struct snd_kcontrol_new snd_cmipci_mixer_switches[] = {
2582 DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
2583 {
2584 .name = "Line-In Mode",
2585 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2586 .info = snd_cmipci_line_in_mode_info,
2587 .get = snd_cmipci_line_in_mode_get,
2588 .put = snd_cmipci_line_in_mode_put,
2589 },
2590};
2591
2592/* for non-multichannel chips */
2593static const struct snd_kcontrol_new snd_cmipci_nomulti_switch =
2594DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
2595
2596/* only for CM8738 */
2597static const struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] = {
2598#if 0 /* controlled in pcm device */
2599 DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
2600 DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
2601 DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
2602#endif
2603 // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
2604 { .name = "IEC958 Output Switch",
2605 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2606 .info = snd_cmipci_uswitch_info,
2607 .get = snd_cmipci_spdout_enable_get,
2608 .put = snd_cmipci_spdout_enable_put,
2609 },
2610 DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
2611 DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
2612 DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
2613// DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
2614 DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
2615 DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
2616};
2617
2618/* only for model 033/037 */
2619static const struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] = {
2620 DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
2621 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
2622 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
2623};
2624
2625/* only for model 039 or later */
2626static const struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] = {
2627 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
2628 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
2629 {
2630 .name = "Mic-In Mode",
2631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2632 .info = snd_cmipci_mic_in_mode_info,
2633 .get = snd_cmipci_mic_in_mode_get,
2634 .put = snd_cmipci_mic_in_mode_put,
2635 }
2636};
2637
2638/* card control switches */
2639static const struct snd_kcontrol_new snd_cmipci_modem_switch =
2640DEFINE_CARD_SWITCH("Modem", modem);
2641
2642
2643static int snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
2644{
2645 struct snd_card *card;
2646 const struct snd_kcontrol_new *sw;
2647 struct snd_kcontrol *kctl;
2648 unsigned int idx;
2649 int err;
2650
2651 if (snd_BUG_ON(!cm || !cm->card))
2652 return -EINVAL;
2653
2654 card = cm->card;
2655
2656 strcpy(card->mixername, "CMedia PCI");
2657
2658 spin_lock_irq(&cm->reg_lock);
2659 snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
2660 spin_unlock_irq(&cm->reg_lock);
2661
2662 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
2663 if (cm->chip_version == 68) { // 8768 has no PCM volume
2664 if (!strcmp(snd_cmipci_mixers[idx].name,
2665 "PCM Playback Volume"))
2666 continue;
2667 }
2668 err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm));
2669 if (err < 0)
2670 return err;
2671 }
2672
2673 /* mixer switches */
2674 sw = snd_cmipci_mixer_switches;
2675 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
2676 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2677 if (err < 0)
2678 return err;
2679 }
2680 if (! cm->can_multi_ch) {
2681 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2682 if (err < 0)
2683 return err;
2684 }
2685 if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2686 cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2687 sw = snd_cmipci_8738_mixer_switches;
2688 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
2689 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2690 if (err < 0)
2691 return err;
2692 }
2693 if (cm->can_ac3_hw) {
2694 kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm);
2695 kctl->id.device = pcm_spdif_device;
2696 err = snd_ctl_add(card, kctl);
2697 if (err < 0)
2698 return err;
2699 kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm);
2700 kctl->id.device = pcm_spdif_device;
2701 err = snd_ctl_add(card, kctl);
2702 if (err < 0)
2703 return err;
2704 kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm);
2705 kctl->id.device = pcm_spdif_device;
2706 err = snd_ctl_add(card, kctl);
2707 if (err < 0)
2708 return err;
2709 }
2710 if (cm->chip_version <= 37) {
2711 sw = snd_cmipci_old_mixer_switches;
2712 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
2713 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2714 if (err < 0)
2715 return err;
2716 }
2717 }
2718 }
2719 if (cm->chip_version >= 39) {
2720 sw = snd_cmipci_extra_mixer_switches;
2721 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
2722 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2723 if (err < 0)
2724 return err;
2725 }
2726 }
2727
2728 /* card switches */
2729 /*
2730 * newer chips don't have the register bits to force modem link
2731 * detection; the bit that was FLINKON now mutes CH1
2732 */
2733 if (cm->chip_version < 39) {
2734 err = snd_ctl_add(cm->card,
2735 snd_ctl_new1(&snd_cmipci_modem_switch, cm));
2736 if (err < 0)
2737 return err;
2738 }
2739
2740 for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
2741 struct snd_kcontrol *ctl;
2742 ctl = snd_ctl_find_id_mixer(cm->card, cm_saved_mixer[idx].name);
2743 if (ctl)
2744 cm->mixer_res_ctl[idx] = ctl;
2745 }
2746
2747 return 0;
2748}
2749
2750
2751/*
2752 * proc interface
2753 */
2754
2755static void snd_cmipci_proc_read(struct snd_info_entry *entry,
2756 struct snd_info_buffer *buffer)
2757{
2758 struct cmipci *cm = entry->private_data;
2759 int i, v;
2760
2761 snd_iprintf(buffer, "%s\n", cm->card->longname);
2762 for (i = 0; i < 0x94; i++) {
2763 if (i == 0x28)
2764 i = 0x90;
2765 v = inb(cm->iobase + i);
2766 if (i % 4 == 0)
2767 snd_iprintf(buffer, "\n%02x:", i);
2768 snd_iprintf(buffer, " %02x", v);
2769 }
2770 snd_iprintf(buffer, "\n");
2771}
2772
2773static void snd_cmipci_proc_init(struct cmipci *cm)
2774{
2775 snd_card_ro_proc_new(cm->card, "cmipci", cm, snd_cmipci_proc_read);
2776}
2777
2778static const struct pci_device_id snd_cmipci_ids[] = {
2779 {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A), 0},
2780 {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B), 0},
2781 {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
2782 {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B), 0},
2783 {PCI_VDEVICE(AL, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
2784 {0,},
2785};
2786
2787
2788/*
2789 * check chip version and capabilities
2790 * driver name is modified according to the chip model
2791 */
2792static void query_chip(struct cmipci *cm)
2793{
2794 unsigned int detect;
2795
2796 /* check reg 0Ch, bit 24-31 */
2797 detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
2798 if (! detect) {
2799 /* check reg 08h, bit 24-28 */
2800 detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
2801 switch (detect) {
2802 case 0:
2803 cm->chip_version = 33;
2804 if (cm->do_soft_ac3)
2805 cm->can_ac3_sw = 1;
2806 else
2807 cm->can_ac3_hw = 1;
2808 break;
2809 case CM_CHIP_037:
2810 cm->chip_version = 37;
2811 cm->can_ac3_hw = 1;
2812 break;
2813 default:
2814 cm->chip_version = 39;
2815 cm->can_ac3_hw = 1;
2816 break;
2817 }
2818 cm->max_channels = 2;
2819 } else {
2820 if (detect & CM_CHIP_039) {
2821 cm->chip_version = 39;
2822 if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
2823 cm->max_channels = 6;
2824 else
2825 cm->max_channels = 4;
2826 } else if (detect & CM_CHIP_8768) {
2827 cm->chip_version = 68;
2828 cm->max_channels = 8;
2829 cm->can_96k = 1;
2830 } else {
2831 cm->chip_version = 55;
2832 cm->max_channels = 6;
2833 cm->can_96k = 1;
2834 }
2835 cm->can_ac3_hw = 1;
2836 cm->can_multi_ch = 1;
2837 }
2838}
2839
2840#ifdef SUPPORT_JOYSTICK
2841static int snd_cmipci_create_gameport(struct cmipci *cm, int dev)
2842{
2843 static const int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
2844 struct gameport *gp;
2845 struct resource *r = NULL;
2846 int i, io_port = 0;
2847
2848 if (joystick_port[dev] == 0)
2849 return -ENODEV;
2850
2851 if (joystick_port[dev] == 1) { /* auto-detect */
2852 for (i = 0; ports[i]; i++) {
2853 io_port = ports[i];
2854 r = devm_request_region(&cm->pci->dev, io_port, 1,
2855 "CMIPCI gameport");
2856 if (r)
2857 break;
2858 }
2859 } else {
2860 io_port = joystick_port[dev];
2861 r = devm_request_region(&cm->pci->dev, io_port, 1,
2862 "CMIPCI gameport");
2863 }
2864
2865 if (!r) {
2866 dev_warn(cm->card->dev, "cannot reserve joystick ports\n");
2867 return -EBUSY;
2868 }
2869
2870 cm->gameport = gp = gameport_allocate_port();
2871 if (!gp) {
2872 dev_err(cm->card->dev, "cannot allocate memory for gameport\n");
2873 return -ENOMEM;
2874 }
2875 gameport_set_name(gp, "C-Media Gameport");
2876 gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2877 gameport_set_dev_parent(gp, &cm->pci->dev);
2878 gp->io = io_port;
2879
2880 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2881
2882 gameport_register_port(cm->gameport);
2883
2884 return 0;
2885}
2886
2887static void snd_cmipci_free_gameport(struct cmipci *cm)
2888{
2889 if (cm->gameport) {
2890 gameport_unregister_port(cm->gameport);
2891 cm->gameport = NULL;
2892
2893 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2894 }
2895}
2896#else
2897static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2898static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
2899#endif
2900
2901static void snd_cmipci_free(struct snd_card *card)
2902{
2903 struct cmipci *cm = card->private_data;
2904
2905 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2906 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
2907 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
2908 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2909 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2910 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
2911 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2912
2913 /* reset mixer */
2914 snd_cmipci_mixer_write(cm, 0, 0);
2915
2916 snd_cmipci_free_gameport(cm);
2917}
2918
2919static int snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
2920{
2921 long iosynth;
2922 unsigned int val;
2923 struct snd_opl3 *opl3;
2924 int err;
2925
2926 if (!fm_port)
2927 goto disable_fm;
2928
2929 if (cm->chip_version >= 39) {
2930 /* first try FM regs in PCI port range */
2931 iosynth = cm->iobase + CM_REG_FM_PCI;
2932 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2933 OPL3_HW_OPL3, 1, &opl3);
2934 } else {
2935 err = -EIO;
2936 }
2937 if (err < 0) {
2938 /* then try legacy ports */
2939 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
2940 iosynth = fm_port;
2941 switch (iosynth) {
2942 case 0x3E8: val |= CM_FMSEL_3E8; break;
2943 case 0x3E0: val |= CM_FMSEL_3E0; break;
2944 case 0x3C8: val |= CM_FMSEL_3C8; break;
2945 case 0x388: val |= CM_FMSEL_388; break;
2946 default:
2947 goto disable_fm;
2948 }
2949 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
2950 /* enable FM */
2951 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2952
2953 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2954 OPL3_HW_OPL3, 0, &opl3) < 0) {
2955 dev_err(cm->card->dev,
2956 "no OPL device at %#lx, skipping...\n",
2957 iosynth);
2958 goto disable_fm;
2959 }
2960 }
2961 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2962 if (err < 0) {
2963 dev_err(cm->card->dev, "cannot create OPL3 hwdep\n");
2964 return err;
2965 }
2966 return 0;
2967
2968 disable_fm:
2969 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
2970 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2971 return 0;
2972}
2973
2974static int snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
2975 int dev)
2976{
2977 struct cmipci *cm = card->private_data;
2978 int err;
2979 unsigned int val;
2980 long iomidi = 0;
2981 int integrated_midi = 0;
2982 char modelstr[16];
2983 int pcm_index, pcm_spdif_index;
2984 static const struct pci_device_id intel_82437vx[] = {
2985 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
2986 {0},
2987 };
2988
2989 err = pcim_enable_device(pci);
2990 if (err < 0)
2991 return err;
2992
2993 spin_lock_init(&cm->reg_lock);
2994 mutex_init(&cm->open_mutex);
2995 cm->device = pci->device;
2996 cm->card = card;
2997 cm->pci = pci;
2998 cm->irq = -1;
2999 cm->channel[0].ch = 0;
3000 cm->channel[1].ch = 1;
3001 cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
3002
3003 err = pci_request_regions(pci, card->driver);
3004 if (err < 0)
3005 return err;
3006 cm->iobase = pci_resource_start(pci, 0);
3007
3008 if (devm_request_irq(&pci->dev, pci->irq, snd_cmipci_interrupt,
3009 IRQF_SHARED, KBUILD_MODNAME, cm)) {
3010 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
3011 return -EBUSY;
3012 }
3013 cm->irq = pci->irq;
3014 card->sync_irq = cm->irq;
3015 card->private_free = snd_cmipci_free;
3016
3017 pci_set_master(cm->pci);
3018
3019 /*
3020 * check chip version, max channels and capabilities
3021 */
3022
3023 cm->chip_version = 0;
3024 cm->max_channels = 2;
3025 cm->do_soft_ac3 = soft_ac3[dev];
3026
3027 if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
3028 pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
3029 query_chip(cm);
3030 /* added -MCx suffix for chip supporting multi-channels */
3031 if (cm->can_multi_ch)
3032 sprintf(cm->card->driver + strlen(cm->card->driver),
3033 "-MC%d", cm->max_channels);
3034 else if (cm->can_ac3_sw)
3035 strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
3036
3037 cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3038 cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3039
3040#if CM_CH_PLAY == 1
3041 cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
3042#else
3043 cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
3044#endif
3045
3046 /* initialize codec registers */
3047 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
3048 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
3049 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
3050 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3051 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3052 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
3053 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
3054
3055 snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
3056 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
3057#if CM_CH_PLAY == 1
3058 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
3059#else
3060 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
3061#endif
3062 if (cm->chip_version) {
3063 snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
3064 snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
3065 }
3066 /* Set Bus Master Request */
3067 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
3068
3069 /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
3070 switch (pci->device) {
3071 case PCI_DEVICE_ID_CMEDIA_CM8738:
3072 case PCI_DEVICE_ID_CMEDIA_CM8738B:
3073 if (!pci_dev_present(intel_82437vx))
3074 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
3075 break;
3076 default:
3077 break;
3078 }
3079
3080 if (cm->chip_version < 68) {
3081 val = pci->device < 0x110 ? 8338 : 8738;
3082 } else {
3083 switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
3084 case 0:
3085 val = 8769;
3086 break;
3087 case 2:
3088 val = 8762;
3089 break;
3090 default:
3091 switch ((pci->subsystem_vendor << 16) |
3092 pci->subsystem_device) {
3093 case 0x13f69761:
3094 case 0x584d3741:
3095 case 0x584d3751:
3096 case 0x584d3761:
3097 case 0x584d3771:
3098 case 0x72848384:
3099 val = 8770;
3100 break;
3101 default:
3102 val = 8768;
3103 break;
3104 }
3105 }
3106 }
3107 sprintf(card->shortname, "C-Media CMI%d", val);
3108 if (cm->chip_version < 68)
3109 scnprintf(modelstr, sizeof(modelstr),
3110 " (model %d)", cm->chip_version);
3111 else
3112 modelstr[0] = '\0';
3113 scnprintf(card->longname, sizeof(card->longname),
3114 "%s%s at %#lx, irq %i",
3115 card->shortname, modelstr, cm->iobase, cm->irq);
3116
3117 if (cm->chip_version >= 39) {
3118 val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
3119 if (val != 0x00 && val != 0xff) {
3120 if (mpu_port[dev])
3121 iomidi = cm->iobase + CM_REG_MPU_PCI;
3122 integrated_midi = 1;
3123 }
3124 }
3125 if (!integrated_midi) {
3126 val = 0;
3127 iomidi = mpu_port[dev];
3128 switch (iomidi) {
3129 case 0x320: val = CM_VMPU_320; break;
3130 case 0x310: val = CM_VMPU_310; break;
3131 case 0x300: val = CM_VMPU_300; break;
3132 case 0x330: val = CM_VMPU_330; break;
3133 default:
3134 iomidi = 0; break;
3135 }
3136 if (iomidi > 0) {
3137 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
3138 /* enable UART */
3139 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
3140 if (inb(iomidi + 1) == 0xff) {
3141 dev_err(cm->card->dev,
3142 "cannot enable MPU-401 port at %#lx\n",
3143 iomidi);
3144 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
3145 CM_UART_EN);
3146 iomidi = 0;
3147 }
3148 }
3149 }
3150
3151 if (cm->chip_version < 68) {
3152 err = snd_cmipci_create_fm(cm, fm_port[dev]);
3153 if (err < 0)
3154 return err;
3155 }
3156
3157 /* reset mixer */
3158 snd_cmipci_mixer_write(cm, 0, 0);
3159
3160 snd_cmipci_proc_init(cm);
3161
3162 /* create pcm devices */
3163 pcm_index = pcm_spdif_index = 0;
3164 err = snd_cmipci_pcm_new(cm, pcm_index);
3165 if (err < 0)
3166 return err;
3167 pcm_index++;
3168 err = snd_cmipci_pcm2_new(cm, pcm_index);
3169 if (err < 0)
3170 return err;
3171 pcm_index++;
3172 if (cm->can_ac3_hw || cm->can_ac3_sw) {
3173 pcm_spdif_index = pcm_index;
3174 err = snd_cmipci_pcm_spdif_new(cm, pcm_index);
3175 if (err < 0)
3176 return err;
3177 }
3178
3179 /* create mixer interface & switches */
3180 err = snd_cmipci_mixer_new(cm, pcm_spdif_index);
3181 if (err < 0)
3182 return err;
3183
3184 if (iomidi > 0) {
3185 err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
3186 iomidi,
3187 (integrated_midi ?
3188 MPU401_INFO_INTEGRATED : 0) |
3189 MPU401_INFO_IRQ_HOOK,
3190 -1, &cm->rmidi);
3191 if (err < 0)
3192 dev_err(cm->card->dev,
3193 "no UART401 device at 0x%lx\n", iomidi);
3194 }
3195
3196#ifdef USE_VAR48KRATE
3197 for (val = 0; val < ARRAY_SIZE(rates); val++)
3198 snd_cmipci_set_pll(cm, rates[val], val);
3199
3200 /*
3201 * (Re-)Enable external switch spdo_48k
3202 */
3203 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
3204#endif /* USE_VAR48KRATE */
3205
3206 if (snd_cmipci_create_gameport(cm, dev) < 0)
3207 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
3208
3209 return 0;
3210}
3211
3212/*
3213 */
3214
3215MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
3216
3217static int snd_cmipci_probe(struct pci_dev *pci,
3218 const struct pci_device_id *pci_id)
3219{
3220 static int dev;
3221 struct snd_card *card;
3222 int err;
3223
3224 if (dev >= SNDRV_CARDS)
3225 return -ENODEV;
3226 if (! enable[dev]) {
3227 dev++;
3228 return -ENOENT;
3229 }
3230
3231 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
3232 sizeof(struct cmipci), &card);
3233 if (err < 0)
3234 return err;
3235
3236 switch (pci->device) {
3237 case PCI_DEVICE_ID_CMEDIA_CM8738:
3238 case PCI_DEVICE_ID_CMEDIA_CM8738B:
3239 strcpy(card->driver, "CMI8738");
3240 break;
3241 case PCI_DEVICE_ID_CMEDIA_CM8338A:
3242 case PCI_DEVICE_ID_CMEDIA_CM8338B:
3243 strcpy(card->driver, "CMI8338");
3244 break;
3245 default:
3246 strcpy(card->driver, "CMIPCI");
3247 break;
3248 }
3249
3250 err = snd_cmipci_create(card, pci, dev);
3251 if (err < 0)
3252 goto error;
3253
3254 err = snd_card_register(card);
3255 if (err < 0)
3256 goto error;
3257
3258 pci_set_drvdata(pci, card);
3259 dev++;
3260 return 0;
3261
3262 error:
3263 snd_card_free(card);
3264 return err;
3265}
3266
3267/*
3268 * power management
3269 */
3270static const unsigned char saved_regs[] = {
3271 CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
3272 CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_AUX_VOL, CM_REG_PLL,
3273 CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
3274 CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
3275 CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
3276};
3277
3278static const unsigned char saved_mixers[] = {
3279 SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
3280 SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
3281 SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
3282 SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
3283 SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
3284 SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
3285 CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
3286 SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
3287};
3288
3289static int snd_cmipci_suspend(struct device *dev)
3290{
3291 struct snd_card *card = dev_get_drvdata(dev);
3292 struct cmipci *cm = card->private_data;
3293 int i;
3294
3295 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3296
3297 /* save registers */
3298 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3299 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3300 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3301 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3302
3303 /* disable ints */
3304 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3305 return 0;
3306}
3307
3308static int snd_cmipci_resume(struct device *dev)
3309{
3310 struct snd_card *card = dev_get_drvdata(dev);
3311 struct cmipci *cm = card->private_data;
3312 int i;
3313
3314 /* reset / initialize to a sane state */
3315 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3316 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3317 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3318 snd_cmipci_mixer_write(cm, 0, 0);
3319
3320 /* restore registers */
3321 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3322 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3323 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3324 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
3325
3326 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3327 return 0;
3328}
3329
3330static DEFINE_SIMPLE_DEV_PM_OPS(snd_cmipci_pm, snd_cmipci_suspend, snd_cmipci_resume);
3331
3332static struct pci_driver cmipci_driver = {
3333 .name = KBUILD_MODNAME,
3334 .id_table = snd_cmipci_ids,
3335 .probe = snd_cmipci_probe,
3336 .driver = {
3337 .pm = &snd_cmipci_pm,
3338 },
3339};
3340
3341module_pci_driver(cmipci_driver);
Note: See TracBrowser for help on using the repository browser.