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