1 /*        $NetBSD: netwalker_pwr.c,v 1.1 2014/05/06 11:08:51 hkenken Exp $      */
2 
3 /*
4  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
5  * Written by Hashimoto Kenichi for Genetec Corporation.
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 GENETEC CORPORATION ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netwalker_pwr.c,v 1.1 2014/05/06 11:08:51 hkenken Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/gpio.h>
36 
37 #include <dev/gpio/gpiovar.h>
38 #include <dev/sysmon/sysmonvar.h>
39 #include <dev/sysmon/sysmon_taskq.h>
40 
41 #include <dev/gpio/gpiovar.h>
42 
43 #include <evbarm/netwalker/netwalker.h>
44 
45 #define PWR_PIN_INPUT         0
46 #define PWR_NPINS   1
47 
48 
49 struct netwalker_pwr_softc {
50           device_t sc_dev;
51           void *sc_gpio;
52           void *sc_intr;
53 
54           struct gpio_pinmap sc_map;
55           int sc_map_pins[PWR_NPINS];
56 
57           struct sysmon_pswitch sc_smpsw;
58           int sc_state;
59 };
60 
61 static int netwalker_pwr_match(device_t, cfdata_t, void *);
62 static void netwalker_pwr_attach(device_t, device_t, void *);
63 static int netwalker_pwr_detach(device_t, int);
64 
65 CFATTACH_DECL_NEW(netwalker_pwr, sizeof(struct netwalker_pwr_softc),
66     netwalker_pwr_match, netwalker_pwr_attach, netwalker_pwr_detach, NULL);
67 
68 static void netwalker_pwr_refresh(void *);
69 static int netwalker_pwr_intr(void *);
70 
71 
72 static int
netwalker_pwr_match(device_t parent,cfdata_t cf,void * aux)73 netwalker_pwr_match(device_t parent, cfdata_t cf, void * aux)
74 {
75           struct gpio_attach_args *ga = aux;
76 
77           if (strcmp(ga->ga_dvname, cf->cf_name))
78                     return 0;
79           if (ga->ga_offset == -1)
80                     return 0;
81           /* check that we have enough pins */
82           if (gpio_npins(ga->ga_mask) != PWR_NPINS) {
83                     aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
84                         ga->ga_mask);
85                     return 0;
86           }
87 
88           return 1;
89 }
90 
91 static void
netwalker_pwr_attach(device_t parent,device_t self,void * aux)92 netwalker_pwr_attach(device_t parent, device_t self, void *aux)
93 {
94           struct netwalker_pwr_softc *sc = device_private(self);
95           struct gpio_attach_args *ga = aux;
96           int caps;
97 
98           sc->sc_dev = self;
99           sc->sc_gpio = ga->ga_gpio;
100 
101           /* map pins */
102           sc->sc_map.pm_map = sc->sc_map_pins;
103           if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, &sc->sc_map)) {
104                     aprint_error(": couldn't map the pins\n");
105                     return;
106           }
107 
108           /* configure the input pin */
109           caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, PWR_PIN_INPUT);
110           if (!(caps & GPIO_PIN_INPUT)) {
111                     aprint_error(": pin is unable to read input\n");
112                     gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
113                     return;
114           }
115           gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, PWR_PIN_INPUT,
116               GPIO_PIN_INPUT);
117 
118           sc->sc_intr = intr_establish(GPIO1_IRQBASE + ga->ga_offset,
119               IPL_VM, IST_EDGE_BOTH, netwalker_pwr_intr, sc);
120           if (sc->sc_intr == NULL) {
121                     aprint_error(": couldn't establish interrupt\n");
122                     gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
123                     return;
124           }
125 
126           aprint_normal(": NETWALKER power button\n");
127           aprint_naive(": NETWALKER power button\n");
128 
129           if (!pmf_device_register(sc->sc_dev, NULL, NULL)) {
130                     aprint_error_dev(sc->sc_dev,
131                         "couldn't establish power handler\n");
132           }
133 
134           sysmon_task_queue_init();
135           sc->sc_smpsw.smpsw_name = device_xname(self);
136           sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
137           sc->sc_state = PSWITCH_EVENT_RELEASED;
138           sysmon_pswitch_register(&sc->sc_smpsw);
139 }
140 
141 static int
netwalker_pwr_detach(device_t self,int flags)142 netwalker_pwr_detach(device_t self, int flags)
143 {
144           struct netwalker_pwr_softc *sc = device_private(self);
145 
146           if (sc->sc_intr != NULL)
147                     intr_disestablish(sc->sc_intr);
148 
149           gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
150           pmf_device_deregister(self);
151           sysmon_task_queue_fini();
152 
153           return 0;
154 }
155 
156 static int
netwalker_pwr_intr(void * v)157 netwalker_pwr_intr(void *v)
158 {
159           struct netwalker_pwr_softc *sc = v;
160 
161           sysmon_task_queue_sched(0, netwalker_pwr_refresh, sc);
162           return 1;
163 }
164 
165 static void
netwalker_pwr_refresh(void * v)166 netwalker_pwr_refresh(void *v)
167 {
168           struct netwalker_pwr_softc *sc = v;
169           int pwr;
170           int event;
171 
172           pwr = gpio_pin_read(sc->sc_gpio, &sc->sc_map, PWR_PIN_INPUT);
173           event = (pwr == GPIO_PIN_HIGH) ?
174               PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED;
175 
176           /* crude way to avoid duplicate events */
177           if(event == sc->sc_state)
178                     return;
179 
180           sc->sc_state = event;
181           /* report the event */
182           sysmon_pswitch_event(&sc->sc_smpsw, event);
183 }
184