xref: /openbmc/linux/sound/pci/sis7019.c (revision 412b979c)
1175859bfSDavid Dillow /*
2175859bfSDavid Dillow  *  Driver for SiS7019 Audio Accelerator
3175859bfSDavid Dillow  *
4175859bfSDavid Dillow  *  Copyright (C) 2004-2007, David Dillow
5175859bfSDavid Dillow  *  Written by David Dillow <dave@thedillows.org>
6175859bfSDavid Dillow  *  Inspired by the Trident 4D-WaveDX/NX driver.
7175859bfSDavid Dillow  *
8175859bfSDavid Dillow  *  All rights reserved.
9175859bfSDavid Dillow  *
10175859bfSDavid Dillow  *  This program is free software; you can redistribute it and/or modify
11175859bfSDavid Dillow  *  it under the terms of the GNU General Public License as published by
12175859bfSDavid Dillow  *  the Free Software Foundation, version 2.
13175859bfSDavid Dillow  *
14175859bfSDavid Dillow  *  This program is distributed in the hope that it will be useful,
15175859bfSDavid Dillow  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16175859bfSDavid Dillow  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17175859bfSDavid Dillow  *  GNU General Public License for more details.
18175859bfSDavid Dillow  *
19175859bfSDavid Dillow  *  You should have received a copy of the GNU General Public License
20175859bfSDavid Dillow  *  along with this program; if not, write to the Free Software
21175859bfSDavid Dillow  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22175859bfSDavid Dillow  */
23175859bfSDavid Dillow 
24175859bfSDavid Dillow #include <linux/init.h>
25175859bfSDavid Dillow #include <linux/pci.h>
26175859bfSDavid Dillow #include <linux/time.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
2865a77217SPaul Gortmaker #include <linux/module.h>
29175859bfSDavid Dillow #include <linux/interrupt.h>
30175859bfSDavid Dillow #include <linux/delay.h>
31175859bfSDavid Dillow #include <sound/core.h>
32175859bfSDavid Dillow #include <sound/ac97_codec.h>
33175859bfSDavid Dillow #include <sound/initval.h>
34175859bfSDavid Dillow #include "sis7019.h"
35175859bfSDavid Dillow 
36175859bfSDavid Dillow MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
37175859bfSDavid Dillow MODULE_DESCRIPTION("SiS7019");
38175859bfSDavid Dillow MODULE_LICENSE("GPL");
39175859bfSDavid Dillow MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
40175859bfSDavid Dillow 
41175859bfSDavid Dillow static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
42175859bfSDavid Dillow static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
43a67ff6a5SRusty Russell static bool enable = 1;
44fc084e0bSDavid Dillow static int codecs = 1;
45175859bfSDavid Dillow 
46175859bfSDavid Dillow module_param(index, int, 0444);
47175859bfSDavid Dillow MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
48175859bfSDavid Dillow module_param(id, charp, 0444);
49175859bfSDavid Dillow MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
50175859bfSDavid Dillow module_param(enable, bool, 0444);
51175859bfSDavid Dillow MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
52fc084e0bSDavid Dillow module_param(codecs, int, 0444);
53fc084e0bSDavid Dillow MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
54175859bfSDavid Dillow 
559baa3c34SBenoit Taine static const struct pci_device_id snd_sis7019_ids[] = {
56175859bfSDavid Dillow 	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
57175859bfSDavid Dillow 	{ 0, }
58175859bfSDavid Dillow };
59175859bfSDavid Dillow 
60175859bfSDavid Dillow MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
61175859bfSDavid Dillow 
62175859bfSDavid Dillow /* There are three timing modes for the voices.
63175859bfSDavid Dillow  *
64175859bfSDavid Dillow  * For both playback and capture, when the buffer is one or two periods long,
65175859bfSDavid Dillow  * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
66175859bfSDavid Dillow  * to let us know when the periods have ended.
67175859bfSDavid Dillow  *
68175859bfSDavid Dillow  * When performing playback with more than two periods per buffer, we set
69175859bfSDavid Dillow  * the "Stop Sample Offset" and tell the hardware to interrupt us when we
70175859bfSDavid Dillow  * reach it. We then update the offset and continue on until we are
71175859bfSDavid Dillow  * interrupted for the next period.
72175859bfSDavid Dillow  *
73175859bfSDavid Dillow  * Capture channels do not have a SSO, so we allocate a playback channel to
74175859bfSDavid Dillow  * use as a timer for the capture periods. We use the SSO on the playback
75175859bfSDavid Dillow  * channel to clock out virtual periods, and adjust the virtual period length
76175859bfSDavid Dillow  * to maintain synchronization. This algorithm came from the Trident driver.
77175859bfSDavid Dillow  *
78175859bfSDavid Dillow  * FIXME: It'd be nice to make use of some of the synth features in the
79175859bfSDavid Dillow  * hardware, but a woeful lack of documentation is a significant roadblock.
80175859bfSDavid Dillow  */
81175859bfSDavid Dillow struct voice {
82175859bfSDavid Dillow 	u16 flags;
83175859bfSDavid Dillow #define 	VOICE_IN_USE		1
84175859bfSDavid Dillow #define 	VOICE_CAPTURE		2
85175859bfSDavid Dillow #define 	VOICE_SSO_TIMING	4
86175859bfSDavid Dillow #define 	VOICE_SYNC_TIMING	8
87175859bfSDavid Dillow 	u16 sync_cso;
88175859bfSDavid Dillow 	u16 period_size;
89175859bfSDavid Dillow 	u16 buffer_size;
90175859bfSDavid Dillow 	u16 sync_period_size;
91175859bfSDavid Dillow 	u16 sync_buffer_size;
92175859bfSDavid Dillow 	u32 sso;
93175859bfSDavid Dillow 	u32 vperiod;
94175859bfSDavid Dillow 	struct snd_pcm_substream *substream;
95175859bfSDavid Dillow 	struct voice *timing;
96175859bfSDavid Dillow 	void __iomem *ctrl_base;
97175859bfSDavid Dillow 	void __iomem *wave_base;
98175859bfSDavid Dillow 	void __iomem *sync_base;
99175859bfSDavid Dillow 	int num;
100175859bfSDavid Dillow };
101175859bfSDavid Dillow 
102175859bfSDavid Dillow /* We need four pages to store our wave parameters during a suspend. If
103175859bfSDavid Dillow  * we're not doing power management, we still need to allocate a page
104175859bfSDavid Dillow  * for the silence buffer.
105175859bfSDavid Dillow  */
106c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
107175859bfSDavid Dillow #define SIS_SUSPEND_PAGES	4
108175859bfSDavid Dillow #else
109175859bfSDavid Dillow #define SIS_SUSPEND_PAGES	1
110175859bfSDavid Dillow #endif
111175859bfSDavid Dillow 
112175859bfSDavid Dillow struct sis7019 {
113175859bfSDavid Dillow 	unsigned long ioport;
114175859bfSDavid Dillow 	void __iomem *ioaddr;
115175859bfSDavid Dillow 	int irq;
116175859bfSDavid Dillow 	int codecs_present;
117175859bfSDavid Dillow 
118175859bfSDavid Dillow 	struct pci_dev *pci;
119175859bfSDavid Dillow 	struct snd_pcm *pcm;
120175859bfSDavid Dillow 	struct snd_card *card;
121175859bfSDavid Dillow 	struct snd_ac97 *ac97[3];
122175859bfSDavid Dillow 
123175859bfSDavid Dillow 	/* Protect against more than one thread hitting the AC97
124175859bfSDavid Dillow 	 * registers (in a more polite manner than pounding the hardware
125175859bfSDavid Dillow 	 * semaphore)
126175859bfSDavid Dillow 	 */
127175859bfSDavid Dillow 	struct mutex ac97_mutex;
128175859bfSDavid Dillow 
129175859bfSDavid Dillow 	/* voice_lock protects allocation/freeing of the voice descriptions
130175859bfSDavid Dillow 	 */
131175859bfSDavid Dillow 	spinlock_t voice_lock;
132175859bfSDavid Dillow 
133175859bfSDavid Dillow 	struct voice voices[64];
134175859bfSDavid Dillow 	struct voice capture_voice;
135175859bfSDavid Dillow 
136175859bfSDavid Dillow 	/* Allocate pages to store the internal wave state during
137175859bfSDavid Dillow 	 * suspends. When we're operating, this can be used as a silence
138175859bfSDavid Dillow 	 * buffer for a timing channel.
139175859bfSDavid Dillow 	 */
140175859bfSDavid Dillow 	void *suspend_state[SIS_SUSPEND_PAGES];
141175859bfSDavid Dillow 
142175859bfSDavid Dillow 	int silence_users;
143175859bfSDavid Dillow 	dma_addr_t silence_dma_addr;
144175859bfSDavid Dillow };
145175859bfSDavid Dillow 
146fc084e0bSDavid Dillow /* These values are also used by the module param 'codecs' to indicate
147fc084e0bSDavid Dillow  * which codecs should be present.
148fc084e0bSDavid Dillow  */
149175859bfSDavid Dillow #define SIS_PRIMARY_CODEC_PRESENT	0x0001
150175859bfSDavid Dillow #define SIS_SECONDARY_CODEC_PRESENT	0x0002
151175859bfSDavid Dillow #define SIS_TERTIARY_CODEC_PRESENT	0x0004
152175859bfSDavid Dillow 
153175859bfSDavid Dillow /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
154175859bfSDavid Dillow  * documented range of 8-0xfff8 samples. Given that they are 0-based,
155175859bfSDavid Dillow  * that places our period/buffer range at 9-0xfff9 samples. That makes the
156175859bfSDavid Dillow  * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
157175859bfSDavid Dillow  * max samples / min samples gives us the max periods in a buffer.
158175859bfSDavid Dillow  *
159175859bfSDavid Dillow  * We'll add a constraint upon open that limits the period and buffer sample
160175859bfSDavid Dillow  * size to values that are legal for the hardware.
161175859bfSDavid Dillow  */
162175859bfSDavid Dillow static struct snd_pcm_hardware sis_playback_hw_info = {
163175859bfSDavid Dillow 	.info = (SNDRV_PCM_INFO_MMAP |
164175859bfSDavid Dillow 		 SNDRV_PCM_INFO_MMAP_VALID |
165175859bfSDavid Dillow 		 SNDRV_PCM_INFO_INTERLEAVED |
166175859bfSDavid Dillow 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
167175859bfSDavid Dillow 		 SNDRV_PCM_INFO_SYNC_START |
168175859bfSDavid Dillow 		 SNDRV_PCM_INFO_RESUME),
169175859bfSDavid Dillow 	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
170175859bfSDavid Dillow 		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
171175859bfSDavid Dillow 	.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
172175859bfSDavid Dillow 	.rate_min = 4000,
173175859bfSDavid Dillow 	.rate_max = 48000,
174175859bfSDavid Dillow 	.channels_min = 1,
175175859bfSDavid Dillow 	.channels_max = 2,
176175859bfSDavid Dillow 	.buffer_bytes_max = (0xfff9 * 4),
177175859bfSDavid Dillow 	.period_bytes_min = 9,
178175859bfSDavid Dillow 	.period_bytes_max = (0xfff9 * 4),
179175859bfSDavid Dillow 	.periods_min = 1,
180175859bfSDavid Dillow 	.periods_max = (0xfff9 / 9),
181175859bfSDavid Dillow };
182175859bfSDavid Dillow 
183175859bfSDavid Dillow static struct snd_pcm_hardware sis_capture_hw_info = {
184175859bfSDavid Dillow 	.info = (SNDRV_PCM_INFO_MMAP |
185175859bfSDavid Dillow 		 SNDRV_PCM_INFO_MMAP_VALID |
186175859bfSDavid Dillow 		 SNDRV_PCM_INFO_INTERLEAVED |
187175859bfSDavid Dillow 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
188175859bfSDavid Dillow 		 SNDRV_PCM_INFO_SYNC_START |
189175859bfSDavid Dillow 		 SNDRV_PCM_INFO_RESUME),
190175859bfSDavid Dillow 	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
191175859bfSDavid Dillow 		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
192175859bfSDavid Dillow 	.rates = SNDRV_PCM_RATE_48000,
193175859bfSDavid Dillow 	.rate_min = 4000,
194175859bfSDavid Dillow 	.rate_max = 48000,
195175859bfSDavid Dillow 	.channels_min = 1,
196175859bfSDavid Dillow 	.channels_max = 2,
197175859bfSDavid Dillow 	.buffer_bytes_max = (0xfff9 * 4),
198175859bfSDavid Dillow 	.period_bytes_min = 9,
199175859bfSDavid Dillow 	.period_bytes_max = (0xfff9 * 4),
200175859bfSDavid Dillow 	.periods_min = 1,
201175859bfSDavid Dillow 	.periods_max = (0xfff9 / 9),
202175859bfSDavid Dillow };
203175859bfSDavid Dillow 
204175859bfSDavid Dillow static void sis_update_sso(struct voice *voice, u16 period)
205175859bfSDavid Dillow {
206175859bfSDavid Dillow 	void __iomem *base = voice->ctrl_base;
207175859bfSDavid Dillow 
208175859bfSDavid Dillow 	voice->sso += period;
209175859bfSDavid Dillow 	if (voice->sso >= voice->buffer_size)
210175859bfSDavid Dillow 		voice->sso -= voice->buffer_size;
211175859bfSDavid Dillow 
212175859bfSDavid Dillow 	/* Enforce the documented hardware minimum offset */
213175859bfSDavid Dillow 	if (voice->sso < 8)
214175859bfSDavid Dillow 		voice->sso = 8;
215175859bfSDavid Dillow 
216175859bfSDavid Dillow 	/* The SSO is in the upper 16 bits of the register. */
217175859bfSDavid Dillow 	writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
218175859bfSDavid Dillow }
219175859bfSDavid Dillow 
220175859bfSDavid Dillow static void sis_update_voice(struct voice *voice)
221175859bfSDavid Dillow {
222175859bfSDavid Dillow 	if (voice->flags & VOICE_SSO_TIMING) {
223175859bfSDavid Dillow 		sis_update_sso(voice, voice->period_size);
224175859bfSDavid Dillow 	} else if (voice->flags & VOICE_SYNC_TIMING) {
225175859bfSDavid Dillow 		int sync;
226175859bfSDavid Dillow 
227175859bfSDavid Dillow 		/* If we've not hit the end of the virtual period, update
228175859bfSDavid Dillow 		 * our records and keep going.
229175859bfSDavid Dillow 		 */
230175859bfSDavid Dillow 		if (voice->vperiod > voice->period_size) {
231175859bfSDavid Dillow 			voice->vperiod -= voice->period_size;
232175859bfSDavid Dillow 			if (voice->vperiod < voice->period_size)
233175859bfSDavid Dillow 				sis_update_sso(voice, voice->vperiod);
234175859bfSDavid Dillow 			else
235175859bfSDavid Dillow 				sis_update_sso(voice, voice->period_size);
236175859bfSDavid Dillow 			return;
237175859bfSDavid Dillow 		}
238175859bfSDavid Dillow 
239175859bfSDavid Dillow 		/* Calculate our relative offset between the target and
240175859bfSDavid Dillow 		 * the actual CSO value. Since we're operating in a loop,
241175859bfSDavid Dillow 		 * if the value is more than half way around, we can
242175859bfSDavid Dillow 		 * consider ourselves wrapped.
243175859bfSDavid Dillow 		 */
244175859bfSDavid Dillow 		sync = voice->sync_cso;
245175859bfSDavid Dillow 		sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
246175859bfSDavid Dillow 		if (sync > (voice->sync_buffer_size / 2))
247175859bfSDavid Dillow 			sync -= voice->sync_buffer_size;
248175859bfSDavid Dillow 
249175859bfSDavid Dillow 		/* If sync is positive, then we interrupted too early, and
250175859bfSDavid Dillow 		 * we'll need to come back in a few samples and try again.
251175859bfSDavid Dillow 		 * There's a minimum wait, as it takes some time for the DMA
252175859bfSDavid Dillow 		 * engine to startup, etc...
253175859bfSDavid Dillow 		 */
254175859bfSDavid Dillow 		if (sync > 0) {
255175859bfSDavid Dillow 			if (sync < 16)
256175859bfSDavid Dillow 				sync = 16;
257175859bfSDavid Dillow 			sis_update_sso(voice, sync);
258175859bfSDavid Dillow 			return;
259175859bfSDavid Dillow 		}
260175859bfSDavid Dillow 
261175859bfSDavid Dillow 		/* Ok, we interrupted right on time, or (hopefully) just
262175859bfSDavid Dillow 		 * a bit late. We'll adjst our next waiting period based
263175859bfSDavid Dillow 		 * on how close we got.
264175859bfSDavid Dillow 		 *
265175859bfSDavid Dillow 		 * We need to stay just behind the actual channel to ensure
266175859bfSDavid Dillow 		 * it really is past a period when we get our interrupt --
267175859bfSDavid Dillow 		 * otherwise we'll fall into the early code above and have
268175859bfSDavid Dillow 		 * a minimum wait time, which makes us quite late here,
269175859bfSDavid Dillow 		 * eating into the user's time to refresh the buffer, esp.
270175859bfSDavid Dillow 		 * if using small periods.
271175859bfSDavid Dillow 		 *
272175859bfSDavid Dillow 		 * If we're less than 9 samples behind, we're on target.
2733a3d5fd1SDavid Dillow 		 * Otherwise, shorten the next vperiod by the amount we've
2743a3d5fd1SDavid Dillow 		 * been delayed.
275175859bfSDavid Dillow 		 */
276175859bfSDavid Dillow 		if (sync > -9)
277175859bfSDavid Dillow 			voice->vperiod = voice->sync_period_size + 1;
278175859bfSDavid Dillow 		else
2793a3d5fd1SDavid Dillow 			voice->vperiod = voice->sync_period_size + sync + 10;
280175859bfSDavid Dillow 
281175859bfSDavid Dillow 		if (voice->vperiod < voice->buffer_size) {
282175859bfSDavid Dillow 			sis_update_sso(voice, voice->vperiod);
283175859bfSDavid Dillow 			voice->vperiod = 0;
284175859bfSDavid Dillow 		} else
285175859bfSDavid Dillow 			sis_update_sso(voice, voice->period_size);
286175859bfSDavid Dillow 
287175859bfSDavid Dillow 		sync = voice->sync_cso + voice->sync_period_size;
288175859bfSDavid Dillow 		if (sync >= voice->sync_buffer_size)
289175859bfSDavid Dillow 			sync -= voice->sync_buffer_size;
290175859bfSDavid Dillow 		voice->sync_cso = sync;
291175859bfSDavid Dillow 	}
292175859bfSDavid Dillow 
293175859bfSDavid Dillow 	snd_pcm_period_elapsed(voice->substream);
294175859bfSDavid Dillow }
295175859bfSDavid Dillow 
296175859bfSDavid Dillow static void sis_voice_irq(u32 status, struct voice *voice)
297175859bfSDavid Dillow {
298175859bfSDavid Dillow 	int bit;
299175859bfSDavid Dillow 
300175859bfSDavid Dillow 	while (status) {
301175859bfSDavid Dillow 		bit = __ffs(status);
302175859bfSDavid Dillow 		status >>= bit + 1;
303175859bfSDavid Dillow 		voice += bit;
304175859bfSDavid Dillow 		sis_update_voice(voice);
305175859bfSDavid Dillow 		voice++;
306175859bfSDavid Dillow 	}
307175859bfSDavid Dillow }
308175859bfSDavid Dillow 
309175859bfSDavid Dillow static irqreturn_t sis_interrupt(int irq, void *dev)
310175859bfSDavid Dillow {
311175859bfSDavid Dillow 	struct sis7019 *sis = dev;
312175859bfSDavid Dillow 	unsigned long io = sis->ioport;
313175859bfSDavid Dillow 	struct voice *voice;
314175859bfSDavid Dillow 	u32 intr, status;
315175859bfSDavid Dillow 
316175859bfSDavid Dillow 	/* We only use the DMA interrupts, and we don't enable any other
31725985edcSLucas De Marchi 	 * source of interrupts. But, it is possible to see an interrupt
318175859bfSDavid Dillow 	 * status that didn't actually interrupt us, so eliminate anything
319175859bfSDavid Dillow 	 * we're not expecting to avoid falsely claiming an IRQ, and an
320175859bfSDavid Dillow 	 * ensuing endless loop.
321175859bfSDavid Dillow 	 */
322175859bfSDavid Dillow 	intr = inl(io + SIS_GISR);
323175859bfSDavid Dillow 	intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
324175859bfSDavid Dillow 		SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
325175859bfSDavid Dillow 	if (!intr)
326175859bfSDavid Dillow 		return IRQ_NONE;
327175859bfSDavid Dillow 
328175859bfSDavid Dillow 	do {
329175859bfSDavid Dillow 		status = inl(io + SIS_PISR_A);
330175859bfSDavid Dillow 		if (status) {
331175859bfSDavid Dillow 			sis_voice_irq(status, sis->voices);
332175859bfSDavid Dillow 			outl(status, io + SIS_PISR_A);
333175859bfSDavid Dillow 		}
334175859bfSDavid Dillow 
335175859bfSDavid Dillow 		status = inl(io + SIS_PISR_B);
336175859bfSDavid Dillow 		if (status) {
337175859bfSDavid Dillow 			sis_voice_irq(status, &sis->voices[32]);
338175859bfSDavid Dillow 			outl(status, io + SIS_PISR_B);
339175859bfSDavid Dillow 		}
340175859bfSDavid Dillow 
341175859bfSDavid Dillow 		status = inl(io + SIS_RISR);
342175859bfSDavid Dillow 		if (status) {
343175859bfSDavid Dillow 			voice = &sis->capture_voice;
344175859bfSDavid Dillow 			if (!voice->timing)
345175859bfSDavid Dillow 				snd_pcm_period_elapsed(voice->substream);
346175859bfSDavid Dillow 
347175859bfSDavid Dillow 			outl(status, io + SIS_RISR);
348175859bfSDavid Dillow 		}
349175859bfSDavid Dillow 
350175859bfSDavid Dillow 		outl(intr, io + SIS_GISR);
351175859bfSDavid Dillow 		intr = inl(io + SIS_GISR);
352175859bfSDavid Dillow 		intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
353175859bfSDavid Dillow 			SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
354175859bfSDavid Dillow 	} while (intr);
355175859bfSDavid Dillow 
356175859bfSDavid Dillow 	return IRQ_HANDLED;
357175859bfSDavid Dillow }
358175859bfSDavid Dillow 
359175859bfSDavid Dillow static u32 sis_rate_to_delta(unsigned int rate)
360175859bfSDavid Dillow {
361175859bfSDavid Dillow 	u32 delta;
362175859bfSDavid Dillow 
363175859bfSDavid Dillow 	/* This was copied from the trident driver, but it seems its gotten
364175859bfSDavid Dillow 	 * around a bit... nevertheless, it works well.
365175859bfSDavid Dillow 	 *
366175859bfSDavid Dillow 	 * We special case 44100 and 8000 since rounding with the equation
367175859bfSDavid Dillow 	 * does not give us an accurate enough value. For 11025 and 22050
368175859bfSDavid Dillow 	 * the equation gives us the best answer. All other frequencies will
369175859bfSDavid Dillow 	 * also use the equation. JDW
370175859bfSDavid Dillow 	 */
371175859bfSDavid Dillow 	if (rate == 44100)
372175859bfSDavid Dillow 		delta = 0xeb3;
373175859bfSDavid Dillow 	else if (rate == 8000)
374175859bfSDavid Dillow 		delta = 0x2ab;
375175859bfSDavid Dillow 	else if (rate == 48000)
376175859bfSDavid Dillow 		delta = 0x1000;
377175859bfSDavid Dillow 	else
378175859bfSDavid Dillow 		delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
379175859bfSDavid Dillow 	return delta;
380175859bfSDavid Dillow }
381175859bfSDavid Dillow 
382175859bfSDavid Dillow static void __sis_map_silence(struct sis7019 *sis)
383175859bfSDavid Dillow {
384175859bfSDavid Dillow 	/* Helper function: must hold sis->voice_lock on entry */
385175859bfSDavid Dillow 	if (!sis->silence_users)
386412b979cSQuentin Lambert 		sis->silence_dma_addr = dma_map_single(&sis->pci->dev,
387175859bfSDavid Dillow 						sis->suspend_state[0],
388412b979cSQuentin Lambert 						4096, DMA_TO_DEVICE);
389175859bfSDavid Dillow 	sis->silence_users++;
390175859bfSDavid Dillow }
391175859bfSDavid Dillow 
392175859bfSDavid Dillow static void __sis_unmap_silence(struct sis7019 *sis)
393175859bfSDavid Dillow {
394175859bfSDavid Dillow 	/* Helper function: must hold sis->voice_lock on entry */
395175859bfSDavid Dillow 	sis->silence_users--;
396175859bfSDavid Dillow 	if (!sis->silence_users)
397412b979cSQuentin Lambert 		dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
398412b979cSQuentin Lambert 					DMA_TO_DEVICE);
399175859bfSDavid Dillow }
400175859bfSDavid Dillow 
401175859bfSDavid Dillow static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
402175859bfSDavid Dillow {
403175859bfSDavid Dillow 	unsigned long flags;
404175859bfSDavid Dillow 
405175859bfSDavid Dillow 	spin_lock_irqsave(&sis->voice_lock, flags);
406175859bfSDavid Dillow 	if (voice->timing) {
407175859bfSDavid Dillow 		__sis_unmap_silence(sis);
408175859bfSDavid Dillow 		voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
409175859bfSDavid Dillow 						VOICE_SYNC_TIMING);
410175859bfSDavid Dillow 		voice->timing = NULL;
411175859bfSDavid Dillow 	}
412175859bfSDavid Dillow 	voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
413175859bfSDavid Dillow 	spin_unlock_irqrestore(&sis->voice_lock, flags);
414175859bfSDavid Dillow }
415175859bfSDavid Dillow 
416175859bfSDavid Dillow static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
417175859bfSDavid Dillow {
418175859bfSDavid Dillow 	/* Must hold the voice_lock on entry */
419175859bfSDavid Dillow 	struct voice *voice;
420175859bfSDavid Dillow 	int i;
421175859bfSDavid Dillow 
422175859bfSDavid Dillow 	for (i = 0; i < 64; i++) {
423175859bfSDavid Dillow 		voice = &sis->voices[i];
424175859bfSDavid Dillow 		if (voice->flags & VOICE_IN_USE)
425175859bfSDavid Dillow 			continue;
426175859bfSDavid Dillow 		voice->flags |= VOICE_IN_USE;
427175859bfSDavid Dillow 		goto found_one;
428175859bfSDavid Dillow 	}
429175859bfSDavid Dillow 	voice = NULL;
430175859bfSDavid Dillow 
431175859bfSDavid Dillow found_one:
432175859bfSDavid Dillow 	return voice;
433175859bfSDavid Dillow }
434175859bfSDavid Dillow 
435175859bfSDavid Dillow static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
436175859bfSDavid Dillow {
437175859bfSDavid Dillow 	struct voice *voice;
438175859bfSDavid Dillow 	unsigned long flags;
439175859bfSDavid Dillow 
440175859bfSDavid Dillow 	spin_lock_irqsave(&sis->voice_lock, flags);
441175859bfSDavid Dillow 	voice = __sis_alloc_playback_voice(sis);
442175859bfSDavid Dillow 	spin_unlock_irqrestore(&sis->voice_lock, flags);
443175859bfSDavid Dillow 
444175859bfSDavid Dillow 	return voice;
445175859bfSDavid Dillow }
446175859bfSDavid Dillow 
447175859bfSDavid Dillow static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
448175859bfSDavid Dillow 					struct snd_pcm_hw_params *hw_params)
449175859bfSDavid Dillow {
450175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
451175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
452175859bfSDavid Dillow 	struct voice *voice = runtime->private_data;
453175859bfSDavid Dillow 	unsigned int period_size, buffer_size;
454175859bfSDavid Dillow 	unsigned long flags;
455175859bfSDavid Dillow 	int needed;
456175859bfSDavid Dillow 
457175859bfSDavid Dillow 	/* If there are one or two periods per buffer, we don't need a
458175859bfSDavid Dillow 	 * timing voice, as we can use the capture channel's interrupts
459175859bfSDavid Dillow 	 * to clock out the periods.
460175859bfSDavid Dillow 	 */
461175859bfSDavid Dillow 	period_size = params_period_size(hw_params);
462175859bfSDavid Dillow 	buffer_size = params_buffer_size(hw_params);
463175859bfSDavid Dillow 	needed = (period_size != buffer_size &&
464175859bfSDavid Dillow 			period_size != (buffer_size / 2));
465175859bfSDavid Dillow 
466175859bfSDavid Dillow 	if (needed && !voice->timing) {
467175859bfSDavid Dillow 		spin_lock_irqsave(&sis->voice_lock, flags);
468175859bfSDavid Dillow 		voice->timing = __sis_alloc_playback_voice(sis);
469175859bfSDavid Dillow 		if (voice->timing)
470175859bfSDavid Dillow 			__sis_map_silence(sis);
471175859bfSDavid Dillow 		spin_unlock_irqrestore(&sis->voice_lock, flags);
472175859bfSDavid Dillow 		if (!voice->timing)
473175859bfSDavid Dillow 			return -ENOMEM;
474175859bfSDavid Dillow 		voice->timing->substream = substream;
475175859bfSDavid Dillow 	} else if (!needed && voice->timing) {
476175859bfSDavid Dillow 		sis_free_voice(sis, voice);
477175859bfSDavid Dillow 		voice->timing = NULL;
478175859bfSDavid Dillow 	}
479175859bfSDavid Dillow 
480175859bfSDavid Dillow 	return 0;
481175859bfSDavid Dillow }
482175859bfSDavid Dillow 
483175859bfSDavid Dillow static int sis_playback_open(struct snd_pcm_substream *substream)
484175859bfSDavid Dillow {
485175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
486175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
487175859bfSDavid Dillow 	struct voice *voice;
488175859bfSDavid Dillow 
489175859bfSDavid Dillow 	voice = sis_alloc_playback_voice(sis);
490175859bfSDavid Dillow 	if (!voice)
491175859bfSDavid Dillow 		return -EAGAIN;
492175859bfSDavid Dillow 
493175859bfSDavid Dillow 	voice->substream = substream;
494175859bfSDavid Dillow 	runtime->private_data = voice;
495175859bfSDavid Dillow 	runtime->hw = sis_playback_hw_info;
496175859bfSDavid Dillow 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
497175859bfSDavid Dillow 						9, 0xfff9);
498175859bfSDavid Dillow 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
499175859bfSDavid Dillow 						9, 0xfff9);
500175859bfSDavid Dillow 	snd_pcm_set_sync(substream);
501175859bfSDavid Dillow 	return 0;
502175859bfSDavid Dillow }
503175859bfSDavid Dillow 
504175859bfSDavid Dillow static int sis_substream_close(struct snd_pcm_substream *substream)
505175859bfSDavid Dillow {
506175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
507175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
508175859bfSDavid Dillow 	struct voice *voice = runtime->private_data;
509175859bfSDavid Dillow 
510175859bfSDavid Dillow 	sis_free_voice(sis, voice);
511175859bfSDavid Dillow 	return 0;
512175859bfSDavid Dillow }
513175859bfSDavid Dillow 
514175859bfSDavid Dillow static int sis_playback_hw_params(struct snd_pcm_substream *substream,
515175859bfSDavid Dillow 					struct snd_pcm_hw_params *hw_params)
516175859bfSDavid Dillow {
517175859bfSDavid Dillow 	return snd_pcm_lib_malloc_pages(substream,
518175859bfSDavid Dillow 					params_buffer_bytes(hw_params));
519175859bfSDavid Dillow }
520175859bfSDavid Dillow 
521175859bfSDavid Dillow static int sis_hw_free(struct snd_pcm_substream *substream)
522175859bfSDavid Dillow {
523175859bfSDavid Dillow 	return snd_pcm_lib_free_pages(substream);
524175859bfSDavid Dillow }
525175859bfSDavid Dillow 
526175859bfSDavid Dillow static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
527175859bfSDavid Dillow {
528175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
529175859bfSDavid Dillow 	struct voice *voice = runtime->private_data;
530175859bfSDavid Dillow 	void __iomem *ctrl_base = voice->ctrl_base;
531175859bfSDavid Dillow 	void __iomem *wave_base = voice->wave_base;
532175859bfSDavid Dillow 	u32 format, dma_addr, control, sso_eso, delta, reg;
533175859bfSDavid Dillow 	u16 leo;
534175859bfSDavid Dillow 
535175859bfSDavid Dillow 	/* We rely on the PCM core to ensure that the parameters for this
536175859bfSDavid Dillow 	 * substream do not change on us while we're programming the HW.
537175859bfSDavid Dillow 	 */
538175859bfSDavid Dillow 	format = 0;
539175859bfSDavid Dillow 	if (snd_pcm_format_width(runtime->format) == 8)
540175859bfSDavid Dillow 		format |= SIS_PLAY_DMA_FORMAT_8BIT;
541175859bfSDavid Dillow 	if (!snd_pcm_format_signed(runtime->format))
542175859bfSDavid Dillow 		format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
543175859bfSDavid Dillow 	if (runtime->channels == 1)
544175859bfSDavid Dillow 		format |= SIS_PLAY_DMA_FORMAT_MONO;
545175859bfSDavid Dillow 
546175859bfSDavid Dillow 	/* The baseline setup is for a single period per buffer, and
547175859bfSDavid Dillow 	 * we add bells and whistles as needed from there.
548175859bfSDavid Dillow 	 */
549175859bfSDavid Dillow 	dma_addr = runtime->dma_addr;
550175859bfSDavid Dillow 	leo = runtime->buffer_size - 1;
551175859bfSDavid Dillow 	control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
552175859bfSDavid Dillow 	sso_eso = leo;
553175859bfSDavid Dillow 
554175859bfSDavid Dillow 	if (runtime->period_size == (runtime->buffer_size / 2)) {
555175859bfSDavid Dillow 		control |= SIS_PLAY_DMA_INTR_AT_MLP;
556175859bfSDavid Dillow 	} else if (runtime->period_size != runtime->buffer_size) {
557175859bfSDavid Dillow 		voice->flags |= VOICE_SSO_TIMING;
558175859bfSDavid Dillow 		voice->sso = runtime->period_size - 1;
559175859bfSDavid Dillow 		voice->period_size = runtime->period_size;
560175859bfSDavid Dillow 		voice->buffer_size = runtime->buffer_size;
561175859bfSDavid Dillow 
562175859bfSDavid Dillow 		control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
563175859bfSDavid Dillow 		control |= SIS_PLAY_DMA_INTR_AT_SSO;
564175859bfSDavid Dillow 		sso_eso |= (runtime->period_size - 1) << 16;
565175859bfSDavid Dillow 	}
566175859bfSDavid Dillow 
567175859bfSDavid Dillow 	delta = sis_rate_to_delta(runtime->rate);
568175859bfSDavid Dillow 
569175859bfSDavid Dillow 	/* Ok, we're ready to go, set up the channel.
570175859bfSDavid Dillow 	 */
571175859bfSDavid Dillow 	writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
572175859bfSDavid Dillow 	writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
573175859bfSDavid Dillow 	writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
574175859bfSDavid Dillow 	writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
575175859bfSDavid Dillow 
576175859bfSDavid Dillow 	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
577175859bfSDavid Dillow 		writel(0, wave_base + reg);
578175859bfSDavid Dillow 
579175859bfSDavid Dillow 	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
580175859bfSDavid Dillow 	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
581175859bfSDavid Dillow 	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
582175859bfSDavid Dillow 			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
583175859bfSDavid Dillow 			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
584175859bfSDavid Dillow 			wave_base + SIS_WAVE_CHANNEL_CONTROL);
585175859bfSDavid Dillow 
586175859bfSDavid Dillow 	/* Force PCI writes to post. */
587175859bfSDavid Dillow 	readl(ctrl_base);
588175859bfSDavid Dillow 
589175859bfSDavid Dillow 	return 0;
590175859bfSDavid Dillow }
591175859bfSDavid Dillow 
592175859bfSDavid Dillow static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
593175859bfSDavid Dillow {
594175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
595175859bfSDavid Dillow 	unsigned long io = sis->ioport;
596175859bfSDavid Dillow 	struct snd_pcm_substream *s;
597175859bfSDavid Dillow 	struct voice *voice;
598175859bfSDavid Dillow 	void *chip;
599175859bfSDavid Dillow 	int starting;
600175859bfSDavid Dillow 	u32 record = 0;
601175859bfSDavid Dillow 	u32 play[2] = { 0, 0 };
602175859bfSDavid Dillow 
603175859bfSDavid Dillow 	/* No locks needed, as the PCM core will hold the locks on the
604175859bfSDavid Dillow 	 * substreams, and the HW will only start/stop the indicated voices
605175859bfSDavid Dillow 	 * without changing the state of the others.
606175859bfSDavid Dillow 	 */
607175859bfSDavid Dillow 	switch (cmd) {
608175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_START:
609175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
610175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_RESUME:
611175859bfSDavid Dillow 		starting = 1;
612175859bfSDavid Dillow 		break;
613175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_STOP:
614175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
615175859bfSDavid Dillow 	case SNDRV_PCM_TRIGGER_SUSPEND:
616175859bfSDavid Dillow 		starting = 0;
617175859bfSDavid Dillow 		break;
618175859bfSDavid Dillow 	default:
619175859bfSDavid Dillow 		return -EINVAL;
620175859bfSDavid Dillow 	}
621175859bfSDavid Dillow 
622175859bfSDavid Dillow 	snd_pcm_group_for_each_entry(s, substream) {
623175859bfSDavid Dillow 		/* Make sure it is for us... */
624175859bfSDavid Dillow 		chip = snd_pcm_substream_chip(s);
625175859bfSDavid Dillow 		if (chip != sis)
626175859bfSDavid Dillow 			continue;
627175859bfSDavid Dillow 
628175859bfSDavid Dillow 		voice = s->runtime->private_data;
629175859bfSDavid Dillow 		if (voice->flags & VOICE_CAPTURE) {
630175859bfSDavid Dillow 			record |= 1 << voice->num;
631175859bfSDavid Dillow 			voice = voice->timing;
632175859bfSDavid Dillow 		}
633175859bfSDavid Dillow 
634175859bfSDavid Dillow 		/* voice could be NULL if this a recording stream, and it
635175859bfSDavid Dillow 		 * doesn't have an external timing channel.
636175859bfSDavid Dillow 		 */
637175859bfSDavid Dillow 		if (voice)
638175859bfSDavid Dillow 			play[voice->num / 32] |= 1 << (voice->num & 0x1f);
639175859bfSDavid Dillow 
640175859bfSDavid Dillow 		snd_pcm_trigger_done(s, substream);
641175859bfSDavid Dillow 	}
642175859bfSDavid Dillow 
643175859bfSDavid Dillow 	if (starting) {
644175859bfSDavid Dillow 		if (record)
645175859bfSDavid Dillow 			outl(record, io + SIS_RECORD_START_REG);
646175859bfSDavid Dillow 		if (play[0])
647175859bfSDavid Dillow 			outl(play[0], io + SIS_PLAY_START_A_REG);
648175859bfSDavid Dillow 		if (play[1])
649175859bfSDavid Dillow 			outl(play[1], io + SIS_PLAY_START_B_REG);
650175859bfSDavid Dillow 	} else {
651175859bfSDavid Dillow 		if (record)
652175859bfSDavid Dillow 			outl(record, io + SIS_RECORD_STOP_REG);
653175859bfSDavid Dillow 		if (play[0])
654175859bfSDavid Dillow 			outl(play[0], io + SIS_PLAY_STOP_A_REG);
655175859bfSDavid Dillow 		if (play[1])
656175859bfSDavid Dillow 			outl(play[1], io + SIS_PLAY_STOP_B_REG);
657175859bfSDavid Dillow 	}
658175859bfSDavid Dillow 	return 0;
659175859bfSDavid Dillow }
660175859bfSDavid Dillow 
661175859bfSDavid Dillow static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
662175859bfSDavid Dillow {
663175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
664175859bfSDavid Dillow 	struct voice *voice = runtime->private_data;
665175859bfSDavid Dillow 	u32 cso;
666175859bfSDavid Dillow 
667175859bfSDavid Dillow 	cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
668175859bfSDavid Dillow 	cso &= 0xffff;
669175859bfSDavid Dillow 	return cso;
670175859bfSDavid Dillow }
671175859bfSDavid Dillow 
672175859bfSDavid Dillow static int sis_capture_open(struct snd_pcm_substream *substream)
673175859bfSDavid Dillow {
674175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
675175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
676175859bfSDavid Dillow 	struct voice *voice = &sis->capture_voice;
677175859bfSDavid Dillow 	unsigned long flags;
678175859bfSDavid Dillow 
679175859bfSDavid Dillow 	/* FIXME: The driver only supports recording from one channel
680175859bfSDavid Dillow 	 * at the moment, but it could support more.
681175859bfSDavid Dillow 	 */
682175859bfSDavid Dillow 	spin_lock_irqsave(&sis->voice_lock, flags);
683175859bfSDavid Dillow 	if (voice->flags & VOICE_IN_USE)
684175859bfSDavid Dillow 		voice = NULL;
685175859bfSDavid Dillow 	else
686175859bfSDavid Dillow 		voice->flags |= VOICE_IN_USE;
687175859bfSDavid Dillow 	spin_unlock_irqrestore(&sis->voice_lock, flags);
688175859bfSDavid Dillow 
689175859bfSDavid Dillow 	if (!voice)
690175859bfSDavid Dillow 		return -EAGAIN;
691175859bfSDavid Dillow 
692175859bfSDavid Dillow 	voice->substream = substream;
693175859bfSDavid Dillow 	runtime->private_data = voice;
694175859bfSDavid Dillow 	runtime->hw = sis_capture_hw_info;
695175859bfSDavid Dillow 	runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
696175859bfSDavid Dillow 	snd_pcm_limit_hw_rates(runtime);
697175859bfSDavid Dillow 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
698175859bfSDavid Dillow 						9, 0xfff9);
699175859bfSDavid Dillow 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
700175859bfSDavid Dillow 						9, 0xfff9);
701175859bfSDavid Dillow 	snd_pcm_set_sync(substream);
702175859bfSDavid Dillow 	return 0;
703175859bfSDavid Dillow }
704175859bfSDavid Dillow 
705175859bfSDavid Dillow static int sis_capture_hw_params(struct snd_pcm_substream *substream,
706175859bfSDavid Dillow 					struct snd_pcm_hw_params *hw_params)
707175859bfSDavid Dillow {
708175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
709175859bfSDavid Dillow 	int rc;
710175859bfSDavid Dillow 
711175859bfSDavid Dillow 	rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
712175859bfSDavid Dillow 						params_rate(hw_params));
713175859bfSDavid Dillow 	if (rc)
714175859bfSDavid Dillow 		goto out;
715175859bfSDavid Dillow 
716175859bfSDavid Dillow 	rc = snd_pcm_lib_malloc_pages(substream,
717175859bfSDavid Dillow 					params_buffer_bytes(hw_params));
718175859bfSDavid Dillow 	if (rc < 0)
719175859bfSDavid Dillow 		goto out;
720175859bfSDavid Dillow 
721175859bfSDavid Dillow 	rc = sis_alloc_timing_voice(substream, hw_params);
722175859bfSDavid Dillow 
723175859bfSDavid Dillow out:
724175859bfSDavid Dillow 	return rc;
725175859bfSDavid Dillow }
726175859bfSDavid Dillow 
727175859bfSDavid Dillow static void sis_prepare_timing_voice(struct voice *voice,
728175859bfSDavid Dillow 					struct snd_pcm_substream *substream)
729175859bfSDavid Dillow {
730175859bfSDavid Dillow 	struct sis7019 *sis = snd_pcm_substream_chip(substream);
731175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
732175859bfSDavid Dillow 	struct voice *timing = voice->timing;
733175859bfSDavid Dillow 	void __iomem *play_base = timing->ctrl_base;
734175859bfSDavid Dillow 	void __iomem *wave_base = timing->wave_base;
735175859bfSDavid Dillow 	u16 buffer_size, period_size;
736175859bfSDavid Dillow 	u32 format, control, sso_eso, delta;
737175859bfSDavid Dillow 	u32 vperiod, sso, reg;
738175859bfSDavid Dillow 
739175859bfSDavid Dillow 	/* Set our initial buffer and period as large as we can given a
740175859bfSDavid Dillow 	 * single page of silence.
741175859bfSDavid Dillow 	 */
742175859bfSDavid Dillow 	buffer_size = 4096 / runtime->channels;
743175859bfSDavid Dillow 	buffer_size /= snd_pcm_format_size(runtime->format, 1);
744175859bfSDavid Dillow 	period_size = buffer_size;
745175859bfSDavid Dillow 
746175859bfSDavid Dillow 	/* Initially, we want to interrupt just a bit behind the end of
7473a3d5fd1SDavid Dillow 	 * the period we're clocking out. 12 samples seems to give a good
748175859bfSDavid Dillow 	 * delay.
749175859bfSDavid Dillow 	 *
750175859bfSDavid Dillow 	 * We want to spread our interrupts throughout the virtual period,
751175859bfSDavid Dillow 	 * so that we don't end up with two interrupts back to back at the
752175859bfSDavid Dillow 	 * end -- this helps minimize the effects of any jitter. Adjust our
753175859bfSDavid Dillow 	 * clocking period size so that the last period is at least a fourth
754175859bfSDavid Dillow 	 * of a full period.
755175859bfSDavid Dillow 	 *
756175859bfSDavid Dillow 	 * This is all moot if we don't need to use virtual periods.
757175859bfSDavid Dillow 	 */
7583a3d5fd1SDavid Dillow 	vperiod = runtime->period_size + 12;
759175859bfSDavid Dillow 	if (vperiod > period_size) {
760175859bfSDavid Dillow 		u16 tail = vperiod % period_size;
761175859bfSDavid Dillow 		u16 quarter_period = period_size / 4;
762175859bfSDavid Dillow 
763175859bfSDavid Dillow 		if (tail && tail < quarter_period) {
764175859bfSDavid Dillow 			u16 loops = vperiod / period_size;
765175859bfSDavid Dillow 
766175859bfSDavid Dillow 			tail = quarter_period - tail;
767175859bfSDavid Dillow 			tail += loops - 1;
768175859bfSDavid Dillow 			tail /= loops;
769175859bfSDavid Dillow 			period_size -= tail;
770175859bfSDavid Dillow 		}
771175859bfSDavid Dillow 
772175859bfSDavid Dillow 		sso = period_size - 1;
773175859bfSDavid Dillow 	} else {
774175859bfSDavid Dillow 		/* The initial period will fit inside the buffer, so we
775175859bfSDavid Dillow 		 * don't need to use virtual periods -- disable them.
776175859bfSDavid Dillow 		 */
777175859bfSDavid Dillow 		period_size = runtime->period_size;
778175859bfSDavid Dillow 		sso = vperiod - 1;
779175859bfSDavid Dillow 		vperiod = 0;
780175859bfSDavid Dillow 	}
781175859bfSDavid Dillow 
78225985edcSLucas De Marchi 	/* The interrupt handler implements the timing synchronization, so
783175859bfSDavid Dillow 	 * setup its state.
784175859bfSDavid Dillow 	 */
785175859bfSDavid Dillow 	timing->flags |= VOICE_SYNC_TIMING;
786175859bfSDavid Dillow 	timing->sync_base = voice->ctrl_base;
7873a3d5fd1SDavid Dillow 	timing->sync_cso = runtime->period_size;
788175859bfSDavid Dillow 	timing->sync_period_size = runtime->period_size;
789175859bfSDavid Dillow 	timing->sync_buffer_size = runtime->buffer_size;
790175859bfSDavid Dillow 	timing->period_size = period_size;
791175859bfSDavid Dillow 	timing->buffer_size = buffer_size;
792175859bfSDavid Dillow 	timing->sso = sso;
793175859bfSDavid Dillow 	timing->vperiod = vperiod;
794175859bfSDavid Dillow 
795175859bfSDavid Dillow 	/* Using unsigned samples with the all-zero silence buffer
796175859bfSDavid Dillow 	 * forces the output to the lower rail, killing playback.
797175859bfSDavid Dillow 	 * So ignore unsigned vs signed -- it doesn't change the timing.
798175859bfSDavid Dillow 	 */
799175859bfSDavid Dillow 	format = 0;
800175859bfSDavid Dillow 	if (snd_pcm_format_width(runtime->format) == 8)
801175859bfSDavid Dillow 		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
802175859bfSDavid Dillow 	if (runtime->channels == 1)
803175859bfSDavid Dillow 		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
804175859bfSDavid Dillow 
805175859bfSDavid Dillow 	control = timing->buffer_size - 1;
806175859bfSDavid Dillow 	control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
807175859bfSDavid Dillow 	sso_eso = timing->buffer_size - 1;
808175859bfSDavid Dillow 	sso_eso |= timing->sso << 16;
809175859bfSDavid Dillow 
810175859bfSDavid Dillow 	delta = sis_rate_to_delta(runtime->rate);
811175859bfSDavid Dillow 
812175859bfSDavid Dillow 	/* We've done the math, now configure the channel.
813175859bfSDavid Dillow 	 */
814175859bfSDavid Dillow 	writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
815175859bfSDavid Dillow 	writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
816175859bfSDavid Dillow 	writel(control, play_base + SIS_PLAY_DMA_CONTROL);
817175859bfSDavid Dillow 	writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
818175859bfSDavid Dillow 
819175859bfSDavid Dillow 	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
820175859bfSDavid Dillow 		writel(0, wave_base + reg);
821175859bfSDavid Dillow 
822175859bfSDavid Dillow 	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
823175859bfSDavid Dillow 	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
824175859bfSDavid Dillow 	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
825175859bfSDavid Dillow 			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
826175859bfSDavid Dillow 			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
827175859bfSDavid Dillow 			wave_base + SIS_WAVE_CHANNEL_CONTROL);
828175859bfSDavid Dillow }
829175859bfSDavid Dillow 
830175859bfSDavid Dillow static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
831175859bfSDavid Dillow {
832175859bfSDavid Dillow 	struct snd_pcm_runtime *runtime = substream->runtime;
833175859bfSDavid Dillow 	struct voice *voice = runtime->private_data;
834175859bfSDavid Dillow 	void __iomem *rec_base = voice->ctrl_base;
835175859bfSDavid Dillow 	u32 format, dma_addr, control;
836175859bfSDavid Dillow 	u16 leo;
837175859bfSDavid Dillow 
838175859bfSDavid Dillow 	/* We rely on the PCM core to ensure that the parameters for this
839175859bfSDavid Dillow 	 * substream do not change on us while we're programming the HW.
840175859bfSDavid Dillow 	 */
841175859bfSDavid Dillow 	format = 0;
842175859bfSDavid Dillow 	if (snd_pcm_format_width(runtime->format) == 8)
843175859bfSDavid Dillow 		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
844175859bfSDavid Dillow 	if (!snd_pcm_format_signed(runtime->format))
845175859bfSDavid Dillow 		format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
846175859bfSDavid Dillow 	if (runtime->channels == 1)
847175859bfSDavid Dillow 		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
848175859bfSDavid Dillow 
849175859bfSDavid Dillow 	dma_addr = runtime->dma_addr;
850175859bfSDavid Dillow 	leo = runtime->buffer_size - 1;
851175859bfSDavid Dillow 	control = leo | SIS_CAPTURE_DMA_LOOP;
852175859bfSDavid Dillow 
853175859bfSDavid Dillow 	/* If we've got more than two periods per buffer, then we have
854175859bfSDavid Dillow 	 * use a timing voice to clock out the periods. Otherwise, we can
855175859bfSDavid Dillow 	 * use the capture channel's interrupts.
856175859bfSDavid Dillow 	 */
857175859bfSDavid Dillow 	if (voice->timing) {
858175859bfSDavid Dillow 		sis_prepare_timing_voice(voice, substream);
859175859bfSDavid Dillow 	} else {
860175859bfSDavid Dillow 		control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
861175859bfSDavid Dillow 		if (runtime->period_size != runtime->buffer_size)
862175859bfSDavid Dillow 			control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
863175859bfSDavid Dillow 	}
864175859bfSDavid Dillow 
865175859bfSDavid Dillow 	writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
866175859bfSDavid Dillow 	writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
867175859bfSDavid Dillow 	writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
868175859bfSDavid Dillow 
869175859bfSDavid Dillow 	/* Force the writes to post. */
870175859bfSDavid Dillow 	readl(rec_base);
871175859bfSDavid Dillow 
872175859bfSDavid Dillow 	return 0;
873175859bfSDavid Dillow }
874175859bfSDavid Dillow 
875175859bfSDavid Dillow static struct snd_pcm_ops sis_playback_ops = {
876175859bfSDavid Dillow 	.open = sis_playback_open,
877175859bfSDavid Dillow 	.close = sis_substream_close,
878175859bfSDavid Dillow 	.ioctl = snd_pcm_lib_ioctl,
879175859bfSDavid Dillow 	.hw_params = sis_playback_hw_params,
880175859bfSDavid Dillow 	.hw_free = sis_hw_free,
881175859bfSDavid Dillow 	.prepare = sis_pcm_playback_prepare,
882175859bfSDavid Dillow 	.trigger = sis_pcm_trigger,
883175859bfSDavid Dillow 	.pointer = sis_pcm_pointer,
884175859bfSDavid Dillow };
885175859bfSDavid Dillow 
886175859bfSDavid Dillow static struct snd_pcm_ops sis_capture_ops = {
887175859bfSDavid Dillow 	.open = sis_capture_open,
888175859bfSDavid Dillow 	.close = sis_substream_close,
889175859bfSDavid Dillow 	.ioctl = snd_pcm_lib_ioctl,
890175859bfSDavid Dillow 	.hw_params = sis_capture_hw_params,
891175859bfSDavid Dillow 	.hw_free = sis_hw_free,
892175859bfSDavid Dillow 	.prepare = sis_pcm_capture_prepare,
893175859bfSDavid Dillow 	.trigger = sis_pcm_trigger,
894175859bfSDavid Dillow 	.pointer = sis_pcm_pointer,
895175859bfSDavid Dillow };
896175859bfSDavid Dillow 
897e23e7a14SBill Pemberton static int sis_pcm_create(struct sis7019 *sis)
898175859bfSDavid Dillow {
899175859bfSDavid Dillow 	struct snd_pcm *pcm;
900175859bfSDavid Dillow 	int rc;
901175859bfSDavid Dillow 
902175859bfSDavid Dillow 	/* We have 64 voices, and the driver currently records from
903175859bfSDavid Dillow 	 * only one channel, though that could change in the future.
904175859bfSDavid Dillow 	 */
905175859bfSDavid Dillow 	rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
906175859bfSDavid Dillow 	if (rc)
907175859bfSDavid Dillow 		return rc;
908175859bfSDavid Dillow 
909175859bfSDavid Dillow 	pcm->private_data = sis;
910175859bfSDavid Dillow 	strcpy(pcm->name, "SiS7019");
911175859bfSDavid Dillow 	sis->pcm = pcm;
912175859bfSDavid Dillow 
913175859bfSDavid Dillow 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
914175859bfSDavid Dillow 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
915175859bfSDavid Dillow 
916175859bfSDavid Dillow 	/* Try to preallocate some memory, but it's not the end of the
917175859bfSDavid Dillow 	 * world if this fails.
918175859bfSDavid Dillow 	 */
919175859bfSDavid Dillow 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
920175859bfSDavid Dillow 				snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
921175859bfSDavid Dillow 
922175859bfSDavid Dillow 	return 0;
923175859bfSDavid Dillow }
924175859bfSDavid Dillow 
925175859bfSDavid Dillow static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
926175859bfSDavid Dillow {
927175859bfSDavid Dillow 	unsigned long io = sis->ioport;
928175859bfSDavid Dillow 	unsigned short val = 0xffff;
929175859bfSDavid Dillow 	u16 status;
930175859bfSDavid Dillow 	u16 rdy;
931175859bfSDavid Dillow 	int count;
9323f76d984STobias Klauser 	static const u16 codec_ready[3] = {
933175859bfSDavid Dillow 		SIS_AC97_STATUS_CODEC_READY,
934175859bfSDavid Dillow 		SIS_AC97_STATUS_CODEC2_READY,
935175859bfSDavid Dillow 		SIS_AC97_STATUS_CODEC3_READY,
936175859bfSDavid Dillow 	};
937175859bfSDavid Dillow 
938175859bfSDavid Dillow 	rdy = codec_ready[codec];
939175859bfSDavid Dillow 
940175859bfSDavid Dillow 
941175859bfSDavid Dillow 	/* Get the AC97 semaphore -- software first, so we don't spin
942175859bfSDavid Dillow 	 * pounding out IO reads on the hardware semaphore...
943175859bfSDavid Dillow 	 */
944175859bfSDavid Dillow 	mutex_lock(&sis->ac97_mutex);
945175859bfSDavid Dillow 
946175859bfSDavid Dillow 	count = 0xffff;
947175859bfSDavid Dillow 	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
948175859bfSDavid Dillow 		udelay(1);
949175859bfSDavid Dillow 
950175859bfSDavid Dillow 	if (!count)
951175859bfSDavid Dillow 		goto timeout;
952175859bfSDavid Dillow 
953175859bfSDavid Dillow 	/* ... and wait for any outstanding commands to complete ...
954175859bfSDavid Dillow 	 */
955175859bfSDavid Dillow 	count = 0xffff;
956175859bfSDavid Dillow 	do {
957175859bfSDavid Dillow 		status = inw(io + SIS_AC97_STATUS);
958175859bfSDavid Dillow 		if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
959175859bfSDavid Dillow 			break;
960175859bfSDavid Dillow 
961175859bfSDavid Dillow 		udelay(1);
962175859bfSDavid Dillow 	} while (--count);
963175859bfSDavid Dillow 
964175859bfSDavid Dillow 	if (!count)
965175859bfSDavid Dillow 		goto timeout_sema;
966175859bfSDavid Dillow 
967175859bfSDavid Dillow 	/* ... before sending our command and waiting for it to finish ...
968175859bfSDavid Dillow 	 */
969175859bfSDavid Dillow 	outl(cmd, io + SIS_AC97_CMD);
970175859bfSDavid Dillow 	udelay(10);
971175859bfSDavid Dillow 
972175859bfSDavid Dillow 	count = 0xffff;
973175859bfSDavid Dillow 	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
974175859bfSDavid Dillow 		udelay(1);
975175859bfSDavid Dillow 
976175859bfSDavid Dillow 	/* ... and reading the results (if any).
977175859bfSDavid Dillow 	 */
978175859bfSDavid Dillow 	val = inl(io + SIS_AC97_CMD) >> 16;
979175859bfSDavid Dillow 
980175859bfSDavid Dillow timeout_sema:
981175859bfSDavid Dillow 	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
982175859bfSDavid Dillow timeout:
983175859bfSDavid Dillow 	mutex_unlock(&sis->ac97_mutex);
984175859bfSDavid Dillow 
985175859bfSDavid Dillow 	if (!count) {
98670597851SDavid Dillow 		dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
987175859bfSDavid Dillow 					codec, cmd);
988175859bfSDavid Dillow 	}
989175859bfSDavid Dillow 
990175859bfSDavid Dillow 	return val;
991175859bfSDavid Dillow }
992175859bfSDavid Dillow 
993175859bfSDavid Dillow static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
994175859bfSDavid Dillow 				unsigned short val)
995175859bfSDavid Dillow {
9963f76d984STobias Klauser 	static const u32 cmd[3] = {
997175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC_WRITE,
998175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC2_WRITE,
999175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC3_WRITE,
1000175859bfSDavid Dillow 	};
1001175859bfSDavid Dillow 	sis_ac97_rw(ac97->private_data, ac97->num,
1002175859bfSDavid Dillow 			(val << 16) | (reg << 8) | cmd[ac97->num]);
1003175859bfSDavid Dillow }
1004175859bfSDavid Dillow 
1005175859bfSDavid Dillow static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
1006175859bfSDavid Dillow {
10073f76d984STobias Klauser 	static const u32 cmd[3] = {
1008175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC_READ,
1009175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC2_READ,
1010175859bfSDavid Dillow 		SIS_AC97_CMD_CODEC3_READ,
1011175859bfSDavid Dillow 	};
1012175859bfSDavid Dillow 	return sis_ac97_rw(ac97->private_data, ac97->num,
1013175859bfSDavid Dillow 					(reg << 8) | cmd[ac97->num]);
1014175859bfSDavid Dillow }
1015175859bfSDavid Dillow 
1016e23e7a14SBill Pemberton static int sis_mixer_create(struct sis7019 *sis)
1017175859bfSDavid Dillow {
1018175859bfSDavid Dillow 	struct snd_ac97_bus *bus;
1019175859bfSDavid Dillow 	struct snd_ac97_template ac97;
1020175859bfSDavid Dillow 	static struct snd_ac97_bus_ops ops = {
1021175859bfSDavid Dillow 		.write = sis_ac97_write,
1022175859bfSDavid Dillow 		.read = sis_ac97_read,
1023175859bfSDavid Dillow 	};
1024175859bfSDavid Dillow 	int rc;
1025175859bfSDavid Dillow 
1026175859bfSDavid Dillow 	memset(&ac97, 0, sizeof(ac97));
1027175859bfSDavid Dillow 	ac97.private_data = sis;
1028175859bfSDavid Dillow 
1029175859bfSDavid Dillow 	rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1030175859bfSDavid Dillow 	if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1031175859bfSDavid Dillow 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1032175859bfSDavid Dillow 	ac97.num = 1;
1033175859bfSDavid Dillow 	if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1034175859bfSDavid Dillow 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1035175859bfSDavid Dillow 	ac97.num = 2;
1036175859bfSDavid Dillow 	if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1037175859bfSDavid Dillow 		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1038175859bfSDavid Dillow 
1039175859bfSDavid Dillow 	/* If we return an error here, then snd_card_free() should
1040175859bfSDavid Dillow 	 * free up any ac97 codecs that got created, as well as the bus.
1041175859bfSDavid Dillow 	 */
1042175859bfSDavid Dillow 	return rc;
1043175859bfSDavid Dillow }
1044175859bfSDavid Dillow 
1045175859bfSDavid Dillow static void sis_free_suspend(struct sis7019 *sis)
1046175859bfSDavid Dillow {
1047175859bfSDavid Dillow 	int i;
1048175859bfSDavid Dillow 
1049175859bfSDavid Dillow 	for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1050175859bfSDavid Dillow 		kfree(sis->suspend_state[i]);
1051175859bfSDavid Dillow }
1052175859bfSDavid Dillow 
1053175859bfSDavid Dillow static int sis_chip_free(struct sis7019 *sis)
1054175859bfSDavid Dillow {
1055175859bfSDavid Dillow 	/* Reset the chip, and disable all interrputs.
1056175859bfSDavid Dillow 	 */
1057175859bfSDavid Dillow 	outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
105808b45098SDavid Dillow 	udelay(25);
1059175859bfSDavid Dillow 	outl(0, sis->ioport + SIS_GCR);
1060175859bfSDavid Dillow 	outl(0, sis->ioport + SIS_GIER);
1061175859bfSDavid Dillow 
1062175859bfSDavid Dillow 	/* Now, free everything we allocated.
1063175859bfSDavid Dillow 	 */
1064175859bfSDavid Dillow 	if (sis->irq >= 0)
1065175859bfSDavid Dillow 		free_irq(sis->irq, sis);
1066175859bfSDavid Dillow 
1067175859bfSDavid Dillow 	iounmap(sis->ioaddr);
1068175859bfSDavid Dillow 	pci_release_regions(sis->pci);
1069175859bfSDavid Dillow 	pci_disable_device(sis->pci);
1070175859bfSDavid Dillow 	sis_free_suspend(sis);
1071175859bfSDavid Dillow 	return 0;
1072175859bfSDavid Dillow }
1073175859bfSDavid Dillow 
1074175859bfSDavid Dillow static int sis_dev_free(struct snd_device *dev)
1075175859bfSDavid Dillow {
1076175859bfSDavid Dillow 	struct sis7019 *sis = dev->device_data;
1077175859bfSDavid Dillow 	return sis_chip_free(sis);
1078175859bfSDavid Dillow }
1079175859bfSDavid Dillow 
1080175859bfSDavid Dillow static int sis_chip_init(struct sis7019 *sis)
1081175859bfSDavid Dillow {
1082175859bfSDavid Dillow 	unsigned long io = sis->ioport;
1083175859bfSDavid Dillow 	void __iomem *ioaddr = sis->ioaddr;
1084fc084e0bSDavid Dillow 	unsigned long timeout;
1085175859bfSDavid Dillow 	u16 status;
1086175859bfSDavid Dillow 	int count;
1087175859bfSDavid Dillow 	int i;
1088175859bfSDavid Dillow 
1089175859bfSDavid Dillow 	/* Reset the audio controller
1090175859bfSDavid Dillow 	 */
1091175859bfSDavid Dillow 	outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
109208b45098SDavid Dillow 	udelay(25);
1093175859bfSDavid Dillow 	outl(0, io + SIS_GCR);
1094175859bfSDavid Dillow 
1095175859bfSDavid Dillow 	/* Get the AC-link semaphore, and reset the codecs
1096175859bfSDavid Dillow 	 */
1097175859bfSDavid Dillow 	count = 0xffff;
1098175859bfSDavid Dillow 	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1099175859bfSDavid Dillow 		udelay(1);
1100175859bfSDavid Dillow 
1101175859bfSDavid Dillow 	if (!count)
1102175859bfSDavid Dillow 		return -EIO;
1103175859bfSDavid Dillow 
1104175859bfSDavid Dillow 	outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
110508b45098SDavid Dillow 	udelay(250);
1106175859bfSDavid Dillow 
1107175859bfSDavid Dillow 	count = 0xffff;
1108175859bfSDavid Dillow 	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1109175859bfSDavid Dillow 		udelay(1);
1110175859bfSDavid Dillow 
1111fc084e0bSDavid Dillow 	/* Command complete, we can let go of the semaphore now.
1112175859bfSDavid Dillow 	 */
1113fc084e0bSDavid Dillow 	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1114fc084e0bSDavid Dillow 	if (!count)
1115fc084e0bSDavid Dillow 		return -EIO;
1116fc084e0bSDavid Dillow 
1117fc084e0bSDavid Dillow 	/* Now that we've finished the reset, find out what's attached.
1118fc084e0bSDavid Dillow 	 * There are some codec/board combinations that take an extremely
1119fc084e0bSDavid Dillow 	 * long time to come up. 350+ ms has been observed in the field,
1120fc084e0bSDavid Dillow 	 * so we'll give them up to 500ms.
1121fc084e0bSDavid Dillow 	 */
1122fc084e0bSDavid Dillow 	sis->codecs_present = 0;
1123fc084e0bSDavid Dillow 	timeout = msecs_to_jiffies(500) + jiffies;
1124fc084e0bSDavid Dillow 	while (time_before_eq(jiffies, timeout)) {
1125175859bfSDavid Dillow 		status = inl(io + SIS_AC97_STATUS);
1126175859bfSDavid Dillow 		if (status & SIS_AC97_STATUS_CODEC_READY)
1127175859bfSDavid Dillow 			sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1128175859bfSDavid Dillow 		if (status & SIS_AC97_STATUS_CODEC2_READY)
1129175859bfSDavid Dillow 			sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1130175859bfSDavid Dillow 		if (status & SIS_AC97_STATUS_CODEC3_READY)
1131175859bfSDavid Dillow 			sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1132175859bfSDavid Dillow 
1133fc084e0bSDavid Dillow 		if (sis->codecs_present == codecs)
1134fc084e0bSDavid Dillow 			break;
1135fc084e0bSDavid Dillow 
1136fc084e0bSDavid Dillow 		msleep(1);
1137fc084e0bSDavid Dillow 	}
1138fc084e0bSDavid Dillow 
1139fc084e0bSDavid Dillow 	/* All done, check for errors.
1140175859bfSDavid Dillow 	 */
1141fc084e0bSDavid Dillow 	if (!sis->codecs_present) {
114270597851SDavid Dillow 		dev_err(&sis->pci->dev, "could not find any codecs\n");
1143175859bfSDavid Dillow 		return -EIO;
1144fc084e0bSDavid Dillow 	}
1145fc084e0bSDavid Dillow 
1146fc084e0bSDavid Dillow 	if (sis->codecs_present != codecs) {
114770597851SDavid Dillow 		dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1148fc084e0bSDavid Dillow 					 sis->codecs_present, codecs);
1149fc084e0bSDavid Dillow 	}
1150175859bfSDavid Dillow 
1151175859bfSDavid Dillow 	/* Let the hardware know that the audio driver is alive,
1152175859bfSDavid Dillow 	 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1153175859bfSDavid Dillow 	 * record channels. We're going to want to use Variable Rate Audio
1154175859bfSDavid Dillow 	 * for recording, to avoid needlessly resampling from 48kHZ.
1155175859bfSDavid Dillow 	 */
1156175859bfSDavid Dillow 	outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1157175859bfSDavid Dillow 	outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1158175859bfSDavid Dillow 		SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1159175859bfSDavid Dillow 		SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1160175859bfSDavid Dillow 		SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1161175859bfSDavid Dillow 
1162175859bfSDavid Dillow 	/* All AC97 PCM slots should be sourced from sub-mixer 0.
1163175859bfSDavid Dillow 	 */
1164175859bfSDavid Dillow 	outl(0, io + SIS_AC97_PSR);
1165175859bfSDavid Dillow 
1166175859bfSDavid Dillow 	/* There is only one valid DMA setup for a PCI environment.
1167175859bfSDavid Dillow 	 */
1168175859bfSDavid Dillow 	outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1169175859bfSDavid Dillow 
117025985edcSLucas De Marchi 	/* Reset the synchronization groups for all of the channels
1171b3834be5SAdam Buchbinder 	 * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
1172175859bfSDavid Dillow 	 * we'll need to change how we handle these. Until then, we just
1173175859bfSDavid Dillow 	 * assign sub-mixer 0 to all playback channels, and avoid any
1174175859bfSDavid Dillow 	 * attenuation on the audio.
1175175859bfSDavid Dillow 	 */
1176175859bfSDavid Dillow 	outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1177175859bfSDavid Dillow 	outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1178175859bfSDavid Dillow 	outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1179175859bfSDavid Dillow 	outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1180175859bfSDavid Dillow 	outl(0, io + SIS_MIXER_SYNC_GROUP);
1181175859bfSDavid Dillow 
1182175859bfSDavid Dillow 	for (i = 0; i < 64; i++) {
1183175859bfSDavid Dillow 		writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1184175859bfSDavid Dillow 		writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1185175859bfSDavid Dillow 				SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1186175859bfSDavid Dillow 	}
1187175859bfSDavid Dillow 
1188175859bfSDavid Dillow 	/* Don't attenuate any audio set for the wave amplifier.
1189175859bfSDavid Dillow 	 *
1190175859bfSDavid Dillow 	 * FIXME: Maximum attenuation is set for the music amp, which will
1191175859bfSDavid Dillow 	 * need to change if we start using the synth engine.
1192175859bfSDavid Dillow 	 */
1193175859bfSDavid Dillow 	outl(0xffff0000, io + SIS_WEVCR);
1194175859bfSDavid Dillow 
1195175859bfSDavid Dillow 	/* Ensure that the wave engine is in normal operating mode.
1196175859bfSDavid Dillow 	 */
1197175859bfSDavid Dillow 	outl(0, io + SIS_WECCR);
1198175859bfSDavid Dillow 
1199175859bfSDavid Dillow 	/* Go ahead and enable the DMA interrupts. They won't go live
1200175859bfSDavid Dillow 	 * until we start a channel.
1201175859bfSDavid Dillow 	 */
1202175859bfSDavid Dillow 	outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1203175859bfSDavid Dillow 		SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1204175859bfSDavid Dillow 
1205175859bfSDavid Dillow 	return 0;
1206175859bfSDavid Dillow }
1207175859bfSDavid Dillow 
1208c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
120968cb2b55STakashi Iwai static int sis_suspend(struct device *dev)
1210175859bfSDavid Dillow {
121168cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1212175859bfSDavid Dillow 	struct sis7019 *sis = card->private_data;
1213175859bfSDavid Dillow 	void __iomem *ioaddr = sis->ioaddr;
1214175859bfSDavid Dillow 	int i;
1215175859bfSDavid Dillow 
1216175859bfSDavid Dillow 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1217175859bfSDavid Dillow 	snd_pcm_suspend_all(sis->pcm);
1218175859bfSDavid Dillow 	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1219175859bfSDavid Dillow 		snd_ac97_suspend(sis->ac97[0]);
1220175859bfSDavid Dillow 	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1221175859bfSDavid Dillow 		snd_ac97_suspend(sis->ac97[1]);
1222175859bfSDavid Dillow 	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1223175859bfSDavid Dillow 		snd_ac97_suspend(sis->ac97[2]);
1224175859bfSDavid Dillow 
1225175859bfSDavid Dillow 	/* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1226175859bfSDavid Dillow 	 */
1227175859bfSDavid Dillow 	if (sis->irq >= 0) {
1228175859bfSDavid Dillow 		free_irq(sis->irq, sis);
1229175859bfSDavid Dillow 		sis->irq = -1;
1230175859bfSDavid Dillow 	}
1231175859bfSDavid Dillow 
1232175859bfSDavid Dillow 	/* Save the internal state away
1233175859bfSDavid Dillow 	 */
1234175859bfSDavid Dillow 	for (i = 0; i < 4; i++) {
1235175859bfSDavid Dillow 		memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1236175859bfSDavid Dillow 		ioaddr += 4096;
1237175859bfSDavid Dillow 	}
1238175859bfSDavid Dillow 
1239175859bfSDavid Dillow 	return 0;
1240175859bfSDavid Dillow }
1241175859bfSDavid Dillow 
124268cb2b55STakashi Iwai static int sis_resume(struct device *dev)
1243175859bfSDavid Dillow {
124468cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
124568cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1246175859bfSDavid Dillow 	struct sis7019 *sis = card->private_data;
1247175859bfSDavid Dillow 	void __iomem *ioaddr = sis->ioaddr;
1248175859bfSDavid Dillow 	int i;
1249175859bfSDavid Dillow 
1250175859bfSDavid Dillow 	if (sis_chip_init(sis)) {
125170597851SDavid Dillow 		dev_err(&pci->dev, "unable to re-init controller\n");
1252175859bfSDavid Dillow 		goto error;
1253175859bfSDavid Dillow 	}
1254175859bfSDavid Dillow 
125588e24c3aSYong Zhang 	if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1256934c2b6dSTakashi Iwai 			KBUILD_MODNAME, sis)) {
125770597851SDavid Dillow 		dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1258175859bfSDavid Dillow 		goto error;
1259175859bfSDavid Dillow 	}
1260175859bfSDavid Dillow 
1261175859bfSDavid Dillow 	/* Restore saved state, then clear out the page we use for the
1262175859bfSDavid Dillow 	 * silence buffer.
1263175859bfSDavid Dillow 	 */
1264175859bfSDavid Dillow 	for (i = 0; i < 4; i++) {
1265175859bfSDavid Dillow 		memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1266175859bfSDavid Dillow 		ioaddr += 4096;
1267175859bfSDavid Dillow 	}
1268175859bfSDavid Dillow 
1269175859bfSDavid Dillow 	memset(sis->suspend_state[0], 0, 4096);
1270175859bfSDavid Dillow 
1271175859bfSDavid Dillow 	sis->irq = pci->irq;
1272175859bfSDavid Dillow 
1273175859bfSDavid Dillow 	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1274175859bfSDavid Dillow 		snd_ac97_resume(sis->ac97[0]);
1275175859bfSDavid Dillow 	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1276175859bfSDavid Dillow 		snd_ac97_resume(sis->ac97[1]);
1277175859bfSDavid Dillow 	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1278175859bfSDavid Dillow 		snd_ac97_resume(sis->ac97[2]);
1279175859bfSDavid Dillow 
1280175859bfSDavid Dillow 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1281175859bfSDavid Dillow 	return 0;
1282175859bfSDavid Dillow 
1283175859bfSDavid Dillow error:
1284175859bfSDavid Dillow 	snd_card_disconnect(card);
1285175859bfSDavid Dillow 	return -EIO;
1286175859bfSDavid Dillow }
128768cb2b55STakashi Iwai 
128868cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
128968cb2b55STakashi Iwai #define SIS_PM_OPS	&sis_pm
129068cb2b55STakashi Iwai #else
129168cb2b55STakashi Iwai #define SIS_PM_OPS	NULL
1292c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1293175859bfSDavid Dillow 
1294175859bfSDavid Dillow static int sis_alloc_suspend(struct sis7019 *sis)
1295175859bfSDavid Dillow {
1296175859bfSDavid Dillow 	int i;
1297175859bfSDavid Dillow 
1298175859bfSDavid Dillow 	/* We need 16K to store the internal wave engine state during a
1299175859bfSDavid Dillow 	 * suspend, but we don't need it to be contiguous, so play nice
1300175859bfSDavid Dillow 	 * with the memory system. We'll also use this area for a silence
1301175859bfSDavid Dillow 	 * buffer.
1302175859bfSDavid Dillow 	 */
1303175859bfSDavid Dillow 	for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1304175859bfSDavid Dillow 		sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1305175859bfSDavid Dillow 		if (!sis->suspend_state[i])
1306175859bfSDavid Dillow 			return -ENOMEM;
1307175859bfSDavid Dillow 	}
1308175859bfSDavid Dillow 	memset(sis->suspend_state[0], 0, 4096);
1309175859bfSDavid Dillow 
1310175859bfSDavid Dillow 	return 0;
1311175859bfSDavid Dillow }
1312175859bfSDavid Dillow 
1313e23e7a14SBill Pemberton static int sis_chip_create(struct snd_card *card,
1314175859bfSDavid Dillow 			   struct pci_dev *pci)
1315175859bfSDavid Dillow {
1316175859bfSDavid Dillow 	struct sis7019 *sis = card->private_data;
1317175859bfSDavid Dillow 	struct voice *voice;
1318175859bfSDavid Dillow 	static struct snd_device_ops ops = {
1319175859bfSDavid Dillow 		.dev_free = sis_dev_free,
1320175859bfSDavid Dillow 	};
1321175859bfSDavid Dillow 	int rc;
1322175859bfSDavid Dillow 	int i;
1323175859bfSDavid Dillow 
1324175859bfSDavid Dillow 	rc = pci_enable_device(pci);
1325175859bfSDavid Dillow 	if (rc)
1326175859bfSDavid Dillow 		goto error_out;
1327175859bfSDavid Dillow 
1328412b979cSQuentin Lambert 	rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
13298b1dacb6SWei Yongjun 	if (rc < 0) {
133070597851SDavid Dillow 		dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1331175859bfSDavid Dillow 		goto error_out_enabled;
1332175859bfSDavid Dillow 	}
1333175859bfSDavid Dillow 
1334175859bfSDavid Dillow 	memset(sis, 0, sizeof(*sis));
1335175859bfSDavid Dillow 	mutex_init(&sis->ac97_mutex);
1336175859bfSDavid Dillow 	spin_lock_init(&sis->voice_lock);
1337175859bfSDavid Dillow 	sis->card = card;
1338175859bfSDavid Dillow 	sis->pci = pci;
1339175859bfSDavid Dillow 	sis->irq = -1;
1340175859bfSDavid Dillow 	sis->ioport = pci_resource_start(pci, 0);
1341175859bfSDavid Dillow 
1342175859bfSDavid Dillow 	rc = pci_request_regions(pci, "SiS7019");
1343175859bfSDavid Dillow 	if (rc) {
134470597851SDavid Dillow 		dev_err(&pci->dev, "unable request regions\n");
1345175859bfSDavid Dillow 		goto error_out_enabled;
1346175859bfSDavid Dillow 	}
1347175859bfSDavid Dillow 
1348175859bfSDavid Dillow 	rc = -EIO;
1349175859bfSDavid Dillow 	sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1350175859bfSDavid Dillow 	if (!sis->ioaddr) {
135170597851SDavid Dillow 		dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1352175859bfSDavid Dillow 		goto error_out_cleanup;
1353175859bfSDavid Dillow 	}
1354175859bfSDavid Dillow 
1355175859bfSDavid Dillow 	rc = sis_alloc_suspend(sis);
1356175859bfSDavid Dillow 	if (rc < 0) {
135770597851SDavid Dillow 		dev_err(&pci->dev, "unable to allocate state storage\n");
1358175859bfSDavid Dillow 		goto error_out_cleanup;
1359175859bfSDavid Dillow 	}
1360175859bfSDavid Dillow 
1361175859bfSDavid Dillow 	rc = sis_chip_init(sis);
1362175859bfSDavid Dillow 	if (rc)
1363175859bfSDavid Dillow 		goto error_out_cleanup;
1364175859bfSDavid Dillow 
1365ae970eb4SJulia Lawall 	rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1366ae970eb4SJulia Lawall 			 sis);
1367ae970eb4SJulia Lawall 	if (rc) {
136870597851SDavid Dillow 		dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1369175859bfSDavid Dillow 		goto error_out_cleanup;
1370175859bfSDavid Dillow 	}
1371175859bfSDavid Dillow 
1372175859bfSDavid Dillow 	sis->irq = pci->irq;
1373175859bfSDavid Dillow 	pci_set_master(pci);
1374175859bfSDavid Dillow 
1375175859bfSDavid Dillow 	for (i = 0; i < 64; i++) {
1376175859bfSDavid Dillow 		voice = &sis->voices[i];
1377175859bfSDavid Dillow 		voice->num = i;
1378175859bfSDavid Dillow 		voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1379175859bfSDavid Dillow 		voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1380175859bfSDavid Dillow 	}
1381175859bfSDavid Dillow 
1382175859bfSDavid Dillow 	voice = &sis->capture_voice;
1383175859bfSDavid Dillow 	voice->flags = VOICE_CAPTURE;
1384175859bfSDavid Dillow 	voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1385175859bfSDavid Dillow 	voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1386175859bfSDavid Dillow 
1387175859bfSDavid Dillow 	rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1388175859bfSDavid Dillow 	if (rc)
1389175859bfSDavid Dillow 		goto error_out_cleanup;
1390175859bfSDavid Dillow 
1391175859bfSDavid Dillow 	return 0;
1392175859bfSDavid Dillow 
1393175859bfSDavid Dillow error_out_cleanup:
1394175859bfSDavid Dillow 	sis_chip_free(sis);
1395175859bfSDavid Dillow 
1396175859bfSDavid Dillow error_out_enabled:
1397175859bfSDavid Dillow 	pci_disable_device(pci);
1398175859bfSDavid Dillow 
1399175859bfSDavid Dillow error_out:
1400175859bfSDavid Dillow 	return rc;
1401175859bfSDavid Dillow }
1402175859bfSDavid Dillow 
1403e23e7a14SBill Pemberton static int snd_sis7019_probe(struct pci_dev *pci,
1404175859bfSDavid Dillow 			     const struct pci_device_id *pci_id)
1405175859bfSDavid Dillow {
1406175859bfSDavid Dillow 	struct snd_card *card;
1407175859bfSDavid Dillow 	struct sis7019 *sis;
1408175859bfSDavid Dillow 	int rc;
1409175859bfSDavid Dillow 
1410175859bfSDavid Dillow 	rc = -ENOENT;
1411175859bfSDavid Dillow 	if (!enable)
1412175859bfSDavid Dillow 		goto error_out;
1413175859bfSDavid Dillow 
1414fc084e0bSDavid Dillow 	/* The user can specify which codecs should be present so that we
1415fc084e0bSDavid Dillow 	 * can wait for them to show up if they are slow to recover from
1416fc084e0bSDavid Dillow 	 * the AC97 cold reset. We default to a single codec, the primary.
1417fc084e0bSDavid Dillow 	 *
1418fc084e0bSDavid Dillow 	 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1419fc084e0bSDavid Dillow 	 */
1420fc084e0bSDavid Dillow 	codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1421fc084e0bSDavid Dillow 		  SIS_TERTIARY_CODEC_PRESENT;
1422fc084e0bSDavid Dillow 	if (!codecs)
1423fc084e0bSDavid Dillow 		codecs = SIS_PRIMARY_CODEC_PRESENT;
1424fc084e0bSDavid Dillow 
142560c5772bSTakashi Iwai 	rc = snd_card_new(&pci->dev, index, id, THIS_MODULE,
142660c5772bSTakashi Iwai 			  sizeof(*sis), &card);
1427e58de7baSTakashi Iwai 	if (rc < 0)
1428175859bfSDavid Dillow 		goto error_out;
1429175859bfSDavid Dillow 
1430175859bfSDavid Dillow 	strcpy(card->driver, "SiS7019");
1431175859bfSDavid Dillow 	strcpy(card->shortname, "SiS7019");
1432175859bfSDavid Dillow 	rc = sis_chip_create(card, pci);
1433175859bfSDavid Dillow 	if (rc)
1434175859bfSDavid Dillow 		goto card_error_out;
1435175859bfSDavid Dillow 
1436175859bfSDavid Dillow 	sis = card->private_data;
1437175859bfSDavid Dillow 
1438175859bfSDavid Dillow 	rc = sis_mixer_create(sis);
1439175859bfSDavid Dillow 	if (rc)
1440175859bfSDavid Dillow 		goto card_error_out;
1441175859bfSDavid Dillow 
1442175859bfSDavid Dillow 	rc = sis_pcm_create(sis);
1443175859bfSDavid Dillow 	if (rc)
1444175859bfSDavid Dillow 		goto card_error_out;
1445175859bfSDavid Dillow 
1446175859bfSDavid Dillow 	snprintf(card->longname, sizeof(card->longname),
1447175859bfSDavid Dillow 			"%s Audio Accelerator with %s at 0x%lx, irq %d",
1448175859bfSDavid Dillow 			card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1449175859bfSDavid Dillow 			sis->ioport, sis->irq);
1450175859bfSDavid Dillow 
1451175859bfSDavid Dillow 	rc = snd_card_register(card);
1452175859bfSDavid Dillow 	if (rc)
1453175859bfSDavid Dillow 		goto card_error_out;
1454175859bfSDavid Dillow 
1455175859bfSDavid Dillow 	pci_set_drvdata(pci, card);
1456175859bfSDavid Dillow 	return 0;
1457175859bfSDavid Dillow 
1458175859bfSDavid Dillow card_error_out:
1459175859bfSDavid Dillow 	snd_card_free(card);
1460175859bfSDavid Dillow 
1461175859bfSDavid Dillow error_out:
1462175859bfSDavid Dillow 	return rc;
1463175859bfSDavid Dillow }
1464175859bfSDavid Dillow 
1465e23e7a14SBill Pemberton static void snd_sis7019_remove(struct pci_dev *pci)
1466175859bfSDavid Dillow {
1467175859bfSDavid Dillow 	snd_card_free(pci_get_drvdata(pci));
1468175859bfSDavid Dillow }
1469175859bfSDavid Dillow 
1470175859bfSDavid Dillow static struct pci_driver sis7019_driver = {
14713733e424STakashi Iwai 	.name = KBUILD_MODNAME,
1472175859bfSDavid Dillow 	.id_table = snd_sis7019_ids,
1473175859bfSDavid Dillow 	.probe = snd_sis7019_probe,
1474e23e7a14SBill Pemberton 	.remove = snd_sis7019_remove,
147568cb2b55STakashi Iwai 	.driver = {
147668cb2b55STakashi Iwai 		.pm = SIS_PM_OPS,
147768cb2b55STakashi Iwai 	},
1478175859bfSDavid Dillow };
1479175859bfSDavid Dillow 
1480e9f66d9bSTakashi Iwai module_pci_driver(sis7019_driver);
1481