Home | History | Annotate | Line # | Download | only in dev
snapper.c revision 1.64
      1 /*	$NetBSD: snapper.c,v 1.64 2022/06/01 06:05:47 martin 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 /*-
      6  * Copyright (c) 2002, 2003 Tsubai Masanari.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Datasheet is available from
     33  * http://www.ti.com/sc/docs/products/analog/tas3004.html
     34  * http://www.ti.com/sc/docs/products/analog/tas3001.html
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: snapper.c,v 1.64 2022/06/01 06:05:47 martin Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/audioio.h>
     42 #include <sys/device.h>
     43 #include <sys/systm.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <dev/audio/audio_if.h>
     47 #include <dev/ofw/openfirm.h>
     48 #include <macppc/dev/dbdma.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 #include <dev/i2c/i2cvar.h>
     52 #include <dev/onewire/onewirevar.h>
     53 
     54 #include <machine/autoconf.h>
     55 #include <machine/pio.h>
     56 
     57 #include <macppc/dev/deqvar.h>
     58 #include <macppc/dev/obiovar.h>
     59 
     60 #include "opt_snapper.h"
     61 
     62 #ifdef SNAPPER_DEBUG
     63 # define DPRINTF printf
     64 #else
     65 # define DPRINTF while (0) printf
     66 #endif
     67 
     68 #define SNAPPER_MAXPAGES	16
     69 
     70 struct snapper_softc {
     71 	device_t sc_dev;
     72 	int sc_mode;
     73 #define SNAPPER_IS_TAS3004	0 // codec is TAS3004
     74 #define SNAPPER_IS_TAS3001	1 // codec is TAS3001
     75 #define SNAPPER_IS_PCM3052	2 // codec is PCM3052
     76 #define SNAPPER_IS_CS8416	3 // codec is CS8416
     77 #define SNAPPER_SWVOL		4 // software codec
     78 
     79 	int sc_node;
     80 
     81 	void (*sc_ointr)(void *);	/* dma completion intr handler */
     82 	void *sc_oarg;			/* arg for sc_ointr() */
     83 	int sc_opages;			/* # of output pages */
     84 
     85 	void (*sc_iintr)(void *);	/* dma completion intr handler */
     86 	void *sc_iarg;			/* arg for sc_iintr() */
     87 	int sc_ipages;			/* # of input pages */
     88 
     89 	u_int sc_record_source;		/* recording source mask */
     90 	u_int sc_output_mask;		/* output source mask */
     91 
     92 	bus_space_tag_t sc_tag;
     93 	bus_space_handle_t sc_bsh;
     94 	i2c_addr_t sc_deqaddr;
     95 	i2c_tag_t sc_i2c;
     96 	uint32_t sc_baseaddr;
     97 
     98 	int sc_rate;                    /* current sampling rate */
     99 	int sc_bitspersample;
    100 
    101 	/* for SNAPPER_SWVOL */
    102 	u_int sc_swvol_l;
    103 	u_int sc_swvol_r;
    104 
    105 	u_int sc_vol_l;
    106 	u_int sc_vol_r;
    107 	u_int sc_treble;
    108 	u_int sc_bass;
    109 	u_int mixer[6]; /* s1_l, s2_l, an_l, s1_r, s2_r, an_r */
    110 	uint16_t sc_rval;
    111 
    112 	bus_space_handle_t sc_odmah;
    113 	bus_space_handle_t sc_idmah;
    114 	dbdma_regmap_t *sc_odma;
    115 	dbdma_regmap_t *sc_idma;
    116 	unsigned char	dbdma_cmdspace[sizeof(struct dbdma_command) * 40 + 15];
    117 	struct dbdma_command *sc_odmacmd;
    118 	struct dbdma_command *sc_idmacmd;
    119 
    120 	kmutex_t sc_lock;
    121 	kmutex_t sc_intr_lock;
    122 
    123 	struct onewire_bus	sc_ow_bus;
    124 	device_t		sc_ow_dev;
    125 	int			sc_ow_data;
    126 };
    127 
    128 static int snapper_match(device_t, struct cfdata *, void *);
    129 static void snapper_attach(device_t, device_t, void *);
    130 static void snapper_defer(device_t);
    131 static int snapper_intr(void *);
    132 static int snapper_query_format(void *, audio_format_query_t *);
    133 static int snapper_set_format(void *, int,
    134     const audio_params_t *, const audio_params_t *,
    135     audio_filter_reg_t *, audio_filter_reg_t *);
    136 static int snapper_commit_settings(void *);
    137 static int snapper_round_blocksize(void *, int, int, const audio_params_t *);
    138 static int snapper_halt_output(void *);
    139 static int snapper_halt_input(void *);
    140 static int snapper_getdev(void *, struct audio_device *);
    141 static int snapper_set_port(void *, mixer_ctrl_t *);
    142 static int snapper_get_port(void *, mixer_ctrl_t *);
    143 static int snapper_query_devinfo(void *, mixer_devinfo_t *);
    144 static size_t snapper_round_buffersize(void *, int, size_t);
    145 static int snapper_get_props(void *);
    146 static int snapper_trigger_output(void *, void *, void *, int, void (*)(void *),
    147     void *, const audio_params_t *);
    148 static int snapper_trigger_input(void *, void *, void *, int, void (*)(void *),
    149     void *, const audio_params_t *);
    150 static void snapper_get_locks(void *, kmutex_t **, kmutex_t **);
    151 static void snapper_set_volume(struct snapper_softc *, u_int, u_int);
    152 static int snapper_set_rate(struct snapper_softc *);
    153 static void snapper_set_treble(struct snapper_softc *, u_int);
    154 static void snapper_set_bass(struct snapper_softc *, u_int);
    155 static void snapper_write_mixers(struct snapper_softc *);
    156 
    157 static int tas3004_write(struct snapper_softc *, u_int, const void *);
    158 static int gpio_read(bus_size_t);
    159 static void gpio_write(bus_size_t, int);
    160 static void snapper_mute_speaker(struct snapper_softc *, int);
    161 static void snapper_mute_headphone(struct snapper_softc *, int);
    162 static void snapper_mute_lineout(struct snapper_softc *, int);
    163 static int snapper_cint(void *);
    164 static int tas3004_init(struct snapper_softc *);
    165 static void snapper_init(struct snapper_softc *, int);
    166 
    167 static void snapper_setup_ow(struct snapper_softc *);
    168 static int snapper_ow_reset(void *);
    169 static int snapper_ow_read_bit(void *);
    170 static void snapper_ow_write_bit(void *, int);
    171 
    172 static void snapper_bb_rx(void *);
    173 static void snapper_bb_tx(void *);
    174 static int snapper_bb_get(void *);
    175 static void snapper_bb_set(void *, int);
    176 
    177 static const struct onewire_bbops snapper_bbops = {
    178 	snapper_bb_rx,
    179 	snapper_bb_tx,
    180 	snapper_bb_get,
    181 	snapper_bb_set
    182 };
    183 
    184 
    185 static void
    186 snapper_volume(audio_filter_arg_t *arg)
    187 {
    188 	struct snapper_softc *sc;
    189 	const aint_t *src;
    190 	aint_t *dst;
    191 	u_int sample_count;
    192 	u_int i;
    193 
    194 	sc = arg->context;
    195 	src = arg->src;
    196 	dst = arg->dst;
    197 	sample_count = arg->count * arg->srcfmt->channels;
    198 	for (i = 0; i < sample_count; i++) {
    199 		aint2_t l = (aint2_t)(*src++);
    200 		l = l * sc->sc_swvol_l / 255;
    201 		*dst++ = (aint_t)l;
    202 	}
    203 }
    204 
    205 /*
    206  * A hardware bug in the TAS3004 I2S transport
    207  * produces phase differences between channels
    208  * (left channel appears delayed by one sample).
    209  * Fix the phase difference by delaying the right channel
    210  * by one sample.
    211  */
    212 static void
    213 snapper_fixphase(audio_filter_arg_t *arg)
    214 {
    215 	struct snapper_softc *sc;
    216 	const aint_t *src;
    217 	aint_t *dst;
    218 	u_int i;
    219 
    220 	sc = arg->context;
    221 	src = arg->src;
    222 	dst = arg->dst;
    223 	for (i = 0; i < arg->count; i++) {
    224 		*dst++ = *src++;
    225 		*dst++ = sc->sc_rval;
    226 		sc->sc_rval = *src++;
    227 	}
    228 }
    229 
    230 CFATTACH_DECL_NEW(snapper, sizeof(struct snapper_softc), snapper_match,
    231 	snapper_attach, NULL, NULL);
    232 
    233 const struct audio_hw_if snapper_hw_if = {
    234 	.query_format		= snapper_query_format,
    235 	.set_format		= snapper_set_format,
    236 	.commit_settings	= snapper_commit_settings,
    237 	.round_blocksize	= snapper_round_blocksize,
    238 	.halt_output		= snapper_halt_output,
    239 	.halt_input		= snapper_halt_input,
    240 	.getdev			= snapper_getdev,
    241 	.set_port		= snapper_set_port,
    242 	.get_port		= snapper_get_port,
    243 	.query_devinfo		= snapper_query_devinfo,
    244 	.round_buffersize	= snapper_round_buffersize,
    245 	.get_props		= snapper_get_props,
    246 	.trigger_output		= snapper_trigger_output,
    247 	.trigger_input		= snapper_trigger_input,
    248 	.get_locks		= snapper_get_locks,
    249 };
    250 
    251 struct audio_device snapper_device = {
    252 	"SNAPPER",
    253 	"",
    254 	"snapper"
    255 };
    256 
    257 #define SNAPPER_BASSTAB_0DB	18
    258 const uint8_t snapper_basstab[] = {
    259 	0x96,	/* -18dB */
    260 	0x94,	/* -17dB */
    261 	0x92,	/* -16dB */
    262 	0x90,	/* -15dB */
    263 	0x8e,	/* -14dB */
    264 	0x8c,	/* -13dB */
    265 	0x8a,	/* -12dB */
    266 	0x88,	/* -11dB */
    267 	0x86,	/* -10dB */
    268 	0x84,	/* -9dB */
    269 	0x82,	/* -8dB */
    270 	0x80,	/* -7dB */
    271 	0x7e,	/* -6dB */
    272 	0x7c,	/* -5dB */
    273 	0x7a,	/* -4dB */
    274 	0x78,	/* -3dB */
    275 	0x76,	/* -2dB */
    276 	0x74,	/* -1dB */
    277 	0x72,	/* 0dB */
    278 	0x6f,	/* 1dB */
    279 	0x6d,	/* 2dB */
    280 	0x6a,	/* 3dB */
    281 	0x67,	/* 4dB */
    282 	0x65,	/* 5dB */
    283 	0x62,	/* 6dB */
    284 	0x5f,	/* 7dB */
    285 	0x5b,	/* 8dB */
    286 	0x55,	/* 9dB */
    287 	0x4f,	/* 10dB */
    288 	0x49,	/* 11dB */
    289 	0x43,	/* 12dB */
    290 	0x3b,	/* 13dB */
    291 	0x33,	/* 14dB */
    292 	0x29,	/* 15dB */
    293 	0x1e,	/* 16dB */
    294 	0x11,	/* 17dB */
    295 	0x01,	/* 18dB */
    296 };
    297 
    298 #define SNAPPER_MIXER_GAIN_0DB		36
    299 const uint8_t snapper_mixer_gain[178][3] = {
    300 	{ 0x7f, 0x17, 0xaf }, /* 18.0 dB */
    301 	{ 0x77, 0xfb, 0xaa }, /* 17.5 dB */
    302 	{ 0x71, 0x45, 0x75 }, /* 17.0 dB */
    303 	{ 0x6a, 0xef, 0x5d }, /* 16.5 dB */
    304 	{ 0x64, 0xf4, 0x03 }, /* 16.0 dB */
    305 	{ 0x5f, 0x4e, 0x52 }, /* 15.5 dB */
    306 	{ 0x59, 0xf9, 0x80 }, /* 15.0 dB */
    307 	{ 0x54, 0xf1, 0x06 }, /* 14.5 dB */
    308 	{ 0x50, 0x30, 0xa1 }, /* 14.0 dB */
    309 	{ 0x4b, 0xb4, 0x46 }, /* 13.5 dB */
    310 	{ 0x47, 0x78, 0x28 }, /* 13.0 dB */
    311 	{ 0x43, 0x78, 0xb0 }, /* 12.5 dB */
    312 	{ 0x3f, 0xb2, 0x78 }, /* 12.0 dB */
    313 	{ 0x3c, 0x22, 0x4c }, /* 11.5 dB */
    314 	{ 0x38, 0xc5, 0x28 }, /* 11.0 dB */
    315 	{ 0x35, 0x98, 0x2f }, /* 10.5 dB */
    316 	{ 0x32, 0x98, 0xb0 }, /* 10.0 dB */
    317 	{ 0x2f, 0xc4, 0x20 }, /* 9.5 dB */
    318 	{ 0x2d, 0x18, 0x18 }, /* 9.0 dB */
    319 	{ 0x2a, 0x92, 0x54 }, /* 8.5 dB */
    320 	{ 0x28, 0x30, 0xaf }, /* 8.0 dB */
    321 	{ 0x25, 0xf1, 0x25 }, /* 7.5 dB */
    322 	{ 0x23, 0xd1, 0xcd }, /* 7.0 dB */
    323 	{ 0x21, 0xd0, 0xd9 }, /* 6.5 dB */
    324 	{ 0x1f, 0xec, 0x98 }, /* 6.0 dB */
    325 	{ 0x1e, 0x23, 0x6d }, /* 5.5 dB */
    326 	{ 0x1c, 0x73, 0xd5 }, /* 5.0 dB */
    327 	{ 0x1a, 0xdc, 0x61 }, /* 4.5 dB */
    328 	{ 0x19, 0x5b, 0xb8 }, /* 4.0 dB */
    329 	{ 0x17, 0xf0, 0x94 }, /* 3.5 dB */
    330 	{ 0x16, 0x99, 0xc0 }, /* 3.0 dB */
    331 	{ 0x15, 0x56, 0x1a }, /* 2.5 dB */
    332 	{ 0x14, 0x24, 0x8e }, /* 2.0 dB */
    333 	{ 0x13, 0x04, 0x1a }, /* 1.5 dB */
    334 	{ 0x11, 0xf3, 0xc9 }, /* 1.0 dB */
    335 	{ 0x10, 0xf2, 0xb4 }, /* 0.5 dB */
    336 	{ 0x10, 0x00, 0x00 }, /* 0.0 dB */
    337 	{ 0x0f, 0x1a, 0xdf }, /* -0.5 dB */
    338 	{ 0x0e, 0x42, 0x90 }, /* -1.0 dB */
    339 	{ 0x0d, 0x76, 0x5a }, /* -1.5 dB */
    340 	{ 0x0c, 0xb5, 0x91 }, /* -2.0 dB */
    341 	{ 0x0b, 0xff, 0x91 }, /* -2.5 dB */
    342 	{ 0x0b, 0x53, 0xbe }, /* -3.0 dB */
    343 	{ 0x0a, 0xb1, 0x89 }, /* -3.5 dB */
    344 	{ 0x0a, 0x18, 0x66 }, /* -4.0 dB */
    345 	{ 0x09, 0x87, 0xd5 }, /* -4.5 dB */
    346 	{ 0x08, 0xff, 0x59 }, /* -5.0 dB */
    347 	{ 0x08, 0x7e, 0x80 }, /* -5.5 dB */
    348 	{ 0x08, 0x04, 0xdc }, /* -6.0 dB */
    349 	{ 0x07, 0x92, 0x07 }, /* -6.5 dB */
    350 	{ 0x07, 0x25, 0x9d }, /* -7.0 dB */
    351 	{ 0x06, 0xbf, 0x44 }, /* -7.5 dB */
    352 	{ 0x06, 0x5e, 0xa5 }, /* -8.0 dB */
    353 	{ 0x06, 0x03, 0x6e }, /* -8.5 dB */
    354 	{ 0x05, 0xad, 0x50 }, /* -9.0 dB */
    355 	{ 0x05, 0x5c, 0x04 }, /* -9.5 dB */
    356 	{ 0x05, 0x0f, 0x44 }, /* -10.0 dB */
    357 	{ 0x04, 0xc6, 0xd0 }, /* -10.5 dB */
    358 	{ 0x04, 0x82, 0x68 }, /* -11.0 dB */
    359 	{ 0x04, 0x41, 0xd5 }, /* -11.5 dB */
    360 	{ 0x04, 0x04, 0xde }, /* -12.0 dB */
    361 	{ 0x03, 0xcb, 0x50 }, /* -12.5 dB */
    362 	{ 0x03, 0x94, 0xfa }, /* -13.0 dB */
    363 	{ 0x03, 0x61, 0xaf }, /* -13.5 dB */
    364 	{ 0x03, 0x31, 0x42 }, /* -14.0 dB */
    365 	{ 0x03, 0x03, 0x8a }, /* -14.5 dB */
    366 	{ 0x02, 0xd8, 0x62 }, /* -15.0 dB */
    367 	{ 0x02, 0xaf, 0xa3 }, /* -15.5 dB */
    368 	{ 0x02, 0x89, 0x2c }, /* -16.0 dB */
    369 	{ 0x02, 0x64, 0xdb }, /* -16.5 dB */
    370 	{ 0x02, 0x42, 0x93 }, /* -17.0 dB */
    371 	{ 0x02, 0x22, 0x35 }, /* -17.5 dB */
    372 	{ 0x02, 0x03, 0xa7 }, /* -18.0 dB */
    373 	{ 0x01, 0xe6, 0xcf }, /* -18.5 dB */
    374 	{ 0x01, 0xcb, 0x94 }, /* -19.0 dB */
    375 	{ 0x01, 0xb1, 0xde }, /* -19.5 dB */
    376 	{ 0x01, 0x99, 0x99 }, /* -20.0 dB */
    377 	{ 0x01, 0x82, 0xaf }, /* -20.5 dB */
    378 	{ 0x01, 0x6d, 0x0e }, /* -21.0 dB */
    379 	{ 0x01, 0x58, 0xa2 }, /* -21.5 dB */
    380 	{ 0x01, 0x45, 0x5b }, /* -22.0 dB */
    381 	{ 0x01, 0x33, 0x28 }, /* -22.5 dB */
    382 	{ 0x01, 0x21, 0xf9 }, /* -23.0 dB */
    383 	{ 0x01, 0x11, 0xc0 }, /* -23.5 dB */
    384 	{ 0x01, 0x02, 0x70 }, /* -24.0 dB */
    385 	{ 0x00, 0xf3, 0xfb }, /* -24.5 dB */
    386 	{ 0x00, 0xe6, 0x55 }, /* -25.0 dB */
    387 	{ 0x00, 0xd9, 0x73 }, /* -25.5 dB */
    388 	{ 0x00, 0xcd, 0x49 }, /* -26.0 dB */
    389 	{ 0x00, 0xc1, 0xcd }, /* -26.5 dB */
    390 	{ 0x00, 0xb6, 0xf6 }, /* -27.0 dB */
    391 	{ 0x00, 0xac, 0xba }, /* -27.5 dB */
    392 	{ 0x00, 0xa3, 0x10 }, /* -28.0 dB */
    393 	{ 0x00, 0x99, 0xf1 }, /* -28.5 dB */
    394 	{ 0x00, 0x91, 0x54 }, /* -29.0 dB */
    395 	{ 0x00, 0x89, 0x33 }, /* -29.5 dB */
    396 	{ 0x00, 0x81, 0x86 }, /* -30.0 dB */
    397 	{ 0x00, 0x7a, 0x48 }, /* -30.5 dB */
    398 	{ 0x00, 0x73, 0x70 }, /* -31.0 dB */
    399 	{ 0x00, 0x6c, 0xfb }, /* -31.5 dB */
    400 	{ 0x00, 0x66, 0xe3 }, /* -32.0 dB */
    401 	{ 0x00, 0x61, 0x21 }, /* -32.5 dB */
    402 	{ 0x00, 0x5b, 0xb2 }, /* -33.0 dB */
    403 	{ 0x00, 0x56, 0x91 }, /* -33.5 dB */
    404 	{ 0x00, 0x51, 0xb9 }, /* -34.0 dB */
    405 	{ 0x00, 0x4d, 0x27 }, /* -34.5 dB */
    406 	{ 0x00, 0x48, 0xd6 }, /* -35.0 dB */
    407 	{ 0x00, 0x44, 0xc3 }, /* -35.5 dB */
    408 	{ 0x00, 0x40, 0xea }, /* -36.0 dB */
    409 	{ 0x00, 0x3d, 0x49 }, /* -36.5 dB */
    410 	{ 0x00, 0x39, 0xdb }, /* -37.0 dB */
    411 	{ 0x00, 0x36, 0x9e }, /* -37.5 dB */
    412 	{ 0x00, 0x33, 0x90 }, /* -38.0 dB */
    413 	{ 0x00, 0x30, 0xae }, /* -38.5 dB */
    414 	{ 0x00, 0x2d, 0xf5 }, /* -39.0 dB */
    415 	{ 0x00, 0x2b, 0x63 }, /* -39.5 dB */
    416 	{ 0x00, 0x28, 0xf5 }, /* -40.0 dB */
    417 	{ 0x00, 0x26, 0xab }, /* -40.5 dB */
    418 	{ 0x00, 0x24, 0x81 }, /* -41.0 dB */
    419 	{ 0x00, 0x22, 0x76 }, /* -41.5 dB */
    420 	{ 0x00, 0x20, 0x89 }, /* -42.0 dB */
    421 	{ 0x00, 0x1e, 0xb7 }, /* -42.5 dB */
    422 	{ 0x00, 0x1c, 0xff }, /* -43.0 dB */
    423 	{ 0x00, 0x1b, 0x60 }, /* -43.5 dB */
    424 	{ 0x00, 0x19, 0xd8 }, /* -44.0 dB */
    425 	{ 0x00, 0x18, 0x65 }, /* -44.5 dB */
    426 	{ 0x00, 0x17, 0x08 }, /* -45.0 dB */
    427 	{ 0x00, 0x15, 0xbe }, /* -45.5 dB */
    428 	{ 0x00, 0x14, 0x87 }, /* -46.0 dB */
    429 	{ 0x00, 0x13, 0x61 }, /* -46.5 dB */
    430 	{ 0x00, 0x12, 0x4b }, /* -47.0 dB */
    431 	{ 0x00, 0x11, 0x45 }, /* -47.5 dB */
    432 	{ 0x00, 0x10, 0x4e }, /* -48.0 dB */
    433 	{ 0x00, 0x0f, 0x64 }, /* -48.5 dB */
    434 	{ 0x00, 0x0e, 0x88 }, /* -49.0 dB */
    435 	{ 0x00, 0x0d, 0xb8 }, /* -49.5 dB */
    436 	{ 0x00, 0x0c, 0xf3 }, /* -50.0 dB */
    437 	{ 0x00, 0x0c, 0x3a }, /* -50.5 dB */
    438 	{ 0x00, 0x0b, 0x8b }, /* -51.0 dB */
    439 	{ 0x00, 0x0a, 0xe5 }, /* -51.5 dB */
    440 	{ 0x00, 0x0a, 0x49 }, /* -52.0 dB */
    441 	{ 0x00, 0x09, 0xb6 }, /* -52.5 dB */
    442 	{ 0x00, 0x09, 0x2b }, /* -53.0 dB */
    443 	{ 0x00, 0x08, 0xa8 }, /* -53.5 dB */
    444 	{ 0x00, 0x08, 0x2c }, /* -54.0 dB */
    445 	{ 0x00, 0x07, 0xb7 }, /* -54.5 dB */
    446 	{ 0x00, 0x07, 0x48 }, /* -55.0 dB */
    447 	{ 0x00, 0x06, 0xe0 }, /* -55.5 dB */
    448 	{ 0x00, 0x06, 0x7d }, /* -56.0 dB */
    449 	{ 0x00, 0x06, 0x20 }, /* -56.5 dB */
    450 	{ 0x00, 0x05, 0xc9 }, /* -57.0 dB */
    451 	{ 0x00, 0x05, 0x76 }, /* -57.5 dB */
    452 	{ 0x00, 0x05, 0x28 }, /* -58.0 dB */
    453 	{ 0x00, 0x04, 0xde }, /* -58.5 dB */
    454 	{ 0x00, 0x04, 0x98 }, /* -59.0 dB */
    455 	{ 0x00, 0x04, 0x56 }, /* -59.5 dB */
    456 	{ 0x00, 0x04, 0x18 }, /* -60.0 dB */
    457 	{ 0x00, 0x03, 0xdd }, /* -60.5 dB */
    458 	{ 0x00, 0x03, 0xa6 }, /* -61.0 dB */
    459 	{ 0x00, 0x03, 0x72 }, /* -61.5 dB */
    460 	{ 0x00, 0x03, 0x40 }, /* -62.0 dB */
    461 	{ 0x00, 0x03, 0x12 }, /* -62.5 dB */
    462 	{ 0x00, 0x02, 0xe6 }, /* -63.0 dB */
    463 	{ 0x00, 0x02, 0xbc }, /* -63.5 dB */
    464 	{ 0x00, 0x02, 0x95 }, /* -64.0 dB */
    465 	{ 0x00, 0x02, 0x70 }, /* -64.5 dB */
    466 	{ 0x00, 0x02, 0x4d }, /* -65.0 dB */
    467 	{ 0x00, 0x02, 0x2c }, /* -65.5 dB */
    468 	{ 0x00, 0x02, 0x0d }, /* -66.0 dB */
    469 	{ 0x00, 0x01, 0xf0 }, /* -66.5 dB */
    470 	{ 0x00, 0x01, 0xd4 }, /* -67.0 dB */
    471 	{ 0x00, 0x01, 0xba }, /* -67.5 dB */
    472 	{ 0x00, 0x01, 0xa1 }, /* -68.0 dB */
    473 	{ 0x00, 0x01, 0x8a }, /* -68.5 dB */
    474 	{ 0x00, 0x01, 0x74 }, /* -69.0 dB */
    475 	{ 0x00, 0x01, 0x5f }, /* -69.5 dB */
    476 	{ 0x00, 0x01, 0x4b }, /* -70.0 dB */
    477 	{ 0x00, 0x00, 0x00 }  /* Mute */
    478 };
    479 
    480 /* The HW actually supports precisions more than 16bit, but 16bit is enough. */
    481 static const struct audio_format snapper_formats[] = {
    482 	{
    483 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
    484 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    485 		.validbits	= 16,
    486 		.precision	= 16,
    487 		.channels	= 2,
    488 		.channel_mask	= AUFMT_STEREO,
    489 		.frequency_type	= 3,
    490 		.frequency	= { 32000, 44100, 48000 },
    491 	}
    492 };
    493 #define SNAPPER_NFORMATS	__arraycount(snapper_formats)
    494 
    495 static const struct audio_format tumbler_formats[] = {
    496 	{
    497 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
    498 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    499 		.validbits	= 16,
    500 		.precision	= 16,
    501 		.channels	= 2,
    502 		.channel_mask	= AUFMT_STEREO,
    503 		.frequency_type	= 4,
    504 		.frequency	= { 32000, 44100, 48000, 96000 },
    505 	},
    506 };
    507 #define TUMBLER_NFORMATS	__arraycount(tumbler_formats)
    508 
    509 /* OF hands us the codec in 16bit mode, run with it for now */
    510 static const struct audio_format onyx_formats[] = {
    511 	{
    512 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
    513 		.encoding	= AUDIO_ENCODING_SLINEAR_BE,
    514 		.validbits	= 16,
    515 		.precision	= 16,
    516 		.channels	= 2,
    517 		.channel_mask	= AUFMT_STEREO,
    518 		.frequency_type	= 3,
    519 		.frequency	= { 44100, 48000, 96000 },
    520 	},
    521 };
    522 #define ONYX_NFORMATS	__arraycount(onyx_formats)
    523 
    524 static bus_size_t amp_mute;
    525 static bus_size_t headphone_mute = 0;
    526 static bus_size_t audio_hw_reset;
    527 static bus_size_t headphone_detect = 0;
    528 static bus_size_t lineout_detect = 0;
    529 static bus_size_t lineout_mute= 0;
    530 static bus_size_t owaddr = -1;
    531 static uint8_t headphone_detect_active = 0;
    532 static uint8_t lineout_detect_active = 0;
    533 
    534 
    535 /* I2S registers */
    536 #define I2S_INT		0x00
    537 #define I2S_FORMAT	0x10
    538 #define I2S_FRAMECOUNT	0x40
    539 #define I2S_FRAMEMATCH	0x50
    540 #define I2S_WORDSIZE	0x60
    541 
    542 /* I2S_INT register definitions */
    543 #define I2S_INT_CLKSTOPPEND 0x01000000  /* clock-stop interrupt pending */
    544 
    545 /* I2S_WORDSIZE register definitions */
    546 #define INPUT_STEREO            (2 << 24)
    547 #define INPUT_MONO              (1 << 24)
    548 #define INPUT_16BIT             (0 << 16)
    549 #define INPUT_24BIT             (3 << 16)
    550 #define OUTPUT_STEREO           (2 << 8)
    551 #define OUTPUT_MONO             (1 << 8)
    552 #define OUTPUT_16BIT            (0 << 0)
    553 #define OUTPUT_24BIT            (3 << 0)
    554 
    555 /* FCR(0x3c) bits */
    556 #define KEYLARGO_FCR1   0x3c
    557 #define  I2S0CLKEN      0x1000
    558 #define  I2S0EN         0x2000
    559 #define  I2S1CLKEN      0x080000
    560 #define  I2S1EN         0x100000
    561 #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN"
    562 
    563 /* TAS3004/TAS3001 registers */
    564 #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
    565 #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?)
    566                             	   2 bytes (reserved) on the TAS 3001 */
    567 #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
    568 #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
    569 #define DEQ_BASS	0x06	/* Bass control (1byte) */
    570 #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes; 3 on TAS3001) */
    571 #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes; 3 on TAS3001) */
    572 #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
    573 #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
    574 #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
    575 #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
    576 #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
    577 #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
    578 #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
    579 #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
    580 #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
    581 #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
    582 #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
    583 #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
    584 #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
    585 #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
    586 #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
    587 #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
    588 #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
    589 #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
    590 #define DEQ_ACR		0x40	/* [TAS3004] Analog control register (1byte) */
    591 #define DEQ_MCR2	0x43	/* [TAS3004] Main control register 2 (1byte) */
    592 #define DEQ_MCR1_FL	0x80	/* Fast load */
    593 #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
    594 #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
    595 #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
    596 #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
    597 #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
    598 #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
    599 #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
    600 #define DEQ_MCR1_ISM	0x0c	/* [TAS3001] Input serial port mode */
    601 #define  DEQ_MCR1_ISM_L	0x00	/*           Left justified */
    602 #define  DEQ_MCR1_ISM_R	0x04	/*           Right justified */
    603 #define  DEQ_MCR1_ISM_I2S 0x08	/*           I2S */
    604 #define DEQ_MCR1_W	0x03	/* Serial port word length */
    605 #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
    606 #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
    607 #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
    608 #define  DEQ_MCR1_W_24	0x03	/*  24 bit */
    609 
    610 #define DEQ_MCR2_DL	0x80	/* Download */
    611 #define DEQ_MCR2_AP	0x02	/* All pass mode */
    612 
    613 #define DEQ_ACR_ADM	0x80	/* ADC output mode */
    614 #define DEQ_ACR_LRB	0x40	/* Select B input */
    615 #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
    616 #define  DEQ_ACR_DM_OFF	0x00	/*  off */
    617 #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
    618 #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
    619 #define DEQ_ACR_INP	0x02	/* Analog input select */
    620 #define  DEQ_ACR_INP_A	0x00	/*  A */
    621 #define  DEQ_ACR_INP_B	0x02	/*  B */
    622 #define DEQ_ACR_APD	0x01	/* Analog power down */
    623 
    624 struct tas3004_reg {
    625 	u_char MCR1[1];
    626 	u_char DRC[6];
    627 	u_char VOLUME[6];
    628 	u_char TREBLE[1];
    629 	u_char BASS[1];
    630 	u_char MIXER_L[9];
    631 	u_char MIXER_R[9];
    632 	u_char LB0[15];
    633 	u_char LB1[15];
    634 	u_char LB2[15];
    635 	u_char LB3[15];
    636 	u_char LB4[15];
    637 	u_char LB5[15];
    638 	u_char LB6[15];
    639 	u_char RB0[15];
    640 	u_char RB1[15];
    641 	u_char RB2[15];
    642 	u_char RB3[15];
    643 	u_char RB4[15];
    644 	u_char RB5[15];
    645 	u_char RB6[15];
    646 	u_char LLB[15];
    647 	u_char RLB[15];
    648 	u_char LLB_GAIN[3];
    649 	u_char RLB_GAIN[3];
    650 	u_char ACR[1];
    651 	u_char MCR2[1];
    652 };
    653 
    654 #define GPIO_OUTSEL	0xf0	/* Output select */
    655 		/*	0x00	GPIO bit0 is output
    656 			0x10	media-bay power
    657 			0x20	reserved
    658 			0x30	MPIC */
    659 
    660 #define GPIO_ALTOE	0x08	/* Alternate output enable */
    661 		/*	0x00	Use DDR
    662 			0x08	Use output select */
    663 
    664 #define GPIO_DDR	0x04	/* Data direction */
    665 #define GPIO_DDR_OUTPUT	0x04	/* Output */
    666 #define GPIO_DDR_INPUT	0x00	/* Input */
    667 
    668 #define GPIO_LEVEL	0x02	/* Pin level (RO) */
    669 
    670 #define	GPIO_DATA	0x01	/* Data */
    671 
    672 static int
    673 snapper_match(device_t parent, struct cfdata *match, void *aux)
    674 {
    675 	struct confargs *ca;
    676 	int soundbus, soundchip, soundcodec;
    677 	char compat[32];
    678 
    679 	ca = aux;
    680 	if (strcmp(ca->ca_name, "i2s") != 0)
    681 		return 0;
    682 
    683 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
    684 	    (soundchip = OF_child(soundbus)) == 0)
    685 		return 0;
    686 
    687 	memset(compat, 0, sizeof compat);
    688 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
    689 
    690 	if (strcmp(compat, "snapper") == 0)
    691 		return 1;
    692 
    693 	if (strcmp(compat, "tumbler") == 0)
    694 		return 1;
    695 
    696 	if (strcmp(compat, "AOAKeylargo") == 0)
    697 		return 1;
    698 
    699 	if (strcmp(compat, "AOAK2") == 0)
    700 		return 1;
    701 
    702 	if (strcmp(compat, "AOAShasta") == 0)
    703 		return 1;
    704 
    705 	if (strcmp(compat, "AOAbase") == 0)
    706 		return 1;
    707 
    708 	if (OF_getprop(soundchip, "platform-tas-codec-ref",
    709 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
    710 		return 1;
    711 
    712 	return 0;
    713 }
    714 
    715 static void
    716 snapper_attach(device_t parent, device_t self, void *aux)
    717 {
    718 	struct snapper_softc *sc;
    719 	struct confargs *ca;
    720 	int cirq, oirq, iirq, /*cirq_type,*/ oirq_type, iirq_type, soundbus;
    721 	uint32_t intr[6], reg[6];
    722 	char compat[32], intr_xname[INTRDEVNAMEBUF];
    723 
    724 	sc = device_private(self);
    725 	sc->sc_dev = self;
    726 
    727 	ca = aux;
    728 
    729 	soundbus = OF_child(ca->ca_node);
    730 	memset(compat, 0, sizeof compat);
    731 	OF_getprop(OF_child(soundbus), "compatible", compat, sizeof compat);
    732 
    733 	sc->sc_mode = SNAPPER_IS_TAS3004;
    734 
    735 	if (strcmp(compat, "tumbler") == 0)
    736 		sc->sc_mode = SNAPPER_IS_TAS3001;
    737 	sc->sc_swvol_l = 255;
    738 	sc->sc_swvol_r = 255;
    739 	sc->sc_vol_l = 128;
    740 	sc->sc_vol_r = 128;
    741 	sc->sc_rval = 0;
    742 
    743 	sc->sc_odmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    744 				     sizeof(struct dbdma_command), NULL);
    745 	sc->sc_idmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    746 				     sizeof(struct dbdma_command), NULL);
    747 
    748 	sc->sc_baseaddr = ca->ca_baseaddr;
    749 
    750 	OF_getprop(soundbus, "reg", reg, sizeof reg);
    751 	/* deal with messed up properties on PowerMac7,3 and friends */
    752 	if (reg[0] == 0) {
    753 		reg[0] += ca->ca_reg[0];
    754 		reg[2] += ca->ca_reg[2];
    755 		reg[4] += ca->ca_reg[2];
    756 	}
    757 	reg[0] += ca->ca_baseaddr;
    758 	reg[2] += ca->ca_baseaddr;
    759 	reg[4] += ca->ca_baseaddr;
    760 
    761 	sc->sc_node = ca->ca_node;
    762 	sc->sc_tag = ca->ca_tag;
    763 
    764 #ifdef SNAPPER_DEBUG
    765 	{
    766 		int i;
    767 		printf("\n");
    768 		for (i = 0; i < 6; i++) {
    769 			printf(" %08x", reg[i]);
    770 		}
    771 		printf("\n");
    772 	}
    773 #endif
    774 
    775 	bus_space_map(sc->sc_tag, reg[0], reg[1], 0, &sc->sc_bsh);
    776 	obio_space_map(reg[2], reg[3], &sc->sc_odmah);
    777 	obio_space_map(reg[4], reg[5], &sc->sc_idmah);
    778 
    779 	sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
    780 	sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah);
    781 
    782 	DPRINTF("reg %08x odma %08x\n", (uint32_t)sc->sc_bsh, (uint32_t)sc->sc_odmah);
    783 
    784 	OF_getprop(soundbus, "interrupts", intr, sizeof intr);
    785 	cirq = intr[0];
    786 	oirq = intr[2];
    787 	iirq = intr[4];
    788 	/* cirq_type = intr[1] ? IST_LEVEL : IST_EDGE; */
    789 	oirq_type = (intr[3] & 1) ? IST_LEVEL : IST_EDGE;
    790 	iirq_type = (intr[5] & 1) ? IST_LEVEL : IST_EDGE;
    791 
    792 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
    793 
    794 	snprintf(intr_xname, sizeof(intr_xname), "%s out", device_xname(self));
    795 	intr_establish_xname(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc,
    796 	    intr_xname);
    797 
    798 	snprintf(intr_xname, sizeof(intr_xname), "%s in", device_xname(self));
    799 	intr_establish_xname(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc,
    800 	    intr_xname);
    801 
    802 	aprint_normal(": irq %d,%d,%d\n", cirq, oirq, iirq);
    803 
    804 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    805 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    806 
    807 	/* PMF event handler */
    808 	pmf_device_register(sc->sc_dev, NULL, NULL);
    809 
    810 	config_defer(self, snapper_defer);
    811 }
    812 
    813 static void
    814 snapper_defer(device_t dev)
    815 {
    816 	struct snapper_softc *sc;
    817 	device_t dv;
    818 	deviter_t di;
    819 	struct deq_softc *deq;
    820 	char prop[64], next[64], codec[64], *cref;
    821 	int codec_node, soundbus, sound, ok, deqnode = 0;
    822 
    823 	sc = device_private(dev);
    824 
    825 	/* look for platform-*-codec-ref node */
    826 
    827 	/*
    828 	 * XXX
    829 	 * there can be more than one i2sbus, the one we want just so happens
    830 	 * to be the first we see
    831 	 */
    832 	soundbus = OF_child(sc->sc_node);
    833 	sound = OF_child(soundbus);
    834 	ok = OF_nextprop(sound, NULL, next);
    835 	codec_node = 0;
    836 	while (ok && (codec_node == 0)) {
    837 		DPRINTF("prop %d %s\n", ok, next);
    838 		strncpy(prop, next, 64);
    839 		if ((cref = strstr(next, "-codec-ref")) != NULL) {
    840 			OF_getprop(sound, next, &codec_node, 4);
    841 			if (codec_node != 0) {
    842 				OF_getprop(codec_node, "compatible", codec, 64);
    843 				DPRINTF("%08x %s\n", codec_node, codec);
    844 			}
    845 		}
    846 		ok = OF_nextprop(sound, prop, next);
    847 	}
    848 
    849 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    850 	     dv != NULL;
    851 	     dv = deviter_next(&di)) {
    852 		if (device_is_a(dv, "deq")) {
    853 			deq = device_private(dv);
    854 			if (codec_node != 0) {
    855 				if (codec_node != deq->sc_node)
    856 					continue;
    857 			}
    858 			sc->sc_i2c = deq->sc_i2c;
    859 			sc->sc_deqaddr = deq->sc_address;
    860 			deqnode = deq->sc_node;
    861 		}
    862 	}
    863 	deviter_release(&di);
    864 
    865 	DPRINTF("deqnode: %08x\n", deqnode);
    866 
    867 	/* If we don't find a codec, it's not the end of the world;
    868 	 * we can control the volume in software in this case.
    869 	 */
    870 	if (sc->sc_i2c == NULL) {
    871 		sc->sc_mode = SNAPPER_SWVOL;
    872 	} else if (deqnode != 0) {
    873 		int ret;
    874 		codec[0] = 0;
    875 		ret = OF_getprop(deqnode, "compatible", codec, 64);
    876 
    877 		DPRINTF("codec <%s> %d\n", codec, ret);
    878 
    879 		if (codec[0] == 0) {
    880 			if (sc->sc_deqaddr == 0x34) {
    881 				sc->sc_mode = SNAPPER_IS_TAS3001;
    882 			} else {
    883 				int root = OF_finddevice("/");
    884 				char model[32];
    885 				sc->sc_mode = SNAPPER_IS_TAS3004;
    886 				if (OF_getprop(root, "model", model, 32) > 0) {
    887 					printf("model %s\n", model);
    888 					if (strcmp(model, "PowerMac8,1") == 0) {
    889 						sc->sc_mode = SNAPPER_IS_PCM3052;
    890 					}
    891 				}
    892 			}
    893 		} else if (strcmp(codec, "tas3004") == 0) {
    894 			sc->sc_mode = SNAPPER_IS_TAS3004;
    895 		} else if (strcmp(codec, "pcm3052") == 0) {
    896 			sc->sc_mode = SNAPPER_IS_PCM3052;
    897 		} else if (strcmp(codec, "cs8416") == 0) {
    898 			sc->sc_mode = SNAPPER_IS_CS8416;
    899 		}
    900 	}
    901 	DPRINTF("mode %d\n", sc->sc_mode);
    902 	switch (sc->sc_mode) {
    903 	case SNAPPER_SWVOL:
    904 		aprint_verbose("%s: software codec\n", device_xname(dev));
    905 		break;
    906 	case SNAPPER_IS_TAS3001:
    907 		aprint_verbose("%s: codec: TAS3001\n", device_xname(dev));
    908 		break;
    909 	case SNAPPER_IS_TAS3004:
    910 		aprint_verbose("%s: codec: TAS3004\n", device_xname(dev));
    911 		break;
    912 	case SNAPPER_IS_PCM3052:
    913 		aprint_verbose("%s: codec: PCM3052 / ONYX\n", device_xname(dev));
    914 		break;
    915 	default:
    916 		aprint_error_dev(sc->sc_dev, "unsupported codec\n");
    917 		sc->sc_mode = SNAPPER_SWVOL;
    918 	}
    919 
    920 	snapper_init(sc, sc->sc_node);
    921 
    922 	audio_attach_mi(&snapper_hw_if, sc, sc->sc_dev);
    923 }
    924 
    925 static int
    926 snapper_intr(void *v)
    927 {
    928 	struct snapper_softc *sc;
    929 	struct dbdma_command *cmd;
    930 	int count;
    931 	int status;
    932 
    933 	sc = v;
    934 	mutex_spin_enter(&sc->sc_intr_lock);
    935 	cmd = sc->sc_odmacmd;
    936 	count = sc->sc_opages;
    937 	/* Fill used buffer(s). */
    938 	while (count-- > 0) {
    939 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    940 			status = in16rb(&cmd->d_status);
    941 			cmd->d_status = 0;
    942 			if (status)	/* status == 0x8400 */
    943 				if (sc->sc_ointr)
    944 					(*sc->sc_ointr)(sc->sc_oarg);
    945 		}
    946 		cmd++;
    947 	}
    948 
    949 	cmd = sc->sc_idmacmd;
    950 	count = sc->sc_ipages;
    951 	while (count-- > 0) {
    952 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    953 			status = in16rb(&cmd->d_status);
    954 			cmd->d_status = 0;
    955 			if (status)	/* status == 0x8400 */
    956 				if (sc->sc_iintr)
    957 					(*sc->sc_iintr)(sc->sc_iarg);
    958 		}
    959 		cmd++;
    960 	}
    961 	mutex_spin_exit(&sc->sc_intr_lock);
    962 
    963 	return 1;
    964 }
    965 
    966 
    967 static int
    968 snapper_query_format(void *h, audio_format_query_t *afp)
    969 {
    970 	struct snapper_softc *sc = h;
    971 
    972 	switch (sc->sc_mode) {
    973 		case SNAPPER_IS_TAS3001:
    974 			return audio_query_format(tumbler_formats,
    975 			    TUMBLER_NFORMATS, afp);
    976 		case SNAPPER_SWVOL:
    977 		case SNAPPER_IS_TAS3004:
    978 			return audio_query_format(snapper_formats,
    979 			    SNAPPER_NFORMATS, afp);
    980 		case SNAPPER_IS_PCM3052:
    981 			return audio_query_format(onyx_formats,
    982 			    ONYX_NFORMATS, afp);
    983 	}
    984 	return -1;
    985 }
    986 
    987 static int
    988 snapper_set_format(void *h, int setmode,
    989 		   const audio_params_t *play, const audio_params_t *rec,
    990 		   audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    991 {
    992 	struct snapper_softc *sc;
    993 
    994 	sc = h;
    995 
    996 	/* *play and *rec are the identical because !AUDIO_PROP_INDEPENDENT. */
    997 
    998 	if (sc->sc_mode == SNAPPER_SWVOL) {
    999 		pfil->codec = snapper_volume;
   1000 		pfil->context = sc;
   1001 		rfil->codec = snapper_volume;
   1002 		rfil->context = sc;
   1003 	} else if (sc->sc_mode == 0 && play->channels == 2) {
   1004 		/* Fix phase problems on TAS3004.  */
   1005 		pfil->codec = snapper_fixphase;
   1006 		pfil->context = sc;
   1007 		rfil->codec = snapper_fixphase;
   1008 		rfil->context = sc;
   1009 	}
   1010 
   1011 	/* Set the speed. */
   1012 	sc->sc_rate = play->sample_rate;
   1013 	sc->sc_bitspersample = play->precision;
   1014 	return 0;
   1015 }
   1016 
   1017 static int
   1018 snapper_commit_settings(void *h)
   1019 {
   1020 	struct snapper_softc *sc;
   1021 
   1022 	DPRINTF("commit_settings\n");
   1023 	sc = h;
   1024 
   1025 	return snapper_set_rate(sc);
   1026 }
   1027 
   1028 static int
   1029 snapper_round_blocksize(void *h, int size, int mode,
   1030 			const audio_params_t *param)
   1031 {
   1032 
   1033 	if (size < (3 * NBPG))
   1034 		size = (3 * NBPG);
   1035 	return size & ~PGOFSET;
   1036 }
   1037 
   1038 static int
   1039 snapper_halt_output(void *h)
   1040 {
   1041 	struct snapper_softc *sc;
   1042 
   1043 	sc = h;
   1044 	dbdma_stop(sc->sc_odma);
   1045 	dbdma_reset(sc->sc_odma);
   1046 	sc->sc_ointr = NULL;
   1047 	sc->sc_rval = 0;
   1048 	return 0;
   1049 }
   1050 
   1051 static int
   1052 snapper_halt_input(void *h)
   1053 {
   1054 	struct snapper_softc *sc;
   1055 
   1056 	sc = h;
   1057 	dbdma_stop(sc->sc_idma);
   1058 	dbdma_reset(sc->sc_idma);
   1059 	sc->sc_iintr = NULL;
   1060 	sc->sc_rval = 0;
   1061 	return 0;
   1062 }
   1063 
   1064 static int
   1065 snapper_getdev(void *h, struct audio_device *retp)
   1066 {
   1067 
   1068 	*retp = snapper_device;
   1069 	return 0;
   1070 }
   1071 
   1072 enum {
   1073 	SNAPPER_MONITOR_CLASS,
   1074 	SNAPPER_OUTPUT_CLASS,
   1075 	SNAPPER_RECORD_CLASS,
   1076 	SNAPPER_OUTPUT_SELECT,
   1077 	SNAPPER_VOL_OUTPUT,
   1078 	SNAPPER_DIGI1,
   1079 	SNAPPER_DIGI2,
   1080 	SNAPPER_VOL_INPUT,
   1081 	SNAPPER_TREBLE,
   1082 	SNAPPER_BASS,
   1083 	/* From this point, unsupported by the TAS 3001 */
   1084 	SNAPPER_ANALOG,
   1085 	SNAPPER_INPUT_SELECT,
   1086 	SNAPPER_ENUM_LAST
   1087 };
   1088 
   1089 static int
   1090 snapper_set_port(void *h, mixer_ctrl_t *mc)
   1091 {
   1092 	struct snapper_softc *sc;
   1093 	int l, r;
   1094 	u_char data;
   1095 
   1096 	DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
   1097 	sc = h;
   1098 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1099 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1100 
   1101 	switch (mc->dev) {
   1102 	case SNAPPER_OUTPUT_SELECT:
   1103 		/* No change necessary? */
   1104 		if (mc->un.mask == sc->sc_output_mask)
   1105 			return 0;
   1106 
   1107 		snapper_mute_speaker(sc, 1);
   1108 		snapper_mute_headphone(sc, 1);
   1109 		snapper_mute_lineout(sc, 1);
   1110 		if (mc->un.mask & 1 << 0)
   1111 			snapper_mute_speaker(sc, 0);
   1112 		if (mc->un.mask & 1 << 1)
   1113 			snapper_mute_headphone(sc, 0);
   1114 		if (mc->un.mask & 1 << 2)
   1115 			snapper_mute_lineout(sc, 0);
   1116 
   1117 		sc->sc_output_mask = mc->un.mask;
   1118 		return 0;
   1119 
   1120 	case SNAPPER_VOL_OUTPUT:
   1121 		snapper_set_volume(sc, l, r);
   1122 		return 0;
   1123 
   1124 	case SNAPPER_INPUT_SELECT:
   1125 		if (sc->sc_mode != 0)
   1126 			return ENXIO;
   1127 
   1128 		/* no change necessary? */
   1129 		if (mc->un.mask == sc->sc_record_source)
   1130 			return 0;
   1131 		switch (mc->un.mask) {
   1132 		case 1 << 0: /* microphone */
   1133 			/* Select right channel of B input */
   1134 			data = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B;
   1135 			tas3004_write(sc, DEQ_ACR, &data);
   1136 			break;
   1137 		case 1 << 1: /* line in */
   1138 			/* Select both channels of A input */
   1139 			data = 0;
   1140 			tas3004_write(sc, DEQ_ACR, &data);
   1141 			break;
   1142 		default: /* invalid argument */
   1143 			return EINVAL;
   1144 		}
   1145 		sc->sc_record_source = mc->un.mask;
   1146 		return 0;
   1147 
   1148 	case SNAPPER_VOL_INPUT:
   1149 		/* XXX TO BE DONE */
   1150 		return 0;
   1151 
   1152 	case SNAPPER_BASS:
   1153 		if (sc->sc_mode == SNAPPER_SWVOL)
   1154 			return ENXIO;
   1155 		snapper_set_bass(sc, l);
   1156 		return 0;
   1157 	case SNAPPER_TREBLE:
   1158 		if (sc->sc_mode == SNAPPER_SWVOL)
   1159 			return ENXIO;
   1160 		snapper_set_treble(sc, l);
   1161 		return 0;
   1162 	case SNAPPER_DIGI1:
   1163 		if (sc->sc_mode == SNAPPER_SWVOL)
   1164 			return ENXIO;
   1165 
   1166 		sc->mixer[0] = l;
   1167 		sc->mixer[3] = r;
   1168 		snapper_write_mixers(sc);
   1169 		return 0;
   1170 	case SNAPPER_DIGI2:
   1171 		if (sc->sc_mode == SNAPPER_SWVOL)
   1172 			return ENXIO;
   1173 
   1174 		if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1175 			sc->mixer[3] = l;
   1176 		else {
   1177 			sc->mixer[1] = l;
   1178 			sc->mixer[4] = r;
   1179 		}
   1180 		snapper_write_mixers(sc);
   1181 		return 0;
   1182 	case SNAPPER_ANALOG:
   1183 		if (sc->sc_mode != 0)
   1184 			return ENXIO;
   1185 
   1186 		sc->mixer[2] = l;
   1187 		sc->mixer[5] = r;
   1188 		snapper_write_mixers(sc);
   1189 		return 0;
   1190 	}
   1191 	return ENXIO;
   1192 }
   1193 
   1194 static int
   1195 snapper_get_port(void *h, mixer_ctrl_t *mc)
   1196 {
   1197 	struct snapper_softc *sc;
   1198 
   1199 	DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
   1200 	sc = h;
   1201 	switch (mc->dev) {
   1202 	case SNAPPER_OUTPUT_SELECT:
   1203 		mc->un.mask = sc->sc_output_mask;
   1204 		return 0;
   1205 
   1206 	case SNAPPER_VOL_OUTPUT:
   1207 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
   1208 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
   1209 		return 0;
   1210 
   1211 	case SNAPPER_INPUT_SELECT:
   1212 		if (sc->sc_mode != 0)
   1213 			return ENXIO;
   1214 
   1215 		mc->un.mask = sc->sc_record_source;
   1216 		return 0;
   1217 
   1218 	case SNAPPER_VOL_INPUT:
   1219 		/* XXX TO BE DONE */
   1220 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
   1221 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
   1222 		return 0;
   1223 
   1224 	case SNAPPER_TREBLE:
   1225 		if (sc->sc_mode == SNAPPER_SWVOL)
   1226 			return ENXIO;
   1227 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
   1228 		return 0;
   1229 	case SNAPPER_BASS:
   1230 		if (sc->sc_mode == SNAPPER_SWVOL)
   1231 			return ENXIO;
   1232 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
   1233 		return 0;
   1234 
   1235 	case SNAPPER_DIGI1:
   1236 		if (sc->sc_mode == SNAPPER_SWVOL)
   1237 			return ENXIO;
   1238 
   1239 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0];
   1240 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3];
   1241 		return 0;
   1242 	case SNAPPER_DIGI2:
   1243 		if (sc->sc_mode == SNAPPER_SWVOL)
   1244 			return ENXIO;
   1245 
   1246 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1];
   1247 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4];
   1248 		return 0;
   1249 	case SNAPPER_ANALOG:
   1250 		if (sc->sc_mode != 0)
   1251 			return ENXIO;
   1252 
   1253 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2];
   1254 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5];
   1255 		return 0;
   1256 	default:
   1257 		return ENXIO;
   1258 	}
   1259 
   1260 	return 0;
   1261 }
   1262 
   1263 static int
   1264 snapper_query_devinfo(void *h, mixer_devinfo_t *dip)
   1265 {
   1266 	struct snapper_softc *sc = h;
   1267 
   1268 	switch (dip->index) {
   1269 
   1270 	case SNAPPER_OUTPUT_SELECT:
   1271 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1272 		strcpy(dip->label.name, AudioNoutput);
   1273 		dip->type = AUDIO_MIXER_SET;
   1274 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1275 		dip->un.s.num_mem = 3;
   1276 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
   1277 		dip->un.s.member[0].mask = 1 << 0;
   1278 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
   1279 		dip->un.s.member[1].mask = 1 << 1;
   1280 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1281 		dip->un.s.member[2].mask = 1 << 2;
   1282 		return 0;
   1283 
   1284 	case SNAPPER_VOL_OUTPUT:
   1285 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1286 		strcpy(dip->label.name, AudioNmaster);
   1287 		dip->type = AUDIO_MIXER_VALUE;
   1288 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1289 		dip->un.v.num_channels = 2;
   1290 		dip->un.v.delta = 16;
   1291 		strcpy(dip->un.v.units.name, AudioNvolume);
   1292 		return 0;
   1293 
   1294 	case SNAPPER_INPUT_SELECT:
   1295 		if (sc->sc_mode != 0)
   1296 			return ENXIO;
   1297 
   1298 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1299 		strcpy(dip->label.name, AudioNsource);
   1300 		dip->type = AUDIO_MIXER_SET;
   1301 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1302 		dip->un.s.num_mem = 2;
   1303 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1304 		dip->un.s.member[0].mask = 1 << 0;
   1305 		strcpy(dip->un.s.member[1].label.name, AudioNline);
   1306 		dip->un.s.member[1].mask = 1 << 1;
   1307 		return 0;
   1308 
   1309 	case SNAPPER_VOL_INPUT:
   1310 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1311 		strcpy(dip->label.name, AudioNrecord);
   1312 		dip->type = AUDIO_MIXER_VALUE;
   1313 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1314 		dip->un.v.num_channels = 2;
   1315 		strcpy(dip->un.v.units.name, AudioNvolume);
   1316 		return 0;
   1317 
   1318 	case SNAPPER_MONITOR_CLASS:
   1319 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1320 		strcpy(dip->label.name, AudioCmonitor);
   1321 		dip->type = AUDIO_MIXER_CLASS;
   1322 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1323 		return 0;
   1324 
   1325 	case SNAPPER_OUTPUT_CLASS:
   1326 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1327 		strcpy(dip->label.name, AudioCoutputs);
   1328 		dip->type = AUDIO_MIXER_CLASS;
   1329 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1330 		return 0;
   1331 
   1332 	case SNAPPER_RECORD_CLASS:
   1333 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1334 		strcpy(dip->label.name, AudioCrecord);
   1335 		dip->type = AUDIO_MIXER_CLASS;
   1336 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1337 		return 0;
   1338 
   1339 	case SNAPPER_TREBLE:
   1340 		if (sc->sc_mode == SNAPPER_SWVOL)
   1341 			return ENXIO;
   1342 
   1343 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1344 		strcpy(dip->label.name, AudioNtreble);
   1345 		dip->type = AUDIO_MIXER_VALUE;
   1346 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1347 		dip->un.v.num_channels = 1;
   1348 		return 0;
   1349 
   1350 	case SNAPPER_BASS:
   1351 		if (sc->sc_mode == SNAPPER_SWVOL)
   1352 			return ENXIO;
   1353 
   1354 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1355 		strcpy(dip->label.name, AudioNbass);
   1356 		dip->type = AUDIO_MIXER_VALUE;
   1357 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1358 		dip->un.v.num_channels = 1;
   1359 		return 0;
   1360 
   1361 	case SNAPPER_DIGI1:
   1362 		if (sc->sc_mode == SNAPPER_SWVOL)
   1363 			return ENXIO;
   1364 
   1365 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1366 		strcpy(dip->label.name, AudioNdac);
   1367 		dip->type = AUDIO_MIXER_VALUE;
   1368 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1369 		dip->un.v.num_channels =
   1370 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1371 		return 0;
   1372 	case SNAPPER_DIGI2:
   1373 		if (sc->sc_mode == SNAPPER_SWVOL)
   1374 			return ENXIO;
   1375 
   1376 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1377 		strcpy(dip->label.name, AudioNline);
   1378 		dip->type = AUDIO_MIXER_VALUE;
   1379 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1380 		dip->un.v.num_channels =
   1381 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1382 		return 0;
   1383 	case SNAPPER_ANALOG:
   1384 		if (sc->sc_mode != 0)
   1385 			return ENXIO;
   1386 
   1387 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1388 		strcpy(dip->label.name, AudioNmicrophone);
   1389 		dip->type = AUDIO_MIXER_VALUE;
   1390 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1391 		dip->un.v.num_channels = 2;
   1392 		return 0;
   1393 	}
   1394 
   1395 	return ENXIO;
   1396 }
   1397 
   1398 static size_t
   1399 snapper_round_buffersize(void *h, int dir, size_t size)
   1400 {
   1401 
   1402 	if (size > 65536)
   1403 		size = 65536;
   1404 	return size;
   1405 }
   1406 
   1407 static int
   1408 snapper_get_props(void *h)
   1409 {
   1410 
   1411 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
   1412 	    AUDIO_PROP_FULLDUPLEX;
   1413 }
   1414 
   1415 static int
   1416 snapper_trigger_output(void *h, void *start, void *end, int bsize,
   1417 		       void (*intr)(void *), void *arg,
   1418 		       const audio_params_t *param)
   1419 {
   1420 	struct snapper_softc *sc;
   1421 	struct dbdma_command *cmd;
   1422 	vaddr_t va;
   1423 	int i, len, intmode;
   1424 
   1425 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1426 	sc = h;
   1427 
   1428 	cmd = sc->sc_odmacmd;
   1429 	sc->sc_ointr = intr;
   1430 	sc->sc_oarg = arg;
   1431 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
   1432 
   1433 #ifdef DIAGNOSTIC
   1434 	if (sc->sc_opages > SNAPPER_MAXPAGES)
   1435 		panic("snapper_trigger_output");
   1436 #endif
   1437 
   1438 	va = (vaddr_t)start;
   1439 	len = 0;
   1440 	for (i = sc->sc_opages; i > 0; i--) {
   1441 		len += NBPG;
   1442 		if (len < bsize)
   1443 			intmode = 0;
   1444 		else {
   1445 			len = 0;
   1446 			intmode = DBDMA_INT_ALWAYS;
   1447 		}
   1448 
   1449 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
   1450 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1451 		cmd++;
   1452 		va += NBPG;
   1453 	}
   1454 
   1455 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1456 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1457 	    DBDMA_BRANCH_ALWAYS);
   1458 
   1459 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1460 
   1461 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1462 
   1463 	return 0;
   1464 }
   1465 
   1466 static int
   1467 snapper_trigger_input(void *h, void *start, void *end, int bsize,
   1468 		      void (*intr)(void *), void *arg,
   1469 		      const audio_params_t *param)
   1470 {
   1471 	struct snapper_softc *sc;
   1472 	struct dbdma_command *cmd;
   1473 	vaddr_t va;
   1474 	int i, len, intmode;
   1475 
   1476 	DPRINTF("trigger_input %p %p 0x%x\n", start, end, bsize);
   1477 	sc = h;
   1478 
   1479 	cmd = sc->sc_idmacmd;
   1480 	sc->sc_iintr = intr;
   1481 	sc->sc_iarg = arg;
   1482 	sc->sc_ipages = ((char *)end - (char *)start) / NBPG;
   1483 
   1484 #ifdef DIAGNOSTIC
   1485 	if (sc->sc_ipages > SNAPPER_MAXPAGES)
   1486 		panic("snapper_trigger_input");
   1487 #endif
   1488 
   1489 	va = (vaddr_t)start;
   1490 	len = 0;
   1491 	for (i = sc->sc_ipages; i > 0; i--) {
   1492 		len += NBPG;
   1493 		if (len < bsize)
   1494 			intmode = 0;
   1495 		else {
   1496 			len = 0;
   1497 			intmode = DBDMA_INT_ALWAYS;
   1498 		}
   1499 
   1500 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_MORE, 0, NBPG, vtophys(va),
   1501 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1502 		cmd++;
   1503 		va += NBPG;
   1504 	}
   1505 
   1506 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1507 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1508 	    DBDMA_BRANCH_ALWAYS);
   1509 
   1510 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_idmacmd));
   1511 
   1512 	dbdma_start(sc->sc_idma, sc->sc_idmacmd);
   1513 
   1514 	return 0;
   1515 }
   1516 
   1517 static void
   1518 snapper_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
   1519 {
   1520        struct snapper_softc *sc = opaque;
   1521 
   1522        *intr = &sc->sc_intr_lock;
   1523        *thread = &sc->sc_lock;
   1524 }
   1525 
   1526 static void
   1527 snapper_set_volume(struct snapper_softc *sc, u_int left, u_int right)
   1528 {
   1529 	u_char regs[6];
   1530 	int l, r;
   1531 
   1532 	left = uimin(255, left);
   1533 	right = uimin(255, right);
   1534 
   1535 	if (sc->sc_mode == SNAPPER_SWVOL) {
   1536 		sc->sc_swvol_l = left;
   1537 		sc->sc_swvol_r = right;
   1538 	} else {
   1539 		/*
   1540 		 * for some insane reason the gain table for master volume and the
   1541 		 * mixer channels is almost identical - just shifted by 4 bits
   1542 		 * so we use the mixer_gain table and bit-twiddle it...
   1543 		 */
   1544 		l = 177 - (left * 178 / 256);
   1545 		regs[0] =  (snapper_mixer_gain[l][0] >> 4);
   1546 		regs[1] = ((snapper_mixer_gain[l][0] & 0x0f) << 4) |
   1547 			   (snapper_mixer_gain[l][1] >> 4);
   1548 		regs[2] = ((snapper_mixer_gain[l][1] & 0x0f) << 4) |
   1549 			   (snapper_mixer_gain[l][2] >> 4);
   1550 
   1551 		r = 177 - (right * 178 / 256);
   1552 		regs[3] =  (snapper_mixer_gain[r][0] >> 4);
   1553 		regs[4] = ((snapper_mixer_gain[r][0] & 0x0f) << 4) |
   1554 			   (snapper_mixer_gain[r][1] >> 4);
   1555 		regs[5] = ((snapper_mixer_gain[r][1] & 0x0f) << 4) |
   1556 			   (snapper_mixer_gain[r][2] >> 4);
   1557 
   1558 		tas3004_write(sc, DEQ_VOLUME, regs);
   1559 
   1560 		DPRINTF("%d %02x %02x %02x : %d %02x %02x %02x\n", l, regs[0],
   1561 		    regs[1], regs[2], r, regs[3], regs[4], regs[5]);
   1562 	}
   1563 
   1564 	sc->sc_vol_l = left;
   1565 	sc->sc_vol_r = right;
   1566 }
   1567 
   1568 static void
   1569 snapper_set_basstreble(struct snapper_softc *sc, u_int val, u_int mode)
   1570 {
   1571 	int i = val & 0xFF;
   1572 	uint8_t reg;
   1573 
   1574 	/*
   1575 	 * Make 128 match the 0 dB point
   1576 	 */
   1577 	i = (i - (128 - (SNAPPER_BASSTAB_0DB << 2))) >> 2;
   1578 	if (i < 0)
   1579 		i = 0;
   1580 	else if (i >= sizeof(snapper_basstab))
   1581 		i = sizeof(snapper_basstab) - 1;
   1582 	reg = snapper_basstab[i];
   1583 
   1584 	if (sc->sc_mode == SNAPPER_IS_TAS3001 &&
   1585 	    mode == DEQ_BASS) {
   1586 	    /*
   1587 	     * XXX -- The TAS3001 bass table is different
   1588 	     *        than the other tables.
   1589 	     */
   1590 	    reg = (reg >> 1) + 5; // map 0x72 -> 0x3E (0 dB)
   1591 	}
   1592 
   1593 	tas3004_write(sc, mode, &reg);
   1594 }
   1595 
   1596 static void
   1597 snapper_set_treble(struct snapper_softc *sc, u_int val)
   1598 {
   1599 	if (sc->sc_treble != (u_char)val) {
   1600 		sc->sc_treble = val;
   1601 		snapper_set_basstreble(sc, val, DEQ_TREBLE);
   1602 	}
   1603 }
   1604 
   1605 static void
   1606 snapper_set_bass(struct snapper_softc *sc, u_int val)
   1607 {
   1608 	if (sc->sc_bass != (u_char)val) {
   1609 		sc->sc_bass = val;
   1610 		snapper_set_basstreble(sc, val, DEQ_BASS);
   1611 	}
   1612 }
   1613 
   1614 
   1615 /*
   1616  * In the mixer gain setting, make 128 correspond to
   1617  * the 0dB value from the table.
   1618  * Note that the table values are complemented.
   1619  */
   1620 #define SNAPPER_MIXER_GAIN_SIZE	(sizeof(snapper_mixer_gain) / \
   1621                                	 sizeof(snapper_mixer_gain[0]))
   1622 #define NORMALIZE(i)	((~(i) & 0xff) - ((~128 & 0xff) - SNAPPER_MIXER_GAIN_0DB))
   1623 #define ADJUST(v, i)	do { \
   1624                 		(v) = NORMALIZE(i);\
   1625 				if ((v) < 0) \
   1626 					(v) = 0; \
   1627 				else if ((v) >= SNAPPER_MIXER_GAIN_SIZE) \
   1628 					(v) = SNAPPER_MIXER_GAIN_SIZE - 1; \
   1629 				\
   1630 			} while (0)
   1631 static void
   1632 snapper_write_mixers(struct snapper_softc *sc)
   1633 {
   1634 	uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
   1635 	int i;
   1636 if (sc->sc_mode > 1) return;
   1637 	/* Left channel of SDIN1 */
   1638 	ADJUST(i, sc->mixer[0]);
   1639 	regs[0] = snapper_mixer_gain[i][0];
   1640 	regs[1] = snapper_mixer_gain[i][1];
   1641 	regs[2] = snapper_mixer_gain[i][2];
   1642 
   1643 	/* Left channel of SDIN2 */
   1644 	ADJUST(i, sc->mixer[1]);
   1645 	regs[3] = snapper_mixer_gain[i][0];
   1646 	regs[4] = snapper_mixer_gain[i][1];
   1647 	regs[5] = snapper_mixer_gain[i][2];
   1648 
   1649 	/* Left channel of analog input */
   1650 	ADJUST(i, sc->mixer[2]);
   1651 	regs[6] = snapper_mixer_gain[i][0];
   1652 	regs[7] = snapper_mixer_gain[i][1];
   1653 	regs[8] = snapper_mixer_gain[i][2];
   1654 
   1655 	tas3004_write(sc, DEQ_MIXER_L, regs);
   1656 
   1657 	/* Right channel of SDIN1 */
   1658 	ADJUST(i, sc->mixer[3]);
   1659 	regs[0] = snapper_mixer_gain[i][0];
   1660 	regs[1] = snapper_mixer_gain[i][1];
   1661 	regs[2] = snapper_mixer_gain[i][2];
   1662 
   1663 	/* Right channel of SDIN2 */
   1664 	ADJUST(i, sc->mixer[4]);
   1665 	regs[3] = snapper_mixer_gain[i][0];
   1666 	regs[4] = snapper_mixer_gain[i][1];
   1667 	regs[5] = snapper_mixer_gain[i][2];
   1668 
   1669 	/* Right channel of analog input */
   1670 	ADJUST(i, sc->mixer[5]);
   1671 	regs[6] = snapper_mixer_gain[i][0];
   1672 	regs[7] = snapper_mixer_gain[i][1];
   1673 	regs[8] = snapper_mixer_gain[i][2];
   1674 
   1675 	tas3004_write(sc, DEQ_MIXER_R, regs);
   1676 }
   1677 
   1678 #define CLKSRC_49MHz	0x80000000	/* Use 49152000Hz Osc. */
   1679 #define CLKSRC_45MHz	0x40000000	/* Use 45158400Hz Osc. */
   1680 #define CLKSRC_18MHz	0x00000000	/* Use 18432000Hz Osc. */
   1681 #define MCLK_DIV	0x1f000000	/* MCLK = SRC / DIV */
   1682 #define  MCLK_DIV1	0x14000000	/*  MCLK = SRC */
   1683 #define  MCLK_DIV3	0x13000000	/*  MCLK = SRC / 3 */
   1684 #define  MCLK_DIV5	0x12000000	/*  MCLK = SRC / 5 */
   1685 #define SCLK_DIV	0x00f00000	/* SCLK = MCLK / DIV */
   1686 #define  SCLK_DIV1	0x00800000
   1687 #define  SCLK_DIV3	0x00900000
   1688 #define SCLK_MASTER	0x00080000	/* Master mode */
   1689 #define SCLK_SLAVE	0x00000000	/* Slave mode */
   1690 #define SERIAL_FORMAT	0x00070000
   1691 #define  SERIAL_SONY	0x00000000
   1692 #define  SERIAL_64x	0x00010000
   1693 #define  SERIAL_32x	0x00020000
   1694 #define  SERIAL_DAV	0x00040000
   1695 #define  SERIAL_SILICON	0x00050000
   1696 
   1697 /*
   1698  * rate = fs = LRCLK
   1699  * SCLK = 64*LRCLK (I2S)
   1700  * MCLK = 256fs (typ. -- changeable)
   1701  *
   1702  * MCLK = clksrc / mdiv
   1703  * SCLK = MCLK / sdiv
   1704  * rate = SCLK / 64    ( = LRCLK = fs)
   1705  */
   1706 
   1707 int
   1708 snapper_set_rate(struct snapper_softc *sc)
   1709 {
   1710 	u_int reg = 0, x;
   1711 	u_int rate = sc->sc_rate;
   1712 	uint32_t wordsize, ows;
   1713 	int MCLK;
   1714 	int clksrc, mdiv, sdiv;
   1715 	int mclk_fs;
   1716 	int timo;
   1717 	uint8_t mcr1;
   1718 
   1719 	switch (rate) {
   1720 	case 44100:
   1721 		clksrc = 45158400;		/* 45MHz */
   1722 		reg = CLKSRC_45MHz;
   1723 		mclk_fs = 256;
   1724 		break;
   1725 
   1726 	case 32000:
   1727 	case 48000:
   1728 	case 96000:
   1729 		clksrc = 49152000;		/* 49MHz */
   1730 		reg = CLKSRC_49MHz;
   1731 		mclk_fs = 256;
   1732 		break;
   1733 
   1734 	default:
   1735 		DPRINTF("snapper_set_rate: invalid rate %u\n", rate);
   1736 		return EINVAL;
   1737 	}
   1738 
   1739 	MCLK = rate * mclk_fs;
   1740 	mdiv = clksrc / MCLK;			/* 4 */
   1741 	sdiv = mclk_fs / 64;			/* 4 */
   1742 
   1743 	switch (mdiv) {
   1744 	case 1:
   1745 		reg |= MCLK_DIV1;
   1746 		break;
   1747 	case 3:
   1748 		reg |= MCLK_DIV3;
   1749 		break;
   1750 	case 5:
   1751 		reg |= MCLK_DIV5;
   1752 		break;
   1753 	default:
   1754 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
   1755 		break;
   1756 	}
   1757 
   1758 	switch (sdiv) {
   1759 	case 1:
   1760 		reg |= SCLK_DIV1;
   1761 		break;
   1762 	case 3:
   1763 		reg |= SCLK_DIV3;
   1764 		break;
   1765 	default:
   1766 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
   1767 		break;
   1768 	}
   1769 
   1770 	reg |= SCLK_MASTER;	/* XXX master mode */
   1771 
   1772 	reg |= SERIAL_64x;
   1773 
   1774 	/* stereo input and output */
   1775 
   1776 	DPRINTF("precision: %d\n", sc->sc_bitspersample);
   1777 	switch(sc->sc_bitspersample) {
   1778 		case 16:
   1779 			wordsize = INPUT_STEREO | INPUT_16BIT |
   1780 			           OUTPUT_STEREO | OUTPUT_16BIT;
   1781 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16;
   1782 			break;
   1783 		case 24:
   1784 			wordsize = INPUT_STEREO | INPUT_24BIT |
   1785 			           OUTPUT_STEREO | OUTPUT_24BIT;
   1786 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_24;
   1787 			break;
   1788 		default:
   1789 			printf("%s: unsupported sample size %d\n",
   1790 			    device_xname(sc->sc_dev), sc->sc_bitspersample);
   1791 			return EINVAL;
   1792 	}
   1793 
   1794 	if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1795 		mcr1 |= DEQ_MCR1_ISM_I2S;
   1796 
   1797 	ows = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE);
   1798 
   1799 	DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
   1800 	    ows, wordsize);
   1801 	if (ows != wordsize) {
   1802 		bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE,
   1803 		    wordsize);
   1804 		if (sc->sc_mode != SNAPPER_SWVOL)
   1805 			tas3004_write(sc, DEQ_MCR1, &mcr1);
   1806 	}
   1807 
   1808 	x = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT);
   1809 	if (x == reg)
   1810 		return 0;        /* No change; do nothing. */
   1811 
   1812 	DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
   1813 	    bus_space_read_4(sc->sc_tag, sc->sc_bsh, + I2S_FORMAT), reg);
   1814 
   1815 	/* Clear CLKSTOPPEND. */
   1816 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND);
   1817 
   1818 	x = obio_read_4(KEYLARGO_FCR1);                /* FCR */
   1819 	x &= ~I2S0CLKEN;                /* XXX I2S0 */
   1820 	obio_write_4(KEYLARGO_FCR1, x);
   1821 
   1822 	/* Wait until clock is stopped. */
   1823 	for (timo = 1000; timo > 0; timo--) {
   1824 		if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) &
   1825 		    I2S_INT_CLKSTOPPEND)
   1826 			goto done;
   1827 		delay(1);
   1828 	}
   1829 	DPRINTF("snapper_set_rate: timeout\n");
   1830 done:
   1831 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
   1832 
   1833 	x = obio_read_4(KEYLARGO_FCR1);
   1834 	x |= I2S0CLKEN;
   1835 	obio_write_4(KEYLARGO_FCR1, x);
   1836 
   1837 	return 0;
   1838 }
   1839 
   1840 const struct tas3004_reg tas3004_initdata = {
   1841 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16 },	/* MCR1 */
   1842 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
   1843 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
   1844 	{ 0x72 },						/* TREBLE */
   1845 	{ 0x72 },						/* BASS */
   1846 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
   1847 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
   1848 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1849 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1850 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1851 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1852 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1853 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1854 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1855 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1856 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1857 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1858 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1859 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1860 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1861 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1862 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1863 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1864 	{ 0, 0, 0 },						/* LLB_GAIN */
   1865 	{ 0, 0, 0 },						/* RLB_GAIN */
   1866 	{ 0 },							/* ACR - line in */
   1867 	{ 2 }							/* MCR2 - AllPass mode since we don't use the equalizer anyway */
   1868 };
   1869 
   1870 const char tas3004_regsize[] = {
   1871 	0,					/* 0x00 */
   1872 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
   1873 	sizeof tas3004_initdata.DRC,		/* 0x02 */
   1874 	0,					/* 0x03 */
   1875 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
   1876 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
   1877 	sizeof tas3004_initdata.BASS,		/* 0x06 */
   1878 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
   1879 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
   1880 	0,					/* 0x09 */
   1881 	sizeof tas3004_initdata.LB0,		/* 0x0a */
   1882 	sizeof tas3004_initdata.LB1,		/* 0x0b */
   1883 	sizeof tas3004_initdata.LB2,		/* 0x0c */
   1884 	sizeof tas3004_initdata.LB3,		/* 0x0d */
   1885 	sizeof tas3004_initdata.LB4,		/* 0x0e */
   1886 	sizeof tas3004_initdata.LB5,		/* 0x0f */
   1887 	sizeof tas3004_initdata.LB6,		/* 0x10 */
   1888 	0,					/* 0x11 */
   1889 	0,					/* 0x12 */
   1890 	sizeof tas3004_initdata.RB0,		/* 0x13 */
   1891 	sizeof tas3004_initdata.RB1,		/* 0x14 */
   1892 	sizeof tas3004_initdata.RB2,		/* 0x15 */
   1893 	sizeof tas3004_initdata.RB3,		/* 0x16 */
   1894 	sizeof tas3004_initdata.RB4,		/* 0x17 */
   1895 	sizeof tas3004_initdata.RB5,		/* 0x18 */
   1896 	sizeof tas3004_initdata.RB6,		/* 0x19 */
   1897 	0,0,0,0, 0,0,
   1898 	0,					/* 0x20 */
   1899 	sizeof tas3004_initdata.LLB,		/* 0x21 */
   1900 	sizeof tas3004_initdata.RLB,		/* 0x22 */
   1901 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
   1902 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
   1903 	0,0,0,0, 0,0,0,0, 0,0,0,
   1904 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   1905 	sizeof tas3004_initdata.ACR,		/* 0x40 */
   1906 	0,					/* 0x41 */
   1907 	0,					/* 0x42 */
   1908 	sizeof tas3004_initdata.MCR2		/* 0x43 */
   1909 };
   1910 
   1911 static int
   1912 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
   1913 {
   1914 	int size;
   1915 	static char regblock[sizeof(struct tas3004_reg)+1];
   1916 
   1917 	if (sc->sc_i2c == NULL)
   1918 		return 0;
   1919 
   1920 	KASSERT(reg < sizeof tas3004_regsize);
   1921 	size = tas3004_regsize[reg];
   1922 	KASSERT(size > 0);
   1923 
   1924 	DPRINTF("reg: %x, %d %d\n", reg, size, ((const char*)data)[0]);
   1925 
   1926 	regblock[0] = reg;
   1927 	memcpy(&regblock[1], data, size);
   1928 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
   1929 		if (reg == DEQ_MIXER_L || reg == DEQ_MIXER_R)
   1930 			size = 3;
   1931 		else if (reg == DEQ_DRC || reg == DEQ_ACR ||
   1932 			 reg == DEQ_MCR2) {
   1933 			/* these registers are not available on TAS3001 */
   1934 			return 0;
   1935 		}
   1936 	}
   1937 	iic_acquire_bus(sc->sc_i2c, 0);
   1938 	iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, regblock, size + 1,
   1939 	    NULL, 0, 0);
   1940 	iic_release_bus(sc->sc_i2c, 0);
   1941 
   1942 	return 0;
   1943 }
   1944 
   1945 static int
   1946 gpio_read(bus_size_t addr)
   1947 {
   1948 
   1949 	if (obio_read_1(addr) & GPIO_DATA)
   1950 		return 1;
   1951 	return 0;
   1952 }
   1953 
   1954 static void
   1955 gpio_write(bus_size_t addr, int val)
   1956 {
   1957 	uint8_t data;
   1958 
   1959 	data = GPIO_DDR_OUTPUT;
   1960 	if (val)
   1961 		data |= GPIO_DATA;
   1962 	obio_write_1(addr, data);
   1963 }
   1964 
   1965 #define headphone_active 0	/* XXX OF */
   1966 #define lineout_active 0	/* XXX OF */
   1967 #define amp_active 0		/* XXX OF */
   1968 
   1969 static void
   1970 snapper_mute_speaker(struct snapper_softc *sc, int mute)
   1971 {
   1972 	int x;
   1973 
   1974 	if (amp_mute) {
   1975 		DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
   1976 
   1977 		if (mute)
   1978 			x = amp_active;		/* mute */
   1979 		else
   1980 			x = !amp_active;	/* unmute */
   1981 		if (x != gpio_read(amp_mute))
   1982 			gpio_write(amp_mute, x);
   1983 
   1984 		DPRINTF("%d\n", gpio_read(amp_mute));
   1985 	}
   1986 }
   1987 
   1988 static void
   1989 snapper_mute_headphone(struct snapper_softc *sc, int mute)
   1990 {
   1991 	u_int x;
   1992 
   1993 	if (headphone_mute != 0) {
   1994 		DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
   1995 
   1996 		if (mute)
   1997 			x = headphone_active;	/* mute */
   1998 		else
   1999 			x = !headphone_active;	/* unmute */
   2000 		if (x != gpio_read(headphone_mute))
   2001 			gpio_write(headphone_mute, x);
   2002 
   2003 		DPRINTF("%d\n", gpio_read(headphone_mute));
   2004 	}
   2005 }
   2006 
   2007 static void
   2008 snapper_mute_lineout(struct snapper_softc *sc, int mute)
   2009 {
   2010 	u_int x;
   2011 
   2012 	if (lineout_mute != 0) {
   2013 		DPRINTF("lineoutmute %d --> ", gpio_read(lineout_mute));
   2014 
   2015 		if (mute)
   2016 			x = lineout_active;	/* mute */
   2017 		else
   2018 			x = !lineout_active;	/* unmute */
   2019 		if (x != gpio_read(lineout_mute))
   2020 			gpio_write(lineout_mute, x);
   2021 
   2022 		DPRINTF("%d\n", gpio_read(lineout_mute));
   2023 	}
   2024 }
   2025 
   2026 static int
   2027 snapper_cint(void *v)
   2028 {
   2029 	struct snapper_softc *sc = v;
   2030 	u_int sense;
   2031 	int mask = 1 << 0;
   2032 
   2033 	if (headphone_detect != 0) {
   2034 		sense = obio_read_1(headphone_detect);
   2035 		DPRINTF("headphone detect = 0x%x\n", sense);
   2036 
   2037 		if (((sense & 0x02) >> 1) == headphone_detect_active) {
   2038 			DPRINTF("headphone is inserted\n");
   2039 			mask |= 1 << 1;
   2040 			mask &= ~(1 << 0);
   2041 		} else {
   2042 			DPRINTF("headphone is NOT inserted\n");
   2043 		}
   2044 	}
   2045 	if (lineout_detect != 0) {
   2046 		sense = obio_read_1(lineout_detect);
   2047 		DPRINTF("lineout detect = 0x%x\n", sense);
   2048 
   2049 		if (((sense & 0x02) >> 1) == lineout_detect_active) {
   2050 			DPRINTF("lineout is inserted\n");
   2051 			mask |= 1 << 2;
   2052 			mask &= ~(1 << 0);
   2053 		} else {
   2054 			DPRINTF("lineout is NOT inserted\n");
   2055 		}
   2056 	}
   2057 	if (mask != sc->sc_output_mask) {
   2058 		sc->sc_output_mask = mask;
   2059 		if (mask & (1 << 0)) {
   2060 			snapper_mute_speaker(sc, 0);
   2061 		} else snapper_mute_speaker(sc, 1);
   2062 		if (mask & (1 << 1)) {
   2063 			snapper_mute_headphone(sc, 0);
   2064 		} else snapper_mute_headphone(sc, 1);
   2065 		if (mask & (1 << 2)) {
   2066 			snapper_mute_lineout(sc, 0);
   2067 		} else snapper_mute_lineout(sc, 1);
   2068 	}
   2069 	return 1;
   2070 }
   2071 
   2072 #define reset_active 0	/* XXX OF */
   2073 
   2074 #define DEQ_WRITE(sc, reg, addr) \
   2075 	if (tas3004_write(sc, reg, addr)) goto err
   2076 
   2077 static int
   2078 tas3004_init(struct snapper_softc *sc)
   2079 {
   2080 
   2081 	/* No reset port.  Nothing to do. */
   2082 	if (audio_hw_reset == 0)
   2083 		goto noreset;
   2084 
   2085 	/* Reset TAS3004. */
   2086 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   2087 	delay(100000);				/* XXX Really needed? */
   2088 
   2089 	gpio_write(audio_hw_reset, reset_active);	/* Assert RESET */
   2090 	delay(1);
   2091 
   2092 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   2093 	delay(10000);
   2094 
   2095 noreset:
   2096 	if ((sc->sc_mode == SNAPPER_IS_TAS3004) ||
   2097 	    (sc->sc_mode == SNAPPER_IS_TAS3001)) {
   2098 		DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
   2099 		DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
   2100 		DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
   2101 		DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
   2102 		DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
   2103 		DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
   2104 		DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
   2105 		DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
   2106 		DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   2107 		DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   2108 		DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
   2109 		DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
   2110 		DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
   2111 		DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
   2112 		DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
   2113 		DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
   2114 		DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
   2115 		DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
   2116 		DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
   2117 		DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
   2118 		DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
   2119 		DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
   2120 		DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
   2121 		DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
   2122 		DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
   2123 		DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
   2124 		DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
   2125 	}
   2126 	return 0;
   2127 err:
   2128 	printf("tas3004_init: error\n");
   2129 	return -1;
   2130 }
   2131 
   2132 static void
   2133 snapper_init(struct snapper_softc *sc, int node)
   2134 {
   2135 	int gpio;
   2136 	int headphone_detect_intr, lineout_detect_intr;
   2137 	uint32_t gpio_base, reg[1], fcreg;
   2138 	char intr_xname[INTRDEVNAMEBUF];
   2139 #ifdef SNAPPER_DEBUG
   2140 	char fcr[32];
   2141 
   2142 	snprintb(fcr, sizeof(fcr),  FCR3C_BITMASK, obio_read_4(KEYLARGO_FCR1));
   2143 	printf("FCR(0x3c) %s\n", fcr);
   2144 #endif
   2145 	fcreg = obio_read_4(KEYLARGO_FCR1);
   2146 	fcreg |= I2S0CLKEN | I2S0EN;
   2147 	obio_write_4(KEYLARGO_FCR1, fcreg);
   2148 
   2149 	headphone_detect_intr = -1;
   2150 	lineout_detect_intr = -1;
   2151 
   2152 	gpio = of_getnode_byname(OF_parent(node), "gpio");
   2153 	if (OF_getprop(gpio, "reg", reg, sizeof(reg)) == sizeof(reg))
   2154 		gpio_base = reg[0];
   2155 	else
   2156 		gpio_base = 0;
   2157 	DPRINTF(" /gpio 0x%x@0x%x\n", (unsigned)gpio, gpio_base);
   2158 
   2159 	gpio = OF_child(gpio);
   2160 	while (gpio) {
   2161 		char name[64], audio_gpio[64], sid[64];
   2162 		int intr[2];
   2163 		bus_size_t addr;
   2164 
   2165 		memset(name, 0, sizeof name);
   2166 		memset(audio_gpio, 0, sizeof audio_gpio);
   2167 		addr = 0;
   2168 		OF_getprop(gpio, "name", name, sizeof name);
   2169 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
   2170 		OF_getprop(gpio, "one-wire-bus", sid, sizeof sid);
   2171 		if (OF_getprop(gpio, "AAPL,address", &addr, sizeof addr) == -1)
   2172 			if (OF_getprop(gpio, "reg", reg, sizeof reg)
   2173 			    == sizeof reg)
   2174 				addr = gpio_base + reg[0];
   2175 		/*
   2176 		 * XXX
   2177 		 * APL,address contains the absolute address, we only want the
   2178 		 * offset from mac-io's base address
   2179 		 */
   2180 		addr &= 0x7fff;
   2181 		DPRINTF(" 0x%x %s %s %08x\n", gpio, name, audio_gpio, addr);
   2182 
   2183 		/* gpio5 */
   2184 		if (strcmp(audio_gpio, "headphone-mute") == 0 ||
   2185 		    strcmp(name, "headphone-mute") == 0)
   2186 			headphone_mute = addr;
   2187 		/* gpio6 */
   2188 		if (strcmp(audio_gpio, "amp-mute") == 0 ||
   2189 		    strcmp(name, "amp-mute") == 0)
   2190 			amp_mute = addr;
   2191 		/* extint-gpio15 */
   2192 		if (strcmp(audio_gpio, "headphone-detect") == 0 ||
   2193 		    strcmp(name, "headphone-detect") == 0) {
   2194 		    	uint32_t act = 0;
   2195 			headphone_detect = addr;
   2196 			OF_getprop(gpio, "audio-gpio-active-state", &act, 4);
   2197 			headphone_detect_active = act;
   2198 			if (OF_getprop(gpio, "interrupts", intr, 8) == 8) {
   2199 				headphone_detect_intr = intr[0];
   2200 			}
   2201 		}
   2202 		if (strcmp(audio_gpio, "lineout-mute") == 0 ||
   2203 		    strcmp(name, "lineout-mute") == 0 ||
   2204 		    strcmp(name, "line-output-mute") == 0)
   2205 			lineout_mute = addr;
   2206 		if (strcmp(audio_gpio, "lineout-detect") == 0 ||
   2207 		    strcmp(name, "lineout-detect") == 0 ||
   2208 		    strcmp(name, "line-output-detect") == 0) {
   2209 		    	uint32_t act = 0;
   2210 			lineout_detect = addr;
   2211 			OF_getprop(gpio, "audio-gpio-active-state", &act, 4);
   2212 			lineout_detect_active = act;
   2213 			if (OF_getprop(gpio, "interrupts", intr, 8) == 8) {
   2214 				lineout_detect_intr = intr[0];
   2215 			}
   2216 		}
   2217 		/* extint-gpio16 on Quicksilver */
   2218 		if (strcmp(sid, "speaker-id") == 0) {
   2219 			owaddr = addr;
   2220 		}
   2221 		/* gpio11 (keywest-11) */
   2222 		if (strcmp(audio_gpio, "audio-hw-reset") == 0 ||
   2223 		    strcmp(name, "hw-reset") == 0)
   2224 			audio_hw_reset = addr;
   2225 
   2226 		gpio = OF_peer(gpio);
   2227 	}
   2228 
   2229 	if (owaddr != -1) snapper_setup_ow(sc);
   2230 
   2231 	DPRINTF(" headphone-mute %x\n", headphone_mute);
   2232 	DPRINTF(" lineout-mute %x\n", lineout_mute);
   2233 	DPRINTF(" amp-mute %x\n", amp_mute);
   2234 	DPRINTF(" headphone-detect %x\n", headphone_detect);
   2235 	DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
   2236 	DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
   2237 	DPRINTF(" lineout-detect %x\n", lineout_detect);
   2238 	DPRINTF(" lineout-detect active %x\n", lineout_detect_active);
   2239 	DPRINTF(" lineout-detect intr %x\n", lineout_detect_intr);
   2240 	DPRINTF(" audio-hw-reset %x\n", audio_hw_reset);
   2241 
   2242 	if (headphone_detect_intr != -1) {
   2243 		snprintf(intr_xname, sizeof(intr_xname), "%s headphone",
   2244 		    device_xname(sc->sc_dev));
   2245 		intr_establish_xname(headphone_detect_intr, IST_EDGE, IPL_AUDIO,
   2246 		    snapper_cint, sc, intr_xname);
   2247 	}
   2248 
   2249 	if (lineout_detect_intr != -1) {
   2250 		snprintf(intr_xname, sizeof(intr_xname), "%s line out",
   2251 		    device_xname(sc->sc_dev));
   2252 		intr_establish_xname(lineout_detect_intr, IST_EDGE, IPL_AUDIO,
   2253 		    snapper_cint, sc, intr_xname);
   2254 	}
   2255 
   2256 	sc->sc_rate = 44100;	/* default rate */
   2257 	sc->sc_bitspersample = 16;
   2258 
   2259 	/* Enable headphone interrupt? */
   2260 	if (headphone_detect != 0) {
   2261 		obio_write_1(headphone_detect,
   2262 		    obio_read_1(headphone_detect) | 0x80);
   2263 	}
   2264 	if (lineout_detect != 0) {
   2265 		obio_write_1(lineout_detect,
   2266 		    obio_read_1(lineout_detect) | 0x80);
   2267 	}
   2268 
   2269 	if (tas3004_init(sc))
   2270 		return;
   2271 
   2272 	/* Update headphone status. */
   2273 	snapper_cint(sc);
   2274 
   2275 	snapper_set_volume(sc, 128, 128);
   2276 	snapper_set_bass(sc, 128);
   2277 	snapper_set_treble(sc, 128);
   2278 
   2279 	/* Record source defaults to line in.  This reflects the
   2280 	 * default value for the ACR (see tas3004_initdata).
   2281 	 */
   2282 	sc->sc_record_source = 1 << 1;
   2283 
   2284 	/* We mute the analog input for now */
   2285 	sc->mixer[0] = 128;
   2286 	sc->mixer[1] = 128;
   2287 	sc->mixer[2] = 0;
   2288 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
   2289 		sc->mixer[3] = 0;
   2290 	} else
   2291 		sc->mixer[3] = 128;
   2292 	sc->mixer[4] = 128;
   2293 	sc->mixer[5] = 0;
   2294 	snapper_write_mixers(sc);
   2295 }
   2296 
   2297 static void
   2298 snapper_setup_ow(struct snapper_softc *sc)
   2299 {
   2300 	struct onewirebus_attach_args oba;
   2301 
   2302 	/* Attach 1-Wire bus */
   2303 	sc->sc_ow_bus.bus_cookie = sc;
   2304 	sc->sc_ow_bus.bus_reset = snapper_ow_reset;
   2305 	sc->sc_ow_bus.bus_read_bit = snapper_ow_read_bit;
   2306 	sc->sc_ow_bus.bus_write_bit = snapper_ow_write_bit;
   2307 
   2308 	memset(&oba, 0, sizeof(oba));
   2309 	oba.oba_bus = &sc->sc_ow_bus;
   2310 	sc->sc_ow_dev = config_found(sc->sc_dev, &oba, onewirebus_print,
   2311 	    CFARGS(.iattr = "onewirebus"));
   2312 }
   2313 
   2314 static int
   2315 snapper_ow_reset(void *cookie)
   2316 {
   2317 	return (onewire_bb_reset(&snapper_bbops, cookie));
   2318 }
   2319 
   2320 static int
   2321 snapper_ow_read_bit(void *cookie)
   2322 {
   2323 	return (onewire_bb_read_bit(&snapper_bbops, cookie));
   2324 }
   2325 
   2326 static void
   2327 snapper_ow_write_bit(void *cookie, int bit)
   2328 {
   2329 	onewire_bb_write_bit(&snapper_bbops, cookie, bit);
   2330 }
   2331 
   2332 static void
   2333 snapper_bb_rx(void *cookie)
   2334 {
   2335 	struct snapper_softc *sc = cookie;
   2336 
   2337 	sc->sc_ow_data &= ~GPIO_DDR_OUTPUT;
   2338 	obio_write_1(owaddr, sc->sc_ow_data);
   2339 }
   2340 
   2341 static void
   2342 snapper_bb_tx(void *cookie)
   2343 {
   2344 	struct snapper_softc *sc = cookie;
   2345 
   2346 	sc->sc_ow_data |= GPIO_DDR_OUTPUT;
   2347 	obio_write_1(owaddr, sc->sc_ow_data);
   2348 }
   2349 
   2350 static int snapper_bb_get(void *cookie)
   2351 {
   2352 	int data = (obio_read_1(owaddr) & GPIO_LEVEL) ? 1 : 0;
   2353 	return data;
   2354 }
   2355 
   2356 static void snapper_bb_set(void *cookie, int bit)
   2357 {
   2358 	struct snapper_softc *sc = cookie;
   2359 
   2360 	if (bit) {
   2361 		sc->sc_ow_data |= GPIO_DATA;
   2362 	} else
   2363 		sc->sc_ow_data &= ~GPIO_DATA;
   2364 
   2365 	obio_write_1(owaddr, sc->sc_ow_data);
   2366 }
   2367