summaryrefslogtreecommitdiff
path: root/sys/arch/x68k/dev/powsw.c
blob: 2887d8a3b6ff9b8c02f0f7e9fa6371f85d14b551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*	$NetBSD: powsw.c,v 1.4 2022/07/16 04:55:35 isaki Exp $	*/

/*
 * Copyright (c) 2011 Tetsuya Isaki. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Power switch monitor
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: powsw.c,v 1.4 2022/07/16 04:55:35 isaki Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/callout.h>

#include <machine/bus.h>
#include <machine/cpu.h>

#include <arch/x68k/dev/intiovar.h>
#include <arch/x68k/dev/mfp.h>

#include <dev/sysmon/sysmonvar.h>
#include <dev/sysmon/sysmon_taskq.h>

#include "ioconf.h"

//#define POWSW_DEBUG

#if defined(POWSW_DEBUG)
#define DPRINTF(fmt...)		printf(fmt)
#define DEBUG_LOG_ADD(c)	sc->sc_log[sc->sc_loglen++] = (c)
#define DEBUG_LOG_PRINT()	do {	\
	sc->sc_log[sc->sc_loglen] = '\0';	\
	printf("%s", sc->sc_log);	\
} while (0)
#else
#define DPRINTF(fmt...)
#define DEBUG_LOG_ADD(c)
#define DEBUG_LOG_PRINT()
#endif

/* mask */
#define POWSW_ALARM		(0x01)
#define POWSW_EXTERNAL		(0x02)
#define POWSW_FRONT		(0x04)

/* parameter */
#define POWSW_MAX_TICK		(30)
#define POWSW_THRESHOLD		(10)

struct powsw_softc {
	device_t sc_dev;
	struct sysmon_pswitch sc_smpsw;
	callout_t sc_callout;
	int sc_mask;
	int sc_prev;
	int sc_last_sw;
	int sc_tick;
	int sc_count;
#if defined(POWSW_DEBUG)
	char sc_log[100];
	int sc_loglen;
#endif
};

static int  powsw_match(device_t, cfdata_t, void *);
static void powsw_attach(device_t, device_t, void *);
static int  powsw_intr(void *);
static void powsw_softintr(void *);
static void powsw_pswitch_event(void *);
static void powsw_reset_counter(struct powsw_softc *);
static void powsw_set_aer(struct powsw_softc *, int);

CFATTACH_DECL_NEW(powsw, sizeof(struct powsw_softc),
    powsw_match, powsw_attach, NULL, NULL);


typedef const struct {
	int vector;			/* interrupt vector */
	int mask;			/* mask bit for MFP GPIP */
	const char *name;
} powsw_desc_t;

static powsw_desc_t powsw_desc[2] = {
	{ 66, POWSW_FRONT,	"Front Switch", },
	{ 65, POWSW_EXTERNAL,	"External Power Switch", },
	/* XXX I'm not sure about alarm bit */
};


static int
powsw_match(device_t parent, cfdata_t cf, void *aux)
{

	return 1;
}

static void
powsw_attach(device_t parent, device_t self, void *aux)
{
	struct powsw_softc *sc = device_private(self);
	powsw_desc_t *desc;
	const char *xname;
	int unit;
	int sw;

	unit = device_unit(self);
	xname = device_xname(self);
	desc = &powsw_desc[unit];

	memset(sc, 0, sizeof(*sc));
	sc->sc_dev = self;
	sc->sc_mask = desc->mask;
	sc->sc_prev = -1;
	powsw_reset_counter(sc);

	sysmon_task_queue_init();
	sc->sc_smpsw.smpsw_name = xname;
	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0)
		panic("can't register with sysmon");

	callout_init(&sc->sc_callout, 0);
	callout_setfunc(&sc->sc_callout, powsw_softintr, sc);

	if (intio_intr_establish(desc->vector, xname, powsw_intr, sc) < 0)
		panic("%s: can't establish interrupt", xname);

	/* Set AER and enable interrupt */
	sw = (mfp_get_gpip() & sc->sc_mask);
	powsw_set_aer(sc, sw ? 0 : 1);
	mfp_bit_set_ierb(sc->sc_mask);

	aprint_normal(": %s\n", desc->name);
}

static int
powsw_intr(void *arg)
{
	struct powsw_softc *sc = arg;

	if (sc->sc_tick == 0) {
		mfp_bit_clear_ierb(sc->sc_mask);
		sc->sc_tick++;
		DEBUG_LOG_ADD('i');
		/*
		 * The button state seems unstable for few ticks,
		 * so wait a bit to settle.
		 */
		callout_schedule(&sc->sc_callout, 1);
	} else {
		DEBUG_LOG_ADD('x');
	}
	return 0;
}

void
powsw_softintr(void *arg)
{
	struct powsw_softc *sc = arg;
	int sw;
	int s;

	s = spl6();

	if (sc->sc_tick++ >= POWSW_MAX_TICK) {
		/* tick is over, broken switch? */
		printf("%s: unstable power switch?, ignored\n",
		    device_xname(sc->sc_dev));
		powsw_reset_counter(sc);

		mfp_bit_set_ierb(sc->sc_mask);
		splx(s);
		return;
	}

	sw = (mfp_get_gpip() & sc->sc_mask) ? 1 : 0;
	DEBUG_LOG_ADD('0' + sw);

	if (sw == sc->sc_last_sw) {
		sc->sc_count++;
	} else {
		sc->sc_last_sw = sw;
		sc->sc_count = 1;
	}

	if (sc->sc_count < POWSW_THRESHOLD) {
		callout_schedule(&sc->sc_callout, 1);
	} else {
		/* switch seems stable */
		DEBUG_LOG_PRINT();

		if (sc->sc_last_sw == sc->sc_prev) {
			/* switch state is not changed, it was a noise */
			DPRINTF(" ignore(sw=%d,prev=%d)\n",
			    sc->sc_last_sw, sc->sc_prev);
		} else {
			/* switch state has been changed */
			sc->sc_prev = sc->sc_last_sw;
			powsw_set_aer(sc, 1 - sc->sc_prev);
			sysmon_task_queue_sched(0, powsw_pswitch_event, sc);
		}
		powsw_reset_counter(sc);
		/* enable interrupt */
		mfp_bit_set_ierb(sc->sc_mask);
	}

	splx(s);
}

static void
powsw_pswitch_event(void *arg)
{
	struct powsw_softc *sc = arg;
	int poweroff;

	poweroff = sc->sc_prev;

	DPRINTF(" %s is %s\n", device_xname(sc->sc_dev),
	    poweroff ? "off(PRESS)" : "on(RELEASE)");

	sysmon_pswitch_event(&sc->sc_smpsw,
	    poweroff ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
}

static void
powsw_reset_counter(struct powsw_softc *sc)
{

	sc->sc_last_sw = -1;
	sc->sc_tick = 0;
	sc->sc_count = 0;
#if defined(POWSW_DEBUG)
	sc->sc_loglen = 0;
#endif
}

static void
powsw_set_aer(struct powsw_softc *sc, int aer)
{

	KASSERT(aer == 0 || aer == 1);

	if (aer == 0) {
		mfp_bit_clear_aer(sc->sc_mask);
	} else {
		mfp_bit_set_aer(sc->sc_mask);
	}
	DPRINTF(" SetAER=%d", aer);
}