0001
0002
0003
0004
0005
0006
0007 #include <linux/init.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/delay.h>
0010 #include <linux/slab.h>
0011 #include <linux/ioport.h>
0012 #include <linux/module.h>
0013 #include <linux/io.h>
0014 #include <sound/core.h>
0015 #include <sound/es1688.h>
0016 #include <sound/initval.h>
0017
0018 #include <asm/dma.h>
0019
0020 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0021 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
0022 MODULE_LICENSE("GPL");
0023
0024 static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
0025 {
0026 int i;
0027
0028 for (i = 10000; i; i--)
0029 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
0030 outb(val, ES1688P(chip, COMMAND));
0031 return 1;
0032 }
0033 #ifdef CONFIG_SND_DEBUG
0034 printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
0035 #endif
0036 return 0;
0037 }
0038
0039 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
0040 {
0041 int i;
0042
0043 for (i = 1000; i; i--)
0044 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
0045 return inb(ES1688P(chip, READ));
0046 snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
0047 return -ENODEV;
0048 }
0049
0050 static int snd_es1688_write(struct snd_es1688 *chip,
0051 unsigned char reg, unsigned char data)
0052 {
0053 if (!snd_es1688_dsp_command(chip, reg))
0054 return 0;
0055 return snd_es1688_dsp_command(chip, data);
0056 }
0057
0058 static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
0059 {
0060
0061 if (!snd_es1688_dsp_command(chip, 0xc0))
0062 return -1;
0063 if (!snd_es1688_dsp_command(chip, reg))
0064 return -1;
0065 return snd_es1688_dsp_get_byte(chip);
0066 }
0067
0068 void snd_es1688_mixer_write(struct snd_es1688 *chip,
0069 unsigned char reg, unsigned char data)
0070 {
0071 outb(reg, ES1688P(chip, MIXER_ADDR));
0072 udelay(10);
0073 outb(data, ES1688P(chip, MIXER_DATA));
0074 udelay(10);
0075 }
0076
0077 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
0078 {
0079 unsigned char result;
0080
0081 outb(reg, ES1688P(chip, MIXER_ADDR));
0082 udelay(10);
0083 result = inb(ES1688P(chip, MIXER_DATA));
0084 udelay(10);
0085 return result;
0086 }
0087
0088 int snd_es1688_reset(struct snd_es1688 *chip)
0089 {
0090 int i;
0091
0092 outb(3, ES1688P(chip, RESET));
0093 udelay(10);
0094 outb(0, ES1688P(chip, RESET));
0095 udelay(30);
0096 for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
0097 if (inb(ES1688P(chip, READ)) != 0xaa) {
0098 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
0099 return -ENODEV;
0100 }
0101 snd_es1688_dsp_command(chip, 0xc6);
0102 return 0;
0103 }
0104 EXPORT_SYMBOL(snd_es1688_reset);
0105
0106 static int snd_es1688_probe(struct snd_es1688 *chip)
0107 {
0108 unsigned long flags;
0109 unsigned short major, minor;
0110 int i;
0111
0112
0113
0114
0115
0116 spin_lock_irqsave(&chip->reg_lock, flags);
0117 inb(ES1688P(chip, ENABLE1));
0118 inb(ES1688P(chip, ENABLE1));
0119 inb(ES1688P(chip, ENABLE1));
0120 inb(ES1688P(chip, ENABLE2));
0121 inb(ES1688P(chip, ENABLE1));
0122 inb(ES1688P(chip, ENABLE2));
0123 inb(ES1688P(chip, ENABLE1));
0124 inb(ES1688P(chip, ENABLE1));
0125 inb(ES1688P(chip, ENABLE2));
0126 inb(ES1688P(chip, ENABLE1));
0127 inb(ES1688P(chip, ENABLE0));
0128
0129 if (snd_es1688_reset(chip) < 0) {
0130 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
0131 spin_unlock_irqrestore(&chip->reg_lock, flags);
0132 return -ENODEV;
0133 }
0134 snd_es1688_dsp_command(chip, 0xe7);
0135
0136 for (i = 1000, major = minor = 0; i; i--) {
0137 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
0138 if (major == 0) {
0139 major = inb(ES1688P(chip, READ));
0140 } else {
0141 minor = inb(ES1688P(chip, READ));
0142 }
0143 }
0144 }
0145
0146 spin_unlock_irqrestore(&chip->reg_lock, flags);
0147
0148 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
0149
0150 chip->version = (major << 8) | minor;
0151 if (!chip->version)
0152 return -ENODEV;
0153
0154 switch (chip->version & 0xfff0) {
0155 case 0x4880:
0156 snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
0157 "but driver is in another place\n", chip->port);
0158 return -ENODEV;
0159 case 0x6880:
0160 break;
0161 default:
0162 snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
0163 "with version 0x%x (Jazz16 soundcard?)\n",
0164 chip->port, chip->version);
0165 return -ENODEV;
0166 }
0167
0168 spin_lock_irqsave(&chip->reg_lock, flags);
0169 snd_es1688_write(chip, 0xb1, 0x10);
0170 snd_es1688_write(chip, 0xb2, 0x00);
0171 spin_unlock_irqrestore(&chip->reg_lock, flags);
0172
0173
0174 spin_lock_irqsave(&chip->mixer_lock, flags);
0175 snd_es1688_mixer_write(chip, 0x40, 0x01);
0176 spin_unlock_irqrestore(&chip->mixer_lock, flags);
0177
0178 return 0;
0179 }
0180
0181 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
0182 {
0183 static const int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
0184 unsigned long flags;
0185 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
0186
0187
0188 cfg = 0x01;
0189 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
0190 tmp = (chip->mpu_port & 0x0f0) >> 4;
0191 if (tmp <= 3) {
0192 switch (chip->mpu_irq) {
0193 case 9:
0194 tmp1 = 4;
0195 break;
0196 case 5:
0197 tmp1 = 5;
0198 break;
0199 case 7:
0200 tmp1 = 6;
0201 break;
0202 case 10:
0203 tmp1 = 7;
0204 break;
0205 default:
0206 tmp1 = 0;
0207 }
0208 if (tmp1) {
0209 cfg |= (tmp << 3) | (tmp1 << 5);
0210 }
0211 }
0212 }
0213 #if 0
0214 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
0215 #endif
0216 spin_lock_irqsave(&chip->reg_lock, flags);
0217 snd_es1688_mixer_write(chip, 0x40, cfg);
0218 spin_unlock_irqrestore(&chip->reg_lock, flags);
0219
0220 spin_lock_irqsave(&chip->reg_lock, flags);
0221 snd_es1688_read(chip, 0xb1);
0222 snd_es1688_read(chip, 0xb2);
0223 spin_unlock_irqrestore(&chip->reg_lock, flags);
0224 if (enable) {
0225 cfg = 0xf0;
0226 irq_bits = irqs[chip->irq & 0x0f];
0227 if (irq_bits < 0) {
0228 snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
0229 "for ES1688 chip!!\n",
0230 chip->port, chip->irq);
0231 #if 0
0232 irq_bits = 0;
0233 cfg = 0x10;
0234 #endif
0235 return -EINVAL;
0236 }
0237 spin_lock_irqsave(&chip->reg_lock, flags);
0238 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
0239 spin_unlock_irqrestore(&chip->reg_lock, flags);
0240 cfg = 0xf0;
0241 dma = chip->dma8;
0242 if (dma > 3 || dma == 2) {
0243 snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
0244 "for ES1688 chip!!\n", chip->port, dma);
0245 #if 0
0246 dma_bits = 0;
0247 cfg = 0x00;
0248 #endif
0249 return -EINVAL;
0250 } else {
0251 dma_bits = dma;
0252 if (dma != 3)
0253 dma_bits++;
0254 }
0255 spin_lock_irqsave(&chip->reg_lock, flags);
0256 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
0257 spin_unlock_irqrestore(&chip->reg_lock, flags);
0258 } else {
0259 spin_lock_irqsave(&chip->reg_lock, flags);
0260 snd_es1688_write(chip, 0xb1, 0x10);
0261 snd_es1688_write(chip, 0xb2, 0x00);
0262 spin_unlock_irqrestore(&chip->reg_lock, flags);
0263 }
0264 spin_lock_irqsave(&chip->reg_lock, flags);
0265 snd_es1688_read(chip, 0xb1);
0266 snd_es1688_read(chip, 0xb2);
0267 snd_es1688_reset(chip);
0268 spin_unlock_irqrestore(&chip->reg_lock, flags);
0269 return 0;
0270 }
0271
0272
0273
0274
0275
0276 static const struct snd_ratnum clocks[2] = {
0277 {
0278 .num = 795444,
0279 .den_min = 1,
0280 .den_max = 128,
0281 .den_step = 1,
0282 },
0283 {
0284 .num = 397722,
0285 .den_min = 1,
0286 .den_max = 128,
0287 .den_step = 1,
0288 }
0289 };
0290
0291 static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
0292 .nrats = 2,
0293 .rats = clocks,
0294 };
0295
0296 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
0297 {
0298 struct snd_pcm_runtime *runtime = substream->runtime;
0299 unsigned int bits, divider;
0300
0301 if (runtime->rate_num == clocks[0].num)
0302 bits = 256 - runtime->rate_den;
0303 else
0304 bits = 128 - runtime->rate_den;
0305
0306 divider = 256 - 7160000*20/(8*82*runtime->rate);
0307
0308 snd_es1688_write(chip, 0xa1, bits);
0309 snd_es1688_write(chip, 0xa2, divider);
0310 }
0311
0312 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
0313 {
0314 int val;
0315
0316 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
0317 value = 0x00;
0318 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
0319 return -EINVAL;
0320 }
0321 spin_lock(&chip->reg_lock);
0322 chip->trigger_value = value;
0323 val = snd_es1688_read(chip, 0xb8);
0324 if ((val < 0) || (val & 0x0f) == value) {
0325 spin_unlock(&chip->reg_lock);
0326 return -EINVAL;
0327 }
0328 #if 0
0329 printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
0330 printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
0331 snd_dma_pointer(chip->dma8, chip->dma_size));
0332 #endif
0333 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
0334 spin_unlock(&chip->reg_lock);
0335 return 0;
0336 }
0337
0338 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
0339 {
0340 unsigned long flags;
0341 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0342 struct snd_pcm_runtime *runtime = substream->runtime;
0343 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
0344 unsigned int count = snd_pcm_lib_period_bytes(substream);
0345
0346 chip->dma_size = size;
0347 spin_lock_irqsave(&chip->reg_lock, flags);
0348 snd_es1688_reset(chip);
0349 snd_es1688_set_rate(chip, substream);
0350 snd_es1688_write(chip, 0xb8, 4);
0351 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
0352 snd_es1688_write(chip, 0xb9, 2);
0353 if (runtime->channels == 1) {
0354 if (snd_pcm_format_width(runtime->format) == 8) {
0355
0356 snd_es1688_write(chip, 0xb6, 0x80);
0357 snd_es1688_write(chip, 0xb7, 0x51);
0358 snd_es1688_write(chip, 0xb7, 0xd0);
0359 } else {
0360
0361 snd_es1688_write(chip, 0xb6, 0x00);
0362 snd_es1688_write(chip, 0xb7, 0x71);
0363 snd_es1688_write(chip, 0xb7, 0xf4);
0364 }
0365 } else {
0366 if (snd_pcm_format_width(runtime->format) == 8) {
0367
0368 snd_es1688_write(chip, 0xb6, 0x80);
0369 snd_es1688_write(chip, 0xb7, 0x51);
0370 snd_es1688_write(chip, 0xb7, 0x98);
0371 } else {
0372
0373 snd_es1688_write(chip, 0xb6, 0x00);
0374 snd_es1688_write(chip, 0xb7, 0x71);
0375 snd_es1688_write(chip, 0xb7, 0xbc);
0376 }
0377 }
0378 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
0379 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
0380 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
0381 spin_unlock_irqrestore(&chip->reg_lock, flags);
0382
0383 count = -count;
0384 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
0385 spin_lock_irqsave(&chip->reg_lock, flags);
0386 snd_es1688_write(chip, 0xa4, (unsigned char) count);
0387 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
0388 spin_unlock_irqrestore(&chip->reg_lock, flags);
0389 return 0;
0390 }
0391
0392 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
0393 int cmd)
0394 {
0395 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0396 return snd_es1688_trigger(chip, cmd, 0x05);
0397 }
0398
0399 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
0400 {
0401 unsigned long flags;
0402 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0403 struct snd_pcm_runtime *runtime = substream->runtime;
0404 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
0405 unsigned int count = snd_pcm_lib_period_bytes(substream);
0406
0407 chip->dma_size = size;
0408 spin_lock_irqsave(&chip->reg_lock, flags);
0409 snd_es1688_reset(chip);
0410 snd_es1688_set_rate(chip, substream);
0411 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
0412 snd_es1688_write(chip, 0xb8, 0x0e);
0413 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
0414 snd_es1688_write(chip, 0xb9, 2);
0415 if (runtime->channels == 1) {
0416 if (snd_pcm_format_width(runtime->format) == 8) {
0417
0418 snd_es1688_write(chip, 0xb7, 0x51);
0419 snd_es1688_write(chip, 0xb7, 0xd0);
0420 } else {
0421
0422 snd_es1688_write(chip, 0xb7, 0x71);
0423 snd_es1688_write(chip, 0xb7, 0xf4);
0424 }
0425 } else {
0426 if (snd_pcm_format_width(runtime->format) == 8) {
0427
0428 snd_es1688_write(chip, 0xb7, 0x51);
0429 snd_es1688_write(chip, 0xb7, 0x98);
0430 } else {
0431
0432 snd_es1688_write(chip, 0xb7, 0x71);
0433 snd_es1688_write(chip, 0xb7, 0xbc);
0434 }
0435 }
0436 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
0437 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
0438 spin_unlock_irqrestore(&chip->reg_lock, flags);
0439
0440 count = -count;
0441 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
0442 spin_lock_irqsave(&chip->reg_lock, flags);
0443 snd_es1688_write(chip, 0xa4, (unsigned char) count);
0444 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
0445 spin_unlock_irqrestore(&chip->reg_lock, flags);
0446 return 0;
0447 }
0448
0449 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
0450 int cmd)
0451 {
0452 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0453 return snd_es1688_trigger(chip, cmd, 0x0f);
0454 }
0455
0456 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
0457 {
0458 struct snd_es1688 *chip = dev_id;
0459
0460 if (chip->trigger_value == 0x05)
0461 snd_pcm_period_elapsed(chip->playback_substream);
0462 if (chip->trigger_value == 0x0f)
0463 snd_pcm_period_elapsed(chip->capture_substream);
0464
0465 inb(ES1688P(chip, DATA_AVAIL));
0466 return IRQ_HANDLED;
0467 }
0468
0469 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
0470 {
0471 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0472 size_t ptr;
0473
0474 if (chip->trigger_value != 0x05)
0475 return 0;
0476 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
0477 return bytes_to_frames(substream->runtime, ptr);
0478 }
0479
0480 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
0481 {
0482 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0483 size_t ptr;
0484
0485 if (chip->trigger_value != 0x0f)
0486 return 0;
0487 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
0488 return bytes_to_frames(substream->runtime, ptr);
0489 }
0490
0491
0492
0493
0494
0495 static const struct snd_pcm_hardware snd_es1688_playback =
0496 {
0497 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
0498 SNDRV_PCM_INFO_MMAP_VALID),
0499 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
0500 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
0501 .rate_min = 4000,
0502 .rate_max = 48000,
0503 .channels_min = 1,
0504 .channels_max = 2,
0505 .buffer_bytes_max = 65536,
0506 .period_bytes_min = 64,
0507 .period_bytes_max = 65536,
0508 .periods_min = 1,
0509 .periods_max = 1024,
0510 .fifo_size = 0,
0511 };
0512
0513 static const struct snd_pcm_hardware snd_es1688_capture =
0514 {
0515 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
0516 SNDRV_PCM_INFO_MMAP_VALID),
0517 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
0518 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
0519 .rate_min = 4000,
0520 .rate_max = 48000,
0521 .channels_min = 1,
0522 .channels_max = 2,
0523 .buffer_bytes_max = 65536,
0524 .period_bytes_min = 64,
0525 .period_bytes_max = 65536,
0526 .periods_min = 1,
0527 .periods_max = 1024,
0528 .fifo_size = 0,
0529 };
0530
0531
0532
0533
0534
0535 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
0536 {
0537 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0538 struct snd_pcm_runtime *runtime = substream->runtime;
0539
0540 if (chip->capture_substream != NULL)
0541 return -EAGAIN;
0542 chip->playback_substream = substream;
0543 runtime->hw = snd_es1688_playback;
0544 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0545 &hw_constraints_clocks);
0546 return 0;
0547 }
0548
0549 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
0550 {
0551 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0552 struct snd_pcm_runtime *runtime = substream->runtime;
0553
0554 if (chip->playback_substream != NULL)
0555 return -EAGAIN;
0556 chip->capture_substream = substream;
0557 runtime->hw = snd_es1688_capture;
0558 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0559 &hw_constraints_clocks);
0560 return 0;
0561 }
0562
0563 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
0564 {
0565 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0566
0567 chip->playback_substream = NULL;
0568 return 0;
0569 }
0570
0571 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
0572 {
0573 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
0574
0575 chip->capture_substream = NULL;
0576 return 0;
0577 }
0578
0579 static int snd_es1688_free(struct snd_es1688 *chip)
0580 {
0581 if (chip->hardware != ES1688_HW_UNDEF)
0582 snd_es1688_init(chip, 0);
0583 release_and_free_resource(chip->res_port);
0584 if (chip->irq >= 0)
0585 free_irq(chip->irq, (void *) chip);
0586 if (chip->dma8 >= 0) {
0587 disable_dma(chip->dma8);
0588 free_dma(chip->dma8);
0589 }
0590 return 0;
0591 }
0592
0593 static int snd_es1688_dev_free(struct snd_device *device)
0594 {
0595 struct snd_es1688 *chip = device->device_data;
0596 return snd_es1688_free(chip);
0597 }
0598
0599 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
0600 {
0601 static char tmp[16];
0602 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
0603 return tmp;
0604 }
0605
0606 int snd_es1688_create(struct snd_card *card,
0607 struct snd_es1688 *chip,
0608 unsigned long port,
0609 unsigned long mpu_port,
0610 int irq,
0611 int mpu_irq,
0612 int dma8,
0613 unsigned short hardware)
0614 {
0615 static const struct snd_device_ops ops = {
0616 .dev_free = snd_es1688_dev_free,
0617 };
0618
0619 int err;
0620
0621 if (chip == NULL)
0622 return -ENOMEM;
0623 chip->irq = -1;
0624 chip->dma8 = -1;
0625 chip->hardware = ES1688_HW_UNDEF;
0626
0627 chip->res_port = request_region(port + 4, 12, "ES1688");
0628 if (chip->res_port == NULL) {
0629 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
0630 err = -EBUSY;
0631 goto exit;
0632 }
0633
0634 err = request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip);
0635 if (err < 0) {
0636 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
0637 goto exit;
0638 }
0639
0640 chip->irq = irq;
0641 card->sync_irq = chip->irq;
0642 err = request_dma(dma8, "ES1688");
0643
0644 if (err < 0) {
0645 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
0646 goto exit;
0647 }
0648 chip->dma8 = dma8;
0649
0650 spin_lock_init(&chip->reg_lock);
0651 spin_lock_init(&chip->mixer_lock);
0652 chip->port = port;
0653 mpu_port &= ~0x000f;
0654 if (mpu_port < 0x300 || mpu_port > 0x330)
0655 mpu_port = 0;
0656 chip->mpu_port = mpu_port;
0657 chip->mpu_irq = mpu_irq;
0658 chip->hardware = hardware;
0659
0660 err = snd_es1688_probe(chip);
0661 if (err < 0)
0662 goto exit;
0663
0664 err = snd_es1688_init(chip, 1);
0665 if (err < 0)
0666 goto exit;
0667
0668
0669 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
0670 exit:
0671 if (err)
0672 snd_es1688_free(chip);
0673 return err;
0674 }
0675
0676 static const struct snd_pcm_ops snd_es1688_playback_ops = {
0677 .open = snd_es1688_playback_open,
0678 .close = snd_es1688_playback_close,
0679 .prepare = snd_es1688_playback_prepare,
0680 .trigger = snd_es1688_playback_trigger,
0681 .pointer = snd_es1688_playback_pointer,
0682 };
0683
0684 static const struct snd_pcm_ops snd_es1688_capture_ops = {
0685 .open = snd_es1688_capture_open,
0686 .close = snd_es1688_capture_close,
0687 .prepare = snd_es1688_capture_prepare,
0688 .trigger = snd_es1688_capture_trigger,
0689 .pointer = snd_es1688_capture_pointer,
0690 };
0691
0692 int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, int device)
0693 {
0694 struct snd_pcm *pcm;
0695 int err;
0696
0697 err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
0698 if (err < 0)
0699 return err;
0700
0701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
0702 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
0703
0704 pcm->private_data = chip;
0705 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
0706 strcpy(pcm->name, snd_es1688_chip_id(chip));
0707 chip->pcm = pcm;
0708
0709 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev,
0710 64*1024, 64*1024);
0711 return 0;
0712 }
0713
0714
0715
0716
0717
0718 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0719 {
0720 static const char * const texts[8] = {
0721 "Mic", "Mic Master", "CD", "AOUT",
0722 "Mic1", "Mix", "Line", "Master"
0723 };
0724
0725 return snd_ctl_enum_info(uinfo, 1, 8, texts);
0726 }
0727
0728 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0729 {
0730 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0731 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
0732 return 0;
0733 }
0734
0735 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0736 {
0737 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0738 unsigned long flags;
0739 unsigned char oval, nval;
0740 int change;
0741
0742 if (ucontrol->value.enumerated.item[0] > 8)
0743 return -EINVAL;
0744 spin_lock_irqsave(&chip->reg_lock, flags);
0745 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
0746 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
0747 change = nval != oval;
0748 if (change)
0749 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
0750 spin_unlock_irqrestore(&chip->reg_lock, flags);
0751 return change;
0752 }
0753
0754 #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
0755 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0756 .info = snd_es1688_info_single, \
0757 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
0758 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
0759
0760 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0761 {
0762 int mask = (kcontrol->private_value >> 16) & 0xff;
0763
0764 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
0765 uinfo->count = 1;
0766 uinfo->value.integer.min = 0;
0767 uinfo->value.integer.max = mask;
0768 return 0;
0769 }
0770
0771 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0772 {
0773 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0774 unsigned long flags;
0775 int reg = kcontrol->private_value & 0xff;
0776 int shift = (kcontrol->private_value >> 8) & 0xff;
0777 int mask = (kcontrol->private_value >> 16) & 0xff;
0778 int invert = (kcontrol->private_value >> 24) & 0xff;
0779
0780 spin_lock_irqsave(&chip->reg_lock, flags);
0781 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
0782 spin_unlock_irqrestore(&chip->reg_lock, flags);
0783 if (invert)
0784 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
0785 return 0;
0786 }
0787
0788 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0789 {
0790 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0791 unsigned long flags;
0792 int reg = kcontrol->private_value & 0xff;
0793 int shift = (kcontrol->private_value >> 8) & 0xff;
0794 int mask = (kcontrol->private_value >> 16) & 0xff;
0795 int invert = (kcontrol->private_value >> 24) & 0xff;
0796 int change;
0797 unsigned char oval, nval;
0798
0799 nval = (ucontrol->value.integer.value[0] & mask);
0800 if (invert)
0801 nval = mask - nval;
0802 nval <<= shift;
0803 spin_lock_irqsave(&chip->reg_lock, flags);
0804 oval = snd_es1688_mixer_read(chip, reg);
0805 nval = (oval & ~(mask << shift)) | nval;
0806 change = nval != oval;
0807 if (change)
0808 snd_es1688_mixer_write(chip, reg, nval);
0809 spin_unlock_irqrestore(&chip->reg_lock, flags);
0810 return change;
0811 }
0812
0813 #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
0814 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0815 .info = snd_es1688_info_double, \
0816 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
0817 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
0818
0819 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0820 {
0821 int mask = (kcontrol->private_value >> 24) & 0xff;
0822
0823 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
0824 uinfo->count = 2;
0825 uinfo->value.integer.min = 0;
0826 uinfo->value.integer.max = mask;
0827 return 0;
0828 }
0829
0830 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0831 {
0832 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0833 unsigned long flags;
0834 int left_reg = kcontrol->private_value & 0xff;
0835 int right_reg = (kcontrol->private_value >> 8) & 0xff;
0836 int shift_left = (kcontrol->private_value >> 16) & 0x07;
0837 int shift_right = (kcontrol->private_value >> 19) & 0x07;
0838 int mask = (kcontrol->private_value >> 24) & 0xff;
0839 int invert = (kcontrol->private_value >> 22) & 1;
0840 unsigned char left, right;
0841
0842 spin_lock_irqsave(&chip->reg_lock, flags);
0843 if (left_reg < 0xa0)
0844 left = snd_es1688_mixer_read(chip, left_reg);
0845 else
0846 left = snd_es1688_read(chip, left_reg);
0847 if (left_reg != right_reg) {
0848 if (right_reg < 0xa0)
0849 right = snd_es1688_mixer_read(chip, right_reg);
0850 else
0851 right = snd_es1688_read(chip, right_reg);
0852 } else
0853 right = left;
0854 spin_unlock_irqrestore(&chip->reg_lock, flags);
0855 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
0856 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
0857 if (invert) {
0858 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
0859 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
0860 }
0861 return 0;
0862 }
0863
0864 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0865 {
0866 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
0867 unsigned long flags;
0868 int left_reg = kcontrol->private_value & 0xff;
0869 int right_reg = (kcontrol->private_value >> 8) & 0xff;
0870 int shift_left = (kcontrol->private_value >> 16) & 0x07;
0871 int shift_right = (kcontrol->private_value >> 19) & 0x07;
0872 int mask = (kcontrol->private_value >> 24) & 0xff;
0873 int invert = (kcontrol->private_value >> 22) & 1;
0874 int change;
0875 unsigned char val1, val2, oval1, oval2;
0876
0877 val1 = ucontrol->value.integer.value[0] & mask;
0878 val2 = ucontrol->value.integer.value[1] & mask;
0879 if (invert) {
0880 val1 = mask - val1;
0881 val2 = mask - val2;
0882 }
0883 val1 <<= shift_left;
0884 val2 <<= shift_right;
0885 spin_lock_irqsave(&chip->reg_lock, flags);
0886 if (left_reg != right_reg) {
0887 if (left_reg < 0xa0)
0888 oval1 = snd_es1688_mixer_read(chip, left_reg);
0889 else
0890 oval1 = snd_es1688_read(chip, left_reg);
0891 if (right_reg < 0xa0)
0892 oval2 = snd_es1688_mixer_read(chip, right_reg);
0893 else
0894 oval2 = snd_es1688_read(chip, right_reg);
0895 val1 = (oval1 & ~(mask << shift_left)) | val1;
0896 val2 = (oval2 & ~(mask << shift_right)) | val2;
0897 change = val1 != oval1 || val2 != oval2;
0898 if (change) {
0899 if (left_reg < 0xa0)
0900 snd_es1688_mixer_write(chip, left_reg, val1);
0901 else
0902 snd_es1688_write(chip, left_reg, val1);
0903 if (right_reg < 0xa0)
0904 snd_es1688_mixer_write(chip, right_reg, val1);
0905 else
0906 snd_es1688_write(chip, right_reg, val1);
0907 }
0908 } else {
0909 if (left_reg < 0xa0)
0910 oval1 = snd_es1688_mixer_read(chip, left_reg);
0911 else
0912 oval1 = snd_es1688_read(chip, left_reg);
0913 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
0914 change = val1 != oval1;
0915 if (change) {
0916 if (left_reg < 0xa0)
0917 snd_es1688_mixer_write(chip, left_reg, val1);
0918 else
0919 snd_es1688_write(chip, left_reg, val1);
0920 }
0921
0922 }
0923 spin_unlock_irqrestore(&chip->reg_lock, flags);
0924 return change;
0925 }
0926
0927 static const struct snd_kcontrol_new snd_es1688_controls[] = {
0928 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
0929 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
0930 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
0931 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
0932 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
0933 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
0934 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
0935 ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
0936 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
0937 ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
0938 {
0939 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0940 .name = "Capture Source",
0941 .info = snd_es1688_info_mux,
0942 .get = snd_es1688_get_mux,
0943 .put = snd_es1688_put_mux,
0944 },
0945 };
0946
0947 #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
0948
0949 static const unsigned char snd_es1688_init_table[][2] = {
0950 { ES1688_MASTER_DEV, 0 },
0951 { ES1688_PCM_DEV, 0 },
0952 { ES1688_LINE_DEV, 0 },
0953 { ES1688_CD_DEV, 0 },
0954 { ES1688_FM_DEV, 0 },
0955 { ES1688_MIC_DEV, 0 },
0956 { ES1688_AUX_DEV, 0 },
0957 { ES1688_SPEAKER_DEV, 0 },
0958 { ES1688_RECLEV_DEV, 0 },
0959 { ES1688_REC_DEV, 0x17 }
0960 };
0961
0962 int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip)
0963 {
0964 unsigned int idx;
0965 int err;
0966 unsigned char reg, val;
0967
0968 if (snd_BUG_ON(!chip || !card))
0969 return -EINVAL;
0970
0971 strcpy(card->mixername, snd_es1688_chip_id(chip));
0972
0973 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
0974 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip));
0975 if (err < 0)
0976 return err;
0977 }
0978 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
0979 reg = snd_es1688_init_table[idx][0];
0980 val = snd_es1688_init_table[idx][1];
0981 if (reg < 0xa0)
0982 snd_es1688_mixer_write(chip, reg, val);
0983 else
0984 snd_es1688_write(chip, reg, val);
0985 }
0986 return 0;
0987 }
0988
0989 EXPORT_SYMBOL(snd_es1688_mixer_write);
0990 EXPORT_SYMBOL(snd_es1688_create);
0991 EXPORT_SYMBOL(snd_es1688_pcm);
0992 EXPORT_SYMBOL(snd_es1688_mixer);