xref: /freebsd/sys/dev/gpio/gpiopps.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2016 Ian Lepore <ian@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, 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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/gpio.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/timepps.h>
38 
39 #include <dev/gpio/gpiobusvar.h>
40 
41 #include "opt_platform.h"
42 
43 #ifdef FDT
44 #include <dev/ofw/ofw_bus.h>
45 
46 static struct ofw_compat_data compat_data[] = {
47 	{"pps-gpio", 	1},
48 	{NULL,          0}
49 };
50 SIMPLEBUS_PNP_INFO(compat_data);
51 #endif /* FDT */
52 
53 struct pps_softc {
54 	device_t         dev;
55 	gpio_pin_t	 gpin;
56 	void            *ihandler;
57 	struct resource *ires;
58 	int		 irid;
59 	struct cdev     *pps_cdev;
60 	struct pps_state pps_state;
61 	struct mtx       pps_mtx;
62 	bool		 falling_edge;
63 };
64 
65 #define PPS_CDEV_NAME   "gpiopps"
66 
67 static int
68 gpiopps_open(struct cdev *dev, int flags, int fmt, struct thread *td)
69 {
70 	struct pps_softc *sc = dev->si_drv1;
71 
72 	/* We can't be unloaded while open, so mark ourselves BUSY. */
73 	mtx_lock(&sc->pps_mtx);
74 	device_busy(sc->dev);
75 	mtx_unlock(&sc->pps_mtx);
76 
77 	return 0;
78 }
79 
80 static	int
81 gpiopps_close(struct cdev *dev, int flags, int fmt, struct thread *td)
82 {
83 	struct pps_softc *sc = dev->si_drv1;
84 
85 	mtx_lock(&sc->pps_mtx);
86 	device_unbusy(sc->dev);
87 	mtx_unlock(&sc->pps_mtx);
88 
89 	return 0;
90 }
91 
92 static int
93 gpiopps_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
94 {
95 	struct pps_softc *sc = dev->si_drv1;
96 	int err;
97 
98 	/* Let the kernel do the heavy lifting for ioctl. */
99 	mtx_lock(&sc->pps_mtx);
100 	err = pps_ioctl(cmd, data, &sc->pps_state);
101 	mtx_unlock(&sc->pps_mtx);
102 
103 	return err;
104 }
105 
106 static struct cdevsw pps_cdevsw = {
107 	.d_version =    D_VERSION,
108 	.d_flags =	D_TRACKCLOSE,
109 	.d_open =       gpiopps_open,
110 	.d_close =      gpiopps_close,
111 	.d_ioctl =      gpiopps_ioctl,
112 	.d_name =       PPS_CDEV_NAME,
113 };
114 
115 static int
116 gpiopps_ifltr(void *arg)
117 {
118 	struct pps_softc *sc = arg;
119 
120 	/*
121 	 * There is no locking here by design... The kernel cleverly captures
122 	 * the current time into an area of the pps_state structure which is
123 	 * written only by the pps_capture() routine and read only by the
124 	 * pps_event() routine.  We don't need lock-based management of access
125 	 * to the capture area because we have time-based access management:  we
126 	 * can't be reading and writing concurrently because we can't be running
127 	 * both the threaded and filter handlers concurrently (because a new
128 	 * hardware interrupt can't happen until the threaded handler for the
129 	 * current interrupt exits, after which the system does the EOI that
130 	 * enables a new hardware interrupt).
131 	 */
132 	pps_capture(&sc->pps_state);
133 	return (FILTER_SCHEDULE_THREAD);
134 }
135 
136 static void
137 gpiopps_ithrd(void *arg)
138 {
139 	struct pps_softc *sc = arg;
140 
141 	/*
142 	 * Go create a pps event from the data captured in the filter handler.
143 	 *
144 	 * Note that we DO need locking here, unlike the case with the filter
145 	 * handler.  The pps_event() routine updates the non-capture part of the
146 	 * pps_state structure, and the ioctl() code could be accessing that
147 	 * data right now in a non-interrupt context, so we need an interlock.
148 	 */
149 	mtx_lock(&sc->pps_mtx);
150 	pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
151 	mtx_unlock(&sc->pps_mtx);
152 }
153 
154 static int
155 gpiopps_detach(device_t dev)
156 {
157 	struct pps_softc *sc = device_get_softc(dev);
158 
159 	if (sc->pps_cdev != NULL)
160 		destroy_dev(sc->pps_cdev);
161 	if (sc->ihandler != NULL)
162 		bus_teardown_intr(dev, sc->ires, sc->ihandler);
163 	if (sc->ires != NULL)
164 		bus_release_resource(dev, SYS_RES_IRQ, sc->irid, sc->ires);
165 	if (sc->gpin != NULL)
166 		gpiobus_release_pin(GPIO_GET_BUS(sc->gpin->dev), sc->gpin->pin);
167 	return (0);
168 }
169 
170 #ifdef FDT
171 static int
172 gpiopps_fdt_attach(device_t dev)
173 {
174 	struct pps_softc *sc;
175 	struct make_dev_args devargs;
176 	phandle_t node;
177 	uint32_t edge, pincaps;
178 	int err;
179 
180 	sc = device_get_softc(dev);
181 	sc->dev = dev;
182 
183 	mtx_init(&sc->pps_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
184 
185 	/* Initialize the pps_state struct. */
186 	sc->pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
187 	sc->pps_state.driver_abi = PPS_ABI_VERSION;
188 	sc->pps_state.driver_mtx = &sc->pps_mtx;
189 	pps_init_abi(&sc->pps_state);
190 
191 	/* Check which edge we're configured to capture (default is rising). */
192 	if (ofw_bus_has_prop(dev, "assert-falling-edge"))
193 		edge = GPIO_INTR_EDGE_FALLING;
194 	else
195 		edge = GPIO_INTR_EDGE_RISING;
196 
197 	/*
198 	 * Look up the configured gpio pin and ensure it can be configured for
199 	 * the interrupt mode we need.
200 	 */
201 	node = ofw_bus_get_node(dev);
202 	if ((err = gpio_pin_get_by_ofw_idx(dev, node, 0, &sc->gpin)) != 0) {
203 		device_printf(dev, "Cannot obtain gpio pin\n");
204 		return (err);
205 	}
206 	device_printf(dev, "PPS input on %s pin %u\n",
207 	    device_get_nameunit(sc->gpin->dev), sc->gpin->pin);
208 
209 	if ((err = gpio_pin_getcaps(sc->gpin, &pincaps)) != 0) {
210 		device_printf(dev, "Cannot query capabilities of gpio pin\n");
211 		gpiopps_detach(dev);
212 		return (err);
213 	}
214 	if ((pincaps & edge) == 0) {
215 		device_printf(dev, "Pin cannot be configured for the requested signal edge\n");
216 		gpiopps_detach(dev);
217 		return (ENOTSUP);
218 	}
219 
220 	/*
221 	 * Transform our 'gpios' property into an interrupt resource and set up
222 	 * the interrupt.
223 	 */
224 	if ((sc->ires = gpio_alloc_intr_resource(dev, &sc->irid, RF_ACTIVE,
225 	    sc->gpin, edge)) == NULL) {
226 		device_printf(dev, "Cannot allocate an IRQ for the GPIO\n");
227 		gpiopps_detach(dev);
228 		return (err);
229 	}
230 
231 	err = bus_setup_intr(dev, sc->ires, INTR_TYPE_CLK | INTR_MPSAFE,
232 	    gpiopps_ifltr, gpiopps_ithrd, sc, &sc->ihandler);
233 	if (err != 0) {
234 		device_printf(dev, "Unable to setup pps irq handler\n");
235 		gpiopps_detach(dev);
236 		return (err);
237 	}
238 
239 	/* Create the RFC 2783 pps-api cdev. */
240 	make_dev_args_init(&devargs);
241 	devargs.mda_devsw = &pps_cdevsw;
242 	devargs.mda_uid = UID_ROOT;
243 	devargs.mda_gid = GID_WHEEL;
244 	devargs.mda_mode = 0660;
245 	devargs.mda_si_drv1 = sc;
246 	err = make_dev_s(&devargs, &sc->pps_cdev, PPS_CDEV_NAME "%d",
247 	    device_get_unit(dev));
248 	if (err != 0) {
249 		device_printf(dev, "Unable to create pps cdev\n");
250 		gpiopps_detach(dev);
251 		return (err);
252 	}
253 
254 	return (0);
255 }
256 
257 static int
258 gpiopps_fdt_probe(device_t dev)
259 {
260 
261 	if (!ofw_bus_status_okay(dev))
262 		return (ENXIO);
263 
264 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
265 		device_set_desc(dev, "GPIO PPS");
266 		return (BUS_PROBE_DEFAULT);
267 	}
268 
269 	return (ENXIO);
270 }
271 
272 static device_method_t pps_fdt_methods[] = {
273 	DEVMETHOD(device_probe,		gpiopps_fdt_probe),
274 	DEVMETHOD(device_attach,	gpiopps_fdt_attach),
275 	DEVMETHOD(device_detach,	gpiopps_detach),
276 
277 	DEVMETHOD_END
278 };
279 
280 static driver_t pps_fdt_driver = {
281 	"gpiopps",
282 	pps_fdt_methods,
283 	sizeof(struct pps_softc),
284 };
285 
286 DRIVER_MODULE(gpiopps, simplebus, pps_fdt_driver, 0, 0);
287 
288 #endif /* FDT */
289