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

Last change on this file since 680 was 679, checked in by David Azarewicz, 5 years ago

Merge changes from Paul's uniaud32next branch.

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