xref: /netbsd-src/sys/dev/fdt/gpiokeys.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /* $NetBSD: gpiokeys.c,v 1.8 2018/05/10 13:05:18 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@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.8 2018/05/10 13:05:18 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/wscons/wsconsio.h>
44 #include <dev/wscons/wskbdvar.h>
45 #include <dev/wscons/wsksymdef.h>
46 #include <dev/wscons/wsksymvar.h>
47 #include <dev/wscons/linux_keymap.h>
48 
49 #include <dev/fdt/fdtvar.h>
50 
51 #define GPIOKEYS_POLL_INTERVAL	mstohz(200)
52 
53 /* Event types */
54 #define	EV_KEY		1
55 #define	EV_SW		5
56 
57 /* Key and button events */
58 #define	KEY_POWER	116
59 #define	KEY_SLEEP	142
60 
61 /* Switch events */
62 #define	SW_LID		0
63 
64 static int	gpiokeys_match(device_t, cfdata_t, void *);
65 static void	gpiokeys_attach(device_t, device_t, void *);
66 
67 static void	gpiokeys_tick(void *);
68 static void	gpiokeys_task(void *);
69 
70 extern const struct wscons_keydesc hidkbd_keydesctab[];
71 static const struct wskbd_mapdata gpiokeys_keymapdata = {
72 	hidkbd_keydesctab,
73 	KB_US,
74 };
75 
76 struct gpiokeys_softc;
77 
78 struct gpiokeys_key {
79 	struct gpiokeys_softc	*key_sc;
80 	int			key_phandle;
81 	char	 		*key_label;
82 	struct fdtbus_gpio_pin	*key_pin;
83 	u_int			key_debounce;
84 	u_int			key_code;
85 	struct sysmon_pswitch	key_pswitch;
86 	uint8_t			key_usbcode;
87 	int			key_state;
88 
89 	struct gpiokeys_key	*key_next;
90 };
91 
92 struct gpiokeys_softc {
93 	device_t	sc_dev;
94 	int		sc_phandle;
95 
96 	struct fdtbus_gpio_pin *sc_pin;
97 	bool		sc_always_on;
98 	bool		sc_enable_val;
99 
100 	struct gpiokeys_key *sc_keys;
101 	callout_t	sc_tick;
102 
103 	device_t	sc_wskbddev;
104 	int		sc_enabled;
105 };
106 
107 CFATTACH_DECL_NEW(gpiokeys, sizeof(struct gpiokeys_softc),
108     gpiokeys_match, gpiokeys_attach, NULL, NULL);
109 
110 static int
111 gpiokeys_enable(void *v, int on)
112 {
113 	struct gpiokeys_softc * const sc = v;
114 
115 	sc->sc_enabled = on;
116 
117 	return 0;
118 }
119 
120 static void
121 gpiokeys_set_leds(void *v, int leds)
122 {
123 }
124 
125 static int
126 gpiokeys_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
127 {
128 	switch (cmd) {
129 	case WSKBDIO_GTYPE:
130 		*(int *)data = WSKBD_TYPE_USB;
131 		return 0;
132 	}
133 
134 	return EPASSTHROUGH;
135 }
136 
137 static const struct wskbd_accessops gpiokeys_accessops = {
138 	.enable = gpiokeys_enable,
139 	.set_leds = gpiokeys_set_leds,
140 	.ioctl = gpiokeys_ioctl
141 };
142 
143 static int
144 gpiokeys_match(device_t parent, cfdata_t cf, void *aux)
145 {
146 	const char * const compatible[] = { "gpio-keys", NULL };
147 	const struct fdt_attach_args *faa = aux;
148 
149 	return of_match_compatible(faa->faa_phandle, compatible);
150 }
151 
152 static void
153 gpiokeys_attach(device_t parent, device_t self, void *aux)
154 {
155 	struct gpiokeys_softc * const sc = device_private(self);
156 	const struct fdt_attach_args *faa = aux;
157 	const int phandle = faa->faa_phandle;
158 	struct gpiokeys_key *key;
159 	u_int debounce, input_type, code;
160 	int use_wskbddev = 0;
161 	int child, len;
162 
163 	sc->sc_dev = self;
164 	sc->sc_phandle = phandle;
165 
166 	aprint_naive("\n");
167 	aprint_normal(":");
168 
169 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
170 		if (of_getprop_uint32(child, "linux,input-type", &input_type))
171 			input_type = EV_KEY;	/* default */
172 		if (of_getprop_uint32(child, "linux,code", &code))
173 			continue;
174 		if (of_getprop_uint32(child, "debounce-interval", &debounce))
175 			debounce = 5;	/* default */
176 		len = OF_getproplen(child, "label");
177 		if (len <= 0) {
178 			continue;
179 		}
180 		key = kmem_zalloc(sizeof(*key), KM_SLEEP);
181 		key->key_sc = sc;
182 		key->key_phandle = child;
183 		key->key_code = code;
184 		key->key_label = kmem_zalloc(len, KM_SLEEP);
185 		if (OF_getprop(child, "label", key->key_label, len) != len) {
186 			kmem_free(key->key_label, len);
187 			kmem_free(key, sizeof(*key));
188 			continue;
189 		}
190 		key->key_debounce = debounce;
191 		key->key_pin = fdtbus_gpio_acquire(child, "gpios",
192 		    GPIO_PIN_INPUT);
193 		if (key->key_pin)
194 			key->key_state = fdtbus_gpio_read(key->key_pin);
195 
196 		switch (input_type) {
197 		case EV_KEY:
198 			switch (code) {
199 			case KEY_POWER:
200 				key->key_pswitch.smpsw_name = key->key_label;
201 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_POWER;
202 				break;
203 			case KEY_SLEEP:
204 				key->key_pswitch.smpsw_name = key->key_label;
205 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_SLEEP;
206 				break;
207 			default:
208 				key->key_usbcode = linux_key_to_usb(code);
209 				if (key->key_usbcode != 0) {
210 					use_wskbddev++;
211 				} else {
212 					key->key_pswitch.smpsw_name = key->key_label;
213 					key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
214 				}
215 				break;
216 			}
217 			break;
218 		case EV_SW:
219 			key->key_pswitch.smpsw_name = key->key_label;
220 			switch (code) {
221 			case SW_LID:
222 				key->key_state = -1;	/* Send notification on attach */
223 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_LID;
224 				break;
225 			default:
226 				key->key_pswitch.smpsw_type = PSWITCH_TYPE_HOTKEY;
227 				break;
228 			}
229 			break;
230 		}
231 
232 		if (key->key_pswitch.smpsw_name != NULL &&
233 		    sysmon_pswitch_register(&key->key_pswitch) != 0) {
234 			aprint_error(" %s:ERROR", key->key_label);
235 			kmem_free(key->key_label, len);
236 			kmem_free(key, sizeof(*key));
237 			continue;
238 		}
239 
240 		if (sc->sc_keys) {
241 			aprint_normal(", %s", key->key_label);
242 		} else {
243 			aprint_normal(" %s", key->key_label);
244 		}
245 
246 		key->key_next = sc->sc_keys;
247 		sc->sc_keys = key;
248 	}
249 
250 	if (sc->sc_keys == NULL) {
251 		aprint_normal(" no keys configured\n");
252 		return;
253 	}
254 
255 	aprint_normal("\n");
256 
257 	if (use_wskbddev > 0) {
258 		struct wskbddev_attach_args a;
259 		memset(&a, 0, sizeof(a));
260 		a.console = false;
261 		a.keymap = &gpiokeys_keymapdata;
262 		a.accessops = &gpiokeys_accessops;
263 		a.accesscookie = sc;
264 		sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a,
265 		    wskbddevprint);
266 	}
267 
268 	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
269 	callout_setfunc(&sc->sc_tick, gpiokeys_tick, sc);
270 
271 	gpiokeys_tick(sc);
272 }
273 
274 static void
275 gpiokeys_tick(void *priv)
276 {
277 	struct gpiokeys_softc * const sc = priv;
278 	struct gpiokeys_key *key;
279 
280 	for (key = sc->sc_keys; key; key = key->key_next) {
281 		if (key->key_pin == NULL) {
282 			continue;
283 		}
284 		const int new_state = fdtbus_gpio_read(key->key_pin);
285 		if (new_state != key->key_state) {
286 			key->key_state = new_state;
287 			sysmon_task_queue_sched(0, gpiokeys_task, key);
288 		}
289 	}
290 	callout_schedule(&sc->sc_tick, GPIOKEYS_POLL_INTERVAL);
291 }
292 
293 static void
294 gpiokeys_task(void *priv)
295 {
296 	struct gpiokeys_key *key = priv;
297 	struct gpiokeys_softc *sc = key->key_sc;
298 
299 	if (key->key_pswitch.smpsw_name) {
300 		sysmon_pswitch_event(&key->key_pswitch,
301 		    key->key_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
302 	} else if (sc->sc_enabled && sc->sc_wskbddev != NULL && key->key_usbcode != 0) {
303 		wskbd_input(sc->sc_wskbddev,
304 		    key->key_state ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
305 		    key->key_usbcode);
306 	}
307 }
308