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