ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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_set_power_state(snd_card_t *card, unsigned int power_state);
46 #endif
47
48
49 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
50 static int awacs_freqs[8] = {
51         44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
52 };
53 /* fixed frequency table for tumbler */
54 static int tumbler_freqs[2] = {
55         48000, 44100
56 };
57
58 /*
59  * allocate DBDMA command arrays
60  */
61 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
62 {
63         rec->space = kmalloc(sizeof(struct dbdma_cmd) * (size + 1), GFP_KERNEL);
64         if (rec->space == NULL)
65                 return -ENOMEM;
66         rec->size = size;
67         memset(rec->space, 0, sizeof(struct dbdma_cmd) * (size + 1));
68         rec->cmds = (void*)DBDMA_ALIGN(rec->space);
69         rec->addr = virt_to_bus(rec->cmds);
70         return 0;
71 }
72
73 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
74 {
75         if (rec && rec->space)
76                 kfree(rec->space);
77 }
78
79
80 /*
81  * pcm stuff
82  */
83
84 /*
85  * look up frequency table
86  */
87
88 static unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
89 {
90         int i, ok, found;
91
92         ok = rec->cur_freqs;
93         if (rate > chip->freq_table[0])
94                 return 0;
95         found = 0;
96         for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
97                 if (! (ok & 1)) continue;
98                 found = i;
99                 if (rate >= chip->freq_table[i])
100                         break;
101         }
102         return found;
103 }
104
105 /*
106  * check whether another stream is active
107  */
108 static inline int another_stream(int stream)
109 {
110         return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
111                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
112 }
113
114 /*
115  * allocate buffers
116  */
117 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
118                                   snd_pcm_hw_params_t *hw_params)
119 {
120         return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
121 }
122
123 /*
124  * release buffers
125  */
126 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
127 {
128         snd_pcm_lib_free_pages(subs);
129         return 0;
130 }
131
132 /*
133  * get a stream of the opposite direction
134  */
135 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
136 {
137         switch (stream) {
138         case SNDRV_PCM_STREAM_PLAYBACK:
139                 return &chip->playback;
140         case SNDRV_PCM_STREAM_CAPTURE:
141                 return &chip->capture;
142         default:
143                 snd_BUG();
144                 return NULL;
145         }
146 }
147
148 /*
149  * wait while run status is on
150  */
151 inline static void
152 snd_pmac_wait_ack(pmac_stream_t *rec)
153 {
154         int timeout = 50000;
155         while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
156                 udelay(1);
157 }
158
159 /*
160  * set the format and rate to the chip.
161  * call the lowlevel function if defined (e.g. for AWACS).
162  */
163 static void snd_pmac_pcm_set_format(pmac_t *chip)
164 {
165         /* set up frequency and format */
166         out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
167         out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
168         if (chip->set_format)
169                 chip->set_format(chip);
170 }
171
172 /*
173  * stop the DMA transfer
174  */
175 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
176 {
177         out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
178         snd_pmac_wait_ack(rec);
179 }
180
181 /*
182  * set the command pointer address
183  */
184 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
185 {
186         out_le32(&rec->dma->cmdptr, cmd->addr);
187 }
188
189 /*
190  * start the DMA
191  */
192 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
193 {
194         out_le32(&rec->dma->control, status | (status << 16));
195 }
196
197
198 /*
199  * prepare playback/capture stream
200  */
201 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
202 {
203         int i;
204         volatile struct dbdma_cmd *cp;
205         unsigned long flags;
206         snd_pcm_runtime_t *runtime = subs->runtime;
207         int rate_index;
208         long offset;
209         pmac_stream_t *astr;
210         
211         rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
212         rec->period_size = snd_pcm_lib_period_bytes(subs);
213         rec->nperiods = rec->dma_size / rec->period_size;
214         rec->cur_period = 0;
215         rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
216
217         /* set up constraints */
218         astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
219         snd_runtime_check(astr, return -EINVAL);
220         astr->cur_freqs = 1 << rate_index;
221         astr->cur_formats = 1 << runtime->format;
222         chip->rate_index = rate_index;
223         chip->format = runtime->format;
224
225         /* We really want to execute a DMA stop command, after the AWACS
226          * is initialized.
227          * For reasons I don't understand, it stops the hissing noise
228          * common to many PowerBook G3 systems (like mine :-).
229          */
230         spin_lock_irqsave(&chip->reg_lock, flags);
231         snd_pmac_dma_stop(rec);
232         if (rec->stream == SNDRV_PCM_STREAM_PLAYBACK) {
233                 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
234                 snd_pmac_dma_set_command(rec, &chip->extra_dma);
235                 snd_pmac_dma_run(rec, RUN);
236         }
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_irqrestore(&chip->reg_lock, flags);
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         unsigned long flags;
267         volatile struct dbdma_cmd *cp;
268         int i, command;
269
270         switch (cmd) {
271         case SNDRV_PCM_TRIGGER_START:
272         case SNDRV_PCM_TRIGGER_RESUME:
273                 if (rec->running)
274                         return -EBUSY;
275                 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
276                            OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
277                 spin_lock_irqsave(&chip->reg_lock, flags);
278                 snd_pmac_beep_stop(chip);
279                 snd_pmac_pcm_set_format(chip);
280                 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
281                         out_le16(&cp->command, command);
282                 snd_pmac_dma_set_command(rec, &rec->cmd);
283                 (void)in_le32(&rec->dma->status);
284                 snd_pmac_dma_run(rec, RUN|WAKE);
285                 rec->running = 1;
286                 spin_unlock_irqrestore(&chip->reg_lock, flags);
287                 break;
288
289         case SNDRV_PCM_TRIGGER_STOP:
290         case SNDRV_PCM_TRIGGER_SUSPEND:
291                 spin_lock_irqsave(&chip->reg_lock, flags);
292                 rec->running = 0;
293                 /*printk("stopped!!\n");*/
294                 snd_pmac_dma_stop(rec);
295                 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
296                         out_le16(&cp->command, DBDMA_STOP);
297                 spin_unlock_irqrestore(&chip->reg_lock, flags);
298                 break;
299
300         default:
301                 return -EINVAL;
302         }
303
304         return 0;
305 }
306
307 /*
308  * return the current pointer
309  */
310 inline
311 static snd_pcm_uframes_t snd_pmac_pcm_pointer(pmac_t *chip, pmac_stream_t *rec,
312                                               snd_pcm_substream_t *subs)
313 {
314         int count = 0;
315
316 #if 1 /* hmm.. how can we get the current dma pointer?? */
317         int stat;
318         volatile struct dbdma_cmd *cp = &rec->cmd.cmds[rec->cur_period];
319         stat = ld_le16(&cp->xfer_status);
320         if (stat & (ACTIVE|DEAD)) {
321                 count = in_le16(&cp->res_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 *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 =     32768,
430         .period_bytes_min =     256,
431         .period_bytes_max =     16384,
432         .periods_min =          1,
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 =     32768,
449         .period_bytes_min =     256,
450         .period_bytes_max =     16384,
451         .periods_min =          1,
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                 48000,
493                 44100,
494                 22050,
495                 11025,
496                 0,
497         };
498         static int typical_freq_flags[] = {
499                 SNDRV_PCM_RATE_48000,
500                 SNDRV_PCM_RATE_44100,
501                 SNDRV_PCM_RATE_22050,
502                 SNDRV_PCM_RATE_11025,
503                 0,
504         };
505
506         /* look up frequency table and fill bit mask */
507         runtime->hw.rates = 0;
508         fflags = chip->freqs_ok;
509         for (i = 0; typical_freqs[i]; i++) {
510                 for (j = 0; j < chip->num_freqs; j++) {
511                         if ((chip->freqs_ok & (1 << j)) &&
512                             chip->freq_table[j] == typical_freqs[i]) {
513                                 runtime->hw.rates |= typical_freq_flags[i];
514                                 fflags &= ~(1 << j);
515                                 break;
516                         }
517                 }
518         }
519         if (fflags) /* rest */
520                 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
521
522         /* check for minimum and maximum rates */
523         for (i = 0; i < chip->num_freqs; i++) {
524                 if (chip->freqs_ok & (1 << i)) {
525                         runtime->hw.rate_max = chip->freq_table[i];
526                         break;
527                 }
528         }
529         for (i = chip->num_freqs - 1; i >= 0; i--) {
530                 if (chip->freqs_ok & (1 << i)) {
531                         runtime->hw.rate_min = chip->freq_table[i];
532                         break;
533                 }
534         }
535         runtime->hw.formats = chip->formats_ok;
536         if (chip->can_capture) {
537                 if (! chip->can_duplex)
538                         runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
539                 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
540         }
541         runtime->private_data = rec;
542         rec->substream = subs;
543
544 #if 0 /* FIXME: still under development.. */
545         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
546                             snd_pmac_hw_rule_rate, chip, rec->stream, -1);
547         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
548                             snd_pmac_hw_rule_format, chip, rec->stream, -1);
549 #endif
550
551         runtime->hw.periods_max = rec->cmd.size - 1;
552
553         if (chip->can_duplex)
554                 snd_pcm_set_sync(subs);
555
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 = 0;
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  * interrupt handlers
686  */
687 static irqreturn_t
688 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
689 {
690         pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
691         snd_pmac_pcm_update(chip, &chip->playback);
692         return IRQ_HANDLED;
693 }
694
695
696 static irqreturn_t
697 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
698 {
699         pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
700         snd_pmac_pcm_update(chip, &chip->capture);
701         return IRQ_HANDLED;
702 }
703
704
705 static irqreturn_t
706 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
707 {
708         pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
709         int ctrl = in_le32(&chip->awacs->control);
710
711         /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
712         if (ctrl & MASK_PORTCHG) {
713                 /* do something when headphone is plugged/unplugged? */
714                 if (chip->update_automute)
715                         chip->update_automute(chip, 1);
716         }
717         if (ctrl & MASK_CNTLERR) {
718                 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
719                 if (err && chip->model <= PMAC_SCREAMER)
720                         snd_printk(KERN_DEBUG "error %x\n", err);
721         }
722         /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
723         out_le32(&chip->awacs->control, ctrl);
724         return IRQ_HANDLED;
725 }
726
727
728 /*
729  * a wrapper to feature call for compatibility
730  */
731 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
732 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
733 {
734 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
735         ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
736 #else
737         if (chip->is_pbook_G3) {
738                 pmu_suspend();
739                 feature_clear(chip->node, FEATURE_Sound_power);
740                 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
741                 big_mdelay(1000); /* XXX */
742                 pmu_resume();
743         }
744         if (chip->is_pbook_3400) {
745                 feature_set(chip->node, FEATURE_IOBUS_enable);
746                 udelay(10);
747         }
748 #endif
749 }
750 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
751 #define snd_pmac_sound_feature(chip,enable) /**/
752 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
753
754 /*
755  * release resources
756  */
757
758 static int snd_pmac_free(pmac_t *chip)
759 {
760         int i;
761
762         /* stop sounds */
763         if (chip->initialized) {
764                 snd_pmac_dbdma_reset(chip);
765                 /* disable interrupts from awacs interface */
766                 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
767         }
768
769         snd_pmac_sound_feature(chip, 0);
770 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
771         snd_pmac_unregister_sleep_notifier(chip);
772 #endif
773
774         /* clean up mixer if any */
775         if (chip->mixer_free)
776                 chip->mixer_free(chip);
777
778         /* release resources */
779         if (chip->irq >= 0)
780                 free_irq(chip->irq, (void*)chip);
781         if (chip->tx_irq >= 0)
782                 free_irq(chip->tx_irq, (void*)chip);
783         if (chip->rx_irq >= 0)
784                 free_irq(chip->rx_irq, (void*)chip);
785         snd_pmac_dbdma_free(&chip->playback.cmd);
786         snd_pmac_dbdma_free(&chip->capture.cmd);
787         snd_pmac_dbdma_free(&chip->extra_dma);
788         if (chip->macio_base)
789                 iounmap(chip->macio_base);
790         if (chip->latch_base)
791                 iounmap(chip->latch_base);
792         if (chip->awacs)
793                 iounmap((void*)chip->awacs);
794         if (chip->playback.dma)
795                 iounmap((void*)chip->playback.dma);
796         if (chip->capture.dma)
797                 iounmap((void*)chip->capture.dma);
798         if (chip->node) {
799                 for (i = 0; i < 3; i++) {
800                         if (chip->of_requested & (1 << i))
801                                 release_OF_resource(chip->node, i);
802                 }
803         }
804         snd_magic_kfree(chip);
805         return 0;
806 }
807
808
809 /*
810  * free the device
811  */
812 static int snd_pmac_dev_free(snd_device_t *device)
813 {
814         pmac_t *chip = snd_magic_cast(pmac_t, device->device_data, return -ENXIO);
815         return snd_pmac_free(chip);
816 }
817
818
819 /*
820  * check the machine support byteswap (little-endian)
821  */
822
823 static void __init detect_byte_swap(pmac_t *chip)
824 {
825         struct device_node *mio;
826
827         /* if seems that Keylargo can't byte-swap  */
828         for (mio = chip->node->parent; mio; mio = mio->parent) {
829                 if (strcmp(mio->name, "mac-io") == 0) {
830                         if (device_is_compatible(mio, "Keylargo"))
831                                 chip->can_byte_swap = 0;
832                         break;
833                 }
834         }
835
836         /* it seems the Pismo & iBook can't byte-swap in hardware. */
837         if (machine_is_compatible("PowerBook3,1") ||
838             machine_is_compatible("PowerBook2,1"))
839                 chip->can_byte_swap = 0 ;
840
841         if (machine_is_compatible("PowerBook2,1"))
842                 chip->can_duplex = 0;
843 }
844
845
846 /*
847  * detect a sound chip
848  */
849 static int __init snd_pmac_detect(pmac_t *chip)
850 {
851         struct device_node *sound;
852         unsigned int *prop, l;
853
854         if (_machine != _MACH_Pmac)
855                 return -ENODEV;
856
857         chip->subframe = 0;
858         chip->revision = 0;
859         chip->freqs_ok = 0xff; /* all ok */
860         chip->model = PMAC_AWACS;
861         chip->can_byte_swap = 1;
862         chip->can_duplex = 1;
863         chip->can_capture = 1;
864         chip->num_freqs = 8;
865         chip->freq_table = awacs_freqs;
866
867         chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
868
869         /* check machine type */
870         if (machine_is_compatible("AAPL,3400/2400")
871             || machine_is_compatible("AAPL,3500"))
872                 chip->is_pbook_3400 = 1;
873         else if (machine_is_compatible("PowerBook1,1")
874                  || machine_is_compatible("AAPL,PowerBook1998"))
875                 chip->is_pbook_G3 = 1;
876         chip->node = find_devices("awacs");
877         if (chip->node)
878                 return 0; /* ok */
879
880         /*
881          * powermac G3 models have a node called "davbus"
882          * with a child called "sound".
883          */
884         chip->node = find_devices("davbus");
885         /*
886          * if we didn't find a davbus device, try 'i2s-a' since
887          * this seems to be what iBooks have
888          */
889         if (! chip->node)
890                 chip->node = find_devices("i2s-a");
891         if (! chip->node)
892                 return -ENODEV;
893         sound = find_devices("sound");
894         while (sound && sound->parent != chip->node)
895                 sound = sound->next;
896         if (! sound)
897                 return -ENODEV;
898         prop = (unsigned int *) get_property(sound, "sub-frame", 0);
899         if (prop && *prop < 16)
900                 chip->subframe = *prop;
901         /* This should be verified on older screamers */
902         if (device_is_compatible(sound, "screamer")) {
903                 chip->model = PMAC_SCREAMER;
904                 // chip->can_byte_swap = 0; /* FIXME: check this */
905         }
906         if (device_is_compatible(sound, "burgundy")) {
907                 chip->model = PMAC_BURGUNDY;
908                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
909         }
910         if (device_is_compatible(sound, "daca")) {
911                 chip->model = PMAC_DACA;
912                 chip->can_capture = 0;  /* no capture */
913                 chip->can_duplex = 0;
914                 // chip->can_byte_swap = 0; /* FIXME: check this */
915                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
916         }
917         if (device_is_compatible(sound, "tumbler")) {
918                 chip->model = PMAC_TUMBLER;
919                 chip->can_capture = 0;  /* no capture */
920                 chip->can_duplex = 0;
921                 // chip->can_byte_swap = 0; /* FIXME: check this */
922                 chip->num_freqs = 2;
923                 chip->freq_table = tumbler_freqs;
924                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
925         }
926         if (device_is_compatible(sound, "snapper")) {
927                 chip->model = PMAC_SNAPPER;
928                 // chip->can_byte_swap = 0; /* FIXME: check this */
929                 chip->num_freqs = 2;
930                 chip->freq_table = tumbler_freqs;
931                 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
932         }
933         prop = (unsigned int *)get_property(sound, "device-id", 0);
934         if (prop)
935                 chip->device_id = *prop;
936         chip->has_iic = (find_devices("perch") != NULL);
937
938         detect_byte_swap(chip);
939
940         /* look for a property saying what sample rates
941            are available */
942         prop = (unsigned int *) get_property(sound, "sample-rates", &l);
943         if (! prop)
944                 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
945         if (prop) {
946                 int i;
947                 chip->freqs_ok = 0;
948                 for (l /= sizeof(int); l > 0; --l) {
949                         unsigned int r = *prop++;
950                         /* Apple 'Fixed' format */
951                         if (r >= 0x10000)
952                                 r >>= 16;
953                         for (i = 0; i < chip->num_freqs; ++i) {
954                                 if (r == chip->freq_table[i]) {
955                                         chip->freqs_ok |= (1 << i);
956                                         break;
957                                 }
958                         }
959                 }
960         } else {
961                 /* assume only 44.1khz */
962                 chip->freqs_ok = 1;
963         }
964
965         return 0;
966 }
967
968 /*
969  * exported - boolean info callbacks for ease of programming
970  */
971 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
972 {
973         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
974         uinfo->count = 2;
975         uinfo->value.integer.min = 0;
976         uinfo->value.integer.max = 1;
977         return 0;
978 }
979
980 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
981 {
982         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
983         uinfo->count = 1;
984         uinfo->value.integer.min = 0;
985         uinfo->value.integer.max = 1;
986         return 0;
987 }
988
989 #ifdef PMAC_SUPPORT_AUTOMUTE
990 /*
991  * auto-mute
992  */
993 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
994 {
995         pmac_t *chip = snd_kcontrol_chip(kcontrol);
996         ucontrol->value.integer.value[0] = chip->auto_mute;
997         return 0;
998 }
999
1000 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1001 {
1002         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1003         if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1004                 chip->auto_mute = ucontrol->value.integer.value[0];
1005                 if (chip->update_automute)
1006                         chip->update_automute(chip, 1);
1007                 return 1;
1008         }
1009         return 0;
1010 }
1011
1012 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1013 {
1014         pmac_t *chip = snd_kcontrol_chip(kcontrol);
1015         if (chip->detect_headphone)
1016                 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1017         else
1018                 ucontrol->value.integer.value[0] = 0;
1019         return 0;
1020 }
1021
1022 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1023         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1024           .name = "Auto Mute Switch",
1025           .info = snd_pmac_boolean_mono_info,
1026           .get = pmac_auto_mute_get,
1027           .put = pmac_auto_mute_put,
1028         },
1029         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1030           .name = "Headphone Detection",
1031           .access = SNDRV_CTL_ELEM_ACCESS_READ,
1032           .info = snd_pmac_boolean_mono_info,
1033           .get = pmac_hp_detect_get,
1034         },
1035 };
1036
1037 int __init snd_pmac_add_automute(pmac_t *chip)
1038 {
1039         int err;
1040         chip->auto_mute = 1;
1041         err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1042         if (err < 0)
1043                 return err;
1044         chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1045         return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1046 }
1047 #endif /* PMAC_SUPPORT_AUTOMUTE */
1048
1049 /*
1050  * create and detect a pmac chip record
1051  */
1052 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1053 {
1054         pmac_t *chip;
1055         struct device_node *np;
1056         int i, err;
1057         static snd_device_ops_t ops = {
1058                 .dev_free =     snd_pmac_dev_free,
1059         };
1060
1061         snd_runtime_check(chip_return, return -EINVAL);
1062         *chip_return = NULL;
1063
1064         chip = snd_magic_kcalloc(pmac_t, 0, GFP_KERNEL);
1065         if (chip == NULL)
1066                 return -ENOMEM;
1067         chip->card = card;
1068
1069         spin_lock_init(&chip->reg_lock);
1070         chip->irq = chip->tx_irq = chip->rx_irq = -1;
1071
1072         chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1073         chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1074
1075         if ((err = snd_pmac_detect(chip)) < 0)
1076                 goto __error;
1077
1078         if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1079             snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1080             snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1081                 err = -ENOMEM;
1082                 goto __error;
1083         }
1084
1085         np = chip->node;
1086         if (np->n_addrs < 3 || np->n_intrs < 3) {
1087                 err = -ENODEV;
1088                 goto __error;
1089         }
1090
1091         for (i = 0; i < 3; i++) {
1092                 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1093                 if (! request_OF_resource(np, i, name[i])) {
1094                         snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1095                         err = -ENODEV;
1096                         goto __error;
1097                 }
1098                 chip->of_requested |= (1 << i);
1099         }
1100
1101         chip->awacs = (volatile struct awacs_regs *) ioremap(np->addrs[0].address, 0x1000);
1102         chip->playback.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[1].address, 0x100);
1103         chip->capture.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[2].address, 0x100);
1104         if (chip->model <= PMAC_BURGUNDY) {
1105                 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1106                                 "PMac", (void*)chip)) {
1107                         snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1108                         err = -EBUSY;
1109                         goto __error;
1110                 }
1111                 chip->irq = np->intrs[0].line;
1112         }
1113         if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1114                         "PMac Output", (void*)chip)) {
1115                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1116                 err = -EBUSY;
1117                 goto __error;
1118         }
1119         chip->tx_irq = np->intrs[1].line;
1120         if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1121                         "PMac Input", (void*)chip)) {
1122                 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1123                 err = -EBUSY;
1124                 goto __error;
1125         }
1126         chip->rx_irq = np->intrs[2].line;
1127
1128         snd_pmac_sound_feature(chip, 1);
1129
1130         /* reset */
1131         out_le32(&chip->awacs->control, 0x11);
1132
1133         /* Powerbooks have odd ways of enabling inputs such as
1134            an expansion-bay CD or sound from an internal modem
1135            or a PC-card modem. */
1136         if (chip->is_pbook_3400) {
1137                 /* Enable CD and PC-card sound inputs. */
1138                 /* This is done by reading from address
1139                  * f301a000, + 0x10 to enable the expansion-bay
1140                  * CD sound input, + 0x80 to enable the PC-card
1141                  * sound input.  The 0x100 enables the SCSI bus
1142                  * terminator power.
1143                  */
1144                 chip->latch_base = (unsigned char *) ioremap (0xf301a000, 0x1000);
1145                 in_8(chip->latch_base + 0x190);
1146         } else if (chip->is_pbook_G3) {
1147                 struct device_node* mio;
1148                 for (mio = chip->node->parent; mio; mio = mio->parent) {
1149                         if (strcmp(mio->name, "mac-io") == 0
1150                             && mio->n_addrs > 0) {
1151                                 chip->macio_base = (unsigned char *) ioremap
1152                                         (mio->addrs[0].address, 0x40);
1153                                 break;
1154                         }
1155                 }
1156                 /* Enable CD sound input. */
1157                 /* The relevant bits for writing to this byte are 0x8f.
1158                  * I haven't found out what the 0x80 bit does.
1159                  * For the 0xf bits, writing 3 or 7 enables the CD
1160                  * input, any other value disables it.  Values
1161                  * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1162                  * 4, 6, 8 - f enable the input from the modem.
1163                  */
1164                 if (chip->macio_base)
1165                         out_8(chip->macio_base + 0x37, 3);
1166         }
1167
1168         /* Reset dbdma channels */
1169         snd_pmac_dbdma_reset(chip);
1170
1171 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1172         /* add sleep notifier */
1173         snd_pmac_register_sleep_notifier(chip);
1174         card->set_power_state = snd_pmac_set_power_state;
1175         card->power_state_private_data = 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 void snd_pmac_suspend(pmac_t *chip)
1201 {
1202         unsigned long flags;
1203         snd_card_t *card = chip->card;
1204
1205         if (card->power_state == SNDRV_CTL_POWER_D3hot)
1206                 return;
1207
1208         if (chip->suspend)
1209                 chip->suspend(chip);
1210         snd_pcm_suspend_all(chip->pcm);
1211         spin_lock_irqsave(&chip->reg_lock, flags);
1212         snd_pmac_beep_stop(chip);
1213         spin_unlock_irqrestore(&chip->reg_lock, flags);
1214         if (chip->irq >= 0)
1215                 disable_irq(chip->irq);
1216         if (chip->tx_irq >= 0)
1217                 disable_irq(chip->tx_irq);
1218         if (chip->rx_irq >= 0)
1219                 disable_irq(chip->rx_irq);
1220         snd_pmac_sound_feature(chip, 0);
1221         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1222 }
1223
1224 static void snd_pmac_resume(pmac_t *chip)
1225 {
1226         snd_card_t *card = chip->card;
1227
1228         if (card->power_state == SNDRV_CTL_POWER_D0)
1229                 return;
1230
1231         snd_pmac_sound_feature(chip, 1);
1232         if (chip->resume)
1233                 chip->resume(chip);
1234         /* enable CD sound input */
1235         if (chip->macio_base && chip->is_pbook_G3) {
1236                 out_8(chip->macio_base + 0x37, 3);
1237         } else if (chip->is_pbook_3400) {
1238                 in_8(chip->latch_base + 0x190);
1239         }
1240
1241         snd_pmac_pcm_set_format(chip);
1242
1243         if (chip->irq >= 0)
1244                 enable_irq(chip->irq);
1245         if (chip->tx_irq >= 0)
1246                 enable_irq(chip->tx_irq);
1247         if (chip->rx_irq >= 0)
1248                 enable_irq(chip->rx_irq);
1249
1250         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1251 }
1252
1253 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1254  * because we can't have any private data for notify callback.
1255  */
1256 static pmac_t *sleeping_pmac = NULL;
1257
1258 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1259 {
1260         pmac_t *chip;
1261
1262         chip = sleeping_pmac;
1263         snd_runtime_check(chip, return 0);
1264
1265         switch (when) {
1266         case PBOOK_SLEEP_NOW:
1267                 snd_pmac_suspend(chip);
1268                 break;
1269         case PBOOK_WAKE:
1270                 snd_pmac_resume(chip);
1271                 break;
1272         }
1273         return PBOOK_SLEEP_OK;
1274 }
1275
1276 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1277         snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1278 };
1279
1280 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1281 {
1282         /* should be protected here.. */
1283         if (sleeping_pmac) {
1284                 snd_printd("sleep notifier already reigistered\n");
1285                 return -EBUSY;
1286         }
1287         sleeping_pmac = chip;
1288         pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1289         chip->sleep_registered = 1;
1290         return 0;
1291 }
1292                                                     
1293 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1294 {
1295         if (! chip->sleep_registered)
1296                 return 0;
1297         /* should be protected here.. */
1298         if (sleeping_pmac != chip)
1299                 return -ENODEV;
1300         pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1301         sleeping_pmac = NULL;
1302         return 0;
1303 }
1304
1305 /* callback */
1306 static int snd_pmac_set_power_state(snd_card_t *card, unsigned int power_state)
1307 {
1308         pmac_t *chip = snd_magic_cast(pmac_t, card->power_state_private_data, return -ENXIO);
1309         switch (power_state) {
1310         case SNDRV_CTL_POWER_D0:
1311         case SNDRV_CTL_POWER_D1:
1312         case SNDRV_CTL_POWER_D2:
1313                 snd_pmac_resume(chip);
1314                 break;
1315         case SNDRV_CTL_POWER_D3hot:
1316         case SNDRV_CTL_POWER_D3cold:
1317                 snd_pmac_suspend(chip);
1318                 break;
1319         default:
1320                 return -EINVAL;
1321         }
1322         return 0;
1323 }
1324
1325 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */