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