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