1 /* 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 3 * Routines for control of ESS ES1688/688/488 chip 4 * 5 * 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. 10 * 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. 15 * 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 19 * 20 */ 21 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> 30 31 #include <asm/io.h> 32 #include <asm/dma.h> 33 34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 35 MODULE_DESCRIPTION("ESS ESx688 lowlevel module"); 36 MODULE_LICENSE("GPL"); 37 38 static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val) 39 { 40 int i; 41 42 for (i = 10000; i; i--) 43 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) { 44 outb(val, ES1688P(chip, COMMAND)); 45 return 1; 46 } 47 #ifdef CONFIG_SND_DEBUG 48 printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val); 49 #endif 50 return 0; 51 } 52 53 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip) 54 { 55 int i; 56 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))); 61 return -ENODEV; 62 } 63 64 static int snd_es1688_write(struct snd_es1688 *chip, 65 unsigned char reg, unsigned char data) 66 { 67 if (!snd_es1688_dsp_command(chip, reg)) 68 return 0; 69 return snd_es1688_dsp_command(chip, data); 70 } 71 72 static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg) 73 { 74 /* Read a byte from an extended mode register of ES1688 */ 75 if (!snd_es1688_dsp_command(chip, 0xc0)) 76 return -1; 77 if (!snd_es1688_dsp_command(chip, reg)) 78 return -1; 79 return snd_es1688_dsp_get_byte(chip); 80 } 81 82 void snd_es1688_mixer_write(struct snd_es1688 *chip, 83 unsigned char reg, unsigned char data) 84 { 85 outb(reg, ES1688P(chip, MIXER_ADDR)); 86 udelay(10); 87 outb(data, ES1688P(chip, MIXER_DATA)); 88 udelay(10); 89 } 90 91 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg) 92 { 93 unsigned char result; 94 95 outb(reg, ES1688P(chip, MIXER_ADDR)); 96 udelay(10); 97 result = inb(ES1688P(chip, MIXER_DATA)); 98 udelay(10); 99 return result; 100 } 101 102 int snd_es1688_reset(struct snd_es1688 *chip) 103 { 104 int i; 105 106 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */ 107 udelay(10); 108 outb(0, ES1688P(chip, RESET)); 109 udelay(30); 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); 113 return -ENODEV; 114 } 115 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */ 116 return 0; 117 } 118 EXPORT_SYMBOL(snd_es1688_reset); 119 120 static int snd_es1688_probe(struct snd_es1688 *chip) 121 { 122 unsigned long flags; 123 unsigned short major, minor, hw; 124 int i; 125 126 /* 127 * initialization sequence 128 */ 129 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 */ 142 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; 147 } 148 snd_es1688_dsp_command(chip, 0xe7); /* return identification */ 149 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)); 156 } 157 } 158 } 159 160 spin_unlock_irqrestore(&chip->reg_lock, flags); 161 162 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor); 163 164 chip->version = (major << 8) | minor; 165 if (!chip->version) 166 return -ENODEV; /* probably SB */ 167 168 hw = ES1688_HW_AUTO; 169 switch (chip->version & 0xfff0) { 170 case 0x4880: 171 snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, " 172 "but driver is in another place\n", chip->port); 173 return -ENODEV; 174 case 0x6880: 175 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688; 176 break; 177 default: 178 snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip " 179 "with version 0x%x (Jazz16 soundcard?)\n", 180 chip->port, chip->version); 181 return -ENODEV; 182 } 183 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); 188 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); 193 194 return 0; 195 } 196 197 static int snd_es1688_init(struct snd_es1688 * chip, int enable) 198 { 199 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1}; 200 unsigned long flags; 201 int cfg, irq_bits, dma, dma_bits, tmp, tmp1; 202 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; 207 if (tmp <= 3) { 208 switch (chip->mpu_irq) { 209 case 9: 210 tmp1 = 4; 211 break; 212 case 5: 213 tmp1 = 5; 214 break; 215 case 7: 216 tmp1 = 6; 217 break; 218 case 10: 219 tmp1 = 7; 220 break; 221 default: 222 tmp1 = 0; 223 } 224 if (tmp1) { 225 cfg |= (tmp << 3) | (tmp1 << 5); 226 } 227 } 228 } 229 #if 0 230 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg); 231 #endif 232 spin_lock_irqsave(&chip->reg_lock, flags); 233 snd_es1688_mixer_write(chip, 0x40, cfg); 234 spin_unlock_irqrestore(&chip->reg_lock, flags); 235 /* --- */ 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); 240 if (enable) { 241 cfg = 0xf0; /* enable only DMA counter interrupt */ 242 irq_bits = irqs[chip->irq & 0x0f]; 243 if (irq_bits < 0) { 244 snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d " 245 "for ES1688 chip!!\n", 246 chip->port, chip->irq); 247 #if 0 248 irq_bits = 0; 249 cfg = 0x10; 250 #endif 251 return -EINVAL; 252 } 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 */ 257 dma = chip->dma8; 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); 261 #if 0 262 dma_bits = 0; 263 cfg = 0x00; /* disable all DMA */ 264 #endif 265 return -EINVAL; 266 } else { 267 dma_bits = dma; 268 if (dma != 3) 269 dma_bits++; 270 } 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); 274 } else { 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); 279 } 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); 285 return 0; 286 } 287 288 /* 289 290 */ 291 292 static struct snd_ratnum clocks[2] = { 293 { 294 .num = 795444, 295 .den_min = 1, 296 .den_max = 128, 297 .den_step = 1, 298 }, 299 { 300 .num = 397722, 301 .den_min = 1, 302 .den_max = 128, 303 .den_step = 1, 304 } 305 }; 306 307 static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = { 308 .nrats = 2, 309 .rats = clocks, 310 }; 311 312 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream) 313 { 314 struct snd_pcm_runtime *runtime = substream->runtime; 315 unsigned int bits, divider; 316 317 if (runtime->rate_num == clocks[0].num) 318 bits = 256 - runtime->rate_den; 319 else 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); 326 } 327 328 static int snd_es1688_ioctl(struct snd_pcm_substream *substream, 329 unsigned int cmd, void *arg) 330 { 331 return snd_pcm_lib_ioctl(substream, cmd, arg); 332 } 333 334 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value) 335 { 336 int val; 337 338 if (cmd == SNDRV_PCM_TRIGGER_STOP) { 339 value = 0x00; 340 } else if (cmd != SNDRV_PCM_TRIGGER_START) { 341 return -EINVAL; 342 } 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 */ 349 } 350 #if 0 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)); 354 #endif 355 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value); 356 spin_unlock(&chip->reg_lock); 357 return 0; 358 } 359 360 static int snd_es1688_hw_params(struct snd_pcm_substream *substream, 361 struct snd_pcm_hw_params *hw_params) 362 { 363 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); 364 } 365 366 static int snd_es1688_hw_free(struct snd_pcm_substream *substream) 367 { 368 return snd_pcm_lib_free_pages(substream); 369 } 370 371 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream) 372 { 373 unsigned long flags; 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); 378 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) { 388 /* 8. bit mono */ 389 snd_es1688_write(chip, 0xb6, 0x80); 390 snd_es1688_write(chip, 0xb7, 0x51); 391 snd_es1688_write(chip, 0xb7, 0xd0); 392 } else { 393 /* 16. bit mono */ 394 snd_es1688_write(chip, 0xb6, 0x00); 395 snd_es1688_write(chip, 0xb7, 0x71); 396 snd_es1688_write(chip, 0xb7, 0xf4); 397 } 398 } else { 399 if (snd_pcm_format_width(runtime->format) == 8) { 400 /* 8. bit stereo */ 401 snd_es1688_write(chip, 0xb6, 0x80); 402 snd_es1688_write(chip, 0xb7, 0x51); 403 snd_es1688_write(chip, 0xb7, 0x98); 404 } else { 405 /* 16. bit stereo */ 406 snd_es1688_write(chip, 0xb6, 0x00); 407 snd_es1688_write(chip, 0xb7, 0x71); 408 snd_es1688_write(chip, 0xb7, 0xbc); 409 } 410 } 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); 415 /* --- */ 416 count = -count; 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); 422 return 0; 423 } 424 425 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream, 426 int cmd) 427 { 428 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 429 return snd_es1688_trigger(chip, cmd, 0x05); 430 } 431 432 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream) 433 { 434 unsigned long flags; 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); 439 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) { 450 /* 8. bit mono */ 451 snd_es1688_write(chip, 0xb7, 0x51); 452 snd_es1688_write(chip, 0xb7, 0xd0); 453 } else { 454 /* 16. bit mono */ 455 snd_es1688_write(chip, 0xb7, 0x71); 456 snd_es1688_write(chip, 0xb7, 0xf4); 457 } 458 } else { 459 if (snd_pcm_format_width(runtime->format) == 8) { 460 /* 8. bit stereo */ 461 snd_es1688_write(chip, 0xb7, 0x51); 462 snd_es1688_write(chip, 0xb7, 0x98); 463 } else { 464 /* 16. bit stereo */ 465 snd_es1688_write(chip, 0xb7, 0x71); 466 snd_es1688_write(chip, 0xb7, 0xbc); 467 } 468 } 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); 472 /* --- */ 473 count = -count; 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); 479 return 0; 480 } 481 482 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream, 483 int cmd) 484 { 485 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 486 return snd_es1688_trigger(chip, cmd, 0x0f); 487 } 488 489 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id) 490 { 491 struct snd_es1688 *chip = dev_id; 492 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); 497 498 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */ 499 return IRQ_HANDLED; 500 } 501 502 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream) 503 { 504 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 505 size_t ptr; 506 507 if (chip->trigger_value != 0x05) 508 return 0; 509 ptr = snd_dma_pointer(chip->dma8, chip->dma_size); 510 return bytes_to_frames(substream->runtime, ptr); 511 } 512 513 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream) 514 { 515 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 516 size_t ptr; 517 518 if (chip->trigger_value != 0x0f) 519 return 0; 520 ptr = snd_dma_pointer(chip->dma8, chip->dma_size); 521 return bytes_to_frames(substream->runtime, ptr); 522 } 523 524 /* 525 526 */ 527 528 static struct snd_pcm_hardware snd_es1688_playback = 529 { 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, 534 .rate_min = 4000, 535 .rate_max = 48000, 536 .channels_min = 1, 537 .channels_max = 2, 538 .buffer_bytes_max = 65536, 539 .period_bytes_min = 64, 540 .period_bytes_max = 65536, 541 .periods_min = 1, 542 .periods_max = 1024, 543 .fifo_size = 0, 544 }; 545 546 static struct snd_pcm_hardware snd_es1688_capture = 547 { 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, 552 .rate_min = 4000, 553 .rate_max = 48000, 554 .channels_min = 1, 555 .channels_max = 2, 556 .buffer_bytes_max = 65536, 557 .period_bytes_min = 64, 558 .period_bytes_max = 65536, 559 .periods_min = 1, 560 .periods_max = 1024, 561 .fifo_size = 0, 562 }; 563 564 /* 565 566 */ 567 568 static int snd_es1688_playback_open(struct snd_pcm_substream *substream) 569 { 570 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 571 struct snd_pcm_runtime *runtime = substream->runtime; 572 573 if (chip->capture_substream != NULL) 574 return -EAGAIN; 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); 579 return 0; 580 } 581 582 static int snd_es1688_capture_open(struct snd_pcm_substream *substream) 583 { 584 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 585 struct snd_pcm_runtime *runtime = substream->runtime; 586 587 if (chip->playback_substream != NULL) 588 return -EAGAIN; 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); 593 return 0; 594 } 595 596 static int snd_es1688_playback_close(struct snd_pcm_substream *substream) 597 { 598 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 599 600 chip->playback_substream = NULL; 601 return 0; 602 } 603 604 static int snd_es1688_capture_close(struct snd_pcm_substream *substream) 605 { 606 struct snd_es1688 *chip = snd_pcm_substream_chip(substream); 607 608 chip->capture_substream = NULL; 609 return 0; 610 } 611 612 static int snd_es1688_free(struct snd_es1688 *chip) 613 { 614 if (chip->res_port) { 615 snd_es1688_init(chip, 0); 616 release_and_free_resource(chip->res_port); 617 } 618 if (chip->irq >= 0) 619 free_irq(chip->irq, (void *) chip); 620 if (chip->dma8 >= 0) { 621 disable_dma(chip->dma8); 622 free_dma(chip->dma8); 623 } 624 return 0; 625 } 626 627 static int snd_es1688_dev_free(struct snd_device *device) 628 { 629 struct snd_es1688 *chip = device->device_data; 630 return snd_es1688_free(chip); 631 } 632 633 static const char *snd_es1688_chip_id(struct snd_es1688 *chip) 634 { 635 static char tmp[16]; 636 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f); 637 return tmp; 638 } 639 640 int snd_es1688_create(struct snd_card *card, 641 struct snd_es1688 *chip, 642 unsigned long port, 643 unsigned long mpu_port, 644 int irq, 645 int mpu_irq, 646 int dma8, 647 unsigned short hardware) 648 { 649 static struct snd_device_ops ops = { 650 .dev_free = snd_es1688_dev_free, 651 }; 652 653 int err; 654 655 if (chip == NULL) 656 return -ENOMEM; 657 chip->irq = -1; 658 chip->dma8 = -1; 659 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); 662 return -EBUSY; 663 } 664 if (request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip)) { 665 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq); 666 return -EBUSY; 667 } 668 chip->irq = irq; 669 if (request_dma(dma8, "ES1688")) { 670 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8); 671 return -EBUSY; 672 } 673 chip->dma8 = dma8; 674 675 spin_lock_init(&chip->reg_lock); 676 spin_lock_init(&chip->mixer_lock); 677 chip->port = port; 678 mpu_port &= ~0x000f; 679 if (mpu_port < 0x300 || mpu_port > 0x330) 680 mpu_port = 0; 681 chip->mpu_port = mpu_port; 682 chip->mpu_irq = mpu_irq; 683 chip->hardware = hardware; 684 685 err = snd_es1688_probe(chip); 686 if (err < 0) 687 return err; 688 689 err = snd_es1688_init(chip, 1); 690 if (err < 0) 691 return err; 692 693 /* Register device */ 694 return snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 695 } 696 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, 706 }; 707 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, 717 }; 718 719 int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, 720 int device, struct snd_pcm **rpcm) 721 { 722 struct snd_pcm *pcm; 723 int err; 724 725 err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm); 726 if (err < 0) 727 return err; 728 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); 731 732 pcm->private_data = chip; 733 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; 734 sprintf(pcm->name, snd_es1688_chip_id(chip)); 735 chip->pcm = pcm; 736 737 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 738 snd_dma_isa_data(), 739 64*1024, 64*1024); 740 741 if (rpcm) 742 *rpcm = pcm; 743 return 0; 744 } 745 746 /* 747 * MIXER part 748 */ 749 750 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 751 { 752 static char *texts[9] = { 753 "Mic", "Mic Master", "CD", "AOUT", 754 "Mic1", "Mix", "Line", "Master" 755 }; 756 757 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 758 uinfo->count = 1; 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]); 763 return 0; 764 } 765 766 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 767 { 768 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 769 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7; 770 return 0; 771 } 772 773 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 774 { 775 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 776 unsigned long flags; 777 unsigned char oval, nval; 778 int change; 779 780 if (ucontrol->value.enumerated.item[0] > 8) 781 return -EINVAL; 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; 786 if (change) 787 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval); 788 spin_unlock_irqrestore(&chip->reg_lock, flags); 789 return change; 790 } 791 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) } 797 798 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 799 { 800 int mask = (kcontrol->private_value >> 16) & 0xff; 801 802 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; 803 uinfo->count = 1; 804 uinfo->value.integer.min = 0; 805 uinfo->value.integer.max = mask; 806 return 0; 807 } 808 809 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 810 { 811 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 812 unsigned long flags; 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; 817 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); 821 if (invert) 822 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; 823 return 0; 824 } 825 826 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 827 { 828 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 829 unsigned long flags; 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; 834 int change; 835 unsigned char oval, nval; 836 837 nval = (ucontrol->value.integer.value[0] & mask); 838 if (invert) 839 nval = mask - nval; 840 nval <<= shift; 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; 845 if (change) 846 snd_es1688_mixer_write(chip, reg, nval); 847 spin_unlock_irqrestore(&chip->reg_lock, flags); 848 return change; 849 } 850 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) } 856 857 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 858 { 859 int mask = (kcontrol->private_value >> 24) & 0xff; 860 861 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; 862 uinfo->count = 2; 863 uinfo->value.integer.min = 0; 864 uinfo->value.integer.max = mask; 865 return 0; 866 } 867 868 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 869 { 870 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 871 unsigned long flags; 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; 879 880 spin_lock_irqsave(&chip->reg_lock, flags); 881 if (left_reg < 0xa0) 882 left = snd_es1688_mixer_read(chip, left_reg); 883 else 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); 888 else 889 right = snd_es1688_read(chip, right_reg); 890 } else 891 right = left; 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; 895 if (invert) { 896 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; 897 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; 898 } 899 return 0; 900 } 901 902 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 903 { 904 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); 905 unsigned long flags; 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; 912 int change; 913 unsigned char val1, val2, oval1, oval2; 914 915 val1 = ucontrol->value.integer.value[0] & mask; 916 val2 = ucontrol->value.integer.value[1] & mask; 917 if (invert) { 918 val1 = mask - val1; 919 val2 = mask - val2; 920 } 921 val1 <<= shift_left; 922 val2 <<= shift_right; 923 spin_lock_irqsave(&chip->reg_lock, flags); 924 if (left_reg != right_reg) { 925 if (left_reg < 0xa0) 926 oval1 = snd_es1688_mixer_read(chip, left_reg); 927 else 928 oval1 = snd_es1688_read(chip, left_reg); 929 if (right_reg < 0xa0) 930 oval2 = snd_es1688_mixer_read(chip, right_reg); 931 else 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; 936 if (change) { 937 if (left_reg < 0xa0) 938 snd_es1688_mixer_write(chip, left_reg, val1); 939 else 940 snd_es1688_write(chip, left_reg, val1); 941 if (right_reg < 0xa0) 942 snd_es1688_mixer_write(chip, right_reg, val1); 943 else 944 snd_es1688_write(chip, right_reg, val1); 945 } 946 } else { 947 if (left_reg < 0xa0) 948 oval1 = snd_es1688_mixer_read(chip, left_reg); 949 else 950 oval1 = snd_es1688_read(chip, left_reg); 951 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; 952 change = val1 != oval1; 953 if (change) { 954 if (left_reg < 0xa0) 955 snd_es1688_mixer_write(chip, left_reg, val1); 956 else 957 snd_es1688_write(chip, left_reg, val1); 958 } 959 960 } 961 spin_unlock_irqrestore(&chip->reg_lock, flags); 962 return change; 963 } 964 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), 976 { 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, 982 }, 983 }; 984 985 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2) 986 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 } 998 }; 999 1000 int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip) 1001 { 1002 unsigned int idx; 1003 int err; 1004 unsigned char reg, val; 1005 1006 if (snd_BUG_ON(!chip || !card)) 1007 return -EINVAL; 1008 1009 strcpy(card->mixername, snd_es1688_chip_id(chip)); 1010 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) 1013 return err; 1014 } 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]; 1018 if (reg < 0xa0) 1019 snd_es1688_mixer_write(chip, reg, val); 1020 else 1021 snd_es1688_write(chip, reg, val); 1022 } 1023 return 0; 1024 } 1025 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); 1030 1031 /* 1032 * INIT part 1033 */ 1034 1035 static int __init alsa_es1688_init(void) 1036 { 1037 return 0; 1038 } 1039 1040 static void __exit alsa_es1688_exit(void) 1041 { 1042 } 1043 1044 module_init(alsa_es1688_init) 1045 module_exit(alsa_es1688_exit) 1046