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