1*c7fb772bSthorpej /* $NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $ */
20fbc3295Sjmcneill
30fbc3295Sjmcneill /*-
40fbc3295Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
50fbc3295Sjmcneill * All rights reserved.
60fbc3295Sjmcneill *
70fbc3295Sjmcneill * Redistribution and use in source and binary forms, with or without
80fbc3295Sjmcneill * modification, are permitted provided that the following conditions
90fbc3295Sjmcneill * are met:
100fbc3295Sjmcneill * 1. Redistributions of source code must retain the above copyright
110fbc3295Sjmcneill * notice, this list of conditions and the following disclaimer.
120fbc3295Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
130fbc3295Sjmcneill * notice, this list of conditions and the following disclaimer in the
140fbc3295Sjmcneill * documentation and/or other materials provided with the distribution.
150fbc3295Sjmcneill *
160fbc3295Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
170fbc3295Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180fbc3295Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
190fbc3295Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
200fbc3295Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
210fbc3295Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
220fbc3295Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
230fbc3295Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
240fbc3295Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250fbc3295Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260fbc3295Sjmcneill * SUCH DAMAGE.
270fbc3295Sjmcneill */
280fbc3295Sjmcneill
290fbc3295Sjmcneill #include <sys/cdefs.h>
30*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: gpiokeys.c,v 1.11 2021/08/07 16:19:10 thorpej Exp $");
310fbc3295Sjmcneill
320fbc3295Sjmcneill #include <sys/param.h>
330fbc3295Sjmcneill #include <sys/kernel.h>
340fbc3295Sjmcneill #include <sys/systm.h>
350fbc3295Sjmcneill #include <sys/device.h>
360fbc3295Sjmcneill #include <sys/kmem.h>
370fbc3295Sjmcneill #include <sys/bus.h>
380fbc3295Sjmcneill #include <sys/gpio.h>
390fbc3295Sjmcneill
400fbc3295Sjmcneill #include <dev/sysmon/sysmonvar.h>
410fbc3295Sjmcneill #include <dev/sysmon/sysmon_taskq.h>
420fbc3295Sjmcneill
43b6e9d709Sjmcneill #include <dev/wscons/wsconsio.h>
44b6e9d709Sjmcneill #include <dev/wscons/wskbdvar.h>
45b6e9d709Sjmcneill #include <dev/wscons/wsksymdef.h>
46b6e9d709Sjmcneill #include <dev/wscons/wsksymvar.h>
47b6e9d709Sjmcneill #include <dev/wscons/linux_keymap.h>
48b6e9d709Sjmcneill
490fbc3295Sjmcneill #include <dev/fdt/fdtvar.h>
500fbc3295Sjmcneill
510fbc3295Sjmcneill #define GPIOKEYS_POLL_INTERVAL mstohz(200)
520fbc3295Sjmcneill
5375dc1f1cSjmcneill /* Event types */
5475dc1f1cSjmcneill #define EV_KEY 1
5575dc1f1cSjmcneill #define EV_SW 5
5675dc1f1cSjmcneill
5775dc1f1cSjmcneill /* Key and button events */
580fbc3295Sjmcneill #define KEY_POWER 116
590fbc3295Sjmcneill #define KEY_SLEEP 142
600fbc3295Sjmcneill
6175dc1f1cSjmcneill /* Switch events */
6275dc1f1cSjmcneill #define SW_LID 0
6375dc1f1cSjmcneill
640fbc3295Sjmcneill static int gpiokeys_match(device_t, cfdata_t, void *);
650fbc3295Sjmcneill static void gpiokeys_attach(device_t, device_t, void *);
660fbc3295Sjmcneill
670fbc3295Sjmcneill static void gpiokeys_tick(void *);
680fbc3295Sjmcneill static void gpiokeys_task(void *);
690fbc3295Sjmcneill
70404d04cdSbouyer extern const struct wscons_keydesc hidkbd_keydesctab[];
71b6e9d709Sjmcneill static const struct wskbd_mapdata gpiokeys_keymapdata = {
72404d04cdSbouyer hidkbd_keydesctab,
73b6e9d709Sjmcneill KB_US,
74b6e9d709Sjmcneill };
75b6e9d709Sjmcneill
760fbc3295Sjmcneill struct gpiokeys_softc;
770fbc3295Sjmcneill
780fbc3295Sjmcneill struct gpiokeys_key {
79b6e9d709Sjmcneill struct gpiokeys_softc *key_sc;
800fbc3295Sjmcneill int key_phandle;
810fbc3295Sjmcneill char *key_label;
820fbc3295Sjmcneill struct fdtbus_gpio_pin *key_pin;
830fbc3295Sjmcneill u_int key_debounce;
840fbc3295Sjmcneill u_int key_code;
850fbc3295Sjmcneill struct sysmon_pswitch key_pswitch;
86b6e9d709Sjmcneill uint8_t key_usbcode;
871d47c1e1Sjmcneill int key_state;
880fbc3295Sjmcneill
890fbc3295Sjmcneill struct gpiokeys_key *key_next;
900fbc3295Sjmcneill };
910fbc3295Sjmcneill
920fbc3295Sjmcneill struct gpiokeys_softc {
930fbc3295Sjmcneill device_t sc_dev;
940fbc3295Sjmcneill int sc_phandle;
950fbc3295Sjmcneill
960fbc3295Sjmcneill struct fdtbus_gpio_pin *sc_pin;
970fbc3295Sjmcneill bool sc_always_on;
980fbc3295Sjmcneill bool sc_enable_val;
990fbc3295Sjmcneill
1000fbc3295Sjmcneill struct gpiokeys_key *sc_keys;
1010fbc3295Sjmcneill callout_t sc_tick;
102b6e9d709Sjmcneill
103b6e9d709Sjmcneill device_t sc_wskbddev;
104b6e9d709Sjmcneill int sc_enabled;
1050fbc3295Sjmcneill };
1060fbc3295Sjmcneill
1070fbc3295Sjmcneill CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
1080fbc3295Sjmcneill gpiokeys_match, gpiokeys_attach, NULL, NULL);
1090fbc3295Sjmcneill
1100fbc3295Sjmcneill static int
gpiokeys_enable(void * v,int on)111b6e9d709Sjmcneill gpiokeys_enable(void *v, int on)
112b6e9d709Sjmcneill {
113b6e9d709Sjmcneill struct gpiokeys_softc * const sc = v;
114b6e9d709Sjmcneill
115b6e9d709Sjmcneill sc->sc_enabled = on;
116b6e9d709Sjmcneill
117b6e9d709Sjmcneill return 0;
118b6e9d709Sjmcneill }
119b6e9d709Sjmcneill
120b6e9d709Sjmcneill static void
gpiokeys_set_leds(void * v,int leds)121b6e9d709Sjmcneill gpiokeys_set_leds(void *v, int leds)
122b6e9d709Sjmcneill {
123b6e9d709Sjmcneill }
124b6e9d709Sjmcneill
125b6e9d709Sjmcneill static int
gpiokeys_ioctl(void * v,u_long cmd,void * data,int flag,lwp_t * l)126b6e9d709Sjmcneill gpiokeys_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
127b6e9d709Sjmcneill {
128b6e9d709Sjmcneill switch (cmd) {
129b6e9d709Sjmcneill case WSKBDIO_GTYPE:
130b6e9d709Sjmcneill *(int *)data = WSKBD_TYPE_USB;
131b6e9d709Sjmcneill return 0;
132b6e9d709Sjmcneill }
133b6e9d709Sjmcneill
134b6e9d709Sjmcneill return EPASSTHROUGH;
135b6e9d709Sjmcneill }
136b6e9d709Sjmcneill
137b6e9d709Sjmcneill static const struct wskbd_accessops gpiokeys_accessops = {
138b6e9d709Sjmcneill .enable = gpiokeys_enable,
139b6e9d709Sjmcneill .set_leds = gpiokeys_set_leds,
140b6e9d709Sjmcneill .ioctl = gpiokeys_ioctl
141b6e9d709Sjmcneill };
142b6e9d709Sjmcneill
1436e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
1446e54367aSthorpej { .compat = "gpio-keys" },
1456e54367aSthorpej DEVICE_COMPAT_EOL
1466e54367aSthorpej };
1476e54367aSthorpej
148b6e9d709Sjmcneill static int
gpiokeys_match(device_t parent,cfdata_t cf,void * aux)1490fbc3295Sjmcneill gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
1500fbc3295Sjmcneill {
1510fbc3295Sjmcneill const struct fdt_attach_args *faa = aux;
1520fbc3295Sjmcneill
1536e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
1540fbc3295Sjmcneill }
1550fbc3295Sjmcneill
1560fbc3295Sjmcneill static void
gpiokeys_attach(device_t parent,device_t self,void * aux)1570fbc3295Sjmcneill gpiokeys_attach(device_t parent, device_t self, void *aux)
1580fbc3295Sjmcneill {
1590fbc3295Sjmcneill struct gpiokeys_softc * const sc = device_private(self);
1600fbc3295Sjmcneill const struct fdt_attach_args *faa = aux;
1610fbc3295Sjmcneill const int phandle = faa->faa_phandle;
1620fbc3295Sjmcneill struct gpiokeys_key *key;
16375dc1f1cSjmcneill u_int debounce, input_type, code;
164b6e9d709Sjmcneill int use_wskbddev = 0;
165b6e9d709Sjmcneill int child, len;
1660fbc3295Sjmcneill
1670fbc3295Sjmcneill sc->sc_dev = self;
1680fbc3295Sjmcneill sc->sc_phandle = phandle;
1690fbc3295Sjmcneill
1700fbc3295Sjmcneill aprint_naive("\n");
1710fbc3295Sjmcneill aprint_normal(":");
1720fbc3295Sjmcneill
1730fbc3295Sjmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
17475dc1f1cSjmcneill if (of_getprop_uint32(child, "linux,input-type", &input_type))
17575dc1f1cSjmcneill input_type = EV_KEY; /* default */
17637b4dc38Sjmcneill if (of_getprop_uint32(child, "linux,code", &code))
1770fbc3295Sjmcneill continue;
17837b4dc38Sjmcneill if (of_getprop_uint32(child, "debounce-interval", &debounce))
17937b4dc38Sjmcneill debounce = 5; /* default */
1800fbc3295Sjmcneill len = OF_getproplen(child, "label");
1810fbc3295Sjmcneill if (len <= 0) {
1820fbc3295Sjmcneill continue;
1830fbc3295Sjmcneill }
1840fbc3295Sjmcneill key = kmem_zalloc(sizeof(*key), KM_SLEEP);
185b6e9d709Sjmcneill key->key_sc = sc;
1860fbc3295Sjmcneill key->key_phandle = child;
1870fbc3295Sjmcneill key->key_code = code;
1880fbc3295Sjmcneill key->key_label = kmem_zalloc(len, KM_SLEEP);
1890fbc3295Sjmcneill if (OF_getprop(child, "label", key->key_label, len) != len) {
1900fbc3295Sjmcneill kmem_free(key->key_label, len);
1910fbc3295Sjmcneill kmem_free(key, sizeof(*key));
1920fbc3295Sjmcneill continue;
1930fbc3295Sjmcneill }
19437b4dc38Sjmcneill key->key_debounce = debounce;
1950fbc3295Sjmcneill key->key_pin = fdtbus_gpio_acquire(child, "gpios",
1960fbc3295Sjmcneill GPIO_PIN_INPUT);
19778659f08Sjmcneill if (key->key_pin)
198c9d3de5cSjmcneill key->key_state = fdtbus_gpio_read(key->key_pin);
1990fbc3295Sjmcneill
20075dc1f1cSjmcneill switch (input_type) {
20175dc1f1cSjmcneill case EV_KEY:
2020fbc3295Sjmcneill switch (code) {
2030fbc3295Sjmcneill case KEY_POWER:
204b6e9d709Sjmcneill key->key_pswitch.smpsw_name = key->key_label;
2050fbc3295Sjmcneill key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
2060fbc3295Sjmcneill break;
2070fbc3295Sjmcneill case KEY_SLEEP:
208b6e9d709Sjmcneill key->key_pswitch.smpsw_name = key->key_label;
2090fbc3295Sjmcneill key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
2100fbc3295Sjmcneill break;
2110fbc3295Sjmcneill default:
212b6e9d709Sjmcneill key->key_usbcode = linux_key_to_usb(code);
213b6e9d709Sjmcneill if (key->key_usbcode != 0) {
214b6e9d709Sjmcneill use_wskbddev++;
215b6e9d709Sjmcneill } else {
216b6e9d709Sjmcneill key->key_pswitch.smpsw_name = key->key_label;
2170fbc3295Sjmcneill key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
218b6e9d709Sjmcneill }
2190fbc3295Sjmcneill break;
2200fbc3295Sjmcneill }
22175dc1f1cSjmcneill break;
22275dc1f1cSjmcneill case EV_SW:
22375dc1f1cSjmcneill key->key_pswitch.smpsw_name = key->key_label;
22475dc1f1cSjmcneill switch (code) {
22575dc1f1cSjmcneill case SW_LID:
2261d47c1e1Sjmcneill key->key_state = -1; /* Send notification on attach */
22775dc1f1cSjmcneill key->key_pswitch.smpsw_type = PSWITCH_TYPE_LID;
22875dc1f1cSjmcneill break;
22975dc1f1cSjmcneill default:
23075dc1f1cSjmcneill key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
23175dc1f1cSjmcneill break;
23275dc1f1cSjmcneill }
23375dc1f1cSjmcneill break;
23475dc1f1cSjmcneill }
2350fbc3295Sjmcneill
236b6e9d709Sjmcneill if (key->key_pswitch.smpsw_name != NULL &&
237b6e9d709Sjmcneill sysmon_pswitch_register(&key->key_pswitch) != 0) {
2380fbc3295Sjmcneill aprint_error(" %s:ERROR", key->key_label);
2390fbc3295Sjmcneill kmem_free(key->key_label, len);
2400fbc3295Sjmcneill kmem_free(key, sizeof(*key));
2410fbc3295Sjmcneill continue;
2420fbc3295Sjmcneill }
2430fbc3295Sjmcneill
2440fbc3295Sjmcneill if (sc->sc_keys) {
2450fbc3295Sjmcneill aprint_normal(", %s", key->key_label);
2460fbc3295Sjmcneill } else {
2470fbc3295Sjmcneill aprint_normal(" %s", key->key_label);
2480fbc3295Sjmcneill }
2490fbc3295Sjmcneill
2500fbc3295Sjmcneill key->key_next = sc->sc_keys;
2510fbc3295Sjmcneill sc->sc_keys = key;
2520fbc3295Sjmcneill }
2530fbc3295Sjmcneill
2540fbc3295Sjmcneill if (sc->sc_keys == NULL) {
2550fbc3295Sjmcneill aprint_normal(" no keys configured\n");
2560fbc3295Sjmcneill return;
2570fbc3295Sjmcneill }
2580fbc3295Sjmcneill
2590fbc3295Sjmcneill aprint_normal("\n");
2600fbc3295Sjmcneill
261b6e9d709Sjmcneill if (use_wskbddev > 0) {
262b6e9d709Sjmcneill struct wskbddev_attach_args a;
263b6e9d709Sjmcneill memset(&a, 0, sizeof(a));
264b6e9d709Sjmcneill a.console = false;
265b6e9d709Sjmcneill a.keymap = &gpiokeys_keymapdata;
266b6e9d709Sjmcneill a.accessops = &gpiokeys_accessops;
267b6e9d709Sjmcneill a.accesscookie = sc;
2682685996bSthorpej sc->sc_wskbddev =
269*c7fb772bSthorpej config_found(self, &a, wskbddevprint, CFARGS_NONE);
270b6e9d709Sjmcneill }
271b6e9d709Sjmcneill
2720fbc3295Sjmcneill callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
2730fbc3295Sjmcneill callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);
2740fbc3295Sjmcneill
2750fbc3295Sjmcneill gpiokeys_tick(sc);
2760fbc3295Sjmcneill }
2770fbc3295Sjmcneill
2780fbc3295Sjmcneill static void
gpiokeys_tick(void * priv)2790fbc3295Sjmcneill gpiokeys_tick(void *priv)
2800fbc3295Sjmcneill {
2810fbc3295Sjmcneill struct gpiokeys_softc * const sc = priv;
2820fbc3295Sjmcneill struct gpiokeys_key *key;
2830fbc3295Sjmcneill
2840fbc3295Sjmcneill for (key = sc->sc_keys; key; key = key->key_next) {
2850fbc3295Sjmcneill if (key->key_pin == NULL) {
2860fbc3295Sjmcneill continue;
2870fbc3295Sjmcneill }
2880fbc3295Sjmcneill const int new_state = fdtbus_gpio_read(key->key_pin);
2890fbc3295Sjmcneill if (new_state != key->key_state) {
2900fbc3295Sjmcneill key->key_state = new_state;
2910fbc3295Sjmcneill sysmon_task_queue_sched(0, gpiokeys_task, key);
2920fbc3295Sjmcneill }
2930fbc3295Sjmcneill }
2940fbc3295Sjmcneill callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
2950fbc3295Sjmcneill }
2960fbc3295Sjmcneill
2970fbc3295Sjmcneill static void
gpiokeys_task(void * priv)2980fbc3295Sjmcneill gpiokeys_task(void *priv)
2990fbc3295Sjmcneill {
3000fbc3295Sjmcneill struct gpiokeys_key *key = priv;
301b6e9d709Sjmcneill struct gpiokeys_softc *sc = key->key_sc;
3020fbc3295Sjmcneill
303b6e9d709Sjmcneill if (key->key_pswitch.smpsw_name) {
3040fbc3295Sjmcneill sysmon_pswitch_event(&key->key_pswitch,
3050fbc3295Sjmcneill key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
306b6e9d709Sjmcneill } else if (sc->sc_enabled && sc->sc_wskbddev != NULL && key->key_usbcode != 0) {
307b6e9d709Sjmcneill wskbd_input(sc->sc_wskbddev,
308b6e9d709Sjmcneill key->key_state ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
309b6e9d709Sjmcneill key->key_usbcode);
310b6e9d709Sjmcneill }
3110fbc3295Sjmcneill }
312