pwrsw_obio.c revision 1.1
11.1Suwe/*	$NetBSD: pwrsw_obio.c,v 1.1 2006/09/01 21:26:18 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.1Suwe__KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.1 2006/09/01 21:26:18 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.1Suwe	struct device		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.1Suwestatic int pwrsw_obio_probe(struct device *, struct cfdata *, void *);
611.1Suwestatic void pwrsw_obio_attach(struct device *, struct device *, void *);
621.1Suwe
631.1Suwestatic int pwrsw_intr(void *aux);
641.1Suwestatic void pwrsw_pressed_event(void *arg);
651.1Suwe
661.1SuweCFATTACH_DECL(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.1Suwepwrsw_obio_probe(struct device *parent, struct cfdata *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.1Suwepwrsw_obio_attach(struct device *parent, struct device *self, void *aux)
891.1Suwe{
901.1Suwe	struct pwrsw_obio_softc *sc = (void *)self;
911.1Suwe
921.1Suwe	printf(": Power Switch\n");
931.1Suwe
941.1Suwe	pwrsw_softc = sc;
951.1Suwe
961.1Suwe	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
971.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
981.1Suwe
991.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY,
1001.1Suwe	    pwrsw_intr, sc);
1011.1Suwe	if (sc->sc_ih == NULL) {
1021.1Suwe		printf("%s: couldn't establish intr handler",
1031.1Suwe		    sc->sc_dev.dv_xname);
1041.1Suwe		panic("extintr_establish");
1051.1Suwe	}
1061.1Suwe
1071.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1081.1Suwe		printf("%s: unable to register with sysmon\n",
1091.1Suwe		    sc->sc_dev.dv_xname);
1101.1Suwe		return;
1111.1Suwe	}
1121.1Suwe	sc->sc_flags |= SYSMON_ATTACHED;
1131.1Suwe}
1141.1Suwe
1151.1Suwestatic int
1161.1Suwepwrsw_intr(void *arg)
1171.1Suwe{
1181.1Suwe	struct pwrsw_obio_softc *sc = (void *)arg;
1191.1Suwe	int status;
1201.1Suwe
1211.1Suwe	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1221.1Suwe	if (status == -1) {
1231.1Suwe		return (0);
1241.1Suwe	}
1251.1Suwe
1261.1Suwe	status = ~status;
1271.1Suwe	if (status & BTN_POWER_BIT) {
1281.1Suwe		if (sc->sc_flags & SYSMON_ATTACHED) {
1291.1Suwe			sysmon_task_queue_sched(0, pwrsw_pressed_event, sc);
1301.1Suwe			extintr_disable(sc->sc_ih);
1311.1Suwe#if NBTN_OBIO > 0
1321.1Suwe			extintr_disable_by_num(LANDISK_INTR_BTN);
1331.1Suwe#endif
1341.1Suwe		} else {
1351.1Suwe			printf("%s: pressed\n", sc->sc_dev.dv_xname);
1361.1Suwe		}
1371.1Suwe		_reg_write_1(LANDISK_PWRSW_INTCLR, 1);
1381.1Suwe		return (1);
1391.1Suwe	}
1401.1Suwe	return (0);
1411.1Suwe}
1421.1Suwe
1431.1Suwestatic void
1441.1Suwepwrsw_pressed_event(void *arg)
1451.1Suwe{
1461.1Suwe	struct pwrsw_obio_softc *sc = (void *)arg;
1471.1Suwe
1481.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1491.1Suwe}
150