Home | History | Annotate | Line # | Download | only in fdt
gpiokeys.c revision 1.4
      1 /* $NetBSD: gpiokeys.c,v 1.4 2017/07/06 14:26:00 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      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 THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: gpiokeys.c,v 1.4 2017/07/06 14:26:00 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/kernel.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 #include <sys/kmem.h>
     37 #include <sys/bus.h>
     38 #include <sys/gpio.h>
     39 
     40 #include <dev/sysmon/sysmonvar.h>
     41 #include <dev/sysmon/sysmon_taskq.h>
     42 
     43 #include <dev/fdt/fdtvar.h>
     44 
     45 #define GPIOKEYS_POLL_INTERVAL	mstohz(200)
     46 
     47 #define KEY_POWER	116
     48 #define KEY_SLEEP	142
     49 
     50 static int	gpiokeys_match(device_t, cfdata_t, void *);
     51 static void	gpiokeys_attach(device_t, device_t, void *);
     52 
     53 static void	gpiokeys_tick(void *);
     54 static void	gpiokeys_task(void *);
     55 
     56 struct gpiokeys_softc;
     57 
     58 struct gpiokeys_key {
     59 	int			key_phandle;
     60 	char	 		*key_label;
     61 	struct fdtbus_gpio_pin	*key_pin;
     62 	u_int			key_debounce;
     63 	u_int			key_code;
     64 	struct sysmon_pswitch	key_pswitch;
     65 	u_int			key_state;
     66 
     67 	struct gpiokeys_key	*key_next;
     68 };
     69 
     70 struct gpiokeys_softc {
     71 	device_t	sc_dev;
     72 	int		sc_phandle;
     73 
     74 	struct fdtbus_gpio_pin *sc_pin;
     75 	bool		sc_always_on;
     76 	bool		sc_enable_val;
     77 
     78 	struct gpiokeys_key *sc_keys;
     79 	callout_t	sc_tick;
     80 };
     81 
     82 CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
     83     gpiokeys_match, gpiokeys_attach, NULL, NULL);
     84 
     85 static int
     86 gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
     87 {
     88 	const char * const compatible[] = { "gpio-keys", NULL };
     89 	const struct fdt_attach_args *faa = aux;
     90 
     91 	return of_match_compatible(faa->faa_phandle, compatible);
     92 }
     93 
     94 static void
     95 gpiokeys_attach(device_t parent, device_t self, void *aux)
     96 {
     97 	struct gpiokeys_softc * const sc = device_private(self);
     98 	const struct fdt_attach_args *faa = aux;
     99 	const int phandle = faa->faa_phandle;
    100 	struct gpiokeys_key *key;
    101 	int child, len;
    102 	u_int debounce, code;
    103 
    104 	sc->sc_dev = self;
    105 	sc->sc_phandle = phandle;
    106 
    107 	aprint_naive("\n");
    108 	aprint_normal(":");
    109 
    110 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    111 		if (of_getprop_uint32(child, "linux,code", &code))
    112 			continue;
    113 		if (of_getprop_uint32(child, "debounce-interval", &debounce))
    114 			debounce = 5;	/* default */
    115 		len = OF_getproplen(child, "label");
    116 		if (len <= 0) {
    117 			continue;
    118 		}
    119 		key = kmem_zalloc(sizeof(*key), KM_SLEEP);
    120 		key->key_phandle = child;
    121 		key->key_code = code;
    122 		key->key_label = kmem_zalloc(len, KM_SLEEP);
    123 		if (OF_getprop(child, "label", key->key_label, len) != len) {
    124 			kmem_free(key->key_label, len);
    125 			kmem_free(key, sizeof(*key));
    126 			continue;
    127 		}
    128 		key->key_debounce = debounce;
    129 		key->key_pin = fdtbus_gpio_acquire(child, "gpios",
    130 		    GPIO_PIN_INPUT);
    131 		if (key->key_pin)
    132 			key->key_state = fdtbus_gpio_read(key->key_pin);
    133 
    134 		key->key_pswitch.smpsw_name = key->key_label;
    135 		switch (code) {
    136 		case KEY_POWER:
    137 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
    138 			break;
    139 		case KEY_SLEEP:
    140 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
    141 			break;
    142 		default:
    143 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
    144 			break;
    145 		}
    146 
    147 		if (sysmon_pswitch_register(&key->key_pswitch) != 0) {
    148 			aprint_error(" %s:ERROR", key->key_label);
    149 			kmem_free(key->key_label, len);
    150 			kmem_free(key, sizeof(*key));
    151 			continue;
    152 		}
    153 
    154 		if (sc->sc_keys) {
    155 			aprint_normal(", %s", key->key_label);
    156 		} else {
    157 			aprint_normal(" %s", key->key_label);
    158 		}
    159 
    160 		key->key_next = sc->sc_keys;
    161 		sc->sc_keys = key;
    162 	}
    163 
    164 	if (sc->sc_keys == NULL) {
    165 		aprint_normal(" no keys configured\n");
    166 		return;
    167 	}
    168 
    169 	aprint_normal("\n");
    170 
    171 	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
    172 	callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);
    173 
    174 	gpiokeys_tick(sc);
    175 }
    176 
    177 static void
    178 gpiokeys_tick(void *priv)
    179 {
    180 	struct gpiokeys_softc * const sc = priv;
    181 	struct gpiokeys_key *key;
    182 
    183 	for (key = sc->sc_keys; key; key = key->key_next) {
    184 		if (key->key_pin == NULL) {
    185 			continue;
    186 		}
    187 		const int new_state = fdtbus_gpio_read(key->key_pin);
    188 		if (new_state != key->key_state) {
    189 			key->key_state = new_state;
    190 			sysmon_task_queue_sched(0, gpiokeys_task, key);
    191 		}
    192 	}
    193 	callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
    194 }
    195 
    196 static void
    197 gpiokeys_task(void *priv)
    198 {
    199 	struct gpiokeys_key *key = priv;
    200 
    201 	sysmon_pswitch_event(&key->key_pswitch,
    202 	    key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    203 }
    204