Linux v2.6.13
[linux-2.6/next.git] / sound / isa / es18xx.c
blob1d832b2adb7ca34ad93863927d3c2ec03413db4a
1 /*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* GENERAL NOTES:
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
38 * - The driver use dma2 for playback and dma1 for capture.
42 * ES1869 NOTES:
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
52 * BUGS:
54 * - There is a major trouble I noted:
56 * using both channel for playback stereo 16 bit samples at 44100 Hz
57 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
59 * The same happens using Audio1 for captureing.
61 * The Windows driver does not suffer of this (although it use Audio1
62 * only for captureing). I'm unable to discover why.
67 #include <sound/driver.h>
68 #include <asm/io.h>
69 #include <asm/dma.h>
70 #include <linux/init.h>
71 #include <linux/pm.h>
72 #include <linux/slab.h>
73 #include <linux/pnp.h>
74 #include <linux/isapnp.h>
75 #include <linux/moduleparam.h>
76 #include <sound/core.h>
77 #include <sound/control.h>
78 #include <sound/pcm.h>
79 #include <sound/pcm_params.h>
80 #include <sound/mpu401.h>
81 #include <sound/opl3.h>
82 #define SNDRV_LEGACY_AUTO_PROBE
83 #define SNDRV_LEGACY_FIND_FREE_IRQ
84 #define SNDRV_LEGACY_FIND_FREE_DMA
85 #include <sound/initval.h>
87 #define PFX "es18xx: "
89 struct _snd_es18xx {
90 unsigned long port; /* port of ESS chip */
91 unsigned long mpu_port; /* MPU-401 port of ESS chip */
92 unsigned long fm_port; /* FM port */
93 unsigned long ctrl_port; /* Control port of ESS chip */
94 struct resource *res_port;
95 struct resource *res_mpu_port;
96 struct resource *res_ctrl_port;
97 int irq; /* IRQ number of ESS chip */
98 int dma1; /* DMA1 */
99 int dma2; /* DMA2 */
100 unsigned short version; /* version of ESS chip */
101 int caps; /* Chip capabilities */
102 unsigned short audio2_vol; /* volume level of audio2 */
104 unsigned short active; /* active channel mask */
105 unsigned int dma1_size;
106 unsigned int dma2_size;
107 unsigned int dma1_shift;
108 unsigned int dma2_shift;
110 snd_card_t *card;
111 snd_pcm_t *pcm;
112 snd_pcm_substream_t *playback_a_substream;
113 snd_pcm_substream_t *capture_a_substream;
114 snd_pcm_substream_t *playback_b_substream;
116 snd_rawmidi_t *rmidi;
118 snd_kcontrol_t *hw_volume;
119 snd_kcontrol_t *hw_switch;
120 snd_kcontrol_t *master_volume;
121 snd_kcontrol_t *master_switch;
123 spinlock_t reg_lock;
124 spinlock_t mixer_lock;
125 spinlock_t ctrl_lock;
126 #ifdef CONFIG_PM
127 unsigned char pm_reg;
128 #endif
131 #define AUDIO1_IRQ 0x01
132 #define AUDIO2_IRQ 0x02
133 #define HWV_IRQ 0x04
134 #define MPU_IRQ 0x08
136 #define ES18XX_PCM2 0x0001 /* Has two useable PCM */
137 #define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
138 #define ES18XX_RECMIX 0x0004 /* Has record mixer */
139 #define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
140 #define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
141 #define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
142 #define ES18XX_AUXB 0x0040 /* AuxB mixer control */
143 #define ES18XX_HWV 0x0080 /* Has hardware volume */
144 #define ES18XX_MONO 0x0100 /* Mono_in mixer control */
145 #define ES18XX_I2S 0x0200 /* I2S mixer control */
146 #define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
147 #define ES18XX_CONTROL 0x0800 /* Has control ports */
149 /* Power Management */
150 #define ES18XX_PM 0x07
151 #define ES18XX_PM_GPO0 0x01
152 #define ES18XX_PM_GPO1 0x02
153 #define ES18XX_PM_PDR 0x04
154 #define ES18XX_PM_ANA 0x08
155 #define ES18XX_PM_FM 0x020
156 #define ES18XX_PM_SUS 0x080
158 typedef struct _snd_es18xx es18xx_t;
160 /* Lowlevel */
162 #define DAC1 0x01
163 #define ADC1 0x02
164 #define DAC2 0x04
165 #define MILLISECOND 10000
167 static int snd_es18xx_dsp_command(es18xx_t *chip, unsigned char val)
169 int i;
171 for(i = MILLISECOND; i; i--)
172 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
173 outb(val, chip->port + 0x0C);
174 return 0;
176 snd_printk("dsp_command: timeout (0x%x)\n", val);
177 return -EINVAL;
180 static int snd_es18xx_dsp_get_byte(es18xx_t *chip)
182 int i;
184 for(i = MILLISECOND/10; i; i--)
185 if (inb(chip->port + 0x0C) & 0x40)
186 return inb(chip->port + 0x0A);
187 snd_printk("dsp_get_byte failed: 0x%lx = 0x%x!!!\n", chip->port + 0x0A, inb(chip->port + 0x0A));
188 return -ENODEV;
191 #undef REG_DEBUG
193 static int snd_es18xx_write(es18xx_t *chip,
194 unsigned char reg, unsigned char data)
196 unsigned long flags;
197 int ret;
199 spin_lock_irqsave(&chip->reg_lock, flags);
200 ret = snd_es18xx_dsp_command(chip, reg);
201 if (ret < 0)
202 goto end;
203 ret = snd_es18xx_dsp_command(chip, data);
204 end:
205 spin_unlock_irqrestore(&chip->reg_lock, flags);
206 #ifdef REG_DEBUG
207 snd_printk("Reg %02x set to %02x\n", reg, data);
208 #endif
209 return ret;
212 static int snd_es18xx_read(es18xx_t *chip, unsigned char reg)
214 unsigned long flags;
215 int ret, data;
216 spin_lock_irqsave(&chip->reg_lock, flags);
217 ret = snd_es18xx_dsp_command(chip, 0xC0);
218 if (ret < 0)
219 goto end;
220 ret = snd_es18xx_dsp_command(chip, reg);
221 if (ret < 0)
222 goto end;
223 data = snd_es18xx_dsp_get_byte(chip);
224 ret = data;
225 #ifdef REG_DEBUG
226 snd_printk("Reg %02x now is %02x (%d)\n", reg, data, ret);
227 #endif
228 end:
229 spin_unlock_irqrestore(&chip->reg_lock, flags);
230 return ret;
233 /* Return old value */
234 static int snd_es18xx_bits(es18xx_t *chip, unsigned char reg,
235 unsigned char mask, unsigned char val)
237 int ret;
238 unsigned char old, new, oval;
239 unsigned long flags;
240 spin_lock_irqsave(&chip->reg_lock, flags);
241 ret = snd_es18xx_dsp_command(chip, 0xC0);
242 if (ret < 0)
243 goto end;
244 ret = snd_es18xx_dsp_command(chip, reg);
245 if (ret < 0)
246 goto end;
247 ret = snd_es18xx_dsp_get_byte(chip);
248 if (ret < 0) {
249 goto end;
251 old = ret;
252 oval = old & mask;
253 if (val != oval) {
254 ret = snd_es18xx_dsp_command(chip, reg);
255 if (ret < 0)
256 goto end;
257 new = (old & ~mask) | (val & mask);
258 ret = snd_es18xx_dsp_command(chip, new);
259 if (ret < 0)
260 goto end;
261 #ifdef REG_DEBUG
262 snd_printk("Reg %02x was %02x, set to %02x (%d)\n", reg, old, new, ret);
263 #endif
265 ret = oval;
266 end:
267 spin_unlock_irqrestore(&chip->reg_lock, flags);
268 return ret;
271 static inline void snd_es18xx_mixer_write(es18xx_t *chip,
272 unsigned char reg, unsigned char data)
274 unsigned long flags;
275 spin_lock_irqsave(&chip->mixer_lock, flags);
276 outb(reg, chip->port + 0x04);
277 outb(data, chip->port + 0x05);
278 spin_unlock_irqrestore(&chip->mixer_lock, flags);
279 #ifdef REG_DEBUG
280 snd_printk("Mixer reg %02x set to %02x\n", reg, data);
281 #endif
284 static inline int snd_es18xx_mixer_read(es18xx_t *chip, unsigned char reg)
286 unsigned long flags;
287 int data;
288 spin_lock_irqsave(&chip->mixer_lock, flags);
289 outb(reg, chip->port + 0x04);
290 data = inb(chip->port + 0x05);
291 spin_unlock_irqrestore(&chip->mixer_lock, flags);
292 #ifdef REG_DEBUG
293 snd_printk("Mixer reg %02x now is %02x\n", reg, data);
294 #endif
295 return data;
298 /* Return old value */
299 static inline int snd_es18xx_mixer_bits(es18xx_t *chip, unsigned char reg,
300 unsigned char mask, unsigned char val)
302 unsigned char old, new, oval;
303 unsigned long flags;
304 spin_lock_irqsave(&chip->mixer_lock, flags);
305 outb(reg, chip->port + 0x04);
306 old = inb(chip->port + 0x05);
307 oval = old & mask;
308 if (val != oval) {
309 new = (old & ~mask) | (val & mask);
310 outb(new, chip->port + 0x05);
311 #ifdef REG_DEBUG
312 snd_printk("Mixer reg %02x was %02x, set to %02x\n", reg, old, new);
313 #endif
315 spin_unlock_irqrestore(&chip->mixer_lock, flags);
316 return oval;
319 static inline int snd_es18xx_mixer_writable(es18xx_t *chip, unsigned char reg,
320 unsigned char mask)
322 int old, expected, new;
323 unsigned long flags;
324 spin_lock_irqsave(&chip->mixer_lock, flags);
325 outb(reg, chip->port + 0x04);
326 old = inb(chip->port + 0x05);
327 expected = old ^ mask;
328 outb(expected, chip->port + 0x05);
329 new = inb(chip->port + 0x05);
330 spin_unlock_irqrestore(&chip->mixer_lock, flags);
331 #ifdef REG_DEBUG
332 snd_printk("Mixer reg %02x was %02x, set to %02x, now is %02x\n", reg, old, expected, new);
333 #endif
334 return expected == new;
338 static int snd_es18xx_reset(es18xx_t *chip)
340 int i;
341 outb(0x03, chip->port + 0x06);
342 inb(chip->port + 0x06);
343 outb(0x00, chip->port + 0x06);
344 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
345 if (inb(chip->port + 0x0A) != 0xAA)
346 return -1;
347 return 0;
350 static int snd_es18xx_reset_fifo(es18xx_t *chip)
352 outb(0x02, chip->port + 0x06);
353 inb(chip->port + 0x06);
354 outb(0x00, chip->port + 0x06);
355 return 0;
358 static ratnum_t new_clocks[2] = {
360 .num = 793800,
361 .den_min = 1,
362 .den_max = 128,
363 .den_step = 1,
366 .num = 768000,
367 .den_min = 1,
368 .den_max = 128,
369 .den_step = 1,
373 static snd_pcm_hw_constraint_ratnums_t new_hw_constraints_clocks = {
374 .nrats = 2,
375 .rats = new_clocks,
378 static ratnum_t old_clocks[2] = {
380 .num = 795444,
381 .den_min = 1,
382 .den_max = 128,
383 .den_step = 1,
386 .num = 397722,
387 .den_min = 1,
388 .den_max = 128,
389 .den_step = 1,
393 static snd_pcm_hw_constraint_ratnums_t old_hw_constraints_clocks = {
394 .nrats = 2,
395 .rats = old_clocks,
399 static void snd_es18xx_rate_set(es18xx_t *chip,
400 snd_pcm_substream_t *substream,
401 int mode)
403 unsigned int bits, div0;
404 snd_pcm_runtime_t *runtime = substream->runtime;
405 if (chip->caps & ES18XX_NEW_RATE) {
406 if (runtime->rate_num == new_clocks[0].num)
407 bits = 128 - runtime->rate_den;
408 else
409 bits = 256 - runtime->rate_den;
410 } else {
411 if (runtime->rate_num == old_clocks[0].num)
412 bits = 256 - runtime->rate_den;
413 else
414 bits = 128 - runtime->rate_den;
417 /* set filter register */
418 div0 = 256 - 7160000*20/(8*82*runtime->rate);
420 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
421 snd_es18xx_mixer_write(chip, 0x70, bits);
423 * Comment from kernel oss driver:
424 * FKS: fascinating: 0x72 doesn't seem to work.
426 snd_es18xx_write(chip, 0xA2, div0);
427 snd_es18xx_mixer_write(chip, 0x72, div0);
428 } else {
429 snd_es18xx_write(chip, 0xA1, bits);
430 snd_es18xx_write(chip, 0xA2, div0);
434 static int snd_es18xx_playback_hw_params(snd_pcm_substream_t * substream,
435 snd_pcm_hw_params_t * hw_params)
437 es18xx_t *chip = snd_pcm_substream_chip(substream);
438 int shift, err;
440 shift = 0;
441 if (params_channels(hw_params) == 2)
442 shift++;
443 if (snd_pcm_format_width(params_format(hw_params)) == 16)
444 shift++;
446 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
447 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
448 (chip->capture_a_substream) &&
449 params_channels(hw_params) != 1) {
450 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
451 return -EBUSY;
453 chip->dma2_shift = shift;
454 } else {
455 chip->dma1_shift = shift;
457 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
458 return err;
459 return 0;
462 static int snd_es18xx_pcm_hw_free(snd_pcm_substream_t * substream)
464 return snd_pcm_lib_free_pages(substream);
467 static int snd_es18xx_playback1_prepare(es18xx_t *chip,
468 snd_pcm_substream_t *substream)
470 snd_pcm_runtime_t *runtime = substream->runtime;
471 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
472 unsigned int count = snd_pcm_lib_period_bytes(substream);
474 chip->dma2_size = size;
476 snd_es18xx_rate_set(chip, substream, DAC2);
478 /* Transfer Count Reload */
479 count = 0x10000 - count;
480 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
481 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
483 /* Set format */
484 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
485 ((runtime->channels == 1) ? 0x00 : 0x02) |
486 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
487 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
489 /* Set DMA controller */
490 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
492 return 0;
495 static int snd_es18xx_playback1_trigger(es18xx_t *chip,
496 snd_pcm_substream_t * substream,
497 int cmd)
499 switch (cmd) {
500 case SNDRV_PCM_TRIGGER_START:
501 case SNDRV_PCM_TRIGGER_RESUME:
502 if (chip->active & DAC2)
503 return 0;
504 chip->active |= DAC2;
505 /* Start DMA */
506 if (chip->dma2 >= 4)
507 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
508 else
509 snd_es18xx_mixer_write(chip, 0x78, 0x93);
510 #ifdef AVOID_POPS
511 /* Avoid pops */
512 udelay(100000);
513 if (chip->caps & ES18XX_PCM2)
514 /* Restore Audio 2 volume */
515 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
516 else
517 /* Enable PCM output */
518 snd_es18xx_dsp_command(chip, 0xD1);
519 #endif
520 break;
521 case SNDRV_PCM_TRIGGER_STOP:
522 case SNDRV_PCM_TRIGGER_SUSPEND:
523 if (!(chip->active & DAC2))
524 return 0;
525 chip->active &= ~DAC2;
526 /* Stop DMA */
527 snd_es18xx_mixer_write(chip, 0x78, 0x00);
528 #ifdef AVOID_POPS
529 udelay(25000);
530 if (chip->caps & ES18XX_PCM2)
531 /* Set Audio 2 volume to 0 */
532 snd_es18xx_mixer_write(chip, 0x7C, 0);
533 else
534 /* Disable PCM output */
535 snd_es18xx_dsp_command(chip, 0xD3);
536 #endif
537 break;
538 default:
539 return -EINVAL;
542 return 0;
545 static int snd_es18xx_capture_hw_params(snd_pcm_substream_t * substream,
546 snd_pcm_hw_params_t * hw_params)
548 es18xx_t *chip = snd_pcm_substream_chip(substream);
549 int shift, err;
551 shift = 0;
552 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
553 chip->playback_a_substream &&
554 params_channels(hw_params) != 1) {
555 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
556 return -EBUSY;
558 if (params_channels(hw_params) == 2)
559 shift++;
560 if (snd_pcm_format_width(params_format(hw_params)) == 16)
561 shift++;
562 chip->dma1_shift = shift;
563 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
564 return err;
565 return 0;
568 static int snd_es18xx_capture_prepare(snd_pcm_substream_t *substream)
570 es18xx_t *chip = snd_pcm_substream_chip(substream);
571 snd_pcm_runtime_t *runtime = substream->runtime;
572 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
573 unsigned int count = snd_pcm_lib_period_bytes(substream);
575 chip->dma1_size = size;
577 snd_es18xx_reset_fifo(chip);
579 /* Set stereo/mono */
580 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
582 snd_es18xx_rate_set(chip, substream, ADC1);
584 /* Transfer Count Reload */
585 count = 0x10000 - count;
586 snd_es18xx_write(chip, 0xA4, count & 0xff);
587 snd_es18xx_write(chip, 0xA5, count >> 8);
589 #ifdef AVOID_POPS
590 udelay(100000);
591 #endif
593 /* Set format */
594 snd_es18xx_write(chip, 0xB7,
595 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
596 snd_es18xx_write(chip, 0xB7, 0x90 |
597 ((runtime->channels == 1) ? 0x40 : 0x08) |
598 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
599 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
601 /* Set DMA controler */
602 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
604 return 0;
607 static int snd_es18xx_capture_trigger(snd_pcm_substream_t *substream,
608 int cmd)
610 es18xx_t *chip = snd_pcm_substream_chip(substream);
612 switch (cmd) {
613 case SNDRV_PCM_TRIGGER_START:
614 case SNDRV_PCM_TRIGGER_RESUME:
615 if (chip->active & ADC1)
616 return 0;
617 chip->active |= ADC1;
618 /* Start DMA */
619 snd_es18xx_write(chip, 0xB8, 0x0f);
620 break;
621 case SNDRV_PCM_TRIGGER_STOP:
622 case SNDRV_PCM_TRIGGER_SUSPEND:
623 if (!(chip->active & ADC1))
624 return 0;
625 chip->active &= ~ADC1;
626 /* Stop DMA */
627 snd_es18xx_write(chip, 0xB8, 0x00);
628 break;
629 default:
630 return -EINVAL;
633 return 0;
636 static int snd_es18xx_playback2_prepare(es18xx_t *chip,
637 snd_pcm_substream_t *substream)
639 snd_pcm_runtime_t *runtime = substream->runtime;
640 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
641 unsigned int count = snd_pcm_lib_period_bytes(substream);
643 chip->dma1_size = size;
645 snd_es18xx_reset_fifo(chip);
647 /* Set stereo/mono */
648 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
650 snd_es18xx_rate_set(chip, substream, DAC1);
652 /* Transfer Count Reload */
653 count = 0x10000 - count;
654 snd_es18xx_write(chip, 0xA4, count & 0xff);
655 snd_es18xx_write(chip, 0xA5, count >> 8);
657 /* Set format */
658 snd_es18xx_write(chip, 0xB6,
659 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
660 snd_es18xx_write(chip, 0xB7,
661 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
662 snd_es18xx_write(chip, 0xB7, 0x90 |
663 (runtime->channels == 1 ? 0x40 : 0x08) |
664 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
665 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
667 /* Set DMA controler */
668 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
670 return 0;
673 static int snd_es18xx_playback2_trigger(es18xx_t *chip,
674 snd_pcm_substream_t *substream,
675 int cmd)
677 switch (cmd) {
678 case SNDRV_PCM_TRIGGER_START:
679 case SNDRV_PCM_TRIGGER_RESUME:
680 if (chip->active & DAC1)
681 return 0;
682 chip->active |= DAC1;
683 /* Start DMA */
684 snd_es18xx_write(chip, 0xB8, 0x05);
685 #ifdef AVOID_POPS
686 /* Avoid pops */
687 udelay(100000);
688 /* Enable Audio 1 */
689 snd_es18xx_dsp_command(chip, 0xD1);
690 #endif
691 break;
692 case SNDRV_PCM_TRIGGER_STOP:
693 case SNDRV_PCM_TRIGGER_SUSPEND:
694 if (!(chip->active & DAC1))
695 return 0;
696 chip->active &= ~DAC1;
697 /* Stop DMA */
698 snd_es18xx_write(chip, 0xB8, 0x00);
699 #ifdef AVOID_POPS
700 /* Avoid pops */
701 udelay(25000);
702 /* Disable Audio 1 */
703 snd_es18xx_dsp_command(chip, 0xD3);
704 #endif
705 break;
706 default:
707 return -EINVAL;
710 return 0;
713 static int snd_es18xx_playback_prepare(snd_pcm_substream_t *substream)
715 es18xx_t *chip = snd_pcm_substream_chip(substream);
716 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
717 return snd_es18xx_playback1_prepare(chip, substream);
718 else
719 return snd_es18xx_playback2_prepare(chip, substream);
722 static int snd_es18xx_playback_trigger(snd_pcm_substream_t *substream,
723 int cmd)
725 es18xx_t *chip = snd_pcm_substream_chip(substream);
726 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
727 return snd_es18xx_playback1_trigger(chip, substream, cmd);
728 else
729 return snd_es18xx_playback2_trigger(chip, substream, cmd);
732 static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
734 es18xx_t *chip = dev_id;
735 unsigned char status;
737 if (chip->caps & ES18XX_CONTROL) {
738 /* Read Interrupt status */
739 status = inb(chip->ctrl_port + 6);
740 } else {
741 /* Read Interrupt status */
742 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
744 #if 0
745 else {
746 status = 0;
747 if (inb(chip->port + 0x0C) & 0x01)
748 status |= AUDIO1_IRQ;
749 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
750 status |= AUDIO2_IRQ;
751 if ((chip->caps & ES18XX_HWV) &&
752 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
753 status |= HWV_IRQ;
755 #endif
757 /* Audio 1 & Audio 2 */
758 if (status & AUDIO2_IRQ) {
759 if (chip->active & DAC2)
760 snd_pcm_period_elapsed(chip->playback_a_substream);
761 /* ack interrupt */
762 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
764 if (status & AUDIO1_IRQ) {
765 /* ok.. capture is active */
766 if (chip->active & ADC1)
767 snd_pcm_period_elapsed(chip->capture_a_substream);
768 /* ok.. playback2 is active */
769 else if (chip->active & DAC1)
770 snd_pcm_period_elapsed(chip->playback_b_substream);
771 /* ack interrupt */
772 inb(chip->port + 0x0E);
775 /* MPU */
776 if ((status & MPU_IRQ) && chip->rmidi)
777 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
779 /* Hardware volume */
780 if (status & HWV_IRQ) {
781 int split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
782 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
783 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
784 if (!split) {
785 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
786 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
788 /* ack interrupt */
789 snd_es18xx_mixer_write(chip, 0x66, 0x00);
791 return IRQ_HANDLED;
794 static snd_pcm_uframes_t snd_es18xx_playback_pointer(snd_pcm_substream_t * substream)
796 es18xx_t *chip = snd_pcm_substream_chip(substream);
797 int pos;
799 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
800 if (!(chip->active & DAC2))
801 return 0;
802 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
803 return pos >> chip->dma2_shift;
804 } else {
805 if (!(chip->active & DAC1))
806 return 0;
807 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
808 return pos >> chip->dma1_shift;
812 static snd_pcm_uframes_t snd_es18xx_capture_pointer(snd_pcm_substream_t * substream)
814 es18xx_t *chip = snd_pcm_substream_chip(substream);
815 int pos;
817 if (!(chip->active & ADC1))
818 return 0;
819 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
820 return pos >> chip->dma1_shift;
823 static snd_pcm_hardware_t snd_es18xx_playback =
825 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
826 SNDRV_PCM_INFO_RESUME |
827 SNDRV_PCM_INFO_MMAP_VALID),
828 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
829 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
830 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
831 .rate_min = 4000,
832 .rate_max = 48000,
833 .channels_min = 1,
834 .channels_max = 2,
835 .buffer_bytes_max = 65536,
836 .period_bytes_min = 64,
837 .period_bytes_max = 65536,
838 .periods_min = 1,
839 .periods_max = 1024,
840 .fifo_size = 0,
843 static snd_pcm_hardware_t snd_es18xx_capture =
845 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
846 SNDRV_PCM_INFO_RESUME |
847 SNDRV_PCM_INFO_MMAP_VALID),
848 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
849 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
850 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
851 .rate_min = 4000,
852 .rate_max = 48000,
853 .channels_min = 1,
854 .channels_max = 2,
855 .buffer_bytes_max = 65536,
856 .period_bytes_min = 64,
857 .period_bytes_max = 65536,
858 .periods_min = 1,
859 .periods_max = 1024,
860 .fifo_size = 0,
863 static int snd_es18xx_playback_open(snd_pcm_substream_t * substream)
865 snd_pcm_runtime_t *runtime = substream->runtime;
866 es18xx_t *chip = snd_pcm_substream_chip(substream);
868 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
869 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
870 chip->capture_a_substream &&
871 chip->capture_a_substream->runtime->channels != 1)
872 return -EAGAIN;
873 chip->playback_a_substream = substream;
874 } else if (substream->number <= 1) {
875 if (chip->capture_a_substream)
876 return -EAGAIN;
877 chip->playback_b_substream = substream;
878 } else {
879 snd_BUG();
880 return -EINVAL;
882 substream->runtime->hw = snd_es18xx_playback;
883 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
884 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
885 return 0;
888 static int snd_es18xx_capture_open(snd_pcm_substream_t * substream)
890 snd_pcm_runtime_t *runtime = substream->runtime;
891 es18xx_t *chip = snd_pcm_substream_chip(substream);
893 if (chip->playback_b_substream)
894 return -EAGAIN;
895 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
896 chip->playback_a_substream &&
897 chip->playback_a_substream->runtime->channels != 1)
898 return -EAGAIN;
899 chip->capture_a_substream = substream;
900 substream->runtime->hw = snd_es18xx_capture;
901 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
902 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
903 return 0;
906 static int snd_es18xx_playback_close(snd_pcm_substream_t * substream)
908 es18xx_t *chip = snd_pcm_substream_chip(substream);
910 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
911 chip->playback_a_substream = NULL;
912 else
913 chip->playback_b_substream = NULL;
915 snd_pcm_lib_free_pages(substream);
916 return 0;
919 static int snd_es18xx_capture_close(snd_pcm_substream_t * substream)
921 es18xx_t *chip = snd_pcm_substream_chip(substream);
923 chip->capture_a_substream = NULL;
924 snd_pcm_lib_free_pages(substream);
925 return 0;
929 * MIXER part
932 static int snd_es18xx_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
934 static char *texts[8] = {
935 "Mic", "Mic Master", "CD", "AOUT",
936 "Mic1", "Mix", "Line", "Master"
939 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
940 uinfo->count = 1;
941 uinfo->value.enumerated.items = 8;
942 if (uinfo->value.enumerated.item > 7)
943 uinfo->value.enumerated.item = 7;
944 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
945 return 0;
948 static int snd_es18xx_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
950 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
951 ucontrol->value.enumerated.item[0] = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
952 return 0;
955 static int snd_es18xx_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
957 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
958 unsigned char val = ucontrol->value.enumerated.item[0];
960 if (val > 7)
961 return -EINVAL;
962 return snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val;
965 static int snd_es18xx_info_spatializer_enable(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
967 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
968 uinfo->count = 1;
969 uinfo->value.integer.min = 0;
970 uinfo->value.integer.max = 1;
971 return 0;
974 static int snd_es18xx_get_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
976 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
977 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
978 ucontrol->value.integer.value[0] = !!(val & 8);
979 return 0;
982 static int snd_es18xx_put_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
984 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
985 unsigned char oval, nval;
986 int change;
987 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
988 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
989 change = nval != oval;
990 if (change) {
991 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
992 snd_es18xx_mixer_write(chip, 0x50, nval);
994 return change;
997 static int snd_es18xx_info_hw_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
999 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1000 uinfo->count = 2;
1001 uinfo->value.integer.min = 0;
1002 uinfo->value.integer.max = 63;
1003 return 0;
1006 static int snd_es18xx_get_hw_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1008 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1009 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1010 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1011 return 0;
1014 static int snd_es18xx_info_hw_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1016 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1017 uinfo->count = 2;
1018 uinfo->value.integer.min = 0;
1019 uinfo->value.integer.max = 1;
1020 return 0;
1023 static int snd_es18xx_get_hw_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1025 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1026 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1027 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1028 return 0;
1031 static void snd_es18xx_hwv_free(snd_kcontrol_t *kcontrol)
1033 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1034 chip->master_volume = NULL;
1035 chip->master_switch = NULL;
1036 chip->hw_volume = NULL;
1037 chip->hw_switch = NULL;
1040 static int snd_es18xx_reg_bits(es18xx_t *chip, unsigned char reg,
1041 unsigned char mask, unsigned char val)
1043 if (reg < 0xa0)
1044 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1045 else
1046 return snd_es18xx_bits(chip, reg, mask, val);
1049 static int snd_es18xx_reg_read(es18xx_t *chip, unsigned char reg)
1051 if (reg < 0xa0)
1052 return snd_es18xx_mixer_read(chip, reg);
1053 else
1054 return snd_es18xx_read(chip, reg);
1057 #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1058 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1059 .info = snd_es18xx_info_single, \
1060 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1061 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1063 static int snd_es18xx_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1065 int mask = (kcontrol->private_value >> 16) & 0xff;
1067 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1068 uinfo->count = 1;
1069 uinfo->value.integer.min = 0;
1070 uinfo->value.integer.max = mask;
1071 return 0;
1074 static int snd_es18xx_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1076 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1077 int reg = kcontrol->private_value & 0xff;
1078 int shift = (kcontrol->private_value >> 8) & 0xff;
1079 int mask = (kcontrol->private_value >> 16) & 0xff;
1080 int invert = (kcontrol->private_value >> 24) & 0xff;
1081 int val;
1083 val = snd_es18xx_reg_read(chip, reg);
1084 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1085 if (invert)
1086 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1087 return 0;
1090 static int snd_es18xx_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1092 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1093 int reg = kcontrol->private_value & 0xff;
1094 int shift = (kcontrol->private_value >> 8) & 0xff;
1095 int mask = (kcontrol->private_value >> 16) & 0xff;
1096 int invert = (kcontrol->private_value >> 24) & 0xff;
1097 unsigned char val;
1099 val = (ucontrol->value.integer.value[0] & mask);
1100 if (invert)
1101 val = mask - val;
1102 mask <<= shift;
1103 val <<= shift;
1104 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1107 #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1108 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1109 .info = snd_es18xx_info_double, \
1110 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1111 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1113 static int snd_es18xx_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1115 int mask = (kcontrol->private_value >> 24) & 0xff;
1117 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1118 uinfo->count = 2;
1119 uinfo->value.integer.min = 0;
1120 uinfo->value.integer.max = mask;
1121 return 0;
1124 static int snd_es18xx_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1126 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1127 int left_reg = kcontrol->private_value & 0xff;
1128 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1129 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1130 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1131 int mask = (kcontrol->private_value >> 24) & 0xff;
1132 int invert = (kcontrol->private_value >> 22) & 1;
1133 unsigned char left, right;
1135 left = snd_es18xx_reg_read(chip, left_reg);
1136 if (left_reg != right_reg)
1137 right = snd_es18xx_reg_read(chip, right_reg);
1138 else
1139 right = left;
1140 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1141 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1142 if (invert) {
1143 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1144 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1146 return 0;
1149 static int snd_es18xx_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1151 es18xx_t *chip = snd_kcontrol_chip(kcontrol);
1152 int left_reg = kcontrol->private_value & 0xff;
1153 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1154 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1155 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1156 int mask = (kcontrol->private_value >> 24) & 0xff;
1157 int invert = (kcontrol->private_value >> 22) & 1;
1158 int change;
1159 unsigned char val1, val2, mask1, mask2;
1161 val1 = ucontrol->value.integer.value[0] & mask;
1162 val2 = ucontrol->value.integer.value[1] & mask;
1163 if (invert) {
1164 val1 = mask - val1;
1165 val2 = mask - val2;
1167 val1 <<= shift_left;
1168 val2 <<= shift_right;
1169 mask1 = mask << shift_left;
1170 mask2 = mask << shift_right;
1171 if (left_reg != right_reg) {
1172 change = 0;
1173 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1174 change = 1;
1175 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1176 change = 1;
1177 } else {
1178 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1179 val1 | val2) != (val1 | val2));
1181 return change;
1184 static snd_kcontrol_new_t snd_es18xx_base_controls[] = {
1185 ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1186 ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1187 ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1188 ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1189 ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1190 ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1191 ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1192 ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1193 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0),
1194 ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1195 ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1196 ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1198 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1199 .name = "Capture Source",
1200 .info = snd_es18xx_info_mux,
1201 .get = snd_es18xx_get_mux,
1202 .put = snd_es18xx_put_mux,
1206 static snd_kcontrol_new_t snd_es18xx_mono_in_control =
1207 ES18XX_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0);
1209 static snd_kcontrol_new_t snd_es18xx_recmix_controls[] = {
1210 ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1211 ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1212 ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1213 ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1214 ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
1215 ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1216 ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1219 static snd_kcontrol_new_t snd_es18xx_pcm1_controls[] = {
1220 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1223 static snd_kcontrol_new_t snd_es18xx_pcm2_controls[] = {
1224 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1225 ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1228 static snd_kcontrol_new_t snd_es18xx_spatializer_controls[] = {
1229 ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1232 .name = "3D Control - Switch",
1233 .info = snd_es18xx_info_spatializer_enable,
1234 .get = snd_es18xx_get_spatializer_enable,
1235 .put = snd_es18xx_put_spatializer_enable,
1239 static snd_kcontrol_new_t snd_es18xx_micpre1_control =
1240 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1242 static snd_kcontrol_new_t snd_es18xx_micpre2_control =
1243 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1245 static snd_kcontrol_new_t snd_es18xx_hw_volume_controls[] = {
1247 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1248 .name = "Hardware Master Playback Volume",
1249 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1250 .info = snd_es18xx_info_hw_volume,
1251 .get = snd_es18xx_get_hw_volume,
1254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1255 .name = "Hardware Master Playback Switch",
1256 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1257 .info = snd_es18xx_info_hw_switch,
1258 .get = snd_es18xx_get_hw_switch,
1260 ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1263 #if 0
1264 static int __devinit snd_es18xx_config_read(es18xx_t *chip, unsigned char reg)
1266 int data;
1267 unsigned long flags;
1268 spin_lock_irqsave(&chip->ctrl_lock, flags);
1269 outb(reg, chip->ctrl_port);
1270 data = inb(chip->ctrl_port + 1);
1271 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1272 return data;
1274 #endif
1276 static void __devinit snd_es18xx_config_write(es18xx_t *chip,
1277 unsigned char reg, unsigned char data)
1279 /* No need for spinlocks, this function is used only in
1280 otherwise protected init code */
1281 outb(reg, chip->ctrl_port);
1282 outb(data, chip->ctrl_port + 1);
1283 #ifdef REG_DEBUG
1284 snd_printk("Config reg %02x set to %02x\n", reg, data);
1285 #endif
1288 static int __devinit snd_es18xx_initialize(es18xx_t *chip)
1290 int mask = 0;
1292 /* enable extended mode */
1293 snd_es18xx_dsp_command(chip, 0xC6);
1294 /* Reset mixer registers */
1295 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1297 /* Audio 1 DMA demand mode (4 bytes/request) */
1298 snd_es18xx_write(chip, 0xB9, 2);
1299 if (chip->caps & ES18XX_CONTROL) {
1300 /* Hardware volume IRQ */
1301 snd_es18xx_config_write(chip, 0x27, chip->irq);
1302 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1303 /* FM I/O */
1304 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1305 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1307 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1308 /* MPU-401 I/O */
1309 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1310 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1311 /* MPU-401 IRQ */
1312 snd_es18xx_config_write(chip, 0x28, chip->irq);
1314 /* Audio1 IRQ */
1315 snd_es18xx_config_write(chip, 0x70, chip->irq);
1316 /* Audio2 IRQ */
1317 snd_es18xx_config_write(chip, 0x72, chip->irq);
1318 /* Audio1 DMA */
1319 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1320 /* Audio2 DMA */
1321 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1323 /* Enable Audio 1 IRQ */
1324 snd_es18xx_write(chip, 0xB1, 0x50);
1325 /* Enable Audio 2 IRQ */
1326 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1327 /* Enable Audio 1 DMA */
1328 snd_es18xx_write(chip, 0xB2, 0x50);
1329 /* Enable MPU and hardware volume interrupt */
1330 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1332 else {
1333 int irqmask, dma1mask, dma2mask;
1334 switch (chip->irq) {
1335 case 2:
1336 case 9:
1337 irqmask = 0;
1338 break;
1339 case 5:
1340 irqmask = 1;
1341 break;
1342 case 7:
1343 irqmask = 2;
1344 break;
1345 case 10:
1346 irqmask = 3;
1347 break;
1348 default:
1349 snd_printk("invalid irq %d\n", chip->irq);
1350 return -ENODEV;
1352 switch (chip->dma1) {
1353 case 0:
1354 dma1mask = 1;
1355 break;
1356 case 1:
1357 dma1mask = 2;
1358 break;
1359 case 3:
1360 dma1mask = 3;
1361 break;
1362 default:
1363 snd_printk("invalid dma1 %d\n", chip->dma1);
1364 return -ENODEV;
1366 switch (chip->dma2) {
1367 case 0:
1368 dma2mask = 0;
1369 break;
1370 case 1:
1371 dma2mask = 1;
1372 break;
1373 case 3:
1374 dma2mask = 2;
1375 break;
1376 case 5:
1377 dma2mask = 3;
1378 break;
1379 default:
1380 snd_printk("invalid dma2 %d\n", chip->dma2);
1381 return -ENODEV;
1384 /* Enable and set Audio 1 IRQ */
1385 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1386 /* Enable and set Audio 1 DMA */
1387 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1388 /* Set Audio 2 DMA */
1389 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1390 /* Enable Audio 2 IRQ and DMA
1391 Set capture mixer input */
1392 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1393 /* Enable and set hardware volume interrupt */
1394 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1395 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1396 /* MPU401 share irq with audio
1397 Joystick enabled
1398 FM enabled */
1399 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1401 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1403 if (chip->caps & ES18XX_NEW_RATE) {
1404 /* Change behaviour of register A1
1405 4x oversampling
1406 2nd channel DAC asynchronous */
1407 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1409 if (!(chip->caps & ES18XX_PCM2)) {
1410 /* Enable DMA FIFO */
1411 snd_es18xx_write(chip, 0xB7, 0x80);
1413 if (chip->caps & ES18XX_SPATIALIZER) {
1414 /* Set spatializer parameters to recommended values */
1415 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1416 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1417 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1418 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1420 /* Mute input source */
1421 if (chip->caps & ES18XX_MUTEREC)
1422 mask = 0x10;
1423 if (chip->caps & ES18XX_RECMIX)
1424 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1425 else {
1426 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1427 snd_es18xx_write(chip, 0xb4, 0x00);
1429 #ifndef AVOID_POPS
1430 /* Enable PCM output */
1431 snd_es18xx_dsp_command(chip, 0xD1);
1432 #endif
1434 return 0;
1437 static int __devinit snd_es18xx_identify(es18xx_t *chip)
1439 int hi,lo;
1441 /* reset */
1442 if (snd_es18xx_reset(chip) < 0) {
1443 snd_printk("reset at 0x%lx failed!!!\n", chip->port);
1444 return -ENODEV;
1447 snd_es18xx_dsp_command(chip, 0xe7);
1448 hi = snd_es18xx_dsp_get_byte(chip);
1449 if (hi < 0) {
1450 return hi;
1452 lo = snd_es18xx_dsp_get_byte(chip);
1453 if ((lo & 0xf0) != 0x80) {
1454 return -ENODEV;
1456 if (hi == 0x48) {
1457 chip->version = 0x488;
1458 return 0;
1460 if (hi != 0x68) {
1461 return -ENODEV;
1463 if ((lo & 0x0f) < 8) {
1464 chip->version = 0x688;
1465 return 0;
1468 outb(0x40, chip->port + 0x04);
1469 hi = inb(chip->port + 0x05);
1470 lo = inb(chip->port + 0x05);
1471 if (hi != lo) {
1472 chip->version = hi << 8 | lo;
1473 chip->ctrl_port = inb(chip->port + 0x05) << 8;
1474 chip->ctrl_port += inb(chip->port + 0x05);
1476 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1477 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1478 return -EBUSY;
1481 return 0;
1484 /* If has Hardware volume */
1485 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1486 /* If has Audio2 */
1487 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1488 /* If has volume count */
1489 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1490 chip->version = 0x1887;
1491 } else {
1492 chip->version = 0x1888;
1494 } else {
1495 chip->version = 0x1788;
1498 else
1499 chip->version = 0x1688;
1500 return 0;
1503 static int __devinit snd_es18xx_probe(es18xx_t *chip)
1505 if (snd_es18xx_identify(chip) < 0) {
1506 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1507 return -ENODEV;
1510 switch (chip->version) {
1511 case 0x1868:
1512 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_HWV;
1513 break;
1514 case 0x1869:
1515 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1516 break;
1517 case 0x1878:
1518 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1519 break;
1520 case 0x1879:
1521 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1522 break;
1523 case 0x1887:
1524 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1525 break;
1526 case 0x1888:
1527 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1528 break;
1529 default:
1530 snd_printk("[0x%lx] unsupported chip ES%x\n",
1531 chip->port, chip->version);
1532 return -ENODEV;
1535 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1537 if (chip->dma1 == chip->dma2)
1538 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1540 return snd_es18xx_initialize(chip);
1543 static snd_pcm_ops_t snd_es18xx_playback_ops = {
1544 .open = snd_es18xx_playback_open,
1545 .close = snd_es18xx_playback_close,
1546 .ioctl = snd_pcm_lib_ioctl,
1547 .hw_params = snd_es18xx_playback_hw_params,
1548 .hw_free = snd_es18xx_pcm_hw_free,
1549 .prepare = snd_es18xx_playback_prepare,
1550 .trigger = snd_es18xx_playback_trigger,
1551 .pointer = snd_es18xx_playback_pointer,
1554 static snd_pcm_ops_t snd_es18xx_capture_ops = {
1555 .open = snd_es18xx_capture_open,
1556 .close = snd_es18xx_capture_close,
1557 .ioctl = snd_pcm_lib_ioctl,
1558 .hw_params = snd_es18xx_capture_hw_params,
1559 .hw_free = snd_es18xx_pcm_hw_free,
1560 .prepare = snd_es18xx_capture_prepare,
1561 .trigger = snd_es18xx_capture_trigger,
1562 .pointer = snd_es18xx_capture_pointer,
1565 static void snd_es18xx_pcm_free(snd_pcm_t *pcm)
1567 es18xx_t *codec = pcm->private_data;
1568 codec->pcm = NULL;
1569 snd_pcm_lib_preallocate_free_for_all(pcm);
1572 static int __devinit snd_es18xx_pcm(es18xx_t *chip, int device, snd_pcm_t ** rpcm)
1574 snd_pcm_t *pcm;
1575 char str[16];
1576 int err;
1578 if (rpcm)
1579 *rpcm = NULL;
1580 sprintf(str, "ES%x", chip->version);
1581 if (chip->caps & ES18XX_PCM2) {
1582 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
1583 } else {
1584 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
1586 if (err < 0)
1587 return err;
1589 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1590 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1592 /* global setup */
1593 pcm->private_data = chip;
1594 pcm->private_free = snd_es18xx_pcm_free;
1595 pcm->info_flags = 0;
1596 if (chip->caps & ES18XX_DUPLEX_SAME)
1597 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1598 if (! (chip->caps & ES18XX_PCM2))
1599 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1600 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1601 chip->pcm = pcm;
1603 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1604 snd_dma_isa_data(),
1605 64*1024,
1606 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1608 if (rpcm)
1609 *rpcm = pcm;
1610 return 0;
1613 /* Power Management support functions */
1614 #ifdef CONFIG_PM
1615 static int snd_es18xx_suspend(snd_card_t *card, pm_message_t state)
1617 es18xx_t *chip = card->pm_private_data;
1619 snd_pcm_suspend_all(chip->pcm);
1621 /* power down */
1622 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1623 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1624 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1625 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1627 return 0;
1630 static int snd_es18xx_resume(snd_card_t *card)
1632 es18xx_t *chip = card->pm_private_data;
1634 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1635 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1637 return 0;
1639 #endif /* CONFIG_PM */
1641 static int snd_es18xx_free(es18xx_t *chip)
1643 if (chip->res_port) {
1644 release_resource(chip->res_port);
1645 kfree_nocheck(chip->res_port);
1647 if (chip->res_ctrl_port) {
1648 release_resource(chip->res_ctrl_port);
1649 kfree_nocheck(chip->res_ctrl_port);
1651 if (chip->res_mpu_port) {
1652 release_resource(chip->res_mpu_port);
1653 kfree_nocheck(chip->res_mpu_port);
1655 if (chip->irq >= 0)
1656 free_irq(chip->irq, (void *) chip);
1657 if (chip->dma1 >= 0) {
1658 disable_dma(chip->dma1);
1659 free_dma(chip->dma1);
1661 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1662 disable_dma(chip->dma2);
1663 free_dma(chip->dma2);
1665 kfree(chip);
1666 return 0;
1669 static int snd_es18xx_dev_free(snd_device_t *device)
1671 es18xx_t *chip = device->device_data;
1672 return snd_es18xx_free(chip);
1675 static int __devinit snd_es18xx_new_device(snd_card_t * card,
1676 unsigned long port,
1677 unsigned long mpu_port,
1678 unsigned long fm_port,
1679 int irq, int dma1, int dma2,
1680 es18xx_t ** rchip)
1682 es18xx_t *chip;
1683 static snd_device_ops_t ops = {
1684 .dev_free = snd_es18xx_dev_free,
1686 int err;
1688 *rchip = NULL;
1689 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1690 if (chip == NULL)
1691 return -ENOMEM;
1692 spin_lock_init(&chip->reg_lock);
1693 spin_lock_init(&chip->mixer_lock);
1694 spin_lock_init(&chip->ctrl_lock);
1695 chip->card = card;
1696 chip->port = port;
1697 chip->mpu_port = mpu_port;
1698 chip->fm_port = fm_port;
1699 chip->irq = -1;
1700 chip->dma1 = -1;
1701 chip->dma2 = -1;
1702 chip->audio2_vol = 0x00;
1703 chip->active = 0;
1705 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1706 snd_es18xx_free(chip);
1707 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1708 return -EBUSY;
1711 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1712 snd_es18xx_free(chip);
1713 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1714 return -EBUSY;
1716 chip->irq = irq;
1718 if (request_dma(dma1, "ES18xx DMA 1")) {
1719 snd_es18xx_free(chip);
1720 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1721 return -EBUSY;
1723 chip->dma1 = dma1;
1725 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1726 snd_es18xx_free(chip);
1727 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1728 return -EBUSY;
1730 chip->dma2 = dma2;
1732 if (snd_es18xx_probe(chip) < 0) {
1733 snd_es18xx_free(chip);
1734 return -ENODEV;
1736 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1737 snd_es18xx_free(chip);
1738 return err;
1740 *rchip = chip;
1741 return 0;
1744 static int __devinit snd_es18xx_mixer(es18xx_t *chip)
1746 snd_card_t *card;
1747 int err;
1748 unsigned int idx;
1750 card = chip->card;
1752 strcpy(card->mixername, chip->pcm->name);
1754 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1755 snd_kcontrol_t *kctl;
1756 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1757 if (chip->caps & ES18XX_HWV) {
1758 switch (idx) {
1759 case 0:
1760 chip->master_volume = kctl;
1761 kctl->private_free = snd_es18xx_hwv_free;
1762 break;
1763 case 1:
1764 chip->master_switch = kctl;
1765 kctl->private_free = snd_es18xx_hwv_free;
1766 break;
1769 if ((err = snd_ctl_add(card, kctl)) < 0)
1770 return err;
1772 if (chip->caps & ES18XX_PCM2) {
1773 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1774 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1775 return err;
1777 } else {
1778 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1779 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1780 return err;
1784 if (chip->caps & ES18XX_MONO) {
1785 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_mono_in_control, chip))) < 0)
1786 return err;
1788 if (chip->caps & ES18XX_RECMIX) {
1789 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1790 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1791 return err;
1794 switch (chip->version) {
1795 default:
1796 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1797 return err;
1798 break;
1799 case 0x1869:
1800 case 0x1879:
1801 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1802 return err;
1803 break;
1805 if (chip->caps & ES18XX_SPATIALIZER) {
1806 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1807 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1808 return err;
1811 if (chip->caps & ES18XX_HWV) {
1812 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1813 snd_kcontrol_t *kctl;
1814 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1815 if (idx == 0)
1816 chip->hw_volume = kctl;
1817 else
1818 chip->hw_switch = kctl;
1819 kctl->private_free = snd_es18xx_hwv_free;
1820 if ((err = snd_ctl_add(card, kctl)) < 0)
1821 return err;
1825 return 0;
1829 /* Card level */
1831 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1832 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1833 MODULE_LICENSE("GPL");
1834 MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1835 "{ESS,ES1869 PnP AudioDrive},"
1836 "{ESS,ES1878 PnP AudioDrive},"
1837 "{ESS,ES1879 PnP AudioDrive},"
1838 "{ESS,ES1887 PnP AudioDrive},"
1839 "{ESS,ES1888 PnP AudioDrive},"
1840 "{ESS,ES1887 AudioDrive},"
1841 "{ESS,ES1888 AudioDrive}}");
1843 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1844 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1845 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1846 #ifdef CONFIG_PNP
1847 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1848 #endif
1849 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1850 #ifndef CONFIG_PNP
1851 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1852 #else
1853 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1854 #endif
1855 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1856 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1857 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1858 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1860 module_param_array(index, int, NULL, 0444);
1861 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1862 module_param_array(id, charp, NULL, 0444);
1863 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1864 module_param_array(enable, bool, NULL, 0444);
1865 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1866 #ifdef CONFIG_PNP
1867 module_param_array(isapnp, bool, NULL, 0444);
1868 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1869 #endif
1870 module_param_array(port, long, NULL, 0444);
1871 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1872 module_param_array(mpu_port, long, NULL, 0444);
1873 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1874 module_param_array(fm_port, long, NULL, 0444);
1875 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1876 module_param_array(irq, int, NULL, 0444);
1877 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1878 module_param_array(dma1, int, NULL, 0444);
1879 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1880 module_param_array(dma2, int, NULL, 0444);
1881 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1883 struct snd_audiodrive {
1884 #ifdef CONFIG_PNP
1885 struct pnp_dev *dev;
1886 struct pnp_dev *devc;
1887 #endif
1890 static snd_card_t *snd_audiodrive_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
1892 #ifdef CONFIG_PNP
1894 static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1895 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1896 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1897 /* ESS 1868 (integrated on Maxisound Cards) */
1898 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1899 /* ESS 1868 (integrated on Maxisound Cards) */
1900 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
1901 /* ESS ES1869 Plug and Play AudioDrive */
1902 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
1903 /* ESS 1869 */
1904 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
1905 /* ESS 1878 */
1906 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
1907 /* ESS 1879 */
1908 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
1909 /* --- */
1910 { .id = "" } /* end */
1913 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
1915 static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
1916 struct pnp_card_link *card,
1917 const struct pnp_card_device_id *id)
1919 struct pnp_dev *pdev;
1920 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
1921 int err;
1923 if (!cfg)
1924 return -ENOMEM;
1925 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
1926 if (acard->dev == NULL) {
1927 kfree(cfg);
1928 return -EBUSY;
1930 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
1931 if (acard->devc == NULL) {
1932 kfree(cfg);
1933 return -EBUSY;
1935 /* Control port initialization */
1936 err = pnp_activate_dev(acard->devc);
1937 if (err < 0) {
1938 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
1939 return -EAGAIN;
1941 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
1942 /* PnP initialization */
1943 pdev = acard->dev;
1944 pnp_init_resource_table(cfg);
1945 if (port[dev] != SNDRV_AUTO_PORT)
1946 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
1947 if (fm_port[dev] != SNDRV_AUTO_PORT)
1948 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
1949 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1950 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
1951 if (dma1[dev] != SNDRV_AUTO_DMA)
1952 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
1953 if (dma2[dev] != SNDRV_AUTO_DMA)
1954 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
1955 if (irq[dev] != SNDRV_AUTO_IRQ)
1956 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
1957 err = pnp_manual_config_dev(pdev, cfg, 0);
1958 if (err < 0)
1959 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
1960 err = pnp_activate_dev(pdev);
1961 if (err < 0) {
1962 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1963 kfree(cfg);
1964 return -EBUSY;
1966 /* ok. hack using Vendor-Defined Card-Level registers */
1967 /* skip csn and logdev initialization - already done in isapnp_configure */
1968 if (pnp_device_is_isapnp(pdev)) {
1969 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1970 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1971 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1972 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1973 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1974 isapnp_cfg_end();
1975 } else {
1976 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
1978 port[dev] = pnp_port_start(pdev, 0);
1979 fm_port[dev] = pnp_port_start(pdev, 1);
1980 mpu_port[dev] = pnp_port_start(pdev, 2);
1981 dma1[dev] = pnp_dma(pdev, 0);
1982 dma2[dev] = pnp_dma(pdev, 1);
1983 irq[dev] = pnp_irq(pdev, 0);
1984 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
1985 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
1986 kfree(cfg);
1987 return 0;
1989 #endif /* CONFIG_PNP */
1991 static int __devinit snd_audiodrive_probe(int dev, struct pnp_card_link *pcard,
1992 const struct pnp_card_device_id *pid)
1994 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
1995 static int possible_dmas[] = {1, 0, 3, 5, -1};
1996 int xirq, xdma1, xdma2;
1997 snd_card_t *card;
1998 struct snd_audiodrive *acard;
1999 snd_rawmidi_t *rmidi = NULL;
2000 es18xx_t *chip;
2001 opl3_t *opl3;
2002 int err;
2004 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
2005 sizeof(struct snd_audiodrive));
2006 if (card == NULL)
2007 return -ENOMEM;
2008 acard = (struct snd_audiodrive *)card->private_data;
2009 #ifdef CONFIG_PNP
2010 if (isapnp[dev]) {
2011 if ((err = snd_audiodrive_pnp(dev, acard, pcard, pid)) < 0) {
2012 snd_card_free(card);
2013 return err;
2015 snd_card_set_dev(card, &pcard->card->dev);
2017 #endif
2019 xirq = irq[dev];
2020 if (xirq == SNDRV_AUTO_IRQ) {
2021 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2022 snd_card_free(card);
2023 snd_printk("unable to find a free IRQ\n");
2024 return -EBUSY;
2027 xdma1 = dma1[dev];
2028 if (xdma1 == SNDRV_AUTO_DMA) {
2029 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2030 snd_card_free(card);
2031 snd_printk("unable to find a free DMA1\n");
2032 return -EBUSY;
2035 xdma2 = dma2[dev];
2036 if (xdma2 == SNDRV_AUTO_DMA) {
2037 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2038 snd_card_free(card);
2039 snd_printk("unable to find a free DMA2\n");
2040 return -EBUSY;
2044 if ((err = snd_es18xx_new_device(card,
2045 port[dev],
2046 mpu_port[dev],
2047 fm_port[dev],
2048 xirq, xdma1, xdma2,
2049 &chip)) < 0) {
2050 snd_card_free(card);
2051 return err;
2054 sprintf(card->driver, "ES%x", chip->version);
2055 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2056 if (xdma1 != xdma2)
2057 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2058 card->shortname,
2059 chip->port,
2060 xirq, xdma1, xdma2);
2061 else
2062 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2063 card->shortname,
2064 chip->port,
2065 xirq, xdma1);
2067 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0) {
2068 snd_card_free(card);
2069 return err;
2071 if ((err = snd_es18xx_mixer(chip)) < 0) {
2072 snd_card_free(card);
2073 return err;
2076 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2077 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
2078 snd_printk(KERN_ERR PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
2079 } else {
2080 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
2081 snd_card_free(card);
2082 return err;
2087 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2088 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2089 chip->mpu_port, 0,
2090 xirq, 0,
2091 &rmidi)) < 0) {
2092 snd_card_free(card);
2093 return err;
2095 chip->rmidi = rmidi;
2098 /* Power Management */
2099 snd_card_set_isa_pm_callback(card, snd_es18xx_suspend, snd_es18xx_resume, chip);
2101 if ((err = snd_card_register(card)) < 0) {
2102 snd_card_free(card);
2103 return err;
2105 if (pcard)
2106 pnp_set_card_drvdata(pcard, card);
2107 else
2108 snd_audiodrive_legacy[dev] = card;
2109 return 0;
2112 static int __devinit snd_audiodrive_probe_legacy_port(unsigned long xport)
2114 static int dev;
2115 int res;
2117 for ( ; dev < SNDRV_CARDS; dev++) {
2118 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
2119 continue;
2120 #ifdef CONFIG_PNP
2121 if (isapnp[dev])
2122 continue;
2123 #endif
2124 port[dev] = xport;
2125 res = snd_audiodrive_probe(dev, NULL, NULL);
2126 if (res < 0)
2127 port[dev] = SNDRV_AUTO_PORT;
2128 return res;
2130 return -ENODEV;
2134 #ifdef CONFIG_PNP
2135 static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *card,
2136 const struct pnp_card_device_id *id)
2138 static int dev;
2139 int res;
2141 for ( ; dev < SNDRV_CARDS; dev++) {
2142 if (!enable[dev] || !isapnp[dev])
2143 continue;
2144 res = snd_audiodrive_probe(dev, card, id);
2145 if (res < 0)
2146 return res;
2147 dev++;
2148 return 0;
2151 return -ENODEV;
2154 static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2156 snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
2158 snd_card_disconnect(card);
2159 snd_card_free_in_thread(card);
2162 static struct pnp_card_driver es18xx_pnpc_driver = {
2163 .flags = PNP_DRIVER_RES_DISABLE,
2164 .name = "es18xx",
2165 .id_table = snd_audiodrive_pnpids,
2166 .probe = snd_audiodrive_pnp_detect,
2167 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2169 #endif /* CONFIG_PNP */
2171 static int __init alsa_card_es18xx_init(void)
2173 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280, -1};
2174 int dev, cards = 0, i;
2176 /* legacy non-auto cards at first */
2177 for (dev = 0; dev < SNDRV_CARDS; dev++) {
2178 if (!enable[dev] || port[dev] == SNDRV_AUTO_PORT)
2179 continue;
2180 #ifdef CONFIG_PNP
2181 if (isapnp[dev])
2182 continue;
2183 #endif
2184 if (snd_audiodrive_probe(dev, NULL, NULL) >= 0)
2185 cards++;
2187 /* legacy auto configured cards */
2188 i = snd_legacy_auto_probe(possible_ports, snd_audiodrive_probe_legacy_port);
2189 if (i > 0)
2190 cards += i;
2192 #ifdef CONFIG_PNP
2193 /* ISA PnP cards at last */
2194 i = pnp_register_card_driver(&es18xx_pnpc_driver);
2195 if (i > 0)
2196 cards += i;
2198 #endif
2199 if(!cards) {
2200 #ifdef CONFIG_PNP
2201 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2202 #endif
2203 #ifdef MODULE
2204 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2205 #endif
2206 return -ENODEV;
2208 return 0;
2211 static void __exit alsa_card_es18xx_exit(void)
2213 int idx;
2215 #ifdef CONFIG_PNP
2216 /* PnP cards first */
2217 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2218 #endif
2219 for(idx = 0; idx < SNDRV_CARDS; idx++)
2220 snd_card_free(snd_audiodrive_legacy[idx]);
2223 module_init(alsa_card_es18xx_init)
2224 module_exit(alsa_card_es18xx_exit)