blob: b86565dcdbe41fe3fb9a86ed00b080296ee5a8fd [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ash Willisb3a70d52006-03-27 13:20:40 +02002/*
3 * als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
4 * Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
5 *
Ash Willisb3a70d52006-03-27 13:20:40 +02006 * 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
Ash Willisb3a70d52006-03-27 13:20:40 +020020#include <linux/delay.h>
21#include <linux/init.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040022#include <linux/module.h>
Ash Willisb3a70d52006-03-27 13:20:40 +020023#include <linux/pci.h>
Tobias Klauser56b146d2006-04-10 22:54:21 -070024#include <linux/dma-mapping.h>
Ash Willisb3a70d52006-03-27 13:20:40 +020025#include <linux/interrupt.h>
26#include <linux/slab.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010027#include <linux/io.h>
Ash Willisb3a70d52006-03-27 13:20:40 +020028
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
Ash Willis65ff2352007-05-29 14:34:17 +020076#define DEBUG_PLAY_REC 0
Ash Willisb3a70d52006-03-27 13:20:40 +020077
Ash Willisb3a70d52006-03-27 13:20:40 +020078#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");
Ash Willisb3a70d52006-03-27 13:20:40 +020089
90static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
91static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Takashi Iwaicde94482011-12-19 10:32:48 +010092static 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.");
Ash Willisb3a70d52006-03-27 13:20:40 +0200100
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
Benoit Taine9baa3c32014-08-08 15:56:03 +0200129static const struct pci_device_id snd_als300_ids[] = {
Ash Willisb3a70d52006-03-27 13:20:40 +0200130 { 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200154
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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200164}
165
Takashi Iwai21a93142021-07-15 09:58:31 +0200166static void snd_als300_free(struct snd_card *card)
Ash Willisb3a70d52006-03-27 13:20:40 +0200167{
Takashi Iwai21a93142021-07-15 09:58:31 +0200168 struct snd_als300 *chip = card->private_data;
Ash Willisb3a70d52006-03-27 13:20:40 +0200169
Takashi Iwai21a93142021-07-15 09:58:31 +0200170 snd_als300_set_irq_flag(chip, IRQ_DISABLE);
Ash Willisb3a70d52006-03-27 13:20:40 +0200171}
172
David Howells7d12e782006-10-05 14:55:46 +0100173static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
Ash Willisb3a70d52006-03-27 13:20:40 +0200174{
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
David Howells7d12e782006-10-05 14:55:46 +0100204static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
Ash Willisb3a70d52006-03-27 13:20:40 +0200205{
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
Ash Willisb3a70d52006-03-27 13:20:40 +0200241static 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;
Takashi Iwai51055da2020-01-03 09:16:43 +0100281 static const struct snd_ac97_bus_ops ops = {
Ash Willisb3a70d52006-03-27 13:20:40 +0200282 .write = snd_als300_ac97_write,
283 .read = snd_als300_ac97_read,
284 };
285
Takashi Iwai5bd11522021-06-08 16:04:51 +0200286 err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
287 if (err < 0)
Ash Willisb3a70d52006-03-27 13:20:40 +0200288 return err;
289
290 memset(&ac97, 0, sizeof(ac97));
291 ac97.private_data = chip;
292
Ash Willisb3a70d52006-03-27 13:20:40 +0200293 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 */
Bhumika Goyaldee49892017-08-12 21:01:28 +0530303static const struct snd_pcm_hardware snd_als300_playback_hw =
Ash Willisb3a70d52006-03-27 13:20:40 +0200304{
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
Bhumika Goyaldee49892017-08-12 21:01:28 +0530322static const struct snd_pcm_hardware snd_als300_capture_hw =
Ash Willisb3a70d52006-03-27 13:20:40 +0200323{
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
Denis Kirjanov21b3de82012-10-22 17:05:53 +0400348 if (!data)
349 return -ENOMEM;
Ash Willisb3a70d52006-03-27 13:20:40 +0200350 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;
Ash Willisb3a70d52006-03-27 13:20:40 +0200355 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;
Ash Willisb3a70d52006-03-27 13:20:40 +0200364 kfree(data);
365 chip->playback_substream = NULL;
Ash Willisb3a70d52006-03-27 13:20:40 +0200366 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
Denis Kirjanov21b3de82012-10-22 17:05:53 +0400376 if (!data)
377 return -ENOMEM;
Ash Willisb3a70d52006-03-27 13:20:40 +0200378 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;
Ash Willisb3a70d52006-03-27 13:20:40 +0200383 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;
Ash Willisb3a70d52006-03-27 13:20:40 +0200392 kfree(data);
393 chip->capture_substream = NULL;
Ash Willisb3a70d52006-03-27 13:20:40 +0200394 return 0;
395}
396
Ash Willisb3a70d52006-03-27 13:20:40 +0200397static 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
Ash Willisb3a70d52006-03-27 13:20:40 +0200405 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200423 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
Ash Willisb3a70d52006-03-27 13:20:40 +0200434 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200452 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
Ash Willisb3a70d52006-03-27 13:20:40 +0200466 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200496 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
Ash Willisb3a70d52006-03-27 13:20:40 +0200509 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200521 return bytes_to_frames(substream->runtime, current_ptr);
522}
523
Julia Lawall6769e9882016-09-02 00:13:10 +0200524static const struct snd_pcm_ops snd_als300_playback_ops = {
Ash Willisb3a70d52006-03-27 13:20:40 +0200525 .open = snd_als300_playback_open,
526 .close = snd_als300_playback_close,
Ash Willisb3a70d52006-03-27 13:20:40 +0200527 .prepare = snd_als300_playback_prepare,
528 .trigger = snd_als300_trigger,
529 .pointer = snd_als300_pointer,
530};
531
Julia Lawall6769e9882016-09-02 00:13:10 +0200532static const struct snd_pcm_ops snd_als300_capture_ops = {
Ash Willisb3a70d52006-03-27 13:20:40 +0200533 .open = snd_als300_capture_open,
534 .close = snd_als300_capture_close,
Ash Willisb3a70d52006-03-27 13:20:40 +0200535 .prepare = snd_als300_capture_prepare,
536 .trigger = snd_als300_trigger,
537 .pointer = snd_als300_pointer,
538};
539
Bill Pembertone23e7a12012-12-06 12:35:10 -0500540static int snd_als300_new_pcm(struct snd_als300 *chip)
Ash Willisb3a70d52006-03-27 13:20:40 +0200541{
542 struct snd_pcm *pcm;
543 int err;
544
Ash Willisb3a70d52006-03-27 13:20:40 +0200545 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 */
Takashi Iwai909c7d22019-12-09 10:48:53 +0100559 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
560 64*1024, 64*1024);
Ash Willisb3a70d52006-03-27 13:20:40 +0200561 return 0;
562}
563
564static void snd_als300_init(struct snd_als300 *chip)
565{
566 unsigned long flags;
567 u32 tmp;
568
Ash Willisb3a70d52006-03-27 13:20:40 +0200569 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);
Ash Willisb3a70d52006-03-27 13:20:40 +0200595}
596
Bill Pembertone23e7a12012-12-06 12:35:10 -0500597static int snd_als300_create(struct snd_card *card,
Takashi Iwai21a93142021-07-15 09:58:31 +0200598 struct pci_dev *pci, int chip_type)
Ash Willisb3a70d52006-03-27 13:20:40 +0200599{
Takashi Iwai21a93142021-07-15 09:58:31 +0200600 struct snd_als300 *chip = card->private_data;
Ash Willisb3a70d52006-03-27 13:20:40 +0200601 void *irq_handler;
602 int err;
603
Takashi Iwai21a93142021-07-15 09:58:31 +0200604 err = pcim_enable_device(pci);
Takashi Iwai5bd11522021-06-08 16:04:51 +0200605 if (err < 0)
Ash Willisb3a70d52006-03-27 13:20:40 +0200606 return err;
607
Takashi Iwai669f65e2021-01-14 13:54:11 +0100608 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28))) {
Takashi Iwaic778f7e2014-02-25 12:48:27 +0100609 dev_err(card->dev, "error setting 28bit DMA mask\n");
Ash Willisb3a70d52006-03-27 13:20:40 +0200610 return -ENXIO;
611 }
612 pci_set_master(pci);
613
Ash Willisb3a70d52006-03-27 13:20:40 +0200614 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
Takashi Iwai5bd11522021-06-08 16:04:51 +0200620 err = pci_request_regions(pci, "ALS300");
Takashi Iwai21a93142021-07-15 09:58:31 +0200621 if (err < 0)
Ash Willisb3a70d52006-03-27 13:20:40 +0200622 return err;
Takashi Iwai21a93142021-07-15 09:58:31 +0200623
Ash Willisb3a70d52006-03-27 13:20:40 +0200624 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
Takashi Iwai21a93142021-07-15 09:58:31 +0200631 if (devm_request_irq(&pci->dev, pci->irq, irq_handler, IRQF_SHARED,
632 KBUILD_MODNAME, chip)) {
Takashi Iwaic778f7e2014-02-25 12:48:27 +0100633 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Ash Willisb3a70d52006-03-27 13:20:40 +0200634 return -EBUSY;
635 }
636 chip->irq = pci->irq;
Takashi Iwaiea2eab52019-12-10 07:34:02 +0100637 card->sync_irq = chip->irq;
Takashi Iwai21a93142021-07-15 09:58:31 +0200638 card->private_free = snd_als300_free;
Ash Willisb3a70d52006-03-27 13:20:40 +0200639
640 snd_als300_init(chip);
641
Ash Willis65ff2352007-05-29 14:34:17 +0200642 err = snd_als300_ac97(chip);
643 if (err < 0) {
Takashi Iwaic778f7e2014-02-25 12:48:27 +0100644 dev_err(card->dev, "Could not create ac97\n");
Ash Willisb3a70d52006-03-27 13:20:40 +0200645 return err;
646 }
647
Takashi Iwai5bd11522021-06-08 16:04:51 +0200648 err = snd_als300_new_pcm(chip);
649 if (err < 0) {
Takashi Iwaic778f7e2014-02-25 12:48:27 +0100650 dev_err(card->dev, "Could not create PCM\n");
Ash Willisb3a70d52006-03-27 13:20:40 +0200651 return err;
652 }
653
Ash Willisb3a70d52006-03-27 13:20:40 +0200654 return 0;
655}
656
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200657#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200658static int snd_als300_suspend(struct device *dev)
Ash Willisb3a70d52006-03-27 13:20:40 +0200659{
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200660 struct snd_card *card = dev_get_drvdata(dev);
Ash Willisb3a70d52006-03-27 13:20:40 +0200661 struct snd_als300 *chip = card->private_data;
662
663 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Ash Willisb3a70d52006-03-27 13:20:40 +0200664 snd_ac97_suspend(chip->ac97);
Ash Willisb3a70d52006-03-27 13:20:40 +0200665 return 0;
666}
667
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200668static int snd_als300_resume(struct device *dev)
Ash Willisb3a70d52006-03-27 13:20:40 +0200669{
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200670 struct snd_card *card = dev_get_drvdata(dev);
Ash Willisb3a70d52006-03-27 13:20:40 +0200671 struct snd_als300 *chip = card->private_data;
672
Ash Willisb3a70d52006-03-27 13:20:40 +0200673 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}
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200679
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
Ash Willisb3a70d52006-03-27 13:20:40 +0200684#endif
685
Bill Pembertone23e7a12012-12-06 12:35:10 -0500686static int snd_als300_probe(struct pci_dev *pci,
Ash Willisb3a70d52006-03-27 13:20:40 +0200687 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
Takashi Iwai21a93142021-07-15 09:58:31 +0200701 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
702 sizeof(*chip), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100703 if (err < 0)
704 return err;
Takashi Iwaif263a2c2021-07-21 00:18:18 +0200705 chip = card->private_data;
Ash Willisb3a70d52006-03-27 13:20:40 +0200706
707 chip_type = pci_id->driver_data;
708
Takashi Iwai21a93142021-07-15 09:58:31 +0200709 err = snd_als300_create(card, pci, chip_type);
710 if (err < 0)
Ash Willisb3a70d52006-03-27 13:20:40 +0200711 return err;
Ash Willisb3a70d52006-03-27 13:20:40 +0200712
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
Takashi Iwai5bd11522021-06-08 16:04:51 +0200724 err = snd_card_register(card);
Takashi Iwai21a93142021-07-15 09:58:31 +0200725 if (err < 0)
Ash Willisb3a70d52006-03-27 13:20:40 +0200726 return err;
Takashi Iwai21a93142021-07-15 09:58:31 +0200727
Ash Willisb3a70d52006-03-27 13:20:40 +0200728 pci_set_drvdata(pci, card);
729 dev++;
730 return 0;
731}
732
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200733static struct pci_driver als300_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +0200734 .name = KBUILD_MODNAME,
Ash Willisb3a70d52006-03-27 13:20:40 +0200735 .id_table = snd_als300_ids,
736 .probe = snd_als300_probe,
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200737 .driver = {
738 .pm = SND_ALS300_PM_OPS,
739 },
Ash Willisb3a70d52006-03-27 13:20:40 +0200740};
741
Takashi Iwaie9f66d92012-04-24 12:25:00 +0200742module_pci_driver(als300_driver);