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
|
/* $OpenBSD: mavb.c,v 1.13 2010/07/15 03:43:11 jakemsr Exp $ */
/*
* Copyright (c) 2005 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/timeout.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <machine/autoconf.h>
#include <sys/audioio.h>
#include <dev/audio_if.h>
#include <mips64/archtype.h>
#include <sgi/localbus/macebus.h>
#include <sgi/localbus/macebusvar.h>
#include <sgi/dev/mavbreg.h>
#include <dev/ic/ad1843reg.h>
#undef MAVB_DEBUG
#ifdef MAVB_DEBUG
#define DPRINTF(l,x) do { if (mavb_debug & (l)) printf x; } while (0)
#define MAVB_DEBUG_INTR 0x0100
int mavb_debug = ~MAVB_DEBUG_INTR;
#else
#define DPRINTF(l,x) /* nothing */
#endif
/* Repeat delays for volume buttons. */
#define MAVB_VOLUME_BUTTON_REPEAT_DEL1 400 /* 400ms to start repeating */
#define MAVB_VOLUME_BUTTON_REPEAT_DELN 100 /* 100ms between repeats */
/* XXX We need access to some of the MACE ISA registers. */
#define MAVB_ISA_NREGS 0x20
#define MAVB_ISA_RING_SIZE 0x4000 /* Mace ISA DMA ring size. */
#define MAVB_CHAN_RING_SIZE 0x1000 /* DMA buffer size per channel. */
#define MAVB_CHAN_INTR_SIZE 0x0800 /* Interrupt on 50% buffer transfer. */
#define MAVB_CHAN_CHUNK_SIZE 0x0400 /* Move data in 25% buffer chunks. */
/*
* AD1843 Mixer.
*/
enum {
AD1843_RECORD_CLASS,
AD1843_ADC_SOURCE, /* ADC Source Select */
AD1843_ADC_GAIN, /* ADC Input Gain */
AD1843_ADC_MIC_GAIN, /* ADC Mic Input Gain */
AD1843_INPUT_CLASS,
AD1843_DAC1_GAIN, /* DAC1 Analog/Digital Gain/Attenuation */
AD1843_DAC1_MUTE, /* DAC1 Analog Mute */
AD1843_DAC2_GAIN, /* DAC2 Mix Gain */
AD1843_AUX1_GAIN, /* Auxilliary 1 Mix Gain */
AD1843_AUX2_GAIN, /* Auxilliary 2 Mix Gain */
AD1843_AUX3_GAIN, /* Auxilliary 3 Mix Gain */
AD1843_MIC_GAIN, /* Microphone Mix Gain */
AD1843_MONO_GAIN, /* Mono Mix Gain */
AD1843_DAC2_MUTE, /* DAC2 Mix Mute */
AD1843_AUX1_MUTE, /* Auxilliary 1 Mix Mute */
AD1843_AUX2_MUTE, /* Auxilliary 2 Mix Mute */
AD1843_AUX3_MUTE, /* Auxilliary 3 Mix Mute */
AD1843_MIC_MUTE, /* Microphone Mix Mute */
AD1843_MONO_MUTE, /* Mono Mix Mute */
AD1843_SUM_MUTE, /* Sum Mute */
AD1843_OUTPUT_CLASS,
AD1843_MNO_MUTE, /* Mono Output Mute */
AD1843_HPO_MUTE /* Headphone Output Mute */
};
/* ADC Source Select. The order matches the hardware bits. */
const char *ad1843_source[] = {
AudioNline,
AudioNmicrophone,
AudioNaux "1",
AudioNaux "2",
AudioNaux "3",
AudioNmono,
AudioNdac "1",
AudioNdac "2"
};
/* Mix Control. The order matches the hardware register numbering. */
const char *ad1843_input[] = {
AudioNdac "2", /* AD1843_DAC2__TO_MIXER */
AudioNaux "1",
AudioNaux "2",
AudioNaux "3",
AudioNmicrophone,
AudioNmono /* AD1843_MISC_SETTINGS */
};
struct mavb_chan {
caddr_t hw_start;
caddr_t sw_start;
caddr_t sw_end;
caddr_t sw_cur;
void (*intr)(void *);
void *intrarg;
u_long rate;
u_int format;
int blksize;
};
struct mavb_softc {
struct device sc_dev;
bus_space_tag_t sc_st;
bus_space_handle_t sc_sh;
bus_dma_tag_t sc_dmat;
bus_dmamap_t sc_dmamap;
/* XXX We need access to some of the MACE ISA registers. */
bus_space_handle_t sc_isash;
caddr_t sc_ring;
struct mavb_chan play;
struct mavb_chan rec;
struct timeout sc_volume_button_to;
};
typedef u_long ad1843_addr_t;
u_int16_t ad1843_reg_read(struct mavb_softc *, ad1843_addr_t);
u_int16_t ad1843_reg_write(struct mavb_softc *, ad1843_addr_t, u_int16_t);
void ad1843_dump_regs(struct mavb_softc *);
int mavb_match(struct device *, void *, void *);
void mavb_attach(struct device *, struct device *, void *);
struct cfattach mavb_ca = {
sizeof(struct mavb_softc), mavb_match, mavb_attach
};
struct cfdriver mavb_cd = {
NULL, "mavb", DV_DULL
};
int mavb_open(void *, int);
void mavb_close(void *);
int mavb_query_encoding(void *, struct audio_encoding *);
int mavb_set_params(void *, int, int, struct audio_params *,
struct audio_params *);
int mavb_round_blocksize(void *hdl, int bs);
int mavb_halt_output(void *);
int mavb_halt_input(void *);
int mavb_getdev(void *, struct audio_device *);
int mavb_set_port(void *, struct mixer_ctrl *);
int mavb_get_port(void *, struct mixer_ctrl *);
int mavb_query_devinfo(void *, struct mixer_devinfo *);
int mavb_get_props(void *);
int mavb_trigger_output(void *, void *, void *, int, void (*)(void *),
void *, struct audio_params *);
int mavb_trigger_input(void *, void *, void *, int, void (*)(void *),
void *, struct audio_params *);
void mavb_get_default_params(void *, int, struct audio_params *);
struct audio_hw_if mavb_sa_hw_if = {
mavb_open,
mavb_close,
0,
mavb_query_encoding,
mavb_set_params,
mavb_round_blocksize,
0,
0,
0,
0,
0,
mavb_halt_output,
mavb_halt_input,
0,
mavb_getdev,
0,
mavb_set_port,
mavb_get_port,
mavb_query_devinfo,
0,
0,
0,
0,
mavb_get_props,
mavb_trigger_output,
mavb_trigger_input,
mavb_get_default_params
};
struct audio_device mavb_device = {
"A3",
"",
"mavb"
};
int
mavb_open(void *hdl, int flags)
{
return (0);
}
void
mavb_close(void *hdl)
{
}
int
mavb_query_encoding(void *hdl, struct audio_encoding *ae)
{
switch (ae->index) {
case 0:
/* 8-bit Unsigned Linear PCM. */
strlcpy(ae->name, AudioEulinear, sizeof ae->name);
ae->encoding = AUDIO_ENCODING_ULINEAR;
ae->precision = 8;
ae->flags = 0;
break;
case 1:
/* 16-bit Signed Linear PCM. */
strlcpy(ae->name, AudioEslinear_be, sizeof ae->name);
ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
ae->precision = 16;
ae->flags = 0;
break;
case 2:
/* 8-bit mu-Law Companded. */
strlcpy(ae->name, AudioEmulaw, sizeof ae->name);
ae->encoding = AUDIO_ENCODING_ULAW;
ae->precision = 8;
ae->flags = 0;
break;
case 3:
/* 8-bit A-Law Companded. */
strlcpy(ae->name, AudioEalaw, sizeof ae->name);
ae->encoding = AUDIO_ENCODING_ALAW;
ae->precision = 8;
ae->flags = 0;
break;
default:
return (EINVAL);
}
ae->bps = AUDIO_BPS(ae->precision);
ae->msb = 1;
return (0);
}
/*
* For some reason SGI has decided to standardize their sound hardware
* interfaces on 24-bit PCM even though the AD1843 codec used in the
* Moosehead A/V Board only supports 16-bit and 8-bit formats.
* Therefore we must convert everything to 24-bit samples only to have
* the MACE hardware convert them back into 16-bit samples again. To
* complicate matters further, the 24-bit samples are embedded 32-bit
* integers. The 8-bit and 16-bit samples are first converted into
* 24-bit samples by padding them to the right with zeroes. Then they
* are sign-extended into 32-bit integers. This conversion is
* conveniently done through the software encoding layer of the high
* level audio driver by using the functions below. Conversion of
* mu-law and A-law formats is done by the hardware.
*/
static void
linear16_to_linear24_be(void *hdl, u_char *p, int cc)
{
u_char *q = p;
p += cc;
q += cc * 2;
while ((cc -= 2) >= 0) {
q -= 4;
q[3] = 0;
q[2] = *--p;
q[1] = *--p;
q[0] = (*p & 0x80) ? 0xff : 0;
}
}
static void
linear24_to_linear16_be(void *hdl, u_char *p, int cc)
{
u_char *q = p;
while ((cc -= 4) >= 0) {
*q++ = p[1];
*q++ = p[2];
p += 4;
}
}
static void
ulinear8_to_ulinear24_be(void *hdl, u_char *p, int cc)
{
u_char *q = p;
p += cc;
q += cc * 4;
while (--cc >= 0) {
q -= 4;
q[3] = 0;
q[2] = 0;
q[1] = *--p;
q[0] = (*p & 0x80) ? 0xff : 0;
}
}
static void
ulinear24_to_ulinear8_be(void *hdl, u_char *p, int cc)
{
u_char *q = p;
while ((cc -= 4) >= 0) {
*q++ = p[1];
p += 4;
}
}
static void
linear16_to_linear24_be_mts(void *hdl, u_char *p, int cc)
{
u_char *q = p;
p += cc;
q += cc * 4;
while ((cc -= 2) >= 0) {
q -= 8;
q[3] = q[7] = 0;
q[2] = q[6] = *--p;
q[1] = q[5] = *--p;
q[0] = q[4] = (*p & 0x80) ? 0xff : 0;
}
}
static void
ulinear8_to_ulinear24_be_mts(void *hdl, u_char *p, int cc)
{
u_char *q = p;
p += cc;
q += cc * 8;
while (--cc >= 0) {
q -= 8;
q[3] = q[7] = 0;
q[2] = q[6] = 0;
q[1] = q[5] = *--p;
q[0] = q[4] = (*p & 0x80) ? 0xff : 0;
}
}
void
mavb_get_default_params(void *hdl, int mode, struct audio_params *p)
{
p->sample_rate = 48000;
p->encoding = AUDIO_ENCODING_SLINEAR_BE;
p->precision = 16;
p->bps = 2;
p->msb = 1;
p->channels = 2;
p->factor = 2;
if (mode == AUMODE_PLAY)
p->sw_code = linear16_to_linear24_be;
else
p->sw_code = linear24_to_linear16_be;
}
static int
mavb_set_play_rate(struct mavb_softc *sc, u_long sample_rate)
{
if (sample_rate < 4000 || sample_rate > 48000)
return (EINVAL);
if (sc->play.rate != sample_rate) {
ad1843_reg_write(sc, AD1843_CLOCK2_SAMPLE_RATE, sample_rate);
sc->play.rate = sample_rate;
}
return (0);
}
static int
mavb_set_rec_rate(struct mavb_softc *sc, u_long sample_rate)
{
if (sample_rate < 4000 || sample_rate > 48000)
return (EINVAL);
if (sc->rec.rate != sample_rate) {
ad1843_reg_write(sc, AD1843_CLOCK1_SAMPLE_RATE, sample_rate);
sc->rec.rate = sample_rate;
}
return (0);
}
static int
mavb_get_format(u_int encoding, u_int *format)
{
switch(encoding) {
case AUDIO_ENCODING_ULINEAR_BE:
*format = AD1843_PCM8;
break;
case AUDIO_ENCODING_SLINEAR_BE:
*format = AD1843_PCM16;
break;
case AUDIO_ENCODING_ULAW:
*format = AD1843_ULAW;
break;
case AUDIO_ENCODING_ALAW:
*format = AD1843_ALAW;
break;
default:
return (EINVAL);
}
return (0);
}
static int
mavb_set_play_format(struct mavb_softc *sc, u_int encoding)
{
u_int16_t value;
u_int format;
int err;
err = mavb_get_format(encoding, &format);
if (err)
return (err);
if (sc->play.format != format) {
value = ad1843_reg_read(sc, AD1843_SERIAL_INTERFACE);
value &= ~AD1843_DA1F_MASK;
value |= (format << AD1843_DA1F_SHIFT);
ad1843_reg_write(sc, AD1843_SERIAL_INTERFACE, value);
sc->play.format = format;
}
return (0);
}
static int
mavb_set_rec_format(struct mavb_softc *sc, u_int encoding)
{
u_int16_t value;
u_int format;
int err;
err = mavb_get_format(encoding, &format);
if (err)
return (err);
if (sc->rec.format != format) {
value = ad1843_reg_read(sc, AD1843_SERIAL_INTERFACE);
value &= ~(AD1843_ADRF_MASK | AD1843_ADLF_MASK);
value |= (format << AD1843_ADRF_SHIFT) |
(format << AD1843_ADLF_SHIFT);
ad1843_reg_write(sc, AD1843_SERIAL_INTERFACE, value);
sc->rec.format = format;
}
return (0);
}
int
mavb_set_params(void *hdl, int setmode, int usemode,
struct audio_params *play, struct audio_params *rec)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
int error;
DPRINTF(1, ("%s: mavb_set_params: sample=%ld precision=%d "
"channels=%d\n", sc->sc_dev.dv_xname, play->sample_rate,
play->precision, play->channels));
if (setmode & AUMODE_PLAY) {
switch (play->encoding) {
case AUDIO_ENCODING_ULAW:
case AUDIO_ENCODING_ALAW:
case AUDIO_ENCODING_ULINEAR_BE:
if (play->precision != 8)
return (EINVAL);
switch (play->channels) {
case 1:
play->factor = 8;
play->sw_code = ulinear8_to_ulinear24_be_mts;
break;
case 2:
play->factor = 4;
play->sw_code = ulinear8_to_ulinear24_be;
break;
default:
return (EINVAL);
}
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (play->precision == 16) {
switch (play->channels) {
case 1:
play->factor = 4;
play->sw_code =
linear16_to_linear24_be_mts;
break;
case 2:
play->factor = 2;
play->sw_code =
linear16_to_linear24_be;
break;
default:
return (EINVAL);
}
} else {
return (EINVAL);
}
break;
default:
return (EINVAL);
}
error = mavb_set_play_rate(sc, play->sample_rate);
if (error)
return (error);
error = mavb_set_play_format(sc, play->encoding);
if (error)
return (error);
play->bps = AUDIO_BPS(play->precision);
play->msb = 1;
}
if (setmode & AUMODE_RECORD) {
switch (rec->encoding) {
case AUDIO_ENCODING_ULAW:
case AUDIO_ENCODING_ALAW:
case AUDIO_ENCODING_ULINEAR_BE:
if (rec->precision != 8)
return (EINVAL);
rec->factor = 4;
rec->sw_code = ulinear24_to_ulinear8_be;
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (rec->precision == 16) {
rec->factor = 2;
rec->sw_code = linear24_to_linear16_be;
} else {
return (EINVAL);
}
break;
default:
return (EINVAL);
}
/* stereo to mono conversions not yet implemented */
rec->channels = 2;
error = mavb_set_rec_rate(sc, rec->sample_rate);
if (error)
return (error);
error = mavb_set_rec_format(sc, rec->encoding);
if (error)
return (error);
rec->bps = AUDIO_BPS(rec->precision);
rec->msb = 1;
}
return (0);
}
int
mavb_round_blocksize(void *hdl, int bs)
{
if (bs == 0)
bs = MAVB_CHAN_INTR_SIZE;
else
bs = (bs + MAVB_CHAN_INTR_SIZE - 1) &
~(MAVB_CHAN_INTR_SIZE - 1);
return (bs);
}
int
mavb_halt_output(void *hdl)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
DPRINTF(1, ("%s: mavb_halt_output called\n", sc->sc_dev.dv_xname));
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL2_CONTROL, 0);
return (0);
}
int
mavb_halt_input(void *hdl)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
DPRINTF(1, ("%s: mavb_halt_input called\n", sc->sc_dev.dv_xname));
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL1_CONTROL, 0);
return (0);
}
int
mavb_getdev(void *hdl, struct audio_device *ret)
{
*ret = mavb_device;
return (0);
}
int
mavb_set_port(void *hdl, struct mixer_ctrl *mc)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
u_char left, right;
ad1843_addr_t reg;
u_int16_t value;
DPRINTF(1, ("%s: mavb_set_port: dev=%d\n", sc->sc_dev.dv_xname,
mc->dev));
switch (mc->dev) {
case AD1843_ADC_SOURCE:
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
value &= ~(AD1843_LSS_MASK | AD1843_RSS_MASK);
value |= ((mc->un.ord << AD1843_LSS_SHIFT) & AD1843_LSS_MASK);
value |= ((mc->un.ord << AD1843_RSS_SHIFT) & AD1843_RSS_MASK);
ad1843_reg_write(sc, AD1843_ADC_SOURCE_GAIN, value);
break;
case AD1843_ADC_GAIN:
left = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
right = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
value &= ~(AD1843_LIG_MASK | AD1843_RIG_MASK);
value |= ((left >> 4) << AD1843_LIG_SHIFT);
value |= ((right >> 4) << AD1843_RIG_SHIFT);
ad1843_reg_write(sc, AD1843_ADC_SOURCE_GAIN, value);
break;
case AD1843_ADC_MIC_GAIN:
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
if (mc->un.ord == 0)
value &= ~(AD1843_LMGE | AD1843_RMGE);
else
value |= (AD1843_LMGE | AD1843_RMGE);
ad1843_reg_write(sc, AD1843_ADC_SOURCE_GAIN, value);
break;
case AD1843_DAC1_GAIN:
left = AUDIO_MAX_GAIN -
mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
right = AUDIO_MAX_GAIN -
mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
value &= ~(AD1843_LDA1G_MASK | AD1843_RDA1G_MASK);
value |= ((left >> 2) << AD1843_LDA1G_SHIFT);
value |= ((right >> 2) << AD1843_RDA1G_SHIFT);
ad1843_reg_write(sc, AD1843_DAC1_ANALOG_GAIN, value);
break;
case AD1843_DAC1_MUTE:
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
if (mc->un.ord == 0)
value &= ~(AD1843_LDA1GM | AD1843_RDA1GM);
else
value |= (AD1843_LDA1GM | AD1843_RDA1GM);
ad1843_reg_write(sc, AD1843_DAC1_ANALOG_GAIN, value);
break;
case AD1843_DAC2_GAIN:
case AD1843_AUX1_GAIN:
case AD1843_AUX2_GAIN:
case AD1843_AUX3_GAIN:
case AD1843_MIC_GAIN:
left = AUDIO_MAX_GAIN -
mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
right = AUDIO_MAX_GAIN -
mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
reg = AD1843_DAC2_TO_MIXER + mc->dev - AD1843_DAC2_GAIN;
value = ad1843_reg_read(sc, reg);
value &= ~(AD1843_LD2M_MASK | AD1843_RD2M_MASK);
value |= ((left >> 3) << AD1843_LD2M_SHIFT);
value |= ((right >> 3) << AD1843_RD2M_SHIFT);
ad1843_reg_write(sc, reg, value);
break;
case AD1843_MONO_GAIN:
left = AUDIO_MAX_GAIN -
mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
value &= ~AD1843_MNM_MASK;
value |= ((left >> 3) << AD1843_MNM_SHIFT);
ad1843_reg_write(sc, AD1843_MISC_SETTINGS, value);
break;
case AD1843_DAC2_MUTE:
case AD1843_AUX1_MUTE:
case AD1843_AUX2_MUTE:
case AD1843_AUX3_MUTE:
case AD1843_MIC_MUTE:
case AD1843_MONO_MUTE: /* matches left channel */
reg = AD1843_DAC2_TO_MIXER + mc->dev - AD1843_DAC2_MUTE;
value = ad1843_reg_read(sc, reg);
if (mc->un.ord == 0)
value &= ~(AD1843_LD2MM | AD1843_RD2MM);
else
value |= (AD1843_LD2MM | AD1843_RD2MM);
ad1843_reg_write(sc, reg, value);
break;
case AD1843_SUM_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
if (mc->un.ord == 0)
value &= ~AD1843_SUMM;
else
value |= AD1843_SUMM;
ad1843_reg_write(sc, AD1843_MISC_SETTINGS, value);
break;
case AD1843_MNO_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
if (mc->un.ord == 0)
value &= ~AD1843_MNOM;
else
value |= AD1843_MNOM;
ad1843_reg_write(sc, AD1843_MISC_SETTINGS, value);
break;
case AD1843_HPO_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
if (mc->un.ord == 0)
value &= ~AD1843_HPOM;
else
value |= AD1843_HPOM;
ad1843_reg_write(sc, AD1843_MISC_SETTINGS, value);
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
break;
default:
return (EINVAL);
}
return (0);
}
int
mavb_get_port(void *hdl, struct mixer_ctrl *mc)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
u_char left, right;
ad1843_addr_t reg;
u_int16_t value;
DPRINTF(1, ("%s: mavb_get_port: dev=%d\n", sc->sc_dev.dv_xname,
mc->dev));
switch (mc->dev) {
case AD1843_ADC_SOURCE:
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
mc->un.ord = (value & AD1843_LSS_MASK) >> AD1843_LSS_SHIFT;
break;
case AD1843_ADC_GAIN:
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
left = (value & AD1843_LIG_MASK) >> AD1843_LIG_SHIFT;
right = (value & AD1843_RIG_MASK) >> AD1843_RIG_SHIFT;
mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
(left << 4) | left;
mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
(right << 4) | right;
break;
case AD1843_ADC_MIC_GAIN:
value = ad1843_reg_read(sc, AD1843_ADC_SOURCE_GAIN);
mc->un.ord = (value & AD1843_LMGE) ? 1 : 0;
break;
case AD1843_DAC1_GAIN:
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
left = (value & AD1843_LDA1G_MASK) >> AD1843_LDA1G_SHIFT;
right = (value & AD1843_RDA1G_MASK) >> AD1843_RDA1G_SHIFT;
mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
AUDIO_MAX_GAIN - (left << 2);
mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
AUDIO_MAX_GAIN - (right << 2);
break;
case AD1843_DAC1_MUTE:
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
mc->un.ord = (value & AD1843_LDA1GM) ? 1 : 0;
break;
case AD1843_DAC2_GAIN:
case AD1843_AUX1_GAIN:
case AD1843_AUX2_GAIN:
case AD1843_AUX3_GAIN:
case AD1843_MIC_GAIN:
reg = AD1843_DAC2_TO_MIXER + mc->dev - AD1843_DAC2_GAIN;
value = ad1843_reg_read(sc, reg);
left = (value & AD1843_LD2M_MASK) >> AD1843_LD2M_SHIFT;
right = (value & AD1843_RD2M_MASK) >> AD1843_RD2M_SHIFT;
mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
AUDIO_MAX_GAIN - (left << 3);
mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
AUDIO_MAX_GAIN - (right << 3);
break;
case AD1843_MONO_GAIN:
if (mc->un.value.num_channels != 1)
return (EINVAL);
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
left = (value & AD1843_MNM_MASK) >> AD1843_MNM_SHIFT;
mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
AUDIO_MAX_GAIN - (left << 3);
break;
case AD1843_DAC2_MUTE:
case AD1843_AUX1_MUTE:
case AD1843_AUX2_MUTE:
case AD1843_AUX3_MUTE:
case AD1843_MIC_MUTE:
case AD1843_MONO_MUTE: /* matches left channel */
reg = AD1843_DAC2_TO_MIXER + mc->dev - AD1843_DAC2_MUTE;
value = ad1843_reg_read(sc, reg);
mc->un.ord = (value & AD1843_LD2MM) ? 1 : 0;
break;
case AD1843_SUM_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
mc->un.ord = (value & AD1843_SUMM) ? 1 : 0;
break;
case AD1843_MNO_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
mc->un.ord = (value & AD1843_MNOM) ? 1 : 0;
break;
case AD1843_HPO_MUTE:
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
mc->un.ord = (value & AD1843_HPOM) ? 1 : 0;
break;
default:
return (EINVAL);
}
return (0);
}
int
mavb_query_devinfo(void *hdl, struct mixer_devinfo *di)
{
int i;
di->prev = di->next = AUDIO_MIXER_LAST;
switch (di->index) {
case AD1843_RECORD_CLASS:
di->type = AUDIO_MIXER_CLASS;
di->mixer_class = AD1843_RECORD_CLASS;
strlcpy(di->label.name, AudioCrecord, sizeof di->label.name);
break;
case AD1843_ADC_SOURCE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_RECORD_CLASS;
di->next = AD1843_ADC_GAIN;
strlcpy(di->label.name, AudioNsource, sizeof di->label.name);
di->un.e.num_mem =
sizeof ad1843_source / sizeof ad1843_source[1];
for (i = 0; i < di->un.e.num_mem; i++) {
strlcpy(di->un.e.member[i].label.name,
ad1843_source[i],
sizeof di->un.e.member[0].label.name);
di->un.e.member[i].ord = i;
}
break;
case AD1843_ADC_GAIN:
di->type = AUDIO_MIXER_VALUE;
di->mixer_class = AD1843_RECORD_CLASS;
di->prev = AD1843_ADC_SOURCE;
strlcpy(di->label.name, AudioNvolume, sizeof di->label.name);
di->un.v.num_channels = 2;
strlcpy(di->un.v.units.name, AudioNvolume,
sizeof di->un.v.units.name);
break;
case AD1843_ADC_MIC_GAIN:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_RECORD_CLASS;
strlcpy(di->label.name, AudioNmicrophone "." AudioNpreamp,
sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
case AD1843_INPUT_CLASS:
di->type = AUDIO_MIXER_CLASS;
di->mixer_class = AD1843_INPUT_CLASS;
strlcpy(di->label.name, AudioCinputs, sizeof di->label.name);
break;
case AD1843_DAC1_GAIN:
di->type = AUDIO_MIXER_VALUE;
di->mixer_class = AD1843_INPUT_CLASS;
di->next = AD1843_DAC1_MUTE;
strlcpy(di->label.name, AudioNdac "1", sizeof di->label.name);
di->un.v.num_channels = 2;
strlcpy(di->un.v.units.name, AudioNvolume,
sizeof di->un.v.units.name);
break;
case AD1843_DAC1_MUTE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_INPUT_CLASS;
di->prev = AD1843_DAC1_GAIN;
strlcpy(di->label.name, AudioNmute, sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
case AD1843_DAC2_GAIN:
case AD1843_AUX1_GAIN:
case AD1843_AUX2_GAIN:
case AD1843_AUX3_GAIN:
case AD1843_MIC_GAIN:
case AD1843_MONO_GAIN:
di->type = AUDIO_MIXER_VALUE;
di->mixer_class = AD1843_INPUT_CLASS;
di->next = di->index + AD1843_DAC2_MUTE - AD1843_DAC2_GAIN;
strlcpy(di->label.name,
ad1843_input[di->index - AD1843_DAC2_GAIN],
sizeof di->label.name);
if (di->index == AD1843_MONO_GAIN)
di->un.v.num_channels = 1;
else
di->un.v.num_channels = 2;
strlcpy(di->un.v.units.name, AudioNvolume,
sizeof di->un.v.units.name);
break;
case AD1843_DAC2_MUTE:
case AD1843_AUX1_MUTE:
case AD1843_AUX2_MUTE:
case AD1843_AUX3_MUTE:
case AD1843_MIC_MUTE:
case AD1843_MONO_MUTE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_INPUT_CLASS;
di->prev = di->index + AD1843_DAC2_GAIN - AD1843_DAC2_MUTE;
strlcpy(di->label.name, AudioNmute, sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
case AD1843_SUM_MUTE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_INPUT_CLASS;
strlcpy(di->label.name, "sum." AudioNmute,
sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
case AD1843_OUTPUT_CLASS:
di->type = AUDIO_MIXER_CLASS;
di->mixer_class = AD1843_OUTPUT_CLASS;
strlcpy(di->label.name, AudioCoutputs, sizeof di->label.name);
break;
case AD1843_MNO_MUTE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_OUTPUT_CLASS;
strlcpy(di->label.name, AudioNmono "." AudioNmute,
sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
case AD1843_HPO_MUTE:
di->type = AUDIO_MIXER_ENUM;
di->mixer_class = AD1843_OUTPUT_CLASS;
strlcpy(di->label.name, AudioNheadphone "." AudioNmute,
sizeof di->label.name);
di->un.e.num_mem = 2;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
sizeof di->un.e.member[0].label.name);
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[1].label.name, AudioNon,
sizeof di->un.e.member[1].label.name);
di->un.e.member[1].ord = 1;
break;
default:
return (EINVAL);
}
return (0);
}
int
mavb_get_props(void *hdl)
{
return (AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT);
}
static void
mavb_dma_output(struct mavb_softc *sc)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
u_int64_t write_ptr;
caddr_t src, dst, end;
int count;
write_ptr = bus_space_read_8(st, sh, MAVB_CHANNEL2_WRITE_PTR);
end = sc->play.hw_start + MAVB_CHAN_RING_SIZE;
dst = sc->play.hw_start + write_ptr;
src = sc->play.sw_cur;
if (write_ptr % MAVB_CHAN_CHUNK_SIZE) {
printf("%s: write_ptr=%d\n", sc->sc_dev.dv_xname, write_ptr);
return;
}
if ((src - sc->play.sw_start) % MAVB_CHAN_CHUNK_SIZE) {
printf("%s: src=%d\n", sc->sc_dev.dv_xname,
src - sc->play.sw_start);
return;
}
count = MAVB_CHAN_INTR_SIZE / MAVB_CHAN_CHUNK_SIZE;
while (--count >= 0) {
memcpy(dst, src, MAVB_CHAN_CHUNK_SIZE);
dst += MAVB_CHAN_CHUNK_SIZE;
src += MAVB_CHAN_CHUNK_SIZE;
if (dst >= end)
dst = sc->play.hw_start;
if (src >= sc->play.sw_end)
src = sc->play.sw_start;
if (!((src - sc->play.sw_start) % sc->play.blksize)) {
if (sc->play.intr)
sc->play.intr(sc->play.intrarg);
}
}
write_ptr = dst - sc->play.hw_start;
bus_space_write_8(st, sh, MAVB_CHANNEL2_WRITE_PTR, write_ptr);
sc->play.sw_cur = src;
}
static void
mavb_dma_input(struct mavb_softc *sc)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
u_int64_t read_ptr;
caddr_t src, dst, end;
int count;
read_ptr = bus_space_read_8(st, sh, MAVB_CHANNEL1_READ_PTR);
end = sc->rec.hw_start + MAVB_CHAN_RING_SIZE;
src = sc->rec.hw_start + read_ptr;
dst = sc->rec.sw_cur;
if (read_ptr % MAVB_CHAN_CHUNK_SIZE) {
printf("%s: read_ptr=%d\n", sc->sc_dev.dv_xname, read_ptr);
return;
}
if ((dst - sc->rec.sw_start) % MAVB_CHAN_CHUNK_SIZE) {
printf("%s: dst=%d\n", sc->sc_dev.dv_xname,
dst - sc->rec.sw_start);
return;
}
count = MAVB_CHAN_INTR_SIZE / MAVB_CHAN_CHUNK_SIZE;
while (--count >= 0) {
memcpy(dst, src, MAVB_CHAN_CHUNK_SIZE);
dst += MAVB_CHAN_CHUNK_SIZE;
src += MAVB_CHAN_CHUNK_SIZE;
if (src >= end)
src = sc->rec.hw_start;
if (dst >= sc->rec.sw_end)
dst = sc->rec.sw_start;
if (!((dst - sc->rec.sw_start) % sc->rec.blksize)) {
if (sc->rec.intr)
sc->rec.intr(sc->rec.intrarg);
}
}
read_ptr = src - sc->rec.hw_start;
bus_space_write_8(st, sh, MAVB_CHANNEL1_READ_PTR, read_ptr);
sc->rec.sw_cur = dst;
}
int
mavb_trigger_output(void *hdl, void *start, void *end, int blksize,
void (*intr)(void *), void *intrarg, struct audio_params *param)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
DPRINTF(1, ("%s: mavb_trigger_output: start=%p end=%p "
"blksize=%d intr=%p(%p)\n", sc->sc_dev.dv_xname,
start, end, blksize, intr, intrarg));
sc->play.blksize = blksize;
sc->play.intr = intr;
sc->play.intrarg = intrarg;
sc->play.sw_start = sc->play.sw_cur = start;
sc->play.sw_end = end;
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL2_CONTROL,
MAVB_CHANNEL_RESET);
delay(1000);
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL2_CONTROL, 0);
/* Fill first 25% of buffer with silence. */
bzero(sc->play.hw_start, MAVB_CHAN_CHUNK_SIZE);
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL2_WRITE_PTR,
MAVB_CHAN_CHUNK_SIZE);
/* Fill next 50% of buffer with audio data. */
mavb_dma_output(sc);
/* The buffer is now 75% full. Start DMA and get interrupts
* when the buffer is 25% full. The interrupt handler fills
* in 50% of the buffer size, putting it back to 75% full.
*/
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL2_CONTROL,
MAVB_CHANNEL_DMA_ENABLE | MAVB_CHANNEL_INT_25);
return (0);
}
int
mavb_trigger_input(void *hdl, void *start, void *end, int blksize,
void (*intr)(void *), void *intrarg, struct audio_params *param)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
DPRINTF(1, ("%s: mavb_trigger_output: start=%p end=%p "
"blksize=%d intr=%p(%p)\n", sc->sc_dev.dv_xname,
start, end, blksize, intr, intrarg));
sc->rec.blksize = blksize;
sc->rec.intr = intr;
sc->rec.intrarg = intrarg;
sc->rec.sw_start = sc->rec.sw_cur = start;
sc->rec.sw_end = end;
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL1_CONTROL,
MAVB_CHANNEL_RESET);
delay(1000);
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL1_CONTROL, 0);
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CHANNEL1_CONTROL,
MAVB_CHANNEL_DMA_ENABLE | MAVB_CHANNEL_INT_50);
return (0);
}
static void
mavb_button_repeat(void *hdl)
{
struct mavb_softc *sc = (struct mavb_softc *)hdl;
u_int64_t intmask, control;
u_int16_t value, left, right;
DPRINTF(1, ("%s: mavb_repeat called\n", sc->sc_dev.dv_xname));
#define MAVB_CONTROL_VOLUME_BUTTONS \
(MAVB_CONTROL_VOLUME_BUTTON_UP | MAVB_CONTROL_VOLUME_BUTTON_DOWN)
control = bus_space_read_8(sc->sc_st, sc->sc_sh, MAVB_CONTROL);
if (control & MAVB_CONTROL_VOLUME_BUTTONS) {
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
left = (value & AD1843_LDA1G_MASK) >> AD1843_LDA1G_SHIFT;
right = (value & AD1843_RDA1G_MASK) >> AD1843_RDA1G_SHIFT;
if (control & MAVB_CONTROL_VOLUME_BUTTON_UP) {
control &= ~MAVB_CONTROL_VOLUME_BUTTON_UP;
if (left > 0)
left--; /* attenuation! */
if (right > 0)
right--;
}
if (control & MAVB_CONTROL_VOLUME_BUTTON_DOWN) {
control &= ~MAVB_CONTROL_VOLUME_BUTTON_DOWN;
if (left < 63)
left++;
if (right < 63)
right++;
}
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CONTROL, control);
value &= ~(AD1843_LDA1G_MASK | AD1843_RDA1G_MASK);
value |= (left << AD1843_LDA1G_SHIFT);
value |= (right << AD1843_RDA1G_SHIFT);
ad1843_reg_write(sc, AD1843_DAC1_ANALOG_GAIN, value);
timeout_add_msec(&sc->sc_volume_button_to,
MAVB_VOLUME_BUTTON_REPEAT_DELN);
} else {
/* Enable volume button interrupts again. */
intmask = bus_space_read_8(sc->sc_st, sc->sc_isash,
MACE_ISA_INT_MASK);
bus_space_write_8(sc->sc_st, sc->sc_isash, MACE_ISA_INT_MASK,
intmask | MACE_ISA_INT_AUDIO_SC);
}
}
static int
mavb_intr(void *arg)
{
struct mavb_softc *sc = arg;
u_int64_t intstat, intmask;
intstat = bus_space_read_8(sc->sc_st, sc->sc_isash, MACE_ISA_INT_STAT);
DPRINTF(MAVB_DEBUG_INTR, ("%s: mavb_intr: intstat = 0x%lx\n",
sc->sc_dev.dv_xname, intstat));
if (intstat & MACE_ISA_INT_AUDIO_SC) {
/* Disable volume button interrupts. */
intmask = bus_space_read_8(sc->sc_st, sc->sc_isash,
MACE_ISA_INT_MASK);
bus_space_write_8(sc->sc_st, sc->sc_isash, MACE_ISA_INT_MASK,
intmask & ~MACE_ISA_INT_AUDIO_SC);
timeout_add_msec(&sc->sc_volume_button_to,
MAVB_VOLUME_BUTTON_REPEAT_DEL1);
}
if (intstat & MACE_ISA_INT_AUDIO_DMA1)
mavb_dma_input(sc);
if (intstat & MACE_ISA_INT_AUDIO_DMA2)
mavb_dma_output(sc);
return 1;
}
int
mavb_match(struct device *parent, void *match, void *aux)
{
struct macebus_attach_args *maa = aux;
bus_space_handle_t ioh;
u_int64_t control;
if (bus_space_map(maa->maa_iot, maa->maa_baseaddr, MAVB_NREGS, 0,
&ioh) != 0)
return (0);
control = bus_space_read_8(maa->maa_iot, ioh, MAVB_CONTROL);
bus_space_unmap(maa->maa_iot, ioh, MAVB_NREGS);
return ((control & MAVB_CONTROL_CODEC_PRESENT) != 0);
}
void
mavb_attach(struct device *parent, struct device *self, void *aux)
{
struct mavb_softc *sc = (void *)self;
struct macebus_attach_args *maa = aux;
bus_dma_segment_t seg;
u_int16_t value;
int rseg;
sc->sc_st = maa->maa_iot;
if (bus_space_map(sc->sc_st, maa->maa_baseaddr, MAVB_NREGS, 0,
&sc->sc_sh) != 0) {
printf(": can't map i/o space\n");
return;
}
/* XXX We need access to some of the MACE ISA registers. */
extern bus_space_handle_t mace_h;
bus_space_subregion(sc->sc_st, mace_h, 0, MAVB_ISA_NREGS,
&sc->sc_isash);
/* Set up DMA structures. */
sc->sc_dmat = maa->maa_dmat;
if (bus_dmamap_create(sc->sc_dmat, MAVB_ISA_RING_SIZE, 1,
MAVB_ISA_RING_SIZE, 0, 0, &sc->sc_dmamap)) {
printf(": can't create MACE ISA DMA map\n");
return;
}
if (bus_dmamem_alloc(sc->sc_dmat, MAVB_ISA_RING_SIZE,
MACE_ISA_RING_ALIGN, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
printf(": can't allocate ring buffer\n");
return;
}
if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MAVB_ISA_RING_SIZE,
&sc->sc_ring, BUS_DMA_COHERENT)) {
printf(": can't map ring buffer\n");
return;
}
if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ring,
MAVB_ISA_RING_SIZE, NULL, BUS_DMA_NOWAIT)) {
printf(": can't load MACE ISA DMA map\n");
return;
}
sc->rec.hw_start = sc->sc_ring;
sc->play.hw_start = sc->sc_ring + MAVB_CHAN_RING_SIZE;
bus_space_write_8(sc->sc_st, sc->sc_isash, MACE_ISA_RING_BASE,
sc->sc_dmamap->dm_segs[0].ds_addr);
/* Establish interrupt. */
macebus_intr_establish(maa->maa_intr, maa->maa_mace_intr,
IST_EDGE, IPL_AUDIO, mavb_intr, sc, sc->sc_dev.dv_xname);
/* 2. Assert the RESET signal. */
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CONTROL,
MAVB_CONTROL_RESET);
delay(1); /* at least 100 ns */
/* 3. Deassert the RESET signal and enter a wait period to
allow the AD1843 internal clocks and the external
crystal oscillator to stabilize. */
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CONTROL, 0);
delay(800); /* typically 400 us to 800 us */
if (ad1843_reg_read(sc, AD1843_CODEC_STATUS) & AD1843_INIT) {
printf(": codec not ready\n");
return;
}
/* 4. Put the conversion sources into standby. */
value = ad1843_reg_read(sc, AD1843_FUNDAMENTAL_SETTINGS);
ad1843_reg_write(sc, AD1843_FUNDAMENTAL_SETTINGS,
value & ~AD1843_PDNI);
delay (500000); /* approximately 474 ms */
if (ad1843_reg_read(sc, AD1843_CODEC_STATUS) & AD1843_PDNO) {
printf(": can't power up conversion resources\n");
return;
}
/* 5. Power up the clock generators and enable clock output pins. */
value = ad1843_reg_read(sc, AD1843_FUNDAMENTAL_SETTINGS);
ad1843_reg_write(sc, AD1843_FUNDAMENTAL_SETTINGS,
value | AD1843_C1EN | AD1843_C2EN);
/* 6. Configure conversion resources while they are in standby. */
value = ad1843_reg_read(sc, AD1843_SERIAL_INTERFACE);
ad1843_reg_write(sc, AD1843_SERIAL_INTERFACE, value | AD1843_ADTLK);
value = ad1843_reg_read(sc, AD1843_CHANNEL_SAMPLE_RATE);
ad1843_reg_write(sc, AD1843_CHANNEL_SAMPLE_RATE,
value | (2 << AD1843_DA1C_SHIFT) |
(1 << AD1843_ADRC_SHIFT) | (1 << AD1843_ADLC_SHIFT));
/* 7. Enable conversion resources. */
value = ad1843_reg_read(sc, AD1843_CHANNEL_POWER_DOWN);
ad1843_reg_write(sc, AD1843_CHANNEL_POWER_DOWN,
value | (AD1843_DA1EN | AD1843_ANAEN | AD1843_AAMEN |
AD1843_ADREN | AD1843_ADLEN));
/* 8. Configure conversion resources while they are enabled. */
value = ad1843_reg_read(sc, AD1843_DAC1_ANALOG_GAIN);
ad1843_reg_write(sc, AD1843_DAC1_ANALOG_GAIN,
value & ~(AD1843_LDA1GM | AD1843_RDA1GM));
value = ad1843_reg_read(sc, AD1843_DAC1_DIGITAL_GAIN);
ad1843_reg_write(sc, AD1843_DAC1_DIGITAL_GAIN,
value & ~(AD1843_LDA1AM | AD1843_RDA1AM));
value = ad1843_reg_read(sc, AD1843_MISC_SETTINGS);
ad1843_reg_write(sc, AD1843_MISC_SETTINGS,
value & ~(AD1843_HPOM | AD1843_MNOM));
value = ad1843_reg_read(sc, AD1843_CODEC_STATUS);
printf(": AD1843 rev %d\n", (u_int)value & AD1843_REVISION_MASK);
sc->play.rate = sc->rec.rate = 48000;
sc->play.format = sc->rec.format = AD1843_PCM8;
timeout_set(&sc->sc_volume_button_to, mavb_button_repeat, sc);
audio_attach_mi(&mavb_sa_hw_if, sc, &sc->sc_dev);
return;
}
u_int16_t
ad1843_reg_read(struct mavb_softc *sc, ad1843_addr_t addr)
{
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CODEC_CONTROL,
(addr & MAVB_CODEC_ADDRESS_MASK) << MAVB_CODEC_ADDRESS_SHIFT |
MAVB_CODEC_READ);
delay(200);
return bus_space_read_8(sc->sc_st, sc->sc_sh, MAVB_CODEC_STATUS);
}
u_int16_t
ad1843_reg_write(struct mavb_softc *sc, ad1843_addr_t addr, u_int16_t value)
{
bus_space_write_8(sc->sc_st, sc->sc_sh, MAVB_CODEC_CONTROL,
(addr & MAVB_CODEC_ADDRESS_MASK) << MAVB_CODEC_ADDRESS_SHIFT |
(value & MAVB_CODEC_WORD_MASK) << MAVB_CODEC_WORD_SHIFT);
delay(200);
return bus_space_read_8(sc->sc_st, sc->sc_sh, MAVB_CODEC_STATUS);
}
void
ad1843_dump_regs(struct mavb_softc *sc)
{
u_int16_t addr;
for (addr = 0; addr < AD1843_NREGS; addr++)
printf("%d: 0x%04x\n", addr, ad1843_reg_read(sc, addr));
}
|