1 /* $NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 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 "locators.h"
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: gpiobutton.c,v 1.3 2015/10/04 18:35:44 jmcneill Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/gpio.h>
41 
42 #include <dev/sysmon/sysmonvar.h>
43 #include <dev/sysmon/sysmon_taskq.h>
44 
45 #include <dev/gpio/gpiovar.h>
46 
47 #define GPIOBUTTON_POLL_INTERVAL        mstohz(200)
48 
49 #define GPIOBUTTON_POLARITY_MASK        0x80
50 #define GPIOBUTTON_POLARITY_ACTIVE_LOW  0
51 #define GPIOBUTTON_POLARITY_ACTIVE_HIGH 1
52 #define GPIOBUTTON_TYPE_MASK            0x0f
53 #define GPIOBUTTON_TYPE_POWER           1
54 #define GPIOBUTTON_TYPE_SLEEP           2
55 
56 static int          gpiobutton_match(device_t, cfdata_t, void *);
57 static void         gpiobutton_attach(device_t, device_t, void *);
58 
59 struct gpiobutton_softc {
60           device_t            sc_dev;
61           void                          *sc_gpio;
62           struct gpio_pinmap  sc_map;
63           int                           sc_pinmap[1];
64           bool                          sc_active_high;
65 
66           struct sysmon_pswitch         sc_smpsw;
67 
68           callout_t           sc_tick;
69           bool                          sc_state;
70 };
71 
72 static bool         gpiobutton_is_pressed(struct gpiobutton_softc *);
73 static void         gpiobutton_tick(void *);
74 static void         gpiobutton_task(void *);
75 
76 CFATTACH_DECL_NEW(gpiobutton, sizeof(struct gpiobutton_softc),
77           gpiobutton_match, gpiobutton_attach, NULL, NULL);
78 
79 static int
gpiobutton_match(device_t parent,cfdata_t cf,void * aux)80 gpiobutton_match(device_t parent, cfdata_t cf, void *aux)
81 {
82           struct gpio_attach_args * const ga = aux;
83 
84           if (strcmp(ga->ga_dvname, cf->cf_name) != 0)
85                     return 0;
86 
87           if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1)
88                     return 0;
89 
90           const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
91 
92           switch (type) {
93           case GPIOBUTTON_TYPE_POWER:
94           case GPIOBUTTON_TYPE_SLEEP:
95                     return 1;
96           default:
97                     return 0;
98           }
99 }
100 
101 static void
gpiobutton_attach(device_t parent,device_t self,void * aux)102 gpiobutton_attach(device_t parent, device_t self, void *aux)
103 {
104           struct gpiobutton_softc * const sc = device_private(self);
105           struct gpio_attach_args * const ga = aux;
106           const char *desc;
107           int caps;
108 
109           const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
110           const u_int pol = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_POLARITY_MASK);
111 
112           sc->sc_dev = self;
113           sc->sc_gpio = ga->ga_gpio;
114           sc->sc_map.pm_map = sc->sc_pinmap;
115           if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
116               &sc->sc_map)) {
117                     aprint_error(": couldn't map pins\n");
118                     return;
119           }
120           sc->sc_active_high = pol == GPIOBUTTON_POLARITY_ACTIVE_HIGH;
121 
122           caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0);
123           if ((caps & GPIO_PIN_INPUT) == 0) {
124                     aprint_error(": pin is not an input pin\n");
125                     return;
126           }
127 
128           gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_INPUT);
129 
130           sc->sc_smpsw.smpsw_name = device_xname(self);
131           switch (type) {
132           case GPIOBUTTON_TYPE_POWER:
133                     sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
134                     desc = "Power";
135                     break;
136           case GPIOBUTTON_TYPE_SLEEP:
137                     sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
138                     desc = "Sleep";
139                     break;
140           default:
141                     panic("%s: impossible", __func__);
142           }
143 
144           aprint_naive("\n");
145           aprint_normal(": %s button\n", desc);
146 
147           sysmon_pswitch_register(&sc->sc_smpsw);
148 
149           callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
150           callout_setfunc(&sc->sc_tick, gpiobutton_tick, sc);
151 
152           gpiobutton_tick(sc);
153 }
154 
155 static bool
gpiobutton_is_pressed(struct gpiobutton_softc * sc)156 gpiobutton_is_pressed(struct gpiobutton_softc *sc)
157 {
158           int val;
159 
160           val = gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0);
161           if (!sc->sc_active_high)
162                     val = !val;
163 
164           return val;
165 }
166 
167 static void
gpiobutton_tick(void * priv)168 gpiobutton_tick(void *priv)
169 {
170           struct gpiobutton_softc * const sc = priv;
171 
172           const bool new_state = gpiobutton_is_pressed(sc);
173           if (new_state != sc->sc_state) {
174                     sc->sc_state = new_state;
175                     sysmon_task_queue_sched(0, gpiobutton_task, sc);
176           }
177           callout_schedule(&sc->sc_tick, GPIOBUTTON_POLL_INTERVAL);
178 }
179 
180 static void
gpiobutton_task(void * priv)181 gpiobutton_task(void *priv)
182 {
183           struct gpiobutton_softc * const sc = priv;
184 
185           sysmon_pswitch_event(&sc->sc_smpsw,
186               sc->sc_state ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
187 }
188