Get rid of arch/mips64/kernel. 9116 lines of code gone.
[linux-2.6/linux-mips.git] / sound / ppc / pmac.c
blob842bbacb6fd486109161411c2a35ea1034ed9650
1 /*
2 * PMac DBDMA lowlevel functions
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 * code based on dmasound.c.
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.
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.
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
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
39 #define chip_t pmac_t
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
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
53 /* fixed frequency table for tumbler */
54 static int tumbler_freqs[2] = {
55 48000, 44100
59 * allocate DBDMA command arrays
61 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
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;
73 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
75 if (rec && rec->space)
76 kfree(rec->space);
81 * pcm stuff
85 * look up frequency table
88 static unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
90 int i, ok, found;
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;
102 return found;
106 * check whether another stream is active
108 static inline int another_stream(int stream)
110 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
111 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
115 * allocate buffers
117 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
118 snd_pcm_hw_params_t *hw_params)
120 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
124 * release buffers
126 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
128 snd_pcm_lib_free_pages(subs);
129 return 0;
133 * get a stream of the opposite direction
135 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
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;
149 * wait while run status is on
151 inline static void
152 snd_pmac_wait_ack(pmac_stream_t *rec)
154 int timeout = 50000;
155 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
156 udelay(1);
160 * set the format and rate to the chip.
161 * call the lowlevel function if defined (e.g. for AWACS).
163 static void snd_pmac_pcm_set_format(pmac_t *chip)
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);
173 * stop the DMA transfer
175 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
177 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
178 snd_pmac_wait_ack(rec);
182 * set the command pointer address
184 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
186 out_le32(&rec->dma->cmdptr, cmd->addr);
190 * start the DMA
192 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
194 out_le32(&rec->dma->control, status | (status << 16));
199 * prepare playback/capture stream
201 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
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;
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);
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;
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 :-).
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);
237 /* continuous DMA memory type doesn't provide the physical address,
238 * so we need to resolve the address here...
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;
248 /* make loop */
249 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
250 st_le32(&cp->cmd_dep, rec->cmd.addr);
252 snd_pmac_dma_stop(rec);
253 snd_pmac_dma_set_command(rec, &rec->cmd);
254 spin_unlock_irqrestore(&chip->reg_lock, flags);
256 return 0;
261 * PCM trigger/stop
263 static int snd_pmac_pcm_trigger(pmac_t *chip, pmac_stream_t *rec,
264 snd_pcm_substream_t *subs, int cmd)
266 unsigned long flags;
267 volatile struct dbdma_cmd *cp;
268 int i, command;
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;
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;
300 default:
301 return -EINVAL;
304 return 0;
308 * return the current pointer
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)
314 int count = 0;
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;
324 #endif
325 count += rec->cur_period * rec->period_size;
326 /*printk("pointer=%d\n", count);*/
327 return bytes_to_frames(subs->runtime, count);
331 * playback
334 static int snd_pmac_playback_prepare(snd_pcm_substream_t *subs)
336 pmac_t *chip = snd_pcm_substream_chip(subs);
337 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
340 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
341 int cmd)
343 pmac_t *chip = snd_pcm_substream_chip(subs);
344 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
347 static snd_pcm_uframes_t snd_pmac_playback_pointer(snd_pcm_substream_t *subs)
349 pmac_t *chip = snd_pcm_substream_chip(subs);
350 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
355 * capture
358 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
360 pmac_t *chip = snd_pcm_substream_chip(subs);
361 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
364 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
365 int cmd)
367 pmac_t *chip = snd_pcm_substream_chip(subs);
368 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
371 static snd_pcm_uframes_t snd_pmac_capture_pointer(snd_pcm_substream_t *subs)
373 pmac_t *chip = snd_pcm_substream_chip(subs);
374 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
379 * update playback/capture pointer from interrupts
381 static void snd_pmac_pcm_update(pmac_t *chip, pmac_stream_t *rec)
383 volatile struct dbdma_cmd *cp;
384 int c;
385 int stat;
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);
409 spin_unlock(&chip->reg_lock);
414 * hw info
417 static snd_pcm_hardware_t snd_pmac_playback =
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,
436 static snd_pcm_hardware_t snd_pmac_capture =
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,
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)
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;
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];
471 return snd_interval_list(hw_param_interval(params, rule->var),
472 num_freqs, freq_table, 0);
475 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
476 snd_pcm_hw_rule_t *rule)
478 pmac_t *chip = rule->private;
479 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
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);
485 #endif // NYI
487 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
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,
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,
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;
519 if (fflags) /* rest */
520 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
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;
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;
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;
541 runtime->private_data = rec;
542 rec->substream = subs;
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
551 runtime->hw.periods_max = rec->cmd.size - 1;
553 if (chip->can_duplex)
554 snd_pcm_set_sync(subs);
556 return 0;
559 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
561 pmac_stream_t *astr;
563 snd_pmac_dma_stop(rec);
565 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
566 snd_runtime_check(astr, return -EINVAL);
568 /* reset constraints */
569 astr->cur_freqs = chip->freqs_ok;
570 astr->cur_formats = chip->formats_ok;
572 return 0;
575 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
577 pmac_t *chip = snd_pcm_substream_chip(subs);
579 subs->runtime->hw = snd_pmac_playback;
580 return snd_pmac_pcm_open(chip, &chip->playback, subs);
583 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
585 pmac_t *chip = snd_pcm_substream_chip(subs);
587 subs->runtime->hw = snd_pmac_capture;
588 return snd_pmac_pcm_open(chip, &chip->capture, subs);
591 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
593 pmac_t *chip = snd_pcm_substream_chip(subs);
595 return snd_pmac_pcm_close(chip, &chip->playback, subs);
598 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
600 pmac_t *chip = snd_pcm_substream_chip(subs);
602 return snd_pmac_pcm_close(chip, &chip->capture, subs);
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,
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,
630 static void pmac_pcm_free(snd_pcm_t *pcm)
632 snd_pcm_lib_preallocate_free_for_all(pcm);
635 int __init snd_pmac_pcm_new(pmac_t *chip)
637 snd_pcm_t *pcm;
638 int err;
639 int num_captures = 1;
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;
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);
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;
657 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
658 if (chip->can_byte_swap)
659 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
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;
666 /* preallocate 64k buffer */
667 snd_pcm_lib_preallocate_pages_for_all(pcm, 64 * 1024, 64 * 1024, GFP_KERNEL);
669 return 0;
673 static void snd_pmac_dbdma_reset(pmac_t *chip)
675 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
676 snd_pmac_wait_ack(&chip->playback);
677 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
678 snd_pmac_wait_ack(&chip->capture);
683 * interrupt handlers
685 static irqreturn_t
686 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
688 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
689 snd_pmac_pcm_update(chip, &chip->playback);
690 return IRQ_HANDLED;
694 static irqreturn_t
695 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
697 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
698 snd_pmac_pcm_update(chip, &chip->capture);
699 return IRQ_HANDLED;
703 static irqreturn_t
704 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
706 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
707 int ctrl = in_le32(&chip->awacs->control);
709 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
710 if (ctrl & MASK_PORTCHG) {
711 /* do something when headphone is plugged/unplugged? */
712 if (chip->update_automute)
713 chip->update_automute(chip, 1);
715 if (ctrl & MASK_CNTLERR) {
716 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
717 if (err && chip->model <= PMAC_SCREAMER)
718 snd_printk(KERN_DEBUG "error %x\n", err);
720 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
721 out_le32(&chip->awacs->control, ctrl);
722 return IRQ_HANDLED;
727 * a wrapper to feature call for compatibility
729 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
730 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
732 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
733 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
734 #else
735 if (chip->is_pbook_G3) {
736 pmu_suspend();
737 feature_clear(chip->node, FEATURE_Sound_power);
738 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
739 mdelay(1000); /* XXX */
740 pmu_resume();
742 if (chip->is_pbook_3400) {
743 feature_set(chip->node, FEATURE_IOBUS_enable);
744 udelay(10);
746 #endif
748 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
749 #define snd_pmac_sound_feature(chip,enable) /**/
750 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
753 * release resources
756 static int snd_pmac_free(pmac_t *chip)
758 int i;
760 /* stop sounds */
761 if (chip->initialized) {
762 snd_pmac_dbdma_reset(chip);
763 /* disable interrupts from awacs interface */
764 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
767 snd_pmac_sound_feature(chip, 0);
768 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
769 snd_pmac_unregister_sleep_notifier(chip);
770 #endif
772 /* clean up mixer if any */
773 if (chip->mixer_free)
774 chip->mixer_free(chip);
776 /* release resources */
777 if (chip->irq >= 0)
778 free_irq(chip->irq, (void*)chip);
779 if (chip->tx_irq >= 0)
780 free_irq(chip->tx_irq, (void*)chip);
781 if (chip->rx_irq >= 0)
782 free_irq(chip->rx_irq, (void*)chip);
783 snd_pmac_dbdma_free(&chip->playback.cmd);
784 snd_pmac_dbdma_free(&chip->capture.cmd);
785 snd_pmac_dbdma_free(&chip->extra_dma);
786 if (chip->macio_base)
787 iounmap(chip->macio_base);
788 if (chip->latch_base)
789 iounmap(chip->latch_base);
790 if (chip->awacs)
791 iounmap((void*)chip->awacs);
792 if (chip->playback.dma)
793 iounmap((void*)chip->playback.dma);
794 if (chip->capture.dma)
795 iounmap((void*)chip->capture.dma);
796 if (chip->node) {
797 for (i = 0; i < 3; i++) {
798 if (chip->of_requested & (1 << i))
799 release_OF_resource(chip->node, i);
802 snd_magic_kfree(chip);
803 return 0;
808 * free the device
810 static int snd_pmac_dev_free(snd_device_t *device)
812 pmac_t *chip = snd_magic_cast(pmac_t, device->device_data, return -ENXIO);
813 return snd_pmac_free(chip);
818 * check the machine support byteswap (little-endian)
821 static void __init detect_byte_swap(pmac_t *chip)
823 struct device_node *mio;
825 /* if seems that Keylargo can't byte-swap */
826 for (mio = chip->node->parent; mio; mio = mio->parent) {
827 if (strcmp(mio->name, "mac-io") == 0) {
828 if (device_is_compatible(mio, "Keylargo"))
829 chip->can_byte_swap = 0;
830 break;
834 /* it seems the Pismo & iBook can't byte-swap in hardware. */
835 if (machine_is_compatible("PowerBook3,1") ||
836 machine_is_compatible("PowerBook2,1"))
837 chip->can_byte_swap = 0 ;
839 if (machine_is_compatible("PowerBook2,1"))
840 chip->can_duplex = 0;
845 * detect a sound chip
847 static int __init snd_pmac_detect(pmac_t *chip)
849 struct device_node *sound;
850 unsigned int *prop, l;
852 if (_machine != _MACH_Pmac)
853 return -ENODEV;
855 chip->subframe = 0;
856 chip->revision = 0;
857 chip->freqs_ok = 0xff; /* all ok */
858 chip->model = PMAC_AWACS;
859 chip->can_byte_swap = 1;
860 chip->can_duplex = 1;
861 chip->can_capture = 1;
862 chip->num_freqs = 8;
863 chip->freq_table = awacs_freqs;
865 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
867 /* check machine type */
868 if (machine_is_compatible("AAPL,3400/2400")
869 || machine_is_compatible("AAPL,3500"))
870 chip->is_pbook_3400 = 1;
871 else if (machine_is_compatible("PowerBook1,1")
872 || machine_is_compatible("AAPL,PowerBook1998"))
873 chip->is_pbook_G3 = 1;
874 chip->node = find_devices("awacs");
875 if (chip->node)
876 return 0; /* ok */
879 * powermac G3 models have a node called "davbus"
880 * with a child called "sound".
882 chip->node = find_devices("davbus");
884 * if we didn't find a davbus device, try 'i2s-a' since
885 * this seems to be what iBooks have
887 if (! chip->node)
888 chip->node = find_devices("i2s-a");
889 if (! chip->node)
890 return -ENODEV;
891 sound = find_devices("sound");
892 while (sound && sound->parent != chip->node)
893 sound = sound->next;
894 if (! sound)
895 return -ENODEV;
896 prop = (unsigned int *) get_property(sound, "sub-frame", 0);
897 if (prop && *prop < 16)
898 chip->subframe = *prop;
899 /* This should be verified on older screamers */
900 if (device_is_compatible(sound, "screamer")) {
901 chip->model = PMAC_SCREAMER;
902 // chip->can_byte_swap = 0; /* FIXME: check this */
904 if (device_is_compatible(sound, "burgundy")) {
905 chip->model = PMAC_BURGUNDY;
906 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
908 if (device_is_compatible(sound, "daca")) {
909 chip->model = PMAC_DACA;
910 chip->can_capture = 0; /* no capture */
911 chip->can_duplex = 0;
912 // chip->can_byte_swap = 0; /* FIXME: check this */
913 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
915 if (device_is_compatible(sound, "tumbler")) {
916 chip->model = PMAC_TUMBLER;
917 chip->can_capture = 0; /* no capture */
918 chip->can_duplex = 0;
919 // chip->can_byte_swap = 0; /* FIXME: check this */
920 chip->num_freqs = 2;
921 chip->freq_table = tumbler_freqs;
922 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
924 if (device_is_compatible(sound, "snapper")) {
925 chip->model = PMAC_SNAPPER;
926 chip->can_capture = 0; /* no capture */
927 chip->can_duplex = 0;
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 */
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);
938 detect_byte_swap(chip);
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;
960 } else {
961 /* assume only 44.1khz */
962 chip->freqs_ok = 1;
965 return 0;
969 * exported - boolean info callbacks for ease of programming
971 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
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;
980 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
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;
989 #ifdef PMAC_SUPPORT_AUTOMUTE
991 * auto-mute
993 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
995 pmac_t *chip = snd_kcontrol_chip(kcontrol);
996 ucontrol->value.integer.value[0] = chip->auto_mute;
997 return 0;
1000 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
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;
1009 return 0;
1012 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
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;
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,
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,
1037 int __init snd_pmac_add_automute(pmac_t *chip)
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);
1047 #endif /* PMAC_SUPPORT_AUTOMUTE */
1050 * create and detect a pmac chip record
1052 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
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,
1061 snd_runtime_check(chip_return, return -EINVAL);
1062 *chip_return = NULL;
1064 chip = snd_magic_kcalloc(pmac_t, 0, GFP_KERNEL);
1065 if (chip == NULL)
1066 return -ENOMEM;
1067 chip->card = card;
1069 spin_lock_init(&chip->reg_lock);
1070 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1072 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1073 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1075 if ((err = snd_pmac_detect(chip)) < 0)
1076 goto __error;
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;
1085 np = chip->node;
1086 if (np->n_addrs < 3 || np->n_intrs < 3) {
1087 err = -ENODEV;
1088 goto __error;
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;
1098 chip->of_requested |= (1 << i);
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;
1111 chip->irq = np->intrs[0].line;
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;
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;
1126 chip->rx_irq = np->intrs[2].line;
1128 snd_pmac_sound_feature(chip, 1);
1130 /* reset */
1131 out_le32(&chip->awacs->control, 0x11);
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.
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;
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.
1164 if (chip->macio_base)
1165 out_8(chip->macio_base + 0x37, 3);
1168 /* Reset dbdma channels */
1169 snd_pmac_dbdma_reset(chip);
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
1178 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1179 goto __error;
1181 *chip_return = chip;
1182 return 0;
1184 __error:
1185 snd_pmac_free(chip);
1186 return err;
1191 * sleep notify for powerbook
1194 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1197 * Save state when going to sleep, restore it afterwards.
1200 static void snd_pmac_suspend(pmac_t *chip)
1202 unsigned long flags;
1203 snd_card_t *card = chip->card;
1205 if (card->power_state == SNDRV_CTL_POWER_D3hot)
1206 return;
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);
1224 static void snd_pmac_resume(pmac_t *chip)
1226 snd_card_t *card = chip->card;
1228 if (card->power_state == SNDRV_CTL_POWER_D0)
1229 return;
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);
1241 snd_pmac_pcm_set_format(chip);
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);
1250 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1253 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1254 * because we can't have any private data for notify callback.
1256 static pmac_t *sleeping_pmac = NULL;
1258 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1260 pmac_t *chip;
1262 chip = sleeping_pmac;
1263 snd_runtime_check(chip, return 0);
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;
1273 return PBOOK_SLEEP_OK;
1276 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1277 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1280 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1282 /* should be protected here.. */
1283 if (sleeping_pmac) {
1284 snd_printd("sleep notifier already reigistered\n");
1285 return -EBUSY;
1287 sleeping_pmac = chip;
1288 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1289 chip->sleep_registered = 1;
1290 return 0;
1293 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
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;
1305 /* callback */
1306 static int snd_pmac_set_power_state(snd_card_t *card, unsigned int power_state)
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;
1322 return 0;
1325 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */