1
2
3
4
5
6
7
8#include <common.h>
9#include <dm.h>
10#include <fdtdec.h>
11#include <input.h>
12#include <keyboard.h>
13#include <key_matrix.h>
14#include <stdio_dev.h>
15#include <tegra-kbc.h>
16#include <asm/io.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/funcmux.h>
19#include <asm/arch-tegra/timer.h>
20#include <linux/input.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24enum {
25 KBC_MAX_GPIO = 24,
26 KBC_MAX_KPENT = 8,
27};
28
29#define KBC_FIFO_TH_CNT_SHIFT 14
30#define KBC_DEBOUNCE_CNT_SHIFT 4
31#define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
32#define KBC_CONTROL_KBC_EN (1 << 0)
33#define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
34#define KBC_KPENT_VALID (1 << 7)
35#define KBC_ST_STATUS (1 << 3)
36
37enum {
38 KBC_DEBOUNCE_COUNT = 2,
39 KBC_REPEAT_RATE_MS = 30,
40 KBC_REPEAT_DELAY_MS = 240,
41 KBC_CLOCK_KHZ = 32,
42};
43
44
45struct tegra_kbd_priv {
46 struct input_config *input;
47 struct key_matrix matrix;
48
49 struct kbc_tegra *kbc;
50 unsigned char inited;
51 unsigned char first_scan;
52
53
54
55
56
57
58 unsigned int init_dly_ms;
59 unsigned int start_time_ms;
60 unsigned int last_poll_ms;
61 unsigned int next_repeat_ms;
62};
63
64
65
66
67
68
69
70
71
72static int tegra_kbc_find_keys(struct tegra_kbd_priv *priv, int *fifo,
73 int max_keycodes)
74{
75 struct key_matrix_key keys[KBC_MAX_KPENT], *key;
76 u32 kp_ent = 0;
77 int i;
78
79 for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) {
80
81 if (!(i & 3))
82 kp_ent = readl(&priv->kbc->kp_ent[i / 4]);
83
84 key->valid = (kp_ent & KBC_KPENT_VALID) != 0;
85 key->row = (kp_ent >> 3) & 0xf;
86 key->col = kp_ent & 0x7;
87
88
89 kp_ent >>= 8;
90 }
91 return key_matrix_decode(&priv->matrix, keys, KBC_MAX_KPENT, fifo,
92 max_keycodes);
93}
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113static void process_fifo(struct tegra_kbd_priv *priv, int fifo_cnt)
114{
115 int fifo[KBC_MAX_KPENT];
116 int cnt = 0;
117
118
119 do {
120 if (fifo_cnt)
121 cnt = tegra_kbc_find_keys(priv, fifo, KBC_MAX_KPENT);
122
123 input_send_keycodes(priv->input, fifo, cnt);
124 } while (--fifo_cnt > 0);
125}
126
127
128
129
130
131
132
133static void check_for_keys(struct tegra_kbd_priv *priv)
134{
135 int fifo_cnt;
136
137 if (!priv->first_scan &&
138 get_timer(priv->last_poll_ms) < KBC_REPEAT_RATE_MS)
139 return;
140 priv->last_poll_ms = get_timer(0);
141 priv->first_scan = 0;
142
143
144
145
146
147 fifo_cnt = (readl(&priv->kbc->interrupt) >> 4) & 0xf;
148 process_fifo(priv, fifo_cnt);
149}
150
151
152
153
154
155
156
157
158
159static void kbd_wait_for_fifo_init(struct tegra_kbd_priv *priv)
160{
161 if (!priv->inited) {
162 unsigned long elapsed_time;
163 long delay_ms;
164
165 elapsed_time = get_timer(priv->start_time_ms);
166 delay_ms = priv->init_dly_ms - elapsed_time;
167 if (delay_ms > 0) {
168 udelay(delay_ms * 1000);
169 debug("%s: delay %ldms\n", __func__, delay_ms);
170 }
171
172 priv->inited = 1;
173 }
174}
175
176
177
178
179
180
181
182
183
184
185static int tegra_kbc_check(struct input_config *input)
186{
187 struct tegra_kbd_priv *priv = dev_get_priv(input->dev);
188
189 kbd_wait_for_fifo_init(priv);
190 check_for_keys(priv);
191
192 return 1;
193}
194
195
196static void config_kbc_gpio(struct tegra_kbd_priv *priv, struct kbc_tegra *kbc)
197{
198 int i;
199
200 for (i = 0; i < KBC_MAX_GPIO; i++) {
201 u32 row_cfg, col_cfg;
202 u32 r_shift = 5 * (i % 6);
203 u32 c_shift = 4 * (i % 8);
204 u32 r_mask = 0x1f << r_shift;
205 u32 c_mask = 0xf << c_shift;
206 u32 r_offs = i / 6;
207 u32 c_offs = i / 8;
208
209 row_cfg = readl(&kbc->row_cfg[r_offs]);
210 col_cfg = readl(&kbc->col_cfg[c_offs]);
211
212 row_cfg &= ~r_mask;
213 col_cfg &= ~c_mask;
214
215 if (i < priv->matrix.num_rows) {
216 row_cfg |= ((i << 1) | 1) << r_shift;
217 } else {
218 col_cfg |= (((i - priv->matrix.num_rows) << 1) | 1)
219 << c_shift;
220 }
221
222 writel(row_cfg, &kbc->row_cfg[r_offs]);
223 writel(col_cfg, &kbc->col_cfg[c_offs]);
224 }
225}
226
227
228
229
230static void tegra_kbc_open(struct tegra_kbd_priv *priv)
231{
232 struct kbc_tegra *kbc = priv->kbc;
233 unsigned int scan_period;
234 u32 val;
235
236
237
238
239
240 scan_period = KBC_REPEAT_RATE_MS / 2;
241 writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly);
242 writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly);
243
244
245
246
247 priv->init_dly_ms = scan_period * 2 + 2;
248
249 val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT;
250 val |= 1 << KBC_FIFO_TH_CNT_SHIFT;
251 val |= KBC_CONTROL_KBC_EN;
252 writel(val, &kbc->control);
253
254 priv->start_time_ms = get_timer(0);
255 priv->last_poll_ms = get_timer(0);
256 priv->next_repeat_ms = priv->last_poll_ms;
257 priv->first_scan = 1;
258}
259
260static int tegra_kbd_start(struct udevice *dev)
261{
262 struct tegra_kbd_priv *priv = dev_get_priv(dev);
263
264
265 funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT);
266 clock_enable(PERIPH_ID_KBC);
267 config_kbc_gpio(priv, priv->kbc);
268
269 tegra_kbc_open(priv);
270 debug("%s: Tegra keyboard ready\n", __func__);
271
272 return 0;
273}
274
275
276
277
278
279
280
281
282
283
284
285
286
287static int tegra_kbd_probe(struct udevice *dev)
288{
289 struct tegra_kbd_priv *priv = dev_get_priv(dev);
290 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
291 struct stdio_dev *sdev = &uc_priv->sdev;
292 struct input_config *input = &uc_priv->input;
293 int node = dev->of_offset;
294 int ret;
295
296 priv->kbc = (struct kbc_tegra *)dev_get_addr(dev);
297 if ((fdt_addr_t)priv->kbc == FDT_ADDR_T_NONE) {
298 debug("%s: No keyboard register found\n", __func__);
299 return -EINVAL;
300 }
301 input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
302
303
304 ret = key_matrix_init(&priv->matrix, 16, 8, 1);
305 if (ret) {
306 debug("%s: Could not init key matrix: %d\n", __func__, ret);
307 return ret;
308 }
309 ret = key_matrix_decode_fdt(&priv->matrix, gd->fdt_blob, node);
310 if (ret) {
311 debug("%s: Could not decode key matrix from fdt: %d\n",
312 __func__, ret);
313 return ret;
314 }
315 input_add_tables(input, false);
316 if (priv->matrix.fn_keycode) {
317 ret = input_add_table(input, KEY_FN, -1,
318 priv->matrix.fn_keycode,
319 priv->matrix.key_count);
320 if (ret) {
321 debug("%s: input_add_table() failed\n", __func__);
322 return ret;
323 }
324 }
325
326
327 priv->input = input;
328 input->dev = dev;
329 input->read_keys = tegra_kbc_check;
330 strcpy(sdev->name, "tegra-kbc");
331 ret = input_stdio_register(sdev);
332 if (ret) {
333 debug("%s: input_stdio_register() failed\n", __func__);
334 return ret;
335 }
336
337 return 0;
338}
339
340static const struct keyboard_ops tegra_kbd_ops = {
341 .start = tegra_kbd_start,
342};
343
344static const struct udevice_id tegra_kbd_ids[] = {
345 { .compatible = "nvidia,tegra20-kbc" },
346 { }
347};
348
349U_BOOT_DRIVER(tegra_kbd) = {
350 .name = "tegra_kbd",
351 .id = UCLASS_KEYBOARD,
352 .of_match = tegra_kbd_ids,
353 .probe = tegra_kbd_probe,
354 .ops = &tegra_kbd_ops,
355 .priv_auto_alloc_size = sizeof(struct tegra_kbd_priv),
356};
357