Blackfin arch: Add option to priorize DMA over Core
[linux-2.6/openmoko-kernel.git] / sound / ppc / pmac.c
blob7a22f0f3784ab5e1f1c90401bdd8281d795cd2a5
1 /*
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>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/pci.h>
31 #include <linux/dma-mapping.h>
32 #include <sound/core.h>
33 #include "pmac.h"
34 #include <sound/pcm_params.h>
35 #include <asm/pmac_feature.h>
36 #include <asm/pci-bridge.h>
39 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
40 static int awacs_freqs[8] = {
41 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
43 /* fixed frequency table for tumbler */
44 static int tumbler_freqs[1] = {
45 44100
49 * allocate DBDMA command arrays
51 static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
53 unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
55 rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
56 &rec->dma_base, GFP_KERNEL);
57 if (rec->space == NULL)
58 return -ENOMEM;
59 rec->size = size;
60 memset(rec->space, 0, rsize);
61 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
62 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
64 return 0;
67 static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
69 if (rec->space) {
70 unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
72 dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
78 * pcm stuff
82 * look up frequency table
85 unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
87 int i, ok, found;
89 ok = rec->cur_freqs;
90 if (rate > chip->freq_table[0])
91 return 0;
92 found = 0;
93 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
94 if (! (ok & 1)) continue;
95 found = i;
96 if (rate >= chip->freq_table[i])
97 break;
99 return found;
103 * check whether another stream is active
105 static inline int another_stream(int stream)
107 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
108 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
112 * allocate buffers
114 static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
115 struct snd_pcm_hw_params *hw_params)
117 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
121 * release buffers
123 static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
125 snd_pcm_lib_free_pages(subs);
126 return 0;
130 * get a stream of the opposite direction
132 static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
134 switch (stream) {
135 case SNDRV_PCM_STREAM_PLAYBACK:
136 return &chip->playback;
137 case SNDRV_PCM_STREAM_CAPTURE:
138 return &chip->capture;
139 default:
140 snd_BUG();
141 return NULL;
146 * wait while run status is on
148 static inline void
149 snd_pmac_wait_ack(struct pmac_stream *rec)
151 int timeout = 50000;
152 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
153 udelay(1);
157 * set the format and rate to the chip.
158 * call the lowlevel function if defined (e.g. for AWACS).
160 static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
162 /* set up frequency and format */
163 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
164 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
165 if (chip->set_format)
166 chip->set_format(chip);
170 * stop the DMA transfer
172 static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
174 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
175 snd_pmac_wait_ack(rec);
179 * set the command pointer address
181 static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
183 out_le32(&rec->dma->cmdptr, cmd->addr);
187 * start the DMA
189 static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
191 out_le32(&rec->dma->control, status | (status << 16));
196 * prepare playback/capture stream
198 static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
200 int i;
201 volatile struct dbdma_cmd __iomem *cp;
202 struct snd_pcm_runtime *runtime = subs->runtime;
203 int rate_index;
204 long offset;
205 struct pmac_stream *astr;
207 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
208 rec->period_size = snd_pcm_lib_period_bytes(subs);
209 rec->nperiods = rec->dma_size / rec->period_size;
210 rec->cur_period = 0;
211 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
213 /* set up constraints */
214 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
215 if (! astr)
216 return -EINVAL;
217 astr->cur_freqs = 1 << rate_index;
218 astr->cur_formats = 1 << runtime->format;
219 chip->rate_index = rate_index;
220 chip->format = runtime->format;
222 /* We really want to execute a DMA stop command, after the AWACS
223 * is initialized.
224 * For reasons I don't understand, it stops the hissing noise
225 * common to many PowerBook G3 systems and random noise otherwise
226 * captured on iBook2's about every third time. -ReneR
228 spin_lock_irq(&chip->reg_lock);
229 snd_pmac_dma_stop(rec);
230 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
231 snd_pmac_dma_set_command(rec, &chip->extra_dma);
232 snd_pmac_dma_run(rec, RUN);
233 spin_unlock_irq(&chip->reg_lock);
234 mdelay(5);
235 spin_lock_irq(&chip->reg_lock);
236 /* continuous DMA memory type doesn't provide the physical address,
237 * so we need to resolve the address here...
239 offset = runtime->dma_addr;
240 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
241 st_le32(&cp->phy_addr, offset);
242 st_le16(&cp->req_count, rec->period_size);
243 /*st_le16(&cp->res_count, 0);*/
244 st_le16(&cp->xfer_status, 0);
245 offset += rec->period_size;
247 /* make loop */
248 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
249 st_le32(&cp->cmd_dep, rec->cmd.addr);
251 snd_pmac_dma_stop(rec);
252 snd_pmac_dma_set_command(rec, &rec->cmd);
253 spin_unlock_irq(&chip->reg_lock);
255 return 0;
260 * PCM trigger/stop
262 static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
263 struct snd_pcm_substream *subs, int cmd)
265 volatile struct dbdma_cmd __iomem *cp;
266 int i, command;
268 switch (cmd) {
269 case SNDRV_PCM_TRIGGER_START:
270 case SNDRV_PCM_TRIGGER_RESUME:
271 if (rec->running)
272 return -EBUSY;
273 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
274 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
275 spin_lock(&chip->reg_lock);
276 snd_pmac_beep_stop(chip);
277 snd_pmac_pcm_set_format(chip);
278 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
279 out_le16(&cp->command, command);
280 snd_pmac_dma_set_command(rec, &rec->cmd);
281 (void)in_le32(&rec->dma->status);
282 snd_pmac_dma_run(rec, RUN|WAKE);
283 rec->running = 1;
284 spin_unlock(&chip->reg_lock);
285 break;
287 case SNDRV_PCM_TRIGGER_STOP:
288 case SNDRV_PCM_TRIGGER_SUSPEND:
289 spin_lock(&chip->reg_lock);
290 rec->running = 0;
291 /*printk("stopped!!\n");*/
292 snd_pmac_dma_stop(rec);
293 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
294 out_le16(&cp->command, DBDMA_STOP);
295 spin_unlock(&chip->reg_lock);
296 break;
298 default:
299 return -EINVAL;
302 return 0;
306 * return the current pointer
308 inline
309 static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
310 struct pmac_stream *rec,
311 struct snd_pcm_substream *subs)
313 int count = 0;
315 #if 1 /* hmm.. how can we get the current dma pointer?? */
316 int stat;
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);
321 if (count)
322 count = rec->period_size - count;
324 #endif
325 count += rec->cur_period * rec->period_size;
326 /*printk("pointer=%d\n", count);*/
327 return bytes_to_frames(subs->runtime, count);
331 * playback
334 static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
336 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
337 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
340 static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
341 int cmd)
343 struct snd_pmac *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(struct snd_pcm_substream *subs)
349 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
350 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
355 * capture
358 static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
360 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
361 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
364 static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
365 int cmd)
367 struct snd_pmac *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(struct snd_pcm_substream *subs)
373 struct snd_pmac *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(struct snd_pmac *chip, struct pmac_stream *rec)
383 volatile struct dbdma_cmd __iomem *cp;
384 int c;
385 int stat;
387 spin_lock(&chip->reg_lock);
388 if (rec->running) {
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))
393 break;
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);*/
398 rec->cur_period++;
399 if (rec->cur_period >= rec->nperiods) {
400 rec->cur_period = 0;
401 cp = rec->cmd.cmds;
402 } else
403 cp++;
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);
414 * hw info
417 static struct snd_pcm_hardware 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,
425 .rate_min = 7350,
426 .rate_max = 44100,
427 .channels_min = 2,
428 .channels_max = 2,
429 .buffer_bytes_max = 131072,
430 .period_bytes_min = 256,
431 .period_bytes_max = 16384,
432 .periods_min = 3,
433 .periods_max = PMAC_MAX_FRAGS,
436 static struct snd_pcm_hardware 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,
444 .rate_min = 7350,
445 .rate_max = 44100,
446 .channels_min = 2,
447 .channels_max = 2,
448 .buffer_bytes_max = 131072,
449 .period_bytes_min = 256,
450 .period_bytes_max = 16384,
451 .periods_min = 3,
452 .periods_max = PMAC_MAX_FRAGS,
456 #if 0 // NYI
457 static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
458 struct snd_pcm_hw_rule *rule)
460 struct snd_pmac *chip = rule->private;
461 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
462 int i, freq_table[8], num_freqs;
464 if (! rec)
465 return -EINVAL;
466 num_freqs = 0;
467 for (i = chip->num_freqs - 1; i >= 0; i--) {
468 if (rec->cur_freqs & (1 << i))
469 freq_table[num_freqs++] = chip->freq_table[i];
472 return snd_interval_list(hw_param_interval(params, rule->var),
473 num_freqs, freq_table, 0);
476 static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
477 struct snd_pcm_hw_rule *rule)
479 struct snd_pmac *chip = rule->private;
480 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
482 if (! rec)
483 return -EINVAL;
484 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
485 rec->cur_formats);
487 #endif // NYI
489 static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
490 struct snd_pcm_substream *subs)
492 struct snd_pcm_runtime *runtime = subs->runtime;
493 int i, j, fflags;
494 static int typical_freqs[] = {
495 44100,
496 22050,
497 11025,
500 static int typical_freq_flags[] = {
501 SNDRV_PCM_RATE_44100,
502 SNDRV_PCM_RATE_22050,
503 SNDRV_PCM_RATE_11025,
507 /* look up frequency table and fill bit mask */
508 runtime->hw.rates = 0;
509 fflags = chip->freqs_ok;
510 for (i = 0; typical_freqs[i]; i++) {
511 for (j = 0; j < chip->num_freqs; j++) {
512 if ((chip->freqs_ok & (1 << j)) &&
513 chip->freq_table[j] == typical_freqs[i]) {
514 runtime->hw.rates |= typical_freq_flags[i];
515 fflags &= ~(1 << j);
516 break;
520 if (fflags) /* rest */
521 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
523 /* check for minimum and maximum rates */
524 for (i = 0; i < chip->num_freqs; i++) {
525 if (chip->freqs_ok & (1 << i)) {
526 runtime->hw.rate_max = chip->freq_table[i];
527 break;
530 for (i = chip->num_freqs - 1; i >= 0; i--) {
531 if (chip->freqs_ok & (1 << i)) {
532 runtime->hw.rate_min = chip->freq_table[i];
533 break;
536 runtime->hw.formats = chip->formats_ok;
537 if (chip->can_capture) {
538 if (! chip->can_duplex)
539 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
540 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
542 runtime->private_data = rec;
543 rec->substream = subs;
545 #if 0 /* FIXME: still under development.. */
546 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
547 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
548 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
549 snd_pmac_hw_rule_format, chip, rec->stream, -1);
550 #endif
552 runtime->hw.periods_max = rec->cmd.size - 1;
554 if (chip->can_duplex)
555 snd_pcm_set_sync(subs);
557 /* constraints to fix choppy sound */
558 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
559 return 0;
562 static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
563 struct snd_pcm_substream *subs)
565 struct pmac_stream *astr;
567 snd_pmac_dma_stop(rec);
569 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
570 if (! astr)
571 return -EINVAL;
573 /* reset constraints */
574 astr->cur_freqs = chip->freqs_ok;
575 astr->cur_formats = chip->formats_ok;
577 return 0;
580 static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
582 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
584 subs->runtime->hw = snd_pmac_playback;
585 return snd_pmac_pcm_open(chip, &chip->playback, subs);
588 static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
590 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
592 subs->runtime->hw = snd_pmac_capture;
593 return snd_pmac_pcm_open(chip, &chip->capture, subs);
596 static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
598 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
600 return snd_pmac_pcm_close(chip, &chip->playback, subs);
603 static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
605 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
607 return snd_pmac_pcm_close(chip, &chip->capture, subs);
613 static struct snd_pcm_ops snd_pmac_playback_ops = {
614 .open = snd_pmac_playback_open,
615 .close = snd_pmac_playback_close,
616 .ioctl = snd_pcm_lib_ioctl,
617 .hw_params = snd_pmac_pcm_hw_params,
618 .hw_free = snd_pmac_pcm_hw_free,
619 .prepare = snd_pmac_playback_prepare,
620 .trigger = snd_pmac_playback_trigger,
621 .pointer = snd_pmac_playback_pointer,
624 static struct snd_pcm_ops snd_pmac_capture_ops = {
625 .open = snd_pmac_capture_open,
626 .close = snd_pmac_capture_close,
627 .ioctl = snd_pcm_lib_ioctl,
628 .hw_params = snd_pmac_pcm_hw_params,
629 .hw_free = snd_pmac_pcm_hw_free,
630 .prepare = snd_pmac_capture_prepare,
631 .trigger = snd_pmac_capture_trigger,
632 .pointer = snd_pmac_capture_pointer,
635 int __init snd_pmac_pcm_new(struct snd_pmac *chip)
637 struct snd_pcm *pcm;
638 int err;
639 int num_captures = 1;
641 if (! chip->can_capture)
642 num_captures = 0;
643 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
644 if (err < 0)
645 return err;
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->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
653 strcpy(pcm->name, chip->card->shortname);
654 chip->pcm = pcm;
656 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
657 if (chip->can_byte_swap)
658 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
660 chip->playback.cur_formats = chip->formats_ok;
661 chip->capture.cur_formats = chip->formats_ok;
662 chip->playback.cur_freqs = chip->freqs_ok;
663 chip->capture.cur_freqs = chip->freqs_ok;
665 /* preallocate 64k buffer */
666 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
667 &chip->pdev->dev,
668 64 * 1024, 64 * 1024);
670 return 0;
674 static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
676 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
677 snd_pmac_wait_ack(&chip->playback);
678 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
679 snd_pmac_wait_ack(&chip->capture);
684 * handling beep
686 void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
688 struct pmac_stream *rec = &chip->playback;
690 snd_pmac_dma_stop(rec);
691 st_le16(&chip->extra_dma.cmds->req_count, bytes);
692 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
693 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
694 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
695 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
696 out_le32(&chip->awacs->control,
697 (in_le32(&chip->awacs->control) & ~0x1f00)
698 | (speed << 8));
699 out_le32(&chip->awacs->byteswap, 0);
700 snd_pmac_dma_set_command(rec, &chip->extra_dma);
701 snd_pmac_dma_run(rec, RUN);
704 void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
706 snd_pmac_dma_stop(&chip->playback);
707 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
708 snd_pmac_pcm_set_format(chip); /* reset format */
713 * interrupt handlers
715 static irqreturn_t
716 snd_pmac_tx_intr(int irq, void *devid)
718 struct snd_pmac *chip = devid;
719 snd_pmac_pcm_update(chip, &chip->playback);
720 return IRQ_HANDLED;
724 static irqreturn_t
725 snd_pmac_rx_intr(int irq, void *devid)
727 struct snd_pmac *chip = devid;
728 snd_pmac_pcm_update(chip, &chip->capture);
729 return IRQ_HANDLED;
733 static irqreturn_t
734 snd_pmac_ctrl_intr(int irq, void *devid)
736 struct snd_pmac *chip = devid;
737 int ctrl = in_le32(&chip->awacs->control);
739 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
740 if (ctrl & MASK_PORTCHG) {
741 /* do something when headphone is plugged/unplugged? */
742 if (chip->update_automute)
743 chip->update_automute(chip, 1);
745 if (ctrl & MASK_CNTLERR) {
746 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
747 if (err && chip->model <= PMAC_SCREAMER)
748 snd_printk(KERN_DEBUG "error %x\n", err);
750 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
751 out_le32(&chip->awacs->control, ctrl);
752 return IRQ_HANDLED;
757 * a wrapper to feature call for compatibility
759 static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
761 if (ppc_md.feature_call)
762 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
766 * release resources
769 static int snd_pmac_free(struct snd_pmac *chip)
771 /* stop sounds */
772 if (chip->initialized) {
773 snd_pmac_dbdma_reset(chip);
774 /* disable interrupts from awacs interface */
775 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
778 if (chip->node)
779 snd_pmac_sound_feature(chip, 0);
781 /* clean up mixer if any */
782 if (chip->mixer_free)
783 chip->mixer_free(chip);
785 snd_pmac_detach_beep(chip);
787 /* release resources */
788 if (chip->irq >= 0)
789 free_irq(chip->irq, (void*)chip);
790 if (chip->tx_irq >= 0)
791 free_irq(chip->tx_irq, (void*)chip);
792 if (chip->rx_irq >= 0)
793 free_irq(chip->rx_irq, (void*)chip);
794 snd_pmac_dbdma_free(chip, &chip->playback.cmd);
795 snd_pmac_dbdma_free(chip, &chip->capture.cmd);
796 snd_pmac_dbdma_free(chip, &chip->extra_dma);
797 if (chip->macio_base)
798 iounmap(chip->macio_base);
799 if (chip->latch_base)
800 iounmap(chip->latch_base);
801 if (chip->awacs)
802 iounmap(chip->awacs);
803 if (chip->playback.dma)
804 iounmap(chip->playback.dma);
805 if (chip->capture.dma)
806 iounmap(chip->capture.dma);
808 if (chip->node) {
809 int i;
810 for (i = 0; i < 3; i++) {
811 if (chip->requested & (1 << i))
812 release_mem_region(chip->rsrc[i].start,
813 chip->rsrc[i].end -
814 chip->rsrc[i].start + 1);
818 if (chip->pdev)
819 pci_dev_put(chip->pdev);
820 of_node_put(chip->node);
821 kfree(chip);
822 return 0;
827 * free the device
829 static int snd_pmac_dev_free(struct snd_device *device)
831 struct snd_pmac *chip = device->device_data;
832 return snd_pmac_free(chip);
837 * check the machine support byteswap (little-endian)
840 static void __init detect_byte_swap(struct snd_pmac *chip)
842 struct device_node *mio;
844 /* if seems that Keylargo can't byte-swap */
845 for (mio = chip->node->parent; mio; mio = mio->parent) {
846 if (strcmp(mio->name, "mac-io") == 0) {
847 if (of_device_is_compatible(mio, "Keylargo"))
848 chip->can_byte_swap = 0;
849 break;
853 /* it seems the Pismo & iBook can't byte-swap in hardware. */
854 if (machine_is_compatible("PowerBook3,1") ||
855 machine_is_compatible("PowerBook2,1"))
856 chip->can_byte_swap = 0 ;
858 if (machine_is_compatible("PowerBook2,1"))
859 chip->can_duplex = 0;
864 * detect a sound chip
866 static int __init snd_pmac_detect(struct snd_pmac *chip)
868 struct device_node *sound;
869 struct device_node *dn;
870 const unsigned int *prop;
871 unsigned int l;
872 struct macio_chip* macio;
874 if (!machine_is(powermac))
875 return -ENODEV;
877 chip->subframe = 0;
878 chip->revision = 0;
879 chip->freqs_ok = 0xff; /* all ok */
880 chip->model = PMAC_AWACS;
881 chip->can_byte_swap = 1;
882 chip->can_duplex = 1;
883 chip->can_capture = 1;
884 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
885 chip->freq_table = awacs_freqs;
886 chip->pdev = NULL;
888 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
890 /* check machine type */
891 if (machine_is_compatible("AAPL,3400/2400")
892 || machine_is_compatible("AAPL,3500"))
893 chip->is_pbook_3400 = 1;
894 else if (machine_is_compatible("PowerBook1,1")
895 || machine_is_compatible("AAPL,PowerBook1998"))
896 chip->is_pbook_G3 = 1;
897 chip->node = of_find_node_by_name(NULL, "awacs");
898 sound = of_node_get(chip->node);
901 * powermac G3 models have a node called "davbus"
902 * with a child called "sound".
904 if (!chip->node)
905 chip->node = of_find_node_by_name(NULL, "davbus");
907 * if we didn't find a davbus device, try 'i2s-a' since
908 * this seems to be what iBooks have
910 if (! chip->node) {
911 chip->node = of_find_node_by_name(NULL, "i2s-a");
912 if (chip->node && chip->node->parent &&
913 chip->node->parent->parent) {
914 if (of_device_is_compatible(chip->node->parent->parent,
915 "K2-Keylargo"))
916 chip->is_k2 = 1;
919 if (! chip->node)
920 return -ENODEV;
922 if (!sound) {
923 sound = of_find_node_by_name(NULL, "sound");
924 while (sound && sound->parent != chip->node)
925 sound = of_find_node_by_name(sound, "sound");
927 if (! sound) {
928 of_node_put(chip->node);
929 chip->node = NULL;
930 return -ENODEV;
932 prop = of_get_property(sound, "sub-frame", NULL);
933 if (prop && *prop < 16)
934 chip->subframe = *prop;
935 prop = of_get_property(sound, "layout-id", NULL);
936 if (prop) {
937 /* partly deprecate snd-powermac, for those machines
938 * that have a layout-id property for now */
939 printk(KERN_INFO "snd-powermac no longer handles any "
940 "machines with a layout-id property "
941 "in the device-tree, use snd-aoa.\n");
942 of_node_put(sound);
943 of_node_put(chip->node);
944 chip->node = NULL;
945 return -ENODEV;
947 /* This should be verified on older screamers */
948 if (of_device_is_compatible(sound, "screamer")) {
949 chip->model = PMAC_SCREAMER;
950 // chip->can_byte_swap = 0; /* FIXME: check this */
952 if (of_device_is_compatible(sound, "burgundy")) {
953 chip->model = PMAC_BURGUNDY;
954 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
956 if (of_device_is_compatible(sound, "daca")) {
957 chip->model = PMAC_DACA;
958 chip->can_capture = 0; /* no capture */
959 chip->can_duplex = 0;
960 // chip->can_byte_swap = 0; /* FIXME: check this */
961 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
963 if (of_device_is_compatible(sound, "tumbler")) {
964 chip->model = PMAC_TUMBLER;
965 chip->can_capture = 0; /* no capture */
966 chip->can_duplex = 0;
967 // chip->can_byte_swap = 0; /* FIXME: check this */
968 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
969 chip->freq_table = tumbler_freqs;
970 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
972 if (of_device_is_compatible(sound, "snapper")) {
973 chip->model = PMAC_SNAPPER;
974 // chip->can_byte_swap = 0; /* FIXME: check this */
975 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
976 chip->freq_table = tumbler_freqs;
977 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
979 prop = of_get_property(sound, "device-id", NULL);
980 if (prop)
981 chip->device_id = *prop;
982 dn = of_find_node_by_name(NULL, "perch");
983 chip->has_iic = (dn != NULL);
984 of_node_put(dn);
986 /* We need the PCI device for DMA allocations, let's use a crude method
987 * for now ...
989 macio = macio_find(chip->node, macio_unknown);
990 if (macio == NULL)
991 printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
992 else {
993 struct pci_dev *pdev = NULL;
995 for_each_pci_dev(pdev) {
996 struct device_node *np = pci_device_to_OF_node(pdev);
997 if (np && np == macio->of_node) {
998 chip->pdev = pdev;
999 break;
1003 if (chip->pdev == NULL)
1004 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1005 " device !\n");
1007 detect_byte_swap(chip);
1009 /* look for a property saying what sample rates
1010 are available */
1011 prop = of_get_property(sound, "sample-rates", &l);
1012 if (! prop)
1013 prop = of_get_property(sound, "output-frame-rates", &l);
1014 if (prop) {
1015 int i;
1016 chip->freqs_ok = 0;
1017 for (l /= sizeof(int); l > 0; --l) {
1018 unsigned int r = *prop++;
1019 /* Apple 'Fixed' format */
1020 if (r >= 0x10000)
1021 r >>= 16;
1022 for (i = 0; i < chip->num_freqs; ++i) {
1023 if (r == chip->freq_table[i]) {
1024 chip->freqs_ok |= (1 << i);
1025 break;
1029 } else {
1030 /* assume only 44.1khz */
1031 chip->freqs_ok = 1;
1034 of_node_put(sound);
1035 return 0;
1039 * exported - boolean info callbacks for ease of programming
1041 int snd_pmac_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1042 struct snd_ctl_elem_info *uinfo)
1044 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1045 uinfo->count = 2;
1046 uinfo->value.integer.min = 0;
1047 uinfo->value.integer.max = 1;
1048 return 0;
1051 int snd_pmac_boolean_mono_info(struct snd_kcontrol *kcontrol,
1052 struct snd_ctl_elem_info *uinfo)
1054 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1055 uinfo->count = 1;
1056 uinfo->value.integer.min = 0;
1057 uinfo->value.integer.max = 1;
1058 return 0;
1061 #ifdef PMAC_SUPPORT_AUTOMUTE
1063 * auto-mute
1065 static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1066 struct snd_ctl_elem_value *ucontrol)
1068 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1069 ucontrol->value.integer.value[0] = chip->auto_mute;
1070 return 0;
1073 static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1074 struct snd_ctl_elem_value *ucontrol)
1076 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1077 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1078 chip->auto_mute = ucontrol->value.integer.value[0];
1079 if (chip->update_automute)
1080 chip->update_automute(chip, 1);
1081 return 1;
1083 return 0;
1086 static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1087 struct snd_ctl_elem_value *ucontrol)
1089 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1090 if (chip->detect_headphone)
1091 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1092 else
1093 ucontrol->value.integer.value[0] = 0;
1094 return 0;
1097 static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
1098 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1099 .name = "Auto Mute Switch",
1100 .info = snd_pmac_boolean_mono_info,
1101 .get = pmac_auto_mute_get,
1102 .put = pmac_auto_mute_put,
1104 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1105 .name = "Headphone Detection",
1106 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1107 .info = snd_pmac_boolean_mono_info,
1108 .get = pmac_hp_detect_get,
1112 int __init snd_pmac_add_automute(struct snd_pmac *chip)
1114 int err;
1115 chip->auto_mute = 1;
1116 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1117 if (err < 0) {
1118 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
1119 return err;
1121 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1122 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1124 #endif /* PMAC_SUPPORT_AUTOMUTE */
1127 * create and detect a pmac chip record
1129 int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1131 struct snd_pmac *chip;
1132 struct device_node *np;
1133 int i, err;
1134 unsigned int irq;
1135 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1136 static struct snd_device_ops ops = {
1137 .dev_free = snd_pmac_dev_free,
1140 *chip_return = NULL;
1142 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1143 if (chip == NULL)
1144 return -ENOMEM;
1145 chip->card = card;
1147 spin_lock_init(&chip->reg_lock);
1148 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1150 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1151 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1153 if ((err = snd_pmac_detect(chip)) < 0)
1154 goto __error;
1156 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1157 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1158 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
1159 err = -ENOMEM;
1160 goto __error;
1163 np = chip->node;
1164 chip->requested = 0;
1165 if (chip->is_k2) {
1166 static char *rnames[] = {
1167 "Sound Control", "Sound DMA" };
1168 for (i = 0; i < 2; i ++) {
1169 if (of_address_to_resource(np->parent, i,
1170 &chip->rsrc[i])) {
1171 printk(KERN_ERR "snd: can't translate rsrc "
1172 " %d (%s)\n", i, rnames[i]);
1173 err = -ENODEV;
1174 goto __error;
1176 if (request_mem_region(chip->rsrc[i].start,
1177 chip->rsrc[i].end -
1178 chip->rsrc[i].start + 1,
1179 rnames[i]) == NULL) {
1180 printk(KERN_ERR "snd: can't request rsrc "
1181 " %d (%s: 0x%016llx:%016llx)\n",
1182 i, rnames[i],
1183 (unsigned long long)chip->rsrc[i].start,
1184 (unsigned long long)chip->rsrc[i].end);
1185 err = -ENODEV;
1186 goto __error;
1188 chip->requested |= (1 << i);
1190 ctrl_addr = chip->rsrc[0].start;
1191 txdma_addr = chip->rsrc[1].start;
1192 rxdma_addr = txdma_addr + 0x100;
1193 } else {
1194 static char *rnames[] = {
1195 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1196 for (i = 0; i < 3; i ++) {
1197 if (of_address_to_resource(np, i,
1198 &chip->rsrc[i])) {
1199 printk(KERN_ERR "snd: can't translate rsrc "
1200 " %d (%s)\n", i, rnames[i]);
1201 err = -ENODEV;
1202 goto __error;
1204 if (request_mem_region(chip->rsrc[i].start,
1205 chip->rsrc[i].end -
1206 chip->rsrc[i].start + 1,
1207 rnames[i]) == NULL) {
1208 printk(KERN_ERR "snd: can't request rsrc "
1209 " %d (%s: 0x%016llx:%016llx)\n",
1210 i, rnames[i],
1211 (unsigned long long)chip->rsrc[i].start,
1212 (unsigned long long)chip->rsrc[i].end);
1213 err = -ENODEV;
1214 goto __error;
1216 chip->requested |= (1 << i);
1218 ctrl_addr = chip->rsrc[0].start;
1219 txdma_addr = chip->rsrc[1].start;
1220 rxdma_addr = chip->rsrc[2].start;
1223 chip->awacs = ioremap(ctrl_addr, 0x1000);
1224 chip->playback.dma = ioremap(txdma_addr, 0x100);
1225 chip->capture.dma = ioremap(rxdma_addr, 0x100);
1226 if (chip->model <= PMAC_BURGUNDY) {
1227 irq = irq_of_parse_and_map(np, 0);
1228 if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1229 "PMac", (void*)chip)) {
1230 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1231 irq);
1232 err = -EBUSY;
1233 goto __error;
1235 chip->irq = irq;
1237 irq = irq_of_parse_and_map(np, 1);
1238 if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1239 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1240 err = -EBUSY;
1241 goto __error;
1243 chip->tx_irq = irq;
1244 irq = irq_of_parse_and_map(np, 2);
1245 if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1246 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1247 err = -EBUSY;
1248 goto __error;
1250 chip->rx_irq = irq;
1252 snd_pmac_sound_feature(chip, 1);
1254 /* reset */
1255 if (chip->model == PMAC_AWACS)
1256 out_le32(&chip->awacs->control, 0x11);
1258 /* Powerbooks have odd ways of enabling inputs such as
1259 an expansion-bay CD or sound from an internal modem
1260 or a PC-card modem. */
1261 if (chip->is_pbook_3400) {
1262 /* Enable CD and PC-card sound inputs. */
1263 /* This is done by reading from address
1264 * f301a000, + 0x10 to enable the expansion-bay
1265 * CD sound input, + 0x80 to enable the PC-card
1266 * sound input. The 0x100 enables the SCSI bus
1267 * terminator power.
1269 chip->latch_base = ioremap (0xf301a000, 0x1000);
1270 in_8(chip->latch_base + 0x190);
1271 } else if (chip->is_pbook_G3) {
1272 struct device_node* mio;
1273 for (mio = chip->node->parent; mio; mio = mio->parent) {
1274 if (strcmp(mio->name, "mac-io") == 0) {
1275 struct resource r;
1276 if (of_address_to_resource(mio, 0, &r) == 0)
1277 chip->macio_base =
1278 ioremap(r.start, 0x40);
1279 break;
1282 /* Enable CD sound input. */
1283 /* The relevant bits for writing to this byte are 0x8f.
1284 * I haven't found out what the 0x80 bit does.
1285 * For the 0xf bits, writing 3 or 7 enables the CD
1286 * input, any other value disables it. Values
1287 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1288 * 4, 6, 8 - f enable the input from the modem.
1290 if (chip->macio_base)
1291 out_8(chip->macio_base + 0x37, 3);
1294 /* Reset dbdma channels */
1295 snd_pmac_dbdma_reset(chip);
1297 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1298 goto __error;
1300 *chip_return = chip;
1301 return 0;
1303 __error:
1304 snd_pmac_free(chip);
1305 return err;
1310 * sleep notify for powerbook
1313 #ifdef CONFIG_PM
1316 * Save state when going to sleep, restore it afterwards.
1319 void snd_pmac_suspend(struct snd_pmac *chip)
1321 unsigned long flags;
1323 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1324 if (chip->suspend)
1325 chip->suspend(chip);
1326 snd_pcm_suspend_all(chip->pcm);
1327 spin_lock_irqsave(&chip->reg_lock, flags);
1328 snd_pmac_beep_stop(chip);
1329 spin_unlock_irqrestore(&chip->reg_lock, flags);
1330 if (chip->irq >= 0)
1331 disable_irq(chip->irq);
1332 if (chip->tx_irq >= 0)
1333 disable_irq(chip->tx_irq);
1334 if (chip->rx_irq >= 0)
1335 disable_irq(chip->rx_irq);
1336 snd_pmac_sound_feature(chip, 0);
1339 void snd_pmac_resume(struct snd_pmac *chip)
1341 snd_pmac_sound_feature(chip, 1);
1342 if (chip->resume)
1343 chip->resume(chip);
1344 /* enable CD sound input */
1345 if (chip->macio_base && chip->is_pbook_G3)
1346 out_8(chip->macio_base + 0x37, 3);
1347 else if (chip->is_pbook_3400)
1348 in_8(chip->latch_base + 0x190);
1350 snd_pmac_pcm_set_format(chip);
1352 if (chip->irq >= 0)
1353 enable_irq(chip->irq);
1354 if (chip->tx_irq >= 0)
1355 enable_irq(chip->tx_irq);
1356 if (chip->rx_irq >= 0)
1357 enable_irq(chip->rx_irq);
1359 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1362 #endif /* CONFIG_PM */