Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / sound / isa / es1688 / es1688_lib.c
bloba2ab99f2ac35fc2f513465be6c1092856651b297
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of ESS ES1688/688/488 chip
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/ioport.h>
28 #include <sound/core.h>
29 #include <sound/es1688.h>
30 #include <sound/initval.h>
32 #include <asm/io.h>
33 #include <asm/dma.h>
35 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.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)
41 int i;
43 for (i = 10000; i; i--)
44 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
45 outb(val, ES1688P(chip, COMMAND));
46 return 1;
48 #ifdef CONFIG_SND_DEBUG
49 printk("snd_es1688_dsp_command: timeout (0x%x)\n", val);
50 #endif
51 return 0;
54 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
56 int i;
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)));
62 return -ENODEV;
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))
69 return 0;
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))
77 return -1;
78 if (!snd_es1688_dsp_command(chip, reg))
79 return -1;
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));
87 udelay(10);
88 outb(data, ES1688P(chip, MIXER_DATA));
89 udelay(10);
92 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
94 unsigned char result;
96 outb(reg, ES1688P(chip, MIXER_ADDR));
97 udelay(10);
98 result = inb(ES1688P(chip, MIXER_DATA));
99 udelay(10);
100 return result;
103 static int snd_es1688_reset(struct snd_es1688 *chip)
105 int i;
107 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
108 udelay(10);
109 outb(0, ES1688P(chip, RESET));
110 udelay(30);
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);
114 return -ENODEV;
116 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
117 return 0;
120 static int snd_es1688_probe(struct snd_es1688 *chip)
122 unsigned long flags;
123 unsigned short major, minor, hw;
124 int i;
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);
146 return -ENODEV;
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) {
152 if (major == 0) {
153 major = inb(ES1688P(chip, READ));
154 } else {
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;
165 if (!chip->version)
166 return -ENODEV; /* probably SB */
168 hw = ES1688_HW_AUTO;
169 switch (chip->version & 0xfff0) {
170 case 0x4880:
171 snd_printk("[0x%lx] ESS: AudioDrive ES488 detected, but driver is in another place\n", chip->port);
172 return -ENODEV;
173 case 0x6880:
174 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
175 break;
176 default:
177 snd_printk("[0x%lx] ESS: unknown AudioDrive chip with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version);
178 return -ENODEV;
181 spin_lock_irqsave(&chip->reg_lock, flags);
182 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
183 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
184 spin_unlock_irqrestore(&chip->reg_lock, flags);
186 /* enable joystick, but disable OPL3 */
187 spin_lock_irqsave(&chip->mixer_lock, flags);
188 snd_es1688_mixer_write(chip, 0x40, 0x01);
189 spin_unlock_irqrestore(&chip->mixer_lock, flags);
191 return 0;
194 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
196 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
197 unsigned long flags;
198 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
200 /* ok.. setup MPU-401 port and joystick and OPL3 */
201 cfg = 0x01; /* enable joystick, but disable OPL3 */
202 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
203 tmp = (chip->mpu_port & 0x0f0) >> 4;
204 if (tmp <= 3) {
205 switch (chip->mpu_irq) {
206 case 9:
207 tmp1 = 4;
208 break;
209 case 5:
210 tmp1 = 5;
211 break;
212 case 7:
213 tmp1 = 6;
214 break;
215 case 10:
216 tmp1 = 7;
217 break;
218 default:
219 tmp1 = 0;
221 if (tmp1) {
222 cfg |= (tmp << 3) | (tmp1 << 5);
226 #if 0
227 snd_printk("mpu cfg = 0x%x\n", cfg);
228 #endif
229 spin_lock_irqsave(&chip->reg_lock, flags);
230 snd_es1688_mixer_write(chip, 0x40, cfg);
231 spin_unlock_irqrestore(&chip->reg_lock, flags);
232 /* --- */
233 spin_lock_irqsave(&chip->reg_lock, flags);
234 snd_es1688_read(chip, 0xb1);
235 snd_es1688_read(chip, 0xb2);
236 spin_unlock_irqrestore(&chip->reg_lock, flags);
237 if (enable) {
238 cfg = 0xf0; /* enable only DMA counter interrupt */
239 irq_bits = irqs[chip->irq & 0x0f];
240 if (irq_bits < 0) {
241 snd_printk("[0x%lx] ESS: bad IRQ %d for ES1688 chip!!\n", chip->port, chip->irq);
242 #if 0
243 irq_bits = 0;
244 cfg = 0x10;
245 #endif
246 return -EINVAL;
248 spin_lock_irqsave(&chip->reg_lock, flags);
249 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
250 spin_unlock_irqrestore(&chip->reg_lock, flags);
251 cfg = 0xf0; /* extended mode DMA enable */
252 dma = chip->dma8;
253 if (dma > 3 || dma == 2) {
254 snd_printk("[0x%lx] ESS: bad DMA channel %d for ES1688 chip!!\n", chip->port, dma);
255 #if 0
256 dma_bits = 0;
257 cfg = 0x00; /* disable all DMA */
258 #endif
259 return -EINVAL;
260 } else {
261 dma_bits = dma;
262 if (dma != 3)
263 dma_bits++;
265 spin_lock_irqsave(&chip->reg_lock, flags);
266 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
267 spin_unlock_irqrestore(&chip->reg_lock, flags);
268 } else {
269 spin_lock_irqsave(&chip->reg_lock, flags);
270 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
271 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
272 spin_unlock_irqrestore(&chip->reg_lock, flags);
274 spin_lock_irqsave(&chip->reg_lock, flags);
275 snd_es1688_read(chip, 0xb1);
276 snd_es1688_read(chip, 0xb2);
277 snd_es1688_reset(chip);
278 spin_unlock_irqrestore(&chip->reg_lock, flags);
279 return 0;
286 static struct snd_ratnum clocks[2] = {
288 .num = 795444,
289 .den_min = 1,
290 .den_max = 128,
291 .den_step = 1,
294 .num = 397722,
295 .den_min = 1,
296 .den_max = 128,
297 .den_step = 1,
301 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
302 .nrats = 2,
303 .rats = clocks,
306 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
308 struct snd_pcm_runtime *runtime = substream->runtime;
309 unsigned int bits, divider;
311 if (runtime->rate_num == clocks[0].num)
312 bits = 256 - runtime->rate_den;
313 else
314 bits = 128 - runtime->rate_den;
315 /* set filter register */
316 divider = 256 - 7160000*20/(8*82*runtime->rate);
317 /* write result to hardware */
318 snd_es1688_write(chip, 0xa1, bits);
319 snd_es1688_write(chip, 0xa2, divider);
322 static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
323 unsigned int cmd, void *arg)
325 return snd_pcm_lib_ioctl(substream, cmd, arg);
328 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
330 int val;
332 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
333 value = 0x00;
334 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
335 return -EINVAL;
337 spin_lock(&chip->reg_lock);
338 chip->trigger_value = value;
339 val = snd_es1688_read(chip, 0xb8);
340 if ((val < 0) || (val & 0x0f) == value) {
341 spin_unlock(&chip->reg_lock);
342 return -EINVAL; /* something is wrong */
344 #if 0
345 printk("trigger: val = 0x%x, value = 0x%x\n", val, value);
346 printk("trigger: pointer = 0x%x\n", snd_dma_pointer(chip->dma8, chip->dma_size));
347 #endif
348 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
349 spin_unlock(&chip->reg_lock);
350 return 0;
353 static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *hw_params)
356 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
359 static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
361 return snd_pcm_lib_free_pages(substream);
364 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
366 unsigned long flags;
367 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
368 struct snd_pcm_runtime *runtime = substream->runtime;
369 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
370 unsigned int count = snd_pcm_lib_period_bytes(substream);
372 chip->dma_size = size;
373 spin_lock_irqsave(&chip->reg_lock, flags);
374 snd_es1688_reset(chip);
375 snd_es1688_set_rate(chip, substream);
376 snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
377 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
378 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
379 if (runtime->channels == 1) {
380 if (snd_pcm_format_width(runtime->format) == 8) {
381 /* 8. bit mono */
382 snd_es1688_write(chip, 0xb6, 0x80);
383 snd_es1688_write(chip, 0xb7, 0x51);
384 snd_es1688_write(chip, 0xb7, 0xd0);
385 } else {
386 /* 16. bit mono */
387 snd_es1688_write(chip, 0xb6, 0x00);
388 snd_es1688_write(chip, 0xb7, 0x71);
389 snd_es1688_write(chip, 0xb7, 0xf4);
391 } else {
392 if (snd_pcm_format_width(runtime->format) == 8) {
393 /* 8. bit stereo */
394 snd_es1688_write(chip, 0xb6, 0x80);
395 snd_es1688_write(chip, 0xb7, 0x51);
396 snd_es1688_write(chip, 0xb7, 0x98);
397 } else {
398 /* 16. bit stereo */
399 snd_es1688_write(chip, 0xb6, 0x00);
400 snd_es1688_write(chip, 0xb7, 0x71);
401 snd_es1688_write(chip, 0xb7, 0xbc);
404 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
405 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
406 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
407 spin_unlock_irqrestore(&chip->reg_lock, flags);
408 /* --- */
409 count = -count;
410 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
411 spin_lock_irqsave(&chip->reg_lock, flags);
412 snd_es1688_write(chip, 0xa4, (unsigned char) count);
413 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
414 spin_unlock_irqrestore(&chip->reg_lock, flags);
415 return 0;
418 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
419 int cmd)
421 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
422 return snd_es1688_trigger(chip, cmd, 0x05);
425 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
427 unsigned long flags;
428 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
429 struct snd_pcm_runtime *runtime = substream->runtime;
430 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
431 unsigned int count = snd_pcm_lib_period_bytes(substream);
433 chip->dma_size = size;
434 spin_lock_irqsave(&chip->reg_lock, flags);
435 snd_es1688_reset(chip);
436 snd_es1688_set_rate(chip, substream);
437 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
438 snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
439 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
440 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
441 if (runtime->channels == 1) {
442 if (snd_pcm_format_width(runtime->format) == 8) {
443 /* 8. bit mono */
444 snd_es1688_write(chip, 0xb7, 0x51);
445 snd_es1688_write(chip, 0xb7, 0xd0);
446 } else {
447 /* 16. bit mono */
448 snd_es1688_write(chip, 0xb7, 0x71);
449 snd_es1688_write(chip, 0xb7, 0xf4);
451 } else {
452 if (snd_pcm_format_width(runtime->format) == 8) {
453 /* 8. bit stereo */
454 snd_es1688_write(chip, 0xb7, 0x51);
455 snd_es1688_write(chip, 0xb7, 0x98);
456 } else {
457 /* 16. bit stereo */
458 snd_es1688_write(chip, 0xb7, 0x71);
459 snd_es1688_write(chip, 0xb7, 0xbc);
462 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
463 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
464 spin_unlock_irqrestore(&chip->reg_lock, flags);
465 /* --- */
466 count = -count;
467 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
468 spin_lock_irqsave(&chip->reg_lock, flags);
469 snd_es1688_write(chip, 0xa4, (unsigned char) count);
470 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
471 spin_unlock_irqrestore(&chip->reg_lock, flags);
472 return 0;
475 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
476 int cmd)
478 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
479 return snd_es1688_trigger(chip, cmd, 0x0f);
482 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
484 struct snd_es1688 *chip = dev_id;
486 if (chip->trigger_value == 0x05) /* ok.. playback is active */
487 snd_pcm_period_elapsed(chip->playback_substream);
488 if (chip->trigger_value == 0x0f) /* ok.. capture is active */
489 snd_pcm_period_elapsed(chip->capture_substream);
491 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
492 return IRQ_HANDLED;
495 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
497 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
498 size_t ptr;
500 if (chip->trigger_value != 0x05)
501 return 0;
502 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
503 return bytes_to_frames(substream->runtime, ptr);
506 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
508 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
509 size_t ptr;
511 if (chip->trigger_value != 0x0f)
512 return 0;
513 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
514 return bytes_to_frames(substream->runtime, ptr);
521 static struct snd_pcm_hardware snd_es1688_playback =
523 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
524 SNDRV_PCM_INFO_MMAP_VALID),
525 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
526 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
527 .rate_min = 4000,
528 .rate_max = 48000,
529 .channels_min = 1,
530 .channels_max = 2,
531 .buffer_bytes_max = 65536,
532 .period_bytes_min = 64,
533 .period_bytes_max = 65536,
534 .periods_min = 1,
535 .periods_max = 1024,
536 .fifo_size = 0,
539 static struct snd_pcm_hardware snd_es1688_capture =
541 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
542 SNDRV_PCM_INFO_MMAP_VALID),
543 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
544 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
545 .rate_min = 4000,
546 .rate_max = 48000,
547 .channels_min = 1,
548 .channels_max = 2,
549 .buffer_bytes_max = 65536,
550 .period_bytes_min = 64,
551 .period_bytes_max = 65536,
552 .periods_min = 1,
553 .periods_max = 1024,
554 .fifo_size = 0,
561 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
563 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
564 struct snd_pcm_runtime *runtime = substream->runtime;
566 if (chip->capture_substream != NULL)
567 return -EAGAIN;
568 chip->playback_substream = substream;
569 runtime->hw = snd_es1688_playback;
570 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
571 &hw_constraints_clocks);
572 return 0;
575 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
577 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
578 struct snd_pcm_runtime *runtime = substream->runtime;
580 if (chip->playback_substream != NULL)
581 return -EAGAIN;
582 chip->capture_substream = substream;
583 runtime->hw = snd_es1688_capture;
584 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
585 &hw_constraints_clocks);
586 return 0;
589 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
591 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
593 chip->playback_substream = NULL;
594 return 0;
597 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
599 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
601 chip->capture_substream = NULL;
602 return 0;
605 static int snd_es1688_free(struct snd_es1688 *chip)
607 if (chip->res_port) {
608 snd_es1688_init(chip, 0);
609 release_and_free_resource(chip->res_port);
611 if (chip->irq >= 0)
612 free_irq(chip->irq, (void *) chip);
613 if (chip->dma8 >= 0) {
614 disable_dma(chip->dma8);
615 free_dma(chip->dma8);
617 kfree(chip);
618 return 0;
621 static int snd_es1688_dev_free(struct snd_device *device)
623 struct snd_es1688 *chip = device->device_data;
624 return snd_es1688_free(chip);
627 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
629 static char tmp[16];
630 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
631 return tmp;
634 int snd_es1688_create(struct snd_card *card,
635 unsigned long port,
636 unsigned long mpu_port,
637 int irq,
638 int mpu_irq,
639 int dma8,
640 unsigned short hardware,
641 struct snd_es1688 **rchip)
643 static struct snd_device_ops ops = {
644 .dev_free = snd_es1688_dev_free,
647 struct snd_es1688 *chip;
648 int err;
650 *rchip = NULL;
651 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
652 if (chip == NULL)
653 return -ENOMEM;
654 chip->irq = -1;
655 chip->dma8 = -1;
657 if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
658 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
659 snd_es1688_free(chip);
660 return -EBUSY;
662 if (request_irq(irq, snd_es1688_interrupt, IRQF_DISABLED, "ES1688", (void *) chip)) {
663 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
664 snd_es1688_free(chip);
665 return -EBUSY;
667 chip->irq = irq;
668 if (request_dma(dma8, "ES1688")) {
669 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
670 snd_es1688_free(chip);
671 return -EBUSY;
673 chip->dma8 = dma8;
675 spin_lock_init(&chip->reg_lock);
676 spin_lock_init(&chip->mixer_lock);
677 chip->card = card;
678 chip->port = port;
679 mpu_port &= ~0x000f;
680 if (mpu_port < 0x300 || mpu_port > 0x330)
681 mpu_port = 0;
682 chip->mpu_port = mpu_port;
683 chip->mpu_irq = mpu_irq;
684 chip->hardware = hardware;
686 if ((err = snd_es1688_probe(chip)) < 0) {
687 snd_es1688_free(chip);
688 return err;
690 if ((err = snd_es1688_init(chip, 1)) < 0) {
691 snd_es1688_free(chip);
692 return err;
695 /* Register device */
696 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
697 snd_es1688_free(chip);
698 return err;
701 *rchip = chip;
702 return 0;
705 static struct snd_pcm_ops snd_es1688_playback_ops = {
706 .open = snd_es1688_playback_open,
707 .close = snd_es1688_playback_close,
708 .ioctl = snd_es1688_ioctl,
709 .hw_params = snd_es1688_hw_params,
710 .hw_free = snd_es1688_hw_free,
711 .prepare = snd_es1688_playback_prepare,
712 .trigger = snd_es1688_playback_trigger,
713 .pointer = snd_es1688_playback_pointer,
716 static struct snd_pcm_ops snd_es1688_capture_ops = {
717 .open = snd_es1688_capture_open,
718 .close = snd_es1688_capture_close,
719 .ioctl = snd_es1688_ioctl,
720 .hw_params = snd_es1688_hw_params,
721 .hw_free = snd_es1688_hw_free,
722 .prepare = snd_es1688_capture_prepare,
723 .trigger = snd_es1688_capture_trigger,
724 .pointer = snd_es1688_capture_pointer,
727 int snd_es1688_pcm(struct snd_es1688 * chip, int device, struct snd_pcm ** rpcm)
729 struct snd_pcm *pcm;
730 int err;
732 if ((err = snd_pcm_new(chip->card, "ESx688", device, 1, 1, &pcm)) < 0)
733 return err;
735 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
736 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
738 pcm->private_data = chip;
739 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
740 sprintf(pcm->name, snd_es1688_chip_id(chip));
741 chip->pcm = pcm;
743 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
744 snd_dma_isa_data(),
745 64*1024, 64*1024);
747 if (rpcm)
748 *rpcm = pcm;
749 return 0;
753 * MIXER part
756 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
758 static char *texts[9] = {
759 "Mic", "Mic Master", "CD", "AOUT",
760 "Mic1", "Mix", "Line", "Master"
763 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
764 uinfo->count = 1;
765 uinfo->value.enumerated.items = 8;
766 if (uinfo->value.enumerated.item > 7)
767 uinfo->value.enumerated.item = 7;
768 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
769 return 0;
772 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
774 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
775 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
776 return 0;
779 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
781 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
782 unsigned long flags;
783 unsigned char oval, nval;
784 int change;
786 if (ucontrol->value.enumerated.item[0] > 8)
787 return -EINVAL;
788 spin_lock_irqsave(&chip->reg_lock, flags);
789 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
790 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
791 change = nval != oval;
792 if (change)
793 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
794 spin_unlock_irqrestore(&chip->reg_lock, flags);
795 return change;
798 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
799 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
800 .info = snd_es1688_info_single, \
801 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
802 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
804 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
806 int mask = (kcontrol->private_value >> 16) & 0xff;
808 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
809 uinfo->count = 1;
810 uinfo->value.integer.min = 0;
811 uinfo->value.integer.max = mask;
812 return 0;
815 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
817 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
818 unsigned long flags;
819 int reg = kcontrol->private_value & 0xff;
820 int shift = (kcontrol->private_value >> 8) & 0xff;
821 int mask = (kcontrol->private_value >> 16) & 0xff;
822 int invert = (kcontrol->private_value >> 24) & 0xff;
824 spin_lock_irqsave(&chip->reg_lock, flags);
825 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
826 spin_unlock_irqrestore(&chip->reg_lock, flags);
827 if (invert)
828 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
829 return 0;
832 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
834 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
835 unsigned long flags;
836 int reg = kcontrol->private_value & 0xff;
837 int shift = (kcontrol->private_value >> 8) & 0xff;
838 int mask = (kcontrol->private_value >> 16) & 0xff;
839 int invert = (kcontrol->private_value >> 24) & 0xff;
840 int change;
841 unsigned char oval, nval;
843 nval = (ucontrol->value.integer.value[0] & mask);
844 if (invert)
845 nval = mask - nval;
846 nval <<= shift;
847 spin_lock_irqsave(&chip->reg_lock, flags);
848 oval = snd_es1688_mixer_read(chip, reg);
849 nval = (oval & ~(mask << shift)) | nval;
850 change = nval != oval;
851 if (change)
852 snd_es1688_mixer_write(chip, reg, nval);
853 spin_unlock_irqrestore(&chip->reg_lock, flags);
854 return change;
857 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
858 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
859 .info = snd_es1688_info_double, \
860 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
861 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
863 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
865 int mask = (kcontrol->private_value >> 24) & 0xff;
867 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
868 uinfo->count = 2;
869 uinfo->value.integer.min = 0;
870 uinfo->value.integer.max = mask;
871 return 0;
874 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
876 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
877 unsigned long flags;
878 int left_reg = kcontrol->private_value & 0xff;
879 int right_reg = (kcontrol->private_value >> 8) & 0xff;
880 int shift_left = (kcontrol->private_value >> 16) & 0x07;
881 int shift_right = (kcontrol->private_value >> 19) & 0x07;
882 int mask = (kcontrol->private_value >> 24) & 0xff;
883 int invert = (kcontrol->private_value >> 22) & 1;
884 unsigned char left, right;
886 spin_lock_irqsave(&chip->reg_lock, flags);
887 if (left_reg < 0xa0)
888 left = snd_es1688_mixer_read(chip, left_reg);
889 else
890 left = snd_es1688_read(chip, left_reg);
891 if (left_reg != right_reg) {
892 if (right_reg < 0xa0)
893 right = snd_es1688_mixer_read(chip, right_reg);
894 else
895 right = snd_es1688_read(chip, right_reg);
896 } else
897 right = left;
898 spin_unlock_irqrestore(&chip->reg_lock, flags);
899 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
900 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
901 if (invert) {
902 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
903 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
905 return 0;
908 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
910 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
911 unsigned long flags;
912 int left_reg = kcontrol->private_value & 0xff;
913 int right_reg = (kcontrol->private_value >> 8) & 0xff;
914 int shift_left = (kcontrol->private_value >> 16) & 0x07;
915 int shift_right = (kcontrol->private_value >> 19) & 0x07;
916 int mask = (kcontrol->private_value >> 24) & 0xff;
917 int invert = (kcontrol->private_value >> 22) & 1;
918 int change;
919 unsigned char val1, val2, oval1, oval2;
921 val1 = ucontrol->value.integer.value[0] & mask;
922 val2 = ucontrol->value.integer.value[1] & mask;
923 if (invert) {
924 val1 = mask - val1;
925 val2 = mask - val2;
927 val1 <<= shift_left;
928 val2 <<= shift_right;
929 spin_lock_irqsave(&chip->reg_lock, flags);
930 if (left_reg != right_reg) {
931 if (left_reg < 0xa0)
932 oval1 = snd_es1688_mixer_read(chip, left_reg);
933 else
934 oval1 = snd_es1688_read(chip, left_reg);
935 if (right_reg < 0xa0)
936 oval2 = snd_es1688_mixer_read(chip, right_reg);
937 else
938 oval2 = snd_es1688_read(chip, right_reg);
939 val1 = (oval1 & ~(mask << shift_left)) | val1;
940 val2 = (oval2 & ~(mask << shift_right)) | val2;
941 change = val1 != oval1 || val2 != oval2;
942 if (change) {
943 if (left_reg < 0xa0)
944 snd_es1688_mixer_write(chip, left_reg, val1);
945 else
946 snd_es1688_write(chip, left_reg, val1);
947 if (right_reg < 0xa0)
948 snd_es1688_mixer_write(chip, right_reg, val1);
949 else
950 snd_es1688_write(chip, right_reg, val1);
952 } else {
953 if (left_reg < 0xa0)
954 oval1 = snd_es1688_mixer_read(chip, left_reg);
955 else
956 oval1 = snd_es1688_read(chip, left_reg);
957 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
958 change = val1 != oval1;
959 if (change) {
960 if (left_reg < 0xa0)
961 snd_es1688_mixer_write(chip, left_reg, val1);
962 else
963 snd_es1688_write(chip, left_reg, val1);
967 spin_unlock_irqrestore(&chip->reg_lock, flags);
968 return change;
971 static struct snd_kcontrol_new snd_es1688_controls[] = {
972 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
973 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
974 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
975 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
976 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
977 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
978 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
979 ES1688_SINGLE("PC Speaker Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
980 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
981 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
983 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
984 .name = "Capture Source",
985 .info = snd_es1688_info_mux,
986 .get = snd_es1688_get_mux,
987 .put = snd_es1688_put_mux,
991 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
993 static unsigned char snd_es1688_init_table[][2] = {
994 { ES1688_MASTER_DEV, 0 },
995 { ES1688_PCM_DEV, 0 },
996 { ES1688_LINE_DEV, 0 },
997 { ES1688_CD_DEV, 0 },
998 { ES1688_FM_DEV, 0 },
999 { ES1688_MIC_DEV, 0 },
1000 { ES1688_AUX_DEV, 0 },
1001 { ES1688_SPEAKER_DEV, 0 },
1002 { ES1688_RECLEV_DEV, 0 },
1003 { ES1688_REC_DEV, 0x17 }
1006 int snd_es1688_mixer(struct snd_es1688 *chip)
1008 struct snd_card *card;
1009 unsigned int idx;
1010 int err;
1011 unsigned char reg, val;
1013 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1015 card = chip->card;
1017 strcpy(card->mixername, snd_es1688_chip_id(chip));
1019 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
1020 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1021 return err;
1023 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1024 reg = snd_es1688_init_table[idx][0];
1025 val = snd_es1688_init_table[idx][1];
1026 if (reg < 0xa0)
1027 snd_es1688_mixer_write(chip, reg, val);
1028 else
1029 snd_es1688_write(chip, reg, val);
1031 return 0;
1034 EXPORT_SYMBOL(snd_es1688_mixer_write);
1035 EXPORT_SYMBOL(snd_es1688_create);
1036 EXPORT_SYMBOL(snd_es1688_pcm);
1037 EXPORT_SYMBOL(snd_es1688_mixer);
1040 * INIT part
1043 static int __init alsa_es1688_init(void)
1045 return 0;
1048 static void __exit alsa_es1688_exit(void)
1052 module_init(alsa_es1688_init)
1053 module_exit(alsa_es1688_exit)