vserver 1.9.3
[linux-2.6.git] / sound / ppc / pmac.c
1 /*
2  * PMac DBDMA lowlevel functions
3  *
4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  * code based on dmasound.c.
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22
23 #include <sound/driver.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <sound/core.h>
31 #include "pmac.h"
32 #include <sound/pcm_params.h>
33 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
34 #include <asm/pmac_feature.h>
35 #else
36 #include <asm/feature.h>
37 #endif
38
39
40 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
41 static int snd_pmac_register_sleep_notifier(pmac_t *chip);
42 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip);
43 static int snd_pmac_suspend(snd_card_t *card, unsigned int state);
44 static int snd_pmac_resume(snd_card_t *card, unsigned int state);
45 #endif
46
47
48 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
49 static int awacs_freqs[8] = {
50         44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
51 };
52 /* fixed frequency table for tumbler */
53 static int tumbler_freqs[1] = {
54         44100
55 };
56
57 /*
58  * allocate DBDMA command arrays
59  */
60 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
61 {
62         rec->space = kmalloc(sizeof(struct dbdma_cmd) * (size + 1), GFP_KERNEL);
63         if (rec->space == NULL)
64                 return -ENOMEM;
65         rec->size = size;
66         memset(rec->space, 0, sizeof(struct dbdma_cmd) * (size + 1));
67         rec->cmds = (void*)DBDMA_ALIGN(rec->space);
68         rec->addr = virt_to_bus(rec->cmds);
69         return 0;
70 }
71
72 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
73 {
74         if (rec && rec->space)
75                 kfree(rec->space);
76 }
77
78
79 /*
80  * pcm stuff
81  */
82
83 /*
84  * look up frequency table
85  */
86
87 unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
88 {
89         int i, ok, found;
90
91         ok = rec->cur_freqs;
92         if (rate > chip->freq_table[0])
93                 return 0;
94         found = 0;
95         for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
96                 if (! (ok & 1)) continue;
97                 found = i;
98                 if (rate >= chip->freq_table[i])
99                         break;
100         }
101         return found;
102 }
103
104 /*
105  * check whether another stream is active
106  */
107 static inline int another_stream(int stream)
108 {
109         return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
110                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
111 }
112
113 /*
114  * allocate buffers
115  */
116 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
117                                   snd_pcm_hw_params_t *hw_params)
118 {
119         return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
120 }
121
122 /*
123  * release buffers
124  */
125 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
126 {
127         snd_pcm_lib_free_pages(subs);
128         return 0;
129 }
130
131 /*
132  * get a stream of the opposite direction
133  */
134 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
135 {
136         switch (stream) {
137         case SNDRV_PCM_STREAM_PLAYBACK:
138                 return &chip->playback;
139         case SNDRV_PCM_STREAM_CAPTURE:
140                 return &chip->capture;
141         default:
142                 snd_BUG();
143                 return NULL;
144         }
145 }
146
147 /*
148  * wait while run status is on
149  */
150 inline static void
151 snd_pmac_wait_ack(pmac_stream_t *rec)
152 {
153         int timeout = 50000;
154         while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
155                 udelay(1);
156 }
157
158 /*
159  * set the format and rate to the chip.
160  * call the lowlevel function if defined (e.g. for AWACS).
161  */
162 static void snd_pmac_pcm_set_format(pmac_t *chip)
163 {
164         /* set up frequency and format */
165         out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
166         out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
167         if (chip->set_format)
168                 chip->set_format(chip);
169 }
170
171 /*
172  * stop the DMA transfer
173  */
174 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
175 {
176         out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
177         snd_pmac_wait_ack(rec);
178 }
179
180 /*
181  * set the command pointer address
182  */
183 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
184 {
185         out_le32(&rec->dma->cmdptr, cmd->addr);
186 }
187
188 /*
189  * start the DMA
190  */
191 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
192 {
193         out_le32(&rec->dma->control, status | (status << 16));
194 }
195
196
197 /*
198  * prepare playback/capture stream
199  */
200 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
201 {
202         int i;
203         volatile struct dbdma_cmd *cp;
204         snd_pcm_runtime_t *runtime = subs->runtime;
205         int rate_index;
206         long offset;
207         pmac_stream_t *astr;
208         
209         rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
210         rec->period_size = snd_pcm_lib_period_bytes(subs);
211         rec->nperiods = rec->dma_size / rec->period_size;
212         rec->cur_period = 0;
213         rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
214
215         /* set up constraints */
216         astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
217         snd_runtime_check(astr, return -EINVAL);
218         astr->cur_freqs = 1 << rate_index;
219         astr->cur_formats = 1 << runtime->format;
220         chip->rate_index = rate_index;
221         chip->format = runtime->format;
222
223         /* We really want to execute a DMA stop command, after the AWACS
224          * is initialized.
225          * For reasons I don't understand, it stops the hissing noise
226          * common to many PowerBook G3 systems and random noise otherwise
227          * captured on iBook2's about every third time. -ReneR
228          */
229         spin_lock_irq(&chip->reg_lock);
230         snd_pmac_dma_stop(rec);
231         st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
232         snd_pmac_dma_set_command(rec, &chip->extra_dma);
233         snd_pmac_dma_run(rec, RUN);
234         spin_unlock_irq(&chip->reg_lock);
235         mdelay(5);
236         spin_lock_irq(&chip->reg_lock);
237         /* continuous DMA memory type doesn't provide the physical address,
238          * so we need to resolve the address here...
239          */
240         offset = virt_to_bus(runtime->dma_area);
241         for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
242                 st_le32(&cp->phy_addr, offset);
243                 st_le16(&cp->req_count, rec->period_size);
244                 /*st_le16(&cp->res_count, 0);*/
245                 st_le16(&cp->xfer_status, 0);
246                 offset += rec->period_size;
247         }
248         /* make loop */
249         st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
250         st_le32(&cp->cmd_dep, rec->cmd.addr);
251
252         snd_pmac_dma_stop(rec);
253         snd_pmac_dma_set_command(rec, &rec->cmd);
254         spin_unlock_irq(&chip->reg_lock);
255
256         return 0;
257 }
258
259
260 /*
261  * PCM trigger/stop
262  */
263 static int snd_pmac_pcm_trigger(pmac_t *chip, pmac_stream_t *rec,
264                                 snd_pcm_substream_t *subs, int cmd)
265 {
266         volatile struct dbdma_cmd *cp;
267         int i, command;
268
269         switch (cmd) {
270         case SNDRV_PCM_TRIGGER_START:
271         case SNDRV_PCM_TRIGGER_RESUME:
272                 if (rec->running)
273                         return -EBUSY;
274                 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
275                            OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
276                 spin_lock(&chip->reg_lock);
277                 snd_pmac_beep_stop(chip);
278                 snd_pmac_pcm_set_format(chip);
279                 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
280                         out_le16(&cp->command, command);
281                 snd_pmac_dma_set_command(rec, &rec->cmd);
282                 (void)in_le32(&rec->dma->status);
283                 snd_pmac_dma_run(rec, RUN|WAKE);
284                 rec->running = 1;
285                 spin_unlock(&chip->reg_lock);
286                 break;
287
288         case SNDRV_PCM_TRIGGER_STOP:
289         case SNDRV_PCM_TRIGGER_SUSPEND:
290                 spin_lock(&chip->reg_lock);
291                 rec->running = 0;
292                 /*printk("stopped!!\n");*/
293                 snd_pmac_dma_stop(rec);
294                 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
295                         out_le16(&cp->command, DBDMA_STOP);
296                 spin_unlock(&chip->reg_lock);
297                 break;
298
299         default:
300                 return -EINVAL;
301         }
302
303         return 0;
304 }
305
306 /*
307  * return the current pointer
308  */
309 inline
310 static snd_pcm_uframes_t snd_pmac_pcm_pointer(pmac_t *chip, pmac_stream_t *rec,
311                                               snd_pcm_substream_t *subs)
312 {
313         int count = 0;
314
315 #if 1 /* hmm.. how can we get the current dma pointer?? */
316         int stat;
317         volatile struct dbdma_cmd *cp = &rec->cmd.cmds[rec->cur_period];
318         stat = ld_le16(&cp->xfer_status);
319         if (stat & (ACTIVE|DEAD)) {
320                 count = in_le16(&cp->res_count);
321                 count = rec->period_size - count;
322         }
323 #endif
324         count += rec->cur_period * rec->period_size;
325         /*printk("pointer=%d\n", count);*/
326         return bytes_to_frames(subs->runtime, count);
327 }
328
329 /*
330  * playback
331  */
332
333 static int snd_pmac_playback_prepare(snd_pcm_substream_t *subs)
334 {
335         pmac_t *chip = snd_pcm_substream_chip(subs);
336         return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
337 }
338
339 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
340                                      int cmd)
341 {
342         pmac_t *chip = snd_pcm_substream_chip(subs);
343         return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
344 }
345
346 static snd_pcm_uframes_t snd_pmac_playback_pointer(snd_pcm_substream_t *subs)
347 {
348         pmac_t *chip = snd_pcm_substream_chip(subs);
349         return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
350 }
351
352
353 /*
354  * capture
355  */
356
357 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
358 {
359         pmac_t *chip = snd_pcm_substream_chip(subs);
360         return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
361 }
362
363 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
364                                     int cmd)
365 {
366         pmac_t *chip = snd_pcm_substream_chip(subs);
367         return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
368 }
369
370 static snd_pcm_uframes_t snd_pmac_capture_pointer(snd_pcm_substream_t *subs)
371 {
372         pmac_t *chip = snd_pcm_substream_chip(subs);
373         return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
374 }
375
376
377 /*
378  * update playback/capture pointer from interrupts
379  */
380 static void snd_pmac_pcm_update(pmac_t *chip, pmac_stream_t *rec)
381 {
382         volatile struct dbdma_cmd *cp;
383         int c;
384         int stat;
385
386         spin_lock(&chip->reg_lock);
387         if (rec->running) {
388                 cp = &rec->cmd.cmds[rec->cur_period];
389                 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
390                         stat = ld_le16(&cp->xfer_status);
391                         if (! (stat & ACTIVE))
392                                 break;
393                         /*printk("update frag %d\n", rec->cur_period);*/
394                         st_le16(&cp->xfer_status, 0);
395                         st_le16(&cp->req_count, rec->period_size);
396                         /*st_le16(&cp->res_count, 0);*/
397                         rec->cur_period++;
398                         if (rec->cur_period >= rec->nperiods) {
399                                 rec->cur_period = 0;
400                                 cp = rec->cmd.cmds;
401                         } else
402                                 cp++;
403                         spin_unlock(&chip->reg_lock);
404                         snd_pcm_period_elapsed(rec->substream);
405                         spin_lock(&chip->reg_lock);
406                 }
407         }
408         spin_unlock(&chip->reg_lock);
409 }
410
411
412 /*
413  * hw info
414  */
415
416 static snd_pcm_hardware_t snd_pmac_playback =
417 {
418         .info =                 (SNDRV_PCM_INFO_INTERLEAVED |
419                                  SNDRV_PCM_INFO_MMAP |
420                                  SNDRV_PCM_INFO_MMAP_VALID |
421                                  SNDRV_PCM_INFO_RESUME),
422         .formats =              SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
423         .rates =                SNDRV_PCM_RATE_8000_44100,
424         .rate_min =             7350,
425         .rate_max =             44100,
426         .channels_min =         2,
427         .channels_max =         2,
428         .buffer_bytes_max =     32768,
429         .period_bytes_min =     256,
430         .period_bytes_max =     16384,
431         .periods_min =          1,
432         .periods_max =          PMAC_MAX_FRAGS,
433 };
434
435 static snd_pcm_hardware_t snd_pmac_capture =
436 {
437         .info =                 (SNDRV_PCM_INFO_INTERLEAVED |
438                                  SNDRV_PCM_INFO_MMAP |
439                                  SNDRV_PCM_INFO_MMAP_VALID |
440                                  SNDRV_PCM_INFO_RESUME),
441         .formats =              SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
442         .rates =                SNDRV_PCM_RATE_8000_44100,
443         .rate_min =             7350,
444         .rate_max =             44100,
445         .channels_min =         2,
446         .channels_max =         2,
447         .buffer_bytes_max =     32768,
448         .period_bytes_min =     256,
449         .period_bytes_max =     16384,
450         .periods_min =          1,
451         .periods_max =          PMAC_MAX_FRAGS,
452 };
453
454
455 #if 0 // NYI
456 static int snd_pmac_hw_rule_rate(snd_pcm_hw_params_t *params,
457                                  snd_pcm_hw_rule_t *rule)
458 {
459         pmac_t *chip = rule->private;
460         pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
461         int i, freq_table[8], num_freqs;
462
463         snd_runtime_check(rec, return -EINVAL);
464         num_freqs = 0;
465         for (i = chip->num_freqs - 1; i >= 0; i--) {
466                 if (rec->cur_freqs & (1 << i))
467                         freq_table[num_freqs++] = chip->freq_table[i];
468         }
469
470         return snd_interval_list(hw_param_interval(params, rule->var),
471                                  num_freqs, freq_table, 0);
472 }
473
474 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
475                                    snd_pcm_hw_rule_t *rule)
476 {
477         pmac_t *chip = rule->private;
478         pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
479
480         snd_runtime_check(rec, return -EINVAL);
481         return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
482                                    rec->cur_formats);
483 }
484 #endif // NYI
485
486 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
487 {
488         snd_pcm_runtime_t *runtime = subs->runtime;
489         int i, j, fflags;
490         static int typical_freqs[] = {
491                 44100,
492                 22050,
493                 11025,
494                 0,
495         };
496         static int typical_freq_flags[] = {
497                 SNDRV_PCM_RATE_44100,
498                 SNDRV_PCM_RATE_22050,
499                 SNDRV_PCM_RATE_11025,
500                 0,
501         };
502
503         /* look up frequency table and fill bit mask */
504         runtime->hw.rates = 0;
505         fflags = chip->freqs_ok;
506         for (i = 0; typical_freqs[i]; i++) {
507                 for (j = 0; j < chip->num_freqs; j++) {
508                         if ((chip->freqs_ok & (1 << j)) &&
509                             chip->freq_table[j] == typical_freqs[i]) {
510                                 runtime->hw.rates |= typical_freq_flags[i];
511                                 fflags &= ~(1 << j);
512                                 break;
513                         }
514                 }
515         }
516         if (fflags) /* rest */
517                 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
518
519         /* check for minimum and maximum rates */
520         for (i = 0; i < chip->num_freqs; i++) {
521                 if (chip->freqs_ok & (1 << i)) {
522                         runtime->hw.rate_max = chip->freq_table[i];
523                         break;
524                 }
525         }
526         for (i = chip->num_freqs - 1; i >= 0; i--) {
527                 if (chip->freqs_ok & (1 << i)) {
528                         runtime->hw.rate_min = chip->freq_table[i];
529                         break;
530                 }
531         }
532         runtime->hw.formats = chip->formats_ok;
533         if (chip->can_capture) {
534                 if (! chip->can_duplex)
535                         runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
536                 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
537         }
538         runtime->private_data = rec;
539         rec->substream = subs;
540
541 #if 0 /* FIXME: still under development.. */
542         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
543                             snd_pmac_hw_rule_rate, chip, rec->stream, -1);
544         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
545                             snd_pmac_hw_rule_format, chip, rec->stream, -1);
546 #endif
547
548         runtime->hw.periods_max = rec->cmd.size - 1;
549
550         if (chip->can_duplex)
551                 snd_pcm_set_sync(subs);
552
553         return 0;
554 }
555
556 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
557 {
558         pmac_stream_t *astr;
559
560         snd_pmac_dma_stop(rec);
561
562         astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
563         snd_runtime_check(astr, return -EINVAL);
564
565         /* reset constraints */
566         astr->cur_freqs = chip->freqs_ok;
567         astr->cur_formats = chip->formats_ok;
568         
569         return 0;
570 }
571
572 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
573 {
574         pmac_t *chip = snd_pcm_substream_chip(subs);
575
576         subs->runtime->hw = snd_pmac_playback;
577         return snd_pmac_pcm_open(chip, &chip->playback, subs);
578 }
579
580 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
581 {
582         pmac_t *chip = snd_pcm_substream_chip(subs);
583
584         subs->runtime->hw = snd_pmac_capture;
585         return snd_pmac_pcm_open(chip, &chip->capture, subs);
586 }
587
588 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
589 {
590         pmac_t *chip = snd_pcm_substream_chip(subs);
591
592         return snd_pmac_pcm_close(chip, &chip->playback, subs);
593 }
594
595 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
596 {
597         pmac_t *chip = snd_pcm_substream_chip(subs);
598
599         return snd_pmac_pcm_close(chip, &chip->capture, subs);
600 }
601
602 /*
603  */
604
605 static snd_pcm_ops_t snd_pmac_playback_ops = {
606         .open =         snd_pmac_playback_open,
607         .close =        snd_pmac_playback_close,
608         .ioctl =        snd_pcm_lib_ioctl,
609         .hw_params =    snd_pmac_pcm_hw_params,
610         .hw_free =      snd_pmac_pcm_hw_free,
611         .prepare =      snd_pmac_playback_prepare,
612         .trigger =      snd_pmac_playback_trigger,
613         .pointer =      snd_pmac_playback_pointer,
614 };
615
616 static snd_pcm_ops_t snd_pmac_capture_ops = {
617         .open =         snd_pmac_capture_open,
618         .close =        snd_pmac_capture_close,
619         .ioctl =        snd_pcm_lib_ioctl,
620         .hw_params =    snd_pmac_pcm_hw_params,
621         .hw_free =      snd_pmac_pcm_hw_free,
622         .prepare =      snd_pmac_capture_prepare,
623         .trigger =      snd_pmac_capture_trigger,
624         .pointer =      snd_pmac_capture_pointer,
625 };
626
627 static void pmac_pcm_free(snd_pcm_t *pcm)
628 {
629         snd_pcm_lib_preallocate_free_for_all(pcm);
630 }
631
632 int __init snd_pmac_pcm_new(pmac_t *chip)
633 {
634         snd_pcm_t *pcm;
635         int err;
636         int num_captures = 1;
637
638         if (! chip->can_capture)
639                 num_captures = 0;
640         err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
641         if (err < 0)
642                 return err;
643
644         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
645         if (chip->can_capture)
646                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
647
648         pcm->private_data = chip;
649         pcm->private_free = pmac_pcm_free;
650         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
651         strcpy(pcm->name, chip->card->shortname);
652         chip->pcm = pcm;
653
654         chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
655         if (chip->can_byte_swap)
656                 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
657
658         chip->playback.cur_formats = chip->formats_ok;
659         chip->capture.cur_formats = chip->formats_ok;
660         chip->playback.cur_freqs = chip->freqs_ok;
661         chip->capture.cur_freqs = chip->freqs_ok;
662
663         /* preallocate 64k buffer */
664         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 
665                                               snd_dma_continuous_data(GFP_KERNEL),
666                                               64 * 1024, 64 * 1024);
667
668         return 0;
669 }
670
671
672 static void snd_pmac_dbdma_reset(pmac_t *chip)
673 {
674         out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
675         snd_pmac_wait_ack(&chip->playback);
676         out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
677         snd_pmac_wait_ack(&chip->capture);
678 }
679
680
681 /*
682  * handling beep
683  */
684 void snd_pmac_beep_dma_start(pmac_t *chip, int bytes, unsigned long addr, int speed)
685 {
686         pmac_stream_t *rec = &chip->playback;
687
688         snd_pmac_dma_stop(rec);
689         st_le16(&chip->extra_dma.cmds->req_count, bytes);
690         st_le16(&chip->extra_dma.cmds->xfer_status, 0);
691         st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
692         st_le32(&chip->extra_dma.cmds->phy_addr, addr);
693         st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
694         out_le32(&chip->awacs->control,
695                  (in_le32(&chip->awacs->control) & ~0x1f00)
696                  | (speed << 8));
697         out_le32(&chip->awacs->byteswap, 0);
698         snd_pmac_dma_set_command(rec, &chip->extra_dma);
699         snd_pmac_dma_run(rec, RUN);
700 }
701
702 void snd_pmac_beep_dma_stop(pmac_t *chip)
703 {
704         snd_pmac_dma_stop(&chip->playback);
705         st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
706         snd_pmac_pcm_set_format(chip); /* reset format */
707 }
708
709
710 /*
711  * interrupt handlers
712  */
713 static irqreturn_t
714 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
715 {
716         pmac_t *chip = devid;
717         snd_pmac_pcm_update(chip, &chip->playback);
718         return IRQ_HANDLED;
719 }
720
721
722 static irqreturn_t
723 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
724 {
725         pmac_t *chip = devid;
726         snd_pmac_pcm_update(chip, &chip->capture);
727         return IRQ_HANDLED;
728 }
729
730
731 static irqreturn_t
732 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
733 {
734         pmac_t *chip = devid;
735         int ctrl = in_le32(&chip->awacs->control);
736
737         /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
738         if (ctrl & MASK_PORTCHG) {
739                 /* do something when headphone is plugged/unplugged? */
740                 if (chip->update_automute)
741                         chip->update_automute(chip, 1);
742         }
743         if (ctrl & MASK_CNTLERR) {
744                 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
745                 if (err && chip->model <= PMAC_SCREAMER)
746                         snd_printk(KERN_DEBUG "error %x\n", err);
747         }
748         /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
749         out_le32(&chip->awacs->control, ctrl);
750         return IRQ_HANDLED;
751 }
752
753
754 /*
755  * a wrapper to feature call for compatibility
756  */
757 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
758 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
759 {
760 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
761         ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
762 #else
763         if (chip->is_pbook_G3) {
764                 pmu_suspend();
765                 feature_clear(chip->node, FEATURE_Sound_power);
766                 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
767                 big_mdelay(1000); /* XXX */
768                 pmu_resume();
769         }
770         if (chip->is_pbook_3400) {
771                 feature_set(chip->node, FEATURE_IOBUS_enable);
772                 udelay(10);
773         }
774 #endif
775 }
776 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
777 #define snd_pmac_sound_feature(chip,enable) /**/
778 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
779
780 /*
781  * release resources
782  */
783
784 static int snd_pmac_free(pmac_t *chip)
785 {
786         int i;
787
788         /* stop sounds */
789         if (chip->initialized) {
790                 snd_pmac_dbdma_reset(chip);
791                 /* disable interrupts from awacs interface */
792                 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
793         }
794
795         snd_pmac_sound_feature(chip, 0);
796 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
797         snd_pmac_unregister_sleep_notifier(chip);
798 #endif
799
800         /* clean up mixer if any */
801         if (chip->mixer_free)
802                 chip->mixer_free(chip);
803
804         snd_pmac_detach_beep(chip);
805
806         /* release resources */
807         if (chip->irq >= 0)
808                 free_irq(chip->irq, (void*)chip);
809         if (chip->tx_irq >= 0)
810                 free_irq(chip->tx_irq, (void*)chip);
811         if (chip->rx_irq >= 0)
812                 free_irq(chip->rx_irq, (void*)chip);
813         snd_pmac_dbdma_free(&chip->playback.cmd);
814         snd_pmac_dbdma_free(&chip->capture.cmd);
815         snd_pmac_dbdma_free(&chip->extra_dma);
816         if (chip->macio_base)
817                 iounmap(chip->macio_base);
818         if (chip->latch_base)
819                 iounmap(chip->latch_base);
820         if (chip->awacs)
821                 iounmap((void*)chip->awacs);
822         if (chip->playback.dma)
823                 iounmap((void*)chip->playback.dma);
824         if (chip->capture.dma)
825                 iounmap((void*)chip->capture.dma);
826         if (chip->node) {
827                 for (i = 0; i < 3; i++) {
828                         if (chip->of_requested & (1 << i))
829                                 release_OF_resource(chip->node, i);
830                 }
831         }
832         kfree(chip);
833         return 0;
834 }
835
836
837 /*
838  * free the device
839  */
840 static int snd_pmac_dev_free(snd_device_t *device)
841 {
842         pmac_t *chip = device->device_data;
843         return snd_pmac_free(chip);
844 }
845
846
847 /*
848  * check the machine support byteswap (little-endian)
849  */
850
851 static void __init detect_byte_swap(pmac_t *chip)
852 {
853         struct device_node *mio;
854
855         /* if seems that Keylargo can't byte-swap  */
856         for (mio = chip->node->parent; mio; mio = mio->parent) {
857                 if (strcmp(mio->name, "mac-io") == 0) {
858                         if (device_is_compatible(mio, "Keylargo"))
859                                 chip->can_byte_swap = 0;
860                         break;
861                 }
862         }
863
864         /* it seems the Pismo & iBook can't byte-swap in hardware. */
865         if (machine_is_compatible("PowerBook3,1") ||
866             machine_is_compatible("PowerBook2,1"))
867                 chip->can_byte_swap = 0 ;
868
869         if (machine_is_compatible("PowerBook2,1"))
870                 chip->can_duplex = 0;
871 }
872
873
874 /*
875  * detect a sound chip
876  */
877 static int __init snd_pmac_detect(pmac_t *chip)
878 {
879         struct device_node *sound;
880         unsigned int *prop, l;
881
882         if (_machine != _MACH_Pmac)
883                 return -ENODEV;
884
885         chip->subframe = 0;
886         chip->revision = 0;
887         chip->freqs_ok = 0xff; /* all ok */
888         chip->model = PMAC_AWACS;
889         chip->can_byte_swap = 1;
890         chip->can_duplex = 1;
891         chip->can_capture = 1;
892         chip->num_freqs = ARRAY_SIZE(awacs_freqs);
893         chip->freq_table = awacs_freqs;
894
895         chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
896
897         /* check machine type */
898         if (machine_is_compatible("AAPL,3400/2400")
899             || machine_is_compatible("AAPL,3500"))
900                 chip->is_pbook_3400 = 1;
901         else if (machine_is_compatible("PowerBook1,1")
902                  || machine_is_compatible("AAPL,PowerBook1998"))
903                 chip->is_pbook_G3 = 1;
904         chip->node = find_devices("awacs");
905         if (chip->node)
906                 return 0; /* ok */
907
908         /*
909          * powermac G3 models have a node called "davbus"
910          * with a child called "sound".
911          */
912         chip->node = find_devices("davbus");
913         /*
914          * if we didn't find a davbus device, try 'i2s-a' since
915          * this seems to be what iBooks have
916          */
917         if (! chip->node)
918                 chip->node = find_devices("i2s-a");
919         if (! chip->node)
920                 return -ENODEV;
921         sound = find_devices("sound");
922         while (sound && sound->parent != chip->node)
923                 sound = sound->next;
924         if (! sound)
925                 return -ENODEV;
926         prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
927         if (prop && *prop < 16)
928                 chip->subframe = *prop;
929         /* This should be verified on older screamers */
930         if (device_is_compatible(sound, "screamer")) {
931                 chip->model = PMAC_SCREAMER;
932                 // chip->can_byte_swap = 0; /* FIXME: check this */
933         }
934         if (device_is_compatible(sound, "burgundy")) {
935                 chip->model = PMAC_BURGUNDY;
936                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
937         }
938         if (device_is_compatible(sound, "daca")) {
939                 chip->model = PMAC_DACA;
940                 chip->can_capture = 0;  /* no capture */
941                 chip->can_duplex = 0;
942                 // chip->can_byte_swap = 0; /* FIXME: check this */
943                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
944         }
945         if (device_is_compatible(sound, "tumbler")) {
946                 chip->model = PMAC_TUMBLER;
947                 chip->can_capture = 0;  /* no capture */
948                 chip->can_duplex = 0;
949                 // chip->can_byte_swap = 0; /* FIXME: check this */
950                 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
951                 chip->freq_table = tumbler_freqs;
952                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
953         }
954         if (device_is_compatible(sound, "snapper")) {
955                 chip->model = PMAC_SNAPPER;
956                 // chip->can_byte_swap = 0; /* FIXME: check this */
957                 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
958                 chip->freq_table = tumbler_freqs;
959                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
960         }
961         if (device_is_compatible(sound, "AOAKeylargo")) {
962                 /* Seems to support the stock AWACS frequencies, but has
963                    a snapper mixer */
964                 chip->model = PMAC_SNAPPER;
965                 // chip->can_byte_swap = 0; /* FIXME: check this */
966                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
967         }
968         prop = (unsigned int *)get_property(sound, "device-id", NULL);
969         if (prop)
970                 chip->device_id = *prop;
971         chip->has_iic = (find_devices("perch") != NULL);
972
973         detect_byte_swap(chip);
974
975         /* look for a property saying what sample rates
976            are available */
977         prop = (unsigned int *) get_property(sound, "sample-rates", &l);
978         if (! prop)
979                 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
980         if (prop) {
981                 int i;
982                 chip->freqs_ok = 0;
983                 for (l /= sizeof(int); l > 0; --l) {
984                         unsigned int r = *prop++;
985                         /* Apple 'Fixed' format */
986                         if (r >= 0x10000)
987                                 r >>= 16;
988                         for (i = 0; i < chip->num_freqs; ++i) {
989                                 if (r == chip->freq_table[i]) {
990                                         chip->freqs_ok |= (1 << i);
991                                         break;
992                                 }
993                         }
994                 }
995         } else {
996                 /* assume only 44.1khz */
997                 chip->freqs_ok = 1;
998         }
999
1000         return 0;
1001 }
1002
1003 /*
1004  * exported - boolean info callbacks for ease of programming
1005  */
1006 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1007 {
1008         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1009         uinfo->count = 2;
1010         uinfo->value.integer.min = 0;
1011         uinfo->value.integer.max = 1;
1012         return 0;
1013 }
1014
1015 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1016 {
1017         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1018         uinfo->count = 1;
1019         uinfo->value.integer.min = 0;
1020         uinfo->value.integer.max = 1;
1021         return 0;
1022 }
1023
1024 #ifdef PMAC_SUPPORT_AUTOMUTE
1025 /*
1026  * auto-mute
1027  */
1028 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1029 {
1030         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1031         ucontrol->value.integer.value[0] = chip->auto_mute;
1032         return 0;
1033 }
1034
1035 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1036 {
1037         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1038         if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1039                 chip->auto_mute = ucontrol->value.integer.value[0];
1040                 if (chip->update_automute)
1041                         chip->update_automute(chip, 1);
1042                 return 1;
1043         }
1044         return 0;
1045 }
1046
1047 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1048 {
1049         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1050         if (chip->detect_headphone)
1051                 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1052         else
1053                 ucontrol->value.integer.value[0] = 0;
1054         return 0;
1055 }
1056
1057 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1058         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1059           .name = "Auto Mute Switch",
1060           .info = snd_pmac_boolean_mono_info,
1061           .get = pmac_auto_mute_get,
1062           .put = pmac_auto_mute_put,
1063         },
1064         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1065           .name = "Headphone Detection",
1066           .access = SNDRV_CTL_ELEM_ACCESS_READ,
1067           .info = snd_pmac_boolean_mono_info,
1068           .get = pmac_hp_detect_get,
1069         },
1070 };
1071
1072 int __init snd_pmac_add_automute(pmac_t *chip)
1073 {
1074         int err;
1075         chip->auto_mute = 1;
1076         err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1077         if (err < 0)
1078                 return err;
1079         chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1080         return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1081 }
1082 #endif /* PMAC_SUPPORT_AUTOMUTE */
1083
1084 /*
1085  * create and detect a pmac chip record
1086  */
1087 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1088 {
1089         pmac_t *chip;
1090         struct device_node *np;
1091         int i, err;
1092         static snd_device_ops_t ops = {
1093                 .dev_free =     snd_pmac_dev_free,
1094         };
1095
1096         snd_runtime_check(chip_return, return -EINVAL);
1097         *chip_return = NULL;
1098
1099         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1100         if (chip == NULL)
1101                 return -ENOMEM;
1102         chip->card = card;
1103
1104         spin_lock_init(&chip->reg_lock);
1105         chip->irq = chip->tx_irq = chip->rx_irq = -1;
1106
1107         chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1108         chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1109
1110         if ((err = snd_pmac_detect(chip)) < 0)
1111                 goto __error;
1112
1113         if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1114             snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1115             snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1116                 err = -ENOMEM;
1117                 goto __error;
1118         }
1119
1120         np = chip->node;
1121         if (np->n_addrs < 3 || np->n_intrs < 3) {
1122                 err = -ENODEV;
1123                 goto __error;
1124         }
1125
1126         for (i = 0; i < 3; i++) {
1127                 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1128                 if (! request_OF_resource(np, i, name[i])) {
1129                         snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1130                         err = -ENODEV;
1131                         goto __error;
1132                 }
1133                 chip->of_requested |= (1 << i);
1134         }
1135
1136         chip->awacs = (volatile struct awacs_regs *) ioremap(np->addrs[0].address, 0x1000);
1137         chip->playback.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[1].address, 0x100);
1138         chip->capture.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[2].address, 0x100);
1139         if (chip->model <= PMAC_BURGUNDY) {
1140                 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1141                                 "PMac", (void*)chip)) {
1142                         snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1143                         err = -EBUSY;
1144                         goto __error;
1145                 }
1146                 chip->irq = np->intrs[0].line;
1147         }
1148         if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1149                         "PMac Output", (void*)chip)) {
1150                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1151                 err = -EBUSY;
1152                 goto __error;
1153         }
1154         chip->tx_irq = np->intrs[1].line;
1155         if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1156                         "PMac Input", (void*)chip)) {
1157                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1158                 err = -EBUSY;
1159                 goto __error;
1160         }
1161         chip->rx_irq = np->intrs[2].line;
1162
1163         snd_pmac_sound_feature(chip, 1);
1164
1165         /* reset */
1166         out_le32(&chip->awacs->control, 0x11);
1167
1168         /* Powerbooks have odd ways of enabling inputs such as
1169            an expansion-bay CD or sound from an internal modem
1170            or a PC-card modem. */
1171         if (chip->is_pbook_3400) {
1172                 /* Enable CD and PC-card sound inputs. */
1173                 /* This is done by reading from address
1174                  * f301a000, + 0x10 to enable the expansion-bay
1175                  * CD sound input, + 0x80 to enable the PC-card
1176                  * sound input.  The 0x100 enables the SCSI bus
1177                  * terminator power.
1178                  */
1179                 chip->latch_base = (unsigned char *) ioremap (0xf301a000, 0x1000);
1180                 in_8(chip->latch_base + 0x190);
1181         } else if (chip->is_pbook_G3) {
1182                 struct device_node* mio;
1183                 for (mio = chip->node->parent; mio; mio = mio->parent) {
1184                         if (strcmp(mio->name, "mac-io") == 0
1185                             && mio->n_addrs > 0) {
1186                                 chip->macio_base = (unsigned char *) ioremap
1187                                         (mio->addrs[0].address, 0x40);
1188                                 break;
1189                         }
1190                 }
1191                 /* Enable CD sound input. */
1192                 /* The relevant bits for writing to this byte are 0x8f.
1193                  * I haven't found out what the 0x80 bit does.
1194                  * For the 0xf bits, writing 3 or 7 enables the CD
1195                  * input, any other value disables it.  Values
1196                  * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1197                  * 4, 6, 8 - f enable the input from the modem.
1198                  */
1199                 if (chip->macio_base)
1200                         out_8(chip->macio_base + 0x37, 3);
1201         }
1202
1203         /* Reset dbdma channels */
1204         snd_pmac_dbdma_reset(chip);
1205
1206 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1207         /* add sleep notifier */
1208         if (! snd_pmac_register_sleep_notifier(chip))
1209                 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1210 #endif
1211
1212         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1213                 goto __error;
1214
1215         *chip_return = chip;
1216         return 0;
1217
1218  __error:
1219         snd_pmac_free(chip);
1220         return err;
1221 }
1222
1223
1224 /*
1225  * sleep notify for powerbook
1226  */
1227
1228 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1229
1230 /*
1231  * Save state when going to sleep, restore it afterwards.
1232  */
1233
1234 static int snd_pmac_suspend(snd_card_t *card, unsigned int state)
1235 {
1236         pmac_t *chip = card->pm_private_data;
1237         unsigned long flags;
1238
1239         if (chip->suspend)
1240                 chip->suspend(chip);
1241         snd_pcm_suspend_all(chip->pcm);
1242         spin_lock_irqsave(&chip->reg_lock, flags);
1243         snd_pmac_beep_stop(chip);
1244         spin_unlock_irqrestore(&chip->reg_lock, flags);
1245         if (chip->irq >= 0)
1246                 disable_irq(chip->irq);
1247         if (chip->tx_irq >= 0)
1248                 disable_irq(chip->tx_irq);
1249         if (chip->rx_irq >= 0)
1250                 disable_irq(chip->rx_irq);
1251         snd_pmac_sound_feature(chip, 0);
1252         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1253         return 0;
1254 }
1255
1256 static int snd_pmac_resume(snd_card_t *card, unsigned int state)
1257 {
1258         pmac_t *chip = card->pm_private_data;
1259
1260         snd_pmac_sound_feature(chip, 1);
1261         if (chip->resume)
1262                 chip->resume(chip);
1263         /* enable CD sound input */
1264         if (chip->macio_base && chip->is_pbook_G3) {
1265                 out_8(chip->macio_base + 0x37, 3);
1266         } else if (chip->is_pbook_3400) {
1267                 in_8(chip->latch_base + 0x190);
1268         }
1269
1270         snd_pmac_pcm_set_format(chip);
1271
1272         if (chip->irq >= 0)
1273                 enable_irq(chip->irq);
1274         if (chip->tx_irq >= 0)
1275                 enable_irq(chip->tx_irq);
1276         if (chip->rx_irq >= 0)
1277                 enable_irq(chip->rx_irq);
1278
1279         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1280         return 0;
1281 }
1282
1283 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1284  * because we can't have any private data for notify callback.
1285  */
1286 static pmac_t *sleeping_pmac = NULL;
1287
1288 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1289 {
1290         pmac_t *chip;
1291
1292         chip = sleeping_pmac;
1293         snd_runtime_check(chip, return 0);
1294
1295         switch (when) {
1296         case PBOOK_SLEEP_NOW:
1297                 snd_pmac_suspend(chip->card, 0);
1298                 break;
1299         case PBOOK_WAKE:
1300                 snd_pmac_resume(chip->card, 0);
1301                 break;
1302         }
1303         return PBOOK_SLEEP_OK;
1304 }
1305
1306 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1307         snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1308 };
1309
1310 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1311 {
1312         /* should be protected here.. */
1313         snd_assert(! sleeping_pmac, return -EBUSY);
1314         sleeping_pmac = chip;
1315         pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1316         return 0;
1317 }
1318                                                     
1319 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1320 {
1321         /* should be protected here.. */
1322         snd_assert(sleeping_pmac == chip, return -ENODEV);
1323         pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1324         sleeping_pmac = NULL;
1325         return 0;
1326 }
1327
1328 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */