blob: 1e615c4d12f8fbaf19a50a671d5619db017105f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Ensoniq ES1370/ES1371 AudioPCI soundcard
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Thomas Sailer <sailer@ife.ee.ethz.ch>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Kurt J. Boschf31a31b2005-11-16 18:41:21 +010022/* Power-Management-Code ( CONFIG_PM )
23 * for ens1371 only ( FIXME )
24 * derived from cs4281.c, atiixp.c and via82xx.c
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020025 * using http://www.alsa-project.org/~tiwai/writing-an-alsa-driver/
Kurt J. Boschf31a31b2005-11-16 18:41:21 +010026 * by Kurt J. Bosch
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
30#include <linux/delay.h>
31#include <linux/interrupt.h>
32#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/slab.h>
35#include <linux/gameport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040036#include <linux/module.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010037#include <linux/mutex.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
42#include <sound/rawmidi.h>
43#ifdef CHIP1371
44#include <sound/ac97_codec.h>
45#else
46#include <sound/ak4531_codec.h>
47#endif
48#include <sound/initval.h>
49#include <sound/asoundef.h>
50
51#ifndef CHIP1371
52#undef CHIP1370
53#define CHIP1370
54#endif
55
56#ifdef CHIP1370
57#define DRIVER_NAME "ENS1370"
Takashi Iwai1fe4d422012-09-12 15:46:20 +020058#define CHIP_NAME "ES1370" /* it can be ENS but just to keep compatibility... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#else
60#define DRIVER_NAME "ENS1371"
Takashi Iwai1fe4d422012-09-12 15:46:20 +020061#define CHIP_NAME "ES1371"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
64
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020065MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Thomas Sailer <sailer@ife.ee.ethz.ch>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_LICENSE("GPL");
67#ifdef CHIP1370
68MODULE_DESCRIPTION("Ensoniq AudioPCI ES1370");
69MODULE_SUPPORTED_DEVICE("{{Ensoniq,AudioPCI-97 ES1370},"
70 "{Creative Labs,SB PCI64/128 (ES1370)}}");
71#endif
72#ifdef CHIP1371
73MODULE_DESCRIPTION("Ensoniq/Creative AudioPCI ES1371+");
74MODULE_SUPPORTED_DEVICE("{{Ensoniq,AudioPCI ES1371/73},"
75 "{Ensoniq,AudioPCI ES1373},"
76 "{Creative Labs,Ectiva EV1938},"
77 "{Creative Labs,SB PCI64/128 (ES1371/73)},"
78 "{Creative Labs,Vibra PCI128},"
79 "{Ectiva,EV1938}}");
80#endif
81
82#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
83#define SUPPORT_JOYSTICK
84#endif
85
86static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
87static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103088static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#ifdef SUPPORT_JOYSTICK
90#ifdef CHIP1371
91static int joystick_port[SNDRV_CARDS];
92#else
Rusty Russella67ff6a2011-12-15 13:49:36 +103093static bool joystick[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
95#endif
Clemens Ladisch156b2aa2005-12-07 09:07:25 +010096#ifdef CHIP1371
97static int spdif[SNDRV_CARDS];
98static int lineio[SNDRV_CARDS];
99#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101module_param_array(index, int, NULL, 0444);
102MODULE_PARM_DESC(index, "Index value for Ensoniq AudioPCI soundcard.");
103module_param_array(id, charp, NULL, 0444);
104MODULE_PARM_DESC(id, "ID string for Ensoniq AudioPCI soundcard.");
105module_param_array(enable, bool, NULL, 0444);
106MODULE_PARM_DESC(enable, "Enable Ensoniq AudioPCI soundcard.");
107#ifdef SUPPORT_JOYSTICK
108#ifdef CHIP1371
109module_param_array(joystick_port, int, NULL, 0444);
110MODULE_PARM_DESC(joystick_port, "Joystick port address.");
111#else
112module_param_array(joystick, bool, NULL, 0444);
113MODULE_PARM_DESC(joystick, "Enable joystick.");
114#endif
115#endif /* SUPPORT_JOYSTICK */
Clemens Ladisch156b2aa2005-12-07 09:07:25 +0100116#ifdef CHIP1371
117module_param_array(spdif, int, NULL, 0444);
118MODULE_PARM_DESC(spdif, "S/PDIF output (-1 = none, 0 = auto, 1 = force).");
119module_param_array(lineio, int, NULL, 0444);
120MODULE_PARM_DESC(lineio, "Line In to Rear Out (0 = auto, 1 = force).");
121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* ES1371 chip ID */
124/* This is a little confusing because all ES1371 compatible chips have the
125 same DEVICE_ID, the only thing differentiating them is the REV_ID field.
126 This is only significant if you want to enable features on the later parts.
127 Yes, I know it's stupid and why didn't we use the sub IDs?
128*/
129#define ES1371REV_ES1373_A 0x04
130#define ES1371REV_ES1373_B 0x06
131#define ES1371REV_CT5880_A 0x07
132#define CT5880REV_CT5880_C 0x02
133#define CT5880REV_CT5880_D 0x03 /* ??? -jk */
134#define CT5880REV_CT5880_E 0x04 /* mw */
135#define ES1371REV_ES1371_B 0x09
136#define EV1938REV_EV1938_A 0x00
137#define ES1371REV_ES1373_8 0x08
138
139/*
140 * Direct registers
141 */
142
143#define ES_REG(ensoniq, x) ((ensoniq)->port + ES_REG_##x)
144
145#define ES_REG_CONTROL 0x00 /* R/W: Interrupt/Chip select control register */
146#define ES_1370_ADC_STOP (1<<31) /* disable capture buffer transfers */
147#define ES_1370_XCTL1 (1<<30) /* general purpose output bit */
148#define ES_1373_BYPASS_P1 (1<<31) /* bypass SRC for PB1 */
149#define ES_1373_BYPASS_P2 (1<<30) /* bypass SRC for PB2 */
150#define ES_1373_BYPASS_R (1<<29) /* bypass SRC for REC */
151#define ES_1373_TEST_BIT (1<<28) /* should be set to 0 for normal operation */
152#define ES_1373_RECEN_B (1<<27) /* mix record with playback for I2S/SPDIF out */
153#define ES_1373_SPDIF_THRU (1<<26) /* 0 = SPDIF thru mode, 1 = SPDIF == dig out */
154#define ES_1371_JOY_ASEL(o) (((o)&0x03)<<24)/* joystick port mapping */
155#define ES_1371_JOY_ASELM (0x03<<24) /* mask for above */
156#define ES_1371_JOY_ASELI(i) (((i)>>24)&0x03)
157#define ES_1371_GPIO_IN(i) (((i)>>20)&0x0f)/* GPIO in [3:0] pins - R/O */
158#define ES_1370_PCLKDIVO(o) (((o)&0x1fff)<<16)/* clock divide ratio for DAC2 */
159#define ES_1370_PCLKDIVM ((0x1fff)<<16) /* mask for above */
160#define ES_1370_PCLKDIVI(i) (((i)>>16)&0x1fff)/* clock divide ratio for DAC2 */
161#define ES_1371_GPIO_OUT(o) (((o)&0x0f)<<16)/* GPIO out [3:0] pins - W/R */
162#define ES_1371_GPIO_OUTM (0x0f<<16) /* mask for above */
163#define ES_MSFMTSEL (1<<15) /* MPEG serial data format; 0 = SONY, 1 = I2S */
164#define ES_1370_M_SBB (1<<14) /* clock source for DAC - 0 = clock generator; 1 = MPEG clocks */
165#define ES_1371_SYNC_RES (1<<14) /* Warm AC97 reset */
166#define ES_1370_WTSRSEL(o) (((o)&0x03)<<12)/* fixed frequency clock for DAC1 */
167#define ES_1370_WTSRSELM (0x03<<12) /* mask for above */
168#define ES_1371_ADC_STOP (1<<13) /* disable CCB transfer capture information */
169#define ES_1371_PWR_INTRM (1<<12) /* power level change interrupts enable */
170#define ES_1370_DAC_SYNC (1<<11) /* DAC's are synchronous */
171#define ES_1371_M_CB (1<<11) /* capture clock source; 0 = AC'97 ADC; 1 = I2S */
172#define ES_CCB_INTRM (1<<10) /* CCB voice interrupts enable */
173#define ES_1370_M_CB (1<<9) /* capture clock source; 0 = ADC; 1 = MPEG */
174#define ES_1370_XCTL0 (1<<8) /* generap purpose output bit */
175#define ES_1371_PDLEV(o) (((o)&0x03)<<8) /* current power down level */
176#define ES_1371_PDLEVM (0x03<<8) /* mask for above */
177#define ES_BREQ (1<<7) /* memory bus request enable */
178#define ES_DAC1_EN (1<<6) /* DAC1 playback channel enable */
179#define ES_DAC2_EN (1<<5) /* DAC2 playback channel enable */
180#define ES_ADC_EN (1<<4) /* ADC capture channel enable */
181#define ES_UART_EN (1<<3) /* UART enable */
182#define ES_JYSTK_EN (1<<2) /* Joystick module enable */
183#define ES_1370_CDC_EN (1<<1) /* Codec interface enable */
184#define ES_1371_XTALCKDIS (1<<1) /* Xtal clock disable */
185#define ES_1370_SERR_DISABLE (1<<0) /* PCI serr signal disable */
186#define ES_1371_PCICLKDIS (1<<0) /* PCI clock disable */
187#define ES_REG_STATUS 0x04 /* R/O: Interrupt/Chip select status register */
188#define ES_INTR (1<<31) /* Interrupt is pending */
189#define ES_1371_ST_AC97_RST (1<<29) /* CT5880 AC'97 Reset bit */
190#define ES_1373_REAR_BIT27 (1<<27) /* rear bits: 000 - front, 010 - mirror, 101 - separate */
191#define ES_1373_REAR_BIT26 (1<<26)
192#define ES_1373_REAR_BIT24 (1<<24)
193#define ES_1373_GPIO_INT_EN(o)(((o)&0x0f)<<20)/* GPIO [3:0] pins - interrupt enable */
194#define ES_1373_SPDIF_EN (1<<18) /* SPDIF enable */
195#define ES_1373_SPDIF_TEST (1<<17) /* SPDIF test */
196#define ES_1371_TEST (1<<16) /* test ASIC */
197#define ES_1373_GPIO_INT(i) (((i)&0x0f)>>12)/* GPIO [3:0] pins - interrupt pending */
198#define ES_1370_CSTAT (1<<10) /* CODEC is busy or register write in progress */
199#define ES_1370_CBUSY (1<<9) /* CODEC is busy */
200#define ES_1370_CWRIP (1<<8) /* CODEC register write in progress */
201#define ES_1371_SYNC_ERR (1<<8) /* CODEC synchronization error occurred */
202#define ES_1371_VC(i) (((i)>>6)&0x03) /* voice code from CCB module */
203#define ES_1370_VC(i) (((i)>>5)&0x03) /* voice code from CCB module */
204#define ES_1371_MPWR (1<<5) /* power level interrupt pending */
205#define ES_MCCB (1<<4) /* CCB interrupt pending */
206#define ES_UART (1<<3) /* UART interrupt pending */
207#define ES_DAC1 (1<<2) /* DAC1 channel interrupt pending */
208#define ES_DAC2 (1<<1) /* DAC2 channel interrupt pending */
209#define ES_ADC (1<<0) /* ADC channel interrupt pending */
210#define ES_REG_UART_DATA 0x08 /* R/W: UART data register */
211#define ES_REG_UART_STATUS 0x09 /* R/O: UART status register */
212#define ES_RXINT (1<<7) /* RX interrupt occurred */
213#define ES_TXINT (1<<2) /* TX interrupt occurred */
214#define ES_TXRDY (1<<1) /* transmitter ready */
215#define ES_RXRDY (1<<0) /* receiver ready */
216#define ES_REG_UART_CONTROL 0x09 /* W/O: UART control register */
217#define ES_RXINTEN (1<<7) /* RX interrupt enable */
218#define ES_TXINTENO(o) (((o)&0x03)<<5) /* TX interrupt enable */
219#define ES_TXINTENM (0x03<<5) /* mask for above */
220#define ES_TXINTENI(i) (((i)>>5)&0x03)
221#define ES_CNTRL(o) (((o)&0x03)<<0) /* control */
222#define ES_CNTRLM (0x03<<0) /* mask for above */
223#define ES_REG_UART_RES 0x0a /* R/W: UART reserver register */
224#define ES_TEST_MODE (1<<0) /* test mode enabled */
225#define ES_REG_MEM_PAGE 0x0c /* R/W: Memory page register */
226#define ES_MEM_PAGEO(o) (((o)&0x0f)<<0) /* memory page select - out */
227#define ES_MEM_PAGEM (0x0f<<0) /* mask for above */
228#define ES_MEM_PAGEI(i) (((i)>>0)&0x0f) /* memory page select - in */
229#define ES_REG_1370_CODEC 0x10 /* W/O: Codec write register address */
230#define ES_1370_CODEC_WRITE(a,d) ((((a)&0xff)<<8)|(((d)&0xff)<<0))
231#define ES_REG_1371_CODEC 0x14 /* W/R: Codec Read/Write register address */
232#define ES_1371_CODEC_RDY (1<<31) /* codec ready */
233#define ES_1371_CODEC_WIP (1<<30) /* codec register access in progress */
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200234#define EV_1938_CODEC_MAGIC (1<<26)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define ES_1371_CODEC_PIRD (1<<23) /* codec read/write select register */
236#define ES_1371_CODEC_WRITE(a,d) ((((a)&0x7f)<<16)|(((d)&0xffff)<<0))
237#define ES_1371_CODEC_READS(a) ((((a)&0x7f)<<16)|ES_1371_CODEC_PIRD)
238#define ES_1371_CODEC_READ(i) (((i)>>0)&0xffff)
239
240#define ES_REG_1371_SMPRATE 0x10 /* W/R: Codec rate converter interface register */
241#define ES_1371_SRC_RAM_ADDRO(o) (((o)&0x7f)<<25)/* address of the sample rate converter */
242#define ES_1371_SRC_RAM_ADDRM (0x7f<<25) /* mask for above */
243#define ES_1371_SRC_RAM_ADDRI(i) (((i)>>25)&0x7f)/* address of the sample rate converter */
244#define ES_1371_SRC_RAM_WE (1<<24) /* R/W: read/write control for sample rate converter */
245#define ES_1371_SRC_RAM_BUSY (1<<23) /* R/O: sample rate memory is busy */
246#define ES_1371_SRC_DISABLE (1<<22) /* sample rate converter disable */
247#define ES_1371_DIS_P1 (1<<21) /* playback channel 1 accumulator update disable */
248#define ES_1371_DIS_P2 (1<<20) /* playback channel 1 accumulator update disable */
249#define ES_1371_DIS_R1 (1<<19) /* capture channel accumulator update disable */
250#define ES_1371_SRC_RAM_DATAO(o) (((o)&0xffff)<<0)/* current value of the sample rate converter */
251#define ES_1371_SRC_RAM_DATAM (0xffff<<0) /* mask for above */
252#define ES_1371_SRC_RAM_DATAI(i) (((i)>>0)&0xffff)/* current value of the sample rate converter */
253
254#define ES_REG_1371_LEGACY 0x18 /* W/R: Legacy control/status register */
255#define ES_1371_JFAST (1<<31) /* fast joystick timing */
256#define ES_1371_HIB (1<<30) /* host interrupt blocking enable */
257#define ES_1371_VSB (1<<29) /* SB; 0 = addr 0x220xH, 1 = 0x22FxH */
258#define ES_1371_VMPUO(o) (((o)&0x03)<<27)/* base register address; 0 = 0x320xH; 1 = 0x330xH; 2 = 0x340xH; 3 = 0x350xH */
259#define ES_1371_VMPUM (0x03<<27) /* mask for above */
260#define ES_1371_VMPUI(i) (((i)>>27)&0x03)/* base register address */
261#define ES_1371_VCDCO(o) (((o)&0x03)<<25)/* CODEC; 0 = 0x530xH; 1 = undefined; 2 = 0xe80xH; 3 = 0xF40xH */
262#define ES_1371_VCDCM (0x03<<25) /* mask for above */
263#define ES_1371_VCDCI(i) (((i)>>25)&0x03)/* CODEC address */
264#define ES_1371_FIRQ (1<<24) /* force an interrupt */
265#define ES_1371_SDMACAP (1<<23) /* enable event capture for slave DMA controller */
266#define ES_1371_SPICAP (1<<22) /* enable event capture for slave IRQ controller */
267#define ES_1371_MDMACAP (1<<21) /* enable event capture for master DMA controller */
268#define ES_1371_MPICAP (1<<20) /* enable event capture for master IRQ controller */
269#define ES_1371_ADCAP (1<<19) /* enable event capture for ADLIB register; 0x388xH */
270#define ES_1371_SVCAP (1<<18) /* enable event capture for SB registers */
271#define ES_1371_CDCCAP (1<<17) /* enable event capture for CODEC registers */
272#define ES_1371_BACAP (1<<16) /* enable event capture for SoundScape base address */
273#define ES_1371_EXI(i) (((i)>>8)&0x07) /* event number */
274#define ES_1371_AI(i) (((i)>>3)&0x1f) /* event significant I/O address */
275#define ES_1371_WR (1<<2) /* event capture; 0 = read; 1 = write */
276#define ES_1371_LEGINT (1<<0) /* interrupt for legacy events; 0 = interrupt did occur */
277
278#define ES_REG_CHANNEL_STATUS 0x1c /* R/W: first 32-bits from S/PDIF channel status block, es1373 */
279
280#define ES_REG_SERIAL 0x20 /* R/W: Serial interface control register */
281#define ES_1371_DAC_TEST (1<<22) /* DAC test mode enable */
282#define ES_P2_END_INCO(o) (((o)&0x07)<<19)/* binary offset value to increment / loop end */
283#define ES_P2_END_INCM (0x07<<19) /* mask for above */
284#define ES_P2_END_INCI(i) (((i)>>16)&0x07)/* binary offset value to increment / loop end */
285#define ES_P2_ST_INCO(o) (((o)&0x07)<<16)/* binary offset value to increment / start */
286#define ES_P2_ST_INCM (0x07<<16) /* mask for above */
287#define ES_P2_ST_INCI(i) (((i)<<16)&0x07)/* binary offset value to increment / start */
288#define ES_R1_LOOP_SEL (1<<15) /* ADC; 0 - loop mode; 1 = stop mode */
289#define ES_P2_LOOP_SEL (1<<14) /* DAC2; 0 - loop mode; 1 = stop mode */
290#define ES_P1_LOOP_SEL (1<<13) /* DAC1; 0 - loop mode; 1 = stop mode */
291#define ES_P2_PAUSE (1<<12) /* DAC2; 0 - play mode; 1 = pause mode */
292#define ES_P1_PAUSE (1<<11) /* DAC1; 0 - play mode; 1 = pause mode */
293#define ES_R1_INT_EN (1<<10) /* ADC interrupt enable */
294#define ES_P2_INT_EN (1<<9) /* DAC2 interrupt enable */
295#define ES_P1_INT_EN (1<<8) /* DAC1 interrupt enable */
296#define ES_P1_SCT_RLD (1<<7) /* force sample counter reload for DAC1 */
297#define ES_P2_DAC_SEN (1<<6) /* when stop mode: 0 - DAC2 play back zeros; 1 = DAC2 play back last sample */
298#define ES_R1_MODEO(o) (((o)&0x03)<<4) /* ADC mode; 0 = 8-bit mono; 1 = 8-bit stereo; 2 = 16-bit mono; 3 = 16-bit stereo */
299#define ES_R1_MODEM (0x03<<4) /* mask for above */
300#define ES_R1_MODEI(i) (((i)>>4)&0x03)
301#define ES_P2_MODEO(o) (((o)&0x03)<<2) /* DAC2 mode; -- '' -- */
302#define ES_P2_MODEM (0x03<<2) /* mask for above */
303#define ES_P2_MODEI(i) (((i)>>2)&0x03)
304#define ES_P1_MODEO(o) (((o)&0x03)<<0) /* DAC1 mode; -- '' -- */
305#define ES_P1_MODEM (0x03<<0) /* mask for above */
306#define ES_P1_MODEI(i) (((i)>>0)&0x03)
307
308#define ES_REG_DAC1_COUNT 0x24 /* R/W: DAC1 sample count register */
309#define ES_REG_DAC2_COUNT 0x28 /* R/W: DAC2 sample count register */
310#define ES_REG_ADC_COUNT 0x2c /* R/W: ADC sample count register */
311#define ES_REG_CURR_COUNT(i) (((i)>>16)&0xffff)
312#define ES_REG_COUNTO(o) (((o)&0xffff)<<0)
313#define ES_REG_COUNTM (0xffff<<0)
314#define ES_REG_COUNTI(i) (((i)>>0)&0xffff)
315
316#define ES_REG_DAC1_FRAME 0x30 /* R/W: PAGE 0x0c; DAC1 frame address */
317#define ES_REG_DAC1_SIZE 0x34 /* R/W: PAGE 0x0c; DAC1 frame size */
318#define ES_REG_DAC2_FRAME 0x38 /* R/W: PAGE 0x0c; DAC2 frame address */
319#define ES_REG_DAC2_SIZE 0x3c /* R/W: PAGE 0x0c; DAC2 frame size */
320#define ES_REG_ADC_FRAME 0x30 /* R/W: PAGE 0x0d; ADC frame address */
321#define ES_REG_ADC_SIZE 0x34 /* R/W: PAGE 0x0d; ADC frame size */
322#define ES_REG_FCURR_COUNTO(o) (((o)&0xffff)<<16)
323#define ES_REG_FCURR_COUNTM (0xffff<<16)
324#define ES_REG_FCURR_COUNTI(i) (((i)>>14)&0x3fffc)
325#define ES_REG_FSIZEO(o) (((o)&0xffff)<<0)
326#define ES_REG_FSIZEM (0xffff<<0)
327#define ES_REG_FSIZEI(i) (((i)>>0)&0xffff)
328#define ES_REG_PHANTOM_FRAME 0x38 /* R/W: PAGE 0x0d: phantom frame address */
329#define ES_REG_PHANTOM_COUNT 0x3c /* R/W: PAGE 0x0d: phantom frame count */
330
331#define ES_REG_UART_FIFO 0x30 /* R/W: PAGE 0x0e; UART FIFO register */
332#define ES_REG_UF_VALID (1<<8)
333#define ES_REG_UF_BYTEO(o) (((o)&0xff)<<0)
334#define ES_REG_UF_BYTEM (0xff<<0)
335#define ES_REG_UF_BYTEI(i) (((i)>>0)&0xff)
336
337
338/*
339 * Pages
340 */
341
342#define ES_PAGE_DAC 0x0c
343#define ES_PAGE_ADC 0x0d
344#define ES_PAGE_UART 0x0e
345#define ES_PAGE_UART1 0x0f
346
347/*
348 * Sample rate converter addresses
349 */
350
351#define ES_SMPREG_DAC1 0x70
352#define ES_SMPREG_DAC2 0x74
353#define ES_SMPREG_ADC 0x78
354#define ES_SMPREG_VOL_ADC 0x6c
355#define ES_SMPREG_VOL_DAC1 0x7c
356#define ES_SMPREG_VOL_DAC2 0x7e
357#define ES_SMPREG_TRUNC_N 0x00
358#define ES_SMPREG_INT_REGS 0x01
359#define ES_SMPREG_ACCUM_FRAC 0x02
360#define ES_SMPREG_VFREQ_FRAC 0x03
361
362/*
363 * Some contants
364 */
365
366#define ES_1370_SRCLOCK 1411200
367#define ES_1370_SRTODIV(x) (ES_1370_SRCLOCK/(x)-2)
368
369/*
370 * Open modes
371 */
372
373#define ES_MODE_PLAY1 0x0001
374#define ES_MODE_PLAY2 0x0002
375#define ES_MODE_CAPTURE 0x0004
376
377#define ES_MODE_OUTPUT 0x0001 /* for MIDI */
378#define ES_MODE_INPUT 0x0002 /* for MIDI */
379
380/*
381
382 */
383
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100384struct ensoniq {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spinlock_t reg_lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100386 struct mutex src_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 int irq;
389
390 unsigned long playback1size;
391 unsigned long playback2size;
392 unsigned long capture3size;
393
394 unsigned long port;
395 unsigned int mode;
396 unsigned int uartm; /* UART mode */
397
398 unsigned int ctrl; /* control register */
399 unsigned int sctrl; /* serial control register */
400 unsigned int cssr; /* control status register */
401 unsigned int uartc; /* uart control register */
402 unsigned int rev; /* chip revision */
403
404 union {
405#ifdef CHIP1371
406 struct {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100407 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 } es1371;
409#else
410 struct {
411 int pclkdiv_lock;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100412 struct snd_ak4531 *ak4531;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 } es1370;
414#endif
415 } u;
416
417 struct pci_dev *pci;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100418 struct snd_card *card;
419 struct snd_pcm *pcm1; /* DAC1/ADC PCM */
420 struct snd_pcm *pcm2; /* DAC2 PCM */
421 struct snd_pcm_substream *playback1_substream;
422 struct snd_pcm_substream *playback2_substream;
423 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int p1_dma_size;
425 unsigned int p2_dma_size;
426 unsigned int c_dma_size;
427 unsigned int p1_period_size;
428 unsigned int p2_period_size;
429 unsigned int c_period_size;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100430 struct snd_rawmidi *rmidi;
431 struct snd_rawmidi_substream *midi_input;
432 struct snd_rawmidi_substream *midi_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 unsigned int spdif;
435 unsigned int spdif_default;
436 unsigned int spdif_stream;
437
438#ifdef CHIP1370
439 struct snd_dma_buffer dma_bug;
440#endif
441
442#ifdef SUPPORT_JOYSTICK
443 struct gameport *gameport;
444#endif
445};
446
David Howells7d12e782006-10-05 14:55:46 +0100447static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200449static DEFINE_PCI_DEVICE_TABLE(snd_audiopci_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef CHIP1370
Joe Perches28d27aa2009-06-24 22:13:35 -0700451 { PCI_VDEVICE(ENSONIQ, 0x5000), 0, }, /* ES1370 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#endif
453#ifdef CHIP1371
Joe Perches28d27aa2009-06-24 22:13:35 -0700454 { PCI_VDEVICE(ENSONIQ, 0x1371), 0, }, /* ES1371 */
455 { PCI_VDEVICE(ENSONIQ, 0x5880), 0, }, /* ES1373 - CT5880 */
Joe Perches0d7392e2009-06-24 23:18:02 -0700456 { PCI_VDEVICE(ECTIVA, 0x8938), 0, }, /* Ectiva EV1938 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
458 { 0, }
459};
460
461MODULE_DEVICE_TABLE(pci, snd_audiopci_ids);
462
463/*
464 * constants
465 */
466
467#define POLL_COUNT 0xa000
468
469#ifdef CHIP1370
470static unsigned int snd_es1370_fixed_rates[] =
471 {5512, 11025, 22050, 44100};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100472static struct snd_pcm_hw_constraint_list snd_es1370_hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .count = 4,
474 .list = snd_es1370_fixed_rates,
475 .mask = 0,
476};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100477static struct snd_ratnum es1370_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 .num = ES_1370_SRCLOCK,
479 .den_min = 29,
480 .den_max = 353,
481 .den_step = 1,
482};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100483static struct snd_pcm_hw_constraint_ratnums snd_es1370_hw_constraints_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .nrats = 1,
485 .rats = &es1370_clock,
486};
487#else
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100488static struct snd_ratden es1371_dac_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 .num_min = 3000 * (1 << 15),
490 .num_max = 48000 * (1 << 15),
491 .num_step = 3000,
492 .den = 1 << 15,
493};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100494static struct snd_pcm_hw_constraint_ratdens snd_es1371_hw_constraints_dac_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .nrats = 1,
496 .rats = &es1371_dac_clock,
497};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100498static struct snd_ratnum es1371_adc_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .num = 48000 << 15,
500 .den_min = 32768,
501 .den_max = 393216,
502 .den_step = 1,
503};
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100504static struct snd_pcm_hw_constraint_ratnums snd_es1371_hw_constraints_adc_clock = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .nrats = 1,
506 .rats = &es1371_adc_clock,
507};
508#endif
509static const unsigned int snd_ensoniq_sample_shift[] =
510 {0, 1, 1, 2};
511
512/*
513 * common I/O routines
514 */
515
516#ifdef CHIP1371
517
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100518static unsigned int snd_es1371_wait_src_ready(struct ensoniq * ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 unsigned int t, r = 0;
521
522 for (t = 0; t < POLL_COUNT; t++) {
523 r = inl(ES_REG(ensoniq, 1371_SMPRATE));
524 if ((r & ES_1371_SRC_RAM_BUSY) == 0)
525 return r;
526 cond_resched();
527 }
Rene Herman810fd3f2008-07-17 09:22:29 +0200528 snd_printk(KERN_ERR "wait src ready timeout 0x%lx [0x%x]\n",
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100529 ES_REG(ensoniq, 1371_SMPRATE), r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
531}
532
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100533static unsigned int snd_es1371_src_read(struct ensoniq * ensoniq, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 unsigned int temp, i, orig, r;
536
537 /* wait for ready */
538 temp = orig = snd_es1371_wait_src_ready(ensoniq);
539
540 /* expose the SRC state bits */
541 r = temp & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
542 ES_1371_DIS_P2 | ES_1371_DIS_R1);
543 r |= ES_1371_SRC_RAM_ADDRO(reg) | 0x10000;
544 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
545
546 /* now, wait for busy and the correct time to read */
547 temp = snd_es1371_wait_src_ready(ensoniq);
548
549 if ((temp & 0x00870000) != 0x00010000) {
550 /* wait for the right state */
551 for (i = 0; i < POLL_COUNT; i++) {
552 temp = inl(ES_REG(ensoniq, 1371_SMPRATE));
553 if ((temp & 0x00870000) == 0x00010000)
554 break;
555 }
556 }
557
558 /* hide the state bits */
559 r = orig & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
560 ES_1371_DIS_P2 | ES_1371_DIS_R1);
561 r |= ES_1371_SRC_RAM_ADDRO(reg);
562 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
563
564 return temp;
565}
566
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100567static void snd_es1371_src_write(struct ensoniq * ensoniq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned short reg, unsigned short data)
569{
570 unsigned int r;
571
572 r = snd_es1371_wait_src_ready(ensoniq) &
573 (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
574 ES_1371_DIS_P2 | ES_1371_DIS_R1);
575 r |= ES_1371_SRC_RAM_ADDRO(reg) | ES_1371_SRC_RAM_DATAO(data);
576 outl(r | ES_1371_SRC_RAM_WE, ES_REG(ensoniq, 1371_SMPRATE));
577}
578
579#endif /* CHIP1371 */
580
581#ifdef CHIP1370
582
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100583static void snd_es1370_codec_write(struct snd_ak4531 *ak4531,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned short reg, unsigned short val)
585{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100586 struct ensoniq *ensoniq = ak4531->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned long end_time = jiffies + HZ / 10;
588
589#if 0
Takashi Iwaiee419652009-02-05 16:11:31 +0100590 printk(KERN_DEBUG
591 "CODEC WRITE: reg = 0x%x, val = 0x%x (0x%x), creg = 0x%x\n",
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100592 reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
594 do {
595 if (!(inl(ES_REG(ensoniq, STATUS)) & ES_1370_CSTAT)) {
596 outw(ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
597 return;
598 }
Nishanth Aravamudan8433a502005-10-24 15:02:37 +0200599 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } while (time_after(end_time, jiffies));
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100601 snd_printk(KERN_ERR "codec write timeout, status = 0x%x\n",
602 inl(ES_REG(ensoniq, STATUS)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605#endif /* CHIP1370 */
606
607#ifdef CHIP1371
608
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200609static inline bool is_ev1938(struct ensoniq *ensoniq)
610{
611 return ensoniq->pci->device == 0x8938;
612}
613
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100614static void snd_es1371_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned short reg, unsigned short val)
616{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100617 struct ensoniq *ensoniq = ac97->private_data;
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200618 unsigned int t, x, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200620 flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
Ingo Molnar62932df2006-01-16 16:34:20 +0100621 mutex_lock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 for (t = 0; t < POLL_COUNT; t++) {
623 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
624 /* save the current state for latter */
625 x = snd_es1371_wait_src_ready(ensoniq);
626 outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
627 ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
628 ES_REG(ensoniq, 1371_SMPRATE));
629 /* wait for not busy (state 0) first to avoid
630 transition states */
631 for (t = 0; t < POLL_COUNT; t++) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100632 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
633 0x00000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635 }
636 /* wait for a SAFE time to write addr/data and then do it, dammit */
637 for (t = 0; t < POLL_COUNT; t++) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100638 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
639 0x00010000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 }
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200642 outl(ES_1371_CODEC_WRITE(reg, val) | flag,
643 ES_REG(ensoniq, 1371_CODEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* restore SRC reg */
645 snd_es1371_wait_src_ready(ensoniq);
646 outl(x, ES_REG(ensoniq, 1371_SMPRATE));
Ingo Molnar62932df2006-01-16 16:34:20 +0100647 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
649 }
650 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100651 mutex_unlock(&ensoniq->src_mutex);
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100652 snd_printk(KERN_ERR "codec write timeout at 0x%lx [0x%x]\n",
653 ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100656static unsigned short snd_es1371_codec_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned short reg)
658{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100659 struct ensoniq *ensoniq = ac97->private_data;
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200660 unsigned int t, x, flag, fail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200662 flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 __again:
Ingo Molnar62932df2006-01-16 16:34:20 +0100664 mutex_lock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 for (t = 0; t < POLL_COUNT; t++) {
666 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
667 /* save the current state for latter */
668 x = snd_es1371_wait_src_ready(ensoniq);
669 outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
670 ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
671 ES_REG(ensoniq, 1371_SMPRATE));
672 /* wait for not busy (state 0) first to avoid
673 transition states */
674 for (t = 0; t < POLL_COUNT; t++) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100675 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
676 0x00000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 }
679 /* wait for a SAFE time to write addr/data and then do it, dammit */
680 for (t = 0; t < POLL_COUNT; t++) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100681 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
682 0x00010000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 }
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200685 outl(ES_1371_CODEC_READS(reg) | flag,
686 ES_REG(ensoniq, 1371_CODEC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* restore SRC reg */
688 snd_es1371_wait_src_ready(ensoniq);
689 outl(x, ES_REG(ensoniq, 1371_SMPRATE));
690 /* wait for WIP again */
691 for (t = 0; t < POLL_COUNT; t++) {
692 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP))
693 break;
694 }
695 /* now wait for the stinkin' data (RDY) */
696 for (t = 0; t < POLL_COUNT; t++) {
697 if ((x = inl(ES_REG(ensoniq, 1371_CODEC))) & ES_1371_CODEC_RDY) {
Clemens Ladisch6ebb8a42011-03-30 08:24:25 +0200698 if (is_ev1938(ensoniq)) {
699 for (t = 0; t < 100; t++)
700 inl(ES_REG(ensoniq, CONTROL));
701 x = inl(ES_REG(ensoniq, 1371_CODEC));
702 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100703 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return ES_1371_CODEC_READ(x);
705 }
706 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100707 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (++fail > 10) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100709 snd_printk(KERN_ERR "codec read timeout (final) "
710 "at 0x%lx, reg = 0x%x [0x%x]\n",
711 ES_REG(ensoniq, 1371_CODEC), reg,
712 inl(ES_REG(ensoniq, 1371_CODEC)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714 }
715 goto __again;
716 }
717 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100718 mutex_unlock(&ensoniq->src_mutex);
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100719 snd_printk(KERN_ERR "es1371: codec read timeout at 0x%lx [0x%x]\n",
720 ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
723
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100724static void snd_es1371_codec_wait(struct snd_ac97 *ac97)
Jaroslav Kysela072c0112005-07-09 10:07:55 +0200725{
726 msleep(750);
727 snd_es1371_codec_read(ac97, AC97_RESET);
728 snd_es1371_codec_read(ac97, AC97_VENDOR_ID1);
729 snd_es1371_codec_read(ac97, AC97_VENDOR_ID2);
730 msleep(50);
731}
732
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100733static void snd_es1371_adc_rate(struct ensoniq * ensoniq, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 unsigned int n, truncm, freq, result;
736
Ingo Molnar62932df2006-01-16 16:34:20 +0100737 mutex_lock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 n = rate / 3000;
739 if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
740 n--;
741 truncm = (21 * n - 1) | 1;
742 freq = ((48000UL << 15) / rate) * n;
743 result = (48000UL << 15) / (freq / n);
744 if (rate >= 24000) {
745 if (truncm > 239)
746 truncm = 239;
747 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
748 (((239 - truncm) >> 1) << 9) | (n << 4));
749 } else {
750 if (truncm > 119)
751 truncm = 119;
752 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
753 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
754 }
755 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100756 (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC +
757 ES_SMPREG_INT_REGS) & 0x00ff) |
758 ((freq >> 5) & 0xfc00));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
760 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
761 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
Ingo Molnar62932df2006-01-16 16:34:20 +0100762 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100765static void snd_es1371_dac1_rate(struct ensoniq * ensoniq, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 unsigned int freq, r;
768
Ingo Molnar62932df2006-01-16 16:34:20 +0100769 mutex_lock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 freq = ((rate << 15) + 1500) / 3000;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100771 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
772 ES_1371_DIS_P2 | ES_1371_DIS_R1)) |
773 ES_1371_DIS_P1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
775 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100776 (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 +
777 ES_SMPREG_INT_REGS) & 0x00ff) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 ((freq >> 5) & 0xfc00));
779 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100780 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
781 ES_1371_DIS_P2 | ES_1371_DIS_R1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
Ingo Molnar62932df2006-01-16 16:34:20 +0100783 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100786static void snd_es1371_dac2_rate(struct ensoniq * ensoniq, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 unsigned int freq, r;
789
Ingo Molnar62932df2006-01-16 16:34:20 +0100790 mutex_lock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 freq = ((rate << 15) + 1500) / 3000;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100792 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
793 ES_1371_DIS_P1 | ES_1371_DIS_R1)) |
794 ES_1371_DIS_P2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
796 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100797 (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 +
798 ES_SMPREG_INT_REGS) & 0x00ff) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ((freq >> 5) & 0xfc00));
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100800 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC,
801 freq & 0x7fff);
802 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
803 ES_1371_DIS_P1 | ES_1371_DIS_R1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
Ingo Molnar62932df2006-01-16 16:34:20 +0100805 mutex_unlock(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808#endif /* CHIP1371 */
809
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100810static int snd_ensoniq_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100812 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 switch (cmd) {
814 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
815 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
816 {
817 unsigned int what = 0;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100818 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +0100819 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (s == ensoniq->playback1_substream) {
821 what |= ES_P1_PAUSE;
822 snd_pcm_trigger_done(s, substream);
823 } else if (s == ensoniq->playback2_substream) {
824 what |= ES_P2_PAUSE;
825 snd_pcm_trigger_done(s, substream);
826 } else if (s == ensoniq->capture_substream)
827 return -EINVAL;
828 }
829 spin_lock(&ensoniq->reg_lock);
830 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
831 ensoniq->sctrl |= what;
832 else
833 ensoniq->sctrl &= ~what;
834 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
835 spin_unlock(&ensoniq->reg_lock);
836 break;
837 }
838 case SNDRV_PCM_TRIGGER_START:
839 case SNDRV_PCM_TRIGGER_STOP:
840 {
841 unsigned int what = 0;
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100842 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +0100843 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (s == ensoniq->playback1_substream) {
845 what |= ES_DAC1_EN;
846 snd_pcm_trigger_done(s, substream);
847 } else if (s == ensoniq->playback2_substream) {
848 what |= ES_DAC2_EN;
849 snd_pcm_trigger_done(s, substream);
850 } else if (s == ensoniq->capture_substream) {
851 what |= ES_ADC_EN;
852 snd_pcm_trigger_done(s, substream);
853 }
854 }
855 spin_lock(&ensoniq->reg_lock);
856 if (cmd == SNDRV_PCM_TRIGGER_START)
857 ensoniq->ctrl |= what;
858 else
859 ensoniq->ctrl &= ~what;
860 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
861 spin_unlock(&ensoniq->reg_lock);
862 break;
863 }
864 default:
865 return -EINVAL;
866 }
867 return 0;
868}
869
870/*
871 * PCM part
872 */
873
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100874static int snd_ensoniq_hw_params(struct snd_pcm_substream *substream,
875 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
878}
879
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100880static int snd_ensoniq_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 return snd_pcm_lib_free_pages(substream);
883}
884
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100885static int snd_ensoniq_playback1_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100887 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
888 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int mode = 0;
890
891 ensoniq->p1_dma_size = snd_pcm_lib_buffer_bytes(substream);
892 ensoniq->p1_period_size = snd_pcm_lib_period_bytes(substream);
893 if (snd_pcm_format_width(runtime->format) == 16)
894 mode |= 0x02;
895 if (runtime->channels > 1)
896 mode |= 0x01;
897 spin_lock_irq(&ensoniq->reg_lock);
898 ensoniq->ctrl &= ~ES_DAC1_EN;
899#ifdef CHIP1371
900 /* 48k doesn't need SRC (it breaks AC3-passthru) */
901 if (runtime->rate == 48000)
902 ensoniq->ctrl |= ES_1373_BYPASS_P1;
903 else
904 ensoniq->ctrl &= ~ES_1373_BYPASS_P1;
905#endif
906 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
907 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
908 outl(runtime->dma_addr, ES_REG(ensoniq, DAC1_FRAME));
909 outl((ensoniq->p1_dma_size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
910 ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
911 ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
912 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100913 outl((ensoniq->p1_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
914 ES_REG(ensoniq, DAC1_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#ifdef CHIP1370
916 ensoniq->ctrl &= ~ES_1370_WTSRSELM;
917 switch (runtime->rate) {
918 case 5512: ensoniq->ctrl |= ES_1370_WTSRSEL(0); break;
919 case 11025: ensoniq->ctrl |= ES_1370_WTSRSEL(1); break;
920 case 22050: ensoniq->ctrl |= ES_1370_WTSRSEL(2); break;
921 case 44100: ensoniq->ctrl |= ES_1370_WTSRSEL(3); break;
922 default: snd_BUG();
923 }
924#endif
925 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
926 spin_unlock_irq(&ensoniq->reg_lock);
927#ifndef CHIP1370
928 snd_es1371_dac1_rate(ensoniq, runtime->rate);
929#endif
930 return 0;
931}
932
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100933static int snd_ensoniq_playback2_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100935 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
936 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 unsigned int mode = 0;
938
939 ensoniq->p2_dma_size = snd_pcm_lib_buffer_bytes(substream);
940 ensoniq->p2_period_size = snd_pcm_lib_period_bytes(substream);
941 if (snd_pcm_format_width(runtime->format) == 16)
942 mode |= 0x02;
943 if (runtime->channels > 1)
944 mode |= 0x01;
945 spin_lock_irq(&ensoniq->reg_lock);
946 ensoniq->ctrl &= ~ES_DAC2_EN;
947 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
948 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
949 outl(runtime->dma_addr, ES_REG(ensoniq, DAC2_FRAME));
950 outl((ensoniq->p2_dma_size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
951 ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
952 ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
953 ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
954 ES_P2_END_INCO(mode & 2 ? 2 : 1) | ES_P2_ST_INCO(0);
955 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100956 outl((ensoniq->p2_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
957 ES_REG(ensoniq, DAC2_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#ifdef CHIP1370
959 if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_CAPTURE)) {
960 ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
961 ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
962 ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
963 }
964#endif
965 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
966 spin_unlock_irq(&ensoniq->reg_lock);
967#ifndef CHIP1370
968 snd_es1371_dac2_rate(ensoniq, runtime->rate);
969#endif
970 return 0;
971}
972
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100973static int snd_ensoniq_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100975 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
976 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned int mode = 0;
978
979 ensoniq->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
980 ensoniq->c_period_size = snd_pcm_lib_period_bytes(substream);
981 if (snd_pcm_format_width(runtime->format) == 16)
982 mode |= 0x02;
983 if (runtime->channels > 1)
984 mode |= 0x01;
985 spin_lock_irq(&ensoniq->reg_lock);
986 ensoniq->ctrl &= ~ES_ADC_EN;
987 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
988 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
989 outl(runtime->dma_addr, ES_REG(ensoniq, ADC_FRAME));
990 outl((ensoniq->c_dma_size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
991 ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
992 ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
993 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
Takashi Iwaieb3414b2005-11-17 15:03:46 +0100994 outl((ensoniq->c_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
995 ES_REG(ensoniq, ADC_COUNT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#ifdef CHIP1370
997 if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_PLAY2)) {
998 ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
999 ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
1000 ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
1001 }
1002#endif
1003 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1004 spin_unlock_irq(&ensoniq->reg_lock);
1005#ifndef CHIP1370
1006 snd_es1371_adc_rate(ensoniq, runtime->rate);
1007#endif
1008 return 0;
1009}
1010
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001011static snd_pcm_uframes_t snd_ensoniq_playback1_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001013 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 size_t ptr;
1015
1016 spin_lock(&ensoniq->reg_lock);
1017 if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC1_EN) {
1018 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
1019 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC1_SIZE)));
1020 ptr = bytes_to_frames(substream->runtime, ptr);
1021 } else {
1022 ptr = 0;
1023 }
1024 spin_unlock(&ensoniq->reg_lock);
1025 return ptr;
1026}
1027
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001028static snd_pcm_uframes_t snd_ensoniq_playback2_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001030 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 size_t ptr;
1032
1033 spin_lock(&ensoniq->reg_lock);
1034 if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC2_EN) {
1035 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
1036 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC2_SIZE)));
1037 ptr = bytes_to_frames(substream->runtime, ptr);
1038 } else {
1039 ptr = 0;
1040 }
1041 spin_unlock(&ensoniq->reg_lock);
1042 return ptr;
1043}
1044
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001045static snd_pcm_uframes_t snd_ensoniq_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001047 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 size_t ptr;
1049
1050 spin_lock(&ensoniq->reg_lock);
1051 if (inl(ES_REG(ensoniq, CONTROL)) & ES_ADC_EN) {
1052 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
1053 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, ADC_SIZE)));
1054 ptr = bytes_to_frames(substream->runtime, ptr);
1055 } else {
1056 ptr = 0;
1057 }
1058 spin_unlock(&ensoniq->reg_lock);
1059 return ptr;
1060}
1061
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001062static struct snd_pcm_hardware snd_ensoniq_playback1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1065 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1066 SNDRV_PCM_INFO_MMAP_VALID |
1067 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1068 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1069 .rates =
1070#ifndef CHIP1370
1071 SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1072#else
1073 (SNDRV_PCM_RATE_KNOT | /* 5512Hz rate */
1074 SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 |
1075 SNDRV_PCM_RATE_44100),
1076#endif
1077 .rate_min = 4000,
1078 .rate_max = 48000,
1079 .channels_min = 1,
1080 .channels_max = 2,
1081 .buffer_bytes_max = (128*1024),
1082 .period_bytes_min = 64,
1083 .period_bytes_max = (128*1024),
1084 .periods_min = 1,
1085 .periods_max = 1024,
1086 .fifo_size = 0,
1087};
1088
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001089static struct snd_pcm_hardware snd_ensoniq_playback2 =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1092 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1093 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
1094 SNDRV_PCM_INFO_SYNC_START),
1095 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1096 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1097 .rate_min = 4000,
1098 .rate_max = 48000,
1099 .channels_min = 1,
1100 .channels_max = 2,
1101 .buffer_bytes_max = (128*1024),
1102 .period_bytes_min = 64,
1103 .period_bytes_max = (128*1024),
1104 .periods_min = 1,
1105 .periods_max = 1024,
1106 .fifo_size = 0,
1107};
1108
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001109static struct snd_pcm_hardware snd_ensoniq_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1112 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1113 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1114 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1115 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1116 .rate_min = 4000,
1117 .rate_max = 48000,
1118 .channels_min = 1,
1119 .channels_max = 2,
1120 .buffer_bytes_max = (128*1024),
1121 .period_bytes_min = 64,
1122 .period_bytes_max = (128*1024),
1123 .periods_min = 1,
1124 .periods_max = 1024,
1125 .fifo_size = 0,
1126};
1127
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001128static int snd_ensoniq_playback1_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001130 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1131 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 ensoniq->mode |= ES_MODE_PLAY1;
1134 ensoniq->playback1_substream = substream;
1135 runtime->hw = snd_ensoniq_playback1;
1136 snd_pcm_set_sync(substream);
1137 spin_lock_irq(&ensoniq->reg_lock);
1138 if (ensoniq->spdif && ensoniq->playback2_substream == NULL)
1139 ensoniq->spdif_stream = ensoniq->spdif_default;
1140 spin_unlock_irq(&ensoniq->reg_lock);
1141#ifdef CHIP1370
1142 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1143 &snd_es1370_hw_constraints_rates);
1144#else
1145 snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1146 &snd_es1371_hw_constraints_dac_clock);
1147#endif
1148 return 0;
1149}
1150
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001151static int snd_ensoniq_playback2_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001153 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1154 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 ensoniq->mode |= ES_MODE_PLAY2;
1157 ensoniq->playback2_substream = substream;
1158 runtime->hw = snd_ensoniq_playback2;
1159 snd_pcm_set_sync(substream);
1160 spin_lock_irq(&ensoniq->reg_lock);
1161 if (ensoniq->spdif && ensoniq->playback1_substream == NULL)
1162 ensoniq->spdif_stream = ensoniq->spdif_default;
1163 spin_unlock_irq(&ensoniq->reg_lock);
1164#ifdef CHIP1370
1165 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1166 &snd_es1370_hw_constraints_clock);
1167#else
1168 snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1169 &snd_es1371_hw_constraints_dac_clock);
1170#endif
1171 return 0;
1172}
1173
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001174static int snd_ensoniq_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001176 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1177 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 ensoniq->mode |= ES_MODE_CAPTURE;
1180 ensoniq->capture_substream = substream;
1181 runtime->hw = snd_ensoniq_capture;
1182 snd_pcm_set_sync(substream);
1183#ifdef CHIP1370
1184 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1185 &snd_es1370_hw_constraints_clock);
1186#else
1187 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1188 &snd_es1371_hw_constraints_adc_clock);
1189#endif
1190 return 0;
1191}
1192
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001193static int snd_ensoniq_playback1_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001195 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 ensoniq->playback1_substream = NULL;
1198 ensoniq->mode &= ~ES_MODE_PLAY1;
1199 return 0;
1200}
1201
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001202static int snd_ensoniq_playback2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001204 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 ensoniq->playback2_substream = NULL;
1207 spin_lock_irq(&ensoniq->reg_lock);
1208#ifdef CHIP1370
1209 ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
1210#endif
1211 ensoniq->mode &= ~ES_MODE_PLAY2;
1212 spin_unlock_irq(&ensoniq->reg_lock);
1213 return 0;
1214}
1215
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001216static int snd_ensoniq_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001218 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 ensoniq->capture_substream = NULL;
1221 spin_lock_irq(&ensoniq->reg_lock);
1222#ifdef CHIP1370
1223 ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
1224#endif
1225 ensoniq->mode &= ~ES_MODE_CAPTURE;
1226 spin_unlock_irq(&ensoniq->reg_lock);
1227 return 0;
1228}
1229
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001230static struct snd_pcm_ops snd_ensoniq_playback1_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .open = snd_ensoniq_playback1_open,
1232 .close = snd_ensoniq_playback1_close,
1233 .ioctl = snd_pcm_lib_ioctl,
1234 .hw_params = snd_ensoniq_hw_params,
1235 .hw_free = snd_ensoniq_hw_free,
1236 .prepare = snd_ensoniq_playback1_prepare,
1237 .trigger = snd_ensoniq_trigger,
1238 .pointer = snd_ensoniq_playback1_pointer,
1239};
1240
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001241static struct snd_pcm_ops snd_ensoniq_playback2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .open = snd_ensoniq_playback2_open,
1243 .close = snd_ensoniq_playback2_close,
1244 .ioctl = snd_pcm_lib_ioctl,
1245 .hw_params = snd_ensoniq_hw_params,
1246 .hw_free = snd_ensoniq_hw_free,
1247 .prepare = snd_ensoniq_playback2_prepare,
1248 .trigger = snd_ensoniq_trigger,
1249 .pointer = snd_ensoniq_playback2_pointer,
1250};
1251
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001252static struct snd_pcm_ops snd_ensoniq_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .open = snd_ensoniq_capture_open,
1254 .close = snd_ensoniq_capture_close,
1255 .ioctl = snd_pcm_lib_ioctl,
1256 .hw_params = snd_ensoniq_hw_params,
1257 .hw_free = snd_ensoniq_hw_free,
1258 .prepare = snd_ensoniq_capture_prepare,
1259 .trigger = snd_ensoniq_trigger,
1260 .pointer = snd_ensoniq_capture_pointer,
1261};
1262
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001263static int __devinit snd_ensoniq_pcm(struct ensoniq * ensoniq, int device,
1264 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001266 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int err;
1268
1269 if (rpcm)
1270 *rpcm = NULL;
Takashi Iwai1fe4d422012-09-12 15:46:20 +02001271 err = snd_pcm_new(ensoniq->card, CHIP_NAME "/1", device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (err < 0)
1273 return err;
1274
1275#ifdef CHIP1370
1276 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
1277#else
1278 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
1279#endif
1280 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ensoniq_capture_ops);
1281
1282 pcm->private_data = ensoniq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 pcm->info_flags = 0;
Takashi Iwai1fe4d422012-09-12 15:46:20 +02001284 strcpy(pcm->name, CHIP_NAME " DAC2/ADC");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 ensoniq->pcm1 = pcm;
1286
1287 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1288 snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
1289
1290 if (rpcm)
1291 *rpcm = pcm;
1292 return 0;
1293}
1294
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001295static int __devinit snd_ensoniq_pcm2(struct ensoniq * ensoniq, int device,
1296 struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001298 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int err;
1300
1301 if (rpcm)
1302 *rpcm = NULL;
Takashi Iwai1fe4d422012-09-12 15:46:20 +02001303 err = snd_pcm_new(ensoniq->card, CHIP_NAME "/2", device, 1, 0, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (err < 0)
1305 return err;
1306
1307#ifdef CHIP1370
1308 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
1309#else
1310 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
1311#endif
1312 pcm->private_data = ensoniq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 pcm->info_flags = 0;
Takashi Iwai1fe4d422012-09-12 15:46:20 +02001314 strcpy(pcm->name, CHIP_NAME " DAC1");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ensoniq->pcm2 = pcm;
1316
1317 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1318 snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
1319
1320 if (rpcm)
1321 *rpcm = pcm;
1322 return 0;
1323}
1324
1325/*
1326 * Mixer section
1327 */
1328
1329/*
1330 * ENS1371 mixer (including SPDIF interface)
1331 */
1332#ifdef CHIP1371
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001333static int snd_ens1373_spdif_info(struct snd_kcontrol *kcontrol,
1334 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1337 uinfo->count = 1;
1338 return 0;
1339}
1340
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001341static int snd_ens1373_spdif_default_get(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001344 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_lock_irq(&ensoniq->reg_lock);
1346 ucontrol->value.iec958.status[0] = (ensoniq->spdif_default >> 0) & 0xff;
1347 ucontrol->value.iec958.status[1] = (ensoniq->spdif_default >> 8) & 0xff;
1348 ucontrol->value.iec958.status[2] = (ensoniq->spdif_default >> 16) & 0xff;
1349 ucontrol->value.iec958.status[3] = (ensoniq->spdif_default >> 24) & 0xff;
1350 spin_unlock_irq(&ensoniq->reg_lock);
1351 return 0;
1352}
1353
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001354static int snd_ens1373_spdif_default_put(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001357 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 unsigned int val;
1359 int change;
1360
1361 val = ((u32)ucontrol->value.iec958.status[0] << 0) |
1362 ((u32)ucontrol->value.iec958.status[1] << 8) |
1363 ((u32)ucontrol->value.iec958.status[2] << 16) |
1364 ((u32)ucontrol->value.iec958.status[3] << 24);
1365 spin_lock_irq(&ensoniq->reg_lock);
1366 change = ensoniq->spdif_default != val;
1367 ensoniq->spdif_default = val;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001368 if (change && ensoniq->playback1_substream == NULL &&
1369 ensoniq->playback2_substream == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
1371 spin_unlock_irq(&ensoniq->reg_lock);
1372 return change;
1373}
1374
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001375static int snd_ens1373_spdif_mask_get(struct snd_kcontrol *kcontrol,
1376 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 ucontrol->value.iec958.status[0] = 0xff;
1379 ucontrol->value.iec958.status[1] = 0xff;
1380 ucontrol->value.iec958.status[2] = 0xff;
1381 ucontrol->value.iec958.status[3] = 0xff;
1382 return 0;
1383}
1384
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001385static int snd_ens1373_spdif_stream_get(struct snd_kcontrol *kcontrol,
1386 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001388 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_lock_irq(&ensoniq->reg_lock);
1390 ucontrol->value.iec958.status[0] = (ensoniq->spdif_stream >> 0) & 0xff;
1391 ucontrol->value.iec958.status[1] = (ensoniq->spdif_stream >> 8) & 0xff;
1392 ucontrol->value.iec958.status[2] = (ensoniq->spdif_stream >> 16) & 0xff;
1393 ucontrol->value.iec958.status[3] = (ensoniq->spdif_stream >> 24) & 0xff;
1394 spin_unlock_irq(&ensoniq->reg_lock);
1395 return 0;
1396}
1397
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001398static int snd_ens1373_spdif_stream_put(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001401 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned int val;
1403 int change;
1404
1405 val = ((u32)ucontrol->value.iec958.status[0] << 0) |
1406 ((u32)ucontrol->value.iec958.status[1] << 8) |
1407 ((u32)ucontrol->value.iec958.status[2] << 16) |
1408 ((u32)ucontrol->value.iec958.status[3] << 24);
1409 spin_lock_irq(&ensoniq->reg_lock);
1410 change = ensoniq->spdif_stream != val;
1411 ensoniq->spdif_stream = val;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001412 if (change && (ensoniq->playback1_substream != NULL ||
1413 ensoniq->playback2_substream != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
1415 spin_unlock_irq(&ensoniq->reg_lock);
1416 return change;
1417}
1418
1419#define ES1371_SPDIF(xname) \
1420{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_es1371_spdif_info, \
1421 .get = snd_es1371_spdif_get, .put = snd_es1371_spdif_put }
1422
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001423#define snd_es1371_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001425static int snd_es1371_spdif_get(struct snd_kcontrol *kcontrol,
1426 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001428 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 spin_lock_irq(&ensoniq->reg_lock);
1431 ucontrol->value.integer.value[0] = ensoniq->ctrl & ES_1373_SPDIF_THRU ? 1 : 0;
1432 spin_unlock_irq(&ensoniq->reg_lock);
1433 return 0;
1434}
1435
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001436static int snd_es1371_spdif_put(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001439 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned int nval1, nval2;
1441 int change;
1442
1443 nval1 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_THRU : 0;
1444 nval2 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_EN : 0;
1445 spin_lock_irq(&ensoniq->reg_lock);
1446 change = (ensoniq->ctrl & ES_1373_SPDIF_THRU) != nval1;
1447 ensoniq->ctrl &= ~ES_1373_SPDIF_THRU;
1448 ensoniq->ctrl |= nval1;
1449 ensoniq->cssr &= ~ES_1373_SPDIF_EN;
1450 ensoniq->cssr |= nval2;
1451 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1452 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1453 spin_unlock_irq(&ensoniq->reg_lock);
1454 return change;
1455}
1456
1457
1458/* spdif controls */
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001459static struct snd_kcontrol_new snd_es1371_mixer_spdif[] __devinitdata = {
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001460 ES1371_SPDIF(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 {
Clemens Ladisch5549d542005-08-03 13:50:30 +02001462 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1464 .info = snd_ens1373_spdif_info,
1465 .get = snd_ens1373_spdif_default_get,
1466 .put = snd_ens1373_spdif_default_put,
1467 },
1468 {
1469 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001470 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1472 .info = snd_ens1373_spdif_info,
1473 .get = snd_ens1373_spdif_mask_get
1474 },
1475 {
Clemens Ladisch5549d542005-08-03 13:50:30 +02001476 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1478 .info = snd_ens1373_spdif_info,
1479 .get = snd_ens1373_spdif_stream_get,
1480 .put = snd_ens1373_spdif_stream_put
1481 },
1482};
1483
1484
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001485#define snd_es1373_rear_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001487static int snd_es1373_rear_get(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001490 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int val = 0;
1492
1493 spin_lock_irq(&ensoniq->reg_lock);
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001494 if ((ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|
1495 ES_1373_REAR_BIT24)) == ES_1373_REAR_BIT26)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 val = 1;
1497 ucontrol->value.integer.value[0] = val;
1498 spin_unlock_irq(&ensoniq->reg_lock);
1499 return 0;
1500}
1501
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001502static int snd_es1373_rear_put(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001505 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 unsigned int nval1;
1507 int change;
1508
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001509 nval1 = ucontrol->value.integer.value[0] ?
1510 ES_1373_REAR_BIT26 : (ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_lock_irq(&ensoniq->reg_lock);
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001512 change = (ensoniq->cssr & (ES_1373_REAR_BIT27|
1513 ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) != nval1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24);
1515 ensoniq->cssr |= nval1;
1516 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1517 spin_unlock_irq(&ensoniq->reg_lock);
1518 return change;
1519}
1520
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001521static struct snd_kcontrol_new snd_ens1373_rear __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1524 .name = "AC97 2ch->4ch Copy Switch",
1525 .info = snd_es1373_rear_info,
1526 .get = snd_es1373_rear_get,
1527 .put = snd_es1373_rear_put,
1528};
1529
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001530#define snd_es1373_line_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001532static int snd_es1373_line_get(struct snd_kcontrol *kcontrol,
1533 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001535 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int val = 0;
1537
1538 spin_lock_irq(&ensoniq->reg_lock);
1539 if ((ensoniq->ctrl & ES_1371_GPIO_OUTM) >= 4)
1540 val = 1;
1541 ucontrol->value.integer.value[0] = val;
1542 spin_unlock_irq(&ensoniq->reg_lock);
1543 return 0;
1544}
1545
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001546static int snd_es1373_line_put(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001549 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int changed;
1551 unsigned int ctrl;
1552
1553 spin_lock_irq(&ensoniq->reg_lock);
1554 ctrl = ensoniq->ctrl;
1555 if (ucontrol->value.integer.value[0])
1556 ensoniq->ctrl |= ES_1371_GPIO_OUT(4); /* switch line-in -> rear out */
1557 else
1558 ensoniq->ctrl &= ~ES_1371_GPIO_OUT(4);
1559 changed = (ctrl != ensoniq->ctrl);
1560 if (changed)
1561 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1562 spin_unlock_irq(&ensoniq->reg_lock);
1563 return changed;
1564}
1565
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001566static struct snd_kcontrol_new snd_ens1373_line __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1569 .name = "Line In->Rear Out Switch",
1570 .info = snd_es1373_line_info,
1571 .get = snd_es1373_line_get,
1572 .put = snd_es1373_line_put,
1573};
1574
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001575static void snd_ensoniq_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001577 struct ensoniq *ensoniq = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ensoniq->u.es1371.ac97 = NULL;
1579}
1580
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001581struct es1371_quirk {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 unsigned short vid; /* vendor ID */
1583 unsigned short did; /* device ID */
1584 unsigned char rev; /* revision */
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001585};
1586
Randy Dunlap076c0e42007-06-26 11:43:52 +02001587static int es1371_quirk_lookup(struct ensoniq *ensoniq,
1588 struct es1371_quirk *list)
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001589{
1590 while (list->vid != (unsigned short)PCI_ANY_ID) {
1591 if (ensoniq->pci->vendor == list->vid &&
1592 ensoniq->pci->device == list->did &&
1593 ensoniq->rev == list->rev)
1594 return 1;
1595 list++;
1596 }
1597 return 0;
1598}
1599
1600static struct es1371_quirk es1371_spdif_present[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
1602 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
1603 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
1604 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
1605 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
1606 { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
1607};
1608
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001609static struct snd_pci_quirk ens1373_line_quirk[] __devinitdata = {
1610 SND_PCI_QUIRK_ID(0x1274, 0x2000), /* GA-7DXR */
1611 SND_PCI_QUIRK_ID(0x1458, 0xa000), /* GA-8IEXP */
1612 { } /* end */
1613};
1614
1615static int __devinit snd_ensoniq_1371_mixer(struct ensoniq *ensoniq,
1616 int has_spdif, int has_line)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001618 struct snd_card *card = ensoniq->card;
1619 struct snd_ac97_bus *pbus;
1620 struct snd_ac97_template ac97;
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001621 int err;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001622 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 .write = snd_es1371_codec_write,
1624 .read = snd_es1371_codec_read,
Jaroslav Kysela072c0112005-07-09 10:07:55 +02001625 .wait = snd_es1371_codec_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 };
1627
1628 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
1629 return err;
1630
1631 memset(&ac97, 0, sizeof(ac97));
1632 ac97.private_data = ensoniq;
1633 ac97.private_free = snd_ensoniq_mixer_free_ac97;
Rene Herman462dba22008-07-17 14:02:16 +02001634 ac97.pci = ensoniq->pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ac97.scaps = AC97_SCAP_AUDIO;
1636 if ((err = snd_ac97_mixer(pbus, &ac97, &ensoniq->u.es1371.ac97)) < 0)
1637 return err;
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001638 if (has_spdif > 0 ||
1639 (!has_spdif && es1371_quirk_lookup(ensoniq, es1371_spdif_present))) {
1640 struct snd_kcontrol *kctl;
Harvey Harrison405b0a32008-02-28 11:53:07 +01001641 int i, is_spdif = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001643 ensoniq->spdif_default = ensoniq->spdif_stream =
1644 SNDRV_PCM_DEFAULT_CON_SPDIF;
1645 outl(ensoniq->spdif_default, ES_REG(ensoniq, CHANNEL_STATUS));
Jaroslav Kysela015b6a12005-11-28 10:50:59 +01001646
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001647 if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SPDIF)
Harvey Harrison405b0a32008-02-28 11:53:07 +01001648 is_spdif++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001650 for (i = 0; i < ARRAY_SIZE(snd_es1371_mixer_spdif); i++) {
1651 kctl = snd_ctl_new1(&snd_es1371_mixer_spdif[i], ensoniq);
1652 if (!kctl)
1653 return -ENOMEM;
Harvey Harrison405b0a32008-02-28 11:53:07 +01001654 kctl->id.index = is_spdif;
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001655 err = snd_ctl_add(card, kctl);
1656 if (err < 0)
1657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SDAC) {
1661 /* mirror rear to front speakers */
1662 ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
1663 ensoniq->cssr |= ES_1373_REAR_BIT26;
1664 err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_rear, ensoniq));
1665 if (err < 0)
1666 return err;
1667 }
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001668 if (has_line > 0 ||
1669 snd_pci_quirk_lookup(ensoniq->pci, ens1373_line_quirk)) {
1670 err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_line,
1671 ensoniq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (err < 0)
1673 return err;
1674 }
1675
1676 return 0;
1677}
1678
1679#endif /* CHIP1371 */
1680
1681/* generic control callbacks for ens1370 */
1682#ifdef CHIP1370
1683#define ENSONIQ_CONTROL(xname, mask) \
1684{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, .info = snd_ensoniq_control_info, \
1685 .get = snd_ensoniq_control_get, .put = snd_ensoniq_control_put, \
1686 .private_value = mask }
1687
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001688#define snd_ensoniq_control_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001690static int snd_ensoniq_control_get(struct snd_kcontrol *kcontrol,
1691 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001693 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int mask = kcontrol->private_value;
1695
1696 spin_lock_irq(&ensoniq->reg_lock);
1697 ucontrol->value.integer.value[0] = ensoniq->ctrl & mask ? 1 : 0;
1698 spin_unlock_irq(&ensoniq->reg_lock);
1699 return 0;
1700}
1701
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001702static int snd_ensoniq_control_put(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001705 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int mask = kcontrol->private_value;
1707 unsigned int nval;
1708 int change;
1709
1710 nval = ucontrol->value.integer.value[0] ? mask : 0;
1711 spin_lock_irq(&ensoniq->reg_lock);
1712 change = (ensoniq->ctrl & mask) != nval;
1713 ensoniq->ctrl &= ~mask;
1714 ensoniq->ctrl |= nval;
1715 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1716 spin_unlock_irq(&ensoniq->reg_lock);
1717 return change;
1718}
1719
1720/*
1721 * ENS1370 mixer
1722 */
1723
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001724static struct snd_kcontrol_new snd_es1370_controls[2] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725ENSONIQ_CONTROL("PCM 0 Output also on Line-In Jack", ES_1370_XCTL0),
1726ENSONIQ_CONTROL("Mic +5V bias", ES_1370_XCTL1)
1727};
1728
1729#define ES1370_CONTROLS ARRAY_SIZE(snd_es1370_controls)
1730
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001731static void snd_ensoniq_mixer_free_ak4531(struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001733 struct ensoniq *ensoniq = ak4531->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 ensoniq->u.es1370.ak4531 = NULL;
1735}
1736
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001737static int __devinit snd_ensoniq_1370_mixer(struct ensoniq * ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001739 struct snd_card *card = ensoniq->card;
1740 struct snd_ak4531 ak4531;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 unsigned int idx;
1742 int err;
1743
1744 /* try reset AK4531 */
1745 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
1746 inw(ES_REG(ensoniq, 1370_CODEC));
1747 udelay(100);
1748 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
1749 inw(ES_REG(ensoniq, 1370_CODEC));
1750 udelay(100);
1751
1752 memset(&ak4531, 0, sizeof(ak4531));
1753 ak4531.write = snd_es1370_codec_write;
1754 ak4531.private_data = ensoniq;
1755 ak4531.private_free = snd_ensoniq_mixer_free_ak4531;
1756 if ((err = snd_ak4531_mixer(card, &ak4531, &ensoniq->u.es1370.ak4531)) < 0)
1757 return err;
1758 for (idx = 0; idx < ES1370_CONTROLS; idx++) {
1759 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1370_controls[idx], ensoniq));
1760 if (err < 0)
1761 return err;
1762 }
1763 return 0;
1764}
1765
1766#endif /* CHIP1370 */
1767
1768#ifdef SUPPORT_JOYSTICK
1769
1770#ifdef CHIP1371
1771static int __devinit snd_ensoniq_get_joystick_port(int dev)
1772{
1773 switch (joystick_port[dev]) {
1774 case 0: /* disabled */
1775 case 1: /* auto-detect */
1776 case 0x200:
1777 case 0x208:
1778 case 0x210:
1779 case 0x218:
1780 return joystick_port[dev];
1781
1782 default:
1783 printk(KERN_ERR "ens1371: invalid joystick port %#x", joystick_port[dev]);
1784 return 0;
1785 }
1786}
1787#else
1788static inline int snd_ensoniq_get_joystick_port(int dev)
1789{
1790 return joystick[dev] ? 0x200 : 0;
1791}
1792#endif
1793
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001794static int __devinit snd_ensoniq_create_gameport(struct ensoniq *ensoniq, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct gameport *gp;
1797 int io_port;
1798
1799 io_port = snd_ensoniq_get_joystick_port(dev);
1800
1801 switch (io_port) {
1802 case 0:
1803 return -ENOSYS;
1804
1805 case 1: /* auto_detect */
1806 for (io_port = 0x200; io_port <= 0x218; io_port += 8)
1807 if (request_region(io_port, 8, "ens137x: gameport"))
1808 break;
1809 if (io_port > 0x218) {
1810 printk(KERN_WARNING "ens137x: no gameport ports available\n");
1811 return -EBUSY;
1812 }
1813 break;
1814
1815 default:
1816 if (!request_region(io_port, 8, "ens137x: gameport")) {
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001817 printk(KERN_WARNING "ens137x: gameport io port 0x%#x in use\n",
1818 io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return -EBUSY;
1820 }
1821 break;
1822 }
1823
1824 ensoniq->gameport = gp = gameport_allocate_port();
1825 if (!gp) {
1826 printk(KERN_ERR "ens137x: cannot allocate memory for gameport\n");
1827 release_region(io_port, 8);
1828 return -ENOMEM;
1829 }
1830
1831 gameport_set_name(gp, "ES137x");
1832 gameport_set_phys(gp, "pci%s/gameport0", pci_name(ensoniq->pci));
1833 gameport_set_dev_parent(gp, &ensoniq->pci->dev);
1834 gp->io = io_port;
1835
1836 ensoniq->ctrl |= ES_JYSTK_EN;
1837#ifdef CHIP1371
1838 ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
1839 ensoniq->ctrl |= ES_1371_JOY_ASEL((io_port - 0x200) / 8);
1840#endif
1841 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1842
1843 gameport_register_port(ensoniq->gameport);
1844
1845 return 0;
1846}
1847
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001848static void snd_ensoniq_free_gameport(struct ensoniq *ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 if (ensoniq->gameport) {
1851 int port = ensoniq->gameport->io;
1852
1853 gameport_unregister_port(ensoniq->gameport);
1854 ensoniq->gameport = NULL;
1855 ensoniq->ctrl &= ~ES_JYSTK_EN;
1856 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1857 release_region(port, 8);
1858 }
1859}
1860#else
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001861static inline int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, long port) { return -ENOSYS; }
1862static inline void snd_ensoniq_free_gameport(struct ensoniq *ensoniq) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#endif /* SUPPORT_JOYSTICK */
1864
1865/*
1866
1867 */
1868
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001869static void snd_ensoniq_proc_read(struct snd_info_entry *entry,
1870 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001872 struct ensoniq *ensoniq = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Takashi Iwai1fe4d422012-09-12 15:46:20 +02001874 snd_iprintf(buffer, "Ensoniq AudioPCI " CHIP_NAME "\n\n");
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001875 snd_iprintf(buffer, "Joystick enable : %s\n",
1876 ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877#ifdef CHIP1370
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001878 snd_iprintf(buffer, "MIC +5V bias : %s\n",
1879 ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
1880 snd_iprintf(buffer, "Line In to AOUT : %s\n",
1881 ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882#else
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001883 snd_iprintf(buffer, "Joystick port : 0x%x\n",
1884 (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885#endif
1886}
1887
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001888static void __devinit snd_ensoniq_proc_init(struct ensoniq * ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001890 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 if (! snd_card_proc_new(ensoniq->card, "audiopci", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001893 snd_info_set_text_ops(entry, ensoniq, snd_ensoniq_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896/*
1897
1898 */
1899
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001900static int snd_ensoniq_free(struct ensoniq *ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 snd_ensoniq_free_gameport(ensoniq);
1903 if (ensoniq->irq < 0)
1904 goto __hw_end;
1905#ifdef CHIP1370
1906 outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL)); /* switch everything off */
1907 outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
1908#else
1909 outl(0, ES_REG(ensoniq, CONTROL)); /* switch everything off */
1910 outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
1911#endif
Jeff Garzikf000fd82008-04-22 13:50:34 +02001912 if (ensoniq->irq >= 0)
1913 synchronize_irq(ensoniq->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 pci_set_power_state(ensoniq->pci, 3);
1915 __hw_end:
1916#ifdef CHIP1370
1917 if (ensoniq->dma_bug.area)
1918 snd_dma_free_pages(&ensoniq->dma_bug);
1919#endif
1920 if (ensoniq->irq >= 0)
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001921 free_irq(ensoniq->irq, ensoniq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 pci_release_regions(ensoniq->pci);
1923 pci_disable_device(ensoniq->pci);
1924 kfree(ensoniq);
1925 return 0;
1926}
1927
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001928static int snd_ensoniq_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001930 struct ensoniq *ensoniq = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return snd_ensoniq_free(ensoniq);
1932}
1933
1934#ifdef CHIP1371
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001935static struct snd_pci_quirk es1371_amplifier_hack[] __devinitdata = {
1936 SND_PCI_QUIRK_ID(0x107b, 0x2150), /* Gateway Solo 2150 */
1937 SND_PCI_QUIRK_ID(0x13bd, 0x100c), /* EV1938 on Mebius PC-MJ100V */
1938 SND_PCI_QUIRK_ID(0x1102, 0x5938), /* Targa Xtender300 */
1939 SND_PCI_QUIRK_ID(0x1102, 0x8938), /* IPC Topnote G notebook */
1940 { } /* end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941};
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001942
1943static struct es1371_quirk es1371_ac97_reset_hack[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
1945 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
1946 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
1947 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
1948 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
1949 { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
1950};
1951#endif
1952
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001953static void snd_ensoniq_chip_init(struct ensoniq *ensoniq)
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01001954{
1955#ifdef CHIP1371
1956 int idx;
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01001957#endif
Takashi Iwaieb3414b2005-11-17 15:03:46 +01001958 /* this code was part of snd_ensoniq_create before intruduction
1959 * of suspend/resume
1960 */
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01001961#ifdef CHIP1370
1962 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1963 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
1964 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
1965 outl(ensoniq->dma_bug.addr, ES_REG(ensoniq, PHANTOM_FRAME));
1966 outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
1967#else
1968 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1969 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
1970 outl(0, ES_REG(ensoniq, 1371_LEGACY));
Takashi Iwai5da8fa22006-11-24 15:38:18 +01001971 if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack)) {
1972 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1973 /* need to delay around 20ms(bleech) to give
1974 some CODECs enough time to wakeup */
1975 msleep(20);
1976 }
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01001977 /* AC'97 warm reset to start the bitclk */
1978 outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
1979 inl(ES_REG(ensoniq, CONTROL));
1980 udelay(20);
1981 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1982 /* Init the sample rate converter */
1983 snd_es1371_wait_src_ready(ensoniq);
1984 outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
1985 for (idx = 0; idx < 0x80; idx++)
1986 snd_es1371_src_write(ensoniq, idx, 0);
1987 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
1988 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
1989 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
1990 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
1991 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
1992 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
1993 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
1994 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
1995 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
1996 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
1997 snd_es1371_adc_rate(ensoniq, 22050);
1998 snd_es1371_dac1_rate(ensoniq, 22050);
1999 snd_es1371_dac2_rate(ensoniq, 22050);
2000 /* WARNING:
2001 * enabling the sample rate converter without properly programming
2002 * its parameters causes the chip to lock up (the SRC busy bit will
2003 * be stuck high, and I've found no way to rectify this other than
2004 * power cycle) - Thomas Sailer
2005 */
2006 snd_es1371_wait_src_ready(ensoniq);
2007 outl(0, ES_REG(ensoniq, 1371_SMPRATE));
2008 /* try reset codec directly */
2009 outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
2010#endif
2011 outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
2012 outb(0x00, ES_REG(ensoniq, UART_RES));
2013 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
2014 synchronize_irq(ensoniq->irq);
2015}
2016
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002017#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002018static int snd_ensoniq_suspend(struct device *dev)
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002019{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002020 struct pci_dev *pci = to_pci_dev(dev);
2021 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaife8be102005-11-17 16:13:41 +01002022 struct ensoniq *ensoniq = card->private_data;
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002023
Takashi Iwaife8be102005-11-17 16:13:41 +01002024 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2025
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002026 snd_pcm_suspend_all(ensoniq->pcm1);
2027 snd_pcm_suspend_all(ensoniq->pcm2);
2028
2029#ifdef CHIP1371
Takashi Iwaife8be102005-11-17 16:13:41 +01002030 snd_ac97_suspend(ensoniq->u.es1371.ac97);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002031#else
Takashi Iwai15f500a2006-01-12 11:43:49 +01002032 /* try to reset AK4531 */
2033 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
2034 inw(ES_REG(ensoniq, 1370_CODEC));
2035 udelay(100);
2036 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
2037 inw(ES_REG(ensoniq, 1370_CODEC));
2038 udelay(100);
Takashi Iwaife8be102005-11-17 16:13:41 +01002039 snd_ak4531_suspend(ensoniq->u.es1370.ak4531);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002040#endif
Takashi Iwai30b35392006-10-11 18:52:53 +02002041
Takashi Iwaife8be102005-11-17 16:13:41 +01002042 pci_disable_device(pci);
2043 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002044 pci_set_power_state(pci, PCI_D3hot);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002045 return 0;
2046}
2047
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002048static int snd_ensoniq_resume(struct device *dev)
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002049{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002050 struct pci_dev *pci = to_pci_dev(dev);
2051 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaife8be102005-11-17 16:13:41 +01002052 struct ensoniq *ensoniq = card->private_data;
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002053
Takashi Iwaife8be102005-11-17 16:13:41 +01002054 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02002055 pci_restore_state(pci);
2056 if (pci_enable_device(pci) < 0) {
2057 printk(KERN_ERR DRIVER_NAME ": pci_enable_device failed, "
2058 "disabling device\n");
2059 snd_card_disconnect(card);
2060 return -EIO;
2061 }
Takashi Iwaife8be102005-11-17 16:13:41 +01002062 pci_set_master(pci);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002063
2064 snd_ensoniq_chip_init(ensoniq);
2065
2066#ifdef CHIP1371
Takashi Iwaife8be102005-11-17 16:13:41 +01002067 snd_ac97_resume(ensoniq->u.es1371.ac97);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002068#else
Takashi Iwaife8be102005-11-17 16:13:41 +01002069 snd_ak4531_resume(ensoniq->u.es1370.ak4531);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002070#endif
Takashi Iwaife8be102005-11-17 16:13:41 +01002071 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002072 return 0;
2073}
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002074
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002075static SIMPLE_DEV_PM_OPS(snd_ensoniq_pm, snd_ensoniq_suspend, snd_ensoniq_resume);
2076#define SND_ENSONIQ_PM_OPS &snd_ensoniq_pm
2077#else
2078#define SND_ENSONIQ_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002079#endif /* CONFIG_PM_SLEEP */
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002080
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002081static int __devinit snd_ensoniq_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct pci_dev *pci,
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002083 struct ensoniq ** rensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002085 struct ensoniq *ensoniq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 int err;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002087 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 .dev_free = snd_ensoniq_dev_free,
2089 };
2090
2091 *rensoniq = NULL;
2092 if ((err = pci_enable_device(pci)) < 0)
2093 return err;
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002094 ensoniq = kzalloc(sizeof(*ensoniq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (ensoniq == NULL) {
2096 pci_disable_device(pci);
2097 return -ENOMEM;
2098 }
2099 spin_lock_init(&ensoniq->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002100 mutex_init(&ensoniq->src_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 ensoniq->card = card;
2102 ensoniq->pci = pci;
2103 ensoniq->irq = -1;
2104 if ((err = pci_request_regions(pci, "Ensoniq AudioPCI")) < 0) {
2105 kfree(ensoniq);
2106 pci_disable_device(pci);
2107 return err;
2108 }
2109 ensoniq->port = pci_resource_start(pci, 0);
Takashi Iwai437a5a42006-11-21 12:14:23 +01002110 if (request_irq(pci->irq, snd_audiopci_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002111 KBUILD_MODNAME, ensoniq)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002112 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 snd_ensoniq_free(ensoniq);
2114 return -EBUSY;
2115 }
2116 ensoniq->irq = pci->irq;
2117#ifdef CHIP1370
2118 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2119 16, &ensoniq->dma_bug) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002120 snd_printk(KERN_ERR "unable to allocate space for phantom area - dma_bug\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 snd_ensoniq_free(ensoniq);
2122 return -EBUSY;
2123 }
2124#endif
2125 pci_set_master(pci);
Auke Kok44c10132007-06-08 15:46:36 -07002126 ensoniq->rev = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef CHIP1370
2128#if 0
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002129 ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE |
2130 ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131#else /* get microphone working */
2132 ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
2133#endif
2134 ensoniq->sctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135#else
2136 ensoniq->ctrl = 0;
2137 ensoniq->sctrl = 0;
2138 ensoniq->cssr = 0;
Takashi Iwai5da8fa22006-11-24 15:38:18 +01002139 if (snd_pci_quirk_lookup(pci, es1371_amplifier_hack))
2140 ensoniq->ctrl |= ES_1371_GPIO_OUT(1); /* turn amplifier on */
2141
2142 if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack))
2143 ensoniq->cssr |= ES_1371_ST_AC97_RST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#endif
Kurt J. Boschf31a31b2005-11-16 18:41:21 +01002145
2146 snd_ensoniq_chip_init(ensoniq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ensoniq, &ops)) < 0) {
2149 snd_ensoniq_free(ensoniq);
2150 return err;
2151 }
2152
2153 snd_ensoniq_proc_init(ensoniq);
2154
2155 snd_card_set_dev(card, &pci->dev);
2156
2157 *rensoniq = ensoniq;
2158 return 0;
2159}
2160
2161/*
2162 * MIDI section
2163 */
2164
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002165static void snd_ensoniq_midi_interrupt(struct ensoniq * ensoniq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002167 struct snd_rawmidi *rmidi = ensoniq->rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 unsigned char status, mask, byte;
2169
2170 if (rmidi == NULL)
2171 return;
2172 /* do Rx at first */
2173 spin_lock(&ensoniq->reg_lock);
2174 mask = ensoniq->uartm & ES_MODE_INPUT ? ES_RXRDY : 0;
2175 while (mask) {
2176 status = inb(ES_REG(ensoniq, UART_STATUS));
2177 if ((status & mask) == 0)
2178 break;
2179 byte = inb(ES_REG(ensoniq, UART_DATA));
2180 snd_rawmidi_receive(ensoniq->midi_input, &byte, 1);
2181 }
2182 spin_unlock(&ensoniq->reg_lock);
2183
2184 /* do Tx at second */
2185 spin_lock(&ensoniq->reg_lock);
2186 mask = ensoniq->uartm & ES_MODE_OUTPUT ? ES_TXRDY : 0;
2187 while (mask) {
2188 status = inb(ES_REG(ensoniq, UART_STATUS));
2189 if ((status & mask) == 0)
2190 break;
2191 if (snd_rawmidi_transmit(ensoniq->midi_output, &byte, 1) != 1) {
2192 ensoniq->uartc &= ~ES_TXINTENM;
2193 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2194 mask &= ~ES_TXRDY;
2195 } else {
2196 outb(byte, ES_REG(ensoniq, UART_DATA));
2197 }
2198 }
2199 spin_unlock(&ensoniq->reg_lock);
2200}
2201
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002202static int snd_ensoniq_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002204 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 spin_lock_irq(&ensoniq->reg_lock);
2207 ensoniq->uartm |= ES_MODE_INPUT;
2208 ensoniq->midi_input = substream;
2209 if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
2210 outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
2211 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2212 outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
2213 }
2214 spin_unlock_irq(&ensoniq->reg_lock);
2215 return 0;
2216}
2217
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002218static int snd_ensoniq_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002220 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 spin_lock_irq(&ensoniq->reg_lock);
2223 if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
2224 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2225 outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
2226 } else {
2227 outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
2228 }
2229 ensoniq->midi_input = NULL;
2230 ensoniq->uartm &= ~ES_MODE_INPUT;
2231 spin_unlock_irq(&ensoniq->reg_lock);
2232 return 0;
2233}
2234
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002235static int snd_ensoniq_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002237 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 spin_lock_irq(&ensoniq->reg_lock);
2240 ensoniq->uartm |= ES_MODE_OUTPUT;
2241 ensoniq->midi_output = substream;
2242 if (!(ensoniq->uartm & ES_MODE_INPUT)) {
2243 outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
2244 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2245 outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
2246 }
2247 spin_unlock_irq(&ensoniq->reg_lock);
2248 return 0;
2249}
2250
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002251static int snd_ensoniq_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002253 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 spin_lock_irq(&ensoniq->reg_lock);
2256 if (!(ensoniq->uartm & ES_MODE_INPUT)) {
2257 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2258 outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
2259 } else {
2260 outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
2261 }
2262 ensoniq->midi_output = NULL;
2263 ensoniq->uartm &= ~ES_MODE_OUTPUT;
2264 spin_unlock_irq(&ensoniq->reg_lock);
2265 return 0;
2266}
2267
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002268static void snd_ensoniq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 unsigned long flags;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002271 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 int idx;
2273
2274 spin_lock_irqsave(&ensoniq->reg_lock, flags);
2275 if (up) {
2276 if ((ensoniq->uartc & ES_RXINTEN) == 0) {
2277 /* empty input FIFO */
2278 for (idx = 0; idx < 32; idx++)
2279 inb(ES_REG(ensoniq, UART_DATA));
2280 ensoniq->uartc |= ES_RXINTEN;
2281 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2282 }
2283 } else {
2284 if (ensoniq->uartc & ES_RXINTEN) {
2285 ensoniq->uartc &= ~ES_RXINTEN;
2286 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2287 }
2288 }
2289 spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
2290}
2291
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002292static void snd_ensoniq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 unsigned long flags;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002295 struct ensoniq *ensoniq = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 unsigned char byte;
2297
2298 spin_lock_irqsave(&ensoniq->reg_lock, flags);
2299 if (up) {
2300 if (ES_TXINTENI(ensoniq->uartc) == 0) {
2301 ensoniq->uartc |= ES_TXINTENO(1);
2302 /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
2303 while (ES_TXINTENI(ensoniq->uartc) == 1 &&
2304 (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
2305 if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2306 ensoniq->uartc &= ~ES_TXINTENM;
2307 } else {
2308 outb(byte, ES_REG(ensoniq, UART_DATA));
2309 }
2310 }
2311 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2312 }
2313 } else {
2314 if (ES_TXINTENI(ensoniq->uartc) == 1) {
2315 ensoniq->uartc &= ~ES_TXINTENM;
2316 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2317 }
2318 }
2319 spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
2320}
2321
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002322static struct snd_rawmidi_ops snd_ensoniq_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 .open = snd_ensoniq_midi_output_open,
2325 .close = snd_ensoniq_midi_output_close,
2326 .trigger = snd_ensoniq_midi_output_trigger,
2327};
2328
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002329static struct snd_rawmidi_ops snd_ensoniq_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 .open = snd_ensoniq_midi_input_open,
2332 .close = snd_ensoniq_midi_input_close,
2333 .trigger = snd_ensoniq_midi_input_trigger,
2334};
2335
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002336static int __devinit snd_ensoniq_midi(struct ensoniq * ensoniq, int device,
2337 struct snd_rawmidi **rrawmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002339 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 int err;
2341
2342 if (rrawmidi)
2343 *rrawmidi = NULL;
2344 if ((err = snd_rawmidi_new(ensoniq->card, "ES1370/1", device, 1, 1, &rmidi)) < 0)
2345 return err;
Takashi Iwai1fe4d422012-09-12 15:46:20 +02002346 strcpy(rmidi->name, CHIP_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_ensoniq_midi_output);
2348 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_ensoniq_midi_input);
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002349 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
2350 SNDRV_RAWMIDI_INFO_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 rmidi->private_data = ensoniq;
2352 ensoniq->rmidi = rmidi;
2353 if (rrawmidi)
2354 *rrawmidi = rmidi;
2355 return 0;
2356}
2357
2358/*
2359 * Interrupt handler
2360 */
2361
David Howells7d12e782006-10-05 14:55:46 +01002362static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002364 struct ensoniq *ensoniq = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 unsigned int status, sctrl;
2366
2367 if (ensoniq == NULL)
2368 return IRQ_NONE;
2369
2370 status = inl(ES_REG(ensoniq, STATUS));
2371 if (!(status & ES_INTR))
2372 return IRQ_NONE;
2373
2374 spin_lock(&ensoniq->reg_lock);
2375 sctrl = ensoniq->sctrl;
2376 if (status & ES_DAC1)
2377 sctrl &= ~ES_P1_INT_EN;
2378 if (status & ES_DAC2)
2379 sctrl &= ~ES_P2_INT_EN;
2380 if (status & ES_ADC)
2381 sctrl &= ~ES_R1_INT_EN;
2382 outl(sctrl, ES_REG(ensoniq, SERIAL));
2383 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
2384 spin_unlock(&ensoniq->reg_lock);
2385
2386 if (status & ES_UART)
2387 snd_ensoniq_midi_interrupt(ensoniq);
2388 if ((status & ES_DAC2) && ensoniq->playback2_substream)
2389 snd_pcm_period_elapsed(ensoniq->playback2_substream);
2390 if ((status & ES_ADC) && ensoniq->capture_substream)
2391 snd_pcm_period_elapsed(ensoniq->capture_substream);
2392 if ((status & ES_DAC1) && ensoniq->playback1_substream)
2393 snd_pcm_period_elapsed(ensoniq->playback1_substream);
2394 return IRQ_HANDLED;
2395}
2396
2397static int __devinit snd_audiopci_probe(struct pci_dev *pci,
2398 const struct pci_device_id *pci_id)
2399{
2400 static int dev;
Takashi Iwaieb3414b2005-11-17 15:03:46 +01002401 struct snd_card *card;
2402 struct ensoniq *ensoniq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 int err, pcm_devs[2];
2404
2405 if (dev >= SNDRV_CARDS)
2406 return -ENODEV;
2407 if (!enable[dev]) {
2408 dev++;
2409 return -ENOENT;
2410 }
2411
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002412 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2413 if (err < 0)
2414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 if ((err = snd_ensoniq_create(card, pci, &ensoniq)) < 0) {
2417 snd_card_free(card);
2418 return err;
2419 }
Takashi Iwaife8be102005-11-17 16:13:41 +01002420 card->private_data = ensoniq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 pcm_devs[0] = 0; pcm_devs[1] = 1;
2423#ifdef CHIP1370
2424 if ((err = snd_ensoniq_1370_mixer(ensoniq)) < 0) {
2425 snd_card_free(card);
2426 return err;
2427 }
2428#endif
2429#ifdef CHIP1371
Jaroslav Kysela015b6a12005-11-28 10:50:59 +01002430 if ((err = snd_ensoniq_1371_mixer(ensoniq, spdif[dev], lineio[dev])) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 snd_card_free(card);
2432 return err;
2433 }
2434#endif
2435 if ((err = snd_ensoniq_pcm(ensoniq, 0, NULL)) < 0) {
2436 snd_card_free(card);
2437 return err;
2438 }
2439 if ((err = snd_ensoniq_pcm2(ensoniq, 1, NULL)) < 0) {
2440 snd_card_free(card);
2441 return err;
2442 }
2443 if ((err = snd_ensoniq_midi(ensoniq, 0, NULL)) < 0) {
2444 snd_card_free(card);
2445 return err;
2446 }
2447
2448 snd_ensoniq_create_gameport(ensoniq, dev);
2449
2450 strcpy(card->driver, DRIVER_NAME);
2451
2452 strcpy(card->shortname, "Ensoniq AudioPCI");
2453 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
2454 card->shortname,
2455 card->driver,
2456 ensoniq->port,
2457 ensoniq->irq);
2458
2459 if ((err = snd_card_register(card)) < 0) {
2460 snd_card_free(card);
2461 return err;
2462 }
2463
2464 pci_set_drvdata(pci, card);
2465 dev++;
2466 return 0;
2467}
2468
2469static void __devexit snd_audiopci_remove(struct pci_dev *pci)
2470{
2471 snd_card_free(pci_get_drvdata(pci));
2472 pci_set_drvdata(pci, NULL);
2473}
2474
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002475static struct pci_driver ens137x_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002476 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 .id_table = snd_audiopci_ids,
2478 .probe = snd_audiopci_probe,
2479 .remove = __devexit_p(snd_audiopci_remove),
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002480 .driver = {
2481 .pm = SND_ENSONIQ_PM_OPS,
2482 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483};
2484
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002485module_pci_driver(ens137x_driver);