0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/delay.h>
0011 #include <linux/device.h>
0012 #include <linux/firmware.h>
0013 #include <linux/io.h>
0014 #include <sound/core.h>
0015 #include "vxpocket.h"
0016
0017
0018 static const int vxp_reg_offset[VX_REG_MAX] = {
0019 [VX_ICR] = 0x00,
0020 [VX_CVR] = 0x01,
0021 [VX_ISR] = 0x02,
0022 [VX_IVR] = 0x03,
0023 [VX_RXH] = 0x05,
0024 [VX_RXM] = 0x06,
0025 [VX_RXL] = 0x07,
0026 [VX_DMA] = 0x04,
0027 [VX_CDSP] = 0x08,
0028 [VX_LOFREQ] = 0x09,
0029 [VX_HIFREQ] = 0x0a,
0030 [VX_DATA] = 0x0b,
0031 [VX_MICRO] = 0x0c,
0032 [VX_DIALOG] = 0x0d,
0033 [VX_CSUER] = 0x0e,
0034 [VX_RUER] = 0x0f,
0035 };
0036
0037
0038 static inline unsigned long vxp_reg_addr(struct vx_core *_chip, int reg)
0039 {
0040 struct snd_vxpocket *chip = to_vxpocket(_chip);
0041 return chip->port + vxp_reg_offset[reg];
0042 }
0043
0044
0045
0046
0047
0048 static unsigned char vxp_inb(struct vx_core *chip, int offset)
0049 {
0050 return inb(vxp_reg_addr(chip, offset));
0051 }
0052
0053
0054
0055
0056
0057
0058 static void vxp_outb(struct vx_core *chip, int offset, unsigned char val)
0059 {
0060 outb(val, vxp_reg_addr(chip, offset));
0061 }
0062
0063
0064
0065
0066 #undef vx_inb
0067 #define vx_inb(chip,reg) vxp_inb((struct vx_core *)(chip), VX_##reg)
0068 #undef vx_outb
0069 #define vx_outb(chip,reg,val) vxp_outb((struct vx_core *)(chip), VX_##reg,val)
0070
0071
0072
0073
0074
0075
0076
0077 static int vx_check_magic(struct vx_core *chip)
0078 {
0079 unsigned long end_time = jiffies + HZ / 5;
0080 int c;
0081 do {
0082 c = vx_inb(chip, CDSP);
0083 if (c == CDSP_MAGIC)
0084 return 0;
0085 msleep(10);
0086 } while (time_after_eq(end_time, jiffies));
0087 snd_printk(KERN_ERR "cannot find xilinx magic word (%x)\n", c);
0088 return -EIO;
0089 }
0090
0091
0092
0093
0094
0095
0096 #define XX_DSP_RESET_WAIT_TIME 2
0097
0098 static void vxp_reset_dsp(struct vx_core *_chip)
0099 {
0100 struct snd_vxpocket *chip = to_vxpocket(_chip);
0101
0102
0103 vx_outb(chip, CDSP, chip->regCDSP | VXP_CDSP_DSP_RESET_MASK);
0104 vx_inb(chip, CDSP);
0105 mdelay(XX_DSP_RESET_WAIT_TIME);
0106
0107 chip->regCDSP &= ~VXP_CDSP_DSP_RESET_MASK;
0108 vx_outb(chip, CDSP, chip->regCDSP);
0109 vx_inb(chip, CDSP);
0110 mdelay(XX_DSP_RESET_WAIT_TIME);
0111 }
0112
0113
0114
0115
0116 static void vxp_reset_codec(struct vx_core *_chip)
0117 {
0118 struct snd_vxpocket *chip = to_vxpocket(_chip);
0119
0120
0121 vx_outb(chip, CDSP, chip->regCDSP | VXP_CDSP_CODEC_RESET_MASK);
0122 vx_inb(chip, CDSP);
0123 msleep(10);
0124
0125 chip->regCDSP &= ~VXP_CDSP_CODEC_RESET_MASK;
0126 vx_outb(chip, CDSP, chip->regCDSP);
0127 vx_inb(chip, CDSP);
0128 msleep(1);
0129 }
0130
0131
0132
0133
0134
0135 static int vxp_load_xilinx_binary(struct vx_core *_chip, const struct firmware *fw)
0136 {
0137 struct snd_vxpocket *chip = to_vxpocket(_chip);
0138 unsigned int i;
0139 int c;
0140 int regCSUER, regRUER;
0141 const unsigned char *image;
0142 unsigned char data;
0143
0144
0145 chip->regDIALOG |= VXP_DLG_XILINX_REPROG_MASK;
0146 vx_outb(chip, DIALOG, chip->regDIALOG);
0147
0148
0149 regCSUER = vx_inb(chip, CSUER);
0150 regRUER = vx_inb(chip, RUER);
0151
0152
0153 vx_outb(chip, ICR, 0);
0154
0155
0156 snd_printdd(KERN_DEBUG "check ISR_HF2\n");
0157 if (vx_check_isr(_chip, ISR_HF2, ISR_HF2, 20) < 0)
0158 goto _error;
0159
0160
0161 vx_outb(chip, ICR, ICR_HF1);
0162 image = fw->data;
0163 for (i = 0; i < fw->size; i++, image++) {
0164 data = *image;
0165 if (vx_wait_isr_bit(_chip, ISR_TX_EMPTY) < 0)
0166 goto _error;
0167 vx_outb(chip, TXL, data);
0168
0169 if (vx_wait_for_rx_full(_chip) < 0)
0170 goto _error;
0171 c = vx_inb(chip, RXL);
0172 if (c != (int)data)
0173 snd_printk(KERN_ERR "vxpocket: load xilinx mismatch at %d: 0x%x != 0x%x\n", i, c, (int)data);
0174 }
0175
0176
0177 vx_outb(chip, ICR, 0);
0178
0179
0180 if (vx_check_isr(_chip, ISR_HF3, ISR_HF3, 20) < 0)
0181 goto _error;
0182
0183
0184 if (vx_wait_for_rx_full(_chip) < 0)
0185 goto _error;
0186
0187 c = (int)vx_inb(chip, RXH) << 16;
0188 c |= (int)vx_inb(chip, RXM) << 8;
0189 c |= vx_inb(chip, RXL);
0190
0191 snd_printdd(KERN_DEBUG "xilinx: dsp size received 0x%x, orig 0x%zx\n", c, fw->size);
0192
0193 vx_outb(chip, ICR, ICR_HF0);
0194
0195
0196 msleep(300);
0197
0198
0199 if (vx_check_magic(_chip) < 0)
0200 goto _error;
0201
0202
0203 vx_outb(chip, CSUER, regCSUER);
0204 vx_outb(chip, RUER, regRUER);
0205
0206
0207 chip->regDIALOG |= VXP_DLG_XILINX_REPROG_MASK;
0208 vx_outb(chip, DIALOG, chip->regDIALOG);
0209 vx_inb(chip, DIALOG);
0210 msleep(10);
0211 chip->regDIALOG &= ~VXP_DLG_XILINX_REPROG_MASK;
0212 vx_outb(chip, DIALOG, chip->regDIALOG);
0213 vx_inb(chip, DIALOG);
0214
0215
0216 vxp_reset_codec(_chip);
0217 vx_reset_dsp(_chip);
0218
0219 return 0;
0220
0221 _error:
0222 vx_outb(chip, CSUER, regCSUER);
0223 vx_outb(chip, RUER, regRUER);
0224 chip->regDIALOG &= ~VXP_DLG_XILINX_REPROG_MASK;
0225 vx_outb(chip, DIALOG, chip->regDIALOG);
0226 return -EIO;
0227 }
0228
0229
0230
0231
0232
0233 static int vxp_load_dsp(struct vx_core *vx, int index, const struct firmware *fw)
0234 {
0235 int err;
0236
0237 switch (index) {
0238 case 0:
0239
0240 err = vx_check_magic(vx);
0241 if (err < 0)
0242 return err;
0243 err = snd_vx_load_boot_image(vx, fw);
0244 if (err < 0)
0245 return err;
0246 return 0;
0247 case 1:
0248
0249 return vxp_load_xilinx_binary(vx, fw);
0250 case 2:
0251
0252 return snd_vx_dsp_boot(vx, fw);
0253 case 3:
0254
0255 return snd_vx_dsp_load(vx, fw);
0256 default:
0257 snd_BUG();
0258 return -EINVAL;
0259 }
0260 }
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 static int vxp_test_and_ack(struct vx_core *_chip)
0271 {
0272 struct snd_vxpocket *chip = to_vxpocket(_chip);
0273
0274
0275 if (! (_chip->chip_status & VX_STAT_XILINX_LOADED))
0276 return -ENXIO;
0277
0278 if (! (vx_inb(chip, DIALOG) & VXP_DLG_MEMIRQ_MASK))
0279 return -EIO;
0280
0281
0282
0283 vx_outb(chip, DIALOG, chip->regDIALOG | VXP_DLG_ACK_MEMIRQ_MASK);
0284
0285
0286
0287 vx_inb(chip, DIALOG);
0288 vx_outb(chip, DIALOG, chip->regDIALOG & ~VXP_DLG_ACK_MEMIRQ_MASK);
0289
0290 return 0;
0291 }
0292
0293
0294
0295
0296
0297 static void vxp_validate_irq(struct vx_core *_chip, int enable)
0298 {
0299 struct snd_vxpocket *chip = to_vxpocket(_chip);
0300
0301
0302 if (enable)
0303 chip->regCDSP |= VXP_CDSP_VALID_IRQ_MASK;
0304 else
0305 chip->regCDSP &= ~VXP_CDSP_VALID_IRQ_MASK;
0306 vx_outb(chip, CDSP, chip->regCDSP);
0307 }
0308
0309
0310
0311
0312
0313 static void vx_setup_pseudo_dma(struct vx_core *_chip, int do_write)
0314 {
0315 struct snd_vxpocket *chip = to_vxpocket(_chip);
0316
0317
0318 vx_outb(chip, ICR, do_write ? ICR_TREQ : ICR_RREQ);
0319
0320 vx_inb(chip, ISR);
0321 vx_outb(chip, ISR, 0);
0322
0323
0324 chip->regDIALOG |= VXP_DLG_DMA16_SEL_MASK;
0325 chip->regDIALOG |= do_write ? VXP_DLG_DMAWRITE_SEL_MASK : VXP_DLG_DMAREAD_SEL_MASK;
0326 vx_outb(chip, DIALOG, chip->regDIALOG);
0327
0328 }
0329
0330
0331
0332
0333 static void vx_release_pseudo_dma(struct vx_core *_chip)
0334 {
0335 struct snd_vxpocket *chip = to_vxpocket(_chip);
0336
0337
0338 chip->regDIALOG &= ~(VXP_DLG_DMAWRITE_SEL_MASK|
0339 VXP_DLG_DMAREAD_SEL_MASK|
0340 VXP_DLG_DMA16_SEL_MASK);
0341 vx_outb(chip, DIALOG, chip->regDIALOG);
0342
0343 vx_outb(chip, ICR, 0);
0344 }
0345
0346
0347
0348
0349
0350
0351
0352
0353 static void vxp_dma_write(struct vx_core *chip, struct snd_pcm_runtime *runtime,
0354 struct vx_pipe *pipe, int count)
0355 {
0356 long port = vxp_reg_addr(chip, VX_DMA);
0357 int offset = pipe->hw_ptr;
0358 unsigned short *addr = (unsigned short *)(runtime->dma_area + offset);
0359
0360 vx_setup_pseudo_dma(chip, 1);
0361 if (offset + count >= pipe->buffer_bytes) {
0362 int length = pipe->buffer_bytes - offset;
0363 count -= length;
0364 length >>= 1;
0365
0366 for (; length > 0; length--) {
0367 outw(*addr, port);
0368 addr++;
0369 }
0370 addr = (unsigned short *)runtime->dma_area;
0371 pipe->hw_ptr = 0;
0372 }
0373 pipe->hw_ptr += count;
0374 count >>= 1;
0375
0376 for (; count > 0; count--) {
0377 outw(*addr, port);
0378 addr++;
0379 }
0380 vx_release_pseudo_dma(chip);
0381 }
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392 static void vxp_dma_read(struct vx_core *chip, struct snd_pcm_runtime *runtime,
0393 struct vx_pipe *pipe, int count)
0394 {
0395 struct snd_vxpocket *pchip = to_vxpocket(chip);
0396 long port = vxp_reg_addr(chip, VX_DMA);
0397 int offset = pipe->hw_ptr;
0398 unsigned short *addr = (unsigned short *)(runtime->dma_area + offset);
0399
0400 if (snd_BUG_ON(count % 2))
0401 return;
0402 vx_setup_pseudo_dma(chip, 0);
0403 if (offset + count >= pipe->buffer_bytes) {
0404 int length = pipe->buffer_bytes - offset;
0405 count -= length;
0406 length >>= 1;
0407
0408 for (; length > 0; length--)
0409 *addr++ = inw(port);
0410 addr = (unsigned short *)runtime->dma_area;
0411 pipe->hw_ptr = 0;
0412 }
0413 pipe->hw_ptr += count;
0414 count >>= 1;
0415
0416 for (; count > 1; count--)
0417 *addr++ = inw(port);
0418
0419 pchip->regDIALOG &= ~VXP_DLG_DMAREAD_SEL_MASK;
0420 vx_outb(chip, DIALOG, pchip->regDIALOG);
0421
0422 *addr = inw(port);
0423
0424 pchip->regDIALOG &= ~VXP_DLG_DMA16_SEL_MASK;
0425 vx_outb(chip, DIALOG, pchip->regDIALOG);
0426
0427 vx_outb(chip, ICR, 0);
0428 }
0429
0430
0431
0432
0433
0434 static void vxp_write_codec_reg(struct vx_core *chip, int codec, unsigned int data)
0435 {
0436 int i;
0437
0438
0439 if (! codec)
0440 vx_inb(chip, LOFREQ);
0441 else
0442 vx_inb(chip, CODEC2);
0443
0444
0445 for (i = 0; i < 24; i++, data <<= 1)
0446 vx_outb(chip, DATA, ((data & 0x800000) ? VX_DATA_CODEC_MASK : 0));
0447
0448
0449 vx_inb(chip, HIFREQ);
0450 }
0451
0452
0453
0454
0455
0456
0457 void vx_set_mic_boost(struct vx_core *chip, int boost)
0458 {
0459 struct snd_vxpocket *pchip = to_vxpocket(chip);
0460
0461 if (chip->chip_status & VX_STAT_IS_STALE)
0462 return;
0463
0464 mutex_lock(&chip->lock);
0465 if (pchip->regCDSP & P24_CDSP_MICS_SEL_MASK) {
0466 if (boost) {
0467
0468 pchip->regCDSP &= ~P24_CDSP_MIC20_SEL_MASK;
0469 pchip->regCDSP |= P24_CDSP_MIC38_SEL_MASK;
0470 } else {
0471
0472 pchip->regCDSP |= P24_CDSP_MIC20_SEL_MASK;
0473 pchip->regCDSP &= ~P24_CDSP_MIC38_SEL_MASK;
0474 }
0475 vx_outb(chip, CDSP, pchip->regCDSP);
0476 }
0477 mutex_unlock(&chip->lock);
0478 }
0479
0480
0481
0482
0483 static int vx_compute_mic_level(int level)
0484 {
0485 switch (level) {
0486 case 5: level = 6 ; break;
0487 case 6: level = 8 ; break;
0488 case 7: level = 11; break;
0489 case 8: level = 15; break;
0490 default: break ;
0491 }
0492 return level;
0493 }
0494
0495
0496
0497
0498
0499 void vx_set_mic_level(struct vx_core *chip, int level)
0500 {
0501 struct snd_vxpocket *pchip = to_vxpocket(chip);
0502
0503 if (chip->chip_status & VX_STAT_IS_STALE)
0504 return;
0505
0506 mutex_lock(&chip->lock);
0507 if (pchip->regCDSP & VXP_CDSP_MIC_SEL_MASK) {
0508 level = vx_compute_mic_level(level);
0509 vx_outb(chip, MICRO, level);
0510 }
0511 mutex_unlock(&chip->lock);
0512 }
0513
0514
0515
0516
0517
0518 static void vxp_change_audio_source(struct vx_core *_chip, int src)
0519 {
0520 struct snd_vxpocket *chip = to_vxpocket(_chip);
0521
0522 switch (src) {
0523 case VX_AUDIO_SRC_DIGITAL:
0524 chip->regCDSP |= VXP_CDSP_DATAIN_SEL_MASK;
0525 vx_outb(chip, CDSP, chip->regCDSP);
0526 break;
0527 case VX_AUDIO_SRC_LINE:
0528 chip->regCDSP &= ~VXP_CDSP_DATAIN_SEL_MASK;
0529 if (_chip->type == VX_TYPE_VXP440)
0530 chip->regCDSP &= ~P24_CDSP_MICS_SEL_MASK;
0531 else
0532 chip->regCDSP &= ~VXP_CDSP_MIC_SEL_MASK;
0533 vx_outb(chip, CDSP, chip->regCDSP);
0534 break;
0535 case VX_AUDIO_SRC_MIC:
0536 chip->regCDSP &= ~VXP_CDSP_DATAIN_SEL_MASK;
0537
0538 if (_chip->type == VX_TYPE_VXP440) {
0539 chip->regCDSP &= ~P24_CDSP_MICS_SEL_MASK;
0540 if (chip->mic_level)
0541 chip->regCDSP |= P24_CDSP_MIC38_SEL_MASK;
0542 else
0543 chip->regCDSP |= P24_CDSP_MIC20_SEL_MASK;
0544 vx_outb(chip, CDSP, chip->regCDSP);
0545 } else {
0546 chip->regCDSP |= VXP_CDSP_MIC_SEL_MASK;
0547 vx_outb(chip, CDSP, chip->regCDSP);
0548 vx_outb(chip, MICRO, vx_compute_mic_level(chip->mic_level));
0549 }
0550 break;
0551 }
0552 }
0553
0554
0555
0556
0557
0558 static void vxp_set_clock_source(struct vx_core *_chip, int source)
0559 {
0560 struct snd_vxpocket *chip = to_vxpocket(_chip);
0561
0562 if (source == INTERNAL_QUARTZ)
0563 chip->regCDSP &= ~VXP_CDSP_CLOCKIN_SEL_MASK;
0564 else
0565 chip->regCDSP |= VXP_CDSP_CLOCKIN_SEL_MASK;
0566 vx_outb(chip, CDSP, chip->regCDSP);
0567 }
0568
0569
0570
0571
0572
0573 static void vxp_reset_board(struct vx_core *_chip, int cold_reset)
0574 {
0575 struct snd_vxpocket *chip = to_vxpocket(_chip);
0576
0577 chip->regCDSP = 0;
0578 chip->regDIALOG = 0;
0579 }
0580
0581
0582
0583
0584
0585
0586 const struct snd_vx_ops snd_vxpocket_ops = {
0587 .in8 = vxp_inb,
0588 .out8 = vxp_outb,
0589 .test_and_ack = vxp_test_and_ack,
0590 .validate_irq = vxp_validate_irq,
0591 .write_codec = vxp_write_codec_reg,
0592 .reset_codec = vxp_reset_codec,
0593 .change_audio_source = vxp_change_audio_source,
0594 .set_clock_source = vxp_set_clock_source,
0595 .load_dsp = vxp_load_dsp,
0596 .add_controls = vxp_add_mic_controls,
0597 .reset_dsp = vxp_reset_dsp,
0598 .reset_board = vxp_reset_board,
0599 .dma_write = vxp_dma_write,
0600 .dma_read = vxp_dma_read,
0601 };