11.8Sthorpej/*	$NetBSD: btn_obio.c,v 1.8 2023/12/20 15:00:08 thorpej Exp $	*/
21.1Suwe
31.1Suwe/*-
41.6Snonaka * 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.6Snonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.6Snonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.6Snonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.6Snonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.6Snonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.6Snonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.6Snonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.6Snonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.6Snonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.6Snonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Suwe */
271.1Suwe
281.1Suwe#include "pwrsw_obio.h"
291.1Suwe
301.1Suwe#include <sys/cdefs.h>
311.8Sthorpej__KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.8 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/kernel.h>
381.1Suwe#include <sys/conf.h>
391.1Suwe#include <sys/ioctl.h>
401.1Suwe#include <sys/callout.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 <machine/button.h>
481.1Suwe
491.1Suwe#include <landisk/landisk/landiskreg.h>
501.1Suwe#include <landisk/dev/obiovar.h>
511.1Suwe#include <landisk/dev/buttonvar.h>
521.1Suwe
531.1Suwe#define	BTN_SELECT	0
541.1Suwe#define	BTN_COPY	1
551.1Suwe#define	BTN_REMOVE	2
561.1Suwe#define	NBUTTON		3
571.1Suwe
581.1Suwestruct btn_obio_softc {
591.4Suwe	device_t		sc_dev;
601.1Suwe	void			*sc_ih;
611.1Suwe
621.1Suwe	struct callout		sc_guard_ch;
631.1Suwe	struct sysmon_pswitch	sc_smpsw;	/* reset */
641.1Suwe	struct btn_event	sc_bev[NBUTTON];
651.1Suwe
661.1Suwe	int			sc_mask;
671.1Suwe};
681.1Suwe
691.1Suwe#ifndef	BTN_TIMEOUT
701.1Suwe#define	BTN_TIMEOUT	(hz / 10)	/* 100ms */
711.1Suwe#endif
721.1Suwe
731.4Suwestatic int btn_obio_probe(device_t, cfdata_t, void *);
741.4Suwestatic void btn_obio_attach(device_t, device_t, void *);
751.1Suwe
761.1Suwestatic int btn_intr(void *aux);
771.1Suwestatic void btn_sysmon_pressed_event(void *arg);
781.1Suwestatic void btn_pressed_event(void *arg);
791.1Suwestatic void btn_guard_timeout(void *arg);
801.1Suwe
811.1Suwestatic struct btn_obio_softc *btn_softc;
821.1Suwe
831.1Suwestatic const struct {
841.1Suwe	int idx;
851.1Suwe	int mask;
861.1Suwe	const char *name;
871.1Suwe} btnlist[NBUTTON] = {
881.1Suwe	{ BTN_SELECT,	BTN_SELECT_BIT, "select" },
891.1Suwe	{ BTN_COPY,	BTN_COPY_BIT,	"copy"   },
901.1Suwe	{ BTN_REMOVE,	BTN_REMOVE_BIT,	"remove" },
911.1Suwe};
921.1Suwe
931.4SuweCFATTACH_DECL_NEW(btn_obio, sizeof(struct btn_obio_softc),
941.1Suwe    btn_obio_probe, btn_obio_attach, NULL, NULL);
951.1Suwe
961.1Suwestatic int
971.4Suwebtn_obio_probe(device_t parent, cfdata_t cfp, void *aux)
981.1Suwe{
991.1Suwe	struct obio_attach_args *oa = aux;
1001.1Suwe
1011.1Suwe	if (btn_softc)
1021.1Suwe		return (0);
1031.1Suwe
1041.1Suwe	oa->oa_nio = 0;
1051.1Suwe	oa->oa_niomem = 0;
1061.1Suwe	oa->oa_nirq = 1;
1071.1Suwe	oa->oa_irq[0].or_irq = LANDISK_INTR_BTN;
1081.1Suwe
1091.1Suwe	return (1);
1101.1Suwe}
1111.1Suwe
1121.1Suwestatic void
1131.4Suwebtn_obio_attach(device_t parent, device_t self, void *aux)
1141.1Suwe{
1151.4Suwe	struct btn_obio_softc *sc;
1161.1Suwe	int i;
1171.1Suwe
1181.4Suwe	aprint_naive("\n");
1191.5Suwe	aprint_normal(": USL-5P buttons\n");
1201.4Suwe
1211.4Suwe	sc = device_private(self);
1221.4Suwe	sc->sc_dev = self;
1231.1Suwe
1241.1Suwe	btn_softc = sc;
1251.1Suwe
1261.2Sad	callout_init(&sc->sc_guard_ch, 0);
1271.1Suwe	callout_setfunc(&sc->sc_guard_ch, btn_guard_timeout, sc);
1281.1Suwe
1291.1Suwe	sc->sc_ih = extintr_establish(LANDISK_INTR_BTN, IPL_TTY, btn_intr, sc);
1301.1Suwe	if (sc->sc_ih == NULL) {
1311.4Suwe		aprint_error_dev(self, "unable to establish interrupt");
1321.1Suwe		panic("extintr_establish");
1331.1Suwe	}
1341.1Suwe
1351.4Suwe	sc->sc_smpsw.smpsw_name = device_xname(self);
1361.1Suwe	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_RESET;
1371.1Suwe	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1381.4Suwe		aprint_error_dev(self, "unable to register with sysmon\n");
1391.1Suwe		return;
1401.1Suwe	}
1411.1Suwe	sc->sc_mask |= BTN_RESET_BIT;
1421.1Suwe
1431.7Srin	btn_init();
1441.1Suwe	for (i = 0; i < NBUTTON; i++) {
1451.1Suwe		int idx = btnlist[i].idx;
1461.1Suwe		sc->sc_bev[idx].bev_name = btnlist[i].name;
1471.1Suwe		if (btn_event_register(&sc->sc_bev[idx]) != 0) {
1481.4Suwe			aprint_error_dev(self,
1491.4Suwe					 "unable to register '%s' button\n",
1501.4Suwe					 btnlist[i].name);
1511.1Suwe		} else {
1521.1Suwe			sc->sc_mask |= btnlist[i].mask;
1531.1Suwe		}
1541.1Suwe	}
1551.1Suwe}
1561.1Suwe
1571.1Suwestatic int
1581.1Suwebtn_intr(void *arg)
1591.1Suwe{
1601.1Suwe	struct btn_obio_softc *sc = (void *)arg;
1611.4Suwe	device_t self = sc->sc_dev;
1621.1Suwe	int status;
1631.1Suwe	int i;
1641.1Suwe
1651.1Suwe	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1661.1Suwe	if (status == -1) {
1671.1Suwe		return (0);
1681.1Suwe	}
1691.1Suwe
1701.1Suwe	status = ~status;
1711.1Suwe	if (status & BTN_ALL_BIT) {
1721.1Suwe		if (status & BTN_RESET_BIT) {
1731.1Suwe			if (sc->sc_mask & BTN_RESET_BIT) {
1741.1Suwe				extintr_disable(sc->sc_ih);
1751.1Suwe#if NPWRSW_OBIO > 0
1761.1Suwe				extintr_disable_by_num(LANDISK_INTR_PWRSW);
1771.1Suwe#endif
1781.1Suwe				sysmon_task_queue_sched(0,
1791.1Suwe				    btn_sysmon_pressed_event, sc);
1801.1Suwe				return (1);
1811.1Suwe			} else {
1821.4Suwe				aprint_normal_dev(self,
1831.4Suwe					"reset button pressed\n");
1841.1Suwe			}
1851.1Suwe		}
1861.1Suwe
1871.1Suwe		for (i = 0; i < NBUTTON; i++) {
1881.1Suwe			uint8_t mask = btnlist[i].mask;
1891.1Suwe			int rv = 0;
1901.1Suwe			if (status & mask) {
1911.1Suwe				if (sc->sc_mask & mask) {
1921.1Suwe					sysmon_task_queue_sched(1,
1931.1Suwe					    btn_pressed_event,
1941.1Suwe					    &sc->sc_bev[btnlist[i].idx]);
1951.1Suwe				} else {
1961.4Suwe					aprint_normal_dev(self,
1971.4Suwe						"%s button pressed\n",
1981.4Suwe						btnlist[i].name);
1991.1Suwe				}
2001.1Suwe				rv = 1;
2011.1Suwe			}
2021.1Suwe			if (rv != 0) {
2031.1Suwe				extintr_disable(sc->sc_ih);
2041.1Suwe				callout_schedule(&sc->sc_guard_ch, BTN_TIMEOUT);
2051.1Suwe			}
2061.1Suwe		}
2071.1Suwe		return (1);
2081.1Suwe	}
2091.1Suwe	return (0);
2101.1Suwe}
2111.1Suwe
2121.1Suwestatic void
2131.1Suwebtn_sysmon_pressed_event(void *arg)
2141.1Suwe{
2151.1Suwe	struct btn_obio_softc *sc = (void *)arg;
2161.1Suwe
2171.1Suwe	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
2181.1Suwe}
2191.1Suwe
2201.1Suwestatic void
2211.1Suwebtn_pressed_event(void *arg)
2221.1Suwe{
2231.1Suwe	struct btn_event *bev = (void *)arg;
2241.1Suwe
2251.1Suwe	btn_event_send(bev, BUTTON_EVENT_PRESSED);
2261.1Suwe}
2271.1Suwe
2281.1Suwestatic void
2291.1Suwebtn_guard_timeout(void *arg)
2301.1Suwe{
2311.1Suwe	struct btn_obio_softc *sc = arg;
2321.1Suwe
2331.1Suwe	callout_stop(&sc->sc_guard_ch);
2341.1Suwe	extintr_enable(sc->sc_ih);
2351.1Suwe}
236