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
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
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.
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
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 <linux/init.h>
69 #include <linux/err.h>
70 #include <linux/platform_device.h>
71 #include <linux/slab.h>
72 #include <linux/pnp.h>
73 #include <linux/isapnp.h>
74 #include <linux/moduleparam.h>
77 #include <sound/core.h>
78 #include <sound/control.h>
79 #include <sound/pcm.h>
80 #include <sound/pcm_params.h>
81 #include <sound/mpu401.h>
82 #include <sound/opl3.h>
83 #define SNDRV_LEGACY_FIND_FREE_IRQ
84 #define SNDRV_LEGACY_FIND_FREE_DMA
85 #include <sound/initval.h>
87 #define PFX "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 */
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 struct snd_card *card;
112 struct snd_pcm_substream *playback_a_substream;
113 struct snd_pcm_substream *capture_a_substream;
114 struct snd_pcm_substream *playback_b_substream;
116 struct snd_rawmidi *rmidi;
118 struct snd_kcontrol *hw_volume;
119 struct snd_kcontrol *hw_switch;
120 struct snd_kcontrol *master_volume;
121 struct snd_kcontrol *master_switch;
124 spinlock_t mixer_lock;
125 spinlock_t ctrl_lock;
127 unsigned char pm_reg;
131 struct snd_audiodrive {
132 struct snd_es18xx *chip;
135 struct pnp_dev *devc;
139 #define AUDIO1_IRQ 0x01
140 #define AUDIO2_IRQ 0x02
144 #define ES18XX_PCM2 0x0001 /* Has two useable PCM */
145 #define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
146 #define ES18XX_RECMIX 0x0004 /* Has record mixer */
147 #define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
148 #define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
149 #define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
150 #define ES18XX_AUXB 0x0040 /* AuxB mixer control */
151 #define ES18XX_HWV 0x0080 /* Has hardware volume */
152 #define ES18XX_MONO 0x0100 /* Mono_in mixer control */
153 #define ES18XX_I2S 0x0200 /* I2S mixer control */
154 #define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
155 #define ES18XX_CONTROL 0x0800 /* Has control ports */
157 /* Power Management */
158 #define ES18XX_PM 0x07
159 #define ES18XX_PM_GPO0 0x01
160 #define ES18XX_PM_GPO1 0x02
161 #define ES18XX_PM_PDR 0x04
162 #define ES18XX_PM_ANA 0x08
163 #define ES18XX_PM_FM 0x020
164 #define ES18XX_PM_SUS 0x080
171 #define MILLISECOND 10000
173 static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
177 for(i = MILLISECOND; i; i--)
178 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
179 outb(val, chip->port + 0x0C);
182 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
186 static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
190 for(i = MILLISECOND/10; i; i--)
191 if (inb(chip->port + 0x0C) & 0x40)
192 return inb(chip->port + 0x0A);
193 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
194 chip->port + 0x0A, inb(chip->port + 0x0A));
200 static int snd_es18xx_write(struct snd_es18xx *chip,
201 unsigned char reg, unsigned char data)
206 spin_lock_irqsave(&chip->reg_lock, flags);
207 ret = snd_es18xx_dsp_command(chip, reg);
210 ret = snd_es18xx_dsp_command(chip, data);
212 spin_unlock_irqrestore(&chip->reg_lock, flags);
214 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
219 static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
223 spin_lock_irqsave(&chip->reg_lock, flags);
224 ret = snd_es18xx_dsp_command(chip, 0xC0);
227 ret = snd_es18xx_dsp_command(chip, reg);
230 data = snd_es18xx_dsp_get_byte(chip);
233 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
236 spin_unlock_irqrestore(&chip->reg_lock, flags);
240 /* Return old value */
241 static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
242 unsigned char mask, unsigned char val)
245 unsigned char old, new, oval;
247 spin_lock_irqsave(&chip->reg_lock, flags);
248 ret = snd_es18xx_dsp_command(chip, 0xC0);
251 ret = snd_es18xx_dsp_command(chip, reg);
254 ret = snd_es18xx_dsp_get_byte(chip);
261 ret = snd_es18xx_dsp_command(chip, reg);
264 new = (old & ~mask) | (val & mask);
265 ret = snd_es18xx_dsp_command(chip, new);
269 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
275 spin_unlock_irqrestore(&chip->reg_lock, flags);
279 static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
280 unsigned char reg, unsigned char data)
283 spin_lock_irqsave(&chip->mixer_lock, flags);
284 outb(reg, chip->port + 0x04);
285 outb(data, chip->port + 0x05);
286 spin_unlock_irqrestore(&chip->mixer_lock, flags);
288 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
292 static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
296 spin_lock_irqsave(&chip->mixer_lock, flags);
297 outb(reg, chip->port + 0x04);
298 data = inb(chip->port + 0x05);
299 spin_unlock_irqrestore(&chip->mixer_lock, flags);
301 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
306 /* Return old value */
307 static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
308 unsigned char mask, unsigned char val)
310 unsigned char old, new, oval;
312 spin_lock_irqsave(&chip->mixer_lock, flags);
313 outb(reg, chip->port + 0x04);
314 old = inb(chip->port + 0x05);
317 new = (old & ~mask) | (val & mask);
318 outb(new, chip->port + 0x05);
320 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
324 spin_unlock_irqrestore(&chip->mixer_lock, flags);
328 static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
331 int old, expected, new;
333 spin_lock_irqsave(&chip->mixer_lock, flags);
334 outb(reg, chip->port + 0x04);
335 old = inb(chip->port + 0x05);
336 expected = old ^ mask;
337 outb(expected, chip->port + 0x05);
338 new = inb(chip->port + 0x05);
339 spin_unlock_irqrestore(&chip->mixer_lock, flags);
341 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
342 reg, old, expected, new);
344 return expected == new;
348 static int snd_es18xx_reset(struct snd_es18xx *chip)
351 outb(0x03, chip->port + 0x06);
352 inb(chip->port + 0x06);
353 outb(0x00, chip->port + 0x06);
354 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
355 if (inb(chip->port + 0x0A) != 0xAA)
360 static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
362 outb(0x02, chip->port + 0x06);
363 inb(chip->port + 0x06);
364 outb(0x00, chip->port + 0x06);
368 static struct snd_ratnum new_clocks[2] = {
383 static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
388 static struct snd_ratnum old_clocks[2] = {
403 static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
409 static void snd_es18xx_rate_set(struct snd_es18xx *chip,
410 struct snd_pcm_substream *substream,
413 unsigned int bits, div0;
414 struct snd_pcm_runtime *runtime = substream->runtime;
415 if (chip->caps & ES18XX_NEW_RATE) {
416 if (runtime->rate_num == new_clocks[0].num)
417 bits = 128 - runtime->rate_den;
419 bits = 256 - runtime->rate_den;
421 if (runtime->rate_num == old_clocks[0].num)
422 bits = 256 - runtime->rate_den;
424 bits = 128 - runtime->rate_den;
427 /* set filter register */
428 div0 = 256 - 7160000*20/(8*82*runtime->rate);
430 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
431 snd_es18xx_mixer_write(chip, 0x70, bits);
433 * Comment from kernel oss driver:
434 * FKS: fascinating: 0x72 doesn't seem to work.
436 snd_es18xx_write(chip, 0xA2, div0);
437 snd_es18xx_mixer_write(chip, 0x72, div0);
439 snd_es18xx_write(chip, 0xA1, bits);
440 snd_es18xx_write(chip, 0xA2, div0);
444 static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *hw_params)
447 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
451 if (params_channels(hw_params) == 2)
453 if (snd_pcm_format_width(params_format(hw_params)) == 16)
456 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
457 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
458 (chip->capture_a_substream) &&
459 params_channels(hw_params) != 1) {
460 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
463 chip->dma2_shift = shift;
465 chip->dma1_shift = shift;
467 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
472 static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
474 return snd_pcm_lib_free_pages(substream);
477 static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
478 struct snd_pcm_substream *substream)
480 struct snd_pcm_runtime *runtime = substream->runtime;
481 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
482 unsigned int count = snd_pcm_lib_period_bytes(substream);
484 chip->dma2_size = size;
486 snd_es18xx_rate_set(chip, substream, DAC2);
488 /* Transfer Count Reload */
489 count = 0x10000 - count;
490 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
491 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
494 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
495 ((runtime->channels == 1) ? 0x00 : 0x02) |
496 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
497 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
499 /* Set DMA controller */
500 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
505 static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
506 struct snd_pcm_substream *substream,
510 case SNDRV_PCM_TRIGGER_START:
511 case SNDRV_PCM_TRIGGER_RESUME:
512 if (chip->active & DAC2)
514 chip->active |= DAC2;
517 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
519 snd_es18xx_mixer_write(chip, 0x78, 0x93);
523 if (chip->caps & ES18XX_PCM2)
524 /* Restore Audio 2 volume */
525 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
527 /* Enable PCM output */
528 snd_es18xx_dsp_command(chip, 0xD1);
531 case SNDRV_PCM_TRIGGER_STOP:
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 if (!(chip->active & DAC2))
535 chip->active &= ~DAC2;
537 snd_es18xx_mixer_write(chip, 0x78, 0x00);
540 if (chip->caps & ES18XX_PCM2)
541 /* Set Audio 2 volume to 0 */
542 snd_es18xx_mixer_write(chip, 0x7C, 0);
544 /* Disable PCM output */
545 snd_es18xx_dsp_command(chip, 0xD3);
555 static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
556 struct snd_pcm_hw_params *hw_params)
558 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
562 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
563 chip->playback_a_substream &&
564 params_channels(hw_params) != 1) {
565 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
568 if (params_channels(hw_params) == 2)
570 if (snd_pcm_format_width(params_format(hw_params)) == 16)
572 chip->dma1_shift = shift;
573 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
578 static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
580 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
581 struct snd_pcm_runtime *runtime = substream->runtime;
582 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
583 unsigned int count = snd_pcm_lib_period_bytes(substream);
585 chip->dma1_size = size;
587 snd_es18xx_reset_fifo(chip);
589 /* Set stereo/mono */
590 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
592 snd_es18xx_rate_set(chip, substream, ADC1);
594 /* Transfer Count Reload */
595 count = 0x10000 - count;
596 snd_es18xx_write(chip, 0xA4, count & 0xff);
597 snd_es18xx_write(chip, 0xA5, count >> 8);
604 snd_es18xx_write(chip, 0xB7,
605 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
606 snd_es18xx_write(chip, 0xB7, 0x90 |
607 ((runtime->channels == 1) ? 0x40 : 0x08) |
608 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
609 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
611 /* Set DMA controler */
612 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
617 static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
620 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
623 case SNDRV_PCM_TRIGGER_START:
624 case SNDRV_PCM_TRIGGER_RESUME:
625 if (chip->active & ADC1)
627 chip->active |= ADC1;
629 snd_es18xx_write(chip, 0xB8, 0x0f);
631 case SNDRV_PCM_TRIGGER_STOP:
632 case SNDRV_PCM_TRIGGER_SUSPEND:
633 if (!(chip->active & ADC1))
635 chip->active &= ~ADC1;
637 snd_es18xx_write(chip, 0xB8, 0x00);
646 static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
647 struct snd_pcm_substream *substream)
649 struct snd_pcm_runtime *runtime = substream->runtime;
650 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
651 unsigned int count = snd_pcm_lib_period_bytes(substream);
653 chip->dma1_size = size;
655 snd_es18xx_reset_fifo(chip);
657 /* Set stereo/mono */
658 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
660 snd_es18xx_rate_set(chip, substream, DAC1);
662 /* Transfer Count Reload */
663 count = 0x10000 - count;
664 snd_es18xx_write(chip, 0xA4, count & 0xff);
665 snd_es18xx_write(chip, 0xA5, count >> 8);
668 snd_es18xx_write(chip, 0xB6,
669 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
670 snd_es18xx_write(chip, 0xB7,
671 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
672 snd_es18xx_write(chip, 0xB7, 0x90 |
673 (runtime->channels == 1 ? 0x40 : 0x08) |
674 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
675 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
677 /* Set DMA controler */
678 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
683 static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
684 struct snd_pcm_substream *substream,
688 case SNDRV_PCM_TRIGGER_START:
689 case SNDRV_PCM_TRIGGER_RESUME:
690 if (chip->active & DAC1)
692 chip->active |= DAC1;
694 snd_es18xx_write(chip, 0xB8, 0x05);
699 snd_es18xx_dsp_command(chip, 0xD1);
702 case SNDRV_PCM_TRIGGER_STOP:
703 case SNDRV_PCM_TRIGGER_SUSPEND:
704 if (!(chip->active & DAC1))
706 chip->active &= ~DAC1;
708 snd_es18xx_write(chip, 0xB8, 0x00);
712 /* Disable Audio 1 */
713 snd_es18xx_dsp_command(chip, 0xD3);
723 static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
725 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
726 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
727 return snd_es18xx_playback1_prepare(chip, substream);
729 return snd_es18xx_playback2_prepare(chip, substream);
732 static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
735 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
736 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
737 return snd_es18xx_playback1_trigger(chip, substream, cmd);
739 return snd_es18xx_playback2_trigger(chip, substream, cmd);
742 static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
744 struct snd_es18xx *chip = dev_id;
745 unsigned char status;
747 if (chip->caps & ES18XX_CONTROL) {
748 /* Read Interrupt status */
749 status = inb(chip->ctrl_port + 6);
751 /* Read Interrupt status */
752 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
757 if (inb(chip->port + 0x0C) & 0x01)
758 status |= AUDIO1_IRQ;
759 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
760 status |= AUDIO2_IRQ;
761 if ((chip->caps & ES18XX_HWV) &&
762 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
767 /* Audio 1 & Audio 2 */
768 if (status & AUDIO2_IRQ) {
769 if (chip->active & DAC2)
770 snd_pcm_period_elapsed(chip->playback_a_substream);
772 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
774 if (status & AUDIO1_IRQ) {
775 /* ok.. capture is active */
776 if (chip->active & ADC1)
777 snd_pcm_period_elapsed(chip->capture_a_substream);
778 /* ok.. playback2 is active */
779 else if (chip->active & DAC1)
780 snd_pcm_period_elapsed(chip->playback_b_substream);
782 inb(chip->port + 0x0E);
786 if ((status & MPU_IRQ) && chip->rmidi)
787 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
789 /* Hardware volume */
790 if (status & HWV_IRQ) {
791 int split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
792 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
793 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
795 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
796 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
799 snd_es18xx_mixer_write(chip, 0x66, 0x00);
804 static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
806 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
809 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
810 if (!(chip->active & DAC2))
812 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
813 return pos >> chip->dma2_shift;
815 if (!(chip->active & DAC1))
817 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
818 return pos >> chip->dma1_shift;
822 static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
824 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
827 if (!(chip->active & ADC1))
829 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
830 return pos >> chip->dma1_shift;
833 static struct snd_pcm_hardware snd_es18xx_playback =
835 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
836 SNDRV_PCM_INFO_RESUME |
837 SNDRV_PCM_INFO_MMAP_VALID),
838 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
839 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
840 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
845 .buffer_bytes_max = 65536,
846 .period_bytes_min = 64,
847 .period_bytes_max = 65536,
853 static struct snd_pcm_hardware snd_es18xx_capture =
855 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
856 SNDRV_PCM_INFO_RESUME |
857 SNDRV_PCM_INFO_MMAP_VALID),
858 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
859 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
860 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
865 .buffer_bytes_max = 65536,
866 .period_bytes_min = 64,
867 .period_bytes_max = 65536,
873 static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
875 struct snd_pcm_runtime *runtime = substream->runtime;
876 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
878 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
879 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
880 chip->capture_a_substream &&
881 chip->capture_a_substream->runtime->channels != 1)
883 chip->playback_a_substream = substream;
884 } else if (substream->number <= 1) {
885 if (chip->capture_a_substream)
887 chip->playback_b_substream = substream;
892 substream->runtime->hw = snd_es18xx_playback;
893 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
894 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
898 static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
900 struct snd_pcm_runtime *runtime = substream->runtime;
901 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
903 if (chip->playback_b_substream)
905 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
906 chip->playback_a_substream &&
907 chip->playback_a_substream->runtime->channels != 1)
909 chip->capture_a_substream = substream;
910 substream->runtime->hw = snd_es18xx_capture;
911 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
912 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
916 static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
918 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
920 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
921 chip->playback_a_substream = NULL;
923 chip->playback_b_substream = NULL;
925 snd_pcm_lib_free_pages(substream);
929 static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
931 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
933 chip->capture_a_substream = NULL;
934 snd_pcm_lib_free_pages(substream);
942 /* Record source mux routines:
943 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
944 * bit table for the 4/5 source mux:
951 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
952 * either the play mixer or the capture mixer.
954 * "map4Source" translates from source number to reg bit pattern
955 * "invMap4Source" translates from reg bit pattern to source number
958 static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
960 static char *texts4Source[4] = {
961 "Mic", "CD", "Line", "Master"
963 static char *texts5Source[5] = {
964 "Mic", "CD", "Line", "Master", "Mix"
966 static char *texts8Source[8] = {
967 "Mic", "Mic Master", "CD", "AOUT",
968 "Mic1", "Mix", "Line", "Master"
970 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
972 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
974 switch (chip->version) {
977 uinfo->value.enumerated.items = 4;
978 if (uinfo->value.enumerated.item > 3)
979 uinfo->value.enumerated.item = 3;
980 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
984 uinfo->value.enumerated.items = 5;
985 if (uinfo->value.enumerated.item > 4)
986 uinfo->value.enumerated.item = 4;
987 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
989 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
991 uinfo->value.enumerated.items = 8;
992 if (uinfo->value.enumerated.item > 7)
993 uinfo->value.enumerated.item = 7;
994 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1002 static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1004 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
1005 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1006 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1007 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1008 muxSource = invMap4Source[muxSource];
1010 (chip->version == 0x1887 || chip->version == 0x1888) &&
1011 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1015 ucontrol->value.enumerated.item[0] = muxSource;
1019 static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1021 static unsigned char map4Source[4] = {0, 2, 6, 7};
1022 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1023 unsigned char val = ucontrol->value.enumerated.item[0];
1024 unsigned char retVal = 0;
1026 switch (chip->version) {
1027 /* 5 source chips */
1033 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1036 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1037 /* 4 source chips */
1042 val = map4Source[val];
1044 /* 8 source chips */
1053 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
1056 static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1058 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1060 uinfo->value.integer.min = 0;
1061 uinfo->value.integer.max = 1;
1065 static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1067 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1068 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1069 ucontrol->value.integer.value[0] = !!(val & 8);
1073 static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1075 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1076 unsigned char oval, nval;
1078 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1079 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1080 change = nval != oval;
1082 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1083 snd_es18xx_mixer_write(chip, 0x50, nval);
1088 static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1090 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1092 uinfo->value.integer.min = 0;
1093 uinfo->value.integer.max = 63;
1097 static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1099 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1100 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1101 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1105 static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1107 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1109 uinfo->value.integer.min = 0;
1110 uinfo->value.integer.max = 1;
1114 static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1116 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1117 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1118 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1122 static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
1124 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1125 chip->master_volume = NULL;
1126 chip->master_switch = NULL;
1127 chip->hw_volume = NULL;
1128 chip->hw_switch = NULL;
1131 static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
1132 unsigned char mask, unsigned char val)
1135 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1137 return snd_es18xx_bits(chip, reg, mask, val);
1140 static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
1143 return snd_es18xx_mixer_read(chip, reg);
1145 return snd_es18xx_read(chip, reg);
1148 #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1149 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1150 .info = snd_es18xx_info_single, \
1151 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1152 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1154 static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1156 int mask = (kcontrol->private_value >> 16) & 0xff;
1158 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1160 uinfo->value.integer.min = 0;
1161 uinfo->value.integer.max = mask;
1165 static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1167 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1168 int reg = kcontrol->private_value & 0xff;
1169 int shift = (kcontrol->private_value >> 8) & 0xff;
1170 int mask = (kcontrol->private_value >> 16) & 0xff;
1171 int invert = (kcontrol->private_value >> 24) & 0xff;
1174 val = snd_es18xx_reg_read(chip, reg);
1175 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1177 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1181 static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1183 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1184 int reg = kcontrol->private_value & 0xff;
1185 int shift = (kcontrol->private_value >> 8) & 0xff;
1186 int mask = (kcontrol->private_value >> 16) & 0xff;
1187 int invert = (kcontrol->private_value >> 24) & 0xff;
1190 val = (ucontrol->value.integer.value[0] & mask);
1195 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1198 #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1199 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1200 .info = snd_es18xx_info_double, \
1201 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1202 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1204 static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1206 int mask = (kcontrol->private_value >> 24) & 0xff;
1208 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1210 uinfo->value.integer.min = 0;
1211 uinfo->value.integer.max = mask;
1215 static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1217 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1218 int left_reg = kcontrol->private_value & 0xff;
1219 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1220 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1221 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1222 int mask = (kcontrol->private_value >> 24) & 0xff;
1223 int invert = (kcontrol->private_value >> 22) & 1;
1224 unsigned char left, right;
1226 left = snd_es18xx_reg_read(chip, left_reg);
1227 if (left_reg != right_reg)
1228 right = snd_es18xx_reg_read(chip, right_reg);
1231 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1232 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1234 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1235 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1240 static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1242 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1243 int left_reg = kcontrol->private_value & 0xff;
1244 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1245 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1246 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1247 int mask = (kcontrol->private_value >> 24) & 0xff;
1248 int invert = (kcontrol->private_value >> 22) & 1;
1250 unsigned char val1, val2, mask1, mask2;
1252 val1 = ucontrol->value.integer.value[0] & mask;
1253 val2 = ucontrol->value.integer.value[1] & mask;
1258 val1 <<= shift_left;
1259 val2 <<= shift_right;
1260 mask1 = mask << shift_left;
1261 mask2 = mask << shift_right;
1262 if (left_reg != right_reg) {
1264 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1266 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1269 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1270 val1 | val2) != (val1 | val2));
1276 * These arrays contain setup data for mixer controls.
1278 * The controls that are universal to all chipsets are fully initialized
1281 static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
1282 ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1283 ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1284 ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1285 ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1286 ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1287 ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1288 ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1289 ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1290 ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1292 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1293 .name = "Capture Source",
1294 .info = snd_es18xx_info_mux,
1295 .get = snd_es18xx_get_mux,
1296 .put = snd_es18xx_put_mux,
1300 static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
1301 ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1302 ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1303 ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1304 ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1305 ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1306 ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1310 * The chipset specific mixer controls
1312 static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1313 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1315 static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1316 ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1317 ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1318 ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1321 static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
1322 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1325 static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
1326 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1327 ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1330 static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
1331 ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1333 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1334 .name = "3D Control - Switch",
1335 .info = snd_es18xx_info_spatializer_enable,
1336 .get = snd_es18xx_get_spatializer_enable,
1337 .put = snd_es18xx_put_spatializer_enable,
1341 static struct snd_kcontrol_new snd_es18xx_micpre1_control =
1342 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1344 static struct snd_kcontrol_new snd_es18xx_micpre2_control =
1345 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1347 static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
1349 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1350 .name = "Hardware Master Playback Volume",
1351 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1352 .info = snd_es18xx_info_hw_volume,
1353 .get = snd_es18xx_get_hw_volume,
1356 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1357 .name = "Hardware Master Playback Switch",
1358 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1359 .info = snd_es18xx_info_hw_switch,
1360 .get = snd_es18xx_get_hw_switch,
1362 ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1366 static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
1369 unsigned long flags;
1370 spin_lock_irqsave(&chip->ctrl_lock, flags);
1371 outb(reg, chip->ctrl_port);
1372 data = inb(chip->ctrl_port + 1);
1373 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1378 static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
1379 unsigned char reg, unsigned char data)
1381 /* No need for spinlocks, this function is used only in
1382 otherwise protected init code */
1383 outb(reg, chip->ctrl_port);
1384 outb(data, chip->ctrl_port + 1);
1386 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
1390 static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
1394 /* enable extended mode */
1395 snd_es18xx_dsp_command(chip, 0xC6);
1396 /* Reset mixer registers */
1397 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1399 /* Audio 1 DMA demand mode (4 bytes/request) */
1400 snd_es18xx_write(chip, 0xB9, 2);
1401 if (chip->caps & ES18XX_CONTROL) {
1402 /* Hardware volume IRQ */
1403 snd_es18xx_config_write(chip, 0x27, chip->irq);
1404 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1406 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1407 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1409 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1411 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1412 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1414 snd_es18xx_config_write(chip, 0x28, chip->irq);
1417 snd_es18xx_config_write(chip, 0x70, chip->irq);
1419 snd_es18xx_config_write(chip, 0x72, chip->irq);
1421 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1423 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1425 /* Enable Audio 1 IRQ */
1426 snd_es18xx_write(chip, 0xB1, 0x50);
1427 /* Enable Audio 2 IRQ */
1428 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1429 /* Enable Audio 1 DMA */
1430 snd_es18xx_write(chip, 0xB2, 0x50);
1431 /* Enable MPU and hardware volume interrupt */
1432 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1435 int irqmask, dma1mask, dma2mask;
1436 switch (chip->irq) {
1451 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
1454 switch (chip->dma1) {
1465 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
1468 switch (chip->dma2) {
1482 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
1486 /* Enable and set Audio 1 IRQ */
1487 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1488 /* Enable and set Audio 1 DMA */
1489 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1490 /* Set Audio 2 DMA */
1491 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1492 /* Enable Audio 2 IRQ and DMA
1493 Set capture mixer input */
1494 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1495 /* Enable and set hardware volume interrupt */
1496 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1497 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1498 /* MPU401 share irq with audio
1501 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1503 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1505 if (chip->caps & ES18XX_NEW_RATE) {
1506 /* Change behaviour of register A1
1508 2nd channel DAC asynchronous */
1509 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1511 if (!(chip->caps & ES18XX_PCM2)) {
1512 /* Enable DMA FIFO */
1513 snd_es18xx_write(chip, 0xB7, 0x80);
1515 if (chip->caps & ES18XX_SPATIALIZER) {
1516 /* Set spatializer parameters to recommended values */
1517 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1518 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1519 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1520 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1522 /* Mute input source */
1523 if (chip->caps & ES18XX_MUTEREC)
1525 if (chip->caps & ES18XX_RECMIX)
1526 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1528 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1529 snd_es18xx_write(chip, 0xb4, 0x00);
1532 /* Enable PCM output */
1533 snd_es18xx_dsp_command(chip, 0xD1);
1539 static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
1544 if (snd_es18xx_reset(chip) < 0) {
1545 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
1549 snd_es18xx_dsp_command(chip, 0xe7);
1550 hi = snd_es18xx_dsp_get_byte(chip);
1554 lo = snd_es18xx_dsp_get_byte(chip);
1555 if ((lo & 0xf0) != 0x80) {
1559 chip->version = 0x488;
1565 if ((lo & 0x0f) < 8) {
1566 chip->version = 0x688;
1570 outb(0x40, chip->port + 0x04);
1572 hi = inb(chip->port + 0x05);
1574 lo = inb(chip->port + 0x05);
1576 chip->version = hi << 8 | lo;
1577 chip->ctrl_port = inb(chip->port + 0x05) << 8;
1579 chip->ctrl_port += inb(chip->port + 0x05);
1581 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1582 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1589 /* If has Hardware volume */
1590 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1592 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1593 /* If has volume count */
1594 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1595 chip->version = 0x1887;
1597 chip->version = 0x1888;
1600 chip->version = 0x1788;
1604 chip->version = 0x1688;
1608 static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
1610 if (snd_es18xx_identify(chip) < 0) {
1611 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1615 switch (chip->version) {
1617 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_HWV;
1620 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1623 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1626 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1629 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1632 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1635 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
1636 chip->port, chip->version);
1640 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1642 if (chip->dma1 == chip->dma2)
1643 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1645 return snd_es18xx_initialize(chip);
1648 static struct snd_pcm_ops snd_es18xx_playback_ops = {
1649 .open = snd_es18xx_playback_open,
1650 .close = snd_es18xx_playback_close,
1651 .ioctl = snd_pcm_lib_ioctl,
1652 .hw_params = snd_es18xx_playback_hw_params,
1653 .hw_free = snd_es18xx_pcm_hw_free,
1654 .prepare = snd_es18xx_playback_prepare,
1655 .trigger = snd_es18xx_playback_trigger,
1656 .pointer = snd_es18xx_playback_pointer,
1659 static struct snd_pcm_ops snd_es18xx_capture_ops = {
1660 .open = snd_es18xx_capture_open,
1661 .close = snd_es18xx_capture_close,
1662 .ioctl = snd_pcm_lib_ioctl,
1663 .hw_params = snd_es18xx_capture_hw_params,
1664 .hw_free = snd_es18xx_pcm_hw_free,
1665 .prepare = snd_es18xx_capture_prepare,
1666 .trigger = snd_es18xx_capture_trigger,
1667 .pointer = snd_es18xx_capture_pointer,
1670 static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
1672 struct snd_pcm *pcm;
1678 sprintf(str, "ES%x", chip->version);
1679 if (chip->caps & ES18XX_PCM2)
1680 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
1682 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
1686 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1687 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1690 pcm->private_data = chip;
1691 pcm->info_flags = 0;
1692 if (chip->caps & ES18XX_DUPLEX_SAME)
1693 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1694 if (! (chip->caps & ES18XX_PCM2))
1695 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1696 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1699 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1702 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1709 /* Power Management support functions */
1711 static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1713 struct snd_audiodrive *acard = card->private_data;
1714 struct snd_es18xx *chip = acard->chip;
1716 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1718 snd_pcm_suspend_all(chip->pcm);
1721 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1722 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1723 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1724 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1729 static int snd_es18xx_resume(struct snd_card *card)
1731 struct snd_audiodrive *acard = card->private_data;
1732 struct snd_es18xx *chip = acard->chip;
1734 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1735 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1737 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1740 #endif /* CONFIG_PM */
1742 static int snd_es18xx_free(struct snd_es18xx *chip)
1744 release_and_free_resource(chip->res_port);
1745 release_and_free_resource(chip->res_ctrl_port);
1746 release_and_free_resource(chip->res_mpu_port);
1748 free_irq(chip->irq, (void *) chip);
1749 if (chip->dma1 >= 0) {
1750 disable_dma(chip->dma1);
1751 free_dma(chip->dma1);
1753 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1754 disable_dma(chip->dma2);
1755 free_dma(chip->dma2);
1761 static int snd_es18xx_dev_free(struct snd_device *device)
1763 struct snd_es18xx *chip = device->device_data;
1764 return snd_es18xx_free(chip);
1767 static int __devinit snd_es18xx_new_device(struct snd_card *card,
1769 unsigned long mpu_port,
1770 unsigned long fm_port,
1771 int irq, int dma1, int dma2,
1772 struct snd_es18xx ** rchip)
1774 struct snd_es18xx *chip;
1775 static struct snd_device_ops ops = {
1776 .dev_free = snd_es18xx_dev_free,
1781 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1784 spin_lock_init(&chip->reg_lock);
1785 spin_lock_init(&chip->mixer_lock);
1786 spin_lock_init(&chip->ctrl_lock);
1789 chip->mpu_port = mpu_port;
1790 chip->fm_port = fm_port;
1794 chip->audio2_vol = 0x00;
1797 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1798 snd_es18xx_free(chip);
1799 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1803 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1804 snd_es18xx_free(chip);
1805 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1810 if (request_dma(dma1, "ES18xx DMA 1")) {
1811 snd_es18xx_free(chip);
1812 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1817 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1818 snd_es18xx_free(chip);
1819 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1824 if (snd_es18xx_probe(chip) < 0) {
1825 snd_es18xx_free(chip);
1828 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1829 snd_es18xx_free(chip);
1836 static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
1838 struct snd_card *card;
1844 strcpy(card->mixername, chip->pcm->name);
1846 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1847 struct snd_kcontrol *kctl;
1848 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1849 if (chip->caps & ES18XX_HWV) {
1852 chip->master_volume = kctl;
1853 kctl->private_free = snd_es18xx_hwv_free;
1856 chip->master_switch = kctl;
1857 kctl->private_free = snd_es18xx_hwv_free;
1861 if ((err = snd_ctl_add(card, kctl)) < 0)
1864 if (chip->caps & ES18XX_PCM2) {
1865 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1866 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1870 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1871 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1876 if (chip->caps & ES18XX_RECMIX) {
1877 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1878 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1882 switch (chip->version) {
1884 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1889 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1893 if (chip->caps & ES18XX_SPATIALIZER) {
1894 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1895 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1899 if (chip->caps & ES18XX_HWV) {
1900 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1901 struct snd_kcontrol *kctl;
1902 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1904 chip->hw_volume = kctl;
1906 chip->hw_switch = kctl;
1907 kctl->private_free = snd_es18xx_hwv_free;
1908 if ((err = snd_ctl_add(card, kctl)) < 0)
1913 /* finish initializing other chipset specific controls
1915 if (chip->version != 0x1868) {
1916 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1921 if (chip->version == 0x1869) {
1922 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1923 err = snd_ctl_add(card,
1924 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1936 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1937 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1938 MODULE_LICENSE("GPL");
1939 MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1940 "{ESS,ES1869 PnP AudioDrive},"
1941 "{ESS,ES1878 PnP AudioDrive},"
1942 "{ESS,ES1879 PnP AudioDrive},"
1943 "{ESS,ES1887 PnP AudioDrive},"
1944 "{ESS,ES1888 PnP AudioDrive},"
1945 "{ESS,ES1887 AudioDrive},"
1946 "{ESS,ES1888 AudioDrive}}");
1948 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1949 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1950 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1952 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1954 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1956 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1958 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1960 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1961 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1962 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1963 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1965 module_param_array(index, int, NULL, 0444);
1966 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1967 module_param_array(id, charp, NULL, 0444);
1968 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1969 module_param_array(enable, bool, NULL, 0444);
1970 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1972 module_param_array(isapnp, bool, NULL, 0444);
1973 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1975 module_param_array(port, long, NULL, 0444);
1976 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1977 module_param_array(mpu_port, long, NULL, 0444);
1978 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1979 module_param_array(fm_port, long, NULL, 0444);
1980 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1981 module_param_array(irq, int, NULL, 0444);
1982 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1983 module_param_array(dma1, int, NULL, 0444);
1984 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1985 module_param_array(dma2, int, NULL, 0444);
1986 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1988 static struct platform_device *platform_devices[SNDRV_CARDS];
1991 static int pnp_registered;
1993 static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1994 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1995 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1996 /* ESS 1868 (integrated on Maxisound Cards) */
1997 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1998 /* ESS 1868 (integrated on Maxisound Cards) */
1999 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2000 /* ESS ES1869 Plug and Play AudioDrive */
2001 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2003 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2005 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2007 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2009 { .id = "" } /* end */
2012 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2014 static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2015 struct pnp_card_link *card,
2016 const struct pnp_card_device_id *id)
2018 struct pnp_dev *pdev;
2019 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
2024 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2025 if (acard->dev == NULL) {
2029 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2030 if (acard->devc == NULL) {
2034 /* Control port initialization */
2035 err = pnp_activate_dev(acard->devc);
2037 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2040 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
2041 /* PnP initialization */
2043 pnp_init_resource_table(cfg);
2044 if (port[dev] != SNDRV_AUTO_PORT)
2045 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
2046 if (fm_port[dev] != SNDRV_AUTO_PORT)
2047 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
2048 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2049 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
2050 if (dma1[dev] != SNDRV_AUTO_DMA)
2051 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
2052 if (dma2[dev] != SNDRV_AUTO_DMA)
2053 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
2054 if (irq[dev] != SNDRV_AUTO_IRQ)
2055 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
2056 err = pnp_manual_config_dev(pdev, cfg, 0);
2058 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
2059 err = pnp_activate_dev(pdev);
2061 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2065 /* ok. hack using Vendor-Defined Card-Level registers */
2066 /* skip csn and logdev initialization - already done in isapnp_configure */
2067 if (pnp_device_is_isapnp(pdev)) {
2068 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2069 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2070 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2071 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2072 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2075 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
2077 port[dev] = pnp_port_start(pdev, 0);
2078 fm_port[dev] = pnp_port_start(pdev, 1);
2079 mpu_port[dev] = pnp_port_start(pdev, 2);
2080 dma1[dev] = pnp_dma(pdev, 0);
2081 dma2[dev] = pnp_dma(pdev, 1);
2082 irq[dev] = pnp_irq(pdev, 0);
2083 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2084 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2088 #endif /* CONFIG_PNP */
2091 #define is_isapnp_selected(dev) isapnp[dev]
2093 #define is_isapnp_selected(dev) 0
2096 static struct snd_card *snd_es18xx_card_new(int dev)
2098 return snd_card_new(index[dev], id[dev], THIS_MODULE,
2099 sizeof(struct snd_audiodrive));
2102 static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2104 struct snd_audiodrive *acard = card->private_data;
2105 struct snd_es18xx *chip;
2106 struct snd_opl3 *opl3;
2109 if ((err = snd_es18xx_new_device(card,
2113 irq[dev], dma1[dev], dma2[dev],
2118 sprintf(card->driver, "ES%x", chip->version);
2120 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2121 if (dma1[dev] != dma2[dev])
2122 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2125 irq[dev], dma1[dev], dma2[dev]);
2127 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2130 irq[dev], dma1[dev]);
2132 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
2135 if ((err = snd_es18xx_mixer(chip)) < 0)
2138 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2139 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
2140 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
2142 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
2147 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2148 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2155 return snd_card_register(card);
2158 static int __init snd_es18xx_nonpnp_probe1(int dev, struct platform_device *devptr)
2160 struct snd_card *card;
2163 card = snd_es18xx_card_new(dev);
2166 snd_card_set_dev(card, &devptr->dev);
2167 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2168 snd_card_free(card);
2171 platform_set_drvdata(devptr, card);
2175 static int __init snd_es18xx_nonpnp_probe(struct platform_device *pdev)
2179 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2180 static int possible_dmas[] = {1, 0, 3, 5, -1};
2182 if (irq[dev] == SNDRV_AUTO_IRQ) {
2183 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2184 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2188 if (dma1[dev] == SNDRV_AUTO_DMA) {
2189 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2190 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2194 if (dma2[dev] == SNDRV_AUTO_DMA) {
2195 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2196 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2201 if (port[dev] != SNDRV_AUTO_PORT) {
2202 return snd_es18xx_nonpnp_probe1(dev, pdev);
2204 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2206 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2207 port[dev] = possible_ports[i];
2208 err = snd_es18xx_nonpnp_probe1(dev, pdev);
2216 static int __devexit snd_es18xx_nonpnp_remove(struct platform_device *devptr)
2218 snd_card_free(platform_get_drvdata(devptr));
2219 platform_set_drvdata(devptr, NULL);
2224 static int snd_es18xx_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
2226 return snd_es18xx_suspend(platform_get_drvdata(dev), state);
2229 static int snd_es18xx_nonpnp_resume(struct platform_device *dev)
2231 return snd_es18xx_resume(platform_get_drvdata(dev));
2235 #define ES18XX_DRIVER "snd_es18xx"
2237 static struct platform_driver snd_es18xx_nonpnp_driver = {
2238 .probe = snd_es18xx_nonpnp_probe,
2239 .remove = __devexit_p(snd_es18xx_nonpnp_remove),
2241 .suspend = snd_es18xx_nonpnp_suspend,
2242 .resume = snd_es18xx_nonpnp_resume,
2245 .name = ES18XX_DRIVER
2251 static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *pcard,
2252 const struct pnp_card_device_id *pid)
2255 struct snd_card *card;
2258 for ( ; dev < SNDRV_CARDS; dev++) {
2259 if (enable[dev] && isapnp[dev])
2262 if (dev >= SNDRV_CARDS)
2265 card = snd_es18xx_card_new(dev);
2269 if ((res = snd_audiodrive_pnp(dev, card->private_data, pcard, pid)) < 0) {
2270 snd_card_free(card);
2273 snd_card_set_dev(card, &pcard->card->dev);
2274 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2275 snd_card_free(card);
2279 pnp_set_card_drvdata(pcard, card);
2284 static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2286 snd_card_free(pnp_get_card_drvdata(pcard));
2287 pnp_set_card_drvdata(pcard, NULL);
2291 static int snd_audiodrive_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
2293 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2296 static int snd_audiodrive_pnp_resume(struct pnp_card_link *pcard)
2298 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2303 static struct pnp_card_driver es18xx_pnpc_driver = {
2304 .flags = PNP_DRIVER_RES_DISABLE,
2306 .id_table = snd_audiodrive_pnpids,
2307 .probe = snd_audiodrive_pnp_detect,
2308 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2310 .suspend = snd_audiodrive_pnp_suspend,
2311 .resume = snd_audiodrive_pnp_resume,
2314 #endif /* CONFIG_PNP */
2316 static void __init_or_module snd_es18xx_unregister_all(void)
2322 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2324 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
2325 platform_device_unregister(platform_devices[i]);
2326 platform_driver_unregister(&snd_es18xx_nonpnp_driver);
2329 static int __init alsa_card_es18xx_init(void)
2331 int i, err, cards = 0;
2333 if ((err = platform_driver_register(&snd_es18xx_nonpnp_driver)) < 0)
2336 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
2337 struct platform_device *device;
2338 if (is_isapnp_selected(i))
2340 device = platform_device_register_simple(ES18XX_DRIVER,
2342 if (IS_ERR(device)) {
2343 err = PTR_ERR(device);
2346 platform_devices[i] = device;
2351 i = pnp_register_card_driver(&es18xx_pnpc_driver);
2360 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2368 snd_es18xx_unregister_all();
2372 static void __exit alsa_card_es18xx_exit(void)
2374 snd_es18xx_unregister_all();
2377 module_init(alsa_card_es18xx_init)
2378 module_exit(alsa_card_es18xx_exit)