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