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