Import 2.3.18pre1
[davej-history.git] / drivers / sound / sb_ess.c
blobba8fc0c7182661e496e8422b7110d4348b1c8e27
1 #undef FKS_LOGGING
2 #undef FKS_TEST
4 /*
5 * tabs should be 4 spaces, in vi(m): set tabstop=4
7 * TODO: consistency speed calculations!!
8 * cleanup!
9 * ????: Did I break MIDI support?
11 * History:
13 * Rolf Fokkens (Dec 20 1998): ES188x recording level support on a per
14 * fokkensr@vertis.nl input basis.
15 * (Dec 24 1998): Recognition of ES1788, ES1887, ES1888,
16 * ES1868, ES1869 and ES1878. Could be used for
17 * specific handling in the future. All except
18 * ES1887 and ES1888 and ES688 are handled like
19 * ES1688.
20 * (Dec 27 1998): RECLEV for all (?) ES1688+ chips. ES188x now
21 * have the "Dec 20" support + RECLEV
22 * (Jan 2 1999): Preparation for Full Duplex. This means
23 * Audio 2 is now used for playback when dma16
24 * is specified. The next step would be to use
25 * Audio 1 and Audio 2 at the same time.
26 * (Jan 9 1999): Put all ESS stuff into sb_ess.[ch], this
27 * includes both the ESS stuff that has been in
28 * sb_*[ch] before I touched it and the ESS support
29 * I added later
30 * (Jan 23 1999): Full Duplex seems to work. I wrote a small
31 * test proggy which works OK. Haven't found
32 * any applications to test it though. So why did
33 * I bother to create it anyway?? :) Just for
34 * fun.
35 * (May 2 1999): I tried to be too smart by "introducing"
36 * ess_calc_best_speed (). The idea was that two
37 * dividers could be used to setup a samplerate,
38 * ess_calc_best_speed () would choose the best.
39 * This works for playback, but results in
40 * recording problems for high samplerates. I
41 * fixed this by removing ess_calc_best_speed ()
42 * and just doing what the documentation says.
43 * Andy Sloane (June 4 1999): Stole some code from ALSA to fix the playback
44 * andy@guildsoftware.com speed on ES1869, ES1879, ES1887, and ES1888.
45 * 1879's were previously ignored by this driver;
46 * added (untested) support for those.
48 * This files contains ESS chip specifics. It's based on the existing ESS
49 * handling as it resided in sb_common.c, sb_mixer.c and sb_audio.c. This
50 * file adds features like:
51 * - Chip Identification (as shown in /proc/sound)
52 * - RECLEV support for ES1688 and later
53 * - 6 bits playback level support chips later than ES1688
54 * - Recording level support on a per-device basis for ES1887
55 * - Full-Duplex for ES1887 (under development)
57 * Full duplex is enabled by specifying dma16. While the normal dma must
58 * be one of 0, 1 or 3, dma16 can be one of 0, 1, 3 or 5. DMA 5 is a 16 bit
59 * DMA channel, while the others are 8 bit..
61 * ESS detection isn't full proof (yet). If it fails an additional module
62 * parameter esstype can be specified to be one of the following:
63 * -1, 0, 688, 1688, 1868, 1869, 1788, 1887, 1888
64 * -1 means: mimic 2.0 behaviour,
65 * 0 means: auto detect.
66 * others: explicitly specify chip
67 * -1 is default, cause auto detect still doesn't work.
71 * About the documentation
73 * I don't know if the chips all are OK, but the documentation is buggy. 'cause
74 * I don't have all the cips myself, there's a lot I cannot verify. I'll try to
75 * keep track of my latest insights about his here. If you have additional info,
76 * please enlighten me (fokkensr@vertis.nl)!
78 * I had the impression that ES1688 also has 6 bit master volume control. The
79 * documentation about ES1888 (rev C, october '95) claims that ES1888 has
80 * the following features ES1688 doesn't have:
81 * - 6 bit master volume
82 * - Full Duplex
83 * So ES1688 apparently doesn't have 6 bit master volume control, but the
84 * ES1688 does have RECLEV control. Makes me wonder: does ES688 have it too?
85 * Without RECLEV ES688 won't be much fun I guess.
87 * From the ES1888 (rev C, october '95) documentation I got the impression
88 * that registers 0x68 to 0x6e don't exist which means: no recording volume
89 * controls. To my surprise the ES888 documentation (1/14/96) claims that
90 * ES888 does have these record mixer registers, but that ES1888 doesn't have
91 * 0x69 and 0x6b. So the rest should be there.
93 * I'm trying to get ES1887 Full Duplex. Audio 2 is playback only, while Audio 2
94 * is both record and playback. I think I should use Audio 2 for all playback.
96 * The documentation is an adventure: it's close but not fully accurate. I
97 * found out that after a reset some registers are *NOT* reset, though the
98 * docs say the would be. Interresting ones are 0x7f, 0x7d and 0x7a. They are
99 * related to the Audio 2 channel. I also was suprised about the consequenses
100 * of writing 0x00 to 0x7f (which should be done by reset): The ES1887 moves
101 * into ES1888 mode. This means that it claims IRQ 11, which happens to be my
102 * ISDN adapter. Needless to say it no longer worked. I now understand why
103 * after rebooting 0x7f already was 0x05, the value of my choise: the BIOS
104 * did it.
106 * Oh, and this is another trap: in ES1887 docs mixer register 0x70 is decribed
107 * as if it's exactly the same as register 0xa1. This is *NOT* true. The
108 * description of 0x70 in ES1869 docs is accurate however.
109 * Well, the assumption about ES1869 was wrong: register 0x70 is very much
110 * like register 0xa1, except that bit 7 is allways 1, whatever you want
111 * it to be.
113 * When using audio 2 mixer register 0x72 seems te be meaningless. Only 0xa2
114 * has effect.
116 * Software reset not being able to reset all registers is great! Especially
117 * the fact that register 0x78 isn't reset is great when you wanna change back
118 * to single dma operation (simplex): audio 2 is still operation, and uses the
119 * same dma as audio 1: your ess changes into a funny echo machine.
121 * Received the new that ES1688 is detected as a ES1788. Did some thinking:
122 * the ES1887 detection scheme suggests in step 2 to try if bit 3 of register
123 * 0x64 can be changed. This is inaccurate, first I inverted the * check: "If
124 * can be modified, it's a 1688", which lead to a correct detection
125 * of my ES1887. It resulted however in bad detection of 1688 (reported by mail)
126 * and 1868 (if no PnP detection first): they result in a 1788 being detected.
127 * I don't have docs on 1688, but I do have docs on 1868: The documentation is
128 * probably inaccurate in the fact that I should check bit 2, not bit 3. This
129 * is what I do now.
133 * About recognition of ESS chips
135 * The distinction of ES688, ES1688, ES1788, ES1887 and ES1888 is described in
136 * a (preliminary ??) datasheet on ES1887. It's aim is to identify ES1887, but
137 * during detection the text claims that "this chip may be ..." when a step
138 * fails. This scheme is used to distinct between the above chips.
139 * It appears however that some PnP chips like ES1868 are recognized as ES1788
140 * by the ES1887 detection scheme. These PnP chips can be detected in another
141 * way however: ES1868, ES1869 and ES1878 can be recognized (full proof I think)
142 * by repeatedly reading mixer register 0x40. This is done by ess_identify in
143 * sb_common.c.
144 * This results in the following detection steps:
145 * - distinct between ES688 and ES1688+ (as always done in this driver)
146 * if ES688 we're ready
147 * - try to detect ES1868, ES1869 or ES1878
148 * if successful we're ready
149 * - try to detect ES1888, ES1887 or ES1788
150 * if successful we're ready
151 * - Dunno. Must be 1688. Will do in general
153 * About RECLEV support:
155 * The existing ES1688 support didn't take care of the ES1688+ recording
156 * levels very well. Whenever a device was selected (recmask) for recording
157 * it's recording level was loud, and it couldn't be changed. The fact that
158 * internal register 0xb4 could take care of RECLEV, didn't work meaning until
159 * it's value was restored every time the chip was reset; this reset the
160 * value of 0xb4 too. I guess that's what 4front also had (have?) trouble with.
162 * About ES1887 support:
164 * The ES1887 has separate registers to control the recording levels, for all
165 * inputs. The ES1887 specific software makes these levels the same as their
166 * corresponding playback levels, unless recmask says they aren't recorded. In
167 * the latter case the recording volumes are 0.
168 * Now recording levels of inputs can be controlled, by changing the playback
169 * levels. Futhermore several devices can be recorded together (which is not
170 * possible with the ES1688.
171 * Besides the separate recording level control for each input, the common
172 * recordig level can also be controlled by RECLEV as described above.
174 * Not only ES1887 have this recording mixer. I know the following from the
175 * documentation:
176 * ES688 no
177 * ES1688 no
178 * ES1868 no
179 * ES1869 yes
180 * ES1878 no
181 * ES1879 yes
182 * ES1888 no/yes Contradicting documentation; most recent: yes
183 * ES1946 yes This is a PCI chip; not handled by this driver
186 #include <linux/delay.h>
188 #include "sound_config.h"
189 #include "sb_mixer.h"
190 #include "sb.h"
192 #include "sb_ess.h"
194 #define ESSTYPE_LIKE20 -1 /* Mimic 2.0 behaviour */
195 #define ESSTYPE_DETECT 0 /* Mimic 2.0 behaviour */
197 int esstype = ESSTYPE_DETECT; /* module parameter in sb_card.c */
199 #define SUBMDL_ES1788 0x10 /* Subtype ES1788 for specific handling */
200 #define SUBMDL_ES1868 0x11 /* Subtype ES1868 for specific handling */
201 #define SUBMDL_ES1869 0x12 /* Subtype ES1869 for specific handling */
202 #define SUBMDL_ES1878 0x13 /* Subtype ES1878 for specific handling */
203 #define SUBMDL_ES1879 0x16 /* ES1879 was initially forgotten */
204 #define SUBMDL_ES1887 0x14 /* Subtype ES1887 for specific handling */
205 #define SUBMDL_ES1888 0x15 /* Subtype ES1888 for specific handling */
207 #define SB_CAP_ES18XX_RATE 0x100
209 #define ES1688_CLOCK1 795444 /* 128 - div */
210 #define ES1688_CLOCK2 397722 /* 256 - div */
211 #define ES18XX_CLOCK1 793800 /* 128 - div */
212 #define ES18XX_CLOCK2 768000 /* 256 - div */
214 #ifdef FKS_LOGGING
215 static void ess_show_mixerregs (sb_devc *devc);
216 #endif
217 static int ess_read (sb_devc * devc, unsigned char reg);
218 static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data);
219 static void ess_chgmixer
220 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val);
222 /****************************************************************************
224 * ESS audio *
226 ****************************************************************************/
228 struct ess_command {short cmd; short data;};
231 * Commands for initializing Audio 1 for input (record)
233 static struct ess_command ess_i08m[] = /* input 8 bit mono */
234 { {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
235 static struct ess_command ess_i16m[] = /* input 16 bit mono */
236 { {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
237 static struct ess_command ess_i08s[] = /* input 8 bit stereo */
238 { {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
239 static struct ess_command ess_i16s[] = /* input 16 bit stereo */
240 { {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
242 static struct ess_command *ess_inp_cmds[] =
243 { ess_i08m, ess_i16m, ess_i08s, ess_i16s };
247 * Commands for initializing Audio 1 for output (playback)
249 static struct ess_command ess_o08m[] = /* output 8 bit mono */
250 { {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
251 static struct ess_command ess_o16m[] = /* output 16 bit mono */
252 { {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
253 static struct ess_command ess_o08s[] = /* output 8 bit stereo */
254 { {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
255 static struct ess_command ess_o16s[] = /* output 16 bit stereo */
256 { {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
258 static struct ess_command *ess_out_cmds[] =
259 { ess_o08m, ess_o16m, ess_o08s, ess_o16s };
261 static void ess_exec_commands
262 (sb_devc *devc, struct ess_command *cmdtab[])
264 struct ess_command *cmd;
266 cmd = cmdtab [ ((devc->channels != 1) << 1) + (devc->bits != AFMT_U8) ];
268 while (cmd->cmd != -1) {
269 ess_write (devc, cmd->cmd, cmd->data);
270 cmd++;
274 static void ess_change
275 (sb_devc *devc, unsigned int reg, unsigned int mask, unsigned int val)
277 int value;
279 value = ess_read (devc, reg);
280 value = (value & ~mask) | (val & mask);
281 ess_write (devc, reg, value);
284 static void ess_set_output_parms
285 (int dev, unsigned long buf, int nr_bytes, int intrflag)
287 sb_devc *devc = audio_devs[dev]->devc;
289 if (devc->duplex) {
290 devc->trg_buf_16 = buf;
291 devc->trg_bytes_16 = nr_bytes;
292 devc->trg_intrflag_16 = intrflag;
293 devc->irq_mode_16 = IMODE_OUTPUT;
294 } else {
295 devc->trg_buf = buf;
296 devc->trg_bytes = nr_bytes;
297 devc->trg_intrflag = intrflag;
298 devc->irq_mode = IMODE_OUTPUT;
302 static void ess_set_input_parms
303 (int dev, unsigned long buf, int count, int intrflag)
305 sb_devc *devc = audio_devs[dev]->devc;
307 devc->trg_buf = buf;
308 devc->trg_bytes = count;
309 devc->trg_intrflag = intrflag;
310 devc->irq_mode = IMODE_INPUT;
313 static int ess_calc_div (int clock, int revert, int *speedp, int *diffp)
315 int divider;
316 int speed, diff;
317 int retval;
319 speed = *speedp;
320 divider = (clock + speed / 2) / speed;
321 retval = revert - divider;
322 if (retval > revert - 1) {
323 retval = revert - 1;
324 divider = revert - retval;
326 /* This line is suggested. Must be wrong I think
327 *speedp = (clock + divider / 2) / divider;
328 So I chose the next one */
330 *speedp = clock / divider;
331 diff = speed - *speedp;
332 if (diff < 0) diff =-diff;
333 *diffp = diff;
335 return retval;
338 static int ess_calc_best_speed
339 (int clock1, int rev1, int clock2, int rev2, int *divp, int *speedp)
341 int speed1 = *speedp, speed2 = *speedp;
342 int div1, div2;
343 int diff1, diff2;
344 int retval;
346 div1 = ess_calc_div (clock1, rev1, &speed1, &diff1);
347 div2 = ess_calc_div (clock2, rev2, &speed2, &diff2);
349 if (diff1 < diff2) {
350 *divp = div1;
351 *speedp = speed1;
352 retval = 1;
353 } else {
354 *divp = div2;
355 *speedp = speed2;
356 retval = 2;
359 return retval;
363 * Depending on the audiochannel ESS devices can
364 * have different clock settings. These are made consistent for duplex
365 * however.
366 * callers of ess_speed only do an audionum suggestion, which means
367 * input suggests 1, output suggests 2. This suggestion is only true
368 * however when doing duplex.
370 static void ess_common_speed (sb_devc *devc, int *speedp, int *divp)
372 int diff = 0, div;
374 if (devc->duplex) {
376 * The 0x80 is important for the first audio channel
378 div = 0x80 | ess_calc_div (795500, 128, speedp, &diff);
379 } else if(devc->caps & SB_CAP_ES18XX_RATE) {
380 ess_calc_best_speed(ES18XX_CLOCK1, 128, ES18XX_CLOCK2, 256,
381 &div, speedp);
382 } else {
383 if (*speedp > 22000) {
384 div = 0x80 | ess_calc_div (ES1688_CLOCK1, 256, speedp, &diff);
385 } else {
386 div = 0x00 | ess_calc_div (ES1688_CLOCK2, 128, speedp, &diff);
389 *divp = div;
392 static void ess_speed (sb_devc *devc, int audionum)
394 int speed;
395 int div, div2;
397 ess_common_speed (devc, &(devc->speed), &div);
399 #ifdef FKS_REG_LOGGING
400 printk (KERN_INFO "FKS: ess_speed (%d) b speed = %d, div=%x\n", audionum, devc->speed, div);
401 #endif
403 /* Set filter roll-off to 90% of speed/2 */
404 speed = (devc->speed * 9) / 20;
406 div2 = 256 - 7160000 / (speed * 82);
408 if (!devc->duplex) audionum = 1;
410 if (audionum == 1) {
411 /* Change behaviour of register A1 *
412 sb_chg_mixer(devc, 0x71, 0x20, 0x20)
413 * For ES1869 only??? */
414 ess_write (devc, 0xa1, div);
415 ess_write (devc, 0xa2, div2);
416 } else {
417 ess_setmixer (devc, 0x70, div);
419 * FKS: fascinating: 0x72 doesn't seem to work.
421 ess_write (devc, 0xa2, div2);
422 ess_setmixer (devc, 0x72, div2);
426 static int ess_audio_prepare_for_input(int dev, int bsize, int bcount)
428 sb_devc *devc = audio_devs[dev]->devc;
430 ess_speed(devc, 1);
432 sb_dsp_command(devc, DSP_CMD_SPKOFF);
434 ess_write (devc, 0xb8, 0x0e); /* Auto init DMA mode */
435 ess_change (devc, 0xa8, 0x03, 3 - devc->channels); /* Mono/stereo */
436 ess_write (devc, 0xb9, 2); /* Demand mode (4 bytes/DMA request) */
438 ess_exec_commands (devc, ess_inp_cmds);
440 ess_change (devc, 0xb1, 0xf0, 0x50);
441 ess_change (devc, 0xb2, 0xf0, 0x50);
443 devc->trigger_bits = 0;
444 return 0;
447 static int ess_audio_prepare_for_output_audio1 (int dev, int bsize, int bcount)
449 sb_devc *devc = audio_devs[dev]->devc;
451 sb_dsp_reset(devc);
452 ess_speed(devc, 1);
453 ess_write (devc, 0xb8, 4); /* Auto init DMA mode */
454 ess_change (devc, 0xa8, 0x03, 3 - devc->channels); /* Mono/stereo */
455 ess_write (devc, 0xb9, 2); /* Demand mode (4 bytes/request) */
457 ess_exec_commands (devc, ess_out_cmds);
459 ess_change (devc, 0xb1, 0xf0, 0x50); /* Enable DMA */
460 ess_change (devc, 0xb2, 0xf0, 0x50); /* Enable IRQ */
462 sb_dsp_command(devc, DSP_CMD_SPKON); /* There be sound! */
464 devc->trigger_bits = 0;
465 return 0;
468 static int ess_audio_prepare_for_output_audio2 (int dev, int bsize, int bcount)
470 sb_devc *devc = audio_devs[dev]->devc;
471 unsigned char bits;
473 /* FKS: qqq
474 sb_dsp_reset(devc);
478 * Auto-Initialize:
479 * DMA mode + demand mode (8 bytes/request, yes I want it all!)
480 * But leave 16-bit DMA bit untouched!
482 ess_chgmixer (devc, 0x78, 0xd0, 0xd0);
484 ess_speed(devc, 2);
486 /* bits 4:3 on ES1887 represent recording source. Keep them! */
487 bits = ess_getmixer (devc, 0x7a) & 0x18;
489 /* Set stereo/mono */
490 if (devc->channels != 1) bits |= 0x02;
492 /* Init DACs; UNSIGNED mode for 8 bit; SIGNED mode for 16 bit */
493 if (devc->bits != AFMT_U8) bits |= 0x05; /* 16 bit */
495 /* Enable DMA, IRQ will be shared (hopefully)*/
496 bits |= 0x60;
498 ess_setmixer (devc, 0x7a, bits);
500 ess_mixer_reload (devc, SOUND_MIXER_PCM); /* There be sound! */
502 devc->trigger_bits = 0;
503 return 0;
506 static int ess_audio_prepare_for_output(int dev, int bsize, int bcount)
508 sb_devc *devc = audio_devs[dev]->devc;
510 #ifdef FKS_REG_LOGGING
511 printk(KERN_INFO "ess_audio_prepare_for_output: dma_out=%d,dma_in=%d\n"
512 , audio_devs[dev]->dmap_out->dma, audio_devs[dev]->dmap_in->dma);
513 #endif
515 if (devc->duplex) {
516 return ess_audio_prepare_for_output_audio2 (dev, bsize, bcount);
517 } else {
518 return ess_audio_prepare_for_output_audio1 (dev, bsize, bcount);
522 static void ess_audio_halt_xfer(int dev)
524 unsigned long flags;
525 sb_devc *devc = audio_devs[dev]->devc;
527 save_flags(flags);
528 cli();
529 sb_dsp_reset(devc);
530 restore_flags(flags);
533 * Audio 2 may still be operational! Creates awful sounds!
535 if (devc->duplex) ess_chgmixer(devc, 0x78, 0x03, 0x00);
538 static void ess_audio_start_input
539 (int dev, unsigned long buf, int nr_bytes, int intrflag)
541 int count = nr_bytes;
542 sb_devc *devc = audio_devs[dev]->devc;
543 short c = -nr_bytes;
546 * Start a DMA input to the buffer pointed by dmaqtail
549 if (audio_devs[dev]->dmap_in->dma > 3) count >>= 1;
550 count--;
552 devc->irq_mode = IMODE_INPUT;
554 ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
555 ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
557 ess_change (devc, 0xb8, 0x0f, 0x0f); /* Go */
558 devc->intr_active = 1;
561 static void ess_audio_output_block_audio1
562 (int dev, unsigned long buf, int nr_bytes, int intrflag)
564 int count = nr_bytes;
565 sb_devc *devc = audio_devs[dev]->devc;
566 short c = -nr_bytes;
568 if (audio_devs[dev]->dmap_out->dma > 3)
569 count >>= 1;
570 count--;
572 devc->irq_mode = IMODE_OUTPUT;
574 ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
575 ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
577 ess_change (devc, 0xb8, 0x05, 0x05); /* Go */
578 devc->intr_active = 1;
581 static void ess_audio_output_block_audio2
582 (int dev, unsigned long buf, int nr_bytes, int intrflag)
584 int count = nr_bytes;
585 sb_devc *devc = audio_devs[dev]->devc;
586 short c = -nr_bytes;
588 if (audio_devs[dev]->dmap_out->dma > 3) count >>= 1;
589 count--;
591 ess_setmixer (devc, 0x74, (unsigned char) ((unsigned short) c & 0xff));
592 ess_setmixer (devc, 0x76, (unsigned char) (((unsigned short) c >> 8) & 0xff));
593 ess_chgmixer (devc, 0x78, 0x03, 0x03); /* Go */
595 devc->irq_mode_16 = IMODE_OUTPUT;
596 devc->intr_active_16 = 1;
599 static void ess_audio_output_block
600 (int dev, unsigned long buf, int nr_bytes, int intrflag)
602 sb_devc *devc = audio_devs[dev]->devc;
604 if (devc->duplex) {
605 ess_audio_output_block_audio2 (dev, buf, nr_bytes, intrflag);
606 } else {
607 ess_audio_output_block_audio1 (dev, buf, nr_bytes, intrflag);
612 * FKS: the if-statements for both bits and bits_16 are quite alike.
613 * Combine this...
615 static void ess_audio_trigger(int dev, int bits)
617 sb_devc *devc = audio_devs[dev]->devc;
619 int bits_16 = bits & devc->irq_mode_16;
620 bits &= devc->irq_mode;
622 if (!bits && !bits_16) {
623 /* FKS oh oh.... wrong?? for dma 16? */
624 sb_dsp_command(devc, 0xd0); /* Halt DMA */
627 if (bits) {
628 switch (devc->irq_mode)
630 case IMODE_INPUT:
631 ess_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
632 devc->trg_intrflag);
633 break;
635 case IMODE_OUTPUT:
636 ess_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
637 devc->trg_intrflag);
638 break;
642 if (bits_16) {
643 switch (devc->irq_mode_16) {
644 case IMODE_INPUT:
645 ess_audio_start_input(dev, devc->trg_buf_16, devc->trg_bytes_16,
646 devc->trg_intrflag_16);
647 break;
649 case IMODE_OUTPUT:
650 ess_audio_output_block(dev, devc->trg_buf_16, devc->trg_bytes_16,
651 devc->trg_intrflag_16);
652 break;
656 devc->trigger_bits = bits | bits_16;
659 static int ess_audio_set_speed(int dev, int speed)
661 sb_devc *devc = audio_devs[dev]->devc;
662 int minspeed, maxspeed, dummydiv;
664 if (speed > 0) {
665 minspeed = (devc->duplex ? 6215 : 5000 );
666 maxspeed = (devc->duplex ? 44100 : 48000);
667 if (speed < minspeed) speed = minspeed;
668 if (speed > maxspeed) speed = maxspeed;
670 ess_common_speed (devc, &speed, &dummydiv);
672 devc->speed = speed;
674 return devc->speed;
678 * FKS: This is a one-on-one copy of sb1_audio_set_bits
680 static unsigned int ess_audio_set_bits(int dev, unsigned int bits)
682 sb_devc *devc = audio_devs[dev]->devc;
684 if (bits != 0) {
685 if (bits == AFMT_U8 || bits == AFMT_S16_LE) {
686 devc->bits = bits;
687 } else {
688 devc->bits = AFMT_U8;
692 return devc->bits;
696 * FKS: This is a one-on-one copy of sbpro_audio_set_channels
697 * (*) Modified it!!
699 static short ess_audio_set_channels(int dev, short channels)
701 sb_devc *devc = audio_devs[dev]->devc;
703 if (channels == 1 || channels == 2) devc->channels = channels;
705 return devc->channels;
708 static struct audio_driver ess_audio_driver = /* ESS ES688/1688 */
710 sb_audio_open,
711 sb_audio_close,
712 ess_set_output_parms,
713 ess_set_input_parms,
714 NULL,
715 ess_audio_prepare_for_input,
716 ess_audio_prepare_for_output,
717 ess_audio_halt_xfer,
718 NULL, /* local_qlen */
719 NULL, /* copy_from_user */
720 NULL,
721 NULL,
722 ess_audio_trigger,
723 ess_audio_set_speed,
724 ess_audio_set_bits,
725 ess_audio_set_channels
729 * ess_audio_init must be called from sb_audio_init
731 struct audio_driver *ess_audio_init
732 (sb_devc *devc, int *audio_flags, int *format_mask)
734 *audio_flags = DMA_AUTOMODE;
735 *format_mask |= AFMT_S16_LE;
737 if (devc->duplex) {
738 int tmp_dma;
740 * sb_audio_init thinks dma8 is for playback and
741 * dma16 is for record. Not now! So swap them.
743 tmp_dma = devc->dma16;
744 devc->dma16 = devc->dma8;
745 devc->dma8 = tmp_dma;
747 *audio_flags |= DMA_DUPLEX;
750 return &ess_audio_driver;
753 /****************************************************************************
755 * ESS common *
757 ****************************************************************************/
758 static void ess_handle_channel
759 (char *channel, int dev, int intr_active, unsigned char flag, int irq_mode)
761 if (!intr_active || !flag) return;
762 #ifdef FKS_REG_LOGGING
763 printk(KERN_INFO "FKS: ess_handle_channel %s irq_mode=%d\n", channel, irq_mode);
764 #endif
765 switch (irq_mode) {
766 case IMODE_OUTPUT:
767 DMAbuf_outputintr (dev, 1);
768 break;
770 case IMODE_INPUT:
771 DMAbuf_inputintr (dev);
772 break;
774 case IMODE_INIT:
775 break;
777 default:
778 /* printk(KERN_WARN "ESS: Unexpected interrupt\n"); */
783 * FKS: TODO!!! Finish this!
785 * I think midi stuff uses uart401, without interrupts.
786 * So IMODE_MIDI isn't a value for devc->irq_mode.
788 void ess_intr (sb_devc *devc)
790 int status;
791 unsigned char src;
793 if (devc->submodel == SUBMDL_ES1887) {
794 src = ess_getmixer (devc, 0x7f) >> 4;
795 } else {
796 src = 0xff;
799 #ifdef FKS_REG_LOGGING
800 printk(KERN_INFO "FKS: sbintr src=%x\n",(int)src);
801 #endif
802 ess_handle_channel
803 ( "Audio 1"
804 , devc->dev, devc->intr_active , src & 0x01, devc->irq_mode );
805 ess_handle_channel
806 ( "Audio 2"
807 , devc->dev, devc->intr_active_16, src & 0x02, devc->irq_mode_16);
809 * Acknowledge interrupts
811 if (devc->submodel == SUBMDL_ES1887 && (src & 0x02)) {
812 ess_chgmixer (devc, 0x7a, 0x80, 0x00);
815 if (src & 0x01) {
816 status = inb(DSP_DATA_AVAIL);
820 static void ess_extended (sb_devc * devc)
822 /* Enable extended mode */
824 sb_dsp_command(devc, 0xc6);
827 static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data)
829 #ifdef FKS_REG_LOGGING
830 printk(KERN_INFO "FKS: write reg %x: %x\n", reg, data);
831 #endif
832 /* Write a byte to an extended mode register of ES1688 */
834 if (!sb_dsp_command(devc, reg))
835 return 0;
837 return sb_dsp_command(devc, data);
840 static int ess_read (sb_devc * devc, unsigned char reg)
842 /* Read a byte from an extended mode register of ES1688 */
844 /* Read register command */
845 if (!sb_dsp_command(devc, 0xc0)) return -1;
847 if (!sb_dsp_command(devc, reg )) return -1;
849 return sb_dsp_get_byte(devc);
852 int ess_dsp_reset(sb_devc * devc)
854 int loopc;
856 #ifdef FKS_REG_LOGGING
857 printk(KERN_INFO "FKS: ess_dsp_reset 1\n");
858 ess_show_mixerregs (devc);
859 #endif
861 DEB(printk("Entered ess_dsp_reset()\n"));
863 outb(3, DSP_RESET); /* Reset FIFO too */
865 udelay(10);
866 outb(0, DSP_RESET);
867 udelay(30);
869 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
871 if (inb(DSP_READ) != 0xAA) {
872 DDB(printk("sb: No response to RESET\n"));
873 return 0; /* Sorry */
875 ess_extended (devc);
877 DEB(printk("sb_dsp_reset() OK\n"));
879 #ifdef FKS_LOGGING
880 printk(KERN_INFO "FKS: dsp_reset 2\n");
881 ess_show_mixerregs (devc);
882 #endif
884 return 1;
887 static int ess_irq_bits (int irq)
889 switch (irq) {
890 case 2:
891 case 9:
892 return 0;
894 case 5:
895 return 1;
897 case 7:
898 return 2;
900 case 10:
901 return 3;
903 default:
904 printk(KERN_ERR "ESS1688: Invalid IRQ %d\n", irq);
905 return -1;
910 * Set IRQ configuration register for all ESS models
912 static int ess_common_set_irq_hw (sb_devc * devc)
914 int irq_bits;
916 if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return 0;
918 if (!ess_write (devc, 0xb1, 0x50 | (irq_bits << 2))) {
919 printk(KERN_ERR "ES1688: Failed to write to IRQ config register\n");
920 return 0;
922 return 1;
926 * I wanna use modern ES1887 mixer irq handling. Funny is the
927 * fact that my BIOS wants the same. But suppose someone's BIOS
928 * doesn't do this!
929 * This is independent of duplex. If there's a 1887 this will
930 * prevent it from going into 1888 mode.
932 static void ess_es1887_set_irq_hw (sb_devc * devc)
934 int irq_bits;
936 if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return;
938 ess_chgmixer (devc, 0x7f, 0x0f, 0x01 | ((irq_bits + 1) << 1));
941 static int ess_set_irq_hw (sb_devc * devc)
943 if (devc->submodel == SUBMDL_ES1887) ess_es1887_set_irq_hw (devc);
945 return ess_common_set_irq_hw (devc);
948 #ifdef FKS_TEST
951 * FKS_test:
952 * for ES1887: 00, 18, non wr bits: 0001 1000
953 * for ES1868: 00, b8, non wr bits: 1011 1000
954 * for ES1888: 00, f8, non wr bits: 1111 1000
955 * for ES1688: 00, f8, non wr bits: 1111 1000
956 * + ES968
959 static void FKS_test (sb_devc * devc)
961 int val1, val2;
962 val1 = ess_getmixer (devc, 0x64);
963 ess_setmixer (devc, 0x64, ~val1);
964 val2 = ess_getmixer (devc, 0x64) ^ ~val1;
965 ess_setmixer (devc, 0x64, val1);
966 val1 ^= ess_getmixer (devc, 0x64);
967 printk (KERN_INFO "FKS: FKS_test %02x, %02x\n", (val1 & 0x0ff), (val2 & 0x0ff));
969 #endif
971 static unsigned int ess_identify (sb_devc * devc)
973 unsigned int val;
974 unsigned long flags;
976 save_flags(flags);
977 cli();
978 outb(((unsigned char) (0x40 & 0xff)), MIXER_ADDR);
980 udelay(20);
981 val = inb(MIXER_DATA) << 8;
982 udelay(20);
983 val |= inb(MIXER_DATA);
984 udelay(20);
985 restore_flags(flags);
987 return val;
991 * ESS technology describes a detection scheme in their docs. It involves
992 * fiddling with the bits in certain mixer registers. ess_probe is supposed
993 * to help.
995 * FKS: tracing shows ess_probe writes wrong value to 0x64. Bit 3 reads 1, but
996 * should be written 0 only. Check this.
998 static int ess_probe (sb_devc * devc, int reg, int xorval)
1000 int val1, val2, val3;
1002 val1 = ess_getmixer (devc, reg);
1003 val2 = val1 ^ xorval;
1004 ess_setmixer (devc, reg, val2);
1005 val3 = ess_getmixer (devc, reg);
1006 ess_setmixer (devc, reg, val1);
1008 return (val2 == val3);
1011 int ess_init(sb_devc * devc, struct address_info *hw_config)
1013 unsigned char cfg;
1014 int ess_major = 0, ess_minor = 0;
1015 int i;
1016 static char name[100], modelname[10];
1019 * Try to detect ESS chips.
1022 sb_dsp_command(devc, 0xe7); /* Return identification */
1024 for (i = 1000; i; i--) {
1025 if (inb(DSP_DATA_AVAIL) & 0x80) {
1026 if (ess_major == 0) {
1027 ess_major = inb(DSP_READ);
1028 } else {
1029 ess_minor = inb(DSP_READ);
1030 break;
1035 if (ess_major == 0) return 0;
1037 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80) {
1038 sprintf(name, "ESS ES488 AudioDrive (rev %d)",
1039 ess_minor & 0x0f);
1040 hw_config->name = name;
1041 devc->model = MDL_SBPRO;
1042 return 1;
1046 * This the detection heuristic of ESS technology, though somewhat
1047 * changed to actually make it work.
1048 * This results in the following detection steps:
1049 * - distinct between ES688 and ES1688+ (as always done in this driver)
1050 * if ES688 we're ready
1051 * - try to detect ES1868, ES1869 or ES1878 (ess_identify)
1052 * if successful we're ready
1053 * - try to detect ES1888, ES1887 or ES1788 (aim: detect ES1887)
1054 * if successful we're ready
1055 * - Dunno. Must be 1688. Will do in general
1057 * This is the most BETA part of the software: Will the detection
1058 * always work?
1060 devc->model = MDL_ESS;
1061 devc->submodel = ess_minor & 0x0f;
1063 if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80) {
1064 char *chip = NULL;
1065 int submodel = -1;
1067 switch (esstype) {
1068 case ESSTYPE_DETECT:
1069 case ESSTYPE_LIKE20:
1070 break;
1071 case 688:
1072 submodel = 0x00;
1073 break;
1074 case 1688:
1075 submodel = 0x08;
1076 break;
1077 case 1868:
1078 submodel = SUBMDL_ES1868;
1079 break;
1080 case 1869:
1081 submodel = SUBMDL_ES1869;
1082 break;
1083 case 1788:
1084 submodel = SUBMDL_ES1788;
1085 break;
1086 case 1878:
1087 submodel = SUBMDL_ES1878;
1088 break;
1089 case 1879:
1090 submodel = SUBMDL_ES1879;
1091 break;
1092 case 1887:
1093 submodel = SUBMDL_ES1887;
1094 break;
1095 case 1888:
1096 submodel = SUBMDL_ES1888;
1097 break;
1098 default:
1099 printk (KERN_ERR "Invalid esstype=%d specified\n", esstype);
1100 return 0;
1102 if (submodel != -1) {
1103 devc->submodel = submodel;
1104 sprintf (modelname, "ES%d", esstype);
1105 chip = modelname;
1107 if (chip == NULL && (ess_minor & 0x0f) < 8) {
1108 chip = "ES688";
1110 #ifdef FKS_TEST
1111 FKS_test (devc);
1112 #endif
1114 * If Nothing detected yet, and we want 2.0 behaviour...
1115 * Then let's assume it's ES1688.
1117 if (chip == NULL && esstype == ESSTYPE_LIKE20) {
1118 chip = "ES1688";
1121 if (chip == NULL) {
1122 int type;
1124 type = ess_identify (devc);
1126 switch (type) {
1127 case 0x1868:
1128 chip = "ES1868";
1129 devc->submodel = SUBMDL_ES1868;
1130 break;
1131 case 0x1869:
1132 chip = "ES1869";
1133 devc->submodel = SUBMDL_ES1869;
1134 break;
1135 case 0x1878:
1136 chip = "ES1878";
1137 devc->submodel = SUBMDL_ES1878;
1138 break;
1139 case 0x1879:
1140 chip = "ES1879";
1141 devc->submodel = SUBMDL_ES1879;
1142 break;
1143 default:
1144 if ((type & 0x00ff) != ((type >> 8) & 0x00ff)) {
1145 printk ("ess_init: Unrecognized %04x\n", type);
1149 #if 0
1151 * this one failed:
1152 * the probing of bit 4 is another thought: from ES1788 and up, all
1153 * chips seem to have hardware volume control. Bit 4 is readonly to
1154 * check if a hardware volume interrupt has fired.
1155 * Cause ES688/ES1688 don't have this feature, bit 4 might be writeable
1156 * for these chips.
1158 if (chip == NULL && !ess_probe(devc, 0x64, (1 << 4))) {
1159 #endif
1161 * the probing of bit 2 is my idea. The ES1887 docs want me to probe
1162 * bit 3. This results in ES1688 being detected as ES1788.
1163 * Bit 2 is for "Enable HWV IRQE", but as ES(1)688 chips don't have
1164 * HardWare Volume, I think they don't have this IRQE.
1166 if (chip == NULL && ess_probe(devc, 0x64, (1 << 2))) {
1167 if (ess_probe (devc, 0x70, 0x7f)) {
1168 if (ess_probe (devc, 0x64, (1 << 5))) {
1169 chip = "ES1887";
1170 devc->submodel = SUBMDL_ES1887;
1171 } else {
1172 chip = "ES1888";
1173 devc->submodel = SUBMDL_ES1888;
1175 } else {
1176 chip = "ES1788";
1177 devc->submodel = SUBMDL_ES1788;
1180 if (chip == NULL) {
1181 chip = "ES1688";
1184 printk ( KERN_INFO "ESS chip %s %s%s\n"
1185 , chip
1186 , ( esstype == ESSTYPE_DETECT || esstype == ESSTYPE_LIKE20
1187 ? "detected"
1188 : "specified"
1190 , ( esstype == ESSTYPE_LIKE20
1191 ? " (kernel 2.0 compatible)"
1192 : ""
1196 sprintf(name,"ESS %s AudioDrive (rev %d)", chip, ess_minor & 0x0f);
1197 } else {
1198 strcpy(name, "Jazz16");
1201 /* AAS: info stolen from ALSA: these boards have different clocks */
1202 switch(devc->submodel) {
1203 /* APPARENTLY NOT 1869
1204 case SUBMDL_ES1869:
1206 case SUBMDL_ES1887:
1207 case SUBMDL_ES1888:
1208 devc->caps |= SB_CAP_ES18XX_RATE;
1209 break;
1212 /* AAS: info stolen from ALSA: these boards have different clocks */
1213 switch(devc->submodel) {
1214 case SUBMDL_ES1869:
1215 case SUBMDL_ES1887:
1216 case SUBMDL_ES1888:
1217 devc->caps |= SB_CAP_ES18XX_RATE;
1218 break;
1221 hw_config->name = name;
1222 /* FKS: sb_dsp_reset to enable extended mode???? */
1223 sb_dsp_reset(devc); /* Turn on extended mode */
1226 * Enable joystick and OPL3
1228 cfg = ess_getmixer (devc, 0x40);
1229 ess_setmixer (devc, 0x40, cfg | 0x03);
1230 if (devc->submodel >= 8) { /* ES1688 */
1231 devc->caps |= SB_NO_MIDI; /* ES1688 uses MPU401 MIDI mode */
1233 sb_dsp_reset (devc);
1236 * This is important! If it's not done, the IRQ probe in sb_dsp_init
1237 * may fail.
1239 return ess_set_irq_hw (devc);
1242 static int ess_set_dma_hw(sb_devc * devc)
1244 unsigned char cfg, dma_bits = 0, dma16_bits;
1245 int dma;
1247 #ifdef FKS_LOGGING
1248 printk(KERN_INFO "ess_set_dma_hw: dma8=%d,dma16=%d,dup=%d\n"
1249 , devc->dma8, devc->dma16, devc->duplex);
1250 #endif
1253 * FKS: It seems as if this duplex flag isn't set yet. Check it.
1255 dma = devc->dma8;
1257 if (dma > 3 || dma < 0 || dma == 2) {
1258 dma_bits = 0;
1259 printk(KERN_ERR "ESS1688: Invalid DMA8 %d\n", dma);
1260 return 0;
1261 } else {
1262 /* Extended mode DMA enable */
1263 cfg = 0x50;
1265 if (dma == 3) {
1266 dma_bits = 3;
1267 } else {
1268 dma_bits = dma + 1;
1272 if (!ess_write (devc, 0xb2, cfg | (dma_bits << 2))) {
1273 printk(KERN_ERR "ESS1688: Failed to write to DMA config register\n");
1274 return 0;
1277 if (devc->duplex) {
1278 dma = devc->dma16;
1279 dma16_bits = 0;
1281 if (dma >= 0) {
1282 switch (dma) {
1283 case 0:
1284 dma_bits = 0x04;
1285 break;
1286 case 1:
1287 dma_bits = 0x05;
1288 break;
1289 case 3:
1290 dma_bits = 0x06;
1291 break;
1292 case 5:
1293 dma_bits = 0x07;
1294 dma16_bits = 0x20;
1295 break;
1296 default:
1297 printk(KERN_ERR "ESS1887: Invalid DMA16 %d\n", dma);
1298 return 0;
1300 ess_chgmixer (devc, 0x78, 0x20, dma16_bits);
1301 ess_chgmixer (devc, 0x7d, 0x07, dma_bits);
1304 return 1;
1308 * This one is called from sb_dsp_init.
1310 * Return values:
1311 * 0: Failed
1312 * 1: Succeeded or doesn't apply (not SUBMDL_ES1887)
1314 int ess_dsp_init (sb_devc *devc, struct address_info *hw_config)
1317 * This for ES1887 to run Full Duplex. Actually ES1888
1318 * is allowed to do so too. I have no idea yet if this
1319 * will work for ES1888 however.
1321 * For SB16 having both dma8 and dma16 means enable
1322 * Full Duplex. Let's try this for ES1887 too
1325 if (devc->submodel == SUBMDL_ES1887) {
1326 if (hw_config->dma2 != -1) {
1327 devc->dma16 = hw_config->dma2;
1330 * devc->duplex initialization is put here, cause
1331 * ess_set_dma_hw needs it.
1333 if (devc->dma8 != devc->dma16 && devc->dma16 != -1) {
1334 devc->duplex = 1;
1337 if (!ess_set_dma_hw (devc)) {
1338 free_irq(devc->irq, devc);
1339 return 0;
1341 return 1;
1342 } else {
1343 return -1;
1347 /****************************************************************************
1349 * ESS mixer *
1351 ****************************************************************************/
1353 #define ES688_RECORDING_DEVICES \
1354 ( SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD )
1355 #define ES688_MIXER_DEVICES \
1356 ( SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE \
1357 | SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME \
1358 | SOUND_MASK_LINE2 | SOUND_MASK_SPEAKER )
1360 #define ES1688_RECORDING_DEVICES \
1361 ( ES688_RECORDING_DEVICES )
1362 #define ES1688_MIXER_DEVICES \
1363 ( ES688_MIXER_DEVICES | SOUND_MASK_RECLEV )
1365 #define ES1887_RECORDING_DEVICES \
1366 ( ES1688_RECORDING_DEVICES | SOUND_MASK_LINE2 | SOUND_MASK_SYNTH)
1367 #define ES1887_MIXER_DEVICES \
1368 ( ES1688_MIXER_DEVICES )
1371 * Mixer registers of ES1887
1373 * These registers specifically take care of recording levels. To make the
1374 * mapping from playback devices to recording devices every recording
1375 * devices = playback device + ES_REC_MIXER_RECDIFF
1377 #define ES_REC_MIXER_RECBASE (SOUND_MIXER_LINE3 + 1)
1378 #define ES_REC_MIXER_RECDIFF (ES_REC_MIXER_RECBASE - SOUND_MIXER_SYNTH)
1380 #define ES_REC_MIXER_RECSYNTH (SOUND_MIXER_SYNTH + ES_REC_MIXER_RECDIFF)
1381 #define ES_REC_MIXER_RECPCM (SOUND_MIXER_PCM + ES_REC_MIXER_RECDIFF)
1382 #define ES_REC_MIXER_RECSPEAKER (SOUND_MIXER_SPEAKER + ES_REC_MIXER_RECDIFF)
1383 #define ES_REC_MIXER_RECLINE (SOUND_MIXER_LINE + ES_REC_MIXER_RECDIFF)
1384 #define ES_REC_MIXER_RECMIC (SOUND_MIXER_MIC + ES_REC_MIXER_RECDIFF)
1385 #define ES_REC_MIXER_RECCD (SOUND_MIXER_CD + ES_REC_MIXER_RECDIFF)
1386 #define ES_REC_MIXER_RECIMIX (SOUND_MIXER_IMIX + ES_REC_MIXER_RECDIFF)
1387 #define ES_REC_MIXER_RECALTPCM (SOUND_MIXER_ALTPCM + ES_REC_MIXER_RECDIFF)
1388 #define ES_REC_MIXER_RECRECLEV (SOUND_MIXER_RECLEV + ES_REC_MIXER_RECDIFF)
1389 #define ES_REC_MIXER_RECIGAIN (SOUND_MIXER_IGAIN + ES_REC_MIXER_RECDIFF)
1390 #define ES_REC_MIXER_RECOGAIN (SOUND_MIXER_OGAIN + ES_REC_MIXER_RECDIFF)
1391 #define ES_REC_MIXER_RECLINE1 (SOUND_MIXER_LINE1 + ES_REC_MIXER_RECDIFF)
1392 #define ES_REC_MIXER_RECLINE2 (SOUND_MIXER_LINE2 + ES_REC_MIXER_RECDIFF)
1393 #define ES_REC_MIXER_RECLINE3 (SOUND_MIXER_LINE3 + ES_REC_MIXER_RECDIFF)
1395 static mixer_tab es688_mix = {
1396 MIX_ENT(SOUND_MIXER_VOLUME, 0x32, 7, 4, 0x32, 3, 4),
1397 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1398 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1399 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1400 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1401 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1402 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1403 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1404 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1405 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1406 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1407 MIX_ENT(SOUND_MIXER_RECLEV, 0x00, 0, 0, 0x00, 0, 0),
1408 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1409 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1410 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1411 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1412 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1416 * The ES1688 specifics... hopefully correct...
1417 * - 6 bit master volume
1418 * I was wrong, ES1888 docs say ES1688 didn't have it.
1419 * - RECLEV control
1420 * These may apply to ES688 too. I have no idea.
1422 static mixer_tab es1688_mix = {
1423 MIX_ENT(SOUND_MIXER_VOLUME, 0x32, 7, 4, 0x32, 3, 4),
1424 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1425 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1426 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1427 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1428 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1429 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1430 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1431 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1432 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1433 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1434 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1435 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1436 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1437 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1438 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1439 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1442 static mixer_tab es1688later_mix = {
1443 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1444 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1445 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1446 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1447 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1448 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1449 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1450 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1451 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1452 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1453 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1454 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1455 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1456 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1457 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1458 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1459 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1463 * This one is for all ESS chips with a record mixer.
1464 * It's not used (yet) however
1466 static mixer_tab es_rec_mix = {
1467 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1468 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1469 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1470 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1471 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1472 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1473 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1474 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1475 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1476 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1477 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1478 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1479 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1480 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1481 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1482 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1483 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0),
1484 MIX_ENT(ES_REC_MIXER_RECSYNTH, 0x6b, 7, 4, 0x6b, 3, 4),
1485 MIX_ENT(ES_REC_MIXER_RECPCM, 0x00, 0, 0, 0x00, 0, 0),
1486 MIX_ENT(ES_REC_MIXER_RECSPEAKER, 0x00, 0, 0, 0x00, 0, 0),
1487 MIX_ENT(ES_REC_MIXER_RECLINE, 0x6e, 7, 4, 0x6e, 3, 4),
1488 MIX_ENT(ES_REC_MIXER_RECMIC, 0x68, 7, 4, 0x68, 3, 4),
1489 MIX_ENT(ES_REC_MIXER_RECCD, 0x6a, 7, 4, 0x6a, 3, 4),
1490 MIX_ENT(ES_REC_MIXER_RECIMIX, 0x00, 0, 0, 0x00, 0, 0),
1491 MIX_ENT(ES_REC_MIXER_RECALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1492 MIX_ENT(ES_REC_MIXER_RECRECLEV, 0x00, 0, 0, 0x00, 0, 0),
1493 MIX_ENT(ES_REC_MIXER_RECIGAIN, 0x00, 0, 0, 0x00, 0, 0),
1494 MIX_ENT(ES_REC_MIXER_RECOGAIN, 0x00, 0, 0, 0x00, 0, 0),
1495 MIX_ENT(ES_REC_MIXER_RECLINE1, 0x00, 0, 0, 0x00, 0, 0),
1496 MIX_ENT(ES_REC_MIXER_RECLINE2, 0x6c, 7, 4, 0x6c, 3, 4),
1497 MIX_ENT(ES_REC_MIXER_RECLINE3, 0x00, 0, 0, 0x00, 0, 0)
1501 * This one is for ES1887. It's little different from es_rec_mix: it
1502 * has 0x7c for PCM playback level. This is because ES1887 uses
1503 * Audio 2 for playback.
1505 static mixer_tab es1887_mix = {
1506 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1507 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1508 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1509 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1510 MIX_ENT(SOUND_MIXER_PCM, 0x7c, 7, 4, 0x7c, 3, 4),
1511 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1512 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1513 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1514 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1515 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1516 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1517 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1518 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1519 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1520 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1521 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1522 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0),
1523 MIX_ENT(ES_REC_MIXER_RECSYNTH, 0x6b, 7, 4, 0x6b, 3, 4),
1524 MIX_ENT(ES_REC_MIXER_RECPCM, 0x00, 0, 0, 0x00, 0, 0),
1525 MIX_ENT(ES_REC_MIXER_RECSPEAKER, 0x00, 0, 0, 0x00, 0, 0),
1526 MIX_ENT(ES_REC_MIXER_RECLINE, 0x6e, 7, 4, 0x6e, 3, 4),
1527 MIX_ENT(ES_REC_MIXER_RECMIC, 0x68, 7, 4, 0x68, 3, 4),
1528 MIX_ENT(ES_REC_MIXER_RECCD, 0x6a, 7, 4, 0x6a, 3, 4),
1529 MIX_ENT(ES_REC_MIXER_RECIMIX, 0x00, 0, 0, 0x00, 0, 0),
1530 MIX_ENT(ES_REC_MIXER_RECALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1531 MIX_ENT(ES_REC_MIXER_RECRECLEV, 0x00, 0, 0, 0x00, 0, 0),
1532 MIX_ENT(ES_REC_MIXER_RECIGAIN, 0x00, 0, 0, 0x00, 0, 0),
1533 MIX_ENT(ES_REC_MIXER_RECOGAIN, 0x00, 0, 0, 0x00, 0, 0),
1534 MIX_ENT(ES_REC_MIXER_RECLINE1, 0x00, 0, 0, 0x00, 0, 0),
1535 MIX_ENT(ES_REC_MIXER_RECLINE2, 0x6c, 7, 4, 0x6c, 3, 4),
1536 MIX_ENT(ES_REC_MIXER_RECLINE3, 0x00, 0, 0, 0x00, 0, 0)
1539 static int ess_has_rec_mixer (int submodel)
1541 switch (submodel) {
1542 case SUBMDL_ES1887:
1543 return 1;
1544 default:
1545 return 0;
1549 #ifdef FKS_LOGGING
1550 static int ess_mixer_mon_regs[]
1551 = { 0x70, 0x71, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7d, 0x7f
1552 , 0xa1, 0xa2, 0xa4, 0xa5, 0xa8, 0xa9
1553 , 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb9
1554 , 0x00};
1556 static void ess_show_mixerregs (sb_devc *devc)
1558 int *mp = ess_mixer_mon_regs;
1560 return;
1562 while (*mp != 0) {
1563 printk (KERN_INFO "res (%x)=%x\n", *mp, (int)(ess_getmixer (devc, *mp)));
1564 mp++;
1567 #endif
1569 void ess_setmixer (sb_devc * devc, unsigned int port, unsigned int value)
1571 unsigned long flags;
1573 #ifdef FKS_LOGGING
1574 printk(KERN_INFO "FKS: write mixer %x: %x\n", port, value);
1575 #endif
1577 save_flags(flags);
1578 cli();
1579 if (port >= 0xa0) {
1580 ess_write (devc, port, value);
1581 } else {
1582 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1584 udelay(20);
1585 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
1586 udelay(20);
1588 restore_flags(flags);
1591 unsigned int ess_getmixer (sb_devc * devc, unsigned int port)
1593 unsigned int val;
1594 unsigned long flags;
1596 save_flags(flags);
1597 cli();
1599 if (port >= 0xa0) {
1600 val = ess_read (devc, port);
1601 } else {
1602 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1604 udelay(20);
1605 val = inb(MIXER_DATA);
1606 udelay(20);
1608 restore_flags(flags);
1610 return val;
1613 static void ess_chgmixer
1614 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
1616 int value;
1618 value = ess_getmixer (devc, reg);
1619 value = (value & ~mask) | (val & mask);
1620 ess_setmixer (devc, reg, value);
1624 * ess_mixer_init must be called from sb_mixer_init
1626 void ess_mixer_init (sb_devc * devc)
1628 devc->mixer_caps = SOUND_CAP_EXCL_INPUT;
1631 * Take care of ES1887 specifics...
1633 switch (devc->submodel) {
1634 case SUBMDL_ES1887:
1635 devc->supported_devices = ES1887_MIXER_DEVICES;
1636 devc->supported_rec_devices = ES1887_RECORDING_DEVICES;
1637 #ifdef FKS_LOGGING
1638 printk (KERN_INFO "FKS: ess_mixer_init dup = %d\n", devc->duplex);
1639 #endif
1640 if (devc->duplex) {
1641 devc->iomap = &es1887_mix;
1642 } else {
1643 devc->iomap = &es_rec_mix;
1645 break;
1646 default:
1647 if (devc->submodel < 8) {
1648 devc->supported_devices = ES688_MIXER_DEVICES;
1649 devc->supported_rec_devices = ES688_RECORDING_DEVICES;
1650 devc->iomap = &es688_mix;
1651 } else {
1653 * es1688 has 4 bits master vol.
1654 * later chips have 6 bits (?)
1656 devc->supported_devices = ES1688_MIXER_DEVICES;
1657 devc->supported_rec_devices = ES1688_RECORDING_DEVICES;
1658 if (devc->submodel < 0x10) {
1659 devc->iomap = &es1688_mix;
1660 } else {
1661 devc->iomap = &es1688later_mix;
1668 * Changing playback levels at an ESS chip with record mixer means having to
1669 * take care of recording levels of recorded inputs (devc->recmask) too!
1671 int ess_mixer_set(sb_devc *devc, int dev, int left, int right)
1673 if (ess_has_rec_mixer (devc->submodel) && (devc->recmask & (1 << dev))) {
1674 sb_common_mixer_set (devc, dev + ES_REC_MIXER_RECDIFF, left, right);
1676 return sb_common_mixer_set (devc, dev, left, right);
1680 * After a sb_dsp_reset extended register 0xb4 (RECLEV) is reset too. After
1681 * sb_dsp_reset RECLEV has to be restored. This is where ess_mixer_reload
1682 * helps.
1684 void ess_mixer_reload (sb_devc *devc, int dev)
1686 int left, right, value;
1688 value = devc->levels[dev];
1689 left = value & 0x000000ff;
1690 right = (value & 0x0000ff00) >> 8;
1692 sb_common_mixer_set(devc, dev, left, right);
1695 int es_rec_set_recmask(sb_devc * devc, int mask)
1697 int i, i_mask, cur_mask, diff_mask;
1698 int value, left, right;
1700 #ifdef FKS_LOGGING
1701 printk (KERN_INFO "FKS: es_rec_set_recmask mask = %x\n", mask);
1702 #endif
1704 * Changing the recmask on an ESS chip with recording mixer means:
1705 * (1) Find the differences
1706 * (2) For "turned-on" inputs: make the recording level the playback level
1707 * (3) For "turned-off" inputs: make the recording level zero
1709 cur_mask = devc->recmask;
1710 diff_mask = (cur_mask ^ mask);
1712 for (i = 0; i < 32; i++) {
1713 i_mask = (1 << i);
1714 if (diff_mask & i_mask) { /* Difference? (1) */
1715 if (mask & i_mask) { /* Turn it on (2) */
1716 value = devc->levels[i];
1717 left = value & 0x000000ff;
1718 right = (value & 0x0000ff00) >> 8;
1719 } else { /* Turn it off (3) */
1720 left = 0;
1721 left = 0;
1722 right = 0;
1724 sb_common_mixer_set(devc, i + ES_REC_MIXER_RECDIFF, left, right);
1727 return mask;
1730 int ess_set_recmask(sb_devc * devc, int *mask)
1732 /* This applies to ESS chips with record mixers only! */
1734 if (ess_has_rec_mixer (devc->submodel)) {
1735 *mask = es_rec_set_recmask (devc, *mask);
1736 return 1; /* Applied */
1737 } else {
1738 return 0; /* Not applied */
1743 * ess_mixer_reset must be called from sb_mixer_reset
1745 int ess_mixer_reset (sb_devc * devc)
1748 * Separate actions for ESS chips with a record mixer:
1750 if (ess_has_rec_mixer (devc->submodel)) {
1751 switch (devc->submodel) {
1752 case SUBMDL_ES1887:
1754 * Separate actions for ES1887:
1755 * Change registers 7a and 1c to make the record mixer the
1756 * actual recording source.
1758 ess_chgmixer(devc, 0x7a, 0x18, 0x08);
1759 ess_chgmixer(devc, 0x1c, 0x07, 0x07);
1760 break;
1763 * Call set_recmask for proper initialization
1765 devc->recmask = devc->supported_rec_devices;
1766 es_rec_set_recmask(devc, 0);
1767 devc->recmask = 0;
1769 return 1; /* We took care of recmask. */
1770 } else {
1771 return 0; /* We didn't take care; caller do it */
1775 /****************************************************************************
1777 * ESS midi *
1779 ****************************************************************************/
1782 * FKS: IRQ may be shared. Hm. And if so? Then What?
1784 int ess_midi_init(sb_devc * devc, struct address_info *hw_config)
1786 unsigned char cfg, tmp;
1788 cfg = ess_getmixer (devc, 0x40) & 0x03;
1790 if (devc->submodel < 8) {
1791 ess_setmixer (devc, 0x40, cfg | 0x03); /* Enable OPL3 & joystick */
1792 return 0; /* ES688 doesn't support MPU401 mode */
1794 tmp = (hw_config->io_base & 0x0f0) >> 4;
1796 if (tmp > 3) {
1797 ess_setmixer (devc, 0x40, cfg);
1798 return 0;
1800 cfg |= tmp << 3;
1802 tmp = 1; /* MPU enabled without interrupts */
1804 /* May be shared: if so the value is -ve */
1806 switch (abs(hw_config->irq)) {
1807 case 9:
1808 tmp = 0x4;
1809 break;
1810 case 5:
1811 tmp = 0x5;
1812 break;
1813 case 7:
1814 tmp = 0x6;
1815 break;
1816 case 10:
1817 tmp = 0x7;
1818 break;
1819 default:
1820 return 0;
1823 cfg |= tmp << 5;
1824 ess_setmixer (devc, 0x40, cfg | 0x03);
1826 return 1;