2 * Driver for SiS7019 Audio Accelerator
4 * Copyright (C) 2004-2007, David Dillow
5 * Written by David Dillow <dave@thedillows.org>
6 * Inspired by the Trident 4D-WaveDX/NX driver.
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.
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.
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
24 #include <linux/init.h>
25 #include <linux/pci.h>
26 #include <linux/time.h>
27 #include <linux/slab.h>
28 #include <linux/module.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>
36 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
37 MODULE_DESCRIPTION("SiS7019");
38 MODULE_LICENSE("GPL");
39 MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
41 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
42 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
43 static bool enable = 1;
44 static int codecs = 1;
46 module_param(index, int, 0444);
47 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
48 module_param(id, charp, 0444);
49 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
50 module_param(enable, bool, 0444);
51 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
52 module_param(codecs, int, 0444);
53 MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
55 static DEFINE_PCI_DEVICE_TABLE(snd_sis7019_ids) = {
56 { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
60 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
62 /* There are three timing modes for the voices.
64 * For both playback and capture, when the buffer is one or two periods long,
65 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
66 * to let us know when the periods have ended.
68 * When performing playback with more than two periods per buffer, we set
69 * the "Stop Sample Offset" and tell the hardware to interrupt us when we
70 * reach it. We then update the offset and continue on until we are
71 * interrupted for the next period.
73 * Capture channels do not have a SSO, so we allocate a playback channel to
74 * use as a timer for the capture periods. We use the SSO on the playback
75 * channel to clock out virtual periods, and adjust the virtual period length
76 * to maintain synchronization. This algorithm came from the Trident driver.
78 * FIXME: It'd be nice to make use of some of the synth features in the
79 * hardware, but a woeful lack of documentation is a significant roadblock.
83 #define VOICE_IN_USE 1
84 #define VOICE_CAPTURE 2
85 #define VOICE_SSO_TIMING 4
86 #define VOICE_SYNC_TIMING 8
94 struct snd_pcm_substream *substream;
96 void __iomem *ctrl_base;
97 void __iomem *wave_base;
98 void __iomem *sync_base;
102 /* We need four pages to store our wave parameters during a suspend. If
103 * we're not doing power management, we still need to allocate a page
104 * for the silence buffer.
106 #ifdef CONFIG_PM_SLEEP
107 #define SIS_SUSPEND_PAGES 4
109 #define SIS_SUSPEND_PAGES 1
113 unsigned long ioport;
114 void __iomem *ioaddr;
120 struct snd_card *card;
121 struct snd_ac97 *ac97[3];
123 /* Protect against more than one thread hitting the AC97
124 * registers (in a more polite manner than pounding the hardware
127 struct mutex ac97_mutex;
129 /* voice_lock protects allocation/freeing of the voice descriptions
131 spinlock_t voice_lock;
133 struct voice voices[64];
134 struct voice capture_voice;
136 /* Allocate pages to store the internal wave state during
137 * suspends. When we're operating, this can be used as a silence
138 * buffer for a timing channel.
140 void *suspend_state[SIS_SUSPEND_PAGES];
143 dma_addr_t silence_dma_addr;
146 /* These values are also used by the module param 'codecs' to indicate
147 * which codecs should be present.
149 #define SIS_PRIMARY_CODEC_PRESENT 0x0001
150 #define SIS_SECONDARY_CODEC_PRESENT 0x0002
151 #define SIS_TERTIARY_CODEC_PRESENT 0x0004
153 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
154 * documented range of 8-0xfff8 samples. Given that they are 0-based,
155 * that places our period/buffer range at 9-0xfff9 samples. That makes the
156 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
157 * max samples / min samples gives us the max periods in a buffer.
159 * We'll add a constraint upon open that limits the period and buffer sample
160 * size to values that are legal for the hardware.
162 static struct snd_pcm_hardware sis_playback_hw_info = {
163 .info = (SNDRV_PCM_INFO_MMAP |
164 SNDRV_PCM_INFO_MMAP_VALID |
165 SNDRV_PCM_INFO_INTERLEAVED |
166 SNDRV_PCM_INFO_BLOCK_TRANSFER |
167 SNDRV_PCM_INFO_SYNC_START |
168 SNDRV_PCM_INFO_RESUME),
169 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
170 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
171 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
176 .buffer_bytes_max = (0xfff9 * 4),
177 .period_bytes_min = 9,
178 .period_bytes_max = (0xfff9 * 4),
180 .periods_max = (0xfff9 / 9),
183 static struct snd_pcm_hardware sis_capture_hw_info = {
184 .info = (SNDRV_PCM_INFO_MMAP |
185 SNDRV_PCM_INFO_MMAP_VALID |
186 SNDRV_PCM_INFO_INTERLEAVED |
187 SNDRV_PCM_INFO_BLOCK_TRANSFER |
188 SNDRV_PCM_INFO_SYNC_START |
189 SNDRV_PCM_INFO_RESUME),
190 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
191 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
192 .rates = SNDRV_PCM_RATE_48000,
197 .buffer_bytes_max = (0xfff9 * 4),
198 .period_bytes_min = 9,
199 .period_bytes_max = (0xfff9 * 4),
201 .periods_max = (0xfff9 / 9),
204 static void sis_update_sso(struct voice *voice, u16 period)
206 void __iomem *base = voice->ctrl_base;
208 voice->sso += period;
209 if (voice->sso >= voice->buffer_size)
210 voice->sso -= voice->buffer_size;
212 /* Enforce the documented hardware minimum offset */
216 /* The SSO is in the upper 16 bits of the register. */
217 writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
220 static void sis_update_voice(struct voice *voice)
222 if (voice->flags & VOICE_SSO_TIMING) {
223 sis_update_sso(voice, voice->period_size);
224 } else if (voice->flags & VOICE_SYNC_TIMING) {
227 /* If we've not hit the end of the virtual period, update
228 * our records and keep going.
230 if (voice->vperiod > voice->period_size) {
231 voice->vperiod -= voice->period_size;
232 if (voice->vperiod < voice->period_size)
233 sis_update_sso(voice, voice->vperiod);
235 sis_update_sso(voice, voice->period_size);
239 /* Calculate our relative offset between the target and
240 * the actual CSO value. Since we're operating in a loop,
241 * if the value is more than half way around, we can
242 * consider ourselves wrapped.
244 sync = voice->sync_cso;
245 sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
246 if (sync > (voice->sync_buffer_size / 2))
247 sync -= voice->sync_buffer_size;
249 /* If sync is positive, then we interrupted too early, and
250 * we'll need to come back in a few samples and try again.
251 * There's a minimum wait, as it takes some time for the DMA
252 * engine to startup, etc...
257 sis_update_sso(voice, sync);
261 /* Ok, we interrupted right on time, or (hopefully) just
262 * a bit late. We'll adjst our next waiting period based
263 * on how close we got.
265 * We need to stay just behind the actual channel to ensure
266 * it really is past a period when we get our interrupt --
267 * otherwise we'll fall into the early code above and have
268 * a minimum wait time, which makes us quite late here,
269 * eating into the user's time to refresh the buffer, esp.
270 * if using small periods.
272 * If we're less than 9 samples behind, we're on target.
273 * Otherwise, shorten the next vperiod by the amount we've
277 voice->vperiod = voice->sync_period_size + 1;
279 voice->vperiod = voice->sync_period_size + sync + 10;
281 if (voice->vperiod < voice->buffer_size) {
282 sis_update_sso(voice, voice->vperiod);
285 sis_update_sso(voice, voice->period_size);
287 sync = voice->sync_cso + voice->sync_period_size;
288 if (sync >= voice->sync_buffer_size)
289 sync -= voice->sync_buffer_size;
290 voice->sync_cso = sync;
293 snd_pcm_period_elapsed(voice->substream);
296 static void sis_voice_irq(u32 status, struct voice *voice)
304 sis_update_voice(voice);
309 static irqreturn_t sis_interrupt(int irq, void *dev)
311 struct sis7019 *sis = dev;
312 unsigned long io = sis->ioport;
316 /* We only use the DMA interrupts, and we don't enable any other
317 * source of interrupts. But, it is possible to see an interrupt
318 * status that didn't actually interrupt us, so eliminate anything
319 * we're not expecting to avoid falsely claiming an IRQ, and an
320 * ensuing endless loop.
322 intr = inl(io + SIS_GISR);
323 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
324 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
329 status = inl(io + SIS_PISR_A);
331 sis_voice_irq(status, sis->voices);
332 outl(status, io + SIS_PISR_A);
335 status = inl(io + SIS_PISR_B);
337 sis_voice_irq(status, &sis->voices[32]);
338 outl(status, io + SIS_PISR_B);
341 status = inl(io + SIS_RISR);
343 voice = &sis->capture_voice;
345 snd_pcm_period_elapsed(voice->substream);
347 outl(status, io + SIS_RISR);
350 outl(intr, io + SIS_GISR);
351 intr = inl(io + SIS_GISR);
352 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
353 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
359 static u32 sis_rate_to_delta(unsigned int rate)
363 /* This was copied from the trident driver, but it seems its gotten
364 * around a bit... nevertheless, it works well.
366 * We special case 44100 and 8000 since rounding with the equation
367 * does not give us an accurate enough value. For 11025 and 22050
368 * the equation gives us the best answer. All other frequencies will
369 * also use the equation. JDW
373 else if (rate == 8000)
375 else if (rate == 48000)
378 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
382 static void __sis_map_silence(struct sis7019 *sis)
384 /* Helper function: must hold sis->voice_lock on entry */
385 if (!sis->silence_users)
386 sis->silence_dma_addr = pci_map_single(sis->pci,
387 sis->suspend_state[0],
388 4096, PCI_DMA_TODEVICE);
389 sis->silence_users++;
392 static void __sis_unmap_silence(struct sis7019 *sis)
394 /* Helper function: must hold sis->voice_lock on entry */
395 sis->silence_users--;
396 if (!sis->silence_users)
397 pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096,
401 static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
405 spin_lock_irqsave(&sis->voice_lock, flags);
407 __sis_unmap_silence(sis);
408 voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
410 voice->timing = NULL;
412 voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
413 spin_unlock_irqrestore(&sis->voice_lock, flags);
416 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
418 /* Must hold the voice_lock on entry */
422 for (i = 0; i < 64; i++) {
423 voice = &sis->voices[i];
424 if (voice->flags & VOICE_IN_USE)
426 voice->flags |= VOICE_IN_USE;
435 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
440 spin_lock_irqsave(&sis->voice_lock, flags);
441 voice = __sis_alloc_playback_voice(sis);
442 spin_unlock_irqrestore(&sis->voice_lock, flags);
447 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *hw_params)
450 struct sis7019 *sis = snd_pcm_substream_chip(substream);
451 struct snd_pcm_runtime *runtime = substream->runtime;
452 struct voice *voice = runtime->private_data;
453 unsigned int period_size, buffer_size;
457 /* If there are one or two periods per buffer, we don't need a
458 * timing voice, as we can use the capture channel's interrupts
459 * to clock out the periods.
461 period_size = params_period_size(hw_params);
462 buffer_size = params_buffer_size(hw_params);
463 needed = (period_size != buffer_size &&
464 period_size != (buffer_size / 2));
466 if (needed && !voice->timing) {
467 spin_lock_irqsave(&sis->voice_lock, flags);
468 voice->timing = __sis_alloc_playback_voice(sis);
470 __sis_map_silence(sis);
471 spin_unlock_irqrestore(&sis->voice_lock, flags);
474 voice->timing->substream = substream;
475 } else if (!needed && voice->timing) {
476 sis_free_voice(sis, voice);
477 voice->timing = NULL;
483 static int sis_playback_open(struct snd_pcm_substream *substream)
485 struct sis7019 *sis = snd_pcm_substream_chip(substream);
486 struct snd_pcm_runtime *runtime = substream->runtime;
489 voice = sis_alloc_playback_voice(sis);
493 voice->substream = substream;
494 runtime->private_data = voice;
495 runtime->hw = sis_playback_hw_info;
496 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
498 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
500 snd_pcm_set_sync(substream);
504 static int sis_substream_close(struct snd_pcm_substream *substream)
506 struct sis7019 *sis = snd_pcm_substream_chip(substream);
507 struct snd_pcm_runtime *runtime = substream->runtime;
508 struct voice *voice = runtime->private_data;
510 sis_free_voice(sis, voice);
514 static int sis_playback_hw_params(struct snd_pcm_substream *substream,
515 struct snd_pcm_hw_params *hw_params)
517 return snd_pcm_lib_malloc_pages(substream,
518 params_buffer_bytes(hw_params));
521 static int sis_hw_free(struct snd_pcm_substream *substream)
523 return snd_pcm_lib_free_pages(substream);
526 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
528 struct snd_pcm_runtime *runtime = substream->runtime;
529 struct voice *voice = runtime->private_data;
530 void __iomem *ctrl_base = voice->ctrl_base;
531 void __iomem *wave_base = voice->wave_base;
532 u32 format, dma_addr, control, sso_eso, delta, reg;
535 /* We rely on the PCM core to ensure that the parameters for this
536 * substream do not change on us while we're programming the HW.
539 if (snd_pcm_format_width(runtime->format) == 8)
540 format |= SIS_PLAY_DMA_FORMAT_8BIT;
541 if (!snd_pcm_format_signed(runtime->format))
542 format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
543 if (runtime->channels == 1)
544 format |= SIS_PLAY_DMA_FORMAT_MONO;
546 /* The baseline setup is for a single period per buffer, and
547 * we add bells and whistles as needed from there.
549 dma_addr = runtime->dma_addr;
550 leo = runtime->buffer_size - 1;
551 control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
554 if (runtime->period_size == (runtime->buffer_size / 2)) {
555 control |= SIS_PLAY_DMA_INTR_AT_MLP;
556 } else if (runtime->period_size != runtime->buffer_size) {
557 voice->flags |= VOICE_SSO_TIMING;
558 voice->sso = runtime->period_size - 1;
559 voice->period_size = runtime->period_size;
560 voice->buffer_size = runtime->buffer_size;
562 control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
563 control |= SIS_PLAY_DMA_INTR_AT_SSO;
564 sso_eso |= (runtime->period_size - 1) << 16;
567 delta = sis_rate_to_delta(runtime->rate);
569 /* Ok, we're ready to go, set up the channel.
571 writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
572 writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
573 writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
574 writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
576 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
577 writel(0, wave_base + reg);
579 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
580 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
581 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
582 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
583 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
584 wave_base + SIS_WAVE_CHANNEL_CONTROL);
586 /* Force PCI writes to post. */
592 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
594 struct sis7019 *sis = snd_pcm_substream_chip(substream);
595 unsigned long io = sis->ioport;
596 struct snd_pcm_substream *s;
601 u32 play[2] = { 0, 0 };
603 /* No locks needed, as the PCM core will hold the locks on the
604 * substreams, and the HW will only start/stop the indicated voices
605 * without changing the state of the others.
608 case SNDRV_PCM_TRIGGER_START:
609 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
610 case SNDRV_PCM_TRIGGER_RESUME:
613 case SNDRV_PCM_TRIGGER_STOP:
614 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
615 case SNDRV_PCM_TRIGGER_SUSPEND:
622 snd_pcm_group_for_each_entry(s, substream) {
623 /* Make sure it is for us... */
624 chip = snd_pcm_substream_chip(s);
628 voice = s->runtime->private_data;
629 if (voice->flags & VOICE_CAPTURE) {
630 record |= 1 << voice->num;
631 voice = voice->timing;
634 /* voice could be NULL if this a recording stream, and it
635 * doesn't have an external timing channel.
638 play[voice->num / 32] |= 1 << (voice->num & 0x1f);
640 snd_pcm_trigger_done(s, substream);
645 outl(record, io + SIS_RECORD_START_REG);
647 outl(play[0], io + SIS_PLAY_START_A_REG);
649 outl(play[1], io + SIS_PLAY_START_B_REG);
652 outl(record, io + SIS_RECORD_STOP_REG);
654 outl(play[0], io + SIS_PLAY_STOP_A_REG);
656 outl(play[1], io + SIS_PLAY_STOP_B_REG);
661 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
663 struct snd_pcm_runtime *runtime = substream->runtime;
664 struct voice *voice = runtime->private_data;
667 cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
672 static int sis_capture_open(struct snd_pcm_substream *substream)
674 struct sis7019 *sis = snd_pcm_substream_chip(substream);
675 struct snd_pcm_runtime *runtime = substream->runtime;
676 struct voice *voice = &sis->capture_voice;
679 /* FIXME: The driver only supports recording from one channel
680 * at the moment, but it could support more.
682 spin_lock_irqsave(&sis->voice_lock, flags);
683 if (voice->flags & VOICE_IN_USE)
686 voice->flags |= VOICE_IN_USE;
687 spin_unlock_irqrestore(&sis->voice_lock, flags);
692 voice->substream = substream;
693 runtime->private_data = voice;
694 runtime->hw = sis_capture_hw_info;
695 runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
696 snd_pcm_limit_hw_rates(runtime);
697 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
699 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
701 snd_pcm_set_sync(substream);
705 static int sis_capture_hw_params(struct snd_pcm_substream *substream,
706 struct snd_pcm_hw_params *hw_params)
708 struct sis7019 *sis = snd_pcm_substream_chip(substream);
711 rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
712 params_rate(hw_params));
716 rc = snd_pcm_lib_malloc_pages(substream,
717 params_buffer_bytes(hw_params));
721 rc = sis_alloc_timing_voice(substream, hw_params);
727 static void sis_prepare_timing_voice(struct voice *voice,
728 struct snd_pcm_substream *substream)
730 struct sis7019 *sis = snd_pcm_substream_chip(substream);
731 struct snd_pcm_runtime *runtime = substream->runtime;
732 struct voice *timing = voice->timing;
733 void __iomem *play_base = timing->ctrl_base;
734 void __iomem *wave_base = timing->wave_base;
735 u16 buffer_size, period_size;
736 u32 format, control, sso_eso, delta;
737 u32 vperiod, sso, reg;
739 /* Set our initial buffer and period as large as we can given a
740 * single page of silence.
742 buffer_size = 4096 / runtime->channels;
743 buffer_size /= snd_pcm_format_size(runtime->format, 1);
744 period_size = buffer_size;
746 /* Initially, we want to interrupt just a bit behind the end of
747 * the period we're clocking out. 12 samples seems to give a good
750 * We want to spread our interrupts throughout the virtual period,
751 * so that we don't end up with two interrupts back to back at the
752 * end -- this helps minimize the effects of any jitter. Adjust our
753 * clocking period size so that the last period is at least a fourth
756 * This is all moot if we don't need to use virtual periods.
758 vperiod = runtime->period_size + 12;
759 if (vperiod > period_size) {
760 u16 tail = vperiod % period_size;
761 u16 quarter_period = period_size / 4;
763 if (tail && tail < quarter_period) {
764 u16 loops = vperiod / period_size;
766 tail = quarter_period - tail;
772 sso = period_size - 1;
774 /* The initial period will fit inside the buffer, so we
775 * don't need to use virtual periods -- disable them.
777 period_size = runtime->period_size;
782 /* The interrupt handler implements the timing synchronization, so
785 timing->flags |= VOICE_SYNC_TIMING;
786 timing->sync_base = voice->ctrl_base;
787 timing->sync_cso = runtime->period_size;
788 timing->sync_period_size = runtime->period_size;
789 timing->sync_buffer_size = runtime->buffer_size;
790 timing->period_size = period_size;
791 timing->buffer_size = buffer_size;
793 timing->vperiod = vperiod;
795 /* Using unsigned samples with the all-zero silence buffer
796 * forces the output to the lower rail, killing playback.
797 * So ignore unsigned vs signed -- it doesn't change the timing.
800 if (snd_pcm_format_width(runtime->format) == 8)
801 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
802 if (runtime->channels == 1)
803 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
805 control = timing->buffer_size - 1;
806 control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
807 sso_eso = timing->buffer_size - 1;
808 sso_eso |= timing->sso << 16;
810 delta = sis_rate_to_delta(runtime->rate);
812 /* We've done the math, now configure the channel.
814 writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
815 writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
816 writel(control, play_base + SIS_PLAY_DMA_CONTROL);
817 writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
819 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
820 writel(0, wave_base + reg);
822 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
823 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
824 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
825 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
826 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
827 wave_base + SIS_WAVE_CHANNEL_CONTROL);
830 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
832 struct snd_pcm_runtime *runtime = substream->runtime;
833 struct voice *voice = runtime->private_data;
834 void __iomem *rec_base = voice->ctrl_base;
835 u32 format, dma_addr, control;
838 /* We rely on the PCM core to ensure that the parameters for this
839 * substream do not change on us while we're programming the HW.
842 if (snd_pcm_format_width(runtime->format) == 8)
843 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
844 if (!snd_pcm_format_signed(runtime->format))
845 format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
846 if (runtime->channels == 1)
847 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
849 dma_addr = runtime->dma_addr;
850 leo = runtime->buffer_size - 1;
851 control = leo | SIS_CAPTURE_DMA_LOOP;
853 /* If we've got more than two periods per buffer, then we have
854 * use a timing voice to clock out the periods. Otherwise, we can
855 * use the capture channel's interrupts.
858 sis_prepare_timing_voice(voice, substream);
860 control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
861 if (runtime->period_size != runtime->buffer_size)
862 control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
865 writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
866 writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
867 writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
869 /* Force the writes to post. */
875 static struct snd_pcm_ops sis_playback_ops = {
876 .open = sis_playback_open,
877 .close = sis_substream_close,
878 .ioctl = snd_pcm_lib_ioctl,
879 .hw_params = sis_playback_hw_params,
880 .hw_free = sis_hw_free,
881 .prepare = sis_pcm_playback_prepare,
882 .trigger = sis_pcm_trigger,
883 .pointer = sis_pcm_pointer,
886 static struct snd_pcm_ops sis_capture_ops = {
887 .open = sis_capture_open,
888 .close = sis_substream_close,
889 .ioctl = snd_pcm_lib_ioctl,
890 .hw_params = sis_capture_hw_params,
891 .hw_free = sis_hw_free,
892 .prepare = sis_pcm_capture_prepare,
893 .trigger = sis_pcm_trigger,
894 .pointer = sis_pcm_pointer,
897 static int sis_pcm_create(struct sis7019 *sis)
902 /* We have 64 voices, and the driver currently records from
903 * only one channel, though that could change in the future.
905 rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
909 pcm->private_data = sis;
910 strcpy(pcm->name, "SiS7019");
913 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
914 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
916 /* Try to preallocate some memory, but it's not the end of the
917 * world if this fails.
919 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
920 snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
925 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
927 unsigned long io = sis->ioport;
928 unsigned short val = 0xffff;
932 static const u16 codec_ready[3] = {
933 SIS_AC97_STATUS_CODEC_READY,
934 SIS_AC97_STATUS_CODEC2_READY,
935 SIS_AC97_STATUS_CODEC3_READY,
938 rdy = codec_ready[codec];
941 /* Get the AC97 semaphore -- software first, so we don't spin
942 * pounding out IO reads on the hardware semaphore...
944 mutex_lock(&sis->ac97_mutex);
947 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
953 /* ... and wait for any outstanding commands to complete ...
957 status = inw(io + SIS_AC97_STATUS);
958 if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
967 /* ... before sending our command and waiting for it to finish ...
969 outl(cmd, io + SIS_AC97_CMD);
973 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
976 /* ... and reading the results (if any).
978 val = inl(io + SIS_AC97_CMD) >> 16;
981 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
983 mutex_unlock(&sis->ac97_mutex);
986 dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
993 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
996 static const u32 cmd[3] = {
997 SIS_AC97_CMD_CODEC_WRITE,
998 SIS_AC97_CMD_CODEC2_WRITE,
999 SIS_AC97_CMD_CODEC3_WRITE,
1001 sis_ac97_rw(ac97->private_data, ac97->num,
1002 (val << 16) | (reg << 8) | cmd[ac97->num]);
1005 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
1007 static const u32 cmd[3] = {
1008 SIS_AC97_CMD_CODEC_READ,
1009 SIS_AC97_CMD_CODEC2_READ,
1010 SIS_AC97_CMD_CODEC3_READ,
1012 return sis_ac97_rw(ac97->private_data, ac97->num,
1013 (reg << 8) | cmd[ac97->num]);
1016 static int sis_mixer_create(struct sis7019 *sis)
1018 struct snd_ac97_bus *bus;
1019 struct snd_ac97_template ac97;
1020 static struct snd_ac97_bus_ops ops = {
1021 .write = sis_ac97_write,
1022 .read = sis_ac97_read,
1026 memset(&ac97, 0, sizeof(ac97));
1027 ac97.private_data = sis;
1029 rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1030 if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1031 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1033 if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1034 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1036 if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1037 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1039 /* If we return an error here, then snd_card_free() should
1040 * free up any ac97 codecs that got created, as well as the bus.
1045 static void sis_free_suspend(struct sis7019 *sis)
1049 for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1050 kfree(sis->suspend_state[i]);
1053 static int sis_chip_free(struct sis7019 *sis)
1055 /* Reset the chip, and disable all interrputs.
1057 outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1059 outl(0, sis->ioport + SIS_GCR);
1060 outl(0, sis->ioport + SIS_GIER);
1062 /* Now, free everything we allocated.
1065 free_irq(sis->irq, sis);
1068 iounmap(sis->ioaddr);
1070 pci_release_regions(sis->pci);
1071 pci_disable_device(sis->pci);
1073 sis_free_suspend(sis);
1077 static int sis_dev_free(struct snd_device *dev)
1079 struct sis7019 *sis = dev->device_data;
1080 return sis_chip_free(sis);
1083 static int sis_chip_init(struct sis7019 *sis)
1085 unsigned long io = sis->ioport;
1086 void __iomem *ioaddr = sis->ioaddr;
1087 unsigned long timeout;
1092 /* Reset the audio controller
1094 outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1096 outl(0, io + SIS_GCR);
1098 /* Get the AC-link semaphore, and reset the codecs
1101 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1107 outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1111 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1114 /* Command complete, we can let go of the semaphore now.
1116 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1120 /* Now that we've finished the reset, find out what's attached.
1121 * There are some codec/board combinations that take an extremely
1122 * long time to come up. 350+ ms has been observed in the field,
1123 * so we'll give them up to 500ms.
1125 sis->codecs_present = 0;
1126 timeout = msecs_to_jiffies(500) + jiffies;
1127 while (time_before_eq(jiffies, timeout)) {
1128 status = inl(io + SIS_AC97_STATUS);
1129 if (status & SIS_AC97_STATUS_CODEC_READY)
1130 sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1131 if (status & SIS_AC97_STATUS_CODEC2_READY)
1132 sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1133 if (status & SIS_AC97_STATUS_CODEC3_READY)
1134 sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1136 if (sis->codecs_present == codecs)
1142 /* All done, check for errors.
1144 if (!sis->codecs_present) {
1145 dev_err(&sis->pci->dev, "could not find any codecs\n");
1149 if (sis->codecs_present != codecs) {
1150 dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1151 sis->codecs_present, codecs);
1154 /* Let the hardware know that the audio driver is alive,
1155 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1156 * record channels. We're going to want to use Variable Rate Audio
1157 * for recording, to avoid needlessly resampling from 48kHZ.
1159 outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1160 outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1161 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1162 SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1163 SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1165 /* All AC97 PCM slots should be sourced from sub-mixer 0.
1167 outl(0, io + SIS_AC97_PSR);
1169 /* There is only one valid DMA setup for a PCI environment.
1171 outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1173 /* Reset the synchronization groups for all of the channels
1174 * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc.
1175 * we'll need to change how we handle these. Until then, we just
1176 * assign sub-mixer 0 to all playback channels, and avoid any
1177 * attenuation on the audio.
1179 outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1180 outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1181 outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1182 outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1183 outl(0, io + SIS_MIXER_SYNC_GROUP);
1185 for (i = 0; i < 64; i++) {
1186 writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1187 writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1188 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1191 /* Don't attenuate any audio set for the wave amplifier.
1193 * FIXME: Maximum attenuation is set for the music amp, which will
1194 * need to change if we start using the synth engine.
1196 outl(0xffff0000, io + SIS_WEVCR);
1198 /* Ensure that the wave engine is in normal operating mode.
1200 outl(0, io + SIS_WECCR);
1202 /* Go ahead and enable the DMA interrupts. They won't go live
1203 * until we start a channel.
1205 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1206 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1211 #ifdef CONFIG_PM_SLEEP
1212 static int sis_suspend(struct device *dev)
1214 struct pci_dev *pci = to_pci_dev(dev);
1215 struct snd_card *card = dev_get_drvdata(dev);
1216 struct sis7019 *sis = card->private_data;
1217 void __iomem *ioaddr = sis->ioaddr;
1220 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1221 snd_pcm_suspend_all(sis->pcm);
1222 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1223 snd_ac97_suspend(sis->ac97[0]);
1224 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1225 snd_ac97_suspend(sis->ac97[1]);
1226 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1227 snd_ac97_suspend(sis->ac97[2]);
1229 /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1231 if (sis->irq >= 0) {
1232 free_irq(sis->irq, sis);
1236 /* Save the internal state away
1238 for (i = 0; i < 4; i++) {
1239 memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1243 pci_disable_device(pci);
1244 pci_save_state(pci);
1245 pci_set_power_state(pci, PCI_D3hot);
1249 static int sis_resume(struct device *dev)
1251 struct pci_dev *pci = to_pci_dev(dev);
1252 struct snd_card *card = dev_get_drvdata(dev);
1253 struct sis7019 *sis = card->private_data;
1254 void __iomem *ioaddr = sis->ioaddr;
1257 pci_set_power_state(pci, PCI_D0);
1258 pci_restore_state(pci);
1260 if (pci_enable_device(pci) < 0) {
1261 dev_err(&pci->dev, "unable to re-enable device\n");
1265 if (sis_chip_init(sis)) {
1266 dev_err(&pci->dev, "unable to re-init controller\n");
1270 if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1271 KBUILD_MODNAME, sis)) {
1272 dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1276 /* Restore saved state, then clear out the page we use for the
1279 for (i = 0; i < 4; i++) {
1280 memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1284 memset(sis->suspend_state[0], 0, 4096);
1286 sis->irq = pci->irq;
1287 pci_set_master(pci);
1289 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1290 snd_ac97_resume(sis->ac97[0]);
1291 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1292 snd_ac97_resume(sis->ac97[1]);
1293 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1294 snd_ac97_resume(sis->ac97[2]);
1296 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1300 snd_card_disconnect(card);
1304 static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
1305 #define SIS_PM_OPS &sis_pm
1307 #define SIS_PM_OPS NULL
1308 #endif /* CONFIG_PM_SLEEP */
1310 static int sis_alloc_suspend(struct sis7019 *sis)
1314 /* We need 16K to store the internal wave engine state during a
1315 * suspend, but we don't need it to be contiguous, so play nice
1316 * with the memory system. We'll also use this area for a silence
1319 for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1320 sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1321 if (!sis->suspend_state[i])
1324 memset(sis->suspend_state[0], 0, 4096);
1329 static int sis_chip_create(struct snd_card *card,
1330 struct pci_dev *pci)
1332 struct sis7019 *sis = card->private_data;
1333 struct voice *voice;
1334 static struct snd_device_ops ops = {
1335 .dev_free = sis_dev_free,
1340 rc = pci_enable_device(pci);
1344 if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0) {
1345 dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1346 goto error_out_enabled;
1349 memset(sis, 0, sizeof(*sis));
1350 mutex_init(&sis->ac97_mutex);
1351 spin_lock_init(&sis->voice_lock);
1355 sis->ioport = pci_resource_start(pci, 0);
1357 rc = pci_request_regions(pci, "SiS7019");
1359 dev_err(&pci->dev, "unable request regions\n");
1360 goto error_out_enabled;
1364 sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1366 dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1367 goto error_out_cleanup;
1370 rc = sis_alloc_suspend(sis);
1372 dev_err(&pci->dev, "unable to allocate state storage\n");
1373 goto error_out_cleanup;
1376 rc = sis_chip_init(sis);
1378 goto error_out_cleanup;
1380 rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1383 dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1384 goto error_out_cleanup;
1387 sis->irq = pci->irq;
1388 pci_set_master(pci);
1390 for (i = 0; i < 64; i++) {
1391 voice = &sis->voices[i];
1393 voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1394 voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1397 voice = &sis->capture_voice;
1398 voice->flags = VOICE_CAPTURE;
1399 voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1400 voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1402 rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1404 goto error_out_cleanup;
1406 snd_card_set_dev(card, &pci->dev);
1414 pci_disable_device(pci);
1420 static int snd_sis7019_probe(struct pci_dev *pci,
1421 const struct pci_device_id *pci_id)
1423 struct snd_card *card;
1424 struct sis7019 *sis;
1431 /* The user can specify which codecs should be present so that we
1432 * can wait for them to show up if they are slow to recover from
1433 * the AC97 cold reset. We default to a single codec, the primary.
1435 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1437 codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1438 SIS_TERTIARY_CODEC_PRESENT;
1440 codecs = SIS_PRIMARY_CODEC_PRESENT;
1442 rc = snd_card_create(index, id, THIS_MODULE, sizeof(*sis), &card);
1446 strcpy(card->driver, "SiS7019");
1447 strcpy(card->shortname, "SiS7019");
1448 rc = sis_chip_create(card, pci);
1450 goto card_error_out;
1452 sis = card->private_data;
1454 rc = sis_mixer_create(sis);
1456 goto card_error_out;
1458 rc = sis_pcm_create(sis);
1460 goto card_error_out;
1462 snprintf(card->longname, sizeof(card->longname),
1463 "%s Audio Accelerator with %s at 0x%lx, irq %d",
1464 card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1465 sis->ioport, sis->irq);
1467 rc = snd_card_register(card);
1469 goto card_error_out;
1471 pci_set_drvdata(pci, card);
1475 snd_card_free(card);
1481 static void snd_sis7019_remove(struct pci_dev *pci)
1483 snd_card_free(pci_get_drvdata(pci));
1484 pci_set_drvdata(pci, NULL);
1487 static struct pci_driver sis7019_driver = {
1488 .name = KBUILD_MODNAME,
1489 .id_table = snd_sis7019_ids,
1490 .probe = snd_sis7019_probe,
1491 .remove = snd_sis7019_remove,
1497 module_pci_driver(sis7019_driver);