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