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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
|
/* $OpenBSD: ad1848.c,v 1.20 2000/03/02 23:02:48 mickey Exp $ */
/* $NetBSD: ad1848.c,v 1.45 1998/01/30 02:02:38 augustss Exp $ */
/*
* Copyright (c) 1994 John Brezak
* Copyright (c) 1991-1993 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Copyright by Hannu Savolainen 1994
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Portions of this code are from the VOXware support for the ad1848
* by Hannu Savolainen <hannu@voxware.pp.fi>
*
* Portions also supplied from the SoundBlaster driver for NetBSD.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <machine/cpu.h>
#include <machine/bus.h>
#include <sys/audioio.h>
#include <vm/vm.h>
#include <dev/audio_if.h>
#include <dev/auconv.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>
#include <dev/ic/ad1848reg.h>
#include <dev/ic/cs4231reg.h>
#include <dev/isa/ad1848var.h>
#include <dev/isa/cs4231var.h>
#ifdef AUDIO_DEBUG
#define DPRINTF(x) if (ad1848debug) printf x
int ad1848debug = 0;
#else
#define DPRINTF(x)
#endif
/*
* Initial values for the indirect registers of CS4248/AD1848.
*/
static int ad1848_init_values[] = {
/* Left Input Control */
GAIN_12|INPUT_MIC_GAIN_ENABLE,
/* Right Input Control */
GAIN_12|INPUT_MIC_GAIN_ENABLE,
ATTEN_12, /* Left Aux #1 Input Control */
ATTEN_12, /* Right Aux #1 Input Control */
ATTEN_12, /* Left Aux #2 Input Control */
ATTEN_12, /* Right Aux #2 Input Control */
/* bits 5-0 are attenuation select */
ATTEN_12, /* Left DAC output Control */
ATTEN_12, /* Right DAC output Control */
/* Clock and Data Format */
CLOCK_XTAL1|FMT_PCM8,
/* Interface Config */
SINGLE_DMA|AUTO_CAL_ENABLE,
INTERRUPT_ENABLE, /* Pin control */
0x00, /* Test and Init */
MODE2, /* Misc control */
ATTEN_0<<2, /* Digital Mix Control */
0, /* Upper base Count */
0, /* Lower base Count */
/* These are for CS4231 &c. only (additional registers): */
0, /* Alt feature 1 */
0, /* Alt feature 2 */
ATTEN_12, /* Left line in */
ATTEN_12, /* Right line in */
0, /* Timer low */
0, /* Timer high */
0, /* unused */
0, /* unused */
0, /* IRQ status */
0, /* unused */
/* Mono input (a.k.a speaker) (mic) Control */
MONO_INPUT_MUTE|ATTEN_6, /* mute speaker by default */
0, /* unused */
0, /* record format */
0, /* Crystal Clock Select */
0, /* upper record count */
0 /* lower record count */
};
void ad1848_reset __P((struct ad1848_softc *));
int ad1848_set_speed __P((struct ad1848_softc *, u_long *));
void ad1848_mute_monitor __P((void *, int));
static int ad_read __P((struct ad1848_softc *, int));
static void ad_write __P((struct ad1848_softc *, int, int));
static void ad_set_MCE __P((struct ad1848_softc *, int));
static void wait_for_calibration __P((struct ad1848_softc *));
#define ADREAD(sc, addr) bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (sc)->sc_iooffs+(addr))
#define ADWRITE(sc, addr, data) bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (sc)->sc_iooffs+(addr), (data))
static int
ad_read(sc, reg)
struct ad1848_softc *sc;
int reg;
{
int x, s;
s = splaudio();
ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
x = ADREAD(sc, AD1848_IDATA);
splx(s);
/* printf("(%02x<-%02x) ", reg|sc->MCE_bit, x); */
return x;
}
static void
ad_write(sc, reg, data)
struct ad1848_softc *sc;
int reg;
int data;
{
int s = splaudio();
ADWRITE(sc, AD1848_IADDR, (reg & 0xff) | sc->MCE_bit);
ADWRITE(sc, AD1848_IDATA, data & 0xff);
splx(s);
/* printf("(%02x->%02x) ", reg|sc->MCE_bit, data); */
}
static void
ad_set_MCE(sc, state)
struct ad1848_softc *sc;
int state;
{
if (state)
sc->MCE_bit = MODE_CHANGE_ENABLE;
else
sc->MCE_bit = 0;
ADWRITE(sc, AD1848_IADDR, sc->MCE_bit);
}
static void
wait_for_calibration(sc)
struct ad1848_softc *sc;
{
int timeout;
DPRINTF(("ad1848: Auto calibration started.\n"));
/*
* Wait until the auto calibration process has finished.
*
* 1) Wait until the chip becomes ready (reads don't return 0x80).
* 2) Wait until the ACI bit of I11 gets on and then off.
*/
timeout = 100000;
while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
timeout--;
if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
DPRINTF(("ad1848: Auto calibration timed out(1).\n"));
ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
timeout = 100000;
while (timeout > 0 && ADREAD(sc, AD1848_IADDR) != SP_TEST_AND_INIT)
timeout--;
if (ADREAD(sc, AD1848_IADDR) == SP_TEST_AND_INIT)
DPRINTF(("ad1848: Auto calibration timed out(1.5).\n"));
if (!(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)) {
timeout = 100000;
while (timeout > 0 && !(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG))
timeout--;
if (!(ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG))
DPRINTF(("ad1848: Auto calibration timed out(2).\n"));
}
timeout = 100000;
while (timeout > 0 && ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)
timeout--;
if (ad_read(sc, SP_TEST_AND_INIT) & AUTO_CAL_IN_PROG)
DPRINTF(("ad1848: Auto calibration timed out(3).\n"));
}
#ifdef AUDIO_DEBUG
void ad1848_dump_regs __P((struct ad1848_softc *));
void
ad1848_dump_regs(sc)
struct ad1848_softc *sc;
{
int i;
u_char r;
printf("ad1848 status=%02x", ADREAD(sc, AD1848_STATUS));
printf(" regs: ");
for (i = 0; i < 16; i++) {
r = ad_read(sc, i);
printf("%02x ", r);
}
if (sc->mode == 2) {
for (i = 16; i < 32; i++) {
r = ad_read(sc, i);
printf("%02x ", r);
}
}
printf("\n");
}
#endif
/*
* Map and probe for the ad1848 chip
*/
int
ad1848_mapprobe(sc, iobase)
struct ad1848_softc *sc;
int iobase;
{
if (!AD1848_BASE_VALID(iobase)) {
#ifdef AUDIO_DEBUG
printf("ad1848: configured iobase %04x invalid\n", iobase);
#endif
return 0;
}
sc->sc_iooffs = 0;
/* Map the AD1848 ports */
if (bus_space_map(sc->sc_iot, iobase, AD1848_NPORT, 0, &sc->sc_ioh))
return 0;
if (!ad1848_probe(sc)) {
bus_space_unmap(sc->sc_iot, sc->sc_ioh, AD1848_NPORT);
return 0;
} else
return 1;
}
/*
* Probe for the ad1848 chip
*/
int
ad1848_probe(sc)
struct ad1848_softc *sc;
{
u_char tmp, tmp1 = 0xff, tmp2 = 0xff;
#if 0
int i;
#endif
/* Is there an ad1848 chip ? */
sc->MCE_bit = MODE_CHANGE_ENABLE;
sc->mode = 1; /* MODE 1 = original ad1848/ad1846/cs4248 */
/*
* Check that the I/O address is in use.
*
* The SP_IN_INIT bit of the base I/O port is known to be 0 after the
* chip has performed its power-on initialization. Just assume
* this has happened before the OS is starting.
*
* If the I/O address is unused, inb() typically returns 0xff.
*/
tmp = ADREAD(sc, AD1848_IADDR);
if (tmp & SP_IN_INIT) { /* Not a AD1848 */
#if 0
DPRINTF(("ad_detect_A %x\n", tmp));
#endif
goto bad;
}
/*
* Test if it's possible to change contents of the indirect registers.
* Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
* so try to avoid using it.
*/
ad_write(sc, 0, 0xaa);
ad_write(sc, 1, 0x45); /* 0x55 with bit 0x10 clear */
if ((tmp1 = ad_read(sc, 0)) != 0xaa ||
(tmp2 = ad_read(sc, 1)) != 0x45) {
DPRINTF(("ad_detect_B (%x/%x)\n", tmp1, tmp2));
goto bad;
}
ad_write(sc, 0, 0x45);
ad_write(sc, 1, 0xaa);
if ((tmp1 = ad_read(sc, 0)) != 0x45 ||
(tmp2 = ad_read(sc, 1)) != 0xaa) {
DPRINTF(("ad_detect_C (%x/%x)\n", tmp1, tmp2));
goto bad;
}
/*
* The indirect register I12 has some read only bits. Lets
* try to change them.
*/
tmp = ad_read(sc, SP_MISC_INFO);
ad_write(sc, SP_MISC_INFO, (~tmp) & 0x0f);
if ((tmp & 0x0f) != ((tmp1 = ad_read(sc, SP_MISC_INFO)) & 0x0f)) {
DPRINTF(("ad_detect_D (%x)\n", tmp1));
goto bad;
}
/*
* MSB and 4 LSBs of the reg I12 tell the chip revision.
*
* A preliminary version of the AD1846 data sheet stated that it
* used an ID field of 0x0B. The current version, however,
* states that the AD1846 uses ID 0x0A, just like the AD1848K.
*
* this switch statement will need updating as newer clones arrive....
*/
switch (tmp1 & 0x8f) {
case 0x09:
sc->chip_name = "AD1848J";
break;
case 0x0A:
sc->chip_name = "AD1848K";
break;
#if 0 /* See above */
case 0x0B:
sc->chip_name = "AD1846";
break;
#endif
case 0x81:
sc->chip_name = "CS4248revB"; /* or CS4231 rev B; see below */
break;
case 0x89:
sc->chip_name = "CS4248";
break;
case 0x8A:
sc->chip_name = "broken"; /* CS4231/AD1845; see below */
break;
default:
sc->chip_name = "unknown";
DPRINTF(("ad1848: unknown codec version %#02X\n", (tmp1 & 0x8f)));
}
#if 0
/*
* XXX I don't know why, but this probe fails on an otherwise well-working
* AW35/pro card, so I'll just take it out for now. [niklas@openbsd.org]
*/
/*
* The original AD1848/CS4248 has just 16 indirect registers. This means
* that I0 and I16 should return the same value (etc.).
* Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
* with CS4231, AD1845, etc.
*/
ad_write(sc, SP_MISC_INFO, 0); /* Mode2 = disabled */
for (i = 0; i < 16; i++)
if ((tmp1 = ad_read(sc, i)) != (tmp2 = ad_read(sc, i + 16))) {
if (i != SP_TEST_AND_INIT) {
DPRINTF(("ad_detect_F(%d/%x/%x)\n", i, tmp1, tmp2));
goto bad;
}
}
#endif
/*
* Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit
* The bit 0x80 is always 1 in CS4248, CS4231, and AD1845.
*/
ad_write(sc, SP_MISC_INFO, MODE2); /* Set mode2, clear 0x80 */
tmp1 = ad_read(sc, SP_MISC_INFO);
if ((tmp1 & 0xc0) == (0x80 | MODE2)) {
/*
* CS4231 or AD1845 detected - is it?
*
* Verify that setting I2 doesn't change I18.
*/
ad_write(sc, 18, 0x88); /* Set I18 to known value */
ad_write(sc, 2, 0x45);
if ((tmp2 = ad_read(sc, 18)) != 0x45) { /* No change -> CS4231? */
ad_write(sc, 2, 0xaa);
if ((tmp2 = ad_read(sc, 18)) == 0xaa) { /* Rotten bits? */
DPRINTF(("ad_detect_H(%x)\n", tmp2));
goto bad;
}
/*
* It's a CS4231, or another clone with 32 registers.
* Let's find out which by checking I25.
*/
if ((tmp1 & 0x8f) == 0x8a) {
tmp1 = ad_read(sc, CS_VERSION_ID);
switch (tmp1 & 0xe7) {
case 0xA0:
sc->chip_name = "CS4231A";
break;
case 0x80:
/* XXX I25 no good, AD1845 same as CS4231 */
sc->chip_name = "CS4231 or AD1845";
break;
case 0x82:
case 0xa2:
sc->chip_name = "CS4232";
break;
case 0x03:
sc->chip_name = "CS4236/CS4236B";
break;
}
}
sc->mode = 2;
}
}
/* Wait for 1848 to init */
while(ADREAD(sc, AD1848_IADDR) & SP_IN_INIT)
;
/* Wait for 1848 to autocal */
ADWRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
while(ADREAD(sc, AD1848_IDATA) & AUTO_CAL_IN_PROG)
;
return 1;
bad:
return 0;
}
/* Unmap the I/O ports */
void
ad1848_unmap(sc)
struct ad1848_softc *sc;
{
bus_space_unmap(sc->sc_iot, sc->sc_ioh, AD1848_NPORT);
}
/*
* Attach hardware to driver, attach hardware driver to audio
* pseudo-device driver .
*/
void
ad1848_attach(sc)
struct ad1848_softc *sc;
{
int i;
struct ad1848_volume vol_mid = {220, 220};
struct ad1848_volume vol_0 = {0, 0};
struct audio_params pparams, rparams;
int timeout;
sc->sc_locked = 0;
sc->sc_playrun = NOTRUNNING;
sc->sc_recrun = NOTRUNNING;
if (sc->sc_drq != -1) {
if (isa_dmamap_create(sc->sc_isa, sc->sc_drq, MAX_ISADMA,
BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
printf("ad1848_attach: can't create map for drq %d\n",
sc->sc_drq);
return;
}
}
if (sc->sc_recdrq != -1 && sc->sc_recdrq != sc->sc_drq) {
if (isa_dmamap_create(sc->sc_isa, sc->sc_recdrq, MAX_ISADMA,
BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
printf("ad1848_attach: can't creape map for drq %d\n",
sc->sc_recdrq);
return;
}
}
/* Initialize the ad1848... */
for (i = 0; i < 0x10; i++) {
ad_write(sc, i, ad1848_init_values[i]);
timeout = 100000;
while (timeout > 0 && ad_read(sc, AD1848_IADDR) & SP_IN_INIT)
timeout--;
}
/* ...and additional CS4231 stuff too */
if (sc->mode == 2) {
ad_write(sc, SP_INTERFACE_CONFIG, 0); /* disable SINGLE_DMA */
for (i = 0x10; i < 0x20; i++)
if (ad1848_init_values[i] != 0) {
ad_write(sc, i, ad1848_init_values[i]);
timeout = 100000;
while (timeout > 0 &&
ad_read(sc, AD1848_IADDR) & SP_IN_INIT)
timeout--;
}
}
ad1848_reset(sc);
pparams = audio_default;
rparams = audio_default;
(void) ad1848_set_params(sc, AUMODE_RECORD|AUMODE_PLAY, 0, &pparams, &rparams);
/* Set default gains */
(void) ad1848_set_rec_gain(sc, &vol_mid);
(void) ad1848_set_channel_gain(sc, AD1848_DAC_CHANNEL, &vol_mid);
(void) ad1848_set_channel_gain(sc, AD1848_MONITOR_CHANNEL, &vol_0);
(void) ad1848_set_channel_gain(sc, AD1848_AUX1_CHANNEL, &vol_mid); /* CD volume */
if (sc->mode == 2) {
(void) ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_mid); /* CD volume */
(void) ad1848_set_channel_gain(sc, AD1848_LINE_CHANNEL, &vol_mid);
(void) ad1848_set_channel_gain(sc, AD1848_MONO_CHANNEL, &vol_0);
sc->mute[AD1848_MONO_CHANNEL] = MUTE_ALL;
} else
(void) ad1848_set_channel_gain(sc, AD1848_AUX2_CHANNEL, &vol_0);
/* Set default port */
(void) ad1848_set_rec_port(sc, MIC_IN_PORT);
if (sc->chip_name)
printf(": %s", sc->chip_name);
#undef WAITREADY
}
/*
* Various routines to interface to higher level audio driver
*/
struct ad1848_mixerinfo {
int left_reg;
int right_reg;
int atten_bits;
int atten_mask;
} mixer_channel_info[] =
{ { SP_LEFT_AUX2_CONTROL, SP_RIGHT_AUX2_CONTROL, AUX_INPUT_ATTEN_BITS,
AUX_INPUT_ATTEN_MASK },
{ SP_LEFT_AUX1_CONTROL, SP_RIGHT_AUX1_CONTROL, AUX_INPUT_ATTEN_BITS,
AUX_INPUT_ATTEN_MASK },
{ SP_LEFT_OUTPUT_CONTROL, SP_RIGHT_OUTPUT_CONTROL,
OUTPUT_ATTEN_BITS, OUTPUT_ATTEN_MASK },
{ CS_LEFT_LINE_CONTROL, CS_RIGHT_LINE_CONTROL, LINE_INPUT_ATTEN_BITS,
LINE_INPUT_ATTEN_MASK },
{ CS_MONO_IO_CONTROL, 0, MONO_INPUT_ATTEN_BITS, MONO_INPUT_ATTEN_MASK },
{ SP_DIGITAL_MIX, 0, OUTPUT_ATTEN_BITS, MIX_ATTEN_MASK }
};
/*
* This function doesn't set the mute flags but does use them.
* The mute flags reflect the mutes that have been applied by the user.
* However, the driver occasionally wants to mute devices (e.g. when chaing
* sampling rate). These operations should not affect the mute flags.
*/
void
ad1848_mute_channel(sc, device, mute)
struct ad1848_softc *sc;
int device;
int mute;
{
u_char reg;
reg = ad_read(sc, mixer_channel_info[device].left_reg);
if (mute & MUTE_LEFT) {
if (device == AD1848_MONITOR_CHANNEL)
ad_write(sc, mixer_channel_info[device].left_reg, reg & 0xFE);
else
ad_write(sc, mixer_channel_info[device].left_reg, reg | 0x80);
} else if (!(sc->mute[device] & MUTE_LEFT)) {
if (device == AD1848_MONITOR_CHANNEL)
ad_write(sc, mixer_channel_info[device].left_reg, reg | 0x01);
else
ad_write(sc, mixer_channel_info[device].left_reg, reg & ~0x80);
}
if (!mixer_channel_info[device].right_reg) {
return;
}
reg = ad_read(sc, mixer_channel_info[device].right_reg);
if (mute & MUTE_RIGHT)
ad_write(sc, mixer_channel_info[device].right_reg, reg | 0x80);
else if (!(sc->mute[device] & MUTE_RIGHT)) {
ad_write(sc, mixer_channel_info[device].right_reg, reg & ~0x80);
}
}
int
ad1848_set_channel_gain(sc, device, gp)
struct ad1848_softc *sc;
int device;
struct ad1848_volume *gp;
{
struct ad1848_mixerinfo *info = &mixer_channel_info[device];
u_char reg;
u_int atten;
sc->gains[device] = *gp;
atten = ((AUDIO_MAX_GAIN - gp->left) * info->atten_bits)/AUDIO_MAX_GAIN;
reg = ad_read(sc, info->left_reg) & (info->atten_mask);
if (device == AD1848_MONITOR_CHANNEL)
reg |= ((atten & info->atten_bits) << 2);
else
reg |= ((atten & info->atten_bits));
ad_write(sc, info->left_reg, reg);
if (!info->right_reg)
return (0);
atten = ((AUDIO_MAX_GAIN - gp->right) * info->atten_bits)/AUDIO_MAX_GAIN;
reg = ad_read(sc, info->right_reg);
reg &= (info->atten_mask);
ad_write(sc, info->right_reg, (atten& info->atten_bits)|reg);
return(0);
}
int
ad1848_get_device_gain(sc, device, gp)
struct ad1848_softc *sc;
int device;
struct ad1848_volume *gp;
{
*gp = sc->gains[device];
return(0);
}
int
ad1848_get_rec_gain(sc, gp)
struct ad1848_softc *sc;
struct ad1848_volume *gp;
{
*gp = sc->rec_gain;
return(0);
}
int
ad1848_set_rec_gain(sc, gp)
struct ad1848_softc *sc;
struct ad1848_volume *gp;
{
u_char reg, gain;
DPRINTF(("ad1848_set_rec_gain: %d:%d\n", gp->left, gp->right));
sc->rec_gain = *gp;
gain = (gp->left * GAIN_22_5)/AUDIO_MAX_GAIN;
reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
reg &= INPUT_GAIN_MASK;
ad_write(sc, SP_LEFT_INPUT_CONTROL, (gain&0x0f)|reg);
gain = (gp->right * GAIN_22_5)/AUDIO_MAX_GAIN;
reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
reg &= INPUT_GAIN_MASK;
ad_write(sc, SP_RIGHT_INPUT_CONTROL, (gain&0x0f)|reg);
return(0);
}
void
ad1848_mute_monitor(addr, mute)
void *addr;
int mute;
{
struct ad1848_softc *sc = addr;
DPRINTF(("ad1848_mute_monitor: %smuting\n", mute ? "" : "un"));
if (sc->mode == 2) {
ad1848_mute_channel(sc, AD1848_DAC_CHANNEL, mute ? MUTE_ALL : 0);
ad1848_mute_channel(sc, AD1848_MONO_CHANNEL, mute ? MUTE_MONO : 0);
ad1848_mute_channel(sc, AD1848_LINE_CHANNEL, mute ? MUTE_ALL : 0);
}
ad1848_mute_channel(sc, AD1848_AUX2_CHANNEL, mute ? MUTE_ALL : 0);
ad1848_mute_channel(sc, AD1848_AUX1_CHANNEL, mute ? MUTE_ALL : 0);
}
int
ad1848_set_mic_gain(sc, gp)
struct ad1848_softc *sc;
struct ad1848_volume *gp;
{
u_char reg;
DPRINTF(("cs4231_set_mic_gain: %d\n", gp->left));
if (gp->left > AUDIO_MAX_GAIN/2) {
sc->mic_gain_on = 1;
reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
ad_write(sc, SP_LEFT_INPUT_CONTROL, reg | INPUT_MIC_GAIN_ENABLE);
} else {
sc->mic_gain_on = 0;
reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
ad_write(sc, SP_LEFT_INPUT_CONTROL, reg & ~INPUT_MIC_GAIN_ENABLE);
}
return(0);
}
int
ad1848_get_mic_gain(sc, gp)
struct ad1848_softc *sc;
struct ad1848_volume *gp;
{
if (sc->mic_gain_on)
gp->left = gp->right = AUDIO_MAX_GAIN;
else
gp->left = gp->right = AUDIO_MIN_GAIN;
return(0);
}
static ad1848_devmap_t *ad1848_mixer_find_dev __P((ad1848_devmap_t *, int, mixer_ctrl_t *));
static ad1848_devmap_t *
ad1848_mixer_find_dev(map, cnt, cp)
ad1848_devmap_t *map;
int cnt;
mixer_ctrl_t *cp;
{
int idx;
for (idx = 0; idx < cnt; idx++) {
if (map[idx].id == cp->dev) {
return (&map[idx]);
}
}
return (NULL);
}
int
ad1848_mixer_get_port(ac, map, cnt, cp)
struct ad1848_softc *ac;
struct ad1848_devmap *map;
int cnt;
mixer_ctrl_t *cp;
{
ad1848_devmap_t *entry;
struct ad1848_volume vol;
int error = EINVAL;
int dev;
if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
return (ENXIO);
dev = entry->dev;
switch (entry->kind) {
case AD1848_KIND_LVL:
if (cp->type != AUDIO_MIXER_VALUE)
break;
if (dev < AD1848_AUX2_CHANNEL ||
dev > AD1848_MONITOR_CHANNEL)
break;
if (cp->un.value.num_channels != 1 &&
mixer_channel_info[dev].right_reg == 0)
break;
error = ad1848_get_device_gain(ac, dev, &vol);
if (!error)
ad1848_from_vol(cp, &vol);
break;
case AD1848_KIND_MUTE:
if (cp->type != AUDIO_MIXER_ENUM) break;
cp->un.ord = ac->mute[dev] ? 1 : 0;
error = 0;
break;
case AD1848_KIND_RECORDGAIN:
if (cp->type != AUDIO_MIXER_VALUE) break;
error = ad1848_get_rec_gain(ac, &vol);
if (!error)
ad1848_from_vol(cp, &vol);
break;
case AD1848_KIND_MICGAIN:
if (cp->type != AUDIO_MIXER_VALUE) break;
error = ad1848_get_mic_gain(ac, &vol);
if (!error)
ad1848_from_vol(cp, &vol);
break;
case AD1848_KIND_RECORDSOURCE:
if (cp->type != AUDIO_MIXER_ENUM) break;
cp->un.ord = ad1848_get_rec_port(ac);
error = 0;
break;
default:
printf ("Invalid kind\n");
break;
}
return (error);
}
int
ad1848_mixer_set_port(ac, map, cnt, cp)
struct ad1848_softc *ac;
struct ad1848_devmap *map;
int cnt;
mixer_ctrl_t *cp;
{
ad1848_devmap_t *entry;
struct ad1848_volume vol;
int error = EINVAL;
int dev;
if (!(entry = ad1848_mixer_find_dev(map, cnt, cp)))
return (ENXIO);
dev = entry->dev;
switch (entry->kind) {
case AD1848_KIND_LVL:
if (cp->type != AUDIO_MIXER_VALUE)
break;
if (dev < AD1848_AUX2_CHANNEL ||
dev > AD1848_MONITOR_CHANNEL)
break;
if (cp->un.value.num_channels != 1 &&
mixer_channel_info[dev].right_reg == 0)
break;
ad1848_to_vol(cp, &vol);
error = ad1848_set_channel_gain(ac, dev, &vol);
break;
case AD1848_KIND_MUTE:
if (cp->type != AUDIO_MIXER_ENUM) break;
ac->mute[dev] = (cp->un.ord ? MUTE_ALL : 0);
ad1848_mute_channel(ac, dev, ac->mute[dev]);
error = 0;
break;
case AD1848_KIND_RECORDGAIN:
if (cp->type != AUDIO_MIXER_VALUE) break;
ad1848_to_vol(cp, &vol);
error = ad1848_set_rec_gain(ac, &vol);
break;
case AD1848_KIND_MICGAIN:
if (cp->type != AUDIO_MIXER_VALUE) break;
ad1848_to_vol(cp, &vol);
error = ad1848_set_mic_gain(ac, &vol);
break;
case AD1848_KIND_RECORDSOURCE:
if (cp->type != AUDIO_MIXER_ENUM) break;
error = ad1848_set_rec_port(ac, cp->un.ord);
break;
default:
printf ("Invalid kind\n");
break;
}
return (error);
}
int
ad1848_query_encoding(addr, fp)
void *addr;
struct audio_encoding *fp;
{
struct ad1848_softc *sc = addr;
switch (fp->index) {
case 0:
strcpy(fp->name, AudioEmulaw);
fp->encoding = AUDIO_ENCODING_ULAW;
fp->precision = 8;
fp->flags = 0;
break;
case 1:
strcpy(fp->name, AudioEalaw);
fp->encoding = AUDIO_ENCODING_ALAW;
fp->precision = 8;
fp->flags = 0;
break;
case 2:
strcpy(fp->name, AudioEslinear_le);
fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
fp->precision = 16;
fp->flags = 0;
break;
case 3:
strcpy(fp->name, AudioEulinear);
fp->encoding = AUDIO_ENCODING_ULINEAR;
fp->precision = 8;
fp->flags = 0;
break;
case 4: /* only on CS4231 */
strcpy(fp->name, AudioEslinear_be);
fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
fp->precision = 16;
fp->flags = sc->mode == 1 ? AUDIO_ENCODINGFLAG_EMULATED : 0;
break;
/* emulate some modes */
case 5:
strcpy(fp->name, AudioEslinear);
fp->encoding = AUDIO_ENCODING_SLINEAR;
fp->precision = 8;
fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
break;
case 6:
strcpy(fp->name, AudioEulinear_le);
fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
fp->precision = 16;
fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
break;
case 7:
strcpy(fp->name, AudioEulinear_be);
fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
fp->precision = 16;
fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
break;
case 8: /* only on CS4231 */
if (sc->mode == 1)
return EINVAL;
strcpy(fp->name, AudioEadpcm);
fp->encoding = AUDIO_ENCODING_ADPCM;
fp->precision = 8;
fp->flags = 0;
break;
default:
return EINVAL;
/*NOTREACHED*/
}
return (0);
}
int
ad1848_set_params(addr, setmode, usemode, p, r)
void *addr;
int setmode, usemode;
struct audio_params *p, *r;
{
struct ad1848_softc *sc = addr;
int error, bits, enc;
void (*pswcode) __P((void *, u_char *buf, int cnt));
void (*rswcode) __P((void *, u_char *buf, int cnt));
DPRINTF(("ad1848_set_params: %d %d %d %ld\n",
p->encoding, p->precision, p->channels, p->sample_rate));
enc = p->encoding;
pswcode = rswcode = 0;
switch (enc) {
case AUDIO_ENCODING_SLINEAR_LE:
if (p->precision == 8) {
enc = AUDIO_ENCODING_ULINEAR_LE;
pswcode = rswcode = change_sign8;
}
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (p->precision == 16 && sc->mode == 1) {
enc = AUDIO_ENCODING_SLINEAR_LE;
pswcode = rswcode = swap_bytes;
}
break;
case AUDIO_ENCODING_ULINEAR_LE:
if (p->precision == 16) {
enc = AUDIO_ENCODING_SLINEAR_LE;
pswcode = rswcode = change_sign16;
}
break;
case AUDIO_ENCODING_ULINEAR_BE:
if (p->precision == 16) {
enc = AUDIO_ENCODING_SLINEAR_LE;
pswcode = swap_bytes_change_sign16;
rswcode = change_sign16_swap_bytes;
}
break;
}
switch (enc) {
case AUDIO_ENCODING_ULAW:
bits = FMT_ULAW >> 5;
break;
case AUDIO_ENCODING_ALAW:
bits = FMT_ALAW >> 5;
break;
case AUDIO_ENCODING_ADPCM:
bits = FMT_ADPCM >> 5;
break;
case AUDIO_ENCODING_SLINEAR_LE:
if (p->precision == 16)
bits = FMT_TWOS_COMP >> 5;
else
return EINVAL;
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (p->precision == 16)
bits = FMT_TWOS_COMP_BE >> 5;
else
return EINVAL;
break;
case AUDIO_ENCODING_ULINEAR_LE:
if (p->precision == 8)
bits = FMT_PCM8 >> 5;
else
return EINVAL;
break;
default:
return EINVAL;
}
if (p->channels < 1 || p->channels > 2)
return EINVAL;
error = ad1848_set_speed(sc, &p->sample_rate);
if (error)
return error;
p->sw_code = pswcode;
r->sw_code = rswcode;
sc->format_bits = bits;
sc->channels = p->channels;
sc->precision = p->precision;
sc->need_commit = 1;
DPRINTF(("ad1848_set_params succeeded, bits=%x\n", bits));
return (0);
}
int
ad1848_set_rec_port(sc, port)
struct ad1848_softc *sc;
int port;
{
u_char inp, reg;
DPRINTF(("ad1848_set_rec_port: 0x%x\n", port));
if (port == MIC_IN_PORT) {
inp = MIC_INPUT;
}
else if (port == LINE_IN_PORT) {
inp = LINE_INPUT;
}
else if (port == DAC_IN_PORT) {
inp = MIXED_DAC_INPUT;
}
else if (sc->mode == 2 && port == AUX1_IN_PORT) {
inp = AUX_INPUT;
}
else
return(EINVAL);
reg = ad_read(sc, SP_LEFT_INPUT_CONTROL);
reg &= INPUT_SOURCE_MASK;
ad_write(sc, SP_LEFT_INPUT_CONTROL, (inp|reg));
reg = ad_read(sc, SP_RIGHT_INPUT_CONTROL);
reg &= INPUT_SOURCE_MASK;
ad_write(sc, SP_RIGHT_INPUT_CONTROL, (inp|reg));
sc->rec_port = port;
return(0);
}
int
ad1848_get_rec_port(sc)
struct ad1848_softc *sc;
{
return(sc->rec_port);
}
int
ad1848_round_blocksize(addr, blk)
void *addr;
int blk;
{
struct ad1848_softc *sc = addr;
sc->sc_lastcc = -1;
/* Round to a multiple of the biggest sample size. */
blk &= -4;
return (blk);
}
int
ad1848_open(addr, flags)
void *addr;
int flags;
{
struct ad1848_softc *sc = addr;
DPRINTF(("ad1848_open: sc=%p\n", sc));
sc->sc_intr = 0;
sc->sc_lastcc = -1;
sc->sc_locked = 0;
/* Enable interrupts */
DPRINTF(("ad1848_open: enable intrs\n"));
ad_write(sc, SP_PIN_CONTROL, INTERRUPT_ENABLE|ad_read(sc, SP_PIN_CONTROL));
#ifdef AUDIO_DEBUG
if (ad1848debug)
ad1848_dump_regs(sc);
#endif
return 0;
}
/*
* Close function is called at splaudio().
*/
void
ad1848_close(addr)
void *addr;
{
struct ad1848_softc *sc = addr;
u_char r;
sc->sc_intr = 0;
DPRINTF(("ad1848_close: stop DMA\n"));
if (sc->sc_playrun != NOTRUNNING) {
isa_dmaabort(sc->sc_isa, sc->sc_drq);
sc->sc_playrun = NOTRUNNING;
}
if (sc->sc_recrun != NOTRUNNING) {
isa_dmaabort(sc->sc_isa, sc->sc_recdrq);
sc->sc_recrun = NOTRUNNING;
}
ad_write(sc, SP_LOWER_BASE_COUNT, (u_char)0);
ad_write(sc, SP_UPPER_BASE_COUNT, (u_char)0);
/* Disable interrupts */
DPRINTF(("ad1848_close: disable intrs\n"));
ad_write(sc, SP_PIN_CONTROL,
ad_read(sc, SP_PIN_CONTROL) & ~INTERRUPT_ENABLE);
DPRINTF(("ad1848_close: disable capture and playback\n"));
r = ad_read(sc, SP_INTERFACE_CONFIG);
r &= ~(CAPTURE_ENABLE|PLAYBACK_ENABLE);
ad_write(sc, SP_INTERFACE_CONFIG, r);
#ifdef AUDIO_DEBUG
if (ad1848debug)
ad1848_dump_regs(sc);
#endif
}
/*
* Lower-level routines
*/
int
ad1848_commit_settings(addr)
void *addr;
{
struct ad1848_softc *sc = addr;
int timeout;
u_char fs;
int s;
if (!sc->need_commit)
return 0;
s = splaudio();
ad1848_mute_monitor(sc, 1);
ad_set_MCE(sc, 1); /* Enables changes to the format select reg */
fs = sc->speed_bits | (sc->format_bits << 5);
if (sc->channels == 2)
fs |= FMT_STEREO;
ad_write(sc, SP_CLOCK_DATA_FORMAT, fs);
/*
* If mode == 2 (CS4231), set I28 also. It's the capture format register.
*/
if (sc->mode == 2) {
/* Gravis Ultrasound MAX SDK sources says something about errata
* sheets, with the implication that these inb()s are necessary.
*/
(void)ADREAD(sc, AD1848_IDATA);
(void)ADREAD(sc, AD1848_IDATA);
/*
* Write to I8 starts resyncronization. Wait until it completes.
*/
timeout = 100000;
while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
timeout--;
ad_write(sc, CS_REC_FORMAT, fs);
/* Gravis Ultrasound MAX SDK sources says something about errata
* sheets, with the implication that these inb()s are necessary.
*/
(void)ADREAD(sc, AD1848_IDATA);
(void)ADREAD(sc, AD1848_IDATA);
/* Now wait for resync for capture side of the house */
}
/*
* Write to I8 starts resyncronization. Wait until it completes.
*/
timeout = 100000;
while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
timeout--;
if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT)
printf("ad1848_commit: Auto calibration timed out\n");
/*
* Starts the calibration process and
* enters playback mode after it.
*/
ad_set_MCE(sc, 0);
wait_for_calibration(sc);
ad1848_mute_monitor(sc, 0);
sc->sc_lastcc = -1;
splx(s);
sc->need_commit = 0;
return 0;
}
void
ad1848_reset(sc)
struct ad1848_softc *sc;
{
u_char r;
DPRINTF(("ad1848_reset\n"));
/* Clear the PEN and CEN bits */
r = ad_read(sc, SP_INTERFACE_CONFIG);
r &= ~(CAPTURE_ENABLE|PLAYBACK_ENABLE);
ad_write(sc, SP_INTERFACE_CONFIG, r);
if (sc->mode == 2) {
ADWRITE(sc, AD1848_IADDR, CS_IRQ_STATUS);
ADWRITE(sc, AD1848_IDATA, 0);
}
/* Clear interrupt status */
ADWRITE(sc, AD1848_STATUS, 0);
#ifdef AUDIO_DEBUG
if (ad1848debug)
ad1848_dump_regs(sc);
#endif
}
int
ad1848_set_speed(sc, argp)
struct ad1848_softc *sc;
u_long *argp;
{
/*
* The sampling speed is encoded in the least significant nible of I8. The
* LSB selects the clock source (0=24.576 MHz, 1=16.9344 Mhz) and other
* three bits select the divisor (indirectly):
*
* The available speeds are in the following table. Keep the speeds in
* the increasing order.
*/
typedef struct {
int speed;
u_char bits;
} speed_struct;
u_long arg = *argp;
static speed_struct speed_table[] = {
{5510, (0 << 1) | 1},
{5510, (0 << 1) | 1},
{6620, (7 << 1) | 1},
{8000, (0 << 1) | 0},
{9600, (7 << 1) | 0},
{11025, (1 << 1) | 1},
{16000, (1 << 1) | 0},
{18900, (2 << 1) | 1},
{22050, (3 << 1) | 1},
{27420, (2 << 1) | 0},
{32000, (3 << 1) | 0},
{33075, (6 << 1) | 1},
{37800, (4 << 1) | 1},
{44100, (5 << 1) | 1},
{48000, (6 << 1) | 0}
};
int i, n, selected = -1;
n = sizeof(speed_table) / sizeof(speed_struct);
if (arg < speed_table[0].speed)
selected = 0;
if (arg > speed_table[n - 1].speed)
selected = n - 1;
for (i = 1 /*really*/ ; selected == -1 && i < n; i++)
if (speed_table[i].speed == arg)
selected = i;
else if (speed_table[i].speed > arg) {
int diff1, diff2;
diff1 = arg - speed_table[i - 1].speed;
diff2 = speed_table[i].speed - arg;
if (diff1 < diff2)
selected = i - 1;
else
selected = i;
}
if (selected == -1) {
printf("ad1848: Can't find speed???\n");
selected = 3;
}
sc->speed_bits = speed_table[selected].bits;
sc->need_commit = 1;
*argp = speed_table[selected].speed;
return (0);
}
/*
* Halt a DMA in progress.
*/
int
ad1848_halt_out_dma(addr)
void *addr;
{
struct ad1848_softc *sc = addr;
u_char reg;
DPRINTF(("ad1848: ad1848_halt_out_dma\n"));
reg = ad_read(sc, SP_INTERFACE_CONFIG);
ad_write(sc, SP_INTERFACE_CONFIG, (reg & ~PLAYBACK_ENABLE));
sc->sc_locked = 0;
return(0);
}
int
ad1848_halt_in_dma(addr)
void *addr;
{
struct ad1848_softc *sc = addr;
u_char reg;
DPRINTF(("ad1848: ad1848_halt_in_dma\n"));
reg = ad_read(sc, SP_INTERFACE_CONFIG);
ad_write(sc, SP_INTERFACE_CONFIG, (reg & ~CAPTURE_ENABLE));
sc->sc_locked = 0;
return(0);
}
int
ad1848_dma_init_input(addr, buf, cc)
void *addr;
void *buf;
int cc;
{
struct ad1848_softc *sc = addr;
sc->sc_recrun = DMARUNNING;
sc->sc_dma_flags = DMAMODE_READ | DMAMODE_LOOP;
sc->sc_dma_bp = buf;
sc->sc_dma_cnt = cc;
isa_dmastart(sc->sc_isa, sc->sc_recdrq, buf, cc, NULL,
sc->sc_dma_flags, BUS_DMA_NOWAIT);
DPRINTF(("ad1848_dma_init_input: %p %d\n", buf, cc));
return 0;
}
/*
* DMA input/output are called at splaudio().
*/
int
ad1848_dma_input(addr, p, cc, intr, arg)
void *addr;
void *p;
int cc;
void (*intr) __P((void *));
void *arg;
{
struct ad1848_softc *sc = addr;
u_char reg;
if (sc->sc_locked) {
DPRINTF(("ad1848_dma_input: locked\n"));
return 0;
}
#ifdef AUDIO_DEBUG
if (ad1848debug > 1)
printf("ad1848_dma_input: cc=%d %p (%p)\n", cc, intr, arg);
#endif
sc->sc_locked = 1;
sc->sc_intr = intr;
sc->sc_arg = arg;
switch (sc->sc_recrun) {
case NOTRUNNING:
sc->sc_dma_flags = DMAMODE_READ;
sc->sc_dma_bp = p;
sc->sc_dma_cnt = cc;
isa_dmastart(sc->sc_isa, sc->sc_recdrq, p, cc, NULL,
DMAMODE_READ, BUS_DMA_NOWAIT);
goto startpcm;
case DMARUNNING:
sc->sc_recrun = PCMRUNNING;
startpcm:
if (sc->precision == 16)
cc >>= 1;
if (sc->channels == 2)
cc >>= 1;
cc--;
if (sc->sc_lastcc != cc || sc->sc_mode != AUMODE_RECORD) {
ad_write(sc, SP_LOWER_BASE_COUNT, (u_char)(cc & 0xff));
ad_write(sc, SP_UPPER_BASE_COUNT, (u_char)((cc >> 8) & 0xff));
if (sc->mode == 2) {
ad_write(sc, CS_LOWER_REC_CNT, (u_char)(cc & 0xff));
ad_write(sc, CS_UPPER_REC_CNT, (u_char)((cc >> 8) & 0xff));
}
reg = ad_read(sc, SP_INTERFACE_CONFIG);
ad_write(sc, SP_INTERFACE_CONFIG, (CAPTURE_ENABLE|reg));
sc->sc_lastcc = cc;
sc->sc_mode = AUMODE_RECORD;
#ifdef AUDIO_DEBUG
if (ad1848debug > 1)
printf("ad1848_dma_input: started capture\n");
#endif
}
case PCMRUNNING:
break;
}
return 0;
}
int
ad1848_dma_init_output(addr, buf, cc)
void *addr;
void *buf;
int cc;
{
struct ad1848_softc *sc = addr;
sc->sc_playrun = DMARUNNING;
sc->sc_dma_flags = DMAMODE_WRITE | DMAMODE_LOOP;
sc->sc_dma_bp = buf;
sc->sc_dma_cnt = cc;
isa_dmastart(sc->sc_isa, sc->sc_drq, buf, cc, NULL,
sc->sc_dma_flags, BUS_DMA_NOWAIT);
DPRINTF(("ad1848_dma_init_output: %p %d\n", buf, cc));
return 0;
}
int
ad1848_dma_output(addr, p, cc, intr, arg)
void *addr;
void *p;
int cc;
void (*intr) __P((void *));
void *arg;
{
struct ad1848_softc *sc = addr;
u_char reg;
if (sc->sc_locked) {
DPRINTF(("ad1848_dma_output: locked\n"));
return 0;
}
#ifdef AUDIO_DEBUG
if (ad1848debug > 0)
printf("ad1848_dma_output: cc=%d at %p 0x%p (0x%p)\n", cc, p, intr, arg);
#endif
sc->sc_locked = 1;
sc->sc_intr = intr;
sc->sc_arg = arg;
switch (sc->sc_playrun) {
case NOTRUNNING:
sc->sc_dma_flags = DMAMODE_WRITE;
sc->sc_dma_bp = p;
sc->sc_dma_cnt = cc;
isa_dmastart(sc->sc_isa, sc->sc_drq, p, cc, NULL,
DMAMODE_WRITE, BUS_DMA_NOWAIT);
goto startpcm;
case DMARUNNING:
sc->sc_playrun = PCMRUNNING;
startpcm:
if (sc->precision == 16)
cc >>= 1;
if (sc->channels == 2)
cc >>= 1;
cc--;
if (sc->sc_lastcc != cc || sc->sc_mode != AUMODE_PLAY) {
ad_write(sc, SP_LOWER_BASE_COUNT, (u_char)(cc & 0xff));
ad_write(sc, SP_UPPER_BASE_COUNT, (u_char)((cc >> 8) & 0xff));
reg = ad_read(sc, SP_INTERFACE_CONFIG);
ad_write(sc, SP_INTERFACE_CONFIG, (PLAYBACK_ENABLE|reg));
sc->sc_lastcc = cc;
sc->sc_mode = AUMODE_PLAY;
}
break;
case PCMRUNNING:
break;
}
return 0;
}
int
ad1848_intr(arg)
void *arg;
{
struct ad1848_softc *sc = arg;
int retval = 0;
u_char status;
/* Get intr status */
status = ADREAD(sc, AD1848_STATUS);
#ifdef AUDIO_DEBUG
if (ad1848debug > 1)
printf("ad1848_intr: intr=%p status=%x\n", sc->sc_intr, status);
#endif
sc->sc_locked = 0;
sc->sc_interrupts++;
/* Handle interrupt */
if (sc->sc_intr && (status & INTERRUPT_STATUS)) {
/* ACK DMA read because it may be in a bounce buffer */
/* XXX Do write to mask DMA ? */
if ((sc->sc_dma_flags & DMAMODE_READ) && sc->sc_recrun == NOTRUNNING)
isa_dmadone(sc->sc_isa, sc->sc_recdrq);
(*sc->sc_intr)(sc->sc_arg);
retval = 1;
}
/* clear interrupt */
if (status & INTERRUPT_STATUS)
ADWRITE(sc, AD1848_STATUS, 0);
return(retval);
}
void *
ad1848_malloc(addr, size, pool, flags)
void *addr;
unsigned long size;
int pool;
int flags;
{
struct ad1848_softc *sc = addr;
return isa_malloc(sc->sc_isa, 4, size, pool, flags);
}
void
ad1848_free(addr, ptr, pool)
void *addr;
void *ptr;
int pool;
{
isa_free(ptr, pool);
}
unsigned long
ad1848_round(addr, size)
void *addr;
unsigned long size;
{
if (size > MAX_ISADMA)
size = MAX_ISADMA;
return size;
}
int
ad1848_mappage(addr, mem, off, prot)
void *addr;
void *mem;
int off;
int prot;
{
return isa_mappage(mem, off, prot);
}
int
ad1848_get_props(addr)
void *addr;
{
struct ad1848_softc *sc = addr;
return AUDIO_PROP_MMAP |
(sc->sc_drq != sc->sc_recdrq ? AUDIO_PROP_FULLDUPLEX : 0);
}
|