j6x0tp.c revision 1.1 1 1.1 uwe /* $NetBSD: j6x0tp.c,v 1.1 2003/10/19 02:20:25 uwe Exp $ */
2 1.1 uwe
3 1.1 uwe /*
4 1.1 uwe * Copyright (c) 2003 Valeriy E. Ushakov
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 uwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 uwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 uwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 uwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 uwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 uwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 uwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 uwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 uwe */
29 1.1 uwe
30 1.1 uwe #include <sys/cdefs.h>
31 1.1 uwe __KERNEL_RCSID(0, "$NetBSD: j6x0tp.c,v 1.1 2003/10/19 02:20:25 uwe Exp $");
32 1.1 uwe
33 1.1 uwe #include <sys/param.h>
34 1.1 uwe #include <sys/kernel.h>
35 1.1 uwe #include <sys/device.h>
36 1.1 uwe #include <sys/malloc.h>
37 1.1 uwe #include <sys/systm.h>
38 1.1 uwe #include <sys/callout.h>
39 1.1 uwe #ifdef GPROF
40 1.1 uwe #include <sys/gmon.h>
41 1.1 uwe #endif
42 1.1 uwe
43 1.1 uwe #include <dev/wscons/wsconsio.h>
44 1.1 uwe #include <dev/wscons/wsmousevar.h>
45 1.1 uwe #include <dev/hpc/tpcalibvar.h>
46 1.1 uwe
47 1.1 uwe #include <machine/platid.h>
48 1.1 uwe #include <machine/platid_mask.h>
49 1.1 uwe
50 1.1 uwe #include <machine/intr.h>
51 1.1 uwe
52 1.1 uwe #include <sh3/exception.h>
53 1.1 uwe #include <sh3/intcreg.h>
54 1.1 uwe #include <sh3/pfcreg.h>
55 1.1 uwe #include <sh3/adcreg.h>
56 1.1 uwe
57 1.1 uwe #define J6X0TP_DEBUG
58 1.1 uwe #if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
59 1.1 uwe #define DPRINTF_ENABLE
60 1.1 uwe #define DPRINTF_DEBUG j6x0tp_debug
61 1.1 uwe #define DPRINTF_LEVEL 0
62 1.1 uwe #include <machine/debug.h>
63 1.1 uwe #else
64 1.1 uwe #ifdef J6X0TP_DEBUG
65 1.1 uwe volatile int j6x0tp_debug = 0;
66 1.1 uwe #define DPRINTF(arg) if (j6x0tp_debug) printf_nolog arg
67 1.1 uwe #define DPRINTFN(n, arg) if (j6x0tp_debug > (n)) printf_nolog arg
68 1.1 uwe #else
69 1.1 uwe #define DPRINTF(arg) ((void)0)
70 1.1 uwe #define DPRINTFN(n, arg) ((void)0)
71 1.1 uwe #endif
72 1.1 uwe #endif
73 1.1 uwe
74 1.1 uwe
75 1.1 uwe /* PFC bits pertinent to Jornada 6x0 touchpanel */
76 1.1 uwe #define PHDR_TP_PEN_DOWN 0x08
77 1.1 uwe
78 1.1 uwe #define SCPDR_TP_SCAN_ENABLE 0x20
79 1.1 uwe #define SCPDR_TP_SCAN_Y 0x02
80 1.1 uwe #define SCPDR_TP_SCAN_X 0x01
81 1.1 uwe
82 1.1 uwe /* A/D covnerter channels to get x/y from */
83 1.1 uwe #define ADC_CHANNEL_TP_Y 1
84 1.1 uwe #define ADC_CHANNEL_TP_X 2
85 1.1 uwe
86 1.1 uwe extern int adc_sample_channel(int); /* XXX: adcvar.h */
87 1.1 uwe
88 1.1 uwe
89 1.1 uwe struct j6x0tp_softc {
90 1.1 uwe struct device sc_dev;
91 1.1 uwe
92 1.1 uwe struct callout sc_touch_ch;
93 1.1 uwe int sc_intrlevel;
94 1.1 uwe int sc_enabled;
95 1.1 uwe
96 1.1 uwe struct device *sc_wsmousedev;
97 1.1 uwe struct tpcalib_softc sc_tpcalib;
98 1.1 uwe };
99 1.1 uwe
100 1.1 uwe static int j6x0tp_match(struct device *, struct cfdata *, void *);
101 1.1 uwe static void j6x0tp_attach(struct device *, struct device *, void *);
102 1.1 uwe
103 1.1 uwe CFATTACH_DECL(j6x0tp, sizeof(struct j6x0tp_softc),
104 1.1 uwe j6x0tp_match, j6x0tp_attach, NULL, NULL);
105 1.1 uwe
106 1.1 uwe
107 1.1 uwe static int j6x0tp_enable(void *);
108 1.1 uwe static int j6x0tp_ioctl(void *, u_long, caddr_t, int, struct proc *);
109 1.1 uwe static void j6x0tp_disable(void *);
110 1.1 uwe
111 1.1 uwe const struct wsmouse_accessops j6x0tp_accessops = {
112 1.1 uwe j6x0tp_enable,
113 1.1 uwe j6x0tp_ioctl,
114 1.1 uwe j6x0tp_disable,
115 1.1 uwe };
116 1.1 uwe
117 1.1 uwe static const struct wsmouse_calibcoords j6x0tp_default_calib = {
118 1.1 uwe 0, 0, 639, 239,
119 1.1 uwe 4,
120 1.1 uwe {{ 38, 80, 0, 0 }, /* upper left */
121 1.1 uwe { 950, 80, 639, 0 }, /* upper right */
122 1.1 uwe { 38, 900, 0, 239 }, /* lower left */
123 1.1 uwe { 950, 900, 639, 239 }} /* lower right */
124 1.1 uwe };
125 1.1 uwe
126 1.1 uwe
127 1.1 uwe static int j6x0tp_intr(void *);
128 1.1 uwe static void j6x0tp_poll_callout(void *self);
129 1.1 uwe
130 1.1 uwe
131 1.1 uwe static int
132 1.1 uwe j6x0tp_match(struct device *parent, struct cfdata *cfp, void *aux)
133 1.1 uwe {
134 1.1 uwe
135 1.1 uwe /*
136 1.1 uwe * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
137 1.1 uwe * Is 620 wired similarly?
138 1.1 uwe */
139 1.1 uwe if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
140 1.1 uwe return (0);
141 1.1 uwe
142 1.1 uwe if (strcmp(cfp->cf_name, "j6x0tp") != 0)
143 1.1 uwe return (0);
144 1.1 uwe
145 1.1 uwe return (1);
146 1.1 uwe }
147 1.1 uwe
148 1.1 uwe
149 1.1 uwe static void
150 1.1 uwe j6x0tp_attach(struct device *parent, struct device *self, void *aux)
151 1.1 uwe {
152 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
153 1.1 uwe struct wsmousedev_attach_args wsma;
154 1.1 uwe
155 1.1 uwe printf("\n");
156 1.1 uwe
157 1.1 uwe sc->sc_enabled = 0;
158 1.1 uwe
159 1.1 uwe wsma.accessops = &j6x0tp_accessops;
160 1.1 uwe wsma.accesscookie = sc;
161 1.1 uwe
162 1.1 uwe sc->sc_wsmousedev = config_found(self, &wsma, wsmousedevprint);
163 1.1 uwe if (sc->sc_wsmousedev == NULL)
164 1.1 uwe return;
165 1.1 uwe
166 1.1 uwe tpcalib_init(&sc->sc_tpcalib);
167 1.1 uwe tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
168 1.1 uwe (caddr_t)&j6x0tp_default_calib, 0, 0);
169 1.1 uwe
170 1.1 uwe callout_init(&sc->sc_touch_ch);
171 1.1 uwe
172 1.1 uwe intc_intr_establish(SH7709_INTEVT2_IRQ3, IST_EDGE, IPL_TTY,
173 1.1 uwe j6x0tp_intr, sc);
174 1.1 uwe sc->sc_intrlevel = intc_intr_disable(SH7709_INTEVT2_IRQ3);
175 1.1 uwe }
176 1.1 uwe
177 1.1 uwe
178 1.1 uwe static int
179 1.1 uwe j6x0tp_enable(void *self)
180 1.1 uwe {
181 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
182 1.1 uwe int s;
183 1.1 uwe
184 1.1 uwe DPRINTFN(1, ("%s: enable\n", sc->sc_dev.dv_xname));
185 1.1 uwe
186 1.1 uwe s = spltty();
187 1.1 uwe
188 1.1 uwe sc->sc_enabled = 1;
189 1.1 uwe intc_intr_enable(SH7709_INTEVT2_IRQ3, sc->sc_intrlevel);
190 1.1 uwe
191 1.1 uwe splx(s);
192 1.1 uwe return (0);
193 1.1 uwe }
194 1.1 uwe
195 1.1 uwe
196 1.1 uwe static void
197 1.1 uwe j6x0tp_disable(void *self)
198 1.1 uwe {
199 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
200 1.1 uwe int s;
201 1.1 uwe
202 1.1 uwe s = spltty();
203 1.1 uwe
204 1.1 uwe DPRINTFN(1, ("%s: disable\n", sc->sc_dev.dv_xname));
205 1.1 uwe
206 1.1 uwe sc->sc_enabled = 0;
207 1.1 uwe intc_intr_disable(SH7709_INTEVT2_IRQ3);
208 1.1 uwe callout_stop(&sc->sc_touch_ch);
209 1.1 uwe
210 1.1 uwe splx(s);
211 1.1 uwe }
212 1.1 uwe
213 1.1 uwe
214 1.1 uwe static int
215 1.1 uwe j6x0tp_intr(void *self)
216 1.1 uwe {
217 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
218 1.1 uwe
219 1.1 uwe uint8_t irr0;
220 1.1 uwe uint8_t phdr, touched;
221 1.1 uwe unsigned int steady, tremor_timeout;
222 1.1 uwe
223 1.1 uwe irr0 = _reg_read_1(SH7709_IRR0);
224 1.1 uwe if ((irr0 & IRR0_IRQ3) == 0) {
225 1.1 uwe #ifdef DIAGNOSTIC
226 1.1 uwe printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
227 1.1 uwe #endif
228 1.1 uwe return (0);
229 1.1 uwe }
230 1.1 uwe
231 1.1 uwe /*
232 1.1 uwe * Number of times the "touched" bit should be read
233 1.1 uwe * consecutively.
234 1.1 uwe */
235 1.1 uwe # define TREMOR_THRESHOLD 0x300
236 1.1 uwe
237 1.1 uwe steady = 0;
238 1.1 uwe tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */
239 1.1 uwe touched = PHDR_TP_PEN_DOWN; /* we start with "touched" state */
240 1.1 uwe
241 1.1 uwe do {
242 1.1 uwe phdr = _reg_read_1(SH7709_PHDR);
243 1.1 uwe
244 1.1 uwe if ((phdr & PHDR_TP_PEN_DOWN) == touched)
245 1.1 uwe ++steady;
246 1.1 uwe else {
247 1.1 uwe steady = 0;
248 1.1 uwe touched = phdr & PHDR_TP_PEN_DOWN;
249 1.1 uwe }
250 1.1 uwe
251 1.1 uwe if (--tremor_timeout == 0) {
252 1.1 uwe DPRINTF(("%s: tremor timeout!\n",
253 1.1 uwe sc->sc_dev.dv_xname));
254 1.1 uwe goto served;
255 1.1 uwe }
256 1.1 uwe } while (steady < TREMOR_THRESHOLD);
257 1.1 uwe
258 1.1 uwe if (touched == 0) {
259 1.1 uwe DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
260 1.1 uwe goto served;
261 1.1 uwe }
262 1.1 uwe
263 1.1 uwe intc_intr_disable(SH7709_INTEVT2_IRQ3);
264 1.1 uwe
265 1.1 uwe if (sc->sc_enabled)
266 1.1 uwe callout_reset(&sc->sc_touch_ch, hz/32,
267 1.1 uwe j6x0tp_poll_callout, sc);
268 1.1 uwe else
269 1.1 uwe DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
270 1.1 uwe
271 1.1 uwe served:
272 1.1 uwe /* clear the interrupt */
273 1.1 uwe _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
274 1.1 uwe
275 1.1 uwe return (1);
276 1.1 uwe }
277 1.1 uwe
278 1.1 uwe
279 1.1 uwe static void
280 1.1 uwe j6x0tp_poll_callout(void *self)
281 1.1 uwe {
282 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
283 1.1 uwe uint8_t phdr, scpdr;
284 1.1 uwe uint8_t irr0;
285 1.1 uwe int rawx, rawy, x, y;
286 1.1 uwe int s;
287 1.1 uwe
288 1.1 uwe s = spltty();
289 1.1 uwe
290 1.1 uwe if (!sc->sc_enabled) {
291 1.1 uwe DPRINTFN(1, ("%s: callout: !sc_enabled\n",
292 1.1 uwe sc->sc_dev.dv_xname));
293 1.1 uwe splx(s);
294 1.1 uwe return;
295 1.1 uwe }
296 1.1 uwe
297 1.1 uwe phdr = _reg_read_1(SH7709_PHDR);
298 1.1 uwe if ((phdr & PHDR_TP_PEN_DOWN) == 0) {
299 1.1 uwe wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0); /* mouse up */
300 1.1 uwe
301 1.1 uwe intc_intr_enable(SH7709_INTEVT2_IRQ3, sc->sc_intrlevel);
302 1.1 uwe
303 1.1 uwe irr0 = _reg_read_1(SH7709_IRR0);
304 1.1 uwe if ((irr0 & IRR0_IRQ3) != 0)
305 1.1 uwe _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
306 1.1 uwe
307 1.1 uwe splx(s);
308 1.1 uwe return;
309 1.1 uwe }
310 1.1 uwe
311 1.1 uwe /* XXX: protect accesses to SCPDR? */
312 1.1 uwe
313 1.1 uwe /* Y axis */
314 1.1 uwe scpdr = _reg_read_1(SH7709_SCPDR);
315 1.1 uwe scpdr |= SCPDR_TP_SCAN_ENABLE;
316 1.1 uwe scpdr &= ~SCPDR_TP_SCAN_Y; /* pull low to scan */
317 1.1 uwe _reg_write_1(SH7709_SCPDR, scpdr);
318 1.1 uwe delay(10);
319 1.1 uwe rawy = adc_sample_channel(ADC_CHANNEL_TP_Y);
320 1.1 uwe
321 1.1 uwe /* X axis */
322 1.1 uwe scpdr = _reg_read_1(SH7709_SCPDR);
323 1.1 uwe scpdr |= SCPDR_TP_SCAN_Y;
324 1.1 uwe scpdr &= ~SCPDR_TP_SCAN_X; /* pull low to scan */
325 1.1 uwe _reg_write_1(SH7709_SCPDR, scpdr);
326 1.1 uwe delay(10);
327 1.1 uwe rawx = adc_sample_channel(ADC_CHANNEL_TP_X);
328 1.1 uwe
329 1.1 uwe /* restore SCPDR */
330 1.1 uwe scpdr = _reg_read_1(SH7709_SCPDR);
331 1.1 uwe scpdr |= SCPDR_TP_SCAN_X;
332 1.1 uwe scpdr &= ~SCPDR_TP_SCAN_ENABLE;
333 1.1 uwe _reg_write_1(SH7709_SCPDR, scpdr);
334 1.1 uwe
335 1.1 uwe tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
336 1.1 uwe
337 1.1 uwe DPRINTFN(2, ("%s: %4d %4d -> %3d %3d\n",
338 1.1 uwe sc->sc_dev.dv_xname, rawx, rawy, x, y));
339 1.1 uwe
340 1.1 uwe wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
341 1.1 uwe WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
342 1.1 uwe
343 1.1 uwe callout_schedule(&sc->sc_touch_ch, hz/32);
344 1.1 uwe splx(s);
345 1.1 uwe }
346 1.1 uwe
347 1.1 uwe
348 1.1 uwe static int
349 1.1 uwe j6x0tp_ioctl(void *self, u_long cmd, caddr_t data, int flag,
350 1.1 uwe struct proc *p)
351 1.1 uwe {
352 1.1 uwe struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
353 1.1 uwe
354 1.1 uwe switch (cmd) {
355 1.1 uwe case WSMOUSEIO_GTYPE:
356 1.1 uwe *(u_int *)data = WSMOUSE_TYPE_TPANEL;
357 1.1 uwe return (0);
358 1.1 uwe
359 1.1 uwe case WSMOUSEIO_SCALIBCOORDS:
360 1.1 uwe case WSMOUSEIO_GCALIBCOORDS:
361 1.1 uwe case WSMOUSEIO_GETID:
362 1.1 uwe return (tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p));
363 1.1 uwe
364 1.1 uwe default:
365 1.1 uwe return (EPASSTHROUGH);
366 1.1 uwe }
367 1.1 uwe }
368