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