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