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