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