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 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 */
118 EXPORT_SYMBOL(snd_es1688_reset
);
120 static int snd_es1688_probe(struct snd_es1688
*chip
)
123 unsigned short major
, minor
, hw
;
127 * initialization sequence
130 spin_lock_irqsave(&chip
->reg_lock
, flags
); /* Some ESS1688 cards need this */
131 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
132 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
133 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
134 inb(ES1688P(chip
, ENABLE2
)); /* ENABLE2 */
135 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
136 inb(ES1688P(chip
, ENABLE2
)); /* ENABLE2 */
137 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
138 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
139 inb(ES1688P(chip
, ENABLE2
)); /* ENABLE2 */
140 inb(ES1688P(chip
, ENABLE1
)); /* ENABLE1 */
141 inb(ES1688P(chip
, ENABLE0
)); /* ENABLE0 */
143 if (snd_es1688_reset(chip
) < 0) {
144 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip
->port
, inb(ES1688P(chip
, READ
)));
145 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
148 snd_es1688_dsp_command(chip
, 0xe7); /* return identification */
150 for (i
= 1000, major
= minor
= 0; i
; i
--) {
151 if (inb(ES1688P(chip
, DATA_AVAIL
)) & 0x80) {
153 major
= inb(ES1688P(chip
, READ
));
155 minor
= inb(ES1688P(chip
, READ
));
160 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
162 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip
->port
, major
, minor
);
164 chip
->version
= (major
<< 8) | minor
;
166 return -ENODEV
; /* probably SB */
169 switch (chip
->version
& 0xfff0) {
171 snd_printk(KERN_ERR
"[0x%lx] ESS: AudioDrive ES488 detected, "
172 "but driver is in another place\n", chip
->port
);
175 hw
= (chip
->version
& 0x0f) >= 8 ? ES1688_HW_1688
: ES1688_HW_688
;
178 snd_printk(KERN_ERR
"[0x%lx] ESS: unknown AudioDrive chip "
179 "with version 0x%x (Jazz16 soundcard?)\n",
180 chip
->port
, chip
->version
);
184 spin_lock_irqsave(&chip
->reg_lock
, flags
);
185 snd_es1688_write(chip
, 0xb1, 0x10); /* disable IRQ */
186 snd_es1688_write(chip
, 0xb2, 0x00); /* disable DMA */
187 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
189 /* enable joystick, but disable OPL3 */
190 spin_lock_irqsave(&chip
->mixer_lock
, flags
);
191 snd_es1688_mixer_write(chip
, 0x40, 0x01);
192 spin_unlock_irqrestore(&chip
->mixer_lock
, flags
);
197 static int snd_es1688_init(struct snd_es1688
* chip
, int enable
)
199 static int irqs
[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
201 int cfg
, irq_bits
, dma
, dma_bits
, tmp
, tmp1
;
203 /* ok.. setup MPU-401 port and joystick and OPL3 */
204 cfg
= 0x01; /* enable joystick, but disable OPL3 */
205 if (enable
&& chip
->mpu_port
>= 0x300 && chip
->mpu_irq
> 0 && chip
->hardware
!= ES1688_HW_688
) {
206 tmp
= (chip
->mpu_port
& 0x0f0) >> 4;
208 switch (chip
->mpu_irq
) {
225 cfg
|= (tmp
<< 3) | (tmp1
<< 5);
230 snd_printk(KERN_DEBUG
"mpu cfg = 0x%x\n", cfg
);
232 spin_lock_irqsave(&chip
->reg_lock
, flags
);
233 snd_es1688_mixer_write(chip
, 0x40, cfg
);
234 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
236 spin_lock_irqsave(&chip
->reg_lock
, flags
);
237 snd_es1688_read(chip
, 0xb1);
238 snd_es1688_read(chip
, 0xb2);
239 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
241 cfg
= 0xf0; /* enable only DMA counter interrupt */
242 irq_bits
= irqs
[chip
->irq
& 0x0f];
244 snd_printk(KERN_ERR
"[0x%lx] ESS: bad IRQ %d "
245 "for ES1688 chip!!\n",
246 chip
->port
, chip
->irq
);
253 spin_lock_irqsave(&chip
->reg_lock
, flags
);
254 snd_es1688_write(chip
, 0xb1, cfg
| (irq_bits
<< 2));
255 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
256 cfg
= 0xf0; /* extended mode DMA enable */
258 if (dma
> 3 || dma
== 2) {
259 snd_printk(KERN_ERR
"[0x%lx] ESS: bad DMA channel %d "
260 "for ES1688 chip!!\n", chip
->port
, dma
);
263 cfg
= 0x00; /* disable all DMA */
271 spin_lock_irqsave(&chip
->reg_lock
, flags
);
272 snd_es1688_write(chip
, 0xb2, cfg
| (dma_bits
<< 2));
273 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
275 spin_lock_irqsave(&chip
->reg_lock
, flags
);
276 snd_es1688_write(chip
, 0xb1, 0x10); /* disable IRQ */
277 snd_es1688_write(chip
, 0xb2, 0x00); /* disable DMA */
278 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
280 spin_lock_irqsave(&chip
->reg_lock
, flags
);
281 snd_es1688_read(chip
, 0xb1);
282 snd_es1688_read(chip
, 0xb2);
283 snd_es1688_reset(chip
);
284 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
292 static struct snd_ratnum clocks
[2] = {
307 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks
= {
312 static void snd_es1688_set_rate(struct snd_es1688
*chip
, struct snd_pcm_substream
*substream
)
314 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
315 unsigned int bits
, divider
;
317 if (runtime
->rate_num
== clocks
[0].num
)
318 bits
= 256 - runtime
->rate_den
;
320 bits
= 128 - runtime
->rate_den
;
321 /* set filter register */
322 divider
= 256 - 7160000*20/(8*82*runtime
->rate
);
323 /* write result to hardware */
324 snd_es1688_write(chip
, 0xa1, bits
);
325 snd_es1688_write(chip
, 0xa2, divider
);
328 static int snd_es1688_ioctl(struct snd_pcm_substream
*substream
,
329 unsigned int cmd
, void *arg
)
331 return snd_pcm_lib_ioctl(substream
, cmd
, arg
);
334 static int snd_es1688_trigger(struct snd_es1688
*chip
, int cmd
, unsigned char value
)
338 if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
340 } else if (cmd
!= SNDRV_PCM_TRIGGER_START
) {
343 spin_lock(&chip
->reg_lock
);
344 chip
->trigger_value
= value
;
345 val
= snd_es1688_read(chip
, 0xb8);
346 if ((val
< 0) || (val
& 0x0f) == value
) {
347 spin_unlock(&chip
->reg_lock
);
348 return -EINVAL
; /* something is wrong */
351 printk(KERN_DEBUG
"trigger: val = 0x%x, value = 0x%x\n", val
, value
);
352 printk(KERN_DEBUG
"trigger: pointer = 0x%x\n",
353 snd_dma_pointer(chip
->dma8
, chip
->dma_size
));
355 snd_es1688_write(chip
, 0xb8, (val
& 0xf0) | value
);
356 spin_unlock(&chip
->reg_lock
);
360 static int snd_es1688_hw_params(struct snd_pcm_substream
*substream
,
361 struct snd_pcm_hw_params
*hw_params
)
363 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
366 static int snd_es1688_hw_free(struct snd_pcm_substream
*substream
)
368 return snd_pcm_lib_free_pages(substream
);
371 static int snd_es1688_playback_prepare(struct snd_pcm_substream
*substream
)
374 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
375 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
376 unsigned int size
= snd_pcm_lib_buffer_bytes(substream
);
377 unsigned int count
= snd_pcm_lib_period_bytes(substream
);
379 chip
->dma_size
= size
;
380 spin_lock_irqsave(&chip
->reg_lock
, flags
);
381 snd_es1688_reset(chip
);
382 snd_es1688_set_rate(chip
, substream
);
383 snd_es1688_write(chip
, 0xb8, 4); /* auto init DMA mode */
384 snd_es1688_write(chip
, 0xa8, (snd_es1688_read(chip
, 0xa8) & ~0x03) | (3 - runtime
->channels
));
385 snd_es1688_write(chip
, 0xb9, 2); /* demand mode (4 bytes/request) */
386 if (runtime
->channels
== 1) {
387 if (snd_pcm_format_width(runtime
->format
) == 8) {
389 snd_es1688_write(chip
, 0xb6, 0x80);
390 snd_es1688_write(chip
, 0xb7, 0x51);
391 snd_es1688_write(chip
, 0xb7, 0xd0);
394 snd_es1688_write(chip
, 0xb6, 0x00);
395 snd_es1688_write(chip
, 0xb7, 0x71);
396 snd_es1688_write(chip
, 0xb7, 0xf4);
399 if (snd_pcm_format_width(runtime
->format
) == 8) {
401 snd_es1688_write(chip
, 0xb6, 0x80);
402 snd_es1688_write(chip
, 0xb7, 0x51);
403 snd_es1688_write(chip
, 0xb7, 0x98);
406 snd_es1688_write(chip
, 0xb6, 0x00);
407 snd_es1688_write(chip
, 0xb7, 0x71);
408 snd_es1688_write(chip
, 0xb7, 0xbc);
411 snd_es1688_write(chip
, 0xb1, (snd_es1688_read(chip
, 0xb1) & 0x0f) | 0x50);
412 snd_es1688_write(chip
, 0xb2, (snd_es1688_read(chip
, 0xb2) & 0x0f) | 0x50);
413 snd_es1688_dsp_command(chip
, ES1688_DSP_CMD_SPKON
);
414 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
417 snd_dma_program(chip
->dma8
, runtime
->dma_addr
, size
, DMA_MODE_WRITE
| DMA_AUTOINIT
);
418 spin_lock_irqsave(&chip
->reg_lock
, flags
);
419 snd_es1688_write(chip
, 0xa4, (unsigned char) count
);
420 snd_es1688_write(chip
, 0xa5, (unsigned char) (count
>> 8));
421 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
425 static int snd_es1688_playback_trigger(struct snd_pcm_substream
*substream
,
428 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
429 return snd_es1688_trigger(chip
, cmd
, 0x05);
432 static int snd_es1688_capture_prepare(struct snd_pcm_substream
*substream
)
435 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
436 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
437 unsigned int size
= snd_pcm_lib_buffer_bytes(substream
);
438 unsigned int count
= snd_pcm_lib_period_bytes(substream
);
440 chip
->dma_size
= size
;
441 spin_lock_irqsave(&chip
->reg_lock
, flags
);
442 snd_es1688_reset(chip
);
443 snd_es1688_set_rate(chip
, substream
);
444 snd_es1688_dsp_command(chip
, ES1688_DSP_CMD_SPKOFF
);
445 snd_es1688_write(chip
, 0xb8, 0x0e); /* auto init DMA mode */
446 snd_es1688_write(chip
, 0xa8, (snd_es1688_read(chip
, 0xa8) & ~0x03) | (3 - runtime
->channels
));
447 snd_es1688_write(chip
, 0xb9, 2); /* demand mode (4 bytes/request) */
448 if (runtime
->channels
== 1) {
449 if (snd_pcm_format_width(runtime
->format
) == 8) {
451 snd_es1688_write(chip
, 0xb7, 0x51);
452 snd_es1688_write(chip
, 0xb7, 0xd0);
455 snd_es1688_write(chip
, 0xb7, 0x71);
456 snd_es1688_write(chip
, 0xb7, 0xf4);
459 if (snd_pcm_format_width(runtime
->format
) == 8) {
461 snd_es1688_write(chip
, 0xb7, 0x51);
462 snd_es1688_write(chip
, 0xb7, 0x98);
465 snd_es1688_write(chip
, 0xb7, 0x71);
466 snd_es1688_write(chip
, 0xb7, 0xbc);
469 snd_es1688_write(chip
, 0xb1, (snd_es1688_read(chip
, 0xb1) & 0x0f) | 0x50);
470 snd_es1688_write(chip
, 0xb2, (snd_es1688_read(chip
, 0xb2) & 0x0f) | 0x50);
471 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
474 snd_dma_program(chip
->dma8
, runtime
->dma_addr
, size
, DMA_MODE_READ
| DMA_AUTOINIT
);
475 spin_lock_irqsave(&chip
->reg_lock
, flags
);
476 snd_es1688_write(chip
, 0xa4, (unsigned char) count
);
477 snd_es1688_write(chip
, 0xa5, (unsigned char) (count
>> 8));
478 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
482 static int snd_es1688_capture_trigger(struct snd_pcm_substream
*substream
,
485 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
486 return snd_es1688_trigger(chip
, cmd
, 0x0f);
489 static irqreturn_t
snd_es1688_interrupt(int irq
, void *dev_id
)
491 struct snd_es1688
*chip
= dev_id
;
493 if (chip
->trigger_value
== 0x05) /* ok.. playback is active */
494 snd_pcm_period_elapsed(chip
->playback_substream
);
495 if (chip
->trigger_value
== 0x0f) /* ok.. capture is active */
496 snd_pcm_period_elapsed(chip
->capture_substream
);
498 inb(ES1688P(chip
, DATA_AVAIL
)); /* ack interrupt */
502 static snd_pcm_uframes_t
snd_es1688_playback_pointer(struct snd_pcm_substream
*substream
)
504 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
507 if (chip
->trigger_value
!= 0x05)
509 ptr
= snd_dma_pointer(chip
->dma8
, chip
->dma_size
);
510 return bytes_to_frames(substream
->runtime
, ptr
);
513 static snd_pcm_uframes_t
snd_es1688_capture_pointer(struct snd_pcm_substream
*substream
)
515 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
518 if (chip
->trigger_value
!= 0x0f)
520 ptr
= snd_dma_pointer(chip
->dma8
, chip
->dma_size
);
521 return bytes_to_frames(substream
->runtime
, ptr
);
528 static struct snd_pcm_hardware snd_es1688_playback
=
530 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
531 SNDRV_PCM_INFO_MMAP_VALID
),
532 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
533 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
538 .buffer_bytes_max
= 65536,
539 .period_bytes_min
= 64,
540 .period_bytes_max
= 65536,
546 static struct snd_pcm_hardware snd_es1688_capture
=
548 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
549 SNDRV_PCM_INFO_MMAP_VALID
),
550 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
551 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
556 .buffer_bytes_max
= 65536,
557 .period_bytes_min
= 64,
558 .period_bytes_max
= 65536,
568 static int snd_es1688_playback_open(struct snd_pcm_substream
*substream
)
570 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
571 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
573 if (chip
->capture_substream
!= NULL
)
575 chip
->playback_substream
= substream
;
576 runtime
->hw
= snd_es1688_playback
;
577 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
578 &hw_constraints_clocks
);
582 static int snd_es1688_capture_open(struct snd_pcm_substream
*substream
)
584 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
585 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
587 if (chip
->playback_substream
!= NULL
)
589 chip
->capture_substream
= substream
;
590 runtime
->hw
= snd_es1688_capture
;
591 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
592 &hw_constraints_clocks
);
596 static int snd_es1688_playback_close(struct snd_pcm_substream
*substream
)
598 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
600 chip
->playback_substream
= NULL
;
604 static int snd_es1688_capture_close(struct snd_pcm_substream
*substream
)
606 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
608 chip
->capture_substream
= NULL
;
612 static int snd_es1688_free(struct snd_es1688
*chip
)
614 if (chip
->res_port
) {
615 snd_es1688_init(chip
, 0);
616 release_and_free_resource(chip
->res_port
);
619 free_irq(chip
->irq
, (void *) chip
);
620 if (chip
->dma8
>= 0) {
621 disable_dma(chip
->dma8
);
622 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
,
641 struct snd_es1688
*chip
,
643 unsigned long mpu_port
,
647 unsigned short hardware
)
649 static struct snd_device_ops ops
= {
650 .dev_free
= snd_es1688_dev_free
,
660 if ((chip
->res_port
= request_region(port
+ 4, 12, "ES1688")) == NULL
) {
661 snd_printk(KERN_ERR
"es1688: can't grab port 0x%lx\n", port
+ 4);
664 if (request_irq(irq
, snd_es1688_interrupt
, IRQF_DISABLED
, "ES1688", (void *) chip
)) {
665 snd_printk(KERN_ERR
"es1688: can't grab IRQ %d\n", irq
);
669 if (request_dma(dma8
, "ES1688")) {
670 snd_printk(KERN_ERR
"es1688: can't grab DMA8 %d\n", dma8
);
675 spin_lock_init(&chip
->reg_lock
);
676 spin_lock_init(&chip
->mixer_lock
);
679 if (mpu_port
< 0x300 || mpu_port
> 0x330)
681 chip
->mpu_port
= mpu_port
;
682 chip
->mpu_irq
= mpu_irq
;
683 chip
->hardware
= hardware
;
685 err
= snd_es1688_probe(chip
);
689 err
= snd_es1688_init(chip
, 1);
693 /* Register device */
694 return snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
697 static struct snd_pcm_ops snd_es1688_playback_ops
= {
698 .open
= snd_es1688_playback_open
,
699 .close
= snd_es1688_playback_close
,
700 .ioctl
= snd_es1688_ioctl
,
701 .hw_params
= snd_es1688_hw_params
,
702 .hw_free
= snd_es1688_hw_free
,
703 .prepare
= snd_es1688_playback_prepare
,
704 .trigger
= snd_es1688_playback_trigger
,
705 .pointer
= snd_es1688_playback_pointer
,
708 static struct snd_pcm_ops snd_es1688_capture_ops
= {
709 .open
= snd_es1688_capture_open
,
710 .close
= snd_es1688_capture_close
,
711 .ioctl
= snd_es1688_ioctl
,
712 .hw_params
= snd_es1688_hw_params
,
713 .hw_free
= snd_es1688_hw_free
,
714 .prepare
= snd_es1688_capture_prepare
,
715 .trigger
= snd_es1688_capture_trigger
,
716 .pointer
= snd_es1688_capture_pointer
,
719 int snd_es1688_pcm(struct snd_card
*card
, struct snd_es1688
*chip
,
720 int device
, struct snd_pcm
**rpcm
)
725 err
= snd_pcm_new(card
, "ESx688", device
, 1, 1, &pcm
);
729 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_es1688_playback_ops
);
730 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_es1688_capture_ops
);
732 pcm
->private_data
= chip
;
733 pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
734 sprintf(pcm
->name
, snd_es1688_chip_id(chip
));
737 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
750 static int snd_es1688_info_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
752 static char *texts
[9] = {
753 "Mic", "Mic Master", "CD", "AOUT",
754 "Mic1", "Mix", "Line", "Master"
757 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
759 uinfo
->value
.enumerated
.items
= 8;
760 if (uinfo
->value
.enumerated
.item
> 7)
761 uinfo
->value
.enumerated
.item
= 7;
762 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
766 static int snd_es1688_get_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
768 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
769 ucontrol
->value
.enumerated
.item
[0] = snd_es1688_mixer_read(chip
, ES1688_REC_DEV
) & 7;
773 static int snd_es1688_put_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
775 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
777 unsigned char oval
, nval
;
780 if (ucontrol
->value
.enumerated
.item
[0] > 8)
782 spin_lock_irqsave(&chip
->reg_lock
, flags
);
783 oval
= snd_es1688_mixer_read(chip
, ES1688_REC_DEV
);
784 nval
= (ucontrol
->value
.enumerated
.item
[0] & 7) | (oval
& ~15);
785 change
= nval
!= oval
;
787 snd_es1688_mixer_write(chip
, ES1688_REC_DEV
, nval
);
788 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
792 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
793 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
794 .info = snd_es1688_info_single, \
795 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
796 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
798 static int snd_es1688_info_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
800 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
802 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
804 uinfo
->value
.integer
.min
= 0;
805 uinfo
->value
.integer
.max
= mask
;
809 static int snd_es1688_get_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
811 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
813 int reg
= kcontrol
->private_value
& 0xff;
814 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
815 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
816 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
818 spin_lock_irqsave(&chip
->reg_lock
, flags
);
819 ucontrol
->value
.integer
.value
[0] = (snd_es1688_mixer_read(chip
, reg
) >> shift
) & mask
;
820 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
822 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
826 static int snd_es1688_put_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
828 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
830 int reg
= kcontrol
->private_value
& 0xff;
831 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
832 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
833 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
835 unsigned char oval
, nval
;
837 nval
= (ucontrol
->value
.integer
.value
[0] & mask
);
841 spin_lock_irqsave(&chip
->reg_lock
, flags
);
842 oval
= snd_es1688_mixer_read(chip
, reg
);
843 nval
= (oval
& ~(mask
<< shift
)) | nval
;
844 change
= nval
!= oval
;
846 snd_es1688_mixer_write(chip
, reg
, nval
);
847 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
851 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
852 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
853 .info = snd_es1688_info_double, \
854 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
855 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
857 static int snd_es1688_info_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
859 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
861 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
863 uinfo
->value
.integer
.min
= 0;
864 uinfo
->value
.integer
.max
= mask
;
868 static int snd_es1688_get_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
870 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
872 int left_reg
= kcontrol
->private_value
& 0xff;
873 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
874 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
875 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
876 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
877 int invert
= (kcontrol
->private_value
>> 22) & 1;
878 unsigned char left
, right
;
880 spin_lock_irqsave(&chip
->reg_lock
, flags
);
882 left
= snd_es1688_mixer_read(chip
, left_reg
);
884 left
= snd_es1688_read(chip
, left_reg
);
885 if (left_reg
!= right_reg
) {
886 if (right_reg
< 0xa0)
887 right
= snd_es1688_mixer_read(chip
, right_reg
);
889 right
= snd_es1688_read(chip
, right_reg
);
892 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
893 ucontrol
->value
.integer
.value
[0] = (left
>> shift_left
) & mask
;
894 ucontrol
->value
.integer
.value
[1] = (right
>> shift_right
) & mask
;
896 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
897 ucontrol
->value
.integer
.value
[1] = mask
- ucontrol
->value
.integer
.value
[1];
902 static int snd_es1688_put_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
904 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
906 int left_reg
= kcontrol
->private_value
& 0xff;
907 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
908 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
909 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
910 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
911 int invert
= (kcontrol
->private_value
>> 22) & 1;
913 unsigned char val1
, val2
, oval1
, oval2
;
915 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
916 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
922 val2
<<= shift_right
;
923 spin_lock_irqsave(&chip
->reg_lock
, flags
);
924 if (left_reg
!= right_reg
) {
926 oval1
= snd_es1688_mixer_read(chip
, left_reg
);
928 oval1
= snd_es1688_read(chip
, left_reg
);
929 if (right_reg
< 0xa0)
930 oval2
= snd_es1688_mixer_read(chip
, right_reg
);
932 oval2
= snd_es1688_read(chip
, right_reg
);
933 val1
= (oval1
& ~(mask
<< shift_left
)) | val1
;
934 val2
= (oval2
& ~(mask
<< shift_right
)) | val2
;
935 change
= val1
!= oval1
|| val2
!= oval2
;
938 snd_es1688_mixer_write(chip
, left_reg
, val1
);
940 snd_es1688_write(chip
, left_reg
, val1
);
941 if (right_reg
< 0xa0)
942 snd_es1688_mixer_write(chip
, right_reg
, val1
);
944 snd_es1688_write(chip
, right_reg
, val1
);
948 oval1
= snd_es1688_mixer_read(chip
, left_reg
);
950 oval1
= snd_es1688_read(chip
, left_reg
);
951 val1
= (oval1
& ~((mask
<< shift_left
) | (mask
<< shift_right
))) | val1
| val2
;
952 change
= val1
!= oval1
;
955 snd_es1688_mixer_write(chip
, left_reg
, val1
);
957 snd_es1688_write(chip
, left_reg
, val1
);
961 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
965 static struct snd_kcontrol_new snd_es1688_controls
[] = {
966 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV
, ES1688_MASTER_DEV
, 4, 0, 15, 0),
967 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV
, ES1688_PCM_DEV
, 4, 0, 15, 0),
968 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV
, ES1688_LINE_DEV
, 4, 0, 15, 0),
969 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV
, ES1688_CD_DEV
, 4, 0, 15, 0),
970 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV
, ES1688_FM_DEV
, 4, 0, 15, 0),
971 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV
, ES1688_MIC_DEV
, 4, 0, 15, 0),
972 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV
, ES1688_AUX_DEV
, 4, 0, 15, 0),
973 ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV
, 0, 7, 0),
974 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV
, ES1688_RECLEV_DEV
, 4, 0, 15, 0),
975 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV
, 4, 1, 1),
977 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
978 .name
= "Capture Source",
979 .info
= snd_es1688_info_mux
,
980 .get
= snd_es1688_get_mux
,
981 .put
= snd_es1688_put_mux
,
985 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
987 static unsigned char snd_es1688_init_table
[][2] = {
988 { ES1688_MASTER_DEV
, 0 },
989 { ES1688_PCM_DEV
, 0 },
990 { ES1688_LINE_DEV
, 0 },
991 { ES1688_CD_DEV
, 0 },
992 { ES1688_FM_DEV
, 0 },
993 { ES1688_MIC_DEV
, 0 },
994 { ES1688_AUX_DEV
, 0 },
995 { ES1688_SPEAKER_DEV
, 0 },
996 { ES1688_RECLEV_DEV
, 0 },
997 { ES1688_REC_DEV
, 0x17 }
1000 int snd_es1688_mixer(struct snd_card
*card
, struct snd_es1688
*chip
)
1004 unsigned char reg
, val
;
1006 if (snd_BUG_ON(!chip
|| !card
))
1009 strcpy(card
->mixername
, snd_es1688_chip_id(chip
));
1011 for (idx
= 0; idx
< ARRAY_SIZE(snd_es1688_controls
); idx
++) {
1012 if ((err
= snd_ctl_add(card
, snd_ctl_new1(&snd_es1688_controls
[idx
], chip
))) < 0)
1015 for (idx
= 0; idx
< ES1688_INIT_TABLE_SIZE
; idx
++) {
1016 reg
= snd_es1688_init_table
[idx
][0];
1017 val
= snd_es1688_init_table
[idx
][1];
1019 snd_es1688_mixer_write(chip
, reg
, val
);
1021 snd_es1688_write(chip
, reg
, val
);
1026 EXPORT_SYMBOL(snd_es1688_mixer_write
);
1027 EXPORT_SYMBOL(snd_es1688_create
);
1028 EXPORT_SYMBOL(snd_es1688_pcm
);
1029 EXPORT_SYMBOL(snd_es1688_mixer
);
1035 static int __init
alsa_es1688_init(void)
1040 static void __exit
alsa_es1688_exit(void)
1044 module_init(alsa_es1688_init
)
1045 module_exit(alsa_es1688_exit
)