blob: a2501e02063e0abb4689c1d1994696751875c4d9 [file] [log] [blame]
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08001/*
2 * Chromium OS Matrix Keyboard
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08007 */
8
9#include <common.h>
10#include <cros_ec.h>
Simon Glass041699f2014-02-27 13:26:05 -070011#include <errno.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080012#include <fdtdec.h>
13#include <input.h>
14#include <key_matrix.h>
15#include <stdio_dev.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19enum {
20 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
21};
22
23static struct keyb {
24 struct cros_ec_dev *dev; /* The CROS_EC device */
25 struct input_config input; /* The input layer */
26 struct key_matrix matrix; /* The key matrix layer */
27 int key_rows; /* Number of keyboard rows */
28 int key_cols; /* Number of keyboard columns */
29 unsigned int repeat_delay_ms; /* Time before autorepeat starts */
30 unsigned int repeat_rate_ms; /* Autorepeat rate in ms */
31 int ghost_filter; /* 1 to enable ghost filter, else 0 */
32 int inited; /* 1 if keyboard is ready */
33} config;
34
35
36/**
37 * Check the keyboard controller and return a list of key matrix positions
38 * for which a key is pressed
39 *
40 * @param config Keyboard config
41 * @param keys List of keys that we have detected
42 * @param max_count Maximum number of keys to return
Simon Glass041699f2014-02-27 13:26:05 -070043 * @param samep Set to true if this scan repeats the last, else false
44 * @return number of pressed keys, 0 for none, -EIO on error
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080045 */
46static int check_for_keys(struct keyb *config,
Simon Glass041699f2014-02-27 13:26:05 -070047 struct key_matrix_key *keys, int max_count,
48 bool *samep)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080049{
50 struct key_matrix_key *key;
Simon Glass041699f2014-02-27 13:26:05 -070051 static struct mbkp_keyscan last_scan;
52 static bool last_scan_valid;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080053 struct mbkp_keyscan scan;
54 unsigned int row, col, bit, data;
55 int num_keys;
56
57 if (cros_ec_scan_keyboard(config->dev, &scan)) {
58 debug("%s: keyboard scan failed\n", __func__);
Simon Glass041699f2014-02-27 13:26:05 -070059 return -EIO;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080060 }
Simon Glass041699f2014-02-27 13:26:05 -070061 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
62
63 /*
64 * This is a bit odd. The EC has no way to tell us that it has run
65 * out of key scans. It just returns the same scan over and over
66 * again. So the only way to detect that we have run out is to detect
67 * that this scan is the same as the last.
68 */
69 last_scan_valid = true;
70 memcpy(&last_scan, &scan, sizeof(last_scan));
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080071
72 for (col = num_keys = bit = 0; col < config->matrix.num_cols;
73 col++) {
74 for (row = 0; row < config->matrix.num_rows; row++) {
75 unsigned int mask = 1 << (bit & 7);
76
77 data = scan.data[bit / 8];
78 if ((data & mask) && num_keys < max_count) {
79 key = keys + num_keys++;
80 key->row = row;
81 key->col = col;
82 key->valid = 1;
83 }
84 bit++;
85 }
86 }
87
88 return num_keys;
89}
90
91/**
92 * Test if keys are available to be read
93 *
94 * @return 0 if no keys available, 1 if keys are available
95 */
96static int kbd_tstc(void)
97{
98 /* Just get input to do this for us */
99 return config.inited ? input_tstc(&config.input) : 0;
100}
101
102/**
103 * Read a key
104 *
105 * @return ASCII key code, or 0 if no key, or -1 if error
106 */
107static int kbd_getc(void)
108{
109 /* Just get input to do this for us */
110 return config.inited ? input_getc(&config.input) : 0;
111}
112
113/**
114 * Check the keyboard, and send any keys that are pressed.
115 *
116 * This is called by input_tstc() and input_getc() when they need more
117 * characters
118 *
119 * @param input Input configuration
120 * @return 1, to indicate that we have something to look at
121 */
122int cros_ec_kbc_check(struct input_config *input)
123{
124 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
125 static int last_num_keys;
126 struct key_matrix_key keys[KBC_MAX_KEYS];
127 int keycodes[KBC_MAX_KEYS];
128 int num_keys, num_keycodes;
129 int irq_pending, sent;
Simon Glass041699f2014-02-27 13:26:05 -0700130 bool same = false;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800131
132 /*
133 * Loop until the EC has no more keyscan records, or we have
134 * received at least one character. This means we know that tstc()
135 * will always return non-zero if keys have been pressed.
136 *
137 * Without this loop, a key release (which generates no new ascii
138 * characters) will cause us to exit this function, and just tstc()
139 * may return 0 before all keys have been read from the EC.
140 */
141 do {
142 irq_pending = cros_ec_interrupt_pending(config.dev);
143 if (irq_pending) {
Simon Glass041699f2014-02-27 13:26:05 -0700144 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS,
145 &same);
146 if (num_keys < 0)
147 return 0;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800148 last_num_keys = num_keys;
149 memcpy(last_keys, keys, sizeof(keys));
150 } else {
151 /*
152 * EC doesn't want to be asked, so use keys from last
153 * time.
154 */
155 num_keys = last_num_keys;
156 memcpy(keys, last_keys, sizeof(keys));
157 }
158
159 if (num_keys < 0)
160 return -1;
161 num_keycodes = key_matrix_decode(&config.matrix, keys,
162 num_keys, keycodes, KBC_MAX_KEYS);
163 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glass041699f2014-02-27 13:26:05 -0700164
165 /*
166 * For those ECs without an interrupt, stop scanning when we
167 * see that the scan is the same as last time.
168 */
169 if ((irq_pending < 0) && same)
170 break;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800171 } while (irq_pending && !sent);
172
173 return 1;
174}
175
176/**
177 * Decode MBKP keyboard details from the device tree
178 *
179 * @param blob Device tree blob
180 * @param node Node to decode from
181 * @param config Configuration data read from fdt
182 * @return 0 if ok, -1 on error
183 */
184static int cros_ec_keyb_decode_fdt(const void *blob, int node,
185 struct keyb *config)
186{
187 /*
188 * Get keyboard rows and columns - at present we are limited to
189 * 8 columns by the protocol (one byte per row scan)
190 */
191 config->key_rows = fdtdec_get_int(blob, node, "google,key-rows", 0);
192 config->key_cols = fdtdec_get_int(blob, node, "google,key-columns", 0);
193 if (!config->key_rows || !config->key_cols ||
194 config->key_rows * config->key_cols / 8
195 > CROS_EC_KEYSCAN_COLS) {
196 debug("%s: Invalid key matrix size %d x %d\n", __func__,
197 config->key_rows, config->key_cols);
198 return -1;
199 }
200 config->repeat_delay_ms = fdtdec_get_int(blob, node,
201 "google,repeat-delay-ms", 0);
202 config->repeat_rate_ms = fdtdec_get_int(blob, node,
203 "google,repeat-rate-ms", 0);
204 config->ghost_filter = fdtdec_get_bool(blob, node,
205 "google,ghost-filter");
206 return 0;
207}
208
209/**
210 * Set up the keyboard. This is called by the stdio device handler.
211 *
212 * We want to do this init when the keyboard is actually used rather than
213 * at start-up, since keyboard input may not currently be selected.
214 *
215 * @return 0 if ok, -1 on error
216 */
217static int cros_ec_init_keyboard(void)
218{
219 const void *blob = gd->fdt_blob;
220 int node;
221
222 config.dev = board_get_cros_ec_dev();
223 if (!config.dev) {
224 debug("%s: no cros_ec device: cannot init keyboard\n",
225 __func__);
226 return -1;
227 }
228 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
229 if (node < 0) {
230 debug("%s: Node not found\n", __func__);
231 return -1;
232 }
233 if (cros_ec_keyb_decode_fdt(blob, node, &config))
234 return -1;
235 input_set_delays(&config.input, config.repeat_delay_ms,
236 config.repeat_rate_ms);
237 if (key_matrix_init(&config.matrix, config.key_rows,
238 config.key_cols, config.ghost_filter)) {
239 debug("%s: cannot init key matrix\n", __func__);
240 return -1;
241 }
242 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
243 debug("%s: Could not decode key matrix from fdt\n", __func__);
244 return -1;
245 }
246 config.inited = 1;
247 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
248 config.key_cols);
249
250 return 0;
251}
252
253int drv_keyboard_init(void)
254{
255 struct stdio_dev dev;
256
257 if (input_init(&config.input, 0)) {
258 debug("%s: Cannot set up input\n", __func__);
259 return -1;
260 }
261 config.input.read_keys = cros_ec_kbc_check;
262
263 memset(&dev, '\0', sizeof(dev));
264 strcpy(dev.name, "cros-ec-keyb");
265 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
266 dev.getc = kbd_getc;
267 dev.tstc = kbd_tstc;
268 dev.start = cros_ec_init_keyboard;
269
270 /* Register the device. cros_ec_init_keyboard() will be called soon */
271 return input_stdio_register(&dev);
272}