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, pm_message_t state);
44 static int snd_pmac_resume(snd_card_t *card);
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 __iomem *)DBDMA_ALIGN(rec->space);
68 rec->addr = virt_to_bus(rec->cmds);
72 static void snd_pmac_dbdma_free(pmac_dbdma_t *rec)
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 __iomem *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 __iomem *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 __iomem *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);
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 __iomem *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 = 131072,
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 = 131072,
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[] = {
497 static int typical_freq_flags[] = {
498 SNDRV_PCM_RATE_44100,
499 SNDRV_PCM_RATE_22050,
500 SNDRV_PCM_RATE_11025,
504 /* look up frequency table and fill bit mask */
505 runtime->hw.rates = 0;
506 fflags = chip->freqs_ok;
507 for (i = 0; typical_freqs[i]; i++) {
508 for (j = 0; j < chip->num_freqs; j++) {
509 if ((chip->freqs_ok & (1 << j)) &&
510 chip->freq_table[j] == typical_freqs[i]) {
511 runtime->hw.rates |= typical_freq_flags[i];
517 if (fflags) /* rest */
518 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
520 /* check for minimum and maximum rates */
521 for (i = 0; i < chip->num_freqs; i++) {
522 if (chip->freqs_ok & (1 << i)) {
523 runtime->hw.rate_max = chip->freq_table[i];
527 for (i = chip->num_freqs - 1; i >= 0; i--) {
528 if (chip->freqs_ok & (1 << i)) {
529 runtime->hw.rate_min = chip->freq_table[i];
533 runtime->hw.formats = chip->formats_ok;
534 if (chip->can_capture) {
535 if (! chip->can_duplex)
536 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
537 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
539 runtime->private_data = rec;
540 rec->substream = subs;
542 #if 0 /* FIXME: still under development.. */
543 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
544 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
545 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
546 snd_pmac_hw_rule_format, chip, rec->stream, -1);
549 runtime->hw.periods_max = rec->cmd.size - 1;
551 if (chip->can_duplex)
552 snd_pcm_set_sync(subs);
554 /* constraints to fix choppy sound */
555 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
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;
653 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
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);
687 void snd_pmac_beep_dma_start(pmac_t *chip, int bytes, unsigned long addr, int speed)
689 pmac_stream_t *rec = &chip->playback;
691 snd_pmac_dma_stop(rec);
692 st_le16(&chip->extra_dma.cmds->req_count, bytes);
693 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
694 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
695 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
696 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
697 out_le32(&chip->awacs->control,
698 (in_le32(&chip->awacs->control) & ~0x1f00)
700 out_le32(&chip->awacs->byteswap, 0);
701 snd_pmac_dma_set_command(rec, &chip->extra_dma);
702 snd_pmac_dma_run(rec, RUN);
705 void snd_pmac_beep_dma_stop(pmac_t *chip)
707 snd_pmac_dma_stop(&chip->playback);
708 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
709 snd_pmac_pcm_set_format(chip); /* reset format */
717 snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
719 pmac_t *chip = devid;
720 snd_pmac_pcm_update(chip, &chip->playback);
726 snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
728 pmac_t *chip = devid;
729 snd_pmac_pcm_update(chip, &chip->capture);
735 snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
737 pmac_t *chip = devid;
738 int ctrl = in_le32(&chip->awacs->control);
740 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
741 if (ctrl & MASK_PORTCHG) {
742 /* do something when headphone is plugged/unplugged? */
743 if (chip->update_automute)
744 chip->update_automute(chip, 1);
746 if (ctrl & MASK_CNTLERR) {
747 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
748 if (err && chip->model <= PMAC_SCREAMER)
749 snd_printk(KERN_DEBUG "error %x\n", err);
751 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
752 out_le32(&chip->awacs->control, ctrl);
758 * a wrapper to feature call for compatibility
760 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
761 static void snd_pmac_sound_feature(pmac_t *chip, int enable)
763 #ifdef CONFIG_PPC_HAS_FEATURE_CALLS
764 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
766 if (chip->is_pbook_G3) {
768 feature_clear(chip->node, FEATURE_Sound_power);
769 feature_clear(chip->node, FEATURE_Sound_CLK_enable);
770 big_mdelay(1000); /* XXX */
773 if (chip->is_pbook_3400) {
774 feature_set(chip->node, FEATURE_IOBUS_enable);
779 #else /* CONFIG_PM && CONFIG_PMAC_PBOOK */
780 #define snd_pmac_sound_feature(chip,enable) /**/
781 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */
787 static int snd_pmac_free(pmac_t *chip)
792 if (chip->initialized) {
793 snd_pmac_dbdma_reset(chip);
794 /* disable interrupts from awacs interface */
795 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
798 snd_pmac_sound_feature(chip, 0);
799 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
800 snd_pmac_unregister_sleep_notifier(chip);
803 /* clean up mixer if any */
804 if (chip->mixer_free)
805 chip->mixer_free(chip);
807 snd_pmac_detach_beep(chip);
809 /* release resources */
811 free_irq(chip->irq, (void*)chip);
812 if (chip->tx_irq >= 0)
813 free_irq(chip->tx_irq, (void*)chip);
814 if (chip->rx_irq >= 0)
815 free_irq(chip->rx_irq, (void*)chip);
816 snd_pmac_dbdma_free(&chip->playback.cmd);
817 snd_pmac_dbdma_free(&chip->capture.cmd);
818 snd_pmac_dbdma_free(&chip->extra_dma);
819 if (chip->macio_base)
820 iounmap(chip->macio_base);
821 if (chip->latch_base)
822 iounmap(chip->latch_base);
824 iounmap(chip->awacs);
825 if (chip->playback.dma)
826 iounmap(chip->playback.dma);
827 if (chip->capture.dma)
828 iounmap(chip->capture.dma);
830 for (i = 0; i < 3; i++) {
831 if (chip->of_requested & (1 << i))
832 release_OF_resource(chip->node, i);
843 static int snd_pmac_dev_free(snd_device_t *device)
845 pmac_t *chip = device->device_data;
846 return snd_pmac_free(chip);
851 * check the machine support byteswap (little-endian)
854 static void __init detect_byte_swap(pmac_t *chip)
856 struct device_node *mio;
858 /* if seems that Keylargo can't byte-swap */
859 for (mio = chip->node->parent; mio; mio = mio->parent) {
860 if (strcmp(mio->name, "mac-io") == 0) {
861 if (device_is_compatible(mio, "Keylargo"))
862 chip->can_byte_swap = 0;
867 /* it seems the Pismo & iBook can't byte-swap in hardware. */
868 if (machine_is_compatible("PowerBook3,1") ||
869 machine_is_compatible("PowerBook2,1"))
870 chip->can_byte_swap = 0 ;
872 if (machine_is_compatible("PowerBook2,1"))
873 chip->can_duplex = 0;
878 * detect a sound chip
880 static int __init snd_pmac_detect(pmac_t *chip)
882 struct device_node *sound;
883 unsigned int *prop, l;
886 if (_machine != _MACH_Pmac)
891 chip->freqs_ok = 0xff; /* all ok */
892 chip->model = PMAC_AWACS;
893 chip->can_byte_swap = 1;
894 chip->can_duplex = 1;
895 chip->can_capture = 1;
896 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
897 chip->freq_table = awacs_freqs;
899 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
901 /* check machine type */
902 if (machine_is_compatible("AAPL,3400/2400")
903 || machine_is_compatible("AAPL,3500"))
904 chip->is_pbook_3400 = 1;
905 else if (machine_is_compatible("PowerBook1,1")
906 || machine_is_compatible("AAPL,PowerBook1998"))
907 chip->is_pbook_G3 = 1;
908 chip->node = find_devices("awacs");
913 * powermac G3 models have a node called "davbus"
914 * with a child called "sound".
916 chip->node = find_devices("davbus");
918 * if we didn't find a davbus device, try 'i2s-a' since
919 * this seems to be what iBooks have
922 chip->node = find_devices("i2s-a");
925 sound = find_devices("sound");
926 while (sound && sound->parent != chip->node)
930 prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
931 if (prop && *prop < 16)
932 chip->subframe = *prop;
933 prop = (unsigned int *) get_property(sound, "layout-id", NULL);
936 /* This should be verified on older screamers */
937 if (device_is_compatible(sound, "screamer")) {
938 chip->model = PMAC_SCREAMER;
939 // chip->can_byte_swap = 0; /* FIXME: check this */
941 if (device_is_compatible(sound, "burgundy")) {
942 chip->model = PMAC_BURGUNDY;
943 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
945 if (device_is_compatible(sound, "daca")) {
946 chip->model = PMAC_DACA;
947 chip->can_capture = 0; /* no capture */
948 chip->can_duplex = 0;
949 // chip->can_byte_swap = 0; /* FIXME: check this */
950 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
952 if (device_is_compatible(sound, "tumbler")) {
953 chip->model = PMAC_TUMBLER;
954 chip->can_capture = 0; /* no capture */
955 chip->can_duplex = 0;
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, "snapper")) {
962 chip->model = PMAC_SNAPPER;
963 // chip->can_byte_swap = 0; /* FIXME: check this */
964 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
965 chip->freq_table = tumbler_freqs;
966 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
968 if (device_is_compatible(sound, "AOAKeylargo") ||
969 device_is_compatible(sound, "AOAbase")) {
970 /* For now, only support very basic TAS3004 based machines with
971 * single frequency until proper i2s control is implemented
978 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
979 chip->model = PMAC_SNAPPER;
980 chip->can_byte_swap = 0; /* FIXME: check this */
981 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
985 prop = (unsigned int *)get_property(sound, "device-id", NULL);
987 chip->device_id = *prop;
988 chip->has_iic = (find_devices("perch") != NULL);
990 detect_byte_swap(chip);
992 /* look for a property saying what sample rates
994 prop = (unsigned int *) get_property(sound, "sample-rates", &l);
996 prop = (unsigned int *) get_property(sound, "output-frame-rates", &l);
1000 for (l /= sizeof(int); l > 0; --l) {
1001 unsigned int r = *prop++;
1002 /* Apple 'Fixed' format */
1005 for (i = 0; i < chip->num_freqs; ++i) {
1006 if (r == chip->freq_table[i]) {
1007 chip->freqs_ok |= (1 << i);
1013 /* assume only 44.1khz */
1021 * exported - boolean info callbacks for ease of programming
1023 int snd_pmac_boolean_stereo_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1025 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1027 uinfo->value.integer.min = 0;
1028 uinfo->value.integer.max = 1;
1032 int snd_pmac_boolean_mono_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1034 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1036 uinfo->value.integer.min = 0;
1037 uinfo->value.integer.max = 1;
1041 #ifdef PMAC_SUPPORT_AUTOMUTE
1045 static int pmac_auto_mute_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1047 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1048 ucontrol->value.integer.value[0] = chip->auto_mute;
1052 static int pmac_auto_mute_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1054 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1055 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1056 chip->auto_mute = ucontrol->value.integer.value[0];
1057 if (chip->update_automute)
1058 chip->update_automute(chip, 1);
1064 static int pmac_hp_detect_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1066 pmac_t *chip = snd_kcontrol_chip(kcontrol);
1067 if (chip->detect_headphone)
1068 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1070 ucontrol->value.integer.value[0] = 0;
1074 static snd_kcontrol_new_t auto_mute_controls[] __initdata = {
1075 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1076 .name = "Auto Mute Switch",
1077 .info = snd_pmac_boolean_mono_info,
1078 .get = pmac_auto_mute_get,
1079 .put = pmac_auto_mute_put,
1081 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1082 .name = "Headphone Detection",
1083 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1084 .info = snd_pmac_boolean_mono_info,
1085 .get = pmac_hp_detect_get,
1089 int __init snd_pmac_add_automute(pmac_t *chip)
1092 chip->auto_mute = 1;
1093 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1096 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1097 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1099 #endif /* PMAC_SUPPORT_AUTOMUTE */
1102 * create and detect a pmac chip record
1104 int __init snd_pmac_new(snd_card_t *card, pmac_t **chip_return)
1107 struct device_node *np;
1109 static snd_device_ops_t ops = {
1110 .dev_free = snd_pmac_dev_free,
1113 snd_runtime_check(chip_return, return -EINVAL);
1114 *chip_return = NULL;
1116 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1121 spin_lock_init(&chip->reg_lock);
1122 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1124 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1125 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1127 if ((err = snd_pmac_detect(chip)) < 0)
1130 if (snd_pmac_dbdma_alloc(&chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1131 snd_pmac_dbdma_alloc(&chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1132 snd_pmac_dbdma_alloc(&chip->extra_dma, 2) < 0) {
1138 if (np->n_addrs < 3 || np->n_intrs < 3) {
1143 for (i = 0; i < 3; i++) {
1144 static char *name[3] = { NULL, "- Tx DMA", "- Rx DMA" };
1145 if (! request_OF_resource(np, i, name[i])) {
1146 snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
1150 chip->of_requested |= (1 << i);
1153 chip->awacs = ioremap(np->addrs[0].address, 0x1000);
1154 chip->playback.dma = ioremap(np->addrs[1].address, 0x100);
1155 chip->capture.dma = ioremap(np->addrs[2].address, 0x100);
1156 if (chip->model <= PMAC_BURGUNDY) {
1157 if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
1158 "PMac", (void*)chip)) {
1159 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
1163 chip->irq = np->intrs[0].line;
1165 if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
1166 "PMac Output", (void*)chip)) {
1167 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
1171 chip->tx_irq = np->intrs[1].line;
1172 if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
1173 "PMac Input", (void*)chip)) {
1174 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
1178 chip->rx_irq = np->intrs[2].line;
1180 snd_pmac_sound_feature(chip, 1);
1183 out_le32(&chip->awacs->control, 0x11);
1185 /* Powerbooks have odd ways of enabling inputs such as
1186 an expansion-bay CD or sound from an internal modem
1187 or a PC-card modem. */
1188 if (chip->is_pbook_3400) {
1189 /* Enable CD and PC-card sound inputs. */
1190 /* This is done by reading from address
1191 * f301a000, + 0x10 to enable the expansion-bay
1192 * CD sound input, + 0x80 to enable the PC-card
1193 * sound input. The 0x100 enables the SCSI bus
1196 chip->latch_base = ioremap (0xf301a000, 0x1000);
1197 in_8(chip->latch_base + 0x190);
1198 } else if (chip->is_pbook_G3) {
1199 struct device_node* mio;
1200 for (mio = chip->node->parent; mio; mio = mio->parent) {
1201 if (strcmp(mio->name, "mac-io") == 0
1202 && mio->n_addrs > 0) {
1203 chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
1207 /* Enable CD sound input. */
1208 /* The relevant bits for writing to this byte are 0x8f.
1209 * I haven't found out what the 0x80 bit does.
1210 * For the 0xf bits, writing 3 or 7 enables the CD
1211 * input, any other value disables it. Values
1212 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1213 * 4, 6, 8 - f enable the input from the modem.
1215 if (chip->macio_base)
1216 out_8(chip->macio_base + 0x37, 3);
1219 /* Reset dbdma channels */
1220 snd_pmac_dbdma_reset(chip);
1222 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1223 /* add sleep notifier */
1224 if (! snd_pmac_register_sleep_notifier(chip))
1225 snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
1228 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1231 *chip_return = chip;
1235 snd_pmac_free(chip);
1241 * sleep notify for powerbook
1244 #if defined(CONFIG_PM) && defined(CONFIG_PMAC_PBOOK)
1247 * Save state when going to sleep, restore it afterwards.
1250 static int snd_pmac_suspend(snd_card_t *card, pm_message_t state)
1252 pmac_t *chip = card->pm_private_data;
1253 unsigned long flags;
1256 chip->suspend(chip);
1257 snd_pcm_suspend_all(chip->pcm);
1258 spin_lock_irqsave(&chip->reg_lock, flags);
1259 snd_pmac_beep_stop(chip);
1260 spin_unlock_irqrestore(&chip->reg_lock, flags);
1262 disable_irq(chip->irq);
1263 if (chip->tx_irq >= 0)
1264 disable_irq(chip->tx_irq);
1265 if (chip->rx_irq >= 0)
1266 disable_irq(chip->rx_irq);
1267 snd_pmac_sound_feature(chip, 0);
1271 static int snd_pmac_resume(snd_card_t *card)
1273 pmac_t *chip = card->pm_private_data;
1275 snd_pmac_sound_feature(chip, 1);
1278 /* enable CD sound input */
1279 if (chip->macio_base && chip->is_pbook_G3) {
1280 out_8(chip->macio_base + 0x37, 3);
1281 } else if (chip->is_pbook_3400) {
1282 in_8(chip->latch_base + 0x190);
1285 snd_pmac_pcm_set_format(chip);
1288 enable_irq(chip->irq);
1289 if (chip->tx_irq >= 0)
1290 enable_irq(chip->tx_irq);
1291 if (chip->rx_irq >= 0)
1292 enable_irq(chip->rx_irq);
1297 /* the chip is stored statically by snd_pmac_register_sleep_notifier
1298 * because we can't have any private data for notify callback.
1300 static pmac_t *sleeping_pmac = NULL;
1302 static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
1306 chip = sleeping_pmac;
1307 snd_runtime_check(chip, return 0);
1310 case PBOOK_SLEEP_NOW:
1311 snd_pmac_suspend(chip->card, PMSG_SUSPEND);
1314 snd_pmac_resume(chip->card);
1317 return PBOOK_SLEEP_OK;
1320 static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
1321 snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
1324 static int __init snd_pmac_register_sleep_notifier(pmac_t *chip)
1326 /* should be protected here.. */
1327 snd_assert(! sleeping_pmac, return -EBUSY);
1328 sleeping_pmac = chip;
1329 pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
1333 static int snd_pmac_unregister_sleep_notifier(pmac_t *chip)
1335 /* should be protected here.. */
1336 snd_assert(sleeping_pmac == chip, return -ENODEV);
1337 pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
1338 sleeping_pmac = NULL;
1342 #endif /* CONFIG_PM && CONFIG_PMAC_PBOOK */