ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / es1688 / es1688_lib.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of ESS ES1688/688/488 chip
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/ioport.h>
28 #include <sound/core.h>
29 #include <sound/es1688.h>
30 #include <sound/initval.h>
31
32 #include <asm/io.h>
33 #include <asm/dma.h>
34
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
36 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
37 MODULE_CLASSES("{sound}");
38 MODULE_LICENSE("GPL");
39
40 static int snd_es1688_dsp_command(es1688_t *chip, unsigned char val)
41 {
42         int i;
43
44         for (i = 10000; i; i--)
45                 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
46                         outb(val, ES1688P(chip, COMMAND));
47                         return 1;
48                 }
49 #ifdef CONFIG_SND_DEBUG
50         printk("snd_es1688_dsp_command: timeout (0x%x)\n", val);
51 #endif
52         return 0;
53 }
54
55 static int snd_es1688_dsp_get_byte(es1688_t *chip)
56 {
57         int i;
58
59         for (i = 1000; i; i--)
60                 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
61                         return inb(ES1688P(chip, READ));
62         snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
63         return -ENODEV;
64 }
65
66 static int snd_es1688_write(es1688_t *chip,
67                             unsigned char reg, unsigned char data)
68 {
69         if (!snd_es1688_dsp_command(chip, reg))
70                 return 0;
71         return snd_es1688_dsp_command(chip, data);
72 }
73
74 int snd_es1688_read(es1688_t *chip, unsigned char reg)
75 {
76         /* Read a byte from an extended mode register of ES1688 */
77         if (!snd_es1688_dsp_command(chip, 0xc0))
78                 return -1;
79         if (!snd_es1688_dsp_command(chip, reg))
80                 return -1;
81         return snd_es1688_dsp_get_byte(chip);
82 }
83
84 void snd_es1688_mixer_write(es1688_t *chip,
85                             unsigned char reg, unsigned char data)
86 {
87         outb(reg, ES1688P(chip, MIXER_ADDR));
88         udelay(10);
89         outb(data, ES1688P(chip, MIXER_DATA));
90         udelay(10);
91 }
92
93 unsigned char snd_es1688_mixer_read(es1688_t *chip, unsigned char reg)
94 {
95         unsigned char result;
96
97         outb(reg, ES1688P(chip, MIXER_ADDR));
98         udelay(10);
99         result = inb(ES1688P(chip, MIXER_DATA));
100         udelay(10);
101         return result;
102 }
103
104 static int snd_es1688_reset(es1688_t *chip)
105 {
106         int i;
107
108         outb(3, ES1688P(chip, RESET));          /* valid only for ESS chips, SB -> 1 */
109         udelay(10);
110         outb(0, ES1688P(chip, RESET));
111         udelay(30);
112         for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
113         if (inb(ES1688P(chip, READ)) != 0xaa) {
114                 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
115                 return -ENODEV;
116         }
117         snd_es1688_dsp_command(chip, 0xc6);     /* enable extended mode */
118         return 0;
119 }
120
121 static int snd_es1688_probe(es1688_t *chip)
122 {
123         unsigned long flags;
124         unsigned short major, minor, hw;
125         int i;
126
127         /*
128          *  initialization sequence
129          */
130
131         spin_lock_irqsave(&chip->reg_lock, flags);      /* Some ESS1688 cards need this */
132         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
133         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
134         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
135         inb(ES1688P(chip, ENABLE2));    /* ENABLE2 */
136         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
137         inb(ES1688P(chip, ENABLE2));    /* ENABLE2 */
138         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
139         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
140         inb(ES1688P(chip, ENABLE2));    /* ENABLE2 */
141         inb(ES1688P(chip, ENABLE1));    /* ENABLE1 */
142         inb(ES1688P(chip, ENABLE0));    /* ENABLE0 */
143
144         if (snd_es1688_reset(chip) < 0) {
145                 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
146                 spin_unlock_irqrestore(&chip->reg_lock, flags);
147                 return -ENODEV;
148         }
149         snd_es1688_dsp_command(chip, 0xe7);     /* return identification */
150
151         for (i = 1000, major = minor = 0; i; i--) {
152                 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
153                         if (major == 0) {
154                                 major = inb(ES1688P(chip, READ));
155                         } else {
156                                 minor = inb(ES1688P(chip, READ));
157                         }
158                 }
159         }
160
161         spin_unlock_irqrestore(&chip->reg_lock, flags);
162
163         snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
164
165         chip->version = (major << 8) | minor;
166         if (!chip->version)
167                 return -ENODEV; /* probably SB */
168
169         hw = ES1688_HW_AUTO;
170         switch (chip->version & 0xfff0) {
171         case 0x4880:
172                 snd_printk("[0x%lx] ESS: AudioDrive ES488 detected, but driver is in another place\n", chip->port);
173                 return -ENODEV;
174         case 0x6880:
175                 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
176                 break;
177         default:
178                 snd_printk("[0x%lx] ESS: unknown AudioDrive chip with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version);
179                 return -ENODEV;
180         }
181
182         spin_lock_irqsave(&chip->reg_lock, flags);
183         snd_es1688_write(chip, 0xb1, 0x10);     /* disable IRQ */
184         snd_es1688_write(chip, 0xb2, 0x00);     /* disable DMA */
185         spin_unlock_irqrestore(&chip->reg_lock, flags);
186
187         /* enable joystick, but disable OPL3 */
188         spin_lock_irqsave(&chip->mixer_lock, flags);
189         snd_es1688_mixer_write(chip, 0x40, 0x01);
190         spin_unlock_irqrestore(&chip->mixer_lock, flags);
191
192         return 0;
193 }
194
195 static int snd_es1688_init(es1688_t * chip, int enable)
196 {
197         static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
198         unsigned long flags;
199         int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
200
201         /* ok.. setup MPU-401 port and joystick and OPL3 */
202         cfg = 0x01;             /* enable joystick, but disable OPL3 */
203         if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
204                 tmp = (chip->mpu_port & 0x0f0) >> 4;
205                 if (tmp <= 3) {
206                         switch (chip->mpu_irq) {
207                         case 9:
208                                 tmp1 = 4;
209                                 break;
210                         case 5:
211                                 tmp1 = 5;
212                                 break;
213                         case 7:
214                                 tmp1 = 6;
215                                 break;
216                         case 10:
217                                 tmp1 = 7;
218                                 break;
219                         default:
220                                 tmp1 = 0;
221                         }
222                         if (tmp1) {
223                                 cfg |= (tmp << 3) | (tmp1 << 5);
224                         }
225                 }
226         }
227 #if 0
228         snd_printk("mpu cfg = 0x%x\n", cfg);
229 #endif
230         spin_lock_irqsave(&chip->reg_lock, flags);
231         snd_es1688_mixer_write(chip, 0x40, cfg);
232         spin_unlock_irqrestore(&chip->reg_lock, flags);
233         /* --- */
234         spin_lock_irqsave(&chip->reg_lock, flags);
235         snd_es1688_read(chip, 0xb1);
236         snd_es1688_read(chip, 0xb2);
237         spin_unlock_irqrestore(&chip->reg_lock, flags);
238         if (enable) {
239                 cfg = 0xf0;     /* enable only DMA counter interrupt */
240                 irq_bits = irqs[chip->irq & 0x0f];
241                 if (irq_bits < 0) {
242                         snd_printk("[0x%lx] ESS: bad IRQ %d for ES1688 chip!!\n", chip->port, chip->irq);
243 #if 0
244                         irq_bits = 0;
245                         cfg = 0x10;
246 #endif
247                         return -EINVAL;
248                 }
249                 spin_lock_irqsave(&chip->reg_lock, flags);
250                 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
251                 spin_unlock_irqrestore(&chip->reg_lock, flags);
252                 cfg = 0xf0;     /* extended mode DMA enable */
253                 dma = chip->dma8;
254                 if (dma > 3 || dma == 2) {
255                         snd_printk("[0x%lx] ESS: bad DMA channel %d for ES1688 chip!!\n", chip->port, dma);
256 #if 0
257                         dma_bits = 0;
258                         cfg = 0x00;     /* disable all DMA */
259 #endif
260                         return -EINVAL;
261                 } else {
262                         dma_bits = dma;
263                         if (dma != 3)
264                                 dma_bits++;
265                 }
266                 spin_lock_irqsave(&chip->reg_lock, flags);
267                 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
268                 spin_unlock_irqrestore(&chip->reg_lock, flags);
269         } else {
270                 spin_lock_irqsave(&chip->reg_lock, flags);
271                 snd_es1688_write(chip, 0xb1, 0x10);     /* disable IRQ */
272                 snd_es1688_write(chip, 0xb2, 0x00);     /* disable DMA */
273                 spin_unlock_irqrestore(&chip->reg_lock, flags);
274         }
275         spin_lock_irqsave(&chip->reg_lock, flags);
276         snd_es1688_read(chip, 0xb1);
277         snd_es1688_read(chip, 0xb2);
278         snd_es1688_reset(chip);
279         spin_unlock_irqrestore(&chip->reg_lock, flags);
280         return 0;
281 }
282
283 /*
284
285  */
286
287 static ratnum_t clocks[2] = {
288         {
289                 .num = 795444,
290                 .den_min = 1,
291                 .den_max = 128,
292                 .den_step = 1,
293         },
294         {
295                 .num = 397722,
296                 .den_min = 1,
297                 .den_max = 128,
298                 .den_step = 1,
299         }
300 };
301
302 static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks  = {
303         .nrats = 2,
304         .rats = clocks,
305 };
306
307 static void snd_es1688_set_rate(es1688_t *chip, snd_pcm_substream_t *substream)
308 {
309         snd_pcm_runtime_t *runtime = substream->runtime;
310         unsigned int bits, divider;
311
312         if (runtime->rate_num == clocks[0].num)
313                 bits = 256 - runtime->rate_den;
314         else
315                 bits = 128 - runtime->rate_den;
316         /* set filter register */
317         divider = 256 - 7160000*20/(8*82*runtime->rate);
318         /* write result to hardware */
319         snd_es1688_write(chip, 0xa1, bits);
320         snd_es1688_write(chip, 0xa2, divider);
321 }
322
323 static int snd_es1688_ioctl(snd_pcm_substream_t * substream,
324                             unsigned int cmd, void *arg)
325 {
326         return snd_pcm_lib_ioctl(substream, cmd, arg);
327 }
328
329 static int snd_es1688_trigger(es1688_t *chip, int cmd, unsigned char value)
330 {
331         int val;
332
333         if (cmd == SNDRV_PCM_TRIGGER_STOP) {
334                 value = 0x00;
335         } else if (cmd != SNDRV_PCM_TRIGGER_START) {
336                 return -EINVAL;
337         }
338         spin_lock(&chip->reg_lock);
339         chip->trigger_value = value;
340         val = snd_es1688_read(chip, 0xb8);
341         if ((val < 0) || (val & 0x0f) == value) {
342                 spin_unlock(&chip->reg_lock);
343                 return -EINVAL; /* something is wrong */
344         }
345 #if 0
346         printk("trigger: val = 0x%x, value = 0x%x\n", val, value);
347         printk("trigger: pointer = 0x%x\n", snd_dma_pointer(chip->dma8, chip->dma_size));
348 #endif
349         snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
350         spin_unlock(&chip->reg_lock);
351         return 0;
352 }
353
354 static int snd_es1688_hw_params(snd_pcm_substream_t * substream,
355                                 snd_pcm_hw_params_t * hw_params)
356 {
357         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
358 }
359
360 static int snd_es1688_hw_free(snd_pcm_substream_t * substream)
361 {
362         return snd_pcm_lib_free_pages(substream);
363 }
364
365 static int snd_es1688_playback_prepare(snd_pcm_substream_t * substream)
366 {
367         unsigned long flags;
368         es1688_t *chip = snd_pcm_substream_chip(substream);
369         snd_pcm_runtime_t *runtime = substream->runtime;
370         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
371         unsigned int count = snd_pcm_lib_period_bytes(substream);
372
373         chip->dma_size = size;
374         spin_lock_irqsave(&chip->reg_lock, flags);
375         snd_es1688_reset(chip);
376         snd_es1688_set_rate(chip, substream);
377         snd_es1688_write(chip, 0xb8, 4);        /* auto init DMA mode */
378         snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
379         snd_es1688_write(chip, 0xb9, 2);        /* demand mode (4 bytes/request) */
380         if (runtime->channels == 1) {
381                 if (snd_pcm_format_width(runtime->format) == 8) {
382                         /* 8. bit mono */
383                         snd_es1688_write(chip, 0xb6, 0x80);
384                         snd_es1688_write(chip, 0xb7, 0x51);
385                         snd_es1688_write(chip, 0xb7, 0xd0);
386                 } else {
387                         /* 16. bit mono */
388                         snd_es1688_write(chip, 0xb6, 0x00);
389                         snd_es1688_write(chip, 0xb7, 0x71);
390                         snd_es1688_write(chip, 0xb7, 0xf4);
391                 }
392         } else {
393                 if (snd_pcm_format_width(runtime->format) == 8) {
394                         /* 8. bit stereo */
395                         snd_es1688_write(chip, 0xb6, 0x80);
396                         snd_es1688_write(chip, 0xb7, 0x51);
397                         snd_es1688_write(chip, 0xb7, 0x98);
398                 } else {
399                         /* 16. bit stereo */
400                         snd_es1688_write(chip, 0xb6, 0x00);
401                         snd_es1688_write(chip, 0xb7, 0x71);
402                         snd_es1688_write(chip, 0xb7, 0xbc);
403                 }
404         }
405         snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
406         snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
407         snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
408         spin_unlock_irqrestore(&chip->reg_lock, flags);
409         /* --- */
410         count = -count;
411         snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
412         spin_lock_irqsave(&chip->reg_lock, flags);
413         snd_es1688_write(chip, 0xa4, (unsigned char) count);
414         snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
415         spin_unlock_irqrestore(&chip->reg_lock, flags);
416         return 0;
417 }
418
419 static int snd_es1688_playback_trigger(snd_pcm_substream_t * substream,
420                                        int cmd)
421 {
422         es1688_t *chip = snd_pcm_substream_chip(substream);
423         return snd_es1688_trigger(chip, cmd, 0x05);
424 }
425
426 static int snd_es1688_capture_prepare(snd_pcm_substream_t * substream)
427 {
428         unsigned long flags;
429         es1688_t *chip = snd_pcm_substream_chip(substream);
430         snd_pcm_runtime_t *runtime = substream->runtime;
431         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
432         unsigned int count = snd_pcm_lib_period_bytes(substream);
433
434         chip->dma_size = size;
435         spin_lock_irqsave(&chip->reg_lock, flags);
436         snd_es1688_reset(chip);
437         snd_es1688_set_rate(chip, substream);
438         snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
439         snd_es1688_write(chip, 0xb8, 0x0e);     /* auto init DMA mode */
440         snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
441         snd_es1688_write(chip, 0xb9, 2);        /* demand mode (4 bytes/request) */
442         if (runtime->channels == 1) {
443                 if (snd_pcm_format_width(runtime->format) == 8) {
444                         /* 8. bit mono */
445                         snd_es1688_write(chip, 0xb7, 0x51);
446                         snd_es1688_write(chip, 0xb7, 0xd0);
447                 } else {
448                         /* 16. bit mono */
449                         snd_es1688_write(chip, 0xb7, 0x71);
450                         snd_es1688_write(chip, 0xb7, 0xf4);
451                 }
452         } else {
453                 if (snd_pcm_format_width(runtime->format) == 8) {
454                         /* 8. bit stereo */
455                         snd_es1688_write(chip, 0xb7, 0x51);
456                         snd_es1688_write(chip, 0xb7, 0x98);
457                 } else {
458                         /* 16. bit stereo */
459                         snd_es1688_write(chip, 0xb7, 0x71);
460                         snd_es1688_write(chip, 0xb7, 0xbc);
461                 }
462         }
463         snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
464         snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
465         spin_unlock_irqrestore(&chip->reg_lock, flags);
466         /* --- */
467         count = -count;
468         snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
469         spin_lock_irqsave(&chip->reg_lock, flags);
470         snd_es1688_write(chip, 0xa4, (unsigned char) count);
471         snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
472         spin_unlock_irqrestore(&chip->reg_lock, flags);
473         return 0;
474 }
475
476 static int snd_es1688_capture_trigger(snd_pcm_substream_t * substream,
477                                       int cmd)
478 {
479         es1688_t *chip = snd_pcm_substream_chip(substream);
480         return snd_es1688_trigger(chip, cmd, 0x0f);
481 }
482
483 irqreturn_t snd_es1688_interrupt(int irq, void *dev_id, struct pt_regs *regs)
484 {
485         es1688_t *chip = snd_magic_cast(es1688_t, dev_id, return IRQ_NONE);
486
487         if (chip->trigger_value == 0x05)        /* ok.. playback is active */
488                 snd_pcm_period_elapsed(chip->playback_substream);
489         if (chip->trigger_value == 0x0f)        /* ok.. capture is active */
490                 snd_pcm_period_elapsed(chip->capture_substream);
491
492         inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
493         return IRQ_HANDLED;
494 }
495
496 static snd_pcm_uframes_t snd_es1688_playback_pointer(snd_pcm_substream_t * substream)
497 {
498         es1688_t *chip = snd_pcm_substream_chip(substream);
499         size_t ptr;
500         
501         if (chip->trigger_value != 0x05)
502                 return 0;
503         ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
504         return bytes_to_frames(substream->runtime, ptr);
505 }
506
507 static snd_pcm_uframes_t snd_es1688_capture_pointer(snd_pcm_substream_t * substream)
508 {
509         es1688_t *chip = snd_pcm_substream_chip(substream);
510         size_t ptr;
511         
512         if (chip->trigger_value != 0x0f)
513                 return 0;
514         ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
515         return bytes_to_frames(substream->runtime, ptr);
516 }
517
518 /*
519
520  */
521
522 static snd_pcm_hardware_t snd_es1688_playback =
523 {
524         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
525                                  SNDRV_PCM_INFO_MMAP_VALID),
526         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
527         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
528         .rate_min =             4000,
529         .rate_max =             48000,
530         .channels_min =         1,
531         .channels_max =         2,
532         .buffer_bytes_max =     65536,
533         .period_bytes_min =     64,
534         .period_bytes_max =     65536,
535         .periods_min =          1,
536         .periods_max =          1024,
537         .fifo_size =            0,
538 };
539
540 static snd_pcm_hardware_t snd_es1688_capture =
541 {
542         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
543                                  SNDRV_PCM_INFO_MMAP_VALID),
544         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
545         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
546         .rate_min =             4000,
547         .rate_max =             48000,
548         .channels_min =         1,
549         .channels_max =         2,
550         .buffer_bytes_max =     65536,
551         .period_bytes_min =     64,
552         .period_bytes_max =     65536,
553         .periods_min =          1,
554         .periods_max =          1024,
555         .fifo_size =            0,
556 };
557
558 /*
559
560  */
561
562 static int snd_es1688_playback_open(snd_pcm_substream_t * substream)
563 {
564         es1688_t *chip = snd_pcm_substream_chip(substream);
565         snd_pcm_runtime_t *runtime = substream->runtime;
566
567         if (chip->capture_substream != NULL)
568                 return -EAGAIN;
569         chip->playback_substream = substream;
570         runtime->hw = snd_es1688_playback;
571         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
572                                       &hw_constraints_clocks);
573         return 0;
574 }
575
576 static int snd_es1688_capture_open(snd_pcm_substream_t * substream)
577 {
578         es1688_t *chip = snd_pcm_substream_chip(substream);
579         snd_pcm_runtime_t *runtime = substream->runtime;
580
581         if (chip->playback_substream != NULL)
582                 return -EAGAIN;
583         chip->capture_substream = substream;
584         runtime->hw = snd_es1688_capture;
585         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
586                                       &hw_constraints_clocks);
587         return 0;
588 }
589
590 static int snd_es1688_playback_close(snd_pcm_substream_t * substream)
591 {
592         es1688_t *chip = snd_pcm_substream_chip(substream);
593
594         chip->playback_substream = NULL;
595         return 0;
596 }
597
598 static int snd_es1688_capture_close(snd_pcm_substream_t * substream)
599 {
600         es1688_t *chip = snd_pcm_substream_chip(substream);
601
602         chip->capture_substream = NULL;
603         return 0;
604 }
605
606 static int snd_es1688_free(es1688_t *chip)
607 {
608         if (chip->res_port) {
609                 snd_es1688_init(chip, 0);
610                 release_resource(chip->res_port);
611                 kfree_nocheck(chip->res_port);
612         }
613         if (chip->irq >= 0)
614                 free_irq(chip->irq, (void *) chip);
615         if (chip->dma8 >= 0) {
616                 disable_dma(chip->dma8);
617                 free_dma(chip->dma8);
618         }
619         snd_magic_kfree(chip);
620         return 0;
621 }
622
623 static int snd_es1688_dev_free(snd_device_t *device)
624 {
625         es1688_t *chip = snd_magic_cast(es1688_t, device->device_data, return -ENXIO);
626         return snd_es1688_free(chip);
627 }
628
629 static const char *snd_es1688_chip_id(es1688_t *chip)
630 {
631         static char tmp[16];
632         sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
633         return tmp;
634 }
635
636 int snd_es1688_create(snd_card_t * card,
637                       unsigned long port,
638                       unsigned long mpu_port,
639                       int irq,
640                       int mpu_irq,
641                       int dma8,
642                       unsigned short hardware,
643                       es1688_t **rchip)
644 {
645         static snd_device_ops_t ops = {
646                 .dev_free =     snd_es1688_dev_free,
647         };
648                                 
649         es1688_t *chip;
650         int err;
651
652         *rchip = NULL;
653         chip = snd_magic_kcalloc(es1688_t, 0, GFP_KERNEL);
654         if (chip == NULL)
655                 return -ENOMEM;
656         chip->irq = -1;
657         chip->dma8 = -1;
658         
659         if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
660                 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
661                 snd_es1688_free(chip);
662                 return -EBUSY;
663         }
664         if (request_irq(irq, snd_es1688_interrupt, SA_INTERRUPT, "ES1688", (void *) chip)) {
665                 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
666                 snd_es1688_free(chip);
667                 return -EBUSY;
668         }
669         chip->irq = irq;
670         if (request_dma(dma8, "ES1688")) {
671                 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
672                 snd_es1688_free(chip);
673                 return -EBUSY;
674         }
675         chip->dma8 = dma8;
676
677         spin_lock_init(&chip->reg_lock);
678         spin_lock_init(&chip->mixer_lock);
679         chip->card = card;
680         chip->port = port;
681         mpu_port &= ~0x000f;
682         if (mpu_port < 0x300 || mpu_port > 0x330)
683                 mpu_port = 0;
684         chip->mpu_port = mpu_port;
685         chip->mpu_irq = mpu_irq;
686         chip->hardware = hardware;
687
688         if ((err = snd_es1688_probe(chip)) < 0) {
689                 snd_es1688_free(chip);
690                 return err;
691         }
692         if ((err = snd_es1688_init(chip, 1)) < 0) {
693                 snd_es1688_free(chip);
694                 return err;
695         }
696
697         /* Register device */
698         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
699                 snd_es1688_free(chip);
700                 return err;
701         }
702
703         *rchip = chip;
704         return 0;
705 }
706
707 static snd_pcm_ops_t snd_es1688_playback_ops = {
708         .open =                 snd_es1688_playback_open,
709         .close =                snd_es1688_playback_close,
710         .ioctl =                snd_es1688_ioctl,
711         .hw_params =            snd_es1688_hw_params,
712         .hw_free =              snd_es1688_hw_free,
713         .prepare =              snd_es1688_playback_prepare,
714         .trigger =              snd_es1688_playback_trigger,
715         .pointer =              snd_es1688_playback_pointer,
716 };
717
718 static snd_pcm_ops_t snd_es1688_capture_ops = {
719         .open =                 snd_es1688_capture_open,
720         .close =                snd_es1688_capture_close,
721         .ioctl =                snd_es1688_ioctl,
722         .hw_params =            snd_es1688_hw_params,
723         .hw_free =              snd_es1688_hw_free,
724         .prepare =              snd_es1688_capture_prepare,
725         .trigger =              snd_es1688_capture_trigger,
726         .pointer =              snd_es1688_capture_pointer,
727 };
728
729 static void snd_es1688_pcm_free(snd_pcm_t *pcm)
730 {
731         es1688_t *chip = snd_magic_cast(es1688_t, pcm->private_data, return);
732         chip->pcm = NULL;
733         snd_pcm_lib_preallocate_free_for_all(pcm);
734 }
735
736 int snd_es1688_pcm(es1688_t * chip, int device, snd_pcm_t ** rpcm)
737 {
738         snd_pcm_t *pcm;
739         int err;
740
741         if ((err = snd_pcm_new(chip->card, "ESx688", device, 1, 1, &pcm)) < 0)
742                 return err;
743
744         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
745         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
746
747         pcm->private_data = chip;
748         pcm->private_free = snd_es1688_pcm_free;
749         pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
750         sprintf(pcm->name, snd_es1688_chip_id(chip));
751         chip->pcm = pcm;
752
753         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
754                                               snd_dma_isa_data(),
755                                               64*1024, 64*1024);
756
757         if (rpcm)
758                 *rpcm = pcm;
759         return 0;
760 }
761
762 /*
763  *  MIXER part
764  */
765
766 static int snd_es1688_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
767 {
768         static char *texts[9] = {
769                 "Mic", "Mic Master", "CD", "AOUT",
770                 "Mic1", "Mix", "Line", "Master"
771         };
772
773         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
774         uinfo->count = 1;
775         uinfo->value.enumerated.items = 8;
776         if (uinfo->value.enumerated.item > 7)
777                 uinfo->value.enumerated.item = 7;
778         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
779         return 0;
780 }
781
782 static int snd_es1688_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
783 {
784         es1688_t *chip = snd_kcontrol_chip(kcontrol);
785         ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
786         return 0;
787 }
788
789 static int snd_es1688_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
790 {
791         es1688_t *chip = snd_kcontrol_chip(kcontrol);
792         unsigned long flags;
793         unsigned char oval, nval;
794         int change;
795         
796         if (ucontrol->value.enumerated.item[0] > 8)
797                 return -EINVAL;
798         spin_lock_irqsave(&chip->reg_lock, flags);
799         oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
800         nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
801         change = nval != oval;
802         if (change)
803                 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
804         spin_unlock_irqrestore(&chip->reg_lock, flags);
805         return change;
806 }
807
808 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
809 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
810   .info = snd_es1688_info_single, \
811   .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
812   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
813
814 static int snd_es1688_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
815 {
816         int mask = (kcontrol->private_value >> 16) & 0xff;
817
818         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
819         uinfo->count = 1;
820         uinfo->value.integer.min = 0;
821         uinfo->value.integer.max = mask;
822         return 0;
823 }
824
825 static int snd_es1688_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
826 {
827         es1688_t *chip = snd_kcontrol_chip(kcontrol);
828         unsigned long flags;
829         int reg = kcontrol->private_value & 0xff;
830         int shift = (kcontrol->private_value >> 8) & 0xff;
831         int mask = (kcontrol->private_value >> 16) & 0xff;
832         int invert = (kcontrol->private_value >> 24) & 0xff;
833         
834         spin_lock_irqsave(&chip->reg_lock, flags);
835         ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
836         spin_unlock_irqrestore(&chip->reg_lock, flags);
837         if (invert)
838                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
839         return 0;
840 }
841
842 static int snd_es1688_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
843 {
844         es1688_t *chip = snd_kcontrol_chip(kcontrol);
845         unsigned long flags;
846         int reg = kcontrol->private_value & 0xff;
847         int shift = (kcontrol->private_value >> 8) & 0xff;
848         int mask = (kcontrol->private_value >> 16) & 0xff;
849         int invert = (kcontrol->private_value >> 24) & 0xff;
850         int change;
851         unsigned char oval, nval;
852         
853         nval = (ucontrol->value.integer.value[0] & mask);
854         if (invert)
855                 nval = mask - nval;
856         nval <<= shift;
857         spin_lock_irqsave(&chip->reg_lock, flags);
858         oval = snd_es1688_mixer_read(chip, reg);
859         nval = (oval & ~(mask << shift)) | nval;
860         change = nval != oval;
861         if (change)
862                 snd_es1688_mixer_write(chip, reg, nval);
863         spin_unlock_irqrestore(&chip->reg_lock, flags);
864         return change;
865 }
866
867 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
868 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
869   .info = snd_es1688_info_double, \
870   .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
871   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
872
873 static int snd_es1688_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
874 {
875         int mask = (kcontrol->private_value >> 24) & 0xff;
876
877         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
878         uinfo->count = 2;
879         uinfo->value.integer.min = 0;
880         uinfo->value.integer.max = mask;
881         return 0;
882 }
883
884 static int snd_es1688_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
885 {
886         es1688_t *chip = snd_kcontrol_chip(kcontrol);
887         unsigned long flags;
888         int left_reg = kcontrol->private_value & 0xff;
889         int right_reg = (kcontrol->private_value >> 8) & 0xff;
890         int shift_left = (kcontrol->private_value >> 16) & 0x07;
891         int shift_right = (kcontrol->private_value >> 19) & 0x07;
892         int mask = (kcontrol->private_value >> 24) & 0xff;
893         int invert = (kcontrol->private_value >> 22) & 1;
894         unsigned char left, right;
895         
896         spin_lock_irqsave(&chip->reg_lock, flags);
897         if (left_reg < 0xa0)
898                 left = snd_es1688_mixer_read(chip, left_reg);
899         else
900                 left = snd_es1688_read(chip, left_reg);
901         if (left_reg != right_reg) {
902                 if (right_reg < 0xa0) 
903                         right = snd_es1688_mixer_read(chip, right_reg);
904                 else
905                         right = snd_es1688_read(chip, right_reg);
906         } else
907                 right = left;
908         spin_unlock_irqrestore(&chip->reg_lock, flags);
909         ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
910         ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
911         if (invert) {
912                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
913                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
914         }
915         return 0;
916 }
917
918 static int snd_es1688_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
919 {
920         es1688_t *chip = snd_kcontrol_chip(kcontrol);
921         unsigned long flags;
922         int left_reg = kcontrol->private_value & 0xff;
923         int right_reg = (kcontrol->private_value >> 8) & 0xff;
924         int shift_left = (kcontrol->private_value >> 16) & 0x07;
925         int shift_right = (kcontrol->private_value >> 19) & 0x07;
926         int mask = (kcontrol->private_value >> 24) & 0xff;
927         int invert = (kcontrol->private_value >> 22) & 1;
928         int change;
929         unsigned char val1, val2, oval1, oval2;
930         
931         val1 = ucontrol->value.integer.value[0] & mask;
932         val2 = ucontrol->value.integer.value[1] & mask;
933         if (invert) {
934                 val1 = mask - val1;
935                 val2 = mask - val2;
936         }
937         val1 <<= shift_left;
938         val2 <<= shift_right;
939         spin_lock_irqsave(&chip->reg_lock, flags);
940         if (left_reg != right_reg) {
941                 if (left_reg < 0xa0)
942                         oval1 = snd_es1688_mixer_read(chip, left_reg);
943                 else
944                         oval1 = snd_es1688_read(chip, left_reg);
945                 if (right_reg < 0xa0)
946                         oval2 = snd_es1688_mixer_read(chip, right_reg);
947                 else
948                         oval2 = snd_es1688_read(chip, right_reg);
949                 val1 = (oval1 & ~(mask << shift_left)) | val1;
950                 val2 = (oval2 & ~(mask << shift_right)) | val2;
951                 change = val1 != oval1 || val2 != oval2;
952                 if (change) {
953                         if (left_reg < 0xa0)
954                                 snd_es1688_mixer_write(chip, left_reg, val1);
955                         else
956                                 snd_es1688_write(chip, left_reg, val1);
957                         if (right_reg < 0xa0)
958                                 snd_es1688_mixer_write(chip, right_reg, val1);
959                         else
960                                 snd_es1688_write(chip, right_reg, val1);
961                 }
962         } else {
963                 if (left_reg < 0xa0)
964                         oval1 = snd_es1688_mixer_read(chip, left_reg);
965                 else
966                         oval1 = snd_es1688_read(chip, left_reg);
967                 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
968                 change = val1 != oval1;
969                 if (change) {
970                         if (left_reg < 0xa0)
971                                 snd_es1688_mixer_write(chip, left_reg, val1);
972                         else
973                                 snd_es1688_write(chip, left_reg, val1);
974                 }
975                         
976         }
977         spin_unlock_irqrestore(&chip->reg_lock, flags);
978         return change;
979 }
980
981 #define ES1688_CONTROLS (sizeof(snd_es1688_controls)/sizeof(snd_kcontrol_new_t))
982
983 static snd_kcontrol_new_t snd_es1688_controls[] = {
984 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
985 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
986 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
987 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
988 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
989 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
990 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
991 ES1688_SINGLE("PC Speaker Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
992 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
993 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
994 {
995         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
996         .name = "Capture Source",
997         .info = snd_es1688_info_mux,
998         .get = snd_es1688_get_mux,
999         .put = snd_es1688_put_mux,
1000 },
1001 };
1002
1003 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
1004
1005 static unsigned char snd_es1688_init_table[][2] = {
1006         { ES1688_MASTER_DEV, 0 },
1007         { ES1688_PCM_DEV, 0 },
1008         { ES1688_LINE_DEV, 0 },
1009         { ES1688_CD_DEV, 0 },
1010         { ES1688_FM_DEV, 0 },
1011         { ES1688_MIC_DEV, 0 },
1012         { ES1688_AUX_DEV, 0 },
1013         { ES1688_SPEAKER_DEV, 0 },
1014         { ES1688_RECLEV_DEV, 0 },
1015         { ES1688_REC_DEV, 0x17 }
1016 };
1017                                         
1018 int snd_es1688_mixer(es1688_t *chip)
1019 {
1020         snd_card_t *card;
1021         unsigned int idx;
1022         int err;
1023         unsigned char reg, val;
1024
1025         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1026
1027         card = chip->card;
1028
1029         strcpy(card->mixername, snd_es1688_chip_id(chip));
1030
1031         for (idx = 0; idx < ES1688_CONTROLS; idx++) {
1032                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1033                         return err;
1034         }
1035         for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1036                 reg = snd_es1688_init_table[idx][0];
1037                 val = snd_es1688_init_table[idx][1];
1038                 if (reg < 0xa0)
1039                         snd_es1688_mixer_write(chip, reg, val);
1040                 else
1041                         snd_es1688_write(chip, reg, val);
1042         }
1043         return 0;
1044 }
1045
1046 EXPORT_SYMBOL(snd_es1688_mixer_write);
1047 EXPORT_SYMBOL(snd_es1688_mixer_read);
1048 EXPORT_SYMBOL(snd_es1688_interrupt);
1049 EXPORT_SYMBOL(snd_es1688_create);
1050 EXPORT_SYMBOL(snd_es1688_pcm);
1051 EXPORT_SYMBOL(snd_es1688_mixer);
1052
1053 /*
1054  *  INIT part
1055  */
1056
1057 static int __init alsa_es1688_init(void)
1058 {
1059         return 0;
1060 }
1061
1062 static void __exit alsa_es1688_exit(void)
1063 {
1064 }
1065
1066 module_init(alsa_es1688_init)
1067 module_exit(alsa_es1688_exit)