blob: 7cf4fc193419789f81b9a88f09e08da8b1fa98e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for C-Media CMI8338 and 8738 PCI soundcards.
3 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/* Does not work. Warning may block system in capture mode */
21/* #define USE_VAR48KRATE */
22
23#include <sound/driver.h>
24#include <asm/io.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/init.h>
28#include <linux/pci.h>
29#include <linux/slab.h>
30#include <linux/gameport.h>
31#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/core.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/pcm.h>
37#include <sound/rawmidi.h>
38#include <sound/mpu401.h>
39#include <sound/opl3.h>
40#include <sound/sb.h>
41#include <sound/asoundef.h>
42#include <sound/initval.h>
43
44MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
45MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
46MODULE_LICENSE("GPL");
47MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
48 "{C-Media,CMI8738B},"
49 "{C-Media,CMI8338A},"
50 "{C-Media,CMI8338B}}");
51
52#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
53#define SUPPORT_JOYSTICK 1
54#endif
55
56static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
57static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
58static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
59static long mpu_port[SNDRV_CARDS];
Takashi Iwai2f24d1592007-02-15 18:56:43 +010060static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
62#ifdef SUPPORT_JOYSTICK
63static int joystick_port[SNDRV_CARDS];
64#endif
65
66module_param_array(index, int, NULL, 0444);
67MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
68module_param_array(id, charp, NULL, 0444);
69MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
70module_param_array(enable, bool, NULL, 0444);
71MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
72module_param_array(mpu_port, long, NULL, 0444);
73MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
74module_param_array(fm_port, long, NULL, 0444);
75MODULE_PARM_DESC(fm_port, "FM port.");
76module_param_array(soft_ac3, bool, NULL, 0444);
77MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
78#ifdef SUPPORT_JOYSTICK
79module_param_array(joystick_port, int, NULL, 0444);
80MODULE_PARM_DESC(joystick_port, "Joystick port address.");
81#endif
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * CM8x38 registers definition
85 */
86
87#define CM_REG_FUNCTRL0 0x00
88#define CM_RST_CH1 0x00080000
89#define CM_RST_CH0 0x00040000
90#define CM_CHEN1 0x00020000 /* ch1: enable */
91#define CM_CHEN0 0x00010000 /* ch0: enable */
92#define CM_PAUSE1 0x00000008 /* ch1: pause */
93#define CM_PAUSE0 0x00000004 /* ch0: pause */
94#define CM_CHADC1 0x00000002 /* ch1, 0:playback, 1:record */
95#define CM_CHADC0 0x00000001 /* ch0, 0:playback, 1:record */
96
97#define CM_REG_FUNCTRL1 0x04
Clemens Ladischa839a332007-09-17 09:35:46 +020098#define CM_DSFC_MASK 0x0000E000 /* channel 1 (DAC?) sampling frequency */
99#define CM_DSFC_SHIFT 13
100#define CM_ASFC_MASK 0x00001C00 /* channel 0 (ADC?) sampling frequency */
101#define CM_ASFC_SHIFT 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define CM_SPDF_1 0x00000200 /* SPDIF IN/OUT at channel B */
103#define CM_SPDF_0 0x00000100 /* SPDIF OUT only channel A */
Clemens Ladischa839a332007-09-17 09:35:46 +0200104#define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/IN -> OUT loopback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
106#define CM_INTRM 0x00000020 /* master control block (MCB) interrupt enabled */
107#define CM_BREQ 0x00000010 /* bus master enabled */
108#define CM_VOICE_EN 0x00000008 /* legacy voice (SB16,FM) */
Clemens Ladischa839a332007-09-17 09:35:46 +0200109#define CM_UART_EN 0x00000004 /* legacy UART */
110#define CM_JYSTK_EN 0x00000002 /* legacy joystick */
111#define CM_ZVPORT 0x00000001 /* ZVPORT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#define CM_REG_CHFORMAT 0x08
114
115#define CM_CHB3D5C 0x80000000 /* 5,6 channels */
Clemens Ladischa839a332007-09-17 09:35:46 +0200116#define CM_FMOFFSET2 0x40000000 /* initial FM PCM offset 2 when Fmute=1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define CM_CHB3D 0x20000000 /* 4 channels */
118
119#define CM_CHIP_MASK1 0x1f000000
120#define CM_CHIP_037 0x01000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200121#define CM_SETLAT48 0x00800000 /* set latency timer 48h */
122#define CM_EDGEIRQ 0x00400000 /* emulated edge trigger legacy IRQ */
123#define CM_SPD24SEL39 0x00200000 /* 24-bit spdif: model 039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define CM_AC3EN1 0x00100000 /* enable AC3: model 037 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200125#define CM_SPDIF_SELECT1 0x00080000 /* for model <= 037 ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define CM_SPD24SEL 0x00020000 /* 24bit spdif: model 037 */
127/* #define CM_SPDIF_INVERSE 0x00010000 */ /* ??? */
128
129#define CM_ADCBITLEN_MASK 0x0000C000
130#define CM_ADCBITLEN_16 0x00000000
131#define CM_ADCBITLEN_15 0x00004000
132#define CM_ADCBITLEN_14 0x00008000
133#define CM_ADCBITLEN_13 0x0000C000
134
Clemens Ladischa839a332007-09-17 09:35:46 +0200135#define CM_ADCDACLEN_MASK 0x00003000 /* model 037 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define CM_ADCDACLEN_060 0x00000000
137#define CM_ADCDACLEN_066 0x00001000
138#define CM_ADCDACLEN_130 0x00002000
139#define CM_ADCDACLEN_280 0x00003000
140
Clemens Ladischa839a332007-09-17 09:35:46 +0200141#define CM_ADCDLEN_MASK 0x00003000 /* model 039 */
142#define CM_ADCDLEN_ORIGINAL 0x00000000
143#define CM_ADCDLEN_EXTRA 0x00001000
144#define CM_ADCDLEN_24K 0x00002000
145#define CM_ADCDLEN_WEIGHT 0x00003000
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define CM_CH1_SRATE_176K 0x00000800
Clemens Ladisch8992e182007-09-03 09:54:55 +0200148#define CM_CH1_SRATE_96K 0x00000800 /* model 055? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define CM_CH1_SRATE_88K 0x00000400
150#define CM_CH0_SRATE_176K 0x00000200
Clemens Ladisch8992e182007-09-03 09:54:55 +0200151#define CM_CH0_SRATE_96K 0x00000200 /* model 055? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define CM_CH0_SRATE_88K 0x00000100
153
154#define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
Clemens Ladischa839a332007-09-17 09:35:46 +0200155#define CM_DBLSPDS 0x00000040 /* double SPDIF sample rate 88.2/96 */
156#define CM_POLVALID 0x00000020 /* inverse SPDIF/IN valid bit */
157#define CM_SPDLOCKED 0x00000010
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Clemens Ladischa839a332007-09-17 09:35:46 +0200159#define CM_CH1FMT_MASK 0x0000000C /* bit 3: 16 bits, bit 2: stereo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define CM_CH1FMT_SHIFT 2
Clemens Ladischa839a332007-09-17 09:35:46 +0200161#define CM_CH0FMT_MASK 0x00000003 /* bit 1: 16 bits, bit 0: stereo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define CM_CH0FMT_SHIFT 0
163
164#define CM_REG_INT_HLDCLR 0x0C
165#define CM_CHIP_MASK2 0xff000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200166#define CM_CHIP_8768 0x20000000
167#define CM_CHIP_055 0x08000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define CM_CHIP_039 0x04000000
169#define CM_CHIP_039_6CH 0x01000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200170#define CM_UNKNOWN_INT_EN 0x00080000 /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define CM_TDMA_INT_EN 0x00040000
172#define CM_CH1_INT_EN 0x00020000
173#define CM_CH0_INT_EN 0x00010000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175#define CM_REG_INT_STATUS 0x10
176#define CM_INTR 0x80000000
177#define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
178#define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
179#define CM_UARTINT 0x00010000
180#define CM_LTDMAINT 0x00008000
181#define CM_HTDMAINT 0x00004000
182#define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
183#define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
184#define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
185#define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
186#define CM_CH1BUSY 0x00000008
187#define CM_CH0BUSY 0x00000004
188#define CM_CHINT1 0x00000002
189#define CM_CHINT0 0x00000001
190
191#define CM_REG_LEGACY_CTRL 0x14
Clemens Ladischa839a332007-09-17 09:35:46 +0200192#define CM_NXCHG 0x80000000 /* don't map base reg dword->sample */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
194#define CM_VMPU_330 0x00000000
195#define CM_VMPU_320 0x20000000
196#define CM_VMPU_310 0x40000000
197#define CM_VMPU_300 0x60000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200198#define CM_ENWR8237 0x10000000 /* enable bus master to write 8237 base reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
200#define CM_VSBSEL_220 0x00000000
201#define CM_VSBSEL_240 0x04000000
202#define CM_VSBSEL_260 0x08000000
203#define CM_VSBSEL_280 0x0C000000
204#define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
205#define CM_FMSEL_388 0x00000000
206#define CM_FMSEL_3C8 0x01000000
207#define CM_FMSEL_3E0 0x02000000
208#define CM_FMSEL_3E8 0x03000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200209#define CM_ENSPDOUT 0x00800000 /* enable XSPDIF/OUT to I/O interface */
210#define CM_SPDCOPYRHT 0x00400000 /* spdif in/out copyright bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
Clemens Ladischa839a332007-09-17 09:35:46 +0200212#define CM_INVIDWEN 0x00100000 /* internal vendor ID write enable, model 039? */
213#define CM_SETRETRY 0x00100000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
214#define CM_C_EEACCESS 0x00080000 /* direct programming eeprom regs */
215#define CM_C_EECS 0x00040000
216#define CM_C_EEDI46 0x00020000
217#define CM_C_EECK46 0x00010000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
Clemens Ladischa839a332007-09-17 09:35:46 +0200219#define CM_CENTR2LIN 0x00004000 /* line-in as center out */
220#define CM_BASE2LIN 0x00002000 /* line-in as bass out */
221#define CM_EXBASEN 0x00001000 /* external bass input enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#define CM_REG_MISC_CTRL 0x18
Clemens Ladischa839a332007-09-17 09:35:46 +0200224#define CM_PWD 0x80000000 /* power down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define CM_RESET 0x40000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200226#define CM_SFIL_MASK 0x30000000 /* filter control at front end DAC, model 037? */
227#define CM_VMGAIN 0x10000000 /* analog master amp +6dB, model 039? */
228#define CM_TXVX 0x08000000 /* model 037? */
229#define CM_N4SPK3D 0x04000000 /* copy front to rear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
231#define CM_SPDIF48K 0x01000000 /* write */
232#define CM_SPATUS48K 0x01000000 /* read */
Clemens Ladischa839a332007-09-17 09:35:46 +0200233#define CM_ENDBDAC 0x00800000 /* enable double dac */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
235#define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
Clemens Ladischa839a332007-09-17 09:35:46 +0200236#define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
237#define CM_FM_EN 0x00080000 /* enable legacy FM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200239#define CM_ENWRASID 0x00010000 /* choose writable internal SUBID (audio) */
240#define CM_VIDWPDSB 0x00010000 /* model 037? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
Clemens Ladischa839a332007-09-17 09:35:46 +0200242#define CM_MASK_EN 0x00004000 /* activate channel mask on legacy DMA */
243#define CM_ENWRMSID 0x00002000 /* choose writable internal SUBID (modem) */
244#define CM_VIDWPPRT 0x00002000 /* model 037? */
245#define CM_SFILENB 0x00001000 /* filter stepping at front end DAC, model 037? */
246#define CM_MMODE_MASK 0x00000E00 /* model DAA interface mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
248#define CM_ENCENTER 0x00000080
Clemens Ladischa839a332007-09-17 09:35:46 +0200249#define CM_FLINKON 0x00000080 /* force modem link detection on, model 037 */
250#define CM_MUTECH1 0x00000040 /* mute PCI ch1 to DAC */
251#define CM_FLINKOFF 0x00000040 /* force modem link detection off, model 037 */
252#define CM_UNKNOWN_18_5 0x00000020 /* ? */
253#define CM_MIDSMP 0x00000010 /* 1/2 interpolation at front end DAC */
254#define CM_UPDDMA_MASK 0x0000000C /* TDMA position update notification */
255#define CM_UPDDMA_2048 0x00000000
256#define CM_UPDDMA_1024 0x00000004
257#define CM_UPDDMA_512 0x00000008
258#define CM_UPDDMA_256 0x0000000C
259#define CM_TWAIT_MASK 0x00000003 /* model 037 */
260#define CM_TWAIT1 0x00000002 /* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
261#define CM_TWAIT0 0x00000001 /* i/o cycle, 0: 4, 1: 6 PCICLKs */
262
263#define CM_REG_TDMA_POSITION 0x1C
264#define CM_TDMA_CNT_MASK 0xFFFF0000 /* current byte/word count */
265#define CM_TDMA_ADR_MASK 0x0000FFFF /* current address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* byte */
268#define CM_REG_MIXER0 0x20
Clemens Ladischa839a332007-09-17 09:35:46 +0200269#define CM_REG_SBVR 0x20 /* write: sb16 version */
270#define CM_REG_DEV 0x20 /* read: hardware device version */
271
272#define CM_REG_MIXER21 0x21
273#define CM_UNKNOWN_21_MASK 0x78 /* ? */
274#define CM_X_ADPCM 0x04 /* SB16 ADPCM enable */
275#define CM_PROINV 0x02 /* SBPro left/right channel switching */
276#define CM_X_SB16 0x01 /* SB16 compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278#define CM_REG_SB16_DATA 0x22
279#define CM_REG_SB16_ADDR 0x23
280
281#define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
282#define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
283#define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
284#define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
285
286#define CM_REG_MIXER1 0x24
287#define CM_FMMUTE 0x80 /* mute FM */
288#define CM_FMMUTE_SHIFT 7
289#define CM_WSMUTE 0x40 /* mute PCM */
290#define CM_WSMUTE_SHIFT 6
Clemens Ladischa839a332007-09-17 09:35:46 +0200291#define CM_REAR2LIN 0x20 /* lin-in -> rear line out */
292#define CM_REAR2LIN_SHIFT 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#define CM_REAR2FRONT 0x10 /* exchange rear/front */
294#define CM_REAR2FRONT_SHIFT 4
295#define CM_WAVEINL 0x08 /* digital wave rec. left chan */
296#define CM_WAVEINL_SHIFT 3
297#define CM_WAVEINR 0x04 /* digical wave rec. right */
298#define CM_WAVEINR_SHIFT 2
299#define CM_X3DEN 0x02 /* 3D surround enable */
300#define CM_X3DEN_SHIFT 1
301#define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
302#define CM_CDPLAY_SHIFT 0
303
304#define CM_REG_MIXER2 0x25
305#define CM_RAUXREN 0x80 /* AUX right capture */
306#define CM_RAUXREN_SHIFT 7
307#define CM_RAUXLEN 0x40 /* AUX left capture */
308#define CM_RAUXLEN_SHIFT 6
309#define CM_VAUXRM 0x20 /* AUX right mute */
310#define CM_VAUXRM_SHIFT 5
311#define CM_VAUXLM 0x10 /* AUX left mute */
312#define CM_VAUXLM_SHIFT 4
313#define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
314#define CM_VADMIC_SHIFT 1
315#define CM_MICGAINZ 0x01 /* mic boost */
316#define CM_MICGAINZ_SHIFT 0
317
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100318#define CM_REG_MIXER3 0x24
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define CM_REG_AUX_VOL 0x26
320#define CM_VAUXL_MASK 0xf0
321#define CM_VAUXR_MASK 0x0f
322
323#define CM_REG_MISC 0x27
Clemens Ladischa839a332007-09-17 09:35:46 +0200324#define CM_UNKNOWN_27_MASK 0xd8 /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define CM_XGPO1 0x20
326// #define CM_XGPBIO 0x04
327#define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
328#define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
329#define CM_SPDVALID 0x02 /* spdif input valid check */
Clemens Ladischa839a332007-09-17 09:35:46 +0200330#define CM_DMAUTO 0x01 /* SB16 DMA auto detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332#define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
333/*
334 * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
335 * or identical with AC97 codec?
336 */
337#define CM_REG_EXTERN_CODEC CM_REG_AC97
338
339/*
340 * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
341 */
342#define CM_REG_MPU_PCI 0x40
343
344/*
345 * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
346 */
347#define CM_REG_FM_PCI 0x50
348
349/*
Takashi Iwai2eff7ec2005-06-30 13:45:20 +0200350 * access from SB-mixer port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352#define CM_REG_EXTENT_IND 0xf0
353#define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
354#define CM_VPHONE_SHIFT 5
355#define CM_VPHOM 0x10 /* Phone mute control */
356#define CM_VSPKM 0x08 /* Speaker mute control, default high */
357#define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
358#define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
Takashi Iwai2eff7ec2005-06-30 13:45:20 +0200359#define CM_VADMIC3 0x01 /* Mic record boost */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/*
362 * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
363 * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
364 * unit (readonly?).
365 */
366#define CM_REG_PLL 0xf8
367
368/*
369 * extended registers
370 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200371#define CM_REG_CH0_FRAME1 0x80 /* write: base address */
372#define CM_REG_CH0_FRAME2 0x84 /* read: current address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
374#define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
Clemens Ladischa839a332007-09-17 09:35:46 +0200375
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100376#define CM_REG_EXT_MISC 0x90
Clemens Ladischa839a332007-09-17 09:35:46 +0200377#define CM_ADC48K44K 0x10000000 /* ADC parameters group, 0: 44k, 1: 48k */
378#define CM_CHB3D8C 0x00200000 /* 7.1 channels support */
379#define CM_SPD32FMT 0x00100000 /* SPDIF/IN 32k sample rate */
380#define CM_ADC2SPDIF 0x00080000 /* ADC output to SPDIF/OUT */
381#define CM_SHAREADC 0x00040000 /* DAC in ADC as Center/LFE */
382#define CM_REALTCMP 0x00020000 /* monitor the CMPL/CMPR of ADC */
383#define CM_INVLRCK 0x00010000 /* invert ZVPORT's LRCK */
384#define CM_UNKNOWN_90_MASK 0x0000FFFF /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/*
387 * size of i/o region
388 */
389#define CM_EXTENT_CODEC 0x100
390#define CM_EXTENT_MIDI 0x2
391#define CM_EXTENT_SYNTH 0x4
392
393
394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * channels for playback / capture
396 */
397#define CM_CH_PLAY 0
398#define CM_CH_CAPT 1
399
400/*
401 * flags to check device open/close
402 */
403#define CM_OPEN_NONE 0
404#define CM_OPEN_CH_MASK 0x01
405#define CM_OPEN_DAC 0x10
406#define CM_OPEN_ADC 0x20
407#define CM_OPEN_SPDIF 0x40
408#define CM_OPEN_MCHAN 0x80
409#define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
410#define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
411#define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
412#define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
413#define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
414#define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
415
416
417#if CM_CH_PLAY == 1
418#define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
419#define CM_PLAYBACK_SPDF CM_SPDF_1
420#define CM_CAPTURE_SPDF CM_SPDF_0
421#else
422#define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
423#define CM_PLAYBACK_SPDF CM_SPDF_0
424#define CM_CAPTURE_SPDF CM_SPDF_1
425#endif
426
427
428/*
429 * driver data
430 */
431
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100432struct cmipci_pcm {
433 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int running; /* dac/adc running? */
435 unsigned int dma_size; /* in frames */
436 unsigned int period_size; /* in frames */
437 unsigned int offset; /* physical address of the buffer */
438 unsigned int fmt; /* format bits */
439 int ch; /* channel (0/1) */
440 unsigned int is_dac; /* is dac? */
441 int bytes_per_frame;
442 int shift;
443};
444
445/* mixer elements toggled/resumed during ac3 playback */
446struct cmipci_mixer_auto_switches {
447 const char *name; /* switch to toggle */
448 int toggle_on; /* value to change when ac3 mode */
449};
450static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
451 {"PCM Playback Switch", 0},
452 {"IEC958 Output Switch", 1},
453 {"IEC958 Mix Analog", 0},
454 // {"IEC958 Out To DAC", 1}, // no longer used
455 {"IEC958 Loop", 0},
456};
457#define CM_SAVED_MIXERS ARRAY_SIZE(cm_saved_mixer)
458
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100459struct cmipci {
460 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 struct pci_dev *pci;
463 unsigned int device; /* device ID */
464 int irq;
465
466 unsigned long iobase;
467 unsigned int ctrl; /* FUNCTRL0 current value */
468
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100469 struct snd_pcm *pcm; /* DAC/ADC PCM */
470 struct snd_pcm *pcm2; /* 2nd DAC */
471 struct snd_pcm *pcm_spdif; /* SPDIF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 int chip_version;
474 int max_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned int can_ac3_sw: 1;
476 unsigned int can_ac3_hw: 1;
477 unsigned int can_multi_ch: 1;
478 unsigned int do_soft_ac3: 1;
479
480 unsigned int spdif_playback_avail: 1; /* spdif ready? */
481 unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
482 int spdif_counter; /* for software AC3 */
483
484 unsigned int dig_status;
485 unsigned int dig_pcm_status;
486
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100487 struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 int opened[2]; /* open mode */
Ingo Molnar62932df2006-01-16 16:34:20 +0100490 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 unsigned int mixer_insensitive: 1;
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100493 struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int mixer_res_status[CM_SAVED_MIXERS];
495
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100496 struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* external MIDI */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100499 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501#ifdef SUPPORT_JOYSTICK
502 struct gameport *gameport;
503#endif
504
505 spinlock_t reg_lock;
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100506
507#ifdef CONFIG_PM
508 unsigned int saved_regs[0x20];
509 unsigned char saved_mixers[0x20];
510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
513
514/* read/write operations for dword register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100515static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 outl(data, cm->iobase + cmd);
518}
Jesper Juhl77933d72005-07-27 11:46:09 -0700519
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100520static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 return inl(cm->iobase + cmd);
523}
524
525/* read/write operations for word register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100526static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 outw(data, cm->iobase + cmd);
529}
Jesper Juhl77933d72005-07-27 11:46:09 -0700530
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100531static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 return inw(cm->iobase + cmd);
534}
535
536/* read/write operations for byte register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100537static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 outb(data, cm->iobase + cmd);
540}
541
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100542static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 return inb(cm->iobase + cmd);
545}
546
547/* bit operations for dword register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100548static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200550 unsigned int val, oval;
551 val = oval = inl(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 val |= flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200553 if (val == oval)
554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 outl(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200556 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100559static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200561 unsigned int val, oval;
562 val = oval = inl(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 val &= ~flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200564 if (val == oval)
565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 outl(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200567 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/* bit operations for byte register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100571static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200573 unsigned char val, oval;
574 val = oval = inb(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 val |= flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200576 if (val == oval)
577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 outb(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200579 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100582static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200584 unsigned char val, oval;
585 val = oval = inb(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 val &= ~flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200587 if (val == oval)
588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 outb(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200590 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593
594/*
595 * PCM interface
596 */
597
598/*
599 * calculate frequency
600 */
601
602static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
603
604static unsigned int snd_cmipci_rate_freq(unsigned int rate)
605{
606 unsigned int i;
Clemens Ladisch0f28eca2007-09-17 09:40:24 +0200607
608 if (rate > 48000)
609 rate /= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 for (i = 0; i < ARRAY_SIZE(rates); i++) {
611 if (rates[i] == rate)
612 return i;
613 }
614 snd_BUG();
615 return 0;
616}
617
618#ifdef USE_VAR48KRATE
619/*
620 * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
621 * does it this way .. maybe not. Never get any information from C-Media about
622 * that <werner@suse.de>.
623 */
624static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
625{
626 unsigned int delta, tolerance;
627 int xm, xn, xr;
628
629 for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
630 rate <<= 1;
631 *n = -1;
632 if (*r > 0xff)
633 goto out;
634 tolerance = rate*CM_TOLERANCE_RATE;
635
636 for (xn = (1+2); xn < (0x1f+2); xn++) {
637 for (xm = (1+2); xm < (0xff+2); xm++) {
638 xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
639
640 if (xr < rate)
641 delta = rate - xr;
642 else
643 delta = xr - rate;
644
645 /*
646 * If we found one, remember this,
647 * and try to find a closer one
648 */
649 if (delta < tolerance) {
650 tolerance = delta;
651 *m = xm - 2;
652 *n = xn - 2;
653 }
654 }
655 }
656out:
657 return (*n > -1);
658}
659
660/*
661 * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
662 * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
663 * at the register CM_REG_FUNCTRL1 (0x04).
664 * Problem: other ways are also possible (any information about that?)
665 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100666static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 unsigned int reg = CM_REG_PLL + slot;
669 /*
670 * Guess that this programs at reg. 0x04 the pos 15:13/12:10
671 * for DSFC/ASFC (000 upto 111).
672 */
673
674 /* FIXME: Init (Do we've to set an other register first before programming?) */
675
676 /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
677 snd_cmipci_write_b(cm, reg, rate>>8);
678 snd_cmipci_write_b(cm, reg, rate&0xff);
679
680 /* FIXME: Setup (Do we've to set an other register first to enable this?) */
681}
682#endif /* USE_VAR48KRATE */
683
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100684static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
685 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
688}
689
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100690static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
691 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100693 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (params_channels(hw_params) > 2) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100695 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (cm->opened[CM_CH_PLAY]) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100697 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -EBUSY;
699 }
700 /* reserve the channel A */
701 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
Ingo Molnar62932df2006-01-16 16:34:20 +0100702 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
705}
706
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100707static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int reset = CM_RST_CH0 << (cm->channel[ch].ch);
710 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
711 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
712 udelay(10);
713}
714
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100715static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 return snd_pcm_lib_free_pages(substream);
718}
719
720
721/*
722 */
723
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200724static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100725static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .count = 3,
727 .list = hw_channels,
728 .mask = 0,
729};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100730static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200731 .count = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .list = hw_channels,
733 .mask = 0,
734};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100735static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200736 .count = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 .list = hw_channels,
738 .mask = 0,
739};
740
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100741static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 if (channels > 2) {
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200744 if (!cm->can_multi_ch || !rec->ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -EINVAL;
746 if (rec->fmt != 0x03) /* stereo 16bit only */
747 return -EINVAL;
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200750 if (cm->can_multi_ch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_lock_irq(&cm->reg_lock);
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200752 if (channels > 2) {
753 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
754 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200757 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
758 }
759 if (channels == 8)
760 snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
761 else
762 snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
763 if (channels == 6) {
764 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
765 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
766 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
768 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200770 if (channels == 4)
771 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
772 else
773 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
774 spin_unlock_irq(&cm->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 return 0;
777}
778
779
780/*
781 * prepare playback/capture channel
782 * channel to be used must have been set in rec->ch.
783 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100784static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
785 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 unsigned int reg, freq, val;
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100788 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 rec->fmt = 0;
791 rec->shift = 0;
792 if (snd_pcm_format_width(runtime->format) >= 16) {
793 rec->fmt |= 0x02;
794 if (snd_pcm_format_width(runtime->format) > 16)
795 rec->shift++; /* 24/32bit */
796 }
797 if (runtime->channels > 1)
798 rec->fmt |= 0x01;
799 if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
800 snd_printd("cannot set dac channels\n");
801 return -EINVAL;
802 }
803
804 rec->offset = runtime->dma_addr;
805 /* buffer and period sizes in frame */
806 rec->dma_size = runtime->buffer_size << rec->shift;
807 rec->period_size = runtime->period_size << rec->shift;
808 if (runtime->channels > 2) {
809 /* multi-channels */
810 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
811 rec->period_size = (rec->period_size * runtime->channels) / 2;
812 }
813
814 spin_lock_irq(&cm->reg_lock);
815
816 /* set buffer address */
817 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
818 snd_cmipci_write(cm, reg, rec->offset);
819 /* program sample counts */
820 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
821 snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
822 snd_cmipci_write_w(cm, reg + 2, rec->period_size - 1);
823
824 /* set adc/dac flag */
825 val = rec->ch ? CM_CHADC1 : CM_CHADC0;
826 if (rec->is_dac)
827 cm->ctrl &= ~val;
828 else
829 cm->ctrl |= val;
830 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
831 //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
832
833 /* set sample rate */
834 freq = snd_cmipci_rate_freq(runtime->rate);
835 val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
836 if (rec->ch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 val &= ~CM_DSFC_MASK;
838 val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
Clemens Ladischa839a332007-09-17 09:35:46 +0200839 } else {
840 val &= ~CM_ASFC_MASK;
841 val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
844 //snd_printd("cmipci: functrl1 = %08x\n", val);
845
846 /* set format */
847 val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
848 if (rec->ch) {
849 val &= ~CM_CH1FMT_MASK;
850 val |= rec->fmt << CM_CH1FMT_SHIFT;
851 } else {
852 val &= ~CM_CH0FMT_MASK;
853 val |= rec->fmt << CM_CH0FMT_SHIFT;
854 }
Clemens Ladisch8992e182007-09-03 09:54:55 +0200855 if (cm->chip_version == 68) {
856 if (runtime->rate == 88200)
857 val |= CM_CH0_SRATE_88K << (rec->ch * 2);
858 else
859 val &= ~(CM_CH0_SRATE_88K << (rec->ch * 2));
860 if (runtime->rate == 96000)
861 val |= CM_CH0_SRATE_96K << (rec->ch * 2);
862 else
863 val &= ~(CM_CH0_SRATE_96K << (rec->ch * 2));
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
866 //snd_printd("cmipci: chformat = %08x\n", val);
867
868 rec->running = 0;
869 spin_unlock_irq(&cm->reg_lock);
870
871 return 0;
872}
873
874/*
875 * PCM trigger/stop
876 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100877static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
878 struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 unsigned int inthld, chen, reset, pause;
881 int result = 0;
882
883 inthld = CM_CH0_INT_EN << rec->ch;
884 chen = CM_CHEN0 << rec->ch;
885 reset = CM_RST_CH0 << rec->ch;
886 pause = CM_PAUSE0 << rec->ch;
887
888 spin_lock(&cm->reg_lock);
889 switch (cmd) {
890 case SNDRV_PCM_TRIGGER_START:
891 rec->running = 1;
892 /* set interrupt */
893 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
894 cm->ctrl |= chen;
895 /* enable channel */
896 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
897 //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
898 break;
899 case SNDRV_PCM_TRIGGER_STOP:
900 rec->running = 0;
901 /* disable interrupt */
902 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
903 /* reset */
904 cm->ctrl &= ~chen;
905 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
906 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
907 break;
908 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100909 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 cm->ctrl |= pause;
911 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
912 break;
913 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100914 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 cm->ctrl &= ~pause;
916 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
917 break;
918 default:
919 result = -EINVAL;
920 break;
921 }
922 spin_unlock(&cm->reg_lock);
923 return result;
924}
925
926/*
927 * return the current pointer
928 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100929static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
930 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 size_t ptr;
933 unsigned int reg;
934 if (!rec->running)
935 return 0;
936#if 1 // this seems better..
937 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
938 ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
939 ptr >>= rec->shift;
940#else
941 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
942 ptr = snd_cmipci_read(cm, reg) - rec->offset;
943 ptr = bytes_to_frames(substream->runtime, ptr);
944#endif
945 if (substream->runtime->channels > 2)
946 ptr = (ptr * 2) / substream->runtime->channels;
947 return ptr;
948}
949
950/*
951 * playback
952 */
953
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100954static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int cmd)
956{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100957 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], substream, cmd);
959}
960
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100961static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100963 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
965}
966
967
968
969/*
970 * capture
971 */
972
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100973static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int cmd)
975{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100976 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], substream, cmd);
978}
979
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100980static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100982 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
984}
985
986
987/*
988 * hw preparation for spdif
989 */
990
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100991static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
995 uinfo->count = 1;
996 return 0;
997}
998
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100999static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
1000 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001002 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int i;
1004
1005 spin_lock_irq(&chip->reg_lock);
1006 for (i = 0; i < 4; i++)
1007 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
1008 spin_unlock_irq(&chip->reg_lock);
1009 return 0;
1010}
1011
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001012static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
1013 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001015 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int i, change;
1017 unsigned int val;
1018
1019 val = 0;
1020 spin_lock_irq(&chip->reg_lock);
1021 for (i = 0; i < 4; i++)
1022 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1023 change = val != chip->dig_status;
1024 chip->dig_status = val;
1025 spin_unlock_irq(&chip->reg_lock);
1026 return change;
1027}
1028
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001029static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1032 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1033 .info = snd_cmipci_spdif_default_info,
1034 .get = snd_cmipci_spdif_default_get,
1035 .put = snd_cmipci_spdif_default_put
1036};
1037
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001038static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1042 uinfo->count = 1;
1043 return 0;
1044}
1045
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001046static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1047 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 ucontrol->value.iec958.status[0] = 0xff;
1050 ucontrol->value.iec958.status[1] = 0xff;
1051 ucontrol->value.iec958.status[2] = 0xff;
1052 ucontrol->value.iec958.status[3] = 0xff;
1053 return 0;
1054}
1055
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001056static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001059 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1061 .info = snd_cmipci_spdif_mask_info,
1062 .get = snd_cmipci_spdif_mask_get,
1063};
1064
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001065static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
1066 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1069 uinfo->count = 1;
1070 return 0;
1071}
1072
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001073static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1074 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001076 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int i;
1078
1079 spin_lock_irq(&chip->reg_lock);
1080 for (i = 0; i < 4; i++)
1081 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1082 spin_unlock_irq(&chip->reg_lock);
1083 return 0;
1084}
1085
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001086static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1087 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001089 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int i, change;
1091 unsigned int val;
1092
1093 val = 0;
1094 spin_lock_irq(&chip->reg_lock);
1095 for (i = 0; i < 4; i++)
1096 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1097 change = val != chip->dig_pcm_status;
1098 chip->dig_pcm_status = val;
1099 spin_unlock_irq(&chip->reg_lock);
1100 return change;
1101}
1102
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001103static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1106 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1107 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1108 .info = snd_cmipci_spdif_stream_info,
1109 .get = snd_cmipci_spdif_stream_get,
1110 .put = snd_cmipci_spdif_stream_put
1111};
1112
1113/*
1114 */
1115
1116/* save mixer setting and mute for AC3 playback */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001117static int save_mixer_state(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 if (! cm->mixer_insensitive) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001120 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned int i;
1122
1123 val = kmalloc(sizeof(*val), GFP_ATOMIC);
1124 if (!val)
1125 return -ENOMEM;
1126 for (i = 0; i < CM_SAVED_MIXERS; i++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001127 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (ctl) {
1129 int event;
1130 memset(val, 0, sizeof(*val));
1131 ctl->get(ctl, val);
1132 cm->mixer_res_status[i] = val->value.integer.value[0];
1133 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1134 event = SNDRV_CTL_EVENT_MASK_INFO;
1135 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1136 ctl->put(ctl, val); /* toggle */
1137 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1138 }
1139 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1140 snd_ctl_notify(cm->card, event, &ctl->id);
1141 }
1142 }
1143 kfree(val);
1144 cm->mixer_insensitive = 1;
1145 }
1146 return 0;
1147}
1148
1149
1150/* restore the previously saved mixer status */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001151static void restore_mixer_state(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 if (cm->mixer_insensitive) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001154 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unsigned int i;
1156
1157 val = kmalloc(sizeof(*val), GFP_KERNEL);
1158 if (!val)
1159 return;
1160 cm->mixer_insensitive = 0; /* at first clear this;
1161 otherwise the changes will be ignored */
1162 for (i = 0; i < CM_SAVED_MIXERS; i++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001163 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (ctl) {
1165 int event;
1166
1167 memset(val, 0, sizeof(*val));
1168 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1169 ctl->get(ctl, val);
1170 event = SNDRV_CTL_EVENT_MASK_INFO;
1171 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1172 val->value.integer.value[0] = cm->mixer_res_status[i];
1173 ctl->put(ctl, val);
1174 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1175 }
1176 snd_ctl_notify(cm->card, event, &ctl->id);
1177 }
1178 }
1179 kfree(val);
1180 }
1181}
1182
1183/* spinlock held! */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001184static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 if (do_ac3) {
1187 /* AC3EN for 037 */
1188 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1189 /* AC3EN for 039 */
1190 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1191
1192 if (cm->can_ac3_hw) {
1193 /* SPD24SEL for 037, 0x02 */
1194 /* SPD24SEL for 039, 0x20, but cannot be set */
1195 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1196 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1197 } else { /* can_ac3_sw */
1198 /* SPD32SEL for 037 & 039, 0x20 */
1199 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1200 /* set 176K sample rate to fix 033 HW bug */
1201 if (cm->chip_version == 33) {
1202 if (rate >= 48000) {
1203 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1204 } else {
1205 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1206 }
1207 }
1208 }
1209
1210 } else {
1211 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1212 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1213
1214 if (cm->can_ac3_hw) {
1215 /* chip model >= 37 */
1216 if (snd_pcm_format_width(subs->runtime->format) > 16) {
1217 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1218 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1219 } else {
1220 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1221 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1222 }
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 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1227 }
1228 }
1229}
1230
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001231static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 int rate, err;
1234
1235 rate = subs->runtime->rate;
1236
1237 if (up && do_ac3)
1238 if ((err = save_mixer_state(cm)) < 0)
1239 return err;
1240
1241 spin_lock_irq(&cm->reg_lock);
1242 cm->spdif_playback_avail = up;
1243 if (up) {
1244 /* they are controlled via "IEC958 Output Switch" */
1245 /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1246 /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1247 if (cm->spdif_playback_enabled)
1248 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1249 setup_ac3(cm, subs, do_ac3, rate);
1250
Clemens Ladisch8992e182007-09-03 09:54:55 +02001251 if (rate == 48000 || rate == 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1253 else
1254 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
Clemens Ladisch8992e182007-09-03 09:54:55 +02001255 if (rate > 48000)
1256 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1257 else
1258 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 } else {
1260 /* they are controlled via "IEC958 Output Switch" */
1261 /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1262 /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
Clemens Ladisch8992e182007-09-03 09:54:55 +02001263 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1265 setup_ac3(cm, subs, 0, 0);
1266 }
1267 spin_unlock_irq(&cm->reg_lock);
1268 return 0;
1269}
1270
1271
1272/*
1273 * preparation
1274 */
1275
1276/* playback - enable spdif only on the certain condition */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001277static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001279 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int rate = substream->runtime->rate;
1281 int err, do_spdif, do_ac3 = 0;
1282
Clemens Ladisch8992e182007-09-03 09:54:55 +02001283 do_spdif = (rate >= 44100 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1285 substream->runtime->channels == 2);
1286 if (do_spdif && cm->can_ac3_hw)
1287 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1288 if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
1289 return err;
1290 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1291}
1292
1293/* playback (via device #2) - enable spdif always */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001294static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001296 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int err, do_ac3;
1298
1299 if (cm->can_ac3_hw)
1300 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1301 else
1302 do_ac3 = 1; /* doesn't matter */
1303 if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
1304 return err;
1305 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1306}
1307
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001308static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001310 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 setup_spdif_playback(cm, substream, 0, 0);
1312 restore_mixer_state(cm);
1313 return snd_cmipci_hw_free(substream);
1314}
1315
1316/* capture */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001317static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001319 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1321}
1322
1323/* capture with spdif (via device #2) */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001324static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001326 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 spin_lock_irq(&cm->reg_lock);
1329 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1330 spin_unlock_irq(&cm->reg_lock);
1331
1332 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1333}
1334
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001335static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001337 struct cmipci *cm = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 spin_lock_irq(&cm->reg_lock);
1340 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1341 spin_unlock_irq(&cm->reg_lock);
1342
1343 return snd_cmipci_hw_free(subs);
1344}
1345
1346
1347/*
1348 * interrupt handler
1349 */
David Howells7d12e782006-10-05 14:55:46 +01001350static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001352 struct cmipci *cm = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 unsigned int status, mask = 0;
1354
1355 /* fastpath out, to ease interrupt sharing */
1356 status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
1357 if (!(status & CM_INTR))
1358 return IRQ_NONE;
1359
1360 /* acknowledge interrupt */
1361 spin_lock(&cm->reg_lock);
1362 if (status & CM_CHINT0)
1363 mask |= CM_CH0_INT_EN;
1364 if (status & CM_CHINT1)
1365 mask |= CM_CH1_INT_EN;
1366 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
1367 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
1368 spin_unlock(&cm->reg_lock);
1369
1370 if (cm->rmidi && (status & CM_UARTINT))
David Howells7d12e782006-10-05 14:55:46 +01001371 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (cm->pcm) {
1374 if ((status & CM_CHINT0) && cm->channel[0].running)
1375 snd_pcm_period_elapsed(cm->channel[0].substream);
1376 if ((status & CM_CHINT1) && cm->channel[1].running)
1377 snd_pcm_period_elapsed(cm->channel[1].substream);
1378 }
1379 return IRQ_HANDLED;
1380}
1381
1382/*
1383 * h/w infos
1384 */
1385
1386/* playback on channel A */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001387static struct snd_pcm_hardware snd_cmipci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1390 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001391 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1393 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1394 .rate_min = 5512,
1395 .rate_max = 48000,
1396 .channels_min = 1,
1397 .channels_max = 2,
1398 .buffer_bytes_max = (128*1024),
1399 .period_bytes_min = 64,
1400 .period_bytes_max = (128*1024),
1401 .periods_min = 2,
1402 .periods_max = 1024,
1403 .fifo_size = 0,
1404};
1405
1406/* capture on channel B */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001407static struct snd_pcm_hardware snd_cmipci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1410 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001411 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1413 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1414 .rate_min = 5512,
1415 .rate_max = 48000,
1416 .channels_min = 1,
1417 .channels_max = 2,
1418 .buffer_bytes_max = (128*1024),
1419 .period_bytes_min = 64,
1420 .period_bytes_max = (128*1024),
1421 .periods_min = 2,
1422 .periods_max = 1024,
1423 .fifo_size = 0,
1424};
1425
1426/* playback on channel B - stereo 16bit only? */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001427static struct snd_pcm_hardware snd_cmipci_playback2 =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1430 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001431 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1433 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1434 .rate_min = 5512,
1435 .rate_max = 48000,
1436 .channels_min = 2,
1437 .channels_max = 2,
1438 .buffer_bytes_max = (128*1024),
1439 .period_bytes_min = 64,
1440 .period_bytes_max = (128*1024),
1441 .periods_min = 2,
1442 .periods_max = 1024,
1443 .fifo_size = 0,
1444};
1445
1446/* spdif playback on channel A */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001447static struct snd_pcm_hardware snd_cmipci_playback_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1450 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001451 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1453 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1454 .rate_min = 44100,
1455 .rate_max = 48000,
1456 .channels_min = 2,
1457 .channels_max = 2,
1458 .buffer_bytes_max = (128*1024),
1459 .period_bytes_min = 64,
1460 .period_bytes_max = (128*1024),
1461 .periods_min = 2,
1462 .periods_max = 1024,
1463 .fifo_size = 0,
1464};
1465
1466/* spdif playback on channel A (32bit, IEC958 subframes) */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001467static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1470 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001471 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1473 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1474 .rate_min = 44100,
1475 .rate_max = 48000,
1476 .channels_min = 2,
1477 .channels_max = 2,
1478 .buffer_bytes_max = (128*1024),
1479 .period_bytes_min = 64,
1480 .period_bytes_max = (128*1024),
1481 .periods_min = 2,
1482 .periods_max = 1024,
1483 .fifo_size = 0,
1484};
1485
1486/* spdif capture on channel B */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001487static struct snd_pcm_hardware snd_cmipci_capture_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1490 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001491 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1493 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1494 .rate_min = 44100,
1495 .rate_max = 48000,
1496 .channels_min = 2,
1497 .channels_max = 2,
1498 .buffer_bytes_max = (128*1024),
1499 .period_bytes_min = 64,
1500 .period_bytes_max = (128*1024),
1501 .periods_min = 2,
1502 .periods_max = 1024,
1503 .fifo_size = 0,
1504};
1505
1506/*
1507 * check device open/close
1508 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001509static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int ch = mode & CM_OPEN_CH_MASK;
1512
1513 /* FIXME: a file should wait until the device becomes free
1514 * when it's opened on blocking mode. however, since the current
1515 * pcm framework doesn't pass file pointer before actually opened,
1516 * we can't know whether blocking mode or not in open callback..
1517 */
Ingo Molnar62932df2006-01-16 16:34:20 +01001518 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (cm->opened[ch]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001520 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EBUSY;
1522 }
1523 cm->opened[ch] = mode;
1524 cm->channel[ch].substream = subs;
1525 if (! (mode & CM_OPEN_DAC)) {
1526 /* disable dual DAC mode */
1527 cm->channel[ch].is_dac = 0;
1528 spin_lock_irq(&cm->reg_lock);
1529 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1530 spin_unlock_irq(&cm->reg_lock);
1531 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001532 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return 0;
1534}
1535
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001536static void close_device_check(struct cmipci *cm, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 int ch = mode & CM_OPEN_CH_MASK;
1539
Ingo Molnar62932df2006-01-16 16:34:20 +01001540 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (cm->opened[ch] == mode) {
1542 if (cm->channel[ch].substream) {
1543 snd_cmipci_ch_reset(cm, ch);
1544 cm->channel[ch].running = 0;
1545 cm->channel[ch].substream = NULL;
1546 }
1547 cm->opened[ch] = 0;
1548 if (! cm->channel[ch].is_dac) {
1549 /* enable dual DAC mode again */
1550 cm->channel[ch].is_dac = 1;
1551 spin_lock_irq(&cm->reg_lock);
1552 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1553 spin_unlock_irq(&cm->reg_lock);
1554 }
1555 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001556 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/*
1560 */
1561
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001562static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001564 struct cmipci *cm = snd_pcm_substream_chip(substream);
1565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int err;
1567
1568 if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
1569 return err;
1570 runtime->hw = snd_cmipci_playback;
Clemens Ladisch8992e182007-09-03 09:54:55 +02001571 if (cm->chip_version == 68) {
1572 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1573 SNDRV_PCM_RATE_96000;
1574 runtime->hw.rate_max = 96000;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1577 cm->dig_pcm_status = cm->dig_status;
1578 return 0;
1579}
1580
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001581static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001583 struct cmipci *cm = snd_pcm_substream_chip(substream);
1584 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 int err;
1586
1587 if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
1588 return err;
1589 runtime->hw = snd_cmipci_capture;
1590 if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
1591 runtime->hw.rate_min = 41000;
1592 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1593 }
1594 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1595 return 0;
1596}
1597
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001598static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001600 struct cmipci *cm = snd_pcm_substream_chip(substream);
1601 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 int err;
1603
1604 if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
1605 return err;
1606 runtime->hw = snd_cmipci_playback2;
Ingo Molnar62932df2006-01-16 16:34:20 +01001607 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (! cm->opened[CM_CH_PLAY]) {
1609 if (cm->can_multi_ch) {
1610 runtime->hw.channels_max = cm->max_channels;
1611 if (cm->max_channels == 4)
1612 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
1613 else if (cm->max_channels == 6)
1614 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
1615 else if (cm->max_channels == 8)
1616 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001619 mutex_unlock(&cm->open_mutex);
Clemens Ladisch22a22f52007-09-17 09:37:47 +02001620 if (cm->chip_version == 68) {
1621 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1622 SNDRV_PCM_RATE_96000;
1623 runtime->hw.rate_max = 96000;
1624 }
1625 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627}
1628
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001629static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001631 struct cmipci *cm = snd_pcm_substream_chip(substream);
1632 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int err;
1634
1635 if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
1636 return err;
1637 if (cm->can_ac3_hw) {
1638 runtime->hw = snd_cmipci_playback_spdif;
Clemens Ladisch57bd68b2007-09-07 10:44:13 +02001639 if (cm->chip_version >= 37) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
Clemens Ladisch57bd68b2007-09-07 10:44:13 +02001641 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1642 }
Clemens Ladisch8992e182007-09-03 09:54:55 +02001643 if (cm->chip_version == 68) {
1644 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1645 SNDRV_PCM_RATE_96000;
1646 runtime->hw.rate_max = 96000;
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 } else {
1649 runtime->hw = snd_cmipci_playback_iec958_subframe;
1650 }
1651 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1652 cm->dig_pcm_status = cm->dig_status;
1653 return 0;
1654}
1655
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001656static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001658 struct cmipci *cm = snd_pcm_substream_chip(substream);
1659 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int err;
1661
1662 if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
1663 return err;
1664 runtime->hw = snd_cmipci_capture_spdif;
1665 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1666 return 0;
1667}
1668
1669
1670/*
1671 */
1672
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001673static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001675 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 close_device_check(cm, CM_OPEN_PLAYBACK);
1677 return 0;
1678}
1679
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001680static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001682 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 close_device_check(cm, CM_OPEN_CAPTURE);
1684 return 0;
1685}
1686
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001687static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001689 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 close_device_check(cm, CM_OPEN_PLAYBACK2);
1691 close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
1692 return 0;
1693}
1694
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001695static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001697 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
1699 return 0;
1700}
1701
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001702static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001704 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
1706 return 0;
1707}
1708
1709
1710/*
1711 */
1712
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001713static struct snd_pcm_ops snd_cmipci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .open = snd_cmipci_playback_open,
1715 .close = snd_cmipci_playback_close,
1716 .ioctl = snd_pcm_lib_ioctl,
1717 .hw_params = snd_cmipci_hw_params,
1718 .hw_free = snd_cmipci_playback_hw_free,
1719 .prepare = snd_cmipci_playback_prepare,
1720 .trigger = snd_cmipci_playback_trigger,
1721 .pointer = snd_cmipci_playback_pointer,
1722};
1723
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001724static struct snd_pcm_ops snd_cmipci_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 .open = snd_cmipci_capture_open,
1726 .close = snd_cmipci_capture_close,
1727 .ioctl = snd_pcm_lib_ioctl,
1728 .hw_params = snd_cmipci_hw_params,
1729 .hw_free = snd_cmipci_hw_free,
1730 .prepare = snd_cmipci_capture_prepare,
1731 .trigger = snd_cmipci_capture_trigger,
1732 .pointer = snd_cmipci_capture_pointer,
1733};
1734
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001735static struct snd_pcm_ops snd_cmipci_playback2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 .open = snd_cmipci_playback2_open,
1737 .close = snd_cmipci_playback2_close,
1738 .ioctl = snd_pcm_lib_ioctl,
1739 .hw_params = snd_cmipci_playback2_hw_params,
1740 .hw_free = snd_cmipci_hw_free,
1741 .prepare = snd_cmipci_capture_prepare, /* channel B */
1742 .trigger = snd_cmipci_capture_trigger, /* channel B */
1743 .pointer = snd_cmipci_capture_pointer, /* channel B */
1744};
1745
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001746static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 .open = snd_cmipci_playback_spdif_open,
1748 .close = snd_cmipci_playback_spdif_close,
1749 .ioctl = snd_pcm_lib_ioctl,
1750 .hw_params = snd_cmipci_hw_params,
1751 .hw_free = snd_cmipci_playback_hw_free,
1752 .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
1753 .trigger = snd_cmipci_playback_trigger,
1754 .pointer = snd_cmipci_playback_pointer,
1755};
1756
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001757static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 .open = snd_cmipci_capture_spdif_open,
1759 .close = snd_cmipci_capture_spdif_close,
1760 .ioctl = snd_pcm_lib_ioctl,
1761 .hw_params = snd_cmipci_hw_params,
1762 .hw_free = snd_cmipci_capture_spdif_hw_free,
1763 .prepare = snd_cmipci_capture_spdif_prepare,
1764 .trigger = snd_cmipci_capture_trigger,
1765 .pointer = snd_cmipci_capture_pointer,
1766};
1767
1768
1769/*
1770 */
1771
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001772static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001774 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int err;
1776
1777 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1778 if (err < 0)
1779 return err;
1780
1781 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
1782 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
1783
1784 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 pcm->info_flags = 0;
1786 strcpy(pcm->name, "C-Media PCI DAC/ADC");
1787 cm->pcm = pcm;
1788
1789 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1790 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1791
1792 return 0;
1793}
1794
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001795static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001797 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int err;
1799
1800 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1801 if (err < 0)
1802 return err;
1803
1804 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
1805
1806 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 pcm->info_flags = 0;
1808 strcpy(pcm->name, "C-Media PCI 2nd DAC");
1809 cm->pcm2 = pcm;
1810
1811 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1812 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1813
1814 return 0;
1815}
1816
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001817static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001819 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int err;
1821
1822 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1823 if (err < 0)
1824 return err;
1825
1826 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
1827 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
1828
1829 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 pcm->info_flags = 0;
1831 strcpy(pcm->name, "C-Media PCI IEC958");
1832 cm->pcm_spdif = pcm;
1833
1834 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1835 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1836
1837 return 0;
1838}
1839
1840/*
1841 * mixer interface:
1842 * - CM8338/8738 has a compatible mixer interface with SB16, but
1843 * lack of some elements like tone control, i/o gain and AGC.
1844 * - Access to native registers:
1845 * - A 3D switch
1846 * - Output mute switches
1847 */
1848
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001849static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1852 outb(data, s->iobase + CM_REG_SB16_DATA);
1853}
1854
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001855static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 unsigned char v;
1858
1859 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1860 v = inb(s->iobase + CM_REG_SB16_DATA);
1861 return v;
1862}
1863
1864/*
1865 * general mixer element
1866 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001867struct cmipci_sb_reg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 unsigned int left_reg, right_reg;
1869 unsigned int left_shift, right_shift;
1870 unsigned int mask;
1871 unsigned int invert: 1;
1872 unsigned int stereo: 1;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001873};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875#define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1876 ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1877
1878#define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1879{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1880 .info = snd_cmipci_info_volume, \
1881 .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
1882 .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1883}
1884
1885#define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
1886#define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
1887#define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
1888#define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
1889
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001890static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 r->left_reg = val & 0xff;
1893 r->right_reg = (val >> 8) & 0xff;
1894 r->left_shift = (val >> 16) & 0x07;
1895 r->right_shift = (val >> 19) & 0x07;
1896 r->invert = (val >> 22) & 1;
1897 r->stereo = (val >> 23) & 1;
1898 r->mask = (val >> 24) & 0xff;
1899}
1900
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001901static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
1902 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001904 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1907 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1908 uinfo->count = reg.stereo + 1;
1909 uinfo->value.integer.min = 0;
1910 uinfo->value.integer.max = reg.mask;
1911 return 0;
1912}
1913
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001914static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001917 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1918 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int val;
1920
1921 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1922 spin_lock_irq(&cm->reg_lock);
1923 val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
1924 if (reg.invert)
1925 val = reg.mask - val;
1926 ucontrol->value.integer.value[0] = val;
1927 if (reg.stereo) {
1928 val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
1929 if (reg.invert)
1930 val = reg.mask - val;
1931 ucontrol->value.integer.value[1] = val;
1932 }
1933 spin_unlock_irq(&cm->reg_lock);
1934 return 0;
1935}
1936
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001937static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
1938 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001940 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
1941 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int change;
1943 int left, right, oleft, oright;
1944
1945 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
1946 left = ucontrol->value.integer.value[0] & reg.mask;
1947 if (reg.invert)
1948 left = reg.mask - left;
1949 left <<= reg.left_shift;
1950 if (reg.stereo) {
1951 right = ucontrol->value.integer.value[1] & reg.mask;
1952 if (reg.invert)
1953 right = reg.mask - right;
1954 right <<= reg.right_shift;
1955 } else
1956 right = 0;
1957 spin_lock_irq(&cm->reg_lock);
1958 oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
1959 left |= oleft & ~(reg.mask << reg.left_shift);
1960 change = left != oleft;
1961 if (reg.stereo) {
1962 if (reg.left_reg != reg.right_reg) {
1963 snd_cmipci_mixer_write(cm, reg.left_reg, left);
1964 oright = snd_cmipci_mixer_read(cm, reg.right_reg);
1965 } else
1966 oright = left;
1967 right |= oright & ~(reg.mask << reg.right_shift);
1968 change |= right != oright;
1969 snd_cmipci_mixer_write(cm, reg.right_reg, right);
1970 } else
1971 snd_cmipci_mixer_write(cm, reg.left_reg, left);
1972 spin_unlock_irq(&cm->reg_lock);
1973 return change;
1974}
1975
1976/*
1977 * input route (left,right) -> (left,right)
1978 */
1979#define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
1980{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1981 .info = snd_cmipci_info_input_sw, \
1982 .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
1983 .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
1984}
1985
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001986static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1990 uinfo->count = 4;
1991 uinfo->value.integer.min = 0;
1992 uinfo->value.integer.max = 1;
1993 return 0;
1994}
1995
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001996static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
1997 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001999 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2000 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int val1, val2;
2002
2003 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2004 spin_lock_irq(&cm->reg_lock);
2005 val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2006 val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2007 spin_unlock_irq(&cm->reg_lock);
2008 ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2009 ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2010 ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2011 ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2012 return 0;
2013}
2014
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002015static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002018 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2019 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int change;
2021 int val1, val2, oval1, oval2;
2022
2023 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2024 spin_lock_irq(&cm->reg_lock);
2025 oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2026 oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2027 val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2028 val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2029 val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2030 val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2031 val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2032 val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2033 change = val1 != oval1 || val2 != oval2;
2034 snd_cmipci_mixer_write(cm, reg.left_reg, val1);
2035 snd_cmipci_mixer_write(cm, reg.right_reg, val2);
2036 spin_unlock_irq(&cm->reg_lock);
2037 return change;
2038}
2039
2040/*
2041 * native mixer switches/volumes
2042 */
2043
2044#define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
2045{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2046 .info = snd_cmipci_info_native_mixer, \
2047 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2048 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
2049}
2050
2051#define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
2052{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2053 .info = snd_cmipci_info_native_mixer, \
2054 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2055 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
2056}
2057
2058#define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
2059{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2060 .info = snd_cmipci_info_native_mixer, \
2061 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2062 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
2063}
2064
2065#define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
2066{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2067 .info = snd_cmipci_info_native_mixer, \
2068 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2069 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
2070}
2071
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002072static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
2073 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002075 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2078 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2079 uinfo->count = reg.stereo + 1;
2080 uinfo->value.integer.min = 0;
2081 uinfo->value.integer.max = reg.mask;
2082 return 0;
2083
2084}
2085
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002086static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002089 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2090 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 unsigned char oreg, val;
2092
2093 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2094 spin_lock_irq(&cm->reg_lock);
2095 oreg = inb(cm->iobase + reg.left_reg);
2096 val = (oreg >> reg.left_shift) & reg.mask;
2097 if (reg.invert)
2098 val = reg.mask - val;
2099 ucontrol->value.integer.value[0] = val;
2100 if (reg.stereo) {
2101 val = (oreg >> reg.right_shift) & reg.mask;
2102 if (reg.invert)
2103 val = reg.mask - val;
2104 ucontrol->value.integer.value[1] = val;
2105 }
2106 spin_unlock_irq(&cm->reg_lock);
2107 return 0;
2108}
2109
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002110static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002113 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2114 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 unsigned char oreg, nreg, val;
2116
2117 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2118 spin_lock_irq(&cm->reg_lock);
2119 oreg = inb(cm->iobase + reg.left_reg);
2120 val = ucontrol->value.integer.value[0] & reg.mask;
2121 if (reg.invert)
2122 val = reg.mask - val;
2123 nreg = oreg & ~(reg.mask << reg.left_shift);
2124 nreg |= (val << reg.left_shift);
2125 if (reg.stereo) {
2126 val = ucontrol->value.integer.value[1] & reg.mask;
2127 if (reg.invert)
2128 val = reg.mask - val;
2129 nreg &= ~(reg.mask << reg.right_shift);
2130 nreg |= (val << reg.right_shift);
2131 }
2132 outb(nreg, cm->iobase + reg.left_reg);
2133 spin_unlock_irq(&cm->reg_lock);
2134 return (nreg != oreg);
2135}
2136
2137/*
2138 * special case - check mixer sensitivity
2139 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002140static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002143 //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
2145}
2146
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002147static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002150 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (cm->mixer_insensitive) {
2152 /* ignored */
2153 return 0;
2154 }
2155 return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
2156}
2157
2158
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002159static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
2161 CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2162 CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
2163 //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
2164 { /* switch with sensitivity */
2165 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2166 .name = "PCM Playback Switch",
2167 .info = snd_cmipci_info_native_mixer,
2168 .get = snd_cmipci_get_native_mixer_sensitive,
2169 .put = snd_cmipci_put_native_mixer_sensitive,
2170 .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
2171 },
2172 CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
2173 CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
2174 CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
2175 CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
2176 CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
2177 CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
2178 CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
2179 CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
2180 CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
2181 CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
2182 CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
2183 CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
2184 CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
2185 CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
2186 CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
2187 CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
2188 CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
Takashi Iwai2eff7ec2005-06-30 13:45:20 +02002189 CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
Takashi Iwai2eff7ec2005-06-30 13:45:20 +02002191 CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
2192 CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
Takashi Iwaif26eb782006-05-29 19:05:28 +02002193 CMIPCI_DOUBLE("PC Speaker Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
Takashi Iwai2eff7ec2005-06-30 13:45:20 +02002194 CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195};
2196
2197/*
2198 * other switches
2199 */
2200
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002201struct cmipci_switch_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int reg; /* register index */
2203 unsigned int mask; /* mask bits */
2204 unsigned int mask_on; /* mask bits to turn on */
2205 unsigned int is_byte: 1; /* byte access? */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002206 unsigned int ac3_sensitive: 1; /* access forbidden during
2207 * non-audio operation?
2208 */
2209};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002211#define snd_cmipci_uswitch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002213static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol,
2215 struct cmipci_switch_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 unsigned int val;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002218 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 spin_lock_irq(&cm->reg_lock);
2221 if (args->ac3_sensitive && cm->mixer_insensitive) {
2222 ucontrol->value.integer.value[0] = 0;
2223 spin_unlock_irq(&cm->reg_lock);
2224 return 0;
2225 }
2226 if (args->is_byte)
2227 val = inb(cm->iobase + args->reg);
2228 else
2229 val = snd_cmipci_read(cm, args->reg);
2230 ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2231 spin_unlock_irq(&cm->reg_lock);
2232 return 0;
2233}
2234
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002235static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2236 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002238 struct cmipci_switch_args *args;
2239 args = (struct cmipci_switch_args *)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 snd_assert(args != NULL, return -EINVAL);
2241 return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
2242}
2243
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002244static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol,
2246 struct cmipci_switch_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 unsigned int val;
2249 int change;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002250 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 spin_lock_irq(&cm->reg_lock);
2253 if (args->ac3_sensitive && cm->mixer_insensitive) {
2254 /* ignored */
2255 spin_unlock_irq(&cm->reg_lock);
2256 return 0;
2257 }
2258 if (args->is_byte)
2259 val = inb(cm->iobase + args->reg);
2260 else
2261 val = snd_cmipci_read(cm, args->reg);
Timofei V. Bondarenko8c670712006-12-20 19:20:07 +01002262 change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
2263 args->mask_on : (args->mask & ~args->mask_on));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (change) {
2265 val &= ~args->mask;
2266 if (ucontrol->value.integer.value[0])
2267 val |= args->mask_on;
2268 else
2269 val |= (args->mask & ~args->mask_on);
2270 if (args->is_byte)
2271 outb((unsigned char)val, cm->iobase + args->reg);
2272 else
2273 snd_cmipci_write(cm, args->reg, val);
2274 }
2275 spin_unlock_irq(&cm->reg_lock);
2276 return change;
2277}
2278
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002279static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2280 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002282 struct cmipci_switch_args *args;
2283 args = (struct cmipci_switch_args *)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 snd_assert(args != NULL, return -EINVAL);
2285 return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
2286}
2287
2288#define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002289static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .reg = xreg, \
2291 .mask = xmask, \
2292 .mask_on = xmask_on, \
2293 .is_byte = xis_byte, \
2294 .ac3_sensitive = xac3, \
2295}
2296
2297#define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
2298 DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
2299
2300#if 0 /* these will be controlled in pcm device */
2301DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
2302DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
2303#endif
2304DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
2305DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
2306DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
2307DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
2308DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
2309DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
2310DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
2311DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
2312// DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
2313DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
2314DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
2315/* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
2316DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
2317DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
2318#if CM_CH_PLAY == 1
2319DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
2320#else
2321DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
2322#endif
2323DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
Clemens Ladischa839a332007-09-17 09:35:46 +02002324// DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
2325// DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326// DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
2327DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
2328
2329#define DEFINE_SWITCH(sname, stype, sarg) \
2330{ .name = sname, \
2331 .iface = stype, \
2332 .info = snd_cmipci_uswitch_info, \
2333 .get = snd_cmipci_uswitch_get, \
2334 .put = snd_cmipci_uswitch_put, \
2335 .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
2336}
2337
2338#define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
2339#define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
2340
2341
2342/*
2343 * callbacks for spdif output switch
2344 * needs toggle two registers..
2345 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002346static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 int changed;
2350 changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2351 changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2352 return changed;
2353}
2354
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002355static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
2356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002358 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int changed;
2360 changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2361 changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2362 if (changed) {
2363 if (ucontrol->value.integer.value[0]) {
2364 if (chip->spdif_playback_avail)
2365 snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2366 } else {
2367 if (chip->spdif_playback_avail)
2368 snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2369 }
2370 }
2371 chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2372 return changed;
2373}
2374
2375
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002376static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_info *uinfo)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002378{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002379 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002380 static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
2381 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2382 uinfo->count = 1;
2383 uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
2384 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2385 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2386 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2387 return 0;
2388}
2389
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002390static inline unsigned int get_line_in_mode(struct cmipci *cm)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002391{
2392 unsigned int val;
2393 if (cm->chip_version >= 39) {
2394 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
Clemens Ladischa839a332007-09-17 09:35:46 +02002395 if (val & (CM_CENTR2LIN | CM_BASE2LIN))
Takashi Iwai01d25d42005-04-11 16:58:24 +02002396 return 2;
2397 }
2398 val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
Clemens Ladischa839a332007-09-17 09:35:46 +02002399 if (val & CM_REAR2LIN)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002400 return 1;
2401 return 0;
2402}
2403
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002404static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002406{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002407 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002408
2409 spin_lock_irq(&cm->reg_lock);
2410 ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2411 spin_unlock_irq(&cm->reg_lock);
2412 return 0;
2413}
2414
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002415static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002417{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002418 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002419 int change;
2420
2421 spin_lock_irq(&cm->reg_lock);
2422 if (ucontrol->value.enumerated.item[0] == 2)
Clemens Ladischa839a332007-09-17 09:35:46 +02002423 change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002424 else
Clemens Ladischa839a332007-09-17 09:35:46 +02002425 change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002426 if (ucontrol->value.enumerated.item[0] == 1)
Clemens Ladischa839a332007-09-17 09:35:46 +02002427 change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002428 else
Clemens Ladischa839a332007-09-17 09:35:46 +02002429 change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002430 spin_unlock_irq(&cm->reg_lock);
2431 return change;
2432}
2433
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002434static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_info *uinfo)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002436{
2437 static char *texts[2] = { "Mic-In", "Center/LFE Output" };
2438 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2439 uinfo->count = 1;
2440 uinfo->value.enumerated.items = 2;
2441 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2442 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2443 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2444 return 0;
2445}
2446
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002447static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002449{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002450 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002451 /* same bit as spdi_phase */
2452 spin_lock_irq(&cm->reg_lock);
2453 ucontrol->value.enumerated.item[0] =
2454 (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
2455 spin_unlock_irq(&cm->reg_lock);
2456 return 0;
2457}
2458
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002459static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002461{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002462 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002463 int change;
2464
2465 spin_lock_irq(&cm->reg_lock);
2466 if (ucontrol->value.enumerated.item[0])
2467 change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2468 else
2469 change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2470 spin_unlock_irq(&cm->reg_lock);
2471 return change;
2472}
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474/* both for CM8338/8738 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002475static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
Takashi Iwai01d25d42005-04-11 16:58:24 +02002477 {
2478 .name = "Line-In Mode",
2479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2480 .info = snd_cmipci_line_in_mode_info,
2481 .get = snd_cmipci_line_in_mode_get,
2482 .put = snd_cmipci_line_in_mode_put,
2483 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484};
2485
2486/* for non-multichannel chips */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002487static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
2489
2490/* only for CM8738 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002491static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#if 0 /* controlled in pcm device */
2493 DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
2494 DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
2495 DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
2496#endif
2497 // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
2498 { .name = "IEC958 Output Switch",
2499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2500 .info = snd_cmipci_uswitch_info,
2501 .get = snd_cmipci_spdout_enable_get,
2502 .put = snd_cmipci_spdout_enable_put,
2503 },
2504 DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
2505 DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
2506 DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
2507// DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
2508 DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
2509 DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
2510};
2511
2512/* only for model 033/037 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002513static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
2515 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
2516 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
2517};
2518
2519/* only for model 039 or later */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002520static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
2522 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
Takashi Iwai01d25d42005-04-11 16:58:24 +02002523 {
2524 .name = "Mic-In Mode",
2525 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2526 .info = snd_cmipci_mic_in_mode_info,
2527 .get = snd_cmipci_mic_in_mode_get,
2528 .put = snd_cmipci_mic_in_mode_put,
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530};
2531
2532/* card control switches */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002533static struct snd_kcontrol_new snd_cmipci_control_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 // DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
2535 DEFINE_CARD_SWITCH("Modem", modem),
2536};
2537
2538
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002539static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002541 struct snd_card *card;
2542 struct snd_kcontrol_new *sw;
2543 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 unsigned int idx;
2545 int err;
2546
2547 snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
2548
2549 card = cm->card;
2550
2551 strcpy(card->mixername, "CMedia PCI");
2552
2553 spin_lock_irq(&cm->reg_lock);
2554 snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
2555 spin_unlock_irq(&cm->reg_lock);
2556
2557 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
2558 if (cm->chip_version == 68) { // 8768 has no PCM volume
2559 if (!strcmp(snd_cmipci_mixers[idx].name,
2560 "PCM Playback Volume"))
2561 continue;
2562 }
2563 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
2564 return err;
2565 }
2566
2567 /* mixer switches */
2568 sw = snd_cmipci_mixer_switches;
2569 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
2570 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2571 if (err < 0)
2572 return err;
2573 }
2574 if (! cm->can_multi_ch) {
2575 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2576 if (err < 0)
2577 return err;
2578 }
2579 if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2580 cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2581 sw = snd_cmipci_8738_mixer_switches;
2582 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
2583 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2584 if (err < 0)
2585 return err;
2586 }
2587 if (cm->can_ac3_hw) {
2588 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
2589 return err;
2590 kctl->id.device = pcm_spdif_device;
2591 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
2592 return err;
2593 kctl->id.device = pcm_spdif_device;
2594 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
2595 return err;
2596 kctl->id.device = pcm_spdif_device;
2597 }
2598 if (cm->chip_version <= 37) {
2599 sw = snd_cmipci_old_mixer_switches;
2600 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
2601 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2602 if (err < 0)
2603 return err;
2604 }
2605 }
2606 }
2607 if (cm->chip_version >= 39) {
2608 sw = snd_cmipci_extra_mixer_switches;
2609 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
2610 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2611 if (err < 0)
2612 return err;
2613 }
2614 }
2615
2616 /* card switches */
2617 sw = snd_cmipci_control_switches;
2618 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
2619 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2620 if (err < 0)
2621 return err;
2622 }
2623
2624 for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002625 struct snd_ctl_elem_id id;
2626 struct snd_kcontrol *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 memset(&id, 0, sizeof(id));
2628 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2629 strcpy(id.name, cm_saved_mixer[idx].name);
2630 if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
2631 cm->mixer_res_ctl[idx] = ctl;
2632 }
2633
2634 return 0;
2635}
2636
2637
2638/*
2639 * proc interface
2640 */
2641
2642#ifdef CONFIG_PROC_FS
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002643static void snd_cmipci_proc_read(struct snd_info_entry *entry,
2644 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002646 struct cmipci *cm = entry->private_data;
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002647 int i, v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002649 snd_iprintf(buffer, "%s\n", cm->card->longname);
2650 for (i = 0; i < 0x94; i++) {
2651 if (i == 0x28)
2652 i = 0x90;
2653 v = inb(cm->iobase + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (i % 4 == 0)
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002655 snd_iprintf(buffer, "\n%02x:", i);
2656 snd_iprintf(buffer, " %02x", v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002658 snd_iprintf(buffer, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002661static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002663 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 if (! snd_card_proc_new(cm->card, "cmipci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002666 snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668#else /* !CONFIG_PROC_FS */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002669static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670#endif
2671
2672
Takashi Iwaif40b6892006-07-05 16:51:05 +02002673static struct pci_device_id snd_cmipci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2675 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2676 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2677 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2678 {PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2679 {0,},
2680};
2681
2682
2683/*
2684 * check chip version and capabilities
2685 * driver name is modified according to the chip model
2686 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002687static void __devinit query_chip(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 unsigned int detect;
2690
2691 /* check reg 0Ch, bit 24-31 */
2692 detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
2693 if (! detect) {
2694 /* check reg 08h, bit 24-28 */
2695 detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002696 switch (detect) {
2697 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 cm->chip_version = 33;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (cm->do_soft_ac3)
2700 cm->can_ac3_sw = 1;
2701 else
2702 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002703 break;
Clemens Ladisch6935e682007-09-17 09:34:59 +02002704 case CM_CHIP_037:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 cm->chip_version = 37;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002707 break;
2708 default:
2709 cm->chip_version = 39;
2710 cm->can_ac3_hw = 1;
2711 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Clemens Ladisch133271f2007-08-27 09:20:31 +02002713 cm->max_channels = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 } else {
Clemens Ladisch133271f2007-08-27 09:20:31 +02002715 if (detect & CM_CHIP_039) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 cm->chip_version = 39;
2717 if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
2718 cm->max_channels = 6;
2719 else
2720 cm->max_channels = 4;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002721 } else if (detect & CM_CHIP_8768) {
2722 cm->chip_version = 68;
2723 cm->max_channels = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 } else {
Clemens Ladisch133271f2007-08-27 09:20:31 +02002725 cm->chip_version = 55;
2726 cm->max_channels = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
Clemens Ladisch133271f2007-08-27 09:20:31 +02002728 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002729 cm->can_multi_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731}
2732
2733#ifdef SUPPORT_JOYSTICK
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002734static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
2737 struct gameport *gp;
2738 struct resource *r = NULL;
2739 int i, io_port = 0;
2740
2741 if (joystick_port[dev] == 0)
2742 return -ENODEV;
2743
2744 if (joystick_port[dev] == 1) { /* auto-detect */
2745 for (i = 0; ports[i]; i++) {
2746 io_port = ports[i];
2747 r = request_region(io_port, 1, "CMIPCI gameport");
2748 if (r)
2749 break;
2750 }
2751 } else {
2752 io_port = joystick_port[dev];
2753 r = request_region(io_port, 1, "CMIPCI gameport");
2754 }
2755
2756 if (!r) {
2757 printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
2758 return -EBUSY;
2759 }
2760
2761 cm->gameport = gp = gameport_allocate_port();
2762 if (!gp) {
2763 printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02002764 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return -ENOMEM;
2766 }
2767 gameport_set_name(gp, "C-Media Gameport");
2768 gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2769 gameport_set_dev_parent(gp, &cm->pci->dev);
2770 gp->io = io_port;
2771 gameport_set_port_data(gp, r);
2772
2773 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2774
2775 gameport_register_port(cm->gameport);
2776
2777 return 0;
2778}
2779
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002780static void snd_cmipci_free_gameport(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 if (cm->gameport) {
2783 struct resource *r = gameport_get_port_data(cm->gameport);
2784
2785 gameport_unregister_port(cm->gameport);
2786 cm->gameport = NULL;
2787
2788 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002789 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791}
2792#else
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002793static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2794static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795#endif
2796
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002797static int snd_cmipci_free(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
2799 if (cm->irq >= 0) {
2800 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2801 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
2802 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
2803 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2804 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2805 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
2806 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2807
2808 /* reset mixer */
2809 snd_cmipci_mixer_write(cm, 0, 0);
2810
2811 synchronize_irq(cm->irq);
2812
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002813 free_irq(cm->irq, cm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815
2816 snd_cmipci_free_gameport(cm);
2817 pci_release_regions(cm->pci);
2818 pci_disable_device(cm->pci);
2819 kfree(cm);
2820 return 0;
2821}
2822
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002823static int snd_cmipci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002825 struct cmipci *cm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return snd_cmipci_free(cm);
2827}
2828
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002829static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
Clemens Ladisch5747e542005-09-14 08:33:46 +02002830{
2831 long iosynth;
2832 unsigned int val;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002833 struct snd_opl3 *opl3;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002834 int err;
2835
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002836 if (!fm_port)
2837 goto disable_fm;
2838
Clemens Ladischc78c950d2007-09-03 09:55:49 +02002839 if (cm->chip_version >= 39) {
Clemens Ladisch45c41b42007-08-22 09:45:03 +02002840 /* first try FM regs in PCI port range */
2841 iosynth = cm->iobase + CM_REG_FM_PCI;
2842 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2843 OPL3_HW_OPL3, 1, &opl3);
2844 } else {
2845 err = -EIO;
2846 }
Clemens Ladisch5747e542005-09-14 08:33:46 +02002847 if (err < 0) {
2848 /* then try legacy ports */
2849 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
2850 iosynth = fm_port;
2851 switch (iosynth) {
2852 case 0x3E8: val |= CM_FMSEL_3E8; break;
2853 case 0x3E0: val |= CM_FMSEL_3E0; break;
2854 case 0x3C8: val |= CM_FMSEL_3C8; break;
2855 case 0x388: val |= CM_FMSEL_388; break;
2856 default:
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002857 goto disable_fm;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002858 }
2859 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
2860 /* enable FM */
2861 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2862
2863 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2864 OPL3_HW_OPL3, 0, &opl3) < 0) {
2865 printk(KERN_ERR "cmipci: no OPL device at %#lx, "
2866 "skipping...\n", iosynth);
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002867 goto disable_fm;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002868 }
2869 }
2870 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
2871 printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
2872 return err;
2873 }
2874 return 0;
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002875
2876 disable_fm:
2877 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
2878 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2879 return 0;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002880}
2881
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002882static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
2883 int dev, struct cmipci **rcmipci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002885 struct cmipci *cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int err;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002887 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 .dev_free = snd_cmipci_dev_free,
2889 };
Clemens Ladischd6426252007-08-27 09:22:31 +02002890 unsigned int val;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002891 long iomidi;
Clemens Ladischc9116ae2007-08-24 09:18:04 +02002892 int integrated_midi = 0;
Clemens Ladischb7e054a2007-09-03 09:56:45 +02002893 char modelstr[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 int pcm_index, pcm_spdif_index;
2895 static struct pci_device_id intel_82437vx[] = {
2896 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
2897 { },
2898 };
2899
2900 *rcmipci = NULL;
2901
2902 if ((err = pci_enable_device(pci)) < 0)
2903 return err;
2904
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002905 cm = kzalloc(sizeof(*cm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (cm == NULL) {
2907 pci_disable_device(pci);
2908 return -ENOMEM;
2909 }
2910
2911 spin_lock_init(&cm->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002912 mutex_init(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 cm->device = pci->device;
2914 cm->card = card;
2915 cm->pci = pci;
2916 cm->irq = -1;
2917 cm->channel[0].ch = 0;
2918 cm->channel[1].ch = 1;
2919 cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
2920
2921 if ((err = pci_request_regions(pci, card->driver)) < 0) {
2922 kfree(cm);
2923 pci_disable_device(pci);
2924 return err;
2925 }
2926 cm->iobase = pci_resource_start(pci, 0);
2927
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002928 if (request_irq(pci->irq, snd_cmipci_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002929 IRQF_SHARED, card->driver, cm)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002930 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 snd_cmipci_free(cm);
2932 return -EBUSY;
2933 }
2934 cm->irq = pci->irq;
2935
2936 pci_set_master(cm->pci);
2937
2938 /*
2939 * check chip version, max channels and capabilities
2940 */
2941
2942 cm->chip_version = 0;
2943 cm->max_channels = 2;
2944 cm->do_soft_ac3 = soft_ac3[dev];
2945
2946 if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
2947 pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
2948 query_chip(cm);
2949 /* added -MCx suffix for chip supporting multi-channels */
2950 if (cm->can_multi_ch)
2951 sprintf(cm->card->driver + strlen(cm->card->driver),
2952 "-MC%d", cm->max_channels);
2953 else if (cm->can_ac3_sw)
2954 strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
2955
2956 cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2957 cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
2958
2959#if CM_CH_PLAY == 1
2960 cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
2961#else
2962 cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
2963#endif
2964
2965 /* initialize codec registers */
Clemens Ladisch3042ef72007-09-17 09:36:49 +02002966 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
2967 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
2969 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2970 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2971 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
2972 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2973
2974 snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
2975 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
2976#if CM_CH_PLAY == 1
2977 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
2978#else
2979 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
2980#endif
Clemens Ladisch4ee72712007-09-17 09:37:19 +02002981 if (cm->chip_version) {
2982 snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
2983 snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 /* Set Bus Master Request */
2986 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
2987
2988 /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
2989 switch (pci->device) {
2990 case PCI_DEVICE_ID_CMEDIA_CM8738:
2991 case PCI_DEVICE_ID_CMEDIA_CM8738B:
2992 if (!pci_dev_present(intel_82437vx))
2993 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
2994 break;
2995 default:
2996 break;
2997 }
2998
Clemens Ladischd6426252007-08-27 09:22:31 +02002999 if (cm->chip_version < 68) {
3000 val = pci->device < 0x110 ? 8338 : 8738;
Clemens Ladischd6426252007-08-27 09:22:31 +02003001 } else {
3002 switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
3003 case 0:
3004 val = 8769;
3005 break;
3006 case 2:
3007 val = 8762;
3008 break;
3009 default:
3010 switch ((pci->subsystem_vendor << 16) |
3011 pci->subsystem_device) {
3012 case 0x13f69761:
3013 case 0x584d3741:
3014 case 0x584d3751:
3015 case 0x584d3761:
3016 case 0x584d3771:
3017 case 0x72848384:
3018 val = 8770;
3019 break;
3020 default:
3021 val = 8768;
3022 break;
3023 }
3024 }
Clemens Ladischd6426252007-08-27 09:22:31 +02003025 }
Clemens Ladischb7e054a2007-09-03 09:56:45 +02003026 sprintf(card->shortname, "C-Media CMI%d", val);
3027 if (cm->chip_version < 68)
3028 sprintf(modelstr, " (model %d)", cm->chip_version);
3029 else
3030 modelstr[0] = '\0';
3031 sprintf(card->longname, "%s%s at %#lx, irq %i",
3032 card->shortname, modelstr, cm->iobase, cm->irq);
Clemens Ladisch1e02d6e2007-08-21 08:58:35 +02003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
3035 snd_cmipci_free(cm);
3036 return err;
3037 }
3038
Clemens Ladischc78c950d2007-09-03 09:55:49 +02003039 if (cm->chip_version >= 39) {
Clemens Ladischc9116ae2007-08-24 09:18:04 +02003040 val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
3041 if (val != 0x00 && val != 0xff) {
3042 iomidi = cm->iobase + CM_REG_MPU_PCI;
3043 integrated_midi = 1;
3044 }
3045 }
3046 if (!integrated_midi) {
Clemens Ladischc78c950d2007-09-03 09:55:49 +02003047 val = 0;
Clemens Ladisch5747e542005-09-14 08:33:46 +02003048 iomidi = mpu_port[dev];
3049 switch (iomidi) {
3050 case 0x320: val = CM_VMPU_320; break;
3051 case 0x310: val = CM_VMPU_310; break;
3052 case 0x300: val = CM_VMPU_300; break;
3053 case 0x330: val = CM_VMPU_330; break;
3054 default:
3055 iomidi = 0; break;
3056 }
3057 if (iomidi > 0) {
3058 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
3059 /* enable UART */
3060 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
Clemens Ladisch88039812007-09-03 09:56:23 +02003061 if (inb(iomidi + 1) == 0xff) {
3062 snd_printk(KERN_ERR "cannot enable MPU-401 port"
3063 " at %#lx\n", iomidi);
3064 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
3065 CM_UART_EN);
3066 iomidi = 0;
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 }
Clemens Ladisch5747e542005-09-14 08:33:46 +02003070
Clemens Ladisch45c41b42007-08-22 09:45:03 +02003071 if (cm->chip_version < 68) {
3072 err = snd_cmipci_create_fm(cm, fm_port[dev]);
3073 if (err < 0)
3074 return err;
3075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 /* reset mixer */
3078 snd_cmipci_mixer_write(cm, 0, 0);
3079
3080 snd_cmipci_proc_init(cm);
3081
3082 /* create pcm devices */
3083 pcm_index = pcm_spdif_index = 0;
3084 if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
3085 return err;
3086 pcm_index++;
Clemens Ladischb080ebb2007-08-27 09:21:02 +02003087 if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
3088 return err;
3089 pcm_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (cm->can_ac3_hw || cm->can_ac3_sw) {
3091 pcm_spdif_index = pcm_index;
3092 if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
3093 return err;
3094 }
3095
3096 /* create mixer interface & switches */
3097 if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
3098 return err;
3099
3100 if (iomidi > 0) {
3101 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
Takashi Iwai302e4c22006-05-23 13:24:30 +02003102 iomidi,
3103 (integrated_midi ?
3104 MPU401_INFO_INTEGRATED : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 cm->irq, 0, &cm->rmidi)) < 0) {
3106 printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
3107 }
3108 }
3109
3110#ifdef USE_VAR48KRATE
3111 for (val = 0; val < ARRAY_SIZE(rates); val++)
3112 snd_cmipci_set_pll(cm, rates[val], val);
3113
3114 /*
3115 * (Re-)Enable external switch spdo_48k
3116 */
3117 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
3118#endif /* USE_VAR48KRATE */
3119
3120 if (snd_cmipci_create_gameport(cm, dev) < 0)
3121 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
3122
3123 snd_card_set_dev(card, &pci->dev);
3124
3125 *rcmipci = cm;
3126 return 0;
3127}
3128
3129/*
3130 */
3131
3132MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
3133
3134static int __devinit snd_cmipci_probe(struct pci_dev *pci,
3135 const struct pci_device_id *pci_id)
3136{
3137 static int dev;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01003138 struct snd_card *card;
3139 struct cmipci *cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 int err;
3141
3142 if (dev >= SNDRV_CARDS)
3143 return -ENODEV;
3144 if (! enable[dev]) {
3145 dev++;
3146 return -ENOENT;
3147 }
3148
3149 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
3150 if (card == NULL)
3151 return -ENOMEM;
3152
3153 switch (pci->device) {
3154 case PCI_DEVICE_ID_CMEDIA_CM8738:
3155 case PCI_DEVICE_ID_CMEDIA_CM8738B:
3156 strcpy(card->driver, "CMI8738");
3157 break;
3158 case PCI_DEVICE_ID_CMEDIA_CM8338A:
3159 case PCI_DEVICE_ID_CMEDIA_CM8338B:
3160 strcpy(card->driver, "CMI8338");
3161 break;
3162 default:
3163 strcpy(card->driver, "CMIPCI");
3164 break;
3165 }
3166
3167 if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
3168 snd_card_free(card);
3169 return err;
3170 }
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003171 card->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if ((err = snd_card_register(card)) < 0) {
3174 snd_card_free(card);
3175 return err;
3176 }
3177 pci_set_drvdata(pci, card);
3178 dev++;
3179 return 0;
3180
3181}
3182
3183static void __devexit snd_cmipci_remove(struct pci_dev *pci)
3184{
3185 snd_card_free(pci_get_drvdata(pci));
3186 pci_set_drvdata(pci, NULL);
3187}
3188
3189
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003190#ifdef CONFIG_PM
3191/*
3192 * power management
3193 */
3194static unsigned char saved_regs[] = {
3195 CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
3196 CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
3197 CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
3198 CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
3199 CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
3200};
3201
3202static unsigned char saved_mixers[] = {
3203 SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
3204 SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
3205 SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
3206 SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
3207 SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
3208 SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
3209 CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
3210 SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
3211};
3212
3213static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
3214{
3215 struct snd_card *card = pci_get_drvdata(pci);
3216 struct cmipci *cm = card->private_data;
3217 int i;
3218
3219 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3220
3221 snd_pcm_suspend_all(cm->pcm);
3222 snd_pcm_suspend_all(cm->pcm2);
3223 snd_pcm_suspend_all(cm->pcm_spdif);
3224
3225 /* save registers */
3226 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3227 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3228 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3229 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3230
3231 /* disable ints */
3232 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3233
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003234 pci_disable_device(pci);
3235 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02003236 pci_set_power_state(pci, pci_choose_state(pci, state));
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003237 return 0;
3238}
3239
3240static int snd_cmipci_resume(struct pci_dev *pci)
3241{
3242 struct snd_card *card = pci_get_drvdata(pci);
3243 struct cmipci *cm = card->private_data;
3244 int i;
3245
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003246 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02003247 pci_restore_state(pci);
3248 if (pci_enable_device(pci) < 0) {
3249 printk(KERN_ERR "cmipci: pci_enable_device failed, "
3250 "disabling device\n");
3251 snd_card_disconnect(card);
3252 return -EIO;
3253 }
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003254 pci_set_master(pci);
3255
3256 /* reset / initialize to a sane state */
3257 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3258 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3259 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3260 snd_cmipci_mixer_write(cm, 0, 0);
3261
3262 /* restore registers */
3263 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3264 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3265 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3266 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
3267
3268 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3269 return 0;
3270}
3271#endif /* CONFIG_PM */
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273static struct pci_driver driver = {
3274 .name = "C-Media PCI",
3275 .id_table = snd_cmipci_ids,
3276 .probe = snd_cmipci_probe,
3277 .remove = __devexit_p(snd_cmipci_remove),
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003278#ifdef CONFIG_PM
3279 .suspend = snd_cmipci_suspend,
3280 .resume = snd_cmipci_resume,
3281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282};
3283
3284static int __init alsa_card_cmipci_init(void)
3285{
Takashi Iwai01d25d42005-04-11 16:58:24 +02003286 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
3289static void __exit alsa_card_cmipci_exit(void)
3290{
3291 pci_unregister_driver(&driver);
3292}
3293
3294module_init(alsa_card_cmipci_init)
3295module_exit(alsa_card_cmipci_exit)