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 <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/es1688.h>
30 #include <sound/initval.h>
35 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
36 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
37 MODULE_LICENSE("GPL");
39 static int snd_es1688_dsp_command(struct snd_es1688
*chip
, unsigned char val
)
43 for (i
= 10000; i
; i
--)
44 if ((inb(ES1688P(chip
, STATUS
)) & 0x80) == 0) {
45 outb(val
, ES1688P(chip
, COMMAND
));
48 #ifdef CONFIG_SND_DEBUG
49 printk(KERN_DEBUG
"snd_es1688_dsp_command: timeout (0x%x)\n", val
);
54 static int snd_es1688_dsp_get_byte(struct snd_es1688
*chip
)
58 for (i
= 1000; i
; i
--)
59 if (inb(ES1688P(chip
, DATA_AVAIL
)) & 0x80)
60 return inb(ES1688P(chip
, READ
));
61 snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip
, DATA_AVAIL
), inb(ES1688P(chip
, DATA_AVAIL
)));
65 static int snd_es1688_write(struct snd_es1688
*chip
,
66 unsigned char reg
, unsigned char data
)
68 if (!snd_es1688_dsp_command(chip
, reg
))
70 return snd_es1688_dsp_command(chip
, data
);
73 static int snd_es1688_read(struct snd_es1688
*chip
, unsigned char reg
)
75 /* Read a byte from an extended mode register of ES1688 */
76 if (!snd_es1688_dsp_command(chip
, 0xc0))
78 if (!snd_es1688_dsp_command(chip
, reg
))
80 return snd_es1688_dsp_get_byte(chip
);
83 void snd_es1688_mixer_write(struct snd_es1688
*chip
,
84 unsigned char reg
, unsigned char data
)
86 outb(reg
, ES1688P(chip
, MIXER_ADDR
));
88 outb(data
, ES1688P(chip
, MIXER_DATA
));
92 static unsigned char snd_es1688_mixer_read(struct snd_es1688
*chip
, unsigned char reg
)
96 outb(reg
, ES1688P(chip
, MIXER_ADDR
));
98 result
= inb(ES1688P(chip
, MIXER_DATA
));
103 int snd_es1688_reset(struct snd_es1688
*chip
)
107 outb(3, ES1688P(chip
, RESET
)); /* valid only for ESS chips, SB -> 1 */
109 outb(0, ES1688P(chip
, RESET
));
111 for (i
= 0; i
< 1000 && !(inb(ES1688P(chip
, DATA_AVAIL
)) & 0x80); i
++);
112 if (inb(ES1688P(chip
, READ
)) != 0xaa) {
113 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip
->port
);
116 snd_es1688_dsp_command(chip
, 0xc6); /* enable extended mode */
119 EXPORT_SYMBOL(snd_es1688_reset
);
121 static int snd_es1688_probe(struct snd_es1688
*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(KERN_ERR
"[0x%lx] ESS: AudioDrive ES488 detected, "
173 "but driver is in another place\n", chip
->port
);
176 hw
= (chip
->version
& 0x0f) >= 8 ? ES1688_HW_1688
: ES1688_HW_688
;
179 snd_printk(KERN_ERR
"[0x%lx] ESS: unknown AudioDrive chip "
180 "with version 0x%x (Jazz16 soundcard?)\n",
181 chip
->port
, chip
->version
);
185 spin_lock_irqsave(&chip
->reg_lock
, flags
);
186 snd_es1688_write(chip
, 0xb1, 0x10); /* disable IRQ */
187 snd_es1688_write(chip
, 0xb2, 0x00); /* disable DMA */
188 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
190 /* enable joystick, but disable OPL3 */
191 spin_lock_irqsave(&chip
->mixer_lock
, flags
);
192 snd_es1688_mixer_write(chip
, 0x40, 0x01);
193 spin_unlock_irqrestore(&chip
->mixer_lock
, flags
);
198 static int snd_es1688_init(struct snd_es1688
* chip
, int enable
)
200 static int irqs
[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
202 int cfg
, irq_bits
, dma
, dma_bits
, tmp
, tmp1
;
204 /* ok.. setup MPU-401 port and joystick and OPL3 */
205 cfg
= 0x01; /* enable joystick, but disable OPL3 */
206 if (enable
&& chip
->mpu_port
>= 0x300 && chip
->mpu_irq
> 0 && chip
->hardware
!= ES1688_HW_688
) {
207 tmp
= (chip
->mpu_port
& 0x0f0) >> 4;
209 switch (chip
->mpu_irq
) {
226 cfg
|= (tmp
<< 3) | (tmp1
<< 5);
231 snd_printk(KERN_DEBUG
"mpu cfg = 0x%x\n", cfg
);
233 spin_lock_irqsave(&chip
->reg_lock
, flags
);
234 snd_es1688_mixer_write(chip
, 0x40, cfg
);
235 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
237 spin_lock_irqsave(&chip
->reg_lock
, flags
);
238 snd_es1688_read(chip
, 0xb1);
239 snd_es1688_read(chip
, 0xb2);
240 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
242 cfg
= 0xf0; /* enable only DMA counter interrupt */
243 irq_bits
= irqs
[chip
->irq
& 0x0f];
245 snd_printk(KERN_ERR
"[0x%lx] ESS: bad IRQ %d "
246 "for ES1688 chip!!\n",
247 chip
->port
, chip
->irq
);
254 spin_lock_irqsave(&chip
->reg_lock
, flags
);
255 snd_es1688_write(chip
, 0xb1, cfg
| (irq_bits
<< 2));
256 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
257 cfg
= 0xf0; /* extended mode DMA enable */
259 if (dma
> 3 || dma
== 2) {
260 snd_printk(KERN_ERR
"[0x%lx] ESS: bad DMA channel %d "
261 "for ES1688 chip!!\n", chip
->port
, dma
);
264 cfg
= 0x00; /* disable all DMA */
272 spin_lock_irqsave(&chip
->reg_lock
, flags
);
273 snd_es1688_write(chip
, 0xb2, cfg
| (dma_bits
<< 2));
274 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
276 spin_lock_irqsave(&chip
->reg_lock
, flags
);
277 snd_es1688_write(chip
, 0xb1, 0x10); /* disable IRQ */
278 snd_es1688_write(chip
, 0xb2, 0x00); /* disable DMA */
279 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
281 spin_lock_irqsave(&chip
->reg_lock
, flags
);
282 snd_es1688_read(chip
, 0xb1);
283 snd_es1688_read(chip
, 0xb2);
284 snd_es1688_reset(chip
);
285 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
293 static struct snd_ratnum clocks
[2] = {
308 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks
= {
313 static void snd_es1688_set_rate(struct snd_es1688
*chip
, struct snd_pcm_substream
*substream
)
315 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
316 unsigned int bits
, divider
;
318 if (runtime
->rate_num
== clocks
[0].num
)
319 bits
= 256 - runtime
->rate_den
;
321 bits
= 128 - runtime
->rate_den
;
322 /* set filter register */
323 divider
= 256 - 7160000*20/(8*82*runtime
->rate
);
324 /* write result to hardware */
325 snd_es1688_write(chip
, 0xa1, bits
);
326 snd_es1688_write(chip
, 0xa2, divider
);
329 static int snd_es1688_ioctl(struct snd_pcm_substream
*substream
,
330 unsigned int cmd
, void *arg
)
332 return snd_pcm_lib_ioctl(substream
, cmd
, arg
);
335 static int snd_es1688_trigger(struct snd_es1688
*chip
, int cmd
, unsigned char value
)
339 if (cmd
== SNDRV_PCM_TRIGGER_STOP
) {
341 } else if (cmd
!= SNDRV_PCM_TRIGGER_START
) {
344 spin_lock(&chip
->reg_lock
);
345 chip
->trigger_value
= value
;
346 val
= snd_es1688_read(chip
, 0xb8);
347 if ((val
< 0) || (val
& 0x0f) == value
) {
348 spin_unlock(&chip
->reg_lock
);
349 return -EINVAL
; /* something is wrong */
352 printk(KERN_DEBUG
"trigger: val = 0x%x, value = 0x%x\n", val
, value
);
353 printk(KERN_DEBUG
"trigger: pointer = 0x%x\n",
354 snd_dma_pointer(chip
->dma8
, chip
->dma_size
));
356 snd_es1688_write(chip
, 0xb8, (val
& 0xf0) | value
);
357 spin_unlock(&chip
->reg_lock
);
361 static int snd_es1688_hw_params(struct snd_pcm_substream
*substream
,
362 struct snd_pcm_hw_params
*hw_params
)
364 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
367 static int snd_es1688_hw_free(struct snd_pcm_substream
*substream
)
369 return snd_pcm_lib_free_pages(substream
);
372 static int snd_es1688_playback_prepare(struct snd_pcm_substream
*substream
)
375 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
376 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
377 unsigned int size
= snd_pcm_lib_buffer_bytes(substream
);
378 unsigned int count
= snd_pcm_lib_period_bytes(substream
);
380 chip
->dma_size
= size
;
381 spin_lock_irqsave(&chip
->reg_lock
, flags
);
382 snd_es1688_reset(chip
);
383 snd_es1688_set_rate(chip
, substream
);
384 snd_es1688_write(chip
, 0xb8, 4); /* auto init DMA mode */
385 snd_es1688_write(chip
, 0xa8, (snd_es1688_read(chip
, 0xa8) & ~0x03) | (3 - runtime
->channels
));
386 snd_es1688_write(chip
, 0xb9, 2); /* demand mode (4 bytes/request) */
387 if (runtime
->channels
== 1) {
388 if (snd_pcm_format_width(runtime
->format
) == 8) {
390 snd_es1688_write(chip
, 0xb6, 0x80);
391 snd_es1688_write(chip
, 0xb7, 0x51);
392 snd_es1688_write(chip
, 0xb7, 0xd0);
395 snd_es1688_write(chip
, 0xb6, 0x00);
396 snd_es1688_write(chip
, 0xb7, 0x71);
397 snd_es1688_write(chip
, 0xb7, 0xf4);
400 if (snd_pcm_format_width(runtime
->format
) == 8) {
402 snd_es1688_write(chip
, 0xb6, 0x80);
403 snd_es1688_write(chip
, 0xb7, 0x51);
404 snd_es1688_write(chip
, 0xb7, 0x98);
407 snd_es1688_write(chip
, 0xb6, 0x00);
408 snd_es1688_write(chip
, 0xb7, 0x71);
409 snd_es1688_write(chip
, 0xb7, 0xbc);
412 snd_es1688_write(chip
, 0xb1, (snd_es1688_read(chip
, 0xb1) & 0x0f) | 0x50);
413 snd_es1688_write(chip
, 0xb2, (snd_es1688_read(chip
, 0xb2) & 0x0f) | 0x50);
414 snd_es1688_dsp_command(chip
, ES1688_DSP_CMD_SPKON
);
415 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
418 snd_dma_program(chip
->dma8
, runtime
->dma_addr
, size
, DMA_MODE_WRITE
| DMA_AUTOINIT
);
419 spin_lock_irqsave(&chip
->reg_lock
, flags
);
420 snd_es1688_write(chip
, 0xa4, (unsigned char) count
);
421 snd_es1688_write(chip
, 0xa5, (unsigned char) (count
>> 8));
422 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
426 static int snd_es1688_playback_trigger(struct snd_pcm_substream
*substream
,
429 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
430 return snd_es1688_trigger(chip
, cmd
, 0x05);
433 static int snd_es1688_capture_prepare(struct snd_pcm_substream
*substream
)
436 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
437 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
438 unsigned int size
= snd_pcm_lib_buffer_bytes(substream
);
439 unsigned int count
= snd_pcm_lib_period_bytes(substream
);
441 chip
->dma_size
= size
;
442 spin_lock_irqsave(&chip
->reg_lock
, flags
);
443 snd_es1688_reset(chip
);
444 snd_es1688_set_rate(chip
, substream
);
445 snd_es1688_dsp_command(chip
, ES1688_DSP_CMD_SPKOFF
);
446 snd_es1688_write(chip
, 0xb8, 0x0e); /* auto init DMA mode */
447 snd_es1688_write(chip
, 0xa8, (snd_es1688_read(chip
, 0xa8) & ~0x03) | (3 - runtime
->channels
));
448 snd_es1688_write(chip
, 0xb9, 2); /* demand mode (4 bytes/request) */
449 if (runtime
->channels
== 1) {
450 if (snd_pcm_format_width(runtime
->format
) == 8) {
452 snd_es1688_write(chip
, 0xb7, 0x51);
453 snd_es1688_write(chip
, 0xb7, 0xd0);
456 snd_es1688_write(chip
, 0xb7, 0x71);
457 snd_es1688_write(chip
, 0xb7, 0xf4);
460 if (snd_pcm_format_width(runtime
->format
) == 8) {
462 snd_es1688_write(chip
, 0xb7, 0x51);
463 snd_es1688_write(chip
, 0xb7, 0x98);
466 snd_es1688_write(chip
, 0xb7, 0x71);
467 snd_es1688_write(chip
, 0xb7, 0xbc);
470 snd_es1688_write(chip
, 0xb1, (snd_es1688_read(chip
, 0xb1) & 0x0f) | 0x50);
471 snd_es1688_write(chip
, 0xb2, (snd_es1688_read(chip
, 0xb2) & 0x0f) | 0x50);
472 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
475 snd_dma_program(chip
->dma8
, runtime
->dma_addr
, size
, DMA_MODE_READ
| DMA_AUTOINIT
);
476 spin_lock_irqsave(&chip
->reg_lock
, flags
);
477 snd_es1688_write(chip
, 0xa4, (unsigned char) count
);
478 snd_es1688_write(chip
, 0xa5, (unsigned char) (count
>> 8));
479 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
483 static int snd_es1688_capture_trigger(struct snd_pcm_substream
*substream
,
486 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
487 return snd_es1688_trigger(chip
, cmd
, 0x0f);
490 static irqreturn_t
snd_es1688_interrupt(int irq
, void *dev_id
)
492 struct snd_es1688
*chip
= dev_id
;
494 if (chip
->trigger_value
== 0x05) /* ok.. playback is active */
495 snd_pcm_period_elapsed(chip
->playback_substream
);
496 if (chip
->trigger_value
== 0x0f) /* ok.. capture is active */
497 snd_pcm_period_elapsed(chip
->capture_substream
);
499 inb(ES1688P(chip
, DATA_AVAIL
)); /* ack interrupt */
503 static snd_pcm_uframes_t
snd_es1688_playback_pointer(struct snd_pcm_substream
*substream
)
505 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
508 if (chip
->trigger_value
!= 0x05)
510 ptr
= snd_dma_pointer(chip
->dma8
, chip
->dma_size
);
511 return bytes_to_frames(substream
->runtime
, ptr
);
514 static snd_pcm_uframes_t
snd_es1688_capture_pointer(struct snd_pcm_substream
*substream
)
516 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
519 if (chip
->trigger_value
!= 0x0f)
521 ptr
= snd_dma_pointer(chip
->dma8
, chip
->dma_size
);
522 return bytes_to_frames(substream
->runtime
, ptr
);
529 static struct snd_pcm_hardware snd_es1688_playback
=
531 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
532 SNDRV_PCM_INFO_MMAP_VALID
),
533 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
534 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
539 .buffer_bytes_max
= 65536,
540 .period_bytes_min
= 64,
541 .period_bytes_max
= 65536,
547 static struct snd_pcm_hardware snd_es1688_capture
=
549 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
550 SNDRV_PCM_INFO_MMAP_VALID
),
551 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
552 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
557 .buffer_bytes_max
= 65536,
558 .period_bytes_min
= 64,
559 .period_bytes_max
= 65536,
569 static int snd_es1688_playback_open(struct snd_pcm_substream
*substream
)
571 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
572 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
574 if (chip
->capture_substream
!= NULL
)
576 chip
->playback_substream
= substream
;
577 runtime
->hw
= snd_es1688_playback
;
578 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
579 &hw_constraints_clocks
);
583 static int snd_es1688_capture_open(struct snd_pcm_substream
*substream
)
585 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
586 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
588 if (chip
->playback_substream
!= NULL
)
590 chip
->capture_substream
= substream
;
591 runtime
->hw
= snd_es1688_capture
;
592 snd_pcm_hw_constraint_ratnums(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
593 &hw_constraints_clocks
);
597 static int snd_es1688_playback_close(struct snd_pcm_substream
*substream
)
599 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
601 chip
->playback_substream
= NULL
;
605 static int snd_es1688_capture_close(struct snd_pcm_substream
*substream
)
607 struct snd_es1688
*chip
= snd_pcm_substream_chip(substream
);
609 chip
->capture_substream
= NULL
;
613 static int snd_es1688_free(struct snd_es1688
*chip
)
615 if (chip
->hardware
!= ES1688_HW_UNDEF
)
616 snd_es1688_init(chip
, 0);
618 release_and_free_resource(chip
->res_port
);
620 free_irq(chip
->irq
, (void *) chip
);
621 if (chip
->dma8
>= 0) {
622 disable_dma(chip
->dma8
);
623 free_dma(chip
->dma8
);
628 static int snd_es1688_dev_free(struct snd_device
*device
)
630 struct snd_es1688
*chip
= device
->device_data
;
631 return snd_es1688_free(chip
);
634 static const char *snd_es1688_chip_id(struct snd_es1688
*chip
)
637 sprintf(tmp
, "ES%s688 rev %i", chip
->hardware
== ES1688_HW_688
? "" : "1", chip
->version
& 0x0f);
641 int snd_es1688_create(struct snd_card
*card
,
642 struct snd_es1688
*chip
,
644 unsigned long mpu_port
,
648 unsigned short hardware
)
650 static struct snd_device_ops ops
= {
651 .dev_free
= snd_es1688_dev_free
,
660 chip
->hardware
= ES1688_HW_UNDEF
;
662 chip
->res_port
= request_region(port
+ 4, 12, "ES1688");
663 if (chip
->res_port
== NULL
) {
664 snd_printk(KERN_ERR
"es1688: can't grab port 0x%lx\n", port
+ 4);
669 err
= request_irq(irq
, snd_es1688_interrupt
, 0, "ES1688", (void *) chip
);
671 snd_printk(KERN_ERR
"es1688: can't grab IRQ %d\n", irq
);
676 err
= request_dma(dma8
, "ES1688");
679 snd_printk(KERN_ERR
"es1688: can't grab DMA8 %d\n", dma8
);
684 spin_lock_init(&chip
->reg_lock
);
685 spin_lock_init(&chip
->mixer_lock
);
688 if (mpu_port
< 0x300 || mpu_port
> 0x330)
690 chip
->mpu_port
= mpu_port
;
691 chip
->mpu_irq
= mpu_irq
;
692 chip
->hardware
= hardware
;
694 err
= snd_es1688_probe(chip
);
698 err
= snd_es1688_init(chip
, 1);
702 /* Register device */
703 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
);
706 snd_es1688_free(chip
);
710 static struct snd_pcm_ops snd_es1688_playback_ops
= {
711 .open
= snd_es1688_playback_open
,
712 .close
= snd_es1688_playback_close
,
713 .ioctl
= snd_es1688_ioctl
,
714 .hw_params
= snd_es1688_hw_params
,
715 .hw_free
= snd_es1688_hw_free
,
716 .prepare
= snd_es1688_playback_prepare
,
717 .trigger
= snd_es1688_playback_trigger
,
718 .pointer
= snd_es1688_playback_pointer
,
721 static struct snd_pcm_ops snd_es1688_capture_ops
= {
722 .open
= snd_es1688_capture_open
,
723 .close
= snd_es1688_capture_close
,
724 .ioctl
= snd_es1688_ioctl
,
725 .hw_params
= snd_es1688_hw_params
,
726 .hw_free
= snd_es1688_hw_free
,
727 .prepare
= snd_es1688_capture_prepare
,
728 .trigger
= snd_es1688_capture_trigger
,
729 .pointer
= snd_es1688_capture_pointer
,
732 int snd_es1688_pcm(struct snd_card
*card
, struct snd_es1688
*chip
,
733 int device
, struct snd_pcm
**rpcm
)
738 err
= snd_pcm_new(card
, "ESx688", device
, 1, 1, &pcm
);
742 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_es1688_playback_ops
);
743 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_es1688_capture_ops
);
745 pcm
->private_data
= chip
;
746 pcm
->info_flags
= SNDRV_PCM_INFO_HALF_DUPLEX
;
747 sprintf(pcm
->name
, snd_es1688_chip_id(chip
));
750 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
763 static int snd_es1688_info_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
765 static char *texts
[9] = {
766 "Mic", "Mic Master", "CD", "AOUT",
767 "Mic1", "Mix", "Line", "Master"
770 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
772 uinfo
->value
.enumerated
.items
= 8;
773 if (uinfo
->value
.enumerated
.item
> 7)
774 uinfo
->value
.enumerated
.item
= 7;
775 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
779 static int snd_es1688_get_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
781 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
782 ucontrol
->value
.enumerated
.item
[0] = snd_es1688_mixer_read(chip
, ES1688_REC_DEV
) & 7;
786 static int snd_es1688_put_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
788 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
790 unsigned char oval
, nval
;
793 if (ucontrol
->value
.enumerated
.item
[0] > 8)
795 spin_lock_irqsave(&chip
->reg_lock
, flags
);
796 oval
= snd_es1688_mixer_read(chip
, ES1688_REC_DEV
);
797 nval
= (ucontrol
->value
.enumerated
.item
[0] & 7) | (oval
& ~15);
798 change
= nval
!= oval
;
800 snd_es1688_mixer_write(chip
, ES1688_REC_DEV
, nval
);
801 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
805 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
806 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
807 .info = snd_es1688_info_single, \
808 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
809 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
811 static int snd_es1688_info_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
813 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
815 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
817 uinfo
->value
.integer
.min
= 0;
818 uinfo
->value
.integer
.max
= mask
;
822 static int snd_es1688_get_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
824 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
826 int reg
= kcontrol
->private_value
& 0xff;
827 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
828 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
829 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
831 spin_lock_irqsave(&chip
->reg_lock
, flags
);
832 ucontrol
->value
.integer
.value
[0] = (snd_es1688_mixer_read(chip
, reg
) >> shift
) & mask
;
833 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
835 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
839 static int snd_es1688_put_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
841 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
843 int reg
= kcontrol
->private_value
& 0xff;
844 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
845 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
846 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
848 unsigned char oval
, nval
;
850 nval
= (ucontrol
->value
.integer
.value
[0] & mask
);
854 spin_lock_irqsave(&chip
->reg_lock
, flags
);
855 oval
= snd_es1688_mixer_read(chip
, reg
);
856 nval
= (oval
& ~(mask
<< shift
)) | nval
;
857 change
= nval
!= oval
;
859 snd_es1688_mixer_write(chip
, reg
, nval
);
860 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
864 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
865 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
866 .info = snd_es1688_info_double, \
867 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
868 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
870 static int snd_es1688_info_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
872 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
874 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
876 uinfo
->value
.integer
.min
= 0;
877 uinfo
->value
.integer
.max
= mask
;
881 static int snd_es1688_get_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
883 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
885 int left_reg
= kcontrol
->private_value
& 0xff;
886 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
887 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
888 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
889 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
890 int invert
= (kcontrol
->private_value
>> 22) & 1;
891 unsigned char left
, right
;
893 spin_lock_irqsave(&chip
->reg_lock
, flags
);
895 left
= snd_es1688_mixer_read(chip
, left_reg
);
897 left
= snd_es1688_read(chip
, left_reg
);
898 if (left_reg
!= right_reg
) {
899 if (right_reg
< 0xa0)
900 right
= snd_es1688_mixer_read(chip
, right_reg
);
902 right
= snd_es1688_read(chip
, right_reg
);
905 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
906 ucontrol
->value
.integer
.value
[0] = (left
>> shift_left
) & mask
;
907 ucontrol
->value
.integer
.value
[1] = (right
>> shift_right
) & mask
;
909 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
910 ucontrol
->value
.integer
.value
[1] = mask
- ucontrol
->value
.integer
.value
[1];
915 static int snd_es1688_put_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
917 struct snd_es1688
*chip
= snd_kcontrol_chip(kcontrol
);
919 int left_reg
= kcontrol
->private_value
& 0xff;
920 int right_reg
= (kcontrol
->private_value
>> 8) & 0xff;
921 int shift_left
= (kcontrol
->private_value
>> 16) & 0x07;
922 int shift_right
= (kcontrol
->private_value
>> 19) & 0x07;
923 int mask
= (kcontrol
->private_value
>> 24) & 0xff;
924 int invert
= (kcontrol
->private_value
>> 22) & 1;
926 unsigned char val1
, val2
, oval1
, oval2
;
928 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
929 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
935 val2
<<= shift_right
;
936 spin_lock_irqsave(&chip
->reg_lock
, flags
);
937 if (left_reg
!= right_reg
) {
939 oval1
= snd_es1688_mixer_read(chip
, left_reg
);
941 oval1
= snd_es1688_read(chip
, left_reg
);
942 if (right_reg
< 0xa0)
943 oval2
= snd_es1688_mixer_read(chip
, right_reg
);
945 oval2
= snd_es1688_read(chip
, right_reg
);
946 val1
= (oval1
& ~(mask
<< shift_left
)) | val1
;
947 val2
= (oval2
& ~(mask
<< shift_right
)) | val2
;
948 change
= val1
!= oval1
|| val2
!= oval2
;
951 snd_es1688_mixer_write(chip
, left_reg
, val1
);
953 snd_es1688_write(chip
, left_reg
, val1
);
954 if (right_reg
< 0xa0)
955 snd_es1688_mixer_write(chip
, right_reg
, val1
);
957 snd_es1688_write(chip
, right_reg
, val1
);
961 oval1
= snd_es1688_mixer_read(chip
, left_reg
);
963 oval1
= snd_es1688_read(chip
, left_reg
);
964 val1
= (oval1
& ~((mask
<< shift_left
) | (mask
<< shift_right
))) | val1
| val2
;
965 change
= val1
!= oval1
;
968 snd_es1688_mixer_write(chip
, left_reg
, val1
);
970 snd_es1688_write(chip
, left_reg
, val1
);
974 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
978 static struct snd_kcontrol_new snd_es1688_controls
[] = {
979 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV
, ES1688_MASTER_DEV
, 4, 0, 15, 0),
980 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV
, ES1688_PCM_DEV
, 4, 0, 15, 0),
981 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV
, ES1688_LINE_DEV
, 4, 0, 15, 0),
982 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV
, ES1688_CD_DEV
, 4, 0, 15, 0),
983 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV
, ES1688_FM_DEV
, 4, 0, 15, 0),
984 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV
, ES1688_MIC_DEV
, 4, 0, 15, 0),
985 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV
, ES1688_AUX_DEV
, 4, 0, 15, 0),
986 ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV
, 0, 7, 0),
987 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV
, ES1688_RECLEV_DEV
, 4, 0, 15, 0),
988 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV
, 4, 1, 1),
990 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
991 .name
= "Capture Source",
992 .info
= snd_es1688_info_mux
,
993 .get
= snd_es1688_get_mux
,
994 .put
= snd_es1688_put_mux
,
998 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
1000 static unsigned char snd_es1688_init_table
[][2] = {
1001 { ES1688_MASTER_DEV
, 0 },
1002 { ES1688_PCM_DEV
, 0 },
1003 { ES1688_LINE_DEV
, 0 },
1004 { ES1688_CD_DEV
, 0 },
1005 { ES1688_FM_DEV
, 0 },
1006 { ES1688_MIC_DEV
, 0 },
1007 { ES1688_AUX_DEV
, 0 },
1008 { ES1688_SPEAKER_DEV
, 0 },
1009 { ES1688_RECLEV_DEV
, 0 },
1010 { ES1688_REC_DEV
, 0x17 }
1013 int snd_es1688_mixer(struct snd_card
*card
, struct snd_es1688
*chip
)
1017 unsigned char reg
, val
;
1019 if (snd_BUG_ON(!chip
|| !card
))
1022 strcpy(card
->mixername
, snd_es1688_chip_id(chip
));
1024 for (idx
= 0; idx
< ARRAY_SIZE(snd_es1688_controls
); idx
++) {
1025 if ((err
= snd_ctl_add(card
, snd_ctl_new1(&snd_es1688_controls
[idx
], chip
))) < 0)
1028 for (idx
= 0; idx
< ES1688_INIT_TABLE_SIZE
; idx
++) {
1029 reg
= snd_es1688_init_table
[idx
][0];
1030 val
= snd_es1688_init_table
[idx
][1];
1032 snd_es1688_mixer_write(chip
, reg
, val
);
1034 snd_es1688_write(chip
, reg
, val
);
1039 EXPORT_SYMBOL(snd_es1688_mixer_write
);
1040 EXPORT_SYMBOL(snd_es1688_create
);
1041 EXPORT_SYMBOL(snd_es1688_pcm
);
1042 EXPORT_SYMBOL(snd_es1688_mixer
);
1048 static int __init
alsa_es1688_init(void)
1053 static void __exit
alsa_es1688_exit(void)
1057 module_init(alsa_es1688_init
)
1058 module_exit(alsa_es1688_exit
)