Home | History | Annotate | Line # | Download | only in fdt
gpiokeys.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: gpiokeys.c,v 1.1 2015/12/14 20:58:45 jmcneill 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: gpiokeys.c,v 1.1 2015/12/14 20:58:45 jmcneill 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.1  jmcneill #include <dev/fdt/fdtvar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #define GPIOKEYS_POLL_INTERVAL	mstohz(200)
     46  1.1  jmcneill 
     47  1.1  jmcneill #define KEY_POWER	116
     48  1.1  jmcneill #define KEY_SLEEP	142
     49  1.1  jmcneill 
     50  1.1  jmcneill static int	gpiokeys_match(device_t, cfdata_t, void *);
     51  1.1  jmcneill static void	gpiokeys_attach(device_t, device_t, void *);
     52  1.1  jmcneill 
     53  1.1  jmcneill static void	gpiokeys_tick(void *);
     54  1.1  jmcneill static void	gpiokeys_task(void *);
     55  1.1  jmcneill 
     56  1.1  jmcneill struct gpiokeys_softc;
     57  1.1  jmcneill 
     58  1.1  jmcneill struct gpiokeys_key {
     59  1.1  jmcneill 	int			key_phandle;
     60  1.1  jmcneill 	char	 		*key_label;
     61  1.1  jmcneill 	struct fdtbus_gpio_pin	*key_pin;
     62  1.1  jmcneill 	u_int			key_debounce;
     63  1.1  jmcneill 	u_int			key_code;
     64  1.1  jmcneill 	struct sysmon_pswitch	key_pswitch;
     65  1.1  jmcneill 	u_int			key_state;
     66  1.1  jmcneill 
     67  1.1  jmcneill 	struct gpiokeys_key	*key_next;
     68  1.1  jmcneill };
     69  1.1  jmcneill 
     70  1.1  jmcneill struct gpiokeys_softc {
     71  1.1  jmcneill 	device_t	sc_dev;
     72  1.1  jmcneill 	int		sc_phandle;
     73  1.1  jmcneill 
     74  1.1  jmcneill 	struct fdtbus_gpio_pin *sc_pin;
     75  1.1  jmcneill 	bool		sc_always_on;
     76  1.1  jmcneill 	bool		sc_enable_val;
     77  1.1  jmcneill 
     78  1.1  jmcneill 	struct gpiokeys_key *sc_keys;
     79  1.1  jmcneill 	callout_t	sc_tick;
     80  1.1  jmcneill };
     81  1.1  jmcneill 
     82  1.1  jmcneill CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
     83  1.1  jmcneill     gpiokeys_match, gpiokeys_attach, NULL, NULL);
     84  1.1  jmcneill 
     85  1.1  jmcneill static int
     86  1.1  jmcneill gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	const char * const compatible[] = { "gpio-keys", NULL };
     89  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     90  1.1  jmcneill 
     91  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     92  1.1  jmcneill }
     93  1.1  jmcneill 
     94  1.1  jmcneill static void
     95  1.1  jmcneill gpiokeys_attach(device_t parent, device_t self, void *aux)
     96  1.1  jmcneill {
     97  1.1  jmcneill 	struct gpiokeys_softc * const sc = device_private(self);
     98  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     99  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    100  1.1  jmcneill 	struct gpiokeys_key *key;
    101  1.1  jmcneill 	int child, len;
    102  1.1  jmcneill 	u_int debounce, code;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	sc->sc_dev = self;
    105  1.1  jmcneill 	sc->sc_phandle = phandle;
    106  1.1  jmcneill 
    107  1.1  jmcneill 	aprint_naive("\n");
    108  1.1  jmcneill 	aprint_normal(":");
    109  1.1  jmcneill 
    110  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    111  1.1  jmcneill 		len = OF_getprop(child, "linux,code", &code, sizeof(code));
    112  1.1  jmcneill 		if (len != sizeof(code)) {
    113  1.1  jmcneill 			continue;
    114  1.1  jmcneill 		}
    115  1.1  jmcneill 		code = be32toh(code);
    116  1.1  jmcneill 		len = OF_getproplen(child, "label");
    117  1.1  jmcneill 		if (len <= 0) {
    118  1.1  jmcneill 			continue;
    119  1.1  jmcneill 		}
    120  1.1  jmcneill 		key = kmem_zalloc(sizeof(*key), KM_SLEEP);
    121  1.1  jmcneill 		key->key_phandle = child;
    122  1.1  jmcneill 		key->key_code = code;
    123  1.1  jmcneill 		key->key_label = kmem_zalloc(len, KM_SLEEP);
    124  1.1  jmcneill 		if (OF_getprop(child, "label", key->key_label, len) != len) {
    125  1.1  jmcneill 			kmem_free(key->key_label, len);
    126  1.1  jmcneill 			kmem_free(key, sizeof(*key));
    127  1.1  jmcneill 			continue;
    128  1.1  jmcneill 		}
    129  1.1  jmcneill 		if (OF_getprop(child, "debounce-interval", &debounce,
    130  1.1  jmcneill 		    sizeof(debounce)) == sizeof(debounce)) {
    131  1.1  jmcneill 			key->key_debounce = be32toh(debounce);
    132  1.1  jmcneill 		} else {
    133  1.1  jmcneill 			key->key_debounce = 5; /* default */
    134  1.1  jmcneill 		}
    135  1.1  jmcneill 		key->key_pin = fdtbus_gpio_acquire(child, "gpios",
    136  1.1  jmcneill 		    GPIO_PIN_INPUT);
    137  1.1  jmcneill 
    138  1.1  jmcneill 		key->key_pswitch.smpsw_name = key->key_label;
    139  1.1  jmcneill 		switch (code) {
    140  1.1  jmcneill 		case KEY_POWER:
    141  1.1  jmcneill 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
    142  1.1  jmcneill 			break;
    143  1.1  jmcneill 		case KEY_SLEEP:
    144  1.1  jmcneill 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
    145  1.1  jmcneill 			break;
    146  1.1  jmcneill 		default:
    147  1.1  jmcneill 			key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
    148  1.1  jmcneill 			break;
    149  1.1  jmcneill 		}
    150  1.1  jmcneill 
    151  1.1  jmcneill 		if (sysmon_pswitch_register(&key->key_pswitch) != 0) {
    152  1.1  jmcneill 			aprint_error(" %s:ERROR", key->key_label);
    153  1.1  jmcneill 			kmem_free(key->key_label, len);
    154  1.1  jmcneill 			kmem_free(key, sizeof(*key));
    155  1.1  jmcneill 			continue;
    156  1.1  jmcneill 		}
    157  1.1  jmcneill 
    158  1.1  jmcneill 		if (sc->sc_keys) {
    159  1.1  jmcneill 			aprint_normal(", %s", key->key_label);
    160  1.1  jmcneill 		} else {
    161  1.1  jmcneill 			aprint_normal(" %s", key->key_label);
    162  1.1  jmcneill 		}
    163  1.1  jmcneill 
    164  1.1  jmcneill 		key->key_next = sc->sc_keys;
    165  1.1  jmcneill 		sc->sc_keys = key;
    166  1.1  jmcneill 	}
    167  1.1  jmcneill 
    168  1.1  jmcneill 	if (sc->sc_keys == NULL) {
    169  1.1  jmcneill 		aprint_normal(" no keys configured\n");
    170  1.1  jmcneill 		return;
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 
    173  1.1  jmcneill 	aprint_normal("\n");
    174  1.1  jmcneill 
    175  1.1  jmcneill 	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
    176  1.1  jmcneill 	callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);
    177  1.1  jmcneill 
    178  1.1  jmcneill 	gpiokeys_tick(sc);
    179  1.1  jmcneill }
    180  1.1  jmcneill 
    181  1.1  jmcneill static void
    182  1.1  jmcneill gpiokeys_tick(void *priv)
    183  1.1  jmcneill {
    184  1.1  jmcneill 	struct gpiokeys_softc * const sc = priv;
    185  1.1  jmcneill 	struct gpiokeys_key *key;
    186  1.1  jmcneill 
    187  1.1  jmcneill 	for (key = sc->sc_keys; key; key = key->key_next) {
    188  1.1  jmcneill 		if (key->key_pin == NULL) {
    189  1.1  jmcneill 			continue;
    190  1.1  jmcneill 		}
    191  1.1  jmcneill 		const int new_state = fdtbus_gpio_read(key->key_pin);
    192  1.1  jmcneill 		if (new_state != key->key_state) {
    193  1.1  jmcneill 			key->key_state = new_state;
    194  1.1  jmcneill 			sysmon_task_queue_sched(0, gpiokeys_task, key);
    195  1.1  jmcneill 		}
    196  1.1  jmcneill 	}
    197  1.1  jmcneill 	callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
    198  1.1  jmcneill }
    199  1.1  jmcneill 
    200  1.1  jmcneill static void
    201  1.1  jmcneill gpiokeys_task(void *priv)
    202  1.1  jmcneill {
    203  1.1  jmcneill 	struct gpiokeys_key *key = priv;
    204  1.1  jmcneill 
    205  1.1  jmcneill 	sysmon_pswitch_event(&key->key_pswitch,
    206  1.1  jmcneill 	    key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    207  1.1  jmcneill }
    208