j720tp.c revision 1.1.14.2 1 1.1.14.2 yamt /* $NetBSD: j720tp.c,v 1.1.14.2 2006/06/21 14:51:44 yamt Exp $ */
2 1.1.14.2 yamt
3 1.1.14.2 yamt /*-
4 1.1.14.2 yamt * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1.14.2 yamt * All rights reserved.
6 1.1.14.2 yamt *
7 1.1.14.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.14.2 yamt * by IWAMOTO Toshihiro and Peter Postma.
9 1.1.14.2 yamt *
10 1.1.14.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.14.2 yamt * modification, are permitted provided that the following conditions
12 1.1.14.2 yamt * are met:
13 1.1.14.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.14.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.14.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.14.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.14.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.14.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.14.2 yamt * must display the following acknowledgement:
20 1.1.14.2 yamt * This product includes software developed by the NetBSD
21 1.1.14.2 yamt * Foundation, Inc. and its contributors.
22 1.1.14.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.14.2 yamt * contributors may be used to endorse or promote products derived
24 1.1.14.2 yamt * from this software without specific prior written permission.
25 1.1.14.2 yamt *
26 1.1.14.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.14.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.14.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.14.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.14.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.14.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.14.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.14.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.14.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.14.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.14.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.14.2 yamt */
38 1.1.14.2 yamt
39 1.1.14.2 yamt /* Jornada 720 touch-panel driver. */
40 1.1.14.2 yamt
41 1.1.14.2 yamt #include <sys/cdefs.h>
42 1.1.14.2 yamt __KERNEL_RCSID(0, "$NetBSD: j720tp.c,v 1.1.14.2 2006/06/21 14:51:44 yamt Exp $");
43 1.1.14.2 yamt
44 1.1.14.2 yamt #include <sys/param.h>
45 1.1.14.2 yamt #include <sys/systm.h>
46 1.1.14.2 yamt #include <sys/device.h>
47 1.1.14.2 yamt #include <sys/kernel.h>
48 1.1.14.2 yamt #include <sys/malloc.h>
49 1.1.14.2 yamt #include <sys/ioctl.h>
50 1.1.14.2 yamt #include <sys/callout.h>
51 1.1.14.2 yamt
52 1.1.14.2 yamt #include <machine/platid.h>
53 1.1.14.2 yamt #include <machine/platid_mask.h>
54 1.1.14.2 yamt
55 1.1.14.2 yamt #include <arm/sa11x0/sa11x0_var.h>
56 1.1.14.2 yamt #include <arm/sa11x0/sa11x0_gpioreg.h>
57 1.1.14.2 yamt #include <arm/sa11x0/sa11x0_ppcreg.h>
58 1.1.14.2 yamt #include <arm/sa11x0/sa11x0_sspreg.h>
59 1.1.14.2 yamt
60 1.1.14.2 yamt #include <dev/wscons/wsconsio.h>
61 1.1.14.2 yamt #include <dev/wscons/wsmousevar.h>
62 1.1.14.2 yamt #include <dev/hpc/hpctpanelvar.h>
63 1.1.14.2 yamt
64 1.1.14.2 yamt #include <hpcarm/dev/j720sspvar.h>
65 1.1.14.2 yamt
66 1.1.14.2 yamt #include "opt_j720tp.h"
67 1.1.14.2 yamt
68 1.1.14.2 yamt #ifdef J720TP_DEBUG
69 1.1.14.2 yamt #define DPRINTF(arg) printf arg
70 1.1.14.2 yamt #else
71 1.1.14.2 yamt #define DPRINTF(arg) /* nothing */
72 1.1.14.2 yamt #endif
73 1.1.14.2 yamt
74 1.1.14.2 yamt struct j720tp_softc {
75 1.1.14.2 yamt struct device sc_dev;
76 1.1.14.2 yamt
77 1.1.14.2 yamt int sc_enabled;
78 1.1.14.2 yamt
79 1.1.14.2 yamt struct callout sc_tpcallout;
80 1.1.14.2 yamt struct j720ssp_softc *sc_ssp;
81 1.1.14.2 yamt
82 1.1.14.2 yamt struct device *sc_wsmousedev;
83 1.1.14.2 yamt
84 1.1.14.2 yamt struct tpcalib_softc sc_tpcalib;
85 1.1.14.2 yamt };
86 1.1.14.2 yamt
87 1.1.14.2 yamt static int j720tp_match(struct device *, struct cfdata *, void *);
88 1.1.14.2 yamt static void j720tp_attach(struct device *, struct device *, void *);
89 1.1.14.2 yamt
90 1.1.14.2 yamt static int j720tp_wsmouse_enable(void *);
91 1.1.14.2 yamt static int j720tp_wsmouse_ioctl(void *, u_long, caddr_t, int,
92 1.1.14.2 yamt struct lwp *);
93 1.1.14.2 yamt static void j720tp_wsmouse_disable(void *);
94 1.1.14.2 yamt
95 1.1.14.2 yamt static void j720tp_enable_intr(struct j720tp_softc *);
96 1.1.14.2 yamt static void j720tp_disable_intr(struct j720tp_softc *);
97 1.1.14.2 yamt static int j720tp_intr(void *);
98 1.1.14.2 yamt static int j720tp_get_rawxy(struct j720tp_softc *, int *, int *);
99 1.1.14.2 yamt static void j720tp_wsmouse_input(struct j720tp_softc *, int, int);
100 1.1.14.2 yamt static void j720tp_wsmouse_callout(void *);
101 1.1.14.2 yamt
102 1.1.14.2 yamt
103 1.1.14.2 yamt const struct wsmouse_accessops j720tp_wsmouse_accessops = {
104 1.1.14.2 yamt j720tp_wsmouse_enable,
105 1.1.14.2 yamt j720tp_wsmouse_ioctl,
106 1.1.14.2 yamt j720tp_wsmouse_disable
107 1.1.14.2 yamt };
108 1.1.14.2 yamt
109 1.1.14.2 yamt static const struct wsmouse_calibcoords j720tp_default_calib = {
110 1.1.14.2 yamt 0, 0, 639, 239,
111 1.1.14.2 yamt 4,
112 1.1.14.2 yamt {{ 988, 80, 0, 0 },
113 1.1.14.2 yamt { 88, 84, 639, 0 },
114 1.1.14.2 yamt { 988, 927, 0, 239 },
115 1.1.14.2 yamt { 88, 940, 639, 239 }}
116 1.1.14.2 yamt };
117 1.1.14.2 yamt
118 1.1.14.2 yamt CFATTACH_DECL(j720tp, sizeof(struct j720tp_softc),
119 1.1.14.2 yamt j720tp_match, j720tp_attach, NULL, NULL);
120 1.1.14.2 yamt
121 1.1.14.2 yamt
122 1.1.14.2 yamt static int
123 1.1.14.2 yamt j720tp_match(struct device *parent, struct cfdata *cf, void *aux)
124 1.1.14.2 yamt {
125 1.1.14.2 yamt
126 1.1.14.2 yamt if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
127 1.1.14.2 yamt return 0;
128 1.1.14.2 yamt if (strcmp(cf->cf_name, "j720tp") != 0)
129 1.1.14.2 yamt return 0;
130 1.1.14.2 yamt
131 1.1.14.2 yamt return 1;
132 1.1.14.2 yamt }
133 1.1.14.2 yamt
134 1.1.14.2 yamt static void
135 1.1.14.2 yamt j720tp_attach(struct device *parent, struct device *self, void *aux)
136 1.1.14.2 yamt {
137 1.1.14.2 yamt struct j720tp_softc *sc = (struct j720tp_softc *)self;
138 1.1.14.2 yamt struct wsmousedev_attach_args wsma;
139 1.1.14.2 yamt
140 1.1.14.2 yamt printf("\n");
141 1.1.14.2 yamt
142 1.1.14.2 yamt sc->sc_ssp = (struct j720ssp_softc *)parent;
143 1.1.14.2 yamt sc->sc_enabled = 0;
144 1.1.14.2 yamt
145 1.1.14.2 yamt /* Touch-panel as a pointing device. */
146 1.1.14.2 yamt wsma.accessops = &j720tp_wsmouse_accessops;
147 1.1.14.2 yamt wsma.accesscookie = sc;
148 1.1.14.2 yamt
149 1.1.14.2 yamt sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &wsma,
150 1.1.14.2 yamt wsmousedevprint);
151 1.1.14.2 yamt
152 1.1.14.2 yamt /* Initialize calibration, set default parameters. */
153 1.1.14.2 yamt tpcalib_init(&sc->sc_tpcalib);
154 1.1.14.2 yamt tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
155 1.1.14.2 yamt __UNCONST(&j720tp_default_calib), 0, 0);
156 1.1.14.2 yamt
157 1.1.14.2 yamt j720tp_wsmouse_disable(sc);
158 1.1.14.2 yamt callout_init(&sc->sc_tpcallout);
159 1.1.14.2 yamt
160 1.1.14.2 yamt /* Setup touch-panel interrupt. */
161 1.1.14.2 yamt sa11x0_intr_establish(0, 9, 1, IPL_TTY, j720tp_intr, sc);
162 1.1.14.2 yamt }
163 1.1.14.2 yamt
164 1.1.14.2 yamt static int
165 1.1.14.2 yamt j720tp_wsmouse_enable(void *self)
166 1.1.14.2 yamt {
167 1.1.14.2 yamt struct j720tp_softc *sc = self;
168 1.1.14.2 yamt int s;
169 1.1.14.2 yamt
170 1.1.14.2 yamt s = spltty();
171 1.1.14.2 yamt
172 1.1.14.2 yamt j720tp_enable_intr(sc);
173 1.1.14.2 yamt
174 1.1.14.2 yamt sc->sc_enabled = 1;
175 1.1.14.2 yamt
176 1.1.14.2 yamt splx(s);
177 1.1.14.2 yamt return 0;
178 1.1.14.2 yamt }
179 1.1.14.2 yamt
180 1.1.14.2 yamt static int
181 1.1.14.2 yamt j720tp_wsmouse_ioctl(void *self, u_long cmd, caddr_t data, int flag,
182 1.1.14.2 yamt struct lwp *l)
183 1.1.14.2 yamt {
184 1.1.14.2 yamt struct j720tp_softc *sc = self;
185 1.1.14.2 yamt
186 1.1.14.2 yamt return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
187 1.1.14.2 yamt }
188 1.1.14.2 yamt
189 1.1.14.2 yamt static void
190 1.1.14.2 yamt j720tp_wsmouse_disable(void *self)
191 1.1.14.2 yamt {
192 1.1.14.2 yamt struct j720tp_softc *sc = self;
193 1.1.14.2 yamt int s;
194 1.1.14.2 yamt
195 1.1.14.2 yamt s = spltty();
196 1.1.14.2 yamt
197 1.1.14.2 yamt j720tp_disable_intr(sc);
198 1.1.14.2 yamt callout_stop(&sc->sc_tpcallout);
199 1.1.14.2 yamt
200 1.1.14.2 yamt sc->sc_enabled = 0;
201 1.1.14.2 yamt
202 1.1.14.2 yamt splx(s);
203 1.1.14.2 yamt }
204 1.1.14.2 yamt
205 1.1.14.2 yamt /*
206 1.1.14.2 yamt * Enable touch-panel interrupt. Must be called at spltty().
207 1.1.14.2 yamt */
208 1.1.14.2 yamt static void
209 1.1.14.2 yamt j720tp_enable_intr(struct j720tp_softc *sc)
210 1.1.14.2 yamt {
211 1.1.14.2 yamt struct j720ssp_softc *ssp = sc->sc_ssp;
212 1.1.14.2 yamt uint32_t er;
213 1.1.14.2 yamt
214 1.1.14.2 yamt er = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
215 1.1.14.2 yamt er |= 1 << 9;
216 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, er);
217 1.1.14.2 yamt }
218 1.1.14.2 yamt
219 1.1.14.2 yamt /*
220 1.1.14.2 yamt * Disable touch-panel interrupt. Must be called at spltty().
221 1.1.14.2 yamt */
222 1.1.14.2 yamt static void
223 1.1.14.2 yamt j720tp_disable_intr(struct j720tp_softc *sc)
224 1.1.14.2 yamt {
225 1.1.14.2 yamt struct j720ssp_softc *ssp = sc->sc_ssp;
226 1.1.14.2 yamt uint32_t er;
227 1.1.14.2 yamt
228 1.1.14.2 yamt er = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
229 1.1.14.2 yamt er &= ~(1 << 9);
230 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, er);
231 1.1.14.2 yamt }
232 1.1.14.2 yamt
233 1.1.14.2 yamt static int
234 1.1.14.2 yamt j720tp_intr(void *arg)
235 1.1.14.2 yamt {
236 1.1.14.2 yamt struct j720tp_softc *sc = arg;
237 1.1.14.2 yamt struct j720ssp_softc *ssp = sc->sc_ssp;
238 1.1.14.2 yamt int rawx, rawy;
239 1.1.14.2 yamt
240 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1 << 9);
241 1.1.14.2 yamt
242 1.1.14.2 yamt if (!sc->sc_enabled) {
243 1.1.14.2 yamt DPRINTF(("j720tp_intr: !sc_enabled\n"));
244 1.1.14.2 yamt return 0;
245 1.1.14.2 yamt }
246 1.1.14.2 yamt
247 1.1.14.2 yamt j720tp_disable_intr(sc);
248 1.1.14.2 yamt
249 1.1.14.2 yamt if (j720tp_get_rawxy(sc, &rawx, &rawy))
250 1.1.14.2 yamt j720tp_wsmouse_input(sc, rawx, rawy);
251 1.1.14.2 yamt
252 1.1.14.2 yamt callout_reset(&sc->sc_tpcallout, hz / 32, j720tp_wsmouse_callout, sc);
253 1.1.14.2 yamt
254 1.1.14.2 yamt return 1;
255 1.1.14.2 yamt }
256 1.1.14.2 yamt
257 1.1.14.2 yamt static int
258 1.1.14.2 yamt j720tp_get_rawxy(struct j720tp_softc *sc, int *rawx, int *rawy)
259 1.1.14.2 yamt {
260 1.1.14.2 yamt struct j720ssp_softc *ssp = sc->sc_ssp;
261 1.1.14.2 yamt int buf[8], data, i;
262 1.1.14.2 yamt
263 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
264 1.1.14.2 yamt
265 1.1.14.2 yamt /* Send read touch-panel command. */
266 1.1.14.2 yamt if (j720ssp_readwrite(ssp, 1, 0x500, &data, 100) < 0 || data != 0x88) {
267 1.1.14.2 yamt DPRINTF(("j720tp_get_rawxy: no dummy received\n"));
268 1.1.14.2 yamt goto out;
269 1.1.14.2 yamt }
270 1.1.14.2 yamt
271 1.1.14.2 yamt for (i = 0; i < 8; i++) {
272 1.1.14.2 yamt if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 100) < 0)
273 1.1.14.2 yamt goto out;
274 1.1.14.2 yamt J720SSP_INVERT(data);
275 1.1.14.2 yamt buf[i] = data;
276 1.1.14.2 yamt }
277 1.1.14.2 yamt
278 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
279 1.1.14.2 yamt
280 1.1.14.2 yamt buf[6] <<= 8;
281 1.1.14.2 yamt buf[7] <<= 8;
282 1.1.14.2 yamt for (i = 0; i < 3; i++) {
283 1.1.14.2 yamt buf[i] |= buf[6] & 0x300;
284 1.1.14.2 yamt buf[6] >>= 2;
285 1.1.14.2 yamt buf[i + 3] |= buf[7] & 0x300;
286 1.1.14.2 yamt buf[7] >>= 2;
287 1.1.14.2 yamt }
288 1.1.14.2 yamt
289 1.1.14.2 yamt #if 0
290 1.1.14.2 yamt DPRINTF(("j720tp_get_rawxy: %d:%d:%d:%d:%d:%d:%d:%d\n", buf[0], buf[1],
291 1.1.14.2 yamt buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
292 1.1.14.2 yamt #endif
293 1.1.14.2 yamt
294 1.1.14.2 yamt *rawx = buf[1];
295 1.1.14.2 yamt *rawy = buf[4];
296 1.1.14.2 yamt
297 1.1.14.2 yamt return 1;
298 1.1.14.2 yamt out:
299 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
300 1.1.14.2 yamt
301 1.1.14.2 yamt /* reset SSP */
302 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
303 1.1.14.2 yamt delay(100);
304 1.1.14.2 yamt bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
305 1.1.14.2 yamt
306 1.1.14.2 yamt DPRINTF(("j720tp_get_rawxy: error %x\n", data));
307 1.1.14.2 yamt return 0;
308 1.1.14.2 yamt }
309 1.1.14.2 yamt
310 1.1.14.2 yamt static void
311 1.1.14.2 yamt j720tp_wsmouse_input(struct j720tp_softc *sc, int rawx, int rawy)
312 1.1.14.2 yamt {
313 1.1.14.2 yamt int x, y;
314 1.1.14.2 yamt
315 1.1.14.2 yamt tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
316 1.1.14.2 yamt wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
317 1.1.14.2 yamt WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
318 1.1.14.2 yamt }
319 1.1.14.2 yamt
320 1.1.14.2 yamt static void
321 1.1.14.2 yamt j720tp_wsmouse_callout(void *arg)
322 1.1.14.2 yamt {
323 1.1.14.2 yamt struct j720tp_softc *sc = arg;
324 1.1.14.2 yamt struct j720ssp_softc *ssp = sc->sc_ssp;
325 1.1.14.2 yamt int rawx, rawy, s;
326 1.1.14.2 yamt
327 1.1.14.2 yamt s = spltty();
328 1.1.14.2 yamt
329 1.1.14.2 yamt if (!sc->sc_enabled) {
330 1.1.14.2 yamt DPRINTF(("j720tp_wsmouse_callout: !sc_enabled\n"));
331 1.1.14.2 yamt splx(s);
332 1.1.14.2 yamt return;
333 1.1.14.2 yamt }
334 1.1.14.2 yamt
335 1.1.14.2 yamt if (bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR) & (1<<9)) {
336 1.1.14.2 yamt wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0);
337 1.1.14.2 yamt j720tp_enable_intr(sc);
338 1.1.14.2 yamt } else {
339 1.1.14.2 yamt if (j720tp_get_rawxy(sc, &rawx, &rawy))
340 1.1.14.2 yamt j720tp_wsmouse_input(sc, rawx, rawy);
341 1.1.14.2 yamt
342 1.1.14.2 yamt callout_schedule(&sc->sc_tpcallout, hz / 32);
343 1.1.14.2 yamt }
344 1.1.14.2 yamt
345 1.1.14.2 yamt splx(s);
346 1.1.14.2 yamt }
347