Linux 2.6.19-rc1
[linux-2.6/next.git] / sound / oss / waveartist.c
blob59a2f28eb5a5a8bb1e7fb6ecc36f7c964c1b8612
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/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/spinlock.h>
42 #include <linux/bitops.h>
44 #include <asm/system.h>
46 #include "sound_config.h"
47 #include "waveartist.h"
49 #ifdef CONFIG_ARM
50 #include <asm/hardware.h>
51 #include <asm/mach-types.h>
52 #endif
54 #ifndef NO_DMA
55 #define NO_DMA 255
56 #endif
58 #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_SYNTH |\
59 SOUND_MASK_PCM |\
60 SOUND_MASK_LINE |\
61 SOUND_MASK_MIC |\
62 SOUND_MASK_LINE1 |\
63 SOUND_MASK_RECLEV |\
64 SOUND_MASK_VOLUME |\
65 SOUND_MASK_IMIX)
67 static unsigned short levels[SOUND_MIXER_NRDEVICES] = {
68 0x5555, /* Master Volume */
69 0x0000, /* Bass */
70 0x0000, /* Treble */
71 0x2323, /* Synth (FM) */
72 0x4b4b, /* PCM */
73 0x6464, /* PC Speaker */
74 0x0000, /* Ext Line */
75 0x0000, /* Mic */
76 0x0000, /* CD */
77 0x6464, /* Recording monitor */
78 0x0000, /* SB PCM (ALT PCM) */
79 0x0000, /* Recording level */
80 0x6464, /* Input gain */
81 0x6464, /* Output gain */
82 0x0000, /* Line1 (Aux1) */
83 0x0000, /* Line2 (Aux2) */
84 0x0000, /* Line3 (Aux3) */
85 0x0000, /* Digital1 */
86 0x0000, /* Digital2 */
87 0x0000, /* Digital3 */
88 0x0000, /* Phone In */
89 0x6464, /* Phone Out */
90 0x0000, /* Video */
91 0x0000, /* Radio */
92 0x0000 /* Monitor */
95 typedef struct {
96 struct address_info hw; /* hardware */
97 char *chip_name;
99 int xfer_count;
100 int audio_mode;
101 int open_mode;
102 int audio_flags;
103 int record_dev;
104 int playback_dev;
105 int dev_no;
107 /* Mixer parameters */
108 const struct waveartist_mixer_info *mix;
110 unsigned short *levels; /* cache of volume settings */
111 int recmask; /* currently enabled recording device! */
113 #ifdef CONFIG_ARCH_NETWINDER
114 signed int slider_vol; /* hardware slider volume */
115 unsigned int handset_detect :1;
116 unsigned int telephone_detect:1;
117 unsigned int no_autoselect :1;/* handset/telephone autoselects a path */
118 unsigned int spkr_mute_state :1;/* set by ioctl or autoselect */
119 unsigned int line_mute_state :1;/* set by ioctl or autoselect */
120 unsigned int use_slider :1;/* use slider setting for o/p vol */
121 #endif
122 } wavnc_info;
125 * This is the implementation specific mixer information.
127 struct waveartist_mixer_info {
128 unsigned int supported_devs; /* Supported devices */
129 unsigned int recording_devs; /* Recordable devies */
130 unsigned int stereo_devs; /* Stereo devices */
132 unsigned int (*select_input)(wavnc_info *, unsigned int,
133 unsigned char *, unsigned char *);
134 int (*decode_mixer)(wavnc_info *, int,
135 unsigned char, unsigned char);
136 int (*get_mixer)(wavnc_info *, int);
139 typedef struct wavnc_port_info {
140 int open_mode;
141 int speed;
142 int channels;
143 int audio_format;
144 } wavnc_port_info;
146 static int nr_waveartist_devs;
147 static wavnc_info adev_info[MAX_AUDIO_DEV];
148 static DEFINE_SPINLOCK(waveartist_lock);
150 #ifndef CONFIG_ARCH_NETWINDER
151 #define machine_is_netwinder() 0
152 #else
153 static struct timer_list vnc_timer;
154 static void vnc_configure_mixer(wavnc_info *devc, unsigned int input_mask);
155 static int vnc_private_ioctl(int dev, unsigned int cmd, int __user *arg);
156 static void vnc_slider_tick(unsigned long data);
157 #endif
159 static inline void
160 waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
162 unsigned int ctlr_port = hw->io_base + CTLR;
164 clear = ~clear & inb(ctlr_port);
166 outb(clear | set, ctlr_port);
169 /* Toggle IRQ acknowledge line
171 static inline void
172 waveartist_iack(wavnc_info *devc)
174 unsigned int ctlr_port = devc->hw.io_base + CTLR;
175 int old_ctlr;
177 old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
179 outb(old_ctlr | IRQ_ACK, ctlr_port);
180 outb(old_ctlr, ctlr_port);
183 static inline int
184 waveartist_sleep(int timeout_ms)
186 unsigned int timeout = timeout_ms * 10 * HZ / 100;
188 do {
189 set_current_state(TASK_INTERRUPTIBLE);
190 timeout = schedule_timeout(timeout);
191 } while (timeout);
193 return 0;
196 static int
197 waveartist_reset(wavnc_info *devc)
199 struct address_info *hw = &devc->hw;
200 unsigned int timeout, res = -1;
202 waveartist_set_ctlr(hw, -1, RESET);
203 waveartist_sleep(2);
204 waveartist_set_ctlr(hw, RESET, 0);
206 timeout = 500;
207 do {
208 mdelay(2);
210 if (inb(hw->io_base + STATR) & CMD_RF) {
211 res = inw(hw->io_base + CMDR);
212 if (res == 0x55aa)
213 break;
215 } while (--timeout);
217 if (timeout == 0) {
218 printk(KERN_WARNING "WaveArtist: reset timeout ");
219 if (res != (unsigned int)-1)
220 printk("(res=%04X)", res);
221 printk("\n");
222 return 1;
224 return 0;
227 /* Helper function to send and receive words
228 * from WaveArtist. It handles all the handshaking
229 * and can send or receive multiple words.
231 static int
232 waveartist_cmd(wavnc_info *devc,
233 int nr_cmd, unsigned int *cmd,
234 int nr_resp, unsigned int *resp)
236 unsigned int io_base = devc->hw.io_base;
237 unsigned int timed_out = 0;
238 unsigned int i;
240 if (debug_flg & DEBUG_CMD) {
241 printk("waveartist_cmd: cmd=");
243 for (i = 0; i < nr_cmd; i++)
244 printk("%04X ", cmd[i]);
246 printk("\n");
249 if (inb(io_base + STATR) & CMD_RF) {
250 int old_data;
252 /* flush the port
255 old_data = inw(io_base + CMDR);
257 if (debug_flg & DEBUG_CMD)
258 printk("flushed %04X...", old_data);
260 udelay(10);
263 for (i = 0; !timed_out && i < nr_cmd; i++) {
264 int count;
266 for (count = 5000; count; count--)
267 if (inb(io_base + STATR) & CMD_WE)
268 break;
270 if (!count)
271 timed_out = 1;
272 else
273 outw(cmd[i], io_base + CMDR);
276 for (i = 0; !timed_out && i < nr_resp; i++) {
277 int count;
279 for (count = 5000; count; count--)
280 if (inb(io_base + STATR) & CMD_RF)
281 break;
283 if (!count)
284 timed_out = 1;
285 else
286 resp[i] = inw(io_base + CMDR);
289 if (debug_flg & DEBUG_CMD) {
290 if (!timed_out) {
291 printk("waveartist_cmd: resp=");
293 for (i = 0; i < nr_resp; i++)
294 printk("%04X ", resp[i]);
296 printk("\n");
297 } else
298 printk("waveartist_cmd: timed out\n");
301 return timed_out ? 1 : 0;
305 * Send one command word
307 static inline int
308 waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
310 return waveartist_cmd(devc, 1, &cmd, 0, NULL);
314 * Send one command, receive one word
316 static inline unsigned int
317 waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
319 unsigned int ret;
321 waveartist_cmd(devc, 1, &cmd, 1, &ret);
323 return ret;
327 * Send a double command, receive one
328 * word (and throw it away)
330 static inline int
331 waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
333 unsigned int vals[2];
335 vals[0] = cmd;
336 vals[1] = arg;
338 return waveartist_cmd(devc, 2, vals, 1, vals);
342 * Send a triple command
344 static inline int
345 waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
346 unsigned int arg1, unsigned int arg2)
348 unsigned int vals[3];
350 vals[0] = cmd;
351 vals[1] = arg1;
352 vals[2] = arg2;
354 return waveartist_cmd(devc, 3, vals, 0, NULL);
357 static int
358 waveartist_getrev(wavnc_info *devc, char *rev)
360 unsigned int temp[2];
361 unsigned int cmd = WACMD_GETREV;
363 waveartist_cmd(devc, 1, &cmd, 2, temp);
365 rev[0] = temp[0] >> 8;
366 rev[1] = temp[0] & 255;
367 rev[2] = '\0';
369 return temp[0];
372 static void waveartist_halt_output(int dev);
373 static void waveartist_halt_input(int dev);
374 static void waveartist_halt(int dev);
375 static void waveartist_trigger(int dev, int state);
377 static int
378 waveartist_open(int dev, int mode)
380 wavnc_info *devc;
381 wavnc_port_info *portc;
382 unsigned long flags;
384 if (dev < 0 || dev >= num_audiodevs)
385 return -ENXIO;
387 devc = (wavnc_info *) audio_devs[dev]->devc;
388 portc = (wavnc_port_info *) audio_devs[dev]->portc;
390 spin_lock_irqsave(&waveartist_lock, flags);
391 if (portc->open_mode || (devc->open_mode & mode)) {
392 spin_unlock_irqrestore(&waveartist_lock, flags);
393 return -EBUSY;
396 devc->audio_mode = 0;
397 devc->open_mode |= mode;
398 portc->open_mode = mode;
399 waveartist_trigger(dev, 0);
401 if (mode & OPEN_READ)
402 devc->record_dev = dev;
403 if (mode & OPEN_WRITE)
404 devc->playback_dev = dev;
405 spin_unlock_irqrestore(&waveartist_lock, flags);
407 return 0;
410 static void
411 waveartist_close(int dev)
413 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
414 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
415 unsigned long flags;
417 spin_lock_irqsave(&waveartist_lock, flags);
419 waveartist_halt(dev);
421 devc->audio_mode = 0;
422 devc->open_mode &= ~portc->open_mode;
423 portc->open_mode = 0;
425 spin_unlock_irqrestore(&waveartist_lock, flags);
428 static void
429 waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
431 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
432 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
433 unsigned long flags;
434 unsigned int count = __count;
436 if (debug_flg & DEBUG_OUT)
437 printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
438 buf, count);
440 * 16 bit data
442 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
443 count >>= 1;
445 if (portc->channels > 1)
446 count >>= 1;
448 count -= 1;
450 if (devc->audio_mode & PCM_ENABLE_OUTPUT &&
451 audio_devs[dev]->flags & DMA_AUTOMODE &&
452 intrflag &&
453 count == devc->xfer_count) {
454 devc->audio_mode |= PCM_ENABLE_OUTPUT;
455 return; /*
456 * Auto DMA mode on. No need to react
460 spin_lock_irqsave(&waveartist_lock, flags);
463 * set sample count
465 waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
467 devc->xfer_count = count;
468 devc->audio_mode |= PCM_ENABLE_OUTPUT;
470 spin_unlock_irqrestore(&waveartist_lock, flags);
473 static void
474 waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
476 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
477 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
478 unsigned long flags;
479 unsigned int count = __count;
481 if (debug_flg & DEBUG_IN)
482 printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
483 buf, count);
485 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
486 count >>= 1;
488 if (portc->channels > 1)
489 count >>= 1;
491 count -= 1;
493 if (devc->audio_mode & PCM_ENABLE_INPUT &&
494 audio_devs[dev]->flags & DMA_AUTOMODE &&
495 intrflag &&
496 count == devc->xfer_count) {
497 devc->audio_mode |= PCM_ENABLE_INPUT;
498 return; /*
499 * Auto DMA mode on. No need to react
503 spin_lock_irqsave(&waveartist_lock, flags);
506 * set sample count
508 waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
510 devc->xfer_count = count;
511 devc->audio_mode |= PCM_ENABLE_INPUT;
513 spin_unlock_irqrestore(&waveartist_lock, flags);
516 static int
517 waveartist_ioctl(int dev, unsigned int cmd, void __user * arg)
519 return -EINVAL;
522 static unsigned int
523 waveartist_get_speed(wavnc_port_info *portc)
525 unsigned int speed;
528 * program the speed, channels, bits
530 if (portc->speed == 8000)
531 speed = 0x2E71;
532 else if (portc->speed == 11025)
533 speed = 0x4000;
534 else if (portc->speed == 22050)
535 speed = 0x8000;
536 else if (portc->speed == 44100)
537 speed = 0x0;
538 else {
540 * non-standard - just calculate
542 speed = portc->speed << 16;
544 speed = (speed / 44100) & 65535;
547 return speed;
550 static unsigned int
551 waveartist_get_bits(wavnc_port_info *portc)
553 unsigned int bits;
555 if (portc->audio_format == AFMT_S16_LE)
556 bits = 1;
557 else if (portc->audio_format == AFMT_S8)
558 bits = 0;
559 else
560 bits = 2; //default AFMT_U8
562 return bits;
565 static int
566 waveartist_prepare_for_input(int dev, int bsize, int bcount)
568 unsigned long flags;
569 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
570 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
571 unsigned int speed, bits;
573 if (devc->audio_mode)
574 return 0;
576 speed = waveartist_get_speed(portc);
577 bits = waveartist_get_bits(portc);
579 spin_lock_irqsave(&waveartist_lock, flags);
581 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
582 printk(KERN_WARNING "waveartist: error setting the "
583 "record format to %d\n", portc->audio_format);
585 if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
586 printk(KERN_WARNING "waveartist: error setting record "
587 "to %d channels\n", portc->channels);
590 * write cmd SetSampleSpeedTimeConstant
592 if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
593 printk(KERN_WARNING "waveartist: error setting the record "
594 "speed to %dHz.\n", portc->speed);
596 if (waveartist_cmd2(devc, WACMD_INPUTDMA, 1))
597 printk(KERN_WARNING "waveartist: error setting the record "
598 "data path to 0x%X\n", 1);
600 if (waveartist_cmd2(devc, WACMD_INPUTFORMAT, bits))
601 printk(KERN_WARNING "waveartist: error setting the record "
602 "format to %d\n", portc->audio_format);
604 devc->xfer_count = 0;
605 spin_unlock_irqrestore(&waveartist_lock, flags);
606 waveartist_halt_input(dev);
608 if (debug_flg & DEBUG_INTR) {
609 printk("WA CTLR reg: 0x%02X.\n",
610 inb(devc->hw.io_base + CTLR));
611 printk("WA STAT reg: 0x%02X.\n",
612 inb(devc->hw.io_base + STATR));
613 printk("WA IRQS reg: 0x%02X.\n",
614 inb(devc->hw.io_base + IRQSTAT));
617 return 0;
620 static int
621 waveartist_prepare_for_output(int dev, int bsize, int bcount)
623 unsigned long flags;
624 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
625 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
626 unsigned int speed, bits;
629 * program the speed, channels, bits
631 speed = waveartist_get_speed(portc);
632 bits = waveartist_get_bits(portc);
634 spin_lock_irqsave(&waveartist_lock, flags);
636 if (waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed) &&
637 waveartist_cmd2(devc, WACMD_OUTPUTSPEED, speed))
638 printk(KERN_WARNING "waveartist: error setting the playback "
639 "speed to %dHz.\n", portc->speed);
641 if (waveartist_cmd2(devc, WACMD_OUTPUTCHANNELS, portc->channels))
642 printk(KERN_WARNING "waveartist: error setting the playback "
643 "to %d channels\n", portc->channels);
645 if (waveartist_cmd2(devc, WACMD_OUTPUTDMA, 0))
646 printk(KERN_WARNING "waveartist: error setting the playback "
647 "data path to 0x%X\n", 0);
649 if (waveartist_cmd2(devc, WACMD_OUTPUTFORMAT, bits))
650 printk(KERN_WARNING "waveartist: error setting the playback "
651 "format to %d\n", portc->audio_format);
653 devc->xfer_count = 0;
654 spin_unlock_irqrestore(&waveartist_lock, flags);
655 waveartist_halt_output(dev);
657 if (debug_flg & DEBUG_INTR) {
658 printk("WA CTLR reg: 0x%02X.\n",inb(devc->hw.io_base + CTLR));
659 printk("WA STAT reg: 0x%02X.\n",inb(devc->hw.io_base + STATR));
660 printk("WA IRQS reg: 0x%02X.\n",inb(devc->hw.io_base + IRQSTAT));
663 return 0;
666 static void
667 waveartist_halt(int dev)
669 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
670 wavnc_info *devc;
672 if (portc->open_mode & OPEN_WRITE)
673 waveartist_halt_output(dev);
675 if (portc->open_mode & OPEN_READ)
676 waveartist_halt_input(dev);
678 devc = (wavnc_info *) audio_devs[dev]->devc;
679 devc->audio_mode = 0;
682 static void
683 waveartist_halt_input(int dev)
685 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
686 unsigned long flags;
688 spin_lock_irqsave(&waveartist_lock, flags);
691 * Stop capture
693 waveartist_cmd1(devc, WACMD_INPUTSTOP);
695 devc->audio_mode &= ~PCM_ENABLE_INPUT;
698 * Clear interrupt by toggling
699 * the IRQ_ACK bit in CTRL
701 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
702 waveartist_iack(devc);
704 // devc->audio_mode &= ~PCM_ENABLE_INPUT;
706 spin_unlock_irqrestore(&waveartist_lock, flags);
709 static void
710 waveartist_halt_output(int dev)
712 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
713 unsigned long flags;
715 spin_lock_irqsave(&waveartist_lock, flags);
717 waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
719 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
722 * Clear interrupt by toggling
723 * the IRQ_ACK bit in CTRL
725 if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
726 waveartist_iack(devc);
728 // devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
730 spin_unlock_irqrestore(&waveartist_lock, flags);
733 static void
734 waveartist_trigger(int dev, int state)
736 wavnc_info *devc = (wavnc_info *) audio_devs[dev]->devc;
737 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
738 unsigned long flags;
740 if (debug_flg & DEBUG_TRIGGER) {
741 printk("wavnc: audio trigger ");
742 if (state & PCM_ENABLE_INPUT)
743 printk("in ");
744 if (state & PCM_ENABLE_OUTPUT)
745 printk("out");
746 printk("\n");
749 spin_lock_irqsave(&waveartist_lock, flags);
751 state &= devc->audio_mode;
753 if (portc->open_mode & OPEN_READ &&
754 state & PCM_ENABLE_INPUT)
756 * enable ADC Data Transfer to PC
758 waveartist_cmd1(devc, WACMD_INPUTSTART);
760 if (portc->open_mode & OPEN_WRITE &&
761 state & PCM_ENABLE_OUTPUT)
763 * enable DAC data transfer from PC
765 waveartist_cmd1(devc, WACMD_OUTPUTSTART);
767 spin_unlock_irqrestore(&waveartist_lock, flags);
770 static int
771 waveartist_set_speed(int dev, int arg)
773 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
775 if (arg <= 0)
776 return portc->speed;
778 if (arg < 5000)
779 arg = 5000;
780 if (arg > 44100)
781 arg = 44100;
783 portc->speed = arg;
784 return portc->speed;
788 static short
789 waveartist_set_channels(int dev, short arg)
791 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
793 if (arg != 1 && arg != 2)
794 return portc->channels;
796 portc->channels = arg;
797 return arg;
800 static unsigned int
801 waveartist_set_bits(int dev, unsigned int arg)
803 wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
805 if (arg == 0)
806 return portc->audio_format;
808 if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
809 arg = AFMT_U8;
811 portc->audio_format = arg;
813 return arg;
816 static struct audio_driver waveartist_audio_driver = {
817 .owner = THIS_MODULE,
818 .open = waveartist_open,
819 .close = waveartist_close,
820 .output_block = waveartist_output_block,
821 .start_input = waveartist_start_input,
822 .ioctl = waveartist_ioctl,
823 .prepare_for_input = waveartist_prepare_for_input,
824 .prepare_for_output = waveartist_prepare_for_output,
825 .halt_io = waveartist_halt,
826 .halt_input = waveartist_halt_input,
827 .halt_output = waveartist_halt_output,
828 .trigger = waveartist_trigger,
829 .set_speed = waveartist_set_speed,
830 .set_bits = waveartist_set_bits,
831 .set_channels = waveartist_set_channels
835 static irqreturn_t
836 waveartist_intr(int irq, void *dev_id, struct pt_regs *regs)
838 wavnc_info *devc = (wavnc_info *)dev_id;
839 int irqstatus, status;
841 spin_lock(&waveartist_lock);
842 irqstatus = inb(devc->hw.io_base + IRQSTAT);
843 status = inb(devc->hw.io_base + STATR);
845 if (debug_flg & DEBUG_INTR)
846 printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
847 status, irqstatus);
849 if (status & IRQ_REQ) /* Clear interrupt */
850 waveartist_iack(devc);
851 else
852 printk(KERN_WARNING "waveartist: unexpected interrupt\n");
854 if (irqstatus & 0x01) {
855 int temp = 1;
857 /* PCM buffer done
859 if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
860 DMAbuf_outputintr(devc->playback_dev, 1);
861 temp = 0;
863 if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
864 DMAbuf_inputintr(devc->record_dev);
865 temp = 0;
867 if (temp) //default:
868 printk(KERN_WARNING "waveartist: Unknown interrupt\n");
870 if (irqstatus & 0x2)
871 // We do not use SB mode natively...
872 printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
873 spin_unlock(&waveartist_lock);
874 return IRQ_HANDLED;
877 /* -------------------------------------------------------------------------
878 * Mixer stuff
880 struct mix_ent {
881 unsigned char reg_l;
882 unsigned char reg_r;
883 unsigned char shift;
884 unsigned char max;
887 static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
888 { 2, 6, 1, 7 }, /* SOUND_MIXER_VOLUME */
889 { 0, 0, 0, 0 }, /* SOUND_MIXER_BASS */
890 { 0, 0, 0, 0 }, /* SOUND_MIXER_TREBLE */
891 { 0, 0, 0, 0 }, /* SOUND_MIXER_SYNTH */
892 { 0, 0, 0, 0 }, /* SOUND_MIXER_PCM */
893 { 0, 0, 0, 0 }, /* SOUND_MIXER_SPEAKER */
894 { 0, 4, 6, 31 }, /* SOUND_MIXER_LINE */
895 { 2, 6, 4, 3 }, /* SOUND_MIXER_MIC */
896 { 0, 0, 0, 0 }, /* SOUND_MIXER_CD */
897 { 0, 0, 0, 0 }, /* SOUND_MIXER_IMIX */
898 { 0, 0, 0, 0 }, /* SOUND_MIXER_ALTPCM */
899 #if 0
900 { 3, 7, 0, 10 }, /* SOUND_MIXER_RECLEV */
901 { 0, 0, 0, 0 }, /* SOUND_MIXER_IGAIN */
902 #else
903 { 0, 0, 0, 0 }, /* SOUND_MIXER_RECLEV */
904 { 3, 7, 0, 7 }, /* SOUND_MIXER_IGAIN */
905 #endif
906 { 0, 0, 0, 0 }, /* SOUND_MIXER_OGAIN */
907 { 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1 */
908 { 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2 */
909 { 0, 0, 0, 0 }, /* SOUND_MIXER_LINE3 */
910 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL1 */
911 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL2 */
912 { 0, 0, 0, 0 }, /* SOUND_MIXER_DIGITAL3 */
913 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEIN */
914 { 0, 0, 0, 0 }, /* SOUND_MIXER_PHONEOUT */
915 { 0, 0, 0, 0 }, /* SOUND_MIXER_VIDEO */
916 { 0, 0, 0, 0 }, /* SOUND_MIXER_RADIO */
917 { 0, 0, 0, 0 } /* SOUND_MIXER_MONITOR */
920 static void
921 waveartist_mixer_update(wavnc_info *devc, int whichDev)
923 unsigned int lev_left, lev_right;
925 lev_left = devc->levels[whichDev] & 0xff;
926 lev_right = devc->levels[whichDev] >> 8;
928 if (lev_left > 100)
929 lev_left = 100;
930 if (lev_right > 100)
931 lev_right = 100;
933 #define SCALE(lev,max) ((lev) * (max) / 100)
935 if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
936 whichDev = SOUND_MIXER_VOLUME;
938 if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
939 const struct mix_ent *mix = mix_devs + whichDev;
940 unsigned int mask, left, right;
942 mask = mix->max << mix->shift;
943 lev_left = SCALE(lev_left, mix->max) << mix->shift;
944 lev_right = SCALE(lev_right, mix->max) << mix->shift;
946 /* read left setting */
947 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
948 mix->reg_l << 8);
950 /* read right setting */
951 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
952 mix->reg_r << 8);
954 left = (left & ~mask) | (lev_left & mask);
955 right = (right & ~mask) | (lev_right & mask);
957 /* write left,right back */
958 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
959 } else {
960 switch(whichDev) {
961 case SOUND_MIXER_PCM:
962 waveartist_cmd3(devc, WACMD_SET_LEVEL,
963 SCALE(lev_left, 32767),
964 SCALE(lev_right, 32767));
965 break;
967 case SOUND_MIXER_SYNTH:
968 waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
969 SCALE(lev_left, 32767),
970 SCALE(lev_right, 32767));
971 break;
977 * Set the ADC MUX to the specified values. We do NOT do any
978 * checking of the values passed, since we assume that the
979 * relevant *_select_input function has done that for us.
981 static void
982 waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
984 unsigned int reg_08, reg_09;
986 reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
987 reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
989 reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
991 waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
995 * Decode a recording mask into a mixer selection as follows:
997 * OSS Source WA Source Actual source
998 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
999 * SOUND_MASK_LINE Line Line in
1000 * SOUND_MASK_LINE1 Aux 1 Aux 1 in
1001 * SOUND_MASK_LINE2 Aux 2 Aux 2 in
1002 * SOUND_MASK_MIC Mic Microphone
1004 static unsigned int
1005 waveartist_select_input(wavnc_info *devc, unsigned int recmask,
1006 unsigned char *dev_l, unsigned char *dev_r)
1008 unsigned int recdev = ADC_MUX_NONE;
1010 if (recmask & SOUND_MASK_IMIX) {
1011 recmask = SOUND_MASK_IMIX;
1012 recdev = ADC_MUX_MIXER;
1013 } else if (recmask & SOUND_MASK_LINE2) {
1014 recmask = SOUND_MASK_LINE2;
1015 recdev = ADC_MUX_AUX2;
1016 } else if (recmask & SOUND_MASK_LINE1) {
1017 recmask = SOUND_MASK_LINE1;
1018 recdev = ADC_MUX_AUX1;
1019 } else if (recmask & SOUND_MASK_LINE) {
1020 recmask = SOUND_MASK_LINE;
1021 recdev = ADC_MUX_LINE;
1022 } else if (recmask & SOUND_MASK_MIC) {
1023 recmask = SOUND_MASK_MIC;
1024 recdev = ADC_MUX_MIC;
1027 *dev_l = *dev_r = recdev;
1029 return recmask;
1032 static int
1033 waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1034 unsigned char lev_r)
1036 switch (dev) {
1037 case SOUND_MIXER_VOLUME:
1038 case SOUND_MIXER_SYNTH:
1039 case SOUND_MIXER_PCM:
1040 case SOUND_MIXER_LINE:
1041 case SOUND_MIXER_MIC:
1042 case SOUND_MIXER_IGAIN:
1043 case SOUND_MIXER_LINE1:
1044 case SOUND_MIXER_LINE2:
1045 devc->levels[dev] = lev_l | lev_r << 8;
1046 break;
1048 case SOUND_MIXER_IMIX:
1049 break;
1051 default:
1052 dev = -EINVAL;
1053 break;
1056 return dev;
1059 static int waveartist_get_mixer(wavnc_info *devc, int dev)
1061 return devc->levels[dev];
1064 static const struct waveartist_mixer_info waveartist_mixer = {
1065 .supported_devs = SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1066 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1067 SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1068 SOUND_MASK_IMIX,
1069 .stereo_devs = (SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1070 (SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1071 .select_input = waveartist_select_input,
1072 .decode_mixer = waveartist_decode_mixer,
1073 .get_mixer = waveartist_get_mixer,
1076 static void
1077 waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1079 unsigned char dev_l, dev_r;
1081 recmask &= devc->mix->recording_devs;
1084 * If more than one recording device selected,
1085 * disable the device that is currently in use.
1087 if (hweight32(recmask) > 1)
1088 recmask &= ~devc->recmask;
1091 * Translate the recording device mask into
1092 * the ADC multiplexer settings.
1094 devc->recmask = devc->mix->select_input(devc, recmask,
1095 &dev_l, &dev_r);
1097 waveartist_set_adc_mux(devc, dev_l, dev_r);
1100 static int
1101 waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1103 unsigned int lev_left = level & 0x00ff;
1104 unsigned int lev_right = (level & 0xff00) >> 8;
1106 if (lev_left > 100)
1107 lev_left = 100;
1108 if (lev_right > 100)
1109 lev_right = 100;
1112 * Mono devices have their right volume forced to their
1113 * left volume. (from ALSA driver OSS emulation).
1115 if (!(devc->mix->stereo_devs & (1 << dev)))
1116 lev_right = lev_left;
1118 dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1120 if (dev >= 0)
1121 waveartist_mixer_update(devc, dev);
1123 return dev < 0 ? dev : 0;
1126 static int
1127 waveartist_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
1129 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1130 int ret = 0, val, nr;
1133 * All SOUND_MIXER_* ioctls use type 'M'
1135 if (((cmd >> 8) & 255) != 'M')
1136 return -ENOIOCTLCMD;
1138 #ifdef CONFIG_ARCH_NETWINDER
1139 if (machine_is_netwinder()) {
1140 ret = vnc_private_ioctl(dev, cmd, arg);
1141 if (ret != -ENOIOCTLCMD)
1142 return ret;
1143 else
1144 ret = 0;
1146 #endif
1148 nr = cmd & 0xff;
1150 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1151 if (get_user(val, (int __user *)arg))
1152 return -EFAULT;
1154 switch (nr) {
1155 case SOUND_MIXER_RECSRC:
1156 waveartist_set_recmask(devc, val);
1157 break;
1159 default:
1160 ret = -EINVAL;
1161 if (nr < SOUND_MIXER_NRDEVICES &&
1162 devc->mix->supported_devs & (1 << nr))
1163 ret = waveartist_set_mixer(devc, nr, val);
1167 if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1168 ret = -EINVAL;
1170 switch (nr) {
1171 case SOUND_MIXER_RECSRC:
1172 ret = devc->recmask;
1173 break;
1175 case SOUND_MIXER_DEVMASK:
1176 ret = devc->mix->supported_devs;
1177 break;
1179 case SOUND_MIXER_STEREODEVS:
1180 ret = devc->mix->stereo_devs;
1181 break;
1183 case SOUND_MIXER_RECMASK:
1184 ret = devc->mix->recording_devs;
1185 break;
1187 case SOUND_MIXER_CAPS:
1188 ret = SOUND_CAP_EXCL_INPUT;
1189 break;
1191 default:
1192 if (nr < SOUND_MIXER_NRDEVICES)
1193 ret = devc->mix->get_mixer(devc, nr);
1194 break;
1197 if (ret >= 0)
1198 ret = put_user(ret, (int __user *)arg) ? -EFAULT : 0;
1201 return ret;
1204 static struct mixer_operations waveartist_mixer_operations =
1206 .owner = THIS_MODULE,
1207 .id = "WaveArtist",
1208 .name = "WaveArtist",
1209 .ioctl = waveartist_mixer_ioctl
1212 static void
1213 waveartist_mixer_reset(wavnc_info *devc)
1215 int i;
1217 if (debug_flg & DEBUG_MIXER)
1218 printk("%s: mixer_reset\n", devc->hw.name);
1221 * reset mixer cmd
1223 waveartist_cmd1(devc, WACMD_RST_MIXER);
1226 * set input for ADC to come from 'quiet'
1227 * turn on default modes
1229 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1232 * set mixer input select to none, RX filter gains 0 dB
1234 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1237 * set bit 0 reg 2 to 1 - unmute MonoOut
1239 waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1241 /* set default input device = internal mic
1242 * current recording device = none
1244 waveartist_set_recmask(devc, 0);
1246 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1247 waveartist_mixer_update(devc, i);
1250 static int __init waveartist_init(wavnc_info *devc)
1252 wavnc_port_info *portc;
1253 char rev[3], dev_name[64];
1254 int my_dev;
1256 if (waveartist_reset(devc))
1257 return -ENODEV;
1259 sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1261 if (waveartist_getrev(devc, rev)) {
1262 strcat(dev_name, " rev. ");
1263 strcat(dev_name, rev);
1265 strcat(dev_name, ")");
1267 conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1268 devc->hw.dma, devc->hw.dma2);
1270 portc = (wavnc_port_info *)kmalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1271 if (portc == NULL)
1272 goto nomem;
1274 memset(portc, 0, sizeof(wavnc_port_info));
1276 my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1277 &waveartist_audio_driver, sizeof(struct audio_driver),
1278 devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1279 devc, devc->hw.dma, devc->hw.dma2);
1281 if (my_dev < 0)
1282 goto free;
1284 audio_devs[my_dev]->portc = portc;
1286 waveartist_mixer_reset(devc);
1289 * clear any pending interrupt
1291 waveartist_iack(devc);
1293 if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1294 printk(KERN_ERR "%s: IRQ %d in use\n",
1295 devc->hw.name, devc->hw.irq);
1296 goto uninstall;
1299 if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1300 printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1301 devc->hw.name, devc->hw.dma);
1302 goto uninstall_irq;
1305 if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1306 if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1307 printk(KERN_ERR "%s: can't allocate DMA%d\n",
1308 devc->hw.name, devc->hw.dma2);
1309 goto uninstall_dma;
1312 waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1314 audio_devs[my_dev]->mixer_dev =
1315 sound_install_mixer(MIXER_DRIVER_VERSION,
1316 dev_name,
1317 &waveartist_mixer_operations,
1318 sizeof(struct mixer_operations),
1319 devc);
1321 return my_dev;
1323 uninstall_dma:
1324 sound_free_dma(devc->hw.dma);
1326 uninstall_irq:
1327 free_irq(devc->hw.irq, devc);
1329 uninstall:
1330 sound_unload_audiodev(my_dev);
1332 free:
1333 kfree(portc);
1335 nomem:
1336 return -1;
1339 static int __init probe_waveartist(struct address_info *hw_config)
1341 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1343 if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1344 printk(KERN_WARNING "waveartist: too many audio devices\n");
1345 return 0;
1348 if (!request_region(hw_config->io_base, 15, hw_config->name)) {
1349 printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1350 return 0;
1353 if (hw_config->irq > 15 || hw_config->irq < 0) {
1354 release_region(hw_config->io_base, 15);
1355 printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1356 hw_config->irq);
1357 return 0;
1360 if (hw_config->dma != 3) {
1361 release_region(hw_config->io_base, 15);
1362 printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1363 hw_config->dma);
1364 return 0;
1367 hw_config->name = "WaveArtist";
1368 devc->hw = *hw_config;
1369 devc->open_mode = 0;
1370 devc->chip_name = "RWA-010";
1372 return 1;
1375 static void __init
1376 attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1378 wavnc_info *devc = &adev_info[nr_waveartist_devs];
1381 * NOTE! If irq < 0, there is another driver which has allocated the
1382 * IRQ so that this driver doesn't need to allocate/deallocate it.
1383 * The actually used IRQ is ABS(irq).
1385 devc->hw = *hw;
1386 devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1387 devc->open_mode = 0;
1388 devc->playback_dev = 0;
1389 devc->record_dev = 0;
1390 devc->audio_flags = DMA_AUTOMODE;
1391 devc->levels = levels;
1393 if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1394 devc->audio_flags |= DMA_DUPLEX;
1396 devc->mix = mix;
1397 devc->dev_no = waveartist_init(devc);
1399 if (devc->dev_no < 0)
1400 release_region(hw->io_base, 15);
1401 else {
1402 #ifdef CONFIG_ARCH_NETWINDER
1403 if (machine_is_netwinder()) {
1404 init_timer(&vnc_timer);
1405 vnc_timer.function = vnc_slider_tick;
1406 vnc_timer.expires = jiffies;
1407 vnc_timer.data = nr_waveartist_devs;
1408 add_timer(&vnc_timer);
1410 vnc_configure_mixer(devc, 0);
1412 devc->no_autoselect = 1;
1414 #endif
1415 nr_waveartist_devs += 1;
1419 static void __exit unload_waveartist(struct address_info *hw)
1421 wavnc_info *devc = NULL;
1422 int i;
1424 for (i = 0; i < nr_waveartist_devs; i++)
1425 if (hw->io_base == adev_info[i].hw.io_base) {
1426 devc = adev_info + i;
1427 break;
1430 if (devc != NULL) {
1431 int mixer;
1433 #ifdef CONFIG_ARCH_NETWINDER
1434 if (machine_is_netwinder())
1435 del_timer(&vnc_timer);
1436 #endif
1438 release_region(devc->hw.io_base, 15);
1440 waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1442 if (devc->hw.irq >= 0)
1443 free_irq(devc->hw.irq, devc);
1445 sound_free_dma(devc->hw.dma);
1447 if (devc->hw.dma != devc->hw.dma2 &&
1448 devc->hw.dma2 != NO_DMA)
1449 sound_free_dma(devc->hw.dma2);
1451 mixer = audio_devs[devc->dev_no]->mixer_dev;
1453 if (mixer >= 0)
1454 sound_unload_mixerdev(mixer);
1456 if (devc->dev_no >= 0)
1457 sound_unload_audiodev(devc->dev_no);
1459 nr_waveartist_devs -= 1;
1461 for (; i < nr_waveartist_devs; i++)
1462 adev_info[i] = adev_info[i + 1];
1463 } else
1464 printk(KERN_WARNING "waveartist: can't find device "
1465 "to unload\n");
1468 #ifdef CONFIG_ARCH_NETWINDER
1471 * Rebel.com Netwinder specifics...
1474 #include <asm/hardware/dec21285.h>
1476 #define VNC_TIMER_PERIOD (HZ/4) //check slider 4 times/sec
1478 #define MIXER_PRIVATE3_RESET 0x53570000
1479 #define MIXER_PRIVATE3_READ 0x53570001
1480 #define MIXER_PRIVATE3_WRITE 0x53570002
1482 #define VNC_MUTE_INTERNAL_SPKR 0x01 //the sw mute on/off control bit
1483 #define VNC_MUTE_LINE_OUT 0x10
1484 #define VNC_PHONE_DETECT 0x20
1485 #define VNC_HANDSET_DETECT 0x40
1486 #define VNC_DISABLE_AUTOSWITCH 0x80
1488 extern spinlock_t gpio_lock;
1490 static inline void
1491 vnc_mute_spkr(wavnc_info *devc)
1493 unsigned long flags;
1495 spin_lock_irqsave(&gpio_lock, flags);
1496 cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1497 spin_unlock_irqrestore(&gpio_lock, flags);
1500 static void
1501 vnc_mute_lout(wavnc_info *devc)
1503 unsigned int left, right;
1505 left = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1506 right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1508 if (devc->line_mute_state) {
1509 left &= ~1;
1510 right &= ~1;
1511 } else {
1512 left |= 1;
1513 right |= 1;
1515 waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1519 static int
1520 vnc_volume_slider(wavnc_info *devc)
1522 static signed int old_slider_volume;
1523 unsigned long flags;
1524 signed int volume = 255;
1526 *CSR_TIMER1_LOAD = 0x00ffffff;
1528 spin_lock_irqsave(&waveartist_lock, flags);
1530 outb(0xFF, 0x201);
1531 *CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1533 while (volume && (inb(0x201) & 0x01))
1534 volume--;
1536 *CSR_TIMER1_CNTL = 0;
1538 spin_unlock_irqrestore(&waveartist_lock,flags);
1540 volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1543 #ifndef REVERSE
1544 volume = 150 - (volume >> 5);
1545 #else
1546 volume = (volume >> 6) - 25;
1547 #endif
1549 if (volume < 0)
1550 volume = 0;
1552 if (volume > 100)
1553 volume = 100;
1556 * slider quite often reads +-8, so debounce this random noise
1558 if (abs(volume - old_slider_volume) > 7) {
1559 old_slider_volume = volume;
1561 if (debug_flg & DEBUG_MIXER)
1562 printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1565 return old_slider_volume;
1569 * Decode a recording mask into a mixer selection on the NetWinder
1570 * as follows:
1572 * OSS Source WA Source Actual source
1573 * SOUND_MASK_IMIX Mixer Mixer output (same as AD1848)
1574 * SOUND_MASK_LINE Line Line in
1575 * SOUND_MASK_LINE1 Left Mic Handset
1576 * SOUND_MASK_PHONEIN Left Aux Telephone microphone
1577 * SOUND_MASK_MIC Right Mic Builtin microphone
1579 static unsigned int
1580 netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1581 unsigned char *dev_l, unsigned char *dev_r)
1583 unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1585 if (recmask & SOUND_MASK_IMIX) {
1586 recmask = SOUND_MASK_IMIX;
1587 recdev_l = ADC_MUX_MIXER;
1588 recdev_r = ADC_MUX_MIXER;
1589 } else if (recmask & SOUND_MASK_LINE) {
1590 recmask = SOUND_MASK_LINE;
1591 recdev_l = ADC_MUX_LINE;
1592 recdev_r = ADC_MUX_LINE;
1593 } else if (recmask & SOUND_MASK_LINE1) {
1594 recmask = SOUND_MASK_LINE1;
1595 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1596 recdev_l = ADC_MUX_MIC;
1597 recdev_r = ADC_MUX_NONE;
1598 } else if (recmask & SOUND_MASK_PHONEIN) {
1599 recmask = SOUND_MASK_PHONEIN;
1600 waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1601 recdev_l = ADC_MUX_AUX1;
1602 recdev_r = ADC_MUX_NONE;
1603 } else if (recmask & SOUND_MASK_MIC) {
1604 recmask = SOUND_MASK_MIC;
1605 waveartist_cmd1(devc, WACMD_SET_MONO | 0x100); /* right */
1606 recdev_l = ADC_MUX_NONE;
1607 recdev_r = ADC_MUX_MIC;
1610 *dev_l = recdev_l;
1611 *dev_r = recdev_r;
1613 return recmask;
1616 static int
1617 netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1618 unsigned char lev_r)
1620 switch (dev) {
1621 case SOUND_MIXER_VOLUME:
1622 case SOUND_MIXER_SYNTH:
1623 case SOUND_MIXER_PCM:
1624 case SOUND_MIXER_LINE:
1625 case SOUND_MIXER_IGAIN:
1626 devc->levels[dev] = lev_l | lev_r << 8;
1627 break;
1629 case SOUND_MIXER_MIC: /* right mic only */
1630 devc->levels[SOUND_MIXER_MIC] &= 0xff;
1631 devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1632 break;
1634 case SOUND_MIXER_LINE1: /* left mic only */
1635 devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1636 devc->levels[SOUND_MIXER_MIC] |= lev_l;
1637 dev = SOUND_MIXER_MIC;
1638 break;
1640 case SOUND_MIXER_PHONEIN: /* left aux only */
1641 devc->levels[SOUND_MIXER_LINE1] = lev_l;
1642 dev = SOUND_MIXER_LINE1;
1643 break;
1645 case SOUND_MIXER_IMIX:
1646 case SOUND_MIXER_PHONEOUT:
1647 break;
1649 default:
1650 dev = -EINVAL;
1651 break;
1653 return dev;
1656 static int netwinder_get_mixer(wavnc_info *devc, int dev)
1658 int levels;
1660 switch (dev) {
1661 case SOUND_MIXER_VOLUME:
1662 case SOUND_MIXER_SYNTH:
1663 case SOUND_MIXER_PCM:
1664 case SOUND_MIXER_LINE:
1665 case SOUND_MIXER_IGAIN:
1666 levels = devc->levels[dev];
1667 break;
1669 case SOUND_MIXER_MIC: /* builtin mic: right mic only */
1670 levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1671 levels |= levels << 8;
1672 break;
1674 case SOUND_MIXER_LINE1: /* handset mic: left mic only */
1675 levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1676 levels |= levels << 8;
1677 break;
1679 case SOUND_MIXER_PHONEIN: /* phone mic: left aux1 only */
1680 levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1681 levels |= levels << 8;
1682 break;
1684 default:
1685 levels = 0;
1688 return levels;
1692 * Waveartist specific mixer information.
1694 static const struct waveartist_mixer_info netwinder_mixer = {
1695 .supported_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1696 SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
1697 SOUND_MASK_LINE | SOUND_MASK_MIC |
1698 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1699 SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1700 SOUND_MASK_IGAIN,
1702 .recording_devs = SOUND_MASK_LINE | SOUND_MASK_MIC |
1703 SOUND_MASK_IMIX | SOUND_MASK_LINE1 |
1704 SOUND_MASK_PHONEIN,
1706 .stereo_devs = SOUND_MASK_VOLUME | SOUND_MASK_SYNTH |
1707 SOUND_MASK_PCM | SOUND_MASK_LINE |
1708 SOUND_MASK_IMIX | SOUND_MASK_IGAIN,
1710 .select_input = netwinder_select_input,
1711 .decode_mixer = netwinder_decode_mixer,
1712 .get_mixer = netwinder_get_mixer,
1715 static void
1716 vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1718 if (!devc->no_autoselect) {
1719 if (devc->handset_detect) {
1720 recmask = SOUND_MASK_LINE1;
1721 devc->spkr_mute_state = devc->line_mute_state = 1;
1722 } else if (devc->telephone_detect) {
1723 recmask = SOUND_MASK_PHONEIN;
1724 devc->spkr_mute_state = devc->line_mute_state = 1;
1725 } else {
1726 /* unless someone has asked for LINE-IN,
1727 * we default to MIC
1729 if ((devc->recmask & SOUND_MASK_LINE) == 0)
1730 devc->recmask = SOUND_MASK_MIC;
1731 devc->spkr_mute_state = devc->line_mute_state = 0;
1733 vnc_mute_spkr(devc);
1734 vnc_mute_lout(devc);
1736 if (recmask != devc->recmask)
1737 waveartist_set_recmask(devc, recmask);
1741 static int
1742 vnc_slider(wavnc_info *devc)
1744 signed int slider_volume;
1745 unsigned int temp, old_hs, old_td;
1748 * read the "buttons" state.
1749 * Bit 4 = 0 means handset present
1750 * Bit 5 = 1 means phone offhook
1752 temp = inb(0x201);
1754 old_hs = devc->handset_detect;
1755 old_td = devc->telephone_detect;
1757 devc->handset_detect = !(temp & 0x10);
1758 devc->telephone_detect = !!(temp & 0x20);
1760 if (!devc->no_autoselect &&
1761 (old_hs != devc->handset_detect ||
1762 old_td != devc->telephone_detect))
1763 vnc_configure_mixer(devc, devc->recmask);
1765 slider_volume = vnc_volume_slider(devc);
1768 * If we're using software controlled volume, and
1769 * the slider moves by more than 20%, then we
1770 * switch back to slider controlled volume.
1772 if (abs(devc->slider_vol - slider_volume) > 20)
1773 devc->use_slider = 1;
1776 * use only left channel
1778 temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1780 if (slider_volume != temp && devc->use_slider) {
1781 devc->slider_vol = slider_volume;
1783 waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1784 slider_volume | slider_volume << 8);
1786 return 1;
1789 return 0;
1792 static void
1793 vnc_slider_tick(unsigned long data)
1795 int next_timeout;
1797 if (vnc_slider(adev_info + data))
1798 next_timeout = 5; // mixer reported change
1799 else
1800 next_timeout = VNC_TIMER_PERIOD;
1802 mod_timer(&vnc_timer, jiffies + next_timeout);
1805 static int
1806 vnc_private_ioctl(int dev, unsigned int cmd, int __user * arg)
1808 wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1809 int val;
1811 switch (cmd) {
1812 case SOUND_MIXER_PRIVATE1:
1814 u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1815 int val;
1817 if (get_user(val, arg))
1818 return -EFAULT;
1820 /* check if parameter is logical */
1821 if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1822 VNC_MUTE_LINE_OUT |
1823 VNC_DISABLE_AUTOSWITCH))
1824 return -EINVAL;
1826 prev_auto_state = devc->no_autoselect;
1827 prev_spkr_mute = devc->spkr_mute_state;
1828 prev_line_mute = devc->line_mute_state;
1830 devc->no_autoselect = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1831 devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1832 devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1834 if (prev_spkr_mute != devc->spkr_mute_state)
1835 vnc_mute_spkr(devc);
1837 if (prev_line_mute != devc->line_mute_state)
1838 vnc_mute_lout(devc);
1840 if (prev_auto_state != devc->no_autoselect)
1841 vnc_configure_mixer(devc, devc->recmask);
1843 return 0;
1846 case SOUND_MIXER_PRIVATE2:
1847 if (get_user(val, arg))
1848 return -EFAULT;
1850 switch (val) {
1851 #define VNC_SOUND_PAUSE 0x53 //to pause the DSP
1852 #define VNC_SOUND_RESUME 0x57 //to unpause the DSP
1853 case VNC_SOUND_PAUSE:
1854 waveartist_cmd1(devc, 0x16);
1855 break;
1857 case VNC_SOUND_RESUME:
1858 waveartist_cmd1(devc, 0x18);
1859 break;
1861 default:
1862 return -EINVAL;
1864 return 0;
1866 /* private ioctl to allow bulk access to waveartist */
1867 case SOUND_MIXER_PRIVATE3:
1869 unsigned long flags;
1870 int mixer_reg[15], i, val;
1872 if (get_user(val, arg))
1873 return -EFAULT;
1874 if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1875 return -EFAULT;
1877 switch (mixer_reg[14]) {
1878 case MIXER_PRIVATE3_RESET:
1879 waveartist_mixer_reset(devc);
1880 break;
1882 case MIXER_PRIVATE3_WRITE:
1883 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1884 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1885 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1886 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1887 waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1889 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1890 waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1891 break;
1893 case MIXER_PRIVATE3_READ:
1894 spin_lock_irqsave(&waveartist_lock, flags);
1896 for (i = 0x30; i < 14 << 8; i += 1 << 8)
1897 waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1899 spin_unlock_irqrestore(&waveartist_lock, flags);
1901 if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1902 return -EFAULT;
1903 break;
1905 default:
1906 return -EINVAL;
1908 return 0;
1911 /* read back the state from PRIVATE1 */
1912 case SOUND_MIXER_PRIVATE4:
1913 val = (devc->spkr_mute_state ? VNC_MUTE_INTERNAL_SPKR : 0) |
1914 (devc->line_mute_state ? VNC_MUTE_LINE_OUT : 0) |
1915 (devc->handset_detect ? VNC_HANDSET_DETECT : 0) |
1916 (devc->telephone_detect ? VNC_PHONE_DETECT : 0) |
1917 (devc->no_autoselect ? VNC_DISABLE_AUTOSWITCH : 0);
1919 return put_user(val, arg) ? -EFAULT : 0;
1922 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1924 * special case for master volume: if we
1925 * received this call - switch from hw
1926 * volume control to a software volume
1927 * control, till the hw volume is modified
1928 * to signal that user wants to be back in
1929 * hardware...
1931 if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1932 devc->use_slider = 0;
1934 /* speaker output */
1935 if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1936 unsigned int val, l, r;
1938 if (get_user(val, arg))
1939 return -EFAULT;
1941 l = val & 0x7f;
1942 r = (val & 0x7f00) >> 8;
1943 val = (l + r) / 2;
1944 devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1945 devc->spkr_mute_state = (val <= 50);
1946 vnc_mute_spkr(devc);
1947 return 0;
1951 return -ENOIOCTLCMD;
1954 #endif
1956 static struct address_info cfg;
1958 static int attached;
1960 static int __initdata io = 0;
1961 static int __initdata irq = 0;
1962 static int __initdata dma = 0;
1963 static int __initdata dma2 = 0;
1966 static int __init init_waveartist(void)
1968 const struct waveartist_mixer_info *mix;
1970 if (!io && machine_is_netwinder()) {
1972 * The NetWinder WaveArtist is at a fixed address.
1973 * If the user does not supply an address, use the
1974 * well-known parameters.
1976 io = 0x250;
1977 irq = 12;
1978 dma = 3;
1979 dma2 = 7;
1982 mix = &waveartist_mixer;
1983 #ifdef CONFIG_ARCH_NETWINDER
1984 if (machine_is_netwinder())
1985 mix = &netwinder_mixer;
1986 #endif
1988 cfg.io_base = io;
1989 cfg.irq = irq;
1990 cfg.dma = dma;
1991 cfg.dma2 = dma2;
1993 if (!probe_waveartist(&cfg))
1994 return -ENODEV;
1996 attach_waveartist(&cfg, mix);
1997 attached = 1;
1999 return 0;
2002 static void __exit cleanup_waveartist(void)
2004 if (attached)
2005 unload_waveartist(&cfg);
2008 module_init(init_waveartist);
2009 module_exit(cleanup_waveartist);
2011 #ifndef MODULE
2012 static int __init setup_waveartist(char *str)
2014 /* io, irq, dma, dma2 */
2015 int ints[5];
2017 str = get_options(str, ARRAY_SIZE(ints), ints);
2019 io = ints[1];
2020 irq = ints[2];
2021 dma = ints[3];
2022 dma2 = ints[4];
2024 return 1;
2026 __setup("waveartist=", setup_waveartist);
2027 #endif
2029 MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2030 module_param(io, int, 0); /* IO base */
2031 module_param(irq, int, 0); /* IRQ */
2032 module_param(dma, int, 0); /* DMA */
2033 module_param(dma2, int, 0); /* DMA2 */
2034 MODULE_LICENSE("GPL");