summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/calypso/keypad.c
blob: 27f48c0bc0a366f13db475f1a644caee76e3d8e7 (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
/* 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 <debug.h>
#include <delay.h>
#include <memory.h>
#include <keypad.h>

#include <calypso/irq.h>
#include <abb/twl3025.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 (state == RELEASED)
		if (key == KEY_POWER)
			twl3025_power_off();

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

volatile uint32_t lastbuttons;

#define BTN_TO_KEY(name) \
	((diff & BTN_##name) == BTN_##name)	\
	{					\
		key = KEY_##name;		\
		diff = diff & ~BTN_##name;	\
	}

void dispatch_buttons(uint32_t buttons)
{
	uint8_t state;

	if (buttons == lastbuttons)
		return;

	if (buttons > lastbuttons)
		state = PRESSED;
	else
		state = RELEASED;

	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;
		}
		if ( key == KEY_POWER )
			diff = 0;
		emit_key(key, state);
	}
	lastbuttons = buttons;
}

static void keypad_irq(enum irq_nr nr)
{
	keypad_scan();
}

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

	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_scan()
{
	uint16_t reg;
	uint16_t col;
	uint32_t buttons;

//	putchar('\n');
	buttons = 0x0;
	//scan for BTN_POWER
	writew(0xff, KBC_REG);
	delay_ms(1);
	reg = readw(KBR_LATCH_REG);
//	printd("%02x ", (~reg & 0x1f));
	buttons = buttons | ((~reg & 0x1f) << 20 );

	//scan for muxed keys if not powerbtn
	if ((~reg & 0x1f) != 0x10)
	  for (col=0;col<4;col++)
	  {
		writew(0x1f & ~(0x1 << col ), KBC_REG);
		delay_ms(1);
		reg = readw(KBR_LATCH_REG);
		buttons = buttons | ((~reg & 0x1f) << (col * 5 ));
//		printd("%02x ", (~reg & 0x1f));
	}
	//enable keypad irq via master 'or' gate (needs col lines low in idle to work)
	writew(0, KBC_REG);
	dispatch_buttons(buttons);

}