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