Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Chromium OS Matrix Keyboard |
| 4 | * |
| 5 | * Copyright (c) 2012 The Chromium OS Authors. |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 8 | #include <cros_ec.h> |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 11 | #include <input.h> |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 12 | #include <keyboard.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 13 | #include <key_matrix.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 15 | #include <stdio_dev.h> |
| 16 | |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 17 | enum { |
| 18 | KBC_MAX_KEYS = 8, /* Maximum keys held down at once */ |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 19 | KBC_REPEAT_RATE_MS = 30, |
| 20 | KBC_REPEAT_DELAY_MS = 240, |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 21 | }; |
| 22 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 23 | struct cros_ec_keyb_priv { |
| 24 | struct input_config *input; /* The input layer */ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 25 | struct key_matrix matrix; /* The key matrix layer */ |
| 26 | int key_rows; /* Number of keyboard rows */ |
| 27 | int key_cols; /* Number of keyboard columns */ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 28 | int ghost_filter; /* 1 to enable ghost filter, else 0 */ |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 29 | }; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 30 | |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 31 | /** |
| 32 | * Check the keyboard controller and return a list of key matrix positions |
| 33 | * for which a key is pressed |
| 34 | * |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 35 | * @param dev Keyboard device |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 36 | * @param keys List of keys that we have detected |
| 37 | * @param max_count Maximum number of keys to return |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 38 | * @param samep Set to true if this scan repeats the last, else false |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 39 | * Return: number of pressed keys, 0 for none, -EIO on error |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 40 | */ |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 41 | static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys, |
| 42 | int max_count, bool *samep) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 43 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 44 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 45 | struct key_matrix_key *key; |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 46 | static struct mbkp_keyscan last_scan; |
| 47 | static bool last_scan_valid; |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 48 | struct ec_response_get_next_event event; |
| 49 | struct mbkp_keyscan *scan = (struct mbkp_keyscan *) |
| 50 | &event.data.key_matrix; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 51 | unsigned int row, col, bit, data; |
| 52 | int num_keys; |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 53 | int ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 54 | |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 55 | /* Get pending MKBP event. It may not be a key matrix event. */ |
| 56 | do { |
| 57 | ret = cros_ec_get_next_event(dev->parent, &event); |
| 58 | /* The EC has no events for us at this time. */ |
| 59 | if (ret == -EC_RES_UNAVAILABLE) |
| 60 | return -EIO; |
| 61 | else if (ret) |
| 62 | break; |
| 63 | } while (event.event_type != EC_MKBP_EVENT_KEY_MATRIX); |
| 64 | |
| 65 | /* Try the old command if the EC doesn't support the above. */ |
| 66 | if (ret == -EC_RES_INVALID_COMMAND) { |
| 67 | if (cros_ec_scan_keyboard(dev->parent, scan)) { |
| 68 | debug("%s: keyboard scan failed\n", __func__); |
| 69 | return -EIO; |
| 70 | } |
| 71 | } else if (ret) { |
| 72 | debug("%s: Error getting next MKBP event. (%d)\n", |
| 73 | __func__, ret); |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 74 | return -EIO; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 75 | } |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 76 | *samep = last_scan_valid && !memcmp(&last_scan, scan, sizeof(*scan)); |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * This is a bit odd. The EC has no way to tell us that it has run |
| 80 | * out of key scans. It just returns the same scan over and over |
| 81 | * again. So the only way to detect that we have run out is to detect |
| 82 | * that this scan is the same as the last. |
| 83 | */ |
| 84 | last_scan_valid = true; |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 85 | memcpy(&last_scan, scan, sizeof(last_scan)); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 86 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 87 | for (col = num_keys = bit = 0; col < priv->matrix.num_cols; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 88 | col++) { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 89 | for (row = 0; row < priv->matrix.num_rows; row++) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 90 | unsigned int mask = 1 << (bit & 7); |
| 91 | |
Alper Nebi Yasak | 1ce0729 | 2020-10-30 20:25:20 +0300 | [diff] [blame] | 92 | data = scan->data[bit / 8]; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 93 | if ((data & mask) && num_keys < max_count) { |
| 94 | key = keys + num_keys++; |
| 95 | key->row = row; |
| 96 | key->col = col; |
| 97 | key->valid = 1; |
| 98 | } |
| 99 | bit++; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return num_keys; |
| 104 | } |
| 105 | |
| 106 | /** |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 107 | * Check the keyboard, and send any keys that are pressed. |
| 108 | * |
| 109 | * This is called by input_tstc() and input_getc() when they need more |
| 110 | * characters |
| 111 | * |
| 112 | * @param input Input configuration |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 113 | * Return: 1, to indicate that we have something to look at |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 114 | */ |
| 115 | int cros_ec_kbc_check(struct input_config *input) |
| 116 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 117 | struct udevice *dev = input->dev; |
| 118 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 119 | static struct key_matrix_key last_keys[KBC_MAX_KEYS]; |
| 120 | static int last_num_keys; |
| 121 | struct key_matrix_key keys[KBC_MAX_KEYS]; |
| 122 | int keycodes[KBC_MAX_KEYS]; |
| 123 | int num_keys, num_keycodes; |
| 124 | int irq_pending, sent; |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 125 | bool same = false; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * Loop until the EC has no more keyscan records, or we have |
| 129 | * received at least one character. This means we know that tstc() |
| 130 | * will always return non-zero if keys have been pressed. |
| 131 | * |
| 132 | * Without this loop, a key release (which generates no new ascii |
| 133 | * characters) will cause us to exit this function, and just tstc() |
| 134 | * may return 0 before all keys have been read from the EC. |
| 135 | */ |
| 136 | do { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 137 | irq_pending = cros_ec_interrupt_pending(dev->parent); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 138 | if (irq_pending) { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 139 | num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS, |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 140 | &same); |
| 141 | if (num_keys < 0) |
| 142 | return 0; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 143 | last_num_keys = num_keys; |
| 144 | memcpy(last_keys, keys, sizeof(keys)); |
| 145 | } else { |
| 146 | /* |
| 147 | * EC doesn't want to be asked, so use keys from last |
| 148 | * time. |
| 149 | */ |
| 150 | num_keys = last_num_keys; |
| 151 | memcpy(keys, last_keys, sizeof(keys)); |
| 152 | } |
| 153 | |
| 154 | if (num_keys < 0) |
| 155 | return -1; |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 156 | num_keycodes = key_matrix_decode(&priv->matrix, keys, |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 157 | num_keys, keycodes, KBC_MAX_KEYS); |
| 158 | sent = input_send_keycodes(input, keycodes, num_keycodes); |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * For those ECs without an interrupt, stop scanning when we |
| 162 | * see that the scan is the same as last time. |
| 163 | */ |
| 164 | if ((irq_pending < 0) && same) |
| 165 | break; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 166 | } while (irq_pending && !sent); |
| 167 | |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Decode MBKP keyboard details from the device tree |
| 173 | * |
| 174 | * @param blob Device tree blob |
| 175 | * @param node Node to decode from |
| 176 | * @param config Configuration data read from fdt |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 177 | * Return: 0 if ok, -1 on error |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 178 | */ |
Simon Glass | bf45b2e | 2017-05-18 20:09:53 -0600 | [diff] [blame] | 179 | static int cros_ec_keyb_decode_fdt(struct udevice *dev, |
| 180 | struct cros_ec_keyb_priv *config) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 181 | { |
| 182 | /* |
| 183 | * Get keyboard rows and columns - at present we are limited to |
| 184 | * 8 columns by the protocol (one byte per row scan) |
| 185 | */ |
Simon Glass | bf45b2e | 2017-05-18 20:09:53 -0600 | [diff] [blame] | 186 | config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0); |
| 187 | config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 188 | if (!config->key_rows || !config->key_cols || |
| 189 | config->key_rows * config->key_cols / 8 |
| 190 | > CROS_EC_KEYSCAN_COLS) { |
| 191 | debug("%s: Invalid key matrix size %d x %d\n", __func__, |
| 192 | config->key_rows, config->key_cols); |
| 193 | return -1; |
| 194 | } |
Simon Glass | bf45b2e | 2017-05-18 20:09:53 -0600 | [diff] [blame] | 195 | config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter"); |
| 196 | |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 200 | static int cros_ec_kbd_probe(struct udevice *dev) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 201 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 202 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
| 203 | struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); |
| 204 | struct stdio_dev *sdev = &uc_priv->sdev; |
| 205 | struct input_config *input = &uc_priv->input; |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 206 | int ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 207 | |
Simon Glass | bf45b2e | 2017-05-18 20:09:53 -0600 | [diff] [blame] | 208 | ret = cros_ec_keyb_decode_fdt(dev, priv); |
| 209 | if (ret) { |
| 210 | debug("%s: Cannot decode node (ret=%d)\n", __func__, ret); |
| 211 | return -EINVAL; |
| 212 | } |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 213 | input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS); |
| 214 | ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols, |
| 215 | priv->ghost_filter); |
| 216 | if (ret) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 217 | debug("%s: cannot init key matrix\n", __func__); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 218 | return ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 219 | } |
Simon Glass | bf45b2e | 2017-05-18 20:09:53 -0600 | [diff] [blame] | 220 | ret = key_matrix_decode_fdt(dev, &priv->matrix); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 221 | if (ret) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 222 | debug("%s: Could not decode key matrix from fdt\n", __func__); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 223 | return ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 224 | } |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 225 | debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows, |
| 226 | priv->key_cols); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 227 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 228 | priv->input = input; |
| 229 | input->dev = dev; |
Simon Glass | e6fa485 | 2015-11-11 10:05:37 -0700 | [diff] [blame] | 230 | input_add_tables(input, false); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 231 | input->read_keys = cros_ec_kbc_check; |
| 232 | strcpy(sdev->name, "cros-ec-keyb"); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 233 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 234 | /* Register the device. cros_ec_init_keyboard() will be called soon */ |
| 235 | return input_stdio_register(sdev); |
| 236 | } |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 237 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 238 | static const struct keyboard_ops cros_ec_kbd_ops = { |
| 239 | }; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 240 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 241 | static const struct udevice_id cros_ec_kbd_ids[] = { |
| 242 | { .compatible = "google,cros-ec-keyb" }, |
| 243 | { } |
| 244 | }; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 245 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 246 | U_BOOT_DRIVER(google_cros_ec_keyb) = { |
| 247 | .name = "google_cros_ec_keyb", |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 248 | .id = UCLASS_KEYBOARD, |
| 249 | .of_match = cros_ec_kbd_ids, |
| 250 | .probe = cros_ec_kbd_probe, |
| 251 | .ops = &cros_ec_kbd_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 252 | .priv_auto = sizeof(struct cros_ec_keyb_priv), |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 253 | }; |