1 /* $NetBSD: j6x0tp.c,v 1.22 2008/03/27 03:58:47 uwe Exp $ */
4 * Copyright (c) 2003 Valeriy E. Ushakov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: j6x0tp.c,v 1.22 2008/03/27 03:58:47 uwe Exp $");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
40 #include "opt_j6x0tp.h"
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
44 #include <dev/wscons/wskbdvar.h>
45 #include <dev/wscons/wsksymvar.h>
46 #include <dev/wscons/wsksymdef.h>
47 #include <dev/hpc/hpctpanelvar.h>
49 #include <machine/platid.h>
50 #include <machine/platid_mask.h>
52 #include <machine/intr.h>
54 #include <sh3/exception.h>
55 #include <sh3/intcreg.h>
56 #include <sh3/pfcreg.h>
57 #include <sh3/adcreg.h>
59 #include <sh3/dev/adcvar.h>
62 #if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
63 #define DPRINTF_ENABLE
64 #define DPRINTF_DEBUG j6x0tp_debug
65 #define DPRINTF_LEVEL 0
66 #include <machine/debug.h>
69 volatile int j6x0tp_debug
= 0;
70 #define DPRINTF_PRINTF printf_nolog
71 #define DPRINTF(arg) if (j6x0tp_debug) DPRINTF_PRINTF arg
72 #define DPRINTFN(n, arg) if (j6x0tp_debug > (n)) DPRINTF_PRINTF arg
74 #define DPRINTF(arg) ((void)0)
75 #define DPRINTFN(n, arg) ((void)0)
81 * PFC bits pertinent to Jornada 6x0 touchpanel
83 #define PHDR_TP_PEN_DOWN 0x08
85 #define SCPDR_TP_SCAN_ENABLE 0x20
86 #define SCPDR_TP_SCAN_Y 0x02
87 #define SCPDR_TP_SCAN_X 0x01
90 * A/D converter channels to get x/y from
92 #define ADC_CHANNEL_TP_Y 1
93 #define ADC_CHANNEL_TP_X 2
96 * Default (read: my device :) raw X/Y values for framebuffer edges.
99 #define J6X0TP_FB_LEFT 38
100 #define J6X0TP_FB_RIGHT 950
101 #define J6X0TP_FB_TOP 80
102 #define J6X0TP_FB_BOTTOM 900
105 * Bottom of the n'th hard icon (n = 1..4)
107 #define J6X0TP_HARD_ICON_MAX_Y(n) \
108 (J6X0TP_FB_TOP + ((J6X0TP_FB_BOTTOM - J6X0TP_FB_TOP) / 4) * (n))
111 struct j6x0tp_softc
{
114 #define J6X0TP_WSMOUSE_ENABLED 0x01
115 #define J6X0TP_WSKBD_ENABLED 0x02
120 struct callout sc_touch_ch
;
122 device_t sc_wsmousedev
;
123 device_t sc_wskbddev
;
125 struct tpcalib_softc sc_tpcalib
; /* calibration info for wsmouse */
129 /* config machinery */
130 static int j6x0tp_match(device_t
, cfdata_t
, void *);
131 static void j6x0tp_attach(device_t
, device_t
, void *);
133 /* wsmouse accessops */
134 static int j6x0tp_wsmouse_enable(void *);
135 static int j6x0tp_wsmouse_ioctl(void *, u_long
, void *, int, lwp_t
*);
136 static void j6x0tp_wsmouse_disable(void *);
138 /* wskbd accessops */
139 static int j6x0tp_wskbd_enable(void *, int);
140 static void j6x0tp_wskbd_set_leds(void *, int);
141 static int j6x0tp_wskbd_ioctl(void *, u_long
, void *, int, lwp_t
*);
143 /* internal driver routines */
144 static void j6x0tp_enable(struct j6x0tp_softc
*);
145 static void j6x0tp_disable(struct j6x0tp_softc
*);
146 static int j6x0tp_enable_child(struct j6x0tp_softc
*, int, int);
147 static int j6x0tp_intr(void *);
148 static void j6x0tp_start_polling(void *);
149 static void j6x0tp_stop_polling(struct j6x0tp_softc
*);
150 static void j6x0tp_callout_wsmouse(void *);
151 static void j6x0tp_callout_wskbd(void *);
152 static void j6x0tp_wsmouse_input(struct j6x0tp_softc
*, int, int);
153 static void j6x0tp_get_raw_xy(int *, int *);
154 static int j6x0tp_get_hard_icon(int, int);
157 static const struct wsmouse_accessops j6x0tp_accessops
= {
158 j6x0tp_wsmouse_enable
,
159 j6x0tp_wsmouse_ioctl
,
160 j6x0tp_wsmouse_disable
163 static const struct wsmouse_calibcoords j6x0tp_default_calib
= {
166 {{ J6X0TP_FB_LEFT
, J6X0TP_FB_TOP
, 0, 0 },
167 { J6X0TP_FB_RIGHT
, J6X0TP_FB_TOP
, 639, 0 },
168 { J6X0TP_FB_LEFT
, J6X0TP_FB_BOTTOM
, 0, 239 },
169 { J6X0TP_FB_RIGHT
, J6X0TP_FB_BOTTOM
, 639, 239 }}
172 static const struct wskbd_accessops j6x0tp_wskbd_accessops
= {
174 j6x0tp_wskbd_set_leds
,
179 #ifndef J6X0TP_SETTINGS_ICON_KEYSYM
180 #define J6X0TP_SETTINGS_ICON_KEYSYM KS_Home
182 #ifndef J6X0TP_PGUP_ICON_KEYSYM
183 #define J6X0TP_PGUP_ICON_KEYSYM KS_Prior
185 #ifndef J6X0TP_PGDN_ICON_KEYSYM
186 #define J6X0TP_PGDN_ICON_KEYSYM KS_Next
188 #ifndef J6X0TP_SWITCH_ICON_KEYSYM
189 #define J6X0TP_SWITCH_ICON_KEYSYM KS_End
192 static const keysym_t j6x0tp_wskbd_keydesc
[] = {
193 KS_KEYCODE(1), J6X0TP_SETTINGS_ICON_KEYSYM
,
194 KS_KEYCODE(2), J6X0TP_PGUP_ICON_KEYSYM
,
195 KS_KEYCODE(3), J6X0TP_PGDN_ICON_KEYSYM
,
196 KS_KEYCODE(4), J6X0TP_SWITCH_ICON_KEYSYM
199 static const struct wscons_keydesc j6x0tp_wskbd_keydesctab
[] = {
201 sizeof(j6x0tp_wskbd_keydesc
)/sizeof(keysym_t
),
207 static const struct wskbd_mapdata j6x0tp_wskbd_keymapdata
= {
208 j6x0tp_wskbd_keydesctab
, KB_US
212 CFATTACH_DECL_NEW(j6x0tp
, sizeof(struct j6x0tp_softc
),
213 j6x0tp_match
, j6x0tp_attach
, NULL
, NULL
);
217 j6x0tp_match(device_t parent
, cfdata_t cf
, void *aux
)
221 * XXX: platid_mask_MACH_HP_LX also matches 360LX. It's not
222 * confirmed whether touch panel in 360LX is connected this
223 * way. We may need to regroup platid masks.
225 if (!platid_match(&platid
, &platid_mask_MACH_HP_JORNADA_6XX
)
226 && !platid_match(&platid
, &platid_mask_MACH_HP_LX
))
229 if (strcmp(cf
->cf_name
, "j6x0tp") != 0)
237 j6x0tp_attach(device_t parent
, device_t self
, void *aux
)
239 struct j6x0tp_softc
*sc
;
240 struct wsmousedev_attach_args wsma
;
241 struct wskbddev_attach_args wska
;
246 sc
= device_private(self
);
250 sc
->sc_hard_icon
= 0;
252 /* touch-panel as a pointing device */
253 wsma
.accessops
= &j6x0tp_accessops
;
254 wsma
.accesscookie
= sc
;
256 sc
->sc_wsmousedev
= config_found_ia(self
, "wsmousedev", &wsma
,
258 if (sc
->sc_wsmousedev
== NULL
)
261 /* on-screen "hard icons" as a keyboard device */
263 wska
.keymap
= &j6x0tp_wskbd_keymapdata
;
264 wska
.accessops
= &j6x0tp_wskbd_accessops
;
265 wska
.accesscookie
= sc
;
267 sc
->sc_wskbddev
= config_found_ia(self
, "wskbddev", &wska
,
270 /* init calibration, set default parameters */
271 tpcalib_init(&sc
->sc_tpcalib
);
272 tpcalib_ioctl(&sc
->sc_tpcalib
, WSMOUSEIO_SCALIBCOORDS
,
273 (void *)__UNCONST(&j6x0tp_default_calib
), 0, 0);
275 /* used when in polling mode */
276 callout_init(&sc
->sc_touch_ch
, 0);
278 /* establish interrupt handler, but disable until opened */
279 intc_intr_establish(SH7709_INTEVT2_IRQ3
, IST_EDGE
, IPL_TTY
,
281 intc_intr_disable(SH7709_INTEVT2_IRQ3
);
283 if (!pmf_device_register(self
, NULL
, NULL
))
284 aprint_error_dev(self
, "unable to establish power handler\n");
289 * Enable touch panel: we start in interrupt mode.
290 * Must be called as spltty().
293 j6x0tp_enable(struct j6x0tp_softc
*sc
)
296 DPRINTFN(2, ("%s: enable\n", device_xname(sc
->sc_dev
)));
297 intc_intr_enable(SH7709_INTEVT2_IRQ3
);
302 * Disable touch panel: disable interrupt, cancel pending callout.
303 * Must be called as spltty().
306 j6x0tp_disable(struct j6x0tp_softc
*sc
)
309 DPRINTFN(2, ("%s: disable\n", device_xname(sc
->sc_dev
)));
310 intc_intr_disable(SH7709_INTEVT2_IRQ3
);
311 callout_stop(&sc
->sc_touch_ch
);
316 j6x0tp_enable_child(struct j6x0tp_softc
*sc
, int child
, int on
)
323 sc
->sc_enabled
|= child
;
325 sc
->sc_enabled
&= ~child
;
336 j6x0tp_wsmouse_enable(void *arg
)
338 struct j6x0tp_softc
*sc
= arg
;
340 DPRINTFN(1, ("%s: wsmouse enable\n", device_xname(sc
->sc_dev
)));
341 return (j6x0tp_enable_child(sc
, J6X0TP_WSMOUSE_ENABLED
, 1));
346 j6x0tp_wsmouse_disable(void *arg
)
348 struct j6x0tp_softc
*sc
= arg
;
350 DPRINTFN(1, ("%s: wsmouse disable\n", device_xname(sc
->sc_dev
)));
351 j6x0tp_enable_child(sc
, J6X0TP_WSMOUSE_ENABLED
, 0);
356 j6x0tp_wskbd_enable(void *arg
, int on
)
358 struct j6x0tp_softc
*sc
= arg
;
360 DPRINTFN(1, ("%s: wskbd %sable\n", device_xname(sc
->sc_dev
),
362 return (j6x0tp_enable_child(sc
, J6X0TP_WSKBD_ENABLED
, on
));
367 j6x0tp_intr(void *arg
)
369 struct j6x0tp_softc
*sc
= arg
;
372 uint8_t phdr
, touched
;
373 unsigned int steady
, tremor_timeout
;
375 irr0
= _reg_read_1(SH7709_IRR0
);
376 if ((irr0
& IRR0_IRQ3
) == 0) {
378 printf("%s: irr0 %02x?\n", device_xname(sc
->sc_dev
), irr0
);
383 if (!sc
->sc_enabled
) {
384 DPRINTFN(1, ("%s: intr: !sc_enabled\n",
385 device_xname(sc
->sc_dev
)));
386 intc_intr_disable(SH7709_INTEVT2_IRQ3
);
392 * Number of times the "touched" bit should be read
395 # define TREMOR_THRESHOLD 0x300
398 tremor_timeout
= TREMOR_THRESHOLD
* 16; /* XXX: arbitrary */
399 touched
= PHDR_TP_PEN_DOWN
; /* we start with "touched" state */
402 phdr
= _reg_read_1(SH7709_PHDR
);
404 if ((phdr
& PHDR_TP_PEN_DOWN
) == touched
)
408 touched
= phdr
& PHDR_TP_PEN_DOWN
;
411 if (--tremor_timeout
== 0) {
412 DPRINTF(("%s: tremor timeout!\n",
413 device_xname(sc
->sc_dev
)));
416 } while (steady
< TREMOR_THRESHOLD
);
419 intc_intr_disable(SH7709_INTEVT2_IRQ3
);
422 * ADC readings are not stable yet, so schedule
423 * callout instead of accessing ADC from the interrupt
424 * handler only to immediately delay().
426 callout_reset(&sc
->sc_touch_ch
, hz
/32,
427 j6x0tp_start_polling
, sc
);
429 DPRINTFN(1, ("%s: tremor\n", device_xname(sc
->sc_dev
)));
431 /* clear the interrupt (XXX: protect access?) */
432 _reg_write_1(SH7709_IRR0
, irr0
& ~IRR0_IRQ3
);
439 * Called from the interrupt handler at spltty() upon first touch.
440 * Decide if we are going to report this touch as a mouse click/drag
444 j6x0tp_start_polling(void *arg
)
446 struct j6x0tp_softc
*sc
= arg
;
448 int do_mouse
, do_kbd
;
452 phdr
= _reg_read_1(SH7709_PHDR
);
453 if ((phdr
& PHDR_TP_PEN_DOWN
) == 0) {
454 DPRINTFN(2, ("%s: start: pen is not down\n",
455 device_xname(sc
->sc_dev
)));
456 j6x0tp_stop_polling(sc
);
459 j6x0tp_get_raw_xy(&rawx
, &rawy
);
460 DPRINTFN(2, ("%s: start: %4d %4d -> ",
461 device_xname(sc
->sc_dev
), rawx
, rawy
));
463 do_mouse
= sc
->sc_enabled
& J6X0TP_WSMOUSE_ENABLED
;
464 #ifdef J6X0TP_WSMOUSE_EXCLUSIVE
469 do_kbd
= sc
->sc_enabled
& J6X0TP_WSKBD_ENABLED
;
473 icon
= j6x0tp_get_hard_icon(rawx
, rawy
);
476 DPRINTFN(2, ("icon %d\n", icon
));
477 sc
->sc_hard_icon
= icon
;
478 wskbd_input(sc
->sc_wskbddev
, WSCONS_EVENT_KEY_DOWN
, icon
);
479 callout_reset(&sc
->sc_touch_ch
, hz
/32,
480 j6x0tp_callout_wskbd
, sc
);
481 } else if (do_mouse
) {
482 DPRINTFN(2, ("mouse\n"));
483 j6x0tp_wsmouse_input(sc
, rawx
, rawy
);
484 callout_reset(&sc
->sc_touch_ch
, hz
/32,
485 j6x0tp_callout_wsmouse
, sc
);
487 DPRINTFN(2, ("ignore\n"));
488 j6x0tp_stop_polling(sc
);
494 * Re-enable touch panel interrupt.
495 * Called as spltty() when polling code detects pen-up.
498 j6x0tp_stop_polling(struct j6x0tp_softc
*sc
)
502 DPRINTFN(2, ("%s: stop\n", device_xname(sc
->sc_dev
)));
504 /* clear pending interrupt signal before re-enabling the interrupt */
505 irr0
= _reg_read_1(SH7709_IRR0
);
506 if ((irr0
& IRR0_IRQ3
) != 0)
507 _reg_write_1(SH7709_IRR0
, irr0
& ~IRR0_IRQ3
);
509 intc_intr_enable(SH7709_INTEVT2_IRQ3
);
514 * We are reporting this touch as a keyboard event.
515 * Poll touch screen waiting for pen-up.
518 j6x0tp_callout_wskbd(void *arg
)
520 struct j6x0tp_softc
*sc
= arg
;
526 if (!sc
->sc_enabled
) {
527 DPRINTFN(1, ("%s: wskbd callout: !sc_enabled\n",
528 device_xname(sc
->sc_dev
)));
533 phdr
= _reg_read_1(SH7709_PHDR
);
534 if ((phdr
& PHDR_TP_PEN_DOWN
) != 0) {
536 * Pen is still down, continue polling. Wskbd's
537 * auto-repeat takes care of repeating the key.
539 callout_schedule(&sc
->sc_touch_ch
, hz
/32);
541 wskbd_input(sc
->sc_wskbddev
,
542 WSCONS_EVENT_KEY_UP
, sc
->sc_hard_icon
);
543 j6x0tp_stop_polling(sc
);
550 * We are reporting this touch as a mouse click/drag.
553 j6x0tp_callout_wsmouse(void *arg
)
555 struct j6x0tp_softc
*sc
= arg
;
562 if (!sc
->sc_enabled
) {
563 DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
564 device_xname(sc
->sc_dev
)));
569 phdr
= _reg_read_1(SH7709_PHDR
);
570 if ((phdr
& PHDR_TP_PEN_DOWN
) != 0) {
571 j6x0tp_get_raw_xy(&rawx
, &rawy
);
572 j6x0tp_wsmouse_input(sc
, rawx
, rawy
); /* mouse dragged */
573 callout_schedule(&sc
->sc_touch_ch
, hz
/32);
575 wsmouse_input(sc
->sc_wsmousedev
, 0, 0, 0, 0, 0, /* button up */
576 WSMOUSE_INPUT_DELTA
);
577 j6x0tp_stop_polling(sc
);
584 * Report mouse click/drag.
587 j6x0tp_wsmouse_input(struct j6x0tp_softc
*sc
, int rawx
, int rawy
)
591 tpcalib_trans(&sc
->sc_tpcalib
, rawx
, rawy
, &x
, &y
);
593 DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
594 device_xname(sc
->sc_dev
), rawx
, rawy
, x
, y
));
596 wsmouse_input(sc
->sc_wsmousedev
,
599 WSMOUSE_INPUT_ABSOLUTE_X
| WSMOUSE_INPUT_ABSOLUTE_Y
);
604 * Read raw X/Y coordinates from the ADC.
605 * XXX: protect accesses to SCPDR?
608 j6x0tp_get_raw_xy(int *rawxp
, int *rawyp
)
613 scpdr
= _reg_read_1(SH7709_SCPDR
);
614 scpdr
|= SCPDR_TP_SCAN_ENABLE
;
615 scpdr
&= ~SCPDR_TP_SCAN_Y
; /* pull low to scan */
616 _reg_write_1(SH7709_SCPDR
, scpdr
);
619 *rawyp
= adc_sample_channel(ADC_CHANNEL_TP_Y
);
622 scpdr
= _reg_read_1(SH7709_SCPDR
);
623 scpdr
|= SCPDR_TP_SCAN_Y
;
624 scpdr
&= ~SCPDR_TP_SCAN_X
; /* pull low to scan */
625 _reg_write_1(SH7709_SCPDR
, scpdr
);
628 *rawxp
= adc_sample_channel(ADC_CHANNEL_TP_X
);
631 scpdr
= _reg_read_1(SH7709_SCPDR
);
632 scpdr
|= SCPDR_TP_SCAN_X
;
633 scpdr
&= ~SCPDR_TP_SCAN_ENABLE
;
634 _reg_write_1(SH7709_SCPDR
, scpdr
);
639 * Check if the (rawx, rawy) is inside one of the 4 hard icons.
640 * Return the icon number 1..4, or 0 if not inside an icon.
643 j6x0tp_get_hard_icon(int rawx
, int rawy
)
645 if (rawx
<= J6X0TP_FB_RIGHT
)
648 if (rawy
< J6X0TP_HARD_ICON_MAX_Y(1))
650 else if (rawy
< J6X0TP_HARD_ICON_MAX_Y(2))
652 else if (rawy
< J6X0TP_HARD_ICON_MAX_Y(3))
660 j6x0tp_wsmouse_ioctl(void *arg
, u_long cmd
, void *data
, int flag
, lwp_t
*l
)
662 struct j6x0tp_softc
*sc
= arg
;
664 return hpc_tpanel_ioctl(&sc
->sc_tpcalib
, cmd
, data
, flag
, l
);
669 j6x0tp_wskbd_ioctl(void *arg
, u_long cmd
, void *data
, int flag
, lwp_t
*l
)
671 /* struct j6x0tp_softc *sc = arg; */
675 *(int *)data
= WSKBD_TYPE_HPC_BTN
; /* may be use new type? */
678 case WSKBDIO_GETLEDS
:
683 return (EPASSTHROUGH
);
689 j6x0tp_wskbd_set_leds(void *arg
, int leds
)