Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Chromium OS Matrix Keyboard |
| 3 | * |
| 4 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <cros_ec.h> |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 11 | #include <errno.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <input.h> |
| 14 | #include <key_matrix.h> |
| 15 | #include <stdio_dev.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | enum { |
| 20 | KBC_MAX_KEYS = 8, /* Maximum keys held down at once */ |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 21 | KBC_REPEAT_RATE_MS = 30, |
| 22 | KBC_REPEAT_DELAY_MS = 240, |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static struct keyb { |
| 26 | struct cros_ec_dev *dev; /* The CROS_EC device */ |
| 27 | struct input_config input; /* The input layer */ |
| 28 | struct key_matrix matrix; /* The key matrix layer */ |
| 29 | int key_rows; /* Number of keyboard rows */ |
| 30 | int key_cols; /* Number of keyboard columns */ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 43 | * @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 Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 45 | */ |
| 46 | static int check_for_keys(struct keyb *config, |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 47 | struct key_matrix_key *keys, int max_count, |
| 48 | bool *samep) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 49 | { |
| 50 | struct key_matrix_key *key; |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 51 | static struct mbkp_keyscan last_scan; |
| 52 | static bool last_scan_valid; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 53 | 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 Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 59 | return -EIO; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 60 | } |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 61 | *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 Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 71 | |
| 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 | */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 96 | static int kbd_tstc(struct stdio_dev *dev) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 107 | static int kbd_getc(struct stdio_dev *dev) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | */ |
| 122 | int 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 Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 130 | bool same = false; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 131 | |
| 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 Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 144 | num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS, |
| 145 | &same); |
| 146 | if (num_keys < 0) |
| 147 | return 0; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 148 | 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 Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 164 | |
| 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 Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 171 | } 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 | */ |
| 184 | static 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 | */ |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 191 | config->key_rows = fdtdec_get_int(blob, node, "keypad,num-rows", 0); |
| 192 | config->key_cols = fdtdec_get_int(blob, node, "keypad,num-columns", 0); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | } |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 200 | config->ghost_filter = fdtdec_get_bool(blob, node, |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 201 | "google,needs-ghost-filter"); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Set up the keyboard. This is called by the stdio device handler. |
| 207 | * |
| 208 | * We want to do this init when the keyboard is actually used rather than |
| 209 | * at start-up, since keyboard input may not currently be selected. |
| 210 | * |
| 211 | * @return 0 if ok, -1 on error |
| 212 | */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 213 | static int cros_ec_init_keyboard(struct stdio_dev *dev) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 214 | { |
| 215 | const void *blob = gd->fdt_blob; |
| 216 | int node; |
| 217 | |
| 218 | config.dev = board_get_cros_ec_dev(); |
| 219 | if (!config.dev) { |
| 220 | debug("%s: no cros_ec device: cannot init keyboard\n", |
| 221 | __func__); |
| 222 | return -1; |
| 223 | } |
| 224 | node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB); |
| 225 | if (node < 0) { |
| 226 | debug("%s: Node not found\n", __func__); |
| 227 | return -1; |
| 228 | } |
| 229 | if (cros_ec_keyb_decode_fdt(blob, node, &config)) |
| 230 | return -1; |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 231 | input_set_delays(&config.input, KBC_REPEAT_DELAY_MS, |
| 232 | KBC_REPEAT_RATE_MS); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 233 | if (key_matrix_init(&config.matrix, config.key_rows, |
| 234 | config.key_cols, config.ghost_filter)) { |
| 235 | debug("%s: cannot init key matrix\n", __func__); |
| 236 | return -1; |
| 237 | } |
| 238 | if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) { |
| 239 | debug("%s: Could not decode key matrix from fdt\n", __func__); |
| 240 | return -1; |
| 241 | } |
| 242 | config.inited = 1; |
| 243 | debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows, |
| 244 | config.key_cols); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int drv_keyboard_init(void) |
| 250 | { |
| 251 | struct stdio_dev dev; |
| 252 | |
| 253 | if (input_init(&config.input, 0)) { |
| 254 | debug("%s: Cannot set up input\n", __func__); |
| 255 | return -1; |
| 256 | } |
| 257 | config.input.read_keys = cros_ec_kbc_check; |
| 258 | |
| 259 | memset(&dev, '\0', sizeof(dev)); |
| 260 | strcpy(dev.name, "cros-ec-keyb"); |
| 261 | dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 262 | dev.getc = kbd_getc; |
| 263 | dev.tstc = kbd_tstc; |
| 264 | dev.start = cros_ec_init_keyboard; |
| 265 | |
| 266 | /* Register the device. cros_ec_init_keyboard() will be called soon */ |
| 267 | return input_stdio_register(&dev); |
| 268 | } |