[PATCH] pmac: sound support for latest laptops
[safe/jmp/linux-2.6] / 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, pm_message_t state);
44 static int snd_pmac_resume(snd_card_t *card);
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 __iomem *)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)
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 __iomem *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 __iomem *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 __iomem *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                 if (count)
322                         count = rec->period_size - count;
323         }
324 #endif
325         count += rec->cur_period * rec->period_size;
326         /*printk("pointer=%d\n", count);*/
327         return bytes_to_frames(subs->runtime, count);
328 }
329
330 /*
331  * playback
332  */
333
334 static int snd_pmac_playback_prepare(snd_pcm_substream_t *subs)
335 {
336         pmac_t *chip = snd_pcm_substream_chip(subs);
337         return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
338 }
339
340 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
341                                      int cmd)
342 {
343         pmac_t *chip = snd_pcm_substream_chip(subs);
344         return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
345 }
346
347 static snd_pcm_uframes_t snd_pmac_playback_pointer(snd_pcm_substream_t *subs)
348 {
349         pmac_t *chip = snd_pcm_substream_chip(subs);
350         return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
351 }
352
353
354 /*
355  * capture
356  */
357
358 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
359 {
360         pmac_t *chip = snd_pcm_substream_chip(subs);
361         return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
362 }
363
364 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
365                                     int cmd)
366 {
367         pmac_t *chip = snd_pcm_substream_chip(subs);
368         return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
369 }
370
371 static snd_pcm_uframes_t snd_pmac_capture_pointer(snd_pcm_substream_t *subs)
372 {
373         pmac_t *chip = snd_pcm_substream_chip(subs);
374         return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
375 }
376
377
378 /*
379  * update playback/capture pointer from interrupts
380  */
381 static void snd_pmac_pcm_update(pmac_t *chip, pmac_stream_t *rec)
382 {
383         volatile struct dbdma_cmd __iomem *cp;
384         int c;
385         int stat;
386
387         spin_lock(&chip->reg_lock);
388         if (rec->running) {
389                 cp = &rec->cmd.cmds[rec->cur_period];
390                 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
391                         stat = ld_le16(&cp->xfer_status);
392                         if (! (stat & ACTIVE))
393                                 break;
394                         /*printk("update frag %d\n", rec->cur_period);*/
395                         st_le16(&cp->xfer_status, 0);
396                         st_le16(&cp->req_count, rec->period_size);
397                         /*st_le16(&cp->res_count, 0);*/
398                         rec->cur_period++;
399                         if (rec->cur_period >= rec->nperiods) {
400                                 rec->cur_period = 0;
401                                 cp = rec->cmd.cmds;
402                         } else
403                                 cp++;
404                         spin_unlock(&chip->reg_lock);
405                         snd_pcm_period_elapsed(rec->substream);
406                         spin_lock(&chip->reg_lock);
407                 }
408         }
409         spin_unlock(&chip->reg_lock);
410 }
411
412
413 /*
414  * hw info
415  */
416
417 static snd_pcm_hardware_t snd_pmac_playback =
418 {
419         .info =                 (SNDRV_PCM_INFO_INTERLEAVED |
420                                  SNDRV_PCM_INFO_MMAP |
421                                  SNDRV_PCM_INFO_MMAP_VALID |
422                                  SNDRV_PCM_INFO_RESUME),
423         .formats =              SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
424         .rates =                SNDRV_PCM_RATE_8000_44100,
425         .rate_min =             7350,
426         .rate_max =             44100,
427         .channels_min =         2,
428         .channels_max =         2,
429         .buffer_bytes_max =     131072,
430         .period_bytes_min =     256,
431         .period_bytes_max =     16384,
432         .periods_min =          3,
433         .periods_max =          PMAC_MAX_FRAGS,
434 };
435
436 static snd_pcm_hardware_t snd_pmac_capture =
437 {
438         .info =                 (SNDRV_PCM_INFO_INTERLEAVED |
439                                  SNDRV_PCM_INFO_MMAP |
440                                  SNDRV_PCM_INFO_MMAP_VALID |
441                                  SNDRV_PCM_INFO_RESUME),
442         .formats =              SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
443         .rates =                SNDRV_PCM_RATE_8000_44100,
444         .rate_min =             7350,
445         .rate_max =             44100,
446         .channels_min =         2,
447         .channels_max =         2,
448         .buffer_bytes_max =     131072,
449         .period_bytes_min =     256,
450         .period_bytes_max =     16384,
451         .periods_min =          3,
452         .periods_max =          PMAC_MAX_FRAGS,
453 };
454
455
456 #if 0 // NYI
457 static int snd_pmac_hw_rule_rate(snd_pcm_hw_params_t *params,
458                                  snd_pcm_hw_rule_t *rule)
459 {
460         pmac_t *chip = rule->private;
461         pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
462         int i, freq_table[8], num_freqs;
463
464         snd_runtime_check(rec, return -EINVAL);
465         num_freqs = 0;
466         for (i = chip->num_freqs - 1; i >= 0; i--) {
467                 if (rec->cur_freqs & (1 << i))
468                         freq_table[num_freqs++] = chip->freq_table[i];
469         }
470
471         return snd_interval_list(hw_param_interval(params, rule->var),
472                                  num_freqs, freq_table, 0);
473 }
474
475 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
476                                    snd_pcm_hw_rule_t *rule)
477 {
478         pmac_t *chip = rule->private;
479         pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
480
481         snd_runtime_check(rec, return -EINVAL);
482         return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
483                                    rec->cur_formats);
484 }
485 #endif // NYI
486
487 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
488 {
489         snd_pcm_runtime_t *runtime = subs->runtime;
490         int i, j, fflags;
491         static int typical_freqs[] = {
492                 44100,
493                 22050,
494                 11025,
495                 0,
496         };
497         static int typical_freq_flags[] = {
498                 SNDRV_PCM_RATE_44100,
499                 SNDRV_PCM_RATE_22050,
500                 SNDRV_PCM_RATE_11025,
501                 0,
502         };
503
504         /* look up frequency table and fill bit mask */
505         runtime->hw.rates = 0;
506         fflags = chip->freqs_ok;
507         for (i = 0; typical_freqs[i]; i++) {
508                 for (j = 0; j < chip->num_freqs; j++) {
509                         if ((chip->freqs_ok & (1 << j)) &&
510                             chip->freq_table[j] == typical_freqs[i]) {
511                                 runtime->hw.rates |= typical_freq_flags[i];
512                                 fflags &= ~(1 << j);
513                                 break;
514                         }
515                 }
516         }
517         if (fflags) /* rest */
518                 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
519
520         /* check for minimum and maximum rates */
521         for (i = 0; i < chip->num_freqs; i++) {
522                 if (chip->freqs_ok & (1 << i)) {
523                         runtime->hw.rate_max = chip->freq_table[i];
524                         break;
525                 }
526         }
527         for (i = chip->num_freqs - 1; i >= 0; i--) {
528                 if (chip->freqs_ok & (1 << i)) {
529                         runtime->hw.rate_min = chip->freq_table[i];
530                         break;
531                 }
532         }
533         runtime->hw.formats = chip->formats_ok;
534         if (chip->can_capture) {
535                 if (! chip->can_duplex)
536                         runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
537                 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
538         }
539         runtime->private_data = rec;
540         rec->substream = subs;
541
542 #if 0 /* FIXME: still under development.. */
543         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
544                             snd_pmac_hw_rule_rate, chip, rec->stream, -1);
545         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
546                             snd_pmac_hw_rule_format, chip, rec->stream, -1);
547 #endif
548
549         runtime->hw.periods_max = rec->cmd.size - 1;
550
551         if (chip->can_duplex)
552                 snd_pcm_set_sync(subs);
553
554         /* constraints to fix choppy sound */
555         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
556         return 0;
557 }
558
559 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
560 {
561         pmac_stream_t *astr;
562
563         snd_pmac_dma_stop(rec);
564
565         astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
566         snd_runtime_check(astr, return -EINVAL);
567
568         /* reset constraints */
569         astr->cur_freqs = chip->freqs_ok;
570         astr->cur_formats = chip->formats_ok;
571         
572         return 0;
573 }
574
575 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
576 {
577         pmac_t *chip = snd_pcm_substream_chip(subs);
578
579         subs->runtime->hw = snd_pmac_playback;
580         return snd_pmac_pcm_open(chip, &chip->playback, subs);
581 }
582
583 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
584 {
585         pmac_t *chip = snd_pcm_substream_chip(subs);
586
587         subs->runtime->hw = snd_pmac_capture;
588         return snd_pmac_pcm_open(chip, &chip->capture, subs);
589 }
590
591 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
592 {
593         pmac_t *chip = snd_pcm_substream_chip(subs);
594
595         return snd_pmac_pcm_close(chip, &chip->playback, subs);
596 }
597
598 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
599 {
600         pmac_t *chip = snd_pcm_substream_chip(subs);
601
602         return snd_pmac_pcm_close(chip, &chip->capture, subs);
603 }
604
605 /*
606  */
607
608 static snd_pcm_ops_t snd_pmac_playback_ops = {
609         .open =         snd_pmac_playback_open,
610         .close =        snd_pmac_playback_close,
611         .ioctl =        snd_pcm_lib_ioctl,
612         .hw_params =    snd_pmac_pcm_hw_params,
613         .hw_free =      snd_pmac_pcm_hw_free,
614         .prepare =      snd_pmac_playback_prepare,
615         .trigger =      snd_pmac_playback_trigger,
616         .pointer =      snd_pmac_playback_pointer,
617 };
618
619 static snd_pcm_ops_t snd_pmac_capture_ops = {
620         .open =         snd_pmac_capture_open,
621         .close =        snd_pmac_capture_close,
622         .ioctl =        snd_pcm_lib_ioctl,
623         .hw_params =    snd_pmac_pcm_hw_params,
624         .hw_free =      snd_pmac_pcm_hw_free,
625         .prepare =      snd_pmac_capture_prepare,
626         .trigger =      snd_pmac_capture_trigger,
627         .pointer =      snd_pmac_capture_pointer,
628 };
629
630 static void pmac_pcm_free(snd_pcm_t *pcm)
631 {
632         snd_pcm_lib_preallocate_free_for_all(pcm);
633 }
634
635 int __init snd_pmac_pcm_new(pmac_t *chip)
636 {
637         snd_pcm_t *pcm;
638         int err;
639         int num_captures = 1;
640
641         if (! chip->can_capture)
642                 num_captures = 0;
643         err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
644         if (err < 0)
645                 return err;
646
647         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
648         if (chip->can_capture)
649                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
650
651         pcm->private_data = chip;
652         pcm->private_free = pmac_pcm_free;
653         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
654         strcpy(pcm->name, chip->card->shortname);
655         chip->pcm = pcm;
656
657         chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
658         if (chip->can_byte_swap)
659                 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
660
661         chip->playback.cur_formats = chip->formats_ok;
662         chip->capture.cur_formats = chip->formats_ok;
663         chip->playback.cur_freqs = chip->freqs_ok;
664         chip->capture.cur_freqs = chip->freqs_ok;
665
666         /* preallocate 64k buffer */
667         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 
668                                               snd_dma_continuous_data(GFP_KERNEL),
669                                               64 * 1024, 64 * 1024);
670
671         return 0;
672 }
673
674
675 static void snd_pmac_dbdma_reset(pmac_t *chip)
676 {
677         out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
678         snd_pmac_wait_ack(&chip->playback);
679         out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
680         snd_pmac_wait_ack(&chip->capture);
681 }
682
683
684 /*
685  * handling beep
686  */
687 void snd_pmac_beep_dma_start(pmac_t *chip, int bytes, unsigned long addr, int speed)
688 {
689         pmac_stream_t *rec = &chip->playback;
690
691         snd_pmac_dma_stop(rec);
692         st_le16(&chip->extra_dma.cmds->req_count, bytes);
693         st_le16(&chip->extra_dma.cmds->xfer_status, 0);
694         st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
695         st_le32(&chip->extra_dma.cmds->phy_addr, addr);
696         st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
697         out_le32(&chip->awacs->control,
698                  (in_le32(&chip->awacs->control) & ~0x1f00)
699                  | (speed << 8));
700         out_le32(&chip->awacs->byteswap, 0);
701         snd_pmac_dma_set_command(rec, &chip->extra_dma);
702         snd_pmac_dma_run(rec, RUN);
703 }
704
705 void snd_pmac_beep_dma_stop(pmac_t *chip)
706 {
707         snd_pmac_dma_stop(&chip->playback);
708         st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
709         snd_pmac_pcm_set_format(chip); /* reset format */
710 }
711
712
713 /*
714  * interrupt handlers
715  */
716 static irqreturn_t
717 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
718 {
719         pmac_t *chip = devid;
720         snd_pmac_pcm_update(chip, &chip->playback);
721         return IRQ_HANDLED;
722 }
723
724
725 static irqreturn_t
726 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
727 {
728         pmac_t *chip = devid;
729         snd_pmac_pcm_update(chip, &chip->capture);
730         return IRQ_HANDLED;
731 }
732
733
734 static irqreturn_t
735 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
736 {
737         pmac_t *chip = devid;
738         int ctrl = in_le32(&chip->awacs->control);
739
740         /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
741         if (ctrl & MASK_PORTCHG) {
742                 /* do something when headphone is plugged/unplugged? */
743                 if (chip->update_automute)
744                         chip->update_automute(chip, 1);
745         }
746         if (ctrl & MASK_CNTLERR) {
747                 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
748                 if (err && chip->model <= PMAC_SCREAMER)
749                         snd_printk(KERN_DEBUG "error %x\n", err);
750         }
751         /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
752         out_le32(&chip->awacs->control, ctrl);
753         return IRQ_HANDLED;
754 }
755
756
757 /*
758  * a wrapper to feature call for compatibility
759  */
760 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
761 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
762 {
763 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
764         ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
765 #else
766         if (chip->is_pbook_G3) {
767                 pmu_suspend();
768                 feature_clear(chip->node, FEATURE_Sound_power);
769                 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
770                 big_mdelay(1000); /* XXX */
771                 pmu_resume();
772         }
773         if (chip->is_pbook_3400) {
774                 feature_set(chip->node, FEATURE_IOBUS_enable);
775                 udelay(10);
776         }
777 #endif
778 }
779 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
780 #define snd_pmac_sound_feature(chip,enable) /**/
781 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
782
783 /*
784  * release resources
785  */
786
787 static int snd_pmac_free(pmac_t *chip)
788 {
789         int i;
790
791         /* stop sounds */
792         if (chip->initialized) {
793                 snd_pmac_dbdma_reset(chip);
794                 /* disable interrupts from awacs interface */
795                 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
796         }
797
798         snd_pmac_sound_feature(chip, 0);
799 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
800         snd_pmac_unregister_sleep_notifier(chip);
801 #endif
802
803         /* clean up mixer if any */
804         if (chip->mixer_free)
805                 chip->mixer_free(chip);
806
807         snd_pmac_detach_beep(chip);
808
809         /* release resources */
810         if (chip->irq >= 0)
811                 free_irq(chip->irq, (void*)chip);
812         if (chip->tx_irq >= 0)
813                 free_irq(chip->tx_irq, (void*)chip);
814         if (chip->rx_irq >= 0)
815                 free_irq(chip->rx_irq, (void*)chip);
816         snd_pmac_dbdma_free(&chip->playback.cmd);
817         snd_pmac_dbdma_free(&chip->capture.cmd);
818         snd_pmac_dbdma_free(&chip->extra_dma);
819         if (chip->macio_base)
820                 iounmap(chip->macio_base);
821         if (chip->latch_base)
822                 iounmap(chip->latch_base);
823         if (chip->awacs)
824                 iounmap(chip->awacs);
825         if (chip->playback.dma)
826                 iounmap(chip->playback.dma);
827         if (chip->capture.dma)
828                 iounmap(chip->capture.dma);
829         if (chip->node) {
830                 for (i = 0; i < 3; i++) {
831                         if (chip->of_requested & (1 << i))
832                                 release_OF_resource(chip->node, i);
833                 }
834         }
835         kfree(chip);
836         return 0;
837 }
838
839
840 /*
841  * free the device
842  */
843 static int snd_pmac_dev_free(snd_device_t *device)
844 {
845         pmac_t *chip = device->device_data;
846         return snd_pmac_free(chip);
847 }
848
849
850 /*
851  * check the machine support byteswap (little-endian)
852  */
853
854 static void __init detect_byte_swap(pmac_t *chip)
855 {
856         struct device_node *mio;
857
858         /* if seems that Keylargo can't byte-swap  */
859         for (mio = chip->node->parent; mio; mio = mio->parent) {
860                 if (strcmp(mio->name, "mac-io") == 0) {
861                         if (device_is_compatible(mio, "Keylargo"))
862                                 chip->can_byte_swap = 0;
863                         break;
864                 }
865         }
866
867         /* it seems the Pismo & iBook can't byte-swap in hardware. */
868         if (machine_is_compatible("PowerBook3,1") ||
869             machine_is_compatible("PowerBook2,1"))
870                 chip->can_byte_swap = 0 ;
871
872         if (machine_is_compatible("PowerBook2,1"))
873                 chip->can_duplex = 0;
874 }
875
876
877 /*
878  * detect a sound chip
879  */
880 static int __init snd_pmac_detect(pmac_t *chip)
881 {
882         struct device_node *sound;
883         unsigned int *prop, l;
884         u32 layout_id = 0;
885
886         if (_machine != _MACH_Pmac)
887                 return -ENODEV;
888
889         chip->subframe = 0;
890         chip->revision = 0;
891         chip->freqs_ok = 0xff; /* all ok */
892         chip->model = PMAC_AWACS;
893         chip->can_byte_swap = 1;
894         chip->can_duplex = 1;
895         chip->can_capture = 1;
896         chip->num_freqs = ARRAY_SIZE(awacs_freqs);
897         chip->freq_table = awacs_freqs;
898
899         chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
900
901         /* check machine type */
902         if (machine_is_compatible("AAPL,3400/2400")
903             || machine_is_compatible("AAPL,3500"))
904                 chip->is_pbook_3400 = 1;
905         else if (machine_is_compatible("PowerBook1,1")
906                  || machine_is_compatible("AAPL,PowerBook1998"))
907                 chip->is_pbook_G3 = 1;
908         chip->node = find_devices("awacs");
909         if (chip->node)
910                 return 0; /* ok */
911
912         /*
913          * powermac G3 models have a node called "davbus"
914          * with a child called "sound".
915          */
916         chip->node = find_devices("davbus");
917         /*
918          * if we didn't find a davbus device, try 'i2s-a' since
919          * this seems to be what iBooks have
920          */
921         if (! chip->node)
922                 chip->node = find_devices("i2s-a");
923         if (! chip->node)
924                 return -ENODEV;
925         sound = find_devices("sound");
926         while (sound && sound->parent != chip->node)
927                 sound = sound->next;
928         if (! sound)
929                 return -ENODEV;
930         prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
931         if (prop && *prop < 16)
932                 chip->subframe = *prop;
933         prop = (unsigned int *) get_property(sound, "layout-id", NULL);
934         if (prop)
935                 layout_id = *prop;
936         /* This should be verified on older screamers */
937         if (device_is_compatible(sound, "screamer")) {
938                 chip->model = PMAC_SCREAMER;
939                 // chip->can_byte_swap = 0; /* FIXME: check this */
940         }
941         if (device_is_compatible(sound, "burgundy")) {
942                 chip->model = PMAC_BURGUNDY;
943                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
944         }
945         if (device_is_compatible(sound, "daca")) {
946                 chip->model = PMAC_DACA;
947                 chip->can_capture = 0;  /* no capture */
948                 chip->can_duplex = 0;
949                 // chip->can_byte_swap = 0; /* FIXME: check this */
950                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
951         }
952         if (device_is_compatible(sound, "tumbler")) {
953                 chip->model = PMAC_TUMBLER;
954                 chip->can_capture = 0;  /* no capture */
955                 chip->can_duplex = 0;
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, "snapper")) {
962                 chip->model = PMAC_SNAPPER;
963                 // chip->can_byte_swap = 0; /* FIXME: check this */
964                 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
965                 chip->freq_table = tumbler_freqs;
966                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
967         }
968         if (device_is_compatible(sound, "AOAKeylargo") ||
969             device_is_compatible(sound, "AOAbase")) {
970                 /* For now, only support very basic TAS3004 based machines with
971                  * single frequency until proper i2s control is implemented
972                  */
973                 switch(layout_id) {
974                 case 0x48:
975                 case 0x46:
976                 case 0x33:
977                 case 0x29:
978                         chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
979                         chip->model = PMAC_SNAPPER;
980                         chip->can_byte_swap = 0; /* FIXME: check this */
981                         chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
982                         break;
983                 }
984         }
985         prop = (unsigned int *)get_property(sound, "device-id", NULL);
986         if (prop)
987                 chip->device_id = *prop;
988         chip->has_iic = (find_devices("perch") != NULL);
989
990         detect_byte_swap(chip);
991
992         /* look for a property saying what sample rates
993            are available */
994         prop = (unsigned int *) get_property(sound, "sample-rates", &l);
995         if (! prop)
996                 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
997         if (prop) {
998                 int i;
999                 chip->freqs_ok = 0;
1000                 for (l /= sizeof(int); l > 0; --l) {
1001                         unsigned int r = *prop++;
1002                         /* Apple 'Fixed' format */
1003                         if (r >= 0x10000)
1004                                 r >>= 16;
1005                         for (i = 0; i < chip->num_freqs; ++i) {
1006                                 if (r == chip->freq_table[i]) {
1007                                         chip->freqs_ok |= (1 << i);
1008                                         break;
1009                                 }
1010                         }
1011                 }
1012         } else {
1013                 /* assume only 44.1khz */
1014                 chip->freqs_ok = 1;
1015         }
1016
1017         return 0;
1018 }
1019
1020 /*
1021  * exported - boolean info callbacks for ease of programming
1022  */
1023 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1024 {
1025         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1026         uinfo->count = 2;
1027         uinfo->value.integer.min = 0;
1028         uinfo->value.integer.max = 1;
1029         return 0;
1030 }
1031
1032 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1033 {
1034         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1035         uinfo->count = 1;
1036         uinfo->value.integer.min = 0;
1037         uinfo->value.integer.max = 1;
1038         return 0;
1039 }
1040
1041 #ifdef PMAC_SUPPORT_AUTOMUTE
1042 /*
1043  * auto-mute
1044  */
1045 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1046 {
1047         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1048         ucontrol->value.integer.value[0] = chip->auto_mute;
1049         return 0;
1050 }
1051
1052 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1053 {
1054         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1055         if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1056                 chip->auto_mute = ucontrol->value.integer.value[0];
1057                 if (chip->update_automute)
1058                         chip->update_automute(chip, 1);
1059                 return 1;
1060         }
1061         return 0;
1062 }
1063
1064 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1065 {
1066         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1067         if (chip->detect_headphone)
1068                 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1069         else
1070                 ucontrol->value.integer.value[0] = 0;
1071         return 0;
1072 }
1073
1074 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1075         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1076           .name = "Auto Mute Switch",
1077           .info = snd_pmac_boolean_mono_info,
1078           .get = pmac_auto_mute_get,
1079           .put = pmac_auto_mute_put,
1080         },
1081         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1082           .name = "Headphone Detection",
1083           .access = SNDRV_CTL_ELEM_ACCESS_READ,
1084           .info = snd_pmac_boolean_mono_info,
1085           .get = pmac_hp_detect_get,
1086         },
1087 };
1088
1089 int __init snd_pmac_add_automute(pmac_t *chip)
1090 {
1091         int err;
1092         chip->auto_mute = 1;
1093         err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1094         if (err < 0)
1095                 return err;
1096         chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1097         return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1098 }
1099 #endif /* PMAC_SUPPORT_AUTOMUTE */
1100
1101 /*
1102  * create and detect a pmac chip record
1103  */
1104 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1105 {
1106         pmac_t *chip;
1107         struct device_node *np;
1108         int i, err;
1109         static snd_device_ops_t ops = {
1110                 .dev_free =     snd_pmac_dev_free,
1111         };
1112
1113         snd_runtime_check(chip_return, return -EINVAL);
1114         *chip_return = NULL;
1115
1116         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1117         if (chip == NULL)
1118                 return -ENOMEM;
1119         chip->card = card;
1120
1121         spin_lock_init(&chip->reg_lock);
1122         chip->irq = chip->tx_irq = chip->rx_irq = -1;
1123
1124         chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1125         chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1126
1127         if ((err = snd_pmac_detect(chip)) < 0)
1128                 goto __error;
1129
1130         if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1131             snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1132             snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1133                 err = -ENOMEM;
1134                 goto __error;
1135         }
1136
1137         np = chip->node;
1138         if (np->n_addrs < 3 || np->n_intrs < 3) {
1139                 err = -ENODEV;
1140                 goto __error;
1141         }
1142
1143         for (i = 0; i < 3; i++) {
1144                 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1145                 if (! request_OF_resource(np, i, name[i])) {
1146                         snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1147                         err = -ENODEV;
1148                         goto __error;
1149                 }
1150                 chip->of_requested |= (1 << i);
1151         }
1152
1153         chip->awacs = ioremap(np->addrs[0].address, 0x1000);
1154         chip->playback.dma = ioremap(np->addrs[1].address, 0x100);
1155         chip->capture.dma = ioremap(np->addrs[2].address, 0x100);
1156         if (chip->model <= PMAC_BURGUNDY) {
1157                 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1158                                 "PMac", (void*)chip)) {
1159                         snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1160                         err = -EBUSY;
1161                         goto __error;
1162                 }
1163                 chip->irq = np->intrs[0].line;
1164         }
1165         if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1166                         "PMac Output", (void*)chip)) {
1167                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1168                 err = -EBUSY;
1169                 goto __error;
1170         }
1171         chip->tx_irq = np->intrs[1].line;
1172         if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1173                         "PMac Input", (void*)chip)) {
1174                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1175                 err = -EBUSY;
1176                 goto __error;
1177         }
1178         chip->rx_irq = np->intrs[2].line;
1179
1180         snd_pmac_sound_feature(chip, 1);
1181
1182         /* reset */
1183         out_le32(&chip->awacs->control, 0x11);
1184
1185         /* Powerbooks have odd ways of enabling inputs such as
1186            an expansion-bay CD or sound from an internal modem
1187            or a PC-card modem. */
1188         if (chip->is_pbook_3400) {
1189                 /* Enable CD and PC-card sound inputs. */
1190                 /* This is done by reading from address
1191                  * f301a000, + 0x10 to enable the expansion-bay
1192                  * CD sound input, + 0x80 to enable the PC-card
1193                  * sound input.  The 0x100 enables the SCSI bus
1194                  * terminator power.
1195                  */
1196                 chip->latch_base = ioremap (0xf301a000, 0x1000);
1197                 in_8(chip->latch_base + 0x190);
1198         } else if (chip->is_pbook_G3) {
1199                 struct device_node* mio;
1200                 for (mio = chip->node->parent; mio; mio = mio->parent) {
1201                         if (strcmp(mio->name, "mac-io") == 0
1202                             && mio->n_addrs > 0) {
1203                                 chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
1204                                 break;
1205                         }
1206                 }
1207                 /* Enable CD sound input. */
1208                 /* The relevant bits for writing to this byte are 0x8f.
1209                  * I haven't found out what the 0x80 bit does.
1210                  * For the 0xf bits, writing 3 or 7 enables the CD
1211                  * input, any other value disables it.  Values
1212                  * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1213                  * 4, 6, 8 - f enable the input from the modem.
1214                  */
1215                 if (chip->macio_base)
1216                         out_8(chip->macio_base + 0x37, 3);
1217         }
1218
1219         /* Reset dbdma channels */
1220         snd_pmac_dbdma_reset(chip);
1221
1222 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1223         /* add sleep notifier */
1224         if (! snd_pmac_register_sleep_notifier(chip))
1225                 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1226 #endif
1227
1228         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1229                 goto __error;
1230
1231         *chip_return = chip;
1232         return 0;
1233
1234  __error:
1235         snd_pmac_free(chip);
1236         return err;
1237 }
1238
1239
1240 /*
1241  * sleep notify for powerbook
1242  */
1243
1244 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1245
1246 /*
1247  * Save state when going to sleep, restore it afterwards.
1248  */
1249
1250 static int snd_pmac_suspend(snd_card_t *card, pm_message_t state)
1251 {
1252         pmac_t *chip = card->pm_private_data;
1253         unsigned long flags;
1254
1255         if (chip->suspend)
1256                 chip->suspend(chip);
1257         snd_pcm_suspend_all(chip->pcm);
1258         spin_lock_irqsave(&chip->reg_lock, flags);
1259         snd_pmac_beep_stop(chip);
1260         spin_unlock_irqrestore(&chip->reg_lock, flags);
1261         if (chip->irq >= 0)
1262                 disable_irq(chip->irq);
1263         if (chip->tx_irq >= 0)
1264                 disable_irq(chip->tx_irq);
1265         if (chip->rx_irq >= 0)
1266                 disable_irq(chip->rx_irq);
1267         snd_pmac_sound_feature(chip, 0);
1268         return 0;
1269 }
1270
1271 static int snd_pmac_resume(snd_card_t *card)
1272 {
1273         pmac_t *chip = card->pm_private_data;
1274
1275         snd_pmac_sound_feature(chip, 1);
1276         if (chip->resume)
1277                 chip->resume(chip);
1278         /* enable CD sound input */
1279         if (chip->macio_base && chip->is_pbook_G3) {
1280                 out_8(chip->macio_base + 0x37, 3);
1281         } else if (chip->is_pbook_3400) {
1282                 in_8(chip->latch_base + 0x190);
1283         }
1284
1285         snd_pmac_pcm_set_format(chip);
1286
1287         if (chip->irq >= 0)
1288                 enable_irq(chip->irq);
1289         if (chip->tx_irq >= 0)
1290                 enable_irq(chip->tx_irq);
1291         if (chip->rx_irq >= 0)
1292                 enable_irq(chip->rx_irq);
1293
1294         return 0;
1295 }
1296
1297 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1298  * because we can't have any private data for notify callback.
1299  */
1300 static pmac_t *sleeping_pmac = NULL;
1301
1302 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1303 {
1304         pmac_t *chip;
1305
1306         chip = sleeping_pmac;
1307         snd_runtime_check(chip, return 0);
1308
1309         switch (when) {
1310         case PBOOK_SLEEP_NOW:
1311                 snd_pmac_suspend(chip->card, PMSG_SUSPEND);
1312                 break;
1313         case PBOOK_WAKE:
1314                 snd_pmac_resume(chip->card);
1315                 break;
1316         }
1317         return PBOOK_SLEEP_OK;
1318 }
1319
1320 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1321         snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1322 };
1323
1324 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1325 {
1326         /* should be protected here.. */
1327         snd_assert(! sleeping_pmac, return -EBUSY);
1328         sleeping_pmac = chip;
1329         pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1330         return 0;
1331 }
1332                                                     
1333 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1334 {
1335         /* should be protected here.. */
1336         snd_assert(sleeping_pmac == chip, return -ENODEV);
1337         pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1338         sleeping_pmac = NULL;
1339         return 0;
1340 }
1341
1342 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */