blob: b481bb8c31bc7ba0fad5d62f6b57112fd82ca587 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/* GENERAL NOTES:
23 *
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
Mark Salazar95b71292006-01-16 11:35:40 +010052 * - Zoom Video is implemented by sharing the FM DAC, thus the user can
53 * have either FM playback or Video playback but not both simultaneously.
54 * The Video Playback Switch mixer control toggles this choice.
55 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * BUGS:
57 *
58 * - There is a major trouble I noted:
59 *
60 * using both channel for playback stereo 16 bit samples at 44100 Hz
61 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
62 *
63 * The same happens using Audio1 for captureing.
64 *
65 * The Windows driver does not suffer of this (although it use Audio1
66 * only for captureing). I'm unable to discover why.
67 *
68 */
69
Mark Salazar95b71292006-01-16 11:35:40 +010070/*
71 * ES1879 NOTES:
72 * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
73 * seems to be effected (speaker_test plays a lower frequency). Can't find
74 * anything in the datasheet to account for this, so a Video Playback Switch
75 * control has been included to allow ZV to be enabled only when necessary.
76 * Then again on at least one test system the 0x71 bit 6 enable bit is not
77 * needed for ZV, so maybe the datasheet is entirely wrong here.
78 */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010081#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010082#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/pnp.h>
84#include <linux/isapnp.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040085#include <linux/module.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070086#include <linux/delay.h>
87
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010088#include <asm/io.h>
89#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <sound/core.h>
91#include <sound/control.h>
92#include <sound/pcm.h>
93#include <sound/pcm_params.h>
94#include <sound/mpu401.h>
95#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define SNDRV_LEGACY_FIND_FREE_IRQ
97#define SNDRV_LEGACY_FIND_FREE_DMA
98#include <sound/initval.h>
99
100#define PFX "es18xx: "
101
Takashi Iwai8047c912005-11-17 14:41:22 +0100102struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long port; /* port of ESS chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long ctrl_port; /* Control port of ESS chip */
105 struct resource *res_port;
106 struct resource *res_mpu_port;
107 struct resource *res_ctrl_port;
108 int irq; /* IRQ number of ESS chip */
109 int dma1; /* DMA1 */
110 int dma2; /* DMA2 */
111 unsigned short version; /* version of ESS chip */
112 int caps; /* Chip capabilities */
113 unsigned short audio2_vol; /* volume level of audio2 */
114
115 unsigned short active; /* active channel mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int dma1_shift;
117 unsigned int dma2_shift;
118
Takashi Iwai8047c912005-11-17 14:41:22 +0100119 struct snd_pcm *pcm;
120 struct snd_pcm_substream *playback_a_substream;
121 struct snd_pcm_substream *capture_a_substream;
122 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Takashi Iwai8047c912005-11-17 14:41:22 +0100124 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai8047c912005-11-17 14:41:22 +0100126 struct snd_kcontrol *hw_volume;
127 struct snd_kcontrol *hw_switch;
128 struct snd_kcontrol *master_volume;
129 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 spinlock_t reg_lock;
132 spinlock_t mixer_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#ifdef CONFIG_PM
134 unsigned char pm_reg;
135#endif
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100136#ifdef CONFIG_PNP
137 struct pnp_dev *dev;
138 struct pnp_dev *devc;
139#endif
140};
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define AUDIO1_IRQ 0x01
143#define AUDIO2_IRQ 0x02
144#define HWV_IRQ 0x04
145#define MPU_IRQ 0x08
146
147#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
148#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
149#define ES18XX_RECMIX 0x0004 /* Has record mixer */
150#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
151#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
152#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
153#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100154#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
156#define ES18XX_I2S 0x0200 /* I2S mixer control */
157#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
158#define ES18XX_CONTROL 0x0800 /* Has control ports */
Ondrej Zary2603fe22014-11-03 21:35:48 +0100159#define ES18XX_GPO_2BIT 0x1000 /* GPO0,1 controlled by PM port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* Power Management */
162#define ES18XX_PM 0x07
163#define ES18XX_PM_GPO0 0x01
164#define ES18XX_PM_GPO1 0x02
165#define ES18XX_PM_PDR 0x04
166#define ES18XX_PM_ANA 0x08
167#define ES18XX_PM_FM 0x020
168#define ES18XX_PM_SUS 0x080
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Lowlevel */
171
172#define DAC1 0x01
173#define ADC1 0x02
174#define DAC2 0x04
175#define MILLISECOND 10000
176
Takashi Iwai8047c912005-11-17 14:41:22 +0100177static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int i;
180
181 for(i = MILLISECOND; i; i--)
182 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
183 outb(val, chip->port + 0x0C);
184 return 0;
185 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200186 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188}
189
Takashi Iwai8047c912005-11-17 14:41:22 +0100190static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int i;
193
194 for(i = MILLISECOND/10; i; i--)
195 if (inb(chip->port + 0x0C) & 0x40)
196 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200197 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
198 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENODEV;
200}
201
202#undef REG_DEBUG
203
Takashi Iwai8047c912005-11-17 14:41:22 +0100204static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned char reg, unsigned char data)
206{
207 unsigned long flags;
208 int ret;
209
210 spin_lock_irqsave(&chip->reg_lock, flags);
211 ret = snd_es18xx_dsp_command(chip, reg);
212 if (ret < 0)
213 goto end;
214 ret = snd_es18xx_dsp_command(chip, data);
215 end:
216 spin_unlock_irqrestore(&chip->reg_lock, flags);
217#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200218 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220 return ret;
221}
222
Takashi Iwai8047c912005-11-17 14:41:22 +0100223static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 unsigned long flags;
226 int ret, data;
227 spin_lock_irqsave(&chip->reg_lock, flags);
228 ret = snd_es18xx_dsp_command(chip, 0xC0);
229 if (ret < 0)
230 goto end;
231 ret = snd_es18xx_dsp_command(chip, reg);
232 if (ret < 0)
233 goto end;
234 data = snd_es18xx_dsp_get_byte(chip);
235 ret = data;
236#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200237 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239 end:
240 spin_unlock_irqrestore(&chip->reg_lock, flags);
241 return ret;
242}
243
244/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100245static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned char mask, unsigned char val)
247{
248 int ret;
249 unsigned char old, new, oval;
250 unsigned long flags;
251 spin_lock_irqsave(&chip->reg_lock, flags);
252 ret = snd_es18xx_dsp_command(chip, 0xC0);
253 if (ret < 0)
254 goto end;
255 ret = snd_es18xx_dsp_command(chip, reg);
256 if (ret < 0)
257 goto end;
258 ret = snd_es18xx_dsp_get_byte(chip);
259 if (ret < 0) {
260 goto end;
261 }
262 old = ret;
263 oval = old & mask;
264 if (val != oval) {
265 ret = snd_es18xx_dsp_command(chip, reg);
266 if (ret < 0)
267 goto end;
268 new = (old & ~mask) | (val & mask);
269 ret = snd_es18xx_dsp_command(chip, new);
270 if (ret < 0)
271 goto end;
272#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200273 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
274 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276 }
277 ret = oval;
278 end:
279 spin_unlock_irqrestore(&chip->reg_lock, flags);
280 return ret;
281}
282
Takashi Iwai8047c912005-11-17 14:41:22 +0100283static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned char reg, unsigned char data)
285{
286 unsigned long flags;
287 spin_lock_irqsave(&chip->mixer_lock, flags);
288 outb(reg, chip->port + 0x04);
289 outb(data, chip->port + 0x05);
290 spin_unlock_irqrestore(&chip->mixer_lock, flags);
291#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200292 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#endif
294}
295
Takashi Iwai8047c912005-11-17 14:41:22 +0100296static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 unsigned long flags;
299 int data;
300 spin_lock_irqsave(&chip->mixer_lock, flags);
301 outb(reg, chip->port + 0x04);
302 data = inb(chip->port + 0x05);
303 spin_unlock_irqrestore(&chip->mixer_lock, flags);
304#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200305 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
307 return data;
308}
309
310/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100311static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned char mask, unsigned char val)
313{
314 unsigned char old, new, oval;
315 unsigned long flags;
316 spin_lock_irqsave(&chip->mixer_lock, flags);
317 outb(reg, chip->port + 0x04);
318 old = inb(chip->port + 0x05);
319 oval = old & mask;
320 if (val != oval) {
321 new = (old & ~mask) | (val & mask);
322 outb(new, chip->port + 0x05);
323#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200324 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
325 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif
327 }
328 spin_unlock_irqrestore(&chip->mixer_lock, flags);
329 return oval;
330}
331
Takashi Iwai8047c912005-11-17 14:41:22 +0100332static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned char mask)
334{
335 int old, expected, new;
336 unsigned long flags;
337 spin_lock_irqsave(&chip->mixer_lock, flags);
338 outb(reg, chip->port + 0x04);
339 old = inb(chip->port + 0x05);
340 expected = old ^ mask;
341 outb(expected, chip->port + 0x05);
342 new = inb(chip->port + 0x05);
343 spin_unlock_irqrestore(&chip->mixer_lock, flags);
344#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200345 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
346 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
348 return expected == new;
349}
350
351
Bill Pemberton1bff2922012-12-06 12:35:21 -0500352static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int i;
355 outb(0x03, chip->port + 0x06);
356 inb(chip->port + 0x06);
357 outb(0x00, chip->port + 0x06);
358 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
359 if (inb(chip->port + 0x0A) != 0xAA)
360 return -1;
361 return 0;
362}
363
Takashi Iwai8047c912005-11-17 14:41:22 +0100364static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 outb(0x02, chip->port + 0x06);
367 inb(chip->port + 0x06);
368 outb(0x00, chip->port + 0x06);
369 return 0;
370}
371
Takashi Iwai8047c912005-11-17 14:41:22 +0100372static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 {
374 .num = 793800,
375 .den_min = 1,
376 .den_max = 128,
377 .den_step = 1,
378 },
379 {
380 .num = 768000,
381 .den_min = 1,
382 .den_max = 128,
383 .den_step = 1,
384 }
385};
386
Takashi Iwai8047c912005-11-17 14:41:22 +0100387static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .nrats = 2,
389 .rats = new_clocks,
390};
391
Takashi Iwai8047c912005-11-17 14:41:22 +0100392static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 {
394 .num = 795444,
395 .den_min = 1,
396 .den_max = 128,
397 .den_step = 1,
398 },
399 {
400 .num = 397722,
401 .den_min = 1,
402 .den_max = 128,
403 .den_step = 1,
404 }
405};
406
Takashi Iwai8047c912005-11-17 14:41:22 +0100407static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 .nrats = 2,
409 .rats = old_clocks,
410};
411
412
Takashi Iwai8047c912005-11-17 14:41:22 +0100413static void snd_es18xx_rate_set(struct snd_es18xx *chip,
414 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int mode)
416{
417 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (chip->caps & ES18XX_NEW_RATE) {
420 if (runtime->rate_num == new_clocks[0].num)
421 bits = 128 - runtime->rate_den;
422 else
423 bits = 256 - runtime->rate_den;
424 } else {
425 if (runtime->rate_num == old_clocks[0].num)
426 bits = 256 - runtime->rate_den;
427 else
428 bits = 128 - runtime->rate_den;
429 }
430
431 /* set filter register */
432 div0 = 256 - 7160000*20/(8*82*runtime->rate);
433
434 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
435 snd_es18xx_mixer_write(chip, 0x70, bits);
436 /*
437 * Comment from kernel oss driver:
438 * FKS: fascinating: 0x72 doesn't seem to work.
439 */
440 snd_es18xx_write(chip, 0xA2, div0);
441 snd_es18xx_mixer_write(chip, 0x72, div0);
442 } else {
443 snd_es18xx_write(chip, 0xA1, bits);
444 snd_es18xx_write(chip, 0xA2, div0);
445 }
446}
447
Takashi Iwai8047c912005-11-17 14:41:22 +0100448static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
449 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Takashi Iwai8047c912005-11-17 14:41:22 +0100451 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int shift, err;
453
454 shift = 0;
455 if (params_channels(hw_params) == 2)
456 shift++;
457 if (snd_pcm_format_width(params_format(hw_params)) == 16)
458 shift++;
459
460 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
461 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
462 (chip->capture_a_substream) &&
463 params_channels(hw_params) != 1) {
464 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
465 return -EBUSY;
466 }
467 chip->dma2_shift = shift;
468 } else {
469 chip->dma1_shift = shift;
470 }
471 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
472 return err;
473 return 0;
474}
475
Takashi Iwai8047c912005-11-17 14:41:22 +0100476static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 return snd_pcm_lib_free_pages(substream);
479}
480
Takashi Iwai8047c912005-11-17 14:41:22 +0100481static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
482 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwai8047c912005-11-17 14:41:22 +0100484 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
486 unsigned int count = snd_pcm_lib_period_bytes(substream);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 snd_es18xx_rate_set(chip, substream, DAC2);
489
490 /* Transfer Count Reload */
491 count = 0x10000 - count;
492 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
493 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
494
495 /* Set format */
496 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
497 ((runtime->channels == 1) ? 0x00 : 0x02) |
498 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
499 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
500
501 /* Set DMA controller */
502 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
503
504 return 0;
505}
506
Takashi Iwai8047c912005-11-17 14:41:22 +0100507static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
508 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int cmd)
510{
511 switch (cmd) {
512 case SNDRV_PCM_TRIGGER_START:
513 case SNDRV_PCM_TRIGGER_RESUME:
514 if (chip->active & DAC2)
515 return 0;
516 chip->active |= DAC2;
517 /* Start DMA */
518 if (chip->dma2 >= 4)
519 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
520 else
521 snd_es18xx_mixer_write(chip, 0x78, 0x93);
522#ifdef AVOID_POPS
523 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800524 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (chip->caps & ES18XX_PCM2)
526 /* Restore Audio 2 volume */
527 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
528 else
529 /* Enable PCM output */
530 snd_es18xx_dsp_command(chip, 0xD1);
531#endif
532 break;
533 case SNDRV_PCM_TRIGGER_STOP:
534 case SNDRV_PCM_TRIGGER_SUSPEND:
535 if (!(chip->active & DAC2))
536 return 0;
537 chip->active &= ~DAC2;
538 /* Stop DMA */
539 snd_es18xx_mixer_write(chip, 0x78, 0x00);
540#ifdef AVOID_POPS
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800541 mdelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (chip->caps & ES18XX_PCM2)
543 /* Set Audio 2 volume to 0 */
544 snd_es18xx_mixer_write(chip, 0x7C, 0);
545 else
546 /* Disable PCM output */
547 snd_es18xx_dsp_command(chip, 0xD3);
548#endif
549 break;
550 default:
551 return -EINVAL;
552 }
553
554 return 0;
555}
556
Takashi Iwai8047c912005-11-17 14:41:22 +0100557static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
558 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai8047c912005-11-17 14:41:22 +0100560 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int shift, err;
562
563 shift = 0;
564 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
565 chip->playback_a_substream &&
566 params_channels(hw_params) != 1) {
567 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
568 return -EBUSY;
569 }
570 if (params_channels(hw_params) == 2)
571 shift++;
572 if (snd_pcm_format_width(params_format(hw_params)) == 16)
573 shift++;
574 chip->dma1_shift = shift;
575 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
576 return err;
577 return 0;
578}
579
Takashi Iwai8047c912005-11-17 14:41:22 +0100580static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai8047c912005-11-17 14:41:22 +0100582 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
583 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
585 unsigned int count = snd_pcm_lib_period_bytes(substream);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 snd_es18xx_reset_fifo(chip);
588
589 /* Set stereo/mono */
590 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
591
592 snd_es18xx_rate_set(chip, substream, ADC1);
593
594 /* Transfer Count Reload */
595 count = 0x10000 - count;
596 snd_es18xx_write(chip, 0xA4, count & 0xff);
597 snd_es18xx_write(chip, 0xA5, count >> 8);
598
599#ifdef AVOID_POPS
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800600 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601#endif
602
603 /* Set format */
604 snd_es18xx_write(chip, 0xB7,
605 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
606 snd_es18xx_write(chip, 0xB7, 0x90 |
607 ((runtime->channels == 1) ? 0x40 : 0x08) |
608 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
609 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
610
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200611 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
613
614 return 0;
615}
616
Takashi Iwai8047c912005-11-17 14:41:22 +0100617static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int cmd)
619{
Takashi Iwai8047c912005-11-17 14:41:22 +0100620 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 switch (cmd) {
623 case SNDRV_PCM_TRIGGER_START:
624 case SNDRV_PCM_TRIGGER_RESUME:
625 if (chip->active & ADC1)
626 return 0;
627 chip->active |= ADC1;
628 /* Start DMA */
629 snd_es18xx_write(chip, 0xB8, 0x0f);
630 break;
631 case SNDRV_PCM_TRIGGER_STOP:
632 case SNDRV_PCM_TRIGGER_SUSPEND:
633 if (!(chip->active & ADC1))
634 return 0;
635 chip->active &= ~ADC1;
636 /* Stop DMA */
637 snd_es18xx_write(chip, 0xB8, 0x00);
638 break;
639 default:
640 return -EINVAL;
641 }
642
643 return 0;
644}
645
Takashi Iwai8047c912005-11-17 14:41:22 +0100646static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
647 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai8047c912005-11-17 14:41:22 +0100649 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
651 unsigned int count = snd_pcm_lib_period_bytes(substream);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 snd_es18xx_reset_fifo(chip);
654
655 /* Set stereo/mono */
656 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
657
658 snd_es18xx_rate_set(chip, substream, DAC1);
659
660 /* Transfer Count Reload */
661 count = 0x10000 - count;
662 snd_es18xx_write(chip, 0xA4, count & 0xff);
663 snd_es18xx_write(chip, 0xA5, count >> 8);
664
665 /* Set format */
666 snd_es18xx_write(chip, 0xB6,
667 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
668 snd_es18xx_write(chip, 0xB7,
669 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
670 snd_es18xx_write(chip, 0xB7, 0x90 |
671 (runtime->channels == 1 ? 0x40 : 0x08) |
672 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
673 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
674
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200675 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
677
678 return 0;
679}
680
Takashi Iwai8047c912005-11-17 14:41:22 +0100681static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
682 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int cmd)
684{
685 switch (cmd) {
686 case SNDRV_PCM_TRIGGER_START:
687 case SNDRV_PCM_TRIGGER_RESUME:
688 if (chip->active & DAC1)
689 return 0;
690 chip->active |= DAC1;
691 /* Start DMA */
692 snd_es18xx_write(chip, 0xB8, 0x05);
693#ifdef AVOID_POPS
694 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800695 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Enable Audio 1 */
697 snd_es18xx_dsp_command(chip, 0xD1);
698#endif
699 break;
700 case SNDRV_PCM_TRIGGER_STOP:
701 case SNDRV_PCM_TRIGGER_SUSPEND:
702 if (!(chip->active & DAC1))
703 return 0;
704 chip->active &= ~DAC1;
705 /* Stop DMA */
706 snd_es18xx_write(chip, 0xB8, 0x00);
707#ifdef AVOID_POPS
708 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800709 mdelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Disable Audio 1 */
711 snd_es18xx_dsp_command(chip, 0xD3);
712#endif
713 break;
714 default:
715 return -EINVAL;
716 }
717
718 return 0;
719}
720
Takashi Iwai8047c912005-11-17 14:41:22 +0100721static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai8047c912005-11-17 14:41:22 +0100723 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
725 return snd_es18xx_playback1_prepare(chip, substream);
726 else
727 return snd_es18xx_playback2_prepare(chip, substream);
728}
729
Takashi Iwai8047c912005-11-17 14:41:22 +0100730static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int cmd)
732{
Takashi Iwai8047c912005-11-17 14:41:22 +0100733 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
735 return snd_es18xx_playback1_trigger(chip, substream, cmd);
736 else
737 return snd_es18xx_playback2_trigger(chip, substream, cmd);
738}
739
David Howells7d12e782006-10-05 14:55:46 +0100740static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100742 struct snd_card *card = dev_id;
Krzysztof Heltb14f5de2009-10-25 11:10:01 +0100743 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned char status;
745
746 if (chip->caps & ES18XX_CONTROL) {
747 /* Read Interrupt status */
748 status = inb(chip->ctrl_port + 6);
749 } else {
750 /* Read Interrupt status */
751 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
752 }
753#if 0
754 else {
755 status = 0;
756 if (inb(chip->port + 0x0C) & 0x01)
757 status |= AUDIO1_IRQ;
758 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
759 status |= AUDIO2_IRQ;
760 if ((chip->caps & ES18XX_HWV) &&
761 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
762 status |= HWV_IRQ;
763 }
764#endif
765
766 /* Audio 1 & Audio 2 */
767 if (status & AUDIO2_IRQ) {
768 if (chip->active & DAC2)
769 snd_pcm_period_elapsed(chip->playback_a_substream);
770 /* ack interrupt */
771 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
772 }
773 if (status & AUDIO1_IRQ) {
774 /* ok.. capture is active */
775 if (chip->active & ADC1)
776 snd_pcm_period_elapsed(chip->capture_a_substream);
777 /* ok.. playback2 is active */
778 else if (chip->active & DAC1)
779 snd_pcm_period_elapsed(chip->playback_b_substream);
780 /* ack interrupt */
781 inb(chip->port + 0x0E);
782 }
783
784 /* MPU */
785 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100786 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Hardware volume */
789 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100790 int split = 0;
791 if (chip->caps & ES18XX_HWV) {
792 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100793 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
794 &chip->hw_switch->id);
795 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
796 &chip->hw_volume->id);
Mark Salazar14086772006-01-16 11:33:52 +0100797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!split) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100799 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
800 &chip->master_switch->id);
801 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
802 &chip->master_volume->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 /* ack interrupt */
805 snd_es18xx_mixer_write(chip, 0x66, 0x00);
806 }
807 return IRQ_HANDLED;
808}
809
Takashi Iwai8047c912005-11-17 14:41:22 +0100810static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwai8047c912005-11-17 14:41:22 +0100812 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100813 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int pos;
815
816 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
817 if (!(chip->active & DAC2))
818 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100819 pos = snd_dma_pointer(chip->dma2, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return pos >> chip->dma2_shift;
821 } else {
822 if (!(chip->active & DAC1))
823 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100824 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return pos >> chip->dma1_shift;
826 }
827}
828
Takashi Iwai8047c912005-11-17 14:41:22 +0100829static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai8047c912005-11-17 14:41:22 +0100831 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100832 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int pos;
834
835 if (!(chip->active & ADC1))
836 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100837 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return pos >> chip->dma1_shift;
839}
840
Takashi Iwai8047c912005-11-17 14:41:22 +0100841static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
844 SNDRV_PCM_INFO_RESUME |
845 SNDRV_PCM_INFO_MMAP_VALID),
846 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
847 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
848 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
849 .rate_min = 4000,
850 .rate_max = 48000,
851 .channels_min = 1,
852 .channels_max = 2,
853 .buffer_bytes_max = 65536,
854 .period_bytes_min = 64,
855 .period_bytes_max = 65536,
856 .periods_min = 1,
857 .periods_max = 1024,
858 .fifo_size = 0,
859};
860
Takashi Iwai8047c912005-11-17 14:41:22 +0100861static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
864 SNDRV_PCM_INFO_RESUME |
865 SNDRV_PCM_INFO_MMAP_VALID),
866 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
867 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
868 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
869 .rate_min = 4000,
870 .rate_max = 48000,
871 .channels_min = 1,
872 .channels_max = 2,
873 .buffer_bytes_max = 65536,
874 .period_bytes_min = 64,
875 .period_bytes_max = 65536,
876 .periods_min = 1,
877 .periods_max = 1024,
878 .fifo_size = 0,
879};
880
Takashi Iwai8047c912005-11-17 14:41:22 +0100881static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai8047c912005-11-17 14:41:22 +0100883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
887 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
888 chip->capture_a_substream &&
889 chip->capture_a_substream->runtime->channels != 1)
890 return -EAGAIN;
891 chip->playback_a_substream = substream;
892 } else if (substream->number <= 1) {
893 if (chip->capture_a_substream)
894 return -EAGAIN;
895 chip->playback_b_substream = substream;
896 } else {
897 snd_BUG();
898 return -EINVAL;
899 }
900 substream->runtime->hw = snd_es18xx_playback;
901 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
902 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
903 return 0;
904}
905
Takashi Iwai8047c912005-11-17 14:41:22 +0100906static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai8047c912005-11-17 14:41:22 +0100908 struct snd_pcm_runtime *runtime = substream->runtime;
909 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (chip->playback_b_substream)
912 return -EAGAIN;
913 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
914 chip->playback_a_substream &&
915 chip->playback_a_substream->runtime->channels != 1)
916 return -EAGAIN;
917 chip->capture_a_substream = substream;
918 substream->runtime->hw = snd_es18xx_capture;
919 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
920 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
921 return 0;
922}
923
Takashi Iwai8047c912005-11-17 14:41:22 +0100924static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwai8047c912005-11-17 14:41:22 +0100926 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
929 chip->playback_a_substream = NULL;
930 else
931 chip->playback_b_substream = NULL;
932
933 snd_pcm_lib_free_pages(substream);
934 return 0;
935}
936
Takashi Iwai8047c912005-11-17 14:41:22 +0100937static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwai8047c912005-11-17 14:41:22 +0100939 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 chip->capture_a_substream = NULL;
942 snd_pcm_lib_free_pages(substream);
943 return 0;
944}
945
946/*
947 * MIXER part
948 */
949
Mark Salazarf4df2212006-01-16 11:31:14 +0100950/* Record source mux routines:
951 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
952 * bit table for the 4/5 source mux:
953 * reg 1C:
954 * b2 b1 b0 muxSource
955 * x 0 x microphone
956 * 0 1 x CD
957 * 1 1 0 line
958 * 1 1 1 mixer
959 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
960 * either the play mixer or the capture mixer.
961 *
962 * "map4Source" translates from source number to reg bit pattern
963 * "invMap4Source" translates from reg bit pattern to source number
964 */
965
Takashi Iwai8047c912005-11-17 14:41:22 +0100966static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Takashi Iwai4e283502014-10-20 18:14:24 +0200968 static const char * const texts5Source[5] = {
Mark Salazarf4df2212006-01-16 11:31:14 +0100969 "Mic", "CD", "Line", "Master", "Mix"
970 };
Takashi Iwai4e283502014-10-20 18:14:24 +0200971 static const char * const texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 "Mic", "Mic Master", "CD", "AOUT",
973 "Mic1", "Mix", "Line", "Master"
974 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100975 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mark Salazarf4df2212006-01-16 11:31:14 +0100977 switch (chip->version) {
978 case 0x1868:
979 case 0x1878:
Takashi Iwai4e283502014-10-20 18:14:24 +0200980 return snd_ctl_enum_info(uinfo, 1, 4, texts5Source);
Mark Salazarf4df2212006-01-16 11:31:14 +0100981 case 0x1887:
982 case 0x1888:
Takashi Iwai4e283502014-10-20 18:14:24 +0200983 return snd_ctl_enum_info(uinfo, 1, 5, texts5Source);
Mark Salazarf4df2212006-01-16 11:31:14 +0100984 case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
985 case 0x1879:
Takashi Iwai4e283502014-10-20 18:14:24 +0200986 return snd_ctl_enum_info(uinfo, 1, 8, texts8Source);
Mark Salazarf4df2212006-01-16 11:31:14 +0100987 default:
988 return -EINVAL;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Takashi Iwai8047c912005-11-17 14:41:22 +0100992static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Mark Salazarf4df2212006-01-16 11:31:14 +0100994 static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +0100995 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +0100996 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
997 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
998 muxSource = invMap4Source[muxSource];
999 if (muxSource==3 &&
1000 (chip->version == 0x1887 || chip->version == 0x1888) &&
1001 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1002 )
1003 muxSource = 4;
1004 }
1005 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
1007}
1008
Takashi Iwai8047c912005-11-17 14:41:22 +01001009static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Mark Salazarf4df2212006-01-16 11:31:14 +01001011 static unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +01001012 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +01001014 unsigned char retVal = 0;
1015
1016 switch (chip->version) {
1017 /* 5 source chips */
1018 case 0x1887:
1019 case 0x1888:
1020 if (val > 4)
1021 return -EINVAL;
1022 if (val == 4) {
1023 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1024 val = 3;
1025 } else
1026 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1027 /* 4 source chips */
1028 case 0x1868:
1029 case 0x1878:
1030 if (val > 3)
1031 return -EINVAL;
1032 val = map4Source[val];
1033 break;
1034 /* 8 source chips */
1035 case 0x1869:
1036 case 0x1879:
1037 if (val > 7)
1038 return -EINVAL;
1039 break;
1040 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001042 }
1043 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001046#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Takashi Iwai8047c912005-11-17 14:41:22 +01001048static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai8047c912005-11-17 14:41:22 +01001050 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1052 ucontrol->value.integer.value[0] = !!(val & 8);
1053 return 0;
1054}
1055
Takashi Iwai8047c912005-11-17 14:41:22 +01001056static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai8047c912005-11-17 14:41:22 +01001058 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 unsigned char oval, nval;
1060 int change;
1061 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1062 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1063 change = nval != oval;
1064 if (change) {
1065 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1066 snd_es18xx_mixer_write(chip, 0x50, nval);
1067 }
1068 return change;
1069}
1070
Takashi Iwai8047c912005-11-17 14:41:22 +01001071static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1074 uinfo->count = 2;
1075 uinfo->value.integer.min = 0;
1076 uinfo->value.integer.max = 63;
1077 return 0;
1078}
1079
Takashi Iwai8047c912005-11-17 14:41:22 +01001080static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Takashi Iwai8047c912005-11-17 14:41:22 +01001082 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1084 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1085 return 0;
1086}
1087
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001088#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Takashi Iwai8047c912005-11-17 14:41:22 +01001090static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai8047c912005-11-17 14:41:22 +01001092 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1094 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1095 return 0;
1096}
1097
Takashi Iwai8047c912005-11-17 14:41:22 +01001098static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Takashi Iwai8047c912005-11-17 14:41:22 +01001100 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 chip->master_volume = NULL;
1102 chip->master_switch = NULL;
1103 chip->hw_volume = NULL;
1104 chip->hw_switch = NULL;
1105}
1106
Takashi Iwai8047c912005-11-17 14:41:22 +01001107static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 unsigned char mask, unsigned char val)
1109{
1110 if (reg < 0xa0)
1111 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1112 else
1113 return snd_es18xx_bits(chip, reg, mask, val);
1114}
1115
Takashi Iwai8047c912005-11-17 14:41:22 +01001116static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 if (reg < 0xa0)
1119 return snd_es18xx_mixer_read(chip, reg);
1120 else
1121 return snd_es18xx_read(chip, reg);
1122}
1123
Ondrej Zary2603fe22014-11-03 21:35:48 +01001124#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1126 .info = snd_es18xx_info_single, \
1127 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
Ondrej Zary2603fe22014-11-03 21:35:48 +01001128 .private_value = reg | (shift << 8) | (mask << 16) | (flags << 24) }
1129
1130#define ES18XX_FL_INVERT (1 << 0)
1131#define ES18XX_FL_PMPORT (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Takashi Iwai8047c912005-11-17 14:41:22 +01001133static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int mask = (kcontrol->private_value >> 16) & 0xff;
1136
1137 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1138 uinfo->count = 1;
1139 uinfo->value.integer.min = 0;
1140 uinfo->value.integer.max = mask;
1141 return 0;
1142}
1143
Takashi Iwai8047c912005-11-17 14:41:22 +01001144static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Takashi Iwai8047c912005-11-17 14:41:22 +01001146 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int reg = kcontrol->private_value & 0xff;
1148 int shift = (kcontrol->private_value >> 8) & 0xff;
1149 int mask = (kcontrol->private_value >> 16) & 0xff;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001150 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1151 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int val;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001153
1154 if (pm_port)
1155 val = inb(chip->port + ES18XX_PM);
1156 else
1157 val = snd_es18xx_reg_read(chip, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1159 if (invert)
1160 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1161 return 0;
1162}
1163
Takashi Iwai8047c912005-11-17 14:41:22 +01001164static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Takashi Iwai8047c912005-11-17 14:41:22 +01001166 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int reg = kcontrol->private_value & 0xff;
1168 int shift = (kcontrol->private_value >> 8) & 0xff;
1169 int mask = (kcontrol->private_value >> 16) & 0xff;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001170 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1171 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 unsigned char val;
1173
1174 val = (ucontrol->value.integer.value[0] & mask);
1175 if (invert)
1176 val = mask - val;
1177 mask <<= shift;
1178 val <<= shift;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001179 if (pm_port) {
1180 unsigned char cur = inb(chip->port + ES18XX_PM);
1181
1182 if ((cur & mask) == val)
1183 return 0;
1184 outb((cur & ~mask) | val, chip->port + ES18XX_PM);
1185 return 1;
1186 }
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1189}
1190
1191#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1192{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1193 .info = snd_es18xx_info_double, \
1194 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1195 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1196
Takashi Iwai8047c912005-11-17 14:41:22 +01001197static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 int mask = (kcontrol->private_value >> 24) & 0xff;
1200
1201 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1202 uinfo->count = 2;
1203 uinfo->value.integer.min = 0;
1204 uinfo->value.integer.max = mask;
1205 return 0;
1206}
1207
Takashi Iwai8047c912005-11-17 14:41:22 +01001208static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwai8047c912005-11-17 14:41:22 +01001210 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int left_reg = kcontrol->private_value & 0xff;
1212 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1213 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1214 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1215 int mask = (kcontrol->private_value >> 24) & 0xff;
1216 int invert = (kcontrol->private_value >> 22) & 1;
1217 unsigned char left, right;
1218
1219 left = snd_es18xx_reg_read(chip, left_reg);
1220 if (left_reg != right_reg)
1221 right = snd_es18xx_reg_read(chip, right_reg);
1222 else
1223 right = left;
1224 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1225 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1226 if (invert) {
1227 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1228 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1229 }
1230 return 0;
1231}
1232
Takashi Iwai8047c912005-11-17 14:41:22 +01001233static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Takashi Iwai8047c912005-11-17 14:41:22 +01001235 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int left_reg = kcontrol->private_value & 0xff;
1237 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1238 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1239 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1240 int mask = (kcontrol->private_value >> 24) & 0xff;
1241 int invert = (kcontrol->private_value >> 22) & 1;
1242 int change;
1243 unsigned char val1, val2, mask1, mask2;
1244
1245 val1 = ucontrol->value.integer.value[0] & mask;
1246 val2 = ucontrol->value.integer.value[1] & mask;
1247 if (invert) {
1248 val1 = mask - val1;
1249 val2 = mask - val2;
1250 }
1251 val1 <<= shift_left;
1252 val2 <<= shift_right;
1253 mask1 = mask << shift_left;
1254 mask2 = mask << shift_right;
1255 if (left_reg != right_reg) {
1256 change = 0;
1257 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1258 change = 1;
1259 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1260 change = 1;
1261 } else {
1262 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1263 val1 | val2) != (val1 | val2));
1264 }
1265 return change;
1266}
1267
Mark Salazarc1f6d412006-01-16 11:29:09 +01001268/* Mixer controls
1269 * These arrays contain setup data for mixer controls.
1270 *
1271 * The controls that are universal to all chipsets are fully initialized
1272 * here.
1273 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001274static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1276ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1277ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1278ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1279ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1281ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1283ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1286 .name = "Capture Source",
1287 .info = snd_es18xx_info_mux,
1288 .get = snd_es18xx_get_mux,
1289 .put = snd_es18xx_put_mux,
1290}
1291};
1292
Takashi Iwai8047c912005-11-17 14:41:22 +01001293static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1295ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1296ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1297ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1299ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1300};
1301
Mark Salazarc1f6d412006-01-16 11:29:09 +01001302/*
1303 * The chipset specific mixer controls
1304 */
1305static struct snd_kcontrol_new snd_es18xx_opt_speaker =
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001306 ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001307
1308static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
Ondrej Zary2603fe22014-11-03 21:35:48 +01001309ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT),
Mark Salazar95b71292006-01-16 11:35:40 +01001310ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001311ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1312ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1313};
1314
Mark Salazar95b71292006-01-16 11:35:40 +01001315static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1316 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1317
1318static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1319ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1320ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1321ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1322};
1323
Takashi Iwai8047c912005-11-17 14:41:22 +01001324static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1326};
1327
Takashi Iwai8047c912005-11-17 14:41:22 +01001328static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1330ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1331};
1332
Takashi Iwai8047c912005-11-17 14:41:22 +01001333static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1335{
1336 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1337 .name = "3D Control - Switch",
1338 .info = snd_es18xx_info_spatializer_enable,
1339 .get = snd_es18xx_get_spatializer_enable,
1340 .put = snd_es18xx_put_spatializer_enable,
1341}
1342};
1343
Takashi Iwai8047c912005-11-17 14:41:22 +01001344static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1346
Takashi Iwai8047c912005-11-17 14:41:22 +01001347static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1349
Takashi Iwai8047c912005-11-17 14:41:22 +01001350static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1353 .name = "Hardware Master Playback Volume",
1354 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1355 .info = snd_es18xx_info_hw_volume,
1356 .get = snd_es18xx_get_hw_volume,
1357},
1358{
1359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1360 .name = "Hardware Master Playback Switch",
1361 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1362 .info = snd_es18xx_info_hw_switch,
1363 .get = snd_es18xx_get_hw_switch,
1364},
1365ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1366};
1367
Ondrej Zary2603fe22014-11-03 21:35:48 +01001368static struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = {
1369ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT),
1370ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT),
1371};
1372
Bill Pemberton1bff2922012-12-06 12:35:21 -05001373static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 int data;
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 outb(reg, chip->ctrl_port);
1378 data = inb(chip->ctrl_port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return data;
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Bill Pemberton1bff2922012-12-06 12:35:21 -05001382static void snd_es18xx_config_write(struct snd_es18xx *chip,
1383 unsigned char reg, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 /* No need for spinlocks, this function is used only in
1386 otherwise protected init code */
1387 outb(reg, chip->ctrl_port);
1388 outb(data, chip->ctrl_port + 1);
1389#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001390 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391#endif
1392}
1393
Bill Pemberton1bff2922012-12-06 12:35:21 -05001394static int snd_es18xx_initialize(struct snd_es18xx *chip,
1395 unsigned long mpu_port,
1396 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 int mask = 0;
1399
1400 /* enable extended mode */
1401 snd_es18xx_dsp_command(chip, 0xC6);
1402 /* Reset mixer registers */
1403 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1404
1405 /* Audio 1 DMA demand mode (4 bytes/request) */
1406 snd_es18xx_write(chip, 0xB9, 2);
1407 if (chip->caps & ES18XX_CONTROL) {
1408 /* Hardware volume IRQ */
1409 snd_es18xx_config_write(chip, 0x27, chip->irq);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001410 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* FM I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001412 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1413 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001415 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* MPU-401 I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001417 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1418 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* MPU-401 IRQ */
1420 snd_es18xx_config_write(chip, 0x28, chip->irq);
1421 }
1422 /* Audio1 IRQ */
1423 snd_es18xx_config_write(chip, 0x70, chip->irq);
1424 /* Audio2 IRQ */
1425 snd_es18xx_config_write(chip, 0x72, chip->irq);
1426 /* Audio1 DMA */
1427 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1428 /* Audio2 DMA */
1429 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1430
1431 /* Enable Audio 1 IRQ */
1432 snd_es18xx_write(chip, 0xB1, 0x50);
1433 /* Enable Audio 2 IRQ */
1434 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1435 /* Enable Audio 1 DMA */
1436 snd_es18xx_write(chip, 0xB2, 0x50);
1437 /* Enable MPU and hardware volume interrupt */
1438 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001439 /* Enable ESS wavetable input */
1440 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 else {
1443 int irqmask, dma1mask, dma2mask;
1444 switch (chip->irq) {
1445 case 2:
1446 case 9:
1447 irqmask = 0;
1448 break;
1449 case 5:
1450 irqmask = 1;
1451 break;
1452 case 7:
1453 irqmask = 2;
1454 break;
1455 case 10:
1456 irqmask = 3;
1457 break;
1458 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001459 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -ENODEV;
1461 }
1462 switch (chip->dma1) {
1463 case 0:
1464 dma1mask = 1;
1465 break;
1466 case 1:
1467 dma1mask = 2;
1468 break;
1469 case 3:
1470 dma1mask = 3;
1471 break;
1472 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001473 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -ENODEV;
1475 }
1476 switch (chip->dma2) {
1477 case 0:
1478 dma2mask = 0;
1479 break;
1480 case 1:
1481 dma2mask = 1;
1482 break;
1483 case 3:
1484 dma2mask = 2;
1485 break;
1486 case 5:
1487 dma2mask = 3;
1488 break;
1489 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001490 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -ENODEV;
1492 }
1493
1494 /* Enable and set Audio 1 IRQ */
1495 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1496 /* Enable and set Audio 1 DMA */
1497 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1498 /* Set Audio 2 DMA */
1499 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1500 /* Enable Audio 2 IRQ and DMA
1501 Set capture mixer input */
1502 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1503 /* Enable and set hardware volume interrupt */
1504 snd_es18xx_mixer_write(chip, 0x64, 0x06);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001505 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* MPU401 share irq with audio
1507 Joystick enabled
1508 FM enabled */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001509 snd_es18xx_mixer_write(chip, 0x40,
1510 0x43 | (mpu_port & 0xf0) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1513 }
1514 if (chip->caps & ES18XX_NEW_RATE) {
1515 /* Change behaviour of register A1
1516 4x oversampling
1517 2nd channel DAC asynchronous */
1518 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1519 }
1520 if (!(chip->caps & ES18XX_PCM2)) {
1521 /* Enable DMA FIFO */
1522 snd_es18xx_write(chip, 0xB7, 0x80);
1523 }
1524 if (chip->caps & ES18XX_SPATIALIZER) {
1525 /* Set spatializer parameters to recommended values */
1526 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1527 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1528 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1529 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1530 }
Mark Salazar95b71292006-01-16 11:35:40 +01001531 /* Flip the "enable I2S" bits for those chipsets that need it */
1532 switch (chip->version) {
1533 case 0x1879:
1534 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1535 //so a Switch control has been added to toggle this 0x71 bit on/off:
1536 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1537 /* Note: we fall through on purpose here. */
1538 case 0x1878:
1539 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1540 break;
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /* Mute input source */
1543 if (chip->caps & ES18XX_MUTEREC)
1544 mask = 0x10;
1545 if (chip->caps & ES18XX_RECMIX)
1546 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1547 else {
1548 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1549 snd_es18xx_write(chip, 0xb4, 0x00);
1550 }
1551#ifndef AVOID_POPS
1552 /* Enable PCM output */
1553 snd_es18xx_dsp_command(chip, 0xD1);
1554#endif
1555
1556 return 0;
1557}
1558
Bill Pemberton1bff2922012-12-06 12:35:21 -05001559static int snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 int hi,lo;
1562
1563 /* reset */
1564 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001565 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -ENODEV;
1567 }
1568
1569 snd_es18xx_dsp_command(chip, 0xe7);
1570 hi = snd_es18xx_dsp_get_byte(chip);
1571 if (hi < 0) {
1572 return hi;
1573 }
1574 lo = snd_es18xx_dsp_get_byte(chip);
1575 if ((lo & 0xf0) != 0x80) {
1576 return -ENODEV;
1577 }
1578 if (hi == 0x48) {
1579 chip->version = 0x488;
1580 return 0;
1581 }
1582 if (hi != 0x68) {
1583 return -ENODEV;
1584 }
1585 if ((lo & 0x0f) < 8) {
1586 chip->version = 0x688;
1587 return 0;
1588 }
1589
1590 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001591 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001593 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 lo = inb(chip->port + 0x05);
1595 if (hi != lo) {
1596 chip->version = hi << 8 | lo;
1597 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001598 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 chip->ctrl_port += inb(chip->port + 0x05);
1600
1601 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1602 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1603 return -EBUSY;
1604 }
1605
1606 return 0;
1607 }
1608
1609 /* If has Hardware volume */
1610 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1611 /* If has Audio2 */
1612 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1613 /* If has volume count */
1614 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1615 chip->version = 0x1887;
1616 } else {
1617 chip->version = 0x1888;
1618 }
1619 } else {
1620 chip->version = 0x1788;
1621 }
1622 }
1623 else
1624 chip->version = 0x1688;
1625 return 0;
1626}
1627
Bill Pemberton1bff2922012-12-06 12:35:21 -05001628static int snd_es18xx_probe(struct snd_es18xx *chip,
1629 unsigned long mpu_port,
1630 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 if (snd_es18xx_identify(chip) < 0) {
1633 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1634 return -ENODEV;
1635 }
1636
1637 switch (chip->version) {
1638 case 0x1868:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001639 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case 0x1869:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001642 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001645 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647 case 0x1879:
1648 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1649 break;
1650 case 0x1887:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 case 0x1888:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001652 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001655 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 chip->port, chip->version);
1657 return -ENODEV;
1658 }
1659
1660 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1661
1662 if (chip->dma1 == chip->dma2)
1663 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1664
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001665 return snd_es18xx_initialize(chip, mpu_port, fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Takashi Iwai8047c912005-11-17 14:41:22 +01001668static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 .open = snd_es18xx_playback_open,
1670 .close = snd_es18xx_playback_close,
1671 .ioctl = snd_pcm_lib_ioctl,
1672 .hw_params = snd_es18xx_playback_hw_params,
1673 .hw_free = snd_es18xx_pcm_hw_free,
1674 .prepare = snd_es18xx_playback_prepare,
1675 .trigger = snd_es18xx_playback_trigger,
1676 .pointer = snd_es18xx_playback_pointer,
1677};
1678
Takashi Iwai8047c912005-11-17 14:41:22 +01001679static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 .open = snd_es18xx_capture_open,
1681 .close = snd_es18xx_capture_close,
1682 .ioctl = snd_pcm_lib_ioctl,
1683 .hw_params = snd_es18xx_capture_hw_params,
1684 .hw_free = snd_es18xx_pcm_hw_free,
1685 .prepare = snd_es18xx_capture_prepare,
1686 .trigger = snd_es18xx_capture_trigger,
1687 .pointer = snd_es18xx_capture_pointer,
1688};
1689
Bill Pemberton1bff2922012-12-06 12:35:21 -05001690static int snd_es18xx_pcm(struct snd_card *card, int device,
1691 struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001693 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001694 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 char str[16];
1696 int err;
1697
1698 if (rpcm)
1699 *rpcm = NULL;
1700 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001701 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001702 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001703 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001704 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (err < 0)
1706 return err;
1707
1708 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1709 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1710
1711 /* global setup */
1712 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 pcm->info_flags = 0;
1714 if (chip->caps & ES18XX_DUPLEX_SAME)
1715 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1716 if (! (chip->caps & ES18XX_PCM2))
1717 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1718 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1719 chip->pcm = pcm;
1720
1721 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1722 snd_dma_isa_data(),
1723 64*1024,
1724 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1725
1726 if (rpcm)
1727 *rpcm = pcm;
1728 return 0;
1729}
1730
1731/* Power Management support functions */
1732#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001733static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001735 struct snd_es18xx *chip = card->private_data;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001736
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001737 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 snd_pcm_suspend_all(chip->pcm);
1740
1741 /* power down */
1742 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1743 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1744 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1745 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1746
1747 return 0;
1748}
1749
Takashi Iwai8047c912005-11-17 14:41:22 +01001750static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001752 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1755 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1756
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001757 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760#endif /* CONFIG_PM */
1761
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001762static int snd_es18xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001764 struct snd_es18xx *chip = card->private_data;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001765
Takashi Iwaib1d57762005-10-10 11:56:31 +02001766 release_and_free_resource(chip->res_port);
1767 release_and_free_resource(chip->res_ctrl_port);
1768 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (chip->irq >= 0)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001770 free_irq(chip->irq, (void *) card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (chip->dma1 >= 0) {
1772 disable_dma(chip->dma1);
1773 free_dma(chip->dma1);
1774 }
1775 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1776 disable_dma(chip->dma2);
1777 free_dma(chip->dma2);
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return 0;
1780}
1781
Takashi Iwai8047c912005-11-17 14:41:22 +01001782static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001784 return snd_es18xx_free(device->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Bill Pemberton1bff2922012-12-06 12:35:21 -05001787static int snd_es18xx_new_device(struct snd_card *card,
1788 unsigned long port,
1789 unsigned long mpu_port,
1790 unsigned long fm_port,
1791 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001793 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001794 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .dev_free = snd_es18xx_dev_free,
1796 };
1797 int err;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 spin_lock_init(&chip->reg_lock);
1800 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 chip->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 chip->irq = -1;
1803 chip->dma1 = -1;
1804 chip->dma2 = -1;
1805 chip->audio2_vol = 0x00;
1806 chip->active = 0;
1807
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001808 chip->res_port = request_region(port, 16, "ES18xx");
1809 if (chip->res_port == NULL) {
1810 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1812 return -EBUSY;
1813 }
1814
Yong Zhang88e24c32011-09-22 16:59:20 +08001815 if (request_irq(irq, snd_es18xx_interrupt, 0, "ES18xx",
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001816 (void *) card)) {
1817 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1819 return -EBUSY;
1820 }
1821 chip->irq = irq;
1822
1823 if (request_dma(dma1, "ES18xx DMA 1")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001824 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1826 return -EBUSY;
1827 }
1828 chip->dma1 = dma1;
1829
1830 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001831 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1833 return -EBUSY;
1834 }
1835 chip->dma2 = dma2;
1836
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001837 if (snd_es18xx_probe(chip, mpu_port, fm_port) < 0) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001838 snd_es18xx_free(card);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001839 return -ENODEV;
1840 }
1841 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001842 if (err < 0) {
1843 snd_es18xx_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
1847}
1848
Bill Pemberton1bff2922012-12-06 12:35:21 -05001849static int snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001851 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 int err;
1853 unsigned int idx;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 strcpy(card->mixername, chip->pcm->name);
1856
1857 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001858 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1860 if (chip->caps & ES18XX_HWV) {
1861 switch (idx) {
1862 case 0:
1863 chip->master_volume = kctl;
1864 kctl->private_free = snd_es18xx_hwv_free;
1865 break;
1866 case 1:
1867 chip->master_switch = kctl;
1868 kctl->private_free = snd_es18xx_hwv_free;
1869 break;
1870 }
1871 }
1872 if ((err = snd_ctl_add(card, kctl)) < 0)
1873 return err;
1874 }
1875 if (chip->caps & ES18XX_PCM2) {
1876 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1877 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1878 return err;
1879 }
1880 } else {
1881 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1882 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1883 return err;
1884 }
1885 }
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (chip->caps & ES18XX_RECMIX) {
1888 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1889 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1890 return err;
1891 }
1892 }
1893 switch (chip->version) {
1894 default:
1895 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1896 return err;
1897 break;
1898 case 0x1869:
1899 case 0x1879:
1900 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1901 return err;
1902 break;
1903 }
1904 if (chip->caps & ES18XX_SPATIALIZER) {
1905 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1906 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1907 return err;
1908 }
1909 }
1910 if (chip->caps & ES18XX_HWV) {
1911 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001912 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1914 if (idx == 0)
1915 chip->hw_volume = kctl;
1916 else
1917 chip->hw_switch = kctl;
1918 kctl->private_free = snd_es18xx_hwv_free;
1919 if ((err = snd_ctl_add(card, kctl)) < 0)
1920 return err;
1921
1922 }
1923 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001924 /* finish initializing other chipset specific controls
1925 */
1926 if (chip->version != 0x1868) {
1927 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1928 chip));
1929 if (err < 0)
1930 return err;
1931 }
1932 if (chip->version == 0x1869) {
1933 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1934 err = snd_ctl_add(card,
1935 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1936 chip));
1937 if (err < 0)
1938 return err;
1939 }
Mark Salazar95b71292006-01-16 11:35:40 +01001940 } else if (chip->version == 0x1878) {
1941 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1942 chip));
1943 if (err < 0)
1944 return err;
1945 } else if (chip->version == 0x1879) {
1946 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1947 err = snd_ctl_add(card,
1948 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1949 chip));
1950 if (err < 0)
1951 return err;
1952 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001953 }
Ondrej Zary2603fe22014-11-03 21:35:48 +01001954 if (chip->caps & ES18XX_GPO_2BIT) {
1955 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
1956 err = snd_ctl_add(card,
1957 snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
1958 chip));
1959 if (err < 0)
1960 return err;
1961 }
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964}
1965
1966
1967/* Card level */
1968
1969MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1970MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1971MODULE_LICENSE("GPL");
1972MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1973 "{ESS,ES1869 PnP AudioDrive},"
1974 "{ESS,ES1878 PnP AudioDrive},"
1975 "{ESS,ES1879 PnP AudioDrive},"
1976 "{ESS,ES1887 PnP AudioDrive},"
1977 "{ESS,ES1888 PnP AudioDrive},"
1978 "{ESS,ES1887 AudioDrive},"
1979 "{ESS,ES1888 AudioDrive}}");
1980
1981static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1982static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +10301983static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +10301985static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#endif
1987static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1988#ifndef CONFIG_PNP
1989static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1990#else
1991static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1992#endif
1993static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1994static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1995static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1996static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1997
1998module_param_array(index, int, NULL, 0444);
1999MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2000module_param_array(id, charp, NULL, 0444);
2001MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2002module_param_array(enable, bool, NULL, 0444);
2003MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2004#ifdef CONFIG_PNP
2005module_param_array(isapnp, bool, NULL, 0444);
2006MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2007#endif
2008module_param_array(port, long, NULL, 0444);
2009MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2010module_param_array(mpu_port, long, NULL, 0444);
2011MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2012module_param_array(fm_port, long, NULL, 0444);
2013MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2014module_param_array(irq, int, NULL, 0444);
2015MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2016module_param_array(dma1, int, NULL, 0444);
2017MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2018module_param_array(dma2, int, NULL, 0444);
2019MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002022static int isa_registered;
2023static int pnp_registered;
2024static int pnpc_registered;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002025
2026static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2027 { .id = "ESS1869" },
Rene Herman4848ffe2007-08-01 23:50:21 +02002028 { .id = "ESS1879" },
Ondrej Zary1c3985582006-07-31 12:51:57 +02002029 { .id = "" } /* end */
2030};
2031
2032MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2033
2034/* PnP main device initialization */
Bill Pemberton1bff2922012-12-06 12:35:21 -05002035static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002036{
Rene Herman109c53f842007-11-30 17:59:25 +01002037 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c3985582006-07-31 12:51:57 +02002038 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2039 return -EBUSY;
2040 }
2041 /* ok. hack using Vendor-Defined Card-Level registers */
2042 /* skip csn and logdev initialization - already done in isapnp_configure */
2043 if (pnp_device_is_isapnp(pdev)) {
2044 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2045 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
2046 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2047 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2048 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
2049 isapnp_cfg_end();
2050 }
2051 port[dev] = pnp_port_start(pdev, 0);
2052 fm_port[dev] = pnp_port_start(pdev, 1);
2053 mpu_port[dev] = pnp_port_start(pdev, 2);
2054 dma1[dev] = pnp_dma(pdev, 0);
2055 dma2[dev] = pnp_dma(pdev, 1);
2056 irq[dev] = pnp_irq(pdev, 0);
2057 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2058 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2059 return 0;
2060}
2061
Bill Pemberton1bff2922012-12-06 12:35:21 -05002062static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
2063 struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002064{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002065 chip->dev = pdev;
2066 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002067 return -EBUSY;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002068 return 0;
2069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2072 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2073 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2074 /* ESS 1868 (integrated on Maxisound Cards) */
2075 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2076 /* ESS 1868 (integrated on Maxisound Cards) */
2077 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2078 /* ESS ES1869 Plug and Play AudioDrive */
2079 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2080 /* ESS 1869 */
2081 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2082 /* ESS 1878 */
2083 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2084 /* ESS 1879 */
2085 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2086 /* --- */
2087 { .id = "" } /* end */
2088};
2089
2090MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2091
Bill Pemberton1bff2922012-12-06 12:35:21 -05002092static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2093 struct pnp_card_link *card,
2094 const struct pnp_card_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002096 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2097 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002099
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002100 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2101 if (chip->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /* Control port initialization */
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002105 if (pnp_activate_dev(chip->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2107 return -EAGAIN;
2108 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002109 snd_printdd("pnp: port=0x%llx\n",
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002110 (unsigned long long)pnp_port_start(chip->devc, 0));
2111 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116#endif /* CONFIG_PNP */
2117
Takashi Iwai43bcd972005-09-05 17:19:20 +02002118#ifdef CONFIG_PNP
2119#define is_isapnp_selected(dev) isapnp[dev]
2120#else
2121#define is_isapnp_selected(dev) 0
2122#endif
2123
Takashi Iwai4323cc42014-01-29 13:03:56 +01002124static int snd_es18xx_card_new(struct device *pdev, int dev,
2125 struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Takashi Iwai4323cc42014-01-29 13:03:56 +01002127 return snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2128 sizeof(struct snd_es18xx), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002129}
2130
Bill Pemberton1bff2922012-12-06 12:35:21 -05002131static int snd_audiodrive_probe(struct snd_card *card, int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002132{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002133 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002134 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int err;
2136
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002137 err = snd_es18xx_new_device(card,
2138 port[dev], mpu_port[dev], fm_port[dev],
2139 irq[dev], dma1[dev], dma2[dev]);
2140 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002146 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2148 card->shortname,
2149 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002150 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 else
2152 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2153 card->shortname,
2154 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002155 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002157 err = snd_es18xx_pcm(card, 0, NULL);
2158 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002159 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002160
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002161 err = snd_es18xx_mixer(card);
2162 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002166 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2167 OPL3_HW_OPL3, 0, &opl3) < 0) {
2168 snd_printk(KERN_WARNING PFX
2169 "opl3 not detected at 0x%lx\n",
2170 fm_port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 } else {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002172 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2173 if (err < 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) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002179 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002180 mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2181 -1, &chip->rmidi);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002182 if (err < 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
Bill Pemberton1bff2922012-12-06 12:35:21 -05002189static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002190{
2191 return enable[dev] && !is_isapnp_selected(dev);
2192}
2193
Bill Pemberton1bff2922012-12-06 12:35:21 -05002194static int 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 Iwai4323cc42014-01-29 13:03:56 +01002199 err = snd_es18xx_card_new(devptr, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002200 if (err < 0)
2201 return err;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002202 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2203 snd_card_free(card);
2204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002206 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Bill Pemberton1bff2922012-12-06 12:35:21 -05002210static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002211{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002212 int err;
2213 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2214 static int possible_dmas[] = {1, 0, 3, 5, -1};
2215
2216 if (irq[dev] == SNDRV_AUTO_IRQ) {
2217 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2218 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2219 return -EBUSY;
2220 }
2221 }
2222 if (dma1[dev] == SNDRV_AUTO_DMA) {
2223 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2224 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2225 return -EBUSY;
2226 }
2227 }
2228 if (dma2[dev] == SNDRV_AUTO_DMA) {
2229 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2230 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2231 return -EBUSY;
2232 }
2233 }
2234
2235 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002236 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002237 } else {
2238 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2239 int i;
2240 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2241 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002242 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002243 if (! err)
2244 return 0;
2245 }
2246 return err;
2247 }
2248}
2249
Bill Pemberton1bff2922012-12-06 12:35:21 -05002250static int snd_es18xx_isa_remove(struct device *devptr,
2251 unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002252{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002253 snd_card_free(dev_get_drvdata(devptr));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002254 return 0;
2255}
2256
2257#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002258static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2259 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002260{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002261 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002262}
2263
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002264static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002265{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002266 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002267}
2268#endif
2269
Rene Herman83c51c02007-03-20 11:33:46 +01002270#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002271
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002272static struct isa_driver snd_es18xx_isa_driver = {
2273 .match = snd_es18xx_isa_match,
2274 .probe = snd_es18xx_isa_probe,
Bill Pemberton1bff2922012-12-06 12:35:21 -05002275 .remove = snd_es18xx_isa_remove,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002276#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002277 .suspend = snd_es18xx_isa_suspend,
2278 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002279#endif
2280 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002281 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002282 },
2283};
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -05002287static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2288 const struct pnp_device_id *id)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002289{
2290 static int dev;
2291 int err;
2292 struct snd_card *card;
2293
2294 if (pnp_device_is_isapnp(pdev))
2295 return -ENOENT; /* we have another procedure - card */
2296 for (; dev < SNDRV_CARDS; dev++) {
2297 if (enable[dev] && isapnp[dev])
2298 break;
2299 }
2300 if (dev >= SNDRV_CARDS)
2301 return -ENODEV;
2302
Takashi Iwai4323cc42014-01-29 13:03:56 +01002303 err = snd_es18xx_card_new(&pdev->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002304 if (err < 0)
2305 return err;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002306 if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2307 snd_card_free(card);
2308 return err;
2309 }
Ondrej Zary1c3985582006-07-31 12:51:57 +02002310 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2311 snd_card_free(card);
2312 return err;
2313 }
2314 pnp_set_drvdata(pdev, card);
2315 dev++;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002316 return 0;
2317}
2318
Bill Pemberton1bff2922012-12-06 12:35:21 -05002319static void snd_audiodrive_pnp_remove(struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002320{
2321 snd_card_free(pnp_get_drvdata(pdev));
Ondrej Zary1c3985582006-07-31 12:51:57 +02002322}
2323
2324#ifdef CONFIG_PM
2325static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2326{
2327 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2328}
2329static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2330{
2331 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2332}
2333#endif
2334
2335static struct pnp_driver es18xx_pnp_driver = {
2336 .name = "es18xx-pnpbios",
2337 .id_table = snd_audiodrive_pnpbiosids,
2338 .probe = snd_audiodrive_pnp_detect,
Bill Pemberton1bff2922012-12-06 12:35:21 -05002339 .remove = snd_audiodrive_pnp_remove,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002340#ifdef CONFIG_PM
2341 .suspend = snd_audiodrive_pnp_suspend,
2342 .resume = snd_audiodrive_pnp_resume,
2343#endif
2344};
2345
Bill Pemberton1bff2922012-12-06 12:35:21 -05002346static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2347 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002350 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int res;
2352
2353 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002354 if (enable[dev] && isapnp[dev])
2355 break;
2356 }
2357 if (dev >= SNDRV_CARDS)
2358 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Takashi Iwai4323cc42014-01-29 13:03:56 +01002360 res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002361 if (res < 0)
2362 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002363
Ondrej Zary1c3985582006-07-31 12:51:57 +02002364 if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002365 snd_card_free(card);
2366 return res;
2367 }
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002368 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2369 snd_card_free(card);
2370 return res;
2371 }
2372
2373 pnp_set_card_drvdata(pcard, card);
2374 dev++;
2375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Bill Pemberton1bff2922012-12-06 12:35:21 -05002378static void snd_audiodrive_pnpc_remove(struct pnp_card_link *pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002380 snd_card_free(pnp_get_card_drvdata(pcard));
2381 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002384#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002385static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002386{
2387 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2388}
2389
Ondrej Zary1c3985582006-07-31 12:51:57 +02002390static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002391{
2392 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2393}
2394
2395#endif
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397static struct pnp_card_driver es18xx_pnpc_driver = {
2398 .flags = PNP_DRIVER_RES_DISABLE,
2399 .name = "es18xx",
2400 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002401 .probe = snd_audiodrive_pnpc_detect,
Bill Pemberton1bff2922012-12-06 12:35:21 -05002402 .remove = snd_audiodrive_pnpc_remove,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002403#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002404 .suspend = snd_audiodrive_pnpc_suspend,
2405 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407};
2408#endif /* CONFIG_PNP */
2409
2410static int __init alsa_card_es18xx_init(void)
2411{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002412 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002414 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002415#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002416 if (!err)
2417 isa_registered = 1;
2418
Ondrej Zary1c3985582006-07-31 12:51:57 +02002419 err = pnp_register_driver(&es18xx_pnp_driver);
2420 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002421 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002422
Ondrej Zary1c3985582006-07-31 12:51:57 +02002423 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2424 if (!err)
2425 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002426
2427 if (isa_registered || pnp_registered)
2428 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002429#endif
Rene Herman609d7692007-05-15 11:42:56 +02002430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433static void __exit alsa_card_es18xx_exit(void)
2434{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002435#ifdef CONFIG_PNP
2436 if (pnpc_registered)
2437 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2438 if (pnp_registered)
2439 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002440 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002441#endif
Rene Herman609d7692007-05-15 11:42:56 +02002442 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445module_init(alsa_card_es18xx_init)
2446module_exit(alsa_card_es18xx_exit)