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