ia64/linux-2.6.18-xen.hg

annotate sound/mips/au1x00.c @ 890:2e94884f0e8d

Dom0 PCI: fix a regression introduced by the SR-IOV change

The device class may be changed during the early fixup. So need to
re-read the device class from pci_dev after the fixup.

The patch "PCI: centralize device setup code" (c/s 825) wrongly
cleaned up the device class re-read. This patch reverts that change.

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