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