Home | History | Annotate | Line # | Download | only in dev
powsw.c revision 1.1
      1  1.1  isaki /*	$NetBSD: powsw.c,v 1.1 2011/11/27 09:00:32 isaki Exp $	*/
      2  1.1  isaki 
      3  1.1  isaki /*
      4  1.1  isaki  * Copyright (c) 2011 Tetsuya Isaki. All rights reserved.
      5  1.1  isaki  *
      6  1.1  isaki  * Redistribution and use in source and binary forms, with or without
      7  1.1  isaki  * modification, are permitted provided that the following conditions
      8  1.1  isaki  * are met:
      9  1.1  isaki  * 1. Redistributions of source code must retain the above copyright
     10  1.1  isaki  *    notice, this list of conditions and the following disclaimer.
     11  1.1  isaki  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  isaki  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  isaki  *    documentation and/or other materials provided with the distribution.
     14  1.1  isaki  *
     15  1.1  isaki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  isaki  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  isaki  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  isaki  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  isaki  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  isaki  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  isaki  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  isaki  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  isaki  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  isaki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  isaki  * SUCH DAMAGE.
     26  1.1  isaki  */
     27  1.1  isaki 
     28  1.1  isaki /*
     29  1.1  isaki  * Power switch monitor
     30  1.1  isaki  */
     31  1.1  isaki 
     32  1.1  isaki #include <sys/cdefs.h>
     33  1.1  isaki __KERNEL_RCSID(0, "$NetBSD: powsw.c,v 1.1 2011/11/27 09:00:32 isaki Exp $");
     34  1.1  isaki 
     35  1.1  isaki #include <sys/param.h>
     36  1.1  isaki #include <sys/systm.h>
     37  1.1  isaki #include <sys/conf.h>
     38  1.1  isaki #include <sys/device.h>
     39  1.1  isaki #include <sys/intr.h>
     40  1.1  isaki #include <sys/callout.h>
     41  1.1  isaki 
     42  1.1  isaki #include <machine/bus.h>
     43  1.1  isaki #include <machine/cpu.h>
     44  1.1  isaki 
     45  1.1  isaki #include <arch/x68k/dev/intiovar.h>
     46  1.1  isaki #include <arch/x68k/dev/mfp.h>
     47  1.1  isaki 
     48  1.1  isaki #include <dev/sysmon/sysmonvar.h>
     49  1.1  isaki #include <dev/sysmon/sysmon_taskq.h>
     50  1.1  isaki 
     51  1.1  isaki extern int power_switch_is_off;		/* XXX should be in .h */
     52  1.1  isaki 
     53  1.1  isaki //#define POWSW_DEBUG
     54  1.1  isaki 
     55  1.1  isaki #if defined(POWSW_DEBUG)
     56  1.1  isaki #define DPRINTF(fmt...)	printf(fmt)
     57  1.1  isaki #define DEBUG_LOG_ADD(c)	sc->sc_log[sc->sc_loglen++] = (c)
     58  1.1  isaki #define DEBUG_LOG_PRINT()	do {	\
     59  1.1  isaki 	sc->sc_log[sc->sc_loglen] = '\0';	\
     60  1.1  isaki 	printf("%s", sc->sc_log);	\
     61  1.1  isaki } while (0)
     62  1.1  isaki #else
     63  1.1  isaki #define DPRINTF(fmt...)
     64  1.1  isaki #define DEBUG_LOG_ADD(c)
     65  1.1  isaki #define DEBUG_LOG_PRINT()
     66  1.1  isaki #endif
     67  1.1  isaki 
     68  1.1  isaki /* mask */
     69  1.1  isaki #define POWSW_ALARM		(0x01)
     70  1.1  isaki #define POWSW_EXTERNAL	(0x02)
     71  1.1  isaki #define POWSW_FRONT		(0x04)
     72  1.1  isaki 
     73  1.1  isaki /* parameter */
     74  1.1  isaki #define POWSW_MAX_TICK	(30)
     75  1.1  isaki #define POWSW_THRESHOLD	(10)
     76  1.1  isaki 
     77  1.1  isaki struct powsw_softc {
     78  1.1  isaki 	device_t sc_dev;
     79  1.1  isaki 	struct sysmon_pswitch sc_smpsw;
     80  1.1  isaki 	callout_t sc_callout;
     81  1.1  isaki 	int sc_mask;
     82  1.1  isaki 	int sc_prev;
     83  1.1  isaki 	int sc_last_sw;
     84  1.1  isaki 	int sc_tick;
     85  1.1  isaki 	int sc_count;
     86  1.1  isaki #if defined(POWSW_DEBUG)
     87  1.1  isaki 	char sc_log[100];
     88  1.1  isaki 	int sc_loglen;
     89  1.1  isaki #endif
     90  1.1  isaki };
     91  1.1  isaki 
     92  1.1  isaki extern struct cfdriver powsw_cd;
     93  1.1  isaki 
     94  1.1  isaki static int  powsw_match(device_t, cfdata_t, void *);
     95  1.1  isaki static void powsw_attach(device_t, device_t, void *);
     96  1.1  isaki static int  powsw_intr(void *);
     97  1.1  isaki static void powsw_softintr(void *);
     98  1.1  isaki static void powsw_pswitch_event(void *);
     99  1.1  isaki static void powsw_shutdown_check(void *);
    100  1.1  isaki static void powsw_reset_counter(struct powsw_softc *);
    101  1.1  isaki static void powsw_set_aer(struct powsw_softc *, int);
    102  1.1  isaki 
    103  1.1  isaki CFATTACH_DECL_NEW(powsw, sizeof(struct powsw_softc),
    104  1.1  isaki     powsw_match, powsw_attach, NULL, NULL);
    105  1.1  isaki 
    106  1.1  isaki 
    107  1.1  isaki typedef const struct {
    108  1.1  isaki 	int vector;			/* interrupt vector */
    109  1.1  isaki 	int mask;			/* mask bit for MFP GPIP */
    110  1.1  isaki 	const char *name;
    111  1.1  isaki } powsw_desc_t;
    112  1.1  isaki 
    113  1.1  isaki static powsw_desc_t powsw_desc[2] = {
    114  1.1  isaki 	{ 66, POWSW_FRONT,		"Front Switch", },
    115  1.1  isaki 	{ 65, POWSW_EXTERNAL,	"External Power Switch", },
    116  1.1  isaki 	/* XXX I'm not sure about alarm bit */
    117  1.1  isaki };
    118  1.1  isaki 
    119  1.1  isaki 
    120  1.1  isaki static int
    121  1.1  isaki powsw_match(device_t parent, cfdata_t cf, void *aux)
    122  1.1  isaki {
    123  1.1  isaki 	return 1;
    124  1.1  isaki }
    125  1.1  isaki 
    126  1.1  isaki static void
    127  1.1  isaki powsw_attach(device_t parent, device_t self, void *aux)
    128  1.1  isaki {
    129  1.1  isaki 	struct powsw_softc *sc = device_private(self);
    130  1.1  isaki 	powsw_desc_t *desc;
    131  1.1  isaki 	const char *xname;
    132  1.1  isaki 	int unit;
    133  1.1  isaki 	int sw;
    134  1.1  isaki 
    135  1.1  isaki 	unit = device_unit(self);
    136  1.1  isaki 	xname = device_xname(self);
    137  1.1  isaki 	desc = &powsw_desc[unit];
    138  1.1  isaki 
    139  1.1  isaki 	memset(sc, 0, sizeof(*sc));
    140  1.1  isaki 	sc->sc_dev = self;
    141  1.1  isaki 	sc->sc_mask = desc->mask;
    142  1.1  isaki 	sc->sc_prev = -1;
    143  1.1  isaki 	powsw_reset_counter(sc);
    144  1.1  isaki 
    145  1.1  isaki 	sysmon_task_queue_init();
    146  1.1  isaki 	sc->sc_smpsw.smpsw_name = xname;
    147  1.1  isaki 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
    148  1.1  isaki 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0)
    149  1.1  isaki 		panic("can't register with sysmon");
    150  1.1  isaki 
    151  1.1  isaki 	callout_init(&sc->sc_callout, 0);
    152  1.1  isaki 	callout_setfunc(&sc->sc_callout, powsw_softintr, sc);
    153  1.1  isaki 
    154  1.1  isaki 	if (shutdownhook_establish(powsw_shutdown_check, sc) == NULL)
    155  1.1  isaki 		panic("%s: can't establish shutdown hook", xname);
    156  1.1  isaki 
    157  1.1  isaki 	if (intio_intr_establish(desc->vector, xname, powsw_intr, sc) < 0)
    158  1.1  isaki 		panic("%s: can't establish interrupt", xname);
    159  1.1  isaki 
    160  1.1  isaki 	/* Set AER and enable interrupt */
    161  1.1  isaki 	sw = (mfp_get_gpip() & sc->sc_mask);
    162  1.1  isaki 	powsw_set_aer(sc, sw ? 0 : 1);
    163  1.1  isaki 	mfp_bit_set_ierb(sc->sc_mask);
    164  1.1  isaki 
    165  1.1  isaki 	aprint_normal(": %s\n", desc->name);
    166  1.1  isaki }
    167  1.1  isaki 
    168  1.1  isaki static int
    169  1.1  isaki powsw_intr(void *arg)
    170  1.1  isaki {
    171  1.1  isaki 	struct powsw_softc *sc = arg;
    172  1.1  isaki 
    173  1.1  isaki 	if (sc->sc_tick == 0) {
    174  1.1  isaki 		mfp_bit_clear_ierb(sc->sc_mask);
    175  1.1  isaki 		sc->sc_tick++;
    176  1.1  isaki 		DEBUG_LOG_ADD('i');
    177  1.1  isaki 		/*
    178  1.1  isaki 		 * The button state seems unstable for few ticks,
    179  1.1  isaki 		 * so wait a bit to settle.
    180  1.1  isaki 		 */
    181  1.1  isaki 		callout_schedule(&sc->sc_callout, 1);
    182  1.1  isaki 	} else {
    183  1.1  isaki 		DEBUG_LOG_ADD('x');
    184  1.1  isaki 	}
    185  1.1  isaki 	return 0;
    186  1.1  isaki }
    187  1.1  isaki 
    188  1.1  isaki void
    189  1.1  isaki powsw_softintr(void *arg)
    190  1.1  isaki {
    191  1.1  isaki 	struct powsw_softc *sc = arg;
    192  1.1  isaki 	int sw;
    193  1.1  isaki 	int s;
    194  1.1  isaki 
    195  1.1  isaki 	s = spl6();
    196  1.1  isaki 
    197  1.1  isaki 	if (sc->sc_tick++ >= POWSW_MAX_TICK) {
    198  1.1  isaki 		/* tick is over, broken switch? */
    199  1.1  isaki 		printf("%s: unstable power switch?, ignored\n",
    200  1.1  isaki 		    device_xname(sc->sc_dev));
    201  1.1  isaki 		powsw_reset_counter(sc);
    202  1.1  isaki 
    203  1.1  isaki 		mfp_bit_set_ierb(sc->sc_mask);
    204  1.1  isaki 		splx(s);
    205  1.1  isaki 		return;
    206  1.1  isaki 	}
    207  1.1  isaki 
    208  1.1  isaki 	sw = (mfp_get_gpip() & sc->sc_mask) ? 1 : 0;
    209  1.1  isaki 	DEBUG_LOG_ADD('0' + sw);
    210  1.1  isaki 
    211  1.1  isaki 	if (sw == sc->sc_last_sw) {
    212  1.1  isaki 		sc->sc_count++;
    213  1.1  isaki 	} else {
    214  1.1  isaki 		sc->sc_last_sw = sw;
    215  1.1  isaki 		sc->sc_count = 1;
    216  1.1  isaki 	}
    217  1.1  isaki 
    218  1.1  isaki 	if (sc->sc_count < POWSW_THRESHOLD) {
    219  1.1  isaki 		callout_schedule(&sc->sc_callout, 1);
    220  1.1  isaki 	} else {
    221  1.1  isaki 		/* switch seems stable */
    222  1.1  isaki 		DEBUG_LOG_PRINT();
    223  1.1  isaki 
    224  1.1  isaki 		if (sc->sc_last_sw == sc->sc_prev) {
    225  1.1  isaki 			/* switch state is not changed, it was a noise */
    226  1.1  isaki 			DPRINTF(" ignore(sw=%d,prev=%d)\n", sc->sc_last_sw, sc->sc_prev);
    227  1.1  isaki 		} else {
    228  1.1  isaki 			/* switch state has been changed */
    229  1.1  isaki 			sc->sc_prev = sc->sc_last_sw;
    230  1.1  isaki 			powsw_set_aer(sc, 1 - sc->sc_prev);
    231  1.1  isaki 			sysmon_task_queue_sched(0, powsw_pswitch_event, sc);
    232  1.1  isaki 		}
    233  1.1  isaki 		powsw_reset_counter(sc);
    234  1.1  isaki 		mfp_bit_set_ierb(sc->sc_mask);	// enable interrupt
    235  1.1  isaki 	}
    236  1.1  isaki 
    237  1.1  isaki 	splx(s);
    238  1.1  isaki }
    239  1.1  isaki 
    240  1.1  isaki static void
    241  1.1  isaki powsw_pswitch_event(void *arg)
    242  1.1  isaki {
    243  1.1  isaki 	struct powsw_softc *sc = arg;
    244  1.1  isaki 	int poweroff;
    245  1.1  isaki 
    246  1.1  isaki 	poweroff = sc->sc_prev;
    247  1.1  isaki 
    248  1.1  isaki 	DPRINTF(" %s is %s\n", device_xname(sc->sc_dev),
    249  1.1  isaki 	    poweroff ? "off(PRESS)" : "on(RELEASE)");
    250  1.1  isaki 
    251  1.1  isaki 	sysmon_pswitch_event(&sc->sc_smpsw,
    252  1.1  isaki 		poweroff ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    253  1.1  isaki }
    254  1.1  isaki 
    255  1.1  isaki static void
    256  1.1  isaki powsw_shutdown_check(void *arg)
    257  1.1  isaki {
    258  1.1  isaki 	struct powsw_softc *sc = arg;
    259  1.1  isaki 	int poweroff;
    260  1.1  isaki 
    261  1.1  isaki 	poweroff = sc->sc_prev;
    262  1.1  isaki 	if (poweroff)
    263  1.1  isaki 		power_switch_is_off = 1;
    264  1.1  isaki 	DPRINTF("powsw_shutdown_check %s = %d\n",
    265  1.1  isaki 		device_xname(sc->sc_dev), power_switch_is_off);
    266  1.1  isaki }
    267  1.1  isaki 
    268  1.1  isaki static void
    269  1.1  isaki powsw_reset_counter(struct powsw_softc *sc)
    270  1.1  isaki {
    271  1.1  isaki 	sc->sc_last_sw = -1;
    272  1.1  isaki 	sc->sc_tick = 0;
    273  1.1  isaki 	sc->sc_count = 0;
    274  1.1  isaki #if defined(POWSW_DEBUG)
    275  1.1  isaki 	sc->sc_loglen = 0;
    276  1.1  isaki #endif
    277  1.1  isaki }
    278  1.1  isaki 
    279  1.1  isaki static void
    280  1.1  isaki powsw_set_aer(struct powsw_softc *sc, int aer)
    281  1.1  isaki {
    282  1.1  isaki 	KASSERT(aer == 0 || aer == 1);
    283  1.1  isaki 
    284  1.1  isaki 	if (aer == 0) {
    285  1.1  isaki 		mfp_bit_clear_aer(sc->sc_mask);
    286  1.1  isaki 	} else {
    287  1.1  isaki 		mfp_bit_set_aer(sc->sc_mask);
    288  1.1  isaki 	}
    289  1.1  isaki 	DPRINTF(" SetAER=%d", aer);
    290  1.1  isaki }
    291