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