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