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