Home | History | Annotate | Line # | Download | only in hpc
      1 /*	$NetBSD: button.c,v 1.1 2018/04/09 20:07:22 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2009 Michael Lorenz
      5  * All rights reserved.
      6  *
      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.
     15  *
     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.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: button.c,v 1.1 2018/04/09 20:07:22 christos Exp $");
     31 
     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>
     38 
     39 #include <sys/bus.h>
     40 
     41 #include <dev/sysmon/sysmonvar.h>
     42 #include <dev/sysmon/sysmon_taskq.h>
     43 
     44 #include <machine/autoconf.h>
     45 #include <machine/machtype.h>
     46 
     47 #include <sgimips/ioc/iocreg.h>
     48 #include <sgimips/hpc/hpcvar.h>
     49 #include <sgimips/hpc/hpcreg.h>
     50 
     51 struct button_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;
     57 };
     58 
     59 static int button_match(device_t, cfdata_t, void *);
     60 static void button_attach(device_t, device_t, void *);
     61 
     62 static int button_intr(void *);
     63 static void button_powerbutton(void *);
     64 
     65 CFATTACH_DECL_NEW(button, sizeof(struct button_softc),
     66 				button_match,
     67 				button_attach,
     68 				NULL,
     69 				NULL);
     70 
     71 static int
     72 button_match(device_t parent, cfdata_t match, void *aux)
     73 {
     74 	struct hpc_attach_args *ha = aux;
     75 
     76 	if (strcmp(ha->ha_name, match->cf_name) != 0)
     77 		return 0;
     78 
     79 	if (mach_type == MACH_SGI_IP22)
     80 		return 1;
     81 
     82 	return 0;
     83 }
     84 
     85 static void
     86 button_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	struct button_softc *sc;
     89 	struct hpc_attach_args *haa;
     90 
     91 	sc = device_private(self);
     92 	sc->sc_dev = self;
     93 	haa = aux;
     94 	sc->sc_tag = haa->ha_st;
     95 	sc->sc_fired = 0;
     96 
     97 	aprint_normal("\n");
     98 	if (bus_space_subregion(haa->ha_st, haa->ha_sh, haa->ha_devoff,
     99 			0x4, 		/* just a single register */
    100 			&sc->sc_hreg)) {
    101 		aprint_error(": unable to map button register\n");
    102 		return;
    103 	}
    104 
    105 	if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
    106 	     button_intr, sc)) == NULL) {
    107 		printf(": unable to establish interrupt!\n");
    108 		return;
    109 	}
    110 
    111 	sc->sc_last = 0;
    112 
    113 	sysmon_task_queue_init();
    114 	memset(&sc->sc_pbutton, 0, sizeof(struct sysmon_pswitch));
    115 	sc->sc_pbutton.smpsw_name = device_xname(sc->sc_dev);
    116 	sc->sc_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
    117 	if (sysmon_pswitch_register(&sc->sc_pbutton) != 0)
    118 		aprint_error_dev(sc->sc_dev,
    119 		    "unable to register power button with sysmon\n");
    120 	pmf_device_register(self, NULL, NULL);
    121 }
    122 
    123 static int
    124 button_intr(void *cookie)
    125 {
    126 	struct button_softc *sc = cookie;
    127 	uint8_t reg;
    128 
    129 	reg = bus_space_read_4(sc->sc_tag, sc->sc_hreg, 0);
    130 	bus_space_write_4(sc->sc_tag, sc->sc_hreg, 0,
    131 	    IOC_PANEL_VDOWN_IRQ | IOC_PANEL_VUP_IRQ | IOC_PANEL_POWER_IRQ);
    132 	if ((reg & IOC_PANEL_POWER_IRQ) == 0) {
    133 		if (!sc->sc_fired)
    134 			sysmon_task_queue_sched(0, button_powerbutton, sc);
    135 		sc->sc_fired = 1;
    136 	}
    137 	if (time_second == sc->sc_last)
    138 		return 1;
    139 	sc->sc_last = time_second;
    140 	if ((reg & IOC_PANEL_VDOWN_HOLD) == 0)
    141 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    142 	if ((reg & IOC_PANEL_VUP_HOLD) == 0)
    143 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    144 
    145 	return 1;
    146 }
    147 
    148 static void
    149 button_powerbutton(void *cookie)
    150 {
    151 	struct button_softc *sc = cookie;
    152 
    153 	sysmon_pswitch_event(&sc->sc_pbutton, PSWITCH_EVENT_PRESSED);
    154 }
    155