Add linux-next specific files for 20110716
[linux-2.6/next.git] / sound / oss / waveartist.c
blob52468742d9f240650376da69b368927c095041e7
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>
45 #include <asm/system.h>
47 #include "sound_config.h"
48 #include "waveartist.h"
50 #ifdef CONFIG_ARM
51 #include <mach/hardware.h>
52 #include <asm/mach-types.h>
53 #endif
55 #ifndef NO_DMA
56 #define NO_DMA 255
57 #endif
59 #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH |\
60 SOUND_MASK_PCM |\
61 SOUND_MASK_LINE |\
62 SOUND_MASK_MIC |\
63 SOUND_MASK_LINE1 |\
64 SOUND_MASK_RECLEV |\
65 SOUND_MASK_VOLUME |\
66 SOUND_MASK_IMIX)
68 static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
69 0x5555, /* Master Volume */
70 0x0000, /* Bass */
71 0x0000, /* Treble */
72 0x2323, /* Synth (FM) */
73 0x4b4b, /* PCM */
74 0x6464, /* PC Speaker */
75 0x0000, /* Ext Line */
76 0x0000, /* Mic */
77 0x0000, /* CD */
78 0x6464, /* Recording monitor */
79 0x0000, /* SB PCM (ALT PCM) */
80 0x0000, /* Recording level */
81 0x6464, /* Input gain */
82 0x6464, /* Output gain */
83 0x0000, /* Line1 (Aux1) */
84 0x0000, /* Line2 (Aux2) */
85 0x0000, /* Line3 (Aux3) */
86 0x0000, /* Digital1 */
87 0x0000, /* Digital2 */
88 0x0000, /* Digital3 */
89 0x0000, /* Phone In */
90 0x6464, /* Phone Out */
91 0x0000, /* Video */
92 0x0000, /* Radio */
93 0x0000 /* Monitor */
96 typedef struct {
97 struct address_info hw; /* hardware */
98 char *chip_name;
100 int xfer_count;
101 int audio_mode;
102 int open_mode;
103 int audio_flags;
104 int record_dev;
105 int playback_dev;
106 int dev_no;
108 /* Mixer parameters */
109 const struct waveartist_mixer_info *mix;
111 unsigned short *levels; /* cache of volume settings */
112 int recmask; /* currently enabled recording device! */
114 #ifdef CONFIG_ARCH_NETWINDER
115 signed int slider_vol; /* hardware slider volume */
116 unsigned int handset_detect :1;
117 unsigned int telephone_detect:1;
118 unsigned int no_autoselect :1;/* handset/telephone autoselects a path */
119 unsigned int spkr_mute_state :1;/* set by ioctl or autoselect */
120 unsigned int line_mute_state :1;/* set by ioctl or autoselect */
121 unsigned int use_slider :1;/* use slider setting for o/p vol */
122 #endif
123 } wavnc_info;
126 * This is the implementation specific mixer information.
128 struct waveartist_mixer_info {
129 unsigned int supported_devs; /* Supported devices */
130 unsigned int recording_devs; /* Recordable devies */
131 unsigned int stereo_devs; /* Stereo devices */
133 unsigned int (*select_input)(wavnc_info *, unsigned int,
134 unsigned char *, unsigned char *);
135 int (*decode_mixer)(wavnc_info *, int,
136 unsigned char, unsigned char);
137 int (*get_mixer)(wavnc_info *, int);
140 typedef struct wavnc_port_info {
141 int open_mode;
142 int speed;
143 int channels;
144 int audio_format;
145 } wavnc_port_info;
147 static int nr_waveartist_devs;
148 static wavnc_info adev_info[MAX_AUDIO_DEV];
149 static DEFINE_SPINLOCK(waveartist_lock);
151 #ifndef CONFIG_ARCH_NETWINDER
152 #define machine_is_netwinder() 0
153 #else
154 static struct timer_list vnc_timer;
155 static void vnc_configure_mixer(wavnc_info *devc, 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(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(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(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(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(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(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(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(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 wavnc_info *devc;
376 wavnc_port_info *portc;
377 unsigned long flags;
379 if (dev < 0 || dev >= num_audiodevs)
380 return -ENXIO;
382 devc = (wavnc_info *) audio_devs[dev]->devc;
383 portc = (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 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
409 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
410 unsigned long flags;
412 spin_lock_irqsave(&waveartist_lock, flags);
414 waveartist_halt(dev);
416 devc->audio_mode = 0;
417 devc->open_mode &= ~portc->open_mode;
418 portc->open_mode = 0;
420 spin_unlock_irqrestore(&waveartist_lock, flags);
423 static void
424 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
426 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
427 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
428 unsigned long flags;
429 unsigned int count = __count;
431 if (debug_flg & DEBUG_OUT)
432 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
433 buf, count);
435 * 16 bit data
437 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
438 count >>= 1;
440 if (portc->channels > 1)
441 count >>= 1;
443 count -= 1;
445 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
446 audio_devs[dev]->flags & DMA_AUTOMODE &&
447 intrflag &&
448 count == devc->xfer_count) {
449 devc->audio_mode |= PCM_ENABLE_OUTPUT;
450 return; /*
451 * Auto DMA mode on. No need to react
455 spin_lock_irqsave(&waveartist_lock, flags);
458 * set sample count
460 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
462 devc->xfer_count = count;
463 devc->audio_mode |= PCM_ENABLE_OUTPUT;
465 spin_unlock_irqrestore(&waveartist_lock, flags);
468 static void
469 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
471 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
472 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
473 unsigned long flags;
474 unsigned int count = __count;
476 if (debug_flg & DEBUG_IN)
477 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
478 buf, count);
480 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
481 count >>= 1;
483 if (portc->channels > 1)
484 count >>= 1;
486 count -= 1;
488 if (devc->audio_mode & PCM_ENABLE_INPUT &&
489 audio_devs[dev]->flags & DMA_AUTOMODE &&
490 intrflag &&
491 count == devc->xfer_count) {
492 devc->audio_mode |= PCM_ENABLE_INPUT;
493 return; /*
494 * Auto DMA mode on. No need to react
498 spin_lock_irqsave(&waveartist_lock, flags);
501 * set sample count
503 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
505 devc->xfer_count = count;
506 devc->audio_mode |= PCM_ENABLE_INPUT;
508 spin_unlock_irqrestore(&waveartist_lock, flags);
511 static int
512 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
514 return -EINVAL;
517 static unsigned int
518 waveartist_get_speed(wavnc_port_info *portc)
520 unsigned int speed;
523 * program the speed, channels, bits
525 if (portc->speed == 8000)
526 speed = 0x2E71;
527 else if (portc->speed == 11025)
528 speed = 0x4000;
529 else if (portc->speed == 22050)
530 speed = 0x8000;
531 else if (portc->speed == 44100)
532 speed = 0x0;
533 else {
535 * non-standard - just calculate
537 speed = portc->speed << 16;
539 speed = (speed / 44100) & 65535;
542 return speed;
545 static unsigned int
546 waveartist_get_bits(wavnc_port_info *portc)
548 unsigned int bits;
550 if (portc->audio_format == AFMT_S16_LE)
551 bits = 1;
552 else if (portc->audio_format == AFMT_S8)
553 bits = 0;
554 else
555 bits = 2; //default AFMT_U8
557 return bits;
560 static int
561 waveartist_prepare_for_input(int dev, int bsize, int bcount)
563 unsigned long flags;
564 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
565 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
566 unsigned int speed, bits;
568 if (devc->audio_mode)
569 return 0;
571 speed = waveartist_get_speed(portc);
572 bits = waveartist_get_bits(portc);
574 spin_lock_irqsave(&waveartist_lock, flags);
576 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
577 printk(KERN_WARNING "waveartist: error setting the "
578 "record format to %d\n", portc->audio_format);
580 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
581 printk(KERN_WARNING "waveartist: error setting record "
582 "to %d channels\n", portc->channels);
585 * write cmd SetSampleSpeedTimeConstant
587 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
588 printk(KERN_WARNING "waveartist: error setting the record "
589 "speed to %dHz.\n", portc->speed);
591 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
592 printk(KERN_WARNING "waveartist: error setting the record "
593 "data path to 0x%X\n", 1);
595 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
596 printk(KERN_WARNING "waveartist: error setting the record "
597 "format to %d\n", portc->audio_format);
599 devc->xfer_count = 0;
600 spin_unlock_irqrestore(&waveartist_lock, flags);
601 waveartist_halt_input(dev);
603 if (debug_flg & DEBUG_INTR) {
604 printk("WA CTLR reg: 0x%02X.\n",
605 inb(devc->hw.io_base + CTLR));
606 printk("WA STAT reg: 0x%02X.\n",
607 inb(devc->hw.io_base + STATR));
608 printk("WA IRQS reg: 0x%02X.\n",
609 inb(devc->hw.io_base + IRQSTAT));
612 return 0;
615 static int
616 waveartist_prepare_for_output(int dev, int bsize, int bcount)
618 unsigned long flags;
619 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
620 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
621 unsigned int speed, bits;
624 * program the speed, channels, bits
626 speed = waveartist_get_speed(portc);
627 bits = waveartist_get_bits(portc);
629 spin_lock_irqsave(&waveartist_lock, flags);
631 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
632 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
633 printk(KERN_WARNING "waveartist: error setting the playback "
634 "speed to %dHz.\n", portc->speed);
636 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
637 printk(KERN_WARNING "waveartist: error setting the playback "
638 "to %d channels\n", portc->channels);
640 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
641 printk(KERN_WARNING "waveartist: error setting the playback "
642 "data path to 0x%X\n", 0);
644 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
645 printk(KERN_WARNING "waveartist: error setting the playback "
646 "format to %d\n", portc->audio_format);
648 devc->xfer_count = 0;
649 spin_unlock_irqrestore(&waveartist_lock, flags);
650 waveartist_halt_output(dev);
652 if (debug_flg & DEBUG_INTR) {
653 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
654 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
655 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
658 return 0;
661 static void
662 waveartist_halt(int dev)
664 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
665 wavnc_info *devc;
667 if (portc->open_mode & OPEN_WRITE)
668 waveartist_halt_output(dev);
670 if (portc->open_mode & OPEN_READ)
671 waveartist_halt_input(dev);
673 devc = (wavnc_info *) audio_devs[dev]->devc;
674 devc->audio_mode = 0;
677 static void
678 waveartist_halt_input(int dev)
680 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
681 unsigned long flags;
683 spin_lock_irqsave(&waveartist_lock, flags);
686 * Stop capture
688 waveartist_cmd1(devc, WACMD_INPUTSTOP);
690 devc->audio_mode &= ~PCM_ENABLE_INPUT;
693 * Clear interrupt by toggling
694 * the IRQ_ACK bit in CTRL
696 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
697 waveartist_iack(devc);
699 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
701 spin_unlock_irqrestore(&waveartist_lock, flags);
704 static void
705 waveartist_halt_output(int dev)
707 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
708 unsigned long flags;
710 spin_lock_irqsave(&waveartist_lock, flags);
712 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
714 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
717 * Clear interrupt by toggling
718 * the IRQ_ACK bit in CTRL
720 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
721 waveartist_iack(devc);
723 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
725 spin_unlock_irqrestore(&waveartist_lock, flags);
728 static void
729 waveartist_trigger(int dev, int state)
731 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
732 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
733 unsigned long flags;
735 if (debug_flg & DEBUG_TRIGGER) {
736 printk("wavnc: audio trigger ");
737 if (state & PCM_ENABLE_INPUT)
738 printk("in ");
739 if (state & PCM_ENABLE_OUTPUT)
740 printk("out");
741 printk("\n");
744 spin_lock_irqsave(&waveartist_lock, flags);
746 state &= devc->audio_mode;
748 if (portc->open_mode & OPEN_READ &&
749 state & PCM_ENABLE_INPUT)
751 * enable ADC Data Transfer to PC
753 waveartist_cmd1(devc, WACMD_INPUTSTART);
755 if (portc->open_mode & OPEN_WRITE &&
756 state & PCM_ENABLE_OUTPUT)
758 * enable DAC data transfer from PC
760 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
762 spin_unlock_irqrestore(&waveartist_lock, flags);
765 static int
766 waveartist_set_speed(int dev, int arg)
768 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
770 if (arg <= 0)
771 return portc->speed;
773 if (arg < 5000)
774 arg = 5000;
775 if (arg > 44100)
776 arg = 44100;
778 portc->speed = arg;
779 return portc->speed;
783 static short
784 waveartist_set_channels(int dev, short arg)
786 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
788 if (arg != 1 && arg != 2)
789 return portc->channels;
791 portc->channels = arg;
792 return arg;
795 static unsigned int
796 waveartist_set_bits(int dev, unsigned int arg)
798 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
800 if (arg == 0)
801 return portc->audio_format;
803 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
804 arg = AFMT_U8;
806 portc->audio_format = arg;
808 return arg;
811 static struct audio_driver waveartist_audio_driver = {
812 .owner = THIS_MODULE,
813 .open = waveartist_open,
814 .close = waveartist_close,
815 .output_block = waveartist_output_block,
816 .start_input = waveartist_start_input,
817 .ioctl = waveartist_ioctl,
818 .prepare_for_input = waveartist_prepare_for_input,
819 .prepare_for_output = waveartist_prepare_for_output,
820 .halt_io = waveartist_halt,
821 .halt_input = waveartist_halt_input,
822 .halt_output = waveartist_halt_output,
823 .trigger = waveartist_trigger,
824 .set_speed = waveartist_set_speed,
825 .set_bits = waveartist_set_bits,
826 .set_channels = waveartist_set_channels
830 static irqreturn_t
831 waveartist_intr(int irq, void *dev_id)
833 wavnc_info *devc = dev_id;
834 int irqstatus, status;
836 spin_lock(&waveartist_lock);
837 irqstatus = inb(devc->hw.io_base + IRQSTAT);
838 status = inb(devc->hw.io_base + STATR);
840 if (debug_flg & DEBUG_INTR)
841 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
842 status, irqstatus);
844 if (status & IRQ_REQ) /* Clear interrupt */
845 waveartist_iack(devc);
846 else
847 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
849 if (irqstatus & 0x01) {
850 int temp = 1;
852 /* PCM buffer done
854 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
855 DMAbuf_outputintr(devc->playback_dev, 1);
856 temp = 0;
858 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
859 DMAbuf_inputintr(devc->record_dev);
860 temp = 0;
862 if (temp) //default:
863 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
865 if (irqstatus & 0x2)
866 // We do not use SB mode natively...
867 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
868 spin_unlock(&waveartist_lock);
869 return IRQ_HANDLED;
872 /* -------------------------------------------------------------------------
873 * Mixer stuff
875 struct mix_ent {
876 unsigned char reg_l;
877 unsigned char reg_r;
878 unsigned char shift;
879 unsigned char max;
882 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
883 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
884 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
885 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
886 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
887 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
888 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
889 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
890 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
893 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
894 #if 0
895 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
896 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
897 #else
898 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
899 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
900 #endif
901 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
902 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
903 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
904 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
905 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
906 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
907 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
908 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
909 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
912 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
915 static void
916 waveartist_mixer_update(wavnc_info *devc, int whichDev)
918 unsigned int lev_left, lev_right;
920 lev_left = devc->levels[whichDev] & 0xff;
921 lev_right = devc->levels[whichDev] >> 8;
923 if (lev_left > 100)
924 lev_left = 100;
925 if (lev_right > 100)
926 lev_right = 100;
928 #define SCALE(lev,max) ((lev) * (max) / 100)
930 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
931 whichDev = SOUND_MIXER_VOLUME;
933 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
934 const struct mix_ent *mix = mix_devs + whichDev;
935 unsigned int mask, left, right;
937 mask = mix->max << mix->shift;
938 lev_left = SCALE(lev_left, mix->max) << mix->shift;
939 lev_right = SCALE(lev_right, mix->max) << mix->shift;
941 /* read left setting */
942 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
943 mix->reg_l << 8);
945 /* read right setting */
946 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
947 mix->reg_r << 8);
949 left = (left & ~mask) | (lev_left & mask);
950 right = (right & ~mask) | (lev_right & mask);
952 /* write left,right back */
953 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
954 } else {
955 switch(whichDev) {
956 case SOUND_MIXER_PCM:
957 waveartist_cmd3(devc, WACMD_SET_LEVEL,
958 SCALE(lev_left, 32767),
959 SCALE(lev_right, 32767));
960 break;
962 case SOUND_MIXER_SYNTH:
963 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
964 SCALE(lev_left, 32767),
965 SCALE(lev_right, 32767));
966 break;
972 * Set the ADC MUX to the specified values. We do NOT do any
973 * checking of the values passed, since we assume that the
974 * relevant *_select_input function has done that for us.
976 static void
977 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
979 unsigned int reg_08, reg_09;
981 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
982 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
984 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
986 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
990 * Decode a recording mask into a mixer selection as follows:
992 * OSS Source WA Source Actual source
993 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
994 * SOUND_MASK_LINE Line Line in
995 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
996 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
997 * SOUND_MASK_MIC Mic Microphone
999 static unsigned int
1000 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1001 unsigned char *dev_l, unsigned char *dev_r)
1003 unsigned int recdev = ADC_MUX_NONE;
1005 if (recmask & SOUND_MASK_IMIX) {
1006 recmask = SOUND_MASK_IMIX;
1007 recdev = ADC_MUX_MIXER;
1008 } else if (recmask & SOUND_MASK_LINE2) {
1009 recmask = SOUND_MASK_LINE2;
1010 recdev = ADC_MUX_AUX2;
1011 } else if (recmask & SOUND_MASK_LINE1) {
1012 recmask = SOUND_MASK_LINE1;
1013 recdev = ADC_MUX_AUX1;
1014 } else if (recmask & SOUND_MASK_LINE) {
1015 recmask = SOUND_MASK_LINE;
1016 recdev = ADC_MUX_LINE;
1017 } else if (recmask & SOUND_MASK_MIC) {
1018 recmask = SOUND_MASK_MIC;
1019 recdev = ADC_MUX_MIC;
1022 *dev_l = *dev_r = recdev;
1024 return recmask;
1027 static int
1028 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1029 unsigned char lev_r)
1031 switch (dev) {
1032 case SOUND_MIXER_VOLUME:
1033 case SOUND_MIXER_SYNTH:
1034 case SOUND_MIXER_PCM:
1035 case SOUND_MIXER_LINE:
1036 case SOUND_MIXER_MIC:
1037 case SOUND_MIXER_IGAIN:
1038 case SOUND_MIXER_LINE1:
1039 case SOUND_MIXER_LINE2:
1040 devc->levels[dev] = lev_l | lev_r << 8;
1041 break;
1043 case SOUND_MIXER_IMIX:
1044 break;
1046 default:
1047 dev = -EINVAL;
1048 break;
1051 return dev;
1054 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1056 return devc->levels[dev];
1059 static const struct waveartist_mixer_info waveartist_mixer = {
1060 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1061 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1062 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1063 SOUND_MASK_IMIX,
1064 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1065 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1066 .select_input = waveartist_select_input,
1067 .decode_mixer = waveartist_decode_mixer,
1068 .get_mixer = waveartist_get_mixer,
1071 static void
1072 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1074 unsigned char dev_l, dev_r;
1076 recmask &= devc->mix->recording_devs;
1079 * If more than one recording device selected,
1080 * disable the device that is currently in use.
1082 if (hweight32(recmask) > 1)
1083 recmask &= ~devc->recmask;
1086 * Translate the recording device mask into
1087 * the ADC multiplexer settings.
1089 devc->recmask = devc->mix->select_input(devc, recmask,
1090 &dev_l, &dev_r);
1092 waveartist_set_adc_mux(devc, dev_l, dev_r);
1095 static int
1096 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1098 unsigned int lev_left = level & 0x00ff;
1099 unsigned int lev_right = (level & 0xff00) >> 8;
1101 if (lev_left > 100)
1102 lev_left = 100;
1103 if (lev_right > 100)
1104 lev_right = 100;
1107 * Mono devices have their right volume forced to their
1108 * left volume. (from ALSA driver OSS emulation).
1110 if (!(devc->mix->stereo_devs & (1 << dev)))
1111 lev_right = lev_left;
1113 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1115 if (dev >= 0)
1116 waveartist_mixer_update(devc, dev);
1118 return dev < 0 ? dev : 0;
1121 static int
1122 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1124 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1125 int ret = 0, val, nr;
1128 * All SOUND_MIXER_* ioctls use type 'M'
1130 if (((cmd >> 8) & 255) != 'M')
1131 return -ENOIOCTLCMD;
1133 #ifdef CONFIG_ARCH_NETWINDER
1134 if (machine_is_netwinder()) {
1135 ret = vnc_private_ioctl(dev, cmd, arg);
1136 if (ret != -ENOIOCTLCMD)
1137 return ret;
1138 else
1139 ret = 0;
1141 #endif
1143 nr = cmd & 0xff;
1145 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1146 if (get_user(val, (int __user *)arg))
1147 return -EFAULT;
1149 switch (nr) {
1150 case SOUND_MIXER_RECSRC:
1151 waveartist_set_recmask(devc, val);
1152 break;
1154 default:
1155 ret = -EINVAL;
1156 if (nr < SOUND_MIXER_NRDEVICES &&
1157 devc->mix->supported_devs & (1 << nr))
1158 ret = waveartist_set_mixer(devc, nr, val);
1162 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1163 ret = -EINVAL;
1165 switch (nr) {
1166 case SOUND_MIXER_RECSRC:
1167 ret = devc->recmask;
1168 break;
1170 case SOUND_MIXER_DEVMASK:
1171 ret = devc->mix->supported_devs;
1172 break;
1174 case SOUND_MIXER_STEREODEVS:
1175 ret = devc->mix->stereo_devs;
1176 break;
1178 case SOUND_MIXER_RECMASK:
1179 ret = devc->mix->recording_devs;
1180 break;
1182 case SOUND_MIXER_CAPS:
1183 ret = SOUND_CAP_EXCL_INPUT;
1184 break;
1186 default:
1187 if (nr < SOUND_MIXER_NRDEVICES)
1188 ret = devc->mix->get_mixer(devc, nr);
1189 break;
1192 if (ret >= 0)
1193 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1196 return ret;
1199 static struct mixer_operations waveartist_mixer_operations =
1201 .owner = THIS_MODULE,
1202 .id = "WaveArtist",
1203 .name = "WaveArtist",
1204 .ioctl = waveartist_mixer_ioctl
1207 static void
1208 waveartist_mixer_reset(wavnc_info *devc)
1210 int i;
1212 if (debug_flg & DEBUG_MIXER)
1213 printk("%s: mixer_reset\n", devc->hw.name);
1216 * reset mixer cmd
1218 waveartist_cmd1(devc, WACMD_RST_MIXER);
1221 * set input for ADC to come from 'quiet'
1222 * turn on default modes
1224 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1227 * set mixer input select to none, RX filter gains 0 dB
1229 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1232 * set bit 0 reg 2 to 1 - unmute MonoOut
1234 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1236 /* set default input device = internal mic
1237 * current recording device = none
1239 waveartist_set_recmask(devc, 0);
1241 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1242 waveartist_mixer_update(devc, i);
1245 static int __init waveartist_init(wavnc_info *devc)
1247 wavnc_port_info *portc;
1248 char rev[3], dev_name[64];
1249 int my_dev;
1251 if (waveartist_reset(devc))
1252 return -ENODEV;
1254 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1256 if (waveartist_getrev(devc, rev)) {
1257 strcat(dev_name, " rev. ");
1258 strcat(dev_name, rev);
1260 strcat(dev_name, ")");
1262 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1263 devc->hw.dma, devc->hw.dma2);
1265 portc = kzalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1266 if (portc == NULL)
1267 goto nomem;
1269 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1270 &waveartist_audio_driver, sizeof(struct audio_driver),
1271 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1272 devc, devc->hw.dma, devc->hw.dma2);
1274 if (my_dev < 0)
1275 goto free;
1277 audio_devs[my_dev]->portc = portc;
1279 waveartist_mixer_reset(devc);
1282 * clear any pending interrupt
1284 waveartist_iack(devc);
1286 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1287 printk(KERN_ERR "%s: IRQ %d in use\n",
1288 devc->hw.name, devc->hw.irq);
1289 goto uninstall;
1292 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1293 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1294 devc->hw.name, devc->hw.dma);
1295 goto uninstall_irq;
1298 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1299 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1300 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1301 devc->hw.name, devc->hw.dma2);
1302 goto uninstall_dma;
1305 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1307 audio_devs[my_dev]->mixer_dev =
1308 sound_install_mixer(MIXER_DRIVER_VERSION,
1309 dev_name,
1310 &waveartist_mixer_operations,
1311 sizeof(struct mixer_operations),
1312 devc);
1314 return my_dev;
1316 uninstall_dma:
1317 sound_free_dma(devc->hw.dma);
1319 uninstall_irq:
1320 free_irq(devc->hw.irq, devc);
1322 uninstall:
1323 sound_unload_audiodev(my_dev);
1325 free:
1326 kfree(portc);
1328 nomem:
1329 return -1;
1332 static int __init probe_waveartist(struct address_info *hw_config)
1334 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1336 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1337 printk(KERN_WARNING "waveartist: too many audio devices\n");
1338 return 0;
1341 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1342 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1343 return 0;
1346 if (hw_config->irq > 15 || hw_config->irq < 0) {
1347 release_region(hw_config->io_base, 15);
1348 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1349 hw_config->irq);
1350 return 0;
1353 if (hw_config->dma != 3) {
1354 release_region(hw_config->io_base, 15);
1355 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1356 hw_config->dma);
1357 return 0;
1360 hw_config->name = "WaveArtist";
1361 devc->hw = *hw_config;
1362 devc->open_mode = 0;
1363 devc->chip_name = "RWA-010";
1365 return 1;
1368 static void __init
1369 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1371 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1374 * NOTE! If irq < 0, there is another driver which has allocated the
1375 * IRQ so that this driver doesn't need to allocate/deallocate it.
1376 * The actually used IRQ is ABS(irq).
1378 devc->hw = *hw;
1379 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1380 devc->open_mode = 0;
1381 devc->playback_dev = 0;
1382 devc->record_dev = 0;
1383 devc->audio_flags = DMA_AUTOMODE;
1384 devc->levels = levels;
1386 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1387 devc->audio_flags |= DMA_DUPLEX;
1389 devc->mix = mix;
1390 devc->dev_no = waveartist_init(devc);
1392 if (devc->dev_no < 0)
1393 release_region(hw->io_base, 15);
1394 else {
1395 #ifdef CONFIG_ARCH_NETWINDER
1396 if (machine_is_netwinder()) {
1397 init_timer(&vnc_timer);
1398 vnc_timer.function = vnc_slider_tick;
1399 vnc_timer.expires = jiffies;
1400 vnc_timer.data = nr_waveartist_devs;
1401 add_timer(&vnc_timer);
1403 vnc_configure_mixer(devc, 0);
1405 devc->no_autoselect = 1;
1407 #endif
1408 nr_waveartist_devs += 1;
1412 static void __exit unload_waveartist(struct address_info *hw)
1414 wavnc_info *devc = NULL;
1415 int i;
1417 for (i = 0; i < nr_waveartist_devs; i++)
1418 if (hw->io_base == adev_info[i].hw.io_base) {
1419 devc = adev_info + i;
1420 break;
1423 if (devc != NULL) {
1424 int mixer;
1426 #ifdef CONFIG_ARCH_NETWINDER
1427 if (machine_is_netwinder())
1428 del_timer(&vnc_timer);
1429 #endif
1431 release_region(devc->hw.io_base, 15);
1433 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1435 if (devc->hw.irq >= 0)
1436 free_irq(devc->hw.irq, devc);
1438 sound_free_dma(devc->hw.dma);
1440 if (devc->hw.dma != devc->hw.dma2 &&
1441 devc->hw.dma2 != NO_DMA)
1442 sound_free_dma(devc->hw.dma2);
1444 mixer = audio_devs[devc->dev_no]->mixer_dev;
1446 if (mixer >= 0)
1447 sound_unload_mixerdev(mixer);
1449 if (devc->dev_no >= 0)
1450 sound_unload_audiodev(devc->dev_no);
1452 nr_waveartist_devs -= 1;
1454 for (; i < nr_waveartist_devs; i++)
1455 adev_info[i] = adev_info[i + 1];
1456 } else
1457 printk(KERN_WARNING "waveartist: can't find device "
1458 "to unload\n");
1461 #ifdef CONFIG_ARCH_NETWINDER
1464 * Rebel.com Netwinder specifics...
1467 #include <asm/hardware/dec21285.h>
1469 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1471 #define MIXER_PRIVATE3_RESET 0x53570000
1472 #define MIXER_PRIVATE3_READ 0x53570001
1473 #define MIXER_PRIVATE3_WRITE 0x53570002
1475 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1476 #define VNC_MUTE_LINE_OUT 0x10
1477 #define VNC_PHONE_DETECT 0x20
1478 #define VNC_HANDSET_DETECT 0x40
1479 #define VNC_DISABLE_AUTOSWITCH 0x80
1481 static inline void
1482 vnc_mute_spkr(wavnc_info *devc)
1484 unsigned long flags;
1486 spin_lock_irqsave(&nw_gpio_lock, flags);
1487 nw_cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1488 spin_unlock_irqrestore(&nw_gpio_lock, flags);
1491 static void
1492 vnc_mute_lout(wavnc_info *devc)
1494 unsigned int left, right;
1496 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1497 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1499 if (devc->line_mute_state) {
1500 left &= ~1;
1501 right &= ~1;
1502 } else {
1503 left |= 1;
1504 right |= 1;
1506 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1510 static int
1511 vnc_volume_slider(wavnc_info *devc)
1513 static signed int old_slider_volume;
1514 unsigned long flags;
1515 signed int volume = 255;
1517 *CSR_TIMER1_LOAD = 0x00ffffff;
1519 spin_lock_irqsave(&waveartist_lock, flags);
1521 outb(0xFF, 0x201);
1522 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1524 while (volume && (inb(0x201) & 0x01))
1525 volume--;
1527 *CSR_TIMER1_CNTL = 0;
1529 spin_unlock_irqrestore(&waveartist_lock,flags);
1531 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1534 #ifndef REVERSE
1535 volume = 150 - (volume >> 5);
1536 #else
1537 volume = (volume >> 6) - 25;
1538 #endif
1540 if (volume < 0)
1541 volume = 0;
1543 if (volume > 100)
1544 volume = 100;
1547 * slider quite often reads +-8, so debounce this random noise
1549 if (abs(volume - old_slider_volume) > 7) {
1550 old_slider_volume = volume;
1552 if (debug_flg & DEBUG_MIXER)
1553 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1556 return old_slider_volume;
1560 * Decode a recording mask into a mixer selection on the NetWinder
1561 * as follows:
1563 * OSS Source WA Source Actual source
1564 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1565 * SOUND_MASK_LINE Line Line in
1566 * SOUND_MASK_LINE1 Left Mic Handset
1567 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1568 * SOUND_MASK_MIC Right Mic Builtin microphone
1570 static unsigned int
1571 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1572 unsigned char *dev_l, unsigned char *dev_r)
1574 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1576 if (recmask & SOUND_MASK_IMIX) {
1577 recmask = SOUND_MASK_IMIX;
1578 recdev_l = ADC_MUX_MIXER;
1579 recdev_r = ADC_MUX_MIXER;
1580 } else if (recmask & SOUND_MASK_LINE) {
1581 recmask = SOUND_MASK_LINE;
1582 recdev_l = ADC_MUX_LINE;
1583 recdev_r = ADC_MUX_LINE;
1584 } else if (recmask & SOUND_MASK_LINE1) {
1585 recmask = SOUND_MASK_LINE1;
1586 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1587 recdev_l = ADC_MUX_MIC;
1588 recdev_r = ADC_MUX_NONE;
1589 } else if (recmask & SOUND_MASK_PHONEIN) {
1590 recmask = SOUND_MASK_PHONEIN;
1591 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1592 recdev_l = ADC_MUX_AUX1;
1593 recdev_r = ADC_MUX_NONE;
1594 } else if (recmask & SOUND_MASK_MIC) {
1595 recmask = SOUND_MASK_MIC;
1596 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1597 recdev_l = ADC_MUX_NONE;
1598 recdev_r = ADC_MUX_MIC;
1601 *dev_l = recdev_l;
1602 *dev_r = recdev_r;
1604 return recmask;
1607 static int
1608 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1609 unsigned char lev_r)
1611 switch (dev) {
1612 case SOUND_MIXER_VOLUME:
1613 case SOUND_MIXER_SYNTH:
1614 case SOUND_MIXER_PCM:
1615 case SOUND_MIXER_LINE:
1616 case SOUND_MIXER_IGAIN:
1617 devc->levels[dev] = lev_l | lev_r << 8;
1618 break;
1620 case SOUND_MIXER_MIC: /* right mic only */
1621 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1622 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1623 break;
1625 case SOUND_MIXER_LINE1: /* left mic only */
1626 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1627 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1628 dev = SOUND_MIXER_MIC;
1629 break;
1631 case SOUND_MIXER_PHONEIN: /* left aux only */
1632 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1633 dev = SOUND_MIXER_LINE1;
1634 break;
1636 case SOUND_MIXER_IMIX:
1637 case SOUND_MIXER_PHONEOUT:
1638 break;
1640 default:
1641 dev = -EINVAL;
1642 break;
1644 return dev;
1647 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1649 int levels;
1651 switch (dev) {
1652 case SOUND_MIXER_VOLUME:
1653 case SOUND_MIXER_SYNTH:
1654 case SOUND_MIXER_PCM:
1655 case SOUND_MIXER_LINE:
1656 case SOUND_MIXER_IGAIN:
1657 levels = devc->levels[dev];
1658 break;
1660 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1661 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1662 levels |= levels << 8;
1663 break;
1665 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1666 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1667 levels |= levels << 8;
1668 break;
1670 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1671 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1672 levels |= levels << 8;
1673 break;
1675 default:
1676 levels = 0;
1679 return levels;
1683 * Waveartist specific mixer information.
1685 static const struct waveartist_mixer_info netwinder_mixer = {
1686 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1687 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1688 SOUND_MASK_LINE | SOUND_MASK_MIC |
1689 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1690 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1691 SOUND_MASK_IGAIN,
1693 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1694 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1695 SOUND_MASK_PHONEIN,
1697 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1698 SOUND_MASK_PCM | SOUND_MASK_LINE |
1699 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1701 .select_input = netwinder_select_input,
1702 .decode_mixer = netwinder_decode_mixer,
1703 .get_mixer = netwinder_get_mixer,
1706 static void
1707 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1709 if (!devc->no_autoselect) {
1710 if (devc->handset_detect) {
1711 recmask = SOUND_MASK_LINE1;
1712 devc->spkr_mute_state = devc->line_mute_state = 1;
1713 } else if (devc->telephone_detect) {
1714 recmask = SOUND_MASK_PHONEIN;
1715 devc->spkr_mute_state = devc->line_mute_state = 1;
1716 } else {
1717 /* unless someone has asked for LINE-IN,
1718 * we default to MIC
1720 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1721 devc->recmask = SOUND_MASK_MIC;
1722 devc->spkr_mute_state = devc->line_mute_state = 0;
1724 vnc_mute_spkr(devc);
1725 vnc_mute_lout(devc);
1727 if (recmask != devc->recmask)
1728 waveartist_set_recmask(devc, recmask);
1732 static int
1733 vnc_slider(wavnc_info *devc)
1735 signed int slider_volume;
1736 unsigned int temp, old_hs, old_td;
1739 * read the "buttons" state.
1740 * Bit 4 = 0 means handset present
1741 * Bit 5 = 1 means phone offhook
1743 temp = inb(0x201);
1745 old_hs = devc->handset_detect;
1746 old_td = devc->telephone_detect;
1748 devc->handset_detect = !(temp & 0x10);
1749 devc->telephone_detect = !!(temp & 0x20);
1751 if (!devc->no_autoselect &&
1752 (old_hs != devc->handset_detect ||
1753 old_td != devc->telephone_detect))
1754 vnc_configure_mixer(devc, devc->recmask);
1756 slider_volume = vnc_volume_slider(devc);
1759 * If we're using software controlled volume, and
1760 * the slider moves by more than 20%, then we
1761 * switch back to slider controlled volume.
1763 if (abs(devc->slider_vol - slider_volume) > 20)
1764 devc->use_slider = 1;
1767 * use only left channel
1769 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1771 if (slider_volume != temp && devc->use_slider) {
1772 devc->slider_vol = slider_volume;
1774 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1775 slider_volume | slider_volume << 8);
1777 return 1;
1780 return 0;
1783 static void
1784 vnc_slider_tick(unsigned long data)
1786 int next_timeout;
1788 if (vnc_slider(adev_info + data))
1789 next_timeout = 5; // mixer reported change
1790 else
1791 next_timeout = VNC_TIMER_PERIOD;
1793 mod_timer(&vnc_timer, jiffies + next_timeout);
1796 static int
1797 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1799 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1800 int val;
1802 switch (cmd) {
1803 case SOUND_MIXER_PRIVATE1:
1805 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1806 int val;
1808 if (get_user(val, arg))
1809 return -EFAULT;
1811 /* check if parameter is logical */
1812 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1813 VNC_MUTE_LINE_OUT |
1814 VNC_DISABLE_AUTOSWITCH))
1815 return -EINVAL;
1817 prev_auto_state = devc->no_autoselect;
1818 prev_spkr_mute = devc->spkr_mute_state;
1819 prev_line_mute = devc->line_mute_state;
1821 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1822 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1823 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1825 if (prev_spkr_mute != devc->spkr_mute_state)
1826 vnc_mute_spkr(devc);
1828 if (prev_line_mute != devc->line_mute_state)
1829 vnc_mute_lout(devc);
1831 if (prev_auto_state != devc->no_autoselect)
1832 vnc_configure_mixer(devc, devc->recmask);
1834 return 0;
1837 case SOUND_MIXER_PRIVATE2:
1838 if (get_user(val, arg))
1839 return -EFAULT;
1841 switch (val) {
1842 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1843 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1844 case VNC_SOUND_PAUSE:
1845 waveartist_cmd1(devc, 0x16);
1846 break;
1848 case VNC_SOUND_RESUME:
1849 waveartist_cmd1(devc, 0x18);
1850 break;
1852 default:
1853 return -EINVAL;
1855 return 0;
1857 /* private ioctl to allow bulk access to waveartist */
1858 case SOUND_MIXER_PRIVATE3:
1860 unsigned long flags;
1861 int mixer_reg[15], i, val;
1863 if (get_user(val, arg))
1864 return -EFAULT;
1865 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1866 return -EFAULT;
1868 switch (mixer_reg[14]) {
1869 case MIXER_PRIVATE3_RESET:
1870 waveartist_mixer_reset(devc);
1871 break;
1873 case MIXER_PRIVATE3_WRITE:
1874 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1875 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1876 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1877 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1878 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1880 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1881 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1882 break;
1884 case MIXER_PRIVATE3_READ:
1885 spin_lock_irqsave(&waveartist_lock, flags);
1887 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1888 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1890 spin_unlock_irqrestore(&waveartist_lock, flags);
1892 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1893 return -EFAULT;
1894 break;
1896 default:
1897 return -EINVAL;
1899 return 0;
1902 /* read back the state from PRIVATE1 */
1903 case SOUND_MIXER_PRIVATE4:
1904 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1905 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1906 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1907 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1908 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1910 return put_user(val, arg) ? -EFAULT : 0;
1913 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1915 * special case for master volume: if we
1916 * received this call - switch from hw
1917 * volume control to a software volume
1918 * control, till the hw volume is modified
1919 * to signal that user wants to be back in
1920 * hardware...
1922 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1923 devc->use_slider = 0;
1925 /* speaker output */
1926 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1927 unsigned int val, l, r;
1929 if (get_user(val, arg))
1930 return -EFAULT;
1932 l = val & 0x7f;
1933 r = (val & 0x7f00) >> 8;
1934 val = (l + r) / 2;
1935 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1936 devc->spkr_mute_state = (val <= 50);
1937 vnc_mute_spkr(devc);
1938 return 0;
1942 return -ENOIOCTLCMD;
1945 #endif
1947 static struct address_info cfg;
1949 static int attached;
1951 static int __initdata io = 0;
1952 static int __initdata irq = 0;
1953 static int __initdata dma = 0;
1954 static int __initdata dma2 = 0;
1957 static int __init init_waveartist(void)
1959 const struct waveartist_mixer_info *mix;
1961 if (!io && machine_is_netwinder()) {
1963 * The NetWinder WaveArtist is at a fixed address.
1964 * If the user does not supply an address, use the
1965 * well-known parameters.
1967 io = 0x250;
1968 irq = 12;
1969 dma = 3;
1970 dma2 = 7;
1973 mix = &waveartist_mixer;
1974 #ifdef CONFIG_ARCH_NETWINDER
1975 if (machine_is_netwinder())
1976 mix = &netwinder_mixer;
1977 #endif
1979 cfg.io_base = io;
1980 cfg.irq = irq;
1981 cfg.dma = dma;
1982 cfg.dma2 = dma2;
1984 if (!probe_waveartist(&cfg))
1985 return -ENODEV;
1987 attach_waveartist(&cfg, mix);
1988 attached = 1;
1990 return 0;
1993 static void __exit cleanup_waveartist(void)
1995 if (attached)
1996 unload_waveartist(&cfg);
1999 module_init(init_waveartist);
2000 module_exit(cleanup_waveartist);
2002 #ifndef MODULE
2003 static int __init setup_waveartist(char *str)
2005 /* io, irq, dma, dma2 */
2006 int ints[5];
2008 str = get_options(str, ARRAY_SIZE(ints), ints);
2010 io = ints[1];
2011 irq = ints[2];
2012 dma = ints[3];
2013 dma2 = ints[4];
2015 return 1;
2017 __setup("waveartist=", setup_waveartist);
2018 #endif
2020 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2021 module_param(io, int, 0); /* IO base */
2022 module_param(irq, int, 0); /* IRQ */
2023 module_param(dma, int, 0); /* DMA */
2024 module_param(dma2, int, 0); /* DMA2 */
2025 MODULE_LICENSE("GPL");