netwalker_lid.c revision 1.1.2.2 1 1.1.2.2 rmind /* $NetBSD: netwalker_lid.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_lid.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 LID_PIN_INPUT 0
46 1.1.2.2 rmind #define LID_NPINS 1
47 1.1.2.2 rmind
48 1.1.2.2 rmind struct netwalker_lid_softc {
49 1.1.2.2 rmind device_t sc_dev;
50 1.1.2.2 rmind void *sc_gpio;
51 1.1.2.2 rmind void *sc_intr;
52 1.1.2.2 rmind
53 1.1.2.2 rmind struct gpio_pinmap sc_map;
54 1.1.2.2 rmind int sc_map_pins[LID_NPINS];
55 1.1.2.2 rmind
56 1.1.2.2 rmind struct sysmon_pswitch sc_smpsw;
57 1.1.2.2 rmind int sc_state;
58 1.1.2.2 rmind };
59 1.1.2.2 rmind
60 1.1.2.2 rmind static int netwalker_lid_match(device_t, cfdata_t, void *);
61 1.1.2.2 rmind static void netwalker_lid_attach(device_t, device_t, void *);
62 1.1.2.2 rmind static int netwalker_lid_detach(device_t, int);
63 1.1.2.2 rmind
64 1.1.2.2 rmind CFATTACH_DECL_NEW(netwalker_lid, sizeof(struct netwalker_lid_softc),
65 1.1.2.2 rmind netwalker_lid_match, netwalker_lid_attach, netwalker_lid_detach, NULL);
66 1.1.2.2 rmind
67 1.1.2.2 rmind static void netwalker_lid_refresh(void *);
68 1.1.2.2 rmind static int netwalker_lid_intr(void *);
69 1.1.2.2 rmind
70 1.1.2.2 rmind
71 1.1.2.2 rmind static int
72 1.1.2.2 rmind netwalker_lid_match(device_t parent, cfdata_t cf, void * aux)
73 1.1.2.2 rmind {
74 1.1.2.2 rmind struct gpio_attach_args *ga = aux;
75 1.1.2.2 rmind
76 1.1.2.2 rmind if (strcmp(ga->ga_dvname, cf->cf_name))
77 1.1.2.2 rmind return 0;
78 1.1.2.2 rmind if (ga->ga_offset == -1)
79 1.1.2.2 rmind return 0;
80 1.1.2.2 rmind /* check that we have enough pins */
81 1.1.2.2 rmind if (gpio_npins(ga->ga_mask) != LID_NPINS) {
82 1.1.2.2 rmind aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
83 1.1.2.2 rmind ga->ga_mask);
84 1.1.2.2 rmind return 0;
85 1.1.2.2 rmind }
86 1.1.2.2 rmind
87 1.1.2.2 rmind return 1;
88 1.1.2.2 rmind }
89 1.1.2.2 rmind
90 1.1.2.2 rmind static void
91 1.1.2.2 rmind netwalker_lid_attach(device_t parent, device_t self, void *aux)
92 1.1.2.2 rmind {
93 1.1.2.2 rmind struct netwalker_lid_softc *sc = device_private(self);
94 1.1.2.2 rmind struct gpio_attach_args *ga = aux;
95 1.1.2.2 rmind int caps;
96 1.1.2.2 rmind
97 1.1.2.2 rmind sc->sc_dev = self;
98 1.1.2.2 rmind sc->sc_gpio = ga->ga_gpio;
99 1.1.2.2 rmind
100 1.1.2.2 rmind /* map pins */
101 1.1.2.2 rmind sc->sc_map.pm_map = sc->sc_map_pins;
102 1.1.2.2 rmind if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, &sc->sc_map)) {
103 1.1.2.2 rmind aprint_error(": couldn't map the pins\n");
104 1.1.2.2 rmind return;
105 1.1.2.2 rmind }
106 1.1.2.2 rmind
107 1.1.2.2 rmind /* configure the input pin */
108 1.1.2.2 rmind caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT);
109 1.1.2.2 rmind if (!(caps & GPIO_PIN_INPUT)) {
110 1.1.2.2 rmind aprint_error(": pin is unable to read input\n");
111 1.1.2.2 rmind gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
112 1.1.2.2 rmind return;
113 1.1.2.2 rmind }
114 1.1.2.2 rmind gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT,
115 1.1.2.2 rmind GPIO_PIN_INPUT);
116 1.1.2.2 rmind
117 1.1.2.2 rmind sc->sc_intr = intr_establish(GPIO3_IRQBASE + ga->ga_offset,
118 1.1.2.2 rmind IPL_VM, IST_EDGE_BOTH, netwalker_lid_intr, sc);
119 1.1.2.2 rmind if (sc->sc_intr == NULL) {
120 1.1.2.2 rmind aprint_error(": couldn't establish interrupt\n");
121 1.1.2.2 rmind gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
122 1.1.2.2 rmind return;
123 1.1.2.2 rmind }
124 1.1.2.2 rmind
125 1.1.2.2 rmind aprint_normal(": NETWALKER lid switch\n");
126 1.1.2.2 rmind aprint_naive(": NETWALKER lid switch\n");
127 1.1.2.2 rmind
128 1.1.2.2 rmind if (!pmf_device_register(sc->sc_dev, NULL, NULL)) {
129 1.1.2.2 rmind aprint_error_dev(sc->sc_dev,
130 1.1.2.2 rmind "couldn't establish lid handler\n");
131 1.1.2.2 rmind }
132 1.1.2.2 rmind
133 1.1.2.2 rmind sysmon_task_queue_init();
134 1.1.2.2 rmind sc->sc_smpsw.smpsw_name = device_xname(self);
135 1.1.2.2 rmind sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
136 1.1.2.2 rmind sc->sc_state = PSWITCH_EVENT_RELEASED;
137 1.1.2.2 rmind sysmon_pswitch_register(&sc->sc_smpsw);
138 1.1.2.2 rmind }
139 1.1.2.2 rmind
140 1.1.2.2 rmind static int
141 1.1.2.2 rmind netwalker_lid_detach(device_t self, int flags)
142 1.1.2.2 rmind {
143 1.1.2.2 rmind struct netwalker_lid_softc *sc = device_private(self);
144 1.1.2.2 rmind
145 1.1.2.2 rmind if (sc->sc_intr != NULL)
146 1.1.2.2 rmind intr_disestablish(sc->sc_intr);
147 1.1.2.2 rmind
148 1.1.2.2 rmind gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
149 1.1.2.2 rmind pmf_device_deregister(self);
150 1.1.2.2 rmind sysmon_task_queue_fini();
151 1.1.2.2 rmind return 0;
152 1.1.2.2 rmind }
153 1.1.2.2 rmind
154 1.1.2.2 rmind static int
155 1.1.2.2 rmind netwalker_lid_intr(void *v)
156 1.1.2.2 rmind {
157 1.1.2.2 rmind struct netwalker_lid_softc *sc = v;
158 1.1.2.2 rmind
159 1.1.2.2 rmind sysmon_task_queue_sched(0, netwalker_lid_refresh, sc);
160 1.1.2.2 rmind return 1;
161 1.1.2.2 rmind }
162 1.1.2.2 rmind
163 1.1.2.2 rmind static void
164 1.1.2.2 rmind netwalker_lid_refresh(void *v)
165 1.1.2.2 rmind {
166 1.1.2.2 rmind struct netwalker_lid_softc *sc = v;
167 1.1.2.2 rmind int lid;
168 1.1.2.2 rmind int event;
169 1.1.2.2 rmind
170 1.1.2.2 rmind lid = gpio_pin_read(sc->sc_gpio, &sc->sc_map, LID_PIN_INPUT);
171 1.1.2.2 rmind event = (lid == GPIO_PIN_HIGH) ?
172 1.1.2.2 rmind PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED;
173 1.1.2.2 rmind
174 1.1.2.2 rmind /* crude way to avoid duplicate events */
175 1.1.2.2 rmind if(event == sc->sc_state)
176 1.1.2.2 rmind return;
177 1.1.2.2 rmind
178 1.1.2.2 rmind sc->sc_state = event;
179 1.1.2.2 rmind /* report the event */
180 1.1.2.2 rmind sysmon_pswitch_event(&sc->sc_smpsw, event);
181 1.1.2.2 rmind }
182