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