blob: 0a32845b1017a6463b4471869e8acc693f51a5d5 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 outb(reg, chip->ctrl_port);
ye xingchen96ecdc712022-09-20 06:46:05 +00001348 return inb(chip->ctrl_port + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Bill Pemberton1bff2922012-12-06 12:35:21 -05001351static void snd_es18xx_config_write(struct snd_es18xx *chip,
1352 unsigned char reg, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 /* No need for spinlocks, this function is used only in
1355 otherwise protected init code */
1356 outb(reg, chip->ctrl_port);
1357 outb(data, chip->ctrl_port + 1);
1358#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001359 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#endif
1361}
1362
Bill Pemberton1bff2922012-12-06 12:35:21 -05001363static int snd_es18xx_initialize(struct snd_es18xx *chip,
1364 unsigned long mpu_port,
1365 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 int mask = 0;
1368
1369 /* enable extended mode */
1370 snd_es18xx_dsp_command(chip, 0xC6);
1371 /* Reset mixer registers */
1372 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1373
1374 /* Audio 1 DMA demand mode (4 bytes/request) */
1375 snd_es18xx_write(chip, 0xB9, 2);
1376 if (chip->caps & ES18XX_CONTROL) {
1377 /* Hardware volume IRQ */
1378 snd_es18xx_config_write(chip, 0x27, chip->irq);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001379 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* FM I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001381 snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1382 snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001384 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* MPU-401 I/O */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001386 snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1387 snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* MPU-401 IRQ */
1389 snd_es18xx_config_write(chip, 0x28, chip->irq);
1390 }
1391 /* Audio1 IRQ */
1392 snd_es18xx_config_write(chip, 0x70, chip->irq);
1393 /* Audio2 IRQ */
1394 snd_es18xx_config_write(chip, 0x72, chip->irq);
1395 /* Audio1 DMA */
1396 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1397 /* Audio2 DMA */
1398 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1399
1400 /* Enable Audio 1 IRQ */
1401 snd_es18xx_write(chip, 0xB1, 0x50);
1402 /* Enable Audio 2 IRQ */
1403 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1404 /* Enable Audio 1 DMA */
1405 snd_es18xx_write(chip, 0xB2, 0x50);
1406 /* Enable MPU and hardware volume interrupt */
1407 snd_es18xx_mixer_write(chip, 0x64, 0x42);
Krzysztof Helt1bc9eed2008-01-07 12:24:45 +01001408 /* Enable ESS wavetable input */
1409 snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 else {
1412 int irqmask, dma1mask, dma2mask;
1413 switch (chip->irq) {
1414 case 2:
1415 case 9:
1416 irqmask = 0;
1417 break;
1418 case 5:
1419 irqmask = 1;
1420 break;
1421 case 7:
1422 irqmask = 2;
1423 break;
1424 case 10:
1425 irqmask = 3;
1426 break;
1427 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001428 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -ENODEV;
1430 }
1431 switch (chip->dma1) {
1432 case 0:
1433 dma1mask = 1;
1434 break;
1435 case 1:
1436 dma1mask = 2;
1437 break;
1438 case 3:
1439 dma1mask = 3;
1440 break;
1441 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001442 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -ENODEV;
1444 }
1445 switch (chip->dma2) {
1446 case 0:
1447 dma2mask = 0;
1448 break;
1449 case 1:
1450 dma2mask = 1;
1451 break;
1452 case 3:
1453 dma2mask = 2;
1454 break;
1455 case 5:
1456 dma2mask = 3;
1457 break;
1458 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001459 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return -ENODEV;
1461 }
1462
1463 /* Enable and set Audio 1 IRQ */
1464 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1465 /* Enable and set Audio 1 DMA */
1466 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1467 /* Set Audio 2 DMA */
1468 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1469 /* Enable Audio 2 IRQ and DMA
1470 Set capture mixer input */
1471 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1472 /* Enable and set hardware volume interrupt */
1473 snd_es18xx_mixer_write(chip, 0x64, 0x06);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001474 if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* MPU401 share irq with audio
1476 Joystick enabled
1477 FM enabled */
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001478 snd_es18xx_mixer_write(chip, 0x40,
1479 0x43 | (mpu_port & 0xf0) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1482 }
1483 if (chip->caps & ES18XX_NEW_RATE) {
1484 /* Change behaviour of register A1
1485 4x oversampling
1486 2nd channel DAC asynchronous */
1487 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1488 }
1489 if (!(chip->caps & ES18XX_PCM2)) {
1490 /* Enable DMA FIFO */
1491 snd_es18xx_write(chip, 0xB7, 0x80);
1492 }
1493 if (chip->caps & ES18XX_SPATIALIZER) {
1494 /* Set spatializer parameters to recommended values */
1495 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1496 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1497 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1498 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1499 }
Mark Salazar95b71292006-01-16 11:35:40 +01001500 /* Flip the "enable I2S" bits for those chipsets that need it */
1501 switch (chip->version) {
1502 case 0x1879:
1503 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1504 //so a Switch control has been added to toggle this 0x71 bit on/off:
1505 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1506 /* Note: we fall through on purpose here. */
1507 case 0x1878:
1508 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1509 break;
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Mute input source */
1512 if (chip->caps & ES18XX_MUTEREC)
1513 mask = 0x10;
1514 if (chip->caps & ES18XX_RECMIX)
1515 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1516 else {
1517 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1518 snd_es18xx_write(chip, 0xb4, 0x00);
1519 }
1520#ifndef AVOID_POPS
1521 /* Enable PCM output */
1522 snd_es18xx_dsp_command(chip, 0xD1);
1523#endif
1524
1525 return 0;
1526}
1527
Takashi Iwai34d65992021-07-15 09:59:25 +02001528static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int hi,lo;
1531
1532 /* reset */
1533 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001534 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -ENODEV;
1536 }
1537
1538 snd_es18xx_dsp_command(chip, 0xe7);
1539 hi = snd_es18xx_dsp_get_byte(chip);
1540 if (hi < 0) {
1541 return hi;
1542 }
1543 lo = snd_es18xx_dsp_get_byte(chip);
1544 if ((lo & 0xf0) != 0x80) {
1545 return -ENODEV;
1546 }
1547 if (hi == 0x48) {
1548 chip->version = 0x488;
1549 return 0;
1550 }
1551 if (hi != 0x68) {
1552 return -ENODEV;
1553 }
1554 if ((lo & 0x0f) < 8) {
1555 chip->version = 0x688;
1556 return 0;
1557 }
1558
1559 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001560 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001562 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 lo = inb(chip->port + 0x05);
1564 if (hi != lo) {
1565 chip->version = hi << 8 | lo;
1566 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001567 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 chip->ctrl_port += inb(chip->port + 0x05);
1569
Takashi Iwai34d65992021-07-15 09:59:25 +02001570 if (!devm_request_region(card->dev, chip->ctrl_port, 8,
1571 "ES18xx - CTRL")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1573 return -EBUSY;
1574 }
1575
1576 return 0;
1577 }
1578
1579 /* If has Hardware volume */
1580 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1581 /* If has Audio2 */
1582 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1583 /* If has volume count */
1584 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1585 chip->version = 0x1887;
1586 } else {
1587 chip->version = 0x1888;
1588 }
1589 } else {
1590 chip->version = 0x1788;
1591 }
1592 }
1593 else
1594 chip->version = 0x1688;
1595 return 0;
1596}
1597
Takashi Iwai34d65992021-07-15 09:59:25 +02001598static int snd_es18xx_probe(struct snd_card *card,
1599 struct snd_es18xx *chip,
Bill Pemberton1bff2922012-12-06 12:35:21 -05001600 unsigned long mpu_port,
1601 unsigned long fm_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Takashi Iwai34d65992021-07-15 09:59:25 +02001603 if (snd_es18xx_identify(card, chip) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1605 return -ENODEV;
1606 }
1607
1608 switch (chip->version) {
1609 case 0x1868:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001610 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
1612 case 0x1869:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001613 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 -07001614 break;
1615 case 0x1878:
Mark Salazar14086772006-01-16 11:33:52 +01001616 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
1618 case 0x1879:
1619 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1620 break;
1621 case 0x1887:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 case 0x1888:
Ondrej Zary2603fe22014-11-03 21:35:48 +01001623 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 break;
1625 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001626 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 chip->port, chip->version);
1628 return -ENODEV;
1629 }
1630
1631 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1632
1633 if (chip->dma1 == chip->dma2)
1634 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1635
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001636 return snd_es18xx_initialize(chip, mpu_port, fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Arvind Yadav8965e532017-08-11 17:28:01 +05301639static const struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 .open = snd_es18xx_playback_open,
1641 .close = snd_es18xx_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 .hw_params = snd_es18xx_playback_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 .prepare = snd_es18xx_playback_prepare,
1644 .trigger = snd_es18xx_playback_trigger,
1645 .pointer = snd_es18xx_playback_pointer,
1646};
1647
Arvind Yadav8965e532017-08-11 17:28:01 +05301648static const struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 .open = snd_es18xx_capture_open,
1650 .close = snd_es18xx_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 .hw_params = snd_es18xx_capture_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .prepare = snd_es18xx_capture_prepare,
1653 .trigger = snd_es18xx_capture_trigger,
1654 .pointer = snd_es18xx_capture_pointer,
1655};
1656
Lars-Peter Clausen1a2515a2015-01-02 12:24:38 +01001657static int snd_es18xx_pcm(struct snd_card *card, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001659 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01001660 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 char str[16];
1662 int err;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001665 if (chip->caps & ES18XX_PCM2)
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001666 err = snd_pcm_new(card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001667 else
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001668 err = snd_pcm_new(card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (err < 0)
1670 return err;
1671
1672 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1673 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1674
1675 /* global setup */
1676 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 pcm->info_flags = 0;
1678 if (chip->caps & ES18XX_DUPLEX_SAME)
1679 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1680 if (! (chip->caps & ES18XX_PCM2))
1681 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1682 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1683 chip->pcm = pcm;
1684
Takashi Iwai9ce57052019-12-09 10:48:45 +01001685 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev,
1686 64*1024,
1687 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return 0;
1689}
1690
1691/* Power Management support functions */
1692#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001693static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001695 struct snd_es18xx *chip = card->private_data;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001696
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001697 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* power down */
1700 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1701 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1702 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1703 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1704
1705 return 0;
1706}
1707
Takashi Iwai8047c912005-11-17 14:41:22 +01001708static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001710 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1713 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1714
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01001715 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717}
1718#endif /* CONFIG_PM */
1719
Bill Pemberton1bff2922012-12-06 12:35:21 -05001720static int snd_es18xx_new_device(struct snd_card *card,
1721 unsigned long port,
1722 unsigned long mpu_port,
1723 unsigned long fm_port,
1724 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001726 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_lock_init(&chip->reg_lock);
1729 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 chip->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 chip->irq = -1;
1732 chip->dma1 = -1;
1733 chip->dma2 = -1;
1734 chip->audio2_vol = 0x00;
1735 chip->active = 0;
1736
Takashi Iwai34d65992021-07-15 09:59:25 +02001737 if (!devm_request_region(card->dev, port, 16, "ES18xx")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1739 return -EBUSY;
1740 }
1741
Takashi Iwai34d65992021-07-15 09:59:25 +02001742 if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx",
1743 (void *) card)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1745 return -EBUSY;
1746 }
1747 chip->irq = irq;
Takashi Iwai58dad832019-12-10 07:34:40 +01001748 card->sync_irq = chip->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Takashi Iwai34d65992021-07-15 09:59:25 +02001750 if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1752 return -EBUSY;
1753 }
1754 chip->dma1 = dma1;
1755
Takashi Iwai34d65992021-07-15 09:59:25 +02001756 if (dma2 != dma1 &&
1757 snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1759 return -EBUSY;
1760 }
1761 chip->dma2 = dma2;
1762
Takashi Iwai34d65992021-07-15 09:59:25 +02001763 if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0)
Krzysztof Heltfaa12422009-11-08 11:58:08 +01001764 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return 0;
1766}
1767
Bill Pemberton1bff2922012-12-06 12:35:21 -05001768static int snd_es18xx_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001770 struct snd_es18xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int err;
1772 unsigned int idx;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 strcpy(card->mixername, chip->pcm->name);
1775
1776 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001777 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1779 if (chip->caps & ES18XX_HWV) {
1780 switch (idx) {
1781 case 0:
1782 chip->master_volume = kctl;
1783 kctl->private_free = snd_es18xx_hwv_free;
1784 break;
1785 case 1:
1786 chip->master_switch = kctl;
1787 kctl->private_free = snd_es18xx_hwv_free;
1788 break;
1789 }
1790 }
Takashi Iwai45e04712021-06-08 16:04:44 +02001791 err = snd_ctl_add(card, kctl);
1792 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return err;
1794 }
1795 if (chip->caps & ES18XX_PCM2) {
1796 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001797 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip));
1798 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return err;
1800 }
1801 } else {
1802 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001803 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip));
1804 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return err;
1806 }
1807 }
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (chip->caps & ES18XX_RECMIX) {
1810 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001811 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip));
1812 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return err;
1814 }
1815 }
1816 switch (chip->version) {
1817 default:
Takashi Iwai45e04712021-06-08 16:04:44 +02001818 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip));
1819 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return err;
1821 break;
1822 case 0x1869:
1823 case 0x1879:
Takashi Iwai45e04712021-06-08 16:04:44 +02001824 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip));
1825 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return err;
1827 break;
1828 }
1829 if (chip->caps & ES18XX_SPATIALIZER) {
1830 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
Takashi Iwai45e04712021-06-08 16:04:44 +02001831 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip));
1832 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return err;
1834 }
1835 }
1836 if (chip->caps & ES18XX_HWV) {
1837 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001838 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1840 if (idx == 0)
1841 chip->hw_volume = kctl;
1842 else
1843 chip->hw_switch = kctl;
1844 kctl->private_free = snd_es18xx_hwv_free;
Takashi Iwai45e04712021-06-08 16:04:44 +02001845 err = snd_ctl_add(card, kctl);
1846 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return err;
1848
1849 }
1850 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001851 /* finish initializing other chipset specific controls
1852 */
1853 if (chip->version != 0x1868) {
1854 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1855 chip));
1856 if (err < 0)
1857 return err;
1858 }
1859 if (chip->version == 0x1869) {
1860 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1861 err = snd_ctl_add(card,
1862 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1863 chip));
1864 if (err < 0)
1865 return err;
1866 }
Mark Salazar95b71292006-01-16 11:35:40 +01001867 } else if (chip->version == 0x1878) {
1868 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1869 chip));
1870 if (err < 0)
1871 return err;
1872 } else if (chip->version == 0x1879) {
1873 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1874 err = snd_ctl_add(card,
1875 snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1876 chip));
1877 if (err < 0)
1878 return err;
1879 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001880 }
Ondrej Zary2603fe22014-11-03 21:35:48 +01001881 if (chip->caps & ES18XX_GPO_2BIT) {
1882 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
1883 err = snd_ctl_add(card,
1884 snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
1885 chip));
1886 if (err < 0)
1887 return err;
1888 }
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return 0;
1891}
1892
1893
1894/* Card level */
1895
Leon Romanovsky6417f032021-03-17 12:45:47 +02001896MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1898MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1901static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +10301902static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903#ifdef CONFIG_PNP
Rusty Russella67ff6a2011-12-15 13:49:36 +10301904static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#endif
1906static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1907#ifndef CONFIG_PNP
1908static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1909#else
1910static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1911#endif
1912static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1913static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1914static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1915static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1916
1917module_param_array(index, int, NULL, 0444);
1918MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1919module_param_array(id, charp, NULL, 0444);
1920MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1921module_param_array(enable, bool, NULL, 0444);
1922MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1923#ifdef CONFIG_PNP
1924module_param_array(isapnp, bool, NULL, 0444);
1925MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1926#endif
David Howellse992ef52017-04-04 16:54:30 +01001927module_param_hw_array(port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001929module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001931module_param_hw_array(fm_port, long, ioport, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001933module_param_hw_array(irq, int, irq, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001935module_param_hw_array(dma1, int, dma, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
David Howellse992ef52017-04-04 16:54:30 +01001937module_param_hw_array(dma2, int, dma, NULL, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02001941static int isa_registered;
1942static int pnp_registered;
1943static int pnpc_registered;
Ondrej Zary1c3985582006-07-31 12:51:57 +02001944
Arvind Yadav05cb1832017-08-17 15:36:17 +05301945static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
Ondrej Zary1c3985582006-07-31 12:51:57 +02001946 { .id = "ESS1869" },
Rene Herman4848ffe52007-08-01 23:50:21 +02001947 { .id = "ESS1879" },
Ondrej Zary1c3985582006-07-31 12:51:57 +02001948 { .id = "" } /* end */
1949};
1950
1951MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
1952
1953/* PnP main device initialization */
Bill Pemberton1bff2922012-12-06 12:35:21 -05001954static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001955{
Rene Herman109c53f842007-11-30 17:59:25 +01001956 if (pnp_activate_dev(pdev) < 0) {
Ondrej Zary1c3985582006-07-31 12:51:57 +02001957 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1958 return -EBUSY;
1959 }
1960 /* ok. hack using Vendor-Defined Card-Level registers */
1961 /* skip csn and logdev initialization - already done in isapnp_configure */
1962 if (pnp_device_is_isapnp(pdev)) {
1963 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1964 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1965 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1966 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1967 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1968 isapnp_cfg_end();
1969 }
1970 port[dev] = pnp_port_start(pdev, 0);
1971 fm_port[dev] = pnp_port_start(pdev, 1);
1972 mpu_port[dev] = pnp_port_start(pdev, 2);
1973 dma1[dev] = pnp_dma(pdev, 0);
1974 dma2[dev] = pnp_dma(pdev, 1);
1975 irq[dev] = pnp_irq(pdev, 0);
1976 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
1977 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
1978 return 0;
1979}
1980
Bill Pemberton1bff2922012-12-06 12:35:21 -05001981static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
1982 struct pnp_dev *pdev)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001983{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01001984 chip->dev = pdev;
1985 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02001986 return -EBUSY;
Ondrej Zary1c3985582006-07-31 12:51:57 +02001987 return 0;
1988}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Arvind Yadav05cb1832017-08-17 15:36:17 +05301990static const struct pnp_card_device_id snd_audiodrive_pnpids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1992 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1993 /* ESS 1868 (integrated on Maxisound Cards) */
1994 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1995 /* ESS 1868 (integrated on Maxisound Cards) */
1996 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
1997 /* ESS ES1869 Plug and Play AudioDrive */
1998 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
1999 /* ESS 1869 */
2000 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2001 /* ESS 1878 */
2002 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2003 /* ESS 1879 */
2004 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2005 /* --- */
2006 { .id = "" } /* end */
2007};
2008
2009MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2010
Bill Pemberton1bff2922012-12-06 12:35:21 -05002011static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2012 struct pnp_card_link *card,
2013 const struct pnp_card_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002015 chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2016 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002018
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002019 chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2020 if (chip->devc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Control port initialization */
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002024 if (pnp_activate_dev(chip->devc) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2026 return -EAGAIN;
2027 }
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002028 snd_printdd("pnp: port=0x%llx\n",
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002029 (unsigned long long)pnp_port_start(chip->devc, 0));
2030 if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return 0;
2034}
2035#endif /* CONFIG_PNP */
2036
Takashi Iwai43bcd972005-09-05 17:19:20 +02002037#ifdef CONFIG_PNP
2038#define is_isapnp_selected(dev) isapnp[dev]
2039#else
2040#define is_isapnp_selected(dev) 0
2041#endif
2042
Takashi Iwai4323cc42014-01-29 13:03:56 +01002043static int snd_es18xx_card_new(struct device *pdev, int dev,
2044 struct snd_card **cardp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Takashi Iwai34d65992021-07-15 09:59:25 +02002046 return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2047 sizeof(struct snd_es18xx), cardp);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002048}
2049
Bill Pemberton1bff2922012-12-06 12:35:21 -05002050static int snd_audiodrive_probe(struct snd_card *card, int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002051{
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002052 struct snd_es18xx *chip = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002053 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int err;
2055
Krzysztof Heltb14f5de2009-10-25 11:10:01 +01002056 err = snd_es18xx_new_device(card,
2057 port[dev], mpu_port[dev], fm_port[dev],
2058 irq[dev], dma1[dev], dma2[dev]);
2059 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002065 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2067 card->shortname,
2068 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002069 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 else
2071 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2072 card->shortname,
2073 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002074 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Lars-Peter Clausen1a2515a2015-01-02 12:24:38 +01002076 err = snd_es18xx_pcm(card, 0);
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002077 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002078 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002079
Krzysztof Helt3c76b4d2009-10-25 11:05:19 +01002080 err = snd_es18xx_mixer(card);
2081 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002085 if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2086 OPL3_HW_OPL3, 0, &opl3) < 0) {
2087 snd_printk(KERN_WARNING PFX
2088 "opl3 not detected at 0x%lx\n",
2089 fm_port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 } else {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002091 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2092 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
2096
2097 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002098 err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002099 mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2100 -1, &chip->rmidi);
Krzysztof Heltfaa12422009-11-08 11:58:08 +01002101 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002105 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Bill Pemberton1bff2922012-12-06 12:35:21 -05002108static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002109{
2110 return enable[dev] && !is_isapnp_selected(dev);
2111}
2112
Bill Pemberton1bff2922012-12-06 12:35:21 -05002113static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002115 struct snd_card *card;
2116 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Takashi Iwai4323cc42014-01-29 13:03:56 +01002118 err = snd_es18xx_card_new(devptr, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002119 if (err < 0)
2120 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002121 err = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002122 if (err < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002123 return err;
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002124 dev_set_drvdata(devptr, card);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Bill Pemberton1bff2922012-12-06 12:35:21 -05002128static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002129{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002130 int err;
Takashi Iwaifce67092020-01-05 15:48:03 +01002131 static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2132 static const int possible_dmas[] = {1, 0, 3, 5, -1};
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002133
2134 if (irq[dev] == SNDRV_AUTO_IRQ) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002135 irq[dev] = snd_legacy_find_free_irq(possible_irqs);
2136 if (irq[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002137 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2138 return -EBUSY;
2139 }
2140 }
2141 if (dma1[dev] == SNDRV_AUTO_DMA) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002142 dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
2143 if (dma1[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002144 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2145 return -EBUSY;
2146 }
2147 }
2148 if (dma2[dev] == SNDRV_AUTO_DMA) {
Takashi Iwai45e04712021-06-08 16:04:44 +02002149 dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
2150 if (dma2[dev] < 0) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002151 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2152 return -EBUSY;
2153 }
2154 }
2155
2156 if (port[dev] != SNDRV_AUTO_PORT) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002157 return snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002158 } else {
Takashi Iwaifce67092020-01-05 15:48:03 +01002159 static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002160 int i;
2161 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2162 port[dev] = possible_ports[i];
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002163 err = snd_es18xx_isa_probe1(dev, pdev);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002164 if (! err)
2165 return 0;
2166 }
2167 return err;
2168 }
2169}
2170
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002171#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002172static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2173 pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002174{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002175 return snd_es18xx_suspend(dev_get_drvdata(dev), state);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002176}
2177
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002178static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002179{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002180 return snd_es18xx_resume(dev_get_drvdata(dev));
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002181}
2182#endif
2183
Rene Herman83c51c02007-03-20 11:33:46 +01002184#define DEV_NAME "es18xx"
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002185
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002186static struct isa_driver snd_es18xx_isa_driver = {
2187 .match = snd_es18xx_isa_match,
2188 .probe = snd_es18xx_isa_probe,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002189#ifdef CONFIG_PM
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002190 .suspend = snd_es18xx_isa_suspend,
2191 .resume = snd_es18xx_isa_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002192#endif
2193 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01002194 .name = DEV_NAME
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002195 },
2196};
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -05002200static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2201 const struct pnp_device_id *id)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002202{
2203 static int dev;
2204 int err;
2205 struct snd_card *card;
2206
2207 if (pnp_device_is_isapnp(pdev))
2208 return -ENOENT; /* we have another procedure - card */
2209 for (; dev < SNDRV_CARDS; dev++) {
2210 if (enable[dev] && isapnp[dev])
2211 break;
2212 }
2213 if (dev >= SNDRV_CARDS)
2214 return -ENODEV;
2215
Takashi Iwai4323cc42014-01-29 13:03:56 +01002216 err = snd_es18xx_card_new(&pdev->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002217 if (err < 0)
2218 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002219 err = snd_audiodrive_pnp(dev, card->private_data, pdev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002220 if (err < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002221 return err;
Takashi Iwai45e04712021-06-08 16:04:44 +02002222 err = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002223 if (err < 0)
Ondrej Zary1c3985582006-07-31 12:51:57 +02002224 return err;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002225 pnp_set_drvdata(pdev, card);
2226 dev++;
Ondrej Zary1c3985582006-07-31 12:51:57 +02002227 return 0;
2228}
2229
Ondrej Zary1c3985582006-07-31 12:51:57 +02002230#ifdef CONFIG_PM
2231static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2232{
2233 return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2234}
2235static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2236{
2237 return snd_es18xx_resume(pnp_get_drvdata(pdev));
2238}
2239#endif
2240
2241static struct pnp_driver es18xx_pnp_driver = {
2242 .name = "es18xx-pnpbios",
2243 .id_table = snd_audiodrive_pnpbiosids,
2244 .probe = snd_audiodrive_pnp_detect,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002245#ifdef CONFIG_PM
2246 .suspend = snd_audiodrive_pnp_suspend,
2247 .resume = snd_audiodrive_pnp_resume,
2248#endif
2249};
2250
Bill Pemberton1bff2922012-12-06 12:35:21 -05002251static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2252 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002255 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int res;
2257
2258 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002259 if (enable[dev] && isapnp[dev])
2260 break;
2261 }
2262 if (dev >= SNDRV_CARDS)
2263 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Takashi Iwai4323cc42014-01-29 13:03:56 +01002265 res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01002266 if (res < 0)
2267 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002268
Takashi Iwai45e04712021-06-08 16:04:44 +02002269 res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid);
Takashi Iwai34d65992021-07-15 09:59:25 +02002270 if (res < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002271 return res;
Takashi Iwai45e04712021-06-08 16:04:44 +02002272 res = snd_audiodrive_probe(card, dev);
Takashi Iwai34d65992021-07-15 09:59:25 +02002273 if (res < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002274 return res;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002275
2276 pnp_set_card_drvdata(pcard, card);
2277 dev++;
2278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002281#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002282static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002283{
2284 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2285}
2286
Ondrej Zary1c3985582006-07-31 12:51:57 +02002287static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002288{
2289 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2290}
2291
2292#endif
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294static struct pnp_card_driver es18xx_pnpc_driver = {
2295 .flags = PNP_DRIVER_RES_DISABLE,
2296 .name = "es18xx",
2297 .id_table = snd_audiodrive_pnpids,
Ondrej Zary1c3985582006-07-31 12:51:57 +02002298 .probe = snd_audiodrive_pnpc_detect,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002299#ifdef CONFIG_PM
Ondrej Zary1c3985582006-07-31 12:51:57 +02002300 .suspend = snd_audiodrive_pnpc_suspend,
2301 .resume = snd_audiodrive_pnpc_resume,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304#endif /* CONFIG_PNP */
2305
2306static int __init alsa_card_es18xx_init(void)
2307{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002308 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002310 err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002311#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02002312 if (!err)
2313 isa_registered = 1;
2314
Ondrej Zary1c3985582006-07-31 12:51:57 +02002315 err = pnp_register_driver(&es18xx_pnp_driver);
2316 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01002317 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002318
Ondrej Zary1c3985582006-07-31 12:51:57 +02002319 err = pnp_register_card_driver(&es18xx_pnpc_driver);
2320 if (!err)
2321 pnpc_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02002322
2323 if (isa_registered || pnp_registered)
2324 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01002325#endif
Rene Herman609d7692007-05-15 11:42:56 +02002326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
2329static void __exit alsa_card_es18xx_exit(void)
2330{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002331#ifdef CONFIG_PNP
2332 if (pnpc_registered)
2333 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2334 if (pnp_registered)
2335 pnp_unregister_driver(&es18xx_pnp_driver);
Rene Herman609d7692007-05-15 11:42:56 +02002336 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002337#endif
Rene Herman609d7692007-05-15 11:42:56 +02002338 isa_unregister_driver(&snd_es18xx_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
2341module_init(alsa_card_es18xx_init)
2342module_exit(alsa_card_es18xx_exit)