patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / pcmcia / vx / vxp_ops.c
1 /*
2  * Driver for Digigram VXpocket soundcards
3  *
4  * lowlevel routines for VXpocket soundcards
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <sound/driver.h>
24 #include <linux/delay.h>
25 #include <sound/core.h>
26 #include <asm/io.h>
27 #include "vxpocket.h"
28
29 #define chip_t vx_core_t
30
31
32 static int vxp_reg_offset[VX_REG_MAX] = {
33         [VX_ICR]        = 0x00,         // ICR
34         [VX_CVR]        = 0x01,         // CVR
35         [VX_ISR]        = 0x02,         // ISR
36         [VX_IVR]        = 0x03,         // IVR
37         [VX_RXH]        = 0x05,         // RXH
38         [VX_RXM]        = 0x06,         // RXM
39         [VX_RXL]        = 0x07,         // RXL
40         [VX_DMA]        = 0x04,         // DMA
41         [VX_CDSP]       = 0x08,         // CDSP
42         [VX_LOFREQ]     = 0x09,         // LFREQ
43         [VX_HIFREQ]     = 0x0a,         // HFREQ
44         [VX_DATA]       = 0x0b,         // DATA
45         [VX_MICRO]      = 0x0c,         // MICRO
46         [VX_DIALOG]     = 0x0d,         // DIALOG
47         [VX_CSUER]      = 0x0e,         // CSUER
48         [VX_RUER]       = 0x0f,         // RUER
49 };
50
51
52 inline static unsigned long vxp_reg_addr(vx_core_t *_chip, int reg)
53 {
54         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
55         return chip->port + vxp_reg_offset[reg];
56 }
57
58 /*
59  * snd_vx_inb - read a byte from the register
60  * @offset: register offset
61  */
62 static unsigned char vxp_inb(vx_core_t *chip, int offset)
63 {
64         return inb(vxp_reg_addr(chip, offset));
65 }
66
67 /*
68  * snd_vx_outb - write a byte on the register
69  * @offset: the register offset
70  * @val: the value to write
71  */
72 static void vxp_outb(vx_core_t *chip, int offset, unsigned char val)
73 {
74         outb(val, vxp_reg_addr(chip, offset));
75 }
76
77 /*
78  * redefine macros to call directly
79  */
80 #undef vx_inb
81 #define vx_inb(chip,reg)        vxp_inb((vx_core_t*)(chip), VX_##reg)
82 #undef vx_outb
83 #define vx_outb(chip,reg,val)   vxp_outb((vx_core_t*)(chip), VX_##reg,val)
84
85
86 /*
87  * vx_check_magic - check the magic word on xilinx
88  *
89  * returns zero if a magic word is detected, or a negative error code.
90  */
91 static int vx_check_magic(vx_core_t *chip)
92 {
93         unsigned long end_time = jiffies + HZ / 5;
94         int c;
95         do {
96                 c = vx_inb(chip, CDSP);
97                 if (c == CDSP_MAGIC)
98                         return 0;
99                 snd_vx_delay(chip, 10);
100         } while (time_after_eq(end_time, jiffies));
101         snd_printk(KERN_ERR "cannot find xilinx magic word (%x)\n", c);
102         return -EIO;
103 }
104
105
106 /*
107  * vx_reset_dsp - reset the DSP
108  */
109
110 #define XX_DSP_RESET_WAIT_TIME          2       /* ms */
111
112 static void vxp_reset_dsp(vx_core_t *_chip)
113 {
114         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
115
116         /* set the reset dsp bit to 1 */
117         vx_outb(chip, CDSP, chip->regCDSP | VXP_CDSP_DSP_RESET_MASK);
118         vx_inb(chip, CDSP);
119         mdelay(XX_DSP_RESET_WAIT_TIME);
120         /* reset the bit */
121         chip->regCDSP &= ~VXP_CDSP_DSP_RESET_MASK;
122         vx_outb(chip, CDSP, chip->regCDSP);
123         vx_inb(chip, CDSP);
124         mdelay(XX_DSP_RESET_WAIT_TIME);
125 }
126
127 /*
128  * reset codec bit
129  */
130 static void vxp_reset_codec(vx_core_t *_chip)
131 {
132         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
133
134         /* Set the reset CODEC bit to 1. */
135         vx_outb(chip, CDSP, chip->regCDSP | VXP_CDSP_CODEC_RESET_MASK);
136         vx_inb(chip, CDSP);
137         snd_vx_delay(_chip, 10);
138         /* Set the reset CODEC bit to 0. */
139         chip->regCDSP &= ~VXP_CDSP_CODEC_RESET_MASK;
140         vx_outb(chip, CDSP, chip->regCDSP);
141         vx_inb(chip, CDSP);
142         snd_vx_delay(_chip, 1);
143 }
144
145 /*
146  * vx_load_xilinx_binary - load the xilinx binary image
147  * the binary image is the binary array converted from the bitstream file.
148  */
149 static int vxp_load_xilinx_binary(vx_core_t *_chip, const snd_hwdep_dsp_image_t *xilinx)
150 {
151         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
152         unsigned int i;
153         int c;
154         int regCSUER, regRUER;
155         unsigned char __user *image;
156         unsigned char data;
157
158         /* Switch to programmation mode */
159         chip->regDIALOG |= VXP_DLG_XILINX_REPROG_MASK;
160         vx_outb(chip, DIALOG, chip->regDIALOG);
161
162         /* Save register CSUER and RUER */
163         regCSUER = vx_inb(chip, CSUER);
164         regRUER = vx_inb(chip, RUER);
165
166         /* reset HF0 and HF1 */
167         vx_outb(chip, ICR, 0);
168
169         /* Wait for answer HF2 equal to 1 */
170         snd_printdd(KERN_DEBUG "check ISR_HF2\n");
171         if (vx_check_isr(_chip, ISR_HF2, ISR_HF2, 20) < 0)
172                 goto _error;
173
174         /* set HF1 for loading xilinx binary */
175         vx_outb(chip, ICR, ICR_HF1);
176         image = xilinx->image;
177         for (i = 0; i < xilinx->length; i++, image++) {
178                 __get_user(data, image);
179                 if (vx_wait_isr_bit(_chip, ISR_TX_EMPTY) < 0)
180                         goto _error;
181                 vx_outb(chip, TXL, data);
182                 /* wait for reading */
183                 if (vx_wait_for_rx_full(_chip) < 0)
184                         goto _error;
185                 c = vx_inb(chip, RXL);
186                 if (c != (int)data)
187                         snd_printk(KERN_ERR "vxpocket: load xilinx mismatch at %d: 0x%x != 0x%x\n", i, c, (int)data);
188         }
189
190         /* reset HF1 */
191         vx_outb(chip, ICR, 0);
192
193         /* wait for HF3 */
194         if (vx_check_isr(_chip, ISR_HF3, ISR_HF3, 20) < 0)
195                 goto _error;
196
197         /* read the number of bytes received */
198         if (vx_wait_for_rx_full(_chip) < 0)
199                 goto _error;
200
201         c = (int)vx_inb(chip, RXH) << 16;
202         c |= (int)vx_inb(chip, RXM) << 8;
203         c |= vx_inb(chip, RXL);
204
205         snd_printdd(KERN_DEBUG "xilinx: dsp size received 0x%x, orig 0x%x\n", c, xilinx->length);
206
207         vx_outb(chip, ICR, ICR_HF0);
208
209         /* TEMPO 250ms : wait until Xilinx is downloaded */
210         snd_vx_delay(_chip, 300);
211
212         /* test magical word */
213         if (vx_check_magic(_chip) < 0)
214                 goto _error;
215
216         /* Restore register 0x0E and 0x0F (thus replacing COR and FCSR) */
217         vx_outb(chip, CSUER, regCSUER);
218         vx_outb(chip, RUER, regRUER);
219
220         /* Reset the Xilinx's signal enabling IO access */
221         chip->regDIALOG |= VXP_DLG_XILINX_REPROG_MASK;
222         vx_outb(chip, DIALOG, chip->regDIALOG);
223         vx_inb(chip, DIALOG);
224         snd_vx_delay(_chip, 10);
225         chip->regDIALOG &= ~VXP_DLG_XILINX_REPROG_MASK;
226         vx_outb(chip, DIALOG, chip->regDIALOG);
227         vx_inb(chip, DIALOG);
228
229         /* Reset of the Codec */
230         vxp_reset_codec(_chip);
231         vx_reset_dsp(_chip);
232
233         return 0;
234
235  _error:
236         vx_outb(chip, CSUER, regCSUER);
237         vx_outb(chip, RUER, regRUER);
238         chip->regDIALOG &= ~VXP_DLG_XILINX_REPROG_MASK;
239         vx_outb(chip, DIALOG, chip->regDIALOG);
240         return -EIO;
241 }
242
243
244 /*
245  * vxp_load_dsp - load_dsp callback
246  */
247 static int vxp_load_dsp(vx_core_t *vx, const snd_hwdep_dsp_image_t *dsp)
248 {
249         int err;
250
251         if (*dsp->name)
252                 snd_printdd("loading dsp [%d] %s, size = %d\n", dsp->index, dsp->name, dsp->length);
253
254         switch (dsp->index) {
255         case 0:
256                 /* xilinx boot */
257                 if ((err = vx_check_magic(vx)) < 0)
258                         return err;
259                 if ((err = snd_vx_load_boot_image(vx, dsp)) < 0)
260                         return err;
261                 return 0;
262         case 1:
263                 /* xilinx image */
264                 return vxp_load_xilinx_binary(vx, dsp);
265         case 2:
266                 /* DSP boot */
267                 return snd_vx_dsp_boot(vx, dsp);
268         case 3:
269                 /* DSP image */
270                 return snd_vx_dsp_load(vx, dsp);
271         default:
272                 snd_BUG();
273                 return -EINVAL;
274         }
275 }
276                 
277
278 /*
279  * vx_test_and_ack - test and acknowledge interrupt
280  *
281  * called from irq hander, too
282  *
283  * spinlock held!
284  */
285 static int vxp_test_and_ack(vx_core_t *_chip)
286 {
287         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
288
289         /* not booted yet? */
290         if (! (_chip->chip_status & VX_STAT_XILINX_LOADED))
291                 return -ENXIO;
292
293         if (! (vx_inb(chip, DIALOG) & VXP_DLG_MEMIRQ_MASK))
294                 return -EIO;
295         
296         /* ok, interrupts generated, now ack it */
297         /* set ACQUIT bit up and down */
298         vx_outb(chip, DIALOG, chip->regDIALOG | VXP_DLG_ACK_MEMIRQ_MASK);
299         /* useless read just to spend some time and maintain
300          * the ACQUIT signal up for a while ( a bus cycle )
301          */
302         vx_inb(chip, DIALOG);
303         vx_outb(chip, DIALOG, chip->regDIALOG & ~VXP_DLG_ACK_MEMIRQ_MASK);
304
305         return 0;
306 }
307
308
309 /*
310  * vx_validate_irq - enable/disable IRQ
311  */
312 static void vxp_validate_irq(vx_core_t *_chip, int enable)
313 {
314         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
315
316         /* Set the interrupt enable bit to 1 in CDSP register */
317         if (enable)
318                 chip->regCDSP |= VXP_CDSP_VALID_IRQ_MASK;
319         else
320                 chip->regCDSP &= ~VXP_CDSP_VALID_IRQ_MASK;
321         vx_outb(chip, CDSP, chip->regCDSP);
322 }
323
324 /*
325  * vx_setup_pseudo_dma - set up the pseudo dma read/write mode.
326  * @do_write: 0 = read, 1 = set up for DMA write
327  */
328 static void vx_setup_pseudo_dma(vx_core_t *_chip, int do_write)
329 {
330         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
331
332         /* Interrupt mode and HREQ pin enabled for host transmit / receive data transfers */
333         vx_outb(chip, ICR, do_write ? ICR_TREQ : ICR_RREQ);
334         /* Reset the pseudo-dma register */
335         vx_inb(chip, ISR);
336         vx_outb(chip, ISR, 0);
337
338         /* Select DMA in read/write transfer mode and in 16-bit accesses */
339         chip->regDIALOG |= VXP_DLG_DMA16_SEL_MASK;
340         chip->regDIALOG |= do_write ? VXP_DLG_DMAWRITE_SEL_MASK : VXP_DLG_DMAREAD_SEL_MASK;
341         vx_outb(chip, DIALOG, chip->regDIALOG);
342
343 }
344
345 /*
346  * vx_release_pseudo_dma - disable the pseudo-DMA mode
347  */
348 static void vx_release_pseudo_dma(vx_core_t *_chip)
349 {
350         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
351
352         /* Disable DMA and 16-bit accesses */
353         chip->regDIALOG &= ~(VXP_DLG_DMAWRITE_SEL_MASK|
354                              VXP_DLG_DMAREAD_SEL_MASK|
355                              VXP_DLG_DMA16_SEL_MASK);
356         vx_outb(chip, DIALOG, chip->regDIALOG);
357         /* HREQ pin disabled. */
358         vx_outb(chip, ICR, 0);
359 }
360
361 /*
362  * vx_pseudo_dma_write - write bulk data on pseudo-DMA mode
363  * @count: data length to transfer in bytes
364  *
365  * data size must be aligned to 6 bytes to ensure the 24bit alignment on DSP.
366  * NB: call with a certain lock!
367  */
368 static void vxp_dma_write(vx_core_t *chip, snd_pcm_runtime_t *runtime,
369                           vx_pipe_t *pipe, int count)
370 {
371         long port = vxp_reg_addr(chip, VX_DMA);
372         int offset = pipe->hw_ptr;
373         unsigned short *addr = (unsigned short *)(runtime->dma_area + offset);
374
375         vx_setup_pseudo_dma(chip, 1);
376         if (offset + count > pipe->buffer_bytes) {
377                 int length = pipe->buffer_bytes - offset;
378                 count -= length;
379                 length >>= 1; /* in 16bit words */
380                 /* Transfer using pseudo-dma. */
381                 while (length-- > 0) {
382                         outw(cpu_to_le16(*addr), port);
383                         addr++;
384                 }
385                 addr = (unsigned short *)runtime->dma_area;
386                 pipe->hw_ptr = 0;
387         }
388         pipe->hw_ptr += count;
389         count >>= 1; /* in 16bit words */
390         /* Transfer using pseudo-dma. */
391         while (count-- > 0) {
392                 outw(cpu_to_le16(*addr), port);
393                 addr++;
394         }
395         vx_release_pseudo_dma(chip);
396 }
397
398
399 /*
400  * vx_pseudo_dma_read - read bulk data on pseudo DMA mode
401  * @offset: buffer offset in bytes
402  * @count: data length to transfer in bytes
403  *
404  * the read length must be aligned to 6 bytes, as well as write.
405  * NB: call with a certain lock!
406  */
407 static void vxp_dma_read(vx_core_t *chip, snd_pcm_runtime_t *runtime,
408                          vx_pipe_t *pipe, int count)
409 {
410         struct snd_vxpocket *pchip = (struct snd_vxpocket *)chip;
411         long port = vxp_reg_addr(chip, VX_DMA);
412         int offset = pipe->hw_ptr;
413         unsigned short *addr = (unsigned short *)(runtime->dma_area + offset);
414
415         snd_assert(count % 2 == 0, return);
416         vx_setup_pseudo_dma(chip, 0);
417         if (offset + count > pipe->buffer_bytes) {
418                 int length = pipe->buffer_bytes - offset;
419                 count -= length;
420                 length >>= 1; /* in 16bit words */
421                 /* Transfer using pseudo-dma. */
422                 while (length-- > 0)
423                         *addr++ = le16_to_cpu(inw(port));
424                 addr = (unsigned short *)runtime->dma_area;
425                 pipe->hw_ptr = 0;
426         }
427         pipe->hw_ptr += count;
428         count >>= 1; /* in 16bit words */
429         /* Transfer using pseudo-dma. */
430         while (count-- > 1)
431                 *addr++ = le16_to_cpu(inw(port));
432         /* Disable DMA */
433         pchip->regDIALOG &= ~VXP_DLG_DMAREAD_SEL_MASK;
434         vx_outb(chip, DIALOG, pchip->regDIALOG);
435         /* Read the last word (16 bits) */
436         *addr = le16_to_cpu(inw(port));
437         /* Disable 16-bit accesses */
438         pchip->regDIALOG &= ~VXP_DLG_DMA16_SEL_MASK;
439         vx_outb(chip, DIALOG, pchip->regDIALOG);
440         /* HREQ pin disabled. */
441         vx_outb(chip, ICR, 0);
442 }
443
444
445 /*
446  * write a codec data (24bit)
447  */
448 static void vxp_write_codec_reg(vx_core_t *chip, int codec, unsigned int data)
449 {
450         int i;
451
452         /* Activate access to the corresponding codec register */
453         if (! codec)
454                 vx_inb(chip, LOFREQ);
455         else
456                 vx_inb(chip, CODEC2);
457                 
458         /* We have to send 24 bits (3 x 8 bits). Start with most signif. Bit */
459         for (i = 0; i < 24; i++, data <<= 1)
460                 vx_outb(chip, DATA, ((data & 0x800000) ? VX_DATA_CODEC_MASK : 0));
461         
462         /* Terminate access to codec registers */
463         vx_inb(chip, HIFREQ);
464 }
465
466
467 /*
468  * vx_set_mic_boost - set mic boost level (on vxp440 only)
469  * @boost: 0 = 20dB, 1 = +38dB
470  */
471 void vx_set_mic_boost(vx_core_t *chip, int boost)
472 {
473         struct snd_vxpocket *pchip = (struct snd_vxpocket *)chip;
474         unsigned long flags;
475
476         if (chip->chip_status & VX_STAT_IS_STALE)
477                 return;
478
479         spin_lock_irqsave(&chip->lock, flags);
480         if (pchip->regCDSP & P24_CDSP_MICS_SEL_MASK) {
481                 if (boost) {
482                         /* boost: 38 dB */
483                         pchip->regCDSP &= ~P24_CDSP_MIC20_SEL_MASK;
484                         pchip->regCDSP |=  P24_CDSP_MIC38_SEL_MASK;
485                 } else {
486                         /* minimum value: 20 dB */
487                         pchip->regCDSP |=  P24_CDSP_MIC20_SEL_MASK;
488                         pchip->regCDSP &= ~P24_CDSP_MIC38_SEL_MASK;
489                 }
490                 vx_outb(chip, CDSP, pchip->regCDSP);
491         }
492         spin_unlock_irqrestore(&chip->lock, flags);
493 }
494
495 /*
496  * remap the linear value (0-8) to the actual value (0-15)
497  */
498 static int vx_compute_mic_level(int level)
499 {
500         switch (level) {
501         case 5: level = 6 ; break;
502         case 6: level = 8 ; break;
503         case 7: level = 11; break;
504         case 8: level = 15; break;
505         default: break ;
506         }
507         return level;
508 }
509
510 /*
511  * vx_set_mic_level - set mic level (on vxpocket only)
512  * @level: the mic level = 0 - 8 (max)
513  */
514 void vx_set_mic_level(vx_core_t *chip, int level)
515 {
516         struct snd_vxpocket *pchip = (struct snd_vxpocket *)chip;
517         unsigned long flags;
518
519         if (chip->chip_status & VX_STAT_IS_STALE)
520                 return;
521
522         spin_lock_irqsave(&chip->lock, flags);
523         if (pchip->regCDSP & VXP_CDSP_MIC_SEL_MASK) {
524                 level = vx_compute_mic_level(level);
525                 vx_outb(chip, MICRO, level);
526         }
527         spin_unlock_irqrestore(&chip->lock, flags);
528 }
529
530
531 /*
532  * change the input audio source
533  */
534 static void vxp_change_audio_source(vx_core_t *_chip, int src)
535 {
536         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
537
538         switch (src) {
539         case VX_AUDIO_SRC_DIGITAL:
540                 chip->regCDSP |= VXP_CDSP_DATAIN_SEL_MASK;
541                 vx_outb(chip, CDSP, chip->regCDSP);
542                 break;
543         case VX_AUDIO_SRC_LINE:
544                 chip->regCDSP &= ~VXP_CDSP_DATAIN_SEL_MASK;
545                 if (_chip->type == VX_TYPE_VXP440)
546                         chip->regCDSP &= ~P24_CDSP_MICS_SEL_MASK;
547                 else
548                         chip->regCDSP &= ~VXP_CDSP_MIC_SEL_MASK;
549                 vx_outb(chip, CDSP, chip->regCDSP);
550                 break;
551         case VX_AUDIO_SRC_MIC:
552                 chip->regCDSP &= ~VXP_CDSP_DATAIN_SEL_MASK;
553                 /* reset mic levels */
554                 if (_chip->type == VX_TYPE_VXP440) {
555                         chip->regCDSP &= ~P24_CDSP_MICS_SEL_MASK;
556                         if (chip->mic_level)
557                                 chip->regCDSP |=  P24_CDSP_MIC38_SEL_MASK;
558                         else
559                                 chip->regCDSP |= P24_CDSP_MIC20_SEL_MASK;
560                         vx_outb(chip, CDSP, chip->regCDSP);
561                 } else {
562                         chip->regCDSP |= VXP_CDSP_MIC_SEL_MASK;
563                         vx_outb(chip, CDSP, chip->regCDSP);
564                         vx_outb(chip, MICRO, vx_compute_mic_level(chip->mic_level));
565                 }
566                 break;
567         }
568 }
569
570 /*
571  * change the clock source
572  * source = INTERNAL_QUARTZ or UER_SYNC
573  */
574 static void vxp_set_clock_source(vx_core_t *_chip, int source)
575 {
576         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
577
578         if (source == INTERNAL_QUARTZ)
579                 chip->regCDSP &= ~VXP_CDSP_CLOCKIN_SEL_MASK;
580         else
581                 chip->regCDSP |= VXP_CDSP_CLOCKIN_SEL_MASK;
582         vx_outb(chip, CDSP, chip->regCDSP);
583 }
584
585
586 /*
587  * reset the board
588  */
589 static void vxp_reset_board(vx_core_t *_chip, int cold_reset)
590 {
591         struct snd_vxpocket *chip = (struct snd_vxpocket *)_chip;
592
593         chip->regCDSP = 0;
594         chip->regDIALOG = 0;
595 }
596
597
598 /*
599  * callbacks
600  */
601 /* exported */
602 struct snd_vx_ops snd_vxpocket_ops = {
603         .in8 = vxp_inb,
604         .out8 = vxp_outb,
605         .test_and_ack = vxp_test_and_ack,
606         .validate_irq = vxp_validate_irq,
607         .write_codec = vxp_write_codec_reg,
608         .reset_codec = vxp_reset_codec,
609         .change_audio_source = vxp_change_audio_source,
610         .set_clock_source = vxp_set_clock_source,
611         .load_dsp = vxp_load_dsp,
612         .add_controls = vxp_add_mic_controls,
613         .reset_dsp = vxp_reset_dsp,
614         .reset_board = vxp_reset_board,
615         .dma_write = vxp_dma_write,
616         .dma_read = vxp_dma_read,
617 };