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