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