blob: 93335000c51e6a04ed96bc81ef7230308f77b3a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/* GENERAL NOTES:
23 *
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
52 * BUGS:
53 *
54 * - There is a major trouble I noted:
55 *
56 * using both channel for playback stereo 16 bit samples at 44100 Hz
57 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
58 *
59 * The same happens using Audio1 for captureing.
60 *
61 * The Windows driver does not suffer of this (although it use Audio1
62 * only for captureing). I'm unable to discover why.
63 *
64 */
65
66
67#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010069#include <linux/err.h>
70#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/pnp.h>
73#include <linux/isapnp.h>
74#include <linux/moduleparam.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010075#include <asm/io.h>
76#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <sound/core.h>
78#include <sound/control.h>
79#include <sound/pcm.h>
80#include <sound/pcm_params.h>
81#include <sound/mpu401.h>
82#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define SNDRV_LEGACY_FIND_FREE_IRQ
84#define SNDRV_LEGACY_FIND_FREE_DMA
85#include <sound/initval.h>
86
87#define PFX "es18xx: "
88
Takashi Iwai8047c912005-11-17 14:41:22 +010089struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long port; /* port of ESS chip */
91 unsigned long mpu_port; /* MPU-401 port of ESS chip */
92 unsigned long fm_port; /* FM port */
93 unsigned long ctrl_port; /* Control port of ESS chip */
94 struct resource *res_port;
95 struct resource *res_mpu_port;
96 struct resource *res_ctrl_port;
97 int irq; /* IRQ number of ESS chip */
98 int dma1; /* DMA1 */
99 int dma2; /* DMA2 */
100 unsigned short version; /* version of ESS chip */
101 int caps; /* Chip capabilities */
102 unsigned short audio2_vol; /* volume level of audio2 */
103
104 unsigned short active; /* active channel mask */
105 unsigned int dma1_size;
106 unsigned int dma2_size;
107 unsigned int dma1_shift;
108 unsigned int dma2_shift;
109
Takashi Iwai8047c912005-11-17 14:41:22 +0100110 struct snd_card *card;
111 struct snd_pcm *pcm;
112 struct snd_pcm_substream *playback_a_substream;
113 struct snd_pcm_substream *capture_a_substream;
114 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwai8047c912005-11-17 14:41:22 +0100116 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai8047c912005-11-17 14:41:22 +0100118 struct snd_kcontrol *hw_volume;
119 struct snd_kcontrol *hw_switch;
120 struct snd_kcontrol *master_volume;
121 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 spinlock_t reg_lock;
124 spinlock_t mixer_lock;
125 spinlock_t ctrl_lock;
126#ifdef CONFIG_PM
127 unsigned char pm_reg;
128#endif
129};
130
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100131struct snd_audiodrive {
132 struct snd_es18xx *chip;
133#ifdef CONFIG_PNP
134 struct pnp_dev *dev;
135 struct pnp_dev *devc;
136#endif
137};
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define AUDIO1_IRQ 0x01
140#define AUDIO2_IRQ 0x02
141#define HWV_IRQ 0x04
142#define MPU_IRQ 0x08
143
144#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
145#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
146#define ES18XX_RECMIX 0x0004 /* Has record mixer */
147#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
148#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
149#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
150#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Mark Salazar14086772006-01-16 11:33:52 +0100151#define ES18XX_HWV 0x0080 /* Has seperate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
153#define ES18XX_I2S 0x0200 /* I2S mixer control */
154#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
155#define ES18XX_CONTROL 0x0800 /* Has control ports */
156
157/* Power Management */
158#define ES18XX_PM 0x07
159#define ES18XX_PM_GPO0 0x01
160#define ES18XX_PM_GPO1 0x02
161#define ES18XX_PM_PDR 0x04
162#define ES18XX_PM_ANA 0x08
163#define ES18XX_PM_FM 0x020
164#define ES18XX_PM_SUS 0x080
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Lowlevel */
167
168#define DAC1 0x01
169#define ADC1 0x02
170#define DAC2 0x04
171#define MILLISECOND 10000
172
Takashi Iwai8047c912005-11-17 14:41:22 +0100173static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int i;
176
177 for(i = MILLISECOND; i; i--)
178 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
179 outb(val, chip->port + 0x0C);
180 return 0;
181 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200182 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184}
185
Takashi Iwai8047c912005-11-17 14:41:22 +0100186static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
189
190 for(i = MILLISECOND/10; i; i--)
191 if (inb(chip->port + 0x0C) & 0x40)
192 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200193 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
194 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ENODEV;
196}
197
198#undef REG_DEBUG
199
Takashi Iwai8047c912005-11-17 14:41:22 +0100200static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned char reg, unsigned char data)
202{
203 unsigned long flags;
204 int ret;
205
206 spin_lock_irqsave(&chip->reg_lock, flags);
207 ret = snd_es18xx_dsp_command(chip, reg);
208 if (ret < 0)
209 goto end;
210 ret = snd_es18xx_dsp_command(chip, data);
211 end:
212 spin_unlock_irqrestore(&chip->reg_lock, flags);
213#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200214 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216 return ret;
217}
218
Takashi Iwai8047c912005-11-17 14:41:22 +0100219static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned long flags;
222 int ret, data;
223 spin_lock_irqsave(&chip->reg_lock, flags);
224 ret = snd_es18xx_dsp_command(chip, 0xC0);
225 if (ret < 0)
226 goto end;
227 ret = snd_es18xx_dsp_command(chip, reg);
228 if (ret < 0)
229 goto end;
230 data = snd_es18xx_dsp_get_byte(chip);
231 ret = data;
232#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200233 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif
235 end:
236 spin_unlock_irqrestore(&chip->reg_lock, flags);
237 return ret;
238}
239
240/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100241static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned char mask, unsigned char val)
243{
244 int ret;
245 unsigned char old, new, oval;
246 unsigned long flags;
247 spin_lock_irqsave(&chip->reg_lock, flags);
248 ret = snd_es18xx_dsp_command(chip, 0xC0);
249 if (ret < 0)
250 goto end;
251 ret = snd_es18xx_dsp_command(chip, reg);
252 if (ret < 0)
253 goto end;
254 ret = snd_es18xx_dsp_get_byte(chip);
255 if (ret < 0) {
256 goto end;
257 }
258 old = ret;
259 oval = old & mask;
260 if (val != oval) {
261 ret = snd_es18xx_dsp_command(chip, reg);
262 if (ret < 0)
263 goto end;
264 new = (old & ~mask) | (val & mask);
265 ret = snd_es18xx_dsp_command(chip, new);
266 if (ret < 0)
267 goto end;
268#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200269 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
270 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#endif
272 }
273 ret = oval;
274 end:
275 spin_unlock_irqrestore(&chip->reg_lock, flags);
276 return ret;
277}
278
Takashi Iwai8047c912005-11-17 14:41:22 +0100279static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned char reg, unsigned char data)
281{
282 unsigned long flags;
283 spin_lock_irqsave(&chip->mixer_lock, flags);
284 outb(reg, chip->port + 0x04);
285 outb(data, chip->port + 0x05);
286 spin_unlock_irqrestore(&chip->mixer_lock, flags);
287#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200288 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif
290}
291
Takashi Iwai8047c912005-11-17 14:41:22 +0100292static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned long flags;
295 int data;
296 spin_lock_irqsave(&chip->mixer_lock, flags);
297 outb(reg, chip->port + 0x04);
298 data = inb(chip->port + 0x05);
299 spin_unlock_irqrestore(&chip->mixer_lock, flags);
300#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200301 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303 return data;
304}
305
306/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100307static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned char mask, unsigned char val)
309{
310 unsigned char old, new, oval;
311 unsigned long flags;
312 spin_lock_irqsave(&chip->mixer_lock, flags);
313 outb(reg, chip->port + 0x04);
314 old = inb(chip->port + 0x05);
315 oval = old & mask;
316 if (val != oval) {
317 new = (old & ~mask) | (val & mask);
318 outb(new, chip->port + 0x05);
319#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200320 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
321 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#endif
323 }
324 spin_unlock_irqrestore(&chip->mixer_lock, flags);
325 return oval;
326}
327
Takashi Iwai8047c912005-11-17 14:41:22 +0100328static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned char mask)
330{
331 int old, expected, new;
332 unsigned long flags;
333 spin_lock_irqsave(&chip->mixer_lock, flags);
334 outb(reg, chip->port + 0x04);
335 old = inb(chip->port + 0x05);
336 expected = old ^ mask;
337 outb(expected, chip->port + 0x05);
338 new = inb(chip->port + 0x05);
339 spin_unlock_irqrestore(&chip->mixer_lock, flags);
340#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200341 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
342 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#endif
344 return expected == new;
345}
346
347
Takashi Iwai8047c912005-11-17 14:41:22 +0100348static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int i;
351 outb(0x03, chip->port + 0x06);
352 inb(chip->port + 0x06);
353 outb(0x00, chip->port + 0x06);
354 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
355 if (inb(chip->port + 0x0A) != 0xAA)
356 return -1;
357 return 0;
358}
359
Takashi Iwai8047c912005-11-17 14:41:22 +0100360static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 outb(0x02, chip->port + 0x06);
363 inb(chip->port + 0x06);
364 outb(0x00, chip->port + 0x06);
365 return 0;
366}
367
Takashi Iwai8047c912005-11-17 14:41:22 +0100368static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 {
370 .num = 793800,
371 .den_min = 1,
372 .den_max = 128,
373 .den_step = 1,
374 },
375 {
376 .num = 768000,
377 .den_min = 1,
378 .den_max = 128,
379 .den_step = 1,
380 }
381};
382
Takashi Iwai8047c912005-11-17 14:41:22 +0100383static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .nrats = 2,
385 .rats = new_clocks,
386};
387
Takashi Iwai8047c912005-11-17 14:41:22 +0100388static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 {
390 .num = 795444,
391 .den_min = 1,
392 .den_max = 128,
393 .den_step = 1,
394 },
395 {
396 .num = 397722,
397 .den_min = 1,
398 .den_max = 128,
399 .den_step = 1,
400 }
401};
402
Takashi Iwai8047c912005-11-17 14:41:22 +0100403static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .nrats = 2,
405 .rats = old_clocks,
406};
407
408
Takashi Iwai8047c912005-11-17 14:41:22 +0100409static void snd_es18xx_rate_set(struct snd_es18xx *chip,
410 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int mode)
412{
413 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100414 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (chip->caps & ES18XX_NEW_RATE) {
416 if (runtime->rate_num == new_clocks[0].num)
417 bits = 128 - runtime->rate_den;
418 else
419 bits = 256 - runtime->rate_den;
420 } else {
421 if (runtime->rate_num == old_clocks[0].num)
422 bits = 256 - runtime->rate_den;
423 else
424 bits = 128 - runtime->rate_den;
425 }
426
427 /* set filter register */
428 div0 = 256 - 7160000*20/(8*82*runtime->rate);
429
430 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
431 snd_es18xx_mixer_write(chip, 0x70, bits);
432 /*
433 * Comment from kernel oss driver:
434 * FKS: fascinating: 0x72 doesn't seem to work.
435 */
436 snd_es18xx_write(chip, 0xA2, div0);
437 snd_es18xx_mixer_write(chip, 0x72, div0);
438 } else {
439 snd_es18xx_write(chip, 0xA1, bits);
440 snd_es18xx_write(chip, 0xA2, div0);
441 }
442}
443
Takashi Iwai8047c912005-11-17 14:41:22 +0100444static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai8047c912005-11-17 14:41:22 +0100447 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int shift, err;
449
450 shift = 0;
451 if (params_channels(hw_params) == 2)
452 shift++;
453 if (snd_pcm_format_width(params_format(hw_params)) == 16)
454 shift++;
455
456 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
457 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
458 (chip->capture_a_substream) &&
459 params_channels(hw_params) != 1) {
460 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
461 return -EBUSY;
462 }
463 chip->dma2_shift = shift;
464 } else {
465 chip->dma1_shift = shift;
466 }
467 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
468 return err;
469 return 0;
470}
471
Takashi Iwai8047c912005-11-17 14:41:22 +0100472static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 return snd_pcm_lib_free_pages(substream);
475}
476
Takashi Iwai8047c912005-11-17 14:41:22 +0100477static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
478 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Takashi Iwai8047c912005-11-17 14:41:22 +0100480 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
482 unsigned int count = snd_pcm_lib_period_bytes(substream);
483
484 chip->dma2_size = size;
485
486 snd_es18xx_rate_set(chip, substream, DAC2);
487
488 /* Transfer Count Reload */
489 count = 0x10000 - count;
490 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
491 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
492
493 /* Set format */
494 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
495 ((runtime->channels == 1) ? 0x00 : 0x02) |
496 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
497 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
498
499 /* Set DMA controller */
500 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
501
502 return 0;
503}
504
Takashi Iwai8047c912005-11-17 14:41:22 +0100505static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
506 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int cmd)
508{
509 switch (cmd) {
510 case SNDRV_PCM_TRIGGER_START:
511 case SNDRV_PCM_TRIGGER_RESUME:
512 if (chip->active & DAC2)
513 return 0;
514 chip->active |= DAC2;
515 /* Start DMA */
516 if (chip->dma2 >= 4)
517 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
518 else
519 snd_es18xx_mixer_write(chip, 0x78, 0x93);
520#ifdef AVOID_POPS
521 /* Avoid pops */
522 udelay(100000);
523 if (chip->caps & ES18XX_PCM2)
524 /* Restore Audio 2 volume */
525 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
526 else
527 /* Enable PCM output */
528 snd_es18xx_dsp_command(chip, 0xD1);
529#endif
530 break;
531 case SNDRV_PCM_TRIGGER_STOP:
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 if (!(chip->active & DAC2))
534 return 0;
535 chip->active &= ~DAC2;
536 /* Stop DMA */
537 snd_es18xx_mixer_write(chip, 0x78, 0x00);
538#ifdef AVOID_POPS
539 udelay(25000);
540 if (chip->caps & ES18XX_PCM2)
541 /* Set Audio 2 volume to 0 */
542 snd_es18xx_mixer_write(chip, 0x7C, 0);
543 else
544 /* Disable PCM output */
545 snd_es18xx_dsp_command(chip, 0xD3);
546#endif
547 break;
548 default:
549 return -EINVAL;
550 }
551
552 return 0;
553}
554
Takashi Iwai8047c912005-11-17 14:41:22 +0100555static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
556 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai8047c912005-11-17 14:41:22 +0100558 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int shift, err;
560
561 shift = 0;
562 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
563 chip->playback_a_substream &&
564 params_channels(hw_params) != 1) {
565 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
566 return -EBUSY;
567 }
568 if (params_channels(hw_params) == 2)
569 shift++;
570 if (snd_pcm_format_width(params_format(hw_params)) == 16)
571 shift++;
572 chip->dma1_shift = shift;
573 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
574 return err;
575 return 0;
576}
577
Takashi Iwai8047c912005-11-17 14:41:22 +0100578static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai8047c912005-11-17 14:41:22 +0100580 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
583 unsigned int count = snd_pcm_lib_period_bytes(substream);
584
585 chip->dma1_size = size;
586
587 snd_es18xx_reset_fifo(chip);
588
589 /* Set stereo/mono */
590 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
591
592 snd_es18xx_rate_set(chip, substream, ADC1);
593
594 /* Transfer Count Reload */
595 count = 0x10000 - count;
596 snd_es18xx_write(chip, 0xA4, count & 0xff);
597 snd_es18xx_write(chip, 0xA5, count >> 8);
598
599#ifdef AVOID_POPS
600 udelay(100000);
601#endif
602
603 /* Set format */
604 snd_es18xx_write(chip, 0xB7,
605 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
606 snd_es18xx_write(chip, 0xB7, 0x90 |
607 ((runtime->channels == 1) ? 0x40 : 0x08) |
608 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
609 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
610
611 /* Set DMA controler */
612 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
613
614 return 0;
615}
616
Takashi Iwai8047c912005-11-17 14:41:22 +0100617static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int cmd)
619{
Takashi Iwai8047c912005-11-17 14:41:22 +0100620 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 switch (cmd) {
623 case SNDRV_PCM_TRIGGER_START:
624 case SNDRV_PCM_TRIGGER_RESUME:
625 if (chip->active & ADC1)
626 return 0;
627 chip->active |= ADC1;
628 /* Start DMA */
629 snd_es18xx_write(chip, 0xB8, 0x0f);
630 break;
631 case SNDRV_PCM_TRIGGER_STOP:
632 case SNDRV_PCM_TRIGGER_SUSPEND:
633 if (!(chip->active & ADC1))
634 return 0;
635 chip->active &= ~ADC1;
636 /* Stop DMA */
637 snd_es18xx_write(chip, 0xB8, 0x00);
638 break;
639 default:
640 return -EINVAL;
641 }
642
643 return 0;
644}
645
Takashi Iwai8047c912005-11-17 14:41:22 +0100646static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
647 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai8047c912005-11-17 14:41:22 +0100649 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
651 unsigned int count = snd_pcm_lib_period_bytes(substream);
652
653 chip->dma1_size = size;
654
655 snd_es18xx_reset_fifo(chip);
656
657 /* Set stereo/mono */
658 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
659
660 snd_es18xx_rate_set(chip, substream, DAC1);
661
662 /* Transfer Count Reload */
663 count = 0x10000 - count;
664 snd_es18xx_write(chip, 0xA4, count & 0xff);
665 snd_es18xx_write(chip, 0xA5, count >> 8);
666
667 /* Set format */
668 snd_es18xx_write(chip, 0xB6,
669 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
670 snd_es18xx_write(chip, 0xB7,
671 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
672 snd_es18xx_write(chip, 0xB7, 0x90 |
673 (runtime->channels == 1 ? 0x40 : 0x08) |
674 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
675 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
676
677 /* Set DMA controler */
678 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
679
680 return 0;
681}
682
Takashi Iwai8047c912005-11-17 14:41:22 +0100683static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
684 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int cmd)
686{
687 switch (cmd) {
688 case SNDRV_PCM_TRIGGER_START:
689 case SNDRV_PCM_TRIGGER_RESUME:
690 if (chip->active & DAC1)
691 return 0;
692 chip->active |= DAC1;
693 /* Start DMA */
694 snd_es18xx_write(chip, 0xB8, 0x05);
695#ifdef AVOID_POPS
696 /* Avoid pops */
697 udelay(100000);
698 /* Enable Audio 1 */
699 snd_es18xx_dsp_command(chip, 0xD1);
700#endif
701 break;
702 case SNDRV_PCM_TRIGGER_STOP:
703 case SNDRV_PCM_TRIGGER_SUSPEND:
704 if (!(chip->active & DAC1))
705 return 0;
706 chip->active &= ~DAC1;
707 /* Stop DMA */
708 snd_es18xx_write(chip, 0xB8, 0x00);
709#ifdef AVOID_POPS
710 /* Avoid pops */
711 udelay(25000);
712 /* Disable Audio 1 */
713 snd_es18xx_dsp_command(chip, 0xD3);
714#endif
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 return 0;
721}
722
Takashi Iwai8047c912005-11-17 14:41:22 +0100723static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Takashi Iwai8047c912005-11-17 14:41:22 +0100725 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
727 return snd_es18xx_playback1_prepare(chip, substream);
728 else
729 return snd_es18xx_playback2_prepare(chip, substream);
730}
731
Takashi Iwai8047c912005-11-17 14:41:22 +0100732static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int cmd)
734{
Takashi Iwai8047c912005-11-17 14:41:22 +0100735 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
737 return snd_es18xx_playback1_trigger(chip, substream, cmd);
738 else
739 return snd_es18xx_playback2_trigger(chip, substream, cmd);
740}
741
742static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
743{
Takashi Iwai8047c912005-11-17 14:41:22 +0100744 struct snd_es18xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned char status;
746
747 if (chip->caps & ES18XX_CONTROL) {
748 /* Read Interrupt status */
749 status = inb(chip->ctrl_port + 6);
750 } else {
751 /* Read Interrupt status */
752 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
753 }
754#if 0
755 else {
756 status = 0;
757 if (inb(chip->port + 0x0C) & 0x01)
758 status |= AUDIO1_IRQ;
759 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
760 status |= AUDIO2_IRQ;
761 if ((chip->caps & ES18XX_HWV) &&
762 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
763 status |= HWV_IRQ;
764 }
765#endif
766
767 /* Audio 1 & Audio 2 */
768 if (status & AUDIO2_IRQ) {
769 if (chip->active & DAC2)
770 snd_pcm_period_elapsed(chip->playback_a_substream);
771 /* ack interrupt */
772 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
773 }
774 if (status & AUDIO1_IRQ) {
775 /* ok.. capture is active */
776 if (chip->active & ADC1)
777 snd_pcm_period_elapsed(chip->capture_a_substream);
778 /* ok.. playback2 is active */
779 else if (chip->active & DAC1)
780 snd_pcm_period_elapsed(chip->playback_b_substream);
781 /* ack interrupt */
782 inb(chip->port + 0x0E);
783 }
784
785 /* MPU */
786 if ((status & MPU_IRQ) && chip->rmidi)
787 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
788
789 /* Hardware volume */
790 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100791 int split = 0;
792 if (chip->caps & ES18XX_HWV) {
793 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
794 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
795 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!split) {
798 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
799 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
800 }
801 /* ack interrupt */
802 snd_es18xx_mixer_write(chip, 0x66, 0x00);
803 }
804 return IRQ_HANDLED;
805}
806
Takashi Iwai8047c912005-11-17 14:41:22 +0100807static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai8047c912005-11-17 14:41:22 +0100809 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int pos;
811
812 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
813 if (!(chip->active & DAC2))
814 return 0;
815 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
816 return pos >> chip->dma2_shift;
817 } else {
818 if (!(chip->active & DAC1))
819 return 0;
820 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
821 return pos >> chip->dma1_shift;
822 }
823}
824
Takashi Iwai8047c912005-11-17 14:41:22 +0100825static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai8047c912005-11-17 14:41:22 +0100827 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int pos;
829
830 if (!(chip->active & ADC1))
831 return 0;
832 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
833 return pos >> chip->dma1_shift;
834}
835
Takashi Iwai8047c912005-11-17 14:41:22 +0100836static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
839 SNDRV_PCM_INFO_RESUME |
840 SNDRV_PCM_INFO_MMAP_VALID),
841 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
842 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
843 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
844 .rate_min = 4000,
845 .rate_max = 48000,
846 .channels_min = 1,
847 .channels_max = 2,
848 .buffer_bytes_max = 65536,
849 .period_bytes_min = 64,
850 .period_bytes_max = 65536,
851 .periods_min = 1,
852 .periods_max = 1024,
853 .fifo_size = 0,
854};
855
Takashi Iwai8047c912005-11-17 14:41:22 +0100856static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
859 SNDRV_PCM_INFO_RESUME |
860 SNDRV_PCM_INFO_MMAP_VALID),
861 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
862 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
863 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
864 .rate_min = 4000,
865 .rate_max = 48000,
866 .channels_min = 1,
867 .channels_max = 2,
868 .buffer_bytes_max = 65536,
869 .period_bytes_min = 64,
870 .period_bytes_max = 65536,
871 .periods_min = 1,
872 .periods_max = 1024,
873 .fifo_size = 0,
874};
875
Takashi Iwai8047c912005-11-17 14:41:22 +0100876static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwai8047c912005-11-17 14:41:22 +0100878 struct snd_pcm_runtime *runtime = substream->runtime;
879 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
882 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
883 chip->capture_a_substream &&
884 chip->capture_a_substream->runtime->channels != 1)
885 return -EAGAIN;
886 chip->playback_a_substream = substream;
887 } else if (substream->number <= 1) {
888 if (chip->capture_a_substream)
889 return -EAGAIN;
890 chip->playback_b_substream = substream;
891 } else {
892 snd_BUG();
893 return -EINVAL;
894 }
895 substream->runtime->hw = snd_es18xx_playback;
896 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
897 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
898 return 0;
899}
900
Takashi Iwai8047c912005-11-17 14:41:22 +0100901static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwai8047c912005-11-17 14:41:22 +0100903 struct snd_pcm_runtime *runtime = substream->runtime;
904 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (chip->playback_b_substream)
907 return -EAGAIN;
908 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
909 chip->playback_a_substream &&
910 chip->playback_a_substream->runtime->channels != 1)
911 return -EAGAIN;
912 chip->capture_a_substream = substream;
913 substream->runtime->hw = snd_es18xx_capture;
914 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
915 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
916 return 0;
917}
918
Takashi Iwai8047c912005-11-17 14:41:22 +0100919static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai8047c912005-11-17 14:41:22 +0100921 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
924 chip->playback_a_substream = NULL;
925 else
926 chip->playback_b_substream = NULL;
927
928 snd_pcm_lib_free_pages(substream);
929 return 0;
930}
931
Takashi Iwai8047c912005-11-17 14:41:22 +0100932static int snd_es18xx_capture_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 chip->capture_a_substream = NULL;
937 snd_pcm_lib_free_pages(substream);
938 return 0;
939}
940
941/*
942 * MIXER part
943 */
944
Mark Salazarf4df2212006-01-16 11:31:14 +0100945/* Record source mux routines:
946 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
947 * bit table for the 4/5 source mux:
948 * reg 1C:
949 * b2 b1 b0 muxSource
950 * x 0 x microphone
951 * 0 1 x CD
952 * 1 1 0 line
953 * 1 1 1 mixer
954 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
955 * either the play mixer or the capture mixer.
956 *
957 * "map4Source" translates from source number to reg bit pattern
958 * "invMap4Source" translates from reg bit pattern to source number
959 */
960
Takashi Iwai8047c912005-11-17 14:41:22 +0100961static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Mark Salazarf4df2212006-01-16 11:31:14 +0100963 static char *texts4Source[4] = {
964 "Mic", "CD", "Line", "Master"
965 };
966 static char *texts5Source[5] = {
967 "Mic", "CD", "Line", "Master", "Mix"
968 };
969 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 "Mic", "Mic Master", "CD", "AOUT",
971 "Mic1", "Mix", "Line", "Master"
972 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100973 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
976 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100977 switch (chip->version) {
978 case 0x1868:
979 case 0x1878:
980 uinfo->value.enumerated.items = 4;
981 if (uinfo->value.enumerated.item > 3)
982 uinfo->value.enumerated.item = 3;
983 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
984 break;
985 case 0x1887:
986 case 0x1888:
987 uinfo->value.enumerated.items = 5;
988 if (uinfo->value.enumerated.item > 4)
989 uinfo->value.enumerated.item = 4;
990 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
991 break;
992 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
993 case 0x1879:
994 uinfo->value.enumerated.items = 8;
995 if (uinfo->value.enumerated.item > 7)
996 uinfo->value.enumerated.item = 7;
997 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
998 break;
999 default:
1000 return -EINVAL;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003}
1004
Takashi Iwai8047c912005-11-17 14:41:22 +01001005static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Mark Salazarf4df2212006-01-16 11:31:14 +01001007 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001008 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001009 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1010 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1011 muxSource = invMap4Source[muxSource];
1012 if (muxSource==3 &&
1013 (chip->version == 0x1887 || chip->version == 0x1888) &&
1014 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1015 )
1016 muxSource = 4;
1017 }
1018 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Takashi Iwai8047c912005-11-17 14:41:22 +01001022static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Mark Salazarf4df2212006-01-16 11:31:14 +01001024 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001025 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001027 unsigned char retVal = 0;
1028
1029 switch (chip->version) {
1030 /* 5 source chips */
1031 case 0x1887:
1032 case 0x1888:
1033 if (val > 4)
1034 return -EINVAL;
1035 if (val == 4) {
1036 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1037 val = 3;
1038 } else
1039 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1040 /* 4 source chips */
1041 case 0x1868:
1042 case 0x1878:
1043 if (val > 3)
1044 return -EINVAL;
1045 val = map4Source[val];
1046 break;
1047 /* 8 source chips */
1048 case 0x1869:
1049 case 0x1879:
1050 if (val > 7)
1051 return -EINVAL;
1052 break;
1053 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001055 }
1056 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Takashi Iwai8047c912005-11-17 14:41:22 +01001059static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1062 uinfo->count = 1;
1063 uinfo->value.integer.min = 0;
1064 uinfo->value.integer.max = 1;
1065 return 0;
1066}
1067
Takashi Iwai8047c912005-11-17 14:41:22 +01001068static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai8047c912005-11-17 14:41:22 +01001070 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1072 ucontrol->value.integer.value[0] = !!(val & 8);
1073 return 0;
1074}
1075
Takashi Iwai8047c912005-11-17 14:41:22 +01001076static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai8047c912005-11-17 14:41:22 +01001078 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned char oval, nval;
1080 int change;
1081 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1082 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1083 change = nval != oval;
1084 if (change) {
1085 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1086 snd_es18xx_mixer_write(chip, 0x50, nval);
1087 }
1088 return change;
1089}
1090
Takashi Iwai8047c912005-11-17 14:41:22 +01001091static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1094 uinfo->count = 2;
1095 uinfo->value.integer.min = 0;
1096 uinfo->value.integer.max = 63;
1097 return 0;
1098}
1099
Takashi Iwai8047c912005-11-17 14:41:22 +01001100static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwai8047c912005-11-17 14:41:22 +01001102 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1104 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1105 return 0;
1106}
1107
Takashi Iwai8047c912005-11-17 14:41:22 +01001108static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1111 uinfo->count = 2;
1112 uinfo->value.integer.min = 0;
1113 uinfo->value.integer.max = 1;
1114 return 0;
1115}
1116
Takashi Iwai8047c912005-11-17 14:41:22 +01001117static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai8047c912005-11-17 14:41:22 +01001119 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1121 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1122 return 0;
1123}
1124
Takashi Iwai8047c912005-11-17 14:41:22 +01001125static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Takashi Iwai8047c912005-11-17 14:41:22 +01001127 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 chip->master_volume = NULL;
1129 chip->master_switch = NULL;
1130 chip->hw_volume = NULL;
1131 chip->hw_switch = NULL;
1132}
1133
Takashi Iwai8047c912005-11-17 14:41:22 +01001134static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 unsigned char mask, unsigned char val)
1136{
1137 if (reg < 0xa0)
1138 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1139 else
1140 return snd_es18xx_bits(chip, reg, mask, val);
1141}
1142
Takashi Iwai8047c912005-11-17 14:41:22 +01001143static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 if (reg < 0xa0)
1146 return snd_es18xx_mixer_read(chip, reg);
1147 else
1148 return snd_es18xx_read(chip, reg);
1149}
1150
1151#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1152{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1153 .info = snd_es18xx_info_single, \
1154 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1155 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1156
Takashi Iwai8047c912005-11-17 14:41:22 +01001157static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 int mask = (kcontrol->private_value >> 16) & 0xff;
1160
1161 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1162 uinfo->count = 1;
1163 uinfo->value.integer.min = 0;
1164 uinfo->value.integer.max = mask;
1165 return 0;
1166}
1167
Takashi Iwai8047c912005-11-17 14:41:22 +01001168static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Takashi Iwai8047c912005-11-17 14:41:22 +01001170 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int reg = kcontrol->private_value & 0xff;
1172 int shift = (kcontrol->private_value >> 8) & 0xff;
1173 int mask = (kcontrol->private_value >> 16) & 0xff;
1174 int invert = (kcontrol->private_value >> 24) & 0xff;
1175 int val;
1176
1177 val = snd_es18xx_reg_read(chip, reg);
1178 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1179 if (invert)
1180 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1181 return 0;
1182}
1183
Takashi Iwai8047c912005-11-17 14:41:22 +01001184static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Takashi Iwai8047c912005-11-17 14:41:22 +01001186 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int reg = kcontrol->private_value & 0xff;
1188 int shift = (kcontrol->private_value >> 8) & 0xff;
1189 int mask = (kcontrol->private_value >> 16) & 0xff;
1190 int invert = (kcontrol->private_value >> 24) & 0xff;
1191 unsigned char val;
1192
1193 val = (ucontrol->value.integer.value[0] & mask);
1194 if (invert)
1195 val = mask - val;
1196 mask <<= shift;
1197 val <<= shift;
1198 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1199}
1200
1201#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1202{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1203 .info = snd_es18xx_info_double, \
1204 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1205 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1206
Takashi Iwai8047c912005-11-17 14:41:22 +01001207static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 int mask = (kcontrol->private_value >> 24) & 0xff;
1210
1211 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1212 uinfo->count = 2;
1213 uinfo->value.integer.min = 0;
1214 uinfo->value.integer.max = mask;
1215 return 0;
1216}
1217
Takashi Iwai8047c912005-11-17 14:41:22 +01001218static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Takashi Iwai8047c912005-11-17 14:41:22 +01001220 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int left_reg = kcontrol->private_value & 0xff;
1222 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1223 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1224 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1225 int mask = (kcontrol->private_value >> 24) & 0xff;
1226 int invert = (kcontrol->private_value >> 22) & 1;
1227 unsigned char left, right;
1228
1229 left = snd_es18xx_reg_read(chip, left_reg);
1230 if (left_reg != right_reg)
1231 right = snd_es18xx_reg_read(chip, right_reg);
1232 else
1233 right = left;
1234 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1235 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1236 if (invert) {
1237 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1238 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1239 }
1240 return 0;
1241}
1242
Takashi Iwai8047c912005-11-17 14:41:22 +01001243static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Takashi Iwai8047c912005-11-17 14:41:22 +01001245 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int left_reg = kcontrol->private_value & 0xff;
1247 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1248 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1249 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1250 int mask = (kcontrol->private_value >> 24) & 0xff;
1251 int invert = (kcontrol->private_value >> 22) & 1;
1252 int change;
1253 unsigned char val1, val2, mask1, mask2;
1254
1255 val1 = ucontrol->value.integer.value[0] & mask;
1256 val2 = ucontrol->value.integer.value[1] & mask;
1257 if (invert) {
1258 val1 = mask - val1;
1259 val2 = mask - val2;
1260 }
1261 val1 <<= shift_left;
1262 val2 <<= shift_right;
1263 mask1 = mask << shift_left;
1264 mask2 = mask << shift_right;
1265 if (left_reg != right_reg) {
1266 change = 0;
1267 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1268 change = 1;
1269 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1270 change = 1;
1271 } else {
1272 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1273 val1 | val2) != (val1 | val2));
1274 }
1275 return change;
1276}
1277
Mark Salazarc1f6d412006-01-16 11:29:09 +01001278/* Mixer controls
1279 * These arrays contain setup data for mixer controls.
1280 *
1281 * The controls that are universal to all chipsets are fully initialized
1282 * here.
1283 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001284static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1286ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1287ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1288ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1289ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1291ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1293ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1296 .name = "Capture Source",
1297 .info = snd_es18xx_info_mux,
1298 .get = snd_es18xx_get_mux,
1299 .put = snd_es18xx_put_mux,
1300}
1301};
1302
Takashi Iwai8047c912005-11-17 14:41:22 +01001303static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1305ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1306ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1307ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1309ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1310};
1311
Mark Salazarc1f6d412006-01-16 11:29:09 +01001312/*
1313 * The chipset specific mixer controls
1314 */
1315static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1316 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1317
1318static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1319ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1320ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1321ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1322};
1323
Takashi Iwai8047c912005-11-17 14:41:22 +01001324static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1326};
1327
Takashi Iwai8047c912005-11-17 14:41:22 +01001328static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1330ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1331};
1332
Takashi Iwai8047c912005-11-17 14:41:22 +01001333static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1335{
1336 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1337 .name = "3D Control - Switch",
1338 .info = snd_es18xx_info_spatializer_enable,
1339 .get = snd_es18xx_get_spatializer_enable,
1340 .put = snd_es18xx_put_spatializer_enable,
1341}
1342};
1343
Takashi Iwai8047c912005-11-17 14:41:22 +01001344static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1346
Takashi Iwai8047c912005-11-17 14:41:22 +01001347static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1349
Takashi Iwai8047c912005-11-17 14:41:22 +01001350static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1353 .name = "Hardware Master Playback Volume",
1354 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1355 .info = snd_es18xx_info_hw_volume,
1356 .get = snd_es18xx_get_hw_volume,
1357},
1358{
1359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1360 .name = "Hardware Master Playback Switch",
1361 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1362 .info = snd_es18xx_info_hw_switch,
1363 .get = snd_es18xx_get_hw_switch,
1364},
1365ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1366};
1367
1368#if 0
Takashi Iwai8047c912005-11-17 14:41:22 +01001369static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 int data;
1372 unsigned long flags;
1373 spin_lock_irqsave(&chip->ctrl_lock, flags);
1374 outb(reg, chip->ctrl_port);
1375 data = inb(chip->ctrl_port + 1);
1376 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1377 return data;
1378}
1379#endif
1380
Takashi Iwai8047c912005-11-17 14:41:22 +01001381static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned char reg, unsigned char data)
1383{
1384 /* No need for spinlocks, this function is used only in
1385 otherwise protected init code */
1386 outb(reg, chip->ctrl_port);
1387 outb(data, chip->ctrl_port + 1);
1388#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001389 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#endif
1391}
1392
Takashi Iwai8047c912005-11-17 14:41:22 +01001393static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 int mask = 0;
1396
1397 /* enable extended mode */
1398 snd_es18xx_dsp_command(chip, 0xC6);
1399 /* Reset mixer registers */
1400 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1401
1402 /* Audio 1 DMA demand mode (4 bytes/request) */
1403 snd_es18xx_write(chip, 0xB9, 2);
1404 if (chip->caps & ES18XX_CONTROL) {
1405 /* Hardware volume IRQ */
1406 snd_es18xx_config_write(chip, 0x27, chip->irq);
1407 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1408 /* FM I/O */
1409 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1410 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1411 }
1412 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1413 /* MPU-401 I/O */
1414 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1415 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1416 /* MPU-401 IRQ */
1417 snd_es18xx_config_write(chip, 0x28, chip->irq);
1418 }
1419 /* Audio1 IRQ */
1420 snd_es18xx_config_write(chip, 0x70, chip->irq);
1421 /* Audio2 IRQ */
1422 snd_es18xx_config_write(chip, 0x72, chip->irq);
1423 /* Audio1 DMA */
1424 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1425 /* Audio2 DMA */
1426 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1427
1428 /* Enable Audio 1 IRQ */
1429 snd_es18xx_write(chip, 0xB1, 0x50);
1430 /* Enable Audio 2 IRQ */
1431 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1432 /* Enable Audio 1 DMA */
1433 snd_es18xx_write(chip, 0xB2, 0x50);
1434 /* Enable MPU and hardware volume interrupt */
1435 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1436 }
1437 else {
1438 int irqmask, dma1mask, dma2mask;
1439 switch (chip->irq) {
1440 case 2:
1441 case 9:
1442 irqmask = 0;
1443 break;
1444 case 5:
1445 irqmask = 1;
1446 break;
1447 case 7:
1448 irqmask = 2;
1449 break;
1450 case 10:
1451 irqmask = 3;
1452 break;
1453 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001454 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENODEV;
1456 }
1457 switch (chip->dma1) {
1458 case 0:
1459 dma1mask = 1;
1460 break;
1461 case 1:
1462 dma1mask = 2;
1463 break;
1464 case 3:
1465 dma1mask = 3;
1466 break;
1467 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001468 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -ENODEV;
1470 }
1471 switch (chip->dma2) {
1472 case 0:
1473 dma2mask = 0;
1474 break;
1475 case 1:
1476 dma2mask = 1;
1477 break;
1478 case 3:
1479 dma2mask = 2;
1480 break;
1481 case 5:
1482 dma2mask = 3;
1483 break;
1484 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001485 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -ENODEV;
1487 }
1488
1489 /* Enable and set Audio 1 IRQ */
1490 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1491 /* Enable and set Audio 1 DMA */
1492 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1493 /* Set Audio 2 DMA */
1494 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1495 /* Enable Audio 2 IRQ and DMA
1496 Set capture mixer input */
1497 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1498 /* Enable and set hardware volume interrupt */
1499 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1500 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1501 /* MPU401 share irq with audio
1502 Joystick enabled
1503 FM enabled */
1504 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1505 }
1506 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1507 }
1508 if (chip->caps & ES18XX_NEW_RATE) {
1509 /* Change behaviour of register A1
1510 4x oversampling
1511 2nd channel DAC asynchronous */
1512 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1513 }
1514 if (!(chip->caps & ES18XX_PCM2)) {
1515 /* Enable DMA FIFO */
1516 snd_es18xx_write(chip, 0xB7, 0x80);
1517 }
1518 if (chip->caps & ES18XX_SPATIALIZER) {
1519 /* Set spatializer parameters to recommended values */
1520 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1521 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1522 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1523 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1524 }
1525 /* Mute input source */
1526 if (chip->caps & ES18XX_MUTEREC)
1527 mask = 0x10;
1528 if (chip->caps & ES18XX_RECMIX)
1529 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1530 else {
1531 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1532 snd_es18xx_write(chip, 0xb4, 0x00);
1533 }
1534#ifndef AVOID_POPS
1535 /* Enable PCM output */
1536 snd_es18xx_dsp_command(chip, 0xD1);
1537#endif
1538
1539 return 0;
1540}
1541
Takashi Iwai8047c912005-11-17 14:41:22 +01001542static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 int hi,lo;
1545
1546 /* reset */
1547 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001548 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -ENODEV;
1550 }
1551
1552 snd_es18xx_dsp_command(chip, 0xe7);
1553 hi = snd_es18xx_dsp_get_byte(chip);
1554 if (hi < 0) {
1555 return hi;
1556 }
1557 lo = snd_es18xx_dsp_get_byte(chip);
1558 if ((lo & 0xf0) != 0x80) {
1559 return -ENODEV;
1560 }
1561 if (hi == 0x48) {
1562 chip->version = 0x488;
1563 return 0;
1564 }
1565 if (hi != 0x68) {
1566 return -ENODEV;
1567 }
1568 if ((lo & 0x0f) < 8) {
1569 chip->version = 0x688;
1570 return 0;
1571 }
1572
1573 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001574 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001576 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 lo = inb(chip->port + 0x05);
1578 if (hi != lo) {
1579 chip->version = hi << 8 | lo;
1580 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001581 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 chip->ctrl_port += inb(chip->port + 0x05);
1583
1584 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1585 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1586 return -EBUSY;
1587 }
1588
1589 return 0;
1590 }
1591
1592 /* If has Hardware volume */
1593 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1594 /* If has Audio2 */
1595 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1596 /* If has volume count */
1597 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1598 chip->version = 0x1887;
1599 } else {
1600 chip->version = 0x1888;
1601 }
1602 } else {
1603 chip->version = 0x1788;
1604 }
1605 }
1606 else
1607 chip->version = 0x1688;
1608 return 0;
1609}
1610
Takashi Iwai8047c912005-11-17 14:41:22 +01001611static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 if (snd_es18xx_identify(chip) < 0) {
1614 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1615 return -ENODEV;
1616 }
1617
1618 switch (chip->version) {
1619 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001620 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
1622 case 0x1869:
1623 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1624 break;
1625 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001626 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 case 0x1879:
1629 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1630 break;
1631 case 0x1887:
Mark Salazar14086772006-01-16 11:33:52 +01001632 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001635 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001638 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 chip->port, chip->version);
1640 return -ENODEV;
1641 }
1642
1643 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1644
1645 if (chip->dma1 == chip->dma2)
1646 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1647
1648 return snd_es18xx_initialize(chip);
1649}
1650
Takashi Iwai8047c912005-11-17 14:41:22 +01001651static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .open = snd_es18xx_playback_open,
1653 .close = snd_es18xx_playback_close,
1654 .ioctl = snd_pcm_lib_ioctl,
1655 .hw_params = snd_es18xx_playback_hw_params,
1656 .hw_free = snd_es18xx_pcm_hw_free,
1657 .prepare = snd_es18xx_playback_prepare,
1658 .trigger = snd_es18xx_playback_trigger,
1659 .pointer = snd_es18xx_playback_pointer,
1660};
1661
Takashi Iwai8047c912005-11-17 14:41:22 +01001662static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 .open = snd_es18xx_capture_open,
1664 .close = snd_es18xx_capture_close,
1665 .ioctl = snd_pcm_lib_ioctl,
1666 .hw_params = snd_es18xx_capture_hw_params,
1667 .hw_free = snd_es18xx_pcm_hw_free,
1668 .prepare = snd_es18xx_capture_prepare,
1669 .trigger = snd_es18xx_capture_trigger,
1670 .pointer = snd_es18xx_capture_pointer,
1671};
1672
Takashi Iwai8047c912005-11-17 14:41:22 +01001673static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai8047c912005-11-17 14:41:22 +01001675 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 char str[16];
1677 int err;
1678
1679 if (rpcm)
1680 *rpcm = NULL;
1681 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001682 if (chip->caps & ES18XX_PCM2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001684 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (err < 0)
1687 return err;
1688
1689 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1690 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1691
1692 /* global setup */
1693 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 pcm->info_flags = 0;
1695 if (chip->caps & ES18XX_DUPLEX_SAME)
1696 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1697 if (! (chip->caps & ES18XX_PCM2))
1698 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1699 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1700 chip->pcm = pcm;
1701
1702 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1703 snd_dma_isa_data(),
1704 64*1024,
1705 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1706
1707 if (rpcm)
1708 *rpcm = pcm;
1709 return 0;
1710}
1711
1712/* Power Management support functions */
1713#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001714static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001716 struct snd_audiodrive *acard = card->private_data;
1717 struct snd_es18xx *chip = acard->chip;
1718
1719 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 snd_pcm_suspend_all(chip->pcm);
1722
1723 /* power down */
1724 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1725 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1726 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1727 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1728
1729 return 0;
1730}
1731
Takashi Iwai8047c912005-11-17 14:41:22 +01001732static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001734 struct snd_audiodrive *acard = card->private_data;
1735 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1738 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1739
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001740 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return 0;
1742}
1743#endif /* CONFIG_PM */
1744
Takashi Iwai8047c912005-11-17 14:41:22 +01001745static int snd_es18xx_free(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001747 release_and_free_resource(chip->res_port);
1748 release_and_free_resource(chip->res_ctrl_port);
1749 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (chip->irq >= 0)
1751 free_irq(chip->irq, (void *) chip);
1752 if (chip->dma1 >= 0) {
1753 disable_dma(chip->dma1);
1754 free_dma(chip->dma1);
1755 }
1756 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1757 disable_dma(chip->dma2);
1758 free_dma(chip->dma2);
1759 }
1760 kfree(chip);
1761 return 0;
1762}
1763
Takashi Iwai8047c912005-11-17 14:41:22 +01001764static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwai8047c912005-11-17 14:41:22 +01001766 struct snd_es18xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return snd_es18xx_free(chip);
1768}
1769
Takashi Iwai8047c912005-11-17 14:41:22 +01001770static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 unsigned long port,
1772 unsigned long mpu_port,
1773 unsigned long fm_port,
1774 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001775 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai8047c912005-11-17 14:41:22 +01001777 struct snd_es18xx *chip;
1778 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 .dev_free = snd_es18xx_dev_free,
1780 };
1781 int err;
1782
1783 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001784 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (chip == NULL)
1786 return -ENOMEM;
1787 spin_lock_init(&chip->reg_lock);
1788 spin_lock_init(&chip->mixer_lock);
1789 spin_lock_init(&chip->ctrl_lock);
1790 chip->card = card;
1791 chip->port = port;
1792 chip->mpu_port = mpu_port;
1793 chip->fm_port = fm_port;
1794 chip->irq = -1;
1795 chip->dma1 = -1;
1796 chip->dma2 = -1;
1797 chip->audio2_vol = 0x00;
1798 chip->active = 0;
1799
1800 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1801 snd_es18xx_free(chip);
1802 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1803 return -EBUSY;
1804 }
1805
1806 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1807 snd_es18xx_free(chip);
1808 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1809 return -EBUSY;
1810 }
1811 chip->irq = irq;
1812
1813 if (request_dma(dma1, "ES18xx DMA 1")) {
1814 snd_es18xx_free(chip);
1815 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1816 return -EBUSY;
1817 }
1818 chip->dma1 = dma1;
1819
1820 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1821 snd_es18xx_free(chip);
1822 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1823 return -EBUSY;
1824 }
1825 chip->dma2 = dma2;
1826
1827 if (snd_es18xx_probe(chip) < 0) {
1828 snd_es18xx_free(chip);
1829 return -ENODEV;
1830 }
1831 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1832 snd_es18xx_free(chip);
1833 return err;
1834 }
1835 *rchip = chip;
1836 return 0;
1837}
1838
Takashi Iwai8047c912005-11-17 14:41:22 +01001839static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Takashi Iwai8047c912005-11-17 14:41:22 +01001841 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int err;
1843 unsigned int idx;
1844
1845 card = chip->card;
1846
1847 strcpy(card->mixername, chip->pcm->name);
1848
1849 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001850 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1852 if (chip->caps & ES18XX_HWV) {
1853 switch (idx) {
1854 case 0:
1855 chip->master_volume = kctl;
1856 kctl->private_free = snd_es18xx_hwv_free;
1857 break;
1858 case 1:
1859 chip->master_switch = kctl;
1860 kctl->private_free = snd_es18xx_hwv_free;
1861 break;
1862 }
1863 }
1864 if ((err = snd_ctl_add(card, kctl)) < 0)
1865 return err;
1866 }
1867 if (chip->caps & ES18XX_PCM2) {
1868 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1869 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1870 return err;
1871 }
1872 } else {
1873 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1874 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1875 return err;
1876 }
1877 }
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (chip->caps & ES18XX_RECMIX) {
1880 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1881 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1882 return err;
1883 }
1884 }
1885 switch (chip->version) {
1886 default:
1887 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1888 return err;
1889 break;
1890 case 0x1869:
1891 case 0x1879:
1892 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1893 return err;
1894 break;
1895 }
1896 if (chip->caps & ES18XX_SPATIALIZER) {
1897 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1898 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1899 return err;
1900 }
1901 }
1902 if (chip->caps & ES18XX_HWV) {
1903 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001904 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1906 if (idx == 0)
1907 chip->hw_volume = kctl;
1908 else
1909 chip->hw_switch = kctl;
1910 kctl->private_free = snd_es18xx_hwv_free;
1911 if ((err = snd_ctl_add(card, kctl)) < 0)
1912 return err;
1913
1914 }
1915 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001916 /* finish initializing other chipset specific controls
1917 */
1918 if (chip->version != 0x1868) {
1919 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1920 chip));
1921 if (err < 0)
1922 return err;
1923 }
1924 if (chip->version == 0x1869) {
1925 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1926 err = snd_ctl_add(card,
1927 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1928 chip));
1929 if (err < 0)
1930 return err;
1931 }
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
1936
1937/* Card level */
1938
1939MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1940MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1941MODULE_LICENSE("GPL");
1942MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1943 "{ESS,ES1869 PnP AudioDrive},"
1944 "{ESS,ES1878 PnP AudioDrive},"
1945 "{ESS,ES1879 PnP AudioDrive},"
1946 "{ESS,ES1887 PnP AudioDrive},"
1947 "{ESS,ES1888 PnP AudioDrive},"
1948 "{ESS,ES1887 AudioDrive},"
1949 "{ESS,ES1888 AudioDrive}}");
1950
1951static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1952static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1953static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1954#ifdef CONFIG_PNP
1955static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1956#endif
1957static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1958#ifndef CONFIG_PNP
1959static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1960#else
1961static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1962#endif
1963static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1964static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1965static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1966static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1967
1968module_param_array(index, int, NULL, 0444);
1969MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1970module_param_array(id, charp, NULL, 0444);
1971MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1972module_param_array(enable, bool, NULL, 0444);
1973MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1974#ifdef CONFIG_PNP
1975module_param_array(isapnp, bool, NULL, 0444);
1976MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1977#endif
1978module_param_array(port, long, NULL, 0444);
1979MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1980module_param_array(mpu_port, long, NULL, 0444);
1981MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1982module_param_array(fm_port, long, NULL, 0444);
1983MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1984module_param_array(irq, int, NULL, 0444);
1985MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1986module_param_array(dma1, int, NULL, 0444);
1987MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1988module_param_array(dma2, int, NULL, 0444);
1989MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1990
Clemens Ladischf7a92752005-12-07 09:13:42 +01001991static struct platform_device *platform_devices[SNDRV_CARDS];
Clemens Ladischf7a92752005-12-07 09:13:42 +01001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993#ifdef CONFIG_PNP
Takashi Iwai59b1b342006-01-04 15:06:44 +01001994static int pnp_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1997 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1998 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1999 /* ESS 1868 (integrated on Maxisound Cards) */
2000 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2001 /* ESS 1868 (integrated on Maxisound Cards) */
2002 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2003 /* ESS ES1869 Plug and Play AudioDrive */
2004 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2005 /* ESS 1869 */
2006 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2007 /* ESS 1878 */
2008 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2009 /* ESS 1879 */
2010 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2011 /* --- */
2012 { .id = "" } /* end */
2013};
2014
2015MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2016
2017static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2018 struct pnp_card_link *card,
2019 const struct pnp_card_device_id *id)
2020{
2021 struct pnp_dev *pdev;
2022 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
2023 int err;
2024
2025 if (!cfg)
2026 return -ENOMEM;
2027 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2028 if (acard->dev == NULL) {
2029 kfree(cfg);
2030 return -EBUSY;
2031 }
2032 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2033 if (acard->devc == NULL) {
2034 kfree(cfg);
2035 return -EBUSY;
2036 }
2037 /* Control port initialization */
2038 err = pnp_activate_dev(acard->devc);
2039 if (err < 0) {
2040 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2041 return -EAGAIN;
2042 }
2043 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
2044 /* PnP initialization */
2045 pdev = acard->dev;
2046 pnp_init_resource_table(cfg);
2047 if (port[dev] != SNDRV_AUTO_PORT)
2048 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
2049 if (fm_port[dev] != SNDRV_AUTO_PORT)
2050 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
2051 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2052 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
2053 if (dma1[dev] != SNDRV_AUTO_DMA)
2054 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
2055 if (dma2[dev] != SNDRV_AUTO_DMA)
2056 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
2057 if (irq[dev] != SNDRV_AUTO_IRQ)
2058 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
2059 err = pnp_manual_config_dev(pdev, cfg, 0);
2060 if (err < 0)
2061 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
2062 err = pnp_activate_dev(pdev);
2063 if (err < 0) {
2064 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2065 kfree(cfg);
2066 return -EBUSY;
2067 }
2068 /* ok. hack using Vendor-Defined Card-Level registers */
2069 /* skip csn and logdev initialization - already done in isapnp_configure */
2070 if (pnp_device_is_isapnp(pdev)) {
2071 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2072 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2073 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2074 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2075 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2076 isapnp_cfg_end();
2077 } else {
2078 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
2079 }
2080 port[dev] = pnp_port_start(pdev, 0);
2081 fm_port[dev] = pnp_port_start(pdev, 1);
2082 mpu_port[dev] = pnp_port_start(pdev, 2);
2083 dma1[dev] = pnp_dma(pdev, 0);
2084 dma2[dev] = pnp_dma(pdev, 1);
2085 irq[dev] = pnp_irq(pdev, 0);
2086 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2087 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2088 kfree(cfg);
2089 return 0;
2090}
2091#endif /* CONFIG_PNP */
2092
Takashi Iwai43bcd972005-09-05 17:19:20 +02002093#ifdef CONFIG_PNP
2094#define is_isapnp_selected(dev) isapnp[dev]
2095#else
2096#define is_isapnp_selected(dev) 0
2097#endif
2098
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002099static struct snd_card *snd_es18xx_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002101 return snd_card_new(index[dev], id[dev], THIS_MODULE,
2102 sizeof(struct snd_audiodrive));
2103}
2104
2105static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2106{
2107 struct snd_audiodrive *acard = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002108 struct snd_es18xx *chip;
2109 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 int err;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if ((err = snd_es18xx_new_device(card,
2113 port[dev],
2114 mpu_port[dev],
2115 fm_port[dev],
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002116 irq[dev], dma1[dev], dma2[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +02002117 &chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002118 return err;
2119 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002124 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2126 card->shortname,
2127 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002128 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 else
2130 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2131 card->shortname,
2132 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002133 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Takashi Iwai43bcd972005-09-05 17:19:20 +02002135 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002136 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002137
2138 if ((err = snd_es18xx_mixer(chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2142 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 +02002143 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002145 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 }
2149
2150 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2151 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2152 chip->mpu_port, 0,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002153 irq[dev], 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002154 &chip->rmidi)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002158 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002161static int __init snd_es18xx_nonpnp_probe1(int dev, struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002163 struct snd_card *card;
2164 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002166 card = snd_es18xx_card_new(dev);
2167 if (! card)
2168 return -ENOMEM;
2169 snd_card_set_dev(card, &devptr->dev);
2170 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2171 snd_card_free(card);
2172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002174 platform_set_drvdata(devptr, card);
2175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002178static int __init snd_es18xx_nonpnp_probe(struct platform_device *pdev)
2179{
2180 int dev = pdev->id;
2181 int err;
2182 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2183 static int possible_dmas[] = {1, 0, 3, 5, -1};
2184
2185 if (irq[dev] == SNDRV_AUTO_IRQ) {
2186 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2187 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2188 return -EBUSY;
2189 }
2190 }
2191 if (dma1[dev] == SNDRV_AUTO_DMA) {
2192 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2193 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2194 return -EBUSY;
2195 }
2196 }
2197 if (dma2[dev] == SNDRV_AUTO_DMA) {
2198 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2199 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2200 return -EBUSY;
2201 }
2202 }
2203
2204 if (port[dev] != SNDRV_AUTO_PORT) {
2205 return snd_es18xx_nonpnp_probe1(dev, pdev);
2206 } else {
2207 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2208 int i;
2209 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2210 port[dev] = possible_ports[i];
2211 err = snd_es18xx_nonpnp_probe1(dev, pdev);
2212 if (! err)
2213 return 0;
2214 }
2215 return err;
2216 }
2217}
2218
2219static int __devexit snd_es18xx_nonpnp_remove(struct platform_device *devptr)
2220{
2221 snd_card_free(platform_get_drvdata(devptr));
2222 platform_set_drvdata(devptr, NULL);
2223 return 0;
2224}
2225
2226#ifdef CONFIG_PM
2227static int snd_es18xx_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
2228{
2229 return snd_es18xx_suspend(platform_get_drvdata(dev), state);
2230}
2231
2232static int snd_es18xx_nonpnp_resume(struct platform_device *dev)
2233{
2234 return snd_es18xx_resume(platform_get_drvdata(dev));
2235}
2236#endif
2237
2238#define ES18XX_DRIVER "snd_es18xx"
2239
2240static struct platform_driver snd_es18xx_nonpnp_driver = {
2241 .probe = snd_es18xx_nonpnp_probe,
2242 .remove = __devexit_p(snd_es18xx_nonpnp_remove),
2243#ifdef CONFIG_PM
2244 .suspend = snd_es18xx_nonpnp_suspend,
2245 .resume = snd_es18xx_nonpnp_resume,
2246#endif
2247 .driver = {
2248 .name = ES18XX_DRIVER
2249 },
2250};
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253#ifdef CONFIG_PNP
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002254static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *pcard,
2255 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002258 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int res;
2260
2261 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002262 if (enable[dev] && isapnp[dev])
2263 break;
2264 }
2265 if (dev >= SNDRV_CARDS)
2266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002268 card = snd_es18xx_card_new(dev);
2269 if (! card)
2270 return -ENOMEM;
2271
2272 if ((res = snd_audiodrive_pnp(dev, card->private_data, pcard, pid)) < 0) {
2273 snd_card_free(card);
2274 return res;
2275 }
2276 snd_card_set_dev(card, &pcard->card->dev);
2277 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2278 snd_card_free(card);
2279 return res;
2280 }
2281
2282 pnp_set_card_drvdata(pcard, card);
2283 dev++;
2284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2288{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002289 snd_card_free(pnp_get_card_drvdata(pcard));
2290 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002293#ifdef CONFIG_PM
2294static int snd_audiodrive_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
2295{
2296 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2297}
2298
2299static int snd_audiodrive_pnp_resume(struct pnp_card_link *pcard)
2300{
2301 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2302}
2303
2304#endif
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static struct pnp_card_driver es18xx_pnpc_driver = {
2307 .flags = PNP_DRIVER_RES_DISABLE,
2308 .name = "es18xx",
2309 .id_table = snd_audiodrive_pnpids,
2310 .probe = snd_audiodrive_pnp_detect,
2311 .remove = __devexit_p(snd_audiodrive_pnp_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002312#ifdef CONFIG_PM
2313 .suspend = snd_audiodrive_pnp_suspend,
2314 .resume = snd_audiodrive_pnp_resume,
2315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316};
2317#endif /* CONFIG_PNP */
2318
Clemens Ladischf7a92752005-12-07 09:13:42 +01002319static void __init_or_module snd_es18xx_unregister_all(void)
2320{
2321 int i;
2322
Takashi Iwai59b1b342006-01-04 15:06:44 +01002323#ifdef CONFIG_PNP
Clemens Ladischf7a92752005-12-07 09:13:42 +01002324 if (pnp_registered)
2325 pnp_unregister_card_driver(&es18xx_pnpc_driver);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002326#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002327 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
2328 platform_device_unregister(platform_devices[i]);
2329 platform_driver_unregister(&snd_es18xx_nonpnp_driver);
2330}
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332static int __init alsa_card_es18xx_init(void)
2333{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002334 int i, err, cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002336 if ((err = platform_driver_register(&snd_es18xx_nonpnp_driver)) < 0)
2337 return err;
2338
2339 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
2340 struct platform_device *device;
2341 if (is_isapnp_selected(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 continue;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002343 device = platform_device_register_simple(ES18XX_DRIVER,
2344 i, NULL, 0);
2345 if (IS_ERR(device)) {
2346 err = PTR_ERR(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002347 goto errout;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002348 }
Clemens Ladischf7a92752005-12-07 09:13:42 +01002349 platform_devices[i] = device;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002350 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Takashi Iwai59b1b342006-01-04 15:06:44 +01002353#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 i = pnp_register_card_driver(&es18xx_pnpc_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002355 if (i >= 0) {
2356 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 cards += i;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002358 }
Takashi Iwai59b1b342006-01-04 15:06:44 +01002359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if(!cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362#ifdef MODULE
2363 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2364#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002365 err = -ENODEV;
2366 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 return 0;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002369
2370 errout:
2371 snd_es18xx_unregister_all();
2372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
2375static void __exit alsa_card_es18xx_exit(void)
2376{
Clemens Ladischf7a92752005-12-07 09:13:42 +01002377 snd_es18xx_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380module_init(alsa_card_es18xx_init)
2381module_exit(alsa_card_es18xx_exit)