1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Routines for control of ESS ES1688/688/488 chip
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/ioport.h>
12 #include <linux/module.h>
14 #include <sound/core.h>
15 #include <sound/es1688.h>
16 #include <sound/initval.h>
20 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
21 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
22 MODULE_LICENSE("GPL");
24 static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
28 for (i = 10000; i; i--)
29 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
30 outb(val, ES1688P(chip, COMMAND));
33 #ifdef CONFIG_SND_DEBUG
34 printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
39 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
43 for (i = 1000; i; i--)
44 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
45 return inb(ES1688P(chip, READ));
46 snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
50 static int snd_es1688_write(struct snd_es1688 *chip,
51 unsigned char reg, unsigned char data)
53 if (!snd_es1688_dsp_command(chip, reg))
55 return snd_es1688_dsp_command(chip, data);
58 static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
60 /* Read a byte from an extended mode register of ES1688 */
61 if (!snd_es1688_dsp_command(chip, 0xc0))
63 if (!snd_es1688_dsp_command(chip, reg))
65 return snd_es1688_dsp_get_byte(chip);
68 void snd_es1688_mixer_write(struct snd_es1688 *chip,
69 unsigned char reg, unsigned char data)
71 outb(reg, ES1688P(chip, MIXER_ADDR));
73 outb(data, ES1688P(chip, MIXER_DATA));
77 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
81 outb(reg, ES1688P(chip, MIXER_ADDR));
83 result = inb(ES1688P(chip, MIXER_DATA));
88 int snd_es1688_reset(struct snd_es1688 *chip)
92 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
94 outb(0, ES1688P(chip, RESET));
96 for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
97 if (inb(ES1688P(chip, READ)) != 0xaa) {
98 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
101 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
104 EXPORT_SYMBOL(snd_es1688_reset);
106 static int snd_es1688_probe(struct snd_es1688 *chip)
109 unsigned short major, minor;
113 * initialization sequence
116 spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */
117 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
118 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
119 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
120 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
121 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
122 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
123 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
124 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
125 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
126 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
127 inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */
129 if (snd_es1688_reset(chip) < 0) {
130 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
131 spin_unlock_irqrestore(&chip->reg_lock, flags);
134 snd_es1688_dsp_command(chip, 0xe7); /* return identification */
136 for (i = 1000, major = minor = 0; i; i--) {
137 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
139 major = inb(ES1688P(chip, READ));
141 minor = inb(ES1688P(chip, READ));
146 spin_unlock_irqrestore(&chip->reg_lock, flags);
148 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
150 chip->version = (major << 8) | minor;
152 return -ENODEV; /* probably SB */
154 switch (chip->version & 0xfff0) {
156 snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
157 "but driver is in another place\n", chip->port);
162 snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
163 "with version 0x%x (Jazz16 soundcard?)\n",
164 chip->port, chip->version);
168 spin_lock_irqsave(&chip->reg_lock, flags);
169 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
170 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
171 spin_unlock_irqrestore(&chip->reg_lock, flags);
173 /* enable joystick, but disable OPL3 */
174 spin_lock_irqsave(&chip->mixer_lock, flags);
175 snd_es1688_mixer_write(chip, 0x40, 0x01);
176 spin_unlock_irqrestore(&chip->mixer_lock, flags);
181 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
183 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
185 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
187 /* ok.. setup MPU-401 port and joystick and OPL3 */
188 cfg = 0x01; /* enable joystick, but disable OPL3 */
189 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
190 tmp = (chip->mpu_port & 0x0f0) >> 4;
192 switch (chip->mpu_irq) {
209 cfg |= (tmp << 3) | (tmp1 << 5);
214 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
216 spin_lock_irqsave(&chip->reg_lock, flags);
217 snd_es1688_mixer_write(chip, 0x40, cfg);
218 spin_unlock_irqrestore(&chip->reg_lock, flags);
220 spin_lock_irqsave(&chip->reg_lock, flags);
221 snd_es1688_read(chip, 0xb1);
222 snd_es1688_read(chip, 0xb2);
223 spin_unlock_irqrestore(&chip->reg_lock, flags);
225 cfg = 0xf0; /* enable only DMA counter interrupt */
226 irq_bits = irqs[chip->irq & 0x0f];
228 snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
229 "for ES1688 chip!!\n",
230 chip->port, chip->irq);
237 spin_lock_irqsave(&chip->reg_lock, flags);
238 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
239 spin_unlock_irqrestore(&chip->reg_lock, flags);
240 cfg = 0xf0; /* extended mode DMA enable */
242 if (dma > 3 || dma == 2) {
243 snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
244 "for ES1688 chip!!\n", chip->port, dma);
247 cfg = 0x00; /* disable all DMA */
255 spin_lock_irqsave(&chip->reg_lock, flags);
256 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
257 spin_unlock_irqrestore(&chip->reg_lock, flags);
259 spin_lock_irqsave(&chip->reg_lock, flags);
260 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
261 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
262 spin_unlock_irqrestore(&chip->reg_lock, flags);
264 spin_lock_irqsave(&chip->reg_lock, flags);
265 snd_es1688_read(chip, 0xb1);
266 snd_es1688_read(chip, 0xb2);
267 snd_es1688_reset(chip);
268 spin_unlock_irqrestore(&chip->reg_lock, flags);
276 static const struct snd_ratnum clocks[2] = {
291 static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
296 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
298 struct snd_pcm_runtime *runtime = substream->runtime;
299 unsigned int bits, divider;
301 if (runtime->rate_num == clocks[0].num)
302 bits = 256 - runtime->rate_den;
304 bits = 128 - runtime->rate_den;
305 /* set filter register */
306 divider = 256 - 7160000*20/(8*82*runtime->rate);
307 /* write result to hardware */
308 snd_es1688_write(chip, 0xa1, bits);
309 snd_es1688_write(chip, 0xa2, divider);
312 static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
313 unsigned int cmd, void *arg)
315 return snd_pcm_lib_ioctl(substream, cmd, arg);
318 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
322 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
324 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
327 spin_lock(&chip->reg_lock);
328 chip->trigger_value = value;
329 val = snd_es1688_read(chip, 0xb8);
330 if ((val < 0) || (val & 0x0f) == value) {
331 spin_unlock(&chip->reg_lock);
332 return -EINVAL; /* something is wrong */
335 printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
336 printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
337 snd_dma_pointer(chip->dma8, chip->dma_size));
339 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
340 spin_unlock(&chip->reg_lock);
344 static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
345 struct snd_pcm_hw_params *hw_params)
347 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
350 static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
352 return snd_pcm_lib_free_pages(substream);
355 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
358 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
359 struct snd_pcm_runtime *runtime = substream->runtime;
360 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
361 unsigned int count = snd_pcm_lib_period_bytes(substream);
363 chip->dma_size = size;
364 spin_lock_irqsave(&chip->reg_lock, flags);
365 snd_es1688_reset(chip);
366 snd_es1688_set_rate(chip, substream);
367 snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
368 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
369 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
370 if (runtime->channels == 1) {
371 if (snd_pcm_format_width(runtime->format) == 8) {
373 snd_es1688_write(chip, 0xb6, 0x80);
374 snd_es1688_write(chip, 0xb7, 0x51);
375 snd_es1688_write(chip, 0xb7, 0xd0);
378 snd_es1688_write(chip, 0xb6, 0x00);
379 snd_es1688_write(chip, 0xb7, 0x71);
380 snd_es1688_write(chip, 0xb7, 0xf4);
383 if (snd_pcm_format_width(runtime->format) == 8) {
385 snd_es1688_write(chip, 0xb6, 0x80);
386 snd_es1688_write(chip, 0xb7, 0x51);
387 snd_es1688_write(chip, 0xb7, 0x98);
390 snd_es1688_write(chip, 0xb6, 0x00);
391 snd_es1688_write(chip, 0xb7, 0x71);
392 snd_es1688_write(chip, 0xb7, 0xbc);
395 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
396 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
397 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
398 spin_unlock_irqrestore(&chip->reg_lock, flags);
401 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
402 spin_lock_irqsave(&chip->reg_lock, flags);
403 snd_es1688_write(chip, 0xa4, (unsigned char) count);
404 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
405 spin_unlock_irqrestore(&chip->reg_lock, flags);
409 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
412 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
413 return snd_es1688_trigger(chip, cmd, 0x05);
416 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
419 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
420 struct snd_pcm_runtime *runtime = substream->runtime;
421 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
422 unsigned int count = snd_pcm_lib_period_bytes(substream);
424 chip->dma_size = size;
425 spin_lock_irqsave(&chip->reg_lock, flags);
426 snd_es1688_reset(chip);
427 snd_es1688_set_rate(chip, substream);
428 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
429 snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
430 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
431 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
432 if (runtime->channels == 1) {
433 if (snd_pcm_format_width(runtime->format) == 8) {
435 snd_es1688_write(chip, 0xb7, 0x51);
436 snd_es1688_write(chip, 0xb7, 0xd0);
439 snd_es1688_write(chip, 0xb7, 0x71);
440 snd_es1688_write(chip, 0xb7, 0xf4);
443 if (snd_pcm_format_width(runtime->format) == 8) {
445 snd_es1688_write(chip, 0xb7, 0x51);
446 snd_es1688_write(chip, 0xb7, 0x98);
449 snd_es1688_write(chip, 0xb7, 0x71);
450 snd_es1688_write(chip, 0xb7, 0xbc);
453 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
454 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
455 spin_unlock_irqrestore(&chip->reg_lock, flags);
458 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
459 spin_lock_irqsave(&chip->reg_lock, flags);
460 snd_es1688_write(chip, 0xa4, (unsigned char) count);
461 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
462 spin_unlock_irqrestore(&chip->reg_lock, flags);
466 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
469 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
470 return snd_es1688_trigger(chip, cmd, 0x0f);
473 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
475 struct snd_es1688 *chip = dev_id;
477 if (chip->trigger_value == 0x05) /* ok.. playback is active */
478 snd_pcm_period_elapsed(chip->playback_substream);
479 if (chip->trigger_value == 0x0f) /* ok.. capture is active */
480 snd_pcm_period_elapsed(chip->capture_substream);
482 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
486 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
488 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
491 if (chip->trigger_value != 0x05)
493 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
494 return bytes_to_frames(substream->runtime, ptr);
497 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
499 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
502 if (chip->trigger_value != 0x0f)
504 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
505 return bytes_to_frames(substream->runtime, ptr);
512 static const struct snd_pcm_hardware snd_es1688_playback =
514 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
515 SNDRV_PCM_INFO_MMAP_VALID),
516 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
517 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
522 .buffer_bytes_max = 65536,
523 .period_bytes_min = 64,
524 .period_bytes_max = 65536,
530 static const struct snd_pcm_hardware snd_es1688_capture =
532 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
533 SNDRV_PCM_INFO_MMAP_VALID),
534 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
535 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
540 .buffer_bytes_max = 65536,
541 .period_bytes_min = 64,
542 .period_bytes_max = 65536,
552 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
554 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
555 struct snd_pcm_runtime *runtime = substream->runtime;
557 if (chip->capture_substream != NULL)
559 chip->playback_substream = substream;
560 runtime->hw = snd_es1688_playback;
561 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
562 &hw_constraints_clocks);
566 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
568 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
569 struct snd_pcm_runtime *runtime = substream->runtime;
571 if (chip->playback_substream != NULL)
573 chip->capture_substream = substream;
574 runtime->hw = snd_es1688_capture;
575 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
576 &hw_constraints_clocks);
580 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
582 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
584 chip->playback_substream = NULL;
588 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
590 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
592 chip->capture_substream = NULL;
596 static int snd_es1688_free(struct snd_es1688 *chip)
598 if (chip->hardware != ES1688_HW_UNDEF)
599 snd_es1688_init(chip, 0);
600 release_and_free_resource(chip->res_port);
602 free_irq(chip->irq, (void *) chip);
603 if (chip->dma8 >= 0) {
604 disable_dma(chip->dma8);
605 free_dma(chip->dma8);
610 static int snd_es1688_dev_free(struct snd_device *device)
612 struct snd_es1688 *chip = device->device_data;
613 return snd_es1688_free(chip);
616 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
619 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
623 int snd_es1688_create(struct snd_card *card,
624 struct snd_es1688 *chip,
626 unsigned long mpu_port,
630 unsigned short hardware)
632 static struct snd_device_ops ops = {
633 .dev_free = snd_es1688_dev_free,
642 chip->hardware = ES1688_HW_UNDEF;
644 chip->res_port = request_region(port + 4, 12, "ES1688");
645 if (chip->res_port == NULL) {
646 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
651 err = request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip);
653 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
658 err = request_dma(dma8, "ES1688");
661 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
666 spin_lock_init(&chip->reg_lock);
667 spin_lock_init(&chip->mixer_lock);
670 if (mpu_port < 0x300 || mpu_port > 0x330)
672 chip->mpu_port = mpu_port;
673 chip->mpu_irq = mpu_irq;
674 chip->hardware = hardware;
676 err = snd_es1688_probe(chip);
680 err = snd_es1688_init(chip, 1);
684 /* Register device */
685 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
688 snd_es1688_free(chip);
692 static const struct snd_pcm_ops snd_es1688_playback_ops = {
693 .open = snd_es1688_playback_open,
694 .close = snd_es1688_playback_close,
695 .ioctl = snd_es1688_ioctl,
696 .hw_params = snd_es1688_hw_params,
697 .hw_free = snd_es1688_hw_free,
698 .prepare = snd_es1688_playback_prepare,
699 .trigger = snd_es1688_playback_trigger,
700 .pointer = snd_es1688_playback_pointer,
703 static const struct snd_pcm_ops snd_es1688_capture_ops = {
704 .open = snd_es1688_capture_open,
705 .close = snd_es1688_capture_close,
706 .ioctl = snd_es1688_ioctl,
707 .hw_params = snd_es1688_hw_params,
708 .hw_free = snd_es1688_hw_free,
709 .prepare = snd_es1688_capture_prepare,
710 .trigger = snd_es1688_capture_trigger,
711 .pointer = snd_es1688_capture_pointer,
714 int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, int device)
719 err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
723 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
724 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
726 pcm->private_data = chip;
727 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
728 strcpy(pcm->name, snd_es1688_chip_id(chip));
731 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
741 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
743 static const char * const texts[8] = {
744 "Mic", "Mic Master", "CD", "AOUT",
745 "Mic1", "Mix", "Line", "Master"
748 return snd_ctl_enum_info(uinfo, 1, 8, texts);
751 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
753 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
754 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
758 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
760 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
762 unsigned char oval, nval;
765 if (ucontrol->value.enumerated.item[0] > 8)
767 spin_lock_irqsave(&chip->reg_lock, flags);
768 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
769 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
770 change = nval != oval;
772 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
773 spin_unlock_irqrestore(&chip->reg_lock, flags);
777 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
778 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
779 .info = snd_es1688_info_single, \
780 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
781 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
783 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
785 int mask = (kcontrol->private_value >> 16) & 0xff;
787 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
789 uinfo->value.integer.min = 0;
790 uinfo->value.integer.max = mask;
794 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
796 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
798 int reg = kcontrol->private_value & 0xff;
799 int shift = (kcontrol->private_value >> 8) & 0xff;
800 int mask = (kcontrol->private_value >> 16) & 0xff;
801 int invert = (kcontrol->private_value >> 24) & 0xff;
803 spin_lock_irqsave(&chip->reg_lock, flags);
804 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
805 spin_unlock_irqrestore(&chip->reg_lock, flags);
807 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
811 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
813 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
815 int reg = kcontrol->private_value & 0xff;
816 int shift = (kcontrol->private_value >> 8) & 0xff;
817 int mask = (kcontrol->private_value >> 16) & 0xff;
818 int invert = (kcontrol->private_value >> 24) & 0xff;
820 unsigned char oval, nval;
822 nval = (ucontrol->value.integer.value[0] & mask);
826 spin_lock_irqsave(&chip->reg_lock, flags);
827 oval = snd_es1688_mixer_read(chip, reg);
828 nval = (oval & ~(mask << shift)) | nval;
829 change = nval != oval;
831 snd_es1688_mixer_write(chip, reg, nval);
832 spin_unlock_irqrestore(&chip->reg_lock, flags);
836 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
837 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
838 .info = snd_es1688_info_double, \
839 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
840 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
842 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
844 int mask = (kcontrol->private_value >> 24) & 0xff;
846 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
848 uinfo->value.integer.min = 0;
849 uinfo->value.integer.max = mask;
853 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
855 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
857 int left_reg = kcontrol->private_value & 0xff;
858 int right_reg = (kcontrol->private_value >> 8) & 0xff;
859 int shift_left = (kcontrol->private_value >> 16) & 0x07;
860 int shift_right = (kcontrol->private_value >> 19) & 0x07;
861 int mask = (kcontrol->private_value >> 24) & 0xff;
862 int invert = (kcontrol->private_value >> 22) & 1;
863 unsigned char left, right;
865 spin_lock_irqsave(&chip->reg_lock, flags);
867 left = snd_es1688_mixer_read(chip, left_reg);
869 left = snd_es1688_read(chip, left_reg);
870 if (left_reg != right_reg) {
871 if (right_reg < 0xa0)
872 right = snd_es1688_mixer_read(chip, right_reg);
874 right = snd_es1688_read(chip, right_reg);
877 spin_unlock_irqrestore(&chip->reg_lock, flags);
878 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
879 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
881 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
882 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
887 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
889 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
891 int left_reg = kcontrol->private_value & 0xff;
892 int right_reg = (kcontrol->private_value >> 8) & 0xff;
893 int shift_left = (kcontrol->private_value >> 16) & 0x07;
894 int shift_right = (kcontrol->private_value >> 19) & 0x07;
895 int mask = (kcontrol->private_value >> 24) & 0xff;
896 int invert = (kcontrol->private_value >> 22) & 1;
898 unsigned char val1, val2, oval1, oval2;
900 val1 = ucontrol->value.integer.value[0] & mask;
901 val2 = ucontrol->value.integer.value[1] & mask;
907 val2 <<= shift_right;
908 spin_lock_irqsave(&chip->reg_lock, flags);
909 if (left_reg != right_reg) {
911 oval1 = snd_es1688_mixer_read(chip, left_reg);
913 oval1 = snd_es1688_read(chip, left_reg);
914 if (right_reg < 0xa0)
915 oval2 = snd_es1688_mixer_read(chip, right_reg);
917 oval2 = snd_es1688_read(chip, right_reg);
918 val1 = (oval1 & ~(mask << shift_left)) | val1;
919 val2 = (oval2 & ~(mask << shift_right)) | val2;
920 change = val1 != oval1 || val2 != oval2;
923 snd_es1688_mixer_write(chip, left_reg, val1);
925 snd_es1688_write(chip, left_reg, val1);
926 if (right_reg < 0xa0)
927 snd_es1688_mixer_write(chip, right_reg, val1);
929 snd_es1688_write(chip, right_reg, val1);
933 oval1 = snd_es1688_mixer_read(chip, left_reg);
935 oval1 = snd_es1688_read(chip, left_reg);
936 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
937 change = val1 != oval1;
940 snd_es1688_mixer_write(chip, left_reg, val1);
942 snd_es1688_write(chip, left_reg, val1);
946 spin_unlock_irqrestore(&chip->reg_lock, flags);
950 static struct snd_kcontrol_new snd_es1688_controls[] = {
951 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
952 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
953 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
954 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
955 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
956 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
957 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
958 ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
959 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
960 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
963 .name = "Capture Source",
964 .info = snd_es1688_info_mux,
965 .get = snd_es1688_get_mux,
966 .put = snd_es1688_put_mux,
970 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
972 static unsigned char snd_es1688_init_table[][2] = {
973 { ES1688_MASTER_DEV, 0 },
974 { ES1688_PCM_DEV, 0 },
975 { ES1688_LINE_DEV, 0 },
976 { ES1688_CD_DEV, 0 },
977 { ES1688_FM_DEV, 0 },
978 { ES1688_MIC_DEV, 0 },
979 { ES1688_AUX_DEV, 0 },
980 { ES1688_SPEAKER_DEV, 0 },
981 { ES1688_RECLEV_DEV, 0 },
982 { ES1688_REC_DEV, 0x17 }
985 int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip)
989 unsigned char reg, val;
991 if (snd_BUG_ON(!chip || !card))
994 strcpy(card->mixername, snd_es1688_chip_id(chip));
996 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
997 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1000 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1001 reg = snd_es1688_init_table[idx][0];
1002 val = snd_es1688_init_table[idx][1];
1004 snd_es1688_mixer_write(chip, reg, val);
1006 snd_es1688_write(chip, reg, val);
1011 EXPORT_SYMBOL(snd_es1688_mixer_write);
1012 EXPORT_SYMBOL(snd_es1688_create);
1013 EXPORT_SYMBOL(snd_es1688_pcm);
1014 EXPORT_SYMBOL(snd_es1688_mixer);