forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ali5451.c
2378 lines (1987 loc) · 58 KB
/
ali5451.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Matt Wu <[email protected]>
* Apr 26, 2001
* Routines for control of ALi pci audio M5451
*
* BUGS:
* --
*
* TODO:
* --
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public Lcodecnse as published by
* the Free Software Foundation; either version 2 of the Lcodecnse, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public Lcodecnse for more details.
*
* You should have received a copy of the GNU General Public Lcodecnse
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/info.h>
#include <sound/ac97_codec.h>
#include <sound/mpu401.h>
#include <sound/initval.h>
MODULE_AUTHOR("Matt Wu <[email protected]>");
MODULE_DESCRIPTION("ALI M5451");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
static int index = SNDRV_DEFAULT_IDX1; /* Index */
static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
static int pcm_channels = 32;
static int spdif;
module_param(index, int, 0444);
MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
module_param(id, charp, 0444);
MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
module_param(pcm_channels, int, 0444);
MODULE_PARM_DESC(pcm_channels, "PCM Channels");
module_param(spdif, bool, 0444);
MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
/* just for backward compatibility */
static int enable;
module_param(enable, bool, 0444);
/*
* Debug part definitions
*/
/* #define ALI_DEBUG */
#ifdef ALI_DEBUG
#define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
#else
#define snd_ali_printk(format, args...)
#endif
/*
* Constants definition
*/
#define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
#define ALI_CHANNELS 32
#define ALI_PCM_IN_CHANNEL 31
#define ALI_SPDIF_IN_CHANNEL 19
#define ALI_SPDIF_OUT_CHANNEL 15
#define ALI_CENTER_CHANNEL 24
#define ALI_LEF_CHANNEL 23
#define ALI_SURR_LEFT_CHANNEL 26
#define ALI_SURR_RIGHT_CHANNEL 25
#define ALI_MODEM_IN_CHANNEL 21
#define ALI_MODEM_OUT_CHANNEL 20
#define SNDRV_ALI_VOICE_TYPE_PCM 01
#define SNDRV_ALI_VOICE_TYPE_OTH 02
#define ALI_5451_V02 0x02
/*
* Direct Registers
*/
#define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
#define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
#define ALI_LEGACY_DMAR11 0x0b /* MOD */
#define ALI_LEGACY_DMAR15 0x0f /* MMR */
#define ALI_MPUR0 0x20
#define ALI_MPUR1 0x21
#define ALI_MPUR2 0x22
#define ALI_MPUR3 0x23
#define ALI_AC97_WRITE 0x40
#define ALI_AC97_READ 0x44
#define ALI_SCTRL 0x48
#define ALI_SPDIF_OUT_ENABLE 0x20
#define ALI_SCTRL_LINE_IN2 (1 << 9)
#define ALI_SCTRL_GPIO_IN2 (1 << 13)
#define ALI_SCTRL_LINE_OUT_EN (1 << 20)
#define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
#define ALI_SCTRL_CODEC1_READY (1 << 24)
#define ALI_SCTRL_CODEC2_READY (1 << 25)
#define ALI_AC97_GPIO 0x4c
#define ALI_AC97_GPIO_ENABLE 0x8000
#define ALI_AC97_GPIO_DATA_SHIFT 16
#define ALI_SPDIF_CS 0x70
#define ALI_SPDIF_CTRL 0x74
#define ALI_SPDIF_IN_FUNC_ENABLE 0x02
#define ALI_SPDIF_IN_CH_STATUS 0x40
#define ALI_SPDIF_OUT_CH_STATUS 0xbf
#define ALI_START 0x80
#define ALI_STOP 0x84
#define ALI_CSPF 0x90
#define ALI_AINT 0x98
#define ALI_GC_CIR 0xa0
#define ENDLP_IE 0x00001000
#define MIDLP_IE 0x00002000
#define ALI_AINTEN 0xa4
#define ALI_VOLUME 0xa8
#define ALI_SBDELTA_DELTA_R 0xac
#define ALI_MISCINT 0xb0
#define ADDRESS_IRQ 0x00000020
#define TARGET_REACHED 0x00008000
#define MIXER_OVERFLOW 0x00000800
#define MIXER_UNDERFLOW 0x00000400
#define GPIO_IRQ 0x01000000
#define ALI_SBBL_SBCL 0xc0
#define ALI_SBCTRL_SBE2R_SBDD 0xc4
#define ALI_STIMER 0xc8
#define ALI_GLOBAL_CONTROL 0xd4
#define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
#define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
#define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
#define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
#define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
#define ALI_CSO_ALPHA_FMS 0xe0
#define ALI_LBA 0xe4
#define ALI_ESO_DELTA 0xe8
#define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
#define ALI_EBUF1 0xf4
#define ALI_EBUF2 0xf8
#define ALI_REG(codec, x) ((codec)->port + x)
#define MAX_CODECS 2
struct snd_ali;
struct snd_ali_voice;
struct snd_ali_channel_control {
/* register data */
struct REGDATA {
unsigned int start;
unsigned int stop;
unsigned int aint;
unsigned int ainten;
} data;
/* register addresses */
struct REGS {
unsigned int start;
unsigned int stop;
unsigned int aint;
unsigned int ainten;
unsigned int ac97read;
unsigned int ac97write;
} regs;
};
struct snd_ali_voice {
unsigned int number;
unsigned int use :1,
pcm :1,
midi :1,
mode :1,
synth :1,
running :1;
/* PCM data */
struct snd_ali *codec;
struct snd_pcm_substream *substream;
struct snd_ali_voice *extra;
int eso; /* final ESO value for channel */
int count; /* runtime->period_size */
/* --- */
void *private_data;
void (*private_free)(void *private_data);
};
struct snd_alidev {
struct snd_ali_voice voices[ALI_CHANNELS];
unsigned int chcnt; /* num of opened channels */
unsigned int chmap; /* bitmap for opened channels */
unsigned int synthcount;
};
#define ALI_GLOBAL_REGS 56
#define ALI_CHANNEL_REGS 8
struct snd_ali_image {
u32 regs[ALI_GLOBAL_REGS];
u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
};
struct snd_ali {
int irq;
unsigned long port;
unsigned char revision;
unsigned int hw_initialized :1;
unsigned int spdif_support :1;
struct pci_dev *pci;
struct pci_dev *pci_m1533;
struct pci_dev *pci_m7101;
struct snd_card *card;
struct snd_pcm *pcm[MAX_CODECS];
struct snd_alidev synth;
struct snd_ali_channel_control chregs;
/* S/PDIF Mask */
unsigned int spdif_mask;
unsigned int spurious_irq_count;
unsigned int spurious_irq_max_delta;
unsigned int num_of_codecs;
struct snd_ac97_bus *ac97_bus;
struct snd_ac97 *ac97[MAX_CODECS];
unsigned short ac97_ext_id;
unsigned short ac97_ext_status;
spinlock_t reg_lock;
spinlock_t voice_alloc;
#ifdef CONFIG_PM
struct snd_ali_image *image;
#endif
};
static struct pci_device_id snd_ali_ids[] = {
{PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
{0, }
};
MODULE_DEVICE_TABLE(pci, snd_ali_ids);
static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
unsigned short);
/*
* AC97 ACCESS
*/
static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
unsigned int port)
{
return (unsigned int)inl(ALI_REG(codec, port));
}
static inline void snd_ali_5451_poke(struct snd_ali *codec,
unsigned int port,
unsigned int val)
{
outl((unsigned int)val, ALI_REG(codec, port));
}
static int snd_ali_codec_ready(struct snd_ali *codec,
unsigned int port)
{
unsigned long end_time;
unsigned int res;
end_time = jiffies + msecs_to_jiffies(250);
do {
res = snd_ali_5451_peek(codec,port);
if (!(res & 0x8000))
return 0;
schedule_timeout_uninterruptible(1);
} while (time_after_eq(end_time, jiffies));
snd_ali_5451_poke(codec, port, res & ~0x8000);
snd_printdd("ali_codec_ready: codec is not ready.\n ");
return -EIO;
}
static int snd_ali_stimer_ready(struct snd_ali *codec)
{
unsigned long end_time;
unsigned long dwChk1,dwChk2;
dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
end_time = jiffies + msecs_to_jiffies(250);
do {
dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
if (dwChk2 != dwChk1)
return 0;
schedule_timeout_uninterruptible(1);
} while (time_after_eq(end_time, jiffies));
snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
return -EIO;
}
static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
unsigned short reg,
unsigned short val)
{
unsigned int dwVal;
unsigned int port;
if (reg >= 0x80) {
snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
return;
}
port = codec->chregs.regs.ac97write;
if (snd_ali_codec_ready(codec, port) < 0)
return;
if (snd_ali_stimer_ready(codec) < 0)
return;
dwVal = (unsigned int) (reg & 0xff);
dwVal |= 0x8000 | (val << 16);
if (secondary)
dwVal |= 0x0080;
if (codec->revision == ALI_5451_V02)
dwVal |= 0x0100;
snd_ali_5451_poke(codec, port, dwVal);
return ;
}
static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
int secondary,
unsigned short reg)
{
unsigned int dwVal;
unsigned int port;
if (reg >= 0x80) {
snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
return ~0;
}
port = codec->chregs.regs.ac97read;
if (snd_ali_codec_ready(codec, port) < 0)
return ~0;
if (snd_ali_stimer_ready(codec) < 0)
return ~0;
dwVal = (unsigned int) (reg & 0xff);
dwVal |= 0x8000; /* bit 15*/
if (secondary)
dwVal |= 0x0080;
snd_ali_5451_poke(codec, port, dwVal);
if (snd_ali_stimer_ready(codec) < 0)
return ~0;
if (snd_ali_codec_ready(codec, port) < 0)
return ~0;
return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
}
static void snd_ali_codec_write(struct snd_ac97 *ac97,
unsigned short reg,
unsigned short val )
{
struct snd_ali *codec = ac97->private_data;
snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
if (reg == AC97_GPIO_STATUS) {
outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
ALI_REG(codec, ALI_AC97_GPIO));
return;
}
snd_ali_codec_poke(codec, ac97->num, reg, val);
return ;
}
static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
unsigned short reg)
{
struct snd_ali *codec = ac97->private_data;
snd_ali_printk("codec_read reg=%xh.\n", reg);
return snd_ali_codec_peek(codec, ac97->num, reg);
}
/*
* AC97 Reset
*/
static int snd_ali_reset_5451(struct snd_ali *codec)
{
struct pci_dev *pci_dev;
unsigned short wCount, wReg;
unsigned int dwVal;
pci_dev = codec->pci_m1533;
if (pci_dev) {
pci_read_config_dword(pci_dev, 0x7c, &dwVal);
pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
udelay(5000);
pci_read_config_dword(pci_dev, 0x7c, &dwVal);
pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
udelay(5000);
}
pci_dev = codec->pci;
pci_read_config_dword(pci_dev, 0x44, &dwVal);
pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
udelay(500);
pci_read_config_dword(pci_dev, 0x44, &dwVal);
pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
udelay(5000);
wCount = 200;
while(wCount--) {
wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
if ((wReg & 0x000f) == 0x000f)
return 0;
udelay(5000);
}
/* non-fatal if you have a non PM capable codec */
/* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
return 0;
}
#ifdef CODEC_RESET
static int snd_ali_reset_codec(struct snd_ali *codec)
{
struct pci_dev *pci_dev;
unsigned char bVal;
unsigned int dwVal;
unsigned short wCount, wReg;
pci_dev = codec->pci_m1533;
pci_read_config_dword(pci_dev, 0x7c, &dwVal);
pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
udelay(5000);
pci_read_config_dword(pci_dev, 0x7c, &dwVal);
pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
udelay(5000);
bVal = inb(ALI_REG(codec,ALI_SCTRL));
bVal |= 0x02;
outb(ALI_REG(codec,ALI_SCTRL),bVal);
udelay(5000);
bVal = inb(ALI_REG(codec,ALI_SCTRL));
bVal &= 0xfd;
outb(ALI_REG(codec,ALI_SCTRL),bVal);
udelay(15000);
wCount = 200;
while (wCount--) {
wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
if ((wReg & 0x000f) == 0x000f)
return 0;
udelay(5000);
}
return -1;
}
#endif
/*
* ALI 5451 Controller
*/
static void snd_ali_enable_special_channel(struct snd_ali *codec,
unsigned int channel)
{
unsigned long dwVal;
dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
dwVal |= 1 << (channel & 0x0000001f);
outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
}
static void snd_ali_disable_special_channel(struct snd_ali *codec,
unsigned int channel)
{
unsigned long dwVal;
dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
dwVal &= ~(1 << (channel & 0x0000001f));
outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
}
static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
{
unsigned int gc;
gc = inl(ALI_REG(codec, ALI_GC_CIR));
gc |= ENDLP_IE;
gc |= MIDLP_IE;
outl( gc, ALI_REG(codec, ALI_GC_CIR));
}
static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
{
unsigned int gc;
gc = inl(ALI_REG(codec, ALI_GC_CIR));
gc &= ~ENDLP_IE;
gc &= ~MIDLP_IE;
outl(gc, ALI_REG(codec, ALI_GC_CIR));
}
#if 0 /* not used */
static void snd_ali_enable_voice_irq(struct snd_ali *codec,
unsigned int channel)
{
unsigned int mask;
struct snd_ali_channel_control *pchregs = &(codec->chregs);
snd_ali_printk("enable_voice_irq channel=%d\n",channel);
mask = 1 << (channel & 0x1f);
pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
pchregs->data.ainten |= mask;
outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
}
#endif
static void snd_ali_disable_voice_irq(struct snd_ali *codec,
unsigned int channel)
{
unsigned int mask;
struct snd_ali_channel_control *pchregs = &(codec->chregs);
snd_ali_printk("disable_voice_irq channel=%d\n",channel);
mask = 1 << (channel & 0x1f);
pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
pchregs->data.ainten &= ~mask;
outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
}
static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
{
unsigned int idx = channel & 0x1f;
if (codec->synth.chcnt >= ALI_CHANNELS){
snd_printk(KERN_ERR
"ali_alloc_pcm_channel: no free channels.\n");
return -1;
}
if (!(codec->synth.chmap & (1 << idx))) {
codec->synth.chmap |= 1 << idx;
codec->synth.chcnt++;
snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
return idx;
}
return -1;
}
static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
{
int idx;
int result = -1;
snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
/* recording */
if (rec) {
if (codec->spdif_support &&
(inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
ALI_SPDIF_IN_SUPPORT))
idx = ALI_SPDIF_IN_CHANNEL;
else
idx = ALI_PCM_IN_CHANNEL;
result = snd_ali_alloc_pcm_channel(codec, idx);
if (result >= 0)
return result;
else {
snd_printk(KERN_ERR "ali_find_free_channel: "
"record channel is busy now.\n");
return -1;
}
}
/* playback... */
if (codec->spdif_support &&
(inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
ALI_SPDIF_OUT_CH_ENABLE)) {
idx = ALI_SPDIF_OUT_CHANNEL;
result = snd_ali_alloc_pcm_channel(codec, idx);
if (result >= 0)
return result;
else
snd_printk(KERN_ERR "ali_find_free_channel: "
"S/PDIF out channel is in busy now.\n");
}
for (idx = 0; idx < ALI_CHANNELS; idx++) {
result = snd_ali_alloc_pcm_channel(codec, idx);
if (result >= 0)
return result;
}
snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
return -1;
}
static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
{
unsigned int idx = channel & 0x0000001f;
snd_ali_printk("free_channel_pcm channel=%d\n",channel);
if (channel < 0 || channel >= ALI_CHANNELS)
return;
if (!(codec->synth.chmap & (1 << idx))) {
snd_printk(KERN_ERR "ali_free_channel_pcm: "
"channel %d is not in use.\n", channel);
return;
} else {
codec->synth.chmap &= ~(1 << idx);
codec->synth.chcnt--;
}
}
#if 0 /* not used */
static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
{
unsigned int mask = 1 << (channel & 0x1f);
snd_ali_printk("start_voice: channel=%d\n",channel);
outl(mask, ALI_REG(codec,codec->chregs.regs.start));
}
#endif
static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
{
unsigned int mask = 1 << (channel & 0x1f);
snd_ali_printk("stop_voice: channel=%d\n",channel);
outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
}
/*
* S/PDIF Part
*/
static void snd_ali_delay(struct snd_ali *codec,int interval)
{
unsigned long begintimer,currenttimer;
begintimer = inl(ALI_REG(codec, ALI_STIMER));
currenttimer = inl(ALI_REG(codec, ALI_STIMER));
while (currenttimer < begintimer + interval) {
if (snd_ali_stimer_ready(codec) < 0)
break;
currenttimer = inl(ALI_REG(codec, ALI_STIMER));
cpu_relax();
}
}
static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
{
u16 wval;
u16 count = 0;
u8 bval, R1 = 0, R2;
bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
bval |= 0x1F;
outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
count ++;
snd_ali_delay(codec, 6);
bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
R1 = bval & 0x1F;
}
if (count > 50000) {
snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
return;
}
for (count = 0; count <= 50000; count++) {
snd_ali_delay(codec, 6);
bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
R2 = bval & 0x1F;
if (R2 != R1)
R1 = R2;
else
break;
}
if (count > 50000) {
snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
return;
}
if (R2 >= 0x0b && R2 <= 0x0e) {
wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
wval &= 0xe0f0;
wval |= (0x09 << 8) | 0x05;
outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
} else if (R2 == 0x12) {
wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
wval &= 0xe0f0;
wval |= (0x0e << 8) | 0x08;
outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
}
}
static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
{
u32 dwRate;
u8 bval;
bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
bval &= 0x7f;
bval |= 0x40;
outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
snd_ali_detect_spdif_rate(codec);
bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
bval &= 0x0f;
switch (bval) {
case 0: dwRate = 44100; break;
case 1: dwRate = 48000; break;
case 2: dwRate = 32000; break;
default: dwRate = 0; break;
}
return dwRate;
}
static void snd_ali_enable_spdif_in(struct snd_ali *codec)
{
unsigned int dwVal;
dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
dwVal |= ALI_SPDIF_IN_SUPPORT;
outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
dwVal |= 0x02;
outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
}
static void snd_ali_disable_spdif_in(struct snd_ali *codec)
{
unsigned int dwVal;
dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
dwVal &= ~ALI_SPDIF_IN_SUPPORT;
outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
}
static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
{
unsigned char bVal;
unsigned int dwRate;
switch (rate) {
case 32000: dwRate = 0x300; break;
case 48000: dwRate = 0x200; break;
default: dwRate = 0; break;
}
bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
bVal &= (unsigned char)(~(1<<6));
bVal |= 0x80; /* select right */
outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
bVal &= ~0x80; /* select left */
outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
}
static void snd_ali_enable_spdif_out(struct snd_ali *codec)
{
unsigned short wVal;
unsigned char bVal;
struct pci_dev *pci_dev;
pci_dev = codec->pci_m1533;
if (pci_dev == NULL)
return;
pci_read_config_byte(pci_dev, 0x61, &bVal);
bVal |= 0x40;
pci_write_config_byte(pci_dev, 0x61, bVal);
pci_read_config_byte(pci_dev, 0x7d, &bVal);
bVal |= 0x01;
pci_write_config_byte(pci_dev, 0x7d, bVal);
pci_read_config_byte(pci_dev, 0x7e, &bVal);
bVal &= (~0x20);
bVal |= 0x10;
pci_write_config_byte(pci_dev, 0x7e, bVal);
bVal = inb(ALI_REG(codec, ALI_SCTRL));
outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
wVal |= ALI_SPDIF_OUT_SEL_PCM;
outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
}
static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
{
unsigned short wVal;
wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
/*
wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
if (flag & ALI_SPDIF_OUT_NON_PCM)
wVal |= 0x0002;
else
wVal &= (~0x0002);
outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
*/
snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
}
static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
{
unsigned short wVal;
wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
wVal |= ALI_SPDIF_OUT_SEL_PCM;
outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
}
static void snd_ali_disable_spdif_out(struct snd_ali *codec)
{
unsigned char bVal;
bVal = inb(ALI_REG(codec, ALI_SCTRL));
outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
snd_ali_disable_spdif_chnout(codec);
}
static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
{
struct snd_ali_voice *pvoice;
struct snd_pcm_runtime *runtime;
struct snd_ali_channel_control *pchregs;
unsigned int old, mask;
#ifdef ALI_DEBUG
unsigned int temp, cspf;
#endif
pchregs = &(codec->chregs);
/* check if interrupt occurred for channel */
old = pchregs->data.aint;
mask = 1U << (channel & 0x1f);
if (!(old & mask))
return;
pvoice = &codec->synth.voices[channel];
runtime = pvoice->substream->runtime;
udelay(100);
spin_lock(&codec->reg_lock);
if (pvoice->pcm && pvoice->substream) {
/* pcm interrupt */
#ifdef ALI_DEBUG
outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
#endif
if (pvoice->running) {
snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
(u16)temp, cspf);
spin_unlock(&codec->reg_lock);
snd_pcm_period_elapsed(pvoice->substream);
spin_lock(&codec->reg_lock);
} else {
snd_ali_stop_voice(codec, channel);
snd_ali_disable_voice_irq(codec, channel);
}
} else if (codec->synth.voices[channel].synth) {
/* synth interrupt */
} else if (codec->synth.voices[channel].midi) {
/* midi interrupt */
} else {
/* unknown interrupt */
snd_ali_stop_voice(codec, channel);
snd_ali_disable_voice_irq(codec, channel);
}
spin_unlock(&codec->reg_lock);
outl(mask,ALI_REG(codec,pchregs->regs.aint));
pchregs->data.aint = old & (~mask);
}
static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
{
struct snd_ali *codec = dev_id;
int channel;
unsigned int audio_int;
struct snd_ali_channel_control *pchregs;
if (codec == NULL || !codec->hw_initialized)
return IRQ_NONE;
audio_int = inl(ALI_REG(codec, ALI_MISCINT));
if (!audio_int)
return IRQ_NONE;
pchregs = &(codec->chregs);
if (audio_int & ADDRESS_IRQ) {
/* get interrupt status for all channels */
pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
for (channel = 0; channel < ALI_CHANNELS; channel++)
snd_ali_update_ptr(codec, channel);
}
outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
ALI_REG(codec, ALI_MISCINT));
return IRQ_HANDLED;
}
static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
int type, int rec, int channel)
{