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