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