1 /*- 2 * Copyright (c) 2016 Justin Hibbits 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/eventhandler.h> 35 #include <sys/gpio.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/reboot.h> 39 40 #include <dev/ofw/ofw_bus.h> 41 42 #include <dev/gpio/gpiobusvar.h> 43 44 struct gpiopower_softc { 45 gpio_pin_t sc_pin; 46 int sc_rbmask; 47 }; 48 49 static void gpiopower_assert(device_t dev, int howto); 50 51 static int 52 gpiopower_probe(device_t dev) 53 { 54 55 if (ofw_bus_is_compatible(dev, "gpio-poweroff")) { 56 device_set_desc(dev, "GPIO poweroff control"); 57 return (0); 58 } else if (ofw_bus_is_compatible(dev, "gpio-restart")) { 59 device_set_desc(dev, "GPIO restart control"); 60 return (0); 61 } 62 63 return (ENXIO); 64 } 65 66 static int 67 gpiopower_attach(device_t dev) 68 { 69 struct gpiopower_softc *sc; 70 phandle_t node; 71 72 sc = device_get_softc(dev); 73 74 if ((node = ofw_bus_get_node(dev)) == -1) 75 return (ENXIO); 76 77 if (ofw_gpiobus_parse_gpios(dev, "gpios", &sc->sc_pin) <= 0) { 78 device_printf(dev, "failed to map GPIO pin\n"); 79 return (ENXIO); 80 } 81 82 if (ofw_bus_is_compatible(dev, "gpio-poweroff")) 83 sc->sc_rbmask = RB_HALT | RB_POWEROFF; 84 else 85 sc->sc_rbmask = 0; 86 EVENTHANDLER_REGISTER(shutdown_final, gpiopower_assert, dev, 87 SHUTDOWN_PRI_LAST); 88 gpio_pin_setflags(sc->sc_pin, GPIO_PIN_OUTPUT); 89 90 return (0); 91 } 92 93 static void 94 gpiopower_assert(device_t dev, int howto) 95 { 96 struct gpiopower_softc *sc; 97 int do_assert; 98 99 sc = device_get_softc(dev); 100 do_assert = sc->sc_rbmask ? (sc->sc_rbmask & howto) : 101 ((howto & RB_HALT) == 0); 102 103 if (!do_assert) 104 return; 105 106 if (howto & RB_POWEROFF) 107 device_printf(dev, "powering system off\n"); 108 else if ((howto & RB_HALT) == 0) 109 device_printf(dev, "resetting system\n"); 110 else 111 return; 112 113 gpio_pin_set_active(sc->sc_pin, true); 114 115 /* Wait a second for it to trip */ 116 DELAY(10000000); 117 } 118 119 static device_method_t gpiopower_methods[] = { 120 /* Device interface */ 121 DEVMETHOD(device_probe, gpiopower_probe), 122 DEVMETHOD(device_attach, gpiopower_attach), 123 124 DEVMETHOD_END 125 }; 126 127 static driver_t gpiopower_driver = { 128 "gpiopower", 129 gpiopower_methods, 130 sizeof(struct gpiopower_softc) 131 }; 132 133 DRIVER_MODULE(gpiopower, simplebus, gpiopower_driver, 0, 0); 134 MODULE_DEPEND(gpiopower, gpiobus, 1, 1, 1); 135