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