xref: /linux/sound/pci/als300.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
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 
84 enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
85 
86 MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
87 MODULE_DESCRIPTION("Avance Logic ALS300");
88 MODULE_LICENSE("GPL");
89 
90 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
91 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
92 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
93 
94 module_param_array(index, int, NULL, 0444);
95 MODULE_PARM_DESC(index, "Index value for ALS300 sound card.");
96 module_param_array(id, charp, NULL, 0444);
97 MODULE_PARM_DESC(id, "ID string for ALS300 sound card.");
98 module_param_array(enable, bool, NULL, 0444);
99 MODULE_PARM_DESC(enable, "Enable ALS300 sound card.");
100 
101 struct 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 
123 struct snd_als300_substream_data {
124 	int period_flipflop;
125 	int control_register;
126 	int block_counter_register;
127 };
128 
129 static 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 
135 MODULE_DEVICE_TABLE(pci, snd_als300_ids);
136 
snd_als300_gcr_read(unsigned long port,unsigned short reg)137 static 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 
snd_als300_gcr_write(unsigned long port,unsigned short reg,u32 val)143 static 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 */
snd_als300_set_irq_flag(struct snd_als300 * chip,int cmd)151 static 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 
snd_als300_free(struct snd_card * card)166 static 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 
snd_als300_interrupt(int irq,void * dev_id)173 static 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 
snd_als300plus_interrupt(int irq,void * dev_id)204 static 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 
snd_als300_ac97_read(struct snd_ac97 * ac97,unsigned short reg)241 static 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 
snd_als300_ac97_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)262 static 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 
snd_als300_ac97(struct snd_als300 * chip)276 static 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  */
303 static 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 
322 static 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 
snd_als300_playback_open(struct snd_pcm_substream * substream)341 static 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_obj(*data);
346 
347 	if (!data)
348 		return -ENOMEM;
349 	chip->playback_substream = substream;
350 	runtime->hw = snd_als300_playback_hw;
351 	runtime->private_data = data;
352 	data->control_register = PLAYBACK_CONTROL;
353 	data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
354 	return 0;
355 }
356 
snd_als300_playback_close(struct snd_pcm_substream * substream)357 static int snd_als300_playback_close(struct snd_pcm_substream *substream)
358 {
359 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
360 	struct snd_als300_substream_data *data;
361 
362 	data = substream->runtime->private_data;
363 	kfree(data);
364 	chip->playback_substream = NULL;
365 	return 0;
366 }
367 
snd_als300_capture_open(struct snd_pcm_substream * substream)368 static int snd_als300_capture_open(struct snd_pcm_substream *substream)
369 {
370 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
371 	struct snd_pcm_runtime *runtime = substream->runtime;
372 	struct snd_als300_substream_data *data = kzalloc_obj(*data);
373 
374 	if (!data)
375 		return -ENOMEM;
376 	chip->capture_substream = substream;
377 	runtime->hw = snd_als300_capture_hw;
378 	runtime->private_data = data;
379 	data->control_register = RECORD_CONTROL;
380 	data->block_counter_register = RECORD_BLOCK_COUNTER;
381 	return 0;
382 }
383 
snd_als300_capture_close(struct snd_pcm_substream * substream)384 static int snd_als300_capture_close(struct snd_pcm_substream *substream)
385 {
386 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
387 	struct snd_als300_substream_data *data;
388 
389 	data = substream->runtime->private_data;
390 	kfree(data);
391 	chip->capture_substream = NULL;
392 	return 0;
393 }
394 
snd_als300_playback_prepare(struct snd_pcm_substream * substream)395 static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
396 {
397 	u32 tmp;
398 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
399 	struct snd_pcm_runtime *runtime = substream->runtime;
400 	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
401 	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
402 
403 	guard(spinlock_irq)(&chip->reg_lock);
404 	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
405 	tmp &= ~TRANSFER_START;
406 
407 	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
408 						period_bytes, buffer_bytes);
409 
410 	/* set block size */
411 	tmp &= 0xffff0000;
412 	tmp |= period_bytes - 1;
413 	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
414 
415 	/* set dma area */
416 	snd_als300_gcr_write(chip->port, PLAYBACK_START,
417 					runtime->dma_addr);
418 	snd_als300_gcr_write(chip->port, PLAYBACK_END,
419 					runtime->dma_addr + buffer_bytes - 1);
420 	return 0;
421 }
422 
snd_als300_capture_prepare(struct snd_pcm_substream * substream)423 static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
424 {
425 	u32 tmp;
426 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
427 	struct snd_pcm_runtime *runtime = substream->runtime;
428 	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
429 	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
430 
431 	guard(spinlock_irq)(&chip->reg_lock);
432 	tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
433 	tmp &= ~TRANSFER_START;
434 
435 	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
436 							buffer_bytes);
437 
438 	/* set block size */
439 	tmp &= 0xffff0000;
440 	tmp |= period_bytes - 1;
441 
442 	/* set dma area */
443 	snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
444 	snd_als300_gcr_write(chip->port, RECORD_START,
445 					runtime->dma_addr);
446 	snd_als300_gcr_write(chip->port, RECORD_END,
447 					runtime->dma_addr + buffer_bytes - 1);
448 	return 0;
449 }
450 
snd_als300_trigger(struct snd_pcm_substream * substream,int cmd)451 static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
452 {
453 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
454 	u32 tmp;
455 	struct snd_als300_substream_data *data;
456 	unsigned short reg;
457 	int ret = 0;
458 
459 	data = substream->runtime->private_data;
460 	reg = data->control_register;
461 
462 	guard(spinlock)(&chip->reg_lock);
463 	switch (cmd) {
464 	case SNDRV_PCM_TRIGGER_START:
465 	case SNDRV_PCM_TRIGGER_RESUME:
466 		tmp = snd_als300_gcr_read(chip->port, reg);
467 		data->period_flipflop = 1;
468 		snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
469 		snd_als300_dbgplay("TRIGGER START\n");
470 		break;
471 	case SNDRV_PCM_TRIGGER_STOP:
472 	case SNDRV_PCM_TRIGGER_SUSPEND:
473 		tmp = snd_als300_gcr_read(chip->port, reg);
474 		snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
475 		snd_als300_dbgplay("TRIGGER STOP\n");
476 		break;
477 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
478 		tmp = snd_als300_gcr_read(chip->port, reg);
479 		snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
480 		snd_als300_dbgplay("TRIGGER PAUSE\n");
481 		break;
482 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
483 		tmp = snd_als300_gcr_read(chip->port, reg);
484 		snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
485 		snd_als300_dbgplay("TRIGGER RELEASE\n");
486 		break;
487 	default:
488 		snd_als300_dbgplay("TRIGGER INVALID\n");
489 		ret = -EINVAL;
490 	}
491 	return ret;
492 }
493 
snd_als300_pointer(struct snd_pcm_substream * substream)494 static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
495 {
496 	u16 current_ptr;
497 	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
498 	struct snd_als300_substream_data *data;
499 	unsigned short period_bytes;
500 
501 	data = substream->runtime->private_data;
502 	period_bytes = snd_pcm_lib_period_bytes(substream);
503 
504 	scoped_guard(spinlock, &chip->reg_lock) {
505 		current_ptr = (u16) snd_als300_gcr_read(chip->port,
506 							data->block_counter_register) + 4;
507 	}
508 	if (current_ptr > period_bytes)
509 		current_ptr = 0;
510 	else
511 		current_ptr = period_bytes - current_ptr;
512 
513 	if (data->period_flipflop == 0)
514 		current_ptr += period_bytes;
515 	snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
516 	return bytes_to_frames(substream->runtime, current_ptr);
517 }
518 
519 static const struct snd_pcm_ops snd_als300_playback_ops = {
520 	.open =		snd_als300_playback_open,
521 	.close =	snd_als300_playback_close,
522 	.prepare =	snd_als300_playback_prepare,
523 	.trigger =	snd_als300_trigger,
524 	.pointer =	snd_als300_pointer,
525 };
526 
527 static const struct snd_pcm_ops snd_als300_capture_ops = {
528 	.open =		snd_als300_capture_open,
529 	.close =	snd_als300_capture_close,
530 	.prepare =	snd_als300_capture_prepare,
531 	.trigger =	snd_als300_trigger,
532 	.pointer =	snd_als300_pointer,
533 };
534 
snd_als300_new_pcm(struct snd_als300 * chip)535 static int snd_als300_new_pcm(struct snd_als300 *chip)
536 {
537 	struct snd_pcm *pcm;
538 	int err;
539 
540 	err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
541 	if (err < 0)
542 		return err;
543 	pcm->private_data = chip;
544 	strscpy(pcm->name, "ALS300");
545 	chip->pcm = pcm;
546 
547 	/* set operators */
548 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
549 				&snd_als300_playback_ops);
550 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
551 				&snd_als300_capture_ops);
552 
553 	/* pre-allocation of buffers */
554 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
555 				       64*1024, 64*1024);
556 	return 0;
557 }
558 
snd_als300_init(struct snd_als300 * chip)559 static void snd_als300_init(struct snd_als300 *chip)
560 {
561 	u32 tmp;
562 
563 	guard(spinlock_irqsave)(&chip->reg_lock);
564 	chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
565 								& 0x0000000F;
566 	/* Setup DRAM */
567 	tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
568 	snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
569 						(tmp | DRAM_MODE_2)
570 						& ~WRITE_TRANS_START);
571 
572 	/* Enable IRQ output */
573 	snd_als300_set_irq_flag(chip, IRQ_ENABLE);
574 
575 	/* Unmute hardware devices so their outputs get routed to
576 	 * the onboard mixer */
577 	tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
578 	snd_als300_gcr_write(chip->port, MISC_CONTROL,
579 			tmp | VMUTE_NORMAL | MMUTE_NORMAL);
580 
581 	/* Reset volumes */
582 	snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
583 
584 	/* Make sure playback transfer is stopped */
585 	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
586 	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
587 			tmp & ~TRANSFER_START);
588 }
589 
snd_als300_create(struct snd_card * card,struct pci_dev * pci,int chip_type)590 static int snd_als300_create(struct snd_card *card,
591 			     struct pci_dev *pci, int chip_type)
592 {
593 	struct snd_als300 *chip = card->private_data;
594 	void *irq_handler;
595 	int err;
596 
597 	err = pcim_enable_device(pci);
598 	if (err < 0)
599 		return err;
600 
601 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28))) {
602 		dev_err(card->dev, "error setting 28bit DMA mask\n");
603 		return -ENXIO;
604 	}
605 	pci_set_master(pci);
606 
607 	chip->card = card;
608 	chip->pci = pci;
609 	chip->irq = -1;
610 	chip->chip_type = chip_type;
611 	spin_lock_init(&chip->reg_lock);
612 
613 	err = pcim_request_all_regions(pci, "ALS300");
614 	if (err < 0)
615 		return err;
616 
617 	chip->port = pci_resource_start(pci, 0);
618 
619 	if (chip->chip_type == DEVICE_ALS300_PLUS)
620 		irq_handler = snd_als300plus_interrupt;
621 	else
622 		irq_handler = snd_als300_interrupt;
623 
624 	if (devm_request_irq(&pci->dev, pci->irq, irq_handler, IRQF_SHARED,
625 			     KBUILD_MODNAME, chip)) {
626 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
627 		return -EBUSY;
628 	}
629 	chip->irq = pci->irq;
630 	card->sync_irq = chip->irq;
631 	card->private_free = snd_als300_free;
632 
633 	snd_als300_init(chip);
634 
635 	err = snd_als300_ac97(chip);
636 	if (err < 0) {
637 		dev_err(card->dev, "Could not create ac97\n");
638 		return err;
639 	}
640 
641 	err = snd_als300_new_pcm(chip);
642 	if (err < 0) {
643 		dev_err(card->dev, "Could not create PCM\n");
644 		return err;
645 	}
646 
647 	return 0;
648 }
649 
snd_als300_suspend(struct device * dev)650 static int snd_als300_suspend(struct device *dev)
651 {
652 	struct snd_card *card = dev_get_drvdata(dev);
653 	struct snd_als300 *chip = card->private_data;
654 
655 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
656 	snd_ac97_suspend(chip->ac97);
657 	return 0;
658 }
659 
snd_als300_resume(struct device * dev)660 static int snd_als300_resume(struct device *dev)
661 {
662 	struct snd_card *card = dev_get_drvdata(dev);
663 	struct snd_als300 *chip = card->private_data;
664 
665 	snd_als300_init(chip);
666 	snd_ac97_resume(chip->ac97);
667 
668 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
669 	return 0;
670 }
671 
672 static DEFINE_SIMPLE_DEV_PM_OPS(snd_als300_pm, snd_als300_suspend, snd_als300_resume);
673 
snd_als300_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)674 static int snd_als300_probe(struct pci_dev *pci,
675                              const struct pci_device_id *pci_id)
676 {
677 	static int dev;
678 	struct snd_card *card;
679 	struct snd_als300 *chip;
680 	int err, chip_type;
681 
682 	if (dev >= SNDRV_CARDS)
683 		return -ENODEV;
684 	if (!enable[dev]) {
685 		dev++;
686 		return -ENOENT;
687 	}
688 
689 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
690 				sizeof(*chip), &card);
691 	if (err < 0)
692 		return err;
693 	chip = card->private_data;
694 
695 	chip_type = pci_id->driver_data;
696 
697 	err = snd_als300_create(card, pci, chip_type);
698 	if (err < 0)
699 		goto error;
700 
701 	strscpy(card->driver, "ALS300");
702 	if (chip->chip_type == DEVICE_ALS300_PLUS)
703 		/* don't know much about ALS300+ yet
704 		 * print revision number for now */
705 		sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
706 	else
707 		sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
708 							chip->revision - 1);
709 	sprintf(card->longname, "%s at 0x%lx irq %i",
710 				card->shortname, chip->port, chip->irq);
711 
712 	err = snd_card_register(card);
713 	if (err < 0)
714 		goto error;
715 
716 	pci_set_drvdata(pci, card);
717 	dev++;
718 	return 0;
719 
720  error:
721 	snd_card_free(card);
722 	return err;
723 }
724 
725 static struct pci_driver als300_driver = {
726 	.name = KBUILD_MODNAME,
727 	.id_table = snd_als300_ids,
728 	.probe = snd_als300_probe,
729 	.driver = {
730 		.pm = &snd_als300_pm,
731 	},
732 };
733 
734 module_pci_driver(als300_driver);
735