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