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_set_power_state(snd_card_t *card, unsigned int power_state);
49 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
50 static int awacs_freqs[8] = {
51 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
53 /* fixed frequency table for tumbler */
54 static int tumbler_freqs[2] = {
59 * allocate DBDMA command arrays
61 static int snd_pmac_dbdma_alloc(pmac_dbdma_t *rec, int size)
63 rec->space = kmalloc(sizeof(struct dbdma_cmd) * (size + 1), GFP_KERNEL);
64 if (rec->space == NULL)
67 memset(rec->space, 0, sizeof(struct dbdma_cmd) * (size + 1));
68 rec->cmds = (void*)DBDMA_ALIGN(rec->space);
69 rec->addr = virt_to_bus(rec->cmds);
73 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
75 if (rec && rec->space)
85 * look up frequency table
88 static unsigned int snd_pmac_rate_index(pmac_t *chip, pmac_stream_t *rec, unsigned int rate)
93 if (rate > chip->freq_table[0])
96 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
97 if (! (ok & 1)) continue;
99 if (rate >= chip->freq_table[i])
106 * check whether another stream is active
108 static inline int another_stream(int stream)
110 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
111 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
117 static int snd_pmac_pcm_hw_params(snd_pcm_substream_t *subs,
118 snd_pcm_hw_params_t *hw_params)
120 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
126 static int snd_pmac_pcm_hw_free(snd_pcm_substream_t *subs)
128 snd_pcm_lib_free_pages(subs);
133 * get a stream of the opposite direction
135 static pmac_stream_t *snd_pmac_get_stream(pmac_t *chip, int stream)
138 case SNDRV_PCM_STREAM_PLAYBACK:
139 return &chip->playback;
140 case SNDRV_PCM_STREAM_CAPTURE:
141 return &chip->capture;
149 * wait while run status is on
152 snd_pmac_wait_ack(pmac_stream_t *rec)
155 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
160 * set the format and rate to the chip.
161 * call the lowlevel function if defined (e.g. for AWACS).
163 static void snd_pmac_pcm_set_format(pmac_t *chip)
165 /* set up frequency and format */
166 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
167 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
168 if (chip->set_format)
169 chip->set_format(chip);
173 * stop the DMA transfer
175 inline static void snd_pmac_dma_stop(pmac_stream_t *rec)
177 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
178 snd_pmac_wait_ack(rec);
182 * set the command pointer address
184 inline static void snd_pmac_dma_set_command(pmac_stream_t *rec, pmac_dbdma_t *cmd)
186 out_le32(&rec->dma->cmdptr, cmd->addr);
192 inline static void snd_pmac_dma_run(pmac_stream_t *rec, int status)
194 out_le32(&rec->dma->control, status | (status << 16));
199 * prepare playback/capture stream
201 static int snd_pmac_pcm_prepare(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
204 volatile struct dbdma_cmd *cp;
206 snd_pcm_runtime_t *runtime = subs->runtime;
211 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
212 rec->period_size = snd_pcm_lib_period_bytes(subs);
213 rec->nperiods = rec->dma_size / rec->period_size;
215 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
217 /* set up constraints */
218 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
219 snd_runtime_check(astr, return -EINVAL);
220 astr->cur_freqs = 1 << rate_index;
221 astr->cur_formats = 1 << runtime->format;
222 chip->rate_index = rate_index;
223 chip->format = runtime->format;
225 /* We really want to execute a DMA stop command, after the AWACS
227 * For reasons I don't understand, it stops the hissing noise
228 * common to many PowerBook G3 systems (like mine :-).
230 spin_lock_irqsave(&chip->reg_lock, flags);
231 snd_pmac_dma_stop(rec);
232 if (rec->stream == SNDRV_PCM_STREAM_PLAYBACK) {
233 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
234 snd_pmac_dma_set_command(rec, &chip->extra_dma);
235 snd_pmac_dma_run(rec, RUN);
237 /* continuous DMA memory type doesn't provide the physical address,
238 * so we need to resolve the address here...
240 offset = virt_to_bus(runtime->dma_area);
241 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
242 st_le32(&cp->phy_addr, offset);
243 st_le16(&cp->req_count, rec->period_size);
244 /*st_le16(&cp->res_count, 0);*/
245 st_le16(&cp->xfer_status, 0);
246 offset += rec->period_size;
249 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
250 st_le32(&cp->cmd_dep, rec->cmd.addr);
252 snd_pmac_dma_stop(rec);
253 snd_pmac_dma_set_command(rec, &rec->cmd);
254 spin_unlock_irqrestore(&chip->reg_lock, flags);
263 static int snd_pmac_pcm_trigger(pmac_t *chip, pmac_stream_t *rec,
264 snd_pcm_substream_t *subs, int cmd)
267 volatile struct dbdma_cmd *cp;
271 case SNDRV_PCM_TRIGGER_START:
272 case SNDRV_PCM_TRIGGER_RESUME:
275 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
276 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
277 spin_lock_irqsave(&chip->reg_lock, flags);
278 snd_pmac_beep_stop(chip);
279 snd_pmac_pcm_set_format(chip);
280 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
281 out_le16(&cp->command, command);
282 snd_pmac_dma_set_command(rec, &rec->cmd);
283 (void)in_le32(&rec->dma->status);
284 snd_pmac_dma_run(rec, RUN|WAKE);
286 spin_unlock_irqrestore(&chip->reg_lock, flags);
289 case SNDRV_PCM_TRIGGER_STOP:
290 case SNDRV_PCM_TRIGGER_SUSPEND:
291 spin_lock_irqsave(&chip->reg_lock, flags);
293 /*printk("stopped!!\n");*/
294 snd_pmac_dma_stop(rec);
295 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
296 out_le16(&cp->command, DBDMA_STOP);
297 spin_unlock_irqrestore(&chip->reg_lock, flags);
308 * return the current pointer
311 static snd_pcm_uframes_t snd_pmac_pcm_pointer(pmac_t *chip, pmac_stream_t *rec,
312 snd_pcm_substream_t *subs)
316 #if 1 /* hmm.. how can we get the current dma pointer?? */
318 volatile struct dbdma_cmd *cp = &rec->cmd.cmds[rec->cur_period];
319 stat = ld_le16(&cp->xfer_status);
320 if (stat & (ACTIVE|DEAD)) {
321 count = in_le16(&cp->res_count);
322 count = rec->period_size - count;
325 count += rec->cur_period * rec->period_size;
326 /*printk("pointer=%d\n", count);*/
327 return bytes_to_frames(subs->runtime, count);
334 static int snd_pmac_playback_prepare(snd_pcm_substream_t *subs)
336 pmac_t *chip = snd_pcm_substream_chip(subs);
337 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
340 static int snd_pmac_playback_trigger(snd_pcm_substream_t *subs,
343 pmac_t *chip = snd_pcm_substream_chip(subs);
344 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
347 static snd_pcm_uframes_t snd_pmac_playback_pointer(snd_pcm_substream_t *subs)
349 pmac_t *chip = snd_pcm_substream_chip(subs);
350 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
358 static int snd_pmac_capture_prepare(snd_pcm_substream_t *subs)
360 pmac_t *chip = snd_pcm_substream_chip(subs);
361 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
364 static int snd_pmac_capture_trigger(snd_pcm_substream_t *subs,
367 pmac_t *chip = snd_pcm_substream_chip(subs);
368 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
371 static snd_pcm_uframes_t snd_pmac_capture_pointer(snd_pcm_substream_t *subs)
373 pmac_t *chip = snd_pcm_substream_chip(subs);
374 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
379 * update playback/capture pointer from interrupts
381 static void snd_pmac_pcm_update(pmac_t *chip, pmac_stream_t *rec)
383 volatile struct dbdma_cmd *cp;
387 spin_lock(&chip->reg_lock);
389 cp = &rec->cmd.cmds[rec->cur_period];
390 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
391 stat = ld_le16(&cp->xfer_status);
392 if (! (stat & ACTIVE))
394 /*printk("update frag %d\n", rec->cur_period);*/
395 st_le16(&cp->xfer_status, 0);
396 st_le16(&cp->req_count, rec->period_size);
397 /*st_le16(&cp->res_count, 0);*/
399 if (rec->cur_period >= rec->nperiods) {
404 spin_unlock(&chip->reg_lock);
405 snd_pcm_period_elapsed(rec->substream);
406 spin_lock(&chip->reg_lock);
409 spin_unlock(&chip->reg_lock);
417 static snd_pcm_hardware_t snd_pmac_playback =
419 .info = (SNDRV_PCM_INFO_INTERLEAVED |
420 SNDRV_PCM_INFO_MMAP |
421 SNDRV_PCM_INFO_MMAP_VALID |
422 SNDRV_PCM_INFO_RESUME),
423 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
424 .rates = SNDRV_PCM_RATE_8000_44100,
429 .buffer_bytes_max = 32768,
430 .period_bytes_min = 256,
431 .period_bytes_max = 16384,
433 .periods_max = PMAC_MAX_FRAGS,
436 static snd_pcm_hardware_t snd_pmac_capture =
438 .info = (SNDRV_PCM_INFO_INTERLEAVED |
439 SNDRV_PCM_INFO_MMAP |
440 SNDRV_PCM_INFO_MMAP_VALID |
441 SNDRV_PCM_INFO_RESUME),
442 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
443 .rates = SNDRV_PCM_RATE_8000_44100,
448 .buffer_bytes_max = 32768,
449 .period_bytes_min = 256,
450 .period_bytes_max = 16384,
452 .periods_max = PMAC_MAX_FRAGS,
457 static int snd_pmac_hw_rule_rate(snd_pcm_hw_params_t *params,
458 snd_pcm_hw_rule_t *rule)
460 pmac_t *chip = rule->private;
461 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
462 int i, freq_table[8], num_freqs;
464 snd_runtime_check(rec, return -EINVAL);
466 for (i = chip->num_freqs - 1; i >= 0; i--) {
467 if (rec->cur_freqs & (1 << i))
468 freq_table[num_freqs++] = chip->freq_table[i];
471 return snd_interval_list(hw_param_interval(params, rule->var),
472 num_freqs, freq_table, 0);
475 static int snd_pmac_hw_rule_format(snd_pcm_hw_params_t *params,
476 snd_pcm_hw_rule_t *rule)
478 pmac_t *chip = rule->private;
479 pmac_stream_t *rec = snd_pmac_get_stream(chip, rule->deps[0]);
481 snd_runtime_check(rec, return -EINVAL);
482 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
487 static int snd_pmac_pcm_open(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
489 snd_pcm_runtime_t *runtime = subs->runtime;
491 static int typical_freqs[] = {
498 static int typical_freq_flags[] = {
499 SNDRV_PCM_RATE_48000,
500 SNDRV_PCM_RATE_44100,
501 SNDRV_PCM_RATE_22050,
502 SNDRV_PCM_RATE_11025,
506 /* look up frequency table and fill bit mask */
507 runtime->hw.rates = 0;
508 fflags = chip->freqs_ok;
509 for (i = 0; typical_freqs[i]; i++) {
510 for (j = 0; j < chip->num_freqs; j++) {
511 if ((chip->freqs_ok & (1 << j)) &&
512 chip->freq_table[j] == typical_freqs[i]) {
513 runtime->hw.rates |= typical_freq_flags[i];
519 if (fflags) /* rest */
520 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
522 /* check for minimum and maximum rates */
523 for (i = 0; i < chip->num_freqs; i++) {
524 if (chip->freqs_ok & (1 << i)) {
525 runtime->hw.rate_max = chip->freq_table[i];
529 for (i = chip->num_freqs - 1; i >= 0; i--) {
530 if (chip->freqs_ok & (1 << i)) {
531 runtime->hw.rate_min = chip->freq_table[i];
535 runtime->hw.formats = chip->formats_ok;
536 if (chip->can_capture) {
537 if (! chip->can_duplex)
538 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
539 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
541 runtime->private_data = rec;
542 rec->substream = subs;
544 #if 0 /* FIXME: still under development.. */
545 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
546 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
547 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
548 snd_pmac_hw_rule_format, chip, rec->stream, -1);
551 runtime->hw.periods_max = rec->cmd.size - 1;
553 if (chip->can_duplex)
554 snd_pcm_set_sync(subs);
559 static int snd_pmac_pcm_close(pmac_t *chip, pmac_stream_t *rec, snd_pcm_substream_t *subs)
563 snd_pmac_dma_stop(rec);
565 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
566 snd_runtime_check(astr, return -EINVAL);
568 /* reset constraints */
569 astr->cur_freqs = chip->freqs_ok;
570 astr->cur_formats = chip->formats_ok;
575 static int snd_pmac_playback_open(snd_pcm_substream_t *subs)
577 pmac_t *chip = snd_pcm_substream_chip(subs);
579 subs->runtime->hw = snd_pmac_playback;
580 return snd_pmac_pcm_open(chip, &chip->playback, subs);
583 static int snd_pmac_capture_open(snd_pcm_substream_t *subs)
585 pmac_t *chip = snd_pcm_substream_chip(subs);
587 subs->runtime->hw = snd_pmac_capture;
588 return snd_pmac_pcm_open(chip, &chip->capture, subs);
591 static int snd_pmac_playback_close(snd_pcm_substream_t *subs)
593 pmac_t *chip = snd_pcm_substream_chip(subs);
595 return snd_pmac_pcm_close(chip, &chip->playback, subs);
598 static int snd_pmac_capture_close(snd_pcm_substream_t *subs)
600 pmac_t *chip = snd_pcm_substream_chip(subs);
602 return snd_pmac_pcm_close(chip, &chip->capture, subs);
608 static snd_pcm_ops_t snd_pmac_playback_ops = {
609 .open = snd_pmac_playback_open,
610 .close = snd_pmac_playback_close,
611 .ioctl = snd_pcm_lib_ioctl,
612 .hw_params = snd_pmac_pcm_hw_params,
613 .hw_free = snd_pmac_pcm_hw_free,
614 .prepare = snd_pmac_playback_prepare,
615 .trigger = snd_pmac_playback_trigger,
616 .pointer = snd_pmac_playback_pointer,
619 static snd_pcm_ops_t snd_pmac_capture_ops = {
620 .open = snd_pmac_capture_open,
621 .close = snd_pmac_capture_close,
622 .ioctl = snd_pcm_lib_ioctl,
623 .hw_params = snd_pmac_pcm_hw_params,
624 .hw_free = snd_pmac_pcm_hw_free,
625 .prepare = snd_pmac_capture_prepare,
626 .trigger = snd_pmac_capture_trigger,
627 .pointer = snd_pmac_capture_pointer,
630 static void pmac_pcm_free(snd_pcm_t *pcm)
632 snd_pcm_lib_preallocate_free_for_all(pcm);
635 int __init snd_pmac_pcm_new(pmac_t *chip)
639 int num_captures = 1;
641 if (! chip->can_capture)
643 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
647 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
648 if (chip->can_capture)
649 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
651 pcm->private_data = chip;
652 pcm->private_free = pmac_pcm_free;
654 strcpy(pcm->name, chip->card->shortname);
657 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
658 if (chip->can_byte_swap)
659 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
661 chip->playback.cur_formats = chip->formats_ok;
662 chip->capture.cur_formats = chip->formats_ok;
663 chip->playback.cur_freqs = chip->freqs_ok;
664 chip->capture.cur_freqs = chip->freqs_ok;
666 /* preallocate 64k buffer */
667 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
668 snd_dma_continuous_data(GFP_KERNEL),
669 64 * 1024, 64 * 1024);
675 static void snd_pmac_dbdma_reset(pmac_t *chip)
677 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
678 snd_pmac_wait_ack(&chip->playback);
679 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
680 snd_pmac_wait_ack(&chip->capture);
688 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
690 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
691 snd_pmac_pcm_update(chip, &chip->playback);
697 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
699 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
700 snd_pmac_pcm_update(chip, &chip->capture);
706 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
708 pmac_t *chip = snd_magic_cast(pmac_t, devid, return);
709 int ctrl = in_le32(&chip->awacs->control);
711 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
712 if (ctrl & MASK_PORTCHG) {
713 /* do something when headphone is plugged/unplugged? */
714 if (chip->update_automute)
715 chip->update_automute(chip, 1);
717 if (ctrl & MASK_CNTLERR) {
718 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
719 if (err && chip->model <= PMAC_SCREAMER)
720 snd_printk(KERN_DEBUG "error %x\n", err);
722 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
723 out_le32(&chip->awacs->control, ctrl);
729 * a wrapper to feature call for compatibility
731 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
732 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
734 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
735 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
737 if (chip->is_pbook_G3) {
739 feature_clear(chip->node, FEATURE_Sound_power);
740 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
741 big_mdelay(1000); /* XXX */
744 if (chip->is_pbook_3400) {
745 feature_set(chip->node, FEATURE_IOBUS_enable);
750 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
751 #define snd_pmac_sound_feature(chip,enable) /**/
752 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
758 static int snd_pmac_free(pmac_t *chip)
763 if (chip->initialized) {
764 snd_pmac_dbdma_reset(chip);
765 /* disable interrupts from awacs interface */
766 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
769 snd_pmac_sound_feature(chip, 0);
770 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
771 snd_pmac_unregister_sleep_notifier(chip);
774 /* clean up mixer if any */
775 if (chip->mixer_free)
776 chip->mixer_free(chip);
778 /* release resources */
780 free_irq(chip->irq, (void*)chip);
781 if (chip->tx_irq >= 0)
782 free_irq(chip->tx_irq, (void*)chip);
783 if (chip->rx_irq >= 0)
784 free_irq(chip->rx_irq, (void*)chip);
785 snd_pmac_dbdma_free(&chip->playback.cmd);
786 snd_pmac_dbdma_free(&chip->capture.cmd);
787 snd_pmac_dbdma_free(&chip->extra_dma);
788 if (chip->macio_base)
789 iounmap(chip->macio_base);
790 if (chip->latch_base)
791 iounmap(chip->latch_base);
793 iounmap((void*)chip->awacs);
794 if (chip->playback.dma)
795 iounmap((void*)chip->playback.dma);
796 if (chip->capture.dma)
797 iounmap((void*)chip->capture.dma);
799 for (i = 0; i < 3; i++) {
800 if (chip->of_requested & (1 << i))
801 release_OF_resource(chip->node, i);
804 snd_magic_kfree(chip);
812 static int snd_pmac_dev_free(snd_device_t *device)
814 pmac_t *chip = snd_magic_cast(pmac_t, device->device_data, return -ENXIO);
815 return snd_pmac_free(chip);
820 * check the machine support byteswap (little-endian)
823 static void __init detect_byte_swap(pmac_t *chip)
825 struct device_node *mio;
827 /* if seems that Keylargo can't byte-swap */
828 for (mio = chip->node->parent; mio; mio = mio->parent) {
829 if (strcmp(mio->name, "mac-io") == 0) {
830 if (device_is_compatible(mio, "Keylargo"))
831 chip->can_byte_swap = 0;
836 /* it seems the Pismo & iBook can't byte-swap in hardware. */
837 if (machine_is_compatible("PowerBook3,1") ||
838 machine_is_compatible("PowerBook2,1"))
839 chip->can_byte_swap = 0 ;
841 if (machine_is_compatible("PowerBook2,1"))
842 chip->can_duplex = 0;
847 * detect a sound chip
849 static int __init snd_pmac_detect(pmac_t *chip)
851 struct device_node *sound;
852 unsigned int *prop, l;
854 if (_machine != _MACH_Pmac)
859 chip->freqs_ok = 0xff; /* all ok */
860 chip->model = PMAC_AWACS;
861 chip->can_byte_swap = 1;
862 chip->can_duplex = 1;
863 chip->can_capture = 1;
865 chip->freq_table = awacs_freqs;
867 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
869 /* check machine type */
870 if (machine_is_compatible("AAPL,3400/2400")
871 || machine_is_compatible("AAPL,3500"))
872 chip->is_pbook_3400 = 1;
873 else if (machine_is_compatible("PowerBook1,1")
874 || machine_is_compatible("AAPL,PowerBook1998"))
875 chip->is_pbook_G3 = 1;
876 chip->node = find_devices("awacs");
881 * powermac G3 models have a node called "davbus"
882 * with a child called "sound".
884 chip->node = find_devices("davbus");
886 * if we didn't find a davbus device, try 'i2s-a' since
887 * this seems to be what iBooks have
890 chip->node = find_devices("i2s-a");
893 sound = find_devices("sound");
894 while (sound && sound->parent != chip->node)
898 prop = (unsigned int *) get_property(sound, "sub-frame", 0);
899 if (prop && *prop < 16)
900 chip->subframe = *prop;
901 /* This should be verified on older screamers */
902 if (device_is_compatible(sound, "screamer")) {
903 chip->model = PMAC_SCREAMER;
904 // chip->can_byte_swap = 0; /* FIXME: check this */
906 if (device_is_compatible(sound, "burgundy")) {
907 chip->model = PMAC_BURGUNDY;
908 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
910 if (device_is_compatible(sound, "daca")) {
911 chip->model = PMAC_DACA;
912 chip->can_capture = 0; /* no capture */
913 chip->can_duplex = 0;
914 // chip->can_byte_swap = 0; /* FIXME: check this */
915 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
917 if (device_is_compatible(sound, "tumbler")) {
918 chip->model = PMAC_TUMBLER;
919 chip->can_capture = 0; /* no capture */
920 chip->can_duplex = 0;
921 // chip->can_byte_swap = 0; /* FIXME: check this */
923 chip->freq_table = tumbler_freqs;
924 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
926 if (device_is_compatible(sound, "snapper")) {
927 chip->model = PMAC_SNAPPER;
928 // chip->can_byte_swap = 0; /* FIXME: check this */
930 chip->freq_table = tumbler_freqs;
931 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
933 prop = (unsigned int *)get_property(sound, "device-id", 0);
935 chip->device_id = *prop;
936 chip->has_iic = (find_devices("perch") != NULL);
938 detect_byte_swap(chip);
940 /* look for a property saying what sample rates
942 prop = (unsigned int *) get_property(sound, "sample-rates", &l);
944 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
948 for (l /= sizeof(int); l > 0; --l) {
949 unsigned int r = *prop++;
950 /* Apple 'Fixed' format */
953 for (i = 0; i < chip->num_freqs; ++i) {
954 if (r == chip->freq_table[i]) {
955 chip->freqs_ok |= (1 << i);
961 /* assume only 44.1khz */
969 * exported - boolean info callbacks for ease of programming
971 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
973 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
975 uinfo->value.integer.min = 0;
976 uinfo->value.integer.max = 1;
980 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
982 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
984 uinfo->value.integer.min = 0;
985 uinfo->value.integer.max = 1;
989 #ifdef PMAC_SUPPORT_AUTOMUTE
993 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
995 pmac_t *chip = snd_kcontrol_chip(kcontrol);
996 ucontrol->value.integer.value[0] = chip->auto_mute;
1000 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1002 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1003 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1004 chip->auto_mute = ucontrol->value.integer.value[0];
1005 if (chip->update_automute)
1006 chip->update_automute(chip, 1);
1012 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1014 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1015 if (chip->detect_headphone)
1016 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1018 ucontrol->value.integer.value[0] = 0;
1022 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1023 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1024 .name = "Auto Mute Switch",
1025 .info = snd_pmac_boolean_mono_info,
1026 .get = pmac_auto_mute_get,
1027 .put = pmac_auto_mute_put,
1029 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1030 .name = "Headphone Detection",
1031 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1032 .info = snd_pmac_boolean_mono_info,
1033 .get = pmac_hp_detect_get,
1037 int __init snd_pmac_add_automute(pmac_t *chip)
1040 chip->auto_mute = 1;
1041 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1044 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1045 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1047 #endif /* PMAC_SUPPORT_AUTOMUTE */
1050 * create and detect a pmac chip record
1052 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1055 struct device_node *np;
1057 static snd_device_ops_t ops = {
1058 .dev_free = snd_pmac_dev_free,
1061 snd_runtime_check(chip_return, return -EINVAL);
1062 *chip_return = NULL;
1064 chip = snd_magic_kcalloc(pmac_t, 0, GFP_KERNEL);
1069 spin_lock_init(&chip->reg_lock);
1070 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1072 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1073 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1075 if ((err = snd_pmac_detect(chip)) < 0)
1078 if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1079 snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1080 snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1086 if (np->n_addrs < 3 || np->n_intrs < 3) {
1091 for (i = 0; i < 3; i++) {
1092 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1093 if (! request_OF_resource(np, i, name[i])) {
1094 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1098 chip->of_requested |= (1 << i);
1101 chip->awacs = (volatile struct awacs_regs *) ioremap(np->addrs[0].address, 0x1000);
1102 chip->playback.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[1].address, 0x100);
1103 chip->capture.dma = (volatile struct dbdma_regs *) ioremap(np->addrs[2].address, 0x100);
1104 if (chip->model <= PMAC_BURGUNDY) {
1105 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1106 "PMac", (void*)chip)) {
1107 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1111 chip->irq = np->intrs[0].line;
1113 if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1114 "PMac Output", (void*)chip)) {
1115 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1119 chip->tx_irq = np->intrs[1].line;
1120 if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1121 "PMac Input", (void*)chip)) {
1122 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1126 chip->rx_irq = np->intrs[2].line;
1128 snd_pmac_sound_feature(chip, 1);
1131 out_le32(&chip->awacs->control, 0x11);
1133 /* Powerbooks have odd ways of enabling inputs such as
1134 an expansion-bay CD or sound from an internal modem
1135 or a PC-card modem. */
1136 if (chip->is_pbook_3400) {
1137 /* Enable CD and PC-card sound inputs. */
1138 /* This is done by reading from address
1139 * f301a000, + 0x10 to enable the expansion-bay
1140 * CD sound input, + 0x80 to enable the PC-card
1141 * sound input. The 0x100 enables the SCSI bus
1144 chip->latch_base = (unsigned char *) ioremap (0xf301a000, 0x1000);
1145 in_8(chip->latch_base + 0x190);
1146 } else if (chip->is_pbook_G3) {
1147 struct device_node* mio;
1148 for (mio = chip->node->parent; mio; mio = mio->parent) {
1149 if (strcmp(mio->name, "mac-io") == 0
1150 && mio->n_addrs > 0) {
1151 chip->macio_base = (unsigned char *) ioremap
1152 (mio->addrs[0].address, 0x40);
1156 /* Enable CD sound input. */
1157 /* The relevant bits for writing to this byte are 0x8f.
1158 * I haven't found out what the 0x80 bit does.
1159 * For the 0xf bits, writing 3 or 7 enables the CD
1160 * input, any other value disables it. Values
1161 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1162 * 4, 6, 8 - f enable the input from the modem.
1164 if (chip->macio_base)
1165 out_8(chip->macio_base + 0x37, 3);
1168 /* Reset dbdma channels */
1169 snd_pmac_dbdma_reset(chip);
1171 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1172 /* add sleep notifier */
1173 snd_pmac_register_sleep_notifier(chip);
1174 card->set_power_state = snd_pmac_set_power_state;
1175 card->power_state_private_data = chip;
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 void snd_pmac_suspend(pmac_t *chip)
1202 unsigned long flags;
1203 snd_card_t *card = chip->card;
1205 if (card->power_state == SNDRV_CTL_POWER_D3hot)
1209 chip->suspend(chip);
1210 snd_pcm_suspend_all(chip->pcm);
1211 spin_lock_irqsave(&chip->reg_lock, flags);
1212 snd_pmac_beep_stop(chip);
1213 spin_unlock_irqrestore(&chip->reg_lock, flags);
1215 disable_irq(chip->irq);
1216 if (chip->tx_irq >= 0)
1217 disable_irq(chip->tx_irq);
1218 if (chip->rx_irq >= 0)
1219 disable_irq(chip->rx_irq);
1220 snd_pmac_sound_feature(chip, 0);
1221 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1224 static void snd_pmac_resume(pmac_t *chip)
1226 snd_card_t *card = chip->card;
1228 if (card->power_state == SNDRV_CTL_POWER_D0)
1231 snd_pmac_sound_feature(chip, 1);
1234 /* enable CD sound input */
1235 if (chip->macio_base && chip->is_pbook_G3) {
1236 out_8(chip->macio_base + 0x37, 3);
1237 } else if (chip->is_pbook_3400) {
1238 in_8(chip->latch_base + 0x190);
1241 snd_pmac_pcm_set_format(chip);
1244 enable_irq(chip->irq);
1245 if (chip->tx_irq >= 0)
1246 enable_irq(chip->tx_irq);
1247 if (chip->rx_irq >= 0)
1248 enable_irq(chip->rx_irq);
1250 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1253 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1254 * because we can't have any private data for notify callback.
1256 static pmac_t *sleeping_pmac = NULL;
1258 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1262 chip = sleeping_pmac;
1263 snd_runtime_check(chip, return 0);
1266 case PBOOK_SLEEP_NOW:
1267 snd_pmac_suspend(chip);
1270 snd_pmac_resume(chip);
1273 return PBOOK_SLEEP_OK;
1276 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1277 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1280 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1282 /* should be protected here.. */
1283 if (sleeping_pmac) {
1284 snd_printd("sleep notifier already reigistered\n");
1287 sleeping_pmac = chip;
1288 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1289 chip->sleep_registered = 1;
1293 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1295 if (! chip->sleep_registered)
1297 /* should be protected here.. */
1298 if (sleeping_pmac != chip)
1300 pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1301 sleeping_pmac = NULL;
1306 static int snd_pmac_set_power_state(snd_card_t *card, unsigned int power_state)
1308 pmac_t *chip = snd_magic_cast(pmac_t, card->power_state_private_data, return -ENXIO);
1309 switch (power_state) {
1310 case SNDRV_CTL_POWER_D0:
1311 case SNDRV_CTL_POWER_D1:
1312 case SNDRV_CTL_POWER_D2:
1313 snd_pmac_resume(chip);
1315 case SNDRV_CTL_POWER_D3hot:
1316 case SNDRV_CTL_POWER_D3cold:
1317 snd_pmac_suspend(chip);
1325 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */