1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for generic ESS AudioDrive ES18xx soundcards
4  *  Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
5  *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
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  *
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  *
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 
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 
65 #include <linux/init.h>
66 #include <linux/err.h>
67 #include <linux/isa.h>
68 #include <linux/pnp.h>
69 #include <linux/isapnp.h>
70 #include <linux/module.h>
71 #include <linux/delay.h>
72 #include <linux/io.h>
73 
74 #include <asm/dma.h>
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>
81 #define SNDRV_LEGACY_FIND_FREE_IRQ
82 #define SNDRV_LEGACY_FIND_FREE_DMA
83 #include <sound/initval.h>
84 
85 struct snd_es18xx {
86 	struct snd_card *card;
87 	unsigned long port;		/* port of ESS chip */
88 	unsigned long ctrl_port;	/* Control port of ESS chip */
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 */
97 	unsigned int dma1_shift;
98 	unsigned int dma2_shift;
99 
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;
104 
105 	struct snd_rawmidi *rmidi;
106 
107 	struct snd_kcontrol *hw_volume;
108 	struct snd_kcontrol *hw_switch;
109 	struct snd_kcontrol *master_volume;
110 	struct snd_kcontrol *master_switch;
111 
112 	spinlock_t reg_lock;
113 	spinlock_t mixer_lock;
114 #ifdef CONFIG_PM
115 	unsigned char pm_reg;
116 #endif
117 #ifdef CONFIG_PNP
118 	struct pnp_dev *dev;
119 	struct pnp_dev *devc;
120 #endif
121 };
122 
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 */
135 #define ES18XX_HWV	0x0080	/* Has separate hardware volume mixer controls*/
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 */
140 #define ES18XX_GPO_2BIT	0x1000	/* GPO0,1 controlled by PM port */
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 
151 /* Lowlevel */
152 
153 #define DAC1 0x01
154 #define ADC1 0x02
155 #define DAC2 0x04
156 #define MILLISECOND 10000
157 
snd_es18xx_dsp_command(struct snd_es18xx * chip,unsigned char val)158 static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
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                 }
167 	dev_err(chip->card->dev, "dsp_command: timeout (0x%x)\n", val);
168         return -EINVAL;
169 }
170 
snd_es18xx_dsp_get_byte(struct snd_es18xx * chip)171 static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
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);
178 	dev_err(chip->card->dev, "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
179 		chip->port + 0x0A, inb(chip->port + 0x0A));
180         return -ENODEV;
181 }
182 
183 #undef REG_DEBUG
184 
snd_es18xx_write(struct snd_es18xx * chip,unsigned char reg,unsigned char data)185 static int snd_es18xx_write(struct snd_es18xx *chip,
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
199 	dev_dbg(chip->card->dev, "Reg %02x set to %02x\n", reg, data);
200 #endif
201 	return ret;
202 }
203 
snd_es18xx_read(struct snd_es18xx * chip,unsigned char reg)204 static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
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
218 	dev_dbg(chip->card->dev, "Reg %02x now is %02x (%d)\n", reg, data, ret);
219 #endif
220  end:
221         spin_unlock_irqrestore(&chip->reg_lock, flags);
222 	return ret;
223 }
224 
225 /* Return old value */
snd_es18xx_bits(struct snd_es18xx * chip,unsigned char reg,unsigned char mask,unsigned char val)226 static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
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
254 		dev_dbg(chip->card->dev, "Reg %02x was %02x, set to %02x (%d)\n",
255 			reg, old, new, ret);
256 #endif
257 	}
258 	ret = oval;
259  end:
260         spin_unlock_irqrestore(&chip->reg_lock, flags);
261 	return ret;
262 }
263 
snd_es18xx_mixer_write(struct snd_es18xx * chip,unsigned char reg,unsigned char data)264 static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
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
273 	dev_dbg(chip->card->dev, "Mixer reg %02x set to %02x\n", reg, data);
274 #endif
275 }
276 
snd_es18xx_mixer_read(struct snd_es18xx * chip,unsigned char reg)277 static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
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
286 	dev_dbg(chip->card->dev, "Mixer reg %02x now is %02x\n", reg, data);
287 #endif
288         return data;
289 }
290 
291 /* Return old value */
snd_es18xx_mixer_bits(struct snd_es18xx * chip,unsigned char reg,unsigned char mask,unsigned char val)292 static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
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
305 		dev_dbg(chip->card->dev, "Mixer reg %02x was %02x, set to %02x\n",
306 			reg, old, new);
307 #endif
308 	}
309         spin_unlock_irqrestore(&chip->mixer_lock, flags);
310 	return oval;
311 }
312 
snd_es18xx_mixer_writable(struct snd_es18xx * chip,unsigned char reg,unsigned char mask)313 static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
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
326 	dev_dbg(chip->card->dev, "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
327 		reg, old, expected, new);
328 #endif
329 	return expected == new;
330 }
331 
332 
snd_es18xx_reset(struct snd_es18xx * chip)333 static int snd_es18xx_reset(struct snd_es18xx *chip)
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 
snd_es18xx_reset_fifo(struct snd_es18xx * chip)345 static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
346 {
347         outb(0x02, chip->port + 0x06);
348         inb(chip->port + 0x06);
349         outb(0x00, chip->port + 0x06);
350 	return 0;
351 }
352 
353 static const struct snd_ratnum new_clocks[2] = {
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 
368 static const struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
369 	.nrats = 2,
370 	.rats = new_clocks,
371 };
372 
373 static const struct snd_ratnum old_clocks[2] = {
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 
388 static const struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks  = {
389 	.nrats = 2,
390 	.rats = old_clocks,
391 };
392 
393 
snd_es18xx_rate_set(struct snd_es18xx * chip,struct snd_pcm_substream * substream,int mode)394 static void snd_es18xx_rate_set(struct snd_es18xx *chip,
395 				struct snd_pcm_substream *substream,
396 				int mode)
397 {
398 	unsigned int bits, div0;
399 	struct snd_pcm_runtime *runtime = substream->runtime;
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 
snd_es18xx_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)429 static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
430 					 struct snd_pcm_hw_params *hw_params)
431 {
432 	struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
433 	int shift;
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 	}
452 	return 0;
453 }
454 
snd_es18xx_playback1_prepare(struct snd_es18xx * chip,struct snd_pcm_substream * substream)455 static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
456 					struct snd_pcm_substream *substream)
457 {
458 	struct snd_pcm_runtime *runtime = substream->runtime;
459 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
460 	unsigned int count = snd_pcm_lib_period_bytes(substream);
461 
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 
snd_es18xx_playback1_trigger(struct snd_es18xx * chip,struct snd_pcm_substream * substream,int cmd)481 static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
482 					struct snd_pcm_substream *substream,
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 */
498 		mdelay(100);
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
515 		mdelay(25);
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 
snd_es18xx_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)531 static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
532 					struct snd_pcm_hw_params *hw_params)
533 {
534 	struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
535 	int shift;
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;
549 	return 0;
550 }
551 
snd_es18xx_capture_prepare(struct snd_pcm_substream * substream)552 static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
553 {
554         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
555 	struct snd_pcm_runtime *runtime = substream->runtime;
556 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
557 	unsigned int count = snd_pcm_lib_period_bytes(substream);
558 
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
572 	mdelay(100);
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 
583         /* Set DMA controller */
584         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
585 
586 	return 0;
587 }
588 
snd_es18xx_capture_trigger(struct snd_pcm_substream * substream,int cmd)589 static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
590 				      int cmd)
591 {
592         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
snd_es18xx_playback2_prepare(struct snd_es18xx * chip,struct snd_pcm_substream * substream)618 static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
619 					struct snd_pcm_substream *substream)
620 {
621 	struct snd_pcm_runtime *runtime = substream->runtime;
622 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
623 	unsigned int count = snd_pcm_lib_period_bytes(substream);
624 
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 
647         /* Set DMA controller */
648         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
649 
650 	return 0;
651 }
652 
snd_es18xx_playback2_trigger(struct snd_es18xx * chip,struct snd_pcm_substream * substream,int cmd)653 static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
654 					struct snd_pcm_substream *substream,
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 */
667 		mdelay(100);
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 */
681 		mdelay(25);
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 
snd_es18xx_playback_prepare(struct snd_pcm_substream * substream)693 static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
694 {
695         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
snd_es18xx_playback_trigger(struct snd_pcm_substream * substream,int cmd)702 static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
703 				       int cmd)
704 {
705         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
snd_es18xx_interrupt(int irq,void * dev_id)712 static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
713 {
714 	struct snd_card *card = dev_id;
715 	struct snd_es18xx *chip = card->private_data;
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)
758 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
759 
760 	/* Hardware volume */
761 	if (status & HWV_IRQ) {
762 		int split = 0;
763 		if (chip->caps & ES18XX_HWV) {
764 			split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
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);
769 		}
770 		if (!split) {
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);
775 		}
776 		/* ack interrupt */
777 		snd_es18xx_mixer_write(chip, 0x66, 0x00);
778 	}
779 	return IRQ_HANDLED;
780 }
781 
snd_es18xx_playback_pointer(struct snd_pcm_substream * substream)782 static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
783 {
784         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
785 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
786 	int pos;
787 
788 	if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
789 		if (!(chip->active & DAC2))
790 			return 0;
791 		pos = snd_dma_pointer(chip->dma2, size);
792 		return pos >> chip->dma2_shift;
793 	} else {
794 		if (!(chip->active & DAC1))
795 			return 0;
796 		pos = snd_dma_pointer(chip->dma1, size);
797 		return pos >> chip->dma1_shift;
798 	}
799 }
800 
snd_es18xx_capture_pointer(struct snd_pcm_substream * substream)801 static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
802 {
803         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
804 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
805 	int pos;
806 
807         if (!(chip->active & ADC1))
808                 return 0;
809 	pos = snd_dma_pointer(chip->dma1, size);
810 	return pos >> chip->dma1_shift;
811 }
812 
813 static const struct snd_pcm_hardware snd_es18xx_playback =
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 
833 static const struct snd_pcm_hardware snd_es18xx_capture =
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 
snd_es18xx_playback_open(struct snd_pcm_substream * substream)853 static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
854 {
855 	struct snd_pcm_runtime *runtime = substream->runtime;
856         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
snd_es18xx_capture_open(struct snd_pcm_substream * substream)878 static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
879 {
880 	struct snd_pcm_runtime *runtime = substream->runtime;
881         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
snd_es18xx_playback_close(struct snd_pcm_substream * substream)896 static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
897 {
898         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
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 
905 	return 0;
906 }
907 
snd_es18xx_capture_close(struct snd_pcm_substream * substream)908 static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
909 {
910         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
911 
912         chip->capture_a_substream = NULL;
913         return 0;
914 }
915 
916 /*
917  *  MIXER part
918  */
919 
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 
snd_es18xx_info_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)936 static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
937 {
938 	static const char * const texts5Source[5] = {
939 		"Mic", "CD", "Line", "Master", "Mix"
940 	};
941 	static const char * const texts8Source[8] = {
942 		"Mic", "Mic Master", "CD", "AOUT",
943 		"Mic1", "Mix", "Line", "Master"
944 	};
945 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
946 
947 	switch (chip->version) {
948 	case 0x1868:
949 	case 0x1878:
950 		return snd_ctl_enum_info(uinfo, 1, 4, texts5Source);
951 	case 0x1887:
952 	case 0x1888:
953 		return snd_ctl_enum_info(uinfo, 1, 5, texts5Source);
954 	case 0x1869: /* DS somewhat contradictory for 1869: could be 5 or 8 */
955 	case 0x1879:
956 		return snd_ctl_enum_info(uinfo, 1, 8, texts8Source);
957 	default:
958 		return -EINVAL;
959 	}
960 }
961 
snd_es18xx_get_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)962 static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
963 {
964 	static const unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
965 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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;
976 	return 0;
977 }
978 
snd_es18xx_put_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)979 static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
980 {
981 	static const unsigned char map4Source[4] = {0, 2, 6, 7};
982 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
983 	unsigned char val = ucontrol->value.enumerated.item[0];
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;
997 		fallthrough;
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:
1012 		return -EINVAL;
1013 	}
1014 	return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
1015 }
1016 
1017 #define snd_es18xx_info_spatializer_enable	snd_ctl_boolean_mono_info
1018 
snd_es18xx_get_spatializer_enable(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1019 static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1020 {
1021 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1022 	unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1023 	ucontrol->value.integer.value[0] = !!(val & 8);
1024 	return 0;
1025 }
1026 
snd_es18xx_put_spatializer_enable(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1027 static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1028 {
1029 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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 
snd_es18xx_info_hw_volume(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1042 static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
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 
snd_es18xx_get_hw_volume(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1051 static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1052 {
1053 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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 
1059 #define snd_es18xx_info_hw_switch	snd_ctl_boolean_stereo_info
1060 
snd_es18xx_get_hw_switch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1061 static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1062 {
1063 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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 
snd_es18xx_hwv_free(struct snd_kcontrol * kcontrol)1069 static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
1070 {
1071 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1072 	chip->master_volume = NULL;
1073 	chip->master_switch = NULL;
1074 	chip->hw_volume = NULL;
1075 	chip->hw_switch = NULL;
1076 }
1077 
snd_es18xx_reg_bits(struct snd_es18xx * chip,unsigned char reg,unsigned char mask,unsigned char val)1078 static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
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 
snd_es18xx_reg_read(struct snd_es18xx * chip,unsigned char reg)1087 static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
1088 {
1089 	if (reg < 0xa0)
1090 		return snd_es18xx_mixer_read(chip, reg);
1091 	else
1092 		return snd_es18xx_read(chip, reg);
1093 }
1094 
1095 #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \
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, \
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)
1103 
snd_es18xx_info_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1104 static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
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 
snd_es18xx_get_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1115 static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1116 {
1117 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1118 	int reg = kcontrol->private_value & 0xff;
1119 	int shift = (kcontrol->private_value >> 8) & 0xff;
1120 	int mask = (kcontrol->private_value >> 16) & 0xff;
1121 	int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1122 	int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
1123 	int val;
1124 
1125 	if (pm_port)
1126 		val = inb(chip->port + ES18XX_PM);
1127 	else
1128 		val = snd_es18xx_reg_read(chip, reg);
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 
snd_es18xx_put_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1135 static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1136 {
1137 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1138 	int reg = kcontrol->private_value & 0xff;
1139 	int shift = (kcontrol->private_value >> 8) & 0xff;
1140 	int mask = (kcontrol->private_value >> 16) & 0xff;
1141 	int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1142 	int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
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;
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 
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 
snd_es18xx_info_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1168 static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
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 
snd_es18xx_get_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1179 static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1180 {
1181 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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 
snd_es18xx_put_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1204 static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1205 {
1206 	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
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 
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  */
1245 static const struct snd_kcontrol_new snd_es18xx_base_controls[] = {
1246 ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1247 ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1248 ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1249 ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1250 ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1251 ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1252 ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1253 ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1254 ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
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 
1264 static const struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
1265 ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1266 ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1267 ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1268 ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1269 ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1270 ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1271 };
1272 
1273 /*
1274  * The chipset specific mixer controls
1275  */
1276 static const struct snd_kcontrol_new snd_es18xx_opt_speaker =
1277 	ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
1278 
1279 static const struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1280 ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT),
1281 ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
1282 ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1283 ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1284 };
1285 
1286 static const struct snd_kcontrol_new snd_es18xx_opt_1878 =
1287 	ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1288 
1289 static const struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1290 ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1291 ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1292 ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1293 };
1294 
1295 static const struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
1296 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1297 };
1298 
1299 static const struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
1300 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1301 ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1302 };
1303 
1304 static const struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
1305 ES18XX_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 
1315 static const struct snd_kcontrol_new snd_es18xx_micpre1_control =
1316 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1317 
1318 static const struct snd_kcontrol_new snd_es18xx_micpre2_control =
1319 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1320 
1321 static const struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
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 },
1336 ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1337 };
1338 
1339 static const struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = {
1340 ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT),
1341 ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT),
1342 };
1343 
snd_es18xx_config_read(struct snd_es18xx * chip,unsigned char reg)1344 static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
1345 {
1346 	outb(reg, chip->ctrl_port);
1347 	return inb(chip->ctrl_port + 1);
1348 }
1349 
snd_es18xx_config_write(struct snd_es18xx * chip,unsigned char reg,unsigned char data)1350 static void snd_es18xx_config_write(struct snd_es18xx *chip,
1351 				    unsigned char reg, unsigned char data)
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
1358 	dev_dbg(chip->card->dev, "Config reg %02x set to %02x\n", reg, data);
1359 #endif
1360 }
1361 
snd_es18xx_initialize(struct snd_es18xx * chip,unsigned long mpu_port,unsigned long fm_port)1362 static int snd_es18xx_initialize(struct snd_es18xx *chip,
1363 				 unsigned long mpu_port,
1364 				 unsigned long fm_port)
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);
1378 		if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1379 			/* FM I/O */
1380 			snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1381 			snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
1382 		}
1383 		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1384 			/* MPU-401 I/O */
1385 			snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1386 			snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
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);
1407 		/* Enable ESS wavetable input */
1408 		snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
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:
1427 			dev_err(chip->card->dev, "invalid irq %d\n", chip->irq);
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:
1441 			dev_err(chip->card->dev, "invalid dma1 %d\n", chip->dma1);
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:
1458 			dev_err(chip->card->dev, "invalid dma2 %d\n", chip->dma2);
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);
1473 		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1474 			/* MPU401 share irq with audio
1475 			   Joystick enabled
1476 			   FM enabled */
1477 			snd_es18xx_mixer_write(chip, 0x40,
1478 					       0x43 | (mpu_port & 0xf0) >> 1);
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 	}
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 	}
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 
snd_es18xx_identify(struct snd_card * card,struct snd_es18xx * chip)1527 static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip)
1528 {
1529 	int hi,lo;
1530 
1531 	/* reset */
1532 	if (snd_es18xx_reset(chip) < 0) {
1533 		dev_err(card->dev, "reset at 0x%lx failed!!!\n", chip->port);
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);
1559 	udelay(10);
1560 	hi = inb(chip->port + 0x05);
1561 	udelay(10);
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;
1566 		udelay(10);
1567 		chip->ctrl_port += inb(chip->port + 0x05);
1568 
1569 		if (!devm_request_region(card->dev, chip->ctrl_port, 8,
1570 					 "ES18xx - CTRL")) {
1571 			dev_err(card->dev, "unable go grab port 0x%lx\n", chip->ctrl_port);
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 
snd_es18xx_probe(struct snd_card * card,struct snd_es18xx * chip,unsigned long mpu_port,unsigned long fm_port)1597 static int snd_es18xx_probe(struct snd_card *card,
1598 			    struct snd_es18xx *chip,
1599 			    unsigned long mpu_port,
1600 			    unsigned long fm_port)
1601 {
1602 	if (snd_es18xx_identify(card, chip) < 0) {
1603 		dev_err(card->dev, "[0x%lx] ESS chip not found\n", chip->port);
1604                 return -ENODEV;
1605 	}
1606 
1607 	switch (chip->version) {
1608 	case 0x1868:
1609 		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
1610 		break;
1611 	case 0x1869:
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;
1613 		break;
1614 	case 0x1878:
1615 		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
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:
1621 	case 0x1888:
1622 		chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
1623 		break;
1624 	default:
1625 		dev_err(card->dev, "[0x%lx] unsupported chip ES%x\n",
1626 			chip->port, chip->version);
1627                 return -ENODEV;
1628         }
1629 
1630 	dev_dbg(card->dev, "[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1631 
1632 	if (chip->dma1 == chip->dma2)
1633 		chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1634 
1635 	return snd_es18xx_initialize(chip, mpu_port, fm_port);
1636 }
1637 
1638 static const struct snd_pcm_ops snd_es18xx_playback_ops = {
1639 	.open =		snd_es18xx_playback_open,
1640 	.close =	snd_es18xx_playback_close,
1641 	.hw_params =	snd_es18xx_playback_hw_params,
1642 	.prepare =	snd_es18xx_playback_prepare,
1643 	.trigger =	snd_es18xx_playback_trigger,
1644 	.pointer =	snd_es18xx_playback_pointer,
1645 };
1646 
1647 static const struct snd_pcm_ops snd_es18xx_capture_ops = {
1648 	.open =		snd_es18xx_capture_open,
1649 	.close =	snd_es18xx_capture_close,
1650 	.hw_params =	snd_es18xx_capture_hw_params,
1651 	.prepare =	snd_es18xx_capture_prepare,
1652 	.trigger =	snd_es18xx_capture_trigger,
1653 	.pointer =	snd_es18xx_capture_pointer,
1654 };
1655 
snd_es18xx_pcm(struct snd_card * card,int device)1656 static int snd_es18xx_pcm(struct snd_card *card, int device)
1657 {
1658 	struct snd_es18xx *chip = card->private_data;
1659         struct snd_pcm *pcm;
1660 	char str[16];
1661 	int err;
1662 
1663 	sprintf(str, "ES%x", chip->version);
1664 	if (chip->caps & ES18XX_PCM2)
1665 		err = snd_pcm_new(card, str, device, 2, 1, &pcm);
1666 	else
1667 		err = snd_pcm_new(card, str, device, 1, 1, &pcm);
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;
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 
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);
1687 	return 0;
1688 }
1689 
1690 /* Power Management support functions */
1691 #ifdef CONFIG_PM
snd_es18xx_suspend(struct snd_card * card,pm_message_t state)1692 static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1693 {
1694 	struct snd_es18xx *chip = card->private_data;
1695 
1696 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1697 
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 
snd_es18xx_resume(struct snd_card * card)1707 static int snd_es18xx_resume(struct snd_card *card)
1708 {
1709 	struct snd_es18xx *chip = card->private_data;
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 
1714 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1715 	return 0;
1716 }
1717 #endif /* CONFIG_PM */
1718 
snd_es18xx_new_device(struct snd_card * card,unsigned long port,unsigned long mpu_port,unsigned long fm_port,int irq,int dma1,int dma2)1719 static 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)
1724 {
1725 	struct snd_es18xx *chip = card->private_data;
1726 
1727 	chip->card = card;
1728 	spin_lock_init(&chip->reg_lock);
1729  	spin_lock_init(&chip->mixer_lock);
1730         chip->port = port;
1731         chip->irq = -1;
1732         chip->dma1 = -1;
1733         chip->dma2 = -1;
1734         chip->audio2_vol = 0x00;
1735 	chip->active = 0;
1736 
1737 	if (!devm_request_region(card->dev, port, 16, "ES18xx")) {
1738 		dev_err(card->dev, "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1739 		return -EBUSY;
1740 	}
1741 
1742 	if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx",
1743 			     (void *) card)) {
1744 		dev_err(card->dev, "unable to grap IRQ %d\n", irq);
1745 		return -EBUSY;
1746 	}
1747 	chip->irq = irq;
1748 	card->sync_irq = chip->irq;
1749 
1750 	if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) {
1751 		dev_err(card->dev, "unable to grap DMA1 %d\n", dma1);
1752 		return -EBUSY;
1753 	}
1754 	chip->dma1 = dma1;
1755 
1756 	if (dma2 != dma1 &&
1757 	    snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) {
1758 		dev_err(card->dev, "unable to grap DMA2 %d\n", dma2);
1759 		return -EBUSY;
1760 	}
1761 	chip->dma2 = dma2;
1762 
1763 	if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0)
1764 		return -ENODEV;
1765         return 0;
1766 }
1767 
snd_es18xx_mixer(struct snd_card * card)1768 static int snd_es18xx_mixer(struct snd_card *card)
1769 {
1770 	struct snd_es18xx *chip = card->private_data;
1771 	int err;
1772 	unsigned int idx;
1773 
1774 	strcpy(card->mixername, chip->pcm->name);
1775 
1776 	for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1777 		struct snd_kcontrol *kctl;
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 		}
1791 		err = snd_ctl_add(card, kctl);
1792 		if (err < 0)
1793 			return err;
1794 	}
1795 	if (chip->caps & ES18XX_PCM2) {
1796 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1797 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip));
1798 			if (err < 0)
1799 				return err;
1800 		}
1801 	} else {
1802 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1803 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip));
1804 			if (err < 0)
1805 				return err;
1806 		}
1807 	}
1808 
1809 	if (chip->caps & ES18XX_RECMIX) {
1810 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1811 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip));
1812 			if (err < 0)
1813 				return err;
1814 		}
1815 	}
1816 	switch (chip->version) {
1817 	default:
1818 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip));
1819 		if (err < 0)
1820 			return err;
1821 		break;
1822 	case 0x1869:
1823 	case 0x1879:
1824 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip));
1825 		if (err < 0)
1826 			return err;
1827 		break;
1828 	}
1829 	if (chip->caps & ES18XX_SPATIALIZER) {
1830 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1831 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip));
1832 			if (err < 0)
1833 				return err;
1834 		}
1835 	}
1836 	if (chip->caps & ES18XX_HWV) {
1837 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1838 			struct snd_kcontrol *kctl;
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;
1845 			err = snd_ctl_add(card, kctl);
1846 			if (err < 0)
1847 				return err;
1848 
1849 		}
1850 	}
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 		}
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 		}
1880 	}
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 	}
1890 	return 0;
1891 }
1892 
1893 
1894 /* Card level */
1895 
1896 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1897 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1898 MODULE_LICENSE("GPL");
1899 
1900 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
1901 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
1902 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1903 #ifdef CONFIG_PNP
1904 static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
1905 #endif
1906 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260,0x280 */
1907 #ifndef CONFIG_PNP
1908 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1909 #else
1910 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1911 #endif
1912 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1913 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
1914 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
1915 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
1916 
1917 module_param_array(index, int, NULL, 0444);
1918 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1919 module_param_array(id, charp, NULL, 0444);
1920 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1921 module_param_array(enable, bool, NULL, 0444);
1922 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1923 #ifdef CONFIG_PNP
1924 module_param_array(isapnp, bool, NULL, 0444);
1925 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1926 #endif
1927 module_param_hw_array(port, long, ioport, NULL, 0444);
1928 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1929 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
1930 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1931 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
1932 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1933 module_param_hw_array(irq, int, irq, NULL, 0444);
1934 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1935 module_param_hw_array(dma1, int, dma, NULL, 0444);
1936 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1937 module_param_hw_array(dma2, int, dma, NULL, 0444);
1938 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1939 
1940 #ifdef CONFIG_PNP
1941 static int isa_registered;
1942 static int pnp_registered;
1943 static int pnpc_registered;
1944 
1945 static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
1946 	{ .id = "ESS1869" },
1947 	{ .id = "ESS1879" },
1948 	{ .id = "" }		/* end */
1949 };
1950 
1951 MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
1952 
1953 /* PnP main device initialization */
snd_audiodrive_pnp_init_main(int dev,struct pnp_dev * pdev)1954 static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
1955 {
1956 	if (pnp_activate_dev(pdev) < 0) {
1957 		dev_err(&pdev->dev, "PnP configure failure (out of resources?)\n");
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);
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]);
1982 	return 0;
1983 }
1984 
snd_audiodrive_pnp(int dev,struct snd_es18xx * chip,struct pnp_dev * pdev)1985 static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
1986 			      struct pnp_dev *pdev)
1987 {
1988 	chip->dev = pdev;
1989 	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1990 		return -EBUSY;
1991 	return 0;
1992 }
1993 
1994 static const struct pnp_card_device_id snd_audiodrive_pnpids[] = {
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 
2013 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2014 
snd_audiodrive_pnpc(int dev,struct snd_es18xx * chip,struct pnp_card_link * card,const struct pnp_card_device_id * id)2015 static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2016 			       struct pnp_card_link *card,
2017 			       const struct pnp_card_device_id *id)
2018 {
2019 	chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2020 	if (chip->dev == NULL)
2021 		return -EBUSY;
2022 
2023 	chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2024 	if (chip->devc == NULL)
2025 		return -EBUSY;
2026 
2027 	/* Control port initialization */
2028 	if (pnp_activate_dev(chip->devc) < 0) {
2029 		dev_err(chip->card->dev,
2030 			"PnP control configure failure (out of resources?)\n");
2031 		return -EAGAIN;
2032 	}
2033 	dev_dbg(chip->card->dev, "pnp: port=0x%llx\n",
2034 		(unsigned long long)pnp_port_start(chip->devc, 0));
2035 	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
2036 		return -EBUSY;
2037 
2038 	return 0;
2039 }
2040 #endif /* CONFIG_PNP */
2041 
2042 #ifdef CONFIG_PNP
2043 #define is_isapnp_selected(dev)		isapnp[dev]
2044 #else
2045 #define is_isapnp_selected(dev)		0
2046 #endif
2047 
snd_es18xx_card_new(struct device * pdev,int dev,struct snd_card ** cardp)2048 static int snd_es18xx_card_new(struct device *pdev, int dev,
2049 			       struct snd_card **cardp)
2050 {
2051 	return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2052 				 sizeof(struct snd_es18xx), cardp);
2053 }
2054 
snd_audiodrive_probe(struct snd_card * card,int dev)2055 static int snd_audiodrive_probe(struct snd_card *card, int dev)
2056 {
2057 	struct snd_es18xx *chip = card->private_data;
2058 	struct snd_opl3 *opl3;
2059 	int err;
2060 
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)
2065 		return err;
2066 
2067 	sprintf(card->driver, "ES%x", chip->version);
2068 
2069 	sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2070 	if (dma1[dev] != dma2[dev])
2071 		sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2072 			card->shortname,
2073 			chip->port,
2074 			irq[dev], dma1[dev], dma2[dev]);
2075 	else
2076 		sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2077 			card->shortname,
2078 			chip->port,
2079 			irq[dev], dma1[dev]);
2080 
2081 	err = snd_es18xx_pcm(card, 0);
2082 	if (err < 0)
2083 		return err;
2084 
2085 	err = snd_es18xx_mixer(card);
2086 	if (err < 0)
2087 		return err;
2088 
2089 	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2090 		if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2091 				    OPL3_HW_OPL3, 0, &opl3) < 0) {
2092 			dev_warn(card->dev,
2093 				 "opl3 not detected at 0x%lx\n",
2094 				 fm_port[dev]);
2095 		} else {
2096 			err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2097 			if (err < 0)
2098 				return err;
2099 		}
2100 	}
2101 
2102 	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2103 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2104 					  mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2105 					  -1, &chip->rmidi);
2106 		if (err < 0)
2107 			return err;
2108 	}
2109 
2110 	return snd_card_register(card);
2111 }
2112 
snd_es18xx_isa_match(struct device * pdev,unsigned int dev)2113 static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2114 {
2115 	return enable[dev] && !is_isapnp_selected(dev);
2116 }
2117 
snd_es18xx_isa_probe1(int dev,struct device * devptr)2118 static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
2119 {
2120 	struct snd_card *card;
2121 	int err;
2122 
2123 	err = snd_es18xx_card_new(devptr, dev, &card);
2124 	if (err < 0)
2125 		return err;
2126 	err = snd_audiodrive_probe(card, dev);
2127 	if (err < 0)
2128 		return err;
2129 	dev_set_drvdata(devptr, card);
2130 	return 0;
2131 }
2132 
snd_es18xx_isa_probe(struct device * pdev,unsigned int dev)2133 static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
2134 {
2135 	int err;
2136 	static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2137 	static const int possible_dmas[] = {1, 0, 3, 5, -1};
2138 
2139 	if (irq[dev] == SNDRV_AUTO_IRQ) {
2140 		irq[dev] = snd_legacy_find_free_irq(possible_irqs);
2141 		if (irq[dev] < 0) {
2142 			dev_err(pdev, "unable to find a free IRQ\n");
2143 			return -EBUSY;
2144 		}
2145 	}
2146 	if (dma1[dev] == SNDRV_AUTO_DMA) {
2147 		dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
2148 		if (dma1[dev] < 0) {
2149 			dev_err(pdev, "unable to find a free DMA1\n");
2150 			return -EBUSY;
2151 		}
2152 	}
2153 	if (dma2[dev] == SNDRV_AUTO_DMA) {
2154 		dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
2155 		if (dma2[dev] < 0) {
2156 			dev_err(pdev, "unable to find a free DMA2\n");
2157 			return -EBUSY;
2158 		}
2159 	}
2160 
2161 	if (port[dev] != SNDRV_AUTO_PORT) {
2162 		return snd_es18xx_isa_probe1(dev, pdev);
2163 	} else {
2164 		static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2165 		int i;
2166 		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2167 			port[dev] = possible_ports[i];
2168 			err = snd_es18xx_isa_probe1(dev, pdev);
2169 			if (! err)
2170 				return 0;
2171 		}
2172 		return err;
2173 	}
2174 }
2175 
2176 #ifdef CONFIG_PM
snd_es18xx_isa_suspend(struct device * dev,unsigned int n,pm_message_t state)2177 static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2178 				  pm_message_t state)
2179 {
2180 	return snd_es18xx_suspend(dev_get_drvdata(dev), state);
2181 }
2182 
snd_es18xx_isa_resume(struct device * dev,unsigned int n)2183 static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
2184 {
2185 	return snd_es18xx_resume(dev_get_drvdata(dev));
2186 }
2187 #endif
2188 
2189 #define DEV_NAME "es18xx"
2190 
2191 static struct isa_driver snd_es18xx_isa_driver = {
2192 	.match		= snd_es18xx_isa_match,
2193 	.probe		= snd_es18xx_isa_probe,
2194 #ifdef CONFIG_PM
2195 	.suspend	= snd_es18xx_isa_suspend,
2196 	.resume		= snd_es18xx_isa_resume,
2197 #endif
2198 	.driver		= {
2199 		.name	= DEV_NAME
2200 	},
2201 };
2202 
2203 
2204 #ifdef CONFIG_PNP
snd_audiodrive_pnp_detect(struct pnp_dev * pdev,const struct pnp_device_id * id)2205 static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2206 				     const struct pnp_device_id *id)
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 
2221 	err = snd_es18xx_card_new(&pdev->dev, dev, &card);
2222 	if (err < 0)
2223 		return err;
2224 	err = snd_audiodrive_pnp(dev, card->private_data, pdev);
2225 	if (err < 0)
2226 		return err;
2227 	err = snd_audiodrive_probe(card, dev);
2228 	if (err < 0)
2229 		return err;
2230 	pnp_set_drvdata(pdev, card);
2231 	dev++;
2232 	return 0;
2233 }
2234 
2235 #ifdef CONFIG_PM
snd_audiodrive_pnp_suspend(struct pnp_dev * pdev,pm_message_t state)2236 static 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 }
snd_audiodrive_pnp_resume(struct pnp_dev * pdev)2240 static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2241 {
2242 	return snd_es18xx_resume(pnp_get_drvdata(pdev));
2243 }
2244 #endif
2245 
2246 static struct pnp_driver es18xx_pnp_driver = {
2247 	.name = "es18xx-pnpbios",
2248 	.id_table = snd_audiodrive_pnpbiosids,
2249 	.probe = snd_audiodrive_pnp_detect,
2250 #ifdef CONFIG_PM
2251 	.suspend = snd_audiodrive_pnp_suspend,
2252 	.resume = snd_audiodrive_pnp_resume,
2253 #endif
2254 };
2255 
snd_audiodrive_pnpc_detect(struct pnp_card_link * pcard,const struct pnp_card_device_id * pid)2256 static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2257 				      const struct pnp_card_device_id *pid)
2258 {
2259 	static int dev;
2260 	struct snd_card *card;
2261 	int res;
2262 
2263 	for ( ; dev < SNDRV_CARDS; dev++) {
2264 		if (enable[dev] && isapnp[dev])
2265 			break;
2266 	}
2267 	if (dev >= SNDRV_CARDS)
2268 		return -ENODEV;
2269 
2270 	res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
2271 	if (res < 0)
2272 		return res;
2273 
2274 	res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid);
2275 	if (res < 0)
2276 		return res;
2277 	res = snd_audiodrive_probe(card, dev);
2278 	if (res < 0)
2279 		return res;
2280 
2281 	pnp_set_card_drvdata(pcard, card);
2282 	dev++;
2283 	return 0;
2284 }
2285 
2286 #ifdef CONFIG_PM
snd_audiodrive_pnpc_suspend(struct pnp_card_link * pcard,pm_message_t state)2287 static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
2288 {
2289 	return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2290 }
2291 
snd_audiodrive_pnpc_resume(struct pnp_card_link * pcard)2292 static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
2293 {
2294 	return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2295 }
2296 
2297 #endif
2298 
2299 static struct pnp_card_driver es18xx_pnpc_driver = {
2300 	.flags = PNP_DRIVER_RES_DISABLE,
2301 	.name = "es18xx",
2302 	.id_table = snd_audiodrive_pnpids,
2303 	.probe = snd_audiodrive_pnpc_detect,
2304 #ifdef CONFIG_PM
2305 	.suspend	= snd_audiodrive_pnpc_suspend,
2306 	.resume		= snd_audiodrive_pnpc_resume,
2307 #endif
2308 };
2309 #endif /* CONFIG_PNP */
2310 
alsa_card_es18xx_init(void)2311 static int __init alsa_card_es18xx_init(void)
2312 {
2313 	int err;
2314 
2315 	err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
2316 #ifdef CONFIG_PNP
2317 	if (!err)
2318 		isa_registered = 1;
2319 
2320 	err = pnp_register_driver(&es18xx_pnp_driver);
2321 	if (!err)
2322 		pnp_registered = 1;
2323 
2324 	err = pnp_register_card_driver(&es18xx_pnpc_driver);
2325 	if (!err)
2326 		pnpc_registered = 1;
2327 
2328 	if (isa_registered || pnp_registered)
2329 		err = 0;
2330 #endif
2331 	return err;
2332 }
2333 
alsa_card_es18xx_exit(void)2334 static void __exit alsa_card_es18xx_exit(void)
2335 {
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);
2341 	if (isa_registered)
2342 #endif
2343 		isa_unregister_driver(&snd_es18xx_isa_driver);
2344 }
2345 
2346 module_init(alsa_card_es18xx_init)
2347 module_exit(alsa_card_es18xx_exit)
2348