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