Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BRIEF MODULE DESCRIPTION |
| 3 | * Driver for AMD Au1000 MIPS Processor, AC'97 Sound Port |
| 4 | * |
| 5 | * Copyright 2004 Cooper Street Innovations Inc. |
| 6 | * Author: Charles Eidsness <charles@cooper-street.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 14 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 15 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 16 | * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 19 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License along |
| 25 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 26 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | * |
| 28 | * History: |
| 29 | * |
| 30 | * 2004-09-09 Charles Eidsness -- Original verion -- based on |
| 31 | * sa11xx-uda1341.c ALSA driver and the |
| 32 | * au1000.c OSS driver. |
| 33 | * 2004-09-09 Matt Porter -- Added support for ALSA 1.0.6 |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <linux/ioport.h> |
| 38 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/init.h> |
| 40 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <sound/core.h> |
| 42 | #include <sound/initval.h> |
| 43 | #include <sound/pcm.h> |
Sergei Shtylylov | d8327c7 | 2006-03-20 18:38:21 +0100 | [diff] [blame] | 44 | #include <sound/pcm_params.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <sound/ac97_codec.h> |
| 46 | #include <asm/mach-au1x00/au1000.h> |
| 47 | #include <asm/mach-au1x00/au1000_dma.h> |
| 48 | |
| 49 | MODULE_AUTHOR("Charles Eidsness <charles@cooper-street.com>"); |
| 50 | MODULE_DESCRIPTION("Au1000 AC'97 ALSA Driver"); |
| 51 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | MODULE_SUPPORTED_DEVICE("{{AMD,Au1000 AC'97}}"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define PLAYBACK 0 |
| 55 | #define CAPTURE 1 |
| 56 | #define AC97_SLOT_3 0x01 |
| 57 | #define AC97_SLOT_4 0x02 |
| 58 | #define AC97_SLOT_6 0x08 |
| 59 | #define AC97_CMD_IRQ 31 |
| 60 | #define READ 0 |
| 61 | #define WRITE 1 |
| 62 | #define READ_WAIT 2 |
| 63 | #define RW_DONE 3 |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | struct au1000_period |
| 66 | { |
| 67 | u32 start; |
| 68 | u32 relative_end; /*realtive to start of buffer*/ |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 69 | struct au1000_period * next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /*Au1000 AC97 Port Control Reisters*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct au1000_ac97_reg { |
| 74 | u32 volatile config; |
| 75 | u32 volatile status; |
| 76 | u32 volatile data; |
| 77 | u32 volatile cmd; |
| 78 | u32 volatile cntrl; |
| 79 | }; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct audio_stream { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 82 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int dma; |
| 84 | spinlock_t dma_lock; |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 85 | struct au1000_period * buffer; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 86 | unsigned int period_size; |
| 87 | unsigned int periods; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 90 | struct snd_au1000 { |
| 91 | struct snd_card *card; |
| 92 | struct au1000_ac97_reg volatile *ac97_ioport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | struct resource *ac97_res_port; |
| 95 | spinlock_t ac97_lock; |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 96 | struct snd_ac97 *ac97; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 98 | struct snd_pcm *pcm; |
| 99 | struct audio_stream *stream[2]; /* playback & capture */ |
| 100 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /*--------------------------- Local Functions --------------------------------*/ |
| 103 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 104 | au1000_set_ac97_xmit_slots(struct snd_au1000 *au1000, long xmit_slots) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | u32 volatile ac97_config; |
| 107 | |
| 108 | spin_lock(&au1000->ac97_lock); |
| 109 | ac97_config = au1000->ac97_ioport->config; |
| 110 | ac97_config = ac97_config & ~AC97C_XMIT_SLOTS_MASK; |
| 111 | ac97_config |= (xmit_slots << AC97C_XMIT_SLOTS_BIT); |
| 112 | au1000->ac97_ioport->config = ac97_config; |
| 113 | spin_unlock(&au1000->ac97_lock); |
| 114 | } |
| 115 | |
| 116 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 117 | au1000_set_ac97_recv_slots(struct snd_au1000 *au1000, long recv_slots) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | u32 volatile ac97_config; |
| 120 | |
| 121 | spin_lock(&au1000->ac97_lock); |
| 122 | ac97_config = au1000->ac97_ioport->config; |
| 123 | ac97_config = ac97_config & ~AC97C_RECV_SLOTS_MASK; |
| 124 | ac97_config |= (recv_slots << AC97C_RECV_SLOTS_BIT); |
| 125 | au1000->ac97_ioport->config = ac97_config; |
| 126 | spin_unlock(&au1000->ac97_lock); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 131 | au1000_release_dma_link(struct audio_stream *stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 133 | struct au1000_period * pointer; |
| 134 | struct au1000_period * pointer_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 136 | stream->period_size = 0; |
| 137 | stream->periods = 0; |
| 138 | pointer = stream->buffer; |
| 139 | if (! pointer) |
| 140 | return; |
| 141 | do { |
| 142 | pointer_next = pointer->next; |
| 143 | kfree(pointer); |
| 144 | pointer = pointer_next; |
| 145 | } while (pointer != stream->buffer); |
| 146 | stream->buffer = NULL; |
| 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 149 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 150 | au1000_setup_dma_link(struct audio_stream *stream, unsigned int period_bytes, |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 151 | unsigned int periods) |
| 152 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 153 | struct snd_pcm_substream *substream = stream->substream; |
| 154 | struct snd_pcm_runtime *runtime = substream->runtime; |
Sergei Shtylylov | d8327c7 | 2006-03-20 18:38:21 +0100 | [diff] [blame] | 155 | struct au1000_period *pointer; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 156 | unsigned long dma_start; |
| 157 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 159 | dma_start = virt_to_phys(runtime->dma_area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 161 | if (stream->period_size == period_bytes && |
| 162 | stream->periods == periods) |
| 163 | return 0; /* not changed */ |
| 164 | |
| 165 | au1000_release_dma_link(stream); |
| 166 | |
| 167 | stream->period_size = period_bytes; |
| 168 | stream->periods = periods; |
| 169 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 170 | stream->buffer = kmalloc(sizeof(struct au1000_period), GFP_KERNEL); |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 171 | if (! stream->buffer) |
| 172 | return -ENOMEM; |
| 173 | pointer = stream->buffer; |
| 174 | for (i = 0; i < periods; i++) { |
| 175 | pointer->start = (u32)(dma_start + (i * period_bytes)); |
| 176 | pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1); |
| 177 | if (i < periods - 1) { |
| 178 | pointer->next = kmalloc(sizeof(struct au1000_period), GFP_KERNEL); |
| 179 | if (! pointer->next) { |
| 180 | au1000_release_dma_link(stream); |
| 181 | return -ENOMEM; |
| 182 | } |
| 183 | pointer = pointer->next; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 186 | pointer->next = stream->buffer; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 191 | au1000_dma_stop(struct audio_stream *stream) |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 192 | { |
| 193 | snd_assert(stream->buffer, return); |
| 194 | disable_dma(stream->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 198 | au1000_dma_start(struct audio_stream *stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 200 | snd_assert(stream->buffer, return); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 202 | init_dma(stream->dma); |
| 203 | if (get_dma_active_buffer(stream->dma) == 0) { |
| 204 | clear_dma_done0(stream->dma); |
| 205 | set_dma_addr0(stream->dma, stream->buffer->start); |
| 206 | set_dma_count0(stream->dma, stream->period_size >> 1); |
| 207 | set_dma_addr1(stream->dma, stream->buffer->next->start); |
| 208 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 209 | } else { |
| 210 | clear_dma_done1(stream->dma); |
| 211 | set_dma_addr1(stream->dma, stream->buffer->start); |
| 212 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 213 | set_dma_addr0(stream->dma, stream->buffer->next->start); |
| 214 | set_dma_count0(stream->dma, stream->period_size >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 216 | enable_dma_buffers(stream->dma); |
| 217 | start_dma(stream->dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 221 | au1000_dma_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 223 | struct audio_stream *stream = (struct audio_stream *) dev_id; |
| 224 | struct snd_pcm_substream *substream = stream->substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | spin_lock(&stream->dma_lock); |
| 227 | switch (get_dma_buffer_done(stream->dma)) { |
| 228 | case DMA_D0: |
| 229 | stream->buffer = stream->buffer->next; |
| 230 | clear_dma_done0(stream->dma); |
| 231 | set_dma_addr0(stream->dma, stream->buffer->next->start); |
| 232 | set_dma_count0(stream->dma, stream->period_size >> 1); |
| 233 | enable_dma_buffer0(stream->dma); |
| 234 | break; |
| 235 | case DMA_D1: |
| 236 | stream->buffer = stream->buffer->next; |
| 237 | clear_dma_done1(stream->dma); |
| 238 | set_dma_addr1(stream->dma, stream->buffer->next->start); |
| 239 | set_dma_count1(stream->dma, stream->period_size >> 1); |
| 240 | enable_dma_buffer1(stream->dma); |
| 241 | break; |
| 242 | case (DMA_D0 | DMA_D1): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | printk(KERN_ERR "DMA %d missed interrupt.\n",stream->dma); |
| 244 | au1000_dma_stop(stream); |
| 245 | au1000_dma_start(stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | break; |
| 247 | case (~DMA_D0 & ~DMA_D1): |
| 248 | printk(KERN_ERR "DMA %d empty irq.\n",stream->dma); |
| 249 | } |
| 250 | spin_unlock(&stream->dma_lock); |
| 251 | snd_pcm_period_elapsed(substream); |
| 252 | return IRQ_HANDLED; |
| 253 | } |
| 254 | |
| 255 | /*-------------------------- PCM Audio Streams -------------------------------*/ |
| 256 | |
| 257 | static unsigned int rates[] = {8000, 11025, 16000, 22050}; |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 258 | static struct snd_pcm_hw_constraint_list hw_constraints_rates = { |
Tobias Klauser | 2ad3479 | 2006-09-29 02:00:20 -0700 | [diff] [blame] | 259 | .count = ARRAY_SIZE(rates), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | .list = rates, |
| 261 | .mask = 0, |
| 262 | }; |
| 263 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 264 | static struct snd_pcm_hardware snd_au1000_hw = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | .info = (SNDRV_PCM_INFO_INTERLEAVED | \ |
| 267 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID), |
| 268 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 269 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | |
| 270 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050), |
| 271 | .rate_min = 8000, |
| 272 | .rate_max = 22050, |
| 273 | .channels_min = 1, |
| 274 | .channels_max = 2, |
| 275 | .buffer_bytes_max = 128*1024, |
| 276 | .period_bytes_min = 32, |
| 277 | .period_bytes_max = 16*1024, |
| 278 | .periods_min = 8, |
| 279 | .periods_max = 255, |
| 280 | .fifo_size = 16, |
| 281 | }; |
| 282 | |
| 283 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 284 | snd_au1000_playback_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 286 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | au1000->stream[PLAYBACK]->substream = substream; |
| 289 | au1000->stream[PLAYBACK]->buffer = NULL; |
| 290 | substream->private_data = au1000->stream[PLAYBACK]; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 291 | substream->runtime->hw = snd_au1000_hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return (snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 293 | SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0); |
| 294 | } |
| 295 | |
| 296 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 297 | snd_au1000_capture_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 299 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | au1000->stream[CAPTURE]->substream = substream; |
| 302 | au1000->stream[CAPTURE]->buffer = NULL; |
| 303 | substream->private_data = au1000->stream[CAPTURE]; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 304 | substream->runtime->hw = snd_au1000_hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return (snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 306 | SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates) < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 310 | snd_au1000_playback_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 312 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | au1000->stream[PLAYBACK]->substream = NULL; |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 319 | snd_au1000_capture_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 321 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | au1000->stream[CAPTURE]->substream = NULL; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 328 | snd_au1000_hw_params(struct snd_pcm_substream *substream, |
| 329 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 331 | struct audio_stream *stream = substream->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 332 | int err; |
| 333 | |
| 334 | err = snd_pcm_lib_malloc_pages(substream, |
| 335 | params_buffer_bytes(hw_params)); |
| 336 | if (err < 0) |
| 337 | return err; |
| 338 | return au1000_setup_dma_link(stream, |
| 339 | params_period_bytes(hw_params), |
| 340 | params_periods(hw_params)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 344 | snd_au1000_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 346 | struct audio_stream *stream = substream->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 347 | au1000_release_dma_link(stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return snd_pcm_lib_free_pages(substream); |
| 349 | } |
| 350 | |
| 351 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 352 | snd_au1000_playback_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 354 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
| 355 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 357 | if (runtime->channels == 1) |
| 358 | au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | else |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 360 | au1000_set_ac97_xmit_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | snd_ac97_set_rate(au1000->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 366 | snd_au1000_capture_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 368 | struct snd_au1000 *au1000 = substream->pcm->private_data; |
| 369 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 371 | if (runtime->channels == 1) |
| 372 | au1000_set_ac97_recv_slots(au1000, AC97_SLOT_4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | else |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 374 | au1000_set_ac97_recv_slots(au1000, AC97_SLOT_3 | AC97_SLOT_4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | snd_ac97_set_rate(au1000->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 380 | snd_au1000_trigger(struct snd_pcm_substream *substream, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 382 | struct audio_stream *stream = substream->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | int err = 0; |
| 384 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 385 | spin_lock(&stream->dma_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | switch (cmd) { |
| 387 | case SNDRV_PCM_TRIGGER_START: |
| 388 | au1000_dma_start(stream); |
| 389 | break; |
| 390 | case SNDRV_PCM_TRIGGER_STOP: |
| 391 | au1000_dma_stop(stream); |
| 392 | break; |
| 393 | default: |
| 394 | err = -EINVAL; |
| 395 | break; |
| 396 | } |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 397 | spin_unlock(&stream->dma_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return err; |
| 399 | } |
| 400 | |
| 401 | static snd_pcm_uframes_t |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 402 | snd_au1000_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 404 | struct audio_stream *stream = substream->private_data; |
| 405 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | long location; |
| 407 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 408 | spin_lock(&stream->dma_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | location = get_dma_residue(stream->dma); |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 410 | spin_unlock(&stream->dma_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | location = stream->buffer->relative_end - location; |
| 412 | if (location == -1) |
| 413 | location = 0; |
| 414 | return bytes_to_frames(runtime,location); |
| 415 | } |
| 416 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 417 | static struct snd_pcm_ops snd_card_au1000_playback_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | .open = snd_au1000_playback_open, |
| 419 | .close = snd_au1000_playback_close, |
| 420 | .ioctl = snd_pcm_lib_ioctl, |
| 421 | .hw_params = snd_au1000_hw_params, |
| 422 | .hw_free = snd_au1000_hw_free, |
| 423 | .prepare = snd_au1000_playback_prepare, |
| 424 | .trigger = snd_au1000_trigger, |
| 425 | .pointer = snd_au1000_pointer, |
| 426 | }; |
| 427 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 428 | static struct snd_pcm_ops snd_card_au1000_capture_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | .open = snd_au1000_capture_open, |
| 430 | .close = snd_au1000_capture_close, |
| 431 | .ioctl = snd_pcm_lib_ioctl, |
| 432 | .hw_params = snd_au1000_hw_params, |
| 433 | .hw_free = snd_au1000_hw_free, |
| 434 | .prepare = snd_au1000_capture_prepare, |
| 435 | .trigger = snd_au1000_trigger, |
| 436 | .pointer = snd_au1000_pointer, |
| 437 | }; |
| 438 | |
| 439 | static int __devinit |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 440 | snd_au1000_pcm_new(struct snd_au1000 *au1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 442 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | int err; |
| 444 | unsigned long flags; |
| 445 | |
| 446 | if ((err = snd_pcm_new(au1000->card, "AU1000 AC97 PCM", 0, 1, 1, &pcm)) < 0) |
| 447 | return err; |
| 448 | |
| 449 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| 450 | snd_dma_continuous_data(GFP_KERNEL), 128*1024, 128*1024); |
| 451 | |
| 452 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 453 | &snd_card_au1000_playback_ops); |
| 454 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, |
| 455 | &snd_card_au1000_capture_ops); |
| 456 | |
| 457 | pcm->private_data = au1000; |
| 458 | pcm->info_flags = 0; |
| 459 | strcpy(pcm->name, "Au1000 AC97 PCM"); |
| 460 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 461 | spin_lock_init(&au1000->stream[PLAYBACK]->dma_lock); |
| 462 | spin_lock_init(&au1000->stream[CAPTURE]->dma_lock); |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | flags = claim_dma_lock(); |
| 465 | if ((au1000->stream[PLAYBACK]->dma = request_au1000_dma(DMA_ID_AC97C_TX, |
Thomas Gleixner | 65ca68b | 2006-07-01 19:29:46 -0700 | [diff] [blame] | 466 | "AC97 TX", au1000_dma_interrupt, IRQF_DISABLED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | au1000->stream[PLAYBACK])) < 0) { |
| 468 | release_dma_lock(flags); |
| 469 | return -EBUSY; |
| 470 | } |
| 471 | if ((au1000->stream[CAPTURE]->dma = request_au1000_dma(DMA_ID_AC97C_RX, |
Thomas Gleixner | 65ca68b | 2006-07-01 19:29:46 -0700 | [diff] [blame] | 472 | "AC97 RX", au1000_dma_interrupt, IRQF_DISABLED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | au1000->stream[CAPTURE])) < 0){ |
| 474 | release_dma_lock(flags); |
| 475 | return -EBUSY; |
| 476 | } |
| 477 | /* enable DMA coherency in read/write DMA channels */ |
| 478 | set_dma_mode(au1000->stream[PLAYBACK]->dma, |
| 479 | get_dma_mode(au1000->stream[PLAYBACK]->dma) & ~DMA_NC); |
| 480 | set_dma_mode(au1000->stream[CAPTURE]->dma, |
| 481 | get_dma_mode(au1000->stream[CAPTURE]->dma) & ~DMA_NC); |
| 482 | release_dma_lock(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | au1000->pcm = pcm; |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | /*-------------------------- AC97 CODEC Control ------------------------------*/ |
| 489 | |
| 490 | static unsigned short |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 491 | snd_au1000_ac97_read(struct snd_ac97 *ac97, unsigned short reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 493 | struct snd_au1000 *au1000 = ac97->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | u32 volatile cmd; |
| 495 | u16 volatile data; |
| 496 | int i; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 497 | |
Konstantin Baydarov | 708f997 | 2005-10-27 17:25:02 +0200 | [diff] [blame] | 498 | spin_lock(&au1000->ac97_lock); |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 499 | /* would rather use the interrupt than this polling but it works and I can't |
| 500 | get the interrupt driven case to work efficiently */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | for (i = 0; i < 0x5000; i++) |
| 502 | if (!(au1000->ac97_ioport->status & AC97C_CP)) |
| 503 | break; |
| 504 | if (i == 0x5000) |
| 505 | printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n"); |
| 506 | |
| 507 | cmd = (u32) reg & AC97C_INDEX_MASK; |
| 508 | cmd |= AC97C_READ; |
| 509 | au1000->ac97_ioport->cmd = cmd; |
| 510 | |
| 511 | /* now wait for the data */ |
| 512 | for (i = 0; i < 0x5000; i++) |
| 513 | if (!(au1000->ac97_ioport->status & AC97C_CP)) |
| 514 | break; |
| 515 | if (i == 0x5000) { |
| 516 | printk(KERN_ERR "au1000 AC97: AC97 command read timeout\n"); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | data = au1000->ac97_ioport->cmd & 0xffff; |
Konstantin Baydarov | 708f997 | 2005-10-27 17:25:02 +0200 | [diff] [blame] | 521 | spin_unlock(&au1000->ac97_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | return data; |
| 524 | |
| 525 | } |
| 526 | |
| 527 | |
| 528 | static void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 529 | snd_au1000_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 531 | struct snd_au1000 *au1000 = ac97->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | u32 cmd; |
| 533 | int i; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 534 | |
Konstantin Baydarov | 708f997 | 2005-10-27 17:25:02 +0200 | [diff] [blame] | 535 | spin_lock(&au1000->ac97_lock); |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 536 | /* would rather use the interrupt than this polling but it works and I can't |
| 537 | get the interrupt driven case to work efficiently */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | for (i = 0; i < 0x5000; i++) |
| 539 | if (!(au1000->ac97_ioport->status & AC97C_CP)) |
| 540 | break; |
| 541 | if (i == 0x5000) |
| 542 | printk(KERN_ERR "au1000 AC97: AC97 command write timeout\n"); |
| 543 | |
| 544 | cmd = (u32) reg & AC97C_INDEX_MASK; |
| 545 | cmd &= ~AC97C_READ; |
| 546 | cmd |= ((u32) val << AC97C_WD_BIT); |
| 547 | au1000->ac97_ioport->cmd = cmd; |
Konstantin Baydarov | 708f997 | 2005-10-27 17:25:02 +0200 | [diff] [blame] | 548 | spin_unlock(&au1000->ac97_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | static int __devinit |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 552 | snd_au1000_ac97_new(struct snd_au1000 *au1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
| 554 | int err; |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 555 | struct snd_ac97_bus *pbus; |
| 556 | struct snd_ac97_template ac97; |
| 557 | static struct snd_ac97_bus_ops ops = { |
| 558 | .write = snd_au1000_ac97_write, |
| 559 | .read = snd_au1000_ac97_read, |
| 560 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Sergei Shtylyov | c5c079e | 2006-03-21 12:01:17 +0100 | [diff] [blame] | 562 | if ((au1000->ac97_res_port = request_mem_region(CPHYSADDR(AC97C_CONFIG), |
| 563 | 0x100000, "Au1x00 AC97")) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | snd_printk(KERN_ERR "ALSA AC97: can't grap AC97 port\n"); |
| 565 | return -EBUSY; |
| 566 | } |
Sergei Shtylyov | c5c079e | 2006-03-21 12:01:17 +0100 | [diff] [blame] | 567 | au1000->ac97_ioport = (struct au1000_ac97_reg *) |
| 568 | KSEG1ADDR(au1000->ac97_res_port->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | spin_lock_init(&au1000->ac97_lock); |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | /* configure pins for AC'97 |
| 573 | TODO: move to board_setup.c */ |
| 574 | au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC); |
| 575 | |
| 576 | /* Initialise Au1000's AC'97 Control Block */ |
| 577 | au1000->ac97_ioport->cntrl = AC97C_RS | AC97C_CE; |
| 578 | udelay(10); |
| 579 | au1000->ac97_ioport->cntrl = AC97C_CE; |
| 580 | udelay(10); |
| 581 | |
| 582 | /* Initialise External CODEC -- cold reset */ |
| 583 | au1000->ac97_ioport->config = AC97C_RESET; |
| 584 | udelay(10); |
| 585 | au1000->ac97_ioport->config = 0x0; |
| 586 | mdelay(5); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /* Initialise AC97 middle-layer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if ((err = snd_ac97_bus(au1000->card, 0, &ops, au1000, &pbus)) < 0) |
| 590 | return err; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | memset(&ac97, 0, sizeof(ac97)); |
| 593 | ac97.private_data = au1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if ((err = snd_ac97_mixer(pbus, &ac97, &au1000->ac97)) < 0) |
| 595 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 597 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /*------------------------------ Setup / Destroy ----------------------------*/ |
| 601 | |
| 602 | void |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 603 | snd_au1000_free(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 605 | struct snd_au1000 *au1000 = card->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | if (au1000->ac97_res_port) { |
| 608 | /* put internal AC97 block into reset */ |
| 609 | au1000->ac97_ioport->cntrl = AC97C_RS; |
| 610 | au1000->ac97_ioport = NULL; |
Takashi Iwai | b1d5776 | 2005-10-10 11:56:31 +0200 | [diff] [blame] | 611 | release_and_free_resource(au1000->ac97_res_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Sergei Shtylyov | bb160b8 | 2006-03-21 11:58:48 +0100 | [diff] [blame] | 614 | if (au1000->stream[PLAYBACK]) { |
| 615 | if (au1000->stream[PLAYBACK]->dma >= 0) |
| 616 | free_au1000_dma(au1000->stream[PLAYBACK]->dma); |
| 617 | kfree(au1000->stream[PLAYBACK]); |
| 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Sergei Shtylyov | bb160b8 | 2006-03-21 11:58:48 +0100 | [diff] [blame] | 620 | if (au1000->stream[CAPTURE]) { |
| 621 | if (au1000->stream[CAPTURE]->dma >= 0) |
| 622 | free_au1000_dma(au1000->stream[CAPTURE]->dma); |
| 623 | kfree(au1000->stream[CAPTURE]); |
| 624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 627 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 628 | static struct snd_card *au1000_card; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | static int __init |
| 631 | au1000_init(void) |
| 632 | { |
| 633 | int err; |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 634 | struct snd_card *card; |
| 635 | struct snd_au1000 *au1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 637 | card = snd_card_new(-1, "AC97", THIS_MODULE, sizeof(struct snd_au1000)); |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 638 | if (card == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return -ENOMEM; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 640 | |
| 641 | card->private_free = snd_au1000_free; |
| 642 | au1000 = card->private_data; |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 643 | au1000->card = card; |
Sergei Shtylyov | bb160b8 | 2006-03-21 11:58:48 +0100 | [diff] [blame] | 644 | |
Takashi Iwai | a0d6f88 | 2005-11-17 15:12:31 +0100 | [diff] [blame] | 645 | au1000->stream[PLAYBACK] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL); |
Sergei Shtylyov | bb160b8 | 2006-03-21 11:58:48 +0100 | [diff] [blame] | 646 | au1000->stream[CAPTURE ] = kmalloc(sizeof(struct audio_stream), GFP_KERNEL); |
| 647 | /* so that snd_au1000_free will work as intended */ |
| 648 | au1000->ac97_res_port = NULL; |
| 649 | if (au1000->stream[PLAYBACK]) |
| 650 | au1000->stream[PLAYBACK]->dma = -1; |
| 651 | if (au1000->stream[CAPTURE ]) |
| 652 | au1000->stream[CAPTURE ]->dma = -1; |
| 653 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 654 | if (au1000->stream[PLAYBACK] == NULL || |
Sergei Shtylyov | bb160b8 | 2006-03-21 11:58:48 +0100 | [diff] [blame] | 655 | au1000->stream[CAPTURE ] == NULL) { |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 656 | snd_card_free(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return -ENOMEM; |
| 658 | } |
| 659 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 660 | if ((err = snd_au1000_ac97_new(au1000)) < 0 ) { |
| 661 | snd_card_free(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return err; |
| 663 | } |
| 664 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 665 | if ((err = snd_au1000_pcm_new(au1000)) < 0) { |
| 666 | snd_card_free(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | return err; |
| 668 | } |
| 669 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 670 | strcpy(card->driver, "Au1000-AC97"); |
| 671 | strcpy(card->shortname, "AMD Au1000-AC97"); |
| 672 | sprintf(card->longname, "AMD Au1000--AC97 ALSA Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 674 | if ((err = snd_card_register(card)) < 0) { |
| 675 | snd_card_free(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return err; |
| 677 | } |
| 678 | |
| 679 | printk( KERN_INFO "ALSA AC97: Driver Initialized\n" ); |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 680 | au1000_card = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static void __exit au1000_exit(void) |
| 685 | { |
Takashi Iwai | 33ea25c | 2005-11-17 10:32:43 +0100 | [diff] [blame] | 686 | snd_card_free(au1000_card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | module_init(au1000_init); |
| 690 | module_exit(au1000_exit); |
| 691 | |