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