pwrsw_obio.c revision 1.2
11.2Suwe/*	$NetBSD: pwrsw_obio.c,v 1.2 2008/03/27 02:52:04 uwe Exp $	*/
21.1Suwe
31.1Suwe/*-
41.1Suwe * Copyright (c) 2005 NONAKA Kimihiro
51.1Suwe * All rights reserved.
61.1Suwe *
71.1Suwe * Redistribution and use in source and binary forms, with or without
81.1Suwe * modification, are permitted provided that the following conditions
91.1Suwe * are met:
101.1Suwe * 1. Redistributions of source code must retain the above copyright
111.1Suwe *    notice, this list of conditions and the following disclaimer.
121.1Suwe * 2. Redistributions in binary form must reproduce the above copyright
131.1Suwe *    notice, this list of conditions and the following disclaimer in the
141.1Suwe *    documentation and/or other materials provided with the distribution.
151.1Suwe *
161.1Suwe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
171.1Suwe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Suwe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Suwe * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
201.1Suwe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Suwe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Suwe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Suwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Suwe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Suwe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Suwe * SUCH DAMAGE.
271.1Suwe */
281.1Suwe
291.1Suwe#include "btn_obio.h"
301.1Suwe
311.1Suwe#include <sys/cdefs.h>
321.2Suwe__KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.2 2008/03/27 02:52:04 uwe Exp $");
331.1Suwe
341.1Suwe#include <sys/types.h>
351.1Suwe#include <sys/param.h>
361.1Suwe#include <sys/systm.h>
371.1Suwe#include <sys/device.h>
381.1Suwe#include <sys/malloc.h>
391.1Suwe#include <sys/conf.h>
401.1Suwe#include <sys/ioctl.h>
411.1Suwe
421.1Suwe#include <dev/sysmon/sysmonvar.h>
431.1Suwe#include <dev/sysmon/sysmon_taskq.h>
441.1Suwe
451.1Suwe#include <sh3/devreg.h>
461.1Suwe
471.1Suwe#include <landisk/landisk/landiskreg.h>
481.1Suwe#include <landisk/dev/obiovar.h>
491.1Suwe
501.1Suwestruct pwrsw_obio_softc {
511.2Suwe	device_t		sc_dev;
521.1Suwe	void			*sc_ih;
531.1Suwe
541.1Suwe	struct sysmon_pswitch	sc_smpsw; /* our sysmon glue */
551.1Suwe
561.1Suwe	int			sc_flags;
571.1Suwe#define	SYSMON_ATTACHED	1
581.1Suwe};
591.1Suwe
601.2Suwestatic int pwrsw_obio_probe(device_t, cfdata_t, void *);
611.2Suwestatic void pwrsw_obio_attach(device_t, device_t, void *);
621.1Suwe
631.1Suwestatic int pwrsw_intr(void *aux);
641.1Suwestatic void pwrsw_pressed_event(void *arg);
651.1Suwe
661.2SuweCFATTACH_DECL_NEW(pwrsw_obio, sizeof(struct pwrsw_obio_softc),
671.1Suwe    pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL);
681.1Suwe
691.1Suwestatic struct pwrsw_obio_softc *pwrsw_softc;
701.1Suwe
711.1Suwestatic int
721.2Suwepwrsw_obio_probe(device_t parent, cfdata_t cfp, void *aux)
731.1Suwe{
741.1Suwe	struct obio_attach_args *oa = aux;
751.1Suwe
761.1Suwe	if (pwrsw_softc)
771.1Suwe		return (0);
781.1Suwe
791.1Suwe	oa->oa_nio = 0;
801.1Suwe	oa->oa_niomem = 0;
811.1Suwe	oa->oa_nirq = 1;
821.1Suwe	oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW;
831.1Suwe
841.1Suwe	return (1);
851.1Suwe}
861.1Suwe
871.1Suwestatic void
881.2Suwepwrsw_obio_attach(device_t parent, device_t self, void *aux)
891.1Suwe{
901.2Suwe	struct pwrsw_obio_softc *sc;
911.1Suwe
921.2Suwe	aprint_naive("\n");
931.2Suwe	aprint_normal(": Power Switch\n");
941.2Suwe
951.2Suwe	sc = device_private(self);
961.2Suwe	sc->sc_dev = self;
971.1Suwe
981.1Suwe	pwrsw_softc = sc;
991.1Suwe
1001.2Suwe	sc->sc_smpsw.smpsw_name = device_xname(self);
1011.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1021.1Suwe
1031.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY,
1041.1Suwe	    pwrsw_intr, sc);
1051.1Suwe	if (sc->sc_ih == NULL) {
1061.2Suwe		aprint_error_dev(self, "unable to establish interrupt");
1071.1Suwe		panic("extintr_establish");
1081.1Suwe	}
1091.1Suwe
1101.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1111.2Suwe		aprint_error_dev(self, "unable to register with sysmon\n");
1121.1Suwe		return;
1131.1Suwe	}
1141.1Suwe	sc->sc_flags |= SYSMON_ATTACHED;
1151.1Suwe}
1161.1Suwe
1171.1Suwestatic int
1181.1Suwepwrsw_intr(void *arg)
1191.1Suwe{
1201.2Suwe	struct pwrsw_obio_softc *sc = arg;
1211.1Suwe	int status;
1221.1Suwe
1231.1Suwe	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1241.1Suwe	if (status == -1) {
1251.1Suwe		return (0);
1261.1Suwe	}
1271.1Suwe
1281.1Suwe	status = ~status;
1291.1Suwe	if (status & BTN_POWER_BIT) {
1301.1Suwe		if (sc->sc_flags & SYSMON_ATTACHED) {
1311.1Suwe			sysmon_task_queue_sched(0, pwrsw_pressed_event, sc);
1321.1Suwe			extintr_disable(sc->sc_ih);
1331.1Suwe#if NBTN_OBIO > 0
1341.1Suwe			extintr_disable_by_num(LANDISK_INTR_BTN);
1351.1Suwe#endif
1361.1Suwe		} else {
1371.2Suwe			aprint_normal_dev(sc->sc_dev, "pressed\n");
1381.1Suwe		}
1391.1Suwe		_reg_write_1(LANDISK_PWRSW_INTCLR, 1);
1401.1Suwe		return (1);
1411.1Suwe	}
1421.1Suwe	return (0);
1431.1Suwe}
1441.1Suwe
1451.1Suwestatic void
1461.1Suwepwrsw_pressed_event(void *arg)
1471.1Suwe{
1481.2Suwe	struct pwrsw_obio_softc *sc = arg;
1491.1Suwe
1501.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1511.1Suwe}
152