GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / ppc / pmac.c
blob7b5798e36b3e544eca5d9d51aa1b6e9aad53f42a
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 <asm/io.h>
24 #include <asm/irq.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/pci.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include "pmac.h"
33 #include <sound/pcm_params.h>
34 #include <asm/pmac_feature.h>
35 #include <asm/pci-bridge.h>
38 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
39 static int awacs_freqs[8] = {
40 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
42 /* fixed frequency table for tumbler */
43 static int tumbler_freqs[1] = {
44 44100
49 * we will allocate a single 'emergency' dbdma cmd block to use if the
50 * tx status comes up "DEAD". This happens on some PowerComputing Pmac
51 * clones, either owing to a bug in dbdma or some interaction between
52 * IDE and sound. However, this measure would deal with DEAD status if
53 * it appeared elsewhere.
55 static struct pmac_dbdma emergency_dbdma;
56 static int emergency_in_use;
60 * allocate DBDMA command arrays
62 static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
64 unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
66 rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
67 &rec->dma_base, GFP_KERNEL);
68 if (rec->space == NULL)
69 return -ENOMEM;
70 rec->size = size;
71 memset(rec->space, 0, rsize);
72 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
73 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
75 return 0;
78 static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
80 if (rec->space) {
81 unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
83 dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
89 * pcm stuff
93 * look up frequency table
96 unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
98 int i, ok, found;
100 ok = rec->cur_freqs;
101 if (rate > chip->freq_table[0])
102 return 0;
103 found = 0;
104 for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
105 if (! (ok & 1)) continue;
106 found = i;
107 if (rate >= chip->freq_table[i])
108 break;
110 return found;
114 * check whether another stream is active
116 static inline int another_stream(int stream)
118 return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
119 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
123 * allocate buffers
125 static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
126 struct snd_pcm_hw_params *hw_params)
128 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
132 * release buffers
134 static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
136 snd_pcm_lib_free_pages(subs);
137 return 0;
141 * get a stream of the opposite direction
143 static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
145 switch (stream) {
146 case SNDRV_PCM_STREAM_PLAYBACK:
147 return &chip->playback;
148 case SNDRV_PCM_STREAM_CAPTURE:
149 return &chip->capture;
150 default:
151 snd_BUG();
152 return NULL;
157 * wait while run status is on
159 static inline void
160 snd_pmac_wait_ack(struct pmac_stream *rec)
162 int timeout = 50000;
163 while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
164 udelay(1);
168 * set the format and rate to the chip.
169 * call the lowlevel function if defined (e.g. for AWACS).
171 static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
173 /* set up frequency and format */
174 out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
175 out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
176 if (chip->set_format)
177 chip->set_format(chip);
181 * stop the DMA transfer
183 static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
185 out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
186 snd_pmac_wait_ack(rec);
190 * set the command pointer address
192 static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
194 out_le32(&rec->dma->cmdptr, cmd->addr);
198 * start the DMA
200 static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
202 out_le32(&rec->dma->control, status | (status << 16));
207 * prepare playback/capture stream
209 static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
211 int i;
212 volatile struct dbdma_cmd __iomem *cp;
213 struct snd_pcm_runtime *runtime = subs->runtime;
214 int rate_index;
215 long offset;
216 struct pmac_stream *astr;
218 rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
219 rec->period_size = snd_pcm_lib_period_bytes(subs);
220 rec->nperiods = rec->dma_size / rec->period_size;
221 rec->cur_period = 0;
222 rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
224 /* set up constraints */
225 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
226 if (! astr)
227 return -EINVAL;
228 astr->cur_freqs = 1 << rate_index;
229 astr->cur_formats = 1 << runtime->format;
230 chip->rate_index = rate_index;
231 chip->format = runtime->format;
233 /* We really want to execute a DMA stop command, after the AWACS
234 * is initialized.
235 * For reasons I don't understand, it stops the hissing noise
236 * common to many PowerBook G3 systems and random noise otherwise
237 * captured on iBook2's about every third time. -ReneR
239 spin_lock_irq(&chip->reg_lock);
240 snd_pmac_dma_stop(rec);
241 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
242 snd_pmac_dma_set_command(rec, &chip->extra_dma);
243 snd_pmac_dma_run(rec, RUN);
244 spin_unlock_irq(&chip->reg_lock);
245 mdelay(5);
246 spin_lock_irq(&chip->reg_lock);
247 /* continuous DMA memory type doesn't provide the physical address,
248 * so we need to resolve the address here...
250 offset = runtime->dma_addr;
251 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
252 st_le32(&cp->phy_addr, offset);
253 st_le16(&cp->req_count, rec->period_size);
254 /*st_le16(&cp->res_count, 0);*/
255 st_le16(&cp->xfer_status, 0);
256 offset += rec->period_size;
258 /* make loop */
259 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
260 st_le32(&cp->cmd_dep, rec->cmd.addr);
262 snd_pmac_dma_stop(rec);
263 snd_pmac_dma_set_command(rec, &rec->cmd);
264 spin_unlock_irq(&chip->reg_lock);
266 return 0;
271 * PCM trigger/stop
273 static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
274 struct snd_pcm_substream *subs, int cmd)
276 volatile struct dbdma_cmd __iomem *cp;
277 int i, command;
279 switch (cmd) {
280 case SNDRV_PCM_TRIGGER_START:
281 case SNDRV_PCM_TRIGGER_RESUME:
282 if (rec->running)
283 return -EBUSY;
284 command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
285 OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
286 spin_lock(&chip->reg_lock);
287 snd_pmac_beep_stop(chip);
288 snd_pmac_pcm_set_format(chip);
289 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
290 out_le16(&cp->command, command);
291 snd_pmac_dma_set_command(rec, &rec->cmd);
292 (void)in_le32(&rec->dma->status);
293 snd_pmac_dma_run(rec, RUN|WAKE);
294 rec->running = 1;
295 spin_unlock(&chip->reg_lock);
296 break;
298 case SNDRV_PCM_TRIGGER_STOP:
299 case SNDRV_PCM_TRIGGER_SUSPEND:
300 spin_lock(&chip->reg_lock);
301 rec->running = 0;
302 /*printk(KERN_DEBUG "stopped!!\n");*/
303 snd_pmac_dma_stop(rec);
304 for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
305 out_le16(&cp->command, DBDMA_STOP);
306 spin_unlock(&chip->reg_lock);
307 break;
309 default:
310 return -EINVAL;
313 return 0;
317 * return the current pointer
319 inline
320 static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
321 struct pmac_stream *rec,
322 struct snd_pcm_substream *subs)
324 int count = 0;
326 int stat;
327 volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
328 stat = ld_le16(&cp->xfer_status);
329 if (stat & (ACTIVE|DEAD)) {
330 count = in_le16(&cp->res_count);
331 if (count)
332 count = rec->period_size - count;
334 count += rec->cur_period * rec->period_size;
335 /*printk(KERN_DEBUG "pointer=%d\n", count);*/
336 return bytes_to_frames(subs->runtime, count);
340 * playback
343 static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
345 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
346 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
349 static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
350 int cmd)
352 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
353 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
356 static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
358 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
359 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
364 * capture
367 static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
369 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
370 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
373 static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
374 int cmd)
376 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
377 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
380 static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
382 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
383 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
388 * Handle DEAD DMA transfers:
389 * if the TX status comes up "DEAD" - reported on some Power Computing machines
390 * we need to re-start the dbdma - but from a different physical start address
391 * and with a different transfer length. It would get very messy to do this
392 * with the normal dbdma_cmd blocks - we would have to re-write the buffer start
393 * addresses each time. So, we will keep a single dbdma_cmd block which can be
394 * fiddled with.
395 * When DEAD status is first reported the content of the faulted dbdma block is
396 * copied into the emergency buffer and we note that the buffer is in use.
397 * we then bump the start physical address by the amount that was successfully
398 * output before it died.
399 * On any subsequent DEAD result we just do the bump-ups (we know that we are
400 * already using the emergency dbdma_cmd).
401 * CHECK: this just tries to "do it". It is possible that we should abandon
402 * xfers when the number of residual bytes gets below a certain value - I can
403 * see that this might cause a loop-forever if a too small transfer causes
404 * DEAD status. However this is a TODO for now - we'll see what gets reported.
405 * When we get a successful transfer result with the emergency buffer we just
406 * pretend that it completed using the original dmdma_cmd and carry on. The
407 * 'next_cmd' field will already point back to the original loop of blocks.
409 static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec,
410 volatile struct dbdma_cmd __iomem *cp)
412 unsigned short req, res ;
413 unsigned int phy ;
415 /* printk(KERN_WARNING "snd-powermac: DMA died - patching it up!\n"); */
417 /* to clear DEAD status we must first clear RUN
418 set it to quiescent to be on the safe side */
419 (void)in_le32(&rec->dma->status);
420 out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
422 if (!emergency_in_use) { /* new problem */
423 memcpy((void *)emergency_dbdma.cmds, (void *)cp,
424 sizeof(struct dbdma_cmd));
425 emergency_in_use = 1;
426 st_le16(&cp->xfer_status, 0);
427 st_le16(&cp->req_count, rec->period_size);
428 cp = emergency_dbdma.cmds;
431 /* now bump the values to reflect the amount
432 we haven't yet shifted */
433 req = ld_le16(&cp->req_count);
434 res = ld_le16(&cp->res_count);
435 phy = ld_le32(&cp->phy_addr);
436 phy += (req - res);
437 st_le16(&cp->req_count, res);
438 st_le16(&cp->res_count, 0);
439 st_le16(&cp->xfer_status, 0);
440 st_le32(&cp->phy_addr, phy);
442 st_le32(&cp->cmd_dep, rec->cmd.addr
443 + sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods));
445 st_le16(&cp->command, OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
447 /* point at our patched up command block */
448 out_le32(&rec->dma->cmdptr, emergency_dbdma.addr);
450 /* we must re-start the controller */
451 (void)in_le32(&rec->dma->status);
452 /* should complete clearing the DEAD status */
453 out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
457 * update playback/capture pointer from interrupts
459 static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
461 volatile struct dbdma_cmd __iomem *cp;
462 int c;
463 int stat;
465 spin_lock(&chip->reg_lock);
466 if (rec->running) {
467 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
469 if (emergency_in_use) /* already using DEAD xfer? */
470 cp = emergency_dbdma.cmds;
471 else
472 cp = &rec->cmd.cmds[rec->cur_period];
474 stat = ld_le16(&cp->xfer_status);
476 if (stat & DEAD) {
477 snd_pmac_pcm_dead_xfer(rec, cp);
478 break; /* this block is still going */
481 if (emergency_in_use)
482 emergency_in_use = 0 ; /* done that */
484 if (! (stat & ACTIVE))
485 break;
487 /*printk(KERN_DEBUG "update frag %d\n", rec->cur_period);*/
488 st_le16(&cp->xfer_status, 0);
489 st_le16(&cp->req_count, rec->period_size);
490 /*st_le16(&cp->res_count, 0);*/
491 rec->cur_period++;
492 if (rec->cur_period >= rec->nperiods) {
493 rec->cur_period = 0;
496 spin_unlock(&chip->reg_lock);
497 snd_pcm_period_elapsed(rec->substream);
498 spin_lock(&chip->reg_lock);
501 spin_unlock(&chip->reg_lock);
506 * hw info
509 static struct snd_pcm_hardware snd_pmac_playback =
511 .info = (SNDRV_PCM_INFO_INTERLEAVED |
512 SNDRV_PCM_INFO_MMAP |
513 SNDRV_PCM_INFO_MMAP_VALID |
514 SNDRV_PCM_INFO_RESUME),
515 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
516 .rates = SNDRV_PCM_RATE_8000_44100,
517 .rate_min = 7350,
518 .rate_max = 44100,
519 .channels_min = 2,
520 .channels_max = 2,
521 .buffer_bytes_max = 131072,
522 .period_bytes_min = 256,
523 .period_bytes_max = 16384,
524 .periods_min = 3,
525 .periods_max = PMAC_MAX_FRAGS,
528 static struct snd_pcm_hardware snd_pmac_capture =
530 .info = (SNDRV_PCM_INFO_INTERLEAVED |
531 SNDRV_PCM_INFO_MMAP |
532 SNDRV_PCM_INFO_MMAP_VALID |
533 SNDRV_PCM_INFO_RESUME),
534 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
535 .rates = SNDRV_PCM_RATE_8000_44100,
536 .rate_min = 7350,
537 .rate_max = 44100,
538 .channels_min = 2,
539 .channels_max = 2,
540 .buffer_bytes_max = 131072,
541 .period_bytes_min = 256,
542 .period_bytes_max = 16384,
543 .periods_min = 3,
544 .periods_max = PMAC_MAX_FRAGS,
549 static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
550 struct snd_pcm_substream *subs)
552 struct snd_pcm_runtime *runtime = subs->runtime;
553 int i;
555 /* look up frequency table and fill bit mask */
556 runtime->hw.rates = 0;
557 for (i = 0; i < chip->num_freqs; i++)
558 if (chip->freqs_ok & (1 << i))
559 runtime->hw.rates |=
560 snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
562 /* check for minimum and maximum rates */
563 for (i = 0; i < chip->num_freqs; i++) {
564 if (chip->freqs_ok & (1 << i)) {
565 runtime->hw.rate_max = chip->freq_table[i];
566 break;
569 for (i = chip->num_freqs - 1; i >= 0; i--) {
570 if (chip->freqs_ok & (1 << i)) {
571 runtime->hw.rate_min = chip->freq_table[i];
572 break;
575 runtime->hw.formats = chip->formats_ok;
576 if (chip->can_capture) {
577 if (! chip->can_duplex)
578 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
579 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
581 runtime->private_data = rec;
582 rec->substream = subs;
585 runtime->hw.periods_max = rec->cmd.size - 1;
587 /* constraints to fix choppy sound */
588 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
589 return 0;
592 static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
593 struct snd_pcm_substream *subs)
595 struct pmac_stream *astr;
597 snd_pmac_dma_stop(rec);
599 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
600 if (! astr)
601 return -EINVAL;
603 /* reset constraints */
604 astr->cur_freqs = chip->freqs_ok;
605 astr->cur_formats = chip->formats_ok;
607 return 0;
610 static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
612 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
614 subs->runtime->hw = snd_pmac_playback;
615 return snd_pmac_pcm_open(chip, &chip->playback, subs);
618 static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
620 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
622 subs->runtime->hw = snd_pmac_capture;
623 return snd_pmac_pcm_open(chip, &chip->capture, subs);
626 static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
628 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
630 return snd_pmac_pcm_close(chip, &chip->playback, subs);
633 static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
635 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
637 return snd_pmac_pcm_close(chip, &chip->capture, subs);
643 static struct snd_pcm_ops snd_pmac_playback_ops = {
644 .open = snd_pmac_playback_open,
645 .close = snd_pmac_playback_close,
646 .ioctl = snd_pcm_lib_ioctl,
647 .hw_params = snd_pmac_pcm_hw_params,
648 .hw_free = snd_pmac_pcm_hw_free,
649 .prepare = snd_pmac_playback_prepare,
650 .trigger = snd_pmac_playback_trigger,
651 .pointer = snd_pmac_playback_pointer,
654 static struct snd_pcm_ops snd_pmac_capture_ops = {
655 .open = snd_pmac_capture_open,
656 .close = snd_pmac_capture_close,
657 .ioctl = snd_pcm_lib_ioctl,
658 .hw_params = snd_pmac_pcm_hw_params,
659 .hw_free = snd_pmac_pcm_hw_free,
660 .prepare = snd_pmac_capture_prepare,
661 .trigger = snd_pmac_capture_trigger,
662 .pointer = snd_pmac_capture_pointer,
665 int __devinit snd_pmac_pcm_new(struct snd_pmac *chip)
667 struct snd_pcm *pcm;
668 int err;
669 int num_captures = 1;
671 if (! chip->can_capture)
672 num_captures = 0;
673 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
674 if (err < 0)
675 return err;
677 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
678 if (chip->can_capture)
679 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
681 pcm->private_data = chip;
682 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
683 strcpy(pcm->name, chip->card->shortname);
684 chip->pcm = pcm;
686 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
687 if (chip->can_byte_swap)
688 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
690 chip->playback.cur_formats = chip->formats_ok;
691 chip->capture.cur_formats = chip->formats_ok;
692 chip->playback.cur_freqs = chip->freqs_ok;
693 chip->capture.cur_freqs = chip->freqs_ok;
695 /* preallocate 64k buffer */
696 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
697 &chip->pdev->dev,
698 64 * 1024, 64 * 1024);
700 return 0;
704 static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
706 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
707 snd_pmac_wait_ack(&chip->playback);
708 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
709 snd_pmac_wait_ack(&chip->capture);
714 * handling beep
716 void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
718 struct pmac_stream *rec = &chip->playback;
720 snd_pmac_dma_stop(rec);
721 st_le16(&chip->extra_dma.cmds->req_count, bytes);
722 st_le16(&chip->extra_dma.cmds->xfer_status, 0);
723 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
724 st_le32(&chip->extra_dma.cmds->phy_addr, addr);
725 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
726 out_le32(&chip->awacs->control,
727 (in_le32(&chip->awacs->control) & ~0x1f00)
728 | (speed << 8));
729 out_le32(&chip->awacs->byteswap, 0);
730 snd_pmac_dma_set_command(rec, &chip->extra_dma);
731 snd_pmac_dma_run(rec, RUN);
734 void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
736 snd_pmac_dma_stop(&chip->playback);
737 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
738 snd_pmac_pcm_set_format(chip); /* reset format */
743 * interrupt handlers
745 static irqreturn_t
746 snd_pmac_tx_intr(int irq, void *devid)
748 struct snd_pmac *chip = devid;
749 snd_pmac_pcm_update(chip, &chip->playback);
750 return IRQ_HANDLED;
754 static irqreturn_t
755 snd_pmac_rx_intr(int irq, void *devid)
757 struct snd_pmac *chip = devid;
758 snd_pmac_pcm_update(chip, &chip->capture);
759 return IRQ_HANDLED;
763 static irqreturn_t
764 snd_pmac_ctrl_intr(int irq, void *devid)
766 struct snd_pmac *chip = devid;
767 int ctrl = in_le32(&chip->awacs->control);
769 /*printk(KERN_DEBUG "pmac: control interrupt.. 0x%x\n", ctrl);*/
770 if (ctrl & MASK_PORTCHG) {
771 /* do something when headphone is plugged/unplugged? */
772 if (chip->update_automute)
773 chip->update_automute(chip, 1);
775 if (ctrl & MASK_CNTLERR) {
776 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
777 if (err && chip->model <= PMAC_SCREAMER)
778 snd_printk(KERN_DEBUG "error %x\n", err);
780 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
781 out_le32(&chip->awacs->control, ctrl);
782 return IRQ_HANDLED;
787 * a wrapper to feature call for compatibility
789 static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
791 if (ppc_md.feature_call)
792 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
796 * release resources
799 static int snd_pmac_free(struct snd_pmac *chip)
801 /* stop sounds */
802 if (chip->initialized) {
803 snd_pmac_dbdma_reset(chip);
804 /* disable interrupts from awacs interface */
805 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
808 if (chip->node)
809 snd_pmac_sound_feature(chip, 0);
811 /* clean up mixer if any */
812 if (chip->mixer_free)
813 chip->mixer_free(chip);
815 snd_pmac_detach_beep(chip);
817 /* release resources */
818 if (chip->irq >= 0)
819 free_irq(chip->irq, (void*)chip);
820 if (chip->tx_irq >= 0)
821 free_irq(chip->tx_irq, (void*)chip);
822 if (chip->rx_irq >= 0)
823 free_irq(chip->rx_irq, (void*)chip);
824 snd_pmac_dbdma_free(chip, &chip->playback.cmd);
825 snd_pmac_dbdma_free(chip, &chip->capture.cmd);
826 snd_pmac_dbdma_free(chip, &chip->extra_dma);
827 snd_pmac_dbdma_free(chip, &emergency_dbdma);
828 if (chip->macio_base)
829 iounmap(chip->macio_base);
830 if (chip->latch_base)
831 iounmap(chip->latch_base);
832 if (chip->awacs)
833 iounmap(chip->awacs);
834 if (chip->playback.dma)
835 iounmap(chip->playback.dma);
836 if (chip->capture.dma)
837 iounmap(chip->capture.dma);
839 if (chip->node) {
840 int i;
841 for (i = 0; i < 3; i++) {
842 if (chip->requested & (1 << i))
843 release_mem_region(chip->rsrc[i].start,
844 chip->rsrc[i].end -
845 chip->rsrc[i].start + 1);
849 if (chip->pdev)
850 pci_dev_put(chip->pdev);
851 of_node_put(chip->node);
852 kfree(chip);
853 return 0;
858 * free the device
860 static int snd_pmac_dev_free(struct snd_device *device)
862 struct snd_pmac *chip = device->device_data;
863 return snd_pmac_free(chip);
868 * check the machine support byteswap (little-endian)
871 static void __devinit detect_byte_swap(struct snd_pmac *chip)
873 struct device_node *mio;
875 /* if seems that Keylargo can't byte-swap */
876 for (mio = chip->node->parent; mio; mio = mio->parent) {
877 if (strcmp(mio->name, "mac-io") == 0) {
878 if (of_device_is_compatible(mio, "Keylargo"))
879 chip->can_byte_swap = 0;
880 break;
884 /* it seems the Pismo & iBook can't byte-swap in hardware. */
885 if (of_machine_is_compatible("PowerBook3,1") ||
886 of_machine_is_compatible("PowerBook2,1"))
887 chip->can_byte_swap = 0 ;
889 if (of_machine_is_compatible("PowerBook2,1"))
890 chip->can_duplex = 0;
895 * detect a sound chip
897 static int __devinit snd_pmac_detect(struct snd_pmac *chip)
899 struct device_node *sound;
900 struct device_node *dn;
901 const unsigned int *prop;
902 unsigned int l;
903 struct macio_chip* macio;
905 if (!machine_is(powermac))
906 return -ENODEV;
908 chip->subframe = 0;
909 chip->revision = 0;
910 chip->freqs_ok = 0xff; /* all ok */
911 chip->model = PMAC_AWACS;
912 chip->can_byte_swap = 1;
913 chip->can_duplex = 1;
914 chip->can_capture = 1;
915 chip->num_freqs = ARRAY_SIZE(awacs_freqs);
916 chip->freq_table = awacs_freqs;
917 chip->pdev = NULL;
919 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
921 /* check machine type */
922 if (of_machine_is_compatible("AAPL,3400/2400")
923 || of_machine_is_compatible("AAPL,3500"))
924 chip->is_pbook_3400 = 1;
925 else if (of_machine_is_compatible("PowerBook1,1")
926 || of_machine_is_compatible("AAPL,PowerBook1998"))
927 chip->is_pbook_G3 = 1;
928 chip->node = of_find_node_by_name(NULL, "awacs");
929 sound = of_node_get(chip->node);
932 * powermac G3 models have a node called "davbus"
933 * with a child called "sound".
935 if (!chip->node)
936 chip->node = of_find_node_by_name(NULL, "davbus");
938 * if we didn't find a davbus device, try 'i2s-a' since
939 * this seems to be what iBooks have
941 if (! chip->node) {
942 chip->node = of_find_node_by_name(NULL, "i2s-a");
943 if (chip->node && chip->node->parent &&
944 chip->node->parent->parent) {
945 if (of_device_is_compatible(chip->node->parent->parent,
946 "K2-Keylargo"))
947 chip->is_k2 = 1;
950 if (! chip->node)
951 return -ENODEV;
953 if (!sound) {
954 sound = of_find_node_by_name(NULL, "sound");
955 while (sound && sound->parent != chip->node)
956 sound = of_find_node_by_name(sound, "sound");
958 if (! sound) {
959 of_node_put(chip->node);
960 chip->node = NULL;
961 return -ENODEV;
963 prop = of_get_property(sound, "sub-frame", NULL);
964 if (prop && *prop < 16)
965 chip->subframe = *prop;
966 prop = of_get_property(sound, "layout-id", NULL);
967 if (prop) {
968 /* partly deprecate snd-powermac, for those machines
969 * that have a layout-id property for now */
970 printk(KERN_INFO "snd-powermac no longer handles any "
971 "machines with a layout-id property "
972 "in the device-tree, use snd-aoa.\n");
973 of_node_put(sound);
974 of_node_put(chip->node);
975 chip->node = NULL;
976 return -ENODEV;
978 /* This should be verified on older screamers */
979 if (of_device_is_compatible(sound, "screamer")) {
980 chip->model = PMAC_SCREAMER;
981 // chip->can_byte_swap = 0;
983 if (of_device_is_compatible(sound, "burgundy")) {
984 chip->model = PMAC_BURGUNDY;
985 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
987 if (of_device_is_compatible(sound, "daca")) {
988 chip->model = PMAC_DACA;
989 chip->can_capture = 0; /* no capture */
990 chip->can_duplex = 0;
991 // chip->can_byte_swap = 0;
992 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
994 if (of_device_is_compatible(sound, "tumbler")) {
995 chip->model = PMAC_TUMBLER;
996 chip->can_capture = of_machine_is_compatible("PowerMac4,2")
997 || of_machine_is_compatible("PowerBook4,1");
998 chip->can_duplex = 0;
999 // chip->can_byte_swap = 0;
1000 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1001 chip->freq_table = tumbler_freqs;
1002 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1004 if (of_device_is_compatible(sound, "snapper")) {
1005 chip->model = PMAC_SNAPPER;
1006 // chip->can_byte_swap = 0;
1007 chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1008 chip->freq_table = tumbler_freqs;
1009 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1011 prop = of_get_property(sound, "device-id", NULL);
1012 if (prop)
1013 chip->device_id = *prop;
1014 dn = of_find_node_by_name(NULL, "perch");
1015 chip->has_iic = (dn != NULL);
1016 of_node_put(dn);
1018 /* We need the PCI device for DMA allocations, let's use a crude method
1019 * for now ...
1021 macio = macio_find(chip->node, macio_unknown);
1022 if (macio == NULL)
1023 printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
1024 else {
1025 struct pci_dev *pdev = NULL;
1027 for_each_pci_dev(pdev) {
1028 struct device_node *np = pci_device_to_OF_node(pdev);
1029 if (np && np == macio->of_node) {
1030 chip->pdev = pdev;
1031 break;
1035 if (chip->pdev == NULL)
1036 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1037 " device !\n");
1039 detect_byte_swap(chip);
1041 /* look for a property saying what sample rates
1042 are available */
1043 prop = of_get_property(sound, "sample-rates", &l);
1044 if (! prop)
1045 prop = of_get_property(sound, "output-frame-rates", &l);
1046 if (prop) {
1047 int i;
1048 chip->freqs_ok = 0;
1049 for (l /= sizeof(int); l > 0; --l) {
1050 unsigned int r = *prop++;
1051 /* Apple 'Fixed' format */
1052 if (r >= 0x10000)
1053 r >>= 16;
1054 for (i = 0; i < chip->num_freqs; ++i) {
1055 if (r == chip->freq_table[i]) {
1056 chip->freqs_ok |= (1 << i);
1057 break;
1061 } else {
1062 /* assume only 44.1khz */
1063 chip->freqs_ok = 1;
1066 of_node_put(sound);
1067 return 0;
1070 #ifdef PMAC_SUPPORT_AUTOMUTE
1072 * auto-mute
1074 static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1075 struct snd_ctl_elem_value *ucontrol)
1077 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1078 ucontrol->value.integer.value[0] = chip->auto_mute;
1079 return 0;
1082 static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1083 struct snd_ctl_elem_value *ucontrol)
1085 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1086 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1087 chip->auto_mute = !!ucontrol->value.integer.value[0];
1088 if (chip->update_automute)
1089 chip->update_automute(chip, 1);
1090 return 1;
1092 return 0;
1095 static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1098 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1099 if (chip->detect_headphone)
1100 ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1101 else
1102 ucontrol->value.integer.value[0] = 0;
1103 return 0;
1106 static struct snd_kcontrol_new auto_mute_controls[] __devinitdata = {
1107 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1108 .name = "Auto Mute Switch",
1109 .info = snd_pmac_boolean_mono_info,
1110 .get = pmac_auto_mute_get,
1111 .put = pmac_auto_mute_put,
1113 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1114 .name = "Headphone Detection",
1115 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1116 .info = snd_pmac_boolean_mono_info,
1117 .get = pmac_hp_detect_get,
1121 int __devinit snd_pmac_add_automute(struct snd_pmac *chip)
1123 int err;
1124 chip->auto_mute = 1;
1125 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1126 if (err < 0) {
1127 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
1128 return err;
1130 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1131 return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1133 #endif /* PMAC_SUPPORT_AUTOMUTE */
1136 * create and detect a pmac chip record
1138 int __devinit snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1140 struct snd_pmac *chip;
1141 struct device_node *np;
1142 int i, err;
1143 unsigned int irq;
1144 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1145 static struct snd_device_ops ops = {
1146 .dev_free = snd_pmac_dev_free,
1149 *chip_return = NULL;
1151 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1152 if (chip == NULL)
1153 return -ENOMEM;
1154 chip->card = card;
1156 spin_lock_init(&chip->reg_lock);
1157 chip->irq = chip->tx_irq = chip->rx_irq = -1;
1159 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1160 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1162 if ((err = snd_pmac_detect(chip)) < 0)
1163 goto __error;
1165 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1166 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1167 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 ||
1168 snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) {
1169 err = -ENOMEM;
1170 goto __error;
1173 np = chip->node;
1174 chip->requested = 0;
1175 if (chip->is_k2) {
1176 static char *rnames[] = {
1177 "Sound Control", "Sound DMA" };
1178 for (i = 0; i < 2; i ++) {
1179 if (of_address_to_resource(np->parent, i,
1180 &chip->rsrc[i])) {
1181 printk(KERN_ERR "snd: can't translate rsrc "
1182 " %d (%s)\n", i, rnames[i]);
1183 err = -ENODEV;
1184 goto __error;
1186 if (request_mem_region(chip->rsrc[i].start,
1187 chip->rsrc[i].end -
1188 chip->rsrc[i].start + 1,
1189 rnames[i]) == NULL) {
1190 printk(KERN_ERR "snd: can't request rsrc "
1191 " %d (%s: 0x%016llx:%016llx)\n",
1192 i, rnames[i],
1193 (unsigned long long)chip->rsrc[i].start,
1194 (unsigned long long)chip->rsrc[i].end);
1195 err = -ENODEV;
1196 goto __error;
1198 chip->requested |= (1 << i);
1200 ctrl_addr = chip->rsrc[0].start;
1201 txdma_addr = chip->rsrc[1].start;
1202 rxdma_addr = txdma_addr + 0x100;
1203 } else {
1204 static char *rnames[] = {
1205 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1206 for (i = 0; i < 3; i ++) {
1207 if (of_address_to_resource(np, i,
1208 &chip->rsrc[i])) {
1209 printk(KERN_ERR "snd: can't translate rsrc "
1210 " %d (%s)\n", i, rnames[i]);
1211 err = -ENODEV;
1212 goto __error;
1214 if (request_mem_region(chip->rsrc[i].start,
1215 chip->rsrc[i].end -
1216 chip->rsrc[i].start + 1,
1217 rnames[i]) == NULL) {
1218 printk(KERN_ERR "snd: can't request rsrc "
1219 " %d (%s: 0x%016llx:%016llx)\n",
1220 i, rnames[i],
1221 (unsigned long long)chip->rsrc[i].start,
1222 (unsigned long long)chip->rsrc[i].end);
1223 err = -ENODEV;
1224 goto __error;
1226 chip->requested |= (1 << i);
1228 ctrl_addr = chip->rsrc[0].start;
1229 txdma_addr = chip->rsrc[1].start;
1230 rxdma_addr = chip->rsrc[2].start;
1233 chip->awacs = ioremap(ctrl_addr, 0x1000);
1234 chip->playback.dma = ioremap(txdma_addr, 0x100);
1235 chip->capture.dma = ioremap(rxdma_addr, 0x100);
1236 if (chip->model <= PMAC_BURGUNDY) {
1237 irq = irq_of_parse_and_map(np, 0);
1238 if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1239 "PMac", (void*)chip)) {
1240 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1241 irq);
1242 err = -EBUSY;
1243 goto __error;
1245 chip->irq = irq;
1247 irq = irq_of_parse_and_map(np, 1);
1248 if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1249 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1250 err = -EBUSY;
1251 goto __error;
1253 chip->tx_irq = irq;
1254 irq = irq_of_parse_and_map(np, 2);
1255 if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1256 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1257 err = -EBUSY;
1258 goto __error;
1260 chip->rx_irq = irq;
1262 snd_pmac_sound_feature(chip, 1);
1264 /* reset & enable interrupts */
1265 if (chip->model <= PMAC_BURGUNDY)
1266 out_le32(&chip->awacs->control, chip->control_mask);
1268 /* Powerbooks have odd ways of enabling inputs such as
1269 an expansion-bay CD or sound from an internal modem
1270 or a PC-card modem. */
1271 if (chip->is_pbook_3400) {
1272 /* Enable CD and PC-card sound inputs. */
1273 /* This is done by reading from address
1274 * f301a000, + 0x10 to enable the expansion-bay
1275 * CD sound input, + 0x80 to enable the PC-card
1276 * sound input. The 0x100 enables the SCSI bus
1277 * terminator power.
1279 chip->latch_base = ioremap (0xf301a000, 0x1000);
1280 in_8(chip->latch_base + 0x190);
1281 } else if (chip->is_pbook_G3) {
1282 struct device_node* mio;
1283 for (mio = chip->node->parent; mio; mio = mio->parent) {
1284 if (strcmp(mio->name, "mac-io") == 0) {
1285 struct resource r;
1286 if (of_address_to_resource(mio, 0, &r) == 0)
1287 chip->macio_base =
1288 ioremap(r.start, 0x40);
1289 break;
1292 /* Enable CD sound input. */
1293 /* The relevant bits for writing to this byte are 0x8f.
1294 * I haven't found out what the 0x80 bit does.
1295 * For the 0xf bits, writing 3 or 7 enables the CD
1296 * input, any other value disables it. Values
1297 * 1, 3, 5, 7 enable the microphone. Values 0, 2,
1298 * 4, 6, 8 - f enable the input from the modem.
1300 if (chip->macio_base)
1301 out_8(chip->macio_base + 0x37, 3);
1304 /* Reset dbdma channels */
1305 snd_pmac_dbdma_reset(chip);
1307 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1308 goto __error;
1310 *chip_return = chip;
1311 return 0;
1313 __error:
1314 snd_pmac_free(chip);
1315 return err;
1320 * sleep notify for powerbook
1323 #ifdef CONFIG_PM
1326 * Save state when going to sleep, restore it afterwards.
1329 void snd_pmac_suspend(struct snd_pmac *chip)
1331 unsigned long flags;
1333 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1334 if (chip->suspend)
1335 chip->suspend(chip);
1336 snd_pcm_suspend_all(chip->pcm);
1337 spin_lock_irqsave(&chip->reg_lock, flags);
1338 snd_pmac_beep_stop(chip);
1339 spin_unlock_irqrestore(&chip->reg_lock, flags);
1340 if (chip->irq >= 0)
1341 disable_irq(chip->irq);
1342 if (chip->tx_irq >= 0)
1343 disable_irq(chip->tx_irq);
1344 if (chip->rx_irq >= 0)
1345 disable_irq(chip->rx_irq);
1346 snd_pmac_sound_feature(chip, 0);
1349 void snd_pmac_resume(struct snd_pmac *chip)
1351 snd_pmac_sound_feature(chip, 1);
1352 if (chip->resume)
1353 chip->resume(chip);
1354 /* enable CD sound input */
1355 if (chip->macio_base && chip->is_pbook_G3)
1356 out_8(chip->macio_base + 0x37, 3);
1357 else if (chip->is_pbook_3400)
1358 in_8(chip->latch_base + 0x190);
1360 snd_pmac_pcm_set_format(chip);
1362 if (chip->irq >= 0)
1363 enable_irq(chip->irq);
1364 if (chip->tx_irq >= 0)
1365 enable_irq(chip->tx_irq);
1366 if (chip->rx_irq >= 0)
1367 enable_irq(chip->rx_irq);
1369 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1372 #endif /* CONFIG_PM */