blob: 160752bc2e8e26cf5ec620441d1b6bdb32c17cd0 [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/slab.h>
84#include <linux/pnp.h>
85#include <linux/isapnp.h>
86#include <linux/moduleparam.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070087#include <linux/delay.h>
88
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010089#include <asm/io.h>
90#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <sound/core.h>
92#include <sound/control.h>
93#include <sound/pcm.h>
94#include <sound/pcm_params.h>
95#include <sound/mpu401.h>
96#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define SNDRV_LEGACY_FIND_FREE_IRQ
98#define SNDRV_LEGACY_FIND_FREE_DMA
99#include <sound/initval.h>
100
101#define PFX "es18xx: "
102
Takashi Iwai8047c912005-11-17 14:41:22 +0100103struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long port; /* port of ESS chip */
105 unsigned long mpu_port; /* MPU-401 port of ESS chip */
106 unsigned long fm_port; /* FM port */
107 unsigned long ctrl_port; /* Control port of ESS chip */
108 struct resource *res_port;
109 struct resource *res_mpu_port;
110 struct resource *res_ctrl_port;
111 int irq; /* IRQ number of ESS chip */
112 int dma1; /* DMA1 */
113 int dma2; /* DMA2 */
114 unsigned short version; /* version of ESS chip */
115 int caps; /* Chip capabilities */
116 unsigned short audio2_vol; /* volume level of audio2 */
117
118 unsigned short active; /* active channel mask */
119 unsigned int dma1_size;
120 unsigned int dma2_size;
121 unsigned int dma1_shift;
122 unsigned int dma2_shift;
123
Takashi Iwai8047c912005-11-17 14:41:22 +0100124 struct snd_pcm *pcm;
125 struct snd_pcm_substream *playback_a_substream;
126 struct snd_pcm_substream *capture_a_substream;
127 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Takashi Iwai8047c912005-11-17 14:41:22 +0100129 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai8047c912005-11-17 14:41:22 +0100131 struct snd_kcontrol *hw_volume;
132 struct snd_kcontrol *hw_switch;
133 struct snd_kcontrol *master_volume;
134 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 spinlock_t reg_lock;
137 spinlock_t mixer_lock;
138 spinlock_t ctrl_lock;
139#ifdef CONFIG_PM
140 unsigned char pm_reg;
141#endif
142};
143
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100144struct snd_audiodrive {
145 struct snd_es18xx *chip;
146#ifdef CONFIG_PNP
147 struct pnp_dev *dev;
148 struct pnp_dev *devc;
149#endif
150};
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define AUDIO1_IRQ 0x01
153#define AUDIO2_IRQ 0x02
154#define HWV_IRQ 0x04
155#define MPU_IRQ 0x08
156
157#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
158#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
159#define ES18XX_RECMIX 0x0004 /* Has record mixer */
160#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
161#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
162#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
163#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100164#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
166#define ES18XX_I2S 0x0200 /* I2S mixer control */
167#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
168#define ES18XX_CONTROL 0x0800 /* Has control ports */
169
170/* Power Management */
171#define ES18XX_PM 0x07
172#define ES18XX_PM_GPO0 0x01
173#define ES18XX_PM_GPO1 0x02
174#define ES18XX_PM_PDR 0x04
175#define ES18XX_PM_ANA 0x08
176#define ES18XX_PM_FM 0x020
177#define ES18XX_PM_SUS 0x080
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Lowlevel */
180
181#define DAC1 0x01
182#define ADC1 0x02
183#define DAC2 0x04
184#define MILLISECOND 10000
185
Takashi Iwai8047c912005-11-17 14:41:22 +0100186static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
189
190 for(i = MILLISECOND; i; i--)
191 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
192 outb(val, chip->port + 0x0C);
193 return 0;
194 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200195 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197}
198
Takashi Iwai8047c912005-11-17 14:41:22 +0100199static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int i;
202
203 for(i = MILLISECOND/10; i; i--)
204 if (inb(chip->port + 0x0C) & 0x40)
205 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200206 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
207 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENODEV;
209}
210
211#undef REG_DEBUG
212
Takashi Iwai8047c912005-11-17 14:41:22 +0100213static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned char reg, unsigned char data)
215{
216 unsigned long flags;
217 int ret;
218
219 spin_lock_irqsave(&chip->reg_lock, flags);
220 ret = snd_es18xx_dsp_command(chip, reg);
221 if (ret < 0)
222 goto end;
223 ret = snd_es18xx_dsp_command(chip, data);
224 end:
225 spin_unlock_irqrestore(&chip->reg_lock, flags);
226#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200227 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#endif
229 return ret;
230}
231
Takashi Iwai8047c912005-11-17 14:41:22 +0100232static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 unsigned long flags;
235 int ret, data;
236 spin_lock_irqsave(&chip->reg_lock, flags);
237 ret = snd_es18xx_dsp_command(chip, 0xC0);
238 if (ret < 0)
239 goto end;
240 ret = snd_es18xx_dsp_command(chip, reg);
241 if (ret < 0)
242 goto end;
243 data = snd_es18xx_dsp_get_byte(chip);
244 ret = data;
245#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200246 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
248 end:
249 spin_unlock_irqrestore(&chip->reg_lock, flags);
250 return ret;
251}
252
253/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100254static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned char mask, unsigned char val)
256{
257 int ret;
258 unsigned char old, new, oval;
259 unsigned long flags;
260 spin_lock_irqsave(&chip->reg_lock, flags);
261 ret = snd_es18xx_dsp_command(chip, 0xC0);
262 if (ret < 0)
263 goto end;
264 ret = snd_es18xx_dsp_command(chip, reg);
265 if (ret < 0)
266 goto end;
267 ret = snd_es18xx_dsp_get_byte(chip);
268 if (ret < 0) {
269 goto end;
270 }
271 old = ret;
272 oval = old & mask;
273 if (val != oval) {
274 ret = snd_es18xx_dsp_command(chip, reg);
275 if (ret < 0)
276 goto end;
277 new = (old & ~mask) | (val & mask);
278 ret = snd_es18xx_dsp_command(chip, new);
279 if (ret < 0)
280 goto end;
281#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200282 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
283 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285 }
286 ret = oval;
287 end:
288 spin_unlock_irqrestore(&chip->reg_lock, flags);
289 return ret;
290}
291
Takashi Iwai8047c912005-11-17 14:41:22 +0100292static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned char reg, unsigned char data)
294{
295 unsigned long flags;
296 spin_lock_irqsave(&chip->mixer_lock, flags);
297 outb(reg, chip->port + 0x04);
298 outb(data, chip->port + 0x05);
299 spin_unlock_irqrestore(&chip->mixer_lock, flags);
300#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200301 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303}
304
Takashi Iwai8047c912005-11-17 14:41:22 +0100305static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 unsigned long flags;
308 int data;
309 spin_lock_irqsave(&chip->mixer_lock, flags);
310 outb(reg, chip->port + 0x04);
311 data = inb(chip->port + 0x05);
312 spin_unlock_irqrestore(&chip->mixer_lock, flags);
313#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200314 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
316 return data;
317}
318
319/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100320static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned char mask, unsigned char val)
322{
323 unsigned char old, new, oval;
324 unsigned long flags;
325 spin_lock_irqsave(&chip->mixer_lock, flags);
326 outb(reg, chip->port + 0x04);
327 old = inb(chip->port + 0x05);
328 oval = old & mask;
329 if (val != oval) {
330 new = (old & ~mask) | (val & mask);
331 outb(new, chip->port + 0x05);
332#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200333 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
334 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
336 }
337 spin_unlock_irqrestore(&chip->mixer_lock, flags);
338 return oval;
339}
340
Takashi Iwai8047c912005-11-17 14:41:22 +0100341static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned char mask)
343{
344 int old, expected, new;
345 unsigned long flags;
346 spin_lock_irqsave(&chip->mixer_lock, flags);
347 outb(reg, chip->port + 0x04);
348 old = inb(chip->port + 0x05);
349 expected = old ^ mask;
350 outb(expected, chip->port + 0x05);
351 new = inb(chip->port + 0x05);
352 spin_unlock_irqrestore(&chip->mixer_lock, flags);
353#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200354 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
355 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#endif
357 return expected == new;
358}
359
360
Takashi Iwai8047c912005-11-17 14:41:22 +0100361static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int i;
364 outb(0x03, chip->port + 0x06);
365 inb(chip->port + 0x06);
366 outb(0x00, chip->port + 0x06);
367 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
368 if (inb(chip->port + 0x0A) != 0xAA)
369 return -1;
370 return 0;
371}
372
Takashi Iwai8047c912005-11-17 14:41:22 +0100373static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 outb(0x02, chip->port + 0x06);
376 inb(chip->port + 0x06);
377 outb(0x00, chip->port + 0x06);
378 return 0;
379}
380
Takashi Iwai8047c912005-11-17 14:41:22 +0100381static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 {
383 .num = 793800,
384 .den_min = 1,
385 .den_max = 128,
386 .den_step = 1,
387 },
388 {
389 .num = 768000,
390 .den_min = 1,
391 .den_max = 128,
392 .den_step = 1,
393 }
394};
395
Takashi Iwai8047c912005-11-17 14:41:22 +0100396static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .nrats = 2,
398 .rats = new_clocks,
399};
400
Takashi Iwai8047c912005-11-17 14:41:22 +0100401static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 {
403 .num = 795444,
404 .den_min = 1,
405 .den_max = 128,
406 .den_step = 1,
407 },
408 {
409 .num = 397722,
410 .den_min = 1,
411 .den_max = 128,
412 .den_step = 1,
413 }
414};
415
Takashi Iwai8047c912005-11-17 14:41:22 +0100416static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .nrats = 2,
418 .rats = old_clocks,
419};
420
421
Takashi Iwai8047c912005-11-17 14:41:22 +0100422static void snd_es18xx_rate_set(struct snd_es18xx *chip,
423 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int mode)
425{
426 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100427 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (chip->caps & ES18XX_NEW_RATE) {
429 if (runtime->rate_num == new_clocks[0].num)
430 bits = 128 - runtime->rate_den;
431 else
432 bits = 256 - runtime->rate_den;
433 } else {
434 if (runtime->rate_num == old_clocks[0].num)
435 bits = 256 - runtime->rate_den;
436 else
437 bits = 128 - runtime->rate_den;
438 }
439
440 /* set filter register */
441 div0 = 256 - 7160000*20/(8*82*runtime->rate);
442
443 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
444 snd_es18xx_mixer_write(chip, 0x70, bits);
445 /*
446 * Comment from kernel oss driver:
447 * FKS: fascinating: 0x72 doesn't seem to work.
448 */
449 snd_es18xx_write(chip, 0xA2, div0);
450 snd_es18xx_mixer_write(chip, 0x72, div0);
451 } else {
452 snd_es18xx_write(chip, 0xA1, bits);
453 snd_es18xx_write(chip, 0xA2, div0);
454 }
455}
456
Takashi Iwai8047c912005-11-17 14:41:22 +0100457static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
458 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai8047c912005-11-17 14:41:22 +0100460 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int shift, err;
462
463 shift = 0;
464 if (params_channels(hw_params) == 2)
465 shift++;
466 if (snd_pcm_format_width(params_format(hw_params)) == 16)
467 shift++;
468
469 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
470 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
471 (chip->capture_a_substream) &&
472 params_channels(hw_params) != 1) {
473 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
474 return -EBUSY;
475 }
476 chip->dma2_shift = shift;
477 } else {
478 chip->dma1_shift = shift;
479 }
480 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
481 return err;
482 return 0;
483}
484
Takashi Iwai8047c912005-11-17 14:41:22 +0100485static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 return snd_pcm_lib_free_pages(substream);
488}
489
Takashi Iwai8047c912005-11-17 14:41:22 +0100490static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
491 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai8047c912005-11-17 14:41:22 +0100493 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
495 unsigned int count = snd_pcm_lib_period_bytes(substream);
496
497 chip->dma2_size = size;
498
499 snd_es18xx_rate_set(chip, substream, DAC2);
500
501 /* Transfer Count Reload */
502 count = 0x10000 - count;
503 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
504 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
505
506 /* Set format */
507 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
508 ((runtime->channels == 1) ? 0x00 : 0x02) |
509 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
510 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
511
512 /* Set DMA controller */
513 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
514
515 return 0;
516}
517
Takashi Iwai8047c912005-11-17 14:41:22 +0100518static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
519 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int cmd)
521{
522 switch (cmd) {
523 case SNDRV_PCM_TRIGGER_START:
524 case SNDRV_PCM_TRIGGER_RESUME:
525 if (chip->active & DAC2)
526 return 0;
527 chip->active |= DAC2;
528 /* Start DMA */
529 if (chip->dma2 >= 4)
530 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
531 else
532 snd_es18xx_mixer_write(chip, 0x78, 0x93);
533#ifdef AVOID_POPS
534 /* Avoid pops */
535 udelay(100000);
536 if (chip->caps & ES18XX_PCM2)
537 /* Restore Audio 2 volume */
538 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
539 else
540 /* Enable PCM output */
541 snd_es18xx_dsp_command(chip, 0xD1);
542#endif
543 break;
544 case SNDRV_PCM_TRIGGER_STOP:
545 case SNDRV_PCM_TRIGGER_SUSPEND:
546 if (!(chip->active & DAC2))
547 return 0;
548 chip->active &= ~DAC2;
549 /* Stop DMA */
550 snd_es18xx_mixer_write(chip, 0x78, 0x00);
551#ifdef AVOID_POPS
552 udelay(25000);
553 if (chip->caps & ES18XX_PCM2)
554 /* Set Audio 2 volume to 0 */
555 snd_es18xx_mixer_write(chip, 0x7C, 0);
556 else
557 /* Disable PCM output */
558 snd_es18xx_dsp_command(chip, 0xD3);
559#endif
560 break;
561 default:
562 return -EINVAL;
563 }
564
565 return 0;
566}
567
Takashi Iwai8047c912005-11-17 14:41:22 +0100568static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
569 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai8047c912005-11-17 14:41:22 +0100571 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int shift, err;
573
574 shift = 0;
575 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
576 chip->playback_a_substream &&
577 params_channels(hw_params) != 1) {
578 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
579 return -EBUSY;
580 }
581 if (params_channels(hw_params) == 2)
582 shift++;
583 if (snd_pcm_format_width(params_format(hw_params)) == 16)
584 shift++;
585 chip->dma1_shift = shift;
586 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
587 return err;
588 return 0;
589}
590
Takashi Iwai8047c912005-11-17 14:41:22 +0100591static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai8047c912005-11-17 14:41:22 +0100593 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
594 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
596 unsigned int count = snd_pcm_lib_period_bytes(substream);
597
598 chip->dma1_size = size;
599
600 snd_es18xx_reset_fifo(chip);
601
602 /* Set stereo/mono */
603 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
604
605 snd_es18xx_rate_set(chip, substream, ADC1);
606
607 /* Transfer Count Reload */
608 count = 0x10000 - count;
609 snd_es18xx_write(chip, 0xA4, count & 0xff);
610 snd_es18xx_write(chip, 0xA5, count >> 8);
611
612#ifdef AVOID_POPS
613 udelay(100000);
614#endif
615
616 /* Set format */
617 snd_es18xx_write(chip, 0xB7,
618 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
619 snd_es18xx_write(chip, 0xB7, 0x90 |
620 ((runtime->channels == 1) ? 0x40 : 0x08) |
621 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
622 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
623
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200624 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
626
627 return 0;
628}
629
Takashi Iwai8047c912005-11-17 14:41:22 +0100630static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int cmd)
632{
Takashi Iwai8047c912005-11-17 14:41:22 +0100633 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 switch (cmd) {
636 case SNDRV_PCM_TRIGGER_START:
637 case SNDRV_PCM_TRIGGER_RESUME:
638 if (chip->active & ADC1)
639 return 0;
640 chip->active |= ADC1;
641 /* Start DMA */
642 snd_es18xx_write(chip, 0xB8, 0x0f);
643 break;
644 case SNDRV_PCM_TRIGGER_STOP:
645 case SNDRV_PCM_TRIGGER_SUSPEND:
646 if (!(chip->active & ADC1))
647 return 0;
648 chip->active &= ~ADC1;
649 /* Stop DMA */
650 snd_es18xx_write(chip, 0xB8, 0x00);
651 break;
652 default:
653 return -EINVAL;
654 }
655
656 return 0;
657}
658
Takashi Iwai8047c912005-11-17 14:41:22 +0100659static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
660 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai8047c912005-11-17 14:41:22 +0100662 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
664 unsigned int count = snd_pcm_lib_period_bytes(substream);
665
666 chip->dma1_size = size;
667
668 snd_es18xx_reset_fifo(chip);
669
670 /* Set stereo/mono */
671 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
672
673 snd_es18xx_rate_set(chip, substream, DAC1);
674
675 /* Transfer Count Reload */
676 count = 0x10000 - count;
677 snd_es18xx_write(chip, 0xA4, count & 0xff);
678 snd_es18xx_write(chip, 0xA5, count >> 8);
679
680 /* Set format */
681 snd_es18xx_write(chip, 0xB6,
682 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
683 snd_es18xx_write(chip, 0xB7,
684 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
685 snd_es18xx_write(chip, 0xB7, 0x90 |
686 (runtime->channels == 1 ? 0x40 : 0x08) |
687 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
688 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
689
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200690 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
692
693 return 0;
694}
695
Takashi Iwai8047c912005-11-17 14:41:22 +0100696static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
697 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int cmd)
699{
700 switch (cmd) {
701 case SNDRV_PCM_TRIGGER_START:
702 case SNDRV_PCM_TRIGGER_RESUME:
703 if (chip->active & DAC1)
704 return 0;
705 chip->active |= DAC1;
706 /* Start DMA */
707 snd_es18xx_write(chip, 0xB8, 0x05);
708#ifdef AVOID_POPS
709 /* Avoid pops */
710 udelay(100000);
711 /* Enable Audio 1 */
712 snd_es18xx_dsp_command(chip, 0xD1);
713#endif
714 break;
715 case SNDRV_PCM_TRIGGER_STOP:
716 case SNDRV_PCM_TRIGGER_SUSPEND:
717 if (!(chip->active & DAC1))
718 return 0;
719 chip->active &= ~DAC1;
720 /* Stop DMA */
721 snd_es18xx_write(chip, 0xB8, 0x00);
722#ifdef AVOID_POPS
723 /* Avoid pops */
724 udelay(25000);
725 /* Disable Audio 1 */
726 snd_es18xx_dsp_command(chip, 0xD3);
727#endif
728 break;
729 default:
730 return -EINVAL;
731 }
732
733 return 0;
734}
735
Takashi Iwai8047c912005-11-17 14:41:22 +0100736static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Takashi Iwai8047c912005-11-17 14:41:22 +0100738 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
740 return snd_es18xx_playback1_prepare(chip, substream);
741 else
742 return snd_es18xx_playback2_prepare(chip, substream);
743}
744
Takashi Iwai8047c912005-11-17 14:41:22 +0100745static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int cmd)
747{
Takashi Iwai8047c912005-11-17 14:41:22 +0100748 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
750 return snd_es18xx_playback1_trigger(chip, substream, cmd);
751 else
752 return snd_es18xx_playback2_trigger(chip, substream, cmd);
753}
754
David Howells7d12e782006-10-05 14:55:46 +0100755static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100757 struct snd_card *card = dev_id;
758 struct snd_audiodrive *acard = card->private_data;
759 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned char status;
761
762 if (chip->caps & ES18XX_CONTROL) {
763 /* Read Interrupt status */
764 status = inb(chip->ctrl_port + 6);
765 } else {
766 /* Read Interrupt status */
767 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
768 }
769#if 0
770 else {
771 status = 0;
772 if (inb(chip->port + 0x0C) & 0x01)
773 status |= AUDIO1_IRQ;
774 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
775 status |= AUDIO2_IRQ;
776 if ((chip->caps & ES18XX_HWV) &&
777 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
778 status |= HWV_IRQ;
779 }
780#endif
781
782 /* Audio 1 & Audio 2 */
783 if (status & AUDIO2_IRQ) {
784 if (chip->active & DAC2)
785 snd_pcm_period_elapsed(chip->playback_a_substream);
786 /* ack interrupt */
787 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
788 }
789 if (status & AUDIO1_IRQ) {
790 /* ok.. capture is active */
791 if (chip->active & ADC1)
792 snd_pcm_period_elapsed(chip->capture_a_substream);
793 /* ok.. playback2 is active */
794 else if (chip->active & DAC1)
795 snd_pcm_period_elapsed(chip->playback_b_substream);
796 /* ack interrupt */
797 inb(chip->port + 0x0E);
798 }
799
800 /* MPU */
801 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100802 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* Hardware volume */
805 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100806 int split = 0;
807 if (chip->caps & ES18XX_HWV) {
808 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100809 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
810 &chip->hw_switch->id);
811 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
812 &chip->hw_volume->id);
Mark Salazar14086772006-01-16 11:33:52 +0100813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!split) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100815 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
816 &chip->master_switch->id);
817 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
818 &chip->master_volume->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 /* ack interrupt */
821 snd_es18xx_mixer_write(chip, 0x66, 0x00);
822 }
823 return IRQ_HANDLED;
824}
825
Takashi Iwai8047c912005-11-17 14:41:22 +0100826static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai8047c912005-11-17 14:41:22 +0100828 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int pos;
830
831 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
832 if (!(chip->active & DAC2))
833 return 0;
834 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
835 return pos >> chip->dma2_shift;
836 } else {
837 if (!(chip->active & DAC1))
838 return 0;
839 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
840 return pos >> chip->dma1_shift;
841 }
842}
843
Takashi Iwai8047c912005-11-17 14:41:22 +0100844static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Takashi Iwai8047c912005-11-17 14:41:22 +0100846 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int pos;
848
849 if (!(chip->active & ADC1))
850 return 0;
851 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
852 return pos >> chip->dma1_shift;
853}
854
Takashi Iwai8047c912005-11-17 14:41:22 +0100855static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
858 SNDRV_PCM_INFO_RESUME |
859 SNDRV_PCM_INFO_MMAP_VALID),
860 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
861 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
862 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
863 .rate_min = 4000,
864 .rate_max = 48000,
865 .channels_min = 1,
866 .channels_max = 2,
867 .buffer_bytes_max = 65536,
868 .period_bytes_min = 64,
869 .period_bytes_max = 65536,
870 .periods_min = 1,
871 .periods_max = 1024,
872 .fifo_size = 0,
873};
874
Takashi Iwai8047c912005-11-17 14:41:22 +0100875static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
878 SNDRV_PCM_INFO_RESUME |
879 SNDRV_PCM_INFO_MMAP_VALID),
880 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
881 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
882 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
883 .rate_min = 4000,
884 .rate_max = 48000,
885 .channels_min = 1,
886 .channels_max = 2,
887 .buffer_bytes_max = 65536,
888 .period_bytes_min = 64,
889 .period_bytes_max = 65536,
890 .periods_min = 1,
891 .periods_max = 1024,
892 .fifo_size = 0,
893};
894
Takashi Iwai8047c912005-11-17 14:41:22 +0100895static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwai8047c912005-11-17 14:41:22 +0100897 struct snd_pcm_runtime *runtime = substream->runtime;
898 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
901 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
902 chip->capture_a_substream &&
903 chip->capture_a_substream->runtime->channels != 1)
904 return -EAGAIN;
905 chip->playback_a_substream = substream;
906 } else if (substream->number <= 1) {
907 if (chip->capture_a_substream)
908 return -EAGAIN;
909 chip->playback_b_substream = substream;
910 } else {
911 snd_BUG();
912 return -EINVAL;
913 }
914 substream->runtime->hw = snd_es18xx_playback;
915 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
916 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
917 return 0;
918}
919
Takashi Iwai8047c912005-11-17 14:41:22 +0100920static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai8047c912005-11-17 14:41:22 +0100922 struct snd_pcm_runtime *runtime = substream->runtime;
923 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (chip->playback_b_substream)
926 return -EAGAIN;
927 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
928 chip->playback_a_substream &&
929 chip->playback_a_substream->runtime->channels != 1)
930 return -EAGAIN;
931 chip->capture_a_substream = substream;
932 substream->runtime->hw = snd_es18xx_capture;
933 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
934 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
935 return 0;
936}
937
Takashi Iwai8047c912005-11-17 14:41:22 +0100938static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Takashi Iwai8047c912005-11-17 14:41:22 +0100940 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
943 chip->playback_a_substream = NULL;
944 else
945 chip->playback_b_substream = NULL;
946
947 snd_pcm_lib_free_pages(substream);
948 return 0;
949}
950
Takashi Iwai8047c912005-11-17 14:41:22 +0100951static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai8047c912005-11-17 14:41:22 +0100953 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 chip->capture_a_substream = NULL;
956 snd_pcm_lib_free_pages(substream);
957 return 0;
958}
959
960/*
961 * MIXER part
962 */
963
Mark Salazarf4df2212006-01-16 11:31:14 +0100964/* Record source mux routines:
965 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
966 * bit table for the 4/5 source mux:
967 * reg 1C:
968 * b2 b1 b0 muxSource
969 * x 0 x microphone
970 * 0 1 x CD
971 * 1 1 0 line
972 * 1 1 1 mixer
973 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
974 * either the play mixer or the capture mixer.
975 *
976 * "map4Source" translates from source number to reg bit pattern
977 * "invMap4Source" translates from reg bit pattern to source number
978 */
979
Takashi Iwai8047c912005-11-17 14:41:22 +0100980static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Mark Salazarf4df2212006-01-16 11:31:14 +0100982 static char *texts4Source[4] = {
983 "Mic", "CD", "Line", "Master"
984 };
985 static char *texts5Source[5] = {
986 "Mic", "CD", "Line", "Master", "Mix"
987 };
988 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 "Mic", "Mic Master", "CD", "AOUT",
990 "Mic1", "Mix", "Line", "Master"
991 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100992 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
995 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100996 switch (chip->version) {
997 case 0x1868:
998 case 0x1878:
999 uinfo->value.enumerated.items = 4;
1000 if (uinfo->value.enumerated.item > 3)
1001 uinfo->value.enumerated.item = 3;
1002 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
1003 break;
1004 case 0x1887:
1005 case 0x1888:
1006 uinfo->value.enumerated.items = 5;
1007 if (uinfo->value.enumerated.item > 4)
1008 uinfo->value.enumerated.item = 4;
1009 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
1010 break;
1011 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
1012 case 0x1879:
1013 uinfo->value.enumerated.items = 8;
1014 if (uinfo->value.enumerated.item > 7)
1015 uinfo->value.enumerated.item = 7;
1016 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1017 break;
1018 default:
1019 return -EINVAL;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
1023
Takashi Iwai8047c912005-11-17 14:41:22 +01001024static int snd_es18xx_get_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 invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001027 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001028 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1029 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1030 muxSource = invMap4Source[muxSource];
1031 if (muxSource==3 &&
1032 (chip->version == 0x1887 || chip->version == 0x1888) &&
1033 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1034 )
1035 muxSource = 4;
1036 }
1037 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039}
1040
Takashi Iwai8047c912005-11-17 14:41:22 +01001041static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Mark Salazarf4df2212006-01-16 11:31:14 +01001043 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001044 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001046 unsigned char retVal = 0;
1047
1048 switch (chip->version) {
1049 /* 5 source chips */
1050 case 0x1887:
1051 case 0x1888:
1052 if (val > 4)
1053 return -EINVAL;
1054 if (val == 4) {
1055 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1056 val = 3;
1057 } else
1058 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1059 /* 4 source chips */
1060 case 0x1868:
1061 case 0x1878:
1062 if (val > 3)
1063 return -EINVAL;
1064 val = map4Source[val];
1065 break;
1066 /* 8 source chips */
1067 case 0x1869:
1068 case 0x1879:
1069 if (val > 7)
1070 return -EINVAL;
1071 break;
1072 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001074 }
1075 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001078#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Takashi Iwai8047c912005-11-17 14:41:22 +01001080static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Takashi Iwai8047c912005-11-17 14:41:22 +01001082 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1084 ucontrol->value.integer.value[0] = !!(val & 8);
1085 return 0;
1086}
1087
Takashi Iwai8047c912005-11-17 14:41:22 +01001088static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Takashi Iwai8047c912005-11-17 14:41:22 +01001090 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unsigned char oval, nval;
1092 int change;
1093 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1094 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1095 change = nval != oval;
1096 if (change) {
1097 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1098 snd_es18xx_mixer_write(chip, 0x50, nval);
1099 }
1100 return change;
1101}
1102
Takashi Iwai8047c912005-11-17 14:41:22 +01001103static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1106 uinfo->count = 2;
1107 uinfo->value.integer.min = 0;
1108 uinfo->value.integer.max = 63;
1109 return 0;
1110}
1111
Takashi Iwai8047c912005-11-17 14:41:22 +01001112static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Takashi Iwai8047c912005-11-17 14:41:22 +01001114 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1116 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1117 return 0;
1118}
1119
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001120#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Takashi Iwai8047c912005-11-17 14:41:22 +01001122static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Takashi Iwai8047c912005-11-17 14:41:22 +01001124 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1126 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1127 return 0;
1128}
1129
Takashi Iwai8047c912005-11-17 14:41:22 +01001130static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Takashi Iwai8047c912005-11-17 14:41:22 +01001132 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 chip->master_volume = NULL;
1134 chip->master_switch = NULL;
1135 chip->hw_volume = NULL;
1136 chip->hw_switch = NULL;
1137}
1138
Takashi Iwai8047c912005-11-17 14:41:22 +01001139static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned char mask, unsigned char val)
1141{
1142 if (reg < 0xa0)
1143 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1144 else
1145 return snd_es18xx_bits(chip, reg, mask, val);
1146}
1147
Takashi Iwai8047c912005-11-17 14:41:22 +01001148static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 if (reg < 0xa0)
1151 return snd_es18xx_mixer_read(chip, reg);
1152 else
1153 return snd_es18xx_read(chip, reg);
1154}
1155
1156#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1157{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1158 .info = snd_es18xx_info_single, \
1159 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1160 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1161
Takashi Iwai8047c912005-11-17 14:41:22 +01001162static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 int mask = (kcontrol->private_value >> 16) & 0xff;
1165
1166 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1167 uinfo->count = 1;
1168 uinfo->value.integer.min = 0;
1169 uinfo->value.integer.max = mask;
1170 return 0;
1171}
1172
Takashi Iwai8047c912005-11-17 14:41:22 +01001173static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwai8047c912005-11-17 14:41:22 +01001175 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int reg = kcontrol->private_value & 0xff;
1177 int shift = (kcontrol->private_value >> 8) & 0xff;
1178 int mask = (kcontrol->private_value >> 16) & 0xff;
1179 int invert = (kcontrol->private_value >> 24) & 0xff;
1180 int val;
1181
1182 val = snd_es18xx_reg_read(chip, reg);
1183 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1184 if (invert)
1185 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1186 return 0;
1187}
1188
Takashi Iwai8047c912005-11-17 14:41:22 +01001189static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai8047c912005-11-17 14:41:22 +01001191 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int reg = kcontrol->private_value & 0xff;
1193 int shift = (kcontrol->private_value >> 8) & 0xff;
1194 int mask = (kcontrol->private_value >> 16) & 0xff;
1195 int invert = (kcontrol->private_value >> 24) & 0xff;
1196 unsigned char val;
1197
1198 val = (ucontrol->value.integer.value[0] & mask);
1199 if (invert)
1200 val = mask - val;
1201 mask <<= shift;
1202 val <<= shift;
1203 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1204}
1205
1206#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1207{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1208 .info = snd_es18xx_info_double, \
1209 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1210 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1211
Takashi Iwai8047c912005-11-17 14:41:22 +01001212static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 int mask = (kcontrol->private_value >> 24) & 0xff;
1215
1216 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1217 uinfo->count = 2;
1218 uinfo->value.integer.min = 0;
1219 uinfo->value.integer.max = mask;
1220 return 0;
1221}
1222
Takashi Iwai8047c912005-11-17 14:41:22 +01001223static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Takashi Iwai8047c912005-11-17 14:41:22 +01001225 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int left_reg = kcontrol->private_value & 0xff;
1227 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1228 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1229 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1230 int mask = (kcontrol->private_value >> 24) & 0xff;
1231 int invert = (kcontrol->private_value >> 22) & 1;
1232 unsigned char left, right;
1233
1234 left = snd_es18xx_reg_read(chip, left_reg);
1235 if (left_reg != right_reg)
1236 right = snd_es18xx_reg_read(chip, right_reg);
1237 else
1238 right = left;
1239 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1240 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1241 if (invert) {
1242 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1243 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1244 }
1245 return 0;
1246}
1247
Takashi Iwai8047c912005-11-17 14:41:22 +01001248static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai8047c912005-11-17 14:41:22 +01001250 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int left_reg = kcontrol->private_value & 0xff;
1252 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1253 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1254 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1255 int mask = (kcontrol->private_value >> 24) & 0xff;
1256 int invert = (kcontrol->private_value >> 22) & 1;
1257 int change;
1258 unsigned char val1, val2, mask1, mask2;
1259
1260 val1 = ucontrol->value.integer.value[0] & mask;
1261 val2 = ucontrol->value.integer.value[1] & mask;
1262 if (invert) {
1263 val1 = mask - val1;
1264 val2 = mask - val2;
1265 }
1266 val1 <<= shift_left;
1267 val2 <<= shift_right;
1268 mask1 = mask << shift_left;
1269 mask2 = mask << shift_right;
1270 if (left_reg != right_reg) {
1271 change = 0;
1272 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1273 change = 1;
1274 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1275 change = 1;
1276 } else {
1277 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1278 val1 | val2) != (val1 | val2));
1279 }
1280 return change;
1281}
1282
Mark Salazarc1f6d412006-01-16 11:29:09 +01001283/* Mixer controls
1284 * These arrays contain setup data for mixer controls.
1285 *
1286 * The controls that are universal to all chipsets are fully initialized
1287 * here.
1288 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001289static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1291ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1292ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1293ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1294ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1296ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1298ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1301 .name = "Capture Source",
1302 .info = snd_es18xx_info_mux,
1303 .get = snd_es18xx_get_mux,
1304 .put = snd_es18xx_put_mux,
1305}
1306};
1307
Takashi Iwai8047c912005-11-17 14:41:22 +01001308static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1310ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1311ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1312ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1314ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1315};
1316
Mark Salazarc1f6d412006-01-16 11:29:09 +01001317/*
1318 * The chipset specific mixer controls
1319 */
1320static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1321 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1322
1323static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1324ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
Mark Salazar95b71292006-01-16 11:35:40 +01001325ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001326ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1327ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1328};
1329
Mark Salazar95b71292006-01-16 11:35:40 +01001330static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1331 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1332
1333static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1334ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1335ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1336ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1337};
1338
Takashi Iwai8047c912005-11-17 14:41:22 +01001339static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1341};
1342
Takashi Iwai8047c912005-11-17 14:41:22 +01001343static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1345ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1346};
1347
Takashi Iwai8047c912005-11-17 14:41:22 +01001348static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1350{
1351 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1352 .name = "3D Control - Switch",
1353 .info = snd_es18xx_info_spatializer_enable,
1354 .get = snd_es18xx_get_spatializer_enable,
1355 .put = snd_es18xx_put_spatializer_enable,
1356}
1357};
1358
Takashi Iwai8047c912005-11-17 14:41:22 +01001359static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1361
Takashi Iwai8047c912005-11-17 14:41:22 +01001362static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1364
Takashi Iwai8047c912005-11-17 14:41:22 +01001365static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1368 .name = "Hardware Master Playback Volume",
1369 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1370 .info = snd_es18xx_info_hw_volume,
1371 .get = snd_es18xx_get_hw_volume,
1372},
1373{
1374 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1375 .name = "Hardware Master Playback Switch",
1376 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1377 .info = snd_es18xx_info_hw_switch,
1378 .get = snd_es18xx_get_hw_switch,
1379},
1380ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1381};
1382
Takashi Iwai8047c912005-11-17 14:41:22 +01001383static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 int data;
1386 unsigned long flags;
1387 spin_lock_irqsave(&chip->ctrl_lock, flags);
1388 outb(reg, chip->ctrl_port);
1389 data = inb(chip->ctrl_port + 1);
1390 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1391 return data;
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwai8047c912005-11-17 14:41:22 +01001394static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned char reg, unsigned char data)
1396{
1397 /* No need for spinlocks, this function is used only in
1398 otherwise protected init code */
1399 outb(reg, chip->ctrl_port);
1400 outb(data, chip->ctrl_port + 1);
1401#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001402 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif
1404}
1405
Takashi Iwai8047c912005-11-17 14:41:22 +01001406static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int mask = 0;
1409
1410 /* enable extended mode */
1411 snd_es18xx_dsp_command(chip, 0xC6);
1412 /* Reset mixer registers */
1413 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1414
1415 /* Audio 1 DMA demand mode (4 bytes/request) */
1416 snd_es18xx_write(chip, 0xB9, 2);
1417 if (chip->caps & ES18XX_CONTROL) {
1418 /* Hardware volume IRQ */
1419 snd_es18xx_config_write(chip, 0x27, chip->irq);
1420 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1421 /* FM I/O */
1422 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1423 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1424 }
1425 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1426 /* MPU-401 I/O */
1427 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1428 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1429 /* MPU-401 IRQ */
1430 snd_es18xx_config_write(chip, 0x28, chip->irq);
1431 }
1432 /* Audio1 IRQ */
1433 snd_es18xx_config_write(chip, 0x70, chip->irq);
1434 /* Audio2 IRQ */
1435 snd_es18xx_config_write(chip, 0x72, chip->irq);
1436 /* Audio1 DMA */
1437 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1438 /* Audio2 DMA */
1439 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1440
1441 /* Enable Audio 1 IRQ */
1442 snd_es18xx_write(chip, 0xB1, 0x50);
1443 /* Enable Audio 2 IRQ */
1444 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1445 /* Enable Audio 1 DMA */
1446 snd_es18xx_write(chip, 0xB2, 0x50);
1447 /* Enable MPU and hardware volume interrupt */
1448 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001449 /* Enable ESS wavetable input */
1450 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452 else {
1453 int irqmask, dma1mask, dma2mask;
1454 switch (chip->irq) {
1455 case 2:
1456 case 9:
1457 irqmask = 0;
1458 break;
1459 case 5:
1460 irqmask = 1;
1461 break;
1462 case 7:
1463 irqmask = 2;
1464 break;
1465 case 10:
1466 irqmask = 3;
1467 break;
1468 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001469 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -ENODEV;
1471 }
1472 switch (chip->dma1) {
1473 case 0:
1474 dma1mask = 1;
1475 break;
1476 case 1:
1477 dma1mask = 2;
1478 break;
1479 case 3:
1480 dma1mask = 3;
1481 break;
1482 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001483 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -ENODEV;
1485 }
1486 switch (chip->dma2) {
1487 case 0:
1488 dma2mask = 0;
1489 break;
1490 case 1:
1491 dma2mask = 1;
1492 break;
1493 case 3:
1494 dma2mask = 2;
1495 break;
1496 case 5:
1497 dma2mask = 3;
1498 break;
1499 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001500 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -ENODEV;
1502 }
1503
1504 /* Enable and set Audio 1 IRQ */
1505 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1506 /* Enable and set Audio 1 DMA */
1507 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1508 /* Set Audio 2 DMA */
1509 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1510 /* Enable Audio 2 IRQ and DMA
1511 Set capture mixer input */
1512 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1513 /* Enable and set hardware volume interrupt */
1514 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1515 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1516 /* MPU401 share irq with audio
1517 Joystick enabled
1518 FM enabled */
1519 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1520 }
1521 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1522 }
1523 if (chip->caps & ES18XX_NEW_RATE) {
1524 /* Change behaviour of register A1
1525 4x oversampling
1526 2nd channel DAC asynchronous */
1527 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1528 }
1529 if (!(chip->caps & ES18XX_PCM2)) {
1530 /* Enable DMA FIFO */
1531 snd_es18xx_write(chip, 0xB7, 0x80);
1532 }
1533 if (chip->caps & ES18XX_SPATIALIZER) {
1534 /* Set spatializer parameters to recommended values */
1535 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1536 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1537 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1538 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1539 }
Mark Salazar95b71292006-01-16 11:35:40 +01001540 /* Flip the "enable I2S" bits for those chipsets that need it */
1541 switch (chip->version) {
1542 case 0x1879:
1543 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1544 //so a Switch control has been added to toggle this 0x71 bit on/off:
1545 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1546 /* Note: we fall through on purpose here. */
1547 case 0x1878:
1548 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1549 break;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /* Mute input source */
1552 if (chip->caps & ES18XX_MUTEREC)
1553 mask = 0x10;
1554 if (chip->caps & ES18XX_RECMIX)
1555 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1556 else {
1557 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1558 snd_es18xx_write(chip, 0xb4, 0x00);
1559 }
1560#ifndef AVOID_POPS
1561 /* Enable PCM output */
1562 snd_es18xx_dsp_command(chip, 0xD1);
1563#endif
1564
1565 return 0;
1566}
1567
Takashi Iwai8047c912005-11-17 14:41:22 +01001568static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 int hi,lo;
1571
1572 /* reset */
1573 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001574 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -ENODEV;
1576 }
1577
1578 snd_es18xx_dsp_command(chip, 0xe7);
1579 hi = snd_es18xx_dsp_get_byte(chip);
1580 if (hi < 0) {
1581 return hi;
1582 }
1583 lo = snd_es18xx_dsp_get_byte(chip);
1584 if ((lo & 0xf0) != 0x80) {
1585 return -ENODEV;
1586 }
1587 if (hi == 0x48) {
1588 chip->version = 0x488;
1589 return 0;
1590 }
1591 if (hi != 0x68) {
1592 return -ENODEV;
1593 }
1594 if ((lo & 0x0f) < 8) {
1595 chip->version = 0x688;
1596 return 0;
1597 }
1598
1599 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001600 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001602 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 lo = inb(chip->port + 0x05);
1604 if (hi != lo) {
1605 chip->version = hi << 8 | lo;
1606 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001607 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 chip->ctrl_port += inb(chip->port + 0x05);
1609
1610 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1611 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1612 return -EBUSY;
1613 }
1614
1615 return 0;
1616 }
1617
1618 /* If has Hardware volume */
1619 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1620 /* If has Audio2 */
1621 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1622 /* If has volume count */
1623 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1624 chip->version = 0x1887;
1625 } else {
1626 chip->version = 0x1888;
1627 }
1628 } else {
1629 chip->version = 0x1788;
1630 }
1631 }
1632 else
1633 chip->version = 0x1688;
1634 return 0;
1635}
1636
Takashi Iwai8047c912005-11-17 14:41:22 +01001637static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 if (snd_es18xx_identify(chip) < 0) {
1640 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1641 return -ENODEV;
1642 }
1643
1644 switch (chip->version) {
1645 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001646 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
1648 case 0x1869:
1649 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1650 break;
1651 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001652 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 case 0x1879:
1655 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1656 break;
1657 case 0x1887:
Mark Salazar14086772006-01-16 11:33:52 +01001658 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 break;
1660 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001661 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001664 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 chip->port, chip->version);
1666 return -ENODEV;
1667 }
1668
1669 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1670
1671 if (chip->dma1 == chip->dma2)
1672 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1673
1674 return snd_es18xx_initialize(chip);
1675}
1676
Takashi Iwai8047c912005-11-17 14:41:22 +01001677static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 .open = snd_es18xx_playback_open,
1679 .close = snd_es18xx_playback_close,
1680 .ioctl = snd_pcm_lib_ioctl,
1681 .hw_params = snd_es18xx_playback_hw_params,
1682 .hw_free = snd_es18xx_pcm_hw_free,
1683 .prepare = snd_es18xx_playback_prepare,
1684 .trigger = snd_es18xx_playback_trigger,
1685 .pointer = snd_es18xx_playback_pointer,
1686};
1687
Takashi Iwai8047c912005-11-17 14:41:22 +01001688static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 .open = snd_es18xx_capture_open,
1690 .close = snd_es18xx_capture_close,
1691 .ioctl = snd_pcm_lib_ioctl,
1692 .hw_params = snd_es18xx_capture_hw_params,
1693 .hw_free = snd_es18xx_pcm_hw_free,
1694 .prepare = snd_es18xx_capture_prepare,
1695 .trigger = snd_es18xx_capture_trigger,
1696 .pointer = snd_es18xx_capture_pointer,
1697};
1698
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001699static int __devinit snd_es18xx_pcm(struct snd_card *card, int device,
1700 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001702 struct snd_audiodrive *acard = card->private_data;
1703 struct snd_es18xx *chip = acard->chip;
Takashi Iwai8047c912005-11-17 14:41:22 +01001704 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 char str[16];
1706 int err;
1707
1708 if (rpcm)
1709 *rpcm = NULL;
1710 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001711 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001712 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001713 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001714 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (err < 0)
1716 return err;
1717
1718 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1719 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1720
1721 /* global setup */
1722 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 pcm->info_flags = 0;
1724 if (chip->caps & ES18XX_DUPLEX_SAME)
1725 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1726 if (! (chip->caps & ES18XX_PCM2))
1727 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1728 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1729 chip->pcm = pcm;
1730
1731 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1732 snd_dma_isa_data(),
1733 64*1024,
1734 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1735
1736 if (rpcm)
1737 *rpcm = pcm;
1738 return 0;
1739}
1740
1741/* Power Management support functions */
1742#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001743static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001745 struct snd_audiodrive *acard = card->private_data;
1746 struct snd_es18xx *chip = acard->chip;
1747
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001748 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 snd_pcm_suspend_all(chip->pcm);
1751
1752 /* power down */
1753 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1754 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1755 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1756 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1757
1758 return 0;
1759}
1760
Takashi Iwai8047c912005-11-17 14:41:22 +01001761static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001763 struct snd_audiodrive *acard = card->private_data;
1764 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1767 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1768
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001769 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
1771}
1772#endif /* CONFIG_PM */
1773
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001774static int snd_es18xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001776 struct snd_audiodrive *acard = card->private_data;
1777 struct snd_es18xx *chip = acard->chip;
1778
Takashi Iwaib1d57762005-10-10 11:56:31 +02001779 release_and_free_resource(chip->res_port);
1780 release_and_free_resource(chip->res_ctrl_port);
1781 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (chip->irq >= 0)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001783 free_irq(chip->irq, (void *) card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (chip->dma1 >= 0) {
1785 disable_dma(chip->dma1);
1786 free_dma(chip->dma1);
1787 }
1788 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1789 disable_dma(chip->dma2);
1790 free_dma(chip->dma2);
1791 }
1792 kfree(chip);
1793 return 0;
1794}
1795
Takashi Iwai8047c912005-11-17 14:41:22 +01001796static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001798 return snd_es18xx_free(device->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Takashi Iwai8047c912005-11-17 14:41:22 +01001801static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 unsigned long port,
1803 unsigned long mpu_port,
1804 unsigned long fm_port,
1805 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001806 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Takashi Iwai8047c912005-11-17 14:41:22 +01001808 struct snd_es18xx *chip;
1809 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 .dev_free = snd_es18xx_dev_free,
1811 };
1812 int err;
1813
1814 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001815 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (chip == NULL)
1817 return -ENOMEM;
1818 spin_lock_init(&chip->reg_lock);
1819 spin_lock_init(&chip->mixer_lock);
1820 spin_lock_init(&chip->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 chip->port = port;
1822 chip->mpu_port = mpu_port;
1823 chip->fm_port = fm_port;
1824 chip->irq = -1;
1825 chip->dma1 = -1;
1826 chip->dma2 = -1;
1827 chip->audio2_vol = 0x00;
1828 chip->active = 0;
1829
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001830 chip->res_port = request_region(port, 16, "ES18xx");
1831 if (chip->res_port == NULL) {
1832 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1834 return -EBUSY;
1835 }
1836
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001837 if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx",
1838 (void *) card)) {
1839 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1841 return -EBUSY;
1842 }
1843 chip->irq = irq;
1844
1845 if (request_dma(dma1, "ES18xx DMA 1")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001846 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1848 return -EBUSY;
1849 }
1850 chip->dma1 = dma1;
1851
1852 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001853 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1855 return -EBUSY;
1856 }
1857 chip->dma2 = dma2;
1858
1859 if (snd_es18xx_probe(chip) < 0) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001860 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return -ENODEV;
1862 }
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001863 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, NULL, &ops);
1864 if (err < 0) {
1865 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return err;
1867 }
1868 *rchip = chip;
1869 return 0;
1870}
1871
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001872static int __devinit snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001874 struct snd_audiodrive *acard = card->private_data;
1875 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int err;
1877 unsigned int idx;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 strcpy(card->mixername, chip->pcm->name);
1880
1881 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001882 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1884 if (chip->caps & ES18XX_HWV) {
1885 switch (idx) {
1886 case 0:
1887 chip->master_volume = kctl;
1888 kctl->private_free = snd_es18xx_hwv_free;
1889 break;
1890 case 1:
1891 chip->master_switch = kctl;
1892 kctl->private_free = snd_es18xx_hwv_free;
1893 break;
1894 }
1895 }
1896 if ((err = snd_ctl_add(card, kctl)) < 0)
1897 return err;
1898 }
1899 if (chip->caps & ES18XX_PCM2) {
1900 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1901 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1902 return err;
1903 }
1904 } else {
1905 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1906 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1907 return err;
1908 }
1909 }
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (chip->caps & ES18XX_RECMIX) {
1912 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1913 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1914 return err;
1915 }
1916 }
1917 switch (chip->version) {
1918 default:
1919 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1920 return err;
1921 break;
1922 case 0x1869:
1923 case 0x1879:
1924 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1925 return err;
1926 break;
1927 }
1928 if (chip->caps & ES18XX_SPATIALIZER) {
1929 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1930 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1931 return err;
1932 }
1933 }
1934 if (chip->caps & ES18XX_HWV) {
1935 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001936 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1938 if (idx == 0)
1939 chip->hw_volume = kctl;
1940 else
1941 chip->hw_switch = kctl;
1942 kctl->private_free = snd_es18xx_hwv_free;
1943 if ((err = snd_ctl_add(card, kctl)) < 0)
1944 return err;
1945
1946 }
1947 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001948 /* finish initializing other chipset specific controls
1949 */
1950 if (chip->version != 0x1868) {
1951 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1952 chip));
1953 if (err < 0)
1954 return err;
1955 }
1956 if (chip->version == 0x1869) {
1957 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1958 err = snd_ctl_add(card,
1959 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1960 chip));
1961 if (err < 0)
1962 return err;
1963 }
Mark Salazar95b71292006-01-16 11:35:40 +01001964 } else if (chip->version == 0x1878) {
1965 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1966 chip));
1967 if (err < 0)
1968 return err;
1969 } else if (chip->version == 0x1879) {
1970 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1971 err = snd_ctl_add(card,
1972 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1973 chip));
1974 if (err < 0)
1975 return err;
1976 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return 0;
1979}
1980
1981
1982/* Card level */
1983
1984MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1985MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1986MODULE_LICENSE("GPL");
1987MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1988 "{ESS,ES1869 PnP AudioDrive},"
1989 "{ESS,ES1878 PnP AudioDrive},"
1990 "{ESS,ES1879 PnP AudioDrive},"
1991 "{ESS,ES1887 PnP AudioDrive},"
1992 "{ESS,ES1888 PnP AudioDrive},"
1993 "{ESS,ES1887 AudioDrive},"
1994 "{ESS,ES1888 AudioDrive}}");
1995
1996static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1997static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1998static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1999#ifdef CONFIG_PNP
2000static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
2001#endif
2002static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
2003#ifndef CONFIG_PNP
2004static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
2005#else
2006static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2007#endif
2008static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2009static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
2010static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
2011static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
2012
2013module_param_array(index, int, NULL, 0444);
2014MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2015module_param_array(id, charp, NULL, 0444);
2016MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2017module_param_array(enable, bool, NULL, 0444);
2018MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2019#ifdef CONFIG_PNP
2020module_param_array(isapnp, bool, NULL, 0444);
2021MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2022#endif
2023module_param_array(port, long, NULL, 0444);
2024MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2025module_param_array(mpu_port, long, NULL, 0444);
2026MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2027module_param_array(fm_port, long, NULL, 0444);
2028MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2029module_param_array(irq, int, NULL, 0444);
2030MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2031module_param_array(dma1, int, NULL, 0444);
2032MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2033module_param_array(dma2, int, NULL, 0444);
2034MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002037static int isa_registered;
2038static int pnp_registered;
2039static int pnpc_registered;
Ondrej Zary1c398552006-07-31 12:51:57 +02002040
2041static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2042 { .id = "ESS1869" },
Rene Herman4848ffe2007-08-01 23:50:21 +02002043 { .id = "ESS1879" },
Ondrej Zary1c398552006-07-31 12:51:57 +02002044 { .id = "" } /* end */
2045};
2046
2047MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2048
2049/* PnP main device initialization */
Rene Herman109c53f842007-11-30 17:59:25 +01002050static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c398552006-07-31 12:51:57 +02002051{
Rene Herman109c53f842007-11-30 17:59:25 +01002052 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c398552006-07-31 12:51:57 +02002053 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2054 return -EBUSY;
2055 }
2056 /* ok. hack using Vendor-Defined Card-Level registers */
2057 /* skip csn and logdev initialization - already done in isapnp_configure */
2058 if (pnp_device_is_isapnp(pdev)) {
2059 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2060 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2061 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2062 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2063 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2064 isapnp_cfg_end();
2065 }
2066 port[dev] = pnp_port_start(pdev, 0);
2067 fm_port[dev] = pnp_port_start(pdev, 1);
2068 mpu_port[dev] = pnp_port_start(pdev, 2);
2069 dma1[dev] = pnp_dma(pdev, 0);
2070 dma2[dev] = pnp_dma(pdev, 1);
2071 irq[dev] = pnp_irq(pdev, 0);
2072 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2073 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2074 return 0;
2075}
2076
2077static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2078 struct pnp_dev *pdev)
2079{
Ondrej Zary1c398552006-07-31 12:51:57 +02002080 acard->dev = pdev;
Rene Herman109c53f842007-11-30 17:59:25 +01002081 if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
Ondrej Zary1c398552006-07-31 12:51:57 +02002082 return -EBUSY;
Ondrej Zary1c398552006-07-31 12:51:57 +02002083 return 0;
2084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2087 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2088 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2089 /* ESS 1868 (integrated on Maxisound Cards) */
2090 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2091 /* ESS 1868 (integrated on Maxisound Cards) */
2092 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2093 /* ESS ES1869 Plug and Play AudioDrive */
2094 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2095 /* ESS 1869 */
2096 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2097 /* ESS 1878 */
2098 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2099 /* ESS 1879 */
2100 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2101 /* --- */
2102 { .id = "" } /* end */
2103};
2104
2105MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2106
Ondrej Zary1c398552006-07-31 12:51:57 +02002107static int __devinit snd_audiodrive_pnpc(int dev, struct snd_audiodrive *acard,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 struct pnp_card_link *card,
2109 const struct pnp_card_device_id *id)
2110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +01002112 if (acard->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +01002116 if (acard->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* Control port initialization */
Ondrej Zary1c398552006-07-31 12:51:57 +02002120 if (pnp_activate_dev(acard->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2122 return -EAGAIN;
2123 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002124 snd_printdd("pnp: port=0x%llx\n",
2125 (unsigned long long)pnp_port_start(acard->devc, 0));
Rene Herman109c53f842007-11-30 17:59:25 +01002126 if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return 0;
2130}
2131#endif /* CONFIG_PNP */
2132
Takashi Iwai43bcd972005-09-05 17:19:20 +02002133#ifdef CONFIG_PNP
2134#define is_isapnp_selected(dev) isapnp[dev]
2135#else
2136#define is_isapnp_selected(dev) 0
2137#endif
2138
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002139static int snd_es18xx_card_new(int dev, struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002141 return snd_card_create(index[dev], id[dev], THIS_MODULE,
2142 sizeof(struct snd_audiodrive), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002143}
2144
2145static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2146{
2147 struct snd_audiodrive *acard = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002148 struct snd_es18xx *chip;
2149 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int err;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if ((err = snd_es18xx_new_device(card,
2153 port[dev],
2154 mpu_port[dev],
2155 fm_port[dev],
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002156 irq[dev], dma1[dev], dma2[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +02002157 &chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002158 return err;
2159 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002164 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2166 card->shortname,
2167 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002168 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 else
2170 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2171 card->shortname,
2172 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002173 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002175 err = snd_es18xx_pcm(card, 0, NULL);
2176 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002177 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002178
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002179 err = snd_es18xx_mixer(card);
2180 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002181 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2184 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002185 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002187 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190 }
2191
2192 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2193 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2194 chip->mpu_port, 0,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002195 irq[dev], 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002196 &chip->rmidi)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002200 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002203static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2204{
2205 return enable[dev] && !is_isapnp_selected(dev);
2206}
2207
2208static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002210 struct snd_card *card;
2211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002213 err = snd_es18xx_card_new(dev, &card);
2214 if (err < 0)
2215 return err;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002216 snd_card_set_dev(card, devptr);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002217 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2218 snd_card_free(card);
2219 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002221 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002225static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002226{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002227 int err;
2228 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2229 static int possible_dmas[] = {1, 0, 3, 5, -1};
2230
2231 if (irq[dev] == SNDRV_AUTO_IRQ) {
2232 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2233 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2234 return -EBUSY;
2235 }
2236 }
2237 if (dma1[dev] == SNDRV_AUTO_DMA) {
2238 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2239 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2240 return -EBUSY;
2241 }
2242 }
2243 if (dma2[dev] == SNDRV_AUTO_DMA) {
2244 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2245 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2246 return -EBUSY;
2247 }
2248 }
2249
2250 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002251 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002252 } else {
2253 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2254 int i;
2255 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2256 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002257 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002258 if (! err)
2259 return 0;
2260 }
2261 return err;
2262 }
2263}
2264
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002265static int __devexit snd_es18xx_isa_remove(struct device *devptr,
2266 unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002267{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002268 snd_card_free(dev_get_drvdata(devptr));
2269 dev_set_drvdata(devptr, NULL);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002270 return 0;
2271}
2272
2273#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002274static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2275 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002276{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002277 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002278}
2279
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002280static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002281{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002282 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002283}
2284#endif
2285
Rene Herman83c51c02007-03-20 11:33:46 +01002286#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002287
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002288static struct isa_driver snd_es18xx_isa_driver = {
2289 .match = snd_es18xx_isa_match,
2290 .probe = snd_es18xx_isa_probe,
2291 .remove = __devexit_p(snd_es18xx_isa_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002292#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002293 .suspend = snd_es18xx_isa_suspend,
2294 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002295#endif
2296 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002297 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002298 },
2299};
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302#ifdef CONFIG_PNP
Ondrej Zary1c398552006-07-31 12:51:57 +02002303static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2304 const struct pnp_device_id *id)
2305{
2306 static int dev;
2307 int err;
2308 struct snd_card *card;
2309
2310 if (pnp_device_is_isapnp(pdev))
2311 return -ENOENT; /* we have another procedure - card */
2312 for (; dev < SNDRV_CARDS; dev++) {
2313 if (enable[dev] && isapnp[dev])
2314 break;
2315 }
2316 if (dev >= SNDRV_CARDS)
2317 return -ENODEV;
2318
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002319 err = snd_es18xx_card_new(dev, &card);
2320 if (err < 0)
2321 return err;
Ondrej Zary1c398552006-07-31 12:51:57 +02002322 if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2323 snd_card_free(card);
2324 return err;
2325 }
2326 snd_card_set_dev(card, &pdev->dev);
2327 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2328 snd_card_free(card);
2329 return err;
2330 }
2331 pnp_set_drvdata(pdev, card);
2332 dev++;
Ondrej Zary1c398552006-07-31 12:51:57 +02002333 return 0;
2334}
2335
2336static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2337{
2338 snd_card_free(pnp_get_drvdata(pdev));
2339 pnp_set_drvdata(pdev, NULL);
2340}
2341
2342#ifdef CONFIG_PM
2343static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2344{
2345 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2346}
2347static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2348{
2349 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2350}
2351#endif
2352
2353static struct pnp_driver es18xx_pnp_driver = {
2354 .name = "es18xx-pnpbios",
2355 .id_table = snd_audiodrive_pnpbiosids,
2356 .probe = snd_audiodrive_pnp_detect,
2357 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2358#ifdef CONFIG_PM
2359 .suspend = snd_audiodrive_pnp_suspend,
2360 .resume = snd_audiodrive_pnp_resume,
2361#endif
2362};
2363
2364static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002365 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002368 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 int res;
2370
2371 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002372 if (enable[dev] && isapnp[dev])
2373 break;
2374 }
2375 if (dev >= SNDRV_CARDS)
2376 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002378 res = snd_es18xx_card_new(dev, &card);
2379 if (res < 0)
2380 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002381
Ondrej Zary1c398552006-07-31 12:51:57 +02002382 if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002383 snd_card_free(card);
2384 return res;
2385 }
2386 snd_card_set_dev(card, &pcard->card->dev);
2387 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2388 snd_card_free(card);
2389 return res;
2390 }
2391
2392 pnp_set_card_drvdata(pcard, card);
2393 dev++;
2394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Ondrej Zary1c398552006-07-31 12:51:57 +02002397static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002399 snd_card_free(pnp_get_card_drvdata(pcard));
2400 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002403#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002404static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002405{
2406 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2407}
2408
Ondrej Zary1c398552006-07-31 12:51:57 +02002409static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002410{
2411 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2412}
2413
2414#endif
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static struct pnp_card_driver es18xx_pnpc_driver = {
2417 .flags = PNP_DRIVER_RES_DISABLE,
2418 .name = "es18xx",
2419 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c398552006-07-31 12:51:57 +02002420 .probe = snd_audiodrive_pnpc_detect,
2421 .remove = __devexit_p(snd_audiodrive_pnpc_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002422#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002423 .suspend = snd_audiodrive_pnpc_suspend,
2424 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426};
2427#endif /* CONFIG_PNP */
2428
2429static int __init alsa_card_es18xx_init(void)
2430{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002433 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002434#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002435 if (!err)
2436 isa_registered = 1;
2437
Ondrej Zary1c398552006-07-31 12:51:57 +02002438 err = pnp_register_driver(&es18xx_pnp_driver);
2439 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002440 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002441
Ondrej Zary1c398552006-07-31 12:51:57 +02002442 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2443 if (!err)
2444 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002445
2446 if (isa_registered || pnp_registered)
2447 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002448#endif
Rene Herman609d7692007-05-15 11:42:56 +02002449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452static void __exit alsa_card_es18xx_exit(void)
2453{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002454#ifdef CONFIG_PNP
2455 if (pnpc_registered)
2456 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2457 if (pnp_registered)
2458 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002459 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002460#endif
Rene Herman609d7692007-05-15 11:42:56 +02002461 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
2464module_init(alsa_card_es18xx_init)
2465module_exit(alsa_card_es18xx_exit)