Home | History | Annotate | Line # | Download | only in spi
oj6sh.c revision 1.8.2.1
      1 /*	$NetBSD: oj6sh.c,v 1.8.2.1 2021/05/19 03:46:26 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
      5  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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  *
     16  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Sharp NetWalker's Optical Joystick
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.8.2.1 2021/05/19 03:46:26 thorpej Exp $");
     35 
     36 #include "opt_oj6sh.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/lock.h>
     43 #include <sys/callout.h>
     44 #include <sys/bus.h>
     45 #include <sys/mutex.h>
     46 #include <sys/workqueue.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/wsmousevar.h>
     50 #include <dev/wscons/wsdisplayvar.h>
     51 
     52 #include <dev/hpc/hpcfbio.h>
     53 #include <dev/hpc/hpctpanelvar.h>
     54 
     55 #include <dev/spi/spivar.h>
     56 
     57 #ifdef OJ6SH_DEBUG
     58 int oj6sh_debug = OJ6SH_DEBUG;
     59 #define DPRINTF(n,x)	if (oj6sh_debug>(n)) printf x;
     60 #else
     61 #define DPRINTF(n,x)
     62 #endif
     63 
     64 #define POLLRATE (hz/30)
     65 
     66 /* register address */
     67 #define OJ6SH_PRODUCT		0x00
     68 #define OJ6SH_REVISION		0x01
     69 #define OJ6SH_MOTION		0x02
     70 #define OJ6SH_DELTA_X		0x03
     71 #define OJ6SH_DELTA_Y		0x04
     72 #define OJ6SH_SQUAL		0x05
     73 #define OJ6SH_SHUTTER		0x06
     74 #define OJ6SH_CONFIG		0x11
     75 #define OJ6SH_RESET		0x3a
     76 #define  POWERON_RESET		0x5a
     77 #define OJ6SH_N_REVISION	0x3e
     78 #define OJ6SH_N_PRODUCT		0x3f
     79 
     80 struct oj6sh_softc {
     81 	device_t sc_dev;
     82 
     83 	struct spi_handle *sc_sh;
     84 	struct callout sc_c;
     85 
     86 	kmutex_t sc_lock;
     87 	struct workqueue *sc_wq;
     88 	struct work sc_wk;
     89 
     90 	int sc_enabled;
     91 
     92 	device_t sc_wsmousedev;
     93 };
     94 
     95 struct oj6sh_delta {
     96 	int x;
     97 	int y;
     98 };
     99 
    100 static uint8_t oj6sh_read(struct spi_handle *, uint8_t);
    101 static void oj6sh_write(struct spi_handle *, uint8_t, uint8_t);
    102 
    103 static int oj6sh_match(device_t , cfdata_t , void *);
    104 static void oj6sh_attach(device_t , device_t , void *);
    105 
    106 CFATTACH_DECL_NEW(oj6sh, sizeof(struct oj6sh_softc),
    107     oj6sh_match, oj6sh_attach, NULL, NULL);
    108 
    109 static bool oj6sh_motion(struct spi_handle *);
    110 static bool oj6sh_squal(struct spi_handle *);
    111 static bool oj6sh_shuttrer(struct spi_handle *);
    112 static int oj6sh_readdelta(struct spi_handle *, struct oj6sh_delta *);
    113 
    114 static void oj6sh_poll(void *);
    115 static void oj6sh_cb(struct work *, void *);
    116 static int oj6sh_enable(void *);
    117 static void oj6sh_disable(void *);
    118 static int oj6sh_ioctl(void *, u_long, void *, int, struct lwp *);
    119 
    120 static bool oj6sh_resume(device_t, const pmf_qual_t *);
    121 static bool oj6sh_suspend(device_t, const pmf_qual_t *);
    122 
    123 static const struct wsmouse_accessops oj6sh_accessops = {
    124 	.enable = oj6sh_enable,
    125 	.ioctl = oj6sh_ioctl,
    126 	.disable = oj6sh_disable
    127 };
    128 
    129 static const struct device_compatible_entry compat_data[] = {
    130 	{ .compat = "oj6sh" },
    131 	DEVICE_COMPAT_EOL
    132 };
    133 
    134 static int
    135 oj6sh_match(device_t parent, cfdata_t cf, void *aux)
    136 {
    137 	struct spi_attach_args *sa = aux;
    138 
    139 	if (spi_compatible_match(sa, cf, compat_data) == 0)
    140 		return 0;
    141 
    142 	return 2;
    143 }
    144 
    145 static void
    146 oj6sh_doattach(device_t self)
    147 {
    148 	struct oj6sh_softc *sc = device_private(self);
    149 	uint8_t product;
    150 	uint8_t rev;
    151 	uint8_t product_inv;
    152 	uint8_t rev_inv;
    153 
    154 	/* reset */
    155 	oj6sh_write(sc->sc_sh, OJ6SH_RESET, POWERON_RESET);
    156 	delay(10000);
    157 
    158 	/* resolution */
    159 	oj6sh_write(sc->sc_sh, OJ6SH_CONFIG, 0x80);
    160 
    161 	product = oj6sh_read(sc->sc_sh, OJ6SH_PRODUCT);
    162 	rev = oj6sh_read(sc->sc_sh, OJ6SH_REVISION);
    163 	product_inv = oj6sh_read(sc->sc_sh, OJ6SH_N_PRODUCT);
    164 	rev_inv = oj6sh_read(sc->sc_sh, OJ6SH_N_REVISION);
    165 
    166 	if (((product | product_inv) != 0xff) || ((rev | rev_inv) != 0xff)) {
    167 		aprint_error_dev(self,
    168 		    "mismatch product (%02x:%02x), rev (%02x:%02x)\n",
    169 		    product, product_inv, rev, rev_inv);
    170 		return;
    171 	}
    172 
    173 	aprint_normal("%s: id 0x%02x, revision 0x%02x\n",
    174 	    device_xname(sc->sc_dev), product, rev);
    175 
    176 	return;
    177 }
    178 
    179 static void
    180 oj6sh_attach(device_t parent, device_t self, void *aux)
    181 {
    182 	struct oj6sh_softc *sc = device_private(self);
    183 	struct spi_attach_args *sa = aux;
    184 	struct wsmousedev_attach_args a;
    185 	int errir;
    186 
    187 	aprint_naive("\n");
    188 	aprint_normal(": OJ6SH-T25 Optical Joystick\n");
    189 
    190 	error = spi_configure(sa->sa_handle, SPI_MODE_0, 2500000);
    191 	if (error) {
    192 		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
    193 		    error);
    194 		return;
    195 	}
    196 
    197 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    198 
    199 	sc->sc_dev = self;
    200 	sc->sc_enabled = 0;
    201 
    202 	callout_init(&sc->sc_c, 0);
    203 	workqueue_create(&sc->sc_wq, "oj6sh",
    204 	    oj6sh_cb, sc, PRI_NONE, IPL_BIO, 0);
    205 
    206 	sc->sc_sh = sa->sa_handle;
    207 
    208 	a.accessops = &oj6sh_accessops;
    209 	a.accesscookie = sc;
    210 
    211 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARG_EOL);
    212 
    213 	config_interrupts(self, oj6sh_doattach);
    214 }
    215 
    216 static void
    217 oj6sh_poll(void *arg)
    218 {
    219 	struct oj6sh_softc *sc = (struct oj6sh_softc *)arg;
    220 	workqueue_enqueue(sc->sc_wq, &sc->sc_wk, NULL);
    221 }
    222 
    223 static void
    224 oj6sh_cb(struct work *wk, void *arg)
    225 {
    226 	struct oj6sh_softc *sc = (struct oj6sh_softc *)arg;
    227 	struct oj6sh_delta delta = {0, 0};
    228 	uint32_t buttons = 0;
    229 	int s;
    230 	int x, y;
    231 
    232 	mutex_enter(&sc->sc_lock);
    233 
    234 	if (oj6sh_motion(sc->sc_sh) == false)
    235 		goto out;
    236 	else if ((oj6sh_squal(sc->sc_sh) == true) &&
    237 	    (oj6sh_shuttrer(sc->sc_sh) == true))
    238 		goto out;
    239 
    240 	oj6sh_readdelta(sc->sc_sh, &delta);
    241 	DPRINTF(3,("%s: x = %d, y = %d\n", device_xname(sc->sc_dev),
    242 		delta.x, delta.y));
    243 
    244 #if defined(OJ6SH_DOWN_Y_LEFT_X)
    245 	y = -delta.y;
    246 	x = -delta.x;
    247 #elif defined(OJ6SH_UP_X_LEFT_Y)
    248 	y = delta.x;
    249 	x = -delta.y;
    250 #elif defined(OJ6SH_DOWN_X_RIGHT_Y)
    251 	y = -delta.x;
    252 	x = delta.y;
    253 #else /* OJ6SH_UP_Y_RIGHT_X */
    254 	y = delta.y;
    255 	x = delta.x;
    256 #endif
    257 	s = spltty();
    258 	wsmouse_input(sc->sc_wsmousedev, buttons, x, y, 0, 0,
    259 	    WSMOUSE_INPUT_DELTA);
    260 	splx(s);
    261 out:
    262 	mutex_exit(&sc->sc_lock);
    263 
    264 	if (sc->sc_enabled)
    265 		callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
    266 }
    267 
    268 static uint8_t
    269 oj6sh_read(struct spi_handle *spi, uint8_t reg)
    270 {
    271 	uint8_t ret = 0;
    272 
    273 	spi_send_recv(spi, 1, &reg, 1, &ret);
    274 	DPRINTF(4,("%s: 0x%02x = 0x%02x\n", __func__, reg, ret));
    275 	return ret;
    276 }
    277 
    278 static void
    279 oj6sh_write(struct spi_handle *spi, uint8_t reg, uint8_t val)
    280 {
    281 	uint8_t tmp[2] = {reg | 0x80, val};
    282 
    283 	spi_send(spi, 2, tmp);
    284 	DPRINTF(4,("%s: 0x%02x = 0x%02x\n", __func__, reg, val));
    285 	return;
    286 }
    287 
    288 static bool
    289 oj6sh_motion(struct spi_handle *spi)
    290 {
    291 	uint16_t motion;
    292 	motion = oj6sh_read(spi, OJ6SH_MOTION);
    293 	return (motion & __BIT(7) ? true : false);
    294 }
    295 
    296 static bool
    297 oj6sh_squal(struct spi_handle *spi)
    298 {
    299 	uint16_t squal;
    300 	squal = oj6sh_read(spi, OJ6SH_SQUAL);
    301 	return (squal < 25 ? true : false);
    302 }
    303 
    304 static bool
    305 oj6sh_shuttrer(struct spi_handle *spi)
    306 {
    307 	uint16_t shutter;
    308 	shutter = oj6sh_read(spi, OJ6SH_SHUTTER) << 8;
    309 	shutter |= oj6sh_read(spi, OJ6SH_SHUTTER + 1);
    310 	return (shutter > 600 ? true : false);
    311 }
    312 
    313 static int
    314 oj6sh_readdelta(struct spi_handle *spi, struct oj6sh_delta *delta)
    315 {
    316 	delta->x = (int8_t)oj6sh_read(spi, OJ6SH_DELTA_X);
    317 	delta->y = (int8_t)oj6sh_read(spi, OJ6SH_DELTA_Y);
    318 	return 0;
    319 }
    320 
    321 int
    322 oj6sh_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    323 {
    324 	struct wsmouse_id *id;
    325 
    326 	switch (cmd) {
    327 	case WSMOUSEIO_GTYPE:
    328 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    329 		return 0;
    330 	case WSMOUSEIO_GETID:
    331 		id = (struct wsmouse_id *)data;
    332 		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
    333 			return EINVAL;
    334 		strlcpy(id->data, "OJ6SH-T25", WSMOUSE_ID_MAXLEN);
    335 		id->length = strlen(id->data);
    336 		return 0;
    337 	}
    338 
    339 	return EPASSTHROUGH;
    340 }
    341 
    342 int
    343 oj6sh_enable(void *v)
    344 {
    345 	struct oj6sh_softc *sc = (struct oj6sh_softc *)v;
    346 
    347 	DPRINTF(3,("%s: oj6sh_enable()\n", device_xname(sc->sc_dev)));
    348 	if (sc->sc_enabled) {
    349 		DPRINTF(3,("%s: already enabled\n", device_xname(sc->sc_dev)));
    350 		return EBUSY;
    351 	}
    352 
    353 	if (!pmf_device_register(sc->sc_dev, oj6sh_suspend, oj6sh_resume))
    354 		aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
    355 
    356 	sc->sc_enabled = 1;
    357 	callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
    358 
    359 	return 0;
    360 }
    361 
    362 void
    363 oj6sh_disable(void *v)
    364 {
    365 	struct oj6sh_softc *sc = (struct oj6sh_softc *)v;
    366 
    367 	DPRINTF(3,("%s: oj6sh_disable()\n", device_xname(sc->sc_dev)));
    368 	if (!sc->sc_enabled) {
    369 		DPRINTF(3,("%s: already disabled()\n", device_xname(sc->sc_dev)));
    370 		return;
    371 	}
    372 
    373 	pmf_device_deregister(sc->sc_dev);
    374 
    375 	sc->sc_enabled = 0;
    376 
    377 	return;
    378 }
    379 
    380 static bool
    381 oj6sh_suspend(device_t dv, const pmf_qual_t *qual)
    382 {
    383 	struct oj6sh_softc *sc = device_private(dv);
    384 
    385 	DPRINTF(3,("%s: oj6sh_suspend()\n", device_xname(sc->sc_dev)));
    386 	callout_stop(&sc->sc_c);
    387 	sc->sc_enabled = 0;
    388 
    389 	return true;
    390 }
    391 
    392 static bool
    393 oj6sh_resume(device_t dv, const pmf_qual_t *qual)
    394 {
    395 	struct oj6sh_softc *sc = device_private(dv);
    396 
    397 	DPRINTF(3,("%s: oj6sh_resume()\n", device_xname(sc->sc_dev)));
    398 	sc->sc_enabled = 1;
    399 	callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
    400 
    401 	return true;
    402 }
    403 
    404