source: rtems-libbsd/rtemsbsd/sys/arm/freescale/imx/imx_rtems_gpio.c @ af0fcc3

Last change on this file since af0fcc3 was af0fcc3, checked in by Christian Mauderer <christian.mauderer@…>, on 07/13/23 at 13:42:24

imx: Enable GPIO driver for imxrt too

  • Property mode set to 100644
File size: 7.4 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2/* SPDX-License-Identifier: BSD-2-Clause */
3
4/*
5 * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <bsp.h>
30#if defined(LIBBSP_ARM_IMX_BSP_H) || defined(LIBBSP_ARM_IMXRT_BSP_H)
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/gpio.h>
40
41#include <dev/fdt/fdt_common.h>
42#include <dev/gpio/gpiobusvar.h>
43#include <dev/ofw/ofw_bus.h>
44#include <dev/ofw/ofw_bus_subr.h>
45
46#include <bsp/imx-gpio.h>
47
48#define MAXPIN          (32)
49
50#define VALID_PIN(u)    ((u) >= 0 && (u) <= MAXPIN)
51
52struct imx_rtems_gpio_softc {
53        device_t                dev;
54        device_t                busdev;
55        struct imx_gpio         *imx_gpio;
56        struct {
57                struct imx_gpio_pin     pin;
58                bool                    initialized;
59                uint32_t                flags;
60        } pin[MAXPIN];
61};
62
63static device_t
64imx_rtems_gpio_get_bus(device_t dev)
65{
66        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
67
68        return (sc->busdev);
69}
70
71static int
72imx_rtems_gpio_pin_max(device_t dev, int *maxpin)
73{
74
75        *maxpin = MAXPIN;
76        return (0);
77}
78
79static int
80imx_rtems_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
81{
82
83        if (!VALID_PIN(pin))
84                return (EINVAL);
85
86        *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
87
88        return (0);
89}
90
91/* Get a specific pin's name. */
92static int
93imx_rtems_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
94{
95        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
96
97        if (!VALID_PIN(pin))
98                return (EINVAL);
99
100        snprintf(name, GPIOMAXNAME, "%s_%d",
101            imx_gpio_get_name(sc->imx_gpio), pin);
102        name[GPIOMAXNAME-1] = '\0';
103
104        return (0);
105}
106
107static void
108imx_rtems_gpio_pin_init(struct imx_rtems_gpio_softc *sc, uint32_t pin)
109{
110        imx_gpio_init(&sc->pin[pin].pin);
111        sc->pin[pin].initialized = true;
112}
113
114static int
115imx_rtems_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
116{
117        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
118
119        if (!VALID_PIN(pin) || value > 1)
120                return (EINVAL);
121
122        if (!sc->pin[pin].initialized) {
123                imx_rtems_gpio_pin_init(sc, pin);
124        }
125
126        imx_gpio_set_output(&sc->pin[pin].pin, value);
127
128        return (0);
129}
130
131static int
132imx_rtems_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
133{
134        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
135
136        if (!VALID_PIN(pin))
137                return (EINVAL);
138
139        if (!sc->pin[pin].initialized) {
140                imx_rtems_gpio_pin_init(sc, pin);
141        }
142
143        *value = imx_gpio_get_input(&sc->pin[pin].pin);
144
145        return (0);
146}
147
148static int
149imx_rtems_gpio_pin_toggle(device_t dev, uint32_t pin)
150{
151        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
152
153        if (!VALID_PIN(pin))
154                return (EINVAL);
155
156        if (!sc->pin[pin].initialized) {
157                imx_rtems_gpio_pin_init(sc, pin);
158        }
159
160        imx_gpio_toggle_output(&sc->pin[pin].pin);
161
162        return (0);
163}
164
165static int
166imx_rtems_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
167{
168        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
169
170        if (!VALID_PIN(pin))
171                return (EINVAL);
172
173        *flags = sc->pin[pin].flags;
174
175        return (0);
176}
177
178static int
179imx_rtems_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
180{
181        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
182
183        if (!VALID_PIN(pin))
184                return (EINVAL);
185
186        if (flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) {
187                if (flags & GPIO_PIN_PRESET_LOW) {
188                        imx_gpio_set_output(&sc->pin[pin].pin, 0);
189                } else if (flags & GPIO_PIN_PRESET_HIGH) {
190                        imx_gpio_set_output(&sc->pin[pin].pin, 1);
191                }
192                if (flags & GPIO_PIN_OUTPUT) {
193                        sc->pin[pin].pin.mode = IMX_GPIO_MODE_OUTPUT;
194                } else {
195                        sc->pin[pin].pin.mode = IMX_GPIO_MODE_INPUT;
196                }
197                imx_rtems_gpio_pin_init(sc, pin);
198                sc->pin[pin].flags = flags;
199        } else {
200                return (EINVAL);
201        }
202
203        return (0);
204}
205
206static struct ofw_compat_data compat_data[] = {
207        {"fsl,imx35-gpio",  1},
208        {"fsl,imx7d-gpio",  1},
209        {NULL,              0}
210};
211
212static int
213imx_rtems_gpio_probe(device_t dev)
214{
215        if (!ofw_bus_status_okay(dev))
216                return (ENXIO);
217
218        if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
219                device_set_desc(dev, "Freescale i.MX GPIO Controller (RTEMS)");
220                return (BUS_PROBE_DEFAULT);
221        }
222       
223        return (ENXIO);
224}
225
226static int
227imx_rtems_gpio_attach(device_t dev)
228{
229        struct imx_rtems_gpio_softc *sc = device_get_softc(dev);
230        u_long base, size;
231        size_t i;
232
233        sc->dev = dev;
234
235        /* Get register address */
236        if (fdt_regsize(ofw_bus_get_node(sc->dev), &base, &size) != 0) {
237                device_printf(dev, "Can't get register address");
238                return (EINVAL);
239        }
240
241        sc->imx_gpio = imx_gpio_get_by_register((void *)base);
242        if (sc->imx_gpio == NULL) {
243                device_printf(dev, "Can't get RTEMS imx_gpio handle");
244                return (EINVAL);
245        }
246
247        memset(sc->pin, 0, sizeof(sc->pin));
248        for (i = 0; i < MAXPIN; ++i) {
249                sc->pin[i].pin.gpio = sc->imx_gpio;
250                sc->pin[i].pin.mask = 1 << i;
251                sc->pin[i].pin.shift = i;
252                sc->pin[i].pin.mode = IMX_GPIO_MODE_INPUT;
253        }
254        /*
255         * Do _not_ initialize the pins here. Otherwise we would overwrite prior
256         * initializations done by other application parts.
257         */
258
259        sc->busdev = gpiobus_attach_bus(dev);
260        if (sc->busdev == NULL) {
261                return (ENOMEM);
262        }
263
264        return (0);
265}
266
267static int
268imx_rtems_gpio_detach(device_t dev)
269{
270        gpiobus_detach_bus(dev);
271
272        return (0);
273}
274
275static device_method_t imx_rtems_gpio_methods[] = {
276        /* device_if */
277        DEVMETHOD(device_probe,         imx_rtems_gpio_probe),
278        DEVMETHOD(device_attach,        imx_rtems_gpio_attach),
279        DEVMETHOD(device_detach,        imx_rtems_gpio_detach),
280
281        /* GPIO protocol */
282        DEVMETHOD(gpio_get_bus,         imx_rtems_gpio_get_bus),
283        DEVMETHOD(gpio_pin_max,         imx_rtems_gpio_pin_max),
284        DEVMETHOD(gpio_pin_getname,     imx_rtems_gpio_pin_getname),
285        DEVMETHOD(gpio_pin_getflags,    imx_rtems_gpio_pin_getflags),
286        DEVMETHOD(gpio_pin_getcaps,     imx_rtems_gpio_pin_getcaps),
287        DEVMETHOD(gpio_pin_get,         imx_rtems_gpio_pin_get),
288        DEVMETHOD(gpio_pin_setflags,    imx_rtems_gpio_pin_setflags),
289        DEVMETHOD(gpio_pin_set,         imx_rtems_gpio_pin_set),
290        DEVMETHOD(gpio_pin_toggle,      imx_rtems_gpio_pin_toggle),
291
292        DEVMETHOD_END
293};
294
295static driver_t imx_rtems_gpio_driver = {
296        "gpio",
297        imx_rtems_gpio_methods,
298        sizeof(struct imx_rtems_gpio_softc),
299};
300static devclass_t imx_rtems_gpio_devclass;
301
302EARLY_DRIVER_MODULE(imx_rtems_gpio, simplebus, imx_rtems_gpio_driver,
303    imx_rtems_gpio_devclass, NULL, NULL,
304    BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
305
306#endif /* LIBBSP_ARM_IMX_BSP_H || LIBBSP_ARM_IMXRT_BSP_H */
Note: See TracBrowser for help on using the repository browser.