Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * PMac DBDMA lowlevel functions |
| 4 | * |
| 5 | * Copyright (c) by Takashi Iwai <tiwai@suse.de> |
| 6 | * code based on dmasound.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
Takashi Iwai | 6cbbfe1 | 2015-01-28 16:49:33 +0100 | [diff] [blame] | 10 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/irq.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/interrupt.h> |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 16 | #include <linux/pci.h> |
| 17 | #include <linux/dma-mapping.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <sound/core.h> |
| 21 | #include "pmac.h" |
| 22 | #include <sound/pcm_params.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/pmac_feature.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */ |
Takashi Iwai | 6e9ef32 | 2020-01-05 15:47:32 +0100 | [diff] [blame] | 27 | static const int awacs_freqs[8] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350 |
| 29 | }; |
| 30 | /* fixed frequency table for tumbler */ |
Takashi Iwai | 6e9ef32 | 2020-01-05 15:47:32 +0100 | [diff] [blame] | 31 | static const int tumbler_freqs[1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | 44100 |
| 33 | }; |
| 34 | |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * we will allocate a single 'emergency' dbdma cmd block to use if the |
| 38 | * tx status comes up "DEAD". This happens on some PowerComputing Pmac |
| 39 | * clones, either owing to a bug in dbdma or some interaction between |
| 40 | * IDE and sound. However, this measure would deal with DEAD status if |
| 41 | * it appeared elsewhere. |
| 42 | */ |
| 43 | static struct pmac_dbdma emergency_dbdma; |
| 44 | static int emergency_in_use; |
| 45 | |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * allocate DBDMA command arrays |
| 49 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 50 | static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 52 | unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1); |
| 53 | |
| 54 | rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize, |
| 55 | &rec->dma_base, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (rec->space == NULL) |
| 57 | return -ENOMEM; |
| 58 | rec->size = size; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 59 | memset(rec->space, 0, rsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 61 | rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space); |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 66 | static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Benjamin Herrenschmidt | 367636e | 2006-02-08 15:04:18 +1100 | [diff] [blame] | 68 | if (rec->space) { |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 69 | unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1); |
| 70 | |
| 71 | dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base); |
| 72 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * pcm stuff |
| 78 | */ |
| 79 | |
| 80 | /* |
| 81 | * look up frequency table |
| 82 | */ |
| 83 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 84 | unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | int i, ok, found; |
| 87 | |
| 88 | ok = rec->cur_freqs; |
| 89 | if (rate > chip->freq_table[0]) |
| 90 | return 0; |
| 91 | found = 0; |
| 92 | for (i = 0; i < chip->num_freqs; i++, ok >>= 1) { |
| 93 | if (! (ok & 1)) continue; |
| 94 | found = i; |
| 95 | if (rate >= chip->freq_table[i]) |
| 96 | break; |
| 97 | } |
| 98 | return found; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * check whether another stream is active |
| 103 | */ |
| 104 | static inline int another_stream(int stream) |
| 105 | { |
| 106 | return (stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 107 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 108 | } |
| 109 | |
| 110 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * get a stream of the opposite direction |
| 112 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 113 | static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | switch (stream) { |
| 116 | case SNDRV_PCM_STREAM_PLAYBACK: |
| 117 | return &chip->playback; |
| 118 | case SNDRV_PCM_STREAM_CAPTURE: |
| 119 | return &chip->capture; |
| 120 | default: |
| 121 | snd_BUG(); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * wait while run status is on |
| 128 | */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 129 | static inline void |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 130 | snd_pmac_wait_ack(struct pmac_stream *rec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | int timeout = 50000; |
| 133 | while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0) |
| 134 | udelay(1); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * set the format and rate to the chip. |
| 139 | * call the lowlevel function if defined (e.g. for AWACS). |
| 140 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 141 | static void snd_pmac_pcm_set_format(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | /* set up frequency and format */ |
| 144 | out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8)); |
| 145 | out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0); |
| 146 | if (chip->set_format) |
| 147 | chip->set_format(chip); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * stop the DMA transfer |
| 152 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 153 | static inline void snd_pmac_dma_stop(struct pmac_stream *rec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16); |
| 156 | snd_pmac_wait_ack(rec); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * set the command pointer address |
| 161 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 162 | static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | out_le32(&rec->dma->cmdptr, cmd->addr); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * start the DMA |
| 169 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 170 | static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | out_le32(&rec->dma->control, status | (status << 16)); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * prepare playback/capture stream |
| 178 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 179 | static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | int i; |
| 182 | volatile struct dbdma_cmd __iomem *cp; |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 183 | struct snd_pcm_runtime *runtime = subs->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int rate_index; |
| 185 | long offset; |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 186 | struct pmac_stream *astr; |
Risto Suominen | 946cda7 | 2008-04-16 13:16:05 +0200 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | rec->dma_size = snd_pcm_lib_buffer_bytes(subs); |
| 189 | rec->period_size = snd_pcm_lib_period_bytes(subs); |
| 190 | rec->nperiods = rec->dma_size / rec->period_size; |
| 191 | rec->cur_period = 0; |
| 192 | rate_index = snd_pmac_rate_index(chip, rec, runtime->rate); |
| 193 | |
| 194 | /* set up constraints */ |
| 195 | astr = snd_pmac_get_stream(chip, another_stream(rec->stream)); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 196 | if (! astr) |
| 197 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | astr->cur_freqs = 1 << rate_index; |
| 199 | astr->cur_formats = 1 << runtime->format; |
| 200 | chip->rate_index = rate_index; |
| 201 | chip->format = runtime->format; |
| 202 | |
| 203 | /* We really want to execute a DMA stop command, after the AWACS |
| 204 | * is initialized. |
| 205 | * For reasons I don't understand, it stops the hissing noise |
| 206 | * common to many PowerBook G3 systems and random noise otherwise |
| 207 | * captured on iBook2's about every third time. -ReneR |
| 208 | */ |
| 209 | spin_lock_irq(&chip->reg_lock); |
| 210 | snd_pmac_dma_stop(rec); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 211 | chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | snd_pmac_dma_set_command(rec, &chip->extra_dma); |
| 213 | snd_pmac_dma_run(rec, RUN); |
| 214 | spin_unlock_irq(&chip->reg_lock); |
| 215 | mdelay(5); |
| 216 | spin_lock_irq(&chip->reg_lock); |
| 217 | /* continuous DMA memory type doesn't provide the physical address, |
| 218 | * so we need to resolve the address here... |
| 219 | */ |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 220 | offset = runtime->dma_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) { |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 222 | cp->phy_addr = cpu_to_le32(offset); |
| 223 | cp->req_count = cpu_to_le16(rec->period_size); |
| 224 | /*cp->res_count = cpu_to_le16(0);*/ |
| 225 | cp->xfer_status = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | offset += rec->period_size; |
| 227 | } |
| 228 | /* make loop */ |
Samuel Zou | 59f0071 | 2020-05-07 11:54:07 +0800 | [diff] [blame] | 229 | cp->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 230 | cp->cmd_dep = cpu_to_le32(rec->cmd.addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | snd_pmac_dma_stop(rec); |
| 233 | snd_pmac_dma_set_command(rec, &rec->cmd); |
| 234 | spin_unlock_irq(&chip->reg_lock); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /* |
| 241 | * PCM trigger/stop |
| 242 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 243 | static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec, |
| 244 | struct snd_pcm_substream *subs, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | volatile struct dbdma_cmd __iomem *cp; |
| 247 | int i, command; |
| 248 | |
| 249 | switch (cmd) { |
| 250 | case SNDRV_PCM_TRIGGER_START: |
| 251 | case SNDRV_PCM_TRIGGER_RESUME: |
| 252 | if (rec->running) |
| 253 | return -EBUSY; |
| 254 | command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ? |
| 255 | OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS; |
| 256 | spin_lock(&chip->reg_lock); |
| 257 | snd_pmac_beep_stop(chip); |
| 258 | snd_pmac_pcm_set_format(chip); |
| 259 | for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) |
| 260 | out_le16(&cp->command, command); |
| 261 | snd_pmac_dma_set_command(rec, &rec->cmd); |
| 262 | (void)in_le32(&rec->dma->status); |
| 263 | snd_pmac_dma_run(rec, RUN|WAKE); |
| 264 | rec->running = 1; |
| 265 | spin_unlock(&chip->reg_lock); |
| 266 | break; |
| 267 | |
| 268 | case SNDRV_PCM_TRIGGER_STOP: |
| 269 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 270 | spin_lock(&chip->reg_lock); |
| 271 | rec->running = 0; |
Takashi Iwai | 6da6711 | 2009-02-05 16:02:46 +0100 | [diff] [blame] | 272 | /*printk(KERN_DEBUG "stopped!!\n");*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | snd_pmac_dma_stop(rec); |
| 274 | for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) |
| 275 | out_le16(&cp->command, DBDMA_STOP); |
| 276 | spin_unlock(&chip->reg_lock); |
| 277 | break; |
| 278 | |
| 279 | default: |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * return the current pointer |
| 288 | */ |
| 289 | inline |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 290 | static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip, |
| 291 | struct pmac_stream *rec, |
| 292 | struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | int count = 0; |
| 295 | |
| 296 | #if 1 /* hmm.. how can we get the current dma pointer?? */ |
| 297 | int stat; |
| 298 | volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period]; |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 299 | stat = le16_to_cpu(cp->xfer_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (stat & (ACTIVE|DEAD)) { |
| 301 | count = in_le16(&cp->res_count); |
| 302 | if (count) |
| 303 | count = rec->period_size - count; |
| 304 | } |
| 305 | #endif |
| 306 | count += rec->cur_period * rec->period_size; |
Takashi Iwai | 6da6711 | 2009-02-05 16:02:46 +0100 | [diff] [blame] | 307 | /*printk(KERN_DEBUG "pointer=%d\n", count);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return bytes_to_frames(subs->runtime, count); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * playback |
| 313 | */ |
| 314 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 315 | static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 317 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return snd_pmac_pcm_prepare(chip, &chip->playback, subs); |
| 319 | } |
| 320 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 321 | static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | int cmd) |
| 323 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 324 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd); |
| 326 | } |
| 327 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 328 | static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 330 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return snd_pmac_pcm_pointer(chip, &chip->playback, subs); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
| 336 | * capture |
| 337 | */ |
| 338 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 339 | static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 341 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | return snd_pmac_pcm_prepare(chip, &chip->capture, subs); |
| 343 | } |
| 344 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 345 | static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | int cmd) |
| 347 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 348 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd); |
| 350 | } |
| 351 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 352 | static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 354 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return snd_pmac_pcm_pointer(chip, &chip->capture, subs); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /* |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 360 | * Handle DEAD DMA transfers: |
| 361 | * if the TX status comes up "DEAD" - reported on some Power Computing machines |
| 362 | * we need to re-start the dbdma - but from a different physical start address |
| 363 | * and with a different transfer length. It would get very messy to do this |
| 364 | * with the normal dbdma_cmd blocks - we would have to re-write the buffer start |
| 365 | * addresses each time. So, we will keep a single dbdma_cmd block which can be |
| 366 | * fiddled with. |
| 367 | * When DEAD status is first reported the content of the faulted dbdma block is |
| 368 | * copied into the emergency buffer and we note that the buffer is in use. |
| 369 | * we then bump the start physical address by the amount that was successfully |
| 370 | * output before it died. |
| 371 | * On any subsequent DEAD result we just do the bump-ups (we know that we are |
| 372 | * already using the emergency dbdma_cmd). |
| 373 | * CHECK: this just tries to "do it". It is possible that we should abandon |
| 374 | * xfers when the number of residual bytes gets below a certain value - I can |
| 375 | * see that this might cause a loop-forever if a too small transfer causes |
| 376 | * DEAD status. However this is a TODO for now - we'll see what gets reported. |
| 377 | * When we get a successful transfer result with the emergency buffer we just |
| 378 | * pretend that it completed using the original dmdma_cmd and carry on. The |
| 379 | * 'next_cmd' field will already point back to the original loop of blocks. |
| 380 | */ |
| 381 | static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec, |
| 382 | volatile struct dbdma_cmd __iomem *cp) |
| 383 | { |
| 384 | unsigned short req, res ; |
| 385 | unsigned int phy ; |
| 386 | |
| 387 | /* printk(KERN_WARNING "snd-powermac: DMA died - patching it up!\n"); */ |
| 388 | |
| 389 | /* to clear DEAD status we must first clear RUN |
| 390 | set it to quiescent to be on the safe side */ |
| 391 | (void)in_le32(&rec->dma->status); |
| 392 | out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16); |
| 393 | |
| 394 | if (!emergency_in_use) { /* new problem */ |
| 395 | memcpy((void *)emergency_dbdma.cmds, (void *)cp, |
| 396 | sizeof(struct dbdma_cmd)); |
| 397 | emergency_in_use = 1; |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 398 | cp->xfer_status = cpu_to_le16(0); |
| 399 | cp->req_count = cpu_to_le16(rec->period_size); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 400 | cp = emergency_dbdma.cmds; |
| 401 | } |
| 402 | |
| 403 | /* now bump the values to reflect the amount |
| 404 | we haven't yet shifted */ |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 405 | req = le16_to_cpu(cp->req_count); |
| 406 | res = le16_to_cpu(cp->res_count); |
| 407 | phy = le32_to_cpu(cp->phy_addr); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 408 | phy += (req - res); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 409 | cp->req_count = cpu_to_le16(res); |
| 410 | cp->res_count = cpu_to_le16(0); |
| 411 | cp->xfer_status = cpu_to_le16(0); |
| 412 | cp->phy_addr = cpu_to_le32(phy); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 413 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 414 | cp->cmd_dep = cpu_to_le32(rec->cmd.addr |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 415 | + sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods)); |
| 416 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 417 | cp->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 418 | |
| 419 | /* point at our patched up command block */ |
| 420 | out_le32(&rec->dma->cmdptr, emergency_dbdma.addr); |
| 421 | |
| 422 | /* we must re-start the controller */ |
| 423 | (void)in_le32(&rec->dma->status); |
| 424 | /* should complete clearing the DEAD status */ |
| 425 | out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE)); |
| 426 | } |
| 427 | |
| 428 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | * update playback/capture pointer from interrupts |
| 430 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 431 | static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | volatile struct dbdma_cmd __iomem *cp; |
| 434 | int c; |
| 435 | int stat; |
| 436 | |
| 437 | spin_lock(&chip->reg_lock); |
| 438 | if (rec->running) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */ |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 440 | |
| 441 | if (emergency_in_use) /* already using DEAD xfer? */ |
| 442 | cp = emergency_dbdma.cmds; |
| 443 | else |
| 444 | cp = &rec->cmd.cmds[rec->cur_period]; |
| 445 | |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 446 | stat = le16_to_cpu(cp->xfer_status); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 447 | |
| 448 | if (stat & DEAD) { |
| 449 | snd_pmac_pcm_dead_xfer(rec, cp); |
| 450 | break; /* this block is still going */ |
| 451 | } |
| 452 | |
| 453 | if (emergency_in_use) |
| 454 | emergency_in_use = 0 ; /* done that */ |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (! (stat & ACTIVE)) |
| 457 | break; |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 458 | |
Takashi Iwai | 6da6711 | 2009-02-05 16:02:46 +0100 | [diff] [blame] | 459 | /*printk(KERN_DEBUG "update frag %d\n", rec->cur_period);*/ |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 460 | cp->xfer_status = cpu_to_le16(0); |
| 461 | cp->req_count = cpu_to_le16(rec->period_size); |
| 462 | /*cp->res_count = cpu_to_le16(0);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | rec->cur_period++; |
| 464 | if (rec->cur_period >= rec->nperiods) { |
| 465 | rec->cur_period = 0; |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | spin_unlock(&chip->reg_lock); |
| 469 | snd_pcm_period_elapsed(rec->substream); |
| 470 | spin_lock(&chip->reg_lock); |
| 471 | } |
| 472 | } |
| 473 | spin_unlock(&chip->reg_lock); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /* |
| 478 | * hw info |
| 479 | */ |
| 480 | |
Bhumika Goyal | 21bf6cf | 2017-08-17 14:45:56 +0530 | [diff] [blame] | 481 | static const struct snd_pcm_hardware snd_pmac_playback = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 484 | SNDRV_PCM_INFO_MMAP | |
| 485 | SNDRV_PCM_INFO_MMAP_VALID | |
| 486 | SNDRV_PCM_INFO_RESUME), |
| 487 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE, |
| 488 | .rates = SNDRV_PCM_RATE_8000_44100, |
| 489 | .rate_min = 7350, |
| 490 | .rate_max = 44100, |
| 491 | .channels_min = 2, |
| 492 | .channels_max = 2, |
| 493 | .buffer_bytes_max = 131072, |
| 494 | .period_bytes_min = 256, |
| 495 | .period_bytes_max = 16384, |
| 496 | .periods_min = 3, |
| 497 | .periods_max = PMAC_MAX_FRAGS, |
| 498 | }; |
| 499 | |
Bhumika Goyal | 21bf6cf | 2017-08-17 14:45:56 +0530 | [diff] [blame] | 500 | static const struct snd_pcm_hardware snd_pmac_capture = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 503 | SNDRV_PCM_INFO_MMAP | |
| 504 | SNDRV_PCM_INFO_MMAP_VALID | |
| 505 | SNDRV_PCM_INFO_RESUME), |
| 506 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE, |
| 507 | .rates = SNDRV_PCM_RATE_8000_44100, |
| 508 | .rate_min = 7350, |
| 509 | .rate_max = 44100, |
| 510 | .channels_min = 2, |
| 511 | .channels_max = 2, |
| 512 | .buffer_bytes_max = 131072, |
| 513 | .period_bytes_min = 256, |
| 514 | .period_bytes_max = 16384, |
| 515 | .periods_min = 3, |
| 516 | .periods_max = PMAC_MAX_FRAGS, |
| 517 | }; |
| 518 | |
| 519 | |
| 520 | #if 0 // NYI |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 521 | static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params, |
| 522 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 524 | struct snd_pmac *chip = rule->private; |
| 525 | struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | int i, freq_table[8], num_freqs; |
| 527 | |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 528 | if (! rec) |
| 529 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | num_freqs = 0; |
| 531 | for (i = chip->num_freqs - 1; i >= 0; i--) { |
| 532 | if (rec->cur_freqs & (1 << i)) |
| 533 | freq_table[num_freqs++] = chip->freq_table[i]; |
| 534 | } |
| 535 | |
| 536 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 537 | num_freqs, freq_table, 0); |
| 538 | } |
| 539 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 540 | static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params, |
| 541 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 543 | struct snd_pmac *chip = rule->private; |
| 544 | struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 546 | if (! rec) |
| 547 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), |
| 549 | rec->cur_formats); |
| 550 | } |
| 551 | #endif // NYI |
| 552 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 553 | static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec, |
| 554 | struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 556 | struct snd_pcm_runtime *runtime = subs->runtime; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 557 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | /* look up frequency table and fill bit mask */ |
| 560 | runtime->hw.rates = 0; |
Clemens Ladisch | 918f3a0 | 2007-08-13 17:40:54 +0200 | [diff] [blame] | 561 | for (i = 0; i < chip->num_freqs; i++) |
| 562 | if (chip->freqs_ok & (1 << i)) |
| 563 | runtime->hw.rates |= |
| 564 | snd_pcm_rate_to_rate_bit(chip->freq_table[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | /* check for minimum and maximum rates */ |
| 567 | for (i = 0; i < chip->num_freqs; i++) { |
| 568 | if (chip->freqs_ok & (1 << i)) { |
| 569 | runtime->hw.rate_max = chip->freq_table[i]; |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | for (i = chip->num_freqs - 1; i >= 0; i--) { |
| 574 | if (chip->freqs_ok & (1 << i)) { |
| 575 | runtime->hw.rate_min = chip->freq_table[i]; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | runtime->hw.formats = chip->formats_ok; |
| 580 | if (chip->can_capture) { |
| 581 | if (! chip->can_duplex) |
| 582 | runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX; |
| 583 | runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 584 | } |
| 585 | runtime->private_data = rec; |
| 586 | rec->substream = subs; |
| 587 | |
| 588 | #if 0 /* FIXME: still under development.. */ |
| 589 | snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 590 | snd_pmac_hw_rule_rate, chip, rec->stream, -1); |
| 591 | snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 592 | snd_pmac_hw_rule_format, chip, rec->stream, -1); |
| 593 | #endif |
| 594 | |
| 595 | runtime->hw.periods_max = rec->cmd.size - 1; |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* constraints to fix choppy sound */ |
| 598 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 599 | return 0; |
| 600 | } |
| 601 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 602 | static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec, |
| 603 | struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 605 | struct pmac_stream *astr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | snd_pmac_dma_stop(rec); |
| 608 | |
| 609 | astr = snd_pmac_get_stream(chip, another_stream(rec->stream)); |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 610 | if (! astr) |
| 611 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | /* reset constraints */ |
| 614 | astr->cur_freqs = chip->freqs_ok; |
| 615 | astr->cur_formats = chip->formats_ok; |
Risto Suominen | 946cda7 | 2008-04-16 13:16:05 +0200 | [diff] [blame] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 620 | static int snd_pmac_playback_open(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 622 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | subs->runtime->hw = snd_pmac_playback; |
| 625 | return snd_pmac_pcm_open(chip, &chip->playback, subs); |
| 626 | } |
| 627 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 628 | static int snd_pmac_capture_open(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 630 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | subs->runtime->hw = snd_pmac_capture; |
| 633 | return snd_pmac_pcm_open(chip, &chip->capture, subs); |
| 634 | } |
| 635 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 636 | static int snd_pmac_playback_close(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 638 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | return snd_pmac_pcm_close(chip, &chip->playback, subs); |
| 641 | } |
| 642 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 643 | static int snd_pmac_capture_close(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 645 | struct snd_pmac *chip = snd_pcm_substream_chip(subs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | return snd_pmac_pcm_close(chip, &chip->capture, subs); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | */ |
| 652 | |
Arvind Yadav | 3179a3e | 2017-08-18 13:15:17 +0530 | [diff] [blame] | 653 | static const struct snd_pcm_ops snd_pmac_playback_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | .open = snd_pmac_playback_open, |
| 655 | .close = snd_pmac_playback_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | .prepare = snd_pmac_playback_prepare, |
| 657 | .trigger = snd_pmac_playback_trigger, |
| 658 | .pointer = snd_pmac_playback_pointer, |
| 659 | }; |
| 660 | |
Arvind Yadav | 3179a3e | 2017-08-18 13:15:17 +0530 | [diff] [blame] | 661 | static const struct snd_pcm_ops snd_pmac_capture_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | .open = snd_pmac_capture_open, |
| 663 | .close = snd_pmac_capture_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | .prepare = snd_pmac_capture_prepare, |
| 665 | .trigger = snd_pmac_capture_trigger, |
| 666 | .pointer = snd_pmac_capture_pointer, |
| 667 | }; |
| 668 | |
Bill Pemberton | 15afafc | 2012-12-06 12:35:23 -0500 | [diff] [blame] | 669 | int snd_pmac_pcm_new(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 671 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | int err; |
| 673 | int num_captures = 1; |
| 674 | |
| 675 | if (! chip->can_capture) |
| 676 | num_captures = 0; |
| 677 | err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm); |
| 678 | if (err < 0) |
| 679 | return err; |
| 680 | |
| 681 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops); |
| 682 | if (chip->can_capture) |
| 683 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops); |
| 684 | |
| 685 | pcm->private_data = chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 687 | strcpy(pcm->name, chip->card->shortname); |
| 688 | chip->pcm = pcm; |
| 689 | |
| 690 | chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE; |
| 691 | if (chip->can_byte_swap) |
| 692 | chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE; |
| 693 | |
| 694 | chip->playback.cur_formats = chip->formats_ok; |
| 695 | chip->capture.cur_formats = chip->formats_ok; |
| 696 | chip->playback.cur_freqs = chip->freqs_ok; |
| 697 | chip->capture.cur_freqs = chip->freqs_ok; |
| 698 | |
| 699 | /* preallocate 64k buffer */ |
Takashi Iwai | 01001fa | 2019-12-09 10:49:30 +0100 | [diff] [blame] | 700 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 701 | &chip->pdev->dev, |
| 702 | 64 * 1024, 64 * 1024); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 708 | static void snd_pmac_dbdma_reset(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
| 710 | out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16); |
| 711 | snd_pmac_wait_ack(&chip->playback); |
| 712 | out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16); |
| 713 | snd_pmac_wait_ack(&chip->capture); |
| 714 | } |
| 715 | |
| 716 | |
| 717 | /* |
| 718 | * handling beep |
| 719 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 720 | void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 722 | struct pmac_stream *rec = &chip->playback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | snd_pmac_dma_stop(rec); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 725 | chip->extra_dma.cmds->req_count = cpu_to_le16(bytes); |
| 726 | chip->extra_dma.cmds->xfer_status = cpu_to_le16(0); |
| 727 | chip->extra_dma.cmds->cmd_dep = cpu_to_le32(chip->extra_dma.addr); |
| 728 | chip->extra_dma.cmds->phy_addr = cpu_to_le32(addr); |
Samuel Zou | 59f0071 | 2020-05-07 11:54:07 +0800 | [diff] [blame] | 729 | chip->extra_dma.cmds->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | out_le32(&chip->awacs->control, |
| 731 | (in_le32(&chip->awacs->control) & ~0x1f00) |
| 732 | | (speed << 8)); |
| 733 | out_le32(&chip->awacs->byteswap, 0); |
| 734 | snd_pmac_dma_set_command(rec, &chip->extra_dma); |
| 735 | snd_pmac_dma_run(rec, RUN); |
| 736 | } |
| 737 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 738 | void snd_pmac_beep_dma_stop(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | snd_pmac_dma_stop(&chip->playback); |
David Gibson | f571872 | 2015-02-03 16:36:21 +1100 | [diff] [blame] | 741 | chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | snd_pmac_pcm_set_format(chip); /* reset format */ |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /* |
| 747 | * interrupt handlers |
| 748 | */ |
| 749 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 750 | snd_pmac_tx_intr(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 752 | struct snd_pmac *chip = devid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | snd_pmac_pcm_update(chip, &chip->playback); |
| 754 | return IRQ_HANDLED; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 759 | snd_pmac_rx_intr(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 761 | struct snd_pmac *chip = devid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | snd_pmac_pcm_update(chip, &chip->capture); |
| 763 | return IRQ_HANDLED; |
| 764 | } |
| 765 | |
| 766 | |
| 767 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 768 | snd_pmac_ctrl_intr(int irq, void *devid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 770 | struct snd_pmac *chip = devid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | int ctrl = in_le32(&chip->awacs->control); |
| 772 | |
Takashi Iwai | 6da6711 | 2009-02-05 16:02:46 +0100 | [diff] [blame] | 773 | /*printk(KERN_DEBUG "pmac: control interrupt.. 0x%x\n", ctrl);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (ctrl & MASK_PORTCHG) { |
| 775 | /* do something when headphone is plugged/unplugged? */ |
| 776 | if (chip->update_automute) |
| 777 | chip->update_automute(chip, 1); |
| 778 | } |
| 779 | if (ctrl & MASK_CNTLERR) { |
| 780 | int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16; |
| 781 | if (err && chip->model <= PMAC_SCREAMER) |
| 782 | snd_printk(KERN_DEBUG "error %x\n", err); |
| 783 | } |
| 784 | /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */ |
| 785 | out_le32(&chip->awacs->control, ctrl); |
| 786 | return IRQ_HANDLED; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /* |
| 791 | * a wrapper to feature call for compatibility |
| 792 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 793 | static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
David Woodhouse | 4e6a06e | 2005-08-17 11:36:35 +0100 | [diff] [blame] | 795 | if (ppc_md.feature_call) |
| 796 | ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * release resources |
| 801 | */ |
| 802 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 803 | static int snd_pmac_free(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* stop sounds */ |
| 806 | if (chip->initialized) { |
| 807 | snd_pmac_dbdma_reset(chip); |
| 808 | /* disable interrupts from awacs interface */ |
| 809 | out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff); |
| 810 | } |
| 811 | |
Benjamin Herrenschmidt | 41e904d | 2007-06-19 14:37:39 +1000 | [diff] [blame] | 812 | if (chip->node) |
| 813 | snd_pmac_sound_feature(chip, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | /* clean up mixer if any */ |
| 816 | if (chip->mixer_free) |
| 817 | chip->mixer_free(chip); |
| 818 | |
| 819 | snd_pmac_detach_beep(chip); |
| 820 | |
| 821 | /* release resources */ |
| 822 | if (chip->irq >= 0) |
| 823 | free_irq(chip->irq, (void*)chip); |
| 824 | if (chip->tx_irq >= 0) |
| 825 | free_irq(chip->tx_irq, (void*)chip); |
| 826 | if (chip->rx_irq >= 0) |
| 827 | free_irq(chip->rx_irq, (void*)chip); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 828 | snd_pmac_dbdma_free(chip, &chip->playback.cmd); |
| 829 | snd_pmac_dbdma_free(chip, &chip->capture.cmd); |
| 830 | snd_pmac_dbdma_free(chip, &chip->extra_dma); |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 831 | snd_pmac_dbdma_free(chip, &emergency_dbdma); |
Markus Elfring | ff6defa | 2015-01-03 22:55:54 +0100 | [diff] [blame] | 832 | iounmap(chip->macio_base); |
| 833 | iounmap(chip->latch_base); |
| 834 | iounmap(chip->awacs); |
| 835 | iounmap(chip->playback.dma); |
| 836 | iounmap(chip->capture.dma); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (chip->node) { |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 839 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | for (i = 0; i < 3; i++) { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 841 | if (chip->requested & (1 << i)) |
| 842 | release_mem_region(chip->rsrc[i].start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 843 | resource_size(&chip->rsrc[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 846 | |
Markus Elfring | 1ea7a56 | 2014-11-17 13:35:54 +0100 | [diff] [blame] | 847 | pci_dev_put(chip->pdev); |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 848 | of_node_put(chip->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | kfree(chip); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /* |
| 855 | * free the device |
| 856 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 857 | static int snd_pmac_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 859 | struct snd_pmac *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | return snd_pmac_free(chip); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /* |
| 865 | * check the machine support byteswap (little-endian) |
| 866 | */ |
| 867 | |
Bill Pemberton | 15afafc | 2012-12-06 12:35:23 -0500 | [diff] [blame] | 868 | static void detect_byte_swap(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
| 870 | struct device_node *mio; |
| 871 | |
| 872 | /* if seems that Keylargo can't byte-swap */ |
| 873 | for (mio = chip->node->parent; mio; mio = mio->parent) { |
Rob Herring | 157ab88 | 2018-12-05 13:50:46 -0600 | [diff] [blame] | 874 | if (of_node_name_eq(mio, "mac-io")) { |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 875 | if (of_device_is_compatible(mio, "Keylargo")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | chip->can_byte_swap = 0; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /* it seems the Pismo & iBook can't byte-swap in hardware. */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 882 | if (of_machine_is_compatible("PowerBook3,1") || |
| 883 | of_machine_is_compatible("PowerBook2,1")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | chip->can_byte_swap = 0 ; |
| 885 | |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 886 | if (of_machine_is_compatible("PowerBook2,1")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | chip->can_duplex = 0; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | /* |
| 892 | * detect a sound chip |
| 893 | */ |
Bill Pemberton | 15afafc | 2012-12-06 12:35:23 -0500 | [diff] [blame] | 894 | static int snd_pmac_detect(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 896 | struct device_node *sound; |
| 897 | struct device_node *dn; |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 898 | const unsigned int *prop; |
| 899 | unsigned int l; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 900 | struct macio_chip* macio; |
| 901 | |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 902 | if (!machine_is(powermac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return -ENODEV; |
| 904 | |
| 905 | chip->subframe = 0; |
| 906 | chip->revision = 0; |
| 907 | chip->freqs_ok = 0xff; /* all ok */ |
| 908 | chip->model = PMAC_AWACS; |
| 909 | chip->can_byte_swap = 1; |
| 910 | chip->can_duplex = 1; |
| 911 | chip->can_capture = 1; |
| 912 | chip->num_freqs = ARRAY_SIZE(awacs_freqs); |
| 913 | chip->freq_table = awacs_freqs; |
Benjamin Herrenschmidt | 367636e | 2006-02-08 15:04:18 +1100 | [diff] [blame] | 914 | chip->pdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */ |
| 917 | |
| 918 | /* check machine type */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 919 | if (of_machine_is_compatible("AAPL,3400/2400") |
| 920 | || of_machine_is_compatible("AAPL,3500")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | chip->is_pbook_3400 = 1; |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 922 | else if (of_machine_is_compatible("PowerBook1,1") |
| 923 | || of_machine_is_compatible("AAPL,PowerBook1998")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | chip->is_pbook_G3 = 1; |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 925 | chip->node = of_find_node_by_name(NULL, "awacs"); |
| 926 | sound = of_node_get(chip->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
| 928 | /* |
| 929 | * powermac G3 models have a node called "davbus" |
| 930 | * with a child called "sound". |
| 931 | */ |
Benjamin Herrenschmidt | 5218064 | 2005-05-18 16:31:51 +0200 | [diff] [blame] | 932 | if (!chip->node) |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 933 | chip->node = of_find_node_by_name(NULL, "davbus"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | /* |
| 935 | * if we didn't find a davbus device, try 'i2s-a' since |
| 936 | * this seems to be what iBooks have |
| 937 | */ |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 938 | if (! chip->node) { |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 939 | chip->node = of_find_node_by_name(NULL, "i2s-a"); |
Benjamin Herrenschmidt | 5218064 | 2005-05-18 16:31:51 +0200 | [diff] [blame] | 940 | if (chip->node && chip->node->parent && |
| 941 | chip->node->parent->parent) { |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 942 | if (of_device_is_compatible(chip->node->parent->parent, |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 943 | "K2-Keylargo")) |
| 944 | chip->is_k2 = 1; |
| 945 | } |
| 946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | if (! chip->node) |
| 948 | return -ENODEV; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 949 | |
Benjamin Herrenschmidt | 5218064 | 2005-05-18 16:31:51 +0200 | [diff] [blame] | 950 | if (!sound) { |
Grant Likely | ccdb8ed | 2014-06-04 16:42:26 +0100 | [diff] [blame] | 951 | for_each_node_by_name(sound, "sound") |
| 952 | if (sound->parent == chip->node) |
| 953 | break; |
Benjamin Herrenschmidt | 5218064 | 2005-05-18 16:31:51 +0200 | [diff] [blame] | 954 | } |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 955 | if (! sound) { |
| 956 | of_node_put(chip->node); |
Benjamin Herrenschmidt | 41e904d | 2007-06-19 14:37:39 +1000 | [diff] [blame] | 957 | chip->node = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | return -ENODEV; |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 959 | } |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 960 | prop = of_get_property(sound, "sub-frame", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | if (prop && *prop < 16) |
| 962 | chip->subframe = *prop; |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 963 | prop = of_get_property(sound, "layout-id", NULL); |
Johannes Berg | 55c385a | 2006-06-21 15:43:44 +0200 | [diff] [blame] | 964 | if (prop) { |
| 965 | /* partly deprecate snd-powermac, for those machines |
| 966 | * that have a layout-id property for now */ |
| 967 | printk(KERN_INFO "snd-powermac no longer handles any " |
| 968 | "machines with a layout-id property " |
| 969 | "in the device-tree, use snd-aoa.\n"); |
Benjamin Herrenschmidt | 41e904d | 2007-06-19 14:37:39 +1000 | [diff] [blame] | 970 | of_node_put(sound); |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 971 | of_node_put(chip->node); |
Benjamin Herrenschmidt | 41e904d | 2007-06-19 14:37:39 +1000 | [diff] [blame] | 972 | chip->node = NULL; |
Johannes Berg | 55c385a | 2006-06-21 15:43:44 +0200 | [diff] [blame] | 973 | return -ENODEV; |
| 974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* This should be verified on older screamers */ |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 976 | if (of_device_is_compatible(sound, "screamer")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | chip->model = PMAC_SCREAMER; |
| 978 | // chip->can_byte_swap = 0; /* FIXME: check this */ |
| 979 | } |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 980 | if (of_device_is_compatible(sound, "burgundy")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | chip->model = PMAC_BURGUNDY; |
| 982 | chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ |
| 983 | } |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 984 | if (of_device_is_compatible(sound, "daca")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | chip->model = PMAC_DACA; |
| 986 | chip->can_capture = 0; /* no capture */ |
| 987 | chip->can_duplex = 0; |
| 988 | // chip->can_byte_swap = 0; /* FIXME: check this */ |
| 989 | chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ |
| 990 | } |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 991 | if (of_device_is_compatible(sound, "tumbler")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | chip->model = PMAC_TUMBLER; |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 993 | chip->can_capture = of_machine_is_compatible("PowerMac4,2") |
Risto Suominen | 8460ae7 | 2011-02-28 10:38:45 +0200 | [diff] [blame] | 994 | || of_machine_is_compatible("PowerBook3,2") |
| 995 | || of_machine_is_compatible("PowerBook3,3") |
| 996 | || of_machine_is_compatible("PowerBook4,1") |
| 997 | || of_machine_is_compatible("PowerBook4,2") |
| 998 | || of_machine_is_compatible("PowerBook4,3"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | 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 | } |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 1005 | if (of_device_is_compatible(sound, "snapper")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 1012 | prop = of_get_property(sound, "device-id", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | if (prop) |
| 1014 | chip->device_id = *prop; |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 1015 | dn = of_find_node_by_name(NULL, "perch"); |
| 1016 | chip->has_iic = (dn != NULL); |
| 1017 | of_node_put(dn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 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) |
Benjamin Herrenschmidt | 5218064 | 2005-05-18 16:31:51 +0200 | [diff] [blame] | 1037 | printk(KERN_WARNING "snd-powermac: can't locate macio PCI" |
| 1038 | " device !\n"); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | detect_byte_swap(chip); |
| 1041 | |
| 1042 | /* look for a property saying what sample rates |
| 1043 | are available */ |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 1044 | prop = of_get_property(sound, "sample-rates", &l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (! prop) |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 1046 | prop = of_get_property(sound, "output-frame-rates", &l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 1067 | of_node_put(sound); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | #ifdef PMAC_SUPPORT_AUTOMUTE |
| 1072 | /* |
| 1073 | * auto-mute |
| 1074 | */ |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1075 | static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol, |
| 1076 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1078 | struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | ucontrol->value.integer.value[0] = chip->auto_mute; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1083 | static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol, |
| 1084 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1086 | struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (ucontrol->value.integer.value[0] != chip->auto_mute) { |
Takashi Iwai | d4079ac | 2007-11-15 16:14:12 +0100 | [diff] [blame] | 1088 | chip->auto_mute = !!ucontrol->value.integer.value[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (chip->update_automute) |
| 1090 | chip->update_automute(chip, 1); |
| 1091 | return 1; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1096 | static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol, |
| 1097 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1099 | struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | c031b0c | 2020-01-03 09:16:54 +0100 | [diff] [blame] | 1107 | static const struct snd_kcontrol_new auto_mute_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bill Pemberton | 15afafc | 2012-12-06 12:35:23 -0500 | [diff] [blame] | 1122 | int snd_pmac_add_automute(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | { |
| 1124 | int err; |
| 1125 | chip->auto_mute = 1; |
| 1126 | err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip)); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1127 | if (err < 0) { |
| 1128 | printk(KERN_ERR "snd-powermac: Failed to add automute control\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | return err; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Bill Pemberton | 15afafc | 2012-12-06 12:35:23 -0500 | [diff] [blame] | 1139 | int snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 1141 | struct snd_pmac *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | struct device_node *np; |
| 1143 | int i, err; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1144 | unsigned int irq; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1145 | unsigned long ctrl_addr, txdma_addr, rxdma_addr; |
Takashi Iwai | 46dd47c | 2020-01-03 09:16:31 +0100 | [diff] [blame] | 1146 | static const struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | .dev_free = snd_pmac_dev_free, |
| 1148 | }; |
| 1149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | *chip_return = NULL; |
| 1151 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1152 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | e73ad38 | 2021-06-08 16:05:38 +0200 | [diff] [blame] | 1163 | err = snd_pmac_detect(chip); |
| 1164 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | goto __error; |
| 1166 | |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 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 || |
T. H. Huth | e70515dd5 | 2008-01-16 15:57:08 +0100 | [diff] [blame] | 1169 | snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 || |
| 1170 | snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | err = -ENOMEM; |
| 1172 | goto __error; |
| 1173 | } |
| 1174 | |
| 1175 | np = chip->node; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1176 | chip->requested = 0; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1177 | if (chip->is_k2) { |
Takashi Iwai | 6e9ef32 | 2020-01-05 15:47:32 +0100 | [diff] [blame] | 1178 | static const char * const rnames[] = { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1179 | "Sound Control", "Sound DMA" }; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 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]); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1185 | err = -ENODEV; |
| 1186 | goto __error; |
| 1187 | } |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1188 | if (request_mem_region(chip->rsrc[i].start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1189 | resource_size(&chip->rsrc[i]), |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1190 | rnames[i]) == NULL) { |
| 1191 | printk(KERN_ERR "snd: can't request rsrc " |
Joe Perches | 2fb50f1 | 2010-11-12 13:38:04 -0800 | [diff] [blame] | 1192 | " %d (%s: %pR)\n", |
| 1193 | i, rnames[i], &chip->rsrc[i]); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1194 | err = -ENODEV; |
| 1195 | goto __error; |
| 1196 | } |
| 1197 | chip->requested |= (1 << i); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1198 | } |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1199 | ctrl_addr = chip->rsrc[0].start; |
| 1200 | txdma_addr = chip->rsrc[1].start; |
| 1201 | rxdma_addr = txdma_addr + 0x100; |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1202 | } else { |
Takashi Iwai | 6e9ef32 | 2020-01-05 15:47:32 +0100 | [diff] [blame] | 1203 | static const char * const rnames[] = { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1204 | "Sound Control", "Sound Tx DMA", "Sound Rx DMA" }; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1205 | for (i = 0; i < 3; i ++) { |
Benjamin Herrenschmidt | 88356e9 | 2006-02-01 03:04:43 -0800 | [diff] [blame] | 1206 | if (of_address_to_resource(np, i, |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1207 | &chip->rsrc[i])) { |
| 1208 | printk(KERN_ERR "snd: can't translate rsrc " |
| 1209 | " %d (%s)\n", i, rnames[i]); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1210 | err = -ENODEV; |
| 1211 | goto __error; |
| 1212 | } |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1213 | if (request_mem_region(chip->rsrc[i].start, |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1214 | resource_size(&chip->rsrc[i]), |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1215 | rnames[i]) == NULL) { |
| 1216 | printk(KERN_ERR "snd: can't request rsrc " |
Joe Perches | 2fb50f1 | 2010-11-12 13:38:04 -0800 | [diff] [blame] | 1217 | " %d (%s: %pR)\n", |
| 1218 | i, rnames[i], &chip->rsrc[i]); |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1219 | err = -ENODEV; |
| 1220 | goto __error; |
| 1221 | } |
| 1222 | chip->requested |= (1 << i); |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1223 | } |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1224 | ctrl_addr = chip->rsrc[0].start; |
| 1225 | txdma_addr = chip->rsrc[1].start; |
| 1226 | rxdma_addr = chip->rsrc[2].start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 1229 | chip->awacs = ioremap(ctrl_addr, 0x1000); |
| 1230 | chip->playback.dma = ioremap(txdma_addr, 0x100); |
| 1231 | chip->capture.dma = ioremap(rxdma_addr, 0x100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (chip->model <= PMAC_BURGUNDY) { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1233 | irq = irq_of_parse_and_map(np, 0); |
| 1234 | if (request_irq(irq, snd_pmac_ctrl_intr, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | "PMac", (void*)chip)) { |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1236 | snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", |
| 1237 | irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | err = -EBUSY; |
| 1239 | goto __error; |
| 1240 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1241 | chip->irq = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | err = -EBUSY; |
| 1247 | goto __error; |
| 1248 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | err = -EBUSY; |
| 1254 | goto __error; |
| 1255 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 1256 | chip->rx_irq = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | snd_pmac_sound_feature(chip, 1); |
| 1259 | |
Risto Suominen | 9a4f20f | 2008-04-16 13:15:38 +0200 | [diff] [blame] | 1260 | /* reset & enable interrupts */ |
| 1261 | if (chip->model <= PMAC_BURGUNDY) |
| 1262 | out_le32(&chip->awacs->control, chip->control_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Rob Herring | 157ab88 | 2018-12-05 13:50:46 -0600 | [diff] [blame] | 1280 | if (of_node_name_eq(mio, "mac-io")) { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 1281 | struct resource r; |
| 1282 | if (of_address_to_resource(mio, 0, &r) == 0) |
| 1283 | chip->macio_base = |
| 1284 | ioremap(r.start, 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | e73ad38 | 2021-06-08 16:05:38 +0200 | [diff] [blame] | 1303 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); |
| 1304 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | goto __error; |
| 1306 | |
| 1307 | *chip_return = chip; |
| 1308 | return 0; |
| 1309 | |
| 1310 | __error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | snd_pmac_free(chip); |
| 1312 | return err; |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /* |
| 1317 | * sleep notify for powerbook |
| 1318 | */ |
| 1319 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 1320 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * Save state when going to sleep, restore it afterwards. |
| 1324 | */ |
| 1325 | |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1326 | void snd_pmac_suspend(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | unsigned long flags; |
| 1329 | |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1330 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | if (chip->suspend) |
| 1332 | chip->suspend(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1345 | void snd_pmac_resume(struct snd_pmac *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | snd_pmac_sound_feature(chip, 1); |
| 1348 | if (chip->resume) |
| 1349 | chip->resume(chip); |
| 1350 | /* enable CD sound input */ |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1351 | if (chip->macio_base && chip->is_pbook_G3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | out_8(chip->macio_base + 0x37, 3); |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1353 | else if (chip->is_pbook_3400) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | in_8(chip->latch_base + 0x190); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 5e12bea | 2005-11-17 17:17:08 +0100 | [diff] [blame] | 1365 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Benjamin Herrenschmidt | 8c87093 | 2005-06-27 14:36:34 -0700 | [diff] [blame] | 1368 | #endif /* CONFIG_PM */ |
| 1369 | |