1 /* $OpenBSD: daca.c,v 1.15 2022/10/26 20:19:07 kn Exp $ */
2
3 /*-
4 * Copyright (c) 2002,2003 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Datasheet is available from
31 * http://www.indata.si/grega/pdfs/dac3550a.pdf
32 */
33
34 #include <sys/param.h>
35 #include <sys/audioio.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38
39 #include <dev/audio_if.h>
40 #include <dev/ofw/openfirm.h>
41 #include <macppc/dev/dbdma.h>
42
43 #include <machine/autoconf.h>
44
45 #include <macppc/dev/i2svar.h>
46
47 #ifdef DACA_DEBUG
48 # define DPRINTF printf
49 #else
50 # define DPRINTF while (0) printf
51 #endif
52
53 /* XXX */
54 #define daca_softc i2s_softc
55
56 /* XXX */
57 int kiic_write(struct device *, int, int, const void *, int);
58 int kiic_writereg(struct device *, int, u_int);
59
60 int daca_match(struct device *, void *, void *);
61 void daca_attach(struct device *, struct device *, void *);
62 void daca_defer(struct device *);
63 void daca_init(struct daca_softc *);
64 void daca_set_volume(struct daca_softc *, int, int);
65
66 const struct cfattach daca_ca = {
67 sizeof(struct daca_softc), daca_match, daca_attach
68 };
69
70 struct cfdriver daca_cd = {
71 NULL, "daca", DV_DULL
72 };
73
74 const struct audio_hw_if daca_hw_if = {
75 .open = i2s_open,
76 .close = i2s_close,
77 .set_params = i2s_set_params,
78 .round_blocksize = i2s_round_blocksize,
79 .halt_output = i2s_halt_output,
80 .halt_input = i2s_halt_input,
81 .set_port = i2s_set_port,
82 .get_port = i2s_get_port,
83 .query_devinfo = i2s_query_devinfo,
84 .allocm = i2s_allocm,
85 .round_buffersize = i2s_round_buffersize,
86 .trigger_output = i2s_trigger_output,
87 .trigger_input = i2s_trigger_input,
88 };
89
90 /* DAC3550A registers */
91 #define DEQ_SR 0x01 /* Sample rate control (8) */
92 #define DEQ_AVOL 0x02 /* Analog volume (16) */
93 #define DEQ_GCFG 0x03 /* Global configuration (8) */
94
95 int
daca_match(struct device * parent,void * match,void * aux)96 daca_match(struct device *parent, void *match, void *aux)
97 {
98 struct confargs *ca = aux;
99 int soundbus, soundchip;
100 char compat[32];
101
102 if (strcmp(ca->ca_name, "i2s") != 0)
103 return (0);
104
105 if ((soundbus = OF_child(ca->ca_node)) == 0 ||
106 (soundchip = OF_child(soundbus)) == 0)
107 return (0);
108
109 bzero(compat, sizeof compat);
110 OF_getprop(soundchip, "compatible", compat, sizeof compat);
111
112 if (strcmp(compat, "daca") != 0)
113 return (0);
114
115 return (1);
116 }
117
118 #define DEQaddr 0x9a
119
120 void
daca_attach(struct device * parent,struct device * self,void * aux)121 daca_attach(struct device *parent,struct device *self, void *aux)
122 {
123 struct daca_softc *sc = (struct daca_softc *)self;
124
125 sc->sc_setvolume = daca_set_volume;
126
127 i2s_attach(parent, sc, aux);
128 config_defer(self, daca_defer);
129 }
130
131 void
daca_defer(struct device * dev)132 daca_defer(struct device *dev)
133 {
134 struct daca_softc *sc = (struct daca_softc *)dev;
135 struct device *dv;
136
137 TAILQ_FOREACH(dv, &alldevs, dv_list)
138 if (strcmp(dv->dv_cfdata->cf_driver->cd_name, "kiic") == 0 &&
139 strcmp(dv->dv_parent->dv_cfdata->cf_driver->cd_name, "macobio") == 0)
140 sc->sc_i2c = dv;
141 if (sc->sc_i2c == NULL) {
142 printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
143 return;
144 }
145
146 /* XXX If i2c has failed to attach, what should we do? */
147
148 audio_attach_mi(&daca_hw_if, sc, NULL, &sc->sc_dev);
149
150 daca_init(sc);
151 }
152
153 void
daca_init(struct daca_softc * sc)154 daca_init(struct daca_softc *sc)
155 {
156 i2s_set_rate(sc, 44100);
157 kiic_writereg(sc->sc_i2c, 4, 0x01 | 0x02 | 0x04);
158 }
159
160 void
daca_set_volume(struct daca_softc * sc,int left,int right)161 daca_set_volume(struct daca_softc *sc, int left, int right)
162 {
163 u_int16_t data;
164
165 sc->sc_vol_l = left;
166 sc->sc_vol_r = right;
167
168 left >>= 2;
169 right >>= 2;
170 data = left << 8 | right;
171 kiic_write(sc->sc_i2c, DEQaddr, DEQ_AVOL, &data, 2);
172 }
173