1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for SiS7019 Audio Accelerator
5 * Copyright (C) 2004-2007, David Dillow
6 * Written by David Dillow <dave@thedillows.org>
7 * Inspired by the Trident 4D-WaveDX/NX driver.
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <sound/core.h>
20 #include <sound/ac97_codec.h>
21 #include <sound/initval.h>
24 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
25 MODULE_DESCRIPTION("SiS7019");
26 MODULE_LICENSE("GPL");
28 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
29 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
30 static bool enable
= 1;
31 static int codecs
= 1;
33 module_param(index
, int, 0444);
34 MODULE_PARM_DESC(index
, "Index value for SiS7019 Audio Accelerator.");
35 module_param(id
, charp
, 0444);
36 MODULE_PARM_DESC(id
, "ID string for SiS7019 Audio Accelerator.");
37 module_param(enable
, bool, 0444);
38 MODULE_PARM_DESC(enable
, "Enable SiS7019 Audio Accelerator.");
39 module_param(codecs
, int, 0444);
40 MODULE_PARM_DESC(codecs
, "Set bit to indicate that codec number is expected to be present (default 1)");
42 static const struct pci_device_id snd_sis7019_ids
[] = {
43 { PCI_DEVICE(PCI_VENDOR_ID_SI
, 0x7019) },
47 MODULE_DEVICE_TABLE(pci
, snd_sis7019_ids
);
49 /* There are three timing modes for the voices.
51 * For both playback and capture, when the buffer is one or two periods long,
52 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
53 * to let us know when the periods have ended.
55 * When performing playback with more than two periods per buffer, we set
56 * the "Stop Sample Offset" and tell the hardware to interrupt us when we
57 * reach it. We then update the offset and continue on until we are
58 * interrupted for the next period.
60 * Capture channels do not have a SSO, so we allocate a playback channel to
61 * use as a timer for the capture periods. We use the SSO on the playback
62 * channel to clock out virtual periods, and adjust the virtual period length
63 * to maintain synchronization. This algorithm came from the Trident driver.
65 * FIXME: It'd be nice to make use of some of the synth features in the
66 * hardware, but a woeful lack of documentation is a significant roadblock.
70 #define VOICE_IN_USE 1
71 #define VOICE_CAPTURE 2
72 #define VOICE_SSO_TIMING 4
73 #define VOICE_SYNC_TIMING 8
81 struct snd_pcm_substream
*substream
;
83 void __iomem
*ctrl_base
;
84 void __iomem
*wave_base
;
85 void __iomem
*sync_base
;
89 /* We need four pages to store our wave parameters during a suspend. If
90 * we're not doing power management, we still need to allocate a page
91 * for the silence buffer.
93 #ifdef CONFIG_PM_SLEEP
94 #define SIS_SUSPEND_PAGES 4
96 #define SIS_SUSPEND_PAGES 1
100 unsigned long ioport
;
101 void __iomem
*ioaddr
;
107 struct snd_card
*card
;
108 struct snd_ac97
*ac97
[3];
110 /* Protect against more than one thread hitting the AC97
111 * registers (in a more polite manner than pounding the hardware
114 struct mutex ac97_mutex
;
116 /* voice_lock protects allocation/freeing of the voice descriptions
118 spinlock_t voice_lock
;
120 struct voice voices
[64];
121 struct voice capture_voice
;
123 /* Allocate pages to store the internal wave state during
124 * suspends. When we're operating, this can be used as a silence
125 * buffer for a timing channel.
127 void *suspend_state
[SIS_SUSPEND_PAGES
];
130 dma_addr_t silence_dma_addr
;
133 /* These values are also used by the module param 'codecs' to indicate
134 * which codecs should be present.
136 #define SIS_PRIMARY_CODEC_PRESENT 0x0001
137 #define SIS_SECONDARY_CODEC_PRESENT 0x0002
138 #define SIS_TERTIARY_CODEC_PRESENT 0x0004
140 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
141 * documented range of 8-0xfff8 samples. Given that they are 0-based,
142 * that places our period/buffer range at 9-0xfff9 samples. That makes the
143 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
144 * max samples / min samples gives us the max periods in a buffer.
146 * We'll add a constraint upon open that limits the period and buffer sample
147 * size to values that are legal for the hardware.
149 static const struct snd_pcm_hardware sis_playback_hw_info
= {
150 .info
= (SNDRV_PCM_INFO_MMAP
|
151 SNDRV_PCM_INFO_MMAP_VALID
|
152 SNDRV_PCM_INFO_INTERLEAVED
|
153 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
154 SNDRV_PCM_INFO_SYNC_START
|
155 SNDRV_PCM_INFO_RESUME
),
156 .formats
= (SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U8
|
157 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U16_LE
),
158 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_CONTINUOUS
,
163 .buffer_bytes_max
= (0xfff9 * 4),
164 .period_bytes_min
= 9,
165 .period_bytes_max
= (0xfff9 * 4),
167 .periods_max
= (0xfff9 / 9),
170 static const struct snd_pcm_hardware sis_capture_hw_info
= {
171 .info
= (SNDRV_PCM_INFO_MMAP
|
172 SNDRV_PCM_INFO_MMAP_VALID
|
173 SNDRV_PCM_INFO_INTERLEAVED
|
174 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
175 SNDRV_PCM_INFO_SYNC_START
|
176 SNDRV_PCM_INFO_RESUME
),
177 .formats
= (SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U8
|
178 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U16_LE
),
179 .rates
= SNDRV_PCM_RATE_48000
,
184 .buffer_bytes_max
= (0xfff9 * 4),
185 .period_bytes_min
= 9,
186 .period_bytes_max
= (0xfff9 * 4),
188 .periods_max
= (0xfff9 / 9),
191 static void sis_update_sso(struct voice
*voice
, u16 period
)
193 void __iomem
*base
= voice
->ctrl_base
;
195 voice
->sso
+= period
;
196 if (voice
->sso
>= voice
->buffer_size
)
197 voice
->sso
-= voice
->buffer_size
;
199 /* Enforce the documented hardware minimum offset */
203 /* The SSO is in the upper 16 bits of the register. */
204 writew(voice
->sso
& 0xffff, base
+ SIS_PLAY_DMA_SSO_ESO
+ 2);
207 static void sis_update_voice(struct voice
*voice
)
209 if (voice
->flags
& VOICE_SSO_TIMING
) {
210 sis_update_sso(voice
, voice
->period_size
);
211 } else if (voice
->flags
& VOICE_SYNC_TIMING
) {
214 /* If we've not hit the end of the virtual period, update
215 * our records and keep going.
217 if (voice
->vperiod
> voice
->period_size
) {
218 voice
->vperiod
-= voice
->period_size
;
219 if (voice
->vperiod
< voice
->period_size
)
220 sis_update_sso(voice
, voice
->vperiod
);
222 sis_update_sso(voice
, voice
->period_size
);
226 /* Calculate our relative offset between the target and
227 * the actual CSO value. Since we're operating in a loop,
228 * if the value is more than half way around, we can
229 * consider ourselves wrapped.
231 sync
= voice
->sync_cso
;
232 sync
-= readw(voice
->sync_base
+ SIS_CAPTURE_DMA_FORMAT_CSO
);
233 if (sync
> (voice
->sync_buffer_size
/ 2))
234 sync
-= voice
->sync_buffer_size
;
236 /* If sync is positive, then we interrupted too early, and
237 * we'll need to come back in a few samples and try again.
238 * There's a minimum wait, as it takes some time for the DMA
239 * engine to startup, etc...
244 sis_update_sso(voice
, sync
);
248 /* Ok, we interrupted right on time, or (hopefully) just
249 * a bit late. We'll adjst our next waiting period based
250 * on how close we got.
252 * We need to stay just behind the actual channel to ensure
253 * it really is past a period when we get our interrupt --
254 * otherwise we'll fall into the early code above and have
255 * a minimum wait time, which makes us quite late here,
256 * eating into the user's time to refresh the buffer, esp.
257 * if using small periods.
259 * If we're less than 9 samples behind, we're on target.
260 * Otherwise, shorten the next vperiod by the amount we've
264 voice
->vperiod
= voice
->sync_period_size
+ 1;
266 voice
->vperiod
= voice
->sync_period_size
+ sync
+ 10;
268 if (voice
->vperiod
< voice
->buffer_size
) {
269 sis_update_sso(voice
, voice
->vperiod
);
272 sis_update_sso(voice
, voice
->period_size
);
274 sync
= voice
->sync_cso
+ voice
->sync_period_size
;
275 if (sync
>= voice
->sync_buffer_size
)
276 sync
-= voice
->sync_buffer_size
;
277 voice
->sync_cso
= sync
;
280 snd_pcm_period_elapsed(voice
->substream
);
283 static void sis_voice_irq(u32 status
, struct voice
*voice
)
291 sis_update_voice(voice
);
296 static irqreturn_t
sis_interrupt(int irq
, void *dev
)
298 struct sis7019
*sis
= dev
;
299 unsigned long io
= sis
->ioport
;
303 /* We only use the DMA interrupts, and we don't enable any other
304 * source of interrupts. But, it is possible to see an interrupt
305 * status that didn't actually interrupt us, so eliminate anything
306 * we're not expecting to avoid falsely claiming an IRQ, and an
307 * ensuing endless loop.
309 intr
= inl(io
+ SIS_GISR
);
310 intr
&= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS
|
311 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS
;
316 status
= inl(io
+ SIS_PISR_A
);
318 sis_voice_irq(status
, sis
->voices
);
319 outl(status
, io
+ SIS_PISR_A
);
322 status
= inl(io
+ SIS_PISR_B
);
324 sis_voice_irq(status
, &sis
->voices
[32]);
325 outl(status
, io
+ SIS_PISR_B
);
328 status
= inl(io
+ SIS_RISR
);
330 voice
= &sis
->capture_voice
;
332 snd_pcm_period_elapsed(voice
->substream
);
334 outl(status
, io
+ SIS_RISR
);
337 outl(intr
, io
+ SIS_GISR
);
338 intr
= inl(io
+ SIS_GISR
);
339 intr
&= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS
|
340 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS
;
346 static u32
sis_rate_to_delta(unsigned int rate
)
350 /* This was copied from the trident driver, but it seems its gotten
351 * around a bit... nevertheless, it works well.
353 * We special case 44100 and 8000 since rounding with the equation
354 * does not give us an accurate enough value. For 11025 and 22050
355 * the equation gives us the best answer. All other frequencies will
356 * also use the equation. JDW
360 else if (rate
== 8000)
362 else if (rate
== 48000)
365 delta
= DIV_ROUND_CLOSEST(rate
<< 12, 48000) & 0x0000ffff;
369 static void __sis_map_silence(struct sis7019
*sis
)
371 /* Helper function: must hold sis->voice_lock on entry */
372 if (!sis
->silence_users
)
373 sis
->silence_dma_addr
= dma_map_single(&sis
->pci
->dev
,
374 sis
->suspend_state
[0],
375 4096, DMA_TO_DEVICE
);
376 sis
->silence_users
++;
379 static void __sis_unmap_silence(struct sis7019
*sis
)
381 /* Helper function: must hold sis->voice_lock on entry */
382 sis
->silence_users
--;
383 if (!sis
->silence_users
)
384 dma_unmap_single(&sis
->pci
->dev
, sis
->silence_dma_addr
, 4096,
388 static void sis_free_voice(struct sis7019
*sis
, struct voice
*voice
)
392 spin_lock_irqsave(&sis
->voice_lock
, flags
);
394 __sis_unmap_silence(sis
);
395 voice
->timing
->flags
&= ~(VOICE_IN_USE
| VOICE_SSO_TIMING
|
397 voice
->timing
= NULL
;
399 voice
->flags
&= ~(VOICE_IN_USE
| VOICE_SSO_TIMING
| VOICE_SYNC_TIMING
);
400 spin_unlock_irqrestore(&sis
->voice_lock
, flags
);
403 static struct voice
*__sis_alloc_playback_voice(struct sis7019
*sis
)
405 /* Must hold the voice_lock on entry */
409 for (i
= 0; i
< 64; i
++) {
410 voice
= &sis
->voices
[i
];
411 if (voice
->flags
& VOICE_IN_USE
)
413 voice
->flags
|= VOICE_IN_USE
;
422 static struct voice
*sis_alloc_playback_voice(struct sis7019
*sis
)
427 spin_lock_irqsave(&sis
->voice_lock
, flags
);
428 voice
= __sis_alloc_playback_voice(sis
);
429 spin_unlock_irqrestore(&sis
->voice_lock
, flags
);
434 static int sis_alloc_timing_voice(struct snd_pcm_substream
*substream
,
435 struct snd_pcm_hw_params
*hw_params
)
437 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
438 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
439 struct voice
*voice
= runtime
->private_data
;
440 unsigned int period_size
, buffer_size
;
444 /* If there are one or two periods per buffer, we don't need a
445 * timing voice, as we can use the capture channel's interrupts
446 * to clock out the periods.
448 period_size
= params_period_size(hw_params
);
449 buffer_size
= params_buffer_size(hw_params
);
450 needed
= (period_size
!= buffer_size
&&
451 period_size
!= (buffer_size
/ 2));
453 if (needed
&& !voice
->timing
) {
454 spin_lock_irqsave(&sis
->voice_lock
, flags
);
455 voice
->timing
= __sis_alloc_playback_voice(sis
);
457 __sis_map_silence(sis
);
458 spin_unlock_irqrestore(&sis
->voice_lock
, flags
);
461 voice
->timing
->substream
= substream
;
462 } else if (!needed
&& voice
->timing
) {
463 sis_free_voice(sis
, voice
);
464 voice
->timing
= NULL
;
470 static int sis_playback_open(struct snd_pcm_substream
*substream
)
472 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
473 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
476 voice
= sis_alloc_playback_voice(sis
);
480 voice
->substream
= substream
;
481 runtime
->private_data
= voice
;
482 runtime
->hw
= sis_playback_hw_info
;
483 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
485 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
487 snd_pcm_set_sync(substream
);
491 static int sis_substream_close(struct snd_pcm_substream
*substream
)
493 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
494 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
495 struct voice
*voice
= runtime
->private_data
;
497 sis_free_voice(sis
, voice
);
501 static int sis_pcm_playback_prepare(struct snd_pcm_substream
*substream
)
503 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
504 struct voice
*voice
= runtime
->private_data
;
505 void __iomem
*ctrl_base
= voice
->ctrl_base
;
506 void __iomem
*wave_base
= voice
->wave_base
;
507 u32 format
, dma_addr
, control
, sso_eso
, delta
, reg
;
510 /* We rely on the PCM core to ensure that the parameters for this
511 * substream do not change on us while we're programming the HW.
514 if (snd_pcm_format_width(runtime
->format
) == 8)
515 format
|= SIS_PLAY_DMA_FORMAT_8BIT
;
516 if (!snd_pcm_format_signed(runtime
->format
))
517 format
|= SIS_PLAY_DMA_FORMAT_UNSIGNED
;
518 if (runtime
->channels
== 1)
519 format
|= SIS_PLAY_DMA_FORMAT_MONO
;
521 /* The baseline setup is for a single period per buffer, and
522 * we add bells and whistles as needed from there.
524 dma_addr
= runtime
->dma_addr
;
525 leo
= runtime
->buffer_size
- 1;
526 control
= leo
| SIS_PLAY_DMA_LOOP
| SIS_PLAY_DMA_INTR_AT_LEO
;
529 if (runtime
->period_size
== (runtime
->buffer_size
/ 2)) {
530 control
|= SIS_PLAY_DMA_INTR_AT_MLP
;
531 } else if (runtime
->period_size
!= runtime
->buffer_size
) {
532 voice
->flags
|= VOICE_SSO_TIMING
;
533 voice
->sso
= runtime
->period_size
- 1;
534 voice
->period_size
= runtime
->period_size
;
535 voice
->buffer_size
= runtime
->buffer_size
;
537 control
&= ~SIS_PLAY_DMA_INTR_AT_LEO
;
538 control
|= SIS_PLAY_DMA_INTR_AT_SSO
;
539 sso_eso
|= (runtime
->period_size
- 1) << 16;
542 delta
= sis_rate_to_delta(runtime
->rate
);
544 /* Ok, we're ready to go, set up the channel.
546 writel(format
, ctrl_base
+ SIS_PLAY_DMA_FORMAT_CSO
);
547 writel(dma_addr
, ctrl_base
+ SIS_PLAY_DMA_BASE
);
548 writel(control
, ctrl_base
+ SIS_PLAY_DMA_CONTROL
);
549 writel(sso_eso
, ctrl_base
+ SIS_PLAY_DMA_SSO_ESO
);
551 for (reg
= 0; reg
< SIS_WAVE_SIZE
; reg
+= 4)
552 writel(0, wave_base
+ reg
);
554 writel(SIS_WAVE_GENERAL_WAVE_VOLUME
, wave_base
+ SIS_WAVE_GENERAL
);
555 writel(delta
<< 16, wave_base
+ SIS_WAVE_GENERAL_ARTICULATION
);
556 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE
|
557 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE
|
558 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE
,
559 wave_base
+ SIS_WAVE_CHANNEL_CONTROL
);
561 /* Force PCI writes to post. */
567 static int sis_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
569 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
570 unsigned long io
= sis
->ioport
;
571 struct snd_pcm_substream
*s
;
576 u32 play
[2] = { 0, 0 };
578 /* No locks needed, as the PCM core will hold the locks on the
579 * substreams, and the HW will only start/stop the indicated voices
580 * without changing the state of the others.
583 case SNDRV_PCM_TRIGGER_START
:
584 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
585 case SNDRV_PCM_TRIGGER_RESUME
:
588 case SNDRV_PCM_TRIGGER_STOP
:
589 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
590 case SNDRV_PCM_TRIGGER_SUSPEND
:
597 snd_pcm_group_for_each_entry(s
, substream
) {
598 /* Make sure it is for us... */
599 chip
= snd_pcm_substream_chip(s
);
603 voice
= s
->runtime
->private_data
;
604 if (voice
->flags
& VOICE_CAPTURE
) {
605 record
|= 1 << voice
->num
;
606 voice
= voice
->timing
;
609 /* voice could be NULL if this a recording stream, and it
610 * doesn't have an external timing channel.
613 play
[voice
->num
/ 32] |= 1 << (voice
->num
& 0x1f);
615 snd_pcm_trigger_done(s
, substream
);
620 outl(record
, io
+ SIS_RECORD_START_REG
);
622 outl(play
[0], io
+ SIS_PLAY_START_A_REG
);
624 outl(play
[1], io
+ SIS_PLAY_START_B_REG
);
627 outl(record
, io
+ SIS_RECORD_STOP_REG
);
629 outl(play
[0], io
+ SIS_PLAY_STOP_A_REG
);
631 outl(play
[1], io
+ SIS_PLAY_STOP_B_REG
);
636 static snd_pcm_uframes_t
sis_pcm_pointer(struct snd_pcm_substream
*substream
)
638 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
639 struct voice
*voice
= runtime
->private_data
;
642 cso
= readl(voice
->ctrl_base
+ SIS_PLAY_DMA_FORMAT_CSO
);
647 static int sis_capture_open(struct snd_pcm_substream
*substream
)
649 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
650 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
651 struct voice
*voice
= &sis
->capture_voice
;
654 /* FIXME: The driver only supports recording from one channel
655 * at the moment, but it could support more.
657 spin_lock_irqsave(&sis
->voice_lock
, flags
);
658 if (voice
->flags
& VOICE_IN_USE
)
661 voice
->flags
|= VOICE_IN_USE
;
662 spin_unlock_irqrestore(&sis
->voice_lock
, flags
);
667 voice
->substream
= substream
;
668 runtime
->private_data
= voice
;
669 runtime
->hw
= sis_capture_hw_info
;
670 runtime
->hw
.rates
= sis
->ac97
[0]->rates
[AC97_RATES_ADC
];
671 snd_pcm_limit_hw_rates(runtime
);
672 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
674 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
676 snd_pcm_set_sync(substream
);
680 static int sis_capture_hw_params(struct snd_pcm_substream
*substream
,
681 struct snd_pcm_hw_params
*hw_params
)
683 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
686 rc
= snd_ac97_set_rate(sis
->ac97
[0], AC97_PCM_LR_ADC_RATE
,
687 params_rate(hw_params
));
691 rc
= sis_alloc_timing_voice(substream
, hw_params
);
697 static void sis_prepare_timing_voice(struct voice
*voice
,
698 struct snd_pcm_substream
*substream
)
700 struct sis7019
*sis
= snd_pcm_substream_chip(substream
);
701 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
702 struct voice
*timing
= voice
->timing
;
703 void __iomem
*play_base
= timing
->ctrl_base
;
704 void __iomem
*wave_base
= timing
->wave_base
;
705 u16 buffer_size
, period_size
;
706 u32 format
, control
, sso_eso
, delta
;
707 u32 vperiod
, sso
, reg
;
709 /* Set our initial buffer and period as large as we can given a
710 * single page of silence.
712 buffer_size
= 4096 / runtime
->channels
;
713 buffer_size
/= snd_pcm_format_size(runtime
->format
, 1);
714 period_size
= buffer_size
;
716 /* Initially, we want to interrupt just a bit behind the end of
717 * the period we're clocking out. 12 samples seems to give a good
720 * We want to spread our interrupts throughout the virtual period,
721 * so that we don't end up with two interrupts back to back at the
722 * end -- this helps minimize the effects of any jitter. Adjust our
723 * clocking period size so that the last period is at least a fourth
726 * This is all moot if we don't need to use virtual periods.
728 vperiod
= runtime
->period_size
+ 12;
729 if (vperiod
> period_size
) {
730 u16 tail
= vperiod
% period_size
;
731 u16 quarter_period
= period_size
/ 4;
733 if (tail
&& tail
< quarter_period
) {
734 u16 loops
= vperiod
/ period_size
;
736 tail
= quarter_period
- tail
;
742 sso
= period_size
- 1;
744 /* The initial period will fit inside the buffer, so we
745 * don't need to use virtual periods -- disable them.
747 period_size
= runtime
->period_size
;
752 /* The interrupt handler implements the timing synchronization, so
755 timing
->flags
|= VOICE_SYNC_TIMING
;
756 timing
->sync_base
= voice
->ctrl_base
;
757 timing
->sync_cso
= runtime
->period_size
;
758 timing
->sync_period_size
= runtime
->period_size
;
759 timing
->sync_buffer_size
= runtime
->buffer_size
;
760 timing
->period_size
= period_size
;
761 timing
->buffer_size
= buffer_size
;
763 timing
->vperiod
= vperiod
;
765 /* Using unsigned samples with the all-zero silence buffer
766 * forces the output to the lower rail, killing playback.
767 * So ignore unsigned vs signed -- it doesn't change the timing.
770 if (snd_pcm_format_width(runtime
->format
) == 8)
771 format
= SIS_CAPTURE_DMA_FORMAT_8BIT
;
772 if (runtime
->channels
== 1)
773 format
|= SIS_CAPTURE_DMA_FORMAT_MONO
;
775 control
= timing
->buffer_size
- 1;
776 control
|= SIS_PLAY_DMA_LOOP
| SIS_PLAY_DMA_INTR_AT_SSO
;
777 sso_eso
= timing
->buffer_size
- 1;
778 sso_eso
|= timing
->sso
<< 16;
780 delta
= sis_rate_to_delta(runtime
->rate
);
782 /* We've done the math, now configure the channel.
784 writel(format
, play_base
+ SIS_PLAY_DMA_FORMAT_CSO
);
785 writel(sis
->silence_dma_addr
, play_base
+ SIS_PLAY_DMA_BASE
);
786 writel(control
, play_base
+ SIS_PLAY_DMA_CONTROL
);
787 writel(sso_eso
, play_base
+ SIS_PLAY_DMA_SSO_ESO
);
789 for (reg
= 0; reg
< SIS_WAVE_SIZE
; reg
+= 4)
790 writel(0, wave_base
+ reg
);
792 writel(SIS_WAVE_GENERAL_WAVE_VOLUME
, wave_base
+ SIS_WAVE_GENERAL
);
793 writel(delta
<< 16, wave_base
+ SIS_WAVE_GENERAL_ARTICULATION
);
794 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE
|
795 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE
|
796 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE
,
797 wave_base
+ SIS_WAVE_CHANNEL_CONTROL
);
800 static int sis_pcm_capture_prepare(struct snd_pcm_substream
*substream
)
802 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
803 struct voice
*voice
= runtime
->private_data
;
804 void __iomem
*rec_base
= voice
->ctrl_base
;
805 u32 format
, dma_addr
, control
;
808 /* We rely on the PCM core to ensure that the parameters for this
809 * substream do not change on us while we're programming the HW.
812 if (snd_pcm_format_width(runtime
->format
) == 8)
813 format
= SIS_CAPTURE_DMA_FORMAT_8BIT
;
814 if (!snd_pcm_format_signed(runtime
->format
))
815 format
|= SIS_CAPTURE_DMA_FORMAT_UNSIGNED
;
816 if (runtime
->channels
== 1)
817 format
|= SIS_CAPTURE_DMA_FORMAT_MONO
;
819 dma_addr
= runtime
->dma_addr
;
820 leo
= runtime
->buffer_size
- 1;
821 control
= leo
| SIS_CAPTURE_DMA_LOOP
;
823 /* If we've got more than two periods per buffer, then we have
824 * use a timing voice to clock out the periods. Otherwise, we can
825 * use the capture channel's interrupts.
828 sis_prepare_timing_voice(voice
, substream
);
830 control
|= SIS_CAPTURE_DMA_INTR_AT_LEO
;
831 if (runtime
->period_size
!= runtime
->buffer_size
)
832 control
|= SIS_CAPTURE_DMA_INTR_AT_MLP
;
835 writel(format
, rec_base
+ SIS_CAPTURE_DMA_FORMAT_CSO
);
836 writel(dma_addr
, rec_base
+ SIS_CAPTURE_DMA_BASE
);
837 writel(control
, rec_base
+ SIS_CAPTURE_DMA_CONTROL
);
839 /* Force the writes to post. */
845 static const struct snd_pcm_ops sis_playback_ops
= {
846 .open
= sis_playback_open
,
847 .close
= sis_substream_close
,
848 .prepare
= sis_pcm_playback_prepare
,
849 .trigger
= sis_pcm_trigger
,
850 .pointer
= sis_pcm_pointer
,
853 static const struct snd_pcm_ops sis_capture_ops
= {
854 .open
= sis_capture_open
,
855 .close
= sis_substream_close
,
856 .hw_params
= sis_capture_hw_params
,
857 .prepare
= sis_pcm_capture_prepare
,
858 .trigger
= sis_pcm_trigger
,
859 .pointer
= sis_pcm_pointer
,
862 static int sis_pcm_create(struct sis7019
*sis
)
867 /* We have 64 voices, and the driver currently records from
868 * only one channel, though that could change in the future.
870 rc
= snd_pcm_new(sis
->card
, "SiS7019", 0, 64, 1, &pcm
);
874 pcm
->private_data
= sis
;
875 strcpy(pcm
->name
, "SiS7019");
878 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &sis_playback_ops
);
879 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &sis_capture_ops
);
881 /* Try to preallocate some memory, but it's not the end of the
882 * world if this fails.
884 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV
,
885 &sis
->pci
->dev
, 64*1024, 128*1024);
890 static unsigned short sis_ac97_rw(struct sis7019
*sis
, int codec
, u32 cmd
)
892 unsigned long io
= sis
->ioport
;
893 unsigned short val
= 0xffff;
897 static const u16 codec_ready
[3] = {
898 SIS_AC97_STATUS_CODEC_READY
,
899 SIS_AC97_STATUS_CODEC2_READY
,
900 SIS_AC97_STATUS_CODEC3_READY
,
903 rdy
= codec_ready
[codec
];
906 /* Get the AC97 semaphore -- software first, so we don't spin
907 * pounding out IO reads on the hardware semaphore...
909 mutex_lock(&sis
->ac97_mutex
);
912 while ((inw(io
+ SIS_AC97_SEMA
) & SIS_AC97_SEMA_BUSY
) && --count
)
918 /* ... and wait for any outstanding commands to complete ...
922 status
= inw(io
+ SIS_AC97_STATUS
);
923 if ((status
& rdy
) && !(status
& SIS_AC97_STATUS_BUSY
))
932 /* ... before sending our command and waiting for it to finish ...
934 outl(cmd
, io
+ SIS_AC97_CMD
);
938 while ((inw(io
+ SIS_AC97_STATUS
) & SIS_AC97_STATUS_BUSY
) && --count
)
941 /* ... and reading the results (if any).
943 val
= inl(io
+ SIS_AC97_CMD
) >> 16;
946 outl(SIS_AC97_SEMA_RELEASE
, io
+ SIS_AC97_SEMA
);
948 mutex_unlock(&sis
->ac97_mutex
);
951 dev_err(&sis
->pci
->dev
, "ac97 codec %d timeout cmd 0x%08x\n",
958 static void sis_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
961 static const u32 cmd
[3] = {
962 SIS_AC97_CMD_CODEC_WRITE
,
963 SIS_AC97_CMD_CODEC2_WRITE
,
964 SIS_AC97_CMD_CODEC3_WRITE
,
966 sis_ac97_rw(ac97
->private_data
, ac97
->num
,
967 (val
<< 16) | (reg
<< 8) | cmd
[ac97
->num
]);
970 static unsigned short sis_ac97_read(struct snd_ac97
*ac97
, unsigned short reg
)
972 static const u32 cmd
[3] = {
973 SIS_AC97_CMD_CODEC_READ
,
974 SIS_AC97_CMD_CODEC2_READ
,
975 SIS_AC97_CMD_CODEC3_READ
,
977 return sis_ac97_rw(ac97
->private_data
, ac97
->num
,
978 (reg
<< 8) | cmd
[ac97
->num
]);
981 static int sis_mixer_create(struct sis7019
*sis
)
983 struct snd_ac97_bus
*bus
;
984 struct snd_ac97_template ac97
;
985 static const struct snd_ac97_bus_ops ops
= {
986 .write
= sis_ac97_write
,
987 .read
= sis_ac97_read
,
991 memset(&ac97
, 0, sizeof(ac97
));
992 ac97
.private_data
= sis
;
994 rc
= snd_ac97_bus(sis
->card
, 0, &ops
, NULL
, &bus
);
995 if (!rc
&& sis
->codecs_present
& SIS_PRIMARY_CODEC_PRESENT
)
996 rc
= snd_ac97_mixer(bus
, &ac97
, &sis
->ac97
[0]);
998 if (!rc
&& (sis
->codecs_present
& SIS_SECONDARY_CODEC_PRESENT
))
999 rc
= snd_ac97_mixer(bus
, &ac97
, &sis
->ac97
[1]);
1001 if (!rc
&& (sis
->codecs_present
& SIS_TERTIARY_CODEC_PRESENT
))
1002 rc
= snd_ac97_mixer(bus
, &ac97
, &sis
->ac97
[2]);
1004 /* If we return an error here, then snd_card_free() should
1005 * free up any ac97 codecs that got created, as well as the bus.
1010 static void sis_chip_free(struct snd_card
*card
)
1012 struct sis7019
*sis
= card
->private_data
;
1014 /* Reset the chip, and disable all interrputs.
1016 outl(SIS_GCR_SOFTWARE_RESET
, sis
->ioport
+ SIS_GCR
);
1018 outl(0, sis
->ioport
+ SIS_GCR
);
1019 outl(0, sis
->ioport
+ SIS_GIER
);
1021 /* Now, free everything we allocated.
1024 free_irq(sis
->irq
, sis
);
1027 static int sis_chip_init(struct sis7019
*sis
)
1029 unsigned long io
= sis
->ioport
;
1030 void __iomem
*ioaddr
= sis
->ioaddr
;
1031 unsigned long timeout
;
1036 /* Reset the audio controller
1038 outl(SIS_GCR_SOFTWARE_RESET
, io
+ SIS_GCR
);
1040 outl(0, io
+ SIS_GCR
);
1042 /* Get the AC-link semaphore, and reset the codecs
1045 while ((inw(io
+ SIS_AC97_SEMA
) & SIS_AC97_SEMA_BUSY
) && --count
)
1051 outl(SIS_AC97_CMD_CODEC_COLD_RESET
, io
+ SIS_AC97_CMD
);
1055 while ((inw(io
+ SIS_AC97_STATUS
) & SIS_AC97_STATUS_BUSY
) && --count
)
1058 /* Command complete, we can let go of the semaphore now.
1060 outl(SIS_AC97_SEMA_RELEASE
, io
+ SIS_AC97_SEMA
);
1064 /* Now that we've finished the reset, find out what's attached.
1065 * There are some codec/board combinations that take an extremely
1066 * long time to come up. 350+ ms has been observed in the field,
1067 * so we'll give them up to 500ms.
1069 sis
->codecs_present
= 0;
1070 timeout
= msecs_to_jiffies(500) + jiffies
;
1071 while (time_before_eq(jiffies
, timeout
)) {
1072 status
= inl(io
+ SIS_AC97_STATUS
);
1073 if (status
& SIS_AC97_STATUS_CODEC_READY
)
1074 sis
->codecs_present
|= SIS_PRIMARY_CODEC_PRESENT
;
1075 if (status
& SIS_AC97_STATUS_CODEC2_READY
)
1076 sis
->codecs_present
|= SIS_SECONDARY_CODEC_PRESENT
;
1077 if (status
& SIS_AC97_STATUS_CODEC3_READY
)
1078 sis
->codecs_present
|= SIS_TERTIARY_CODEC_PRESENT
;
1080 if (sis
->codecs_present
== codecs
)
1086 /* All done, check for errors.
1088 if (!sis
->codecs_present
) {
1089 dev_err(&sis
->pci
->dev
, "could not find any codecs\n");
1093 if (sis
->codecs_present
!= codecs
) {
1094 dev_warn(&sis
->pci
->dev
, "missing codecs, found %0x, expected %0x\n",
1095 sis
->codecs_present
, codecs
);
1098 /* Let the hardware know that the audio driver is alive,
1099 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1100 * record channels. We're going to want to use Variable Rate Audio
1101 * for recording, to avoid needlessly resampling from 48kHZ.
1103 outl(SIS_AC97_CONF_AUDIO_ALIVE
, io
+ SIS_AC97_CONF
);
1104 outl(SIS_AC97_CONF_AUDIO_ALIVE
| SIS_AC97_CONF_PCM_LR_ENABLE
|
1105 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE
|
1106 SIS_AC97_CONF_PCM_CAP_LR_ENABLE
|
1107 SIS_AC97_CONF_CODEC_VRA_ENABLE
, io
+ SIS_AC97_CONF
);
1109 /* All AC97 PCM slots should be sourced from sub-mixer 0.
1111 outl(0, io
+ SIS_AC97_PSR
);
1113 /* There is only one valid DMA setup for a PCI environment.
1115 outl(SIS_DMA_CSR_PCI_SETTINGS
, io
+ SIS_DMA_CSR
);
1117 /* Reset the synchronization groups for all of the channels
1118 * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
1119 * we'll need to change how we handle these. Until then, we just
1120 * assign sub-mixer 0 to all playback channels, and avoid any
1121 * attenuation on the audio.
1123 outl(0, io
+ SIS_PLAY_SYNC_GROUP_A
);
1124 outl(0, io
+ SIS_PLAY_SYNC_GROUP_B
);
1125 outl(0, io
+ SIS_PLAY_SYNC_GROUP_C
);
1126 outl(0, io
+ SIS_PLAY_SYNC_GROUP_D
);
1127 outl(0, io
+ SIS_MIXER_SYNC_GROUP
);
1129 for (i
= 0; i
< 64; i
++) {
1130 writel(i
, SIS_MIXER_START_ADDR(ioaddr
, i
));
1131 writel(SIS_MIXER_RIGHT_NO_ATTEN
| SIS_MIXER_LEFT_NO_ATTEN
|
1132 SIS_MIXER_DEST_0
, SIS_MIXER_ADDR(ioaddr
, i
));
1135 /* Don't attenuate any audio set for the wave amplifier.
1137 * FIXME: Maximum attenuation is set for the music amp, which will
1138 * need to change if we start using the synth engine.
1140 outl(0xffff0000, io
+ SIS_WEVCR
);
1142 /* Ensure that the wave engine is in normal operating mode.
1144 outl(0, io
+ SIS_WECCR
);
1146 /* Go ahead and enable the DMA interrupts. They won't go live
1147 * until we start a channel.
1149 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE
|
1150 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE
, io
+ SIS_GIER
);
1155 #ifdef CONFIG_PM_SLEEP
1156 static int sis_suspend(struct device
*dev
)
1158 struct snd_card
*card
= dev_get_drvdata(dev
);
1159 struct sis7019
*sis
= card
->private_data
;
1160 void __iomem
*ioaddr
= sis
->ioaddr
;
1163 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1164 if (sis
->codecs_present
& SIS_PRIMARY_CODEC_PRESENT
)
1165 snd_ac97_suspend(sis
->ac97
[0]);
1166 if (sis
->codecs_present
& SIS_SECONDARY_CODEC_PRESENT
)
1167 snd_ac97_suspend(sis
->ac97
[1]);
1168 if (sis
->codecs_present
& SIS_TERTIARY_CODEC_PRESENT
)
1169 snd_ac97_suspend(sis
->ac97
[2]);
1171 /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1173 if (sis
->irq
>= 0) {
1174 free_irq(sis
->irq
, sis
);
1178 /* Save the internal state away
1180 for (i
= 0; i
< 4; i
++) {
1181 memcpy_fromio(sis
->suspend_state
[i
], ioaddr
, 4096);
1188 static int sis_resume(struct device
*dev
)
1190 struct pci_dev
*pci
= to_pci_dev(dev
);
1191 struct snd_card
*card
= dev_get_drvdata(dev
);
1192 struct sis7019
*sis
= card
->private_data
;
1193 void __iomem
*ioaddr
= sis
->ioaddr
;
1196 if (sis_chip_init(sis
)) {
1197 dev_err(&pci
->dev
, "unable to re-init controller\n");
1201 if (request_irq(pci
->irq
, sis_interrupt
, IRQF_SHARED
,
1202 KBUILD_MODNAME
, sis
)) {
1203 dev_err(&pci
->dev
, "unable to regain IRQ %d\n", pci
->irq
);
1207 /* Restore saved state, then clear out the page we use for the
1210 for (i
= 0; i
< 4; i
++) {
1211 memcpy_toio(ioaddr
, sis
->suspend_state
[i
], 4096);
1215 memset(sis
->suspend_state
[0], 0, 4096);
1217 sis
->irq
= pci
->irq
;
1219 if (sis
->codecs_present
& SIS_PRIMARY_CODEC_PRESENT
)
1220 snd_ac97_resume(sis
->ac97
[0]);
1221 if (sis
->codecs_present
& SIS_SECONDARY_CODEC_PRESENT
)
1222 snd_ac97_resume(sis
->ac97
[1]);
1223 if (sis
->codecs_present
& SIS_TERTIARY_CODEC_PRESENT
)
1224 snd_ac97_resume(sis
->ac97
[2]);
1226 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1230 snd_card_disconnect(card
);
1234 static SIMPLE_DEV_PM_OPS(sis_pm
, sis_suspend
, sis_resume
);
1235 #define SIS_PM_OPS &sis_pm
1237 #define SIS_PM_OPS NULL
1238 #endif /* CONFIG_PM_SLEEP */
1240 static int sis_alloc_suspend(struct sis7019
*sis
)
1244 /* We need 16K to store the internal wave engine state during a
1245 * suspend, but we don't need it to be contiguous, so play nice
1246 * with the memory system. We'll also use this area for a silence
1249 for (i
= 0; i
< SIS_SUSPEND_PAGES
; i
++) {
1250 sis
->suspend_state
[i
] = devm_kmalloc(&sis
->pci
->dev
, 4096,
1252 if (!sis
->suspend_state
[i
])
1255 memset(sis
->suspend_state
[0], 0, 4096);
1260 static int sis_chip_create(struct snd_card
*card
,
1261 struct pci_dev
*pci
)
1263 struct sis7019
*sis
= card
->private_data
;
1264 struct voice
*voice
;
1268 rc
= pcim_enable_device(pci
);
1272 rc
= dma_set_mask(&pci
->dev
, DMA_BIT_MASK(30));
1274 dev_err(&pci
->dev
, "architecture does not support 30-bit PCI busmaster DMA");
1278 mutex_init(&sis
->ac97_mutex
);
1279 spin_lock_init(&sis
->voice_lock
);
1283 sis
->ioport
= pci_resource_start(pci
, 0);
1285 rc
= pci_request_regions(pci
, "SiS7019");
1287 dev_err(&pci
->dev
, "unable request regions\n");
1291 sis
->ioaddr
= devm_ioremap(&pci
->dev
, pci_resource_start(pci
, 1), 0x4000);
1293 dev_err(&pci
->dev
, "unable to remap MMIO, aborting\n");
1297 rc
= sis_alloc_suspend(sis
);
1299 dev_err(&pci
->dev
, "unable to allocate state storage\n");
1303 rc
= sis_chip_init(sis
);
1306 card
->private_free
= sis_chip_free
;
1308 rc
= request_irq(pci
->irq
, sis_interrupt
, IRQF_SHARED
, KBUILD_MODNAME
,
1311 dev_err(&pci
->dev
, "unable to allocate irq %d\n", sis
->irq
);
1315 sis
->irq
= pci
->irq
;
1316 card
->sync_irq
= sis
->irq
;
1317 pci_set_master(pci
);
1319 for (i
= 0; i
< 64; i
++) {
1320 voice
= &sis
->voices
[i
];
1322 voice
->ctrl_base
= SIS_PLAY_DMA_ADDR(sis
->ioaddr
, i
);
1323 voice
->wave_base
= SIS_WAVE_ADDR(sis
->ioaddr
, i
);
1326 voice
= &sis
->capture_voice
;
1327 voice
->flags
= VOICE_CAPTURE
;
1328 voice
->num
= SIS_CAPTURE_CHAN_AC97_PCM_IN
;
1329 voice
->ctrl_base
= SIS_CAPTURE_DMA_ADDR(sis
->ioaddr
, voice
->num
);
1334 static int snd_sis7019_probe(struct pci_dev
*pci
,
1335 const struct pci_device_id
*pci_id
)
1337 struct snd_card
*card
;
1338 struct sis7019
*sis
;
1344 /* The user can specify which codecs should be present so that we
1345 * can wait for them to show up if they are slow to recover from
1346 * the AC97 cold reset. We default to a single codec, the primary.
1348 * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
1350 codecs
&= SIS_PRIMARY_CODEC_PRESENT
| SIS_SECONDARY_CODEC_PRESENT
|
1351 SIS_TERTIARY_CODEC_PRESENT
;
1353 codecs
= SIS_PRIMARY_CODEC_PRESENT
;
1355 rc
= snd_card_new(&pci
->dev
, index
, id
, THIS_MODULE
,
1356 sizeof(*sis
), &card
);
1360 strcpy(card
->driver
, "SiS7019");
1361 strcpy(card
->shortname
, "SiS7019");
1362 rc
= sis_chip_create(card
, pci
);
1366 sis
= card
->private_data
;
1368 rc
= sis_mixer_create(sis
);
1372 rc
= sis_pcm_create(sis
);
1376 snprintf(card
->longname
, sizeof(card
->longname
),
1377 "%s Audio Accelerator with %s at 0x%lx, irq %d",
1378 card
->shortname
, snd_ac97_get_short_name(sis
->ac97
[0]),
1379 sis
->ioport
, sis
->irq
);
1381 rc
= snd_card_register(card
);
1385 pci_set_drvdata(pci
, card
);
1389 static struct pci_driver sis7019_driver
= {
1390 .name
= KBUILD_MODNAME
,
1391 .id_table
= snd_sis7019_ids
,
1392 .probe
= snd_sis7019_probe
,
1398 module_pci_driver(sis7019_driver
);