blob: c20baafd9b7cfc0acfb02a943078da333ef294da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/* GENERAL NOTES:
23 *
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
Mark Salazar95b71292006-01-16 11:35:40 +010052 * - Zoom Video is implemented by sharing the FM DAC, thus the user can
53 * have either FM playback or Video playback but not both simultaneously.
54 * The Video Playback Switch mixer control toggles this choice.
55 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * BUGS:
57 *
58 * - There is a major trouble I noted:
59 *
60 * using both channel for playback stereo 16 bit samples at 44100 Hz
61 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
62 *
63 * The same happens using Audio1 for captureing.
64 *
65 * The Windows driver does not suffer of this (although it use Audio1
66 * only for captureing). I'm unable to discover why.
67 *
68 */
69
Mark Salazar95b71292006-01-16 11:35:40 +010070/*
71 * ES1879 NOTES:
72 * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
73 * seems to be effected (speaker_test plays a lower frequency). Can't find
74 * anything in the datasheet to account for this, so a Video Playback Switch
75 * control has been included to allow ZV to be enabled only when necessary.
76 * Then again on at least one test system the 0x71 bit 6 enable bit is not
77 * needed for ZV, so maybe the datasheet is entirely wrong here.
78 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010081#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010082#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/pnp.h>
84#include <linux/isapnp.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040085#include <linux/module.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070086#include <linux/delay.h>
87
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010088#include <asm/io.h>
89#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <sound/core.h>
91#include <sound/control.h>
92#include <sound/pcm.h>
93#include <sound/pcm_params.h>
94#include <sound/mpu401.h>
95#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define SNDRV_LEGACY_FIND_FREE_IRQ
97#define SNDRV_LEGACY_FIND_FREE_DMA
98#include <sound/initval.h>
99
100#define PFX "es18xx: "
101
Takashi Iwai8047c912005-11-17 14:41:22 +0100102struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long port; /* port of ESS chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long ctrl_port; /* Control port of ESS chip */
105 struct resource *res_port;
106 struct resource *res_mpu_port;
107 struct resource *res_ctrl_port;
108 int irq; /* IRQ number of ESS chip */
109 int dma1; /* DMA1 */
110 int dma2; /* DMA2 */
111 unsigned short version; /* version of ESS chip */
112 int caps; /* Chip capabilities */
113 unsigned short audio2_vol; /* volume level of audio2 */
114
115 unsigned short active; /* active channel mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int dma1_shift;
117 unsigned int dma2_shift;
118
Takashi Iwai8047c912005-11-17 14:41:22 +0100119 struct snd_pcm *pcm;
120 struct snd_pcm_substream *playback_a_substream;
121 struct snd_pcm_substream *capture_a_substream;
122 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Takashi Iwai8047c912005-11-17 14:41:22 +0100124 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai8047c912005-11-17 14:41:22 +0100126 struct snd_kcontrol *hw_volume;
127 struct snd_kcontrol *hw_switch;
128 struct snd_kcontrol *master_volume;
129 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 spinlock_t reg_lock;
132 spinlock_t mixer_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#ifdef CONFIG_PM
134 unsigned char pm_reg;
135#endif
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100136#ifdef CONFIG_PNP
137 struct pnp_dev *dev;
138 struct pnp_dev *devc;
139#endif
140};
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define AUDIO1_IRQ 0x01
143#define AUDIO2_IRQ 0x02
144#define HWV_IRQ 0x04
145#define MPU_IRQ 0x08
146
147#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
148#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
149#define ES18XX_RECMIX 0x0004 /* Has record mixer */
150#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
151#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
152#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
153#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100154#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
156#define ES18XX_I2S 0x0200 /* I2S mixer control */
157#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
158#define ES18XX_CONTROL 0x0800 /* Has control ports */
159
160/* Power Management */
161#define ES18XX_PM 0x07
162#define ES18XX_PM_GPO0 0x01
163#define ES18XX_PM_GPO1 0x02
164#define ES18XX_PM_PDR 0x04
165#define ES18XX_PM_ANA 0x08
166#define ES18XX_PM_FM 0x020
167#define ES18XX_PM_SUS 0x080
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* Lowlevel */
170
171#define DAC1 0x01
172#define ADC1 0x02
173#define DAC2 0x04
174#define MILLISECOND 10000
175
Takashi Iwai8047c912005-11-17 14:41:22 +0100176static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int i;
179
180 for(i = MILLISECOND; i; i--)
181 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
182 outb(val, chip->port + 0x0C);
183 return 0;
184 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200185 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -EINVAL;
187}
188
Takashi Iwai8047c912005-11-17 14:41:22 +0100189static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int i;
192
193 for(i = MILLISECOND/10; i; i--)
194 if (inb(chip->port + 0x0C) & 0x40)
195 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200196 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
197 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -ENODEV;
199}
200
201#undef REG_DEBUG
202
Takashi Iwai8047c912005-11-17 14:41:22 +0100203static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned char reg, unsigned char data)
205{
206 unsigned long flags;
207 int ret;
208
209 spin_lock_irqsave(&chip->reg_lock, flags);
210 ret = snd_es18xx_dsp_command(chip, reg);
211 if (ret < 0)
212 goto end;
213 ret = snd_es18xx_dsp_command(chip, data);
214 end:
215 spin_unlock_irqrestore(&chip->reg_lock, flags);
216#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200217 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219 return ret;
220}
221
Takashi Iwai8047c912005-11-17 14:41:22 +0100222static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 unsigned long flags;
225 int ret, data;
226 spin_lock_irqsave(&chip->reg_lock, flags);
227 ret = snd_es18xx_dsp_command(chip, 0xC0);
228 if (ret < 0)
229 goto end;
230 ret = snd_es18xx_dsp_command(chip, reg);
231 if (ret < 0)
232 goto end;
233 data = snd_es18xx_dsp_get_byte(chip);
234 ret = data;
235#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200236 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#endif
238 end:
239 spin_unlock_irqrestore(&chip->reg_lock, flags);
240 return ret;
241}
242
243/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100244static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned char mask, unsigned char val)
246{
247 int ret;
248 unsigned char old, new, oval;
249 unsigned long flags;
250 spin_lock_irqsave(&chip->reg_lock, flags);
251 ret = snd_es18xx_dsp_command(chip, 0xC0);
252 if (ret < 0)
253 goto end;
254 ret = snd_es18xx_dsp_command(chip, reg);
255 if (ret < 0)
256 goto end;
257 ret = snd_es18xx_dsp_get_byte(chip);
258 if (ret < 0) {
259 goto end;
260 }
261 old = ret;
262 oval = old & mask;
263 if (val != oval) {
264 ret = snd_es18xx_dsp_command(chip, reg);
265 if (ret < 0)
266 goto end;
267 new = (old & ~mask) | (val & mask);
268 ret = snd_es18xx_dsp_command(chip, new);
269 if (ret < 0)
270 goto end;
271#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200272 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
273 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
275 }
276 ret = oval;
277 end:
278 spin_unlock_irqrestore(&chip->reg_lock, flags);
279 return ret;
280}
281
Takashi Iwai8047c912005-11-17 14:41:22 +0100282static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned char reg, unsigned char data)
284{
285 unsigned long flags;
286 spin_lock_irqsave(&chip->mixer_lock, flags);
287 outb(reg, chip->port + 0x04);
288 outb(data, chip->port + 0x05);
289 spin_unlock_irqrestore(&chip->mixer_lock, flags);
290#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200291 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#endif
293}
294
Takashi Iwai8047c912005-11-17 14:41:22 +0100295static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 unsigned long flags;
298 int data;
299 spin_lock_irqsave(&chip->mixer_lock, flags);
300 outb(reg, chip->port + 0x04);
301 data = inb(chip->port + 0x05);
302 spin_unlock_irqrestore(&chip->mixer_lock, flags);
303#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200304 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
306 return data;
307}
308
309/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100310static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned char mask, unsigned char val)
312{
313 unsigned char old, new, oval;
314 unsigned long flags;
315 spin_lock_irqsave(&chip->mixer_lock, flags);
316 outb(reg, chip->port + 0x04);
317 old = inb(chip->port + 0x05);
318 oval = old & mask;
319 if (val != oval) {
320 new = (old & ~mask) | (val & mask);
321 outb(new, chip->port + 0x05);
322#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200323 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
324 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#endif
326 }
327 spin_unlock_irqrestore(&chip->mixer_lock, flags);
328 return oval;
329}
330
Takashi Iwai8047c912005-11-17 14:41:22 +0100331static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned char mask)
333{
334 int old, expected, new;
335 unsigned long flags;
336 spin_lock_irqsave(&chip->mixer_lock, flags);
337 outb(reg, chip->port + 0x04);
338 old = inb(chip->port + 0x05);
339 expected = old ^ mask;
340 outb(expected, chip->port + 0x05);
341 new = inb(chip->port + 0x05);
342 spin_unlock_irqrestore(&chip->mixer_lock, flags);
343#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200344 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
345 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347 return expected == new;
348}
349
350
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100351static int __devinit snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 int i;
354 outb(0x03, chip->port + 0x06);
355 inb(chip->port + 0x06);
356 outb(0x00, chip->port + 0x06);
357 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
358 if (inb(chip->port + 0x0A) != 0xAA)
359 return -1;
360 return 0;
361}
362
Takashi Iwai8047c912005-11-17 14:41:22 +0100363static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 outb(0x02, chip->port + 0x06);
366 inb(chip->port + 0x06);
367 outb(0x00, chip->port + 0x06);
368 return 0;
369}
370
Takashi Iwai8047c912005-11-17 14:41:22 +0100371static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 {
373 .num = 793800,
374 .den_min = 1,
375 .den_max = 128,
376 .den_step = 1,
377 },
378 {
379 .num = 768000,
380 .den_min = 1,
381 .den_max = 128,
382 .den_step = 1,
383 }
384};
385
Takashi Iwai8047c912005-11-17 14:41:22 +0100386static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .nrats = 2,
388 .rats = new_clocks,
389};
390
Takashi Iwai8047c912005-11-17 14:41:22 +0100391static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 {
393 .num = 795444,
394 .den_min = 1,
395 .den_max = 128,
396 .den_step = 1,
397 },
398 {
399 .num = 397722,
400 .den_min = 1,
401 .den_max = 128,
402 .den_step = 1,
403 }
404};
405
Takashi Iwai8047c912005-11-17 14:41:22 +0100406static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .nrats = 2,
408 .rats = old_clocks,
409};
410
411
Takashi Iwai8047c912005-11-17 14:41:22 +0100412static void snd_es18xx_rate_set(struct snd_es18xx *chip,
413 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int mode)
415{
416 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100417 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (chip->caps & ES18XX_NEW_RATE) {
419 if (runtime->rate_num == new_clocks[0].num)
420 bits = 128 - runtime->rate_den;
421 else
422 bits = 256 - runtime->rate_den;
423 } else {
424 if (runtime->rate_num == old_clocks[0].num)
425 bits = 256 - runtime->rate_den;
426 else
427 bits = 128 - runtime->rate_den;
428 }
429
430 /* set filter register */
431 div0 = 256 - 7160000*20/(8*82*runtime->rate);
432
433 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
434 snd_es18xx_mixer_write(chip, 0x70, bits);
435 /*
436 * Comment from kernel oss driver:
437 * FKS: fascinating: 0x72 doesn't seem to work.
438 */
439 snd_es18xx_write(chip, 0xA2, div0);
440 snd_es18xx_mixer_write(chip, 0x72, div0);
441 } else {
442 snd_es18xx_write(chip, 0xA1, bits);
443 snd_es18xx_write(chip, 0xA2, div0);
444 }
445}
446
Takashi Iwai8047c912005-11-17 14:41:22 +0100447static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai8047c912005-11-17 14:41:22 +0100450 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int shift, err;
452
453 shift = 0;
454 if (params_channels(hw_params) == 2)
455 shift++;
456 if (snd_pcm_format_width(params_format(hw_params)) == 16)
457 shift++;
458
459 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
460 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
461 (chip->capture_a_substream) &&
462 params_channels(hw_params) != 1) {
463 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
464 return -EBUSY;
465 }
466 chip->dma2_shift = shift;
467 } else {
468 chip->dma1_shift = shift;
469 }
470 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
471 return err;
472 return 0;
473}
474
Takashi Iwai8047c912005-11-17 14:41:22 +0100475static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 return snd_pcm_lib_free_pages(substream);
478}
479
Takashi Iwai8047c912005-11-17 14:41:22 +0100480static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
481 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai8047c912005-11-17 14:41:22 +0100483 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
485 unsigned int count = snd_pcm_lib_period_bytes(substream);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 snd_es18xx_rate_set(chip, substream, DAC2);
488
489 /* Transfer Count Reload */
490 count = 0x10000 - count;
491 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
492 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
493
494 /* Set format */
495 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
496 ((runtime->channels == 1) ? 0x00 : 0x02) |
497 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
498 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
499
500 /* Set DMA controller */
501 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
502
503 return 0;
504}
505
Takashi Iwai8047c912005-11-17 14:41:22 +0100506static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
507 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int cmd)
509{
510 switch (cmd) {
511 case SNDRV_PCM_TRIGGER_START:
512 case SNDRV_PCM_TRIGGER_RESUME:
513 if (chip->active & DAC2)
514 return 0;
515 chip->active |= DAC2;
516 /* Start DMA */
517 if (chip->dma2 >= 4)
518 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
519 else
520 snd_es18xx_mixer_write(chip, 0x78, 0x93);
521#ifdef AVOID_POPS
522 /* Avoid pops */
523 udelay(100000);
524 if (chip->caps & ES18XX_PCM2)
525 /* Restore Audio 2 volume */
526 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
527 else
528 /* Enable PCM output */
529 snd_es18xx_dsp_command(chip, 0xD1);
530#endif
531 break;
532 case SNDRV_PCM_TRIGGER_STOP:
533 case SNDRV_PCM_TRIGGER_SUSPEND:
534 if (!(chip->active & DAC2))
535 return 0;
536 chip->active &= ~DAC2;
537 /* Stop DMA */
538 snd_es18xx_mixer_write(chip, 0x78, 0x00);
539#ifdef AVOID_POPS
540 udelay(25000);
541 if (chip->caps & ES18XX_PCM2)
542 /* Set Audio 2 volume to 0 */
543 snd_es18xx_mixer_write(chip, 0x7C, 0);
544 else
545 /* Disable PCM output */
546 snd_es18xx_dsp_command(chip, 0xD3);
547#endif
548 break;
549 default:
550 return -EINVAL;
551 }
552
553 return 0;
554}
555
Takashi Iwai8047c912005-11-17 14:41:22 +0100556static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
557 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Takashi Iwai8047c912005-11-17 14:41:22 +0100559 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int shift, err;
561
562 shift = 0;
563 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
564 chip->playback_a_substream &&
565 params_channels(hw_params) != 1) {
566 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
567 return -EBUSY;
568 }
569 if (params_channels(hw_params) == 2)
570 shift++;
571 if (snd_pcm_format_width(params_format(hw_params)) == 16)
572 shift++;
573 chip->dma1_shift = shift;
574 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
575 return err;
576 return 0;
577}
578
Takashi Iwai8047c912005-11-17 14:41:22 +0100579static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwai8047c912005-11-17 14:41:22 +0100581 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
582 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
584 unsigned int count = snd_pcm_lib_period_bytes(substream);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 snd_es18xx_reset_fifo(chip);
587
588 /* Set stereo/mono */
589 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
590
591 snd_es18xx_rate_set(chip, substream, ADC1);
592
593 /* Transfer Count Reload */
594 count = 0x10000 - count;
595 snd_es18xx_write(chip, 0xA4, count & 0xff);
596 snd_es18xx_write(chip, 0xA5, count >> 8);
597
598#ifdef AVOID_POPS
599 udelay(100000);
600#endif
601
602 /* Set format */
603 snd_es18xx_write(chip, 0xB7,
604 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
605 snd_es18xx_write(chip, 0xB7, 0x90 |
606 ((runtime->channels == 1) ? 0x40 : 0x08) |
607 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
608 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
609
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200610 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
612
613 return 0;
614}
615
Takashi Iwai8047c912005-11-17 14:41:22 +0100616static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int cmd)
618{
Takashi Iwai8047c912005-11-17 14:41:22 +0100619 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 switch (cmd) {
622 case SNDRV_PCM_TRIGGER_START:
623 case SNDRV_PCM_TRIGGER_RESUME:
624 if (chip->active & ADC1)
625 return 0;
626 chip->active |= ADC1;
627 /* Start DMA */
628 snd_es18xx_write(chip, 0xB8, 0x0f);
629 break;
630 case SNDRV_PCM_TRIGGER_STOP:
631 case SNDRV_PCM_TRIGGER_SUSPEND:
632 if (!(chip->active & ADC1))
633 return 0;
634 chip->active &= ~ADC1;
635 /* Stop DMA */
636 snd_es18xx_write(chip, 0xB8, 0x00);
637 break;
638 default:
639 return -EINVAL;
640 }
641
642 return 0;
643}
644
Takashi Iwai8047c912005-11-17 14:41:22 +0100645static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
646 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Takashi Iwai8047c912005-11-17 14:41:22 +0100648 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
650 unsigned int count = snd_pcm_lib_period_bytes(substream);
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 snd_es18xx_reset_fifo(chip);
653
654 /* Set stereo/mono */
655 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
656
657 snd_es18xx_rate_set(chip, substream, DAC1);
658
659 /* Transfer Count Reload */
660 count = 0x10000 - count;
661 snd_es18xx_write(chip, 0xA4, count & 0xff);
662 snd_es18xx_write(chip, 0xA5, count >> 8);
663
664 /* Set format */
665 snd_es18xx_write(chip, 0xB6,
666 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
667 snd_es18xx_write(chip, 0xB7,
668 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
669 snd_es18xx_write(chip, 0xB7, 0x90 |
670 (runtime->channels == 1 ? 0x40 : 0x08) |
671 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
672 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
673
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200674 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
676
677 return 0;
678}
679
Takashi Iwai8047c912005-11-17 14:41:22 +0100680static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
681 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int cmd)
683{
684 switch (cmd) {
685 case SNDRV_PCM_TRIGGER_START:
686 case SNDRV_PCM_TRIGGER_RESUME:
687 if (chip->active & DAC1)
688 return 0;
689 chip->active |= DAC1;
690 /* Start DMA */
691 snd_es18xx_write(chip, 0xB8, 0x05);
692#ifdef AVOID_POPS
693 /* Avoid pops */
694 udelay(100000);
695 /* Enable Audio 1 */
696 snd_es18xx_dsp_command(chip, 0xD1);
697#endif
698 break;
699 case SNDRV_PCM_TRIGGER_STOP:
700 case SNDRV_PCM_TRIGGER_SUSPEND:
701 if (!(chip->active & DAC1))
702 return 0;
703 chip->active &= ~DAC1;
704 /* Stop DMA */
705 snd_es18xx_write(chip, 0xB8, 0x00);
706#ifdef AVOID_POPS
707 /* Avoid pops */
708 udelay(25000);
709 /* Disable Audio 1 */
710 snd_es18xx_dsp_command(chip, 0xD3);
711#endif
712 break;
713 default:
714 return -EINVAL;
715 }
716
717 return 0;
718}
719
Takashi Iwai8047c912005-11-17 14:41:22 +0100720static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Takashi Iwai8047c912005-11-17 14:41:22 +0100722 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
724 return snd_es18xx_playback1_prepare(chip, substream);
725 else
726 return snd_es18xx_playback2_prepare(chip, substream);
727}
728
Takashi Iwai8047c912005-11-17 14:41:22 +0100729static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int cmd)
731{
Takashi Iwai8047c912005-11-17 14:41:22 +0100732 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
734 return snd_es18xx_playback1_trigger(chip, substream, cmd);
735 else
736 return snd_es18xx_playback2_trigger(chip, substream, cmd);
737}
738
David Howells7d12e782006-10-05 14:55:46 +0100739static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100741 struct snd_card *card = dev_id;
Krzysztof Heltb14f5de2009-10-25 11:10:01 +0100742 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 unsigned char status;
744
745 if (chip->caps & ES18XX_CONTROL) {
746 /* Read Interrupt status */
747 status = inb(chip->ctrl_port + 6);
748 } else {
749 /* Read Interrupt status */
750 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
751 }
752#if 0
753 else {
754 status = 0;
755 if (inb(chip->port + 0x0C) & 0x01)
756 status |= AUDIO1_IRQ;
757 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
758 status |= AUDIO2_IRQ;
759 if ((chip->caps & ES18XX_HWV) &&
760 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
761 status |= HWV_IRQ;
762 }
763#endif
764
765 /* Audio 1 & Audio 2 */
766 if (status & AUDIO2_IRQ) {
767 if (chip->active & DAC2)
768 snd_pcm_period_elapsed(chip->playback_a_substream);
769 /* ack interrupt */
770 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
771 }
772 if (status & AUDIO1_IRQ) {
773 /* ok.. capture is active */
774 if (chip->active & ADC1)
775 snd_pcm_period_elapsed(chip->capture_a_substream);
776 /* ok.. playback2 is active */
777 else if (chip->active & DAC1)
778 snd_pcm_period_elapsed(chip->playback_b_substream);
779 /* ack interrupt */
780 inb(chip->port + 0x0E);
781 }
782
783 /* MPU */
784 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100785 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* Hardware volume */
788 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100789 int split = 0;
790 if (chip->caps & ES18XX_HWV) {
791 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100792 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
793 &chip->hw_switch->id);
794 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
795 &chip->hw_volume->id);
Mark Salazar14086772006-01-16 11:33:52 +0100796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!split) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100798 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
799 &chip->master_switch->id);
800 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
801 &chip->master_volume->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 /* ack interrupt */
804 snd_es18xx_mixer_write(chip, 0x66, 0x00);
805 }
806 return IRQ_HANDLED;
807}
808
Takashi Iwai8047c912005-11-17 14:41:22 +0100809static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Takashi Iwai8047c912005-11-17 14:41:22 +0100811 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100812 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int pos;
814
815 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
816 if (!(chip->active & DAC2))
817 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100818 pos = snd_dma_pointer(chip->dma2, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return pos >> chip->dma2_shift;
820 } else {
821 if (!(chip->active & DAC1))
822 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100823 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return pos >> chip->dma1_shift;
825 }
826}
827
Takashi Iwai8047c912005-11-17 14:41:22 +0100828static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai8047c912005-11-17 14:41:22 +0100830 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100831 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int pos;
833
834 if (!(chip->active & ADC1))
835 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100836 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return pos >> chip->dma1_shift;
838}
839
Takashi Iwai8047c912005-11-17 14:41:22 +0100840static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
843 SNDRV_PCM_INFO_RESUME |
844 SNDRV_PCM_INFO_MMAP_VALID),
845 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
846 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
847 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
848 .rate_min = 4000,
849 .rate_max = 48000,
850 .channels_min = 1,
851 .channels_max = 2,
852 .buffer_bytes_max = 65536,
853 .period_bytes_min = 64,
854 .period_bytes_max = 65536,
855 .periods_min = 1,
856 .periods_max = 1024,
857 .fifo_size = 0,
858};
859
Takashi Iwai8047c912005-11-17 14:41:22 +0100860static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
863 SNDRV_PCM_INFO_RESUME |
864 SNDRV_PCM_INFO_MMAP_VALID),
865 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
866 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
867 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
868 .rate_min = 4000,
869 .rate_max = 48000,
870 .channels_min = 1,
871 .channels_max = 2,
872 .buffer_bytes_max = 65536,
873 .period_bytes_min = 64,
874 .period_bytes_max = 65536,
875 .periods_min = 1,
876 .periods_max = 1024,
877 .fifo_size = 0,
878};
879
Takashi Iwai8047c912005-11-17 14:41:22 +0100880static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai8047c912005-11-17 14:41:22 +0100882 struct snd_pcm_runtime *runtime = substream->runtime;
883 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
886 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
887 chip->capture_a_substream &&
888 chip->capture_a_substream->runtime->channels != 1)
889 return -EAGAIN;
890 chip->playback_a_substream = substream;
891 } else if (substream->number <= 1) {
892 if (chip->capture_a_substream)
893 return -EAGAIN;
894 chip->playback_b_substream = substream;
895 } else {
896 snd_BUG();
897 return -EINVAL;
898 }
899 substream->runtime->hw = snd_es18xx_playback;
900 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
901 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
902 return 0;
903}
904
Takashi Iwai8047c912005-11-17 14:41:22 +0100905static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Takashi Iwai8047c912005-11-17 14:41:22 +0100907 struct snd_pcm_runtime *runtime = substream->runtime;
908 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (chip->playback_b_substream)
911 return -EAGAIN;
912 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
913 chip->playback_a_substream &&
914 chip->playback_a_substream->runtime->channels != 1)
915 return -EAGAIN;
916 chip->capture_a_substream = substream;
917 substream->runtime->hw = snd_es18xx_capture;
918 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
919 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
920 return 0;
921}
922
Takashi Iwai8047c912005-11-17 14:41:22 +0100923static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai8047c912005-11-17 14:41:22 +0100925 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
928 chip->playback_a_substream = NULL;
929 else
930 chip->playback_b_substream = NULL;
931
932 snd_pcm_lib_free_pages(substream);
933 return 0;
934}
935
Takashi Iwai8047c912005-11-17 14:41:22 +0100936static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Takashi Iwai8047c912005-11-17 14:41:22 +0100938 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 chip->capture_a_substream = NULL;
941 snd_pcm_lib_free_pages(substream);
942 return 0;
943}
944
945/*
946 * MIXER part
947 */
948
Mark Salazarf4df2212006-01-16 11:31:14 +0100949/* Record source mux routines:
950 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
951 * bit table for the 4/5 source mux:
952 * reg 1C:
953 * b2 b1 b0 muxSource
954 * x 0 x microphone
955 * 0 1 x CD
956 * 1 1 0 line
957 * 1 1 1 mixer
958 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
959 * either the play mixer or the capture mixer.
960 *
961 * "map4Source" translates from source number to reg bit pattern
962 * "invMap4Source" translates from reg bit pattern to source number
963 */
964
Takashi Iwai8047c912005-11-17 14:41:22 +0100965static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Mark Salazarf4df2212006-01-16 11:31:14 +0100967 static char *texts5Source[5] = {
968 "Mic", "CD", "Line", "Master", "Mix"
969 };
970 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 "Mic", "Mic Master", "CD", "AOUT",
972 "Mic1", "Mix", "Line", "Master"
973 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100974 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
977 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100978 switch (chip->version) {
979 case 0x1868:
980 case 0x1878:
981 uinfo->value.enumerated.items = 4;
982 if (uinfo->value.enumerated.item > 3)
983 uinfo->value.enumerated.item = 3;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100984 strcpy(uinfo->value.enumerated.name,
985 texts5Source[uinfo->value.enumerated.item]);
Mark Salazarf4df2212006-01-16 11:31:14 +0100986 break;
987 case 0x1887:
988 case 0x1888:
989 uinfo->value.enumerated.items = 5;
990 if (uinfo->value.enumerated.item > 4)
991 uinfo->value.enumerated.item = 4;
992 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
993 break;
994 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
995 case 0x1879:
996 uinfo->value.enumerated.items = 8;
997 if (uinfo->value.enumerated.item > 7)
998 uinfo->value.enumerated.item = 7;
999 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1000 break;
1001 default:
1002 return -EINVAL;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
Takashi Iwai8047c912005-11-17 14:41:22 +01001007static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Mark Salazarf4df2212006-01-16 11:31:14 +01001009 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001010 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001011 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1012 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1013 muxSource = invMap4Source[muxSource];
1014 if (muxSource==3 &&
1015 (chip->version == 0x1887 || chip->version == 0x1888) &&
1016 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1017 )
1018 muxSource = 4;
1019 }
1020 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
1023
Takashi Iwai8047c912005-11-17 14:41:22 +01001024static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Mark Salazarf4df2212006-01-16 11:31:14 +01001026 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001027 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001029 unsigned char retVal = 0;
1030
1031 switch (chip->version) {
1032 /* 5 source chips */
1033 case 0x1887:
1034 case 0x1888:
1035 if (val > 4)
1036 return -EINVAL;
1037 if (val == 4) {
1038 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1039 val = 3;
1040 } else
1041 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1042 /* 4 source chips */
1043 case 0x1868:
1044 case 0x1878:
1045 if (val > 3)
1046 return -EINVAL;
1047 val = map4Source[val];
1048 break;
1049 /* 8 source chips */
1050 case 0x1869:
1051 case 0x1879:
1052 if (val > 7)
1053 return -EINVAL;
1054 break;
1055 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001057 }
1058 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001061#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Takashi Iwai8047c912005-11-17 14:41:22 +01001063static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Takashi Iwai8047c912005-11-17 14:41:22 +01001065 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1067 ucontrol->value.integer.value[0] = !!(val & 8);
1068 return 0;
1069}
1070
Takashi Iwai8047c912005-11-17 14:41:22 +01001071static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Takashi Iwai8047c912005-11-17 14:41:22 +01001073 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned char oval, nval;
1075 int change;
1076 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1077 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1078 change = nval != oval;
1079 if (change) {
1080 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1081 snd_es18xx_mixer_write(chip, 0x50, nval);
1082 }
1083 return change;
1084}
1085
Takashi Iwai8047c912005-11-17 14:41:22 +01001086static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1089 uinfo->count = 2;
1090 uinfo->value.integer.min = 0;
1091 uinfo->value.integer.max = 63;
1092 return 0;
1093}
1094
Takashi Iwai8047c912005-11-17 14:41:22 +01001095static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Takashi Iwai8047c912005-11-17 14:41:22 +01001097 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1099 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1100 return 0;
1101}
1102
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001103#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Takashi Iwai8047c912005-11-17 14:41:22 +01001105static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Takashi Iwai8047c912005-11-17 14:41:22 +01001107 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1109 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1110 return 0;
1111}
1112
Takashi Iwai8047c912005-11-17 14:41:22 +01001113static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai8047c912005-11-17 14:41:22 +01001115 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 chip->master_volume = NULL;
1117 chip->master_switch = NULL;
1118 chip->hw_volume = NULL;
1119 chip->hw_switch = NULL;
1120}
1121
Takashi Iwai8047c912005-11-17 14:41:22 +01001122static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 unsigned char mask, unsigned char val)
1124{
1125 if (reg < 0xa0)
1126 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1127 else
1128 return snd_es18xx_bits(chip, reg, mask, val);
1129}
1130
Takashi Iwai8047c912005-11-17 14:41:22 +01001131static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 if (reg < 0xa0)
1134 return snd_es18xx_mixer_read(chip, reg);
1135 else
1136 return snd_es18xx_read(chip, reg);
1137}
1138
1139#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1140{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1141 .info = snd_es18xx_info_single, \
1142 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1143 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1144
Takashi Iwai8047c912005-11-17 14:41:22 +01001145static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int mask = (kcontrol->private_value >> 16) & 0xff;
1148
1149 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1150 uinfo->count = 1;
1151 uinfo->value.integer.min = 0;
1152 uinfo->value.integer.max = mask;
1153 return 0;
1154}
1155
Takashi Iwai8047c912005-11-17 14:41:22 +01001156static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwai8047c912005-11-17 14:41:22 +01001158 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int reg = kcontrol->private_value & 0xff;
1160 int shift = (kcontrol->private_value >> 8) & 0xff;
1161 int mask = (kcontrol->private_value >> 16) & 0xff;
1162 int invert = (kcontrol->private_value >> 24) & 0xff;
1163 int val;
1164
1165 val = snd_es18xx_reg_read(chip, reg);
1166 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1167 if (invert)
1168 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1169 return 0;
1170}
1171
Takashi Iwai8047c912005-11-17 14:41:22 +01001172static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai8047c912005-11-17 14:41:22 +01001174 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int reg = kcontrol->private_value & 0xff;
1176 int shift = (kcontrol->private_value >> 8) & 0xff;
1177 int mask = (kcontrol->private_value >> 16) & 0xff;
1178 int invert = (kcontrol->private_value >> 24) & 0xff;
1179 unsigned char val;
1180
1181 val = (ucontrol->value.integer.value[0] & mask);
1182 if (invert)
1183 val = mask - val;
1184 mask <<= shift;
1185 val <<= shift;
1186 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1187}
1188
1189#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1190{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1191 .info = snd_es18xx_info_double, \
1192 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1193 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1194
Takashi Iwai8047c912005-11-17 14:41:22 +01001195static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int mask = (kcontrol->private_value >> 24) & 0xff;
1198
1199 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1200 uinfo->count = 2;
1201 uinfo->value.integer.min = 0;
1202 uinfo->value.integer.max = mask;
1203 return 0;
1204}
1205
Takashi Iwai8047c912005-11-17 14:41:22 +01001206static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Takashi Iwai8047c912005-11-17 14:41:22 +01001208 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int left_reg = kcontrol->private_value & 0xff;
1210 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1211 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1212 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1213 int mask = (kcontrol->private_value >> 24) & 0xff;
1214 int invert = (kcontrol->private_value >> 22) & 1;
1215 unsigned char left, right;
1216
1217 left = snd_es18xx_reg_read(chip, left_reg);
1218 if (left_reg != right_reg)
1219 right = snd_es18xx_reg_read(chip, right_reg);
1220 else
1221 right = left;
1222 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1223 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1224 if (invert) {
1225 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1226 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1227 }
1228 return 0;
1229}
1230
Takashi Iwai8047c912005-11-17 14:41:22 +01001231static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwai8047c912005-11-17 14:41:22 +01001233 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int left_reg = kcontrol->private_value & 0xff;
1235 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1236 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1237 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1238 int mask = (kcontrol->private_value >> 24) & 0xff;
1239 int invert = (kcontrol->private_value >> 22) & 1;
1240 int change;
1241 unsigned char val1, val2, mask1, mask2;
1242
1243 val1 = ucontrol->value.integer.value[0] & mask;
1244 val2 = ucontrol->value.integer.value[1] & mask;
1245 if (invert) {
1246 val1 = mask - val1;
1247 val2 = mask - val2;
1248 }
1249 val1 <<= shift_left;
1250 val2 <<= shift_right;
1251 mask1 = mask << shift_left;
1252 mask2 = mask << shift_right;
1253 if (left_reg != right_reg) {
1254 change = 0;
1255 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1256 change = 1;
1257 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1258 change = 1;
1259 } else {
1260 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1261 val1 | val2) != (val1 | val2));
1262 }
1263 return change;
1264}
1265
Mark Salazarc1f6d412006-01-16 11:29:09 +01001266/* Mixer controls
1267 * These arrays contain setup data for mixer controls.
1268 *
1269 * The controls that are universal to all chipsets are fully initialized
1270 * here.
1271 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001272static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1274ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1275ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1276ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1277ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1279ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1281ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1284 .name = "Capture Source",
1285 .info = snd_es18xx_info_mux,
1286 .get = snd_es18xx_get_mux,
1287 .put = snd_es18xx_put_mux,
1288}
1289};
1290
Takashi Iwai8047c912005-11-17 14:41:22 +01001291static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1293ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1294ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1295ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1297ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1298};
1299
Mark Salazarc1f6d412006-01-16 11:29:09 +01001300/*
1301 * The chipset specific mixer controls
1302 */
1303static struct snd_kcontrol_new snd_es18xx_opt_speaker =
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001304 ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001305
1306static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1307ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
Mark Salazar95b71292006-01-16 11:35:40 +01001308ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001309ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1310ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1311};
1312
Mark Salazar95b71292006-01-16 11:35:40 +01001313static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1314 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1315
1316static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1317ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1318ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1319ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1320};
1321
Takashi Iwai8047c912005-11-17 14:41:22 +01001322static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1324};
1325
Takashi Iwai8047c912005-11-17 14:41:22 +01001326static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1328ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1329};
1330
Takashi Iwai8047c912005-11-17 14:41:22 +01001331static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1333{
1334 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1335 .name = "3D Control - Switch",
1336 .info = snd_es18xx_info_spatializer_enable,
1337 .get = snd_es18xx_get_spatializer_enable,
1338 .put = snd_es18xx_put_spatializer_enable,
1339}
1340};
1341
Takashi Iwai8047c912005-11-17 14:41:22 +01001342static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1344
Takashi Iwai8047c912005-11-17 14:41:22 +01001345static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1347
Takashi Iwai8047c912005-11-17 14:41:22 +01001348static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1351 .name = "Hardware Master Playback Volume",
1352 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1353 .info = snd_es18xx_info_hw_volume,
1354 .get = snd_es18xx_get_hw_volume,
1355},
1356{
1357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1358 .name = "Hardware Master Playback Switch",
1359 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1360 .info = snd_es18xx_info_hw_switch,
1361 .get = snd_es18xx_get_hw_switch,
1362},
1363ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1364};
1365
Takashi Iwai8047c912005-11-17 14:41:22 +01001366static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int data;
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 outb(reg, chip->ctrl_port);
1371 data = inb(chip->ctrl_port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return data;
1373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Takashi Iwai8047c912005-11-17 14:41:22 +01001375static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned char reg, unsigned char data)
1377{
1378 /* No need for spinlocks, this function is used only in
1379 otherwise protected init code */
1380 outb(reg, chip->ctrl_port);
1381 outb(data, chip->ctrl_port + 1);
1382#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001383 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif
1385}
1386
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001387static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip,
1388 unsigned long mpu_port,
1389 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 int mask = 0;
1392
1393 /* enable extended mode */
1394 snd_es18xx_dsp_command(chip, 0xC6);
1395 /* Reset mixer registers */
1396 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1397
1398 /* Audio 1 DMA demand mode (4 bytes/request) */
1399 snd_es18xx_write(chip, 0xB9, 2);
1400 if (chip->caps & ES18XX_CONTROL) {
1401 /* Hardware volume IRQ */
1402 snd_es18xx_config_write(chip, 0x27, chip->irq);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001403 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* FM I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001405 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1406 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001408 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* MPU-401 I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001410 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1411 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /* MPU-401 IRQ */
1413 snd_es18xx_config_write(chip, 0x28, chip->irq);
1414 }
1415 /* Audio1 IRQ */
1416 snd_es18xx_config_write(chip, 0x70, chip->irq);
1417 /* Audio2 IRQ */
1418 snd_es18xx_config_write(chip, 0x72, chip->irq);
1419 /* Audio1 DMA */
1420 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1421 /* Audio2 DMA */
1422 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1423
1424 /* Enable Audio 1 IRQ */
1425 snd_es18xx_write(chip, 0xB1, 0x50);
1426 /* Enable Audio 2 IRQ */
1427 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1428 /* Enable Audio 1 DMA */
1429 snd_es18xx_write(chip, 0xB2, 0x50);
1430 /* Enable MPU and hardware volume interrupt */
1431 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001432 /* Enable ESS wavetable input */
1433 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 else {
1436 int irqmask, dma1mask, dma2mask;
1437 switch (chip->irq) {
1438 case 2:
1439 case 9:
1440 irqmask = 0;
1441 break;
1442 case 5:
1443 irqmask = 1;
1444 break;
1445 case 7:
1446 irqmask = 2;
1447 break;
1448 case 10:
1449 irqmask = 3;
1450 break;
1451 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001452 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -ENODEV;
1454 }
1455 switch (chip->dma1) {
1456 case 0:
1457 dma1mask = 1;
1458 break;
1459 case 1:
1460 dma1mask = 2;
1461 break;
1462 case 3:
1463 dma1mask = 3;
1464 break;
1465 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001466 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -ENODEV;
1468 }
1469 switch (chip->dma2) {
1470 case 0:
1471 dma2mask = 0;
1472 break;
1473 case 1:
1474 dma2mask = 1;
1475 break;
1476 case 3:
1477 dma2mask = 2;
1478 break;
1479 case 5:
1480 dma2mask = 3;
1481 break;
1482 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001483 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -ENODEV;
1485 }
1486
1487 /* Enable and set Audio 1 IRQ */
1488 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1489 /* Enable and set Audio 1 DMA */
1490 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1491 /* Set Audio 2 DMA */
1492 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1493 /* Enable Audio 2 IRQ and DMA
1494 Set capture mixer input */
1495 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1496 /* Enable and set hardware volume interrupt */
1497 snd_es18xx_mixer_write(chip, 0x64, 0x06);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001498 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /* MPU401 share irq with audio
1500 Joystick enabled
1501 FM enabled */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001502 snd_es18xx_mixer_write(chip, 0x40,
1503 0x43 | (mpu_port & 0xf0) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1506 }
1507 if (chip->caps & ES18XX_NEW_RATE) {
1508 /* Change behaviour of register A1
1509 4x oversampling
1510 2nd channel DAC asynchronous */
1511 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1512 }
1513 if (!(chip->caps & ES18XX_PCM2)) {
1514 /* Enable DMA FIFO */
1515 snd_es18xx_write(chip, 0xB7, 0x80);
1516 }
1517 if (chip->caps & ES18XX_SPATIALIZER) {
1518 /* Set spatializer parameters to recommended values */
1519 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1520 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1521 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1522 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1523 }
Mark Salazar95b71292006-01-16 11:35:40 +01001524 /* Flip the "enable I2S" bits for those chipsets that need it */
1525 switch (chip->version) {
1526 case 0x1879:
1527 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1528 //so a Switch control has been added to toggle this 0x71 bit on/off:
1529 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1530 /* Note: we fall through on purpose here. */
1531 case 0x1878:
1532 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1533 break;
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* Mute input source */
1536 if (chip->caps & ES18XX_MUTEREC)
1537 mask = 0x10;
1538 if (chip->caps & ES18XX_RECMIX)
1539 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1540 else {
1541 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1542 snd_es18xx_write(chip, 0xb4, 0x00);
1543 }
1544#ifndef AVOID_POPS
1545 /* Enable PCM output */
1546 snd_es18xx_dsp_command(chip, 0xD1);
1547#endif
1548
1549 return 0;
1550}
1551
Takashi Iwai8047c912005-11-17 14:41:22 +01001552static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int hi,lo;
1555
1556 /* reset */
1557 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001558 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -ENODEV;
1560 }
1561
1562 snd_es18xx_dsp_command(chip, 0xe7);
1563 hi = snd_es18xx_dsp_get_byte(chip);
1564 if (hi < 0) {
1565 return hi;
1566 }
1567 lo = snd_es18xx_dsp_get_byte(chip);
1568 if ((lo & 0xf0) != 0x80) {
1569 return -ENODEV;
1570 }
1571 if (hi == 0x48) {
1572 chip->version = 0x488;
1573 return 0;
1574 }
1575 if (hi != 0x68) {
1576 return -ENODEV;
1577 }
1578 if ((lo & 0x0f) < 8) {
1579 chip->version = 0x688;
1580 return 0;
1581 }
1582
1583 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001584 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001586 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 lo = inb(chip->port + 0x05);
1588 if (hi != lo) {
1589 chip->version = hi << 8 | lo;
1590 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001591 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 chip->ctrl_port += inb(chip->port + 0x05);
1593
1594 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1595 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1596 return -EBUSY;
1597 }
1598
1599 return 0;
1600 }
1601
1602 /* If has Hardware volume */
1603 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1604 /* If has Audio2 */
1605 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1606 /* If has volume count */
1607 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1608 chip->version = 0x1887;
1609 } else {
1610 chip->version = 0x1888;
1611 }
1612 } else {
1613 chip->version = 0x1788;
1614 }
1615 }
1616 else
1617 chip->version = 0x1688;
1618 return 0;
1619}
1620
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001621static int __devinit snd_es18xx_probe(struct snd_es18xx *chip,
1622 unsigned long mpu_port,
1623 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 if (snd_es18xx_identify(chip) < 0) {
1626 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1627 return -ENODEV;
1628 }
1629
1630 switch (chip->version) {
1631 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001632 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 case 0x1869:
1635 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1636 break;
1637 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001638 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 break;
1640 case 0x1879:
1641 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1642 break;
1643 case 0x1887:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001645 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001648 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 chip->port, chip->version);
1650 return -ENODEV;
1651 }
1652
1653 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1654
1655 if (chip->dma1 == chip->dma2)
1656 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1657
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001658 return snd_es18xx_initialize(chip, mpu_port, fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Takashi Iwai8047c912005-11-17 14:41:22 +01001661static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 .open = snd_es18xx_playback_open,
1663 .close = snd_es18xx_playback_close,
1664 .ioctl = snd_pcm_lib_ioctl,
1665 .hw_params = snd_es18xx_playback_hw_params,
1666 .hw_free = snd_es18xx_pcm_hw_free,
1667 .prepare = snd_es18xx_playback_prepare,
1668 .trigger = snd_es18xx_playback_trigger,
1669 .pointer = snd_es18xx_playback_pointer,
1670};
1671
Takashi Iwai8047c912005-11-17 14:41:22 +01001672static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 .open = snd_es18xx_capture_open,
1674 .close = snd_es18xx_capture_close,
1675 .ioctl = snd_pcm_lib_ioctl,
1676 .hw_params = snd_es18xx_capture_hw_params,
1677 .hw_free = snd_es18xx_pcm_hw_free,
1678 .prepare = snd_es18xx_capture_prepare,
1679 .trigger = snd_es18xx_capture_trigger,
1680 .pointer = snd_es18xx_capture_pointer,
1681};
1682
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001683static int __devinit snd_es18xx_pcm(struct snd_card *card, int device,
1684 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001686 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001687 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 char str[16];
1689 int err;
1690
1691 if (rpcm)
1692 *rpcm = NULL;
1693 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001694 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001695 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001696 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001697 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (err < 0)
1699 return err;
1700
1701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1702 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1703
1704 /* global setup */
1705 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 pcm->info_flags = 0;
1707 if (chip->caps & ES18XX_DUPLEX_SAME)
1708 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1709 if (! (chip->caps & ES18XX_PCM2))
1710 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1711 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1712 chip->pcm = pcm;
1713
1714 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1715 snd_dma_isa_data(),
1716 64*1024,
1717 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1718
1719 if (rpcm)
1720 *rpcm = pcm;
1721 return 0;
1722}
1723
1724/* Power Management support functions */
1725#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001726static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001728 struct snd_es18xx *chip = card->private_data;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001729
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001730 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 snd_pcm_suspend_all(chip->pcm);
1733
1734 /* power down */
1735 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1736 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1737 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1738 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1739
1740 return 0;
1741}
1742
Takashi Iwai8047c912005-11-17 14:41:22 +01001743static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001745 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1748 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1749
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001750 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753#endif /* CONFIG_PM */
1754
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001755static int snd_es18xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001757 struct snd_es18xx *chip = card->private_data;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001758
Takashi Iwaib1d57762005-10-10 11:56:31 +02001759 release_and_free_resource(chip->res_port);
1760 release_and_free_resource(chip->res_ctrl_port);
1761 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (chip->irq >= 0)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001763 free_irq(chip->irq, (void *) card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (chip->dma1 >= 0) {
1765 disable_dma(chip->dma1);
1766 free_dma(chip->dma1);
1767 }
1768 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1769 disable_dma(chip->dma2);
1770 free_dma(chip->dma2);
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773}
1774
Takashi Iwai8047c912005-11-17 14:41:22 +01001775static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001777 return snd_es18xx_free(device->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Takashi Iwai8047c912005-11-17 14:41:22 +01001780static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned long port,
1782 unsigned long mpu_port,
1783 unsigned long fm_port,
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001784 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001786 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001787 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .dev_free = snd_es18xx_dev_free,
1789 };
1790 int err;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 spin_lock_init(&chip->reg_lock);
1793 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 chip->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 chip->irq = -1;
1796 chip->dma1 = -1;
1797 chip->dma2 = -1;
1798 chip->audio2_vol = 0x00;
1799 chip->active = 0;
1800
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001801 chip->res_port = request_region(port, 16, "ES18xx");
1802 if (chip->res_port == NULL) {
1803 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1805 return -EBUSY;
1806 }
1807
Yong Zhang88e24c32011-09-22 16:59:20 +08001808 if (request_irq(irq, snd_es18xx_interrupt, 0, "ES18xx",
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001809 (void *) card)) {
1810 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1812 return -EBUSY;
1813 }
1814 chip->irq = irq;
1815
1816 if (request_dma(dma1, "ES18xx DMA 1")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001817 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1819 return -EBUSY;
1820 }
1821 chip->dma1 = dma1;
1822
1823 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001824 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1826 return -EBUSY;
1827 }
1828 chip->dma2 = dma2;
1829
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001830 if (snd_es18xx_probe(chip, mpu_port, fm_port) < 0) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001831 snd_es18xx_free(card);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001832 return -ENODEV;
1833 }
1834 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001835 if (err < 0) {
1836 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return err;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
1840}
1841
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001842static int __devinit snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001844 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int err;
1846 unsigned int idx;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 strcpy(card->mixername, chip->pcm->name);
1849
1850 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001851 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1853 if (chip->caps & ES18XX_HWV) {
1854 switch (idx) {
1855 case 0:
1856 chip->master_volume = kctl;
1857 kctl->private_free = snd_es18xx_hwv_free;
1858 break;
1859 case 1:
1860 chip->master_switch = kctl;
1861 kctl->private_free = snd_es18xx_hwv_free;
1862 break;
1863 }
1864 }
1865 if ((err = snd_ctl_add(card, kctl)) < 0)
1866 return err;
1867 }
1868 if (chip->caps & ES18XX_PCM2) {
1869 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1870 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1871 return err;
1872 }
1873 } else {
1874 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1875 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1876 return err;
1877 }
1878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (chip->caps & ES18XX_RECMIX) {
1881 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1882 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1883 return err;
1884 }
1885 }
1886 switch (chip->version) {
1887 default:
1888 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1889 return err;
1890 break;
1891 case 0x1869:
1892 case 0x1879:
1893 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1894 return err;
1895 break;
1896 }
1897 if (chip->caps & ES18XX_SPATIALIZER) {
1898 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1899 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1900 return err;
1901 }
1902 }
1903 if (chip->caps & ES18XX_HWV) {
1904 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001905 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1907 if (idx == 0)
1908 chip->hw_volume = kctl;
1909 else
1910 chip->hw_switch = kctl;
1911 kctl->private_free = snd_es18xx_hwv_free;
1912 if ((err = snd_ctl_add(card, kctl)) < 0)
1913 return err;
1914
1915 }
1916 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001917 /* finish initializing other chipset specific controls
1918 */
1919 if (chip->version != 0x1868) {
1920 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1921 chip));
1922 if (err < 0)
1923 return err;
1924 }
1925 if (chip->version == 0x1869) {
1926 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1927 err = snd_ctl_add(card,
1928 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1929 chip));
1930 if (err < 0)
1931 return err;
1932 }
Mark Salazar95b71292006-01-16 11:35:40 +01001933 } else if (chip->version == 0x1878) {
1934 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1935 chip));
1936 if (err < 0)
1937 return err;
1938 } else if (chip->version == 0x1879) {
1939 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1940 err = snd_ctl_add(card,
1941 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1942 chip));
1943 if (err < 0)
1944 return err;
1945 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return 0;
1948}
1949
1950
1951/* Card level */
1952
1953MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1954MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1955MODULE_LICENSE("GPL");
1956MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1957 "{ESS,ES1869 PnP AudioDrive},"
1958 "{ESS,ES1878 PnP AudioDrive},"
1959 "{ESS,ES1879 PnP AudioDrive},"
1960 "{ESS,ES1887 PnP AudioDrive},"
1961 "{ESS,ES1888 PnP AudioDrive},"
1962 "{ESS,ES1887 AudioDrive},"
1963 "{ESS,ES1888 AudioDrive}}");
1964
1965static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1966static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +10301967static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +10301969static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970#endif
1971static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1972#ifndef CONFIG_PNP
1973static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1974#else
1975static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1976#endif
1977static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1978static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1979static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1980static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1981
1982module_param_array(index, int, NULL, 0444);
1983MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1984module_param_array(id, charp, NULL, 0444);
1985MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1986module_param_array(enable, bool, NULL, 0444);
1987MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1988#ifdef CONFIG_PNP
1989module_param_array(isapnp, bool, NULL, 0444);
1990MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1991#endif
1992module_param_array(port, long, NULL, 0444);
1993MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1994module_param_array(mpu_port, long, NULL, 0444);
1995MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1996module_param_array(fm_port, long, NULL, 0444);
1997MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1998module_param_array(irq, int, NULL, 0444);
1999MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2000module_param_array(dma1, int, NULL, 0444);
2001MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2002module_param_array(dma2, int, NULL, 0444);
2003MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002006static int isa_registered;
2007static int pnp_registered;
2008static int pnpc_registered;
Ondrej Zary1c398552006-07-31 12:51:57 +02002009
2010static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2011 { .id = "ESS1869" },
Rene Herman4848ffe2007-08-01 23:50:21 +02002012 { .id = "ESS1879" },
Ondrej Zary1c398552006-07-31 12:51:57 +02002013 { .id = "" } /* end */
2014};
2015
2016MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2017
2018/* PnP main device initialization */
Rene Herman109c53f842007-11-30 17:59:25 +01002019static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c398552006-07-31 12:51:57 +02002020{
Rene Herman109c53f842007-11-30 17:59:25 +01002021 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c398552006-07-31 12:51:57 +02002022 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2023 return -EBUSY;
2024 }
2025 /* ok. hack using Vendor-Defined Card-Level registers */
2026 /* skip csn and logdev initialization - already done in isapnp_configure */
2027 if (pnp_device_is_isapnp(pdev)) {
2028 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2029 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2030 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2031 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2032 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2033 isapnp_cfg_end();
2034 }
2035 port[dev] = pnp_port_start(pdev, 0);
2036 fm_port[dev] = pnp_port_start(pdev, 1);
2037 mpu_port[dev] = pnp_port_start(pdev, 2);
2038 dma1[dev] = pnp_dma(pdev, 0);
2039 dma2[dev] = pnp_dma(pdev, 1);
2040 irq[dev] = pnp_irq(pdev, 0);
2041 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2042 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2043 return 0;
2044}
2045
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002046static int __devinit snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
Ondrej Zary1c398552006-07-31 12:51:57 +02002047 struct pnp_dev *pdev)
2048{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002049 chip->dev = pdev;
2050 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Ondrej Zary1c398552006-07-31 12:51:57 +02002051 return -EBUSY;
Ondrej Zary1c398552006-07-31 12:51:57 +02002052 return 0;
2053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2056 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2057 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2058 /* ESS 1868 (integrated on Maxisound Cards) */
2059 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2060 /* ESS 1868 (integrated on Maxisound Cards) */
2061 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2062 /* ESS ES1869 Plug and Play AudioDrive */
2063 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2064 /* ESS 1869 */
2065 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2066 /* ESS 1878 */
2067 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2068 /* ESS 1879 */
2069 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2070 /* --- */
2071 { .id = "" } /* end */
2072};
2073
2074MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2075
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002076static int __devinit snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct pnp_card_link *card,
2078 const struct pnp_card_device_id *id)
2079{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002080 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2081 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002083
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002084 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2085 if (chip->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* Control port initialization */
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002089 if (pnp_activate_dev(chip->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2091 return -EAGAIN;
2092 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002093 snd_printdd("pnp: port=0x%llx\n",
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002094 (unsigned long long)pnp_port_start(chip->devc, 0));
2095 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return 0;
2099}
2100#endif /* CONFIG_PNP */
2101
Takashi Iwai43bcd972005-09-05 17:19:20 +02002102#ifdef CONFIG_PNP
2103#define is_isapnp_selected(dev) isapnp[dev]
2104#else
2105#define is_isapnp_selected(dev) 0
2106#endif
2107
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002108static int snd_es18xx_card_new(int dev, struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002110 return snd_card_create(index[dev], id[dev], THIS_MODULE,
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002111 sizeof(struct snd_es18xx), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002112}
2113
2114static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2115{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002116 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002117 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int err;
2119
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002120 err = snd_es18xx_new_device(card,
2121 port[dev], mpu_port[dev], fm_port[dev],
2122 irq[dev], dma1[dev], dma2[dev]);
2123 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002129 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2131 card->shortname,
2132 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002133 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 else
2135 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2136 card->shortname,
2137 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002138 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002140 err = snd_es18xx_pcm(card, 0, NULL);
2141 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002142 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002143
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002144 err = snd_es18xx_mixer(card);
2145 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002149 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2150 OPL3_HW_OPL3, 0, &opl3) < 0) {
2151 snd_printk(KERN_WARNING PFX
2152 "opl3 not detected at 0x%lx\n",
2153 fm_port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002155 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2156 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159 }
2160
2161 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002162 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002163 mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2164 -1, &chip->rmidi);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002165 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002169 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002172static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2173{
2174 return enable[dev] && !is_isapnp_selected(dev);
2175}
2176
2177static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002179 struct snd_card *card;
2180 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002182 err = snd_es18xx_card_new(dev, &card);
2183 if (err < 0)
2184 return err;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002185 snd_card_set_dev(card, devptr);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002186 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2187 snd_card_free(card);
2188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002190 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002194static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002195{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002196 int err;
2197 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2198 static int possible_dmas[] = {1, 0, 3, 5, -1};
2199
2200 if (irq[dev] == SNDRV_AUTO_IRQ) {
2201 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2202 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2203 return -EBUSY;
2204 }
2205 }
2206 if (dma1[dev] == SNDRV_AUTO_DMA) {
2207 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2208 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2209 return -EBUSY;
2210 }
2211 }
2212 if (dma2[dev] == SNDRV_AUTO_DMA) {
2213 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2214 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2215 return -EBUSY;
2216 }
2217 }
2218
2219 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002220 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002221 } else {
2222 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2223 int i;
2224 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2225 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002226 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002227 if (! err)
2228 return 0;
2229 }
2230 return err;
2231 }
2232}
2233
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002234static int __devexit snd_es18xx_isa_remove(struct device *devptr,
2235 unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002236{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002237 snd_card_free(dev_get_drvdata(devptr));
2238 dev_set_drvdata(devptr, NULL);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002239 return 0;
2240}
2241
2242#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002243static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2244 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002245{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002246 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002247}
2248
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002249static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002250{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002251 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002252}
2253#endif
2254
Rene Herman83c51c02007-03-20 11:33:46 +01002255#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002256
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002257static struct isa_driver snd_es18xx_isa_driver = {
2258 .match = snd_es18xx_isa_match,
2259 .probe = snd_es18xx_isa_probe,
2260 .remove = __devexit_p(snd_es18xx_isa_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002261#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002262 .suspend = snd_es18xx_isa_suspend,
2263 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002264#endif
2265 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002266 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002267 },
2268};
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271#ifdef CONFIG_PNP
Ondrej Zary1c398552006-07-31 12:51:57 +02002272static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2273 const struct pnp_device_id *id)
2274{
2275 static int dev;
2276 int err;
2277 struct snd_card *card;
2278
2279 if (pnp_device_is_isapnp(pdev))
2280 return -ENOENT; /* we have another procedure - card */
2281 for (; dev < SNDRV_CARDS; dev++) {
2282 if (enable[dev] && isapnp[dev])
2283 break;
2284 }
2285 if (dev >= SNDRV_CARDS)
2286 return -ENODEV;
2287
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002288 err = snd_es18xx_card_new(dev, &card);
2289 if (err < 0)
2290 return err;
Ondrej Zary1c398552006-07-31 12:51:57 +02002291 if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2292 snd_card_free(card);
2293 return err;
2294 }
2295 snd_card_set_dev(card, &pdev->dev);
2296 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2297 snd_card_free(card);
2298 return err;
2299 }
2300 pnp_set_drvdata(pdev, card);
2301 dev++;
Ondrej Zary1c398552006-07-31 12:51:57 +02002302 return 0;
2303}
2304
2305static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2306{
2307 snd_card_free(pnp_get_drvdata(pdev));
2308 pnp_set_drvdata(pdev, NULL);
2309}
2310
2311#ifdef CONFIG_PM
2312static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2313{
2314 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2315}
2316static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2317{
2318 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2319}
2320#endif
2321
2322static struct pnp_driver es18xx_pnp_driver = {
2323 .name = "es18xx-pnpbios",
2324 .id_table = snd_audiodrive_pnpbiosids,
2325 .probe = snd_audiodrive_pnp_detect,
2326 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2327#ifdef CONFIG_PM
2328 .suspend = snd_audiodrive_pnp_suspend,
2329 .resume = snd_audiodrive_pnp_resume,
2330#endif
2331};
2332
2333static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002334 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002337 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int res;
2339
2340 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002341 if (enable[dev] && isapnp[dev])
2342 break;
2343 }
2344 if (dev >= SNDRV_CARDS)
2345 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002347 res = snd_es18xx_card_new(dev, &card);
2348 if (res < 0)
2349 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002350
Ondrej Zary1c398552006-07-31 12:51:57 +02002351 if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002352 snd_card_free(card);
2353 return res;
2354 }
2355 snd_card_set_dev(card, &pcard->card->dev);
2356 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2357 snd_card_free(card);
2358 return res;
2359 }
2360
2361 pnp_set_card_drvdata(pcard, card);
2362 dev++;
2363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Ondrej Zary1c398552006-07-31 12:51:57 +02002366static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002368 snd_card_free(pnp_get_card_drvdata(pcard));
2369 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002372#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002373static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002374{
2375 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2376}
2377
Ondrej Zary1c398552006-07-31 12:51:57 +02002378static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002379{
2380 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2381}
2382
2383#endif
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385static struct pnp_card_driver es18xx_pnpc_driver = {
2386 .flags = PNP_DRIVER_RES_DISABLE,
2387 .name = "es18xx",
2388 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c398552006-07-31 12:51:57 +02002389 .probe = snd_audiodrive_pnpc_detect,
2390 .remove = __devexit_p(snd_audiodrive_pnpc_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002391#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002392 .suspend = snd_audiodrive_pnpc_suspend,
2393 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395};
2396#endif /* CONFIG_PNP */
2397
2398static int __init alsa_card_es18xx_init(void)
2399{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002400 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002402 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002403#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002404 if (!err)
2405 isa_registered = 1;
2406
Ondrej Zary1c398552006-07-31 12:51:57 +02002407 err = pnp_register_driver(&es18xx_pnp_driver);
2408 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002409 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002410
Ondrej Zary1c398552006-07-31 12:51:57 +02002411 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2412 if (!err)
2413 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002414
2415 if (isa_registered || pnp_registered)
2416 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002417#endif
Rene Herman609d7692007-05-15 11:42:56 +02002418 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
2421static void __exit alsa_card_es18xx_exit(void)
2422{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002423#ifdef CONFIG_PNP
2424 if (pnpc_registered)
2425 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2426 if (pnp_registered)
2427 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002428 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002429#endif
Rene Herman609d7692007-05-15 11:42:56 +02002430 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433module_init(alsa_card_es18xx_init)
2434module_exit(alsa_card_es18xx_exit)