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