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