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
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/pci.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
33 #include <sound/pcm_params.h>
34 #include <asm/pmac_feature.h>
35 #include <asm/pci-bridge.h>
38 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
39 static int awacs_freqs[8] = {
40 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
42 /* fixed frequency table for tumbler */
43 static int tumbler_freqs[1] = {
48 * allocate DBDMA command arrays
50 static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
52 unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
54 rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
55 &rec->dma_base, GFP_KERNEL);
56 if (rec->space == NULL)
59 memset(rec->space, 0, rsize);
60 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
61 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
66 static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
69 unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
71 dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
81 * look up frequency table
84 unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
89 if (rate > chip->freq_table[0])
92 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
93 if (! (ok & 1)) continue;
95 if (rate >= chip->freq_table[i])
102 * check whether another stream is active
104 static inline int another_stream(int stream)
106 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
107 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
113 static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
114 struct snd_pcm_hw_params *hw_params)
116 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
122 static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
124 snd_pcm_lib_free_pages(subs);
129 * get a stream of the opposite direction
131 static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
134 case SNDRV_PCM_STREAM_PLAYBACK:
135 return &chip->playback;
136 case SNDRV_PCM_STREAM_CAPTURE:
137 return &chip->capture;
145 * wait while run status is on
148 snd_pmac_wait_ack(struct pmac_stream *rec)
151 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
156 * set the format and rate to the chip.
157 * call the lowlevel function if defined (e.g. for AWACS).
159 static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
161 /* set up frequency and format */
162 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
163 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
164 if (chip->set_format)
165 chip->set_format(chip);
169 * stop the DMA transfer
171 static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
173 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
174 snd_pmac_wait_ack(rec);
178 * set the command pointer address
180 static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
182 out_le32(&rec->dma->cmdptr, cmd->addr);
188 static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
190 out_le32(&rec->dma->control, status | (status << 16));
195 * prepare playback/capture stream
197 static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
200 volatile struct dbdma_cmd __iomem *cp;
201 struct snd_pcm_runtime *runtime = subs->runtime;
204 struct pmac_stream *astr;
206 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
207 rec->period_size = snd_pcm_lib_period_bytes(subs);
208 rec->nperiods = rec->dma_size / rec->period_size;
210 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
212 /* set up constraints */
213 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
216 astr->cur_freqs = 1 << rate_index;
217 astr->cur_formats = 1 << runtime->format;
218 chip->rate_index = rate_index;
219 chip->format = runtime->format;
221 /* We really want to execute a DMA stop command, after the AWACS
223 * For reasons I don't understand, it stops the hissing noise
224 * common to many PowerBook G3 systems and random noise otherwise
225 * captured on iBook2's about every third time. -ReneR
227 spin_lock_irq(&chip->reg_lock);
228 snd_pmac_dma_stop(rec);
229 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
230 snd_pmac_dma_set_command(rec, &chip->extra_dma);
231 snd_pmac_dma_run(rec, RUN);
232 spin_unlock_irq(&chip->reg_lock);
234 spin_lock_irq(&chip->reg_lock);
235 /* continuous DMA memory type doesn't provide the physical address,
236 * so we need to resolve the address here...
238 offset = runtime->dma_addr;
239 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
240 st_le32(&cp->phy_addr, offset);
241 st_le16(&cp->req_count, rec->period_size);
242 /*st_le16(&cp->res_count, 0);*/
243 st_le16(&cp->xfer_status, 0);
244 offset += rec->period_size;
247 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
248 st_le32(&cp->cmd_dep, rec->cmd.addr);
250 snd_pmac_dma_stop(rec);
251 snd_pmac_dma_set_command(rec, &rec->cmd);
252 spin_unlock_irq(&chip->reg_lock);
261 static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
262 struct snd_pcm_substream *subs, int cmd)
264 volatile struct dbdma_cmd __iomem *cp;
268 case SNDRV_PCM_TRIGGER_START:
269 case SNDRV_PCM_TRIGGER_RESUME:
272 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
273 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
274 spin_lock(&chip->reg_lock);
275 snd_pmac_beep_stop(chip);
276 snd_pmac_pcm_set_format(chip);
277 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
278 out_le16(&cp->command, command);
279 snd_pmac_dma_set_command(rec, &rec->cmd);
280 (void)in_le32(&rec->dma->status);
281 snd_pmac_dma_run(rec, RUN|WAKE);
283 spin_unlock(&chip->reg_lock);
286 case SNDRV_PCM_TRIGGER_STOP:
287 case SNDRV_PCM_TRIGGER_SUSPEND:
288 spin_lock(&chip->reg_lock);
290 /*printk("stopped!!\n");*/
291 snd_pmac_dma_stop(rec);
292 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
293 out_le16(&cp->command, DBDMA_STOP);
294 spin_unlock(&chip->reg_lock);
305 * return the current pointer
308 static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
309 struct pmac_stream *rec,
310 struct snd_pcm_substream *subs)
314 #if 1 /* hmm.. how can we get the current dma pointer?? */
316 volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
317 stat = ld_le16(&cp->xfer_status);
318 if (stat & (ACTIVE|DEAD)) {
319 count = in_le16(&cp->res_count);
321 count = rec->period_size - count;
324 count += rec->cur_period * rec->period_size;
325 /*printk("pointer=%d\n", count);*/
326 return bytes_to_frames(subs->runtime, count);
333 static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
335 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
336 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
339 static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
342 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
343 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
346 static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
348 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
349 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
357 static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
359 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
360 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
363 static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
366 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
367 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
370 static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
372 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
373 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
378 * update playback/capture pointer from interrupts
380 static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
382 volatile struct dbdma_cmd __iomem *cp;
386 spin_lock(&chip->reg_lock);
388 cp = &rec->cmd.cmds[rec->cur_period];
389 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
390 stat = ld_le16(&cp->xfer_status);
391 if (! (stat & ACTIVE))
393 /*printk("update frag %d\n", rec->cur_period);*/
394 st_le16(&cp->xfer_status, 0);
395 st_le16(&cp->req_count, rec->period_size);
396 /*st_le16(&cp->res_count, 0);*/
398 if (rec->cur_period >= rec->nperiods) {
403 spin_unlock(&chip->reg_lock);
404 snd_pcm_period_elapsed(rec->substream);
405 spin_lock(&chip->reg_lock);
408 spin_unlock(&chip->reg_lock);
416 static struct snd_pcm_hardware snd_pmac_playback =
418 .info = (SNDRV_PCM_INFO_INTERLEAVED |
419 SNDRV_PCM_INFO_MMAP |
420 SNDRV_PCM_INFO_MMAP_VALID |
421 SNDRV_PCM_INFO_RESUME),
422 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
423 .rates = SNDRV_PCM_RATE_8000_44100,
428 .buffer_bytes_max = 131072,
429 .period_bytes_min = 256,
430 .period_bytes_max = 16384,
432 .periods_max = PMAC_MAX_FRAGS,
435 static struct snd_pcm_hardware snd_pmac_capture =
437 .info = (SNDRV_PCM_INFO_INTERLEAVED |
438 SNDRV_PCM_INFO_MMAP |
439 SNDRV_PCM_INFO_MMAP_VALID |
440 SNDRV_PCM_INFO_RESUME),
441 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
442 .rates = SNDRV_PCM_RATE_8000_44100,
447 .buffer_bytes_max = 131072,
448 .period_bytes_min = 256,
449 .period_bytes_max = 16384,
451 .periods_max = PMAC_MAX_FRAGS,
456 static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
457 struct snd_pcm_hw_rule *rule)
459 struct snd_pmac *chip = rule->private;
460 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
461 int i, freq_table[8], num_freqs;
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(struct snd_pcm_hw_params *params,
476 struct snd_pcm_hw_rule *rule)
478 struct snd_pmac *chip = rule->private;
479 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
483 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
488 static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
489 struct snd_pcm_substream *subs)
491 struct snd_pcm_runtime *runtime = subs->runtime;
494 /* look up frequency table and fill bit mask */
495 runtime->hw.rates = 0;
496 for (i = 0; i < chip->num_freqs; i++)
497 if (chip->freqs_ok & (1 << i))
499 snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
501 /* check for minimum and maximum rates */
502 for (i = 0; i < chip->num_freqs; i++) {
503 if (chip->freqs_ok & (1 << i)) {
504 runtime->hw.rate_max = chip->freq_table[i];
508 for (i = chip->num_freqs - 1; i >= 0; i--) {
509 if (chip->freqs_ok & (1 << i)) {
510 runtime->hw.rate_min = chip->freq_table[i];
514 runtime->hw.formats = chip->formats_ok;
515 if (chip->can_capture) {
516 if (! chip->can_duplex)
517 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
518 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
520 runtime->private_data = rec;
521 rec->substream = subs;
523 #if 0 /* FIXME: still under development.. */
524 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
525 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
526 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
527 snd_pmac_hw_rule_format, chip, rec->stream, -1);
530 runtime->hw.periods_max = rec->cmd.size - 1;
532 /* constraints to fix choppy sound */
533 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
537 static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
538 struct snd_pcm_substream *subs)
540 struct pmac_stream *astr;
542 snd_pmac_dma_stop(rec);
544 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
548 /* reset constraints */
549 astr->cur_freqs = chip->freqs_ok;
550 astr->cur_formats = chip->formats_ok;
555 static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
557 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
559 subs->runtime->hw = snd_pmac_playback;
560 return snd_pmac_pcm_open(chip, &chip->playback, subs);
563 static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
565 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
567 subs->runtime->hw = snd_pmac_capture;
568 return snd_pmac_pcm_open(chip, &chip->capture, subs);
571 static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
573 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
575 return snd_pmac_pcm_close(chip, &chip->playback, subs);
578 static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
580 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
582 return snd_pmac_pcm_close(chip, &chip->capture, subs);
588 static struct snd_pcm_ops snd_pmac_playback_ops = {
589 .open = snd_pmac_playback_open,
590 .close = snd_pmac_playback_close,
591 .ioctl = snd_pcm_lib_ioctl,
592 .hw_params = snd_pmac_pcm_hw_params,
593 .hw_free = snd_pmac_pcm_hw_free,
594 .prepare = snd_pmac_playback_prepare,
595 .trigger = snd_pmac_playback_trigger,
596 .pointer = snd_pmac_playback_pointer,
599 static struct snd_pcm_ops snd_pmac_capture_ops = {
600 .open = snd_pmac_capture_open,
601 .close = snd_pmac_capture_close,
602 .ioctl = snd_pcm_lib_ioctl,
603 .hw_params = snd_pmac_pcm_hw_params,
604 .hw_free = snd_pmac_pcm_hw_free,
605 .prepare = snd_pmac_capture_prepare,
606 .trigger = snd_pmac_capture_trigger,
607 .pointer = snd_pmac_capture_pointer,
610 int __init snd_pmac_pcm_new(struct snd_pmac *chip)
614 int num_captures = 1;
616 if (! chip->can_capture)
618 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
622 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
623 if (chip->can_capture)
624 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
626 pcm->private_data = chip;
627 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
628 strcpy(pcm->name, chip->card->shortname);
631 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
632 if (chip->can_byte_swap)
633 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
635 chip->playback.cur_formats = chip->formats_ok;
636 chip->capture.cur_formats = chip->formats_ok;
637 chip->playback.cur_freqs = chip->freqs_ok;
638 chip->capture.cur_freqs = chip->freqs_ok;
640 /* preallocate 64k buffer */
641 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
643 64 * 1024, 64 * 1024);
649 static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
651 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
652 snd_pmac_wait_ack(&chip->playback);
653 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
654 snd_pmac_wait_ack(&chip->capture);
661 void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
663 struct pmac_stream *rec = &chip->playback;
665 snd_pmac_dma_stop(rec);
666 st_le16(&chip->extra_dma.cmds->req_count, bytes);
667 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
668 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
669 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
670 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
671 out_le32(&chip->awacs->control,
672 (in_le32(&chip->awacs->control) & ~0x1f00)
674 out_le32(&chip->awacs->byteswap, 0);
675 snd_pmac_dma_set_command(rec, &chip->extra_dma);
676 snd_pmac_dma_run(rec, RUN);
679 void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
681 snd_pmac_dma_stop(&chip->playback);
682 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
683 snd_pmac_pcm_set_format(chip); /* reset format */
691 snd_pmac_tx_intr(int irq, void *devid)
693 struct snd_pmac *chip = devid;
694 snd_pmac_pcm_update(chip, &chip->playback);
700 snd_pmac_rx_intr(int irq, void *devid)
702 struct snd_pmac *chip = devid;
703 snd_pmac_pcm_update(chip, &chip->capture);
709 snd_pmac_ctrl_intr(int irq, void *devid)
711 struct snd_pmac *chip = devid;
712 int ctrl = in_le32(&chip->awacs->control);
714 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
715 if (ctrl & MASK_PORTCHG) {
716 /* do something when headphone is plugged/unplugged? */
717 if (chip->update_automute)
718 chip->update_automute(chip, 1);
720 if (ctrl & MASK_CNTLERR) {
721 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
722 if (err && chip->model <= PMAC_SCREAMER)
723 snd_printk(KERN_DEBUG "error %x\n", err);
725 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
726 out_le32(&chip->awacs->control, ctrl);
732 * a wrapper to feature call for compatibility
734 static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
736 if (ppc_md.feature_call)
737 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
744 static int snd_pmac_free(struct snd_pmac *chip)
747 if (chip->initialized) {
748 snd_pmac_dbdma_reset(chip);
749 /* disable interrupts from awacs interface */
750 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
754 snd_pmac_sound_feature(chip, 0);
756 /* clean up mixer if any */
757 if (chip->mixer_free)
758 chip->mixer_free(chip);
760 snd_pmac_detach_beep(chip);
762 /* release resources */
764 free_irq(chip->irq, (void*)chip);
765 if (chip->tx_irq >= 0)
766 free_irq(chip->tx_irq, (void*)chip);
767 if (chip->rx_irq >= 0)
768 free_irq(chip->rx_irq, (void*)chip);
769 snd_pmac_dbdma_free(chip, &chip->playback.cmd);
770 snd_pmac_dbdma_free(chip, &chip->capture.cmd);
771 snd_pmac_dbdma_free(chip, &chip->extra_dma);
772 if (chip->macio_base)
773 iounmap(chip->macio_base);
774 if (chip->latch_base)
775 iounmap(chip->latch_base);
777 iounmap(chip->awacs);
778 if (chip->playback.dma)
779 iounmap(chip->playback.dma);
780 if (chip->capture.dma)
781 iounmap(chip->capture.dma);
785 for (i = 0; i < 3; i++) {
786 if (chip->requested & (1 << i))
787 release_mem_region(chip->rsrc[i].start,
789 chip->rsrc[i].start + 1);
794 pci_dev_put(chip->pdev);
795 of_node_put(chip->node);
804 static int snd_pmac_dev_free(struct snd_device *device)
806 struct snd_pmac *chip = device->device_data;
807 return snd_pmac_free(chip);
812 * check the machine support byteswap (little-endian)
815 static void __init detect_byte_swap(struct snd_pmac *chip)
817 struct device_node *mio;
819 /* if seems that Keylargo can't byte-swap */
820 for (mio = chip->node->parent; mio; mio = mio->parent) {
821 if (strcmp(mio->name, "mac-io") == 0) {
822 if (of_device_is_compatible(mio, "Keylargo"))
823 chip->can_byte_swap = 0;
828 /* it seems the Pismo & iBook can't byte-swap in hardware. */
829 if (machine_is_compatible("PowerBook3,1") ||
830 machine_is_compatible("PowerBook2,1"))
831 chip->can_byte_swap = 0 ;
833 if (machine_is_compatible("PowerBook2,1"))
834 chip->can_duplex = 0;
839 * detect a sound chip
841 static int __init snd_pmac_detect(struct snd_pmac *chip)
843 struct device_node *sound;
844 struct device_node *dn;
845 const unsigned int *prop;
847 struct macio_chip* macio;
849 if (!machine_is(powermac))
854 chip->freqs_ok = 0xff; /* all ok */
855 chip->model = PMAC_AWACS;
856 chip->can_byte_swap = 1;
857 chip->can_duplex = 1;
858 chip->can_capture = 1;
859 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
860 chip->freq_table = awacs_freqs;
863 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
865 /* check machine type */
866 if (machine_is_compatible("AAPL,3400/2400")
867 || machine_is_compatible("AAPL,3500"))
868 chip->is_pbook_3400 = 1;
869 else if (machine_is_compatible("PowerBook1,1")
870 || machine_is_compatible("AAPL,PowerBook1998"))
871 chip->is_pbook_G3 = 1;
872 chip->node = of_find_node_by_name(NULL, "awacs");
873 sound = of_node_get(chip->node);
876 * powermac G3 models have a node called "davbus"
877 * with a child called "sound".
880 chip->node = of_find_node_by_name(NULL, "davbus");
882 * if we didn't find a davbus device, try 'i2s-a' since
883 * this seems to be what iBooks have
886 chip->node = of_find_node_by_name(NULL, "i2s-a");
887 if (chip->node && chip->node->parent &&
888 chip->node->parent->parent) {
889 if (of_device_is_compatible(chip->node->parent->parent,
898 sound = of_find_node_by_name(NULL, "sound");
899 while (sound && sound->parent != chip->node)
900 sound = of_find_node_by_name(sound, "sound");
903 of_node_put(chip->node);
907 prop = of_get_property(sound, "sub-frame", NULL);
908 if (prop && *prop < 16)
909 chip->subframe = *prop;
910 prop = of_get_property(sound, "layout-id", NULL);
912 /* partly deprecate snd-powermac, for those machines
913 * that have a layout-id property for now */
914 printk(KERN_INFO "snd-powermac no longer handles any "
915 "machines with a layout-id property "
916 "in the device-tree, use snd-aoa.\n");
918 of_node_put(chip->node);
922 /* This should be verified on older screamers */
923 if (of_device_is_compatible(sound, "screamer")) {
924 chip->model = PMAC_SCREAMER;
925 // chip->can_byte_swap = 0; /* FIXME: check this */
927 if (of_device_is_compatible(sound, "burgundy")) {
928 chip->model = PMAC_BURGUNDY;
929 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
931 if (of_device_is_compatible(sound, "daca")) {
932 chip->model = PMAC_DACA;
933 chip->can_capture = 0; /* no capture */
934 chip->can_duplex = 0;
935 // chip->can_byte_swap = 0; /* FIXME: check this */
936 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
938 if (of_device_is_compatible(sound, "tumbler")) {
939 chip->model = PMAC_TUMBLER;
940 chip->can_capture = 0; /* no capture */
941 chip->can_duplex = 0;
942 // chip->can_byte_swap = 0; /* FIXME: check this */
943 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
944 chip->freq_table = tumbler_freqs;
945 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
947 if (of_device_is_compatible(sound, "snapper")) {
948 chip->model = PMAC_SNAPPER;
949 // chip->can_byte_swap = 0; /* FIXME: check this */
950 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
951 chip->freq_table = tumbler_freqs;
952 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
954 prop = of_get_property(sound, "device-id", NULL);
956 chip->device_id = *prop;
957 dn = of_find_node_by_name(NULL, "perch");
958 chip->has_iic = (dn != NULL);
961 /* We need the PCI device for DMA allocations, let's use a crude method
964 macio = macio_find(chip->node, macio_unknown);
966 printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
968 struct pci_dev *pdev = NULL;
970 for_each_pci_dev(pdev) {
971 struct device_node *np = pci_device_to_OF_node(pdev);
972 if (np && np == macio->of_node) {
978 if (chip->pdev == NULL)
979 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
982 detect_byte_swap(chip);
984 /* look for a property saying what sample rates
986 prop = of_get_property(sound, "sample-rates", &l);
988 prop = of_get_property(sound, "output-frame-rates", &l);
992 for (l /= sizeof(int); l > 0; --l) {
993 unsigned int r = *prop++;
994 /* Apple 'Fixed' format */
997 for (i = 0; i < chip->num_freqs; ++i) {
998 if (r == chip->freq_table[i]) {
999 chip->freqs_ok |= (1 << i);
1005 /* assume only 44.1khz */
1013 #ifdef PMAC_SUPPORT_AUTOMUTE
1017 static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
1020 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1021 ucontrol->value.integer.value[0] = chip->auto_mute;
1025 static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1026 struct snd_ctl_elem_value *ucontrol)
1028 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1029 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1030 chip->auto_mute = !!ucontrol->value.integer.value[0];
1031 if (chip->update_automute)
1032 chip->update_automute(chip, 1);
1038 static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_value *ucontrol)
1041 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1042 if (chip->detect_headphone)
1043 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1045 ucontrol->value.integer.value[0] = 0;
1049 static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
1050 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1051 .name = "Auto Mute Switch",
1052 .info = snd_pmac_boolean_mono_info,
1053 .get = pmac_auto_mute_get,
1054 .put = pmac_auto_mute_put,
1056 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1057 .name = "Headphone Detection",
1058 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1059 .info = snd_pmac_boolean_mono_info,
1060 .get = pmac_hp_detect_get,
1064 int __init snd_pmac_add_automute(struct snd_pmac *chip)
1067 chip->auto_mute = 1;
1068 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1070 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
1073 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1074 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1076 #endif /* PMAC_SUPPORT_AUTOMUTE */
1079 * create and detect a pmac chip record
1081 int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1083 struct snd_pmac *chip;
1084 struct device_node *np;
1087 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1088 static struct snd_device_ops ops = {
1089 .dev_free = snd_pmac_dev_free,
1092 *chip_return = NULL;
1094 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1099 spin_lock_init(&chip->reg_lock);
1100 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1102 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1103 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1105 if ((err = snd_pmac_detect(chip)) < 0)
1108 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1109 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1110 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
1116 chip->requested = 0;
1118 static char *rnames[] = {
1119 "Sound Control", "Sound DMA" };
1120 for (i = 0; i < 2; i ++) {
1121 if (of_address_to_resource(np->parent, i,
1123 printk(KERN_ERR "snd: can't translate rsrc "
1124 " %d (%s)\n", i, rnames[i]);
1128 if (request_mem_region(chip->rsrc[i].start,
1130 chip->rsrc[i].start + 1,
1131 rnames[i]) == NULL) {
1132 printk(KERN_ERR "snd: can't request rsrc "
1133 " %d (%s: 0x%016llx:%016llx)\n",
1135 (unsigned long long)chip->rsrc[i].start,
1136 (unsigned long long)chip->rsrc[i].end);
1140 chip->requested |= (1 << i);
1142 ctrl_addr = chip->rsrc[0].start;
1143 txdma_addr = chip->rsrc[1].start;
1144 rxdma_addr = txdma_addr + 0x100;
1146 static char *rnames[] = {
1147 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1148 for (i = 0; i < 3; i ++) {
1149 if (of_address_to_resource(np, i,
1151 printk(KERN_ERR "snd: can't translate rsrc "
1152 " %d (%s)\n", i, rnames[i]);
1156 if (request_mem_region(chip->rsrc[i].start,
1158 chip->rsrc[i].start + 1,
1159 rnames[i]) == NULL) {
1160 printk(KERN_ERR "snd: can't request rsrc "
1161 " %d (%s: 0x%016llx:%016llx)\n",
1163 (unsigned long long)chip->rsrc[i].start,
1164 (unsigned long long)chip->rsrc[i].end);
1168 chip->requested |= (1 << i);
1170 ctrl_addr = chip->rsrc[0].start;
1171 txdma_addr = chip->rsrc[1].start;
1172 rxdma_addr = chip->rsrc[2].start;
1175 chip->awacs = ioremap(ctrl_addr, 0x1000);
1176 chip->playback.dma = ioremap(txdma_addr, 0x100);
1177 chip->capture.dma = ioremap(rxdma_addr, 0x100);
1178 if (chip->model <= PMAC_BURGUNDY) {
1179 irq = irq_of_parse_and_map(np, 0);
1180 if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1181 "PMac", (void*)chip)) {
1182 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1189 irq = irq_of_parse_and_map(np, 1);
1190 if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1191 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1196 irq = irq_of_parse_and_map(np, 2);
1197 if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1198 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1204 snd_pmac_sound_feature(chip, 1);
1207 if (chip->model == PMAC_AWACS)
1208 out_le32(&chip->awacs->control, 0x11);
1210 /* Powerbooks have odd ways of enabling inputs such as
1211 an expansion-bay CD or sound from an internal modem
1212 or a PC-card modem. */
1213 if (chip->is_pbook_3400) {
1214 /* Enable CD and PC-card sound inputs. */
1215 /* This is done by reading from address
1216 * f301a000, + 0x10 to enable the expansion-bay
1217 * CD sound input, + 0x80 to enable the PC-card
1218 * sound input. The 0x100 enables the SCSI bus
1221 chip->latch_base = ioremap (0xf301a000, 0x1000);
1222 in_8(chip->latch_base + 0x190);
1223 } else if (chip->is_pbook_G3) {
1224 struct device_node* mio;
1225 for (mio = chip->node->parent; mio; mio = mio->parent) {
1226 if (strcmp(mio->name, "mac-io") == 0) {
1228 if (of_address_to_resource(mio, 0, &r) == 0)
1230 ioremap(r.start, 0x40);
1234 /* Enable CD sound input. */
1235 /* The relevant bits for writing to this byte are 0x8f.
1236 * I haven't found out what the 0x80 bit does.
1237 * For the 0xf bits, writing 3 or 7 enables the CD
1238 * input, any other value disables it. Values
1239 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1240 * 4, 6, 8 - f enable the input from the modem.
1242 if (chip->macio_base)
1243 out_8(chip->macio_base + 0x37, 3);
1246 /* Reset dbdma channels */
1247 snd_pmac_dbdma_reset(chip);
1249 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1252 *chip_return = chip;
1256 snd_pmac_free(chip);
1262 * sleep notify for powerbook
1268 * Save state when going to sleep, restore it afterwards.
1271 void snd_pmac_suspend(struct snd_pmac *chip)
1273 unsigned long flags;
1275 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1277 chip->suspend(chip);
1278 snd_pcm_suspend_all(chip->pcm);
1279 spin_lock_irqsave(&chip->reg_lock, flags);
1280 snd_pmac_beep_stop(chip);
1281 spin_unlock_irqrestore(&chip->reg_lock, flags);
1283 disable_irq(chip->irq);
1284 if (chip->tx_irq >= 0)
1285 disable_irq(chip->tx_irq);
1286 if (chip->rx_irq >= 0)
1287 disable_irq(chip->rx_irq);
1288 snd_pmac_sound_feature(chip, 0);
1291 void snd_pmac_resume(struct snd_pmac *chip)
1293 snd_pmac_sound_feature(chip, 1);
1296 /* enable CD sound input */
1297 if (chip->macio_base && chip->is_pbook_G3)
1298 out_8(chip->macio_base + 0x37, 3);
1299 else if (chip->is_pbook_3400)
1300 in_8(chip->latch_base + 0x190);
1302 snd_pmac_pcm_set_format(chip);
1305 enable_irq(chip->irq);
1306 if (chip->tx_irq >= 0)
1307 enable_irq(chip->tx_irq);
1308 if (chip->rx_irq >= 0)
1309 enable_irq(chip->rx_irq);
1311 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1314 #endif /* CONFIG_PM */