1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/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>
36#include <linux/module.h>
37#include <linux/mutex.h>
38
39#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"
58#define CHIP_NAME "ES1370"
59#else
60#define DRIVER_NAME "ENS1371"
61#define CHIP_NAME "ES1371"
62#endif
63
64
65MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Thomas Sailer <sailer@ife.ee.ethz.ch>");
66MODULE_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 IS_REACHABLE(CONFIG_GAMEPORT)
83#define SUPPORT_JOYSTICK
84#endif
85
86static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
87static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
88static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
89#ifdef SUPPORT_JOYSTICK
90#ifdef CHIP1371
91static int joystick_port[SNDRV_CARDS];
92#else
93static bool joystick[SNDRV_CARDS];
94#endif
95#endif
96#ifdef CHIP1371
97static int spdif[SNDRV_CARDS];
98static int lineio[SNDRV_CARDS];
99#endif
100
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
116#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
122
123
124
125
126
127
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
134#define CT5880REV_CT5880_E 0x04
135#define ES1371REV_ES1371_B 0x09
136#define EV1938REV_EV1938_A 0x00
137#define ES1371REV_ES1373_8 0x08
138
139
140
141
142
143#define ES_REG(ensoniq, x) ((ensoniq)->port + ES_REG_##x)
144
145#define ES_REG_CONTROL 0x00
146#define ES_1370_ADC_STOP (1<<31)
147#define ES_1370_XCTL1 (1<<30)
148#define ES_1373_BYPASS_P1 (1<<31)
149#define ES_1373_BYPASS_P2 (1<<30)
150#define ES_1373_BYPASS_R (1<<29)
151#define ES_1373_TEST_BIT (1<<28)
152#define ES_1373_RECEN_B (1<<27)
153#define ES_1373_SPDIF_THRU (1<<26)
154#define ES_1371_JOY_ASEL(o) (((o)&0x03)<<24)
155#define ES_1371_JOY_ASELM (0x03<<24)
156#define ES_1371_JOY_ASELI(i) (((i)>>24)&0x03)
157#define ES_1371_GPIO_IN(i) (((i)>>20)&0x0f)
158#define ES_1370_PCLKDIVO(o) (((o)&0x1fff)<<16)
159#define ES_1370_PCLKDIVM ((0x1fff)<<16)
160#define ES_1370_PCLKDIVI(i) (((i)>>16)&0x1fff)
161#define ES_1371_GPIO_OUT(o) (((o)&0x0f)<<16)
162#define ES_1371_GPIO_OUTM (0x0f<<16)
163#define ES_MSFMTSEL (1<<15)
164#define ES_1370_M_SBB (1<<14)
165#define ES_1371_SYNC_RES (1<<14)
166#define ES_1370_WTSRSEL(o) (((o)&0x03)<<12)
167#define ES_1370_WTSRSELM (0x03<<12)
168#define ES_1371_ADC_STOP (1<<13)
169#define ES_1371_PWR_INTRM (1<<12)
170#define ES_1370_DAC_SYNC (1<<11)
171#define ES_1371_M_CB (1<<11)
172#define ES_CCB_INTRM (1<<10)
173#define ES_1370_M_CB (1<<9)
174#define ES_1370_XCTL0 (1<<8)
175#define ES_1371_PDLEV(o) (((o)&0x03)<<8)
176#define ES_1371_PDLEVM (0x03<<8)
177#define ES_BREQ (1<<7)
178#define ES_DAC1_EN (1<<6)
179#define ES_DAC2_EN (1<<5)
180#define ES_ADC_EN (1<<4)
181#define ES_UART_EN (1<<3)
182#define ES_JYSTK_EN (1<<2)
183#define ES_1370_CDC_EN (1<<1)
184#define ES_1371_XTALCKDIS (1<<1)
185#define ES_1370_SERR_DISABLE (1<<0)
186#define ES_1371_PCICLKDIS (1<<0)
187#define ES_REG_STATUS 0x04
188#define ES_INTR (1<<31)
189#define ES_1371_ST_AC97_RST (1<<29)
190#define ES_1373_REAR_BIT27 (1<<27)
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)
194#define ES_1373_SPDIF_EN (1<<18)
195#define ES_1373_SPDIF_TEST (1<<17)
196#define ES_1371_TEST (1<<16)
197#define ES_1373_GPIO_INT(i) (((i)&0x0f)>>12)
198#define ES_1370_CSTAT (1<<10)
199#define ES_1370_CBUSY (1<<9)
200#define ES_1370_CWRIP (1<<8)
201#define ES_1371_SYNC_ERR (1<<8)
202#define ES_1371_VC(i) (((i)>>6)&0x03)
203#define ES_1370_VC(i) (((i)>>5)&0x03)
204#define ES_1371_MPWR (1<<5)
205#define ES_MCCB (1<<4)
206#define ES_UART (1<<3)
207#define ES_DAC1 (1<<2)
208#define ES_DAC2 (1<<1)
209#define ES_ADC (1<<0)
210#define ES_REG_UART_DATA 0x08
211#define ES_REG_UART_STATUS 0x09
212#define ES_RXINT (1<<7)
213#define ES_TXINT (1<<2)
214#define ES_TXRDY (1<<1)
215#define ES_RXRDY (1<<0)
216#define ES_REG_UART_CONTROL 0x09
217#define ES_RXINTEN (1<<7)
218#define ES_TXINTENO(o) (((o)&0x03)<<5)
219#define ES_TXINTENM (0x03<<5)
220#define ES_TXINTENI(i) (((i)>>5)&0x03)
221#define ES_CNTRL(o) (((o)&0x03)<<0)
222#define ES_CNTRLM (0x03<<0)
223#define ES_REG_UART_RES 0x0a
224#define ES_TEST_MODE (1<<0)
225#define ES_REG_MEM_PAGE 0x0c
226#define ES_MEM_PAGEO(o) (((o)&0x0f)<<0)
227#define ES_MEM_PAGEM (0x0f<<0)
228#define ES_MEM_PAGEI(i) (((i)>>0)&0x0f)
229#define ES_REG_1370_CODEC 0x10
230#define ES_1370_CODEC_WRITE(a,d) ((((a)&0xff)<<8)|(((d)&0xff)<<0))
231#define ES_REG_1371_CODEC 0x14
232#define ES_1371_CODEC_RDY (1<<31)
233#define ES_1371_CODEC_WIP (1<<30)
234#define EV_1938_CODEC_MAGIC (1<<26)
235#define ES_1371_CODEC_PIRD (1<<23)
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
241#define ES_1371_SRC_RAM_ADDRO(o) (((o)&0x7f)<<25)
242#define ES_1371_SRC_RAM_ADDRM (0x7f<<25)
243#define ES_1371_SRC_RAM_ADDRI(i) (((i)>>25)&0x7f)
244#define ES_1371_SRC_RAM_WE (1<<24)
245#define ES_1371_SRC_RAM_BUSY (1<<23)
246#define ES_1371_SRC_DISABLE (1<<22)
247#define ES_1371_DIS_P1 (1<<21)
248#define ES_1371_DIS_P2 (1<<20)
249#define ES_1371_DIS_R1 (1<<19)
250#define ES_1371_SRC_RAM_DATAO(o) (((o)&0xffff)<<0)
251#define ES_1371_SRC_RAM_DATAM (0xffff<<0)
252#define ES_1371_SRC_RAM_DATAI(i) (((i)>>0)&0xffff)
253
254#define ES_REG_1371_LEGACY 0x18
255#define ES_1371_JFAST (1<<31)
256#define ES_1371_HIB (1<<30)
257#define ES_1371_VSB (1<<29)
258#define ES_1371_VMPUO(o) (((o)&0x03)<<27)
259#define ES_1371_VMPUM (0x03<<27)
260#define ES_1371_VMPUI(i) (((i)>>27)&0x03)
261#define ES_1371_VCDCO(o) (((o)&0x03)<<25)
262#define ES_1371_VCDCM (0x03<<25)
263#define ES_1371_VCDCI(i) (((i)>>25)&0x03)
264#define ES_1371_FIRQ (1<<24)
265#define ES_1371_SDMACAP (1<<23)
266#define ES_1371_SPICAP (1<<22)
267#define ES_1371_MDMACAP (1<<21)
268#define ES_1371_MPICAP (1<<20)
269#define ES_1371_ADCAP (1<<19)
270#define ES_1371_SVCAP (1<<18)
271#define ES_1371_CDCCAP (1<<17)
272#define ES_1371_BACAP (1<<16)
273#define ES_1371_EXI(i) (((i)>>8)&0x07)
274#define ES_1371_AI(i) (((i)>>3)&0x1f)
275#define ES_1371_WR (1<<2)
276#define ES_1371_LEGINT (1<<0)
277
278#define ES_REG_CHANNEL_STATUS 0x1c
279
280#define ES_REG_SERIAL 0x20
281#define ES_1371_DAC_TEST (1<<22)
282#define ES_P2_END_INCO(o) (((o)&0x07)<<19)
283#define ES_P2_END_INCM (0x07<<19)
284#define ES_P2_END_INCI(i) (((i)>>16)&0x07)
285#define ES_P2_ST_INCO(o) (((o)&0x07)<<16)
286#define ES_P2_ST_INCM (0x07<<16)
287#define ES_P2_ST_INCI(i) (((i)<<16)&0x07)
288#define ES_R1_LOOP_SEL (1<<15)
289#define ES_P2_LOOP_SEL (1<<14)
290#define ES_P1_LOOP_SEL (1<<13)
291#define ES_P2_PAUSE (1<<12)
292#define ES_P1_PAUSE (1<<11)
293#define ES_R1_INT_EN (1<<10)
294#define ES_P2_INT_EN (1<<9)
295#define ES_P1_INT_EN (1<<8)
296#define ES_P1_SCT_RLD (1<<7)
297#define ES_P2_DAC_SEN (1<<6)
298#define ES_R1_MODEO(o) (((o)&0x03)<<4)
299#define ES_R1_MODEM (0x03<<4)
300#define ES_R1_MODEI(i) (((i)>>4)&0x03)
301#define ES_P2_MODEO(o) (((o)&0x03)<<2)
302#define ES_P2_MODEM (0x03<<2)
303#define ES_P2_MODEI(i) (((i)>>2)&0x03)
304#define ES_P1_MODEO(o) (((o)&0x03)<<0)
305#define ES_P1_MODEM (0x03<<0)
306#define ES_P1_MODEI(i) (((i)>>0)&0x03)
307
308#define ES_REG_DAC1_COUNT 0x24
309#define ES_REG_DAC2_COUNT 0x28
310#define ES_REG_ADC_COUNT 0x2c
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
317#define ES_REG_DAC1_SIZE 0x34
318#define ES_REG_DAC2_FRAME 0x38
319#define ES_REG_DAC2_SIZE 0x3c
320#define ES_REG_ADC_FRAME 0x30
321#define ES_REG_ADC_SIZE 0x34
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
329#define ES_REG_PHANTOM_COUNT 0x3c
330
331#define ES_REG_UART_FIFO 0x30
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
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
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
364
365
366#define ES_1370_SRCLOCK 1411200
367#define ES_1370_SRTODIV(x) (ES_1370_SRCLOCK/(x)-2)
368
369
370
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
378#define ES_MODE_INPUT 0x0002
379
380
381
382
383
384struct ensoniq {
385 spinlock_t reg_lock;
386 struct mutex src_mutex;
387
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;
397
398 unsigned int ctrl;
399 unsigned int sctrl;
400 unsigned int cssr;
401 unsigned int uartc;
402 unsigned int rev;
403
404 union {
405#ifdef CHIP1371
406 struct {
407 struct snd_ac97 *ac97;
408 } es1371;
409#else
410 struct {
411 int pclkdiv_lock;
412 struct snd_ak4531 *ak4531;
413 } es1370;
414#endif
415 } u;
416
417 struct pci_dev *pci;
418 struct snd_card *card;
419 struct snd_pcm *pcm1;
420 struct snd_pcm *pcm2;
421 struct snd_pcm_substream *playback1_substream;
422 struct snd_pcm_substream *playback2_substream;
423 struct snd_pcm_substream *capture_substream;
424 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;
430 struct snd_rawmidi *rmidi;
431 struct snd_rawmidi_substream *midi_input;
432 struct snd_rawmidi_substream *midi_output;
433
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
447static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id);
448
449static const struct pci_device_id snd_audiopci_ids[] = {
450#ifdef CHIP1370
451 { PCI_VDEVICE(ENSONIQ, 0x5000), 0, },
452#endif
453#ifdef CHIP1371
454 { PCI_VDEVICE(ENSONIQ, 0x1371), 0, },
455 { PCI_VDEVICE(ENSONIQ, 0x5880), 0, },
456 { PCI_VDEVICE(ECTIVA, 0x8938), 0, },
457#endif
458 { 0, }
459};
460
461MODULE_DEVICE_TABLE(pci, snd_audiopci_ids);
462
463
464
465
466
467#define POLL_COUNT 0xa000
468
469#ifdef CHIP1370
470static const unsigned int snd_es1370_fixed_rates[] =
471 {5512, 11025, 22050, 44100};
472static const struct snd_pcm_hw_constraint_list snd_es1370_hw_constraints_rates = {
473 .count = 4,
474 .list = snd_es1370_fixed_rates,
475 .mask = 0,
476};
477static const struct snd_ratnum es1370_clock = {
478 .num = ES_1370_SRCLOCK,
479 .den_min = 29,
480 .den_max = 353,
481 .den_step = 1,
482};
483static const struct snd_pcm_hw_constraint_ratnums snd_es1370_hw_constraints_clock = {
484 .nrats = 1,
485 .rats = &es1370_clock,
486};
487#else
488static const struct snd_ratden es1371_dac_clock = {
489 .num_min = 3000 * (1 << 15),
490 .num_max = 48000 * (1 << 15),
491 .num_step = 3000,
492 .den = 1 << 15,
493};
494static const struct snd_pcm_hw_constraint_ratdens snd_es1371_hw_constraints_dac_clock = {
495 .nrats = 1,
496 .rats = &es1371_dac_clock,
497};
498static const struct snd_ratnum es1371_adc_clock = {
499 .num = 48000 << 15,
500 .den_min = 32768,
501 .den_max = 393216,
502 .den_step = 1,
503};
504static const struct snd_pcm_hw_constraint_ratnums snd_es1371_hw_constraints_adc_clock = {
505 .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
514
515
516#ifdef CHIP1371
517
518static unsigned int snd_es1371_wait_src_ready(struct ensoniq * ensoniq)
519{
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 }
528 dev_err(ensoniq->card->dev, "wait src ready timeout 0x%lx [0x%x]\n",
529 ES_REG(ensoniq, 1371_SMPRATE), r);
530 return 0;
531}
532
533static unsigned int snd_es1371_src_read(struct ensoniq * ensoniq, unsigned short reg)
534{
535 unsigned int temp, i, orig, r;
536
537
538 temp = orig = snd_es1371_wait_src_ready(ensoniq);
539
540
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
547 temp = snd_es1371_wait_src_ready(ensoniq);
548
549 if ((temp & 0x00870000) != 0x00010000) {
550
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
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
567static void snd_es1371_src_write(struct ensoniq * ensoniq,
568 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
580
581#ifdef CHIP1370
582
583static void snd_es1370_codec_write(struct snd_ak4531 *ak4531,
584 unsigned short reg, unsigned short val)
585{
586 struct ensoniq *ensoniq = ak4531->private_data;
587 unsigned long end_time = jiffies + HZ / 10;
588
589#if 0
590 dev_dbg(ensoniq->card->dev,
591 "CODEC WRITE: reg = 0x%x, val = 0x%x (0x%x), creg = 0x%x\n",
592 reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
593#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 }
599 schedule_timeout_uninterruptible(1);
600 } while (time_after(end_time, jiffies));
601 dev_err(ensoniq->card->dev, "codec write timeout, status = 0x%x\n",
602 inl(ES_REG(ensoniq, STATUS)));
603}
604
605#endif
606
607#ifdef CHIP1371
608
609static inline bool is_ev1938(struct ensoniq *ensoniq)
610{
611 return ensoniq->pci->device == 0x8938;
612}
613
614static void snd_es1371_codec_write(struct snd_ac97 *ac97,
615 unsigned short reg, unsigned short val)
616{
617 struct ensoniq *ensoniq = ac97->private_data;
618 unsigned int t, x, flag;
619
620 flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
621 mutex_lock(&ensoniq->src_mutex);
622 for (t = 0; t < POLL_COUNT; t++) {
623 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
624
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
630
631 for (t = 0; t < POLL_COUNT; t++) {
632 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
633 0x00000000)
634 break;
635 }
636
637 for (t = 0; t < POLL_COUNT; t++) {
638 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
639 0x00010000)
640 break;
641 }
642 outl(ES_1371_CODEC_WRITE(reg, val) | flag,
643 ES_REG(ensoniq, 1371_CODEC));
644
645 snd_es1371_wait_src_ready(ensoniq);
646 outl(x, ES_REG(ensoniq, 1371_SMPRATE));
647 mutex_unlock(&ensoniq->src_mutex);
648 return;
649 }
650 }
651 mutex_unlock(&ensoniq->src_mutex);
652 dev_err(ensoniq->card->dev, "codec write timeout at 0x%lx [0x%x]\n",
653 ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
654}
655
656static unsigned short snd_es1371_codec_read(struct snd_ac97 *ac97,
657 unsigned short reg)
658{
659 struct ensoniq *ensoniq = ac97->private_data;
660 unsigned int t, x, flag, fail = 0;
661
662 flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
663 __again:
664 mutex_lock(&ensoniq->src_mutex);
665 for (t = 0; t < POLL_COUNT; t++) {
666 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
667
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
673
674 for (t = 0; t < POLL_COUNT; t++) {
675 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
676 0x00000000)
677 break;
678 }
679
680 for (t = 0; t < POLL_COUNT; t++) {
681 if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
682 0x00010000)
683 break;
684 }
685 outl(ES_1371_CODEC_READS(reg) | flag,
686 ES_REG(ensoniq, 1371_CODEC));
687
688 snd_es1371_wait_src_ready(ensoniq);
689 outl(x, ES_REG(ensoniq, 1371_SMPRATE));
690
691 for (t = 0; t < POLL_COUNT; t++) {
692 if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP))
693 break;
694 }
695
696 for (t = 0; t < POLL_COUNT; t++) {
697 if ((x = inl(ES_REG(ensoniq, 1371_CODEC))) & ES_1371_CODEC_RDY) {
698 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 }
703 mutex_unlock(&ensoniq->src_mutex);
704 return ES_1371_CODEC_READ(x);
705 }
706 }
707 mutex_unlock(&ensoniq->src_mutex);
708 if (++fail > 10) {
709 dev_err(ensoniq->card->dev,
710 "codec read timeout (final) at 0x%lx, reg = 0x%x [0x%x]\n",
711 ES_REG(ensoniq, 1371_CODEC), reg,
712 inl(ES_REG(ensoniq, 1371_CODEC)));
713 return 0;
714 }
715 goto __again;
716 }
717 }
718 mutex_unlock(&ensoniq->src_mutex);
719 dev_err(ensoniq->card->dev, "codec read timeout at 0x%lx [0x%x]\n",
720 ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
721 return 0;
722}
723
724static void snd_es1371_codec_wait(struct snd_ac97 *ac97)
725{
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
733static void snd_es1371_adc_rate(struct ensoniq * ensoniq, unsigned int rate)
734{
735 unsigned int n, truncm, freq;
736
737 mutex_lock(&ensoniq->src_mutex);
738 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 if (rate >= 24000) {
744 if (truncm > 239)
745 truncm = 239;
746 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
747 (((239 - truncm) >> 1) << 9) | (n << 4));
748 } else {
749 if (truncm > 119)
750 truncm = 119;
751 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
752 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
753 }
754 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
755 (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC +
756 ES_SMPREG_INT_REGS) & 0x00ff) |
757 ((freq >> 5) & 0xfc00));
758 snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
759 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
760 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
761 mutex_unlock(&ensoniq->src_mutex);
762}
763
764static void snd_es1371_dac1_rate(struct ensoniq * ensoniq, unsigned int rate)
765{
766 unsigned int freq, r;
767
768 mutex_lock(&ensoniq->src_mutex);
769 freq = ((rate << 15) + 1500) / 3000;
770 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
771 ES_1371_DIS_P2 | ES_1371_DIS_R1)) |
772 ES_1371_DIS_P1;
773 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
774 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
775 (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 +
776 ES_SMPREG_INT_REGS) & 0x00ff) |
777 ((freq >> 5) & 0xfc00));
778 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
779 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
780 ES_1371_DIS_P2 | ES_1371_DIS_R1));
781 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
782 mutex_unlock(&ensoniq->src_mutex);
783}
784
785static void snd_es1371_dac2_rate(struct ensoniq * ensoniq, unsigned int rate)
786{
787 unsigned int freq, r;
788
789 mutex_lock(&ensoniq->src_mutex);
790 freq = ((rate << 15) + 1500) / 3000;
791 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
792 ES_1371_DIS_P1 | ES_1371_DIS_R1)) |
793 ES_1371_DIS_P2;
794 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
795 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
796 (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 +
797 ES_SMPREG_INT_REGS) & 0x00ff) |
798 ((freq >> 5) & 0xfc00));
799 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC,
800 freq & 0x7fff);
801 r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
802 ES_1371_DIS_P1 | ES_1371_DIS_R1));
803 outl(r, ES_REG(ensoniq, 1371_SMPRATE));
804 mutex_unlock(&ensoniq->src_mutex);
805}
806
807#endif
808
809static int snd_ensoniq_trigger(struct snd_pcm_substream *substream, int cmd)
810{
811 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
812 switch (cmd) {
813 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
814 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
815 {
816 unsigned int what = 0;
817 struct snd_pcm_substream *s;
818 snd_pcm_group_for_each_entry(s, substream) {
819 if (s == ensoniq->playback1_substream) {
820 what |= ES_P1_PAUSE;
821 snd_pcm_trigger_done(s, substream);
822 } else if (s == ensoniq->playback2_substream) {
823 what |= ES_P2_PAUSE;
824 snd_pcm_trigger_done(s, substream);
825 } else if (s == ensoniq->capture_substream)
826 return -EINVAL;
827 }
828 spin_lock(&ensoniq->reg_lock);
829 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
830 ensoniq->sctrl |= what;
831 else
832 ensoniq->sctrl &= ~what;
833 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
834 spin_unlock(&ensoniq->reg_lock);
835 break;
836 }
837 case SNDRV_PCM_TRIGGER_START:
838 case SNDRV_PCM_TRIGGER_STOP:
839 {
840 unsigned int what = 0;
841 struct snd_pcm_substream *s;
842 snd_pcm_group_for_each_entry(s, substream) {
843 if (s == ensoniq->playback1_substream) {
844 what |= ES_DAC1_EN;
845 snd_pcm_trigger_done(s, substream);
846 } else if (s == ensoniq->playback2_substream) {
847 what |= ES_DAC2_EN;
848 snd_pcm_trigger_done(s, substream);
849 } else if (s == ensoniq->capture_substream) {
850 what |= ES_ADC_EN;
851 snd_pcm_trigger_done(s, substream);
852 }
853 }
854 spin_lock(&ensoniq->reg_lock);
855 if (cmd == SNDRV_PCM_TRIGGER_START)
856 ensoniq->ctrl |= what;
857 else
858 ensoniq->ctrl &= ~what;
859 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
860 spin_unlock(&ensoniq->reg_lock);
861 break;
862 }
863 default:
864 return -EINVAL;
865 }
866 return 0;
867}
868
869
870
871
872
873static int snd_ensoniq_hw_params(struct snd_pcm_substream *substream,
874 struct snd_pcm_hw_params *hw_params)
875{
876 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
877}
878
879static int snd_ensoniq_hw_free(struct snd_pcm_substream *substream)
880{
881 return snd_pcm_lib_free_pages(substream);
882}
883
884static int snd_ensoniq_playback1_prepare(struct snd_pcm_substream *substream)
885{
886 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
887 struct snd_pcm_runtime *runtime = substream->runtime;
888 unsigned int mode = 0;
889
890 ensoniq->p1_dma_size = snd_pcm_lib_buffer_bytes(substream);
891 ensoniq->p1_period_size = snd_pcm_lib_period_bytes(substream);
892 if (snd_pcm_format_width(runtime->format) == 16)
893 mode |= 0x02;
894 if (runtime->channels > 1)
895 mode |= 0x01;
896 spin_lock_irq(&ensoniq->reg_lock);
897 ensoniq->ctrl &= ~ES_DAC1_EN;
898#ifdef CHIP1371
899
900 if (runtime->rate == 48000)
901 ensoniq->ctrl |= ES_1373_BYPASS_P1;
902 else
903 ensoniq->ctrl &= ~ES_1373_BYPASS_P1;
904#endif
905 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
906 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
907 outl(runtime->dma_addr, ES_REG(ensoniq, DAC1_FRAME));
908 outl((ensoniq->p1_dma_size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
909 ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
910 ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
911 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
912 outl((ensoniq->p1_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
913 ES_REG(ensoniq, DAC1_COUNT));
914#ifdef CHIP1370
915 ensoniq->ctrl &= ~ES_1370_WTSRSELM;
916 switch (runtime->rate) {
917 case 5512: ensoniq->ctrl |= ES_1370_WTSRSEL(0); break;
918 case 11025: ensoniq->ctrl |= ES_1370_WTSRSEL(1); break;
919 case 22050: ensoniq->ctrl |= ES_1370_WTSRSEL(2); break;
920 case 44100: ensoniq->ctrl |= ES_1370_WTSRSEL(3); break;
921 default: snd_BUG();
922 }
923#endif
924 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
925 spin_unlock_irq(&ensoniq->reg_lock);
926#ifndef CHIP1370
927 snd_es1371_dac1_rate(ensoniq, runtime->rate);
928#endif
929 return 0;
930}
931
932static int snd_ensoniq_playback2_prepare(struct snd_pcm_substream *substream)
933{
934 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
935 struct snd_pcm_runtime *runtime = substream->runtime;
936 unsigned int mode = 0;
937
938 ensoniq->p2_dma_size = snd_pcm_lib_buffer_bytes(substream);
939 ensoniq->p2_period_size = snd_pcm_lib_period_bytes(substream);
940 if (snd_pcm_format_width(runtime->format) == 16)
941 mode |= 0x02;
942 if (runtime->channels > 1)
943 mode |= 0x01;
944 spin_lock_irq(&ensoniq->reg_lock);
945 ensoniq->ctrl &= ~ES_DAC2_EN;
946 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
947 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
948 outl(runtime->dma_addr, ES_REG(ensoniq, DAC2_FRAME));
949 outl((ensoniq->p2_dma_size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
950 ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
951 ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
952 ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
953 ES_P2_END_INCO(mode & 2 ? 2 : 1) | ES_P2_ST_INCO(0);
954 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
955 outl((ensoniq->p2_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
956 ES_REG(ensoniq, DAC2_COUNT));
957#ifdef CHIP1370
958 if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_CAPTURE)) {
959 ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
960 ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
961 ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
962 }
963#endif
964 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
965 spin_unlock_irq(&ensoniq->reg_lock);
966#ifndef CHIP1370
967 snd_es1371_dac2_rate(ensoniq, runtime->rate);
968#endif
969 return 0;
970}
971
972static int snd_ensoniq_capture_prepare(struct snd_pcm_substream *substream)
973{
974 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
975 struct snd_pcm_runtime *runtime = substream->runtime;
976 unsigned int mode = 0;
977
978 ensoniq->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
979 ensoniq->c_period_size = snd_pcm_lib_period_bytes(substream);
980 if (snd_pcm_format_width(runtime->format) == 16)
981 mode |= 0x02;
982 if (runtime->channels > 1)
983 mode |= 0x01;
984 spin_lock_irq(&ensoniq->reg_lock);
985 ensoniq->ctrl &= ~ES_ADC_EN;
986 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
987 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
988 outl(runtime->dma_addr, ES_REG(ensoniq, ADC_FRAME));
989 outl((ensoniq->c_dma_size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
990 ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
991 ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
992 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
993 outl((ensoniq->c_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
994 ES_REG(ensoniq, ADC_COUNT));
995#ifdef CHIP1370
996 if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_PLAY2)) {
997 ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
998 ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
999 ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
1000 }
1001#endif
1002 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1003 spin_unlock_irq(&ensoniq->reg_lock);
1004#ifndef CHIP1370
1005 snd_es1371_adc_rate(ensoniq, runtime->rate);
1006#endif
1007 return 0;
1008}
1009
1010static snd_pcm_uframes_t snd_ensoniq_playback1_pointer(struct snd_pcm_substream *substream)
1011{
1012 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1013 size_t ptr;
1014
1015 spin_lock(&ensoniq->reg_lock);
1016 if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC1_EN) {
1017 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
1018 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC1_SIZE)));
1019 ptr = bytes_to_frames(substream->runtime, ptr);
1020 } else {
1021 ptr = 0;
1022 }
1023 spin_unlock(&ensoniq->reg_lock);
1024 return ptr;
1025}
1026
1027static snd_pcm_uframes_t snd_ensoniq_playback2_pointer(struct snd_pcm_substream *substream)
1028{
1029 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1030 size_t ptr;
1031
1032 spin_lock(&ensoniq->reg_lock);
1033 if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC2_EN) {
1034 outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
1035 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC2_SIZE)));
1036 ptr = bytes_to_frames(substream->runtime, ptr);
1037 } else {
1038 ptr = 0;
1039 }
1040 spin_unlock(&ensoniq->reg_lock);
1041 return ptr;
1042}
1043
1044static snd_pcm_uframes_t snd_ensoniq_capture_pointer(struct snd_pcm_substream *substream)
1045{
1046 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1047 size_t ptr;
1048
1049 spin_lock(&ensoniq->reg_lock);
1050 if (inl(ES_REG(ensoniq, CONTROL)) & ES_ADC_EN) {
1051 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
1052 ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, ADC_SIZE)));
1053 ptr = bytes_to_frames(substream->runtime, ptr);
1054 } else {
1055 ptr = 0;
1056 }
1057 spin_unlock(&ensoniq->reg_lock);
1058 return ptr;
1059}
1060
1061static const struct snd_pcm_hardware snd_ensoniq_playback1 =
1062{
1063 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1064 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1065 SNDRV_PCM_INFO_MMAP_VALID |
1066 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1067 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1068 .rates =
1069#ifndef CHIP1370
1070 SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1071#else
1072 (SNDRV_PCM_RATE_KNOT |
1073 SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 |
1074 SNDRV_PCM_RATE_44100),
1075#endif
1076 .rate_min = 4000,
1077 .rate_max = 48000,
1078 .channels_min = 1,
1079 .channels_max = 2,
1080 .buffer_bytes_max = (128*1024),
1081 .period_bytes_min = 64,
1082 .period_bytes_max = (128*1024),
1083 .periods_min = 1,
1084 .periods_max = 1024,
1085 .fifo_size = 0,
1086};
1087
1088static const struct snd_pcm_hardware snd_ensoniq_playback2 =
1089{
1090 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1091 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1092 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
1093 SNDRV_PCM_INFO_SYNC_START),
1094 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1095 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1096 .rate_min = 4000,
1097 .rate_max = 48000,
1098 .channels_min = 1,
1099 .channels_max = 2,
1100 .buffer_bytes_max = (128*1024),
1101 .period_bytes_min = 64,
1102 .period_bytes_max = (128*1024),
1103 .periods_min = 1,
1104 .periods_max = 1024,
1105 .fifo_size = 0,
1106};
1107
1108static const struct snd_pcm_hardware snd_ensoniq_capture =
1109{
1110 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1111 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1112 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1113 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1114 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1115 .rate_min = 4000,
1116 .rate_max = 48000,
1117 .channels_min = 1,
1118 .channels_max = 2,
1119 .buffer_bytes_max = (128*1024),
1120 .period_bytes_min = 64,
1121 .period_bytes_max = (128*1024),
1122 .periods_min = 1,
1123 .periods_max = 1024,
1124 .fifo_size = 0,
1125};
1126
1127static int snd_ensoniq_playback1_open(struct snd_pcm_substream *substream)
1128{
1129 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1130 struct snd_pcm_runtime *runtime = substream->runtime;
1131
1132 ensoniq->mode |= ES_MODE_PLAY1;
1133 ensoniq->playback1_substream = substream;
1134 runtime->hw = snd_ensoniq_playback1;
1135 snd_pcm_set_sync(substream);
1136 spin_lock_irq(&ensoniq->reg_lock);
1137 if (ensoniq->spdif && ensoniq->playback2_substream == NULL)
1138 ensoniq->spdif_stream = ensoniq->spdif_default;
1139 spin_unlock_irq(&ensoniq->reg_lock);
1140#ifdef CHIP1370
1141 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1142 &snd_es1370_hw_constraints_rates);
1143#else
1144 snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1145 &snd_es1371_hw_constraints_dac_clock);
1146#endif
1147 return 0;
1148}
1149
1150static int snd_ensoniq_playback2_open(struct snd_pcm_substream *substream)
1151{
1152 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1153 struct snd_pcm_runtime *runtime = substream->runtime;
1154
1155 ensoniq->mode |= ES_MODE_PLAY2;
1156 ensoniq->playback2_substream = substream;
1157 runtime->hw = snd_ensoniq_playback2;
1158 snd_pcm_set_sync(substream);
1159 spin_lock_irq(&ensoniq->reg_lock);
1160 if (ensoniq->spdif && ensoniq->playback1_substream == NULL)
1161 ensoniq->spdif_stream = ensoniq->spdif_default;
1162 spin_unlock_irq(&ensoniq->reg_lock);
1163#ifdef CHIP1370
1164 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1165 &snd_es1370_hw_constraints_clock);
1166#else
1167 snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1168 &snd_es1371_hw_constraints_dac_clock);
1169#endif
1170 return 0;
1171}
1172
1173static int snd_ensoniq_capture_open(struct snd_pcm_substream *substream)
1174{
1175 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1176 struct snd_pcm_runtime *runtime = substream->runtime;
1177
1178 ensoniq->mode |= ES_MODE_CAPTURE;
1179 ensoniq->capture_substream = substream;
1180 runtime->hw = snd_ensoniq_capture;
1181 snd_pcm_set_sync(substream);
1182#ifdef CHIP1370
1183 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1184 &snd_es1370_hw_constraints_clock);
1185#else
1186 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1187 &snd_es1371_hw_constraints_adc_clock);
1188#endif
1189 return 0;
1190}
1191
1192static int snd_ensoniq_playback1_close(struct snd_pcm_substream *substream)
1193{
1194 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1195
1196 ensoniq->playback1_substream = NULL;
1197 ensoniq->mode &= ~ES_MODE_PLAY1;
1198 return 0;
1199}
1200
1201static int snd_ensoniq_playback2_close(struct snd_pcm_substream *substream)
1202{
1203 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1204
1205 ensoniq->playback2_substream = NULL;
1206 spin_lock_irq(&ensoniq->reg_lock);
1207#ifdef CHIP1370
1208 ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
1209#endif
1210 ensoniq->mode &= ~ES_MODE_PLAY2;
1211 spin_unlock_irq(&ensoniq->reg_lock);
1212 return 0;
1213}
1214
1215static int snd_ensoniq_capture_close(struct snd_pcm_substream *substream)
1216{
1217 struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
1218
1219 ensoniq->capture_substream = NULL;
1220 spin_lock_irq(&ensoniq->reg_lock);
1221#ifdef CHIP1370
1222 ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
1223#endif
1224 ensoniq->mode &= ~ES_MODE_CAPTURE;
1225 spin_unlock_irq(&ensoniq->reg_lock);
1226 return 0;
1227}
1228
1229static const struct snd_pcm_ops snd_ensoniq_playback1_ops = {
1230 .open = snd_ensoniq_playback1_open,
1231 .close = snd_ensoniq_playback1_close,
1232 .ioctl = snd_pcm_lib_ioctl,
1233 .hw_params = snd_ensoniq_hw_params,
1234 .hw_free = snd_ensoniq_hw_free,
1235 .prepare = snd_ensoniq_playback1_prepare,
1236 .trigger = snd_ensoniq_trigger,
1237 .pointer = snd_ensoniq_playback1_pointer,
1238};
1239
1240static const struct snd_pcm_ops snd_ensoniq_playback2_ops = {
1241 .open = snd_ensoniq_playback2_open,
1242 .close = snd_ensoniq_playback2_close,
1243 .ioctl = snd_pcm_lib_ioctl,
1244 .hw_params = snd_ensoniq_hw_params,
1245 .hw_free = snd_ensoniq_hw_free,
1246 .prepare = snd_ensoniq_playback2_prepare,
1247 .trigger = snd_ensoniq_trigger,
1248 .pointer = snd_ensoniq_playback2_pointer,
1249};
1250
1251static const struct snd_pcm_ops snd_ensoniq_capture_ops = {
1252 .open = snd_ensoniq_capture_open,
1253 .close = snd_ensoniq_capture_close,
1254 .ioctl = snd_pcm_lib_ioctl,
1255 .hw_params = snd_ensoniq_hw_params,
1256 .hw_free = snd_ensoniq_hw_free,
1257 .prepare = snd_ensoniq_capture_prepare,
1258 .trigger = snd_ensoniq_trigger,
1259 .pointer = snd_ensoniq_capture_pointer,
1260};
1261
1262static const struct snd_pcm_chmap_elem surround_map[] = {
1263 { .channels = 1,
1264 .map = { SNDRV_CHMAP_MONO } },
1265 { .channels = 2,
1266 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1267 { }
1268};
1269
1270static int snd_ensoniq_pcm(struct ensoniq *ensoniq, int device)
1271{
1272 struct snd_pcm *pcm;
1273 int err;
1274
1275 err = snd_pcm_new(ensoniq->card, CHIP_NAME "/1", device, 1, 1, &pcm);
1276 if (err < 0)
1277 return err;
1278
1279#ifdef CHIP1370
1280 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
1281#else
1282 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
1283#endif
1284 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ensoniq_capture_ops);
1285
1286 pcm->private_data = ensoniq;
1287 pcm->info_flags = 0;
1288 strcpy(pcm->name, CHIP_NAME " DAC2/ADC");
1289 ensoniq->pcm1 = pcm;
1290
1291 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1292 snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
1293
1294#ifdef CHIP1370
1295 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1296 surround_map, 2, 0, NULL);
1297#else
1298 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1299 snd_pcm_std_chmaps, 2, 0, NULL);
1300#endif
1301 return err;
1302}
1303
1304static int snd_ensoniq_pcm2(struct ensoniq *ensoniq, int device)
1305{
1306 struct snd_pcm *pcm;
1307 int err;
1308
1309 err = snd_pcm_new(ensoniq->card, CHIP_NAME "/2", device, 1, 0, &pcm);
1310 if (err < 0)
1311 return err;
1312
1313#ifdef CHIP1370
1314 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
1315#else
1316 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
1317#endif
1318 pcm->private_data = ensoniq;
1319 pcm->info_flags = 0;
1320 strcpy(pcm->name, CHIP_NAME " DAC1");
1321 ensoniq->pcm2 = pcm;
1322
1323 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1324 snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
1325
1326#ifdef CHIP1370
1327 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1328 snd_pcm_std_chmaps, 2, 0, NULL);
1329#else
1330 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1331 surround_map, 2, 0, NULL);
1332#endif
1333 return err;
1334}
1335
1336
1337
1338
1339
1340
1341
1342
1343#ifdef CHIP1371
1344static int snd_ens1373_spdif_info(struct snd_kcontrol *kcontrol,
1345 struct snd_ctl_elem_info *uinfo)
1346{
1347 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1348 uinfo->count = 1;
1349 return 0;
1350}
1351
1352static int snd_ens1373_spdif_default_get(struct snd_kcontrol *kcontrol,
1353 struct snd_ctl_elem_value *ucontrol)
1354{
1355 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1356 spin_lock_irq(&ensoniq->reg_lock);
1357 ucontrol->value.iec958.status[0] = (ensoniq->spdif_default >> 0) & 0xff;
1358 ucontrol->value.iec958.status[1] = (ensoniq->spdif_default >> 8) & 0xff;
1359 ucontrol->value.iec958.status[2] = (ensoniq->spdif_default >> 16) & 0xff;
1360 ucontrol->value.iec958.status[3] = (ensoniq->spdif_default >> 24) & 0xff;
1361 spin_unlock_irq(&ensoniq->reg_lock);
1362 return 0;
1363}
1364
1365static int snd_ens1373_spdif_default_put(struct snd_kcontrol *kcontrol,
1366 struct snd_ctl_elem_value *ucontrol)
1367{
1368 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1369 unsigned int val;
1370 int change;
1371
1372 val = ((u32)ucontrol->value.iec958.status[0] << 0) |
1373 ((u32)ucontrol->value.iec958.status[1] << 8) |
1374 ((u32)ucontrol->value.iec958.status[2] << 16) |
1375 ((u32)ucontrol->value.iec958.status[3] << 24);
1376 spin_lock_irq(&ensoniq->reg_lock);
1377 change = ensoniq->spdif_default != val;
1378 ensoniq->spdif_default = val;
1379 if (change && ensoniq->playback1_substream == NULL &&
1380 ensoniq->playback2_substream == NULL)
1381 outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
1382 spin_unlock_irq(&ensoniq->reg_lock);
1383 return change;
1384}
1385
1386static int snd_ens1373_spdif_mask_get(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_value *ucontrol)
1388{
1389 ucontrol->value.iec958.status[0] = 0xff;
1390 ucontrol->value.iec958.status[1] = 0xff;
1391 ucontrol->value.iec958.status[2] = 0xff;
1392 ucontrol->value.iec958.status[3] = 0xff;
1393 return 0;
1394}
1395
1396static int snd_ens1373_spdif_stream_get(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_value *ucontrol)
1398{
1399 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1400 spin_lock_irq(&ensoniq->reg_lock);
1401 ucontrol->value.iec958.status[0] = (ensoniq->spdif_stream >> 0) & 0xff;
1402 ucontrol->value.iec958.status[1] = (ensoniq->spdif_stream >> 8) & 0xff;
1403 ucontrol->value.iec958.status[2] = (ensoniq->spdif_stream >> 16) & 0xff;
1404 ucontrol->value.iec958.status[3] = (ensoniq->spdif_stream >> 24) & 0xff;
1405 spin_unlock_irq(&ensoniq->reg_lock);
1406 return 0;
1407}
1408
1409static int snd_ens1373_spdif_stream_put(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
1411{
1412 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1413 unsigned int val;
1414 int change;
1415
1416 val = ((u32)ucontrol->value.iec958.status[0] << 0) |
1417 ((u32)ucontrol->value.iec958.status[1] << 8) |
1418 ((u32)ucontrol->value.iec958.status[2] << 16) |
1419 ((u32)ucontrol->value.iec958.status[3] << 24);
1420 spin_lock_irq(&ensoniq->reg_lock);
1421 change = ensoniq->spdif_stream != val;
1422 ensoniq->spdif_stream = val;
1423 if (change && (ensoniq->playback1_substream != NULL ||
1424 ensoniq->playback2_substream != NULL))
1425 outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
1426 spin_unlock_irq(&ensoniq->reg_lock);
1427 return change;
1428}
1429
1430#define ES1371_SPDIF(xname) \
1431{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_es1371_spdif_info, \
1432 .get = snd_es1371_spdif_get, .put = snd_es1371_spdif_put }
1433
1434#define snd_es1371_spdif_info snd_ctl_boolean_mono_info
1435
1436static int snd_es1371_spdif_get(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
1438{
1439 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1440
1441 spin_lock_irq(&ensoniq->reg_lock);
1442 ucontrol->value.integer.value[0] = ensoniq->ctrl & ES_1373_SPDIF_THRU ? 1 : 0;
1443 spin_unlock_irq(&ensoniq->reg_lock);
1444 return 0;
1445}
1446
1447static int snd_es1371_spdif_put(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_value *ucontrol)
1449{
1450 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1451 unsigned int nval1, nval2;
1452 int change;
1453
1454 nval1 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_THRU : 0;
1455 nval2 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_EN : 0;
1456 spin_lock_irq(&ensoniq->reg_lock);
1457 change = (ensoniq->ctrl & ES_1373_SPDIF_THRU) != nval1;
1458 ensoniq->ctrl &= ~ES_1373_SPDIF_THRU;
1459 ensoniq->ctrl |= nval1;
1460 ensoniq->cssr &= ~ES_1373_SPDIF_EN;
1461 ensoniq->cssr |= nval2;
1462 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1463 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1464 spin_unlock_irq(&ensoniq->reg_lock);
1465 return change;
1466}
1467
1468
1469
1470static struct snd_kcontrol_new snd_es1371_mixer_spdif[] = {
1471 ES1371_SPDIF(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH)),
1472 {
1473 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1474 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1475 .info = snd_ens1373_spdif_info,
1476 .get = snd_ens1373_spdif_default_get,
1477 .put = snd_ens1373_spdif_default_put,
1478 },
1479 {
1480 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1481 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1482 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1483 .info = snd_ens1373_spdif_info,
1484 .get = snd_ens1373_spdif_mask_get
1485 },
1486 {
1487 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1488 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1489 .info = snd_ens1373_spdif_info,
1490 .get = snd_ens1373_spdif_stream_get,
1491 .put = snd_ens1373_spdif_stream_put
1492 },
1493};
1494
1495
1496#define snd_es1373_rear_info snd_ctl_boolean_mono_info
1497
1498static int snd_es1373_rear_get(struct snd_kcontrol *kcontrol,
1499 struct snd_ctl_elem_value *ucontrol)
1500{
1501 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1502 int val = 0;
1503
1504 spin_lock_irq(&ensoniq->reg_lock);
1505 if ((ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|
1506 ES_1373_REAR_BIT24)) == ES_1373_REAR_BIT26)
1507 val = 1;
1508 ucontrol->value.integer.value[0] = val;
1509 spin_unlock_irq(&ensoniq->reg_lock);
1510 return 0;
1511}
1512
1513static int snd_es1373_rear_put(struct snd_kcontrol *kcontrol,
1514 struct snd_ctl_elem_value *ucontrol)
1515{
1516 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1517 unsigned int nval1;
1518 int change;
1519
1520 nval1 = ucontrol->value.integer.value[0] ?
1521 ES_1373_REAR_BIT26 : (ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
1522 spin_lock_irq(&ensoniq->reg_lock);
1523 change = (ensoniq->cssr & (ES_1373_REAR_BIT27|
1524 ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) != nval1;
1525 ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24);
1526 ensoniq->cssr |= nval1;
1527 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1528 spin_unlock_irq(&ensoniq->reg_lock);
1529 return change;
1530}
1531
1532static const struct snd_kcontrol_new snd_ens1373_rear =
1533{
1534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1535 .name = "AC97 2ch->4ch Copy Switch",
1536 .info = snd_es1373_rear_info,
1537 .get = snd_es1373_rear_get,
1538 .put = snd_es1373_rear_put,
1539};
1540
1541#define snd_es1373_line_info snd_ctl_boolean_mono_info
1542
1543static int snd_es1373_line_get(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_value *ucontrol)
1545{
1546 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1547 int val = 0;
1548
1549 spin_lock_irq(&ensoniq->reg_lock);
1550 if (ensoniq->ctrl & ES_1371_GPIO_OUT(4))
1551 val = 1;
1552 ucontrol->value.integer.value[0] = val;
1553 spin_unlock_irq(&ensoniq->reg_lock);
1554 return 0;
1555}
1556
1557static int snd_es1373_line_put(struct snd_kcontrol *kcontrol,
1558 struct snd_ctl_elem_value *ucontrol)
1559{
1560 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1561 int changed;
1562 unsigned int ctrl;
1563
1564 spin_lock_irq(&ensoniq->reg_lock);
1565 ctrl = ensoniq->ctrl;
1566 if (ucontrol->value.integer.value[0])
1567 ensoniq->ctrl |= ES_1371_GPIO_OUT(4);
1568 else
1569 ensoniq->ctrl &= ~ES_1371_GPIO_OUT(4);
1570 changed = (ctrl != ensoniq->ctrl);
1571 if (changed)
1572 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1573 spin_unlock_irq(&ensoniq->reg_lock);
1574 return changed;
1575}
1576
1577static const struct snd_kcontrol_new snd_ens1373_line =
1578{
1579 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1580 .name = "Line In->Rear Out Switch",
1581 .info = snd_es1373_line_info,
1582 .get = snd_es1373_line_get,
1583 .put = snd_es1373_line_put,
1584};
1585
1586static void snd_ensoniq_mixer_free_ac97(struct snd_ac97 *ac97)
1587{
1588 struct ensoniq *ensoniq = ac97->private_data;
1589 ensoniq->u.es1371.ac97 = NULL;
1590}
1591
1592struct es1371_quirk {
1593 unsigned short vid;
1594 unsigned short did;
1595 unsigned char rev;
1596};
1597
1598static int es1371_quirk_lookup(struct ensoniq *ensoniq,
1599 struct es1371_quirk *list)
1600{
1601 while (list->vid != (unsigned short)PCI_ANY_ID) {
1602 if (ensoniq->pci->vendor == list->vid &&
1603 ensoniq->pci->device == list->did &&
1604 ensoniq->rev == list->rev)
1605 return 1;
1606 list++;
1607 }
1608 return 0;
1609}
1610
1611static struct es1371_quirk es1371_spdif_present[] = {
1612 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
1613 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
1614 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
1615 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
1616 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
1617 { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
1618};
1619
1620static struct snd_pci_quirk ens1373_line_quirk[] = {
1621 SND_PCI_QUIRK_ID(0x1274, 0x2000),
1622 SND_PCI_QUIRK_ID(0x1458, 0xa000),
1623 { }
1624};
1625
1626static int snd_ensoniq_1371_mixer(struct ensoniq *ensoniq,
1627 int has_spdif, int has_line)
1628{
1629 struct snd_card *card = ensoniq->card;
1630 struct snd_ac97_bus *pbus;
1631 struct snd_ac97_template ac97;
1632 int err;
1633 static struct snd_ac97_bus_ops ops = {
1634 .write = snd_es1371_codec_write,
1635 .read = snd_es1371_codec_read,
1636 .wait = snd_es1371_codec_wait,
1637 };
1638
1639 if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
1640 return err;
1641
1642 memset(&ac97, 0, sizeof(ac97));
1643 ac97.private_data = ensoniq;
1644 ac97.private_free = snd_ensoniq_mixer_free_ac97;
1645 ac97.pci = ensoniq->pci;
1646 ac97.scaps = AC97_SCAP_AUDIO;
1647 if ((err = snd_ac97_mixer(pbus, &ac97, &ensoniq->u.es1371.ac97)) < 0)
1648 return err;
1649 if (has_spdif > 0 ||
1650 (!has_spdif && es1371_quirk_lookup(ensoniq, es1371_spdif_present))) {
1651 struct snd_kcontrol *kctl;
1652 int i, is_spdif = 0;
1653
1654 ensoniq->spdif_default = ensoniq->spdif_stream =
1655 SNDRV_PCM_DEFAULT_CON_SPDIF;
1656 outl(ensoniq->spdif_default, ES_REG(ensoniq, CHANNEL_STATUS));
1657
1658 if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SPDIF)
1659 is_spdif++;
1660
1661 for (i = 0; i < ARRAY_SIZE(snd_es1371_mixer_spdif); i++) {
1662 kctl = snd_ctl_new1(&snd_es1371_mixer_spdif[i], ensoniq);
1663 if (!kctl)
1664 return -ENOMEM;
1665 kctl->id.index = is_spdif;
1666 err = snd_ctl_add(card, kctl);
1667 if (err < 0)
1668 return err;
1669 }
1670 }
1671 if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SDAC) {
1672
1673 ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
1674 ensoniq->cssr |= ES_1373_REAR_BIT26;
1675 err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_rear, ensoniq));
1676 if (err < 0)
1677 return err;
1678 }
1679 if (has_line > 0 ||
1680 snd_pci_quirk_lookup(ensoniq->pci, ens1373_line_quirk)) {
1681 err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_line,
1682 ensoniq));
1683 if (err < 0)
1684 return err;
1685 }
1686
1687 return 0;
1688}
1689
1690#endif
1691
1692
1693#ifdef CHIP1370
1694#define ENSONIQ_CONTROL(xname, mask) \
1695{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, .info = snd_ensoniq_control_info, \
1696 .get = snd_ensoniq_control_get, .put = snd_ensoniq_control_put, \
1697 .private_value = mask }
1698
1699#define snd_ensoniq_control_info snd_ctl_boolean_mono_info
1700
1701static int snd_ensoniq_control_get(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol)
1703{
1704 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1705 int mask = kcontrol->private_value;
1706
1707 spin_lock_irq(&ensoniq->reg_lock);
1708 ucontrol->value.integer.value[0] = ensoniq->ctrl & mask ? 1 : 0;
1709 spin_unlock_irq(&ensoniq->reg_lock);
1710 return 0;
1711}
1712
1713static int snd_ensoniq_control_put(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_value *ucontrol)
1715{
1716 struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
1717 int mask = kcontrol->private_value;
1718 unsigned int nval;
1719 int change;
1720
1721 nval = ucontrol->value.integer.value[0] ? mask : 0;
1722 spin_lock_irq(&ensoniq->reg_lock);
1723 change = (ensoniq->ctrl & mask) != nval;
1724 ensoniq->ctrl &= ~mask;
1725 ensoniq->ctrl |= nval;
1726 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1727 spin_unlock_irq(&ensoniq->reg_lock);
1728 return change;
1729}
1730
1731
1732
1733
1734
1735static struct snd_kcontrol_new snd_es1370_controls[2] = {
1736ENSONIQ_CONTROL("PCM 0 Output also on Line-In Jack", ES_1370_XCTL0),
1737ENSONIQ_CONTROL("Mic +5V bias", ES_1370_XCTL1)
1738};
1739
1740#define ES1370_CONTROLS ARRAY_SIZE(snd_es1370_controls)
1741
1742static void snd_ensoniq_mixer_free_ak4531(struct snd_ak4531 *ak4531)
1743{
1744 struct ensoniq *ensoniq = ak4531->private_data;
1745 ensoniq->u.es1370.ak4531 = NULL;
1746}
1747
1748static int snd_ensoniq_1370_mixer(struct ensoniq *ensoniq)
1749{
1750 struct snd_card *card = ensoniq->card;
1751 struct snd_ak4531 ak4531;
1752 unsigned int idx;
1753 int err;
1754
1755
1756 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
1757 inw(ES_REG(ensoniq, 1370_CODEC));
1758 udelay(100);
1759 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
1760 inw(ES_REG(ensoniq, 1370_CODEC));
1761 udelay(100);
1762
1763 memset(&ak4531, 0, sizeof(ak4531));
1764 ak4531.write = snd_es1370_codec_write;
1765 ak4531.private_data = ensoniq;
1766 ak4531.private_free = snd_ensoniq_mixer_free_ak4531;
1767 if ((err = snd_ak4531_mixer(card, &ak4531, &ensoniq->u.es1370.ak4531)) < 0)
1768 return err;
1769 for (idx = 0; idx < ES1370_CONTROLS; idx++) {
1770 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1370_controls[idx], ensoniq));
1771 if (err < 0)
1772 return err;
1773 }
1774 return 0;
1775}
1776
1777#endif
1778
1779#ifdef SUPPORT_JOYSTICK
1780
1781#ifdef CHIP1371
1782static int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
1783{
1784 switch (joystick_port[dev]) {
1785 case 0:
1786 case 1:
1787 case 0x200:
1788 case 0x208:
1789 case 0x210:
1790 case 0x218:
1791 return joystick_port[dev];
1792
1793 default:
1794 dev_err(ensoniq->card->dev,
1795 "invalid joystick port %#x", joystick_port[dev]);
1796 return 0;
1797 }
1798}
1799#else
1800static int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
1801{
1802 return joystick[dev] ? 0x200 : 0;
1803}
1804#endif
1805
1806static int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, int dev)
1807{
1808 struct gameport *gp;
1809 int io_port;
1810
1811 io_port = snd_ensoniq_get_joystick_port(ensoniq, dev);
1812
1813 switch (io_port) {
1814 case 0:
1815 return -ENOSYS;
1816
1817 case 1:
1818 for (io_port = 0x200; io_port <= 0x218; io_port += 8)
1819 if (request_region(io_port, 8, "ens137x: gameport"))
1820 break;
1821 if (io_port > 0x218) {
1822 dev_warn(ensoniq->card->dev,
1823 "no gameport ports available\n");
1824 return -EBUSY;
1825 }
1826 break;
1827
1828 default:
1829 if (!request_region(io_port, 8, "ens137x: gameport")) {
1830 dev_warn(ensoniq->card->dev,
1831 "gameport io port %#x in use\n",
1832 io_port);
1833 return -EBUSY;
1834 }
1835 break;
1836 }
1837
1838 ensoniq->gameport = gp = gameport_allocate_port();
1839 if (!gp) {
1840 dev_err(ensoniq->card->dev,
1841 "cannot allocate memory for gameport\n");
1842 release_region(io_port, 8);
1843 return -ENOMEM;
1844 }
1845
1846 gameport_set_name(gp, "ES137x");
1847 gameport_set_phys(gp, "pci%s/gameport0", pci_name(ensoniq->pci));
1848 gameport_set_dev_parent(gp, &ensoniq->pci->dev);
1849 gp->io = io_port;
1850
1851 ensoniq->ctrl |= ES_JYSTK_EN;
1852#ifdef CHIP1371
1853 ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
1854 ensoniq->ctrl |= ES_1371_JOY_ASEL((io_port - 0x200) / 8);
1855#endif
1856 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1857
1858 gameport_register_port(ensoniq->gameport);
1859
1860 return 0;
1861}
1862
1863static void snd_ensoniq_free_gameport(struct ensoniq *ensoniq)
1864{
1865 if (ensoniq->gameport) {
1866 int port = ensoniq->gameport->io;
1867
1868 gameport_unregister_port(ensoniq->gameport);
1869 ensoniq->gameport = NULL;
1870 ensoniq->ctrl &= ~ES_JYSTK_EN;
1871 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1872 release_region(port, 8);
1873 }
1874}
1875#else
1876static inline int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, long port) { return -ENOSYS; }
1877static inline void snd_ensoniq_free_gameport(struct ensoniq *ensoniq) { }
1878#endif
1879
1880
1881
1882
1883
1884static void snd_ensoniq_proc_read(struct snd_info_entry *entry,
1885 struct snd_info_buffer *buffer)
1886{
1887 struct ensoniq *ensoniq = entry->private_data;
1888
1889 snd_iprintf(buffer, "Ensoniq AudioPCI " CHIP_NAME "\n\n");
1890 snd_iprintf(buffer, "Joystick enable : %s\n",
1891 ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
1892#ifdef CHIP1370
1893 snd_iprintf(buffer, "MIC +5V bias : %s\n",
1894 ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
1895 snd_iprintf(buffer, "Line In to AOUT : %s\n",
1896 ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
1897#else
1898 snd_iprintf(buffer, "Joystick port : 0x%x\n",
1899 (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
1900#endif
1901}
1902
1903static void snd_ensoniq_proc_init(struct ensoniq *ensoniq)
1904{
1905 snd_card_ro_proc_new(ensoniq->card, "audiopci", ensoniq,
1906 snd_ensoniq_proc_read);
1907}
1908
1909
1910
1911
1912
1913static int snd_ensoniq_free(struct ensoniq *ensoniq)
1914{
1915 snd_ensoniq_free_gameport(ensoniq);
1916 if (ensoniq->irq < 0)
1917 goto __hw_end;
1918#ifdef CHIP1370
1919 outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL));
1920 outl(0, ES_REG(ensoniq, SERIAL));
1921#else
1922 outl(0, ES_REG(ensoniq, CONTROL));
1923 outl(0, ES_REG(ensoniq, SERIAL));
1924#endif
1925 if (ensoniq->irq >= 0)
1926 synchronize_irq(ensoniq->irq);
1927 pci_set_power_state(ensoniq->pci, PCI_D3hot);
1928 __hw_end:
1929#ifdef CHIP1370
1930 if (ensoniq->dma_bug.area)
1931 snd_dma_free_pages(&ensoniq->dma_bug);
1932#endif
1933 if (ensoniq->irq >= 0)
1934 free_irq(ensoniq->irq, ensoniq);
1935 pci_release_regions(ensoniq->pci);
1936 pci_disable_device(ensoniq->pci);
1937 kfree(ensoniq);
1938 return 0;
1939}
1940
1941static int snd_ensoniq_dev_free(struct snd_device *device)
1942{
1943 struct ensoniq *ensoniq = device->device_data;
1944 return snd_ensoniq_free(ensoniq);
1945}
1946
1947#ifdef CHIP1371
1948static struct snd_pci_quirk es1371_amplifier_hack[] = {
1949 SND_PCI_QUIRK_ID(0x107b, 0x2150),
1950 SND_PCI_QUIRK_ID(0x13bd, 0x100c),
1951 SND_PCI_QUIRK_ID(0x1102, 0x5938),
1952 SND_PCI_QUIRK_ID(0x1102, 0x8938),
1953 { }
1954};
1955
1956static struct es1371_quirk es1371_ac97_reset_hack[] = {
1957 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
1958 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
1959 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
1960 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
1961 { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
1962 { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
1963};
1964#endif
1965
1966static void snd_ensoniq_chip_init(struct ensoniq *ensoniq)
1967{
1968#ifdef CHIP1371
1969 int idx;
1970#endif
1971
1972
1973
1974#ifdef CHIP1370
1975 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1976 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
1977 outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
1978 outl(ensoniq->dma_bug.addr, ES_REG(ensoniq, PHANTOM_FRAME));
1979 outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
1980#else
1981 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1982 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
1983 outl(0, ES_REG(ensoniq, 1371_LEGACY));
1984 if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack)) {
1985 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
1986
1987
1988 msleep(20);
1989 }
1990
1991 outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
1992 inl(ES_REG(ensoniq, CONTROL));
1993 udelay(20);
1994 outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
1995
1996 snd_es1371_wait_src_ready(ensoniq);
1997 outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
1998 for (idx = 0; idx < 0x80; idx++)
1999 snd_es1371_src_write(ensoniq, idx, 0);
2000 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
2001 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
2002 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
2003 snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
2004 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
2005 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
2006 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
2007 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
2008 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
2009 snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
2010 snd_es1371_adc_rate(ensoniq, 22050);
2011 snd_es1371_dac1_rate(ensoniq, 22050);
2012 snd_es1371_dac2_rate(ensoniq, 22050);
2013
2014
2015
2016
2017
2018
2019 snd_es1371_wait_src_ready(ensoniq);
2020 outl(0, ES_REG(ensoniq, 1371_SMPRATE));
2021
2022 outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
2023#endif
2024 outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
2025 outb(0x00, ES_REG(ensoniq, UART_RES));
2026 outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
2027 synchronize_irq(ensoniq->irq);
2028}
2029
2030#ifdef CONFIG_PM_SLEEP
2031static int snd_ensoniq_suspend(struct device *dev)
2032{
2033 struct snd_card *card = dev_get_drvdata(dev);
2034 struct ensoniq *ensoniq = card->private_data;
2035
2036 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2037
2038 snd_pcm_suspend_all(ensoniq->pcm1);
2039 snd_pcm_suspend_all(ensoniq->pcm2);
2040
2041#ifdef CHIP1371
2042 snd_ac97_suspend(ensoniq->u.es1371.ac97);
2043#else
2044
2045 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
2046 inw(ES_REG(ensoniq, 1370_CODEC));
2047 udelay(100);
2048 outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
2049 inw(ES_REG(ensoniq, 1370_CODEC));
2050 udelay(100);
2051 snd_ak4531_suspend(ensoniq->u.es1370.ak4531);
2052#endif
2053 return 0;
2054}
2055
2056static int snd_ensoniq_resume(struct device *dev)
2057{
2058 struct snd_card *card = dev_get_drvdata(dev);
2059 struct ensoniq *ensoniq = card->private_data;
2060
2061 snd_ensoniq_chip_init(ensoniq);
2062
2063#ifdef CHIP1371
2064 snd_ac97_resume(ensoniq->u.es1371.ac97);
2065#else
2066 snd_ak4531_resume(ensoniq->u.es1370.ak4531);
2067#endif
2068 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2069 return 0;
2070}
2071
2072static SIMPLE_DEV_PM_OPS(snd_ensoniq_pm, snd_ensoniq_suspend, snd_ensoniq_resume);
2073#define SND_ENSONIQ_PM_OPS &snd_ensoniq_pm
2074#else
2075#define SND_ENSONIQ_PM_OPS NULL
2076#endif
2077
2078static int snd_ensoniq_create(struct snd_card *card,
2079 struct pci_dev *pci,
2080 struct ensoniq **rensoniq)
2081{
2082 struct ensoniq *ensoniq;
2083 int err;
2084 static struct snd_device_ops ops = {
2085 .dev_free = snd_ensoniq_dev_free,
2086 };
2087
2088 *rensoniq = NULL;
2089 if ((err = pci_enable_device(pci)) < 0)
2090 return err;
2091 ensoniq = kzalloc(sizeof(*ensoniq), GFP_KERNEL);
2092 if (ensoniq == NULL) {
2093 pci_disable_device(pci);
2094 return -ENOMEM;
2095 }
2096 spin_lock_init(&ensoniq->reg_lock);
2097 mutex_init(&ensoniq->src_mutex);
2098 ensoniq->card = card;
2099 ensoniq->pci = pci;
2100 ensoniq->irq = -1;
2101 if ((err = pci_request_regions(pci, "Ensoniq AudioPCI")) < 0) {
2102 kfree(ensoniq);
2103 pci_disable_device(pci);
2104 return err;
2105 }
2106 ensoniq->port = pci_resource_start(pci, 0);
2107 if (request_irq(pci->irq, snd_audiopci_interrupt, IRQF_SHARED,
2108 KBUILD_MODNAME, ensoniq)) {
2109 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2110 snd_ensoniq_free(ensoniq);
2111 return -EBUSY;
2112 }
2113 ensoniq->irq = pci->irq;
2114#ifdef CHIP1370
2115 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2116 16, &ensoniq->dma_bug) < 0) {
2117 dev_err(card->dev, "unable to allocate space for phantom area - dma_bug\n");
2118 snd_ensoniq_free(ensoniq);
2119 return -EBUSY;
2120 }
2121#endif
2122 pci_set_master(pci);
2123 ensoniq->rev = pci->revision;
2124#ifdef CHIP1370
2125#if 0
2126 ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE |
2127 ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
2128#else
2129 ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
2130#endif
2131 ensoniq->sctrl = 0;
2132#else
2133 ensoniq->ctrl = 0;
2134 ensoniq->sctrl = 0;
2135 ensoniq->cssr = 0;
2136 if (snd_pci_quirk_lookup(pci, es1371_amplifier_hack))
2137 ensoniq->ctrl |= ES_1371_GPIO_OUT(1);
2138
2139 if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack))
2140 ensoniq->cssr |= ES_1371_ST_AC97_RST;
2141#endif
2142
2143 snd_ensoniq_chip_init(ensoniq);
2144
2145 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ensoniq, &ops)) < 0) {
2146 snd_ensoniq_free(ensoniq);
2147 return err;
2148 }
2149
2150 snd_ensoniq_proc_init(ensoniq);
2151
2152 *rensoniq = ensoniq;
2153 return 0;
2154}
2155
2156
2157
2158
2159
2160static void snd_ensoniq_midi_interrupt(struct ensoniq * ensoniq)
2161{
2162 struct snd_rawmidi *rmidi = ensoniq->rmidi;
2163 unsigned char status, mask, byte;
2164
2165 if (rmidi == NULL)
2166 return;
2167
2168 spin_lock(&ensoniq->reg_lock);
2169 mask = ensoniq->uartm & ES_MODE_INPUT ? ES_RXRDY : 0;
2170 while (mask) {
2171 status = inb(ES_REG(ensoniq, UART_STATUS));
2172 if ((status & mask) == 0)
2173 break;
2174 byte = inb(ES_REG(ensoniq, UART_DATA));
2175 snd_rawmidi_receive(ensoniq->midi_input, &byte, 1);
2176 }
2177 spin_unlock(&ensoniq->reg_lock);
2178
2179
2180 spin_lock(&ensoniq->reg_lock);
2181 mask = ensoniq->uartm & ES_MODE_OUTPUT ? ES_TXRDY : 0;
2182 while (mask) {
2183 status = inb(ES_REG(ensoniq, UART_STATUS));
2184 if ((status & mask) == 0)
2185 break;
2186 if (snd_rawmidi_transmit(ensoniq->midi_output, &byte, 1) != 1) {
2187 ensoniq->uartc &= ~ES_TXINTENM;
2188 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2189 mask &= ~ES_TXRDY;
2190 } else {
2191 outb(byte, ES_REG(ensoniq, UART_DATA));
2192 }
2193 }
2194 spin_unlock(&ensoniq->reg_lock);
2195}
2196
2197static int snd_ensoniq_midi_input_open(struct snd_rawmidi_substream *substream)
2198{
2199 struct ensoniq *ensoniq = substream->rmidi->private_data;
2200
2201 spin_lock_irq(&ensoniq->reg_lock);
2202 ensoniq->uartm |= ES_MODE_INPUT;
2203 ensoniq->midi_input = substream;
2204 if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
2205 outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
2206 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2207 outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
2208 }
2209 spin_unlock_irq(&ensoniq->reg_lock);
2210 return 0;
2211}
2212
2213static int snd_ensoniq_midi_input_close(struct snd_rawmidi_substream *substream)
2214{
2215 struct ensoniq *ensoniq = substream->rmidi->private_data;
2216
2217 spin_lock_irq(&ensoniq->reg_lock);
2218 if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
2219 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2220 outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
2221 } else {
2222 outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
2223 }
2224 ensoniq->midi_input = NULL;
2225 ensoniq->uartm &= ~ES_MODE_INPUT;
2226 spin_unlock_irq(&ensoniq->reg_lock);
2227 return 0;
2228}
2229
2230static int snd_ensoniq_midi_output_open(struct snd_rawmidi_substream *substream)
2231{
2232 struct ensoniq *ensoniq = substream->rmidi->private_data;
2233
2234 spin_lock_irq(&ensoniq->reg_lock);
2235 ensoniq->uartm |= ES_MODE_OUTPUT;
2236 ensoniq->midi_output = substream;
2237 if (!(ensoniq->uartm & ES_MODE_INPUT)) {
2238 outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
2239 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2240 outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
2241 }
2242 spin_unlock_irq(&ensoniq->reg_lock);
2243 return 0;
2244}
2245
2246static int snd_ensoniq_midi_output_close(struct snd_rawmidi_substream *substream)
2247{
2248 struct ensoniq *ensoniq = substream->rmidi->private_data;
2249
2250 spin_lock_irq(&ensoniq->reg_lock);
2251 if (!(ensoniq->uartm & ES_MODE_INPUT)) {
2252 outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
2253 outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
2254 } else {
2255 outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
2256 }
2257 ensoniq->midi_output = NULL;
2258 ensoniq->uartm &= ~ES_MODE_OUTPUT;
2259 spin_unlock_irq(&ensoniq->reg_lock);
2260 return 0;
2261}
2262
2263static void snd_ensoniq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
2264{
2265 unsigned long flags;
2266 struct ensoniq *ensoniq = substream->rmidi->private_data;
2267 int idx;
2268
2269 spin_lock_irqsave(&ensoniq->reg_lock, flags);
2270 if (up) {
2271 if ((ensoniq->uartc & ES_RXINTEN) == 0) {
2272
2273 for (idx = 0; idx < 32; idx++)
2274 inb(ES_REG(ensoniq, UART_DATA));
2275 ensoniq->uartc |= ES_RXINTEN;
2276 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2277 }
2278 } else {
2279 if (ensoniq->uartc & ES_RXINTEN) {
2280 ensoniq->uartc &= ~ES_RXINTEN;
2281 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2282 }
2283 }
2284 spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
2285}
2286
2287static void snd_ensoniq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
2288{
2289 unsigned long flags;
2290 struct ensoniq *ensoniq = substream->rmidi->private_data;
2291 unsigned char byte;
2292
2293 spin_lock_irqsave(&ensoniq->reg_lock, flags);
2294 if (up) {
2295 if (ES_TXINTENI(ensoniq->uartc) == 0) {
2296 ensoniq->uartc |= ES_TXINTENO(1);
2297
2298 while (ES_TXINTENI(ensoniq->uartc) == 1 &&
2299 (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
2300 if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
2301 ensoniq->uartc &= ~ES_TXINTENM;
2302 } else {
2303 outb(byte, ES_REG(ensoniq, UART_DATA));
2304 }
2305 }
2306 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2307 }
2308 } else {
2309 if (ES_TXINTENI(ensoniq->uartc) == 1) {
2310 ensoniq->uartc &= ~ES_TXINTENM;
2311 outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
2312 }
2313 }
2314 spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
2315}
2316
2317static const struct snd_rawmidi_ops snd_ensoniq_midi_output =
2318{
2319 .open = snd_ensoniq_midi_output_open,
2320 .close = snd_ensoniq_midi_output_close,
2321 .trigger = snd_ensoniq_midi_output_trigger,
2322};
2323
2324static const struct snd_rawmidi_ops snd_ensoniq_midi_input =
2325{
2326 .open = snd_ensoniq_midi_input_open,
2327 .close = snd_ensoniq_midi_input_close,
2328 .trigger = snd_ensoniq_midi_input_trigger,
2329};
2330
2331static int snd_ensoniq_midi(struct ensoniq *ensoniq, int device)
2332{
2333 struct snd_rawmidi *rmidi;
2334 int err;
2335
2336 if ((err = snd_rawmidi_new(ensoniq->card, "ES1370/1", device, 1, 1, &rmidi)) < 0)
2337 return err;
2338 strcpy(rmidi->name, CHIP_NAME);
2339 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_ensoniq_midi_output);
2340 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_ensoniq_midi_input);
2341 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
2342 SNDRV_RAWMIDI_INFO_DUPLEX;
2343 rmidi->private_data = ensoniq;
2344 ensoniq->rmidi = rmidi;
2345 return 0;
2346}
2347
2348
2349
2350
2351
2352static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id)
2353{
2354 struct ensoniq *ensoniq = dev_id;
2355 unsigned int status, sctrl;
2356
2357 if (ensoniq == NULL)
2358 return IRQ_NONE;
2359
2360 status = inl(ES_REG(ensoniq, STATUS));
2361 if (!(status & ES_INTR))
2362 return IRQ_NONE;
2363
2364 spin_lock(&ensoniq->reg_lock);
2365 sctrl = ensoniq->sctrl;
2366 if (status & ES_DAC1)
2367 sctrl &= ~ES_P1_INT_EN;
2368 if (status & ES_DAC2)
2369 sctrl &= ~ES_P2_INT_EN;
2370 if (status & ES_ADC)
2371 sctrl &= ~ES_R1_INT_EN;
2372 outl(sctrl, ES_REG(ensoniq, SERIAL));
2373 outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
2374 spin_unlock(&ensoniq->reg_lock);
2375
2376 if (status & ES_UART)
2377 snd_ensoniq_midi_interrupt(ensoniq);
2378 if ((status & ES_DAC2) && ensoniq->playback2_substream)
2379 snd_pcm_period_elapsed(ensoniq->playback2_substream);
2380 if ((status & ES_ADC) && ensoniq->capture_substream)
2381 snd_pcm_period_elapsed(ensoniq->capture_substream);
2382 if ((status & ES_DAC1) && ensoniq->playback1_substream)
2383 snd_pcm_period_elapsed(ensoniq->playback1_substream);
2384 return IRQ_HANDLED;
2385}
2386
2387static int snd_audiopci_probe(struct pci_dev *pci,
2388 const struct pci_device_id *pci_id)
2389{
2390 static int dev;
2391 struct snd_card *card;
2392 struct ensoniq *ensoniq;
2393 int err;
2394
2395 if (dev >= SNDRV_CARDS)
2396 return -ENODEV;
2397 if (!enable[dev]) {
2398 dev++;
2399 return -ENOENT;
2400 }
2401
2402 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2403 0, &card);
2404 if (err < 0)
2405 return err;
2406
2407 if ((err = snd_ensoniq_create(card, pci, &ensoniq)) < 0) {
2408 snd_card_free(card);
2409 return err;
2410 }
2411 card->private_data = ensoniq;
2412
2413#ifdef CHIP1370
2414 if ((err = snd_ensoniq_1370_mixer(ensoniq)) < 0) {
2415 snd_card_free(card);
2416 return err;
2417 }
2418#endif
2419#ifdef CHIP1371
2420 if ((err = snd_ensoniq_1371_mixer(ensoniq, spdif[dev], lineio[dev])) < 0) {
2421 snd_card_free(card);
2422 return err;
2423 }
2424#endif
2425 if ((err = snd_ensoniq_pcm(ensoniq, 0)) < 0) {
2426 snd_card_free(card);
2427 return err;
2428 }
2429 if ((err = snd_ensoniq_pcm2(ensoniq, 1)) < 0) {
2430 snd_card_free(card);
2431 return err;
2432 }
2433 if ((err = snd_ensoniq_midi(ensoniq, 0)) < 0) {
2434 snd_card_free(card);
2435 return err;
2436 }
2437
2438 snd_ensoniq_create_gameport(ensoniq, dev);
2439
2440 strcpy(card->driver, DRIVER_NAME);
2441
2442 strcpy(card->shortname, "Ensoniq AudioPCI");
2443 sprintf(card->longname, "%s %s at 0x%lx, irq %i",
2444 card->shortname,
2445 card->driver,
2446 ensoniq->port,
2447 ensoniq->irq);
2448
2449 if ((err = snd_card_register(card)) < 0) {
2450 snd_card_free(card);
2451 return err;
2452 }
2453
2454 pci_set_drvdata(pci, card);
2455 dev++;
2456 return 0;
2457}
2458
2459static void snd_audiopci_remove(struct pci_dev *pci)
2460{
2461 snd_card_free(pci_get_drvdata(pci));
2462}
2463
2464static struct pci_driver ens137x_driver = {
2465 .name = KBUILD_MODNAME,
2466 .id_table = snd_audiopci_ids,
2467 .probe = snd_audiopci_probe,
2468 .remove = snd_audiopci_remove,
2469 .driver = {
2470 .pm = SND_ENSONIQ_PM_OPS,
2471 },
2472};
2473
2474module_pci_driver(ens137x_driver);
2475