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