xref: /netbsd/sys/dev/gpio/gpiobutton.c (revision 505bbaa9)
1*505bbaa9Sjmcneill /* $NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 jmcneill Exp $ */
2f25f3b5aSjmcneill 
3f25f3b5aSjmcneill /*-
4f25f3b5aSjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5f25f3b5aSjmcneill  * All rights reserved.
6f25f3b5aSjmcneill  *
7f25f3b5aSjmcneill  * Redistribution and use in source and binary forms, with or without
8f25f3b5aSjmcneill  * modification, are permitted provided that the following conditions
9f25f3b5aSjmcneill  * are met:
10f25f3b5aSjmcneill  * 1. Redistributions of source code must retain the above copyright
11f25f3b5aSjmcneill  *    notice, this list of conditions and the following disclaimer.
12f25f3b5aSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13f25f3b5aSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14f25f3b5aSjmcneill  *    documentation and/or other materials provided with the distribution.
15f25f3b5aSjmcneill  *
16f25f3b5aSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17f25f3b5aSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18f25f3b5aSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19f25f3b5aSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20f25f3b5aSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21f25f3b5aSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22f25f3b5aSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23f25f3b5aSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24f25f3b5aSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f25f3b5aSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f25f3b5aSjmcneill  * SUCH DAMAGE.
27f25f3b5aSjmcneill  */
28f25f3b5aSjmcneill 
29f25f3b5aSjmcneill #include "locators.h"
30f25f3b5aSjmcneill 
31f25f3b5aSjmcneill #include <sys/cdefs.h>
32*505bbaa9Sjmcneill __KERNEL_RCSID(0, "$NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 jmcneill Exp $");
33f25f3b5aSjmcneill 
34f25f3b5aSjmcneill #include <sys/param.h>
35f25f3b5aSjmcneill #include <sys/bus.h>
36f25f3b5aSjmcneill #include <sys/device.h>
37f25f3b5aSjmcneill #include <sys/intr.h>
38f25f3b5aSjmcneill #include <sys/systm.h>
39f25f3b5aSjmcneill #include <sys/kernel.h>
40f25f3b5aSjmcneill #include <sys/gpio.h>
41f25f3b5aSjmcneill 
42f25f3b5aSjmcneill #include <dev/sysmon/sysmonvar.h>
43*505bbaa9Sjmcneill #include <dev/sysmon/sysmon_taskq.h>
44f25f3b5aSjmcneill 
45f25f3b5aSjmcneill #include <dev/gpio/gpiovar.h>
46f25f3b5aSjmcneill 
47f25f3b5aSjmcneill #define GPIOBUTTON_POLL_INTERVAL	mstohz(200)
48f25f3b5aSjmcneill 
49f25f3b5aSjmcneill #define GPIOBUTTON_POLARITY_MASK	0x80
50f25f3b5aSjmcneill #define GPIOBUTTON_POLARITY_ACTIVE_LOW	0
51f25f3b5aSjmcneill #define GPIOBUTTON_POLARITY_ACTIVE_HIGH	1
52f25f3b5aSjmcneill #define GPIOBUTTON_TYPE_MASK		0x0f
53f25f3b5aSjmcneill #define GPIOBUTTON_TYPE_POWER		1
54f25f3b5aSjmcneill #define GPIOBUTTON_TYPE_SLEEP		2
55f25f3b5aSjmcneill 
56f25f3b5aSjmcneill static int	gpiobutton_match(device_t, cfdata_t, void *);
57f25f3b5aSjmcneill static void	gpiobutton_attach(device_t, device_t, void *);
58f25f3b5aSjmcneill 
59f25f3b5aSjmcneill struct gpiobutton_softc {
60f25f3b5aSjmcneill 	device_t		sc_dev;
61f25f3b5aSjmcneill 	void			*sc_gpio;
62f25f3b5aSjmcneill 	struct gpio_pinmap	sc_map;
63f25f3b5aSjmcneill 	int			sc_pinmap[1];
64f25f3b5aSjmcneill 	bool			sc_active_high;
65f25f3b5aSjmcneill 
66f25f3b5aSjmcneill 	struct sysmon_pswitch	sc_smpsw;
67f25f3b5aSjmcneill 
68f25f3b5aSjmcneill 	callout_t		sc_tick;
69f25f3b5aSjmcneill 	bool			sc_state;
70f25f3b5aSjmcneill };
71f25f3b5aSjmcneill 
72f25f3b5aSjmcneill static bool	gpiobutton_is_pressed(struct gpiobutton_softc *);
73f25f3b5aSjmcneill static void	gpiobutton_tick(void *);
74*505bbaa9Sjmcneill static void	gpiobutton_task(void *);
75f25f3b5aSjmcneill 
76f25f3b5aSjmcneill CFATTACH_DECL_NEW(gpiobutton, sizeof(struct gpiobutton_softc),
77f25f3b5aSjmcneill 	gpiobutton_match, gpiobutton_attach, NULL, NULL);
78f25f3b5aSjmcneill 
79f25f3b5aSjmcneill static int
gpiobutton_match(device_t parent,cfdata_t cf,void * aux)80f25f3b5aSjmcneill gpiobutton_match(device_t parent, cfdata_t cf, void *aux)
81f25f3b5aSjmcneill {
82f25f3b5aSjmcneill 	struct gpio_attach_args * const ga = aux;
83f25f3b5aSjmcneill 
84f25f3b5aSjmcneill 	if (strcmp(ga->ga_dvname, cf->cf_name) != 0)
85f25f3b5aSjmcneill 		return 0;
86f25f3b5aSjmcneill 
87f25f3b5aSjmcneill 	if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1)
88f25f3b5aSjmcneill 		return 0;
89f25f3b5aSjmcneill 
90f25f3b5aSjmcneill 	const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
919e4bc75aSjmcneill 
92f25f3b5aSjmcneill 	switch (type) {
93f25f3b5aSjmcneill 	case GPIOBUTTON_TYPE_POWER:
94f25f3b5aSjmcneill 	case GPIOBUTTON_TYPE_SLEEP:
95f25f3b5aSjmcneill 		return 1;
96f25f3b5aSjmcneill 	default:
97f25f3b5aSjmcneill 		return 0;
98f25f3b5aSjmcneill 	}
99f25f3b5aSjmcneill }
100f25f3b5aSjmcneill 
101f25f3b5aSjmcneill static void
gpiobutton_attach(device_t parent,device_t self,void * aux)102f25f3b5aSjmcneill gpiobutton_attach(device_t parent, device_t self, void *aux)
103f25f3b5aSjmcneill {
104f25f3b5aSjmcneill 	struct gpiobutton_softc * const sc = device_private(self);
105f25f3b5aSjmcneill 	struct gpio_attach_args * const ga = aux;
106f25f3b5aSjmcneill 	const char *desc;
107*505bbaa9Sjmcneill 	int caps;
108f25f3b5aSjmcneill 
109f25f3b5aSjmcneill 	const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
110f25f3b5aSjmcneill 	const u_int pol = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_POLARITY_MASK);
111f25f3b5aSjmcneill 
112f25f3b5aSjmcneill 	sc->sc_dev = self;
113f25f3b5aSjmcneill 	sc->sc_gpio = ga->ga_gpio;
114f25f3b5aSjmcneill 	sc->sc_map.pm_map = sc->sc_pinmap;
115f25f3b5aSjmcneill 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
116f25f3b5aSjmcneill 	    &sc->sc_map)) {
117f25f3b5aSjmcneill 		aprint_error(": couldn't map pins\n");
118f25f3b5aSjmcneill 		return;
119f25f3b5aSjmcneill 	}
120f25f3b5aSjmcneill 	sc->sc_active_high = pol == GPIOBUTTON_POLARITY_ACTIVE_HIGH;
121f25f3b5aSjmcneill 
122f25f3b5aSjmcneill 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0);
123f25f3b5aSjmcneill 	if ((caps & GPIO_PIN_INPUT) == 0) {
124f25f3b5aSjmcneill 		aprint_error(": pin is not an input pin\n");
125f25f3b5aSjmcneill 		return;
126f25f3b5aSjmcneill 	}
127f25f3b5aSjmcneill 
128f25f3b5aSjmcneill 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_INPUT);
129f25f3b5aSjmcneill 
130f25f3b5aSjmcneill 	sc->sc_smpsw.smpsw_name = device_xname(self);
131f25f3b5aSjmcneill 	switch (type) {
132f25f3b5aSjmcneill 	case GPIOBUTTON_TYPE_POWER:
133f25f3b5aSjmcneill 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
134f25f3b5aSjmcneill 		desc = "Power";
135f25f3b5aSjmcneill 		break;
136f25f3b5aSjmcneill 	case GPIOBUTTON_TYPE_SLEEP:
137f25f3b5aSjmcneill 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
138f25f3b5aSjmcneill 		desc = "Sleep";
139f25f3b5aSjmcneill 		break;
140f25f3b5aSjmcneill 	default:
141f25f3b5aSjmcneill 		panic("%s: impossible", __func__);
142f25f3b5aSjmcneill 	}
143f25f3b5aSjmcneill 
144f25f3b5aSjmcneill 	aprint_naive("\n");
145f25f3b5aSjmcneill 	aprint_normal(": %s button\n", desc);
146f25f3b5aSjmcneill 
147f25f3b5aSjmcneill 	sysmon_pswitch_register(&sc->sc_smpsw);
148f25f3b5aSjmcneill 
149f25f3b5aSjmcneill 	callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
150f25f3b5aSjmcneill 	callout_setfunc(&sc->sc_tick, gpiobutton_tick, sc);
151f25f3b5aSjmcneill 
152*505bbaa9Sjmcneill 	gpiobutton_tick(sc);
153f25f3b5aSjmcneill }
154f25f3b5aSjmcneill 
155f25f3b5aSjmcneill static bool
gpiobutton_is_pressed(struct gpiobutton_softc * sc)156f25f3b5aSjmcneill gpiobutton_is_pressed(struct gpiobutton_softc *sc)
157f25f3b5aSjmcneill {
158f25f3b5aSjmcneill 	int val;
159f25f3b5aSjmcneill 
160f25f3b5aSjmcneill 	val = gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0);
161f25f3b5aSjmcneill 	if (!sc->sc_active_high)
162f25f3b5aSjmcneill 		val = !val;
163f25f3b5aSjmcneill 
164f25f3b5aSjmcneill 	return val;
165f25f3b5aSjmcneill }
166f25f3b5aSjmcneill 
167f25f3b5aSjmcneill static void
gpiobutton_tick(void * priv)168f25f3b5aSjmcneill gpiobutton_tick(void *priv)
169f25f3b5aSjmcneill {
170f25f3b5aSjmcneill 	struct gpiobutton_softc * const sc = priv;
171f25f3b5aSjmcneill 
172*505bbaa9Sjmcneill 	const bool new_state = gpiobutton_is_pressed(sc);
173*505bbaa9Sjmcneill 	if (new_state != sc->sc_state) {
174*505bbaa9Sjmcneill 		sc->sc_state = new_state;
175*505bbaa9Sjmcneill 		sysmon_task_queue_sched(0, gpiobutton_task, sc);
176*505bbaa9Sjmcneill 	}
177*505bbaa9Sjmcneill 	callout_schedule(&sc->sc_tick, GPIOBUTTON_POLL_INTERVAL);
178f25f3b5aSjmcneill }
179f25f3b5aSjmcneill 
180f25f3b5aSjmcneill static void
gpiobutton_task(void * priv)181*505bbaa9Sjmcneill gpiobutton_task(void *priv)
182f25f3b5aSjmcneill {
183f25f3b5aSjmcneill 	struct gpiobutton_softc * const sc = priv;
184f25f3b5aSjmcneill 
185*505bbaa9Sjmcneill 	sysmon_pswitch_event(&sc->sc_smpsw,
186*505bbaa9Sjmcneill 	    sc->sc_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
187f25f3b5aSjmcneill }
188