xref: /openbsd/sys/arch/macppc/dev/onyx.c (revision 404b540a)
1 /*	$OpenBSD: onyx.c,v 1.9 2008/10/29 00:04:14 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005 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  * http://focus.ti.com/docs/prod/folders/print/pcm3052.html
31  *
32  * Datasheet is available from
33  * http://focus.ti.com/docs/prod/folders/print/pcm3052a.html
34  */
35 
36 #include <sys/param.h>
37 #include <sys/audioio.h>
38 #include <sys/device.h>
39 #include <sys/systm.h>
40 
41 #include <dev/audio_if.h>
42 #include <dev/ofw/openfirm.h>
43 #include <macppc/dev/dbdma.h>
44 
45 #include <machine/autoconf.h>
46 
47 #include <macppc/dev/i2svar.h>
48 #include <macppc/dev/kiicvar.h>
49 
50 #ifdef ONYX_DEBUG
51 # define DPRINTF printf
52 #else
53 # define DPRINTF while (0) printf
54 #endif
55 
56 /* XXX */
57 #define PCM3052_I2C_ADDR	0x8c
58 
59 /* PCM3052 registers */
60 #define PCM3052_REG_LEFT_VOLUME		0x41
61 #define PCM3052_REG_RIGHT_VOLUME	0x42
62 
63 /* XXX */
64 #define onyx_softc i2s_softc
65 
66 /* XXX */
67 void kiic_setmode(struct kiic_softc *, u_int, u_int);
68 int kiic_write(struct device *, int, int, const void *, int);
69 
70 int onyx_getdev(void *, struct audio_device *);
71 int onyx_match(struct device *, void *, void *);
72 void onyx_attach(struct device *, struct device *, void *);
73 void onyx_defer(struct device *);
74 void onyx_set_volume(struct onyx_softc *, int, int);
75 void onyx_get_default_params(void *, int, struct audio_params *);
76 
77 struct cfattach onyx_ca = {
78 	sizeof(struct onyx_softc), onyx_match, onyx_attach
79 };
80 
81 struct cfdriver onyx_cd = {
82 	NULL, "onyx", DV_DULL
83 };
84 
85 struct audio_hw_if onyx_hw_if = {
86 	i2s_open,
87 	i2s_close,
88 	NULL,
89 	i2s_query_encoding,
90 	i2s_set_params,
91 	i2s_round_blocksize,
92 	NULL,
93 	NULL,
94 	NULL,
95 	NULL,
96 	NULL,
97 	i2s_halt_output,
98 	i2s_halt_input,
99 	NULL,
100 	onyx_getdev,
101 	NULL,
102 	i2s_set_port,
103 	i2s_get_port,
104 	i2s_query_devinfo,
105 	i2s_allocm,
106 	NULL,
107 	i2s_round_buffersize,
108 	i2s_mappage,
109 	i2s_get_props,
110 	i2s_trigger_output,
111 	i2s_trigger_input,
112 	onyx_get_default_params
113 };
114 
115 struct audio_device onyx_device = {
116 	"ONYX",
117 	"",
118 	"onyx"
119 };
120 
121 int
122 onyx_match(struct device *parent, void *match, void *aux)
123 {
124 	struct confargs *ca = aux;
125 	int soundbus, soundchip, soundcodec;
126 	int32_t layout = 0;
127 
128 	if (strcmp(ca->ca_name, "i2s") != 0)
129 		return (0);
130 
131 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
132 	    (soundchip = OF_child(soundbus)) == 0)
133 		return (0);
134 
135 	if (OF_getprop(soundchip, "platform-onyx-codec-ref",
136 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
137 		return (1);
138 
139 	/*
140 	 * Apple really messed up.  First and second generation iMac
141 	 * G5 (PowerMac8,1 and PowerMac8,2) have a "deq" i2c device
142 	 * listed in the OF device tree, which is a telltale sign of
143 	 * snapper(4).  But in reality that chip isn't there.  So we
144 	 * match on "layout-id" instead.
145 	 */
146 	if (OF_getprop(soundchip, "layout-id", &layout, sizeof layout) &&
147 	    (layout == 0x2d || layout == 0x56))
148 		return (1);
149 
150 	return (0);
151 }
152 
153 void
154 onyx_attach(struct device *parent, struct device *self, void *aux)
155 {
156 	struct onyx_softc *sc = (struct onyx_softc *)self;
157 
158 	sc->sc_setvolume = onyx_set_volume;
159 
160 	i2s_attach(parent, sc, aux);
161 	config_defer(self, onyx_defer);
162 }
163 
164 void
165 onyx_defer(struct device *dev)
166 {
167 	struct onyx_softc *sc = (struct onyx_softc *)dev;
168 	struct device *dv;
169 
170 	TAILQ_FOREACH(dv, &alldevs, dv_list)
171 		if (strncmp(dv->dv_xname, "kiic", 4) == 0 &&
172 		    strncmp(dv->dv_parent->dv_xname, "macobio", 7) == 0)
173 			sc->sc_i2c = dv;
174 	if (sc->sc_i2c == NULL) {
175 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
176 		return;
177 	}
178 
179 	/* XXX If i2c has failed to attach, what should we do? */
180 
181 	audio_attach_mi(&onyx_hw_if, sc, &sc->sc_dev);
182 
183 	deq_reset(sc);
184 	onyx_set_volume(sc, 192, 192);
185 }
186 
187 int
188 onyx_getdev(void *h, struct audio_device *retp)
189 {
190 	*retp = onyx_device;
191 	return (0);
192 }
193 
194 void
195 onyx_set_volume(struct onyx_softc *sc, int left, int right)
196 {
197 	u_int8_t data;
198 
199 	sc->sc_vol_l = left;
200 	sc->sc_vol_r = right;
201 
202 	kiic_setmode(sc->sc_i2c, I2C_STDSUBMODE, 0);
203 	data = 128 + (left >> 1);
204 	kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR,
205 	    PCM3052_REG_LEFT_VOLUME, &data, 1);
206 	data = 128 + (right >> 1);
207 	kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR,
208 	    PCM3052_REG_RIGHT_VOLUME, &data, 1);
209 }
210 
211 void
212 onyx_get_default_params(void *addr, int mode, struct audio_params *params)
213 {
214 	i2s_get_default_params(params);
215 }
216