xref: /linux/sound/pci/sis7019.c (revision a45ff5994c9cde41af627c46abb9f32beae68943)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for SiS7019 Audio Accelerator
4  *
5  *  Copyright (C) 2004-2007, David Dillow
6  *  Written by David Dillow <dave@thedillows.org>
7  *  Inspired by the Trident 4D-WaveDX/NX driver.
8  *
9  *  All rights reserved.
10  */
11 
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <sound/core.h>
20 #include <sound/ac97_codec.h>
21 #include <sound/initval.h>
22 #include "sis7019.h"
23 
24 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
25 MODULE_DESCRIPTION("SiS7019");
26 MODULE_LICENSE("GPL");
27 MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
28 
29 static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
30 static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
31 static bool enable = 1;
32 static int codecs = 1;
33 
34 module_param(index, int, 0444);
35 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
36 module_param(id, charp, 0444);
37 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
38 module_param(enable, bool, 0444);
39 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
40 module_param(codecs, int, 0444);
41 MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
42 
43 static const struct pci_device_id snd_sis7019_ids[] = {
44 	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
45 	{ 0, }
46 };
47 
48 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
49 
50 /* There are three timing modes for the voices.
51  *
52  * For both playback and capture, when the buffer is one or two periods long,
53  * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
54  * to let us know when the periods have ended.
55  *
56  * When performing playback with more than two periods per buffer, we set
57  * the "Stop Sample Offset" and tell the hardware to interrupt us when we
58  * reach it. We then update the offset and continue on until we are
59  * interrupted for the next period.
60  *
61  * Capture channels do not have a SSO, so we allocate a playback channel to
62  * use as a timer for the capture periods. We use the SSO on the playback
63  * channel to clock out virtual periods, and adjust the virtual period length
64  * to maintain synchronization. This algorithm came from the Trident driver.
65  *
66  * FIXME: It'd be nice to make use of some of the synth features in the
67  * hardware, but a woeful lack of documentation is a significant roadblock.
68  */
69 struct voice {
70 	u16 flags;
71 #define 	VOICE_IN_USE		1
72 #define 	VOICE_CAPTURE		2
73 #define 	VOICE_SSO_TIMING	4
74 #define 	VOICE_SYNC_TIMING	8
75 	u16 sync_cso;
76 	u16 period_size;
77 	u16 buffer_size;
78 	u16 sync_period_size;
79 	u16 sync_buffer_size;
80 	u32 sso;
81 	u32 vperiod;
82 	struct snd_pcm_substream *substream;
83 	struct voice *timing;
84 	void __iomem *ctrl_base;
85 	void __iomem *wave_base;
86 	void __iomem *sync_base;
87 	int num;
88 };
89 
90 /* We need four pages to store our wave parameters during a suspend. If
91  * we're not doing power management, we still need to allocate a page
92  * for the silence buffer.
93  */
94 #ifdef CONFIG_PM_SLEEP
95 #define SIS_SUSPEND_PAGES	4
96 #else
97 #define SIS_SUSPEND_PAGES	1
98 #endif
99 
100 struct sis7019 {
101 	unsigned long ioport;
102 	void __iomem *ioaddr;
103 	int irq;
104 	int codecs_present;
105 
106 	struct pci_dev *pci;
107 	struct snd_pcm *pcm;
108 	struct snd_card *card;
109 	struct snd_ac97 *ac97[3];
110 
111 	/* Protect against more than one thread hitting the AC97
112 	 * registers (in a more polite manner than pounding the hardware
113 	 * semaphore)
114 	 */
115 	struct mutex ac97_mutex;
116 
117 	/* voice_lock protects allocation/freeing of the voice descriptions
118 	 */
119 	spinlock_t voice_lock;
120 
121 	struct voice voices[64];
122 	struct voice capture_voice;
123 
124 	/* Allocate pages to store the internal wave state during
125 	 * suspends. When we're operating, this can be used as a silence
126 	 * buffer for a timing channel.
127 	 */
128 	void *suspend_state[SIS_SUSPEND_PAGES];
129 
130 	int silence_users;
131 	dma_addr_t silence_dma_addr;
132 };
133 
134 /* These values are also used by the module param 'codecs' to indicate
135  * which codecs should be present.
136  */
137 #define SIS_PRIMARY_CODEC_PRESENT	0x0001
138 #define SIS_SECONDARY_CODEC_PRESENT	0x0002
139 #define SIS_TERTIARY_CODEC_PRESENT	0x0004
140 
141 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
142  * documented range of 8-0xfff8 samples. Given that they are 0-based,
143  * that places our period/buffer range at 9-0xfff9 samples. That makes the
144  * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
145  * max samples / min samples gives us the max periods in a buffer.
146  *
147  * We'll add a constraint upon open that limits the period and buffer sample
148  * size to values that are legal for the hardware.
149  */
150 static const struct snd_pcm_hardware sis_playback_hw_info = {
151 	.info = (SNDRV_PCM_INFO_MMAP |
152 		 SNDRV_PCM_INFO_MMAP_VALID |
153 		 SNDRV_PCM_INFO_INTERLEAVED |
154 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
155 		 SNDRV_PCM_INFO_SYNC_START |
156 		 SNDRV_PCM_INFO_RESUME),
157 	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
158 		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
159 	.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
160 	.rate_min = 4000,
161 	.rate_max = 48000,
162 	.channels_min = 1,
163 	.channels_max = 2,
164 	.buffer_bytes_max = (0xfff9 * 4),
165 	.period_bytes_min = 9,
166 	.period_bytes_max = (0xfff9 * 4),
167 	.periods_min = 1,
168 	.periods_max = (0xfff9 / 9),
169 };
170 
171 static const struct snd_pcm_hardware sis_capture_hw_info = {
172 	.info = (SNDRV_PCM_INFO_MMAP |
173 		 SNDRV_PCM_INFO_MMAP_VALID |
174 		 SNDRV_PCM_INFO_INTERLEAVED |
175 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
176 		 SNDRV_PCM_INFO_SYNC_START |
177 		 SNDRV_PCM_INFO_RESUME),
178 	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
179 		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
180 	.rates = SNDRV_PCM_RATE_48000,
181 	.rate_min = 4000,
182 	.rate_max = 48000,
183 	.channels_min = 1,
184 	.channels_max = 2,
185 	.buffer_bytes_max = (0xfff9 * 4),
186 	.period_bytes_min = 9,
187 	.period_bytes_max = (0xfff9 * 4),
188 	.periods_min = 1,
189 	.periods_max = (0xfff9 / 9),
190 };
191 
192 static void sis_update_sso(struct voice *voice, u16 period)
193 {
194 	void __iomem *base = voice->ctrl_base;
195 
196 	voice->sso += period;
197 	if (voice->sso >= voice->buffer_size)
198 		voice->sso -= voice->buffer_size;
199 
200 	/* Enforce the documented hardware minimum offset */
201 	if (voice->sso < 8)
202 		voice->sso = 8;
203 
204 	/* The SSO is in the upper 16 bits of the register. */
205 	writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
206 }
207 
208 static void sis_update_voice(struct voice *voice)
209 {
210 	if (voice->flags & VOICE_SSO_TIMING) {
211 		sis_update_sso(voice, voice->period_size);
212 	} else if (voice->flags & VOICE_SYNC_TIMING) {
213 		int sync;
214 
215 		/* If we've not hit the end of the virtual period, update
216 		 * our records and keep going.
217 		 */
218 		if (voice->vperiod > voice->period_size) {
219 			voice->vperiod -= voice->period_size;
220 			if (voice->vperiod < voice->period_size)
221 				sis_update_sso(voice, voice->vperiod);
222 			else
223 				sis_update_sso(voice, voice->period_size);
224 			return;
225 		}
226 
227 		/* Calculate our relative offset between the target and
228 		 * the actual CSO value. Since we're operating in a loop,
229 		 * if the value is more than half way around, we can
230 		 * consider ourselves wrapped.
231 		 */
232 		sync = voice->sync_cso;
233 		sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
234 		if (sync > (voice->sync_buffer_size / 2))
235 			sync -= voice->sync_buffer_size;
236 
237 		/* If sync is positive, then we interrupted too early, and
238 		 * we'll need to come back in a few samples and try again.
239 		 * There's a minimum wait, as it takes some time for the DMA
240 		 * engine to startup, etc...
241 		 */
242 		if (sync > 0) {
243 			if (sync < 16)
244 				sync = 16;
245 			sis_update_sso(voice, sync);
246 			return;
247 		}
248 
249 		/* Ok, we interrupted right on time, or (hopefully) just
250 		 * a bit late. We'll adjst our next waiting period based
251 		 * on how close we got.
252 		 *
253 		 * We need to stay just behind the actual channel to ensure
254 		 * it really is past a period when we get our interrupt --
255 		 * otherwise we'll fall into the early code above and have
256 		 * a minimum wait time, which makes us quite late here,
257 		 * eating into the user's time to refresh the buffer, esp.
258 		 * if using small periods.
259 		 *
260 		 * If we're less than 9 samples behind, we're on target.
261 		 * Otherwise, shorten the next vperiod by the amount we've
262 		 * been delayed.
263 		 */
264 		if (sync > -9)
265 			voice->vperiod = voice->sync_period_size + 1;
266 		else
267 			voice->vperiod = voice->sync_period_size + sync + 10;
268 
269 		if (voice->vperiod < voice->buffer_size) {
270 			sis_update_sso(voice, voice->vperiod);
271 			voice->vperiod = 0;
272 		} else
273 			sis_update_sso(voice, voice->period_size);
274 
275 		sync = voice->sync_cso + voice->sync_period_size;
276 		if (sync >= voice->sync_buffer_size)
277 			sync -= voice->sync_buffer_size;
278 		voice->sync_cso = sync;
279 	}
280 
281 	snd_pcm_period_elapsed(voice->substream);
282 }
283 
284 static void sis_voice_irq(u32 status, struct voice *voice)
285 {
286 	int bit;
287 
288 	while (status) {
289 		bit = __ffs(status);
290 		status >>= bit + 1;
291 		voice += bit;
292 		sis_update_voice(voice);
293 		voice++;
294 	}
295 }
296 
297 static irqreturn_t sis_interrupt(int irq, void *dev)
298 {
299 	struct sis7019 *sis = dev;
300 	unsigned long io = sis->ioport;
301 	struct voice *voice;
302 	u32 intr, status;
303 
304 	/* We only use the DMA interrupts, and we don't enable any other
305 	 * source of interrupts. But, it is possible to see an interrupt
306 	 * status that didn't actually interrupt us, so eliminate anything
307 	 * we're not expecting to avoid falsely claiming an IRQ, and an
308 	 * ensuing endless loop.
309 	 */
310 	intr = inl(io + SIS_GISR);
311 	intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
312 		SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
313 	if (!intr)
314 		return IRQ_NONE;
315 
316 	do {
317 		status = inl(io + SIS_PISR_A);
318 		if (status) {
319 			sis_voice_irq(status, sis->voices);
320 			outl(status, io + SIS_PISR_A);
321 		}
322 
323 		status = inl(io + SIS_PISR_B);
324 		if (status) {
325 			sis_voice_irq(status, &sis->voices[32]);
326 			outl(status, io + SIS_PISR_B);
327 		}
328 
329 		status = inl(io + SIS_RISR);
330 		if (status) {
331 			voice = &sis->capture_voice;
332 			if (!voice->timing)
333 				snd_pcm_period_elapsed(voice->substream);
334 
335 			outl(status, io + SIS_RISR);
336 		}
337 
338 		outl(intr, io + SIS_GISR);
339 		intr = inl(io + SIS_GISR);
340 		intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
341 			SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
342 	} while (intr);
343 
344 	return IRQ_HANDLED;
345 }
346 
347 static u32 sis_rate_to_delta(unsigned int rate)
348 {
349 	u32 delta;
350 
351 	/* This was copied from the trident driver, but it seems its gotten
352 	 * around a bit... nevertheless, it works well.
353 	 *
354 	 * We special case 44100 and 8000 since rounding with the equation
355 	 * does not give us an accurate enough value. For 11025 and 22050
356 	 * the equation gives us the best answer. All other frequencies will
357 	 * also use the equation. JDW
358 	 */
359 	if (rate == 44100)
360 		delta = 0xeb3;
361 	else if (rate == 8000)
362 		delta = 0x2ab;
363 	else if (rate == 48000)
364 		delta = 0x1000;
365 	else
366 		delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
367 	return delta;
368 }
369 
370 static void __sis_map_silence(struct sis7019 *sis)
371 {
372 	/* Helper function: must hold sis->voice_lock on entry */
373 	if (!sis->silence_users)
374 		sis->silence_dma_addr = dma_map_single(&sis->pci->dev,
375 						sis->suspend_state[0],
376 						4096, DMA_TO_DEVICE);
377 	sis->silence_users++;
378 }
379 
380 static void __sis_unmap_silence(struct sis7019 *sis)
381 {
382 	/* Helper function: must hold sis->voice_lock on entry */
383 	sis->silence_users--;
384 	if (!sis->silence_users)
385 		dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
386 					DMA_TO_DEVICE);
387 }
388 
389 static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
390 {
391 	unsigned long flags;
392 
393 	spin_lock_irqsave(&sis->voice_lock, flags);
394 	if (voice->timing) {
395 		__sis_unmap_silence(sis);
396 		voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
397 						VOICE_SYNC_TIMING);
398 		voice->timing = NULL;
399 	}
400 	voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
401 	spin_unlock_irqrestore(&sis->voice_lock, flags);
402 }
403 
404 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
405 {
406 	/* Must hold the voice_lock on entry */
407 	struct voice *voice;
408 	int i;
409 
410 	for (i = 0; i < 64; i++) {
411 		voice = &sis->voices[i];
412 		if (voice->flags & VOICE_IN_USE)
413 			continue;
414 		voice->flags |= VOICE_IN_USE;
415 		goto found_one;
416 	}
417 	voice = NULL;
418 
419 found_one:
420 	return voice;
421 }
422 
423 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
424 {
425 	struct voice *voice;
426 	unsigned long flags;
427 
428 	spin_lock_irqsave(&sis->voice_lock, flags);
429 	voice = __sis_alloc_playback_voice(sis);
430 	spin_unlock_irqrestore(&sis->voice_lock, flags);
431 
432 	return voice;
433 }
434 
435 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
436 					struct snd_pcm_hw_params *hw_params)
437 {
438 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
439 	struct snd_pcm_runtime *runtime = substream->runtime;
440 	struct voice *voice = runtime->private_data;
441 	unsigned int period_size, buffer_size;
442 	unsigned long flags;
443 	int needed;
444 
445 	/* If there are one or two periods per buffer, we don't need a
446 	 * timing voice, as we can use the capture channel's interrupts
447 	 * to clock out the periods.
448 	 */
449 	period_size = params_period_size(hw_params);
450 	buffer_size = params_buffer_size(hw_params);
451 	needed = (period_size != buffer_size &&
452 			period_size != (buffer_size / 2));
453 
454 	if (needed && !voice->timing) {
455 		spin_lock_irqsave(&sis->voice_lock, flags);
456 		voice->timing = __sis_alloc_playback_voice(sis);
457 		if (voice->timing)
458 			__sis_map_silence(sis);
459 		spin_unlock_irqrestore(&sis->voice_lock, flags);
460 		if (!voice->timing)
461 			return -ENOMEM;
462 		voice->timing->substream = substream;
463 	} else if (!needed && voice->timing) {
464 		sis_free_voice(sis, voice);
465 		voice->timing = NULL;
466 	}
467 
468 	return 0;
469 }
470 
471 static int sis_playback_open(struct snd_pcm_substream *substream)
472 {
473 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
474 	struct snd_pcm_runtime *runtime = substream->runtime;
475 	struct voice *voice;
476 
477 	voice = sis_alloc_playback_voice(sis);
478 	if (!voice)
479 		return -EAGAIN;
480 
481 	voice->substream = substream;
482 	runtime->private_data = voice;
483 	runtime->hw = sis_playback_hw_info;
484 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
485 						9, 0xfff9);
486 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
487 						9, 0xfff9);
488 	snd_pcm_set_sync(substream);
489 	return 0;
490 }
491 
492 static int sis_substream_close(struct snd_pcm_substream *substream)
493 {
494 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
495 	struct snd_pcm_runtime *runtime = substream->runtime;
496 	struct voice *voice = runtime->private_data;
497 
498 	sis_free_voice(sis, voice);
499 	return 0;
500 }
501 
502 static int sis_playback_hw_params(struct snd_pcm_substream *substream,
503 					struct snd_pcm_hw_params *hw_params)
504 {
505 	return snd_pcm_lib_malloc_pages(substream,
506 					params_buffer_bytes(hw_params));
507 }
508 
509 static int sis_hw_free(struct snd_pcm_substream *substream)
510 {
511 	return snd_pcm_lib_free_pages(substream);
512 }
513 
514 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
515 {
516 	struct snd_pcm_runtime *runtime = substream->runtime;
517 	struct voice *voice = runtime->private_data;
518 	void __iomem *ctrl_base = voice->ctrl_base;
519 	void __iomem *wave_base = voice->wave_base;
520 	u32 format, dma_addr, control, sso_eso, delta, reg;
521 	u16 leo;
522 
523 	/* We rely on the PCM core to ensure that the parameters for this
524 	 * substream do not change on us while we're programming the HW.
525 	 */
526 	format = 0;
527 	if (snd_pcm_format_width(runtime->format) == 8)
528 		format |= SIS_PLAY_DMA_FORMAT_8BIT;
529 	if (!snd_pcm_format_signed(runtime->format))
530 		format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
531 	if (runtime->channels == 1)
532 		format |= SIS_PLAY_DMA_FORMAT_MONO;
533 
534 	/* The baseline setup is for a single period per buffer, and
535 	 * we add bells and whistles as needed from there.
536 	 */
537 	dma_addr = runtime->dma_addr;
538 	leo = runtime->buffer_size - 1;
539 	control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
540 	sso_eso = leo;
541 
542 	if (runtime->period_size == (runtime->buffer_size / 2)) {
543 		control |= SIS_PLAY_DMA_INTR_AT_MLP;
544 	} else if (runtime->period_size != runtime->buffer_size) {
545 		voice->flags |= VOICE_SSO_TIMING;
546 		voice->sso = runtime->period_size - 1;
547 		voice->period_size = runtime->period_size;
548 		voice->buffer_size = runtime->buffer_size;
549 
550 		control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
551 		control |= SIS_PLAY_DMA_INTR_AT_SSO;
552 		sso_eso |= (runtime->period_size - 1) << 16;
553 	}
554 
555 	delta = sis_rate_to_delta(runtime->rate);
556 
557 	/* Ok, we're ready to go, set up the channel.
558 	 */
559 	writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
560 	writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
561 	writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
562 	writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
563 
564 	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
565 		writel(0, wave_base + reg);
566 
567 	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
568 	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
569 	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
570 			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
571 			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
572 			wave_base + SIS_WAVE_CHANNEL_CONTROL);
573 
574 	/* Force PCI writes to post. */
575 	readl(ctrl_base);
576 
577 	return 0;
578 }
579 
580 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
581 {
582 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
583 	unsigned long io = sis->ioport;
584 	struct snd_pcm_substream *s;
585 	struct voice *voice;
586 	void *chip;
587 	int starting;
588 	u32 record = 0;
589 	u32 play[2] = { 0, 0 };
590 
591 	/* No locks needed, as the PCM core will hold the locks on the
592 	 * substreams, and the HW will only start/stop the indicated voices
593 	 * without changing the state of the others.
594 	 */
595 	switch (cmd) {
596 	case SNDRV_PCM_TRIGGER_START:
597 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
598 	case SNDRV_PCM_TRIGGER_RESUME:
599 		starting = 1;
600 		break;
601 	case SNDRV_PCM_TRIGGER_STOP:
602 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
603 	case SNDRV_PCM_TRIGGER_SUSPEND:
604 		starting = 0;
605 		break;
606 	default:
607 		return -EINVAL;
608 	}
609 
610 	snd_pcm_group_for_each_entry(s, substream) {
611 		/* Make sure it is for us... */
612 		chip = snd_pcm_substream_chip(s);
613 		if (chip != sis)
614 			continue;
615 
616 		voice = s->runtime->private_data;
617 		if (voice->flags & VOICE_CAPTURE) {
618 			record |= 1 << voice->num;
619 			voice = voice->timing;
620 		}
621 
622 		/* voice could be NULL if this a recording stream, and it
623 		 * doesn't have an external timing channel.
624 		 */
625 		if (voice)
626 			play[voice->num / 32] |= 1 << (voice->num & 0x1f);
627 
628 		snd_pcm_trigger_done(s, substream);
629 	}
630 
631 	if (starting) {
632 		if (record)
633 			outl(record, io + SIS_RECORD_START_REG);
634 		if (play[0])
635 			outl(play[0], io + SIS_PLAY_START_A_REG);
636 		if (play[1])
637 			outl(play[1], io + SIS_PLAY_START_B_REG);
638 	} else {
639 		if (record)
640 			outl(record, io + SIS_RECORD_STOP_REG);
641 		if (play[0])
642 			outl(play[0], io + SIS_PLAY_STOP_A_REG);
643 		if (play[1])
644 			outl(play[1], io + SIS_PLAY_STOP_B_REG);
645 	}
646 	return 0;
647 }
648 
649 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
650 {
651 	struct snd_pcm_runtime *runtime = substream->runtime;
652 	struct voice *voice = runtime->private_data;
653 	u32 cso;
654 
655 	cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
656 	cso &= 0xffff;
657 	return cso;
658 }
659 
660 static int sis_capture_open(struct snd_pcm_substream *substream)
661 {
662 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
663 	struct snd_pcm_runtime *runtime = substream->runtime;
664 	struct voice *voice = &sis->capture_voice;
665 	unsigned long flags;
666 
667 	/* FIXME: The driver only supports recording from one channel
668 	 * at the moment, but it could support more.
669 	 */
670 	spin_lock_irqsave(&sis->voice_lock, flags);
671 	if (voice->flags & VOICE_IN_USE)
672 		voice = NULL;
673 	else
674 		voice->flags |= VOICE_IN_USE;
675 	spin_unlock_irqrestore(&sis->voice_lock, flags);
676 
677 	if (!voice)
678 		return -EAGAIN;
679 
680 	voice->substream = substream;
681 	runtime->private_data = voice;
682 	runtime->hw = sis_capture_hw_info;
683 	runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
684 	snd_pcm_limit_hw_rates(runtime);
685 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
686 						9, 0xfff9);
687 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
688 						9, 0xfff9);
689 	snd_pcm_set_sync(substream);
690 	return 0;
691 }
692 
693 static int sis_capture_hw_params(struct snd_pcm_substream *substream,
694 					struct snd_pcm_hw_params *hw_params)
695 {
696 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
697 	int rc;
698 
699 	rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
700 						params_rate(hw_params));
701 	if (rc)
702 		goto out;
703 
704 	rc = snd_pcm_lib_malloc_pages(substream,
705 					params_buffer_bytes(hw_params));
706 	if (rc < 0)
707 		goto out;
708 
709 	rc = sis_alloc_timing_voice(substream, hw_params);
710 
711 out:
712 	return rc;
713 }
714 
715 static void sis_prepare_timing_voice(struct voice *voice,
716 					struct snd_pcm_substream *substream)
717 {
718 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
719 	struct snd_pcm_runtime *runtime = substream->runtime;
720 	struct voice *timing = voice->timing;
721 	void __iomem *play_base = timing->ctrl_base;
722 	void __iomem *wave_base = timing->wave_base;
723 	u16 buffer_size, period_size;
724 	u32 format, control, sso_eso, delta;
725 	u32 vperiod, sso, reg;
726 
727 	/* Set our initial buffer and period as large as we can given a
728 	 * single page of silence.
729 	 */
730 	buffer_size = 4096 / runtime->channels;
731 	buffer_size /= snd_pcm_format_size(runtime->format, 1);
732 	period_size = buffer_size;
733 
734 	/* Initially, we want to interrupt just a bit behind the end of
735 	 * the period we're clocking out. 12 samples seems to give a good
736 	 * delay.
737 	 *
738 	 * We want to spread our interrupts throughout the virtual period,
739 	 * so that we don't end up with two interrupts back to back at the
740 	 * end -- this helps minimize the effects of any jitter. Adjust our
741 	 * clocking period size so that the last period is at least a fourth
742 	 * of a full period.
743 	 *
744 	 * This is all moot if we don't need to use virtual periods.
745 	 */
746 	vperiod = runtime->period_size + 12;
747 	if (vperiod > period_size) {
748 		u16 tail = vperiod % period_size;
749 		u16 quarter_period = period_size / 4;
750 
751 		if (tail && tail < quarter_period) {
752 			u16 loops = vperiod / period_size;
753 
754 			tail = quarter_period - tail;
755 			tail += loops - 1;
756 			tail /= loops;
757 			period_size -= tail;
758 		}
759 
760 		sso = period_size - 1;
761 	} else {
762 		/* The initial period will fit inside the buffer, so we
763 		 * don't need to use virtual periods -- disable them.
764 		 */
765 		period_size = runtime->period_size;
766 		sso = vperiod - 1;
767 		vperiod = 0;
768 	}
769 
770 	/* The interrupt handler implements the timing synchronization, so
771 	 * setup its state.
772 	 */
773 	timing->flags |= VOICE_SYNC_TIMING;
774 	timing->sync_base = voice->ctrl_base;
775 	timing->sync_cso = runtime->period_size;
776 	timing->sync_period_size = runtime->period_size;
777 	timing->sync_buffer_size = runtime->buffer_size;
778 	timing->period_size = period_size;
779 	timing->buffer_size = buffer_size;
780 	timing->sso = sso;
781 	timing->vperiod = vperiod;
782 
783 	/* Using unsigned samples with the all-zero silence buffer
784 	 * forces the output to the lower rail, killing playback.
785 	 * So ignore unsigned vs signed -- it doesn't change the timing.
786 	 */
787 	format = 0;
788 	if (snd_pcm_format_width(runtime->format) == 8)
789 		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
790 	if (runtime->channels == 1)
791 		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
792 
793 	control = timing->buffer_size - 1;
794 	control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
795 	sso_eso = timing->buffer_size - 1;
796 	sso_eso |= timing->sso << 16;
797 
798 	delta = sis_rate_to_delta(runtime->rate);
799 
800 	/* We've done the math, now configure the channel.
801 	 */
802 	writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
803 	writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
804 	writel(control, play_base + SIS_PLAY_DMA_CONTROL);
805 	writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
806 
807 	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
808 		writel(0, wave_base + reg);
809 
810 	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
811 	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
812 	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
813 			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
814 			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
815 			wave_base + SIS_WAVE_CHANNEL_CONTROL);
816 }
817 
818 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
819 {
820 	struct snd_pcm_runtime *runtime = substream->runtime;
821 	struct voice *voice = runtime->private_data;
822 	void __iomem *rec_base = voice->ctrl_base;
823 	u32 format, dma_addr, control;
824 	u16 leo;
825 
826 	/* We rely on the PCM core to ensure that the parameters for this
827 	 * substream do not change on us while we're programming the HW.
828 	 */
829 	format = 0;
830 	if (snd_pcm_format_width(runtime->format) == 8)
831 		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
832 	if (!snd_pcm_format_signed(runtime->format))
833 		format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
834 	if (runtime->channels == 1)
835 		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
836 
837 	dma_addr = runtime->dma_addr;
838 	leo = runtime->buffer_size - 1;
839 	control = leo | SIS_CAPTURE_DMA_LOOP;
840 
841 	/* If we've got more than two periods per buffer, then we have
842 	 * use a timing voice to clock out the periods. Otherwise, we can
843 	 * use the capture channel's interrupts.
844 	 */
845 	if (voice->timing) {
846 		sis_prepare_timing_voice(voice, substream);
847 	} else {
848 		control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
849 		if (runtime->period_size != runtime->buffer_size)
850 			control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
851 	}
852 
853 	writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
854 	writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
855 	writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
856 
857 	/* Force the writes to post. */
858 	readl(rec_base);
859 
860 	return 0;
861 }
862 
863 static const struct snd_pcm_ops sis_playback_ops = {
864 	.open = sis_playback_open,
865 	.close = sis_substream_close,
866 	.ioctl = snd_pcm_lib_ioctl,
867 	.hw_params = sis_playback_hw_params,
868 	.hw_free = sis_hw_free,
869 	.prepare = sis_pcm_playback_prepare,
870 	.trigger = sis_pcm_trigger,
871 	.pointer = sis_pcm_pointer,
872 };
873 
874 static const struct snd_pcm_ops sis_capture_ops = {
875 	.open = sis_capture_open,
876 	.close = sis_substream_close,
877 	.ioctl = snd_pcm_lib_ioctl,
878 	.hw_params = sis_capture_hw_params,
879 	.hw_free = sis_hw_free,
880 	.prepare = sis_pcm_capture_prepare,
881 	.trigger = sis_pcm_trigger,
882 	.pointer = sis_pcm_pointer,
883 };
884 
885 static int sis_pcm_create(struct sis7019 *sis)
886 {
887 	struct snd_pcm *pcm;
888 	int rc;
889 
890 	/* We have 64 voices, and the driver currently records from
891 	 * only one channel, though that could change in the future.
892 	 */
893 	rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
894 	if (rc)
895 		return rc;
896 
897 	pcm->private_data = sis;
898 	strcpy(pcm->name, "SiS7019");
899 	sis->pcm = pcm;
900 
901 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
902 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
903 
904 	/* Try to preallocate some memory, but it's not the end of the
905 	 * world if this fails.
906 	 */
907 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
908 				snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
909 
910 	return 0;
911 }
912 
913 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
914 {
915 	unsigned long io = sis->ioport;
916 	unsigned short val = 0xffff;
917 	u16 status;
918 	u16 rdy;
919 	int count;
920 	static const u16 codec_ready[3] = {
921 		SIS_AC97_STATUS_CODEC_READY,
922 		SIS_AC97_STATUS_CODEC2_READY,
923 		SIS_AC97_STATUS_CODEC3_READY,
924 	};
925 
926 	rdy = codec_ready[codec];
927 
928 
929 	/* Get the AC97 semaphore -- software first, so we don't spin
930 	 * pounding out IO reads on the hardware semaphore...
931 	 */
932 	mutex_lock(&sis->ac97_mutex);
933 
934 	count = 0xffff;
935 	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
936 		udelay(1);
937 
938 	if (!count)
939 		goto timeout;
940 
941 	/* ... and wait for any outstanding commands to complete ...
942 	 */
943 	count = 0xffff;
944 	do {
945 		status = inw(io + SIS_AC97_STATUS);
946 		if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
947 			break;
948 
949 		udelay(1);
950 	} while (--count);
951 
952 	if (!count)
953 		goto timeout_sema;
954 
955 	/* ... before sending our command and waiting for it to finish ...
956 	 */
957 	outl(cmd, io + SIS_AC97_CMD);
958 	udelay(10);
959 
960 	count = 0xffff;
961 	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
962 		udelay(1);
963 
964 	/* ... and reading the results (if any).
965 	 */
966 	val = inl(io + SIS_AC97_CMD) >> 16;
967 
968 timeout_sema:
969 	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
970 timeout:
971 	mutex_unlock(&sis->ac97_mutex);
972 
973 	if (!count) {
974 		dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
975 					codec, cmd);
976 	}
977 
978 	return val;
979 }
980 
981 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
982 				unsigned short val)
983 {
984 	static const u32 cmd[3] = {
985 		SIS_AC97_CMD_CODEC_WRITE,
986 		SIS_AC97_CMD_CODEC2_WRITE,
987 		SIS_AC97_CMD_CODEC3_WRITE,
988 	};
989 	sis_ac97_rw(ac97->private_data, ac97->num,
990 			(val << 16) | (reg << 8) | cmd[ac97->num]);
991 }
992 
993 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
994 {
995 	static const u32 cmd[3] = {
996 		SIS_AC97_CMD_CODEC_READ,
997 		SIS_AC97_CMD_CODEC2_READ,
998 		SIS_AC97_CMD_CODEC3_READ,
999 	};
1000 	return sis_ac97_rw(ac97->private_data, ac97->num,
1001 					(reg << 8) | cmd[ac97->num]);
1002 }
1003 
1004 static int sis_mixer_create(struct sis7019 *sis)
1005 {
1006 	struct snd_ac97_bus *bus;
1007 	struct snd_ac97_template ac97;
1008 	static struct snd_ac97_bus_ops ops = {
1009 		.write = sis_ac97_write,
1010 		.read = sis_ac97_read,
1011 	};
1012 	int rc;
1013 
1014 	memset(&ac97, 0, sizeof(ac97));
1015 	ac97.private_data = sis;
1016 
1017 	rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1018 	if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1019 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1020 	ac97.num = 1;
1021 	if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1022 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1023 	ac97.num = 2;
1024 	if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1025 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1026 
1027 	/* If we return an error here, then snd_card_free() should
1028 	 * free up any ac97 codecs that got created, as well as the bus.
1029 	 */
1030 	return rc;
1031 }
1032 
1033 static void sis_free_suspend(struct sis7019 *sis)
1034 {
1035 	int i;
1036 
1037 	for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1038 		kfree(sis->suspend_state[i]);
1039 }
1040 
1041 static int sis_chip_free(struct sis7019 *sis)
1042 {
1043 	/* Reset the chip, and disable all interrputs.
1044 	 */
1045 	outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1046 	udelay(25);
1047 	outl(0, sis->ioport + SIS_GCR);
1048 	outl(0, sis->ioport + SIS_GIER);
1049 
1050 	/* Now, free everything we allocated.
1051 	 */
1052 	if (sis->irq >= 0)
1053 		free_irq(sis->irq, sis);
1054 
1055 	iounmap(sis->ioaddr);
1056 	pci_release_regions(sis->pci);
1057 	pci_disable_device(sis->pci);
1058 	sis_free_suspend(sis);
1059 	return 0;
1060 }
1061 
1062 static int sis_dev_free(struct snd_device *dev)
1063 {
1064 	struct sis7019 *sis = dev->device_data;
1065 	return sis_chip_free(sis);
1066 }
1067 
1068 static int sis_chip_init(struct sis7019 *sis)
1069 {
1070 	unsigned long io = sis->ioport;
1071 	void __iomem *ioaddr = sis->ioaddr;
1072 	unsigned long timeout;
1073 	u16 status;
1074 	int count;
1075 	int i;
1076 
1077 	/* Reset the audio controller
1078 	 */
1079 	outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1080 	udelay(25);
1081 	outl(0, io + SIS_GCR);
1082 
1083 	/* Get the AC-link semaphore, and reset the codecs
1084 	 */
1085 	count = 0xffff;
1086 	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1087 		udelay(1);
1088 
1089 	if (!count)
1090 		return -EIO;
1091 
1092 	outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1093 	udelay(250);
1094 
1095 	count = 0xffff;
1096 	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1097 		udelay(1);
1098 
1099 	/* Command complete, we can let go of the semaphore now.
1100 	 */
1101 	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1102 	if (!count)
1103 		return -EIO;
1104 
1105 	/* Now that we've finished the reset, find out what's attached.
1106 	 * There are some codec/board combinations that take an extremely
1107 	 * long time to come up. 350+ ms has been observed in the field,
1108 	 * so we'll give them up to 500ms.
1109 	 */
1110 	sis->codecs_present = 0;
1111 	timeout = msecs_to_jiffies(500) + jiffies;
1112 	while (time_before_eq(jiffies, timeout)) {
1113 		status = inl(io + SIS_AC97_STATUS);
1114 		if (status & SIS_AC97_STATUS_CODEC_READY)
1115 			sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1116 		if (status & SIS_AC97_STATUS_CODEC2_READY)
1117 			sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1118 		if (status & SIS_AC97_STATUS_CODEC3_READY)
1119 			sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1120 
1121 		if (sis->codecs_present == codecs)
1122 			break;
1123 
1124 		msleep(1);
1125 	}
1126 
1127 	/* All done, check for errors.
1128 	 */
1129 	if (!sis->codecs_present) {
1130 		dev_err(&sis->pci->dev, "could not find any codecs\n");
1131 		return -EIO;
1132 	}
1133 
1134 	if (sis->codecs_present != codecs) {
1135 		dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1136 					 sis->codecs_present, codecs);
1137 	}
1138 
1139 	/* Let the hardware know that the audio driver is alive,
1140 	 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1141 	 * record channels. We're going to want to use Variable Rate Audio
1142 	 * for recording, to avoid needlessly resampling from 48kHZ.
1143 	 */
1144 	outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1145 	outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1146 		SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1147 		SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1148 		SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1149 
1150 	/* All AC97 PCM slots should be sourced from sub-mixer 0.
1151 	 */
1152 	outl(0, io + SIS_AC97_PSR);
1153 
1154 	/* There is only one valid DMA setup for a PCI environment.
1155 	 */
1156 	outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1157 
1158 	/* Reset the synchronization groups for all of the channels
1159 	 * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
1160 	 * we'll need to change how we handle these. Until then, we just
1161 	 * assign sub-mixer 0 to all playback channels, and avoid any
1162 	 * attenuation on the audio.
1163 	 */
1164 	outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1165 	outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1166 	outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1167 	outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1168 	outl(0, io + SIS_MIXER_SYNC_GROUP);
1169 
1170 	for (i = 0; i < 64; i++) {
1171 		writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1172 		writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1173 				SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1174 	}
1175 
1176 	/* Don't attenuate any audio set for the wave amplifier.
1177 	 *
1178 	 * FIXME: Maximum attenuation is set for the music amp, which will
1179 	 * need to change if we start using the synth engine.
1180 	 */
1181 	outl(0xffff0000, io + SIS_WEVCR);
1182 
1183 	/* Ensure that the wave engine is in normal operating mode.
1184 	 */
1185 	outl(0, io + SIS_WECCR);
1186 
1187 	/* Go ahead and enable the DMA interrupts. They won't go live
1188 	 * until we start a channel.
1189 	 */
1190 	outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1191 		SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1192 
1193 	return 0;
1194 }
1195 
1196 #ifdef CONFIG_PM_SLEEP
1197 static int sis_suspend(struct device *dev)
1198 {
1199 	struct snd_card *card = dev_get_drvdata(dev);
1200 	struct sis7019 *sis = card->private_data;
1201 	void __iomem *ioaddr = sis->ioaddr;
1202 	int i;
1203 
1204 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1205 	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1206 		snd_ac97_suspend(sis->ac97[0]);
1207 	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1208 		snd_ac97_suspend(sis->ac97[1]);
1209 	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1210 		snd_ac97_suspend(sis->ac97[2]);
1211 
1212 	/* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1213 	 */
1214 	if (sis->irq >= 0) {
1215 		free_irq(sis->irq, sis);
1216 		sis->irq = -1;
1217 	}
1218 
1219 	/* Save the internal state away
1220 	 */
1221 	for (i = 0; i < 4; i++) {
1222 		memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1223 		ioaddr += 4096;
1224 	}
1225 
1226 	return 0;
1227 }
1228 
1229 static int sis_resume(struct device *dev)
1230 {
1231 	struct pci_dev *pci = to_pci_dev(dev);
1232 	struct snd_card *card = dev_get_drvdata(dev);
1233 	struct sis7019 *sis = card->private_data;
1234 	void __iomem *ioaddr = sis->ioaddr;
1235 	int i;
1236 
1237 	if (sis_chip_init(sis)) {
1238 		dev_err(&pci->dev, "unable to re-init controller\n");
1239 		goto error;
1240 	}
1241 
1242 	if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1243 			KBUILD_MODNAME, sis)) {
1244 		dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1245 		goto error;
1246 	}
1247 
1248 	/* Restore saved state, then clear out the page we use for the
1249 	 * silence buffer.
1250 	 */
1251 	for (i = 0; i < 4; i++) {
1252 		memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1253 		ioaddr += 4096;
1254 	}
1255 
1256 	memset(sis->suspend_state[0], 0, 4096);
1257 
1258 	sis->irq = pci->irq;
1259 
1260 	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1261 		snd_ac97_resume(sis->ac97[0]);
1262 	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1263 		snd_ac97_resume(sis->ac97[1]);
1264 	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1265 		snd_ac97_resume(sis->ac97[2]);
1266 
1267 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1268 	return 0;
1269 
1270 error:
1271 	snd_card_disconnect(card);
1272 	return -EIO;
1273 }
1274 
1275 static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
1276 #define SIS_PM_OPS	&sis_pm
1277 #else
1278 #define SIS_PM_OPS	NULL
1279 #endif /* CONFIG_PM_SLEEP */
1280 
1281 static int sis_alloc_suspend(struct sis7019 *sis)
1282 {
1283 	int i;
1284 
1285 	/* We need 16K to store the internal wave engine state during a
1286 	 * suspend, but we don't need it to be contiguous, so play nice
1287 	 * with the memory system. We'll also use this area for a silence
1288 	 * buffer.
1289 	 */
1290 	for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1291 		sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1292 		if (!sis->suspend_state[i])
1293 			return -ENOMEM;
1294 	}
1295 	memset(sis->suspend_state[0], 0, 4096);
1296 
1297 	return 0;
1298 }
1299 
1300 static int sis_chip_create(struct snd_card *card,
1301 			   struct pci_dev *pci)
1302 {
1303 	struct sis7019 *sis = card->private_data;
1304 	struct voice *voice;
1305 	static struct snd_device_ops ops = {
1306 		.dev_free = sis_dev_free,
1307 	};
1308 	int rc;
1309 	int i;
1310 
1311 	rc = pci_enable_device(pci);
1312 	if (rc)
1313 		goto error_out;
1314 
1315 	rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
1316 	if (rc < 0) {
1317 		dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1318 		goto error_out_enabled;
1319 	}
1320 
1321 	memset(sis, 0, sizeof(*sis));
1322 	mutex_init(&sis->ac97_mutex);
1323 	spin_lock_init(&sis->voice_lock);
1324 	sis->card = card;
1325 	sis->pci = pci;
1326 	sis->irq = -1;
1327 	sis->ioport = pci_resource_start(pci, 0);
1328 
1329 	rc = pci_request_regions(pci, "SiS7019");
1330 	if (rc) {
1331 		dev_err(&pci->dev, "unable request regions\n");
1332 		goto error_out_enabled;
1333 	}
1334 
1335 	rc = -EIO;
1336 	sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1337 	if (!sis->ioaddr) {
1338 		dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1339 		goto error_out_cleanup;
1340 	}
1341 
1342 	rc = sis_alloc_suspend(sis);
1343 	if (rc < 0) {
1344 		dev_err(&pci->dev, "unable to allocate state storage\n");
1345 		goto error_out_cleanup;
1346 	}
1347 
1348 	rc = sis_chip_init(sis);
1349 	if (rc)
1350 		goto error_out_cleanup;
1351 
1352 	rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1353 			 sis);
1354 	if (rc) {
1355 		dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1356 		goto error_out_cleanup;
1357 	}
1358 
1359 	sis->irq = pci->irq;
1360 	pci_set_master(pci);
1361 
1362 	for (i = 0; i < 64; i++) {
1363 		voice = &sis->voices[i];
1364 		voice->num = i;
1365 		voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1366 		voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1367 	}
1368 
1369 	voice = &sis->capture_voice;
1370 	voice->flags = VOICE_CAPTURE;
1371 	voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1372 	voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1373 
1374 	rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1375 	if (rc)
1376 		goto error_out_cleanup;
1377 
1378 	return 0;
1379 
1380 error_out_cleanup:
1381 	sis_chip_free(sis);
1382 
1383 error_out_enabled:
1384 	pci_disable_device(pci);
1385 
1386 error_out:
1387 	return rc;
1388 }
1389 
1390 static int snd_sis7019_probe(struct pci_dev *pci,
1391 			     const struct pci_device_id *pci_id)
1392 {
1393 	struct snd_card *card;
1394 	struct sis7019 *sis;
1395 	int rc;
1396 
1397 	rc = -ENOENT;
1398 	if (!enable)
1399 		goto error_out;
1400 
1401 	/* The user can specify which codecs should be present so that we
1402 	 * can wait for them to show up if they are slow to recover from
1403 	 * the AC97 cold reset. We default to a single codec, the primary.
1404 	 *
1405 	 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1406 	 */
1407 	codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1408 		  SIS_TERTIARY_CODEC_PRESENT;
1409 	if (!codecs)
1410 		codecs = SIS_PRIMARY_CODEC_PRESENT;
1411 
1412 	rc = snd_card_new(&pci->dev, index, id, THIS_MODULE,
1413 			  sizeof(*sis), &card);
1414 	if (rc < 0)
1415 		goto error_out;
1416 
1417 	strcpy(card->driver, "SiS7019");
1418 	strcpy(card->shortname, "SiS7019");
1419 	rc = sis_chip_create(card, pci);
1420 	if (rc)
1421 		goto card_error_out;
1422 
1423 	sis = card->private_data;
1424 
1425 	rc = sis_mixer_create(sis);
1426 	if (rc)
1427 		goto card_error_out;
1428 
1429 	rc = sis_pcm_create(sis);
1430 	if (rc)
1431 		goto card_error_out;
1432 
1433 	snprintf(card->longname, sizeof(card->longname),
1434 			"%s Audio Accelerator with %s at 0x%lx, irq %d",
1435 			card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1436 			sis->ioport, sis->irq);
1437 
1438 	rc = snd_card_register(card);
1439 	if (rc)
1440 		goto card_error_out;
1441 
1442 	pci_set_drvdata(pci, card);
1443 	return 0;
1444 
1445 card_error_out:
1446 	snd_card_free(card);
1447 
1448 error_out:
1449 	return rc;
1450 }
1451 
1452 static void snd_sis7019_remove(struct pci_dev *pci)
1453 {
1454 	snd_card_free(pci_get_drvdata(pci));
1455 }
1456 
1457 static struct pci_driver sis7019_driver = {
1458 	.name = KBUILD_MODNAME,
1459 	.id_table = snd_sis7019_ids,
1460 	.probe = snd_sis7019_probe,
1461 	.remove = snd_sis7019_remove,
1462 	.driver = {
1463 		.pm = SIS_PM_OPS,
1464 	},
1465 };
1466 
1467 module_pci_driver(sis7019_driver);
1468