Home | History | Annotate | Line # | Download | only in dev
snapper.c revision 1.27
      1 /*	$NetBSD: snapper.c,v 1.27 2008/05/15 20:11:00 macallan Exp $	*/
      2 /*	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp	*/
      3 /*	Id: i2s.c,v 1.12 2005/01/15 14:32:35 tsubai Exp		*/
      4 
      5 /*-
      6  * Copyright (c) 2002, 2003 Tsubai Masanari.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Datasheet is available from
     33  * http://www.ti.com/sc/docs/products/analog/tas3004.html
     34  * http://www.ti.com/sc/docs/products/analog/tas3001.html
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: snapper.c,v 1.27 2008/05/15 20:11:00 macallan Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/audioio.h>
     42 #include <sys/device.h>
     43 #include <sys/systm.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <dev/auconv.h>
     47 #include <dev/audio_if.h>
     48 #include <dev/mulaw.h>
     49 #include <dev/ofw/openfirm.h>
     50 #include <macppc/dev/dbdma.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 #include <dev/i2c/i2cvar.h>
     54 
     55 #include <machine/autoconf.h>
     56 #include <machine/pio.h>
     57 
     58 #include <macppc/dev/deqvar.h>
     59 
     60 #ifdef SNAPPER_DEBUG
     61 # define DPRINTF printf
     62 #else
     63 # define DPRINTF while (0) printf
     64 #endif
     65 
     66 #define SNAPPER_MAXPAGES	16
     67 
     68 struct snapper_softc {
     69 	device_t sc_dev;
     70 	int sc_mode;		  // 0 for TAS3004
     71 #define SNAPPER_IS_TAS3001	1 // codec is TAS3001
     72 #define SNAPPER_SWVOL		2 // software codec
     73 
     74 	int sc_node;
     75 
     76 	struct audio_encoding_set *sc_encodings;
     77 
     78 	void (*sc_ointr)(void *);	/* dma completion intr handler */
     79 	void *sc_oarg;			/* arg for sc_ointr() */
     80 	int sc_opages;			/* # of output pages */
     81 
     82 	void (*sc_iintr)(void *);	/* dma completion intr handler */
     83 	void *sc_iarg;			/* arg for sc_iintr() */
     84 	int sc_ipages;			/* # of input pages */
     85 
     86 	u_int sc_record_source;		/* recording source mask */
     87 	u_int sc_output_mask;		/* output source mask */
     88 
     89 	bus_space_tag_t sc_tag;
     90 	bus_space_handle_t sc_bsh;
     91 	i2c_addr_t sc_deqaddr;
     92 	i2c_tag_t sc_i2c;
     93 	uint32_t sc_baseaddr;
     94 
     95 	int sc_rate;                    /* current sampling rate */
     96 	int sc_bitspersample;
     97 
     98 	int sc_swvol;
     99 
    100 	u_int sc_vol_l;
    101 	u_int sc_vol_r;
    102 	u_int sc_treble;
    103 	u_int sc_bass;
    104 	u_int mixer[6]; /* s1_l, s2_l, an_l, s1_r, s2_r, an_r */
    105 
    106 	bus_space_handle_t sc_odmah;
    107 	bus_space_handle_t sc_idmah;
    108 	dbdma_regmap_t *sc_odma;
    109 	dbdma_regmap_t *sc_idma;
    110 	unsigned char	dbdma_cmdspace[sizeof(struct dbdma_command) * 40 + 15];
    111 	struct dbdma_command *sc_odmacmd;
    112 	struct dbdma_command *sc_idmacmd;
    113 };
    114 
    115 int snapper_match(device_t, struct cfdata *, void *);
    116 void snapper_attach(device_t, device_t, void *);
    117 void snapper_defer(device_t);
    118 int snapper_intr(void *);
    119 int snapper_query_encoding(void *, struct audio_encoding *);
    120 int snapper_set_params(void *, int, int, audio_params_t *,
    121     audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
    122 int snapper_round_blocksize(void *, int, int, const audio_params_t *);
    123 int snapper_halt_output(void *);
    124 int snapper_halt_input(void *);
    125 int snapper_getdev(void *, struct audio_device *);
    126 int snapper_set_port(void *, mixer_ctrl_t *);
    127 int snapper_get_port(void *, mixer_ctrl_t *);
    128 int snapper_query_devinfo(void *, mixer_devinfo_t *);
    129 size_t snapper_round_buffersize(void *, int, size_t);
    130 paddr_t snapper_mappage(void *, void *, off_t, int);
    131 int snapper_get_props(void *);
    132 int snapper_trigger_output(void *, void *, void *, int, void (*)(void *),
    133     void *, const audio_params_t *);
    134 int snapper_trigger_input(void *, void *, void *, int, void (*)(void *),
    135     void *, const audio_params_t *);
    136 void snapper_set_volume(struct snapper_softc *, u_int, u_int);
    137 int snapper_set_rate(struct snapper_softc *);
    138 void snapper_set_treble(struct snapper_softc *, u_int);
    139 void snapper_set_bass(struct snapper_softc *, u_int);
    140 void snapper_write_mixers(struct snapper_softc *);
    141 
    142 int tas3004_write(struct snapper_softc *, u_int, const void *);
    143 static int gpio_read(char *);
    144 static void gpio_write(char *, int);
    145 void snapper_mute_speaker(struct snapper_softc *, int);
    146 void snapper_mute_headphone(struct snapper_softc *, int);
    147 int snapper_cint(void *);
    148 int tas3004_init(struct snapper_softc *);
    149 void snapper_init(struct snapper_softc *, int);
    150 static void snapper_volume_up(device_t);
    151 static void snapper_volume_down(device_t);
    152 
    153 struct snapper_codecvar {
    154 	stream_filter_t	base;
    155 
    156 #ifdef DIAGNOSTIC
    157 # define SNAPPER_CODECVAR_MAGIC		0xC0DEC
    158 	uint32_t	magic;
    159 #endif // DIAGNOSTIC
    160 
    161 	int16_t		rval; // for snapper_fixphase
    162 };
    163 
    164 static stream_filter_t *snapper_filter_factory
    165 	(int (*)(stream_fetcher_t *, audio_stream_t *, int));
    166 static void snapper_filter_dtor(stream_filter_t *);
    167 
    168 /* XXX We can't access the hw device softc from our audio
    169  *     filter -- lame...
    170  */
    171 static u_int snapper_vol_l = 128, snapper_vol_r = 128;
    172 
    173 /* XXX why doesn't auconv define this? */
    174 #define DEFINE_FILTER(name)	\
    175 static int \
    176 name##_fetch_to(stream_fetcher_t *, audio_stream_t *, int); \
    177 stream_filter_t * name(struct audio_softc *, \
    178     const audio_params_t *, const audio_params_t *); \
    179 stream_filter_t * \
    180 name(struct audio_softc *sc, const audio_params_t *from, \
    181      const audio_params_t *to) \
    182 { \
    183 	return snapper_filter_factory(name##_fetch_to); \
    184 } \
    185 static int \
    186 name##_fetch_to(stream_fetcher_t *self, audio_stream_t *dst, int max_used)
    187 
    188 DEFINE_FILTER(snapper_volume)
    189 {
    190 	stream_filter_t *this;
    191 	int16_t j;
    192 	int16_t *wp;
    193 	int m, err;
    194 
    195 	this = (stream_filter_t *)self;
    196 	max_used = (max_used + 1) & ~1;
    197 	if ((err = this->prev->fetch_to(this->prev, this->src, max_used)))
    198 		return err;
    199 	m = (dst->end - dst->start) & ~1;
    200 	m = min(m, max_used);
    201 	FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) {
    202 		j = (s[0] << 8 | s[1]);
    203 		wp = (int16_t *)d;
    204 		*wp = ((j * snapper_vol_l) / 255);
    205 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    206 
    207 	return 0;
    208 }
    209 
    210 /*
    211  * A hardware bug in the TAS3004 I2S transport
    212  * produces phase differences between channels
    213  * (left channel appears delayed by one sample).
    214  * Fix the phase difference by delaying the right channel
    215  * by one sample.
    216  */
    217 DEFINE_FILTER(snapper_fixphase)
    218 {
    219 	struct snapper_codecvar *cv = (struct snapper_codecvar *) self;
    220 	stream_filter_t *this = &cv->base;
    221 	int err, m;
    222 	const int16_t *rp;
    223 	int16_t *wp, rval = cv->rval;
    224 
    225 #ifdef DIAGNOSTIC
    226 	if (cv->magic != SNAPPER_CODECVAR_MAGIC)
    227 		panic("snapper_fixphase");
    228 #endif
    229 	max_used = (max_used + 3) & ~2;
    230 	if ((err = this->prev->fetch_to(this->prev, this->src, max_used)))
    231 		return err;
    232 
    233 	/* work in stereo frames (4 bytes) */
    234 	m = (dst->end - dst->start) & ~2;
    235 	m = min(m, max_used);
    236 	FILTER_LOOP_PROLOGUE(this->src, 4, dst, 4, m) {
    237 		rp = (const int16_t *) s;
    238 		wp = (int16_t *) d;
    239 		wp[0] = rp[0];
    240 		wp[1] = rval;
    241 		rval = rp[1];
    242 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    243 	cv->rval = rval;
    244 
    245 	return 0;
    246 }
    247 
    248 static stream_filter_t *
    249 snapper_filter_factory(int (*fetch_to)(stream_fetcher_t *, audio_stream_t *, int))
    250 {
    251 	struct snapper_codecvar *this;
    252 
    253 	this = malloc(sizeof(*this), M_DEVBUF, M_WAITOK | M_ZERO);
    254 	this->base.base.fetch_to = fetch_to;
    255 	this->base.dtor = snapper_filter_dtor;
    256 	this->base.set_fetcher = stream_filter_set_fetcher;
    257 	this->base.set_inputbuffer = stream_filter_set_inputbuffer;
    258 
    259 #ifdef DIAGNOSTIC
    260 	this->magic = SNAPPER_CODECVAR_MAGIC;
    261 #endif
    262 	return (stream_filter_t *) this;
    263 }
    264 
    265 static void
    266 snapper_filter_dtor(stream_filter_t *this)
    267 {
    268 	if (this != NULL)
    269 		free(this, M_DEVBUF);
    270 }
    271 
    272 CFATTACH_DECL_NEW(snapper, sizeof(struct snapper_softc), snapper_match,
    273 	snapper_attach, NULL, NULL);
    274 
    275 const struct audio_hw_if snapper_hw_if = {
    276 	NULL,
    277 	NULL,
    278 	NULL,
    279 	snapper_query_encoding,
    280 	snapper_set_params,
    281 	snapper_round_blocksize,
    282 	NULL,
    283 	NULL,
    284 	NULL,
    285 	NULL,
    286 	NULL,
    287 	snapper_halt_output,
    288 	snapper_halt_input,
    289 	NULL,
    290 	snapper_getdev,
    291 	NULL,
    292 	snapper_set_port,
    293 	snapper_get_port,
    294 	snapper_query_devinfo,
    295 	NULL,
    296 	NULL,
    297 	snapper_round_buffersize,
    298 	snapper_mappage,
    299 	snapper_get_props,
    300 	snapper_trigger_output,
    301 	snapper_trigger_input,
    302 	NULL,
    303 	NULL
    304 };
    305 
    306 struct audio_device snapper_device = {
    307 	"SNAPPER",
    308 	"",
    309 	"snapper"
    310 };
    311 
    312 #define SNAPPER_BASSTAB_0DB	18
    313 const uint8_t snapper_basstab[] = {
    314 	0x96,	/* -18dB */
    315 	0x94,	/* -17dB */
    316 	0x92,	/* -16dB */
    317 	0x90,	/* -15dB */
    318 	0x8e,	/* -14dB */
    319 	0x8c,	/* -13dB */
    320 	0x8a,	/* -12dB */
    321 	0x88,	/* -11dB */
    322 	0x86,	/* -10dB */
    323 	0x84,	/* -9dB */
    324 	0x82,	/* -8dB */
    325 	0x80,	/* -7dB */
    326 	0x7e,	/* -6dB */
    327 	0x7c,	/* -5dB */
    328 	0x7a,	/* -4dB */
    329 	0x78,	/* -3dB */
    330 	0x76,	/* -2dB */
    331 	0x74,	/* -1dB */
    332 	0x72,	/* 0dB */
    333 	0x6f,	/* 1dB */
    334 	0x6d,	/* 2dB */
    335 	0x6a,	/* 3dB */
    336 	0x67,	/* 4dB */
    337 	0x65,	/* 5dB */
    338 	0x62,	/* 6dB */
    339 	0x5f,	/* 7dB */
    340 	0x5b,	/* 8dB */
    341 	0x55,	/* 9dB */
    342 	0x4f,	/* 10dB */
    343 	0x49,	/* 11dB */
    344 	0x43,	/* 12dB */
    345 	0x3b,	/* 13dB */
    346 	0x33,	/* 14dB */
    347 	0x29,	/* 15dB */
    348 	0x1e,	/* 16dB */
    349 	0x11,	/* 17dB */
    350 	0x01,	/* 18dB */
    351 };
    352 
    353 #define SNAPPER_MIXER_GAIN_0DB		36
    354 const uint8_t snapper_mixer_gain[178][3] = {
    355 	{ 0x7f, 0x17, 0xaf }, /* 18.0 dB */
    356 	{ 0x77, 0xfb, 0xaa }, /* 17.5 dB */
    357 	{ 0x71, 0x45, 0x75 }, /* 17.0 dB */
    358 	{ 0x6a, 0xef, 0x5d }, /* 16.5 dB */
    359 	{ 0x64, 0xf4, 0x03 }, /* 16.0 dB */
    360 	{ 0x5f, 0x4e, 0x52 }, /* 15.5 dB */
    361 	{ 0x59, 0xf9, 0x80 }, /* 15.0 dB */
    362 	{ 0x54, 0xf1, 0x06 }, /* 14.5 dB */
    363 	{ 0x50, 0x30, 0xa1 }, /* 14.0 dB */
    364 	{ 0x4b, 0xb4, 0x46 }, /* 13.5 dB */
    365 	{ 0x47, 0x78, 0x28 }, /* 13.0 dB */
    366 	{ 0x43, 0x78, 0xb0 }, /* 12.5 dB */
    367 	{ 0x3f, 0xb2, 0x78 }, /* 12.0 dB */
    368 	{ 0x3c, 0x22, 0x4c }, /* 11.5 dB */
    369 	{ 0x38, 0xc5, 0x28 }, /* 11.0 dB */
    370 	{ 0x35, 0x98, 0x2f }, /* 10.5 dB */
    371 	{ 0x32, 0x98, 0xb0 }, /* 10.0 dB */
    372 	{ 0x2f, 0xc4, 0x20 }, /* 9.5 dB */
    373 	{ 0x2d, 0x18, 0x18 }, /* 9.0 dB */
    374 	{ 0x2a, 0x92, 0x54 }, /* 8.5 dB */
    375 	{ 0x28, 0x30, 0xaf }, /* 8.0 dB */
    376 	{ 0x25, 0xf1, 0x25 }, /* 7.5 dB */
    377 	{ 0x23, 0xd1, 0xcd }, /* 7.0 dB */
    378 	{ 0x21, 0xd0, 0xd9 }, /* 6.5 dB */
    379 	{ 0x1f, 0xec, 0x98 }, /* 6.0 dB */
    380 	{ 0x1e, 0x23, 0x6d }, /* 5.5 dB */
    381 	{ 0x1c, 0x73, 0xd5 }, /* 5.0 dB */
    382 	{ 0x1a, 0xdc, 0x61 }, /* 4.5 dB */
    383 	{ 0x19, 0x5b, 0xb8 }, /* 4.0 dB */
    384 	{ 0x17, 0xf0, 0x94 }, /* 3.5 dB */
    385 	{ 0x16, 0x99, 0xc0 }, /* 3.0 dB */
    386 	{ 0x15, 0x56, 0x1a }, /* 2.5 dB */
    387 	{ 0x14, 0x24, 0x8e }, /* 2.0 dB */
    388 	{ 0x13, 0x04, 0x1a }, /* 1.5 dB */
    389 	{ 0x11, 0xf3, 0xc9 }, /* 1.0 dB */
    390 	{ 0x10, 0xf2, 0xb4 }, /* 0.5 dB */
    391 	{ 0x10, 0x00, 0x00 }, /* 0.0 dB */
    392 	{ 0x0f, 0x1a, 0xdf }, /* -0.5 dB */
    393 	{ 0x0e, 0x42, 0x90 }, /* -1.0 dB */
    394 	{ 0x0d, 0x76, 0x5a }, /* -1.5 dB */
    395 	{ 0x0c, 0xb5, 0x91 }, /* -2.0 dB */
    396 	{ 0x0b, 0xff, 0x91 }, /* -2.5 dB */
    397 	{ 0x0b, 0x53, 0xbe }, /* -3.0 dB */
    398 	{ 0x0a, 0xb1, 0x89 }, /* -3.5 dB */
    399 	{ 0x0a, 0x18, 0x66 }, /* -4.0 dB */
    400 	{ 0x09, 0x87, 0xd5 }, /* -4.5 dB */
    401 	{ 0x08, 0xff, 0x59 }, /* -5.0 dB */
    402 	{ 0x08, 0x7e, 0x80 }, /* -5.5 dB */
    403 	{ 0x08, 0x04, 0xdc }, /* -6.0 dB */
    404 	{ 0x07, 0x92, 0x07 }, /* -6.5 dB */
    405 	{ 0x07, 0x25, 0x9d }, /* -7.0 dB */
    406 	{ 0x06, 0xbf, 0x44 }, /* -7.5 dB */
    407 	{ 0x06, 0x5e, 0xa5 }, /* -8.0 dB */
    408 	{ 0x06, 0x03, 0x6e }, /* -8.5 dB */
    409 	{ 0x05, 0xad, 0x50 }, /* -9.0 dB */
    410 	{ 0x05, 0x5c, 0x04 }, /* -9.5 dB */
    411 	{ 0x05, 0x0f, 0x44 }, /* -10.0 dB */
    412 	{ 0x04, 0xc6, 0xd0 }, /* -10.5 dB */
    413 	{ 0x04, 0x82, 0x68 }, /* -11.0 dB */
    414 	{ 0x04, 0x41, 0xd5 }, /* -11.5 dB */
    415 	{ 0x04, 0x04, 0xde }, /* -12.0 dB */
    416 	{ 0x03, 0xcb, 0x50 }, /* -12.5 dB */
    417 	{ 0x03, 0x94, 0xfa }, /* -13.0 dB */
    418 	{ 0x03, 0x61, 0xaf }, /* -13.5 dB */
    419 	{ 0x03, 0x31, 0x42 }, /* -14.0 dB */
    420 	{ 0x03, 0x03, 0x8a }, /* -14.5 dB */
    421 	{ 0x02, 0xd8, 0x62 }, /* -15.0 dB */
    422 	{ 0x02, 0xaf, 0xa3 }, /* -15.5 dB */
    423 	{ 0x02, 0x89, 0x2c }, /* -16.0 dB */
    424 	{ 0x02, 0x64, 0xdb }, /* -16.5 dB */
    425 	{ 0x02, 0x42, 0x93 }, /* -17.0 dB */
    426 	{ 0x02, 0x22, 0x35 }, /* -17.5 dB */
    427 	{ 0x02, 0x03, 0xa7 }, /* -18.0 dB */
    428 	{ 0x01, 0xe6, 0xcf }, /* -18.5 dB */
    429 	{ 0x01, 0xcb, 0x94 }, /* -19.0 dB */
    430 	{ 0x01, 0xb1, 0xde }, /* -19.5 dB */
    431 	{ 0x01, 0x99, 0x99 }, /* -20.0 dB */
    432 	{ 0x01, 0x82, 0xaf }, /* -20.5 dB */
    433 	{ 0x01, 0x6d, 0x0e }, /* -21.0 dB */
    434 	{ 0x01, 0x58, 0xa2 }, /* -21.5 dB */
    435 	{ 0x01, 0x45, 0x5b }, /* -22.0 dB */
    436 	{ 0x01, 0x33, 0x28 }, /* -22.5 dB */
    437 	{ 0x01, 0x21, 0xf9 }, /* -23.0 dB */
    438 	{ 0x01, 0x11, 0xc0 }, /* -23.5 dB */
    439 	{ 0x01, 0x02, 0x70 }, /* -24.0 dB */
    440 	{ 0x00, 0xf3, 0xfb }, /* -24.5 dB */
    441 	{ 0x00, 0xe6, 0x55 }, /* -25.0 dB */
    442 	{ 0x00, 0xd9, 0x73 }, /* -25.5 dB */
    443 	{ 0x00, 0xcd, 0x49 }, /* -26.0 dB */
    444 	{ 0x00, 0xc1, 0xcd }, /* -26.5 dB */
    445 	{ 0x00, 0xb6, 0xf6 }, /* -27.0 dB */
    446 	{ 0x00, 0xac, 0xba }, /* -27.5 dB */
    447 	{ 0x00, 0xa3, 0x10 }, /* -28.0 dB */
    448 	{ 0x00, 0x99, 0xf1 }, /* -28.5 dB */
    449 	{ 0x00, 0x91, 0x54 }, /* -29.0 dB */
    450 	{ 0x00, 0x89, 0x33 }, /* -29.5 dB */
    451 	{ 0x00, 0x81, 0x86 }, /* -30.0 dB */
    452 	{ 0x00, 0x7a, 0x48 }, /* -30.5 dB */
    453 	{ 0x00, 0x73, 0x70 }, /* -31.0 dB */
    454 	{ 0x00, 0x6c, 0xfb }, /* -31.5 dB */
    455 	{ 0x00, 0x66, 0xe3 }, /* -32.0 dB */
    456 	{ 0x00, 0x61, 0x21 }, /* -32.5 dB */
    457 	{ 0x00, 0x5b, 0xb2 }, /* -33.0 dB */
    458 	{ 0x00, 0x56, 0x91 }, /* -33.5 dB */
    459 	{ 0x00, 0x51, 0xb9 }, /* -34.0 dB */
    460 	{ 0x00, 0x4d, 0x27 }, /* -34.5 dB */
    461 	{ 0x00, 0x48, 0xd6 }, /* -35.0 dB */
    462 	{ 0x00, 0x44, 0xc3 }, /* -35.5 dB */
    463 	{ 0x00, 0x40, 0xea }, /* -36.0 dB */
    464 	{ 0x00, 0x3d, 0x49 }, /* -36.5 dB */
    465 	{ 0x00, 0x39, 0xdb }, /* -37.0 dB */
    466 	{ 0x00, 0x36, 0x9e }, /* -37.5 dB */
    467 	{ 0x00, 0x33, 0x90 }, /* -38.0 dB */
    468 	{ 0x00, 0x30, 0xae }, /* -38.5 dB */
    469 	{ 0x00, 0x2d, 0xf5 }, /* -39.0 dB */
    470 	{ 0x00, 0x2b, 0x63 }, /* -39.5 dB */
    471 	{ 0x00, 0x28, 0xf5 }, /* -40.0 dB */
    472 	{ 0x00, 0x26, 0xab }, /* -40.5 dB */
    473 	{ 0x00, 0x24, 0x81 }, /* -41.0 dB */
    474 	{ 0x00, 0x22, 0x76 }, /* -41.5 dB */
    475 	{ 0x00, 0x20, 0x89 }, /* -42.0 dB */
    476 	{ 0x00, 0x1e, 0xb7 }, /* -42.5 dB */
    477 	{ 0x00, 0x1c, 0xff }, /* -43.0 dB */
    478 	{ 0x00, 0x1b, 0x60 }, /* -43.5 dB */
    479 	{ 0x00, 0x19, 0xd8 }, /* -44.0 dB */
    480 	{ 0x00, 0x18, 0x65 }, /* -44.5 dB */
    481 	{ 0x00, 0x17, 0x08 }, /* -45.0 dB */
    482 	{ 0x00, 0x15, 0xbe }, /* -45.5 dB */
    483 	{ 0x00, 0x14, 0x87 }, /* -46.0 dB */
    484 	{ 0x00, 0x13, 0x61 }, /* -46.5 dB */
    485 	{ 0x00, 0x12, 0x4b }, /* -47.0 dB */
    486 	{ 0x00, 0x11, 0x45 }, /* -47.5 dB */
    487 	{ 0x00, 0x10, 0x4e }, /* -48.0 dB */
    488 	{ 0x00, 0x0f, 0x64 }, /* -48.5 dB */
    489 	{ 0x00, 0x0e, 0x88 }, /* -49.0 dB */
    490 	{ 0x00, 0x0d, 0xb8 }, /* -49.5 dB */
    491 	{ 0x00, 0x0c, 0xf3 }, /* -50.0 dB */
    492 	{ 0x00, 0x0c, 0x3a }, /* -50.5 dB */
    493 	{ 0x00, 0x0b, 0x8b }, /* -51.0 dB */
    494 	{ 0x00, 0x0a, 0xe5 }, /* -51.5 dB */
    495 	{ 0x00, 0x0a, 0x49 }, /* -52.0 dB */
    496 	{ 0x00, 0x09, 0xb6 }, /* -52.5 dB */
    497 	{ 0x00, 0x09, 0x2b }, /* -53.0 dB */
    498 	{ 0x00, 0x08, 0xa8 }, /* -53.5 dB */
    499 	{ 0x00, 0x08, 0x2c }, /* -54.0 dB */
    500 	{ 0x00, 0x07, 0xb7 }, /* -54.5 dB */
    501 	{ 0x00, 0x07, 0x48 }, /* -55.0 dB */
    502 	{ 0x00, 0x06, 0xe0 }, /* -55.5 dB */
    503 	{ 0x00, 0x06, 0x7d }, /* -56.0 dB */
    504 	{ 0x00, 0x06, 0x20 }, /* -56.5 dB */
    505 	{ 0x00, 0x05, 0xc9 }, /* -57.0 dB */
    506 	{ 0x00, 0x05, 0x76 }, /* -57.5 dB */
    507 	{ 0x00, 0x05, 0x28 }, /* -58.0 dB */
    508 	{ 0x00, 0x04, 0xde }, /* -58.5 dB */
    509 	{ 0x00, 0x04, 0x98 }, /* -59.0 dB */
    510 	{ 0x00, 0x04, 0x56 }, /* -59.5 dB */
    511 	{ 0x00, 0x04, 0x18 }, /* -60.0 dB */
    512 	{ 0x00, 0x03, 0xdd }, /* -60.5 dB */
    513 	{ 0x00, 0x03, 0xa6 }, /* -61.0 dB */
    514 	{ 0x00, 0x03, 0x72 }, /* -61.5 dB */
    515 	{ 0x00, 0x03, 0x40 }, /* -62.0 dB */
    516 	{ 0x00, 0x03, 0x12 }, /* -62.5 dB */
    517 	{ 0x00, 0x02, 0xe6 }, /* -63.0 dB */
    518 	{ 0x00, 0x02, 0xbc }, /* -63.5 dB */
    519 	{ 0x00, 0x02, 0x95 }, /* -64.0 dB */
    520 	{ 0x00, 0x02, 0x70 }, /* -64.5 dB */
    521 	{ 0x00, 0x02, 0x4d }, /* -65.0 dB */
    522 	{ 0x00, 0x02, 0x2c }, /* -65.5 dB */
    523 	{ 0x00, 0x02, 0x0d }, /* -66.0 dB */
    524 	{ 0x00, 0x01, 0xf0 }, /* -66.5 dB */
    525 	{ 0x00, 0x01, 0xd4 }, /* -67.0 dB */
    526 	{ 0x00, 0x01, 0xba }, /* -67.5 dB */
    527 	{ 0x00, 0x01, 0xa1 }, /* -68.0 dB */
    528 	{ 0x00, 0x01, 0x8a }, /* -68.5 dB */
    529 	{ 0x00, 0x01, 0x74 }, /* -69.0 dB */
    530 	{ 0x00, 0x01, 0x5f }, /* -69.5 dB */
    531 	{ 0x00, 0x01, 0x4b }, /* -70.0 dB */
    532 	{ 0x00, 0x00, 0x00 }  /* Mute */
    533 };
    534 
    535 #define SNAPPER_NFORMATS	2
    536 static const struct audio_format snapper_formats[SNAPPER_NFORMATS] = {
    537 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    538 	 2, AUFMT_STEREO, 3, {32000, 44100, 48000}},
    539 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 24, 24,
    540 	 2, AUFMT_STEREO, 3, {32000, 44100, 48000}},
    541 };
    542 
    543 #define TUMBLER_NFORMATS	1
    544 static const struct audio_format tumbler_formats[TUMBLER_NFORMATS] = {
    545 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    546 	 2, AUFMT_STEREO, 4, {32000, 44100, 48000, 96000}},
    547 };
    548 
    549 static u_char *amp_mute;
    550 static u_char *headphone_mute;
    551 static u_char *audio_hw_reset;
    552 static u_char *headphone_detect;
    553 static int headphone_detect_active;
    554 
    555 
    556 /* I2S registers */
    557 #define I2S_INT		0x00
    558 #define I2S_FORMAT	0x10
    559 #define I2S_FRAMECOUNT	0x40
    560 #define I2S_FRAMEMATCH	0x50
    561 #define I2S_WORDSIZE	0x60
    562 
    563 /* I2S_INT register definitions */
    564 #define I2S_INT_CLKSTOPPEND 0x01000000  /* clock-stop interrupt pending */
    565 
    566 /* FCR(0x3c) bits */
    567 #define KEYLARGO_FCR1   0x3c
    568 #define  I2S0CLKEN      0x1000
    569 #define  I2S0EN         0x2000
    570 #define  I2S1CLKEN      0x080000
    571 #define  I2S1EN         0x100000
    572 #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN"
    573 
    574 /* TAS3004/TAS3001 registers */
    575 #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
    576 #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?)
    577                             	   2 bytes (reserved) on the TAS 3001 */
    578 #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
    579 #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
    580 #define DEQ_BASS	0x06	/* Bass control (1byte) */
    581 #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes; 3 on TAS3001) */
    582 #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes; 3 on TAS3001) */
    583 #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
    584 #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
    585 #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
    586 #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
    587 #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
    588 #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
    589 #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
    590 #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
    591 #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
    592 #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
    593 #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
    594 #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
    595 #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
    596 #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
    597 #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
    598 #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
    599 #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
    600 #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
    601 #define DEQ_ACR		0x40	/* [TAS3004] Analog control register (1byte) */
    602 #define DEQ_MCR2	0x43	/* [TAS3004] Main control register 2 (1byte) */
    603 #define DEQ_MCR1_FL	0x80	/* Fast load */
    604 #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
    605 #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
    606 #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
    607 #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
    608 #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
    609 #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
    610 #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
    611 #define DEQ_MCR1_ISM	0x0c	/* [TAS3001] Input serial port mode */
    612 #define  DEQ_MCR1_ISM_L	0x00	/*           Left justified */
    613 #define  DEQ_MCR1_ISM_R	0x04	/*           Right justified */
    614 #define  DEQ_MCR1_ISM_I2S 0x08	/*           I2S */
    615 #define DEQ_MCR1_W	0x03	/* Serial port word length */
    616 #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
    617 #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
    618 #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
    619 #define  DEQ_MCR1_W_24	0x03	/*  20 bit */
    620 
    621 #define DEQ_MCR2_DL	0x80	/* Download */
    622 #define DEQ_MCR2_AP	0x02	/* All pass mode */
    623 
    624 #define DEQ_ACR_ADM	0x80	/* ADC output mode */
    625 #define DEQ_ACR_LRB	0x40	/* Select B input */
    626 #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
    627 #define  DEQ_ACR_DM_OFF	0x00	/*  off */
    628 #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
    629 #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
    630 #define DEQ_ACR_INP	0x02	/* Analog input select */
    631 #define  DEQ_ACR_INP_A	0x00	/*  A */
    632 #define  DEQ_ACR_INP_B	0x02	/*  B */
    633 #define DEQ_ACR_APD	0x01	/* Analog power down */
    634 
    635 struct tas3004_reg {
    636 	u_char MCR1[1];
    637 	u_char DRC[6];
    638 	u_char VOLUME[6];
    639 	u_char TREBLE[1];
    640 	u_char BASS[1];
    641 	u_char MIXER_L[9];
    642 	u_char MIXER_R[9];
    643 	u_char LB0[15];
    644 	u_char LB1[15];
    645 	u_char LB2[15];
    646 	u_char LB3[15];
    647 	u_char LB4[15];
    648 	u_char LB5[15];
    649 	u_char LB6[15];
    650 	u_char RB0[15];
    651 	u_char RB1[15];
    652 	u_char RB2[15];
    653 	u_char RB3[15];
    654 	u_char RB4[15];
    655 	u_char RB5[15];
    656 	u_char RB6[15];
    657 	u_char LLB[15];
    658 	u_char RLB[15];
    659 	u_char LLB_GAIN[3];
    660 	u_char RLB_GAIN[3];
    661 	u_char ACR[1];
    662 	u_char MCR2[1];
    663 };
    664 
    665 #define GPIO_OUTSEL	0xf0	/* Output select */
    666 		/*	0x00	GPIO bit0 is output
    667 			0x10	media-bay power
    668 			0x20	reserved
    669 			0x30	MPIC */
    670 
    671 #define GPIO_ALTOE	0x08	/* Alternate output enable */
    672 		/*	0x00	Use DDR
    673 			0x08	Use output select */
    674 
    675 #define GPIO_DDR	0x04	/* Data direction */
    676 #define GPIO_DDR_OUTPUT	0x04	/* Output */
    677 #define GPIO_DDR_INPUT	0x00	/* Input */
    678 
    679 #define GPIO_LEVEL	0x02	/* Pin level (RO) */
    680 
    681 #define	GPIO_DATA	0x01	/* Data */
    682 
    683 int
    684 snapper_match(device_t parent, struct cfdata *match, void *aux)
    685 {
    686 	struct confargs *ca;
    687 	int soundbus, soundchip, soundcodec;
    688 	char compat[32];
    689 
    690 	ca = aux;
    691 	if (strcmp(ca->ca_name, "i2s") != 0)
    692 		return 0;
    693 
    694 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
    695 	    (soundchip = OF_child(soundbus)) == 0)
    696 		return 0;
    697 
    698 	bzero(compat, sizeof compat);
    699 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
    700 
    701 	if (strcmp(compat, "snapper") == 0)
    702 		return 1;
    703 
    704 	if (strcmp(compat, "tumbler") == 0)
    705 		return 1;
    706 
    707 	if (strcmp(compat, "AOAKeylargo") == 0)
    708 		return 1;
    709 
    710 	if (strcmp(compat, "AOAK2") == 0)
    711 		return 1;
    712 
    713 	if (OF_getprop(soundchip, "platform-tas-codec-ref",
    714 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
    715 		return 1;
    716 
    717 	return 0;
    718 }
    719 
    720 void
    721 snapper_attach(device_t parent, device_t self, void *aux)
    722 {
    723 	struct snapper_softc *sc;
    724 	struct confargs *ca;
    725 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    726 	int soundbus, intr[6];
    727 	char compat[32];
    728 
    729 	sc = device_private(self);
    730 	sc->sc_dev = self;
    731 
    732 	ca = aux;
    733 
    734 	soundbus = OF_child(ca->ca_node);
    735 	bzero(compat, sizeof compat);
    736 	OF_getprop(OF_child(soundbus), "compatible", compat, sizeof compat);
    737 
    738 	if (strcmp(compat, "tumbler") == 0)
    739 		sc->sc_mode = SNAPPER_IS_TAS3001;
    740 
    741 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
    742 		if (auconv_create_encodings(tumbler_formats, TUMBLER_NFORMATS,
    743 				&sc->sc_encodings) != 0) {
    744 			aprint_normal("can't create encodings\n");
    745 			return;
    746 		}
    747 	} else {
    748 		if (auconv_create_encodings(snapper_formats, SNAPPER_NFORMATS,
    749 				&sc->sc_encodings) != 0) {
    750 			aprint_normal("can't create encodings\n");
    751 			return;
    752 		}
    753 	}
    754 
    755 	sc->sc_odmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    756 				     sizeof(struct dbdma_command));
    757 	sc->sc_idmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    758 				     sizeof(struct dbdma_command));
    759 
    760 	sc->sc_baseaddr = ca->ca_baseaddr;
    761 	ca->ca_reg[0] += ca->ca_baseaddr;
    762 	ca->ca_reg[2] += ca->ca_baseaddr;
    763 	ca->ca_reg[4] += ca->ca_baseaddr;
    764 
    765 	sc->sc_node = ca->ca_node;
    766 	sc->sc_tag = ca->ca_tag;
    767 	bus_space_map(sc->sc_tag, ca->ca_reg[0], ca->ca_reg[1], 0, &sc->sc_bsh);
    768 	bus_space_map(sc->sc_tag, ca->ca_reg[2], ca->ca_reg[3],
    769 	    BUS_SPACE_MAP_LINEAR, &sc->sc_odmah);
    770 	bus_space_map(sc->sc_tag, ca->ca_reg[4], ca->ca_reg[5],
    771 	    BUS_SPACE_MAP_LINEAR, &sc->sc_idmah);
    772 	sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
    773 	sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah);
    774 
    775 	OF_getprop(soundbus, "interrupts", intr, sizeof intr);
    776 	cirq = intr[0];
    777 	oirq = intr[2];
    778 	iirq = intr[4];
    779 	cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    780 	oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    781 	iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    782 
    783 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
    784 	intr_establish(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc);
    785 	intr_establish(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc);
    786 
    787 	aprint_normal(": irq %d,%d,%d\n", cirq, oirq, iirq);
    788 
    789 	/* PMF event handler */
    790 	pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
    791 	    snapper_volume_down, TRUE);
    792 	pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
    793 	    snapper_volume_up, TRUE);
    794 	config_defer(self, snapper_defer);
    795 }
    796 
    797 void
    798 snapper_defer(device_t dev)
    799 {
    800 	struct snapper_softc *sc;
    801 	device_t dv;
    802 	struct deq_softc *deq;
    803 
    804 	sc = device_private(dev);
    805 	TAILQ_FOREACH(dv, &alldevs, dv_list) {
    806 		if (device_is_a(dv, "deq")) {
    807 			deq = device_private(dv);
    808 			sc->sc_i2c = deq->sc_i2c;
    809 			sc->sc_deqaddr = deq->sc_address;
    810 		}
    811 	}
    812 
    813 	/* If we don't find a codec, it's not the end of the world;
    814 	 * we can control the volume in software in this case.
    815 	 */
    816 	if (sc->sc_i2c == NULL)
    817 		sc->sc_mode = SNAPPER_SWVOL;
    818 
    819 	switch (sc->sc_mode) {
    820 	case SNAPPER_SWVOL:
    821 		aprint_verbose("%s: software codec\n", device_xname(dev));
    822 		break;
    823 	case SNAPPER_IS_TAS3001:
    824 		aprint_verbose("%s: codec: TAS3001\n", device_xname(dev));
    825 		break;
    826 	case 0:
    827 		aprint_verbose("%s: codec: TAS3004\n", device_xname(dev));
    828 		break;
    829 	}
    830 
    831 	audio_attach_mi(&snapper_hw_if, sc, sc->sc_dev);
    832 
    833 	/* ki2c_setmode(sc->sc_i2c, I2C_STDSUBMODE); */
    834 	snapper_init(sc, sc->sc_node);
    835 }
    836 
    837 int
    838 snapper_intr(void *v)
    839 {
    840 	struct snapper_softc *sc;
    841 	struct dbdma_command *cmd;
    842 	int count;
    843 	int status;
    844 
    845 	sc = v;
    846 	cmd = sc->sc_odmacmd;
    847 	count = sc->sc_opages;
    848 	/* Fill used buffer(s). */
    849 	while (count-- > 0) {
    850 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    851 			status = in16rb(&cmd->d_status);
    852 			cmd->d_status = 0;
    853 			if (status)	/* status == 0x8400 */
    854 				if (sc->sc_ointr)
    855 					(*sc->sc_ointr)(sc->sc_oarg);
    856 		}
    857 		cmd++;
    858 	}
    859 
    860 	cmd = sc->sc_idmacmd;
    861 	count = sc->sc_ipages;
    862 	while (count-- > 0) {
    863 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    864 			status = in16rb(&cmd->d_status);
    865 			cmd->d_status = 0;
    866 			if (status)	/* status == 0x8400 */
    867 				if (sc->sc_iintr)
    868 					(*sc->sc_iintr)(sc->sc_iarg);
    869 		}
    870 		cmd++;
    871 	}
    872 
    873 
    874 	return 1;
    875 }
    876 
    877 
    878 int
    879 snapper_query_encoding(void *h, struct audio_encoding *ae)
    880 {
    881 
    882 	struct snapper_softc *sc = h;
    883 
    884 	return auconv_query_encoding(sc->sc_encodings, ae);
    885 }
    886 
    887 int
    888 snapper_set_params(void *h, int setmode, int usemode,
    889 		   audio_params_t *play, audio_params_t *rec,
    890 		   stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    891 {
    892 	struct snapper_softc *sc;
    893 	audio_params_t *p;
    894 	stream_filter_list_t *fil = NULL; /* XXX gcc */
    895 	int mode;
    896 
    897 	sc = h;
    898 	p = NULL;
    899 
    900 	/*
    901 	 * This device only has one clock, so make the sample rates match.
    902 	 */
    903 	if (play->sample_rate != rec->sample_rate &&
    904 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    905 		if (setmode == AUMODE_PLAY) {
    906 			rec->sample_rate = play->sample_rate;
    907 			setmode |= AUMODE_RECORD;
    908 		} else if (setmode == AUMODE_RECORD) {
    909 			play->sample_rate = rec->sample_rate;
    910 			setmode |= AUMODE_PLAY;
    911 		} else
    912 			return EINVAL;
    913 	}
    914 
    915 	for (mode = AUMODE_RECORD; mode != -1;
    916 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    917 		if ((setmode & mode) == 0)
    918 			continue;
    919 
    920 		p = mode == AUMODE_PLAY ? play : rec;
    921 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    922 		if (sc->sc_mode == SNAPPER_IS_TAS3001) {
    923 			if (auconv_set_converter(tumbler_formats,
    924 				    TUMBLER_NFORMATS, mode, p, true, fil) < 0) {
    925 				DPRINTF("snapper_set_params: "
    926 					"auconv_set_converter failed\n");
    927 				return EINVAL;
    928 			}
    929 		} else {	/* TAS 3004 */
    930 			if (auconv_set_converter(snapper_formats,
    931 				    SNAPPER_NFORMATS, mode, p, true, fil) < 0) {
    932 				DPRINTF("snapper_set_params: "
    933 					"auconv_set_converter failed\n");
    934 				return EINVAL;
    935 			}
    936 		}
    937 
    938 		if (fil->req_size > 0)
    939 			p = &fil->filters[0].param;
    940 		if (p->precision == 16) {
    941 			if (sc->sc_mode == SNAPPER_SWVOL)
    942 				fil->prepend(fil, snapper_volume, p);
    943 			else if (sc->sc_mode == 0 && p->channels == 2) {
    944 				/*
    945 				 * Fix phase problems on TAS3004.
    946 				 * This filter must go last on the chain,
    947 				 * so prepend it, not append it.
    948 				 */
    949 				fil->prepend(fil, snapper_fixphase, p);
    950 			}
    951 		}
    952 	}
    953 
    954 	/* Set the speed. p points HW encoding. */
    955 	if (p) {
    956 		sc->sc_rate = p->sample_rate;
    957 		sc->sc_bitspersample = p->precision;
    958 	}
    959 	return 0;
    960 }
    961 
    962 int
    963 snapper_round_blocksize(void *h, int size, int mode,
    964 			const audio_params_t *param)
    965 {
    966 
    967 	if (size < NBPG)
    968 		size = NBPG;
    969 	return size & ~PGOFSET;
    970 }
    971 
    972 int
    973 snapper_halt_output(void *h)
    974 {
    975 	struct snapper_softc *sc;
    976 
    977 	sc = h;
    978 	dbdma_stop(sc->sc_odma);
    979 	dbdma_reset(sc->sc_odma);
    980 	sc->sc_ointr = NULL;
    981 	return 0;
    982 }
    983 
    984 int
    985 snapper_halt_input(void *h)
    986 {
    987 	struct snapper_softc *sc;
    988 
    989 	sc = h;
    990 	dbdma_stop(sc->sc_idma);
    991 	dbdma_reset(sc->sc_idma);
    992 	sc->sc_iintr = NULL;
    993 	return 0;
    994 }
    995 
    996 int
    997 snapper_getdev(void *h, struct audio_device *retp)
    998 {
    999 
   1000 	*retp = snapper_device;
   1001 	return 0;
   1002 }
   1003 
   1004 enum {
   1005 	SNAPPER_MONITOR_CLASS,
   1006 	SNAPPER_OUTPUT_CLASS,
   1007 	SNAPPER_RECORD_CLASS,
   1008 	SNAPPER_OUTPUT_SELECT,
   1009 	SNAPPER_VOL_OUTPUT,
   1010 	SNAPPER_DIGI1,
   1011 	SNAPPER_DIGI2,
   1012 	SNAPPER_VOL_INPUT,
   1013 	SNAPPER_TREBLE,
   1014 	SNAPPER_BASS,
   1015 	/* From this point, unsupported by the TAS 3001 */
   1016 	SNAPPER_ANALOG,
   1017 	SNAPPER_INPUT_SELECT,
   1018 	SNAPPER_ENUM_LAST
   1019 };
   1020 
   1021 int
   1022 snapper_set_port(void *h, mixer_ctrl_t *mc)
   1023 {
   1024 	struct snapper_softc *sc;
   1025 	int l, r;
   1026 	u_char data;
   1027 
   1028 	DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
   1029 	sc = h;
   1030 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1031 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1032 
   1033 	switch (mc->dev) {
   1034 	case SNAPPER_OUTPUT_SELECT:
   1035 		/* No change necessary? */
   1036 		if (mc->un.mask == sc->sc_output_mask)
   1037 			return 0;
   1038 
   1039 		snapper_mute_speaker(sc, 1);
   1040 		snapper_mute_headphone(sc, 1);
   1041 		if (mc->un.mask & 1 << 0)
   1042 			snapper_mute_speaker(sc, 0);
   1043 		if (mc->un.mask & 1 << 1)
   1044 			snapper_mute_headphone(sc, 0);
   1045 
   1046 		sc->sc_output_mask = mc->un.mask;
   1047 		return 0;
   1048 
   1049 	case SNAPPER_VOL_OUTPUT:
   1050 		snapper_set_volume(sc, l, r);
   1051 		return 0;
   1052 
   1053 	case SNAPPER_INPUT_SELECT:
   1054 		if (sc->sc_mode != 0)
   1055 			return ENXIO;
   1056 
   1057 		/* no change necessary? */
   1058 		if (mc->un.mask == sc->sc_record_source)
   1059 			return 0;
   1060 		switch (mc->un.mask) {
   1061 		case 1 << 0: /* microphone */
   1062 			/* Select right channel of B input */
   1063 			data = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B;
   1064 			tas3004_write(sc, DEQ_ACR, &data);
   1065 			break;
   1066 		case 1 << 1: /* line in */
   1067 			/* Select both channels of A input */
   1068 			data = 0;
   1069 			tas3004_write(sc, DEQ_ACR, &data);
   1070 			break;
   1071 		default: /* invalid argument */
   1072 			return EINVAL;
   1073 		}
   1074 		sc->sc_record_source = mc->un.mask;
   1075 		return 0;
   1076 
   1077 	case SNAPPER_VOL_INPUT:
   1078 		/* XXX TO BE DONE */
   1079 		return 0;
   1080 
   1081 	case SNAPPER_BASS:
   1082 		if (sc->sc_mode == SNAPPER_SWVOL)
   1083 			return ENXIO;
   1084 		snapper_set_bass(sc, l);
   1085 		return 0;
   1086 	case SNAPPER_TREBLE:
   1087 		if (sc->sc_mode == SNAPPER_SWVOL)
   1088 			return ENXIO;
   1089 		snapper_set_treble(sc, l);
   1090 		return 0;
   1091 	case SNAPPER_DIGI1:
   1092 		if (sc->sc_mode == SNAPPER_SWVOL)
   1093 			return ENXIO;
   1094 
   1095 		sc->mixer[0] = l;
   1096 		sc->mixer[3] = r;
   1097 		snapper_write_mixers(sc);
   1098 		return 0;
   1099 	case SNAPPER_DIGI2:
   1100 		if (sc->sc_mode == SNAPPER_SWVOL)
   1101 			return ENXIO;
   1102 
   1103 		if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1104 			sc->mixer[3] = l;
   1105 		else {
   1106 			sc->mixer[1] = l;
   1107 			sc->mixer[4] = r;
   1108 		}
   1109 		snapper_write_mixers(sc);
   1110 		return 0;
   1111 	case SNAPPER_ANALOG:
   1112 		if (sc->sc_mode != 0)
   1113 			return ENXIO;
   1114 
   1115 		sc->mixer[2] = l;
   1116 		sc->mixer[5] = r;
   1117 		snapper_write_mixers(sc);
   1118 		return 0;
   1119 	}
   1120 	return ENXIO;
   1121 }
   1122 
   1123 int
   1124 snapper_get_port(void *h, mixer_ctrl_t *mc)
   1125 {
   1126 	struct snapper_softc *sc;
   1127 
   1128 	DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
   1129 	sc = h;
   1130 	switch (mc->dev) {
   1131 	case SNAPPER_OUTPUT_SELECT:
   1132 		mc->un.mask = sc->sc_output_mask;
   1133 		return 0;
   1134 
   1135 	case SNAPPER_VOL_OUTPUT:
   1136 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
   1137 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
   1138 		return 0;
   1139 
   1140 	case SNAPPER_INPUT_SELECT:
   1141 		if (sc->sc_mode != 0)
   1142 			return ENXIO;
   1143 
   1144 		mc->un.mask = sc->sc_record_source;
   1145 		return 0;
   1146 
   1147 	case SNAPPER_VOL_INPUT:
   1148 		/* XXX TO BE DONE */
   1149 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
   1150 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
   1151 		return 0;
   1152 
   1153 	case SNAPPER_TREBLE:
   1154 		if (sc->sc_mode == SNAPPER_SWVOL)
   1155 			return ENXIO;
   1156 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
   1157 		return 0;
   1158 	case SNAPPER_BASS:
   1159 		if (sc->sc_mode == SNAPPER_SWVOL)
   1160 			return ENXIO;
   1161 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
   1162 		return 0;
   1163 
   1164 	case SNAPPER_DIGI1:
   1165 		if (sc->sc_mode == SNAPPER_SWVOL)
   1166 			return ENXIO;
   1167 
   1168 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0];
   1169 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3];
   1170 		return 0;
   1171 	case SNAPPER_DIGI2:
   1172 		if (sc->sc_mode == SNAPPER_SWVOL)
   1173 			return ENXIO;
   1174 
   1175 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1];
   1176 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4];
   1177 		return 0;
   1178 	case SNAPPER_ANALOG:
   1179 		if (sc->sc_mode != 0)
   1180 			return ENXIO;
   1181 
   1182 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2];
   1183 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5];
   1184 		return 0;
   1185 	default:
   1186 		return ENXIO;
   1187 	}
   1188 
   1189 	return 0;
   1190 }
   1191 
   1192 int
   1193 snapper_query_devinfo(void *h, mixer_devinfo_t *dip)
   1194 {
   1195 	struct snapper_softc *sc = h;
   1196 
   1197 	switch (dip->index) {
   1198 
   1199 	case SNAPPER_OUTPUT_SELECT:
   1200 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1201 		strcpy(dip->label.name, AudioNoutput);
   1202 		dip->type = AUDIO_MIXER_SET;
   1203 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1204 		dip->un.s.num_mem = 2;
   1205 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
   1206 		dip->un.s.member[0].mask = 1 << 0;
   1207 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
   1208 		dip->un.s.member[1].mask = 1 << 1;
   1209 		return 0;
   1210 
   1211 	case SNAPPER_VOL_OUTPUT:
   1212 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1213 		strcpy(dip->label.name, AudioNmaster);
   1214 		dip->type = AUDIO_MIXER_VALUE;
   1215 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1216 		dip->un.v.num_channels = 2;
   1217 		strcpy(dip->un.v.units.name, AudioNvolume);
   1218 		return 0;
   1219 
   1220 	case SNAPPER_INPUT_SELECT:
   1221 		if (sc->sc_mode != 0)
   1222 			return ENXIO;
   1223 
   1224 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1225 		strcpy(dip->label.name, AudioNsource);
   1226 		dip->type = AUDIO_MIXER_SET;
   1227 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1228 		dip->un.s.num_mem = 2;
   1229 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1230 		dip->un.s.member[0].mask = 1 << 0;
   1231 		strcpy(dip->un.s.member[1].label.name, AudioNline);
   1232 		dip->un.s.member[1].mask = 1 << 1;
   1233 		return 0;
   1234 
   1235 	case SNAPPER_VOL_INPUT:
   1236 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1237 		strcpy(dip->label.name, AudioNrecord);
   1238 		dip->type = AUDIO_MIXER_VALUE;
   1239 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1240 		dip->un.v.num_channels = 2;
   1241 		strcpy(dip->un.v.units.name, AudioNvolume);
   1242 		return 0;
   1243 
   1244 	case SNAPPER_MONITOR_CLASS:
   1245 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1246 		strcpy(dip->label.name, AudioCmonitor);
   1247 		dip->type = AUDIO_MIXER_CLASS;
   1248 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1249 		return 0;
   1250 
   1251 	case SNAPPER_OUTPUT_CLASS:
   1252 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1253 		strcpy(dip->label.name, AudioCoutputs);
   1254 		dip->type = AUDIO_MIXER_CLASS;
   1255 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1256 		return 0;
   1257 
   1258 	case SNAPPER_RECORD_CLASS:
   1259 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1260 		strcpy(dip->label.name, AudioCrecord);
   1261 		dip->type = AUDIO_MIXER_CLASS;
   1262 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1263 		return 0;
   1264 
   1265 	case SNAPPER_TREBLE:
   1266 		if (sc->sc_mode == SNAPPER_SWVOL)
   1267 			return ENXIO;
   1268 
   1269 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1270 		strcpy(dip->label.name, AudioNtreble);
   1271 		dip->type = AUDIO_MIXER_VALUE;
   1272 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1273 		dip->un.v.num_channels = 1;
   1274 		return 0;
   1275 
   1276 	case SNAPPER_BASS:
   1277 		if (sc->sc_mode == SNAPPER_SWVOL)
   1278 			return ENXIO;
   1279 
   1280 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1281 		strcpy(dip->label.name, AudioNbass);
   1282 		dip->type = AUDIO_MIXER_VALUE;
   1283 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1284 		dip->un.v.num_channels = 1;
   1285 		return 0;
   1286 
   1287 	case SNAPPER_DIGI1:
   1288 		if (sc->sc_mode == SNAPPER_SWVOL)
   1289 			return ENXIO;
   1290 
   1291 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1292 		strcpy(dip->label.name, AudioNdac);
   1293 		dip->type = AUDIO_MIXER_VALUE;
   1294 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1295 		dip->un.v.num_channels =
   1296 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1297 		return 0;
   1298 	case SNAPPER_DIGI2:
   1299 		if (sc->sc_mode == SNAPPER_SWVOL)
   1300 			return ENXIO;
   1301 
   1302 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1303 		strcpy(dip->label.name, AudioNline);
   1304 		dip->type = AUDIO_MIXER_VALUE;
   1305 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1306 		dip->un.v.num_channels =
   1307 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1308 		return 0;
   1309 	case SNAPPER_ANALOG:
   1310 		if (sc->sc_mode != 0)
   1311 			return ENXIO;
   1312 
   1313 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1314 		strcpy(dip->label.name, AudioNmicrophone);
   1315 		dip->type = AUDIO_MIXER_VALUE;
   1316 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1317 		dip->un.v.num_channels = 2;
   1318 		return 0;
   1319 	}
   1320 
   1321 	return ENXIO;
   1322 }
   1323 
   1324 size_t
   1325 snapper_round_buffersize(void *h, int dir, size_t size)
   1326 {
   1327 
   1328 	if (size > 65536)
   1329 		size = 65536;
   1330 	return size;
   1331 }
   1332 
   1333 paddr_t
   1334 snapper_mappage(void *h, void *mem, off_t off, int prot)
   1335 {
   1336 
   1337 	if (off < 0)
   1338 		return -1;
   1339 	return -1;	/* XXX */
   1340 }
   1341 
   1342 int
   1343 snapper_get_props(void *h)
   1344 {
   1345 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
   1346 }
   1347 
   1348 int
   1349 snapper_trigger_output(void *h, void *start, void *end, int bsize,
   1350 		       void (*intr)(void *), void *arg,
   1351 		       const audio_params_t *param)
   1352 {
   1353 	struct snapper_softc *sc;
   1354 	struct dbdma_command *cmd;
   1355 	vaddr_t va;
   1356 	int i, len, intmode;
   1357 	int res;
   1358 
   1359 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1360 	sc = h;
   1361 
   1362 	if ((res = snapper_set_rate(sc)) != 0)
   1363 		return res;
   1364 
   1365 	cmd = sc->sc_odmacmd;
   1366 	sc->sc_ointr = intr;
   1367 	sc->sc_oarg = arg;
   1368 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
   1369 
   1370 #ifdef DIAGNOSTIC
   1371 	if (sc->sc_opages > SNAPPER_MAXPAGES)
   1372 		panic("snapper_trigger_output");
   1373 #endif
   1374 
   1375 	va = (vaddr_t)start;
   1376 	len = 0;
   1377 	for (i = sc->sc_opages; i > 0; i--) {
   1378 		len += NBPG;
   1379 		if (len < bsize)
   1380 			intmode = 0;
   1381 		else {
   1382 			len = 0;
   1383 			intmode = DBDMA_INT_ALWAYS;
   1384 		}
   1385 
   1386 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
   1387 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1388 		cmd++;
   1389 		va += NBPG;
   1390 	}
   1391 
   1392 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1393 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1394 	    DBDMA_BRANCH_ALWAYS);
   1395 
   1396 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1397 
   1398 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1399 
   1400 	return 0;
   1401 }
   1402 
   1403 int
   1404 snapper_trigger_input(void *h, void *start, void *end, int bsize,
   1405 		      void (*intr)(void *), void *arg,
   1406 		      const audio_params_t *param)
   1407 {
   1408 	struct snapper_softc *sc;
   1409 	struct dbdma_command *cmd;
   1410 	vaddr_t va;
   1411 	int i, len, intmode;
   1412 	int res;
   1413 
   1414 	DPRINTF("trigger_input %p %p 0x%x\n", start, end, bsize);
   1415 	sc = h;
   1416 
   1417 	if ((res = snapper_set_rate(sc)) != 0)
   1418 		return res;
   1419 
   1420 	cmd = sc->sc_idmacmd;
   1421 	sc->sc_iintr = intr;
   1422 	sc->sc_iarg = arg;
   1423 	sc->sc_ipages = ((char *)end - (char *)start) / NBPG;
   1424 
   1425 #ifdef DIAGNOSTIC
   1426 	if (sc->sc_ipages > SNAPPER_MAXPAGES)
   1427 		panic("snapper_trigger_input");
   1428 #endif
   1429 
   1430 	va = (vaddr_t)start;
   1431 	len = 0;
   1432 	for (i = sc->sc_ipages; i > 0; i--) {
   1433 		len += NBPG;
   1434 		if (len < bsize)
   1435 			intmode = 0;
   1436 		else {
   1437 			len = 0;
   1438 			intmode = DBDMA_INT_ALWAYS;
   1439 		}
   1440 
   1441 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_MORE, 0, NBPG, vtophys(va),
   1442 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1443 		cmd++;
   1444 		va += NBPG;
   1445 	}
   1446 
   1447 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1448 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1449 	    DBDMA_BRANCH_ALWAYS);
   1450 
   1451 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_idmacmd));
   1452 
   1453 	dbdma_start(sc->sc_idma, sc->sc_idmacmd);
   1454 
   1455 	return 0;
   1456 }
   1457 
   1458 void
   1459 snapper_set_volume(struct snapper_softc *sc, u_int left, u_int right)
   1460 {
   1461 	u_char regs[6];
   1462 	int l, r;
   1463 
   1464 	left &= 0xFF;
   1465 	right &= 0xFF;
   1466 
   1467 	if (sc->sc_mode == SNAPPER_SWVOL) {
   1468 		snapper_vol_l = left;
   1469 		snapper_vol_r = right;
   1470 	} else {
   1471 		/*
   1472 		 * for some insane reason the gain table for master volume and the
   1473 		 * mixer channels is almost identical - just shifted by 4 bits
   1474 		 * so we use the mixer_gain table and bit-twiddle it...
   1475 		 */
   1476 		l = 177 - (left * 178 / 256);
   1477 		regs[0] =  (snapper_mixer_gain[l][0] >> 4);
   1478 		regs[1] = ((snapper_mixer_gain[l][0] & 0x0f) << 4) |
   1479 			   (snapper_mixer_gain[l][1] >> 4);
   1480 		regs[2] = ((snapper_mixer_gain[l][1] & 0x0f) << 4) |
   1481 			   (snapper_mixer_gain[l][2] >> 4);
   1482 
   1483 		r = 177 - (right * 178 / 256);
   1484 		regs[3] =  (snapper_mixer_gain[r][0] >> 4);
   1485 		regs[4] = ((snapper_mixer_gain[r][0] & 0x0f) << 4) |
   1486 			   (snapper_mixer_gain[r][1] >> 4);
   1487 		regs[5] = ((snapper_mixer_gain[r][1] & 0x0f) << 4) |
   1488 			   (snapper_mixer_gain[r][2] >> 4);
   1489 
   1490 		tas3004_write(sc, DEQ_VOLUME, regs);
   1491 
   1492 		DPRINTF("%d %02x %02x %02x : %d %02x %02x %02x\n", l, regs[0],
   1493 		    regs[1], regs[2], r, regs[3], regs[4], regs[5]);
   1494 	}
   1495 
   1496 	sc->sc_vol_l = left;
   1497 	sc->sc_vol_r = right;
   1498 }
   1499 
   1500 static void
   1501 snapper_set_basstreble(struct snapper_softc *sc, u_int val, u_int mode)
   1502 {
   1503 	int i = val & 0xFF;
   1504 	uint8_t reg;
   1505 
   1506 	/*
   1507 	 * Make 128 match the 0 dB point
   1508 	 */
   1509 	i = (i - (128 - (SNAPPER_BASSTAB_0DB << 2))) >> 2;
   1510 	if (i < 0)
   1511 		i = 0;
   1512 	else if (i >= sizeof(snapper_basstab))
   1513 		i = sizeof(snapper_basstab) - 1;
   1514 	reg = snapper_basstab[i];
   1515 
   1516 	if (sc->sc_mode == SNAPPER_IS_TAS3001 &&
   1517 	    mode == DEQ_BASS) {
   1518 	    /*
   1519 	     * XXX -- The TAS3001 bass table is different
   1520 	     *        than the other tables.
   1521 	     */
   1522 	    reg = (reg >> 1) + 5; // map 0x72 -> 0x3E (0 dB)
   1523 	}
   1524 
   1525 	tas3004_write(sc, mode, &reg);
   1526 }
   1527 
   1528 void
   1529 snapper_set_treble(struct snapper_softc *sc, u_int val)
   1530 {
   1531 	if (sc->sc_treble != (u_char)val) {
   1532 		sc->sc_treble = val;
   1533 		snapper_set_basstreble(sc, val, DEQ_TREBLE);
   1534 	}
   1535 }
   1536 
   1537 void
   1538 snapper_set_bass(struct snapper_softc *sc, u_int val)
   1539 {
   1540 	if (sc->sc_bass != (u_char)val) {
   1541 		sc->sc_bass = val;
   1542 		snapper_set_basstreble(sc, val, DEQ_BASS);
   1543 	}
   1544 }
   1545 
   1546 
   1547 /*
   1548  * In the mixer gain setting, make 128 correspond to
   1549  * the 0dB value from the table.
   1550  * Note that the table values are complemented.
   1551  */
   1552 #define SNAPPER_MIXER_GAIN_SIZE	(sizeof(snapper_mixer_gain) / \
   1553                                	 sizeof(snapper_mixer_gain[0]))
   1554 #define NORMALIZE(i)	((~(i) & 0xff) - ((~128 & 0xff) - SNAPPER_MIXER_GAIN_0DB))
   1555 #define ADJUST(v, i)	do { \
   1556                 		(v) = NORMALIZE(i);\
   1557 				if ((v) < 0) \
   1558 					(v) = 0; \
   1559 				else if ((v) >= SNAPPER_MIXER_GAIN_SIZE) \
   1560 					(v) = SNAPPER_MIXER_GAIN_SIZE - 1; \
   1561 				\
   1562 			} while (0)
   1563 void
   1564 snapper_write_mixers(struct snapper_softc *sc)
   1565 {
   1566 	uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
   1567 	int i;
   1568 
   1569 	/* Left channel of SDIN1 */
   1570 	ADJUST(i, sc->mixer[0]);
   1571 	regs[0] = snapper_mixer_gain[i][0];
   1572 	regs[1] = snapper_mixer_gain[i][1];
   1573 	regs[2] = snapper_mixer_gain[i][2];
   1574 
   1575 	/* Left channel of SDIN2 */
   1576 	ADJUST(i, sc->mixer[1]);
   1577 	regs[3] = snapper_mixer_gain[i][0];
   1578 	regs[4] = snapper_mixer_gain[i][1];
   1579 	regs[5] = snapper_mixer_gain[i][2];
   1580 
   1581 	/* Left channel of analog input */
   1582 	ADJUST(i, sc->mixer[2]);
   1583 	regs[6] = snapper_mixer_gain[i][0];
   1584 	regs[7] = snapper_mixer_gain[i][1];
   1585 	regs[8] = snapper_mixer_gain[i][2];
   1586 
   1587 	tas3004_write(sc, DEQ_MIXER_L, regs);
   1588 
   1589 	/* Right channel of SDIN1 */
   1590 	ADJUST(i, sc->mixer[3]);
   1591 	regs[0] = snapper_mixer_gain[i][0];
   1592 	regs[1] = snapper_mixer_gain[i][1];
   1593 	regs[2] = snapper_mixer_gain[i][2];
   1594 
   1595 	/* Right channel of SDIN2 */
   1596 	ADJUST(i, sc->mixer[4]);
   1597 	regs[3] = snapper_mixer_gain[i][0];
   1598 	regs[4] = snapper_mixer_gain[i][1];
   1599 	regs[5] = snapper_mixer_gain[i][2];
   1600 
   1601 	/* Right channel of analog input */
   1602 	ADJUST(i, sc->mixer[5]);
   1603 	regs[6] = snapper_mixer_gain[i][0];
   1604 	regs[7] = snapper_mixer_gain[i][1];
   1605 	regs[8] = snapper_mixer_gain[i][2];
   1606 
   1607 	tas3004_write(sc, DEQ_MIXER_R, regs);
   1608 }
   1609 
   1610 #define CLKSRC_49MHz	0x80000000	/* Use 49152000Hz Osc. */
   1611 #define CLKSRC_45MHz	0x40000000	/* Use 45158400Hz Osc. */
   1612 #define CLKSRC_18MHz	0x00000000	/* Use 18432000Hz Osc. */
   1613 #define MCLK_DIV	0x1f000000	/* MCLK = SRC / DIV */
   1614 #define  MCLK_DIV1	0x14000000	/*  MCLK = SRC */
   1615 #define  MCLK_DIV3	0x13000000	/*  MCLK = SRC / 3 */
   1616 #define  MCLK_DIV5	0x12000000	/*  MCLK = SRC / 5 */
   1617 #define SCLK_DIV	0x00f00000	/* SCLK = MCLK / DIV */
   1618 #define  SCLK_DIV1	0x00800000
   1619 #define  SCLK_DIV3	0x00900000
   1620 #define SCLK_MASTER	0x00080000	/* Master mode */
   1621 #define SCLK_SLAVE	0x00000000	/* Slave mode */
   1622 #define SERIAL_FORMAT	0x00070000
   1623 #define  SERIAL_SONY	0x00000000
   1624 #define  SERIAL_64x	0x00010000
   1625 #define  SERIAL_32x	0x00020000
   1626 #define  SERIAL_DAV	0x00040000
   1627 #define  SERIAL_SILICON	0x00050000
   1628 
   1629 /*
   1630  * rate = fs = LRCLK
   1631  * SCLK = 64*LRCLK (I2S)
   1632  * MCLK = 256fs (typ. -- changeable)
   1633  *
   1634  * MCLK = clksrc / mdiv
   1635  * SCLK = MCLK / sdiv
   1636  * rate = SCLK / 64    ( = LRCLK = fs)
   1637  */
   1638 
   1639 int
   1640 snapper_set_rate(struct snapper_softc *sc)
   1641 {
   1642 	u_int reg = 0, x;
   1643 	u_int rate = sc->sc_rate;
   1644 	uint32_t wordsize, ows;
   1645 	int MCLK;
   1646 	int clksrc, mdiv, sdiv;
   1647 	int mclk_fs;
   1648 	int timo;
   1649 	uint8_t mcr1;
   1650 
   1651 	switch (rate) {
   1652 	case 44100:
   1653 		clksrc = 45158400;		/* 45MHz */
   1654 		reg = CLKSRC_45MHz;
   1655 		mclk_fs = 256;
   1656 		break;
   1657 
   1658 	case 32000:
   1659 	case 48000:
   1660 	case 96000:
   1661 		clksrc = 49152000;		/* 49MHz */
   1662 		reg = CLKSRC_49MHz;
   1663 		mclk_fs = 256;
   1664 		break;
   1665 
   1666 	default:
   1667 		DPRINTF("snapper_set_rate: invalid rate %u\n", rate);
   1668 		return EINVAL;
   1669 	}
   1670 
   1671 	MCLK = rate * mclk_fs;
   1672 	mdiv = clksrc / MCLK;			/* 4 */
   1673 	sdiv = mclk_fs / 64;			/* 4 */
   1674 
   1675 	switch (mdiv) {
   1676 	case 1:
   1677 		reg |= MCLK_DIV1;
   1678 		break;
   1679 	case 3:
   1680 		reg |= MCLK_DIV3;
   1681 		break;
   1682 	case 5:
   1683 		reg |= MCLK_DIV5;
   1684 		break;
   1685 	default:
   1686 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
   1687 		break;
   1688 	}
   1689 
   1690 	switch (sdiv) {
   1691 	case 1:
   1692 		reg |= SCLK_DIV1;
   1693 		break;
   1694 	case 3:
   1695 		reg |= SCLK_DIV3;
   1696 		break;
   1697 	default:
   1698 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
   1699 		break;
   1700 	}
   1701 
   1702 	reg |= SCLK_MASTER;	/* XXX master mode */
   1703 
   1704 	reg |= SERIAL_64x;
   1705 
   1706 	/* stereo input and output */
   1707 
   1708 	DPRINTF("precision: %d\n", sc->sc_bitspersample);
   1709 	switch(sc->sc_bitspersample) {
   1710 		case 16:
   1711 			wordsize = 0x02000200;
   1712 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16;
   1713 			break;
   1714 		case 24:
   1715 			wordsize = 0x03000300;
   1716 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_24;
   1717 			break;
   1718 		default:
   1719 			printf("%s: unsupported sample size %d\n",
   1720 			    device_xname(sc->sc_dev), sc->sc_bitspersample);
   1721 			return EINVAL;
   1722 	}
   1723 
   1724 	if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1725 		mcr1 |= DEQ_MCR1_ISM_I2S;
   1726 
   1727 	ows = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE);
   1728 
   1729 	DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
   1730 	    ows, wordsize);
   1731 	if (ows != wordsize) {
   1732 		bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE,
   1733 		    wordsize);
   1734 		if (sc->sc_mode != SNAPPER_SWVOL)
   1735 			tas3004_write(sc, DEQ_MCR1, &mcr1);
   1736 	}
   1737 
   1738 	x = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT);
   1739 	if (x == reg)
   1740 		return 0;        /* No change; do nothing. */
   1741 
   1742 	DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
   1743 	    bus_space_read_4(sc->sc_tag, sc->sc_bsh, + I2S_FORMAT), reg);
   1744 
   1745 	/* Clear CLKSTOPPEND. */
   1746 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND);
   1747 
   1748 	x = in32rb(sc->sc_baseaddr + KEYLARGO_FCR1);                /* FCR */
   1749 	x &= ~I2S0CLKEN;                /* XXX I2S0 */
   1750 	out32rb(sc->sc_baseaddr + KEYLARGO_FCR1, x);
   1751 
   1752 	/* Wait until clock is stopped. */
   1753 	for (timo = 1000; timo > 0; timo--) {
   1754 		if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) &
   1755 		    I2S_INT_CLKSTOPPEND)
   1756 			goto done;
   1757 		delay(1);
   1758 	}
   1759 	DPRINTF("snapper_set_rate: timeout\n");
   1760 done:
   1761 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
   1762 
   1763 	x = in32rb(sc->sc_baseaddr + KEYLARGO_FCR1);
   1764 	x |= I2S0CLKEN;
   1765 	out32rb(sc->sc_baseaddr + KEYLARGO_FCR1, x);
   1766 
   1767 	return 0;
   1768 }
   1769 
   1770 const struct tas3004_reg tas3004_initdata = {
   1771 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16 },	/* MCR1 */
   1772 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
   1773 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
   1774 	{ 0x72 },						/* TREBLE */
   1775 	{ 0x72 },						/* BASS */
   1776 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
   1777 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
   1778 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1779 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1780 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1781 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1782 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1783 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1784 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1785 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1786 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1787 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1788 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1789 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1790 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1791 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1792 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1793 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1794 	{ 0, 0, 0 },						/* LLB_GAIN */
   1795 	{ 0, 0, 0 },						/* RLB_GAIN */
   1796 	{ DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B },		/* ACR - right channel of input B is the microphone */
   1797 	{ 2 }							/* MCR2 - AllPass mode since we don't use the equalizer anyway */
   1798 };
   1799 
   1800 const char tas3004_regsize[] = {
   1801 	0,					/* 0x00 */
   1802 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
   1803 	sizeof tas3004_initdata.DRC,		/* 0x02 */
   1804 	0,					/* 0x03 */
   1805 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
   1806 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
   1807 	sizeof tas3004_initdata.BASS,		/* 0x06 */
   1808 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
   1809 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
   1810 	0,					/* 0x09 */
   1811 	sizeof tas3004_initdata.LB0,		/* 0x0a */
   1812 	sizeof tas3004_initdata.LB1,		/* 0x0b */
   1813 	sizeof tas3004_initdata.LB2,		/* 0x0c */
   1814 	sizeof tas3004_initdata.LB3,		/* 0x0d */
   1815 	sizeof tas3004_initdata.LB4,		/* 0x0e */
   1816 	sizeof tas3004_initdata.LB5,		/* 0x0f */
   1817 	sizeof tas3004_initdata.LB6,		/* 0x10 */
   1818 	0,					/* 0x11 */
   1819 	0,					/* 0x12 */
   1820 	sizeof tas3004_initdata.RB0,		/* 0x13 */
   1821 	sizeof tas3004_initdata.RB1,		/* 0x14 */
   1822 	sizeof tas3004_initdata.RB2,		/* 0x15 */
   1823 	sizeof tas3004_initdata.RB3,		/* 0x16 */
   1824 	sizeof tas3004_initdata.RB4,		/* 0x17 */
   1825 	sizeof tas3004_initdata.RB5,		/* 0x18 */
   1826 	sizeof tas3004_initdata.RB6,		/* 0x19 */
   1827 	0,0,0,0, 0,0,
   1828 	0,					/* 0x20 */
   1829 	sizeof tas3004_initdata.LLB,		/* 0x21 */
   1830 	sizeof tas3004_initdata.RLB,		/* 0x22 */
   1831 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
   1832 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
   1833 	0,0,0,0, 0,0,0,0, 0,0,0,
   1834 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   1835 	sizeof tas3004_initdata.ACR,		/* 0x40 */
   1836 	0,					/* 0x41 */
   1837 	0,					/* 0x42 */
   1838 	sizeof tas3004_initdata.MCR2		/* 0x43 */
   1839 };
   1840 
   1841 int
   1842 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
   1843 {
   1844 	int size;
   1845 	static char regblock[sizeof(struct tas3004_reg)+1];
   1846 
   1847 	if (sc->sc_i2c == NULL)
   1848 		return 0;
   1849 
   1850 	KASSERT(reg < sizeof tas3004_regsize);
   1851 	size = tas3004_regsize[reg];
   1852 	KASSERT(size > 0);
   1853 
   1854 	DPRINTF("reg: %x, %d %d\n", reg, size, ((const char*)data)[0]);
   1855 
   1856 	regblock[0] = reg;
   1857 	memcpy(&regblock[1], data, size);
   1858 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
   1859 		if (reg == DEQ_MIXER_L || reg == DEQ_MIXER_R)
   1860 			size = 3;
   1861 		else if (reg == DEQ_DRC || reg == DEQ_ACR ||
   1862 			 reg == DEQ_MCR2) {
   1863 			/* these registers are not available on TAS3001 */
   1864 			return 0;
   1865 		}
   1866 	}
   1867 	iic_acquire_bus(sc->sc_i2c, 0);
   1868 	iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, regblock, size + 1,
   1869 	    NULL, 0, 0);
   1870 	iic_release_bus(sc->sc_i2c, 0);
   1871 
   1872 	return 0;
   1873 }
   1874 
   1875 int
   1876 gpio_read(char *addr)
   1877 {
   1878 
   1879 	if (*addr & GPIO_DATA)
   1880 		return 1;
   1881 	return 0;
   1882 }
   1883 
   1884 void
   1885 gpio_write(char *addr, int val)
   1886 {
   1887 	u_int data;
   1888 
   1889 	data = GPIO_DDR_OUTPUT;
   1890 	if (val)
   1891 		data |= GPIO_DATA;
   1892 	*addr = data;
   1893 	__asm volatile ("eieio");
   1894 }
   1895 
   1896 #define headphone_active 0	/* XXX OF */
   1897 #define amp_active 0		/* XXX OF */
   1898 
   1899 void
   1900 snapper_mute_speaker(struct snapper_softc *sc, int mute)
   1901 {
   1902 	u_int x;
   1903 
   1904 	DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
   1905 
   1906 	if (mute)
   1907 		x = amp_active;		/* mute */
   1908 	else
   1909 		x = !amp_active;	/* unmute */
   1910 	if (x != gpio_read(amp_mute))
   1911 		gpio_write(amp_mute, x);
   1912 
   1913 	DPRINTF("%d\n", gpio_read(amp_mute));
   1914 }
   1915 
   1916 void
   1917 snapper_mute_headphone(struct snapper_softc *sc, int mute)
   1918 {
   1919 	u_int x;
   1920 
   1921 	DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
   1922 
   1923 	if (mute)
   1924 		x = headphone_active;	/* mute */
   1925 	else
   1926 		x = !headphone_active;	/* unmute */
   1927 	if (x != gpio_read(headphone_mute))
   1928 		gpio_write(headphone_mute, x);
   1929 
   1930 	DPRINTF("%d\n", gpio_read(headphone_mute));
   1931 }
   1932 
   1933 int
   1934 snapper_cint(void *v)
   1935 {
   1936 	struct snapper_softc *sc;
   1937 	u_int sense;
   1938 
   1939 	sc = v;
   1940 	sense = *headphone_detect;
   1941 	DPRINTF("headphone detect = 0x%x\n", sense);
   1942 
   1943 	if (((sense & 0x02) >> 1) == headphone_detect_active) {
   1944 		DPRINTF("headphone is inserted\n");
   1945 		snapper_mute_speaker(sc, 1);
   1946 		snapper_mute_headphone(sc, 0);
   1947 		sc->sc_output_mask = 1 << 1;
   1948 	} else {
   1949 		DPRINTF("headphone is NOT inserted\n");
   1950 		snapper_mute_speaker(sc, 0);
   1951 		snapper_mute_headphone(sc, 1);
   1952 		sc->sc_output_mask = 1 << 0;
   1953 	}
   1954 
   1955 	return 1;
   1956 }
   1957 
   1958 #define reset_active 0	/* XXX OF */
   1959 
   1960 #define DEQ_WRITE(sc, reg, addr) \
   1961 	if (tas3004_write(sc, reg, addr)) goto err
   1962 
   1963 int
   1964 tas3004_init(struct snapper_softc *sc)
   1965 {
   1966 
   1967 	/* No reset port.  Nothing to do. */
   1968 	if (audio_hw_reset == NULL)
   1969 		goto noreset;
   1970 
   1971 	/* Reset TAS3004. */
   1972 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1973 	delay(100000);				/* XXX Really needed? */
   1974 
   1975 	gpio_write(audio_hw_reset, reset_active);	/* Assert RESET */
   1976 	delay(1);
   1977 
   1978 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1979 	delay(10000);
   1980 
   1981 noreset:
   1982 	DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
   1983 	DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
   1984 	DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
   1985 	DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
   1986 	DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
   1987 	DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
   1988 	DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
   1989 	DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
   1990 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1991 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1992 	DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
   1993 	DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
   1994 	DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
   1995 	DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
   1996 	DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
   1997 	DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
   1998 	DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
   1999 	DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
   2000 	DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
   2001 	DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
   2002 	DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
   2003 	DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
   2004 	DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
   2005 	DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
   2006 	DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
   2007 	DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
   2008 	DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
   2009 
   2010 	return 0;
   2011 err:
   2012 	printf("tas3004_init: error\n");
   2013 	return -1;
   2014 }
   2015 
   2016 void
   2017 snapper_init(struct snapper_softc *sc, int node)
   2018 {
   2019 	int gpio;
   2020 	int headphone_detect_intr, headphone_detect_intrtype;
   2021 #ifdef SNAPPER_DEBUG
   2022 	char fcr[32];
   2023 
   2024 	bitmask_snprintf(in32rb(sc->sc_baseaddr + KEYLARGO_FCR1), FCR3C_BITMASK, 	    fcr, sizeof fcr);
   2025 	printf("FCR(0x3c) 0x%s\n", fcr);
   2026 #endif
   2027 	headphone_detect_intr = -1;
   2028 
   2029 	gpio = of_getnode_byname(OF_parent(node), "gpio");
   2030 	DPRINTF(" /gpio 0x%x\n", gpio);
   2031 	gpio = OF_child(gpio);
   2032 	while (gpio) {
   2033 		char name[64], audio_gpio[64];
   2034 		int intr[2];
   2035 		char *addr;
   2036 
   2037 		bzero(name, sizeof name);
   2038 		bzero(audio_gpio, sizeof audio_gpio);
   2039 		addr = 0;
   2040 		OF_getprop(gpio, "name", name, sizeof name);
   2041 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
   2042 		OF_getprop(gpio, "AAPL,address", &addr, sizeof addr);
   2043 		DPRINTF(" 0x%x %s %s\n", gpio, name, audio_gpio);
   2044 
   2045 		/* gpio5 */
   2046 		if (strcmp(audio_gpio, "headphone-mute") == 0)
   2047 			headphone_mute = addr;
   2048 		/* gpio6 */
   2049 		if (strcmp(audio_gpio, "amp-mute") == 0)
   2050 			amp_mute = addr;
   2051 		/* extint-gpio15 */
   2052 		if (strcmp(audio_gpio, "headphone-detect") == 0) {
   2053 			headphone_detect = addr;
   2054 			OF_getprop(gpio, "audio-gpio-active-state",
   2055 			    &headphone_detect_active, 4);
   2056 			OF_getprop(gpio, "interrupts", intr, 8);
   2057 			headphone_detect_intr = intr[0];
   2058 			headphone_detect_intrtype = intr[1];
   2059 		}
   2060 		/* gpio11 (keywest-11) */
   2061 		if (strcmp(audio_gpio, "audio-hw-reset") == 0)
   2062 			audio_hw_reset = addr;
   2063 		gpio = OF_peer(gpio);
   2064 	}
   2065 	DPRINTF(" headphone-mute %p\n", headphone_mute);
   2066 	DPRINTF(" amp-mute %p\n", amp_mute);
   2067 	DPRINTF(" headphone-detect %p\n", headphone_detect);
   2068 	DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
   2069 	DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
   2070 	DPRINTF(" audio-hw-reset %p\n", audio_hw_reset);
   2071 
   2072 	if (headphone_detect_intr != -1)
   2073 		intr_establish(headphone_detect_intr, IST_EDGE, IPL_AUDIO,
   2074 		    snapper_cint, sc);
   2075 
   2076 	sc->sc_rate = 44100;	/* default rate */
   2077 	sc->sc_bitspersample = 16;
   2078 
   2079 	/* Enable headphone interrupt? */
   2080 	*headphone_detect |= 0x80;
   2081 	__asm volatile ("eieio");
   2082 
   2083 	/* i2c_set_port(port); */
   2084 
   2085 	if (tas3004_init(sc))
   2086 		return;
   2087 
   2088 	/* Update headphone status. */
   2089 	snapper_cint(sc);
   2090 
   2091 	snapper_set_volume(sc, 128, 128);
   2092 	snapper_set_bass(sc, 128);
   2093 	snapper_set_treble(sc, 128);
   2094 
   2095 	/* Record source defaults to microphone.  This reflects the
   2096 	 * default value for the ACR (see tas3004_initdata).
   2097 	 */
   2098 	sc->sc_record_source = 1 << 0;
   2099 
   2100 	/* We mute the analog input for now */
   2101 	sc->mixer[0] = 128;
   2102 	sc->mixer[1] = 128;
   2103 	sc->mixer[2] = 0;
   2104 	sc->mixer[3] = 128;
   2105 	sc->mixer[4] = 128;
   2106 	sc->mixer[5] = 0;
   2107 	snapper_write_mixers(sc);
   2108 }
   2109 
   2110 static void
   2111 snapper_volume_up(device_t dev)
   2112 {
   2113 	struct snapper_softc *sc = device_private(dev);
   2114 
   2115 	snapper_set_volume(sc, min(0xff, sc->sc_vol_l + 8),
   2116 	     min(0xff, sc->sc_vol_r + 8));
   2117 }
   2118 
   2119 static void
   2120 snapper_volume_down(device_t dev)
   2121 {
   2122 	struct snapper_softc *sc = device_private(dev);
   2123 
   2124 	snapper_set_volume(sc, max(0, sc->sc_vol_l - 8),
   2125 	     max(0, sc->sc_vol_r - 8));
   2126 }
   2127