]> Git Repo - linux.git/blame - sound/isa/es18xx.c
Linux 6.14-rc3
[linux.git] / sound / isa / es18xx.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Driver for generic ESS AudioDrive ES18xx soundcards
4 * Copyright (c) by Christian Fischbach <[email protected]>
5 * Copyright (c) by Abramo Bagnara <[email protected]>
1da177e4
LT
6 */
7/* GENERAL NOTES:
8 *
9 * BUGS:
10 * - There are pops (we can't delay in trigger function, cause midlevel
11 * often need to trigger down and then up very quickly).
12 * Any ideas?
13 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
14 */
15
16/*
17 * ES1868 NOTES:
18 * - The chip has one half duplex pcm (with very limited full duplex support).
19 *
20 * - Duplex stereophonic sound is impossible.
21 * - Record and playback must share the same frequency rate.
22 *
23 * - The driver use dma2 for playback and dma1 for capture.
24 */
25
26/*
27 * ES1869 NOTES:
28 *
29 * - there are a first full duplex pcm and a second playback only pcm
30 * (incompatible with first pcm capture)
31 *
32 * - there is support for the capture volume and ESS Spatializer 3D effect.
33 *
34 * - contrarily to some pages in DS_1869.PDF the rates can be set
35 * independently.
36 *
95b71296
MS
37 * - Zoom Video is implemented by sharing the FM DAC, thus the user can
38 * have either FM playback or Video playback but not both simultaneously.
39 * The Video Playback Switch mixer control toggles this choice.
40 *
1da177e4
LT
41 * BUGS:
42 *
43 * - There is a major trouble I noted:
44 *
45 * using both channel for playback stereo 16 bit samples at 44100 Hz
46 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
47 *
48 * The same happens using Audio1 for captureing.
49 *
50 * The Windows driver does not suffer of this (although it use Audio1
51 * only for captureing). I'm unable to discover why.
52 *
53 */
54
95b71296
MS
55/*
56 * ES1879 NOTES:
57 * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
58 * seems to be effected (speaker_test plays a lower frequency). Can't find
59 * anything in the datasheet to account for this, so a Video Playback Switch
60 * control has been included to allow ZV to be enabled only when necessary.
61 * Then again on at least one test system the 0x71 bit 6 enable bit is not
62 * needed for ZV, so maybe the datasheet is entirely wrong here.
63 */
64
1da177e4 65#include <linux/init.h>
f7e0ba3e 66#include <linux/err.h>
5e24c1c1 67#include <linux/isa.h>
1da177e4
LT
68#include <linux/pnp.h>
69#include <linux/isapnp.h>
65a77217 70#include <linux/module.h>
1caef6aa 71#include <linux/delay.h>
6cbbfe1c 72#include <linux/io.h>
1caef6aa 73
f7e0ba3e 74#include <asm/dma.h>
1da177e4
LT
75#include <sound/core.h>
76#include <sound/control.h>
77#include <sound/pcm.h>
78#include <sound/pcm_params.h>
79#include <sound/mpu401.h>
80#include <sound/opl3.h>
1da177e4
LT
81#define SNDRV_LEGACY_FIND_FREE_IRQ
82#define SNDRV_LEGACY_FIND_FREE_DMA
83#include <sound/initval.h>
84
8047c910 85struct snd_es18xx {
12174dfe 86 struct snd_card *card;
1da177e4 87 unsigned long port; /* port of ESS chip */
1da177e4 88 unsigned long ctrl_port; /* Control port of ESS chip */
1da177e4
LT
89 int irq; /* IRQ number of ESS chip */
90 int dma1; /* DMA1 */
91 int dma2; /* DMA2 */
92 unsigned short version; /* version of ESS chip */
93 int caps; /* Chip capabilities */
94 unsigned short audio2_vol; /* volume level of audio2 */
95
96 unsigned short active; /* active channel mask */
1da177e4
LT
97 unsigned int dma1_shift;
98 unsigned int dma2_shift;
99
8047c910
TI
100 struct snd_pcm *pcm;
101 struct snd_pcm_substream *playback_a_substream;
102 struct snd_pcm_substream *capture_a_substream;
103 struct snd_pcm_substream *playback_b_substream;
1da177e4 104
8047c910 105 struct snd_rawmidi *rmidi;
1da177e4 106
8047c910
TI
107 struct snd_kcontrol *hw_volume;
108 struct snd_kcontrol *hw_switch;
109 struct snd_kcontrol *master_volume;
110 struct snd_kcontrol *master_switch;
1da177e4
LT
111
112 spinlock_t reg_lock;
113 spinlock_t mixer_lock;
1da177e4
LT
114#ifdef CONFIG_PM
115 unsigned char pm_reg;
116#endif
f7e0ba3e
TI
117#ifdef CONFIG_PNP
118 struct pnp_dev *dev;
119 struct pnp_dev *devc;
120#endif
121};
122
1da177e4
LT
123#define AUDIO1_IRQ 0x01
124#define AUDIO2_IRQ 0x02
125#define HWV_IRQ 0x04
126#define MPU_IRQ 0x08
127
128#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
129#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
130#define ES18XX_RECMIX 0x0004 /* Has record mixer */
131#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
132#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
133#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
134#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
561de31a 135#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
1da177e4
LT
136#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
137#define ES18XX_I2S 0x0200 /* I2S mixer control */
138#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
139#define ES18XX_CONTROL 0x0800 /* Has control ports */
2603fe21 140#define ES18XX_GPO_2BIT 0x1000 /* GPO0,1 controlled by PM port */
1da177e4
LT
141
142/* Power Management */
143#define ES18XX_PM 0x07
144#define ES18XX_PM_GPO0 0x01
145#define ES18XX_PM_GPO1 0x02
146#define ES18XX_PM_PDR 0x04
147#define ES18XX_PM_ANA 0x08
148#define ES18XX_PM_FM 0x020
149#define ES18XX_PM_SUS 0x080
150
1da177e4
LT
151/* Lowlevel */
152
153#define DAC1 0x01
154#define ADC1 0x02
155#define DAC2 0x04
156#define MILLISECOND 10000
157
8047c910 158static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
1da177e4
LT
159{
160 int i;
161
162 for(i = MILLISECOND; i; i--)
163 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
164 outb(val, chip->port + 0x0C);
165 return 0;
166 }
12174dfe 167 dev_err(chip->card->dev, "dsp_command: timeout (0x%x)\n", val);
1da177e4
LT
168 return -EINVAL;
169}
170
8047c910 171static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
1da177e4
LT
172{
173 int i;
174
175 for(i = MILLISECOND/10; i; i--)
176 if (inb(chip->port + 0x0C) & 0x40)
177 return inb(chip->port + 0x0A);
12174dfe
TI
178 dev_err(chip->card->dev, "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
179 chip->port + 0x0A, inb(chip->port + 0x0A));
1da177e4
LT
180 return -ENODEV;
181}
182
183#undef REG_DEBUG
184
8047c910 185static int snd_es18xx_write(struct snd_es18xx *chip,
1da177e4
LT
186 unsigned char reg, unsigned char data)
187{
188 unsigned long flags;
189 int ret;
190
191 spin_lock_irqsave(&chip->reg_lock, flags);
192 ret = snd_es18xx_dsp_command(chip, reg);
193 if (ret < 0)
194 goto end;
195 ret = snd_es18xx_dsp_command(chip, data);
196 end:
197 spin_unlock_irqrestore(&chip->reg_lock, flags);
198#ifdef REG_DEBUG
12174dfe 199 dev_dbg(chip->card->dev, "Reg %02x set to %02x\n", reg, data);
1da177e4
LT
200#endif
201 return ret;
202}
203
8047c910 204static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4
LT
205{
206 unsigned long flags;
207 int ret, data;
208 spin_lock_irqsave(&chip->reg_lock, flags);
209 ret = snd_es18xx_dsp_command(chip, 0xC0);
210 if (ret < 0)
211 goto end;
212 ret = snd_es18xx_dsp_command(chip, reg);
213 if (ret < 0)
214 goto end;
215 data = snd_es18xx_dsp_get_byte(chip);
216 ret = data;
217#ifdef REG_DEBUG
12174dfe 218 dev_dbg(chip->card->dev, "Reg %02x now is %02x (%d)\n", reg, data, ret);
1da177e4
LT
219#endif
220 end:
221 spin_unlock_irqrestore(&chip->reg_lock, flags);
222 return ret;
223}
224
225/* Return old value */
8047c910 226static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4
LT
227 unsigned char mask, unsigned char val)
228{
229 int ret;
230 unsigned char old, new, oval;
231 unsigned long flags;
232 spin_lock_irqsave(&chip->reg_lock, flags);
233 ret = snd_es18xx_dsp_command(chip, 0xC0);
234 if (ret < 0)
235 goto end;
236 ret = snd_es18xx_dsp_command(chip, reg);
237 if (ret < 0)
238 goto end;
239 ret = snd_es18xx_dsp_get_byte(chip);
240 if (ret < 0) {
241 goto end;
242 }
243 old = ret;
244 oval = old & mask;
245 if (val != oval) {
246 ret = snd_es18xx_dsp_command(chip, reg);
247 if (ret < 0)
248 goto end;
249 new = (old & ~mask) | (val & mask);
250 ret = snd_es18xx_dsp_command(chip, new);
251 if (ret < 0)
252 goto end;
253#ifdef REG_DEBUG
12174dfe
TI
254 dev_dbg(chip->card->dev, "Reg %02x was %02x, set to %02x (%d)\n",
255 reg, old, new, ret);
1da177e4
LT
256#endif
257 }
258 ret = oval;
259 end:
260 spin_unlock_irqrestore(&chip->reg_lock, flags);
261 return ret;
262}
263
8047c910 264static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
1da177e4
LT
265 unsigned char reg, unsigned char data)
266{
267 unsigned long flags;
268 spin_lock_irqsave(&chip->mixer_lock, flags);
269 outb(reg, chip->port + 0x04);
270 outb(data, chip->port + 0x05);
271 spin_unlock_irqrestore(&chip->mixer_lock, flags);
272#ifdef REG_DEBUG
12174dfe 273 dev_dbg(chip->card->dev, "Mixer reg %02x set to %02x\n", reg, data);
1da177e4
LT
274#endif
275}
276
8047c910 277static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4
LT
278{
279 unsigned long flags;
280 int data;
281 spin_lock_irqsave(&chip->mixer_lock, flags);
282 outb(reg, chip->port + 0x04);
283 data = inb(chip->port + 0x05);
284 spin_unlock_irqrestore(&chip->mixer_lock, flags);
285#ifdef REG_DEBUG
12174dfe 286 dev_dbg(chip->card->dev, "Mixer reg %02x now is %02x\n", reg, data);
1da177e4
LT
287#endif
288 return data;
289}
290
291/* Return old value */
8047c910 292static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4
LT
293 unsigned char mask, unsigned char val)
294{
295 unsigned char old, new, oval;
296 unsigned long flags;
297 spin_lock_irqsave(&chip->mixer_lock, flags);
298 outb(reg, chip->port + 0x04);
299 old = inb(chip->port + 0x05);
300 oval = old & mask;
301 if (val != oval) {
302 new = (old & ~mask) | (val & mask);
303 outb(new, chip->port + 0x05);
304#ifdef REG_DEBUG
12174dfe
TI
305 dev_dbg(chip->card->dev, "Mixer reg %02x was %02x, set to %02x\n",
306 reg, old, new);
1da177e4
LT
307#endif
308 }
309 spin_unlock_irqrestore(&chip->mixer_lock, flags);
310 return oval;
311}
312
8047c910 313static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
1da177e4
LT
314 unsigned char mask)
315{
316 int old, expected, new;
317 unsigned long flags;
318 spin_lock_irqsave(&chip->mixer_lock, flags);
319 outb(reg, chip->port + 0x04);
320 old = inb(chip->port + 0x05);
321 expected = old ^ mask;
322 outb(expected, chip->port + 0x05);
323 new = inb(chip->port + 0x05);
324 spin_unlock_irqrestore(&chip->mixer_lock, flags);
325#ifdef REG_DEBUG
12174dfe
TI
326 dev_dbg(chip->card->dev, "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
327 reg, old, expected, new);
1da177e4
LT
328#endif
329 return expected == new;
330}
331
332
1bff292e 333static int snd_es18xx_reset(struct snd_es18xx *chip)
1da177e4
LT
334{
335 int i;
336 outb(0x03, chip->port + 0x06);
337 inb(chip->port + 0x06);
338 outb(0x00, chip->port + 0x06);
339 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
340 if (inb(chip->port + 0x0A) != 0xAA)
341 return -1;
342 return 0;
343}
344
8047c910 345static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
1da177e4
LT
346{
347 outb(0x02, chip->port + 0x06);
348 inb(chip->port + 0x06);
349 outb(0x00, chip->port + 0x06);
350 return 0;
351}
352
21709121 353static const struct snd_ratnum new_clocks[2] = {
1da177e4
LT
354 {
355 .num = 793800,
356 .den_min = 1,
357 .den_max = 128,
358 .den_step = 1,
359 },
360 {
361 .num = 768000,
362 .den_min = 1,
363 .den_max = 128,
364 .den_step = 1,
365 }
366};
367
21709121 368static const struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
1da177e4
LT
369 .nrats = 2,
370 .rats = new_clocks,
371};
372
21709121 373static const struct snd_ratnum old_clocks[2] = {
1da177e4
LT
374 {
375 .num = 795444,
376 .den_min = 1,
377 .den_max = 128,
378 .den_step = 1,
379 },
380 {
381 .num = 397722,
382 .den_min = 1,
383 .den_max = 128,
384 .den_step = 1,
385 }
386};
387
21709121 388static const struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
1da177e4
LT
389 .nrats = 2,
390 .rats = old_clocks,
391};
392
393
8047c910
TI
394static void snd_es18xx_rate_set(struct snd_es18xx *chip,
395 struct snd_pcm_substream *substream,
1da177e4
LT
396 int mode)
397{
398 unsigned int bits, div0;
8047c910 399 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
400 if (chip->caps & ES18XX_NEW_RATE) {
401 if (runtime->rate_num == new_clocks[0].num)
402 bits = 128 - runtime->rate_den;
403 else
404 bits = 256 - runtime->rate_den;
405 } else {
406 if (runtime->rate_num == old_clocks[0].num)
407 bits = 256 - runtime->rate_den;
408 else
409 bits = 128 - runtime->rate_den;
410 }
411
412 /* set filter register */
413 div0 = 256 - 7160000*20/(8*82*runtime->rate);
414
415 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
416 snd_es18xx_mixer_write(chip, 0x70, bits);
417 /*
418 * Comment from kernel oss driver:
419 * FKS: fascinating: 0x72 doesn't seem to work.
420 */
421 snd_es18xx_write(chip, 0xA2, div0);
422 snd_es18xx_mixer_write(chip, 0x72, div0);
423 } else {
424 snd_es18xx_write(chip, 0xA1, bits);
425 snd_es18xx_write(chip, 0xA2, div0);
426 }
427}
428
8047c910
TI
429static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
430 struct snd_pcm_hw_params *hw_params)
1da177e4 431{
8047c910 432 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
9ce57056 433 int shift;
1da177e4
LT
434
435 shift = 0;
436 if (params_channels(hw_params) == 2)
437 shift++;
438 if (snd_pcm_format_width(params_format(hw_params)) == 16)
439 shift++;
440
441 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
442 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
443 (chip->capture_a_substream) &&
444 params_channels(hw_params) != 1) {
445 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
446 return -EBUSY;
447 }
448 chip->dma2_shift = shift;
449 } else {
450 chip->dma1_shift = shift;
451 }
1da177e4
LT
452 return 0;
453}
454
8047c910
TI
455static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
456 struct snd_pcm_substream *substream)
1da177e4 457{
8047c910 458 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
459 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
460 unsigned int count = snd_pcm_lib_period_bytes(substream);
461
1da177e4
LT
462 snd_es18xx_rate_set(chip, substream, DAC2);
463
464 /* Transfer Count Reload */
465 count = 0x10000 - count;
466 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
467 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
468
469 /* Set format */
470 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
471 ((runtime->channels == 1) ? 0x00 : 0x02) |
472 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
473 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
474
475 /* Set DMA controller */
476 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
477
478 return 0;
479}
480
8047c910
TI
481static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
482 struct snd_pcm_substream *substream,
1da177e4
LT
483 int cmd)
484{
485 switch (cmd) {
486 case SNDRV_PCM_TRIGGER_START:
487 case SNDRV_PCM_TRIGGER_RESUME:
488 if (chip->active & DAC2)
489 return 0;
490 chip->active |= DAC2;
491 /* Start DMA */
492 if (chip->dma2 >= 4)
493 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
494 else
495 snd_es18xx_mixer_write(chip, 0x78, 0x93);
496#ifdef AVOID_POPS
497 /* Avoid pops */
5e0c1879 498 mdelay(100);
1da177e4
LT
499 if (chip->caps & ES18XX_PCM2)
500 /* Restore Audio 2 volume */
501 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
502 else
503 /* Enable PCM output */
504 snd_es18xx_dsp_command(chip, 0xD1);
505#endif
506 break;
507 case SNDRV_PCM_TRIGGER_STOP:
508 case SNDRV_PCM_TRIGGER_SUSPEND:
509 if (!(chip->active & DAC2))
510 return 0;
511 chip->active &= ~DAC2;
512 /* Stop DMA */
513 snd_es18xx_mixer_write(chip, 0x78, 0x00);
514#ifdef AVOID_POPS
5e0c1879 515 mdelay(25);
1da177e4
LT
516 if (chip->caps & ES18XX_PCM2)
517 /* Set Audio 2 volume to 0 */
518 snd_es18xx_mixer_write(chip, 0x7C, 0);
519 else
520 /* Disable PCM output */
521 snd_es18xx_dsp_command(chip, 0xD3);
522#endif
523 break;
524 default:
525 return -EINVAL;
526 }
527
528 return 0;
529}
530
8047c910
TI
531static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
532 struct snd_pcm_hw_params *hw_params)
1da177e4 533{
8047c910 534 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
9ce57056 535 int shift;
1da177e4
LT
536
537 shift = 0;
538 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
539 chip->playback_a_substream &&
540 params_channels(hw_params) != 1) {
541 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
542 return -EBUSY;
543 }
544 if (params_channels(hw_params) == 2)
545 shift++;
546 if (snd_pcm_format_width(params_format(hw_params)) == 16)
547 shift++;
548 chip->dma1_shift = shift;
1da177e4
LT
549 return 0;
550}
551
8047c910 552static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
1da177e4 553{
8047c910
TI
554 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
555 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
556 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
557 unsigned int count = snd_pcm_lib_period_bytes(substream);
558
1da177e4
LT
559 snd_es18xx_reset_fifo(chip);
560
561 /* Set stereo/mono */
562 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
563
564 snd_es18xx_rate_set(chip, substream, ADC1);
565
566 /* Transfer Count Reload */
567 count = 0x10000 - count;
568 snd_es18xx_write(chip, 0xA4, count & 0xff);
569 snd_es18xx_write(chip, 0xA5, count >> 8);
570
571#ifdef AVOID_POPS
5e0c1879 572 mdelay(100);
1da177e4
LT
573#endif
574
575 /* Set format */
576 snd_es18xx_write(chip, 0xB7,
577 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
578 snd_es18xx_write(chip, 0xB7, 0x90 |
579 ((runtime->channels == 1) ? 0x40 : 0x08) |
580 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
581 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
582
3a4fa0a2 583 /* Set DMA controller */
1da177e4
LT
584 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
585
586 return 0;
587}
588
8047c910 589static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
590 int cmd)
591{
8047c910 592 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
593
594 switch (cmd) {
595 case SNDRV_PCM_TRIGGER_START:
596 case SNDRV_PCM_TRIGGER_RESUME:
597 if (chip->active & ADC1)
598 return 0;
599 chip->active |= ADC1;
600 /* Start DMA */
601 snd_es18xx_write(chip, 0xB8, 0x0f);
602 break;
603 case SNDRV_PCM_TRIGGER_STOP:
604 case SNDRV_PCM_TRIGGER_SUSPEND:
605 if (!(chip->active & ADC1))
606 return 0;
607 chip->active &= ~ADC1;
608 /* Stop DMA */
609 snd_es18xx_write(chip, 0xB8, 0x00);
610 break;
611 default:
612 return -EINVAL;
613 }
614
615 return 0;
616}
617
8047c910
TI
618static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
619 struct snd_pcm_substream *substream)
1da177e4 620{
8047c910 621 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
622 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
623 unsigned int count = snd_pcm_lib_period_bytes(substream);
624
1da177e4
LT
625 snd_es18xx_reset_fifo(chip);
626
627 /* Set stereo/mono */
628 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
629
630 snd_es18xx_rate_set(chip, substream, DAC1);
631
632 /* Transfer Count Reload */
633 count = 0x10000 - count;
634 snd_es18xx_write(chip, 0xA4, count & 0xff);
635 snd_es18xx_write(chip, 0xA5, count >> 8);
636
637 /* Set format */
638 snd_es18xx_write(chip, 0xB6,
639 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
640 snd_es18xx_write(chip, 0xB7,
641 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
642 snd_es18xx_write(chip, 0xB7, 0x90 |
643 (runtime->channels == 1 ? 0x40 : 0x08) |
644 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
645 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
646
3a4fa0a2 647 /* Set DMA controller */
1da177e4
LT
648 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
649
650 return 0;
651}
652
8047c910
TI
653static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
654 struct snd_pcm_substream *substream,
1da177e4
LT
655 int cmd)
656{
657 switch (cmd) {
658 case SNDRV_PCM_TRIGGER_START:
659 case SNDRV_PCM_TRIGGER_RESUME:
660 if (chip->active & DAC1)
661 return 0;
662 chip->active |= DAC1;
663 /* Start DMA */
664 snd_es18xx_write(chip, 0xB8, 0x05);
665#ifdef AVOID_POPS
666 /* Avoid pops */
5e0c1879 667 mdelay(100);
1da177e4
LT
668 /* Enable Audio 1 */
669 snd_es18xx_dsp_command(chip, 0xD1);
670#endif
671 break;
672 case SNDRV_PCM_TRIGGER_STOP:
673 case SNDRV_PCM_TRIGGER_SUSPEND:
674 if (!(chip->active & DAC1))
675 return 0;
676 chip->active &= ~DAC1;
677 /* Stop DMA */
678 snd_es18xx_write(chip, 0xB8, 0x00);
679#ifdef AVOID_POPS
680 /* Avoid pops */
5e0c1879 681 mdelay(25);
1da177e4
LT
682 /* Disable Audio 1 */
683 snd_es18xx_dsp_command(chip, 0xD3);
684#endif
685 break;
686 default:
687 return -EINVAL;
688 }
689
690 return 0;
691}
692
8047c910 693static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
1da177e4 694{
8047c910 695 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
696 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
697 return snd_es18xx_playback1_prepare(chip, substream);
698 else
699 return snd_es18xx_playback2_prepare(chip, substream);
700}
701
8047c910 702static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
703 int cmd)
704{
8047c910 705 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
706 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
707 return snd_es18xx_playback1_trigger(chip, substream, cmd);
708 else
709 return snd_es18xx_playback2_trigger(chip, substream, cmd);
710}
711
7d12e780 712static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
1da177e4 713{
3c76b4d6 714 struct snd_card *card = dev_id;
b14f5de7 715 struct snd_es18xx *chip = card->private_data;
1da177e4
LT
716 unsigned char status;
717
718 if (chip->caps & ES18XX_CONTROL) {
719 /* Read Interrupt status */
720 status = inb(chip->ctrl_port + 6);
721 } else {
722 /* Read Interrupt status */
723 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
724 }
725#if 0
726 else {
727 status = 0;
728 if (inb(chip->port + 0x0C) & 0x01)
729 status |= AUDIO1_IRQ;
730 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
731 status |= AUDIO2_IRQ;
732 if ((chip->caps & ES18XX_HWV) &&
733 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
734 status |= HWV_IRQ;
735 }
736#endif
737
738 /* Audio 1 & Audio 2 */
739 if (status & AUDIO2_IRQ) {
740 if (chip->active & DAC2)
741 snd_pcm_period_elapsed(chip->playback_a_substream);
742 /* ack interrupt */
743 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
744 }
745 if (status & AUDIO1_IRQ) {
746 /* ok.. capture is active */
747 if (chip->active & ADC1)
748 snd_pcm_period_elapsed(chip->capture_a_substream);
749 /* ok.. playback2 is active */
750 else if (chip->active & DAC1)
751 snd_pcm_period_elapsed(chip->playback_b_substream);
752 /* ack interrupt */
753 inb(chip->port + 0x0E);
754 }
755
756 /* MPU */
757 if ((status & MPU_IRQ) && chip->rmidi)
7d12e780 758 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1da177e4
LT
759
760 /* Hardware volume */
761 if (status & HWV_IRQ) {
14086771
MS
762 int split = 0;
763 if (chip->caps & ES18XX_HWV) {
764 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
3c76b4d6
KH
765 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
766 &chip->hw_switch->id);
767 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
768 &chip->hw_volume->id);
14086771 769 }
1da177e4 770 if (!split) {
3c76b4d6
KH
771 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
772 &chip->master_switch->id);
773 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
774 &chip->master_volume->id);
1da177e4
LT
775 }
776 /* ack interrupt */
777 snd_es18xx_mixer_write(chip, 0x66, 0x00);
778 }
779 return IRQ_HANDLED;
780}
781
8047c910 782static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 783{
8047c910 784 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
faa1242c 785 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1da177e4
LT
786 int pos;
787
788 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
789 if (!(chip->active & DAC2))
790 return 0;
faa1242c 791 pos = snd_dma_pointer(chip->dma2, size);
1da177e4
LT
792 return pos >> chip->dma2_shift;
793 } else {
794 if (!(chip->active & DAC1))
795 return 0;
faa1242c 796 pos = snd_dma_pointer(chip->dma1, size);
1da177e4
LT
797 return pos >> chip->dma1_shift;
798 }
799}
800
8047c910 801static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
1da177e4 802{
8047c910 803 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
faa1242c 804 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1da177e4
LT
805 int pos;
806
807 if (!(chip->active & ADC1))
808 return 0;
faa1242c 809 pos = snd_dma_pointer(chip->dma1, size);
1da177e4
LT
810 return pos >> chip->dma1_shift;
811}
812
aec54654 813static const struct snd_pcm_hardware snd_es18xx_playback =
1da177e4
LT
814{
815 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
816 SNDRV_PCM_INFO_RESUME |
817 SNDRV_PCM_INFO_MMAP_VALID),
818 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
819 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
820 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
821 .rate_min = 4000,
822 .rate_max = 48000,
823 .channels_min = 1,
824 .channels_max = 2,
825 .buffer_bytes_max = 65536,
826 .period_bytes_min = 64,
827 .period_bytes_max = 65536,
828 .periods_min = 1,
829 .periods_max = 1024,
830 .fifo_size = 0,
831};
832
aec54654 833static const struct snd_pcm_hardware snd_es18xx_capture =
1da177e4
LT
834{
835 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
836 SNDRV_PCM_INFO_RESUME |
837 SNDRV_PCM_INFO_MMAP_VALID),
838 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
839 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
840 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
841 .rate_min = 4000,
842 .rate_max = 48000,
843 .channels_min = 1,
844 .channels_max = 2,
845 .buffer_bytes_max = 65536,
846 .period_bytes_min = 64,
847 .period_bytes_max = 65536,
848 .periods_min = 1,
849 .periods_max = 1024,
850 .fifo_size = 0,
851};
852
8047c910 853static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
1da177e4 854{
8047c910
TI
855 struct snd_pcm_runtime *runtime = substream->runtime;
856 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
857
858 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
859 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
860 chip->capture_a_substream &&
861 chip->capture_a_substream->runtime->channels != 1)
862 return -EAGAIN;
863 chip->playback_a_substream = substream;
864 } else if (substream->number <= 1) {
865 if (chip->capture_a_substream)
866 return -EAGAIN;
867 chip->playback_b_substream = substream;
868 } else {
869 snd_BUG();
870 return -EINVAL;
871 }
872 substream->runtime->hw = snd_es18xx_playback;
873 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
874 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
875 return 0;
876}
877
8047c910 878static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
1da177e4 879{
8047c910
TI
880 struct snd_pcm_runtime *runtime = substream->runtime;
881 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
882
883 if (chip->playback_b_substream)
884 return -EAGAIN;
885 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
886 chip->playback_a_substream &&
887 chip->playback_a_substream->runtime->channels != 1)
888 return -EAGAIN;
889 chip->capture_a_substream = substream;
890 substream->runtime->hw = snd_es18xx_capture;
891 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
892 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
893 return 0;
894}
895
8047c910 896static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
1da177e4 897{
8047c910 898 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
899
900 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
901 chip->playback_a_substream = NULL;
902 else
903 chip->playback_b_substream = NULL;
904
1da177e4
LT
905 return 0;
906}
907
8047c910 908static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
1da177e4 909{
8047c910 910 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4
LT
911
912 chip->capture_a_substream = NULL;
1da177e4
LT
913 return 0;
914}
915
916/*
917 * MIXER part
918 */
919
f4df221f
MS
920/* Record source mux routines:
921 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
922 * bit table for the 4/5 source mux:
923 * reg 1C:
924 * b2 b1 b0 muxSource
925 * x 0 x microphone
926 * 0 1 x CD
927 * 1 1 0 line
928 * 1 1 1 mixer
929 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
930 * either the play mixer or the capture mixer.
931 *
932 * "map4Source" translates from source number to reg bit pattern
933 * "invMap4Source" translates from reg bit pattern to source number
934 */
935
8047c910 936static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4 937{
4e28350a 938 static const char * const texts5Source[5] = {
f4df221f
MS
939 "Mic", "CD", "Line", "Master", "Mix"
940 };
4e28350a 941 static const char * const texts8Source[8] = {
1da177e4
LT
942 "Mic", "Mic Master", "CD", "AOUT",
943 "Mic1", "Mix", "Line", "Master"
944 };
f4df221f 945 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4 946
f4df221f
MS
947 switch (chip->version) {
948 case 0x1868:
949 case 0x1878:
4e28350a 950 return snd_ctl_enum_info(uinfo, 1, 4, texts5Source);
f4df221f
MS
951 case 0x1887:
952 case 0x1888:
4e28350a 953 return snd_ctl_enum_info(uinfo, 1, 5, texts5Source);
c7293858 954 case 0x1869: /* DS somewhat contradictory for 1869: could be 5 or 8 */
f4df221f 955 case 0x1879:
4e28350a 956 return snd_ctl_enum_info(uinfo, 1, 8, texts8Source);
f4df221f
MS
957 default:
958 return -EINVAL;
959 }
1da177e4
LT
960}
961
8047c910 962static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 963{
fce67097 964 static const unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
8047c910 965 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
f4df221f
MS
966 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
967 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
968 muxSource = invMap4Source[muxSource];
969 if (muxSource==3 &&
970 (chip->version == 0x1887 || chip->version == 0x1888) &&
971 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
972 )
973 muxSource = 4;
974 }
975 ucontrol->value.enumerated.item[0] = muxSource;
1da177e4
LT
976 return 0;
977}
978
8047c910 979static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 980{
fce67097 981 static const unsigned char map4Source[4] = {0, 2, 6, 7};
8047c910 982 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4 983 unsigned char val = ucontrol->value.enumerated.item[0];
f4df221f
MS
984 unsigned char retVal = 0;
985
986 switch (chip->version) {
987 /* 5 source chips */
988 case 0x1887:
989 case 0x1888:
990 if (val > 4)
991 return -EINVAL;
992 if (val == 4) {
993 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
994 val = 3;
995 } else
996 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
e2d413f9 997 fallthrough;
f4df221f
MS
998 /* 4 source chips */
999 case 0x1868:
1000 case 0x1878:
1001 if (val > 3)
1002 return -EINVAL;
1003 val = map4Source[val];
1004 break;
1005 /* 8 source chips */
1006 case 0x1869:
1007 case 0x1879:
1008 if (val > 7)
1009 return -EINVAL;
1010 break;
1011 default:
1da177e4 1012 return -EINVAL;
f4df221f
MS
1013 }
1014 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
1da177e4
LT
1015}
1016
a5ce8890 1017#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
1da177e4 1018
8047c910 1019static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1020{
8047c910 1021 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1022 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1023 ucontrol->value.integer.value[0] = !!(val & 8);
1024 return 0;
1025}
1026
8047c910 1027static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1028{
8047c910 1029 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1030 unsigned char oval, nval;
1031 int change;
1032 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1033 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1034 change = nval != oval;
1035 if (change) {
1036 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1037 snd_es18xx_mixer_write(chip, 0x50, nval);
1038 }
1039 return change;
1040}
1041
8047c910 1042static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1043{
1044 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1045 uinfo->count = 2;
1046 uinfo->value.integer.min = 0;
1047 uinfo->value.integer.max = 63;
1048 return 0;
1049}
1050
8047c910 1051static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1052{
8047c910 1053 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1054 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1055 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1056 return 0;
1057}
1058
a5ce8890 1059#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
1da177e4 1060
8047c910 1061static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1062{
8047c910 1063 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1064 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1065 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1066 return 0;
1067}
1068
8047c910 1069static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
1da177e4 1070{
8047c910 1071 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1072 chip->master_volume = NULL;
1073 chip->master_switch = NULL;
1074 chip->hw_volume = NULL;
1075 chip->hw_switch = NULL;
1076}
1077
8047c910 1078static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4
LT
1079 unsigned char mask, unsigned char val)
1080{
1081 if (reg < 0xa0)
1082 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1083 else
1084 return snd_es18xx_bits(chip, reg, mask, val);
1085}
1086
8047c910 1087static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4
LT
1088{
1089 if (reg < 0xa0)
1090 return snd_es18xx_mixer_read(chip, reg);
1091 else
1092 return snd_es18xx_read(chip, reg);
1093}
1094
2603fe21 1095#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \
1da177e4
LT
1096{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1097 .info = snd_es18xx_info_single, \
1098 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
2603fe21
OZ
1099 .private_value = reg | (shift << 8) | (mask << 16) | (flags << 24) }
1100
1101#define ES18XX_FL_INVERT (1 << 0)
1102#define ES18XX_FL_PMPORT (1 << 1)
1da177e4 1103
8047c910 1104static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1105{
1106 int mask = (kcontrol->private_value >> 16) & 0xff;
1107
1108 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1109 uinfo->count = 1;
1110 uinfo->value.integer.min = 0;
1111 uinfo->value.integer.max = mask;
1112 return 0;
1113}
1114
8047c910 1115static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1116{
8047c910 1117 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1118 int reg = kcontrol->private_value & 0xff;
1119 int shift = (kcontrol->private_value >> 8) & 0xff;
1120 int mask = (kcontrol->private_value >> 16) & 0xff;
2603fe21
OZ
1121 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1122 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
1da177e4 1123 int val;
2603fe21
OZ
1124
1125 if (pm_port)
1126 val = inb(chip->port + ES18XX_PM);
1127 else
1128 val = snd_es18xx_reg_read(chip, reg);
1da177e4
LT
1129 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1130 if (invert)
1131 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1132 return 0;
1133}
1134
8047c910 1135static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1136{
8047c910 1137 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1138 int reg = kcontrol->private_value & 0xff;
1139 int shift = (kcontrol->private_value >> 8) & 0xff;
1140 int mask = (kcontrol->private_value >> 16) & 0xff;
2603fe21
OZ
1141 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1142 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
1da177e4
LT
1143 unsigned char val;
1144
1145 val = (ucontrol->value.integer.value[0] & mask);
1146 if (invert)
1147 val = mask - val;
1148 mask <<= shift;
1149 val <<= shift;
2603fe21
OZ
1150 if (pm_port) {
1151 unsigned char cur = inb(chip->port + ES18XX_PM);
1152
1153 if ((cur & mask) == val)
1154 return 0;
1155 outb((cur & ~mask) | val, chip->port + ES18XX_PM);
1156 return 1;
1157 }
1158
1da177e4
LT
1159 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1160}
1161
1162#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1163{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1164 .info = snd_es18xx_info_double, \
1165 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1166 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1167
8047c910 1168static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1169{
1170 int mask = (kcontrol->private_value >> 24) & 0xff;
1171
1172 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1173 uinfo->count = 2;
1174 uinfo->value.integer.min = 0;
1175 uinfo->value.integer.max = mask;
1176 return 0;
1177}
1178
8047c910 1179static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1180{
8047c910 1181 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1182 int left_reg = kcontrol->private_value & 0xff;
1183 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1184 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1185 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1186 int mask = (kcontrol->private_value >> 24) & 0xff;
1187 int invert = (kcontrol->private_value >> 22) & 1;
1188 unsigned char left, right;
1189
1190 left = snd_es18xx_reg_read(chip, left_reg);
1191 if (left_reg != right_reg)
1192 right = snd_es18xx_reg_read(chip, right_reg);
1193 else
1194 right = left;
1195 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1196 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1197 if (invert) {
1198 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1199 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1200 }
1201 return 0;
1202}
1203
8047c910 1204static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4 1205{
8047c910 1206 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1207 int left_reg = kcontrol->private_value & 0xff;
1208 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1209 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1210 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1211 int mask = (kcontrol->private_value >> 24) & 0xff;
1212 int invert = (kcontrol->private_value >> 22) & 1;
1213 int change;
1214 unsigned char val1, val2, mask1, mask2;
1215
1216 val1 = ucontrol->value.integer.value[0] & mask;
1217 val2 = ucontrol->value.integer.value[1] & mask;
1218 if (invert) {
1219 val1 = mask - val1;
1220 val2 = mask - val2;
1221 }
1222 val1 <<= shift_left;
1223 val2 <<= shift_right;
1224 mask1 = mask << shift_left;
1225 mask2 = mask << shift_right;
1226 if (left_reg != right_reg) {
1227 change = 0;
1228 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1229 change = 1;
1230 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1231 change = 1;
1232 } else {
1233 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1234 val1 | val2) != (val1 | val2));
1235 }
1236 return change;
1237}
1238
c1f6d415
MS
1239/* Mixer controls
1240 * These arrays contain setup data for mixer controls.
1241 *
1242 * The controls that are universal to all chipsets are fully initialized
1243 * here.
1244 */
fdd1f6fd 1245static const struct snd_kcontrol_new snd_es18xx_base_controls[] = {
1da177e4
LT
1246ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1247ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1248ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1249ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1250ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1da177e4
LT
1251ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1252ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1da177e4
LT
1253ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1254ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1da177e4
LT
1255{
1256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1257 .name = "Capture Source",
1258 .info = snd_es18xx_info_mux,
1259 .get = snd_es18xx_get_mux,
1260 .put = snd_es18xx_put_mux,
1261}
1262};
1263
fdd1f6fd 1264static const struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
1da177e4
LT
1265ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1266ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1267ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1268ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1da177e4
LT
1269ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1270ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1271};
1272
c1f6d415
MS
1273/*
1274 * The chipset specific mixer controls
1275 */
fdd1f6fd 1276static const struct snd_kcontrol_new snd_es18xx_opt_speaker =
d355c82a 1277 ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
c1f6d415 1278
fdd1f6fd 1279static const struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
2603fe21 1280ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT),
95b71296 1281ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
c1f6d415
MS
1282ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1283ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1284};
1285
fdd1f6fd 1286static const struct snd_kcontrol_new snd_es18xx_opt_1878 =
95b71296
MS
1287 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1288
fdd1f6fd 1289static const struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
95b71296
MS
1290ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1291ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1292ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1293};
1294
fdd1f6fd 1295static const struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
1da177e4
LT
1296ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1297};
1298
fdd1f6fd 1299static const struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
1da177e4
LT
1300ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1301ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1302};
1303
fdd1f6fd 1304static const struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
1da177e4
LT
1305ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1306{
1307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1308 .name = "3D Control - Switch",
1309 .info = snd_es18xx_info_spatializer_enable,
1310 .get = snd_es18xx_get_spatializer_enable,
1311 .put = snd_es18xx_put_spatializer_enable,
1312}
1313};
1314
fdd1f6fd 1315static const struct snd_kcontrol_new snd_es18xx_micpre1_control =
1da177e4
LT
1316ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1317
fdd1f6fd 1318static const struct snd_kcontrol_new snd_es18xx_micpre2_control =
1da177e4
LT
1319ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1320
fdd1f6fd 1321static const struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
1da177e4
LT
1322{
1323 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1324 .name = "Hardware Master Playback Volume",
1325 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1326 .info = snd_es18xx_info_hw_volume,
1327 .get = snd_es18xx_get_hw_volume,
1328},
1329{
1330 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1331 .name = "Hardware Master Playback Switch",
1332 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1333 .info = snd_es18xx_info_hw_switch,
1334 .get = snd_es18xx_get_hw_switch,
1335},
1336ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1337};
1338
fdd1f6fd 1339static const struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = {
2603fe21
OZ
1340ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT),
1341ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT),
1342};
1343
1bff292e 1344static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4 1345{
1da177e4 1346 outb(reg, chip->ctrl_port);
96ecdc71 1347 return inb(chip->ctrl_port + 1);
1da177e4 1348}
1da177e4 1349
1bff292e
BP
1350static void snd_es18xx_config_write(struct snd_es18xx *chip,
1351 unsigned char reg, unsigned char data)
1da177e4
LT
1352{
1353 /* No need for spinlocks, this function is used only in
1354 otherwise protected init code */
1355 outb(reg, chip->ctrl_port);
1356 outb(data, chip->ctrl_port + 1);
1357#ifdef REG_DEBUG
12174dfe 1358 dev_dbg(chip->card->dev, "Config reg %02x set to %02x\n", reg, data);
1da177e4
LT
1359#endif
1360}
1361
1bff292e
BP
1362static int snd_es18xx_initialize(struct snd_es18xx *chip,
1363 unsigned long mpu_port,
1364 unsigned long fm_port)
1da177e4
LT
1365{
1366 int mask = 0;
1367
1368 /* enable extended mode */
1369 snd_es18xx_dsp_command(chip, 0xC6);
1370 /* Reset mixer registers */
1371 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1372
1373 /* Audio 1 DMA demand mode (4 bytes/request) */
1374 snd_es18xx_write(chip, 0xB9, 2);
1375 if (chip->caps & ES18XX_CONTROL) {
1376 /* Hardware volume IRQ */
1377 snd_es18xx_config_write(chip, 0x27, chip->irq);
faa1242c 1378 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1da177e4 1379 /* FM I/O */
faa1242c
KH
1380 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1381 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
1da177e4 1382 }
faa1242c 1383 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1da177e4 1384 /* MPU-401 I/O */
faa1242c
KH
1385 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1386 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
1da177e4
LT
1387 /* MPU-401 IRQ */
1388 snd_es18xx_config_write(chip, 0x28, chip->irq);
1389 }
1390 /* Audio1 IRQ */
1391 snd_es18xx_config_write(chip, 0x70, chip->irq);
1392 /* Audio2 IRQ */
1393 snd_es18xx_config_write(chip, 0x72, chip->irq);
1394 /* Audio1 DMA */
1395 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1396 /* Audio2 DMA */
1397 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1398
1399 /* Enable Audio 1 IRQ */
1400 snd_es18xx_write(chip, 0xB1, 0x50);
1401 /* Enable Audio 2 IRQ */
1402 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1403 /* Enable Audio 1 DMA */
1404 snd_es18xx_write(chip, 0xB2, 0x50);
1405 /* Enable MPU and hardware volume interrupt */
1406 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1bc9eed3
KH
1407 /* Enable ESS wavetable input */
1408 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
1da177e4
LT
1409 }
1410 else {
1411 int irqmask, dma1mask, dma2mask;
1412 switch (chip->irq) {
1413 case 2:
1414 case 9:
1415 irqmask = 0;
1416 break;
1417 case 5:
1418 irqmask = 1;
1419 break;
1420 case 7:
1421 irqmask = 2;
1422 break;
1423 case 10:
1424 irqmask = 3;
1425 break;
1426 default:
12174dfe 1427 dev_err(chip->card->dev, "invalid irq %d\n", chip->irq);
1da177e4
LT
1428 return -ENODEV;
1429 }
1430 switch (chip->dma1) {
1431 case 0:
1432 dma1mask = 1;
1433 break;
1434 case 1:
1435 dma1mask = 2;
1436 break;
1437 case 3:
1438 dma1mask = 3;
1439 break;
1440 default:
12174dfe 1441 dev_err(chip->card->dev, "invalid dma1 %d\n", chip->dma1);
1da177e4
LT
1442 return -ENODEV;
1443 }
1444 switch (chip->dma2) {
1445 case 0:
1446 dma2mask = 0;
1447 break;
1448 case 1:
1449 dma2mask = 1;
1450 break;
1451 case 3:
1452 dma2mask = 2;
1453 break;
1454 case 5:
1455 dma2mask = 3;
1456 break;
1457 default:
12174dfe 1458 dev_err(chip->card->dev, "invalid dma2 %d\n", chip->dma2);
1da177e4
LT
1459 return -ENODEV;
1460 }
1461
1462 /* Enable and set Audio 1 IRQ */
1463 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1464 /* Enable and set Audio 1 DMA */
1465 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1466 /* Set Audio 2 DMA */
1467 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1468 /* Enable Audio 2 IRQ and DMA
1469 Set capture mixer input */
1470 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1471 /* Enable and set hardware volume interrupt */
1472 snd_es18xx_mixer_write(chip, 0x64, 0x06);
faa1242c 1473 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1da177e4
LT
1474 /* MPU401 share irq with audio
1475 Joystick enabled
1476 FM enabled */
faa1242c
KH
1477 snd_es18xx_mixer_write(chip, 0x40,
1478 0x43 | (mpu_port & 0xf0) >> 1);
1da177e4
LT
1479 }
1480 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1481 }
1482 if (chip->caps & ES18XX_NEW_RATE) {
1483 /* Change behaviour of register A1
1484 4x oversampling
1485 2nd channel DAC asynchronous */
1486 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1487 }
1488 if (!(chip->caps & ES18XX_PCM2)) {
1489 /* Enable DMA FIFO */
1490 snd_es18xx_write(chip, 0xB7, 0x80);
1491 }
1492 if (chip->caps & ES18XX_SPATIALIZER) {
1493 /* Set spatializer parameters to recommended values */
1494 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1495 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1496 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1497 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1498 }
95b71296
MS
1499 /* Flip the "enable I2S" bits for those chipsets that need it */
1500 switch (chip->version) {
1501 case 0x1879:
1502 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1503 //so a Switch control has been added to toggle this 0x71 bit on/off:
1504 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1505 /* Note: we fall through on purpose here. */
1506 case 0x1878:
1507 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1508 break;
1509 }
1da177e4
LT
1510 /* Mute input source */
1511 if (chip->caps & ES18XX_MUTEREC)
1512 mask = 0x10;
1513 if (chip->caps & ES18XX_RECMIX)
1514 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1515 else {
1516 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1517 snd_es18xx_write(chip, 0xb4, 0x00);
1518 }
1519#ifndef AVOID_POPS
1520 /* Enable PCM output */
1521 snd_es18xx_dsp_command(chip, 0xD1);
1522#endif
1523
1524 return 0;
1525}
1526
34d6599b 1527static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip)
1da177e4
LT
1528{
1529 int hi,lo;
1530
1531 /* reset */
1532 if (snd_es18xx_reset(chip) < 0) {
12174dfe 1533 dev_err(card->dev, "reset at 0x%lx failed!!!\n", chip->port);
1da177e4
LT
1534 return -ENODEV;
1535 }
1536
1537 snd_es18xx_dsp_command(chip, 0xe7);
1538 hi = snd_es18xx_dsp_get_byte(chip);
1539 if (hi < 0) {
1540 return hi;
1541 }
1542 lo = snd_es18xx_dsp_get_byte(chip);
1543 if ((lo & 0xf0) != 0x80) {
1544 return -ENODEV;
1545 }
1546 if (hi == 0x48) {
1547 chip->version = 0x488;
1548 return 0;
1549 }
1550 if (hi != 0x68) {
1551 return -ENODEV;
1552 }
1553 if ((lo & 0x0f) < 8) {
1554 chip->version = 0x688;
1555 return 0;
1556 }
1557
1558 outb(0x40, chip->port + 0x04);
c1f6d415 1559 udelay(10);
1da177e4 1560 hi = inb(chip->port + 0x05);
c1f6d415 1561 udelay(10);
1da177e4
LT
1562 lo = inb(chip->port + 0x05);
1563 if (hi != lo) {
1564 chip->version = hi << 8 | lo;
1565 chip->ctrl_port = inb(chip->port + 0x05) << 8;
c1f6d415 1566 udelay(10);
1da177e4
LT
1567 chip->ctrl_port += inb(chip->port + 0x05);
1568
34d6599b
TI
1569 if (!devm_request_region(card->dev, chip->ctrl_port, 8,
1570 "ES18xx - CTRL")) {
12174dfe 1571 dev_err(card->dev, "unable go grab port 0x%lx\n", chip->ctrl_port);
1da177e4
LT
1572 return -EBUSY;
1573 }
1574
1575 return 0;
1576 }
1577
1578 /* If has Hardware volume */
1579 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1580 /* If has Audio2 */
1581 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1582 /* If has volume count */
1583 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1584 chip->version = 0x1887;
1585 } else {
1586 chip->version = 0x1888;
1587 }
1588 } else {
1589 chip->version = 0x1788;
1590 }
1591 }
1592 else
1593 chip->version = 0x1688;
1594 return 0;
1595}
1596
34d6599b
TI
1597static int snd_es18xx_probe(struct snd_card *card,
1598 struct snd_es18xx *chip,
1bff292e
BP
1599 unsigned long mpu_port,
1600 unsigned long fm_port)
1da177e4 1601{
34d6599b 1602 if (snd_es18xx_identify(card, chip) < 0) {
12174dfe 1603 dev_err(card->dev, "[0x%lx] ESS chip not found\n", chip->port);
1da177e4
LT
1604 return -ENODEV;
1605 }
1606
1607 switch (chip->version) {
1608 case 0x1868:
2603fe21 1609 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
1da177e4
LT
1610 break;
1611 case 0x1869:
2603fe21 1612 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT;
1da177e4
LT
1613 break;
1614 case 0x1878:
14086771 1615 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
1da177e4
LT
1616 break;
1617 case 0x1879:
1618 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1619 break;
1620 case 0x1887:
1da177e4 1621 case 0x1888:
2603fe21 1622 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
1da177e4
LT
1623 break;
1624 default:
12174dfe
TI
1625 dev_err(card->dev, "[0x%lx] unsupported chip ES%x\n",
1626 chip->port, chip->version);
1da177e4
LT
1627 return -ENODEV;
1628 }
1629
12174dfe 1630 dev_dbg(card->dev, "[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1da177e4
LT
1631
1632 if (chip->dma1 == chip->dma2)
1633 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1634
faa1242c 1635 return snd_es18xx_initialize(chip, mpu_port, fm_port);
1da177e4
LT
1636}
1637
8965e53d 1638static const struct snd_pcm_ops snd_es18xx_playback_ops = {
1da177e4
LT
1639 .open = snd_es18xx_playback_open,
1640 .close = snd_es18xx_playback_close,
1da177e4 1641 .hw_params = snd_es18xx_playback_hw_params,
1da177e4
LT
1642 .prepare = snd_es18xx_playback_prepare,
1643 .trigger = snd_es18xx_playback_trigger,
1644 .pointer = snd_es18xx_playback_pointer,
1645};
1646
8965e53d 1647static const struct snd_pcm_ops snd_es18xx_capture_ops = {
1da177e4
LT
1648 .open = snd_es18xx_capture_open,
1649 .close = snd_es18xx_capture_close,
1da177e4 1650 .hw_params = snd_es18xx_capture_hw_params,
1da177e4
LT
1651 .prepare = snd_es18xx_capture_prepare,
1652 .trigger = snd_es18xx_capture_trigger,
1653 .pointer = snd_es18xx_capture_pointer,
1654};
1655
1a2515ac 1656static int snd_es18xx_pcm(struct snd_card *card, int device)
1da177e4 1657{
b14f5de7 1658 struct snd_es18xx *chip = card->private_data;
8047c910 1659 struct snd_pcm *pcm;
1da177e4
LT
1660 char str[16];
1661 int err;
1662
1da177e4 1663 sprintf(str, "ES%x", chip->version);
f7e0ba3e 1664 if (chip->caps & ES18XX_PCM2)
3c76b4d6 1665 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
f7e0ba3e 1666 else
3c76b4d6 1667 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
1da177e4
LT
1668 if (err < 0)
1669 return err;
1670
1671 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1672 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1673
1674 /* global setup */
1675 pcm->private_data = chip;
1da177e4
LT
1676 pcm->info_flags = 0;
1677 if (chip->caps & ES18XX_DUPLEX_SAME)
1678 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1679 if (! (chip->caps & ES18XX_PCM2))
1680 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1681 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1682 chip->pcm = pcm;
1683
9ce57056
TI
1684 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev,
1685 64*1024,
1686 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1da177e4
LT
1687 return 0;
1688}
1689
1690/* Power Management support functions */
1691#ifdef CONFIG_PM
8047c910 1692static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1da177e4 1693{
b14f5de7 1694 struct snd_es18xx *chip = card->private_data;
f7e0ba3e 1695
3c76b4d6 1696 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4 1697
1da177e4
LT
1698 /* power down */
1699 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1700 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1701 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1702 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1703
1704 return 0;
1705}
1706
8047c910 1707static int snd_es18xx_resume(struct snd_card *card)
1da177e4 1708{
b14f5de7 1709 struct snd_es18xx *chip = card->private_data;
1da177e4
LT
1710
1711 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1712 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1713
3c76b4d6 1714 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4
LT
1715 return 0;
1716}
1717#endif /* CONFIG_PM */
1718
1bff292e
BP
1719static int snd_es18xx_new_device(struct snd_card *card,
1720 unsigned long port,
1721 unsigned long mpu_port,
1722 unsigned long fm_port,
1723 int irq, int dma1, int dma2)
1da177e4 1724{
b14f5de7 1725 struct snd_es18xx *chip = card->private_data;
1da177e4 1726
12174dfe 1727 chip->card = card;
1da177e4
LT
1728 spin_lock_init(&chip->reg_lock);
1729 spin_lock_init(&chip->mixer_lock);
1da177e4 1730 chip->port = port;
1da177e4
LT
1731 chip->irq = -1;
1732 chip->dma1 = -1;
1733 chip->dma2 = -1;
1734 chip->audio2_vol = 0x00;
1735 chip->active = 0;
1736
34d6599b 1737 if (!devm_request_region(card->dev, port, 16, "ES18xx")) {
12174dfe 1738 dev_err(card->dev, "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1da177e4
LT
1739 return -EBUSY;
1740 }
1741
34d6599b
TI
1742 if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx",
1743 (void *) card)) {
12174dfe 1744 dev_err(card->dev, "unable to grap IRQ %d\n", irq);
1da177e4
LT
1745 return -EBUSY;
1746 }
1747 chip->irq = irq;
58dad836 1748 card->sync_irq = chip->irq;
1da177e4 1749
34d6599b 1750 if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) {
12174dfe 1751 dev_err(card->dev, "unable to grap DMA1 %d\n", dma1);
1da177e4
LT
1752 return -EBUSY;
1753 }
1754 chip->dma1 = dma1;
1755
34d6599b
TI
1756 if (dma2 != dma1 &&
1757 snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) {
12174dfe 1758 dev_err(card->dev, "unable to grap DMA2 %d\n", dma2);
1da177e4
LT
1759 return -EBUSY;
1760 }
1761 chip->dma2 = dma2;
1762
34d6599b 1763 if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0)
faa1242c 1764 return -ENODEV;
1da177e4
LT
1765 return 0;
1766}
1767
1bff292e 1768static int snd_es18xx_mixer(struct snd_card *card)
1da177e4 1769{
b14f5de7 1770 struct snd_es18xx *chip = card->private_data;
1da177e4
LT
1771 int err;
1772 unsigned int idx;
1773
1da177e4
LT
1774 strcpy(card->mixername, chip->pcm->name);
1775
1776 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
8047c910 1777 struct snd_kcontrol *kctl;
1da177e4
LT
1778 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1779 if (chip->caps & ES18XX_HWV) {
1780 switch (idx) {
1781 case 0:
1782 chip->master_volume = kctl;
1783 kctl->private_free = snd_es18xx_hwv_free;
1784 break;
1785 case 1:
1786 chip->master_switch = kctl;
1787 kctl->private_free = snd_es18xx_hwv_free;
1788 break;
1789 }
1790 }
45e0471d
TI
1791 err = snd_ctl_add(card, kctl);
1792 if (err < 0)
1da177e4
LT
1793 return err;
1794 }
1795 if (chip->caps & ES18XX_PCM2) {
1796 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
45e0471d
TI
1797 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip));
1798 if (err < 0)
1da177e4
LT
1799 return err;
1800 }
1801 } else {
1802 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
45e0471d
TI
1803 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip));
1804 if (err < 0)
1da177e4
LT
1805 return err;
1806 }
1807 }
1808
1da177e4
LT
1809 if (chip->caps & ES18XX_RECMIX) {
1810 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
45e0471d
TI
1811 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip));
1812 if (err < 0)
1da177e4
LT
1813 return err;
1814 }
1815 }
1816 switch (chip->version) {
1817 default:
45e0471d
TI
1818 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip));
1819 if (err < 0)
1da177e4
LT
1820 return err;
1821 break;
1822 case 0x1869:
1823 case 0x1879:
45e0471d
TI
1824 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip));
1825 if (err < 0)
1da177e4
LT
1826 return err;
1827 break;
1828 }
1829 if (chip->caps & ES18XX_SPATIALIZER) {
1830 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
45e0471d
TI
1831 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip));
1832 if (err < 0)
1da177e4
LT
1833 return err;
1834 }
1835 }
1836 if (chip->caps & ES18XX_HWV) {
1837 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
8047c910 1838 struct snd_kcontrol *kctl;
1da177e4
LT
1839 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1840 if (idx == 0)
1841 chip->hw_volume = kctl;
1842 else
1843 chip->hw_switch = kctl;
1844 kctl->private_free = snd_es18xx_hwv_free;
45e0471d
TI
1845 err = snd_ctl_add(card, kctl);
1846 if (err < 0)
1da177e4
LT
1847 return err;
1848
1849 }
1850 }
c1f6d415
MS
1851 /* finish initializing other chipset specific controls
1852 */
1853 if (chip->version != 0x1868) {
1854 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1855 chip));
1856 if (err < 0)
1857 return err;
1858 }
1859 if (chip->version == 0x1869) {
1860 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1861 err = snd_ctl_add(card,
1862 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1863 chip));
1864 if (err < 0)
1865 return err;
1866 }
95b71296
MS
1867 } else if (chip->version == 0x1878) {
1868 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1869 chip));
1870 if (err < 0)
1871 return err;
1872 } else if (chip->version == 0x1879) {
1873 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1874 err = snd_ctl_add(card,
1875 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1876 chip));
1877 if (err < 0)
1878 return err;
1879 }
c1f6d415 1880 }
2603fe21
OZ
1881 if (chip->caps & ES18XX_GPO_2BIT) {
1882 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
1883 err = snd_ctl_add(card,
1884 snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
1885 chip));
1886 if (err < 0)
1887 return err;
1888 }
1889 }
1da177e4
LT
1890 return 0;
1891}
1892
1893
1894/* Card level */
1895
6417f031 1896MODULE_AUTHOR("Christian Fischbach <[email protected]>, Abramo Bagnara <[email protected]>");
1da177e4
LT
1897MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1898MODULE_LICENSE("GPL");
1da177e4
LT
1899
1900static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1901static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
a67ff6a5 1902static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1da177e4 1903#ifdef CONFIG_PNP
a67ff6a5 1904static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
1da177e4
LT
1905#endif
1906static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1907#ifndef CONFIG_PNP
1908static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1909#else
1910static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1911#endif
1912static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1913static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1914static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1915static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1916
1917module_param_array(index, int, NULL, 0444);
1918MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1919module_param_array(id, charp, NULL, 0444);
1920MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1921module_param_array(enable, bool, NULL, 0444);
1922MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1923#ifdef CONFIG_PNP
1924module_param_array(isapnp, bool, NULL, 0444);
1925MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1926#endif
e992ef57 1927module_param_hw_array(port, long, ioport, NULL, 0444);
1da177e4 1928MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
e992ef57 1929module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
1da177e4 1930MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
e992ef57 1931module_param_hw_array(fm_port, long, ioport, NULL, 0444);
1da177e4 1932MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
e992ef57 1933module_param_hw_array(irq, int, irq, NULL, 0444);
1da177e4 1934MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
e992ef57 1935module_param_hw_array(dma1, int, dma, NULL, 0444);
1da177e4 1936MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
e992ef57 1937module_param_hw_array(dma2, int, dma, NULL, 0444);
1da177e4
LT
1938MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1939
1da177e4 1940#ifdef CONFIG_PNP
609d7694
RH
1941static int isa_registered;
1942static int pnp_registered;
1943static int pnpc_registered;
1c398558 1944
05cb183e 1945static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
1c398558 1946 { .id = "ESS1869" },
4848ffe5 1947 { .id = "ESS1879" },
1c398558
OZ
1948 { .id = "" } /* end */
1949};
1950
1951MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
1952
1953/* PnP main device initialization */
1bff292e 1954static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
1c398558 1955{
109c53f8 1956 if (pnp_activate_dev(pdev) < 0) {
12174dfe 1957 dev_err(&pdev->dev, "PnP configure failure (out of resources?)\n");
1c398558
OZ
1958 return -EBUSY;
1959 }
1960 /* ok. hack using Vendor-Defined Card-Level registers */
1961 /* skip csn and logdev initialization - already done in isapnp_configure */
1962 if (pnp_device_is_isapnp(pdev)) {
1963 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1964 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1965 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1966 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1967 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1968 isapnp_cfg_end();
1969 }
1970 port[dev] = pnp_port_start(pdev, 0);
1971 fm_port[dev] = pnp_port_start(pdev, 1);
1972 mpu_port[dev] = pnp_port_start(pdev, 2);
1973 dma1[dev] = pnp_dma(pdev, 0);
1974 dma2[dev] = pnp_dma(pdev, 1);
1975 irq[dev] = pnp_irq(pdev, 0);
12174dfe
TI
1976 dev_dbg(&pdev->dev,
1977 "PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n",
1978 port[dev], fm_port[dev], mpu_port[dev]);
1979 dev_dbg(&pdev->dev,
1980 "PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n",
1981 dma1[dev], dma2[dev], irq[dev]);
1c398558
OZ
1982 return 0;
1983}
1984
1bff292e
BP
1985static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
1986 struct pnp_dev *pdev)
1c398558 1987{
b14f5de7
KH
1988 chip->dev = pdev;
1989 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1c398558 1990 return -EBUSY;
1c398558
OZ
1991 return 0;
1992}
1da177e4 1993
05cb183e 1994static const struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1da177e4
LT
1995 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1996 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1997 /* ESS 1868 (integrated on Maxisound Cards) */
1998 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1999 /* ESS 1868 (integrated on Maxisound Cards) */
2000 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2001 /* ESS ES1869 Plug and Play AudioDrive */
2002 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2003 /* ESS 1869 */
2004 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2005 /* ESS 1878 */
2006 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2007 /* ESS 1879 */
2008 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2009 /* --- */
2010 { .id = "" } /* end */
2011};
2012
2013MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2014
1bff292e
BP
2015static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2016 struct pnp_card_link *card,
2017 const struct pnp_card_device_id *id)
1da177e4 2018{
b14f5de7
KH
2019 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2020 if (chip->dev == NULL)
1da177e4 2021 return -EBUSY;
109c53f8 2022
b14f5de7
KH
2023 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2024 if (chip->devc == NULL)
1da177e4 2025 return -EBUSY;
109c53f8 2026
1da177e4 2027 /* Control port initialization */
b14f5de7 2028 if (pnp_activate_dev(chip->devc) < 0) {
12174dfe
TI
2029 dev_err(chip->card->dev,
2030 "PnP control configure failure (out of resources?)\n");
1da177e4
LT
2031 return -EAGAIN;
2032 }
12174dfe
TI
2033 dev_dbg(chip->card->dev, "pnp: port=0x%llx\n",
2034 (unsigned long long)pnp_port_start(chip->devc, 0));
b14f5de7 2035 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1da177e4 2036 return -EBUSY;
109c53f8 2037
1da177e4
LT
2038 return 0;
2039}
2040#endif /* CONFIG_PNP */
2041
43bcd973
TI
2042#ifdef CONFIG_PNP
2043#define is_isapnp_selected(dev) isapnp[dev]
2044#else
2045#define is_isapnp_selected(dev) 0
2046#endif
2047
4323cc4d
TI
2048static int snd_es18xx_card_new(struct device *pdev, int dev,
2049 struct snd_card **cardp)
1da177e4 2050{
34d6599b
TI
2051 return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2052 sizeof(struct snd_es18xx), cardp);
f7e0ba3e
TI
2053}
2054
1bff292e 2055static int snd_audiodrive_probe(struct snd_card *card, int dev)
f7e0ba3e 2056{
b14f5de7 2057 struct snd_es18xx *chip = card->private_data;
8047c910 2058 struct snd_opl3 *opl3;
1da177e4
LT
2059 int err;
2060
b14f5de7
KH
2061 err = snd_es18xx_new_device(card,
2062 port[dev], mpu_port[dev], fm_port[dev],
2063 irq[dev], dma1[dev], dma2[dev]);
2064 if (err < 0)
f7e0ba3e 2065 return err;
1da177e4
LT
2066
2067 sprintf(card->driver, "ES%x", chip->version);
f7e0ba3e 2068
1da177e4 2069 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
f7e0ba3e 2070 if (dma1[dev] != dma2[dev])
1da177e4
LT
2071 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2072 card->shortname,
2073 chip->port,
f7e0ba3e 2074 irq[dev], dma1[dev], dma2[dev]);
1da177e4
LT
2075 else
2076 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2077 card->shortname,
2078 chip->port,
f7e0ba3e 2079 irq[dev], dma1[dev]);
1da177e4 2080
1a2515ac 2081 err = snd_es18xx_pcm(card, 0);
3c76b4d6 2082 if (err < 0)
f7e0ba3e 2083 return err;
43bcd973 2084
3c76b4d6
KH
2085 err = snd_es18xx_mixer(card);
2086 if (err < 0)
f7e0ba3e 2087 return err;
1da177e4
LT
2088
2089 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
faa1242c
KH
2090 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2091 OPL3_HW_OPL3, 0, &opl3) < 0) {
12174dfe
TI
2092 dev_warn(card->dev,
2093 "opl3 not detected at 0x%lx\n",
2094 fm_port[dev]);
1da177e4 2095 } else {
faa1242c
KH
2096 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2097 if (err < 0)
f7e0ba3e 2098 return err;
1da177e4
LT
2099 }
2100 }
2101
2102 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
faa1242c 2103 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
dba8b469
CL
2104 mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2105 -1, &chip->rmidi);
faa1242c 2106 if (err < 0)
f7e0ba3e 2107 return err;
1da177e4
LT
2108 }
2109
f7e0ba3e
TI
2110 return snd_card_register(card);
2111}
1da177e4 2112
1bff292e 2113static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
5e24c1c1
TI
2114{
2115 return enable[dev] && !is_isapnp_selected(dev);
2116}
2117
1bff292e 2118static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
f7e0ba3e
TI
2119{
2120 struct snd_card *card;
2121 int err;
43bcd973 2122
4323cc4d 2123 err = snd_es18xx_card_new(devptr, dev, &card);
3e7fb9f7
TI
2124 if (err < 0)
2125 return err;
45e0471d 2126 err = snd_audiodrive_probe(card, dev);
34d6599b 2127 if (err < 0)
f7e0ba3e 2128 return err;
5e24c1c1 2129 dev_set_drvdata(devptr, card);
1da177e4
LT
2130 return 0;
2131}
2132
1bff292e 2133static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
1da177e4 2134{
f7e0ba3e 2135 int err;
fce67097
TI
2136 static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2137 static const int possible_dmas[] = {1, 0, 3, 5, -1};
1da177e4 2138
f7e0ba3e 2139 if (irq[dev] == SNDRV_AUTO_IRQ) {
45e0471d
TI
2140 irq[dev] = snd_legacy_find_free_irq(possible_irqs);
2141 if (irq[dev] < 0) {
12174dfe 2142 dev_err(pdev, "unable to find a free IRQ\n");
f7e0ba3e
TI
2143 return -EBUSY;
2144 }
2145 }
2146 if (dma1[dev] == SNDRV_AUTO_DMA) {
45e0471d
TI
2147 dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
2148 if (dma1[dev] < 0) {
12174dfe 2149 dev_err(pdev, "unable to find a free DMA1\n");
f7e0ba3e
TI
2150 return -EBUSY;
2151 }
2152 }
2153 if (dma2[dev] == SNDRV_AUTO_DMA) {
45e0471d
TI
2154 dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
2155 if (dma2[dev] < 0) {
12174dfe 2156 dev_err(pdev, "unable to find a free DMA2\n");
f7e0ba3e
TI
2157 return -EBUSY;
2158 }
2159 }
2160
2161 if (port[dev] != SNDRV_AUTO_PORT) {
5e24c1c1 2162 return snd_es18xx_isa_probe1(dev, pdev);
f7e0ba3e 2163 } else {
fce67097 2164 static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
f7e0ba3e
TI
2165 int i;
2166 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2167 port[dev] = possible_ports[i];
5e24c1c1 2168 err = snd_es18xx_isa_probe1(dev, pdev);
f7e0ba3e
TI
2169 if (! err)
2170 return 0;
2171 }
2172 return err;
1da177e4 2173 }
1da177e4
LT
2174}
2175
f7e0ba3e 2176#ifdef CONFIG_PM
5e24c1c1
TI
2177static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2178 pm_message_t state)
f7e0ba3e 2179{
5e24c1c1 2180 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
f7e0ba3e
TI
2181}
2182
5e24c1c1 2183static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
f7e0ba3e 2184{
5e24c1c1 2185 return snd_es18xx_resume(dev_get_drvdata(dev));
f7e0ba3e
TI
2186}
2187#endif
2188
83c51c0a 2189#define DEV_NAME "es18xx"
f7e0ba3e 2190
5e24c1c1
TI
2191static struct isa_driver snd_es18xx_isa_driver = {
2192 .match = snd_es18xx_isa_match,
2193 .probe = snd_es18xx_isa_probe,
f7e0ba3e 2194#ifdef CONFIG_PM
5e24c1c1
TI
2195 .suspend = snd_es18xx_isa_suspend,
2196 .resume = snd_es18xx_isa_resume,
f7e0ba3e
TI
2197#endif
2198 .driver = {
83c51c0a 2199 .name = DEV_NAME
f7e0ba3e
TI
2200 },
2201};
2202
1da177e4
LT
2203
2204#ifdef CONFIG_PNP
1bff292e
BP
2205static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2206 const struct pnp_device_id *id)
1c398558
OZ
2207{
2208 static int dev;
2209 int err;
2210 struct snd_card *card;
2211
2212 if (pnp_device_is_isapnp(pdev))
2213 return -ENOENT; /* we have another procedure - card */
2214 for (; dev < SNDRV_CARDS; dev++) {
2215 if (enable[dev] && isapnp[dev])
2216 break;
2217 }
2218 if (dev >= SNDRV_CARDS)
2219 return -ENODEV;
2220
4323cc4d 2221 err = snd_es18xx_card_new(&pdev->dev, dev, &card);
3e7fb9f7
TI
2222 if (err < 0)
2223 return err;
45e0471d 2224 err = snd_audiodrive_pnp(dev, card->private_data, pdev);
34d6599b 2225 if (err < 0)
1c398558 2226 return err;
45e0471d 2227 err = snd_audiodrive_probe(card, dev);
34d6599b 2228 if (err < 0)
1c398558 2229 return err;
1c398558
OZ
2230 pnp_set_drvdata(pdev, card);
2231 dev++;
1c398558
OZ
2232 return 0;
2233}
2234
1c398558
OZ
2235#ifdef CONFIG_PM
2236static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2237{
2238 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2239}
2240static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2241{
2242 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2243}
2244#endif
2245
2246static struct pnp_driver es18xx_pnp_driver = {
2247 .name = "es18xx-pnpbios",
2248 .id_table = snd_audiodrive_pnpbiosids,
2249 .probe = snd_audiodrive_pnp_detect,
1c398558
OZ
2250#ifdef CONFIG_PM
2251 .suspend = snd_audiodrive_pnp_suspend,
2252 .resume = snd_audiodrive_pnp_resume,
2253#endif
2254};
2255
1bff292e
BP
2256static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2257 const struct pnp_card_device_id *pid)
1da177e4
LT
2258{
2259 static int dev;
f7e0ba3e 2260 struct snd_card *card;
1da177e4
LT
2261 int res;
2262
2263 for ( ; dev < SNDRV_CARDS; dev++) {
f7e0ba3e
TI
2264 if (enable[dev] && isapnp[dev])
2265 break;
2266 }
2267 if (dev >= SNDRV_CARDS)
2268 return -ENODEV;
1da177e4 2269
4323cc4d 2270 res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
3e7fb9f7
TI
2271 if (res < 0)
2272 return res;
f7e0ba3e 2273
45e0471d 2274 res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid);
34d6599b 2275 if (res < 0)
f7e0ba3e 2276 return res;
45e0471d 2277 res = snd_audiodrive_probe(card, dev);
34d6599b 2278 if (res < 0)
f7e0ba3e 2279 return res;
f7e0ba3e
TI
2280
2281 pnp_set_card_drvdata(pcard, card);
2282 dev++;
2283 return 0;
1da177e4
LT
2284}
2285
f7e0ba3e 2286#ifdef CONFIG_PM
1c398558 2287static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
f7e0ba3e
TI
2288{
2289 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2290}
2291
1c398558 2292static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
f7e0ba3e
TI
2293{
2294 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
1da177e4
LT
2295}
2296
f7e0ba3e
TI
2297#endif
2298
1da177e4
LT
2299static struct pnp_card_driver es18xx_pnpc_driver = {
2300 .flags = PNP_DRIVER_RES_DISABLE,
2301 .name = "es18xx",
2302 .id_table = snd_audiodrive_pnpids,
1c398558 2303 .probe = snd_audiodrive_pnpc_detect,
f7e0ba3e 2304#ifdef CONFIG_PM
1c398558
OZ
2305 .suspend = snd_audiodrive_pnpc_suspend,
2306 .resume = snd_audiodrive_pnpc_resume,
f7e0ba3e 2307#endif
1da177e4
LT
2308};
2309#endif /* CONFIG_PNP */
2310
2311static int __init alsa_card_es18xx_init(void)
2312{
5e24c1c1 2313 int err;
1da177e4 2314
5e24c1c1 2315 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
59b1b34f 2316#ifdef CONFIG_PNP
609d7694
RH
2317 if (!err)
2318 isa_registered = 1;
2319
1c398558
OZ
2320 err = pnp_register_driver(&es18xx_pnp_driver);
2321 if (!err)
f7a9275d 2322 pnp_registered = 1;
609d7694 2323
1c398558
OZ
2324 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2325 if (!err)
2326 pnpc_registered = 1;
609d7694
RH
2327
2328 if (isa_registered || pnp_registered)
2329 err = 0;
1da177e4 2330#endif
609d7694 2331 return err;
1da177e4
LT
2332}
2333
2334static void __exit alsa_card_es18xx_exit(void)
2335{
5e24c1c1
TI
2336#ifdef CONFIG_PNP
2337 if (pnpc_registered)
2338 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2339 if (pnp_registered)
2340 pnp_unregister_driver(&es18xx_pnp_driver);
609d7694 2341 if (isa_registered)
5e24c1c1 2342#endif
609d7694 2343 isa_unregister_driver(&snd_es18xx_isa_driver);
1da177e4
LT
2344}
2345
2346module_init(alsa_card_es18xx_init)
2347module_exit(alsa_card_es18xx_exit)
This page took 1.515266 seconds and 4 git commands to generate.