blob: 9a43baae72503598e19eb483991aafa18d29cb8b [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned long ctrl_port; /* Control port of ESS chip */
106 struct resource *res_port;
107 struct resource *res_mpu_port;
108 struct resource *res_ctrl_port;
109 int irq; /* IRQ number of ESS chip */
110 int dma1; /* DMA1 */
111 int dma2; /* DMA2 */
112 unsigned short version; /* version of ESS chip */
113 int caps; /* Chip capabilities */
114 unsigned short audio2_vol; /* volume level of audio2 */
115
116 unsigned short active; /* active channel mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned int dma1_shift;
118 unsigned int dma2_shift;
119
Takashi Iwai8047c912005-11-17 14:41:22 +0100120 struct snd_pcm *pcm;
121 struct snd_pcm_substream *playback_a_substream;
122 struct snd_pcm_substream *capture_a_substream;
123 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Takashi Iwai8047c912005-11-17 14:41:22 +0100125 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwai8047c912005-11-17 14:41:22 +0100127 struct snd_kcontrol *hw_volume;
128 struct snd_kcontrol *hw_switch;
129 struct snd_kcontrol *master_volume;
130 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 spinlock_t reg_lock;
133 spinlock_t mixer_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#ifdef CONFIG_PM
135 unsigned char pm_reg;
136#endif
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100137#ifdef CONFIG_PNP
138 struct pnp_dev *dev;
139 struct pnp_dev *devc;
140#endif
141};
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define AUDIO1_IRQ 0x01
144#define AUDIO2_IRQ 0x02
145#define HWV_IRQ 0x04
146#define MPU_IRQ 0x08
147
148#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
149#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
150#define ES18XX_RECMIX 0x0004 /* Has record mixer */
151#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
152#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
153#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
154#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100155#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
157#define ES18XX_I2S 0x0200 /* I2S mixer control */
158#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
159#define ES18XX_CONTROL 0x0800 /* Has control ports */
160
161/* Power Management */
162#define ES18XX_PM 0x07
163#define ES18XX_PM_GPO0 0x01
164#define ES18XX_PM_GPO1 0x02
165#define ES18XX_PM_PDR 0x04
166#define ES18XX_PM_ANA 0x08
167#define ES18XX_PM_FM 0x020
168#define ES18XX_PM_SUS 0x080
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Lowlevel */
171
172#define DAC1 0x01
173#define ADC1 0x02
174#define DAC2 0x04
175#define MILLISECOND 10000
176
Takashi Iwai8047c912005-11-17 14:41:22 +0100177static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int i;
180
181 for(i = MILLISECOND; i; i--)
182 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
183 outb(val, chip->port + 0x0C);
184 return 0;
185 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200186 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188}
189
Takashi Iwai8047c912005-11-17 14:41:22 +0100190static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int i;
193
194 for(i = MILLISECOND/10; i; i--)
195 if (inb(chip->port + 0x0C) & 0x40)
196 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200197 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
198 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENODEV;
200}
201
202#undef REG_DEBUG
203
Takashi Iwai8047c912005-11-17 14:41:22 +0100204static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned char reg, unsigned char data)
206{
207 unsigned long flags;
208 int ret;
209
210 spin_lock_irqsave(&chip->reg_lock, flags);
211 ret = snd_es18xx_dsp_command(chip, reg);
212 if (ret < 0)
213 goto end;
214 ret = snd_es18xx_dsp_command(chip, data);
215 end:
216 spin_unlock_irqrestore(&chip->reg_lock, flags);
217#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200218 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220 return ret;
221}
222
Takashi Iwai8047c912005-11-17 14:41:22 +0100223static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 unsigned long flags;
226 int ret, data;
227 spin_lock_irqsave(&chip->reg_lock, flags);
228 ret = snd_es18xx_dsp_command(chip, 0xC0);
229 if (ret < 0)
230 goto end;
231 ret = snd_es18xx_dsp_command(chip, reg);
232 if (ret < 0)
233 goto end;
234 data = snd_es18xx_dsp_get_byte(chip);
235 ret = data;
236#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200237 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239 end:
240 spin_unlock_irqrestore(&chip->reg_lock, flags);
241 return ret;
242}
243
244/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100245static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned char mask, unsigned char val)
247{
248 int ret;
249 unsigned char old, new, oval;
250 unsigned long flags;
251 spin_lock_irqsave(&chip->reg_lock, flags);
252 ret = snd_es18xx_dsp_command(chip, 0xC0);
253 if (ret < 0)
254 goto end;
255 ret = snd_es18xx_dsp_command(chip, reg);
256 if (ret < 0)
257 goto end;
258 ret = snd_es18xx_dsp_get_byte(chip);
259 if (ret < 0) {
260 goto end;
261 }
262 old = ret;
263 oval = old & mask;
264 if (val != oval) {
265 ret = snd_es18xx_dsp_command(chip, reg);
266 if (ret < 0)
267 goto end;
268 new = (old & ~mask) | (val & mask);
269 ret = snd_es18xx_dsp_command(chip, new);
270 if (ret < 0)
271 goto end;
272#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200273 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
274 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276 }
277 ret = oval;
278 end:
279 spin_unlock_irqrestore(&chip->reg_lock, flags);
280 return ret;
281}
282
Takashi Iwai8047c912005-11-17 14:41:22 +0100283static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned char reg, unsigned char data)
285{
286 unsigned long flags;
287 spin_lock_irqsave(&chip->mixer_lock, flags);
288 outb(reg, chip->port + 0x04);
289 outb(data, chip->port + 0x05);
290 spin_unlock_irqrestore(&chip->mixer_lock, flags);
291#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200292 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#endif
294}
295
Takashi Iwai8047c912005-11-17 14:41:22 +0100296static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 unsigned long flags;
299 int data;
300 spin_lock_irqsave(&chip->mixer_lock, flags);
301 outb(reg, chip->port + 0x04);
302 data = inb(chip->port + 0x05);
303 spin_unlock_irqrestore(&chip->mixer_lock, flags);
304#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200305 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
307 return data;
308}
309
310/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100311static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned char mask, unsigned char val)
313{
314 unsigned char old, new, oval;
315 unsigned long flags;
316 spin_lock_irqsave(&chip->mixer_lock, flags);
317 outb(reg, chip->port + 0x04);
318 old = inb(chip->port + 0x05);
319 oval = old & mask;
320 if (val != oval) {
321 new = (old & ~mask) | (val & mask);
322 outb(new, chip->port + 0x05);
323#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200324 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
325 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif
327 }
328 spin_unlock_irqrestore(&chip->mixer_lock, flags);
329 return oval;
330}
331
Takashi Iwai8047c912005-11-17 14:41:22 +0100332static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned char mask)
334{
335 int old, expected, new;
336 unsigned long flags;
337 spin_lock_irqsave(&chip->mixer_lock, flags);
338 outb(reg, chip->port + 0x04);
339 old = inb(chip->port + 0x05);
340 expected = old ^ mask;
341 outb(expected, chip->port + 0x05);
342 new = inb(chip->port + 0x05);
343 spin_unlock_irqrestore(&chip->mixer_lock, flags);
344#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200345 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
346 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
348 return expected == new;
349}
350
351
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100352static int __devinit snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int i;
355 outb(0x03, chip->port + 0x06);
356 inb(chip->port + 0x06);
357 outb(0x00, chip->port + 0x06);
358 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
359 if (inb(chip->port + 0x0A) != 0xAA)
360 return -1;
361 return 0;
362}
363
Takashi Iwai8047c912005-11-17 14:41:22 +0100364static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 outb(0x02, chip->port + 0x06);
367 inb(chip->port + 0x06);
368 outb(0x00, chip->port + 0x06);
369 return 0;
370}
371
Takashi Iwai8047c912005-11-17 14:41:22 +0100372static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 {
374 .num = 793800,
375 .den_min = 1,
376 .den_max = 128,
377 .den_step = 1,
378 },
379 {
380 .num = 768000,
381 .den_min = 1,
382 .den_max = 128,
383 .den_step = 1,
384 }
385};
386
Takashi Iwai8047c912005-11-17 14:41:22 +0100387static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .nrats = 2,
389 .rats = new_clocks,
390};
391
Takashi Iwai8047c912005-11-17 14:41:22 +0100392static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 {
394 .num = 795444,
395 .den_min = 1,
396 .den_max = 128,
397 .den_step = 1,
398 },
399 {
400 .num = 397722,
401 .den_min = 1,
402 .den_max = 128,
403 .den_step = 1,
404 }
405};
406
Takashi Iwai8047c912005-11-17 14:41:22 +0100407static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .nrats = 2,
409 .rats = old_clocks,
410};
411
412
Takashi Iwai8047c912005-11-17 14:41:22 +0100413static void snd_es18xx_rate_set(struct snd_es18xx *chip,
414 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int mode)
416{
417 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (chip->caps & ES18XX_NEW_RATE) {
420 if (runtime->rate_num == new_clocks[0].num)
421 bits = 128 - runtime->rate_den;
422 else
423 bits = 256 - runtime->rate_den;
424 } else {
425 if (runtime->rate_num == old_clocks[0].num)
426 bits = 256 - runtime->rate_den;
427 else
428 bits = 128 - runtime->rate_den;
429 }
430
431 /* set filter register */
432 div0 = 256 - 7160000*20/(8*82*runtime->rate);
433
434 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
435 snd_es18xx_mixer_write(chip, 0x70, bits);
436 /*
437 * Comment from kernel oss driver:
438 * FKS: fascinating: 0x72 doesn't seem to work.
439 */
440 snd_es18xx_write(chip, 0xA2, div0);
441 snd_es18xx_mixer_write(chip, 0x72, div0);
442 } else {
443 snd_es18xx_write(chip, 0xA1, bits);
444 snd_es18xx_write(chip, 0xA2, div0);
445 }
446}
447
Takashi Iwai8047c912005-11-17 14:41:22 +0100448static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
449 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Takashi Iwai8047c912005-11-17 14:41:22 +0100451 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int shift, err;
453
454 shift = 0;
455 if (params_channels(hw_params) == 2)
456 shift++;
457 if (snd_pcm_format_width(params_format(hw_params)) == 16)
458 shift++;
459
460 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
461 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
462 (chip->capture_a_substream) &&
463 params_channels(hw_params) != 1) {
464 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
465 return -EBUSY;
466 }
467 chip->dma2_shift = shift;
468 } else {
469 chip->dma1_shift = shift;
470 }
471 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
472 return err;
473 return 0;
474}
475
Takashi Iwai8047c912005-11-17 14:41:22 +0100476static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 return snd_pcm_lib_free_pages(substream);
479}
480
Takashi Iwai8047c912005-11-17 14:41:22 +0100481static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
482 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwai8047c912005-11-17 14:41:22 +0100484 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
486 unsigned int count = snd_pcm_lib_period_bytes(substream);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 snd_es18xx_rate_set(chip, substream, DAC2);
489
490 /* Transfer Count Reload */
491 count = 0x10000 - count;
492 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
493 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
494
495 /* Set format */
496 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
497 ((runtime->channels == 1) ? 0x00 : 0x02) |
498 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
499 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
500
501 /* Set DMA controller */
502 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
503
504 return 0;
505}
506
Takashi Iwai8047c912005-11-17 14:41:22 +0100507static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
508 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int cmd)
510{
511 switch (cmd) {
512 case SNDRV_PCM_TRIGGER_START:
513 case SNDRV_PCM_TRIGGER_RESUME:
514 if (chip->active & DAC2)
515 return 0;
516 chip->active |= DAC2;
517 /* Start DMA */
518 if (chip->dma2 >= 4)
519 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
520 else
521 snd_es18xx_mixer_write(chip, 0x78, 0x93);
522#ifdef AVOID_POPS
523 /* Avoid pops */
524 udelay(100000);
525 if (chip->caps & ES18XX_PCM2)
526 /* Restore Audio 2 volume */
527 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
528 else
529 /* Enable PCM output */
530 snd_es18xx_dsp_command(chip, 0xD1);
531#endif
532 break;
533 case SNDRV_PCM_TRIGGER_STOP:
534 case SNDRV_PCM_TRIGGER_SUSPEND:
535 if (!(chip->active & DAC2))
536 return 0;
537 chip->active &= ~DAC2;
538 /* Stop DMA */
539 snd_es18xx_mixer_write(chip, 0x78, 0x00);
540#ifdef AVOID_POPS
541 udelay(25000);
542 if (chip->caps & ES18XX_PCM2)
543 /* Set Audio 2 volume to 0 */
544 snd_es18xx_mixer_write(chip, 0x7C, 0);
545 else
546 /* Disable PCM output */
547 snd_es18xx_dsp_command(chip, 0xD3);
548#endif
549 break;
550 default:
551 return -EINVAL;
552 }
553
554 return 0;
555}
556
Takashi Iwai8047c912005-11-17 14:41:22 +0100557static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
558 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai8047c912005-11-17 14:41:22 +0100560 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int shift, err;
562
563 shift = 0;
564 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
565 chip->playback_a_substream &&
566 params_channels(hw_params) != 1) {
567 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
568 return -EBUSY;
569 }
570 if (params_channels(hw_params) == 2)
571 shift++;
572 if (snd_pcm_format_width(params_format(hw_params)) == 16)
573 shift++;
574 chip->dma1_shift = shift;
575 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
576 return err;
577 return 0;
578}
579
Takashi Iwai8047c912005-11-17 14:41:22 +0100580static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai8047c912005-11-17 14:41:22 +0100582 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
583 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
585 unsigned int count = snd_pcm_lib_period_bytes(substream);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 snd_es18xx_reset_fifo(chip);
588
589 /* Set stereo/mono */
590 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
591
592 snd_es18xx_rate_set(chip, substream, ADC1);
593
594 /* Transfer Count Reload */
595 count = 0x10000 - count;
596 snd_es18xx_write(chip, 0xA4, count & 0xff);
597 snd_es18xx_write(chip, 0xA5, count >> 8);
598
599#ifdef AVOID_POPS
600 udelay(100000);
601#endif
602
603 /* Set format */
604 snd_es18xx_write(chip, 0xB7,
605 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
606 snd_es18xx_write(chip, 0xB7, 0x90 |
607 ((runtime->channels == 1) ? 0x40 : 0x08) |
608 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
609 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
610
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200611 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
613
614 return 0;
615}
616
Takashi Iwai8047c912005-11-17 14:41:22 +0100617static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int cmd)
619{
Takashi Iwai8047c912005-11-17 14:41:22 +0100620 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 switch (cmd) {
623 case SNDRV_PCM_TRIGGER_START:
624 case SNDRV_PCM_TRIGGER_RESUME:
625 if (chip->active & ADC1)
626 return 0;
627 chip->active |= ADC1;
628 /* Start DMA */
629 snd_es18xx_write(chip, 0xB8, 0x0f);
630 break;
631 case SNDRV_PCM_TRIGGER_STOP:
632 case SNDRV_PCM_TRIGGER_SUSPEND:
633 if (!(chip->active & ADC1))
634 return 0;
635 chip->active &= ~ADC1;
636 /* Stop DMA */
637 snd_es18xx_write(chip, 0xB8, 0x00);
638 break;
639 default:
640 return -EINVAL;
641 }
642
643 return 0;
644}
645
Takashi Iwai8047c912005-11-17 14:41:22 +0100646static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
647 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai8047c912005-11-17 14:41:22 +0100649 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
651 unsigned int count = snd_pcm_lib_period_bytes(substream);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 snd_es18xx_reset_fifo(chip);
654
655 /* Set stereo/mono */
656 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
657
658 snd_es18xx_rate_set(chip, substream, DAC1);
659
660 /* Transfer Count Reload */
661 count = 0x10000 - count;
662 snd_es18xx_write(chip, 0xA4, count & 0xff);
663 snd_es18xx_write(chip, 0xA5, count >> 8);
664
665 /* Set format */
666 snd_es18xx_write(chip, 0xB6,
667 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
668 snd_es18xx_write(chip, 0xB7,
669 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
670 snd_es18xx_write(chip, 0xB7, 0x90 |
671 (runtime->channels == 1 ? 0x40 : 0x08) |
672 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
673 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
674
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200675 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
677
678 return 0;
679}
680
Takashi Iwai8047c912005-11-17 14:41:22 +0100681static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
682 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int cmd)
684{
685 switch (cmd) {
686 case SNDRV_PCM_TRIGGER_START:
687 case SNDRV_PCM_TRIGGER_RESUME:
688 if (chip->active & DAC1)
689 return 0;
690 chip->active |= DAC1;
691 /* Start DMA */
692 snd_es18xx_write(chip, 0xB8, 0x05);
693#ifdef AVOID_POPS
694 /* Avoid pops */
695 udelay(100000);
696 /* Enable Audio 1 */
697 snd_es18xx_dsp_command(chip, 0xD1);
698#endif
699 break;
700 case SNDRV_PCM_TRIGGER_STOP:
701 case SNDRV_PCM_TRIGGER_SUSPEND:
702 if (!(chip->active & DAC1))
703 return 0;
704 chip->active &= ~DAC1;
705 /* Stop DMA */
706 snd_es18xx_write(chip, 0xB8, 0x00);
707#ifdef AVOID_POPS
708 /* Avoid pops */
709 udelay(25000);
710 /* Disable Audio 1 */
711 snd_es18xx_dsp_command(chip, 0xD3);
712#endif
713 break;
714 default:
715 return -EINVAL;
716 }
717
718 return 0;
719}
720
Takashi Iwai8047c912005-11-17 14:41:22 +0100721static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai8047c912005-11-17 14:41:22 +0100723 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
725 return snd_es18xx_playback1_prepare(chip, substream);
726 else
727 return snd_es18xx_playback2_prepare(chip, substream);
728}
729
Takashi Iwai8047c912005-11-17 14:41:22 +0100730static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int cmd)
732{
Takashi Iwai8047c912005-11-17 14:41:22 +0100733 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
735 return snd_es18xx_playback1_trigger(chip, substream, cmd);
736 else
737 return snd_es18xx_playback2_trigger(chip, substream, cmd);
738}
739
David Howells7d12e782006-10-05 14:55:46 +0100740static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100742 struct snd_card *card = dev_id;
Krzysztof Heltb14f5de2009-10-25 11:10:01 +0100743 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned char status;
745
746 if (chip->caps & ES18XX_CONTROL) {
747 /* Read Interrupt status */
748 status = inb(chip->ctrl_port + 6);
749 } else {
750 /* Read Interrupt status */
751 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
752 }
753#if 0
754 else {
755 status = 0;
756 if (inb(chip->port + 0x0C) & 0x01)
757 status |= AUDIO1_IRQ;
758 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
759 status |= AUDIO2_IRQ;
760 if ((chip->caps & ES18XX_HWV) &&
761 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
762 status |= HWV_IRQ;
763 }
764#endif
765
766 /* Audio 1 & Audio 2 */
767 if (status & AUDIO2_IRQ) {
768 if (chip->active & DAC2)
769 snd_pcm_period_elapsed(chip->playback_a_substream);
770 /* ack interrupt */
771 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
772 }
773 if (status & AUDIO1_IRQ) {
774 /* ok.. capture is active */
775 if (chip->active & ADC1)
776 snd_pcm_period_elapsed(chip->capture_a_substream);
777 /* ok.. playback2 is active */
778 else if (chip->active & DAC1)
779 snd_pcm_period_elapsed(chip->playback_b_substream);
780 /* ack interrupt */
781 inb(chip->port + 0x0E);
782 }
783
784 /* MPU */
785 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100786 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Hardware volume */
789 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100790 int split = 0;
791 if (chip->caps & ES18XX_HWV) {
792 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100793 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
794 &chip->hw_switch->id);
795 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
796 &chip->hw_volume->id);
Mark Salazar14086772006-01-16 11:33:52 +0100797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!split) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100799 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
800 &chip->master_switch->id);
801 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
802 &chip->master_volume->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 /* ack interrupt */
805 snd_es18xx_mixer_write(chip, 0x66, 0x00);
806 }
807 return IRQ_HANDLED;
808}
809
Takashi Iwai8047c912005-11-17 14:41:22 +0100810static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwai8047c912005-11-17 14:41:22 +0100812 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100813 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int pos;
815
816 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
817 if (!(chip->active & DAC2))
818 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100819 pos = snd_dma_pointer(chip->dma2, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return pos >> chip->dma2_shift;
821 } else {
822 if (!(chip->active & DAC1))
823 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100824 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return pos >> chip->dma1_shift;
826 }
827}
828
Takashi Iwai8047c912005-11-17 14:41:22 +0100829static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai8047c912005-11-17 14:41:22 +0100831 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100832 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int pos;
834
835 if (!(chip->active & ADC1))
836 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100837 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return pos >> chip->dma1_shift;
839}
840
Takashi Iwai8047c912005-11-17 14:41:22 +0100841static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
844 SNDRV_PCM_INFO_RESUME |
845 SNDRV_PCM_INFO_MMAP_VALID),
846 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
847 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
848 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
849 .rate_min = 4000,
850 .rate_max = 48000,
851 .channels_min = 1,
852 .channels_max = 2,
853 .buffer_bytes_max = 65536,
854 .period_bytes_min = 64,
855 .period_bytes_max = 65536,
856 .periods_min = 1,
857 .periods_max = 1024,
858 .fifo_size = 0,
859};
860
Takashi Iwai8047c912005-11-17 14:41:22 +0100861static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
864 SNDRV_PCM_INFO_RESUME |
865 SNDRV_PCM_INFO_MMAP_VALID),
866 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
867 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
868 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
869 .rate_min = 4000,
870 .rate_max = 48000,
871 .channels_min = 1,
872 .channels_max = 2,
873 .buffer_bytes_max = 65536,
874 .period_bytes_min = 64,
875 .period_bytes_max = 65536,
876 .periods_min = 1,
877 .periods_max = 1024,
878 .fifo_size = 0,
879};
880
Takashi Iwai8047c912005-11-17 14:41:22 +0100881static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai8047c912005-11-17 14:41:22 +0100883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
887 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
888 chip->capture_a_substream &&
889 chip->capture_a_substream->runtime->channels != 1)
890 return -EAGAIN;
891 chip->playback_a_substream = substream;
892 } else if (substream->number <= 1) {
893 if (chip->capture_a_substream)
894 return -EAGAIN;
895 chip->playback_b_substream = substream;
896 } else {
897 snd_BUG();
898 return -EINVAL;
899 }
900 substream->runtime->hw = snd_es18xx_playback;
901 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
902 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
903 return 0;
904}
905
Takashi Iwai8047c912005-11-17 14:41:22 +0100906static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai8047c912005-11-17 14:41:22 +0100908 struct snd_pcm_runtime *runtime = substream->runtime;
909 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (chip->playback_b_substream)
912 return -EAGAIN;
913 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
914 chip->playback_a_substream &&
915 chip->playback_a_substream->runtime->channels != 1)
916 return -EAGAIN;
917 chip->capture_a_substream = substream;
918 substream->runtime->hw = snd_es18xx_capture;
919 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
920 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
921 return 0;
922}
923
Takashi Iwai8047c912005-11-17 14:41:22 +0100924static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwai8047c912005-11-17 14:41:22 +0100926 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
929 chip->playback_a_substream = NULL;
930 else
931 chip->playback_b_substream = NULL;
932
933 snd_pcm_lib_free_pages(substream);
934 return 0;
935}
936
Takashi Iwai8047c912005-11-17 14:41:22 +0100937static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwai8047c912005-11-17 14:41:22 +0100939 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 chip->capture_a_substream = NULL;
942 snd_pcm_lib_free_pages(substream);
943 return 0;
944}
945
946/*
947 * MIXER part
948 */
949
Mark Salazarf4df2212006-01-16 11:31:14 +0100950/* Record source mux routines:
951 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
952 * bit table for the 4/5 source mux:
953 * reg 1C:
954 * b2 b1 b0 muxSource
955 * x 0 x microphone
956 * 0 1 x CD
957 * 1 1 0 line
958 * 1 1 1 mixer
959 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
960 * either the play mixer or the capture mixer.
961 *
962 * "map4Source" translates from source number to reg bit pattern
963 * "invMap4Source" translates from reg bit pattern to source number
964 */
965
Takashi Iwai8047c912005-11-17 14:41:22 +0100966static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Mark Salazarf4df2212006-01-16 11:31:14 +0100968 static char *texts5Source[5] = {
969 "Mic", "CD", "Line", "Master", "Mix"
970 };
971 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 "Mic", "Mic Master", "CD", "AOUT",
973 "Mic1", "Mix", "Line", "Master"
974 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100975 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
978 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100979 switch (chip->version) {
980 case 0x1868:
981 case 0x1878:
982 uinfo->value.enumerated.items = 4;
983 if (uinfo->value.enumerated.item > 3)
984 uinfo->value.enumerated.item = 3;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100985 strcpy(uinfo->value.enumerated.name,
986 texts5Source[uinfo->value.enumerated.item]);
Mark Salazarf4df2212006-01-16 11:31:14 +0100987 break;
988 case 0x1887:
989 case 0x1888:
990 uinfo->value.enumerated.items = 5;
991 if (uinfo->value.enumerated.item > 4)
992 uinfo->value.enumerated.item = 4;
993 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
994 break;
995 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
996 case 0x1879:
997 uinfo->value.enumerated.items = 8;
998 if (uinfo->value.enumerated.item > 7)
999 uinfo->value.enumerated.item = 7;
1000 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1001 break;
1002 default:
1003 return -EINVAL;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006}
1007
Takashi Iwai8047c912005-11-17 14:41:22 +01001008static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Mark Salazarf4df2212006-01-16 11:31:14 +01001010 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001011 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001012 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1013 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1014 muxSource = invMap4Source[muxSource];
1015 if (muxSource==3 &&
1016 (chip->version == 0x1887 || chip->version == 0x1888) &&
1017 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1018 )
1019 muxSource = 4;
1020 }
1021 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
Takashi Iwai8047c912005-11-17 14:41:22 +01001025static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Mark Salazarf4df2212006-01-16 11:31:14 +01001027 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001028 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001030 unsigned char retVal = 0;
1031
1032 switch (chip->version) {
1033 /* 5 source chips */
1034 case 0x1887:
1035 case 0x1888:
1036 if (val > 4)
1037 return -EINVAL;
1038 if (val == 4) {
1039 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1040 val = 3;
1041 } else
1042 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1043 /* 4 source chips */
1044 case 0x1868:
1045 case 0x1878:
1046 if (val > 3)
1047 return -EINVAL;
1048 val = map4Source[val];
1049 break;
1050 /* 8 source chips */
1051 case 0x1869:
1052 case 0x1879:
1053 if (val > 7)
1054 return -EINVAL;
1055 break;
1056 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001058 }
1059 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001062#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Takashi Iwai8047c912005-11-17 14:41:22 +01001064static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai8047c912005-11-17 14:41:22 +01001066 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1068 ucontrol->value.integer.value[0] = !!(val & 8);
1069 return 0;
1070}
1071
Takashi Iwai8047c912005-11-17 14:41:22 +01001072static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai8047c912005-11-17 14:41:22 +01001074 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 unsigned char oval, nval;
1076 int change;
1077 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1078 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1079 change = nval != oval;
1080 if (change) {
1081 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1082 snd_es18xx_mixer_write(chip, 0x50, nval);
1083 }
1084 return change;
1085}
1086
Takashi Iwai8047c912005-11-17 14:41:22 +01001087static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1090 uinfo->count = 2;
1091 uinfo->value.integer.min = 0;
1092 uinfo->value.integer.max = 63;
1093 return 0;
1094}
1095
Takashi Iwai8047c912005-11-17 14:41:22 +01001096static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Takashi Iwai8047c912005-11-17 14:41:22 +01001098 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1100 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1101 return 0;
1102}
1103
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001104#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Takashi Iwai8047c912005-11-17 14:41:22 +01001106static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Takashi Iwai8047c912005-11-17 14:41:22 +01001108 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1110 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1111 return 0;
1112}
1113
Takashi Iwai8047c912005-11-17 14:41:22 +01001114static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Takashi Iwai8047c912005-11-17 14:41:22 +01001116 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 chip->master_volume = NULL;
1118 chip->master_switch = NULL;
1119 chip->hw_volume = NULL;
1120 chip->hw_switch = NULL;
1121}
1122
Takashi Iwai8047c912005-11-17 14:41:22 +01001123static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 unsigned char mask, unsigned char val)
1125{
1126 if (reg < 0xa0)
1127 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1128 else
1129 return snd_es18xx_bits(chip, reg, mask, val);
1130}
1131
Takashi Iwai8047c912005-11-17 14:41:22 +01001132static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 if (reg < 0xa0)
1135 return snd_es18xx_mixer_read(chip, reg);
1136 else
1137 return snd_es18xx_read(chip, reg);
1138}
1139
1140#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1141{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1142 .info = snd_es18xx_info_single, \
1143 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1144 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1145
Takashi Iwai8047c912005-11-17 14:41:22 +01001146static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 int mask = (kcontrol->private_value >> 16) & 0xff;
1149
1150 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1151 uinfo->count = 1;
1152 uinfo->value.integer.min = 0;
1153 uinfo->value.integer.max = mask;
1154 return 0;
1155}
1156
Takashi Iwai8047c912005-11-17 14:41:22 +01001157static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Takashi Iwai8047c912005-11-17 14:41:22 +01001159 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int reg = kcontrol->private_value & 0xff;
1161 int shift = (kcontrol->private_value >> 8) & 0xff;
1162 int mask = (kcontrol->private_value >> 16) & 0xff;
1163 int invert = (kcontrol->private_value >> 24) & 0xff;
1164 int val;
1165
1166 val = snd_es18xx_reg_read(chip, reg);
1167 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1168 if (invert)
1169 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1170 return 0;
1171}
1172
Takashi Iwai8047c912005-11-17 14:41:22 +01001173static int snd_es18xx_put_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 unsigned char val;
1181
1182 val = (ucontrol->value.integer.value[0] & mask);
1183 if (invert)
1184 val = mask - val;
1185 mask <<= shift;
1186 val <<= shift;
1187 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1188}
1189
1190#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1191{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1192 .info = snd_es18xx_info_double, \
1193 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1194 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1195
Takashi Iwai8047c912005-11-17 14:41:22 +01001196static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 int mask = (kcontrol->private_value >> 24) & 0xff;
1199
1200 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1201 uinfo->count = 2;
1202 uinfo->value.integer.min = 0;
1203 uinfo->value.integer.max = mask;
1204 return 0;
1205}
1206
Takashi Iwai8047c912005-11-17 14:41:22 +01001207static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Takashi Iwai8047c912005-11-17 14:41:22 +01001209 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int left_reg = kcontrol->private_value & 0xff;
1211 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1212 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1213 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1214 int mask = (kcontrol->private_value >> 24) & 0xff;
1215 int invert = (kcontrol->private_value >> 22) & 1;
1216 unsigned char left, right;
1217
1218 left = snd_es18xx_reg_read(chip, left_reg);
1219 if (left_reg != right_reg)
1220 right = snd_es18xx_reg_read(chip, right_reg);
1221 else
1222 right = left;
1223 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1224 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1225 if (invert) {
1226 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1227 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1228 }
1229 return 0;
1230}
1231
Takashi Iwai8047c912005-11-17 14:41:22 +01001232static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwai8047c912005-11-17 14:41:22 +01001234 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int left_reg = kcontrol->private_value & 0xff;
1236 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1237 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1238 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1239 int mask = (kcontrol->private_value >> 24) & 0xff;
1240 int invert = (kcontrol->private_value >> 22) & 1;
1241 int change;
1242 unsigned char val1, val2, mask1, mask2;
1243
1244 val1 = ucontrol->value.integer.value[0] & mask;
1245 val2 = ucontrol->value.integer.value[1] & mask;
1246 if (invert) {
1247 val1 = mask - val1;
1248 val2 = mask - val2;
1249 }
1250 val1 <<= shift_left;
1251 val2 <<= shift_right;
1252 mask1 = mask << shift_left;
1253 mask2 = mask << shift_right;
1254 if (left_reg != right_reg) {
1255 change = 0;
1256 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1257 change = 1;
1258 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1259 change = 1;
1260 } else {
1261 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1262 val1 | val2) != (val1 | val2));
1263 }
1264 return change;
1265}
1266
Mark Salazarc1f6d412006-01-16 11:29:09 +01001267/* Mixer controls
1268 * These arrays contain setup data for mixer controls.
1269 *
1270 * The controls that are universal to all chipsets are fully initialized
1271 * here.
1272 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001273static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1275ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1276ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1277ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1278ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1280ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1282ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1285 .name = "Capture Source",
1286 .info = snd_es18xx_info_mux,
1287 .get = snd_es18xx_get_mux,
1288 .put = snd_es18xx_put_mux,
1289}
1290};
1291
Takashi Iwai8047c912005-11-17 14:41:22 +01001292static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1294ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1295ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1296ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1298ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1299};
1300
Mark Salazarc1f6d412006-01-16 11:29:09 +01001301/*
1302 * The chipset specific mixer controls
1303 */
1304static struct snd_kcontrol_new snd_es18xx_opt_speaker =
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001305 ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001306
1307static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1308ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
Mark Salazar95b71292006-01-16 11:35:40 +01001309ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001310ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1311ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1312};
1313
Mark Salazar95b71292006-01-16 11:35:40 +01001314static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1315 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1316
1317static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1318ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1319ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1320ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1321};
1322
Takashi Iwai8047c912005-11-17 14:41:22 +01001323static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1325};
1326
Takashi Iwai8047c912005-11-17 14:41:22 +01001327static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1329ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1330};
1331
Takashi Iwai8047c912005-11-17 14:41:22 +01001332static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1334{
1335 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1336 .name = "3D Control - Switch",
1337 .info = snd_es18xx_info_spatializer_enable,
1338 .get = snd_es18xx_get_spatializer_enable,
1339 .put = snd_es18xx_put_spatializer_enable,
1340}
1341};
1342
Takashi Iwai8047c912005-11-17 14:41:22 +01001343static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1345
Takashi Iwai8047c912005-11-17 14:41:22 +01001346static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1348
Takashi Iwai8047c912005-11-17 14:41:22 +01001349static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1352 .name = "Hardware Master Playback Volume",
1353 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1354 .info = snd_es18xx_info_hw_volume,
1355 .get = snd_es18xx_get_hw_volume,
1356},
1357{
1358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1359 .name = "Hardware Master Playback Switch",
1360 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1361 .info = snd_es18xx_info_hw_switch,
1362 .get = snd_es18xx_get_hw_switch,
1363},
1364ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1365};
1366
Takashi Iwai8047c912005-11-17 14:41:22 +01001367static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 int data;
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 outb(reg, chip->ctrl_port);
1372 data = inb(chip->ctrl_port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return data;
1374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Takashi Iwai8047c912005-11-17 14:41:22 +01001376static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 unsigned char reg, unsigned char data)
1378{
1379 /* No need for spinlocks, this function is used only in
1380 otherwise protected init code */
1381 outb(reg, chip->ctrl_port);
1382 outb(data, chip->ctrl_port + 1);
1383#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001384 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#endif
1386}
1387
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001388static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip,
1389 unsigned long mpu_port,
1390 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 int mask = 0;
1393
1394 /* enable extended mode */
1395 snd_es18xx_dsp_command(chip, 0xC6);
1396 /* Reset mixer registers */
1397 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1398
1399 /* Audio 1 DMA demand mode (4 bytes/request) */
1400 snd_es18xx_write(chip, 0xB9, 2);
1401 if (chip->caps & ES18XX_CONTROL) {
1402 /* Hardware volume IRQ */
1403 snd_es18xx_config_write(chip, 0x27, chip->irq);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001404 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* FM I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001406 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1407 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001409 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* MPU-401 I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001411 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1412 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /* MPU-401 IRQ */
1414 snd_es18xx_config_write(chip, 0x28, chip->irq);
1415 }
1416 /* Audio1 IRQ */
1417 snd_es18xx_config_write(chip, 0x70, chip->irq);
1418 /* Audio2 IRQ */
1419 snd_es18xx_config_write(chip, 0x72, chip->irq);
1420 /* Audio1 DMA */
1421 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1422 /* Audio2 DMA */
1423 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1424
1425 /* Enable Audio 1 IRQ */
1426 snd_es18xx_write(chip, 0xB1, 0x50);
1427 /* Enable Audio 2 IRQ */
1428 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1429 /* Enable Audio 1 DMA */
1430 snd_es18xx_write(chip, 0xB2, 0x50);
1431 /* Enable MPU and hardware volume interrupt */
1432 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001433 /* Enable ESS wavetable input */
1434 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 else {
1437 int irqmask, dma1mask, dma2mask;
1438 switch (chip->irq) {
1439 case 2:
1440 case 9:
1441 irqmask = 0;
1442 break;
1443 case 5:
1444 irqmask = 1;
1445 break;
1446 case 7:
1447 irqmask = 2;
1448 break;
1449 case 10:
1450 irqmask = 3;
1451 break;
1452 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001453 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENODEV;
1455 }
1456 switch (chip->dma1) {
1457 case 0:
1458 dma1mask = 1;
1459 break;
1460 case 1:
1461 dma1mask = 2;
1462 break;
1463 case 3:
1464 dma1mask = 3;
1465 break;
1466 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001467 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -ENODEV;
1469 }
1470 switch (chip->dma2) {
1471 case 0:
1472 dma2mask = 0;
1473 break;
1474 case 1:
1475 dma2mask = 1;
1476 break;
1477 case 3:
1478 dma2mask = 2;
1479 break;
1480 case 5:
1481 dma2mask = 3;
1482 break;
1483 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001484 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -ENODEV;
1486 }
1487
1488 /* Enable and set Audio 1 IRQ */
1489 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1490 /* Enable and set Audio 1 DMA */
1491 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1492 /* Set Audio 2 DMA */
1493 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1494 /* Enable Audio 2 IRQ and DMA
1495 Set capture mixer input */
1496 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1497 /* Enable and set hardware volume interrupt */
1498 snd_es18xx_mixer_write(chip, 0x64, 0x06);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001499 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* MPU401 share irq with audio
1501 Joystick enabled
1502 FM enabled */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001503 snd_es18xx_mixer_write(chip, 0x40,
1504 0x43 | (mpu_port & 0xf0) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1507 }
1508 if (chip->caps & ES18XX_NEW_RATE) {
1509 /* Change behaviour of register A1
1510 4x oversampling
1511 2nd channel DAC asynchronous */
1512 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1513 }
1514 if (!(chip->caps & ES18XX_PCM2)) {
1515 /* Enable DMA FIFO */
1516 snd_es18xx_write(chip, 0xB7, 0x80);
1517 }
1518 if (chip->caps & ES18XX_SPATIALIZER) {
1519 /* Set spatializer parameters to recommended values */
1520 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1521 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1522 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1523 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1524 }
Mark Salazar95b71292006-01-16 11:35:40 +01001525 /* Flip the "enable I2S" bits for those chipsets that need it */
1526 switch (chip->version) {
1527 case 0x1879:
1528 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1529 //so a Switch control has been added to toggle this 0x71 bit on/off:
1530 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1531 /* Note: we fall through on purpose here. */
1532 case 0x1878:
1533 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1534 break;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* Mute input source */
1537 if (chip->caps & ES18XX_MUTEREC)
1538 mask = 0x10;
1539 if (chip->caps & ES18XX_RECMIX)
1540 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1541 else {
1542 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1543 snd_es18xx_write(chip, 0xb4, 0x00);
1544 }
1545#ifndef AVOID_POPS
1546 /* Enable PCM output */
1547 snd_es18xx_dsp_command(chip, 0xD1);
1548#endif
1549
1550 return 0;
1551}
1552
Takashi Iwai8047c912005-11-17 14:41:22 +01001553static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 int hi,lo;
1556
1557 /* reset */
1558 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001559 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -ENODEV;
1561 }
1562
1563 snd_es18xx_dsp_command(chip, 0xe7);
1564 hi = snd_es18xx_dsp_get_byte(chip);
1565 if (hi < 0) {
1566 return hi;
1567 }
1568 lo = snd_es18xx_dsp_get_byte(chip);
1569 if ((lo & 0xf0) != 0x80) {
1570 return -ENODEV;
1571 }
1572 if (hi == 0x48) {
1573 chip->version = 0x488;
1574 return 0;
1575 }
1576 if (hi != 0x68) {
1577 return -ENODEV;
1578 }
1579 if ((lo & 0x0f) < 8) {
1580 chip->version = 0x688;
1581 return 0;
1582 }
1583
1584 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001585 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001587 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 lo = inb(chip->port + 0x05);
1589 if (hi != lo) {
1590 chip->version = hi << 8 | lo;
1591 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001592 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 chip->ctrl_port += inb(chip->port + 0x05);
1594
1595 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1596 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1597 return -EBUSY;
1598 }
1599
1600 return 0;
1601 }
1602
1603 /* If has Hardware volume */
1604 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1605 /* If has Audio2 */
1606 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1607 /* If has volume count */
1608 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1609 chip->version = 0x1887;
1610 } else {
1611 chip->version = 0x1888;
1612 }
1613 } else {
1614 chip->version = 0x1788;
1615 }
1616 }
1617 else
1618 chip->version = 0x1688;
1619 return 0;
1620}
1621
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001622static int __devinit snd_es18xx_probe(struct snd_es18xx *chip,
1623 unsigned long mpu_port,
1624 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 if (snd_es18xx_identify(chip) < 0) {
1627 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1628 return -ENODEV;
1629 }
1630
1631 switch (chip->version) {
1632 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001633 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
1635 case 0x1869:
1636 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1637 break;
1638 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001639 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case 0x1879:
1642 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1643 break;
1644 case 0x1887:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001646 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
1648 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001649 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 chip->port, chip->version);
1651 return -ENODEV;
1652 }
1653
1654 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1655
1656 if (chip->dma1 == chip->dma2)
1657 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1658
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001659 return snd_es18xx_initialize(chip, mpu_port, fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Takashi Iwai8047c912005-11-17 14:41:22 +01001662static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 .open = snd_es18xx_playback_open,
1664 .close = snd_es18xx_playback_close,
1665 .ioctl = snd_pcm_lib_ioctl,
1666 .hw_params = snd_es18xx_playback_hw_params,
1667 .hw_free = snd_es18xx_pcm_hw_free,
1668 .prepare = snd_es18xx_playback_prepare,
1669 .trigger = snd_es18xx_playback_trigger,
1670 .pointer = snd_es18xx_playback_pointer,
1671};
1672
Takashi Iwai8047c912005-11-17 14:41:22 +01001673static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 .open = snd_es18xx_capture_open,
1675 .close = snd_es18xx_capture_close,
1676 .ioctl = snd_pcm_lib_ioctl,
1677 .hw_params = snd_es18xx_capture_hw_params,
1678 .hw_free = snd_es18xx_pcm_hw_free,
1679 .prepare = snd_es18xx_capture_prepare,
1680 .trigger = snd_es18xx_capture_trigger,
1681 .pointer = snd_es18xx_capture_pointer,
1682};
1683
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001684static int __devinit snd_es18xx_pcm(struct snd_card *card, int device,
1685 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001687 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001688 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 char str[16];
1690 int err;
1691
1692 if (rpcm)
1693 *rpcm = NULL;
1694 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001695 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001696 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001697 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001698 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (err < 0)
1700 return err;
1701
1702 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1703 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1704
1705 /* global setup */
1706 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 pcm->info_flags = 0;
1708 if (chip->caps & ES18XX_DUPLEX_SAME)
1709 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1710 if (! (chip->caps & ES18XX_PCM2))
1711 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1712 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1713 chip->pcm = pcm;
1714
1715 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1716 snd_dma_isa_data(),
1717 64*1024,
1718 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1719
1720 if (rpcm)
1721 *rpcm = pcm;
1722 return 0;
1723}
1724
1725/* Power Management support functions */
1726#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001727static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001729 struct snd_es18xx *chip = card->private_data;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001730
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001731 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 snd_pcm_suspend_all(chip->pcm);
1734
1735 /* power down */
1736 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1737 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1738 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1739 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1740
1741 return 0;
1742}
1743
Takashi Iwai8047c912005-11-17 14:41:22 +01001744static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001746 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1749 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1750
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001751 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753}
1754#endif /* CONFIG_PM */
1755
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001756static int snd_es18xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001758 struct snd_es18xx *chip = card->private_data;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001759
Takashi Iwaib1d57762005-10-10 11:56:31 +02001760 release_and_free_resource(chip->res_port);
1761 release_and_free_resource(chip->res_ctrl_port);
1762 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (chip->irq >= 0)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001764 free_irq(chip->irq, (void *) card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (chip->dma1 >= 0) {
1766 disable_dma(chip->dma1);
1767 free_dma(chip->dma1);
1768 }
1769 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1770 disable_dma(chip->dma2);
1771 free_dma(chip->dma2);
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
Takashi Iwai8047c912005-11-17 14:41:22 +01001776static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001778 return snd_es18xx_free(device->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Takashi Iwai8047c912005-11-17 14:41:22 +01001781static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 unsigned long port,
1783 unsigned long mpu_port,
1784 unsigned long fm_port,
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001785 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001787 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001788 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 .dev_free = snd_es18xx_dev_free,
1790 };
1791 int err;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 spin_lock_init(&chip->reg_lock);
1794 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 chip->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 chip->irq = -1;
1797 chip->dma1 = -1;
1798 chip->dma2 = -1;
1799 chip->audio2_vol = 0x00;
1800 chip->active = 0;
1801
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001802 chip->res_port = request_region(port, 16, "ES18xx");
1803 if (chip->res_port == NULL) {
1804 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1806 return -EBUSY;
1807 }
1808
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001809 if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx",
1810 (void *) card)) {
1811 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1813 return -EBUSY;
1814 }
1815 chip->irq = irq;
1816
1817 if (request_dma(dma1, "ES18xx DMA 1")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001818 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1820 return -EBUSY;
1821 }
1822 chip->dma1 = dma1;
1823
1824 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001825 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1827 return -EBUSY;
1828 }
1829 chip->dma2 = dma2;
1830
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001831 if (snd_es18xx_probe(chip, mpu_port, fm_port) < 0) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001832 snd_es18xx_free(card);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001833 return -ENODEV;
1834 }
1835 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001836 if (err < 0) {
1837 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return err;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
1841}
1842
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001843static int __devinit snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001845 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int err;
1847 unsigned int idx;
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 strcpy(card->mixername, chip->pcm->name);
1850
1851 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001852 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1854 if (chip->caps & ES18XX_HWV) {
1855 switch (idx) {
1856 case 0:
1857 chip->master_volume = kctl;
1858 kctl->private_free = snd_es18xx_hwv_free;
1859 break;
1860 case 1:
1861 chip->master_switch = kctl;
1862 kctl->private_free = snd_es18xx_hwv_free;
1863 break;
1864 }
1865 }
1866 if ((err = snd_ctl_add(card, kctl)) < 0)
1867 return err;
1868 }
1869 if (chip->caps & ES18XX_PCM2) {
1870 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1871 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1872 return err;
1873 }
1874 } else {
1875 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1876 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1877 return err;
1878 }
1879 }
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (chip->caps & ES18XX_RECMIX) {
1882 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1883 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1884 return err;
1885 }
1886 }
1887 switch (chip->version) {
1888 default:
1889 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1890 return err;
1891 break;
1892 case 0x1869:
1893 case 0x1879:
1894 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1895 return err;
1896 break;
1897 }
1898 if (chip->caps & ES18XX_SPATIALIZER) {
1899 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1900 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1901 return err;
1902 }
1903 }
1904 if (chip->caps & ES18XX_HWV) {
1905 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001906 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1908 if (idx == 0)
1909 chip->hw_volume = kctl;
1910 else
1911 chip->hw_switch = kctl;
1912 kctl->private_free = snd_es18xx_hwv_free;
1913 if ((err = snd_ctl_add(card, kctl)) < 0)
1914 return err;
1915
1916 }
1917 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001918 /* finish initializing other chipset specific controls
1919 */
1920 if (chip->version != 0x1868) {
1921 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1922 chip));
1923 if (err < 0)
1924 return err;
1925 }
1926 if (chip->version == 0x1869) {
1927 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1928 err = snd_ctl_add(card,
1929 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1930 chip));
1931 if (err < 0)
1932 return err;
1933 }
Mark Salazar95b71292006-01-16 11:35:40 +01001934 } else if (chip->version == 0x1878) {
1935 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1936 chip));
1937 if (err < 0)
1938 return err;
1939 } else if (chip->version == 0x1879) {
1940 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1941 err = snd_ctl_add(card,
1942 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1943 chip));
1944 if (err < 0)
1945 return err;
1946 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
1949}
1950
1951
1952/* Card level */
1953
1954MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1955MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1956MODULE_LICENSE("GPL");
1957MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1958 "{ESS,ES1869 PnP AudioDrive},"
1959 "{ESS,ES1878 PnP AudioDrive},"
1960 "{ESS,ES1879 PnP AudioDrive},"
1961 "{ESS,ES1887 PnP AudioDrive},"
1962 "{ESS,ES1888 PnP AudioDrive},"
1963 "{ESS,ES1887 AudioDrive},"
1964 "{ESS,ES1888 AudioDrive}}");
1965
1966static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1967static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1968static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1969#ifdef CONFIG_PNP
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001970static int isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#endif
1972static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1973#ifndef CONFIG_PNP
1974static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1975#else
1976static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1977#endif
1978static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1979static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1980static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1981static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1982
1983module_param_array(index, int, NULL, 0444);
1984MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1985module_param_array(id, charp, NULL, 0444);
1986MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1987module_param_array(enable, bool, NULL, 0444);
1988MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1989#ifdef CONFIG_PNP
1990module_param_array(isapnp, bool, NULL, 0444);
1991MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1992#endif
1993module_param_array(port, long, NULL, 0444);
1994MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1995module_param_array(mpu_port, long, NULL, 0444);
1996MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1997module_param_array(fm_port, long, NULL, 0444);
1998MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1999module_param_array(irq, int, NULL, 0444);
2000MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2001module_param_array(dma1, int, NULL, 0444);
2002MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2003module_param_array(dma2, int, NULL, 0444);
2004MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002007static int isa_registered;
2008static int pnp_registered;
2009static int pnpc_registered;
Ondrej Zary1c398552006-07-31 12:51:57 +02002010
2011static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2012 { .id = "ESS1869" },
Rene Herman4848ffe2007-08-01 23:50:21 +02002013 { .id = "ESS1879" },
Ondrej Zary1c398552006-07-31 12:51:57 +02002014 { .id = "" } /* end */
2015};
2016
2017MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2018
2019/* PnP main device initialization */
Rene Herman109c53f842007-11-30 17:59:25 +01002020static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c398552006-07-31 12:51:57 +02002021{
Rene Herman109c53f842007-11-30 17:59:25 +01002022 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c398552006-07-31 12:51:57 +02002023 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2024 return -EBUSY;
2025 }
2026 /* ok. hack using Vendor-Defined Card-Level registers */
2027 /* skip csn and logdev initialization - already done in isapnp_configure */
2028 if (pnp_device_is_isapnp(pdev)) {
2029 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2030 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2031 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2032 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2033 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2034 isapnp_cfg_end();
2035 }
2036 port[dev] = pnp_port_start(pdev, 0);
2037 fm_port[dev] = pnp_port_start(pdev, 1);
2038 mpu_port[dev] = pnp_port_start(pdev, 2);
2039 dma1[dev] = pnp_dma(pdev, 0);
2040 dma2[dev] = pnp_dma(pdev, 1);
2041 irq[dev] = pnp_irq(pdev, 0);
2042 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2043 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2044 return 0;
2045}
2046
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002047static int __devinit snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
Ondrej Zary1c398552006-07-31 12:51:57 +02002048 struct pnp_dev *pdev)
2049{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002050 chip->dev = pdev;
2051 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Ondrej Zary1c398552006-07-31 12:51:57 +02002052 return -EBUSY;
Ondrej Zary1c398552006-07-31 12:51:57 +02002053 return 0;
2054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2057 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2058 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2059 /* ESS 1868 (integrated on Maxisound Cards) */
2060 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2061 /* ESS 1868 (integrated on Maxisound Cards) */
2062 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2063 /* ESS ES1869 Plug and Play AudioDrive */
2064 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2065 /* ESS 1869 */
2066 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2067 /* ESS 1878 */
2068 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2069 /* ESS 1879 */
2070 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2071 /* --- */
2072 { .id = "" } /* end */
2073};
2074
2075MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2076
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002077static int __devinit snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct pnp_card_link *card,
2079 const struct pnp_card_device_id *id)
2080{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002081 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2082 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002084
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002085 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2086 if (chip->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /* Control port initialization */
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002090 if (pnp_activate_dev(chip->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2092 return -EAGAIN;
2093 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002094 snd_printdd("pnp: port=0x%llx\n",
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002095 (unsigned long long)pnp_port_start(chip->devc, 0));
2096 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100}
2101#endif /* CONFIG_PNP */
2102
Takashi Iwai43bcd972005-09-05 17:19:20 +02002103#ifdef CONFIG_PNP
2104#define is_isapnp_selected(dev) isapnp[dev]
2105#else
2106#define is_isapnp_selected(dev) 0
2107#endif
2108
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002109static int snd_es18xx_card_new(int dev, struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002111 return snd_card_create(index[dev], id[dev], THIS_MODULE,
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002112 sizeof(struct snd_es18xx), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002113}
2114
2115static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2116{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002117 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002118 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int err;
2120
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002121 err = snd_es18xx_new_device(card,
2122 port[dev], mpu_port[dev], fm_port[dev],
2123 irq[dev], dma1[dev], dma2[dev]);
2124 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002130 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2132 card->shortname,
2133 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002134 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 else
2136 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2137 card->shortname,
2138 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002139 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002141 err = snd_es18xx_pcm(card, 0, NULL);
2142 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002143 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002144
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002145 err = snd_es18xx_mixer(card);
2146 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002150 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2151 OPL3_HW_OPL3, 0, &opl3) < 0) {
2152 snd_printk(KERN_WARNING PFX
2153 "opl3 not detected at 0x%lx\n",
2154 fm_port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 } else {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002156 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2157 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160 }
2161
2162 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002163 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2164 mpu_port[dev], 0,
2165 irq[dev], 0, &chip->rmidi);
2166 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002170 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002173static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2174{
2175 return enable[dev] && !is_isapnp_selected(dev);
2176}
2177
2178static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002180 struct snd_card *card;
2181 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002183 err = snd_es18xx_card_new(dev, &card);
2184 if (err < 0)
2185 return err;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002186 snd_card_set_dev(card, devptr);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002187 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2188 snd_card_free(card);
2189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002191 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002195static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002196{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002197 int err;
2198 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2199 static int possible_dmas[] = {1, 0, 3, 5, -1};
2200
2201 if (irq[dev] == SNDRV_AUTO_IRQ) {
2202 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2203 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2204 return -EBUSY;
2205 }
2206 }
2207 if (dma1[dev] == SNDRV_AUTO_DMA) {
2208 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2209 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2210 return -EBUSY;
2211 }
2212 }
2213 if (dma2[dev] == SNDRV_AUTO_DMA) {
2214 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2215 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2216 return -EBUSY;
2217 }
2218 }
2219
2220 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002221 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002222 } else {
2223 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2224 int i;
2225 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2226 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002227 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002228 if (! err)
2229 return 0;
2230 }
2231 return err;
2232 }
2233}
2234
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002235static int __devexit snd_es18xx_isa_remove(struct device *devptr,
2236 unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002237{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002238 snd_card_free(dev_get_drvdata(devptr));
2239 dev_set_drvdata(devptr, NULL);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002240 return 0;
2241}
2242
2243#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002244static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2245 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002246{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002247 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002248}
2249
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002250static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002251{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002252 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002253}
2254#endif
2255
Rene Herman83c51c02007-03-20 11:33:46 +01002256#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002257
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002258static struct isa_driver snd_es18xx_isa_driver = {
2259 .match = snd_es18xx_isa_match,
2260 .probe = snd_es18xx_isa_probe,
2261 .remove = __devexit_p(snd_es18xx_isa_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002262#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002263 .suspend = snd_es18xx_isa_suspend,
2264 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002265#endif
2266 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002267 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002268 },
2269};
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272#ifdef CONFIG_PNP
Ondrej Zary1c398552006-07-31 12:51:57 +02002273static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2274 const struct pnp_device_id *id)
2275{
2276 static int dev;
2277 int err;
2278 struct snd_card *card;
2279
2280 if (pnp_device_is_isapnp(pdev))
2281 return -ENOENT; /* we have another procedure - card */
2282 for (; dev < SNDRV_CARDS; dev++) {
2283 if (enable[dev] && isapnp[dev])
2284 break;
2285 }
2286 if (dev >= SNDRV_CARDS)
2287 return -ENODEV;
2288
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002289 err = snd_es18xx_card_new(dev, &card);
2290 if (err < 0)
2291 return err;
Ondrej Zary1c398552006-07-31 12:51:57 +02002292 if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2293 snd_card_free(card);
2294 return err;
2295 }
2296 snd_card_set_dev(card, &pdev->dev);
2297 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2298 snd_card_free(card);
2299 return err;
2300 }
2301 pnp_set_drvdata(pdev, card);
2302 dev++;
Ondrej Zary1c398552006-07-31 12:51:57 +02002303 return 0;
2304}
2305
2306static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2307{
2308 snd_card_free(pnp_get_drvdata(pdev));
2309 pnp_set_drvdata(pdev, NULL);
2310}
2311
2312#ifdef CONFIG_PM
2313static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2314{
2315 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2316}
2317static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2318{
2319 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2320}
2321#endif
2322
2323static struct pnp_driver es18xx_pnp_driver = {
2324 .name = "es18xx-pnpbios",
2325 .id_table = snd_audiodrive_pnpbiosids,
2326 .probe = snd_audiodrive_pnp_detect,
2327 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2328#ifdef CONFIG_PM
2329 .suspend = snd_audiodrive_pnp_suspend,
2330 .resume = snd_audiodrive_pnp_resume,
2331#endif
2332};
2333
2334static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002335 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002338 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 int res;
2340
2341 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002342 if (enable[dev] && isapnp[dev])
2343 break;
2344 }
2345 if (dev >= SNDRV_CARDS)
2346 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002348 res = snd_es18xx_card_new(dev, &card);
2349 if (res < 0)
2350 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002351
Ondrej Zary1c398552006-07-31 12:51:57 +02002352 if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002353 snd_card_free(card);
2354 return res;
2355 }
2356 snd_card_set_dev(card, &pcard->card->dev);
2357 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2358 snd_card_free(card);
2359 return res;
2360 }
2361
2362 pnp_set_card_drvdata(pcard, card);
2363 dev++;
2364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Ondrej Zary1c398552006-07-31 12:51:57 +02002367static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002369 snd_card_free(pnp_get_card_drvdata(pcard));
2370 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002373#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002374static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002375{
2376 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2377}
2378
Ondrej Zary1c398552006-07-31 12:51:57 +02002379static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002380{
2381 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2382}
2383
2384#endif
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386static struct pnp_card_driver es18xx_pnpc_driver = {
2387 .flags = PNP_DRIVER_RES_DISABLE,
2388 .name = "es18xx",
2389 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c398552006-07-31 12:51:57 +02002390 .probe = snd_audiodrive_pnpc_detect,
2391 .remove = __devexit_p(snd_audiodrive_pnpc_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002392#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002393 .suspend = snd_audiodrive_pnpc_suspend,
2394 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396};
2397#endif /* CONFIG_PNP */
2398
2399static int __init alsa_card_es18xx_init(void)
2400{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002401 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002403 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002404#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002405 if (!err)
2406 isa_registered = 1;
2407
Ondrej Zary1c398552006-07-31 12:51:57 +02002408 err = pnp_register_driver(&es18xx_pnp_driver);
2409 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002410 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002411
Ondrej Zary1c398552006-07-31 12:51:57 +02002412 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2413 if (!err)
2414 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002415
2416 if (isa_registered || pnp_registered)
2417 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002418#endif
Rene Herman609d7692007-05-15 11:42:56 +02002419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
2422static void __exit alsa_card_es18xx_exit(void)
2423{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002424#ifdef CONFIG_PNP
2425 if (pnpc_registered)
2426 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2427 if (pnp_registered)
2428 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002429 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002430#endif
Rene Herman609d7692007-05-15 11:42:56 +02002431 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
2434module_init(alsa_card_es18xx_init)
2435module_exit(alsa_card_es18xx_exit)