xref: /openbsd/sys/arch/macppc/dev/snapper.c (revision 73471bf0)
1 /*	$OpenBSD: snapper.c,v 1.39 2020/08/26 03:29:06 visa Exp $	*/
2 /*	$NetBSD: snapper.c,v 1.1 2003/12/27 02:19:34 grant Exp $	*/
3 
4 /*-
5  * Copyright (c) 2002 Tsubai Masanari.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Datasheet is available from
32  * http://focus.ti.com/docs/prod/folders/print/tas3004.html
33  */
34 
35 #include <sys/param.h>
36 #include <sys/audioio.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 
40 #include <dev/audio_if.h>
41 #include <dev/ofw/openfirm.h>
42 #include <macppc/dev/dbdma.h>
43 
44 #include <machine/autoconf.h>
45 
46 #include <macppc/dev/i2svar.h>
47 
48 #ifdef SNAPPER_DEBUG
49 # define DPRINTF printf
50 #else
51 # define DPRINTF while (0) printf
52 #endif
53 
54 /* XXX */
55 #define snapper_softc i2s_softc
56 
57 /* XXX */
58 int kiic_write(struct device *, int, int, const void *, int);
59 int kiic_writereg(struct device *, int, u_int);
60 
61 void snapper_init(struct snapper_softc *);
62 int snapper_match(struct device *, void *, void *);
63 void snapper_attach(struct device *, struct device *, void *);
64 void snapper_defer(struct device *);
65 void snapper_set_volume(struct snapper_softc *, int, int);
66 void snapper_set_bass(struct snapper_softc *, int);
67 void snapper_set_treble(struct snapper_softc *, int);
68 void snapper_set_input(struct snapper_softc *, int);
69 
70 int tas3004_write(struct snapper_softc *, u_int, const void *);
71 int tas3004_init(struct snapper_softc *);
72 
73 struct cfattach snapper_ca = {
74 	sizeof(struct snapper_softc), snapper_match, snapper_attach
75 };
76 struct cfdriver snapper_cd = {
77 	NULL, "snapper", DV_DULL
78 };
79 
80 struct audio_hw_if snapper_hw_if = {
81 	i2s_open,
82 	i2s_close,
83 	i2s_set_params,
84 	i2s_round_blocksize,
85 	NULL,
86 	NULL,
87 	NULL,
88 	NULL,
89 	NULL,
90 	i2s_halt_output,
91 	i2s_halt_input,
92 	NULL,
93 	NULL,
94 	i2s_set_port,
95 	i2s_get_port,
96 	i2s_query_devinfo,
97 	i2s_allocm,		/* allocm */
98 	NULL,
99 	i2s_round_buffersize,
100 	i2s_get_props,
101 	i2s_trigger_output,
102 	i2s_trigger_input
103 };
104 
105 const uint8_t snapper_trebletab[] = {
106 	0x96,	/* -18dB */
107 	0x94,	/* -17dB */
108 	0x92,	/* -16dB */
109 	0x90,	/* -15dB */
110 	0x8e,	/* -14dB */
111 	0x8c,	/* -13dB */
112 	0x8a,	/* -12dB */
113 	0x88,	/* -11dB */
114 	0x86,	/* -10dB */
115 	0x84,	/* -9dB */
116 	0x82,	/* -8dB */
117 	0x80,	/* -7dB */
118 	0x7e,	/* -6dB */
119 	0x7c,	/* -5dB */
120 	0x7a,	/* -4dB */
121 	0x78,	/* -3dB */
122 	0x76,	/* -2dB */
123 	0x74,	/* -1dB */
124 	0x72,	/* 0dB */
125 	0x70,	/* 1dB */
126 	0x6d,	/* 2dB */
127 	0x6b,	/* 3dB */
128 	0x68,	/* 4dB */
129 	0x65,	/* 5dB */
130 	0x62,	/* 6dB */
131 	0x5d,	/* 7dB */
132 	0x59,	/* 8dB */
133 	0x53,	/* 9dB */
134 	0x4d,	/* 10dB */
135 	0x47,	/* 11dB */
136 	0x3f,	/* 12dB */
137 	0x36,	/* 13dB */
138 	0x2c,	/* 14dB */
139 	0x20,	/* 15dB */
140 	0x13,	/* 16dB */
141 	0x04,	/* 17dB */
142 	0x01,	/* 18dB */
143 };
144 
145 const uint8_t snapper_basstab[] = {
146 	0x96,	/* -18dB */
147 	0x94,	/* -17dB */
148 	0x92,	/* -16dB */
149 	0x90,	/* -15dB */
150 	0x8e,	/* -14dB */
151 	0x8c,	/* -13dB */
152 	0x8a,	/* -12dB */
153 	0x88,	/* -11dB */
154 	0x86,	/* -10dB */
155 	0x84,	/* -9dB */
156 	0x82,	/* -8dB */
157 	0x80,	/* -7dB */
158 	0x7e,	/* -6dB */
159 	0x7c,	/* -5dB */
160 	0x7a,	/* -4dB */
161 	0x78,	/* -3dB */
162 	0x76,	/* -2dB */
163 	0x74,	/* -1dB */
164 	0x72,	/* 0dB */
165 	0x6f,	/* 1dB */
166 	0x6d,	/* 2dB */
167 	0x6a,	/* 3dB */
168 	0x67,	/* 4dB */
169 	0x65,	/* 5dB */
170 	0x62,	/* 6dB */
171 	0x5f,	/* 7dB */
172 	0x5b,	/* 8dB */
173 	0x55,	/* 9dB */
174 	0x4f,	/* 10dB */
175 	0x49,	/* 11dB */
176 	0x43,	/* 12dB */
177 	0x3b,	/* 13dB */
178 	0x33,	/* 14dB */
179 	0x29,	/* 15dB */
180 	0x1e,	/* 16dB */
181 	0x11,	/* 17dB */
182 	0x01,	/* 18dB */
183 };
184 
185 struct {
186 	int high, mid, low;
187 } snapper_volumetab[] = {
188 	{ 0x07, 0xF1, 0x7B }, /* 18.0 */
189 	{ 0x07, 0x7F, 0xBB }, /* 17.5 */
190 	{ 0x07, 0x14, 0x57 }, /* 17.0 */
191 	{ 0x06, 0xAE, 0xF6 }, /* 16.5 */
192 	{ 0x06, 0x4F, 0x40 }, /* 16.0 */
193 	{ 0x05, 0xF4, 0xE5 }, /* 15.5 */
194 	{ 0x05, 0x9F, 0x98 }, /* 15.0 */
195 	{ 0x05, 0x4F, 0x10 }, /* 14.5 */
196 	{ 0x05, 0x03, 0x0A }, /* 14.0 */
197 	{ 0x04, 0xBB, 0x44 }, /* 13.5 */
198 	{ 0x04, 0x77, 0x83 }, /* 13.0 */
199 	{ 0x04, 0x37, 0x8B }, /* 12.5 */
200 	{ 0x03, 0xFB, 0x28 }, /* 12.0 */
201 	{ 0x03, 0xC2, 0x25 }, /* 11.5 */
202 	{ 0x03, 0x8C, 0x53 }, /* 11.0 */
203 	{ 0x03, 0x59, 0x83 }, /* 10.5 */
204 	{ 0x03, 0x29, 0x8B }, /* 10.0 */
205 	{ 0x02, 0xFC, 0x42 }, /* 9.5 */
206 	{ 0x02, 0xD1, 0x82 }, /* 9.0 */
207 	{ 0x02, 0xA9, 0x25 }, /* 8.5 */
208 	{ 0x02, 0x83, 0x0B }, /* 8.0 */
209 	{ 0x02, 0x5F, 0x12 }, /* 7.5 */
210 	{ 0x02, 0x3D, 0x1D }, /* 7.0 */
211 	{ 0x02, 0x1D, 0x0E }, /* 6.5 */
212 	{ 0x01, 0xFE, 0xCA }, /* 6.0 */
213 	{ 0x01, 0xE2, 0x37 }, /* 5.5 */
214 	{ 0x01, 0xC7, 0x3D }, /* 5.0 */
215 	{ 0x01, 0xAD, 0xC6 }, /* 4.5 */
216 	{ 0x01, 0x95, 0xBC }, /* 4.0 */
217 	{ 0x01, 0x7F, 0x09 }, /* 3.5 */
218 	{ 0x01, 0x69, 0x9C }, /* 3.0 */
219 	{ 0x01, 0x55, 0x62 }, /* 2.5 */
220 	{ 0x01, 0x42, 0x49 }, /* 2.0 */
221 	{ 0x01, 0x30, 0x42 }, /* 1.5 */
222 	{ 0x01, 0x1F, 0x3D }, /* 1.0 */
223 	{ 0x01, 0x0F, 0x2B }, /* 0.5 */
224 	{ 0x01, 0x00, 0x00 }, /* 0.0 */
225 	{ 0x00, 0xF1, 0xAE }, /* -0.5 */
226 	{ 0x00, 0xE4, 0x29 }, /* -1.0 */
227 	{ 0x00, 0xD7, 0x66 }, /* -1.5 */
228 	{ 0x00, 0xCB, 0x59 }, /* -2.0 */
229 	{ 0x00, 0xBF, 0xF9 }, /* -2.5 */
230 	{ 0x00, 0xB5, 0x3C }, /* -3.0 */
231 	{ 0x00, 0xAB, 0x19 }, /* -3.5 */
232 	{ 0x00, 0xA1, 0x86 }, /* -4.0 */
233 	{ 0x00, 0x98, 0x7D }, /* -4.5 */
234 	{ 0x00, 0x8F, 0xF6 }, /* -5.0 */
235 	{ 0x00, 0x87, 0xE8 }, /* -5.5 */
236 	{ 0x00, 0x80, 0x4E }, /* -6.0 */
237 	{ 0x00, 0x79, 0x20 }, /* -6.5 */
238 	{ 0x00, 0x72, 0x5A }, /* -7.0 */
239 	{ 0x00, 0x6B, 0xF4 }, /* -7.5 */
240 	{ 0x00, 0x65, 0xEA }, /* -8.0 */
241 	{ 0x00, 0x60, 0x37 }, /* -8.5 */
242 	{ 0x00, 0x5A, 0xD5 }, /* -9.0 */
243 	{ 0x00, 0x55, 0xC0 }, /* -9.5 */
244 	{ 0x00, 0x50, 0xF4 }, /* -10.0 */
245 	{ 0x00, 0x4C, 0x6D }, /* -10.5 */
246 	{ 0x00, 0x48, 0x27 }, /* -11.0 */
247 	{ 0x00, 0x44, 0x1D }, /* -11.5 */
248 	{ 0x00, 0x40, 0x4E }, /* -12.0 */
249 	{ 0x00, 0x3C, 0xB5 }, /* -12.5 */
250 	{ 0x00, 0x39, 0x50 }, /* -13.0 */
251 	{ 0x00, 0x36, 0x1B }, /* -13.5 */
252 	{ 0x00, 0x33, 0x14 }, /* -14.0 */
253 	{ 0x00, 0x30, 0x39 }, /* -14.5 */
254 	{ 0x00, 0x2D, 0x86 }, /* -15.0 */
255 	{ 0x00, 0x2A, 0xFA }, /* -15.5 */
256 	{ 0x00, 0x28, 0x93 }, /* -16.0 */
257 	{ 0x00, 0x26, 0x4E }, /* -16.5 */
258 	{ 0x00, 0x24, 0x29 }, /* -17.0 */
259 	{ 0x00, 0x22, 0x23 }, /* -17.5 */
260 	{ 0x00, 0x20, 0x3A }, /* -18.0 */
261 	{ 0x00, 0x1E, 0x6D }, /* -18.5 */
262 	{ 0x00, 0x1C, 0xB9 }, /* -19.0 */
263 	{ 0x00, 0x1B, 0x1E }, /* -19.5 */
264 	{ 0x00, 0x19, 0x9A }, /* -20.0 */
265 	{ 0x00, 0x18, 0x2B }, /* -20.5 */
266 	{ 0x00, 0x16, 0xD1 }, /* -21.0 */
267 	{ 0x00, 0x15, 0x8A }, /* -21.5 */
268 	{ 0x00, 0x14, 0x56 }, /* -22.0 */
269 	{ 0x00, 0x13, 0x33 }, /* -22.5 */
270 	{ 0x00, 0x12, 0x20 }, /* -23.0 */
271 	{ 0x00, 0x11, 0x1C }, /* -23.5 */
272 	{ 0x00, 0x10, 0x27 }, /* -24.0 */
273 	{ 0x00, 0x0F, 0x40 }, /* -24.5 */
274 	{ 0x00, 0x0E, 0x65 }, /* -25.0 */
275 	{ 0x00, 0x0D, 0x97 }, /* -25.5 */
276 	{ 0x00, 0x0C, 0xD5 }, /* -26.0 */
277 	{ 0x00, 0x0C, 0x1D }, /* -26.5 */
278 	{ 0x00, 0x0B, 0x6F }, /* -27.0 */
279 	{ 0x00, 0x0A, 0xCC }, /* -27.5 */
280 	{ 0x00, 0x0A, 0x31 }, /* -28.0 */
281 	{ 0x00, 0x09, 0x9F }, /* -28.5 */
282 	{ 0x00, 0x09, 0x15 }, /* -29.0 */
283 	{ 0x00, 0x08, 0x93 }, /* -29.5 */
284 	{ 0x00, 0x08, 0x18 }, /* -30.0 */
285 	{ 0x00, 0x07, 0xA5 }, /* -30.5 */
286 	{ 0x00, 0x07, 0x37 }, /* -31.0 */
287 	{ 0x00, 0x06, 0xD0 }, /* -31.5 */
288 	{ 0x00, 0x06, 0x6E }, /* -32.0 */
289 	{ 0x00, 0x06, 0x12 }, /* -32.5 */
290 	{ 0x00, 0x05, 0xBB }, /* -33.0 */
291 	{ 0x00, 0x05, 0x69 }, /* -33.5 */
292 	{ 0x00, 0x05, 0x1C }, /* -34.0 */
293 	{ 0x00, 0x04, 0xD2 }, /* -34.5 */
294 	{ 0x00, 0x04, 0x8D }, /* -35.0 */
295 	{ 0x00, 0x04, 0x4C }, /* -35.5 */
296 	{ 0x00, 0x04, 0x0F }, /* -36.0 */
297 	{ 0x00, 0x03, 0xD5 }, /* -36.5 */
298 	{ 0x00, 0x03, 0x9E }, /* -37.0 */
299 	{ 0x00, 0x03, 0x6A }, /* -37.5 */
300 	{ 0x00, 0x03, 0x39 }, /* -38.0 */
301 	{ 0x00, 0x03, 0x0B }, /* -38.5 */
302 	{ 0x00, 0x02, 0xDF }, /* -39.0 */
303 	{ 0x00, 0x02, 0xB6 }, /* -39.5 */
304 	{ 0x00, 0x02, 0x8F }, /* -40.0 */
305 	{ 0x00, 0x02, 0x6B }, /* -40.5 */
306 	{ 0x00, 0x02, 0x48 }, /* -41.0 */
307 	{ 0x00, 0x02, 0x27 }, /* -41.5 */
308 	{ 0x00, 0x02, 0x09 }, /* -42.0 */
309 	{ 0x00, 0x01, 0xEB }, /* -42.5 */
310 	{ 0x00, 0x01, 0xD0 }, /* -43.0 */
311 	{ 0x00, 0x01, 0xB6 }, /* -43.5 */
312 	{ 0x00, 0x01, 0x9E }, /* -44.0 */
313 	{ 0x00, 0x01, 0x86 }, /* -44.5 */
314 	{ 0x00, 0x01, 0x71 }, /* -45.0 */
315 	{ 0x00, 0x01, 0x5C }, /* -45.5 */
316 	{ 0x00, 0x01, 0x48 }, /* -46.0 */
317 	{ 0x00, 0x01, 0x36 }, /* -46.5 */
318 	{ 0x00, 0x01, 0x25 }, /* -47.0 */
319 	{ 0x00, 0x01, 0x14 }, /* -47.5 */
320 	{ 0x00, 0x01, 0x05 }, /* -48.0 */
321 	{ 0x00, 0x00, 0xF6 }, /* -48.5 */
322 	{ 0x00, 0x00, 0xE9 }, /* -49.0 */
323 	{ 0x00, 0x00, 0xDC }, /* -49.5 */
324 	{ 0x00, 0x00, 0xCF }, /* -50.0 */
325 	{ 0x00, 0x00, 0xC4 }, /* -50.5 */
326 	{ 0x00, 0x00, 0xB9 }, /* -51.0 */
327 	{ 0x00, 0x00, 0xAE }, /* -51.5 */
328 	{ 0x00, 0x00, 0xA5 }, /* -52.0 */
329 	{ 0x00, 0x00, 0x9B }, /* -52.5 */
330 	{ 0x00, 0x00, 0x93 }, /* -53.0 */
331 	{ 0x00, 0x00, 0x8B }, /* -53.5 */
332 	{ 0x00, 0x00, 0x83 }, /* -54.0 */
333 	{ 0x00, 0x00, 0x7B }, /* -54.5 */
334 	{ 0x00, 0x00, 0x75 }, /* -55.0 */
335 	{ 0x00, 0x00, 0x6E }, /* -55.5 */
336 	{ 0x00, 0x00, 0x68 }, /* -56.0 */
337 	{ 0x00, 0x00, 0x62 }, /* -56.5 */
338 	{ 0x00, 0x00, 0x0 } /* Mute? */
339 
340 };
341 
342 /* TAS3004 registers */
343 #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
344 #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?) */
345 #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
346 #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
347 #define DEQ_BASS	0x06	/* Bass control (1byte) */
348 #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes) */
349 #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes) */
350 #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
351 #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
352 #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
353 #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
354 #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
355 #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
356 #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
357 #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
358 #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
359 #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
360 #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
361 #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
362 #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
363 #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
364 #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
365 #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
366 #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
367 #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
368 #define DEQ_ACR		0x40	/* Analog control register (1byte) */
369 #define DEQ_MCR2	0x43	/* Main control register 2 (1byte) */
370 
371 #define DEQ_MCR1_FL	0x80	/* Fast load */
372 #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
373 #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
374 #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
375 #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
376 #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
377 #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
378 #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
379 #define DEQ_MCR1_W	0x03	/* Serial port word length */
380 #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
381 #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
382 #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
383 
384 #define DEQ_MCR2_DL	0x80	/* Download */
385 #define DEQ_MCR2_AP	0x02	/* All pass mode */
386 
387 #define DEQ_ACR_ADM	0x80	/* ADC output mode */
388 #define DEQ_ACR_LRB	0x40	/* Select B input */
389 #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
390 #define  DEQ_ACR_DM_OFF	0x00	/*  off */
391 #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
392 #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
393 #define DEQ_ACR_INP	0x02	/* Analog input select */
394 #define  DEQ_ACR_INP_A	0x00	/*  A */
395 #define  DEQ_ACR_INP_B	0x02	/*  B */
396 #define DEQ_ACR_APD	0x01	/* Analog power down */
397 
398 struct tas3004_reg {
399 	u_char MCR1[1];
400 	u_char DRC[6];
401 	u_char VOLUME[6];
402 	u_char TREBLE[1];
403 	u_char BASS[1];
404 	u_char MIXER_L[9];
405 	u_char MIXER_R[9];
406 	u_char LB0[15];
407 	u_char LB1[15];
408 	u_char LB2[15];
409 	u_char LB3[15];
410 	u_char LB4[15];
411 	u_char LB5[15];
412 	u_char LB6[15];
413 	u_char RB0[15];
414 	u_char RB1[15];
415 	u_char RB2[15];
416 	u_char RB3[15];
417 	u_char RB4[15];
418 	u_char RB5[15];
419 	u_char RB6[15];
420 	u_char LLB[15];
421 	u_char RLB[15];
422 	u_char LLB_GAIN[3];
423 	u_char RLB_GAIN[3];
424 	u_char ACR[1];
425 	u_char MCR2[1];
426 };
427 
428 int
429 snapper_match(struct device *parent, void *match, void *aux)
430 {
431 	struct confargs *ca = aux;
432 	int soundbus, soundchip, soundcodec;
433 	char compat[32];
434 
435 	if (strcmp(ca->ca_name, "i2s") != 0)
436 		return (0);
437 
438 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
439 	    (soundchip = OF_child(soundbus)) == 0)
440 		return (0);
441 
442 	bzero(compat, sizeof compat);
443 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
444 
445 	if (strcmp(compat, "AOAKeylargo") == 0 &&
446 	    strcmp(hw_prod, "PowerBook5,4") == 0)
447 		return (1);
448 	if (strcmp(compat, "snapper") == 0)
449 		return (1);
450 
451 	if (OF_getprop(soundchip, "platform-tas-codec-ref",
452 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
453 		return (1);
454 
455 	return (0);
456 }
457 
458 void
459 snapper_attach(struct device *parent, struct device *self, void *aux)
460 {
461 	struct snapper_softc *sc = (struct snapper_softc *)self;
462 
463 	sc->sc_setvolume = snapper_set_volume;
464 	sc->sc_setbass = snapper_set_bass;
465 	sc->sc_settreble = snapper_set_treble;
466 	sc->sc_setinput = snapper_set_input;
467 
468 	i2s_attach(parent, sc, aux);
469 	config_defer(self, snapper_defer);
470 }
471 
472 void
473 snapper_defer(struct device *dev)
474 {
475 	struct snapper_softc *sc = (struct snapper_softc *)dev;
476 	struct device *dv;
477 
478 	TAILQ_FOREACH(dv, &alldevs, dv_list)
479 		if (strcmp(dv->dv_cfdata->cf_driver->cd_name, "kiic") == 0 &&
480 		    strcmp(dv->dv_parent->dv_cfdata->cf_driver->cd_name, "macobio") == 0)
481 			sc->sc_i2c = dv;
482 	if (sc->sc_i2c == NULL) {
483 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
484 		return;
485 	}
486 
487 	/* XXX If i2c has failed to attach, what should we do? */
488 
489 	audio_attach_mi(&snapper_hw_if, sc, &sc->sc_dev);
490 
491 	/* kiic_setmode(sc->sc_i2c, I2C_STDSUBMODE); */
492 	snapper_init(sc);
493 }
494 
495 void
496 snapper_set_volume(struct snapper_softc *sc, int left, int right)
497 {
498 	u_char vol[6];
499 	int nentries = sizeof(snapper_volumetab) / sizeof(snapper_volumetab[0]);
500 	int l, r;
501 
502 	sc->sc_vol_l = left;
503 	sc->sc_vol_r = right;
504 
505 	l = nentries - (left * nentries / 256);
506 	r = nentries - (right * nentries / 256);
507 
508 	DPRINTF(" left %d vol %d %d, right %d vol %d %d\n",
509 		left, l, nentries,
510 		right, r, nentries);
511 	if (l >= nentries)
512 		l = nentries-1;
513 	if (r >= nentries)
514 		r = nentries-1;
515 
516 	vol[0] = snapper_volumetab[l].high;
517 	vol[1] = snapper_volumetab[l].mid;
518 	vol[2] = snapper_volumetab[l].low;
519 	vol[3] = snapper_volumetab[r].high;
520 	vol[4] = snapper_volumetab[r].mid;
521 	vol[5] = snapper_volumetab[r].low;
522 
523 	tas3004_write(sc, DEQ_VOLUME, vol);
524 }
525 
526 void
527 snapper_set_treble(struct snapper_softc *sc, int value)
528 {
529 	uint8_t reg;
530 
531 	if ((value >= 0) && (value <= 255) && (value != sc->sc_treble)) {
532 		reg = snapper_trebletab[(value >> 3) + 2];
533 		if (tas3004_write(sc, DEQ_TREBLE, &reg) < 0)
534 			return;
535 		sc->sc_treble = value;
536 	}
537 }
538 
539 void
540 snapper_set_bass(struct snapper_softc *sc, int value)
541 {
542 	uint8_t reg;
543 
544 	if ((value >= 0) && (value <= 255) && (value != sc->sc_bass)) {
545 		reg = snapper_basstab[(value >> 3) + 2];
546 		if (tas3004_write(sc, DEQ_BASS, &reg) < 0)
547 			return;
548 		sc->sc_bass = value;
549 	}
550 }
551 
552 void
553 snapper_set_input(struct snapper_softc *sc, int mask)
554 {
555 	uint8_t val = 0;
556 
557 	switch (mask) {
558 	case    1 << 0: /* microphone */
559 		val = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B;
560 		break;
561 	case    1 << 1: /* line in */
562 		val = 0;
563 		break;
564 	}
565 	tas3004_write(sc, DEQ_ACR, &val);
566 }
567 
568 const struct tas3004_reg tas3004_initdata = {
569 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_20 },	/* MCR1 */
570 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
571 	{ 0x00, 0xd7, 0x66, 0x00, 0xd7, 0x66 },			/* VOLUME */
572 	{ 0x72 },						/* TREBLE */
573 	{ 0x72 },						/* BASS */
574 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
575 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
576 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
577 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
578 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
579 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
580 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
581 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
582 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
583 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
584 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
585 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
586 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
587 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
588 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
589 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
590 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
591 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
592 	{ 0, 0, 0 },						/* LLB_GAIN */
593 	{ 0, 0, 0 },						/* RLB_GAIN */
594 	{ DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B },		/* ACR */
595 	{ 0 }							/* MCR2 */
596 };
597 
598 const char tas3004_regsize[] = {
599 	0,					/* 0x00 */
600 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
601 	sizeof tas3004_initdata.DRC,		/* 0x02 */
602 	0,					/* 0x03 */
603 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
604 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
605 	sizeof tas3004_initdata.BASS,		/* 0x06 */
606 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
607 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
608 	0,					/* 0x09 */
609 	sizeof tas3004_initdata.LB0,		/* 0x0a */
610 	sizeof tas3004_initdata.LB1,		/* 0x0b */
611 	sizeof tas3004_initdata.LB2,		/* 0x0c */
612 	sizeof tas3004_initdata.LB3,		/* 0x0d */
613 	sizeof tas3004_initdata.LB4,		/* 0x0e */
614 	sizeof tas3004_initdata.LB5,		/* 0x0f */
615 	sizeof tas3004_initdata.LB6,		/* 0x10 */
616 	0,					/* 0x11 */
617 	0,					/* 0x12 */
618 	sizeof tas3004_initdata.RB0,		/* 0x13 */
619 	sizeof tas3004_initdata.RB1,		/* 0x14 */
620 	sizeof tas3004_initdata.RB2,		/* 0x15 */
621 	sizeof tas3004_initdata.RB3,		/* 0x16 */
622 	sizeof tas3004_initdata.RB4,		/* 0x17 */
623 	sizeof tas3004_initdata.RB5,		/* 0x18 */
624 	sizeof tas3004_initdata.RB6,		/* 0x19 */
625 	0,0,0,0, 0,0,
626 	0,					/* 0x20 */
627 	sizeof tas3004_initdata.LLB,		/* 0x21 */
628 	sizeof tas3004_initdata.RLB,		/* 0x22 */
629 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
630 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
631 	0,0,0,0, 0,0,0,0, 0,0,0,
632 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
633 	sizeof tas3004_initdata.ACR,		/* 0x40 */
634 	0,					/* 0x41 */
635 	0,					/* 0x42 */
636 	sizeof tas3004_initdata.MCR2		/* 0x43 */
637 };
638 
639 #define DEQaddr 0x6a
640 
641 int
642 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
643 {
644 	int size;
645 
646 	KASSERT(reg < sizeof tas3004_regsize);
647 	size = tas3004_regsize[reg];
648 	KASSERT(size > 0);
649 
650 	if (kiic_write(sc->sc_i2c, DEQaddr, reg, data, size))
651 		return (-1);
652 
653 	return (0);
654 }
655 
656 #define DEQ_WRITE(sc, reg, addr) \
657 	if (tas3004_write(sc, reg, addr)) goto err
658 
659 int
660 tas3004_init(struct snapper_softc *sc)
661 {
662 	deq_reset(sc);
663 
664 	DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
665 	DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
666 	DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
667 	DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
668 	DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
669 	DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
670 	DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
671 	DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
672 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
673 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
674 	DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
675 	DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
676 	DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
677 	DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
678 	DEQ_WRITE(sc, DEQ_RB6, tas3004_initdata.RB6);
679 	DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
680 	DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
681 	DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
682 	DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
683 	DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
684 	DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
685 	DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
686 	DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
687 	DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
688 	DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
689 	DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
690 	DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
691 	DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
692 
693 	return (0);
694 err:
695 	printf("%s: tas3004_init failed\n", sc->sc_dev.dv_xname);
696 	return (-1);
697 }
698 
699 void
700 snapper_init(struct snapper_softc *sc)
701 {
702 
703 	/* "sample-rates" (44100, 48000) */
704 	i2s_set_rate(sc, 44100);
705 
706 #if 1
707 	/* Enable I2C interrupts. */
708 #define IER 4
709 #define I2C_INT_DATA 0x01
710 #define I2C_INT_ADDR 0x02
711 #define I2C_INT_STOP 0x04
712 	kiic_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
713 #endif
714 
715 	if (tas3004_init(sc))
716 		return;
717 
718 	snapper_set_volume(sc, 190, 190);
719 	snapper_set_treble(sc, 128); /* 0 dB */
720 	snapper_set_bass(sc, 128); /* 0 dB */
721 
722 	/* Mic in, reflects tas3004_initdata.ACR */
723 	sc->sc_record_source = 1 << 1;
724 	snapper_set_input(sc, sc->sc_record_source);
725 }
726