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