#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <arm/s3c2xx0/s3c24x0var.h>
#include <arm/s3c2xx0/s3c2440var.h>
#include <arm/s3c2xx0/s3c2440reg.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <dev/wscons/tpcalibvar.h>
#include <lib/libsa/qsort.c>
#include <dev/hpc/hpcfbio.h>
#define MAX_SAMPLES 20
struct sstouch_softc {
device_t dev;
bus_space_tag_t iot;
bus_space_handle_t ioh;
uint32_t next_stylus_intr;
device_t wsmousedev;
struct tpcalib_softc tpcalib;
int sample_count;
int samples_x[MAX_SAMPLES];
int samples_y[MAX_SAMPLES];
callout_t callout;
};
static int sstouch_match (device_t, cfdata_t, void *);
static void sstouch_attach (device_t, device_t, void *);
CFATTACH_DECL_NEW(sstouch, sizeof(struct sstouch_softc), sstouch_match,
sstouch_attach, NULL, NULL);
int sstouch_enable(void *);
int sstouch_ioctl(void *, u_long, void *, int, struct lwp *);
void sstouch_disable(void *);
const struct wsmouse_accessops sstouch_accessops = {
sstouch_enable,
sstouch_ioctl,
sstouch_disable
};
int sstouch_tc_intr(void *arg);
int sstouch_adc_intr(void *arg);
void sstouch_callout(void *arg);
int sstouch_filter_values(int *vals, int val_count);
void sstouch_initialize(struct sstouch_softc *sc);
#define STYLUS_DOWN 0
#define STYLUS_UP ADCTSC_UD_SEN
static struct wsmouse_calibcoords default_calib = {
.minx = 0,
.miny = 0,
.maxx = 0,
.maxy = 0,
.samplelen = WSMOUSE_CALIBCOORDS_RESET
};
int
sstouch_match(device_t parent, cfdata_t match, void *aux)
{
return 1;
}
void
sstouch_attach(device_t parent, device_t self, void *aux)
{
struct sstouch_softc *sc = device_private(self);
struct s3c2xx0_attach_args *sa = aux;
struct wsmousedev_attach_args mas;
sc->dev = self;
sc->iot = sa->sa_iot;
if (bus_space_map(sc->iot, S3C2440_ADC_BASE,
S3C2440_ADC_SIZE, 0, &sc->ioh)) {
aprint_error(": failed to map registers");
return;
}
sc->next_stylus_intr = STYLUS_DOWN;
s3c24x0_intr_establish(S3C2440_INT_TC, IPL_BIO, IST_EDGE_RISING,
sstouch_tc_intr, sc);
s3c24x0_intr_establish(S3C2440_INT_ADC, IPL_BIO, IST_EDGE_RISING,
sstouch_adc_intr, sc);
aprint_normal("\n");
mas.accessops = &sstouch_accessops;
mas.accesscookie = sc;
sc->wsmousedev = config_found(self, &mas, wsmousedevprint, CFARGS_NONE);
tpcalib_init(&sc->tpcalib);
tpcalib_ioctl(&sc->tpcalib, WSMOUSEIO_SCALIBCOORDS,
(void*)&default_calib, 0, 0);
sc->sample_count = 0;
callout_init(&sc->callout, 0);
callout_setfunc(&sc->callout, sstouch_callout, sc);
}
int
sstouch_tc_intr(void *arg)
{
struct sstouch_softc *sc = (struct sstouch_softc*)arg;
uint32_t reg;
reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCUPDN);
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCUPDN, 0x0);
if( sc->next_stylus_intr == STYLUS_DOWN && (reg & ADCUPDN_TSC_DN) ) {
sc->next_stylus_intr = STYLUS_UP;
sstouch_callout(sc);
} else if (sc->next_stylus_intr == STYLUS_UP && (reg & ADCUPDN_TSC_UP)) {
uint32_t adctsc = 0;
sc->next_stylus_intr = STYLUS_DOWN;
wsmouse_input(sc->wsmousedev, 0x0, 0, 0, 0, 0, 0);
sc->sample_count = 0;
adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
sc->next_stylus_intr |
3;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
}
return 1;
}
int
sstouch_adc_intr(void *arg)
{
struct sstouch_softc *sc = (struct sstouch_softc*)arg;
uint32_t reg;
uint32_t adctsc = 0;
int x, y;
reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCDAT0);
y = reg & ADCDAT_DATAMASK;
reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCDAT1);
x = reg & ADCDAT_DATAMASK;
sc->samples_x[sc->sample_count] = x;
sc->samples_y[sc->sample_count] = y;
sc->sample_count++;
x = sstouch_filter_values(sc->samples_x, sc->sample_count);
y = sstouch_filter_values(sc->samples_y, sc->sample_count);
if (x == -1 || y == -1) {
sstouch_callout(sc);
return 1;
}
sc->sample_count = 0;
tpcalib_trans(&sc->tpcalib, x, y, &x, &y);
wsmouse_input(sc->wsmousedev, 0x1, x, y, 0, 0,
WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
if (sc->next_stylus_intr == STYLUS_UP) {
callout_schedule(&sc->callout, hz/50);
}
adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
sc->next_stylus_intr |
3;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
return 1;
}
int
sstouch_enable(void *arg)
{
struct sstouch_softc *sc = arg;
sstouch_initialize(sc);
return 0;
}
int
sstouch_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct sstouch_softc *sc = v;
aprint_normal("%s\n", __func__);
switch (cmd) {
case WSMOUSEIO_GTYPE:
*(uint *)data = WSMOUSE_TYPE_PSEUDO;
break;
case WSMOUSEIO_GCALIBCOORDS:
case WSMOUSEIO_SCALIBCOORDS:
return tpcalib_ioctl(&sc->tpcalib, cmd, data, flag, l);
default:
return EPASSTHROUGH;
}
return 0;
}
void
sstouch_disable(void *arg)
{
struct sstouch_softc *sc = (struct sstouch_softc*)arg;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON, 0);
}
void
sstouch_callout(void *arg)
{
struct sstouch_softc *sc = (struct sstouch_softc*)arg;
if (sc->next_stylus_intr == STYLUS_UP) {
uint32_t reg;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC,
ADCTSC_YM_SEN | ADCTSC_YP_SEN |
ADCTSC_XP_SEN | ADCTSC_PULL_UP |
ADCTSC_AUTO_PST);
reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCCON);
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON,
reg | ADCCON_ENABLE_START);
}
}
int
sstouch_filter_values(int *vals, int val_count)
{
int sum = 0;
if (val_count < 5)
return -1;
for (int i=0; i<val_count; i++) {
sum += vals[i];
}
return sum/val_count;
}
void
sstouch_initialize(struct sstouch_softc *sc)
{
int prescaler;
uint32_t adccon = 0;
uint32_t adctsc = 0;
prescaler = 0xff;
adccon |= ((prescaler<<ADCCON_PRSCVL_SHIFT) &
ADCCON_PRSCVL_MASK);
adccon |= ADCCON_PRSCEN;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON, adccon);
adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
sc->next_stylus_intr |
3;
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCUPDN, 0x0);
bus_space_write_4(sc->iot, sc->ioh, ADC_ADCDLY, 10000);
}