xref: /freebsd/sys/powerpc/mikrotik/rb_led.c (revision 38a52bd3)
1 /*-
2  * Copyright (c) 2017 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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/openfirm.h>
40 #include <dev/led/led.h>
41 
42 #include "gpio_if.h"
43 
44 struct rbled_softc {
45 	struct cdev	*sc_led;
46 	device_t	 sc_gpio;
47 	uint32_t	 sc_ledpin;
48 };
49 
50 static int	rbled_probe(device_t);
51 static int	rbled_attach(device_t);
52 static int	rbled_detach(device_t);
53 static void	rbled_toggle(void *, int);
54 
55 static device_method_t  rbled_methods[] = {
56 	/* Device interface */
57 	DEVMETHOD(device_probe,		rbled_probe),
58 	DEVMETHOD(device_attach,	rbled_attach),
59         DEVMETHOD(device_detach,        rbled_detach),
60 
61 	DEVMETHOD_END
62 };
63 
64 static driver_t rbled_driver = {
65 	"rbled",
66 	rbled_methods,
67 	sizeof(struct rbled_softc),
68 };
69 
70 DRIVER_MODULE(rbled, simplebus, rbled_driver, 0, 0);
71 
72 static int
73 rbled_probe(device_t dev)
74 {
75 	phandle_t node;
76 	const char *name;
77 	cell_t gp[2];
78 	char model[6];
79 
80 	node = ofw_bus_get_node(dev);
81 
82 	name = ofw_bus_get_name(dev);
83 	if (name == NULL)
84 		return (ENXIO);
85 	if (strcmp(name, "led") != 0)
86 		return (ENXIO);
87 
88 	if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
89 		return (ENXIO);
90 
91 	/* Check root model. */
92 	node = OF_peer(0);
93 	if (OF_getprop(node, "model", model, sizeof(model)) <= 0)
94 		return (ENXIO);
95 	if (strcmp(model, "RB800") != 0)
96 		return (ENXIO);
97 
98 	device_set_desc(dev, "RouterBoard LED");
99 	return (0);
100 }
101 
102 static int
103 rbled_attach(device_t dev)
104 {
105 	struct rbled_softc *sc;
106 	phandle_t node;
107 	cell_t gp[2];
108 
109 	sc = device_get_softc(dev);
110 	node = ofw_bus_get_node(dev);
111 
112 	if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
113 		return (ENXIO);
114 
115 	sc->sc_gpio = OF_device_from_xref(gp[0]);
116 	if (sc->sc_gpio == NULL) {
117 		device_printf(dev, "No GPIO resource found!\n");
118 		return (ENXIO);
119 	}
120 	sc->sc_ledpin = gp[1];
121 
122 	sc->sc_led = led_create(rbled_toggle, sc, "user_led");
123 
124 	if (sc->sc_led == NULL)
125 		return (ENXIO);
126 
127 	return (0);
128 }
129 
130 static int
131 rbled_detach(device_t dev)
132 {
133 	struct rbled_softc *sc;
134 
135 	sc = device_get_softc(dev);
136 	led_destroy(sc->sc_led);
137 
138 	return (0);
139 }
140 
141 static void
142 rbled_toggle(void *priv, int onoff)
143 {
144 	struct rbled_softc *sc = priv;
145 
146 	GPIO_PIN_SET(sc->sc_gpio, sc->sc_ledpin, onoff);
147 }
148