psh3tp.c revision 1.14 1 /* $NetBSD: psh3tp.c,v 1.14 2010/07/24 15:33:12 tsutsui 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 device_t sc_dev;
96
97 #define PSH3TP_WSMOUSE_ENABLED 0x01
98 int sc_enabled;
99 struct callout sc_touch_ch;
100 device_t sc_wsmousedev;
101 struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
102 };
103
104
105 /* config machinery */
106 static int psh3tp_match(device_t, struct cfdata *, void *);
107 static void psh3tp_attach(device_t, device_t, void *);
108
109 /* wsmouse accessops */
110 static int psh3tp_wsmouse_enable(void *);
111 static int psh3tp_wsmouse_ioctl(void *, u_long, void *, 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_NEW(psh3tp, sizeof(struct psh3tp_softc),
143 psh3tp_match, psh3tp_attach, NULL, NULL);
144
145
146 /* ARGSUSED */
147 static int
148 psh3tp_match(device_t parent __unused, struct cfdata *cf, void *aux __unused)
149 {
150
151 if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
152 return 0;
153
154 if (strcmp(cf->cf_name, "psh3tp") != 0)
155 return 0;
156
157 return 1;
158 }
159
160
161 /*
162 * Attach the touch panel driver and its wsmouse child.
163 *
164 * Note that we have to use submatch to distinguish between child because
165 * wsmouse_match matches unconditionally.
166 */
167 /* ARGSUSED */
168 static void
169 psh3tp_attach(device_t parent __unused, device_t self, void *aux __unused)
170 {
171 struct psh3tp_softc *sc = device_private(self);
172 struct wsmousedev_attach_args wsma;
173
174 aprint_naive("\n");
175 aprint_normal("\n");
176
177 sc->sc_dev = self;
178 sc->sc_enabled = 0;
179
180 /* touch-panel as a pointing device */
181 wsma.accessops = &psh3tp_accessops;
182 wsma.accesscookie = sc;
183
184 sc->sc_wsmousedev = config_found_ia(
185 self, "wsmousedev", &wsma, wsmousedevprint);
186 if (sc->sc_wsmousedev == NULL)
187 return;
188
189 /* init calibration, set default parameters */
190 tpcalib_init(&sc->sc_tpcalib);
191 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
192 (void *)__UNCONST(&psh3tp_default_calib), 0, 0);
193
194 /* used when in polling mode */
195 callout_init(&sc->sc_touch_ch, 0);
196
197 /* establish interrupt handler, but disable until opened */
198 intc_intr_establish(SH7709_INTEVT2_IRQ2,
199 IST_EDGE, IPL_TTY, psh3tp_intr, sc);
200 intc_intr_disable(SH7709_INTEVT2_IRQ2);
201
202 if (!pmf_device_register(self, NULL, NULL))
203 aprint_error_dev(self, "unable to establish power handler\n");
204 }
205
206
207 /*
208 * Enable touch panel: we start in interrupt mode.
209 * Must be called at spltty().
210 */
211 /* ARGSUSED */
212 static void
213 psh3tp_enable(struct psh3tp_softc *sc __unused)
214 {
215
216 DPRINTFN(2, ("%s: enable\n", sc->sc_dev.dv_xname));
217 intc_intr_enable(SH7709_INTEVT2_IRQ2);
218 }
219
220
221 /*
222 * Disable touch panel: disable interrupt, cancel pending callout.
223 * Must be called at spltty().
224 */
225 static void
226 psh3tp_disable(struct psh3tp_softc *sc)
227 {
228
229 DPRINTFN(2, ("%s: disable\n", sc->sc_dev.dv_xname));
230 intc_intr_disable(SH7709_INTEVT2_IRQ2);
231 callout_stop(&sc->sc_touch_ch);
232 }
233
234
235 static int
236 psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child)
237 {
238 int s = spltty();
239
240 if (on) {
241 if (!sc->sc_enabled)
242 psh3tp_enable(sc);
243 sc->sc_enabled |= child;
244 } else {
245 sc->sc_enabled &= ~child;
246 if (!sc->sc_enabled)
247 psh3tp_disable(sc);
248 }
249
250 splx(s);
251 return 0;
252 }
253
254
255 static int
256 psh3tp_wsmouse_enable(void *cookie)
257 {
258 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;
259
260 DPRINTFN(1, ("%s: wsmouse enable\n", sc->sc_dev.dv_xname));
261 return psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED);
262 }
263
264
265 static void
266 psh3tp_wsmouse_disable(void *cookie)
267 {
268 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;
269
270 DPRINTFN(1, ("%s: wsmouse disable\n", sc->sc_dev.dv_xname));
271 psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED);
272 }
273
274
275 static int
276 psh3tp_intr(void *arg)
277 {
278 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
279
280 uint8_t irr0;
281 uint8_t phdr, touched;
282 unsigned int steady, tremor_timeout;
283
284 irr0 = _reg_read_1(SH7709_IRR0);
285 if ((irr0 & IRR0_IRQ2) == 0) {
286 #ifdef DIAGNOSTIC
287 printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
288 #endif
289 return 0;
290 }
291
292 if (!sc->sc_enabled) {
293 DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
294 intc_intr_disable(SH7709_INTEVT2_IRQ2);
295 goto served;
296 }
297
298 /*
299 * Number of times the "touched" bit should be read
300 * consecutively.
301 */
302 #define TREMOR_THRESHOLD 0x300
303 steady = 0;
304 tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */
305 touched = true; /* we start with "touched" state */
306
307 do {
308 uint8_t state;
309
310 phdr = _reg_read_1(SH7709_PHDR);
311 state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);
312
313 if (state == touched)
314 ++steady;
315 else {
316 steady = 0;
317 touched = state;
318 }
319
320 if (--tremor_timeout == 0) {
321 DPRINTF(("%s: tremor timeout!\n", sc->sc_dev.dv_xname));
322 goto served;
323 }
324 } while (steady < TREMOR_THRESHOLD);
325
326 if (touched) {
327 intc_intr_disable(SH7709_INTEVT2_IRQ2);
328
329 /*
330 * ADC readings are not stable yet, so schedule
331 * callout instead of accessing ADC from the interrupt
332 * handler only to immediately delay().
333 */
334 callout_reset(&sc->sc_touch_ch,
335 hz/32, psh3tp_start_polling, sc);
336 } else
337 DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
338 served:
339 /* clear the interrupt */
340 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
341
342 return 1;
343 }
344
345
346 /*
347 * Called from the interrupt handler at spltty() upon first touch.
348 * Decide if we are going to report this touch as a mouse click/drag.
349 */
350 static void
351 psh3tp_start_polling(void *arg)
352 {
353 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
354 uint8_t phdr;
355 int rawx, rawy;
356
357 phdr = _reg_read_1(SH7709_PHDR);
358 if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
359 DPRINTFN(2, ("%s: start: pen is not down\n",
360 sc->sc_dev.dv_xname));
361 psh3tp_stop_polling(sc);
362 return;
363 }
364
365 psh3tp_get_raw_xy(&rawx, &rawy);
366 DPRINTFN(2, ("%s: start: %4d %4d -> ",
367 sc->sc_dev.dv_xname, rawx, rawy));
368
369 if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) {
370 DPRINTFN(2, ("mouse\n"));
371 psh3tp_wsmouse_input(sc, rawx, rawy);
372 callout_reset(&sc->sc_touch_ch,
373 hz/32, psh3tp_callout_wsmouse, sc);
374 } else {
375 DPRINTFN(2, ("ignore\n"));
376 psh3tp_stop_polling(sc);
377 }
378 }
379
380
381 /*
382 * Re-enable touch panel interrupt.
383 * Called at spltty() when polling code detects pen-up.
384 */
385 /* ARGSUSED */
386 static void
387 psh3tp_stop_polling(struct psh3tp_softc *sc __unused)
388 {
389 uint8_t irr0;
390
391 DPRINTFN(2, ("%s: stop\n", sc->sc_dev.dv_xname));
392
393 /* clear pending interrupt signal before re-enabling the interrupt */
394 irr0 = _reg_read_1(SH7709_IRR0);
395 if ((irr0 & IRR0_IRQ2) != 0)
396 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
397
398 intc_intr_enable(SH7709_INTEVT2_IRQ2);
399 }
400
401
402 /*
403 * We are reporting this touch as a mouse click/drag.
404 */
405 static void
406 psh3tp_callout_wsmouse(void *arg)
407 {
408 struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
409 uint8_t phdr;
410 int rawx, rawy;
411 int s;
412
413 s = spltty();
414
415 if (!sc->sc_enabled) {
416 DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
417 sc->sc_dev.dv_xname));
418 splx(s);
419 return;
420 }
421
422 phdr = _reg_read_1(SH7709_PHDR);
423 if ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP) {
424 psh3tp_get_raw_xy(&rawx, &rawy);
425 psh3tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
426 callout_schedule(&sc->sc_touch_ch, hz/32);
427 } else {
428 wsmouse_input( /* button up */
429 sc->sc_wsmousedev, 0, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
430 psh3tp_stop_polling(sc);
431 }
432 splx(s);
433 }
434
435
436 /*
437 * Report mouse click/drag.
438 */
439 static void
440 psh3tp_wsmouse_input(struct psh3tp_softc *sc, int rawx, int rawy)
441 {
442 int x, y;
443
444 tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
445
446 DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
447 sc->sc_dev.dv_xname, rawx, rawy, x, y));
448
449 wsmouse_input(sc->sc_wsmousedev,
450 1, /* button */
451 x, y, 0, 0,
452 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
453 }
454
455
456 /*
457 * Read raw X/Y coordinates from the ADC.
458 */
459 static void
460 psh3tp_get_raw_xy(int *rawxp, int *rawyp)
461 {
462 uint8_t scpdr;
463
464 /* X axis */
465 scpdr = _reg_read_1(SH7709_SCPDR);
466 scpdr &= ~SCPDR_TP_SCAN_DISABLE;
467 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X);
468 _reg_write_1(SH7709_SCPDR, scpdr);
469 delay(40);
470
471 *rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
472
473 /* Y axis */
474 scpdr = _reg_read_1(SH7709_SCPDR);
475 scpdr &= ~SCPDR_TP_SCAN_X;
476 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
477 _reg_write_1(SH7709_SCPDR, scpdr);
478 delay(40);
479
480 *rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
481
482 /* restore SCPDR */
483 scpdr = _reg_read_1(SH7709_SCPDR);
484 scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
485 scpdr |= SCPDR_TP_SCAN_DISABLE;
486 _reg_write_1(SH7709_SCPDR, scpdr);
487 }
488
489
490 static int
491 psh3tp_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
492 struct lwp *l)
493 {
494 struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;
495
496 return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
497 }
498