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