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