xref: /freebsd/sys/dev/gpio/gpiopower.c (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
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/fdt/fdt_common.h>
38 #include <dev/ofw/ofw_bus.h>
39 
40 #include <dev/gpio/gpiobusvar.h>
41 
42 struct gpiopower_softc {
43 	gpio_pin_t	sc_pin;
44 	int		sc_rbmask;
45 	int		sc_hi_period;
46 	int		sc_lo_period;
47 	int		sc_timeout;
48 };
49 
50 static void gpiopower_assert(device_t dev, int howto);
51 
52 static int
53 gpiopower_probe(device_t dev)
54 {
55 
56 	if (ofw_bus_is_compatible(dev, "gpio-poweroff")) {
57 		device_set_desc(dev, "GPIO poweroff control");
58 		return (0);
59 	} else if (ofw_bus_is_compatible(dev, "gpio-restart")) {
60 		device_set_desc(dev, "GPIO restart control");
61 		return (0);
62 	}
63 
64 	return (ENXIO);
65 }
66 
67 static int
68 gpiopower_attach(device_t dev)
69 {
70 	struct gpiopower_softc *sc;
71 	phandle_t node;
72 	uint32_t prop;
73 
74 	sc = device_get_softc(dev);
75 
76 	if ((node = ofw_bus_get_node(dev)) == -1)
77 		return (ENXIO);
78 
79 	if (ofw_gpiobus_parse_gpios(dev, "gpios", &sc->sc_pin) <= 0) {
80 		device_printf(dev, "failed to map GPIO pin\n");
81 		return (ENXIO);
82 	}
83 
84 	if (ofw_bus_is_compatible(dev, "gpio-poweroff"))
85 		sc->sc_rbmask = RB_HALT | RB_POWEROFF;
86 	else
87 		sc->sc_rbmask = 0;
88 
89 	sc->sc_hi_period = 100000;
90 	sc->sc_lo_period = 100000;
91 	sc->sc_timeout = 1000000;
92 
93 	if ((OF_getprop(node, "active-delay-ms", &prop, sizeof(prop))) > 0)
94 		sc->sc_hi_period = fdt32_to_cpu(prop) * 1000;
95 	if ((OF_getprop(node, "inactive-delay-ms", &prop, sizeof(prop))) > 0)
96 		sc->sc_lo_period = fdt32_to_cpu(prop) * 1000;
97 	if ((OF_getprop(node, "timeout-ms", &prop, sizeof(prop))) > 0)
98 		sc->sc_timeout = fdt32_to_cpu(prop) * 1000;
99 
100 	EVENTHANDLER_REGISTER(shutdown_final, gpiopower_assert, dev,
101 	    SHUTDOWN_PRI_LAST);
102 
103 	return (0);
104 }
105 
106 static void
107 gpiopower_assert(device_t dev, int howto)
108 {
109 	struct gpiopower_softc *sc;
110 	int do_assert;
111 
112 	sc = device_get_softc(dev);
113 	do_assert = sc->sc_rbmask ? (sc->sc_rbmask & howto) :
114 	    ((howto & RB_HALT) == 0);
115 
116 	if (!do_assert)
117 		return;
118 
119 	if (howto & RB_POWEROFF)
120 		device_printf(dev, "powering system off\n");
121 	else if ((howto & RB_HALT) == 0)
122 		device_printf(dev, "resetting system\n");
123 	else
124 		return;
125 
126 	gpio_pin_setflags(sc->sc_pin, GPIO_PIN_OUTPUT);
127 	gpio_pin_set_active(sc->sc_pin, true);
128 	DELAY(sc->sc_hi_period);
129 	gpio_pin_set_active(sc->sc_pin, false);
130 	DELAY(sc->sc_lo_period);
131 	gpio_pin_set_active(sc->sc_pin, true);
132 	DELAY(sc->sc_timeout);
133 
134 	device_printf(dev, "%s failed\n",
135 	    (howto & RB_POWEROFF) != 0 ? "power off" : "reset");
136 }
137 
138 static device_method_t gpiopower_methods[] = {
139 	/* Device interface */
140 	DEVMETHOD(device_probe,		gpiopower_probe),
141 	DEVMETHOD(device_attach,	gpiopower_attach),
142 
143 	DEVMETHOD_END
144 };
145 
146 static driver_t gpiopower_driver = {
147 	"gpiopower",
148 	gpiopower_methods,
149 	sizeof(struct gpiopower_softc)
150 };
151 
152 DRIVER_MODULE(gpiopower, simplebus, gpiopower_driver, 0, 0);
153 MODULE_DEPEND(gpiopower, gpiobus, 1, 1, 1);
154