gpiolib: never report open-drain/source lines as 'input' to user-space
[linux/fpc-iii.git] / sound / oss / waveartist.c
blob0b8d0de872732e618a55f6b26e9bec9935956d60
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 setup_timer(&vnc_timer, vnc_slider_tick,
1418 nr_waveartist_devs);
1419 mod_timer(&vnc_timer, jiffies);
1421 vnc_configure_mixer(devc, 0);
1423 devc->no_autoselect = 1;
1425 #endif
1426 nr_waveartist_devs += 1;
1430 static void __exit unload_waveartist(struct address_info *hw)
1432 struct wavnc_info *devc = NULL;
1433 int i;
1435 for (i = 0; i < nr_waveartist_devs; i++)
1436 if (hw->io_base == adev_info[i].hw.io_base) {
1437 devc = adev_info + i;
1438 break;
1441 if (devc != NULL) {
1442 int mixer;
1444 #ifdef CONFIG_ARCH_NETWINDER
1445 if (machine_is_netwinder())
1446 del_timer(&vnc_timer);
1447 #endif
1449 release_region(devc->hw.io_base, 15);
1451 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1453 if (devc->hw.irq >= 0)
1454 free_irq(devc->hw.irq, devc);
1456 sound_free_dma(devc->hw.dma);
1458 if (devc->hw.dma != devc->hw.dma2 &&
1459 devc->hw.dma2 != NO_DMA)
1460 sound_free_dma(devc->hw.dma2);
1462 mixer = audio_devs[devc->dev_no]->mixer_dev;
1464 if (mixer >= 0)
1465 sound_unload_mixerdev(mixer);
1467 if (devc->dev_no >= 0)
1468 sound_unload_audiodev(devc->dev_no);
1470 nr_waveartist_devs -= 1;
1472 for (; i < nr_waveartist_devs; i++)
1473 adev_info[i] = adev_info[i + 1];
1474 } else
1475 printk(KERN_WARNING "waveartist: can't find device "
1476 "to unload\n");
1479 #ifdef CONFIG_ARCH_NETWINDER
1482 * Rebel.com Netwinder specifics...
1485 #include <asm/hardware/dec21285.h>
1487 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1489 #define MIXER_PRIVATE3_RESET 0x53570000
1490 #define MIXER_PRIVATE3_READ 0x53570001
1491 #define MIXER_PRIVATE3_WRITE 0x53570002
1493 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1494 #define VNC_MUTE_LINE_OUT 0x10
1495 #define VNC_PHONE_DETECT 0x20
1496 #define VNC_HANDSET_DETECT 0x40
1497 #define VNC_DISABLE_AUTOSWITCH 0x80
1499 static inline void
1500 vnc_mute_spkr(struct wavnc_info *devc)
1502 unsigned long flags;
1504 raw_spin_lock_irqsave(&nw_gpio_lock, flags);
1505 nw_cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1506 raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
1509 static void
1510 vnc_mute_lout(struct wavnc_info *devc)
1512 unsigned int left, right;
1514 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1515 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1517 if (devc->line_mute_state) {
1518 left &= ~1;
1519 right &= ~1;
1520 } else {
1521 left |= 1;
1522 right |= 1;
1524 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1528 static int
1529 vnc_volume_slider(struct wavnc_info *devc)
1531 static signed int old_slider_volume;
1532 unsigned long flags;
1533 signed int volume = 255;
1535 *CSR_TIMER1_LOAD = 0x00ffffff;
1537 spin_lock_irqsave(&waveartist_lock, flags);
1539 outb(0xFF, 0x201);
1540 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1542 while (volume && (inb(0x201) & 0x01))
1543 volume--;
1545 *CSR_TIMER1_CNTL = 0;
1547 spin_unlock_irqrestore(&waveartist_lock,flags);
1549 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1552 #ifndef REVERSE
1553 volume = 150 - (volume >> 5);
1554 #else
1555 volume = (volume >> 6) - 25;
1556 #endif
1558 if (volume < 0)
1559 volume = 0;
1561 if (volume > 100)
1562 volume = 100;
1565 * slider quite often reads +-8, so debounce this random noise
1567 if (abs(volume - old_slider_volume) > 7) {
1568 old_slider_volume = volume;
1570 if (debug_flg & DEBUG_MIXER)
1571 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1574 return old_slider_volume;
1578 * Decode a recording mask into a mixer selection on the NetWinder
1579 * as follows:
1581 * OSS Source WA Source Actual source
1582 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1583 * SOUND_MASK_LINE Line Line in
1584 * SOUND_MASK_LINE1 Left Mic Handset
1585 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1586 * SOUND_MASK_MIC Right Mic Builtin microphone
1588 static unsigned int
1589 netwinder_select_input(struct wavnc_info *devc, unsigned int recmask,
1590 unsigned char *dev_l, unsigned char *dev_r)
1592 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1594 if (recmask & SOUND_MASK_IMIX) {
1595 recmask = SOUND_MASK_IMIX;
1596 recdev_l = ADC_MUX_MIXER;
1597 recdev_r = ADC_MUX_MIXER;
1598 } else if (recmask & SOUND_MASK_LINE) {
1599 recmask = SOUND_MASK_LINE;
1600 recdev_l = ADC_MUX_LINE;
1601 recdev_r = ADC_MUX_LINE;
1602 } else if (recmask & SOUND_MASK_LINE1) {
1603 recmask = SOUND_MASK_LINE1;
1604 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1605 recdev_l = ADC_MUX_MIC;
1606 recdev_r = ADC_MUX_NONE;
1607 } else if (recmask & SOUND_MASK_PHONEIN) {
1608 recmask = SOUND_MASK_PHONEIN;
1609 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1610 recdev_l = ADC_MUX_AUX1;
1611 recdev_r = ADC_MUX_NONE;
1612 } else if (recmask & SOUND_MASK_MIC) {
1613 recmask = SOUND_MASK_MIC;
1614 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1615 recdev_l = ADC_MUX_NONE;
1616 recdev_r = ADC_MUX_MIC;
1619 *dev_l = recdev_l;
1620 *dev_r = recdev_r;
1622 return recmask;
1625 static int
1626 netwinder_decode_mixer(struct wavnc_info *devc, int dev, unsigned char lev_l,
1627 unsigned char lev_r)
1629 switch (dev) {
1630 case SOUND_MIXER_VOLUME:
1631 case SOUND_MIXER_SYNTH:
1632 case SOUND_MIXER_PCM:
1633 case SOUND_MIXER_LINE:
1634 case SOUND_MIXER_IGAIN:
1635 devc->levels[dev] = lev_l | lev_r << 8;
1636 break;
1638 case SOUND_MIXER_MIC: /* right mic only */
1639 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1640 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1641 break;
1643 case SOUND_MIXER_LINE1: /* left mic only */
1644 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1645 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1646 dev = SOUND_MIXER_MIC;
1647 break;
1649 case SOUND_MIXER_PHONEIN: /* left aux only */
1650 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1651 dev = SOUND_MIXER_LINE1;
1652 break;
1654 case SOUND_MIXER_IMIX:
1655 case SOUND_MIXER_PHONEOUT:
1656 break;
1658 default:
1659 dev = -EINVAL;
1660 break;
1662 return dev;
1665 static int netwinder_get_mixer(struct wavnc_info *devc, int dev)
1667 int levels;
1669 switch (dev) {
1670 case SOUND_MIXER_VOLUME:
1671 case SOUND_MIXER_SYNTH:
1672 case SOUND_MIXER_PCM:
1673 case SOUND_MIXER_LINE:
1674 case SOUND_MIXER_IGAIN:
1675 levels = devc->levels[dev];
1676 break;
1678 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1679 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1680 levels |= levels << 8;
1681 break;
1683 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1684 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1685 levels |= levels << 8;
1686 break;
1688 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1689 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1690 levels |= levels << 8;
1691 break;
1693 default:
1694 levels = 0;
1697 return levels;
1701 * Waveartist specific mixer information.
1703 static const struct waveartist_mixer_info netwinder_mixer = {
1704 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1705 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1706 SOUND_MASK_LINE | SOUND_MASK_MIC |
1707 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1708 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1709 SOUND_MASK_IGAIN,
1711 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1712 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1713 SOUND_MASK_PHONEIN,
1715 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1716 SOUND_MASK_PCM | SOUND_MASK_LINE |
1717 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1719 .select_input = netwinder_select_input,
1720 .decode_mixer = netwinder_decode_mixer,
1721 .get_mixer = netwinder_get_mixer,
1724 static void
1725 vnc_configure_mixer(struct wavnc_info *devc, unsigned int recmask)
1727 if (!devc->no_autoselect) {
1728 if (devc->handset_detect) {
1729 recmask = SOUND_MASK_LINE1;
1730 devc->spkr_mute_state = devc->line_mute_state = 1;
1731 } else if (devc->telephone_detect) {
1732 recmask = SOUND_MASK_PHONEIN;
1733 devc->spkr_mute_state = devc->line_mute_state = 1;
1734 } else {
1735 /* unless someone has asked for LINE-IN,
1736 * we default to MIC
1738 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1739 devc->recmask = SOUND_MASK_MIC;
1740 devc->spkr_mute_state = devc->line_mute_state = 0;
1742 vnc_mute_spkr(devc);
1743 vnc_mute_lout(devc);
1745 if (recmask != devc->recmask)
1746 waveartist_set_recmask(devc, recmask);
1750 static int
1751 vnc_slider(struct wavnc_info *devc)
1753 signed int slider_volume;
1754 unsigned int temp, old_hs, old_td;
1757 * read the "buttons" state.
1758 * Bit 4 = 0 means handset present
1759 * Bit 5 = 1 means phone offhook
1761 temp = inb(0x201);
1763 old_hs = devc->handset_detect;
1764 old_td = devc->telephone_detect;
1766 devc->handset_detect = !(temp & 0x10);
1767 devc->telephone_detect = !!(temp & 0x20);
1769 if (!devc->no_autoselect &&
1770 (old_hs != devc->handset_detect ||
1771 old_td != devc->telephone_detect))
1772 vnc_configure_mixer(devc, devc->recmask);
1774 slider_volume = vnc_volume_slider(devc);
1777 * If we're using software controlled volume, and
1778 * the slider moves by more than 20%, then we
1779 * switch back to slider controlled volume.
1781 if (abs(devc->slider_vol - slider_volume) > 20)
1782 devc->use_slider = 1;
1785 * use only left channel
1787 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1789 if (slider_volume != temp && devc->use_slider) {
1790 devc->slider_vol = slider_volume;
1792 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1793 slider_volume | slider_volume << 8);
1795 return 1;
1798 return 0;
1801 static void
1802 vnc_slider_tick(unsigned long data)
1804 int next_timeout;
1806 if (vnc_slider(adev_info + data))
1807 next_timeout = 5; // mixer reported change
1808 else
1809 next_timeout = VNC_TIMER_PERIOD;
1811 mod_timer(&vnc_timer, jiffies + next_timeout);
1814 static int
1815 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1817 struct wavnc_info *devc = (struct wavnc_info *)audio_devs[dev]->devc;
1818 int val;
1820 switch (cmd) {
1821 case SOUND_MIXER_PRIVATE1:
1823 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1824 int val;
1826 if (get_user(val, arg))
1827 return -EFAULT;
1829 /* check if parameter is logical */
1830 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1831 VNC_MUTE_LINE_OUT |
1832 VNC_DISABLE_AUTOSWITCH))
1833 return -EINVAL;
1835 prev_auto_state = devc->no_autoselect;
1836 prev_spkr_mute = devc->spkr_mute_state;
1837 prev_line_mute = devc->line_mute_state;
1839 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1840 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1841 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1843 if (prev_spkr_mute != devc->spkr_mute_state)
1844 vnc_mute_spkr(devc);
1846 if (prev_line_mute != devc->line_mute_state)
1847 vnc_mute_lout(devc);
1849 if (prev_auto_state != devc->no_autoselect)
1850 vnc_configure_mixer(devc, devc->recmask);
1852 return 0;
1855 case SOUND_MIXER_PRIVATE2:
1856 if (get_user(val, arg))
1857 return -EFAULT;
1859 switch (val) {
1860 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1861 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1862 case VNC_SOUND_PAUSE:
1863 waveartist_cmd1(devc, 0x16);
1864 break;
1866 case VNC_SOUND_RESUME:
1867 waveartist_cmd1(devc, 0x18);
1868 break;
1870 default:
1871 return -EINVAL;
1873 return 0;
1875 /* private ioctl to allow bulk access to waveartist */
1876 case SOUND_MIXER_PRIVATE3:
1878 unsigned long flags;
1879 int mixer_reg[15], i, val;
1881 if (get_user(val, arg))
1882 return -EFAULT;
1883 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1884 return -EFAULT;
1886 switch (mixer_reg[14]) {
1887 case MIXER_PRIVATE3_RESET:
1888 waveartist_mixer_reset(devc);
1889 break;
1891 case MIXER_PRIVATE3_WRITE:
1892 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1893 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1894 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1895 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1896 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1898 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1899 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1900 break;
1902 case MIXER_PRIVATE3_READ:
1903 spin_lock_irqsave(&waveartist_lock, flags);
1905 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1906 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1908 spin_unlock_irqrestore(&waveartist_lock, flags);
1910 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1911 return -EFAULT;
1912 break;
1914 default:
1915 return -EINVAL;
1917 return 0;
1920 /* read back the state from PRIVATE1 */
1921 case SOUND_MIXER_PRIVATE4:
1922 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1923 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1924 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1925 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1926 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1928 return put_user(val, arg) ? -EFAULT : 0;
1931 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1933 * special case for master volume: if we
1934 * received this call - switch from hw
1935 * volume control to a software volume
1936 * control, till the hw volume is modified
1937 * to signal that user wants to be back in
1938 * hardware...
1940 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1941 devc->use_slider = 0;
1943 /* speaker output */
1944 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1945 unsigned int val, l, r;
1947 if (get_user(val, arg))
1948 return -EFAULT;
1950 l = val & 0x7f;
1951 r = (val & 0x7f00) >> 8;
1952 val = (l + r) / 2;
1953 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1954 devc->spkr_mute_state = (val <= 50);
1955 vnc_mute_spkr(devc);
1956 return 0;
1960 return -ENOIOCTLCMD;
1963 #endif
1965 static struct address_info cfg;
1967 static int attached;
1969 static int __initdata io = 0;
1970 static int __initdata irq = 0;
1971 static int __initdata dma = 0;
1972 static int __initdata dma2 = 0;
1975 static int __init init_waveartist(void)
1977 const struct waveartist_mixer_info *mix;
1979 if (!io && machine_is_netwinder()) {
1981 * The NetWinder WaveArtist is at a fixed address.
1982 * If the user does not supply an address, use the
1983 * well-known parameters.
1985 io = 0x250;
1986 irq = 12;
1987 dma = 3;
1988 dma2 = 7;
1991 mix = &waveartist_mixer;
1992 #ifdef CONFIG_ARCH_NETWINDER
1993 if (machine_is_netwinder())
1994 mix = &netwinder_mixer;
1995 #endif
1997 cfg.io_base = io;
1998 cfg.irq = irq;
1999 cfg.dma = dma;
2000 cfg.dma2 = dma2;
2002 if (!probe_waveartist(&cfg))
2003 return -ENODEV;
2005 attach_waveartist(&cfg, mix);
2006 attached = 1;
2008 return 0;
2011 static void __exit cleanup_waveartist(void)
2013 if (attached)
2014 unload_waveartist(&cfg);
2017 module_init(init_waveartist);
2018 module_exit(cleanup_waveartist);
2020 #ifndef MODULE
2021 static int __init setup_waveartist(char *str)
2023 /* io, irq, dma, dma2 */
2024 int ints[5];
2026 str = get_options(str, ARRAY_SIZE(ints), ints);
2028 io = ints[1];
2029 irq = ints[2];
2030 dma = ints[3];
2031 dma2 = ints[4];
2033 return 1;
2035 __setup("waveartist=", setup_waveartist);
2036 #endif
2038 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2039 module_param(io, int, 0); /* IO base */
2040 module_param(irq, int, 0); /* IRQ */
2041 module_param(dma, int, 0); /* DMA */
2042 module_param(dma2, int, 0); /* DMA2 */
2043 MODULE_LICENSE("GPL");