1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/init.h>
25#include <linux/pci.h>
26#include <linux/time.h>
27#include <linux/slab.h>
28#include <linux/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>
34#include "sis7019.h"
35
36MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
37MODULE_DESCRIPTION("SiS7019");
38MODULE_LICENSE("GPL");
39MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
40
41static int index = SNDRV_DEFAULT_IDX1;
42static char *id = SNDRV_DEFAULT_STR1;
43static bool enable = 1;
44static int codecs = 1;
45
46module_param(index, int, 0444);
47MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
48module_param(id, charp, 0444);
49MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
50module_param(enable, bool, 0444);
51MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
52module_param(codecs, int, 0444);
53MODULE_PARM_DESC(codecs, "Set bit to indicate that codec number is expected to be present (default 1)");
54
55static const struct pci_device_id snd_sis7019_ids[] = {
56 { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
57 { 0, }
58};
59
60MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81struct voice {
82 u16 flags;
83#define VOICE_IN_USE 1
84#define VOICE_CAPTURE 2
85#define VOICE_SSO_TIMING 4
86#define VOICE_SYNC_TIMING 8
87 u16 sync_cso;
88 u16 period_size;
89 u16 buffer_size;
90 u16 sync_period_size;
91 u16 sync_buffer_size;
92 u32 sso;
93 u32 vperiod;
94 struct snd_pcm_substream *substream;
95 struct voice *timing;
96 void __iomem *ctrl_base;
97 void __iomem *wave_base;
98 void __iomem *sync_base;
99 int num;
100};
101
102
103
104
105
106#ifdef CONFIG_PM_SLEEP
107#define SIS_SUSPEND_PAGES 4
108#else
109#define SIS_SUSPEND_PAGES 1
110#endif
111
112struct sis7019 {
113 unsigned long ioport;
114 void __iomem *ioaddr;
115 int irq;
116 int codecs_present;
117
118 struct pci_dev *pci;
119 struct snd_pcm *pcm;
120 struct snd_card *card;
121 struct snd_ac97 *ac97[3];
122
123
124
125
126
127 struct mutex ac97_mutex;
128
129
130
131 spinlock_t voice_lock;
132
133 struct voice voices[64];
134 struct voice capture_voice;
135
136
137
138
139
140 void *suspend_state[SIS_SUSPEND_PAGES];
141
142 int silence_users;
143 dma_addr_t silence_dma_addr;
144};
145
146
147
148
149#define SIS_PRIMARY_CODEC_PRESENT 0x0001
150#define SIS_SECONDARY_CODEC_PRESENT 0x0002
151#define SIS_TERTIARY_CODEC_PRESENT 0x0004
152
153
154
155
156
157
158
159
160
161
162static const 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,
172 .rate_min = 4000,
173 .rate_max = 48000,
174 .channels_min = 1,
175 .channels_max = 2,
176 .buffer_bytes_max = (0xfff9 * 4),
177 .period_bytes_min = 9,
178 .period_bytes_max = (0xfff9 * 4),
179 .periods_min = 1,
180 .periods_max = (0xfff9 / 9),
181};
182
183static const 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,
193 .rate_min = 4000,
194 .rate_max = 48000,
195 .channels_min = 1,
196 .channels_max = 2,
197 .buffer_bytes_max = (0xfff9 * 4),
198 .period_bytes_min = 9,
199 .period_bytes_max = (0xfff9 * 4),
200 .periods_min = 1,
201 .periods_max = (0xfff9 / 9),
202};
203
204static void sis_update_sso(struct voice *voice, u16 period)
205{
206 void __iomem *base = voice->ctrl_base;
207
208 voice->sso += period;
209 if (voice->sso >= voice->buffer_size)
210 voice->sso -= voice->buffer_size;
211
212
213 if (voice->sso < 8)
214 voice->sso = 8;
215
216
217 writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
218}
219
220static void sis_update_voice(struct voice *voice)
221{
222 if (voice->flags & VOICE_SSO_TIMING) {
223 sis_update_sso(voice, voice->period_size);
224 } else if (voice->flags & VOICE_SYNC_TIMING) {
225 int sync;
226
227
228
229
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);
234 else
235 sis_update_sso(voice, voice->period_size);
236 return;
237 }
238
239
240
241
242
243
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;
248
249
250
251
252
253
254 if (sync > 0) {
255 if (sync < 16)
256 sync = 16;
257 sis_update_sso(voice, sync);
258 return;
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 if (sync > -9)
277 voice->vperiod = voice->sync_period_size + 1;
278 else
279 voice->vperiod = voice->sync_period_size + sync + 10;
280
281 if (voice->vperiod < voice->buffer_size) {
282 sis_update_sso(voice, voice->vperiod);
283 voice->vperiod = 0;
284 } else
285 sis_update_sso(voice, voice->period_size);
286
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;
291 }
292
293 snd_pcm_period_elapsed(voice->substream);
294}
295
296static void sis_voice_irq(u32 status, struct voice *voice)
297{
298 int bit;
299
300 while (status) {
301 bit = __ffs(status);
302 status >>= bit + 1;
303 voice += bit;
304 sis_update_voice(voice);
305 voice++;
306 }
307}
308
309static irqreturn_t sis_interrupt(int irq, void *dev)
310{
311 struct sis7019 *sis = dev;
312 unsigned long io = sis->ioport;
313 struct voice *voice;
314 u32 intr, status;
315
316
317
318
319
320
321
322 intr = inl(io + SIS_GISR);
323 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
324 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
325 if (!intr)
326 return IRQ_NONE;
327
328 do {
329 status = inl(io + SIS_PISR_A);
330 if (status) {
331 sis_voice_irq(status, sis->voices);
332 outl(status, io + SIS_PISR_A);
333 }
334
335 status = inl(io + SIS_PISR_B);
336 if (status) {
337 sis_voice_irq(status, &sis->voices[32]);
338 outl(status, io + SIS_PISR_B);
339 }
340
341 status = inl(io + SIS_RISR);
342 if (status) {
343 voice = &sis->capture_voice;
344 if (!voice->timing)
345 snd_pcm_period_elapsed(voice->substream);
346
347 outl(status, io + SIS_RISR);
348 }
349
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;
354 } while (intr);
355
356 return IRQ_HANDLED;
357}
358
359static u32 sis_rate_to_delta(unsigned int rate)
360{
361 u32 delta;
362
363
364
365
366
367
368
369
370
371 if (rate == 44100)
372 delta = 0xeb3;
373 else if (rate == 8000)
374 delta = 0x2ab;
375 else if (rate == 48000)
376 delta = 0x1000;
377 else
378 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
379 return delta;
380}
381
382static void __sis_map_silence(struct sis7019 *sis)
383{
384
385 if (!sis->silence_users)
386 sis->silence_dma_addr = dma_map_single(&sis->pci->dev,
387 sis->suspend_state[0],
388 4096, DMA_TO_DEVICE);
389 sis->silence_users++;
390}
391
392static void __sis_unmap_silence(struct sis7019 *sis)
393{
394
395 sis->silence_users--;
396 if (!sis->silence_users)
397 dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
398 DMA_TO_DEVICE);
399}
400
401static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
402{
403 unsigned long flags;
404
405 spin_lock_irqsave(&sis->voice_lock, flags);
406 if (voice->timing) {
407 __sis_unmap_silence(sis);
408 voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
409 VOICE_SYNC_TIMING);
410 voice->timing = NULL;
411 }
412 voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
413 spin_unlock_irqrestore(&sis->voice_lock, flags);
414}
415
416static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
417{
418
419 struct voice *voice;
420 int i;
421
422 for (i = 0; i < 64; i++) {
423 voice = &sis->voices[i];
424 if (voice->flags & VOICE_IN_USE)
425 continue;
426 voice->flags |= VOICE_IN_USE;
427 goto found_one;
428 }
429 voice = NULL;
430
431found_one:
432 return voice;
433}
434
435static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
436{
437 struct voice *voice;
438 unsigned long flags;
439
440 spin_lock_irqsave(&sis->voice_lock, flags);
441 voice = __sis_alloc_playback_voice(sis);
442 spin_unlock_irqrestore(&sis->voice_lock, flags);
443
444 return voice;
445}
446
447static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *hw_params)
449{
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;
454 unsigned long flags;
455 int needed;
456
457
458
459
460
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));
465
466 if (needed && !voice->timing) {
467 spin_lock_irqsave(&sis->voice_lock, flags);
468 voice->timing = __sis_alloc_playback_voice(sis);
469 if (voice->timing)
470 __sis_map_silence(sis);
471 spin_unlock_irqrestore(&sis->voice_lock, flags);
472 if (!voice->timing)
473 return -ENOMEM;
474 voice->timing->substream = substream;
475 } else if (!needed && voice->timing) {
476 sis_free_voice(sis, voice);
477 voice->timing = NULL;
478 }
479
480 return 0;
481}
482
483static int sis_playback_open(struct snd_pcm_substream *substream)
484{
485 struct sis7019 *sis = snd_pcm_substream_chip(substream);
486 struct snd_pcm_runtime *runtime = substream->runtime;
487 struct voice *voice;
488
489 voice = sis_alloc_playback_voice(sis);
490 if (!voice)
491 return -EAGAIN;
492
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,
497 9, 0xfff9);
498 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
499 9, 0xfff9);
500 snd_pcm_set_sync(substream);
501 return 0;
502}
503
504static int sis_substream_close(struct snd_pcm_substream *substream)
505{
506 struct sis7019 *sis = snd_pcm_substream_chip(substream);
507 struct snd_pcm_runtime *runtime = substream->runtime;
508 struct voice *voice = runtime->private_data;
509
510 sis_free_voice(sis, voice);
511 return 0;
512}
513
514static int sis_playback_hw_params(struct snd_pcm_substream *substream,
515 struct snd_pcm_hw_params *hw_params)
516{
517 return snd_pcm_lib_malloc_pages(substream,
518 params_buffer_bytes(hw_params));
519}
520
521static int sis_hw_free(struct snd_pcm_substream *substream)
522{
523 return snd_pcm_lib_free_pages(substream);
524}
525
526static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
527{
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;
533 u16 leo;
534
535
536
537
538 format = 0;
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;
545
546
547
548
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;
552 sso_eso = leo;
553
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;
561
562 control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
563 control |= SIS_PLAY_DMA_INTR_AT_SSO;
564 sso_eso |= (runtime->period_size - 1) << 16;
565 }
566
567 delta = sis_rate_to_delta(runtime->rate);
568
569
570
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);
575
576 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
577 writel(0, wave_base + reg);
578
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);
585
586
587 readl(ctrl_base);
588
589 return 0;
590}
591
592static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
593{
594 struct sis7019 *sis = snd_pcm_substream_chip(substream);
595 unsigned long io = sis->ioport;
596 struct snd_pcm_substream *s;
597 struct voice *voice;
598 void *chip;
599 int starting;
600 u32 record = 0;
601 u32 play[2] = { 0, 0 };
602
603
604
605
606
607 switch (cmd) {
608 case SNDRV_PCM_TRIGGER_START:
609 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
610 case SNDRV_PCM_TRIGGER_RESUME:
611 starting = 1;
612 break;
613 case SNDRV_PCM_TRIGGER_STOP:
614 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
615 case SNDRV_PCM_TRIGGER_SUSPEND:
616 starting = 0;
617 break;
618 default:
619 return -EINVAL;
620 }
621
622 snd_pcm_group_for_each_entry(s, substream) {
623
624 chip = snd_pcm_substream_chip(s);
625 if (chip != sis)
626 continue;
627
628 voice = s->runtime->private_data;
629 if (voice->flags & VOICE_CAPTURE) {
630 record |= 1 << voice->num;
631 voice = voice->timing;
632 }
633
634
635
636
637 if (voice)
638 play[voice->num / 32] |= 1 << (voice->num & 0x1f);
639
640 snd_pcm_trigger_done(s, substream);
641 }
642
643 if (starting) {
644 if (record)
645 outl(record, io + SIS_RECORD_START_REG);
646 if (play[0])
647 outl(play[0], io + SIS_PLAY_START_A_REG);
648 if (play[1])
649 outl(play[1], io + SIS_PLAY_START_B_REG);
650 } else {
651 if (record)
652 outl(record, io + SIS_RECORD_STOP_REG);
653 if (play[0])
654 outl(play[0], io + SIS_PLAY_STOP_A_REG);
655 if (play[1])
656 outl(play[1], io + SIS_PLAY_STOP_B_REG);
657 }
658 return 0;
659}
660
661static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
662{
663 struct snd_pcm_runtime *runtime = substream->runtime;
664 struct voice *voice = runtime->private_data;
665 u32 cso;
666
667 cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
668 cso &= 0xffff;
669 return cso;
670}
671
672static int sis_capture_open(struct snd_pcm_substream *substream)
673{
674 struct sis7019 *sis = snd_pcm_substream_chip(substream);
675 struct snd_pcm_runtime *runtime = substream->runtime;
676 struct voice *voice = &sis->capture_voice;
677 unsigned long flags;
678
679
680
681
682 spin_lock_irqsave(&sis->voice_lock, flags);
683 if (voice->flags & VOICE_IN_USE)
684 voice = NULL;
685 else
686 voice->flags |= VOICE_IN_USE;
687 spin_unlock_irqrestore(&sis->voice_lock, flags);
688
689 if (!voice)
690 return -EAGAIN;
691
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,
698 9, 0xfff9);
699 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
700 9, 0xfff9);
701 snd_pcm_set_sync(substream);
702 return 0;
703}
704
705static int sis_capture_hw_params(struct snd_pcm_substream *substream,
706 struct snd_pcm_hw_params *hw_params)
707{
708 struct sis7019 *sis = snd_pcm_substream_chip(substream);
709 int rc;
710
711 rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
712 params_rate(hw_params));
713 if (rc)
714 goto out;
715
716 rc = snd_pcm_lib_malloc_pages(substream,
717 params_buffer_bytes(hw_params));
718 if (rc < 0)
719 goto out;
720
721 rc = sis_alloc_timing_voice(substream, hw_params);
722
723out:
724 return rc;
725}
726
727static void sis_prepare_timing_voice(struct voice *voice,
728 struct snd_pcm_substream *substream)
729{
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;
738
739
740
741
742 buffer_size = 4096 / runtime->channels;
743 buffer_size /= snd_pcm_format_size(runtime->format, 1);
744 period_size = buffer_size;
745
746
747
748
749
750
751
752
753
754
755
756
757
758 vperiod = runtime->period_size + 12;
759 if (vperiod > period_size) {
760 u16 tail = vperiod % period_size;
761 u16 quarter_period = period_size / 4;
762
763 if (tail && tail < quarter_period) {
764 u16 loops = vperiod / period_size;
765
766 tail = quarter_period - tail;
767 tail += loops - 1;
768 tail /= loops;
769 period_size -= tail;
770 }
771
772 sso = period_size - 1;
773 } else {
774
775
776
777 period_size = runtime->period_size;
778 sso = vperiod - 1;
779 vperiod = 0;
780 }
781
782
783
784
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;
792 timing->sso = sso;
793 timing->vperiod = vperiod;
794
795
796
797
798
799 format = 0;
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;
804
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;
809
810 delta = sis_rate_to_delta(runtime->rate);
811
812
813
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);
818
819 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
820 writel(0, wave_base + reg);
821
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);
828}
829
830static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
831{
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;
836 u16 leo;
837
838
839
840
841 format = 0;
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;
848
849 dma_addr = runtime->dma_addr;
850 leo = runtime->buffer_size - 1;
851 control = leo | SIS_CAPTURE_DMA_LOOP;
852
853
854
855
856
857 if (voice->timing) {
858 sis_prepare_timing_voice(voice, substream);
859 } else {
860 control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
861 if (runtime->period_size != runtime->buffer_size)
862 control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
863 }
864
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);
868
869
870 readl(rec_base);
871
872 return 0;
873}
874
875static const 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,
884};
885
886static const 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,
895};
896
897static int sis_pcm_create(struct sis7019 *sis)
898{
899 struct snd_pcm *pcm;
900 int rc;
901
902
903
904
905 rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
906 if (rc)
907 return rc;
908
909 pcm->private_data = sis;
910 strcpy(pcm->name, "SiS7019");
911 sis->pcm = pcm;
912
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);
915
916
917
918
919 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
920 snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
921
922 return 0;
923}
924
925static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
926{
927 unsigned long io = sis->ioport;
928 unsigned short val = 0xffff;
929 u16 status;
930 u16 rdy;
931 int count;
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,
936 };
937
938 rdy = codec_ready[codec];
939
940
941
942
943
944 mutex_lock(&sis->ac97_mutex);
945
946 count = 0xffff;
947 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
948 udelay(1);
949
950 if (!count)
951 goto timeout;
952
953
954
955 count = 0xffff;
956 do {
957 status = inw(io + SIS_AC97_STATUS);
958 if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
959 break;
960
961 udelay(1);
962 } while (--count);
963
964 if (!count)
965 goto timeout_sema;
966
967
968
969 outl(cmd, io + SIS_AC97_CMD);
970 udelay(10);
971
972 count = 0xffff;
973 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
974 udelay(1);
975
976
977
978 val = inl(io + SIS_AC97_CMD) >> 16;
979
980timeout_sema:
981 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
982timeout:
983 mutex_unlock(&sis->ac97_mutex);
984
985 if (!count) {
986 dev_err(&sis->pci->dev, "ac97 codec %d timeout cmd 0x%08x\n",
987 codec, cmd);
988 }
989
990 return val;
991}
992
993static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
994 unsigned short val)
995{
996 static const u32 cmd[3] = {
997 SIS_AC97_CMD_CODEC_WRITE,
998 SIS_AC97_CMD_CODEC2_WRITE,
999 SIS_AC97_CMD_CODEC3_WRITE,
1000 };
1001 sis_ac97_rw(ac97->private_data, ac97->num,
1002 (val << 16) | (reg << 8) | cmd[ac97->num]);
1003}
1004
1005static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
1006{
1007 static const u32 cmd[3] = {
1008 SIS_AC97_CMD_CODEC_READ,
1009 SIS_AC97_CMD_CODEC2_READ,
1010 SIS_AC97_CMD_CODEC3_READ,
1011 };
1012 return sis_ac97_rw(ac97->private_data, ac97->num,
1013 (reg << 8) | cmd[ac97->num]);
1014}
1015
1016static int sis_mixer_create(struct sis7019 *sis)
1017{
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,
1023 };
1024 int rc;
1025
1026 memset(&ac97, 0, sizeof(ac97));
1027 ac97.private_data = sis;
1028
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]);
1032 ac97.num = 1;
1033 if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1034 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1035 ac97.num = 2;
1036 if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1037 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1038
1039
1040
1041
1042 return rc;
1043}
1044
1045static void sis_free_suspend(struct sis7019 *sis)
1046{
1047 int i;
1048
1049 for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1050 kfree(sis->suspend_state[i]);
1051}
1052
1053static int sis_chip_free(struct sis7019 *sis)
1054{
1055
1056
1057 outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1058 udelay(25);
1059 outl(0, sis->ioport + SIS_GCR);
1060 outl(0, sis->ioport + SIS_GIER);
1061
1062
1063
1064 if (sis->irq >= 0)
1065 free_irq(sis->irq, sis);
1066
1067 iounmap(sis->ioaddr);
1068 pci_release_regions(sis->pci);
1069 pci_disable_device(sis->pci);
1070 sis_free_suspend(sis);
1071 return 0;
1072}
1073
1074static int sis_dev_free(struct snd_device *dev)
1075{
1076 struct sis7019 *sis = dev->device_data;
1077 return sis_chip_free(sis);
1078}
1079
1080static int sis_chip_init(struct sis7019 *sis)
1081{
1082 unsigned long io = sis->ioport;
1083 void __iomem *ioaddr = sis->ioaddr;
1084 unsigned long timeout;
1085 u16 status;
1086 int count;
1087 int i;
1088
1089
1090
1091 outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1092 udelay(25);
1093 outl(0, io + SIS_GCR);
1094
1095
1096
1097 count = 0xffff;
1098 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1099 udelay(1);
1100
1101 if (!count)
1102 return -EIO;
1103
1104 outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1105 udelay(250);
1106
1107 count = 0xffff;
1108 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1109 udelay(1);
1110
1111
1112
1113 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1114 if (!count)
1115 return -EIO;
1116
1117
1118
1119
1120
1121
1122 sis->codecs_present = 0;
1123 timeout = msecs_to_jiffies(500) + jiffies;
1124 while (time_before_eq(jiffies, timeout)) {
1125 status = inl(io + SIS_AC97_STATUS);
1126 if (status & SIS_AC97_STATUS_CODEC_READY)
1127 sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1128 if (status & SIS_AC97_STATUS_CODEC2_READY)
1129 sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1130 if (status & SIS_AC97_STATUS_CODEC3_READY)
1131 sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1132
1133 if (sis->codecs_present == codecs)
1134 break;
1135
1136 msleep(1);
1137 }
1138
1139
1140
1141 if (!sis->codecs_present) {
1142 dev_err(&sis->pci->dev, "could not find any codecs\n");
1143 return -EIO;
1144 }
1145
1146 if (sis->codecs_present != codecs) {
1147 dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
1148 sis->codecs_present, codecs);
1149 }
1150
1151
1152
1153
1154
1155
1156 outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1157 outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1158 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1159 SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1160 SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1161
1162
1163
1164 outl(0, io + SIS_AC97_PSR);
1165
1166
1167
1168 outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1169
1170
1171
1172
1173
1174
1175
1176 outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1177 outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1178 outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1179 outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1180 outl(0, io + SIS_MIXER_SYNC_GROUP);
1181
1182 for (i = 0; i < 64; i++) {
1183 writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1184 writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1185 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1186 }
1187
1188
1189
1190
1191
1192
1193 outl(0xffff0000, io + SIS_WEVCR);
1194
1195
1196
1197 outl(0, io + SIS_WECCR);
1198
1199
1200
1201
1202 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1203 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1204
1205 return 0;
1206}
1207
1208#ifdef CONFIG_PM_SLEEP
1209static int sis_suspend(struct device *dev)
1210{
1211 struct snd_card *card = dev_get_drvdata(dev);
1212 struct sis7019 *sis = card->private_data;
1213 void __iomem *ioaddr = sis->ioaddr;
1214 int i;
1215
1216 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1217 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1218 snd_ac97_suspend(sis->ac97[0]);
1219 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1220 snd_ac97_suspend(sis->ac97[1]);
1221 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1222 snd_ac97_suspend(sis->ac97[2]);
1223
1224
1225
1226 if (sis->irq >= 0) {
1227 free_irq(sis->irq, sis);
1228 sis->irq = -1;
1229 }
1230
1231
1232
1233 for (i = 0; i < 4; i++) {
1234 memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1235 ioaddr += 4096;
1236 }
1237
1238 return 0;
1239}
1240
1241static int sis_resume(struct device *dev)
1242{
1243 struct pci_dev *pci = to_pci_dev(dev);
1244 struct snd_card *card = dev_get_drvdata(dev);
1245 struct sis7019 *sis = card->private_data;
1246 void __iomem *ioaddr = sis->ioaddr;
1247 int i;
1248
1249 if (sis_chip_init(sis)) {
1250 dev_err(&pci->dev, "unable to re-init controller\n");
1251 goto error;
1252 }
1253
1254 if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
1255 KBUILD_MODNAME, sis)) {
1256 dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
1257 goto error;
1258 }
1259
1260
1261
1262
1263 for (i = 0; i < 4; i++) {
1264 memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1265 ioaddr += 4096;
1266 }
1267
1268 memset(sis->suspend_state[0], 0, 4096);
1269
1270 sis->irq = pci->irq;
1271
1272 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1273 snd_ac97_resume(sis->ac97[0]);
1274 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1275 snd_ac97_resume(sis->ac97[1]);
1276 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1277 snd_ac97_resume(sis->ac97[2]);
1278
1279 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1280 return 0;
1281
1282error:
1283 snd_card_disconnect(card);
1284 return -EIO;
1285}
1286
1287static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
1288#define SIS_PM_OPS &sis_pm
1289#else
1290#define SIS_PM_OPS NULL
1291#endif
1292
1293static int sis_alloc_suspend(struct sis7019 *sis)
1294{
1295 int i;
1296
1297
1298
1299
1300
1301
1302 for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1303 sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1304 if (!sis->suspend_state[i])
1305 return -ENOMEM;
1306 }
1307 memset(sis->suspend_state[0], 0, 4096);
1308
1309 return 0;
1310}
1311
1312static int sis_chip_create(struct snd_card *card,
1313 struct pci_dev *pci)
1314{
1315 struct sis7019 *sis = card->private_data;
1316 struct voice *voice;
1317 static struct snd_device_ops ops = {
1318 .dev_free = sis_dev_free,
1319 };
1320 int rc;
1321 int i;
1322
1323 rc = pci_enable_device(pci);
1324 if (rc)
1325 goto error_out;
1326
1327 rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
1328 if (rc < 0) {
1329 dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
1330 goto error_out_enabled;
1331 }
1332
1333 memset(sis, 0, sizeof(*sis));
1334 mutex_init(&sis->ac97_mutex);
1335 spin_lock_init(&sis->voice_lock);
1336 sis->card = card;
1337 sis->pci = pci;
1338 sis->irq = -1;
1339 sis->ioport = pci_resource_start(pci, 0);
1340
1341 rc = pci_request_regions(pci, "SiS7019");
1342 if (rc) {
1343 dev_err(&pci->dev, "unable request regions\n");
1344 goto error_out_enabled;
1345 }
1346
1347 rc = -EIO;
1348 sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1349 if (!sis->ioaddr) {
1350 dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
1351 goto error_out_cleanup;
1352 }
1353
1354 rc = sis_alloc_suspend(sis);
1355 if (rc < 0) {
1356 dev_err(&pci->dev, "unable to allocate state storage\n");
1357 goto error_out_cleanup;
1358 }
1359
1360 rc = sis_chip_init(sis);
1361 if (rc)
1362 goto error_out_cleanup;
1363
1364 rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
1365 sis);
1366 if (rc) {
1367 dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
1368 goto error_out_cleanup;
1369 }
1370
1371 sis->irq = pci->irq;
1372 pci_set_master(pci);
1373
1374 for (i = 0; i < 64; i++) {
1375 voice = &sis->voices[i];
1376 voice->num = i;
1377 voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1378 voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1379 }
1380
1381 voice = &sis->capture_voice;
1382 voice->flags = VOICE_CAPTURE;
1383 voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1384 voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1385
1386 rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1387 if (rc)
1388 goto error_out_cleanup;
1389
1390 return 0;
1391
1392error_out_cleanup:
1393 sis_chip_free(sis);
1394
1395error_out_enabled:
1396 pci_disable_device(pci);
1397
1398error_out:
1399 return rc;
1400}
1401
1402static int snd_sis7019_probe(struct pci_dev *pci,
1403 const struct pci_device_id *pci_id)
1404{
1405 struct snd_card *card;
1406 struct sis7019 *sis;
1407 int rc;
1408
1409 rc = -ENOENT;
1410 if (!enable)
1411 goto error_out;
1412
1413
1414
1415
1416
1417
1418
1419 codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
1420 SIS_TERTIARY_CODEC_PRESENT;
1421 if (!codecs)
1422 codecs = SIS_PRIMARY_CODEC_PRESENT;
1423
1424 rc = snd_card_new(&pci->dev, index, id, THIS_MODULE,
1425 sizeof(*sis), &card);
1426 if (rc < 0)
1427 goto error_out;
1428
1429 strcpy(card->driver, "SiS7019");
1430 strcpy(card->shortname, "SiS7019");
1431 rc = sis_chip_create(card, pci);
1432 if (rc)
1433 goto card_error_out;
1434
1435 sis = card->private_data;
1436
1437 rc = sis_mixer_create(sis);
1438 if (rc)
1439 goto card_error_out;
1440
1441 rc = sis_pcm_create(sis);
1442 if (rc)
1443 goto card_error_out;
1444
1445 snprintf(card->longname, sizeof(card->longname),
1446 "%s Audio Accelerator with %s at 0x%lx, irq %d",
1447 card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1448 sis->ioport, sis->irq);
1449
1450 rc = snd_card_register(card);
1451 if (rc)
1452 goto card_error_out;
1453
1454 pci_set_drvdata(pci, card);
1455 return 0;
1456
1457card_error_out:
1458 snd_card_free(card);
1459
1460error_out:
1461 return rc;
1462}
1463
1464static void snd_sis7019_remove(struct pci_dev *pci)
1465{
1466 snd_card_free(pci_get_drvdata(pci));
1467}
1468
1469static struct pci_driver sis7019_driver = {
1470 .name = KBUILD_MODNAME,
1471 .id_table = snd_sis7019_ids,
1472 .probe = snd_sis7019_probe,
1473 .remove = snd_sis7019_remove,
1474 .driver = {
1475 .pm = SIS_PM_OPS,
1476 },
1477};
1478
1479module_pci_driver(sis7019_driver);
1480