Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PMac DBDMA lowlevel functions
0004  *
0005  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
0006  * code based on dmasound.c.
0007  */
0008 
0009 
0010 #include <linux/io.h>
0011 #include <asm/irq.h>
0012 #include <linux/init.h>
0013 #include <linux/delay.h>
0014 #include <linux/slab.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/pci.h>
0017 #include <linux/dma-mapping.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_irq.h>
0020 #include <sound/core.h>
0021 #include "pmac.h"
0022 #include <sound/pcm_params.h>
0023 #include <asm/pmac_feature.h>
0024 
0025 
0026 /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
0027 static const int awacs_freqs[8] = {
0028     44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
0029 };
0030 /* fixed frequency table for tumbler */
0031 static const int tumbler_freqs[1] = {
0032     44100
0033 };
0034 
0035 
0036 /*
0037  * we will allocate a single 'emergency' dbdma cmd block to use if the
0038  * tx status comes up "DEAD".  This happens on some PowerComputing Pmac
0039  * clones, either owing to a bug in dbdma or some interaction between
0040  * IDE and sound.  However, this measure would deal with DEAD status if
0041  * it appeared elsewhere.
0042  */
0043 static struct pmac_dbdma emergency_dbdma;
0044 static int emergency_in_use;
0045 
0046 
0047 /*
0048  * allocate DBDMA command arrays
0049  */
0050 static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
0051 {
0052     unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
0053 
0054     rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
0055                     &rec->dma_base, GFP_KERNEL);
0056     if (rec->space == NULL)
0057         return -ENOMEM;
0058     rec->size = size;
0059     memset(rec->space, 0, rsize);
0060     rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
0061     rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
0062 
0063     return 0;
0064 }
0065 
0066 static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
0067 {
0068     if (rec->space) {
0069         unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
0070 
0071         dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
0072     }
0073 }
0074 
0075 
0076 /*
0077  * pcm stuff
0078  */
0079 
0080 /*
0081  * look up frequency table
0082  */
0083 
0084 unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
0085 {
0086     int i, ok, found;
0087 
0088     ok = rec->cur_freqs;
0089     if (rate > chip->freq_table[0])
0090         return 0;
0091     found = 0;
0092     for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
0093         if (! (ok & 1)) continue;
0094         found = i;
0095         if (rate >= chip->freq_table[i])
0096             break;
0097     }
0098     return found;
0099 }
0100 
0101 /*
0102  * check whether another stream is active
0103  */
0104 static inline int another_stream(int stream)
0105 {
0106     return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
0107         SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
0108 }
0109 
0110 /*
0111  * get a stream of the opposite direction
0112  */
0113 static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
0114 {
0115     switch (stream) {
0116     case SNDRV_PCM_STREAM_PLAYBACK:
0117         return &chip->playback;
0118     case SNDRV_PCM_STREAM_CAPTURE:
0119         return &chip->capture;
0120     default:
0121         snd_BUG();
0122         return NULL;
0123     }
0124 }
0125 
0126 /*
0127  * wait while run status is on
0128  */
0129 static inline void
0130 snd_pmac_wait_ack(struct pmac_stream *rec)
0131 {
0132     int timeout = 50000;
0133     while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
0134         udelay(1);
0135 }
0136 
0137 /*
0138  * set the format and rate to the chip.
0139  * call the lowlevel function if defined (e.g. for AWACS).
0140  */
0141 static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
0142 {
0143     /* set up frequency and format */
0144     out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
0145     out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
0146     if (chip->set_format)
0147         chip->set_format(chip);
0148 }
0149 
0150 /*
0151  * stop the DMA transfer
0152  */
0153 static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
0154 {
0155     out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
0156     snd_pmac_wait_ack(rec);
0157 }
0158 
0159 /*
0160  * set the command pointer address
0161  */
0162 static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
0163 {
0164     out_le32(&rec->dma->cmdptr, cmd->addr);
0165 }
0166 
0167 /*
0168  * start the DMA
0169  */
0170 static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
0171 {
0172     out_le32(&rec->dma->control, status | (status << 16));
0173 }
0174 
0175 
0176 /*
0177  * prepare playback/capture stream
0178  */
0179 static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
0180 {
0181     int i;
0182     volatile struct dbdma_cmd __iomem *cp;
0183     struct snd_pcm_runtime *runtime = subs->runtime;
0184     int rate_index;
0185     long offset;
0186     struct pmac_stream *astr;
0187 
0188     rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
0189     rec->period_size = snd_pcm_lib_period_bytes(subs);
0190     rec->nperiods = rec->dma_size / rec->period_size;
0191     rec->cur_period = 0;
0192     rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
0193 
0194     /* set up constraints */
0195     astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
0196     if (! astr)
0197         return -EINVAL;
0198     astr->cur_freqs = 1 << rate_index;
0199     astr->cur_formats = 1 << runtime->format;
0200     chip->rate_index = rate_index;
0201     chip->format = runtime->format;
0202 
0203     /* We really want to execute a DMA stop command, after the AWACS
0204      * is initialized.
0205      * For reasons I don't understand, it stops the hissing noise
0206      * common to many PowerBook G3 systems and random noise otherwise
0207      * captured on iBook2's about every third time. -ReneR
0208      */
0209     spin_lock_irq(&chip->reg_lock);
0210     snd_pmac_dma_stop(rec);
0211     chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
0212     snd_pmac_dma_set_command(rec, &chip->extra_dma);
0213     snd_pmac_dma_run(rec, RUN);
0214     spin_unlock_irq(&chip->reg_lock);
0215     mdelay(5);
0216     spin_lock_irq(&chip->reg_lock);
0217     /* continuous DMA memory type doesn't provide the physical address,
0218      * so we need to resolve the address here...
0219      */
0220     offset = runtime->dma_addr;
0221     for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
0222         cp->phy_addr = cpu_to_le32(offset);
0223         cp->req_count = cpu_to_le16(rec->period_size);
0224         /*cp->res_count = cpu_to_le16(0);*/
0225         cp->xfer_status = cpu_to_le16(0);
0226         offset += rec->period_size;
0227     }
0228     /* make loop */
0229     cp->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
0230     cp->cmd_dep = cpu_to_le32(rec->cmd.addr);
0231 
0232     snd_pmac_dma_stop(rec);
0233     snd_pmac_dma_set_command(rec, &rec->cmd);
0234     spin_unlock_irq(&chip->reg_lock);
0235 
0236     return 0;
0237 }
0238 
0239 
0240 /*
0241  * PCM trigger/stop
0242  */
0243 static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
0244                 struct snd_pcm_substream *subs, int cmd)
0245 {
0246     volatile struct dbdma_cmd __iomem *cp;
0247     int i, command;
0248 
0249     switch (cmd) {
0250     case SNDRV_PCM_TRIGGER_START:
0251     case SNDRV_PCM_TRIGGER_RESUME:
0252         if (rec->running)
0253             return -EBUSY;
0254         command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
0255                OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
0256         spin_lock(&chip->reg_lock);
0257         snd_pmac_beep_stop(chip);
0258         snd_pmac_pcm_set_format(chip);
0259         for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
0260             out_le16(&cp->command, command);
0261         snd_pmac_dma_set_command(rec, &rec->cmd);
0262         (void)in_le32(&rec->dma->status);
0263         snd_pmac_dma_run(rec, RUN|WAKE);
0264         rec->running = 1;
0265         spin_unlock(&chip->reg_lock);
0266         break;
0267 
0268     case SNDRV_PCM_TRIGGER_STOP:
0269     case SNDRV_PCM_TRIGGER_SUSPEND:
0270         spin_lock(&chip->reg_lock);
0271         rec->running = 0;
0272         /*printk(KERN_DEBUG "stopped!!\n");*/
0273         snd_pmac_dma_stop(rec);
0274         for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
0275             out_le16(&cp->command, DBDMA_STOP);
0276         spin_unlock(&chip->reg_lock);
0277         break;
0278 
0279     default:
0280         return -EINVAL;
0281     }
0282 
0283     return 0;
0284 }
0285 
0286 /*
0287  * return the current pointer
0288  */
0289 inline
0290 static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
0291                           struct pmac_stream *rec,
0292                           struct snd_pcm_substream *subs)
0293 {
0294     int count = 0;
0295 
0296 #if 1 /* hmm.. how can we get the current dma pointer?? */
0297     int stat;
0298     volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
0299     stat = le16_to_cpu(cp->xfer_status);
0300     if (stat & (ACTIVE|DEAD)) {
0301         count = in_le16(&cp->res_count);
0302         if (count)
0303             count = rec->period_size - count;
0304     }
0305 #endif
0306     count += rec->cur_period * rec->period_size;
0307     /*printk(KERN_DEBUG "pointer=%d\n", count);*/
0308     return bytes_to_frames(subs->runtime, count);
0309 }
0310 
0311 /*
0312  * playback
0313  */
0314 
0315 static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
0316 {
0317     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0318     return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
0319 }
0320 
0321 static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
0322                      int cmd)
0323 {
0324     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0325     return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
0326 }
0327 
0328 static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
0329 {
0330     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0331     return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
0332 }
0333 
0334 
0335 /*
0336  * capture
0337  */
0338 
0339 static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
0340 {
0341     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0342     return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
0343 }
0344 
0345 static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
0346                     int cmd)
0347 {
0348     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0349     return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
0350 }
0351 
0352 static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
0353 {
0354     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0355     return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
0356 }
0357 
0358 
0359 /*
0360  * Handle DEAD DMA transfers:
0361  * if the TX status comes up "DEAD" - reported on some Power Computing machines
0362  * we need to re-start the dbdma - but from a different physical start address
0363  * and with a different transfer length.  It would get very messy to do this
0364  * with the normal dbdma_cmd blocks - we would have to re-write the buffer start
0365  * addresses each time.  So, we will keep a single dbdma_cmd block which can be
0366  * fiddled with.
0367  * When DEAD status is first reported the content of the faulted dbdma block is
0368  * copied into the emergency buffer and we note that the buffer is in use.
0369  * we then bump the start physical address by the amount that was successfully
0370  * output before it died.
0371  * On any subsequent DEAD result we just do the bump-ups (we know that we are
0372  * already using the emergency dbdma_cmd).
0373  * CHECK: this just tries to "do it".  It is possible that we should abandon
0374  * xfers when the number of residual bytes gets below a certain value - I can
0375  * see that this might cause a loop-forever if a too small transfer causes
0376  * DEAD status.  However this is a TODO for now - we'll see what gets reported.
0377  * When we get a successful transfer result with the emergency buffer we just
0378  * pretend that it completed using the original dmdma_cmd and carry on.  The
0379  * 'next_cmd' field will already point back to the original loop of blocks.
0380  */
0381 static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec,
0382                       volatile struct dbdma_cmd __iomem *cp)
0383 {
0384     unsigned short req, res ;
0385     unsigned int phy ;
0386 
0387     /* printk(KERN_WARNING "snd-powermac: DMA died - patching it up!\n"); */
0388 
0389     /* to clear DEAD status we must first clear RUN
0390        set it to quiescent to be on the safe side */
0391     (void)in_le32(&rec->dma->status);
0392     out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
0393 
0394     if (!emergency_in_use) { /* new problem */
0395         memcpy((void *)emergency_dbdma.cmds, (void *)cp,
0396                sizeof(struct dbdma_cmd));
0397         emergency_in_use = 1;
0398         cp->xfer_status = cpu_to_le16(0);
0399         cp->req_count = cpu_to_le16(rec->period_size);
0400         cp = emergency_dbdma.cmds;
0401     }
0402 
0403     /* now bump the values to reflect the amount
0404        we haven't yet shifted */
0405     req = le16_to_cpu(cp->req_count);
0406     res = le16_to_cpu(cp->res_count);
0407     phy = le32_to_cpu(cp->phy_addr);
0408     phy += (req - res);
0409     cp->req_count = cpu_to_le16(res);
0410     cp->res_count = cpu_to_le16(0);
0411     cp->xfer_status = cpu_to_le16(0);
0412     cp->phy_addr = cpu_to_le32(phy);
0413 
0414     cp->cmd_dep = cpu_to_le32(rec->cmd.addr
0415         + sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods));
0416 
0417     cp->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
0418 
0419     /* point at our patched up command block */
0420     out_le32(&rec->dma->cmdptr, emergency_dbdma.addr);
0421 
0422     /* we must re-start the controller */
0423     (void)in_le32(&rec->dma->status);
0424     /* should complete clearing the DEAD status */
0425     out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
0426 }
0427 
0428 /*
0429  * update playback/capture pointer from interrupts
0430  */
0431 static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
0432 {
0433     volatile struct dbdma_cmd __iomem *cp;
0434     int c;
0435     int stat;
0436 
0437     spin_lock(&chip->reg_lock);
0438     if (rec->running) {
0439         for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
0440 
0441             if (emergency_in_use)   /* already using DEAD xfer? */
0442                 cp = emergency_dbdma.cmds;
0443             else
0444                 cp = &rec->cmd.cmds[rec->cur_period];
0445 
0446             stat = le16_to_cpu(cp->xfer_status);
0447 
0448             if (stat & DEAD) {
0449                 snd_pmac_pcm_dead_xfer(rec, cp);
0450                 break; /* this block is still going */
0451             }
0452 
0453             if (emergency_in_use)
0454                 emergency_in_use = 0 ; /* done that */
0455 
0456             if (! (stat & ACTIVE))
0457                 break;
0458 
0459             /*printk(KERN_DEBUG "update frag %d\n", rec->cur_period);*/
0460             cp->xfer_status = cpu_to_le16(0);
0461             cp->req_count = cpu_to_le16(rec->period_size);
0462             /*cp->res_count = cpu_to_le16(0);*/
0463             rec->cur_period++;
0464             if (rec->cur_period >= rec->nperiods) {
0465                 rec->cur_period = 0;
0466             }
0467 
0468             spin_unlock(&chip->reg_lock);
0469             snd_pcm_period_elapsed(rec->substream);
0470             spin_lock(&chip->reg_lock);
0471         }
0472     }
0473     spin_unlock(&chip->reg_lock);
0474 }
0475 
0476 
0477 /*
0478  * hw info
0479  */
0480 
0481 static const struct snd_pcm_hardware snd_pmac_playback =
0482 {
0483     .info =         (SNDRV_PCM_INFO_INTERLEAVED |
0484                  SNDRV_PCM_INFO_MMAP |
0485                  SNDRV_PCM_INFO_MMAP_VALID |
0486                  SNDRV_PCM_INFO_RESUME),
0487     .formats =      SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
0488     .rates =        SNDRV_PCM_RATE_8000_44100,
0489     .rate_min =     7350,
0490     .rate_max =     44100,
0491     .channels_min =     2,
0492     .channels_max =     2,
0493     .buffer_bytes_max = 131072,
0494     .period_bytes_min = 256,
0495     .period_bytes_max = 16384,
0496     .periods_min =      3,
0497     .periods_max =      PMAC_MAX_FRAGS,
0498 };
0499 
0500 static const struct snd_pcm_hardware snd_pmac_capture =
0501 {
0502     .info =         (SNDRV_PCM_INFO_INTERLEAVED |
0503                  SNDRV_PCM_INFO_MMAP |
0504                  SNDRV_PCM_INFO_MMAP_VALID |
0505                  SNDRV_PCM_INFO_RESUME),
0506     .formats =      SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
0507     .rates =        SNDRV_PCM_RATE_8000_44100,
0508     .rate_min =     7350,
0509     .rate_max =     44100,
0510     .channels_min =     2,
0511     .channels_max =     2,
0512     .buffer_bytes_max = 131072,
0513     .period_bytes_min = 256,
0514     .period_bytes_max = 16384,
0515     .periods_min =      3,
0516     .periods_max =      PMAC_MAX_FRAGS,
0517 };
0518 
0519 
0520 #if 0 // NYI
0521 static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
0522                  struct snd_pcm_hw_rule *rule)
0523 {
0524     struct snd_pmac *chip = rule->private;
0525     struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
0526     int i, freq_table[8], num_freqs;
0527 
0528     if (! rec)
0529         return -EINVAL;
0530     num_freqs = 0;
0531     for (i = chip->num_freqs - 1; i >= 0; i--) {
0532         if (rec->cur_freqs & (1 << i))
0533             freq_table[num_freqs++] = chip->freq_table[i];
0534     }
0535 
0536     return snd_interval_list(hw_param_interval(params, rule->var),
0537                  num_freqs, freq_table, 0);
0538 }
0539 
0540 static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
0541                    struct snd_pcm_hw_rule *rule)
0542 {
0543     struct snd_pmac *chip = rule->private;
0544     struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
0545 
0546     if (! rec)
0547         return -EINVAL;
0548     return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0549                    rec->cur_formats);
0550 }
0551 #endif // NYI
0552 
0553 static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
0554                  struct snd_pcm_substream *subs)
0555 {
0556     struct snd_pcm_runtime *runtime = subs->runtime;
0557     int i;
0558 
0559     /* look up frequency table and fill bit mask */
0560     runtime->hw.rates = 0;
0561     for (i = 0; i < chip->num_freqs; i++)
0562         if (chip->freqs_ok & (1 << i))
0563             runtime->hw.rates |=
0564                 snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
0565 
0566     /* check for minimum and maximum rates */
0567     for (i = 0; i < chip->num_freqs; i++) {
0568         if (chip->freqs_ok & (1 << i)) {
0569             runtime->hw.rate_max = chip->freq_table[i];
0570             break;
0571         }
0572     }
0573     for (i = chip->num_freqs - 1; i >= 0; i--) {
0574         if (chip->freqs_ok & (1 << i)) {
0575             runtime->hw.rate_min = chip->freq_table[i];
0576             break;
0577         }
0578     }
0579     runtime->hw.formats = chip->formats_ok;
0580     if (chip->can_capture) {
0581         if (! chip->can_duplex)
0582             runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
0583         runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
0584     }
0585     runtime->private_data = rec;
0586     rec->substream = subs;
0587 
0588 #if 0 /* FIXME: still under development.. */
0589     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0590                 snd_pmac_hw_rule_rate, chip, rec->stream, -1);
0591     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
0592                 snd_pmac_hw_rule_format, chip, rec->stream, -1);
0593 #endif
0594 
0595     runtime->hw.periods_max = rec->cmd.size - 1;
0596 
0597     /* constraints to fix choppy sound */
0598     snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0599     return 0;
0600 }
0601 
0602 static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
0603                   struct snd_pcm_substream *subs)
0604 {
0605     struct pmac_stream *astr;
0606 
0607     snd_pmac_dma_stop(rec);
0608 
0609     astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
0610     if (! astr)
0611         return -EINVAL;
0612 
0613     /* reset constraints */
0614     astr->cur_freqs = chip->freqs_ok;
0615     astr->cur_formats = chip->formats_ok;
0616 
0617     return 0;
0618 }
0619 
0620 static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
0621 {
0622     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0623 
0624     subs->runtime->hw = snd_pmac_playback;
0625     return snd_pmac_pcm_open(chip, &chip->playback, subs);
0626 }
0627 
0628 static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
0629 {
0630     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0631 
0632     subs->runtime->hw = snd_pmac_capture;
0633     return snd_pmac_pcm_open(chip, &chip->capture, subs);
0634 }
0635 
0636 static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
0637 {
0638     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0639 
0640     return snd_pmac_pcm_close(chip, &chip->playback, subs);
0641 }
0642 
0643 static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
0644 {
0645     struct snd_pmac *chip = snd_pcm_substream_chip(subs);
0646 
0647     return snd_pmac_pcm_close(chip, &chip->capture, subs);
0648 }
0649 
0650 /*
0651  */
0652 
0653 static const struct snd_pcm_ops snd_pmac_playback_ops = {
0654     .open =     snd_pmac_playback_open,
0655     .close =    snd_pmac_playback_close,
0656     .prepare =  snd_pmac_playback_prepare,
0657     .trigger =  snd_pmac_playback_trigger,
0658     .pointer =  snd_pmac_playback_pointer,
0659 };
0660 
0661 static const struct snd_pcm_ops snd_pmac_capture_ops = {
0662     .open =     snd_pmac_capture_open,
0663     .close =    snd_pmac_capture_close,
0664     .prepare =  snd_pmac_capture_prepare,
0665     .trigger =  snd_pmac_capture_trigger,
0666     .pointer =  snd_pmac_capture_pointer,
0667 };
0668 
0669 int snd_pmac_pcm_new(struct snd_pmac *chip)
0670 {
0671     struct snd_pcm *pcm;
0672     int err;
0673     int num_captures = 1;
0674 
0675     if (! chip->can_capture)
0676         num_captures = 0;
0677     err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
0678     if (err < 0)
0679         return err;
0680 
0681     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
0682     if (chip->can_capture)
0683         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
0684 
0685     pcm->private_data = chip;
0686     pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
0687     strcpy(pcm->name, chip->card->shortname);
0688     chip->pcm = pcm;
0689 
0690     chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
0691     if (chip->can_byte_swap)
0692         chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
0693 
0694     chip->playback.cur_formats = chip->formats_ok;
0695     chip->capture.cur_formats = chip->formats_ok;
0696     chip->playback.cur_freqs = chip->freqs_ok;
0697     chip->capture.cur_freqs = chip->freqs_ok;
0698 
0699     /* preallocate 64k buffer */
0700     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
0701                        &chip->pdev->dev,
0702                        64 * 1024, 64 * 1024);
0703 
0704     return 0;
0705 }
0706 
0707 
0708 static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
0709 {
0710     out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
0711     snd_pmac_wait_ack(&chip->playback);
0712     out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
0713     snd_pmac_wait_ack(&chip->capture);
0714 }
0715 
0716 
0717 /*
0718  * handling beep
0719  */
0720 void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
0721 {
0722     struct pmac_stream *rec = &chip->playback;
0723 
0724     snd_pmac_dma_stop(rec);
0725     chip->extra_dma.cmds->req_count = cpu_to_le16(bytes);
0726     chip->extra_dma.cmds->xfer_status = cpu_to_le16(0);
0727     chip->extra_dma.cmds->cmd_dep = cpu_to_le32(chip->extra_dma.addr);
0728     chip->extra_dma.cmds->phy_addr = cpu_to_le32(addr);
0729     chip->extra_dma.cmds->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS);
0730     out_le32(&chip->awacs->control,
0731          (in_le32(&chip->awacs->control) & ~0x1f00)
0732          | (speed << 8));
0733     out_le32(&chip->awacs->byteswap, 0);
0734     snd_pmac_dma_set_command(rec, &chip->extra_dma);
0735     snd_pmac_dma_run(rec, RUN);
0736 }
0737 
0738 void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
0739 {
0740     snd_pmac_dma_stop(&chip->playback);
0741     chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
0742     snd_pmac_pcm_set_format(chip); /* reset format */
0743 }
0744 
0745 
0746 /*
0747  * interrupt handlers
0748  */
0749 static irqreturn_t
0750 snd_pmac_tx_intr(int irq, void *devid)
0751 {
0752     struct snd_pmac *chip = devid;
0753     snd_pmac_pcm_update(chip, &chip->playback);
0754     return IRQ_HANDLED;
0755 }
0756 
0757 
0758 static irqreturn_t
0759 snd_pmac_rx_intr(int irq, void *devid)
0760 {
0761     struct snd_pmac *chip = devid;
0762     snd_pmac_pcm_update(chip, &chip->capture);
0763     return IRQ_HANDLED;
0764 }
0765 
0766 
0767 static irqreturn_t
0768 snd_pmac_ctrl_intr(int irq, void *devid)
0769 {
0770     struct snd_pmac *chip = devid;
0771     int ctrl = in_le32(&chip->awacs->control);
0772 
0773     /*printk(KERN_DEBUG "pmac: control interrupt.. 0x%x\n", ctrl);*/
0774     if (ctrl & MASK_PORTCHG) {
0775         /* do something when headphone is plugged/unplugged? */
0776         if (chip->update_automute)
0777             chip->update_automute(chip, 1);
0778     }
0779     if (ctrl & MASK_CNTLERR) {
0780         int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
0781         if (err && chip->model <= PMAC_SCREAMER)
0782             snd_printk(KERN_DEBUG "error %x\n", err);
0783     }
0784     /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
0785     out_le32(&chip->awacs->control, ctrl);
0786     return IRQ_HANDLED;
0787 }
0788 
0789 
0790 /*
0791  * a wrapper to feature call for compatibility
0792  */
0793 static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
0794 {
0795     if (ppc_md.feature_call)
0796         ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
0797 }
0798 
0799 /*
0800  * release resources
0801  */
0802 
0803 static int snd_pmac_free(struct snd_pmac *chip)
0804 {
0805     /* stop sounds */
0806     if (chip->initialized) {
0807         snd_pmac_dbdma_reset(chip);
0808         /* disable interrupts from awacs interface */
0809         out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
0810     }
0811 
0812     if (chip->node)
0813         snd_pmac_sound_feature(chip, 0);
0814 
0815     /* clean up mixer if any */
0816     if (chip->mixer_free)
0817         chip->mixer_free(chip);
0818 
0819     snd_pmac_detach_beep(chip);
0820 
0821     /* release resources */
0822     if (chip->irq >= 0)
0823         free_irq(chip->irq, (void*)chip);
0824     if (chip->tx_irq >= 0)
0825         free_irq(chip->tx_irq, (void*)chip);
0826     if (chip->rx_irq >= 0)
0827         free_irq(chip->rx_irq, (void*)chip);
0828     snd_pmac_dbdma_free(chip, &chip->playback.cmd);
0829     snd_pmac_dbdma_free(chip, &chip->capture.cmd);
0830     snd_pmac_dbdma_free(chip, &chip->extra_dma);
0831     snd_pmac_dbdma_free(chip, &emergency_dbdma);
0832     iounmap(chip->macio_base);
0833     iounmap(chip->latch_base);
0834     iounmap(chip->awacs);
0835     iounmap(chip->playback.dma);
0836     iounmap(chip->capture.dma);
0837 
0838     if (chip->node) {
0839         int i;
0840         for (i = 0; i < 3; i++) {
0841             if (chip->requested & (1 << i))
0842                 release_mem_region(chip->rsrc[i].start,
0843                            resource_size(&chip->rsrc[i]));
0844         }
0845     }
0846 
0847     pci_dev_put(chip->pdev);
0848     of_node_put(chip->node);
0849     kfree(chip);
0850     return 0;
0851 }
0852 
0853 
0854 /*
0855  * free the device
0856  */
0857 static int snd_pmac_dev_free(struct snd_device *device)
0858 {
0859     struct snd_pmac *chip = device->device_data;
0860     return snd_pmac_free(chip);
0861 }
0862 
0863 
0864 /*
0865  * check the machine support byteswap (little-endian)
0866  */
0867 
0868 static void detect_byte_swap(struct snd_pmac *chip)
0869 {
0870     struct device_node *mio;
0871 
0872     /* if seems that Keylargo can't byte-swap  */
0873     for (mio = chip->node->parent; mio; mio = mio->parent) {
0874         if (of_node_name_eq(mio, "mac-io")) {
0875             if (of_device_is_compatible(mio, "Keylargo"))
0876                 chip->can_byte_swap = 0;
0877             break;
0878         }
0879     }
0880 
0881     /* it seems the Pismo & iBook can't byte-swap in hardware. */
0882     if (of_machine_is_compatible("PowerBook3,1") ||
0883         of_machine_is_compatible("PowerBook2,1"))
0884         chip->can_byte_swap = 0 ;
0885 
0886     if (of_machine_is_compatible("PowerBook2,1"))
0887         chip->can_duplex = 0;
0888 }
0889 
0890 
0891 /*
0892  * detect a sound chip
0893  */
0894 static int snd_pmac_detect(struct snd_pmac *chip)
0895 {
0896     struct device_node *sound;
0897     struct device_node *dn;
0898     const unsigned int *prop;
0899     unsigned int l;
0900     struct macio_chip* macio;
0901 
0902     if (!machine_is(powermac))
0903         return -ENODEV;
0904 
0905     chip->subframe = 0;
0906     chip->revision = 0;
0907     chip->freqs_ok = 0xff; /* all ok */
0908     chip->model = PMAC_AWACS;
0909     chip->can_byte_swap = 1;
0910     chip->can_duplex = 1;
0911     chip->can_capture = 1;
0912     chip->num_freqs = ARRAY_SIZE(awacs_freqs);
0913     chip->freq_table = awacs_freqs;
0914     chip->pdev = NULL;
0915 
0916     chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
0917 
0918     /* check machine type */
0919     if (of_machine_is_compatible("AAPL,3400/2400")
0920         || of_machine_is_compatible("AAPL,3500"))
0921         chip->is_pbook_3400 = 1;
0922     else if (of_machine_is_compatible("PowerBook1,1")
0923          || of_machine_is_compatible("AAPL,PowerBook1998"))
0924         chip->is_pbook_G3 = 1;
0925     chip->node = of_find_node_by_name(NULL, "awacs");
0926     sound = of_node_get(chip->node);
0927 
0928     /*
0929      * powermac G3 models have a node called "davbus"
0930      * with a child called "sound".
0931      */
0932     if (!chip->node)
0933         chip->node = of_find_node_by_name(NULL, "davbus");
0934     /*
0935      * if we didn't find a davbus device, try 'i2s-a' since
0936      * this seems to be what iBooks have
0937      */
0938     if (! chip->node) {
0939         chip->node = of_find_node_by_name(NULL, "i2s-a");
0940         if (chip->node && chip->node->parent &&
0941             chip->node->parent->parent) {
0942             if (of_device_is_compatible(chip->node->parent->parent,
0943                          "K2-Keylargo"))
0944                 chip->is_k2 = 1;
0945         }
0946     }
0947     if (! chip->node)
0948         return -ENODEV;
0949 
0950     if (!sound) {
0951         for_each_node_by_name(sound, "sound")
0952             if (sound->parent == chip->node)
0953                 break;
0954     }
0955     if (! sound) {
0956         of_node_put(chip->node);
0957         chip->node = NULL;
0958         return -ENODEV;
0959     }
0960     prop = of_get_property(sound, "sub-frame", NULL);
0961     if (prop && *prop < 16)
0962         chip->subframe = *prop;
0963     prop = of_get_property(sound, "layout-id", NULL);
0964     if (prop) {
0965         /* partly deprecate snd-powermac, for those machines
0966          * that have a layout-id property for now */
0967         printk(KERN_INFO "snd-powermac no longer handles any "
0968                  "machines with a layout-id property "
0969                  "in the device-tree, use snd-aoa.\n");
0970         of_node_put(sound);
0971         of_node_put(chip->node);
0972         chip->node = NULL;
0973         return -ENODEV;
0974     }
0975     /* This should be verified on older screamers */
0976     if (of_device_is_compatible(sound, "screamer")) {
0977         chip->model = PMAC_SCREAMER;
0978         // chip->can_byte_swap = 0; /* FIXME: check this */
0979     }
0980     if (of_device_is_compatible(sound, "burgundy")) {
0981         chip->model = PMAC_BURGUNDY;
0982         chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
0983     }
0984     if (of_device_is_compatible(sound, "daca")) {
0985         chip->model = PMAC_DACA;
0986         chip->can_capture = 0;  /* no capture */
0987         chip->can_duplex = 0;
0988         // chip->can_byte_swap = 0; /* FIXME: check this */
0989         chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
0990     }
0991     if (of_device_is_compatible(sound, "tumbler")) {
0992         chip->model = PMAC_TUMBLER;
0993         chip->can_capture = of_machine_is_compatible("PowerMac4,2")
0994                 || of_machine_is_compatible("PowerBook3,2")
0995                 || of_machine_is_compatible("PowerBook3,3")
0996                 || of_machine_is_compatible("PowerBook4,1")
0997                 || of_machine_is_compatible("PowerBook4,2")
0998                 || of_machine_is_compatible("PowerBook4,3");
0999         chip->can_duplex = 0;
1000         // chip->can_byte_swap = 0; /* FIXME: check this */
1001         chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1002         chip->freq_table = tumbler_freqs;
1003         chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1004     }
1005     if (of_device_is_compatible(sound, "snapper")) {
1006         chip->model = PMAC_SNAPPER;
1007         // chip->can_byte_swap = 0; /* FIXME: check this */
1008         chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1009         chip->freq_table = tumbler_freqs;
1010         chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1011     }
1012     prop = of_get_property(sound, "device-id", NULL);
1013     if (prop)
1014         chip->device_id = *prop;
1015     dn = of_find_node_by_name(NULL, "perch");
1016     chip->has_iic = (dn != NULL);
1017     of_node_put(dn);
1018 
1019     /* We need the PCI device for DMA allocations, let's use a crude method
1020      * for now ...
1021      */
1022     macio = macio_find(chip->node, macio_unknown);
1023     if (macio == NULL)
1024         printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
1025     else {
1026         struct pci_dev *pdev = NULL;
1027 
1028         for_each_pci_dev(pdev) {
1029             struct device_node *np = pci_device_to_OF_node(pdev);
1030             if (np && np == macio->of_node) {
1031                 chip->pdev = pdev;
1032                 break;
1033             }
1034         }
1035     }
1036     if (chip->pdev == NULL)
1037         printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1038                " device !\n");
1039 
1040     detect_byte_swap(chip);
1041 
1042     /* look for a property saying what sample rates
1043        are available */
1044     prop = of_get_property(sound, "sample-rates", &l);
1045     if (! prop)
1046         prop = of_get_property(sound, "output-frame-rates", &l);
1047     if (prop) {
1048         int i;
1049         chip->freqs_ok = 0;
1050         for (l /= sizeof(int); l > 0; --l) {
1051             unsigned int r = *prop++;
1052             /* Apple 'Fixed' format */
1053             if (r >= 0x10000)
1054                 r >>= 16;
1055             for (i = 0; i < chip->num_freqs; ++i) {
1056                 if (r == chip->freq_table[i]) {
1057                     chip->freqs_ok |= (1 << i);
1058                     break;
1059                 }
1060             }
1061         }
1062     } else {
1063         /* assume only 44.1khz */
1064         chip->freqs_ok = 1;
1065     }
1066 
1067     of_node_put(sound);
1068     return 0;
1069 }
1070 
1071 #ifdef PMAC_SUPPORT_AUTOMUTE
1072 /*
1073  * auto-mute
1074  */
1075 static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1076                   struct snd_ctl_elem_value *ucontrol)
1077 {
1078     struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1079     ucontrol->value.integer.value[0] = chip->auto_mute;
1080     return 0;
1081 }
1082 
1083 static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1084                   struct snd_ctl_elem_value *ucontrol)
1085 {
1086     struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1087     if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1088         chip->auto_mute = !!ucontrol->value.integer.value[0];
1089         if (chip->update_automute)
1090             chip->update_automute(chip, 1);
1091         return 1;
1092     }
1093     return 0;
1094 }
1095 
1096 static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1097                   struct snd_ctl_elem_value *ucontrol)
1098 {
1099     struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1100     if (chip->detect_headphone)
1101         ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1102     else
1103         ucontrol->value.integer.value[0] = 0;
1104     return 0;
1105 }
1106 
1107 static const struct snd_kcontrol_new auto_mute_controls[] = {
1108     { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1109       .name = "Auto Mute Switch",
1110       .info = snd_pmac_boolean_mono_info,
1111       .get = pmac_auto_mute_get,
1112       .put = pmac_auto_mute_put,
1113     },
1114     { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1115       .name = "Headphone Detection",
1116       .access = SNDRV_CTL_ELEM_ACCESS_READ,
1117       .info = snd_pmac_boolean_mono_info,
1118       .get = pmac_hp_detect_get,
1119     },
1120 };
1121 
1122 int snd_pmac_add_automute(struct snd_pmac *chip)
1123 {
1124     int err;
1125     chip->auto_mute = 1;
1126     err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1127     if (err < 0) {
1128         printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
1129         return err;
1130     }
1131     chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1132     return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1133 }
1134 #endif /* PMAC_SUPPORT_AUTOMUTE */
1135 
1136 /*
1137  * create and detect a pmac chip record
1138  */
1139 int snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1140 {
1141     struct snd_pmac *chip;
1142     struct device_node *np;
1143     int i, err;
1144     unsigned int irq;
1145     unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1146     static const struct snd_device_ops ops = {
1147         .dev_free = snd_pmac_dev_free,
1148     };
1149 
1150     *chip_return = NULL;
1151 
1152     chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1153     if (chip == NULL)
1154         return -ENOMEM;
1155     chip->card = card;
1156 
1157     spin_lock_init(&chip->reg_lock);
1158     chip->irq = chip->tx_irq = chip->rx_irq = -1;
1159 
1160     chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1161     chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1162 
1163     err = snd_pmac_detect(chip);
1164     if (err < 0)
1165         goto __error;
1166 
1167     if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1168         snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1169         snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 ||
1170         snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) {
1171         err = -ENOMEM;
1172         goto __error;
1173     }
1174 
1175     np = chip->node;
1176     chip->requested = 0;
1177     if (chip->is_k2) {
1178         static const char * const rnames[] = {
1179             "Sound Control", "Sound DMA" };
1180         for (i = 0; i < 2; i ++) {
1181             if (of_address_to_resource(np->parent, i,
1182                            &chip->rsrc[i])) {
1183                 printk(KERN_ERR "snd: can't translate rsrc "
1184                        " %d (%s)\n", i, rnames[i]);
1185                 err = -ENODEV;
1186                 goto __error;
1187             }
1188             if (request_mem_region(chip->rsrc[i].start,
1189                            resource_size(&chip->rsrc[i]),
1190                            rnames[i]) == NULL) {
1191                 printk(KERN_ERR "snd: can't request rsrc "
1192                        " %d (%s: %pR)\n",
1193                        i, rnames[i], &chip->rsrc[i]);
1194                 err = -ENODEV;
1195                 goto __error;
1196             }
1197             chip->requested |= (1 << i);
1198         }
1199         ctrl_addr = chip->rsrc[0].start;
1200         txdma_addr = chip->rsrc[1].start;
1201         rxdma_addr = txdma_addr + 0x100;
1202     } else {
1203         static const char * const rnames[] = {
1204             "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1205         for (i = 0; i < 3; i ++) {
1206             if (of_address_to_resource(np, i,
1207                            &chip->rsrc[i])) {
1208                 printk(KERN_ERR "snd: can't translate rsrc "
1209                        " %d (%s)\n", i, rnames[i]);
1210                 err = -ENODEV;
1211                 goto __error;
1212             }
1213             if (request_mem_region(chip->rsrc[i].start,
1214                            resource_size(&chip->rsrc[i]),
1215                            rnames[i]) == NULL) {
1216                 printk(KERN_ERR "snd: can't request rsrc "
1217                        " %d (%s: %pR)\n",
1218                        i, rnames[i], &chip->rsrc[i]);
1219                 err = -ENODEV;
1220                 goto __error;
1221             }
1222             chip->requested |= (1 << i);
1223         }
1224         ctrl_addr = chip->rsrc[0].start;
1225         txdma_addr = chip->rsrc[1].start;
1226         rxdma_addr = chip->rsrc[2].start;
1227     }
1228 
1229     chip->awacs = ioremap(ctrl_addr, 0x1000);
1230     chip->playback.dma = ioremap(txdma_addr, 0x100);
1231     chip->capture.dma = ioremap(rxdma_addr, 0x100);
1232     if (chip->model <= PMAC_BURGUNDY) {
1233         irq = irq_of_parse_and_map(np, 0);
1234         if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1235                 "PMac", (void*)chip)) {
1236             snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1237                    irq);
1238             err = -EBUSY;
1239             goto __error;
1240         }
1241         chip->irq = irq;
1242     }
1243     irq = irq_of_parse_and_map(np, 1);
1244     if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1245         snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1246         err = -EBUSY;
1247         goto __error;
1248     }
1249     chip->tx_irq = irq;
1250     irq = irq_of_parse_and_map(np, 2);
1251     if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1252         snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1253         err = -EBUSY;
1254         goto __error;
1255     }
1256     chip->rx_irq = irq;
1257 
1258     snd_pmac_sound_feature(chip, 1);
1259 
1260     /* reset & enable interrupts */
1261     if (chip->model <= PMAC_BURGUNDY)
1262         out_le32(&chip->awacs->control, chip->control_mask);
1263 
1264     /* Powerbooks have odd ways of enabling inputs such as
1265        an expansion-bay CD or sound from an internal modem
1266        or a PC-card modem. */
1267     if (chip->is_pbook_3400) {
1268         /* Enable CD and PC-card sound inputs. */
1269         /* This is done by reading from address
1270          * f301a000, + 0x10 to enable the expansion-bay
1271          * CD sound input, + 0x80 to enable the PC-card
1272          * sound input.  The 0x100 enables the SCSI bus
1273          * terminator power.
1274          */
1275         chip->latch_base = ioremap (0xf301a000, 0x1000);
1276         in_8(chip->latch_base + 0x190);
1277     } else if (chip->is_pbook_G3) {
1278         struct device_node* mio;
1279         for (mio = chip->node->parent; mio; mio = mio->parent) {
1280             if (of_node_name_eq(mio, "mac-io")) {
1281                 struct resource r;
1282                 if (of_address_to_resource(mio, 0, &r) == 0)
1283                     chip->macio_base =
1284                         ioremap(r.start, 0x40);
1285                 break;
1286             }
1287         }
1288         /* Enable CD sound input. */
1289         /* The relevant bits for writing to this byte are 0x8f.
1290          * I haven't found out what the 0x80 bit does.
1291          * For the 0xf bits, writing 3 or 7 enables the CD
1292          * input, any other value disables it.  Values
1293          * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1294          * 4, 6, 8 - f enable the input from the modem.
1295          */
1296         if (chip->macio_base)
1297             out_8(chip->macio_base + 0x37, 3);
1298     }
1299 
1300     /* Reset dbdma channels */
1301     snd_pmac_dbdma_reset(chip);
1302 
1303     err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1304     if (err < 0)
1305         goto __error;
1306 
1307     *chip_return = chip;
1308     return 0;
1309 
1310  __error:
1311     snd_pmac_free(chip);
1312     return err;
1313 }
1314 
1315 
1316 /*
1317  * sleep notify for powerbook
1318  */
1319 
1320 #ifdef CONFIG_PM
1321 
1322 /*
1323  * Save state when going to sleep, restore it afterwards.
1324  */
1325 
1326 void snd_pmac_suspend(struct snd_pmac *chip)
1327 {
1328     unsigned long flags;
1329 
1330     snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1331     if (chip->suspend)
1332         chip->suspend(chip);
1333     spin_lock_irqsave(&chip->reg_lock, flags);
1334     snd_pmac_beep_stop(chip);
1335     spin_unlock_irqrestore(&chip->reg_lock, flags);
1336     if (chip->irq >= 0)
1337         disable_irq(chip->irq);
1338     if (chip->tx_irq >= 0)
1339         disable_irq(chip->tx_irq);
1340     if (chip->rx_irq >= 0)
1341         disable_irq(chip->rx_irq);
1342     snd_pmac_sound_feature(chip, 0);
1343 }
1344 
1345 void snd_pmac_resume(struct snd_pmac *chip)
1346 {
1347     snd_pmac_sound_feature(chip, 1);
1348     if (chip->resume)
1349         chip->resume(chip);
1350     /* enable CD sound input */
1351     if (chip->macio_base && chip->is_pbook_G3)
1352         out_8(chip->macio_base + 0x37, 3);
1353     else if (chip->is_pbook_3400)
1354         in_8(chip->latch_base + 0x190);
1355 
1356     snd_pmac_pcm_set_format(chip);
1357 
1358     if (chip->irq >= 0)
1359         enable_irq(chip->irq);
1360     if (chip->tx_irq >= 0)
1361         enable_irq(chip->tx_irq);
1362     if (chip->rx_irq >= 0)
1363         enable_irq(chip->rx_irq);
1364 
1365     snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1366 }
1367 
1368 #endif /* CONFIG_PM */
1369