Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * als300.c - driver for Avance Logic ALS300/ALS300+ soundcards. |
| 3 | * Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * TODO |
| 20 | * 4 channel playback for ALS300+ |
| 21 | * gameport |
| 22 | * mpu401 |
| 23 | * opl3 |
| 24 | * |
| 25 | * NOTES |
| 26 | * The BLOCK_COUNTER registers for the ALS300(+) return a figure related to |
| 27 | * the position in the current period, NOT the whole buffer. It is important |
| 28 | * to know which period we are in so we can calculate the correct pointer. |
| 29 | * This is why we always use 2 periods. We can then use a flip-flop variable |
| 30 | * to keep track of what period we are in. |
| 31 | */ |
| 32 | |
| 33 | #include <sound/driver.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/moduleparam.h> |
| 37 | #include <linux/pci.h> |
Tobias Klauser | 56b146d | 2006-04-10 22:54:21 -0700 | [diff] [blame] | 38 | #include <linux/dma-mapping.h> |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/slab.h> |
| 41 | |
| 42 | #include <asm/io.h> |
| 43 | |
| 44 | #include <sound/core.h> |
| 45 | #include <sound/control.h> |
| 46 | #include <sound/initval.h> |
| 47 | #include <sound/pcm.h> |
| 48 | #include <sound/pcm_params.h> |
| 49 | #include <sound/ac97_codec.h> |
| 50 | #include <sound/opl3.h> |
| 51 | |
| 52 | /* snd_als300_set_irq_flag */ |
| 53 | #define IRQ_DISABLE 0 |
| 54 | #define IRQ_ENABLE 1 |
| 55 | |
| 56 | /* I/O port layout */ |
| 57 | #define AC97_ACCESS 0x00 |
| 58 | #define AC97_READ 0x04 |
| 59 | #define AC97_STATUS 0x06 |
| 60 | #define AC97_DATA_AVAIL (1<<6) |
| 61 | #define AC97_BUSY (1<<7) |
| 62 | #define ALS300_IRQ_STATUS 0x07 /* ALS300 Only */ |
| 63 | #define IRQ_PLAYBACK (1<<3) |
| 64 | #define IRQ_CAPTURE (1<<2) |
| 65 | #define GCR_DATA 0x08 |
| 66 | #define GCR_INDEX 0x0C |
| 67 | #define ALS300P_DRAM_IRQ_STATUS 0x0D /* ALS300+ Only */ |
| 68 | #define MPU_IRQ_STATUS 0x0E /* ALS300 Rev. E+, ALS300+ */ |
| 69 | #define ALS300P_IRQ_STATUS 0x0F /* ALS300+ Only */ |
| 70 | |
| 71 | /* General Control Registers */ |
| 72 | #define PLAYBACK_START 0x80 |
| 73 | #define PLAYBACK_END 0x81 |
| 74 | #define PLAYBACK_CONTROL 0x82 |
| 75 | #define TRANSFER_START (1<<16) |
| 76 | #define FIFO_PAUSE (1<<17) |
| 77 | #define RECORD_START 0x83 |
| 78 | #define RECORD_END 0x84 |
| 79 | #define RECORD_CONTROL 0x85 |
| 80 | #define DRAM_WRITE_CONTROL 0x8B |
| 81 | #define WRITE_TRANS_START (1<<16) |
| 82 | #define DRAM_MODE_2 (1<<17) |
| 83 | #define MISC_CONTROL 0x8C |
| 84 | #define IRQ_SET_BIT (1<<15) |
| 85 | #define VMUTE_NORMAL (1<<20) |
| 86 | #define MMUTE_NORMAL (1<<21) |
| 87 | #define MUS_VOC_VOL 0x8E |
| 88 | #define PLAYBACK_BLOCK_COUNTER 0x9A |
| 89 | #define RECORD_BLOCK_COUNTER 0x9B |
| 90 | |
| 91 | #define DEBUG_CALLS 1 |
| 92 | #define DEBUG_PLAY_REC 1 |
| 93 | |
| 94 | #if DEBUG_CALLS |
| 95 | #define snd_als300_dbgcalls(format, args...) printk(format, ##args) |
| 96 | #define snd_als300_dbgcallenter() printk(KERN_ERR "--> %s\n", __FUNCTION__) |
| 97 | #define snd_als300_dbgcallleave() printk(KERN_ERR "<-- %s\n", __FUNCTION__) |
| 98 | #else |
| 99 | #define snd_als300_dbgcalls(format, args...) |
| 100 | #define snd_als300_dbgcallenter() |
| 101 | #define snd_als300_dbgcallleave() |
| 102 | #endif |
| 103 | |
| 104 | #if DEBUG_PLAY_REC |
| 105 | #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args) |
| 106 | #else |
| 107 | #define snd_als300_dbgplay(format, args...) |
| 108 | #endif |
| 109 | |
| 110 | enum {DEVICE_ALS300, DEVICE_ALS300_PLUS}; |
| 111 | |
| 112 | MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>"); |
| 113 | MODULE_DESCRIPTION("Avance Logic ALS300"); |
| 114 | MODULE_LICENSE("GPL"); |
| 115 | MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}"); |
| 116 | |
| 117 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
| 118 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; |
| 119 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; |
| 120 | |
| 121 | struct snd_als300 { |
| 122 | unsigned long port; |
| 123 | spinlock_t reg_lock; |
| 124 | struct snd_card *card; |
| 125 | struct pci_dev *pci; |
| 126 | |
| 127 | struct snd_pcm *pcm; |
| 128 | struct snd_pcm_substream *playback_substream; |
| 129 | struct snd_pcm_substream *capture_substream; |
| 130 | |
| 131 | struct snd_ac97 *ac97; |
| 132 | struct snd_opl3 *opl3; |
| 133 | |
| 134 | struct resource *res_port; |
| 135 | |
| 136 | int irq; |
| 137 | |
| 138 | int chip_type; /* ALS300 or ALS300+ */ |
| 139 | |
| 140 | char revision; |
| 141 | }; |
| 142 | |
| 143 | struct snd_als300_substream_data { |
| 144 | int period_flipflop; |
| 145 | int control_register; |
| 146 | int block_counter_register; |
| 147 | }; |
| 148 | |
Takashi Iwai | f40b689 | 2006-07-05 16:51:05 +0200 | [diff] [blame] | 149 | static struct pci_device_id snd_als300_ids[] = { |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 150 | { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 }, |
| 151 | { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS }, |
| 152 | { 0, } |
| 153 | }; |
| 154 | |
| 155 | MODULE_DEVICE_TABLE(pci, snd_als300_ids); |
| 156 | |
| 157 | static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg) |
| 158 | { |
| 159 | outb(reg, port+GCR_INDEX); |
| 160 | return inl(port+GCR_DATA); |
| 161 | } |
| 162 | |
| 163 | static inline void snd_als300_gcr_write(unsigned long port, |
| 164 | unsigned short reg, u32 val) |
| 165 | { |
| 166 | outb(reg, port+GCR_INDEX); |
| 167 | outl(val, port+GCR_DATA); |
| 168 | } |
| 169 | |
| 170 | /* Enable/Disable Interrupts */ |
| 171 | static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd) |
| 172 | { |
| 173 | u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL); |
| 174 | snd_als300_dbgcallenter(); |
| 175 | |
| 176 | /* boolean XOR check, since old vs. new hardware have |
| 177 | directly reversed bit setting for ENABLE and DISABLE. |
| 178 | ALS300+ acts like newer versions of ALS300 */ |
| 179 | if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^ |
| 180 | (cmd == IRQ_ENABLE)) == 0) |
| 181 | tmp |= IRQ_SET_BIT; |
| 182 | else |
| 183 | tmp &= ~IRQ_SET_BIT; |
| 184 | snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp); |
| 185 | snd_als300_dbgcallleave(); |
| 186 | } |
| 187 | |
| 188 | static int snd_als300_free(struct snd_als300 *chip) |
| 189 | { |
| 190 | snd_als300_dbgcallenter(); |
| 191 | snd_als300_set_irq_flag(chip, IRQ_DISABLE); |
| 192 | if (chip->irq >= 0) |
Takashi Iwai | 437a5a4 | 2006-11-21 12:14:23 +0100 | [diff] [blame] | 193 | free_irq(chip->irq, chip); |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 194 | pci_release_regions(chip->pci); |
| 195 | pci_disable_device(chip->pci); |
| 196 | kfree(chip); |
| 197 | snd_als300_dbgcallleave(); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int snd_als300_dev_free(struct snd_device *device) |
| 202 | { |
| 203 | struct snd_als300 *chip = device->device_data; |
| 204 | return snd_als300_free(chip); |
| 205 | } |
| 206 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 207 | static irqreturn_t snd_als300_interrupt(int irq, void *dev_id) |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 208 | { |
| 209 | u8 status; |
| 210 | struct snd_als300 *chip = dev_id; |
| 211 | struct snd_als300_substream_data *data; |
| 212 | |
| 213 | status = inb(chip->port+ALS300_IRQ_STATUS); |
| 214 | if (!status) /* shared IRQ, for different device?? Exit ASAP! */ |
| 215 | return IRQ_NONE; |
| 216 | |
| 217 | /* ACK everything ASAP */ |
| 218 | outb(status, chip->port+ALS300_IRQ_STATUS); |
| 219 | if (status & IRQ_PLAYBACK) { |
| 220 | if (chip->pcm && chip->playback_substream) { |
| 221 | data = chip->playback_substream->runtime->private_data; |
| 222 | data->period_flipflop ^= 1; |
| 223 | snd_pcm_period_elapsed(chip->playback_substream); |
| 224 | snd_als300_dbgplay("IRQ_PLAYBACK\n"); |
| 225 | } |
| 226 | } |
| 227 | if (status & IRQ_CAPTURE) { |
| 228 | if (chip->pcm && chip->capture_substream) { |
| 229 | data = chip->capture_substream->runtime->private_data; |
| 230 | data->period_flipflop ^= 1; |
| 231 | snd_pcm_period_elapsed(chip->capture_substream); |
| 232 | snd_als300_dbgplay("IRQ_CAPTURE\n"); |
| 233 | } |
| 234 | } |
| 235 | return IRQ_HANDLED; |
| 236 | } |
| 237 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 238 | static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id) |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 239 | { |
| 240 | u8 general, mpu, dram; |
| 241 | struct snd_als300 *chip = dev_id; |
| 242 | struct snd_als300_substream_data *data; |
| 243 | |
| 244 | general = inb(chip->port+ALS300P_IRQ_STATUS); |
| 245 | mpu = inb(chip->port+MPU_IRQ_STATUS); |
| 246 | dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS); |
| 247 | |
| 248 | /* shared IRQ, for different device?? Exit ASAP! */ |
| 249 | if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0)) |
| 250 | return IRQ_NONE; |
| 251 | |
| 252 | if (general & IRQ_PLAYBACK) { |
| 253 | if (chip->pcm && chip->playback_substream) { |
| 254 | outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS); |
| 255 | data = chip->playback_substream->runtime->private_data; |
| 256 | data->period_flipflop ^= 1; |
| 257 | snd_pcm_period_elapsed(chip->playback_substream); |
| 258 | snd_als300_dbgplay("IRQ_PLAYBACK\n"); |
| 259 | } |
| 260 | } |
| 261 | if (general & IRQ_CAPTURE) { |
| 262 | if (chip->pcm && chip->capture_substream) { |
| 263 | outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS); |
| 264 | data = chip->capture_substream->runtime->private_data; |
| 265 | data->period_flipflop ^= 1; |
| 266 | snd_pcm_period_elapsed(chip->capture_substream); |
| 267 | snd_als300_dbgplay("IRQ_CAPTURE\n"); |
| 268 | } |
| 269 | } |
| 270 | /* FIXME: Ack other interrupt types. Not important right now as |
| 271 | * those other devices aren't enabled. */ |
| 272 | return IRQ_HANDLED; |
| 273 | } |
| 274 | |
| 275 | static void __devexit snd_als300_remove(struct pci_dev *pci) |
| 276 | { |
| 277 | snd_als300_dbgcallenter(); |
| 278 | snd_card_free(pci_get_drvdata(pci)); |
| 279 | pci_set_drvdata(pci, NULL); |
| 280 | snd_als300_dbgcallleave(); |
| 281 | } |
| 282 | |
| 283 | static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97, |
| 284 | unsigned short reg) |
| 285 | { |
| 286 | int i; |
| 287 | struct snd_als300 *chip = ac97->private_data; |
| 288 | |
| 289 | for (i = 0; i < 1000; i++) { |
| 290 | if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0) |
| 291 | break; |
| 292 | udelay(10); |
| 293 | } |
| 294 | outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS); |
| 295 | |
| 296 | for (i = 0; i < 1000; i++) { |
| 297 | if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0) |
| 298 | break; |
| 299 | udelay(10); |
| 300 | } |
| 301 | return inw(chip->port+AC97_READ); |
| 302 | } |
| 303 | |
| 304 | static void snd_als300_ac97_write(struct snd_ac97 *ac97, |
| 305 | unsigned short reg, unsigned short val) |
| 306 | { |
| 307 | int i; |
| 308 | struct snd_als300 *chip = ac97->private_data; |
| 309 | |
| 310 | for (i = 0; i < 1000; i++) { |
| 311 | if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0) |
| 312 | break; |
| 313 | udelay(10); |
| 314 | } |
| 315 | outl((reg << 24) | val, chip->port+AC97_ACCESS); |
| 316 | } |
| 317 | |
| 318 | static int snd_als300_ac97(struct snd_als300 *chip) |
| 319 | { |
| 320 | struct snd_ac97_bus *bus; |
| 321 | struct snd_ac97_template ac97; |
| 322 | int err; |
| 323 | static struct snd_ac97_bus_ops ops = { |
| 324 | .write = snd_als300_ac97_write, |
| 325 | .read = snd_als300_ac97_read, |
| 326 | }; |
| 327 | |
| 328 | snd_als300_dbgcallenter(); |
| 329 | if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0) |
| 330 | return err; |
| 331 | |
| 332 | memset(&ac97, 0, sizeof(ac97)); |
| 333 | ac97.private_data = chip; |
| 334 | |
| 335 | snd_als300_dbgcallleave(); |
| 336 | return snd_ac97_mixer(bus, &ac97, &chip->ac97); |
| 337 | } |
| 338 | |
| 339 | /* hardware definition |
| 340 | * |
| 341 | * In AC97 mode, we always use 48k/16bit/stereo. |
| 342 | * Any request to change data type is ignored by |
| 343 | * the card when it is running outside of legacy |
| 344 | * mode. |
| 345 | */ |
| 346 | static struct snd_pcm_hardware snd_als300_playback_hw = |
| 347 | { |
| 348 | .info = (SNDRV_PCM_INFO_MMAP | |
| 349 | SNDRV_PCM_INFO_INTERLEAVED | |
| 350 | SNDRV_PCM_INFO_PAUSE | |
| 351 | SNDRV_PCM_INFO_MMAP_VALID), |
| 352 | .formats = SNDRV_PCM_FMTBIT_S16, |
| 353 | .rates = SNDRV_PCM_RATE_48000, |
| 354 | .rate_min = 48000, |
| 355 | .rate_max = 48000, |
| 356 | .channels_min = 2, |
| 357 | .channels_max = 2, |
| 358 | .buffer_bytes_max = 64 * 1024, |
| 359 | .period_bytes_min = 64, |
| 360 | .period_bytes_max = 32 * 1024, |
| 361 | .periods_min = 2, |
| 362 | .periods_max = 2, |
| 363 | }; |
| 364 | |
| 365 | static struct snd_pcm_hardware snd_als300_capture_hw = |
| 366 | { |
| 367 | .info = (SNDRV_PCM_INFO_MMAP | |
| 368 | SNDRV_PCM_INFO_INTERLEAVED | |
| 369 | SNDRV_PCM_INFO_PAUSE | |
| 370 | SNDRV_PCM_INFO_MMAP_VALID), |
| 371 | .formats = SNDRV_PCM_FMTBIT_S16, |
| 372 | .rates = SNDRV_PCM_RATE_48000, |
| 373 | .rate_min = 48000, |
| 374 | .rate_max = 48000, |
| 375 | .channels_min = 2, |
| 376 | .channels_max = 2, |
| 377 | .buffer_bytes_max = 64 * 1024, |
| 378 | .period_bytes_min = 64, |
| 379 | .period_bytes_max = 32 * 1024, |
| 380 | .periods_min = 2, |
| 381 | .periods_max = 2, |
| 382 | }; |
| 383 | |
| 384 | static int snd_als300_playback_open(struct snd_pcm_substream *substream) |
| 385 | { |
| 386 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 387 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 388 | struct snd_als300_substream_data *data = kzalloc(sizeof(*data), |
| 389 | GFP_KERNEL); |
| 390 | |
| 391 | snd_als300_dbgcallenter(); |
| 392 | chip->playback_substream = substream; |
| 393 | runtime->hw = snd_als300_playback_hw; |
| 394 | runtime->private_data = data; |
| 395 | data->control_register = PLAYBACK_CONTROL; |
| 396 | data->block_counter_register = PLAYBACK_BLOCK_COUNTER; |
| 397 | snd_als300_dbgcallleave(); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static int snd_als300_playback_close(struct snd_pcm_substream *substream) |
| 402 | { |
| 403 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 404 | struct snd_als300_substream_data *data; |
| 405 | |
| 406 | data = substream->runtime->private_data; |
| 407 | snd_als300_dbgcallenter(); |
| 408 | kfree(data); |
| 409 | chip->playback_substream = NULL; |
| 410 | snd_pcm_lib_free_pages(substream); |
| 411 | snd_als300_dbgcallleave(); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int snd_als300_capture_open(struct snd_pcm_substream *substream) |
| 416 | { |
| 417 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 418 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 419 | struct snd_als300_substream_data *data = kzalloc(sizeof(*data), |
| 420 | GFP_KERNEL); |
| 421 | |
| 422 | snd_als300_dbgcallenter(); |
| 423 | chip->capture_substream = substream; |
| 424 | runtime->hw = snd_als300_capture_hw; |
| 425 | runtime->private_data = data; |
| 426 | data->control_register = RECORD_CONTROL; |
| 427 | data->block_counter_register = RECORD_BLOCK_COUNTER; |
| 428 | snd_als300_dbgcallleave(); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int snd_als300_capture_close(struct snd_pcm_substream *substream) |
| 433 | { |
| 434 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 435 | struct snd_als300_substream_data *data; |
| 436 | |
| 437 | data = substream->runtime->private_data; |
| 438 | snd_als300_dbgcallenter(); |
| 439 | kfree(data); |
| 440 | chip->capture_substream = NULL; |
| 441 | snd_pcm_lib_free_pages(substream); |
| 442 | snd_als300_dbgcallleave(); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream, |
Takashi Iwai | d1d985f | 2006-11-23 19:27:12 +0100 | [diff] [blame] | 447 | struct snd_pcm_hw_params *hw_params) |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 448 | { |
| 449 | return snd_pcm_lib_malloc_pages(substream, |
| 450 | params_buffer_bytes(hw_params)); |
| 451 | } |
| 452 | |
| 453 | static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream) |
| 454 | { |
| 455 | return snd_pcm_lib_free_pages(substream); |
| 456 | } |
| 457 | |
| 458 | static int snd_als300_playback_prepare(struct snd_pcm_substream *substream) |
| 459 | { |
| 460 | u32 tmp; |
| 461 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 462 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 463 | unsigned short period_bytes = snd_pcm_lib_period_bytes(substream); |
| 464 | unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream); |
| 465 | |
| 466 | snd_als300_dbgcallenter(); |
| 467 | spin_lock_irq(&chip->reg_lock); |
| 468 | tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL); |
| 469 | tmp &= ~TRANSFER_START; |
| 470 | |
| 471 | snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", |
| 472 | period_bytes, buffer_bytes); |
| 473 | |
| 474 | /* set block size */ |
| 475 | tmp &= 0xffff0000; |
| 476 | tmp |= period_bytes - 1; |
| 477 | snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp); |
| 478 | |
| 479 | /* set dma area */ |
| 480 | snd_als300_gcr_write(chip->port, PLAYBACK_START, |
| 481 | runtime->dma_addr); |
| 482 | snd_als300_gcr_write(chip->port, PLAYBACK_END, |
| 483 | runtime->dma_addr + buffer_bytes - 1); |
| 484 | spin_unlock_irq(&chip->reg_lock); |
| 485 | snd_als300_dbgcallleave(); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int snd_als300_capture_prepare(struct snd_pcm_substream *substream) |
| 490 | { |
| 491 | u32 tmp; |
| 492 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 493 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 494 | unsigned short period_bytes = snd_pcm_lib_period_bytes(substream); |
| 495 | unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream); |
| 496 | |
| 497 | snd_als300_dbgcallenter(); |
| 498 | spin_lock_irq(&chip->reg_lock); |
| 499 | tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL); |
| 500 | tmp &= ~TRANSFER_START; |
| 501 | |
| 502 | snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes, |
| 503 | buffer_bytes); |
| 504 | |
| 505 | /* set block size */ |
| 506 | tmp &= 0xffff0000; |
| 507 | tmp |= period_bytes - 1; |
| 508 | |
| 509 | /* set dma area */ |
| 510 | snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp); |
| 511 | snd_als300_gcr_write(chip->port, RECORD_START, |
| 512 | runtime->dma_addr); |
| 513 | snd_als300_gcr_write(chip->port, RECORD_END, |
| 514 | runtime->dma_addr + buffer_bytes - 1); |
| 515 | spin_unlock_irq(&chip->reg_lock); |
| 516 | snd_als300_dbgcallleave(); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd) |
| 521 | { |
| 522 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 523 | u32 tmp; |
| 524 | struct snd_als300_substream_data *data; |
| 525 | unsigned short reg; |
| 526 | int ret = 0; |
| 527 | |
| 528 | data = substream->runtime->private_data; |
| 529 | reg = data->control_register; |
| 530 | |
| 531 | snd_als300_dbgcallenter(); |
| 532 | spin_lock(&chip->reg_lock); |
| 533 | switch (cmd) { |
| 534 | case SNDRV_PCM_TRIGGER_START: |
| 535 | case SNDRV_PCM_TRIGGER_RESUME: |
| 536 | tmp = snd_als300_gcr_read(chip->port, reg); |
| 537 | data->period_flipflop = 1; |
| 538 | snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START); |
| 539 | snd_als300_dbgplay("TRIGGER START\n"); |
| 540 | break; |
| 541 | case SNDRV_PCM_TRIGGER_STOP: |
| 542 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 543 | tmp = snd_als300_gcr_read(chip->port, reg); |
| 544 | snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START); |
| 545 | snd_als300_dbgplay("TRIGGER STOP\n"); |
| 546 | break; |
| 547 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 548 | tmp = snd_als300_gcr_read(chip->port, reg); |
| 549 | snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE); |
| 550 | snd_als300_dbgplay("TRIGGER PAUSE\n"); |
| 551 | break; |
| 552 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 553 | tmp = snd_als300_gcr_read(chip->port, reg); |
| 554 | snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE); |
| 555 | snd_als300_dbgplay("TRIGGER RELEASE\n"); |
| 556 | break; |
| 557 | default: |
| 558 | snd_als300_dbgplay("TRIGGER INVALID\n"); |
| 559 | ret = -EINVAL; |
| 560 | } |
| 561 | spin_unlock(&chip->reg_lock); |
| 562 | snd_als300_dbgcallleave(); |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream) |
| 567 | { |
| 568 | u16 current_ptr; |
| 569 | struct snd_als300 *chip = snd_pcm_substream_chip(substream); |
| 570 | struct snd_als300_substream_data *data; |
| 571 | unsigned short period_bytes; |
| 572 | |
| 573 | data = substream->runtime->private_data; |
| 574 | period_bytes = snd_pcm_lib_period_bytes(substream); |
| 575 | |
| 576 | snd_als300_dbgcallenter(); |
| 577 | spin_lock(&chip->reg_lock); |
| 578 | current_ptr = (u16) snd_als300_gcr_read(chip->port, |
| 579 | data->block_counter_register) + 4; |
| 580 | spin_unlock(&chip->reg_lock); |
| 581 | if (current_ptr > period_bytes) |
| 582 | current_ptr = 0; |
| 583 | else |
| 584 | current_ptr = period_bytes - current_ptr; |
| 585 | |
| 586 | if (data->period_flipflop == 0) |
| 587 | current_ptr += period_bytes; |
| 588 | snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr); |
| 589 | snd_als300_dbgcallleave(); |
| 590 | return bytes_to_frames(substream->runtime, current_ptr); |
| 591 | } |
| 592 | |
| 593 | static struct snd_pcm_ops snd_als300_playback_ops = { |
| 594 | .open = snd_als300_playback_open, |
| 595 | .close = snd_als300_playback_close, |
| 596 | .ioctl = snd_pcm_lib_ioctl, |
| 597 | .hw_params = snd_als300_pcm_hw_params, |
| 598 | .hw_free = snd_als300_pcm_hw_free, |
| 599 | .prepare = snd_als300_playback_prepare, |
| 600 | .trigger = snd_als300_trigger, |
| 601 | .pointer = snd_als300_pointer, |
| 602 | }; |
| 603 | |
| 604 | static struct snd_pcm_ops snd_als300_capture_ops = { |
| 605 | .open = snd_als300_capture_open, |
| 606 | .close = snd_als300_capture_close, |
| 607 | .ioctl = snd_pcm_lib_ioctl, |
| 608 | .hw_params = snd_als300_pcm_hw_params, |
| 609 | .hw_free = snd_als300_pcm_hw_free, |
| 610 | .prepare = snd_als300_capture_prepare, |
| 611 | .trigger = snd_als300_trigger, |
| 612 | .pointer = snd_als300_pointer, |
| 613 | }; |
| 614 | |
| 615 | static int __devinit snd_als300_new_pcm(struct snd_als300 *chip) |
| 616 | { |
| 617 | struct snd_pcm *pcm; |
| 618 | int err; |
| 619 | |
| 620 | snd_als300_dbgcallenter(); |
| 621 | err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm); |
| 622 | if (err < 0) |
| 623 | return err; |
| 624 | pcm->private_data = chip; |
| 625 | strcpy(pcm->name, "ALS300"); |
| 626 | chip->pcm = pcm; |
| 627 | |
| 628 | /* set operators */ |
| 629 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 630 | &snd_als300_playback_ops); |
| 631 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, |
| 632 | &snd_als300_capture_ops); |
| 633 | |
| 634 | /* pre-allocation of buffers */ |
| 635 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 636 | snd_dma_pci_data(chip->pci), 64*1024, 64*1024); |
| 637 | snd_als300_dbgcallleave(); |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void snd_als300_init(struct snd_als300 *chip) |
| 642 | { |
| 643 | unsigned long flags; |
| 644 | u32 tmp; |
| 645 | |
| 646 | snd_als300_dbgcallenter(); |
| 647 | spin_lock_irqsave(&chip->reg_lock, flags); |
| 648 | chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16) |
| 649 | & 0x0000000F; |
| 650 | /* Setup DRAM */ |
| 651 | tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL); |
| 652 | snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL, |
| 653 | (tmp | DRAM_MODE_2) |
| 654 | & ~WRITE_TRANS_START); |
| 655 | |
| 656 | /* Enable IRQ output */ |
| 657 | snd_als300_set_irq_flag(chip, IRQ_ENABLE); |
| 658 | |
| 659 | /* Unmute hardware devices so their outputs get routed to |
| 660 | * the onboard mixer */ |
| 661 | tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL); |
| 662 | snd_als300_gcr_write(chip->port, MISC_CONTROL, |
| 663 | tmp | VMUTE_NORMAL | MMUTE_NORMAL); |
| 664 | |
| 665 | /* Reset volumes */ |
| 666 | snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0); |
| 667 | |
| 668 | /* Make sure playback transfer is stopped */ |
| 669 | tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL); |
| 670 | snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, |
| 671 | tmp & ~TRANSFER_START); |
| 672 | spin_unlock_irqrestore(&chip->reg_lock, flags); |
| 673 | snd_als300_dbgcallleave(); |
| 674 | } |
| 675 | |
Takashi Iwai | d1d985f | 2006-11-23 19:27:12 +0100 | [diff] [blame] | 676 | static int __devinit snd_als300_create(struct snd_card *card, |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 677 | struct pci_dev *pci, int chip_type, |
| 678 | struct snd_als300 **rchip) |
| 679 | { |
| 680 | struct snd_als300 *chip; |
| 681 | void *irq_handler; |
| 682 | int err; |
| 683 | |
Takashi Iwai | d1d985f | 2006-11-23 19:27:12 +0100 | [diff] [blame] | 684 | static struct snd_device_ops ops = { |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 685 | .dev_free = snd_als300_dev_free, |
| 686 | }; |
| 687 | *rchip = NULL; |
| 688 | |
| 689 | snd_als300_dbgcallenter(); |
| 690 | if ((err = pci_enable_device(pci)) < 0) |
| 691 | return err; |
| 692 | |
Tobias Klauser | 56b146d | 2006-04-10 22:54:21 -0700 | [diff] [blame] | 693 | if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 || |
| 694 | pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) { |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 695 | printk(KERN_ERR "error setting 28bit DMA mask\n"); |
| 696 | pci_disable_device(pci); |
| 697 | return -ENXIO; |
| 698 | } |
| 699 | pci_set_master(pci); |
| 700 | |
| 701 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 702 | if (chip == NULL) { |
| 703 | pci_disable_device(pci); |
| 704 | return -ENOMEM; |
| 705 | } |
| 706 | |
| 707 | chip->card = card; |
| 708 | chip->pci = pci; |
| 709 | chip->irq = -1; |
| 710 | chip->chip_type = chip_type; |
| 711 | spin_lock_init(&chip->reg_lock); |
| 712 | |
| 713 | if ((err = pci_request_regions(pci, "ALS300")) < 0) { |
| 714 | kfree(chip); |
| 715 | pci_disable_device(pci); |
| 716 | return err; |
| 717 | } |
| 718 | chip->port = pci_resource_start(pci, 0); |
| 719 | |
| 720 | if (chip->chip_type == DEVICE_ALS300_PLUS) |
| 721 | irq_handler = snd_als300plus_interrupt; |
| 722 | else |
| 723 | irq_handler = snd_als300_interrupt; |
| 724 | |
Takashi Iwai | 437a5a4 | 2006-11-21 12:14:23 +0100 | [diff] [blame] | 725 | if (request_irq(pci->irq, irq_handler, IRQF_SHARED, |
| 726 | card->shortname, chip)) { |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 727 | snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq); |
| 728 | snd_als300_free(chip); |
| 729 | return -EBUSY; |
| 730 | } |
| 731 | chip->irq = pci->irq; |
| 732 | |
| 733 | |
| 734 | snd_als300_init(chip); |
| 735 | |
| 736 | if (snd_als300_ac97(chip) < 0) { |
| 737 | snd_printk(KERN_WARNING "Could not create ac97\n"); |
| 738 | snd_als300_free(chip); |
| 739 | return err; |
| 740 | } |
| 741 | |
| 742 | if ((err = snd_als300_new_pcm(chip)) < 0) { |
| 743 | snd_printk(KERN_WARNING "Could not create PCM\n"); |
| 744 | snd_als300_free(chip); |
| 745 | return err; |
| 746 | } |
| 747 | |
| 748 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, |
| 749 | chip, &ops)) < 0) { |
| 750 | snd_als300_free(chip); |
| 751 | return err; |
| 752 | } |
| 753 | |
| 754 | snd_card_set_dev(card, &pci->dev); |
| 755 | |
| 756 | *rchip = chip; |
| 757 | snd_als300_dbgcallleave(); |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | #ifdef CONFIG_PM |
| 762 | static int snd_als300_suspend(struct pci_dev *pci, pm_message_t state) |
| 763 | { |
| 764 | struct snd_card *card = pci_get_drvdata(pci); |
| 765 | struct snd_als300 *chip = card->private_data; |
| 766 | |
| 767 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
| 768 | snd_pcm_suspend_all(chip->pcm); |
| 769 | snd_ac97_suspend(chip->ac97); |
| 770 | |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 771 | pci_disable_device(pci); |
| 772 | pci_save_state(pci); |
Takashi Iwai | 30b3539 | 2006-10-11 18:52:53 +0200 | [diff] [blame] | 773 | pci_set_power_state(pci, pci_choose_state(pci, state)); |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static int snd_als300_resume(struct pci_dev *pci) |
| 778 | { |
| 779 | struct snd_card *card = pci_get_drvdata(pci); |
| 780 | struct snd_als300 *chip = card->private_data; |
| 781 | |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 782 | pci_set_power_state(pci, PCI_D0); |
Takashi Iwai | 30b3539 | 2006-10-11 18:52:53 +0200 | [diff] [blame] | 783 | pci_restore_state(pci); |
| 784 | if (pci_enable_device(pci) < 0) { |
| 785 | printk(KERN_ERR "als300: pci_enable_device failed, " |
| 786 | "disabling device\n"); |
| 787 | snd_card_disconnect(card); |
| 788 | return -EIO; |
| 789 | } |
Ash Willis | b3a70d5 | 2006-03-27 13:20:40 +0200 | [diff] [blame] | 790 | pci_set_master(pci); |
| 791 | |
| 792 | snd_als300_init(chip); |
| 793 | snd_ac97_resume(chip->ac97); |
| 794 | |
| 795 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 796 | return 0; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | static int __devinit snd_als300_probe(struct pci_dev *pci, |
| 801 | const struct pci_device_id *pci_id) |
| 802 | { |
| 803 | static int dev; |
| 804 | struct snd_card *card; |
| 805 | struct snd_als300 *chip; |
| 806 | int err, chip_type; |
| 807 | |
| 808 | if (dev >= SNDRV_CARDS) |
| 809 | return -ENODEV; |
| 810 | if (!enable[dev]) { |
| 811 | dev++; |
| 812 | return -ENOENT; |
| 813 | } |
| 814 | |
| 815 | card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0); |
| 816 | |
| 817 | if (card == NULL) |
| 818 | return -ENOMEM; |
| 819 | |
| 820 | chip_type = pci_id->driver_data; |
| 821 | |
| 822 | if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) { |
| 823 | snd_card_free(card); |
| 824 | return err; |
| 825 | } |
| 826 | card->private_data = chip; |
| 827 | |
| 828 | strcpy(card->driver, "ALS300"); |
| 829 | if (chip->chip_type == DEVICE_ALS300_PLUS) |
| 830 | /* don't know much about ALS300+ yet |
| 831 | * print revision number for now */ |
| 832 | sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision); |
| 833 | else |
| 834 | sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' + |
| 835 | chip->revision - 1); |
| 836 | sprintf(card->longname, "%s at 0x%lx irq %i", |
| 837 | card->shortname, chip->port, chip->irq); |
| 838 | |
| 839 | if ((err = snd_card_register(card)) < 0) { |
| 840 | snd_card_free(card); |
| 841 | return err; |
| 842 | } |
| 843 | pci_set_drvdata(pci, card); |
| 844 | dev++; |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static struct pci_driver driver = { |
| 849 | .name = "ALS300", |
| 850 | .id_table = snd_als300_ids, |
| 851 | .probe = snd_als300_probe, |
| 852 | .remove = __devexit_p(snd_als300_remove), |
| 853 | #ifdef CONFIG_PM |
| 854 | .suspend = snd_als300_suspend, |
| 855 | .resume = snd_als300_resume, |
| 856 | #endif |
| 857 | }; |
| 858 | |
| 859 | static int __init alsa_card_als300_init(void) |
| 860 | { |
| 861 | return pci_register_driver(&driver); |
| 862 | } |
| 863 | |
| 864 | static void __exit alsa_card_als300_exit(void) |
| 865 | { |
| 866 | pci_unregister_driver(&driver); |
| 867 | } |
| 868 | |
| 869 | module_init(alsa_card_als300_init) |
| 870 | module_exit(alsa_card_als300_exit) |