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>
40 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
41 static int snd_pmac_register_sleep_notifier(pmac_t *chip);
42 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip);
43 static int snd_pmac_suspend(snd_card_t *card, unsigned int state);
44 static int snd_pmac_resume(snd_card_t *card, unsigned int state);
48 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
49 static int awacs_freqs[8] = {
50 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
52 /* fixed frequency table for tumbler */
53 static int tumbler_freqs[1] = {
58 * allocate DBDMA command arrays
60 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
62 rec->space = kmalloc(sizeof(struct dbdma_cmd) * (size + 1), GFP_KERNEL);
63 if (rec->space == NULL)
66 memset(rec->space, 0, sizeof(struct dbdma_cmd) * (size + 1));
67 rec->cmds = (void*)DBDMA_ALIGN(rec->space);
68 rec->addr = virt_to_bus(rec->cmds);
72 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
74 if (rec && rec->space)
84 * look up frequency table
87 unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
92 if (rate > chip->freq_table[0])
95 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
96 if (! (ok & 1)) continue;
98 if (rate >= chip->freq_table[i])
105 * check whether another stream is active
107 static inline int another_stream(int stream)
109 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
110 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
116 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
117 snd_pcm_hw_params_t *hw_params)
119 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
125 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
127 snd_pcm_lib_free_pages(subs);
132 * get a stream of the opposite direction
134 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
137 case SNDRV_PCM_STREAM_PLAYBACK:
138 return &chip->playback;
139 case SNDRV_PCM_STREAM_CAPTURE:
140 return &chip->capture;
148 * wait while run status is on
151 snd_pmac_wait_ack(pmac_stream_t *rec)
154 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
159 * set the format and rate to the chip.
160 * call the lowlevel function if defined (e.g. for AWACS).
162 static void snd_pmac_pcm_set_format(pmac_t *chip)
164 /* set up frequency and format */
165 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
166 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
167 if (chip->set_format)
168 chip->set_format(chip);
172 * stop the DMA transfer
174 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
176 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
177 snd_pmac_wait_ack(rec);
181 * set the command pointer address
183 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
185 out_le32(&rec->dma->cmdptr, cmd->addr);
191 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
193 out_le32(&rec->dma->control, status | (status << 16));
198 * prepare playback/capture stream
200 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
203 volatile struct dbdma_cmd *cp;
204 snd_pcm_runtime_t *runtime = subs->runtime;
209 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
210 rec->period_size = snd_pcm_lib_period_bytes(subs);
211 rec->nperiods = rec->dma_size / rec->period_size;
213 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
215 /* set up constraints */
216 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
217 snd_runtime_check(astr, return -EINVAL);
218 astr->cur_freqs = 1 << rate_index;
219 astr->cur_formats = 1 << runtime->format;
220 chip->rate_index = rate_index;
221 chip->format = runtime->format;
223 /* We really want to execute a DMA stop command, after the AWACS
225 * For reasons I don't understand, it stops the hissing noise
226 * common to many PowerBook G3 systems and random noise otherwise
227 * captured on iBook2's about every third time. -ReneR
229 spin_lock_irq(&chip->reg_lock);
230 snd_pmac_dma_stop(rec);
231 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
232 snd_pmac_dma_set_command(rec, &chip->extra_dma);
233 snd_pmac_dma_run(rec, RUN);
234 spin_unlock_irq(&chip->reg_lock);
236 spin_lock_irq(&chip->reg_lock);
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;
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_irq(&chip->reg_lock);
263 static int snd_pmac_pcm_trigger(pmac_t *chip, pmac_stream_t *rec,
264 snd_pcm_substream_t *subs, int cmd)
266 volatile struct dbdma_cmd *cp;
270 case SNDRV_PCM_TRIGGER_START:
271 case SNDRV_PCM_TRIGGER_RESUME:
274 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
275 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
276 spin_lock(&chip->reg_lock);
277 snd_pmac_beep_stop(chip);
278 snd_pmac_pcm_set_format(chip);
279 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
280 out_le16(&cp->command, command);
281 snd_pmac_dma_set_command(rec, &rec->cmd);
282 (void)in_le32(&rec->dma->status);
283 snd_pmac_dma_run(rec, RUN|WAKE);
285 spin_unlock(&chip->reg_lock);
288 case SNDRV_PCM_TRIGGER_STOP:
289 case SNDRV_PCM_TRIGGER_SUSPEND:
290 spin_lock(&chip->reg_lock);
292 /*printk("stopped!!\n");*/
293 snd_pmac_dma_stop(rec);
294 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
295 out_le16(&cp->command, DBDMA_STOP);
296 spin_unlock(&chip->reg_lock);
307 * return the current pointer
310 static snd_pcm_uframes_t snd_pmac_pcm_pointer(pmac_t *chip, pmac_stream_t *rec,
311 snd_pcm_substream_t *subs)
315 #if 1 /* hmm.. how can we get the current dma pointer?? */
317 volatile struct dbdma_cmd *cp = &rec->cmd.cmds[rec->cur_period];
318 stat = ld_le16(&cp->xfer_status);
319 if (stat & (ACTIVE|DEAD)) {
320 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(snd_pcm_substream_t *subs)
335 pmac_t *chip = snd_pcm_substream_chip(subs);
336 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
339 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
342 pmac_t *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(snd_pcm_substream_t *subs)
348 pmac_t *chip = snd_pcm_substream_chip(subs);
349 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
357 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
359 pmac_t *chip = snd_pcm_substream_chip(subs);
360 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
363 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
366 pmac_t *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(snd_pcm_substream_t *subs)
372 pmac_t *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(pmac_t *chip, pmac_stream_t *rec)
382 volatile struct dbdma_cmd *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 snd_pcm_hardware_t 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 = 32768,
429 .period_bytes_min = 256,
430 .period_bytes_max = 16384,
432 .periods_max = PMAC_MAX_FRAGS,
435 static snd_pcm_hardware_t 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 = 32768,
448 .period_bytes_min = 256,
449 .period_bytes_max = 16384,
451 .periods_max = PMAC_MAX_FRAGS,
456 static int snd_pmac_hw_rule_rate(snd_pcm_hw_params_t *params,
457 snd_pcm_hw_rule_t *rule)
459 pmac_t *chip = rule->private;
460 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
461 int i, freq_table[8], num_freqs;
463 snd_runtime_check(rec, return -EINVAL);
465 for (i = chip->num_freqs - 1; i >= 0; i--) {
466 if (rec->cur_freqs & (1 << i))
467 freq_table[num_freqs++] = chip->freq_table[i];
470 return snd_interval_list(hw_param_interval(params, rule->var),
471 num_freqs, freq_table, 0);
474 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
475 snd_pcm_hw_rule_t *rule)
477 pmac_t *chip = rule->private;
478 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
480 snd_runtime_check(rec, return -EINVAL);
481 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
486 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
488 snd_pcm_runtime_t *runtime = subs->runtime;
490 static int typical_freqs[] = {
496 static int typical_freq_flags[] = {
497 SNDRV_PCM_RATE_44100,
498 SNDRV_PCM_RATE_22050,
499 SNDRV_PCM_RATE_11025,
503 /* look up frequency table and fill bit mask */
504 runtime->hw.rates = 0;
505 fflags = chip->freqs_ok;
506 for (i = 0; typical_freqs[i]; i++) {
507 for (j = 0; j < chip->num_freqs; j++) {
508 if ((chip->freqs_ok & (1 << j)) &&
509 chip->freq_table[j] == typical_freqs[i]) {
510 runtime->hw.rates |= typical_freq_flags[i];
516 if (fflags) /* rest */
517 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
519 /* check for minimum and maximum rates */
520 for (i = 0; i < chip->num_freqs; i++) {
521 if (chip->freqs_ok & (1 << i)) {
522 runtime->hw.rate_max = chip->freq_table[i];
526 for (i = chip->num_freqs - 1; i >= 0; i--) {
527 if (chip->freqs_ok & (1 << i)) {
528 runtime->hw.rate_min = chip->freq_table[i];
532 runtime->hw.formats = chip->formats_ok;
533 if (chip->can_capture) {
534 if (! chip->can_duplex)
535 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
536 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
538 runtime->private_data = rec;
539 rec->substream = subs;
541 #if 0 /* FIXME: still under development.. */
542 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
543 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
544 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
545 snd_pmac_hw_rule_format, chip, rec->stream, -1);
548 runtime->hw.periods_max = rec->cmd.size - 1;
550 if (chip->can_duplex)
551 snd_pcm_set_sync(subs);
556 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
560 snd_pmac_dma_stop(rec);
562 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
563 snd_runtime_check(astr, return -EINVAL);
565 /* reset constraints */
566 astr->cur_freqs = chip->freqs_ok;
567 astr->cur_formats = chip->formats_ok;
572 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
574 pmac_t *chip = snd_pcm_substream_chip(subs);
576 subs->runtime->hw = snd_pmac_playback;
577 return snd_pmac_pcm_open(chip, &chip->playback, subs);
580 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
582 pmac_t *chip = snd_pcm_substream_chip(subs);
584 subs->runtime->hw = snd_pmac_capture;
585 return snd_pmac_pcm_open(chip, &chip->capture, subs);
588 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
590 pmac_t *chip = snd_pcm_substream_chip(subs);
592 return snd_pmac_pcm_close(chip, &chip->playback, subs);
595 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
597 pmac_t *chip = snd_pcm_substream_chip(subs);
599 return snd_pmac_pcm_close(chip, &chip->capture, subs);
605 static snd_pcm_ops_t snd_pmac_playback_ops = {
606 .open = snd_pmac_playback_open,
607 .close = snd_pmac_playback_close,
608 .ioctl = snd_pcm_lib_ioctl,
609 .hw_params = snd_pmac_pcm_hw_params,
610 .hw_free = snd_pmac_pcm_hw_free,
611 .prepare = snd_pmac_playback_prepare,
612 .trigger = snd_pmac_playback_trigger,
613 .pointer = snd_pmac_playback_pointer,
616 static snd_pcm_ops_t snd_pmac_capture_ops = {
617 .open = snd_pmac_capture_open,
618 .close = snd_pmac_capture_close,
619 .ioctl = snd_pcm_lib_ioctl,
620 .hw_params = snd_pmac_pcm_hw_params,
621 .hw_free = snd_pmac_pcm_hw_free,
622 .prepare = snd_pmac_capture_prepare,
623 .trigger = snd_pmac_capture_trigger,
624 .pointer = snd_pmac_capture_pointer,
627 static void pmac_pcm_free(snd_pcm_t *pcm)
629 snd_pcm_lib_preallocate_free_for_all(pcm);
632 int __init snd_pmac_pcm_new(pmac_t *chip)
636 int num_captures = 1;
638 if (! chip->can_capture)
640 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
645 if (chip->can_capture)
646 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
648 pcm->private_data = chip;
649 pcm->private_free = pmac_pcm_free;
650 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
651 strcpy(pcm->name, chip->card->shortname);
654 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
655 if (chip->can_byte_swap)
656 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
658 chip->playback.cur_formats = chip->formats_ok;
659 chip->capture.cur_formats = chip->formats_ok;
660 chip->playback.cur_freqs = chip->freqs_ok;
661 chip->capture.cur_freqs = chip->freqs_ok;
663 /* preallocate 64k buffer */
664 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
665 snd_dma_continuous_data(GFP_KERNEL),
666 64 * 1024, 64 * 1024);
672 static void snd_pmac_dbdma_reset(pmac_t *chip)
674 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
675 snd_pmac_wait_ack(&chip->playback);
676 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
677 snd_pmac_wait_ack(&chip->capture);
684 void snd_pmac_beep_dma_start(pmac_t *chip, int bytes, unsigned long addr, int speed)
686 pmac_stream_t *rec = &chip->playback;
688 snd_pmac_dma_stop(rec);
689 st_le16(&chip->extra_dma.cmds->req_count, bytes);
690 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
691 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
692 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
693 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
694 out_le32(&chip->awacs->control,
695 (in_le32(&chip->awacs->control) & ~0x1f00)
697 out_le32(&chip->awacs->byteswap, 0);
698 snd_pmac_dma_set_command(rec, &chip->extra_dma);
699 snd_pmac_dma_run(rec, RUN);
702 void snd_pmac_beep_dma_stop(pmac_t *chip)
704 snd_pmac_dma_stop(&chip->playback);
705 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
706 snd_pmac_pcm_set_format(chip); /* reset format */
714 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
716 pmac_t *chip = devid;
717 snd_pmac_pcm_update(chip, &chip->playback);
723 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
725 pmac_t *chip = devid;
726 snd_pmac_pcm_update(chip, &chip->capture);
732 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
734 pmac_t *chip = devid;
735 int ctrl = in_le32(&chip->awacs->control);
737 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
738 if (ctrl & MASK_PORTCHG) {
739 /* do something when headphone is plugged/unplugged? */
740 if (chip->update_automute)
741 chip->update_automute(chip, 1);
743 if (ctrl & MASK_CNTLERR) {
744 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
745 if (err && chip->model <= PMAC_SCREAMER)
746 snd_printk(KERN_DEBUG "error %x\n", err);
748 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
749 out_le32(&chip->awacs->control, ctrl);
755 * a wrapper to feature call for compatibility
757 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
758 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
760 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
761 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
763 if (chip->is_pbook_G3) {
765 feature_clear(chip->node, FEATURE_Sound_power);
766 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
767 big_mdelay(1000); /* XXX */
770 if (chip->is_pbook_3400) {
771 feature_set(chip->node, FEATURE_IOBUS_enable);
776 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
777 #define snd_pmac_sound_feature(chip,enable) /**/
778 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
784 static int snd_pmac_free(pmac_t *chip)
789 if (chip->initialized) {
790 snd_pmac_dbdma_reset(chip);
791 /* disable interrupts from awacs interface */
792 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
795 snd_pmac_sound_feature(chip, 0);
796 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
797 snd_pmac_unregister_sleep_notifier(chip);
800 /* clean up mixer if any */
801 if (chip->mixer_free)
802 chip->mixer_free(chip);
804 snd_pmac_detach_beep(chip);
806 /* release resources */
808 free_irq(chip->irq, (void*)chip);
809 if (chip->tx_irq >= 0)
810 free_irq(chip->tx_irq, (void*)chip);
811 if (chip->rx_irq >= 0)
812 free_irq(chip->rx_irq, (void*)chip);
813 snd_pmac_dbdma_free(&chip->playback.cmd);
814 snd_pmac_dbdma_free(&chip->capture.cmd);
815 snd_pmac_dbdma_free(&chip->extra_dma);
816 if (chip->macio_base)
817 iounmap(chip->macio_base);
818 if (chip->latch_base)
819 iounmap(chip->latch_base);
821 iounmap((void*)chip->awacs);
822 if (chip->playback.dma)
823 iounmap((void*)chip->playback.dma);
824 if (chip->capture.dma)
825 iounmap((void*)chip->capture.dma);
827 for (i = 0; i < 3; i++) {
828 if (chip->of_requested & (1 << i))
829 release_OF_resource(chip->node, i);
840 static int snd_pmac_dev_free(snd_device_t *device)
842 pmac_t *chip = device->device_data;
843 return snd_pmac_free(chip);
848 * check the machine support byteswap (little-endian)
851 static void __init detect_byte_swap(pmac_t *chip)
853 struct device_node *mio;
855 /* if seems that Keylargo can't byte-swap */
856 for (mio = chip->node->parent; mio; mio = mio->parent) {
857 if (strcmp(mio->name, "mac-io") == 0) {
858 if (device_is_compatible(mio, "Keylargo"))
859 chip->can_byte_swap = 0;
864 /* it seems the Pismo & iBook can't byte-swap in hardware. */
865 if (machine_is_compatible("PowerBook3,1") ||
866 machine_is_compatible("PowerBook2,1"))
867 chip->can_byte_swap = 0 ;
869 if (machine_is_compatible("PowerBook2,1"))
870 chip->can_duplex = 0;
875 * detect a sound chip
877 static int __init snd_pmac_detect(pmac_t *chip)
879 struct device_node *sound;
880 unsigned int *prop, l;
882 if (_machine != _MACH_Pmac)
887 chip->freqs_ok = 0xff; /* all ok */
888 chip->model = PMAC_AWACS;
889 chip->can_byte_swap = 1;
890 chip->can_duplex = 1;
891 chip->can_capture = 1;
892 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
893 chip->freq_table = awacs_freqs;
895 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
897 /* check machine type */
898 if (machine_is_compatible("AAPL,3400/2400")
899 || machine_is_compatible("AAPL,3500"))
900 chip->is_pbook_3400 = 1;
901 else if (machine_is_compatible("PowerBook1,1")
902 || machine_is_compatible("AAPL,PowerBook1998"))
903 chip->is_pbook_G3 = 1;
904 chip->node = find_devices("awacs");
909 * powermac G3 models have a node called "davbus"
910 * with a child called "sound".
912 chip->node = find_devices("davbus");
914 * if we didn't find a davbus device, try 'i2s-a' since
915 * this seems to be what iBooks have
918 chip->node = find_devices("i2s-a");
921 sound = find_devices("sound");
922 while (sound && sound->parent != chip->node)
926 prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
927 if (prop && *prop < 16)
928 chip->subframe = *prop;
929 /* This should be verified on older screamers */
930 if (device_is_compatible(sound, "screamer")) {
931 chip->model = PMAC_SCREAMER;
932 // chip->can_byte_swap = 0; /* FIXME: check this */
934 if (device_is_compatible(sound, "burgundy")) {
935 chip->model = PMAC_BURGUNDY;
936 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
938 if (device_is_compatible(sound, "daca")) {
939 chip->model = PMAC_DACA;
940 chip->can_capture = 0; /* no capture */
941 chip->can_duplex = 0;
942 // chip->can_byte_swap = 0; /* FIXME: check this */
943 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
945 if (device_is_compatible(sound, "tumbler")) {
946 chip->model = PMAC_TUMBLER;
947 chip->can_capture = 0; /* no capture */
948 chip->can_duplex = 0;
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 if (device_is_compatible(sound, "snapper")) {
955 chip->model = PMAC_SNAPPER;
956 // chip->can_byte_swap = 0; /* FIXME: check this */
957 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
958 chip->freq_table = tumbler_freqs;
959 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
961 if (device_is_compatible(sound, "AOAKeylargo")) {
962 /* Seems to support the stock AWACS frequencies, but has
964 chip->model = PMAC_SNAPPER;
965 // chip->can_byte_swap = 0; /* FIXME: check this */
966 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
968 prop = (unsigned int *)get_property(sound, "device-id", NULL);
970 chip->device_id = *prop;
971 chip->has_iic = (find_devices("perch") != NULL);
973 detect_byte_swap(chip);
975 /* look for a property saying what sample rates
977 prop = (unsigned int *) get_property(sound, "sample-rates", &l);
979 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
983 for (l /= sizeof(int); l > 0; --l) {
984 unsigned int r = *prop++;
985 /* Apple 'Fixed' format */
988 for (i = 0; i < chip->num_freqs; ++i) {
989 if (r == chip->freq_table[i]) {
990 chip->freqs_ok |= (1 << i);
996 /* assume only 44.1khz */
1004 * exported - boolean info callbacks for ease of programming
1006 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1008 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1010 uinfo->value.integer.min = 0;
1011 uinfo->value.integer.max = 1;
1015 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1017 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1019 uinfo->value.integer.min = 0;
1020 uinfo->value.integer.max = 1;
1024 #ifdef PMAC_SUPPORT_AUTOMUTE
1028 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1030 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1031 ucontrol->value.integer.value[0] = chip->auto_mute;
1035 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1037 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1038 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1039 chip->auto_mute = ucontrol->value.integer.value[0];
1040 if (chip->update_automute)
1041 chip->update_automute(chip, 1);
1047 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1049 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1050 if (chip->detect_headphone)
1051 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1053 ucontrol->value.integer.value[0] = 0;
1057 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1058 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1059 .name = "Auto Mute Switch",
1060 .info = snd_pmac_boolean_mono_info,
1061 .get = pmac_auto_mute_get,
1062 .put = pmac_auto_mute_put,
1064 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1065 .name = "Headphone Detection",
1066 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1067 .info = snd_pmac_boolean_mono_info,
1068 .get = pmac_hp_detect_get,
1072 int __init snd_pmac_add_automute(pmac_t *chip)
1075 chip->auto_mute = 1;
1076 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1079 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1080 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1082 #endif /* PMAC_SUPPORT_AUTOMUTE */
1085 * create and detect a pmac chip record
1087 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1090 struct device_node *np;
1092 static snd_device_ops_t ops = {
1093 .dev_free = snd_pmac_dev_free,
1096 snd_runtime_check(chip_return, return -EINVAL);
1097 *chip_return = NULL;
1099 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1104 spin_lock_init(&chip->reg_lock);
1105 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1107 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1108 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1110 if ((err = snd_pmac_detect(chip)) < 0)
1113 if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1114 snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1115 snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1121 if (np->n_addrs < 3 || np->n_intrs < 3) {
1126 for (i = 0; i < 3; i++) {
1127 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1128 if (! request_OF_resource(np, i, name[i])) {
1129 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1133 chip->of_requested |= (1 << i);
1136 chip->awacs = (volatile struct awacs_regs *) ioremap(np->addrs[0].address, 0x1000);
1137 chip->playback.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[1].address, 0x100);
1138 chip->capture.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[2].address, 0x100);
1139 if (chip->model <= PMAC_BURGUNDY) {
1140 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1141 "PMac", (void*)chip)) {
1142 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1146 chip->irq = np->intrs[0].line;
1148 if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1149 "PMac Output", (void*)chip)) {
1150 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1154 chip->tx_irq = np->intrs[1].line;
1155 if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1156 "PMac Input", (void*)chip)) {
1157 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1161 chip->rx_irq = np->intrs[2].line;
1163 snd_pmac_sound_feature(chip, 1);
1166 out_le32(&chip->awacs->control, 0x11);
1168 /* Powerbooks have odd ways of enabling inputs such as
1169 an expansion-bay CD or sound from an internal modem
1170 or a PC-card modem. */
1171 if (chip->is_pbook_3400) {
1172 /* Enable CD and PC-card sound inputs. */
1173 /* This is done by reading from address
1174 * f301a000, + 0x10 to enable the expansion-bay
1175 * CD sound input, + 0x80 to enable the PC-card
1176 * sound input. The 0x100 enables the SCSI bus
1179 chip->latch_base = (unsigned char *) ioremap (0xf301a000, 0x1000);
1180 in_8(chip->latch_base + 0x190);
1181 } else if (chip->is_pbook_G3) {
1182 struct device_node* mio;
1183 for (mio = chip->node->parent; mio; mio = mio->parent) {
1184 if (strcmp(mio->name, "mac-io") == 0
1185 && mio->n_addrs > 0) {
1186 chip->macio_base = (unsigned char *) ioremap
1187 (mio->addrs[0].address, 0x40);
1191 /* Enable CD sound input. */
1192 /* The relevant bits for writing to this byte are 0x8f.
1193 * I haven't found out what the 0x80 bit does.
1194 * For the 0xf bits, writing 3 or 7 enables the CD
1195 * input, any other value disables it. Values
1196 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1197 * 4, 6, 8 - f enable the input from the modem.
1199 if (chip->macio_base)
1200 out_8(chip->macio_base + 0x37, 3);
1203 /* Reset dbdma channels */
1204 snd_pmac_dbdma_reset(chip);
1206 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1207 /* add sleep notifier */
1208 if (! snd_pmac_register_sleep_notifier(chip))
1209 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1212 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1215 *chip_return = chip;
1219 snd_pmac_free(chip);
1225 * sleep notify for powerbook
1228 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1231 * Save state when going to sleep, restore it afterwards.
1234 static int snd_pmac_suspend(snd_card_t *card, unsigned int state)
1236 pmac_t *chip = card->pm_private_data;
1237 unsigned long flags;
1240 chip->suspend(chip);
1241 snd_pcm_suspend_all(chip->pcm);
1242 spin_lock_irqsave(&chip->reg_lock, flags);
1243 snd_pmac_beep_stop(chip);
1244 spin_unlock_irqrestore(&chip->reg_lock, flags);
1246 disable_irq(chip->irq);
1247 if (chip->tx_irq >= 0)
1248 disable_irq(chip->tx_irq);
1249 if (chip->rx_irq >= 0)
1250 disable_irq(chip->rx_irq);
1251 snd_pmac_sound_feature(chip, 0);
1252 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1256 static int snd_pmac_resume(snd_card_t *card, unsigned int state)
1258 pmac_t *chip = card->pm_private_data;
1260 snd_pmac_sound_feature(chip, 1);
1263 /* enable CD sound input */
1264 if (chip->macio_base && chip->is_pbook_G3) {
1265 out_8(chip->macio_base + 0x37, 3);
1266 } else if (chip->is_pbook_3400) {
1267 in_8(chip->latch_base + 0x190);
1270 snd_pmac_pcm_set_format(chip);
1273 enable_irq(chip->irq);
1274 if (chip->tx_irq >= 0)
1275 enable_irq(chip->tx_irq);
1276 if (chip->rx_irq >= 0)
1277 enable_irq(chip->rx_irq);
1279 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1283 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1284 * because we can't have any private data for notify callback.
1286 static pmac_t *sleeping_pmac = NULL;
1288 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1292 chip = sleeping_pmac;
1293 snd_runtime_check(chip, return 0);
1296 case PBOOK_SLEEP_NOW:
1297 snd_pmac_suspend(chip->card, 0);
1300 snd_pmac_resume(chip->card, 0);
1303 return PBOOK_SLEEP_OK;
1306 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1307 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1310 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1312 /* should be protected here.. */
1313 snd_assert(! sleeping_pmac, return -EBUSY);
1314 sleeping_pmac = chip;
1315 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1319 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1321 /* should be protected here.. */
1322 snd_assert(sleeping_pmac == chip, return -ENODEV);
1323 pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1324 sleeping_pmac = NULL;
1328 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */