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: releng/11.0/sys/dev/gpio/gpiopps.c 300811 2016-05-26 23:56:12Z ian $");
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
46static struct ofw_compat_data compat_data[] = {
47	{"pps-gpio", 	1},
48	{NULL,          0}
49};
50#endif /* FDT */
51
52static devclass_t pps_devclass;
53
54struct pps_softc {
55	device_t         dev;
56	gpio_pin_t	 gpin;
57	void            *ihandler;
58	struct resource *ires;
59	int		 irid;
60	struct cdev     *pps_cdev;
61	struct pps_state pps_state;
62	struct mtx       pps_mtx;
63	bool		 falling_edge;
64};
65
66#define PPS_CDEV_NAME   "gpiopps"
67
68static int
69gpiopps_open(struct cdev *dev, int flags, int fmt, struct thread *td)
70{
71	struct pps_softc *sc = dev->si_drv1;
72
73	/* We can't be unloaded while open, so mark ourselves BUSY. */
74	mtx_lock(&sc->pps_mtx);
75	if (device_get_state(sc->dev) < DS_BUSY) {
76		device_busy(sc->dev);
77	}
78	mtx_unlock(&sc->pps_mtx);
79
80	return 0;
81}
82
83static	int
84gpiopps_close(struct cdev *dev, int flags, int fmt, struct thread *td)
85{
86	struct pps_softc *sc = dev->si_drv1;
87
88	/*
89	 * Un-busy on last close. We rely on the vfs counting stuff to only call
90	 * this routine on last-close, so we don't need any open-count logic.
91	 */
92	mtx_lock(&sc->pps_mtx);
93	device_unbusy(sc->dev);
94	mtx_unlock(&sc->pps_mtx);
95
96	return 0;
97}
98
99static int
100gpiopps_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
101{
102	struct pps_softc *sc = dev->si_drv1;
103	int err;
104
105	/* Let the kernel do the heavy lifting for ioctl. */
106	mtx_lock(&sc->pps_mtx);
107	err = pps_ioctl(cmd, data, &sc->pps_state);
108	mtx_unlock(&sc->pps_mtx);
109
110	return err;
111}
112
113static struct cdevsw pps_cdevsw = {
114	.d_version =    D_VERSION,
115	.d_open =       gpiopps_open,
116	.d_close =      gpiopps_close,
117	.d_ioctl =      gpiopps_ioctl,
118	.d_name =       PPS_CDEV_NAME,
119};
120
121static int
122gpiopps_ifltr(void *arg)
123{
124	struct pps_softc *sc = arg;
125
126	/*
127	 * There is no locking here by design... The kernel cleverly captures
128	 * the current time into an area of the pps_state structure which is
129	 * written only by the pps_capture() routine and read only by the
130	 * pps_event() routine.  We don't need lock-based management of access
131	 * to the capture area because we have time-based access management:  we
132	 * can't be reading and writing concurently because we can't be running
133	 * both the threaded and filter handlers concurrently (because a new
134	 * hardware interrupt can't happen until the threaded handler for the
135	 * current interrupt exits, after which the system does the EOI that
136	 * enables a new hardware interrupt).
137	 */
138	pps_capture(&sc->pps_state);
139	return (FILTER_SCHEDULE_THREAD);
140}
141
142static void
143gpiopps_ithrd(void *arg)
144{
145	struct pps_softc *sc = arg;
146
147	/*
148	 * Go create a pps event from the data captured in the filter handler.
149	 *
150	 * Note that we DO need locking here, unlike the case with the filter
151	 * handler.  The pps_event() routine updates the non-capture part of the
152	 * pps_state structure, and the ioctl() code could be accessing that
153	 * data right now in a non-interrupt context, so we need an interlock.
154	 */
155	mtx_lock(&sc->pps_mtx);
156	pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
157	mtx_unlock(&sc->pps_mtx);
158}
159
160static int
161gpiopps_detach(device_t dev)
162{
163	struct pps_softc *sc = device_get_softc(dev);
164
165	if (sc->pps_cdev != NULL)
166		destroy_dev(sc->pps_cdev);
167	if (sc->ihandler != NULL)
168		bus_teardown_intr(dev, sc->ires, sc->ihandler);
169	if (sc->ires != NULL)
170		bus_release_resource(dev, SYS_RES_IRQ, sc->irid, sc->ires);
171	if (sc->gpin != NULL)
172		gpiobus_release_pin(GPIO_GET_BUS(sc->gpin->dev), sc->gpin->pin);
173	return (0);
174}
175
176#ifdef FDT
177static int
178gpiopps_fdt_attach(device_t dev)
179{
180	struct pps_softc *sc;
181	struct make_dev_args devargs;
182	phandle_t node;
183	uint32_t edge, pincaps;
184	int err;
185
186	sc = device_get_softc(dev);
187	sc->dev = dev;
188
189	mtx_init(&sc->pps_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
190
191	/* Initialize the pps_state struct. */
192	sc->pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
193	sc->pps_state.driver_abi = PPS_ABI_VERSION;
194	sc->pps_state.driver_mtx = &sc->pps_mtx;
195	pps_init_abi(&sc->pps_state);
196
197	/* Check which edge we're configured to capture (default is rising). */
198	if (ofw_bus_has_prop(dev, "assert-falling-edge"))
199		edge = GPIO_INTR_EDGE_FALLING;
200	else
201		edge = GPIO_INTR_EDGE_RISING;
202
203	/*
204	 * Look up the configured gpio pin and ensure it can be configured for
205	 * the interrupt mode we need.
206	 */
207	node = ofw_bus_get_node(dev);
208	if ((err = gpio_pin_get_by_ofw_idx(dev, node, 0, &sc->gpin)) != 0) {
209		device_printf(dev, "Cannot obtain gpio pin\n");
210		return (err);
211	}
212	device_printf(dev, "PPS input on %s pin %u\n",
213	    device_get_nameunit(sc->gpin->dev), sc->gpin->pin);
214
215	if ((err = gpio_pin_getcaps(sc->gpin, &pincaps)) != 0) {
216		device_printf(dev, "Cannot query capabilities of gpio pin\n");
217		gpiopps_detach(dev);
218		return (err);
219	}
220	if ((pincaps & edge) == 0) {
221		device_printf(dev, "Pin cannot be configured for the requested signal edge\n");
222		gpiopps_detach(dev);
223		return (ENOTSUP);
224	}
225
226	/*
227	 * Transform our 'gpios' property into an interrupt resource and set up
228	 * the interrupt.
229	 */
230	if ((sc->ires = gpio_alloc_intr_resource(dev, &sc->irid, RF_ACTIVE,
231	    sc->gpin, edge)) == NULL) {
232		device_printf(dev, "Cannot allocate an IRQ for the GPIO\n");
233		gpiopps_detach(dev);
234		return (err);
235	}
236
237	err = bus_setup_intr(dev, sc->ires, INTR_TYPE_CLK | INTR_MPSAFE,
238	    gpiopps_ifltr, gpiopps_ithrd, sc, &sc->ihandler);
239	if (err != 0) {
240		device_printf(dev, "Unable to setup pps irq handler\n");
241		gpiopps_detach(dev);
242		return (err);
243	}
244
245	/* Create the RFC 2783 pps-api cdev. */
246	make_dev_args_init(&devargs);
247	devargs.mda_devsw = &pps_cdevsw;
248	devargs.mda_uid = UID_ROOT;
249	devargs.mda_gid = GID_WHEEL;
250	devargs.mda_mode = 0660;
251	devargs.mda_si_drv1 = sc;
252	err = make_dev_s(&devargs, &sc->pps_cdev, PPS_CDEV_NAME "%d",
253	    device_get_unit(dev));
254	if (err != 0) {
255		device_printf(dev, "Unable to create pps cdev\n");
256		gpiopps_detach(dev);
257		return (err);
258	}
259
260	return (0);
261}
262
263static int
264gpiopps_fdt_probe(device_t dev)
265{
266
267	if (!ofw_bus_status_okay(dev))
268		return (ENXIO);
269
270	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
271		device_set_desc(dev, "GPIO PPS");
272		return (BUS_PROBE_DEFAULT);
273	}
274
275	return (ENXIO);
276}
277
278static device_method_t pps_fdt_methods[] = {
279	DEVMETHOD(device_probe,		gpiopps_fdt_probe),
280	DEVMETHOD(device_attach,	gpiopps_fdt_attach),
281	DEVMETHOD(device_detach,	gpiopps_detach),
282
283	DEVMETHOD_END
284};
285
286static driver_t pps_fdt_driver = {
287	"gpiopps",
288	pps_fdt_methods,
289	sizeof(struct pps_softc),
290};
291
292DRIVER_MODULE(gpiopps, simplebus, pps_fdt_driver, pps_devclass, 0, 0);
293
294#endif /* FDT */
295