Linux 2.2.0
[davej-history.git] / drivers / sound / sb_ess.c
blobd1fdd4ccb007d0d5d14f6af7f1a99d412fdbeda0
1 /*
2 * Created: 9-Jan-1999
4 * TODO: consistency speed calculations!!
5 * ????: Did I break MIDI support?
7 * This files contains ESS chip specifics. It's based on the existing ESS
8 * handling as it resided in sb_common.c, sb_mixer.c and sb_audio.c. This
9 * file adds features like:
10 * - Chip Identification (as shown in /proc/sound)
11 * - RECLEV support for ES1688 and later
12 * - 6 bits playback level support chips later than ES1688
13 * - Recording level support on a per-device basis for ES1887
14 * - Full-Duplex for ES1887 (under development)
16 * Full duplex is enabled by specifying dma16. While the normal dma must
17 * be one of 0, 1 or 3, dma16 can be one of 0, 1, 3 or 5. DMA 5 is a 16 bit
18 * DMA channel, while the others are 8 bit..
20 * ESS detection isn't full proof (yet). If it fails an additional module
21 * parameter esstype can be specified to be one of the following:
22 * 688, 1688, 1868, 1869, 1788, 1887, 1888
24 * History:
26 * Rolf Fokkens (Dec 20 1998): ES188x recording level support on a per
27 * input basis.
28 * (Dec 24 1998): Recognition of ES1788, ES1887, ES1888,
29 * ES1868, ES1869 and ES1878. Could be used for
30 * specific handling in the future. All except
31 * ES1887 and ES1888 and ES688 are handled like
32 * ES1688.
33 * (Dec 27 1998): RECLEV for all (?) ES1688+ chips. ES188x now
34 * have the "Dec 20" support + RECLEV
35 * (jan 2 1999): Preparation for Full Duplex. This means
36 * Audio 2 is now used for playback when dma16
37 * is specified. The next step would be to use
38 * Audio 1 and Audio 2 at the same time.
41 #undef FKS_LOGGING
44 * About the documentation
46 * I don't know if the chips all are OK, but the documentation is buggy. 'cause
47 * I don't have all the cips myself, there's a lot I cannot verify. I'll try to
48 * keep track of my latest insights about his here. If you have additional info,
49 * please enlighten me (fokkensr@vertis.nl)!
51 * I had the impression that ES1688 also has 6 bit master volume control. The
52 * documentation about ES1888 (rev C, october '95) claims that ES1888 has
53 * the following features ES1688 doesn't have:
54 * - 6 bit master volume
55 * - Full Duplex
56 * So ES1688 apparently doesn't have 6 bit master volume control, but the
57 * ES1688 does have RECLEV control. Makes me wonder: does ES688 have it too?
58 * Without RECLEV ES688 won't be much fun I guess.
60 * From the ES1888 (rev C, october '95) documentation I got the impression
61 * that registers 0x68 to 0x6e don't exist which means: no recording volume
62 * controls. To my surprise the ES888 documentation (1/14/96) claims that
63 * ES888 does have these record mixer registers, but that ES1888 doesn't have
64 * 0x69 and 0x6b. So the rest should be there.
66 * I'm trying to get ES1887 Full Duplex. Audio 2 is playback only, while Audio 2
67 * is both record and playback. I think I should use Audio 2 for all playback.
69 * The documentation is an adventure: it's close but not fully accurate. I
70 * found out that after a reset some registers are *NOT* reset, though the
71 * docs say the would be. Interresting ones are 0x7f, 0x7d and 0x7a. They are
72 * related to the Audio 2 channel. I also was suprised about the consequenses
73 * of writing 0x00 to 0x7f (which should be done by reset): The ES1887 moves
74 * into ES1888 mode. This means that it claims IRQ 11, which happens to be my
75 * ISDN adapter. Needless to say it no longer worked. I now understand why
76 * after rebooting 0x7f already was 0x05, the value of my choise: the BIOS
77 * did it.
79 * Oh, and this is another trap: in ES1887 docs mixer register 0x70 is decribed
80 * as if it's exactly the same as register 0xa1. This is *NOT* true. The
81 * description of 0x70 in ES1869 docs is accurate however.
82 * Well, the assumption about ES1869 was wrong: register 0x70 is very much
83 * like register 0xa1, except that bit 7 is allways 1, whatever you want
84 * it to be.
86 * When using audio 2 mixer register 0x72 seems te be meaningless. Only 0xa2
87 * has effect.
89 * Software reset not being able to reset all registers is great! Especially
90 * the fact that register 0x78 isn't reset is great when you wanna change back
91 * to single dma operation (simplex): audio 2 is still operation, and uses the
92 * same dma as audio 1: your ess changes into a funny echo machine.
94 * Received the new that ES1688 is detected as a ES1788. Did some thinking:
95 * the ES1887 detection scheme suggests in step 2 to try if bit 3 of register
96 * 0x64 can be changed. This is inaccurate, first I inverted the * check: "If
97 * can be modified, it's a 1688", which lead to a correct detection
98 * of my ES1887. It resulted however in bad detection of 1688 (reported by mail)
99 * and 1868 (if no PnP detection first): they result in a 1788 being detected.
100 * I don't have docs on 1688, but I do have docs on 1868: The documentation is
101 * probably inaccurate in the fact that I should check bit 2, not bit 3. This
102 * is what I do now.
106 * About recognition of ESS chips
108 * The distinction of ES688, ES1688, ES1788, ES1887 and ES1888 is described in
109 * a (preliminary ??) datasheet on ES1887. It's aim is to identify ES1887, but
110 * during detection the text claims that "this chip may be ..." when a step
111 * fails. This scheme is used to distinct between the above chips.
112 * It appears however that some PnP chips like ES1868 are recognized as ES1788
113 * by the ES1887 detection scheme. These PnP chips can be detected in another
114 * way however: ES1868, ES1869 and ES1878 can be recognized (full proof I think)
115 * by repeatedly reading mixer register 0x40. This is done by ess_identify in
116 * sb_common.c.
117 * This results in the following detection steps:
118 * - distinct between ES688 and ES1688+ (as always done in this driver)
119 * if ES688 we're ready
120 * - try to detect ES1868, ES1869 or ES1878
121 * if successful we're ready
122 * - try to detect ES1888, ES1887 or ES1788
123 * if successful we're ready
124 * - Dunno. Must be 1688. Will do in general
126 * About RECLEV support:
128 * The existing ES1688 support didn't take care of the ES1688+ recording
129 * levels very well. Whenever a device was selected (recmask) for recording
130 * it's recording level was loud, and it couldn't be changed. The fact that
131 * internal register 0xb4 could take care of RECLEV, didn't work meaning until
132 * it's value was restored every time the chip was reset; this reset the
133 * value of 0xb4 too. I guess that's what 4front also had (have?) trouble with.
135 * About ES1887 support:
137 * The ES1887 has separate registers to control the recording levels, for all
138 * inputs. The ES1887 specific software makes these levels the same as their
139 * corresponding playback levels, unless recmask says they aren't recorded. In
140 * the latter case the recording volumes are 0.
141 * Now recording levels of inputs can be controlled, by changing the playback
142 * levels. Futhermore several devices can be recorded together (which is not
143 * possible with the ES1688.
144 * Besides the separate recording level control for each input, the common
145 * recordig level can also be controlled by RECLEV as described above.
147 * Not only ES1887 have this recording mixer. I know the following from the
148 * documentation:
149 * ES688 no
150 * ES1688 no
151 * ES1868 no
152 * ES1869 yes
153 * ES1878 no
154 * ES1879 yes
155 * ES1888 no/yes Contradicting documentation; most recent: yes
156 * ES1946 yes This is a PCI chip; not handled by this driver
159 #include "sound_config.h"
160 #include "sb_mixer.h"
161 #include "sb.h"
163 #include "sb_ess.h"
165 extern int esstype; /* module parameter in sb_card.c */
167 #ifdef FKS_LOGGING
168 static void ess_show_mixerregs (sb_devc *devc);
169 #endif
170 static int ess_read (sb_devc * devc, unsigned char reg);
171 static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data);
172 static void ess_chgmixer
173 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val);
175 /****************************************************************************
177 * ESS audio *
179 ****************************************************************************/
181 struct ess_command {short cmd; short data;};
184 * Commands for initializing Audio 1 for input (record)
186 static struct ess_command ess_i08m[] = /* input 8 bit mono */
187 { {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
188 static struct ess_command ess_i16m[] = /* input 16 bit mono */
189 { {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
190 static struct ess_command ess_i08s[] = /* input 8 bit stereo */
191 { {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
192 static struct ess_command ess_i16s[] = /* input 16 bit stereo */
193 { {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
195 static struct ess_command *ess_inp_cmds[] =
196 { ess_i08m, ess_i16m, ess_i08s, ess_i16s };
200 * Commands for initializing Audio 1 for output (playback)
202 static struct ess_command ess_o08m[] = /* output 8 bit mono */
203 { {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0xd0}, {-1, 0} };
204 static struct ess_command ess_o16m[] = /* output 16 bit mono */
205 { {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xf4}, {-1, 0} };
206 static struct ess_command ess_o08s[] = /* output 8 bit stereo */
207 { {0xb6, 0x80}, {0xb7, 0x51}, {0xb7, 0x98}, {-1, 0} };
208 static struct ess_command ess_o16s[] = /* output 16 bit stereo */
209 { {0xb6, 0x00}, {0xb7, 0x71}, {0xb7, 0xbc}, {-1, 0} };
211 static struct ess_command *ess_out_cmds[] =
212 { ess_o08m, ess_o16m, ess_o08s, ess_o16s };
214 static void ess_exec_commands
215 (sb_devc *devc, struct ess_command *cmdtab[])
217 struct ess_command *cmd;
219 cmd = cmdtab [ ((devc->channels != 1) << 1) + (devc->bits != AFMT_U8) ];
221 while (cmd->cmd != -1) {
222 ess_write (devc, cmd->cmd, cmd->data);
223 cmd++;
227 static void ess_change
228 (sb_devc *devc, unsigned int reg, unsigned int mask, unsigned int val)
230 int value;
232 value = ess_read (devc, reg);
233 value = (value & ~mask) | (val & mask);
234 ess_write (devc, reg, value);
237 static void ess_set_output_parms
238 (int dev, unsigned long buf, int nr_bytes, int intrflag)
240 sb_devc *devc = audio_devs[dev]->devc;
242 if (devc->duplex) {
243 devc->trg_buf_16 = buf;
244 devc->trg_bytes_16 = nr_bytes;
245 devc->trg_intrflag_16 = intrflag;
246 devc->irq_mode_16 = IMODE_OUTPUT;
247 } else {
248 devc->trg_buf = buf;
249 devc->trg_bytes = nr_bytes;
250 devc->trg_intrflag = intrflag;
251 devc->irq_mode = IMODE_OUTPUT;
255 static void ess_set_input_parms
256 (int dev, unsigned long buf, int count, int intrflag)
258 sb_devc *devc = audio_devs[dev]->devc;
260 devc->trg_buf = buf;
261 devc->trg_bytes = count;
262 devc->trg_intrflag = intrflag;
263 devc->irq_mode = IMODE_INPUT;
266 static int ess_calc_div (int clock, int revert, int *speedp, int *diffp)
268 int divider;
269 int speed, diff;
270 int retval;
272 speed = *speedp;
273 divider = (clock + speed / 2) / speed;
274 retval = revert - divider;
275 if (retval > revert - 1) {
276 retval = revert - 1;
277 divider = revert - retval;
279 /* This line is suggested. Must be wrong I think
280 *speedp = (clock + divider / 2) / divider;
281 So I chose the next one */
283 *speedp = clock / divider;
284 diff = speed - *speedp;
285 if (diff < 0) diff =-diff;
286 *diffp = diff;
288 return retval;
291 static int ess_calc_best_speed
292 (int clock1, int rev1, int clock2, int rev2, int *divp, int *speedp)
294 int speed1 = *speedp, speed2 = *speedp;
295 int div1, div2;
296 int diff1, diff2;
297 int retval;
299 div1 = ess_calc_div (clock1, rev1, &speed1, &diff1);
300 div2 = ess_calc_div (clock2, rev2, &speed2, &diff2);
302 if (diff1 < diff2) {
303 *divp = div1;
304 *speedp = speed1;
305 retval = 1;
306 } else {
307 *divp = div2;
308 *speedp = speed2;
309 retval = 2;
312 return retval;
316 * Depending on the audiochannel ESS devices can
317 * have different clock settings. These are made consistent for duplex
318 * however.
319 * callers of ess_speed only do an audionum suggestion, which means
320 * input suggests 1, output suggests 2. This suggestion is only true
321 * however when doing duplex.
323 static void ess_common_speed (sb_devc *devc, int *speedp, int *divp)
325 int diff = 0, div, choice;
327 if (devc->duplex) {
329 * The 0x80 is important for the first audio channel
331 div = 0x80 | ess_calc_div (795500, 128, speedp, &diff);
332 } else {
333 choice = ess_calc_best_speed (397700, 128, 795500, 256, &div, speedp);
334 if (choice == 2) div |= 0x80;
336 *divp = div;
339 static void ess_speed (sb_devc *devc, int audionum)
341 int speed;
342 int div, div2;
344 ess_common_speed (devc, &(devc->speed), &div);
346 #ifdef FKS_REG_LOGGING
347 printk (KERN_INFO "FKS: ess_speed (%d) b speed = %d, div=%x\n", audionum, devc->speed, div);
348 #endif
350 /* Set filter roll-off to 90% of speed/2 */
351 speed = (devc->speed * 9) / 20;
353 div2 = 256 - 7160000 / (speed * 82);
355 if (!devc->duplex) audionum = 1;
357 if (audionum == 1) {
358 /* Change behaviour of register A1 *
359 sb_chg_mixer(devc, 0x71, 0x20, 0x20)
360 * For ES1869 only??? */
361 ess_write (devc, 0xa1, div);
362 ess_write (devc, 0xa2, div2);
363 } else {
364 ess_setmixer (devc, 0x70, div);
366 * FKS: fascinating: 0x72 doesn't seem to work.
368 ess_write (devc, 0xa2, div2);
369 ess_setmixer (devc, 0x72, div2);
373 #if 0
374 static void ess_speed(sb_devc * devc)
376 int divider;
377 unsigned char bits = 0;
378 int speed = devc->speed;
380 if (speed < 4000)
381 speed = 4000;
382 else if (speed > 48000)
383 speed = 48000;
385 if (speed > 22000)
387 bits = 0x80;
388 divider = 256 - (795500 + speed / 2) / speed;
390 else
392 divider = 128 - (397700 + speed / 2) / speed;
395 bits |= (unsigned char) divider;
397 ess_write (devc, 0xa1, bits);
400 * Set filter divider register
403 speed = (speed * 9) / 20; /* Set filter roll-off to 90% of speed/2 */
404 divider = 256 - 7160000 / (speed * 82);
406 ess_write (devc, 0xa2, divider);
408 return;
410 #endif
412 static int ess_audio_prepare_for_input(int dev, int bsize, int bcount)
414 sb_devc *devc = audio_devs[dev]->devc;
416 ess_speed(devc, 1);
418 sb_dsp_command(devc, DSP_CMD_SPKOFF);
420 ess_write (devc, 0xb8, 0x0e); /* Auto init DMA mode */
421 ess_change (devc, 0xa8, 0x03, 3 - devc->channels); /* Mono/stereo */
422 ess_write (devc, 0xb9, 2); /* Demand mode (4 bytes/DMA request) */
424 ess_exec_commands (devc, ess_inp_cmds);
426 ess_change (devc, 0xb1, 0xf0, 0x50);
427 ess_change (devc, 0xb2, 0xf0, 0x50);
429 devc->trigger_bits = 0;
430 return 0;
433 static int ess_audio_prepare_for_output_audio1 (int dev, int bsize, int bcount)
435 sb_devc *devc = audio_devs[dev]->devc;
437 sb_dsp_reset(devc);
438 ess_speed(devc, 1);
439 ess_write (devc, 0xb8, 4); /* Auto init DMA mode */
440 ess_change (devc, 0xa8, 0x03, 3 - devc->channels); /* Mono/stereo */
441 ess_write (devc, 0xb9, 2); /* Demand mode (4 bytes/request) */
443 ess_exec_commands (devc, ess_out_cmds);
445 ess_change (devc, 0xb1, 0xf0, 0x50); /* Enable DMA */
446 ess_change (devc, 0xb2, 0xf0, 0x50); /* Enable IRQ */
448 sb_dsp_command(devc, DSP_CMD_SPKON); /* There be sound! */
450 devc->trigger_bits = 0;
451 return 0;
454 static int ess_audio_prepare_for_output_audio2 (int dev, int bsize, int bcount)
456 sb_devc *devc = audio_devs[dev]->devc;
457 unsigned char bits;
459 /* FKS: qqq
460 sb_dsp_reset(devc);
464 * Auto-Initialize:
465 * DMA mode + demand mode (8 bytes/request, yes I want it all!)
466 * But leave 16-bit DMA bit untouched!
468 ess_chgmixer (devc, 0x78, 0xd0, 0xd0);
470 ess_speed(devc, 2);
472 /* bits 4:3 on ES1887 represent recording source. Keep them! */
473 bits = ess_getmixer (devc, 0x7a) & 0x18;
475 /* Set stereo/mono */
476 if (devc->channels != 1) bits |= 0x02;
478 /* Init DACs; UNSIGNED mode for 8 bit; SIGNED mode for 16 bit */
479 if (devc->bits != AFMT_U8) bits |= 0x05; /* 16 bit */
481 /* Enable DMA, IRQ will be shared (hopefully)*/
482 bits |= 0x60;
484 ess_setmixer (devc, 0x7a, bits);
486 ess_mixer_reload (devc, SOUND_MIXER_PCM); /* There be sound! */
488 devc->trigger_bits = 0;
489 return 0;
492 static int ess_audio_prepare_for_output(int dev, int bsize, int bcount)
494 sb_devc *devc = audio_devs[dev]->devc;
496 #ifdef FKS_REG_LOGGING
497 printk(KERN_INFO "ess_audio_prepare_for_output: dma_out=%d,dma_in=%d\n"
498 , audio_devs[dev]->dmap_out->dma, audio_devs[dev]->dmap_in->dma);
499 #endif
501 if (devc->duplex) {
502 return ess_audio_prepare_for_output_audio2 (dev, bsize, bcount);
503 } else {
504 return ess_audio_prepare_for_output_audio1 (dev, bsize, bcount);
508 static void ess_audio_halt_xfer(int dev)
510 unsigned long flags;
511 sb_devc *devc = audio_devs[dev]->devc;
513 save_flags(flags);
514 cli();
515 sb_dsp_reset(devc);
516 restore_flags(flags);
519 * Audio 2 may still be operational! Creates awful sounds!
521 if (devc->duplex) ess_chgmixer(devc, 0x78, 0x03, 0x00);
524 static void ess_audio_start_input
525 (int dev, unsigned long buf, int nr_bytes, int intrflag)
527 int count = nr_bytes;
528 sb_devc *devc = audio_devs[dev]->devc;
529 short c = -nr_bytes;
532 * Start a DMA input to the buffer pointed by dmaqtail
535 if (audio_devs[dev]->dmap_in->dma > 3) count >>= 1;
536 count--;
538 devc->irq_mode = IMODE_INPUT;
540 ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
541 ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
543 ess_change (devc, 0xb8, 0x0f, 0x0f); /* Go */
544 devc->intr_active = 1;
547 static void ess_audio_output_block_audio1
548 (int dev, unsigned long buf, int nr_bytes, int intrflag)
550 int count = nr_bytes;
551 sb_devc *devc = audio_devs[dev]->devc;
552 short c = -nr_bytes;
554 if (audio_devs[dev]->dmap_out->dma > 3)
555 count >>= 1;
556 count--;
558 devc->irq_mode = IMODE_OUTPUT;
560 ess_write (devc, 0xa4, (unsigned char) ((unsigned short) c & 0xff));
561 ess_write (devc, 0xa5, (unsigned char) (((unsigned short) c >> 8) & 0xff));
563 ess_change (devc, 0xb8, 0x05, 0x05); /* Go */
564 devc->intr_active = 1;
567 static void ess_audio_output_block_audio2
568 (int dev, unsigned long buf, int nr_bytes, int intrflag)
570 int count = nr_bytes;
571 sb_devc *devc = audio_devs[dev]->devc;
572 short c = -nr_bytes;
574 if (audio_devs[dev]->dmap_out->dma > 3) count >>= 1;
575 count--;
577 ess_setmixer (devc, 0x74, (unsigned char) ((unsigned short) c & 0xff));
578 ess_setmixer (devc, 0x76, (unsigned char) (((unsigned short) c >> 8) & 0xff));
579 ess_chgmixer (devc, 0x78, 0x03, 0x03); /* Go */
581 devc->irq_mode_16 = IMODE_OUTPUT;
582 devc->intr_active_16 = 1;
585 static void ess_audio_output_block
586 (int dev, unsigned long buf, int nr_bytes, int intrflag)
588 sb_devc *devc = audio_devs[dev]->devc;
590 if (devc->duplex) {
591 ess_audio_output_block_audio2 (dev, buf, nr_bytes, intrflag);
592 } else {
593 ess_audio_output_block_audio1 (dev, buf, nr_bytes, intrflag);
598 * FKS: the if-statements for both bits and bits_16 are quite alike.
599 * Combine this...
601 static void ess_audio_trigger(int dev, int bits)
603 sb_devc *devc = audio_devs[dev]->devc;
605 int bits_16 = bits & devc->irq_mode_16;
606 bits &= devc->irq_mode;
608 if (!bits && !bits_16) {
609 /* FKS oh oh.... wrong?? for dma 16? */
610 sb_dsp_command(devc, 0xd0); /* Halt DMA */
613 if (bits) {
614 switch (devc->irq_mode)
616 case IMODE_INPUT:
617 ess_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
618 devc->trg_intrflag);
619 break;
621 case IMODE_OUTPUT:
622 ess_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
623 devc->trg_intrflag);
624 break;
628 if (bits_16) {
629 switch (devc->irq_mode_16) {
630 case IMODE_INPUT:
631 ess_audio_start_input(dev, devc->trg_buf_16, devc->trg_bytes_16,
632 devc->trg_intrflag_16);
633 break;
635 case IMODE_OUTPUT:
636 ess_audio_output_block(dev, devc->trg_buf_16, devc->trg_bytes_16,
637 devc->trg_intrflag_16);
638 break;
642 devc->trigger_bits = bits | bits_16;
645 static int ess_audio_set_speed(int dev, int speed)
647 sb_devc *devc = audio_devs[dev]->devc;
648 int minspeed, maxspeed, dummydiv;
650 if (speed > 0) {
651 minspeed = (devc->duplex ? 6215 : 5000 );
652 maxspeed = (devc->duplex ? 44100 : 48000);
653 if (speed < minspeed) speed = minspeed;
654 if (speed > maxspeed) speed = maxspeed;
656 ess_common_speed (devc, &speed, &dummydiv);
658 devc->speed = speed;
660 return devc->speed;
664 * FKS: This is a one-on-one copy of sb1_audio_set_bits
666 static unsigned int ess_audio_set_bits(int dev, unsigned int bits)
668 sb_devc *devc = audio_devs[dev]->devc;
670 if (bits != 0) {
671 if (bits == AFMT_U8 || bits == AFMT_S16_LE) {
672 devc->bits = bits;
673 } else {
674 devc->bits = AFMT_U8;
678 return devc->bits;
682 * FKS: This is a one-on-one copy of sbpro_audio_set_channels
683 * (*) Modified it!!
685 static short ess_audio_set_channels(int dev, short channels)
687 sb_devc *devc = audio_devs[dev]->devc;
689 if (channels == 1 || channels == 2) devc->channels = channels;
691 return devc->channels;
694 static struct audio_driver ess_audio_driver = /* ESS ES688/1688 */
696 sb_audio_open,
697 sb_audio_close,
698 ess_set_output_parms,
699 ess_set_input_parms,
700 NULL,
701 ess_audio_prepare_for_input,
702 ess_audio_prepare_for_output,
703 ess_audio_halt_xfer,
704 NULL, /* local_qlen */
705 NULL, /* copy_from_user */
706 NULL,
707 NULL,
708 ess_audio_trigger,
709 ess_audio_set_speed,
710 ess_audio_set_bits,
711 ess_audio_set_channels
715 * ess_audio_init must be called from sb_audio_init
717 struct audio_driver *ess_audio_init
718 (sb_devc *devc, int *audio_flags, int *format_mask)
720 *audio_flags = DMA_AUTOMODE;
721 *format_mask |= AFMT_S16_LE;
723 if (devc->duplex) {
724 int tmp_dma;
726 * sb_audio_init thinks dma8 is for playback and
727 * dma16 is for record. Not now! So swap them.
729 tmp_dma = devc->dma16;
730 devc->dma16 = devc->dma8;
731 devc->dma8 = tmp_dma;
733 *audio_flags |= DMA_DUPLEX;
736 return &ess_audio_driver;
739 /****************************************************************************
741 * ESS common *
743 ****************************************************************************/
744 static void ess_handle_channel
745 (char *channel, int dev, int intr_active, unsigned char flag, int irq_mode)
747 if (!intr_active || !flag) return;
748 #ifdef FKS_REG_LOGGING
749 printk(KERN_INFO "FKS: ess_handle_channel %s irq_mode=%d\n", channel, irq_mode);
750 #endif
751 switch (irq_mode) {
752 case IMODE_OUTPUT:
753 DMAbuf_outputintr (dev, 1);
754 break;
756 case IMODE_INPUT:
757 DMAbuf_inputintr (dev);
758 break;
760 case IMODE_INIT:
761 break;
763 default:
764 /* printk(KERN_WARN "ESS: Unexpected interrupt\n"); */
769 * FKS: TODO!!! Finish this!
771 * I think midi stuff uses uart401, without interrupts.
772 * So IMODE_MIDI isn't a value for devc->irq_mode.
774 void ess_intr (sb_devc *devc)
776 int status;
777 unsigned char src;
779 if (devc->submodel == SUBMDL_ES1887) {
780 src = ess_getmixer (devc, 0x7f) >> 4;
781 } else {
782 src = 0xff;
785 #ifdef FKS_REG_LOGGING
786 printk(KERN_INFO "FKS: sbintr src=%x\n",(int)src);
787 #endif
788 ess_handle_channel
789 ( "Audio 1"
790 , devc->dev, devc->intr_active , src & 0x01, devc->irq_mode );
791 ess_handle_channel
792 ( "Audio 2"
793 , devc->dev, devc->intr_active_16, src & 0x02, devc->irq_mode_16);
795 * Acknowledge interrupts
797 if (devc->submodel == SUBMDL_ES1887 && (src & 0x02)) {
798 ess_chgmixer (devc, 0x7a, 0x80, 0x00);
801 if (src & 0x01) {
802 status = inb(DSP_DATA_AVAIL);
806 static void ess_extended (sb_devc * devc)
808 /* Enable extended mode */
810 sb_dsp_command(devc, 0xc6);
813 static int ess_write (sb_devc * devc, unsigned char reg, unsigned char data)
815 #ifdef FKS_REG_LOGGING
816 printk(KERN_INFO "FKS: write reg %x: %x\n", reg, data);
817 #endif
818 /* Write a byte to an extended mode register of ES1688 */
820 if (!sb_dsp_command(devc, reg))
821 return 0;
823 return sb_dsp_command(devc, data);
826 static int ess_read (sb_devc * devc, unsigned char reg)
828 /* Read a byte from an extended mode register of ES1688 */
830 /* Read register command */
831 if (!sb_dsp_command(devc, 0xc0)) return -1;
833 if (!sb_dsp_command(devc, reg )) return -1;
835 return sb_dsp_get_byte(devc);
838 int ess_dsp_reset(sb_devc * devc)
840 int loopc;
842 #ifdef FKS_REG_LOGGING
843 printk(KERN_INFO "FKS: ess_dsp_reset 1\n");
844 ess_show_mixerregs (devc);
845 #endif
847 DEB(printk("Entered ess_dsp_reset()\n"));
849 outb(3, DSP_RESET); /* Reset FIFO too */
851 udelay(10);
852 outb(0, DSP_RESET);
853 udelay(30);
855 for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
857 if (inb(DSP_READ) != 0xAA) {
858 DDB(printk("sb: No response to RESET\n"));
859 return 0; /* Sorry */
861 ess_extended (devc);
863 DEB(printk("sb_dsp_reset() OK\n"));
865 #ifdef FKS_LOGGING
866 printk(KERN_INFO "FKS: dsp_reset 2\n");
867 ess_show_mixerregs (devc);
868 #endif
870 return 1;
873 static int ess_irq_bits (int irq)
875 switch (irq) {
876 case 2:
877 case 9:
878 return 0;
880 case 5:
881 return 1;
883 case 7:
884 return 2;
886 case 10:
887 return 3;
889 default:
890 printk(KERN_ERR "ESS1688: Invalid IRQ %d\n", irq);
891 return -1;
896 * Set IRQ configuration register for all ESS models
898 static int ess_common_set_irq_hw (sb_devc * devc)
900 int irq_bits;
902 if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return 0;
904 if (!ess_write (devc, 0xb1, 0x50 | (irq_bits << 2))) {
905 printk(KERN_ERR "ES1688: Failed to write to IRQ config register\n");
906 return 0;
908 return 1;
912 * I wanna use modern ES1887 mixer irq handling. Funny is the
913 * fact that my BIOS wants the same. But suppose someone's BIOS
914 * doesn't do this!
915 * This is independent of duplex. If there's a 1887 this will
916 * prevent it from going into 1888 mode.
918 static void ess_es1887_set_irq_hw (sb_devc * devc)
920 int irq_bits;
922 if ((irq_bits = ess_irq_bits (devc->irq)) == -1) return;
924 ess_chgmixer (devc, 0x7f, 0x0f, 0x01 | ((irq_bits + 1) << 1));
927 static int ess_set_irq_hw (sb_devc * devc)
929 if (devc->submodel == SUBMDL_ES1887) ess_es1887_set_irq_hw (devc);
931 return ess_common_set_irq_hw (devc);
934 static unsigned int ess_identify (sb_devc * devc)
936 unsigned int val;
937 unsigned long flags;
939 save_flags(flags);
940 cli();
941 outb(((unsigned char) (0x40 & 0xff)), MIXER_ADDR);
943 udelay(20);
944 val = inb(MIXER_DATA) << 8;
945 udelay(20);
946 val |= inb(MIXER_DATA);
947 udelay(20);
948 restore_flags(flags);
950 return val;
954 * ESS technology describes a detection scheme in their docs. It involves
955 * fiddling with the bits in certain mixer registers. ess_probe is supposed
956 * to help.
958 * FKS: tracing shows ess_probe writes wrong value to 0x64. Bit 3 reads 1, but
959 * should be written 0 only. Check this.
961 static int ess_probe (sb_devc * devc, int reg, int xorval)
963 int val1, val2, val3;
965 val1 = ess_getmixer (devc, reg);
966 val2 = val1 ^ xorval;
967 ess_setmixer (devc, reg, val2);
968 val3 = ess_getmixer (devc, reg);
969 ess_setmixer (devc, reg, val1);
971 return (val2 == val3);
974 int ess_init(sb_devc * devc, struct address_info *hw_config)
976 unsigned char cfg;
977 int ess_major = 0, ess_minor = 0;
978 int i;
979 static char name[100], modelname[10];
982 * Try to detect ESS chips.
985 sb_dsp_command(devc, 0xe7); /* Return identification */
987 for (i = 1000; i; i--) {
988 if (inb(DSP_DATA_AVAIL) & 0x80) {
989 if (ess_major == 0) {
990 ess_major = inb(DSP_READ);
991 } else {
992 ess_minor = inb(DSP_READ);
993 break;
998 if (ess_major == 0) return 0;
1000 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80) {
1001 sprintf(name, "ESS ES488 AudioDrive (rev %d)",
1002 ess_minor & 0x0f);
1003 hw_config->name = name;
1004 devc->model = MDL_SBPRO;
1005 return 1;
1009 * This the detection heuristic of ESS technology, though somewhat
1010 * changed to actually make it work.
1011 * This results in the following detection steps:
1012 * - distinct between ES688 and ES1688+ (as always done in this driver)
1013 * if ES688 we're ready
1014 * - try to detect ES1868, ES1869 or ES1878 (ess_identify)
1015 * if successful we're ready
1016 * - try to detect ES1888, ES1887 or ES1788 (aim: detect ES1887)
1017 * if successful we're ready
1018 * - Dunno. Must be 1688. Will do in general
1020 * This is the most BETA part of the software: Will the detection
1021 * always work?
1023 devc->model = MDL_ESS;
1024 devc->submodel = ess_minor & 0x0f;
1026 if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80) {
1027 char *chip = NULL;
1029 if (esstype) {
1030 int submodel = -1;
1032 switch (esstype) {
1033 case 688:
1034 submodel = 0x00;
1035 break;
1036 case 1688:
1037 submodel = 0x08;
1038 break;
1039 case 1868:
1040 submodel = SUBMDL_ES1868;
1041 break;
1042 case 1869:
1043 submodel = SUBMDL_ES1869;
1044 break;
1045 case 1788:
1046 submodel = SUBMDL_ES1788;
1047 break;
1048 case 1887:
1049 submodel = SUBMDL_ES1887;
1050 break;
1051 case 1888:
1052 submodel = SUBMDL_ES1888;
1053 break;
1055 if (submodel != -1) {
1056 devc->submodel = submodel;
1057 sprintf (modelname, "ES%d", esstype);
1058 chip = modelname;
1061 if (chip == NULL && (ess_minor & 0x0f) < 8) {
1062 chip = "ES688";
1065 if (chip == NULL) {
1066 int type;
1068 type = ess_identify (devc);
1070 switch (type) {
1071 case 0x1868:
1072 chip = "ES1868";
1073 devc->submodel = SUBMDL_ES1868;
1074 break;
1075 case 0x1869:
1076 chip = "ES1869";
1077 devc->submodel = SUBMDL_ES1869;
1078 break;
1079 case 0x1878:
1080 chip = "ES1878";
1081 devc->submodel = SUBMDL_ES1878;
1082 break;
1085 #if 0
1087 * this one failed:
1088 * the probing of bit 4 is another thought: from ES1788 and up, all
1089 * chips seem to have hardware volume control. Bit 4 is readonly to
1090 * check if a hardware volume interrupt has fired.
1091 * Cause ES688/ES1688 don't have this feature, bit 4 might be writeable
1092 * for these chips.
1094 if (chip == NULL && !ess_probe(devc, 0x64, (1 << 4))) {
1095 #endif
1097 * the probing of bit 2 is my idea. The ES1887 docs want me to probe
1098 * bit 3. This results in ES1688 being detected as ES1788.
1099 * Bit 2 is for "Enable HWV IRQE", but as ES(1)688 chips don't have
1100 * HardWare Volume, I think they don't have this IRQE.
1102 if (chip == NULL && ess_probe(devc, 0x64, (1 << 2))) {
1103 if (ess_probe (devc, 0x70, 0x7f)) {
1104 if (ess_probe (devc, 0x64, (1 << 5))) {
1105 chip = "ES1887";
1106 devc->submodel = SUBMDL_ES1887;
1107 } else {
1108 chip = "ES1888";
1109 devc->submodel = SUBMDL_ES1888;
1111 } else {
1112 chip = "ES1788";
1113 devc->submodel = SUBMDL_ES1788;
1116 if (chip == NULL) {
1117 chip = "ES1688";
1120 sprintf(name,"ESS %s AudioDrive (rev %d)", chip, ess_minor & 0x0f);
1121 } else {
1122 strcpy(name, "Jazz16");
1125 hw_config->name = name;
1126 /* FKS: sb_dsp_reset to enable extended mode???? */
1127 sb_dsp_reset(devc); /* Turn on extended mode */
1130 * Enable joystick and OPL3
1132 cfg = ess_getmixer (devc, 0x40);
1133 ess_setmixer (devc, 0x40, cfg | 0x03);
1134 if (devc->submodel >= 8) { /* ES1688 */
1135 devc->caps |= SB_NO_MIDI; /* ES1688 uses MPU401 MIDI mode */
1137 sb_dsp_reset (devc);
1140 * This is important! If it's not done, the IRQ probe in sb_dsp_init
1141 * may fail.
1143 return ess_set_irq_hw (devc);
1146 static int ess_set_dma_hw(sb_devc * devc)
1148 unsigned char cfg, dma_bits = 0, dma16_bits;
1149 int dma;
1151 #ifdef FKS_LOGGING
1152 printk(KERN_INFO "ess_set_dma_hw: dma8=%d,dma16=%d,dup=%d\n"
1153 , devc->dma8, devc->dma16, devc->duplex);
1154 #endif
1157 * FKS: It seems as if this duplex flag isn't set yet. Check it.
1159 dma = devc->dma8;
1161 if (dma > 3 || dma < 0 || dma == 2) {
1162 dma_bits = 0;
1163 printk(KERN_ERR "ESS1688: Invalid DMA8 %d\n", dma);
1164 return 0;
1165 } else {
1166 /* Extended mode DMA enable */
1167 cfg = 0x50;
1169 if (dma == 3) {
1170 dma_bits = 3;
1171 } else {
1172 dma_bits = dma + 1;
1176 if (!ess_write (devc, 0xb2, cfg | (dma_bits << 2))) {
1177 printk(KERN_ERR "ESS1688: Failed to write to DMA config register\n");
1178 return 0;
1181 if (devc->duplex) {
1182 dma = devc->dma16;
1183 dma16_bits = 0;
1185 if (dma >= 0) {
1186 switch (dma) {
1187 case 0:
1188 dma_bits = 0x04;
1189 break;
1190 case 1:
1191 dma_bits = 0x05;
1192 break;
1193 case 3:
1194 dma_bits = 0x06;
1195 break;
1196 case 5:
1197 dma_bits = 0x07;
1198 dma16_bits = 0x20;
1199 break;
1200 default:
1201 printk(KERN_ERR "ESS1887: Invalid DMA16 %d\n", dma);
1202 return 0;
1204 ess_chgmixer (devc, 0x78, 0x20, dma16_bits);
1205 ess_chgmixer (devc, 0x7d, 0x07, dma_bits);
1208 return 1;
1212 * This one is called from sb_dsp_init.
1214 * Return values:
1215 * 0: Failed
1216 * 1: Succeeded or doesn't apply (not SUBMDL_ES1887)
1218 int ess_dsp_init (sb_devc *devc, struct address_info *hw_config)
1221 * This for ES1887 to run Full Duplex. Actually ES1888
1222 * is allowed to do so too. I have no idea yet if this
1223 * will work for ES1888 however.
1225 * For SB16 having both dma8 and dma16 means enable
1226 * Full Duplex. Let's try this for ES1887 too
1229 if (devc->submodel == SUBMDL_ES1887) {
1230 if (hw_config->dma2 != -1) {
1231 devc->dma16 = hw_config->dma2;
1234 * devc->duplex initialization is put here, cause
1235 * ess_set_dma_hw needs it.
1237 if (devc->dma8 != devc->dma16 && devc->dma16 != -1) {
1238 devc->duplex = 1;
1241 if (!ess_set_dma_hw (devc)) {
1242 free_irq(devc->irq, devc);
1243 return 0;
1245 return 1;
1246 } else {
1247 return -1;
1251 /****************************************************************************
1253 * ESS mixer *
1255 ****************************************************************************/
1257 #define ES688_RECORDING_DEVICES \
1258 ( SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD )
1259 #define ES688_MIXER_DEVICES \
1260 ( SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE \
1261 | SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME \
1262 | SOUND_MASK_LINE2 | SOUND_MASK_SPEAKER )
1264 #define ES1688_RECORDING_DEVICES \
1265 ( ES688_RECORDING_DEVICES )
1266 #define ES1688_MIXER_DEVICES \
1267 ( ES688_MIXER_DEVICES | SOUND_MASK_RECLEV )
1269 #define ES1887_RECORDING_DEVICES \
1270 ( ES1688_RECORDING_DEVICES | SOUND_MASK_LINE2 | SOUND_MASK_SYNTH)
1271 #define ES1887_MIXER_DEVICES \
1272 ( ES1688_MIXER_DEVICES )
1275 * Mixer registers of ES1887
1277 * These registers specifically take care of recording levels. To make the
1278 * mapping from playback devices to recording devices every recording
1279 * devices = playback device + ES_REC_MIXER_RECDIFF
1281 #define ES_REC_MIXER_RECBASE (SOUND_MIXER_LINE3 + 1)
1282 #define ES_REC_MIXER_RECDIFF (ES_REC_MIXER_RECBASE - SOUND_MIXER_SYNTH)
1284 #define ES_REC_MIXER_RECSYNTH (SOUND_MIXER_SYNTH + ES_REC_MIXER_RECDIFF)
1285 #define ES_REC_MIXER_RECPCM (SOUND_MIXER_PCM + ES_REC_MIXER_RECDIFF)
1286 #define ES_REC_MIXER_RECSPEAKER (SOUND_MIXER_SPEAKER + ES_REC_MIXER_RECDIFF)
1287 #define ES_REC_MIXER_RECLINE (SOUND_MIXER_LINE + ES_REC_MIXER_RECDIFF)
1288 #define ES_REC_MIXER_RECMIC (SOUND_MIXER_MIC + ES_REC_MIXER_RECDIFF)
1289 #define ES_REC_MIXER_RECCD (SOUND_MIXER_CD + ES_REC_MIXER_RECDIFF)
1290 #define ES_REC_MIXER_RECIMIX (SOUND_MIXER_IMIX + ES_REC_MIXER_RECDIFF)
1291 #define ES_REC_MIXER_RECALTPCM (SOUND_MIXER_ALTPCM + ES_REC_MIXER_RECDIFF)
1292 #define ES_REC_MIXER_RECRECLEV (SOUND_MIXER_RECLEV + ES_REC_MIXER_RECDIFF)
1293 #define ES_REC_MIXER_RECIGAIN (SOUND_MIXER_IGAIN + ES_REC_MIXER_RECDIFF)
1294 #define ES_REC_MIXER_RECOGAIN (SOUND_MIXER_OGAIN + ES_REC_MIXER_RECDIFF)
1295 #define ES_REC_MIXER_RECLINE1 (SOUND_MIXER_LINE1 + ES_REC_MIXER_RECDIFF)
1296 #define ES_REC_MIXER_RECLINE2 (SOUND_MIXER_LINE2 + ES_REC_MIXER_RECDIFF)
1297 #define ES_REC_MIXER_RECLINE3 (SOUND_MIXER_LINE3 + ES_REC_MIXER_RECDIFF)
1299 static mixer_tab es688_mix = {
1300 MIX_ENT(SOUND_MIXER_VOLUME, 0x32, 7, 4, 0x32, 3, 4),
1301 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1302 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1303 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1304 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1305 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1306 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1307 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1308 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1309 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1310 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1311 MIX_ENT(SOUND_MIXER_RECLEV, 0x00, 0, 0, 0x00, 0, 0),
1312 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1313 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1314 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1315 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1316 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1320 * The ES1688 specifics... hopefully correct...
1321 * - 6 bit master volume
1322 * I was wrong, ES1888 docs say ES1688 didn't have it.
1323 * - RECLEV control
1324 * These may apply to ES688 too. I have no idea.
1326 static mixer_tab es1688_mix = {
1327 MIX_ENT(SOUND_MIXER_VOLUME, 0x32, 7, 4, 0x32, 3, 4),
1328 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1329 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1330 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1331 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1332 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1333 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1334 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1335 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1336 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1337 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1338 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1339 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1340 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1341 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1342 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1343 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1346 static mixer_tab es1688later_mix = {
1347 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1348 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1349 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1350 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1351 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1352 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1353 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1354 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1355 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1356 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1357 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1358 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1359 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1360 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1361 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1362 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1363 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0)
1367 * This one is for all ESS chips with a record mixer.
1368 * It's not used (yet) however
1370 static mixer_tab es_rec_mix = {
1371 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1372 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1373 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1374 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1375 MIX_ENT(SOUND_MIXER_PCM, 0x14, 7, 4, 0x14, 3, 4),
1376 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1377 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1378 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1379 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1380 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1381 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1382 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1383 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1384 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1385 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1386 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1387 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0),
1388 MIX_ENT(ES_REC_MIXER_RECSYNTH, 0x6b, 7, 4, 0x6b, 3, 4),
1389 MIX_ENT(ES_REC_MIXER_RECPCM, 0x00, 0, 0, 0x00, 0, 0),
1390 MIX_ENT(ES_REC_MIXER_RECSPEAKER, 0x00, 0, 0, 0x00, 0, 0),
1391 MIX_ENT(ES_REC_MIXER_RECLINE, 0x6e, 7, 4, 0x6e, 3, 4),
1392 MIX_ENT(ES_REC_MIXER_RECMIC, 0x68, 7, 4, 0x68, 3, 4),
1393 MIX_ENT(ES_REC_MIXER_RECCD, 0x6a, 7, 4, 0x6a, 3, 4),
1394 MIX_ENT(ES_REC_MIXER_RECIMIX, 0x00, 0, 0, 0x00, 0, 0),
1395 MIX_ENT(ES_REC_MIXER_RECALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1396 MIX_ENT(ES_REC_MIXER_RECRECLEV, 0x00, 0, 0, 0x00, 0, 0),
1397 MIX_ENT(ES_REC_MIXER_RECIGAIN, 0x00, 0, 0, 0x00, 0, 0),
1398 MIX_ENT(ES_REC_MIXER_RECOGAIN, 0x00, 0, 0, 0x00, 0, 0),
1399 MIX_ENT(ES_REC_MIXER_RECLINE1, 0x00, 0, 0, 0x00, 0, 0),
1400 MIX_ENT(ES_REC_MIXER_RECLINE2, 0x6c, 7, 4, 0x6c, 3, 4),
1401 MIX_ENT(ES_REC_MIXER_RECLINE3, 0x00, 0, 0, 0x00, 0, 0)
1405 * This one is for ES1887. It's little different from es_rec_mix: it
1406 * has 0x7c for PCM playback level. This is because ES1887 uses
1407 * Audio 2 for playback.
1409 static mixer_tab es1887_mix = {
1410 MIX_ENT(SOUND_MIXER_VOLUME, 0x60, 5, 6, 0x62, 5, 6),
1411 MIX_ENT(SOUND_MIXER_BASS, 0x00, 0, 0, 0x00, 0, 0),
1412 MIX_ENT(SOUND_MIXER_TREBLE, 0x00, 0, 0, 0x00, 0, 0),
1413 MIX_ENT(SOUND_MIXER_SYNTH, 0x36, 7, 4, 0x36, 3, 4),
1414 MIX_ENT(SOUND_MIXER_PCM, 0x7c, 7, 4, 0x7c, 3, 4),
1415 MIX_ENT(SOUND_MIXER_SPEAKER, 0x3c, 2, 3, 0x00, 0, 0),
1416 MIX_ENT(SOUND_MIXER_LINE, 0x3e, 7, 4, 0x3e, 3, 4),
1417 MIX_ENT(SOUND_MIXER_MIC, 0x1a, 7, 4, 0x1a, 3, 4),
1418 MIX_ENT(SOUND_MIXER_CD, 0x38, 7, 4, 0x38, 3, 4),
1419 MIX_ENT(SOUND_MIXER_IMIX, 0x00, 0, 0, 0x00, 0, 0),
1420 MIX_ENT(SOUND_MIXER_ALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1421 MIX_ENT(SOUND_MIXER_RECLEV, 0xb4, 7, 4, 0xb4, 3, 4),
1422 MIX_ENT(SOUND_MIXER_IGAIN, 0x00, 0, 0, 0x00, 0, 0),
1423 MIX_ENT(SOUND_MIXER_OGAIN, 0x00, 0, 0, 0x00, 0, 0),
1424 MIX_ENT(SOUND_MIXER_LINE1, 0x00, 0, 0, 0x00, 0, 0),
1425 MIX_ENT(SOUND_MIXER_LINE2, 0x3a, 7, 4, 0x3a, 3, 4),
1426 MIX_ENT(SOUND_MIXER_LINE3, 0x00, 0, 0, 0x00, 0, 0),
1427 MIX_ENT(ES_REC_MIXER_RECSYNTH, 0x6b, 7, 4, 0x6b, 3, 4),
1428 MIX_ENT(ES_REC_MIXER_RECPCM, 0x00, 0, 0, 0x00, 0, 0),
1429 MIX_ENT(ES_REC_MIXER_RECSPEAKER, 0x00, 0, 0, 0x00, 0, 0),
1430 MIX_ENT(ES_REC_MIXER_RECLINE, 0x6e, 7, 4, 0x6e, 3, 4),
1431 MIX_ENT(ES_REC_MIXER_RECMIC, 0x68, 7, 4, 0x68, 3, 4),
1432 MIX_ENT(ES_REC_MIXER_RECCD, 0x6a, 7, 4, 0x6a, 3, 4),
1433 MIX_ENT(ES_REC_MIXER_RECIMIX, 0x00, 0, 0, 0x00, 0, 0),
1434 MIX_ENT(ES_REC_MIXER_RECALTPCM, 0x00, 0, 0, 0x00, 0, 0),
1435 MIX_ENT(ES_REC_MIXER_RECRECLEV, 0x00, 0, 0, 0x00, 0, 0),
1436 MIX_ENT(ES_REC_MIXER_RECIGAIN, 0x00, 0, 0, 0x00, 0, 0),
1437 MIX_ENT(ES_REC_MIXER_RECOGAIN, 0x00, 0, 0, 0x00, 0, 0),
1438 MIX_ENT(ES_REC_MIXER_RECLINE1, 0x00, 0, 0, 0x00, 0, 0),
1439 MIX_ENT(ES_REC_MIXER_RECLINE2, 0x6c, 7, 4, 0x6c, 3, 4),
1440 MIX_ENT(ES_REC_MIXER_RECLINE3, 0x00, 0, 0, 0x00, 0, 0)
1443 static int ess_has_rec_mixer (int submodel)
1445 switch (submodel) {
1446 case SUBMDL_ES1887:
1447 return 1;
1448 default:
1449 return 0;
1453 #ifdef FKS_LOGGING
1454 static int ess_mixer_mon_regs[]
1455 = { 0x70, 0x71, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7d, 0x7f
1456 , 0xa1, 0xa2, 0xa4, 0xa5, 0xa8, 0xa9
1457 , 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb9
1458 , 0x00};
1460 static void ess_show_mixerregs (sb_devc *devc)
1462 int *mp = ess_mixer_mon_regs;
1464 return;
1466 while (*mp != 0) {
1467 printk (KERN_INFO "res (%x)=%x\n", *mp, (int)(ess_getmixer (devc, *mp)));
1468 mp++;
1471 #endif
1473 void ess_setmixer (sb_devc * devc, unsigned int port, unsigned int value)
1475 unsigned long flags;
1477 #ifdef FKS_LOGGING
1478 printk(KERN_INFO "FKS: write mixer %x: %x\n", port, value);
1479 #endif
1481 save_flags(flags);
1482 cli();
1483 if (port >= 0xa0) {
1484 ess_write (devc, port, value);
1485 } else {
1486 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1488 udelay(20);
1489 outb(((unsigned char) (value & 0xff)), MIXER_DATA);
1490 udelay(20);
1492 restore_flags(flags);
1495 unsigned int ess_getmixer (sb_devc * devc, unsigned int port)
1497 unsigned int val;
1498 unsigned long flags;
1500 save_flags(flags);
1501 cli();
1503 if (port >= 0xa0) {
1504 val = ess_read (devc, port);
1505 } else {
1506 outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
1508 udelay(20);
1509 val = inb(MIXER_DATA);
1510 udelay(20);
1512 restore_flags(flags);
1514 return val;
1517 static void ess_chgmixer
1518 (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
1520 int value;
1522 value = ess_getmixer (devc, reg);
1523 value = (value & ~mask) | (val & mask);
1524 ess_setmixer (devc, reg, value);
1528 * ess_mixer_init must be called from sb_mixer_init
1530 void ess_mixer_init (sb_devc * devc)
1532 devc->mixer_caps = SOUND_CAP_EXCL_INPUT;
1535 * Take care of ES1887 specifics...
1537 switch (devc->submodel) {
1538 case SUBMDL_ES1887:
1539 devc->supported_devices = ES1887_MIXER_DEVICES;
1540 devc->supported_rec_devices = ES1887_RECORDING_DEVICES;
1541 #ifdef FKS_LOGGING
1542 printk (KERN_INFO "FKS: ess_mixer_init dup = %d\n", devc->duplex);
1543 #endif
1544 if (devc->duplex) {
1545 devc->iomap = &es1887_mix;
1546 } else {
1547 devc->iomap = &es_rec_mix;
1549 break;
1550 default:
1551 if (devc->submodel < 8) {
1552 devc->supported_devices = ES688_MIXER_DEVICES;
1553 devc->supported_rec_devices = ES688_RECORDING_DEVICES;
1554 devc->iomap = &es688_mix;
1555 } else {
1557 * es1688 has 4 bits master vol.
1558 * later chips have 6 bits (?)
1560 devc->supported_devices = ES1688_MIXER_DEVICES;
1561 devc->supported_rec_devices = ES1688_RECORDING_DEVICES;
1562 if (devc->submodel < 0x10) {
1563 devc->iomap = &es1688_mix;
1564 } else {
1565 devc->iomap = &es1688later_mix;
1572 * Changing playback levels at an ESS chip with record mixer means having to
1573 * take care of recording levels of recorded inputs (devc->recmask) too!
1575 int ess_mixer_set(sb_devc *devc, int dev, int left, int right)
1577 if (ess_has_rec_mixer (devc->submodel) && (devc->recmask & (1 << dev))) {
1578 sb_common_mixer_set (devc, dev + ES_REC_MIXER_RECDIFF, left, right);
1580 return sb_common_mixer_set (devc, dev, left, right);
1584 * After a sb_dsp_reset extended register 0xb4 (RECLEV) is reset too. After
1585 * sb_dsp_reset RECLEV has to be restored. This is where ess_mixer_reload
1586 * helps.
1588 void ess_mixer_reload (sb_devc *devc, int dev)
1590 int left, right, value;
1592 value = devc->levels[dev];
1593 left = value & 0x000000ff;
1594 right = (value & 0x0000ff00) >> 8;
1596 sb_common_mixer_set(devc, dev, left, right);
1599 int es_rec_set_recmask(sb_devc * devc, int mask)
1601 int i, i_mask, cur_mask, diff_mask;
1602 int value, left, right;
1604 #ifdef FKS_LOGGING
1605 printk (KERN_INFO "FKS: es_rec_set_recmask mask = %x\n", mask);
1606 #endif
1608 * Changing the recmask on an ESS chip with recording mixer means:
1609 * (1) Find the differences
1610 * (2) For "turned-on" inputs: make the recording level the playback level
1611 * (3) For "turned-off" inputs: make the recording level zero
1613 cur_mask = devc->recmask;
1614 diff_mask = (cur_mask ^ mask);
1616 for (i = 0; i < 32; i++) {
1617 i_mask = (1 << i);
1618 if (diff_mask & i_mask) { /* Difference? (1) */
1619 if (mask & i_mask) { /* Turn it on (2) */
1620 value = devc->levels[i];
1621 left = value & 0x000000ff;
1622 right = (value & 0x0000ff00) >> 8;
1623 } else { /* Turn it off (3) */
1624 left = 0;
1625 left = 0;
1626 right = 0;
1628 sb_common_mixer_set(devc, i + ES_REC_MIXER_RECDIFF, left, right);
1631 return mask;
1634 int ess_set_recmask(sb_devc * devc, int *mask)
1636 /* This applies to ESS chips with record mixers only! */
1638 if (ess_has_rec_mixer (devc->submodel)) {
1639 *mask = es_rec_set_recmask (devc, *mask);
1640 return 1; /* Applied */
1641 } else {
1642 return 0; /* Not applied */
1647 * ess_mixer_reset must be called from sb_mixer_reset
1649 int ess_mixer_reset (sb_devc * devc)
1652 * Separate actions for ESS chips with a record mixer:
1654 if (ess_has_rec_mixer (devc->submodel)) {
1655 switch (devc->submodel) {
1656 case SUBMDL_ES1887:
1658 * Separate actions for ES1887:
1659 * Change registers 7a and 1c to make the record mixer the
1660 * actual recording source.
1662 ess_chgmixer(devc, 0x7a, 0x18, 0x08);
1663 ess_chgmixer(devc, 0x1c, 0x07, 0x07);
1664 break;
1667 * Call set_recmask for proper initialization
1669 devc->recmask = devc->supported_rec_devices;
1670 es_rec_set_recmask(devc, 0);
1671 devc->recmask = 0;
1673 return 1; /* We took care of recmask. */
1674 } else {
1675 return 0; /* We didn't take care; caller do it */
1679 /****************************************************************************
1681 * ESS midi *
1683 ****************************************************************************/
1686 * FKS: IRQ may be shared. Hm. And if so? Then What?
1688 int ess_midi_init(sb_devc * devc, struct address_info *hw_config)
1690 unsigned char cfg, tmp;
1692 cfg = ess_getmixer (devc, 0x40) & 0x03;
1694 if (devc->submodel < 8) {
1695 ess_setmixer (devc, 0x40, cfg | 0x03); /* Enable OPL3 & joystick */
1696 return 0; /* ES688 doesn't support MPU401 mode */
1698 tmp = (hw_config->io_base & 0x0f0) >> 4;
1700 if (tmp > 3) {
1701 ess_setmixer (devc, 0x40, cfg);
1702 return 0;
1704 cfg |= tmp << 3;
1706 tmp = 1; /* MPU enabled without interrupts */
1708 /* May be shared: if so the value is -ve */
1710 switch (abs(hw_config->irq)) {
1711 case 9:
1712 tmp = 0x4;
1713 break;
1714 case 5:
1715 tmp = 0x5;
1716 break;
1717 case 7:
1718 tmp = 0x6;
1719 break;
1720 case 10:
1721 tmp = 0x7;
1722 break;
1723 default:
1724 return 0;
1727 cfg |= tmp << 5;
1728 ess_setmixer (devc, 0x40, cfg | 0x03);
1730 return 1;