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