2 * Copyright (c) 2016 Justin Hibbits
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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * 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>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
40 #include <dev/ofw/ofw_bus.h>
42 #include <dev/gpio/gpiobusvar.h>
44 struct gpiopower_softc {
49 static void gpiopower_assert(device_t dev, int howto);
52 gpiopower_probe(device_t dev)
55 if (ofw_bus_is_compatible(dev, "gpio-poweroff")) {
56 device_set_desc(dev, "GPIO poweroff control");
58 } else if (ofw_bus_is_compatible(dev, "gpio-restart")) {
59 device_set_desc(dev, "GPIO restart control");
67 gpiopower_attach(device_t dev)
69 struct gpiopower_softc *sc;
72 sc = device_get_softc(dev);
74 if ((node = ofw_bus_get_node(dev)) == -1)
77 ofw_gpiobus_parse_gpios(dev, "gpios", &sc->sc_pin);
78 if (sc->sc_pin == NULL) {
79 device_printf(dev, "failed to map GPIO pin\n");
83 if (ofw_bus_is_compatible(dev, "gpio-poweroff"))
84 sc->sc_rbmask = RB_HALT | RB_POWEROFF;
87 EVENTHANDLER_REGISTER(shutdown_final, gpiopower_assert, dev,
89 gpio_pin_setflags(sc->sc_pin, GPIO_PIN_OUTPUT);
95 gpiopower_assert(device_t dev, int howto)
97 struct gpiopower_softc *sc;
100 sc = device_get_softc(dev);
101 do_assert = sc->sc_rbmask ? (sc->sc_rbmask & howto) :
102 ((howto & RB_HALT) == 0);
107 if (howto & RB_POWEROFF)
108 device_printf(dev, "powering system off\n");
109 else if ((howto & RB_HALT) == 0)
110 device_printf(dev, "resetting system\n");
114 gpio_pin_set_active(sc->sc_pin, true);
116 /* Wait a second for it to trip */
120 static devclass_t gpiopower_devclass;
122 static device_method_t gpiopower_methods[] = {
123 /* Device interface */
124 DEVMETHOD(device_probe, gpiopower_probe),
125 DEVMETHOD(device_attach, gpiopower_attach),
130 static driver_t gpiopower_driver = {
133 sizeof(struct gpiopower_softc)
136 DRIVER_MODULE(gpiopower, simplebus, gpiopower_driver, gpiopower_devclass, 0, 0);
137 MODULE_DEPEND(gpiopower, gpiobus, 1, 1, 1);