xref: /openbsd/sys/arch/macppc/dev/onyx.c (revision 09467b48)
1 /*	$OpenBSD: onyx.c,v 1.12 2016/09/19 06:46:43 ratchov 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_match(struct device *, void *, void *);
71 void onyx_attach(struct device *, struct device *, void *);
72 void onyx_defer(struct device *);
73 void onyx_set_volume(struct onyx_softc *, int, int);
74 
75 struct cfattach onyx_ca = {
76 	sizeof(struct onyx_softc), onyx_match, onyx_attach
77 };
78 
79 struct cfdriver onyx_cd = {
80 	NULL, "onyx", DV_DULL
81 };
82 
83 struct audio_hw_if onyx_hw_if = {
84 	i2s_open,
85 	i2s_close,
86 	i2s_set_params,
87 	i2s_round_blocksize,
88 	NULL,
89 	NULL,
90 	NULL,
91 	NULL,
92 	NULL,
93 	i2s_halt_output,
94 	i2s_halt_input,
95 	NULL,
96 	NULL,
97 	i2s_set_port,
98 	i2s_get_port,
99 	i2s_query_devinfo,
100 	i2s_allocm,
101 	NULL,
102 	i2s_round_buffersize,
103 	i2s_get_props,
104 	i2s_trigger_output,
105 	i2s_trigger_input
106 };
107 
108 int
109 onyx_match(struct device *parent, void *match, void *aux)
110 {
111 	struct confargs *ca = aux;
112 	int soundbus, soundchip, soundcodec;
113 	int32_t layout = 0;
114 
115 	if (strcmp(ca->ca_name, "i2s") != 0)
116 		return (0);
117 
118 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
119 	    (soundchip = OF_child(soundbus)) == 0)
120 		return (0);
121 
122 	if (OF_getprop(soundchip, "platform-onyx-codec-ref",
123 	    &soundcodec, sizeof soundcodec) == sizeof soundcodec)
124 		return (1);
125 
126 	/*
127 	 * Apple really messed up.  First and second generation iMac
128 	 * G5 (PowerMac8,1 and PowerMac8,2) have a "deq" i2c device
129 	 * listed in the OF device tree, which is a telltale sign of
130 	 * snapper(4).  But in reality that chip isn't there.  So we
131 	 * match on "layout-id" instead.
132 	 */
133 	if (OF_getprop(soundchip, "layout-id", &layout, sizeof layout) &&
134 	    (layout == 0x2d || layout == 0x56))
135 		return (1);
136 
137 	return (0);
138 }
139 
140 void
141 onyx_attach(struct device *parent, struct device *self, void *aux)
142 {
143 	struct onyx_softc *sc = (struct onyx_softc *)self;
144 
145 	sc->sc_setvolume = onyx_set_volume;
146 
147 	i2s_attach(parent, sc, aux);
148 	config_defer(self, onyx_defer);
149 }
150 
151 void
152 onyx_defer(struct device *dev)
153 {
154 	struct onyx_softc *sc = (struct onyx_softc *)dev;
155 	struct device *dv;
156 
157 	TAILQ_FOREACH(dv, &alldevs, dv_list)
158 		if (strcmp(dv->dv_cfdata->cf_driver->cd_name, "kiic") == 0 &&
159 		    strcmp(dv->dv_parent->dv_cfdata->cf_driver->cd_name, "macobio") == 0)
160 			sc->sc_i2c = dv;
161 	if (sc->sc_i2c == NULL) {
162 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
163 		return;
164 	}
165 
166 	/* XXX If i2c has failed to attach, what should we do? */
167 
168 	audio_attach_mi(&onyx_hw_if, sc, &sc->sc_dev);
169 
170 	deq_reset(sc);
171 	onyx_set_volume(sc, 192, 192);
172 }
173 
174 void
175 onyx_set_volume(struct onyx_softc *sc, int left, int right)
176 {
177 	u_int8_t data;
178 
179 	sc->sc_vol_l = left;
180 	sc->sc_vol_r = right;
181 
182 	kiic_setmode(sc->sc_i2c, I2C_STDSUBMODE, 0);
183 	data = 128 + (left >> 1);
184 	kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR,
185 	    PCM3052_REG_LEFT_VOLUME, &data, 1);
186 	data = 128 + (right >> 1);
187 	kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR,
188 	    PCM3052_REG_RIGHT_VOLUME, &data, 1);
189 }
190