xref: /netbsd/sys/dev/hpc/button.c (revision 6550d01e)
1 /*	$NetBSD: button.c,v 1.15 2009/05/12 14:22:39 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999-2001
5  *         TAKEMURA Shin1 and PocketBSD Project. All rights reserved.
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the PocketBSD project
18  *	and its contributors.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.15 2009/05/12 14:22:39 cegger Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 
44 #include <sys/bus.h>
45 
46 #include <machine/config_hook.h>
47 #include <machine/platid.h>
48 #include <machine/platid_mask.h>
49 
50 #include <dev/hpc/hpciovar.h>
51 
52 #include "locators.h"
53 
54 struct button_softc {
55 	struct device sc_dev;
56 	hpcio_chip_t sc_hc;
57 	hpcio_intr_handle_t sc_intr_handle;
58 	int sc_port;
59 	long sc_id;
60 	int sc_active;
61 	config_hook_tag sc_hook_tag;
62 	config_hook_tag sc_ghook_tag;
63 };
64 
65 static int	button_match(device_t, cfdata_t, void *);
66 static void	button_attach(device_t, device_t, void *);
67 static int	button_intr(void *);
68 static int	button_state(void *, int, long, void *);
69 
70 CFATTACH_DECL(button, sizeof(struct button_softc),
71     button_match, button_attach, NULL, NULL);
72 
73 int
74 button_match(device_t parent, cfdata_t match, void *aux)
75 {
76 	struct hpcio_attach_args *haa = aux;
77 	platid_mask_t mask;
78 
79 	if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
80 		return (0);
81 	if (match->cf_loc[HPCIOIFCF_PLATFORM] == 0)
82 		return (0);
83 	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
84 	return (platid_match(&platid, &mask));
85 }
86 
87 void
88 button_attach(device_t parent, device_t self, void *aux)
89 {
90 	struct hpcio_attach_args *haa = aux;
91 	int *loc;
92 	struct button_softc *sc = (void*)self;
93 	int mode;
94 
95 	loc = device_cfdata(&sc->sc_dev)->cf_loc;
96 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
97 	sc->sc_port = loc[HPCIOIFCF_PORT];
98 	sc->sc_id = loc[HPCIOIFCF_ID];
99 	sc->sc_active = loc[HPCIOIFCF_ACTIVE];
100 	printf(" port=%d id=%ld active=%s",
101 	    sc->sc_port, sc->sc_id, sc->sc_active ? "high" : "low");
102 
103 	mode = HPCIO_INTR_HOLD;
104 	if (loc[HPCIOIFCF_LEVEL] != HPCIOIFCF_LEVEL_DEFAULT) {
105 		mode |= HPCIO_INTR_LEVEL;
106 		if (loc[HPCIOIFCF_LEVEL] == 0)
107 			mode |= HPCIO_INTR_LOW;
108 		else
109 			mode |= HPCIO_INTR_HIGH;
110 		printf(" sense=level");
111 	} else {
112 		mode |= HPCIO_INTR_EDGE;
113 		switch (loc[HPCIOIFCF_EDGE]) {
114 		case 1:
115 			mode |= HPCIO_INTR_POSEDGE;
116 			break;
117 		case 2:
118 			mode |= HPCIO_INTR_NEGEDGE;
119 			break;
120 		case 0:
121 		case 3:
122 		case HPCIOMANCF_EDGE_DEFAULT:
123 			mode |= HPCIO_INTR_POSEDGE;
124 			mode |= HPCIO_INTR_NEGEDGE;
125 			break;
126 		default:
127 			printf("%s(%d): invalid configuration, edge=%d",
128 			    __FILE__, __LINE__, loc[HPCIOIFCF_EDGE]);
129 			break;
130 		}
131 		printf(" sense=edge");
132 	}
133 
134 	if (sc->sc_port == HPCIOIFCF_PORT_DEFAULT ||
135 	    sc->sc_id == HPCIOIFCF_ID_DEFAULT)
136 		printf(" (ignored)");
137 	else
138 		sc->sc_intr_handle =
139 		    hpcio_intr_establish(sc->sc_hc, sc->sc_port,
140 			mode, button_intr, sc);
141 	sc->sc_ghook_tag = config_hook(CONFIG_HOOK_GET, sc->sc_id,
142 	    CONFIG_HOOK_SHARE, button_state, sc);
143 	printf("\n");
144 }
145 
146 int
147 button_state(void *ctx, int type, long id, void *msg)
148 {
149 	struct button_softc *sc = ctx;
150 
151 	if (type != CONFIG_HOOK_GET || id != sc->sc_id)
152 		return (1);
153 
154 	if (CONFIG_HOOK_VALUEP(msg))
155 		return (1);
156 
157 	*(int*)msg = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
158 	return (0);
159 }
160 
161 int
162 button_intr(void *ctx)
163 {
164 	struct button_softc *sc = ctx;
165 	int on;
166 
167 	on = (hpcio_portread(sc->sc_hc, sc->sc_port) == sc->sc_active);
168 
169 	/* Clear interrupt */
170 	hpcio_intr_clear(sc->sc_hc, sc->sc_intr_handle);
171 
172 	config_hook_call(CONFIG_HOOK_BUTTONEVENT, sc->sc_id, (void*)on);
173 
174 	return (0);
175 }
176