blob: bb709264216f9139448bf0e683ed1796c598d937 [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 *
Mark Salazar95b71292006-01-16 11:35:40 +010052 * - Zoom Video is implemented by sharing the FM DAC, thus the user can
53 * have either FM playback or Video playback but not both simultaneously.
54 * The Video Playback Switch mixer control toggles this choice.
55 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * BUGS:
57 *
58 * - There is a major trouble I noted:
59 *
60 * using both channel for playback stereo 16 bit samples at 44100 Hz
61 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
62 *
63 * The same happens using Audio1 for captureing.
64 *
65 * The Windows driver does not suffer of this (although it use Audio1
66 * only for captureing). I'm unable to discover why.
67 *
68 */
69
Mark Salazar95b71292006-01-16 11:35:40 +010070/*
71 * ES1879 NOTES:
72 * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
73 * seems to be effected (speaker_test plays a lower frequency). Can't find
74 * anything in the datasheet to account for this, so a Video Playback Switch
75 * control has been included to allow ZV to be enabled only when necessary.
76 * Then again on at least one test system the 0x71 bit 6 enable bit is not
77 * needed for ZV, so maybe the datasheet is entirely wrong here.
78 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010082#include <linux/err.h>
83#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/slab.h>
85#include <linux/pnp.h>
86#include <linux/isapnp.h>
87#include <linux/moduleparam.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010088#include <asm/io.h>
89#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <sound/core.h>
91#include <sound/control.h>
92#include <sound/pcm.h>
93#include <sound/pcm_params.h>
94#include <sound/mpu401.h>
95#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define SNDRV_LEGACY_FIND_FREE_IRQ
97#define SNDRV_LEGACY_FIND_FREE_DMA
98#include <sound/initval.h>
99
100#define PFX "es18xx: "
101
Takashi Iwai8047c912005-11-17 14:41:22 +0100102struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long port; /* port of ESS chip */
104 unsigned long mpu_port; /* MPU-401 port of ESS chip */
105 unsigned long fm_port; /* FM port */
106 unsigned long ctrl_port; /* Control port of ESS chip */
107 struct resource *res_port;
108 struct resource *res_mpu_port;
109 struct resource *res_ctrl_port;
110 int irq; /* IRQ number of ESS chip */
111 int dma1; /* DMA1 */
112 int dma2; /* DMA2 */
113 unsigned short version; /* version of ESS chip */
114 int caps; /* Chip capabilities */
115 unsigned short audio2_vol; /* volume level of audio2 */
116
117 unsigned short active; /* active channel mask */
118 unsigned int dma1_size;
119 unsigned int dma2_size;
120 unsigned int dma1_shift;
121 unsigned int dma2_shift;
122
Takashi Iwai8047c912005-11-17 14:41:22 +0100123 struct snd_card *card;
124 struct snd_pcm *pcm;
125 struct snd_pcm_substream *playback_a_substream;
126 struct snd_pcm_substream *capture_a_substream;
127 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Takashi Iwai8047c912005-11-17 14:41:22 +0100129 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai8047c912005-11-17 14:41:22 +0100131 struct snd_kcontrol *hw_volume;
132 struct snd_kcontrol *hw_switch;
133 struct snd_kcontrol *master_volume;
134 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 spinlock_t reg_lock;
137 spinlock_t mixer_lock;
138 spinlock_t ctrl_lock;
139#ifdef CONFIG_PM
140 unsigned char pm_reg;
141#endif
142};
143
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100144struct snd_audiodrive {
145 struct snd_es18xx *chip;
146#ifdef CONFIG_PNP
147 struct pnp_dev *dev;
148 struct pnp_dev *devc;
149#endif
150};
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define AUDIO1_IRQ 0x01
153#define AUDIO2_IRQ 0x02
154#define HWV_IRQ 0x04
155#define MPU_IRQ 0x08
156
157#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
158#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
159#define ES18XX_RECMIX 0x0004 /* Has record mixer */
160#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
161#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
162#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
163#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Mark Salazar14086772006-01-16 11:33:52 +0100164#define ES18XX_HWV 0x0080 /* Has seperate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
166#define ES18XX_I2S 0x0200 /* I2S mixer control */
167#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
168#define ES18XX_CONTROL 0x0800 /* Has control ports */
169
170/* Power Management */
171#define ES18XX_PM 0x07
172#define ES18XX_PM_GPO0 0x01
173#define ES18XX_PM_GPO1 0x02
174#define ES18XX_PM_PDR 0x04
175#define ES18XX_PM_ANA 0x08
176#define ES18XX_PM_FM 0x020
177#define ES18XX_PM_SUS 0x080
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Lowlevel */
180
181#define DAC1 0x01
182#define ADC1 0x02
183#define DAC2 0x04
184#define MILLISECOND 10000
185
Takashi Iwai8047c912005-11-17 14:41:22 +0100186static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
189
190 for(i = MILLISECOND; i; i--)
191 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
192 outb(val, chip->port + 0x0C);
193 return 0;
194 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200195 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197}
198
Takashi Iwai8047c912005-11-17 14:41:22 +0100199static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int i;
202
203 for(i = MILLISECOND/10; i; i--)
204 if (inb(chip->port + 0x0C) & 0x40)
205 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200206 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
207 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENODEV;
209}
210
211#undef REG_DEBUG
212
Takashi Iwai8047c912005-11-17 14:41:22 +0100213static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned char reg, unsigned char data)
215{
216 unsigned long flags;
217 int ret;
218
219 spin_lock_irqsave(&chip->reg_lock, flags);
220 ret = snd_es18xx_dsp_command(chip, reg);
221 if (ret < 0)
222 goto end;
223 ret = snd_es18xx_dsp_command(chip, data);
224 end:
225 spin_unlock_irqrestore(&chip->reg_lock, flags);
226#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200227 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#endif
229 return ret;
230}
231
Takashi Iwai8047c912005-11-17 14:41:22 +0100232static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 unsigned long flags;
235 int ret, data;
236 spin_lock_irqsave(&chip->reg_lock, flags);
237 ret = snd_es18xx_dsp_command(chip, 0xC0);
238 if (ret < 0)
239 goto end;
240 ret = snd_es18xx_dsp_command(chip, reg);
241 if (ret < 0)
242 goto end;
243 data = snd_es18xx_dsp_get_byte(chip);
244 ret = data;
245#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200246 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
248 end:
249 spin_unlock_irqrestore(&chip->reg_lock, flags);
250 return ret;
251}
252
253/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100254static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned char mask, unsigned char val)
256{
257 int ret;
258 unsigned char old, new, oval;
259 unsigned long flags;
260 spin_lock_irqsave(&chip->reg_lock, flags);
261 ret = snd_es18xx_dsp_command(chip, 0xC0);
262 if (ret < 0)
263 goto end;
264 ret = snd_es18xx_dsp_command(chip, reg);
265 if (ret < 0)
266 goto end;
267 ret = snd_es18xx_dsp_get_byte(chip);
268 if (ret < 0) {
269 goto end;
270 }
271 old = ret;
272 oval = old & mask;
273 if (val != oval) {
274 ret = snd_es18xx_dsp_command(chip, reg);
275 if (ret < 0)
276 goto end;
277 new = (old & ~mask) | (val & mask);
278 ret = snd_es18xx_dsp_command(chip, new);
279 if (ret < 0)
280 goto end;
281#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200282 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
283 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285 }
286 ret = oval;
287 end:
288 spin_unlock_irqrestore(&chip->reg_lock, flags);
289 return ret;
290}
291
Takashi Iwai8047c912005-11-17 14:41:22 +0100292static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned char reg, unsigned char data)
294{
295 unsigned long flags;
296 spin_lock_irqsave(&chip->mixer_lock, flags);
297 outb(reg, chip->port + 0x04);
298 outb(data, chip->port + 0x05);
299 spin_unlock_irqrestore(&chip->mixer_lock, flags);
300#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200301 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303}
304
Takashi Iwai8047c912005-11-17 14:41:22 +0100305static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned long flags;
308 int data;
309 spin_lock_irqsave(&chip->mixer_lock, flags);
310 outb(reg, chip->port + 0x04);
311 data = inb(chip->port + 0x05);
312 spin_unlock_irqrestore(&chip->mixer_lock, flags);
313#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200314 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
316 return data;
317}
318
319/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100320static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned char mask, unsigned char val)
322{
323 unsigned char old, new, oval;
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 oval = old & mask;
329 if (val != oval) {
330 new = (old & ~mask) | (val & mask);
331 outb(new, chip->port + 0x05);
332#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200333 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
334 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
336 }
337 spin_unlock_irqrestore(&chip->mixer_lock, flags);
338 return oval;
339}
340
Takashi Iwai8047c912005-11-17 14:41:22 +0100341static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned char mask)
343{
344 int old, expected, new;
345 unsigned long flags;
346 spin_lock_irqsave(&chip->mixer_lock, flags);
347 outb(reg, chip->port + 0x04);
348 old = inb(chip->port + 0x05);
349 expected = old ^ mask;
350 outb(expected, chip->port + 0x05);
351 new = inb(chip->port + 0x05);
352 spin_unlock_irqrestore(&chip->mixer_lock, flags);
353#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200354 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
355 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#endif
357 return expected == new;
358}
359
360
Takashi Iwai8047c912005-11-17 14:41:22 +0100361static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int i;
364 outb(0x03, chip->port + 0x06);
365 inb(chip->port + 0x06);
366 outb(0x00, chip->port + 0x06);
367 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
368 if (inb(chip->port + 0x0A) != 0xAA)
369 return -1;
370 return 0;
371}
372
Takashi Iwai8047c912005-11-17 14:41:22 +0100373static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 outb(0x02, chip->port + 0x06);
376 inb(chip->port + 0x06);
377 outb(0x00, chip->port + 0x06);
378 return 0;
379}
380
Takashi Iwai8047c912005-11-17 14:41:22 +0100381static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 {
383 .num = 793800,
384 .den_min = 1,
385 .den_max = 128,
386 .den_step = 1,
387 },
388 {
389 .num = 768000,
390 .den_min = 1,
391 .den_max = 128,
392 .den_step = 1,
393 }
394};
395
Takashi Iwai8047c912005-11-17 14:41:22 +0100396static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .nrats = 2,
398 .rats = new_clocks,
399};
400
Takashi Iwai8047c912005-11-17 14:41:22 +0100401static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 {
403 .num = 795444,
404 .den_min = 1,
405 .den_max = 128,
406 .den_step = 1,
407 },
408 {
409 .num = 397722,
410 .den_min = 1,
411 .den_max = 128,
412 .den_step = 1,
413 }
414};
415
Takashi Iwai8047c912005-11-17 14:41:22 +0100416static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .nrats = 2,
418 .rats = old_clocks,
419};
420
421
Takashi Iwai8047c912005-11-17 14:41:22 +0100422static void snd_es18xx_rate_set(struct snd_es18xx *chip,
423 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int mode)
425{
426 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100427 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (chip->caps & ES18XX_NEW_RATE) {
429 if (runtime->rate_num == new_clocks[0].num)
430 bits = 128 - runtime->rate_den;
431 else
432 bits = 256 - runtime->rate_den;
433 } else {
434 if (runtime->rate_num == old_clocks[0].num)
435 bits = 256 - runtime->rate_den;
436 else
437 bits = 128 - runtime->rate_den;
438 }
439
440 /* set filter register */
441 div0 = 256 - 7160000*20/(8*82*runtime->rate);
442
443 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
444 snd_es18xx_mixer_write(chip, 0x70, bits);
445 /*
446 * Comment from kernel oss driver:
447 * FKS: fascinating: 0x72 doesn't seem to work.
448 */
449 snd_es18xx_write(chip, 0xA2, div0);
450 snd_es18xx_mixer_write(chip, 0x72, div0);
451 } else {
452 snd_es18xx_write(chip, 0xA1, bits);
453 snd_es18xx_write(chip, 0xA2, div0);
454 }
455}
456
Takashi Iwai8047c912005-11-17 14:41:22 +0100457static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
458 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai8047c912005-11-17 14:41:22 +0100460 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int shift, err;
462
463 shift = 0;
464 if (params_channels(hw_params) == 2)
465 shift++;
466 if (snd_pcm_format_width(params_format(hw_params)) == 16)
467 shift++;
468
469 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
470 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
471 (chip->capture_a_substream) &&
472 params_channels(hw_params) != 1) {
473 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
474 return -EBUSY;
475 }
476 chip->dma2_shift = shift;
477 } else {
478 chip->dma1_shift = shift;
479 }
480 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
481 return err;
482 return 0;
483}
484
Takashi Iwai8047c912005-11-17 14:41:22 +0100485static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 return snd_pcm_lib_free_pages(substream);
488}
489
Takashi Iwai8047c912005-11-17 14:41:22 +0100490static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
491 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai8047c912005-11-17 14:41:22 +0100493 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
495 unsigned int count = snd_pcm_lib_period_bytes(substream);
496
497 chip->dma2_size = size;
498
499 snd_es18xx_rate_set(chip, substream, DAC2);
500
501 /* Transfer Count Reload */
502 count = 0x10000 - count;
503 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
504 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
505
506 /* Set format */
507 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
508 ((runtime->channels == 1) ? 0x00 : 0x02) |
509 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
510 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
511
512 /* Set DMA controller */
513 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
514
515 return 0;
516}
517
Takashi Iwai8047c912005-11-17 14:41:22 +0100518static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
519 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int cmd)
521{
522 switch (cmd) {
523 case SNDRV_PCM_TRIGGER_START:
524 case SNDRV_PCM_TRIGGER_RESUME:
525 if (chip->active & DAC2)
526 return 0;
527 chip->active |= DAC2;
528 /* Start DMA */
529 if (chip->dma2 >= 4)
530 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
531 else
532 snd_es18xx_mixer_write(chip, 0x78, 0x93);
533#ifdef AVOID_POPS
534 /* Avoid pops */
535 udelay(100000);
536 if (chip->caps & ES18XX_PCM2)
537 /* Restore Audio 2 volume */
538 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
539 else
540 /* Enable PCM output */
541 snd_es18xx_dsp_command(chip, 0xD1);
542#endif
543 break;
544 case SNDRV_PCM_TRIGGER_STOP:
545 case SNDRV_PCM_TRIGGER_SUSPEND:
546 if (!(chip->active & DAC2))
547 return 0;
548 chip->active &= ~DAC2;
549 /* Stop DMA */
550 snd_es18xx_mixer_write(chip, 0x78, 0x00);
551#ifdef AVOID_POPS
552 udelay(25000);
553 if (chip->caps & ES18XX_PCM2)
554 /* Set Audio 2 volume to 0 */
555 snd_es18xx_mixer_write(chip, 0x7C, 0);
556 else
557 /* Disable PCM output */
558 snd_es18xx_dsp_command(chip, 0xD3);
559#endif
560 break;
561 default:
562 return -EINVAL;
563 }
564
565 return 0;
566}
567
Takashi Iwai8047c912005-11-17 14:41:22 +0100568static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
569 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai8047c912005-11-17 14:41:22 +0100571 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int shift, err;
573
574 shift = 0;
575 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
576 chip->playback_a_substream &&
577 params_channels(hw_params) != 1) {
578 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
579 return -EBUSY;
580 }
581 if (params_channels(hw_params) == 2)
582 shift++;
583 if (snd_pcm_format_width(params_format(hw_params)) == 16)
584 shift++;
585 chip->dma1_shift = shift;
586 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
587 return err;
588 return 0;
589}
590
Takashi Iwai8047c912005-11-17 14:41:22 +0100591static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai8047c912005-11-17 14:41:22 +0100593 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
594 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
596 unsigned int count = snd_pcm_lib_period_bytes(substream);
597
598 chip->dma1_size = size;
599
600 snd_es18xx_reset_fifo(chip);
601
602 /* Set stereo/mono */
603 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
604
605 snd_es18xx_rate_set(chip, substream, ADC1);
606
607 /* Transfer Count Reload */
608 count = 0x10000 - count;
609 snd_es18xx_write(chip, 0xA4, count & 0xff);
610 snd_es18xx_write(chip, 0xA5, count >> 8);
611
612#ifdef AVOID_POPS
613 udelay(100000);
614#endif
615
616 /* Set format */
617 snd_es18xx_write(chip, 0xB7,
618 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
619 snd_es18xx_write(chip, 0xB7, 0x90 |
620 ((runtime->channels == 1) ? 0x40 : 0x08) |
621 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
622 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
623
624 /* Set DMA controler */
625 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
626
627 return 0;
628}
629
Takashi Iwai8047c912005-11-17 14:41:22 +0100630static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int cmd)
632{
Takashi Iwai8047c912005-11-17 14:41:22 +0100633 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 switch (cmd) {
636 case SNDRV_PCM_TRIGGER_START:
637 case SNDRV_PCM_TRIGGER_RESUME:
638 if (chip->active & ADC1)
639 return 0;
640 chip->active |= ADC1;
641 /* Start DMA */
642 snd_es18xx_write(chip, 0xB8, 0x0f);
643 break;
644 case SNDRV_PCM_TRIGGER_STOP:
645 case SNDRV_PCM_TRIGGER_SUSPEND:
646 if (!(chip->active & ADC1))
647 return 0;
648 chip->active &= ~ADC1;
649 /* Stop DMA */
650 snd_es18xx_write(chip, 0xB8, 0x00);
651 break;
652 default:
653 return -EINVAL;
654 }
655
656 return 0;
657}
658
Takashi Iwai8047c912005-11-17 14:41:22 +0100659static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
660 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai8047c912005-11-17 14:41:22 +0100662 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
664 unsigned int count = snd_pcm_lib_period_bytes(substream);
665
666 chip->dma1_size = size;
667
668 snd_es18xx_reset_fifo(chip);
669
670 /* Set stereo/mono */
671 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
672
673 snd_es18xx_rate_set(chip, substream, DAC1);
674
675 /* Transfer Count Reload */
676 count = 0x10000 - count;
677 snd_es18xx_write(chip, 0xA4, count & 0xff);
678 snd_es18xx_write(chip, 0xA5, count >> 8);
679
680 /* Set format */
681 snd_es18xx_write(chip, 0xB6,
682 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
683 snd_es18xx_write(chip, 0xB7,
684 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
685 snd_es18xx_write(chip, 0xB7, 0x90 |
686 (runtime->channels == 1 ? 0x40 : 0x08) |
687 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
688 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
689
690 /* Set DMA controler */
691 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
692
693 return 0;
694}
695
Takashi Iwai8047c912005-11-17 14:41:22 +0100696static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
697 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int cmd)
699{
700 switch (cmd) {
701 case SNDRV_PCM_TRIGGER_START:
702 case SNDRV_PCM_TRIGGER_RESUME:
703 if (chip->active & DAC1)
704 return 0;
705 chip->active |= DAC1;
706 /* Start DMA */
707 snd_es18xx_write(chip, 0xB8, 0x05);
708#ifdef AVOID_POPS
709 /* Avoid pops */
710 udelay(100000);
711 /* Enable Audio 1 */
712 snd_es18xx_dsp_command(chip, 0xD1);
713#endif
714 break;
715 case SNDRV_PCM_TRIGGER_STOP:
716 case SNDRV_PCM_TRIGGER_SUSPEND:
717 if (!(chip->active & DAC1))
718 return 0;
719 chip->active &= ~DAC1;
720 /* Stop DMA */
721 snd_es18xx_write(chip, 0xB8, 0x00);
722#ifdef AVOID_POPS
723 /* Avoid pops */
724 udelay(25000);
725 /* Disable Audio 1 */
726 snd_es18xx_dsp_command(chip, 0xD3);
727#endif
728 break;
729 default:
730 return -EINVAL;
731 }
732
733 return 0;
734}
735
Takashi Iwai8047c912005-11-17 14:41:22 +0100736static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Takashi Iwai8047c912005-11-17 14:41:22 +0100738 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
740 return snd_es18xx_playback1_prepare(chip, substream);
741 else
742 return snd_es18xx_playback2_prepare(chip, substream);
743}
744
Takashi Iwai8047c912005-11-17 14:41:22 +0100745static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int cmd)
747{
Takashi Iwai8047c912005-11-17 14:41:22 +0100748 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
750 return snd_es18xx_playback1_trigger(chip, substream, cmd);
751 else
752 return snd_es18xx_playback2_trigger(chip, substream, cmd);
753}
754
755static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
756{
Takashi Iwai8047c912005-11-17 14:41:22 +0100757 struct snd_es18xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unsigned char status;
759
760 if (chip->caps & ES18XX_CONTROL) {
761 /* Read Interrupt status */
762 status = inb(chip->ctrl_port + 6);
763 } else {
764 /* Read Interrupt status */
765 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
766 }
767#if 0
768 else {
769 status = 0;
770 if (inb(chip->port + 0x0C) & 0x01)
771 status |= AUDIO1_IRQ;
772 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
773 status |= AUDIO2_IRQ;
774 if ((chip->caps & ES18XX_HWV) &&
775 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
776 status |= HWV_IRQ;
777 }
778#endif
779
780 /* Audio 1 & Audio 2 */
781 if (status & AUDIO2_IRQ) {
782 if (chip->active & DAC2)
783 snd_pcm_period_elapsed(chip->playback_a_substream);
784 /* ack interrupt */
785 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
786 }
787 if (status & AUDIO1_IRQ) {
788 /* ok.. capture is active */
789 if (chip->active & ADC1)
790 snd_pcm_period_elapsed(chip->capture_a_substream);
791 /* ok.. playback2 is active */
792 else if (chip->active & DAC1)
793 snd_pcm_period_elapsed(chip->playback_b_substream);
794 /* ack interrupt */
795 inb(chip->port + 0x0E);
796 }
797
798 /* MPU */
799 if ((status & MPU_IRQ) && chip->rmidi)
800 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
801
802 /* Hardware volume */
803 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100804 int split = 0;
805 if (chip->caps & ES18XX_HWV) {
806 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
807 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
808 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!split) {
811 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
812 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
813 }
814 /* ack interrupt */
815 snd_es18xx_mixer_write(chip, 0x66, 0x00);
816 }
817 return IRQ_HANDLED;
818}
819
Takashi Iwai8047c912005-11-17 14:41:22 +0100820static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai8047c912005-11-17 14:41:22 +0100822 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int pos;
824
825 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
826 if (!(chip->active & DAC2))
827 return 0;
828 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
829 return pos >> chip->dma2_shift;
830 } else {
831 if (!(chip->active & DAC1))
832 return 0;
833 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
834 return pos >> chip->dma1_shift;
835 }
836}
837
Takashi Iwai8047c912005-11-17 14:41:22 +0100838static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Takashi Iwai8047c912005-11-17 14:41:22 +0100840 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 int pos;
842
843 if (!(chip->active & ADC1))
844 return 0;
845 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
846 return pos >> chip->dma1_shift;
847}
848
Takashi Iwai8047c912005-11-17 14:41:22 +0100849static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
852 SNDRV_PCM_INFO_RESUME |
853 SNDRV_PCM_INFO_MMAP_VALID),
854 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
855 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
856 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
857 .rate_min = 4000,
858 .rate_max = 48000,
859 .channels_min = 1,
860 .channels_max = 2,
861 .buffer_bytes_max = 65536,
862 .period_bytes_min = 64,
863 .period_bytes_max = 65536,
864 .periods_min = 1,
865 .periods_max = 1024,
866 .fifo_size = 0,
867};
868
Takashi Iwai8047c912005-11-17 14:41:22 +0100869static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
872 SNDRV_PCM_INFO_RESUME |
873 SNDRV_PCM_INFO_MMAP_VALID),
874 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
875 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
876 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
877 .rate_min = 4000,
878 .rate_max = 48000,
879 .channels_min = 1,
880 .channels_max = 2,
881 .buffer_bytes_max = 65536,
882 .period_bytes_min = 64,
883 .period_bytes_max = 65536,
884 .periods_min = 1,
885 .periods_max = 1024,
886 .fifo_size = 0,
887};
888
Takashi Iwai8047c912005-11-17 14:41:22 +0100889static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Takashi Iwai8047c912005-11-17 14:41:22 +0100891 struct snd_pcm_runtime *runtime = substream->runtime;
892 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
895 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
896 chip->capture_a_substream &&
897 chip->capture_a_substream->runtime->channels != 1)
898 return -EAGAIN;
899 chip->playback_a_substream = substream;
900 } else if (substream->number <= 1) {
901 if (chip->capture_a_substream)
902 return -EAGAIN;
903 chip->playback_b_substream = substream;
904 } else {
905 snd_BUG();
906 return -EINVAL;
907 }
908 substream->runtime->hw = snd_es18xx_playback;
909 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
910 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
911 return 0;
912}
913
Takashi Iwai8047c912005-11-17 14:41:22 +0100914static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Takashi Iwai8047c912005-11-17 14:41:22 +0100916 struct snd_pcm_runtime *runtime = substream->runtime;
917 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (chip->playback_b_substream)
920 return -EAGAIN;
921 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
922 chip->playback_a_substream &&
923 chip->playback_a_substream->runtime->channels != 1)
924 return -EAGAIN;
925 chip->capture_a_substream = substream;
926 substream->runtime->hw = snd_es18xx_capture;
927 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
928 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
929 return 0;
930}
931
Takashi Iwai8047c912005-11-17 14:41:22 +0100932static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Takashi Iwai8047c912005-11-17 14:41:22 +0100934 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
937 chip->playback_a_substream = NULL;
938 else
939 chip->playback_b_substream = NULL;
940
941 snd_pcm_lib_free_pages(substream);
942 return 0;
943}
944
Takashi Iwai8047c912005-11-17 14:41:22 +0100945static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Takashi Iwai8047c912005-11-17 14:41:22 +0100947 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 chip->capture_a_substream = NULL;
950 snd_pcm_lib_free_pages(substream);
951 return 0;
952}
953
954/*
955 * MIXER part
956 */
957
Mark Salazarf4df2212006-01-16 11:31:14 +0100958/* Record source mux routines:
959 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
960 * bit table for the 4/5 source mux:
961 * reg 1C:
962 * b2 b1 b0 muxSource
963 * x 0 x microphone
964 * 0 1 x CD
965 * 1 1 0 line
966 * 1 1 1 mixer
967 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
968 * either the play mixer or the capture mixer.
969 *
970 * "map4Source" translates from source number to reg bit pattern
971 * "invMap4Source" translates from reg bit pattern to source number
972 */
973
Takashi Iwai8047c912005-11-17 14:41:22 +0100974static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Mark Salazarf4df2212006-01-16 11:31:14 +0100976 static char *texts4Source[4] = {
977 "Mic", "CD", "Line", "Master"
978 };
979 static char *texts5Source[5] = {
980 "Mic", "CD", "Line", "Master", "Mix"
981 };
982 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 "Mic", "Mic Master", "CD", "AOUT",
984 "Mic1", "Mix", "Line", "Master"
985 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100986 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
989 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100990 switch (chip->version) {
991 case 0x1868:
992 case 0x1878:
993 uinfo->value.enumerated.items = 4;
994 if (uinfo->value.enumerated.item > 3)
995 uinfo->value.enumerated.item = 3;
996 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
997 break;
998 case 0x1887:
999 case 0x1888:
1000 uinfo->value.enumerated.items = 5;
1001 if (uinfo->value.enumerated.item > 4)
1002 uinfo->value.enumerated.item = 4;
1003 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
1004 break;
1005 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
1006 case 0x1879:
1007 uinfo->value.enumerated.items = 8;
1008 if (uinfo->value.enumerated.item > 7)
1009 uinfo->value.enumerated.item = 7;
1010 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1011 break;
1012 default:
1013 return -EINVAL;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
1017
Takashi Iwai8047c912005-11-17 14:41:22 +01001018static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Mark Salazarf4df2212006-01-16 11:31:14 +01001020 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001021 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001022 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1023 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1024 muxSource = invMap4Source[muxSource];
1025 if (muxSource==3 &&
1026 (chip->version == 0x1887 || chip->version == 0x1888) &&
1027 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1028 )
1029 muxSource = 4;
1030 }
1031 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033}
1034
Takashi Iwai8047c912005-11-17 14:41:22 +01001035static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Mark Salazarf4df2212006-01-16 11:31:14 +01001037 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001038 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001040 unsigned char retVal = 0;
1041
1042 switch (chip->version) {
1043 /* 5 source chips */
1044 case 0x1887:
1045 case 0x1888:
1046 if (val > 4)
1047 return -EINVAL;
1048 if (val == 4) {
1049 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1050 val = 3;
1051 } else
1052 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1053 /* 4 source chips */
1054 case 0x1868:
1055 case 0x1878:
1056 if (val > 3)
1057 return -EINVAL;
1058 val = map4Source[val];
1059 break;
1060 /* 8 source chips */
1061 case 0x1869:
1062 case 0x1879:
1063 if (val > 7)
1064 return -EINVAL;
1065 break;
1066 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001068 }
1069 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Takashi Iwai8047c912005-11-17 14:41:22 +01001072static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1075 uinfo->count = 1;
1076 uinfo->value.integer.min = 0;
1077 uinfo->value.integer.max = 1;
1078 return 0;
1079}
1080
Takashi Iwai8047c912005-11-17 14:41:22 +01001081static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai8047c912005-11-17 14:41:22 +01001083 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1085 ucontrol->value.integer.value[0] = !!(val & 8);
1086 return 0;
1087}
1088
Takashi Iwai8047c912005-11-17 14:41:22 +01001089static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Takashi Iwai8047c912005-11-17 14:41:22 +01001091 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 unsigned char oval, nval;
1093 int change;
1094 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1095 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1096 change = nval != oval;
1097 if (change) {
1098 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1099 snd_es18xx_mixer_write(chip, 0x50, nval);
1100 }
1101 return change;
1102}
1103
Takashi Iwai8047c912005-11-17 14:41:22 +01001104static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1107 uinfo->count = 2;
1108 uinfo->value.integer.min = 0;
1109 uinfo->value.integer.max = 63;
1110 return 0;
1111}
1112
Takashi Iwai8047c912005-11-17 14:41:22 +01001113static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai8047c912005-11-17 14:41:22 +01001115 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1117 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1118 return 0;
1119}
1120
Takashi Iwai8047c912005-11-17 14:41:22 +01001121static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1124 uinfo->count = 2;
1125 uinfo->value.integer.min = 0;
1126 uinfo->value.integer.max = 1;
1127 return 0;
1128}
1129
Takashi Iwai8047c912005-11-17 14:41:22 +01001130static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Takashi Iwai8047c912005-11-17 14:41:22 +01001132 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1134 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1135 return 0;
1136}
1137
Takashi Iwai8047c912005-11-17 14:41:22 +01001138static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai8047c912005-11-17 14:41:22 +01001140 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 chip->master_volume = NULL;
1142 chip->master_switch = NULL;
1143 chip->hw_volume = NULL;
1144 chip->hw_switch = NULL;
1145}
1146
Takashi Iwai8047c912005-11-17 14:41:22 +01001147static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned char mask, unsigned char val)
1149{
1150 if (reg < 0xa0)
1151 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1152 else
1153 return snd_es18xx_bits(chip, reg, mask, val);
1154}
1155
Takashi Iwai8047c912005-11-17 14:41:22 +01001156static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 if (reg < 0xa0)
1159 return snd_es18xx_mixer_read(chip, reg);
1160 else
1161 return snd_es18xx_read(chip, reg);
1162}
1163
1164#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1165{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1166 .info = snd_es18xx_info_single, \
1167 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1168 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1169
Takashi Iwai8047c912005-11-17 14:41:22 +01001170static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int mask = (kcontrol->private_value >> 16) & 0xff;
1173
1174 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1175 uinfo->count = 1;
1176 uinfo->value.integer.min = 0;
1177 uinfo->value.integer.max = mask;
1178 return 0;
1179}
1180
Takashi Iwai8047c912005-11-17 14:41:22 +01001181static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Takashi Iwai8047c912005-11-17 14:41:22 +01001183 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 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;
1188 int val;
1189
1190 val = snd_es18xx_reg_read(chip, reg);
1191 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1192 if (invert)
1193 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1194 return 0;
1195}
1196
Takashi Iwai8047c912005-11-17 14:41:22 +01001197static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Takashi Iwai8047c912005-11-17 14:41:22 +01001199 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int reg = kcontrol->private_value & 0xff;
1201 int shift = (kcontrol->private_value >> 8) & 0xff;
1202 int mask = (kcontrol->private_value >> 16) & 0xff;
1203 int invert = (kcontrol->private_value >> 24) & 0xff;
1204 unsigned char val;
1205
1206 val = (ucontrol->value.integer.value[0] & mask);
1207 if (invert)
1208 val = mask - val;
1209 mask <<= shift;
1210 val <<= shift;
1211 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1212}
1213
1214#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1215{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1216 .info = snd_es18xx_info_double, \
1217 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1218 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1219
Takashi Iwai8047c912005-11-17 14:41:22 +01001220static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 int mask = (kcontrol->private_value >> 24) & 0xff;
1223
1224 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1225 uinfo->count = 2;
1226 uinfo->value.integer.min = 0;
1227 uinfo->value.integer.max = mask;
1228 return 0;
1229}
1230
Takashi Iwai8047c912005-11-17 14:41:22 +01001231static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwai8047c912005-11-17 14:41:22 +01001233 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int left_reg = kcontrol->private_value & 0xff;
1235 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1236 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1237 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1238 int mask = (kcontrol->private_value >> 24) & 0xff;
1239 int invert = (kcontrol->private_value >> 22) & 1;
1240 unsigned char left, right;
1241
1242 left = snd_es18xx_reg_read(chip, left_reg);
1243 if (left_reg != right_reg)
1244 right = snd_es18xx_reg_read(chip, right_reg);
1245 else
1246 right = left;
1247 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1248 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1249 if (invert) {
1250 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1251 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1252 }
1253 return 0;
1254}
1255
Takashi Iwai8047c912005-11-17 14:41:22 +01001256static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwai8047c912005-11-17 14:41:22 +01001258 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int left_reg = kcontrol->private_value & 0xff;
1260 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1261 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1262 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1263 int mask = (kcontrol->private_value >> 24) & 0xff;
1264 int invert = (kcontrol->private_value >> 22) & 1;
1265 int change;
1266 unsigned char val1, val2, mask1, mask2;
1267
1268 val1 = ucontrol->value.integer.value[0] & mask;
1269 val2 = ucontrol->value.integer.value[1] & mask;
1270 if (invert) {
1271 val1 = mask - val1;
1272 val2 = mask - val2;
1273 }
1274 val1 <<= shift_left;
1275 val2 <<= shift_right;
1276 mask1 = mask << shift_left;
1277 mask2 = mask << shift_right;
1278 if (left_reg != right_reg) {
1279 change = 0;
1280 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1281 change = 1;
1282 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1283 change = 1;
1284 } else {
1285 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1286 val1 | val2) != (val1 | val2));
1287 }
1288 return change;
1289}
1290
Mark Salazarc1f6d412006-01-16 11:29:09 +01001291/* Mixer controls
1292 * These arrays contain setup data for mixer controls.
1293 *
1294 * The controls that are universal to all chipsets are fully initialized
1295 * here.
1296 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001297static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1299ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1300ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1301ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1302ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1304ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1306ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1309 .name = "Capture Source",
1310 .info = snd_es18xx_info_mux,
1311 .get = snd_es18xx_get_mux,
1312 .put = snd_es18xx_put_mux,
1313}
1314};
1315
Takashi Iwai8047c912005-11-17 14:41:22 +01001316static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1318ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1319ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1320ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1322ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1323};
1324
Mark Salazarc1f6d412006-01-16 11:29:09 +01001325/*
1326 * The chipset specific mixer controls
1327 */
1328static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1329 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1330
1331static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1332ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
Mark Salazar95b71292006-01-16 11:35:40 +01001333ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001334ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1335ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1336};
1337
Mark Salazar95b71292006-01-16 11:35:40 +01001338static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1339 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1340
1341static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1342ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1343ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1344ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1345};
1346
Takashi Iwai8047c912005-11-17 14:41:22 +01001347static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1349};
1350
Takashi Iwai8047c912005-11-17 14:41:22 +01001351static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1353ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1354};
1355
Takashi Iwai8047c912005-11-17 14:41:22 +01001356static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1358{
1359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1360 .name = "3D Control - Switch",
1361 .info = snd_es18xx_info_spatializer_enable,
1362 .get = snd_es18xx_get_spatializer_enable,
1363 .put = snd_es18xx_put_spatializer_enable,
1364}
1365};
1366
Takashi Iwai8047c912005-11-17 14:41:22 +01001367static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1369
Takashi Iwai8047c912005-11-17 14:41:22 +01001370static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1372
Takashi Iwai8047c912005-11-17 14:41:22 +01001373static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1376 .name = "Hardware Master Playback Volume",
1377 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1378 .info = snd_es18xx_info_hw_volume,
1379 .get = snd_es18xx_get_hw_volume,
1380},
1381{
1382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1383 .name = "Hardware Master Playback Switch",
1384 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1385 .info = snd_es18xx_info_hw_switch,
1386 .get = snd_es18xx_get_hw_switch,
1387},
1388ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1389};
1390
Takashi Iwai8047c912005-11-17 14:41:22 +01001391static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 int data;
1394 unsigned long flags;
1395 spin_lock_irqsave(&chip->ctrl_lock, flags);
1396 outb(reg, chip->ctrl_port);
1397 data = inb(chip->ctrl_port + 1);
1398 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1399 return data;
1400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Takashi Iwai8047c912005-11-17 14:41:22 +01001402static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned char reg, unsigned char data)
1404{
1405 /* No need for spinlocks, this function is used only in
1406 otherwise protected init code */
1407 outb(reg, chip->ctrl_port);
1408 outb(data, chip->ctrl_port + 1);
1409#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001410 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411#endif
1412}
1413
Takashi Iwai8047c912005-11-17 14:41:22 +01001414static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 int mask = 0;
1417
1418 /* enable extended mode */
1419 snd_es18xx_dsp_command(chip, 0xC6);
1420 /* Reset mixer registers */
1421 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1422
1423 /* Audio 1 DMA demand mode (4 bytes/request) */
1424 snd_es18xx_write(chip, 0xB9, 2);
1425 if (chip->caps & ES18XX_CONTROL) {
1426 /* Hardware volume IRQ */
1427 snd_es18xx_config_write(chip, 0x27, chip->irq);
1428 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1429 /* FM I/O */
1430 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1431 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1432 }
1433 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1434 /* MPU-401 I/O */
1435 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1436 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1437 /* MPU-401 IRQ */
1438 snd_es18xx_config_write(chip, 0x28, chip->irq);
1439 }
1440 /* Audio1 IRQ */
1441 snd_es18xx_config_write(chip, 0x70, chip->irq);
1442 /* Audio2 IRQ */
1443 snd_es18xx_config_write(chip, 0x72, chip->irq);
1444 /* Audio1 DMA */
1445 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1446 /* Audio2 DMA */
1447 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1448
1449 /* Enable Audio 1 IRQ */
1450 snd_es18xx_write(chip, 0xB1, 0x50);
1451 /* Enable Audio 2 IRQ */
1452 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1453 /* Enable Audio 1 DMA */
1454 snd_es18xx_write(chip, 0xB2, 0x50);
1455 /* Enable MPU and hardware volume interrupt */
1456 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1457 }
1458 else {
1459 int irqmask, dma1mask, dma2mask;
1460 switch (chip->irq) {
1461 case 2:
1462 case 9:
1463 irqmask = 0;
1464 break;
1465 case 5:
1466 irqmask = 1;
1467 break;
1468 case 7:
1469 irqmask = 2;
1470 break;
1471 case 10:
1472 irqmask = 3;
1473 break;
1474 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001475 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -ENODEV;
1477 }
1478 switch (chip->dma1) {
1479 case 0:
1480 dma1mask = 1;
1481 break;
1482 case 1:
1483 dma1mask = 2;
1484 break;
1485 case 3:
1486 dma1mask = 3;
1487 break;
1488 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001489 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -ENODEV;
1491 }
1492 switch (chip->dma2) {
1493 case 0:
1494 dma2mask = 0;
1495 break;
1496 case 1:
1497 dma2mask = 1;
1498 break;
1499 case 3:
1500 dma2mask = 2;
1501 break;
1502 case 5:
1503 dma2mask = 3;
1504 break;
1505 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001506 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -ENODEV;
1508 }
1509
1510 /* Enable and set Audio 1 IRQ */
1511 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1512 /* Enable and set Audio 1 DMA */
1513 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1514 /* Set Audio 2 DMA */
1515 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1516 /* Enable Audio 2 IRQ and DMA
1517 Set capture mixer input */
1518 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1519 /* Enable and set hardware volume interrupt */
1520 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1521 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1522 /* MPU401 share irq with audio
1523 Joystick enabled
1524 FM enabled */
1525 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1526 }
1527 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1528 }
1529 if (chip->caps & ES18XX_NEW_RATE) {
1530 /* Change behaviour of register A1
1531 4x oversampling
1532 2nd channel DAC asynchronous */
1533 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1534 }
1535 if (!(chip->caps & ES18XX_PCM2)) {
1536 /* Enable DMA FIFO */
1537 snd_es18xx_write(chip, 0xB7, 0x80);
1538 }
1539 if (chip->caps & ES18XX_SPATIALIZER) {
1540 /* Set spatializer parameters to recommended values */
1541 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1542 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1543 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1544 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1545 }
Mark Salazar95b71292006-01-16 11:35:40 +01001546 /* Flip the "enable I2S" bits for those chipsets that need it */
1547 switch (chip->version) {
1548 case 0x1879:
1549 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1550 //so a Switch control has been added to toggle this 0x71 bit on/off:
1551 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1552 /* Note: we fall through on purpose here. */
1553 case 0x1878:
1554 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1555 break;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Mute input source */
1558 if (chip->caps & ES18XX_MUTEREC)
1559 mask = 0x10;
1560 if (chip->caps & ES18XX_RECMIX)
1561 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1562 else {
1563 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1564 snd_es18xx_write(chip, 0xb4, 0x00);
1565 }
1566#ifndef AVOID_POPS
1567 /* Enable PCM output */
1568 snd_es18xx_dsp_command(chip, 0xD1);
1569#endif
1570
1571 return 0;
1572}
1573
Takashi Iwai8047c912005-11-17 14:41:22 +01001574static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 int hi,lo;
1577
1578 /* reset */
1579 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001580 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -ENODEV;
1582 }
1583
1584 snd_es18xx_dsp_command(chip, 0xe7);
1585 hi = snd_es18xx_dsp_get_byte(chip);
1586 if (hi < 0) {
1587 return hi;
1588 }
1589 lo = snd_es18xx_dsp_get_byte(chip);
1590 if ((lo & 0xf0) != 0x80) {
1591 return -ENODEV;
1592 }
1593 if (hi == 0x48) {
1594 chip->version = 0x488;
1595 return 0;
1596 }
1597 if (hi != 0x68) {
1598 return -ENODEV;
1599 }
1600 if ((lo & 0x0f) < 8) {
1601 chip->version = 0x688;
1602 return 0;
1603 }
1604
1605 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001606 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001608 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 lo = inb(chip->port + 0x05);
1610 if (hi != lo) {
1611 chip->version = hi << 8 | lo;
1612 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001613 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 chip->ctrl_port += inb(chip->port + 0x05);
1615
1616 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1617 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1618 return -EBUSY;
1619 }
1620
1621 return 0;
1622 }
1623
1624 /* If has Hardware volume */
1625 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1626 /* If has Audio2 */
1627 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1628 /* If has volume count */
1629 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1630 chip->version = 0x1887;
1631 } else {
1632 chip->version = 0x1888;
1633 }
1634 } else {
1635 chip->version = 0x1788;
1636 }
1637 }
1638 else
1639 chip->version = 0x1688;
1640 return 0;
1641}
1642
Takashi Iwai8047c912005-11-17 14:41:22 +01001643static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 if (snd_es18xx_identify(chip) < 0) {
1646 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1647 return -ENODEV;
1648 }
1649
1650 switch (chip->version) {
1651 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001652 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 case 0x1869:
1655 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1656 break;
1657 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001658 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 break;
1660 case 0x1879:
1661 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1662 break;
1663 case 0x1887:
Mark Salazar14086772006-01-16 11:33:52 +01001664 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001667 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001670 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 chip->port, chip->version);
1672 return -ENODEV;
1673 }
1674
1675 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1676
1677 if (chip->dma1 == chip->dma2)
1678 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1679
1680 return snd_es18xx_initialize(chip);
1681}
1682
Takashi Iwai8047c912005-11-17 14:41:22 +01001683static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 .open = snd_es18xx_playback_open,
1685 .close = snd_es18xx_playback_close,
1686 .ioctl = snd_pcm_lib_ioctl,
1687 .hw_params = snd_es18xx_playback_hw_params,
1688 .hw_free = snd_es18xx_pcm_hw_free,
1689 .prepare = snd_es18xx_playback_prepare,
1690 .trigger = snd_es18xx_playback_trigger,
1691 .pointer = snd_es18xx_playback_pointer,
1692};
1693
Takashi Iwai8047c912005-11-17 14:41:22 +01001694static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .open = snd_es18xx_capture_open,
1696 .close = snd_es18xx_capture_close,
1697 .ioctl = snd_pcm_lib_ioctl,
1698 .hw_params = snd_es18xx_capture_hw_params,
1699 .hw_free = snd_es18xx_pcm_hw_free,
1700 .prepare = snd_es18xx_capture_prepare,
1701 .trigger = snd_es18xx_capture_trigger,
1702 .pointer = snd_es18xx_capture_pointer,
1703};
1704
Takashi Iwai8047c912005-11-17 14:41:22 +01001705static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwai8047c912005-11-17 14:41:22 +01001707 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 char str[16];
1709 int err;
1710
1711 if (rpcm)
1712 *rpcm = NULL;
1713 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001714 if (chip->caps & ES18XX_PCM2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001716 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (err < 0)
1719 return err;
1720
1721 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1722 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1723
1724 /* global setup */
1725 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 pcm->info_flags = 0;
1727 if (chip->caps & ES18XX_DUPLEX_SAME)
1728 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1729 if (! (chip->caps & ES18XX_PCM2))
1730 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1731 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1732 chip->pcm = pcm;
1733
1734 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1735 snd_dma_isa_data(),
1736 64*1024,
1737 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1738
1739 if (rpcm)
1740 *rpcm = pcm;
1741 return 0;
1742}
1743
1744/* Power Management support functions */
1745#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001746static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001748 struct snd_audiodrive *acard = card->private_data;
1749 struct snd_es18xx *chip = acard->chip;
1750
1751 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 snd_pcm_suspend_all(chip->pcm);
1754
1755 /* power down */
1756 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1757 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1758 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1759 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1760
1761 return 0;
1762}
1763
Takashi Iwai8047c912005-11-17 14:41:22 +01001764static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001766 struct snd_audiodrive *acard = card->private_data;
1767 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1770 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1771
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001772 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775#endif /* CONFIG_PM */
1776
Takashi Iwai8047c912005-11-17 14:41:22 +01001777static int snd_es18xx_free(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001779 release_and_free_resource(chip->res_port);
1780 release_and_free_resource(chip->res_ctrl_port);
1781 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (chip->irq >= 0)
1783 free_irq(chip->irq, (void *) chip);
1784 if (chip->dma1 >= 0) {
1785 disable_dma(chip->dma1);
1786 free_dma(chip->dma1);
1787 }
1788 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1789 disable_dma(chip->dma2);
1790 free_dma(chip->dma2);
1791 }
1792 kfree(chip);
1793 return 0;
1794}
1795
Takashi Iwai8047c912005-11-17 14:41:22 +01001796static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Takashi Iwai8047c912005-11-17 14:41:22 +01001798 struct snd_es18xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return snd_es18xx_free(chip);
1800}
1801
Takashi Iwai8047c912005-11-17 14:41:22 +01001802static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 unsigned long port,
1804 unsigned long mpu_port,
1805 unsigned long fm_port,
1806 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001807 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwai8047c912005-11-17 14:41:22 +01001809 struct snd_es18xx *chip;
1810 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 .dev_free = snd_es18xx_dev_free,
1812 };
1813 int err;
1814
1815 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001816 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (chip == NULL)
1818 return -ENOMEM;
1819 spin_lock_init(&chip->reg_lock);
1820 spin_lock_init(&chip->mixer_lock);
1821 spin_lock_init(&chip->ctrl_lock);
1822 chip->card = card;
1823 chip->port = port;
1824 chip->mpu_port = mpu_port;
1825 chip->fm_port = fm_port;
1826 chip->irq = -1;
1827 chip->dma1 = -1;
1828 chip->dma2 = -1;
1829 chip->audio2_vol = 0x00;
1830 chip->active = 0;
1831
1832 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1833 snd_es18xx_free(chip);
1834 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1835 return -EBUSY;
1836 }
1837
1838 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1839 snd_es18xx_free(chip);
1840 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1841 return -EBUSY;
1842 }
1843 chip->irq = irq;
1844
1845 if (request_dma(dma1, "ES18xx DMA 1")) {
1846 snd_es18xx_free(chip);
1847 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1848 return -EBUSY;
1849 }
1850 chip->dma1 = dma1;
1851
1852 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1853 snd_es18xx_free(chip);
1854 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1855 return -EBUSY;
1856 }
1857 chip->dma2 = dma2;
1858
1859 if (snd_es18xx_probe(chip) < 0) {
1860 snd_es18xx_free(chip);
1861 return -ENODEV;
1862 }
1863 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1864 snd_es18xx_free(chip);
1865 return err;
1866 }
1867 *rchip = chip;
1868 return 0;
1869}
1870
Takashi Iwai8047c912005-11-17 14:41:22 +01001871static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai8047c912005-11-17 14:41:22 +01001873 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int err;
1875 unsigned int idx;
1876
1877 card = chip->card;
1878
1879 strcpy(card->mixername, chip->pcm->name);
1880
1881 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001882 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1884 if (chip->caps & ES18XX_HWV) {
1885 switch (idx) {
1886 case 0:
1887 chip->master_volume = kctl;
1888 kctl->private_free = snd_es18xx_hwv_free;
1889 break;
1890 case 1:
1891 chip->master_switch = kctl;
1892 kctl->private_free = snd_es18xx_hwv_free;
1893 break;
1894 }
1895 }
1896 if ((err = snd_ctl_add(card, kctl)) < 0)
1897 return err;
1898 }
1899 if (chip->caps & ES18XX_PCM2) {
1900 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1901 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1902 return err;
1903 }
1904 } else {
1905 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1906 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1907 return err;
1908 }
1909 }
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (chip->caps & ES18XX_RECMIX) {
1912 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1913 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1914 return err;
1915 }
1916 }
1917 switch (chip->version) {
1918 default:
1919 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1920 return err;
1921 break;
1922 case 0x1869:
1923 case 0x1879:
1924 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1925 return err;
1926 break;
1927 }
1928 if (chip->caps & ES18XX_SPATIALIZER) {
1929 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1930 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1931 return err;
1932 }
1933 }
1934 if (chip->caps & ES18XX_HWV) {
1935 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001936 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1938 if (idx == 0)
1939 chip->hw_volume = kctl;
1940 else
1941 chip->hw_switch = kctl;
1942 kctl->private_free = snd_es18xx_hwv_free;
1943 if ((err = snd_ctl_add(card, kctl)) < 0)
1944 return err;
1945
1946 }
1947 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001948 /* finish initializing other chipset specific controls
1949 */
1950 if (chip->version != 0x1868) {
1951 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1952 chip));
1953 if (err < 0)
1954 return err;
1955 }
1956 if (chip->version == 0x1869) {
1957 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1958 err = snd_ctl_add(card,
1959 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1960 chip));
1961 if (err < 0)
1962 return err;
1963 }
Mark Salazar95b71292006-01-16 11:35:40 +01001964 } else if (chip->version == 0x1878) {
1965 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1966 chip));
1967 if (err < 0)
1968 return err;
1969 } else if (chip->version == 0x1879) {
1970 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1971 err = snd_ctl_add(card,
1972 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1973 chip));
1974 if (err < 0)
1975 return err;
1976 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979}
1980
1981
1982/* Card level */
1983
1984MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1985MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1986MODULE_LICENSE("GPL");
1987MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1988 "{ESS,ES1869 PnP AudioDrive},"
1989 "{ESS,ES1878 PnP AudioDrive},"
1990 "{ESS,ES1879 PnP AudioDrive},"
1991 "{ESS,ES1887 PnP AudioDrive},"
1992 "{ESS,ES1888 PnP AudioDrive},"
1993 "{ESS,ES1887 AudioDrive},"
1994 "{ESS,ES1888 AudioDrive}}");
1995
1996static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1997static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1998static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1999#ifdef CONFIG_PNP
2000static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
2001#endif
2002static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
2003#ifndef CONFIG_PNP
2004static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
2005#else
2006static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2007#endif
2008static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2009static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
2010static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
2011static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
2012
2013module_param_array(index, int, NULL, 0444);
2014MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2015module_param_array(id, charp, NULL, 0444);
2016MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2017module_param_array(enable, bool, NULL, 0444);
2018MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2019#ifdef CONFIG_PNP
2020module_param_array(isapnp, bool, NULL, 0444);
2021MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2022#endif
2023module_param_array(port, long, NULL, 0444);
2024MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2025module_param_array(mpu_port, long, NULL, 0444);
2026MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2027module_param_array(fm_port, long, NULL, 0444);
2028MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2029module_param_array(irq, int, NULL, 0444);
2030MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2031module_param_array(dma1, int, NULL, 0444);
2032MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2033module_param_array(dma2, int, NULL, 0444);
2034MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2035
Clemens Ladischf7a92752005-12-07 09:13:42 +01002036static struct platform_device *platform_devices[SNDRV_CARDS];
Clemens Ladischf7a92752005-12-07 09:13:42 +01002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038#ifdef CONFIG_PNP
Takashi Iwai59b1b342006-01-04 15:06:44 +01002039static int pnp_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2042 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2043 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2044 /* ESS 1868 (integrated on Maxisound Cards) */
2045 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2046 /* ESS 1868 (integrated on Maxisound Cards) */
2047 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2048 /* ESS ES1869 Plug and Play AudioDrive */
2049 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2050 /* ESS 1869 */
2051 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2052 /* ESS 1878 */
2053 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2054 /* ESS 1879 */
2055 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2056 /* --- */
2057 { .id = "" } /* end */
2058};
2059
2060MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2061
2062static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2063 struct pnp_card_link *card,
2064 const struct pnp_card_device_id *id)
2065{
2066 struct pnp_dev *pdev;
2067 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
2068 int err;
2069
2070 if (!cfg)
2071 return -ENOMEM;
2072 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2073 if (acard->dev == NULL) {
2074 kfree(cfg);
2075 return -EBUSY;
2076 }
2077 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2078 if (acard->devc == NULL) {
2079 kfree(cfg);
2080 return -EBUSY;
2081 }
2082 /* Control port initialization */
2083 err = pnp_activate_dev(acard->devc);
2084 if (err < 0) {
2085 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2086 return -EAGAIN;
2087 }
2088 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
2089 /* PnP initialization */
2090 pdev = acard->dev;
2091 pnp_init_resource_table(cfg);
2092 if (port[dev] != SNDRV_AUTO_PORT)
2093 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
2094 if (fm_port[dev] != SNDRV_AUTO_PORT)
2095 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
2096 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2097 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
2098 if (dma1[dev] != SNDRV_AUTO_DMA)
2099 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
2100 if (dma2[dev] != SNDRV_AUTO_DMA)
2101 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
2102 if (irq[dev] != SNDRV_AUTO_IRQ)
2103 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
2104 err = pnp_manual_config_dev(pdev, cfg, 0);
2105 if (err < 0)
2106 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
2107 err = pnp_activate_dev(pdev);
2108 if (err < 0) {
2109 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2110 kfree(cfg);
2111 return -EBUSY;
2112 }
2113 /* ok. hack using Vendor-Defined Card-Level registers */
2114 /* skip csn and logdev initialization - already done in isapnp_configure */
2115 if (pnp_device_is_isapnp(pdev)) {
2116 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2117 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2118 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2119 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2120 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2121 isapnp_cfg_end();
2122 } else {
2123 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
2124 }
2125 port[dev] = pnp_port_start(pdev, 0);
2126 fm_port[dev] = pnp_port_start(pdev, 1);
2127 mpu_port[dev] = pnp_port_start(pdev, 2);
2128 dma1[dev] = pnp_dma(pdev, 0);
2129 dma2[dev] = pnp_dma(pdev, 1);
2130 irq[dev] = pnp_irq(pdev, 0);
2131 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2132 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2133 kfree(cfg);
2134 return 0;
2135}
2136#endif /* CONFIG_PNP */
2137
Takashi Iwai43bcd972005-09-05 17:19:20 +02002138#ifdef CONFIG_PNP
2139#define is_isapnp_selected(dev) isapnp[dev]
2140#else
2141#define is_isapnp_selected(dev) 0
2142#endif
2143
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002144static struct snd_card *snd_es18xx_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002146 return snd_card_new(index[dev], id[dev], THIS_MODULE,
2147 sizeof(struct snd_audiodrive));
2148}
2149
2150static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2151{
2152 struct snd_audiodrive *acard = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002153 struct snd_es18xx *chip;
2154 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int err;
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if ((err = snd_es18xx_new_device(card,
2158 port[dev],
2159 mpu_port[dev],
2160 fm_port[dev],
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002161 irq[dev], dma1[dev], dma2[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +02002162 &chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002163 return err;
2164 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002169 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2171 card->shortname,
2172 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002173 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 else
2175 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2176 card->shortname,
2177 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002178 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Takashi Iwai43bcd972005-09-05 17:19:20 +02002180 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002181 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002182
2183 if ((err = snd_es18xx_mixer(chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002184 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2187 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 +02002188 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002190 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
2194
2195 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2196 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2197 chip->mpu_port, 0,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002198 irq[dev], 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002199 &chip->rmidi)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002203 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002206static int __init snd_es18xx_nonpnp_probe1(int dev, struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002208 struct snd_card *card;
2209 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002211 card = snd_es18xx_card_new(dev);
2212 if (! card)
2213 return -ENOMEM;
2214 snd_card_set_dev(card, &devptr->dev);
2215 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2216 snd_card_free(card);
2217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002219 platform_set_drvdata(devptr, card);
2220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002223static int __init snd_es18xx_nonpnp_probe(struct platform_device *pdev)
2224{
2225 int dev = pdev->id;
2226 int err;
2227 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2228 static int possible_dmas[] = {1, 0, 3, 5, -1};
2229
2230 if (irq[dev] == SNDRV_AUTO_IRQ) {
2231 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2232 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2233 return -EBUSY;
2234 }
2235 }
2236 if (dma1[dev] == SNDRV_AUTO_DMA) {
2237 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2238 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2239 return -EBUSY;
2240 }
2241 }
2242 if (dma2[dev] == SNDRV_AUTO_DMA) {
2243 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2244 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2245 return -EBUSY;
2246 }
2247 }
2248
2249 if (port[dev] != SNDRV_AUTO_PORT) {
2250 return snd_es18xx_nonpnp_probe1(dev, pdev);
2251 } else {
2252 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2253 int i;
2254 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2255 port[dev] = possible_ports[i];
2256 err = snd_es18xx_nonpnp_probe1(dev, pdev);
2257 if (! err)
2258 return 0;
2259 }
2260 return err;
2261 }
2262}
2263
2264static int __devexit snd_es18xx_nonpnp_remove(struct platform_device *devptr)
2265{
2266 snd_card_free(platform_get_drvdata(devptr));
2267 platform_set_drvdata(devptr, NULL);
2268 return 0;
2269}
2270
2271#ifdef CONFIG_PM
2272static int snd_es18xx_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
2273{
2274 return snd_es18xx_suspend(platform_get_drvdata(dev), state);
2275}
2276
2277static int snd_es18xx_nonpnp_resume(struct platform_device *dev)
2278{
2279 return snd_es18xx_resume(platform_get_drvdata(dev));
2280}
2281#endif
2282
2283#define ES18XX_DRIVER "snd_es18xx"
2284
2285static struct platform_driver snd_es18xx_nonpnp_driver = {
2286 .probe = snd_es18xx_nonpnp_probe,
2287 .remove = __devexit_p(snd_es18xx_nonpnp_remove),
2288#ifdef CONFIG_PM
2289 .suspend = snd_es18xx_nonpnp_suspend,
2290 .resume = snd_es18xx_nonpnp_resume,
2291#endif
2292 .driver = {
2293 .name = ES18XX_DRIVER
2294 },
2295};
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298#ifdef CONFIG_PNP
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002299static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *pcard,
2300 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002303 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int res;
2305
2306 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002307 if (enable[dev] && isapnp[dev])
2308 break;
2309 }
2310 if (dev >= SNDRV_CARDS)
2311 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002313 card = snd_es18xx_card_new(dev);
2314 if (! card)
2315 return -ENOMEM;
2316
2317 if ((res = snd_audiodrive_pnp(dev, card->private_data, pcard, pid)) < 0) {
2318 snd_card_free(card);
2319 return res;
2320 }
2321 snd_card_set_dev(card, &pcard->card->dev);
2322 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2323 snd_card_free(card);
2324 return res;
2325 }
2326
2327 pnp_set_card_drvdata(pcard, card);
2328 dev++;
2329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2333{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002334 snd_card_free(pnp_get_card_drvdata(pcard));
2335 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002338#ifdef CONFIG_PM
2339static int snd_audiodrive_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
2340{
2341 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2342}
2343
2344static int snd_audiodrive_pnp_resume(struct pnp_card_link *pcard)
2345{
2346 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2347}
2348
2349#endif
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351static struct pnp_card_driver es18xx_pnpc_driver = {
2352 .flags = PNP_DRIVER_RES_DISABLE,
2353 .name = "es18xx",
2354 .id_table = snd_audiodrive_pnpids,
2355 .probe = snd_audiodrive_pnp_detect,
2356 .remove = __devexit_p(snd_audiodrive_pnp_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002357#ifdef CONFIG_PM
2358 .suspend = snd_audiodrive_pnp_suspend,
2359 .resume = snd_audiodrive_pnp_resume,
2360#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361};
2362#endif /* CONFIG_PNP */
2363
Clemens Ladischf7a92752005-12-07 09:13:42 +01002364static void __init_or_module snd_es18xx_unregister_all(void)
2365{
2366 int i;
2367
Takashi Iwai59b1b342006-01-04 15:06:44 +01002368#ifdef CONFIG_PNP
Clemens Ladischf7a92752005-12-07 09:13:42 +01002369 if (pnp_registered)
2370 pnp_unregister_card_driver(&es18xx_pnpc_driver);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002371#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002372 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
2373 platform_device_unregister(platform_devices[i]);
2374 platform_driver_unregister(&snd_es18xx_nonpnp_driver);
2375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377static int __init alsa_card_es18xx_init(void)
2378{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002379 int i, err, cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002381 if ((err = platform_driver_register(&snd_es18xx_nonpnp_driver)) < 0)
2382 return err;
2383
Takashi Iwai8278ca82006-02-20 11:57:34 +01002384 for (i = 0; i < SNDRV_CARDS; i++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002385 struct platform_device *device;
Takashi Iwai8278ca82006-02-20 11:57:34 +01002386 if (! enable[i] || is_isapnp_selected(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 continue;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002388 device = platform_device_register_simple(ES18XX_DRIVER,
2389 i, NULL, 0);
2390 if (IS_ERR(device)) {
2391 err = PTR_ERR(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002392 goto errout;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002393 }
Clemens Ladischf7a92752005-12-07 09:13:42 +01002394 platform_devices[i] = device;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002395 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Takashi Iwai59b1b342006-01-04 15:06:44 +01002398#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 i = pnp_register_card_driver(&es18xx_pnpc_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002400 if (i >= 0) {
2401 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 cards += i;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002403 }
Takashi Iwai59b1b342006-01-04 15:06:44 +01002404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if(!cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407#ifdef MODULE
2408 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2409#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002410 err = -ENODEV;
2411 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 return 0;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002414
2415 errout:
2416 snd_es18xx_unregister_all();
2417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420static void __exit alsa_card_es18xx_exit(void)
2421{
Clemens Ladischf7a92752005-12-07 09:13:42 +01002422 snd_es18xx_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
2425module_init(alsa_card_es18xx_init)
2426module_exit(alsa_card_es18xx_exit)