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