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 | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 12 | #include <errno.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 13 | #include <fdtdec.h> |
| 14 | #include <input.h> |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 15 | #include <keyboard.h> |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 16 | #include <key_matrix.h> |
| 17 | #include <stdio_dev.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | enum { |
| 22 | KBC_MAX_KEYS = 8, /* Maximum keys held down at once */ |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 23 | KBC_REPEAT_RATE_MS = 30, |
| 24 | KBC_REPEAT_DELAY_MS = 240, |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 25 | }; |
| 26 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 27 | struct cros_ec_keyb_priv { |
| 28 | struct input_config *input; /* The input layer */ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 29 | struct key_matrix matrix; /* The key matrix layer */ |
| 30 | int key_rows; /* Number of keyboard rows */ |
| 31 | int key_cols; /* Number of keyboard columns */ |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 32 | int ghost_filter; /* 1 to enable ghost filter, else 0 */ |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 33 | }; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 34 | |
| 35 | |
| 36 | /** |
| 37 | * Check the keyboard controller and return a list of key matrix positions |
| 38 | * for which a key is pressed |
| 39 | * |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 40 | * @param dev Keyboard device |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | */ |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 46 | static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys, |
| 47 | int max_count, bool *samep) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 48 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 49 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 57 | if (cros_ec_scan_keyboard(dev->parent, &scan)) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 72 | for (col = num_keys = bit = 0; col < priv->matrix.num_cols; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 73 | col++) { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 74 | for (row = 0; row < priv->matrix.num_rows; row++) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 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 | /** |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 92 | * Check the keyboard, and send any keys that are pressed. |
| 93 | * |
| 94 | * This is called by input_tstc() and input_getc() when they need more |
| 95 | * characters |
| 96 | * |
| 97 | * @param input Input configuration |
| 98 | * @return 1, to indicate that we have something to look at |
| 99 | */ |
| 100 | int cros_ec_kbc_check(struct input_config *input) |
| 101 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 102 | struct udevice *dev = input->dev; |
| 103 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 104 | static struct key_matrix_key last_keys[KBC_MAX_KEYS]; |
| 105 | static int last_num_keys; |
| 106 | struct key_matrix_key keys[KBC_MAX_KEYS]; |
| 107 | int keycodes[KBC_MAX_KEYS]; |
| 108 | int num_keys, num_keycodes; |
| 109 | int irq_pending, sent; |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 110 | bool same = false; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Loop until the EC has no more keyscan records, or we have |
| 114 | * received at least one character. This means we know that tstc() |
| 115 | * will always return non-zero if keys have been pressed. |
| 116 | * |
| 117 | * Without this loop, a key release (which generates no new ascii |
| 118 | * characters) will cause us to exit this function, and just tstc() |
| 119 | * may return 0 before all keys have been read from the EC. |
| 120 | */ |
| 121 | do { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 122 | irq_pending = cros_ec_interrupt_pending(dev->parent); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 123 | if (irq_pending) { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 124 | num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS, |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 125 | &same); |
| 126 | if (num_keys < 0) |
| 127 | return 0; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 128 | last_num_keys = num_keys; |
| 129 | memcpy(last_keys, keys, sizeof(keys)); |
| 130 | } else { |
| 131 | /* |
| 132 | * EC doesn't want to be asked, so use keys from last |
| 133 | * time. |
| 134 | */ |
| 135 | num_keys = last_num_keys; |
| 136 | memcpy(keys, last_keys, sizeof(keys)); |
| 137 | } |
| 138 | |
| 139 | if (num_keys < 0) |
| 140 | return -1; |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 141 | num_keycodes = key_matrix_decode(&priv->matrix, keys, |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 142 | num_keys, keycodes, KBC_MAX_KEYS); |
| 143 | sent = input_send_keycodes(input, keycodes, num_keycodes); |
Simon Glass | 041699f | 2014-02-27 13:26:05 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * For those ECs without an interrupt, stop scanning when we |
| 147 | * see that the scan is the same as last time. |
| 148 | */ |
| 149 | if ((irq_pending < 0) && same) |
| 150 | break; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 151 | } while (irq_pending && !sent); |
| 152 | |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Decode MBKP keyboard details from the device tree |
| 158 | * |
| 159 | * @param blob Device tree blob |
| 160 | * @param node Node to decode from |
| 161 | * @param config Configuration data read from fdt |
| 162 | * @return 0 if ok, -1 on error |
| 163 | */ |
| 164 | static int cros_ec_keyb_decode_fdt(const void *blob, int node, |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 165 | struct cros_ec_keyb_priv *config) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 166 | { |
| 167 | /* |
| 168 | * Get keyboard rows and columns - at present we are limited to |
| 169 | * 8 columns by the protocol (one byte per row scan) |
| 170 | */ |
Sjoerd Simons | d2f895f | 2014-11-27 16:34:08 +0100 | [diff] [blame] | 171 | config->key_rows = fdtdec_get_int(blob, node, "keypad,num-rows", 0); |
| 172 | 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] | 173 | if (!config->key_rows || !config->key_cols || |
| 174 | config->key_rows * config->key_cols / 8 |
| 175 | > CROS_EC_KEYSCAN_COLS) { |
| 176 | debug("%s: Invalid key matrix size %d x %d\n", __func__, |
| 177 | config->key_rows, config->key_cols); |
| 178 | return -1; |
| 179 | } |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 180 | config->ghost_filter = fdtdec_get_bool(blob, node, |
Simon Glass | 73f220c | 2015-03-26 09:29:39 -0600 | [diff] [blame] | 181 | "google,needs-ghost-filter"); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 185 | static int cros_ec_kbd_probe(struct udevice *dev) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 186 | { |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 187 | struct cros_ec_keyb_priv *priv = dev_get_priv(dev); |
| 188 | struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); |
| 189 | struct stdio_dev *sdev = &uc_priv->sdev; |
| 190 | struct input_config *input = &uc_priv->input; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 191 | const void *blob = gd->fdt_blob; |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 192 | int node = dev->of_offset; |
| 193 | int ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 194 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 195 | if (cros_ec_keyb_decode_fdt(blob, node, priv)) |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 196 | return -1; |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 197 | input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS); |
| 198 | ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols, |
| 199 | priv->ghost_filter); |
| 200 | if (ret) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 201 | debug("%s: cannot init key matrix\n", __func__); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 202 | return ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 203 | } |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 204 | ret = key_matrix_decode_fdt(&priv->matrix, gd->fdt_blob, node); |
| 205 | if (ret) { |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 206 | debug("%s: Could not decode key matrix from fdt\n", __func__); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 207 | return ret; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 208 | } |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 209 | debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows, |
| 210 | priv->key_cols); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 211 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 212 | priv->input = input; |
| 213 | input->dev = dev; |
Simon Glass | e6fa485 | 2015-11-11 10:05:37 -0700 | [diff] [blame] | 214 | input_add_tables(input, false); |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 215 | input->read_keys = cros_ec_kbc_check; |
| 216 | strcpy(sdev->name, "cros-ec-keyb"); |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 217 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 218 | /* Register the device. cros_ec_init_keyboard() will be called soon */ |
| 219 | return input_stdio_register(sdev); |
| 220 | } |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 221 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 222 | static const struct keyboard_ops cros_ec_kbd_ops = { |
| 223 | }; |
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 | static const struct udevice_id cros_ec_kbd_ids[] = { |
| 226 | { .compatible = "google,cros-ec-keyb" }, |
| 227 | { } |
| 228 | }; |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 229 | |
Simon Glass | aa8484f | 2015-10-18 21:17:17 -0600 | [diff] [blame] | 230 | U_BOOT_DRIVER(cros_ec_kbd) = { |
| 231 | .name = "cros_ec_kbd", |
| 232 | .id = UCLASS_KEYBOARD, |
| 233 | .of_match = cros_ec_kbd_ids, |
| 234 | .probe = cros_ec_kbd_probe, |
| 235 | .ops = &cros_ec_kbd_ops, |
| 236 | .priv_auto_alloc_size = sizeof(struct cros_ec_keyb_priv), |
| 237 | }; |