xref: /netbsd-src/sys/arch/hpcarm/dev/nbppm.c (revision 587ef94054f54d4e5c9e0e007335f9327db832eb)
1*587ef940Skiyohara /*	$NetBSD: nbppm.c,v 1.1 2011/08/06 03:53:40 kiyohara Exp $ */
2*587ef940Skiyohara /*
3*587ef940Skiyohara  * Copyright (c) 2011 KIYOHARA Takashi
4*587ef940Skiyohara  * All rights reserved.
5*587ef940Skiyohara  *
6*587ef940Skiyohara  * Redistribution and use in source and binary forms, with or without
7*587ef940Skiyohara  * modification, are permitted provided that the following conditions
8*587ef940Skiyohara  * are met:
9*587ef940Skiyohara  * 1. Redistributions of source code must retain the above copyright
10*587ef940Skiyohara  *    notice, this list of conditions and the following disclaimer.
11*587ef940Skiyohara  * 2. Redistributions in binary form must reproduce the above copyright
12*587ef940Skiyohara  *    notice, this list of conditions and the following disclaimer in the
13*587ef940Skiyohara  *    documentation and/or other materials provided with the distribution.
14*587ef940Skiyohara  *
15*587ef940Skiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*587ef940Skiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*587ef940Skiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*587ef940Skiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19*587ef940Skiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20*587ef940Skiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21*587ef940Skiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*587ef940Skiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23*587ef940Skiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24*587ef940Skiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*587ef940Skiyohara  * POSSIBILITY OF SUCH DAMAGE.
26*587ef940Skiyohara  */
27*587ef940Skiyohara #include <sys/cdefs.h>
28*587ef940Skiyohara __KERNEL_RCSID(0, "$NetBSD: nbppm.c,v 1.1 2011/08/06 03:53:40 kiyohara Exp $");
29*587ef940Skiyohara 
30*587ef940Skiyohara #include <sys/param.h>
31*587ef940Skiyohara #include <sys/device.h>
32*587ef940Skiyohara #include <sys/errno.h>
33*587ef940Skiyohara 
34*587ef940Skiyohara #include <machine/config_hook.h>
35*587ef940Skiyohara 
36*587ef940Skiyohara #include <arm/xscale/pxa2x0_gpio.h>
37*587ef940Skiyohara 
38*587ef940Skiyohara #include <hpcarm/dev/nbppconvar.h>
39*587ef940Skiyohara 
40*587ef940Skiyohara #include "locators.h"
41*587ef940Skiyohara 
42*587ef940Skiyohara struct nbppm_softc {
43*587ef940Skiyohara 	device_t sc_dev;
44*587ef940Skiyohara 	device_t sc_parent;
45*587ef940Skiyohara 	int sc_tag;
46*587ef940Skiyohara };
47*587ef940Skiyohara 
48*587ef940Skiyohara static int nbppm_match(device_t, cfdata_t, void *);
49*587ef940Skiyohara static void nbppm_attach(device_t, device_t, void *);
50*587ef940Skiyohara 
51*587ef940Skiyohara static int nbppm_critical_intr(void *);
52*587ef940Skiyohara static int nbppm_suspend_intr(void *);
53*587ef940Skiyohara 
54*587ef940Skiyohara CFATTACH_DECL_NEW(nbppm, sizeof(struct nbppm_softc),
55*587ef940Skiyohara     nbppm_match, nbppm_attach, NULL, NULL);
56*587ef940Skiyohara 
57*587ef940Skiyohara 
58*587ef940Skiyohara /* ARGSUSED */
59*587ef940Skiyohara static int
nbppm_match(device_t parent,cfdata_t match,void * aux)60*587ef940Skiyohara nbppm_match(device_t parent, cfdata_t match, void *aux)
61*587ef940Skiyohara {
62*587ef940Skiyohara 	struct nbppcon_attach_args *pcon = aux;
63*587ef940Skiyohara 
64*587ef940Skiyohara 	if (strcmp(pcon->aa_name, match->cf_name) ||
65*587ef940Skiyohara 	    pcon->aa_tag == NBPPCONCF_TAG_DEFAULT)
66*587ef940Skiyohara 		return 0;
67*587ef940Skiyohara 
68*587ef940Skiyohara 	return 1;
69*587ef940Skiyohara }
70*587ef940Skiyohara 
71*587ef940Skiyohara static void
nbppm_attach(device_t parent,device_t self,void * aux)72*587ef940Skiyohara nbppm_attach(device_t parent, device_t self, void *aux)
73*587ef940Skiyohara {
74*587ef940Skiyohara 	struct nbppm_softc *sc = device_private(self);
75*587ef940Skiyohara 	struct nbppcon_attach_args *pcon = aux;
76*587ef940Skiyohara 
77*587ef940Skiyohara 	aprint_naive("\n");
78*587ef940Skiyohara 	aprint_normal("\n");
79*587ef940Skiyohara 
80*587ef940Skiyohara 	sc->sc_dev = self;
81*587ef940Skiyohara 	sc->sc_parent = parent;
82*587ef940Skiyohara 	sc->sc_tag = pcon->aa_tag;
83*587ef940Skiyohara 
84*587ef940Skiyohara 	/* GPIO 0 is Critical Suspend */
85*587ef940Skiyohara 	pxa2x0_gpio_set_function(0, GPIO_IN);
86*587ef940Skiyohara 	if (pxa2x0_gpio_intr_establish(0, IST_EDGE_RISING, IPL_HIGH,
87*587ef940Skiyohara 					nbppm_critical_intr, sc) == NULL)
88*587ef940Skiyohara 		aprint_error_dev(self,
89*587ef940Skiyohara 		    "unable to establish critical interrupt\n");
90*587ef940Skiyohara 
91*587ef940Skiyohara 	/* GPIO 1 is Suspend */
92*587ef940Skiyohara 	pxa2x0_gpio_set_function(1, GPIO_IN);
93*587ef940Skiyohara 	if (pxa2x0_gpio_intr_establish(1, IST_EDGE_BOTH, IPL_HIGH,
94*587ef940Skiyohara 						nbppm_suspend_intr, sc) == NULL)
95*587ef940Skiyohara 		aprint_error_dev(self,
96*587ef940Skiyohara 		    "unable to establish suspend interrupt\n");
97*587ef940Skiyohara 
98*587ef940Skiyohara 	return;
99*587ef940Skiyohara }
100*587ef940Skiyohara 
101*587ef940Skiyohara static int
nbppm_critical_intr(void * arg)102*587ef940Skiyohara nbppm_critical_intr(void *arg)
103*587ef940Skiyohara {
104*587ef940Skiyohara 	struct nbppm_softc *sc = arg;
105*587ef940Skiyohara 
106*587ef940Skiyohara 	aprint_normal_dev(sc->sc_dev, "Battery Low\n");
107*587ef940Skiyohara 	return 0;
108*587ef940Skiyohara }
109*587ef940Skiyohara 
110*587ef940Skiyohara static int
nbppm_suspend_intr(void * arg)111*587ef940Skiyohara nbppm_suspend_intr(void *arg)
112*587ef940Skiyohara {
113*587ef940Skiyohara 	struct nbppm_softc *sc = arg;
114*587ef940Skiyohara 
115*587ef940Skiyohara 	aprint_verbose_dev(sc->sc_dev, "lid closed\n");
116*587ef940Skiyohara 
117*587ef940Skiyohara 	return 0;
118*587ef940Skiyohara }
119