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>
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)
42 for (i = 10000; i; i--)
43 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
44 outb(val, ES1688P(chip, COMMAND));
47 #ifdef CONFIG_SND_DEBUG
48 printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
53 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
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)));
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))
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))
77 if (!snd_es1688_dsp_command(chip, reg))
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));
87 outb(data, ES1688P(chip, MIXER_DATA));
91 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
95 outb(reg, ES1688P(chip, MIXER_ADDR));
97 result = inb(ES1688P(chip, MIXER_DATA));
102 static int snd_es1688_reset(struct snd_es1688 *chip)
106 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
108 outb(0, ES1688P(chip, RESET));
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);
115 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
119 static int snd_es1688_probe(struct snd_es1688 *chip)
122 unsigned short major, minor, hw;
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);
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) {
152 major = inb(ES1688P(chip, READ));
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;
165 return -ENODEV; /* probably SB */
168 switch (chip->version & 0xfff0) {
170 snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
171 "but driver is in another place\n", chip->port);
174 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
177 snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
178 "with version 0x%x (Jazz16 soundcard?)\n",
179 chip->port, chip->version);
183 spin_lock_irqsave(&chip->reg_lock, flags);
184 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
185 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
186 spin_unlock_irqrestore(&chip->reg_lock, flags);
188 /* enable joystick, but disable OPL3 */
189 spin_lock_irqsave(&chip->mixer_lock, flags);
190 snd_es1688_mixer_write(chip, 0x40, 0x01);
191 spin_unlock_irqrestore(&chip->mixer_lock, flags);
196 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
198 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
200 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
202 /* ok.. setup MPU-401 port and joystick and OPL3 */
203 cfg = 0x01; /* enable joystick, but disable OPL3 */
204 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
205 tmp = (chip->mpu_port & 0x0f0) >> 4;
207 switch (chip->mpu_irq) {
224 cfg |= (tmp << 3) | (tmp1 << 5);
229 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
231 spin_lock_irqsave(&chip->reg_lock, flags);
232 snd_es1688_mixer_write(chip, 0x40, cfg);
233 spin_unlock_irqrestore(&chip->reg_lock, flags);
235 spin_lock_irqsave(&chip->reg_lock, flags);
236 snd_es1688_read(chip, 0xb1);
237 snd_es1688_read(chip, 0xb2);
238 spin_unlock_irqrestore(&chip->reg_lock, flags);
240 cfg = 0xf0; /* enable only DMA counter interrupt */
241 irq_bits = irqs[chip->irq & 0x0f];
243 snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
244 "for ES1688 chip!!\n",
245 chip->port, chip->irq);
252 spin_lock_irqsave(&chip->reg_lock, flags);
253 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
254 spin_unlock_irqrestore(&chip->reg_lock, flags);
255 cfg = 0xf0; /* extended mode DMA enable */
257 if (dma > 3 || dma == 2) {
258 snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
259 "for ES1688 chip!!\n", chip->port, dma);
262 cfg = 0x00; /* disable all DMA */
270 spin_lock_irqsave(&chip->reg_lock, flags);
271 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
272 spin_unlock_irqrestore(&chip->reg_lock, flags);
274 spin_lock_irqsave(&chip->reg_lock, flags);
275 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
276 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
277 spin_unlock_irqrestore(&chip->reg_lock, flags);
279 spin_lock_irqsave(&chip->reg_lock, flags);
280 snd_es1688_read(chip, 0xb1);
281 snd_es1688_read(chip, 0xb2);
282 snd_es1688_reset(chip);
283 spin_unlock_irqrestore(&chip->reg_lock, flags);
291 static struct snd_ratnum clocks[2] = {
306 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
311 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 unsigned int bits, divider;
316 if (runtime->rate_num == clocks[0].num)
317 bits = 256 - runtime->rate_den;
319 bits = 128 - runtime->rate_den;
320 /* set filter register */
321 divider = 256 - 7160000*20/(8*82*runtime->rate);
322 /* write result to hardware */
323 snd_es1688_write(chip, 0xa1, bits);
324 snd_es1688_write(chip, 0xa2, divider);
327 static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
328 unsigned int cmd, void *arg)
330 return snd_pcm_lib_ioctl(substream, cmd, arg);
333 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
337 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
339 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
342 spin_lock(&chip->reg_lock);
343 chip->trigger_value = value;
344 val = snd_es1688_read(chip, 0xb8);
345 if ((val < 0) || (val & 0x0f) == value) {
346 spin_unlock(&chip->reg_lock);
347 return -EINVAL; /* something is wrong */
350 printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
351 printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
352 snd_dma_pointer(chip->dma8, chip->dma_size));
354 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
355 spin_unlock(&chip->reg_lock);
359 static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
360 struct snd_pcm_hw_params *hw_params)
362 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
365 static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
367 return snd_pcm_lib_free_pages(substream);
370 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
373 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
376 unsigned int count = snd_pcm_lib_period_bytes(substream);
378 chip->dma_size = size;
379 spin_lock_irqsave(&chip->reg_lock, flags);
380 snd_es1688_reset(chip);
381 snd_es1688_set_rate(chip, substream);
382 snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
383 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
384 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
385 if (runtime->channels == 1) {
386 if (snd_pcm_format_width(runtime->format) == 8) {
388 snd_es1688_write(chip, 0xb6, 0x80);
389 snd_es1688_write(chip, 0xb7, 0x51);
390 snd_es1688_write(chip, 0xb7, 0xd0);
393 snd_es1688_write(chip, 0xb6, 0x00);
394 snd_es1688_write(chip, 0xb7, 0x71);
395 snd_es1688_write(chip, 0xb7, 0xf4);
398 if (snd_pcm_format_width(runtime->format) == 8) {
400 snd_es1688_write(chip, 0xb6, 0x80);
401 snd_es1688_write(chip, 0xb7, 0x51);
402 snd_es1688_write(chip, 0xb7, 0x98);
405 snd_es1688_write(chip, 0xb6, 0x00);
406 snd_es1688_write(chip, 0xb7, 0x71);
407 snd_es1688_write(chip, 0xb7, 0xbc);
410 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
411 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
412 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
413 spin_unlock_irqrestore(&chip->reg_lock, flags);
416 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
417 spin_lock_irqsave(&chip->reg_lock, flags);
418 snd_es1688_write(chip, 0xa4, (unsigned char) count);
419 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
420 spin_unlock_irqrestore(&chip->reg_lock, flags);
424 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
427 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
428 return snd_es1688_trigger(chip, cmd, 0x05);
431 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
434 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
435 struct snd_pcm_runtime *runtime = substream->runtime;
436 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
437 unsigned int count = snd_pcm_lib_period_bytes(substream);
439 chip->dma_size = size;
440 spin_lock_irqsave(&chip->reg_lock, flags);
441 snd_es1688_reset(chip);
442 snd_es1688_set_rate(chip, substream);
443 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
444 snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
445 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
446 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
447 if (runtime->channels == 1) {
448 if (snd_pcm_format_width(runtime->format) == 8) {
450 snd_es1688_write(chip, 0xb7, 0x51);
451 snd_es1688_write(chip, 0xb7, 0xd0);
454 snd_es1688_write(chip, 0xb7, 0x71);
455 snd_es1688_write(chip, 0xb7, 0xf4);
458 if (snd_pcm_format_width(runtime->format) == 8) {
460 snd_es1688_write(chip, 0xb7, 0x51);
461 snd_es1688_write(chip, 0xb7, 0x98);
464 snd_es1688_write(chip, 0xb7, 0x71);
465 snd_es1688_write(chip, 0xb7, 0xbc);
468 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
469 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
470 spin_unlock_irqrestore(&chip->reg_lock, flags);
473 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
474 spin_lock_irqsave(&chip->reg_lock, flags);
475 snd_es1688_write(chip, 0xa4, (unsigned char) count);
476 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
477 spin_unlock_irqrestore(&chip->reg_lock, flags);
481 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
484 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
485 return snd_es1688_trigger(chip, cmd, 0x0f);
488 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
490 struct snd_es1688 *chip = dev_id;
492 if (chip->trigger_value == 0x05) /* ok.. playback is active */
493 snd_pcm_period_elapsed(chip->playback_substream);
494 if (chip->trigger_value == 0x0f) /* ok.. capture is active */
495 snd_pcm_period_elapsed(chip->capture_substream);
497 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
501 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
503 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
506 if (chip->trigger_value != 0x05)
508 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
509 return bytes_to_frames(substream->runtime, ptr);
512 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
514 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
517 if (chip->trigger_value != 0x0f)
519 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
520 return bytes_to_frames(substream->runtime, ptr);
527 static struct snd_pcm_hardware snd_es1688_playback =
529 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
530 SNDRV_PCM_INFO_MMAP_VALID),
531 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
532 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
537 .buffer_bytes_max = 65536,
538 .period_bytes_min = 64,
539 .period_bytes_max = 65536,
545 static struct snd_pcm_hardware snd_es1688_capture =
547 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
548 SNDRV_PCM_INFO_MMAP_VALID),
549 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
550 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
555 .buffer_bytes_max = 65536,
556 .period_bytes_min = 64,
557 .period_bytes_max = 65536,
567 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
569 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
570 struct snd_pcm_runtime *runtime = substream->runtime;
572 if (chip->capture_substream != NULL)
574 chip->playback_substream = substream;
575 runtime->hw = snd_es1688_playback;
576 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
577 &hw_constraints_clocks);
581 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
583 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
584 struct snd_pcm_runtime *runtime = substream->runtime;
586 if (chip->playback_substream != NULL)
588 chip->capture_substream = substream;
589 runtime->hw = snd_es1688_capture;
590 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
591 &hw_constraints_clocks);
595 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
597 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
599 chip->playback_substream = NULL;
603 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
605 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
607 chip->capture_substream = NULL;
611 static int snd_es1688_free(struct snd_es1688 *chip)
613 if (chip->res_port) {
614 snd_es1688_init(chip, 0);
615 release_and_free_resource(chip->res_port);
618 free_irq(chip->irq, (void *) chip);
619 if (chip->dma8 >= 0) {
620 disable_dma(chip->dma8);
621 free_dma(chip->dma8);
627 static int snd_es1688_dev_free(struct snd_device *device)
629 struct snd_es1688 *chip = device->device_data;
630 return snd_es1688_free(chip);
633 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
636 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
640 int snd_es1688_create(struct snd_card *card,
642 unsigned long mpu_port,
646 unsigned short hardware,
647 struct snd_es1688 **rchip)
649 static struct snd_device_ops ops = {
650 .dev_free = snd_es1688_dev_free,
653 struct snd_es1688 *chip;
657 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
663 if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
664 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
665 snd_es1688_free(chip);
668 if (request_irq(irq, snd_es1688_interrupt, IRQF_DISABLED, "ES1688", (void *) chip)) {
669 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
670 snd_es1688_free(chip);
674 if (request_dma(dma8, "ES1688")) {
675 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
676 snd_es1688_free(chip);
681 spin_lock_init(&chip->reg_lock);
682 spin_lock_init(&chip->mixer_lock);
686 if (mpu_port < 0x300 || mpu_port > 0x330)
688 chip->mpu_port = mpu_port;
689 chip->mpu_irq = mpu_irq;
690 chip->hardware = hardware;
692 if ((err = snd_es1688_probe(chip)) < 0) {
693 snd_es1688_free(chip);
696 if ((err = snd_es1688_init(chip, 1)) < 0) {
697 snd_es1688_free(chip);
701 /* Register device */
702 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
703 snd_es1688_free(chip);
711 static struct snd_pcm_ops snd_es1688_playback_ops = {
712 .open = snd_es1688_playback_open,
713 .close = snd_es1688_playback_close,
714 .ioctl = snd_es1688_ioctl,
715 .hw_params = snd_es1688_hw_params,
716 .hw_free = snd_es1688_hw_free,
717 .prepare = snd_es1688_playback_prepare,
718 .trigger = snd_es1688_playback_trigger,
719 .pointer = snd_es1688_playback_pointer,
722 static struct snd_pcm_ops snd_es1688_capture_ops = {
723 .open = snd_es1688_capture_open,
724 .close = snd_es1688_capture_close,
725 .ioctl = snd_es1688_ioctl,
726 .hw_params = snd_es1688_hw_params,
727 .hw_free = snd_es1688_hw_free,
728 .prepare = snd_es1688_capture_prepare,
729 .trigger = snd_es1688_capture_trigger,
730 .pointer = snd_es1688_capture_pointer,
733 int snd_es1688_pcm(struct snd_es1688 * chip, int device, struct snd_pcm ** rpcm)
738 if ((err = snd_pcm_new(chip->card, "ESx688", device, 1, 1, &pcm)) < 0)
741 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
742 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
744 pcm->private_data = chip;
745 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
746 sprintf(pcm->name, snd_es1688_chip_id(chip));
749 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
762 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
764 static char *texts[9] = {
765 "Mic", "Mic Master", "CD", "AOUT",
766 "Mic1", "Mix", "Line", "Master"
769 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
771 uinfo->value.enumerated.items = 8;
772 if (uinfo->value.enumerated.item > 7)
773 uinfo->value.enumerated.item = 7;
774 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
778 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
780 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
781 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
785 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
787 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
789 unsigned char oval, nval;
792 if (ucontrol->value.enumerated.item[0] > 8)
794 spin_lock_irqsave(&chip->reg_lock, flags);
795 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
796 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
797 change = nval != oval;
799 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
800 spin_unlock_irqrestore(&chip->reg_lock, flags);
804 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
805 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
806 .info = snd_es1688_info_single, \
807 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
808 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
810 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
812 int mask = (kcontrol->private_value >> 16) & 0xff;
814 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
816 uinfo->value.integer.min = 0;
817 uinfo->value.integer.max = mask;
821 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
823 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
825 int reg = kcontrol->private_value & 0xff;
826 int shift = (kcontrol->private_value >> 8) & 0xff;
827 int mask = (kcontrol->private_value >> 16) & 0xff;
828 int invert = (kcontrol->private_value >> 24) & 0xff;
830 spin_lock_irqsave(&chip->reg_lock, flags);
831 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
832 spin_unlock_irqrestore(&chip->reg_lock, flags);
834 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
838 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
840 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
842 int reg = kcontrol->private_value & 0xff;
843 int shift = (kcontrol->private_value >> 8) & 0xff;
844 int mask = (kcontrol->private_value >> 16) & 0xff;
845 int invert = (kcontrol->private_value >> 24) & 0xff;
847 unsigned char oval, nval;
849 nval = (ucontrol->value.integer.value[0] & mask);
853 spin_lock_irqsave(&chip->reg_lock, flags);
854 oval = snd_es1688_mixer_read(chip, reg);
855 nval = (oval & ~(mask << shift)) | nval;
856 change = nval != oval;
858 snd_es1688_mixer_write(chip, reg, nval);
859 spin_unlock_irqrestore(&chip->reg_lock, flags);
863 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
864 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
865 .info = snd_es1688_info_double, \
866 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
867 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
869 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
871 int mask = (kcontrol->private_value >> 24) & 0xff;
873 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
875 uinfo->value.integer.min = 0;
876 uinfo->value.integer.max = mask;
880 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
882 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
884 int left_reg = kcontrol->private_value & 0xff;
885 int right_reg = (kcontrol->private_value >> 8) & 0xff;
886 int shift_left = (kcontrol->private_value >> 16) & 0x07;
887 int shift_right = (kcontrol->private_value >> 19) & 0x07;
888 int mask = (kcontrol->private_value >> 24) & 0xff;
889 int invert = (kcontrol->private_value >> 22) & 1;
890 unsigned char left, right;
892 spin_lock_irqsave(&chip->reg_lock, flags);
894 left = snd_es1688_mixer_read(chip, left_reg);
896 left = snd_es1688_read(chip, left_reg);
897 if (left_reg != right_reg) {
898 if (right_reg < 0xa0)
899 right = snd_es1688_mixer_read(chip, right_reg);
901 right = snd_es1688_read(chip, right_reg);
904 spin_unlock_irqrestore(&chip->reg_lock, flags);
905 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
906 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
908 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
909 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
914 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
916 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
918 int left_reg = kcontrol->private_value & 0xff;
919 int right_reg = (kcontrol->private_value >> 8) & 0xff;
920 int shift_left = (kcontrol->private_value >> 16) & 0x07;
921 int shift_right = (kcontrol->private_value >> 19) & 0x07;
922 int mask = (kcontrol->private_value >> 24) & 0xff;
923 int invert = (kcontrol->private_value >> 22) & 1;
925 unsigned char val1, val2, oval1, oval2;
927 val1 = ucontrol->value.integer.value[0] & mask;
928 val2 = ucontrol->value.integer.value[1] & mask;
934 val2 <<= shift_right;
935 spin_lock_irqsave(&chip->reg_lock, flags);
936 if (left_reg != right_reg) {
938 oval1 = snd_es1688_mixer_read(chip, left_reg);
940 oval1 = snd_es1688_read(chip, left_reg);
941 if (right_reg < 0xa0)
942 oval2 = snd_es1688_mixer_read(chip, right_reg);
944 oval2 = snd_es1688_read(chip, right_reg);
945 val1 = (oval1 & ~(mask << shift_left)) | val1;
946 val2 = (oval2 & ~(mask << shift_right)) | val2;
947 change = val1 != oval1 || val2 != oval2;
950 snd_es1688_mixer_write(chip, left_reg, val1);
952 snd_es1688_write(chip, left_reg, val1);
953 if (right_reg < 0xa0)
954 snd_es1688_mixer_write(chip, right_reg, val1);
956 snd_es1688_write(chip, right_reg, val1);
960 oval1 = snd_es1688_mixer_read(chip, left_reg);
962 oval1 = snd_es1688_read(chip, left_reg);
963 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
964 change = val1 != oval1;
967 snd_es1688_mixer_write(chip, left_reg, val1);
969 snd_es1688_write(chip, left_reg, val1);
973 spin_unlock_irqrestore(&chip->reg_lock, flags);
977 static struct snd_kcontrol_new snd_es1688_controls[] = {
978 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
979 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
980 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
981 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
982 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
983 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
984 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
985 ES1688_SINGLE("PC Speaker Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
986 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
987 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
990 .name = "Capture Source",
991 .info = snd_es1688_info_mux,
992 .get = snd_es1688_get_mux,
993 .put = snd_es1688_put_mux,
997 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
999 static unsigned char snd_es1688_init_table[][2] = {
1000 { ES1688_MASTER_DEV, 0 },
1001 { ES1688_PCM_DEV, 0 },
1002 { ES1688_LINE_DEV, 0 },
1003 { ES1688_CD_DEV, 0 },
1004 { ES1688_FM_DEV, 0 },
1005 { ES1688_MIC_DEV, 0 },
1006 { ES1688_AUX_DEV, 0 },
1007 { ES1688_SPEAKER_DEV, 0 },
1008 { ES1688_RECLEV_DEV, 0 },
1009 { ES1688_REC_DEV, 0x17 }
1012 int snd_es1688_mixer(struct snd_es1688 *chip)
1014 struct snd_card *card;
1017 unsigned char reg, val;
1019 if (snd_BUG_ON(!chip || !chip->card))
1024 strcpy(card->mixername, snd_es1688_chip_id(chip));
1026 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
1027 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1030 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1031 reg = snd_es1688_init_table[idx][0];
1032 val = snd_es1688_init_table[idx][1];
1034 snd_es1688_mixer_write(chip, reg, val);
1036 snd_es1688_write(chip, reg, val);
1041 EXPORT_SYMBOL(snd_es1688_mixer_write);
1042 EXPORT_SYMBOL(snd_es1688_create);
1043 EXPORT_SYMBOL(snd_es1688_pcm);
1044 EXPORT_SYMBOL(snd_es1688_mixer);
1050 static int __init alsa_es1688_init(void)
1055 static void __exit alsa_es1688_exit(void)
1059 module_init(alsa_es1688_init)
1060 module_exit(alsa_es1688_exit)