1/*
2 * linux/drivers/sound/waveartist.c
3 *
4 * The low level driver for the RWA010 Rockwell Wave Artist
5 * codec chip used in the Rebel.com NetWinder.
6 *
7 * Cleaned up and integrated into 2.1 by Russell King (rmk@arm.linux.org.uk)
8 * and Pat Beirne (patb@corel.ca)
9 *
10 *
11 * Copyright (C) by Rebel.com 1998-1999
12 *
13 * RWA010 specs received under NDA from Rockwell
14 *
15 * Copyright (C) by Hannu Savolainen 1993-1997
16 *
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.
20 *
21 * Changes:
22 * 11-10-2000	Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
23 *		Added __init to waveartist_init()
24 */
25
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
33
34#define debug_flg (0)
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/config.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>
44
45#include <asm/system.h>
46
47#include "sound_config.h"
48#include "waveartist.h"
49
50#ifdef CONFIG_ARM
51#include <asm/hardware.h>
52#include <asm/mach-types.h>
53#endif
54
55#ifndef NO_DMA
56#define NO_DMA	255
57#endif
58
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)
67
68static 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		 */
94};
95
96typedef struct {
97	struct address_info  hw;	/* hardware */
98	char		*chip_name;
99
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;
107
108	/* Mixer parameters */
109	const struct waveartist_mixer_info *mix;
110
111	unsigned short	*levels;	   /* cache of volume settings   */
112	int		recmask;	   /* currently enabled recording device! */
113
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;
124
125/*
126 * This is the implementation specific mixer information.
127 */
128struct waveartist_mixer_info {
129	unsigned int	supported_devs;	   /* Supported devices */
130	unsigned int	recording_devs;	   /* Recordable devies */
131	unsigned int	stereo_devs;	   /* Stereo devices	*/
132
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);
138};
139
140typedef struct wavnc_port_info {
141	int		open_mode;
142	int		speed;
143	int		channels;
144	int		audio_format;
145} wavnc_port_info;
146
147static int		nr_waveartist_devs;
148static wavnc_info	adev_info[MAX_AUDIO_DEV];
149static spinlock_t	waveartist_lock = SPIN_LOCK_UNLOCKED;
150
151#ifndef CONFIG_ARCH_NETWINDER
152#define machine_is_netwinder() 0
153#else
154static struct timer_list vnc_timer;
155static void vnc_configure_mixer(wavnc_info *devc, unsigned int input_mask);
156static int vnc_private_ioctl(int dev, unsigned int cmd, caddr_t arg);
157static void vnc_slider_tick(unsigned long data);
158#endif
159
160static inline void
161waveartist_set_ctlr(struct address_info *hw, unsigned char clear, unsigned char set)
162{
163	unsigned int ctlr_port = hw->io_base + CTLR;
164
165	clear = ~clear & inb(ctlr_port);
166
167	outb(clear | set, ctlr_port);
168}
169
170/* Toggle IRQ acknowledge line
171 */
172static inline void
173waveartist_iack(wavnc_info *devc)
174{
175	unsigned int ctlr_port = devc->hw.io_base + CTLR;
176	int old_ctlr;
177
178	old_ctlr = inb(ctlr_port) & ~IRQ_ACK;
179
180	outb(old_ctlr | IRQ_ACK, ctlr_port);
181	outb(old_ctlr, ctlr_port);
182}
183
184static inline int
185waveartist_sleep(int timeout_ms)
186{
187	unsigned int timeout = timeout_ms * 10 * HZ / 100;
188
189	do {
190		set_current_state(TASK_INTERRUPTIBLE);
191		timeout = schedule_timeout(timeout);
192	} while (timeout);
193
194	return 0;
195}
196
197static int
198waveartist_reset(wavnc_info *devc)
199{
200	struct address_info *hw = &devc->hw;
201	unsigned int timeout, res = -1;
202
203	waveartist_set_ctlr(hw, -1, RESET);
204	waveartist_sleep(2);
205	waveartist_set_ctlr(hw, RESET, 0);
206
207	timeout = 500;
208	do {
209		mdelay(2);
210
211		if (inb(hw->io_base + STATR) & CMD_RF) {
212			res = inw(hw->io_base + CMDR);
213			if (res == 0x55aa)
214				break;
215		}
216	} while (--timeout);
217
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;
224	}
225	return 0;
226}
227
228/* Helper function to send and receive words
229 * from WaveArtist.  It handles all the handshaking
230 * and can send or receive multiple words.
231 */
232static int
233waveartist_cmd(wavnc_info *devc,
234		int nr_cmd, unsigned int *cmd,
235		int nr_resp, unsigned int *resp)
236{
237	unsigned int io_base = devc->hw.io_base;
238	unsigned int timed_out = 0;
239	unsigned int i;
240
241	if (debug_flg & DEBUG_CMD) {
242		printk("waveartist_cmd: cmd=");
243
244		for (i = 0; i < nr_cmd; i++)
245			printk("%04X ", cmd[i]);
246
247		printk("\n");
248	}
249
250	if (inb(io_base + STATR) & CMD_RF) {
251		int old_data;
252
253		/* flush the port
254		 */
255
256		old_data = inw(io_base + CMDR);
257
258		if (debug_flg & DEBUG_CMD)
259			printk("flushed %04X...", old_data);
260
261		udelay(10);
262	}
263
264	for (i = 0; !timed_out && i < nr_cmd; i++) {
265		int count;
266
267		for (count = 5000; count; count--)
268			if (inb(io_base + STATR) & CMD_WE)
269				break;
270
271		if (!count)
272			timed_out = 1;
273		else
274			outw(cmd[i], io_base + CMDR);
275	}
276
277	for (i = 0; !timed_out && i < nr_resp; i++) {
278		int count;
279
280		for (count = 5000; count; count--)
281			if (inb(io_base + STATR) & CMD_RF)
282				break;
283
284		if (!count)
285			timed_out = 1;
286		else
287			resp[i] = inw(io_base + CMDR);
288	}
289
290	if (debug_flg & DEBUG_CMD) {
291		if (!timed_out) {
292			printk("waveartist_cmd: resp=");
293
294			for (i = 0; i < nr_resp; i++)
295				printk("%04X ", resp[i]);
296
297			printk("\n");
298		} else
299			printk("waveartist_cmd: timed out\n");
300	}
301
302	return timed_out ? 1 : 0;
303}
304
305/*
306 * Send one command word
307 */
308static inline int
309waveartist_cmd1(wavnc_info *devc, unsigned int cmd)
310{
311	return waveartist_cmd(devc, 1, &cmd, 0, NULL);
312}
313
314/*
315 * Send one command, receive one word
316 */
317static inline unsigned int
318waveartist_cmd1_r(wavnc_info *devc, unsigned int cmd)
319{
320	unsigned int ret;
321
322	waveartist_cmd(devc, 1, &cmd, 1, &ret);
323
324	return ret;
325}
326
327/*
328 * Send a double command, receive one
329 * word (and throw it away)
330 */
331static inline int
332waveartist_cmd2(wavnc_info *devc, unsigned int cmd, unsigned int arg)
333{
334	unsigned int vals[2];
335
336	vals[0] = cmd;
337	vals[1] = arg;
338
339	return waveartist_cmd(devc, 2, vals, 1, vals);
340}
341
342/*
343 * Send a triple command
344 */
345static inline int
346waveartist_cmd3(wavnc_info *devc, unsigned int cmd,
347		unsigned int arg1, unsigned int arg2)
348{
349	unsigned int vals[3];
350
351	vals[0] = cmd;
352	vals[1] = arg1;
353	vals[2] = arg2;
354
355	return waveartist_cmd(devc, 3, vals, 0, NULL);
356}
357
358static int
359waveartist_getrev(wavnc_info *devc, char *rev)
360{
361	unsigned int temp[2];
362	unsigned int cmd = WACMD_GETREV;
363
364	waveartist_cmd(devc, 1, &cmd, 2, temp);
365
366	rev[0] = temp[0] >> 8;
367	rev[1] = temp[0] & 255;
368	rev[2] = '\0';
369
370	return temp[0];
371}
372
373static void waveartist_halt_output(int dev);
374static void waveartist_halt_input(int dev);
375static void waveartist_halt(int dev);
376static void waveartist_trigger(int dev, int state);
377
378static int
379waveartist_open(int dev, int mode)
380{
381	wavnc_info	*devc;
382	wavnc_port_info	*portc;
383	unsigned long	flags;
384
385	if (dev < 0 || dev >= num_audiodevs)
386		return -ENXIO;
387
388	devc  = (wavnc_info *) audio_devs[dev]->devc;
389	portc = (wavnc_port_info *) audio_devs[dev]->portc;
390
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;
395	}
396
397	devc->audio_mode  = 0;
398	devc->open_mode  |= mode;
399	portc->open_mode  = mode;
400	waveartist_trigger(dev, 0);
401
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);
407
408	return 0;
409}
410
411static void
412waveartist_close(int dev)
413{
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;
417
418	spin_lock_irqsave(&waveartist_lock, flags);
419
420	waveartist_halt(dev);
421
422	devc->audio_mode = 0;
423	devc->open_mode &= ~portc->open_mode;
424	portc->open_mode = 0;
425
426	spin_unlock_irqrestore(&waveartist_lock, flags);
427}
428
429static void
430waveartist_output_block(int dev, unsigned long buf, int __count, int intrflag)
431{
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;
436
437	if (debug_flg & DEBUG_OUT)
438		printk("waveartist: output block, buf=0x%lx, count=0x%x...\n",
439			buf, count);
440	/*
441	 * 16 bit data
442	 */
443	if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
444		count >>= 1;
445
446	if (portc->channels > 1)
447		count >>= 1;
448
449	count -= 1;
450
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
458			 */
459	}
460
461	spin_lock_irqsave(&waveartist_lock, flags);
462
463	/*
464	 * set sample count
465	 */
466	waveartist_cmd2(devc, WACMD_OUTPUTSIZE, count);
467
468	devc->xfer_count = count;
469	devc->audio_mode |= PCM_ENABLE_OUTPUT;
470
471	spin_unlock_irqrestore(&waveartist_lock, flags);
472}
473
474static void
475waveartist_start_input(int dev, unsigned long buf, int __count, int intrflag)
476{
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;
481
482	if (debug_flg & DEBUG_IN)
483		printk("waveartist: start input, buf=0x%lx, count=0x%x...\n",
484			buf, count);
485
486	if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))	/* 16 bit data */
487		count >>= 1;
488
489	if (portc->channels > 1)
490		count >>= 1;
491
492	count -= 1;
493
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
501			 */
502	}
503
504	spin_lock_irqsave(&waveartist_lock, flags);
505
506	/*
507	 * set sample count
508	 */
509	waveartist_cmd2(devc, WACMD_INPUTSIZE, count);
510
511	devc->xfer_count = count;
512	devc->audio_mode |= PCM_ENABLE_INPUT;
513
514	spin_unlock_irqrestore(&waveartist_lock, flags);
515}
516
517static int
518waveartist_ioctl(int dev, unsigned int cmd, caddr_t arg)
519{
520	return -EINVAL;
521}
522
523static unsigned int
524waveartist_get_speed(wavnc_port_info *portc)
525{
526	unsigned int speed;
527
528	/*
529	 * program the speed, channels, bits
530	 */
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 {
540		/*
541		 * non-standard - just calculate
542		 */
543		speed = portc->speed << 16;
544
545		speed = (speed / 44100) & 65535;
546	}
547
548	return speed;
549}
550
551static unsigned int
552waveartist_get_bits(wavnc_port_info *portc)
553{
554	unsigned int bits;
555
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
562
563	return bits;
564}
565
566static int
567waveartist_prepare_for_input(int dev, int bsize, int bcount)
568{
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;
573
574	if (devc->audio_mode)
575		return 0;
576
577	speed = waveartist_get_speed(portc);
578	bits  = waveartist_get_bits(portc);
579
580	spin_lock_irqsave(&waveartist_lock, flags);
581
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);
585
586	if (waveartist_cmd2(devc, WACMD_INPUTCHANNELS, portc->channels))
587		printk(KERN_WARNING "waveartist: error setting record "
588		       "to %d channels\n", portc->channels);
589
590	/*
591	 * write cmd SetSampleSpeedTimeConstant
592	 */
593	if (waveartist_cmd2(devc, WACMD_INPUTSPEED, speed))
594		printk(KERN_WARNING "waveartist: error setting the record "
595		       "speed to %dHz.\n", portc->speed);
596
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);
600
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);
604
605	devc->xfer_count = 0;
606	spin_unlock_irqrestore(&waveartist_lock, flags);
607	waveartist_halt_input(dev);
608
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));
616	}
617
618	return 0;
619}
620
621static int
622waveartist_prepare_for_output(int dev, int bsize, int bcount)
623{
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;
628
629	/*
630	 * program the speed, channels, bits
631	 */
632	speed = waveartist_get_speed(portc);
633	bits  = waveartist_get_bits(portc);
634
635	spin_lock_irqsave(&waveartist_lock, flags);
636
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);
641
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);
645
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);
649
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);
653
654	devc->xfer_count = 0;
655	spin_unlock_irqrestore(&waveartist_lock, flags);
656	waveartist_halt_output(dev);
657
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));
662	}
663
664	return 0;
665}
666
667static void
668waveartist_halt(int dev)
669{
670	wavnc_port_info	*portc = (wavnc_port_info *) audio_devs[dev]->portc;
671	wavnc_info	*devc;
672
673	if (portc->open_mode & OPEN_WRITE)
674		waveartist_halt_output(dev);
675
676	if (portc->open_mode & OPEN_READ)
677		waveartist_halt_input(dev);
678
679	devc = (wavnc_info *) audio_devs[dev]->devc;
680	devc->audio_mode = 0;
681}
682
683static void
684waveartist_halt_input(int dev)
685{
686	wavnc_info	*devc = (wavnc_info *) audio_devs[dev]->devc;
687	unsigned long	flags;
688
689	spin_lock_irqsave(&waveartist_lock, flags);
690
691	/*
692	 * Stop capture
693	 */
694	waveartist_cmd1(devc, WACMD_INPUTSTOP);
695
696	devc->audio_mode &= ~PCM_ENABLE_INPUT;
697
698	/*
699	 * Clear interrupt by toggling
700	 * the IRQ_ACK bit in CTRL
701	 */
702	if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
703		waveartist_iack(devc);
704
705//	devc->audio_mode &= ~PCM_ENABLE_INPUT;
706
707	spin_unlock_irqrestore(&waveartist_lock, flags);
708}
709
710static void
711waveartist_halt_output(int dev)
712{
713	wavnc_info	*devc = (wavnc_info *) audio_devs[dev]->devc;
714	unsigned long	flags;
715
716	spin_lock_irqsave(&waveartist_lock, flags);
717
718	waveartist_cmd1(devc, WACMD_OUTPUTSTOP);
719
720	devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
721
722	/*
723	 * Clear interrupt by toggling
724	 * the IRQ_ACK bit in CTRL
725	 */
726	if (inb(devc->hw.io_base + STATR) & IRQ_REQ)
727		waveartist_iack(devc);
728
729//	devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
730
731	spin_unlock_irqrestore(&waveartist_lock, flags);
732}
733
734static void
735waveartist_trigger(int dev, int state)
736{
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;
740
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");
748	}
749
750	spin_lock_irqsave(&waveartist_lock, flags);
751
752	state &= devc->audio_mode;
753
754	if (portc->open_mode & OPEN_READ &&
755	    state & PCM_ENABLE_INPUT)
756		/*
757		 * enable ADC Data Transfer to PC
758		 */
759		waveartist_cmd1(devc, WACMD_INPUTSTART);
760
761	if (portc->open_mode & OPEN_WRITE &&
762	    state & PCM_ENABLE_OUTPUT)
763		/*
764		 * enable DAC data transfer from PC
765		 */
766		waveartist_cmd1(devc, WACMD_OUTPUTSTART);
767
768	spin_unlock_irqrestore(&waveartist_lock, flags);
769}
770
771static int
772waveartist_set_speed(int dev, int arg)
773{
774	wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
775
776	if (arg <= 0)
777		return portc->speed;
778
779	if (arg < 5000)
780		arg = 5000;
781	if (arg > 44100)
782		arg = 44100;
783
784	portc->speed = arg;
785	return portc->speed;
786
787}
788
789static short
790waveartist_set_channels(int dev, short arg)
791{
792	wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
793
794	if (arg != 1 && arg != 2)
795		return portc->channels;
796
797	portc->channels = arg;
798	return arg;
799}
800
801static unsigned int
802waveartist_set_bits(int dev, unsigned int arg)
803{
804	wavnc_port_info *portc = (wavnc_port_info *) audio_devs[dev]->portc;
805
806	if (arg == 0)
807		return portc->audio_format;
808
809	if ((arg != AFMT_U8) && (arg != AFMT_S16_LE) && (arg != AFMT_S8))
810		arg = AFMT_U8;
811
812	portc->audio_format = arg;
813
814	return arg;
815}
816
817static 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
833};
834
835
836static void
837waveartist_intr(int irq, void *dev_id, struct pt_regs *regs)
838{
839	wavnc_info *devc = (wavnc_info *)dev_id;
840	int	   irqstatus, status;
841
842	irqstatus = inb(devc->hw.io_base + IRQSTAT);
843	status    = inb(devc->hw.io_base + STATR);
844
845	if (debug_flg & DEBUG_INTR)
846		printk("waveartist_intr: stat=%02x, irqstat=%02x\n",
847		       status, irqstatus);
848
849	if (status & IRQ_REQ)	/* Clear interrupt */
850		waveartist_iack(devc);
851	else
852		printk(KERN_WARNING "waveartist: unexpected interrupt\n");
853
854	if (irqstatus & 0x01) {
855		int temp = 1;
856
857		/* PCM buffer done
858		 */
859		if ((status & DMA0) && (devc->audio_mode & PCM_ENABLE_OUTPUT)) {
860			DMAbuf_outputintr(devc->playback_dev, 1);
861			temp = 0;
862		}
863		if ((status & DMA1) && (devc->audio_mode & PCM_ENABLE_INPUT)) {
864			DMAbuf_inputintr(devc->record_dev);
865			temp = 0;
866		}
867		if (temp)	//default:
868			printk(KERN_WARNING "waveartist: Unknown interrupt\n");
869	}
870	if (irqstatus & 0x2)
871		// We do not use SB mode natively...
872		printk(KERN_WARNING "waveartist: Unexpected SB interrupt...\n");
873}
874
875/* -------------------------------------------------------------------------
876 * Mixer stuff
877 */
878struct mix_ent {
879	unsigned char	reg_l;
880	unsigned char	reg_r;
881	unsigned char	shift;
882	unsigned char	max;
883};
884
885static const struct mix_ent mix_devs[SOUND_MIXER_NRDEVICES] = {
886	{ 2, 6, 1,  7 }, /* SOUND_MIXER_VOLUME   */
887	{ 0, 0, 0,  0 }, /* SOUND_MIXER_BASS     */
888	{ 0, 0, 0,  0 }, /* SOUND_MIXER_TREBLE   */
889	{ 0, 0, 0,  0 }, /* SOUND_MIXER_SYNTH    */
890	{ 0, 0, 0,  0 }, /* SOUND_MIXER_PCM      */
891	{ 0, 0, 0,  0 }, /* SOUND_MIXER_SPEAKER  */
892	{ 0, 4, 6, 31 }, /* SOUND_MIXER_LINE     */
893	{ 2, 6, 4,  3 }, /* SOUND_MIXER_MIC      */
894	{ 0, 0, 0,  0 }, /* SOUND_MIXER_CD       */
895	{ 0, 0, 0,  0 }, /* SOUND_MIXER_IMIX     */
896	{ 0, 0, 0,  0 }, /* SOUND_MIXER_ALTPCM   */
897	{ 0, 0, 0,  0 }, /* SOUND_MIXER_RECLEV   */
898	{ 3, 7, 0,  7 }, /* SOUND_MIXER_IGAIN    */
899	{ 0, 0, 0,  0 }, /* SOUND_MIXER_OGAIN    */
900	{ 0, 4, 1, 31 }, /* SOUND_MIXER_LINE1    */
901	{ 1, 5, 6, 31 }, /* SOUND_MIXER_LINE2    */
902	{ 0, 0, 0,  0 }, /* SOUND_MIXER_LINE3    */
903	{ 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL1 */
904	{ 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL2 */
905	{ 0, 0, 0,  0 }, /* SOUND_MIXER_DIGITAL3 */
906	{ 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEIN  */
907	{ 0, 0, 0,  0 }, /* SOUND_MIXER_PHONEOUT */
908	{ 0, 0, 0,  0 }, /* SOUND_MIXER_VIDEO    */
909	{ 0, 0, 0,  0 }, /* SOUND_MIXER_RADIO    */
910	{ 0, 0, 0,  0 }  /* SOUND_MIXER_MONITOR  */
911};
912
913static void
914waveartist_mixer_update(wavnc_info *devc, int whichDev)
915{
916	unsigned int lev_left, lev_right;
917
918	lev_left  = devc->levels[whichDev] & 0xff;
919	lev_right = devc->levels[whichDev] >> 8;
920
921	if (lev_left > 100)
922		lev_left = 100;
923	if (lev_right > 100)
924		lev_right = 100;
925
926#define SCALE(lev,max)	((lev) * (max) / 100)
927
928	if (machine_is_netwinder() && whichDev == SOUND_MIXER_PHONEOUT)
929		whichDev = SOUND_MIXER_VOLUME;
930
931	if (mix_devs[whichDev].reg_l || mix_devs[whichDev].reg_r) {
932		const struct mix_ent *mix = mix_devs + whichDev;
933		unsigned int mask, left, right;
934
935		mask = mix->max << mix->shift;
936		lev_left  = SCALE(lev_left,  mix->max) << mix->shift;
937		lev_right = SCALE(lev_right, mix->max) << mix->shift;
938
939		/* read left setting */
940		left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
941					       mix->reg_l << 8);
942
943		/* read right setting */
944		right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL |
945						mix->reg_r << 8);
946
947		left  = (left  & ~mask) | (lev_left  & mask);
948		right = (right & ~mask) | (lev_right & mask);
949
950		/* write left,right back */
951		waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
952	} else {
953		switch(whichDev) {
954		case SOUND_MIXER_PCM:
955			waveartist_cmd3(devc, WACMD_SET_LEVEL,
956					SCALE(lev_left,  32767),
957					SCALE(lev_right, 32767));
958			break;
959
960		case SOUND_MIXER_SYNTH:
961			waveartist_cmd3(devc, 0x0100 | WACMD_SET_LEVEL,
962					SCALE(lev_left,  32767),
963					SCALE(lev_right, 32767));
964			break;
965		}
966	}
967}
968
969/*
970 * Set the ADC MUX to the specified values.  We do NOT do any
971 * checking of the values passed, since we assume that the
972 * relevant *_select_input function has done that for us.
973 */
974static void
975waveartist_set_adc_mux(wavnc_info *devc, char left_dev, char right_dev)
976{
977	unsigned int reg_08, reg_09;
978
979	reg_08 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0800);
980	reg_09 = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x0900);
981
982	reg_08 = (reg_08 & ~0x3f) | right_dev << 3 | left_dev;
983
984	waveartist_cmd3(devc, WACMD_SET_MIXER, reg_08, reg_09);
985}
986
987/*
988 * Decode a recording mask into a mixer selection as follows:
989 *
990 *     OSS Source	WA Source	Actual source
991 *  SOUND_MASK_IMIX	Mixer		Mixer output (same as AD1848)
992 *  SOUND_MASK_LINE	Line		Line in
993 *  SOUND_MASK_LINE1	Aux 1		Aux 1 in
994 *  SOUND_MASK_LINE2	Aux 2		Aux 2 in
995 *  SOUND_MASK_MIC	Mic		Microphone
996 */
997static unsigned int
998waveartist_select_input(wavnc_info *devc, unsigned int recmask,
999			unsigned char *dev_l, unsigned char *dev_r)
1000{
1001	unsigned int recdev = ADC_MUX_NONE;
1002
1003	if (recmask & SOUND_MASK_IMIX) {
1004		recmask = SOUND_MASK_IMIX;
1005		recdev = ADC_MUX_MIXER;
1006	} else if (recmask & SOUND_MASK_LINE2) {
1007		recmask = SOUND_MASK_LINE2;
1008		recdev = ADC_MUX_AUX2;
1009	} else if (recmask & SOUND_MASK_LINE1) {
1010		recmask = SOUND_MASK_LINE1;
1011		recdev = ADC_MUX_AUX1;
1012	} else if (recmask & SOUND_MASK_LINE) {
1013		recmask = SOUND_MASK_LINE;
1014		recdev = ADC_MUX_LINE;
1015	} else if (recmask & SOUND_MASK_MIC) {
1016		recmask = SOUND_MASK_MIC;
1017		recdev = ADC_MUX_MIC;
1018	}
1019
1020	*dev_l = *dev_r = recdev;
1021
1022	return recmask;
1023}
1024
1025static int
1026waveartist_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1027			unsigned char lev_r)
1028{
1029	switch (dev) {
1030	case SOUND_MIXER_VOLUME:
1031	case SOUND_MIXER_SYNTH:
1032	case SOUND_MIXER_PCM:
1033	case SOUND_MIXER_LINE:
1034	case SOUND_MIXER_MIC:
1035	case SOUND_MIXER_IGAIN:
1036	case SOUND_MIXER_LINE1:
1037	case SOUND_MIXER_LINE2:
1038		devc->levels[dev] = lev_l | lev_r << 8;
1039		break;
1040
1041	case SOUND_MIXER_IMIX:
1042		break;
1043
1044	default:
1045		dev = -EINVAL;
1046		break;
1047	}
1048
1049	return dev;
1050}
1051
1052static int waveartist_get_mixer(wavnc_info *devc, int dev)
1053{
1054	return devc->levels[dev];
1055}
1056
1057static const struct waveartist_mixer_info waveartist_mixer = {
1058	supported_devs:	SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN,
1059	recording_devs:	SOUND_MASK_LINE  | SOUND_MASK_MIC   |
1060			SOUND_MASK_LINE1 | SOUND_MASK_LINE2 |
1061			SOUND_MASK_IMIX,
1062	stereo_devs:	(SUPPORTED_MIXER_DEVICES | SOUND_MASK_IGAIN) & ~
1063			(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX),
1064	select_input:	waveartist_select_input,
1065	decode_mixer:	waveartist_decode_mixer,
1066	get_mixer:	waveartist_get_mixer,
1067};
1068
1069static void
1070waveartist_set_recmask(wavnc_info *devc, unsigned int recmask)
1071{
1072	unsigned char dev_l, dev_r;
1073
1074	recmask &= devc->mix->recording_devs;
1075
1076	/*
1077	 * If more than one recording device selected,
1078	 * disable the device that is currently in use.
1079	 */
1080	if (hweight32(recmask) > 1)
1081		recmask &= ~devc->recmask;
1082
1083	/*
1084	 * Translate the recording device mask into
1085	 * the ADC multiplexer settings.
1086	 */
1087	devc->recmask = devc->mix->select_input(devc, recmask,
1088						&dev_l, &dev_r);
1089
1090	waveartist_set_adc_mux(devc, dev_l, dev_r);
1091}
1092
1093static int
1094waveartist_set_mixer(wavnc_info *devc, int dev, unsigned int level)
1095{
1096	unsigned int lev_left  = level & 0x00ff;
1097	unsigned int lev_right = (level & 0xff00) >> 8;
1098
1099	if (lev_left > 100)
1100		lev_left = 100;
1101	if (lev_right > 100)
1102		lev_right = 100;
1103
1104	/*
1105	 * Mono devices have their right volume forced to their
1106	 * left volume.  (from ALSA driver OSS emulation).
1107	 */
1108	if (!(devc->mix->stereo_devs & (1 << dev)))
1109		lev_right = lev_left;
1110
1111	dev = devc->mix->decode_mixer(devc, dev, lev_left, lev_right);
1112
1113	if (dev >= 0)
1114		waveartist_mixer_update(devc, dev);
1115
1116	return dev < 0 ? dev : 0;
1117}
1118
1119static int
1120waveartist_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
1121{
1122	wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1123	int ret = 0, val, nr;
1124
1125	/*
1126	 * All SOUND_MIXER_* ioctls use type 'M'
1127	 */
1128	if (((cmd >> 8) & 255) != 'M')
1129		return -ENOIOCTLCMD;
1130
1131#ifdef CONFIG_ARCH_NETWINDER
1132	if (machine_is_netwinder()) {
1133		ret = vnc_private_ioctl(dev, cmd, arg);
1134		if (ret != -ENOIOCTLCMD)
1135			return ret;
1136		else
1137			ret = 0;
1138	}
1139#endif
1140
1141	nr = cmd & 0xff;
1142
1143	if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1144		if (get_user(val, (int *)arg))
1145			return -EFAULT;
1146
1147		switch (nr) {
1148		case SOUND_MIXER_RECSRC:
1149			waveartist_set_recmask(devc, val);
1150			break;
1151
1152		default:
1153			ret = -EINVAL;
1154			if (nr < SOUND_MIXER_NRDEVICES &&
1155			    devc->mix->supported_devs & (1 << nr))
1156				ret = waveartist_set_mixer(devc, nr, val);
1157		}
1158	}
1159
1160	if (ret == 0 && _SIOC_DIR(cmd) & _SIOC_READ) {
1161		ret = -EINVAL;
1162
1163		switch (nr) {
1164		case SOUND_MIXER_RECSRC:
1165			ret = devc->recmask;
1166			break;
1167
1168		case SOUND_MIXER_DEVMASK:
1169			ret = devc->mix->supported_devs;
1170			break;
1171
1172		case SOUND_MIXER_STEREODEVS:
1173			ret = devc->mix->stereo_devs;
1174			break;
1175
1176		case SOUND_MIXER_RECMASK:
1177			ret = devc->mix->recording_devs;
1178			break;
1179
1180		case SOUND_MIXER_CAPS:
1181			ret = SOUND_CAP_EXCL_INPUT;
1182			break;
1183
1184		default:
1185			if (nr < SOUND_MIXER_NRDEVICES)
1186				ret = devc->mix->get_mixer(devc, nr);
1187			break;
1188		}
1189
1190		if (ret >= 0)
1191			ret = put_user(ret, (int *)arg) ? -EFAULT : 0;
1192	}
1193
1194	return ret;
1195}
1196
1197static struct mixer_operations waveartist_mixer_operations =
1198{
1199	owner:	THIS_MODULE,
1200	id:	"WaveArtist",
1201	name:	"WaveArtist",
1202	ioctl:	waveartist_mixer_ioctl
1203};
1204
1205static void
1206waveartist_mixer_reset(wavnc_info *devc)
1207{
1208	int i;
1209
1210	if (debug_flg & DEBUG_MIXER)
1211		printk("%s: mixer_reset\n", devc->hw.name);
1212
1213	/*
1214	 * reset mixer cmd
1215	 */
1216	waveartist_cmd1(devc, WACMD_RST_MIXER);
1217
1218	/*
1219	 * set input for ADC to come from 'quiet'
1220	 * turn on default modes
1221	 */
1222	waveartist_cmd3(devc, WACMD_SET_MIXER, 0x9800, 0xa836);
1223
1224	/*
1225	 * set mixer input select to none, RX filter gains 0 dB
1226	 */
1227	waveartist_cmd3(devc, WACMD_SET_MIXER, 0x4c00, 0x8c00);
1228
1229	/*
1230	 * set bit 0 reg 2 to 1 - unmute MonoOut
1231	 */
1232	waveartist_cmd3(devc, WACMD_SET_MIXER, 0x2801, 0x6800);
1233
1234	/* set default input device = internal mic
1235	 * current recording device = none
1236	 */
1237	waveartist_set_recmask(devc, 0);
1238
1239	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1240		waveartist_mixer_update(devc, i);
1241}
1242
1243static int __init waveartist_init(wavnc_info *devc)
1244{
1245	wavnc_port_info *portc;
1246	char rev[3], dev_name[64];
1247	int my_dev;
1248
1249	if (waveartist_reset(devc))
1250		return -ENODEV;
1251
1252	sprintf(dev_name, "%s (%s", devc->hw.name, devc->chip_name);
1253
1254	if (waveartist_getrev(devc, rev)) {
1255		strcat(dev_name, " rev. ");
1256		strcat(dev_name, rev);
1257	}
1258	strcat(dev_name, ")");
1259
1260	conf_printf2(dev_name, devc->hw.io_base, devc->hw.irq,
1261		     devc->hw.dma, devc->hw.dma2);
1262
1263	portc = (wavnc_port_info *)kmalloc(sizeof(wavnc_port_info), GFP_KERNEL);
1264	if (portc == NULL)
1265		goto nomem;
1266
1267	memset(portc, 0, sizeof(wavnc_port_info));
1268
1269	my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, dev_name,
1270			&waveartist_audio_driver, sizeof(struct audio_driver),
1271			devc->audio_flags, AFMT_U8 | AFMT_S16_LE | AFMT_S8,
1272			devc, devc->hw.dma, devc->hw.dma2);
1273
1274	if (my_dev < 0)
1275		goto free;
1276
1277	audio_devs[my_dev]->portc = portc;
1278
1279	waveartist_mixer_reset(devc);
1280
1281	/*
1282	 * clear any pending interrupt
1283	 */
1284	waveartist_iack(devc);
1285
1286	if (request_irq(devc->hw.irq, waveartist_intr, 0, devc->hw.name, devc) < 0) {
1287		printk(KERN_ERR "%s: IRQ %d in use\n",
1288			devc->hw.name, devc->hw.irq);
1289		goto uninstall;
1290	}
1291
1292	if (sound_alloc_dma(devc->hw.dma, devc->hw.name)) {
1293		printk(KERN_ERR "%s: Can't allocate DMA%d\n",
1294			devc->hw.name, devc->hw.dma);
1295		goto uninstall_irq;
1296	}
1297
1298	if (devc->hw.dma != devc->hw.dma2 && devc->hw.dma2 != NO_DMA)
1299		if (sound_alloc_dma(devc->hw.dma2, devc->hw.name)) {
1300			printk(KERN_ERR "%s: can't allocate DMA%d\n",
1301				devc->hw.name, devc->hw.dma2);
1302			goto uninstall_dma;
1303		}
1304
1305	waveartist_set_ctlr(&devc->hw, 0, DMA1_IE | DMA0_IE);
1306
1307	audio_devs[my_dev]->mixer_dev =
1308		sound_install_mixer(MIXER_DRIVER_VERSION,
1309				dev_name,
1310				&waveartist_mixer_operations,
1311				sizeof(struct mixer_operations),
1312				devc);
1313
1314	return my_dev;
1315
1316uninstall_dma:
1317	sound_free_dma(devc->hw.dma);
1318
1319uninstall_irq:
1320	free_irq(devc->hw.irq, devc);
1321
1322uninstall:
1323	sound_unload_audiodev(my_dev);
1324
1325free:
1326	kfree(portc);
1327
1328nomem:
1329	return -1;
1330}
1331
1332static int __init probe_waveartist(struct address_info *hw_config)
1333{
1334	wavnc_info *devc = &adev_info[nr_waveartist_devs];
1335
1336	if (nr_waveartist_devs >= MAX_AUDIO_DEV) {
1337		printk(KERN_WARNING "waveartist: too many audio devices\n");
1338		return 0;
1339	}
1340
1341	if (check_region(hw_config->io_base, 15))  {
1342		printk(KERN_WARNING "WaveArtist: I/O port conflict\n");
1343		return 0;
1344	}
1345
1346	if (hw_config->irq > 15 || hw_config->irq < 0) {
1347		printk(KERN_WARNING "WaveArtist: Bad IRQ %d\n",
1348		       hw_config->irq);
1349		return 0;
1350	}
1351
1352	if (hw_config->dma != 3) {
1353		printk(KERN_WARNING "WaveArtist: Bad DMA %d\n",
1354		       hw_config->dma);
1355		return 0;
1356	}
1357
1358	hw_config->name = "WaveArtist";
1359	devc->hw = *hw_config;
1360	devc->open_mode = 0;
1361	devc->chip_name = "RWA-010";
1362
1363	return 1;
1364}
1365
1366static void __init
1367attach_waveartist(struct address_info *hw, const struct waveartist_mixer_info *mix)
1368{
1369	wavnc_info *devc = &adev_info[nr_waveartist_devs];
1370
1371	/*
1372	 * NOTE! If irq < 0, there is another driver which has allocated the
1373	 *   IRQ so that this driver doesn't need to allocate/deallocate it.
1374	 *   The actually used IRQ is ABS(irq).
1375	 */
1376	devc->hw = *hw;
1377	devc->hw.irq = (hw->irq > 0) ? hw->irq : 0;
1378	devc->open_mode = 0;
1379	devc->playback_dev = 0;
1380	devc->record_dev = 0;
1381	devc->audio_flags = DMA_AUTOMODE;
1382	devc->levels = levels;
1383
1384	if (hw->dma != hw->dma2 && hw->dma2 != NO_DMA)
1385		devc->audio_flags |= DMA_DUPLEX;
1386
1387	request_region(hw->io_base, 15, devc->hw.name);
1388
1389	devc->mix = mix;
1390	devc->dev_no = waveartist_init(devc);
1391
1392	if (devc->dev_no < 0)
1393		release_region(hw->io_base, 15);
1394	else {
1395#ifdef CONFIG_ARCH_NETWINDER
1396		if (machine_is_netwinder()) {
1397			init_timer(&vnc_timer);
1398			vnc_timer.function = vnc_slider_tick;
1399			vnc_timer.expires  = jiffies;
1400			vnc_timer.data     = nr_waveartist_devs;
1401			add_timer(&vnc_timer);
1402
1403			vnc_configure_mixer(devc, 0);
1404
1405			devc->no_autoselect = 1;
1406		}
1407#endif
1408		nr_waveartist_devs += 1;
1409	}
1410}
1411
1412static void __exit unload_waveartist(struct address_info *hw)
1413{
1414	wavnc_info *devc = NULL;
1415	int i;
1416
1417	for (i = 0; i < nr_waveartist_devs; i++)
1418		if (hw->io_base == adev_info[i].hw.io_base) {
1419			devc = adev_info + i;
1420			break;
1421		}
1422
1423	if (devc != NULL) {
1424		int mixer;
1425
1426#ifdef CONFIG_ARCH_NETWINDER
1427		if (machine_is_netwinder())
1428			del_timer(&vnc_timer);
1429#endif
1430
1431		release_region(devc->hw.io_base, 15);
1432
1433		waveartist_set_ctlr(&devc->hw, DMA1_IE|DMA0_IE, 0);
1434
1435		if (devc->hw.irq >= 0)
1436			free_irq(devc->hw.irq, devc);
1437
1438		sound_free_dma(devc->hw.dma);
1439
1440		if (devc->hw.dma != devc->hw.dma2 &&
1441		    devc->hw.dma2 != NO_DMA)
1442			sound_free_dma(devc->hw.dma2);
1443
1444		mixer = audio_devs[devc->dev_no]->mixer_dev;
1445
1446		if (mixer >= 0)
1447			sound_unload_mixerdev(mixer);
1448
1449		if (devc->dev_no >= 0)
1450			sound_unload_audiodev(devc->dev_no);
1451
1452		nr_waveartist_devs -= 1;
1453
1454		for (; i < nr_waveartist_devs; i++)
1455			adev_info[i] = adev_info[i + 1];
1456	} else
1457		printk(KERN_WARNING "waveartist: can't find device "
1458		       "to unload\n");
1459}
1460
1461#ifdef CONFIG_ARCH_NETWINDER
1462
1463/*
1464 * Rebel.com Netwinder specifics...
1465 */
1466
1467#include <asm/hardware/dec21285.h>
1468
1469#define	VNC_TIMER_PERIOD (HZ/4)	//check slider 4 times/sec
1470
1471#define	MIXER_PRIVATE3_RESET	0x53570000
1472#define	MIXER_PRIVATE3_READ	0x53570001
1473#define	MIXER_PRIVATE3_WRITE	0x53570002
1474
1475#define	VNC_MUTE_INTERNAL_SPKR	0x01	//the sw mute on/off control bit
1476#define	VNC_MUTE_LINE_OUT	0x10
1477#define VNC_PHONE_DETECT	0x20
1478#define VNC_HANDSET_DETECT	0x40
1479#define VNC_DISABLE_AUTOSWITCH	0x80
1480
1481extern spinlock_t gpio_lock;
1482
1483static inline void
1484vnc_mute_spkr(wavnc_info *devc)
1485{
1486	unsigned long flags;
1487
1488	spin_lock_irqsave(&gpio_lock, flags);
1489	cpld_modify(CPLD_UNMUTE, devc->spkr_mute_state ? 0 : CPLD_UNMUTE);
1490	spin_unlock_irqrestore(&gpio_lock, flags);
1491}
1492
1493static void
1494vnc_mute_lout(wavnc_info *devc)
1495{
1496	unsigned int left, right;
1497
1498	left  = waveartist_cmd1_r(devc, WACMD_GET_LEVEL);
1499	right = waveartist_cmd1_r(devc, WACMD_GET_LEVEL | 0x400);
1500
1501	if (devc->line_mute_state) {
1502		left &= ~1;
1503		right &= ~1;
1504	} else {
1505		left |= 1;
1506		right |= 1;
1507	}
1508	waveartist_cmd3(devc, WACMD_SET_MIXER, left, right);
1509
1510}
1511
1512static int
1513vnc_volume_slider(wavnc_info *devc)
1514{
1515	static signed int old_slider_volume;
1516	unsigned long flags;
1517	signed int volume = 255;
1518
1519	*CSR_TIMER1_LOAD = 0x00ffffff;
1520
1521	save_flags(flags);
1522	cli();
1523
1524	outb(0xFF, 0x201);
1525	*CSR_TIMER1_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_DIV1;
1526
1527	while (volume && (inb(0x201) & 0x01))
1528		volume--;
1529
1530	*CSR_TIMER1_CNTL = 0;
1531
1532	restore_flags(flags);
1533
1534	volume = 0x00ffffff - *CSR_TIMER1_VALUE;
1535
1536
1537#ifndef REVERSE
1538	volume = 150 - (volume >> 5);
1539#else
1540	volume = (volume >> 6) - 25;
1541#endif
1542
1543	if (volume < 0)
1544		volume = 0;
1545
1546	if (volume > 100)
1547		volume = 100;
1548
1549	/*
1550	 * slider quite often reads +-8, so debounce this random noise
1551	 */
1552	if (abs(volume - old_slider_volume) > 7) {
1553		old_slider_volume = volume;
1554
1555		if (debug_flg & DEBUG_MIXER)
1556			printk(KERN_DEBUG "Slider volume: %d.\n", volume);
1557	}
1558
1559	return old_slider_volume;
1560}
1561
1562/*
1563 * Decode a recording mask into a mixer selection on the NetWinder
1564 * as follows:
1565 *
1566 *     OSS Source	WA Source	Actual source
1567 *  SOUND_MASK_IMIX	Mixer		Mixer output (same as AD1848)
1568 *  SOUND_MASK_LINE	Line		Line in
1569 *  SOUND_MASK_LINE1	Left Mic	Handset
1570 *  SOUND_MASK_PHONEIN	Left Aux	Telephone microphone
1571 *  SOUND_MASK_MIC	Right Mic	Builtin microphone
1572 */
1573static unsigned int
1574netwinder_select_input(wavnc_info *devc, unsigned int recmask,
1575		       unsigned char *dev_l, unsigned char *dev_r)
1576{
1577	unsigned int recdev_l = ADC_MUX_NONE, recdev_r = ADC_MUX_NONE;
1578
1579	if (recmask & SOUND_MASK_IMIX) {
1580		recmask = SOUND_MASK_IMIX;
1581		recdev_l = ADC_MUX_MIXER;
1582		recdev_r = ADC_MUX_MIXER;
1583	} else if (recmask & SOUND_MASK_LINE) {
1584		recmask = SOUND_MASK_LINE;
1585		recdev_l = ADC_MUX_LINE;
1586		recdev_r = ADC_MUX_LINE;
1587	} else if (recmask & SOUND_MASK_LINE1) {
1588		recmask = SOUND_MASK_LINE1;
1589		waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1590		recdev_l = ADC_MUX_MIC;
1591		recdev_r = ADC_MUX_NONE;
1592	} else if (recmask & SOUND_MASK_PHONEIN) {
1593		recmask = SOUND_MASK_PHONEIN;
1594		waveartist_cmd1(devc, WACMD_SET_MONO); /* left */
1595		recdev_l = ADC_MUX_AUX1;
1596		recdev_r = ADC_MUX_NONE;
1597	} else if (recmask & SOUND_MASK_MIC) {
1598		recmask = SOUND_MASK_MIC;
1599		waveartist_cmd1(devc, WACMD_SET_MONO | 0x100);	/* right */
1600		recdev_l = ADC_MUX_NONE;
1601		recdev_r = ADC_MUX_MIC;
1602	}
1603
1604	*dev_l = recdev_l;
1605	*dev_r = recdev_r;
1606
1607	return recmask;
1608}
1609
1610static int
1611netwinder_decode_mixer(wavnc_info *devc, int dev, unsigned char lev_l,
1612		       unsigned char lev_r)
1613{
1614	switch (dev) {
1615	case SOUND_MIXER_VOLUME:
1616	case SOUND_MIXER_SYNTH:
1617	case SOUND_MIXER_PCM:
1618	case SOUND_MIXER_LINE:
1619	case SOUND_MIXER_IGAIN:
1620		devc->levels[dev] = lev_l | lev_r << 8;
1621		break;
1622
1623	case SOUND_MIXER_MIC:		/* right mic only */
1624		devc->levels[SOUND_MIXER_MIC] &= 0xff;
1625		devc->levels[SOUND_MIXER_MIC] |= lev_l << 8;
1626		break;
1627
1628	case SOUND_MIXER_LINE1:		/* left mic only  */
1629		devc->levels[SOUND_MIXER_MIC] &= 0xff00;
1630		devc->levels[SOUND_MIXER_MIC] |= lev_l;
1631		dev = SOUND_MIXER_MIC;
1632		break;
1633
1634	case SOUND_MIXER_PHONEIN:	/* left aux only  */
1635		devc->levels[SOUND_MIXER_LINE1] = lev_l;
1636		dev = SOUND_MIXER_LINE1;
1637		break;
1638
1639	case SOUND_MIXER_IMIX:
1640	case SOUND_MIXER_PHONEOUT:
1641		break;
1642
1643	default:
1644		dev = -EINVAL;
1645		break;
1646	}
1647	return dev;
1648}
1649
1650static int netwinder_get_mixer(wavnc_info *devc, int dev)
1651{
1652	int levels;
1653
1654	switch (dev) {
1655	case SOUND_MIXER_VOLUME:
1656	case SOUND_MIXER_SYNTH:
1657	case SOUND_MIXER_PCM:
1658	case SOUND_MIXER_LINE:
1659	case SOUND_MIXER_IGAIN:
1660		levels = devc->levels[dev];
1661		break;
1662
1663	case SOUND_MIXER_MIC:		/* builtin mic: right mic only */
1664		levels = devc->levels[SOUND_MIXER_MIC] >> 8;
1665		levels |= levels << 8;
1666		break;
1667
1668	case SOUND_MIXER_LINE1:		/* handset mic: left mic only */
1669		levels = devc->levels[SOUND_MIXER_MIC] & 0xff;
1670		levels |= levels << 8;
1671		break;
1672
1673	case SOUND_MIXER_PHONEIN:	/* phone mic: left aux1 only */
1674		levels = devc->levels[SOUND_MIXER_LINE1] & 0xff;
1675		levels |= levels << 8;
1676		break;
1677
1678	default:
1679		levels = 0;
1680	}
1681
1682	return levels;
1683}
1684
1685/*
1686 * Waveartist specific mixer information.
1687 */
1688static const struct waveartist_mixer_info netwinder_mixer = {
1689	supported_devs:	SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1690			SOUND_MASK_PCM     | SOUND_MASK_SPEAKER |
1691			SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1692			SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1693			SOUND_MASK_PHONEIN | SOUND_MASK_PHONEOUT|
1694			SOUND_MASK_IGAIN,
1695
1696	recording_devs:	SOUND_MASK_LINE    | SOUND_MASK_MIC     |
1697			SOUND_MASK_IMIX    | SOUND_MASK_LINE1   |
1698			SOUND_MASK_PHONEIN,
1699
1700	stereo_devs:	SOUND_MASK_VOLUME  | SOUND_MASK_SYNTH   |
1701			SOUND_MASK_PCM     | SOUND_MASK_LINE    |
1702			SOUND_MASK_IMIX    | SOUND_MASK_IGAIN,
1703
1704	select_input:	netwinder_select_input,
1705	decode_mixer:	netwinder_decode_mixer,
1706	get_mixer:	netwinder_get_mixer,
1707};
1708
1709static void
1710vnc_configure_mixer(wavnc_info *devc, unsigned int recmask)
1711{
1712	if (!devc->no_autoselect) {
1713		if (devc->handset_detect) {
1714			recmask = SOUND_MASK_LINE1;
1715			devc->spkr_mute_state = devc->line_mute_state = 1;
1716		} else if (devc->telephone_detect) {
1717			recmask = SOUND_MASK_PHONEIN;
1718			devc->spkr_mute_state = devc->line_mute_state = 1;
1719		} else {
1720			/* unless someone has asked for LINE-IN,
1721			 * we default to MIC
1722			 */
1723			if ((devc->recmask & SOUND_MASK_LINE) == 0)
1724				devc->recmask = SOUND_MASK_MIC;
1725			devc->spkr_mute_state = devc->line_mute_state = 0;
1726		}
1727		vnc_mute_spkr(devc);
1728		vnc_mute_lout(devc);
1729
1730		if (recmask != devc->recmask)
1731			waveartist_set_recmask(devc, recmask);
1732	}
1733}
1734
1735static int
1736vnc_slider(wavnc_info *devc)
1737{
1738	signed int slider_volume;
1739	unsigned int temp, old_hs, old_td;
1740
1741	/*
1742	 * read the "buttons" state.
1743	 *  Bit 4 = 0 means handset present
1744	 *  Bit 5 = 1 means phone offhook
1745	 */
1746	temp = inb(0x201);
1747
1748	old_hs = devc->handset_detect;
1749	old_td = devc->telephone_detect;
1750
1751	devc->handset_detect = !(temp & 0x10);
1752	devc->telephone_detect = !!(temp & 0x20);
1753
1754	if (!devc->no_autoselect &&
1755	    (old_hs != devc->handset_detect ||
1756	     old_td != devc->telephone_detect))
1757		vnc_configure_mixer(devc, devc->recmask);
1758
1759	slider_volume = vnc_volume_slider(devc);
1760
1761	/*
1762	 * If we're using software controlled volume, and
1763	 * the slider moves by more than 20%, then we
1764	 * switch back to slider controlled volume.
1765	 */
1766	if (abs(devc->slider_vol - slider_volume) > 20)
1767		devc->use_slider = 1;
1768
1769	/*
1770	 * use only left channel
1771	 */
1772	temp = levels[SOUND_MIXER_VOLUME] & 0xFF;
1773
1774	if (slider_volume != temp && devc->use_slider) {
1775		devc->slider_vol = slider_volume;
1776
1777		waveartist_set_mixer(devc, SOUND_MIXER_VOLUME,
1778			slider_volume | slider_volume << 8);
1779
1780		return 1;
1781	}
1782
1783	return 0;
1784}
1785
1786static void
1787vnc_slider_tick(unsigned long data)
1788{
1789	int next_timeout;
1790
1791	if (vnc_slider(adev_info + data))
1792		next_timeout = 5;	// mixer reported change
1793	else
1794		next_timeout = VNC_TIMER_PERIOD;
1795
1796	mod_timer(&vnc_timer, jiffies + next_timeout);
1797}
1798
1799static int
1800vnc_private_ioctl(int dev, unsigned int cmd, caddr_t arg)
1801{
1802	wavnc_info *devc = (wavnc_info *)audio_devs[dev]->devc;
1803	int val;
1804
1805	switch (cmd) {
1806	case SOUND_MIXER_PRIVATE1:
1807	{
1808		u_int prev_spkr_mute, prev_line_mute, prev_auto_state;
1809		int val;
1810
1811		if (get_user(val, (int *)arg))
1812			return -EFAULT;
1813
1814		/* check if parameter is logical */
1815		if (val & ~(VNC_MUTE_INTERNAL_SPKR |
1816			    VNC_MUTE_LINE_OUT |
1817			    VNC_DISABLE_AUTOSWITCH))
1818			return -EINVAL;
1819
1820		prev_auto_state = devc->no_autoselect;
1821		prev_spkr_mute  = devc->spkr_mute_state;
1822		prev_line_mute  = devc->line_mute_state;
1823
1824		devc->no_autoselect   = (val & VNC_DISABLE_AUTOSWITCH) ? 1 : 0;
1825		devc->spkr_mute_state = (val & VNC_MUTE_INTERNAL_SPKR) ? 1 : 0;
1826		devc->line_mute_state = (val & VNC_MUTE_LINE_OUT) ? 1 : 0;
1827
1828		if (prev_spkr_mute != devc->spkr_mute_state)
1829			vnc_mute_spkr(devc);
1830
1831		if (prev_line_mute != devc->line_mute_state)
1832			vnc_mute_lout(devc);
1833
1834		if (prev_auto_state != devc->no_autoselect)
1835			vnc_configure_mixer(devc, devc->recmask);
1836
1837		return 0;
1838	}
1839
1840	case SOUND_MIXER_PRIVATE2:
1841		if (get_user(val, (int *)arg))
1842			return -EFAULT;
1843
1844		switch (val) {
1845#define VNC_SOUND_PAUSE         0x53    //to pause the DSP
1846#define VNC_SOUND_RESUME        0x57    //to unpause the DSP
1847		case VNC_SOUND_PAUSE:
1848			waveartist_cmd1(devc, 0x16);
1849			break;
1850
1851		case VNC_SOUND_RESUME:
1852			waveartist_cmd1(devc, 0x18);
1853			break;
1854
1855		default:
1856			return -EINVAL;
1857		}
1858		return 0;
1859
1860	/* private ioctl to allow bulk access to waveartist */
1861	case SOUND_MIXER_PRIVATE3:
1862	{
1863		unsigned long	flags;
1864		int		mixer_reg[15], i, val;
1865
1866		if (get_user(val, (int *)arg))
1867			return -EFAULT;
1868		if (copy_from_user(mixer_reg, (void *)val, sizeof(mixer_reg)))
1869			return -EFAULT;
1870
1871		switch (mixer_reg[14]) {
1872		case MIXER_PRIVATE3_RESET:
1873			waveartist_mixer_reset(devc);
1874			break;
1875
1876		case MIXER_PRIVATE3_WRITE:
1877			waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[0], mixer_reg[4]);
1878			waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[1], mixer_reg[5]);
1879			waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[2], mixer_reg[6]);
1880			waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[3], mixer_reg[7]);
1881			waveartist_cmd3(devc, WACMD_SET_MIXER, mixer_reg[8], mixer_reg[9]);
1882
1883			waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[10], mixer_reg[11]);
1884			waveartist_cmd3(devc, WACMD_SET_LEVEL, mixer_reg[12], mixer_reg[13]);
1885			break;
1886
1887		case MIXER_PRIVATE3_READ:
1888			spin_lock_irqsave(&waveartist_lock, flags);
1889
1890			for (i = 0x30; i < 14 << 8; i += 1 << 8)
1891				waveartist_cmd(devc, 1, &i, 1, mixer_reg + (i >> 8));
1892
1893			spin_unlock_irqrestore(&waveartist_lock, flags);
1894
1895			if (copy_to_user((void *)val, mixer_reg, sizeof(mixer_reg)))
1896				return -EFAULT;
1897			break;
1898
1899		default:
1900			return -EINVAL;
1901		}
1902		return 0;
1903	}
1904
1905	/* read back the state from PRIVATE1 */
1906	case SOUND_MIXER_PRIVATE4:
1907		val = (devc->spkr_mute_state  ? VNC_MUTE_INTERNAL_SPKR : 0) |
1908		      (devc->line_mute_state  ? VNC_MUTE_LINE_OUT      : 0) |
1909		      (devc->handset_detect   ? VNC_HANDSET_DETECT     : 0) |
1910		      (devc->telephone_detect ? VNC_PHONE_DETECT       : 0) |
1911		      (devc->no_autoselect    ? VNC_DISABLE_AUTOSWITCH : 0);
1912
1913		return put_user(val, (int *)arg) ? -EFAULT : 0;
1914	}
1915
1916	if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
1917		/*
1918		 * special case for master volume: if we
1919		 * received this call - switch from hw
1920		 * volume control to a software volume
1921		 * control, till the hw volume is modified
1922		 * to signal that user wants to be back in
1923		 * hardware...
1924		 */
1925		if ((cmd & 0xff) == SOUND_MIXER_VOLUME)
1926			devc->use_slider = 0;
1927
1928		/* speaker output            */
1929		if ((cmd & 0xff) == SOUND_MIXER_SPEAKER) {
1930			unsigned int val, l, r;
1931
1932			if (get_user(val, (int *)arg))
1933				return -EFAULT;
1934
1935			l = val & 0x7f;
1936			r = (val & 0x7f00) >> 8;
1937			val = (l + r) / 2;
1938			devc->levels[SOUND_MIXER_SPEAKER] = val | (val << 8);
1939			devc->spkr_mute_state = (val <= 50);
1940			vnc_mute_spkr(devc);
1941			return 0;
1942		}
1943	}
1944
1945	return -ENOIOCTLCMD;
1946}
1947
1948#endif
1949
1950static struct address_info cfg;
1951
1952static int attached;
1953
1954static int __initdata io = 0;
1955static int __initdata irq = 0;
1956static int __initdata dma = 0;
1957static int __initdata dma2 = 0;
1958
1959
1960static int __init init_waveartist(void)
1961{
1962	const struct waveartist_mixer_info *mix;
1963
1964	if (!io && machine_is_netwinder()) {
1965		/*
1966		 * The NetWinder WaveArtist is at a fixed address.
1967		 * If the user does not supply an address, use the
1968		 * well-known parameters.
1969		 */
1970		io   = 0x250;
1971		irq  = 12;
1972		dma  = 3;
1973		dma2 = 7;
1974	}
1975
1976	mix = &waveartist_mixer;
1977#ifdef CONFIG_ARCH_NETWINDER
1978	if (machine_is_netwinder())
1979		mix = &netwinder_mixer;
1980#endif
1981
1982	cfg.io_base = io;
1983	cfg.irq = irq;
1984	cfg.dma = dma;
1985	cfg.dma2 = dma2;
1986
1987	if (!probe_waveartist(&cfg))
1988		return -ENODEV;
1989
1990	attach_waveartist(&cfg, mix);
1991	attached = 1;
1992
1993	return 0;
1994}
1995
1996static void __exit cleanup_waveartist(void)
1997{
1998	if (attached)
1999		unload_waveartist(&cfg);
2000}
2001
2002module_init(init_waveartist);
2003module_exit(cleanup_waveartist);
2004
2005#ifndef MODULE
2006static int __init setup_waveartist(char *str)
2007{
2008	/* io, irq, dma, dma2 */
2009	int ints[5];
2010
2011	str = get_options(str, ARRAY_SIZE(ints), ints);
2012
2013	io	= ints[1];
2014	irq	= ints[2];
2015	dma	= ints[3];
2016	dma2	= ints[4];
2017
2018	return 1;
2019}
2020__setup("waveartist=", setup_waveartist);
2021#endif
2022
2023MODULE_DESCRIPTION("Rockwell WaveArtist RWA-010 sound driver");
2024MODULE_PARM(io, "i");		/* IO base */
2025MODULE_PARM(irq, "i");		/* IRQ */
2026MODULE_PARM(dma, "i");		/* DMA */
2027MODULE_PARM(dma2, "i");		/* DMA2 */
2028MODULE_LICENSE("GPL");
2029