Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sgimips / hpc / panel.c
blobc084686faa6b8b003c305e76c0c670d8c9ca371c
1 /* $NetBSD: adb_bus.c,v 1.8 2008/04/29 06:53:02 martin Exp $ */
3 /*-
4 * Copyright (c) 2009 Michael Lorenz
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: adb_bus.c,v 1.8 2008/04/29 06:53:02 martin Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/kthread.h>
39 #include <sys/bus.h>
41 #include <dev/sysmon/sysmonvar.h>
42 #include <dev/sysmon/sysmon_taskq.h>
44 #include <machine/autoconf.h>
45 #include <machine/machtype.h>
47 #include <sgimips/ioc/iocreg.h>
48 #include <sgimips/hpc/hpcvar.h>
49 #include <sgimips/hpc/hpcreg.h>
51 struct panel_softc {
52 device_t sc_dev;
53 struct sysmon_pswitch sc_pbutton;
54 bus_space_tag_t sc_tag;
55 bus_space_handle_t sc_hreg;
56 int sc_last, sc_fired;
59 static int panel_match(device_t, cfdata_t, void *);
60 static void panel_attach(device_t, device_t, void *);
62 static int panel_intr(void *);
63 static void panel_powerbutton(void *);
65 CFATTACH_DECL_NEW(panel, sizeof(struct panel_softc),
66 panel_match,
67 panel_attach,
68 NULL,
69 NULL);
71 static int
72 panel_match(device_t parent, cfdata_t match, void *aux)
74 if (mach_type == MACH_SGI_IP22)
75 return 1;
77 return 0;
80 static void
81 panel_attach(device_t parent, device_t self, void *aux)
83 struct panel_softc *sc;
84 struct hpc_attach_args *haa;
86 sc = device_private(self);
87 sc->sc_dev = self;
88 haa = aux;
89 sc->sc_tag = haa->ha_st;
90 sc->sc_fired = 0;
92 aprint_normal("\n");
93 if (bus_space_subregion(haa->ha_st, haa->ha_sh, haa->ha_devoff,
94 0x1, /* just a single register */
95 &sc->sc_hreg)) {
96 aprint_error(": unable to map panel register\n");
97 return;
100 if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
101 panel_intr, sc)) == NULL) {
102 printf(": unable to establish interrupt!\n");
103 return;
106 sc->sc_last = 0;
108 sysmon_task_queue_init();
109 memset(&sc->sc_pbutton, 0, sizeof(struct sysmon_pswitch));
110 sc->sc_pbutton.smpsw_name = device_xname(sc->sc_dev);
111 sc->sc_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
112 if (sysmon_pswitch_register(&sc->sc_pbutton) != 0)
113 aprint_error_dev(sc->sc_dev,
114 "unable to register power button with sysmon\n");
115 pmf_device_register(self, NULL, NULL);
118 static int
119 panel_intr(void *cookie)
121 struct panel_softc *sc = cookie;
122 uint8_t reg;
124 reg = bus_space_read_1(sc->sc_tag, sc->sc_hreg, 0);
125 bus_space_write_1(sc->sc_tag, sc->sc_hreg, 0,
126 IOC_PANEL_VDOWN_IRQ | IOC_PANEL_VUP_IRQ | IOC_PANEL_POWER_IRQ);
127 if ((reg & IOC_PANEL_POWER_IRQ) == 0) {
128 if (!sc->sc_fired)
129 sysmon_task_queue_sched(0, panel_powerbutton, sc);
130 sc->sc_fired = 1;
132 if (time_second == sc->sc_last)
133 return 1;
134 sc->sc_last = time_second;
135 if ((reg & IOC_PANEL_VDOWN_HOLD) == 0)
136 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
137 if ((reg & IOC_PANEL_VUP_HOLD) == 0)
138 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
140 return 1;
143 static void
144 panel_powerbutton(void *cookie)
146 struct panel_softc *sc = cookie;
148 sysmon_pswitch_event(&sc->sc_pbutton, PSWITCH_EVENT_PRESSED);