xref: /freebsd/sys/powerpc/mikrotik/rb_led.c (revision 953111c9c536daf5304062914c0083cea5efb46b)
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 static devclass_t rbled_devclass;
71 
72 DRIVER_MODULE(rbled, simplebus, rbled_driver, rbled_devclass, 0, 0);
73 
74 static int
75 rbled_probe(device_t dev)
76 {
77 	phandle_t node;
78 	const char *name;
79 	cell_t gp[2];
80 	char model[6];
81 
82 	node = ofw_bus_get_node(dev);
83 
84 	name = ofw_bus_get_name(dev);
85 	if (name == NULL)
86 		return (ENXIO);
87 	if (strcmp(name, "led") != 0)
88 		return (ENXIO);
89 
90 	if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
91 		return (ENXIO);
92 
93 	/* Check root model. */
94 	node = OF_peer(0);
95 	if (OF_getprop(node, "model", model, sizeof(model)) <= 0)
96 		return (ENXIO);
97 	if (strcmp(model, "RB800") != 0)
98 		return (ENXIO);
99 
100 	device_set_desc(dev, "RouterBoard LED");
101 	return (0);
102 }
103 
104 static int
105 rbled_attach(device_t dev)
106 {
107 	struct rbled_softc *sc;
108 	phandle_t node;
109 	cell_t gp[2];
110 
111 	sc = device_get_softc(dev);
112 	node = ofw_bus_get_node(dev);
113 
114 	if (OF_getprop(node, "user_led", gp, sizeof(gp)) <= 0)
115 		return (ENXIO);
116 
117 	sc->sc_gpio = OF_device_from_xref(gp[0]);
118 	if (sc->sc_gpio == NULL) {
119 		device_printf(dev, "No GPIO resource found!\n");
120 		return (ENXIO);
121 	}
122 	sc->sc_ledpin = gp[1];
123 
124 	sc->sc_led = led_create(rbled_toggle, sc, "user_led");
125 
126 	if (sc->sc_led == NULL)
127 		return (ENXIO);
128 
129 	return (0);
130 }
131 
132 static int
133 rbled_detach(device_t dev)
134 {
135 	struct rbled_softc *sc;
136 
137 	sc = device_get_softc(dev);
138 	led_destroy(sc->sc_led);
139 
140 	return (0);
141 }
142 
143 static void
144 rbled_toggle(void *priv, int onoff)
145 {
146 	struct rbled_softc *sc = priv;
147 
148 	GPIO_PIN_SET(sc->sc_gpio, sc->sc_ledpin, onoff);
149 }
150