gpiobutton.c revision 1.2 1 /* $NetBSD: gpiobutton.c,v 1.2 2015/05/30 17:12:16 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) 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.2 2015/05/30 17:12:16 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/workqueue.h>
41 #include <sys/gpio.h>
42
43 #include <dev/sysmon/sysmonvar.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 struct workqueue *sc_wq;
69 callout_t sc_tick;
70 bool sc_state;
71 struct work sc_work;
72 };
73
74 static bool gpiobutton_is_pressed(struct gpiobutton_softc *);
75 static void gpiobutton_tick(void *);
76 static void gpiobutton_task(struct work *, void *);
77
78 CFATTACH_DECL_NEW(gpiobutton, sizeof(struct gpiobutton_softc),
79 gpiobutton_match, gpiobutton_attach, NULL, NULL);
80
81 static int
82 gpiobutton_match(device_t parent, cfdata_t cf, void *aux)
83 {
84 struct gpio_attach_args * const ga = aux;
85
86 if (strcmp(ga->ga_dvname, cf->cf_name) != 0)
87 return 0;
88
89 if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1)
90 return 0;
91
92 const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
93
94 switch (type) {
95 case GPIOBUTTON_TYPE_POWER:
96 case GPIOBUTTON_TYPE_SLEEP:
97 return 1;
98 default:
99 return 0;
100 }
101 }
102
103 static void
104 gpiobutton_attach(device_t parent, device_t self, void *aux)
105 {
106 struct gpiobutton_softc * const sc = device_private(self);
107 struct gpio_attach_args * const ga = aux;
108 const char *desc;
109 int caps, error;
110
111 const u_int type = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_TYPE_MASK);
112 const u_int pol = __SHIFTOUT(ga->ga_flags, GPIOBUTTON_POLARITY_MASK);
113
114 sc->sc_dev = self;
115 sc->sc_gpio = ga->ga_gpio;
116 sc->sc_map.pm_map = sc->sc_pinmap;
117 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
118 &sc->sc_map)) {
119 aprint_error(": couldn't map pins\n");
120 return;
121 }
122 sc->sc_active_high = pol == GPIOBUTTON_POLARITY_ACTIVE_HIGH;
123
124 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0);
125 if ((caps & GPIO_PIN_INPUT) == 0) {
126 aprint_error(": pin is not an input pin\n");
127 return;
128 }
129
130 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_INPUT);
131
132 sc->sc_smpsw.smpsw_name = device_xname(self);
133 switch (type) {
134 case GPIOBUTTON_TYPE_POWER:
135 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
136 desc = "Power";
137 break;
138 case GPIOBUTTON_TYPE_SLEEP:
139 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
140 desc = "Sleep";
141 break;
142 default:
143 panic("%s: impossible", __func__);
144 }
145
146 aprint_naive("\n");
147 aprint_normal(": %s button\n", desc);
148
149 sysmon_pswitch_register(&sc->sc_smpsw);
150
151 callout_init(&sc->sc_tick, CALLOUT_MPSAFE);
152 callout_setfunc(&sc->sc_tick, gpiobutton_tick, sc);
153
154 error = workqueue_create(&sc->sc_wq, device_xname(self),
155 gpiobutton_task, sc, PRI_NONE, IPL_VM, WQ_MPSAFE);
156 if (error) {
157 aprint_error_dev(self, "couldn't create workqueue: %d\n",
158 error);
159 return;
160 }
161
162 gpiobutton_task(&sc->sc_work, sc);
163 }
164
165 static bool
166 gpiobutton_is_pressed(struct gpiobutton_softc *sc)
167 {
168 int val;
169
170 val = gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0);
171 if (!sc->sc_active_high)
172 val = !val;
173
174 return val;
175 }
176
177 static void
178 gpiobutton_tick(void *priv)
179 {
180 struct gpiobutton_softc * const sc = priv;
181
182 workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
183 }
184
185 static void
186 gpiobutton_task(struct work *wk, void *priv)
187 {
188 struct gpiobutton_softc * const sc = priv;
189
190 const bool new_state = gpiobutton_is_pressed(sc);
191 if (new_state != sc->sc_state) {
192 aprint_debug_dev(sc->sc_dev, "button pressed\n");
193 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
194 sc->sc_state = new_state;
195 }
196
197 callout_schedule(&sc->sc_tick, GPIOBUTTON_POLL_INTERVAL);
198 }
199