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