xref: /netbsd-src/sys/arch/sgimips/hpc/button.c (revision 2a6ce19891fa11b4d1fc593f23519616c50e8982)
1*2a6ce198Schristos /*	$NetBSD: button.c,v 1.1 2018/04/09 20:07:22 christos Exp $ */
2*2a6ce198Schristos 
3*2a6ce198Schristos /*-
4*2a6ce198Schristos  * Copyright (c) 2009 Michael Lorenz
5*2a6ce198Schristos  * All rights reserved.
6*2a6ce198Schristos  *
7*2a6ce198Schristos  * Redistribution and use in source and binary forms, with or without
8*2a6ce198Schristos  * modification, are permitted provided that the following conditions
9*2a6ce198Schristos  * are met:
10*2a6ce198Schristos  * 1. Redistributions of source code must retain the above copyright
11*2a6ce198Schristos  *    notice, this list of conditions and the following disclaimer.
12*2a6ce198Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*2a6ce198Schristos  *    notice, this list of conditions and the following disclaimer in the
14*2a6ce198Schristos  *    documentation and/or other materials provided with the distribution.
15*2a6ce198Schristos  *
16*2a6ce198Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*2a6ce198Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*2a6ce198Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*2a6ce198Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*2a6ce198Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*2a6ce198Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*2a6ce198Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*2a6ce198Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*2a6ce198Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*2a6ce198Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*2a6ce198Schristos  * POSSIBILITY OF SUCH DAMAGE.
27*2a6ce198Schristos  */
28*2a6ce198Schristos 
29*2a6ce198Schristos #include <sys/cdefs.h>
30*2a6ce198Schristos __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.1 2018/04/09 20:07:22 christos Exp $");
31*2a6ce198Schristos 
32*2a6ce198Schristos #include <sys/param.h>
33*2a6ce198Schristos #include <sys/systm.h>
34*2a6ce198Schristos #include <sys/kernel.h>
35*2a6ce198Schristos #include <sys/device.h>
36*2a6ce198Schristos #include <sys/proc.h>
37*2a6ce198Schristos #include <sys/kthread.h>
38*2a6ce198Schristos 
39*2a6ce198Schristos #include <sys/bus.h>
40*2a6ce198Schristos 
41*2a6ce198Schristos #include <dev/sysmon/sysmonvar.h>
42*2a6ce198Schristos #include <dev/sysmon/sysmon_taskq.h>
43*2a6ce198Schristos 
44*2a6ce198Schristos #include <machine/autoconf.h>
45*2a6ce198Schristos #include <machine/machtype.h>
46*2a6ce198Schristos 
47*2a6ce198Schristos #include <sgimips/ioc/iocreg.h>
48*2a6ce198Schristos #include <sgimips/hpc/hpcvar.h>
49*2a6ce198Schristos #include <sgimips/hpc/hpcreg.h>
50*2a6ce198Schristos 
51*2a6ce198Schristos struct button_softc {
52*2a6ce198Schristos 	device_t sc_dev;
53*2a6ce198Schristos 	struct sysmon_pswitch sc_pbutton;
54*2a6ce198Schristos 	bus_space_tag_t sc_tag;
55*2a6ce198Schristos 	bus_space_handle_t sc_hreg;
56*2a6ce198Schristos 	int sc_last, sc_fired;
57*2a6ce198Schristos };
58*2a6ce198Schristos 
59*2a6ce198Schristos static int button_match(device_t, cfdata_t, void *);
60*2a6ce198Schristos static void button_attach(device_t, device_t, void *);
61*2a6ce198Schristos 
62*2a6ce198Schristos static int button_intr(void *);
63*2a6ce198Schristos static void button_powerbutton(void *);
64*2a6ce198Schristos 
65*2a6ce198Schristos CFATTACH_DECL_NEW(button, sizeof(struct button_softc),
66*2a6ce198Schristos 				button_match,
67*2a6ce198Schristos 				button_attach,
68*2a6ce198Schristos 				NULL,
69*2a6ce198Schristos 				NULL);
70*2a6ce198Schristos 
71*2a6ce198Schristos static int
button_match(device_t parent,cfdata_t match,void * aux)72*2a6ce198Schristos button_match(device_t parent, cfdata_t match, void *aux)
73*2a6ce198Schristos {
74*2a6ce198Schristos 	struct hpc_attach_args *ha = aux;
75*2a6ce198Schristos 
76*2a6ce198Schristos 	if (strcmp(ha->ha_name, match->cf_name) != 0)
77*2a6ce198Schristos 		return 0;
78*2a6ce198Schristos 
79*2a6ce198Schristos 	if (mach_type == MACH_SGI_IP22)
80*2a6ce198Schristos 		return 1;
81*2a6ce198Schristos 
82*2a6ce198Schristos 	return 0;
83*2a6ce198Schristos }
84*2a6ce198Schristos 
85*2a6ce198Schristos static void
button_attach(device_t parent,device_t self,void * aux)86*2a6ce198Schristos button_attach(device_t parent, device_t self, void *aux)
87*2a6ce198Schristos {
88*2a6ce198Schristos 	struct button_softc *sc;
89*2a6ce198Schristos 	struct hpc_attach_args *haa;
90*2a6ce198Schristos 
91*2a6ce198Schristos 	sc = device_private(self);
92*2a6ce198Schristos 	sc->sc_dev = self;
93*2a6ce198Schristos 	haa = aux;
94*2a6ce198Schristos 	sc->sc_tag = haa->ha_st;
95*2a6ce198Schristos 	sc->sc_fired = 0;
96*2a6ce198Schristos 
97*2a6ce198Schristos 	aprint_normal("\n");
98*2a6ce198Schristos 	if (bus_space_subregion(haa->ha_st, haa->ha_sh, haa->ha_devoff,
99*2a6ce198Schristos 			0x4, 		/* just a single register */
100*2a6ce198Schristos 			&sc->sc_hreg)) {
101*2a6ce198Schristos 		aprint_error(": unable to map button register\n");
102*2a6ce198Schristos 		return;
103*2a6ce198Schristos 	}
104*2a6ce198Schristos 
105*2a6ce198Schristos 	if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
106*2a6ce198Schristos 	     button_intr, sc)) == NULL) {
107*2a6ce198Schristos 		printf(": unable to establish interrupt!\n");
108*2a6ce198Schristos 		return;
109*2a6ce198Schristos 	}
110*2a6ce198Schristos 
111*2a6ce198Schristos 	sc->sc_last = 0;
112*2a6ce198Schristos 
113*2a6ce198Schristos 	sysmon_task_queue_init();
114*2a6ce198Schristos 	memset(&sc->sc_pbutton, 0, sizeof(struct sysmon_pswitch));
115*2a6ce198Schristos 	sc->sc_pbutton.smpsw_name = device_xname(sc->sc_dev);
116*2a6ce198Schristos 	sc->sc_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
117*2a6ce198Schristos 	if (sysmon_pswitch_register(&sc->sc_pbutton) != 0)
118*2a6ce198Schristos 		aprint_error_dev(sc->sc_dev,
119*2a6ce198Schristos 		    "unable to register power button with sysmon\n");
120*2a6ce198Schristos 	pmf_device_register(self, NULL, NULL);
121*2a6ce198Schristos }
122*2a6ce198Schristos 
123*2a6ce198Schristos static int
button_intr(void * cookie)124*2a6ce198Schristos button_intr(void *cookie)
125*2a6ce198Schristos {
126*2a6ce198Schristos 	struct button_softc *sc = cookie;
127*2a6ce198Schristos 	uint8_t reg;
128*2a6ce198Schristos 
129*2a6ce198Schristos 	reg = bus_space_read_4(sc->sc_tag, sc->sc_hreg, 0);
130*2a6ce198Schristos 	bus_space_write_4(sc->sc_tag, sc->sc_hreg, 0,
131*2a6ce198Schristos 	    IOC_PANEL_VDOWN_IRQ | IOC_PANEL_VUP_IRQ | IOC_PANEL_POWER_IRQ);
132*2a6ce198Schristos 	if ((reg & IOC_PANEL_POWER_IRQ) == 0) {
133*2a6ce198Schristos 		if (!sc->sc_fired)
134*2a6ce198Schristos 			sysmon_task_queue_sched(0, button_powerbutton, sc);
135*2a6ce198Schristos 		sc->sc_fired = 1;
136*2a6ce198Schristos 	}
137*2a6ce198Schristos 	if (time_second == sc->sc_last)
138*2a6ce198Schristos 		return 1;
139*2a6ce198Schristos 	sc->sc_last = time_second;
140*2a6ce198Schristos 	if ((reg & IOC_PANEL_VDOWN_HOLD) == 0)
141*2a6ce198Schristos 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
142*2a6ce198Schristos 	if ((reg & IOC_PANEL_VUP_HOLD) == 0)
143*2a6ce198Schristos 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
144*2a6ce198Schristos 
145*2a6ce198Schristos 	return 1;
146*2a6ce198Schristos }
147*2a6ce198Schristos 
148*2a6ce198Schristos static void
button_powerbutton(void * cookie)149*2a6ce198Schristos button_powerbutton(void *cookie)
150*2a6ce198Schristos {
151*2a6ce198Schristos 	struct button_softc *sc = cookie;
152*2a6ce198Schristos 
153*2a6ce198Schristos 	sysmon_pswitch_event(&sc->sc_pbutton, PSWITCH_EVENT_PRESSED);
154*2a6ce198Schristos }
155