]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/gpio/gpiopower.c
Upgrade Unbound to 1.6.2. More to follow.
[FreeBSD/FreeBSD.git] / sys / dev / gpio / gpiopower.c
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         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");
80                 return (ENXIO);
81         }
82
83         if (ofw_bus_is_compatible(dev, "gpio-poweroff"))
84                 sc->sc_rbmask = RB_HALT | RB_POWEROFF;
85         else
86                 sc->sc_rbmask = 0;
87         EVENTHANDLER_REGISTER(shutdown_final, gpiopower_assert, dev,
88             SHUTDOWN_PRI_LAST);
89         gpio_pin_setflags(sc->sc_pin, GPIO_PIN_OUTPUT);
90
91         return (0);
92 }
93
94 static void
95 gpiopower_assert(device_t dev, int howto)
96 {
97         struct gpiopower_softc *sc;
98         int do_assert;
99
100         sc = device_get_softc(dev);
101         do_assert = sc->sc_rbmask ? (sc->sc_rbmask & howto) :
102             ((howto & RB_HALT) == 0);
103
104         if (!do_assert)
105                 return;
106
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");
111         else
112                 return;
113
114         gpio_pin_set_active(sc->sc_pin, true);
115
116         /* Wait a second for it to trip */
117         DELAY(10000000);
118 }
119
120 static devclass_t gpiopower_devclass;
121
122 static device_method_t gpiopower_methods[] = {
123         /* Device interface */
124         DEVMETHOD(device_probe,         gpiopower_probe),
125         DEVMETHOD(device_attach,        gpiopower_attach),
126
127         DEVMETHOD_END
128 };
129
130 static driver_t gpiopower_driver = {
131         "gpiopower",
132         gpiopower_methods,
133         sizeof(struct gpiopower_softc)
134 };
135
136 DRIVER_MODULE(gpiopower, simplebus, gpiopower_driver, gpiopower_devclass, 0, 0);
137 MODULE_DEPEND(gpiopower, gpiobus, 1, 1, 1);