snapper.c revision 1.8 1 /* $NetBSD: snapper.c,v 1.8 2005/12/24 22:45:35 perry Exp $ */
2 /* Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp */
3
4 /*-
5 * Copyright (c) 2002 Tsubai Masanari. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Datasheet is available from
32 * http://www.ti.com/sc/docs/products/analog/tas3004.html
33 */
34
35 #include <sys/param.h>
36 #include <sys/audioio.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39
40 #include <dev/auconv.h>
41 #include <dev/audio_if.h>
42 #include <dev/mulaw.h>
43 #include <dev/ofw/openfirm.h>
44 #include <macppc/dev/dbdma.h>
45
46 #include <uvm/uvm_extern.h>
47 #include <dev/i2c/i2cvar.h>
48
49 #include <machine/autoconf.h>
50 #include <machine/pio.h>
51
52 #include <macppc/dev/deqvar.h>
53
54 #ifdef SNAPPER_DEBUG
55 # define DPRINTF printf
56 #else
57 # define DPRINTF while (0) printf
58 #endif
59
60 struct snapper_softc {
61 struct device sc_dev;
62 int sc_flags;
63 int sc_node;
64
65 void (*sc_ointr)(void *); /* dma completion intr handler */
66 void *sc_oarg; /* arg for sc_ointr() */
67 int sc_opages; /* # of output pages */
68
69 void (*sc_iintr)(void *); /* dma completion intr handler */
70 void *sc_iarg; /* arg for sc_iintr() */
71
72 u_int sc_record_source; /* recording source mask */
73 u_int sc_output_mask; /* output source mask */
74
75 u_char *sc_reg;
76 i2c_addr_t sc_deqaddr;
77 i2c_tag_t sc_i2c;
78
79 u_int sc_vol_l;
80 u_int sc_vol_r;
81 u_int sc_treble;
82 u_int sc_bass;
83 u_int mixer[6]; /* s1_l, s2_l, an_l, s1_r, s2_r, an_r */
84
85 dbdma_regmap_t *sc_odma;
86 dbdma_regmap_t *sc_idma;
87 unsigned char dbdma_cmdspace[sizeof(struct dbdma_command) * 40 + 15];
88 struct dbdma_command *sc_odmacmd;
89 struct dbdma_command *sc_idmacmd;
90 };
91
92 int snapper_match(struct device *, struct cfdata *, void *);
93 void snapper_attach(struct device *, struct device *, void *);
94 void snapper_defer(struct device *);
95 int snapper_intr(void *);
96 void snapper_close(void *);
97 int snapper_query_encoding(void *, struct audio_encoding *);
98 int snapper_set_params(void *, int, int, audio_params_t *,
99 audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
100 int snapper_round_blocksize(void *, int, int, const audio_params_t *);
101 int snapper_halt_output(void *);
102 int snapper_halt_input(void *);
103 int snapper_getdev(void *, struct audio_device *);
104 int snapper_set_port(void *, mixer_ctrl_t *);
105 int snapper_get_port(void *, mixer_ctrl_t *);
106 int snapper_query_devinfo(void *, mixer_devinfo_t *);
107 size_t snapper_round_buffersize(void *, int, size_t);
108 paddr_t snapper_mappage(void *, void *, off_t, int);
109 int snapper_get_props(void *);
110 int snapper_trigger_output(void *, void *, void *, int, void (*)(void *),
111 void *, const audio_params_t *);
112 int snapper_trigger_input(void *, void *, void *, int, void (*)(void *),
113 void *, const audio_params_t *);
114 void snapper_set_volume(struct snapper_softc *, int, int);
115 int snapper_set_rate(struct snapper_softc *, u_int);
116 void snapper_set_treble(struct snapper_softc *, int);
117 void snapper_set_bass(struct snapper_softc *, int);
118 void snapper_write_mixers(struct snapper_softc *);
119
120 int tas3004_write(struct snapper_softc *, u_int, const void *);
121 static int gpio_read(char *);
122 static void gpio_write(char *, int);
123 void snapper_mute_speaker(struct snapper_softc *, int);
124 void snapper_mute_headphone(struct snapper_softc *, int);
125 int snapper_cint(void *);
126 int tas3004_init(struct snapper_softc *);
127 void snapper_init(struct snapper_softc *, int);
128
129 struct cfattach snapper_ca = {
130 "snapper", {}, sizeof(struct snapper_softc),
131 snapper_match, snapper_attach
132 };
133
134 const struct audio_hw_if snapper_hw_if = {
135 NULL, /* open */
136 snapper_close,
137 NULL,
138 snapper_query_encoding,
139 snapper_set_params,
140 snapper_round_blocksize,
141 NULL,
142 NULL,
143 NULL,
144 NULL,
145 NULL,
146 snapper_halt_output,
147 snapper_halt_input,
148 NULL,
149 snapper_getdev,
150 NULL,
151 snapper_set_port,
152 snapper_get_port,
153 snapper_query_devinfo,
154 NULL,
155 NULL,
156 snapper_round_buffersize,
157 snapper_mappage,
158 snapper_get_props,
159 snapper_trigger_output,
160 snapper_trigger_input,
161 NULL
162 };
163
164 struct audio_device snapper_device = {
165 "SNAPPER",
166 "",
167 "snapper"
168 };
169
170 const uint8_t snapper_basstab[] = {
171 0x96, /* -18dB */
172 0x94, /* -17dB */
173 0x92, /* -16dB */
174 0x90, /* -15dB */
175 0x8e, /* -14dB */
176 0x8c, /* -13dB */
177 0x8a, /* -12dB */
178 0x88, /* -11dB */
179 0x86, /* -10dB */
180 0x84, /* -9dB */
181 0x82, /* -8dB */
182 0x80, /* -7dB */
183 0x7e, /* -6dB */
184 0x7c, /* -5dB */
185 0x7a, /* -4dB */
186 0x78, /* -3dB */
187 0x76, /* -2dB */
188 0x74, /* -1dB */
189 0x72, /* 0dB */
190 0x6f, /* 1dB */
191 0x6d, /* 2dB */
192 0x6a, /* 3dB */
193 0x67, /* 4dB */
194 0x65, /* 5dB */
195 0x62, /* 6dB */
196 0x5f, /* 7dB */
197 0x5b, /* 8dB */
198 0x55, /* 9dB */
199 0x4f, /* 10dB */
200 0x49, /* 11dB */
201 0x43, /* 12dB */
202 0x3b, /* 13dB */
203 0x33, /* 14dB */
204 0x29, /* 15dB */
205 0x1e, /* 16dB */
206 0x11, /* 17dB */
207 0x01, /* 18dB */
208 };
209
210 #define SNAPPER_NFORMATS 1
211 static const struct audio_format snapper_formats[SNAPPER_NFORMATS] = {
212 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
213 2, AUFMT_STEREO, 3, {8000, 44100, 48000}},
214 };
215
216 static u_char *amp_mute;
217 static u_char *headphone_mute;
218 static u_char *audio_hw_reset;
219 static u_char *headphone_detect;
220 static int headphone_detect_active;
221
222
223 /* I2S registers */
224 #define I2S_INT 0x00
225 #define I2S_FORMAT 0x10
226 #define I2S_FRAMECOUNT 0x40
227 #define I2S_FRAMEMATCH 0x50
228 #define I2S_WORDSIZE 0x60
229
230 /* TAS3004 registers */
231 #define DEQ_MCR1 0x01 /* Main control register 1 (1byte) */
232 #define DEQ_DRC 0x02 /* Dynamic range compression (6bytes?) */
233 #define DEQ_VOLUME 0x04 /* Volume (6bytes) */
234 #define DEQ_TREBLE 0x05 /* Treble control (1byte) */
235 #define DEQ_BASS 0x06 /* Bass control (1byte) */
236 #define DEQ_MIXER_L 0x07 /* Mixer left gain (9bytes) */
237 #define DEQ_MIXER_R 0x08 /* Mixer right gain (9bytes) */
238 #define DEQ_LB0 0x0a /* Left biquad 0 (15bytes) */
239 #define DEQ_LB1 0x0b /* Left biquad 1 (15bytes) */
240 #define DEQ_LB2 0x0c /* Left biquad 2 (15bytes) */
241 #define DEQ_LB3 0x0d /* Left biquad 3 (15bytes) */
242 #define DEQ_LB4 0x0e /* Left biquad 4 (15bytes) */
243 #define DEQ_LB5 0x0f /* Left biquad 5 (15bytes) */
244 #define DEQ_LB6 0x10 /* Left biquad 6 (15bytes) */
245 #define DEQ_RB0 0x13 /* Right biquad 0 (15bytes) */
246 #define DEQ_RB1 0x14 /* Right biquad 1 (15bytes) */
247 #define DEQ_RB2 0x15 /* Right biquad 2 (15bytes) */
248 #define DEQ_RB3 0x16 /* Right biquad 3 (15bytes) */
249 #define DEQ_RB4 0x17 /* Right biquad 4 (15bytes) */
250 #define DEQ_RB5 0x18 /* Right biquad 5 (15bytes) */
251 #define DEQ_RB6 0x19 /* Right biquad 6 (15bytes) */
252 #define DEQ_LLB 0x21 /* Left loudness biquad (15bytes) */
253 #define DEQ_RLB 0x22 /* Right loudness biquad (15bytes) */
254 #define DEQ_LLB_GAIN 0x23 /* Left loudness biquad gain (3bytes) */
255 #define DEQ_RLB_GAIN 0x24 /* Right loudness biquad gain (3bytes) */
256 #define DEQ_ACR 0x40 /* Analog control register (1byte) */
257 #define DEQ_MCR2 0x43 /* Main control register 2 (1byte) */
258
259 #define DEQ_MCR1_FL 0x80 /* Fast load */
260 #define DEQ_MCR1_SC 0x40 /* SCLK frequency */
261 #define DEQ_MCR1_SC_32 0x00 /* 32fs */
262 #define DEQ_MCR1_SC_64 0x40 /* 64fs */
263 #define DEQ_MCR1_SM 0x30 /* Output serial port mode */
264 #define DEQ_MCR1_SM_L 0x00 /* Left justified */
265 #define DEQ_MCR1_SM_R 0x10 /* Right justified */
266 #define DEQ_MCR1_SM_I2S 0x20 /* I2S */
267 #define DEQ_MCR1_W 0x03 /* Serial port word length */
268 #define DEQ_MCR1_W_16 0x00 /* 16 bit */
269 #define DEQ_MCR1_W_18 0x01 /* 18 bit */
270 #define DEQ_MCR1_W_20 0x02 /* 20 bit */
271
272 #define DEQ_MCR2_DL 0x80 /* Download */
273 #define DEQ_MCR2_AP 0x02 /* All pass mode */
274
275 #define DEQ_ACR_ADM 0x80 /* ADC output mode */
276 #define DEQ_ACR_LRB 0x40 /* Select B input */
277 #define DEQ_ACR_DM 0x0c /* De-emphasis control */
278 #define DEQ_ACR_DM_OFF 0x00 /* off */
279 #define DEQ_ACR_DM_48 0x04 /* fs = 48kHz */
280 #define DEQ_ACR_DM_44 0x08 /* fs = 44.1kHz */
281 #define DEQ_ACR_INP 0x02 /* Analog input select */
282 #define DEQ_ACR_INP_A 0x00 /* A */
283 #define DEQ_ACR_INP_B 0x02 /* B */
284 #define DEQ_ACR_APD 0x01 /* Analog power down */
285
286 struct tas3004_reg {
287 u_char MCR1[1];
288 u_char DRC[6];
289 u_char VOLUME[6];
290 u_char TREBLE[1];
291 u_char BASS[1];
292 u_char MIXER_L[9];
293 u_char MIXER_R[9];
294 u_char LB0[15];
295 u_char LB1[15];
296 u_char LB2[15];
297 u_char LB3[15];
298 u_char LB4[15];
299 u_char LB5[15];
300 u_char LB6[15];
301 u_char RB0[15];
302 u_char RB1[15];
303 u_char RB2[15];
304 u_char RB3[15];
305 u_char RB4[15];
306 u_char RB5[15];
307 u_char RB6[15];
308 u_char LLB[15];
309 u_char RLB[15];
310 u_char LLB_GAIN[3];
311 u_char RLB_GAIN[3];
312 u_char ACR[1];
313 u_char MCR2[1];
314 };
315
316 #define GPIO_OUTSEL 0xf0 /* Output select */
317 /* 0x00 GPIO bit0 is output
318 0x10 media-bay power
319 0x20 reserved
320 0x30 MPIC */
321
322 #define GPIO_ALTOE 0x08 /* Alternate output enable */
323 /* 0x00 Use DDR
324 0x08 Use output select */
325
326 #define GPIO_DDR 0x04 /* Data direction */
327 #define GPIO_DDR_OUTPUT 0x04 /* Output */
328 #define GPIO_DDR_INPUT 0x00 /* Input */
329
330 #define GPIO_LEVEL 0x02 /* Pin level (RO) */
331
332 #define GPIO_DATA 0x01 /* Data */
333
334 int
335 snapper_match(struct device *parent, struct cfdata *match, void *aux)
336 {
337 struct confargs *ca;
338 int soundbus, soundchip;
339 char compat[32];
340
341 ca = aux;
342 if (strcmp(ca->ca_name, "i2s") != 0)
343 return 0;
344
345 if ((soundbus = OF_child(ca->ca_node)) == 0 ||
346 (soundchip = OF_child(soundbus)) == 0)
347 return 0;
348
349 bzero(compat, sizeof compat);
350 OF_getprop(soundchip, "compatible", compat, sizeof compat);
351
352 if (strcmp(compat, "snapper") != 0)
353 return 0;
354
355 return 1;
356 }
357
358 void
359 snapper_attach(struct device *parent, struct device *self, void *aux)
360 {
361 struct snapper_softc *sc;
362 struct confargs *ca;
363 unsigned long v;
364 int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
365 int soundbus, intr[6];
366
367 sc = (struct snapper_softc *)self;
368 ca = aux;
369
370 v = (((unsigned long) &sc->dbdma_cmdspace[0]) + 0xf) & ~0xf;
371 sc->sc_odmacmd = (struct dbdma_command *) v;
372 sc->sc_idmacmd = sc->sc_odmacmd + 20;
373
374 #ifdef DIAGNOSTIC
375 if ((vaddr_t)sc->sc_odmacmd & 0x0f) {
376 printf(": bad dbdma alignment\n");
377 return;
378 }
379 #endif
380
381 ca->ca_reg[0] += ca->ca_baseaddr;
382 ca->ca_reg[2] += ca->ca_baseaddr;
383 ca->ca_reg[4] += ca->ca_baseaddr;
384
385 sc->sc_node = ca->ca_node;
386 sc->sc_reg = (void *)ca->ca_reg[0];
387 sc->sc_odma = (void *)ca->ca_reg[2];
388 sc->sc_idma = (void *)ca->ca_reg[4];
389
390 soundbus = OF_child(ca->ca_node);
391 OF_getprop(soundbus, "interrupts", intr, sizeof intr);
392 cirq = intr[0];
393 oirq = intr[2];
394 iirq = intr[4];
395 cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
396 oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
397 iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
398
399 /* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
400 intr_establish(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc);
401 /* intr_establish(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc); */
402
403 printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
404
405 config_interrupts(self, snapper_defer);
406 }
407
408 void
409 snapper_defer(struct device *dev)
410 {
411 struct snapper_softc *sc;
412 struct device *dv;
413 struct deq_softc *deq;
414
415 sc = (struct snapper_softc *)dev;
416 /*
417 for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
418 if (strncmp(dv->dv_xname, "ki2c", 4) == 0 &&
419 strncmp(dv->dv_parent->dv_xname, "obio", 4) == 0)
420 sc->sc_i2c = dv;
421 */
422 for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
423 if (strncmp(dv->dv_xname, "deq", 3) == 0 &&
424 strncmp(dv->dv_parent->dv_xname, "ki2c", 4) == 0) {
425 deq=(struct deq_softc *)dv;
426 sc->sc_i2c = deq->sc_i2c;
427 sc->sc_deqaddr=deq->sc_address;
428 }
429
430 if (sc->sc_i2c == NULL) {
431 printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
432 return;
433 }
434
435 /* XXX If i2c was failed to attach, what should we do? */
436
437 audio_attach_mi(&snapper_hw_if, sc, &sc->sc_dev);
438
439 /* ki2c_setmode(sc->sc_i2c, I2C_STDSUBMODE); */
440 snapper_init(sc, sc->sc_node);
441 }
442
443 int
444 snapper_intr(void *v)
445 {
446 struct snapper_softc *sc;
447 struct dbdma_command *cmd;
448 int count;
449 int status;
450
451 sc = v;
452 cmd = sc->sc_odmacmd;
453 count = sc->sc_opages;
454 /* Fill used buffer(s). */
455 while (count-- > 0) {
456 if ((dbdma_ld16(&cmd->d_command) & 0x30) == 0x30) {
457 status = dbdma_ld16(&cmd->d_status);
458 cmd->d_status = 0;
459 if (status) /* status == 0x8400 */
460 if (sc->sc_ointr)
461 (*sc->sc_ointr)(sc->sc_oarg);
462 }
463 cmd++;
464 }
465
466 return 1;
467 }
468
469 /*
470 * Close function is called at splaudio().
471 */
472 void
473 snapper_close(void *h)
474 {
475 struct snapper_softc *sc;
476
477 sc = h;
478 snapper_halt_output(sc);
479 snapper_halt_input(sc);
480
481 sc->sc_ointr = 0;
482 sc->sc_iintr = 0;
483 }
484
485 int
486 snapper_query_encoding(void *h, struct audio_encoding *ae)
487 {
488
489 ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
490 switch (ae->index) {
491 case 0:
492 strcpy(ae->name, AudioEslinear);
493 ae->encoding = AUDIO_ENCODING_SLINEAR;
494 ae->precision = 16;
495 ae->flags = 0;
496 return 0;
497 case 1:
498 strcpy(ae->name, AudioEslinear_be);
499 ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
500 ae->precision = 16;
501 ae->flags = 0;
502 return 0;
503 case 2:
504 strcpy(ae->name, AudioEslinear_le);
505 ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
506 ae->precision = 16;
507 return 0;
508 case 3:
509 strcpy(ae->name, AudioEulinear_be);
510 ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
511 ae->precision = 16;
512 return 0;
513 case 4:
514 strcpy(ae->name, AudioEulinear_le);
515 ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
516 ae->precision = 16;
517 return 0;
518 case 5:
519 strcpy(ae->name, AudioEmulaw);
520 ae->encoding = AUDIO_ENCODING_ULAW;
521 ae->precision = 8;
522 return 0;
523 case 6:
524 strcpy(ae->name, AudioEalaw);
525 ae->encoding = AUDIO_ENCODING_ALAW;
526 ae->precision = 8;
527 return 0;
528 default:
529 return EINVAL;
530 }
531 }
532
533 int
534 snapper_set_params(void *h, int setmode, int usemode,
535 audio_params_t *play, audio_params_t *rec,
536 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
537 {
538 struct snapper_softc *sc;
539 audio_params_t *p;
540 stream_filter_list_t *fil;
541 int mode;
542
543 sc = h;
544 p = NULL;
545
546 /*
547 * This device only has one clock, so make the sample rates match.
548 */
549 if (play->sample_rate != rec->sample_rate &&
550 usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
551 if (setmode == AUMODE_PLAY) {
552 rec->sample_rate = play->sample_rate;
553 setmode |= AUMODE_RECORD;
554 } else if (setmode == AUMODE_RECORD) {
555 play->sample_rate = rec->sample_rate;
556 setmode |= AUMODE_PLAY;
557 } else
558 return EINVAL;
559 }
560
561 for (mode = AUMODE_RECORD; mode != -1;
562 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
563 if ((setmode & mode) == 0)
564 continue;
565
566 p = mode == AUMODE_PLAY ? play : rec;
567 if (p->sample_rate < 4000 || p->sample_rate > 50000)
568 return EINVAL;
569
570 fil = mode == AUMODE_PLAY ? pfil : rfil;
571 if (auconv_set_converter(snapper_formats, SNAPPER_NFORMATS,
572 mode, p, TRUE, fil) < 0)
573 return EINVAL;
574 if (fil->req_size > 0)
575 p = &fil->filters[0].param;
576 }
577
578 /* Set the speed. p points HW encoding. */
579 if (snapper_set_rate(sc, p->sample_rate))
580 return EINVAL;
581
582 return 0;
583 }
584
585 int
586 snapper_round_blocksize(void *h, int size, int mode,
587 const audio_params_t *param)
588 {
589
590 if (size < NBPG)
591 size = NBPG;
592 return size & ~PGOFSET;
593 }
594
595 int
596 snapper_halt_output(void *h)
597 {
598 struct snapper_softc *sc;
599
600 sc = h;
601 dbdma_stop(sc->sc_odma);
602 dbdma_reset(sc->sc_odma);
603 return 0;
604 }
605
606 int
607 snapper_halt_input(void *h)
608 {
609 struct snapper_softc *sc;
610
611 sc = h;
612 dbdma_stop(sc->sc_idma);
613 dbdma_reset(sc->sc_idma);
614 return 0;
615 }
616
617 int
618 snapper_getdev(void *h, struct audio_device *retp)
619 {
620
621 *retp = snapper_device;
622 return 0;
623 }
624
625 enum {
626 SNAPPER_MONITOR_CLASS,
627 SNAPPER_OUTPUT_CLASS,
628 SNAPPER_RECORD_CLASS,
629 SNAPPER_OUTPUT_SELECT,
630 SNAPPER_VOL_OUTPUT,
631 SNAPPER_DIGI1,
632 SNAPPER_DIGI2,
633 SNAPPER_ANALOG,
634 SNAPPER_INPUT_SELECT,
635 SNAPPER_VOL_INPUT,
636 SNAPPER_TREBLE,
637 SNAPPER_BASS,
638 SNAPPER_ENUM_LAST
639 };
640
641 int
642 snapper_set_port(void *h, mixer_ctrl_t *mc)
643 {
644 struct snapper_softc *sc;
645 int l, r;
646
647 DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
648 sc = h;
649 l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
650 r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
651
652 switch (mc->dev) {
653 case SNAPPER_OUTPUT_SELECT:
654 /* No change necessary? */
655 if (mc->un.mask == sc->sc_output_mask)
656 return 0;
657
658 snapper_mute_speaker(sc, 1);
659 snapper_mute_headphone(sc, 1);
660 if (mc->un.mask & 1 << 0)
661 snapper_mute_speaker(sc, 0);
662 if (mc->un.mask & 1 << 1)
663 snapper_mute_headphone(sc, 0);
664
665 sc->sc_output_mask = mc->un.mask;
666 return 0;
667
668 case SNAPPER_VOL_OUTPUT:
669 snapper_set_volume(sc, l, r);
670 return 0;
671
672 case SNAPPER_INPUT_SELECT:
673 /* no change necessary? */
674 if (mc->un.mask == sc->sc_record_source)
675 return 0;
676 switch (mc->un.mask) {
677 case 1 << 0: /* CD */
678 case 1 << 1: /* microphone */
679 case 1 << 2: /* line in */
680 /* XXX TO BE DONE */
681 break;
682 default: /* invalid argument */
683 return EINVAL;
684 }
685 sc->sc_record_source = mc->un.mask;
686 return 0;
687
688 case SNAPPER_VOL_INPUT:
689 /* XXX TO BE DONE */
690 return 0;
691
692 case SNAPPER_BASS:
693 snapper_set_bass(sc,l);
694 return 0;
695 case SNAPPER_TREBLE:
696 snapper_set_treble(sc,l);
697 return 0;
698 case SNAPPER_DIGI1:
699 sc->mixer[0]=l;
700 sc->mixer[3]=r;
701 snapper_write_mixers(sc);
702 return 0;
703 case SNAPPER_DIGI2:
704 sc->mixer[1]=l;
705 sc->mixer[4]=r;
706 snapper_write_mixers(sc);
707 return 0;
708 case SNAPPER_ANALOG:
709 sc->mixer[2]=l;
710 sc->mixer[5]=r;
711 snapper_write_mixers(sc);
712 return 0;
713 }
714 return ENXIO;
715 }
716
717 int
718 snapper_get_port(void *h, mixer_ctrl_t *mc)
719 {
720 struct snapper_softc *sc;
721
722 DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
723 sc = h;
724 switch (mc->dev) {
725 case SNAPPER_OUTPUT_SELECT:
726 mc->un.mask = sc->sc_output_mask;
727 return 0;
728
729 case SNAPPER_VOL_OUTPUT:
730 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
731 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
732 return 0;
733
734 case SNAPPER_INPUT_SELECT:
735 mc->un.mask = sc->sc_record_source;
736 return 0;
737
738 case SNAPPER_VOL_INPUT:
739 /* XXX TO BE DONE */
740 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
741 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
742 return 0;
743 case SNAPPER_TREBLE:
744 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]=sc->sc_treble;
745 return 0;
746 case SNAPPER_BASS:
747 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]=sc->sc_bass;
748 return 0;
749 case SNAPPER_DIGI1:
750 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0];
751 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3];
752 return 0;
753 case SNAPPER_DIGI2:
754 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1];
755 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4];
756 return 0;
757 case SNAPPER_ANALOG:
758 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2];
759 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5];
760 return 0;
761 default:
762 return ENXIO;
763 }
764
765 return 0;
766 }
767
768 int
769 snapper_query_devinfo(void *h, mixer_devinfo_t *dip)
770 {
771 switch (dip->index) {
772
773 case SNAPPER_OUTPUT_SELECT:
774 dip->mixer_class = SNAPPER_MONITOR_CLASS;
775 strcpy(dip->label.name, AudioNoutput);
776 dip->type = AUDIO_MIXER_SET;
777 dip->prev = dip->next = AUDIO_MIXER_LAST;
778 dip->un.s.num_mem = 2;
779 strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
780 dip->un.s.member[0].mask = 1 << 0;
781 strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
782 dip->un.s.member[1].mask = 1 << 1;
783 return 0;
784
785 case SNAPPER_VOL_OUTPUT:
786 dip->mixer_class = SNAPPER_MONITOR_CLASS;
787 strcpy(dip->label.name, AudioNmaster);
788 dip->type = AUDIO_MIXER_VALUE;
789 dip->prev = dip->next = AUDIO_MIXER_LAST;
790 dip->un.v.num_channels = 2;
791 strcpy(dip->un.v.units.name, AudioNvolume);
792 return 0;
793
794 case SNAPPER_INPUT_SELECT:
795 dip->mixer_class = SNAPPER_RECORD_CLASS;
796 strcpy(dip->label.name, AudioNsource);
797 dip->type = AUDIO_MIXER_SET;
798 dip->prev = dip->next = AUDIO_MIXER_LAST;
799 dip->un.s.num_mem = 3;
800 strcpy(dip->un.s.member[0].label.name, AudioNcd);
801 dip->un.s.member[0].mask = 1 << 0;
802 strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
803 dip->un.s.member[1].mask = 1 << 1;
804 strcpy(dip->un.s.member[2].label.name, AudioNline);
805 dip->un.s.member[2].mask = 1 << 2;
806 return 0;
807
808 case SNAPPER_VOL_INPUT:
809 dip->mixer_class = SNAPPER_RECORD_CLASS;
810 strcpy(dip->label.name, AudioNrecord);
811 dip->type = AUDIO_MIXER_VALUE;
812 dip->prev = dip->next = AUDIO_MIXER_LAST;
813 dip->un.v.num_channels = 2;
814 strcpy(dip->un.v.units.name, AudioNvolume);
815 return 0;
816
817 case SNAPPER_MONITOR_CLASS:
818 dip->mixer_class = SNAPPER_MONITOR_CLASS;
819 strcpy(dip->label.name, AudioCmonitor);
820 dip->type = AUDIO_MIXER_CLASS;
821 dip->next = dip->prev = AUDIO_MIXER_LAST;
822 return 0;
823
824 case SNAPPER_OUTPUT_CLASS:
825 dip->mixer_class = SNAPPER_OUTPUT_CLASS;
826 strcpy(dip->label.name, AudioCoutputs);
827 dip->type = AUDIO_MIXER_CLASS;
828 dip->next = dip->prev = AUDIO_MIXER_LAST;
829 return 0;
830
831 case SNAPPER_RECORD_CLASS:
832 dip->mixer_class = SNAPPER_RECORD_CLASS;
833 strcpy(dip->label.name, AudioCrecord);
834 dip->type = AUDIO_MIXER_CLASS;
835 dip->next = dip->prev = AUDIO_MIXER_LAST;
836 return 0;
837
838 case SNAPPER_TREBLE:
839 dip->mixer_class = SNAPPER_MONITOR_CLASS;
840 strcpy(dip->label.name, AudioNtreble);
841 dip->type = AUDIO_MIXER_VALUE;
842 dip->prev = dip->next = AUDIO_MIXER_LAST;
843 dip->un.v.num_channels = 1;
844 return 0;
845
846 case SNAPPER_BASS:
847 dip->mixer_class = SNAPPER_MONITOR_CLASS;
848 strcpy(dip->label.name, AudioNbass);
849 dip->type = AUDIO_MIXER_VALUE;
850 dip->prev = dip->next = AUDIO_MIXER_LAST;
851 dip->un.v.num_channels = 1;
852 return 0;
853
854 case SNAPPER_DIGI1:
855 dip->mixer_class = SNAPPER_MONITOR_CLASS;
856 strcpy(dip->label.name, AudioNdac);
857 dip->type = AUDIO_MIXER_VALUE;
858 dip->prev = dip->next = AUDIO_MIXER_LAST;
859 dip->un.v.num_channels = 2;
860 return 0;
861 case SNAPPER_DIGI2:
862 dip->mixer_class = SNAPPER_MONITOR_CLASS;
863 strcpy(dip->label.name, "Digi2");
864 dip->type = AUDIO_MIXER_VALUE;
865 dip->prev = dip->next = AUDIO_MIXER_LAST;
866 dip->un.v.num_channels = 2;
867 return 0;
868 case SNAPPER_ANALOG:
869 dip->mixer_class = SNAPPER_MONITOR_CLASS;
870 strcpy(dip->label.name, "Analog");
871 dip->type = AUDIO_MIXER_VALUE;
872 dip->prev = dip->next = AUDIO_MIXER_LAST;
873 dip->un.v.num_channels = 2;
874 return 0;
875 }
876
877 return ENXIO;
878 }
879
880 size_t
881 snapper_round_buffersize(void *h, int dir, size_t size)
882 {
883
884 if (size > 65536)
885 size = 65536;
886 return size;
887 }
888
889 paddr_t
890 snapper_mappage(void *h, void *mem, off_t off, int prot)
891 {
892
893 if (off < 0)
894 return -1;
895 return -1; /* XXX */
896 }
897
898 int
899 snapper_get_props(void *h)
900 {
901 return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
902 }
903
904 int
905 snapper_trigger_output(void *h, void *start, void *end, int bsize,
906 void (*intr)(void *), void *arg,
907 const audio_params_t *param)
908 {
909 struct snapper_softc *sc;
910 struct dbdma_command *cmd;
911 vaddr_t va;
912 int i, len, intmode;
913
914 DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
915 sc = h;
916 cmd = sc->sc_odmacmd;
917 sc->sc_ointr = intr;
918 sc->sc_oarg = arg;
919 sc->sc_opages = ((char *)end - (char *)start) / NBPG;
920
921 #ifdef DIAGNOSTIC
922 if (sc->sc_opages > 16)
923 panic("snapper_trigger_output");
924 #endif
925
926 va = (vaddr_t)start;
927 len = 0;
928 for (i = sc->sc_opages; i > 0; i--) {
929 len += NBPG;
930 if (len < bsize)
931 intmode = 0;
932 else {
933 len = 0;
934 intmode = DBDMA_INT_ALWAYS;
935 }
936
937 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
938 intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
939 cmd++;
940 va += NBPG;
941 }
942
943 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
944 0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
945 DBDMA_BRANCH_ALWAYS);
946
947 dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
948
949 dbdma_start(sc->sc_odma, sc->sc_odmacmd);
950
951 return 0;
952 }
953
954 int
955 snapper_trigger_input(void *h, void *start, void *end, int bsize,
956 void (*intr)(void *), void *arg,
957 const audio_params_t *param)
958 {
959
960 printf("snapper_trigger_input called\n");
961 return 1;
962 }
963
964 void
965 snapper_set_volume(struct snapper_softc *sc, int left, int right)
966 {
967 u_char vol[6];
968
969 sc->sc_vol_l = left;
970 sc->sc_vol_r = right;
971
972 #if 0
973 left <<= 8; /* XXX for now */
974 right <<= 8;
975
976 vol[0] = left >> 16;
977 vol[1] = left >> 8;
978 vol[2] = left;
979 vol[3] = right >> 16;
980 vol[4] = right >> 8;
981 vol[5] = right;
982 #else
983 /* 0x07ffff is LOUD, 0x000000 is mute */
984 vol[0]=0/*(left>>5)&0xff*/; /* upper 3 bits */
985 vol[1]=(left/*<<3*/)&0xff; /* lower 5 bits */
986 vol[2]=0;
987 vol[3]=0/*(right>>5)&0xff*/; /* upper 3 bits */
988 vol[4]=(right/*<<3*/)&0xff; /* lower 5 bits */
989 vol[5]=0;
990 #endif
991 tas3004_write(sc, DEQ_VOLUME, vol);
992 }
993
994 void snapper_set_treble(struct snapper_softc *sc, int stuff)
995 {
996 uint8_t reg;
997 if((stuff>=0) && (stuff<=255) && (sc->sc_treble!=stuff)) {
998 reg=snapper_basstab[(stuff>>3)+2];
999 sc->sc_treble=stuff;
1000 tas3004_write(sc, DEQ_TREBLE,®);
1001 }
1002 }
1003
1004 void snapper_set_bass(struct snapper_softc *sc, int stuff)
1005 {
1006 uint8_t reg;
1007 if((stuff>=0) && (stuff<=255) && (stuff!=sc->sc_bass)) {
1008 reg=snapper_basstab[(stuff>>3)+2];
1009 sc->sc_bass=stuff;
1010 tas3004_write(sc, DEQ_BASS,®);
1011 }
1012 }
1013
1014 void snapper_write_mixers(struct snapper_softc *sc)
1015 {
1016 uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
1017 regs[0] = (sc->mixer[0] >> 4) & 0xff;
1018 regs[1] = (sc->mixer[0] << 4) & 0xff;
1019 regs[3] = (sc->mixer[1] >> 4) & 0xff;
1020 regs[4] = (sc->mixer[1] << 4) & 0xff;
1021 regs[5] = (sc->mixer[2] >> 4) & 0xff;
1022 regs[7] = (sc->mixer[2] << 4) & 0xff;
1023 tas3004_write(sc, DEQ_MIXER_L, regs);
1024 regs[0] = (sc->mixer[3] >> 4) & 0xff;
1025 regs[1] = (sc->mixer[3] << 4) & 0xff;
1026 regs[3] = (sc->mixer[4] >> 4) & 0xff;
1027 regs[4] = (sc->mixer[4] << 4) & 0xff;
1028 regs[5] = (sc->mixer[5] >> 4) & 0xff;
1029 regs[7] = (sc->mixer[5] << 4) & 0xff;
1030 tas3004_write(sc, DEQ_MIXER_R, regs);
1031 }
1032
1033 #define CLKSRC_49MHz 0x80000000 /* Use 49152000Hz Osc. */
1034 #define CLKSRC_45MHz 0x40000000 /* Use 45158400Hz Osc. */
1035 #define CLKSRC_18MHz 0x00000000 /* Use 18432000Hz Osc. */
1036 #define MCLK_DIV 0x1f000000 /* MCLK = SRC / DIV */
1037 #define MCLK_DIV1 0x14000000 /* MCLK = SRC */
1038 #define MCLK_DIV3 0x13000000 /* MCLK = SRC / 3 */
1039 #define MCLK_DIV5 0x12000000 /* MCLK = SRC / 5 */
1040 #define SCLK_DIV 0x00f00000 /* SCLK = MCLK / DIV */
1041 #define SCLK_DIV1 0x00800000
1042 #define SCLK_DIV3 0x00900000
1043 #define SCLK_MASTER 0x00080000 /* Master mode */
1044 #define SCLK_SLAVE 0x00000000 /* Slave mode */
1045 #define SERIAL_FORMAT 0x00070000
1046 #define SERIAL_SONY 0x00000000
1047 #define SERIAL_64x 0x00010000
1048 #define SERIAL_32x 0x00020000
1049 #define SERIAL_DAV 0x00040000
1050 #define SERIAL_SILICON 0x00050000
1051
1052 // rate = fs = LRCLK
1053 // SCLK = 64*LRCLK (I2S)
1054 // MCLK = 256fs (typ. -- changeable)
1055
1056 // MCLK = clksrc / mdiv
1057 // SCLK = MCLK / sdiv
1058 // rate = SCLK / 64 ( = LRCLK = fs)
1059
1060 int
1061 snapper_set_rate(struct snapper_softc *sc, u_int rate)
1062 {
1063 u_int reg;
1064 int MCLK;
1065 int clksrc, mdiv, sdiv;
1066 int mclk_fs;
1067
1068 reg = 0;
1069 switch (rate) {
1070 case 8000:
1071 clksrc = 18432000; /* 18MHz */
1072 reg = CLKSRC_18MHz;
1073 mclk_fs = 256;
1074 break;
1075
1076 case 44100:
1077 clksrc = 45158400; /* 45MHz */
1078 reg = CLKSRC_45MHz;
1079 mclk_fs = 256;
1080 break;
1081
1082 case 48000:
1083 clksrc = 49152000; /* 49MHz */
1084 reg = CLKSRC_49MHz;
1085 mclk_fs = 256;
1086 break;
1087
1088 default:
1089 return EINVAL;
1090 }
1091
1092 MCLK = rate * mclk_fs;
1093 mdiv = clksrc / MCLK; // 4
1094 sdiv = mclk_fs / 64; // 4
1095
1096 switch (mdiv) {
1097 case 1:
1098 reg |= MCLK_DIV1;
1099 break;
1100 case 3:
1101 reg |= MCLK_DIV3;
1102 break;
1103 case 5:
1104 reg |= MCLK_DIV5;
1105 break;
1106 default:
1107 reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
1108 break;
1109 }
1110
1111 switch (sdiv) {
1112 case 1:
1113 reg |= SCLK_DIV1;
1114 break;
1115 case 3:
1116 reg |= SCLK_DIV3;
1117 break;
1118 default:
1119 reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
1120 break;
1121 }
1122
1123 reg |= SCLK_MASTER; /* XXX master mode */
1124
1125 reg |= SERIAL_64x;
1126
1127 /* stereo input and output */
1128 DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
1129 in32rb(sc->sc_reg + I2S_WORDSIZE), 0x02000200);
1130 out32rb(sc->sc_reg + I2S_WORDSIZE, 0x02000200);
1131
1132 DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
1133 in32rb(sc->sc_reg + I2S_FORMAT), reg);
1134 out32rb(sc->sc_reg + I2S_FORMAT, reg);
1135
1136 return 0;
1137 }
1138
1139 /*#define DEQaddr 0x6a*/
1140
1141 const struct tas3004_reg tas3004_initdata = {
1142 { DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_20 }, /* MCR1 */
1143 { 1, 0, 0, 0, 0, 0 }, /* DRC */
1144 { 0, 0, 0, 0, 0, 0 }, /* VOLUME */
1145 { 0x72 }, /* TREBLE */
1146 { 0x72 }, /* BASS */
1147 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_L */
1148 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_R */
1149 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1150 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1151 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1152 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1153 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1154 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1155 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1156 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1157 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1158 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1159 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1160 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1161 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1162 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1163 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1164 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */
1165 { 0, 0, 0 }, /* LLB_GAIN */
1166 { 0, 0, 0 }, /* RLB_GAIN */
1167 { 0xc0 }, /* ACR - right channel of input B is the microphone */
1168 { 2 } /* MCR2 - AllPass mode since we don't use the equalizer anyway */
1169 };
1170
1171 const char tas3004_regsize[] = {
1172 0, /* 0x00 */
1173 sizeof tas3004_initdata.MCR1, /* 0x01 */
1174 sizeof tas3004_initdata.DRC, /* 0x02 */
1175 0, /* 0x03 */
1176 sizeof tas3004_initdata.VOLUME, /* 0x04 */
1177 sizeof tas3004_initdata.TREBLE, /* 0x05 */
1178 sizeof tas3004_initdata.BASS, /* 0x06 */
1179 sizeof tas3004_initdata.MIXER_L, /* 0x07 */
1180 sizeof tas3004_initdata.MIXER_R, /* 0x08 */
1181 0, /* 0x09 */
1182 sizeof tas3004_initdata.LB0, /* 0x0a */
1183 sizeof tas3004_initdata.LB1, /* 0x0b */
1184 sizeof tas3004_initdata.LB2, /* 0x0c */
1185 sizeof tas3004_initdata.LB3, /* 0x0d */
1186 sizeof tas3004_initdata.LB4, /* 0x0e */
1187 sizeof tas3004_initdata.LB5, /* 0x0f */
1188 sizeof tas3004_initdata.LB6, /* 0x10 */
1189 0, /* 0x11 */
1190 0, /* 0x12 */
1191 sizeof tas3004_initdata.RB0, /* 0x13 */
1192 sizeof tas3004_initdata.RB1, /* 0x14 */
1193 sizeof tas3004_initdata.RB2, /* 0x15 */
1194 sizeof tas3004_initdata.RB3, /* 0x16 */
1195 sizeof tas3004_initdata.RB4, /* 0x17 */
1196 sizeof tas3004_initdata.RB5, /* 0x18 */
1197 sizeof tas3004_initdata.RB6, /* 0x19 */
1198 0,0,0,0, 0,0,
1199 0, /* 0x20 */
1200 sizeof tas3004_initdata.LLB, /* 0x21 */
1201 sizeof tas3004_initdata.RLB, /* 0x22 */
1202 sizeof tas3004_initdata.LLB_GAIN, /* 0x23 */
1203 sizeof tas3004_initdata.RLB_GAIN, /* 0x24 */
1204 0,0,0,0, 0,0,0,0, 0,0,0,
1205 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
1206 sizeof tas3004_initdata.ACR, /* 0x40 */
1207 0, /* 0x41 */
1208 0, /* 0x42 */
1209 sizeof tas3004_initdata.MCR2 /* 0x43 */
1210 };
1211
1212 int
1213 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
1214 {
1215 int size;
1216 static char regblock[sizeof(struct tas3004_reg)+1];
1217
1218 KASSERT(reg < sizeof tas3004_regsize);
1219 size = tas3004_regsize[reg];
1220 KASSERT(size > 0);
1221
1222 #ifdef DEBUG_SNAPPER
1223 printf("reg: %x, %d %d\n",reg,size,((char*)data)[0]);
1224 #endif
1225 #if 0
1226 ki2c_setmode(sc->sc_i2c, 8); /* std+sub mode */
1227
1228 if (ki2c_write(sc->sc_i2c, DEQaddr, reg, data, size))
1229 return -1;
1230 #endif
1231 /* ugly, but for now... */
1232 regblock[0] = reg;
1233 memcpy(®block[1], data, size);
1234 iic_acquire_bus(sc->sc_i2c, 0);
1235 iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, ®block, size + 1,
1236 NULL, 0, 0);
1237 iic_release_bus(sc->sc_i2c, 0);
1238
1239 return 0;
1240 }
1241
1242 int
1243 gpio_read(char *addr)
1244 {
1245
1246 if (*addr & GPIO_DATA)
1247 return 1;
1248 return 0;
1249 }
1250
1251 void
1252 gpio_write(char *addr, int val)
1253 {
1254 u_int data;
1255
1256 data = GPIO_DDR_OUTPUT;
1257 if (val)
1258 data |= GPIO_DATA;
1259 *addr = data;
1260 __asm volatile ("eieio");
1261 }
1262
1263 #define headphone_active 0 /* XXX OF */
1264 #define amp_active 0 /* XXX OF */
1265
1266 void
1267 snapper_mute_speaker(struct snapper_softc *sc, int mute)
1268 {
1269 u_int x;
1270
1271 DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
1272
1273 if (mute)
1274 x = amp_active; /* mute */
1275 else
1276 x = !amp_active; /* unmute */
1277 if (x != gpio_read(amp_mute))
1278 gpio_write(amp_mute, x);
1279
1280 DPRINTF("%d\n", gpio_read(amp_mute));
1281 }
1282
1283 void
1284 snapper_mute_headphone(struct snapper_softc *sc, int mute)
1285 {
1286 u_int x;
1287
1288 DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
1289
1290 if (mute)
1291 x = headphone_active; /* mute */
1292 else
1293 x = !headphone_active; /* unmute */
1294 if (x != gpio_read(headphone_mute))
1295 gpio_write(headphone_mute, x);
1296
1297 DPRINTF("%d\n", gpio_read(headphone_mute));
1298 }
1299
1300 int
1301 snapper_cint(void *v)
1302 {
1303 struct snapper_softc *sc;
1304 u_int sense;
1305
1306 sc = v;
1307 sense = *headphone_detect;
1308 DPRINTF("headphone detect = 0x%x\n", sense);
1309
1310 if (((sense & 0x02) >> 1) == headphone_detect_active) {
1311 DPRINTF("headphone is inserted\n");
1312 snapper_mute_speaker(sc, 1);
1313 snapper_mute_headphone(sc, 0);
1314 sc->sc_output_mask = 1 << 1;
1315 } else {
1316 DPRINTF("headphone is NOT inserted\n");
1317 snapper_mute_speaker(sc, 0);
1318 snapper_mute_headphone(sc, 1);
1319 sc->sc_output_mask = 1 << 0;
1320 }
1321
1322 return 1;
1323 }
1324
1325 #define reset_active 0 /* XXX OF */
1326
1327 #define DEQ_WRITE(sc, reg, addr) \
1328 if (tas3004_write(sc, reg, addr)) goto err
1329
1330 int
1331 tas3004_init(struct snapper_softc *sc)
1332 {
1333
1334 /* No reset port. Nothing to do. */
1335 if (audio_hw_reset == NULL)
1336 goto noreset;
1337
1338 /* Reset TAS3004. */
1339 gpio_write(audio_hw_reset, !reset_active); /* Negate RESET */
1340 delay(100000); /* XXX Really needed? */
1341
1342 gpio_write(audio_hw_reset, reset_active); /* Assert RESET */
1343 delay(1);
1344
1345 gpio_write(audio_hw_reset, !reset_active); /* Negate RESET */
1346 delay(10000);
1347
1348 noreset:
1349 DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
1350 DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
1351 DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
1352 DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
1353 DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
1354 DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
1355 DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
1356 DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
1357 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
1358 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
1359 DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
1360 DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
1361 DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
1362 DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
1363 DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
1364 DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
1365 DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
1366 DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
1367 DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
1368 DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
1369 DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
1370 DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
1371 DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
1372 DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
1373 DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
1374 DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
1375 DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
1376
1377 return 0;
1378 err:
1379 printf("tas3004_init: error\n");
1380 return -1;
1381 }
1382
1383 /* FCR(0x3c) bits */
1384 #define I2S0CLKEN 0x1000
1385 #define I2S0EN 0x2000
1386 #define I2S1CLKEN 0x080000
1387 #define I2S1EN 0x100000
1388
1389 #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN"
1390
1391 void
1392 snapper_init(struct snapper_softc *sc, int node)
1393 {
1394 int gpio;
1395 int headphone_detect_intr, headphone_detect_intrtype;
1396 #ifdef SNAPPER_DEBUG
1397 char fcr[32];
1398
1399 bitmask_snprintf(in32rb(0x8000003c), FCR3C_BITMASK, fcr, sizeof fcr);
1400 printf("FCR(0x3c) 0x%s\n", fcr);
1401 #endif
1402 headphone_detect_intr = -1;
1403
1404 gpio = getnodebyname(OF_parent(node), "gpio");
1405 DPRINTF(" /gpio 0x%x\n", gpio);
1406 gpio = OF_child(gpio);
1407 while (gpio) {
1408 char name[64], audio_gpio[64];
1409 int intr[2];
1410 char *addr;
1411
1412 bzero(name, sizeof name);
1413 bzero(audio_gpio, sizeof audio_gpio);
1414 addr = 0;
1415 OF_getprop(gpio, "name", name, sizeof name);
1416 OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
1417 OF_getprop(gpio, "AAPL,address", &addr, sizeof addr);
1418 /* printf("0x%x %s %s\n", gpio, name, audio_gpio); */
1419
1420 /* gpio5 */
1421 if (strcmp(audio_gpio, "headphone-mute") == 0)
1422 headphone_mute = addr;
1423 /* gpio6 */
1424 if (strcmp(audio_gpio, "amp-mute") == 0)
1425 amp_mute = addr;
1426 /* extint-gpio15 */
1427 if (strcmp(audio_gpio, "headphone-detect") == 0) {
1428 headphone_detect = addr;
1429 OF_getprop(gpio, "audio-gpio-active-state",
1430 &headphone_detect_active, 4);
1431 OF_getprop(gpio, "interrupts", intr, 8);
1432 headphone_detect_intr = intr[0];
1433 headphone_detect_intrtype = intr[1];
1434 }
1435 /* gpio11 (keywest-11) */
1436 if (strcmp(audio_gpio, "audio-hw-reset") == 0)
1437 audio_hw_reset = addr;
1438 gpio = OF_peer(gpio);
1439 }
1440 DPRINTF(" headphone-mute %p\n", headphone_mute);
1441 DPRINTF(" amp-mute %p\n", amp_mute);
1442 DPRINTF(" headphone-detect %p\n", headphone_detect);
1443 DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
1444 DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
1445 DPRINTF(" audio-hw-reset %p\n", audio_hw_reset);
1446
1447 if (headphone_detect_intr != -1)
1448 intr_establish(headphone_detect_intr, IST_EDGE, IPL_AUDIO,
1449 snapper_cint, sc);
1450
1451 /* "sample-rates" (44100, 48000) */
1452 snapper_set_rate(sc, 44100);
1453
1454 /* Enable headphone interrupt? */
1455 *headphone_detect |= 0x80;
1456 __asm volatile ("eieio");
1457
1458 /* i2c_set_port(port); */
1459
1460 #if 0
1461 /* Enable I2C interrupts. */
1462 #define IER 4
1463 #define I2C_INT_DATA 0x01
1464 #define I2C_INT_ADDR 0x02
1465 #define I2C_INT_STOP 0x04
1466 ki2c_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
1467 #endif
1468
1469 if (tas3004_init(sc))
1470 return;
1471
1472 /* Update headphone status. */
1473 snapper_cint(sc);
1474
1475 snapper_set_volume(sc, 80, 80);
1476
1477 sc->sc_bass = 128;
1478 sc->sc_treble = 128;
1479
1480 /* We mute the analog input for now */
1481 sc->mixer[0] = 80;
1482 sc->mixer[1] = 80;
1483 sc->mixer[2] = 0;
1484 sc->mixer[3] = 80;
1485 sc->mixer[4] = 80;
1486 sc->mixer[5] = 0;
1487 snapper_write_mixers(sc);
1488 }
1489