1 /* $NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $ */ 2 3 /*- 4 * Copyright (C) 2005 NONAKA Kimihiro <nonaka (at) netbsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "btn_obio.h" 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $"); 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/conf.h> 38 #include <sys/ioctl.h> 39 40 #include <dev/sysmon/sysmonvar.h> 41 #include <dev/sysmon/sysmon_taskq.h> 42 43 #include <sh3/devreg.h> 44 45 #include <landisk/landisk/landiskreg.h> 46 #include <landisk/dev/obiovar.h> 47 48 struct pwrsw_obio_softc { 49 device_t sc_dev; 50 void *sc_ih; 51 52 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */ 53 54 int sc_flags; 55 #define SYSMON_ATTACHED 1 56 }; 57 58 static int pwrsw_obio_probe(device_t, cfdata_t, void *); 59 static void pwrsw_obio_attach(device_t, device_t, void *); 60 61 static int pwrsw_intr(void *aux); 62 static void pwrsw_pressed_event(void *arg); 63 64 CFATTACH_DECL_NEW(pwrsw_obio, sizeof(struct pwrsw_obio_softc), 65 pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL); 66 67 static struct pwrsw_obio_softc *pwrsw_softc; 68 69 static int 70 pwrsw_obio_probe(device_t parent, cfdata_t cfp, void *aux) 71 { 72 struct obio_attach_args *oa = aux; 73 74 if (pwrsw_softc) 75 return (0); 76 77 oa->oa_nio = 0; 78 oa->oa_niomem = 0; 79 oa->oa_nirq = 1; 80 oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW; 81 82 return (1); 83 } 84 85 static void 86 pwrsw_obio_attach(device_t parent, device_t self, void *aux) 87 { 88 struct pwrsw_obio_softc *sc; 89 90 aprint_naive("\n"); 91 aprint_normal(": Power Switch\n"); 92 93 sc = device_private(self); 94 sc->sc_dev = self; 95 96 pwrsw_softc = sc; 97 98 sc->sc_smpsw.smpsw_name = device_xname(self); 99 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 100 101 sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY, 102 pwrsw_intr, sc); 103 if (sc->sc_ih == NULL) { 104 aprint_error_dev(self, "unable to establish interrupt"); 105 panic("extintr_establish"); 106 } 107 108 if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) { 109 aprint_error_dev(self, "unable to register with sysmon\n"); 110 return; 111 } 112 sc->sc_flags |= SYSMON_ATTACHED; 113 } 114 115 static int 116 pwrsw_intr(void *arg) 117 { 118 struct pwrsw_obio_softc *sc = arg; 119 int status; 120 121 status = (int8_t)_reg_read_1(LANDISK_BTNSTAT); 122 if (status == -1) { 123 return (0); 124 } 125 126 status = ~status; 127 if (status & BTN_POWER_BIT) { 128 if (sc->sc_flags & SYSMON_ATTACHED) { 129 sysmon_task_queue_sched(0, pwrsw_pressed_event, sc); 130 extintr_disable(sc->sc_ih); 131 #if NBTN_OBIO > 0 132 extintr_disable_by_num(LANDISK_INTR_BTN); 133 #endif 134 } else { 135 aprint_normal_dev(sc->sc_dev, "pressed\n"); 136 } 137 _reg_write_1(LANDISK_PWRSW_INTCLR, 1); 138 return (1); 139 } 140 return (0); 141 } 142 143 static void 144 pwrsw_pressed_event(void *arg) 145 { 146 struct pwrsw_obio_softc *sc = arg; 147 148 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 149 } 150