2 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <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>
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
36 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
37 MODULE_CLASSES("{sound}");
38 MODULE_LICENSE("GPL");
40 static int snd_es1688_dsp_command(es1688_t *chip, unsigned char val)
44 for (i = 10000; i; i--)
45 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
46 outb(val, ES1688P(chip, COMMAND));
49 #ifdef CONFIG_SND_DEBUG
50 printk("snd_es1688_dsp_command: timeout (0x%x)\n", val);
55 static int snd_es1688_dsp_get_byte(es1688_t *chip)
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)));
66 static int snd_es1688_write(es1688_t *chip,
67 unsigned char reg, unsigned char data)
69 if (!snd_es1688_dsp_command(chip, reg))
71 return snd_es1688_dsp_command(chip, data);
74 int snd_es1688_read(es1688_t *chip, unsigned char reg)
76 /* Read a byte from an extended mode register of ES1688 */
77 if (!snd_es1688_dsp_command(chip, 0xc0))
79 if (!snd_es1688_dsp_command(chip, reg))
81 return snd_es1688_dsp_get_byte(chip);
84 void snd_es1688_mixer_write(es1688_t *chip,
85 unsigned char reg, unsigned char data)
87 outb(reg, ES1688P(chip, MIXER_ADDR));
89 outb(data, ES1688P(chip, MIXER_DATA));
93 unsigned char snd_es1688_mixer_read(es1688_t *chip, unsigned char reg)
97 outb(reg, ES1688P(chip, MIXER_ADDR));
99 result = inb(ES1688P(chip, MIXER_DATA));
104 static int snd_es1688_reset(es1688_t *chip)
108 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
110 outb(0, ES1688P(chip, RESET));
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);
117 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
121 static int snd_es1688_probe(es1688_t *chip)
124 unsigned short major, minor, hw;
128 * initialization sequence
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 */
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);
149 snd_es1688_dsp_command(chip, 0xe7); /* return identification */
151 for (i = 1000, major = minor = 0; i; i--) {
152 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
154 major = inb(ES1688P(chip, READ));
156 minor = inb(ES1688P(chip, READ));
161 spin_unlock_irqrestore(&chip->reg_lock, flags);
163 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
165 chip->version = (major << 8) | minor;
167 return -ENODEV; /* probably SB */
170 switch (chip->version & 0xfff0) {
172 snd_printk("[0x%lx] ESS: AudioDrive ES488 detected, but driver is in another place\n", chip->port);
175 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
178 snd_printk("[0x%lx] ESS: unknown AudioDrive chip with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version);
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);
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);
195 static int snd_es1688_init(es1688_t * chip, int enable)
197 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
199 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
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;
206 switch (chip->mpu_irq) {
223 cfg |= (tmp << 3) | (tmp1 << 5);
228 snd_printk("mpu cfg = 0x%x\n", cfg);
230 spin_lock_irqsave(&chip->reg_lock, flags);
231 snd_es1688_mixer_write(chip, 0x40, cfg);
232 spin_unlock_irqrestore(&chip->reg_lock, flags);
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);
239 cfg = 0xf0; /* enable only DMA counter interrupt */
240 irq_bits = irqs[chip->irq & 0x0f];
242 snd_printk("[0x%lx] ESS: bad IRQ %d for ES1688 chip!!\n", chip->port, chip->irq);
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 */
254 if (dma > 3 || dma == 2) {
255 snd_printk("[0x%lx] ESS: bad DMA channel %d for ES1688 chip!!\n", chip->port, dma);
258 cfg = 0x00; /* disable all DMA */
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);
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);
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);
287 static ratnum_t clocks[2] = {
302 static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks = {
307 static void snd_es1688_set_rate(es1688_t *chip, snd_pcm_substream_t *substream)
309 snd_pcm_runtime_t *runtime = substream->runtime;
310 unsigned int bits, divider;
312 if (runtime->rate_num == clocks[0].num)
313 bits = 256 - runtime->rate_den;
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);
323 static int snd_es1688_ioctl(snd_pcm_substream_t * substream,
324 unsigned int cmd, void *arg)
326 return snd_pcm_lib_ioctl(substream, cmd, arg);
329 static int snd_es1688_trigger(es1688_t *chip, int cmd, unsigned char value)
333 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
335 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
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 */
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));
349 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
350 spin_unlock(&chip->reg_lock);
354 static int snd_es1688_hw_params(snd_pcm_substream_t * substream,
355 snd_pcm_hw_params_t * hw_params)
357 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
360 static int snd_es1688_hw_free(snd_pcm_substream_t * substream)
362 return snd_pcm_lib_free_pages(substream);
365 static int snd_es1688_playback_prepare(snd_pcm_substream_t * substream)
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);
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) {
383 snd_es1688_write(chip, 0xb6, 0x80);
384 snd_es1688_write(chip, 0xb7, 0x51);
385 snd_es1688_write(chip, 0xb7, 0xd0);
388 snd_es1688_write(chip, 0xb6, 0x00);
389 snd_es1688_write(chip, 0xb7, 0x71);
390 snd_es1688_write(chip, 0xb7, 0xf4);
393 if (snd_pcm_format_width(runtime->format) == 8) {
395 snd_es1688_write(chip, 0xb6, 0x80);
396 snd_es1688_write(chip, 0xb7, 0x51);
397 snd_es1688_write(chip, 0xb7, 0x98);
400 snd_es1688_write(chip, 0xb6, 0x00);
401 snd_es1688_write(chip, 0xb7, 0x71);
402 snd_es1688_write(chip, 0xb7, 0xbc);
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);
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);
419 static int snd_es1688_playback_trigger(snd_pcm_substream_t * substream,
422 es1688_t *chip = snd_pcm_substream_chip(substream);
423 return snd_es1688_trigger(chip, cmd, 0x05);
426 static int snd_es1688_capture_prepare(snd_pcm_substream_t * substream)
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);
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) {
445 snd_es1688_write(chip, 0xb7, 0x51);
446 snd_es1688_write(chip, 0xb7, 0xd0);
449 snd_es1688_write(chip, 0xb7, 0x71);
450 snd_es1688_write(chip, 0xb7, 0xf4);
453 if (snd_pcm_format_width(runtime->format) == 8) {
455 snd_es1688_write(chip, 0xb7, 0x51);
456 snd_es1688_write(chip, 0xb7, 0x98);
459 snd_es1688_write(chip, 0xb7, 0x71);
460 snd_es1688_write(chip, 0xb7, 0xbc);
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);
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);
476 static int snd_es1688_capture_trigger(snd_pcm_substream_t * substream,
479 es1688_t *chip = snd_pcm_substream_chip(substream);
480 return snd_es1688_trigger(chip, cmd, 0x0f);
483 irqreturn_t snd_es1688_interrupt(int irq, void *dev_id, struct pt_regs *regs)
485 es1688_t *chip = snd_magic_cast(es1688_t, dev_id, return IRQ_NONE);
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);
492 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
496 static snd_pcm_uframes_t snd_es1688_playback_pointer(snd_pcm_substream_t * substream)
498 es1688_t *chip = snd_pcm_substream_chip(substream);
501 if (chip->trigger_value != 0x05)
503 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
504 return bytes_to_frames(substream->runtime, ptr);
507 static snd_pcm_uframes_t snd_es1688_capture_pointer(snd_pcm_substream_t * substream)
509 es1688_t *chip = snd_pcm_substream_chip(substream);
512 if (chip->trigger_value != 0x0f)
514 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
515 return bytes_to_frames(substream->runtime, ptr);
522 static snd_pcm_hardware_t snd_es1688_playback =
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,
532 .buffer_bytes_max = 65536,
533 .period_bytes_min = 64,
534 .period_bytes_max = 65536,
540 static snd_pcm_hardware_t snd_es1688_capture =
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,
550 .buffer_bytes_max = 65536,
551 .period_bytes_min = 64,
552 .period_bytes_max = 65536,
562 static int snd_es1688_playback_open(snd_pcm_substream_t * substream)
564 es1688_t *chip = snd_pcm_substream_chip(substream);
565 snd_pcm_runtime_t *runtime = substream->runtime;
567 if (chip->capture_substream != NULL)
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);
576 static int snd_es1688_capture_open(snd_pcm_substream_t * substream)
578 es1688_t *chip = snd_pcm_substream_chip(substream);
579 snd_pcm_runtime_t *runtime = substream->runtime;
581 if (chip->playback_substream != NULL)
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);
590 static int snd_es1688_playback_close(snd_pcm_substream_t * substream)
592 es1688_t *chip = snd_pcm_substream_chip(substream);
594 chip->playback_substream = NULL;
598 static int snd_es1688_capture_close(snd_pcm_substream_t * substream)
600 es1688_t *chip = snd_pcm_substream_chip(substream);
602 chip->capture_substream = NULL;
606 static int snd_es1688_free(es1688_t *chip)
608 if (chip->res_port) {
609 snd_es1688_init(chip, 0);
610 release_resource(chip->res_port);
611 kfree_nocheck(chip->res_port);
614 free_irq(chip->irq, (void *) chip);
615 if (chip->dma8 >= 0) {
616 disable_dma(chip->dma8);
617 free_dma(chip->dma8);
619 snd_magic_kfree(chip);
623 static int snd_es1688_dev_free(snd_device_t *device)
625 es1688_t *chip = snd_magic_cast(es1688_t, device->device_data, return -ENXIO);
626 return snd_es1688_free(chip);
629 static const char *snd_es1688_chip_id(es1688_t *chip)
632 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
636 int snd_es1688_create(snd_card_t * card,
638 unsigned long mpu_port,
642 unsigned short hardware,
645 static snd_device_ops_t ops = {
646 .dev_free = snd_es1688_dev_free,
653 chip = snd_magic_kcalloc(es1688_t, 0, GFP_KERNEL);
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);
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);
670 if (request_dma(dma8, "ES1688")) {
671 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
672 snd_es1688_free(chip);
677 spin_lock_init(&chip->reg_lock);
678 spin_lock_init(&chip->mixer_lock);
682 if (mpu_port < 0x300 || mpu_port > 0x330)
684 chip->mpu_port = mpu_port;
685 chip->mpu_irq = mpu_irq;
686 chip->hardware = hardware;
688 if ((err = snd_es1688_probe(chip)) < 0) {
689 snd_es1688_free(chip);
692 if ((err = snd_es1688_init(chip, 1)) < 0) {
693 snd_es1688_free(chip);
697 /* Register device */
698 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
699 snd_es1688_free(chip);
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,
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,
729 static void snd_es1688_pcm_free(snd_pcm_t *pcm)
731 es1688_t *chip = snd_magic_cast(es1688_t, pcm->private_data, return);
733 snd_pcm_lib_preallocate_free_for_all(pcm);
736 int snd_es1688_pcm(es1688_t * chip, int device, snd_pcm_t ** rpcm)
741 if ((err = snd_pcm_new(chip->card, "ESx688", device, 1, 1, &pcm)) < 0)
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);
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));
753 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
766 static int snd_es1688_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
768 static char *texts[9] = {
769 "Mic", "Mic Master", "CD", "AOUT",
770 "Mic1", "Mix", "Line", "Master"
773 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
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]);
782 static int snd_es1688_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
784 es1688_t *chip = snd_kcontrol_chip(kcontrol);
785 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
789 static int snd_es1688_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
791 es1688_t *chip = snd_kcontrol_chip(kcontrol);
793 unsigned char oval, nval;
796 if (ucontrol->value.enumerated.item[0] > 8)
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;
803 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
804 spin_unlock_irqrestore(&chip->reg_lock, flags);
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) }
814 static int snd_es1688_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
816 int mask = (kcontrol->private_value >> 16) & 0xff;
818 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
820 uinfo->value.integer.min = 0;
821 uinfo->value.integer.max = mask;
825 static int snd_es1688_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
827 es1688_t *chip = snd_kcontrol_chip(kcontrol);
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;
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);
838 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
842 static int snd_es1688_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
844 es1688_t *chip = snd_kcontrol_chip(kcontrol);
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;
851 unsigned char oval, nval;
853 nval = (ucontrol->value.integer.value[0] & mask);
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;
862 snd_es1688_mixer_write(chip, reg, nval);
863 spin_unlock_irqrestore(&chip->reg_lock, flags);
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) }
873 static int snd_es1688_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
875 int mask = (kcontrol->private_value >> 24) & 0xff;
877 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
879 uinfo->value.integer.min = 0;
880 uinfo->value.integer.max = mask;
884 static int snd_es1688_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
886 es1688_t *chip = snd_kcontrol_chip(kcontrol);
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;
896 spin_lock_irqsave(&chip->reg_lock, flags);
898 left = snd_es1688_mixer_read(chip, left_reg);
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);
905 right = snd_es1688_read(chip, right_reg);
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;
912 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
913 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
918 static int snd_es1688_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
920 es1688_t *chip = snd_kcontrol_chip(kcontrol);
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;
929 unsigned char val1, val2, oval1, oval2;
931 val1 = ucontrol->value.integer.value[0] & mask;
932 val2 = ucontrol->value.integer.value[1] & mask;
938 val2 <<= shift_right;
939 spin_lock_irqsave(&chip->reg_lock, flags);
940 if (left_reg != right_reg) {
942 oval1 = snd_es1688_mixer_read(chip, left_reg);
944 oval1 = snd_es1688_read(chip, left_reg);
945 if (right_reg < 0xa0)
946 oval2 = snd_es1688_mixer_read(chip, right_reg);
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;
954 snd_es1688_mixer_write(chip, left_reg, val1);
956 snd_es1688_write(chip, left_reg, val1);
957 if (right_reg < 0xa0)
958 snd_es1688_mixer_write(chip, right_reg, val1);
960 snd_es1688_write(chip, right_reg, val1);
964 oval1 = snd_es1688_mixer_read(chip, left_reg);
966 oval1 = snd_es1688_read(chip, left_reg);
967 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
968 change = val1 != oval1;
971 snd_es1688_mixer_write(chip, left_reg, val1);
973 snd_es1688_write(chip, left_reg, val1);
977 spin_unlock_irqrestore(&chip->reg_lock, flags);
981 #define ES1688_CONTROLS (sizeof(snd_es1688_controls)/sizeof(snd_kcontrol_new_t))
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),
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,
1003 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
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 }
1018 int snd_es1688_mixer(es1688_t *chip)
1023 unsigned char reg, val;
1025 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1029 strcpy(card->mixername, snd_es1688_chip_id(chip));
1031 for (idx = 0; idx < ES1688_CONTROLS; idx++) {
1032 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
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];
1039 snd_es1688_mixer_write(chip, reg, val);
1041 snd_es1688_write(chip, reg, val);
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);
1057 static int __init alsa_es1688_init(void)
1062 static void __exit alsa_es1688_exit(void)
1066 module_init(alsa_es1688_init)
1067 module_exit(alsa_es1688_exit)