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/moduleparam.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <sound/core.h>
31 #include <sound/ac97_codec.h>
32 #include <sound/initval.h>
35 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
36 MODULE_DESCRIPTION("SiS7019");
37 MODULE_LICENSE("GPL");
38 MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
40 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
41 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
42 static int enable = 1;
44 module_param(index, int, 0444);
45 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
46 module_param(id, charp, 0444);
47 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
48 module_param(enable, bool, 0444);
49 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
51 static struct pci_device_id snd_sis7019_ids[] = {
52 { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
56 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
58 /* There are three timing modes for the voices.
60 * For both playback and capture, when the buffer is one or two periods long,
61 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
62 * to let us know when the periods have ended.
64 * When performing playback with more than two periods per buffer, we set
65 * the "Stop Sample Offset" and tell the hardware to interrupt us when we
66 * reach it. We then update the offset and continue on until we are
67 * interrupted for the next period.
69 * Capture channels do not have a SSO, so we allocate a playback channel to
70 * use as a timer for the capture periods. We use the SSO on the playback
71 * channel to clock out virtual periods, and adjust the virtual period length
72 * to maintain synchronization. This algorithm came from the Trident driver.
74 * FIXME: It'd be nice to make use of some of the synth features in the
75 * hardware, but a woeful lack of documentation is a significant roadblock.
79 #define VOICE_IN_USE 1
80 #define VOICE_CAPTURE 2
81 #define VOICE_SSO_TIMING 4
82 #define VOICE_SYNC_TIMING 8
90 struct snd_pcm_substream *substream;
92 void __iomem *ctrl_base;
93 void __iomem *wave_base;
94 void __iomem *sync_base;
98 /* We need four pages to store our wave parameters during a suspend. If
99 * we're not doing power management, we still need to allocate a page
100 * for the silence buffer.
103 #define SIS_SUSPEND_PAGES 4
105 #define SIS_SUSPEND_PAGES 1
109 unsigned long ioport;
110 void __iomem *ioaddr;
116 struct snd_card *card;
117 struct snd_ac97 *ac97[3];
119 /* Protect against more than one thread hitting the AC97
120 * registers (in a more polite manner than pounding the hardware
123 struct mutex ac97_mutex;
125 /* voice_lock protects allocation/freeing of the voice descriptions
127 spinlock_t voice_lock;
129 struct voice voices[64];
130 struct voice capture_voice;
132 /* Allocate pages to store the internal wave state during
133 * suspends. When we're operating, this can be used as a silence
134 * buffer for a timing channel.
136 void *suspend_state[SIS_SUSPEND_PAGES];
139 dma_addr_t silence_dma_addr;
142 #define SIS_PRIMARY_CODEC_PRESENT 0x0001
143 #define SIS_SECONDARY_CODEC_PRESENT 0x0002
144 #define SIS_TERTIARY_CODEC_PRESENT 0x0004
146 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
147 * documented range of 8-0xfff8 samples. Given that they are 0-based,
148 * that places our period/buffer range at 9-0xfff9 samples. That makes the
149 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
150 * max samples / min samples gives us the max periods in a buffer.
152 * We'll add a constraint upon open that limits the period and buffer sample
153 * size to values that are legal for the hardware.
155 static struct snd_pcm_hardware sis_playback_hw_info = {
156 .info = (SNDRV_PCM_INFO_MMAP |
157 SNDRV_PCM_INFO_MMAP_VALID |
158 SNDRV_PCM_INFO_INTERLEAVED |
159 SNDRV_PCM_INFO_BLOCK_TRANSFER |
160 SNDRV_PCM_INFO_SYNC_START |
161 SNDRV_PCM_INFO_RESUME),
162 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
163 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
164 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
169 .buffer_bytes_max = (0xfff9 * 4),
170 .period_bytes_min = 9,
171 .period_bytes_max = (0xfff9 * 4),
173 .periods_max = (0xfff9 / 9),
176 static struct snd_pcm_hardware sis_capture_hw_info = {
177 .info = (SNDRV_PCM_INFO_MMAP |
178 SNDRV_PCM_INFO_MMAP_VALID |
179 SNDRV_PCM_INFO_INTERLEAVED |
180 SNDRV_PCM_INFO_BLOCK_TRANSFER |
181 SNDRV_PCM_INFO_SYNC_START |
182 SNDRV_PCM_INFO_RESUME),
183 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
184 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
185 .rates = SNDRV_PCM_RATE_48000,
190 .buffer_bytes_max = (0xfff9 * 4),
191 .period_bytes_min = 9,
192 .period_bytes_max = (0xfff9 * 4),
194 .periods_max = (0xfff9 / 9),
197 static void sis_update_sso(struct voice *voice, u16 period)
199 void __iomem *base = voice->ctrl_base;
201 voice->sso += period;
202 if (voice->sso >= voice->buffer_size)
203 voice->sso -= voice->buffer_size;
205 /* Enforce the documented hardware minimum offset */
209 /* The SSO is in the upper 16 bits of the register. */
210 writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
213 static void sis_update_voice(struct voice *voice)
215 if (voice->flags & VOICE_SSO_TIMING) {
216 sis_update_sso(voice, voice->period_size);
217 } else if (voice->flags & VOICE_SYNC_TIMING) {
220 /* If we've not hit the end of the virtual period, update
221 * our records and keep going.
223 if (voice->vperiod > voice->period_size) {
224 voice->vperiod -= voice->period_size;
225 if (voice->vperiod < voice->period_size)
226 sis_update_sso(voice, voice->vperiod);
228 sis_update_sso(voice, voice->period_size);
232 /* Calculate our relative offset between the target and
233 * the actual CSO value. Since we're operating in a loop,
234 * if the value is more than half way around, we can
235 * consider ourselves wrapped.
237 sync = voice->sync_cso;
238 sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
239 if (sync > (voice->sync_buffer_size / 2))
240 sync -= voice->sync_buffer_size;
242 /* If sync is positive, then we interrupted too early, and
243 * we'll need to come back in a few samples and try again.
244 * There's a minimum wait, as it takes some time for the DMA
245 * engine to startup, etc...
250 sis_update_sso(voice, sync);
254 /* Ok, we interrupted right on time, or (hopefully) just
255 * a bit late. We'll adjst our next waiting period based
256 * on how close we got.
258 * We need to stay just behind the actual channel to ensure
259 * it really is past a period when we get our interrupt --
260 * otherwise we'll fall into the early code above and have
261 * a minimum wait time, which makes us quite late here,
262 * eating into the user's time to refresh the buffer, esp.
263 * if using small periods.
265 * If we're less than 9 samples behind, we're on target.
268 voice->vperiod = voice->sync_period_size + 1;
270 voice->vperiod = voice->sync_period_size - 4;
272 if (voice->vperiod < voice->buffer_size) {
273 sis_update_sso(voice, voice->vperiod);
276 sis_update_sso(voice, voice->period_size);
278 sync = voice->sync_cso + voice->sync_period_size;
279 if (sync >= voice->sync_buffer_size)
280 sync -= voice->sync_buffer_size;
281 voice->sync_cso = sync;
284 snd_pcm_period_elapsed(voice->substream);
287 static void sis_voice_irq(u32 status, struct voice *voice)
295 sis_update_voice(voice);
300 static irqreturn_t sis_interrupt(int irq, void *dev)
302 struct sis7019 *sis = dev;
303 unsigned long io = sis->ioport;
307 /* We only use the DMA interrupts, and we don't enable any other
308 * source of interrupts. But, it is possible to see an interupt
309 * status that didn't actually interrupt us, so eliminate anything
310 * we're not expecting to avoid falsely claiming an IRQ, and an
311 * ensuing endless loop.
313 intr = inl(io + SIS_GISR);
314 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
315 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
320 status = inl(io + SIS_PISR_A);
322 sis_voice_irq(status, sis->voices);
323 outl(status, io + SIS_PISR_A);
326 status = inl(io + SIS_PISR_B);
328 sis_voice_irq(status, &sis->voices[32]);
329 outl(status, io + SIS_PISR_B);
332 status = inl(io + SIS_RISR);
334 voice = &sis->capture_voice;
336 snd_pcm_period_elapsed(voice->substream);
338 outl(status, io + SIS_RISR);
341 outl(intr, io + SIS_GISR);
342 intr = inl(io + SIS_GISR);
343 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
344 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
350 static u32 sis_rate_to_delta(unsigned int rate)
354 /* This was copied from the trident driver, but it seems its gotten
355 * around a bit... nevertheless, it works well.
357 * We special case 44100 and 8000 since rounding with the equation
358 * does not give us an accurate enough value. For 11025 and 22050
359 * the equation gives us the best answer. All other frequencies will
360 * also use the equation. JDW
364 else if (rate == 8000)
366 else if (rate == 48000)
369 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
373 static void __sis_map_silence(struct sis7019 *sis)
375 /* Helper function: must hold sis->voice_lock on entry */
376 if (!sis->silence_users)
377 sis->silence_dma_addr = pci_map_single(sis->pci,
378 sis->suspend_state[0],
379 4096, PCI_DMA_TODEVICE);
380 sis->silence_users++;
383 static void __sis_unmap_silence(struct sis7019 *sis)
385 /* Helper function: must hold sis->voice_lock on entry */
386 sis->silence_users--;
387 if (!sis->silence_users)
388 pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096,
392 static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
396 spin_lock_irqsave(&sis->voice_lock, flags);
398 __sis_unmap_silence(sis);
399 voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
401 voice->timing = NULL;
403 voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
404 spin_unlock_irqrestore(&sis->voice_lock, flags);
407 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
409 /* Must hold the voice_lock on entry */
413 for (i = 0; i < 64; i++) {
414 voice = &sis->voices[i];
415 if (voice->flags & VOICE_IN_USE)
417 voice->flags |= VOICE_IN_USE;
426 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
431 spin_lock_irqsave(&sis->voice_lock, flags);
432 voice = __sis_alloc_playback_voice(sis);
433 spin_unlock_irqrestore(&sis->voice_lock, flags);
438 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
439 struct snd_pcm_hw_params *hw_params)
441 struct sis7019 *sis = snd_pcm_substream_chip(substream);
442 struct snd_pcm_runtime *runtime = substream->runtime;
443 struct voice *voice = runtime->private_data;
444 unsigned int period_size, buffer_size;
448 /* If there are one or two periods per buffer, we don't need a
449 * timing voice, as we can use the capture channel's interrupts
450 * to clock out the periods.
452 period_size = params_period_size(hw_params);
453 buffer_size = params_buffer_size(hw_params);
454 needed = (period_size != buffer_size &&
455 period_size != (buffer_size / 2));
457 if (needed && !voice->timing) {
458 spin_lock_irqsave(&sis->voice_lock, flags);
459 voice->timing = __sis_alloc_playback_voice(sis);
461 __sis_map_silence(sis);
462 spin_unlock_irqrestore(&sis->voice_lock, flags);
465 voice->timing->substream = substream;
466 } else if (!needed && voice->timing) {
467 sis_free_voice(sis, voice);
468 voice->timing = NULL;
474 static int sis_playback_open(struct snd_pcm_substream *substream)
476 struct sis7019 *sis = snd_pcm_substream_chip(substream);
477 struct snd_pcm_runtime *runtime = substream->runtime;
480 voice = sis_alloc_playback_voice(sis);
484 voice->substream = substream;
485 runtime->private_data = voice;
486 runtime->hw = sis_playback_hw_info;
487 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
489 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
491 snd_pcm_set_sync(substream);
495 static int sis_substream_close(struct snd_pcm_substream *substream)
497 struct sis7019 *sis = snd_pcm_substream_chip(substream);
498 struct snd_pcm_runtime *runtime = substream->runtime;
499 struct voice *voice = runtime->private_data;
501 sis_free_voice(sis, voice);
505 static int sis_playback_hw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params *hw_params)
508 return snd_pcm_lib_malloc_pages(substream,
509 params_buffer_bytes(hw_params));
512 static int sis_hw_free(struct snd_pcm_substream *substream)
514 return snd_pcm_lib_free_pages(substream);
517 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
519 struct snd_pcm_runtime *runtime = substream->runtime;
520 struct voice *voice = runtime->private_data;
521 void __iomem *ctrl_base = voice->ctrl_base;
522 void __iomem *wave_base = voice->wave_base;
523 u32 format, dma_addr, control, sso_eso, delta, reg;
526 /* We rely on the PCM core to ensure that the parameters for this
527 * substream do not change on us while we're programming the HW.
530 if (snd_pcm_format_width(runtime->format) == 8)
531 format |= SIS_PLAY_DMA_FORMAT_8BIT;
532 if (!snd_pcm_format_signed(runtime->format))
533 format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
534 if (runtime->channels == 1)
535 format |= SIS_PLAY_DMA_FORMAT_MONO;
537 /* The baseline setup is for a single period per buffer, and
538 * we add bells and whistles as needed from there.
540 dma_addr = runtime->dma_addr;
541 leo = runtime->buffer_size - 1;
542 control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
545 if (runtime->period_size == (runtime->buffer_size / 2)) {
546 control |= SIS_PLAY_DMA_INTR_AT_MLP;
547 } else if (runtime->period_size != runtime->buffer_size) {
548 voice->flags |= VOICE_SSO_TIMING;
549 voice->sso = runtime->period_size - 1;
550 voice->period_size = runtime->period_size;
551 voice->buffer_size = runtime->buffer_size;
553 control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
554 control |= SIS_PLAY_DMA_INTR_AT_SSO;
555 sso_eso |= (runtime->period_size - 1) << 16;
558 delta = sis_rate_to_delta(runtime->rate);
560 /* Ok, we're ready to go, set up the channel.
562 writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
563 writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
564 writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
565 writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
567 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
568 writel(0, wave_base + reg);
570 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
571 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
572 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
573 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
574 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
575 wave_base + SIS_WAVE_CHANNEL_CONTROL);
577 /* Force PCI writes to post. */
583 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
585 struct sis7019 *sis = snd_pcm_substream_chip(substream);
586 unsigned long io = sis->ioport;
587 struct snd_pcm_substream *s;
592 u32 play[2] = { 0, 0 };
594 /* No locks needed, as the PCM core will hold the locks on the
595 * substreams, and the HW will only start/stop the indicated voices
596 * without changing the state of the others.
599 case SNDRV_PCM_TRIGGER_START:
600 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
601 case SNDRV_PCM_TRIGGER_RESUME:
604 case SNDRV_PCM_TRIGGER_STOP:
605 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
606 case SNDRV_PCM_TRIGGER_SUSPEND:
613 snd_pcm_group_for_each_entry(s, substream) {
614 /* Make sure it is for us... */
615 chip = snd_pcm_substream_chip(s);
619 voice = s->runtime->private_data;
620 if (voice->flags & VOICE_CAPTURE) {
621 record |= 1 << voice->num;
622 voice = voice->timing;
625 /* voice could be NULL if this a recording stream, and it
626 * doesn't have an external timing channel.
629 play[voice->num / 32] |= 1 << (voice->num & 0x1f);
631 snd_pcm_trigger_done(s, substream);
636 outl(record, io + SIS_RECORD_START_REG);
638 outl(play[0], io + SIS_PLAY_START_A_REG);
640 outl(play[1], io + SIS_PLAY_START_B_REG);
643 outl(record, io + SIS_RECORD_STOP_REG);
645 outl(play[0], io + SIS_PLAY_STOP_A_REG);
647 outl(play[1], io + SIS_PLAY_STOP_B_REG);
652 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
654 struct snd_pcm_runtime *runtime = substream->runtime;
655 struct voice *voice = runtime->private_data;
658 cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
663 static int sis_capture_open(struct snd_pcm_substream *substream)
665 struct sis7019 *sis = snd_pcm_substream_chip(substream);
666 struct snd_pcm_runtime *runtime = substream->runtime;
667 struct voice *voice = &sis->capture_voice;
670 /* FIXME: The driver only supports recording from one channel
671 * at the moment, but it could support more.
673 spin_lock_irqsave(&sis->voice_lock, flags);
674 if (voice->flags & VOICE_IN_USE)
677 voice->flags |= VOICE_IN_USE;
678 spin_unlock_irqrestore(&sis->voice_lock, flags);
683 voice->substream = substream;
684 runtime->private_data = voice;
685 runtime->hw = sis_capture_hw_info;
686 runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
687 snd_pcm_limit_hw_rates(runtime);
688 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
690 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
692 snd_pcm_set_sync(substream);
696 static int sis_capture_hw_params(struct snd_pcm_substream *substream,
697 struct snd_pcm_hw_params *hw_params)
699 struct sis7019 *sis = snd_pcm_substream_chip(substream);
702 rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
703 params_rate(hw_params));
707 rc = snd_pcm_lib_malloc_pages(substream,
708 params_buffer_bytes(hw_params));
712 rc = sis_alloc_timing_voice(substream, hw_params);
718 static void sis_prepare_timing_voice(struct voice *voice,
719 struct snd_pcm_substream *substream)
721 struct sis7019 *sis = snd_pcm_substream_chip(substream);
722 struct snd_pcm_runtime *runtime = substream->runtime;
723 struct voice *timing = voice->timing;
724 void __iomem *play_base = timing->ctrl_base;
725 void __iomem *wave_base = timing->wave_base;
726 u16 buffer_size, period_size;
727 u32 format, control, sso_eso, delta;
728 u32 vperiod, sso, reg;
730 /* Set our initial buffer and period as large as we can given a
731 * single page of silence.
733 buffer_size = 4096 / runtime->channels;
734 buffer_size /= snd_pcm_format_size(runtime->format, 1);
735 period_size = buffer_size;
737 /* Initially, we want to interrupt just a bit behind the end of
738 * the period we're clocking out. 10 samples seems to give a good
741 * We want to spread our interrupts throughout the virtual period,
742 * so that we don't end up with two interrupts back to back at the
743 * end -- this helps minimize the effects of any jitter. Adjust our
744 * clocking period size so that the last period is at least a fourth
747 * This is all moot if we don't need to use virtual periods.
749 vperiod = runtime->period_size + 10;
750 if (vperiod > period_size) {
751 u16 tail = vperiod % period_size;
752 u16 quarter_period = period_size / 4;
754 if (tail && tail < quarter_period) {
755 u16 loops = vperiod / period_size;
757 tail = quarter_period - tail;
763 sso = period_size - 1;
765 /* The initial period will fit inside the buffer, so we
766 * don't need to use virtual periods -- disable them.
768 period_size = runtime->period_size;
773 /* The interrupt handler implements the timing syncronization, so
776 timing->flags |= VOICE_SYNC_TIMING;
777 timing->sync_base = voice->ctrl_base;
778 timing->sync_cso = runtime->period_size - 1;
779 timing->sync_period_size = runtime->period_size;
780 timing->sync_buffer_size = runtime->buffer_size;
781 timing->period_size = period_size;
782 timing->buffer_size = buffer_size;
784 timing->vperiod = vperiod;
786 /* Using unsigned samples with the all-zero silence buffer
787 * forces the output to the lower rail, killing playback.
788 * So ignore unsigned vs signed -- it doesn't change the timing.
791 if (snd_pcm_format_width(runtime->format) == 8)
792 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
793 if (runtime->channels == 1)
794 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
796 control = timing->buffer_size - 1;
797 control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
798 sso_eso = timing->buffer_size - 1;
799 sso_eso |= timing->sso << 16;
801 delta = sis_rate_to_delta(runtime->rate);
803 /* We've done the math, now configure the channel.
805 writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
806 writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
807 writel(control, play_base + SIS_PLAY_DMA_CONTROL);
808 writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
810 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
811 writel(0, wave_base + reg);
813 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
814 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
815 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
816 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
817 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
818 wave_base + SIS_WAVE_CHANNEL_CONTROL);
821 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
823 struct snd_pcm_runtime *runtime = substream->runtime;
824 struct voice *voice = runtime->private_data;
825 void __iomem *rec_base = voice->ctrl_base;
826 u32 format, dma_addr, control;
829 /* We rely on the PCM core to ensure that the parameters for this
830 * substream do not change on us while we're programming the HW.
833 if (snd_pcm_format_width(runtime->format) == 8)
834 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
835 if (!snd_pcm_format_signed(runtime->format))
836 format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
837 if (runtime->channels == 1)
838 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
840 dma_addr = runtime->dma_addr;
841 leo = runtime->buffer_size - 1;
842 control = leo | SIS_CAPTURE_DMA_LOOP;
844 /* If we've got more than two periods per buffer, then we have
845 * use a timing voice to clock out the periods. Otherwise, we can
846 * use the capture channel's interrupts.
849 sis_prepare_timing_voice(voice, substream);
851 control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
852 if (runtime->period_size != runtime->buffer_size)
853 control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
856 writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
857 writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
858 writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
860 /* Force the writes to post. */
866 static struct snd_pcm_ops sis_playback_ops = {
867 .open = sis_playback_open,
868 .close = sis_substream_close,
869 .ioctl = snd_pcm_lib_ioctl,
870 .hw_params = sis_playback_hw_params,
871 .hw_free = sis_hw_free,
872 .prepare = sis_pcm_playback_prepare,
873 .trigger = sis_pcm_trigger,
874 .pointer = sis_pcm_pointer,
877 static struct snd_pcm_ops sis_capture_ops = {
878 .open = sis_capture_open,
879 .close = sis_substream_close,
880 .ioctl = snd_pcm_lib_ioctl,
881 .hw_params = sis_capture_hw_params,
882 .hw_free = sis_hw_free,
883 .prepare = sis_pcm_capture_prepare,
884 .trigger = sis_pcm_trigger,
885 .pointer = sis_pcm_pointer,
888 static int __devinit sis_pcm_create(struct sis7019 *sis)
893 /* We have 64 voices, and the driver currently records from
894 * only one channel, though that could change in the future.
896 rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
900 pcm->private_data = sis;
901 strcpy(pcm->name, "SiS7019");
904 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
905 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
907 /* Try to preallocate some memory, but it's not the end of the
908 * world if this fails.
910 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
911 snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
916 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
918 unsigned long io = sis->ioport;
919 unsigned short val = 0xffff;
923 static const u16 codec_ready[3] = {
924 SIS_AC97_STATUS_CODEC_READY,
925 SIS_AC97_STATUS_CODEC2_READY,
926 SIS_AC97_STATUS_CODEC3_READY,
929 rdy = codec_ready[codec];
932 /* Get the AC97 semaphore -- software first, so we don't spin
933 * pounding out IO reads on the hardware semaphore...
935 mutex_lock(&sis->ac97_mutex);
938 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
944 /* ... and wait for any outstanding commands to complete ...
948 status = inw(io + SIS_AC97_STATUS);
949 if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
958 /* ... before sending our command and waiting for it to finish ...
960 outl(cmd, io + SIS_AC97_CMD);
964 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
967 /* ... and reading the results (if any).
969 val = inl(io + SIS_AC97_CMD) >> 16;
972 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
974 mutex_unlock(&sis->ac97_mutex);
977 printk(KERN_ERR "sis7019: ac97 codec %d timeout cmd 0x%08x\n",
984 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
987 static const u32 cmd[3] = {
988 SIS_AC97_CMD_CODEC_WRITE,
989 SIS_AC97_CMD_CODEC2_WRITE,
990 SIS_AC97_CMD_CODEC3_WRITE,
992 sis_ac97_rw(ac97->private_data, ac97->num,
993 (val << 16) | (reg << 8) | cmd[ac97->num]);
996 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
998 static const u32 cmd[3] = {
999 SIS_AC97_CMD_CODEC_READ,
1000 SIS_AC97_CMD_CODEC2_READ,
1001 SIS_AC97_CMD_CODEC3_READ,
1003 return sis_ac97_rw(ac97->private_data, ac97->num,
1004 (reg << 8) | cmd[ac97->num]);
1007 static int __devinit sis_mixer_create(struct sis7019 *sis)
1009 struct snd_ac97_bus *bus;
1010 struct snd_ac97_template ac97;
1011 static struct snd_ac97_bus_ops ops = {
1012 .write = sis_ac97_write,
1013 .read = sis_ac97_read,
1017 memset(&ac97, 0, sizeof(ac97));
1018 ac97.private_data = sis;
1020 rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1021 if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1022 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1024 if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1025 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1027 if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1028 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1030 /* If we return an error here, then snd_card_free() should
1031 * free up any ac97 codecs that got created, as well as the bus.
1036 static void sis_free_suspend(struct sis7019 *sis)
1040 for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1041 kfree(sis->suspend_state[i]);
1044 static int sis_chip_free(struct sis7019 *sis)
1046 /* Reset the chip, and disable all interrputs.
1048 outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1050 outl(0, sis->ioport + SIS_GCR);
1051 outl(0, sis->ioport + SIS_GIER);
1053 /* Now, free everything we allocated.
1056 free_irq(sis->irq, sis);
1059 iounmap(sis->ioaddr);
1061 pci_release_regions(sis->pci);
1062 pci_disable_device(sis->pci);
1064 sis_free_suspend(sis);
1068 static int sis_dev_free(struct snd_device *dev)
1070 struct sis7019 *sis = dev->device_data;
1071 return sis_chip_free(sis);
1074 static int sis_chip_init(struct sis7019 *sis)
1076 unsigned long io = sis->ioport;
1077 void __iomem *ioaddr = sis->ioaddr;
1082 /* Reset the audio controller
1084 outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1086 outl(0, io + SIS_GCR);
1088 /* Get the AC-link semaphore, and reset the codecs
1091 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1097 outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1101 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1104 /* Now that we've finished the reset, find out what's attached.
1106 status = inl(io + SIS_AC97_STATUS);
1107 if (status & SIS_AC97_STATUS_CODEC_READY)
1108 sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1109 if (status & SIS_AC97_STATUS_CODEC2_READY)
1110 sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1111 if (status & SIS_AC97_STATUS_CODEC3_READY)
1112 sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1114 /* All done, let go of the semaphore, and check for errors
1116 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1117 if (!sis->codecs_present || !count)
1120 /* Let the hardware know that the audio driver is alive,
1121 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1122 * record channels. We're going to want to use Variable Rate Audio
1123 * for recording, to avoid needlessly resampling from 48kHZ.
1125 outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1126 outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1127 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1128 SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1129 SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1131 /* All AC97 PCM slots should be sourced from sub-mixer 0.
1133 outl(0, io + SIS_AC97_PSR);
1135 /* There is only one valid DMA setup for a PCI environment.
1137 outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1139 /* Reset the syncronization groups for all of the channels
1140 * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc.
1141 * we'll need to change how we handle these. Until then, we just
1142 * assign sub-mixer 0 to all playback channels, and avoid any
1143 * attenuation on the audio.
1145 outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1146 outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1147 outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1148 outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1149 outl(0, io + SIS_MIXER_SYNC_GROUP);
1151 for (i = 0; i < 64; i++) {
1152 writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1153 writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1154 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1157 /* Don't attenuate any audio set for the wave amplifier.
1159 * FIXME: Maximum attenuation is set for the music amp, which will
1160 * need to change if we start using the synth engine.
1162 outl(0xffff0000, io + SIS_WEVCR);
1164 /* Ensure that the wave engine is in normal operating mode.
1166 outl(0, io + SIS_WECCR);
1168 /* Go ahead and enable the DMA interrupts. They won't go live
1169 * until we start a channel.
1171 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1172 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1178 static int sis_suspend(struct pci_dev *pci, pm_message_t state)
1180 struct snd_card *card = pci_get_drvdata(pci);
1181 struct sis7019 *sis = card->private_data;
1182 void __iomem *ioaddr = sis->ioaddr;
1185 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1186 snd_pcm_suspend_all(sis->pcm);
1187 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1188 snd_ac97_suspend(sis->ac97[0]);
1189 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1190 snd_ac97_suspend(sis->ac97[1]);
1191 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1192 snd_ac97_suspend(sis->ac97[2]);
1194 /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1196 if (sis->irq >= 0) {
1197 free_irq(sis->irq, sis);
1201 /* Save the internal state away
1203 for (i = 0; i < 4; i++) {
1204 memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1208 pci_disable_device(pci);
1209 pci_save_state(pci);
1210 pci_set_power_state(pci, pci_choose_state(pci, state));
1214 static int sis_resume(struct pci_dev *pci)
1216 struct snd_card *card = pci_get_drvdata(pci);
1217 struct sis7019 *sis = card->private_data;
1218 void __iomem *ioaddr = sis->ioaddr;
1221 pci_set_power_state(pci, PCI_D0);
1222 pci_restore_state(pci);
1224 if (pci_enable_device(pci) < 0) {
1225 printk(KERN_ERR "sis7019: unable to re-enable device\n");
1229 if (sis_chip_init(sis)) {
1230 printk(KERN_ERR "sis7019: unable to re-init controller\n");
1234 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1235 card->shortname, sis)) {
1236 printk(KERN_ERR "sis7019: unable to regain IRQ %d\n", pci->irq);
1240 /* Restore saved state, then clear out the page we use for the
1243 for (i = 0; i < 4; i++) {
1244 memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1248 memset(sis->suspend_state[0], 0, 4096);
1250 sis->irq = pci->irq;
1251 pci_set_master(pci);
1253 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1254 snd_ac97_resume(sis->ac97[0]);
1255 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1256 snd_ac97_resume(sis->ac97[1]);
1257 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1258 snd_ac97_resume(sis->ac97[2]);
1260 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1264 snd_card_disconnect(card);
1267 #endif /* CONFIG_PM */
1269 static int sis_alloc_suspend(struct sis7019 *sis)
1273 /* We need 16K to store the internal wave engine state during a
1274 * suspend, but we don't need it to be contiguous, so play nice
1275 * with the memory system. We'll also use this area for a silence
1278 for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1279 sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1280 if (!sis->suspend_state[i])
1283 memset(sis->suspend_state[0], 0, 4096);
1288 static int __devinit sis_chip_create(struct snd_card *card,
1289 struct pci_dev *pci)
1291 struct sis7019 *sis = card->private_data;
1292 struct voice *voice;
1293 static struct snd_device_ops ops = {
1294 .dev_free = sis_dev_free,
1299 rc = pci_enable_device(pci);
1303 if (pci_set_dma_mask(pci, DMA_30BIT_MASK) < 0) {
1304 printk(KERN_ERR "sis7019: architecture does not support "
1305 "30-bit PCI busmaster DMA");
1306 goto error_out_enabled;
1309 memset(sis, 0, sizeof(*sis));
1310 mutex_init(&sis->ac97_mutex);
1311 spin_lock_init(&sis->voice_lock);
1315 sis->ioport = pci_resource_start(pci, 0);
1317 rc = pci_request_regions(pci, "SiS7019");
1319 printk(KERN_ERR "sis7019: unable request regions\n");
1320 goto error_out_enabled;
1324 sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1326 printk(KERN_ERR "sis7019: unable to remap MMIO, aborting\n");
1327 goto error_out_cleanup;
1330 rc = sis_alloc_suspend(sis);
1332 printk(KERN_ERR "sis7019: unable to allocate state storage\n");
1333 goto error_out_cleanup;
1336 rc = sis_chip_init(sis);
1338 goto error_out_cleanup;
1340 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1341 card->shortname, sis)) {
1342 printk(KERN_ERR "unable to allocate irq %d\n", sis->irq);
1343 goto error_out_cleanup;
1346 sis->irq = pci->irq;
1347 pci_set_master(pci);
1349 for (i = 0; i < 64; i++) {
1350 voice = &sis->voices[i];
1352 voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1353 voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1356 voice = &sis->capture_voice;
1357 voice->flags = VOICE_CAPTURE;
1358 voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1359 voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1361 rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1363 goto error_out_cleanup;
1365 snd_card_set_dev(card, &pci->dev);
1373 pci_disable_device(pci);
1379 static int __devinit snd_sis7019_probe(struct pci_dev *pci,
1380 const struct pci_device_id *pci_id)
1382 struct snd_card *card;
1383 struct sis7019 *sis;
1391 card = snd_card_new(index, id, THIS_MODULE, sizeof(*sis));
1395 strcpy(card->driver, "SiS7019");
1396 strcpy(card->shortname, "SiS7019");
1397 rc = sis_chip_create(card, pci);
1399 goto card_error_out;
1401 sis = card->private_data;
1403 rc = sis_mixer_create(sis);
1405 goto card_error_out;
1407 rc = sis_pcm_create(sis);
1409 goto card_error_out;
1411 snprintf(card->longname, sizeof(card->longname),
1412 "%s Audio Accelerator with %s at 0x%lx, irq %d",
1413 card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1414 sis->ioport, sis->irq);
1416 rc = snd_card_register(card);
1418 goto card_error_out;
1420 pci_set_drvdata(pci, card);
1424 snd_card_free(card);
1430 static void __devexit snd_sis7019_remove(struct pci_dev *pci)
1432 snd_card_free(pci_get_drvdata(pci));
1433 pci_set_drvdata(pci, NULL);
1436 static struct pci_driver sis7019_driver = {
1438 .id_table = snd_sis7019_ids,
1439 .probe = snd_sis7019_probe,
1440 .remove = __devexit_p(snd_sis7019_remove),
1443 .suspend = sis_suspend,
1444 .resume = sis_resume,
1448 static int __init sis7019_init(void)
1450 return pci_register_driver(&sis7019_driver);
1453 static void __exit sis7019_exit(void)
1455 pci_unregister_driver(&sis7019_driver);
1458 module_init(sis7019_init);
1459 module_exit(sis7019_exit);