2 * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
46 #include <dev/gpio/gpiobusvar.h>
48 #include "gpiobus_if.h"
51 * Only one pin for led
55 #define GPIOBL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
56 #define GPIOBL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
57 #define GPIOBL_LOCK_INIT(_sc) \
58 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
59 "gpiobacklight", MTX_DEF)
60 #define GPIOBL_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
62 struct gpiobacklight_softc
68 struct sysctl_oid *sc_oid;
72 static int gpiobacklight_sysctl(SYSCTL_HANDLER_ARGS);
73 static void gpiobacklight_update_brightness(struct gpiobacklight_softc *);
74 static int gpiobacklight_probe(device_t);
75 static int gpiobacklight_attach(device_t);
76 static int gpiobacklight_detach(device_t);
79 gpiobacklight_update_brightness(struct gpiobacklight_softc *sc)
84 error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev,
90 error = GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev,
91 GPIOBL_PIN, GPIO_PIN_OUTPUT);
93 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOBL_PIN,
94 sc->sc_brightness ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
95 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
100 gpiobacklight_sysctl(SYSCTL_HANDLER_ARGS)
102 struct gpiobacklight_softc *sc;
106 sc = (struct gpiobacklight_softc*)arg1;
107 brightness = sc->sc_brightness;
108 error = sysctl_handle_int(oidp, &brightness, 0, req);
110 if (error != 0 || req->newptr == NULL)
114 sc->sc_brightness = 0;
116 sc->sc_brightness = 1;
118 gpiobacklight_update_brightness(sc);
124 gpiobacklight_identify(driver_t *driver, device_t bus)
126 phandle_t node, root;
128 root = OF_finddevice("/");
131 for (node = OF_child(root); node != 0; node = OF_peer(node)) {
132 if (!fdt_is_compatible_strict(node, "gpio-backlight"))
134 ofw_gpiobus_add_fdt_child(bus, driver->name, node);
139 gpiobacklight_probe(device_t dev)
142 if (!ofw_bus_is_compatible(dev, "gpio-backlight"))
145 device_set_desc(dev, "GPIO backlight");
151 gpiobacklight_attach(device_t dev)
153 struct gpiobacklight_softc *sc;
154 struct sysctl_ctx_list *ctx;
155 struct sysctl_oid *tree;
158 sc = device_get_softc(dev);
160 sc->sc_busdev = device_get_parent(dev);
162 if ((node = ofw_bus_get_node(dev)) == -1)
165 GPIOBL_LOCK_INIT(sc);
166 if (OF_hasprop(node, "default-on"))
167 sc->sc_brightness = 1;
169 sc->sc_brightness = 0;
171 /* Init backlight interface */
172 ctx = device_get_sysctl_ctx(sc->sc_dev);
173 tree = device_get_sysctl_tree(sc->sc_dev);
174 sc->sc_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
175 "brightness", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
176 gpiobacklight_sysctl, "I", "backlight brightness");
178 gpiobacklight_update_brightness(sc);
184 gpiobacklight_detach(device_t dev)
186 struct gpiobacklight_softc *sc;
188 sc = device_get_softc(dev);
189 GPIOBL_LOCK_DESTROY(sc);
193 static devclass_t gpiobacklight_devclass;
195 static device_method_t gpiobacklight_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_identify, gpiobacklight_identify),
198 DEVMETHOD(device_probe, gpiobacklight_probe),
199 DEVMETHOD(device_attach, gpiobacklight_attach),
200 DEVMETHOD(device_detach, gpiobacklight_detach),
205 static driver_t gpiobacklight_driver = {
207 gpiobacklight_methods,
208 sizeof(struct gpiobacklight_softc),
211 DRIVER_MODULE(gpiobacklight, gpiobus, gpiobacklight_driver, gpiobacklight_devclass, 0, 0);