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