[MTD] [NAND] subpage read feature as a way to increase performance.
[linux-2.6/linux-2.6-openrd.git] / sound / isa / es1688 / es1688_lib.c
blob1e1e575b1db329f87b289cb4cc57bbb954f1bae0
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3 * Routines for control of ESS ES1688/688/488 chip
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.
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.
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
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/ioport.h>
27 #include <sound/core.h>
28 #include <sound/es1688.h>
29 #include <sound/initval.h>
31 #include <asm/io.h>
32 #include <asm/dma.h>
34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
35 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
36 MODULE_LICENSE("GPL");
38 static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
40 int i;
42 for (i = 10000; i; i--)
43 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
44 outb(val, ES1688P(chip, COMMAND));
45 return 1;
47 #ifdef CONFIG_SND_DEBUG
48 printk("snd_es1688_dsp_command: timeout (0x%x)\n", val);
49 #endif
50 return 0;
53 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
55 int i;
57 for (i = 1000; i; i--)
58 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
59 return inb(ES1688P(chip, READ));
60 snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
61 return -ENODEV;
64 static int snd_es1688_write(struct snd_es1688 *chip,
65 unsigned char reg, unsigned char data)
67 if (!snd_es1688_dsp_command(chip, reg))
68 return 0;
69 return snd_es1688_dsp_command(chip, data);
72 static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
74 /* Read a byte from an extended mode register of ES1688 */
75 if (!snd_es1688_dsp_command(chip, 0xc0))
76 return -1;
77 if (!snd_es1688_dsp_command(chip, reg))
78 return -1;
79 return snd_es1688_dsp_get_byte(chip);
82 void snd_es1688_mixer_write(struct snd_es1688 *chip,
83 unsigned char reg, unsigned char data)
85 outb(reg, ES1688P(chip, MIXER_ADDR));
86 udelay(10);
87 outb(data, ES1688P(chip, MIXER_DATA));
88 udelay(10);
91 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
93 unsigned char result;
95 outb(reg, ES1688P(chip, MIXER_ADDR));
96 udelay(10);
97 result = inb(ES1688P(chip, MIXER_DATA));
98 udelay(10);
99 return result;
102 static int snd_es1688_reset(struct snd_es1688 *chip)
104 int i;
106 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
107 udelay(10);
108 outb(0, ES1688P(chip, RESET));
109 udelay(30);
110 for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
111 if (inb(ES1688P(chip, READ)) != 0xaa) {
112 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
113 return -ENODEV;
115 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
116 return 0;
119 static int snd_es1688_probe(struct snd_es1688 *chip)
121 unsigned long flags;
122 unsigned short major, minor, hw;
123 int i;
126 * initialization sequence
129 spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */
130 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
131 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
132 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
133 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
134 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
135 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
136 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
137 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
138 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
139 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
140 inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */
142 if (snd_es1688_reset(chip) < 0) {
143 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
144 spin_unlock_irqrestore(&chip->reg_lock, flags);
145 return -ENODEV;
147 snd_es1688_dsp_command(chip, 0xe7); /* return identification */
149 for (i = 1000, major = minor = 0; i; i--) {
150 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
151 if (major == 0) {
152 major = inb(ES1688P(chip, READ));
153 } else {
154 minor = inb(ES1688P(chip, READ));
159 spin_unlock_irqrestore(&chip->reg_lock, flags);
161 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
163 chip->version = (major << 8) | minor;
164 if (!chip->version)
165 return -ENODEV; /* probably SB */
167 hw = ES1688_HW_AUTO;
168 switch (chip->version & 0xfff0) {
169 case 0x4880:
170 snd_printk("[0x%lx] ESS: AudioDrive ES488 detected, but driver is in another place\n", chip->port);
171 return -ENODEV;
172 case 0x6880:
173 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
174 break;
175 default:
176 snd_printk("[0x%lx] ESS: unknown AudioDrive chip with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version);
177 return -ENODEV;
180 spin_lock_irqsave(&chip->reg_lock, flags);
181 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
182 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
183 spin_unlock_irqrestore(&chip->reg_lock, flags);
185 /* enable joystick, but disable OPL3 */
186 spin_lock_irqsave(&chip->mixer_lock, flags);
187 snd_es1688_mixer_write(chip, 0x40, 0x01);
188 spin_unlock_irqrestore(&chip->mixer_lock, flags);
190 return 0;
193 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
195 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
196 unsigned long flags;
197 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
199 /* ok.. setup MPU-401 port and joystick and OPL3 */
200 cfg = 0x01; /* enable joystick, but disable OPL3 */
201 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
202 tmp = (chip->mpu_port & 0x0f0) >> 4;
203 if (tmp <= 3) {
204 switch (chip->mpu_irq) {
205 case 9:
206 tmp1 = 4;
207 break;
208 case 5:
209 tmp1 = 5;
210 break;
211 case 7:
212 tmp1 = 6;
213 break;
214 case 10:
215 tmp1 = 7;
216 break;
217 default:
218 tmp1 = 0;
220 if (tmp1) {
221 cfg |= (tmp << 3) | (tmp1 << 5);
225 #if 0
226 snd_printk("mpu cfg = 0x%x\n", cfg);
227 #endif
228 spin_lock_irqsave(&chip->reg_lock, flags);
229 snd_es1688_mixer_write(chip, 0x40, cfg);
230 spin_unlock_irqrestore(&chip->reg_lock, flags);
231 /* --- */
232 spin_lock_irqsave(&chip->reg_lock, flags);
233 snd_es1688_read(chip, 0xb1);
234 snd_es1688_read(chip, 0xb2);
235 spin_unlock_irqrestore(&chip->reg_lock, flags);
236 if (enable) {
237 cfg = 0xf0; /* enable only DMA counter interrupt */
238 irq_bits = irqs[chip->irq & 0x0f];
239 if (irq_bits < 0) {
240 snd_printk("[0x%lx] ESS: bad IRQ %d for ES1688 chip!!\n", chip->port, chip->irq);
241 #if 0
242 irq_bits = 0;
243 cfg = 0x10;
244 #endif
245 return -EINVAL;
247 spin_lock_irqsave(&chip->reg_lock, flags);
248 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
249 spin_unlock_irqrestore(&chip->reg_lock, flags);
250 cfg = 0xf0; /* extended mode DMA enable */
251 dma = chip->dma8;
252 if (dma > 3 || dma == 2) {
253 snd_printk("[0x%lx] ESS: bad DMA channel %d for ES1688 chip!!\n", chip->port, dma);
254 #if 0
255 dma_bits = 0;
256 cfg = 0x00; /* disable all DMA */
257 #endif
258 return -EINVAL;
259 } else {
260 dma_bits = dma;
261 if (dma != 3)
262 dma_bits++;
264 spin_lock_irqsave(&chip->reg_lock, flags);
265 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
266 spin_unlock_irqrestore(&chip->reg_lock, flags);
267 } else {
268 spin_lock_irqsave(&chip->reg_lock, flags);
269 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
270 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
271 spin_unlock_irqrestore(&chip->reg_lock, flags);
273 spin_lock_irqsave(&chip->reg_lock, flags);
274 snd_es1688_read(chip, 0xb1);
275 snd_es1688_read(chip, 0xb2);
276 snd_es1688_reset(chip);
277 spin_unlock_irqrestore(&chip->reg_lock, flags);
278 return 0;
285 static struct snd_ratnum clocks[2] = {
287 .num = 795444,
288 .den_min = 1,
289 .den_max = 128,
290 .den_step = 1,
293 .num = 397722,
294 .den_min = 1,
295 .den_max = 128,
296 .den_step = 1,
300 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
301 .nrats = 2,
302 .rats = clocks,
305 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
307 struct snd_pcm_runtime *runtime = substream->runtime;
308 unsigned int bits, divider;
310 if (runtime->rate_num == clocks[0].num)
311 bits = 256 - runtime->rate_den;
312 else
313 bits = 128 - runtime->rate_den;
314 /* set filter register */
315 divider = 256 - 7160000*20/(8*82*runtime->rate);
316 /* write result to hardware */
317 snd_es1688_write(chip, 0xa1, bits);
318 snd_es1688_write(chip, 0xa2, divider);
321 static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
322 unsigned int cmd, void *arg)
324 return snd_pcm_lib_ioctl(substream, cmd, arg);
327 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
329 int val;
331 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
332 value = 0x00;
333 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
334 return -EINVAL;
336 spin_lock(&chip->reg_lock);
337 chip->trigger_value = value;
338 val = snd_es1688_read(chip, 0xb8);
339 if ((val < 0) || (val & 0x0f) == value) {
340 spin_unlock(&chip->reg_lock);
341 return -EINVAL; /* something is wrong */
343 #if 0
344 printk("trigger: val = 0x%x, value = 0x%x\n", val, value);
345 printk("trigger: pointer = 0x%x\n", snd_dma_pointer(chip->dma8, chip->dma_size));
346 #endif
347 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
348 spin_unlock(&chip->reg_lock);
349 return 0;
352 static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
353 struct snd_pcm_hw_params *hw_params)
355 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
358 static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
360 return snd_pcm_lib_free_pages(substream);
363 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
365 unsigned long flags;
366 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
367 struct snd_pcm_runtime *runtime = substream->runtime;
368 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
369 unsigned int count = snd_pcm_lib_period_bytes(substream);
371 chip->dma_size = size;
372 spin_lock_irqsave(&chip->reg_lock, flags);
373 snd_es1688_reset(chip);
374 snd_es1688_set_rate(chip, substream);
375 snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
376 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
377 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
378 if (runtime->channels == 1) {
379 if (snd_pcm_format_width(runtime->format) == 8) {
380 /* 8. bit mono */
381 snd_es1688_write(chip, 0xb6, 0x80);
382 snd_es1688_write(chip, 0xb7, 0x51);
383 snd_es1688_write(chip, 0xb7, 0xd0);
384 } else {
385 /* 16. bit mono */
386 snd_es1688_write(chip, 0xb6, 0x00);
387 snd_es1688_write(chip, 0xb7, 0x71);
388 snd_es1688_write(chip, 0xb7, 0xf4);
390 } else {
391 if (snd_pcm_format_width(runtime->format) == 8) {
392 /* 8. bit stereo */
393 snd_es1688_write(chip, 0xb6, 0x80);
394 snd_es1688_write(chip, 0xb7, 0x51);
395 snd_es1688_write(chip, 0xb7, 0x98);
396 } else {
397 /* 16. bit stereo */
398 snd_es1688_write(chip, 0xb6, 0x00);
399 snd_es1688_write(chip, 0xb7, 0x71);
400 snd_es1688_write(chip, 0xb7, 0xbc);
403 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
404 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
405 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
406 spin_unlock_irqrestore(&chip->reg_lock, flags);
407 /* --- */
408 count = -count;
409 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
410 spin_lock_irqsave(&chip->reg_lock, flags);
411 snd_es1688_write(chip, 0xa4, (unsigned char) count);
412 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
413 spin_unlock_irqrestore(&chip->reg_lock, flags);
414 return 0;
417 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
418 int cmd)
420 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
421 return snd_es1688_trigger(chip, cmd, 0x05);
424 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
426 unsigned long flags;
427 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
428 struct snd_pcm_runtime *runtime = substream->runtime;
429 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
430 unsigned int count = snd_pcm_lib_period_bytes(substream);
432 chip->dma_size = size;
433 spin_lock_irqsave(&chip->reg_lock, flags);
434 snd_es1688_reset(chip);
435 snd_es1688_set_rate(chip, substream);
436 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
437 snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
438 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
439 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
440 if (runtime->channels == 1) {
441 if (snd_pcm_format_width(runtime->format) == 8) {
442 /* 8. bit mono */
443 snd_es1688_write(chip, 0xb7, 0x51);
444 snd_es1688_write(chip, 0xb7, 0xd0);
445 } else {
446 /* 16. bit mono */
447 snd_es1688_write(chip, 0xb7, 0x71);
448 snd_es1688_write(chip, 0xb7, 0xf4);
450 } else {
451 if (snd_pcm_format_width(runtime->format) == 8) {
452 /* 8. bit stereo */
453 snd_es1688_write(chip, 0xb7, 0x51);
454 snd_es1688_write(chip, 0xb7, 0x98);
455 } else {
456 /* 16. bit stereo */
457 snd_es1688_write(chip, 0xb7, 0x71);
458 snd_es1688_write(chip, 0xb7, 0xbc);
461 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
462 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
463 spin_unlock_irqrestore(&chip->reg_lock, flags);
464 /* --- */
465 count = -count;
466 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
467 spin_lock_irqsave(&chip->reg_lock, flags);
468 snd_es1688_write(chip, 0xa4, (unsigned char) count);
469 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
470 spin_unlock_irqrestore(&chip->reg_lock, flags);
471 return 0;
474 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
475 int cmd)
477 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
478 return snd_es1688_trigger(chip, cmd, 0x0f);
481 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
483 struct snd_es1688 *chip = dev_id;
485 if (chip->trigger_value == 0x05) /* ok.. playback is active */
486 snd_pcm_period_elapsed(chip->playback_substream);
487 if (chip->trigger_value == 0x0f) /* ok.. capture is active */
488 snd_pcm_period_elapsed(chip->capture_substream);
490 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
491 return IRQ_HANDLED;
494 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
496 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
497 size_t ptr;
499 if (chip->trigger_value != 0x05)
500 return 0;
501 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
502 return bytes_to_frames(substream->runtime, ptr);
505 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
507 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
508 size_t ptr;
510 if (chip->trigger_value != 0x0f)
511 return 0;
512 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
513 return bytes_to_frames(substream->runtime, ptr);
520 static struct snd_pcm_hardware snd_es1688_playback =
522 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
523 SNDRV_PCM_INFO_MMAP_VALID),
524 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
525 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
526 .rate_min = 4000,
527 .rate_max = 48000,
528 .channels_min = 1,
529 .channels_max = 2,
530 .buffer_bytes_max = 65536,
531 .period_bytes_min = 64,
532 .period_bytes_max = 65536,
533 .periods_min = 1,
534 .periods_max = 1024,
535 .fifo_size = 0,
538 static struct snd_pcm_hardware snd_es1688_capture =
540 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
541 SNDRV_PCM_INFO_MMAP_VALID),
542 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
543 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
544 .rate_min = 4000,
545 .rate_max = 48000,
546 .channels_min = 1,
547 .channels_max = 2,
548 .buffer_bytes_max = 65536,
549 .period_bytes_min = 64,
550 .period_bytes_max = 65536,
551 .periods_min = 1,
552 .periods_max = 1024,
553 .fifo_size = 0,
560 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
562 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
563 struct snd_pcm_runtime *runtime = substream->runtime;
565 if (chip->capture_substream != NULL)
566 return -EAGAIN;
567 chip->playback_substream = substream;
568 runtime->hw = snd_es1688_playback;
569 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
570 &hw_constraints_clocks);
571 return 0;
574 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
576 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
577 struct snd_pcm_runtime *runtime = substream->runtime;
579 if (chip->playback_substream != NULL)
580 return -EAGAIN;
581 chip->capture_substream = substream;
582 runtime->hw = snd_es1688_capture;
583 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
584 &hw_constraints_clocks);
585 return 0;
588 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
590 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
592 chip->playback_substream = NULL;
593 return 0;
596 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
598 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
600 chip->capture_substream = NULL;
601 return 0;
604 static int snd_es1688_free(struct snd_es1688 *chip)
606 if (chip->res_port) {
607 snd_es1688_init(chip, 0);
608 release_and_free_resource(chip->res_port);
610 if (chip->irq >= 0)
611 free_irq(chip->irq, (void *) chip);
612 if (chip->dma8 >= 0) {
613 disable_dma(chip->dma8);
614 free_dma(chip->dma8);
616 kfree(chip);
617 return 0;
620 static int snd_es1688_dev_free(struct snd_device *device)
622 struct snd_es1688 *chip = device->device_data;
623 return snd_es1688_free(chip);
626 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
628 static char tmp[16];
629 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
630 return tmp;
633 int snd_es1688_create(struct snd_card *card,
634 unsigned long port,
635 unsigned long mpu_port,
636 int irq,
637 int mpu_irq,
638 int dma8,
639 unsigned short hardware,
640 struct snd_es1688 **rchip)
642 static struct snd_device_ops ops = {
643 .dev_free = snd_es1688_dev_free,
646 struct snd_es1688 *chip;
647 int err;
649 *rchip = NULL;
650 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
651 if (chip == NULL)
652 return -ENOMEM;
653 chip->irq = -1;
654 chip->dma8 = -1;
656 if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
657 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
658 snd_es1688_free(chip);
659 return -EBUSY;
661 if (request_irq(irq, snd_es1688_interrupt, IRQF_DISABLED, "ES1688", (void *) chip)) {
662 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
663 snd_es1688_free(chip);
664 return -EBUSY;
666 chip->irq = irq;
667 if (request_dma(dma8, "ES1688")) {
668 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
669 snd_es1688_free(chip);
670 return -EBUSY;
672 chip->dma8 = dma8;
674 spin_lock_init(&chip->reg_lock);
675 spin_lock_init(&chip->mixer_lock);
676 chip->card = card;
677 chip->port = port;
678 mpu_port &= ~0x000f;
679 if (mpu_port < 0x300 || mpu_port > 0x330)
680 mpu_port = 0;
681 chip->mpu_port = mpu_port;
682 chip->mpu_irq = mpu_irq;
683 chip->hardware = hardware;
685 if ((err = snd_es1688_probe(chip)) < 0) {
686 snd_es1688_free(chip);
687 return err;
689 if ((err = snd_es1688_init(chip, 1)) < 0) {
690 snd_es1688_free(chip);
691 return err;
694 /* Register device */
695 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
696 snd_es1688_free(chip);
697 return err;
700 *rchip = chip;
701 return 0;
704 static struct snd_pcm_ops snd_es1688_playback_ops = {
705 .open = snd_es1688_playback_open,
706 .close = snd_es1688_playback_close,
707 .ioctl = snd_es1688_ioctl,
708 .hw_params = snd_es1688_hw_params,
709 .hw_free = snd_es1688_hw_free,
710 .prepare = snd_es1688_playback_prepare,
711 .trigger = snd_es1688_playback_trigger,
712 .pointer = snd_es1688_playback_pointer,
715 static struct snd_pcm_ops snd_es1688_capture_ops = {
716 .open = snd_es1688_capture_open,
717 .close = snd_es1688_capture_close,
718 .ioctl = snd_es1688_ioctl,
719 .hw_params = snd_es1688_hw_params,
720 .hw_free = snd_es1688_hw_free,
721 .prepare = snd_es1688_capture_prepare,
722 .trigger = snd_es1688_capture_trigger,
723 .pointer = snd_es1688_capture_pointer,
726 int snd_es1688_pcm(struct snd_es1688 * chip, int device, struct snd_pcm ** rpcm)
728 struct snd_pcm *pcm;
729 int err;
731 if ((err = snd_pcm_new(chip->card, "ESx688", device, 1, 1, &pcm)) < 0)
732 return err;
734 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
735 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
737 pcm->private_data = chip;
738 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
739 sprintf(pcm->name, snd_es1688_chip_id(chip));
740 chip->pcm = pcm;
742 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
743 snd_dma_isa_data(),
744 64*1024, 64*1024);
746 if (rpcm)
747 *rpcm = pcm;
748 return 0;
752 * MIXER part
755 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
757 static char *texts[9] = {
758 "Mic", "Mic Master", "CD", "AOUT",
759 "Mic1", "Mix", "Line", "Master"
762 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
763 uinfo->count = 1;
764 uinfo->value.enumerated.items = 8;
765 if (uinfo->value.enumerated.item > 7)
766 uinfo->value.enumerated.item = 7;
767 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
768 return 0;
771 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
773 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
774 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
775 return 0;
778 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
780 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
781 unsigned long flags;
782 unsigned char oval, nval;
783 int change;
785 if (ucontrol->value.enumerated.item[0] > 8)
786 return -EINVAL;
787 spin_lock_irqsave(&chip->reg_lock, flags);
788 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
789 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
790 change = nval != oval;
791 if (change)
792 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
793 spin_unlock_irqrestore(&chip->reg_lock, flags);
794 return change;
797 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
798 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
799 .info = snd_es1688_info_single, \
800 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
801 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
803 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
805 int mask = (kcontrol->private_value >> 16) & 0xff;
807 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
808 uinfo->count = 1;
809 uinfo->value.integer.min = 0;
810 uinfo->value.integer.max = mask;
811 return 0;
814 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
816 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
817 unsigned long flags;
818 int reg = kcontrol->private_value & 0xff;
819 int shift = (kcontrol->private_value >> 8) & 0xff;
820 int mask = (kcontrol->private_value >> 16) & 0xff;
821 int invert = (kcontrol->private_value >> 24) & 0xff;
823 spin_lock_irqsave(&chip->reg_lock, flags);
824 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
825 spin_unlock_irqrestore(&chip->reg_lock, flags);
826 if (invert)
827 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
828 return 0;
831 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
833 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
834 unsigned long flags;
835 int reg = kcontrol->private_value & 0xff;
836 int shift = (kcontrol->private_value >> 8) & 0xff;
837 int mask = (kcontrol->private_value >> 16) & 0xff;
838 int invert = (kcontrol->private_value >> 24) & 0xff;
839 int change;
840 unsigned char oval, nval;
842 nval = (ucontrol->value.integer.value[0] & mask);
843 if (invert)
844 nval = mask - nval;
845 nval <<= shift;
846 spin_lock_irqsave(&chip->reg_lock, flags);
847 oval = snd_es1688_mixer_read(chip, reg);
848 nval = (oval & ~(mask << shift)) | nval;
849 change = nval != oval;
850 if (change)
851 snd_es1688_mixer_write(chip, reg, nval);
852 spin_unlock_irqrestore(&chip->reg_lock, flags);
853 return change;
856 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
857 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
858 .info = snd_es1688_info_double, \
859 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
860 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
862 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
864 int mask = (kcontrol->private_value >> 24) & 0xff;
866 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
867 uinfo->count = 2;
868 uinfo->value.integer.min = 0;
869 uinfo->value.integer.max = mask;
870 return 0;
873 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
875 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
876 unsigned long flags;
877 int left_reg = kcontrol->private_value & 0xff;
878 int right_reg = (kcontrol->private_value >> 8) & 0xff;
879 int shift_left = (kcontrol->private_value >> 16) & 0x07;
880 int shift_right = (kcontrol->private_value >> 19) & 0x07;
881 int mask = (kcontrol->private_value >> 24) & 0xff;
882 int invert = (kcontrol->private_value >> 22) & 1;
883 unsigned char left, right;
885 spin_lock_irqsave(&chip->reg_lock, flags);
886 if (left_reg < 0xa0)
887 left = snd_es1688_mixer_read(chip, left_reg);
888 else
889 left = snd_es1688_read(chip, left_reg);
890 if (left_reg != right_reg) {
891 if (right_reg < 0xa0)
892 right = snd_es1688_mixer_read(chip, right_reg);
893 else
894 right = snd_es1688_read(chip, right_reg);
895 } else
896 right = left;
897 spin_unlock_irqrestore(&chip->reg_lock, flags);
898 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
899 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
900 if (invert) {
901 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
902 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
904 return 0;
907 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
909 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
910 unsigned long flags;
911 int left_reg = kcontrol->private_value & 0xff;
912 int right_reg = (kcontrol->private_value >> 8) & 0xff;
913 int shift_left = (kcontrol->private_value >> 16) & 0x07;
914 int shift_right = (kcontrol->private_value >> 19) & 0x07;
915 int mask = (kcontrol->private_value >> 24) & 0xff;
916 int invert = (kcontrol->private_value >> 22) & 1;
917 int change;
918 unsigned char val1, val2, oval1, oval2;
920 val1 = ucontrol->value.integer.value[0] & mask;
921 val2 = ucontrol->value.integer.value[1] & mask;
922 if (invert) {
923 val1 = mask - val1;
924 val2 = mask - val2;
926 val1 <<= shift_left;
927 val2 <<= shift_right;
928 spin_lock_irqsave(&chip->reg_lock, flags);
929 if (left_reg != right_reg) {
930 if (left_reg < 0xa0)
931 oval1 = snd_es1688_mixer_read(chip, left_reg);
932 else
933 oval1 = snd_es1688_read(chip, left_reg);
934 if (right_reg < 0xa0)
935 oval2 = snd_es1688_mixer_read(chip, right_reg);
936 else
937 oval2 = snd_es1688_read(chip, right_reg);
938 val1 = (oval1 & ~(mask << shift_left)) | val1;
939 val2 = (oval2 & ~(mask << shift_right)) | val2;
940 change = val1 != oval1 || val2 != oval2;
941 if (change) {
942 if (left_reg < 0xa0)
943 snd_es1688_mixer_write(chip, left_reg, val1);
944 else
945 snd_es1688_write(chip, left_reg, val1);
946 if (right_reg < 0xa0)
947 snd_es1688_mixer_write(chip, right_reg, val1);
948 else
949 snd_es1688_write(chip, right_reg, val1);
951 } else {
952 if (left_reg < 0xa0)
953 oval1 = snd_es1688_mixer_read(chip, left_reg);
954 else
955 oval1 = snd_es1688_read(chip, left_reg);
956 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
957 change = val1 != oval1;
958 if (change) {
959 if (left_reg < 0xa0)
960 snd_es1688_mixer_write(chip, left_reg, val1);
961 else
962 snd_es1688_write(chip, left_reg, val1);
966 spin_unlock_irqrestore(&chip->reg_lock, flags);
967 return change;
970 static struct snd_kcontrol_new snd_es1688_controls[] = {
971 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
972 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
973 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
974 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
975 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
976 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
977 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
978 ES1688_SINGLE("PC Speaker Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
979 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
980 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
982 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
983 .name = "Capture Source",
984 .info = snd_es1688_info_mux,
985 .get = snd_es1688_get_mux,
986 .put = snd_es1688_put_mux,
990 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
992 static unsigned char snd_es1688_init_table[][2] = {
993 { ES1688_MASTER_DEV, 0 },
994 { ES1688_PCM_DEV, 0 },
995 { ES1688_LINE_DEV, 0 },
996 { ES1688_CD_DEV, 0 },
997 { ES1688_FM_DEV, 0 },
998 { ES1688_MIC_DEV, 0 },
999 { ES1688_AUX_DEV, 0 },
1000 { ES1688_SPEAKER_DEV, 0 },
1001 { ES1688_RECLEV_DEV, 0 },
1002 { ES1688_REC_DEV, 0x17 }
1005 int snd_es1688_mixer(struct snd_es1688 *chip)
1007 struct snd_card *card;
1008 unsigned int idx;
1009 int err;
1010 unsigned char reg, val;
1012 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1014 card = chip->card;
1016 strcpy(card->mixername, snd_es1688_chip_id(chip));
1018 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
1019 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1020 return err;
1022 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1023 reg = snd_es1688_init_table[idx][0];
1024 val = snd_es1688_init_table[idx][1];
1025 if (reg < 0xa0)
1026 snd_es1688_mixer_write(chip, reg, val);
1027 else
1028 snd_es1688_write(chip, reg, val);
1030 return 0;
1033 EXPORT_SYMBOL(snd_es1688_mixer_write);
1034 EXPORT_SYMBOL(snd_es1688_create);
1035 EXPORT_SYMBOL(snd_es1688_pcm);
1036 EXPORT_SYMBOL(snd_es1688_mixer);
1039 * INIT part
1042 static int __init alsa_es1688_init(void)
1044 return 0;
1047 static void __exit alsa_es1688_exit(void)
1051 module_init(alsa_es1688_init)
1052 module_exit(alsa_es1688_exit)