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>
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>
32 #include <sound/pcm_params.h>
33 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
34 #include <asm/pmac_feature.h>
36 #include <asm/feature.h>
42 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
43 static int snd_pmac_register_sleep_notifier(pmac_t *chip);
44 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip);
45 static int snd_pmac_suspend(snd_card_t *card, unsigned int state);
46 static int snd_pmac_resume(snd_card_t *card, unsigned int state);
50 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
51 static int awacs_freqs[8] = {
52 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
54 /* fixed frequency table for tumbler */
55 static int tumbler_freqs[2] = {
60 * allocate DBDMA command arrays
62 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
64 rec->space = kmalloc(sizeof(struct dbdma_cmd) * (size + 1), GFP_KERNEL);
65 if (rec->space == NULL)
68 memset(rec->space, 0, sizeof(struct dbdma_cmd) * (size + 1));
69 rec->cmds = (void*)DBDMA_ALIGN(rec->space);
70 rec->addr = virt_to_bus(rec->cmds);
74 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
76 if (rec && rec->space)
86 * look up frequency table
89 static unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
94 if (rate > chip->freq_table[0])
97 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
98 if (! (ok & 1)) continue;
100 if (rate >= chip->freq_table[i])
107 * check whether another stream is active
109 static inline int another_stream(int stream)
111 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
112 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
118 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
119 snd_pcm_hw_params_t *hw_params)
121 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
127 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
129 snd_pcm_lib_free_pages(subs);
134 * get a stream of the opposite direction
136 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
139 case SNDRV_PCM_STREAM_PLAYBACK:
140 return &chip->playback;
141 case SNDRV_PCM_STREAM_CAPTURE:
142 return &chip->capture;
150 * wait while run status is on
153 snd_pmac_wait_ack(pmac_stream_t *rec)
156 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
161 * set the format and rate to the chip.
162 * call the lowlevel function if defined (e.g. for AWACS).
164 static void snd_pmac_pcm_set_format(pmac_t *chip)
166 /* set up frequency and format */
167 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
168 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
169 if (chip->set_format)
170 chip->set_format(chip);
174 * stop the DMA transfer
176 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
178 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
179 snd_pmac_wait_ack(rec);
183 * set the command pointer address
185 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
187 out_le32(&rec->dma->cmdptr, cmd->addr);
193 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
195 out_le32(&rec->dma->control, status | (status << 16));
200 * prepare playback/capture stream
202 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
205 volatile struct dbdma_cmd *cp;
207 snd_pcm_runtime_t *runtime = subs->runtime;
212 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
213 rec->period_size = snd_pcm_lib_period_bytes(subs);
214 rec->nperiods = rec->dma_size / rec->period_size;
216 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
218 /* set up constraints */
219 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
220 snd_runtime_check(astr, return -EINVAL);
221 astr->cur_freqs = 1 << rate_index;
222 astr->cur_formats = 1 << runtime->format;
223 chip->rate_index = rate_index;
224 chip->format = runtime->format;
226 /* We really want to execute a DMA stop command, after the AWACS
228 * For reasons I don't understand, it stops the hissing noise
229 * common to many PowerBook G3 systems (like mine :-).
231 spin_lock_irqsave(&chip->reg_lock, flags);
232 snd_pmac_dma_stop(rec);
233 if (rec->stream == SNDRV_PCM_STREAM_PLAYBACK) {
234 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
235 snd_pmac_dma_set_command(rec, &chip->extra_dma);
236 snd_pmac_dma_run(rec, RUN);
238 /* continuous DMA memory type doesn't provide the physical address,
239 * so we need to resolve the address here...
241 offset = virt_to_bus(runtime->dma_area);
242 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
243 st_le32(&cp->phy_addr, offset);
244 st_le16(&cp->req_count, rec->period_size);
245 /*st_le16(&cp->res_count, 0);*/
246 st_le16(&cp->xfer_status, 0);
247 offset += rec->period_size;
250 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
251 st_le32(&cp->cmd_dep, rec->cmd.addr);
253 snd_pmac_dma_stop(rec);
254 snd_pmac_dma_set_command(rec, &rec->cmd);
255 spin_unlock_irqrestore(&chip->reg_lock, flags);
264 static int snd_pmac_pcm_trigger(pmac_t *chip, pmac_stream_t *rec,
265 snd_pcm_substream_t *subs, int cmd)
268 volatile struct dbdma_cmd *cp;
272 case SNDRV_PCM_TRIGGER_START:
273 case SNDRV_PCM_TRIGGER_RESUME:
276 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
277 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
278 spin_lock_irqsave(&chip->reg_lock, flags);
279 snd_pmac_beep_stop(chip);
280 snd_pmac_pcm_set_format(chip);
281 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
282 out_le16(&cp->command, command);
283 snd_pmac_dma_set_command(rec, &rec->cmd);
284 (void)in_le32(&rec->dma->status);
285 snd_pmac_dma_run(rec, RUN|WAKE);
287 spin_unlock_irqrestore(&chip->reg_lock, flags);
290 case SNDRV_PCM_TRIGGER_STOP:
291 case SNDRV_PCM_TRIGGER_SUSPEND:
292 spin_lock_irqsave(&chip->reg_lock, flags);
294 /*printk("stopped!!\n");*/
295 snd_pmac_dma_stop(rec);
296 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
297 out_le16(&cp->command, DBDMA_STOP);
298 spin_unlock_irqrestore(&chip->reg_lock, flags);
309 * return the current pointer
312 static snd_pcm_uframes_t snd_pmac_pcm_pointer(pmac_t *chip, pmac_stream_t *rec,
313 snd_pcm_substream_t *subs)
317 #if 1 /* hmm.. how can we get the current dma pointer?? */
319 volatile struct dbdma_cmd *cp = &rec->cmd.cmds[rec->cur_period];
320 stat = ld_le16(&cp->xfer_status);
321 if (stat & (ACTIVE|DEAD)) {
322 count = in_le16(&cp->res_count);
323 count = rec->period_size - count;
326 count += rec->cur_period * rec->period_size;
327 /*printk("pointer=%d\n", count);*/
328 return bytes_to_frames(subs->runtime, count);
335 static int snd_pmac_playback_prepare(snd_pcm_substream_t *subs)
337 pmac_t *chip = snd_pcm_substream_chip(subs);
338 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
341 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
344 pmac_t *chip = snd_pcm_substream_chip(subs);
345 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
348 static snd_pcm_uframes_t snd_pmac_playback_pointer(snd_pcm_substream_t *subs)
350 pmac_t *chip = snd_pcm_substream_chip(subs);
351 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
359 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
361 pmac_t *chip = snd_pcm_substream_chip(subs);
362 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
365 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
368 pmac_t *chip = snd_pcm_substream_chip(subs);
369 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
372 static snd_pcm_uframes_t snd_pmac_capture_pointer(snd_pcm_substream_t *subs)
374 pmac_t *chip = snd_pcm_substream_chip(subs);
375 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
380 * update playback/capture pointer from interrupts
382 static void snd_pmac_pcm_update(pmac_t *chip, pmac_stream_t *rec)
384 volatile struct dbdma_cmd *cp;
388 spin_lock(&chip->reg_lock);
390 cp = &rec->cmd.cmds[rec->cur_period];
391 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
392 stat = ld_le16(&cp->xfer_status);
393 if (! (stat & ACTIVE))
395 /*printk("update frag %d\n", rec->cur_period);*/
396 st_le16(&cp->xfer_status, 0);
397 st_le16(&cp->req_count, rec->period_size);
398 /*st_le16(&cp->res_count, 0);*/
400 if (rec->cur_period >= rec->nperiods) {
405 spin_unlock(&chip->reg_lock);
406 snd_pcm_period_elapsed(rec->substream);
407 spin_lock(&chip->reg_lock);
410 spin_unlock(&chip->reg_lock);
418 static snd_pcm_hardware_t snd_pmac_playback =
420 .info = (SNDRV_PCM_INFO_INTERLEAVED |
421 SNDRV_PCM_INFO_MMAP |
422 SNDRV_PCM_INFO_MMAP_VALID |
423 SNDRV_PCM_INFO_RESUME),
424 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
425 .rates = SNDRV_PCM_RATE_8000_44100,
430 .buffer_bytes_max = 32768,
431 .period_bytes_min = 256,
432 .period_bytes_max = 16384,
434 .periods_max = PMAC_MAX_FRAGS,
437 static snd_pcm_hardware_t snd_pmac_capture =
439 .info = (SNDRV_PCM_INFO_INTERLEAVED |
440 SNDRV_PCM_INFO_MMAP |
441 SNDRV_PCM_INFO_MMAP_VALID |
442 SNDRV_PCM_INFO_RESUME),
443 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
444 .rates = SNDRV_PCM_RATE_8000_44100,
449 .buffer_bytes_max = 32768,
450 .period_bytes_min = 256,
451 .period_bytes_max = 16384,
453 .periods_max = PMAC_MAX_FRAGS,
458 static int snd_pmac_hw_rule_rate(snd_pcm_hw_params_t *params,
459 snd_pcm_hw_rule_t *rule)
461 pmac_t *chip = rule->private;
462 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
463 int i, freq_table[8], num_freqs;
465 snd_runtime_check(rec, return -EINVAL);
467 for (i = chip->num_freqs - 1; i >= 0; i--) {
468 if (rec->cur_freqs & (1 << i))
469 freq_table[num_freqs++] = chip->freq_table[i];
472 return snd_interval_list(hw_param_interval(params, rule->var),
473 num_freqs, freq_table, 0);
476 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
477 snd_pcm_hw_rule_t *rule)
479 pmac_t *chip = rule->private;
480 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
482 snd_runtime_check(rec, return -EINVAL);
483 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
488 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
490 snd_pcm_runtime_t *runtime = subs->runtime;
492 static int typical_freqs[] = {
499 static int typical_freq_flags[] = {
500 SNDRV_PCM_RATE_48000,
501 SNDRV_PCM_RATE_44100,
502 SNDRV_PCM_RATE_22050,
503 SNDRV_PCM_RATE_11025,
507 /* look up frequency table and fill bit mask */
508 runtime->hw.rates = 0;
509 fflags = chip->freqs_ok;
510 for (i = 0; typical_freqs[i]; i++) {
511 for (j = 0; j < chip->num_freqs; j++) {
512 if ((chip->freqs_ok & (1 << j)) &&
513 chip->freq_table[j] == typical_freqs[i]) {
514 runtime->hw.rates |= typical_freq_flags[i];
520 if (fflags) /* rest */
521 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
523 /* check for minimum and maximum rates */
524 for (i = 0; i < chip->num_freqs; i++) {
525 if (chip->freqs_ok & (1 << i)) {
526 runtime->hw.rate_max = chip->freq_table[i];
530 for (i = chip->num_freqs - 1; i >= 0; i--) {
531 if (chip->freqs_ok & (1 << i)) {
532 runtime->hw.rate_min = chip->freq_table[i];
536 runtime->hw.formats = chip->formats_ok;
537 if (chip->can_capture) {
538 if (! chip->can_duplex)
539 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
540 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
542 runtime->private_data = rec;
543 rec->substream = subs;
545 #if 0 /* FIXME: still under development.. */
546 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
547 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
548 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
549 snd_pmac_hw_rule_format, chip, rec->stream, -1);
552 runtime->hw.periods_max = rec->cmd.size - 1;
554 if (chip->can_duplex)
555 snd_pcm_set_sync(subs);
560 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
564 snd_pmac_dma_stop(rec);
566 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
567 snd_runtime_check(astr, return -EINVAL);
569 /* reset constraints */
570 astr->cur_freqs = chip->freqs_ok;
571 astr->cur_formats = chip->formats_ok;
576 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
578 pmac_t *chip = snd_pcm_substream_chip(subs);
580 subs->runtime->hw = snd_pmac_playback;
581 return snd_pmac_pcm_open(chip, &chip->playback, subs);
584 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
586 pmac_t *chip = snd_pcm_substream_chip(subs);
588 subs->runtime->hw = snd_pmac_capture;
589 return snd_pmac_pcm_open(chip, &chip->capture, subs);
592 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
594 pmac_t *chip = snd_pcm_substream_chip(subs);
596 return snd_pmac_pcm_close(chip, &chip->playback, subs);
599 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
601 pmac_t *chip = snd_pcm_substream_chip(subs);
603 return snd_pmac_pcm_close(chip, &chip->capture, subs);
609 static snd_pcm_ops_t snd_pmac_playback_ops = {
610 .open = snd_pmac_playback_open,
611 .close = snd_pmac_playback_close,
612 .ioctl = snd_pcm_lib_ioctl,
613 .hw_params = snd_pmac_pcm_hw_params,
614 .hw_free = snd_pmac_pcm_hw_free,
615 .prepare = snd_pmac_playback_prepare,
616 .trigger = snd_pmac_playback_trigger,
617 .pointer = snd_pmac_playback_pointer,
620 static snd_pcm_ops_t snd_pmac_capture_ops = {
621 .open = snd_pmac_capture_open,
622 .close = snd_pmac_capture_close,
623 .ioctl = snd_pcm_lib_ioctl,
624 .hw_params = snd_pmac_pcm_hw_params,
625 .hw_free = snd_pmac_pcm_hw_free,
626 .prepare = snd_pmac_capture_prepare,
627 .trigger = snd_pmac_capture_trigger,
628 .pointer = snd_pmac_capture_pointer,
631 static void pmac_pcm_free(snd_pcm_t *pcm)
633 snd_pcm_lib_preallocate_free_for_all(pcm);
636 int __init snd_pmac_pcm_new(pmac_t *chip)
640 int num_captures = 1;
642 if (! chip->can_capture)
644 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
648 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
649 if (chip->can_capture)
650 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
652 pcm->private_data = chip;
653 pcm->private_free = pmac_pcm_free;
655 strcpy(pcm->name, chip->card->shortname);
658 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
659 if (chip->can_byte_swap)
660 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
662 chip->playback.cur_formats = chip->formats_ok;
663 chip->capture.cur_formats = chip->formats_ok;
664 chip->playback.cur_freqs = chip->freqs_ok;
665 chip->capture.cur_freqs = chip->freqs_ok;
667 /* preallocate 64k buffer */
668 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
669 snd_dma_continuous_data(GFP_KERNEL),
670 64 * 1024, 64 * 1024);
676 static void snd_pmac_dbdma_reset(pmac_t *chip)
678 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
679 snd_pmac_wait_ack(&chip->playback);
680 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
681 snd_pmac_wait_ack(&chip->capture);
689 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
691 pmac_t *chip = snd_magic_cast(pmac_t, devid, return IRQ_NONE);
692 snd_pmac_pcm_update(chip, &chip->playback);
698 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
700 pmac_t *chip = snd_magic_cast(pmac_t, devid, return IRQ_NONE);
701 snd_pmac_pcm_update(chip, &chip->capture);
707 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
709 pmac_t *chip = snd_magic_cast(pmac_t, devid, return IRQ_NONE);
710 int ctrl = in_le32(&chip->awacs->control);
712 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
713 if (ctrl & MASK_PORTCHG) {
714 /* do something when headphone is plugged/unplugged? */
715 if (chip->update_automute)
716 chip->update_automute(chip, 1);
718 if (ctrl & MASK_CNTLERR) {
719 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
720 if (err && chip->model <= PMAC_SCREAMER)
721 snd_printk(KERN_DEBUG "error %x\n", err);
723 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
724 out_le32(&chip->awacs->control, ctrl);
730 * a wrapper to feature call for compatibility
732 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
733 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
735 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
736 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
738 if (chip->is_pbook_G3) {
740 feature_clear(chip->node, FEATURE_Sound_power);
741 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
742 big_mdelay(1000); /* XXX */
745 if (chip->is_pbook_3400) {
746 feature_set(chip->node, FEATURE_IOBUS_enable);
751 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
752 #define snd_pmac_sound_feature(chip,enable) /**/
753 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
759 static int snd_pmac_free(pmac_t *chip)
764 if (chip->initialized) {
765 snd_pmac_dbdma_reset(chip);
766 /* disable interrupts from awacs interface */
767 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
770 snd_pmac_sound_feature(chip, 0);
771 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
772 snd_pmac_unregister_sleep_notifier(chip);
775 /* clean up mixer if any */
776 if (chip->mixer_free)
777 chip->mixer_free(chip);
779 /* release resources */
781 free_irq(chip->irq, (void*)chip);
782 if (chip->tx_irq >= 0)
783 free_irq(chip->tx_irq, (void*)chip);
784 if (chip->rx_irq >= 0)
785 free_irq(chip->rx_irq, (void*)chip);
786 snd_pmac_dbdma_free(&chip->playback.cmd);
787 snd_pmac_dbdma_free(&chip->capture.cmd);
788 snd_pmac_dbdma_free(&chip->extra_dma);
789 if (chip->macio_base)
790 iounmap(chip->macio_base);
791 if (chip->latch_base)
792 iounmap(chip->latch_base);
794 iounmap((void*)chip->awacs);
795 if (chip->playback.dma)
796 iounmap((void*)chip->playback.dma);
797 if (chip->capture.dma)
798 iounmap((void*)chip->capture.dma);
800 for (i = 0; i < 3; i++) {
801 if (chip->of_requested & (1 << i))
802 release_OF_resource(chip->node, i);
805 snd_magic_kfree(chip);
813 static int snd_pmac_dev_free(snd_device_t *device)
815 pmac_t *chip = snd_magic_cast(pmac_t, device->device_data, return -ENXIO);
816 return snd_pmac_free(chip);
821 * check the machine support byteswap (little-endian)
824 static void __init detect_byte_swap(pmac_t *chip)
826 struct device_node *mio;
828 /* if seems that Keylargo can't byte-swap */
829 for (mio = chip->node->parent; mio; mio = mio->parent) {
830 if (strcmp(mio->name, "mac-io") == 0) {
831 if (device_is_compatible(mio, "Keylargo"))
832 chip->can_byte_swap = 0;
837 /* it seems the Pismo & iBook can't byte-swap in hardware. */
838 if (machine_is_compatible("PowerBook3,1") ||
839 machine_is_compatible("PowerBook2,1"))
840 chip->can_byte_swap = 0 ;
842 if (machine_is_compatible("PowerBook2,1"))
843 chip->can_duplex = 0;
848 * detect a sound chip
850 static int __init snd_pmac_detect(pmac_t *chip)
852 struct device_node *sound;
853 unsigned int *prop, l;
855 if (_machine != _MACH_Pmac)
860 chip->freqs_ok = 0xff; /* all ok */
861 chip->model = PMAC_AWACS;
862 chip->can_byte_swap = 1;
863 chip->can_duplex = 1;
864 chip->can_capture = 1;
866 chip->freq_table = awacs_freqs;
868 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
870 /* check machine type */
871 if (machine_is_compatible("AAPL,3400/2400")
872 || machine_is_compatible("AAPL,3500"))
873 chip->is_pbook_3400 = 1;
874 else if (machine_is_compatible("PowerBook1,1")
875 || machine_is_compatible("AAPL,PowerBook1998"))
876 chip->is_pbook_G3 = 1;
877 chip->node = find_devices("awacs");
882 * powermac G3 models have a node called "davbus"
883 * with a child called "sound".
885 chip->node = find_devices("davbus");
887 * if we didn't find a davbus device, try 'i2s-a' since
888 * this seems to be what iBooks have
891 chip->node = find_devices("i2s-a");
894 sound = find_devices("sound");
895 while (sound && sound->parent != chip->node)
899 prop = (unsigned int *) get_property(sound, "sub-frame", 0);
900 if (prop && *prop < 16)
901 chip->subframe = *prop;
902 /* This should be verified on older screamers */
903 if (device_is_compatible(sound, "screamer")) {
904 chip->model = PMAC_SCREAMER;
905 // chip->can_byte_swap = 0; /* FIXME: check this */
907 if (device_is_compatible(sound, "burgundy")) {
908 chip->model = PMAC_BURGUNDY;
909 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
911 if (device_is_compatible(sound, "daca")) {
912 chip->model = PMAC_DACA;
913 chip->can_capture = 0; /* no capture */
914 chip->can_duplex = 0;
915 // chip->can_byte_swap = 0; /* FIXME: check this */
916 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
918 if (device_is_compatible(sound, "tumbler")) {
919 chip->model = PMAC_TUMBLER;
920 chip->can_capture = 0; /* no capture */
921 chip->can_duplex = 0;
922 // chip->can_byte_swap = 0; /* FIXME: check this */
924 chip->freq_table = tumbler_freqs;
925 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
927 if (device_is_compatible(sound, "snapper")) {
928 chip->model = PMAC_SNAPPER;
929 // chip->can_byte_swap = 0; /* FIXME: check this */
931 chip->freq_table = tumbler_freqs;
932 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
934 prop = (unsigned int *)get_property(sound, "device-id", 0);
936 chip->device_id = *prop;
937 chip->has_iic = (find_devices("perch") != NULL);
939 detect_byte_swap(chip);
941 /* look for a property saying what sample rates
943 prop = (unsigned int *) get_property(sound, "sample-rates", &l);
945 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
949 for (l /= sizeof(int); l > 0; --l) {
950 unsigned int r = *prop++;
951 /* Apple 'Fixed' format */
954 for (i = 0; i < chip->num_freqs; ++i) {
955 if (r == chip->freq_table[i]) {
956 chip->freqs_ok |= (1 << i);
962 /* assume only 44.1khz */
970 * exported - boolean info callbacks for ease of programming
972 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
974 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
976 uinfo->value.integer.min = 0;
977 uinfo->value.integer.max = 1;
981 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
983 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
985 uinfo->value.integer.min = 0;
986 uinfo->value.integer.max = 1;
990 #ifdef PMAC_SUPPORT_AUTOMUTE
994 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
996 pmac_t *chip = snd_kcontrol_chip(kcontrol);
997 ucontrol->value.integer.value[0] = chip->auto_mute;
1001 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1003 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1004 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1005 chip->auto_mute = ucontrol->value.integer.value[0];
1006 if (chip->update_automute)
1007 chip->update_automute(chip, 1);
1013 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1015 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1016 if (chip->detect_headphone)
1017 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1019 ucontrol->value.integer.value[0] = 0;
1023 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1024 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1025 .name = "Auto Mute Switch",
1026 .info = snd_pmac_boolean_mono_info,
1027 .get = pmac_auto_mute_get,
1028 .put = pmac_auto_mute_put,
1030 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1031 .name = "Headphone Detection",
1032 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1033 .info = snd_pmac_boolean_mono_info,
1034 .get = pmac_hp_detect_get,
1038 int __init snd_pmac_add_automute(pmac_t *chip)
1041 chip->auto_mute = 1;
1042 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1045 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1046 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1048 #endif /* PMAC_SUPPORT_AUTOMUTE */
1051 * create and detect a pmac chip record
1053 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1056 struct device_node *np;
1058 static snd_device_ops_t ops = {
1059 .dev_free = snd_pmac_dev_free,
1062 snd_runtime_check(chip_return, return -EINVAL);
1063 *chip_return = NULL;
1065 chip = snd_magic_kcalloc(pmac_t, 0, GFP_KERNEL);
1070 spin_lock_init(&chip->reg_lock);
1071 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1073 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1074 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1076 if ((err = snd_pmac_detect(chip)) < 0)
1079 if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1080 snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1081 snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1087 if (np->n_addrs < 3 || np->n_intrs < 3) {
1092 for (i = 0; i < 3; i++) {
1093 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1094 if (! request_OF_resource(np, i, name[i])) {
1095 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1099 chip->of_requested |= (1 << i);
1102 chip->awacs = (volatile struct awacs_regs *) ioremap(np->addrs[0].address, 0x1000);
1103 chip->playback.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[1].address, 0x100);
1104 chip->capture.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[2].address, 0x100);
1105 if (chip->model <= PMAC_BURGUNDY) {
1106 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1107 "PMac", (void*)chip)) {
1108 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1112 chip->irq = np->intrs[0].line;
1114 if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1115 "PMac Output", (void*)chip)) {
1116 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1120 chip->tx_irq = np->intrs[1].line;
1121 if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1122 "PMac Input", (void*)chip)) {
1123 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1127 chip->rx_irq = np->intrs[2].line;
1129 snd_pmac_sound_feature(chip, 1);
1132 out_le32(&chip->awacs->control, 0x11);
1134 /* Powerbooks have odd ways of enabling inputs such as
1135 an expansion-bay CD or sound from an internal modem
1136 or a PC-card modem. */
1137 if (chip->is_pbook_3400) {
1138 /* Enable CD and PC-card sound inputs. */
1139 /* This is done by reading from address
1140 * f301a000, + 0x10 to enable the expansion-bay
1141 * CD sound input, + 0x80 to enable the PC-card
1142 * sound input. The 0x100 enables the SCSI bus
1145 chip->latch_base = (unsigned char *) ioremap (0xf301a000, 0x1000);
1146 in_8(chip->latch_base + 0x190);
1147 } else if (chip->is_pbook_G3) {
1148 struct device_node* mio;
1149 for (mio = chip->node->parent; mio; mio = mio->parent) {
1150 if (strcmp(mio->name, "mac-io") == 0
1151 && mio->n_addrs > 0) {
1152 chip->macio_base = (unsigned char *) ioremap
1153 (mio->addrs[0].address, 0x40);
1157 /* Enable CD sound input. */
1158 /* The relevant bits for writing to this byte are 0x8f.
1159 * I haven't found out what the 0x80 bit does.
1160 * For the 0xf bits, writing 3 or 7 enables the CD
1161 * input, any other value disables it. Values
1162 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1163 * 4, 6, 8 - f enable the input from the modem.
1165 if (chip->macio_base)
1166 out_8(chip->macio_base + 0x37, 3);
1169 /* Reset dbdma channels */
1170 snd_pmac_dbdma_reset(chip);
1172 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1173 /* add sleep notifier */
1174 if (! snd_pmac_register_sleep_notifier(chip))
1175 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1178 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1181 *chip_return = chip;
1185 snd_pmac_free(chip);
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 int snd_pmac_suspend(snd_card_t *card, unsigned int state)
1202 pmac_t *chip = snd_magic_cast(pmac_t, card->pm_private_data, return -EINVAL);
1203 unsigned long flags;
1206 chip->suspend(chip);
1207 snd_pcm_suspend_all(chip->pcm);
1208 spin_lock_irqsave(&chip->reg_lock, flags);
1209 snd_pmac_beep_stop(chip);
1210 spin_unlock_irqrestore(&chip->reg_lock, flags);
1212 disable_irq(chip->irq);
1213 if (chip->tx_irq >= 0)
1214 disable_irq(chip->tx_irq);
1215 if (chip->rx_irq >= 0)
1216 disable_irq(chip->rx_irq);
1217 snd_pmac_sound_feature(chip, 0);
1218 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1222 static int snd_pmac_resume(snd_card_t *card, unsigned int state)
1224 pmac_t *chip = snd_magic_cast(pmac_t, card->pm_private_data, return -EINVAL);
1226 snd_pmac_sound_feature(chip, 1);
1229 /* enable CD sound input */
1230 if (chip->macio_base && chip->is_pbook_G3) {
1231 out_8(chip->macio_base + 0x37, 3);
1232 } else if (chip->is_pbook_3400) {
1233 in_8(chip->latch_base + 0x190);
1236 snd_pmac_pcm_set_format(chip);
1239 enable_irq(chip->irq);
1240 if (chip->tx_irq >= 0)
1241 enable_irq(chip->tx_irq);
1242 if (chip->rx_irq >= 0)
1243 enable_irq(chip->rx_irq);
1245 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1249 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1250 * because we can't have any private data for notify callback.
1252 static pmac_t *sleeping_pmac = NULL;
1254 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1258 chip = sleeping_pmac;
1259 snd_runtime_check(chip, return 0);
1262 case PBOOK_SLEEP_NOW:
1263 snd_pmac_suspend(chip->card, 0);
1266 snd_pmac_resume(chip->card, 0);
1269 return PBOOK_SLEEP_OK;
1272 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1273 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1276 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1278 /* should be protected here.. */
1279 snd_assert(! sleeping_pmac, return -EBUSY);
1280 sleeping_pmac = chip;
1281 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1285 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1287 /* should be protected here.. */
1288 snd_assert(sleeping_pmac == chip, return -ENODEV);
1289 pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1290 sleeping_pmac = NULL;
1294 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */