blob: 3a2d942f22bb777c00dfffdb7771c40d19bfa98c [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
Timofei Bondarenko755c48a2007-10-31 17:36:20 +0100153#define CM_CH0_SRATE_128K 0x00000300
154#define CM_CH0_SRATE_MASK 0x00000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
Clemens Ladischa839a332007-09-17 09:35:46 +0200157#define CM_DBLSPDS 0x00000040 /* double SPDIF sample rate 88.2/96 */
158#define CM_POLVALID 0x00000020 /* inverse SPDIF/IN valid bit */
159#define CM_SPDLOCKED 0x00000010
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Clemens Ladischa839a332007-09-17 09:35:46 +0200161#define CM_CH1FMT_MASK 0x0000000C /* bit 3: 16 bits, bit 2: stereo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define CM_CH1FMT_SHIFT 2
Clemens Ladischa839a332007-09-17 09:35:46 +0200163#define CM_CH0FMT_MASK 0x00000003 /* bit 1: 16 bits, bit 0: stereo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define CM_CH0FMT_SHIFT 0
165
166#define CM_REG_INT_HLDCLR 0x0C
167#define CM_CHIP_MASK2 0xff000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200168#define CM_CHIP_8768 0x20000000
169#define CM_CHIP_055 0x08000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define CM_CHIP_039 0x04000000
171#define CM_CHIP_039_6CH 0x01000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200172#define CM_UNKNOWN_INT_EN 0x00080000 /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define CM_TDMA_INT_EN 0x00040000
174#define CM_CH1_INT_EN 0x00020000
175#define CM_CH0_INT_EN 0x00010000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177#define CM_REG_INT_STATUS 0x10
178#define CM_INTR 0x80000000
179#define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
180#define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
181#define CM_UARTINT 0x00010000
182#define CM_LTDMAINT 0x00008000
183#define CM_HTDMAINT 0x00004000
184#define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
185#define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
186#define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
187#define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
188#define CM_CH1BUSY 0x00000008
189#define CM_CH0BUSY 0x00000004
190#define CM_CHINT1 0x00000002
191#define CM_CHINT0 0x00000001
192
193#define CM_REG_LEGACY_CTRL 0x14
Clemens Ladischa839a332007-09-17 09:35:46 +0200194#define CM_NXCHG 0x80000000 /* don't map base reg dword->sample */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
196#define CM_VMPU_330 0x00000000
197#define CM_VMPU_320 0x20000000
198#define CM_VMPU_310 0x40000000
199#define CM_VMPU_300 0x60000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200200#define CM_ENWR8237 0x10000000 /* enable bus master to write 8237 base reg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
202#define CM_VSBSEL_220 0x00000000
203#define CM_VSBSEL_240 0x04000000
204#define CM_VSBSEL_260 0x08000000
205#define CM_VSBSEL_280 0x0C000000
206#define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
207#define CM_FMSEL_388 0x00000000
208#define CM_FMSEL_3C8 0x01000000
209#define CM_FMSEL_3E0 0x02000000
210#define CM_FMSEL_3E8 0x03000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200211#define CM_ENSPDOUT 0x00800000 /* enable XSPDIF/OUT to I/O interface */
212#define CM_SPDCOPYRHT 0x00400000 /* spdif in/out copyright bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
Clemens Ladischa839a332007-09-17 09:35:46 +0200214#define CM_INVIDWEN 0x00100000 /* internal vendor ID write enable, model 039? */
215#define CM_SETRETRY 0x00100000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
216#define CM_C_EEACCESS 0x00080000 /* direct programming eeprom regs */
217#define CM_C_EECS 0x00040000
218#define CM_C_EEDI46 0x00020000
219#define CM_C_EECK46 0x00010000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
Clemens Ladischa839a332007-09-17 09:35:46 +0200221#define CM_CENTR2LIN 0x00004000 /* line-in as center out */
222#define CM_BASE2LIN 0x00002000 /* line-in as bass out */
223#define CM_EXBASEN 0x00001000 /* external bass input enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#define CM_REG_MISC_CTRL 0x18
Clemens Ladischa839a332007-09-17 09:35:46 +0200226#define CM_PWD 0x80000000 /* power down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define CM_RESET 0x40000000
Clemens Ladischa839a332007-09-17 09:35:46 +0200228#define CM_SFIL_MASK 0x30000000 /* filter control at front end DAC, model 037? */
229#define CM_VMGAIN 0x10000000 /* analog master amp +6dB, model 039? */
230#define CM_TXVX 0x08000000 /* model 037? */
231#define CM_N4SPK3D 0x04000000 /* copy front to rear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
233#define CM_SPDIF48K 0x01000000 /* write */
234#define CM_SPATUS48K 0x01000000 /* read */
Clemens Ladischa839a332007-09-17 09:35:46 +0200235#define CM_ENDBDAC 0x00800000 /* enable double dac */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
237#define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
Clemens Ladischa839a332007-09-17 09:35:46 +0200238#define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
239#define CM_FM_EN 0x00080000 /* enable legacy FM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200241#define CM_ENWRASID 0x00010000 /* choose writable internal SUBID (audio) */
242#define CM_VIDWPDSB 0x00010000 /* model 037? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
Clemens Ladischa839a332007-09-17 09:35:46 +0200244#define CM_MASK_EN 0x00004000 /* activate channel mask on legacy DMA */
245#define CM_ENWRMSID 0x00002000 /* choose writable internal SUBID (modem) */
246#define CM_VIDWPPRT 0x00002000 /* model 037? */
247#define CM_SFILENB 0x00001000 /* filter stepping at front end DAC, model 037? */
248#define CM_MMODE_MASK 0x00000E00 /* model DAA interface mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
250#define CM_ENCENTER 0x00000080
Clemens Ladisch56c36ca2007-10-30 08:59:14 +0100251#define CM_FLINKON 0x00000040 /* force modem link detection on, model 037 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200252#define CM_MUTECH1 0x00000040 /* mute PCI ch1 to DAC */
Clemens Ladisch56c36ca2007-10-30 08:59:14 +0100253#define CM_FLINKOFF 0x00000020 /* force modem link detection off, model 037 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200254#define CM_MIDSMP 0x00000010 /* 1/2 interpolation at front end DAC */
255#define CM_UPDDMA_MASK 0x0000000C /* TDMA position update notification */
256#define CM_UPDDMA_2048 0x00000000
257#define CM_UPDDMA_1024 0x00000004
258#define CM_UPDDMA_512 0x00000008
259#define CM_UPDDMA_256 0x0000000C
260#define CM_TWAIT_MASK 0x00000003 /* model 037 */
261#define CM_TWAIT1 0x00000002 /* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
262#define CM_TWAIT0 0x00000001 /* i/o cycle, 0: 4, 1: 6 PCICLKs */
263
264#define CM_REG_TDMA_POSITION 0x1C
265#define CM_TDMA_CNT_MASK 0xFFFF0000 /* current byte/word count */
266#define CM_TDMA_ADR_MASK 0x0000FFFF /* current address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* byte */
269#define CM_REG_MIXER0 0x20
Clemens Ladischa839a332007-09-17 09:35:46 +0200270#define CM_REG_SBVR 0x20 /* write: sb16 version */
271#define CM_REG_DEV 0x20 /* read: hardware device version */
272
273#define CM_REG_MIXER21 0x21
274#define CM_UNKNOWN_21_MASK 0x78 /* ? */
275#define CM_X_ADPCM 0x04 /* SB16 ADPCM enable */
276#define CM_PROINV 0x02 /* SBPro left/right channel switching */
277#define CM_X_SB16 0x01 /* SB16 compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279#define CM_REG_SB16_DATA 0x22
280#define CM_REG_SB16_ADDR 0x23
281
282#define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
283#define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
284#define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
285#define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
286
287#define CM_REG_MIXER1 0x24
288#define CM_FMMUTE 0x80 /* mute FM */
289#define CM_FMMUTE_SHIFT 7
290#define CM_WSMUTE 0x40 /* mute PCM */
291#define CM_WSMUTE_SHIFT 6
Clemens Ladischa839a332007-09-17 09:35:46 +0200292#define CM_REAR2LIN 0x20 /* lin-in -> rear line out */
293#define CM_REAR2LIN_SHIFT 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#define CM_REAR2FRONT 0x10 /* exchange rear/front */
295#define CM_REAR2FRONT_SHIFT 4
296#define CM_WAVEINL 0x08 /* digital wave rec. left chan */
297#define CM_WAVEINL_SHIFT 3
298#define CM_WAVEINR 0x04 /* digical wave rec. right */
299#define CM_WAVEINR_SHIFT 2
300#define CM_X3DEN 0x02 /* 3D surround enable */
301#define CM_X3DEN_SHIFT 1
302#define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
303#define CM_CDPLAY_SHIFT 0
304
305#define CM_REG_MIXER2 0x25
306#define CM_RAUXREN 0x80 /* AUX right capture */
307#define CM_RAUXREN_SHIFT 7
308#define CM_RAUXLEN 0x40 /* AUX left capture */
309#define CM_RAUXLEN_SHIFT 6
310#define CM_VAUXRM 0x20 /* AUX right mute */
311#define CM_VAUXRM_SHIFT 5
312#define CM_VAUXLM 0x10 /* AUX left mute */
313#define CM_VAUXLM_SHIFT 4
314#define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
315#define CM_VADMIC_SHIFT 1
316#define CM_MICGAINZ 0x01 /* mic boost */
317#define CM_MICGAINZ_SHIFT 0
318
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100319#define CM_REG_MIXER3 0x24
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#define CM_REG_AUX_VOL 0x26
321#define CM_VAUXL_MASK 0xf0
322#define CM_VAUXR_MASK 0x0f
323
324#define CM_REG_MISC 0x27
Clemens Ladischa839a332007-09-17 09:35:46 +0200325#define CM_UNKNOWN_27_MASK 0xd8 /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#define CM_XGPO1 0x20
327// #define CM_XGPBIO 0x04
328#define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
329#define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
330#define CM_SPDVALID 0x02 /* spdif input valid check */
Clemens Ladischa839a332007-09-17 09:35:46 +0200331#define CM_DMAUTO 0x01 /* SB16 DMA auto detect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333#define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
334/*
335 * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
336 * or identical with AC97 codec?
337 */
338#define CM_REG_EXTERN_CODEC CM_REG_AC97
339
340/*
341 * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
342 */
343#define CM_REG_MPU_PCI 0x40
344
345/*
346 * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
347 */
348#define CM_REG_FM_PCI 0x50
349
350/*
Takashi Iwai2eff7ec2005-06-30 13:45:20 +0200351 * access from SB-mixer port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353#define CM_REG_EXTENT_IND 0xf0
354#define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
355#define CM_VPHONE_SHIFT 5
356#define CM_VPHOM 0x10 /* Phone mute control */
357#define CM_VSPKM 0x08 /* Speaker mute control, default high */
358#define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
359#define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
Takashi Iwai2eff7ec2005-06-30 13:45:20 +0200360#define CM_VADMIC3 0x01 /* Mic record boost */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/*
363 * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
364 * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
365 * unit (readonly?).
366 */
367#define CM_REG_PLL 0xf8
368
369/*
370 * extended registers
371 */
Clemens Ladischa839a332007-09-17 09:35:46 +0200372#define CM_REG_CH0_FRAME1 0x80 /* write: base address */
373#define CM_REG_CH0_FRAME2 0x84 /* read: current address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
375#define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
Clemens Ladischa839a332007-09-17 09:35:46 +0200376
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100377#define CM_REG_EXT_MISC 0x90
Clemens Ladischa839a332007-09-17 09:35:46 +0200378#define CM_ADC48K44K 0x10000000 /* ADC parameters group, 0: 44k, 1: 48k */
379#define CM_CHB3D8C 0x00200000 /* 7.1 channels support */
380#define CM_SPD32FMT 0x00100000 /* SPDIF/IN 32k sample rate */
381#define CM_ADC2SPDIF 0x00080000 /* ADC output to SPDIF/OUT */
382#define CM_SHAREADC 0x00040000 /* DAC in ADC as Center/LFE */
383#define CM_REALTCMP 0x00020000 /* monitor the CMPL/CMPR of ADC */
384#define CM_INVLRCK 0x00010000 /* invert ZVPORT's LRCK */
385#define CM_UNKNOWN_90_MASK 0x0000FFFF /* ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387/*
388 * size of i/o region
389 */
390#define CM_EXTENT_CODEC 0x100
391#define CM_EXTENT_MIDI 0x2
392#define CM_EXTENT_SYNTH 0x4
393
394
395/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * channels for playback / capture
397 */
398#define CM_CH_PLAY 0
399#define CM_CH_CAPT 1
400
401/*
402 * flags to check device open/close
403 */
404#define CM_OPEN_NONE 0
405#define CM_OPEN_CH_MASK 0x01
406#define CM_OPEN_DAC 0x10
407#define CM_OPEN_ADC 0x20
408#define CM_OPEN_SPDIF 0x40
409#define CM_OPEN_MCHAN 0x80
410#define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
411#define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
412#define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
413#define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
414#define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
415#define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
416
417
418#if CM_CH_PLAY == 1
419#define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
420#define CM_PLAYBACK_SPDF CM_SPDF_1
421#define CM_CAPTURE_SPDF CM_SPDF_0
422#else
423#define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
424#define CM_PLAYBACK_SPDF CM_SPDF_0
425#define CM_CAPTURE_SPDF CM_SPDF_1
426#endif
427
428
429/*
430 * driver data
431 */
432
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100433struct cmipci_pcm {
434 struct snd_pcm_substream *substream;
Clemens Ladischebe9e282007-09-17 09:40:57 +0200435 u8 running; /* dac/adc running? */
436 u8 fmt; /* format bits */
437 u8 is_dac;
Clemens Ladischc36fd8c2007-09-17 09:41:36 +0200438 u8 needs_silencing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned int dma_size; /* in frames */
Clemens Ladischebe9e282007-09-17 09:40:57 +0200440 unsigned int shift;
441 unsigned int ch; /* channel (0/1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned int offset; /* physical address of the buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
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;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +0100478 unsigned int can_96k: 1; /* samplerate above 48k */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned int do_soft_ac3: 1;
480
481 unsigned int spdif_playback_avail: 1; /* spdif ready? */
482 unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
483 int spdif_counter; /* for software AC3 */
484
485 unsigned int dig_status;
486 unsigned int dig_pcm_status;
487
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100488 struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 int opened[2]; /* open mode */
Ingo Molnar62932df2006-01-16 16:34:20 +0100491 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 unsigned int mixer_insensitive: 1;
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100494 struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int mixer_res_status[CM_SAVED_MIXERS];
496
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100497 struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* external MIDI */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100500 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502#ifdef SUPPORT_JOYSTICK
503 struct gameport *gameport;
504#endif
505
506 spinlock_t reg_lock;
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100507
508#ifdef CONFIG_PM
509 unsigned int saved_regs[0x20];
510 unsigned char saved_mixers[0x20];
511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512};
513
514
515/* read/write operations for dword register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100516static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 outl(data, cm->iobase + cmd);
519}
Jesper Juhl77933d72005-07-27 11:46:09 -0700520
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100521static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 return inl(cm->iobase + cmd);
524}
525
526/* read/write operations for word register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100527static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 outw(data, cm->iobase + cmd);
530}
Jesper Juhl77933d72005-07-27 11:46:09 -0700531
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100532static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 return inw(cm->iobase + cmd);
535}
536
537/* read/write operations for byte register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100538static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 outb(data, cm->iobase + cmd);
541}
542
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100543static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 return inb(cm->iobase + cmd);
546}
547
548/* bit operations for dword register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100549static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200551 unsigned int val, oval;
552 val = oval = inl(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 val |= flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200554 if (val == oval)
555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 outl(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200557 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100560static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200562 unsigned int val, oval;
563 val = oval = inl(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 val &= ~flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200565 if (val == oval)
566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 outl(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200568 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/* bit operations for byte register */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100572static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200574 unsigned char val, oval;
575 val = oval = inb(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 val |= flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200577 if (val == oval)
578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 outb(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200580 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100583static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200585 unsigned char val, oval;
586 val = oval = inb(cm->iobase + cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 val &= ~flag;
Takashi Iwai01d25d42005-04-11 16:58:24 +0200588 if (val == oval)
589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 outb(val, cm->iobase + cmd);
Takashi Iwai01d25d42005-04-11 16:58:24 +0200591 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594
595/*
596 * PCM interface
597 */
598
599/*
600 * calculate frequency
601 */
602
603static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
604
605static unsigned int snd_cmipci_rate_freq(unsigned int rate)
606{
607 unsigned int i;
Clemens Ladisch0f28eca2007-09-17 09:40:24 +0200608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 for (i = 0; i < ARRAY_SIZE(rates); i++) {
610 if (rates[i] == rate)
611 return i;
612 }
613 snd_BUG();
614 return 0;
615}
616
617#ifdef USE_VAR48KRATE
618/*
619 * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
620 * does it this way .. maybe not. Never get any information from C-Media about
621 * that <werner@suse.de>.
622 */
623static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
624{
625 unsigned int delta, tolerance;
626 int xm, xn, xr;
627
628 for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
629 rate <<= 1;
630 *n = -1;
631 if (*r > 0xff)
632 goto out;
633 tolerance = rate*CM_TOLERANCE_RATE;
634
635 for (xn = (1+2); xn < (0x1f+2); xn++) {
636 for (xm = (1+2); xm < (0xff+2); xm++) {
637 xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
638
639 if (xr < rate)
640 delta = rate - xr;
641 else
642 delta = xr - rate;
643
644 /*
645 * If we found one, remember this,
646 * and try to find a closer one
647 */
648 if (delta < tolerance) {
649 tolerance = delta;
650 *m = xm - 2;
651 *n = xn - 2;
652 }
653 }
654 }
655out:
656 return (*n > -1);
657}
658
659/*
660 * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
661 * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
662 * at the register CM_REG_FUNCTRL1 (0x04).
663 * Problem: other ways are also possible (any information about that?)
664 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100665static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 unsigned int reg = CM_REG_PLL + slot;
668 /*
669 * Guess that this programs at reg. 0x04 the pos 15:13/12:10
670 * for DSFC/ASFC (000 upto 111).
671 */
672
673 /* FIXME: Init (Do we've to set an other register first before programming?) */
674
675 /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
676 snd_cmipci_write_b(cm, reg, rate>>8);
677 snd_cmipci_write_b(cm, reg, rate&0xff);
678
679 /* FIXME: Setup (Do we've to set an other register first to enable this?) */
680}
681#endif /* USE_VAR48KRATE */
682
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100683static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
684 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
687}
688
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100689static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
690 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100692 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (params_channels(hw_params) > 2) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100694 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (cm->opened[CM_CH_PLAY]) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100696 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EBUSY;
698 }
699 /* reserve the channel A */
700 cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
Ingo Molnar62932df2006-01-16 16:34:20 +0100701 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
704}
705
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100706static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 int reset = CM_RST_CH0 << (cm->channel[ch].ch);
709 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
710 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
711 udelay(10);
712}
713
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100714static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 return snd_pcm_lib_free_pages(substream);
717}
718
719
720/*
721 */
722
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200723static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100724static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 .count = 3,
726 .list = hw_channels,
727 .mask = 0,
728};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100729static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200730 .count = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .list = hw_channels,
732 .mask = 0,
733};
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100734static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
Clemens Ladisch35add1c2007-09-17 09:39:10 +0200735 .count = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 .list = hw_channels,
737 .mask = 0,
738};
739
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100740static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 if (channels > 2) {
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200743 if (!cm->can_multi_ch || !rec->ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EINVAL;
745 if (rec->fmt != 0x03) /* stereo 16bit only */
746 return -EINVAL;
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200749 if (cm->can_multi_ch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 spin_lock_irq(&cm->reg_lock);
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200751 if (channels > 2) {
752 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
753 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200756 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
757 }
758 if (channels == 8)
759 snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
760 else
761 snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
762 if (channels == 6) {
763 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
764 snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
767 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Clemens Ladisch8ffbc012007-09-17 09:39:51 +0200769 if (channels == 4)
770 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
771 else
772 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
773 spin_unlock_irq(&cm->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 return 0;
776}
777
778
779/*
780 * prepare playback/capture channel
781 * channel to be used must have been set in rec->ch.
782 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100783static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
784 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Timofei Bondarenko755c48a2007-10-31 17:36:20 +0100786 unsigned int reg, freq, freq_ext, val;
Clemens Ladischebe9e282007-09-17 09:40:57 +0200787 unsigned int period_size;
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;
Clemens Ladischebe9e282007-09-17 09:40:57 +0200807 period_size = runtime->period_size << rec->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (runtime->channels > 2) {
809 /* multi-channels */
810 rec->dma_size = (rec->dma_size * runtime->channels) / 2;
Clemens Ladischebe9e282007-09-17 09:40:57 +0200811 period_size = (period_size * runtime->channels) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
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);
Clemens Ladischebe9e282007-09-17 09:40:57 +0200822 snd_cmipci_write_w(cm, reg + 2, period_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
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 */
Timofei Bondarenko755c48a2007-10-31 17:36:20 +0100834 freq = 0;
835 freq_ext = 0;
836 if (runtime->rate > 48000)
837 switch (runtime->rate) {
838 case 88200: freq_ext = CM_CH0_SRATE_88K; break;
839 case 96000: freq_ext = CM_CH0_SRATE_96K; break;
840 case 128000: freq_ext = CM_CH0_SRATE_128K; break;
841 default: snd_BUG(); break;
842 }
843 else
844 freq = snd_cmipci_rate_freq(runtime->rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
846 if (rec->ch) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 val &= ~CM_DSFC_MASK;
848 val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
Clemens Ladischa839a332007-09-17 09:35:46 +0200849 } else {
850 val &= ~CM_ASFC_MASK;
851 val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
854 //snd_printd("cmipci: functrl1 = %08x\n", val);
855
856 /* set format */
857 val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
858 if (rec->ch) {
859 val &= ~CM_CH1FMT_MASK;
860 val |= rec->fmt << CM_CH1FMT_SHIFT;
861 } else {
862 val &= ~CM_CH0FMT_MASK;
863 val |= rec->fmt << CM_CH0FMT_SHIFT;
864 }
Timofei Bondarenko755c48a2007-10-31 17:36:20 +0100865 if (cm->can_96k) {
866 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
867 val |= freq_ext << (rec->ch * 2);
Clemens Ladisch8992e182007-09-03 09:54:55 +0200868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
870 //snd_printd("cmipci: chformat = %08x\n", val);
871
872 rec->running = 0;
873 spin_unlock_irq(&cm->reg_lock);
874
875 return 0;
876}
877
878/*
879 * PCM trigger/stop
880 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100881static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
Clemens Ladischebe9e282007-09-17 09:40:57 +0200882 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 unsigned int inthld, chen, reset, pause;
885 int result = 0;
886
887 inthld = CM_CH0_INT_EN << rec->ch;
888 chen = CM_CHEN0 << rec->ch;
889 reset = CM_RST_CH0 << rec->ch;
890 pause = CM_PAUSE0 << rec->ch;
891
892 spin_lock(&cm->reg_lock);
893 switch (cmd) {
894 case SNDRV_PCM_TRIGGER_START:
895 rec->running = 1;
896 /* set interrupt */
897 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
898 cm->ctrl |= chen;
899 /* enable channel */
900 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
901 //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
902 break;
903 case SNDRV_PCM_TRIGGER_STOP:
904 rec->running = 0;
905 /* disable interrupt */
906 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
907 /* reset */
908 cm->ctrl &= ~chen;
909 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
910 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
Clemens Ladischc36fd8c2007-09-17 09:41:36 +0200911 rec->needs_silencing = rec->is_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
913 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100914 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 cm->ctrl |= pause;
916 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
917 break;
918 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaicb60e5f2005-11-17 16:14:49 +0100919 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 cm->ctrl &= ~pause;
921 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
922 break;
923 default:
924 result = -EINVAL;
925 break;
926 }
927 spin_unlock(&cm->reg_lock);
928 return result;
929}
930
931/*
932 * return the current pointer
933 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100934static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
935 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 size_t ptr;
938 unsigned int reg;
939 if (!rec->running)
940 return 0;
941#if 1 // this seems better..
942 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
943 ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
944 ptr >>= rec->shift;
945#else
946 reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
947 ptr = snd_cmipci_read(cm, reg) - rec->offset;
948 ptr = bytes_to_frames(substream->runtime, ptr);
949#endif
950 if (substream->runtime->channels > 2)
951 ptr = (ptr * 2) / substream->runtime->channels;
952 return ptr;
953}
954
955/*
956 * playback
957 */
958
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100959static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int cmd)
961{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100962 struct cmipci *cm = snd_pcm_substream_chip(substream);
Clemens Ladischebe9e282007-09-17 09:40:57 +0200963 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100966static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100968 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
970}
971
972
973
974/*
975 * capture
976 */
977
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100978static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int cmd)
980{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100981 struct cmipci *cm = snd_pcm_substream_chip(substream);
Clemens Ladischebe9e282007-09-17 09:40:57 +0200982 return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100985static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100987 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
989}
990
991
992/*
993 * hw preparation for spdif
994 */
995
Takashi Iwai2cbdb682005-11-17 15:03:13 +0100996static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
997 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1000 uinfo->count = 1;
1001 return 0;
1002}
1003
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001004static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
1005 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001007 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int i;
1009
1010 spin_lock_irq(&chip->reg_lock);
1011 for (i = 0; i < 4; i++)
1012 ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
1013 spin_unlock_irq(&chip->reg_lock);
1014 return 0;
1015}
1016
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001017static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001020 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int i, change;
1022 unsigned int val;
1023
1024 val = 0;
1025 spin_lock_irq(&chip->reg_lock);
1026 for (i = 0; i < 4; i++)
1027 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1028 change = val != chip->dig_status;
1029 chip->dig_status = val;
1030 spin_unlock_irq(&chip->reg_lock);
1031 return change;
1032}
1033
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001034static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1037 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1038 .info = snd_cmipci_spdif_default_info,
1039 .get = snd_cmipci_spdif_default_get,
1040 .put = snd_cmipci_spdif_default_put
1041};
1042
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001043static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1047 uinfo->count = 1;
1048 return 0;
1049}
1050
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001051static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1052 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 ucontrol->value.iec958.status[0] = 0xff;
1055 ucontrol->value.iec958.status[1] = 0xff;
1056 ucontrol->value.iec958.status[2] = 0xff;
1057 ucontrol->value.iec958.status[3] = 0xff;
1058 return 0;
1059}
1060
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001061static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001064 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1066 .info = snd_cmipci_spdif_mask_info,
1067 .get = snd_cmipci_spdif_mask_get,
1068};
1069
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001070static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
1071 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1074 uinfo->count = 1;
1075 return 0;
1076}
1077
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001078static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001081 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 int i;
1083
1084 spin_lock_irq(&chip->reg_lock);
1085 for (i = 0; i < 4; i++)
1086 ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
1087 spin_unlock_irq(&chip->reg_lock);
1088 return 0;
1089}
1090
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001091static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1092 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001094 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int i, change;
1096 unsigned int val;
1097
1098 val = 0;
1099 spin_lock_irq(&chip->reg_lock);
1100 for (i = 0; i < 4; i++)
1101 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
1102 change = val != chip->dig_pcm_status;
1103 chip->dig_pcm_status = val;
1104 spin_unlock_irq(&chip->reg_lock);
1105 return change;
1106}
1107
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001108static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1111 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1112 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1113 .info = snd_cmipci_spdif_stream_info,
1114 .get = snd_cmipci_spdif_stream_get,
1115 .put = snd_cmipci_spdif_stream_put
1116};
1117
1118/*
1119 */
1120
1121/* save mixer setting and mute for AC3 playback */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001122static int save_mixer_state(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 if (! cm->mixer_insensitive) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001125 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned int i;
1127
1128 val = kmalloc(sizeof(*val), GFP_ATOMIC);
1129 if (!val)
1130 return -ENOMEM;
1131 for (i = 0; i < CM_SAVED_MIXERS; i++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001132 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (ctl) {
1134 int event;
1135 memset(val, 0, sizeof(*val));
1136 ctl->get(ctl, val);
1137 cm->mixer_res_status[i] = val->value.integer.value[0];
1138 val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
1139 event = SNDRV_CTL_EVENT_MASK_INFO;
1140 if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
1141 ctl->put(ctl, val); /* toggle */
1142 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1143 }
1144 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1145 snd_ctl_notify(cm->card, event, &ctl->id);
1146 }
1147 }
1148 kfree(val);
1149 cm->mixer_insensitive = 1;
1150 }
1151 return 0;
1152}
1153
1154
1155/* restore the previously saved mixer status */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001156static void restore_mixer_state(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 if (cm->mixer_insensitive) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001159 struct snd_ctl_elem_value *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 unsigned int i;
1161
1162 val = kmalloc(sizeof(*val), GFP_KERNEL);
1163 if (!val)
1164 return;
1165 cm->mixer_insensitive = 0; /* at first clear this;
1166 otherwise the changes will be ignored */
1167 for (i = 0; i < CM_SAVED_MIXERS; i++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001168 struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (ctl) {
1170 int event;
1171
1172 memset(val, 0, sizeof(*val));
1173 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1174 ctl->get(ctl, val);
1175 event = SNDRV_CTL_EVENT_MASK_INFO;
1176 if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
1177 val->value.integer.value[0] = cm->mixer_res_status[i];
1178 ctl->put(ctl, val);
1179 event |= SNDRV_CTL_EVENT_MASK_VALUE;
1180 }
1181 snd_ctl_notify(cm->card, event, &ctl->id);
1182 }
1183 }
1184 kfree(val);
1185 }
1186}
1187
1188/* spinlock held! */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001189static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 if (do_ac3) {
1192 /* AC3EN for 037 */
1193 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1194 /* AC3EN for 039 */
1195 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1196
1197 if (cm->can_ac3_hw) {
1198 /* SPD24SEL for 037, 0x02 */
1199 /* SPD24SEL for 039, 0x20, but cannot be set */
1200 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1201 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1202 } else { /* can_ac3_sw */
1203 /* SPD32SEL for 037 & 039, 0x20 */
1204 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1205 /* set 176K sample rate to fix 033 HW bug */
1206 if (cm->chip_version == 33) {
1207 if (rate >= 48000) {
1208 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1209 } else {
1210 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1211 }
1212 }
1213 }
1214
1215 } else {
1216 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
1217 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
1218
1219 if (cm->can_ac3_hw) {
1220 /* chip model >= 37 */
1221 if (snd_pcm_format_width(subs->runtime->format) > 16) {
1222 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1223 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1224 } else {
1225 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1226 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1227 }
1228 } else {
1229 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
1230 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
1231 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
1232 }
1233 }
1234}
1235
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001236static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 int rate, err;
1239
1240 rate = subs->runtime->rate;
1241
1242 if (up && do_ac3)
1243 if ((err = save_mixer_state(cm)) < 0)
1244 return err;
1245
1246 spin_lock_irq(&cm->reg_lock);
1247 cm->spdif_playback_avail = up;
1248 if (up) {
1249 /* they are controlled via "IEC958 Output Switch" */
1250 /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1251 /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
1252 if (cm->spdif_playback_enabled)
1253 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1254 setup_ac3(cm, subs, do_ac3, rate);
1255
Clemens Ladisch8992e182007-09-03 09:54:55 +02001256 if (rate == 48000 || rate == 96000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
1258 else
1259 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
Clemens Ladisch8992e182007-09-03 09:54:55 +02001260 if (rate > 48000)
1261 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1262 else
1263 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else {
1265 /* they are controlled via "IEC958 Output Switch" */
1266 /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
1267 /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
Clemens Ladisch8992e182007-09-03 09:54:55 +02001268 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
1270 setup_ac3(cm, subs, 0, 0);
1271 }
1272 spin_unlock_irq(&cm->reg_lock);
1273 return 0;
1274}
1275
1276
1277/*
1278 * preparation
1279 */
1280
1281/* playback - enable spdif only on the certain condition */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001282static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001284 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int rate = substream->runtime->rate;
1286 int err, do_spdif, do_ac3 = 0;
1287
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001288 do_spdif = (rate >= 44100 && rate <= 96000 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
1290 substream->runtime->channels == 2);
1291 if (do_spdif && cm->can_ac3_hw)
1292 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1293 if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
1294 return err;
1295 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1296}
1297
1298/* playback (via device #2) - enable spdif always */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001299static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001301 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int err, do_ac3;
1303
1304 if (cm->can_ac3_hw)
1305 do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
1306 else
1307 do_ac3 = 1; /* doesn't matter */
1308 if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
1309 return err;
1310 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
1311}
1312
Clemens Ladischc36fd8c2007-09-17 09:41:36 +02001313/*
1314 * Apparently, the samples last played on channel A stay in some buffer, even
1315 * after the channel is reset, and get added to the data for the rear DACs when
1316 * playing a multichannel stream on channel B. This is likely to generate
1317 * wraparounds and thus distortions.
1318 * To avoid this, we play at least one zero sample after the actual stream has
1319 * stopped.
1320 */
1321static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
1322{
1323 struct snd_pcm_runtime *runtime = rec->substream->runtime;
1324 unsigned int reg, val;
1325
1326 if (rec->needs_silencing && runtime && runtime->dma_area) {
1327 /* set up a small silence buffer */
1328 memset(runtime->dma_area, 0, PAGE_SIZE);
1329 reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1330 val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
1331 snd_cmipci_write(cm, reg, val);
1332
1333 /* configure for 16 bits, 2 channels, 8 kHz */
1334 if (runtime->channels > 2)
1335 set_dac_channels(cm, rec, 2);
1336 spin_lock_irq(&cm->reg_lock);
1337 val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
1338 val &= ~(CM_ASFC_MASK << (rec->ch * 3));
1339 val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
1340 snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
1341 val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
1342 val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
1343 val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001344 if (cm->can_96k)
1345 val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
Clemens Ladischc36fd8c2007-09-17 09:41:36 +02001346 snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
1347
1348 /* start stream (we don't need interrupts) */
1349 cm->ctrl |= CM_CHEN0 << rec->ch;
1350 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
1351 spin_unlock_irq(&cm->reg_lock);
1352
1353 msleep(1);
1354
1355 /* stop and reset stream */
1356 spin_lock_irq(&cm->reg_lock);
1357 cm->ctrl &= ~(CM_CHEN0 << rec->ch);
1358 val = CM_RST_CH0 << rec->ch;
1359 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
1360 snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
1361 spin_unlock_irq(&cm->reg_lock);
1362
1363 rec->needs_silencing = 0;
1364 }
1365}
1366
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001367static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001369 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 setup_spdif_playback(cm, substream, 0, 0);
1371 restore_mixer_state(cm);
Clemens Ladischc36fd8c2007-09-17 09:41:36 +02001372 snd_cmipci_silence_hack(cm, &cm->channel[0]);
1373 return snd_cmipci_hw_free(substream);
1374}
1375
1376static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
1377{
1378 struct cmipci *cm = snd_pcm_substream_chip(substream);
1379 snd_cmipci_silence_hack(cm, &cm->channel[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return snd_cmipci_hw_free(substream);
1381}
1382
1383/* capture */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001384static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001386 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1388}
1389
1390/* capture with spdif (via device #2) */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001391static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001393 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 spin_lock_irq(&cm->reg_lock);
1396 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001397 if (cm->can_96k) {
1398 if (substream->runtime->rate > 48000)
1399 snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1400 else
1401 snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 spin_unlock_irq(&cm->reg_lock);
1404
1405 return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
1406}
1407
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001408static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001410 struct cmipci *cm = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 spin_lock_irq(&cm->reg_lock);
1413 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1414 spin_unlock_irq(&cm->reg_lock);
1415
1416 return snd_cmipci_hw_free(subs);
1417}
1418
1419
1420/*
1421 * interrupt handler
1422 */
David Howells7d12e782006-10-05 14:55:46 +01001423static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001425 struct cmipci *cm = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 unsigned int status, mask = 0;
1427
1428 /* fastpath out, to ease interrupt sharing */
1429 status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
1430 if (!(status & CM_INTR))
1431 return IRQ_NONE;
1432
1433 /* acknowledge interrupt */
1434 spin_lock(&cm->reg_lock);
1435 if (status & CM_CHINT0)
1436 mask |= CM_CH0_INT_EN;
1437 if (status & CM_CHINT1)
1438 mask |= CM_CH1_INT_EN;
1439 snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
1440 snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
1441 spin_unlock(&cm->reg_lock);
1442
1443 if (cm->rmidi && (status & CM_UARTINT))
David Howells7d12e782006-10-05 14:55:46 +01001444 snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (cm->pcm) {
1447 if ((status & CM_CHINT0) && cm->channel[0].running)
1448 snd_pcm_period_elapsed(cm->channel[0].substream);
1449 if ((status & CM_CHINT1) && cm->channel[1].running)
1450 snd_pcm_period_elapsed(cm->channel[1].substream);
1451 }
1452 return IRQ_HANDLED;
1453}
1454
1455/*
1456 * h/w infos
1457 */
1458
1459/* playback on channel A */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001460static struct snd_pcm_hardware snd_cmipci_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1463 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001464 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1466 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1467 .rate_min = 5512,
1468 .rate_max = 48000,
1469 .channels_min = 1,
1470 .channels_max = 2,
1471 .buffer_bytes_max = (128*1024),
1472 .period_bytes_min = 64,
1473 .period_bytes_max = (128*1024),
1474 .periods_min = 2,
1475 .periods_max = 1024,
1476 .fifo_size = 0,
1477};
1478
1479/* capture on channel B */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001480static struct snd_pcm_hardware snd_cmipci_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1483 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001484 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1486 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1487 .rate_min = 5512,
1488 .rate_max = 48000,
1489 .channels_min = 1,
1490 .channels_max = 2,
1491 .buffer_bytes_max = (128*1024),
1492 .period_bytes_min = 64,
1493 .period_bytes_max = (128*1024),
1494 .periods_min = 2,
1495 .periods_max = 1024,
1496 .fifo_size = 0,
1497};
1498
1499/* playback on channel B - stereo 16bit only? */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001500static struct snd_pcm_hardware snd_cmipci_playback2 =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1503 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001504 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1506 .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
1507 .rate_min = 5512,
1508 .rate_max = 48000,
1509 .channels_min = 2,
1510 .channels_max = 2,
1511 .buffer_bytes_max = (128*1024),
1512 .period_bytes_min = 64,
1513 .period_bytes_max = (128*1024),
1514 .periods_min = 2,
1515 .periods_max = 1024,
1516 .fifo_size = 0,
1517};
1518
1519/* spdif playback on channel A */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001520static struct snd_pcm_hardware snd_cmipci_playback_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1523 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001524 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1526 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1527 .rate_min = 44100,
1528 .rate_max = 48000,
1529 .channels_min = 2,
1530 .channels_max = 2,
1531 .buffer_bytes_max = (128*1024),
1532 .period_bytes_min = 64,
1533 .period_bytes_max = (128*1024),
1534 .periods_min = 2,
1535 .periods_max = 1024,
1536 .fifo_size = 0,
1537};
1538
1539/* spdif playback on channel A (32bit, IEC958 subframes) */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001540static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1543 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001544 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1546 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1547 .rate_min = 44100,
1548 .rate_max = 48000,
1549 .channels_min = 2,
1550 .channels_max = 2,
1551 .buffer_bytes_max = (128*1024),
1552 .period_bytes_min = 64,
1553 .period_bytes_max = (128*1024),
1554 .periods_min = 2,
1555 .periods_max = 1024,
1556 .fifo_size = 0,
1557};
1558
1559/* spdif capture on channel B */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001560static struct snd_pcm_hardware snd_cmipci_capture_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1563 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01001564 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1566 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1567 .rate_min = 44100,
1568 .rate_max = 48000,
1569 .channels_min = 2,
1570 .channels_max = 2,
1571 .buffer_bytes_max = (128*1024),
1572 .period_bytes_min = 64,
1573 .period_bytes_max = (128*1024),
1574 .periods_min = 2,
1575 .periods_max = 1024,
1576 .fifo_size = 0,
1577};
1578
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001579static unsigned int rate_constraints[] = { 5512, 8000, 11025, 16000, 22050,
1580 32000, 44100, 48000, 88200, 96000, 128000 };
1581static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
1582 .count = ARRAY_SIZE(rate_constraints),
1583 .list = rate_constraints,
1584 .mask = 0,
1585};
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/*
1588 * check device open/close
1589 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001590static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 int ch = mode & CM_OPEN_CH_MASK;
1593
1594 /* FIXME: a file should wait until the device becomes free
1595 * when it's opened on blocking mode. however, since the current
1596 * pcm framework doesn't pass file pointer before actually opened,
1597 * we can't know whether blocking mode or not in open callback..
1598 */
Ingo Molnar62932df2006-01-16 16:34:20 +01001599 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (cm->opened[ch]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001601 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return -EBUSY;
1603 }
1604 cm->opened[ch] = mode;
1605 cm->channel[ch].substream = subs;
1606 if (! (mode & CM_OPEN_DAC)) {
1607 /* disable dual DAC mode */
1608 cm->channel[ch].is_dac = 0;
1609 spin_lock_irq(&cm->reg_lock);
1610 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1611 spin_unlock_irq(&cm->reg_lock);
1612 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001613 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
1616
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001617static void close_device_check(struct cmipci *cm, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 int ch = mode & CM_OPEN_CH_MASK;
1620
Ingo Molnar62932df2006-01-16 16:34:20 +01001621 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (cm->opened[ch] == mode) {
1623 if (cm->channel[ch].substream) {
1624 snd_cmipci_ch_reset(cm, ch);
1625 cm->channel[ch].running = 0;
1626 cm->channel[ch].substream = NULL;
1627 }
1628 cm->opened[ch] = 0;
1629 if (! cm->channel[ch].is_dac) {
1630 /* enable dual DAC mode again */
1631 cm->channel[ch].is_dac = 1;
1632 spin_lock_irq(&cm->reg_lock);
1633 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
1634 spin_unlock_irq(&cm->reg_lock);
1635 }
1636 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001637 mutex_unlock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640/*
1641 */
1642
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001643static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001645 struct cmipci *cm = snd_pcm_substream_chip(substream);
1646 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int err;
1648
1649 if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
1650 return err;
1651 runtime->hw = snd_cmipci_playback;
Clemens Ladisch8992e182007-09-03 09:54:55 +02001652 if (cm->chip_version == 68) {
1653 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1654 SNDRV_PCM_RATE_96000;
1655 runtime->hw.rate_max = 96000;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001656 } else if (cm->chip_version == 55) {
1657 err = snd_pcm_hw_constraint_list(runtime, 0,
1658 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1659 if (err < 0)
1660 return err;
1661 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1662 runtime->hw.rate_max = 128000;
Clemens Ladisch8992e182007-09-03 09:54:55 +02001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1665 cm->dig_pcm_status = cm->dig_status;
1666 return 0;
1667}
1668
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001669static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001671 struct cmipci *cm = snd_pcm_substream_chip(substream);
1672 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 int err;
1674
1675 if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
1676 return err;
1677 runtime->hw = snd_cmipci_capture;
1678 if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
1679 runtime->hw.rate_min = 41000;
1680 runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001681 } else if (cm->chip_version == 55) {
1682 err = snd_pcm_hw_constraint_list(runtime, 0,
1683 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1684 if (err < 0)
1685 return err;
1686 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1687 runtime->hw.rate_max = 128000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
1690 return 0;
1691}
1692
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001693static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001695 struct cmipci *cm = snd_pcm_substream_chip(substream);
1696 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int err;
1698
1699 if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
1700 return err;
1701 runtime->hw = snd_cmipci_playback2;
Ingo Molnar62932df2006-01-16 16:34:20 +01001702 mutex_lock(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (! cm->opened[CM_CH_PLAY]) {
1704 if (cm->can_multi_ch) {
1705 runtime->hw.channels_max = cm->max_channels;
1706 if (cm->max_channels == 4)
1707 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
1708 else if (cm->max_channels == 6)
1709 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
1710 else if (cm->max_channels == 8)
1711 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001714 mutex_unlock(&cm->open_mutex);
Clemens Ladisch22a22f52007-09-17 09:37:47 +02001715 if (cm->chip_version == 68) {
1716 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1717 SNDRV_PCM_RATE_96000;
1718 runtime->hw.rate_max = 96000;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001719 } else if (cm->chip_version == 55) {
1720 err = snd_pcm_hw_constraint_list(runtime, 0,
1721 SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1722 if (err < 0)
1723 return err;
1724 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1725 runtime->hw.rate_max = 128000;
Clemens Ladisch22a22f52007-09-17 09:37:47 +02001726 }
1727 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return 0;
1729}
1730
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001731static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001733 struct cmipci *cm = snd_pcm_substream_chip(substream);
1734 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int err;
1736
1737 if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
1738 return err;
1739 if (cm->can_ac3_hw) {
1740 runtime->hw = snd_cmipci_playback_spdif;
Clemens Ladisch57bd68b2007-09-07 10:44:13 +02001741 if (cm->chip_version >= 37) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
Clemens Ladisch57bd68b2007-09-07 10:44:13 +02001743 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1744 }
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001745 if (cm->can_96k) {
Clemens Ladisch8992e182007-09-03 09:54:55 +02001746 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1747 SNDRV_PCM_RATE_96000;
1748 runtime->hw.rate_max = 96000;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 } else {
1751 runtime->hw = snd_cmipci_playback_iec958_subframe;
1752 }
1753 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1754 cm->dig_pcm_status = cm->dig_status;
1755 return 0;
1756}
1757
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001758static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001760 struct cmipci *cm = snd_pcm_substream_chip(substream);
1761 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int err;
1763
1764 if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
1765 return err;
1766 runtime->hw = snd_cmipci_capture_spdif;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01001767 if (cm->can_96k && !(cm->chip_version == 68)) {
1768 runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1769 SNDRV_PCM_RATE_96000;
1770 runtime->hw.rate_max = 96000;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
1773 return 0;
1774}
1775
1776
1777/*
1778 */
1779
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001780static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001782 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 close_device_check(cm, CM_OPEN_PLAYBACK);
1784 return 0;
1785}
1786
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001787static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001789 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 close_device_check(cm, CM_OPEN_CAPTURE);
1791 return 0;
1792}
1793
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001794static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001796 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 close_device_check(cm, CM_OPEN_PLAYBACK2);
1798 close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
1799 return 0;
1800}
1801
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001802static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001804 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
1806 return 0;
1807}
1808
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001809static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001811 struct cmipci *cm = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
1813 return 0;
1814}
1815
1816
1817/*
1818 */
1819
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001820static struct snd_pcm_ops snd_cmipci_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .open = snd_cmipci_playback_open,
1822 .close = snd_cmipci_playback_close,
1823 .ioctl = snd_pcm_lib_ioctl,
1824 .hw_params = snd_cmipci_hw_params,
1825 .hw_free = snd_cmipci_playback_hw_free,
1826 .prepare = snd_cmipci_playback_prepare,
1827 .trigger = snd_cmipci_playback_trigger,
1828 .pointer = snd_cmipci_playback_pointer,
1829};
1830
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001831static struct snd_pcm_ops snd_cmipci_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 .open = snd_cmipci_capture_open,
1833 .close = snd_cmipci_capture_close,
1834 .ioctl = snd_pcm_lib_ioctl,
1835 .hw_params = snd_cmipci_hw_params,
1836 .hw_free = snd_cmipci_hw_free,
1837 .prepare = snd_cmipci_capture_prepare,
1838 .trigger = snd_cmipci_capture_trigger,
1839 .pointer = snd_cmipci_capture_pointer,
1840};
1841
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001842static struct snd_pcm_ops snd_cmipci_playback2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 .open = snd_cmipci_playback2_open,
1844 .close = snd_cmipci_playback2_close,
1845 .ioctl = snd_pcm_lib_ioctl,
1846 .hw_params = snd_cmipci_playback2_hw_params,
Clemens Ladischc36fd8c2007-09-17 09:41:36 +02001847 .hw_free = snd_cmipci_playback2_hw_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 .prepare = snd_cmipci_capture_prepare, /* channel B */
1849 .trigger = snd_cmipci_capture_trigger, /* channel B */
1850 .pointer = snd_cmipci_capture_pointer, /* channel B */
1851};
1852
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001853static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 .open = snd_cmipci_playback_spdif_open,
1855 .close = snd_cmipci_playback_spdif_close,
1856 .ioctl = snd_pcm_lib_ioctl,
1857 .hw_params = snd_cmipci_hw_params,
1858 .hw_free = snd_cmipci_playback_hw_free,
1859 .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
1860 .trigger = snd_cmipci_playback_trigger,
1861 .pointer = snd_cmipci_playback_pointer,
1862};
1863
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001864static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .open = snd_cmipci_capture_spdif_open,
1866 .close = snd_cmipci_capture_spdif_close,
1867 .ioctl = snd_pcm_lib_ioctl,
1868 .hw_params = snd_cmipci_hw_params,
1869 .hw_free = snd_cmipci_capture_spdif_hw_free,
1870 .prepare = snd_cmipci_capture_spdif_prepare,
1871 .trigger = snd_cmipci_capture_trigger,
1872 .pointer = snd_cmipci_capture_pointer,
1873};
1874
1875
1876/*
1877 */
1878
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001879static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001881 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int err;
1883
1884 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1885 if (err < 0)
1886 return err;
1887
1888 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
1889 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
1890
1891 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 pcm->info_flags = 0;
1893 strcpy(pcm->name, "C-Media PCI DAC/ADC");
1894 cm->pcm = pcm;
1895
1896 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1897 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1898
1899 return 0;
1900}
1901
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001902static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001904 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int err;
1906
1907 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
1908 if (err < 0)
1909 return err;
1910
1911 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
1912
1913 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 pcm->info_flags = 0;
1915 strcpy(pcm->name, "C-Media PCI 2nd DAC");
1916 cm->pcm2 = pcm;
1917
1918 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1919 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1920
1921 return 0;
1922}
1923
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001924static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001926 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int err;
1928
1929 err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
1930 if (err < 0)
1931 return err;
1932
1933 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
1934 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
1935
1936 pcm->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 pcm->info_flags = 0;
1938 strcpy(pcm->name, "C-Media PCI IEC958");
1939 cm->pcm_spdif = pcm;
1940
1941 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1942 snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
1943
1944 return 0;
1945}
1946
1947/*
1948 * mixer interface:
1949 * - CM8338/8738 has a compatible mixer interface with SB16, but
1950 * lack of some elements like tone control, i/o gain and AGC.
1951 * - Access to native registers:
1952 * - A 3D switch
1953 * - Output mute switches
1954 */
1955
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001956static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1959 outb(data, s->iobase + CM_REG_SB16_DATA);
1960}
1961
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001962static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 unsigned char v;
1965
1966 outb(idx, s->iobase + CM_REG_SB16_ADDR);
1967 v = inb(s->iobase + CM_REG_SB16_DATA);
1968 return v;
1969}
1970
1971/*
1972 * general mixer element
1973 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001974struct cmipci_sb_reg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 unsigned int left_reg, right_reg;
1976 unsigned int left_shift, right_shift;
1977 unsigned int mask;
1978 unsigned int invert: 1;
1979 unsigned int stereo: 1;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001980};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982#define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
1983 ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
1984
1985#define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
1986{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1987 .info = snd_cmipci_info_volume, \
1988 .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
1989 .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
1990}
1991
1992#define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
1993#define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
1994#define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
1995#define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
1996
Takashi Iwai2cbdb682005-11-17 15:03:13 +01001997static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 r->left_reg = val & 0xff;
2000 r->right_reg = (val >> 8) & 0xff;
2001 r->left_shift = (val >> 16) & 0x07;
2002 r->right_shift = (val >> 19) & 0x07;
2003 r->invert = (val >> 22) & 1;
2004 r->stereo = (val >> 23) & 1;
2005 r->mask = (val >> 24) & 0xff;
2006}
2007
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002008static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
2009 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002011 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2014 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2015 uinfo->count = reg.stereo + 1;
2016 uinfo->value.integer.min = 0;
2017 uinfo->value.integer.max = reg.mask;
2018 return 0;
2019}
2020
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002021static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002024 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2025 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int val;
2027
2028 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2029 spin_lock_irq(&cm->reg_lock);
2030 val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
2031 if (reg.invert)
2032 val = reg.mask - val;
2033 ucontrol->value.integer.value[0] = val;
2034 if (reg.stereo) {
2035 val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
2036 if (reg.invert)
2037 val = reg.mask - val;
2038 ucontrol->value.integer.value[1] = val;
2039 }
2040 spin_unlock_irq(&cm->reg_lock);
2041 return 0;
2042}
2043
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002044static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
2045 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002047 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2048 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int change;
2050 int left, right, oleft, oright;
2051
2052 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2053 left = ucontrol->value.integer.value[0] & reg.mask;
2054 if (reg.invert)
2055 left = reg.mask - left;
2056 left <<= reg.left_shift;
2057 if (reg.stereo) {
2058 right = ucontrol->value.integer.value[1] & reg.mask;
2059 if (reg.invert)
2060 right = reg.mask - right;
2061 right <<= reg.right_shift;
2062 } else
2063 right = 0;
2064 spin_lock_irq(&cm->reg_lock);
2065 oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
2066 left |= oleft & ~(reg.mask << reg.left_shift);
2067 change = left != oleft;
2068 if (reg.stereo) {
2069 if (reg.left_reg != reg.right_reg) {
2070 snd_cmipci_mixer_write(cm, reg.left_reg, left);
2071 oright = snd_cmipci_mixer_read(cm, reg.right_reg);
2072 } else
2073 oright = left;
2074 right |= oright & ~(reg.mask << reg.right_shift);
2075 change |= right != oright;
2076 snd_cmipci_mixer_write(cm, reg.right_reg, right);
2077 } else
2078 snd_cmipci_mixer_write(cm, reg.left_reg, left);
2079 spin_unlock_irq(&cm->reg_lock);
2080 return change;
2081}
2082
2083/*
2084 * input route (left,right) -> (left,right)
2085 */
2086#define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
2087{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2088 .info = snd_cmipci_info_input_sw, \
2089 .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
2090 .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
2091}
2092
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002093static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
2094 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2097 uinfo->count = 4;
2098 uinfo->value.integer.min = 0;
2099 uinfo->value.integer.max = 1;
2100 return 0;
2101}
2102
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002103static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002106 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2107 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 int val1, val2;
2109
2110 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2111 spin_lock_irq(&cm->reg_lock);
2112 val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2113 val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2114 spin_unlock_irq(&cm->reg_lock);
2115 ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
2116 ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
2117 ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
2118 ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
2119 return 0;
2120}
2121
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002122static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002125 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2126 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int change;
2128 int val1, val2, oval1, oval2;
2129
2130 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2131 spin_lock_irq(&cm->reg_lock);
2132 oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
2133 oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
2134 val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2135 val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
2136 val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
2137 val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
2138 val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
2139 val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
2140 change = val1 != oval1 || val2 != oval2;
2141 snd_cmipci_mixer_write(cm, reg.left_reg, val1);
2142 snd_cmipci_mixer_write(cm, reg.right_reg, val2);
2143 spin_unlock_irq(&cm->reg_lock);
2144 return change;
2145}
2146
2147/*
2148 * native mixer switches/volumes
2149 */
2150
2151#define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
2152{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2153 .info = snd_cmipci_info_native_mixer, \
2154 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2155 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
2156}
2157
2158#define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
2159{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2160 .info = snd_cmipci_info_native_mixer, \
2161 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2162 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
2163}
2164
2165#define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
2166{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2167 .info = snd_cmipci_info_native_mixer, \
2168 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2169 .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
2170}
2171
2172#define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
2173{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2174 .info = snd_cmipci_info_native_mixer, \
2175 .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
2176 .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
2177}
2178
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002179static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002182 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2185 uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2186 uinfo->count = reg.stereo + 1;
2187 uinfo->value.integer.min = 0;
2188 uinfo->value.integer.max = reg.mask;
2189 return 0;
2190
2191}
2192
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002193static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002196 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2197 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 unsigned char oreg, val;
2199
2200 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2201 spin_lock_irq(&cm->reg_lock);
2202 oreg = inb(cm->iobase + reg.left_reg);
2203 val = (oreg >> reg.left_shift) & reg.mask;
2204 if (reg.invert)
2205 val = reg.mask - val;
2206 ucontrol->value.integer.value[0] = val;
2207 if (reg.stereo) {
2208 val = (oreg >> reg.right_shift) & reg.mask;
2209 if (reg.invert)
2210 val = reg.mask - val;
2211 ucontrol->value.integer.value[1] = val;
2212 }
2213 spin_unlock_irq(&cm->reg_lock);
2214 return 0;
2215}
2216
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002217static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002220 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
2221 struct cmipci_sb_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 unsigned char oreg, nreg, val;
2223
2224 cmipci_sb_reg_decode(&reg, kcontrol->private_value);
2225 spin_lock_irq(&cm->reg_lock);
2226 oreg = inb(cm->iobase + reg.left_reg);
2227 val = ucontrol->value.integer.value[0] & reg.mask;
2228 if (reg.invert)
2229 val = reg.mask - val;
2230 nreg = oreg & ~(reg.mask << reg.left_shift);
2231 nreg |= (val << reg.left_shift);
2232 if (reg.stereo) {
2233 val = ucontrol->value.integer.value[1] & reg.mask;
2234 if (reg.invert)
2235 val = reg.mask - val;
2236 nreg &= ~(reg.mask << reg.right_shift);
2237 nreg |= (val << reg.right_shift);
2238 }
2239 outb(nreg, cm->iobase + reg.left_reg);
2240 spin_unlock_irq(&cm->reg_lock);
2241 return (nreg != oreg);
2242}
2243
2244/*
2245 * special case - check mixer sensitivity
2246 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002247static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002250 //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
2252}
2253
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002254static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002257 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (cm->mixer_insensitive) {
2259 /* ignored */
2260 return 0;
2261 }
2262 return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
2263}
2264
2265
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002266static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
2268 CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
2269 CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
2270 //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
2271 { /* switch with sensitivity */
2272 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2273 .name = "PCM Playback Switch",
2274 .info = snd_cmipci_info_native_mixer,
2275 .get = snd_cmipci_get_native_mixer_sensitive,
2276 .put = snd_cmipci_put_native_mixer_sensitive,
2277 .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
2278 },
2279 CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
2280 CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
2281 CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
2282 CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
2283 CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
2284 CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
2285 CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
2286 CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
2287 CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
2288 CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
2289 CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
2290 CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
2291 CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
2292 CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
2293 CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
2294 CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
2295 CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
Takashi Iwai2eff7ec2005-06-30 13:45:20 +02002296 CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
Takashi Iwai2eff7ec2005-06-30 13:45:20 +02002298 CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
2299 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 +02002300 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 +02002301 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 -07002302};
2303
2304/*
2305 * other switches
2306 */
2307
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002308struct cmipci_switch_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int reg; /* register index */
2310 unsigned int mask; /* mask bits */
2311 unsigned int mask_on; /* mask bits to turn on */
2312 unsigned int is_byte: 1; /* byte access? */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002313 unsigned int ac3_sensitive: 1; /* access forbidden during
2314 * non-audio operation?
2315 */
2316};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002318#define snd_cmipci_uswitch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002320static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_value *ucontrol,
2322 struct cmipci_switch_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 unsigned int val;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002325 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 spin_lock_irq(&cm->reg_lock);
2328 if (args->ac3_sensitive && cm->mixer_insensitive) {
2329 ucontrol->value.integer.value[0] = 0;
2330 spin_unlock_irq(&cm->reg_lock);
2331 return 0;
2332 }
2333 if (args->is_byte)
2334 val = inb(cm->iobase + args->reg);
2335 else
2336 val = snd_cmipci_read(cm, args->reg);
2337 ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
2338 spin_unlock_irq(&cm->reg_lock);
2339 return 0;
2340}
2341
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002342static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002345 struct cmipci_switch_args *args;
2346 args = (struct cmipci_switch_args *)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 snd_assert(args != NULL, return -EINVAL);
2348 return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
2349}
2350
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002351static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_value *ucontrol,
2353 struct cmipci_switch_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 unsigned int val;
2356 int change;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002357 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 spin_lock_irq(&cm->reg_lock);
2360 if (args->ac3_sensitive && cm->mixer_insensitive) {
2361 /* ignored */
2362 spin_unlock_irq(&cm->reg_lock);
2363 return 0;
2364 }
2365 if (args->is_byte)
2366 val = inb(cm->iobase + args->reg);
2367 else
2368 val = snd_cmipci_read(cm, args->reg);
Timofei V. Bondarenko8c670712006-12-20 19:20:07 +01002369 change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
2370 args->mask_on : (args->mask & ~args->mask_on));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (change) {
2372 val &= ~args->mask;
2373 if (ucontrol->value.integer.value[0])
2374 val |= args->mask_on;
2375 else
2376 val |= (args->mask & ~args->mask_on);
2377 if (args->is_byte)
2378 outb((unsigned char)val, cm->iobase + args->reg);
2379 else
2380 snd_cmipci_write(cm, args->reg, val);
2381 }
2382 spin_unlock_irq(&cm->reg_lock);
2383 return change;
2384}
2385
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002386static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002389 struct cmipci_switch_args *args;
2390 args = (struct cmipci_switch_args *)kcontrol->private_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 snd_assert(args != NULL, return -EINVAL);
2392 return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
2393}
2394
2395#define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002396static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 .reg = xreg, \
2398 .mask = xmask, \
2399 .mask_on = xmask_on, \
2400 .is_byte = xis_byte, \
2401 .ac3_sensitive = xac3, \
2402}
2403
2404#define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
2405 DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
2406
2407#if 0 /* these will be controlled in pcm device */
2408DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
2409DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
2410#endif
2411DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
2412DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
2413DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
2414DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
2415DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
2416DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
2417DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
2418DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
2419// DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
2420DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
2421DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
2422/* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
2423DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
2424DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
2425#if CM_CH_PLAY == 1
2426DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
2427#else
2428DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
2429#endif
2430DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
Clemens Ladischa839a332007-09-17 09:35:46 +02002431// DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
2432// DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433// DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
2434DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
2435
2436#define DEFINE_SWITCH(sname, stype, sarg) \
2437{ .name = sname, \
2438 .iface = stype, \
2439 .info = snd_cmipci_uswitch_info, \
2440 .get = snd_cmipci_uswitch_get, \
2441 .put = snd_cmipci_uswitch_put, \
2442 .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
2443}
2444
2445#define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
2446#define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
2447
2448
2449/*
2450 * callbacks for spdif output switch
2451 * needs toggle two registers..
2452 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002453static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 int changed;
2457 changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2458 changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2459 return changed;
2460}
2461
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002462static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002465 struct cmipci *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int changed;
2467 changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
2468 changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
2469 if (changed) {
2470 if (ucontrol->value.integer.value[0]) {
2471 if (chip->spdif_playback_avail)
2472 snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2473 } else {
2474 if (chip->spdif_playback_avail)
2475 snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
2476 }
2477 }
2478 chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
2479 return changed;
2480}
2481
2482
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002483static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_info *uinfo)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002485{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002486 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002487 static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
2488 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2489 uinfo->count = 1;
2490 uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
2491 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2492 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2493 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2494 return 0;
2495}
2496
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002497static inline unsigned int get_line_in_mode(struct cmipci *cm)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002498{
2499 unsigned int val;
2500 if (cm->chip_version >= 39) {
2501 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
Clemens Ladischa839a332007-09-17 09:35:46 +02002502 if (val & (CM_CENTR2LIN | CM_BASE2LIN))
Takashi Iwai01d25d42005-04-11 16:58:24 +02002503 return 2;
2504 }
2505 val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
Clemens Ladischa839a332007-09-17 09:35:46 +02002506 if (val & CM_REAR2LIN)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002507 return 1;
2508 return 0;
2509}
2510
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002511static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002513{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002514 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002515
2516 spin_lock_irq(&cm->reg_lock);
2517 ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
2518 spin_unlock_irq(&cm->reg_lock);
2519 return 0;
2520}
2521
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002522static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002524{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002525 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002526 int change;
2527
2528 spin_lock_irq(&cm->reg_lock);
2529 if (ucontrol->value.enumerated.item[0] == 2)
Clemens Ladischa839a332007-09-17 09:35:46 +02002530 change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002531 else
Clemens Ladischa839a332007-09-17 09:35:46 +02002532 change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002533 if (ucontrol->value.enumerated.item[0] == 1)
Clemens Ladischa839a332007-09-17 09:35:46 +02002534 change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002535 else
Clemens Ladischa839a332007-09-17 09:35:46 +02002536 change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002537 spin_unlock_irq(&cm->reg_lock);
2538 return change;
2539}
2540
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002541static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_info *uinfo)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002543{
2544 static char *texts[2] = { "Mic-In", "Center/LFE Output" };
2545 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2546 uinfo->count = 1;
2547 uinfo->value.enumerated.items = 2;
2548 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2549 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2550 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2551 return 0;
2552}
2553
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002554static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002556{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002557 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002558 /* same bit as spdi_phase */
2559 spin_lock_irq(&cm->reg_lock);
2560 ucontrol->value.enumerated.item[0] =
2561 (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
2562 spin_unlock_irq(&cm->reg_lock);
2563 return 0;
2564}
2565
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002566static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai01d25d42005-04-11 16:58:24 +02002568{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002569 struct cmipci *cm = snd_kcontrol_chip(kcontrol);
Takashi Iwai01d25d42005-04-11 16:58:24 +02002570 int change;
2571
2572 spin_lock_irq(&cm->reg_lock);
2573 if (ucontrol->value.enumerated.item[0])
2574 change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2575 else
2576 change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
2577 spin_unlock_irq(&cm->reg_lock);
2578 return change;
2579}
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581/* both for CM8338/8738 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002582static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
Takashi Iwai01d25d42005-04-11 16:58:24 +02002584 {
2585 .name = "Line-In Mode",
2586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2587 .info = snd_cmipci_line_in_mode_info,
2588 .get = snd_cmipci_line_in_mode_get,
2589 .put = snd_cmipci_line_in_mode_put,
2590 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591};
2592
2593/* for non-multichannel chips */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002594static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
2596
2597/* only for CM8738 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002598static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#if 0 /* controlled in pcm device */
2600 DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
2601 DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
2602 DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
2603#endif
2604 // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
2605 { .name = "IEC958 Output Switch",
2606 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2607 .info = snd_cmipci_uswitch_info,
2608 .get = snd_cmipci_spdout_enable_get,
2609 .put = snd_cmipci_spdout_enable_put,
2610 },
2611 DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
2612 DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
2613 DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
2614// DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
2615 DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
2616 DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
2617};
2618
2619/* only for model 033/037 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002620static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
2622 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
2623 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
2624};
2625
2626/* only for model 039 or later */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002627static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
2629 DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
Takashi Iwai01d25d42005-04-11 16:58:24 +02002630 {
2631 .name = "Mic-In Mode",
2632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2633 .info = snd_cmipci_mic_in_mode_info,
2634 .get = snd_cmipci_mic_in_mode_get,
2635 .put = snd_cmipci_mic_in_mode_put,
2636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637};
2638
2639/* card control switches */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002640static struct snd_kcontrol_new snd_cmipci_control_switches[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 // DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
2642 DEFINE_CARD_SWITCH("Modem", modem),
2643};
2644
2645
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002646static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002648 struct snd_card *card;
2649 struct snd_kcontrol_new *sw;
2650 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 unsigned int idx;
2652 int err;
2653
2654 snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
2655
2656 card = cm->card;
2657
2658 strcpy(card->mixername, "CMedia PCI");
2659
2660 spin_lock_irq(&cm->reg_lock);
2661 snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
2662 spin_unlock_irq(&cm->reg_lock);
2663
2664 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
2665 if (cm->chip_version == 68) { // 8768 has no PCM volume
2666 if (!strcmp(snd_cmipci_mixers[idx].name,
2667 "PCM Playback Volume"))
2668 continue;
2669 }
2670 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
2671 return err;
2672 }
2673
2674 /* mixer switches */
2675 sw = snd_cmipci_mixer_switches;
2676 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
2677 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2678 if (err < 0)
2679 return err;
2680 }
2681 if (! cm->can_multi_ch) {
2682 err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
2683 if (err < 0)
2684 return err;
2685 }
2686 if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
2687 cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
2688 sw = snd_cmipci_8738_mixer_switches;
2689 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
2690 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2691 if (err < 0)
2692 return err;
2693 }
2694 if (cm->can_ac3_hw) {
2695 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
2696 return err;
2697 kctl->id.device = pcm_spdif_device;
2698 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
2699 return err;
2700 kctl->id.device = pcm_spdif_device;
2701 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
2702 return err;
2703 kctl->id.device = pcm_spdif_device;
2704 }
2705 if (cm->chip_version <= 37) {
2706 sw = snd_cmipci_old_mixer_switches;
2707 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
2708 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2709 if (err < 0)
2710 return err;
2711 }
2712 }
2713 }
2714 if (cm->chip_version >= 39) {
2715 sw = snd_cmipci_extra_mixer_switches;
2716 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
2717 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2718 if (err < 0)
2719 return err;
2720 }
2721 }
2722
2723 /* card switches */
2724 sw = snd_cmipci_control_switches;
2725 for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
2726 err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
2727 if (err < 0)
2728 return err;
2729 }
2730
2731 for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002732 struct snd_ctl_elem_id id;
2733 struct snd_kcontrol *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 memset(&id, 0, sizeof(id));
2735 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2736 strcpy(id.name, cm_saved_mixer[idx].name);
2737 if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
2738 cm->mixer_res_ctl[idx] = ctl;
2739 }
2740
2741 return 0;
2742}
2743
2744
2745/*
2746 * proc interface
2747 */
2748
2749#ifdef CONFIG_PROC_FS
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002750static void snd_cmipci_proc_read(struct snd_info_entry *entry,
2751 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002753 struct cmipci *cm = entry->private_data;
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002754 int i, v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002756 snd_iprintf(buffer, "%s\n", cm->card->longname);
2757 for (i = 0; i < 0x94; i++) {
2758 if (i == 0x28)
2759 i = 0x90;
2760 v = inb(cm->iobase + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (i % 4 == 0)
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002762 snd_iprintf(buffer, "\n%02x:", i);
2763 snd_iprintf(buffer, " %02x", v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
Clemens Ladisch54d030c2007-09-17 09:36:16 +02002765 snd_iprintf(buffer, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002768static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002770 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 if (! snd_card_proc_new(cm->card, "cmipci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002773 snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775#else /* !CONFIG_PROC_FS */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002776static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777#endif
2778
2779
Takashi Iwaif40b6892006-07-05 16:51:05 +02002780static struct pci_device_id snd_cmipci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2782 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2783 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2784 {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2785 {PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2786 {0,},
2787};
2788
2789
2790/*
2791 * check chip version and capabilities
2792 * driver name is modified according to the chip model
2793 */
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002794static void __devinit query_chip(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 unsigned int detect;
2797
2798 /* check reg 0Ch, bit 24-31 */
2799 detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
2800 if (! detect) {
2801 /* check reg 08h, bit 24-28 */
2802 detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002803 switch (detect) {
2804 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 cm->chip_version = 33;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (cm->do_soft_ac3)
2807 cm->can_ac3_sw = 1;
2808 else
2809 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002810 break;
Clemens Ladisch6935e682007-09-17 09:34:59 +02002811 case CM_CHIP_037:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 cm->chip_version = 37;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002814 break;
2815 default:
2816 cm->chip_version = 39;
2817 cm->can_ac3_hw = 1;
2818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
Clemens Ladisch133271f2007-08-27 09:20:31 +02002820 cm->max_channels = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 } else {
Clemens Ladisch133271f2007-08-27 09:20:31 +02002822 if (detect & CM_CHIP_039) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 cm->chip_version = 39;
2824 if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
2825 cm->max_channels = 6;
2826 else
2827 cm->max_channels = 4;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002828 } else if (detect & CM_CHIP_8768) {
2829 cm->chip_version = 68;
2830 cm->max_channels = 8;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01002831 cm->can_96k = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 } else {
Clemens Ladisch133271f2007-08-27 09:20:31 +02002833 cm->chip_version = 55;
2834 cm->max_channels = 6;
Timofei Bondarenko755c48a2007-10-31 17:36:20 +01002835 cm->can_96k = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Clemens Ladisch133271f2007-08-27 09:20:31 +02002837 cm->can_ac3_hw = 1;
Clemens Ladisch133271f2007-08-27 09:20:31 +02002838 cm->can_multi_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840}
2841
2842#ifdef SUPPORT_JOYSTICK
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002843static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
2846 struct gameport *gp;
2847 struct resource *r = NULL;
2848 int i, io_port = 0;
2849
2850 if (joystick_port[dev] == 0)
2851 return -ENODEV;
2852
2853 if (joystick_port[dev] == 1) { /* auto-detect */
2854 for (i = 0; ports[i]; i++) {
2855 io_port = ports[i];
2856 r = request_region(io_port, 1, "CMIPCI gameport");
2857 if (r)
2858 break;
2859 }
2860 } else {
2861 io_port = joystick_port[dev];
2862 r = request_region(io_port, 1, "CMIPCI gameport");
2863 }
2864
2865 if (!r) {
2866 printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
2867 return -EBUSY;
2868 }
2869
2870 cm->gameport = gp = gameport_allocate_port();
2871 if (!gp) {
2872 printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02002873 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return -ENOMEM;
2875 }
2876 gameport_set_name(gp, "C-Media Gameport");
2877 gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
2878 gameport_set_dev_parent(gp, &cm->pci->dev);
2879 gp->io = io_port;
2880 gameport_set_port_data(gp, r);
2881
2882 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2883
2884 gameport_register_port(cm->gameport);
2885
2886 return 0;
2887}
2888
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002889static void snd_cmipci_free_gameport(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 if (cm->gameport) {
2892 struct resource *r = gameport_get_port_data(cm->gameport);
2893
2894 gameport_unregister_port(cm->gameport);
2895 cm->gameport = NULL;
2896
2897 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
Takashi Iwaib1d57762005-10-10 11:56:31 +02002898 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900}
2901#else
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002902static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
2903static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904#endif
2905
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002906static int snd_cmipci_free(struct cmipci *cm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 if (cm->irq >= 0) {
2909 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2910 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
2911 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
2912 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
2913 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
2914 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
2915 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
2916
2917 /* reset mixer */
2918 snd_cmipci_mixer_write(cm, 0, 0);
2919
2920 synchronize_irq(cm->irq);
2921
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002922 free_irq(cm->irq, cm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 snd_cmipci_free_gameport(cm);
2926 pci_release_regions(cm->pci);
2927 pci_disable_device(cm->pci);
2928 kfree(cm);
2929 return 0;
2930}
2931
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002932static int snd_cmipci_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002934 struct cmipci *cm = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return snd_cmipci_free(cm);
2936}
2937
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002938static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
Clemens Ladisch5747e542005-09-14 08:33:46 +02002939{
2940 long iosynth;
2941 unsigned int val;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002942 struct snd_opl3 *opl3;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002943 int err;
2944
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002945 if (!fm_port)
2946 goto disable_fm;
2947
Clemens Ladischc78c9502007-09-03 09:55:49 +02002948 if (cm->chip_version >= 39) {
Clemens Ladisch45c41b42007-08-22 09:45:03 +02002949 /* first try FM regs in PCI port range */
2950 iosynth = cm->iobase + CM_REG_FM_PCI;
2951 err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
2952 OPL3_HW_OPL3, 1, &opl3);
2953 } else {
2954 err = -EIO;
2955 }
Clemens Ladisch5747e542005-09-14 08:33:46 +02002956 if (err < 0) {
2957 /* then try legacy ports */
2958 val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
2959 iosynth = fm_port;
2960 switch (iosynth) {
2961 case 0x3E8: val |= CM_FMSEL_3E8; break;
2962 case 0x3E0: val |= CM_FMSEL_3E0; break;
2963 case 0x3C8: val |= CM_FMSEL_3C8; break;
2964 case 0x388: val |= CM_FMSEL_388; break;
2965 default:
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002966 goto disable_fm;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002967 }
2968 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
2969 /* enable FM */
2970 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2971
2972 if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
2973 OPL3_HW_OPL3, 0, &opl3) < 0) {
2974 printk(KERN_ERR "cmipci: no OPL device at %#lx, "
2975 "skipping...\n", iosynth);
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002976 goto disable_fm;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002977 }
2978 }
2979 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
2980 printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
2981 return err;
2982 }
2983 return 0;
Takashi Iwai2f24d1592007-02-15 18:56:43 +01002984
2985 disable_fm:
2986 snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
2987 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
2988 return 0;
Clemens Ladisch5747e542005-09-14 08:33:46 +02002989}
2990
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002991static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
2992 int dev, struct cmipci **rcmipci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002994 struct cmipci *cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 int err;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01002996 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .dev_free = snd_cmipci_dev_free,
2998 };
Clemens Ladischd6426252007-08-27 09:22:31 +02002999 unsigned int val;
Clemens Ladisch5747e542005-09-14 08:33:46 +02003000 long iomidi;
Clemens Ladischc9116ae2007-08-24 09:18:04 +02003001 int integrated_midi = 0;
Clemens Ladischb7e054a2007-09-03 09:56:45 +02003002 char modelstr[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int pcm_index, pcm_spdif_index;
3004 static struct pci_device_id intel_82437vx[] = {
3005 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
3006 { },
3007 };
3008
3009 *rcmipci = NULL;
3010
3011 if ((err = pci_enable_device(pci)) < 0)
3012 return err;
3013
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003014 cm = kzalloc(sizeof(*cm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (cm == NULL) {
3016 pci_disable_device(pci);
3017 return -ENOMEM;
3018 }
3019
3020 spin_lock_init(&cm->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01003021 mutex_init(&cm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 cm->device = pci->device;
3023 cm->card = card;
3024 cm->pci = pci;
3025 cm->irq = -1;
3026 cm->channel[0].ch = 0;
3027 cm->channel[1].ch = 1;
3028 cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
3029
3030 if ((err = pci_request_regions(pci, card->driver)) < 0) {
3031 kfree(cm);
3032 pci_disable_device(pci);
3033 return err;
3034 }
3035 cm->iobase = pci_resource_start(pci, 0);
3036
Takashi Iwai2cbdb682005-11-17 15:03:13 +01003037 if (request_irq(pci->irq, snd_cmipci_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01003038 IRQF_SHARED, card->driver, cm)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02003039 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 snd_cmipci_free(cm);
3041 return -EBUSY;
3042 }
3043 cm->irq = pci->irq;
3044
3045 pci_set_master(cm->pci);
3046
3047 /*
3048 * check chip version, max channels and capabilities
3049 */
3050
3051 cm->chip_version = 0;
3052 cm->max_channels = 2;
3053 cm->do_soft_ac3 = soft_ac3[dev];
3054
3055 if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
3056 pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
3057 query_chip(cm);
3058 /* added -MCx suffix for chip supporting multi-channels */
3059 if (cm->can_multi_ch)
3060 sprintf(cm->card->driver + strlen(cm->card->driver),
3061 "-MC%d", cm->max_channels);
3062 else if (cm->can_ac3_sw)
3063 strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
3064
3065 cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3066 cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
3067
3068#if CM_CH_PLAY == 1
3069 cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
3070#else
3071 cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
3072#endif
3073
3074 /* initialize codec registers */
Clemens Ladisch3042ef72007-09-17 09:36:49 +02003075 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
3076 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
3078 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3079 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3080 snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
3081 snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
3082
3083 snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
3084 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
3085#if CM_CH_PLAY == 1
3086 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
3087#else
3088 snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
3089#endif
Clemens Ladisch4ee72712007-09-17 09:37:19 +02003090 if (cm->chip_version) {
3091 snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
3092 snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 /* Set Bus Master Request */
3095 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
3096
3097 /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
3098 switch (pci->device) {
3099 case PCI_DEVICE_ID_CMEDIA_CM8738:
3100 case PCI_DEVICE_ID_CMEDIA_CM8738B:
3101 if (!pci_dev_present(intel_82437vx))
3102 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
3103 break;
3104 default:
3105 break;
3106 }
3107
Clemens Ladischd6426252007-08-27 09:22:31 +02003108 if (cm->chip_version < 68) {
3109 val = pci->device < 0x110 ? 8338 : 8738;
Clemens Ladischd6426252007-08-27 09:22:31 +02003110 } else {
3111 switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
3112 case 0:
3113 val = 8769;
3114 break;
3115 case 2:
3116 val = 8762;
3117 break;
3118 default:
3119 switch ((pci->subsystem_vendor << 16) |
3120 pci->subsystem_device) {
3121 case 0x13f69761:
3122 case 0x584d3741:
3123 case 0x584d3751:
3124 case 0x584d3761:
3125 case 0x584d3771:
3126 case 0x72848384:
3127 val = 8770;
3128 break;
3129 default:
3130 val = 8768;
3131 break;
3132 }
3133 }
Clemens Ladischd6426252007-08-27 09:22:31 +02003134 }
Clemens Ladischb7e054a2007-09-03 09:56:45 +02003135 sprintf(card->shortname, "C-Media CMI%d", val);
3136 if (cm->chip_version < 68)
3137 sprintf(modelstr, " (model %d)", cm->chip_version);
3138 else
3139 modelstr[0] = '\0';
3140 sprintf(card->longname, "%s%s at %#lx, irq %i",
3141 card->shortname, modelstr, cm->iobase, cm->irq);
Clemens Ladisch1e02d6e2007-08-21 08:58:35 +02003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
3144 snd_cmipci_free(cm);
3145 return err;
3146 }
3147
Clemens Ladischc78c9502007-09-03 09:55:49 +02003148 if (cm->chip_version >= 39) {
Clemens Ladischc9116ae2007-08-24 09:18:04 +02003149 val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
3150 if (val != 0x00 && val != 0xff) {
3151 iomidi = cm->iobase + CM_REG_MPU_PCI;
3152 integrated_midi = 1;
3153 }
3154 }
3155 if (!integrated_midi) {
Clemens Ladischc78c9502007-09-03 09:55:49 +02003156 val = 0;
Clemens Ladisch5747e542005-09-14 08:33:46 +02003157 iomidi = mpu_port[dev];
3158 switch (iomidi) {
3159 case 0x320: val = CM_VMPU_320; break;
3160 case 0x310: val = CM_VMPU_310; break;
3161 case 0x300: val = CM_VMPU_300; break;
3162 case 0x330: val = CM_VMPU_330; break;
3163 default:
3164 iomidi = 0; break;
3165 }
3166 if (iomidi > 0) {
3167 snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
3168 /* enable UART */
3169 snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
Clemens Ladisch88039812007-09-03 09:56:23 +02003170 if (inb(iomidi + 1) == 0xff) {
3171 snd_printk(KERN_ERR "cannot enable MPU-401 port"
3172 " at %#lx\n", iomidi);
3173 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
3174 CM_UART_EN);
3175 iomidi = 0;
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 }
Clemens Ladisch5747e542005-09-14 08:33:46 +02003179
Clemens Ladisch45c41b42007-08-22 09:45:03 +02003180 if (cm->chip_version < 68) {
3181 err = snd_cmipci_create_fm(cm, fm_port[dev]);
3182 if (err < 0)
3183 return err;
3184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 /* reset mixer */
3187 snd_cmipci_mixer_write(cm, 0, 0);
3188
3189 snd_cmipci_proc_init(cm);
3190
3191 /* create pcm devices */
3192 pcm_index = pcm_spdif_index = 0;
3193 if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
3194 return err;
3195 pcm_index++;
Clemens Ladischb080ebb2007-08-27 09:21:02 +02003196 if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
3197 return err;
3198 pcm_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (cm->can_ac3_hw || cm->can_ac3_sw) {
3200 pcm_spdif_index = pcm_index;
3201 if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
3202 return err;
3203 }
3204
3205 /* create mixer interface & switches */
3206 if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
3207 return err;
3208
3209 if (iomidi > 0) {
3210 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
Takashi Iwai302e4c22006-05-23 13:24:30 +02003211 iomidi,
3212 (integrated_midi ?
3213 MPU401_INFO_INTEGRATED : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 cm->irq, 0, &cm->rmidi)) < 0) {
3215 printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
3216 }
3217 }
3218
3219#ifdef USE_VAR48KRATE
3220 for (val = 0; val < ARRAY_SIZE(rates); val++)
3221 snd_cmipci_set_pll(cm, rates[val], val);
3222
3223 /*
3224 * (Re-)Enable external switch spdo_48k
3225 */
3226 snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
3227#endif /* USE_VAR48KRATE */
3228
3229 if (snd_cmipci_create_gameport(cm, dev) < 0)
3230 snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
3231
3232 snd_card_set_dev(card, &pci->dev);
3233
3234 *rcmipci = cm;
3235 return 0;
3236}
3237
3238/*
3239 */
3240
3241MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
3242
3243static int __devinit snd_cmipci_probe(struct pci_dev *pci,
3244 const struct pci_device_id *pci_id)
3245{
3246 static int dev;
Takashi Iwai2cbdb682005-11-17 15:03:13 +01003247 struct snd_card *card;
3248 struct cmipci *cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 int err;
3250
3251 if (dev >= SNDRV_CARDS)
3252 return -ENODEV;
3253 if (! enable[dev]) {
3254 dev++;
3255 return -ENOENT;
3256 }
3257
3258 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
3259 if (card == NULL)
3260 return -ENOMEM;
3261
3262 switch (pci->device) {
3263 case PCI_DEVICE_ID_CMEDIA_CM8738:
3264 case PCI_DEVICE_ID_CMEDIA_CM8738B:
3265 strcpy(card->driver, "CMI8738");
3266 break;
3267 case PCI_DEVICE_ID_CMEDIA_CM8338A:
3268 case PCI_DEVICE_ID_CMEDIA_CM8338B:
3269 strcpy(card->driver, "CMI8338");
3270 break;
3271 default:
3272 strcpy(card->driver, "CMIPCI");
3273 break;
3274 }
3275
3276 if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
3277 snd_card_free(card);
3278 return err;
3279 }
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003280 card->private_data = cm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if ((err = snd_card_register(card)) < 0) {
3283 snd_card_free(card);
3284 return err;
3285 }
3286 pci_set_drvdata(pci, card);
3287 dev++;
3288 return 0;
3289
3290}
3291
3292static void __devexit snd_cmipci_remove(struct pci_dev *pci)
3293{
3294 snd_card_free(pci_get_drvdata(pci));
3295 pci_set_drvdata(pci, NULL);
3296}
3297
3298
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003299#ifdef CONFIG_PM
3300/*
3301 * power management
3302 */
3303static unsigned char saved_regs[] = {
3304 CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
3305 CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
3306 CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
3307 CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
3308 CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
3309};
3310
3311static unsigned char saved_mixers[] = {
3312 SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
3313 SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
3314 SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
3315 SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
3316 SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
3317 SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
3318 CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
3319 SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
3320};
3321
3322static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
3323{
3324 struct snd_card *card = pci_get_drvdata(pci);
3325 struct cmipci *cm = card->private_data;
3326 int i;
3327
3328 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3329
3330 snd_pcm_suspend_all(cm->pcm);
3331 snd_pcm_suspend_all(cm->pcm2);
3332 snd_pcm_suspend_all(cm->pcm_spdif);
3333
3334 /* save registers */
3335 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3336 cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3337 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3338 cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3339
3340 /* disable ints */
3341 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3342
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003343 pci_disable_device(pci);
3344 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02003345 pci_set_power_state(pci, pci_choose_state(pci, state));
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003346 return 0;
3347}
3348
3349static int snd_cmipci_resume(struct pci_dev *pci)
3350{
3351 struct snd_card *card = pci_get_drvdata(pci);
3352 struct cmipci *cm = card->private_data;
3353 int i;
3354
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003355 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02003356 pci_restore_state(pci);
3357 if (pci_enable_device(pci) < 0) {
3358 printk(KERN_ERR "cmipci: pci_enable_device failed, "
3359 "disabling device\n");
3360 snd_card_disconnect(card);
3361 return -EIO;
3362 }
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003363 pci_set_master(pci);
3364
3365 /* reset / initialize to a sane state */
3366 snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3367 snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3368 snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3369 snd_cmipci_mixer_write(cm, 0, 0);
3370
3371 /* restore registers */
3372 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3373 snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3374 for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3375 snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
3376
3377 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3378 return 0;
3379}
3380#endif /* CONFIG_PM */
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382static struct pci_driver driver = {
3383 .name = "C-Media PCI",
3384 .id_table = snd_cmipci_ids,
3385 .probe = snd_cmipci_probe,
3386 .remove = __devexit_p(snd_cmipci_remove),
Takashi Iwaicb60e5f2005-11-17 16:14:49 +01003387#ifdef CONFIG_PM
3388 .suspend = snd_cmipci_suspend,
3389 .resume = snd_cmipci_resume,
3390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391};
3392
3393static int __init alsa_card_cmipci_init(void)
3394{
Takashi Iwai01d25d42005-04-11 16:58:24 +02003395 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
3398static void __exit alsa_card_cmipci_exit(void)
3399{
3400 pci_unregister_driver(&driver);
3401}
3402
3403module_init(alsa_card_cmipci_init)
3404module_exit(alsa_card_cmipci_exit)