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