pwrsw_obio.c revision 1.4
11.4Sthorpej/*	$NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $	*/
21.1Suwe
31.1Suwe/*-
41.3Snonaka * Copyright (C) 2005 NONAKA Kimihiro <nonaka@netbsd.org>
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.3Snonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.3Snonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.3Snonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.3Snonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.3Snonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.3Snonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.3Snonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.3Snonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.3Snonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.3Snonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Suwe */
271.1Suwe
281.1Suwe#include "btn_obio.h"
291.1Suwe
301.1Suwe#include <sys/cdefs.h>
311.4Sthorpej__KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $");
321.1Suwe
331.1Suwe#include <sys/types.h>
341.1Suwe#include <sys/param.h>
351.1Suwe#include <sys/systm.h>
361.1Suwe#include <sys/device.h>
371.1Suwe#include <sys/conf.h>
381.1Suwe#include <sys/ioctl.h>
391.1Suwe
401.1Suwe#include <dev/sysmon/sysmonvar.h>
411.1Suwe#include <dev/sysmon/sysmon_taskq.h>
421.1Suwe
431.1Suwe#include <sh3/devreg.h>
441.1Suwe
451.1Suwe#include <landisk/landisk/landiskreg.h>
461.1Suwe#include <landisk/dev/obiovar.h>
471.1Suwe
481.1Suwestruct pwrsw_obio_softc {
491.2Suwe	device_t		sc_dev;
501.1Suwe	void			*sc_ih;
511.1Suwe
521.1Suwe	struct sysmon_pswitch	sc_smpsw; /* our sysmon glue */
531.1Suwe
541.1Suwe	int			sc_flags;
551.1Suwe#define	SYSMON_ATTACHED	1
561.1Suwe};
571.1Suwe
581.2Suwestatic int pwrsw_obio_probe(device_t, cfdata_t, void *);
591.2Suwestatic void pwrsw_obio_attach(device_t, device_t, void *);
601.1Suwe
611.1Suwestatic int pwrsw_intr(void *aux);
621.1Suwestatic void pwrsw_pressed_event(void *arg);
631.1Suwe
641.2SuweCFATTACH_DECL_NEW(pwrsw_obio, sizeof(struct pwrsw_obio_softc),
651.1Suwe    pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL);
661.1Suwe
671.1Suwestatic struct pwrsw_obio_softc *pwrsw_softc;
681.1Suwe
691.1Suwestatic int
701.2Suwepwrsw_obio_probe(device_t parent, cfdata_t cfp, void *aux)
711.1Suwe{
721.1Suwe	struct obio_attach_args *oa = aux;
731.1Suwe
741.1Suwe	if (pwrsw_softc)
751.1Suwe		return (0);
761.1Suwe
771.1Suwe	oa->oa_nio = 0;
781.1Suwe	oa->oa_niomem = 0;
791.1Suwe	oa->oa_nirq = 1;
801.1Suwe	oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW;
811.1Suwe
821.1Suwe	return (1);
831.1Suwe}
841.1Suwe
851.1Suwestatic void
861.2Suwepwrsw_obio_attach(device_t parent, device_t self, void *aux)
871.1Suwe{
881.2Suwe	struct pwrsw_obio_softc *sc;
891.1Suwe
901.2Suwe	aprint_naive("\n");
911.2Suwe	aprint_normal(": Power Switch\n");
921.2Suwe
931.2Suwe	sc = device_private(self);
941.2Suwe	sc->sc_dev = self;
951.1Suwe
961.1Suwe	pwrsw_softc = sc;
971.1Suwe
981.2Suwe	sc->sc_smpsw.smpsw_name = device_xname(self);
991.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1001.1Suwe
1011.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY,
1021.1Suwe	    pwrsw_intr, sc);
1031.1Suwe	if (sc->sc_ih == NULL) {
1041.2Suwe		aprint_error_dev(self, "unable to establish interrupt");
1051.1Suwe		panic("extintr_establish");
1061.1Suwe	}
1071.1Suwe
1081.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1091.2Suwe		aprint_error_dev(self, "unable to register with sysmon\n");
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.2Suwe	struct pwrsw_obio_softc *sc = 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.2Suwe			aprint_normal_dev(sc->sc_dev, "pressed\n");
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.2Suwe	struct pwrsw_obio_softc *sc = arg;
1471.1Suwe
1481.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1491.1Suwe}
150