blob: 06c0e778494ebd14b85bc39152cf1a38e75eeb1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22/* GENERAL NOTES:
23 *
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.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
52 * BUGS:
53 *
54 * - There is a major trouble I noted:
55 *
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.
58 *
59 * The same happens using Audio1 for captureing.
60 *
61 * The Windows driver does not suffer of this (although it use Audio1
62 * only for captureing). I'm unable to discover why.
63 *
64 */
65
66
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>
86
87#define PFX "es18xx: "
88
Takashi Iwai8047c912005-11-17 14:41:22 +010089struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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 */
103
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;
109
Takashi Iwai8047c912005-11-17 14:41:22 +0100110 struct snd_card *card;
111 struct snd_pcm *pcm;
112 struct snd_pcm_substream *playback_a_substream;
113 struct snd_pcm_substream *capture_a_substream;
114 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwai8047c912005-11-17 14:41:22 +0100116 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai8047c912005-11-17 14:41:22 +0100118 struct snd_kcontrol *hw_volume;
119 struct snd_kcontrol *hw_switch;
120 struct snd_kcontrol *master_volume;
121 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
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
129};
130
131#define AUDIO1_IRQ 0x01
132#define AUDIO2_IRQ 0x02
133#define HWV_IRQ 0x04
134#define MPU_IRQ 0x08
135
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 */
148
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
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Lowlevel */
159
160#define DAC1 0x01
161#define ADC1 0x02
162#define DAC2 0x04
163#define MILLISECOND 10000
164
Takashi Iwai8047c912005-11-17 14:41:22 +0100165static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int i;
168
169 for(i = MILLISECOND; i; i--)
170 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
171 outb(val, chip->port + 0x0C);
172 return 0;
173 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200174 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -EINVAL;
176}
177
Takashi Iwai8047c912005-11-17 14:41:22 +0100178static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int i;
181
182 for(i = MILLISECOND/10; i; i--)
183 if (inb(chip->port + 0x0C) & 0x40)
184 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200185 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
186 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -ENODEV;
188}
189
190#undef REG_DEBUG
191
Takashi Iwai8047c912005-11-17 14:41:22 +0100192static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned char reg, unsigned char data)
194{
195 unsigned long flags;
196 int ret;
197
198 spin_lock_irqsave(&chip->reg_lock, flags);
199 ret = snd_es18xx_dsp_command(chip, reg);
200 if (ret < 0)
201 goto end;
202 ret = snd_es18xx_dsp_command(chip, data);
203 end:
204 spin_unlock_irqrestore(&chip->reg_lock, flags);
205#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200206 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#endif
208 return ret;
209}
210
Takashi Iwai8047c912005-11-17 14:41:22 +0100211static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned long flags;
214 int ret, data;
215 spin_lock_irqsave(&chip->reg_lock, flags);
216 ret = snd_es18xx_dsp_command(chip, 0xC0);
217 if (ret < 0)
218 goto end;
219 ret = snd_es18xx_dsp_command(chip, reg);
220 if (ret < 0)
221 goto end;
222 data = snd_es18xx_dsp_get_byte(chip);
223 ret = data;
224#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200225 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
227 end:
228 spin_unlock_irqrestore(&chip->reg_lock, flags);
229 return ret;
230}
231
232/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100233static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned char mask, unsigned char val)
235{
236 int ret;
237 unsigned char old, new, oval;
238 unsigned long flags;
239 spin_lock_irqsave(&chip->reg_lock, flags);
240 ret = snd_es18xx_dsp_command(chip, 0xC0);
241 if (ret < 0)
242 goto end;
243 ret = snd_es18xx_dsp_command(chip, reg);
244 if (ret < 0)
245 goto end;
246 ret = snd_es18xx_dsp_get_byte(chip);
247 if (ret < 0) {
248 goto end;
249 }
250 old = ret;
251 oval = old & mask;
252 if (val != oval) {
253 ret = snd_es18xx_dsp_command(chip, reg);
254 if (ret < 0)
255 goto end;
256 new = (old & ~mask) | (val & mask);
257 ret = snd_es18xx_dsp_command(chip, new);
258 if (ret < 0)
259 goto end;
260#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200261 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
262 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif
264 }
265 ret = oval;
266 end:
267 spin_unlock_irqrestore(&chip->reg_lock, flags);
268 return ret;
269}
270
Takashi Iwai8047c912005-11-17 14:41:22 +0100271static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned char reg, unsigned char data)
273{
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
Takashi Iwai99b359b2005-10-20 18:26:44 +0200280 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif
282}
283
Takashi Iwai8047c912005-11-17 14:41:22 +0100284static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
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
Takashi Iwai99b359b2005-10-20 18:26:44 +0200293 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#endif
295 return data;
296}
297
298/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100299static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned char mask, unsigned char val)
301{
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
Takashi Iwai99b359b2005-10-20 18:26:44 +0200312 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
313 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif
315 }
316 spin_unlock_irqrestore(&chip->mixer_lock, flags);
317 return oval;
318}
319
Takashi Iwai8047c912005-11-17 14:41:22 +0100320static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned char mask)
322{
323 int old, expected, new;
324 unsigned long flags;
325 spin_lock_irqsave(&chip->mixer_lock, flags);
326 outb(reg, chip->port + 0x04);
327 old = inb(chip->port + 0x05);
328 expected = old ^ mask;
329 outb(expected, chip->port + 0x05);
330 new = inb(chip->port + 0x05);
331 spin_unlock_irqrestore(&chip->mixer_lock, flags);
332#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200333 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
334 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
336 return expected == new;
337}
338
339
Takashi Iwai8047c912005-11-17 14:41:22 +0100340static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 int i;
343 outb(0x03, chip->port + 0x06);
344 inb(chip->port + 0x06);
345 outb(0x00, chip->port + 0x06);
346 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
347 if (inb(chip->port + 0x0A) != 0xAA)
348 return -1;
349 return 0;
350}
351
Takashi Iwai8047c912005-11-17 14:41:22 +0100352static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 outb(0x02, chip->port + 0x06);
355 inb(chip->port + 0x06);
356 outb(0x00, chip->port + 0x06);
357 return 0;
358}
359
Takashi Iwai8047c912005-11-17 14:41:22 +0100360static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 {
362 .num = 793800,
363 .den_min = 1,
364 .den_max = 128,
365 .den_step = 1,
366 },
367 {
368 .num = 768000,
369 .den_min = 1,
370 .den_max = 128,
371 .den_step = 1,
372 }
373};
374
Takashi Iwai8047c912005-11-17 14:41:22 +0100375static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 .nrats = 2,
377 .rats = new_clocks,
378};
379
Takashi Iwai8047c912005-11-17 14:41:22 +0100380static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 {
382 .num = 795444,
383 .den_min = 1,
384 .den_max = 128,
385 .den_step = 1,
386 },
387 {
388 .num = 397722,
389 .den_min = 1,
390 .den_max = 128,
391 .den_step = 1,
392 }
393};
394
Takashi Iwai8047c912005-11-17 14:41:22 +0100395static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .nrats = 2,
397 .rats = old_clocks,
398};
399
400
Takashi Iwai8047c912005-11-17 14:41:22 +0100401static void snd_es18xx_rate_set(struct snd_es18xx *chip,
402 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int mode)
404{
405 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100406 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (chip->caps & ES18XX_NEW_RATE) {
408 if (runtime->rate_num == new_clocks[0].num)
409 bits = 128 - runtime->rate_den;
410 else
411 bits = 256 - runtime->rate_den;
412 } else {
413 if (runtime->rate_num == old_clocks[0].num)
414 bits = 256 - runtime->rate_den;
415 else
416 bits = 128 - runtime->rate_den;
417 }
418
419 /* set filter register */
420 div0 = 256 - 7160000*20/(8*82*runtime->rate);
421
422 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
423 snd_es18xx_mixer_write(chip, 0x70, bits);
424 /*
425 * Comment from kernel oss driver:
426 * FKS: fascinating: 0x72 doesn't seem to work.
427 */
428 snd_es18xx_write(chip, 0xA2, div0);
429 snd_es18xx_mixer_write(chip, 0x72, div0);
430 } else {
431 snd_es18xx_write(chip, 0xA1, bits);
432 snd_es18xx_write(chip, 0xA2, div0);
433 }
434}
435
Takashi Iwai8047c912005-11-17 14:41:22 +0100436static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
437 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai8047c912005-11-17 14:41:22 +0100439 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int shift, err;
441
442 shift = 0;
443 if (params_channels(hw_params) == 2)
444 shift++;
445 if (snd_pcm_format_width(params_format(hw_params)) == 16)
446 shift++;
447
448 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
449 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
450 (chip->capture_a_substream) &&
451 params_channels(hw_params) != 1) {
452 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
453 return -EBUSY;
454 }
455 chip->dma2_shift = shift;
456 } else {
457 chip->dma1_shift = shift;
458 }
459 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
460 return err;
461 return 0;
462}
463
Takashi Iwai8047c912005-11-17 14:41:22 +0100464static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 return snd_pcm_lib_free_pages(substream);
467}
468
Takashi Iwai8047c912005-11-17 14:41:22 +0100469static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
470 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Takashi Iwai8047c912005-11-17 14:41:22 +0100472 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
474 unsigned int count = snd_pcm_lib_period_bytes(substream);
475
476 chip->dma2_size = size;
477
478 snd_es18xx_rate_set(chip, substream, DAC2);
479
480 /* Transfer Count Reload */
481 count = 0x10000 - count;
482 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
483 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
484
485 /* Set format */
486 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
487 ((runtime->channels == 1) ? 0x00 : 0x02) |
488 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
489 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
490
491 /* Set DMA controller */
492 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
493
494 return 0;
495}
496
Takashi Iwai8047c912005-11-17 14:41:22 +0100497static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
498 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int cmd)
500{
501 switch (cmd) {
502 case SNDRV_PCM_TRIGGER_START:
503 case SNDRV_PCM_TRIGGER_RESUME:
504 if (chip->active & DAC2)
505 return 0;
506 chip->active |= DAC2;
507 /* Start DMA */
508 if (chip->dma2 >= 4)
509 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
510 else
511 snd_es18xx_mixer_write(chip, 0x78, 0x93);
512#ifdef AVOID_POPS
513 /* Avoid pops */
514 udelay(100000);
515 if (chip->caps & ES18XX_PCM2)
516 /* Restore Audio 2 volume */
517 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
518 else
519 /* Enable PCM output */
520 snd_es18xx_dsp_command(chip, 0xD1);
521#endif
522 break;
523 case SNDRV_PCM_TRIGGER_STOP:
524 case SNDRV_PCM_TRIGGER_SUSPEND:
525 if (!(chip->active & DAC2))
526 return 0;
527 chip->active &= ~DAC2;
528 /* Stop DMA */
529 snd_es18xx_mixer_write(chip, 0x78, 0x00);
530#ifdef AVOID_POPS
531 udelay(25000);
532 if (chip->caps & ES18XX_PCM2)
533 /* Set Audio 2 volume to 0 */
534 snd_es18xx_mixer_write(chip, 0x7C, 0);
535 else
536 /* Disable PCM output */
537 snd_es18xx_dsp_command(chip, 0xD3);
538#endif
539 break;
540 default:
541 return -EINVAL;
542 }
543
544 return 0;
545}
546
Takashi Iwai8047c912005-11-17 14:41:22 +0100547static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
548 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Takashi Iwai8047c912005-11-17 14:41:22 +0100550 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int shift, err;
552
553 shift = 0;
554 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
555 chip->playback_a_substream &&
556 params_channels(hw_params) != 1) {
557 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
558 return -EBUSY;
559 }
560 if (params_channels(hw_params) == 2)
561 shift++;
562 if (snd_pcm_format_width(params_format(hw_params)) == 16)
563 shift++;
564 chip->dma1_shift = shift;
565 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
566 return err;
567 return 0;
568}
569
Takashi Iwai8047c912005-11-17 14:41:22 +0100570static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai8047c912005-11-17 14:41:22 +0100572 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
573 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
575 unsigned int count = snd_pcm_lib_period_bytes(substream);
576
577 chip->dma1_size = size;
578
579 snd_es18xx_reset_fifo(chip);
580
581 /* Set stereo/mono */
582 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
583
584 snd_es18xx_rate_set(chip, substream, ADC1);
585
586 /* Transfer Count Reload */
587 count = 0x10000 - count;
588 snd_es18xx_write(chip, 0xA4, count & 0xff);
589 snd_es18xx_write(chip, 0xA5, count >> 8);
590
591#ifdef AVOID_POPS
592 udelay(100000);
593#endif
594
595 /* Set format */
596 snd_es18xx_write(chip, 0xB7,
597 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
598 snd_es18xx_write(chip, 0xB7, 0x90 |
599 ((runtime->channels == 1) ? 0x40 : 0x08) |
600 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
601 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
602
603 /* Set DMA controler */
604 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
605
606 return 0;
607}
608
Takashi Iwai8047c912005-11-17 14:41:22 +0100609static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int cmd)
611{
Takashi Iwai8047c912005-11-17 14:41:22 +0100612 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 switch (cmd) {
615 case SNDRV_PCM_TRIGGER_START:
616 case SNDRV_PCM_TRIGGER_RESUME:
617 if (chip->active & ADC1)
618 return 0;
619 chip->active |= ADC1;
620 /* Start DMA */
621 snd_es18xx_write(chip, 0xB8, 0x0f);
622 break;
623 case SNDRV_PCM_TRIGGER_STOP:
624 case SNDRV_PCM_TRIGGER_SUSPEND:
625 if (!(chip->active & ADC1))
626 return 0;
627 chip->active &= ~ADC1;
628 /* Stop DMA */
629 snd_es18xx_write(chip, 0xB8, 0x00);
630 break;
631 default:
632 return -EINVAL;
633 }
634
635 return 0;
636}
637
Takashi Iwai8047c912005-11-17 14:41:22 +0100638static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
639 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Takashi Iwai8047c912005-11-17 14:41:22 +0100641 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
643 unsigned int count = snd_pcm_lib_period_bytes(substream);
644
645 chip->dma1_size = size;
646
647 snd_es18xx_reset_fifo(chip);
648
649 /* Set stereo/mono */
650 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
651
652 snd_es18xx_rate_set(chip, substream, DAC1);
653
654 /* Transfer Count Reload */
655 count = 0x10000 - count;
656 snd_es18xx_write(chip, 0xA4, count & 0xff);
657 snd_es18xx_write(chip, 0xA5, count >> 8);
658
659 /* Set format */
660 snd_es18xx_write(chip, 0xB6,
661 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
662 snd_es18xx_write(chip, 0xB7,
663 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
664 snd_es18xx_write(chip, 0xB7, 0x90 |
665 (runtime->channels == 1 ? 0x40 : 0x08) |
666 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
667 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
668
669 /* Set DMA controler */
670 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
671
672 return 0;
673}
674
Takashi Iwai8047c912005-11-17 14:41:22 +0100675static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
676 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int cmd)
678{
679 switch (cmd) {
680 case SNDRV_PCM_TRIGGER_START:
681 case SNDRV_PCM_TRIGGER_RESUME:
682 if (chip->active & DAC1)
683 return 0;
684 chip->active |= DAC1;
685 /* Start DMA */
686 snd_es18xx_write(chip, 0xB8, 0x05);
687#ifdef AVOID_POPS
688 /* Avoid pops */
689 udelay(100000);
690 /* Enable Audio 1 */
691 snd_es18xx_dsp_command(chip, 0xD1);
692#endif
693 break;
694 case SNDRV_PCM_TRIGGER_STOP:
695 case SNDRV_PCM_TRIGGER_SUSPEND:
696 if (!(chip->active & DAC1))
697 return 0;
698 chip->active &= ~DAC1;
699 /* Stop DMA */
700 snd_es18xx_write(chip, 0xB8, 0x00);
701#ifdef AVOID_POPS
702 /* Avoid pops */
703 udelay(25000);
704 /* Disable Audio 1 */
705 snd_es18xx_dsp_command(chip, 0xD3);
706#endif
707 break;
708 default:
709 return -EINVAL;
710 }
711
712 return 0;
713}
714
Takashi Iwai8047c912005-11-17 14:41:22 +0100715static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Takashi Iwai8047c912005-11-17 14:41:22 +0100717 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
719 return snd_es18xx_playback1_prepare(chip, substream);
720 else
721 return snd_es18xx_playback2_prepare(chip, substream);
722}
723
Takashi Iwai8047c912005-11-17 14:41:22 +0100724static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int cmd)
726{
Takashi Iwai8047c912005-11-17 14:41:22 +0100727 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
729 return snd_es18xx_playback1_trigger(chip, substream, cmd);
730 else
731 return snd_es18xx_playback2_trigger(chip, substream, cmd);
732}
733
734static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
735{
Takashi Iwai8047c912005-11-17 14:41:22 +0100736 struct snd_es18xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned char status;
738
739 if (chip->caps & ES18XX_CONTROL) {
740 /* Read Interrupt status */
741 status = inb(chip->ctrl_port + 6);
742 } else {
743 /* Read Interrupt status */
744 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
745 }
746#if 0
747 else {
748 status = 0;
749 if (inb(chip->port + 0x0C) & 0x01)
750 status |= AUDIO1_IRQ;
751 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
752 status |= AUDIO2_IRQ;
753 if ((chip->caps & ES18XX_HWV) &&
754 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
755 status |= HWV_IRQ;
756 }
757#endif
758
759 /* Audio 1 & Audio 2 */
760 if (status & AUDIO2_IRQ) {
761 if (chip->active & DAC2)
762 snd_pcm_period_elapsed(chip->playback_a_substream);
763 /* ack interrupt */
764 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
765 }
766 if (status & AUDIO1_IRQ) {
767 /* ok.. capture is active */
768 if (chip->active & ADC1)
769 snd_pcm_period_elapsed(chip->capture_a_substream);
770 /* ok.. playback2 is active */
771 else if (chip->active & DAC1)
772 snd_pcm_period_elapsed(chip->playback_b_substream);
773 /* ack interrupt */
774 inb(chip->port + 0x0E);
775 }
776
777 /* MPU */
778 if ((status & MPU_IRQ) && chip->rmidi)
779 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
780
781 /* Hardware volume */
782 if (status & HWV_IRQ) {
783 int split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
784 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
785 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
786 if (!split) {
787 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
788 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
789 }
790 /* ack interrupt */
791 snd_es18xx_mixer_write(chip, 0x66, 0x00);
792 }
793 return IRQ_HANDLED;
794}
795
Takashi Iwai8047c912005-11-17 14:41:22 +0100796static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai8047c912005-11-17 14:41:22 +0100798 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int pos;
800
801 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
802 if (!(chip->active & DAC2))
803 return 0;
804 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
805 return pos >> chip->dma2_shift;
806 } else {
807 if (!(chip->active & DAC1))
808 return 0;
809 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
810 return pos >> chip->dma1_shift;
811 }
812}
813
Takashi Iwai8047c912005-11-17 14:41:22 +0100814static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai8047c912005-11-17 14:41:22 +0100816 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int pos;
818
819 if (!(chip->active & ADC1))
820 return 0;
821 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
822 return pos >> chip->dma1_shift;
823}
824
Takashi Iwai8047c912005-11-17 14:41:22 +0100825static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
828 SNDRV_PCM_INFO_RESUME |
829 SNDRV_PCM_INFO_MMAP_VALID),
830 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
831 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
832 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
833 .rate_min = 4000,
834 .rate_max = 48000,
835 .channels_min = 1,
836 .channels_max = 2,
837 .buffer_bytes_max = 65536,
838 .period_bytes_min = 64,
839 .period_bytes_max = 65536,
840 .periods_min = 1,
841 .periods_max = 1024,
842 .fifo_size = 0,
843};
844
Takashi Iwai8047c912005-11-17 14:41:22 +0100845static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
848 SNDRV_PCM_INFO_RESUME |
849 SNDRV_PCM_INFO_MMAP_VALID),
850 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
851 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
852 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
853 .rate_min = 4000,
854 .rate_max = 48000,
855 .channels_min = 1,
856 .channels_max = 2,
857 .buffer_bytes_max = 65536,
858 .period_bytes_min = 64,
859 .period_bytes_max = 65536,
860 .periods_min = 1,
861 .periods_max = 1024,
862 .fifo_size = 0,
863};
864
Takashi Iwai8047c912005-11-17 14:41:22 +0100865static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai8047c912005-11-17 14:41:22 +0100867 struct snd_pcm_runtime *runtime = substream->runtime;
868 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
871 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
872 chip->capture_a_substream &&
873 chip->capture_a_substream->runtime->channels != 1)
874 return -EAGAIN;
875 chip->playback_a_substream = substream;
876 } else if (substream->number <= 1) {
877 if (chip->capture_a_substream)
878 return -EAGAIN;
879 chip->playback_b_substream = substream;
880 } else {
881 snd_BUG();
882 return -EINVAL;
883 }
884 substream->runtime->hw = snd_es18xx_playback;
885 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
886 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
887 return 0;
888}
889
Takashi Iwai8047c912005-11-17 14:41:22 +0100890static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Takashi Iwai8047c912005-11-17 14:41:22 +0100892 struct snd_pcm_runtime *runtime = substream->runtime;
893 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (chip->playback_b_substream)
896 return -EAGAIN;
897 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
898 chip->playback_a_substream &&
899 chip->playback_a_substream->runtime->channels != 1)
900 return -EAGAIN;
901 chip->capture_a_substream = substream;
902 substream->runtime->hw = snd_es18xx_capture;
903 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
904 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
905 return 0;
906}
907
Takashi Iwai8047c912005-11-17 14:41:22 +0100908static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Takashi Iwai8047c912005-11-17 14:41:22 +0100910 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
913 chip->playback_a_substream = NULL;
914 else
915 chip->playback_b_substream = NULL;
916
917 snd_pcm_lib_free_pages(substream);
918 return 0;
919}
920
Takashi Iwai8047c912005-11-17 14:41:22 +0100921static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Takashi Iwai8047c912005-11-17 14:41:22 +0100923 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 chip->capture_a_substream = NULL;
926 snd_pcm_lib_free_pages(substream);
927 return 0;
928}
929
930/*
931 * MIXER part
932 */
933
Takashi Iwai8047c912005-11-17 14:41:22 +0100934static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 static char *texts[8] = {
937 "Mic", "Mic Master", "CD", "AOUT",
938 "Mic1", "Mix", "Line", "Master"
939 };
940
941 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
942 uinfo->count = 1;
943 uinfo->value.enumerated.items = 8;
944 if (uinfo->value.enumerated.item > 7)
945 uinfo->value.enumerated.item = 7;
946 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
947 return 0;
948}
949
Takashi Iwai8047c912005-11-17 14:41:22 +0100950static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwai8047c912005-11-17 14:41:22 +0100952 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ucontrol->value.enumerated.item[0] = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
954 return 0;
955}
956
Takashi Iwai8047c912005-11-17 14:41:22 +0100957static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Takashi Iwai8047c912005-11-17 14:41:22 +0100959 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 unsigned char val = ucontrol->value.enumerated.item[0];
961
962 if (val > 7)
963 return -EINVAL;
964 return snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val;
965}
966
Takashi Iwai8047c912005-11-17 14:41:22 +0100967static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
970 uinfo->count = 1;
971 uinfo->value.integer.min = 0;
972 uinfo->value.integer.max = 1;
973 return 0;
974}
975
Takashi Iwai8047c912005-11-17 14:41:22 +0100976static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Takashi Iwai8047c912005-11-17 14:41:22 +0100978 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
980 ucontrol->value.integer.value[0] = !!(val & 8);
981 return 0;
982}
983
Takashi Iwai8047c912005-11-17 14:41:22 +0100984static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai8047c912005-11-17 14:41:22 +0100986 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned char oval, nval;
988 int change;
989 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
990 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
991 change = nval != oval;
992 if (change) {
993 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
994 snd_es18xx_mixer_write(chip, 0x50, nval);
995 }
996 return change;
997}
998
Takashi Iwai8047c912005-11-17 14:41:22 +0100999static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1002 uinfo->count = 2;
1003 uinfo->value.integer.min = 0;
1004 uinfo->value.integer.max = 63;
1005 return 0;
1006}
1007
Takashi Iwai8047c912005-11-17 14:41:22 +01001008static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Takashi Iwai8047c912005-11-17 14:41:22 +01001010 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1012 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1013 return 0;
1014}
1015
Takashi Iwai8047c912005-11-17 14:41:22 +01001016static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1019 uinfo->count = 2;
1020 uinfo->value.integer.min = 0;
1021 uinfo->value.integer.max = 1;
1022 return 0;
1023}
1024
Takashi Iwai8047c912005-11-17 14:41:22 +01001025static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwai8047c912005-11-17 14:41:22 +01001027 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1029 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1030 return 0;
1031}
1032
Takashi Iwai8047c912005-11-17 14:41:22 +01001033static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai8047c912005-11-17 14:41:22 +01001035 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 chip->master_volume = NULL;
1037 chip->master_switch = NULL;
1038 chip->hw_volume = NULL;
1039 chip->hw_switch = NULL;
1040}
1041
Takashi Iwai8047c912005-11-17 14:41:22 +01001042static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unsigned char mask, unsigned char val)
1044{
1045 if (reg < 0xa0)
1046 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1047 else
1048 return snd_es18xx_bits(chip, reg, mask, val);
1049}
1050
Takashi Iwai8047c912005-11-17 14:41:22 +01001051static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 if (reg < 0xa0)
1054 return snd_es18xx_mixer_read(chip, reg);
1055 else
1056 return snd_es18xx_read(chip, reg);
1057}
1058
1059#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1060{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1061 .info = snd_es18xx_info_single, \
1062 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1063 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1064
Takashi Iwai8047c912005-11-17 14:41:22 +01001065static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 int mask = (kcontrol->private_value >> 16) & 0xff;
1068
1069 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1070 uinfo->count = 1;
1071 uinfo->value.integer.min = 0;
1072 uinfo->value.integer.max = mask;
1073 return 0;
1074}
1075
Takashi Iwai8047c912005-11-17 14:41:22 +01001076static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai8047c912005-11-17 14:41:22 +01001078 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int reg = kcontrol->private_value & 0xff;
1080 int shift = (kcontrol->private_value >> 8) & 0xff;
1081 int mask = (kcontrol->private_value >> 16) & 0xff;
1082 int invert = (kcontrol->private_value >> 24) & 0xff;
1083 int val;
1084
1085 val = snd_es18xx_reg_read(chip, reg);
1086 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1087 if (invert)
1088 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1089 return 0;
1090}
1091
Takashi Iwai8047c912005-11-17 14:41:22 +01001092static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Takashi Iwai8047c912005-11-17 14:41:22 +01001094 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int reg = kcontrol->private_value & 0xff;
1096 int shift = (kcontrol->private_value >> 8) & 0xff;
1097 int mask = (kcontrol->private_value >> 16) & 0xff;
1098 int invert = (kcontrol->private_value >> 24) & 0xff;
1099 unsigned char val;
1100
1101 val = (ucontrol->value.integer.value[0] & mask);
1102 if (invert)
1103 val = mask - val;
1104 mask <<= shift;
1105 val <<= shift;
1106 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1107}
1108
1109#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1110{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1111 .info = snd_es18xx_info_double, \
1112 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1113 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1114
Takashi Iwai8047c912005-11-17 14:41:22 +01001115static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 int mask = (kcontrol->private_value >> 24) & 0xff;
1118
1119 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1120 uinfo->count = 2;
1121 uinfo->value.integer.min = 0;
1122 uinfo->value.integer.max = mask;
1123 return 0;
1124}
1125
Takashi Iwai8047c912005-11-17 14:41:22 +01001126static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Takashi Iwai8047c912005-11-17 14:41:22 +01001128 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int left_reg = kcontrol->private_value & 0xff;
1130 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1131 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1132 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1133 int mask = (kcontrol->private_value >> 24) & 0xff;
1134 int invert = (kcontrol->private_value >> 22) & 1;
1135 unsigned char left, right;
1136
1137 left = snd_es18xx_reg_read(chip, left_reg);
1138 if (left_reg != right_reg)
1139 right = snd_es18xx_reg_read(chip, right_reg);
1140 else
1141 right = left;
1142 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1143 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1144 if (invert) {
1145 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1146 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1147 }
1148 return 0;
1149}
1150
Takashi Iwai8047c912005-11-17 14:41:22 +01001151static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Takashi Iwai8047c912005-11-17 14:41:22 +01001153 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int left_reg = kcontrol->private_value & 0xff;
1155 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1156 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1157 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1158 int mask = (kcontrol->private_value >> 24) & 0xff;
1159 int invert = (kcontrol->private_value >> 22) & 1;
1160 int change;
1161 unsigned char val1, val2, mask1, mask2;
1162
1163 val1 = ucontrol->value.integer.value[0] & mask;
1164 val2 = ucontrol->value.integer.value[1] & mask;
1165 if (invert) {
1166 val1 = mask - val1;
1167 val2 = mask - val2;
1168 }
1169 val1 <<= shift_left;
1170 val2 <<= shift_right;
1171 mask1 = mask << shift_left;
1172 mask2 = mask << shift_right;
1173 if (left_reg != right_reg) {
1174 change = 0;
1175 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1176 change = 1;
1177 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1178 change = 1;
1179 } else {
1180 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1181 val1 | val2) != (val1 | val2));
1182 }
1183 return change;
1184}
1185
Takashi Iwai8047c912005-11-17 14:41:22 +01001186static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1188ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1189ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1190ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1191ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1192ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1193ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1194ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1195ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0),
1196ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1197ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1198ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1199{
1200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1201 .name = "Capture Source",
1202 .info = snd_es18xx_info_mux,
1203 .get = snd_es18xx_get_mux,
1204 .put = snd_es18xx_put_mux,
1205}
1206};
1207
Takashi Iwai8047c912005-11-17 14:41:22 +01001208static struct snd_kcontrol_new snd_es18xx_mono_in_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209ES18XX_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0);
1210
Takashi Iwai8047c912005-11-17 14:41:22 +01001211static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1213ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1214ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1215ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1216ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
1217ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1218ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1219};
1220
Takashi Iwai8047c912005-11-17 14:41:22 +01001221static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1223};
1224
Takashi Iwai8047c912005-11-17 14:41:22 +01001225static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1227ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1228};
1229
Takashi Iwai8047c912005-11-17 14:41:22 +01001230static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1232{
1233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1234 .name = "3D Control - Switch",
1235 .info = snd_es18xx_info_spatializer_enable,
1236 .get = snd_es18xx_get_spatializer_enable,
1237 .put = snd_es18xx_put_spatializer_enable,
1238}
1239};
1240
Takashi Iwai8047c912005-11-17 14:41:22 +01001241static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1243
Takashi Iwai8047c912005-11-17 14:41:22 +01001244static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1246
Takashi Iwai8047c912005-11-17 14:41:22 +01001247static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1250 .name = "Hardware Master Playback Volume",
1251 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1252 .info = snd_es18xx_info_hw_volume,
1253 .get = snd_es18xx_get_hw_volume,
1254},
1255{
1256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1257 .name = "Hardware Master Playback Switch",
1258 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1259 .info = snd_es18xx_info_hw_switch,
1260 .get = snd_es18xx_get_hw_switch,
1261},
1262ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1263};
1264
1265#if 0
Takashi Iwai8047c912005-11-17 14:41:22 +01001266static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 int data;
1269 unsigned long flags;
1270 spin_lock_irqsave(&chip->ctrl_lock, flags);
1271 outb(reg, chip->ctrl_port);
1272 data = inb(chip->ctrl_port + 1);
1273 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1274 return data;
1275}
1276#endif
1277
Takashi Iwai8047c912005-11-17 14:41:22 +01001278static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned char reg, unsigned char data)
1280{
1281 /* No need for spinlocks, this function is used only in
1282 otherwise protected init code */
1283 outb(reg, chip->ctrl_port);
1284 outb(data, chip->ctrl_port + 1);
1285#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001286 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#endif
1288}
1289
Takashi Iwai8047c912005-11-17 14:41:22 +01001290static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int mask = 0;
1293
1294 /* enable extended mode */
1295 snd_es18xx_dsp_command(chip, 0xC6);
1296 /* Reset mixer registers */
1297 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1298
1299 /* Audio 1 DMA demand mode (4 bytes/request) */
1300 snd_es18xx_write(chip, 0xB9, 2);
1301 if (chip->caps & ES18XX_CONTROL) {
1302 /* Hardware volume IRQ */
1303 snd_es18xx_config_write(chip, 0x27, chip->irq);
1304 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1305 /* FM I/O */
1306 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1307 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1308 }
1309 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1310 /* MPU-401 I/O */
1311 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1312 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1313 /* MPU-401 IRQ */
1314 snd_es18xx_config_write(chip, 0x28, chip->irq);
1315 }
1316 /* Audio1 IRQ */
1317 snd_es18xx_config_write(chip, 0x70, chip->irq);
1318 /* Audio2 IRQ */
1319 snd_es18xx_config_write(chip, 0x72, chip->irq);
1320 /* Audio1 DMA */
1321 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1322 /* Audio2 DMA */
1323 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1324
1325 /* Enable Audio 1 IRQ */
1326 snd_es18xx_write(chip, 0xB1, 0x50);
1327 /* Enable Audio 2 IRQ */
1328 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1329 /* Enable Audio 1 DMA */
1330 snd_es18xx_write(chip, 0xB2, 0x50);
1331 /* Enable MPU and hardware volume interrupt */
1332 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1333 }
1334 else {
1335 int irqmask, dma1mask, dma2mask;
1336 switch (chip->irq) {
1337 case 2:
1338 case 9:
1339 irqmask = 0;
1340 break;
1341 case 5:
1342 irqmask = 1;
1343 break;
1344 case 7:
1345 irqmask = 2;
1346 break;
1347 case 10:
1348 irqmask = 3;
1349 break;
1350 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001351 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -ENODEV;
1353 }
1354 switch (chip->dma1) {
1355 case 0:
1356 dma1mask = 1;
1357 break;
1358 case 1:
1359 dma1mask = 2;
1360 break;
1361 case 3:
1362 dma1mask = 3;
1363 break;
1364 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001365 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -ENODEV;
1367 }
1368 switch (chip->dma2) {
1369 case 0:
1370 dma2mask = 0;
1371 break;
1372 case 1:
1373 dma2mask = 1;
1374 break;
1375 case 3:
1376 dma2mask = 2;
1377 break;
1378 case 5:
1379 dma2mask = 3;
1380 break;
1381 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001382 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -ENODEV;
1384 }
1385
1386 /* Enable and set Audio 1 IRQ */
1387 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1388 /* Enable and set Audio 1 DMA */
1389 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1390 /* Set Audio 2 DMA */
1391 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1392 /* Enable Audio 2 IRQ and DMA
1393 Set capture mixer input */
1394 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1395 /* Enable and set hardware volume interrupt */
1396 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1397 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1398 /* MPU401 share irq with audio
1399 Joystick enabled
1400 FM enabled */
1401 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1402 }
1403 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1404 }
1405 if (chip->caps & ES18XX_NEW_RATE) {
1406 /* Change behaviour of register A1
1407 4x oversampling
1408 2nd channel DAC asynchronous */
1409 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1410 }
1411 if (!(chip->caps & ES18XX_PCM2)) {
1412 /* Enable DMA FIFO */
1413 snd_es18xx_write(chip, 0xB7, 0x80);
1414 }
1415 if (chip->caps & ES18XX_SPATIALIZER) {
1416 /* Set spatializer parameters to recommended values */
1417 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1418 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1419 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1420 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1421 }
1422 /* Mute input source */
1423 if (chip->caps & ES18XX_MUTEREC)
1424 mask = 0x10;
1425 if (chip->caps & ES18XX_RECMIX)
1426 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1427 else {
1428 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1429 snd_es18xx_write(chip, 0xb4, 0x00);
1430 }
1431#ifndef AVOID_POPS
1432 /* Enable PCM output */
1433 snd_es18xx_dsp_command(chip, 0xD1);
1434#endif
1435
1436 return 0;
1437}
1438
Takashi Iwai8047c912005-11-17 14:41:22 +01001439static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 int hi,lo;
1442
1443 /* reset */
1444 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001445 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -ENODEV;
1447 }
1448
1449 snd_es18xx_dsp_command(chip, 0xe7);
1450 hi = snd_es18xx_dsp_get_byte(chip);
1451 if (hi < 0) {
1452 return hi;
1453 }
1454 lo = snd_es18xx_dsp_get_byte(chip);
1455 if ((lo & 0xf0) != 0x80) {
1456 return -ENODEV;
1457 }
1458 if (hi == 0x48) {
1459 chip->version = 0x488;
1460 return 0;
1461 }
1462 if (hi != 0x68) {
1463 return -ENODEV;
1464 }
1465 if ((lo & 0x0f) < 8) {
1466 chip->version = 0x688;
1467 return 0;
1468 }
1469
1470 outb(0x40, chip->port + 0x04);
1471 hi = inb(chip->port + 0x05);
1472 lo = inb(chip->port + 0x05);
1473 if (hi != lo) {
1474 chip->version = hi << 8 | lo;
1475 chip->ctrl_port = inb(chip->port + 0x05) << 8;
1476 chip->ctrl_port += inb(chip->port + 0x05);
1477
1478 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1479 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1480 return -EBUSY;
1481 }
1482
1483 return 0;
1484 }
1485
1486 /* If has Hardware volume */
1487 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1488 /* If has Audio2 */
1489 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1490 /* If has volume count */
1491 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1492 chip->version = 0x1887;
1493 } else {
1494 chip->version = 0x1888;
1495 }
1496 } else {
1497 chip->version = 0x1788;
1498 }
1499 }
1500 else
1501 chip->version = 0x1688;
1502 return 0;
1503}
1504
Takashi Iwai8047c912005-11-17 14:41:22 +01001505static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 if (snd_es18xx_identify(chip) < 0) {
1508 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1509 return -ENODEV;
1510 }
1511
1512 switch (chip->version) {
1513 case 0x1868:
1514 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_HWV;
1515 break;
1516 case 0x1869:
1517 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1518 break;
1519 case 0x1878:
1520 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1521 break;
1522 case 0x1879:
1523 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1524 break;
1525 case 0x1887:
1526 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1527 break;
1528 case 0x1888:
1529 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1530 break;
1531 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001532 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 chip->port, chip->version);
1534 return -ENODEV;
1535 }
1536
1537 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1538
1539 if (chip->dma1 == chip->dma2)
1540 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1541
1542 return snd_es18xx_initialize(chip);
1543}
1544
Takashi Iwai8047c912005-11-17 14:41:22 +01001545static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .open = snd_es18xx_playback_open,
1547 .close = snd_es18xx_playback_close,
1548 .ioctl = snd_pcm_lib_ioctl,
1549 .hw_params = snd_es18xx_playback_hw_params,
1550 .hw_free = snd_es18xx_pcm_hw_free,
1551 .prepare = snd_es18xx_playback_prepare,
1552 .trigger = snd_es18xx_playback_trigger,
1553 .pointer = snd_es18xx_playback_pointer,
1554};
1555
Takashi Iwai8047c912005-11-17 14:41:22 +01001556static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .open = snd_es18xx_capture_open,
1558 .close = snd_es18xx_capture_close,
1559 .ioctl = snd_pcm_lib_ioctl,
1560 .hw_params = snd_es18xx_capture_hw_params,
1561 .hw_free = snd_es18xx_pcm_hw_free,
1562 .prepare = snd_es18xx_capture_prepare,
1563 .trigger = snd_es18xx_capture_trigger,
1564 .pointer = snd_es18xx_capture_pointer,
1565};
1566
Takashi Iwai8047c912005-11-17 14:41:22 +01001567static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Takashi Iwai8047c912005-11-17 14:41:22 +01001569 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 char str[16];
1571 int err;
1572
1573 if (rpcm)
1574 *rpcm = NULL;
1575 sprintf(str, "ES%x", chip->version);
1576 if (chip->caps & ES18XX_PCM2) {
1577 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
1578 } else {
1579 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
1580 }
1581 if (err < 0)
1582 return err;
1583
1584 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1585 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1586
1587 /* global setup */
1588 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 pcm->info_flags = 0;
1590 if (chip->caps & ES18XX_DUPLEX_SAME)
1591 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1592 if (! (chip->caps & ES18XX_PCM2))
1593 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1594 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1595 chip->pcm = pcm;
1596
1597 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1598 snd_dma_isa_data(),
1599 64*1024,
1600 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1601
1602 if (rpcm)
1603 *rpcm = pcm;
1604 return 0;
1605}
1606
1607/* Power Management support functions */
1608#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001609static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Takashi Iwai8047c912005-11-17 14:41:22 +01001611 struct snd_es18xx *chip = card->pm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 snd_pcm_suspend_all(chip->pcm);
1614
1615 /* power down */
1616 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1617 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1618 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1619 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1620
1621 return 0;
1622}
1623
Takashi Iwai8047c912005-11-17 14:41:22 +01001624static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwai8047c912005-11-17 14:41:22 +01001626 struct snd_es18xx *chip = card->pm_private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1629 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1630
1631 return 0;
1632}
1633#endif /* CONFIG_PM */
1634
Takashi Iwai8047c912005-11-17 14:41:22 +01001635static int snd_es18xx_free(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001637 release_and_free_resource(chip->res_port);
1638 release_and_free_resource(chip->res_ctrl_port);
1639 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (chip->irq >= 0)
1641 free_irq(chip->irq, (void *) chip);
1642 if (chip->dma1 >= 0) {
1643 disable_dma(chip->dma1);
1644 free_dma(chip->dma1);
1645 }
1646 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1647 disable_dma(chip->dma2);
1648 free_dma(chip->dma2);
1649 }
1650 kfree(chip);
1651 return 0;
1652}
1653
Takashi Iwai8047c912005-11-17 14:41:22 +01001654static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwai8047c912005-11-17 14:41:22 +01001656 struct snd_es18xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return snd_es18xx_free(chip);
1658}
1659
Takashi Iwai8047c912005-11-17 14:41:22 +01001660static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 unsigned long port,
1662 unsigned long mpu_port,
1663 unsigned long fm_port,
1664 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001665 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Takashi Iwai8047c912005-11-17 14:41:22 +01001667 struct snd_es18xx *chip;
1668 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 .dev_free = snd_es18xx_dev_free,
1670 };
1671 int err;
1672
1673 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001674 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (chip == NULL)
1676 return -ENOMEM;
1677 spin_lock_init(&chip->reg_lock);
1678 spin_lock_init(&chip->mixer_lock);
1679 spin_lock_init(&chip->ctrl_lock);
1680 chip->card = card;
1681 chip->port = port;
1682 chip->mpu_port = mpu_port;
1683 chip->fm_port = fm_port;
1684 chip->irq = -1;
1685 chip->dma1 = -1;
1686 chip->dma2 = -1;
1687 chip->audio2_vol = 0x00;
1688 chip->active = 0;
1689
1690 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1691 snd_es18xx_free(chip);
1692 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1693 return -EBUSY;
1694 }
1695
1696 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1697 snd_es18xx_free(chip);
1698 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1699 return -EBUSY;
1700 }
1701 chip->irq = irq;
1702
1703 if (request_dma(dma1, "ES18xx DMA 1")) {
1704 snd_es18xx_free(chip);
1705 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1706 return -EBUSY;
1707 }
1708 chip->dma1 = dma1;
1709
1710 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1711 snd_es18xx_free(chip);
1712 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1713 return -EBUSY;
1714 }
1715 chip->dma2 = dma2;
1716
1717 if (snd_es18xx_probe(chip) < 0) {
1718 snd_es18xx_free(chip);
1719 return -ENODEV;
1720 }
1721 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1722 snd_es18xx_free(chip);
1723 return err;
1724 }
1725 *rchip = chip;
1726 return 0;
1727}
1728
Takashi Iwai8047c912005-11-17 14:41:22 +01001729static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Takashi Iwai8047c912005-11-17 14:41:22 +01001731 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int err;
1733 unsigned int idx;
1734
1735 card = chip->card;
1736
1737 strcpy(card->mixername, chip->pcm->name);
1738
1739 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001740 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1742 if (chip->caps & ES18XX_HWV) {
1743 switch (idx) {
1744 case 0:
1745 chip->master_volume = kctl;
1746 kctl->private_free = snd_es18xx_hwv_free;
1747 break;
1748 case 1:
1749 chip->master_switch = kctl;
1750 kctl->private_free = snd_es18xx_hwv_free;
1751 break;
1752 }
1753 }
1754 if ((err = snd_ctl_add(card, kctl)) < 0)
1755 return err;
1756 }
1757 if (chip->caps & ES18XX_PCM2) {
1758 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1759 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1760 return err;
1761 }
1762 } else {
1763 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1764 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1765 return err;
1766 }
1767 }
1768
1769 if (chip->caps & ES18XX_MONO) {
1770 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_mono_in_control, chip))) < 0)
1771 return err;
1772 }
1773 if (chip->caps & ES18XX_RECMIX) {
1774 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1775 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1776 return err;
1777 }
1778 }
1779 switch (chip->version) {
1780 default:
1781 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1782 return err;
1783 break;
1784 case 0x1869:
1785 case 0x1879:
1786 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1787 return err;
1788 break;
1789 }
1790 if (chip->caps & ES18XX_SPATIALIZER) {
1791 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1792 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1793 return err;
1794 }
1795 }
1796 if (chip->caps & ES18XX_HWV) {
1797 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001798 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1800 if (idx == 0)
1801 chip->hw_volume = kctl;
1802 else
1803 chip->hw_switch = kctl;
1804 kctl->private_free = snd_es18xx_hwv_free;
1805 if ((err = snd_ctl_add(card, kctl)) < 0)
1806 return err;
1807
1808 }
1809 }
1810 return 0;
1811}
1812
1813
1814/* Card level */
1815
1816MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1817MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1818MODULE_LICENSE("GPL");
1819MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1820 "{ESS,ES1869 PnP AudioDrive},"
1821 "{ESS,ES1878 PnP AudioDrive},"
1822 "{ESS,ES1879 PnP AudioDrive},"
1823 "{ESS,ES1887 PnP AudioDrive},"
1824 "{ESS,ES1888 PnP AudioDrive},"
1825 "{ESS,ES1887 AudioDrive},"
1826 "{ESS,ES1888 AudioDrive}}");
1827
1828static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1829static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1830static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1831#ifdef CONFIG_PNP
1832static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1833#endif
1834static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1835#ifndef CONFIG_PNP
1836static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1837#else
1838static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1839#endif
1840static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1841static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1842static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1843static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1844
1845module_param_array(index, int, NULL, 0444);
1846MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1847module_param_array(id, charp, NULL, 0444);
1848MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1849module_param_array(enable, bool, NULL, 0444);
1850MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1851#ifdef CONFIG_PNP
1852module_param_array(isapnp, bool, NULL, 0444);
1853MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1854#endif
1855module_param_array(port, long, NULL, 0444);
1856MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1857module_param_array(mpu_port, long, NULL, 0444);
1858MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1859module_param_array(fm_port, long, NULL, 0444);
1860MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1861module_param_array(irq, int, NULL, 0444);
1862MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1863module_param_array(dma1, int, NULL, 0444);
1864MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1865module_param_array(dma2, int, NULL, 0444);
1866MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1867
1868struct snd_audiodrive {
1869#ifdef CONFIG_PNP
1870 struct pnp_dev *dev;
1871 struct pnp_dev *devc;
1872#endif
1873};
1874
Takashi Iwai8047c912005-11-17 14:41:22 +01001875static struct snd_card *snd_audiodrive_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877#ifdef CONFIG_PNP
1878
1879static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1880 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1881 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1882 /* ESS 1868 (integrated on Maxisound Cards) */
1883 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1884 /* ESS 1868 (integrated on Maxisound Cards) */
1885 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
1886 /* ESS ES1869 Plug and Play AudioDrive */
1887 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
1888 /* ESS 1869 */
1889 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
1890 /* ESS 1878 */
1891 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
1892 /* ESS 1879 */
1893 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
1894 /* --- */
1895 { .id = "" } /* end */
1896};
1897
1898MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
1899
1900static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
1901 struct pnp_card_link *card,
1902 const struct pnp_card_device_id *id)
1903{
1904 struct pnp_dev *pdev;
1905 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
1906 int err;
1907
1908 if (!cfg)
1909 return -ENOMEM;
1910 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
1911 if (acard->dev == NULL) {
1912 kfree(cfg);
1913 return -EBUSY;
1914 }
1915 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
1916 if (acard->devc == NULL) {
1917 kfree(cfg);
1918 return -EBUSY;
1919 }
1920 /* Control port initialization */
1921 err = pnp_activate_dev(acard->devc);
1922 if (err < 0) {
1923 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
1924 return -EAGAIN;
1925 }
1926 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
1927 /* PnP initialization */
1928 pdev = acard->dev;
1929 pnp_init_resource_table(cfg);
1930 if (port[dev] != SNDRV_AUTO_PORT)
1931 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
1932 if (fm_port[dev] != SNDRV_AUTO_PORT)
1933 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
1934 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1935 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
1936 if (dma1[dev] != SNDRV_AUTO_DMA)
1937 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
1938 if (dma2[dev] != SNDRV_AUTO_DMA)
1939 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
1940 if (irq[dev] != SNDRV_AUTO_IRQ)
1941 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
1942 err = pnp_manual_config_dev(pdev, cfg, 0);
1943 if (err < 0)
1944 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
1945 err = pnp_activate_dev(pdev);
1946 if (err < 0) {
1947 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1948 kfree(cfg);
1949 return -EBUSY;
1950 }
1951 /* ok. hack using Vendor-Defined Card-Level registers */
1952 /* skip csn and logdev initialization - already done in isapnp_configure */
1953 if (pnp_device_is_isapnp(pdev)) {
1954 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1955 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1956 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1957 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1958 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1959 isapnp_cfg_end();
1960 } else {
1961 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
1962 }
1963 port[dev] = pnp_port_start(pdev, 0);
1964 fm_port[dev] = pnp_port_start(pdev, 1);
1965 mpu_port[dev] = pnp_port_start(pdev, 2);
1966 dma1[dev] = pnp_dma(pdev, 0);
1967 dma2[dev] = pnp_dma(pdev, 1);
1968 irq[dev] = pnp_irq(pdev, 0);
1969 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
1970 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
1971 kfree(cfg);
1972 return 0;
1973}
1974#endif /* CONFIG_PNP */
1975
Takashi Iwai43bcd972005-09-05 17:19:20 +02001976#ifdef CONFIG_PNP
1977#define is_isapnp_selected(dev) isapnp[dev]
1978#else
1979#define is_isapnp_selected(dev) 0
1980#endif
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982static int __devinit snd_audiodrive_probe(int dev, struct pnp_card_link *pcard,
1983 const struct pnp_card_device_id *pid)
1984{
1985 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
1986 static int possible_dmas[] = {1, 0, 3, 5, -1};
1987 int xirq, xdma1, xdma2;
Takashi Iwai8047c912005-11-17 14:41:22 +01001988 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct snd_audiodrive *acard;
Takashi Iwai8047c912005-11-17 14:41:22 +01001990 struct snd_es18xx *chip;
1991 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int err;
1993
1994 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1995 sizeof(struct snd_audiodrive));
1996 if (card == NULL)
1997 return -ENOMEM;
1998 acard = (struct snd_audiodrive *)card->private_data;
1999#ifdef CONFIG_PNP
2000 if (isapnp[dev]) {
2001 if ((err = snd_audiodrive_pnp(dev, acard, pcard, pid)) < 0) {
2002 snd_card_free(card);
2003 return err;
2004 }
2005 snd_card_set_dev(card, &pcard->card->dev);
2006 }
2007#endif
2008
2009 xirq = irq[dev];
2010 if (xirq == SNDRV_AUTO_IRQ) {
2011 if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002012 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2013 err = -EBUSY;
2014 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016 }
2017 xdma1 = dma1[dev];
2018 if (xdma1 == SNDRV_AUTO_DMA) {
2019 if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002020 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2021 err = -EBUSY;
2022 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 }
2025 xdma2 = dma2[dev];
2026 if (xdma2 == SNDRV_AUTO_DMA) {
2027 if ((xdma2 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002028 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2029 err = -EBUSY;
2030 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032 }
2033
2034 if ((err = snd_es18xx_new_device(card,
2035 port[dev],
2036 mpu_port[dev],
2037 fm_port[dev],
2038 xirq, xdma1, xdma2,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002039 &chip)) < 0)
2040 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 sprintf(card->driver, "ES%x", chip->version);
2043 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2044 if (xdma1 != xdma2)
2045 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2046 card->shortname,
2047 chip->port,
2048 xirq, xdma1, xdma2);
2049 else
2050 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2051 card->shortname,
2052 chip->port,
2053 xirq, xdma1);
2054
Takashi Iwai43bcd972005-09-05 17:19:20 +02002055 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
2056 goto _err;
2057
2058 if ((err = snd_es18xx_mixer(chip)) < 0)
2059 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2062 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002063 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002065 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
2066 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 }
2069
2070 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2071 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2072 chip->mpu_port, 0,
2073 xirq, 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002074 &chip->rmidi)) < 0)
2075 goto _err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077
Takashi Iwai43bcd972005-09-05 17:19:20 +02002078 if ((err = snd_card_set_generic_dev(card)) < 0)
2079 goto _err;
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* Power Management */
2082 snd_card_set_isa_pm_callback(card, snd_es18xx_suspend, snd_es18xx_resume, chip);
2083
Takashi Iwai43bcd972005-09-05 17:19:20 +02002084 if ((err = snd_card_register(card)) < 0)
2085 goto _err;
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (pcard)
2088 pnp_set_card_drvdata(pcard, card);
2089 else
2090 snd_audiodrive_legacy[dev] = card;
2091 return 0;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002092
2093 _err:
2094 snd_card_free(card);
2095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098static int __devinit snd_audiodrive_probe_legacy_port(unsigned long xport)
2099{
2100 static int dev;
2101 int res;
2102
2103 for ( ; dev < SNDRV_CARDS; dev++) {
2104 if (!enable[dev] || port[dev] != SNDRV_AUTO_PORT)
2105 continue;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002106 if (is_isapnp_selected(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 port[dev] = xport;
2109 res = snd_audiodrive_probe(dev, NULL, NULL);
2110 if (res < 0)
2111 port[dev] = SNDRV_AUTO_PORT;
2112 return res;
2113 }
2114 return -ENODEV;
2115}
2116
2117
2118#ifdef CONFIG_PNP
2119static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *card,
2120 const struct pnp_card_device_id *id)
2121{
2122 static int dev;
2123 int res;
2124
2125 for ( ; dev < SNDRV_CARDS; dev++) {
2126 if (!enable[dev] || !isapnp[dev])
2127 continue;
2128 res = snd_audiodrive_probe(dev, card, id);
2129 if (res < 0)
2130 return res;
2131 dev++;
2132 return 0;
2133 }
2134
2135 return -ENODEV;
2136}
2137
2138static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2139{
Takashi Iwai8047c912005-11-17 14:41:22 +01002140 struct snd_card *card = (struct snd_card *) pnp_get_card_drvdata(pcard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 snd_card_disconnect(card);
2143 snd_card_free_in_thread(card);
2144}
2145
2146static struct pnp_card_driver es18xx_pnpc_driver = {
2147 .flags = PNP_DRIVER_RES_DISABLE,
2148 .name = "es18xx",
2149 .id_table = snd_audiodrive_pnpids,
2150 .probe = snd_audiodrive_pnp_detect,
2151 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2152};
2153#endif /* CONFIG_PNP */
2154
2155static int __init alsa_card_es18xx_init(void)
2156{
2157 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280, -1};
2158 int dev, cards = 0, i;
2159
2160 /* legacy non-auto cards at first */
2161 for (dev = 0; dev < SNDRV_CARDS; dev++) {
2162 if (!enable[dev] || port[dev] == SNDRV_AUTO_PORT)
2163 continue;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002164 if (is_isapnp_selected(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (snd_audiodrive_probe(dev, NULL, NULL) >= 0)
2167 cards++;
2168 }
2169 /* legacy auto configured cards */
2170 i = snd_legacy_auto_probe(possible_ports, snd_audiodrive_probe_legacy_port);
2171 if (i > 0)
2172 cards += i;
2173
2174#ifdef CONFIG_PNP
2175 /* ISA PnP cards at last */
2176 i = pnp_register_card_driver(&es18xx_pnpc_driver);
2177 if (i > 0)
2178 cards += i;
2179
2180#endif
2181 if(!cards) {
2182#ifdef CONFIG_PNP
2183 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2184#endif
2185#ifdef MODULE
2186 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2187#endif
2188 return -ENODEV;
2189 }
2190 return 0;
2191}
2192
2193static void __exit alsa_card_es18xx_exit(void)
2194{
2195 int idx;
2196
2197#ifdef CONFIG_PNP
2198 /* PnP cards first */
2199 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2200#endif
2201 for(idx = 0; idx < SNDRV_CARDS; idx++)
2202 snd_card_free(snd_audiodrive_legacy[idx]);
2203}
2204
2205module_init(alsa_card_es18xx_init)
2206module_exit(alsa_card_es18xx_exit)