staging: ozwpan: Add module parameter description
[linux/fpc-iii.git] / sound / oss / waveartist.c
blobb36ea47527e87f567cd5c97ee7b9d3297676f092
1 /*
2 * linux/sound/oss/waveartist.c
4 * The low level driver for the RWA010 Rockwell Wave Artist
5 * codec chip used in the Rebel.com NetWinder.
7 * Cleaned up and integrated into 2.1 by Russell King (rmk@arm.linux.org.uk)
8 * and Pat Beirne (patb@corel.ca)
11 * Copyright (C) by Rebel.com 1998-1999
13 * RWA010 specs received under NDA from Rockwell
15 * Copyright (C) by Hannu Savolainen 1993-1997
17 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
18 * Version 2 (June 1991). See the "COPYING" file distributed with this software
19 * for more info.
21 * Changes:
22 * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
23 * Added __init to waveartist_init()
26 /* Debugging */
27 #define DEBUG_CMD 1
28 #define DEBUG_OUT 2
29 #define DEBUG_IN 4
30 #define DEBUG_INTR 8
31 #define DEBUG_MIXER 16
32 #define DEBUG_TRIGGER 32
34 #define debug_flg (0)
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/delay.h>
42 #include <linux/spinlock.h>
43 #include <linux/bitops.h>
46 #include "sound_config.h"
47 #include "waveartist.h"
49 #ifdef CONFIG_ARM
50 #include <mach/hardware.h>
51 #include <asm/mach-types.h>
52 #endif
54 #ifndef NO_DMA
55 #define NO_DMA 255
56 #endif
58 #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH |\
59 SOUND_MASK_PCM |\
60 SOUND_MASK_LINE |\
61 SOUND_MASK_MIC |\
62 SOUND_MASK_LINE1 |\
63 SOUND_MASK_RECLEV |\
64 SOUND_MASK_VOLUME |\
65 SOUND_MASK_IMIX)
67 static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
68 0x5555, /* Master Volume */
69 0x0000, /* Bass */
70 0x0000, /* Treble */
71 0x2323, /* Synth (FM) */
72 0x4b4b, /* PCM */
73 0x6464, /* PC Speaker */
74 0x0000, /* Ext Line */
75 0x0000, /* Mic */
76 0x0000, /* CD */
77 0x6464, /* Recording monitor */
78 0x0000, /* SB PCM (ALT PCM) */
79 0x0000, /* Recording level */
80 0x6464, /* Input gain */
81 0x6464, /* Output gain */
82 0x0000, /* Line1 (Aux1) */
83 0x0000, /* Line2 (Aux2) */
84 0x0000, /* Line3 (Aux3) */
85 0x0000, /* Digital1 */
86 0x0000, /* Digital2 */
87 0x0000, /* Digital3 */
88 0x0000, /* Phone In */
89 0x6464, /* Phone Out */
90 0x0000, /* Video */
91 0x0000, /* Radio */
92 0x0000 /* Monitor */
95 struct wavnc_info {
96 struct address_info hw; /* hardware */
97 char *chip_name;
99 int xfer_count;
100 int audio_mode;
101 int open_mode;
102 int audio_flags;
103 int record_dev;
104 int playback_dev;
105 int dev_no;
107 /* Mixer parameters */
108 const struct waveartist_mixer_info *mix;
110 unsigned short *levels; /* cache of volume settings */
111 int recmask; /* currently enabled recording device! */
113 #ifdef CONFIG_ARCH_NETWINDER
114 signed int slider_vol; /* hardware slider volume */
115 unsigned int handset_detect :1;
116 unsigned int telephone_detect:1;
117 unsigned int no_autoselect :1;/* handset/telephone autoselects a path */
118 unsigned int spkr_mute_state :1;/* set by ioctl or autoselect */
119 unsigned int line_mute_state :1;/* set by ioctl or autoselect */
120 unsigned int use_slider :1;/* use slider setting for o/p vol */
121 #endif
125 * This is the implementation specific mixer information.
127 struct waveartist_mixer_info {
128 unsigned int supported_devs; /* Supported devices */
129 unsigned int recording_devs; /* Recordable devies */
130 unsigned int stereo_devs; /* Stereo devices */
132 unsigned int (*select_input)(struct wavnc_info *, unsigned int,
133 unsigned char *, unsigned char *);
134 int (*decode_mixer)(struct wavnc_info *, int,
135 unsigned char, unsigned char);
136 int (*get_mixer)(struct wavnc_info *, int);
139 struct wavnc_port_info {
140 int open_mode;
141 int speed;
142 int channels;
143 int audio_format;
146 static int nr_waveartist_devs;
147 static struct wavnc_info adev_info[MAX_AUDIO_DEV];
148 static DEFINE_SPINLOCK(waveartist_lock);
150 #ifndef CONFIG_ARCH_NETWINDER
151 #define machine_is_netwinder() 0
152 #else
153 static struct timer_list vnc_timer;
154 static void vnc_configure_mixer(struct wavnc_info *devc,
155 unsigned int input_mask);
156 static int vnc_private_ioctl(int dev, unsigned int cmd, int __user *arg);
157 static void vnc_slider_tick(unsigned long data);
158 #endif
160 static inline void
161 waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
163 unsigned int ctlr_port = hw->io_base + CTLR;
165 clear = ~clear & inb(ctlr_port);
167 outb(clear | set, ctlr_port);
170 /* Toggle IRQ acknowledge line
172 static inline void
173 waveartist_iack(struct wavnc_info *devc)
175 unsigned int ctlr_port = devc->hw.io_base + CTLR;
176 int old_ctlr;
178 old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
180 outb(old_ctlr | IRQ_ACK, ctlr_port);
181 outb(old_ctlr, ctlr_port);
184 static inline int
185 waveartist_sleep(int timeout_ms)
187 unsigned int timeout = msecs_to_jiffies(timeout_ms*100);
188 return schedule_timeout_interruptible(timeout);
191 static int
192 waveartist_reset(struct wavnc_info *devc)
194 struct address_info *hw = &devc->hw;
195 unsigned int timeout, res = -1;
197 waveartist_set_ctlr(hw, -1, RESET);
198 waveartist_sleep(2);
199 waveartist_set_ctlr(hw, RESET, 0);
201 timeout = 500;
202 do {
203 mdelay(2);
205 if (inb(hw->io_base + STATR) & CMD_RF) {
206 res = inw(hw->io_base + CMDR);
207 if (res == 0x55aa)
208 break;
210 } while (--timeout);
212 if (timeout == 0) {
213 printk(KERN_WARNING "WaveArtist: reset timeout ");
214 if (res != (unsigned int)-1)
215 printk("(res=%04X)", res);
216 printk("\n");
217 return 1;
219 return 0;
222 /* Helper function to send and receive words
223 * from WaveArtist. It handles all the handshaking
224 * and can send or receive multiple words.
226 static int
227 waveartist_cmd(struct wavnc_info *devc,
228 int nr_cmd, unsigned int *cmd,
229 int nr_resp, unsigned int *resp)
231 unsigned int io_base = devc->hw.io_base;
232 unsigned int timed_out = 0;
233 unsigned int i;
235 if (debug_flg & DEBUG_CMD) {
236 printk("waveartist_cmd: cmd=");
238 for (i = 0; i < nr_cmd; i++)
239 printk("%04X ", cmd[i]);
241 printk("\n");
244 if (inb(io_base + STATR) & CMD_RF) {
245 int old_data;
247 /* flush the port
250 old_data = inw(io_base + CMDR);
252 if (debug_flg & DEBUG_CMD)
253 printk("flushed %04X...", old_data);
255 udelay(10);
258 for (i = 0; !timed_out && i < nr_cmd; i++) {
259 int count;
261 for (count = 5000; count; count--)
262 if (inb(io_base + STATR) & CMD_WE)
263 break;
265 if (!count)
266 timed_out = 1;
267 else
268 outw(cmd[i], io_base + CMDR);
271 for (i = 0; !timed_out && i < nr_resp; i++) {
272 int count;
274 for (count = 5000; count; count--)
275 if (inb(io_base + STATR) & CMD_RF)
276 break;
278 if (!count)
279 timed_out = 1;
280 else
281 resp[i] = inw(io_base + CMDR);
284 if (debug_flg & DEBUG_CMD) {
285 if (!timed_out) {
286 printk("waveartist_cmd: resp=");
288 for (i = 0; i < nr_resp; i++)
289 printk("%04X ", resp[i]);
291 printk("\n");
292 } else
293 printk("waveartist_cmd: timed out\n");
296 return timed_out ? 1 : 0;
300 * Send one command word
302 static inline int
303 waveartist_cmd1(struct wavnc_info *devc, unsigned int cmd)
305 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
309 * Send one command, receive one word
311 static inline unsigned int
312 waveartist_cmd1_r(struct wavnc_info *devc, unsigned int cmd)
314 unsigned int ret;
316 waveartist_cmd(devc, 1, &cmd, 1, &ret);
318 return ret;
322 * Send a double command, receive one
323 * word (and throw it away)
325 static inline int
326 waveartist_cmd2(struct wavnc_info *devc, unsigned int cmd, unsigned int arg)
328 unsigned int vals[2];
330 vals[0] = cmd;
331 vals[1] = arg;
333 return waveartist_cmd(devc, 2, vals, 1, vals);
337 * Send a triple command
339 static inline int
340 waveartist_cmd3(struct wavnc_info *devc, unsigned int cmd,
341 unsigned int arg1, unsigned int arg2)
343 unsigned int vals[3];
345 vals[0] = cmd;
346 vals[1] = arg1;
347 vals[2] = arg2;
349 return waveartist_cmd(devc, 3, vals, 0, NULL);
352 static int
353 waveartist_getrev(struct wavnc_info *devc, char *rev)
355 unsigned int temp[2];
356 unsigned int cmd = WACMD_GETREV;
358 waveartist_cmd(devc, 1, &cmd, 2, temp);
360 rev[0] = temp[0] >> 8;
361 rev[1] = temp[0] & 255;
362 rev[2] = '\0';
364 return temp[0];
367 static void waveartist_halt_output(int dev);
368 static void waveartist_halt_input(int dev);
369 static void waveartist_halt(int dev);
370 static void waveartist_trigger(int dev, int state);
372 static int
373 waveartist_open(int dev, int mode)
375 struct wavnc_info *devc;
376 struct wavnc_port_info *portc;
377 unsigned long flags;
379 if (dev < 0 || dev >= num_audiodevs)
380 return -ENXIO;
382 devc = (struct wavnc_info *) audio_devs[dev]->devc;
383 portc = (struct wavnc_port_info *) audio_devs[dev]->portc;
385 spin_lock_irqsave(&waveartist_lock, flags);
386 if (portc->open_mode || (devc->open_mode & mode)) {
387 spin_unlock_irqrestore(&waveartist_lock, flags);
388 return -EBUSY;
391 devc->audio_mode = 0;
392 devc->open_mode |= mode;
393 portc->open_mode = mode;
394 waveartist_trigger(dev, 0);
396 if (mode & OPEN_READ)
397 devc->record_dev = dev;
398 if (mode & OPEN_WRITE)
399 devc->playback_dev = dev;
400 spin_unlock_irqrestore(&waveartist_lock, flags);
402 return 0;
405 static void
406 waveartist_close(int dev)
408 struct wavnc_info *devc = (struct wavnc_info *)
409 audio_devs[dev]->devc;
410 struct wavnc_port_info *portc = (struct wavnc_port_info *)
411 audio_devs[dev]->portc;
412 unsigned long flags;
414 spin_lock_irqsave(&waveartist_lock, flags);
416 waveartist_halt(dev);
418 devc->audio_mode = 0;
419 devc->open_mode &= ~portc->open_mode;
420 portc->open_mode = 0;
422 spin_unlock_irqrestore(&waveartist_lock, flags);
425 static void
426 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
428 struct wavnc_port_info *portc = (struct wavnc_port_info *)
429 audio_devs[dev]->portc;
430 struct wavnc_info *devc = (struct wavnc_info *)
431 audio_devs[dev]->devc;
432 unsigned long flags;
433 unsigned int count = __count;
435 if (debug_flg & DEBUG_OUT)
436 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
437 buf, count);
439 * 16 bit data
441 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
442 count >>= 1;
444 if (portc->channels > 1)
445 count >>= 1;
447 count -= 1;
449 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
450 audio_devs[dev]->flags & DMA_AUTOMODE &&
451 intrflag &&
452 count == devc->xfer_count) {
453 devc->audio_mode |= PCM_ENABLE_OUTPUT;
454 return; /*
455 * Auto DMA mode on. No need to react
459 spin_lock_irqsave(&waveartist_lock, flags);
462 * set sample count
464 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
466 devc->xfer_count = count;
467 devc->audio_mode |= PCM_ENABLE_OUTPUT;
469 spin_unlock_irqrestore(&waveartist_lock, flags);
472 static void
473 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
475 struct wavnc_port_info *portc = (struct wavnc_port_info *)
476 audio_devs[dev]->portc;
477 struct wavnc_info *devc = (struct wavnc_info *)
478 audio_devs[dev]->devc;
479 unsigned long flags;
480 unsigned int count = __count;
482 if (debug_flg & DEBUG_IN)
483 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
484 buf, count);
486 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
487 count >>= 1;
489 if (portc->channels > 1)
490 count >>= 1;
492 count -= 1;
494 if (devc->audio_mode & PCM_ENABLE_INPUT &&
495 audio_devs[dev]->flags & DMA_AUTOMODE &&
496 intrflag &&
497 count == devc->xfer_count) {
498 devc->audio_mode |= PCM_ENABLE_INPUT;
499 return; /*
500 * Auto DMA mode on. No need to react
504 spin_lock_irqsave(&waveartist_lock, flags);
507 * set sample count
509 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
511 devc->xfer_count = count;
512 devc->audio_mode |= PCM_ENABLE_INPUT;
514 spin_unlock_irqrestore(&waveartist_lock, flags);
517 static int
518 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
520 return -EINVAL;
523 static unsigned int
524 waveartist_get_speed(struct wavnc_port_info *portc)
526 unsigned int speed;
529 * program the speed, channels, bits
531 if (portc->speed == 8000)
532 speed = 0x2E71;
533 else if (portc->speed == 11025)
534 speed = 0x4000;
535 else if (portc->speed == 22050)
536 speed = 0x8000;
537 else if (portc->speed == 44100)
538 speed = 0x0;
539 else {
541 * non-standard - just calculate
543 speed = portc->speed << 16;
545 speed = (speed / 44100) & 65535;
548 return speed;
551 static unsigned int
552 waveartist_get_bits(struct wavnc_port_info *portc)
554 unsigned int bits;
556 if (portc->audio_format == AFMT_S16_LE)
557 bits = 1;
558 else if (portc->audio_format == AFMT_S8)
559 bits = 0;
560 else
561 bits = 2; //default AFMT_U8
563 return bits;
566 static int
567 waveartist_prepare_for_input(int dev, int bsize, int bcount)
569 unsigned long flags;
570 struct wavnc_info *devc = (struct wavnc_info *)
571 audio_devs[dev]->devc;
572 struct wavnc_port_info *portc = (struct wavnc_port_info *)
573 audio_devs[dev]->portc;
574 unsigned int speed, bits;
576 if (devc->audio_mode)
577 return 0;
579 speed = waveartist_get_speed(portc);
580 bits = waveartist_get_bits(portc);
582 spin_lock_irqsave(&waveartist_lock, flags);
584 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
585 printk(KERN_WARNING "waveartist: error setting the "
586 "record format to %d\n", portc->audio_format);
588 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
589 printk(KERN_WARNING "waveartist: error setting record "
590 "to %d channels\n", portc->channels);
593 * write cmd SetSampleSpeedTimeConstant
595 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
596 printk(KERN_WARNING "waveartist: error setting the record "
597 "speed to %dHz.\n", portc->speed);
599 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
600 printk(KERN_WARNING "waveartist: error setting the record "
601 "data path to 0x%X\n", 1);
603 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
604 printk(KERN_WARNING "waveartist: error setting the record "
605 "format to %d\n", portc->audio_format);
607 devc->xfer_count = 0;
608 spin_unlock_irqrestore(&waveartist_lock, flags);
609 waveartist_halt_input(dev);
611 if (debug_flg & DEBUG_INTR) {
612 printk("WA CTLR reg: 0x%02X.\n",
613 inb(devc->hw.io_base + CTLR));
614 printk("WA STAT reg: 0x%02X.\n",
615 inb(devc->hw.io_base + STATR));
616 printk("WA IRQS reg: 0x%02X.\n",
617 inb(devc->hw.io_base + IRQSTAT));
620 return 0;
623 static int
624 waveartist_prepare_for_output(int dev, int bsize, int bcount)
626 unsigned long flags;
627 struct wavnc_info *devc = (struct wavnc_info *)
628 audio_devs[dev]->devc;
629 struct wavnc_port_info *portc = (struct wavnc_port_info *)
630 audio_devs[dev]->portc;
631 unsigned int speed, bits;
634 * program the speed, channels, bits
636 speed = waveartist_get_speed(portc);
637 bits = waveartist_get_bits(portc);
639 spin_lock_irqsave(&waveartist_lock, flags);
641 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
642 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
643 printk(KERN_WARNING "waveartist: error setting the playback "
644 "speed to %dHz.\n", portc->speed);
646 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
647 printk(KERN_WARNING "waveartist: error setting the playback "
648 "to %d channels\n", portc->channels);
650 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
651 printk(KERN_WARNING "waveartist: error setting the playback "
652 "data path to 0x%X\n", 0);
654 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
655 printk(KERN_WARNING "waveartist: error setting the playback "
656 "format to %d\n", portc->audio_format);
658 devc->xfer_count = 0;
659 spin_unlock_irqrestore(&waveartist_lock, flags);
660 waveartist_halt_output(dev);
662 if (debug_flg & DEBUG_INTR) {
663 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
664 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
665 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
668 return 0;
671 static void
672 waveartist_halt(int dev)
674 struct wavnc_port_info *portc = (struct wavnc_port_info *)
675 audio_devs[dev]->portc;
676 struct wavnc_info *devc;
678 if (portc->open_mode & OPEN_WRITE)
679 waveartist_halt_output(dev);
681 if (portc->open_mode & OPEN_READ)
682 waveartist_halt_input(dev);
684 devc = (struct wavnc_info *) audio_devs[dev]->devc;
685 devc->audio_mode = 0;
688 static void
689 waveartist_halt_input(int dev)
691 struct wavnc_info *devc = (struct wavnc_info *)
692 audio_devs[dev]->devc;
693 unsigned long flags;
695 spin_lock_irqsave(&waveartist_lock, flags);
698 * Stop capture
700 waveartist_cmd1(devc, WACMD_INPUTSTOP);
702 devc->audio_mode &= ~PCM_ENABLE_INPUT;
705 * Clear interrupt by toggling
706 * the IRQ_ACK bit in CTRL
708 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
709 waveartist_iack(devc);
711 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
713 spin_unlock_irqrestore(&waveartist_lock, flags);
716 static void
717 waveartist_halt_output(int dev)
719 struct wavnc_info *devc = (struct wavnc_info *)
720 audio_devs[dev]->devc;
721 unsigned long flags;
723 spin_lock_irqsave(&waveartist_lock, flags);
725 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
727 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
730 * Clear interrupt by toggling
731 * the IRQ_ACK bit in CTRL
733 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
734 waveartist_iack(devc);
736 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
738 spin_unlock_irqrestore(&waveartist_lock, flags);
741 static void
742 waveartist_trigger(int dev, int state)
744 struct wavnc_info *devc = (struct wavnc_info *)
745 audio_devs[dev]->devc;
746 struct wavnc_port_info *portc = (struct wavnc_port_info *)
747 audio_devs[dev]->portc;
748 unsigned long flags;
750 if (debug_flg & DEBUG_TRIGGER) {
751 printk("wavnc: audio trigger ");
752 if (state & PCM_ENABLE_INPUT)
753 printk("in ");
754 if (state & PCM_ENABLE_OUTPUT)
755 printk("out");
756 printk("\n");
759 spin_lock_irqsave(&waveartist_lock, flags);
761 state &= devc->audio_mode;
763 if (portc->open_mode & OPEN_READ &&
764 state & PCM_ENABLE_INPUT)
766 * enable ADC Data Transfer to PC
768 waveartist_cmd1(devc, WACMD_INPUTSTART);
770 if (portc->open_mode & OPEN_WRITE &&
771 state & PCM_ENABLE_OUTPUT)
773 * enable DAC data transfer from PC
775 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
777 spin_unlock_irqrestore(&waveartist_lock, flags);
780 static int
781 waveartist_set_speed(int dev, int arg)
783 struct wavnc_port_info *portc = (struct wavnc_port_info *)
784 audio_devs[dev]->portc;
786 if (arg <= 0)
787 return portc->speed;
789 if (arg < 5000)
790 arg = 5000;
791 if (arg > 44100)
792 arg = 44100;
794 portc->speed = arg;
795 return portc->speed;
799 static short
800 waveartist_set_channels(int dev, short arg)
802 struct wavnc_port_info *portc = (struct wavnc_port_info *)
803 audio_devs[dev]->portc;
805 if (arg != 1 && arg != 2)
806 return portc->channels;
808 portc->channels = arg;
809 return arg;
812 static unsigned int
813 waveartist_set_bits(int dev, unsigned int arg)
815 struct wavnc_port_info *portc = (struct wavnc_port_info *)
816 audio_devs[dev]->portc;
818 if (arg == 0)
819 return portc->audio_format;
821 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
822 arg = AFMT_U8;
824 portc->audio_format = arg;
826 return arg;
829 static struct audio_driver waveartist_audio_driver = {
830 .owner = THIS_MODULE,
831 .open = waveartist_open,
832 .close = waveartist_close,
833 .output_block = waveartist_output_block,
834 .start_input = waveartist_start_input,
835 .ioctl = waveartist_ioctl,
836 .prepare_for_input = waveartist_prepare_for_input,
837 .prepare_for_output = waveartist_prepare_for_output,
838 .halt_io = waveartist_halt,
839 .halt_input = waveartist_halt_input,
840 .halt_output = waveartist_halt_output,
841 .trigger = waveartist_trigger,
842 .set_speed = waveartist_set_speed,
843 .set_bits = waveartist_set_bits,
844 .set_channels = waveartist_set_channels
848 static irqreturn_t
849 waveartist_intr(int irq, void *dev_id)
851 struct wavnc_info *devc = dev_id;
852 int irqstatus, status;
854 spin_lock(&waveartist_lock);
855 irqstatus = inb(devc->hw.io_base + IRQSTAT);
856 status = inb(devc->hw.io_base + STATR);
858 if (debug_flg & DEBUG_INTR)
859 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
860 status, irqstatus);
862 if (status & IRQ_REQ) /* Clear interrupt */
863 waveartist_iack(devc);
864 else
865 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
867 if (irqstatus & 0x01) {
868 int temp = 1;
870 /* PCM buffer done
872 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
873 DMAbuf_outputintr(devc->playback_dev, 1);
874 temp = 0;
876 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
877 DMAbuf_inputintr(devc->record_dev);
878 temp = 0;
880 if (temp) //default:
881 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
883 if (irqstatus & 0x2)
884 // We do not use SB mode natively...
885 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
886 spin_unlock(&waveartist_lock);
887 return IRQ_HANDLED;
890 /* -------------------------------------------------------------------------
891 * Mixer stuff
893 struct mix_ent {
894 unsigned char reg_l;
895 unsigned char reg_r;
896 unsigned char shift;
897 unsigned char max;
900 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
901 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
902 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
903 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
904 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
905 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
906 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
907 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
908 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
909 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
912 #if 0
913 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
914 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
915 #else
916 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
917 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
918 #endif
919 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
920 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
921 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
922 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
923 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
924 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
925 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
926 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
927 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
928 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
929 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
930 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
933 static void
934 waveartist_mixer_update(struct wavnc_info *devc, int whichDev)
936 unsigned int lev_left, lev_right;
938 lev_left = devc->levels[whichDev] & 0xff;
939 lev_right = devc->levels[whichDev] >> 8;
941 if (lev_left > 100)
942 lev_left = 100;
943 if (lev_right > 100)
944 lev_right = 100;
946 #define SCALE(lev,max) ((lev) * (max) / 100)
948 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
949 whichDev = SOUND_MIXER_VOLUME;
951 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
952 const struct mix_ent *mix = mix_devs + whichDev;
953 unsigned int mask, left, right;
955 mask = mix->max << mix->shift;
956 lev_left = SCALE(lev_left, mix->max) << mix->shift;
957 lev_right = SCALE(lev_right, mix->max) << mix->shift;
959 /* read left setting */
960 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
961 mix->reg_l << 8);
963 /* read right setting */
964 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
965 mix->reg_r << 8);
967 left = (left & ~mask) | (lev_left & mask);
968 right = (right & ~mask) | (lev_right & mask);
970 /* write left,right back */
971 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
972 } else {
973 switch(whichDev) {
974 case SOUND_MIXER_PCM:
975 waveartist_cmd3(devc, WACMD_SET_LEVEL,
976 SCALE(lev_left, 32767),
977 SCALE(lev_right, 32767));
978 break;
980 case SOUND_MIXER_SYNTH:
981 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
982 SCALE(lev_left, 32767),
983 SCALE(lev_right, 32767));
984 break;
990 * Set the ADC MUX to the specified values. We do NOT do any
991 * checking of the values passed, since we assume that the
992 * relevant *_select_input function has done that for us.
994 static void
995 waveartist_set_adc_mux(struct wavnc_info *devc, char left_dev,
996 char right_dev)
998 unsigned int reg_08, reg_09;
1000 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
1001 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
1003 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
1005 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
1009 * Decode a recording mask into a mixer selection as follows:
1011 * OSS Source WA Source Actual source
1012 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1013 * SOUND_MASK_LINE Line Line in
1014 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
1015 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
1016 * SOUND_MASK_MIC Mic Microphone
1018 static unsigned int
1019 waveartist_select_input(struct wavnc_info *devc, unsigned int recmask,
1020 unsigned char *dev_l, unsigned char *dev_r)
1022 unsigned int recdev = ADC_MUX_NONE;
1024 if (recmask & SOUND_MASK_IMIX) {
1025 recmask = SOUND_MASK_IMIX;
1026 recdev = ADC_MUX_MIXER;
1027 } else if (recmask & SOUND_MASK_LINE2) {
1028 recmask = SOUND_MASK_LINE2;
1029 recdev = ADC_MUX_AUX2;
1030 } else if (recmask & SOUND_MASK_LINE1) {
1031 recmask = SOUND_MASK_LINE1;
1032 recdev = ADC_MUX_AUX1;
1033 } else if (recmask & SOUND_MASK_LINE) {
1034 recmask = SOUND_MASK_LINE;
1035 recdev = ADC_MUX_LINE;
1036 } else if (recmask & SOUND_MASK_MIC) {
1037 recmask = SOUND_MASK_MIC;
1038 recdev = ADC_MUX_MIC;
1041 *dev_l = *dev_r = recdev;
1043 return recmask;
1046 static int
1047 waveartist_decode_mixer(struct wavnc_info *devc, int dev,
1048 unsigned char lev_l,
1049 unsigned char lev_r)
1051 switch (dev) {
1052 case SOUND_MIXER_VOLUME:
1053 case SOUND_MIXER_SYNTH:
1054 case SOUND_MIXER_PCM:
1055 case SOUND_MIXER_LINE:
1056 case SOUND_MIXER_MIC:
1057 case SOUND_MIXER_IGAIN:
1058 case SOUND_MIXER_LINE1:
1059 case SOUND_MIXER_LINE2:
1060 devc->levels[dev] = lev_l | lev_r << 8;
1061 break;
1063 case SOUND_MIXER_IMIX:
1064 break;
1066 default:
1067 dev = -EINVAL;
1068 break;
1071 return dev;
1074 static int waveartist_get_mixer(struct wavnc_info *devc, int dev)
1076 return devc->levels[dev];
1079 static const struct waveartist_mixer_info waveartist_mixer = {
1080 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1081 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1082 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1083 SOUND_MASK_IMIX,
1084 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1085 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1086 .select_input = waveartist_select_input,
1087 .decode_mixer = waveartist_decode_mixer,
1088 .get_mixer = waveartist_get_mixer,
1091 static void
1092 waveartist_set_recmask(struct wavnc_info *devc, unsigned int recmask)
1094 unsigned char dev_l, dev_r;
1096 recmask &= devc->mix->recording_devs;
1099 * If more than one recording device selected,
1100 * disable the device that is currently in use.
1102 if (hweight32(recmask) > 1)
1103 recmask &= ~devc->recmask;
1106 * Translate the recording device mask into
1107 * the ADC multiplexer settings.
1109 devc->recmask = devc->mix->select_input(devc, recmask,
1110 &dev_l, &dev_r);
1112 waveartist_set_adc_mux(devc, dev_l, dev_r);
1115 static int
1116 waveartist_set_mixer(struct wavnc_info *devc, int dev, unsigned int level)
1118 unsigned int lev_left = level & 0x00ff;
1119 unsigned int lev_right = (level & 0xff00) >> 8;
1121 if (lev_left > 100)
1122 lev_left = 100;
1123 if (lev_right > 100)
1124 lev_right = 100;
1127 * Mono devices have their right volume forced to their
1128 * left volume. (from ALSA driver OSS emulation).
1130 if (!(devc->mix->stereo_devs & (1 << dev)))
1131 lev_right = lev_left;
1133 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1135 if (dev >= 0)
1136 waveartist_mixer_update(devc, dev);
1138 return dev < 0 ? dev : 0;
1141 static int
1142 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1144 struct wavnc_info *devc = (struct wavnc_info *)audio_devs[dev]->devc;
1145 int ret = 0, val, nr;
1148 * All SOUND_MIXER_* ioctls use type 'M'
1150 if (((cmd >> 8) & 255) != 'M')
1151 return -ENOIOCTLCMD;
1153 #ifdef CONFIG_ARCH_NETWINDER
1154 if (machine_is_netwinder()) {
1155 ret = vnc_private_ioctl(dev, cmd, arg);
1156 if (ret != -ENOIOCTLCMD)
1157 return ret;
1158 else
1159 ret = 0;
1161 #endif
1163 nr = cmd & 0xff;
1165 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1166 if (get_user(val, (int __user *)arg))
1167 return -EFAULT;
1169 switch (nr) {
1170 case SOUND_MIXER_RECSRC:
1171 waveartist_set_recmask(devc, val);
1172 break;
1174 default:
1175 ret = -EINVAL;
1176 if (nr < SOUND_MIXER_NRDEVICES &&
1177 devc->mix->supported_devs & (1 << nr))
1178 ret = waveartist_set_mixer(devc, nr, val);
1182 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1183 ret = -EINVAL;
1185 switch (nr) {
1186 case SOUND_MIXER_RECSRC:
1187 ret = devc->recmask;
1188 break;
1190 case SOUND_MIXER_DEVMASK:
1191 ret = devc->mix->supported_devs;
1192 break;
1194 case SOUND_MIXER_STEREODEVS:
1195 ret = devc->mix->stereo_devs;
1196 break;
1198 case SOUND_MIXER_RECMASK:
1199 ret = devc->mix->recording_devs;
1200 break;
1202 case SOUND_MIXER_CAPS:
1203 ret = SOUND_CAP_EXCL_INPUT;
1204 break;
1206 default:
1207 if (nr < SOUND_MIXER_NRDEVICES)
1208 ret = devc->mix->get_mixer(devc, nr);
1209 break;
1212 if (ret >= 0)
1213 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1216 return ret;
1219 static struct mixer_operations waveartist_mixer_operations =
1221 .owner = THIS_MODULE,
1222 .id = "WaveArtist",
1223 .name = "WaveArtist",
1224 .ioctl = waveartist_mixer_ioctl
1227 static void
1228 waveartist_mixer_reset(struct wavnc_info *devc)
1230 int i;
1232 if (debug_flg & DEBUG_MIXER)
1233 printk("%s: mixer_reset\n", devc->hw.name);
1236 * reset mixer cmd
1238 waveartist_cmd1(devc, WACMD_RST_MIXER);
1241 * set input for ADC to come from 'quiet'
1242 * turn on default modes
1244 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1247 * set mixer input select to none, RX filter gains 0 dB
1249 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1252 * set bit 0 reg 2 to 1 - unmute MonoOut
1254 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1256 /* set default input device = internal mic
1257 * current recording device = none
1259 waveartist_set_recmask(devc, 0);
1261 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1262 waveartist_mixer_update(devc, i);
1265 static int __init waveartist_init(struct wavnc_info *devc)
1267 struct wavnc_port_info *portc;
1268 char rev[3], dev_name[64];
1269 int my_dev;
1271 if (waveartist_reset(devc))
1272 return -ENODEV;
1274 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1276 if (waveartist_getrev(devc, rev)) {
1277 strcat(dev_name, " rev. ");
1278 strcat(dev_name, rev);
1280 strcat(dev_name, ")");
1282 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1283 devc->hw.dma, devc->hw.dma2);
1285 portc = kzalloc(sizeof(struct wavnc_port_info), GFP_KERNEL);
1286 if (portc == NULL)
1287 goto nomem;
1289 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1290 &waveartist_audio_driver, sizeof(struct audio_driver),
1291 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1292 devc, devc->hw.dma, devc->hw.dma2);
1294 if (my_dev < 0)
1295 goto free;
1297 audio_devs[my_dev]->portc = portc;
1299 waveartist_mixer_reset(devc);
1302 * clear any pending interrupt
1304 waveartist_iack(devc);
1306 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1307 printk(KERN_ERR "%s: IRQ %d in use\n",
1308 devc->hw.name, devc->hw.irq);
1309 goto uninstall;
1312 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1313 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1314 devc->hw.name, devc->hw.dma);
1315 goto uninstall_irq;
1318 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1319 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1320 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1321 devc->hw.name, devc->hw.dma2);
1322 goto uninstall_dma;
1325 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1327 audio_devs[my_dev]->mixer_dev =
1328 sound_install_mixer(MIXER_DRIVER_VERSION,
1329 dev_name,
1330 &waveartist_mixer_operations,
1331 sizeof(struct mixer_operations),
1332 devc);
1334 return my_dev;
1336 uninstall_dma:
1337 sound_free_dma(devc->hw.dma);
1339 uninstall_irq:
1340 free_irq(devc->hw.irq, devc);
1342 uninstall:
1343 sound_unload_audiodev(my_dev);
1345 free:
1346 kfree(portc);
1348 nomem:
1349 return -1;
1352 static int __init probe_waveartist(struct address_info *hw_config)
1354 struct wavnc_info *devc = &adev_info[nr_waveartist_devs];
1356 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1357 printk(KERN_WARNING "waveartist: too many audio devices\n");
1358 return 0;
1361 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1362 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1363 return 0;
1366 if (hw_config->irq > 15 || hw_config->irq < 0) {
1367 release_region(hw_config->io_base, 15);
1368 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1369 hw_config->irq);
1370 return 0;
1373 if (hw_config->dma != 3) {
1374 release_region(hw_config->io_base, 15);
1375 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1376 hw_config->dma);
1377 return 0;
1380 hw_config->name = "WaveArtist";
1381 devc->hw = *hw_config;
1382 devc->open_mode = 0;
1383 devc->chip_name = "RWA-010";
1385 return 1;
1388 static void __init
1389 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1391 struct wavnc_info *devc = &adev_info[nr_waveartist_devs];
1394 * NOTE! If irq < 0, there is another driver which has allocated the
1395 * IRQ so that this driver doesn't need to allocate/deallocate it.
1396 * The actually used IRQ is ABS(irq).
1398 devc->hw = *hw;
1399 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1400 devc->open_mode = 0;
1401 devc->playback_dev = 0;
1402 devc->record_dev = 0;
1403 devc->audio_flags = DMA_AUTOMODE;
1404 devc->levels = levels;
1406 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1407 devc->audio_flags |= DMA_DUPLEX;
1409 devc->mix = mix;
1410 devc->dev_no = waveartist_init(devc);
1412 if (devc->dev_no < 0)
1413 release_region(hw->io_base, 15);
1414 else {
1415 #ifdef CONFIG_ARCH_NETWINDER
1416 if (machine_is_netwinder()) {
1417 init_timer(&vnc_timer);
1418 vnc_timer.function = vnc_slider_tick;
1419 vnc_timer.expires = jiffies;
1420 vnc_timer.data = nr_waveartist_devs;
1421 add_timer(&vnc_timer);
1423 vnc_configure_mixer(devc, 0);
1425 devc->no_autoselect = 1;
1427 #endif
1428 nr_waveartist_devs += 1;
1432 static void __exit unload_waveartist(struct address_info *hw)
1434 struct wavnc_info *devc = NULL;
1435 int i;
1437 for (i = 0; i < nr_waveartist_devs; i++)
1438 if (hw->io_base == adev_info[i].hw.io_base) {
1439 devc = adev_info + i;
1440 break;
1443 if (devc != NULL) {
1444 int mixer;
1446 #ifdef CONFIG_ARCH_NETWINDER
1447 if (machine_is_netwinder())
1448 del_timer(&vnc_timer);
1449 #endif
1451 release_region(devc->hw.io_base, 15);
1453 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1455 if (devc->hw.irq >= 0)
1456 free_irq(devc->hw.irq, devc);
1458 sound_free_dma(devc->hw.dma);
1460 if (devc->hw.dma != devc->hw.dma2 &&
1461 devc->hw.dma2 != NO_DMA)
1462 sound_free_dma(devc->hw.dma2);
1464 mixer = audio_devs[devc->dev_no]->mixer_dev;
1466 if (mixer >= 0)
1467 sound_unload_mixerdev(mixer);
1469 if (devc->dev_no >= 0)
1470 sound_unload_audiodev(devc->dev_no);
1472 nr_waveartist_devs -= 1;
1474 for (; i < nr_waveartist_devs; i++)
1475 adev_info[i] = adev_info[i + 1];
1476 } else
1477 printk(KERN_WARNING "waveartist: can't find device "
1478 "to unload\n");
1481 #ifdef CONFIG_ARCH_NETWINDER
1484 * Rebel.com Netwinder specifics...
1487 #include <asm/hardware/dec21285.h>
1489 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1491 #define MIXER_PRIVATE3_RESET 0x53570000
1492 #define MIXER_PRIVATE3_READ 0x53570001
1493 #define MIXER_PRIVATE3_WRITE 0x53570002
1495 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1496 #define VNC_MUTE_LINE_OUT 0x10
1497 #define VNC_PHONE_DETECT 0x20
1498 #define VNC_HANDSET_DETECT 0x40
1499 #define VNC_DISABLE_AUTOSWITCH 0x80
1501 static inline void
1502 vnc_mute_spkr(struct wavnc_info *devc)
1504 unsigned long flags;
1506 raw_spin_lock_irqsave(&nw_gpio_lock, flags);
1507 nw_cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1508 raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
1511 static void
1512 vnc_mute_lout(struct wavnc_info *devc)
1514 unsigned int left, right;
1516 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1517 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1519 if (devc->line_mute_state) {
1520 left &= ~1;
1521 right &= ~1;
1522 } else {
1523 left |= 1;
1524 right |= 1;
1526 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1530 static int
1531 vnc_volume_slider(struct wavnc_info *devc)
1533 static signed int old_slider_volume;
1534 unsigned long flags;
1535 signed int volume = 255;
1537 *CSR_TIMER1_LOAD = 0x00ffffff;
1539 spin_lock_irqsave(&waveartist_lock, flags);
1541 outb(0xFF, 0x201);
1542 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1544 while (volume && (inb(0x201) & 0x01))
1545 volume--;
1547 *CSR_TIMER1_CNTL = 0;
1549 spin_unlock_irqrestore(&waveartist_lock,flags);
1551 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1554 #ifndef REVERSE
1555 volume = 150 - (volume >> 5);
1556 #else
1557 volume = (volume >> 6) - 25;
1558 #endif
1560 if (volume < 0)
1561 volume = 0;
1563 if (volume > 100)
1564 volume = 100;
1567 * slider quite often reads +-8, so debounce this random noise
1569 if (abs(volume - old_slider_volume) > 7) {
1570 old_slider_volume = volume;
1572 if (debug_flg & DEBUG_MIXER)
1573 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1576 return old_slider_volume;
1580 * Decode a recording mask into a mixer selection on the NetWinder
1581 * as follows:
1583 * OSS Source WA Source Actual source
1584 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1585 * SOUND_MASK_LINE Line Line in
1586 * SOUND_MASK_LINE1 Left Mic Handset
1587 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1588 * SOUND_MASK_MIC Right Mic Builtin microphone
1590 static unsigned int
1591 netwinder_select_input(struct wavnc_info *devc, unsigned int recmask,
1592 unsigned char *dev_l, unsigned char *dev_r)
1594 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1596 if (recmask & SOUND_MASK_IMIX) {
1597 recmask = SOUND_MASK_IMIX;
1598 recdev_l = ADC_MUX_MIXER;
1599 recdev_r = ADC_MUX_MIXER;
1600 } else if (recmask & SOUND_MASK_LINE) {
1601 recmask = SOUND_MASK_LINE;
1602 recdev_l = ADC_MUX_LINE;
1603 recdev_r = ADC_MUX_LINE;
1604 } else if (recmask & SOUND_MASK_LINE1) {
1605 recmask = SOUND_MASK_LINE1;
1606 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1607 recdev_l = ADC_MUX_MIC;
1608 recdev_r = ADC_MUX_NONE;
1609 } else if (recmask & SOUND_MASK_PHONEIN) {
1610 recmask = SOUND_MASK_PHONEIN;
1611 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1612 recdev_l = ADC_MUX_AUX1;
1613 recdev_r = ADC_MUX_NONE;
1614 } else if (recmask & SOUND_MASK_MIC) {
1615 recmask = SOUND_MASK_MIC;
1616 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1617 recdev_l = ADC_MUX_NONE;
1618 recdev_r = ADC_MUX_MIC;
1621 *dev_l = recdev_l;
1622 *dev_r = recdev_r;
1624 return recmask;
1627 static int
1628 netwinder_decode_mixer(struct wavnc_info *devc, int dev, unsigned char lev_l,
1629 unsigned char lev_r)
1631 switch (dev) {
1632 case SOUND_MIXER_VOLUME:
1633 case SOUND_MIXER_SYNTH:
1634 case SOUND_MIXER_PCM:
1635 case SOUND_MIXER_LINE:
1636 case SOUND_MIXER_IGAIN:
1637 devc->levels[dev] = lev_l | lev_r << 8;
1638 break;
1640 case SOUND_MIXER_MIC: /* right mic only */
1641 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1642 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1643 break;
1645 case SOUND_MIXER_LINE1: /* left mic only */
1646 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1647 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1648 dev = SOUND_MIXER_MIC;
1649 break;
1651 case SOUND_MIXER_PHONEIN: /* left aux only */
1652 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1653 dev = SOUND_MIXER_LINE1;
1654 break;
1656 case SOUND_MIXER_IMIX:
1657 case SOUND_MIXER_PHONEOUT:
1658 break;
1660 default:
1661 dev = -EINVAL;
1662 break;
1664 return dev;
1667 static int netwinder_get_mixer(struct wavnc_info *devc, int dev)
1669 int levels;
1671 switch (dev) {
1672 case SOUND_MIXER_VOLUME:
1673 case SOUND_MIXER_SYNTH:
1674 case SOUND_MIXER_PCM:
1675 case SOUND_MIXER_LINE:
1676 case SOUND_MIXER_IGAIN:
1677 levels = devc->levels[dev];
1678 break;
1680 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1681 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1682 levels |= levels << 8;
1683 break;
1685 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1686 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1687 levels |= levels << 8;
1688 break;
1690 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1691 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1692 levels |= levels << 8;
1693 break;
1695 default:
1696 levels = 0;
1699 return levels;
1703 * Waveartist specific mixer information.
1705 static const struct waveartist_mixer_info netwinder_mixer = {
1706 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1707 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1708 SOUND_MASK_LINE | SOUND_MASK_MIC |
1709 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1710 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1711 SOUND_MASK_IGAIN,
1713 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1714 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1715 SOUND_MASK_PHONEIN,
1717 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1718 SOUND_MASK_PCM | SOUND_MASK_LINE |
1719 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1721 .select_input = netwinder_select_input,
1722 .decode_mixer = netwinder_decode_mixer,
1723 .get_mixer = netwinder_get_mixer,
1726 static void
1727 vnc_configure_mixer(struct wavnc_info *devc, unsigned int recmask)
1729 if (!devc->no_autoselect) {
1730 if (devc->handset_detect) {
1731 recmask = SOUND_MASK_LINE1;
1732 devc->spkr_mute_state = devc->line_mute_state = 1;
1733 } else if (devc->telephone_detect) {
1734 recmask = SOUND_MASK_PHONEIN;
1735 devc->spkr_mute_state = devc->line_mute_state = 1;
1736 } else {
1737 /* unless someone has asked for LINE-IN,
1738 * we default to MIC
1740 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1741 devc->recmask = SOUND_MASK_MIC;
1742 devc->spkr_mute_state = devc->line_mute_state = 0;
1744 vnc_mute_spkr(devc);
1745 vnc_mute_lout(devc);
1747 if (recmask != devc->recmask)
1748 waveartist_set_recmask(devc, recmask);
1752 static int
1753 vnc_slider(struct wavnc_info *devc)
1755 signed int slider_volume;
1756 unsigned int temp, old_hs, old_td;
1759 * read the "buttons" state.
1760 * Bit 4 = 0 means handset present
1761 * Bit 5 = 1 means phone offhook
1763 temp = inb(0x201);
1765 old_hs = devc->handset_detect;
1766 old_td = devc->telephone_detect;
1768 devc->handset_detect = !(temp & 0x10);
1769 devc->telephone_detect = !!(temp & 0x20);
1771 if (!devc->no_autoselect &&
1772 (old_hs != devc->handset_detect ||
1773 old_td != devc->telephone_detect))
1774 vnc_configure_mixer(devc, devc->recmask);
1776 slider_volume = vnc_volume_slider(devc);
1779 * If we're using software controlled volume, and
1780 * the slider moves by more than 20%, then we
1781 * switch back to slider controlled volume.
1783 if (abs(devc->slider_vol - slider_volume) > 20)
1784 devc->use_slider = 1;
1787 * use only left channel
1789 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1791 if (slider_volume != temp && devc->use_slider) {
1792 devc->slider_vol = slider_volume;
1794 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1795 slider_volume | slider_volume << 8);
1797 return 1;
1800 return 0;
1803 static void
1804 vnc_slider_tick(unsigned long data)
1806 int next_timeout;
1808 if (vnc_slider(adev_info + data))
1809 next_timeout = 5; // mixer reported change
1810 else
1811 next_timeout = VNC_TIMER_PERIOD;
1813 mod_timer(&vnc_timer, jiffies + next_timeout);
1816 static int
1817 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1819 struct wavnc_info *devc = (struct wavnc_info *)audio_devs[dev]->devc;
1820 int val;
1822 switch (cmd) {
1823 case SOUND_MIXER_PRIVATE1:
1825 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1826 int val;
1828 if (get_user(val, arg))
1829 return -EFAULT;
1831 /* check if parameter is logical */
1832 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1833 VNC_MUTE_LINE_OUT |
1834 VNC_DISABLE_AUTOSWITCH))
1835 return -EINVAL;
1837 prev_auto_state = devc->no_autoselect;
1838 prev_spkr_mute = devc->spkr_mute_state;
1839 prev_line_mute = devc->line_mute_state;
1841 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1842 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1843 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1845 if (prev_spkr_mute != devc->spkr_mute_state)
1846 vnc_mute_spkr(devc);
1848 if (prev_line_mute != devc->line_mute_state)
1849 vnc_mute_lout(devc);
1851 if (prev_auto_state != devc->no_autoselect)
1852 vnc_configure_mixer(devc, devc->recmask);
1854 return 0;
1857 case SOUND_MIXER_PRIVATE2:
1858 if (get_user(val, arg))
1859 return -EFAULT;
1861 switch (val) {
1862 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1863 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1864 case VNC_SOUND_PAUSE:
1865 waveartist_cmd1(devc, 0x16);
1866 break;
1868 case VNC_SOUND_RESUME:
1869 waveartist_cmd1(devc, 0x18);
1870 break;
1872 default:
1873 return -EINVAL;
1875 return 0;
1877 /* private ioctl to allow bulk access to waveartist */
1878 case SOUND_MIXER_PRIVATE3:
1880 unsigned long flags;
1881 int mixer_reg[15], i, val;
1883 if (get_user(val, arg))
1884 return -EFAULT;
1885 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1886 return -EFAULT;
1888 switch (mixer_reg[14]) {
1889 case MIXER_PRIVATE3_RESET:
1890 waveartist_mixer_reset(devc);
1891 break;
1893 case MIXER_PRIVATE3_WRITE:
1894 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1895 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1896 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1897 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1898 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1900 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1901 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1902 break;
1904 case MIXER_PRIVATE3_READ:
1905 spin_lock_irqsave(&waveartist_lock, flags);
1907 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1908 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1910 spin_unlock_irqrestore(&waveartist_lock, flags);
1912 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1913 return -EFAULT;
1914 break;
1916 default:
1917 return -EINVAL;
1919 return 0;
1922 /* read back the state from PRIVATE1 */
1923 case SOUND_MIXER_PRIVATE4:
1924 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1925 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1926 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1927 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1928 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1930 return put_user(val, arg) ? -EFAULT : 0;
1933 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1935 * special case for master volume: if we
1936 * received this call - switch from hw
1937 * volume control to a software volume
1938 * control, till the hw volume is modified
1939 * to signal that user wants to be back in
1940 * hardware...
1942 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1943 devc->use_slider = 0;
1945 /* speaker output */
1946 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1947 unsigned int val, l, r;
1949 if (get_user(val, arg))
1950 return -EFAULT;
1952 l = val & 0x7f;
1953 r = (val & 0x7f00) >> 8;
1954 val = (l + r) / 2;
1955 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1956 devc->spkr_mute_state = (val <= 50);
1957 vnc_mute_spkr(devc);
1958 return 0;
1962 return -ENOIOCTLCMD;
1965 #endif
1967 static struct address_info cfg;
1969 static int attached;
1971 static int __initdata io = 0;
1972 static int __initdata irq = 0;
1973 static int __initdata dma = 0;
1974 static int __initdata dma2 = 0;
1977 static int __init init_waveartist(void)
1979 const struct waveartist_mixer_info *mix;
1981 if (!io && machine_is_netwinder()) {
1983 * The NetWinder WaveArtist is at a fixed address.
1984 * If the user does not supply an address, use the
1985 * well-known parameters.
1987 io = 0x250;
1988 irq = 12;
1989 dma = 3;
1990 dma2 = 7;
1993 mix = &waveartist_mixer;
1994 #ifdef CONFIG_ARCH_NETWINDER
1995 if (machine_is_netwinder())
1996 mix = &netwinder_mixer;
1997 #endif
1999 cfg.io_base = io;
2000 cfg.irq = irq;
2001 cfg.dma = dma;
2002 cfg.dma2 = dma2;
2004 if (!probe_waveartist(&cfg))
2005 return -ENODEV;
2007 attach_waveartist(&cfg, mix);
2008 attached = 1;
2010 return 0;
2013 static void __exit cleanup_waveartist(void)
2015 if (attached)
2016 unload_waveartist(&cfg);
2019 module_init(init_waveartist);
2020 module_exit(cleanup_waveartist);
2022 #ifndef MODULE
2023 static int __init setup_waveartist(char *str)
2025 /* io, irq, dma, dma2 */
2026 int ints[5];
2028 str = get_options(str, ARRAY_SIZE(ints), ints);
2030 io = ints[1];
2031 irq = ints[2];
2032 dma = ints[3];
2033 dma2 = ints[4];
2035 return 1;
2037 __setup("waveartist=", setup_waveartist);
2038 #endif
2040 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2041 module_param(io, int, 0); /* IO base */
2042 module_param(irq, int, 0); /* IRQ */
2043 module_param(dma, int, 0); /* DMA */
2044 module_param(dma2, int, 0); /* DMA2 */
2045 MODULE_LICENSE("GPL");