awacs.c revision 1.46.2.1 1 /* $NetBSD: awacs.c,v 1.46.2.1 2019/04/21 05:11:21 isaki Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: awacs.c,v 1.46.2.1 2019/04/21 05:11:21 isaki Exp $");
31
32 #include <sys/param.h>
33 #include <sys/audioio.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/kthread.h>
37 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40
41 #include <dev/auconv.h>
42 #include <dev/audio_if.h>
43 #include <dev/mulaw.h>
44
45 #include <uvm/uvm_extern.h>
46 #include <machine/autoconf.h>
47 #include <machine/pio.h>
48
49 #include <dev/ofw/openfirm.h>
50 #include <macppc/dev/dbdma.h>
51
52 #include <dev/i2c/sgsmixvar.h>
53 #include "sgsmix.h"
54 #include "opt_awacs.h"
55
56 #ifdef AWACS_DEBUG
57 # define DPRINTF printf
58 #else
59 # define DPRINTF while (0) printf
60 #endif
61
62 /* sc_flags values */
63 #define AWACS_CAP_BSWAP 0x0001
64
65 struct awacs_softc {
66 device_t sc_dev;
67 int sc_flags;
68 bus_space_tag_t sc_tag;
69 bus_space_handle_t sc_regh;
70 bus_space_handle_t sc_idmah;
71 bus_space_handle_t sc_odmah;
72 void (*sc_ointr)(void *); /* DMA completion intr handler */
73 void *sc_oarg; /* arg for sc_ointr() */
74 int sc_opages; /* # of output pages */
75
76 void (*sc_iintr)(void *); /* DMA completion intr handler */
77 void *sc_iarg; /* arg for sc_iintr() */
78
79 uint32_t sc_record_source; /* recording source mask */
80 uint32_t sc_output_mask; /* output mask */
81 uint32_t sc_headphones_mask; /* which reading of the gpio means */
82 uint32_t sc_headphones_in; /* headphones are present */
83
84 int sc_screamer;
85 int sc_have_perch;
86 int vol_l, vol_r;
87 int sc_bass, sc_treble;
88 lwp_t *sc_thread;
89 kcondvar_t sc_event;
90 int sc_output_wanted;
91 int sc_need_parallel_output;
92 #if NSGSMIX > 0
93 device_t sc_sgsmix;
94 #endif
95
96 u_int sc_codecctl0;
97 u_int sc_codecctl1;
98 u_int sc_codecctl2;
99 u_int sc_codecctl4;
100 u_int sc_codecctl5;
101 u_int sc_codecctl6;
102 u_int sc_codecctl7;
103 u_int sc_soundctl;
104
105 struct dbdma_regmap *sc_odma;
106 struct dbdma_regmap *sc_idma;
107 struct dbdma_command *sc_odmacmd;
108 struct dbdma_command *sc_idmacmd;
109
110 #define AWACS_NFORMATS 2
111 struct audio_format sc_formats[AWACS_NFORMATS];
112
113 kmutex_t sc_lock;
114 kmutex_t sc_intr_lock;
115 };
116
117 static int awacs_match(device_t, struct cfdata *, void *);
118 static void awacs_attach(device_t, device_t, void *);
119 static int awacs_intr(void *);
120 static int awacs_status_intr(void *);
121
122 static void awacs_close(void *);
123 static int awacs_query_encoding(void *, struct audio_encoding *);
124 static int awacs_set_params(void *, int, int, audio_params_t *, audio_params_t *,
125 stream_filter_list_t *, stream_filter_list_t *);
126
127 static int awacs_round_blocksize(void *, int, int, const audio_params_t *);
128 static int awacs_trigger_output(void *, void *, void *, int, void (*)(void *),
129 void *, const audio_params_t *);
130 static int awacs_trigger_input(void *, void *, void *, int, void (*)(void *),
131 void *, const audio_params_t *);
132 static int awacs_halt_output(void *);
133 static int awacs_halt_input(void *);
134 static int awacs_getdev(void *, struct audio_device *);
135 static int awacs_set_port(void *, mixer_ctrl_t *);
136 static int awacs_get_port(void *, mixer_ctrl_t *);
137 static int awacs_query_devinfo(void *, mixer_devinfo_t *);
138 static size_t awacs_round_buffersize(void *, int, size_t);
139 static paddr_t awacs_mappage(void *, void *, off_t, int);
140 static int awacs_get_props(void *);
141 static void awacs_get_locks(void *, kmutex_t **, kmutex_t **);
142
143 static inline u_int awacs_read_reg(struct awacs_softc *, int);
144 static inline void awacs_write_reg(struct awacs_softc *, int, int);
145 static void awacs_write_codec(struct awacs_softc *, int);
146
147 void awacs_set_volume(struct awacs_softc *, int, int);
148 static void awacs_set_speaker_volume(struct awacs_softc *, int, int);
149 static void awacs_set_ext_volume(struct awacs_softc *, int, int);
150 static void awacs_set_loopthrough_volume(struct awacs_softc *, int, int);
151 static int awacs_set_rate(struct awacs_softc *, const audio_params_t *);
152 static void awacs_select_output(struct awacs_softc *, int);
153 static int awacs_check_headphones(struct awacs_softc *);
154 static void awacs_thread(void *);
155
156 #if NSGSMIX > 0
157 static void awacs_set_bass(struct awacs_softc *, int);
158 static void awacs_set_treble(struct awacs_softc *, int);
159 #endif
160 static int awacs_setup_sgsmix(device_t);
161
162 CFATTACH_DECL_NEW(awacs, sizeof(struct awacs_softc),
163 awacs_match, awacs_attach, NULL, NULL);
164
165 const struct audio_hw_if awacs_hw_if = {
166 .close = awacs_close,
167 .query_encoding = awacs_query_encoding,
168 .set_params = awacs_set_params,
169 .round_blocksize = awacs_round_blocksize,
170 .halt_output = awacs_halt_output,
171 .halt_input = awacs_halt_input,
172 .getdev = awacs_getdev,
173 .set_port = awacs_set_port,
174 .get_port = awacs_get_port,
175 .query_devinfo = awacs_query_devinfo,
176 .round_buffersize = awacs_round_buffersize,
177 .mappage = awacs_mappage,
178 .get_props = awacs_get_props,
179 .trigger_output = awacs_trigger_output,
180 .trigger_input = awacs_trigger_input,
181 .get_locks = awacs_get_locks,
182 };
183
184 struct audio_device awacs_device = {
185 "AWACS",
186 "",
187 "awacs"
188 };
189
190 #define AWACS_NFORMATS 2
191 #define AWACS_FORMATS_LE 0
192 #define AWACS_FORMAT(enc) \
193 { \
194 .mode = AUMODE_PLAY | AUMODE_RECORD, \
195 .encoding = (enc), \
196 .validbits = 16, \
197 .precision = 16, \
198 .channels = 2, \
199 .channel_mask = AUFMT_STEREO, \
200 .frequency_type = 8, \
201 .frequency = \
202 { 7350, 8820, 11025, 14700, 17640, 22050, 29400, 44100 }, \
203 }
204 static const struct audio_format awacs_formats[AWACS_NFORMATS] = {
205 AWACS_FORMAT(AUDIO_ENCODING_SLINEAR_LE),
206 AWACS_FORMAT(AUDIO_ENCODING_SLINEAR_BE),
207 };
208
209 /* register offset */
210 #define AWACS_SOUND_CTRL 0x00
211 #define AWACS_CODEC_CTRL 0x10
212 #define AWACS_CODEC_STATUS 0x20
213 #define AWACS_CLIP_COUNT 0x30
214 #define AWACS_BYTE_SWAP 0x40
215
216 /* sound control */
217 #define AWACS_INPUT_SUBFRAME0 0x00000001
218 #define AWACS_INPUT_SUBFRAME1 0x00000002
219 #define AWACS_INPUT_SUBFRAME2 0x00000004
220 #define AWACS_INPUT_SUBFRAME3 0x00000008
221
222 #define AWACS_OUTPUT_SUBFRAME0 0x00000010
223 #define AWACS_OUTPUT_SUBFRAME1 0x00000020
224 #define AWACS_OUTPUT_SUBFRAME2 0x00000040
225 #define AWACS_OUTPUT_SUBFRAME3 0x00000080
226
227 #define AWACS_RATE_44100 0x00000000
228 #define AWACS_RATE_29400 0x00000100
229 #define AWACS_RATE_22050 0x00000200
230 #define AWACS_RATE_17640 0x00000300
231 #define AWACS_RATE_14700 0x00000400
232 #define AWACS_RATE_11025 0x00000500
233 #define AWACS_RATE_8820 0x00000600
234 #define AWACS_RATE_7350 0x00000700
235 #define AWACS_RATE_MASK 0x00000700
236
237 #define AWACS_ERROR 0x00000800
238 #define AWACS_PORTCHG 0x00001000
239 #define AWACS_INTR_ERROR 0x00002000 /* interrupt on error */
240 #define AWACS_INTR_PORTCHG 0x00004000 /* interrupt on port change */
241
242 #define AWACS_STATUS_SUBFRAME 0x00018000 /* mask */
243
244 /* codec control */
245 #define AWACS_CODEC_ADDR0 0x00000000
246 #define AWACS_CODEC_ADDR1 0x00001000
247 #define AWACS_CODEC_ADDR2 0x00002000
248 #define AWACS_CODEC_ADDR4 0x00004000
249 #define AWACS_CODEC_ADDR5 0x00005000
250 #define AWACS_CODEC_ADDR6 0x00006000
251 #define AWACS_CODEC_ADDR7 0x00007000
252 #define AWACS_CODEC_EMSEL0 0x00000000
253 #define AWACS_CODEC_EMSEL1 0x00400000
254 #define AWACS_CODEC_EMSEL2 0x00800000
255 #define AWACS_CODEC_EMSEL4 0x00c00000
256 #define AWACS_CODEC_BUSY 0x01000000
257
258 /* cc0 */
259 #define AWACS_DEFAULT_CD_GAIN 0x000000bb
260 #define AWACS_INPUT_CD 0x00000200
261 #define AWACS_INPUT_LINE 0x00000400
262 #define AWACS_INPUT_MICROPHONE 0x00000800
263 #define AWACS_INPUT_MASK 0x00000e00
264
265 /* cc1 */
266 #define AWACS_LOOP_THROUGH 0x00000040
267 #define AWACS_MUTE_SPEAKER 0x00000080
268 #define AWACS_MUTE_HEADPHONE 0x00000200
269 #define AWACS_PARALLEL_OUTPUT 0x00000c00
270
271 /* output */
272 #define OUTPUT_SPEAKER 1
273 #define OUTPUT_HEADPHONES 2
274
275 /* codec status */
276
277 static const char *screamer[] = {"screamer", NULL};
278
279 /*
280 * list machines that have the headphone detect GPIO reversed here.
281 * so far the only known case is the PowerBook 3400c and similar machines
282 */
283 static const char *detect_reversed[] = {"AAPL,3400/2400",
284 "AAPL,3500",
285 NULL};
286
287 static const char *use_gpio4[] = { "PowerMac3,1",
288 "PowerMac3,2",
289 "PowerMac3,3",
290 NULL};
291
292 /*
293 * list of machines that do not require AWACS_PARALLEL_OUTPUT
294 */
295 static const char *no_parallel_output[] = { "PowerBook3,1",
296 NULL};
297
298 static int
299 awacs_match(device_t parent, struct cfdata *match, void *aux)
300 {
301 struct confargs *ca;
302
303 ca = aux;
304
305 if (strcmp(ca->ca_name, "awacs") == 0 ||
306 strcmp(ca->ca_name, "davbus") == 0)
307 return 100;
308
309 if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
310 return 0;
311
312 if (strcmp(ca->ca_name, "i2s") == 0)
313 return 1;
314
315 return 0;
316 }
317
318 static void
319 awacs_attach(device_t parent, device_t self, void *aux)
320 {
321 struct awacs_softc *sc;
322 struct confargs *ca = aux;
323 int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
324 int len = -1, perch;
325 int root_node;
326 char compat[256];
327
328 sc = device_private(self);
329 sc->sc_dev = self;
330 sc->sc_tag = ca->ca_tag;
331
332 if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[0],
333 ca->ca_reg[1], 0, &sc->sc_regh) != 0)
334 printf("couldn't map codec registers\n");
335 if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[2],
336 ca->ca_reg[3], BUS_SPACE_MAP_LINEAR, &sc->sc_odmah) != 0)
337 printf("couldn't map DMA out registers\n");
338 if (bus_space_map(sc->sc_tag, ca->ca_baseaddr + ca->ca_reg[4],
339 ca->ca_reg[5], BUS_SPACE_MAP_LINEAR, &sc->sc_idmah) != 0)
340 printf("couldn't map DMA in registers\n");
341
342 sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
343 sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah);
344 sc->sc_odmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command), NULL);
345 sc->sc_idmacmd = dbdma_alloc(20 * sizeof(struct dbdma_command), NULL);
346
347 if (strcmp(ca->ca_name, "i2s") == 0) {
348 int node, intr[6];
349
350 node = OF_child(ca->ca_node);
351 if (node == 0) {
352 printf("no i2s-a child\n");
353 return;
354 }
355 if (OF_getprop(node, "interrupts", intr, sizeof(intr)) == -1) {
356 printf("no interrupt property\n");
357 return;
358 }
359
360 cirq = intr[0];
361 oirq = intr[2];
362 iirq = intr[4];
363 cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
364 oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
365 iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
366 } else if (ca->ca_nintr == 24) {
367 cirq = ca->ca_intr[0];
368 oirq = ca->ca_intr[2];
369 iirq = ca->ca_intr[4];
370 cirq_type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
371 oirq_type = ca->ca_intr[3] ? IST_LEVEL : IST_EDGE;
372 iirq_type = ca->ca_intr[5] ? IST_LEVEL : IST_EDGE;
373 } else {
374 cirq = ca->ca_intr[0];
375 oirq = ca->ca_intr[1];
376 iirq = ca->ca_intr[2];
377 cirq_type = oirq_type = iirq_type = IST_EDGE;
378 }
379
380 intr_establish(cirq, cirq_type, IPL_BIO, awacs_status_intr, sc);
381 intr_establish(oirq, oirq_type, IPL_AUDIO, awacs_intr, sc);
382 intr_establish(iirq, iirq_type, IPL_AUDIO, awacs_intr, sc);
383
384 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
385 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
386
387 cv_init(&sc->sc_event, "awacs_wait");
388
389 /* check if the chip is a screamer */
390 sc->sc_screamer = (of_compatible(ca->ca_node, screamer) != -1);
391 if (!sc->sc_screamer) {
392 /* look for 'sound' child node */
393 int sound_node;
394
395 sound_node = OF_child(ca->ca_node);
396 while ((sound_node != 0) && (!sc->sc_screamer)) {
397
398 sc->sc_screamer =
399 (of_compatible(sound_node, screamer) != -1);
400 sound_node = OF_peer(sound_node);
401 }
402 }
403
404 if (sc->sc_screamer) {
405 printf(" Screamer");
406 }
407
408 printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
409
410 sc->vol_l = 0;
411 sc->vol_r = 0;
412
413 memcpy(&sc->sc_formats, awacs_formats, sizeof(awacs_formats));
414
415 /* XXX Uni-North based models don't have byteswap capability. */
416 if (OF_finddevice("/uni-n") == -1) {
417
418 sc->sc_flags |= AWACS_CAP_BSWAP;
419 } else {
420
421 AUFMT_INVALIDATE(&sc->sc_formats[AWACS_FORMATS_LE]);
422 }
423
424 sc->sc_soundctl = AWACS_INPUT_SUBFRAME0 | AWACS_OUTPUT_SUBFRAME0 |
425 AWACS_RATE_44100 | AWACS_INTR_PORTCHG;
426 awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
427
428 sc->sc_codecctl0 = AWACS_CODEC_ADDR0 | AWACS_CODEC_EMSEL0;
429 sc->sc_codecctl1 = AWACS_CODEC_ADDR1 | AWACS_CODEC_EMSEL0;
430 sc->sc_codecctl2 = AWACS_CODEC_ADDR2 | AWACS_CODEC_EMSEL0;
431 sc->sc_codecctl4 = AWACS_CODEC_ADDR4 | AWACS_CODEC_EMSEL0;
432 sc->sc_codecctl5 = AWACS_CODEC_ADDR5 | AWACS_CODEC_EMSEL0;
433 sc->sc_codecctl6 = AWACS_CODEC_ADDR6 | AWACS_CODEC_EMSEL0;
434 sc->sc_codecctl7 = AWACS_CODEC_ADDR7 | AWACS_CODEC_EMSEL0;
435
436 sc->sc_codecctl0 |= AWACS_INPUT_CD | AWACS_DEFAULT_CD_GAIN;
437 awacs_write_codec(sc, sc->sc_codecctl0);
438
439 /* Set loopthrough for external mixer on beige G3 */
440 sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
441
442 printf("%s: ", device_xname(sc->sc_dev));
443
444 /*
445 * all(?) awacs have GPIOs to detect if there's something plugged into
446 * the headphone jack. The other GPIOs are either used for other jacks
447 * ( the PB3400c's microphone jack for instance ) or unused.
448 * The problem is that there are at least three different ways how
449 * those GPIOs are wired to the actual jacks.
450 * For now we bother only with headphone detection
451 */
452 perch = OF_finddevice("/perch");
453 root_node = OF_finddevice("/");
454 if (of_compatible(root_node, detect_reversed) != -1) {
455
456 /* 0x02 is for the microphone jack, high active */
457 /*
458 * for some reason the gpio for the headphones jack is low
459 * active on the PB3400 and similar machines
460 */
461 sc->sc_headphones_mask = 0x8;
462 sc->sc_headphones_in = 0x0;
463 } else if ((perch != -1) ||
464 (of_compatible(root_node, use_gpio4) != -1)) {
465 /*
466 * this is for the beige G3's 'personality card' which uses
467 * yet another wiring of the headphone detect GPIOs
468 * some G4s use it as well
469 */
470 sc->sc_headphones_mask = 0x04;
471 sc->sc_headphones_in = 0x04;
472 } else {
473 /* while on most machines it's high active as well */
474 sc->sc_headphones_mask = 0x8;
475 sc->sc_headphones_in = 0x8;
476 }
477
478 if (of_compatible(root_node, no_parallel_output) != -1)
479 sc->sc_need_parallel_output = 0;
480 else {
481 sc->sc_need_parallel_output = 1;
482 sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
483 }
484
485 if (awacs_check_headphones(sc)) {
486
487 /* default output to headphones */
488 printf("headphones\n");
489 sc->sc_output_mask = OUTPUT_HEADPHONES;
490 } else {
491
492 /* default output to speakers */
493 printf("speaker\n");
494 sc->sc_output_mask = OUTPUT_SPEAKER;
495 }
496 sc->sc_output_wanted = sc->sc_output_mask;
497 awacs_select_output(sc, sc->sc_output_mask);
498
499 delay(100);
500 if (sc->sc_screamer) {
501 awacs_write_codec(sc, sc->sc_codecctl6);
502 awacs_write_codec(sc, sc->sc_codecctl5);
503 delay(2);
504 awacs_write_codec(sc, sc->sc_codecctl1);
505 awacs_write_codec(sc, sc->sc_codecctl7);
506 }
507
508 /* default input from CD */
509 sc->sc_record_source = 1 << 0;
510 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
511 sc->sc_codecctl0 |= AWACS_INPUT_CD;
512 awacs_write_codec(sc, sc->sc_codecctl0);
513
514 /* Enable interrupts and looping mode. */
515 /* XXX ... */
516
517 sc->sc_codecctl1 |= AWACS_LOOP_THROUGH;
518 if (sc->sc_need_parallel_output)
519 sc->sc_codecctl1 |= AWACS_PARALLEL_OUTPUT;
520 awacs_write_codec(sc, sc->sc_codecctl1);
521
522 #if NSGSMIX > 0
523 sc->sc_sgsmix = NULL;
524 #endif
525 sc->sc_have_perch = 0;
526 if (perch != -1) {
527
528 len = OF_getprop(perch, "compatible", compat, 255);
529 if (len > 0) {
530 printf("%s: found '%s' personality card\n",
531 device_xname(sc->sc_dev), compat);
532 sc->sc_have_perch = 1;
533 config_finalize_register(sc->sc_dev,
534 awacs_setup_sgsmix);
535 }
536 }
537
538 /* Set initial volume[s] */
539 awacs_set_volume(sc, 144, 144);
540 awacs_set_loopthrough_volume(sc, 0, 0);
541
542 audio_attach_mi(&awacs_hw_if, sc, sc->sc_dev);
543
544 if (kthread_create(PRI_NONE, 0, NULL, awacs_thread, sc,
545 &sc->sc_thread, "%s", "awacs") != 0) {
546 printf("awacs: unable to create event kthread");
547 }
548 pmf_device_register(sc->sc_dev, NULL, NULL);
549 }
550
551 static int
552 awacs_setup_sgsmix(device_t cookie)
553 {
554 struct awacs_softc *sc = device_private(cookie);
555 #if NSGSMIX > 0
556 device_t dv;
557 deviter_t di;
558 #endif
559
560 if (!sc->sc_have_perch)
561 return 0;
562 #if NSGSMIX > 0
563 /* look for sgsmix */
564 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
565 dv != NULL;
566 dv = deviter_next(&di)) {
567 if (device_is_a(dv, "sgsmix")) {
568 sc->sc_sgsmix = dv;
569 break;
570 }
571 }
572 deviter_release(&di);
573 if (sc->sc_sgsmix == NULL)
574 return 0;
575
576 printf("%s: using %s\n", device_xname(sc->sc_dev),
577 device_xname(sc->sc_sgsmix));
578
579 sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
580 awacs_write_codec(sc, sc->sc_codecctl1);
581
582 awacs_select_output(sc, sc->sc_output_mask);
583 awacs_set_volume(sc, sc->vol_l, sc->vol_r);
584 awacs_set_bass(sc, 128);
585 awacs_set_treble(sc, 128);
586 cv_signal(&sc->sc_event);
587 #endif
588 return 0;
589 }
590
591
592 static inline u_int
593 awacs_read_reg(struct awacs_softc *sc, int reg)
594 {
595
596 return bus_space_read_4(sc->sc_tag, sc->sc_regh, reg);
597 }
598
599 static inline void
600 awacs_write_reg(struct awacs_softc *sc, int reg, int val)
601 {
602
603 bus_space_write_4(sc->sc_tag, sc->sc_regh, reg, val);
604 }
605
606 static void
607 awacs_write_codec(struct awacs_softc *sc, int value)
608 {
609
610 do {
611 delay(100);
612 } while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
613
614 awacs_write_reg(sc, AWACS_CODEC_CTRL, value);
615
616 do {
617 delay(100);
618 } while (awacs_read_reg(sc, AWACS_CODEC_CTRL) & AWACS_CODEC_BUSY);
619 }
620
621 static int
622 awacs_intr(void *v)
623 {
624 struct awacs_softc *sc;
625 struct dbdma_command *cmd;
626 int count;
627 int status;
628
629 sc = v;
630 mutex_spin_enter(&sc->sc_intr_lock);
631 cmd = sc->sc_odmacmd;
632 count = sc->sc_opages;
633 /* Fill used buffer(s). */
634 while (count-- > 0) {
635 /* if DBDMA_INT_ALWAYS */
636 if (in16rb(&cmd->d_command) & 0x30) { /* XXX */
637 status = in16rb(&cmd->d_status);
638 cmd->d_status = 0;
639 if (status) /* status == 0x8400 */
640 if (sc->sc_ointr)
641 (*sc->sc_ointr)(sc->sc_oarg);
642 }
643 cmd++;
644 }
645 mutex_spin_exit(&sc->sc_intr_lock);
646
647 return 1;
648 }
649
650 /*
651 * Close function is called at splaudio().
652 */
653 static void
654 awacs_close(void *h)
655 {
656 struct awacs_softc *sc;
657
658 sc = h;
659 awacs_halt_output(sc);
660 awacs_halt_input(sc);
661
662 sc->sc_ointr = 0;
663 sc->sc_iintr = 0;
664 }
665
666 static int
667 awacs_query_encoding(void *h, struct audio_encoding *ae)
668 {
669 struct awacs_softc *sc;
670
671 sc = h;
672 ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
673
674 switch (ae->index) {
675 case 0:
676 strcpy(ae->name, AudioEslinear);
677 ae->encoding = AUDIO_ENCODING_SLINEAR;
678 ae->precision = 16;
679 ae->flags = 0;
680 return 0;
681 case 1:
682 strcpy(ae->name, AudioEslinear_be);
683 ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
684 ae->precision = 16;
685 ae->flags = 0;
686 return 0;
687 case 2:
688 strcpy(ae->name, AudioEslinear_le);
689 ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
690 ae->precision = 16;
691 if (sc->sc_flags & AWACS_CAP_BSWAP)
692 ae->flags = 0;
693 return 0;
694 case 3:
695 strcpy(ae->name, AudioEulinear_be);
696 ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
697 ae->precision = 16;
698 return 0;
699 case 4:
700 strcpy(ae->name, AudioEulinear_le);
701 ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
702 ae->precision = 16;
703 return 0;
704 case 5:
705 strcpy(ae->name, AudioEmulaw);
706 ae->encoding = AUDIO_ENCODING_ULAW;
707 ae->precision = 8;
708 return 0;
709 case 6:
710 strcpy(ae->name, AudioEalaw);
711 ae->encoding = AUDIO_ENCODING_ALAW;
712 ae->precision = 8;
713 return 0;
714 default:
715 return EINVAL;
716 }
717 }
718
719 static int
720 awacs_set_params(void *h, int setmode, int usemode,
721 audio_params_t *play, audio_params_t *rec,
722 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
723 {
724 struct awacs_softc *sc;
725 audio_params_t *p;
726 stream_filter_list_t *fil;
727 int mode, i;
728
729 sc = h;
730 p = NULL;
731 /*
732 * This device only has one clock, so make the sample rates match.
733 */
734 if (play->sample_rate != rec->sample_rate &&
735 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
736 if (setmode == AUMODE_PLAY) {
737 rec->sample_rate = play->sample_rate;
738 setmode |= AUMODE_RECORD;
739 } else if (setmode == AUMODE_RECORD) {
740 play->sample_rate = rec->sample_rate;
741 setmode |= AUMODE_PLAY;
742 } else
743 return EINVAL;
744 }
745
746 for (mode = AUMODE_RECORD; mode != -1;
747 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
748 if ((setmode & mode) == 0)
749 continue;
750
751 p = mode == AUMODE_PLAY ? play : rec;
752 fil = mode == AUMODE_PLAY ? pfil : rfil;
753 switch (p->sample_rate) {
754 case 48000: /* aurateconv */
755 case 44100:
756 case 29400:
757 case 22050:
758 case 17640:
759 case 14700:
760 case 11025:
761 case 8820:
762 case 8000: /* aurateconv */
763 case 7350:
764 break;
765 default:
766 return EINVAL;
767 }
768 awacs_write_reg(sc, AWACS_BYTE_SWAP, 0);
769 i = auconv_set_converter(sc->sc_formats, AWACS_NFORMATS,
770 mode, p, true, fil);
771 if (i < 0)
772 return EINVAL;
773 if (i == AWACS_FORMATS_LE)
774 awacs_write_reg(sc, AWACS_BYTE_SWAP, 1);
775 if (fil->req_size > 0)
776 p = &fil->filters[0].param;
777 if (awacs_set_rate(sc, p))
778 return EINVAL;
779 }
780 return 0;
781 }
782
783 static int
784 awacs_round_blocksize(void *h, int size, int mode, const audio_params_t *param)
785 {
786
787 if (size < PAGE_SIZE)
788 size = PAGE_SIZE;
789 return size & ~PGOFSET;
790 }
791
792 static int
793 awacs_halt_output(void *h)
794 {
795 struct awacs_softc *sc;
796
797 sc = h;
798 dbdma_stop(sc->sc_odma);
799 dbdma_reset(sc->sc_odma);
800 return 0;
801 }
802
803 static int
804 awacs_halt_input(void *h)
805 {
806 struct awacs_softc *sc;
807
808 sc = h;
809 dbdma_stop(sc->sc_idma);
810 dbdma_reset(sc->sc_idma);
811 return 0;
812 }
813
814 static int
815 awacs_getdev(void *h, struct audio_device *retp)
816 {
817
818 *retp = awacs_device;
819 return 0;
820 }
821
822 enum {
823 AWACS_MONITOR_CLASS,
824 AWACS_OUTPUT_CLASS,
825 AWACS_RECORD_CLASS,
826 AWACS_OUTPUT_SELECT,
827 AWACS_VOL_MASTER,
828 AWACS_INPUT_SELECT,
829 AWACS_VOL_INPUT,
830 AWACS_VOL_MONITOR,
831 AWACS_BASS,
832 AWACS_TREBLE,
833 AWACS_ENUM_LAST
834 };
835
836 static int
837 awacs_set_port(void *h, mixer_ctrl_t *mc)
838 {
839 struct awacs_softc *sc;
840 int l, r;
841
842 DPRINTF("awacs_set_port dev = %d, type = %d\n", mc->dev, mc->type);
843 sc = h;
844 l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
845 r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
846
847 switch (mc->dev) {
848 case AWACS_OUTPUT_SELECT:
849 /* No change necessary? */
850 if (mc->un.mask == sc->sc_output_mask)
851 return 0;
852 awacs_select_output(sc, mc->un.mask);
853 return 0;
854
855 case AWACS_VOL_MASTER:
856 awacs_set_volume(sc, l, r);
857 return 0;
858
859 case AWACS_INPUT_SELECT:
860 /* no change necessary? */
861 if (mc->un.mask == sc->sc_record_source)
862 return 0;
863 switch (mc->un.mask) {
864 case 1 << 0: /* CD */
865 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
866 sc->sc_codecctl0 |= AWACS_INPUT_CD;
867 awacs_write_codec(sc, sc->sc_codecctl0);
868 break;
869 case 1 << 1: /* microphone */
870 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
871 sc->sc_codecctl0 |= AWACS_INPUT_MICROPHONE;
872 awacs_write_codec(sc, sc->sc_codecctl0);
873 break;
874 case 1 << 2: /* line in */
875 sc->sc_codecctl0 &= ~AWACS_INPUT_MASK;
876 sc->sc_codecctl0 |= AWACS_INPUT_LINE;
877 awacs_write_codec(sc, sc->sc_codecctl0);
878 break;
879 default: /* invalid argument */
880 return EINVAL;
881 }
882 sc->sc_record_source = mc->un.mask;
883 return 0;
884
885 case AWACS_VOL_INPUT:
886 sc->sc_codecctl0 &= ~0xff;
887 sc->sc_codecctl0 |= (l & 0xf0) | (r >> 4);
888 awacs_write_codec(sc, sc->sc_codecctl0);
889 return 0;
890
891 case AWACS_VOL_MONITOR:
892 awacs_set_loopthrough_volume(sc, l, r);
893 return 0;
894
895 #if NSGSMIX > 0
896 case AWACS_BASS:
897 awacs_set_bass(sc, l);
898 return 0;
899
900 case AWACS_TREBLE:
901 awacs_set_treble(sc, l);
902 return 0;
903 #endif
904 }
905
906 return ENXIO;
907 }
908
909 static int
910 awacs_get_port(void *h, mixer_ctrl_t *mc)
911 {
912 struct awacs_softc *sc;
913 int l, r, vol;
914
915 sc = h;
916 switch (mc->dev) {
917 case AWACS_OUTPUT_SELECT:
918 mc->un.mask = sc->sc_output_mask;
919 return 0;
920
921 case AWACS_VOL_MASTER:
922 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->vol_l;
923 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->vol_r;
924 return 0;
925
926 case AWACS_INPUT_SELECT:
927 mc->un.mask = sc->sc_record_source;
928 return 0;
929
930 case AWACS_VOL_INPUT:
931 vol = sc->sc_codecctl0 & 0xff;
932 l = (vol & 0xf0);
933 r = (vol & 0x0f) << 4;
934 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
935 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
936 return 0;
937
938 case AWACS_VOL_MONITOR:
939 vol = sc->sc_codecctl5 & 0x3cf;
940 l = (vol & 0x3c0) >> 6;
941 r = vol & 0xf;
942 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = (15 - l) << 4;
943 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = (15 - r) << 4;
944 return 0;
945
946 #if NSGSMIX > 0
947 case AWACS_BASS:
948 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
949 return 0;
950
951 case AWACS_TREBLE:
952 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
953 return 0;
954 #endif
955
956 default:
957 return ENXIO;
958 }
959
960 return 0;
961 }
962
963 static int
964 awacs_query_devinfo(void *h, mixer_devinfo_t *dip)
965 {
966 #if NSGSMIX > 0
967 struct awacs_softc *sc = h;
968 #endif
969
970 switch (dip->index) {
971
972 case AWACS_OUTPUT_SELECT:
973 dip->mixer_class = AWACS_OUTPUT_CLASS;
974 strcpy(dip->label.name, AudioNoutput);
975 dip->type = AUDIO_MIXER_SET;
976 dip->prev = dip->next = AUDIO_MIXER_LAST;
977 dip->un.s.num_mem = 2;
978 strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
979 dip->un.s.member[0].mask = 1 << 0;
980 strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
981 dip->un.s.member[1].mask = 1 << 1;
982 return 0;
983
984 case AWACS_VOL_MASTER:
985 dip->mixer_class = AWACS_OUTPUT_CLASS;
986 strcpy(dip->label.name, AudioNmaster);
987 dip->type = AUDIO_MIXER_VALUE;
988 dip->prev = dip->next = AUDIO_MIXER_LAST;
989 dip->un.v.num_channels = 2;
990 dip->un.v.delta = 16;
991 strcpy(dip->un.v.units.name, AudioNvolume);
992 return 0;
993
994 case AWACS_VOL_MONITOR:
995 dip->mixer_class = AWACS_MONITOR_CLASS;
996 strcpy(dip->label.name, AudioNmonitor);
997 dip->type = AUDIO_MIXER_VALUE;
998 dip->prev = dip->next = AUDIO_MIXER_LAST;
999 dip->un.v.num_channels = 2;
1000 strcpy(dip->un.v.units.name, AudioNvolume);
1001 return 0;
1002
1003 #if NSGSMIX > 0
1004 case AWACS_BASS:
1005 if (sc->sc_sgsmix == NULL)
1006 return ENXIO;
1007 dip->mixer_class = AWACS_OUTPUT_CLASS;
1008 strcpy(dip->label.name, AudioNbass);
1009 dip->type = AUDIO_MIXER_VALUE;
1010 dip->prev = dip->next = AUDIO_MIXER_LAST;
1011 dip->un.v.num_channels = 1;
1012 strcpy(dip->un.v.units.name, AudioNbass);
1013 return 0;
1014
1015 case AWACS_TREBLE:
1016 if (sc->sc_sgsmix == NULL)
1017 return ENXIO;
1018 dip->mixer_class = AWACS_OUTPUT_CLASS;
1019 strcpy(dip->label.name, AudioNtreble);
1020 dip->type = AUDIO_MIXER_VALUE;
1021 dip->prev = dip->next = AUDIO_MIXER_LAST;
1022 dip->un.v.num_channels = 1;
1023 strcpy(dip->un.v.units.name, AudioNtreble);
1024 return 0;
1025 #endif
1026
1027 case AWACS_INPUT_SELECT:
1028 dip->mixer_class = AWACS_RECORD_CLASS;
1029 strcpy(dip->label.name, AudioNsource);
1030 dip->type = AUDIO_MIXER_SET;
1031 dip->prev = dip->next = AUDIO_MIXER_LAST;
1032 dip->un.s.num_mem = 3;
1033 strcpy(dip->un.s.member[0].label.name, AudioNcd);
1034 dip->un.s.member[0].mask = 1 << 0;
1035 strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
1036 dip->un.s.member[1].mask = 1 << 1;
1037 strcpy(dip->un.s.member[2].label.name, AudioNline);
1038 dip->un.s.member[2].mask = 1 << 2;
1039 return 0;
1040
1041 case AWACS_VOL_INPUT:
1042 dip->mixer_class = AWACS_RECORD_CLASS;
1043 strcpy(dip->label.name, AudioNrecord);
1044 dip->type = AUDIO_MIXER_VALUE;
1045 dip->prev = dip->next = AUDIO_MIXER_LAST;
1046 dip->un.v.num_channels = 2;
1047 strcpy(dip->un.v.units.name, AudioNvolume);
1048 return 0;
1049
1050 case AWACS_MONITOR_CLASS:
1051 dip->mixer_class = AWACS_MONITOR_CLASS;
1052 strcpy(dip->label.name, AudioCmonitor);
1053 dip->type = AUDIO_MIXER_CLASS;
1054 dip->next = dip->prev = AUDIO_MIXER_LAST;
1055 return 0;
1056
1057 case AWACS_OUTPUT_CLASS:
1058 dip->mixer_class = AWACS_OUTPUT_CLASS;
1059 strcpy(dip->label.name, AudioCoutputs);
1060 dip->type = AUDIO_MIXER_CLASS;
1061 dip->next = dip->prev = AUDIO_MIXER_LAST;
1062 return 0;
1063
1064 case AWACS_RECORD_CLASS:
1065 dip->mixer_class = AWACS_RECORD_CLASS;
1066 strcpy(dip->label.name, AudioCrecord);
1067 dip->type = AUDIO_MIXER_CLASS;
1068 dip->next = dip->prev = AUDIO_MIXER_LAST;
1069 return 0;
1070 }
1071
1072 return ENXIO;
1073 }
1074
1075 static size_t
1076 awacs_round_buffersize(void *h, int dir, size_t size)
1077 {
1078
1079 if (size > 65536)
1080 size = 65536;
1081 return size;
1082 }
1083
1084 static paddr_t
1085 awacs_mappage(void *h, void *mem, off_t off, int prot)
1086 {
1087
1088 if (off < 0)
1089 return -1;
1090 return -1; /* XXX */
1091 }
1092
1093 static int
1094 awacs_get_props(void *h)
1095 {
1096 return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
1097 }
1098
1099 static int
1100 awacs_trigger_output(void *h, void *start, void *end, int bsize,
1101 void (*intr)(void *), void *arg,
1102 const audio_params_t *param)
1103 {
1104 struct awacs_softc *sc;
1105 struct dbdma_command *cmd;
1106 vaddr_t va;
1107 int i, len, intmode;
1108
1109 DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
1110 sc = h;
1111 cmd = sc->sc_odmacmd;
1112 sc->sc_ointr = intr;
1113 sc->sc_oarg = arg;
1114 sc->sc_opages = ((char *)end - (char *)start) / PAGE_SIZE;
1115
1116 #ifdef DIAGNOSTIC
1117 if (sc->sc_opages > 16)
1118 panic("awacs_trigger_output");
1119 #endif
1120
1121 va = (vaddr_t)start;
1122 len = 0;
1123 for (i = sc->sc_opages; i > 0; i--) {
1124 len += PAGE_SIZE;
1125 if (len < bsize)
1126 intmode = DBDMA_INT_NEVER;
1127 else {
1128 len = 0;
1129 intmode = DBDMA_INT_ALWAYS;
1130 }
1131
1132 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, PAGE_SIZE, vtophys(va),
1133 intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
1134 va += PAGE_SIZE;
1135 cmd++;
1136 }
1137
1138 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
1139 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
1140 out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
1141
1142 dbdma_start(sc->sc_odma, sc->sc_odmacmd);
1143
1144 return 0;
1145 }
1146
1147 static int
1148 awacs_trigger_input(void *h, void *start, void *end, int bsize,
1149 void (*intr)(void *), void *arg,
1150 const audio_params_t *param)
1151 {
1152
1153 DPRINTF("awacs_trigger_input called\n");
1154 return 1;
1155 }
1156
1157 static void
1158 awacs_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
1159 {
1160 struct awacs_softc *sc = opaque;
1161
1162 *intr = &sc->sc_intr_lock;
1163 *thread = &sc->sc_lock;
1164 }
1165
1166 static void
1167 awacs_select_output(struct awacs_softc *sc, int mask)
1168 {
1169
1170 #if NSGSMIX > 0
1171 if (sc->sc_sgsmix) {
1172 if (mask & OUTPUT_HEADPHONES) {
1173 /* mute speakers */
1174 sgsmix_set_speaker_vol(sc->sc_sgsmix, 0, 0);
1175 sgsmix_set_headphone_vol(sc->sc_sgsmix,
1176 sc->vol_l, sc->vol_r);
1177 }
1178 if (mask & OUTPUT_SPEAKER) {
1179 /* mute headphones */
1180 sgsmix_set_speaker_vol(sc->sc_sgsmix,
1181 sc->vol_l, sc->vol_r);
1182 sgsmix_set_headphone_vol(sc->sc_sgsmix, 0, 0);
1183 }
1184 } else {
1185 #endif
1186 sc->sc_codecctl1 |= AWACS_MUTE_SPEAKER | AWACS_MUTE_HEADPHONE;
1187 if ((sc->vol_l > 0) || (sc->vol_r > 0)) {
1188 if (mask & OUTPUT_SPEAKER)
1189 sc->sc_codecctl1 &= ~AWACS_MUTE_SPEAKER;
1190 if (mask & OUTPUT_HEADPHONES)
1191 sc->sc_codecctl1 &= ~AWACS_MUTE_HEADPHONE;
1192 }
1193 awacs_write_codec(sc, sc->sc_codecctl1);
1194 #if NSGSMIX > 0
1195 }
1196 #endif
1197 sc->sc_output_mask = mask;
1198 }
1199
1200 static void
1201 awacs_set_speaker_volume(struct awacs_softc *sc, int left, int right)
1202 {
1203
1204 #if NSGSMIX > 0
1205 if (sc->sc_sgsmix) {
1206 if (sc->sc_output_mask & OUTPUT_SPEAKER)
1207 sgsmix_set_speaker_vol(sc->sc_sgsmix, left, right);
1208 } else
1209 #endif
1210 {
1211 int lval;
1212 int rval;
1213 uint32_t codecctl = sc->sc_codecctl1;
1214
1215 lval = 15 - ((left & 0xf0) >> 4);
1216 rval = 15 - ((right & 0xf0) >> 4);
1217 DPRINTF("speaker_volume %d %d\n", lval, rval);
1218
1219 sc->sc_codecctl4 &= ~0x3cf;
1220 sc->sc_codecctl4 |= (lval << 6) | rval;
1221 awacs_write_codec(sc, sc->sc_codecctl4);
1222 if ((left == 0) && (right == 0)) {
1223 /*
1224 * max. attenuation doesn't mean silence so we need to
1225 * mute the output channel here
1226 */
1227 codecctl |= AWACS_MUTE_SPEAKER;
1228 } else if (sc->sc_output_mask & OUTPUT_SPEAKER) {
1229 codecctl &= ~AWACS_MUTE_SPEAKER;
1230 }
1231
1232 if (codecctl != sc->sc_codecctl1) {
1233
1234 sc->sc_codecctl1 = codecctl;
1235 awacs_write_codec(sc, sc->sc_codecctl1);
1236 }
1237 }
1238 }
1239
1240 static void
1241 awacs_set_ext_volume(struct awacs_softc *sc, int left, int right)
1242 {
1243
1244 #if NSGSMIX > 0
1245 if (sc->sc_sgsmix) {
1246 if (sc->sc_output_mask & OUTPUT_HEADPHONES)
1247 sgsmix_set_headphone_vol(sc->sc_sgsmix, left, right);
1248 } else
1249 #endif
1250 {
1251 int lval;
1252 int rval;
1253 uint32_t codecctl = sc->sc_codecctl1;
1254
1255 lval = 15 - ((left & 0xf0) >> 4);
1256 rval = 15 - ((right & 0xf0) >> 4);
1257 DPRINTF("ext_volume %d %d\n", lval, rval);
1258
1259 sc->sc_codecctl2 &= ~0x3cf;
1260 sc->sc_codecctl2 |= (lval << 6) | rval;
1261 awacs_write_codec(sc, sc->sc_codecctl2);
1262
1263 if ((left == 0) && (right == 0)) {
1264 /*
1265 * max. attenuation doesn't mean silence so we need to
1266 * mute the output channel here
1267 */
1268 codecctl |= AWACS_MUTE_HEADPHONE;
1269 } else if (sc->sc_output_mask & OUTPUT_HEADPHONES) {
1270
1271 codecctl &= ~AWACS_MUTE_HEADPHONE;
1272 }
1273
1274 if (codecctl != sc->sc_codecctl1) {
1275
1276 sc->sc_codecctl1 = codecctl;
1277 awacs_write_codec(sc, sc->sc_codecctl1);
1278 }
1279 }
1280 }
1281
1282 void
1283 awacs_set_volume(struct awacs_softc *sc, int left, int right)
1284 {
1285
1286 awacs_set_ext_volume(sc, left, right);
1287 awacs_set_speaker_volume(sc, left, right);
1288
1289 sc->vol_l = left;
1290 sc->vol_r = right;
1291 }
1292
1293 #if NSGSMIX > 0
1294 static void
1295 awacs_set_bass(struct awacs_softc *sc, int bass)
1296 {
1297
1298 if (sc->sc_bass == bass)
1299 return;
1300
1301 sc->sc_bass = bass;
1302 if (sc->sc_sgsmix)
1303 sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
1304 sc->sc_treble);
1305 }
1306
1307 static void
1308 awacs_set_treble(struct awacs_softc *sc, int treble)
1309 {
1310
1311 if (sc->sc_treble == treble)
1312 return;
1313
1314 sc->sc_treble = treble;
1315 if (sc->sc_sgsmix)
1316 sgsmix_set_bass_treble(sc->sc_sgsmix, sc->sc_bass,
1317 sc->sc_treble);
1318 }
1319 #endif
1320
1321 void
1322 awacs_set_loopthrough_volume(struct awacs_softc *sc, int left, int right)
1323 {
1324 int lval;
1325 int rval;
1326
1327 lval = 15 - ((left & 0xff) >> 4);
1328 rval = 15 - ((right & 0xff) >> 4);
1329 DPRINTF("loopthrough_volume %d %d\n", lval, rval);
1330
1331 sc->sc_codecctl5 &= ~0x3cf;
1332 sc->sc_codecctl5 |= (lval << 6) | rval;
1333 awacs_write_codec(sc, sc->sc_codecctl5);
1334 }
1335
1336 int
1337 awacs_set_rate(struct awacs_softc *sc, const audio_params_t *p)
1338 {
1339 int c;
1340
1341 switch (p->sample_rate) {
1342 case 44100:
1343 c = AWACS_RATE_44100;
1344 break;
1345 case 29400:
1346 c = AWACS_RATE_29400;
1347 break;
1348 case 22050:
1349 c = AWACS_RATE_22050;
1350 break;
1351 case 17640:
1352 c = AWACS_RATE_17640;
1353 break;
1354 case 14700:
1355 c = AWACS_RATE_14700;
1356 break;
1357 case 11025:
1358 c = AWACS_RATE_11025;
1359 break;
1360 case 8820:
1361 c = AWACS_RATE_8820;
1362 break;
1363 case 7350:
1364 c = AWACS_RATE_7350;
1365 break;
1366 default:
1367 return -1;
1368 }
1369
1370 sc->sc_soundctl &= ~AWACS_RATE_MASK;
1371 sc->sc_soundctl |= c;
1372 awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl);
1373
1374 return 0;
1375 }
1376
1377 static int
1378 awacs_check_headphones(struct awacs_softc *sc)
1379 {
1380 uint32_t reg;
1381 reg = awacs_read_reg(sc, AWACS_CODEC_STATUS);
1382 DPRINTF("%s: codec status reg %08x\n", device_xname(sc->sc_dev), reg);
1383 return ((reg & sc->sc_headphones_mask) == sc->sc_headphones_in);
1384 }
1385
1386 static int
1387 awacs_status_intr(void *cookie)
1388 {
1389 struct awacs_softc *sc = cookie;
1390 int mask;
1391
1392 mutex_spin_enter(&sc->sc_intr_lock);
1393 mask = awacs_check_headphones(sc) ? OUTPUT_HEADPHONES : OUTPUT_SPEAKER;
1394 if (mask != sc->sc_output_mask) {
1395
1396 sc->sc_output_wanted = mask;
1397 cv_signal(&sc->sc_event);
1398 }
1399 /* clear the interrupt */
1400 awacs_write_reg(sc, AWACS_SOUND_CTRL, sc->sc_soundctl | AWACS_PORTCHG);
1401 mutex_spin_exit(&sc->sc_intr_lock);
1402 return 1;
1403 }
1404
1405 static void
1406 awacs_thread(void *cookie)
1407 {
1408 struct awacs_softc *sc = cookie;
1409
1410 mutex_enter(&sc->sc_intr_lock);
1411 while (1) {
1412 cv_timedwait(&sc->sc_event, &sc->sc_intr_lock, hz);
1413 if (sc->sc_output_wanted == sc->sc_output_mask)
1414 continue;
1415
1416 awacs_select_output(sc, sc->sc_output_wanted);
1417 DPRINTF("%s: switching to %s\n", device_xname(sc->sc_dev),
1418 (sc->sc_output_wanted & OUTPUT_SPEAKER) ?
1419 "speaker" : "headphones");
1420 }
1421 }
1422