xref: /netbsd/sys/arch/hpcmips/dev/ucbtp.c (revision beecddb6)
1 /*	$NetBSD: ucbtp.c,v 1.23 2021/08/07 16:18:54 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
34  *	Touch panel part.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ucbtp.c,v 1.23 2021/08/07 16:18:54 thorpej Exp $");
39 
40 #include "opt_use_poll.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 #include <machine/bootinfo.h> /* bootinfo */
49 
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wsmousevar.h>
52 
53 #include <dev/hpc/hpctpanelvar.h>
54 
55 #include <hpcmips/tx/tx39var.h>
56 #include <hpcmips/tx/tx39sibvar.h>
57 #include <hpcmips/tx/tx39sibreg.h>
58 #include <hpcmips/tx/tx39icureg.h>
59 
60 #include <hpcmips/dev/ucb1200var.h>
61 #include <hpcmips/dev/ucb1200reg.h>
62 
63 #include <hpcmips/tx/txsnd.h>
64 #include <dev/hpc/video_subr.h> /* debug */
65 
66 #ifdef UCBTPDEBUG
67 int	ucbtp_debug = 0;
68 #define	DPRINTF(arg) if (ucbtp_debug) printf arg;
69 #define	DPRINTFN(n, arg) if (ucbtp_debug > (n)) printf arg;
70 #else
71 #define	DPRINTF(arg)
72 #define DPRINTFN(n, arg)
73 #endif
74 
75 enum ucbts_stat {
76 	UCBTS_STAT_DISABLE,
77 	UCBTS_STAT_RELEASE,
78 	UCBTS_STAT_TOUCH,
79 	UCBTS_STAT_DRAG,
80 };
81 
82 #define UCBTS_POSX	1
83 #define UCBTS_POSY	2
84 #define UCBTS_PRESS	3
85 
86 #define UCBTS_PRESS_THRESHOLD	80
87 #define UCBTS_TAP_THRESHOLD	5
88 
89 enum ucbadc_state {
90 /* 0 */	UCBADC_IDLE,
91 /* 1 */	UCBADC_ADC_INIT,
92 /* 2 */	UCBADC_ADC_FINI,
93 /* 3 */	UCBADC_MEASUMENT_INIT,
94 /* 4 */	UCBADC_MEASUMENT_FINI,
95 /* 5 */	UCBADC_ADC_ENABLE,
96 /* 6 */	UCBADC_ADC_START0,
97 /* 7 */	UCBADC_ADC_START1,
98 /* 8 */	UCBADC_ADC_DATAREAD,
99 /* 9 */	UCBADC_ADC_DATAREAD_WAIT,
100 /*10 */	UCBADC_ADC_DISABLE,
101 /*11 */	UCBADC_ADC_INTRMODE,
102 /*12 */	UCBADC_ADC_INPUT,
103 /*13 */	UCBADC_INTR_ACK0,
104 /*14 */	UCBADC_INTR_ACK1,
105 /*15 */	UCBADC_INTR_ACK2,
106 /*16 */	UCBADC_REGREAD,
107 /*17 */	UCBADC_REGWRITE
108 };
109 
110 struct ucbtp_softc {
111 	device_t sc_dev;
112 	device_t sc_sib; /* parent (TX39 SIB module) */
113 	device_t sc_ucb; /* parent (UCB1200 module) */
114 	tx_chipset_tag_t sc_tc;
115 
116 	enum ucbts_stat sc_stat;
117 	int sc_polling;
118 	int sc_polling_finish;
119 	void *sc_pollh;
120 
121 	struct tpcalib_softc sc_tpcalib;
122 	int sc_calibrated;
123 
124 	/* measurement value */
125 	int sc_x, sc_y, sc_p;
126 	int sc_ox, sc_oy;
127 	int sc_xy_reverse; /* some platform pin connect interchanged */
128 
129 	/*
130 	 * touch panel state machine
131 	 */
132 	void *sm_ih; /* TX39 SIB subframe 0 interrupt handler */
133 
134 	int sm_addr; /* UCB1200 register address */
135 	u_int32_t sm_reg;  /* UCB1200 register data & TX39 SIB header */
136 	int sm_tmpreg;
137 #define UCBADC_RETRY_DEFAULT		200
138 	int sm_retry; /* retry counter */
139 
140 	enum ucbadc_state sm_state;
141 	int		sm_measurement; /* X, Y, Pressure */
142 #define	UCBADC_MEASUREMENT_X		0
143 #define	UCBADC_MEASUREMENT_Y		1
144 #define	UCBADC_MEASUREMENT_PRESSURE	2
145 	int sm_returnstate;
146 
147 	int sm_read_state, sm_write_state;
148 	int sm_writing;	/* writing state flag */
149 	u_int32_t sm_write_val;	/* temporary buffer */
150 
151 	int sm_rw_retry; /* retry counter for r/w */
152 
153 	/* wsmouse */
154 	device_t sc_wsmousedev;
155 };
156 
157 int	ucbtp_match(device_t, cfdata_t, void *);
158 void	ucbtp_attach(device_t, device_t, void *);
159 
160 int	ucbtp_sibintr(void *);
161 int	ucbtp_poll(void *);
162 int	ucbtp_adc_async(void *);
163 int	ucbtp_input(struct ucbtp_softc *);
164 int	ucbtp_busy(void *);
165 
166 int	ucbtp_enable(void *);
167 int	ucbtp_ioctl(void *, u_long, void *, int, struct lwp *);
168 void	ucbtp_disable(void *);
169 
170 CFATTACH_DECL_NEW(ucbtp, sizeof(struct ucbtp_softc),
171     ucbtp_match, ucbtp_attach, NULL, NULL);
172 
173 const struct wsmouse_accessops ucbtp_accessops = {
174 	ucbtp_enable,
175 	ucbtp_ioctl,
176 	ucbtp_disable,
177 };
178 
179 /*
180  * XXX currently no calibration method. this is temporary hack.
181  */
182 #include <machine/platid.h>
183 
184 struct	wsmouse_calibcoords *calibration_sample_lookup(void);
185 int	ucbtp_calibration(struct ucbtp_softc *);
186 
187 struct calibration_sample_table {
188 	platid_t	cst_platform;
189 	struct wsmouse_calibcoords cst_sample;
190 } calibration_sample_table[] = {
191 	{{{PLATID_WILD, PLATID_MACH_COMPAQ_C_8XX}},  /* uch machine */
192 	 { 0, 0, 639, 239, 5,
193 	   {{ 507, 510, 320, 120 },
194 	    { 898, 757,  40,  40 },
195 	    { 900, 255,  40, 200 },
196 	    { 109, 249, 600, 200 },
197 	    { 110, 753, 600,  40 }}}},
198 
199 	{{{PLATID_WILD, PLATID_MACH_COMPAQ_C_2010}}, /* uch machine */
200 	 { 0, 0, 639, 239, 5,
201 	   {{ 506, 487, 320, 120 },
202 	    { 880, 250,  40,  40 },
203 	    { 880, 718,  40, 200 },
204 	    { 140, 726, 600, 200 },
205 	    { 137, 250, 600,  40 }}}},
206 
207 	{{{PLATID_WILD, PLATID_MACH_SHARP_MOBILON_HC4100}}, /* uch machine */
208 	 { 0, 0, 639, 239, 5,
209 	   {{ 497, 501, 320, 120 },
210 	    { 752, 893,  40,  40 },
211 	    { 242, 891,  40, 200 },
212 	    { 241, 115, 600, 200 },
213 	    { 747, 101, 600,  40 }}}},
214 
215 	{{{PLATID_WILD, PLATID_MACH_SHARP_TELIOS_HCVJ}}, /* uch machine */
216 	 { 0, 0, 799, 479, 5,
217 	   {{ 850, 150,   1,   1 },
218 	    { 850, 880,   1, 479 },
219 	    { 850, 880,   1, 479 },
220 	    {  85, 880, 799, 479 },
221 	    {  85, 150, 799,   1 }}}},
222 
223 	{{{PLATID_UNKNOWN, PLATID_UNKNOWN}},
224 	 { 0, 0, 639, 239, 5,
225 	   {{0, 0, 0, 0},
226 	    {0, 0, 0, 0},
227 	    {0, 0, 0, 0},
228 	    {0, 0, 0, 0},
229 	    {0, 0, 0, 0}}}},
230 };
231 
232 struct wsmouse_calibcoords *
calibration_sample_lookup(void)233 calibration_sample_lookup(void)
234 {
235 	struct calibration_sample_table *tab;
236 	platid_mask_t mask;
237 
238 	for (tab = calibration_sample_table;
239 	    tab->cst_platform.dw.dw1 != PLATID_UNKNOWN; tab++) {
240 
241 		mask = PLATID_DEREF(&tab->cst_platform);
242 
243 		if (platid_match(&platid, &mask)) {
244 			return (&tab->cst_sample);
245 		}
246 	}
247 
248 	return (0);
249 }
250 
251 int
ucbtp_calibration(struct ucbtp_softc * sc)252 ucbtp_calibration(struct ucbtp_softc *sc)
253 {
254 	struct wsmouse_calibcoords *cs;
255 
256 	if (sc->sc_tc->tc_videot)
257 		video_calibration_pattern(sc->sc_tc->tc_videot); /* debug */
258 
259 	tpcalib_init(&sc->sc_tpcalib);
260 
261 	if (!(cs = calibration_sample_lookup())) {
262 		DPRINTF(("no calibration data"));
263 		return (1);
264 	}
265 
266 	sc->sc_calibrated =
267 	    tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
268 		(void *)cs, 0, 0) == 0 ? 1 : 0;
269 
270 	if (!sc->sc_calibrated)
271 		printf("not ");
272 	printf("calibrated");
273 
274 	return (0);
275 }
276 
277 int
ucbtp_match(device_t parent,cfdata_t cf,void * aux)278 ucbtp_match(device_t parent, cfdata_t cf, void *aux)
279 {
280 
281 	return (1);
282 }
283 
284 void
ucbtp_attach(device_t parent,device_t self,void * aux)285 ucbtp_attach(device_t parent, device_t self, void *aux)
286 {
287 	struct ucb1200_attach_args *ucba = aux;
288 	struct ucbtp_softc *sc = device_private(self);
289 	struct wsmousedev_attach_args wsmaa;
290 	tx_chipset_tag_t tc;
291 
292 	sc->sc_dev = self;
293 	tc = sc->sc_tc = ucba->ucba_tc;
294 	sc->sc_sib = ucba->ucba_sib;
295 	sc->sc_ucb = ucba->ucba_ucb;
296 
297 	printf(": ");
298 	/* touch panel interrupt */
299 	tx_intr_establish(tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBIRQPOSINT),
300 	    IST_EDGE, IPL_TTY, ucbtp_sibintr, sc);
301 
302 	/* attempt to calibrate touch panel */
303 	ucbtp_calibration(sc);
304 #ifdef TX392X /* hack for Telios HC-VJ1C */
305 	sc->sc_xy_reverse = 1;
306 #endif
307 
308 	printf("\n");
309 
310 	wsmaa.accessops = &ucbtp_accessops;
311 	wsmaa.accesscookie = sc;
312 
313 	ucb1200_state_install(parent, ucbtp_busy, self, UCB1200_TP_MODULE);
314 
315 	/*
316 	 * attach the wsmouse
317 	 */
318 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint,
319 	    CFARGS_NONE);
320 }
321 
322 int
ucbtp_busy(void * arg)323 ucbtp_busy(void *arg)
324 {
325 	struct ucbtp_softc *sc = arg;
326 
327 	return (sc->sm_state != UCBADC_IDLE);
328 }
329 
330 int
ucbtp_poll(void * arg)331 ucbtp_poll(void *arg)
332 {
333 	struct ucbtp_softc *sc = arg;
334 
335 	if (!ucb1200_state_idle(sc->sc_ucb)) /* subframe0 busy */
336 		return (POLL_CONT);
337 
338 	if (sc->sc_polling_finish) {
339 		sc->sc_polling_finish = 0;
340 		return (POLL_END);
341 	}
342 
343 	/* execute A-D converter */
344 	sc->sm_state = UCBADC_ADC_INIT;
345 	ucbtp_adc_async(sc);
346 
347 	return (POLL_CONT);
348 }
349 
350 int
ucbtp_sibintr(void * arg)351 ucbtp_sibintr(void *arg)
352 {
353 	struct ucbtp_softc *sc = arg;
354 
355 	sc->sc_stat = UCBTS_STAT_TOUCH;
356 
357 	/* click! */
358 	tx_sound_click(sc->sc_tc);
359 
360 	/* invoke touch panel polling */
361 	if (!sc->sc_polling) {
362 		sc->sc_pollh = tx39_poll_establish(sc->sc_tc, 1, IST_EDGE,
363 		    ucbtp_poll, sc);
364 		if (!sc->sc_pollh) {
365 			printf("%s: can't poll\n", device_xname(sc->sc_dev));
366 		}
367 	}
368 
369 	/* don't acknoledge interrupt until polling finish */
370 
371 	return (0);
372 }
373 
374 #define REGWRITE(addr, reg, ret) (					\
375 	sc->sm_addr = (addr),						\
376 	sc->sm_reg = (reg),						\
377 	sc->sm_returnstate = (ret),					\
378 	sc->sm_state = UCBADC_REGWRITE)
379 #define REGREAD(addr, ret) (						\
380 	sc->sm_addr = (addr),						\
381 	sc->sm_returnstate = (ret),					\
382 	sc->sm_state = UCBADC_REGREAD)
383 
384 int
ucbtp_adc_async(void * arg)385 ucbtp_adc_async(void *arg)
386 {
387 	struct ucbtp_softc *sc = arg;
388 	tx_chipset_tag_t tc = sc->sc_tc;
389 	txreg_t reg;
390 	u_int16_t reg16;
391 
392 	DPRINTFN(9, ("state: %d\n", sc->sm_state));
393 
394 	switch (sc->sm_state) {
395 	default:
396 		panic("ucbtp_adc: invalid state %d", sc->sm_state);
397 		/* NOTREACHED */
398 		break;
399 
400 	case UCBADC_IDLE:
401 		/* nothing to do */
402 		break;
403 
404 	case UCBADC_ADC_INIT:
405 		sc->sc_polling++;
406 		sc->sc_stat = UCBTS_STAT_DRAG;
407 		/* enable heart beat of this state machine */
408 		sc->sm_ih = tx_intr_establish(
409 			tc,
410 			MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
411 			IST_EDGE, IPL_TTY, ucbtp_adc_async, sc);
412 
413 		sc->sm_state = UCBADC_MEASUMENT_INIT;
414 		break;
415 
416 	case UCBADC_ADC_FINI:
417 		/* disable heart beat of this state machine */
418 		tx_intr_disestablish(tc, sc->sm_ih);
419 		sc->sm_state = UCBADC_IDLE;
420 		break;
421 
422 	case UCBADC_MEASUMENT_INIT:
423 		switch (sc->sm_measurement) {
424 		default:
425 			panic("unknown measurement spec.");
426 			/* NOTREACHED */
427 			break;
428 		case UCBADC_MEASUREMENT_X:
429 			REGWRITE(UCB1200_TSCTRL_REG,
430 			    UCB1200_TSCTRL_XPOSITION,
431 			    UCBADC_ADC_ENABLE);
432 			break;
433 		case UCBADC_MEASUREMENT_Y:
434 			REGWRITE(UCB1200_TSCTRL_REG,
435 			    UCB1200_TSCTRL_YPOSITION,
436 			    UCBADC_ADC_ENABLE);
437 			break;
438 		case UCBADC_MEASUREMENT_PRESSURE:
439 			REGWRITE(UCB1200_TSCTRL_REG,
440 			    UCB1200_TSCTRL_PRESSURE,
441 			    UCBADC_ADC_ENABLE);
442 			break;
443 		}
444 		break;
445 
446 	case UCBADC_MEASUMENT_FINI:
447 		switch (sc->sm_measurement) {
448 		case UCBADC_MEASUREMENT_X:
449 			sc->sm_measurement = UCBADC_MEASUREMENT_Y;
450 			sc->sm_state = UCBADC_MEASUMENT_INIT;
451 			break;
452 		case UCBADC_MEASUREMENT_Y:
453 			sc->sm_measurement = UCBADC_MEASUREMENT_PRESSURE;
454 			sc->sm_state = UCBADC_MEASUMENT_INIT;
455 			break;
456 		case UCBADC_MEASUREMENT_PRESSURE:
457 			sc->sm_measurement = UCBADC_MEASUREMENT_X;
458 			/* measurement complete. pass down to wsmouse_input */
459 			sc->sm_state = UCBADC_ADC_INPUT;
460 			break;
461 		}
462 		break;
463 
464 	case UCBADC_ADC_ENABLE:
465 		switch (sc->sm_measurement) {
466 		case UCBADC_MEASUREMENT_PRESSURE:
467 			/* FALLTHROUGH */
468 		case UCBADC_MEASUREMENT_X:
469 			sc->sm_tmpreg = UCB1200_ADCCTRL_INPUT_SET(
470 				UCB1200_ADCCTRL_ENABLE,
471 				UCB1200_ADCCTRL_INPUT_TSPX);
472 			REGWRITE(UCB1200_ADCCTRL_REG, sc->sm_tmpreg,
473 			    UCBADC_ADC_START0);
474 			break;
475 		case UCBADC_MEASUREMENT_Y:
476 			sc->sm_tmpreg = UCB1200_ADCCTRL_INPUT_SET(
477 				UCB1200_ADCCTRL_ENABLE,
478 				UCB1200_ADCCTRL_INPUT_TSPY);
479 			REGWRITE(UCB1200_ADCCTRL_REG, sc->sm_tmpreg,
480 			    UCBADC_ADC_START0);
481 			break;
482 		}
483 		break;
484 
485 	case UCBADC_ADC_START0:
486 		REGWRITE(UCB1200_ADCCTRL_REG,
487 		    sc->sm_tmpreg | UCB1200_ADCCTRL_START,
488 		    UCBADC_ADC_START1);
489 		break;
490 
491 	case UCBADC_ADC_START1:
492 		REGWRITE(UCB1200_ADCCTRL_REG,
493 		    sc->sm_tmpreg,
494 		    UCBADC_ADC_DATAREAD);
495 		sc->sm_retry = UCBADC_RETRY_DEFAULT;
496 		break;
497 
498 	case UCBADC_ADC_DATAREAD:
499 		REGREAD(UCB1200_ADCDATA_REG, UCBADC_ADC_DATAREAD_WAIT);
500 		break;
501 
502 	case UCBADC_ADC_DATAREAD_WAIT:
503 		reg16 = TX39_SIBSF0_REGDATA(sc->sm_reg);
504 		if (!(reg16 & UCB1200_ADCDATA_INPROGRESS) &&
505 		    --sc->sm_retry > 0) {
506 			sc->sm_state = UCBADC_ADC_DATAREAD;
507 		} else {
508 			if (sc->sm_retry <= 0) {
509 				printf("dataread failed\n");
510 				sc->sm_state = UCBADC_ADC_FINI;
511 				break;
512 			}
513 
514 			switch (sc->sm_measurement) {
515 			case UCBADC_MEASUREMENT_X:
516 				sc->sc_x = UCB1200_ADCDATA(reg16);
517 				DPRINTFN(9, ("x=%d\n", sc->sc_x));
518 				break;
519 			case UCBADC_MEASUREMENT_Y:
520 				sc->sc_y = UCB1200_ADCDATA(reg16);
521 				DPRINTFN(9, ("y=%d\n", sc->sc_y));
522 				break;
523 			case UCBADC_MEASUREMENT_PRESSURE:
524 				sc->sc_p = UCB1200_ADCDATA(reg16);
525 				DPRINTFN(9, ("p=%d\n", sc->sc_p));
526 				break;
527 			}
528 
529 			sc->sm_state = UCBADC_ADC_DISABLE;
530 		}
531 
532 		break;
533 
534 	case UCBADC_ADC_DISABLE:
535 		REGWRITE(UCB1200_ADCCTRL_REG, 0, UCBADC_ADC_INTRMODE);
536 
537 		break;
538 	case UCBADC_ADC_INTRMODE:
539 		REGWRITE(UCB1200_TSCTRL_REG, UCB1200_TSCTRL_INTERRUPT,
540 		    UCBADC_MEASUMENT_FINI);
541 		break;
542 
543 	case UCBADC_ADC_INPUT:
544 		if (ucbtp_input(sc) == 0)
545 			sc->sm_state = UCBADC_ADC_FINI;
546 		else
547 			sc->sm_state = UCBADC_INTR_ACK0;
548 		break;
549 
550 	case UCBADC_INTR_ACK0:
551 		REGREAD(UCB1200_INTSTAT_REG, UCBADC_INTR_ACK1);
552 		break;
553 
554 	case UCBADC_INTR_ACK1:
555 		REGWRITE(UCB1200_INTSTAT_REG, sc->sm_reg, UCBADC_INTR_ACK2);
556 		break;
557 
558 	case UCBADC_INTR_ACK2:
559 		sc->sc_polling_finish = 1;
560 		REGWRITE(UCB1200_INTSTAT_REG, 0, UCBADC_ADC_FINI);
561 		break;
562 
563 		/*
564 		 * UCB1200 register access state
565 		 */
566 	case UCBADC_REGREAD:
567 		/*
568 		 * In	: sc->sm_addr
569 		 * Out	: sc->sm_reg  (with SIBtag)
570 		 */
571 #define TXSIB_REGREAD_INIT	0
572 #define TXSIB_REGREAD_READ	1
573 		switch (sc->sm_read_state) {
574 		case TXSIB_REGREAD_INIT:
575 			reg = TX39_SIBSF0_REGADDR_SET(0, sc->sm_addr);
576 			tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
577 			sc->sm_rw_retry = UCBADC_RETRY_DEFAULT;
578 			sc->sm_read_state = TXSIB_REGREAD_READ;
579 			break;
580 		case TXSIB_REGREAD_READ:
581 			reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
582 			if ((TX39_SIBSF0_REGADDR(reg) != sc->sm_addr) &&
583 			    --sc->sm_rw_retry > 0) {
584 				break;
585 			}
586 
587 			if (sc->sm_rw_retry <= 0) {
588 				printf("sf0read: command failed\n");
589 				sc->sm_state = UCBADC_ADC_FINI;
590 			} else {
591 				sc->sm_reg = reg;
592 				sc->sm_read_state = TXSIB_REGREAD_INIT;
593 				DPRINTFN(9, ("%08x\n", reg));
594 				if (sc->sm_writing)
595 					sc->sm_state = UCBADC_REGWRITE;
596 				else
597 					sc->sm_state = sc->sm_returnstate;
598 			}
599 			break;
600 		}
601 		break;
602 
603 	case UCBADC_REGWRITE:
604 		/*
605 		 * In	: sc->sm_addr, sc->sm_reg (lower 16bit only)
606 		 */
607 #define TXSIB_REGWRITE_INIT	0
608 #define TXSIB_REGWRITE_WRITE	1
609 		switch (sc->sm_write_state) {
610 		case TXSIB_REGWRITE_INIT:
611 			sc->sm_writing = 1;
612 			sc->sm_write_state = TXSIB_REGWRITE_WRITE;
613 			sc->sm_state = UCBADC_REGREAD;
614 
615 			sc->sm_write_val = sc->sm_reg;
616 			break;
617 		case TXSIB_REGWRITE_WRITE:
618 			sc->sm_writing = 0;
619 			sc->sm_write_state = TXSIB_REGWRITE_INIT;
620 			sc->sm_state = sc->sm_returnstate;
621 
622 			reg = sc->sm_reg;
623 			reg |= TX39_SIBSF0_WRITE;
624 			TX39_SIBSF0_REGDATA_CLR(reg);
625 			reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sm_write_val);
626 			tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
627 			break;
628 		}
629 		break;
630 	}
631 
632 	return (0);
633 }
634 
635 int
ucbtp_input(struct ucbtp_softc * sc)636 ucbtp_input(struct ucbtp_softc *sc)
637 {
638 	int rx, ry, x, y, p;
639 
640 	rx = sc->sc_x;
641 	ry = sc->sc_y;
642 	p = sc->sc_p;
643 
644 	if (!sc->sc_calibrated) {
645 		DPRINTFN(2, ("x=%4d y=%4d p=%4d\n", rx, ry, p));
646 		DPRINTF(("ucbtp_input: no calibration data\n"));
647 	}
648 
649 	if (p < UCBTS_PRESS_THRESHOLD || rx == 0x3ff || ry == 0x3ff ||
650 	    rx == 0 || ry == 0) {
651 		sc->sc_stat = UCBTS_STAT_RELEASE;
652 		if (sc->sc_polling < UCBTS_TAP_THRESHOLD) {
653 			DPRINTFN(2, ("TAP!\n"));
654 			/* button 0 DOWN */
655 			wsmouse_input(sc->sc_wsmousedev, 1, 0, 0, 0, 0, 0);
656 			/* button 0 UP */
657 			wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0, 0);
658 		} else {
659 			wsmouse_input(sc->sc_wsmousedev, 0,
660 			    sc->sc_ox, sc->sc_oy, 0, 0,
661 			    WSMOUSE_INPUT_ABSOLUTE_X |
662 			    WSMOUSE_INPUT_ABSOLUTE_Y);
663 
664 			DPRINTFN(2, ("RELEASE\n"));
665 		}
666 		sc->sc_polling = 0;
667 
668 		return (1);
669 	}
670 
671 	if (sc->sc_xy_reverse)
672 		tpcalib_trans(&sc->sc_tpcalib, ry, rx, &x, &y);
673 	else
674 		tpcalib_trans(&sc->sc_tpcalib, rx, ry, &x, &y);
675 
676 	DPRINTFN(2, ("x: %4d->%4d y: %4d->%4d pressure=%4d\n",
677 	    rx, x, ry, y, p));
678 
679 	/* debug draw */
680 	if (sc->sc_tc->tc_videot) {
681 		if (sc->sc_polling == 1)
682 			video_dot(sc->sc_tc->tc_videot, x, y);
683 		else
684 			video_line(sc->sc_tc->tc_videot, sc->sc_ox,
685 			    sc->sc_oy, x, y);
686 	}
687 
688 	sc->sc_ox = x, sc->sc_oy = y;
689 
690 	wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0, 0,
691 	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
692 
693 	return (0);
694 }
695 
696 /*
697  * access ops.
698  */
699 
700 int
ucbtp_enable(void * v)701 ucbtp_enable(void *v)
702 {
703 	/* not yet */
704 	return (0);
705 }
706 
707 void
ucbtp_disable(void * v)708 ucbtp_disable(void *v)
709 {
710 	/* not yet */
711 }
712 
713 int
ucbtp_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)714 ucbtp_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
715 {
716 	struct ucbtp_softc *sc = v;
717 
718 	DPRINTF(("%s(%d): ucbtp_ioctl(%08lx)\n", __FILE__, __LINE__, cmd));
719 
720 	switch (cmd) {
721 	case WSMOUSEIO_SRES:
722 		printf("%s(%d): WSMOUSRIO_SRES is not supported",
723 		    __FILE__, __LINE__);
724 		break;
725 
726 	default:
727 		return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
728 	}
729 
730 	return 0;
731 }
732