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