Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: snapper.c,v 1.70 2025/09/08 08:06:19 macallan 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.70 2025/09/08 08:06:19 macallan Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/audioio.h>
     42 #include <sys/device.h>
     43 #include <sys/systm.h>
     44 
     45 #include <dev/audio/audio_if.h>
     46 #include <dev/ofw/openfirm.h>
     47 #include <macppc/dev/dbdma.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 #include <dev/i2c/i2cvar.h>
     51 #include <dev/onewire/onewirevar.h>
     52 
     53 #include <machine/autoconf.h>
     54 #include <machine/pio.h>
     55 
     56 #include <macppc/dev/deqvar.h>
     57 #include <macppc/dev/obiovar.h>
     58 #include <macppc/dev/i2sreg.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 	int16_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 /* TAS3004/TAS3001 registers */
    535 #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
    536 #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?)
    537                             	   2 bytes (reserved) on the TAS 3001 */
    538 #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
    539 #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
    540 #define DEQ_BASS	0x06	/* Bass control (1byte) */
    541 #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes; 3 on TAS3001) */
    542 #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes; 3 on TAS3001) */
    543 #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
    544 #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
    545 #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
    546 #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
    547 #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
    548 #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
    549 #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
    550 #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
    551 #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
    552 #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
    553 #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
    554 #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
    555 #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
    556 #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
    557 #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
    558 #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
    559 #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
    560 #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
    561 #define DEQ_ACR		0x40	/* [TAS3004] Analog control register (1byte) */
    562 #define DEQ_MCR2	0x43	/* [TAS3004] Main control register 2 (1byte) */
    563 #define DEQ_MCR1_FL	0x80	/* Fast load */
    564 #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
    565 #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
    566 #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
    567 #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
    568 #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
    569 #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
    570 #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
    571 #define DEQ_MCR1_ISM	0x0c	/* [TAS3001] Input serial port mode */
    572 #define  DEQ_MCR1_ISM_L	0x00	/*           Left justified */
    573 #define  DEQ_MCR1_ISM_R	0x04	/*           Right justified */
    574 #define  DEQ_MCR1_ISM_I2S 0x08	/*           I2S */
    575 #define DEQ_MCR1_W	0x03	/* Serial port word length */
    576 #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
    577 #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
    578 #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
    579 #define  DEQ_MCR1_W_24	0x03	/*  24 bit */
    580 
    581 #define DEQ_MCR2_DL	0x80	/* Download */
    582 #define DEQ_MCR2_AP	0x02	/* All pass mode */
    583 
    584 #define DEQ_ACR_ADM	0x80	/* ADC output mode */
    585 #define DEQ_ACR_LRB	0x40	/* Select B input */
    586 #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
    587 #define  DEQ_ACR_DM_OFF	0x00	/*  off */
    588 #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
    589 #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
    590 #define DEQ_ACR_INP	0x02	/* Analog input select */
    591 #define  DEQ_ACR_INP_A	0x00	/*  A */
    592 #define  DEQ_ACR_INP_B	0x02	/*  B */
    593 #define DEQ_ACR_APD	0x01	/* Analog power down */
    594 
    595 struct tas3004_reg {
    596 	u_char MCR1[1];
    597 	u_char DRC[6];
    598 	u_char VOLUME[6];
    599 	u_char TREBLE[1];
    600 	u_char BASS[1];
    601 	u_char MIXER_L[9];
    602 	u_char MIXER_R[9];
    603 	u_char LB0[15];
    604 	u_char LB1[15];
    605 	u_char LB2[15];
    606 	u_char LB3[15];
    607 	u_char LB4[15];
    608 	u_char LB5[15];
    609 	u_char LB6[15];
    610 	u_char RB0[15];
    611 	u_char RB1[15];
    612 	u_char RB2[15];
    613 	u_char RB3[15];
    614 	u_char RB4[15];
    615 	u_char RB5[15];
    616 	u_char RB6[15];
    617 	u_char LLB[15];
    618 	u_char RLB[15];
    619 	u_char LLB_GAIN[3];
    620 	u_char RLB_GAIN[3];
    621 	u_char ACR[1];
    622 	u_char MCR2[1];
    623 };
    624 
    625 static int
    626 snapper_match(device_t parent, struct cfdata *match, void *aux)
    627 {
    628 	struct confargs *ca;
    629 	int soundbus, soundchip, soundcodec;
    630 	char compat[32];
    631 
    632 	ca = aux;
    633 	if (strcmp(ca->ca_name, "i2s") != 0)
    634 		return 0;
    635 
    636 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
    637 	    (soundchip = OF_child(soundbus)) == 0)
    638 		return 0;
    639 
    640 	memset(compat, 0, sizeof compat);
    641 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
    642 
    643 	if (strcmp(compat, "snapper") == 0)
    644 		return 1;
    645 
    646 	if (strcmp(compat, "tumbler") == 0)
    647 		return 1;
    648 
    649 	if (strcmp(compat, "AOAKeylargo") == 0)
    650 		return 1;
    651 
    652 	if (strcmp(compat, "AOAK2") == 0)
    653 		return 1;
    654 
    655 	if (strcmp(compat, "AOAShasta") == 0)
    656 		return 1;
    657 
    658 	if (strcmp(compat, "AOAbase") == 0)
    659 		return 1;
    660 
    661 	if (OF_getprop(soundchip, "platform-tas-codec-ref",
    662 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
    663 		return 1;
    664 
    665 	return 0;
    666 }
    667 
    668 static void
    669 snapper_attach(device_t parent, device_t self, void *aux)
    670 {
    671 	struct snapper_softc *sc;
    672 	struct confargs *ca;
    673 	int cirq, oirq, iirq, /*cirq_type,*/ oirq_type, iirq_type, soundbus;
    674 	uint32_t intr[6], reg[6];
    675 	char compat[32], intr_xname[INTRDEVNAMEBUF];
    676 
    677 	sc = device_private(self);
    678 	sc->sc_dev = self;
    679 
    680 	ca = aux;
    681 
    682 	soundbus = OF_child(ca->ca_node);
    683 	memset(compat, 0, sizeof compat);
    684 	OF_getprop(OF_child(soundbus), "compatible", compat, sizeof compat);
    685 
    686 	sc->sc_mode = SNAPPER_IS_TAS3004;
    687 
    688 	if (strcmp(compat, "tumbler") == 0)
    689 		sc->sc_mode = SNAPPER_IS_TAS3001;
    690 	sc->sc_swvol_l = 255;
    691 	sc->sc_swvol_r = 255;
    692 	sc->sc_vol_l = 128;
    693 	sc->sc_vol_r = 128;
    694 	sc->sc_rval = 0;
    695 
    696 	sc->sc_odmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    697 				     sizeof(struct dbdma_command), NULL);
    698 	sc->sc_idmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) *
    699 				     sizeof(struct dbdma_command), NULL);
    700 
    701 	sc->sc_baseaddr = ca->ca_baseaddr;
    702 
    703 	OF_getprop(soundbus, "reg", reg, sizeof reg);
    704 	/* deal with messed up properties on PowerMac7,3 and friends */
    705 	if (reg[0] == 0) {
    706 		reg[0] += ca->ca_reg[0];
    707 		reg[2] += ca->ca_reg[2];
    708 		reg[4] += ca->ca_reg[2];
    709 	}
    710 	reg[0] += ca->ca_baseaddr;
    711 	reg[2] += ca->ca_baseaddr;
    712 	reg[4] += ca->ca_baseaddr;
    713 
    714 	sc->sc_node = ca->ca_node;
    715 	sc->sc_tag = ca->ca_tag;
    716 
    717 #ifdef SNAPPER_DEBUG
    718 	{
    719 		int i;
    720 		printf("\n");
    721 		for (i = 0; i < 6; i++) {
    722 			printf(" %08x", reg[i]);
    723 		}
    724 		printf("\n");
    725 	}
    726 #endif
    727 
    728 	bus_space_map(sc->sc_tag, reg[0], reg[1], 0, &sc->sc_bsh);
    729 	obio_space_map(reg[2], reg[3], &sc->sc_odmah);
    730 	obio_space_map(reg[4], reg[5], &sc->sc_idmah);
    731 
    732 	sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
    733 	sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah);
    734 
    735 	DPRINTF("reg %08x odma %08x\n", (uint32_t)sc->sc_bsh, (uint32_t)sc->sc_odmah);
    736 
    737 	OF_getprop(soundbus, "interrupts", intr, sizeof intr);
    738 	cirq = intr[0];
    739 	oirq = intr[2];
    740 	iirq = intr[4];
    741 	/* cirq_type = intr[1] ? IST_LEVEL : IST_EDGE; */
    742 	oirq_type = (intr[3] & 1) ? IST_LEVEL : IST_EDGE;
    743 	iirq_type = (intr[5] & 1) ? IST_LEVEL : IST_EDGE;
    744 
    745 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
    746 
    747 	snprintf(intr_xname, sizeof(intr_xname), "%s out", device_xname(self));
    748 	intr_establish_xname(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc,
    749 	    intr_xname);
    750 
    751 	snprintf(intr_xname, sizeof(intr_xname), "%s in", device_xname(self));
    752 	intr_establish_xname(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc,
    753 	    intr_xname);
    754 
    755 	aprint_normal(": irq %d,%d,%d\n", cirq, oirq, iirq);
    756 
    757 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    758 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    759 
    760 	/* PMF event handler */
    761 	pmf_device_register(sc->sc_dev, NULL, NULL);
    762 
    763 	config_defer(self, snapper_defer);
    764 }
    765 
    766 static void
    767 snapper_defer(device_t dev)
    768 {
    769 	struct snapper_softc *sc;
    770 	device_t dv;
    771 	deviter_t di;
    772 	struct deq_softc *deq;
    773 	char prop[64], next[64], codec[64], *cref;
    774 	int codec_node, soundbus, sound, ok, deqnode = 0;
    775 
    776 	sc = device_private(dev);
    777 
    778 	/* look for platform-*-codec-ref node */
    779 
    780 	/*
    781 	 * XXX
    782 	 * there can be more than one i2sbus, the one we want just so happens
    783 	 * to be the first we see
    784 	 */
    785 	soundbus = OF_child(sc->sc_node);
    786 	sound = OF_child(soundbus);
    787 	ok = OF_nextprop(sound, NULL, next);
    788 	codec_node = 0;
    789 	while (ok && (codec_node == 0)) {
    790 		DPRINTF("prop %d %s\n", ok, next);
    791 		strncpy(prop, next, 64);
    792 		if ((cref = strstr(next, "-codec-ref")) != NULL) {
    793 			OF_getprop(sound, next, &codec_node, 4);
    794 			if (codec_node != 0) {
    795 				OF_getprop(codec_node, "compatible", codec, 64);
    796 				DPRINTF("%08x %s\n", codec_node, codec);
    797 			}
    798 		}
    799 		ok = OF_nextprop(sound, prop, next);
    800 	}
    801 
    802 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    803 	     dv != NULL;
    804 	     dv = deviter_next(&di)) {
    805 		if (device_is_a(dv, "deq")) {
    806 			deq = device_private(dv);
    807 			if (codec_node != 0) {
    808 				if (codec_node != deq->sc_node)
    809 					continue;
    810 			}
    811 			sc->sc_i2c = deq->sc_i2c;
    812 			sc->sc_deqaddr = deq->sc_address;
    813 			deqnode = deq->sc_node;
    814 		}
    815 	}
    816 	deviter_release(&di);
    817 
    818 	DPRINTF("deqnode: %08x\n", deqnode);
    819 
    820 	/* If we don't find a codec, it's not the end of the world;
    821 	 * we can control the volume in software in this case.
    822 	 */
    823 	if (sc->sc_i2c == NULL) {
    824 		sc->sc_mode = SNAPPER_SWVOL;
    825 	} else if (deqnode != 0) {
    826 		int ret;
    827 		codec[0] = 0;
    828 		ret = OF_getprop(deqnode, "compatible", codec, 64);
    829 
    830 		DPRINTF("codec <%s> %d\n", codec, ret);
    831 
    832 		if (codec[0] == 0) {
    833 			if (sc->sc_deqaddr == 0x34) {
    834 				sc->sc_mode = SNAPPER_IS_TAS3001;
    835 			} else {
    836 				int root = OF_finddevice("/");
    837 				char model[32];
    838 				sc->sc_mode = SNAPPER_IS_TAS3004;
    839 				if (OF_getprop(root, "model", model, 32) > 0) {
    840 					printf("model %s\n", model);
    841 					if (strcmp(model, "PowerMac8,1") == 0) {
    842 						sc->sc_mode = SNAPPER_IS_PCM3052;
    843 					}
    844 				}
    845 			}
    846 		} else if (strcmp(codec, "tas3004") == 0) {
    847 			sc->sc_mode = SNAPPER_IS_TAS3004;
    848 		} else if (strcmp(codec, "pcm3052") == 0) {
    849 			sc->sc_mode = SNAPPER_IS_PCM3052;
    850 		} else if (strcmp(codec, "cs8416") == 0) {
    851 			sc->sc_mode = SNAPPER_IS_CS8416;
    852 		}
    853 	}
    854 	DPRINTF("mode %d\n", sc->sc_mode);
    855 	switch (sc->sc_mode) {
    856 	case SNAPPER_SWVOL:
    857 		aprint_verbose("%s: software codec\n", device_xname(dev));
    858 		break;
    859 	case SNAPPER_IS_TAS3001:
    860 		aprint_verbose("%s: codec: TAS3001\n", device_xname(dev));
    861 		break;
    862 	case SNAPPER_IS_TAS3004:
    863 		aprint_verbose("%s: codec: TAS3004\n", device_xname(dev));
    864 		break;
    865 	case SNAPPER_IS_PCM3052:
    866 		aprint_verbose("%s: codec: PCM3052 / ONYX\n", device_xname(dev));
    867 		break;
    868 	default:
    869 		aprint_error_dev(sc->sc_dev, "unsupported codec\n");
    870 		sc->sc_mode = SNAPPER_SWVOL;
    871 	}
    872 
    873 	snapper_init(sc, sc->sc_node);
    874 
    875 	audio_attach_mi(&snapper_hw_if, sc, sc->sc_dev);
    876 }
    877 
    878 static int
    879 snapper_intr(void *v)
    880 {
    881 	struct snapper_softc *sc;
    882 	struct dbdma_command *cmd;
    883 	int count;
    884 	int status;
    885 
    886 	sc = v;
    887 	mutex_spin_enter(&sc->sc_intr_lock);
    888 	cmd = sc->sc_odmacmd;
    889 	count = sc->sc_opages;
    890 	/* Fill used buffer(s). */
    891 	while (count-- > 0) {
    892 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    893 			status = in16rb(&cmd->d_status);
    894 			cmd->d_status = 0;
    895 			if (status)	/* status == 0x8400 */
    896 				if (sc->sc_ointr)
    897 					(*sc->sc_ointr)(sc->sc_oarg);
    898 		}
    899 		cmd++;
    900 	}
    901 
    902 	cmd = sc->sc_idmacmd;
    903 	count = sc->sc_ipages;
    904 	while (count-- > 0) {
    905 		if ((in16rb(&cmd->d_command) & 0x30) == 0x30) {
    906 			status = in16rb(&cmd->d_status);
    907 			cmd->d_status = 0;
    908 			if (status)	/* status == 0x8400 */
    909 				if (sc->sc_iintr)
    910 					(*sc->sc_iintr)(sc->sc_iarg);
    911 		}
    912 		cmd++;
    913 	}
    914 	mutex_spin_exit(&sc->sc_intr_lock);
    915 
    916 	return 1;
    917 }
    918 
    919 
    920 static int
    921 snapper_query_format(void *h, audio_format_query_t *afp)
    922 {
    923 	struct snapper_softc *sc = h;
    924 
    925 	switch (sc->sc_mode) {
    926 		case SNAPPER_IS_TAS3001:
    927 			return audio_query_format(tumbler_formats,
    928 			    TUMBLER_NFORMATS, afp);
    929 		case SNAPPER_SWVOL:
    930 		case SNAPPER_IS_TAS3004:
    931 			return audio_query_format(snapper_formats,
    932 			    SNAPPER_NFORMATS, afp);
    933 		case SNAPPER_IS_PCM3052:
    934 			return audio_query_format(onyx_formats,
    935 			    ONYX_NFORMATS, afp);
    936 	}
    937 	return -1;
    938 }
    939 
    940 static int
    941 snapper_set_format(void *h, int setmode,
    942 		   const audio_params_t *play, const audio_params_t *rec,
    943 		   audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    944 {
    945 	struct snapper_softc *sc;
    946 
    947 	sc = h;
    948 
    949 	/* *play and *rec are the identical because !AUDIO_PROP_INDEPENDENT. */
    950 
    951 	if (sc->sc_mode == SNAPPER_SWVOL) {
    952 		pfil->codec = snapper_volume;
    953 		pfil->context = sc;
    954 		rfil->codec = snapper_volume;
    955 		rfil->context = sc;
    956 	} else if (sc->sc_mode == 0 && play->channels == 2) {
    957 		/* Fix phase problems on TAS3004.  */
    958 		pfil->codec = snapper_fixphase;
    959 		pfil->context = sc;
    960 		rfil->codec = snapper_fixphase;
    961 		rfil->context = sc;
    962 	}
    963 
    964 	/* Set the speed. */
    965 	sc->sc_rate = play->sample_rate;
    966 	sc->sc_bitspersample = play->precision;
    967 	return 0;
    968 }
    969 
    970 static int
    971 snapper_commit_settings(void *h)
    972 {
    973 	struct snapper_softc *sc;
    974 
    975 	DPRINTF("commit_settings\n");
    976 	sc = h;
    977 
    978 	return snapper_set_rate(sc);
    979 }
    980 
    981 static int
    982 snapper_round_blocksize(void *h, int size, int mode,
    983 			const audio_params_t *param)
    984 {
    985 
    986 	if (size < (3 * NBPG))
    987 		size = (3 * NBPG);
    988 	return size & ~PGOFSET;
    989 }
    990 
    991 static int
    992 snapper_halt_output(void *h)
    993 {
    994 	struct snapper_softc *sc;
    995 
    996 	sc = h;
    997 	dbdma_stop(sc->sc_odma);
    998 	dbdma_reset(sc->sc_odma);
    999 	sc->sc_ointr = NULL;
   1000 	sc->sc_rval = 0;
   1001 	return 0;
   1002 }
   1003 
   1004 static int
   1005 snapper_halt_input(void *h)
   1006 {
   1007 	struct snapper_softc *sc;
   1008 
   1009 	sc = h;
   1010 	dbdma_stop(sc->sc_idma);
   1011 	dbdma_reset(sc->sc_idma);
   1012 	sc->sc_iintr = NULL;
   1013 	sc->sc_rval = 0;
   1014 	return 0;
   1015 }
   1016 
   1017 static int
   1018 snapper_getdev(void *h, struct audio_device *retp)
   1019 {
   1020 
   1021 	*retp = snapper_device;
   1022 	return 0;
   1023 }
   1024 
   1025 enum {
   1026 	SNAPPER_MONITOR_CLASS,
   1027 	SNAPPER_OUTPUT_CLASS,
   1028 	SNAPPER_RECORD_CLASS,
   1029 	SNAPPER_OUTPUT_SELECT,
   1030 	SNAPPER_VOL_OUTPUT,
   1031 	SNAPPER_DIGI1,
   1032 	SNAPPER_DIGI2,
   1033 	SNAPPER_VOL_INPUT,
   1034 	SNAPPER_TREBLE,
   1035 	SNAPPER_BASS,
   1036 	/* From this point, unsupported by the TAS 3001 */
   1037 	SNAPPER_ANALOG,
   1038 	SNAPPER_INPUT_SELECT,
   1039 	SNAPPER_ENUM_LAST
   1040 };
   1041 
   1042 static int
   1043 snapper_set_port(void *h, mixer_ctrl_t *mc)
   1044 {
   1045 	struct snapper_softc *sc;
   1046 	int l, r;
   1047 	u_char data;
   1048 
   1049 	DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
   1050 	sc = h;
   1051 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1052 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1053 
   1054 	switch (mc->dev) {
   1055 	case SNAPPER_OUTPUT_SELECT:
   1056 		/* No change necessary? */
   1057 		if (mc->un.mask == sc->sc_output_mask)
   1058 			return 0;
   1059 
   1060 		snapper_mute_speaker(sc, 1);
   1061 		snapper_mute_headphone(sc, 1);
   1062 		snapper_mute_lineout(sc, 1);
   1063 		if (mc->un.mask & 1 << 0)
   1064 			snapper_mute_speaker(sc, 0);
   1065 		if (mc->un.mask & 1 << 1)
   1066 			snapper_mute_headphone(sc, 0);
   1067 		if (mc->un.mask & 1 << 2)
   1068 			snapper_mute_lineout(sc, 0);
   1069 
   1070 		sc->sc_output_mask = mc->un.mask;
   1071 		return 0;
   1072 
   1073 	case SNAPPER_VOL_OUTPUT:
   1074 		snapper_set_volume(sc, l, r);
   1075 		return 0;
   1076 
   1077 	case SNAPPER_INPUT_SELECT:
   1078 		if (sc->sc_mode != 0)
   1079 			return ENXIO;
   1080 
   1081 		/* no change necessary? */
   1082 		if (mc->un.mask == sc->sc_record_source)
   1083 			return 0;
   1084 		switch (mc->un.mask) {
   1085 		case 1 << 0: /* microphone */
   1086 			/* Select right channel of B input */
   1087 			data = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B;
   1088 			tas3004_write(sc, DEQ_ACR, &data);
   1089 			break;
   1090 		case 1 << 1: /* line in */
   1091 			/* Select both channels of A input */
   1092 			data = 0;
   1093 			tas3004_write(sc, DEQ_ACR, &data);
   1094 			break;
   1095 		default: /* invalid argument */
   1096 			return EINVAL;
   1097 		}
   1098 		sc->sc_record_source = mc->un.mask;
   1099 		return 0;
   1100 
   1101 	case SNAPPER_VOL_INPUT:
   1102 		/* XXX TO BE DONE */
   1103 		return 0;
   1104 
   1105 	case SNAPPER_BASS:
   1106 		if (sc->sc_mode == SNAPPER_SWVOL)
   1107 			return ENXIO;
   1108 		snapper_set_bass(sc, l);
   1109 		return 0;
   1110 	case SNAPPER_TREBLE:
   1111 		if (sc->sc_mode == SNAPPER_SWVOL)
   1112 			return ENXIO;
   1113 		snapper_set_treble(sc, l);
   1114 		return 0;
   1115 	case SNAPPER_DIGI1:
   1116 		if (sc->sc_mode == SNAPPER_SWVOL)
   1117 			return ENXIO;
   1118 
   1119 		sc->mixer[0] = l;
   1120 		sc->mixer[3] = r;
   1121 		snapper_write_mixers(sc);
   1122 		return 0;
   1123 	case SNAPPER_DIGI2:
   1124 		if (sc->sc_mode == SNAPPER_SWVOL)
   1125 			return ENXIO;
   1126 
   1127 		if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1128 			sc->mixer[3] = l;
   1129 		else {
   1130 			sc->mixer[1] = l;
   1131 			sc->mixer[4] = r;
   1132 		}
   1133 		snapper_write_mixers(sc);
   1134 		return 0;
   1135 	case SNAPPER_ANALOG:
   1136 		if (sc->sc_mode != 0)
   1137 			return ENXIO;
   1138 
   1139 		sc->mixer[2] = l;
   1140 		sc->mixer[5] = r;
   1141 		snapper_write_mixers(sc);
   1142 		return 0;
   1143 	}
   1144 	return ENXIO;
   1145 }
   1146 
   1147 static int
   1148 snapper_get_port(void *h, mixer_ctrl_t *mc)
   1149 {
   1150 	struct snapper_softc *sc;
   1151 
   1152 	DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
   1153 	sc = h;
   1154 	switch (mc->dev) {
   1155 	case SNAPPER_OUTPUT_SELECT:
   1156 		mc->un.mask = sc->sc_output_mask;
   1157 		return 0;
   1158 
   1159 	case SNAPPER_VOL_OUTPUT:
   1160 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
   1161 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
   1162 		return 0;
   1163 
   1164 	case SNAPPER_INPUT_SELECT:
   1165 		if (sc->sc_mode != 0)
   1166 			return ENXIO;
   1167 
   1168 		mc->un.mask = sc->sc_record_source;
   1169 		return 0;
   1170 
   1171 	case SNAPPER_VOL_INPUT:
   1172 		/* XXX TO BE DONE */
   1173 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
   1174 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
   1175 		return 0;
   1176 
   1177 	case SNAPPER_TREBLE:
   1178 		if (sc->sc_mode == SNAPPER_SWVOL)
   1179 			return ENXIO;
   1180 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble;
   1181 		return 0;
   1182 	case SNAPPER_BASS:
   1183 		if (sc->sc_mode == SNAPPER_SWVOL)
   1184 			return ENXIO;
   1185 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass;
   1186 		return 0;
   1187 
   1188 	case SNAPPER_DIGI1:
   1189 		if (sc->sc_mode == SNAPPER_SWVOL)
   1190 			return ENXIO;
   1191 
   1192 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0];
   1193 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3];
   1194 		return 0;
   1195 	case SNAPPER_DIGI2:
   1196 		if (sc->sc_mode == SNAPPER_SWVOL)
   1197 			return ENXIO;
   1198 
   1199 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1];
   1200 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4];
   1201 		return 0;
   1202 	case SNAPPER_ANALOG:
   1203 		if (sc->sc_mode != 0)
   1204 			return ENXIO;
   1205 
   1206 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2];
   1207 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5];
   1208 		return 0;
   1209 	default:
   1210 		return ENXIO;
   1211 	}
   1212 
   1213 	return 0;
   1214 }
   1215 
   1216 static int
   1217 snapper_query_devinfo(void *h, mixer_devinfo_t *dip)
   1218 {
   1219 	struct snapper_softc *sc = h;
   1220 
   1221 	switch (dip->index) {
   1222 
   1223 	case SNAPPER_OUTPUT_SELECT:
   1224 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1225 		strcpy(dip->label.name, AudioNoutput);
   1226 		dip->type = AUDIO_MIXER_SET;
   1227 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1228 		dip->un.s.num_mem = 3;
   1229 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
   1230 		dip->un.s.member[0].mask = 1 << 0;
   1231 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
   1232 		dip->un.s.member[1].mask = 1 << 1;
   1233 		strcpy(dip->un.s.member[2].label.name, AudioNline);
   1234 		dip->un.s.member[2].mask = 1 << 2;
   1235 		return 0;
   1236 
   1237 	case SNAPPER_VOL_OUTPUT:
   1238 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1239 		strcpy(dip->label.name, AudioNmaster);
   1240 		dip->type = AUDIO_MIXER_VALUE;
   1241 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1242 		dip->un.v.num_channels = 2;
   1243 		dip->un.v.delta = 16;
   1244 		strcpy(dip->un.v.units.name, AudioNvolume);
   1245 		return 0;
   1246 
   1247 	case SNAPPER_INPUT_SELECT:
   1248 		if (sc->sc_mode != 0)
   1249 			return ENXIO;
   1250 
   1251 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1252 		strcpy(dip->label.name, AudioNsource);
   1253 		dip->type = AUDIO_MIXER_SET;
   1254 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1255 		dip->un.s.num_mem = 2;
   1256 		strcpy(dip->un.s.member[0].label.name, AudioNmicrophone);
   1257 		dip->un.s.member[0].mask = 1 << 0;
   1258 		strcpy(dip->un.s.member[1].label.name, AudioNline);
   1259 		dip->un.s.member[1].mask = 1 << 1;
   1260 		return 0;
   1261 
   1262 	case SNAPPER_VOL_INPUT:
   1263 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1264 		strcpy(dip->label.name, AudioNrecord);
   1265 		dip->type = AUDIO_MIXER_VALUE;
   1266 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1267 		dip->un.v.num_channels = 2;
   1268 		strcpy(dip->un.v.units.name, AudioNvolume);
   1269 		return 0;
   1270 
   1271 	case SNAPPER_MONITOR_CLASS:
   1272 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1273 		strcpy(dip->label.name, AudioCmonitor);
   1274 		dip->type = AUDIO_MIXER_CLASS;
   1275 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1276 		return 0;
   1277 
   1278 	case SNAPPER_OUTPUT_CLASS:
   1279 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1280 		strcpy(dip->label.name, AudioCoutputs);
   1281 		dip->type = AUDIO_MIXER_CLASS;
   1282 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1283 		return 0;
   1284 
   1285 	case SNAPPER_RECORD_CLASS:
   1286 		dip->mixer_class = SNAPPER_RECORD_CLASS;
   1287 		strcpy(dip->label.name, AudioCrecord);
   1288 		dip->type = AUDIO_MIXER_CLASS;
   1289 		dip->next = dip->prev = AUDIO_MIXER_LAST;
   1290 		return 0;
   1291 
   1292 	case SNAPPER_TREBLE:
   1293 		if (sc->sc_mode == SNAPPER_SWVOL)
   1294 			return ENXIO;
   1295 
   1296 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1297 		strcpy(dip->label.name, AudioNtreble);
   1298 		dip->type = AUDIO_MIXER_VALUE;
   1299 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1300 		dip->un.v.num_channels = 1;
   1301 		return 0;
   1302 
   1303 	case SNAPPER_BASS:
   1304 		if (sc->sc_mode == SNAPPER_SWVOL)
   1305 			return ENXIO;
   1306 
   1307 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1308 		strcpy(dip->label.name, AudioNbass);
   1309 		dip->type = AUDIO_MIXER_VALUE;
   1310 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1311 		dip->un.v.num_channels = 1;
   1312 		return 0;
   1313 
   1314 	case SNAPPER_DIGI1:
   1315 		if (sc->sc_mode == SNAPPER_SWVOL)
   1316 			return ENXIO;
   1317 
   1318 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1319 		strcpy(dip->label.name, AudioNdac);
   1320 		dip->type = AUDIO_MIXER_VALUE;
   1321 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1322 		dip->un.v.num_channels =
   1323 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1324 		return 0;
   1325 	case SNAPPER_DIGI2:
   1326 		if (sc->sc_mode == SNAPPER_SWVOL)
   1327 			return ENXIO;
   1328 
   1329 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
   1330 		strcpy(dip->label.name, AudioNline);
   1331 		dip->type = AUDIO_MIXER_VALUE;
   1332 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1333 		dip->un.v.num_channels =
   1334 			sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2;
   1335 		return 0;
   1336 	case SNAPPER_ANALOG:
   1337 		if (sc->sc_mode != 0)
   1338 			return ENXIO;
   1339 
   1340 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
   1341 		strcpy(dip->label.name, AudioNmicrophone);
   1342 		dip->type = AUDIO_MIXER_VALUE;
   1343 		dip->prev = dip->next = AUDIO_MIXER_LAST;
   1344 		dip->un.v.num_channels = 2;
   1345 		return 0;
   1346 	}
   1347 
   1348 	return ENXIO;
   1349 }
   1350 
   1351 static size_t
   1352 snapper_round_buffersize(void *h, int dir, size_t size)
   1353 {
   1354 
   1355 	if (size > 65536)
   1356 		size = 65536;
   1357 	return size;
   1358 }
   1359 
   1360 static int
   1361 snapper_get_props(void *h)
   1362 {
   1363 
   1364 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
   1365 	    AUDIO_PROP_FULLDUPLEX;
   1366 }
   1367 
   1368 static int
   1369 snapper_trigger_output(void *h, void *start, void *end, int bsize,
   1370 		       void (*intr)(void *), void *arg,
   1371 		       const audio_params_t *param)
   1372 {
   1373 	struct snapper_softc *sc;
   1374 	struct dbdma_command *cmd;
   1375 	vaddr_t va;
   1376 	int i, len, intmode;
   1377 
   1378 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
   1379 	sc = h;
   1380 
   1381 	cmd = sc->sc_odmacmd;
   1382 	sc->sc_ointr = intr;
   1383 	sc->sc_oarg = arg;
   1384 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
   1385 
   1386 #ifdef DIAGNOSTIC
   1387 	if (sc->sc_opages > SNAPPER_MAXPAGES)
   1388 		panic("snapper_trigger_output");
   1389 #endif
   1390 
   1391 	va = (vaddr_t)start;
   1392 	len = 0;
   1393 	for (i = sc->sc_opages; i > 0; i--) {
   1394 		len += NBPG;
   1395 		if (len < bsize)
   1396 			intmode = 0;
   1397 		else {
   1398 			len = 0;
   1399 			intmode = DBDMA_INT_ALWAYS;
   1400 		}
   1401 
   1402 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
   1403 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1404 		cmd++;
   1405 		va += NBPG;
   1406 	}
   1407 
   1408 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1409 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1410 	    DBDMA_BRANCH_ALWAYS);
   1411 
   1412 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
   1413 
   1414 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
   1415 
   1416 	return 0;
   1417 }
   1418 
   1419 static int
   1420 snapper_trigger_input(void *h, void *start, void *end, int bsize,
   1421 		      void (*intr)(void *), void *arg,
   1422 		      const audio_params_t *param)
   1423 {
   1424 	struct snapper_softc *sc;
   1425 	struct dbdma_command *cmd;
   1426 	vaddr_t va;
   1427 	int i, len, intmode;
   1428 
   1429 	DPRINTF("trigger_input %p %p 0x%x\n", start, end, bsize);
   1430 	sc = h;
   1431 
   1432 	cmd = sc->sc_idmacmd;
   1433 	sc->sc_iintr = intr;
   1434 	sc->sc_iarg = arg;
   1435 	sc->sc_ipages = ((char *)end - (char *)start) / NBPG;
   1436 
   1437 #ifdef DIAGNOSTIC
   1438 	if (sc->sc_ipages > SNAPPER_MAXPAGES)
   1439 		panic("snapper_trigger_input");
   1440 #endif
   1441 
   1442 	va = (vaddr_t)start;
   1443 	len = 0;
   1444 	for (i = sc->sc_ipages; i > 0; i--) {
   1445 		len += NBPG;
   1446 		if (len < bsize)
   1447 			intmode = 0;
   1448 		else {
   1449 			len = 0;
   1450 			intmode = DBDMA_INT_ALWAYS;
   1451 		}
   1452 
   1453 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_MORE, 0, NBPG, vtophys(va),
   1454 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
   1455 		cmd++;
   1456 		va += NBPG;
   1457 	}
   1458 
   1459 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
   1460 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
   1461 	    DBDMA_BRANCH_ALWAYS);
   1462 
   1463 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_idmacmd));
   1464 
   1465 	dbdma_start(sc->sc_idma, sc->sc_idmacmd);
   1466 
   1467 	return 0;
   1468 }
   1469 
   1470 static void
   1471 snapper_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
   1472 {
   1473        struct snapper_softc *sc = opaque;
   1474 
   1475        *intr = &sc->sc_intr_lock;
   1476        *thread = &sc->sc_lock;
   1477 }
   1478 
   1479 static void
   1480 snapper_set_volume(struct snapper_softc *sc, u_int left, u_int right)
   1481 {
   1482 	u_char regs[6];
   1483 	int l, r;
   1484 
   1485 	left = uimin(255, left);
   1486 	right = uimin(255, right);
   1487 
   1488 	if (sc->sc_mode == SNAPPER_SWVOL) {
   1489 		sc->sc_swvol_l = left;
   1490 		sc->sc_swvol_r = right;
   1491 	} else {
   1492 		/*
   1493 		 * for some insane reason the gain table for master volume and the
   1494 		 * mixer channels is almost identical - just shifted by 4 bits
   1495 		 * so we use the mixer_gain table and bit-twiddle it...
   1496 		 */
   1497 		l = 177 - (left * 178 / 256);
   1498 		regs[0] =  (snapper_mixer_gain[l][0] >> 4);
   1499 		regs[1] = ((snapper_mixer_gain[l][0] & 0x0f) << 4) |
   1500 			   (snapper_mixer_gain[l][1] >> 4);
   1501 		regs[2] = ((snapper_mixer_gain[l][1] & 0x0f) << 4) |
   1502 			   (snapper_mixer_gain[l][2] >> 4);
   1503 
   1504 		r = 177 - (right * 178 / 256);
   1505 		regs[3] =  (snapper_mixer_gain[r][0] >> 4);
   1506 		regs[4] = ((snapper_mixer_gain[r][0] & 0x0f) << 4) |
   1507 			   (snapper_mixer_gain[r][1] >> 4);
   1508 		regs[5] = ((snapper_mixer_gain[r][1] & 0x0f) << 4) |
   1509 			   (snapper_mixer_gain[r][2] >> 4);
   1510 
   1511 		tas3004_write(sc, DEQ_VOLUME, regs);
   1512 
   1513 		DPRINTF("%d %02x %02x %02x : %d %02x %02x %02x\n", l, regs[0],
   1514 		    regs[1], regs[2], r, regs[3], regs[4], regs[5]);
   1515 	}
   1516 
   1517 	sc->sc_vol_l = left;
   1518 	sc->sc_vol_r = right;
   1519 }
   1520 
   1521 static void
   1522 snapper_set_basstreble(struct snapper_softc *sc, u_int val, u_int mode)
   1523 {
   1524 	int i = val & 0xFF;
   1525 	uint8_t reg;
   1526 
   1527 	/*
   1528 	 * Make 128 match the 0 dB point
   1529 	 */
   1530 	i = (i - (128 - (SNAPPER_BASSTAB_0DB << 2))) >> 2;
   1531 	if (i < 0)
   1532 		i = 0;
   1533 	else if (i >= sizeof(snapper_basstab))
   1534 		i = sizeof(snapper_basstab) - 1;
   1535 	reg = snapper_basstab[i];
   1536 
   1537 	if (sc->sc_mode == SNAPPER_IS_TAS3001 &&
   1538 	    mode == DEQ_BASS) {
   1539 	    /*
   1540 	     * XXX -- The TAS3001 bass table is different
   1541 	     *        than the other tables.
   1542 	     */
   1543 	    reg = (reg >> 1) + 5; // map 0x72 -> 0x3E (0 dB)
   1544 	}
   1545 
   1546 	tas3004_write(sc, mode, &reg);
   1547 }
   1548 
   1549 static void
   1550 snapper_set_treble(struct snapper_softc *sc, u_int val)
   1551 {
   1552 	if (sc->sc_treble != (u_char)val) {
   1553 		sc->sc_treble = val;
   1554 		snapper_set_basstreble(sc, val, DEQ_TREBLE);
   1555 	}
   1556 }
   1557 
   1558 static void
   1559 snapper_set_bass(struct snapper_softc *sc, u_int val)
   1560 {
   1561 	if (sc->sc_bass != (u_char)val) {
   1562 		sc->sc_bass = val;
   1563 		snapper_set_basstreble(sc, val, DEQ_BASS);
   1564 	}
   1565 }
   1566 
   1567 
   1568 /*
   1569  * In the mixer gain setting, make 128 correspond to
   1570  * the 0dB value from the table.
   1571  * Note that the table values are complemented.
   1572  */
   1573 #define SNAPPER_MIXER_GAIN_SIZE	(sizeof(snapper_mixer_gain) / \
   1574                                	 sizeof(snapper_mixer_gain[0]))
   1575 #define NORMALIZE(i)	((~(i) & 0xff) - ((~128 & 0xff) - SNAPPER_MIXER_GAIN_0DB))
   1576 #define ADJUST(v, i)	do { \
   1577                 		(v) = NORMALIZE(i);\
   1578 				if ((v) < 0) \
   1579 					(v) = 0; \
   1580 				else if ((v) >= SNAPPER_MIXER_GAIN_SIZE) \
   1581 					(v) = SNAPPER_MIXER_GAIN_SIZE - 1; \
   1582 				\
   1583 			} while (0)
   1584 static void
   1585 snapper_write_mixers(struct snapper_softc *sc)
   1586 {
   1587 	uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
   1588 	int i;
   1589 if (sc->sc_mode > 1) return;
   1590 	/* Left channel of SDIN1 */
   1591 	ADJUST(i, sc->mixer[0]);
   1592 	regs[0] = snapper_mixer_gain[i][0];
   1593 	regs[1] = snapper_mixer_gain[i][1];
   1594 	regs[2] = snapper_mixer_gain[i][2];
   1595 
   1596 	/* Left channel of SDIN2 */
   1597 	ADJUST(i, sc->mixer[1]);
   1598 	regs[3] = snapper_mixer_gain[i][0];
   1599 	regs[4] = snapper_mixer_gain[i][1];
   1600 	regs[5] = snapper_mixer_gain[i][2];
   1601 
   1602 	/* Left channel of analog input */
   1603 	ADJUST(i, sc->mixer[2]);
   1604 	regs[6] = snapper_mixer_gain[i][0];
   1605 	regs[7] = snapper_mixer_gain[i][1];
   1606 	regs[8] = snapper_mixer_gain[i][2];
   1607 
   1608 	tas3004_write(sc, DEQ_MIXER_L, regs);
   1609 
   1610 	/* Right channel of SDIN1 */
   1611 	ADJUST(i, sc->mixer[3]);
   1612 	regs[0] = snapper_mixer_gain[i][0];
   1613 	regs[1] = snapper_mixer_gain[i][1];
   1614 	regs[2] = snapper_mixer_gain[i][2];
   1615 
   1616 	/* Right channel of SDIN2 */
   1617 	ADJUST(i, sc->mixer[4]);
   1618 	regs[3] = snapper_mixer_gain[i][0];
   1619 	regs[4] = snapper_mixer_gain[i][1];
   1620 	regs[5] = snapper_mixer_gain[i][2];
   1621 
   1622 	/* Right channel of analog input */
   1623 	ADJUST(i, sc->mixer[5]);
   1624 	regs[6] = snapper_mixer_gain[i][0];
   1625 	regs[7] = snapper_mixer_gain[i][1];
   1626 	regs[8] = snapper_mixer_gain[i][2];
   1627 
   1628 	tas3004_write(sc, DEQ_MIXER_R, regs);
   1629 }
   1630 
   1631 /*
   1632  * rate = fs = LRCLK
   1633  * SCLK = 64*LRCLK (I2S)
   1634  * MCLK = 256fs (typ. -- changeable)
   1635  *
   1636  * MCLK = clksrc / mdiv
   1637  * SCLK = MCLK / sdiv
   1638  * rate = SCLK / 64    ( = LRCLK = fs)
   1639  */
   1640 
   1641 int
   1642 snapper_set_rate(struct snapper_softc *sc)
   1643 {
   1644 	u_int reg = 0, x;
   1645 	u_int rate = sc->sc_rate;
   1646 	uint32_t wordsize, ows;
   1647 	int MCLK;
   1648 	int clksrc, mdiv, sdiv;
   1649 	int mclk_fs;
   1650 	int timo;
   1651 	uint8_t mcr1;
   1652 
   1653 	switch (rate) {
   1654 	case 44100:
   1655 		clksrc = 45158400;		/* 45MHz */
   1656 		reg = CLKSRC_45MHz;
   1657 		mclk_fs = 256;
   1658 		break;
   1659 
   1660 	case 32000:
   1661 	case 48000:
   1662 	case 96000:
   1663 		clksrc = 49152000;		/* 49MHz */
   1664 		reg = CLKSRC_49MHz;
   1665 		mclk_fs = 256;
   1666 		break;
   1667 
   1668 	default:
   1669 		DPRINTF("snapper_set_rate: invalid rate %u\n", rate);
   1670 		return EINVAL;
   1671 	}
   1672 
   1673 	MCLK = rate * mclk_fs;
   1674 	mdiv = clksrc / MCLK;			/* 4 */
   1675 	sdiv = mclk_fs / 64;			/* 4 */
   1676 
   1677 	switch (mdiv) {
   1678 	case 1:
   1679 		reg |= MCLK_DIV1;
   1680 		break;
   1681 	case 3:
   1682 		reg |= MCLK_DIV3;
   1683 		break;
   1684 	case 5:
   1685 		reg |= MCLK_DIV5;
   1686 		break;
   1687 	default:
   1688 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
   1689 		break;
   1690 	}
   1691 
   1692 	switch (sdiv) {
   1693 	case 1:
   1694 		reg |= SCLK_DIV1;
   1695 		break;
   1696 	case 3:
   1697 		reg |= SCLK_DIV3;
   1698 		break;
   1699 	default:
   1700 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
   1701 		break;
   1702 	}
   1703 
   1704 	reg |= SCLK_MASTER;	/* XXX master mode */
   1705 
   1706 	reg |= SERIAL_64x;
   1707 
   1708 	/* stereo input and output */
   1709 
   1710 	DPRINTF("precision: %d\n", sc->sc_bitspersample);
   1711 	switch(sc->sc_bitspersample) {
   1712 		case 16:
   1713 			wordsize = INPUT_STEREO | INPUT_16BIT |
   1714 			           OUTPUT_STEREO | OUTPUT_16BIT;
   1715 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16;
   1716 			break;
   1717 		case 24:
   1718 			wordsize = INPUT_STEREO | INPUT_24BIT |
   1719 			           OUTPUT_STEREO | OUTPUT_24BIT;
   1720 			mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_24;
   1721 			break;
   1722 		default:
   1723 			printf("%s: unsupported sample size %d\n",
   1724 			    device_xname(sc->sc_dev), sc->sc_bitspersample);
   1725 			return EINVAL;
   1726 	}
   1727 
   1728 	if (sc->sc_mode == SNAPPER_IS_TAS3001)
   1729 		mcr1 |= DEQ_MCR1_ISM_I2S;
   1730 
   1731 	ows = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE);
   1732 
   1733 	DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
   1734 	    ows, wordsize);
   1735 	if (ows != wordsize) {
   1736 		bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE,
   1737 		    wordsize);
   1738 		if (sc->sc_mode != SNAPPER_SWVOL)
   1739 			tas3004_write(sc, DEQ_MCR1, &mcr1);
   1740 	}
   1741 
   1742 	x = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT);
   1743 	if (x == reg)
   1744 		return 0;        /* No change; do nothing. */
   1745 
   1746 	DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
   1747 	    bus_space_read_4(sc->sc_tag, sc->sc_bsh, + I2S_FORMAT), reg);
   1748 
   1749 	/* Clear CLKSTOPPEND. */
   1750 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND);
   1751 
   1752 	x = obio_read_4(KEYLARGO_FCR1);                /* FCR */
   1753 	x &= ~I2S0CLKEN;                /* XXX I2S0 */
   1754 	obio_write_4(KEYLARGO_FCR1, x);
   1755 
   1756 	/* Wait until clock is stopped. */
   1757 	for (timo = 1000; timo > 0; timo--) {
   1758 		if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) &
   1759 		    I2S_INT_CLKSTOPPEND)
   1760 			goto done;
   1761 		delay(1);
   1762 	}
   1763 	DPRINTF("snapper_set_rate: timeout\n");
   1764 done:
   1765 	bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg);
   1766 
   1767 	x = obio_read_4(KEYLARGO_FCR1);
   1768 	x |= I2S0CLKEN;
   1769 	obio_write_4(KEYLARGO_FCR1, x);
   1770 
   1771 	return 0;
   1772 }
   1773 
   1774 const struct tas3004_reg tas3004_initdata = {
   1775 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16 },	/* MCR1 */
   1776 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
   1777 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
   1778 	{ 0x72 },						/* TREBLE */
   1779 	{ 0x72 },						/* BASS */
   1780 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
   1781 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
   1782 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1783 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1784 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1785 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1786 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1787 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1788 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1789 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1790 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1791 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1792 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1793 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1794 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1795 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1796 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1797 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1798 	{ 0, 0, 0 },						/* LLB_GAIN */
   1799 	{ 0, 0, 0 },						/* RLB_GAIN */
   1800 	{ 0 },							/* ACR - line in */
   1801 	{ 2 }							/* MCR2 - AllPass mode since we don't use the equalizer anyway */
   1802 };
   1803 
   1804 const char tas3004_regsize[] = {
   1805 	0,					/* 0x00 */
   1806 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
   1807 	sizeof tas3004_initdata.DRC,		/* 0x02 */
   1808 	0,					/* 0x03 */
   1809 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
   1810 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
   1811 	sizeof tas3004_initdata.BASS,		/* 0x06 */
   1812 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
   1813 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
   1814 	0,					/* 0x09 */
   1815 	sizeof tas3004_initdata.LB0,		/* 0x0a */
   1816 	sizeof tas3004_initdata.LB1,		/* 0x0b */
   1817 	sizeof tas3004_initdata.LB2,		/* 0x0c */
   1818 	sizeof tas3004_initdata.LB3,		/* 0x0d */
   1819 	sizeof tas3004_initdata.LB4,		/* 0x0e */
   1820 	sizeof tas3004_initdata.LB5,		/* 0x0f */
   1821 	sizeof tas3004_initdata.LB6,		/* 0x10 */
   1822 	0,					/* 0x11 */
   1823 	0,					/* 0x12 */
   1824 	sizeof tas3004_initdata.RB0,		/* 0x13 */
   1825 	sizeof tas3004_initdata.RB1,		/* 0x14 */
   1826 	sizeof tas3004_initdata.RB2,		/* 0x15 */
   1827 	sizeof tas3004_initdata.RB3,		/* 0x16 */
   1828 	sizeof tas3004_initdata.RB4,		/* 0x17 */
   1829 	sizeof tas3004_initdata.RB5,		/* 0x18 */
   1830 	sizeof tas3004_initdata.RB6,		/* 0x19 */
   1831 	0,0,0,0, 0,0,
   1832 	0,					/* 0x20 */
   1833 	sizeof tas3004_initdata.LLB,		/* 0x21 */
   1834 	sizeof tas3004_initdata.RLB,		/* 0x22 */
   1835 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
   1836 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
   1837 	0,0,0,0, 0,0,0,0, 0,0,0,
   1838 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   1839 	sizeof tas3004_initdata.ACR,		/* 0x40 */
   1840 	0,					/* 0x41 */
   1841 	0,					/* 0x42 */
   1842 	sizeof tas3004_initdata.MCR2		/* 0x43 */
   1843 };
   1844 
   1845 static int
   1846 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
   1847 {
   1848 	int size;
   1849 	static char regblock[sizeof(struct tas3004_reg)+1];
   1850 
   1851 	if (sc->sc_i2c == NULL)
   1852 		return 0;
   1853 
   1854 	KASSERT(reg < sizeof tas3004_regsize);
   1855 	size = tas3004_regsize[reg];
   1856 	KASSERT(size > 0);
   1857 
   1858 	DPRINTF("reg: %x, %d %d\n", reg, size, ((const char*)data)[0]);
   1859 
   1860 	regblock[0] = reg;
   1861 	memcpy(&regblock[1], data, size);
   1862 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
   1863 		if (reg == DEQ_MIXER_L || reg == DEQ_MIXER_R)
   1864 			size = 3;
   1865 		else if (reg == DEQ_DRC || reg == DEQ_ACR ||
   1866 			 reg == DEQ_MCR2) {
   1867 			/* these registers are not available on TAS3001 */
   1868 			return 0;
   1869 		}
   1870 	}
   1871 	iic_acquire_bus(sc->sc_i2c, 0);
   1872 	iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, regblock, size + 1,
   1873 	    NULL, 0, 0);
   1874 	iic_release_bus(sc->sc_i2c, 0);
   1875 
   1876 	return 0;
   1877 }
   1878 
   1879 static int
   1880 gpio_read(bus_size_t addr)
   1881 {
   1882 
   1883 	if (obio_read_1(addr) & GPIO_DATA)
   1884 		return 1;
   1885 	return 0;
   1886 }
   1887 
   1888 static void
   1889 gpio_write(bus_size_t addr, int val)
   1890 {
   1891 	uint8_t data;
   1892 
   1893 	data = GPIO_DDR_OUTPUT;
   1894 	if (val)
   1895 		data |= GPIO_DATA;
   1896 	obio_write_1(addr, data);
   1897 }
   1898 
   1899 int headphone_active = 0;
   1900 int lineout_active = 0;
   1901 int amp_active = 0;
   1902 
   1903 static void
   1904 snapper_mute_speaker(struct snapper_softc *sc, int mute)
   1905 {
   1906 	int x;
   1907 
   1908 	if (amp_mute) {
   1909 		DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
   1910 
   1911 		if (mute)
   1912 			x = amp_active;		/* mute */
   1913 		else
   1914 			x = amp_active ^ 1;	/* unmute */
   1915 
   1916 		gpio_write(amp_mute, x);
   1917 
   1918 		DPRINTF("%d\n", gpio_read(amp_mute));
   1919 	}
   1920 }
   1921 
   1922 static void
   1923 snapper_mute_headphone(struct snapper_softc *sc, int mute)
   1924 {
   1925 	u_int x;
   1926 
   1927 	if (headphone_mute != 0) {
   1928 		DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
   1929 
   1930 		if (mute)
   1931 			x = headphone_active;	/* mute */
   1932 		else
   1933 			x = headphone_active ^ 1;	/* unmute */
   1934 
   1935 		gpio_write(headphone_mute, x);
   1936 
   1937 		DPRINTF("%d\n", gpio_read(headphone_mute));
   1938 	}
   1939 }
   1940 
   1941 static void
   1942 snapper_mute_lineout(struct snapper_softc *sc, int mute)
   1943 {
   1944 	u_int x;
   1945 
   1946 	if (lineout_mute != 0) {
   1947 		DPRINTF("lineoutmute %d --> ", gpio_read(lineout_mute));
   1948 
   1949 		if (mute)
   1950 			x = lineout_active;	/* mute */
   1951 		else
   1952 			x = lineout_active ^ 1;	/* unmute */
   1953 
   1954 		gpio_write(lineout_mute, x);
   1955 
   1956 		DPRINTF("%d\n", gpio_read(lineout_mute));
   1957 	}
   1958 }
   1959 
   1960 static int
   1961 snapper_cint(void *v)
   1962 {
   1963 	struct snapper_softc *sc = v;
   1964 	u_int sense;
   1965 	int mask = 1 << 0;
   1966 
   1967 	if (headphone_detect != 0) {
   1968 		sense = obio_read_1(headphone_detect);
   1969 		DPRINTF("headphone detect = 0x%x\n", sense);
   1970 
   1971 		if (((sense & 0x02) >> 1) == headphone_detect_active) {
   1972 			DPRINTF("headphone is inserted\n");
   1973 			mask |= 1 << 1;
   1974 			mask &= ~(1 << 0);
   1975 		} else {
   1976 			DPRINTF("headphone is NOT inserted\n");
   1977 		}
   1978 	}
   1979 	if (lineout_detect != 0) {
   1980 		sense = obio_read_1(lineout_detect);
   1981 		DPRINTF("lineout detect = 0x%x\n", sense);
   1982 
   1983 		if (((sense & 0x02) >> 1) == lineout_detect_active) {
   1984 			DPRINTF("lineout is inserted\n");
   1985 			mask |= 1 << 2;
   1986 			mask &= ~(1 << 0);
   1987 		} else {
   1988 			DPRINTF("lineout is NOT inserted\n");
   1989 		}
   1990 	}
   1991 	if (mask != sc->sc_output_mask) {
   1992 		sc->sc_output_mask = mask;
   1993 		if (mask & (1 << 0)) {
   1994 			snapper_mute_speaker(sc, 0);
   1995 		} else snapper_mute_speaker(sc, 1);
   1996 		if (mask & (1 << 1)) {
   1997 			snapper_mute_headphone(sc, 0);
   1998 		} else snapper_mute_headphone(sc, 1);
   1999 		if (mask & (1 << 2)) {
   2000 			snapper_mute_lineout(sc, 0);
   2001 		} else snapper_mute_lineout(sc, 1);
   2002 	}
   2003 	return 1;
   2004 }
   2005 
   2006 #define reset_active 0	/* XXX OF */
   2007 
   2008 #define DEQ_WRITE(sc, reg, addr) \
   2009 	if (tas3004_write(sc, reg, addr)) goto err
   2010 
   2011 static int
   2012 tas3004_init(struct snapper_softc *sc)
   2013 {
   2014 
   2015 	/* No reset port.  Nothing to do. */
   2016 	if (audio_hw_reset == 0)
   2017 		goto noreset;
   2018 
   2019 	/* Reset TAS3004. */
   2020 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   2021 	delay(100000);				/* XXX Really needed? */
   2022 
   2023 	gpio_write(audio_hw_reset, reset_active);	/* Assert RESET */
   2024 	delay(1);
   2025 
   2026 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   2027 	delay(10000);
   2028 
   2029 noreset:
   2030 	if ((sc->sc_mode == SNAPPER_IS_TAS3004) ||
   2031 	    (sc->sc_mode == SNAPPER_IS_TAS3001)) {
   2032 		DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
   2033 		DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
   2034 		DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
   2035 		DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
   2036 		DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
   2037 		DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
   2038 		DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
   2039 		DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
   2040 		DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   2041 		DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   2042 		DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
   2043 		DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
   2044 		DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
   2045 		DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
   2046 		DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
   2047 		DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
   2048 		DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
   2049 		DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
   2050 		DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
   2051 		DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
   2052 		DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
   2053 		DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
   2054 		DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
   2055 		DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
   2056 		DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
   2057 		DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
   2058 		DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
   2059 	}
   2060 	return 0;
   2061 err:
   2062 	printf("tas3004_init: error\n");
   2063 	return -1;
   2064 }
   2065 
   2066 static void
   2067 snapper_init(struct snapper_softc *sc, int node)
   2068 {
   2069 	int gpio;
   2070 	int headphone_detect_intr, lineout_detect_intr;
   2071 	uint32_t gpio_base, reg[1], fcreg, buf[8];
   2072 	char intr_xname[INTRDEVNAMEBUF];
   2073 #ifdef SNAPPER_DEBUG
   2074 	char fcr[32];
   2075 
   2076 	snprintb(fcr, sizeof(fcr),  FCR3C_BITMASK, obio_read_4(KEYLARGO_FCR1));
   2077 	printf("FCR(0x3c) %s\n", fcr);
   2078 #endif
   2079 	fcreg = obio_read_4(KEYLARGO_FCR1);
   2080 	fcreg |= I2S0CLKEN | I2S0EN;
   2081 	obio_write_4(KEYLARGO_FCR1, fcreg);
   2082 
   2083 	headphone_detect_intr = -1;
   2084 	lineout_detect_intr = -1;
   2085 
   2086 	gpio = of_getnode_byname(OF_parent(node), "gpio");
   2087 	if (OF_getprop(gpio, "reg", reg, sizeof(reg)) == sizeof(reg))
   2088 		gpio_base = reg[0];
   2089 	else
   2090 		gpio_base = 0;
   2091 	DPRINTF(" /gpio 0x%x@0x%x\n", (unsigned)gpio, gpio_base);
   2092 
   2093 	gpio = OF_child(gpio);
   2094 	while (gpio) {
   2095 		char name[64], audio_gpio[64], sid[64];
   2096 		int intr[2];
   2097 		bus_size_t addr;
   2098 
   2099 		memset(name, 0, sizeof name);
   2100 		memset(audio_gpio, 0, sizeof audio_gpio);
   2101 		addr = 0;
   2102 		OF_getprop(gpio, "name", name, sizeof name);
   2103 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
   2104 		OF_getprop(gpio, "one-wire-bus", sid, sizeof sid);
   2105 		if (OF_getprop(gpio, "AAPL,address", &addr, sizeof addr) == -1)
   2106 			if (OF_getprop(gpio, "reg", reg, sizeof reg)
   2107 			    == sizeof reg)
   2108 				addr = gpio_base + reg[0];
   2109 		/*
   2110 		 * XXX
   2111 		 * APL,address contains the absolute address, we only want the
   2112 		 * offset from mac-io's base address
   2113 		 */
   2114 		addr &= 0x7fff;
   2115 		DPRINTF(" 0x%x %s %s %08x\n", gpio, name, audio_gpio, addr);
   2116 
   2117 		/* gpio5 */
   2118 		if (strcmp(audio_gpio, "headphone-mute") == 0 ||
   2119 		    strcmp(name, "headphone-mute") == 0) {
   2120 			headphone_mute = addr;
   2121 			if (OF_getprop(gpio,
   2122 			    "platform-do-headphone-mute", buf, 20) == 20) {
   2123 				headphone_active = buf[3] & 1;
   2124 				DPRINTF("platform-do-headphone-mute %d\n",
   2125 				    headphone_active);
   2126 			}
   2127 		}
   2128 		/* gpio6 */
   2129 		if (strcmp(audio_gpio, "amp-mute") == 0 ||
   2130 		    strcmp(name, "amp-mute") == 0) {
   2131 			amp_mute = addr;
   2132 			if (OF_getprop(gpio,
   2133 			    "platform-do-amp-mute", buf, 20) == 20) {
   2134 				amp_active = buf[3] & 1;
   2135 				DPRINTF("platform-do-amp-mute %d\n",
   2136 				    amp_active);
   2137 			}
   2138 		}
   2139 		/* extint-gpio15 */
   2140 		if (strcmp(audio_gpio, "headphone-detect") == 0 ||
   2141 		    strcmp(name, "headphone-detect") == 0) {
   2142 		    	uint32_t act = 0;
   2143 			headphone_detect = addr;
   2144 			OF_getprop(gpio, "audio-gpio-active-state", &act, 4);
   2145 			headphone_detect_active = act;
   2146 			if (OF_getprop(gpio, "interrupts", intr, 8) == 8) {
   2147 				headphone_detect_intr = intr[0];
   2148 			}
   2149 		}
   2150 		if (strcmp(audio_gpio, "lineout-mute") == 0 ||
   2151 		    strcmp(name, "lineout-mute") == 0 ||
   2152 		    strcmp(name, "line-output-mute") == 0) {
   2153 			lineout_mute = addr;
   2154 			if (OF_getprop(gpio,
   2155 			    "platform-do-lineout-mute", buf, 20) == 20) {
   2156 				lineout_active = buf[3] & 1;
   2157 				DPRINTF("platform-do-lineout-mute %d\n",
   2158 				    lineout_active);
   2159 			}
   2160 		}
   2161 		if (strcmp(audio_gpio, "lineout-detect") == 0 ||
   2162 		    strcmp(name, "lineout-detect") == 0 ||
   2163 		    strcmp(name, "line-output-detect") == 0) {
   2164 		    	uint32_t act = 0;
   2165 			lineout_detect = addr;
   2166 			OF_getprop(gpio, "audio-gpio-active-state", &act, 4);
   2167 			lineout_detect_active = act;
   2168 			if (OF_getprop(gpio, "interrupts", intr, 8) == 8) {
   2169 				lineout_detect_intr = intr[0];
   2170 			}
   2171 		}
   2172 		/* extint-gpio16 on Quicksilver */
   2173 		if (strcmp(sid, "speaker-id") == 0) {
   2174 			owaddr = addr;
   2175 		}
   2176 		/* gpio11 (keywest-11) */
   2177 		if (strcmp(audio_gpio, "audio-hw-reset") == 0 ||
   2178 		    strcmp(name, "hw-reset") == 0)
   2179 			audio_hw_reset = addr;
   2180 
   2181 		gpio = OF_peer(gpio);
   2182 	}
   2183 
   2184 	if (owaddr != -1) snapper_setup_ow(sc);
   2185 
   2186 	DPRINTF(" headphone-mute %x\n", headphone_mute);
   2187 	DPRINTF(" lineout-mute %x\n", lineout_mute);
   2188 	DPRINTF(" amp-mute %x\n", amp_mute);
   2189 	DPRINTF(" headphone-detect %x\n", headphone_detect);
   2190 	DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
   2191 	DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
   2192 	DPRINTF(" lineout-detect %x\n", lineout_detect);
   2193 	DPRINTF(" lineout-detect active %x\n", lineout_detect_active);
   2194 	DPRINTF(" lineout-detect intr %x\n", lineout_detect_intr);
   2195 	DPRINTF(" audio-hw-reset %x\n", audio_hw_reset);
   2196 
   2197 	if (headphone_detect_intr != -1) {
   2198 		snprintf(intr_xname, sizeof(intr_xname), "%s headphone",
   2199 		    device_xname(sc->sc_dev));
   2200 		intr_establish_xname(headphone_detect_intr, IST_EDGE,
   2201 		    IPL_AUDIO, snapper_cint, sc, intr_xname);
   2202 	}
   2203 
   2204 	if (lineout_detect_intr != -1) {
   2205 		snprintf(intr_xname, sizeof(intr_xname), "%s line out",
   2206 		    device_xname(sc->sc_dev));
   2207 		intr_establish_xname(lineout_detect_intr, IST_EDGE, IPL_AUDIO,
   2208 		    snapper_cint, sc, intr_xname);
   2209 	}
   2210 
   2211 	sc->sc_rate = 44100;	/* default rate */
   2212 	sc->sc_bitspersample = 16;
   2213 
   2214 	/* Enable headphone interrupt? */
   2215 	if (headphone_detect != 0) {
   2216 		obio_write_1(headphone_detect,
   2217 		    obio_read_1(headphone_detect) | 0x80);
   2218 	}
   2219 	if (lineout_detect != 0) {
   2220 		obio_write_1(lineout_detect,
   2221 		    obio_read_1(lineout_detect) | 0x80);
   2222 	}
   2223 
   2224 	if (tas3004_init(sc))
   2225 		return;
   2226 
   2227 	/* Update headphone status. */
   2228 	snapper_cint(sc);
   2229 
   2230 	snapper_set_volume(sc, 128, 128);
   2231 	snapper_set_bass(sc, 128);
   2232 	snapper_set_treble(sc, 128);
   2233 
   2234 	/* Record source defaults to line in.  This reflects the
   2235 	 * default value for the ACR (see tas3004_initdata).
   2236 	 */
   2237 	sc->sc_record_source = 1 << 1;
   2238 
   2239 	/* We mute the analog input for now */
   2240 	sc->mixer[0] = 128;
   2241 	sc->mixer[1] = 128;
   2242 	sc->mixer[2] = 0;
   2243 	if (sc->sc_mode == SNAPPER_IS_TAS3001) {
   2244 		sc->mixer[3] = 0;
   2245 	} else
   2246 		sc->mixer[3] = 128;
   2247 	sc->mixer[4] = 128;
   2248 	sc->mixer[5] = 0;
   2249 	snapper_write_mixers(sc);
   2250 }
   2251 
   2252 static void
   2253 snapper_setup_ow(struct snapper_softc *sc)
   2254 {
   2255 	struct onewirebus_attach_args oba;
   2256 
   2257 	/* Attach 1-Wire bus */
   2258 	sc->sc_ow_bus.bus_cookie = sc;
   2259 	sc->sc_ow_bus.bus_reset = snapper_ow_reset;
   2260 	sc->sc_ow_bus.bus_read_bit = snapper_ow_read_bit;
   2261 	sc->sc_ow_bus.bus_write_bit = snapper_ow_write_bit;
   2262 
   2263 	memset(&oba, 0, sizeof(oba));
   2264 	oba.oba_bus = &sc->sc_ow_bus;
   2265 	sc->sc_ow_dev = config_found(sc->sc_dev, &oba, onewirebus_print,
   2266 	    CFARGS(.iattr = "onewirebus"));
   2267 }
   2268 
   2269 static int
   2270 snapper_ow_reset(void *cookie)
   2271 {
   2272 	return (onewire_bb_reset(&snapper_bbops, cookie));
   2273 }
   2274 
   2275 static int
   2276 snapper_ow_read_bit(void *cookie)
   2277 {
   2278 	return (onewire_bb_read_bit(&snapper_bbops, cookie));
   2279 }
   2280 
   2281 static void
   2282 snapper_ow_write_bit(void *cookie, int bit)
   2283 {
   2284 	onewire_bb_write_bit(&snapper_bbops, cookie, bit);
   2285 }
   2286 
   2287 static void
   2288 snapper_bb_rx(void *cookie)
   2289 {
   2290 	struct snapper_softc *sc = cookie;
   2291 
   2292 	sc->sc_ow_data &= ~GPIO_DDR_OUTPUT;
   2293 	obio_write_1(owaddr, sc->sc_ow_data);
   2294 }
   2295 
   2296 static void
   2297 snapper_bb_tx(void *cookie)
   2298 {
   2299 	struct snapper_softc *sc = cookie;
   2300 
   2301 	sc->sc_ow_data |= GPIO_DDR_OUTPUT;
   2302 	obio_write_1(owaddr, sc->sc_ow_data);
   2303 }
   2304 
   2305 static int snapper_bb_get(void *cookie)
   2306 {
   2307 	int data = (obio_read_1(owaddr) & GPIO_LEVEL) ? 1 : 0;
   2308 	return data;
   2309 }
   2310 
   2311 static void snapper_bb_set(void *cookie, int bit)
   2312 {
   2313 	struct snapper_softc *sc = cookie;
   2314 
   2315 	if (bit) {
   2316 		sc->sc_ow_data |= GPIO_DATA;
   2317 	} else
   2318 		sc->sc_ow_data &= ~GPIO_DATA;
   2319 
   2320 	obio_write_1(owaddr, sc->sc_ow_data);
   2321 }
   2322