writeback: split writeback_inodes_wb
[linux-2.6/next.git] / sound / oss / waveartist.c
blobe688dde6bbdeb0b54ae26c761eaba38b1ede5a95
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 = timeout_ms * 10 * HZ / 100;
189 do {
190 set_current_state(TASK_INTERRUPTIBLE);
191 timeout = schedule_timeout(timeout);
192 } while (timeout);
194 return 0;
197 static int
198 waveartist_reset(wavnc_info *devc)
200 struct address_info *hw = &devc->hw;
201 unsigned int timeout, res = -1;
203 waveartist_set_ctlr(hw, -1, RESET);
204 waveartist_sleep(2);
205 waveartist_set_ctlr(hw, RESET, 0);
207 timeout = 500;
208 do {
209 mdelay(2);
211 if (inb(hw->io_base + STATR) & CMD_RF) {
212 res = inw(hw->io_base + CMDR);
213 if (res == 0x55aa)
214 break;
216 } while (--timeout);
218 if (timeout == 0) {
219 printk(KERN_WARNING "WaveArtist: reset timeout ");
220 if (res != (unsigned int)-1)
221 printk("(res=%04X)", res);
222 printk("\n");
223 return 1;
225 return 0;
228 /* Helper function to send and receive words
229 * from WaveArtist. It handles all the handshaking
230 * and can send or receive multiple words.
232 static int
233 waveartist_cmd(wavnc_info *devc,
234 int nr_cmd, unsigned int *cmd,
235 int nr_resp, unsigned int *resp)
237 unsigned int io_base = devc->hw.io_base;
238 unsigned int timed_out = 0;
239 unsigned int i;
241 if (debug_flg & DEBUG_CMD) {
242 printk("waveartist_cmd: cmd=");
244 for (i = 0; i < nr_cmd; i++)
245 printk("%04X ", cmd[i]);
247 printk("\n");
250 if (inb(io_base + STATR) & CMD_RF) {
251 int old_data;
253 /* flush the port
256 old_data = inw(io_base + CMDR);
258 if (debug_flg & DEBUG_CMD)
259 printk("flushed %04X...", old_data);
261 udelay(10);
264 for (i = 0; !timed_out && i < nr_cmd; i++) {
265 int count;
267 for (count = 5000; count; count--)
268 if (inb(io_base + STATR) & CMD_WE)
269 break;
271 if (!count)
272 timed_out = 1;
273 else
274 outw(cmd[i], io_base + CMDR);
277 for (i = 0; !timed_out && i < nr_resp; i++) {
278 int count;
280 for (count = 5000; count; count--)
281 if (inb(io_base + STATR) & CMD_RF)
282 break;
284 if (!count)
285 timed_out = 1;
286 else
287 resp[i] = inw(io_base + CMDR);
290 if (debug_flg & DEBUG_CMD) {
291 if (!timed_out) {
292 printk("waveartist_cmd: resp=");
294 for (i = 0; i < nr_resp; i++)
295 printk("%04X ", resp[i]);
297 printk("\n");
298 } else
299 printk("waveartist_cmd: timed out\n");
302 return timed_out ? 1 : 0;
306 * Send one command word
308 static inline int
309 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
311 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
315 * Send one command, receive one word
317 static inline unsigned int
318 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
320 unsigned int ret;
322 waveartist_cmd(devc, 1, &cmd, 1, &ret);
324 return ret;
328 * Send a double command, receive one
329 * word (and throw it away)
331 static inline int
332 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
334 unsigned int vals[2];
336 vals[0] = cmd;
337 vals[1] = arg;
339 return waveartist_cmd(devc, 2, vals, 1, vals);
343 * Send a triple command
345 static inline int
346 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
347 unsigned int arg1, unsigned int arg2)
349 unsigned int vals[3];
351 vals[0] = cmd;
352 vals[1] = arg1;
353 vals[2] = arg2;
355 return waveartist_cmd(devc, 3, vals, 0, NULL);
358 static int
359 waveartist_getrev(wavnc_info *devc, char *rev)
361 unsigned int temp[2];
362 unsigned int cmd = WACMD_GETREV;
364 waveartist_cmd(devc, 1, &cmd, 2, temp);
366 rev[0] = temp[0] >> 8;
367 rev[1] = temp[0] & 255;
368 rev[2] = '\0';
370 return temp[0];
373 static void waveartist_halt_output(int dev);
374 static void waveartist_halt_input(int dev);
375 static void waveartist_halt(int dev);
376 static void waveartist_trigger(int dev, int state);
378 static int
379 waveartist_open(int dev, int mode)
381 wavnc_info *devc;
382 wavnc_port_info *portc;
383 unsigned long flags;
385 if (dev < 0 || dev >= num_audiodevs)
386 return -ENXIO;
388 devc = (wavnc_info *) audio_devs[dev]->devc;
389 portc = (wavnc_port_info *) audio_devs[dev]->portc;
391 spin_lock_irqsave(&waveartist_lock, flags);
392 if (portc->open_mode || (devc->open_mode & mode)) {
393 spin_unlock_irqrestore(&waveartist_lock, flags);
394 return -EBUSY;
397 devc->audio_mode = 0;
398 devc->open_mode |= mode;
399 portc->open_mode = mode;
400 waveartist_trigger(dev, 0);
402 if (mode & OPEN_READ)
403 devc->record_dev = dev;
404 if (mode & OPEN_WRITE)
405 devc->playback_dev = dev;
406 spin_unlock_irqrestore(&waveartist_lock, flags);
408 return 0;
411 static void
412 waveartist_close(int dev)
414 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
415 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
416 unsigned long flags;
418 spin_lock_irqsave(&waveartist_lock, flags);
420 waveartist_halt(dev);
422 devc->audio_mode = 0;
423 devc->open_mode &= ~portc->open_mode;
424 portc->open_mode = 0;
426 spin_unlock_irqrestore(&waveartist_lock, flags);
429 static void
430 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
432 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
433 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
434 unsigned long flags;
435 unsigned int count = __count;
437 if (debug_flg & DEBUG_OUT)
438 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
439 buf, count);
441 * 16 bit data
443 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
444 count >>= 1;
446 if (portc->channels > 1)
447 count >>= 1;
449 count -= 1;
451 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
452 audio_devs[dev]->flags & DMA_AUTOMODE &&
453 intrflag &&
454 count == devc->xfer_count) {
455 devc->audio_mode |= PCM_ENABLE_OUTPUT;
456 return; /*
457 * Auto DMA mode on. No need to react
461 spin_lock_irqsave(&waveartist_lock, flags);
464 * set sample count
466 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
468 devc->xfer_count = count;
469 devc->audio_mode |= PCM_ENABLE_OUTPUT;
471 spin_unlock_irqrestore(&waveartist_lock, flags);
474 static void
475 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
477 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
478 wavnc_info *devc = (wavnc_info *) 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(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(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 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
571 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
572 unsigned int speed, bits;
574 if (devc->audio_mode)
575 return 0;
577 speed = waveartist_get_speed(portc);
578 bits = waveartist_get_bits(portc);
580 spin_lock_irqsave(&waveartist_lock, flags);
582 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
583 printk(KERN_WARNING "waveartist: error setting the "
584 "record format to %d\n", portc->audio_format);
586 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
587 printk(KERN_WARNING "waveartist: error setting record "
588 "to %d channels\n", portc->channels);
591 * write cmd SetSampleSpeedTimeConstant
593 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
594 printk(KERN_WARNING "waveartist: error setting the record "
595 "speed to %dHz.\n", portc->speed);
597 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
598 printk(KERN_WARNING "waveartist: error setting the record "
599 "data path to 0x%X\n", 1);
601 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
602 printk(KERN_WARNING "waveartist: error setting the record "
603 "format to %d\n", portc->audio_format);
605 devc->xfer_count = 0;
606 spin_unlock_irqrestore(&waveartist_lock, flags);
607 waveartist_halt_input(dev);
609 if (debug_flg & DEBUG_INTR) {
610 printk("WA CTLR reg: 0x%02X.\n",
611 inb(devc->hw.io_base + CTLR));
612 printk("WA STAT reg: 0x%02X.\n",
613 inb(devc->hw.io_base + STATR));
614 printk("WA IRQS reg: 0x%02X.\n",
615 inb(devc->hw.io_base + IRQSTAT));
618 return 0;
621 static int
622 waveartist_prepare_for_output(int dev, int bsize, int bcount)
624 unsigned long flags;
625 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
626 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
627 unsigned int speed, bits;
630 * program the speed, channels, bits
632 speed = waveartist_get_speed(portc);
633 bits = waveartist_get_bits(portc);
635 spin_lock_irqsave(&waveartist_lock, flags);
637 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
638 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
639 printk(KERN_WARNING "waveartist: error setting the playback "
640 "speed to %dHz.\n", portc->speed);
642 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
643 printk(KERN_WARNING "waveartist: error setting the playback "
644 "to %d channels\n", portc->channels);
646 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
647 printk(KERN_WARNING "waveartist: error setting the playback "
648 "data path to 0x%X\n", 0);
650 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
651 printk(KERN_WARNING "waveartist: error setting the playback "
652 "format to %d\n", portc->audio_format);
654 devc->xfer_count = 0;
655 spin_unlock_irqrestore(&waveartist_lock, flags);
656 waveartist_halt_output(dev);
658 if (debug_flg & DEBUG_INTR) {
659 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
660 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
661 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
664 return 0;
667 static void
668 waveartist_halt(int dev)
670 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
671 wavnc_info *devc;
673 if (portc->open_mode & OPEN_WRITE)
674 waveartist_halt_output(dev);
676 if (portc->open_mode & OPEN_READ)
677 waveartist_halt_input(dev);
679 devc = (wavnc_info *) audio_devs[dev]->devc;
680 devc->audio_mode = 0;
683 static void
684 waveartist_halt_input(int dev)
686 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
687 unsigned long flags;
689 spin_lock_irqsave(&waveartist_lock, flags);
692 * Stop capture
694 waveartist_cmd1(devc, WACMD_INPUTSTOP);
696 devc->audio_mode &= ~PCM_ENABLE_INPUT;
699 * Clear interrupt by toggling
700 * the IRQ_ACK bit in CTRL
702 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
703 waveartist_iack(devc);
705 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
707 spin_unlock_irqrestore(&waveartist_lock, flags);
710 static void
711 waveartist_halt_output(int dev)
713 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
714 unsigned long flags;
716 spin_lock_irqsave(&waveartist_lock, flags);
718 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
720 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
723 * Clear interrupt by toggling
724 * the IRQ_ACK bit in CTRL
726 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
727 waveartist_iack(devc);
729 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
731 spin_unlock_irqrestore(&waveartist_lock, flags);
734 static void
735 waveartist_trigger(int dev, int state)
737 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
738 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
739 unsigned long flags;
741 if (debug_flg & DEBUG_TRIGGER) {
742 printk("wavnc: audio trigger ");
743 if (state & PCM_ENABLE_INPUT)
744 printk("in ");
745 if (state & PCM_ENABLE_OUTPUT)
746 printk("out");
747 printk("\n");
750 spin_lock_irqsave(&waveartist_lock, flags);
752 state &= devc->audio_mode;
754 if (portc->open_mode & OPEN_READ &&
755 state & PCM_ENABLE_INPUT)
757 * enable ADC Data Transfer to PC
759 waveartist_cmd1(devc, WACMD_INPUTSTART);
761 if (portc->open_mode & OPEN_WRITE &&
762 state & PCM_ENABLE_OUTPUT)
764 * enable DAC data transfer from PC
766 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
768 spin_unlock_irqrestore(&waveartist_lock, flags);
771 static int
772 waveartist_set_speed(int dev, int arg)
774 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
776 if (arg <= 0)
777 return portc->speed;
779 if (arg < 5000)
780 arg = 5000;
781 if (arg > 44100)
782 arg = 44100;
784 portc->speed = arg;
785 return portc->speed;
789 static short
790 waveartist_set_channels(int dev, short arg)
792 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
794 if (arg != 1 && arg != 2)
795 return portc->channels;
797 portc->channels = arg;
798 return arg;
801 static unsigned int
802 waveartist_set_bits(int dev, unsigned int arg)
804 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
806 if (arg == 0)
807 return portc->audio_format;
809 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
810 arg = AFMT_U8;
812 portc->audio_format = arg;
814 return arg;
817 static struct audio_driver waveartist_audio_driver = {
818 .owner = THIS_MODULE,
819 .open = waveartist_open,
820 .close = waveartist_close,
821 .output_block = waveartist_output_block,
822 .start_input = waveartist_start_input,
823 .ioctl = waveartist_ioctl,
824 .prepare_for_input = waveartist_prepare_for_input,
825 .prepare_for_output = waveartist_prepare_for_output,
826 .halt_io = waveartist_halt,
827 .halt_input = waveartist_halt_input,
828 .halt_output = waveartist_halt_output,
829 .trigger = waveartist_trigger,
830 .set_speed = waveartist_set_speed,
831 .set_bits = waveartist_set_bits,
832 .set_channels = waveartist_set_channels
836 static irqreturn_t
837 waveartist_intr(int irq, void *dev_id)
839 wavnc_info *devc = dev_id;
840 int irqstatus, status;
842 spin_lock(&waveartist_lock);
843 irqstatus = inb(devc->hw.io_base + IRQSTAT);
844 status = inb(devc->hw.io_base + STATR);
846 if (debug_flg & DEBUG_INTR)
847 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
848 status, irqstatus);
850 if (status & IRQ_REQ) /* Clear interrupt */
851 waveartist_iack(devc);
852 else
853 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
855 if (irqstatus & 0x01) {
856 int temp = 1;
858 /* PCM buffer done
860 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
861 DMAbuf_outputintr(devc->playback_dev, 1);
862 temp = 0;
864 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
865 DMAbuf_inputintr(devc->record_dev);
866 temp = 0;
868 if (temp) //default:
869 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
871 if (irqstatus & 0x2)
872 // We do not use SB mode natively...
873 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
874 spin_unlock(&waveartist_lock);
875 return IRQ_HANDLED;
878 /* -------------------------------------------------------------------------
879 * Mixer stuff
881 struct mix_ent {
882 unsigned char reg_l;
883 unsigned char reg_r;
884 unsigned char shift;
885 unsigned char max;
888 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
889 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
890 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
893 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
894 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
895 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
896 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
897 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
898 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
899 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
900 #if 0
901 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
902 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
903 #else
904 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
905 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
906 #endif
907 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
908 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
909 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
912 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
913 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
914 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
915 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
916 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
917 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
918 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
921 static void
922 waveartist_mixer_update(wavnc_info *devc, int whichDev)
924 unsigned int lev_left, lev_right;
926 lev_left = devc->levels[whichDev] & 0xff;
927 lev_right = devc->levels[whichDev] >> 8;
929 if (lev_left > 100)
930 lev_left = 100;
931 if (lev_right > 100)
932 lev_right = 100;
934 #define SCALE(lev,max) ((lev) * (max) / 100)
936 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
937 whichDev = SOUND_MIXER_VOLUME;
939 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
940 const struct mix_ent *mix = mix_devs + whichDev;
941 unsigned int mask, left, right;
943 mask = mix->max << mix->shift;
944 lev_left = SCALE(lev_left, mix->max) << mix->shift;
945 lev_right = SCALE(lev_right, mix->max) << mix->shift;
947 /* read left setting */
948 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
949 mix->reg_l << 8);
951 /* read right setting */
952 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
953 mix->reg_r << 8);
955 left = (left & ~mask) | (lev_left & mask);
956 right = (right & ~mask) | (lev_right & mask);
958 /* write left,right back */
959 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
960 } else {
961 switch(whichDev) {
962 case SOUND_MIXER_PCM:
963 waveartist_cmd3(devc, WACMD_SET_LEVEL,
964 SCALE(lev_left, 32767),
965 SCALE(lev_right, 32767));
966 break;
968 case SOUND_MIXER_SYNTH:
969 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
970 SCALE(lev_left, 32767),
971 SCALE(lev_right, 32767));
972 break;
978 * Set the ADC MUX to the specified values. We do NOT do any
979 * checking of the values passed, since we assume that the
980 * relevant *_select_input function has done that for us.
982 static void
983 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
985 unsigned int reg_08, reg_09;
987 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
988 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
990 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
992 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
996 * Decode a recording mask into a mixer selection as follows:
998 * OSS Source WA Source Actual source
999 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1000 * SOUND_MASK_LINE Line Line in
1001 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
1002 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
1003 * SOUND_MASK_MIC Mic Microphone
1005 static unsigned int
1006 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1007 unsigned char *dev_l, unsigned char *dev_r)
1009 unsigned int recdev = ADC_MUX_NONE;
1011 if (recmask & SOUND_MASK_IMIX) {
1012 recmask = SOUND_MASK_IMIX;
1013 recdev = ADC_MUX_MIXER;
1014 } else if (recmask & SOUND_MASK_LINE2) {
1015 recmask = SOUND_MASK_LINE2;
1016 recdev = ADC_MUX_AUX2;
1017 } else if (recmask & SOUND_MASK_LINE1) {
1018 recmask = SOUND_MASK_LINE1;
1019 recdev = ADC_MUX_AUX1;
1020 } else if (recmask & SOUND_MASK_LINE) {
1021 recmask = SOUND_MASK_LINE;
1022 recdev = ADC_MUX_LINE;
1023 } else if (recmask & SOUND_MASK_MIC) {
1024 recmask = SOUND_MASK_MIC;
1025 recdev = ADC_MUX_MIC;
1028 *dev_l = *dev_r = recdev;
1030 return recmask;
1033 static int
1034 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1035 unsigned char lev_r)
1037 switch (dev) {
1038 case SOUND_MIXER_VOLUME:
1039 case SOUND_MIXER_SYNTH:
1040 case SOUND_MIXER_PCM:
1041 case SOUND_MIXER_LINE:
1042 case SOUND_MIXER_MIC:
1043 case SOUND_MIXER_IGAIN:
1044 case SOUND_MIXER_LINE1:
1045 case SOUND_MIXER_LINE2:
1046 devc->levels[dev] = lev_l | lev_r << 8;
1047 break;
1049 case SOUND_MIXER_IMIX:
1050 break;
1052 default:
1053 dev = -EINVAL;
1054 break;
1057 return dev;
1060 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1062 return devc->levels[dev];
1065 static const struct waveartist_mixer_info waveartist_mixer = {
1066 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1067 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1068 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1069 SOUND_MASK_IMIX,
1070 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1071 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1072 .select_input = waveartist_select_input,
1073 .decode_mixer = waveartist_decode_mixer,
1074 .get_mixer = waveartist_get_mixer,
1077 static void
1078 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1080 unsigned char dev_l, dev_r;
1082 recmask &= devc->mix->recording_devs;
1085 * If more than one recording device selected,
1086 * disable the device that is currently in use.
1088 if (hweight32(recmask) > 1)
1089 recmask &= ~devc->recmask;
1092 * Translate the recording device mask into
1093 * the ADC multiplexer settings.
1095 devc->recmask = devc->mix->select_input(devc, recmask,
1096 &dev_l, &dev_r);
1098 waveartist_set_adc_mux(devc, dev_l, dev_r);
1101 static int
1102 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1104 unsigned int lev_left = level & 0x00ff;
1105 unsigned int lev_right = (level & 0xff00) >> 8;
1107 if (lev_left > 100)
1108 lev_left = 100;
1109 if (lev_right > 100)
1110 lev_right = 100;
1113 * Mono devices have their right volume forced to their
1114 * left volume. (from ALSA driver OSS emulation).
1116 if (!(devc->mix->stereo_devs & (1 << dev)))
1117 lev_right = lev_left;
1119 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1121 if (dev >= 0)
1122 waveartist_mixer_update(devc, dev);
1124 return dev < 0 ? dev : 0;
1127 static int
1128 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1130 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1131 int ret = 0, val, nr;
1134 * All SOUND_MIXER_* ioctls use type 'M'
1136 if (((cmd >> 8) & 255) != 'M')
1137 return -ENOIOCTLCMD;
1139 #ifdef CONFIG_ARCH_NETWINDER
1140 if (machine_is_netwinder()) {
1141 ret = vnc_private_ioctl(dev, cmd, arg);
1142 if (ret != -ENOIOCTLCMD)
1143 return ret;
1144 else
1145 ret = 0;
1147 #endif
1149 nr = cmd & 0xff;
1151 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1152 if (get_user(val, (int __user *)arg))
1153 return -EFAULT;
1155 switch (nr) {
1156 case SOUND_MIXER_RECSRC:
1157 waveartist_set_recmask(devc, val);
1158 break;
1160 default:
1161 ret = -EINVAL;
1162 if (nr < SOUND_MIXER_NRDEVICES &&
1163 devc->mix->supported_devs & (1 << nr))
1164 ret = waveartist_set_mixer(devc, nr, val);
1168 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1169 ret = -EINVAL;
1171 switch (nr) {
1172 case SOUND_MIXER_RECSRC:
1173 ret = devc->recmask;
1174 break;
1176 case SOUND_MIXER_DEVMASK:
1177 ret = devc->mix->supported_devs;
1178 break;
1180 case SOUND_MIXER_STEREODEVS:
1181 ret = devc->mix->stereo_devs;
1182 break;
1184 case SOUND_MIXER_RECMASK:
1185 ret = devc->mix->recording_devs;
1186 break;
1188 case SOUND_MIXER_CAPS:
1189 ret = SOUND_CAP_EXCL_INPUT;
1190 break;
1192 default:
1193 if (nr < SOUND_MIXER_NRDEVICES)
1194 ret = devc->mix->get_mixer(devc, nr);
1195 break;
1198 if (ret >= 0)
1199 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1202 return ret;
1205 static struct mixer_operations waveartist_mixer_operations =
1207 .owner = THIS_MODULE,
1208 .id = "WaveArtist",
1209 .name = "WaveArtist",
1210 .ioctl = waveartist_mixer_ioctl
1213 static void
1214 waveartist_mixer_reset(wavnc_info *devc)
1216 int i;
1218 if (debug_flg & DEBUG_MIXER)
1219 printk("%s: mixer_reset\n", devc->hw.name);
1222 * reset mixer cmd
1224 waveartist_cmd1(devc, WACMD_RST_MIXER);
1227 * set input for ADC to come from 'quiet'
1228 * turn on default modes
1230 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1233 * set mixer input select to none, RX filter gains 0 dB
1235 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1238 * set bit 0 reg 2 to 1 - unmute MonoOut
1240 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1242 /* set default input device = internal mic
1243 * current recording device = none
1245 waveartist_set_recmask(devc, 0);
1247 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1248 waveartist_mixer_update(devc, i);
1251 static int __init waveartist_init(wavnc_info *devc)
1253 wavnc_port_info *portc;
1254 char rev[3], dev_name[64];
1255 int my_dev;
1257 if (waveartist_reset(devc))
1258 return -ENODEV;
1260 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1262 if (waveartist_getrev(devc, rev)) {
1263 strcat(dev_name, " rev. ");
1264 strcat(dev_name, rev);
1266 strcat(dev_name, ")");
1268 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1269 devc->hw.dma, devc->hw.dma2);
1271 portc = kzalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1272 if (portc == NULL)
1273 goto nomem;
1275 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1276 &waveartist_audio_driver, sizeof(struct audio_driver),
1277 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1278 devc, devc->hw.dma, devc->hw.dma2);
1280 if (my_dev < 0)
1281 goto free;
1283 audio_devs[my_dev]->portc = portc;
1285 waveartist_mixer_reset(devc);
1288 * clear any pending interrupt
1290 waveartist_iack(devc);
1292 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1293 printk(KERN_ERR "%s: IRQ %d in use\n",
1294 devc->hw.name, devc->hw.irq);
1295 goto uninstall;
1298 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1299 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1300 devc->hw.name, devc->hw.dma);
1301 goto uninstall_irq;
1304 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1305 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1306 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1307 devc->hw.name, devc->hw.dma2);
1308 goto uninstall_dma;
1311 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1313 audio_devs[my_dev]->mixer_dev =
1314 sound_install_mixer(MIXER_DRIVER_VERSION,
1315 dev_name,
1316 &waveartist_mixer_operations,
1317 sizeof(struct mixer_operations),
1318 devc);
1320 return my_dev;
1322 uninstall_dma:
1323 sound_free_dma(devc->hw.dma);
1325 uninstall_irq:
1326 free_irq(devc->hw.irq, devc);
1328 uninstall:
1329 sound_unload_audiodev(my_dev);
1331 free:
1332 kfree(portc);
1334 nomem:
1335 return -1;
1338 static int __init probe_waveartist(struct address_info *hw_config)
1340 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1342 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1343 printk(KERN_WARNING "waveartist: too many audio devices\n");
1344 return 0;
1347 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1348 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1349 return 0;
1352 if (hw_config->irq > 15 || hw_config->irq < 0) {
1353 release_region(hw_config->io_base, 15);
1354 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1355 hw_config->irq);
1356 return 0;
1359 if (hw_config->dma != 3) {
1360 release_region(hw_config->io_base, 15);
1361 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1362 hw_config->dma);
1363 return 0;
1366 hw_config->name = "WaveArtist";
1367 devc->hw = *hw_config;
1368 devc->open_mode = 0;
1369 devc->chip_name = "RWA-010";
1371 return 1;
1374 static void __init
1375 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1377 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1380 * NOTE! If irq < 0, there is another driver which has allocated the
1381 * IRQ so that this driver doesn't need to allocate/deallocate it.
1382 * The actually used IRQ is ABS(irq).
1384 devc->hw = *hw;
1385 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1386 devc->open_mode = 0;
1387 devc->playback_dev = 0;
1388 devc->record_dev = 0;
1389 devc->audio_flags = DMA_AUTOMODE;
1390 devc->levels = levels;
1392 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1393 devc->audio_flags |= DMA_DUPLEX;
1395 devc->mix = mix;
1396 devc->dev_no = waveartist_init(devc);
1398 if (devc->dev_no < 0)
1399 release_region(hw->io_base, 15);
1400 else {
1401 #ifdef CONFIG_ARCH_NETWINDER
1402 if (machine_is_netwinder()) {
1403 init_timer(&vnc_timer);
1404 vnc_timer.function = vnc_slider_tick;
1405 vnc_timer.expires = jiffies;
1406 vnc_timer.data = nr_waveartist_devs;
1407 add_timer(&vnc_timer);
1409 vnc_configure_mixer(devc, 0);
1411 devc->no_autoselect = 1;
1413 #endif
1414 nr_waveartist_devs += 1;
1418 static void __exit unload_waveartist(struct address_info *hw)
1420 wavnc_info *devc = NULL;
1421 int i;
1423 for (i = 0; i < nr_waveartist_devs; i++)
1424 if (hw->io_base == adev_info[i].hw.io_base) {
1425 devc = adev_info + i;
1426 break;
1429 if (devc != NULL) {
1430 int mixer;
1432 #ifdef CONFIG_ARCH_NETWINDER
1433 if (machine_is_netwinder())
1434 del_timer(&vnc_timer);
1435 #endif
1437 release_region(devc->hw.io_base, 15);
1439 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1441 if (devc->hw.irq >= 0)
1442 free_irq(devc->hw.irq, devc);
1444 sound_free_dma(devc->hw.dma);
1446 if (devc->hw.dma != devc->hw.dma2 &&
1447 devc->hw.dma2 != NO_DMA)
1448 sound_free_dma(devc->hw.dma2);
1450 mixer = audio_devs[devc->dev_no]->mixer_dev;
1452 if (mixer >= 0)
1453 sound_unload_mixerdev(mixer);
1455 if (devc->dev_no >= 0)
1456 sound_unload_audiodev(devc->dev_no);
1458 nr_waveartist_devs -= 1;
1460 for (; i < nr_waveartist_devs; i++)
1461 adev_info[i] = adev_info[i + 1];
1462 } else
1463 printk(KERN_WARNING "waveartist: can't find device "
1464 "to unload\n");
1467 #ifdef CONFIG_ARCH_NETWINDER
1470 * Rebel.com Netwinder specifics...
1473 #include <asm/hardware/dec21285.h>
1475 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1477 #define MIXER_PRIVATE3_RESET 0x53570000
1478 #define MIXER_PRIVATE3_READ 0x53570001
1479 #define MIXER_PRIVATE3_WRITE 0x53570002
1481 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1482 #define VNC_MUTE_LINE_OUT 0x10
1483 #define VNC_PHONE_DETECT 0x20
1484 #define VNC_HANDSET_DETECT 0x40
1485 #define VNC_DISABLE_AUTOSWITCH 0x80
1487 static inline void
1488 vnc_mute_spkr(wavnc_info *devc)
1490 unsigned long flags;
1492 spin_lock_irqsave(&nw_gpio_lock, flags);
1493 nw_cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1494 spin_unlock_irqrestore(&nw_gpio_lock, flags);
1497 static void
1498 vnc_mute_lout(wavnc_info *devc)
1500 unsigned int left, right;
1502 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1503 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1505 if (devc->line_mute_state) {
1506 left &= ~1;
1507 right &= ~1;
1508 } else {
1509 left |= 1;
1510 right |= 1;
1512 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1516 static int
1517 vnc_volume_slider(wavnc_info *devc)
1519 static signed int old_slider_volume;
1520 unsigned long flags;
1521 signed int volume = 255;
1523 *CSR_TIMER1_LOAD = 0x00ffffff;
1525 spin_lock_irqsave(&waveartist_lock, flags);
1527 outb(0xFF, 0x201);
1528 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1530 while (volume && (inb(0x201) & 0x01))
1531 volume--;
1533 *CSR_TIMER1_CNTL = 0;
1535 spin_unlock_irqrestore(&waveartist_lock,flags);
1537 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1540 #ifndef REVERSE
1541 volume = 150 - (volume >> 5);
1542 #else
1543 volume = (volume >> 6) - 25;
1544 #endif
1546 if (volume < 0)
1547 volume = 0;
1549 if (volume > 100)
1550 volume = 100;
1553 * slider quite often reads +-8, so debounce this random noise
1555 if (abs(volume - old_slider_volume) > 7) {
1556 old_slider_volume = volume;
1558 if (debug_flg & DEBUG_MIXER)
1559 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1562 return old_slider_volume;
1566 * Decode a recording mask into a mixer selection on the NetWinder
1567 * as follows:
1569 * OSS Source WA Source Actual source
1570 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1571 * SOUND_MASK_LINE Line Line in
1572 * SOUND_MASK_LINE1 Left Mic Handset
1573 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1574 * SOUND_MASK_MIC Right Mic Builtin microphone
1576 static unsigned int
1577 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1578 unsigned char *dev_l, unsigned char *dev_r)
1580 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1582 if (recmask & SOUND_MASK_IMIX) {
1583 recmask = SOUND_MASK_IMIX;
1584 recdev_l = ADC_MUX_MIXER;
1585 recdev_r = ADC_MUX_MIXER;
1586 } else if (recmask & SOUND_MASK_LINE) {
1587 recmask = SOUND_MASK_LINE;
1588 recdev_l = ADC_MUX_LINE;
1589 recdev_r = ADC_MUX_LINE;
1590 } else if (recmask & SOUND_MASK_LINE1) {
1591 recmask = SOUND_MASK_LINE1;
1592 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1593 recdev_l = ADC_MUX_MIC;
1594 recdev_r = ADC_MUX_NONE;
1595 } else if (recmask & SOUND_MASK_PHONEIN) {
1596 recmask = SOUND_MASK_PHONEIN;
1597 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1598 recdev_l = ADC_MUX_AUX1;
1599 recdev_r = ADC_MUX_NONE;
1600 } else if (recmask & SOUND_MASK_MIC) {
1601 recmask = SOUND_MASK_MIC;
1602 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1603 recdev_l = ADC_MUX_NONE;
1604 recdev_r = ADC_MUX_MIC;
1607 *dev_l = recdev_l;
1608 *dev_r = recdev_r;
1610 return recmask;
1613 static int
1614 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1615 unsigned char lev_r)
1617 switch (dev) {
1618 case SOUND_MIXER_VOLUME:
1619 case SOUND_MIXER_SYNTH:
1620 case SOUND_MIXER_PCM:
1621 case SOUND_MIXER_LINE:
1622 case SOUND_MIXER_IGAIN:
1623 devc->levels[dev] = lev_l | lev_r << 8;
1624 break;
1626 case SOUND_MIXER_MIC: /* right mic only */
1627 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1628 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1629 break;
1631 case SOUND_MIXER_LINE1: /* left mic only */
1632 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1633 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1634 dev = SOUND_MIXER_MIC;
1635 break;
1637 case SOUND_MIXER_PHONEIN: /* left aux only */
1638 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1639 dev = SOUND_MIXER_LINE1;
1640 break;
1642 case SOUND_MIXER_IMIX:
1643 case SOUND_MIXER_PHONEOUT:
1644 break;
1646 default:
1647 dev = -EINVAL;
1648 break;
1650 return dev;
1653 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1655 int levels;
1657 switch (dev) {
1658 case SOUND_MIXER_VOLUME:
1659 case SOUND_MIXER_SYNTH:
1660 case SOUND_MIXER_PCM:
1661 case SOUND_MIXER_LINE:
1662 case SOUND_MIXER_IGAIN:
1663 levels = devc->levels[dev];
1664 break;
1666 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1667 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1668 levels |= levels << 8;
1669 break;
1671 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1672 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1673 levels |= levels << 8;
1674 break;
1676 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1677 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1678 levels |= levels << 8;
1679 break;
1681 default:
1682 levels = 0;
1685 return levels;
1689 * Waveartist specific mixer information.
1691 static const struct waveartist_mixer_info netwinder_mixer = {
1692 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1693 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1694 SOUND_MASK_LINE | SOUND_MASK_MIC |
1695 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1696 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1697 SOUND_MASK_IGAIN,
1699 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1700 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1701 SOUND_MASK_PHONEIN,
1703 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1704 SOUND_MASK_PCM | SOUND_MASK_LINE |
1705 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1707 .select_input = netwinder_select_input,
1708 .decode_mixer = netwinder_decode_mixer,
1709 .get_mixer = netwinder_get_mixer,
1712 static void
1713 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1715 if (!devc->no_autoselect) {
1716 if (devc->handset_detect) {
1717 recmask = SOUND_MASK_LINE1;
1718 devc->spkr_mute_state = devc->line_mute_state = 1;
1719 } else if (devc->telephone_detect) {
1720 recmask = SOUND_MASK_PHONEIN;
1721 devc->spkr_mute_state = devc->line_mute_state = 1;
1722 } else {
1723 /* unless someone has asked for LINE-IN,
1724 * we default to MIC
1726 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1727 devc->recmask = SOUND_MASK_MIC;
1728 devc->spkr_mute_state = devc->line_mute_state = 0;
1730 vnc_mute_spkr(devc);
1731 vnc_mute_lout(devc);
1733 if (recmask != devc->recmask)
1734 waveartist_set_recmask(devc, recmask);
1738 static int
1739 vnc_slider(wavnc_info *devc)
1741 signed int slider_volume;
1742 unsigned int temp, old_hs, old_td;
1745 * read the "buttons" state.
1746 * Bit 4 = 0 means handset present
1747 * Bit 5 = 1 means phone offhook
1749 temp = inb(0x201);
1751 old_hs = devc->handset_detect;
1752 old_td = devc->telephone_detect;
1754 devc->handset_detect = !(temp & 0x10);
1755 devc->telephone_detect = !!(temp & 0x20);
1757 if (!devc->no_autoselect &&
1758 (old_hs != devc->handset_detect ||
1759 old_td != devc->telephone_detect))
1760 vnc_configure_mixer(devc, devc->recmask);
1762 slider_volume = vnc_volume_slider(devc);
1765 * If we're using software controlled volume, and
1766 * the slider moves by more than 20%, then we
1767 * switch back to slider controlled volume.
1769 if (abs(devc->slider_vol - slider_volume) > 20)
1770 devc->use_slider = 1;
1773 * use only left channel
1775 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1777 if (slider_volume != temp && devc->use_slider) {
1778 devc->slider_vol = slider_volume;
1780 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1781 slider_volume | slider_volume << 8);
1783 return 1;
1786 return 0;
1789 static void
1790 vnc_slider_tick(unsigned long data)
1792 int next_timeout;
1794 if (vnc_slider(adev_info + data))
1795 next_timeout = 5; // mixer reported change
1796 else
1797 next_timeout = VNC_TIMER_PERIOD;
1799 mod_timer(&vnc_timer, jiffies + next_timeout);
1802 static int
1803 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1805 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1806 int val;
1808 switch (cmd) {
1809 case SOUND_MIXER_PRIVATE1:
1811 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1812 int val;
1814 if (get_user(val, arg))
1815 return -EFAULT;
1817 /* check if parameter is logical */
1818 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1819 VNC_MUTE_LINE_OUT |
1820 VNC_DISABLE_AUTOSWITCH))
1821 return -EINVAL;
1823 prev_auto_state = devc->no_autoselect;
1824 prev_spkr_mute = devc->spkr_mute_state;
1825 prev_line_mute = devc->line_mute_state;
1827 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1828 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1829 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1831 if (prev_spkr_mute != devc->spkr_mute_state)
1832 vnc_mute_spkr(devc);
1834 if (prev_line_mute != devc->line_mute_state)
1835 vnc_mute_lout(devc);
1837 if (prev_auto_state != devc->no_autoselect)
1838 vnc_configure_mixer(devc, devc->recmask);
1840 return 0;
1843 case SOUND_MIXER_PRIVATE2:
1844 if (get_user(val, arg))
1845 return -EFAULT;
1847 switch (val) {
1848 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1849 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1850 case VNC_SOUND_PAUSE:
1851 waveartist_cmd1(devc, 0x16);
1852 break;
1854 case VNC_SOUND_RESUME:
1855 waveartist_cmd1(devc, 0x18);
1856 break;
1858 default:
1859 return -EINVAL;
1861 return 0;
1863 /* private ioctl to allow bulk access to waveartist */
1864 case SOUND_MIXER_PRIVATE3:
1866 unsigned long flags;
1867 int mixer_reg[15], i, val;
1869 if (get_user(val, arg))
1870 return -EFAULT;
1871 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1872 return -EFAULT;
1874 switch (mixer_reg[14]) {
1875 case MIXER_PRIVATE3_RESET:
1876 waveartist_mixer_reset(devc);
1877 break;
1879 case MIXER_PRIVATE3_WRITE:
1880 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1881 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1882 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1883 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1884 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1886 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1887 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1888 break;
1890 case MIXER_PRIVATE3_READ:
1891 spin_lock_irqsave(&waveartist_lock, flags);
1893 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1894 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1896 spin_unlock_irqrestore(&waveartist_lock, flags);
1898 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1899 return -EFAULT;
1900 break;
1902 default:
1903 return -EINVAL;
1905 return 0;
1908 /* read back the state from PRIVATE1 */
1909 case SOUND_MIXER_PRIVATE4:
1910 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1911 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1912 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1913 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1914 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1916 return put_user(val, arg) ? -EFAULT : 0;
1919 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1921 * special case for master volume: if we
1922 * received this call - switch from hw
1923 * volume control to a software volume
1924 * control, till the hw volume is modified
1925 * to signal that user wants to be back in
1926 * hardware...
1928 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1929 devc->use_slider = 0;
1931 /* speaker output */
1932 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1933 unsigned int val, l, r;
1935 if (get_user(val, arg))
1936 return -EFAULT;
1938 l = val & 0x7f;
1939 r = (val & 0x7f00) >> 8;
1940 val = (l + r) / 2;
1941 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1942 devc->spkr_mute_state = (val <= 50);
1943 vnc_mute_spkr(devc);
1944 return 0;
1948 return -ENOIOCTLCMD;
1951 #endif
1953 static struct address_info cfg;
1955 static int attached;
1957 static int __initdata io = 0;
1958 static int __initdata irq = 0;
1959 static int __initdata dma = 0;
1960 static int __initdata dma2 = 0;
1963 static int __init init_waveartist(void)
1965 const struct waveartist_mixer_info *mix;
1967 if (!io && machine_is_netwinder()) {
1969 * The NetWinder WaveArtist is at a fixed address.
1970 * If the user does not supply an address, use the
1971 * well-known parameters.
1973 io = 0x250;
1974 irq = 12;
1975 dma = 3;
1976 dma2 = 7;
1979 mix = &waveartist_mixer;
1980 #ifdef CONFIG_ARCH_NETWINDER
1981 if (machine_is_netwinder())
1982 mix = &netwinder_mixer;
1983 #endif
1985 cfg.io_base = io;
1986 cfg.irq = irq;
1987 cfg.dma = dma;
1988 cfg.dma2 = dma2;
1990 if (!probe_waveartist(&cfg))
1991 return -ENODEV;
1993 attach_waveartist(&cfg, mix);
1994 attached = 1;
1996 return 0;
1999 static void __exit cleanup_waveartist(void)
2001 if (attached)
2002 unload_waveartist(&cfg);
2005 module_init(init_waveartist);
2006 module_exit(cleanup_waveartist);
2008 #ifndef MODULE
2009 static int __init setup_waveartist(char *str)
2011 /* io, irq, dma, dma2 */
2012 int ints[5];
2014 str = get_options(str, ARRAY_SIZE(ints), ints);
2016 io = ints[1];
2017 irq = ints[2];
2018 dma = ints[3];
2019 dma2 = ints[4];
2021 return 1;
2023 __setup("waveartist=", setup_waveartist);
2024 #endif
2026 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2027 module_param(io, int, 0); /* IO base */
2028 module_param(irq, int, 0); /* IRQ */
2029 module_param(dma, int, 0); /* DMA */
2030 module_param(dma2, int, 0); /* DMA2 */
2031 MODULE_LICENSE("GPL");