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