psh3tp.c revision 1.2 1 /* $NetBSD: psh3tp.c,v 1.2 2005/05/31 23:22:14 uwe 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/param.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
36 #include <sys/callout.h>
37
38 #include "opt_psh3tp.h"
39
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wsmousevar.h>
42 #include <dev/hpc/hpctpanelvar.h>
43
44 #include <machine/platid.h>
45 #include <machine/platid_mask.h>
46
47 #include <machine/intr.h>
48
49 #include <sh3/exception.h>
50 #include <sh3/intcreg.h>
51 #include <sh3/pfcreg.h>
52 #include <sh3/adcreg.h>
53
54 #include <sh3/dev/adcvar.h>
55
56
57 #ifdef PSH3TP_DEBUG
58 volatile int psh3tp_debug = 4;
59 #define DPRINTF_PRINTF printf_nolog
60 #define DPRINTF(arg) if (psh3tp_debug) DPRINTF_PRINTF arg
61 #define DPRINTFN(n, arg) if (psh3tp_debug > (n)) DPRINTF_PRINTF arg
62 #else
63 #define DPRINTF(arg) ((void)0)
64 #define DPRINTFN(n, arg) ((void)0)
65 #endif
66
67
68 /*
69 * PFC bits pertinent to PERSONA HPW-50PA touch-panel
70 */
71 #define PHDR_TP_PEN_UP 0x40
72 #define SCPDR_TP_SCAN_ENABLE 0x20
73 #define SCPDR_TP_SCAN_DISABLE 0x01
74 #define SCPDR_TP_SCAN_X 0x06
75 #define SCPDR_TP_SCAN_Y 0x09
76
77 /*
78 * A/D converter channels to get x/y from
79 */
80 #define ADC_CHANNEL_TP_X 1
81 #define ADC_CHANNEL_TP_Y 0
82
83 /*
84 * Default (read: my device) raw X/Y values for framebuffer edges.
85 */
86 #define PSH3TP_FB_RIGHT 56
87 #define PSH3TP_FB_LEFT 969
88 #define PSH3TP_FB_TOP 848
89 #define PSH3TP_FB_BOTTOM 121
90
91
92 struct psh3tp_softc {
93 struct device sc_dev;
94
95 #define PSH3TP_WSMOUSE_ENABLED 0x01
96 int sc_enabled;
97 struct callout sc_touch_ch;
98 struct device *sc_wsmousedev;
99 struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
100 };
101
102
103 /* config machinery */
104 static int psh3tp_match(struct device *, struct cfdata *, void *);
105 static void psh3tp_attach(struct device *, struct device *, void *);
106 static int psh3tp_wsmouse_submatch(struct device *, struct cfdata *, void *);
107
108 /* wsmouse accessops */
109 static int psh3tp_wsmouse_enable(void *);
110 static int psh3tp_wsmouse_ioctl(void *, u_long, caddr_t, int, struct proc *);
111 static void psh3tp_wsmouse_disable(void *);
112
113 /* internal driver routines */
114 static void psh3tp_enable(struct psh3tp_softc *);
115 static void psh3tp_disable(struct psh3tp_softc *);
116 static int psh3tp_set_enable(struct psh3tp_softc *, int, int);
117 static int psh3tp_intr(void *);
118 static void psh3tp_start_polling(void *);
119 static void psh3tp_stop_polling(struct psh3tp_softc *);
120 static void psh3tp_callout_wsmouse(void *);
121 static void psh3tp_wsmouse_input(struct psh3tp_softc *, int, int);
122 static void psh3tp_get_raw_xy(int *, int *);
123
124
125 const struct wsmouse_accessops psh3tp_accessops = {
126 psh3tp_wsmouse_enable,
127 psh3tp_wsmouse_ioctl,
128 psh3tp_wsmouse_disable
129 };
130
131 static const struct wsmouse_calibcoords psh3tp_default_calib = {
132 0, 0, 639, 239,
133 4,
134 {{ PSH3TP_FB_LEFT, PSH3TP_FB_TOP, 0, 0 },
135 { PSH3TP_FB_RIGHT, PSH3TP_FB_TOP, 639, 0 },
136 { PSH3TP_FB_LEFT, PSH3TP_FB_BOTTOM, 0, 239 },
137 { PSH3TP_FB_RIGHT, PSH3TP_FB_BOTTOM, 639, 239 }}
138 };
139
140
141 CFATTACH_DECL(psh3tp, sizeof(struct psh3tp_softc),
142 psh3tp_match, psh3tp_attach, NULL, NULL);
143
144
145 static int
146 psh3tp_match(struct device *parent, struct cfdata *cf, void *aux)
147 {
148
149 if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
150 return (0);
151
152 if (strcmp(cf->cf_name, "psh3tp") != 0)
153 return (0);
154
155 return (1);
156 }
157
158
159 /*
160 * Attach the touch panel driver and its wsmouse child.
161 *
162 * Note that we have to use submatch to distinguish between child because
163 * wsmouse_match matches unconditionally.
164 */
165 static void
166 psh3tp_attach(struct device *parent, struct device *self, void *aux)
167 {
168 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
169 struct wsmousedev_attach_args wsma;
170
171 printf("\n");
172
173 sc->sc_enabled = 0;
174
175 /* touch-panel as a pointing device */
176 wsma.accessops = &psh3tp_accessops;
177 wsma.accesscookie = sc;
178
179 sc->sc_wsmousedev = config_found_sm(
180 self, &wsma, wsmousedevprint, psh3tp_wsmouse_submatch);
181 if (sc->sc_wsmousedev == NULL)
182 return;
183
184 /* init calibration, set default parameters */
185 tpcalib_init(&sc->sc_tpcalib);
186 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
187 (caddr_t)__UNCONST(&psh3tp_default_calib), 0, 0);
188
189 /* used when in polling mode */
190 callout_init(&sc->sc_touch_ch);
191
192 /* establish interrupt handler, but disable until opened */
193 intc_intr_establish(SH7709_INTEVT2_IRQ2,
194 IST_EDGE, IPL_TTY, psh3tp_intr, sc);
195 intc_intr_disable(SH7709_INTEVT2_IRQ2);
196 }
197
198
199 static int
200 psh3tp_wsmouse_submatch(struct device *parent, struct cfdata *cf, void *aux)
201 {
202
203 return (!strcmp(cf->cf_name, "wsmouse"));
204 }
205
206
207 /*
208 * Enable touch panel: we start in interrupt mode.
209 * Must be called at spltty().
210 */
211 static void
212 psh3tp_enable(struct psh3tp_softc *sc)
213 {
214
215 DPRINTFN(2, ("%s: enable\n", sc->sc_dev.dv_xname));
216 intc_intr_enable(SH7709_INTEVT2_IRQ2);
217 }
218
219
220 /*
221 * Disable touch panel: disable interrupt, cancel pending callout.
222 * Must be called at spltty().
223 */
224 static void
225 psh3tp_disable(struct psh3tp_softc *sc)
226 {
227
228 DPRINTFN(2, ("%s: disable\n", sc->sc_dev.dv_xname));
229 intc_intr_disable(SH7709_INTEVT2_IRQ2);
230 callout_stop(&sc->sc_touch_ch);
231 }
232
233
234 static int
235 psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child)
236 {
237 int s = spltty();
238
239 if (on) {
240 if (!sc->sc_enabled)
241 psh3tp_enable(sc);
242 sc->sc_enabled |= child;
243 } else {
244 sc->sc_enabled &= ~child;
245 if (!sc->sc_enabled)
246 psh3tp_disable(sc);
247 }
248
249 splx(s);
250 return (0);
251 }
252
253
254 static int
255 psh3tp_wsmouse_enable(void *self)
256 {
257 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
258
259 DPRINTFN(1, ("%s: wsmouse enable\n", sc->sc_dev.dv_xname));
260 return (psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED));
261 }
262
263
264 static void
265 psh3tp_wsmouse_disable(void *self)
266 {
267 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
268
269 DPRINTFN(1, ("%s: wsmouse disable\n", sc->sc_dev.dv_xname));
270 psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED);
271 }
272
273
274 static int
275 psh3tp_intr(void *self)
276 {
277 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
278
279 uint8_t irr0;
280 uint8_t phdr, touched;
281 unsigned int steady, tremor_timeout;
282
283 irr0 = _reg_read_1(SH7709_IRR0);
284 if ((irr0 & IRR0_IRQ2) == 0) {
285 #ifdef DIAGNOSTIC
286 printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
287 #endif
288 return (0);
289 }
290
291 if (!sc->sc_enabled) {
292 DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
293 intc_intr_disable(SH7709_INTEVT2_IRQ2);
294 goto served;
295 }
296
297 /*
298 * Number of times the "touched" bit should be read
299 * consecutively.
300 */
301 #define TREMOR_THRESHOLD 0x300
302 steady = 0;
303 tremor_timeout = TREMOR_THRESHOLD * 16; /* XXX: arbitrary */
304 touched = TRUE; /* we start with "touched" state */
305
306 do {
307 uint8_t state;
308
309 phdr = _reg_read_1(SH7709_PHDR);
310 state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);
311
312 if (state == touched)
313 ++steady;
314 else {
315 steady = 0;
316 touched = state;
317 }
318
319 if (--tremor_timeout == 0) {
320 DPRINTF(("%s: tremor timeout!\n", sc->sc_dev.dv_xname));
321 goto served;
322 }
323 } while (steady < TREMOR_THRESHOLD);
324
325 if (touched) {
326 intc_intr_disable(SH7709_INTEVT2_IRQ2);
327
328 /*
329 * ADC readings are not stable yet, so schedule
330 * callout instead of accessing ADC from the interrupt
331 * handler only to immediately delay().
332 */
333 callout_reset(&sc->sc_touch_ch,
334 hz/32, psh3tp_start_polling, sc);
335 } else
336 DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
337 served:
338 /* clear the interrupt */
339 _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);
340
341 return (1);
342 }
343
344
345 /*
346 * Called from the interrupt handler at spltty() upon first touch.
347 * Decide if we are going to report this touch as a mouse click/drag.
348 */
349 static void
350 psh3tp_start_polling(void *self)
351 {
352 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
353 uint8_t phdr;
354 int rawx, rawy;
355
356 phdr = _reg_read_1(SH7709_PHDR);
357 if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
358 DPRINTFN(2, ("%s: start: pen is not down\n",
359 sc->sc_dev.dv_xname));
360 psh3tp_stop_polling(sc);
361 return;
362 }
363
364 psh3tp_get_raw_xy(&rawx, &rawy);
365 DPRINTFN(2, ("%s: start: %4d %4d -> ",
366 sc->sc_dev.dv_xname, rawx, rawy));
367
368 if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) {
369 DPRINTFN(2, ("mouse\n"));
370 psh3tp_wsmouse_input(sc, rawx, rawy);
371 callout_reset(&sc->sc_touch_ch,
372 hz/32, psh3tp_callout_wsmouse, sc);
373 } else {
374 DPRINTFN(2, ("ignore\n"));
375 psh3tp_stop_polling(sc);
376 }
377 }
378
379
380 /*
381 * Re-enable touch panel interrupt.
382 * Called at spltty() when polling code detects pen-up.
383 */
384 static void
385 psh3tp_stop_polling(struct psh3tp_softc *sc)
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, 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,
450 0, /* flags */
451 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
452 }
453
454
455 /*
456 * Read raw X/Y coordinates from the ADC.
457 */
458 static void
459 psh3tp_get_raw_xy(int *rawxp, int *rawyp)
460 {
461 uint8_t scpdr;
462
463 /* X axis */
464 scpdr = _reg_read_1(SH7709_SCPDR);
465 scpdr &= ~SCPDR_TP_SCAN_DISABLE;
466 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X);
467 _reg_write_1(SH7709_SCPDR, scpdr);
468 delay(40);
469
470 *rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
471
472 /* Y axis */
473 scpdr = _reg_read_1(SH7709_SCPDR);
474 scpdr &= ~SCPDR_TP_SCAN_X;
475 scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
476 _reg_write_1(SH7709_SCPDR, scpdr);
477 delay(40);
478
479 *rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
480
481 /* restore SCPDR */
482 scpdr = _reg_read_1(SH7709_SCPDR);
483 scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
484 scpdr |= SCPDR_TP_SCAN_DISABLE;
485 _reg_write_1(SH7709_SCPDR, scpdr);
486 }
487
488
489 static int
490 psh3tp_wsmouse_ioctl(
491 void *self, u_long cmd, caddr_t data, int flag, struct proc *p)
492 {
493 struct psh3tp_softc *sc = (struct psh3tp_softc *)self;
494
495 return (hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, p));
496 }
497