Home | History | Annotate | Line # | Download | only in fdt
      1  1.11   thorpej /* $NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.11   thorpej __KERNEL_RCSID(0, "$NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/kernel.h>
     34   1.1  jmcneill #include <sys/systm.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/kmem.h>
     37   1.1  jmcneill #include <sys/bus.h>
     38   1.1  jmcneill #include <sys/gpio.h>
     39   1.1  jmcneill 
     40   1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     41   1.1  jmcneill #include <dev/sysmon/sysmon_taskq.h>
     42   1.1  jmcneill 
     43   1.5  jmcneill #include <dev/wscons/wsconsio.h>
     44   1.5  jmcneill #include <dev/wscons/wskbdvar.h>
     45   1.5  jmcneill #include <dev/wscons/wsksymdef.h>
     46   1.5  jmcneill #include <dev/wscons/wsksymvar.h>
     47   1.5  jmcneill #include <dev/wscons/linux_keymap.h>
     48   1.5  jmcneill 
     49   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     50   1.1  jmcneill 
     51   1.1  jmcneill #define GPIOKEYS_POLL_INTERVAL	mstohz(200)
     52   1.1  jmcneill 
     53   1.7  jmcneill /* Event types */
     54   1.7  jmcneill #define	EV_KEY		1
     55   1.7  jmcneill #define	EV_SW		5
     56   1.7  jmcneill 
     57   1.7  jmcneill /* Key and button events */
     58   1.7  jmcneill #define	KEY_POWER	116
     59   1.7  jmcneill #define	KEY_SLEEP	142
     60   1.7  jmcneill 
     61   1.7  jmcneill /* Switch events */
     62   1.7  jmcneill #define	SW_LID		0
     63   1.1  jmcneill 
     64   1.1  jmcneill static int	gpiokeys_match(device_t, cfdata_t, void *);
     65   1.1  jmcneill static void	gpiokeys_attach(device_t, device_t, void *);
     66   1.1  jmcneill 
     67   1.1  jmcneill static void	gpiokeys_tick(void *);
     68   1.1  jmcneill static void	gpiokeys_task(void *);
     69   1.1  jmcneill 
     70   1.6    bouyer extern const struct wscons_keydesc hidkbd_keydesctab[];
     71   1.5  jmcneill static const struct wskbd_mapdata gpiokeys_keymapdata = {
     72   1.6    bouyer 	hidkbd_keydesctab,
     73   1.5  jmcneill 	KB_US,
     74   1.5  jmcneill };
     75   1.5  jmcneill 
     76   1.1  jmcneill struct gpiokeys_softc;
     77   1.1  jmcneill 
     78   1.1  jmcneill struct gpiokeys_key {
     79   1.5  jmcneill 	struct gpiokeys_softc	*key_sc;
     80   1.1  jmcneill 	int			key_phandle;
     81   1.1  jmcneill 	char	 		*key_label;
     82   1.1  jmcneill 	struct fdtbus_gpio_pin	*key_pin;
     83   1.1  jmcneill 	u_int			key_debounce;
     84   1.1  jmcneill 	u_int			key_code;
     85   1.1  jmcneill 	struct sysmon_pswitch	key_pswitch;
     86   1.5  jmcneill 	uint8_t			key_usbcode;
     87   1.8  jmcneill 	int			key_state;
     88   1.1  jmcneill 
     89   1.1  jmcneill 	struct gpiokeys_key	*key_next;
     90   1.1  jmcneill };
     91   1.1  jmcneill 
     92   1.1  jmcneill struct gpiokeys_softc {
     93   1.1  jmcneill 	device_t	sc_dev;
     94   1.1  jmcneill 	int		sc_phandle;
     95   1.1  jmcneill 
     96   1.1  jmcneill 	struct fdtbus_gpio_pin *sc_pin;
     97   1.1  jmcneill 	bool		sc_always_on;
     98   1.1  jmcneill 	bool		sc_enable_val;
     99   1.1  jmcneill 
    100   1.1  jmcneill 	struct gpiokeys_key *sc_keys;
    101   1.1  jmcneill 	callout_t	sc_tick;
    102   1.5  jmcneill 
    103   1.5  jmcneill 	device_t	sc_wskbddev;
    104   1.5  jmcneill 	int		sc_enabled;
    105   1.1  jmcneill };
    106   1.1  jmcneill 
    107   1.1  jmcneill CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
    108   1.1  jmcneill     gpiokeys_match, gpiokeys_attach, NULL, NULL);
    109   1.1  jmcneill 
    110   1.1  jmcneill static int
    111   1.5  jmcneill gpiokeys_enable(void *v, int on)
    112   1.5  jmcneill {
    113   1.5  jmcneill 	struct gpiokeys_softc * const sc = v;
    114   1.5  jmcneill 
    115   1.5  jmcneill 	sc->sc_enabled = on;
    116   1.5  jmcneill 
    117   1.5  jmcneill 	return 0;
    118   1.5  jmcneill }
    119   1.5  jmcneill 
    120   1.5  jmcneill static void
    121   1.5  jmcneill gpiokeys_set_leds(void *v, int leds)
    122   1.5  jmcneill {
    123   1.5  jmcneill }
    124   1.5  jmcneill 
    125   1.5  jmcneill static int
    126   1.5  jmcneill gpiokeys_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
    127   1.5  jmcneill {
    128   1.5  jmcneill 	switch (cmd) {
    129   1.5  jmcneill 	case WSKBDIO_GTYPE:
    130   1.5  jmcneill 		*(int *)data = WSKBD_TYPE_USB;
    131   1.5  jmcneill 		return 0;
    132   1.5  jmcneill 	}
    133   1.5  jmcneill 
    134   1.5  jmcneill 	return EPASSTHROUGH;
    135   1.5  jmcneill }
    136   1.5  jmcneill 
    137   1.5  jmcneill static const struct wskbd_accessops gpiokeys_accessops = {
    138   1.5  jmcneill 	.enable = gpiokeys_enable,
    139   1.5  jmcneill 	.set_leds = gpiokeys_set_leds,
    140   1.5  jmcneill 	.ioctl = gpiokeys_ioctl
    141   1.5  jmcneill };
    142   1.5  jmcneill 
    143   1.9   thorpej static const struct device_compatible_entry compat_data[] = {
    144   1.9   thorpej 	{ .compat = "gpio-keys" },
    145   1.9   thorpej 	DEVICE_COMPAT_EOL
    146   1.9   thorpej };
    147   1.9   thorpej 
    148   1.5  jmcneill static int
    149   1.1  jmcneill gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
    150   1.1  jmcneill {
    151   1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    152   1.1  jmcneill 
    153   1.9   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    154   1.1  jmcneill }
    155   1.1  jmcneill 
    156   1.1  jmcneill static void
    157   1.1  jmcneill gpiokeys_attach(device_t parent, device_t self, void *aux)
    158   1.1  jmcneill {
    159   1.1  jmcneill 	struct gpiokeys_softc * const sc = device_private(self);
    160   1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
    161   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    162   1.1  jmcneill 	struct gpiokeys_key *key;
    163   1.7  jmcneill 	u_int debounce, input_type, code;
    164   1.5  jmcneill 	int use_wskbddev = 0;
    165   1.1  jmcneill 	int child, len;
    166   1.1  jmcneill 
    167   1.1  jmcneill 	sc->sc_dev = self;
    168   1.1  jmcneill 	sc->sc_phandle = phandle;
    169   1.1  jmcneill 
    170   1.1  jmcneill 	aprint_naive("\n");
    171   1.1  jmcneill 	aprint_normal(":");
    172   1.1  jmcneill 
    173   1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    174   1.7  jmcneill 		if (of_getprop_uint32(child, "linux,input-type", &input_type))
    175   1.7  jmcneill 			input_type = EV_KEY;	/* default */
    176   1.2  jmcneill 		if (of_getprop_uint32(child, "linux,code", &code))
    177   1.1  jmcneill 			continue;
    178   1.2  jmcneill 		if (of_getprop_uint32(child, "debounce-interval", &debounce))
    179   1.2  jmcneill 			debounce = 5;	/* default */
    180   1.1  jmcneill 		len = OF_getproplen(child, "label");
    181   1.1  jmcneill 		if (len <= 0) {
    182   1.1  jmcneill 			continue;
    183   1.1  jmcneill 		}
    184   1.1  jmcneill 		key = kmem_zalloc(sizeof(*key), KM_SLEEP);
    185   1.5  jmcneill 		key->key_sc = sc;
    186   1.1  jmcneill 		key->key_phandle = child;
    187   1.1  jmcneill 		key->key_code = code;
    188   1.1  jmcneill 		key->key_label = kmem_zalloc(len, KM_SLEEP);
    189   1.1  jmcneill 		if (OF_getprop(child, "label", key->key_label, len) != len) {
    190   1.1  jmcneill 			kmem_free(key->key_label, len);
    191   1.1  jmcneill 			kmem_free(key, sizeof(*key));
    192   1.1  jmcneill 			continue;
    193   1.1  jmcneill 		}
    194   1.2  jmcneill 		key->key_debounce = debounce;
    195   1.1  jmcneill 		key->key_pin = fdtbus_gpio_acquire(child, "gpios",
    196   1.1  jmcneill 		    GPIO_PIN_INPUT);
    197   1.4  jmcneill 		if (key->key_pin)
    198   1.4  jmcneill 			key->key_state = fdtbus_gpio_read(key->key_pin);
    199   1.1  jmcneill 
    200   1.7  jmcneill 		switch (input_type) {
    201   1.7  jmcneill 		case EV_KEY:
    202   1.7  jmcneill 			switch (code) {
    203   1.7  jmcneill 			case KEY_POWER:
    204   1.7  jmcneill 				key->key_pswitch.smpsw_name = key->key_label;
    205   1.7  jmcneill 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
    206   1.7  jmcneill 				break;
    207   1.7  jmcneill 			case KEY_SLEEP:
    208   1.7  jmcneill 				key->key_pswitch.smpsw_name = key->key_label;
    209   1.7  jmcneill 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
    210   1.7  jmcneill 				break;
    211   1.7  jmcneill 			default:
    212   1.7  jmcneill 				key->key_usbcode = linux_key_to_usb(code);
    213   1.7  jmcneill 				if (key->key_usbcode != 0) {
    214   1.7  jmcneill 					use_wskbddev++;
    215   1.7  jmcneill 				} else {
    216   1.7  jmcneill 					key->key_pswitch.smpsw_name = key->key_label;
    217   1.7  jmcneill 					key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
    218   1.7  jmcneill 				}
    219   1.7  jmcneill 				break;
    220   1.7  jmcneill 			}
    221   1.1  jmcneill 			break;
    222   1.7  jmcneill 		case EV_SW:
    223   1.5  jmcneill 			key->key_pswitch.smpsw_name = key->key_label;
    224   1.7  jmcneill 			switch (code) {
    225   1.7  jmcneill 			case SW_LID:
    226   1.8  jmcneill 				key->key_state = -1;	/* Send notification on attach */
    227   1.7  jmcneill 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_LID;
    228   1.7  jmcneill 				break;
    229   1.7  jmcneill 			default:
    230   1.5  jmcneill 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
    231   1.7  jmcneill 				break;
    232   1.5  jmcneill 			}
    233   1.1  jmcneill 			break;
    234   1.1  jmcneill 		}
    235   1.1  jmcneill 
    236   1.5  jmcneill 		if (key->key_pswitch.smpsw_name != NULL &&
    237   1.5  jmcneill 		    sysmon_pswitch_register(&key->key_pswitch) != 0) {
    238   1.1  jmcneill 			aprint_error(" %s:ERROR", key->key_label);
    239   1.1  jmcneill 			kmem_free(key->key_label, len);
    240   1.1  jmcneill 			kmem_free(key, sizeof(*key));
    241   1.1  jmcneill 			continue;
    242   1.1  jmcneill 		}
    243   1.1  jmcneill 
    244   1.1  jmcneill 		if (sc->sc_keys) {
    245   1.1  jmcneill 			aprint_normal(", %s", key->key_label);
    246   1.1  jmcneill 		} else {
    247   1.1  jmcneill 			aprint_normal(" %s", key->key_label);
    248   1.1  jmcneill 		}
    249   1.1  jmcneill 
    250   1.1  jmcneill 		key->key_next = sc->sc_keys;
    251   1.1  jmcneill 		sc->sc_keys = key;
    252   1.1  jmcneill 	}
    253   1.1  jmcneill 
    254   1.1  jmcneill 	if (sc->sc_keys == NULL) {
    255   1.1  jmcneill 		aprint_normal(" no keys configured\n");
    256   1.1  jmcneill 		return;
    257   1.1  jmcneill 	}
    258   1.1  jmcneill 
    259   1.1  jmcneill 	aprint_normal("\n");
    260   1.1  jmcneill 
    261   1.5  jmcneill 	if (use_wskbddev > 0) {
    262   1.5  jmcneill 		struct wskbddev_attach_args a;
    263   1.5  jmcneill 		memset(&a, 0, sizeof(a));
    264   1.5  jmcneill 		a.console = false;
    265   1.5  jmcneill 		a.keymap = &gpiokeys_keymapdata;
    266   1.5  jmcneill 		a.accessops = &gpiokeys_accessops;
    267   1.5  jmcneill 		a.accesscookie = sc;
    268  1.10   thorpej 		sc->sc_wskbddev =
    269  1.11   thorpej 		    config_found(self, &a, wskbddevprint, CFARGS_NONE);
    270   1.5  jmcneill 	}
    271   1.5  jmcneill 
    272   1.1  jmcneill 	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
    273   1.1  jmcneill 	callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);
    274   1.1  jmcneill 
    275   1.1  jmcneill 	gpiokeys_tick(sc);
    276   1.1  jmcneill }
    277   1.1  jmcneill 
    278   1.1  jmcneill static void
    279   1.1  jmcneill gpiokeys_tick(void *priv)
    280   1.1  jmcneill {
    281   1.1  jmcneill 	struct gpiokeys_softc * const sc = priv;
    282   1.1  jmcneill 	struct gpiokeys_key *key;
    283   1.1  jmcneill 
    284   1.1  jmcneill 	for (key = sc->sc_keys; key; key = key->key_next) {
    285   1.1  jmcneill 		if (key->key_pin == NULL) {
    286   1.1  jmcneill 			continue;
    287   1.1  jmcneill 		}
    288   1.1  jmcneill 		const int new_state = fdtbus_gpio_read(key->key_pin);
    289   1.1  jmcneill 		if (new_state != key->key_state) {
    290   1.1  jmcneill 			key->key_state = new_state;
    291   1.1  jmcneill 			sysmon_task_queue_sched(0, gpiokeys_task, key);
    292   1.1  jmcneill 		}
    293   1.1  jmcneill 	}
    294   1.1  jmcneill 	callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
    295   1.1  jmcneill }
    296   1.1  jmcneill 
    297   1.1  jmcneill static void
    298   1.1  jmcneill gpiokeys_task(void *priv)
    299   1.1  jmcneill {
    300   1.1  jmcneill 	struct gpiokeys_key *key = priv;
    301   1.5  jmcneill 	struct gpiokeys_softc *sc = key->key_sc;
    302   1.1  jmcneill 
    303   1.5  jmcneill 	if (key->key_pswitch.smpsw_name) {
    304   1.5  jmcneill 		sysmon_pswitch_event(&key->key_pswitch,
    305   1.5  jmcneill 		    key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    306   1.5  jmcneill 	} else if (sc->sc_enabled && sc->sc_wskbddev != NULL && key->key_usbcode != 0) {
    307   1.5  jmcneill 		wskbd_input(sc->sc_wskbddev,
    308   1.5  jmcneill 		    key->key_state ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
    309   1.5  jmcneill 		    key->key_usbcode);
    310   1.5  jmcneill 	}
    311   1.1  jmcneill }
    312