1/*
2 *  linux/sound/oss/dmasound/dmasound_awacs.c
3 *
4 *  PowerMac `AWACS' and `Burgundy' DMA Sound Driver
5 *  with some limited support for DACA & Tumbler
6 *
7 *  See linux/sound/oss/dmasound/dmasound_core.c for copyright and
8 *  history prior to 2001/01/26.
9 *
10 *	26/01/2001 ed 0.1 Iain Sandoe
11 *		- added version info.
12 *		- moved dbdma command buffer allocation to PMacXXXSqSetup()
13 *		- fixed up beep dbdma cmd buffers
14 *
15 *	08/02/2001 [0.2]
16 *		- make SNDCTL_DSP_GETFMTS return the correct info for the h/w
17 *		- move soft format translations to a separate file
18 *		- [0.3] make SNDCTL_DSP_GETCAPS return correct info.
19 *		- [0.4] more informative machine name strings.
20 *		- [0.5]
21 *		- record changes.
22 *		- made the default_hard/soft entries.
23 *	04/04/2001 [0.6]
24 *		- minor correction to bit assignments in awacs_defs.h
25 *		- incorporate mixer changes from 2.2.x back-port.
26 *		- take out passthru as a rec input (it isn't).
27 *              - make Input Gain slider work the 'right way up'.
28 *              - try to make the mixer sliders more logical - so now the
29 *                input selectors are just two-state (>50% == ON) and the
30 *                Input Gain slider handles the rest of the gain issues.
31 *              - try to pick slider representations that most closely match
32 *                the actual use - e.g. IGain for input gain...
33 *              - first stab at over/under-run detection.
34 *		- minor cosmetic changes to IRQ identification.
35 *		- fix bug where rates > max would be reported as supported.
36 *              - first stab at over/under-run detection.
37 *              - make use of i2c for mixer settings conditional on perch
38 *                rather than cuda (some machines without perch have cuda).
39 *              - fix bug where TX stops when dbdma status comes up "DEAD"
40 *		  so far only reported on PowerComputing clones ... but.
41 *		- put in AWACS/Screamer register write timeouts.
42 *		- part way to partitioning the init() stuff
43 *		- first pass at 'tumbler' stuff (not support - just an attempt
44 *		  to allow the driver to load on new G4s).
45 *      01/02/2002 [0.7] - BenH
46 *	        - all sort of minor bits went in since the latest update, I
47 *	          bumped the version number for that reason
48 *
49 *      07/26/2002 [0.8] - BenH
50 *	        - More minor bits since last changelog (I should be more careful
51 *	          with those)
52 *	        - Support for snapper & better tumbler integration by Toby Sargeant
53 *	        - Headphone detect for scremer by Julien Blache
54 *	        - More tumbler fixed by Andreas Schwab
55 *	11/29/2003 [0.8.1] - Renzo Davoli (King Enzo)
56 *		- Support for Snapper line in
57 *		- snapper input resampling (for rates < 44100)
58 *		- software line gain control
59 */
60
61
62#include <linux/types.h>
63#include <linux/module.h>
64#include <linux/slab.h>
65#include <linux/init.h>
66#include <linux/delay.h>
67#include <linux/soundcard.h>
68#include <linux/adb.h>
69#include <linux/nvram.h>
70#include <linux/tty.h>
71#include <linux/vt_kern.h>
72#include <linux/spinlock.h>
73#include <linux/kmod.h>
74#include <linux/interrupt.h>
75#include <linux/input.h>
76#include <linux/mutex.h>
77#ifdef CONFIG_ADB_CUDA
78#include <linux/cuda.h>
79#endif
80#ifdef CONFIG_ADB_PMU
81#include <linux/pmu.h>
82#endif
83
84#include <asm/uaccess.h>
85#include <asm/prom.h>
86#include <asm/machdep.h>
87#include <asm/io.h>
88#include <asm/dbdma.h>
89#include <asm/pmac_feature.h>
90#include <asm/irq.h>
91#include <asm/nvram.h>
92
93#include "awacs_defs.h"
94#include "dmasound.h"
95#include "tas3001c.h"
96#include "tas3004.h"
97#include "tas_common.h"
98
99#define DMASOUND_AWACS_REVISION	0
100#define DMASOUND_AWACS_EDITION	7
101
102#define AWACS_SNAPPER   110	/* fake revision # for snapper */
103#define AWACS_BURGUNDY	100	/* fake revision # for burgundy */
104#define AWACS_TUMBLER    90	/* fake revision # for tumbler */
105#define AWACS_DACA	 80	/* fake revision # for daca (ibook) */
106#define AWACS_AWACS       2     /* holding revision for AWACS */
107#define AWACS_SCREAMER    3     /* holding revision for Screamer */
108/*
109 * Interrupt numbers and addresses, & info obtained from the device tree.
110 */
111static int awacs_irq, awacs_tx_irq, awacs_rx_irq;
112static volatile struct awacs_regs __iomem *awacs;
113static volatile u32 __iomem *i2s;
114static volatile struct dbdma_regs __iomem *awacs_txdma, *awacs_rxdma;
115static int awacs_rate_index;
116static int awacs_subframe;
117static struct device_node* awacs_node;
118static struct device_node* i2s_node;
119static struct resource awacs_rsrc[3];
120
121static char awacs_name[64];
122static int awacs_revision;
123static int awacs_sleeping;
124static DEFINE_MUTEX(dmasound_mutex);
125
126static int sound_device_id;		/* exists after iMac revA */
127static int hw_can_byteswap = 1 ;	/* most pmac sound h/w can */
128
129/* model info */
130/* To be replaced with better interaction with pmac_feature.c */
131static int is_pbook_3X00;
132static int is_pbook_g3;
133
134/* expansion info */
135static int has_perch;
136static int has_ziva;
137
138/* for earlier powerbooks which need fiddling with mac-io to enable
139 * cd etc.
140*/
141static unsigned char __iomem *latch_base;
142static unsigned char __iomem *macio_base;
143
144/*
145 * Space for the DBDMA command blocks.
146 */
147static void *awacs_tx_cmd_space;
148static volatile struct dbdma_cmd *awacs_tx_cmds;
149static int number_of_tx_cmd_buffers;
150
151static void *awacs_rx_cmd_space;
152static volatile struct dbdma_cmd *awacs_rx_cmds;
153static int number_of_rx_cmd_buffers;
154
155
156int awacs_reg[8];
157int awacs_reg1_save;
158
159/* tracking values for the mixer contents
160*/
161
162static int spk_vol;
163static int line_vol;
164static int passthru_vol;
165
166static int ip_gain;           /* mic preamp settings */
167static int rec_lev = 0x4545 ; /* default CD gain 69 % */
168static int mic_lev;
169static int cd_lev = 0x6363 ; /* 99 % */
170static int line_lev;
171
172static int hdp_connected;
173
174/*
175 * Stuff for outputting a beep.  The values range from -327 to +327
176 * so we can multiply by an amplitude in the range 0..100 to get a
177 * signed short value to put in the output buffer.
178 */
179static short beep_wform[256] = {
180	0,	40,	79,	117,	153,	187,	218,	245,
181	269,	288,	304,	316,	323,	327,	327,	324,
182	318,	310,	299,	288,	275,	262,	249,	236,
183	224,	213,	204,	196,	190,	186,	183,	182,
184	182,	183,	186,	189,	192,	196,	200,	203,
185	206,	208,	209,	209,	209,	207,	204,	201,
186	197,	193,	188,	183,	179,	174,	170,	166,
187	163,	161,	160,	159,	159,	160,	161,	162,
188	164,	166,	168,	169,	171,	171,	171,	170,
189	169,	167,	163,	159,	155,	150,	144,	139,
190	133,	128,	122,	117,	113,	110,	107,	105,
191	103,	103,	103,	103,	104,	104,	105,	105,
192	105,	103,	101,	97,	92,	86,	78,	68,
193	58,	45,	32,	18,	3,	-11,	-26,	-41,
194	-55,	-68,	-79,	-88,	-95,	-100,	-102,	-102,
195	-99,	-93,	-85,	-75,	-62,	-48,	-33,	-16,
196	0,	16,	33,	48,	62,	75,	85,	93,
197	99,	102,	102,	100,	95,	88,	79,	68,
198	55,	41,	26,	11,	-3,	-18,	-32,	-45,
199	-58,	-68,	-78,	-86,	-92,	-97,	-101,	-103,
200	-105,	-105,	-105,	-104,	-104,	-103,	-103,	-103,
201	-103,	-105,	-107,	-110,	-113,	-117,	-122,	-128,
202	-133,	-139,	-144,	-150,	-155,	-159,	-163,	-167,
203	-169,	-170,	-171,	-171,	-171,	-169,	-168,	-166,
204	-164,	-162,	-161,	-160,	-159,	-159,	-160,	-161,
205	-163,	-166,	-170,	-174,	-179,	-183,	-188,	-193,
206	-197,	-201,	-204,	-207,	-209,	-209,	-209,	-208,
207	-206,	-203,	-200,	-196,	-192,	-189,	-186,	-183,
208	-182,	-182,	-183,	-186,	-190,	-196,	-204,	-213,
209	-224,	-236,	-249,	-262,	-275,	-288,	-299,	-310,
210	-318,	-324,	-327,	-327,	-323,	-316,	-304,	-288,
211	-269,	-245,	-218,	-187,	-153,	-117,	-79,	-40,
212};
213
214/* beep support */
215#define BEEP_SRATE	22050	/* 22050 Hz sample rate */
216#define BEEP_BUFLEN	512
217#define BEEP_VOLUME	15	/* 0 - 100 */
218
219static int beep_vol = BEEP_VOLUME;
220static int beep_playing;
221static int awacs_beep_state;
222static short *beep_buf;
223static void *beep_dbdma_cmd_space;
224static volatile struct dbdma_cmd *beep_dbdma_cmd;
225
226/* Burgundy functions */
227static void awacs_burgundy_wcw(unsigned addr,unsigned newval);
228static unsigned awacs_burgundy_rcw(unsigned addr);
229static void awacs_burgundy_write_volume(unsigned address, int volume);
230static int awacs_burgundy_read_volume(unsigned address);
231static void awacs_burgundy_write_mvolume(unsigned address, int volume);
232static int awacs_burgundy_read_mvolume(unsigned address);
233
234/* we will allocate a single 'emergency' dbdma cmd block to use if the
235   tx status comes up "DEAD".  This happens on some PowerComputing Pmac
236   clones, either owing to a bug in dbdma or some interaction between
237   IDE and sound.  However, this measure would deal with DEAD status if
238   if appeared elsewhere.
239
240   for the sake of memory efficiency we'll allocate this cmd as part of
241   the beep cmd stuff.
242*/
243
244static volatile struct dbdma_cmd *emergency_dbdma_cmd;
245
246#ifdef CONFIG_PM
247/*
248 * Stuff for restoring after a sleep.
249 */
250static void awacs_sleep_notify(struct pmu_sleep_notifier *self, int when);
251struct pmu_sleep_notifier awacs_sleep_notifier = {
252	awacs_sleep_notify, SLEEP_LEVEL_SOUND,
253};
254#endif /* CONFIG_PM */
255
256/* for (soft) sample rate translations */
257int expand_bal;		/* Balance factor for expanding (not volume!) */
258int expand_read_bal;	/* Balance factor for expanding reads (not volume!) */
259
260/*** Low level stuff *********************************************************/
261
262static void *PMacAlloc(unsigned int size, gfp_t flags);
263static void PMacFree(void *ptr, unsigned int size);
264static int PMacIrqInit(void);
265#ifdef MODULE
266static void PMacIrqCleanup(void);
267#endif
268static void PMacSilence(void);
269static void PMacInit(void);
270static int PMacSetFormat(int format);
271static int PMacSetVolume(int volume);
272static void PMacPlay(void);
273static void PMacRecord(void);
274static irqreturn_t pmac_awacs_tx_intr(int irq, void *devid);
275static irqreturn_t pmac_awacs_rx_intr(int irq, void *devid);
276static irqreturn_t pmac_awacs_intr(int irq, void *devid);
277static void awacs_write(int val);
278static int awacs_get_volume(int reg, int lshift);
279static int awacs_volume_setter(int volume, int n, int mute, int lshift);
280
281
282/*** Mid level stuff **********************************************************/
283
284static int PMacMixerIoctl(u_int cmd, u_long arg);
285static int PMacWriteSqSetup(void);
286static int PMacReadSqSetup(void);
287static void PMacAbortRead(void);
288
289extern TRANS transAwacsNormal ;
290extern TRANS transAwacsExpand ;
291extern TRANS transAwacsNormalRead ;
292extern TRANS transAwacsExpandRead ;
293
294extern int daca_init(void);
295extern void daca_cleanup(void);
296extern int daca_set_volume(uint left_vol, uint right_vol);
297extern void daca_get_volume(uint * left_vol, uint  *right_vol);
298extern int daca_enter_sleep(void);
299extern int daca_leave_sleep(void);
300
301#define TRY_LOCK()	\
302	if ((rc = mutex_lock_interruptible(&dmasound_mutex)) != 0)	\
303		return rc;
304#define LOCK()		mutex_lock(&dmasound_mutex);
305
306#define UNLOCK()	mutex_unlock(&dmasound_mutex);
307
308#undef IOCTL_IN
309#undef IOCTL_OUT
310
311#define IOCTL_IN(arg, ret)	\
312	rc = get_user(ret, (int __user *)(arg)); \
313	if (rc) break;
314#define IOCTL_OUT(arg, ret)	\
315	ioctl_return2((int __user *)(arg), ret)
316
317static inline int ioctl_return2(int __user *addr, int value)
318{
319	return value < 0 ? value : put_user(value, addr);
320}
321
322
323/*** AE - TUMBLER / SNAPPER START ************************************************/
324
325
326int gpio_audio_reset, gpio_audio_reset_pol;
327int gpio_amp_mute, gpio_amp_mute_pol;
328int gpio_headphone_mute, gpio_headphone_mute_pol;
329int gpio_headphone_detect, gpio_headphone_detect_pol;
330int gpio_headphone_irq;
331
332int
333setup_audio_gpio(const char *name, const char* compatible, int *gpio_addr, int* gpio_pol)
334{
335	struct device_node *gpiop;
336	struct device_node *np;
337	const u32* pp;
338	int ret = -ENODEV;
339
340	gpiop = of_find_node_by_name(NULL, "gpio");
341	if (!gpiop)
342		goto done;
343
344	np = of_get_next_child(gpiop, NULL);
345	while(np != 0) {
346		if (name) {
347			const char *property =
348				of_get_property(np,"audio-gpio",NULL);
349			if (property != 0 && strcmp(property,name) == 0)
350				break;
351		} else if (compatible && of_device_is_compatible(np, compatible))
352			break;
353		np = of_get_next_child(gpiop, np);
354	}
355	if (!np)
356		goto done;
357	pp = of_get_property(np, "AAPL,address", NULL);
358	if (!pp)
359		goto done;
360	*gpio_addr = (*pp) & 0x0000ffff;
361	pp = of_get_property(np, "audio-gpio-active-state", NULL);
362	if (pp)
363		*gpio_pol = *pp;
364	else
365		*gpio_pol = 1;
366	ret = irq_of_parse_and_map(np, 0);
367done:
368	of_node_put(np);
369	of_node_put(gpiop);
370	return ret;
371}
372
373static inline void
374write_audio_gpio(int gpio_addr, int data)
375{
376	if (!gpio_addr)
377		return;
378	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio_addr, data ? 0x05 : 0x04);
379}
380
381static inline int
382read_audio_gpio(int gpio_addr)
383{
384	if (!gpio_addr)
385		return 0;
386	return ((pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio_addr, 0) & 0x02) !=0);
387}
388
389/*
390 * Headphone interrupt via GPIO (Tumbler, Snapper, DACA)
391 */
392static irqreturn_t
393headphone_intr(int irq, void *devid)
394{
395	unsigned long flags;
396
397	spin_lock_irqsave(&dmasound.lock, flags);
398	if (read_audio_gpio(gpio_headphone_detect) == gpio_headphone_detect_pol) {
399		printk(KERN_INFO "Audio jack plugged, muting speakers.\n");
400		write_audio_gpio(gpio_headphone_mute, !gpio_headphone_mute_pol);
401		write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
402		tas_output_device_change(sound_device_id,TAS_OUTPUT_HEADPHONES,0);
403	} else {
404		printk(KERN_INFO "Audio jack unplugged, enabling speakers.\n");
405		write_audio_gpio(gpio_amp_mute, !gpio_amp_mute_pol);
406		write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
407		tas_output_device_change(sound_device_id,TAS_OUTPUT_INTERNAL_SPKR,0);
408	}
409	spin_unlock_irqrestore(&dmasound.lock, flags);
410	return IRQ_HANDLED;
411}
412
413
414/* Initialize tumbler */
415
416static int
417tas_dmasound_init(void)
418{
419	setup_audio_gpio(
420		"audio-hw-reset",
421		NULL,
422		&gpio_audio_reset,
423		&gpio_audio_reset_pol);
424	setup_audio_gpio(
425		"amp-mute",
426		NULL,
427		&gpio_amp_mute,
428		&gpio_amp_mute_pol);
429	setup_audio_gpio("headphone-mute",
430		NULL,
431		&gpio_headphone_mute,
432		&gpio_headphone_mute_pol);
433	gpio_headphone_irq = setup_audio_gpio(
434		"headphone-detect",
435		NULL,
436		&gpio_headphone_detect,
437		&gpio_headphone_detect_pol);
438	/* Fix some broken OF entries in desktop machines */
439	if (!gpio_headphone_irq)
440		gpio_headphone_irq = setup_audio_gpio(
441			NULL,
442			"keywest-gpio15",
443			&gpio_headphone_detect,
444			&gpio_headphone_detect_pol);
445
446	write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
447	msleep(100);
448	write_audio_gpio(gpio_audio_reset, !gpio_audio_reset_pol);
449	msleep(100);
450  	if (gpio_headphone_irq) {
451		if (request_irq(gpio_headphone_irq,headphone_intr,0,"Headphone detect",NULL) < 0) {
452    			printk(KERN_ERR "tumbler: Can't request headphone interrupt\n");
453    			gpio_headphone_irq = 0;
454    		} else {
455			u8 val;
456			/* Activate headphone status interrupts */
457			val = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio_headphone_detect, 0);
458			pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio_headphone_detect, val | 0x80);
459			/* Trigger it */
460  			headphone_intr(0, NULL);
461  		}
462  	}
463  	if (!gpio_headphone_irq) {
464  		/* Some machine enter this case ? */
465  		printk(KERN_WARNING "tumbler: Headphone detect IRQ not found, enabling all outputs !\n");
466  		write_audio_gpio(gpio_amp_mute, !gpio_amp_mute_pol);
467  		write_audio_gpio(gpio_headphone_mute, !gpio_headphone_mute_pol);
468  	}
469	return 0;
470}
471
472
473static int
474tas_dmasound_cleanup(void)
475{
476	if (gpio_headphone_irq)
477		free_irq(gpio_headphone_irq, NULL);
478	return 0;
479}
480
481/* We don't support 48k yet */
482static int tas_freqs[1] = { 44100 } ;
483static int tas_freqs_ok[1] = { 1 } ;
484
485/* don't know what to do really - just have to leave it where
486 * OF left things
487*/
488
489static int
490tas_set_frame_rate(void)
491{
492	if (i2s) {
493		out_le32(i2s + (I2S_REG_SERIAL_FORMAT >> 2), 0x41190000);
494		out_le32(i2s + (I2S_REG_DATAWORD_SIZES >> 2), 0x02000200);
495	}
496	dmasound.hard.speed = 44100 ;
497	awacs_rate_index = 0 ;
498	return 44100 ;
499}
500
501static int
502tas_mixer_ioctl(u_int cmd, u_long arg)
503{
504	int __user *argp = (int __user *)arg;
505	int data;
506	int rc;
507
508        rc=tas_device_ioctl(cmd, arg);
509        if (rc != -EINVAL) {
510        	return rc;
511        }
512
513        if ((cmd & ~0xff) == MIXER_WRITE(0) &&
514            tas_supported_mixers() & (1<<(cmd & 0xff))) {
515		rc = get_user(data, argp);
516                if (rc<0) return rc;
517		tas_set_mixer_level(cmd & 0xff, data);
518		tas_get_mixer_level(cmd & 0xff, &data);
519		return ioctl_return2(argp, data);
520        }
521        if ((cmd & ~0xff) == MIXER_READ(0) &&
522            tas_supported_mixers() & (1<<(cmd & 0xff))) {
523		tas_get_mixer_level(cmd & 0xff, &data);
524		return ioctl_return2(argp, data);
525        }
526
527	switch(cmd) {
528	case SOUND_MIXER_READ_DEVMASK:
529		data = tas_supported_mixers() | SOUND_MASK_SPEAKER;
530		rc = IOCTL_OUT(arg, data);
531		break;
532	case SOUND_MIXER_READ_STEREODEVS:
533		data = tas_stereo_mixers();
534		rc = IOCTL_OUT(arg, data);
535		break;
536	case SOUND_MIXER_READ_CAPS:
537		rc = IOCTL_OUT(arg, 0);
538		break;
539	case SOUND_MIXER_READ_RECMASK:
540		data = SOUND_MASK_LINE | SOUND_MASK_MIC;
541		rc = IOCTL_OUT(arg, data);
542		break;
543	case SOUND_MIXER_READ_RECSRC:
544		if (awacs_reg[0] & MASK_MUX_AUDIN)
545			data |= SOUND_MASK_LINE;
546		if (awacs_reg[0] & MASK_MUX_MIC)
547			data |= SOUND_MASK_MIC;
548		rc = IOCTL_OUT(arg, data);
549		break;
550	case SOUND_MIXER_WRITE_RECSRC:
551 		IOCTL_IN(arg, data);
552		data =0;
553 		rc = IOCTL_OUT(arg, data);
554 		break;
555	case SOUND_MIXER_WRITE_SPEAKER:	/* really bell volume */
556 		IOCTL_IN(arg, data);
557 		beep_vol = data & 0xff;
558 		/* fall through */
559	case SOUND_MIXER_READ_SPEAKER:
560		rc = IOCTL_OUT(arg, (beep_vol<<8) | beep_vol);
561 		break;
562	case SOUND_MIXER_OUTMASK:
563	case SOUND_MIXER_OUTSRC:
564	default:
565		rc = -EINVAL;
566	}
567
568	return rc;
569}
570
571static void __init
572tas_init_frame_rates(const unsigned int *prop, unsigned int l)
573{
574	int i ;
575	if (prop) {
576		for (i=0; i<1; i++)
577			tas_freqs_ok[i] = 0;
578		for (l /= sizeof(int); l > 0; --l) {
579			unsigned int r = *prop++;
580			/* Apple 'Fixed' format */
581			if (r >= 0x10000)
582				r >>= 16;
583			for (i = 0; i < 1; ++i) {
584				if (r == tas_freqs[i]) {
585					tas_freqs_ok[i] = 1;
586					break;
587				}
588			}
589		}
590	}
591	/* else we assume that all the rates are available */
592}
593
594
595/*** AE - TUMBLER / SNAPPER END ************************************************/
596
597
598
599/*** Low level stuff *********************************************************/
600
601/*
602 * PCI PowerMac, with AWACS, Screamer, Burgundy, DACA or Tumbler and DBDMA.
603 */
604static void *PMacAlloc(unsigned int size, gfp_t flags)
605{
606	return kmalloc(size, flags);
607}
608
609static void PMacFree(void *ptr, unsigned int size)
610{
611	kfree(ptr);
612}
613
614static int __init PMacIrqInit(void)
615{
616	if (awacs)
617		if (request_irq(awacs_irq, pmac_awacs_intr, 0, "Built-in Sound misc", NULL))
618			return 0;
619	if (request_irq(awacs_tx_irq, pmac_awacs_tx_intr, 0, "Built-in Sound out", NULL)
620	    || request_irq(awacs_rx_irq, pmac_awacs_rx_intr, 0, "Built-in Sound in", NULL))
621		return 0;
622	return 1;
623}
624
625#ifdef MODULE
626static void PMacIrqCleanup(void)
627{
628	/* turn off input & output dma */
629	DBDMA_DO_STOP(awacs_txdma);
630	DBDMA_DO_STOP(awacs_rxdma);
631
632	if (awacs)
633		/* disable interrupts from awacs interface */
634		out_le32(&awacs->control, in_le32(&awacs->control) & 0xfff);
635
636	/* Switch off the sound clock */
637	pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 0);
638	/* Make sure proper bits are set on pismo & tipb */
639	if ((machine_is_compatible("PowerBook3,1") ||
640	    machine_is_compatible("PowerBook3,2")) && awacs) {
641		awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
642		awacs_write(MASK_ADDR1 | awacs_reg[1]);
643		msleep(200);
644	}
645	if (awacs)
646		free_irq(awacs_irq, NULL);
647	free_irq(awacs_tx_irq, NULL);
648	free_irq(awacs_rx_irq, NULL);
649
650	if (awacs)
651		iounmap(awacs);
652	if (i2s)
653		iounmap(i2s);
654	iounmap(awacs_txdma);
655	iounmap(awacs_rxdma);
656
657	release_mem_region(awacs_rsrc[0].start,
658			   awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
659	release_mem_region(awacs_rsrc[1].start,
660			   awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
661	release_mem_region(awacs_rsrc[2].start,
662			   awacs_rsrc[2].end - awacs_rsrc[2].start + 1);
663
664	kfree(awacs_tx_cmd_space);
665	kfree(awacs_rx_cmd_space);
666	kfree(beep_dbdma_cmd_space);
667	kfree(beep_buf);
668#ifdef CONFIG_PM
669	pmu_unregister_sleep_notifier(&awacs_sleep_notifier);
670#endif
671}
672#endif /* MODULE */
673
674static void PMacSilence(void)
675{
676	/* turn off output dma */
677	DBDMA_DO_STOP(awacs_txdma);
678}
679
680/* don't know what to do really - just have to leave it where
681 * OF left things
682*/
683
684static int daca_set_frame_rate(void)
685{
686	if (i2s) {
687		out_le32(i2s + (I2S_REG_SERIAL_FORMAT >> 2), 0x41190000);
688		out_le32(i2s + (I2S_REG_DATAWORD_SIZES >> 2), 0x02000200);
689	}
690	dmasound.hard.speed = 44100 ;
691	awacs_rate_index = 0 ;
692	return 44100 ;
693}
694
695static int awacs_freqs[8] = {
696	44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
697};
698static int awacs_freqs_ok[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
699
700static int
701awacs_set_frame_rate(int desired, int catch_r)
702{
703	int tolerance, i = 8 ;
704	/*
705	 * If we have a sample rate which is within catchRadius percent
706	 * of the requested value, we don't have to expand the samples.
707	 * Otherwise choose the next higher rate.
708	 * N.B.: burgundy awacs only works at 44100 Hz.
709	 */
710	do {
711		tolerance = catch_r * awacs_freqs[--i] / 100;
712		if (awacs_freqs_ok[i]
713		    && dmasound.soft.speed <= awacs_freqs[i] + tolerance)
714			break;
715	} while (i > 0);
716	dmasound.hard.speed = awacs_freqs[i];
717	awacs_rate_index = i;
718
719	out_le32(&awacs->control, MASK_IEPC | (i << 8) | 0x11 );
720	awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) | (i << 3);
721	awacs_write(awacs_reg[1] | MASK_ADDR1);
722	return dmasound.hard.speed;
723}
724
725static int
726burgundy_set_frame_rate(void)
727{
728	awacs_rate_index = 0 ;
729	awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) ;
730	out_le32(&awacs->control, MASK_IEPC | 0 | 0x11 | MASK_IEE);
731	return 44100 ;
732}
733
734static int
735set_frame_rate(int desired, int catch_r)
736{
737	switch (awacs_revision) {
738		case AWACS_BURGUNDY:
739			dmasound.hard.speed = burgundy_set_frame_rate();
740			break ;
741		case AWACS_TUMBLER:
742		case AWACS_SNAPPER:
743			dmasound.hard.speed = tas_set_frame_rate();
744			break ;
745		case AWACS_DACA:
746			dmasound.hard.speed =
747			  daca_set_frame_rate();
748			break ;
749		default:
750			dmasound.hard.speed = awacs_set_frame_rate(desired,
751						catch_r);
752			break ;
753	}
754	return dmasound.hard.speed ;
755}
756
757static void
758awacs_recalibrate(void)
759{
760	/* Sorry for the horrible delays... I hope to get that improved
761	 * by making the whole PM process asynchronous in a future version
762	 */
763	msleep(750);
764	awacs_reg[1] |= MASK_CMUTE | MASK_AMUTE;
765	awacs_write(awacs_reg[1] | MASK_RECALIBRATE | MASK_ADDR1);
766	msleep(1000);
767	awacs_write(awacs_reg[1] | MASK_ADDR1);
768}
769
770static void PMacInit(void)
771{
772	int tolerance;
773
774	switch (dmasound.soft.format) {
775	    case AFMT_S16_LE:
776	    case AFMT_U16_LE:
777		if (hw_can_byteswap)
778			dmasound.hard.format = AFMT_S16_LE;
779		else
780			dmasound.hard.format = AFMT_S16_BE;
781		break;
782	default:
783		dmasound.hard.format = AFMT_S16_BE;
784		break;
785	}
786	dmasound.hard.stereo = 1;
787	dmasound.hard.size = 16;
788
789	/* set dmasound.hard.speed - on the basis of what we want (soft)
790	 * and the tolerance we'll allow.
791	*/
792	set_frame_rate(dmasound.soft.speed, catchRadius) ;
793
794	tolerance = (catchRadius * dmasound.hard.speed) / 100;
795	if (dmasound.soft.speed >= dmasound.hard.speed - tolerance) {
796		dmasound.trans_write = &transAwacsNormal;
797		dmasound.trans_read = &transAwacsNormalRead;
798	} else {
799		dmasound.trans_write = &transAwacsExpand;
800		dmasound.trans_read = &transAwacsExpandRead;
801	}
802
803	if (awacs) {
804		if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE))
805			out_le32(&awacs->byteswap, BS_VAL);
806		else
807			out_le32(&awacs->byteswap, 0);
808	}
809
810	expand_bal = -dmasound.soft.speed;
811	expand_read_bal = -dmasound.soft.speed;
812}
813
814static int PMacSetFormat(int format)
815{
816	int size;
817	int req_format = format;
818
819	switch (format) {
820	case AFMT_QUERY:
821		return dmasound.soft.format;
822	case AFMT_MU_LAW:
823	case AFMT_A_LAW:
824	case AFMT_U8:
825	case AFMT_S8:
826		size = 8;
827		break;
828	case AFMT_S16_LE:
829		if(!hw_can_byteswap)
830			format = AFMT_S16_BE;
831	case AFMT_S16_BE:
832		size = 16;
833		break;
834	case AFMT_U16_LE:
835		if(!hw_can_byteswap)
836			format = AFMT_U16_BE;
837	case AFMT_U16_BE:
838		size = 16;
839		break;
840	default: /* :-) */
841		printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
842		       format);
843		size = 8;
844		format = AFMT_U8;
845	}
846
847	if (req_format == format) {
848		dmasound.soft.format = format;
849		dmasound.soft.size = size;
850		if (dmasound.minDev == SND_DEV_DSP) {
851			dmasound.dsp.format = format;
852			dmasound.dsp.size = size;
853		}
854	}
855
856	return format;
857}
858
859#define AWACS_VOLUME_TO_MASK(x)	(15 - ((((x) - 1) * 15) / 99))
860#define AWACS_MASK_TO_VOLUME(y)	(100 - ((y) * 99 / 15))
861
862static int awacs_get_volume(int reg, int lshift)
863{
864	int volume;
865
866	volume = AWACS_MASK_TO_VOLUME((reg >> lshift) & 0xf);
867	volume |= AWACS_MASK_TO_VOLUME(reg & 0xf) << 8;
868	return volume;
869}
870
871static int awacs_volume_setter(int volume, int n, int mute, int lshift)
872{
873	int r1, rn;
874
875	if (mute && volume == 0) {
876		r1 = awacs_reg[1] | mute;
877	} else {
878		r1 = awacs_reg[1] & ~mute;
879		rn = awacs_reg[n] & ~(0xf | (0xf << lshift));
880		rn |= ((AWACS_VOLUME_TO_MASK(volume & 0xff) & 0xf) << lshift);
881		rn |= AWACS_VOLUME_TO_MASK((volume >> 8) & 0xff) & 0xf;
882		awacs_reg[n] = rn;
883		awacs_write((n << 12) | rn);
884		volume = awacs_get_volume(rn, lshift);
885	}
886	if (r1 != awacs_reg[1]) {
887		awacs_reg[1] = r1;
888		awacs_write(r1 | MASK_ADDR1);
889	}
890	return volume;
891}
892
893static int PMacSetVolume(int volume)
894{
895	printk(KERN_WARNING "Bogus call to PMacSetVolume !\n");
896	return 0;
897}
898
899static void awacs_setup_for_beep(int speed)
900{
901	out_le32(&awacs->control,
902		 (in_le32(&awacs->control) & ~0x1f00)
903		 | ((speed > 0 ? speed : awacs_rate_index) << 8));
904
905	if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE) && speed == -1)
906		out_le32(&awacs->byteswap, BS_VAL);
907	else
908		out_le32(&awacs->byteswap, 0);
909}
910
911/* CHECK: how much of this *really* needs IRQs masked? */
912static void __PMacPlay(void)
913{
914	volatile struct dbdma_cmd *cp;
915	int next_frg, count;
916
917	count = 300 ; /* > two cycles at the lowest sample rate */
918
919	/* what we want to send next */
920	next_frg = (write_sq.front + write_sq.active) % write_sq.max_count;
921
922	if (awacs_beep_state) {
923		/* sound takes precedence over beeps */
924		/* stop the dma channel */
925		out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
926		while ( (in_le32(&awacs_txdma->status) & RUN) && count--)
927			udelay(1);
928		if (awacs)
929			awacs_setup_for_beep(-1);
930		out_le32(&awacs_txdma->cmdptr,
931			 virt_to_bus(&(awacs_tx_cmds[next_frg])));
932
933		beep_playing = 0;
934		awacs_beep_state = 0;
935	}
936	/* this won't allow more than two frags to be in the output queue at
937	   once. (or one, if the max frags is 2 - because count can't exceed
938	   2 in that case)
939	*/
940	while (write_sq.active < 2 && write_sq.active < write_sq.count) {
941		count = (write_sq.count == write_sq.active + 1) ?
942				write_sq.rear_size:write_sq.block_size ;
943		if (count < write_sq.block_size) {
944			if (!write_sq.syncing) /* last block not yet filled,*/
945				break; 	/* and we're not syncing or POST-ed */
946			else {
947				/* pretend the block is full to force a new
948				   block to be started on the next write */
949				write_sq.rear_size = write_sq.block_size ;
950				write_sq.syncing &= ~2 ; /* clear POST */
951			}
952		}
953		cp = &awacs_tx_cmds[next_frg];
954		st_le16(&cp->req_count, count);
955		st_le16(&cp->xfer_status, 0);
956		st_le16(&cp->command, OUTPUT_MORE + INTR_ALWAYS);
957		/* put a STOP at the end of the queue - but only if we have
958		   space for it.  This means that, if we under-run and we only
959		   have two fragments, we might re-play sound from an existing
960		   queued frag.  I guess the solution to that is not to set two
961		   frags if you are likely to under-run...
962		*/
963		if (write_sq.count < write_sq.max_count) {
964			if (++next_frg >= write_sq.max_count)
965				next_frg = 0 ; /* wrap */
966			/* if we get here then we've underrun so we will stop*/
967			st_le16(&awacs_tx_cmds[next_frg].command, DBDMA_STOP);
968		}
969		/* set the dbdma controller going, if it is not already */
970		if (write_sq.active == 0)
971			out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
972		(void)in_le32(&awacs_txdma->status);
973		out_le32(&awacs_txdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
974		++write_sq.active;
975	}
976}
977
978static void PMacPlay(void)
979{
980	LOCK();
981	if (!awacs_sleeping) {
982		unsigned long flags;
983
984		spin_lock_irqsave(&dmasound.lock, flags);
985		__PMacPlay();
986		spin_unlock_irqrestore(&dmasound.lock, flags);
987	}
988	UNLOCK();
989}
990
991static void PMacRecord(void)
992{
993	unsigned long flags;
994
995	if (read_sq.active)
996		return;
997
998	spin_lock_irqsave(&dmasound.lock, flags);
999
1000	/* This is all we have to do......Just start it up.
1001	*/
1002	out_le32(&awacs_rxdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
1003	read_sq.active = 1;
1004
1005	spin_unlock_irqrestore(&dmasound.lock, flags);
1006}
1007
1008/* if the TX status comes up "DEAD" - reported on some Power Computing machines
1009   we need to re-start the dbdma - but from a different physical start address
1010   and with a different transfer length.  It would get very messy to do this
1011   with the normal dbdma_cmd blocks - we would have to re-write the buffer start
1012   addresses each time.  So, we will keep a single dbdma_cmd block which can be
1013   fiddled with.
1014   When DEAD status is first reported the content of the faulted dbdma block is
1015   copied into the emergency buffer and we note that the buffer is in use.
1016   we then bump the start physical address by the amount that was successfully
1017   output before it died.
1018   On any subsequent DEAD result we just do the bump-ups (we know that we are
1019   already using the emergency dbdma_cmd).
1020   CHECK: this just tries to "do it".  It is possible that we should abandon
1021   xfers when the number of residual bytes gets below a certain value - I can
1022   see that this might cause a loop-forever if too small a transfer causes
1023   DEAD status.  However this is a TODO for now - we'll see what gets reported.
1024   When we get a successful transfer result with the emergency buffer we just
1025   pretend that it completed using the original dmdma_cmd and carry on.  The
1026   'next_cmd' field will already point back to the original loop of blocks.
1027*/
1028
1029static irqreturn_t
1030pmac_awacs_tx_intr(int irq, void *devid)
1031{
1032	int i = write_sq.front;
1033	int stat;
1034	int i_nowrap = write_sq.front;
1035	volatile struct dbdma_cmd *cp;
1036	/* != 0 when we are dealing with a DEAD xfer */
1037	static int emergency_in_use;
1038
1039	spin_lock(&dmasound.lock);
1040	while (write_sq.active > 0) { /* we expect to have done something*/
1041		if (emergency_in_use) /* we are dealing with DEAD xfer */
1042			cp = emergency_dbdma_cmd ;
1043		else
1044			cp = &awacs_tx_cmds[i];
1045		stat = ld_le16(&cp->xfer_status);
1046		if (stat & DEAD) {
1047			unsigned short req, res ;
1048			unsigned int phy ;
1049#ifdef DEBUG_DMASOUND
1050printk("dmasound_pmac: tx-irq: xfer died - patching it up...\n") ;
1051#endif
1052			/* to clear DEAD status we must first clear RUN
1053			   set it to quiescent to be on the safe side */
1054			(void)in_le32(&awacs_txdma->status);
1055			out_le32(&awacs_txdma->control,
1056				(RUN|PAUSE|FLUSH|WAKE) << 16);
1057			write_sq.died++ ;
1058			if (!emergency_in_use) { /* new problem */
1059				memcpy((void *)emergency_dbdma_cmd, (void *)cp,
1060					sizeof(struct dbdma_cmd));
1061				emergency_in_use = 1;
1062				cp = emergency_dbdma_cmd;
1063			}
1064			/* now bump the values to reflect the amount
1065			   we haven't yet shifted */
1066			req = ld_le16(&cp->req_count);
1067			res = ld_le16(&cp->res_count);
1068			phy = ld_le32(&cp->phy_addr);
1069			phy += (req - res);
1070			st_le16(&cp->req_count, res);
1071			st_le16(&cp->res_count, 0);
1072			st_le16(&cp->xfer_status, 0);
1073			st_le32(&cp->phy_addr, phy);
1074			st_le32(&cp->cmd_dep, virt_to_bus(&awacs_tx_cmds[(i+1)%write_sq.max_count]));
1075			st_le16(&cp->command, OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
1076
1077			/* point at our patched up command block */
1078			out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
1079			/* we must re-start the controller */
1080			(void)in_le32(&awacs_txdma->status);
1081			/* should complete clearing the DEAD status */
1082			out_le32(&awacs_txdma->control,
1083				((RUN|WAKE) << 16) + (RUN|WAKE));
1084			break; /* this block is still going */
1085		}
1086		if ((stat & ACTIVE) == 0)
1087			break;	/* this frame is still going */
1088		if (emergency_in_use)
1089			emergency_in_use = 0 ; /* done that */
1090		--write_sq.count;
1091		--write_sq.active;
1092		i_nowrap++;
1093		if (++i >= write_sq.max_count)
1094			i = 0;
1095	}
1096
1097	/* if we stopped and we were not sync-ing - then we under-ran */
1098	if( write_sq.syncing == 0 ){
1099		stat = in_le32(&awacs_txdma->status) ;
1100		/* we hit the dbdma_stop */
1101		if( (stat & ACTIVE) == 0 ) write_sq.xruns++ ;
1102	}
1103
1104	/* if we used some data up then wake the writer to supply some more*/
1105	if (i_nowrap != write_sq.front)
1106		WAKE_UP(write_sq.action_queue);
1107	write_sq.front = i;
1108
1109	/* but make sure we funnel what we've already got */\
1110	 if (!awacs_sleeping)
1111		__PMacPlay();
1112
1113	/* make the wake-on-empty conditional on syncing */
1114	if (!write_sq.active && (write_sq.syncing & 1))
1115		WAKE_UP(write_sq.sync_queue); /* any time we're empty */
1116	spin_unlock(&dmasound.lock);
1117	return IRQ_HANDLED;
1118}
1119
1120
1121static irqreturn_t
1122pmac_awacs_rx_intr(int irq, void *devid)
1123{
1124	int stat ;
1125	/* For some reason on my PowerBook G3, I get one interrupt
1126	 * when the interrupt vector is installed (like something is
1127	 * pending).  This happens before the dbdma is initialized by
1128	 * us, so I just check the command pointer and if it is zero,
1129	 * just blow it off.
1130	 */
1131	if (in_le32(&awacs_rxdma->cmdptr) == 0)
1132		return IRQ_HANDLED;
1133
1134	/* We also want to blow 'em off when shutting down.
1135	*/
1136	if (read_sq.active == 0)
1137		return IRQ_HANDLED;
1138
1139	spin_lock(&dmasound.lock);
1140	/* Check multiple buffers in case we were held off from
1141	 * interrupt processing for a long time.  Geeze, I really hope
1142	 * this doesn't happen.
1143	 */
1144	while ((stat=awacs_rx_cmds[read_sq.rear].xfer_status)) {
1145
1146		/* if we got a "DEAD" status then just log it for now.
1147		   and try to restart dma.
1148		   TODO: figure out how best to fix it up
1149		*/
1150		if (stat & DEAD){
1151#ifdef DEBUG_DMASOUND
1152printk("dmasound_pmac: rx-irq: DIED - attempting resurection\n");
1153#endif
1154			/* to clear DEAD status we must first clear RUN
1155			   set it to quiescent to be on the safe side */
1156			(void)in_le32(&awacs_txdma->status);
1157			out_le32(&awacs_txdma->control,
1158				(RUN|PAUSE|FLUSH|WAKE) << 16);
1159			awacs_rx_cmds[read_sq.rear].xfer_status = 0;
1160			awacs_rx_cmds[read_sq.rear].res_count = 0;
1161			read_sq.died++ ;
1162			(void)in_le32(&awacs_txdma->status);
1163			/* re-start the same block */
1164			out_le32(&awacs_rxdma->cmdptr,
1165				virt_to_bus(&awacs_rx_cmds[read_sq.rear]));
1166			/* we must re-start the controller */
1167			(void)in_le32(&awacs_rxdma->status);
1168			/* should complete clearing the DEAD status */
1169			out_le32(&awacs_rxdma->control,
1170				((RUN|WAKE) << 16) + (RUN|WAKE));
1171			spin_unlock(&dmasound.lock);
1172			return IRQ_HANDLED; /* try this block again */
1173		}
1174		/* Clear status and move on to next buffer.
1175		*/
1176		awacs_rx_cmds[read_sq.rear].xfer_status = 0;
1177		read_sq.rear++;
1178
1179		/* Wrap the buffer ring.
1180		*/
1181		if (read_sq.rear >= read_sq.max_active)
1182			read_sq.rear = 0;
1183
1184		/* If we have caught up to the front buffer, bump it.
1185		 * This will cause weird (but not fatal) results if the
1186		 * read loop is currently using this buffer.  The user is
1187		 * behind in this case anyway, so weird things are going
1188		 * to happen.
1189		 */
1190		if (read_sq.rear == read_sq.front) {
1191			read_sq.front++;
1192			read_sq.xruns++ ; /* we overan */
1193			if (read_sq.front >= read_sq.max_active)
1194				read_sq.front = 0;
1195		}
1196	}
1197
1198	WAKE_UP(read_sq.action_queue);
1199	spin_unlock(&dmasound.lock);
1200	return IRQ_HANDLED;
1201}
1202
1203
1204static irqreturn_t
1205pmac_awacs_intr(int irq, void *devid)
1206{
1207	int ctrl;
1208	int status;
1209	int r1;
1210
1211	spin_lock(&dmasound.lock);
1212	ctrl = in_le32(&awacs->control);
1213	status = in_le32(&awacs->codec_stat);
1214
1215	if (ctrl & MASK_PORTCHG) {
1216		/* tested on Screamer, should work on others too */
1217		if (awacs_revision == AWACS_SCREAMER) {
1218			if (((status & MASK_HDPCONN) >> 3) && (hdp_connected == 0)) {
1219				hdp_connected = 1;
1220
1221				r1 = awacs_reg[1] | MASK_SPKMUTE;
1222				awacs_reg[1] = r1;
1223				awacs_write(r1 | MASK_ADDR_MUTE);
1224			} else if (((status & MASK_HDPCONN) >> 3 == 0) && (hdp_connected == 1)) {
1225				hdp_connected = 0;
1226
1227				r1 = awacs_reg[1] & ~MASK_SPKMUTE;
1228				awacs_reg[1] = r1;
1229				awacs_write(r1 | MASK_ADDR_MUTE);
1230			}
1231		}
1232	}
1233	if (ctrl & MASK_CNTLERR) {
1234		int err = (in_le32(&awacs->codec_stat) & MASK_ERRCODE) >> 16;
1235		/* CHECK: we just swallow burgundy errors at the moment..*/
1236		if (err != 0 && awacs_revision != AWACS_BURGUNDY)
1237			printk(KERN_ERR "dmasound_pmac: error %x\n", err);
1238	}
1239	/* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
1240	out_le32(&awacs->control, ctrl);
1241	spin_unlock(&dmasound.lock);
1242	return IRQ_HANDLED;
1243}
1244
1245static void
1246awacs_write(int val)
1247{
1248	int count = 300 ;
1249	if (awacs_revision >= AWACS_DACA || !awacs)
1250		return ;
1251
1252	while ((in_le32(&awacs->codec_ctrl) & MASK_NEWECMD) && count--)
1253		udelay(1) ;	/* timeout is > 2 samples at lowest rate */
1254	out_le32(&awacs->codec_ctrl, val | (awacs_subframe << 22));
1255	(void)in_le32(&awacs->byteswap);
1256}
1257
1258/* this is called when the beep timer expires... it will be called even
1259   if the beep has been overidden by other sound output.
1260*/
1261static void awacs_nosound(unsigned long xx)
1262{
1263	unsigned long flags;
1264	int count = 600 ; /* > four samples at lowest rate */
1265
1266	spin_lock_irqsave(&dmasound.lock, flags);
1267	if (beep_playing) {
1268		st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
1269		out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
1270		while ((in_le32(&awacs_txdma->status) & RUN) && count--)
1271			udelay(1);
1272		if (awacs)
1273			awacs_setup_for_beep(-1);
1274		beep_playing = 0;
1275	}
1276	spin_unlock_irqrestore(&dmasound.lock, flags);
1277}
1278
1279/*
1280 * We generate the beep with a single dbdma command that loops a buffer
1281 * forever - without generating interrupts.
1282 *
1283 * So, to stop it you have to stop dma output as per awacs_nosound.
1284 */
1285static int awacs_beep_event(struct input_dev *dev, unsigned int type,
1286		unsigned int code, int hz)
1287{
1288	unsigned long flags;
1289	int beep_speed = 0;
1290	int srate;
1291	int period, ncycles, nsamples;
1292	int i, j, f;
1293	short *p;
1294	static int beep_hz_cache;
1295	static int beep_nsamples_cache;
1296	static int beep_volume_cache;
1297
1298	if (type != EV_SND)
1299		return -1;
1300	switch (code) {
1301	case SND_BELL:
1302		if (hz)
1303			hz = 1000;
1304		break;
1305	case SND_TONE:
1306		break;
1307	default:
1308		return -1;
1309	}
1310
1311	if (beep_buf == NULL)
1312		return -1;
1313
1314	/* quick-hack fix for DACA, Burgundy & Tumbler */
1315
1316	if (awacs_revision >= AWACS_DACA){
1317		srate = 44100 ;
1318	} else {
1319		for (i = 0; i < 8 && awacs_freqs[i] >= BEEP_SRATE; ++i)
1320			if (awacs_freqs_ok[i])
1321				beep_speed = i;
1322		srate = awacs_freqs[beep_speed];
1323	}
1324
1325	if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
1326		/* cancel beep currently playing */
1327		awacs_nosound(0);
1328		return 0;
1329	}
1330
1331	spin_lock_irqsave(&dmasound.lock, flags);
1332	if (beep_playing || write_sq.active || beep_buf == NULL) {
1333		spin_unlock_irqrestore(&dmasound.lock, flags);
1334		return -1;		/* too hard, sorry :-( */
1335	}
1336	beep_playing = 1;
1337	st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
1338	spin_unlock_irqrestore(&dmasound.lock, flags);
1339
1340	if (hz == beep_hz_cache && beep_vol == beep_volume_cache) {
1341		nsamples = beep_nsamples_cache;
1342	} else {
1343		period = srate * 256 / hz;	/* fixed point */
1344		ncycles = BEEP_BUFLEN * 256 / period;
1345		nsamples = (period * ncycles) >> 8;
1346		f = ncycles * 65536 / nsamples;
1347		j = 0;
1348		p = beep_buf;
1349		for (i = 0; i < nsamples; ++i, p += 2) {
1350			p[0] = p[1] = beep_wform[j >> 8] * beep_vol;
1351			j = (j + f) & 0xffff;
1352		}
1353		beep_hz_cache = hz;
1354		beep_volume_cache = beep_vol;
1355		beep_nsamples_cache = nsamples;
1356	}
1357
1358	st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
1359	st_le16(&beep_dbdma_cmd->xfer_status, 0);
1360	st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
1361	st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
1362	awacs_beep_state = 1;
1363
1364	spin_lock_irqsave(&dmasound.lock, flags);
1365	if (beep_playing) {	/* i.e. haven't been terminated already */
1366		int count = 300 ;
1367		out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
1368		while ((in_le32(&awacs_txdma->status) & RUN) && count--)
1369			udelay(1); /* timeout > 2 samples at lowest rate*/
1370		if (awacs)
1371			awacs_setup_for_beep(beep_speed);
1372		out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
1373		(void)in_le32(&awacs_txdma->status);
1374		out_le32(&awacs_txdma->control, RUN | (RUN << 16));
1375	}
1376	spin_unlock_irqrestore(&dmasound.lock, flags);
1377
1378	return 0;
1379}
1380
1381/* used in init and for wake-up */
1382
1383static void
1384load_awacs(void)
1385{
1386	awacs_write(awacs_reg[0] + MASK_ADDR0);
1387	awacs_write(awacs_reg[1] + MASK_ADDR1);
1388	awacs_write(awacs_reg[2] + MASK_ADDR2);
1389	awacs_write(awacs_reg[4] + MASK_ADDR4);
1390
1391	if (awacs_revision == AWACS_SCREAMER) {
1392		awacs_write(awacs_reg[5] + MASK_ADDR5);
1393		msleep(100);
1394		awacs_write(awacs_reg[6] + MASK_ADDR6);
1395		msleep(2);
1396		awacs_write(awacs_reg[1] + MASK_ADDR1);
1397		awacs_write(awacs_reg[7] + MASK_ADDR7);
1398	}
1399	if (awacs) {
1400		if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE))
1401			out_le32(&awacs->byteswap, BS_VAL);
1402		else
1403			out_le32(&awacs->byteswap, 0);
1404	}
1405}
1406
1407#ifdef CONFIG_PM
1408/*
1409 * Save state when going to sleep, restore it afterwards.
1410 */
1411static void awacs_sleep_notify(struct pmu_sleep_notifier *self, int when)
1412{
1413	unsigned long flags;
1414
1415	switch (when) {
1416	case PBOOK_SLEEP_NOW:
1417		LOCK();
1418		awacs_sleeping = 1;
1419		/* Tell the rest of the driver we are now going to sleep */
1420		mb();
1421		if (awacs_revision == AWACS_SCREAMER ||
1422		    awacs_revision == AWACS_AWACS) {
1423			awacs_reg1_save = awacs_reg[1];
1424			awacs_reg[1] |= MASK_AMUTE | MASK_CMUTE;
1425			awacs_write(MASK_ADDR1 | awacs_reg[1]);
1426		}
1427
1428		PMacSilence();
1429		/* stop rx - if going - a bit of a daft user... but */
1430		out_le32(&awacs_rxdma->control, (RUN|WAKE|FLUSH << 16));
1431		/* deny interrupts */
1432		if (awacs)
1433			disable_irq(awacs_irq);
1434		disable_irq(awacs_tx_irq);
1435		disable_irq(awacs_rx_irq);
1436		/* Chip specific sleep code */
1437		switch (awacs_revision) {
1438			case AWACS_TUMBLER:
1439			case AWACS_SNAPPER:
1440				write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
1441				write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
1442				tas_enter_sleep();
1443				write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
1444				break ;
1445			case AWACS_DACA:
1446				daca_enter_sleep();
1447				break ;
1448			case AWACS_BURGUNDY:
1449				break ;
1450			case AWACS_SCREAMER:
1451			case AWACS_AWACS:
1452			default:
1453				out_le32(&awacs->control, 0x11) ;
1454				break ;
1455		}
1456		/* Disable sound clock */
1457		pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 0);
1458		/* According to Darwin, we do that after turning off the sound
1459		 * chip clock. All this will have to be cleaned up once we properly
1460		 * parse the OF sound-objects
1461		 */
1462		if ((machine_is_compatible("PowerBook3,1") ||
1463		    machine_is_compatible("PowerBook3,2")) && awacs) {
1464			awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
1465			awacs_write(MASK_ADDR1 | awacs_reg[1]);
1466			msleep(200);
1467		}
1468		break;
1469	case PBOOK_WAKE:
1470		/* Enable sound clock */
1471		pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 1);
1472		if ((machine_is_compatible("PowerBook3,1") ||
1473		    machine_is_compatible("PowerBook3,2")) && awacs) {
1474			msleep(100);
1475			awacs_reg[1] &= ~(MASK_PAROUT0 | MASK_PAROUT1);
1476			awacs_write(MASK_ADDR1 | awacs_reg[1]);
1477			msleep(300);
1478		} else
1479			msleep(1000);
1480 		/* restore settings */
1481		switch (awacs_revision) {
1482			case AWACS_TUMBLER:
1483			case AWACS_SNAPPER:
1484				write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
1485				write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
1486				write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
1487				msleep(100);
1488				write_audio_gpio(gpio_audio_reset, !gpio_audio_reset_pol);
1489				msleep(150);
1490				tas_leave_sleep(); /* Stub for now */
1491				headphone_intr(0, NULL);
1492				break;
1493			case AWACS_DACA:
1494				msleep(10); /* Check this !!! */
1495				daca_leave_sleep();
1496				break ;		/* dont know how yet */
1497			case AWACS_BURGUNDY:
1498				break ;
1499			case AWACS_SCREAMER:
1500			case AWACS_AWACS:
1501			default:
1502		 		load_awacs() ;
1503				break ;
1504		}
1505		/* Recalibrate chip */
1506		if (awacs_revision == AWACS_SCREAMER && awacs)
1507			awacs_recalibrate();
1508		/* Make sure dma is stopped */
1509		PMacSilence();
1510		if (awacs)
1511			enable_irq(awacs_irq);
1512		enable_irq(awacs_tx_irq);
1513 		enable_irq(awacs_rx_irq);
1514 		if (awacs) {
1515 			/* OK, allow ints back again */
1516	 		out_le32(&awacs->control, MASK_IEPC
1517 			 	| (awacs_rate_index << 8) | 0x11
1518 				 | (awacs_revision < AWACS_DACA ? MASK_IEE: 0));
1519 		}
1520 		if (macio_base && is_pbook_g3) {
1521			out_8(macio_base + 0x37, 3);
1522 		} else if (is_pbook_3X00) {
1523			in_8(latch_base + 0x190);
1524		}
1525		/* Remove mute */
1526		if (awacs_revision == AWACS_SCREAMER ||
1527		    awacs_revision == AWACS_AWACS) {
1528			awacs_reg[1] = awacs_reg1_save;
1529			awacs_write(MASK_ADDR1 | awacs_reg[1]);
1530		}
1531 		awacs_sleeping = 0;
1532		/* Resume pending sounds. */
1533		/* we don't try to restart input... */
1534		spin_lock_irqsave(&dmasound.lock, flags);
1535		__PMacPlay();
1536		spin_unlock_irqrestore(&dmasound.lock, flags);
1537		UNLOCK();
1538	}
1539}
1540#endif /* CONFIG_PM */
1541
1542
1543/* All the burgundy functions: */
1544
1545/* Waits for busy flag to clear */
1546static inline void
1547awacs_burgundy_busy_wait(void)
1548{
1549	int count = 50; /* > 2 samples at 44k1 */
1550	while ((in_le32(&awacs->codec_ctrl) & MASK_NEWECMD) && count--)
1551		udelay(1) ;
1552}
1553
1554static inline void
1555awacs_burgundy_extend_wait(void)
1556{
1557	int count = 50 ; /* > 2 samples at 44k1 */
1558	while ((!(in_le32(&awacs->codec_stat) & MASK_EXTEND)) && count--)
1559		udelay(1) ;
1560	count = 50;
1561	while ((in_le32(&awacs->codec_stat) & MASK_EXTEND) && count--)
1562		udelay(1);
1563}
1564
1565static void
1566awacs_burgundy_wcw(unsigned addr, unsigned val)
1567{
1568	out_le32(&awacs->codec_ctrl, addr + 0x200c00 + (val & 0xff));
1569	awacs_burgundy_busy_wait();
1570	out_le32(&awacs->codec_ctrl, addr + 0x200d00 +((val>>8) & 0xff));
1571	awacs_burgundy_busy_wait();
1572	out_le32(&awacs->codec_ctrl, addr + 0x200e00 +((val>>16) & 0xff));
1573	awacs_burgundy_busy_wait();
1574	out_le32(&awacs->codec_ctrl, addr + 0x200f00 +((val>>24) & 0xff));
1575	awacs_burgundy_busy_wait();
1576}
1577
1578static unsigned
1579awacs_burgundy_rcw(unsigned addr)
1580{
1581	unsigned val = 0;
1582	unsigned long flags;
1583
1584	/* should have timeouts here */
1585	spin_lock_irqsave(&dmasound.lock, flags);
1586
1587	out_le32(&awacs->codec_ctrl, addr + 0x100000);
1588	awacs_burgundy_busy_wait();
1589	awacs_burgundy_extend_wait();
1590	val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
1591
1592	out_le32(&awacs->codec_ctrl, addr + 0x100100);
1593	awacs_burgundy_busy_wait();
1594	awacs_burgundy_extend_wait();
1595	val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<8;
1596
1597	out_le32(&awacs->codec_ctrl, addr + 0x100200);
1598	awacs_burgundy_busy_wait();
1599	awacs_burgundy_extend_wait();
1600	val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<16;
1601
1602	out_le32(&awacs->codec_ctrl, addr + 0x100300);
1603	awacs_burgundy_busy_wait();
1604	awacs_burgundy_extend_wait();
1605	val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<24;
1606
1607	spin_unlock_irqrestore(&dmasound.lock, flags);
1608
1609	return val;
1610}
1611
1612
1613static void
1614awacs_burgundy_wcb(unsigned addr, unsigned val)
1615{
1616	out_le32(&awacs->codec_ctrl, addr + 0x300000 + (val & 0xff));
1617	awacs_burgundy_busy_wait();
1618}
1619
1620static unsigned
1621awacs_burgundy_rcb(unsigned addr)
1622{
1623	unsigned val = 0;
1624	unsigned long flags;
1625
1626	/* should have timeouts here */
1627	spin_lock_irqsave(&dmasound.lock, flags);
1628
1629	out_le32(&awacs->codec_ctrl, addr + 0x100000);
1630	awacs_burgundy_busy_wait();
1631	awacs_burgundy_extend_wait();
1632	val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
1633
1634	spin_unlock_irqrestore(&dmasound.lock, flags);
1635
1636	return val;
1637}
1638
1639static int
1640awacs_burgundy_check(void)
1641{
1642	/* Checks to see the chip is alive and kicking */
1643	int error = in_le32(&awacs->codec_ctrl) & MASK_ERRCODE;
1644
1645	return error == 0xf0000;
1646}
1647
1648static int
1649awacs_burgundy_init(void)
1650{
1651	if (awacs_burgundy_check()) {
1652		printk(KERN_WARNING "dmasound_pmac: burgundy not working :-(\n");
1653		return 1;
1654	}
1655
1656	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_OUTPUTENABLES,
1657			   DEF_BURGUNDY_OUTPUTENABLES);
1658	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
1659			   DEF_BURGUNDY_MORE_OUTPUTENABLES);
1660	awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
1661			   DEF_BURGUNDY_OUTPUTSELECTS);
1662
1663	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL21,
1664			   DEF_BURGUNDY_INPSEL21);
1665	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL3,
1666			   DEF_BURGUNDY_INPSEL3);
1667	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINCD,
1668			   DEF_BURGUNDY_GAINCD);
1669	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINLINE,
1670			   DEF_BURGUNDY_GAINLINE);
1671	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMIC,
1672			   DEF_BURGUNDY_GAINMIC);
1673	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMODEM,
1674			   DEF_BURGUNDY_GAINMODEM);
1675
1676	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER,
1677			   DEF_BURGUNDY_ATTENSPEAKER);
1678	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENLINEOUT,
1679			   DEF_BURGUNDY_ATTENLINEOUT);
1680	awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENHP,
1681			   DEF_BURGUNDY_ATTENHP);
1682
1683	awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_MASTER_VOLUME,
1684			   DEF_BURGUNDY_MASTER_VOLUME);
1685	awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLCD,
1686			   DEF_BURGUNDY_VOLCD);
1687	awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLLINE,
1688			   DEF_BURGUNDY_VOLLINE);
1689	awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLMIC,
1690			   DEF_BURGUNDY_VOLMIC);
1691	return 0;
1692}
1693
1694static void
1695awacs_burgundy_write_volume(unsigned address, int volume)
1696{
1697	int hardvolume,lvolume,rvolume;
1698
1699	lvolume = (volume & 0xff) ? (volume & 0xff) + 155 : 0;
1700	rvolume = ((volume >>8)&0xff) ? ((volume >> 8)&0xff ) + 155 : 0;
1701
1702	hardvolume = lvolume + (rvolume << 16);
1703
1704	awacs_burgundy_wcw(address, hardvolume);
1705}
1706
1707static int
1708awacs_burgundy_read_volume(unsigned address)
1709{
1710	int softvolume,wvolume;
1711
1712	wvolume = awacs_burgundy_rcw(address);
1713
1714	softvolume = (wvolume & 0xff) - 155;
1715	softvolume += (((wvolume >> 16) & 0xff) - 155)<<8;
1716
1717	return softvolume > 0 ? softvolume : 0;
1718}
1719
1720static int
1721awacs_burgundy_read_mvolume(unsigned address)
1722{
1723	int lvolume,rvolume,wvolume;
1724
1725	wvolume = awacs_burgundy_rcw(address);
1726
1727	wvolume &= 0xffff;
1728
1729	rvolume = (wvolume & 0xff) - 155;
1730	lvolume = ((wvolume & 0xff00)>>8) - 155;
1731
1732	return lvolume + (rvolume << 8);
1733}
1734
1735static void
1736awacs_burgundy_write_mvolume(unsigned address, int volume)
1737{
1738	int lvolume,rvolume,hardvolume;
1739
1740	lvolume = (volume &0xff) ? (volume & 0xff) + 155 :0;
1741	rvolume = ((volume >>8) & 0xff) ? (volume >> 8) + 155 :0;
1742
1743	hardvolume = lvolume + (rvolume << 8);
1744	hardvolume += (hardvolume << 16);
1745
1746	awacs_burgundy_wcw(address, hardvolume);
1747}
1748
1749/* End burgundy functions */
1750
1751/* Set up output volumes on machines with the 'perch/whisper' extension card.
1752 * this has an SGS i2c chip (7433) which is accessed using the cuda.
1753 *
1754 * TODO: split this out and make use of the other parts of the SGS chip to
1755 * do Bass, Treble etc.
1756 */
1757
1758static void
1759awacs_enable_amp(int spkr_vol)
1760{
1761#ifdef CONFIG_ADB_CUDA
1762	struct adb_request req;
1763
1764	if (sys_ctrler != SYS_CTRLER_CUDA)
1765		return;
1766
1767	/* turn on headphones */
1768	cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
1769		     0x8a, 4, 0);
1770	while (!req.complete) cuda_poll();
1771	cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
1772		     0x8a, 6, 0);
1773	while (!req.complete) cuda_poll();
1774
1775	/* turn on speaker */
1776	cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
1777		     0x8a, 3, (100 - (spkr_vol & 0xff)) * 32 / 100);
1778	while (!req.complete) cuda_poll();
1779	cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
1780		     0x8a, 5, (100 - ((spkr_vol >> 8) & 0xff)) * 32 / 100);
1781	while (!req.complete) cuda_poll();
1782
1783	cuda_request(&req, NULL, 5, CUDA_PACKET,
1784		     CUDA_GET_SET_IIC, 0x8a, 1, 0x29);
1785	while (!req.complete) cuda_poll();
1786#endif /* CONFIG_ADB_CUDA */
1787}
1788
1789
1790/*** Mid level stuff *********************************************************/
1791
1792
1793/*
1794 * /dev/mixer abstraction
1795 */
1796
1797static void do_line_lev(int data)
1798{
1799		line_lev = data ;
1800		awacs_reg[0] &= ~MASK_MUX_AUDIN;
1801		if ((data & 0xff) >= 50)
1802			awacs_reg[0] |= MASK_MUX_AUDIN;
1803		awacs_write(MASK_ADDR0 | awacs_reg[0]);
1804}
1805
1806static void do_ip_gain(int data)
1807{
1808	ip_gain = data ;
1809	data &= 0xff;
1810	awacs_reg[0] &= ~MASK_GAINLINE;
1811	if (awacs_revision == AWACS_SCREAMER) {
1812		awacs_reg[6] &= ~MASK_MIC_BOOST ;
1813		if (data >= 33) {
1814			awacs_reg[0] |= MASK_GAINLINE;
1815			if( data >= 66)
1816				awacs_reg[6] |= MASK_MIC_BOOST ;
1817		}
1818		awacs_write(MASK_ADDR6 | awacs_reg[6]) ;
1819	} else {
1820		if (data >= 50)
1821			awacs_reg[0] |= MASK_GAINLINE;
1822	}
1823	awacs_write(MASK_ADDR0 | awacs_reg[0]);
1824}
1825
1826static void do_mic_lev(int data)
1827{
1828	mic_lev = data ;
1829	data &= 0xff;
1830	awacs_reg[0] &= ~MASK_MUX_MIC;
1831	if (data >= 50)
1832		awacs_reg[0] |= MASK_MUX_MIC;
1833	awacs_write(MASK_ADDR0 | awacs_reg[0]);
1834}
1835
1836static void do_cd_lev(int data)
1837{
1838	cd_lev = data ;
1839	awacs_reg[0] &= ~MASK_MUX_CD;
1840	if ((data & 0xff) >= 50)
1841		awacs_reg[0] |= MASK_MUX_CD;
1842	awacs_write(MASK_ADDR0 | awacs_reg[0]);
1843}
1844
1845static void do_rec_lev(int data)
1846{
1847	int left, right ;
1848	rec_lev = data ;
1849	/* need to fudge this to use the volume setter routine */
1850	left = 100 - (data & 0xff) ; if( left < 0 ) left = 0 ;
1851	right = 100 - ((data >> 8) & 0xff) ; if( right < 0 ) right = 0 ;
1852	left |= (right << 8 );
1853	left = awacs_volume_setter(left, 0, 0, 4);
1854}
1855
1856static void do_passthru_vol(int data)
1857{
1858	passthru_vol = data ;
1859	awacs_reg[1] &= ~MASK_LOOPTHRU;
1860	if (awacs_revision == AWACS_SCREAMER) {
1861		if( data ) { /* switch it on for non-zero */
1862			awacs_reg[1] |= MASK_LOOPTHRU;
1863			awacs_write(MASK_ADDR1 | awacs_reg[1]);
1864		}
1865		data = awacs_volume_setter(data, 5, 0, 6) ;
1866	} else {
1867		if ((data & 0xff) >= 50)
1868			awacs_reg[1] |= MASK_LOOPTHRU;
1869		awacs_write(MASK_ADDR1 | awacs_reg[1]);
1870		data = (awacs_reg[1] & MASK_LOOPTHRU)? 100: 0;
1871	}
1872}
1873
1874static int awacs_mixer_ioctl(u_int cmd, u_long arg)
1875{
1876	int data;
1877	int rc;
1878
1879	switch (cmd) {
1880	case SOUND_MIXER_READ_CAPS:
1881		/* say we will allow multiple inputs?  prob. wrong
1882			so I'm switching it to single */
1883		return IOCTL_OUT(arg, 1);
1884	case SOUND_MIXER_READ_DEVMASK:
1885		data  = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
1886			| SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD
1887			| SOUND_MASK_IGAIN | SOUND_MASK_RECLEV
1888			| SOUND_MASK_ALTPCM
1889			| SOUND_MASK_MONITOR;
1890		rc = IOCTL_OUT(arg, data);
1891		break;
1892	case SOUND_MIXER_READ_RECMASK:
1893		data = SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD;
1894		rc = IOCTL_OUT(arg, data);
1895		break;
1896	case SOUND_MIXER_READ_RECSRC:
1897		data = 0;
1898		if (awacs_reg[0] & MASK_MUX_AUDIN)
1899			data |= SOUND_MASK_LINE;
1900		if (awacs_reg[0] & MASK_MUX_MIC)
1901			data |= SOUND_MASK_MIC;
1902		if (awacs_reg[0] & MASK_MUX_CD)
1903			data |= SOUND_MASK_CD;
1904		rc = IOCTL_OUT(arg, data);
1905		break;
1906	case SOUND_MIXER_WRITE_RECSRC:
1907		IOCTL_IN(arg, data);
1908		data &= (SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD);
1909		awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
1910				  | MASK_MUX_AUDIN);
1911		if (data & SOUND_MASK_LINE)
1912			awacs_reg[0] |= MASK_MUX_AUDIN;
1913		if (data & SOUND_MASK_MIC)
1914			awacs_reg[0] |= MASK_MUX_MIC;
1915		if (data & SOUND_MASK_CD)
1916			awacs_reg[0] |= MASK_MUX_CD;
1917		awacs_write(awacs_reg[0] | MASK_ADDR0);
1918		rc = IOCTL_OUT(arg, data);
1919		break;
1920	case SOUND_MIXER_READ_STEREODEVS:
1921		data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER| SOUND_MASK_RECLEV  ;
1922		if (awacs_revision == AWACS_SCREAMER)
1923			data |= SOUND_MASK_MONITOR ;
1924		rc = IOCTL_OUT(arg, data);
1925		break;
1926	case SOUND_MIXER_WRITE_VOLUME:
1927		IOCTL_IN(arg, data);
1928		line_vol = data ;
1929		awacs_volume_setter(data, 2, 0, 6);
1930		/* fall through */
1931	case SOUND_MIXER_READ_VOLUME:
1932		rc = IOCTL_OUT(arg, line_vol);
1933		break;
1934	case SOUND_MIXER_WRITE_SPEAKER:
1935		IOCTL_IN(arg, data);
1936		spk_vol = data ;
1937		if (has_perch)
1938			awacs_enable_amp(data);
1939		else
1940			(void)awacs_volume_setter(data, 4, MASK_CMUTE, 6);
1941		/* fall though */
1942	case SOUND_MIXER_READ_SPEAKER:
1943		rc = IOCTL_OUT(arg, spk_vol);
1944		break;
1945	case SOUND_MIXER_WRITE_ALTPCM:	/* really bell volume */
1946		IOCTL_IN(arg, data);
1947		beep_vol = data & 0xff;
1948		/* fall through */
1949	case SOUND_MIXER_READ_ALTPCM:
1950		rc = IOCTL_OUT(arg, beep_vol);
1951		break;
1952	case SOUND_MIXER_WRITE_LINE:
1953		IOCTL_IN(arg, data);
1954		do_line_lev(data) ;
1955		/* fall through */
1956	case SOUND_MIXER_READ_LINE:
1957		rc = IOCTL_OUT(arg, line_lev);
1958		break;
1959	case SOUND_MIXER_WRITE_IGAIN:
1960		IOCTL_IN(arg, data);
1961		do_ip_gain(data) ;
1962		/* fall through */
1963	case SOUND_MIXER_READ_IGAIN:
1964		rc = IOCTL_OUT(arg, ip_gain);
1965		break;
1966	case SOUND_MIXER_WRITE_MIC:
1967		IOCTL_IN(arg, data);
1968		do_mic_lev(data);
1969		/* fall through */
1970	case SOUND_MIXER_READ_MIC:
1971		rc = IOCTL_OUT(arg, mic_lev);
1972		break;
1973	case SOUND_MIXER_WRITE_CD:
1974		IOCTL_IN(arg, data);
1975		do_cd_lev(data);
1976		/* fall through */
1977	case SOUND_MIXER_READ_CD:
1978		rc = IOCTL_OUT(arg, cd_lev);
1979		break;
1980	case SOUND_MIXER_WRITE_RECLEV:
1981		IOCTL_IN(arg, data);
1982		do_rec_lev(data) ;
1983		/* fall through */
1984	case SOUND_MIXER_READ_RECLEV:
1985		rc = IOCTL_OUT(arg, rec_lev);
1986		break;
1987	case MIXER_WRITE(SOUND_MIXER_MONITOR):
1988		IOCTL_IN(arg, data);
1989		do_passthru_vol(data) ;
1990		/* fall through */
1991	case MIXER_READ(SOUND_MIXER_MONITOR):
1992		rc = IOCTL_OUT(arg, passthru_vol);
1993		break;
1994	default:
1995		rc = -EINVAL;
1996	}
1997
1998	return rc;
1999}
2000
2001static void awacs_mixer_init(void)
2002{
2003	awacs_volume_setter(line_vol, 2, 0, 6);
2004	if (has_perch)
2005		awacs_enable_amp(spk_vol);
2006	else
2007		(void)awacs_volume_setter(spk_vol, 4, MASK_CMUTE, 6);
2008	do_line_lev(line_lev) ;
2009	do_ip_gain(ip_gain) ;
2010	do_mic_lev(mic_lev) ;
2011	do_cd_lev(cd_lev) ;
2012	do_rec_lev(rec_lev) ;
2013	do_passthru_vol(passthru_vol) ;
2014}
2015
2016static int burgundy_mixer_ioctl(u_int cmd, u_long arg)
2017{
2018	int data;
2019	int rc;
2020
2021	/* We are, we are, we are... Burgundy or better */
2022	switch(cmd) {
2023	case SOUND_MIXER_READ_DEVMASK:
2024		data = SOUND_MASK_VOLUME | SOUND_MASK_CD |
2025			SOUND_MASK_LINE | SOUND_MASK_MIC |
2026			SOUND_MASK_SPEAKER | SOUND_MASK_ALTPCM;
2027		rc = IOCTL_OUT(arg, data);
2028		break;
2029	case SOUND_MIXER_READ_RECMASK:
2030		data = SOUND_MASK_LINE | SOUND_MASK_MIC
2031			| SOUND_MASK_CD;
2032		rc = IOCTL_OUT(arg, data);
2033		break;
2034	case SOUND_MIXER_READ_RECSRC:
2035		data = 0;
2036		if (awacs_reg[0] & MASK_MUX_AUDIN)
2037			data |= SOUND_MASK_LINE;
2038		if (awacs_reg[0] & MASK_MUX_MIC)
2039			data |= SOUND_MASK_MIC;
2040		if (awacs_reg[0] & MASK_MUX_CD)
2041			data |= SOUND_MASK_CD;
2042		rc = IOCTL_OUT(arg, data);
2043		break;
2044	case SOUND_MIXER_WRITE_RECSRC:
2045		IOCTL_IN(arg, data);
2046		data &= (SOUND_MASK_LINE
2047			 | SOUND_MASK_MIC | SOUND_MASK_CD);
2048		awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
2049				  | MASK_MUX_AUDIN);
2050		if (data & SOUND_MASK_LINE)
2051			awacs_reg[0] |= MASK_MUX_AUDIN;
2052		if (data & SOUND_MASK_MIC)
2053			awacs_reg[0] |= MASK_MUX_MIC;
2054		if (data & SOUND_MASK_CD)
2055			awacs_reg[0] |= MASK_MUX_CD;
2056		awacs_write(awacs_reg[0] | MASK_ADDR0);
2057		rc = IOCTL_OUT(arg, data);
2058		break;
2059	case SOUND_MIXER_READ_STEREODEVS:
2060		data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
2061			| SOUND_MASK_RECLEV | SOUND_MASK_CD
2062			| SOUND_MASK_LINE;
2063		rc = IOCTL_OUT(arg, data);
2064		break;
2065	case SOUND_MIXER_READ_CAPS:
2066		rc = IOCTL_OUT(arg, 0);
2067		break;
2068	case SOUND_MIXER_WRITE_VOLUME:
2069		IOCTL_IN(arg, data);
2070		awacs_burgundy_write_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME, data);
2071				/* Fall through */
2072	case SOUND_MIXER_READ_VOLUME:
2073		rc = IOCTL_OUT(arg, awacs_burgundy_read_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME));
2074		break;
2075	case SOUND_MIXER_WRITE_SPEAKER:
2076		IOCTL_IN(arg, data);
2077		if (!(data & 0xff)) {
2078			/* Mute the left speaker */
2079			awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
2080					   awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x2);
2081		} else {
2082			/* Unmute the left speaker */
2083			awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
2084					   awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x2);
2085		}
2086		if (!(data & 0xff00)) {
2087			/* Mute the right speaker */
2088			awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
2089					   awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x4);
2090		} else {
2091			/* Unmute the right speaker */
2092			awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
2093					   awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x4);
2094		}
2095
2096		data = (((data&0xff)*16)/100 > 0xf ? 0xf :
2097			(((data&0xff)*16)/100)) +
2098			((((data>>8)*16)/100 > 0xf ? 0xf :
2099			  ((((data>>8)*16)/100)))<<4);
2100
2101		awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER, ~data);
2102				/* Fall through */
2103	case SOUND_MIXER_READ_SPEAKER:
2104		data = awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER);
2105		data = (((data & 0xf)*100)/16) + ((((data>>4)*100)/16)<<8);
2106		rc = IOCTL_OUT(arg, (~data) & 0x0000ffff);
2107		break;
2108	case SOUND_MIXER_WRITE_ALTPCM:	/* really bell volume */
2109		IOCTL_IN(arg, data);
2110		beep_vol = data & 0xff;
2111				/* fall through */
2112	case SOUND_MIXER_READ_ALTPCM:
2113		rc = IOCTL_OUT(arg, beep_vol);
2114		break;
2115	case SOUND_MIXER_WRITE_LINE:
2116		IOCTL_IN(arg, data);
2117		awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLLINE, data);
2118
2119				/* fall through */
2120	case SOUND_MIXER_READ_LINE:
2121		data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLLINE);
2122		rc = IOCTL_OUT(arg, data);
2123		break;
2124	case SOUND_MIXER_WRITE_MIC:
2125		IOCTL_IN(arg, data);
2126				/* Mic is mono device */
2127		data = (data << 8) + (data << 24);
2128		awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLMIC, data);
2129				/* fall through */
2130	case SOUND_MIXER_READ_MIC:
2131		data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLMIC);
2132		data <<= 24;
2133		rc = IOCTL_OUT(arg, data);
2134		break;
2135	case SOUND_MIXER_WRITE_CD:
2136		IOCTL_IN(arg, data);
2137		awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLCD, data);
2138				/* fall through */
2139	case SOUND_MIXER_READ_CD:
2140		data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLCD);
2141		rc = IOCTL_OUT(arg, data);
2142		break;
2143	case SOUND_MIXER_WRITE_RECLEV:
2144		IOCTL_IN(arg, data);
2145		data = awacs_volume_setter(data, 0, 0, 4);
2146		rc = IOCTL_OUT(arg, data);
2147		break;
2148	case SOUND_MIXER_READ_RECLEV:
2149		data = awacs_get_volume(awacs_reg[0], 4);
2150		rc = IOCTL_OUT(arg, data);
2151		break;
2152	case SOUND_MIXER_OUTMASK:
2153	case SOUND_MIXER_OUTSRC:
2154	default:
2155		rc = -EINVAL;
2156	}
2157
2158	return rc;
2159}
2160
2161static int daca_mixer_ioctl(u_int cmd, u_long arg)
2162{
2163	int data;
2164	int rc;
2165
2166	/* And the DACA's no genius either! */
2167
2168	switch(cmd) {
2169	case SOUND_MIXER_READ_DEVMASK:
2170		data = SOUND_MASK_VOLUME;
2171		rc = IOCTL_OUT(arg, data);
2172		break;
2173	case SOUND_MIXER_READ_RECMASK:
2174		data = 0;
2175		rc = IOCTL_OUT(arg, data);
2176		break;
2177	case SOUND_MIXER_READ_RECSRC:
2178		data = 0;
2179		rc = IOCTL_OUT(arg, data);
2180		break;
2181	case SOUND_MIXER_WRITE_RECSRC:
2182		IOCTL_IN(arg, data);
2183		data =0;
2184		rc = IOCTL_OUT(arg, data);
2185		break;
2186	case SOUND_MIXER_READ_STEREODEVS:
2187		data = SOUND_MASK_VOLUME;
2188		rc = IOCTL_OUT(arg, data);
2189		break;
2190	case SOUND_MIXER_READ_CAPS:
2191		rc = IOCTL_OUT(arg, 0);
2192		break;
2193	case SOUND_MIXER_WRITE_VOLUME:
2194		IOCTL_IN(arg, data);
2195		daca_set_volume(data, data);
2196		/* Fall through */
2197	case SOUND_MIXER_READ_VOLUME:
2198		daca_get_volume(& data, &data);
2199		rc = IOCTL_OUT(arg, data);
2200		break;
2201	case SOUND_MIXER_OUTMASK:
2202	case SOUND_MIXER_OUTSRC:
2203	default:
2204		rc = -EINVAL;
2205	}
2206	return rc;
2207}
2208
2209static int PMacMixerIoctl(u_int cmd, u_long arg)
2210{
2211	int rc;
2212
2213	/* Different IOCTLS for burgundy and, eventually, DACA & Tumbler */
2214
2215	TRY_LOCK();
2216
2217	switch (awacs_revision){
2218		case AWACS_BURGUNDY:
2219			rc = burgundy_mixer_ioctl(cmd, arg);
2220			break ;
2221		case AWACS_DACA:
2222			rc = daca_mixer_ioctl(cmd, arg);
2223			break;
2224		case AWACS_TUMBLER:
2225		case AWACS_SNAPPER:
2226			rc = tas_mixer_ioctl(cmd, arg);
2227			break ;
2228		default: /* ;-)) */
2229			rc = awacs_mixer_ioctl(cmd, arg);
2230	}
2231
2232	UNLOCK();
2233
2234	return rc;
2235}
2236
2237static void PMacMixerInit(void)
2238{
2239	switch (awacs_revision) {
2240		case AWACS_TUMBLER:
2241		  printk("AE-Init tumbler mixer\n");
2242		  break ;
2243		case AWACS_SNAPPER:
2244		  printk("AE-Init snapper mixer\n");
2245		  break ;
2246		case AWACS_DACA:
2247		case AWACS_BURGUNDY:
2248			break ;	/* don't know yet */
2249		case AWACS_AWACS:
2250		case AWACS_SCREAMER:
2251		default:
2252			awacs_mixer_init() ;
2253			break ;
2254	}
2255}
2256
2257/* Write/Read sq setup functions:
2258   Check to see if we have enough (or any) dbdma cmd buffers for the
2259   user's fragment settings.  If not, allocate some. If this fails we will
2260   point at the beep buffer - as an emergency provision - to stop dma tromping
2261   on some random bit of memory (if someone lets it go anyway).
2262   The command buffers are then set up to point to the fragment buffers
2263   (allocated elsewhere).  We need n+1 commands the last of which holds
2264   a NOP + loop to start.
2265*/
2266
2267static int PMacWriteSqSetup(void)
2268{
2269	int i, count = 600 ;
2270	volatile struct dbdma_cmd *cp;
2271
2272	LOCK();
2273
2274	/* stop the controller from doing any output - if it isn't already.
2275	   it _should_ be before this is called anyway */
2276
2277	out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
2278	while ((in_le32(&awacs_txdma->status) & RUN) && count--)
2279		udelay(1);
2280#ifdef DEBUG_DMASOUND
2281if (count <= 0)
2282	printk("dmasound_pmac: write sq setup: timeout waiting for dma to stop\n");
2283#endif
2284
2285	if ((write_sq.max_count + 1) > number_of_tx_cmd_buffers) {
2286		kfree(awacs_tx_cmd_space);
2287		number_of_tx_cmd_buffers = 0;
2288
2289		/* we need nbufs + 1 (for the loop) and we should request + 1
2290		   again because the DBDMA_ALIGN might pull the start up by up
2291		   to sizeof(struct dbdma_cmd) - 4.
2292		*/
2293
2294		awacs_tx_cmd_space = kmalloc
2295			((write_sq.max_count + 1 + 1) * sizeof(struct dbdma_cmd),
2296			 GFP_KERNEL);
2297		if (awacs_tx_cmd_space == NULL) {
2298			/* don't leave it dangling - nasty but better than a
2299			   random address */
2300			out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
2301			printk(KERN_ERR
2302			   "dmasound_pmac: can't allocate dbdma cmd buffers"
2303			   ", driver disabled\n");
2304			UNLOCK();
2305			return -ENOMEM;
2306		}
2307		awacs_tx_cmds = (volatile struct dbdma_cmd *)
2308			DBDMA_ALIGN(awacs_tx_cmd_space);
2309		number_of_tx_cmd_buffers = write_sq.max_count + 1;
2310	}
2311
2312	cp = awacs_tx_cmds;
2313	memset((void *)cp, 0, (write_sq.max_count+1) * sizeof(struct dbdma_cmd));
2314	for (i = 0; i < write_sq.max_count; ++i, ++cp) {
2315		st_le32(&cp->phy_addr, virt_to_bus(write_sq.buffers[i]));
2316	}
2317	st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
2318	st_le32(&cp->cmd_dep, virt_to_bus(awacs_tx_cmds));
2319	/* point the controller at the command stack - ready to go */
2320	out_le32(&awacs_txdma->cmdptr, virt_to_bus(awacs_tx_cmds));
2321	UNLOCK();
2322	return 0;
2323}
2324
2325static int PMacReadSqSetup(void)
2326{
2327	int i, count = 600;
2328	volatile struct dbdma_cmd *cp;
2329
2330	LOCK();
2331
2332	/* stop the controller from doing any input - if it isn't already.
2333	   it _should_ be before this is called anyway */
2334
2335	out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
2336	while ((in_le32(&awacs_rxdma->status) & RUN) && count--)
2337		udelay(1);
2338#ifdef DEBUG_DMASOUND
2339if (count <= 0)
2340	printk("dmasound_pmac: read sq setup: timeout waiting for dma to stop\n");
2341#endif
2342
2343	if ((read_sq.max_count+1) > number_of_rx_cmd_buffers ) {
2344		kfree(awacs_rx_cmd_space);
2345		number_of_rx_cmd_buffers = 0;
2346
2347		/* we need nbufs + 1 (for the loop) and we should request + 1 again
2348		   because the DBDMA_ALIGN might pull the start up by up to
2349		   sizeof(struct dbdma_cmd) - 4 (assuming kmalloc aligns 32 bits).
2350		*/
2351
2352		awacs_rx_cmd_space = kmalloc
2353			((read_sq.max_count + 1 + 1) * sizeof(struct dbdma_cmd),
2354			 GFP_KERNEL);
2355		if (awacs_rx_cmd_space == NULL) {
2356			/* don't leave it dangling - nasty but better than a
2357			   random address */
2358			out_le32(&awacs_rxdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
2359			printk(KERN_ERR
2360			   "dmasound_pmac: can't allocate dbdma cmd buffers"
2361			   ", driver disabled\n");
2362			UNLOCK();
2363			return -ENOMEM;
2364		}
2365		awacs_rx_cmds = (volatile struct dbdma_cmd *)
2366			DBDMA_ALIGN(awacs_rx_cmd_space);
2367		number_of_rx_cmd_buffers = read_sq.max_count + 1 ;
2368	}
2369	cp = awacs_rx_cmds;
2370	memset((void *)cp, 0, (read_sq.max_count+1) * sizeof(struct dbdma_cmd));
2371
2372	/* Set dma buffers up in a loop */
2373	for (i = 0; i < read_sq.max_count; i++,cp++) {
2374		st_le32(&cp->phy_addr, virt_to_bus(read_sq.buffers[i]));
2375		st_le16(&cp->command, INPUT_MORE + INTR_ALWAYS);
2376		st_le16(&cp->req_count, read_sq.block_size);
2377		st_le16(&cp->xfer_status, 0);
2378	}
2379
2380	/* The next two lines make the thing loop around.
2381	*/
2382	st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
2383	st_le32(&cp->cmd_dep, virt_to_bus(awacs_rx_cmds));
2384	/* point the controller at the command stack - ready to go */
2385	out_le32(&awacs_rxdma->cmdptr, virt_to_bus(awacs_rx_cmds));
2386
2387	UNLOCK();
2388	return 0;
2389}
2390
2391/* TODO: this needs work to guarantee that when it returns DMA has stopped
2392   but in a more elegant way than is done here....
2393*/
2394
2395static void PMacAbortRead(void)
2396{
2397	int i;
2398	volatile struct dbdma_cmd *cp;
2399
2400	LOCK();
2401	/* give it a chance to update the output and provide the IRQ
2402	   that is expected.
2403	*/
2404
2405	out_le32(&awacs_rxdma->control, ((FLUSH) << 16) + FLUSH );
2406
2407	cp = awacs_rx_cmds;
2408	for (i = 0; i < read_sq.max_count; i++,cp++)
2409		st_le16(&cp->command, DBDMA_STOP);
2410	/*
2411	 * We should probably wait for the thing to stop before we
2412	 * release the memory.
2413	 */
2414
2415	msleep(100) ; /* give it a (small) chance to act */
2416
2417	/* apply the sledgehammer approach - just stop it now */
2418
2419	out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
2420	UNLOCK();
2421}
2422
2423extern char *get_afmt_string(int);
2424static int PMacStateInfo(char *b, size_t sp)
2425{
2426	int i, len = 0;
2427	len = sprintf(b,"HW rates: ");
2428	switch (awacs_revision){
2429		case AWACS_DACA:
2430		case AWACS_BURGUNDY:
2431			len += sprintf(b,"44100 ") ;
2432			break ;
2433		case AWACS_TUMBLER:
2434		case AWACS_SNAPPER:
2435			for (i=0; i<1; i++){
2436				if (tas_freqs_ok[i])
2437					len += sprintf(b+len,"%d ", tas_freqs[i]) ;
2438			}
2439			break ;
2440		case AWACS_AWACS:
2441		case AWACS_SCREAMER:
2442		default:
2443			for (i=0; i<8; i++){
2444				if (awacs_freqs_ok[i])
2445					len += sprintf(b+len,"%d ", awacs_freqs[i]) ;
2446			}
2447			break ;
2448	}
2449	len += sprintf(b+len,"s/sec\n") ;
2450	if (len < sp) {
2451		len += sprintf(b+len,"HW AFMTS: ");
2452		i = AFMT_U16_BE ;
2453		while (i) {
2454			if (i & dmasound.mach.hardware_afmts)
2455				len += sprintf(b+len,"%s ",
2456					get_afmt_string(i & dmasound.mach.hardware_afmts));
2457			i >>= 1 ;
2458		}
2459		len += sprintf(b+len,"\n") ;
2460	}
2461	return len ;
2462}
2463
2464/*** Machine definitions *****************************************************/
2465
2466static SETTINGS def_hard = {
2467	.format	= AFMT_S16_BE,
2468	.stereo	= 1,
2469	.size	= 16,
2470	.speed	= 44100
2471} ;
2472
2473static SETTINGS def_soft = {
2474	.format	= AFMT_S16_BE,
2475	.stereo	= 1,
2476	.size	= 16,
2477	.speed	= 44100
2478} ;
2479
2480static MACHINE machPMac = {
2481	.name		= awacs_name,
2482	.name2		= "PowerMac Built-in Sound",
2483	.owner		= THIS_MODULE,
2484	.dma_alloc	= PMacAlloc,
2485	.dma_free	= PMacFree,
2486	.irqinit	= PMacIrqInit,
2487#ifdef MODULE
2488	.irqcleanup	= PMacIrqCleanup,
2489#endif /* MODULE */
2490	.init		= PMacInit,
2491	.silence	= PMacSilence,
2492	.setFormat	= PMacSetFormat,
2493	.setVolume	= PMacSetVolume,
2494	.play		= PMacPlay,
2495	.record		= NULL,		/* default to no record */
2496	.mixer_init	= PMacMixerInit,
2497	.mixer_ioctl	= PMacMixerIoctl,
2498	.write_sq_setup	= PMacWriteSqSetup,
2499	.read_sq_setup	= PMacReadSqSetup,
2500	.state_info	= PMacStateInfo,
2501	.abort_read	= PMacAbortRead,
2502	.min_dsp_speed	= 7350,
2503	.max_dsp_speed	= 44100,
2504	.version	= ((DMASOUND_AWACS_REVISION<<8) + DMASOUND_AWACS_EDITION)
2505};
2506
2507
2508/*** Config & Setup **********************************************************/
2509
2510/* Check for pmac models that we care about in terms of special actions.
2511*/
2512
2513void __init
2514set_model(void)
2515{
2516	/* portables/lap-tops */
2517
2518	if (machine_is_compatible("AAPL,3400/2400") ||
2519	    machine_is_compatible("AAPL,3500"))	{
2520		is_pbook_3X00 = 1 ;
2521	}
2522	if (machine_is_compatible("PowerBook1,1")  || /* lombard */
2523	    machine_is_compatible("AAPL,PowerBook1998")){ /* wallstreet */
2524		is_pbook_g3 = 1 ;
2525		return ;
2526	}
2527}
2528
2529/* Get the OF node that tells us about the registers, interrupts etc. to use
2530   for sound IO.
2531
2532   On most machines the sound IO OF node is the 'davbus' node.  On newer pmacs
2533   with DACA (& Tumbler) the node to use is i2s-a.  On much older machines i.e.
2534   before 9500 there is no davbus node and we have to use the 'awacs' property.
2535
2536  In the latter case we signal this by setting the codec value - so that the
2537  code that looks for chip properties knows how to go about it.
2538*/
2539
2540static struct device_node* __init
2541get_snd_io_node(void)
2542{
2543	struct device_node *np;
2544
2545	/* set up awacs_node for early OF which doesn't have a full set of
2546	 * properties on davbus
2547	 */
2548	awacs_node = of_find_node_by_name(NULL, "awacs");
2549	if (awacs_node)
2550		awacs_revision = AWACS_AWACS;
2551
2552	/* powermac models after 9500 (other than those which use DACA or
2553	 * Tumbler) have a node called "davbus".
2554	 */
2555	np = of_find_node_by_name(NULL, "davbus");
2556	/*
2557	 * if we didn't find a davbus device, try 'i2s-a' since
2558	 * this seems to be what iBooks (& Tumbler) have.
2559	 */
2560	if (np == NULL) {
2561		i2s_node = of_find_node_by_name(NULL, "i2s-a");
2562		np = of_node_get(i2s_node);
2563	}
2564
2565	/* if we didn't find this - perhaps we are on an early model
2566	 * which _only_ has an 'awacs' node
2567	*/
2568	if (np == NULL && awacs_node)
2569		np = of_node_get(awacs_node);
2570
2571	/* if we failed all these return null - this will cause the
2572	 * driver to give up...
2573	*/
2574	return np ;
2575}
2576
2577/* Get the OF node that contains the info about the sound chip, inputs s-rates
2578   etc.
2579   This node does not exist (or contains much reduced info) on earlier machines
2580   we have to deduce the info other ways for these.
2581*/
2582
2583static struct device_node* __init
2584get_snd_info_node(struct device_node *io)
2585{
2586	struct device_node *info;
2587
2588	for_each_node_by_name(info, "sound")
2589		if (info->parent == io)
2590			break;
2591	return info;
2592}
2593
2594/* Find out what type of codec we have.
2595*/
2596
2597static int __init
2598get_codec_type(struct device_node *info)
2599{
2600	/* already set if pre-davbus model and info will be NULL */
2601	int codec = awacs_revision ;
2602
2603	if (info) {
2604		/* must do awacs first to allow screamer to overide it */
2605		if (of_device_is_compatible(info, "awacs"))
2606			codec = AWACS_AWACS ;
2607		if (of_device_is_compatible(info, "screamer"))
2608			codec = AWACS_SCREAMER;
2609		if (of_device_is_compatible(info, "burgundy"))
2610			codec = AWACS_BURGUNDY ;
2611		if (of_device_is_compatible(info, "daca"))
2612			codec = AWACS_DACA;
2613		if (of_device_is_compatible(info, "tumbler"))
2614			codec = AWACS_TUMBLER;
2615		if (of_device_is_compatible(info, "snapper"))
2616			codec = AWACS_SNAPPER;
2617	}
2618	return codec ;
2619}
2620
2621/* find out what type, if any, of expansion card we have
2622*/
2623static void __init
2624get_expansion_type(void)
2625{
2626	struct device_node *dn;
2627
2628	dn = of_find_node_by_name(NULL, "perch");
2629	if (dn != NULL)
2630		has_perch = 1;
2631	of_node_put(dn);
2632
2633	dn = of_find_node_by_name(NULL, "pb-ziva-pc");
2634	if (dn != NULL)
2635		has_ziva = 1;
2636	of_node_put(dn);
2637	/* need to work out how we deal with iMac SRS module */
2638}
2639
2640/* set up frame rates.
2641 * I suspect that these routines don't quite go about it the right way:
2642 * - where there is more than one rate - I think that the first property
2643 * value is the number of rates.
2644 * TODO: check some more device trees and modify accordingly
2645 *       Set dmasound.mach.max_dsp_rate on the basis of these routines.
2646*/
2647
2648static void __init
2649awacs_init_frame_rates(const unsigned int *prop, unsigned int l)
2650{
2651	int i ;
2652	if (prop) {
2653		for (i=0; i<8; i++)
2654			awacs_freqs_ok[i] = 0 ;
2655		for (l /= sizeof(int); l > 0; --l) {
2656			unsigned int r = *prop++;
2657			/* Apple 'Fixed' format */
2658			if (r >= 0x10000)
2659				r >>= 16;
2660			for (i = 0; i < 8; ++i) {
2661				if (r == awacs_freqs[i]) {
2662					awacs_freqs_ok[i] = 1;
2663					break;
2664				}
2665			}
2666		}
2667	}
2668	/* else we assume that all the rates are available */
2669}
2670
2671static void __init
2672burgundy_init_frame_rates(const unsigned int *prop, unsigned int l)
2673{
2674	int temp[9] ;
2675	int i = 0 ;
2676	if (prop) {
2677		for (l /= sizeof(int); l > 0; --l) {
2678			unsigned int r = *prop++;
2679			/* Apple 'Fixed' format */
2680			if (r >= 0x10000)
2681				r >>= 16;
2682			temp[i] = r ;
2683			i++ ; if(i>=9) i=8;
2684		}
2685	}
2686#ifdef DEBUG_DMASOUND
2687if (i > 1){
2688	int j;
2689	printk("dmasound_pmac: burgundy with multiple frame rates\n");
2690	for(j=0; j<i; j++)
2691		printk("%d ", temp[j]) ;
2692	printk("\n") ;
2693}
2694#endif
2695}
2696
2697static void __init
2698daca_init_frame_rates(const unsigned int *prop, unsigned int l)
2699{
2700	int temp[9] ;
2701	int i = 0 ;
2702	if (prop) {
2703		for (l /= sizeof(int); l > 0; --l) {
2704			unsigned int r = *prop++;
2705			/* Apple 'Fixed' format */
2706			if (r >= 0x10000)
2707				r >>= 16;
2708			temp[i] = r ;
2709			i++ ; if(i>=9) i=8;
2710
2711		}
2712	}
2713#ifdef DEBUG_DMASOUND
2714if (i > 1){
2715	int j;
2716	printk("dmasound_pmac: DACA with multiple frame rates\n");
2717	for(j=0; j<i; j++)
2718		printk("%d ", temp[j]) ;
2719	printk("\n") ;
2720}
2721#endif
2722}
2723
2724static void __init
2725init_frame_rates(const unsigned int *prop, unsigned int l)
2726{
2727	switch (awacs_revision) {
2728		case AWACS_TUMBLER:
2729		case AWACS_SNAPPER:
2730			tas_init_frame_rates(prop, l);
2731			break ;
2732		case AWACS_DACA:
2733			daca_init_frame_rates(prop, l);
2734			break ;
2735		case AWACS_BURGUNDY:
2736			burgundy_init_frame_rates(prop, l);
2737			break ;
2738		default:
2739			awacs_init_frame_rates(prop, l);
2740			break ;
2741	}
2742}
2743
2744/* find things/machines that can't do mac-io byteswap
2745*/
2746
2747static void __init
2748set_hw_byteswap(struct device_node *io)
2749{
2750	struct device_node *mio ;
2751	unsigned int kl = 0 ;
2752
2753	/* if seems that Keylargo can't byte-swap  */
2754
2755	for (mio = io->parent; mio ; mio = mio->parent) {
2756		if (strcmp(mio->name, "mac-io") == 0) {
2757			if (of_device_is_compatible(mio, "Keylargo"))
2758				kl = 1;
2759			break;
2760		}
2761	}
2762	hw_can_byteswap = !kl;
2763}
2764
2765/* Allocate the resources necessary for beep generation.  This cannot be (quite)
2766   done statically (yet) because we cannot do virt_to_bus() on static vars when
2767   the code is loaded as a module.
2768
2769   for the sake of saving the possibility that two allocations will incur the
2770   overhead of two pull-ups in DBDMA_ALIGN() we allocate the 'emergency' dmdma
2771   command here as well... even tho' it is not part of the beep process.
2772*/
2773
2774int32_t
2775__init setup_beep(void)
2776{
2777	/* Initialize beep stuff */
2778	/* want one cmd buffer for beeps, and a second one for emergencies
2779	   - i.e. dbdma error conditions.
2780	   ask for three to allow for pull up in DBDMA_ALIGN().
2781	*/
2782	beep_dbdma_cmd_space =
2783		kmalloc((2 + 1) * sizeof(struct dbdma_cmd), GFP_KERNEL);
2784	if(beep_dbdma_cmd_space == NULL) {
2785		printk(KERN_ERR "dmasound_pmac: no beep dbdma cmd space\n") ;
2786		return -ENOMEM ;
2787	}
2788	beep_dbdma_cmd = (volatile struct dbdma_cmd *)
2789			DBDMA_ALIGN(beep_dbdma_cmd_space);
2790	/* set up emergency dbdma cmd */
2791	emergency_dbdma_cmd = beep_dbdma_cmd+1 ;
2792	beep_buf = kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
2793	if (beep_buf == NULL) {
2794		printk(KERN_ERR "dmasound_pmac: no memory for beep buffer\n");
2795		kfree(beep_dbdma_cmd_space) ;
2796		return -ENOMEM ;
2797	}
2798	return 0 ;
2799}
2800
2801static struct input_dev *awacs_beep_dev;
2802
2803int __init dmasound_awacs_init(void)
2804{
2805	struct device_node *io = NULL, *info = NULL;
2806	int vol, res;
2807
2808	if (!machine_is(powermac))
2809		return -ENODEV;
2810
2811	awacs_subframe = 0;
2812	awacs_revision = 0;
2813	hw_can_byteswap = 1 ; /* most can */
2814
2815	/* look for models we need to handle specially */
2816	set_model() ;
2817
2818	/* find the OF node that tells us about the dbdma stuff
2819	*/
2820	io = get_snd_io_node();
2821	if (io == NULL) {
2822#ifdef DEBUG_DMASOUND
2823printk("dmasound_pmac: couldn't find sound io OF node\n");
2824#endif
2825		goto no_device;
2826	}
2827
2828	/* find the OF node that tells us about the sound sub-system
2829	 * this doesn't exist on pre-davbus machines (earlier than 9500)
2830	*/
2831	if (awacs_revision != AWACS_AWACS) { /* set for pre-davbus */
2832		info = get_snd_info_node(io) ;
2833		if (info == NULL){
2834#ifdef DEBUG_DMASOUND
2835printk("dmasound_pmac: couldn't find 'sound' OF node\n");
2836#endif
2837			goto no_device;
2838		}
2839	}
2840
2841	awacs_revision = get_codec_type(info) ;
2842	if (awacs_revision == 0) {
2843#ifdef DEBUG_DMASOUND
2844printk("dmasound_pmac: couldn't find a Codec we can handle\n");
2845#endif
2846		goto no_device; /* we don't know this type of h/w */
2847	}
2848
2849	/* set up perch, ziva, SRS or whatever else we have as sound
2850	 *  expansion.
2851	*/
2852	get_expansion_type();
2853
2854	/* we've now got enough information to make up the audio topology.
2855	 * we will map the sound part of mac-io now so that we can probe for
2856	 * other info if necessary (early AWACS we want to read chip ids)
2857	 */
2858
2859	if (of_get_address(io, 2, NULL, NULL) == NULL) {
2860		/* OK - maybe we need to use the 'awacs' node (on earlier
2861		 * machines).
2862		 */
2863		if (awacs_node) {
2864			of_node_put(io);
2865			io = of_node_get(awacs_node);
2866			if (of_get_address(io, 2, NULL, NULL) == NULL) {
2867				printk("dmasound_pmac: can't use %s\n",
2868				       io->full_name);
2869				goto no_device;
2870			}
2871		} else
2872			printk("dmasound_pmac: can't use %s\n", io->full_name);
2873	}
2874
2875	if (of_address_to_resource(io, 0, &awacs_rsrc[0]) ||
2876	    request_mem_region(awacs_rsrc[0].start,
2877			       awacs_rsrc[0].end - awacs_rsrc[0].start + 1,
2878			       " (IO)") == NULL) {
2879		printk(KERN_ERR "dmasound: can't request IO resource !\n");
2880		goto no_device;
2881	}
2882	if (of_address_to_resource(io, 1, &awacs_rsrc[1]) ||
2883	    request_mem_region(awacs_rsrc[1].start,
2884			       awacs_rsrc[1].end - awacs_rsrc[1].start + 1,
2885			       " (tx dma)") == NULL) {
2886		release_mem_region(awacs_rsrc[0].start,
2887				   awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
2888		printk(KERN_ERR "dmasound: can't request Tx DMA resource !\n");
2889		goto no_device;
2890	}
2891	if (of_address_to_resource(io, 2, &awacs_rsrc[2]) ||
2892	    request_mem_region(awacs_rsrc[2].start,
2893			       awacs_rsrc[2].end - awacs_rsrc[2].start + 1,
2894			       " (rx dma)") == NULL) {
2895		release_mem_region(awacs_rsrc[0].start,
2896				   awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
2897		release_mem_region(awacs_rsrc[1].start,
2898				   awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
2899		printk(KERN_ERR "dmasound: can't request Rx DMA resource !\n");
2900		goto no_device;
2901	}
2902
2903	awacs_beep_dev = input_allocate_device();
2904	if (!awacs_beep_dev) {
2905		release_mem_region(awacs_rsrc[0].start,
2906				   awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
2907		release_mem_region(awacs_rsrc[1].start,
2908				   awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
2909		release_mem_region(awacs_rsrc[2].start,
2910				   awacs_rsrc[2].end - awacs_rsrc[2].start + 1);
2911		printk(KERN_ERR "dmasound: can't allocate input device !\n");
2912		goto no_device;
2913	}
2914
2915	awacs_beep_dev->name = "dmasound beeper";
2916	awacs_beep_dev->phys = "macio/input0";
2917	awacs_beep_dev->id.bustype = BUS_HOST;
2918	awacs_beep_dev->event = awacs_beep_event;
2919	awacs_beep_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
2920	awacs_beep_dev->evbit[0] = BIT(EV_SND);
2921
2922	/* all OF versions I've seen use this value */
2923	if (i2s_node)
2924		i2s = ioremap(awacs_rsrc[0].start, 0x1000);
2925	else
2926		awacs = ioremap(awacs_rsrc[0].start, 0x1000);
2927	awacs_txdma = ioremap(awacs_rsrc[1].start, 0x100);
2928	awacs_rxdma = ioremap(awacs_rsrc[2].start, 0x100);
2929
2930	/* first of all make sure that the chip is powered up....*/
2931	pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, io, 0, 1);
2932	if (awacs_revision == AWACS_SCREAMER && awacs)
2933		awacs_recalibrate();
2934
2935	awacs_irq = irq_of_parse_and_map(io, 0);
2936	awacs_tx_irq = irq_of_parse_and_map(io, 1);
2937	awacs_rx_irq = irq_of_parse_and_map(io, 2);
2938
2939	/* Hack for legacy crap that will be killed someday */
2940	of_node_put(awacs_node);
2941	awacs_node = of_node_get(io);
2942
2943	/* if we have an awacs or screamer - probe the chip to make
2944	 * sure we have the right revision.
2945	*/
2946
2947	if (awacs_revision <= AWACS_SCREAMER){
2948		uint32_t temp, rev, mfg ;
2949		/* find out the awacs revision from the chip */
2950		temp = in_le32(&awacs->codec_stat);
2951		rev = (temp >> 12) & 0xf;
2952		mfg = (temp >>  8) & 0xf;
2953#ifdef DEBUG_DMASOUND
2954printk("dmasound_pmac: Awacs/Screamer Codec Mfct: %d Rev %d\n", mfg, rev);
2955#endif
2956		if (rev >= AWACS_SCREAMER)
2957			awacs_revision = AWACS_SCREAMER ;
2958		else
2959			awacs_revision = rev ;
2960	}
2961
2962	dmasound.mach = machPMac;
2963
2964	/* find out other bits & pieces from OF, these may be present
2965	   only on some models ... so be careful.
2966	*/
2967
2968	/* in the absence of a frame rates property we will use the defaults
2969	*/
2970
2971	if (info) {
2972		const unsigned int *prop;
2973		unsigned int l;
2974
2975		sound_device_id = 0;
2976		/* device ID appears post g3 b&w */
2977		prop = of_get_property(info, "device-id", NULL);
2978		if (prop != 0)
2979			sound_device_id = *prop;
2980
2981		/* look for a property saying what sample rates
2982		   are available */
2983
2984		prop = of_get_property(info, "sample-rates", &l);
2985		if (prop == 0)
2986			prop = of_get_property(info, "output-frame-rates", &l);
2987
2988		/* if it's there use it to set up frame rates */
2989		init_frame_rates(prop, l) ;
2990		of_node_put(info);
2991		info = NULL;
2992	}
2993
2994	if (awacs)
2995		out_le32(&awacs->control, 0x11); /* set everything quiesent */
2996
2997	set_hw_byteswap(io) ; /* figure out if the h/w can do it */
2998
2999#ifdef CONFIG_NVRAM
3000	/* get default volume from nvram */
3001	vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
3002#else
3003	vol = 0;
3004#endif
3005
3006	/* set up tracking values */
3007	spk_vol = vol * 100 ;
3008	spk_vol /= 7 ; /* get set value to a percentage */
3009	spk_vol |= (spk_vol << 8) ; /* equal left & right */
3010 	line_vol = passthru_vol = spk_vol ;
3011
3012	/* fill regs that are shared between AWACS & Burgundy */
3013
3014	awacs_reg[2] = vol + (vol << 6);
3015	awacs_reg[4] = vol + (vol << 6);
3016	awacs_reg[5] = vol + (vol << 6); /* screamer has loopthru vol control */
3017	awacs_reg[6] = 0; /* maybe should be vol << 3 for PCMCIA speaker */
3018	awacs_reg[7] = 0;
3019
3020	awacs_reg[0] = MASK_MUX_CD;
3021	awacs_reg[1] = MASK_LOOPTHRU;
3022
3023	if (has_perch || sound_device_id == 0x5
3024	    || /*sound_device_id == 0x8 ||*/ sound_device_id == 0xb)
3025		awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
3026
3027	switch (awacs_revision) {
3028		case AWACS_TUMBLER:
3029                        tas_register_driver(&tas3001c_hooks);
3030			tas_init(I2C_DRIVERID_TAS3001C, I2C_DRIVERNAME_TAS3001C);
3031			tas_dmasound_init();
3032			tas_post_init();
3033			break ;
3034		case AWACS_SNAPPER:
3035                        tas_register_driver(&tas3004_hooks);
3036			tas_init(I2C_DRIVERID_TAS3004,I2C_DRIVERNAME_TAS3004);
3037			tas_dmasound_init();
3038			tas_post_init();
3039			break;
3040		case AWACS_DACA:
3041			daca_init();
3042			break;
3043		case AWACS_BURGUNDY:
3044			awacs_burgundy_init();
3045			break ;
3046		case AWACS_SCREAMER:
3047		case AWACS_AWACS:
3048		default:
3049			load_awacs();
3050			break ;
3051	}
3052
3053	/* enable/set-up external modules - when we know how */
3054
3055	if (has_perch)
3056		awacs_enable_amp(100 * 0x101);
3057
3058	/* Reset dbdma channels */
3059	out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
3060	while (in_le32(&awacs_txdma->status) & RUN)
3061		udelay(1);
3062	out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
3063	while (in_le32(&awacs_rxdma->status) & RUN)
3064		udelay(1);
3065
3066	/* Initialize beep stuff */
3067	if ((res=setup_beep()))
3068		return res ;
3069
3070#ifdef CONFIG_PM
3071	pmu_register_sleep_notifier(&awacs_sleep_notifier);
3072#endif /* CONFIG_PM */
3073
3074	/* Powerbooks have odd ways of enabling inputs such as
3075	   an expansion-bay CD or sound from an internal modem
3076	   or a PC-card modem. */
3077	if (is_pbook_3X00) {
3078		/*
3079		 * Enable CD and PC-card sound inputs.
3080		 * This is done by reading from address
3081		 * f301a000, + 0x10 to enable the expansion-bay
3082		 * CD sound input, + 0x80 to enable the PC-card
3083		 * sound input.  The 0x100 enables the SCSI bus
3084		 * terminator power.
3085		 */
3086		latch_base = ioremap (0xf301a000, 0x1000);
3087		in_8(latch_base + 0x190);
3088
3089	} else if (is_pbook_g3) {
3090		struct device_node* mio;
3091		macio_base = NULL;
3092		for (mio = io->parent; mio; mio = mio->parent) {
3093			if (strcmp(mio->name, "mac-io") == 0) {
3094				struct resource r;
3095				if (of_address_to_resource(mio, 0, &r) == 0)
3096					macio_base = ioremap(r.start, 0x40);
3097				break;
3098			}
3099		}
3100		/*
3101		 * Enable CD sound input.
3102		 * The relevant bits for writing to this byte are 0x8f.
3103		 * I haven't found out what the 0x80 bit does.
3104		 * For the 0xf bits, writing 3 or 7 enables the CD
3105		 * input, any other value disables it.  Values
3106		 * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
3107		 * 4, 6, 8 - f enable the input from the modem.
3108		 *  -- paulus.
3109		 */
3110		if (macio_base)
3111			out_8(macio_base + 0x37, 3);
3112	}
3113
3114	if (hw_can_byteswap)
3115 		dmasound.mach.hardware_afmts = (AFMT_S16_BE | AFMT_S16_LE) ;
3116 	else
3117		dmasound.mach.hardware_afmts = AFMT_S16_BE ;
3118
3119	/* shut out chips that do output only.
3120	 * may need to extend this to machines which have no inputs - even tho'
3121	 * they use screamer - IIRC one of the powerbooks is like this.
3122	 */
3123
3124	if (awacs_revision != AWACS_DACA) {
3125		dmasound.mach.capabilities = DSP_CAP_DUPLEX ;
3126		dmasound.mach.record = PMacRecord ;
3127	}
3128
3129	dmasound.mach.default_hard = def_hard ;
3130	dmasound.mach.default_soft = def_soft ;
3131
3132	switch (awacs_revision) {
3133		case AWACS_BURGUNDY:
3134			sprintf(awacs_name, "PowerMac Burgundy ") ;
3135			break ;
3136		case AWACS_DACA:
3137			sprintf(awacs_name, "PowerMac DACA ") ;
3138			break ;
3139		case AWACS_TUMBLER:
3140			sprintf(awacs_name, "PowerMac Tumbler ") ;
3141			break ;
3142		case AWACS_SNAPPER:
3143			sprintf(awacs_name, "PowerMac Snapper ") ;
3144			break ;
3145		case AWACS_SCREAMER:
3146			sprintf(awacs_name, "PowerMac Screamer ") ;
3147			break ;
3148		case AWACS_AWACS:
3149		default:
3150			sprintf(awacs_name, "PowerMac AWACS rev %d ", awacs_revision) ;
3151			break ;
3152	}
3153
3154	input_register_device(awacs_beep_dev);
3155
3156	of_node_put(io);
3157
3158	return dmasound_init();
3159
3160no_device:
3161	of_node_put(info);
3162	of_node_put(awacs_node);
3163	of_node_put(i2s_node);
3164	of_node_put(io);
3165	return -ENODEV ;
3166}
3167
3168static void __exit dmasound_awacs_cleanup(void)
3169{
3170	input_unregister_device(awacs_beep_dev);
3171
3172	switch (awacs_revision) {
3173		case AWACS_TUMBLER:
3174		case AWACS_SNAPPER:
3175			tas_dmasound_cleanup();
3176			tas_cleanup();
3177			break ;
3178		case AWACS_DACA:
3179			daca_cleanup();
3180			break;
3181	}
3182	dmasound_deinit();
3183
3184	of_node_put(awacs_node);
3185	of_node_put(i2s_node);
3186}
3187
3188MODULE_DESCRIPTION("PowerMac built-in audio driver.");
3189MODULE_LICENSE("GPL");
3190
3191module_init(dmasound_awacs_init);
3192module_exit(dmasound_awacs_cleanup);
3193