blob: 3fcd168480b64bb90bf8f50d1ac0c69ae98b8d71 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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 *
Mark Salazar95b71292006-01-16 11:35:40 +010037 * - 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * 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
Mark Salazar95b71292006-01-16 11:35:40 +010055/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010066#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010067#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/pnp.h>
69#include <linux/isapnp.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040070#include <linux/module.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070071#include <linux/delay.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010072#include <linux/io.h>
Andrew Morton1caef6a2006-05-20 15:00:35 -070073
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010074#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define SNDRV_LEGACY_FIND_FREE_IRQ
82#define SNDRV_LEGACY_FIND_FREE_DMA
83#include <sound/initval.h>
84
85#define PFX "es18xx: "
86
Takashi Iwai8047c912005-11-17 14:41:22 +010087struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long port; /* port of ESS chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long ctrl_port; /* Control port of ESS chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int irq; /* IRQ number of ESS chip */
91 int dma1; /* DMA1 */
92 int dma2; /* DMA2 */
93 unsigned short version; /* version of ESS chip */
94 int caps; /* Chip capabilities */
95 unsigned short audio2_vol; /* volume level of audio2 */
96
97 unsigned short active; /* active channel mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned int dma1_shift;
99 unsigned int dma2_shift;
100
Takashi Iwai8047c912005-11-17 14:41:22 +0100101 struct snd_pcm *pcm;
102 struct snd_pcm_substream *playback_a_substream;
103 struct snd_pcm_substream *capture_a_substream;
104 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai8047c912005-11-17 14:41:22 +0100106 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Takashi Iwai8047c912005-11-17 14:41:22 +0100108 struct snd_kcontrol *hw_volume;
109 struct snd_kcontrol *hw_switch;
110 struct snd_kcontrol *master_volume;
111 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 spinlock_t reg_lock;
114 spinlock_t mixer_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#ifdef CONFIG_PM
116 unsigned char pm_reg;
117#endif
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100118#ifdef CONFIG_PNP
119 struct pnp_dev *dev;
120 struct pnp_dev *devc;
121#endif
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define AUDIO1_IRQ 0x01
125#define AUDIO2_IRQ 0x02
126#define HWV_IRQ 0x04
127#define MPU_IRQ 0x08
128
129#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
130#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
131#define ES18XX_RECMIX 0x0004 /* Has record mixer */
132#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
133#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
134#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
135#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
Joe Perches561de312007-12-18 13:13:47 +0100136#define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
138#define ES18XX_I2S 0x0200 /* I2S mixer control */
139#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
140#define ES18XX_CONTROL 0x0800 /* Has control ports */
Ondrej Zary2603fe22014-11-03 21:35:48 +0100141#define ES18XX_GPO_2BIT 0x1000 /* GPO0,1 controlled by PM port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/* Power Management */
144#define ES18XX_PM 0x07
145#define ES18XX_PM_GPO0 0x01
146#define ES18XX_PM_GPO1 0x02
147#define ES18XX_PM_PDR 0x04
148#define ES18XX_PM_ANA 0x08
149#define ES18XX_PM_FM 0x020
150#define ES18XX_PM_SUS 0x080
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Lowlevel */
153
154#define DAC1 0x01
155#define ADC1 0x02
156#define DAC2 0x04
157#define MILLISECOND 10000
158
Takashi Iwai8047c912005-11-17 14:41:22 +0100159static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int i;
162
163 for(i = MILLISECOND; i; i--)
164 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
165 outb(val, chip->port + 0x0C);
166 return 0;
167 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200168 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
170}
171
Takashi Iwai8047c912005-11-17 14:41:22 +0100172static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int i;
175
176 for(i = MILLISECOND/10; i; i--)
177 if (inb(chip->port + 0x0C) & 0x40)
178 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200179 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
180 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -ENODEV;
182}
183
184#undef REG_DEBUG
185
Takashi Iwai8047c912005-11-17 14:41:22 +0100186static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned char reg, unsigned char data)
188{
189 unsigned long flags;
190 int ret;
191
192 spin_lock_irqsave(&chip->reg_lock, flags);
193 ret = snd_es18xx_dsp_command(chip, reg);
194 if (ret < 0)
195 goto end;
196 ret = snd_es18xx_dsp_command(chip, data);
197 end:
198 spin_unlock_irqrestore(&chip->reg_lock, flags);
199#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200200 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#endif
202 return ret;
203}
204
Takashi Iwai8047c912005-11-17 14:41:22 +0100205static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 unsigned long flags;
208 int ret, data;
209 spin_lock_irqsave(&chip->reg_lock, flags);
210 ret = snd_es18xx_dsp_command(chip, 0xC0);
211 if (ret < 0)
212 goto end;
213 ret = snd_es18xx_dsp_command(chip, reg);
214 if (ret < 0)
215 goto end;
216 data = snd_es18xx_dsp_get_byte(chip);
217 ret = data;
218#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200219 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif
221 end:
222 spin_unlock_irqrestore(&chip->reg_lock, flags);
223 return ret;
224}
225
226/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100227static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned char mask, unsigned char val)
229{
230 int ret;
231 unsigned char old, new, oval;
232 unsigned long flags;
233 spin_lock_irqsave(&chip->reg_lock, flags);
234 ret = snd_es18xx_dsp_command(chip, 0xC0);
235 if (ret < 0)
236 goto end;
237 ret = snd_es18xx_dsp_command(chip, reg);
238 if (ret < 0)
239 goto end;
240 ret = snd_es18xx_dsp_get_byte(chip);
241 if (ret < 0) {
242 goto end;
243 }
244 old = ret;
245 oval = old & mask;
246 if (val != oval) {
247 ret = snd_es18xx_dsp_command(chip, reg);
248 if (ret < 0)
249 goto end;
250 new = (old & ~mask) | (val & mask);
251 ret = snd_es18xx_dsp_command(chip, new);
252 if (ret < 0)
253 goto end;
254#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200255 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
256 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif
258 }
259 ret = oval;
260 end:
261 spin_unlock_irqrestore(&chip->reg_lock, flags);
262 return ret;
263}
264
Takashi Iwai8047c912005-11-17 14:41:22 +0100265static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned char reg, unsigned char data)
267{
268 unsigned long flags;
269 spin_lock_irqsave(&chip->mixer_lock, flags);
270 outb(reg, chip->port + 0x04);
271 outb(data, chip->port + 0x05);
272 spin_unlock_irqrestore(&chip->mixer_lock, flags);
273#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200274 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276}
277
Takashi Iwai8047c912005-11-17 14:41:22 +0100278static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned long flags;
281 int data;
282 spin_lock_irqsave(&chip->mixer_lock, flags);
283 outb(reg, chip->port + 0x04);
284 data = inb(chip->port + 0x05);
285 spin_unlock_irqrestore(&chip->mixer_lock, flags);
286#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200287 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
289 return data;
290}
291
292/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100293static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned char mask, unsigned char val)
295{
296 unsigned char old, new, oval;
297 unsigned long flags;
298 spin_lock_irqsave(&chip->mixer_lock, flags);
299 outb(reg, chip->port + 0x04);
300 old = inb(chip->port + 0x05);
301 oval = old & mask;
302 if (val != oval) {
303 new = (old & ~mask) | (val & mask);
304 outb(new, chip->port + 0x05);
305#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200306 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
307 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#endif
309 }
310 spin_unlock_irqrestore(&chip->mixer_lock, flags);
311 return oval;
312}
313
Takashi Iwai8047c912005-11-17 14:41:22 +0100314static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned char mask)
316{
317 int old, expected, new;
318 unsigned long flags;
319 spin_lock_irqsave(&chip->mixer_lock, flags);
320 outb(reg, chip->port + 0x04);
321 old = inb(chip->port + 0x05);
322 expected = old ^ mask;
323 outb(expected, chip->port + 0x05);
324 new = inb(chip->port + 0x05);
325 spin_unlock_irqrestore(&chip->mixer_lock, flags);
326#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200327 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
328 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
330 return expected == new;
331}
332
333
Bill Pemberton1bff2922012-12-06 12:35:21 -0500334static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int i;
337 outb(0x03, chip->port + 0x06);
338 inb(chip->port + 0x06);
339 outb(0x00, chip->port + 0x06);
340 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
341 if (inb(chip->port + 0x0A) != 0xAA)
342 return -1;
343 return 0;
344}
345
Takashi Iwai8047c912005-11-17 14:41:22 +0100346static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 outb(0x02, chip->port + 0x06);
349 inb(chip->port + 0x06);
350 outb(0x00, chip->port + 0x06);
351 return 0;
352}
353
Takashi Iwai21709122017-06-07 14:16:10 +0200354static const struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 {
356 .num = 793800,
357 .den_min = 1,
358 .den_max = 128,
359 .den_step = 1,
360 },
361 {
362 .num = 768000,
363 .den_min = 1,
364 .den_max = 128,
365 .den_step = 1,
366 }
367};
368
Takashi Iwai21709122017-06-07 14:16:10 +0200369static const struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .nrats = 2,
371 .rats = new_clocks,
372};
373
Takashi Iwai21709122017-06-07 14:16:10 +0200374static const struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 {
376 .num = 795444,
377 .den_min = 1,
378 .den_max = 128,
379 .den_step = 1,
380 },
381 {
382 .num = 397722,
383 .den_min = 1,
384 .den_max = 128,
385 .den_step = 1,
386 }
387};
388
Takashi Iwai21709122017-06-07 14:16:10 +0200389static const struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .nrats = 2,
391 .rats = old_clocks,
392};
393
394
Takashi Iwai8047c912005-11-17 14:41:22 +0100395static void snd_es18xx_rate_set(struct snd_es18xx *chip,
396 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int mode)
398{
399 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100400 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (chip->caps & ES18XX_NEW_RATE) {
402 if (runtime->rate_num == new_clocks[0].num)
403 bits = 128 - runtime->rate_den;
404 else
405 bits = 256 - runtime->rate_den;
406 } else {
407 if (runtime->rate_num == old_clocks[0].num)
408 bits = 256 - runtime->rate_den;
409 else
410 bits = 128 - runtime->rate_den;
411 }
412
413 /* set filter register */
414 div0 = 256 - 7160000*20/(8*82*runtime->rate);
415
416 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
417 snd_es18xx_mixer_write(chip, 0x70, bits);
418 /*
419 * Comment from kernel oss driver:
420 * FKS: fascinating: 0x72 doesn't seem to work.
421 */
422 snd_es18xx_write(chip, 0xA2, div0);
423 snd_es18xx_mixer_write(chip, 0x72, div0);
424 } else {
425 snd_es18xx_write(chip, 0xA1, bits);
426 snd_es18xx_write(chip, 0xA2, div0);
427 }
428}
429
Takashi Iwai8047c912005-11-17 14:41:22 +0100430static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
431 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Takashi Iwai8047c912005-11-17 14:41:22 +0100433 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Takashi Iwai9ce57052019-12-09 10:48:45 +0100434 int shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 shift = 0;
437 if (params_channels(hw_params) == 2)
438 shift++;
439 if (snd_pcm_format_width(params_format(hw_params)) == 16)
440 shift++;
441
442 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
443 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
444 (chip->capture_a_substream) &&
445 params_channels(hw_params) != 1) {
446 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
447 return -EBUSY;
448 }
449 chip->dma2_shift = shift;
450 } else {
451 chip->dma1_shift = shift;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454}
455
Takashi Iwai8047c912005-11-17 14:41:22 +0100456static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
457 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai8047c912005-11-17 14:41:22 +0100459 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
461 unsigned int count = snd_pcm_lib_period_bytes(substream);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 snd_es18xx_rate_set(chip, substream, DAC2);
464
465 /* Transfer Count Reload */
466 count = 0x10000 - count;
467 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
468 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
469
470 /* Set format */
471 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
472 ((runtime->channels == 1) ? 0x00 : 0x02) |
473 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
474 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
475
476 /* Set DMA controller */
477 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
478
479 return 0;
480}
481
Takashi Iwai8047c912005-11-17 14:41:22 +0100482static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
483 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int cmd)
485{
486 switch (cmd) {
487 case SNDRV_PCM_TRIGGER_START:
488 case SNDRV_PCM_TRIGGER_RESUME:
489 if (chip->active & DAC2)
490 return 0;
491 chip->active |= DAC2;
492 /* Start DMA */
493 if (chip->dma2 >= 4)
494 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
495 else
496 snd_es18xx_mixer_write(chip, 0x78, 0x93);
497#ifdef AVOID_POPS
498 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800499 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (chip->caps & ES18XX_PCM2)
501 /* Restore Audio 2 volume */
502 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
503 else
504 /* Enable PCM output */
505 snd_es18xx_dsp_command(chip, 0xD1);
506#endif
507 break;
508 case SNDRV_PCM_TRIGGER_STOP:
509 case SNDRV_PCM_TRIGGER_SUSPEND:
510 if (!(chip->active & DAC2))
511 return 0;
512 chip->active &= ~DAC2;
513 /* Stop DMA */
514 snd_es18xx_mixer_write(chip, 0x78, 0x00);
515#ifdef AVOID_POPS
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800516 mdelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (chip->caps & ES18XX_PCM2)
518 /* Set Audio 2 volume to 0 */
519 snd_es18xx_mixer_write(chip, 0x7C, 0);
520 else
521 /* Disable PCM output */
522 snd_es18xx_dsp_command(chip, 0xD3);
523#endif
524 break;
525 default:
526 return -EINVAL;
527 }
528
529 return 0;
530}
531
Takashi Iwai8047c912005-11-17 14:41:22 +0100532static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
533 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Takashi Iwai8047c912005-11-17 14:41:22 +0100535 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Takashi Iwai9ce57052019-12-09 10:48:45 +0100536 int shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 shift = 0;
539 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
540 chip->playback_a_substream &&
541 params_channels(hw_params) != 1) {
542 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
543 return -EBUSY;
544 }
545 if (params_channels(hw_params) == 2)
546 shift++;
547 if (snd_pcm_format_width(params_format(hw_params)) == 16)
548 shift++;
549 chip->dma1_shift = shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
Takashi Iwai8047c912005-11-17 14:41:22 +0100553static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwai8047c912005-11-17 14:41:22 +0100555 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
556 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
558 unsigned int count = snd_pcm_lib_period_bytes(substream);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 snd_es18xx_reset_fifo(chip);
561
562 /* Set stereo/mono */
563 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
564
565 snd_es18xx_rate_set(chip, substream, ADC1);
566
567 /* Transfer Count Reload */
568 count = 0x10000 - count;
569 snd_es18xx_write(chip, 0xA4, count & 0xff);
570 snd_es18xx_write(chip, 0xA5, count >> 8);
571
572#ifdef AVOID_POPS
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800573 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
575
576 /* Set format */
577 snd_es18xx_write(chip, 0xB7,
578 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
579 snd_es18xx_write(chip, 0xB7, 0x90 |
580 ((runtime->channels == 1) ? 0x40 : 0x08) |
581 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
582 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
583
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200584 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
586
587 return 0;
588}
589
Takashi Iwai8047c912005-11-17 14:41:22 +0100590static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int cmd)
592{
Takashi Iwai8047c912005-11-17 14:41:22 +0100593 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 switch (cmd) {
596 case SNDRV_PCM_TRIGGER_START:
597 case SNDRV_PCM_TRIGGER_RESUME:
598 if (chip->active & ADC1)
599 return 0;
600 chip->active |= ADC1;
601 /* Start DMA */
602 snd_es18xx_write(chip, 0xB8, 0x0f);
603 break;
604 case SNDRV_PCM_TRIGGER_STOP:
605 case SNDRV_PCM_TRIGGER_SUSPEND:
606 if (!(chip->active & ADC1))
607 return 0;
608 chip->active &= ~ADC1;
609 /* Stop DMA */
610 snd_es18xx_write(chip, 0xB8, 0x00);
611 break;
612 default:
613 return -EINVAL;
614 }
615
616 return 0;
617}
618
Takashi Iwai8047c912005-11-17 14:41:22 +0100619static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
620 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Takashi Iwai8047c912005-11-17 14:41:22 +0100622 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
624 unsigned int count = snd_pcm_lib_period_bytes(substream);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 snd_es18xx_reset_fifo(chip);
627
628 /* Set stereo/mono */
629 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
630
631 snd_es18xx_rate_set(chip, substream, DAC1);
632
633 /* Transfer Count Reload */
634 count = 0x10000 - count;
635 snd_es18xx_write(chip, 0xA4, count & 0xff);
636 snd_es18xx_write(chip, 0xA5, count >> 8);
637
638 /* Set format */
639 snd_es18xx_write(chip, 0xB6,
640 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
641 snd_es18xx_write(chip, 0xB7,
642 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
643 snd_es18xx_write(chip, 0xB7, 0x90 |
644 (runtime->channels == 1 ? 0x40 : 0x08) |
645 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
646 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
647
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200648 /* Set DMA controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
650
651 return 0;
652}
653
Takashi Iwai8047c912005-11-17 14:41:22 +0100654static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
655 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int cmd)
657{
658 switch (cmd) {
659 case SNDRV_PCM_TRIGGER_START:
660 case SNDRV_PCM_TRIGGER_RESUME:
661 if (chip->active & DAC1)
662 return 0;
663 chip->active |= DAC1;
664 /* Start DMA */
665 snd_es18xx_write(chip, 0xB8, 0x05);
666#ifdef AVOID_POPS
667 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800668 mdelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* Enable Audio 1 */
670 snd_es18xx_dsp_command(chip, 0xD1);
671#endif
672 break;
673 case SNDRV_PCM_TRIGGER_STOP:
674 case SNDRV_PCM_TRIGGER_SUSPEND:
675 if (!(chip->active & DAC1))
676 return 0;
677 chip->active &= ~DAC1;
678 /* Stop DMA */
679 snd_es18xx_write(chip, 0xB8, 0x00);
680#ifdef AVOID_POPS
681 /* Avoid pops */
Li, Zhen-Hua5e0c1872014-04-14 17:41:32 +0800682 mdelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Disable Audio 1 */
684 snd_es18xx_dsp_command(chip, 0xD3);
685#endif
686 break;
687 default:
688 return -EINVAL;
689 }
690
691 return 0;
692}
693
Takashi Iwai8047c912005-11-17 14:41:22 +0100694static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Takashi Iwai8047c912005-11-17 14:41:22 +0100696 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
698 return snd_es18xx_playback1_prepare(chip, substream);
699 else
700 return snd_es18xx_playback2_prepare(chip, substream);
701}
702
Takashi Iwai8047c912005-11-17 14:41:22 +0100703static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int cmd)
705{
Takashi Iwai8047c912005-11-17 14:41:22 +0100706 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
708 return snd_es18xx_playback1_trigger(chip, substream, cmd);
709 else
710 return snd_es18xx_playback2_trigger(chip, substream, cmd);
711}
712
David Howells7d12e782006-10-05 14:55:46 +0100713static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100715 struct snd_card *card = dev_id;
Krzysztof Heltb14f5de2009-10-25 11:10:01 +0100716 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned char status;
718
719 if (chip->caps & ES18XX_CONTROL) {
720 /* Read Interrupt status */
721 status = inb(chip->ctrl_port + 6);
722 } else {
723 /* Read Interrupt status */
724 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
725 }
726#if 0
727 else {
728 status = 0;
729 if (inb(chip->port + 0x0C) & 0x01)
730 status |= AUDIO1_IRQ;
731 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
732 status |= AUDIO2_IRQ;
733 if ((chip->caps & ES18XX_HWV) &&
734 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
735 status |= HWV_IRQ;
736 }
737#endif
738
739 /* Audio 1 & Audio 2 */
740 if (status & AUDIO2_IRQ) {
741 if (chip->active & DAC2)
742 snd_pcm_period_elapsed(chip->playback_a_substream);
743 /* ack interrupt */
744 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
745 }
746 if (status & AUDIO1_IRQ) {
747 /* ok.. capture is active */
748 if (chip->active & ADC1)
749 snd_pcm_period_elapsed(chip->capture_a_substream);
750 /* ok.. playback2 is active */
751 else if (chip->active & DAC1)
752 snd_pcm_period_elapsed(chip->playback_b_substream);
753 /* ack interrupt */
754 inb(chip->port + 0x0E);
755 }
756
757 /* MPU */
758 if ((status & MPU_IRQ) && chip->rmidi)
David Howells7d12e782006-10-05 14:55:46 +0100759 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* Hardware volume */
762 if (status & HWV_IRQ) {
Mark Salazar14086772006-01-16 11:33:52 +0100763 int split = 0;
764 if (chip->caps & ES18XX_HWV) {
765 split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100766 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
767 &chip->hw_switch->id);
768 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
769 &chip->hw_volume->id);
Mark Salazar14086772006-01-16 11:33:52 +0100770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!split) {
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +0100772 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
773 &chip->master_switch->id);
774 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
775 &chip->master_volume->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 /* ack interrupt */
778 snd_es18xx_mixer_write(chip, 0x66, 0x00);
779 }
780 return IRQ_HANDLED;
781}
782
Takashi Iwai8047c912005-11-17 14:41:22 +0100783static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Takashi Iwai8047c912005-11-17 14:41:22 +0100785 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100786 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int pos;
788
789 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
790 if (!(chip->active & DAC2))
791 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100792 pos = snd_dma_pointer(chip->dma2, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return pos >> chip->dma2_shift;
794 } else {
795 if (!(chip->active & DAC1))
796 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100797 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return pos >> chip->dma1_shift;
799 }
800}
801
Takashi Iwai8047c912005-11-17 14:41:22 +0100802static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Takashi Iwai8047c912005-11-17 14:41:22 +0100804 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100805 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int pos;
807
808 if (!(chip->active & ADC1))
809 return 0;
Krzysztof Heltfaa12422009-11-08 11:58:08 +0100810 pos = snd_dma_pointer(chip->dma1, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return pos >> chip->dma1_shift;
812}
813
Bhumika Goyalaec54652017-08-17 14:45:52 +0530814static const struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
817 SNDRV_PCM_INFO_RESUME |
818 SNDRV_PCM_INFO_MMAP_VALID),
819 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
820 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
821 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
822 .rate_min = 4000,
823 .rate_max = 48000,
824 .channels_min = 1,
825 .channels_max = 2,
826 .buffer_bytes_max = 65536,
827 .period_bytes_min = 64,
828 .period_bytes_max = 65536,
829 .periods_min = 1,
830 .periods_max = 1024,
831 .fifo_size = 0,
832};
833
Bhumika Goyalaec54652017-08-17 14:45:52 +0530834static const struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
837 SNDRV_PCM_INFO_RESUME |
838 SNDRV_PCM_INFO_MMAP_VALID),
839 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
840 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
841 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
842 .rate_min = 4000,
843 .rate_max = 48000,
844 .channels_min = 1,
845 .channels_max = 2,
846 .buffer_bytes_max = 65536,
847 .period_bytes_min = 64,
848 .period_bytes_max = 65536,
849 .periods_min = 1,
850 .periods_max = 1024,
851 .fifo_size = 0,
852};
853
Takashi Iwai8047c912005-11-17 14:41:22 +0100854static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Takashi Iwai8047c912005-11-17 14:41:22 +0100856 struct snd_pcm_runtime *runtime = substream->runtime;
857 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
860 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
861 chip->capture_a_substream &&
862 chip->capture_a_substream->runtime->channels != 1)
863 return -EAGAIN;
864 chip->playback_a_substream = substream;
865 } else if (substream->number <= 1) {
866 if (chip->capture_a_substream)
867 return -EAGAIN;
868 chip->playback_b_substream = substream;
869 } else {
870 snd_BUG();
871 return -EINVAL;
872 }
873 substream->runtime->hw = snd_es18xx_playback;
874 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
875 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
876 return 0;
877}
878
Takashi Iwai8047c912005-11-17 14:41:22 +0100879static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai8047c912005-11-17 14:41:22 +0100881 struct snd_pcm_runtime *runtime = substream->runtime;
882 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (chip->playback_b_substream)
885 return -EAGAIN;
886 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
887 chip->playback_a_substream &&
888 chip->playback_a_substream->runtime->channels != 1)
889 return -EAGAIN;
890 chip->capture_a_substream = substream;
891 substream->runtime->hw = snd_es18xx_capture;
892 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
893 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
894 return 0;
895}
896
Takashi Iwai8047c912005-11-17 14:41:22 +0100897static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai8047c912005-11-17 14:41:22 +0100899 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
902 chip->playback_a_substream = NULL;
903 else
904 chip->playback_b_substream = NULL;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907}
908
Takashi Iwai8047c912005-11-17 14:41:22 +0100909static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwai8047c912005-11-17 14:41:22 +0100911 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 chip->capture_a_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915}
916
917/*
918 * MIXER part
919 */
920
Mark Salazarf4df2212006-01-16 11:31:14 +0100921/* Record source mux routines:
922 * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
923 * bit table for the 4/5 source mux:
924 * reg 1C:
925 * b2 b1 b0 muxSource
926 * x 0 x microphone
927 * 0 1 x CD
928 * 1 1 0 line
929 * 1 1 1 mixer
930 * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
931 * either the play mixer or the capture mixer.
932 *
933 * "map4Source" translates from source number to reg bit pattern
934 * "invMap4Source" translates from reg bit pattern to source number
935 */
936
Takashi Iwai8047c912005-11-17 14:41:22 +0100937static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwai4e283502014-10-20 18:14:24 +0200939 static const char * const texts5Source[5] = {
Mark Salazarf4df2212006-01-16 11:31:14 +0100940 "Mic", "CD", "Line", "Master", "Mix"
941 };
Takashi Iwai4e283502014-10-20 18:14:24 +0200942 static const char * const texts8Source[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 "Mic", "Mic Master", "CD", "AOUT",
944 "Mic1", "Mix", "Line", "Master"
945 };
Mark Salazarf4df2212006-01-16 11:31:14 +0100946 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Mark Salazarf4df2212006-01-16 11:31:14 +0100948 switch (chip->version) {
949 case 0x1868:
950 case 0x1878:
Takashi Iwai4e283502014-10-20 18:14:24 +0200951 return snd_ctl_enum_info(uinfo, 1, 4, texts5Source);
Mark Salazarf4df2212006-01-16 11:31:14 +0100952 case 0x1887:
953 case 0x1888:
Takashi Iwai4e283502014-10-20 18:14:24 +0200954 return snd_ctl_enum_info(uinfo, 1, 5, texts5Source);
Randy Dunlapc7293852020-08-05 19:19:16 -0700955 case 0x1869: /* DS somewhat contradictory for 1869: could be 5 or 8 */
Mark Salazarf4df2212006-01-16 11:31:14 +0100956 case 0x1879:
Takashi Iwai4e283502014-10-20 18:14:24 +0200957 return snd_ctl_enum_info(uinfo, 1, 8, texts8Source);
Mark Salazarf4df2212006-01-16 11:31:14 +0100958 default:
959 return -EINVAL;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Takashi Iwai8047c912005-11-17 14:41:22 +0100963static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwaifce67092020-01-05 15:48:03 +0100965 static const unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
Takashi Iwai8047c912005-11-17 14:41:22 +0100966 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Mark Salazarf4df2212006-01-16 11:31:14 +0100967 int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
968 if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
969 muxSource = invMap4Source[muxSource];
970 if (muxSource==3 &&
971 (chip->version == 0x1887 || chip->version == 0x1888) &&
972 (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
973 )
974 muxSource = 4;
975 }
976 ucontrol->value.enumerated.item[0] = muxSource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978}
979
Takashi Iwai8047c912005-11-17 14:41:22 +0100980static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwaifce67092020-01-05 15:48:03 +0100982 static const unsigned char map4Source[4] = {0, 2, 6, 7};
Takashi Iwai8047c912005-11-17 14:41:22 +0100983 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned char val = ucontrol->value.enumerated.item[0];
Mark Salazarf4df2212006-01-16 11:31:14 +0100985 unsigned char retVal = 0;
986
987 switch (chip->version) {
988 /* 5 source chips */
989 case 0x1887:
990 case 0x1888:
991 if (val > 4)
992 return -EINVAL;
993 if (val == 4) {
994 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
995 val = 3;
996 } else
997 retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
Takashi Iwaie2d413f2020-07-09 13:17:50 +0200998 fallthrough;
Mark Salazarf4df2212006-01-16 11:31:14 +0100999 /* 4 source chips */
1000 case 0x1868:
1001 case 0x1878:
1002 if (val > 3)
1003 return -EINVAL;
1004 val = map4Source[val];
1005 break;
1006 /* 8 source chips */
1007 case 0x1869:
1008 case 0x1879:
1009 if (val > 7)
1010 return -EINVAL;
1011 break;
1012 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EINVAL;
Mark Salazarf4df2212006-01-16 11:31:14 +01001014 }
1015 return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001018#define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Takashi Iwai8047c912005-11-17 14:41:22 +01001020static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Takashi Iwai8047c912005-11-17 14:41:22 +01001022 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1024 ucontrol->value.integer.value[0] = !!(val & 8);
1025 return 0;
1026}
1027
Takashi Iwai8047c912005-11-17 14:41:22 +01001028static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai8047c912005-11-17 14:41:22 +01001030 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 unsigned char oval, nval;
1032 int change;
1033 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1034 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1035 change = nval != oval;
1036 if (change) {
1037 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1038 snd_es18xx_mixer_write(chip, 0x50, nval);
1039 }
1040 return change;
1041}
1042
Takashi Iwai8047c912005-11-17 14:41:22 +01001043static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1046 uinfo->count = 2;
1047 uinfo->value.integer.min = 0;
1048 uinfo->value.integer.max = 63;
1049 return 0;
1050}
1051
Takashi Iwai8047c912005-11-17 14:41:22 +01001052static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Takashi Iwai8047c912005-11-17 14:41:22 +01001054 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1056 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1057 return 0;
1058}
1059
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001060#define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Takashi Iwai8047c912005-11-17 14:41:22 +01001062static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwai8047c912005-11-17 14:41:22 +01001064 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1066 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1067 return 0;
1068}
1069
Takashi Iwai8047c912005-11-17 14:41:22 +01001070static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Takashi Iwai8047c912005-11-17 14:41:22 +01001072 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 chip->master_volume = NULL;
1074 chip->master_switch = NULL;
1075 chip->hw_volume = NULL;
1076 chip->hw_switch = NULL;
1077}
1078
Takashi Iwai8047c912005-11-17 14:41:22 +01001079static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 unsigned char mask, unsigned char val)
1081{
1082 if (reg < 0xa0)
1083 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1084 else
1085 return snd_es18xx_bits(chip, reg, mask, val);
1086}
1087
Takashi Iwai8047c912005-11-17 14:41:22 +01001088static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 if (reg < 0xa0)
1091 return snd_es18xx_mixer_read(chip, reg);
1092 else
1093 return snd_es18xx_read(chip, reg);
1094}
1095
Ondrej Zary2603fe22014-11-03 21:35:48 +01001096#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1098 .info = snd_es18xx_info_single, \
1099 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
Ondrej Zary2603fe22014-11-03 21:35:48 +01001100 .private_value = reg | (shift << 8) | (mask << 16) | (flags << 24) }
1101
1102#define ES18XX_FL_INVERT (1 << 0)
1103#define ES18XX_FL_PMPORT (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Takashi Iwai8047c912005-11-17 14:41:22 +01001105static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 int mask = (kcontrol->private_value >> 16) & 0xff;
1108
1109 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1110 uinfo->count = 1;
1111 uinfo->value.integer.min = 0;
1112 uinfo->value.integer.max = mask;
1113 return 0;
1114}
1115
Takashi Iwai8047c912005-11-17 14:41:22 +01001116static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Takashi Iwai8047c912005-11-17 14:41:22 +01001118 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int reg = kcontrol->private_value & 0xff;
1120 int shift = (kcontrol->private_value >> 8) & 0xff;
1121 int mask = (kcontrol->private_value >> 16) & 0xff;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001122 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1123 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int val;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001125
1126 if (pm_port)
1127 val = inb(chip->port + ES18XX_PM);
1128 else
1129 val = snd_es18xx_reg_read(chip, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1131 if (invert)
1132 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1133 return 0;
1134}
1135
Takashi Iwai8047c912005-11-17 14:41:22 +01001136static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Takashi Iwai8047c912005-11-17 14:41:22 +01001138 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int reg = kcontrol->private_value & 0xff;
1140 int shift = (kcontrol->private_value >> 8) & 0xff;
1141 int mask = (kcontrol->private_value >> 16) & 0xff;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001142 int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
1143 int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 unsigned char val;
1145
1146 val = (ucontrol->value.integer.value[0] & mask);
1147 if (invert)
1148 val = mask - val;
1149 mask <<= shift;
1150 val <<= shift;
Ondrej Zary2603fe22014-11-03 21:35:48 +01001151 if (pm_port) {
1152 unsigned char cur = inb(chip->port + ES18XX_PM);
1153
1154 if ((cur & mask) == val)
1155 return 0;
1156 outb((cur & ~mask) | val, chip->port + ES18XX_PM);
1157 return 1;
1158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1161}
1162
1163#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1164{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1165 .info = snd_es18xx_info_double, \
1166 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1167 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1168
Takashi Iwai8047c912005-11-17 14:41:22 +01001169static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 int mask = (kcontrol->private_value >> 24) & 0xff;
1172
1173 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1174 uinfo->count = 2;
1175 uinfo->value.integer.min = 0;
1176 uinfo->value.integer.max = mask;
1177 return 0;
1178}
1179
Takashi Iwai8047c912005-11-17 14:41:22 +01001180static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai8047c912005-11-17 14:41:22 +01001182 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int left_reg = kcontrol->private_value & 0xff;
1184 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1185 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1186 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1187 int mask = (kcontrol->private_value >> 24) & 0xff;
1188 int invert = (kcontrol->private_value >> 22) & 1;
1189 unsigned char left, right;
1190
1191 left = snd_es18xx_reg_read(chip, left_reg);
1192 if (left_reg != right_reg)
1193 right = snd_es18xx_reg_read(chip, right_reg);
1194 else
1195 right = left;
1196 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1197 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1198 if (invert) {
1199 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1200 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1201 }
1202 return 0;
1203}
1204
Takashi Iwai8047c912005-11-17 14:41:22 +01001205static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwai8047c912005-11-17 14:41:22 +01001207 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int left_reg = kcontrol->private_value & 0xff;
1209 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1210 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1211 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1212 int mask = (kcontrol->private_value >> 24) & 0xff;
1213 int invert = (kcontrol->private_value >> 22) & 1;
1214 int change;
1215 unsigned char val1, val2, mask1, mask2;
1216
1217 val1 = ucontrol->value.integer.value[0] & mask;
1218 val2 = ucontrol->value.integer.value[1] & mask;
1219 if (invert) {
1220 val1 = mask - val1;
1221 val2 = mask - val2;
1222 }
1223 val1 <<= shift_left;
1224 val2 <<= shift_right;
1225 mask1 = mask << shift_left;
1226 mask2 = mask << shift_right;
1227 if (left_reg != right_reg) {
1228 change = 0;
1229 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1230 change = 1;
1231 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1232 change = 1;
1233 } else {
1234 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1235 val1 | val2) != (val1 | val2));
1236 }
1237 return change;
1238}
1239
Mark Salazarc1f6d412006-01-16 11:29:09 +01001240/* Mixer controls
1241 * These arrays contain setup data for mixer controls.
1242 *
1243 * The controls that are universal to all chipsets are fully initialized
1244 * here.
1245 */
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001246static const struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1248ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1249ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1250ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1251ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1253ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1255ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1258 .name = "Capture Source",
1259 .info = snd_es18xx_info_mux,
1260 .get = snd_es18xx_get_mux,
1261 .put = snd_es18xx_put_mux,
1262}
1263};
1264
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001265static const struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1267ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1268ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1269ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1271ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1272};
1273
Mark Salazarc1f6d412006-01-16 11:29:09 +01001274/*
1275 * The chipset specific mixer controls
1276 */
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001277static const struct snd_kcontrol_new snd_es18xx_opt_speaker =
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001278 ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001279
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001280static const struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
Ondrej Zary2603fe22014-11-03 21:35:48 +01001281ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT),
Mark Salazar95b71292006-01-16 11:35:40 +01001282ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
Mark Salazarc1f6d412006-01-16 11:29:09 +01001283ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1284ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1285};
1286
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001287static const struct snd_kcontrol_new snd_es18xx_opt_1878 =
Mark Salazar95b71292006-01-16 11:35:40 +01001288 ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1289
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001290static const struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
Mark Salazar95b71292006-01-16 11:35:40 +01001291ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1292ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1293ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1294};
1295
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001296static const struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1298};
1299
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001300static const struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1302ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1303};
1304
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001305static const struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1307{
1308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1309 .name = "3D Control - Switch",
1310 .info = snd_es18xx_info_spatializer_enable,
1311 .get = snd_es18xx_get_spatializer_enable,
1312 .put = snd_es18xx_put_spatializer_enable,
1313}
1314};
1315
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001316static const struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1318
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001319static const struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1321
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001322static const struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1325 .name = "Hardware Master Playback Volume",
1326 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1327 .info = snd_es18xx_info_hw_volume,
1328 .get = snd_es18xx_get_hw_volume,
1329},
1330{
1331 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1332 .name = "Hardware Master Playback Switch",
1333 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1334 .info = snd_es18xx_info_hw_switch,
1335 .get = snd_es18xx_get_hw_switch,
1336},
1337ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1338};
1339
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01001340static const struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = {
Ondrej Zary2603fe22014-11-03 21:35:48 +01001341ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT),
1342ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT),
1343};
1344
Bill Pemberton1bff2922012-12-06 12:35:21 -05001345static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 int data;
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 outb(reg, chip->ctrl_port);
1350 data = inb(chip->ctrl_port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return data;
1352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Bill Pemberton1bff2922012-12-06 12:35:21 -05001354static void snd_es18xx_config_write(struct snd_es18xx *chip,
1355 unsigned char reg, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 /* No need for spinlocks, this function is used only in
1358 otherwise protected init code */
1359 outb(reg, chip->ctrl_port);
1360 outb(data, chip->ctrl_port + 1);
1361#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001362 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#endif
1364}
1365
Bill Pemberton1bff2922012-12-06 12:35:21 -05001366static int snd_es18xx_initialize(struct snd_es18xx *chip,
1367 unsigned long mpu_port,
1368 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 int mask = 0;
1371
1372 /* enable extended mode */
1373 snd_es18xx_dsp_command(chip, 0xC6);
1374 /* Reset mixer registers */
1375 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1376
1377 /* Audio 1 DMA demand mode (4 bytes/request) */
1378 snd_es18xx_write(chip, 0xB9, 2);
1379 if (chip->caps & ES18XX_CONTROL) {
1380 /* Hardware volume IRQ */
1381 snd_es18xx_config_write(chip, 0x27, chip->irq);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001382 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* FM I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001384 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1385 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001387 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* MPU-401 I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001389 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1390 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* MPU-401 IRQ */
1392 snd_es18xx_config_write(chip, 0x28, chip->irq);
1393 }
1394 /* Audio1 IRQ */
1395 snd_es18xx_config_write(chip, 0x70, chip->irq);
1396 /* Audio2 IRQ */
1397 snd_es18xx_config_write(chip, 0x72, chip->irq);
1398 /* Audio1 DMA */
1399 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1400 /* Audio2 DMA */
1401 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1402
1403 /* Enable Audio 1 IRQ */
1404 snd_es18xx_write(chip, 0xB1, 0x50);
1405 /* Enable Audio 2 IRQ */
1406 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1407 /* Enable Audio 1 DMA */
1408 snd_es18xx_write(chip, 0xB2, 0x50);
1409 /* Enable MPU and hardware volume interrupt */
1410 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001411 /* Enable ESS wavetable input */
1412 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 else {
1415 int irqmask, dma1mask, dma2mask;
1416 switch (chip->irq) {
1417 case 2:
1418 case 9:
1419 irqmask = 0;
1420 break;
1421 case 5:
1422 irqmask = 1;
1423 break;
1424 case 7:
1425 irqmask = 2;
1426 break;
1427 case 10:
1428 irqmask = 3;
1429 break;
1430 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001431 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -ENODEV;
1433 }
1434 switch (chip->dma1) {
1435 case 0:
1436 dma1mask = 1;
1437 break;
1438 case 1:
1439 dma1mask = 2;
1440 break;
1441 case 3:
1442 dma1mask = 3;
1443 break;
1444 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001445 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -ENODEV;
1447 }
1448 switch (chip->dma2) {
1449 case 0:
1450 dma2mask = 0;
1451 break;
1452 case 1:
1453 dma2mask = 1;
1454 break;
1455 case 3:
1456 dma2mask = 2;
1457 break;
1458 case 5:
1459 dma2mask = 3;
1460 break;
1461 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001462 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -ENODEV;
1464 }
1465
1466 /* Enable and set Audio 1 IRQ */
1467 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1468 /* Enable and set Audio 1 DMA */
1469 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1470 /* Set Audio 2 DMA */
1471 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1472 /* Enable Audio 2 IRQ and DMA
1473 Set capture mixer input */
1474 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1475 /* Enable and set hardware volume interrupt */
1476 snd_es18xx_mixer_write(chip, 0x64, 0x06);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001477 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* MPU401 share irq with audio
1479 Joystick enabled
1480 FM enabled */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001481 snd_es18xx_mixer_write(chip, 0x40,
1482 0x43 | (mpu_port & 0xf0) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1485 }
1486 if (chip->caps & ES18XX_NEW_RATE) {
1487 /* Change behaviour of register A1
1488 4x oversampling
1489 2nd channel DAC asynchronous */
1490 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1491 }
1492 if (!(chip->caps & ES18XX_PCM2)) {
1493 /* Enable DMA FIFO */
1494 snd_es18xx_write(chip, 0xB7, 0x80);
1495 }
1496 if (chip->caps & ES18XX_SPATIALIZER) {
1497 /* Set spatializer parameters to recommended values */
1498 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1499 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1500 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1501 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1502 }
Mark Salazar95b71292006-01-16 11:35:40 +01001503 /* Flip the "enable I2S" bits for those chipsets that need it */
1504 switch (chip->version) {
1505 case 0x1879:
1506 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1507 //so a Switch control has been added to toggle this 0x71 bit on/off:
1508 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1509 /* Note: we fall through on purpose here. */
1510 case 0x1878:
1511 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1512 break;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* Mute input source */
1515 if (chip->caps & ES18XX_MUTEREC)
1516 mask = 0x10;
1517 if (chip->caps & ES18XX_RECMIX)
1518 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1519 else {
1520 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1521 snd_es18xx_write(chip, 0xb4, 0x00);
1522 }
1523#ifndef AVOID_POPS
1524 /* Enable PCM output */
1525 snd_es18xx_dsp_command(chip, 0xD1);
1526#endif
1527
1528 return 0;
1529}
1530
Takashi Iwai34d65992021-07-15 09:59:25 +02001531static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 int hi,lo;
1534
1535 /* reset */
1536 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001537 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return -ENODEV;
1539 }
1540
1541 snd_es18xx_dsp_command(chip, 0xe7);
1542 hi = snd_es18xx_dsp_get_byte(chip);
1543 if (hi < 0) {
1544 return hi;
1545 }
1546 lo = snd_es18xx_dsp_get_byte(chip);
1547 if ((lo & 0xf0) != 0x80) {
1548 return -ENODEV;
1549 }
1550 if (hi == 0x48) {
1551 chip->version = 0x488;
1552 return 0;
1553 }
1554 if (hi != 0x68) {
1555 return -ENODEV;
1556 }
1557 if ((lo & 0x0f) < 8) {
1558 chip->version = 0x688;
1559 return 0;
1560 }
1561
1562 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001563 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001565 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 lo = inb(chip->port + 0x05);
1567 if (hi != lo) {
1568 chip->version = hi << 8 | lo;
1569 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001570 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 chip->ctrl_port += inb(chip->port + 0x05);
1572
Takashi Iwai34d65992021-07-15 09:59:25 +02001573 if (!devm_request_region(card->dev, chip->ctrl_port, 8,
1574 "ES18xx - CTRL")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1576 return -EBUSY;
1577 }
1578
1579 return 0;
1580 }
1581
1582 /* If has Hardware volume */
1583 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1584 /* If has Audio2 */
1585 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1586 /* If has volume count */
1587 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1588 chip->version = 0x1887;
1589 } else {
1590 chip->version = 0x1888;
1591 }
1592 } else {
1593 chip->version = 0x1788;
1594 }
1595 }
1596 else
1597 chip->version = 0x1688;
1598 return 0;
1599}
1600
Takashi Iwai34d65992021-07-15 09:59:25 +02001601static int snd_es18xx_probe(struct snd_card *card,
1602 struct snd_es18xx *chip,
Bill Pemberton1bff2922012-12-06 12:35:21 -05001603 unsigned long mpu_port,
1604 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Takashi Iwai34d65992021-07-15 09:59:25 +02001606 if (snd_es18xx_identify(card, chip) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1608 return -ENODEV;
1609 }
1610
1611 switch (chip->version) {
1612 case 0x1868:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001613 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 break;
1615 case 0x1869:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001616 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
1618 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001619 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 break;
1621 case 0x1879:
1622 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1623 break;
1624 case 0x1887:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 case 0x1888:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001626 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001629 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 chip->port, chip->version);
1631 return -ENODEV;
1632 }
1633
1634 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1635
1636 if (chip->dma1 == chip->dma2)
1637 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1638
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001639 return snd_es18xx_initialize(chip, mpu_port, fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Arvind Yadav8965e532017-08-11 17:28:01 +05301642static const struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 .open = snd_es18xx_playback_open,
1644 .close = snd_es18xx_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 .hw_params = snd_es18xx_playback_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 .prepare = snd_es18xx_playback_prepare,
1647 .trigger = snd_es18xx_playback_trigger,
1648 .pointer = snd_es18xx_playback_pointer,
1649};
1650
Arvind Yadav8965e532017-08-11 17:28:01 +05301651static const struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .open = snd_es18xx_capture_open,
1653 .close = snd_es18xx_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .hw_params = snd_es18xx_capture_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 .prepare = snd_es18xx_capture_prepare,
1656 .trigger = snd_es18xx_capture_trigger,
1657 .pointer = snd_es18xx_capture_pointer,
1658};
1659
Lars-Peter Clausen1a2515a2015-01-02 12:24:38 +01001660static int snd_es18xx_pcm(struct snd_card *card, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001662 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001663 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 char str[16];
1665 int err;
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001668 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001669 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001670 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001671 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (err < 0)
1673 return err;
1674
1675 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1676 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1677
1678 /* global setup */
1679 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 pcm->info_flags = 0;
1681 if (chip->caps & ES18XX_DUPLEX_SAME)
1682 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1683 if (! (chip->caps & ES18XX_PCM2))
1684 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1685 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1686 chip->pcm = pcm;
1687
Takashi Iwai9ce57052019-12-09 10:48:45 +01001688 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev,
1689 64*1024,
1690 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return 0;
1692}
1693
1694/* Power Management support functions */
1695#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001696static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001698 struct snd_es18xx *chip = card->private_data;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001699
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001700 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /* power down */
1703 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1704 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1705 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1706 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1707
1708 return 0;
1709}
1710
Takashi Iwai8047c912005-11-17 14:41:22 +01001711static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001713 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1716 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1717
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001718 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 0;
1720}
1721#endif /* CONFIG_PM */
1722
Bill Pemberton1bff2922012-12-06 12:35:21 -05001723static int snd_es18xx_new_device(struct snd_card *card,
1724 unsigned long port,
1725 unsigned long mpu_port,
1726 unsigned long fm_port,
1727 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001729 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 spin_lock_init(&chip->reg_lock);
1732 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 chip->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 chip->irq = -1;
1735 chip->dma1 = -1;
1736 chip->dma2 = -1;
1737 chip->audio2_vol = 0x00;
1738 chip->active = 0;
1739
Takashi Iwai34d65992021-07-15 09:59:25 +02001740 if (!devm_request_region(card->dev, port, 16, "ES18xx")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1742 return -EBUSY;
1743 }
1744
Takashi Iwai34d65992021-07-15 09:59:25 +02001745 if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx",
1746 (void *) card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1748 return -EBUSY;
1749 }
1750 chip->irq = irq;
Takashi Iwai58dad832019-12-10 07:34:40 +01001751 card->sync_irq = chip->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Takashi Iwai34d65992021-07-15 09:59:25 +02001753 if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1755 return -EBUSY;
1756 }
1757 chip->dma1 = dma1;
1758
Takashi Iwai34d65992021-07-15 09:59:25 +02001759 if (dma2 != dma1 &&
1760 snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1762 return -EBUSY;
1763 }
1764 chip->dma2 = dma2;
1765
Takashi Iwai34d65992021-07-15 09:59:25 +02001766 if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0)
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001767 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769}
1770
Bill Pemberton1bff2922012-12-06 12:35:21 -05001771static int snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001773 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int err;
1775 unsigned int idx;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 strcpy(card->mixername, chip->pcm->name);
1778
1779 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001780 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1782 if (chip->caps & ES18XX_HWV) {
1783 switch (idx) {
1784 case 0:
1785 chip->master_volume = kctl;
1786 kctl->private_free = snd_es18xx_hwv_free;
1787 break;
1788 case 1:
1789 chip->master_switch = kctl;
1790 kctl->private_free = snd_es18xx_hwv_free;
1791 break;
1792 }
1793 }
Takashi Iwai45e04712021-06-08 16:04:44 +02001794 err = snd_ctl_add(card, kctl);
1795 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return err;
1797 }
1798 if (chip->caps & ES18XX_PCM2) {
1799 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001800 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip));
1801 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return err;
1803 }
1804 } else {
1805 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001806 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip));
1807 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809 }
1810 }
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (chip->caps & ES18XX_RECMIX) {
1813 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001814 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip));
1815 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return err;
1817 }
1818 }
1819 switch (chip->version) {
1820 default:
Takashi Iwai45e04712021-06-08 16:04:44 +02001821 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip));
1822 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return err;
1824 break;
1825 case 0x1869:
1826 case 0x1879:
Takashi Iwai45e04712021-06-08 16:04:44 +02001827 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip));
1828 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830 break;
1831 }
1832 if (chip->caps & ES18XX_SPATIALIZER) {
1833 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001834 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip));
1835 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return err;
1837 }
1838 }
1839 if (chip->caps & ES18XX_HWV) {
1840 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001841 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1843 if (idx == 0)
1844 chip->hw_volume = kctl;
1845 else
1846 chip->hw_switch = kctl;
1847 kctl->private_free = snd_es18xx_hwv_free;
Takashi Iwai45e04712021-06-08 16:04:44 +02001848 err = snd_ctl_add(card, kctl);
1849 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return err;
1851
1852 }
1853 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001854 /* finish initializing other chipset specific controls
1855 */
1856 if (chip->version != 0x1868) {
1857 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1858 chip));
1859 if (err < 0)
1860 return err;
1861 }
1862 if (chip->version == 0x1869) {
1863 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1864 err = snd_ctl_add(card,
1865 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1866 chip));
1867 if (err < 0)
1868 return err;
1869 }
Mark Salazar95b71292006-01-16 11:35:40 +01001870 } else if (chip->version == 0x1878) {
1871 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1872 chip));
1873 if (err < 0)
1874 return err;
1875 } else if (chip->version == 0x1879) {
1876 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1877 err = snd_ctl_add(card,
1878 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1879 chip));
1880 if (err < 0)
1881 return err;
1882 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001883 }
Ondrej Zary2603fe22014-11-03 21:35:48 +01001884 if (chip->caps & ES18XX_GPO_2BIT) {
1885 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
1886 err = snd_ctl_add(card,
1887 snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
1888 chip));
1889 if (err < 0)
1890 return err;
1891 }
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return 0;
1894}
1895
1896
1897/* Card level */
1898
Leon Romanovsky6417f032021-03-17 12:45:47 +02001899MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1901MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1904static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +10301905static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +10301907static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#endif
1909static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1910#ifndef CONFIG_PNP
1911static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1912#else
1913static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1914#endif
1915static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1916static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1917static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1918static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1919
1920module_param_array(index, int, NULL, 0444);
1921MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1922module_param_array(id, charp, NULL, 0444);
1923MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1924module_param_array(enable, bool, NULL, 0444);
1925MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1926#ifdef CONFIG_PNP
1927module_param_array(isapnp, bool, NULL, 0444);
1928MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1929#endif
David Howellse992ef52017-04-04 16:54:30 +01001930module_param_hw_array(port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001932module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001934module_param_hw_array(fm_port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001936module_param_hw_array(irq, int, irq, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001938module_param_hw_array(dma1, int, dma, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001940module_param_hw_array(dma2, int, dma, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02001944static int isa_registered;
1945static int pnp_registered;
1946static int pnpc_registered;
Ondrej Zary1c3985582006-07-31 12:51:57 +02001947
Arvind Yadav05cb1832017-08-17 15:36:17 +05301948static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
Ondrej Zary1c3985582006-07-31 12:51:57 +02001949 { .id = "ESS1869" },
Rene Herman4848ffe52007-08-01 23:50:21 +02001950 { .id = "ESS1879" },
Ondrej Zary1c3985582006-07-31 12:51:57 +02001951 { .id = "" } /* end */
1952};
1953
1954MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
1955
1956/* PnP main device initialization */
Bill Pemberton1bff2922012-12-06 12:35:21 -05001957static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001958{
Rene Herman109c53f842007-11-30 17:59:25 +01001959 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c3985582006-07-31 12:51:57 +02001960 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1961 return -EBUSY;
1962 }
1963 /* ok. hack using Vendor-Defined Card-Level registers */
1964 /* skip csn and logdev initialization - already done in isapnp_configure */
1965 if (pnp_device_is_isapnp(pdev)) {
1966 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1967 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1968 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1969 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1970 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1971 isapnp_cfg_end();
1972 }
1973 port[dev] = pnp_port_start(pdev, 0);
1974 fm_port[dev] = pnp_port_start(pdev, 1);
1975 mpu_port[dev] = pnp_port_start(pdev, 2);
1976 dma1[dev] = pnp_dma(pdev, 0);
1977 dma2[dev] = pnp_dma(pdev, 1);
1978 irq[dev] = pnp_irq(pdev, 0);
1979 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
1980 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
1981 return 0;
1982}
1983
Bill Pemberton1bff2922012-12-06 12:35:21 -05001984static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
1985 struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001986{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001987 chip->dev = pdev;
1988 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001989 return -EBUSY;
Ondrej Zary1c3985582006-07-31 12:51:57 +02001990 return 0;
1991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Arvind Yadav05cb1832017-08-17 15:36:17 +05301993static const struct pnp_card_device_id snd_audiodrive_pnpids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1995 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1996 /* ESS 1868 (integrated on Maxisound Cards) */
1997 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1998 /* ESS 1868 (integrated on Maxisound Cards) */
1999 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2000 /* ESS ES1869 Plug and Play AudioDrive */
2001 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2002 /* ESS 1869 */
2003 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2004 /* ESS 1878 */
2005 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2006 /* ESS 1879 */
2007 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2008 /* --- */
2009 { .id = "" } /* end */
2010};
2011
2012MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2013
Bill Pemberton1bff2922012-12-06 12:35:21 -05002014static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2015 struct pnp_card_link *card,
2016 const struct pnp_card_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002018 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2019 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002021
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002022 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2023 if (chip->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* Control port initialization */
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002027 if (pnp_activate_dev(chip->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2029 return -EAGAIN;
2030 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002031 snd_printdd("pnp: port=0x%llx\n",
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002032 (unsigned long long)pnp_port_start(chip->devc, 0));
2033 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 0;
2037}
2038#endif /* CONFIG_PNP */
2039
Takashi Iwai43bcd972005-09-05 17:19:20 +02002040#ifdef CONFIG_PNP
2041#define is_isapnp_selected(dev) isapnp[dev]
2042#else
2043#define is_isapnp_selected(dev) 0
2044#endif
2045
Takashi Iwai4323cc42014-01-29 13:03:56 +01002046static int snd_es18xx_card_new(struct device *pdev, int dev,
2047 struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Takashi Iwai34d65992021-07-15 09:59:25 +02002049 return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2050 sizeof(struct snd_es18xx), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002051}
2052
Bill Pemberton1bff2922012-12-06 12:35:21 -05002053static int snd_audiodrive_probe(struct snd_card *card, int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002054{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002055 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002056 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int err;
2058
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002059 err = snd_es18xx_new_device(card,
2060 port[dev], mpu_port[dev], fm_port[dev],
2061 irq[dev], dma1[dev], dma2[dev]);
2062 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002068 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2070 card->shortname,
2071 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002072 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 else
2074 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2075 card->shortname,
2076 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002077 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Lars-Peter Clausen1a2515a2015-01-02 12:24:38 +01002079 err = snd_es18xx_pcm(card, 0);
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002080 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002081 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002082
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002083 err = snd_es18xx_mixer(card);
2084 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002085 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002088 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2089 OPL3_HW_OPL3, 0, &opl3) < 0) {
2090 snd_printk(KERN_WARNING PFX
2091 "opl3 not detected at 0x%lx\n",
2092 fm_port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 } else {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002094 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2095 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 }
2099
2100 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002101 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002102 mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2103 -1, &chip->rmidi);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002104 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002108 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Bill Pemberton1bff2922012-12-06 12:35:21 -05002111static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002112{
2113 return enable[dev] && !is_isapnp_selected(dev);
2114}
2115
Bill Pemberton1bff2922012-12-06 12:35:21 -05002116static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002118 struct snd_card *card;
2119 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwai4323cc42014-01-29 13:03:56 +01002121 err = snd_es18xx_card_new(devptr, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002122 if (err < 0)
2123 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002124 err = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002125 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002126 return err;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002127 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Bill Pemberton1bff2922012-12-06 12:35:21 -05002131static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002132{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002133 int err;
Takashi Iwaifce67092020-01-05 15:48:03 +01002134 static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2135 static const int possible_dmas[] = {1, 0, 3, 5, -1};
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002136
2137 if (irq[dev] == SNDRV_AUTO_IRQ) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002138 irq[dev] = snd_legacy_find_free_irq(possible_irqs);
2139 if (irq[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002140 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2141 return -EBUSY;
2142 }
2143 }
2144 if (dma1[dev] == SNDRV_AUTO_DMA) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002145 dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
2146 if (dma1[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002147 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2148 return -EBUSY;
2149 }
2150 }
2151 if (dma2[dev] == SNDRV_AUTO_DMA) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002152 dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
2153 if (dma2[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002154 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2155 return -EBUSY;
2156 }
2157 }
2158
2159 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002160 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002161 } else {
Takashi Iwaifce67092020-01-05 15:48:03 +01002162 static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002163 int i;
2164 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2165 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002166 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002167 if (! err)
2168 return 0;
2169 }
2170 return err;
2171 }
2172}
2173
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002174#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002175static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2176 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002177{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002178 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002179}
2180
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002181static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002182{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002183 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002184}
2185#endif
2186
Rene Herman83c51c02007-03-20 11:33:46 +01002187#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002188
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002189static struct isa_driver snd_es18xx_isa_driver = {
2190 .match = snd_es18xx_isa_match,
2191 .probe = snd_es18xx_isa_probe,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002192#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002193 .suspend = snd_es18xx_isa_suspend,
2194 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002195#endif
2196 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002197 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002198 },
2199};
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -05002203static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2204 const struct pnp_device_id *id)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002205{
2206 static int dev;
2207 int err;
2208 struct snd_card *card;
2209
2210 if (pnp_device_is_isapnp(pdev))
2211 return -ENOENT; /* we have another procedure - card */
2212 for (; dev < SNDRV_CARDS; dev++) {
2213 if (enable[dev] && isapnp[dev])
2214 break;
2215 }
2216 if (dev >= SNDRV_CARDS)
2217 return -ENODEV;
2218
Takashi Iwai4323cc42014-01-29 13:03:56 +01002219 err = snd_es18xx_card_new(&pdev->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002220 if (err < 0)
2221 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002222 err = snd_audiodrive_pnp(dev, card->private_data, pdev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002223 if (err < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002224 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002225 err = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002226 if (err < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002227 return err;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002228 pnp_set_drvdata(pdev, card);
2229 dev++;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002230 return 0;
2231}
2232
Ondrej Zary1c3985582006-07-31 12:51:57 +02002233#ifdef CONFIG_PM
2234static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2235{
2236 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2237}
2238static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2239{
2240 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2241}
2242#endif
2243
2244static struct pnp_driver es18xx_pnp_driver = {
2245 .name = "es18xx-pnpbios",
2246 .id_table = snd_audiodrive_pnpbiosids,
2247 .probe = snd_audiodrive_pnp_detect,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002248#ifdef CONFIG_PM
2249 .suspend = snd_audiodrive_pnp_suspend,
2250 .resume = snd_audiodrive_pnp_resume,
2251#endif
2252};
2253
Bill Pemberton1bff2922012-12-06 12:35:21 -05002254static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2255 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002258 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int res;
2260
2261 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002262 if (enable[dev] && isapnp[dev])
2263 break;
2264 }
2265 if (dev >= SNDRV_CARDS)
2266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Takashi Iwai4323cc42014-01-29 13:03:56 +01002268 res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002269 if (res < 0)
2270 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002271
Takashi Iwai45e04712021-06-08 16:04:44 +02002272 res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid);
Takashi Iwai34d65992021-07-15 09:59:25 +02002273 if (res < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002274 return res;
Takashi Iwai45e04712021-06-08 16:04:44 +02002275 res = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002276 if (res < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002277 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002278
2279 pnp_set_card_drvdata(pcard, card);
2280 dev++;
2281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002284#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002285static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002286{
2287 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2288}
2289
Ondrej Zary1c3985582006-07-31 12:51:57 +02002290static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002291{
2292 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2293}
2294
2295#endif
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static struct pnp_card_driver es18xx_pnpc_driver = {
2298 .flags = PNP_DRIVER_RES_DISABLE,
2299 .name = "es18xx",
2300 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002301 .probe = snd_audiodrive_pnpc_detect,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002302#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002303 .suspend = snd_audiodrive_pnpc_suspend,
2304 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306};
2307#endif /* CONFIG_PNP */
2308
2309static int __init alsa_card_es18xx_init(void)
2310{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002311 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002313 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002314#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002315 if (!err)
2316 isa_registered = 1;
2317
Ondrej Zary1c3985582006-07-31 12:51:57 +02002318 err = pnp_register_driver(&es18xx_pnp_driver);
2319 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002320 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002321
Ondrej Zary1c3985582006-07-31 12:51:57 +02002322 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2323 if (!err)
2324 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002325
2326 if (isa_registered || pnp_registered)
2327 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002328#endif
Rene Herman609d7692007-05-15 11:42:56 +02002329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332static void __exit alsa_card_es18xx_exit(void)
2333{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002334#ifdef CONFIG_PNP
2335 if (pnpc_registered)
2336 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2337 if (pnp_registered)
2338 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002339 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002340#endif
Rene Herman609d7692007-05-15 11:42:56 +02002341 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344module_init(alsa_card_es18xx_init)
2345module_exit(alsa_card_es18xx_exit)