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