i2cmux_fdt.c revision 1.5 1 1.5 jmcneill /* $NetBSD: i2cmux_fdt.c,v 1.5 2021/01/17 19:54:23 jmcneill Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: i2cmux_fdt.c,v 1.5 2021/01/17 19:54:23 jmcneill Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej #include <sys/device.h>
37 1.1 thorpej #include <sys/kernel.h>
38 1.1 thorpej #include <sys/kmem.h>
39 1.1 thorpej #include <sys/bus.h>
40 1.1 thorpej #include <sys/gpio.h>
41 1.1 thorpej
42 1.1 thorpej #include <dev/fdt/fdtvar.h>
43 1.4 thorpej #include <dev/i2c/i2cmuxvar.h>
44 1.1 thorpej
45 1.4 thorpej /*****************************************************************************/
46 1.1 thorpej
47 1.4 thorpej struct mux_info_gpio {
48 1.4 thorpej struct fdtbus_gpio_pin **pins;
49 1.4 thorpej int npins;
50 1.4 thorpej uint32_t idle_value;
51 1.4 thorpej bool has_idle_value;
52 1.1 thorpej };
53 1.1 thorpej
54 1.4 thorpej struct bus_info_gpio {
55 1.4 thorpej bus_addr_t value;
56 1.1 thorpej };
57 1.1 thorpej
58 1.4 thorpej static void *
59 1.1 thorpej iicmux_gpio_get_mux_info(struct iicmux_softc * const sc)
60 1.1 thorpej {
61 1.4 thorpej struct mux_info_gpio *mux_data;
62 1.1 thorpej int i;
63 1.1 thorpej
64 1.4 thorpej mux_data = kmem_zalloc(sizeof(*mux_data), KM_SLEEP);
65 1.4 thorpej
66 1.4 thorpej mux_data->npins = fdtbus_gpio_count(sc->sc_phandle, "mux-gpios");
67 1.4 thorpej if (mux_data->npins == 0) {
68 1.1 thorpej aprint_error_dev(sc->sc_dev,
69 1.1 thorpej "unable to get mux-gpios property\n");
70 1.4 thorpej goto bad;
71 1.1 thorpej }
72 1.1 thorpej
73 1.4 thorpej mux_data->pins =
74 1.4 thorpej kmem_zalloc(sizeof(*mux_data->pins) * mux_data->npins, KM_SLEEP);
75 1.4 thorpej for (i = 0; i < mux_data->npins; i++) {
76 1.4 thorpej mux_data->pins[i] = fdtbus_gpio_acquire_index(sc->sc_phandle,
77 1.1 thorpej "mux-gpios", i, GPIO_PIN_OUTPUT);
78 1.4 thorpej if (mux_data->pins[i] == NULL) {
79 1.1 thorpej aprint_error_dev(sc->sc_dev,
80 1.1 thorpej "unable to acquire gpio #%d\n", i);
81 1.4 thorpej goto bad;
82 1.1 thorpej }
83 1.1 thorpej }
84 1.1 thorpej
85 1.4 thorpej mux_data->has_idle_value =
86 1.1 thorpej of_getprop_uint32(sc->sc_phandle, "idle-state",
87 1.4 thorpej &mux_data->idle_value) == 0;
88 1.1 thorpej
89 1.4 thorpej return mux_data;
90 1.4 thorpej
91 1.4 thorpej bad:
92 1.4 thorpej for (i = 0; i < mux_data->npins; i++) {
93 1.4 thorpej if (mux_data->pins[i] != NULL) {
94 1.4 thorpej fdtbus_gpio_release(mux_data->pins[i]);
95 1.4 thorpej }
96 1.4 thorpej }
97 1.4 thorpej kmem_free(mux_data, sizeof(*mux_data));
98 1.4 thorpej return NULL;
99 1.1 thorpej }
100 1.1 thorpej
101 1.4 thorpej static void *
102 1.1 thorpej iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
103 1.1 thorpej {
104 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
105 1.4 thorpej struct bus_info_gpio *bus_info;
106 1.1 thorpej int error;
107 1.1 thorpej
108 1.4 thorpej bus_info = kmem_zalloc(sizeof(*bus_info), KM_SLEEP);
109 1.4 thorpej
110 1.4 thorpej error = fdtbus_get_reg(bus->phandle, 0, &bus_info->value, NULL);
111 1.1 thorpej if (error) {
112 1.1 thorpej aprint_error_dev(sc->sc_dev,
113 1.1 thorpej "unable to get reg property for bus %d\n", bus->busidx);
114 1.4 thorpej kmem_free(bus_info, sizeof(*bus_info));
115 1.4 thorpej return NULL;
116 1.1 thorpej }
117 1.1 thorpej
118 1.4 thorpej return bus_info;
119 1.1 thorpej }
120 1.1 thorpej
121 1.1 thorpej static void
122 1.1 thorpej iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value)
123 1.1 thorpej {
124 1.4 thorpej struct mux_info_gpio * const mux_info = sc->sc_mux_data;
125 1.1 thorpej int i;
126 1.1 thorpej
127 1.4 thorpej for (i = 0; i < mux_info->npins; i++, value >>= 1) {
128 1.4 thorpej fdtbus_gpio_write(mux_info->pins[i], value & 1);
129 1.1 thorpej }
130 1.1 thorpej }
131 1.1 thorpej
132 1.1 thorpej static int
133 1.1 thorpej iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused)
134 1.1 thorpej {
135 1.4 thorpej struct bus_info_gpio * const bus_info = bus->bus_data;
136 1.4 thorpej
137 1.4 thorpej iicmux_gpio_set_value(bus->mux, bus_info->value);
138 1.1 thorpej
139 1.1 thorpej return 0;
140 1.1 thorpej }
141 1.1 thorpej
142 1.1 thorpej static void
143 1.1 thorpej iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused)
144 1.1 thorpej {
145 1.4 thorpej struct iicmux_softc * const sc = bus->mux;
146 1.4 thorpej struct mux_info_gpio * const mux_info = sc->sc_mux_data;
147 1.1 thorpej
148 1.4 thorpej if (mux_info->has_idle_value) {
149 1.4 thorpej iicmux_gpio_set_value(sc, mux_info->idle_value);
150 1.1 thorpej }
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej static const struct iicmux_config iicmux_gpio_config = {
154 1.1 thorpej .desc = "GPIO",
155 1.1 thorpej .get_mux_info = iicmux_gpio_get_mux_info,
156 1.1 thorpej .get_bus_info = iicmux_gpio_get_bus_info,
157 1.1 thorpej .acquire_bus = iicmux_gpio_acquire_bus,
158 1.1 thorpej .release_bus = iicmux_gpio_release_bus,
159 1.1 thorpej };
160 1.1 thorpej
161 1.1 thorpej /*****************************************************************************/
162 1.1 thorpej
163 1.4 thorpej struct mux_info_pinctrl {
164 1.4 thorpej u_int idle_idx;
165 1.4 thorpej bool has_idle_idx;
166 1.4 thorpej } sc_pinctrl;
167 1.4 thorpej
168 1.4 thorpej struct bus_info_pinctrl {
169 1.4 thorpej bus_addr_t idx;
170 1.4 thorpej };
171 1.4 thorpej
172 1.4 thorpej static void *
173 1.1 thorpej iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
174 1.1 thorpej {
175 1.4 thorpej struct mux_info_pinctrl *mux_info;
176 1.4 thorpej
177 1.4 thorpej mux_info = kmem_alloc(sizeof(*mux_info), KM_SLEEP);
178 1.1 thorpej
179 1.4 thorpej mux_info->has_idle_idx =
180 1.1 thorpej fdtbus_get_index(sc->sc_phandle, "pinctrl-names", "idle",
181 1.4 thorpej &mux_info->idle_idx) == 0;
182 1.1 thorpej
183 1.4 thorpej return mux_info;
184 1.1 thorpej }
185 1.1 thorpej
186 1.4 thorpej static void *
187 1.1 thorpej iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
188 1.1 thorpej {
189 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
190 1.4 thorpej struct bus_info_pinctrl *bus_info;
191 1.1 thorpej int error;
192 1.1 thorpej
193 1.4 thorpej bus_info = kmem_alloc(sizeof(*bus_info), KM_SLEEP);
194 1.4 thorpej
195 1.4 thorpej error = fdtbus_get_reg(bus->phandle, 0, &bus_info->idx, NULL);
196 1.1 thorpej if (error) {
197 1.1 thorpej aprint_error_dev(sc->sc_dev,
198 1.1 thorpej "unable to get reg property for bus %d\n", bus->busidx);
199 1.4 thorpej kmem_free(bus_info, sizeof(*bus_info));
200 1.4 thorpej return NULL;
201 1.1 thorpej }
202 1.1 thorpej
203 1.4 thorpej return bus_info;
204 1.1 thorpej }
205 1.1 thorpej
206 1.1 thorpej static int
207 1.1 thorpej iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus,
208 1.1 thorpej int const flags __unused)
209 1.1 thorpej {
210 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
211 1.4 thorpej struct bus_info_pinctrl * const bus_info = bus->bus_data;
212 1.1 thorpej
213 1.4 thorpej return fdtbus_pinctrl_set_config_index(sc->sc_phandle, bus_info->idx);
214 1.1 thorpej }
215 1.1 thorpej
216 1.1 thorpej static void
217 1.1 thorpej iicmux_pinctrl_release_bus(struct iicmux_bus * const bus,
218 1.1 thorpej int const flags __unused)
219 1.1 thorpej {
220 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
221 1.4 thorpej struct mux_info_pinctrl * const mux_info = sc->sc_mux_data;
222 1.1 thorpej
223 1.4 thorpej if (mux_info->has_idle_idx) {
224 1.1 thorpej (void) fdtbus_pinctrl_set_config_index(sc->sc_phandle,
225 1.4 thorpej mux_info->idle_idx);
226 1.1 thorpej }
227 1.1 thorpej }
228 1.1 thorpej
229 1.1 thorpej static const struct iicmux_config iicmux_pinctrl_config = {
230 1.1 thorpej .desc = "PinMux",
231 1.1 thorpej .get_mux_info = iicmux_pinctrl_get_mux_info,
232 1.1 thorpej .get_bus_info = iicmux_pinctrl_get_bus_info,
233 1.1 thorpej .acquire_bus = iicmux_pinctrl_acquire_bus,
234 1.1 thorpej .release_bus = iicmux_pinctrl_release_bus,
235 1.1 thorpej };
236 1.1 thorpej
237 1.1 thorpej /*****************************************************************************/
238 1.1 thorpej
239 1.1 thorpej static const struct of_compat_data compat_data[] = {
240 1.1 thorpej { .compat = "i2c-mux-gpio",
241 1.1 thorpej .data = (uintptr_t)&iicmux_gpio_config },
242 1.1 thorpej
243 1.1 thorpej { .compat = "i2c-mux-pinctrl",
244 1.1 thorpej .data = (uintptr_t)&iicmux_pinctrl_config },
245 1.1 thorpej
246 1.1 thorpej { NULL }
247 1.1 thorpej };
248 1.1 thorpej
249 1.1 thorpej static int
250 1.1 thorpej iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux)
251 1.1 thorpej {
252 1.1 thorpej struct fdt_attach_args * const faa = aux;
253 1.1 thorpej
254 1.1 thorpej return of_match_compat_data(faa->faa_phandle, compat_data);
255 1.1 thorpej }
256 1.1 thorpej
257 1.1 thorpej static void
258 1.1 thorpej iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux)
259 1.1 thorpej {
260 1.1 thorpej struct iicmux_softc * const sc = device_private(self);
261 1.1 thorpej struct fdt_attach_args * const faa = aux;
262 1.1 thorpej
263 1.1 thorpej sc->sc_dev = self;
264 1.1 thorpej sc->sc_phandle = faa->faa_phandle;
265 1.1 thorpej sc->sc_config = (const struct iicmux_config *)
266 1.1 thorpej of_search_compatible(sc->sc_phandle, compat_data)->data;
267 1.1 thorpej
268 1.1 thorpej aprint_naive("\n");
269 1.1 thorpej aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
270 1.1 thorpej
271 1.1 thorpej sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_phandle, "i2c-parent");
272 1.1 thorpej if (sc->sc_i2c_parent == NULL) {
273 1.1 thorpej aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
274 1.1 thorpej return;
275 1.1 thorpej }
276 1.1 thorpej
277 1.4 thorpej iicmux_attach(sc);
278 1.1 thorpej }
279 1.1 thorpej
280 1.1 thorpej CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc),
281 1.5 jmcneill iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL);
282