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