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