blob: ece504170aa988c6c384fa801222f4755d06153f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/* GENERAL NOTES:
23 *
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
Mark Salazar95b71292006-01-16 11:35:40 +010052 * - Zoom Video is implemented by sharing the FM DAC, thus the user can
53 * have either FM playback or Video playback but not both simultaneously.
54 * The Video Playback Switch mixer control toggles this choice.
55 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * BUGS:
57 *
58 * - There is a major trouble I noted:
59 *
60 * using both channel for playback stereo 16 bit samples at 44100 Hz
61 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
62 *
63 * The same happens using Audio1 for captureing.
64 *
65 * The Windows driver does not suffer of this (although it use Audio1
66 * only for captureing). I'm unable to discover why.
67 *
68 */
69
Mark Salazar95b71292006-01-16 11:35:40 +010070/*
71 * ES1879 NOTES:
72 * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
73 * seems to be effected (speaker_test plays a lower frequency). Can't find
74 * anything in the datasheet to account for this, so a Video Playback Switch
75 * control has been included to allow ZV to be enabled only when necessary.
76 * Then again on at least one test system the 0x71 bit 6 enable bit is not
77 * needed for ZV, so maybe the datasheet is entirely wrong here.
78 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010082#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010083#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/slab.h>
85#include <linux/pnp.h>
86#include <linux/isapnp.h>
87#include <linux/moduleparam.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070088#include <linux/delay.h>
89
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010090#include <asm/io.h>
91#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <sound/core.h>
93#include <sound/control.h>
94#include <sound/pcm.h>
95#include <sound/pcm_params.h>
96#include <sound/mpu401.h>
97#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define SNDRV_LEGACY_FIND_FREE_IRQ
99#define SNDRV_LEGACY_FIND_FREE_DMA
100#include <sound/initval.h>
101
102#define PFX "es18xx: "
103
Takashi Iwai8047c912005-11-17 14:41:22 +0100104struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned long port; /* port of ESS chip */
106 unsigned long mpu_port; /* MPU-401 port of ESS chip */
107 unsigned long fm_port; /* FM port */
108 unsigned long ctrl_port; /* Control port of ESS chip */
109 struct resource *res_port;
110 struct resource *res_mpu_port;
111 struct resource *res_ctrl_port;
112 int irq; /* IRQ number of ESS chip */
113 int dma1; /* DMA1 */
114 int dma2; /* DMA2 */
115 unsigned short version; /* version of ESS chip */
116 int caps; /* Chip capabilities */
117 unsigned short audio2_vol; /* volume level of audio2 */
118
119 unsigned short active; /* active channel mask */
120 unsigned int dma1_size;
121 unsigned int dma2_size;
122 unsigned int dma1_shift;
123 unsigned int dma2_shift;
124
Takashi Iwai8047c912005-11-17 14:41:22 +0100125 struct snd_card *card;
126 struct snd_pcm *pcm;
127 struct snd_pcm_substream *playback_a_substream;
128 struct snd_pcm_substream *capture_a_substream;
129 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwai8047c912005-11-17 14:41:22 +0100131 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Takashi Iwai8047c912005-11-17 14:41:22 +0100133 struct snd_kcontrol *hw_volume;
134 struct snd_kcontrol *hw_switch;
135 struct snd_kcontrol *master_volume;
136 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 spinlock_t reg_lock;
139 spinlock_t mixer_lock;
140 spinlock_t ctrl_lock;
141#ifdef CONFIG_PM
142 unsigned char pm_reg;
143#endif
144};
145
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100146struct snd_audiodrive {
147 struct snd_es18xx *chip;
148#ifdef CONFIG_PNP
149 struct pnp_dev *dev;
150 struct pnp_dev *devc;
151#endif
152};
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define AUDIO1_IRQ 0x01
155#define AUDIO2_IRQ 0x02
156#define HWV_IRQ 0x04
157#define MPU_IRQ 0x08
158
159#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
160#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
161#define ES18XX_RECMIX 0x0004 /* Has record mixer */
162#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
163#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
164#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
165#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100166#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
168#define ES18XX_I2S 0x0200 /* I2S mixer control */
169#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
170#define ES18XX_CONTROL 0x0800 /* Has control ports */
171
172/* Power Management */
173#define ES18XX_PM 0x07
174#define ES18XX_PM_GPO0 0x01
175#define ES18XX_PM_GPO1 0x02
176#define ES18XX_PM_PDR 0x04
177#define ES18XX_PM_ANA 0x08
178#define ES18XX_PM_FM 0x020
179#define ES18XX_PM_SUS 0x080
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Lowlevel */
182
183#define DAC1 0x01
184#define ADC1 0x02
185#define DAC2 0x04
186#define MILLISECOND 10000
187
Takashi Iwai8047c912005-11-17 14:41:22 +0100188static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int i;
191
192 for(i = MILLISECOND; i; i--)
193 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
194 outb(val, chip->port + 0x0C);
195 return 0;
196 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200197 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
199}
200
Takashi Iwai8047c912005-11-17 14:41:22 +0100201static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204
205 for(i = MILLISECOND/10; i; i--)
206 if (inb(chip->port + 0x0C) & 0x40)
207 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200208 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
209 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -ENODEV;
211}
212
213#undef REG_DEBUG
214
Takashi Iwai8047c912005-11-17 14:41:22 +0100215static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned char reg, unsigned char data)
217{
218 unsigned long flags;
219 int ret;
220
221 spin_lock_irqsave(&chip->reg_lock, flags);
222 ret = snd_es18xx_dsp_command(chip, reg);
223 if (ret < 0)
224 goto end;
225 ret = snd_es18xx_dsp_command(chip, data);
226 end:
227 spin_unlock_irqrestore(&chip->reg_lock, flags);
228#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200229 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#endif
231 return ret;
232}
233
Takashi Iwai8047c912005-11-17 14:41:22 +0100234static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned long flags;
237 int ret, data;
238 spin_lock_irqsave(&chip->reg_lock, flags);
239 ret = snd_es18xx_dsp_command(chip, 0xC0);
240 if (ret < 0)
241 goto end;
242 ret = snd_es18xx_dsp_command(chip, reg);
243 if (ret < 0)
244 goto end;
245 data = snd_es18xx_dsp_get_byte(chip);
246 ret = data;
247#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200248 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#endif
250 end:
251 spin_unlock_irqrestore(&chip->reg_lock, flags);
252 return ret;
253}
254
255/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100256static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned char mask, unsigned char val)
258{
259 int ret;
260 unsigned char old, new, oval;
261 unsigned long flags;
262 spin_lock_irqsave(&chip->reg_lock, flags);
263 ret = snd_es18xx_dsp_command(chip, 0xC0);
264 if (ret < 0)
265 goto end;
266 ret = snd_es18xx_dsp_command(chip, reg);
267 if (ret < 0)
268 goto end;
269 ret = snd_es18xx_dsp_get_byte(chip);
270 if (ret < 0) {
271 goto end;
272 }
273 old = ret;
274 oval = old & mask;
275 if (val != oval) {
276 ret = snd_es18xx_dsp_command(chip, reg);
277 if (ret < 0)
278 goto end;
279 new = (old & ~mask) | (val & mask);
280 ret = snd_es18xx_dsp_command(chip, new);
281 if (ret < 0)
282 goto end;
283#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200284 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
285 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
287 }
288 ret = oval;
289 end:
290 spin_unlock_irqrestore(&chip->reg_lock, flags);
291 return ret;
292}
293
Takashi Iwai8047c912005-11-17 14:41:22 +0100294static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned char reg, unsigned char data)
296{
297 unsigned long flags;
298 spin_lock_irqsave(&chip->mixer_lock, flags);
299 outb(reg, chip->port + 0x04);
300 outb(data, chip->port + 0x05);
301 spin_unlock_irqrestore(&chip->mixer_lock, flags);
302#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200303 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif
305}
306
Takashi Iwai8047c912005-11-17 14:41:22 +0100307static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long flags;
310 int data;
311 spin_lock_irqsave(&chip->mixer_lock, flags);
312 outb(reg, chip->port + 0x04);
313 data = inb(chip->port + 0x05);
314 spin_unlock_irqrestore(&chip->mixer_lock, flags);
315#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200316 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#endif
318 return data;
319}
320
321/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100322static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned char mask, unsigned char val)
324{
325 unsigned char old, new, oval;
326 unsigned long flags;
327 spin_lock_irqsave(&chip->mixer_lock, flags);
328 outb(reg, chip->port + 0x04);
329 old = inb(chip->port + 0x05);
330 oval = old & mask;
331 if (val != oval) {
332 new = (old & ~mask) | (val & mask);
333 outb(new, chip->port + 0x05);
334#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200335 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
336 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#endif
338 }
339 spin_unlock_irqrestore(&chip->mixer_lock, flags);
340 return oval;
341}
342
Takashi Iwai8047c912005-11-17 14:41:22 +0100343static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned char mask)
345{
346 int old, expected, new;
347 unsigned long flags;
348 spin_lock_irqsave(&chip->mixer_lock, flags);
349 outb(reg, chip->port + 0x04);
350 old = inb(chip->port + 0x05);
351 expected = old ^ mask;
352 outb(expected, chip->port + 0x05);
353 new = inb(chip->port + 0x05);
354 spin_unlock_irqrestore(&chip->mixer_lock, flags);
355#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200356 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
357 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#endif
359 return expected == new;
360}
361
362
Takashi Iwai8047c912005-11-17 14:41:22 +0100363static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 int i;
366 outb(0x03, chip->port + 0x06);
367 inb(chip->port + 0x06);
368 outb(0x00, chip->port + 0x06);
369 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
370 if (inb(chip->port + 0x0A) != 0xAA)
371 return -1;
372 return 0;
373}
374
Takashi Iwai8047c912005-11-17 14:41:22 +0100375static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 outb(0x02, chip->port + 0x06);
378 inb(chip->port + 0x06);
379 outb(0x00, chip->port + 0x06);
380 return 0;
381}
382
Takashi Iwai8047c912005-11-17 14:41:22 +0100383static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 {
385 .num = 793800,
386 .den_min = 1,
387 .den_max = 128,
388 .den_step = 1,
389 },
390 {
391 .num = 768000,
392 .den_min = 1,
393 .den_max = 128,
394 .den_step = 1,
395 }
396};
397
Takashi Iwai8047c912005-11-17 14:41:22 +0100398static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .nrats = 2,
400 .rats = new_clocks,
401};
402
Takashi Iwai8047c912005-11-17 14:41:22 +0100403static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 {
405 .num = 795444,
406 .den_min = 1,
407 .den_max = 128,
408 .den_step = 1,
409 },
410 {
411 .num = 397722,
412 .den_min = 1,
413 .den_max = 128,
414 .den_step = 1,
415 }
416};
417
Takashi Iwai8047c912005-11-17 14:41:22 +0100418static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .nrats = 2,
420 .rats = old_clocks,
421};
422
423
Takashi Iwai8047c912005-11-17 14:41:22 +0100424static void snd_es18xx_rate_set(struct snd_es18xx *chip,
425 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int mode)
427{
428 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100429 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (chip->caps & ES18XX_NEW_RATE) {
431 if (runtime->rate_num == new_clocks[0].num)
432 bits = 128 - runtime->rate_den;
433 else
434 bits = 256 - runtime->rate_den;
435 } else {
436 if (runtime->rate_num == old_clocks[0].num)
437 bits = 256 - runtime->rate_den;
438 else
439 bits = 128 - runtime->rate_den;
440 }
441
442 /* set filter register */
443 div0 = 256 - 7160000*20/(8*82*runtime->rate);
444
445 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
446 snd_es18xx_mixer_write(chip, 0x70, bits);
447 /*
448 * Comment from kernel oss driver:
449 * FKS: fascinating: 0x72 doesn't seem to work.
450 */
451 snd_es18xx_write(chip, 0xA2, div0);
452 snd_es18xx_mixer_write(chip, 0x72, div0);
453 } else {
454 snd_es18xx_write(chip, 0xA1, bits);
455 snd_es18xx_write(chip, 0xA2, div0);
456 }
457}
458
Takashi Iwai8047c912005-11-17 14:41:22 +0100459static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwai8047c912005-11-17 14:41:22 +0100462 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int shift, err;
464
465 shift = 0;
466 if (params_channels(hw_params) == 2)
467 shift++;
468 if (snd_pcm_format_width(params_format(hw_params)) == 16)
469 shift++;
470
471 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
472 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
473 (chip->capture_a_substream) &&
474 params_channels(hw_params) != 1) {
475 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
476 return -EBUSY;
477 }
478 chip->dma2_shift = shift;
479 } else {
480 chip->dma1_shift = shift;
481 }
482 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
483 return err;
484 return 0;
485}
486
Takashi Iwai8047c912005-11-17 14:41:22 +0100487static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 return snd_pcm_lib_free_pages(substream);
490}
491
Takashi Iwai8047c912005-11-17 14:41:22 +0100492static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
493 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwai8047c912005-11-17 14:41:22 +0100495 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
497 unsigned int count = snd_pcm_lib_period_bytes(substream);
498
499 chip->dma2_size = size;
500
501 snd_es18xx_rate_set(chip, substream, DAC2);
502
503 /* Transfer Count Reload */
504 count = 0x10000 - count;
505 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
506 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
507
508 /* Set format */
509 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
510 ((runtime->channels == 1) ? 0x00 : 0x02) |
511 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
512 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
513
514 /* Set DMA controller */
515 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
516
517 return 0;
518}
519
Takashi Iwai8047c912005-11-17 14:41:22 +0100520static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
521 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int cmd)
523{
524 switch (cmd) {
525 case SNDRV_PCM_TRIGGER_START:
526 case SNDRV_PCM_TRIGGER_RESUME:
527 if (chip->active & DAC2)
528 return 0;
529 chip->active |= DAC2;
530 /* Start DMA */
531 if (chip->dma2 >= 4)
532 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
533 else
534 snd_es18xx_mixer_write(chip, 0x78, 0x93);
535#ifdef AVOID_POPS
536 /* Avoid pops */
537 udelay(100000);
538 if (chip->caps & ES18XX_PCM2)
539 /* Restore Audio 2 volume */
540 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
541 else
542 /* Enable PCM output */
543 snd_es18xx_dsp_command(chip, 0xD1);
544#endif
545 break;
546 case SNDRV_PCM_TRIGGER_STOP:
547 case SNDRV_PCM_TRIGGER_SUSPEND:
548 if (!(chip->active & DAC2))
549 return 0;
550 chip->active &= ~DAC2;
551 /* Stop DMA */
552 snd_es18xx_mixer_write(chip, 0x78, 0x00);
553#ifdef AVOID_POPS
554 udelay(25000);
555 if (chip->caps & ES18XX_PCM2)
556 /* Set Audio 2 volume to 0 */
557 snd_es18xx_mixer_write(chip, 0x7C, 0);
558 else
559 /* Disable PCM output */
560 snd_es18xx_dsp_command(chip, 0xD3);
561#endif
562 break;
563 default:
564 return -EINVAL;
565 }
566
567 return 0;
568}
569
Takashi Iwai8047c912005-11-17 14:41:22 +0100570static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
571 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai8047c912005-11-17 14:41:22 +0100573 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int shift, err;
575
576 shift = 0;
577 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
578 chip->playback_a_substream &&
579 params_channels(hw_params) != 1) {
580 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
581 return -EBUSY;
582 }
583 if (params_channels(hw_params) == 2)
584 shift++;
585 if (snd_pcm_format_width(params_format(hw_params)) == 16)
586 shift++;
587 chip->dma1_shift = shift;
588 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
589 return err;
590 return 0;
591}
592
Takashi Iwai8047c912005-11-17 14:41:22 +0100593static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Takashi Iwai8047c912005-11-17 14:41:22 +0100595 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
596 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
598 unsigned int count = snd_pcm_lib_period_bytes(substream);
599
600 chip->dma1_size = size;
601
602 snd_es18xx_reset_fifo(chip);
603
604 /* Set stereo/mono */
605 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
606
607 snd_es18xx_rate_set(chip, substream, ADC1);
608
609 /* Transfer Count Reload */
610 count = 0x10000 - count;
611 snd_es18xx_write(chip, 0xA4, count & 0xff);
612 snd_es18xx_write(chip, 0xA5, count >> 8);
613
614#ifdef AVOID_POPS
615 udelay(100000);
616#endif
617
618 /* Set format */
619 snd_es18xx_write(chip, 0xB7,
620 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
621 snd_es18xx_write(chip, 0xB7, 0x90 |
622 ((runtime->channels == 1) ? 0x40 : 0x08) |
623 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
624 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
625
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200626 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
628
629 return 0;
630}
631
Takashi Iwai8047c912005-11-17 14:41:22 +0100632static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int cmd)
634{
Takashi Iwai8047c912005-11-17 14:41:22 +0100635 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 switch (cmd) {
638 case SNDRV_PCM_TRIGGER_START:
639 case SNDRV_PCM_TRIGGER_RESUME:
640 if (chip->active & ADC1)
641 return 0;
642 chip->active |= ADC1;
643 /* Start DMA */
644 snd_es18xx_write(chip, 0xB8, 0x0f);
645 break;
646 case SNDRV_PCM_TRIGGER_STOP:
647 case SNDRV_PCM_TRIGGER_SUSPEND:
648 if (!(chip->active & ADC1))
649 return 0;
650 chip->active &= ~ADC1;
651 /* Stop DMA */
652 snd_es18xx_write(chip, 0xB8, 0x00);
653 break;
654 default:
655 return -EINVAL;
656 }
657
658 return 0;
659}
660
Takashi Iwai8047c912005-11-17 14:41:22 +0100661static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
662 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai8047c912005-11-17 14:41:22 +0100664 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
666 unsigned int count = snd_pcm_lib_period_bytes(substream);
667
668 chip->dma1_size = size;
669
670 snd_es18xx_reset_fifo(chip);
671
672 /* Set stereo/mono */
673 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
674
675 snd_es18xx_rate_set(chip, substream, DAC1);
676
677 /* Transfer Count Reload */
678 count = 0x10000 - count;
679 snd_es18xx_write(chip, 0xA4, count & 0xff);
680 snd_es18xx_write(chip, 0xA5, count >> 8);
681
682 /* Set format */
683 snd_es18xx_write(chip, 0xB6,
684 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
685 snd_es18xx_write(chip, 0xB7,
686 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
687 snd_es18xx_write(chip, 0xB7, 0x90 |
688 (runtime->channels == 1 ? 0x40 : 0x08) |
689 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
690 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
691
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200692 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
694
695 return 0;
696}
697
Takashi Iwai8047c912005-11-17 14:41:22 +0100698static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
699 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int cmd)
701{
702 switch (cmd) {
703 case SNDRV_PCM_TRIGGER_START:
704 case SNDRV_PCM_TRIGGER_RESUME:
705 if (chip->active & DAC1)
706 return 0;
707 chip->active |= DAC1;
708 /* Start DMA */
709 snd_es18xx_write(chip, 0xB8, 0x05);
710#ifdef AVOID_POPS
711 /* Avoid pops */
712 udelay(100000);
713 /* Enable Audio 1 */
714 snd_es18xx_dsp_command(chip, 0xD1);
715#endif
716 break;
717 case SNDRV_PCM_TRIGGER_STOP:
718 case SNDRV_PCM_TRIGGER_SUSPEND:
719 if (!(chip->active & DAC1))
720 return 0;
721 chip->active &= ~DAC1;
722 /* Stop DMA */
723 snd_es18xx_write(chip, 0xB8, 0x00);
724#ifdef AVOID_POPS
725 /* Avoid pops */
726 udelay(25000);
727 /* Disable Audio 1 */
728 snd_es18xx_dsp_command(chip, 0xD3);
729#endif
730 break;
731 default:
732 return -EINVAL;
733 }
734
735 return 0;
736}
737
Takashi Iwai8047c912005-11-17 14:41:22 +0100738static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Takashi Iwai8047c912005-11-17 14:41:22 +0100740 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
742 return snd_es18xx_playback1_prepare(chip, substream);
743 else
744 return snd_es18xx_playback2_prepare(chip, substream);
745}
746
Takashi Iwai8047c912005-11-17 14:41:22 +0100747static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int cmd)
749{
Takashi Iwai8047c912005-11-17 14:41:22 +0100750 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
752 return snd_es18xx_playback1_trigger(chip, substream, cmd);
753 else
754 return snd_es18xx_playback2_trigger(chip, substream, cmd);
755}
756
David Howells7d12e782006-10-05 14:55:46 +0100757static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Takashi Iwai8047c912005-11-17 14:41:22 +0100759 struct snd_es18xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned char status;
761
762 if (chip->caps & ES18XX_CONTROL) {
763 /* Read Interrupt status */
764 status = inb(chip->ctrl_port + 6);
765 } else {
766 /* Read Interrupt status */
767 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
768 }
769#if 0
770 else {
771 status = 0;
772 if (inb(chip->port + 0x0C) & 0x01)
773 status |= AUDIO1_IRQ;
774 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
775 status |= AUDIO2_IRQ;
776 if ((chip->caps & ES18XX_HWV) &&
777 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
778 status |= HWV_IRQ;
779 }
780#endif
781
782 /* Audio 1 & Audio 2 */
783 if (status & AUDIO2_IRQ) {
784 if (chip->active & DAC2)
785 snd_pcm_period_elapsed(chip->playback_a_substream);
786 /* ack interrupt */
787 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
788 }
789 if (status & AUDIO1_IRQ) {
790 /* ok.. capture is active */
791 if (chip->active & ADC1)
792 snd_pcm_period_elapsed(chip->capture_a_substream);
793 /* ok.. playback2 is active */
794 else if (chip->active & DAC1)
795 snd_pcm_period_elapsed(chip->playback_b_substream);
796 /* ack interrupt */
797 inb(chip->port + 0x0E);
798 }
799
800 /* MPU */
801 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100802 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* Hardware volume */
805 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100806 int split = 0;
807 if (chip->caps & ES18XX_HWV) {
808 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
809 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
810 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!split) {
813 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
814 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
815 }
816 /* ack interrupt */
817 snd_es18xx_mixer_write(chip, 0x66, 0x00);
818 }
819 return IRQ_HANDLED;
820}
821
Takashi Iwai8047c912005-11-17 14:41:22 +0100822static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai8047c912005-11-17 14:41:22 +0100824 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int pos;
826
827 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
828 if (!(chip->active & DAC2))
829 return 0;
830 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
831 return pos >> chip->dma2_shift;
832 } else {
833 if (!(chip->active & DAC1))
834 return 0;
835 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
836 return pos >> chip->dma1_shift;
837 }
838}
839
Takashi Iwai8047c912005-11-17 14:41:22 +0100840static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai8047c912005-11-17 14:41:22 +0100842 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int pos;
844
845 if (!(chip->active & ADC1))
846 return 0;
847 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
848 return pos >> chip->dma1_shift;
849}
850
Takashi Iwai8047c912005-11-17 14:41:22 +0100851static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
854 SNDRV_PCM_INFO_RESUME |
855 SNDRV_PCM_INFO_MMAP_VALID),
856 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
857 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
858 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
859 .rate_min = 4000,
860 .rate_max = 48000,
861 .channels_min = 1,
862 .channels_max = 2,
863 .buffer_bytes_max = 65536,
864 .period_bytes_min = 64,
865 .period_bytes_max = 65536,
866 .periods_min = 1,
867 .periods_max = 1024,
868 .fifo_size = 0,
869};
870
Takashi Iwai8047c912005-11-17 14:41:22 +0100871static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
874 SNDRV_PCM_INFO_RESUME |
875 SNDRV_PCM_INFO_MMAP_VALID),
876 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
877 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
878 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
879 .rate_min = 4000,
880 .rate_max = 48000,
881 .channels_min = 1,
882 .channels_max = 2,
883 .buffer_bytes_max = 65536,
884 .period_bytes_min = 64,
885 .period_bytes_max = 65536,
886 .periods_min = 1,
887 .periods_max = 1024,
888 .fifo_size = 0,
889};
890
Takashi Iwai8047c912005-11-17 14:41:22 +0100891static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Takashi Iwai8047c912005-11-17 14:41:22 +0100893 struct snd_pcm_runtime *runtime = substream->runtime;
894 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
897 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
898 chip->capture_a_substream &&
899 chip->capture_a_substream->runtime->channels != 1)
900 return -EAGAIN;
901 chip->playback_a_substream = substream;
902 } else if (substream->number <= 1) {
903 if (chip->capture_a_substream)
904 return -EAGAIN;
905 chip->playback_b_substream = substream;
906 } else {
907 snd_BUG();
908 return -EINVAL;
909 }
910 substream->runtime->hw = snd_es18xx_playback;
911 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
912 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
913 return 0;
914}
915
Takashi Iwai8047c912005-11-17 14:41:22 +0100916static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwai8047c912005-11-17 14:41:22 +0100918 struct snd_pcm_runtime *runtime = substream->runtime;
919 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (chip->playback_b_substream)
922 return -EAGAIN;
923 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
924 chip->playback_a_substream &&
925 chip->playback_a_substream->runtime->channels != 1)
926 return -EAGAIN;
927 chip->capture_a_substream = substream;
928 substream->runtime->hw = snd_es18xx_capture;
929 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
930 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
931 return 0;
932}
933
Takashi Iwai8047c912005-11-17 14:41:22 +0100934static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Takashi Iwai8047c912005-11-17 14:41:22 +0100936 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
939 chip->playback_a_substream = NULL;
940 else
941 chip->playback_b_substream = NULL;
942
943 snd_pcm_lib_free_pages(substream);
944 return 0;
945}
946
Takashi Iwai8047c912005-11-17 14:41:22 +0100947static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai8047c912005-11-17 14:41:22 +0100949 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 chip->capture_a_substream = NULL;
952 snd_pcm_lib_free_pages(substream);
953 return 0;
954}
955
956/*
957 * MIXER part
958 */
959
Mark Salazarf4df2212006-01-16 11:31:14 +0100960/* Record source mux routines:
961 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
962 * bit table for the 4/5 source mux:
963 * reg 1C:
964 * b2 b1 b0 muxSource
965 * x 0 x microphone
966 * 0 1 x CD
967 * 1 1 0 line
968 * 1 1 1 mixer
969 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
970 * either the play mixer or the capture mixer.
971 *
972 * "map4Source" translates from source number to reg bit pattern
973 * "invMap4Source" translates from reg bit pattern to source number
974 */
975
Takashi Iwai8047c912005-11-17 14:41:22 +0100976static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Mark Salazarf4df2212006-01-16 11:31:14 +0100978 static char *texts4Source[4] = {
979 "Mic", "CD", "Line", "Master"
980 };
981 static char *texts5Source[5] = {
982 "Mic", "CD", "Line", "Master", "Mix"
983 };
984 static char *texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 "Mic", "Mic Master", "CD", "AOUT",
986 "Mic1", "Mix", "Line", "Master"
987 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100988 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
991 uinfo->count = 1;
Mark Salazarf4df2212006-01-16 11:31:14 +0100992 switch (chip->version) {
993 case 0x1868:
994 case 0x1878:
995 uinfo->value.enumerated.items = 4;
996 if (uinfo->value.enumerated.item > 3)
997 uinfo->value.enumerated.item = 3;
998 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
999 break;
1000 case 0x1887:
1001 case 0x1888:
1002 uinfo->value.enumerated.items = 5;
1003 if (uinfo->value.enumerated.item > 4)
1004 uinfo->value.enumerated.item = 4;
1005 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
1006 break;
1007 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
1008 case 0x1879:
1009 uinfo->value.enumerated.items = 8;
1010 if (uinfo->value.enumerated.item > 7)
1011 uinfo->value.enumerated.item = 7;
1012 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1013 break;
1014 default:
1015 return -EINVAL;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
1019
Takashi Iwai8047c912005-11-17 14:41:22 +01001020static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Mark Salazarf4df2212006-01-16 11:31:14 +01001022 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +01001023 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +01001024 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1025 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1026 muxSource = invMap4Source[muxSource];
1027 if (muxSource==3 &&
1028 (chip->version == 0x1887 || chip->version == 0x1888) &&
1029 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1030 )
1031 muxSource = 4;
1032 }
1033 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
1035}
1036
Takashi Iwai8047c912005-11-17 14:41:22 +01001037static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Mark Salazarf4df2212006-01-16 11:31:14 +01001039 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001040 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001042 unsigned char retVal = 0;
1043
1044 switch (chip->version) {
1045 /* 5 source chips */
1046 case 0x1887:
1047 case 0x1888:
1048 if (val > 4)
1049 return -EINVAL;
1050 if (val == 4) {
1051 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1052 val = 3;
1053 } else
1054 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1055 /* 4 source chips */
1056 case 0x1868:
1057 case 0x1878:
1058 if (val > 3)
1059 return -EINVAL;
1060 val = map4Source[val];
1061 break;
1062 /* 8 source chips */
1063 case 0x1869:
1064 case 0x1879:
1065 if (val > 7)
1066 return -EINVAL;
1067 break;
1068 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001070 }
1071 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001074#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Takashi Iwai8047c912005-11-17 14:41:22 +01001076static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai8047c912005-11-17 14:41:22 +01001078 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1080 ucontrol->value.integer.value[0] = !!(val & 8);
1081 return 0;
1082}
1083
Takashi Iwai8047c912005-11-17 14:41:22 +01001084static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Takashi Iwai8047c912005-11-17 14:41:22 +01001086 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unsigned char oval, nval;
1088 int change;
1089 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1090 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1091 change = nval != oval;
1092 if (change) {
1093 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1094 snd_es18xx_mixer_write(chip, 0x50, nval);
1095 }
1096 return change;
1097}
1098
Takashi Iwai8047c912005-11-17 14:41:22 +01001099static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1102 uinfo->count = 2;
1103 uinfo->value.integer.min = 0;
1104 uinfo->value.integer.max = 63;
1105 return 0;
1106}
1107
Takashi Iwai8047c912005-11-17 14:41:22 +01001108static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Takashi Iwai8047c912005-11-17 14:41:22 +01001110 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1112 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1113 return 0;
1114}
1115
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001116#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Takashi Iwai8047c912005-11-17 14:41:22 +01001118static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Takashi Iwai8047c912005-11-17 14:41:22 +01001120 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1122 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1123 return 0;
1124}
1125
Takashi Iwai8047c912005-11-17 14:41:22 +01001126static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Takashi Iwai8047c912005-11-17 14:41:22 +01001128 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 chip->master_volume = NULL;
1130 chip->master_switch = NULL;
1131 chip->hw_volume = NULL;
1132 chip->hw_switch = NULL;
1133}
1134
Takashi Iwai8047c912005-11-17 14:41:22 +01001135static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 unsigned char mask, unsigned char val)
1137{
1138 if (reg < 0xa0)
1139 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1140 else
1141 return snd_es18xx_bits(chip, reg, mask, val);
1142}
1143
Takashi Iwai8047c912005-11-17 14:41:22 +01001144static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 if (reg < 0xa0)
1147 return snd_es18xx_mixer_read(chip, reg);
1148 else
1149 return snd_es18xx_read(chip, reg);
1150}
1151
1152#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1153{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1154 .info = snd_es18xx_info_single, \
1155 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1156 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1157
Takashi Iwai8047c912005-11-17 14:41:22 +01001158static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 int mask = (kcontrol->private_value >> 16) & 0xff;
1161
1162 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1163 uinfo->count = 1;
1164 uinfo->value.integer.min = 0;
1165 uinfo->value.integer.max = mask;
1166 return 0;
1167}
1168
Takashi Iwai8047c912005-11-17 14:41:22 +01001169static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai8047c912005-11-17 14:41:22 +01001171 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int reg = kcontrol->private_value & 0xff;
1173 int shift = (kcontrol->private_value >> 8) & 0xff;
1174 int mask = (kcontrol->private_value >> 16) & 0xff;
1175 int invert = (kcontrol->private_value >> 24) & 0xff;
1176 int val;
1177
1178 val = snd_es18xx_reg_read(chip, reg);
1179 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1180 if (invert)
1181 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1182 return 0;
1183}
1184
Takashi Iwai8047c912005-11-17 14:41:22 +01001185static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwai8047c912005-11-17 14:41:22 +01001187 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int reg = kcontrol->private_value & 0xff;
1189 int shift = (kcontrol->private_value >> 8) & 0xff;
1190 int mask = (kcontrol->private_value >> 16) & 0xff;
1191 int invert = (kcontrol->private_value >> 24) & 0xff;
1192 unsigned char val;
1193
1194 val = (ucontrol->value.integer.value[0] & mask);
1195 if (invert)
1196 val = mask - val;
1197 mask <<= shift;
1198 val <<= shift;
1199 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1200}
1201
1202#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1203{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1204 .info = snd_es18xx_info_double, \
1205 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1206 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1207
Takashi Iwai8047c912005-11-17 14:41:22 +01001208static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int mask = (kcontrol->private_value >> 24) & 0xff;
1211
1212 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1213 uinfo->count = 2;
1214 uinfo->value.integer.min = 0;
1215 uinfo->value.integer.max = mask;
1216 return 0;
1217}
1218
Takashi Iwai8047c912005-11-17 14:41:22 +01001219static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Takashi Iwai8047c912005-11-17 14:41:22 +01001221 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int left_reg = kcontrol->private_value & 0xff;
1223 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1224 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1225 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1226 int mask = (kcontrol->private_value >> 24) & 0xff;
1227 int invert = (kcontrol->private_value >> 22) & 1;
1228 unsigned char left, right;
1229
1230 left = snd_es18xx_reg_read(chip, left_reg);
1231 if (left_reg != right_reg)
1232 right = snd_es18xx_reg_read(chip, right_reg);
1233 else
1234 right = left;
1235 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1236 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1237 if (invert) {
1238 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1239 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1240 }
1241 return 0;
1242}
1243
Takashi Iwai8047c912005-11-17 14:41:22 +01001244static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Takashi Iwai8047c912005-11-17 14:41:22 +01001246 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int left_reg = kcontrol->private_value & 0xff;
1248 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1249 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1250 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1251 int mask = (kcontrol->private_value >> 24) & 0xff;
1252 int invert = (kcontrol->private_value >> 22) & 1;
1253 int change;
1254 unsigned char val1, val2, mask1, mask2;
1255
1256 val1 = ucontrol->value.integer.value[0] & mask;
1257 val2 = ucontrol->value.integer.value[1] & mask;
1258 if (invert) {
1259 val1 = mask - val1;
1260 val2 = mask - val2;
1261 }
1262 val1 <<= shift_left;
1263 val2 <<= shift_right;
1264 mask1 = mask << shift_left;
1265 mask2 = mask << shift_right;
1266 if (left_reg != right_reg) {
1267 change = 0;
1268 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1269 change = 1;
1270 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1271 change = 1;
1272 } else {
1273 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1274 val1 | val2) != (val1 | val2));
1275 }
1276 return change;
1277}
1278
Mark Salazarc1f6d412006-01-16 11:29:09 +01001279/* Mixer controls
1280 * These arrays contain setup data for mixer controls.
1281 *
1282 * The controls that are universal to all chipsets are fully initialized
1283 * here.
1284 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001285static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1287ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1288ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1289ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1290ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1292ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1294ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1297 .name = "Capture Source",
1298 .info = snd_es18xx_info_mux,
1299 .get = snd_es18xx_get_mux,
1300 .put = snd_es18xx_put_mux,
1301}
1302};
1303
Takashi Iwai8047c912005-11-17 14:41:22 +01001304static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1306ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1307ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1308ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1310ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1311};
1312
Mark Salazarc1f6d412006-01-16 11:29:09 +01001313/*
1314 * The chipset specific mixer controls
1315 */
1316static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1317 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1318
1319static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1320ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
Mark Salazar95b71292006-01-16 11:35:40 +01001321ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001322ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1323ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1324};
1325
Mark Salazar95b71292006-01-16 11:35:40 +01001326static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1327 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1328
1329static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1330ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1331ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1332ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1333};
1334
Takashi Iwai8047c912005-11-17 14:41:22 +01001335static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1337};
1338
Takashi Iwai8047c912005-11-17 14:41:22 +01001339static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1341ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1342};
1343
Takashi Iwai8047c912005-11-17 14:41:22 +01001344static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1346{
1347 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1348 .name = "3D Control - Switch",
1349 .info = snd_es18xx_info_spatializer_enable,
1350 .get = snd_es18xx_get_spatializer_enable,
1351 .put = snd_es18xx_put_spatializer_enable,
1352}
1353};
1354
Takashi Iwai8047c912005-11-17 14:41:22 +01001355static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1357
Takashi Iwai8047c912005-11-17 14:41:22 +01001358static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1360
Takashi Iwai8047c912005-11-17 14:41:22 +01001361static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1364 .name = "Hardware Master Playback Volume",
1365 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1366 .info = snd_es18xx_info_hw_volume,
1367 .get = snd_es18xx_get_hw_volume,
1368},
1369{
1370 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1371 .name = "Hardware Master Playback Switch",
1372 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1373 .info = snd_es18xx_info_hw_switch,
1374 .get = snd_es18xx_get_hw_switch,
1375},
1376ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1377};
1378
Takashi Iwai8047c912005-11-17 14:41:22 +01001379static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 int data;
1382 unsigned long flags;
1383 spin_lock_irqsave(&chip->ctrl_lock, flags);
1384 outb(reg, chip->ctrl_port);
1385 data = inb(chip->ctrl_port + 1);
1386 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1387 return data;
1388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Takashi Iwai8047c912005-11-17 14:41:22 +01001390static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 unsigned char reg, unsigned char data)
1392{
1393 /* No need for spinlocks, this function is used only in
1394 otherwise protected init code */
1395 outb(reg, chip->ctrl_port);
1396 outb(data, chip->ctrl_port + 1);
1397#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001398 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399#endif
1400}
1401
Takashi Iwai8047c912005-11-17 14:41:22 +01001402static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 int mask = 0;
1405
1406 /* enable extended mode */
1407 snd_es18xx_dsp_command(chip, 0xC6);
1408 /* Reset mixer registers */
1409 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1410
1411 /* Audio 1 DMA demand mode (4 bytes/request) */
1412 snd_es18xx_write(chip, 0xB9, 2);
1413 if (chip->caps & ES18XX_CONTROL) {
1414 /* Hardware volume IRQ */
1415 snd_es18xx_config_write(chip, 0x27, chip->irq);
1416 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1417 /* FM I/O */
1418 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1419 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1420 }
1421 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1422 /* MPU-401 I/O */
1423 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1424 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1425 /* MPU-401 IRQ */
1426 snd_es18xx_config_write(chip, 0x28, chip->irq);
1427 }
1428 /* Audio1 IRQ */
1429 snd_es18xx_config_write(chip, 0x70, chip->irq);
1430 /* Audio2 IRQ */
1431 snd_es18xx_config_write(chip, 0x72, chip->irq);
1432 /* Audio1 DMA */
1433 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1434 /* Audio2 DMA */
1435 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1436
1437 /* Enable Audio 1 IRQ */
1438 snd_es18xx_write(chip, 0xB1, 0x50);
1439 /* Enable Audio 2 IRQ */
1440 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1441 /* Enable Audio 1 DMA */
1442 snd_es18xx_write(chip, 0xB2, 0x50);
1443 /* Enable MPU and hardware volume interrupt */
1444 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1445 }
1446 else {
1447 int irqmask, dma1mask, dma2mask;
1448 switch (chip->irq) {
1449 case 2:
1450 case 9:
1451 irqmask = 0;
1452 break;
1453 case 5:
1454 irqmask = 1;
1455 break;
1456 case 7:
1457 irqmask = 2;
1458 break;
1459 case 10:
1460 irqmask = 3;
1461 break;
1462 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001463 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -ENODEV;
1465 }
1466 switch (chip->dma1) {
1467 case 0:
1468 dma1mask = 1;
1469 break;
1470 case 1:
1471 dma1mask = 2;
1472 break;
1473 case 3:
1474 dma1mask = 3;
1475 break;
1476 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001477 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -ENODEV;
1479 }
1480 switch (chip->dma2) {
1481 case 0:
1482 dma2mask = 0;
1483 break;
1484 case 1:
1485 dma2mask = 1;
1486 break;
1487 case 3:
1488 dma2mask = 2;
1489 break;
1490 case 5:
1491 dma2mask = 3;
1492 break;
1493 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001494 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -ENODEV;
1496 }
1497
1498 /* Enable and set Audio 1 IRQ */
1499 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1500 /* Enable and set Audio 1 DMA */
1501 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1502 /* Set Audio 2 DMA */
1503 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1504 /* Enable Audio 2 IRQ and DMA
1505 Set capture mixer input */
1506 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1507 /* Enable and set hardware volume interrupt */
1508 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1509 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1510 /* MPU401 share irq with audio
1511 Joystick enabled
1512 FM enabled */
1513 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1514 }
1515 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1516 }
1517 if (chip->caps & ES18XX_NEW_RATE) {
1518 /* Change behaviour of register A1
1519 4x oversampling
1520 2nd channel DAC asynchronous */
1521 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1522 }
1523 if (!(chip->caps & ES18XX_PCM2)) {
1524 /* Enable DMA FIFO */
1525 snd_es18xx_write(chip, 0xB7, 0x80);
1526 }
1527 if (chip->caps & ES18XX_SPATIALIZER) {
1528 /* Set spatializer parameters to recommended values */
1529 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1530 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1531 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1532 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1533 }
Mark Salazar95b71292006-01-16 11:35:40 +01001534 /* Flip the "enable I2S" bits for those chipsets that need it */
1535 switch (chip->version) {
1536 case 0x1879:
1537 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1538 //so a Switch control has been added to toggle this 0x71 bit on/off:
1539 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1540 /* Note: we fall through on purpose here. */
1541 case 0x1878:
1542 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1543 break;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* Mute input source */
1546 if (chip->caps & ES18XX_MUTEREC)
1547 mask = 0x10;
1548 if (chip->caps & ES18XX_RECMIX)
1549 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1550 else {
1551 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1552 snd_es18xx_write(chip, 0xb4, 0x00);
1553 }
1554#ifndef AVOID_POPS
1555 /* Enable PCM output */
1556 snd_es18xx_dsp_command(chip, 0xD1);
1557#endif
1558
1559 return 0;
1560}
1561
Takashi Iwai8047c912005-11-17 14:41:22 +01001562static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 int hi,lo;
1565
1566 /* reset */
1567 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001568 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -ENODEV;
1570 }
1571
1572 snd_es18xx_dsp_command(chip, 0xe7);
1573 hi = snd_es18xx_dsp_get_byte(chip);
1574 if (hi < 0) {
1575 return hi;
1576 }
1577 lo = snd_es18xx_dsp_get_byte(chip);
1578 if ((lo & 0xf0) != 0x80) {
1579 return -ENODEV;
1580 }
1581 if (hi == 0x48) {
1582 chip->version = 0x488;
1583 return 0;
1584 }
1585 if (hi != 0x68) {
1586 return -ENODEV;
1587 }
1588 if ((lo & 0x0f) < 8) {
1589 chip->version = 0x688;
1590 return 0;
1591 }
1592
1593 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001594 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001596 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 lo = inb(chip->port + 0x05);
1598 if (hi != lo) {
1599 chip->version = hi << 8 | lo;
1600 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001601 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 chip->ctrl_port += inb(chip->port + 0x05);
1603
1604 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1605 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1606 return -EBUSY;
1607 }
1608
1609 return 0;
1610 }
1611
1612 /* If has Hardware volume */
1613 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1614 /* If has Audio2 */
1615 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1616 /* If has volume count */
1617 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1618 chip->version = 0x1887;
1619 } else {
1620 chip->version = 0x1888;
1621 }
1622 } else {
1623 chip->version = 0x1788;
1624 }
1625 }
1626 else
1627 chip->version = 0x1688;
1628 return 0;
1629}
1630
Takashi Iwai8047c912005-11-17 14:41:22 +01001631static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 if (snd_es18xx_identify(chip) < 0) {
1634 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1635 return -ENODEV;
1636 }
1637
1638 switch (chip->version) {
1639 case 0x1868:
Mark Salazar14086772006-01-16 11:33:52 +01001640 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642 case 0x1869:
1643 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1644 break;
1645 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001646 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
1648 case 0x1879:
1649 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1650 break;
1651 case 0x1887:
Mark Salazar14086772006-01-16 11:33:52 +01001652 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 case 0x1888:
Mark Salazar14086772006-01-16 11:33:52 +01001655 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 break;
1657 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001658 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 chip->port, chip->version);
1660 return -ENODEV;
1661 }
1662
1663 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1664
1665 if (chip->dma1 == chip->dma2)
1666 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1667
1668 return snd_es18xx_initialize(chip);
1669}
1670
Takashi Iwai8047c912005-11-17 14:41:22 +01001671static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .open = snd_es18xx_playback_open,
1673 .close = snd_es18xx_playback_close,
1674 .ioctl = snd_pcm_lib_ioctl,
1675 .hw_params = snd_es18xx_playback_hw_params,
1676 .hw_free = snd_es18xx_pcm_hw_free,
1677 .prepare = snd_es18xx_playback_prepare,
1678 .trigger = snd_es18xx_playback_trigger,
1679 .pointer = snd_es18xx_playback_pointer,
1680};
1681
Takashi Iwai8047c912005-11-17 14:41:22 +01001682static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 .open = snd_es18xx_capture_open,
1684 .close = snd_es18xx_capture_close,
1685 .ioctl = snd_pcm_lib_ioctl,
1686 .hw_params = snd_es18xx_capture_hw_params,
1687 .hw_free = snd_es18xx_pcm_hw_free,
1688 .prepare = snd_es18xx_capture_prepare,
1689 .trigger = snd_es18xx_capture_trigger,
1690 .pointer = snd_es18xx_capture_pointer,
1691};
1692
Takashi Iwai8047c912005-11-17 14:41:22 +01001693static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai8047c912005-11-17 14:41:22 +01001695 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 char str[16];
1697 int err;
1698
1699 if (rpcm)
1700 *rpcm = NULL;
1701 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001702 if (chip->caps & ES18XX_PCM2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001704 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (err < 0)
1707 return err;
1708
1709 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1710 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1711
1712 /* global setup */
1713 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 pcm->info_flags = 0;
1715 if (chip->caps & ES18XX_DUPLEX_SAME)
1716 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1717 if (! (chip->caps & ES18XX_PCM2))
1718 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1719 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1720 chip->pcm = pcm;
1721
1722 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1723 snd_dma_isa_data(),
1724 64*1024,
1725 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1726
1727 if (rpcm)
1728 *rpcm = pcm;
1729 return 0;
1730}
1731
1732/* Power Management support functions */
1733#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001734static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001736 struct snd_audiodrive *acard = card->private_data;
1737 struct snd_es18xx *chip = acard->chip;
1738
1739 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 snd_pcm_suspend_all(chip->pcm);
1742
1743 /* power down */
1744 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1745 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1746 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1747 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1748
1749 return 0;
1750}
1751
Takashi Iwai8047c912005-11-17 14:41:22 +01001752static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001754 struct snd_audiodrive *acard = card->private_data;
1755 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1758 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1759
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001760 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763#endif /* CONFIG_PM */
1764
Takashi Iwai8047c912005-11-17 14:41:22 +01001765static int snd_es18xx_free(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001767 release_and_free_resource(chip->res_port);
1768 release_and_free_resource(chip->res_ctrl_port);
1769 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (chip->irq >= 0)
1771 free_irq(chip->irq, (void *) chip);
1772 if (chip->dma1 >= 0) {
1773 disable_dma(chip->dma1);
1774 free_dma(chip->dma1);
1775 }
1776 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1777 disable_dma(chip->dma2);
1778 free_dma(chip->dma2);
1779 }
1780 kfree(chip);
1781 return 0;
1782}
1783
Takashi Iwai8047c912005-11-17 14:41:22 +01001784static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Takashi Iwai8047c912005-11-17 14:41:22 +01001786 struct snd_es18xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return snd_es18xx_free(chip);
1788}
1789
Takashi Iwai8047c912005-11-17 14:41:22 +01001790static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned long port,
1792 unsigned long mpu_port,
1793 unsigned long fm_port,
1794 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001795 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Takashi Iwai8047c912005-11-17 14:41:22 +01001797 struct snd_es18xx *chip;
1798 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 .dev_free = snd_es18xx_dev_free,
1800 };
1801 int err;
1802
1803 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001804 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (chip == NULL)
1806 return -ENOMEM;
1807 spin_lock_init(&chip->reg_lock);
1808 spin_lock_init(&chip->mixer_lock);
1809 spin_lock_init(&chip->ctrl_lock);
1810 chip->card = card;
1811 chip->port = port;
1812 chip->mpu_port = mpu_port;
1813 chip->fm_port = fm_port;
1814 chip->irq = -1;
1815 chip->dma1 = -1;
1816 chip->dma2 = -1;
1817 chip->audio2_vol = 0x00;
1818 chip->active = 0;
1819
1820 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1821 snd_es18xx_free(chip);
1822 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1823 return -EBUSY;
1824 }
1825
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001826 if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx", (void *) chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 snd_es18xx_free(chip);
1828 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1829 return -EBUSY;
1830 }
1831 chip->irq = irq;
1832
1833 if (request_dma(dma1, "ES18xx DMA 1")) {
1834 snd_es18xx_free(chip);
1835 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1836 return -EBUSY;
1837 }
1838 chip->dma1 = dma1;
1839
1840 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1841 snd_es18xx_free(chip);
1842 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1843 return -EBUSY;
1844 }
1845 chip->dma2 = dma2;
1846
1847 if (snd_es18xx_probe(chip) < 0) {
1848 snd_es18xx_free(chip);
1849 return -ENODEV;
1850 }
1851 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1852 snd_es18xx_free(chip);
1853 return err;
1854 }
1855 *rchip = chip;
1856 return 0;
1857}
1858
Takashi Iwai8047c912005-11-17 14:41:22 +01001859static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Takashi Iwai8047c912005-11-17 14:41:22 +01001861 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int err;
1863 unsigned int idx;
1864
1865 card = chip->card;
1866
1867 strcpy(card->mixername, chip->pcm->name);
1868
1869 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001870 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1872 if (chip->caps & ES18XX_HWV) {
1873 switch (idx) {
1874 case 0:
1875 chip->master_volume = kctl;
1876 kctl->private_free = snd_es18xx_hwv_free;
1877 break;
1878 case 1:
1879 chip->master_switch = kctl;
1880 kctl->private_free = snd_es18xx_hwv_free;
1881 break;
1882 }
1883 }
1884 if ((err = snd_ctl_add(card, kctl)) < 0)
1885 return err;
1886 }
1887 if (chip->caps & ES18XX_PCM2) {
1888 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1889 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1890 return err;
1891 }
1892 } else {
1893 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1894 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1895 return err;
1896 }
1897 }
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (chip->caps & ES18XX_RECMIX) {
1900 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1901 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1902 return err;
1903 }
1904 }
1905 switch (chip->version) {
1906 default:
1907 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1908 return err;
1909 break;
1910 case 0x1869:
1911 case 0x1879:
1912 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1913 return err;
1914 break;
1915 }
1916 if (chip->caps & ES18XX_SPATIALIZER) {
1917 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1918 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1919 return err;
1920 }
1921 }
1922 if (chip->caps & ES18XX_HWV) {
1923 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001924 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1926 if (idx == 0)
1927 chip->hw_volume = kctl;
1928 else
1929 chip->hw_switch = kctl;
1930 kctl->private_free = snd_es18xx_hwv_free;
1931 if ((err = snd_ctl_add(card, kctl)) < 0)
1932 return err;
1933
1934 }
1935 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001936 /* finish initializing other chipset specific controls
1937 */
1938 if (chip->version != 0x1868) {
1939 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1940 chip));
1941 if (err < 0)
1942 return err;
1943 }
1944 if (chip->version == 0x1869) {
1945 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1946 err = snd_ctl_add(card,
1947 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1948 chip));
1949 if (err < 0)
1950 return err;
1951 }
Mark Salazar95b71292006-01-16 11:35:40 +01001952 } else if (chip->version == 0x1878) {
1953 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1954 chip));
1955 if (err < 0)
1956 return err;
1957 } else if (chip->version == 0x1879) {
1958 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1959 err = snd_ctl_add(card,
1960 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1961 chip));
1962 if (err < 0)
1963 return err;
1964 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return 0;
1967}
1968
1969
1970/* Card level */
1971
1972MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1973MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1974MODULE_LICENSE("GPL");
1975MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1976 "{ESS,ES1869 PnP AudioDrive},"
1977 "{ESS,ES1878 PnP AudioDrive},"
1978 "{ESS,ES1879 PnP AudioDrive},"
1979 "{ESS,ES1887 PnP AudioDrive},"
1980 "{ESS,ES1888 PnP AudioDrive},"
1981 "{ESS,ES1887 AudioDrive},"
1982 "{ESS,ES1888 AudioDrive}}");
1983
1984static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1985static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1986static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1987#ifdef CONFIG_PNP
1988static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1989#endif
1990static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1991#ifndef CONFIG_PNP
1992static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1993#else
1994static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1995#endif
1996static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1997static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1998static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1999static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
2000
2001module_param_array(index, int, NULL, 0444);
2002MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2003module_param_array(id, charp, NULL, 0444);
2004MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2005module_param_array(enable, bool, NULL, 0444);
2006MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2007#ifdef CONFIG_PNP
2008module_param_array(isapnp, bool, NULL, 0444);
2009MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2010#endif
2011module_param_array(port, long, NULL, 0444);
2012MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2013module_param_array(mpu_port, long, NULL, 0444);
2014MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2015module_param_array(fm_port, long, NULL, 0444);
2016MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2017module_param_array(irq, int, NULL, 0444);
2018MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2019module_param_array(dma1, int, NULL, 0444);
2020MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2021module_param_array(dma2, int, NULL, 0444);
2022MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002025static int isa_registered;
2026static int pnp_registered;
2027static int pnpc_registered;
Ondrej Zary1c398552006-07-31 12:51:57 +02002028
2029static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2030 { .id = "ESS1869" },
Rene Herman4848ffe2007-08-01 23:50:21 +02002031 { .id = "ESS1879" },
Ondrej Zary1c398552006-07-31 12:51:57 +02002032 { .id = "" } /* end */
2033};
2034
2035MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2036
2037/* PnP main device initialization */
Rene Herman109c53f842007-11-30 17:59:25 +01002038static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c398552006-07-31 12:51:57 +02002039{
Rene Herman109c53f842007-11-30 17:59:25 +01002040 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c398552006-07-31 12:51:57 +02002041 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2042 return -EBUSY;
2043 }
2044 /* ok. hack using Vendor-Defined Card-Level registers */
2045 /* skip csn and logdev initialization - already done in isapnp_configure */
2046 if (pnp_device_is_isapnp(pdev)) {
2047 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2048 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2049 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2050 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2051 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2052 isapnp_cfg_end();
2053 }
2054 port[dev] = pnp_port_start(pdev, 0);
2055 fm_port[dev] = pnp_port_start(pdev, 1);
2056 mpu_port[dev] = pnp_port_start(pdev, 2);
2057 dma1[dev] = pnp_dma(pdev, 0);
2058 dma2[dev] = pnp_dma(pdev, 1);
2059 irq[dev] = pnp_irq(pdev, 0);
2060 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2061 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2062 return 0;
2063}
2064
2065static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2066 struct pnp_dev *pdev)
2067{
Ondrej Zary1c398552006-07-31 12:51:57 +02002068 acard->dev = pdev;
Rene Herman109c53f842007-11-30 17:59:25 +01002069 if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
Ondrej Zary1c398552006-07-31 12:51:57 +02002070 return -EBUSY;
Ondrej Zary1c398552006-07-31 12:51:57 +02002071 return 0;
2072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2075 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2076 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2077 /* ESS 1868 (integrated on Maxisound Cards) */
2078 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2079 /* ESS 1868 (integrated on Maxisound Cards) */
2080 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2081 /* ESS ES1869 Plug and Play AudioDrive */
2082 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2083 /* ESS 1869 */
2084 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2085 /* ESS 1878 */
2086 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2087 /* ESS 1879 */
2088 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2089 /* --- */
2090 { .id = "" } /* end */
2091};
2092
2093MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2094
Ondrej Zary1c398552006-07-31 12:51:57 +02002095static int __devinit snd_audiodrive_pnpc(int dev, struct snd_audiodrive *acard,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct pnp_card_link *card,
2097 const struct pnp_card_device_id *id)
2098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +01002100 if (acard->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +01002104 if (acard->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* Control port initialization */
Ondrej Zary1c398552006-07-31 12:51:57 +02002108 if (pnp_activate_dev(acard->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2110 return -EAGAIN;
2111 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002112 snd_printdd("pnp: port=0x%llx\n",
2113 (unsigned long long)pnp_port_start(acard->devc, 0));
Rene Herman109c53f842007-11-30 17:59:25 +01002114 if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return 0;
2118}
2119#endif /* CONFIG_PNP */
2120
Takashi Iwai43bcd972005-09-05 17:19:20 +02002121#ifdef CONFIG_PNP
2122#define is_isapnp_selected(dev) isapnp[dev]
2123#else
2124#define is_isapnp_selected(dev) 0
2125#endif
2126
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002127static struct snd_card *snd_es18xx_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002129 return snd_card_new(index[dev], id[dev], THIS_MODULE,
2130 sizeof(struct snd_audiodrive));
2131}
2132
2133static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2134{
2135 struct snd_audiodrive *acard = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002136 struct snd_es18xx *chip;
2137 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int err;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if ((err = snd_es18xx_new_device(card,
2141 port[dev],
2142 mpu_port[dev],
2143 fm_port[dev],
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002144 irq[dev], dma1[dev], dma2[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +02002145 &chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002146 return err;
2147 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002152 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2154 card->shortname,
2155 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002156 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 else
2158 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2159 card->shortname,
2160 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002161 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Takashi Iwai43bcd972005-09-05 17:19:20 +02002163 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002164 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002165
2166 if ((err = snd_es18xx_mixer(chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2170 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002171 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002173 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 }
2177
2178 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2179 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2180 chip->mpu_port, 0,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002181 irq[dev], 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002182 &chip->rmidi)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002186 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002189static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2190{
2191 return enable[dev] && !is_isapnp_selected(dev);
2192}
2193
2194static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002196 struct snd_card *card;
2197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002199 card = snd_es18xx_card_new(dev);
2200 if (! card)
2201 return -ENOMEM;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002202 snd_card_set_dev(card, devptr);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002203 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2204 snd_card_free(card);
2205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002207 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002211static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002212{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002213 int err;
2214 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2215 static int possible_dmas[] = {1, 0, 3, 5, -1};
2216
2217 if (irq[dev] == SNDRV_AUTO_IRQ) {
2218 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2219 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2220 return -EBUSY;
2221 }
2222 }
2223 if (dma1[dev] == SNDRV_AUTO_DMA) {
2224 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2225 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2226 return -EBUSY;
2227 }
2228 }
2229 if (dma2[dev] == SNDRV_AUTO_DMA) {
2230 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2231 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2232 return -EBUSY;
2233 }
2234 }
2235
2236 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002237 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002238 } else {
2239 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2240 int i;
2241 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2242 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002243 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002244 if (! err)
2245 return 0;
2246 }
2247 return err;
2248 }
2249}
2250
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002251static int __devexit snd_es18xx_isa_remove(struct device *devptr,
2252 unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002253{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002254 snd_card_free(dev_get_drvdata(devptr));
2255 dev_set_drvdata(devptr, NULL);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002256 return 0;
2257}
2258
2259#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002260static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2261 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002262{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002263 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002264}
2265
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002266static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002267{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002268 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002269}
2270#endif
2271
Rene Herman83c51c02007-03-20 11:33:46 +01002272#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002273
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002274static struct isa_driver snd_es18xx_isa_driver = {
2275 .match = snd_es18xx_isa_match,
2276 .probe = snd_es18xx_isa_probe,
2277 .remove = __devexit_p(snd_es18xx_isa_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002278#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002279 .suspend = snd_es18xx_isa_suspend,
2280 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002281#endif
2282 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002283 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002284 },
2285};
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288#ifdef CONFIG_PNP
Ondrej Zary1c398552006-07-31 12:51:57 +02002289static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2290 const struct pnp_device_id *id)
2291{
2292 static int dev;
2293 int err;
2294 struct snd_card *card;
2295
2296 if (pnp_device_is_isapnp(pdev))
2297 return -ENOENT; /* we have another procedure - card */
2298 for (; dev < SNDRV_CARDS; dev++) {
2299 if (enable[dev] && isapnp[dev])
2300 break;
2301 }
2302 if (dev >= SNDRV_CARDS)
2303 return -ENODEV;
2304
2305 card = snd_es18xx_card_new(dev);
2306 if (! card)
2307 return -ENOMEM;
2308 if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2309 snd_card_free(card);
2310 return err;
2311 }
2312 snd_card_set_dev(card, &pdev->dev);
2313 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2314 snd_card_free(card);
2315 return err;
2316 }
2317 pnp_set_drvdata(pdev, card);
2318 dev++;
Ondrej Zary1c398552006-07-31 12:51:57 +02002319 return 0;
2320}
2321
2322static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2323{
2324 snd_card_free(pnp_get_drvdata(pdev));
2325 pnp_set_drvdata(pdev, NULL);
2326}
2327
2328#ifdef CONFIG_PM
2329static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2330{
2331 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2332}
2333static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2334{
2335 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2336}
2337#endif
2338
2339static struct pnp_driver es18xx_pnp_driver = {
2340 .name = "es18xx-pnpbios",
2341 .id_table = snd_audiodrive_pnpbiosids,
2342 .probe = snd_audiodrive_pnp_detect,
2343 .remove = __devexit_p(snd_audiodrive_pnp_remove),
2344#ifdef CONFIG_PM
2345 .suspend = snd_audiodrive_pnp_suspend,
2346 .resume = snd_audiodrive_pnp_resume,
2347#endif
2348};
2349
2350static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002351 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002354 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 int res;
2356
2357 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002358 if (enable[dev] && isapnp[dev])
2359 break;
2360 }
2361 if (dev >= SNDRV_CARDS)
2362 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002364 card = snd_es18xx_card_new(dev);
2365 if (! card)
2366 return -ENOMEM;
2367
Ondrej Zary1c398552006-07-31 12:51:57 +02002368 if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002369 snd_card_free(card);
2370 return res;
2371 }
2372 snd_card_set_dev(card, &pcard->card->dev);
2373 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2374 snd_card_free(card);
2375 return res;
2376 }
2377
2378 pnp_set_card_drvdata(pcard, card);
2379 dev++;
2380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Ondrej Zary1c398552006-07-31 12:51:57 +02002383static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002385 snd_card_free(pnp_get_card_drvdata(pcard));
2386 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002389#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002390static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002391{
2392 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2393}
2394
Ondrej Zary1c398552006-07-31 12:51:57 +02002395static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002396{
2397 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2398}
2399
2400#endif
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402static struct pnp_card_driver es18xx_pnpc_driver = {
2403 .flags = PNP_DRIVER_RES_DISABLE,
2404 .name = "es18xx",
2405 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c398552006-07-31 12:51:57 +02002406 .probe = snd_audiodrive_pnpc_detect,
2407 .remove = __devexit_p(snd_audiodrive_pnpc_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002408#ifdef CONFIG_PM
Ondrej Zary1c398552006-07-31 12:51:57 +02002409 .suspend = snd_audiodrive_pnpc_suspend,
2410 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412};
2413#endif /* CONFIG_PNP */
2414
2415static int __init alsa_card_es18xx_init(void)
2416{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002417 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002419 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002420#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002421 if (!err)
2422 isa_registered = 1;
2423
Ondrej Zary1c398552006-07-31 12:51:57 +02002424 err = pnp_register_driver(&es18xx_pnp_driver);
2425 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002426 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002427
Ondrej Zary1c398552006-07-31 12:51:57 +02002428 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2429 if (!err)
2430 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002431
2432 if (isa_registered || pnp_registered)
2433 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002434#endif
Rene Herman609d7692007-05-15 11:42:56 +02002435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438static void __exit alsa_card_es18xx_exit(void)
2439{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002440#ifdef CONFIG_PNP
2441 if (pnpc_registered)
2442 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2443 if (pnp_registered)
2444 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002445 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002446#endif
Rene Herman609d7692007-05-15 11:42:56 +02002447 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
2450module_init(alsa_card_es18xx_init)
2451module_exit(alsa_card_es18xx_exit)