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