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