Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*	$NetBSD: psh3tp.c,v 1.17 2021/08/07 16:18:54 thorpej Exp $	*/
/*
 * Copyright (c) 2005 KIYOHARA Takashi
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#include <sys/cdefs.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/callout.h>

#include "opt_psh3tp.h"

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <dev/hpc/hpctpanelvar.h>

#include <machine/platid.h>
#include <machine/platid_mask.h>

#include <machine/intr.h>

#include <sh3/exception.h>
#include <sh3/intcreg.h>
#include <sh3/pfcreg.h>
#include <sh3/adcreg.h>

#include <sh3/dev/adcvar.h>


#ifdef PSH3TP_DEBUG
volatile int psh3tp_debug = 4;
#define DPRINTF_PRINTF		printf_nolog
#define DPRINTF(arg)		if (psh3tp_debug) DPRINTF_PRINTF arg
#define DPRINTFN(n, arg)	if (psh3tp_debug > (n)) DPRINTF_PRINTF arg
#else
#define DPRINTF(arg)		((void)0)
#define DPRINTFN(n, arg)	((void)0)
#endif


/*
 * PFC bits pertinent to PERSONA HPW-50PA touch-panel
 */
#define PHDR_TP_PEN_UP		0x40
#define SCPDR_TP_SCAN_ENABLE	0x20
#define SCPDR_TP_SCAN_DISABLE	0x01
#define SCPDR_TP_SCAN_X		0x06
#define SCPDR_TP_SCAN_Y		0x09

/*
 * A/D converter channels to get x/y from
 */
#define ADC_CHANNEL_TP_X	1
#define ADC_CHANNEL_TP_Y	0

/*
 * Default (read: my device) raw X/Y values for framebuffer edges.
 */
#define PSH3TP_FB_RIGHT		 56
#define PSH3TP_FB_LEFT		969
#define PSH3TP_FB_TOP		848
#define PSH3TP_FB_BOTTOM	121


struct psh3tp_softc {
	device_t sc_dev;

#define PSH3TP_WSMOUSE_ENABLED	0x01
	int sc_enabled;
	struct callout sc_touch_ch;
	device_t sc_wsmousedev;
	struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
};


/* config machinery */
static int psh3tp_match(device_t, struct cfdata *, void *);
static void psh3tp_attach(device_t, device_t, void *);

/* wsmouse accessops */
static int psh3tp_wsmouse_enable(void *);
static int psh3tp_wsmouse_ioctl(void *, u_long, void *, int, struct lwp *);
static void psh3tp_wsmouse_disable(void *);

/* internal driver routines */
static void psh3tp_enable(struct psh3tp_softc *);
static void psh3tp_disable(struct psh3tp_softc *);
static int psh3tp_set_enable(struct psh3tp_softc *, int, int);
static int psh3tp_intr(void *);
static void psh3tp_start_polling(void *);
static void psh3tp_stop_polling(struct psh3tp_softc *);
static void psh3tp_callout_wsmouse(void *);
static void psh3tp_wsmouse_input(struct psh3tp_softc *, int, int);
static void psh3tp_get_raw_xy(int *, int *);


const struct wsmouse_accessops psh3tp_accessops = {
	psh3tp_wsmouse_enable,
	psh3tp_wsmouse_ioctl,
	psh3tp_wsmouse_disable
};

static const struct wsmouse_calibcoords psh3tp_default_calib = {
	0, 0, 639, 239,
	4,
	{{ PSH3TP_FB_LEFT,  PSH3TP_FB_TOP,      0,   0 },
	 { PSH3TP_FB_RIGHT, PSH3TP_FB_TOP,    639,   0 },
	 { PSH3TP_FB_LEFT,  PSH3TP_FB_BOTTOM,   0, 239 },
	 { PSH3TP_FB_RIGHT, PSH3TP_FB_BOTTOM, 639, 239 }}
};


CFATTACH_DECL_NEW(psh3tp, sizeof(struct psh3tp_softc),
    psh3tp_match, psh3tp_attach, NULL, NULL);


/* ARGSUSED */
static int
psh3tp_match(device_t parent __unused, struct cfdata *cf, void *aux __unused)
{

	if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
		return 0;

	if (strcmp(cf->cf_name, "psh3tp") != 0)
		return 0;

	return 1;
}


/*
 * Attach the touch panel driver and its wsmouse child.
 *
 * Note that we have to use submatch to distinguish between child because
 * wsmouse_match matches unconditionally.
 */
/* ARGSUSED */
static void
psh3tp_attach(device_t parent __unused, device_t self, void *aux __unused)
{
	struct psh3tp_softc *sc = device_private(self);
	struct wsmousedev_attach_args wsma;

	aprint_naive("\n");
	aprint_normal("\n");

	sc->sc_dev = self;
	sc->sc_enabled = 0;

	/* touch-panel as a pointing device */
	wsma.accessops = &psh3tp_accessops;
	wsma.accesscookie = sc;

	sc->sc_wsmousedev = config_found(
	    self, &wsma, wsmousedevprint, CFARGS_NONE);
	if (sc->sc_wsmousedev == NULL)
		return;

	/* init calibration, set default parameters */
	tpcalib_init(&sc->sc_tpcalib);
	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
	    (void *)__UNCONST(&psh3tp_default_calib), 0, 0);

	/* used when in polling mode */
	callout_init(&sc->sc_touch_ch, 0);

	/* establish interrupt handler, but disable until opened */
	intc_intr_establish(SH7709_INTEVT2_IRQ2,
	    IST_EDGE, IPL_TTY, psh3tp_intr, sc);
	intc_intr_disable(SH7709_INTEVT2_IRQ2);

 	if (!pmf_device_register(self, NULL, NULL))
 		aprint_error_dev(self, "unable to establish power handler\n");
}


/*
 * Enable touch panel:  we start in interrupt mode.
 * Must be called at spltty().
 */
/* ARGSUSED */
static void
psh3tp_enable(struct psh3tp_softc *sc __unused)
{

	DPRINTFN(2, ("%s: enable\n", device_xname(sc->sc_dev)));
	intc_intr_enable(SH7709_INTEVT2_IRQ2);
}


/*
 * Disable touch panel: disable interrupt, cancel pending callout.
 * Must be called at spltty().
 */
static void
psh3tp_disable(struct psh3tp_softc *sc)
{

	DPRINTFN(2, ("%s: disable\n", device_xname(sc->sc_dev)));
	intc_intr_disable(SH7709_INTEVT2_IRQ2);
	callout_stop(&sc->sc_touch_ch);
}


static int
psh3tp_set_enable(struct psh3tp_softc *sc, int on, int child)
{
	int s = spltty();

	if (on) {
		if (!sc->sc_enabled)
			psh3tp_enable(sc);
		sc->sc_enabled |= child;
	} else {
		sc->sc_enabled &= ~child;
		if (!sc->sc_enabled)
			psh3tp_disable(sc);
	}

	splx(s);
	return 0;
}


static int
psh3tp_wsmouse_enable(void *cookie)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;

	DPRINTFN(1, ("%s: wsmouse enable\n", device_xname(sc->sc_dev)));
	return psh3tp_set_enable(sc, 1, PSH3TP_WSMOUSE_ENABLED);
}


static void
psh3tp_wsmouse_disable(void *cookie)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;

	DPRINTFN(1, ("%s: wsmouse disable\n", device_xname(sc->sc_dev)));
	psh3tp_set_enable(sc, 0, PSH3TP_WSMOUSE_ENABLED);
}


static int
psh3tp_intr(void *arg)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;

	uint8_t irr0;
	uint8_t phdr, touched;
	unsigned int steady, tremor_timeout;

	irr0 = _reg_read_1(SH7709_IRR0);
	if ((irr0 & IRR0_IRQ2) == 0) {
#ifdef DIAGNOSTIC
		printf("%s: irr0 %02x?\n", device_xname(sc->sc_dev), irr0);
#endif
		return 0;
	}

	if (!sc->sc_enabled) {
		DPRINTFN(1, ("%s: intr: !sc_enabled\n", device_xname(sc->sc_dev)));
		intc_intr_disable(SH7709_INTEVT2_IRQ2);
		goto served;
	}

	/*
	 * Number of times the "touched" bit should be read
	 * consecutively.
	 */
#define TREMOR_THRESHOLD 0x300
	steady = 0;
	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
	touched = true;		/* we start with "touched" state */

	do {
		uint8_t state;

		phdr = _reg_read_1(SH7709_PHDR);
		state = ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP);

		if (state == touched)
			++steady;
		else {
			steady = 0;
			touched = state;
		}

		if (--tremor_timeout == 0) {
			DPRINTF(("%s: tremor timeout!\n",
			    device_xname(sc->sc_dev)));
			goto served;
		}
	} while (steady < TREMOR_THRESHOLD);

	if (touched) {
		intc_intr_disable(SH7709_INTEVT2_IRQ2);

		/*
		 * ADC readings are not stable yet, so schedule
		 * callout instead of accessing ADC from the interrupt
		 * handler only to immediately delay().
		 */
		callout_reset(&sc->sc_touch_ch,
		    hz/32, psh3tp_start_polling, sc);
	} else
		DPRINTFN(1, ("%s: tremor\n", device_xname(sc->sc_dev)));
served:
	/* clear the interrupt */
	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);

	return 1;
}


/*
 * Called from the interrupt handler at spltty() upon first touch.
 * Decide if we are going to report this touch as a mouse click/drag.
 */
static void
psh3tp_start_polling(void *arg)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
	uint8_t phdr;
	int rawx, rawy;

	phdr = _reg_read_1(SH7709_PHDR);
	if ((phdr & PHDR_TP_PEN_UP) == PHDR_TP_PEN_UP) {
		DPRINTFN(2, ("%s: start: pen is not down\n",
		    device_xname(sc->sc_dev)));
		psh3tp_stop_polling(sc);
		return;
	}

	psh3tp_get_raw_xy(&rawx, &rawy);
	DPRINTFN(2, ("%s: start: %4d %4d -> ",
	    device_xname(sc->sc_dev), rawx, rawy));

	if (sc->sc_enabled & PSH3TP_WSMOUSE_ENABLED) {
		DPRINTFN(2, ("mouse\n"));
		psh3tp_wsmouse_input(sc, rawx, rawy);
		callout_reset(&sc->sc_touch_ch,
		    hz/32, psh3tp_callout_wsmouse, sc);
	} else {
		DPRINTFN(2, ("ignore\n"));
		psh3tp_stop_polling(sc);
	}
}


/*
 * Re-enable touch panel interrupt.
 * Called at spltty() when polling code detects pen-up.
 */
/* ARGSUSED */
static void
psh3tp_stop_polling(struct psh3tp_softc *sc __unused)
{
	uint8_t irr0;

	DPRINTFN(2, ("%s: stop\n", device_xname(sc->sc_dev)));

	/* clear pending interrupt signal before re-enabling the interrupt */
	irr0 = _reg_read_1(SH7709_IRR0);
	if ((irr0 & IRR0_IRQ2) != 0)
		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ2);

	intc_intr_enable(SH7709_INTEVT2_IRQ2);
}


/*
 * We are reporting this touch as a mouse click/drag.
 */
static void
psh3tp_callout_wsmouse(void *arg)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)arg;
	uint8_t phdr;
	int rawx, rawy;
	int s;

	s = spltty();

	if (!sc->sc_enabled) {
		DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
		    device_xname(sc->sc_dev)));
		splx(s);
		return;
	}

	phdr = _reg_read_1(SH7709_PHDR);
	if ((phdr & PHDR_TP_PEN_UP) != PHDR_TP_PEN_UP) {
		psh3tp_get_raw_xy(&rawx, &rawy);
		psh3tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
		callout_schedule(&sc->sc_touch_ch, hz/32);
	} else {
		wsmouse_input( /* button up */
		    sc->sc_wsmousedev, 0, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
		psh3tp_stop_polling(sc);
	}
	splx(s);
}


/*
 * Report mouse click/drag.
 */
static void
psh3tp_wsmouse_input(struct psh3tp_softc *sc, int rawx, int rawy)
{
	int x, y;

	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
		
	DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
	     device_xname(sc->sc_dev), rawx, rawy, x, y));

	wsmouse_input(sc->sc_wsmousedev,
	    1,	/* button */
	    x, y, 0, 0,
	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
}


/*
 * Read raw X/Y coordinates from the ADC.
 */
static void
psh3tp_get_raw_xy(int *rawxp, int *rawyp)
{
	uint8_t scpdr;

	/* X axis */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr &= ~SCPDR_TP_SCAN_DISABLE;
	scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_X);
	_reg_write_1(SH7709_SCPDR, scpdr);
	delay(40);

	*rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);

	/* Y axis */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr &=  ~SCPDR_TP_SCAN_X;
	scpdr |= (SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
	_reg_write_1(SH7709_SCPDR, scpdr);
	delay(40);

	*rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);

	/* restore SCPDR */
	scpdr = _reg_read_1(SH7709_SCPDR);
	scpdr &= ~(SCPDR_TP_SCAN_ENABLE | SCPDR_TP_SCAN_Y);
	scpdr |= SCPDR_TP_SCAN_DISABLE;
	_reg_write_1(SH7709_SCPDR, scpdr);
}


static int
psh3tp_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
		     struct lwp *l)
{
	struct psh3tp_softc *sc = (struct psh3tp_softc *)cookie;

	return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
}