xref: /netbsd-src/sys/arch/landisk/dev/pwrsw_obio.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: pwrsw_obio.c,v 1.3 2012/01/21 19:44:29 nonaka Exp $	*/
2 
3 /*-
4  * Copyright (C) 2005 NONAKA Kimihiro <nonaka@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.3 2012/01/21 19:44:29 nonaka 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/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/ioctl.h>
40 
41 #include <dev/sysmon/sysmonvar.h>
42 #include <dev/sysmon/sysmon_taskq.h>
43 
44 #include <sh3/devreg.h>
45 
46 #include <landisk/landisk/landiskreg.h>
47 #include <landisk/dev/obiovar.h>
48 
49 struct pwrsw_obio_softc {
50 	device_t		sc_dev;
51 	void			*sc_ih;
52 
53 	struct sysmon_pswitch	sc_smpsw; /* our sysmon glue */
54 
55 	int			sc_flags;
56 #define	SYSMON_ATTACHED	1
57 };
58 
59 static int pwrsw_obio_probe(device_t, cfdata_t, void *);
60 static void pwrsw_obio_attach(device_t, device_t, void *);
61 
62 static int pwrsw_intr(void *aux);
63 static void pwrsw_pressed_event(void *arg);
64 
65 CFATTACH_DECL_NEW(pwrsw_obio, sizeof(struct pwrsw_obio_softc),
66     pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL);
67 
68 static struct pwrsw_obio_softc *pwrsw_softc;
69 
70 static int
71 pwrsw_obio_probe(device_t parent, cfdata_t cfp, void *aux)
72 {
73 	struct obio_attach_args *oa = aux;
74 
75 	if (pwrsw_softc)
76 		return (0);
77 
78 	oa->oa_nio = 0;
79 	oa->oa_niomem = 0;
80 	oa->oa_nirq = 1;
81 	oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW;
82 
83 	return (1);
84 }
85 
86 static void
87 pwrsw_obio_attach(device_t parent, device_t self, void *aux)
88 {
89 	struct pwrsw_obio_softc *sc;
90 
91 	aprint_naive("\n");
92 	aprint_normal(": Power Switch\n");
93 
94 	sc = device_private(self);
95 	sc->sc_dev = self;
96 
97 	pwrsw_softc = sc;
98 
99 	sc->sc_smpsw.smpsw_name = device_xname(self);
100 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
101 
102 	sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY,
103 	    pwrsw_intr, sc);
104 	if (sc->sc_ih == NULL) {
105 		aprint_error_dev(self, "unable to establish interrupt");
106 		panic("extintr_establish");
107 	}
108 
109 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
110 		aprint_error_dev(self, "unable to register with sysmon\n");
111 		return;
112 	}
113 	sc->sc_flags |= SYSMON_ATTACHED;
114 }
115 
116 static int
117 pwrsw_intr(void *arg)
118 {
119 	struct pwrsw_obio_softc *sc = arg;
120 	int status;
121 
122 	status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
123 	if (status == -1) {
124 		return (0);
125 	}
126 
127 	status = ~status;
128 	if (status & BTN_POWER_BIT) {
129 		if (sc->sc_flags & SYSMON_ATTACHED) {
130 			sysmon_task_queue_sched(0, pwrsw_pressed_event, sc);
131 			extintr_disable(sc->sc_ih);
132 #if NBTN_OBIO > 0
133 			extintr_disable_by_num(LANDISK_INTR_BTN);
134 #endif
135 		} else {
136 			aprint_normal_dev(sc->sc_dev, "pressed\n");
137 		}
138 		_reg_write_1(LANDISK_PWRSW_INTCLR, 1);
139 		return (1);
140 	}
141 	return (0);
142 }
143 
144 static void
145 pwrsw_pressed_event(void *arg)
146 {
147 	struct pwrsw_obio_softc *sc = arg;
148 
149 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
150 }
151