psh3tp.c revision 1.8 1 /* $NetBSD: psh3tp.c,v 1.8 2007/02/22 05:39:44 thorpej Exp $ */
2 /*
3 * Copyright (c) 2005 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39
40 #include "opt_psh3tp.h"
41
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
44 #include <dev/hpc/hpctpanelvar.h>
45
46 #include <machine/platid.h>
47 #include <machine/platid_mask.h>
48
49 #include <machine/intr.h>
50
51 #include <sh3/exception.h>
52 #include <sh3/intcreg.h>
53 #include <sh3/pfcreg.h>
54 #include <sh3/adcreg.h>
55
56 #include <sh3/dev/adcvar.h>
57
58
59 #ifdef PSH3TP_DEBUG
60 volatile int psh3tp_debug = 4;
61 #define DPRINTF_PRINTF printf_nolog
62 #define DPRINTF(arg) if (psh3tp_debug) DPRINTF_PRINTF arg
63 #define DPRINTFN(n, arg) if (psh3tp_debug > (n)) DPRINTF_PRINTF arg
64 #else
65 #define DPRINTF(arg) ((void)0)
66 #define DPRINTFN(n, arg) ((void)0)
67 #endif
68
69
70 /*
71 * PFC bits pertinent to PERSONA HPW-50PA touch-panel
72 */
73 #define PHDR_TP_PEN_UP 0x40
74 #define SCPDR_TP_SCAN_ENABLE 0x20
75 #define SCPDR_TP_SCAN_DISABLE 0x01
76 #define SCPDR_TP_SCAN_X 0x06
77 #define SCPDR_TP_SCAN_Y 0x09
78
79 /*
80 * A/D converter channels to get x/y from
81 */
82 #define ADC_CHANNEL_TP_X 1
83 #define ADC_CHANNEL_TP_Y 0
84
85 /*
86 * Default (read: my device) raw X/Y values for framebuffer edges.
87 */
88 #define PSH3TP_FB_RIGHT 56
89 #define PSH3TP_FB_LEFT 969
90 #define PSH3TP_FB_TOP 848
91 #define PSH3TP_FB_BOTTOM 121
92
93
94 struct psh3tp_softc {
95 struct device sc_dev;
96
97 #define PSH3TP_WSMOUSE_ENABLED 0x01
98 int sc_enabled;
99 struct callout sc_touch_ch;
100 struct device *sc_wsmousedev;
101 struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
102 };
103
104
105 /* config machinery */
106 static int psh3tp_match(struct device *, struct cfdata *, void *);
107 static void psh3tp_attach(struct device *, struct device *, void *);
108
109 /* wsmouse accessops */
110 static int psh3tp_wsmouse_enable(void *);
111 static int psh3tp_wsmouse_ioctl(void *, u_long, caddr_t, int, struct lwp *);
112 static void psh3tp_wsmouse_disable(void *);
113
114 /* internal driver routines */
115 static void psh3tp_enable(struct psh3tp_softc *);
116 static void psh3tp_disable(struct psh3tp_softc *);
117 static int psh3tp_set_enable(struct psh3tp_softc *, int, int);
118 static int psh3tp_intr(void *);
119 static void psh3tp_start_polling(void *);
120 static void psh3tp_stop_polling(struct psh3tp_softc *);
121 static void psh3tp_callout_wsmouse(void *);
122 static void psh3tp_wsmouse_input(struct psh3tp_softc *, int, int);
123 static void psh3tp_get_raw_xy(int *, int *);
124
125
126 const struct wsmouse_accessops psh3tp_accessops = {
127 psh3tp_wsmouse_enable,
128 psh3tp_wsmouse_ioctl,
129 psh3tp_wsmouse_disable
130 };
131
132 static const struct wsmouse_calibcoords psh3tp_default_calib = {
133 0, 0, 639, 239,
134 4,
135 {{ PSH3TP_FB_LEFT, PSH3TP_FB_TOP, 0, 0 },
136 { PSH3TP_FB_RIGHT, PSH3TP_FB_TOP, 639, 0 },
137 { PSH3TP_FB_LEFT, PSH3TP_FB_BOTTOM, 0, 239 },
138 { PSH3TP_FB_RIGHT, PSH3TP_FB_BOTTOM, 639, 239 }}
139 };
140
141
142 CFATTACH_DECL(psh3tp, sizeof(struct psh3tp_softc),
143 psh3tp_match, psh3tp_attach, NULL, NULL);
144
145
146 /* ARGSUSED */
147 static int
148 psh3tp_match(struct device *parent __unused, struct cfdata *cf,
149 void *aux __unused)
150 {
151
152 if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
153 return 0;
154
155 if (strcmp(cf->cf_name, "psh3tp") != 0)
156 return 0;
157
158 return 1;
159 }
160
161
162 /*
163 * Attach the touch panel driver and its wsmouse child.
164 *
165 * Note that we have to use submatch to distinguish between child because
166 * wsmouse_match matches unconditionally.
167 */
168 /* ARGSUSED */
169 static void
170 psh3tp_attach(struct device *parent __unused, struct device *self,
171 void *aux __unused)
172 {
173 struct psh3tp_softc *sc = device_private(self);
174 struct wsmousedev_attach_args wsma;
175
176 aprint_naive("\n");
177 aprint_normal("\n");
178
179 sc->sc_enabled = 0;
180
181 /* touch-panel as a pointing device */
182 wsma.accessops = &psh3tp_accessops;
183 wsma.accesscookie = sc;
184
185 sc->sc_wsmousedev = config_found_ia(
186 self, "wsmousedev", &wsma, wsmousedevprint);
187 if (sc->sc_wsmousedev == NULL)
188 return;
189
190 /* init calibration, set default parameters */
191 tpcalib_init(&sc->sc_tpcalib);
192 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
193 (caddr_t)__UNCONST(&psh3tp_default_calib), 0, 0);
194
195 /* used when in polling mode */
196 callout_init(&sc->sc_touch_ch);
197
198 /* establish interrupt handler, but disable until opened */
199 intc_intr_establish(SH7709_INTEVT2_IRQ2,
200 IST_EDGE, IPL_TTY, psh3tp_intr, sc);
201 intc_intr_disable(SH7709_INTEVT2_IRQ2);
202 }
203
204
205 /*
206 * Enable touch panel: we start in interrupt mode.
207 * Must be called at spltty().
208 */
209 /* ARGSUSED */
210 static void
211 psh3tp_enable(struct psh3tp_softc *sc __unused)
212 {
213
214 DPRINTFN(2, ("%s: enable\n", sc->sc_dev.dv_xname));
215 intc_intr_enable(SH7709_INTEVT2_IRQ2);
216 }
217
218
219 /*
220 * Disable touch panel: disable interrupt, cancel pending callout.
221 * Must be called at spltty().
222 */
223 static void
224 psh3tp_disable(struct psh3tp_softc *sc)
225 {
226
227 DPRINTFN(2, ("%s: disable\n", sc->sc_dev.dv_xname));
228 intc_intr_disable(SH7709_INTEVT2_IRQ2);
229 callout_stop(&sc->sc_touch_ch);
230 }
231
232
233 static int
234 psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child)
235 {
236 int s = spltty();
237
238 if (on) {
239 if (!sc->sc_enabled)
240 psh3tp_enable(sc);
241 sc->sc_enabled |= child;
242 } else {
243 sc->sc_enabled &= ~child;
244 if (!sc->sc_enabled)
245 psh3tp_disable(sc);
246 }
247
248 splx(s);
249 return 0;
250 }
251
252
253 static int
254 psh3tp_wsmouse_enable(void *self)
255 {
256 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
257
258 DPRINTFN(1, ("%s: wsmouse enable\n", sc->sc_dev.dv_xname));
259 return psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED);
260 }
261
262
263 static void
264 psh3tp_wsmouse_disable(void *self)
265 {
266 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
267
268 DPRINTFN(1, ("%s: wsmouse disable\n", sc->sc_dev.dv_xname));
269 psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED);
270 }
271
272
273 static int
274 psh3tp_intr(void *self)
275 {
276 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
277
278 uint8_t irr0;
279 uint8_t phdr, touched;
280 unsigned int steady, tremor_timeout;
281
282 irr0 = _reg_read_1(SH7709_IRR0);
283 if ((irr0 & IRR0_IRQ2) == 0) {
284 #ifdef DIAGNOSTIC
285 printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
286 #endif
287 return 0;
288 }
289
290 if (!sc->sc_enabled) {
291 DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
292 intc_intr_disable(SH7709_INTEVT2_IRQ2);
293 goto served;
294 }
295
296 /*
297 * Number of times the "touched" bit should be read
298 * consecutively.
299 */
300 #define TREMOR_THRESHOLD 0x300
301 steady = 0;
302 tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */
303 touched = true; /* we start with "touched" state */
304
305 do {
306 uint8_t state;
307
308 phdr = _reg_read_1(SH7709_PHDR);
309 state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);
310
311 if (state == touched)
312 ++steady;
313 else {
314 steady = 0;
315 touched = state;
316 }
317
318 if (--tremor_timeout == 0) {
319 DPRINTF(("%s: tremor timeout!\n", sc->sc_dev.dv_xname));
320 goto served;
321 }
322 } while (steady < TREMOR_THRESHOLD);
323
324 if (touched) {
325 intc_intr_disable(SH7709_INTEVT2_IRQ2);
326
327 /*
328 * ADC readings are not stable yet, so schedule
329 * callout instead of accessing ADC from the interrupt
330 * handler only to immediately delay().
331 */
332 callout_reset(&sc->sc_touch_ch,
333 hz/32, psh3tp_start_polling, sc);
334 } else
335 DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
336 served:
337 /* clear the interrupt */
338 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
339
340 return 1;
341 }
342
343
344 /*
345 * Called from the interrupt handler at spltty() upon first touch.
346 * Decide if we are going to report this touch as a mouse click/drag.
347 */
348 static void
349 psh3tp_start_polling(void *self)
350 {
351 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
352 uint8_t phdr;
353 int rawx, rawy;
354
355 phdr = _reg_read_1(SH7709_PHDR);
356 if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
357 DPRINTFN(2, ("%s: start: pen is not down\n",
358 sc->sc_dev.dv_xname));
359 psh3tp_stop_polling(sc);
360 return;
361 }
362
363 psh3tp_get_raw_xy(&rawx, &rawy);
364 DPRINTFN(2, ("%s: start: %4d %4d -> ",
365 sc->sc_dev.dv_xname, rawx, rawy));
366
367 if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) {
368 DPRINTFN(2, ("mouse\n"));
369 psh3tp_wsmouse_input(sc, rawx, rawy);
370 callout_reset(&sc->sc_touch_ch,
371 hz/32, psh3tp_callout_wsmouse, sc);
372 } else {
373 DPRINTFN(2, ("ignore\n"));
374 psh3tp_stop_polling(sc);
375 }
376 }
377
378
379 /*
380 * Re-enable touch panel interrupt.
381 * Called at spltty() when polling code detects pen-up.
382 */
383 /* ARGSUSED */
384 static void
385 psh3tp_stop_polling(struct psh3tp_softc *sc __unused)
386 {
387 uint8_t irr0;
388
389 DPRINTFN(2, ("%s: stop\n", sc->sc_dev.dv_xname));
390
391 /* clear pending interrupt signal before re-enabling the interrupt */
392 irr0 = _reg_read_1(SH7709_IRR0);
393 if ((irr0 & IRR0_IRQ2) != 0)
394 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
395
396 intc_intr_enable(SH7709_INTEVT2_IRQ2);
397 }
398
399
400 /*
401 * We are reporting this touch as a mouse click/drag.
402 */
403 static void
404 psh3tp_callout_wsmouse(void *self)
405 {
406 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
407 uint8_t phdr;
408 int rawx, rawy;
409 int s;
410
411 s = spltty();
412
413 if (!sc->sc_enabled) {
414 DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
415 sc->sc_dev.dv_xname));
416 splx(s);
417 return;
418 }
419
420 phdr = _reg_read_1(SH7709_PHDR);
421 if ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP) {
422 psh3tp_get_raw_xy(&rawx, &rawy);
423 psh3tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
424 callout_schedule(&sc->sc_touch_ch, hz/32);
425 } else {
426 wsmouse_input( /* button up */
427 sc->sc_wsmousedev, 0, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
428 psh3tp_stop_polling(sc);
429 }
430 splx(s);
431 }
432
433
434 /*
435 * Report mouse click/drag.
436 */
437 static void
438 psh3tp_wsmouse_input(struct psh3tp_softc *sc, int rawx, int rawy)
439 {
440 int x, y;
441
442 tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
443
444 DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
445 sc->sc_dev.dv_xname, rawx, rawy, x, y));
446
447 wsmouse_input(sc->sc_wsmousedev,
448 1, /* button */
449 x, y, 0, 0,
450 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
451 }
452
453
454 /*
455 * Read raw X/Y coordinates from the ADC.
456 */
457 static void
458 psh3tp_get_raw_xy(int *rawxp, int *rawyp)
459 {
460 uint8_t scpdr;
461
462 /* X axis */
463 scpdr = _reg_read_1(SH7709_SCPDR);
464 scpdr &= ~SCPDR_TP_SCAN_DISABLE;
465 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X);
466 _reg_write_1(SH7709_SCPDR, scpdr);
467 delay(40);
468
469 *rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
470
471 /* Y axis */
472 scpdr = _reg_read_1(SH7709_SCPDR);
473 scpdr &= ~SCPDR_TP_SCAN_X;
474 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
475 _reg_write_1(SH7709_SCPDR, scpdr);
476 delay(40);
477
478 *rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
479
480 /* restore SCPDR */
481 scpdr = _reg_read_1(SH7709_SCPDR);
482 scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
483 scpdr |= SCPDR_TP_SCAN_DISABLE;
484 _reg_write_1(SH7709_SCPDR, scpdr);
485 }
486
487
488 static int
489 psh3tp_wsmouse_ioctl(void *self, u_long cmd, caddr_t data, int flag,
490 struct lwp *l)
491 {
492 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
493
494 return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
495 }
496