summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/calypso/keypad.c
blob: f2dea9df1b85edfea6fa79e4c95041319fc0c3ec (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
/* Driver for the keypad attached to the TI Calypso */

/* (C) 2010 by roh <roh@hyte.de>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdint.h>
#include <stdio.h>

#include <defines.h>
#include <debug.h>
#include <delay.h>
#include <memory.h>
#include <keypad.h>

#include <calypso/irq.h>
#include <abb/twl3025.h>
#include <comm/timer.h>


#define KBR_LATCH_REG	0xfffe480a
#define KBC_REG		0xfffe480c
#define KBD_GPIO_INT	0xfffe4816
#define KBD_GPIO_MASKIT	0xfffe4818

static key_handler_t key_handler = NULL;

void emit_key(uint8_t key, uint8_t state)
{
	printf("key=%u %s\n", key, state == PRESSED ? "pressed" : "released");

	if(key_handler) {
		key_handler(key, state);
	}
}

volatile uint32_t lastbuttons = 0;
unsigned long power_hold = 0;

#define BTN_TO_KEY(name) \
	((diff & BTN_##name) == BTN_##name)	\
	{					\
		key = KEY_##name;		\
		diff = diff & ~BTN_##name;	\
		state = (buttons & BTN_##name) ? PRESSED : RELEASED;	\
	}

void dispatch_buttons(uint32_t buttons)
{
	uint8_t state;

	if ((buttons & BTN_POWER)) {
		/* hold button 500ms to shut down */
		if ((lastbuttons & BTN_POWER)) {
			unsigned long elapsed = jiffies - power_hold;
			if (elapsed > 50)
				twl3025_power_off();
			power_hold++;
		} else
		power_hold = jiffies;
	}

	if (buttons == lastbuttons)
		return;

	uint32_t diff = buttons ^ lastbuttons;
	uint8_t key=KEY_INV;

	while (diff != 0)
	{
		if BTN_TO_KEY(POWER)
		else if BTN_TO_KEY(0)
		else if BTN_TO_KEY(1)
		else if BTN_TO_KEY(2)
		else if BTN_TO_KEY(3)
		else if BTN_TO_KEY(4)
		else if BTN_TO_KEY(5)
		else if BTN_TO_KEY(6)
		else if BTN_TO_KEY(7)
		else if BTN_TO_KEY(8)
		else if BTN_TO_KEY(9)
		else if BTN_TO_KEY(STAR)
		else if BTN_TO_KEY(HASH)
		else if BTN_TO_KEY(MENU)
		else if BTN_TO_KEY(LEFT_SB)
		else if BTN_TO_KEY(RIGHT_SB)
		else if BTN_TO_KEY(UP)
		else if BTN_TO_KEY(DOWN)
		else if BTN_TO_KEY(LEFT)
		else if BTN_TO_KEY(RIGHT)
		else if BTN_TO_KEY(OK)
		else
		{
			printf("\nunknown keycode: 0x%08x\n", diff);
			break;
		}
		emit_key(key, state);
	}
	lastbuttons = buttons;
}

static uint8_t	polling = 0;
static uint8_t  with_interrupts = 0;

static void keypad_irq(__unused enum irq_nr nr)
{
	/* enable polling */
	polling = 1;
	irq_disable(IRQ_KEYPAD_GPIO);
}

void keypad_init(uint8_t interrupts)
{
	lastbuttons = 0;
	polling = 0;
	writew(0, KBD_GPIO_MASKIT);
	writew(0, KBC_REG);

	if(interrupts) {
		with_interrupts = 1;
		irq_register_handler(IRQ_KEYPAD_GPIO, &keypad_irq);
		irq_config(IRQ_KEYPAD_GPIO, 0, 0, 0);
		irq_enable(IRQ_KEYPAD_GPIO);
	}
}

void keypad_set_handler(key_handler_t handler)
{
	key_handler = handler;
}

void keypad_poll()
{
	static uint16_t reg;
	static uint16_t col;
	static uint32_t buttons = 0, debounce1 = 0, debounce2 = 0;

	if (with_interrupts && !polling)
		return;

	/* start polling */
	if (polling == 1) {
		writew(0x1f & ~0x1, KBC_REG); /* first col */
		col = 0;
		polling = 2;
		return;
	}

	/* enable keypad irq after the signal settles */
	if (polling == 3) {
		if(with_interrupts) {
			irq_enable(IRQ_KEYPAD_GPIO);
			polling = 0;
		} else {
			polling = 1;
		}
		return;
	}

	reg = readw(KBR_LATCH_REG);
	buttons = (buttons & ~(0x1f << (col * 5)))
		| ((~reg & 0x1f) << (col * 5 ));
	/* if key is released, stay in column for faster debounce */
	if ((debounce1 | debounce2) & ~buttons) {
		debounce2 = debounce1;
		debounce1 = buttons;
		return;
	}

	col++;
	if (col > 4) {
		col = 0;
		/* if power button, ignore other states */
		if (buttons & BTN_POWER)
			buttons = lastbuttons | BTN_POWER;
		else if (lastbuttons & BTN_POWER)
			buttons = lastbuttons & ~BTN_POWER;
		dispatch_buttons(buttons);
		if (buttons == 0) {
			writew(0x0, KBC_REG);
			polling = 3;
			return;
		}
	}
	if (col == 4)
		writew(0xff, KBC_REG);
	else
		writew(0x1f & ~(0x1 << col ), KBC_REG);

}