blob: 0917ee20fedc76cc22990de667d3ff6b76c6bedc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08002/*
3 * Chromium OS Matrix Keyboard
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08006 */
7
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08008#include <cros_ec.h>
Simon Glassaa8484f2015-10-18 21:17:17 -06009#include <dm.h>
Simon Glass041699f2014-02-27 13:26:05 -070010#include <errno.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080011#include <input.h>
Simon Glassaa8484f2015-10-18 21:17:17 -060012#include <keyboard.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080013#include <key_matrix.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080015#include <stdio_dev.h>
16
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080017enum {
18 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
Sjoerd Simonsd2f895f2014-11-27 16:34:08 +010019 KBC_REPEAT_RATE_MS = 30,
20 KBC_REPEAT_DELAY_MS = 240,
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080021};
22
Simon Glassaa8484f2015-10-18 21:17:17 -060023struct cros_ec_keyb_priv {
24 struct input_config *input; /* The input layer */
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080025 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 Tyan4a48bcf2013-05-15 18:27:32 +080028 int ghost_filter; /* 1 to enable ghost filter, else 0 */
Simon Glassaa8484f2015-10-18 21:17:17 -060029};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080030
31
32/**
33 * Check the keyboard controller and return a list of key matrix positions
34 * for which a key is pressed
35 *
Simon Glassaa8484f2015-10-18 21:17:17 -060036 * @param dev Keyboard device
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080037 * @param keys List of keys that we have detected
38 * @param max_count Maximum number of keys to return
Simon Glass041699f2014-02-27 13:26:05 -070039 * @param samep Set to true if this scan repeats the last, else false
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010040 * Return: number of pressed keys, 0 for none, -EIO on error
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080041 */
Simon Glassaa8484f2015-10-18 21:17:17 -060042static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys,
43 int max_count, bool *samep)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080044{
Simon Glassaa8484f2015-10-18 21:17:17 -060045 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080046 struct key_matrix_key *key;
Simon Glass041699f2014-02-27 13:26:05 -070047 static struct mbkp_keyscan last_scan;
48 static bool last_scan_valid;
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030049 struct ec_response_get_next_event event;
50 struct mbkp_keyscan *scan = (struct mbkp_keyscan *)
51 &event.data.key_matrix;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080052 unsigned int row, col, bit, data;
53 int num_keys;
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030054 int ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080055
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030056 /* Get pending MKBP event. It may not be a key matrix event. */
57 do {
58 ret = cros_ec_get_next_event(dev->parent, &event);
59 /* The EC has no events for us at this time. */
60 if (ret == -EC_RES_UNAVAILABLE)
61 return -EIO;
62 else if (ret)
63 break;
64 } while (event.event_type != EC_MKBP_EVENT_KEY_MATRIX);
65
66 /* Try the old command if the EC doesn't support the above. */
67 if (ret == -EC_RES_INVALID_COMMAND) {
68 if (cros_ec_scan_keyboard(dev->parent, scan)) {
69 debug("%s: keyboard scan failed\n", __func__);
70 return -EIO;
71 }
72 } else if (ret) {
73 debug("%s: Error getting next MKBP event. (%d)\n",
74 __func__, ret);
Simon Glass041699f2014-02-27 13:26:05 -070075 return -EIO;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080076 }
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030077 *samep = last_scan_valid && !memcmp(&last_scan, scan, sizeof(*scan));
Simon Glass041699f2014-02-27 13:26:05 -070078
79 /*
80 * This is a bit odd. The EC has no way to tell us that it has run
81 * out of key scans. It just returns the same scan over and over
82 * again. So the only way to detect that we have run out is to detect
83 * that this scan is the same as the last.
84 */
85 last_scan_valid = true;
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030086 memcpy(&last_scan, scan, sizeof(last_scan));
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080087
Simon Glassaa8484f2015-10-18 21:17:17 -060088 for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080089 col++) {
Simon Glassaa8484f2015-10-18 21:17:17 -060090 for (row = 0; row < priv->matrix.num_rows; row++) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080091 unsigned int mask = 1 << (bit & 7);
92
Alper Nebi Yasak1ce07292020-10-30 20:25:20 +030093 data = scan->data[bit / 8];
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080094 if ((data & mask) && num_keys < max_count) {
95 key = keys + num_keys++;
96 key->row = row;
97 key->col = col;
98 key->valid = 1;
99 }
100 bit++;
101 }
102 }
103
104 return num_keys;
105}
106
107/**
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800108 * Check the keyboard, and send any keys that are pressed.
109 *
110 * This is called by input_tstc() and input_getc() when they need more
111 * characters
112 *
113 * @param input Input configuration
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100114 * Return: 1, to indicate that we have something to look at
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800115 */
116int cros_ec_kbc_check(struct input_config *input)
117{
Simon Glassaa8484f2015-10-18 21:17:17 -0600118 struct udevice *dev = input->dev;
119 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800120 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
121 static int last_num_keys;
122 struct key_matrix_key keys[KBC_MAX_KEYS];
123 int keycodes[KBC_MAX_KEYS];
124 int num_keys, num_keycodes;
125 int irq_pending, sent;
Simon Glass041699f2014-02-27 13:26:05 -0700126 bool same = false;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800127
128 /*
129 * Loop until the EC has no more keyscan records, or we have
130 * received at least one character. This means we know that tstc()
131 * will always return non-zero if keys have been pressed.
132 *
133 * Without this loop, a key release (which generates no new ascii
134 * characters) will cause us to exit this function, and just tstc()
135 * may return 0 before all keys have been read from the EC.
136 */
137 do {
Simon Glassaa8484f2015-10-18 21:17:17 -0600138 irq_pending = cros_ec_interrupt_pending(dev->parent);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800139 if (irq_pending) {
Simon Glassaa8484f2015-10-18 21:17:17 -0600140 num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
Simon Glass041699f2014-02-27 13:26:05 -0700141 &same);
142 if (num_keys < 0)
143 return 0;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800144 last_num_keys = num_keys;
145 memcpy(last_keys, keys, sizeof(keys));
146 } else {
147 /*
148 * EC doesn't want to be asked, so use keys from last
149 * time.
150 */
151 num_keys = last_num_keys;
152 memcpy(keys, last_keys, sizeof(keys));
153 }
154
155 if (num_keys < 0)
156 return -1;
Simon Glassaa8484f2015-10-18 21:17:17 -0600157 num_keycodes = key_matrix_decode(&priv->matrix, keys,
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800158 num_keys, keycodes, KBC_MAX_KEYS);
159 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glass041699f2014-02-27 13:26:05 -0700160
161 /*
162 * For those ECs without an interrupt, stop scanning when we
163 * see that the scan is the same as last time.
164 */
165 if ((irq_pending < 0) && same)
166 break;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800167 } while (irq_pending && !sent);
168
169 return 1;
170}
171
172/**
173 * Decode MBKP keyboard details from the device tree
174 *
175 * @param blob Device tree blob
176 * @param node Node to decode from
177 * @param config Configuration data read from fdt
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100178 * Return: 0 if ok, -1 on error
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800179 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600180static int cros_ec_keyb_decode_fdt(struct udevice *dev,
181 struct cros_ec_keyb_priv *config)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800182{
183 /*
184 * Get keyboard rows and columns - at present we are limited to
185 * 8 columns by the protocol (one byte per row scan)
186 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600187 config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
188 config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800189 if (!config->key_rows || !config->key_cols ||
190 config->key_rows * config->key_cols / 8
191 > CROS_EC_KEYSCAN_COLS) {
192 debug("%s: Invalid key matrix size %d x %d\n", __func__,
193 config->key_rows, config->key_cols);
194 return -1;
195 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600196 config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
197
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800198 return 0;
199}
200
Simon Glassaa8484f2015-10-18 21:17:17 -0600201static int cros_ec_kbd_probe(struct udevice *dev)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800202{
Simon Glassaa8484f2015-10-18 21:17:17 -0600203 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
204 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
205 struct stdio_dev *sdev = &uc_priv->sdev;
206 struct input_config *input = &uc_priv->input;
Simon Glassaa8484f2015-10-18 21:17:17 -0600207 int ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800208
Simon Glassbf45b2e2017-05-18 20:09:53 -0600209 ret = cros_ec_keyb_decode_fdt(dev, priv);
210 if (ret) {
211 debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
212 return -EINVAL;
213 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600214 input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
215 ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols,
216 priv->ghost_filter);
217 if (ret) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800218 debug("%s: cannot init key matrix\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600219 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800220 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600221 ret = key_matrix_decode_fdt(dev, &priv->matrix);
Simon Glassaa8484f2015-10-18 21:17:17 -0600222 if (ret) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800223 debug("%s: Could not decode key matrix from fdt\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600224 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800225 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600226 debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
227 priv->key_cols);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800228
Simon Glassaa8484f2015-10-18 21:17:17 -0600229 priv->input = input;
230 input->dev = dev;
Simon Glasse6fa4852015-11-11 10:05:37 -0700231 input_add_tables(input, false);
Simon Glassaa8484f2015-10-18 21:17:17 -0600232 input->read_keys = cros_ec_kbc_check;
233 strcpy(sdev->name, "cros-ec-keyb");
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800234
Simon Glassaa8484f2015-10-18 21:17:17 -0600235 /* Register the device. cros_ec_init_keyboard() will be called soon */
236 return input_stdio_register(sdev);
237}
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800238
Simon Glassaa8484f2015-10-18 21:17:17 -0600239static const struct keyboard_ops cros_ec_kbd_ops = {
240};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800241
Simon Glassaa8484f2015-10-18 21:17:17 -0600242static const struct udevice_id cros_ec_kbd_ids[] = {
243 { .compatible = "google,cros-ec-keyb" },
244 { }
245};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800246
Walter Lozano2901ac62020-06-25 01:10:04 -0300247U_BOOT_DRIVER(google_cros_ec_keyb) = {
248 .name = "google_cros_ec_keyb",
Simon Glassaa8484f2015-10-18 21:17:17 -0600249 .id = UCLASS_KEYBOARD,
250 .of_match = cros_ec_kbd_ids,
251 .probe = cros_ec_kbd_probe,
252 .ops = &cros_ec_kbd_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700253 .priv_auto = sizeof(struct cros_ec_keyb_priv),
Simon Glassaa8484f2015-10-18 21:17:17 -0600254};