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