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