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