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