xref: /netbsd-src/sys/arch/evbarm/nslu2/nslu2_buttons.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: nslu2_buttons.c,v 1.3 2008/04/28 20:23:17 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Steve C. Woodford.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: nslu2_buttons.c,v 1.3 2008/04/28 20:23:17 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 
39 #include <dev/sysmon/sysmonvar.h>
40 #include <dev/sysmon/sysmon_taskq.h>
41 
42 #include <arm/xscale/ixp425reg.h>
43 #include <arm/xscale/ixp425var.h>
44 
45 #include <evbarm/nslu2/nslu2reg.h>
46 
47 struct slugbutt_softc {
48 	struct device sc_dev;
49 	struct sysmon_pswitch sc_smpwr;
50 	struct sysmon_pswitch sc_smrst;
51 };
52 
53 static int slugbutt_attached;
54 
55 #define	SLUGBUTT_PWR_BIT	(1u << GPIO_BUTTON_PWR)
56 #define	SLUGBUTT_RST_BIT	(1u << GPIO_BUTTON_RST)
57 
58 static void
59 power_event(void *arg)
60 {
61 	struct slugbutt_softc *sc = arg;
62 
63 	sysmon_pswitch_event(&sc->sc_smpwr, PSWITCH_EVENT_PRESSED);
64 }
65 
66 static int
67 power_intr(void *arg)
68 {
69 	struct slugbutt_softc *sc = arg;
70 	int rv;
71 
72 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT);
73 
74 	rv = sysmon_task_queue_sched(0, power_event, sc);
75 	if (rv) {
76 		printf("%s: WARNING: unable to queue power button "
77 		    "callback: %d\n", sc->sc_dev.dv_xname, rv);
78 	}
79 
80 	return (1);
81 }
82 
83 static void
84 reset_event(void *arg)
85 {
86 	struct slugbutt_softc *sc = arg;
87 
88 	sysmon_pswitch_event(&sc->sc_smrst, PSWITCH_EVENT_PRESSED);
89 }
90 
91 static int
92 reset_intr(void *arg)
93 {
94 	struct slugbutt_softc *sc = arg;
95 	int rv;
96 
97 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_RST_BIT);
98 
99 	rv = sysmon_task_queue_sched(0, reset_event, sc);
100 	if (rv) {
101 		printf("%s: WARNING: unable to queue reset button "
102 		    "callback: %d\n", sc->sc_dev.dv_xname, rv);
103 	}
104 
105 	return (1);
106 }
107 
108 static void
109 slugbutt_deferred(struct device *self)
110 {
111 	struct slugbutt_softc *sc = (struct slugbutt_softc *) self;
112 	struct ixp425_softc *ixsc = ixp425_softc;
113 	uint32_t reg;
114 
115 	/* Configure the GPIO pins as inputs */
116 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
117 	reg |= SLUGBUTT_PWR_BIT | SLUGBUTT_RST_BIT;
118 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
119 
120 	/* Configure the input type: Falling edge */
121 	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR));
122 	reg &= ~GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_MASK);
123 	reg |= GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_EDG_FALLING);
124 	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR), reg);
125 
126 	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST));
127 	reg &= ~GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_MASK);
128 	reg |= GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_EDG_FALLING);
129 	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST), reg);
130 
131 	/* Clear any existing interrupt */
132 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT |
133 	    SLUGBUTT_RST_BIT);
134 
135 	sysmon_task_queue_init();
136 
137 	sc->sc_smpwr.smpsw_name = sc->sc_dev.dv_xname;
138 	sc->sc_smpwr.smpsw_type = PSWITCH_TYPE_POWER;
139 
140 	if (sysmon_pswitch_register(&sc->sc_smpwr) != 0) {
141 		printf("%s: unable to register power button with sysmon\n",
142 		    sc->sc_dev.dv_xname);
143 		return;
144 	}
145 
146 	sc->sc_smrst.smpsw_name = sc->sc_dev.dv_xname;
147 	sc->sc_smrst.smpsw_type = PSWITCH_TYPE_RESET;
148 
149 	if (sysmon_pswitch_register(&sc->sc_smrst) != 0) {
150 		printf("%s: unable to register reset button with sysmon\n",
151 		    sc->sc_dev.dv_xname);
152 		return;
153 	}
154 
155 	/* Hook the interrupts */
156 	ixp425_intr_establish(BUTTON_PWR_INT, IPL_TTY, power_intr, sc);
157 	ixp425_intr_establish(BUTTON_RST_INT, IPL_TTY, reset_intr, sc);
158 }
159 
160 
161 static int
162 slugbutt_match(struct device *parent, struct cfdata *cf, void *arg)
163 {
164 
165 	return (slugbutt_attached == 0);
166 }
167 
168 static void
169 slugbutt_attach(struct device *parent, struct device *self, void *arg)
170 {
171 
172 	slugbutt_attached = 1;
173 
174 	aprint_normal(": Power and Reset buttons\n");
175 
176 	/* Defer, to ensure ixp425_softc has been initialised */
177 	config_interrupts(self, slugbutt_deferred);
178 }
179 
180 CFATTACH_DECL(slugbutt, sizeof(struct slugbutt_softc),
181     slugbutt_match, slugbutt_attach, NULL, NULL);
182