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