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