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