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