Home | History | Annotate | Line # | Download | only in dev
snapper.c revision 1.2
      1  1.2   yamt /*	$NetBSD: snapper.c,v 1.2 2004/10/29 12:57:16 yamt Exp $	*/
      2  1.1  grant /*	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp	*/
      3  1.1  grant 
      4  1.1  grant /*-
      5  1.1  grant  * Copyright (c) 2002 Tsubai Masanari.  All rights reserved.
      6  1.1  grant  *
      7  1.1  grant  * Redistribution and use in source and binary forms, with or without
      8  1.1  grant  * modification, are permitted provided that the following conditions
      9  1.1  grant  * are met:
     10  1.1  grant  * 1. Redistributions of source code must retain the above copyright
     11  1.1  grant  *    notice, this list of conditions and the following disclaimer.
     12  1.1  grant  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  grant  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  grant  *    documentation and/or other materials provided with the distribution.
     15  1.1  grant  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  grant  *    derived from this software without specific prior written permission.
     17  1.1  grant  *
     18  1.1  grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  1.1  grant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  grant  */
     29  1.1  grant 
     30  1.1  grant /*
     31  1.1  grant  * Datasheet is available from
     32  1.1  grant  * http://www.ti.com/sc/docs/products/analog/tas3004.html
     33  1.1  grant  */
     34  1.1  grant 
     35  1.1  grant #include <sys/param.h>
     36  1.1  grant #include <sys/audioio.h>
     37  1.1  grant #include <sys/device.h>
     38  1.1  grant #include <sys/systm.h>
     39  1.1  grant 
     40  1.1  grant #include <dev/auconv.h>
     41  1.1  grant #include <dev/audio_if.h>
     42  1.1  grant #include <dev/mulaw.h>
     43  1.1  grant #include <dev/ofw/openfirm.h>
     44  1.1  grant #include <macppc/dev/dbdma.h>
     45  1.1  grant 
     46  1.1  grant #include <uvm/uvm_extern.h>
     47  1.1  grant 
     48  1.1  grant #include <machine/autoconf.h>
     49  1.1  grant #include <machine/pio.h>
     50  1.1  grant 
     51  1.1  grant #ifdef SNAPPER_DEBUG
     52  1.1  grant # define DPRINTF printf
     53  1.1  grant #else
     54  1.1  grant # define DPRINTF while (0) printf
     55  1.1  grant #endif
     56  1.1  grant 
     57  1.1  grant struct snapper_softc {
     58  1.1  grant 	struct device sc_dev;
     59  1.1  grant 	int sc_flags;
     60  1.1  grant 	int sc_node;
     61  1.1  grant 
     62  1.1  grant 	void (*sc_ointr)(void *);	/* dma completion intr handler */
     63  1.1  grant 	void *sc_oarg;			/* arg for sc_ointr() */
     64  1.1  grant 	int sc_opages;			/* # of output pages */
     65  1.1  grant 
     66  1.1  grant 	void (*sc_iintr)(void *);	/* dma completion intr handler */
     67  1.1  grant 	void *sc_iarg;			/* arg for sc_iintr() */
     68  1.1  grant 
     69  1.1  grant 	u_int sc_record_source;		/* recording source mask */
     70  1.1  grant 	u_int sc_output_mask;		/* output source mask */
     71  1.1  grant 
     72  1.1  grant 	u_char *sc_reg;
     73  1.1  grant 	struct device *sc_i2c;
     74  1.1  grant 
     75  1.1  grant 	u_int sc_vol_l;
     76  1.1  grant 	u_int sc_vol_r;
     77  1.1  grant 
     78  1.1  grant 	dbdma_regmap_t *sc_odma;
     79  1.1  grant 	dbdma_regmap_t *sc_idma;
     80  1.1  grant 	struct dbdma_command sc_odmacmd[20];
     81  1.1  grant 	struct dbdma_command sc_idmacmd[20];
     82  1.1  grant };
     83  1.1  grant 
     84  1.1  grant int snapper_match(struct device *, struct cfdata *, void *);
     85  1.1  grant void snapper_attach(struct device *, struct device *, void *);
     86  1.1  grant void snapper_defer(struct device *);
     87  1.1  grant int snapper_intr(void *);
     88  1.1  grant int snapper_open(void *, int);
     89  1.1  grant void snapper_close(void *);
     90  1.1  grant int snapper_query_encoding(void *, struct audio_encoding *);
     91  1.1  grant int snapper_set_params(void *, int, int, struct audio_params *,
     92  1.1  grant     struct audio_params *);
     93  1.1  grant int snapper_round_blocksize(void *, int);
     94  1.1  grant int snapper_halt_output(void *);
     95  1.1  grant int snapper_halt_input(void *);
     96  1.1  grant int snapper_getdev(void *, struct audio_device *);
     97  1.1  grant int snapper_set_port(void *, mixer_ctrl_t *);
     98  1.1  grant int snapper_get_port(void *, mixer_ctrl_t *);
     99  1.1  grant int snapper_query_devinfo(void *, mixer_devinfo_t *);
    100  1.1  grant size_t snapper_round_buffersize(void *, int, size_t);
    101  1.1  grant paddr_t snapper_mappage(void *, void *, off_t, int);
    102  1.1  grant int snapper_get_props(void *);
    103  1.1  grant int snapper_trigger_output(void *, void *, void *, int, void (*)(void *),
    104  1.1  grant     void *, struct audio_params *);
    105  1.1  grant int snapper_trigger_input(void *, void *, void *, int, void (*)(void *),
    106  1.1  grant     void *, struct audio_params *);
    107  1.1  grant void snapper_set_volume(struct snapper_softc *, int, int);
    108  1.1  grant int snapper_set_rate(struct snapper_softc *, int);
    109  1.1  grant 
    110  1.1  grant int tas3004_write(struct snapper_softc *, u_int, const void *);
    111  1.1  grant static int gpio_read(char *);
    112  1.1  grant static void gpio_write(char *, int);
    113  1.1  grant void snapper_mute_speaker(struct snapper_softc *, int);
    114  1.1  grant void snapper_mute_headphone(struct snapper_softc *, int);
    115  1.1  grant int snapper_cint(void *);
    116  1.1  grant int tas3004_init(struct snapper_softc *);
    117  1.1  grant void snapper_init(struct snapper_softc *, int);
    118  1.1  grant 
    119  1.1  grant static void mono16_to_stereo16(void *, u_char *, int);
    120  1.1  grant static void swap_bytes_mono16_to_stereo16(void *, u_char *, int);
    121  1.1  grant 
    122  1.1  grant /* XXX */
    123  1.1  grant int ki2c_setmode(struct device *, int);
    124  1.1  grant int ki2c_write(struct device *, int, int, const void *, int);
    125  1.1  grant void ki2c_writereg(struct device *, int, u_int);
    126  1.1  grant 
    127  1.1  grant 
    128  1.1  grant struct cfattach snapper_ca = {
    129  1.1  grant 	"snapper", {}, sizeof(struct snapper_softc),
    130  1.1  grant 	snapper_match, snapper_attach
    131  1.1  grant };
    132  1.1  grant 
    133  1.2   yamt const struct audio_hw_if snapper_hw_if = {
    134  1.1  grant 	snapper_open,
    135  1.1  grant 	snapper_close,
    136  1.1  grant 	NULL,
    137  1.1  grant 	snapper_query_encoding,
    138  1.1  grant 	snapper_set_params,
    139  1.1  grant 	snapper_round_blocksize,
    140  1.1  grant 	NULL,
    141  1.1  grant 	NULL,
    142  1.1  grant 	NULL,
    143  1.1  grant 	NULL,
    144  1.1  grant 	NULL,
    145  1.1  grant 	snapper_halt_output,
    146  1.1  grant 	snapper_halt_input,
    147  1.1  grant 	NULL,
    148  1.1  grant 	snapper_getdev,
    149  1.1  grant 	NULL,
    150  1.1  grant 	snapper_set_port,
    151  1.1  grant 	snapper_get_port,
    152  1.1  grant 	snapper_query_devinfo,
    153  1.1  grant 	NULL,
    154  1.1  grant 	NULL,
    155  1.1  grant 	snapper_round_buffersize,
    156  1.1  grant 	snapper_mappage,
    157  1.1  grant 	snapper_get_props,
    158  1.1  grant 	snapper_trigger_output,
    159  1.1  grant 	snapper_trigger_input,
    160  1.1  grant 	NULL
    161  1.1  grant };
    162  1.1  grant 
    163  1.1  grant struct audio_device snapper_device = {
    164  1.1  grant 	"SNAPPER",
    165  1.1  grant 	"",
    166  1.1  grant 	"snapper"
    167  1.1  grant };
    168  1.1  grant 
    169  1.1  grant static u_char *amp_mute;
    170  1.1  grant static u_char *headphone_mute;
    171  1.1  grant static u_char *audio_hw_reset;
    172  1.1  grant static u_char *headphone_detect;
    173  1.1  grant static int headphone_detect_active;
    174  1.1  grant 
    175  1.1  grant 
    176  1.1  grant /* I2S registers */
    177  1.1  grant #define I2S_INT		0x00
    178  1.1  grant #define I2S_FORMAT	0x10
    179  1.1  grant #define I2S_FRAMECOUNT	0x40
    180  1.1  grant #define I2S_FRAMEMATCH	0x50
    181  1.1  grant #define I2S_WORDSIZE	0x60
    182  1.1  grant 
    183  1.1  grant /* TAS3004 registers */
    184  1.1  grant #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
    185  1.1  grant #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?) */
    186  1.1  grant #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
    187  1.1  grant #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
    188  1.1  grant #define DEQ_BASS	0x06	/* Bass control (1byte) */
    189  1.1  grant #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes) */
    190  1.1  grant #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes) */
    191  1.1  grant #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
    192  1.1  grant #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
    193  1.1  grant #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
    194  1.1  grant #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
    195  1.1  grant #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
    196  1.1  grant #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
    197  1.1  grant #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
    198  1.1  grant #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
    199  1.1  grant #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
    200  1.1  grant #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
    201  1.1  grant #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
    202  1.1  grant #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
    203  1.1  grant #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
    204  1.1  grant #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
    205  1.1  grant #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
    206  1.1  grant #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
    207  1.1  grant #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
    208  1.1  grant #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
    209  1.1  grant #define DEQ_ACR		0x40	/* Analog control register (1byte) */
    210  1.1  grant #define DEQ_MCR2	0x43	/* Main control register 2 (1byte) */
    211  1.1  grant 
    212  1.1  grant #define DEQ_MCR1_FL	0x80	/* Fast load */
    213  1.1  grant #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
    214  1.1  grant #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
    215  1.1  grant #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
    216  1.1  grant #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
    217  1.1  grant #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
    218  1.1  grant #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
    219  1.1  grant #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
    220  1.1  grant #define DEQ_MCR1_W	0x03	/* Serial port word length */
    221  1.1  grant #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
    222  1.1  grant #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
    223  1.1  grant #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
    224  1.1  grant 
    225  1.1  grant #define DEQ_MCR2_DL	0x80	/* Download */
    226  1.1  grant #define DEQ_MCR2_AP	0x02	/* All pass mode */
    227  1.1  grant 
    228  1.1  grant #define DEQ_ACR_ADM	0x80	/* ADC output mode */
    229  1.1  grant #define DEQ_ACR_LRB	0x40	/* Select B input */
    230  1.1  grant #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
    231  1.1  grant #define  DEQ_ACR_DM_OFF	0x00	/*  off */
    232  1.1  grant #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
    233  1.1  grant #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
    234  1.1  grant #define DEQ_ACR_INP	0x02	/* Analog input select */
    235  1.1  grant #define  DEQ_ACR_INP_A	0x00	/*  A */
    236  1.1  grant #define  DEQ_ACR_INP_B	0x02	/*  B */
    237  1.1  grant #define DEQ_ACR_APD	0x01	/* Analog power down */
    238  1.1  grant 
    239  1.1  grant struct tas3004_reg {
    240  1.1  grant 	u_char MCR1[1];
    241  1.1  grant 	u_char DRC[6];
    242  1.1  grant 	u_char VOLUME[6];
    243  1.1  grant 	u_char TREBLE[1];
    244  1.1  grant 	u_char BASS[1];
    245  1.1  grant 	u_char MIXER_L[9];
    246  1.1  grant 	u_char MIXER_R[9];
    247  1.1  grant 	u_char LB0[15];
    248  1.1  grant 	u_char LB1[15];
    249  1.1  grant 	u_char LB2[15];
    250  1.1  grant 	u_char LB3[15];
    251  1.1  grant 	u_char LB4[15];
    252  1.1  grant 	u_char LB5[15];
    253  1.1  grant 	u_char LB6[15];
    254  1.1  grant 	u_char RB0[15];
    255  1.1  grant 	u_char RB1[15];
    256  1.1  grant 	u_char RB2[15];
    257  1.1  grant 	u_char RB3[15];
    258  1.1  grant 	u_char RB4[15];
    259  1.1  grant 	u_char RB5[15];
    260  1.1  grant 	u_char RB6[15];
    261  1.1  grant 	u_char LLB[15];
    262  1.1  grant 	u_char RLB[15];
    263  1.1  grant 	u_char LLB_GAIN[3];
    264  1.1  grant 	u_char RLB_GAIN[3];
    265  1.1  grant 	u_char ACR[1];
    266  1.1  grant 	u_char MCR2[1];
    267  1.1  grant };
    268  1.1  grant 
    269  1.1  grant #define GPIO_OUTSEL	0xf0	/* Output select */
    270  1.1  grant 		/*	0x00	GPIO bit0 is output
    271  1.1  grant 			0x10	media-bay power
    272  1.1  grant 			0x20	reserved
    273  1.1  grant 			0x30	MPIC */
    274  1.1  grant 
    275  1.1  grant #define GPIO_ALTOE	0x08	/* Alternate output enable */
    276  1.1  grant 		/*	0x00	Use DDR
    277  1.1  grant 			0x08	Use output select */
    278  1.1  grant 
    279  1.1  grant #define GPIO_DDR	0x04	/* Data direction */
    280  1.1  grant #define GPIO_DDR_OUTPUT	0x04	/* Output */
    281  1.1  grant #define GPIO_DDR_INPUT	0x00	/* Input */
    282  1.1  grant 
    283  1.1  grant #define GPIO_LEVEL	0x02	/* Pin level (RO) */
    284  1.1  grant 
    285  1.1  grant #define	GPIO_DATA	0x01	/* Data */
    286  1.1  grant 
    287  1.1  grant int
    288  1.1  grant snapper_match(parent, match, aux)
    289  1.1  grant 	struct device *parent;
    290  1.1  grant 	struct cfdata *match;
    291  1.1  grant 	void *aux;
    292  1.1  grant {
    293  1.1  grant 	struct confargs *ca = aux;
    294  1.1  grant 	int soundbus, soundchip;
    295  1.1  grant 	char compat[32];
    296  1.1  grant 
    297  1.1  grant 	if (strcmp(ca->ca_name, "i2s") != 0)
    298  1.1  grant 		return 0;
    299  1.1  grant 
    300  1.1  grant 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
    301  1.1  grant 	    (soundchip = OF_child(soundbus)) == 0)
    302  1.1  grant 		return 0;
    303  1.1  grant 
    304  1.1  grant 	bzero(compat, sizeof compat);
    305  1.1  grant 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
    306  1.1  grant 
    307  1.1  grant 	if (strcmp(compat, "snapper") != 0)
    308  1.1  grant 		return 0;
    309  1.1  grant 
    310  1.1  grant 	return 1;
    311  1.1  grant }
    312  1.1  grant 
    313  1.1  grant void
    314  1.1  grant snapper_attach(parent, self, aux)
    315  1.1  grant 	struct device *parent;
    316  1.1  grant 	struct device *self;
    317  1.1  grant 	void *aux;
    318  1.1  grant {
    319  1.1  grant 	struct snapper_softc *sc = (struct snapper_softc *)self;
    320  1.1  grant 	struct confargs *ca = aux;
    321  1.1  grant 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    322  1.1  grant 	int soundbus, intr[6];
    323  1.1  grant 
    324  1.1  grant #ifdef DIAGNOSTIC
    325  1.1  grant 	if ((vaddr_t)sc->sc_odmacmd & 0x0f) {
    326  1.1  grant 		printf(": bad dbdma alignment\n");
    327  1.1  grant 		return;
    328  1.1  grant 	}
    329  1.1  grant #endif
    330  1.1  grant 
    331  1.1  grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    332  1.1  grant 	ca->ca_reg[2] += ca->ca_baseaddr;
    333  1.1  grant 	ca->ca_reg[4] += ca->ca_baseaddr;
    334  1.1  grant 
    335  1.1  grant 	sc->sc_node = ca->ca_node;
    336  1.1  grant 	sc->sc_reg = (void *)ca->ca_reg[0];
    337  1.1  grant 	sc->sc_odma = (void *)ca->ca_reg[2];
    338  1.1  grant 	sc->sc_idma = (void *)ca->ca_reg[4];
    339  1.1  grant 
    340  1.1  grant 	soundbus = OF_child(ca->ca_node);
    341  1.1  grant 	OF_getprop(soundbus, "interrupts", intr, sizeof intr);
    342  1.1  grant 	cirq = intr[0];
    343  1.1  grant 	oirq = intr[2];
    344  1.1  grant 	iirq = intr[4];
    345  1.1  grant 	cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    346  1.1  grant 	oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    347  1.1  grant 	iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    348  1.1  grant 
    349  1.1  grant 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
    350  1.1  grant 	intr_establish(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc);
    351  1.1  grant 	/* intr_establish(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc); */
    352  1.1  grant 
    353  1.1  grant 	printf("%s: irq %d,%d,%d\n", sc->sc_dev.dv_xname, cirq, oirq, iirq);
    354  1.1  grant 
    355  1.1  grant 	config_interrupts(self, snapper_defer);
    356  1.1  grant }
    357  1.1  grant 
    358  1.1  grant void
    359  1.1  grant snapper_defer(struct device *dev)
    360  1.1  grant {
    361  1.1  grant 	struct snapper_softc *sc = (struct snapper_softc *)dev;
    362  1.1  grant 	struct device *dv;
    363  1.1  grant 
    364  1.1  grant 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
    365  1.1  grant 		if (strncmp(dv->dv_xname, "ki2c", 4) == 0 &&
    366  1.1  grant 		    strncmp(dv->dv_parent->dv_xname, "obio", 4) == 0)
    367  1.1  grant 			sc->sc_i2c = dv;
    368  1.1  grant 	if (sc->sc_i2c == NULL) {
    369  1.1  grant 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
    370  1.1  grant 		return;
    371  1.1  grant 	}
    372  1.1  grant 
    373  1.1  grant 	/* XXX If i2c was failed to attach, what should we do? */
    374  1.1  grant 
    375  1.1  grant 	audio_attach_mi(&snapper_hw_if, sc, &sc->sc_dev);
    376  1.1  grant 
    377  1.1  grant 	/* ki2c_setmode(sc->sc_i2c, I2C_STDSUBMODE); */
    378  1.1  grant 	snapper_init(sc, sc->sc_node);
    379  1.1  grant }
    380  1.1  grant 
    381  1.1  grant int
    382  1.1  grant snapper_intr(v)
    383  1.1  grant 	void *v;
    384  1.1  grant {
    385  1.1  grant 	struct snapper_softc *sc = v;
    386  1.1  grant 	struct dbdma_command *cmd = sc->sc_odmacmd;
    387  1.1  grant 	int count = sc->sc_opages;
    388  1.1  grant 	int status;
    389  1.1  grant 
    390  1.1  grant 	/* Fill used buffer(s). */
    391  1.1  grant 	while (count-- > 0) {
    392  1.1  grant 		if ((dbdma_ld16(&cmd->d_command) & 0x30) == 0x30) {
    393  1.1  grant 			status = dbdma_ld16(&cmd->d_status);
    394  1.1  grant 			cmd->d_status = 0;
    395  1.1  grant 			if (status)	/* status == 0x8400 */
    396  1.1  grant 				if (sc->sc_ointr)
    397  1.1  grant 					(*sc->sc_ointr)(sc->sc_oarg);
    398  1.1  grant 		}
    399  1.1  grant 		cmd++;
    400  1.1  grant 	}
    401  1.1  grant 
    402  1.1  grant 	return 1;
    403  1.1  grant }
    404  1.1  grant 
    405  1.1  grant int
    406  1.1  grant snapper_open(h, flags)
    407  1.1  grant 	void *h;
    408  1.1  grant 	int flags;
    409  1.1  grant {
    410  1.1  grant 	return 0;
    411  1.1  grant }
    412  1.1  grant 
    413  1.1  grant /*
    414  1.1  grant  * Close function is called at splaudio().
    415  1.1  grant  */
    416  1.1  grant void
    417  1.1  grant snapper_close(h)
    418  1.1  grant 	void *h;
    419  1.1  grant {
    420  1.1  grant 	struct snapper_softc *sc = h;
    421  1.1  grant 
    422  1.1  grant 	snapper_halt_output(sc);
    423  1.1  grant 	snapper_halt_input(sc);
    424  1.1  grant 
    425  1.1  grant 	sc->sc_ointr = 0;
    426  1.1  grant 	sc->sc_iintr = 0;
    427  1.1  grant }
    428  1.1  grant 
    429  1.1  grant int
    430  1.1  grant snapper_query_encoding(h, ae)
    431  1.1  grant 	void *h;
    432  1.1  grant 	struct audio_encoding *ae;
    433  1.1  grant {
    434  1.1  grant 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    435  1.1  grant 
    436  1.1  grant 	switch (ae->index) {
    437  1.1  grant 	case 0:
    438  1.1  grant 		strcpy(ae->name, AudioEslinear);
    439  1.1  grant 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    440  1.1  grant 		ae->precision = 16;
    441  1.1  grant 		ae->flags = 0;
    442  1.1  grant 		return 0;
    443  1.1  grant 	case 1:
    444  1.1  grant 		strcpy(ae->name, AudioEslinear_be);
    445  1.1  grant 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    446  1.1  grant 		ae->precision = 16;
    447  1.1  grant 		ae->flags = 0;
    448  1.1  grant 		return 0;
    449  1.1  grant 	case 2:
    450  1.1  grant 		strcpy(ae->name, AudioEslinear_le);
    451  1.1  grant 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    452  1.1  grant 		ae->precision = 16;
    453  1.1  grant 		return 0;
    454  1.1  grant 	case 3:
    455  1.1  grant 		strcpy(ae->name, AudioEulinear_be);
    456  1.1  grant 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    457  1.1  grant 		ae->precision = 16;
    458  1.1  grant 		return 0;
    459  1.1  grant 	case 4:
    460  1.1  grant 		strcpy(ae->name, AudioEulinear_le);
    461  1.1  grant 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    462  1.1  grant 		ae->precision = 16;
    463  1.1  grant 		return 0;
    464  1.1  grant 	case 5:
    465  1.1  grant 		strcpy(ae->name, AudioEmulaw);
    466  1.1  grant 		ae->encoding = AUDIO_ENCODING_ULAW;
    467  1.1  grant 		ae->precision = 8;
    468  1.1  grant 		return 0;
    469  1.1  grant 	case 6:
    470  1.1  grant 		strcpy(ae->name, AudioEalaw);
    471  1.1  grant 		ae->encoding = AUDIO_ENCODING_ALAW;
    472  1.1  grant 		ae->precision = 8;
    473  1.1  grant 		return 0;
    474  1.1  grant 	default:
    475  1.1  grant 		return EINVAL;
    476  1.1  grant 	}
    477  1.1  grant }
    478  1.1  grant 
    479  1.1  grant static void
    480  1.1  grant mono16_to_stereo16(v, p, cc)
    481  1.1  grant 	void *v;
    482  1.1  grant 	u_char *p;
    483  1.1  grant 	int cc;
    484  1.1  grant {
    485  1.1  grant 	int x;
    486  1.1  grant 	int16_t *src, *dst;
    487  1.1  grant 
    488  1.1  grant 	src = (void *)(p + cc);
    489  1.1  grant 	dst = (void *)(p + cc * 2);
    490  1.1  grant 	while (cc > 0) {
    491  1.1  grant 		x = *--src;
    492  1.1  grant 		*--dst = x;
    493  1.1  grant 		*--dst = x;
    494  1.1  grant 		cc -= 2;
    495  1.1  grant 	}
    496  1.1  grant }
    497  1.1  grant 
    498  1.1  grant static void
    499  1.1  grant swap_bytes_mono16_to_stereo16(v, p, cc)
    500  1.1  grant 	void *v;
    501  1.1  grant 	u_char *p;
    502  1.1  grant 	int cc;
    503  1.1  grant {
    504  1.1  grant 	swap_bytes(v, p, cc);
    505  1.1  grant 	mono16_to_stereo16(v, p, cc);
    506  1.1  grant }
    507  1.1  grant 
    508  1.1  grant int
    509  1.1  grant snapper_set_params(h, setmode, usemode, play, rec)
    510  1.1  grant 	void *h;
    511  1.1  grant 	int setmode, usemode;
    512  1.1  grant 	struct audio_params *play, *rec;
    513  1.1  grant {
    514  1.1  grant 	struct snapper_softc *sc = h;
    515  1.1  grant 	struct audio_params *p;
    516  1.1  grant 	int mode, rate;
    517  1.1  grant 
    518  1.1  grant 	p = NULL;
    519  1.1  grant 
    520  1.1  grant 	/*
    521  1.1  grant 	 * This device only has one clock, so make the sample rates match.
    522  1.1  grant 	 */
    523  1.1  grant 	if (play->sample_rate != rec->sample_rate &&
    524  1.1  grant 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    525  1.1  grant 		if (setmode == AUMODE_PLAY) {
    526  1.1  grant 			rec->sample_rate = play->sample_rate;
    527  1.1  grant 			setmode |= AUMODE_RECORD;
    528  1.1  grant 		} else if (setmode == AUMODE_RECORD) {
    529  1.1  grant 			play->sample_rate = rec->sample_rate;
    530  1.1  grant 			setmode |= AUMODE_PLAY;
    531  1.1  grant 		} else
    532  1.1  grant 			return EINVAL;
    533  1.1  grant 	}
    534  1.1  grant 
    535  1.1  grant 	for (mode = AUMODE_RECORD; mode != -1;
    536  1.1  grant 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    537  1.1  grant 		if ((setmode & mode) == 0)
    538  1.1  grant 			continue;
    539  1.1  grant 
    540  1.1  grant 		p = mode == AUMODE_PLAY ? play : rec;
    541  1.1  grant 
    542  1.1  grant 		if (p->sample_rate < 4000 || p->sample_rate > 50000 ||
    543  1.1  grant 		    (p->precision != 8 && p->precision != 16) ||
    544  1.1  grant 		    (p->channels != 1 && p->channels != 2))
    545  1.1  grant 			return EINVAL;
    546  1.1  grant 
    547  1.1  grant 		p->factor = 1;
    548  1.1  grant 		p->sw_code = 0;
    549  1.1  grant 
    550  1.1  grant 		switch (p->encoding) {
    551  1.1  grant 
    552  1.1  grant 		case AUDIO_ENCODING_SLINEAR_LE:
    553  1.1  grant 			if (p->channels == 2 && p->precision == 16) {
    554  1.1  grant 				p->sw_code = swap_bytes;
    555  1.1  grant 				break;
    556  1.1  grant 			}
    557  1.1  grant 			if (p->channels == 1 && p->precision == 16) {
    558  1.1  grant 				p->factor = 2;
    559  1.1  grant 				p->sw_code = swap_bytes_mono16_to_stereo16;
    560  1.1  grant 				break;
    561  1.1  grant 			}
    562  1.1  grant 			return EINVAL;
    563  1.1  grant 		case AUDIO_ENCODING_SLINEAR_BE:
    564  1.1  grant 			if (p->channels == 1 && p->precision == 16) {
    565  1.1  grant 				p->factor = 2;
    566  1.1  grant 				p->sw_code = mono16_to_stereo16;
    567  1.1  grant 				break;
    568  1.1  grant 			}
    569  1.1  grant 			if (p->channels == 2 && p->precision == 16)
    570  1.1  grant 				break;
    571  1.1  grant 
    572  1.1  grant 			return EINVAL;
    573  1.1  grant 
    574  1.1  grant 		case AUDIO_ENCODING_ULINEAR_LE:
    575  1.1  grant 			if (p->channels == 2 && p->precision == 16) {
    576  1.1  grant 				p->sw_code = swap_bytes_change_sign16_be;
    577  1.1  grant 				break;
    578  1.1  grant 			}
    579  1.1  grant 			return EINVAL;
    580  1.1  grant 
    581  1.1  grant 		case AUDIO_ENCODING_ULINEAR_BE:
    582  1.1  grant 			if (p->channels == 2 && p->precision == 16) {
    583  1.1  grant 				p->sw_code = change_sign16_be;
    584  1.1  grant 				break;
    585  1.1  grant 			}
    586  1.1  grant 			return EINVAL;
    587  1.1  grant 
    588  1.1  grant 		case AUDIO_ENCODING_ULAW:
    589  1.1  grant 			if (mode == AUMODE_PLAY) {
    590  1.1  grant 				p->factor = 2;
    591  1.1  grant 				p->sw_code = mulaw_to_slinear16_be;
    592  1.1  grant 				break;
    593  1.1  grant 			} else
    594  1.1  grant 				break;		/* XXX */
    595  1.1  grant 
    596  1.1  grant 			return EINVAL;
    597  1.1  grant 
    598  1.1  grant 		case AUDIO_ENCODING_ALAW:
    599  1.1  grant 			if (mode == AUMODE_PLAY) {
    600  1.1  grant 				p->factor = 2;
    601  1.1  grant 				p->sw_code = alaw_to_slinear16_be;
    602  1.1  grant 				break;
    603  1.1  grant 			}
    604  1.1  grant 			return EINVAL;
    605  1.1  grant 
    606  1.1  grant 		default:
    607  1.1  grant 			return EINVAL;
    608  1.1  grant 		}
    609  1.1  grant 	}
    610  1.1  grant 
    611  1.1  grant 	/* Set the speed */
    612  1.1  grant 	rate = p->sample_rate;
    613  1.1  grant 
    614  1.1  grant 	if (snapper_set_rate(sc, rate))
    615  1.1  grant 		return EINVAL;
    616  1.1  grant 
    617  1.1  grant 	return 0;
    618  1.1  grant }
    619  1.1  grant 
    620  1.1  grant int
    621  1.1  grant snapper_round_blocksize(h, size)
    622  1.1  grant 	void *h;
    623  1.1  grant 	int size;
    624  1.1  grant {
    625  1.1  grant 	if (size < NBPG)
    626  1.1  grant 		size = NBPG;
    627  1.1  grant 	return size & ~PGOFSET;
    628  1.1  grant }
    629  1.1  grant 
    630  1.1  grant int
    631  1.1  grant snapper_halt_output(h)
    632  1.1  grant 	void *h;
    633  1.1  grant {
    634  1.1  grant 	struct snapper_softc *sc = h;
    635  1.1  grant 
    636  1.1  grant 	dbdma_stop(sc->sc_odma);
    637  1.1  grant 	dbdma_reset(sc->sc_odma);
    638  1.1  grant 	return 0;
    639  1.1  grant }
    640  1.1  grant 
    641  1.1  grant int
    642  1.1  grant snapper_halt_input(h)
    643  1.1  grant 	void *h;
    644  1.1  grant {
    645  1.1  grant 	struct snapper_softc *sc = h;
    646  1.1  grant 
    647  1.1  grant 	dbdma_stop(sc->sc_idma);
    648  1.1  grant 	dbdma_reset(sc->sc_idma);
    649  1.1  grant 	return 0;
    650  1.1  grant }
    651  1.1  grant 
    652  1.1  grant int
    653  1.1  grant snapper_getdev(h, retp)
    654  1.1  grant 	void *h;
    655  1.1  grant 	struct audio_device *retp;
    656  1.1  grant {
    657  1.1  grant 	*retp = snapper_device;
    658  1.1  grant 	return 0;
    659  1.1  grant }
    660  1.1  grant 
    661  1.1  grant enum {
    662  1.1  grant 	SNAPPER_MONITOR_CLASS,
    663  1.1  grant 	SNAPPER_OUTPUT_CLASS,
    664  1.1  grant 	SNAPPER_RECORD_CLASS,
    665  1.1  grant 	SNAPPER_OUTPUT_SELECT,
    666  1.1  grant 	SNAPPER_VOL_OUTPUT,
    667  1.1  grant 	SNAPPER_INPUT_SELECT,
    668  1.1  grant 	SNAPPER_VOL_INPUT,
    669  1.1  grant 	SNAPPER_ENUM_LAST
    670  1.1  grant };
    671  1.1  grant 
    672  1.1  grant int
    673  1.1  grant snapper_set_port(h, mc)
    674  1.1  grant 	void *h;
    675  1.1  grant 	mixer_ctrl_t *mc;
    676  1.1  grant {
    677  1.1  grant 	struct snapper_softc *sc = h;
    678  1.1  grant 	int l, r;
    679  1.1  grant 
    680  1.1  grant 	DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    681  1.1  grant 
    682  1.1  grant 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    683  1.1  grant 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    684  1.1  grant 
    685  1.1  grant 	switch (mc->dev) {
    686  1.1  grant 	case SNAPPER_OUTPUT_SELECT:
    687  1.1  grant 		/* No change necessary? */
    688  1.1  grant 		if (mc->un.mask == sc->sc_output_mask)
    689  1.1  grant 			return 0;
    690  1.1  grant 
    691  1.1  grant 		snapper_mute_speaker(sc, 1);
    692  1.1  grant 		snapper_mute_headphone(sc, 1);
    693  1.1  grant 		if (mc->un.mask & 1 << 0)
    694  1.1  grant 			snapper_mute_speaker(sc, 0);
    695  1.1  grant 		if (mc->un.mask & 1 << 1)
    696  1.1  grant 			snapper_mute_headphone(sc, 0);
    697  1.1  grant 
    698  1.1  grant 		sc->sc_output_mask = mc->un.mask;
    699  1.1  grant 		return 0;
    700  1.1  grant 
    701  1.1  grant 	case SNAPPER_VOL_OUTPUT:
    702  1.1  grant 		snapper_set_volume(sc, l, r);
    703  1.1  grant 		return 0;
    704  1.1  grant 
    705  1.1  grant 	case SNAPPER_INPUT_SELECT:
    706  1.1  grant 		/* no change necessary? */
    707  1.1  grant 		if (mc->un.mask == sc->sc_record_source)
    708  1.1  grant 			return 0;
    709  1.1  grant 		switch (mc->un.mask) {
    710  1.1  grant 		case 1 << 0: /* CD */
    711  1.1  grant 		case 1 << 1: /* microphone */
    712  1.1  grant 		case 1 << 2: /* line in */
    713  1.1  grant 			/* XXX TO BE DONE */
    714  1.1  grant 			break;
    715  1.1  grant 		default: /* invalid argument */
    716  1.1  grant 			return EINVAL;
    717  1.1  grant 		}
    718  1.1  grant 		sc->sc_record_source = mc->un.mask;
    719  1.1  grant 		return 0;
    720  1.1  grant 
    721  1.1  grant 	case SNAPPER_VOL_INPUT:
    722  1.1  grant 		/* XXX TO BE DONE */
    723  1.1  grant 		return 0;
    724  1.1  grant 	}
    725  1.1  grant 
    726  1.1  grant 	return ENXIO;
    727  1.1  grant }
    728  1.1  grant 
    729  1.1  grant int
    730  1.1  grant snapper_get_port(h, mc)
    731  1.1  grant 	void *h;
    732  1.1  grant 	mixer_ctrl_t *mc;
    733  1.1  grant {
    734  1.1  grant 	struct snapper_softc *sc = h;
    735  1.1  grant 
    736  1.1  grant 	DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
    737  1.1  grant 
    738  1.1  grant 	switch (mc->dev) {
    739  1.1  grant 	case SNAPPER_OUTPUT_SELECT:
    740  1.1  grant 		mc->un.mask = sc->sc_output_mask;
    741  1.1  grant 		return 0;
    742  1.1  grant 
    743  1.1  grant 	case SNAPPER_VOL_OUTPUT:
    744  1.1  grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
    745  1.1  grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
    746  1.1  grant 		return 0;
    747  1.1  grant 
    748  1.1  grant 	case SNAPPER_INPUT_SELECT:
    749  1.1  grant 		mc->un.mask = sc->sc_record_source;
    750  1.1  grant 		return 0;
    751  1.1  grant 
    752  1.1  grant 	case SNAPPER_VOL_INPUT:
    753  1.1  grant 		/* XXX TO BE DONE */
    754  1.1  grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
    755  1.1  grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
    756  1.1  grant 		return 0;
    757  1.1  grant 
    758  1.1  grant 	default:
    759  1.1  grant 		return ENXIO;
    760  1.1  grant 	}
    761  1.1  grant 
    762  1.1  grant 	return 0;
    763  1.1  grant }
    764  1.1  grant 
    765  1.1  grant int
    766  1.1  grant snapper_query_devinfo(h, dip)
    767  1.1  grant 	void *h;
    768  1.1  grant 	mixer_devinfo_t *dip;
    769  1.1  grant {
    770  1.1  grant 	switch (dip->index) {
    771  1.1  grant 
    772  1.1  grant 	case SNAPPER_OUTPUT_SELECT:
    773  1.1  grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    774  1.1  grant 		strcpy(dip->label.name, AudioNoutput);
    775  1.1  grant 		dip->type = AUDIO_MIXER_SET;
    776  1.1  grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    777  1.1  grant 		dip->un.s.num_mem = 2;
    778  1.1  grant 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    779  1.1  grant 		dip->un.s.member[0].mask = 1 << 0;
    780  1.1  grant 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    781  1.1  grant 		dip->un.s.member[1].mask = 1 << 1;
    782  1.1  grant 		return 0;
    783  1.1  grant 
    784  1.1  grant 	case SNAPPER_VOL_OUTPUT:
    785  1.1  grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    786  1.1  grant 		strcpy(dip->label.name, AudioNmaster);
    787  1.1  grant 		dip->type = AUDIO_MIXER_VALUE;
    788  1.1  grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    789  1.1  grant 		dip->un.v.num_channels = 2;
    790  1.1  grant 		strcpy(dip->un.v.units.name, AudioNvolume);
    791  1.1  grant 		return 0;
    792  1.1  grant 
    793  1.1  grant 	case SNAPPER_INPUT_SELECT:
    794  1.1  grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    795  1.1  grant 		strcpy(dip->label.name, AudioNsource);
    796  1.1  grant 		dip->type = AUDIO_MIXER_SET;
    797  1.1  grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    798  1.1  grant 		dip->un.s.num_mem = 3;
    799  1.1  grant 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
    800  1.1  grant 		dip->un.s.member[0].mask = 1 << 0;
    801  1.1  grant 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
    802  1.1  grant 		dip->un.s.member[1].mask = 1 << 1;
    803  1.1  grant 		strcpy(dip->un.s.member[2].label.name, AudioNline);
    804  1.1  grant 		dip->un.s.member[2].mask = 1 << 2;
    805  1.1  grant 		return 0;
    806  1.1  grant 
    807  1.1  grant 	case SNAPPER_VOL_INPUT:
    808  1.1  grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    809  1.1  grant 		strcpy(dip->label.name, AudioNrecord);
    810  1.1  grant 		dip->type = AUDIO_MIXER_VALUE;
    811  1.1  grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    812  1.1  grant 		dip->un.v.num_channels = 2;
    813  1.1  grant 		strcpy(dip->un.v.units.name, AudioNvolume);
    814  1.1  grant 		return 0;
    815  1.1  grant 
    816  1.1  grant 	case SNAPPER_MONITOR_CLASS:
    817  1.1  grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    818  1.1  grant 		strcpy(dip->label.name, AudioCmonitor);
    819  1.1  grant 		dip->type = AUDIO_MIXER_CLASS;
    820  1.1  grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    821  1.1  grant 		return 0;
    822  1.1  grant 
    823  1.1  grant 	case SNAPPER_OUTPUT_CLASS:
    824  1.1  grant 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
    825  1.1  grant 		strcpy(dip->label.name, AudioCoutputs);
    826  1.1  grant 		dip->type = AUDIO_MIXER_CLASS;
    827  1.1  grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    828  1.1  grant 		return 0;
    829  1.1  grant 
    830  1.1  grant 	case SNAPPER_RECORD_CLASS:
    831  1.1  grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    832  1.1  grant 		strcpy(dip->label.name, AudioCrecord);
    833  1.1  grant 		dip->type = AUDIO_MIXER_CLASS;
    834  1.1  grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    835  1.1  grant 		return 0;
    836  1.1  grant 	}
    837  1.1  grant 
    838  1.1  grant 	return ENXIO;
    839  1.1  grant }
    840  1.1  grant 
    841  1.1  grant size_t
    842  1.1  grant snapper_round_buffersize(h, dir, size)
    843  1.1  grant 	void *h;
    844  1.1  grant 	int dir;
    845  1.1  grant 	size_t size;
    846  1.1  grant {
    847  1.1  grant 	if (size > 65536)
    848  1.1  grant 		size = 65536;
    849  1.1  grant 	return size;
    850  1.1  grant }
    851  1.1  grant 
    852  1.1  grant paddr_t
    853  1.1  grant snapper_mappage(h, mem, off, prot)
    854  1.1  grant 	void *h;
    855  1.1  grant 	void *mem;
    856  1.1  grant 	off_t off;
    857  1.1  grant 	int prot;
    858  1.1  grant {
    859  1.1  grant 	if (off < 0)
    860  1.1  grant 		return -1;
    861  1.1  grant 	return -1;	/* XXX */
    862  1.1  grant }
    863  1.1  grant 
    864  1.1  grant int
    865  1.1  grant snapper_get_props(h)
    866  1.1  grant 	void *h;
    867  1.1  grant {
    868  1.1  grant 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
    869  1.1  grant }
    870  1.1  grant 
    871  1.1  grant int
    872  1.1  grant snapper_trigger_output(h, start, end, bsize, intr, arg, param)
    873  1.1  grant 	void *h;
    874  1.1  grant 	void *start, *end;
    875  1.1  grant 	int bsize;
    876  1.1  grant 	void (*intr)(void *);
    877  1.1  grant 	void *arg;
    878  1.1  grant 	struct audio_params *param;
    879  1.1  grant {
    880  1.1  grant 	struct snapper_softc *sc = h;
    881  1.1  grant 	struct dbdma_command *cmd = sc->sc_odmacmd;
    882  1.1  grant 	vaddr_t va;
    883  1.1  grant 	int i, len, intmode;
    884  1.1  grant 
    885  1.1  grant 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
    886  1.1  grant 
    887  1.1  grant 	sc->sc_ointr = intr;
    888  1.1  grant 	sc->sc_oarg = arg;
    889  1.1  grant 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
    890  1.1  grant 
    891  1.1  grant #ifdef DIAGNOSTIC
    892  1.1  grant 	if (sc->sc_opages > 16)
    893  1.1  grant 		panic("snapper_trigger_output");
    894  1.1  grant #endif
    895  1.1  grant 
    896  1.1  grant 	va = (vaddr_t)start;
    897  1.1  grant 	len = 0;
    898  1.1  grant 	for (i = sc->sc_opages; i > 0; i--) {
    899  1.1  grant 		len += NBPG;
    900  1.1  grant 		if (len < bsize)
    901  1.1  grant 			intmode = 0;
    902  1.1  grant 		else {
    903  1.1  grant 			len = 0;
    904  1.1  grant 			intmode = DBDMA_INT_ALWAYS;
    905  1.1  grant 		}
    906  1.1  grant 
    907  1.1  grant 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va), intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    908  1.1  grant 		cmd++;
    909  1.1  grant 		va += NBPG;
    910  1.1  grant 	}
    911  1.1  grant 
    912  1.1  grant 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
    913  1.1  grant 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
    914  1.1  grant 
    915  1.1  grant 	dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
    916  1.1  grant 
    917  1.1  grant 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
    918  1.1  grant 
    919  1.1  grant 	return 0;
    920  1.1  grant }
    921  1.1  grant 
    922  1.1  grant int
    923  1.1  grant snapper_trigger_input(h, start, end, bsize, intr, arg, param)
    924  1.1  grant 	void *h;
    925  1.1  grant 	void *start, *end;
    926  1.1  grant 	int bsize;
    927  1.1  grant 	void (*intr)(void *);
    928  1.1  grant 	void *arg;
    929  1.1  grant 	struct audio_params *param;
    930  1.1  grant {
    931  1.1  grant 	printf("snapper_trigger_input called\n");
    932  1.1  grant 
    933  1.1  grant 	return 1;
    934  1.1  grant }
    935  1.1  grant 
    936  1.1  grant void
    937  1.1  grant snapper_set_volume(sc, left, right)
    938  1.1  grant 	struct snapper_softc *sc;
    939  1.1  grant 	int left, right;
    940  1.1  grant {
    941  1.1  grant 	u_char vol[6];
    942  1.1  grant 
    943  1.1  grant 	sc->sc_vol_l = left;
    944  1.1  grant 	sc->sc_vol_r = right;
    945  1.1  grant 
    946  1.1  grant 	left <<= 8;	/* XXX for now */
    947  1.1  grant 	right <<= 8;
    948  1.1  grant 
    949  1.1  grant 	vol[0] = left >> 16;
    950  1.1  grant 	vol[1] = left >> 8;
    951  1.1  grant 	vol[2] = left;
    952  1.1  grant 	vol[3] = right >> 16;
    953  1.1  grant 	vol[4] = right >> 8;
    954  1.1  grant 	vol[5] = right;
    955  1.1  grant 
    956  1.1  grant 	tas3004_write(sc, DEQ_VOLUME, vol);
    957  1.1  grant }
    958  1.1  grant 
    959  1.1  grant #define CLKSRC_49MHz	0x80000000	/* Use 49152000Hz Osc. */
    960  1.1  grant #define CLKSRC_45MHz	0x40000000	/* Use 45158400Hz Osc. */
    961  1.1  grant #define CLKSRC_18MHz	0x00000000	/* Use 18432000Hz Osc. */
    962  1.1  grant #define MCLK_DIV	0x1f000000	/* MCLK = SRC / DIV */
    963  1.1  grant #define  MCLK_DIV1	0x14000000	/*  MCLK = SRC */
    964  1.1  grant #define  MCLK_DIV3	0x13000000	/*  MCLK = SRC / 3 */
    965  1.1  grant #define  MCLK_DIV5	0x12000000	/*  MCLK = SRC / 5 */
    966  1.1  grant #define SCLK_DIV	0x00f00000	/* SCLK = MCLK / DIV */
    967  1.1  grant #define  SCLK_DIV1	0x00800000
    968  1.1  grant #define  SCLK_DIV3	0x00900000
    969  1.1  grant #define SCLK_MASTER	0x00080000	/* Master mode */
    970  1.1  grant #define SCLK_SLAVE	0x00000000	/* Slave mode */
    971  1.1  grant #define SERIAL_FORMAT	0x00070000
    972  1.1  grant #define  SERIAL_SONY	0x00000000
    973  1.1  grant #define  SERIAL_64x	0x00010000
    974  1.1  grant #define  SERIAL_32x	0x00020000
    975  1.1  grant #define  SERIAL_DAV	0x00040000
    976  1.1  grant #define  SERIAL_SILICON	0x00050000
    977  1.1  grant 
    978  1.1  grant // rate = fs = LRCLK
    979  1.1  grant // SCLK = 64*LRCLK (I2S)
    980  1.1  grant // MCLK = 256fs (typ. -- changeable)
    981  1.1  grant 
    982  1.1  grant // MCLK = clksrc / mdiv
    983  1.1  grant // SCLK = MCLK / sdiv
    984  1.1  grant // rate = SCLK / 64    ( = LRCLK = fs)
    985  1.1  grant 
    986  1.1  grant int
    987  1.1  grant snapper_set_rate(sc, rate)
    988  1.1  grant 	struct snapper_softc *sc;
    989  1.1  grant 	int rate;
    990  1.1  grant {
    991  1.1  grant 	u_int reg = 0;
    992  1.1  grant 	int MCLK;
    993  1.1  grant 	int clksrc, mdiv, sdiv;
    994  1.1  grant 	int mclk_fs;
    995  1.1  grant 
    996  1.1  grant 	switch (rate) {
    997  1.1  grant 	case 8000:
    998  1.1  grant 		clksrc = 18432000;		/* 18MHz */
    999  1.1  grant 		reg = CLKSRC_18MHz;
   1000  1.1  grant 		mclk_fs = 256;
   1001  1.1  grant 		break;
   1002  1.1  grant 
   1003  1.1  grant 	case 44100:
   1004  1.1  grant 		clksrc = 45158400;		/* 45MHz */
   1005  1.1  grant 		reg = CLKSRC_45MHz;
   1006  1.1  grant 		mclk_fs = 256;
   1007  1.1  grant 		break;
   1008  1.1  grant 
   1009  1.1  grant 	case 48000:
   1010  1.1  grant 		clksrc = 49152000;		/* 49MHz */
   1011  1.1  grant 		reg = CLKSRC_49MHz;
   1012  1.1  grant 		mclk_fs = 256;
   1013  1.1  grant 		break;
   1014  1.1  grant 
   1015  1.1  grant 	default:
   1016  1.1  grant 		return EINVAL;
   1017  1.1  grant 	}
   1018  1.1  grant 
   1019  1.1  grant 	MCLK = rate * mclk_fs;
   1020  1.1  grant 	mdiv = clksrc / MCLK;			// 4
   1021  1.1  grant 	sdiv = mclk_fs / 64;			// 4
   1022  1.1  grant 
   1023  1.1  grant 	switch (mdiv) {
   1024  1.1  grant 	case 1:
   1025  1.1  grant 		reg |= MCLK_DIV1;
   1026  1.1  grant 		break;
   1027  1.1  grant 	case 3:
   1028  1.1  grant 		reg |= MCLK_DIV3;
   1029  1.1  grant 		break;
   1030  1.1  grant 	case 5:
   1031  1.1  grant 		reg |= MCLK_DIV5;
   1032  1.1  grant 		break;
   1033  1.1  grant 	default:
   1034  1.1  grant 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
   1035  1.1  grant 		break;
   1036  1.1  grant 	}
   1037  1.1  grant 
   1038  1.1  grant 	switch (sdiv) {
   1039  1.1  grant 	case 1:
   1040  1.1  grant 		reg |= SCLK_DIV1;
   1041  1.1  grant 		break;
   1042  1.1  grant 	case 3:
   1043  1.1  grant 		reg |= SCLK_DIV3;
   1044  1.1  grant 		break;
   1045  1.1  grant 	default:
   1046  1.1  grant 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
   1047  1.1  grant 		break;
   1048  1.1  grant 	}
   1049  1.1  grant 
   1050  1.1  grant 	reg |= SCLK_MASTER;	/* XXX master mode */
   1051  1.1  grant 
   1052  1.1  grant 	reg |= SERIAL_64x;
   1053  1.1  grant 
   1054  1.1  grant 	/* stereo input and output */
   1055  1.1  grant 	DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
   1056  1.1  grant 	    in32rb(sc->sc_reg + I2S_WORDSIZE), 0x02000200);
   1057  1.1  grant 	out32rb(sc->sc_reg + I2S_WORDSIZE, 0x02000200);
   1058  1.1  grant 
   1059  1.1  grant 	DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
   1060  1.1  grant 	    in32rb(sc->sc_reg + I2S_FORMAT), reg);
   1061  1.1  grant 	out32rb(sc->sc_reg + I2S_FORMAT, reg);
   1062  1.1  grant 
   1063  1.1  grant 	return 0;
   1064  1.1  grant }
   1065  1.1  grant 
   1066  1.1  grant #define DEQaddr 0x6a
   1067  1.1  grant 
   1068  1.1  grant const struct tas3004_reg tas3004_initdata = {
   1069  1.1  grant 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_20 },	/* MCR1 */
   1070  1.1  grant 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
   1071  1.1  grant 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
   1072  1.1  grant 	{ 0x72 },						/* TREBLE */
   1073  1.1  grant 	{ 0x72 },						/* BASS */
   1074  1.1  grant 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
   1075  1.1  grant 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
   1076  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1077  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1078  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1079  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1080  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1081  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1082  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1083  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1084  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1085  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1086  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1087  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1088  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1089  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1090  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1091  1.1  grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1092  1.1  grant 	{ 0, 0, 0 },						/* LLB_GAIN */
   1093  1.1  grant 	{ 0, 0, 0 },						/* RLB_GAIN */
   1094  1.1  grant 	{ 0 },							/* ACR */
   1095  1.1  grant 	{ 0 }							/* MCR2 */
   1096  1.1  grant };
   1097  1.1  grant 
   1098  1.1  grant const char tas3004_regsize[] = {
   1099  1.1  grant 	0,					/* 0x00 */
   1100  1.1  grant 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
   1101  1.1  grant 	sizeof tas3004_initdata.DRC,		/* 0x02 */
   1102  1.1  grant 	0,					/* 0x03 */
   1103  1.1  grant 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
   1104  1.1  grant 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
   1105  1.1  grant 	sizeof tas3004_initdata.BASS,		/* 0x06 */
   1106  1.1  grant 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
   1107  1.1  grant 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
   1108  1.1  grant 	0,					/* 0x09 */
   1109  1.1  grant 	sizeof tas3004_initdata.LB0,		/* 0x0a */
   1110  1.1  grant 	sizeof tas3004_initdata.LB1,		/* 0x0b */
   1111  1.1  grant 	sizeof tas3004_initdata.LB2,		/* 0x0c */
   1112  1.1  grant 	sizeof tas3004_initdata.LB3,		/* 0x0d */
   1113  1.1  grant 	sizeof tas3004_initdata.LB4,		/* 0x0e */
   1114  1.1  grant 	sizeof tas3004_initdata.LB5,		/* 0x0f */
   1115  1.1  grant 	sizeof tas3004_initdata.LB6,		/* 0x10 */
   1116  1.1  grant 	0,					/* 0x11 */
   1117  1.1  grant 	0,					/* 0x12 */
   1118  1.1  grant 	sizeof tas3004_initdata.RB0,		/* 0x13 */
   1119  1.1  grant 	sizeof tas3004_initdata.RB1,		/* 0x14 */
   1120  1.1  grant 	sizeof tas3004_initdata.RB2,		/* 0x15 */
   1121  1.1  grant 	sizeof tas3004_initdata.RB3,		/* 0x16 */
   1122  1.1  grant 	sizeof tas3004_initdata.RB4,		/* 0x17 */
   1123  1.1  grant 	sizeof tas3004_initdata.RB5,		/* 0x18 */
   1124  1.1  grant 	sizeof tas3004_initdata.RB6,		/* 0x19 */
   1125  1.1  grant 	0,0,0,0, 0,0,
   1126  1.1  grant 	0,					/* 0x20 */
   1127  1.1  grant 	sizeof tas3004_initdata.LLB,		/* 0x21 */
   1128  1.1  grant 	sizeof tas3004_initdata.RLB,		/* 0x22 */
   1129  1.1  grant 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
   1130  1.1  grant 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
   1131  1.1  grant 	0,0,0,0, 0,0,0,0, 0,0,0,
   1132  1.1  grant 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   1133  1.1  grant 	sizeof tas3004_initdata.ACR,		/* 0x40 */
   1134  1.1  grant 	0,					/* 0x41 */
   1135  1.1  grant 	0,					/* 0x42 */
   1136  1.1  grant 	sizeof tas3004_initdata.MCR2		/* 0x43 */
   1137  1.1  grant };
   1138  1.1  grant 
   1139  1.1  grant int
   1140  1.1  grant tas3004_write(sc, reg, data)
   1141  1.1  grant 	struct snapper_softc *sc;
   1142  1.1  grant 	u_int reg;
   1143  1.1  grant 	const void *data;
   1144  1.1  grant {
   1145  1.1  grant 	int size;
   1146  1.1  grant 
   1147  1.1  grant 	KASSERT(reg < sizeof tas3004_regsize);
   1148  1.1  grant 	size = tas3004_regsize[reg];
   1149  1.1  grant 	KASSERT(size > 0);
   1150  1.1  grant 
   1151  1.1  grant 	if (ki2c_write(sc->sc_i2c, DEQaddr, reg, data, size))
   1152  1.1  grant 		return -1;
   1153  1.1  grant 
   1154  1.1  grant 	return 0;
   1155  1.1  grant }
   1156  1.1  grant 
   1157  1.1  grant int
   1158  1.1  grant gpio_read(addr)
   1159  1.1  grant 	char *addr;
   1160  1.1  grant {
   1161  1.1  grant 	if (*addr & GPIO_DATA)
   1162  1.1  grant 		return 1;
   1163  1.1  grant 	return 0;
   1164  1.1  grant }
   1165  1.1  grant 
   1166  1.1  grant void
   1167  1.1  grant gpio_write(addr, val)
   1168  1.1  grant 	char *addr;
   1169  1.1  grant 	int val;
   1170  1.1  grant {
   1171  1.1  grant 	u_int data = GPIO_DDR_OUTPUT;
   1172  1.1  grant 
   1173  1.1  grant 	if (val)
   1174  1.1  grant 		data |= GPIO_DATA;
   1175  1.1  grant 	*addr = data;
   1176  1.1  grant 	asm volatile ("eieio");
   1177  1.1  grant }
   1178  1.1  grant 
   1179  1.1  grant #define headphone_active 0	/* XXX OF */
   1180  1.1  grant #define amp_active 0		/* XXX OF */
   1181  1.1  grant 
   1182  1.1  grant void
   1183  1.1  grant snapper_mute_speaker(sc, mute)
   1184  1.1  grant 	struct snapper_softc *sc;
   1185  1.1  grant 	int mute;
   1186  1.1  grant {
   1187  1.1  grant 	u_int x;
   1188  1.1  grant 
   1189  1.1  grant 	DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
   1190  1.1  grant 
   1191  1.1  grant 	if (mute)
   1192  1.1  grant 		x = amp_active;		/* mute */
   1193  1.1  grant 	else
   1194  1.1  grant 		x = !amp_active;	/* unmute */
   1195  1.1  grant 	if (x != gpio_read(amp_mute))
   1196  1.1  grant 		gpio_write(amp_mute, x);
   1197  1.1  grant 
   1198  1.1  grant 	DPRINTF("%d\n", gpio_read(amp_mute));
   1199  1.1  grant }
   1200  1.1  grant 
   1201  1.1  grant void
   1202  1.1  grant snapper_mute_headphone(sc, mute)
   1203  1.1  grant 	struct snapper_softc *sc;
   1204  1.1  grant 	int mute;
   1205  1.1  grant {
   1206  1.1  grant 	u_int x;
   1207  1.1  grant 
   1208  1.1  grant 	DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
   1209  1.1  grant 
   1210  1.1  grant 	if (mute)
   1211  1.1  grant 		x = headphone_active;	/* mute */
   1212  1.1  grant 	else
   1213  1.1  grant 		x = !headphone_active;	/* unmute */
   1214  1.1  grant 	if (x != gpio_read(headphone_mute))
   1215  1.1  grant 		gpio_write(headphone_mute, x);
   1216  1.1  grant 
   1217  1.1  grant 	DPRINTF("%d\n", gpio_read(headphone_mute));
   1218  1.1  grant }
   1219  1.1  grant 
   1220  1.1  grant int
   1221  1.1  grant snapper_cint(v)
   1222  1.1  grant 	void *v;
   1223  1.1  grant {
   1224  1.1  grant 	struct snapper_softc *sc = v;
   1225  1.1  grant 	u_int sense;
   1226  1.1  grant 
   1227  1.1  grant 	sense = *headphone_detect;
   1228  1.1  grant 	DPRINTF("headphone detect = 0x%x\n", sense);
   1229  1.1  grant 
   1230  1.1  grant 	if (((sense & 0x02) >> 1) == headphone_detect_active) {
   1231  1.1  grant 		DPRINTF("headphone is inserted\n");
   1232  1.1  grant 		snapper_mute_speaker(sc, 1);
   1233  1.1  grant 		snapper_mute_headphone(sc, 0);
   1234  1.1  grant 		sc->sc_output_mask = 1 << 1;
   1235  1.1  grant 	} else {
   1236  1.1  grant 		DPRINTF("headphone is NOT inserted\n");
   1237  1.1  grant 		snapper_mute_speaker(sc, 0);
   1238  1.1  grant 		snapper_mute_headphone(sc, 1);
   1239  1.1  grant 		sc->sc_output_mask = 1 << 0;
   1240  1.1  grant 	}
   1241  1.1  grant 
   1242  1.1  grant 	return 1;
   1243  1.1  grant }
   1244  1.1  grant 
   1245  1.1  grant #define reset_active 0	/* XXX OF */
   1246  1.1  grant 
   1247  1.1  grant #define DEQ_WRITE(sc, reg, addr) \
   1248  1.1  grant 	if (tas3004_write(sc, reg, addr)) goto err
   1249  1.1  grant 
   1250  1.1  grant int
   1251  1.1  grant tas3004_init(sc)
   1252  1.1  grant 	struct snapper_softc *sc;
   1253  1.1  grant {
   1254  1.1  grant 
   1255  1.1  grant 	/* No reset port.  Nothing to do. */
   1256  1.1  grant 	if (audio_hw_reset == NULL)
   1257  1.1  grant 		goto noreset;
   1258  1.1  grant 
   1259  1.1  grant 	/* Reset TAS3004. */
   1260  1.1  grant 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1261  1.1  grant 	delay(100000);				/* XXX Really needed? */
   1262  1.1  grant 
   1263  1.1  grant 	gpio_write(audio_hw_reset, reset_active);	/* Assert RESET */
   1264  1.1  grant 	delay(1);
   1265  1.1  grant 
   1266  1.1  grant 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1267  1.1  grant 	delay(10000);
   1268  1.1  grant 
   1269  1.1  grant noreset:
   1270  1.1  grant 	DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
   1271  1.1  grant 	DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
   1272  1.1  grant 	DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
   1273  1.1  grant 	DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
   1274  1.1  grant 	DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
   1275  1.1  grant 	DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
   1276  1.1  grant 	DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
   1277  1.1  grant 	DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
   1278  1.1  grant 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1279  1.1  grant 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1280  1.1  grant 	DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
   1281  1.1  grant 	DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
   1282  1.1  grant 	DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
   1283  1.1  grant 	DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
   1284  1.1  grant 	DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
   1285  1.1  grant 	DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
   1286  1.1  grant 	DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
   1287  1.1  grant 	DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
   1288  1.1  grant 	DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
   1289  1.1  grant 	DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
   1290  1.1  grant 	DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
   1291  1.1  grant 	DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
   1292  1.1  grant 	DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
   1293  1.1  grant 	DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
   1294  1.1  grant 	DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
   1295  1.1  grant 	DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
   1296  1.1  grant 	DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
   1297  1.1  grant 
   1298  1.1  grant 	return 0;
   1299  1.1  grant err:
   1300  1.1  grant 	printf("tas3004_init: error\n");
   1301  1.1  grant 	return -1;
   1302  1.1  grant }
   1303  1.1  grant 
   1304  1.1  grant /* FCR(0x3c) bits */
   1305  1.1  grant #define I2S0CLKEN	0x1000
   1306  1.1  grant #define I2S0EN		0x2000
   1307  1.1  grant #define I2S1CLKEN	0x080000
   1308  1.1  grant #define I2S1EN		0x100000
   1309  1.1  grant 
   1310  1.1  grant #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN"
   1311  1.1  grant 
   1312  1.1  grant void
   1313  1.1  grant snapper_init(sc, node)
   1314  1.1  grant 	struct snapper_softc *sc;
   1315  1.1  grant 	int node;
   1316  1.1  grant {
   1317  1.1  grant 	int gpio;
   1318  1.1  grant 	int headphone_detect_intr = -1, headphone_detect_intrtype;
   1319  1.1  grant 
   1320  1.1  grant #ifdef SNAPPER_DEBUG
   1321  1.1  grant 	char fcr[32];
   1322  1.1  grant 
   1323  1.1  grant 	bitmask_snprintf(in32rb(0x8000003c), FCR3C_BITMASK, fcr, sizeof fcr);
   1324  1.1  grant 	printf("FCR(0x3c) 0x%s\n", fcr);
   1325  1.1  grant #endif
   1326  1.1  grant 
   1327  1.1  grant 	gpio = getnodebyname(OF_parent(node), "gpio");
   1328  1.1  grant 	DPRINTF(" /gpio 0x%x\n", gpio);
   1329  1.1  grant 	gpio = OF_child(gpio);
   1330  1.1  grant 	while (gpio) {
   1331  1.1  grant 		char name[64], audio_gpio[64];
   1332  1.1  grant 		int intr[2];
   1333  1.1  grant 		char *addr;
   1334  1.1  grant 
   1335  1.1  grant 		bzero(name, sizeof name);
   1336  1.1  grant 		bzero(audio_gpio, sizeof audio_gpio);
   1337  1.1  grant 		addr = 0;
   1338  1.1  grant 		OF_getprop(gpio, "name", name, sizeof name);
   1339  1.1  grant 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
   1340  1.1  grant 		OF_getprop(gpio, "AAPL,address", &addr, sizeof addr);
   1341  1.1  grant 		/* printf("0x%x %s %s\n", gpio, name, audio_gpio); */
   1342  1.1  grant 
   1343  1.1  grant 		/* gpio5 */
   1344  1.1  grant 		if (strcmp(audio_gpio, "headphone-mute") == 0)
   1345  1.1  grant 			headphone_mute = addr;
   1346  1.1  grant 		/* gpio6 */
   1347  1.1  grant 		if (strcmp(audio_gpio, "amp-mute") == 0)
   1348  1.1  grant 			amp_mute = addr;
   1349  1.1  grant 		/* extint-gpio15 */
   1350  1.1  grant 		if (strcmp(audio_gpio, "headphone-detect") == 0) {
   1351  1.1  grant 			headphone_detect = addr;
   1352  1.1  grant 			OF_getprop(gpio, "audio-gpio-active-state",
   1353  1.1  grant 			    &headphone_detect_active, 4);
   1354  1.1  grant 			OF_getprop(gpio, "interrupts", intr, 8);
   1355  1.1  grant 			headphone_detect_intr = intr[0];
   1356  1.1  grant 			headphone_detect_intrtype = intr[1];
   1357  1.1  grant 		}
   1358  1.1  grant 		/* gpio11 (keywest-11) */
   1359  1.1  grant 		if (strcmp(audio_gpio, "audio-hw-reset") == 0)
   1360  1.1  grant 			audio_hw_reset = addr;
   1361  1.1  grant 		gpio = OF_peer(gpio);
   1362  1.1  grant 	}
   1363  1.1  grant 	DPRINTF(" headphone-mute %p\n", headphone_mute);
   1364  1.1  grant 	DPRINTF(" amp-mute %p\n", amp_mute);
   1365  1.1  grant 	DPRINTF(" headphone-detect %p\n", headphone_detect);
   1366  1.1  grant 	DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
   1367  1.1  grant 	DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
   1368  1.1  grant 	DPRINTF(" audio-hw-reset %p\n", audio_hw_reset);
   1369  1.1  grant 
   1370  1.1  grant 	if (headphone_detect_intr != -1)
   1371  1.1  grant 		intr_establish(headphone_detect_intr, IST_EDGE, IPL_AUDIO,
   1372  1.1  grant 		    snapper_cint, sc);
   1373  1.1  grant 
   1374  1.1  grant 	/* "sample-rates" (44100, 48000) */
   1375  1.1  grant 	snapper_set_rate(sc, 44100);
   1376  1.1  grant 
   1377  1.1  grant 	/* Enable headphone interrupt? */
   1378  1.1  grant 	*headphone_detect |= 0x80;
   1379  1.1  grant 	asm volatile ("eieio");
   1380  1.1  grant 
   1381  1.1  grant 	/* i2c_set_port(port); */
   1382  1.1  grant 
   1383  1.1  grant #if 1
   1384  1.1  grant 	/* Enable I2C interrupts. */
   1385  1.1  grant #define IER 4
   1386  1.1  grant #define I2C_INT_DATA 0x01
   1387  1.1  grant #define I2C_INT_ADDR 0x02
   1388  1.1  grant #define I2C_INT_STOP 0x04
   1389  1.1  grant 	ki2c_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
   1390  1.1  grant #endif
   1391  1.1  grant 
   1392  1.1  grant 	if (tas3004_init(sc))
   1393  1.1  grant 		return;
   1394  1.1  grant 
   1395  1.1  grant 	/* Update headphone status. */
   1396  1.1  grant 	snapper_cint(sc);
   1397  1.1  grant 
   1398  1.1  grant 	snapper_set_volume(sc, 80, 80);
   1399  1.1  grant }
   1400