xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_ft5406.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * Copyright (C) 2016 Oleksandr Tymoshenko <gonzo@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 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/cpu.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/selinfo.h>
41 #include <sys/poll.h>
42 #include <sys/uio.h>
43 #include <sys/conf.h>
44 
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <dev/evdev/input.h>
52 #include <dev/evdev/evdev.h>
53 
54 #include <machine/bus.h>
55 #include <machine/cpu.h>
56 #include <machine/intr.h>
57 
58 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
59 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
60 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
61 
62 #include "mbox_if.h"
63 
64 #ifdef DEBUG
65 #define DPRINTF(fmt, ...) do {			\
66 	printf("%s:%u: ", __func__, __LINE__);	\
67 	printf(fmt, ##__VA_ARGS__);		\
68 } while (0)
69 #else
70 #define DPRINTF(fmt, ...)
71 #endif
72 
73 #define	FT5406_LOCK(_sc)		\
74 	mtx_lock(&(_sc)->sc_mtx)
75 #define	FT5406_UNLOCK(_sc)		\
76 	mtx_unlock(&(_sc)->sc_mtx)
77 #define	FT5406_LOCK_INIT(_sc)	\
78 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
79 	    "ft5406", MTX_DEF)
80 #define	FT5406_LOCK_DESTROY(_sc)	\
81 	mtx_destroy(&_sc->sc_mtx);
82 #define	FT5406_LOCK_ASSERT(_sc)	\
83 	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
84 
85 #define	FT5406_DEVICE_MODE	0
86 #define	FT5406_GESTURE_ID	1
87 #define	FT5406_NUM_POINTS	2
88 #define	FT5406_POINT_XH(n)	(0 + 3 + (n)*6)
89 #define	FT5406_POINT_XL(n)	(1 + 3 + (n)*6)
90 #define	FT5406_POINT_YH(n)	(2 + 3 + (n)*6)
91 #define	FT5406_POINT_YL(n)	(3 + 3 + (n)*6)
92 #define	FT5406_WINDOW_SIZE	64
93 
94 #define	GET_NUM_POINTS(buf)	(buf[FT5406_NUM_POINTS])
95 #define	GET_X(buf, n)		(((buf[FT5406_POINT_XH(n)] & 0xf) << 8) | \
96 				    (buf[FT5406_POINT_XL(n)]))
97 #define	GET_Y(buf, n)		(((buf[FT5406_POINT_YH(n)] & 0xf) << 8) | \
98 				    (buf[FT5406_POINT_YL(n)]))
99 #define	GET_TOUCH_ID(buf, n)	((buf[FT5406_POINT_YH(n)] >> 4) & 0xf)
100 
101 #define	NO_POINTS		99
102 #define	SCREEN_WIDTH		800
103 #define	SCREEN_HEIGHT		480
104 #define	SCREEN_WIDTH_MM		155
105 #define	SCREEN_HEIGHT_MM	86
106 #define	SCREEN_RES_X	(SCREEN_WIDTH / SCREEN_WIDTH_MM)
107 #define	SCREEN_RES_Y	(SCREEN_HEIGHT / SCREEN_HEIGHT_MM)
108 #define	MAX_TOUCH_ID	(10 - 1)
109 
110 struct ft5406ts_softc {
111 	device_t		sc_dev;
112 	struct mtx		sc_mtx;
113 	int			sc_tick;
114 	struct callout		sc_callout;
115 
116 	/* mbox buffer (mapped to KVA) */
117 	uint8_t			*touch_buf;
118 
119 	/* initial hook for waiting mbox intr */
120 	struct intr_config_hook	sc_init_hook;
121 
122 	struct evdev_dev	*sc_evdev;
123 
124 	uint8_t			sc_window[FT5406_WINDOW_SIZE];
125 };
126 
127 static evdev_open_t ft5406ts_ev_open;
128 static evdev_close_t ft5406ts_ev_close;
129 
130 static const struct evdev_methods ft5406ts_evdev_methods = {
131 	.ev_open = &ft5406ts_ev_open,
132 	.ev_close = &ft5406ts_ev_close,
133 };
134 
135 static void
136 ft5406ts_callout(void *data)
137 {
138 	struct ft5406ts_softc *sc = (struct ft5406ts_softc *)data;
139 	int points;
140 	int id, i, x, y;
141 
142 	FT5406_LOCK_ASSERT(sc);
143 
144 	memcpy(sc->sc_window, sc->touch_buf, FT5406_WINDOW_SIZE);
145 	sc->touch_buf[FT5406_NUM_POINTS] = NO_POINTS;
146 
147 	points = GET_NUM_POINTS(sc->sc_window);
148 	/*
149 	 * No update from VC - do nothing.
150 	 */
151 	if (points == NO_POINTS)
152 		goto out;
153 
154 	for (i = 0; i < points; i++) {
155 		id = GET_TOUCH_ID(sc->sc_window, i);
156 		x = GET_X(sc->sc_window, i);
157 		y = GET_Y(sc->sc_window, i);
158 
159 		if (id > MAX_TOUCH_ID) {
160 			device_printf(sc->sc_dev, "bad touch id: %d", id);
161 			continue;
162 		}
163 		evdev_push_event(sc->sc_evdev, EV_ABS, ABS_MT_SLOT, id);
164 		evdev_push_event(sc->sc_evdev, EV_ABS, ABS_MT_TRACKING_ID, id);
165 		evdev_push_event(sc->sc_evdev, EV_ABS, ABS_MT_POSITION_X, x);
166 		evdev_push_event(sc->sc_evdev, EV_ABS, ABS_MT_POSITION_Y, y);
167 	}
168 	evdev_sync(sc->sc_evdev);
169 out:
170 	callout_reset(&sc->sc_callout, sc->sc_tick, ft5406ts_callout, sc);
171 }
172 
173 static int
174 ft5406ts_ev_close(struct evdev_dev *evdev)
175 {
176 	struct ft5406ts_softc *sc = evdev_get_softc(evdev);
177 
178 	FT5406_LOCK_ASSERT(sc);
179 
180 	callout_stop(&sc->sc_callout);
181 
182 	return (0);
183 }
184 
185 static int
186 ft5406ts_ev_open(struct evdev_dev *evdev)
187 {
188 	struct ft5406ts_softc *sc = evdev_get_softc(evdev);
189 
190 	FT5406_LOCK_ASSERT(sc);
191 
192 	callout_reset(&sc->sc_callout, sc->sc_tick, ft5406ts_callout, sc);
193 
194 	return (0);
195 }
196 
197 static void
198 ft5406ts_init(void *arg)
199 {
200 	struct ft5406ts_softc *sc = arg;
201 	struct bcm2835_mbox_tag_touchbuf msg;
202 	uint32_t touchbuf;
203 	int err;
204 
205 	/* release this hook (continue boot) */
206 	config_intrhook_disestablish(&sc->sc_init_hook);
207 
208 	memset(&msg, 0, sizeof(msg));
209 	msg.hdr.buf_size = sizeof(msg);
210 	msg.hdr.code = BCM2835_MBOX_CODE_REQ;
211 	msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_TOUCHBUF;
212 	msg.tag_hdr.val_buf_size = sizeof(msg.body);
213 	msg.tag_hdr.val_len = sizeof(msg.body);
214 	msg.end_tag = 0;
215 
216 	/* call mailbox property */
217 	err = bcm2835_mbox_property(&msg, sizeof(msg));
218 	if (err) {
219 		device_printf(sc->sc_dev, "failed to get touchbuf address\n");
220 		return;
221 	}
222 
223 	if (msg.body.resp.address == 0) {
224 		device_printf(sc->sc_dev, "touchscreen not detected\n");
225 		return;
226 	}
227 
228 	touchbuf = VCBUS_TO_ARMC(msg.body.resp.address);
229 	sc->touch_buf = (uint8_t*)pmap_mapdev(touchbuf, FT5406_WINDOW_SIZE);
230 
231 	/* 60Hz */
232 	sc->sc_tick = hz * 17 / 1000;
233 	if (sc->sc_tick == 0)
234 		sc->sc_tick = 1;
235 
236 	sc->sc_evdev = evdev_alloc();
237 	evdev_set_name(sc->sc_evdev, device_get_desc(sc->sc_dev));
238 	evdev_set_phys(sc->sc_evdev, device_get_nameunit(sc->sc_dev));
239 	evdev_set_id(sc->sc_evdev, BUS_HOST, 0, 0, 0);
240 	evdev_set_methods(sc->sc_evdev, sc, &ft5406ts_evdev_methods);
241 	evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_STCOMPAT);
242 	evdev_set_flag(sc->sc_evdev, EVDEV_FLAG_MT_AUTOREL);
243 	evdev_support_prop(sc->sc_evdev, INPUT_PROP_DIRECT);
244 	evdev_support_event(sc->sc_evdev, EV_SYN);
245 	evdev_support_event(sc->sc_evdev, EV_ABS);
246 
247 	evdev_support_abs(sc->sc_evdev, ABS_MT_SLOT, 0,
248 	    MAX_TOUCH_ID, 0, 0, 0);
249 	evdev_support_abs(sc->sc_evdev, ABS_MT_TRACKING_ID, -1,
250 	    MAX_TOUCH_ID, 0, 0, 0);
251 	evdev_support_abs(sc->sc_evdev, ABS_MT_POSITION_X, 0,
252 	    SCREEN_WIDTH, 0, 0, SCREEN_RES_X);
253 	evdev_support_abs(sc->sc_evdev, ABS_MT_POSITION_Y, 0,
254 	    SCREEN_HEIGHT, 0, 0, SCREEN_RES_Y);
255 
256 	err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
257 	if (err) {
258 		evdev_free(sc->sc_evdev);
259 		sc->sc_evdev = NULL;	/* Avoid double free */
260 		return;
261 	}
262 
263 	sc->touch_buf[FT5406_NUM_POINTS] = NO_POINTS;
264 	callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
265 }
266 
267 static int
268 ft5406ts_probe(device_t dev)
269 {
270 
271 	if (!ofw_bus_is_compatible(dev, "raspberrypi,firmware-ts"))
272 		return (ENXIO);
273 
274 	device_set_desc(dev, "FT5406 touchscreen (VC memory interface)");
275 
276 	return (BUS_PROBE_DEFAULT);
277 }
278 
279 static int
280 ft5406ts_attach(device_t dev)
281 {
282 	struct ft5406ts_softc *sc;
283 
284 	/* set self dev */
285 	sc = device_get_softc(dev);
286 	sc->sc_dev = dev;
287 
288 	/* register callback for using mbox when interrupts are enabled */
289 	sc->sc_init_hook.ich_func = ft5406ts_init;
290 	sc->sc_init_hook.ich_arg = sc;
291 
292 	FT5406_LOCK_INIT(sc);
293 
294 	if (config_intrhook_establish(&sc->sc_init_hook) != 0) {
295 		device_printf(dev, "config_intrhook_establish failed\n");
296 		FT5406_LOCK_DESTROY(sc);
297 		return (ENOMEM);
298 	}
299 
300 	return (0);
301 }
302 
303 static int
304 ft5406ts_detach(device_t dev)
305 {
306 	struct ft5406ts_softc *sc;
307 
308 	sc = device_get_softc(dev);
309 
310 	evdev_free(sc->sc_evdev);
311 
312 	FT5406_LOCK_DESTROY(sc);
313 
314 	return (0);
315 }
316 
317 static device_method_t ft5406ts_methods[] = {
318 	/* Device interface */
319 	DEVMETHOD(device_probe,		ft5406ts_probe),
320 	DEVMETHOD(device_attach,	ft5406ts_attach),
321 	DEVMETHOD(device_detach,	ft5406ts_detach),
322 
323 	DEVMETHOD_END
324 };
325 
326 static driver_t ft5406ts_driver = {
327 	"ft5406ts",
328 	ft5406ts_methods,
329 	sizeof(struct ft5406ts_softc),
330 };
331 
332 DRIVER_MODULE(ft5406ts, simplebus, ft5406ts_driver, 0, 0);
333 MODULE_DEPEND(ft5406ts, evdev, 1, 1, 1);
334