blob: dc3b08c0f66865d9883e371160089f78657a2010 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan713cb682013-05-15 18:27:32 +08002/*
3 * Chromium OS Matrix Keyboard
4 *
5 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan713cb682013-05-15 18:27:32 +08006 */
7
8#include <common.h>
9#include <cros_ec.h>
Simon Glass1fa4bfd2015-10-18 21:17:17 -060010#include <dm.h>
Simon Glasse0dd81e2014-02-27 13:26:05 -070011#include <errno.h>
Hung-ying Tyan713cb682013-05-15 18:27:32 +080012#include <input.h>
Simon Glass1fa4bfd2015-10-18 21:17:17 -060013#include <keyboard.h>
Hung-ying Tyan713cb682013-05-15 18:27:32 +080014#include <key_matrix.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Hung-ying Tyan713cb682013-05-15 18:27:32 +080016#include <stdio_dev.h>
17
Hung-ying Tyan713cb682013-05-15 18:27:32 +080018enum {
19 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
Sjoerd Simons93322742014-11-27 16:34:08 +010020 KBC_REPEAT_RATE_MS = 30,
21 KBC_REPEAT_DELAY_MS = 240,
Hung-ying Tyan713cb682013-05-15 18:27:32 +080022};
23
Simon Glass1fa4bfd2015-10-18 21:17:17 -060024struct cros_ec_keyb_priv {
25 struct input_config *input; /* The input layer */
Hung-ying Tyan713cb682013-05-15 18:27:32 +080026 struct key_matrix matrix; /* The key matrix layer */
27 int key_rows; /* Number of keyboard rows */
28 int key_cols; /* Number of keyboard columns */
Hung-ying Tyan713cb682013-05-15 18:27:32 +080029 int ghost_filter; /* 1 to enable ghost filter, else 0 */
Simon Glass1fa4bfd2015-10-18 21:17:17 -060030};
Hung-ying Tyan713cb682013-05-15 18:27:32 +080031
32
33/**
34 * Check the keyboard controller and return a list of key matrix positions
35 * for which a key is pressed
36 *
Simon Glass1fa4bfd2015-10-18 21:17:17 -060037 * @param dev Keyboard device
Hung-ying Tyan713cb682013-05-15 18:27:32 +080038 * @param keys List of keys that we have detected
39 * @param max_count Maximum number of keys to return
Simon Glasse0dd81e2014-02-27 13:26:05 -070040 * @param samep Set to true if this scan repeats the last, else false
41 * @return number of pressed keys, 0 for none, -EIO on error
Hung-ying Tyan713cb682013-05-15 18:27:32 +080042 */
Simon Glass1fa4bfd2015-10-18 21:17:17 -060043static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys,
44 int max_count, bool *samep)
Hung-ying Tyan713cb682013-05-15 18:27:32 +080045{
Simon Glass1fa4bfd2015-10-18 21:17:17 -060046 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan713cb682013-05-15 18:27:32 +080047 struct key_matrix_key *key;
Simon Glasse0dd81e2014-02-27 13:26:05 -070048 static struct mbkp_keyscan last_scan;
49 static bool last_scan_valid;
Alper Nebi Yasak69007972020-10-30 20:25:20 +030050 struct ec_response_get_next_event event;
51 struct mbkp_keyscan *scan = (struct mbkp_keyscan *)
52 &event.data.key_matrix;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080053 unsigned int row, col, bit, data;
54 int num_keys;
Alper Nebi Yasak69007972020-10-30 20:25:20 +030055 int ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080056
Alper Nebi Yasak69007972020-10-30 20:25:20 +030057 /* Get pending MKBP event. It may not be a key matrix event. */
58 do {
59 ret = cros_ec_get_next_event(dev->parent, &event);
60 /* The EC has no events for us at this time. */
61 if (ret == -EC_RES_UNAVAILABLE)
62 return -EIO;
63 else if (ret)
64 break;
65 } while (event.event_type != EC_MKBP_EVENT_KEY_MATRIX);
66
67 /* Try the old command if the EC doesn't support the above. */
68 if (ret == -EC_RES_INVALID_COMMAND) {
69 if (cros_ec_scan_keyboard(dev->parent, scan)) {
70 debug("%s: keyboard scan failed\n", __func__);
71 return -EIO;
72 }
73 } else if (ret) {
74 debug("%s: Error getting next MKBP event. (%d)\n",
75 __func__, ret);
Simon Glasse0dd81e2014-02-27 13:26:05 -070076 return -EIO;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080077 }
Alper Nebi Yasak69007972020-10-30 20:25:20 +030078 *samep = last_scan_valid && !memcmp(&last_scan, scan, sizeof(*scan));
Simon Glasse0dd81e2014-02-27 13:26:05 -070079
80 /*
81 * This is a bit odd. The EC has no way to tell us that it has run
82 * out of key scans. It just returns the same scan over and over
83 * again. So the only way to detect that we have run out is to detect
84 * that this scan is the same as the last.
85 */
86 last_scan_valid = true;
Alper Nebi Yasak69007972020-10-30 20:25:20 +030087 memcpy(&last_scan, scan, sizeof(last_scan));
Hung-ying Tyan713cb682013-05-15 18:27:32 +080088
Simon Glass1fa4bfd2015-10-18 21:17:17 -060089 for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080090 col++) {
Simon Glass1fa4bfd2015-10-18 21:17:17 -060091 for (row = 0; row < priv->matrix.num_rows; row++) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +080092 unsigned int mask = 1 << (bit & 7);
93
Alper Nebi Yasak69007972020-10-30 20:25:20 +030094 data = scan->data[bit / 8];
Hung-ying Tyan713cb682013-05-15 18:27:32 +080095 if ((data & mask) && num_keys < max_count) {
96 key = keys + num_keys++;
97 key->row = row;
98 key->col = col;
99 key->valid = 1;
100 }
101 bit++;
102 }
103 }
104
105 return num_keys;
106}
107
108/**
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800109 * Check the keyboard, and send any keys that are pressed.
110 *
111 * This is called by input_tstc() and input_getc() when they need more
112 * characters
113 *
114 * @param input Input configuration
115 * @return 1, to indicate that we have something to look at
116 */
117int cros_ec_kbc_check(struct input_config *input)
118{
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600119 struct udevice *dev = input->dev;
120 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800121 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
122 static int last_num_keys;
123 struct key_matrix_key keys[KBC_MAX_KEYS];
124 int keycodes[KBC_MAX_KEYS];
125 int num_keys, num_keycodes;
126 int irq_pending, sent;
Simon Glasse0dd81e2014-02-27 13:26:05 -0700127 bool same = false;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800128
129 /*
130 * Loop until the EC has no more keyscan records, or we have
131 * received at least one character. This means we know that tstc()
132 * will always return non-zero if keys have been pressed.
133 *
134 * Without this loop, a key release (which generates no new ascii
135 * characters) will cause us to exit this function, and just tstc()
136 * may return 0 before all keys have been read from the EC.
137 */
138 do {
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600139 irq_pending = cros_ec_interrupt_pending(dev->parent);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800140 if (irq_pending) {
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600141 num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
Simon Glasse0dd81e2014-02-27 13:26:05 -0700142 &same);
143 if (num_keys < 0)
144 return 0;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800145 last_num_keys = num_keys;
146 memcpy(last_keys, keys, sizeof(keys));
147 } else {
148 /*
149 * EC doesn't want to be asked, so use keys from last
150 * time.
151 */
152 num_keys = last_num_keys;
153 memcpy(keys, last_keys, sizeof(keys));
154 }
155
156 if (num_keys < 0)
157 return -1;
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600158 num_keycodes = key_matrix_decode(&priv->matrix, keys,
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800159 num_keys, keycodes, KBC_MAX_KEYS);
160 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glasse0dd81e2014-02-27 13:26:05 -0700161
162 /*
163 * For those ECs without an interrupt, stop scanning when we
164 * see that the scan is the same as last time.
165 */
166 if ((irq_pending < 0) && same)
167 break;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800168 } while (irq_pending && !sent);
169
170 return 1;
171}
172
173/**
174 * Decode MBKP keyboard details from the device tree
175 *
176 * @param blob Device tree blob
177 * @param node Node to decode from
178 * @param config Configuration data read from fdt
179 * @return 0 if ok, -1 on error
180 */
Simon Glass8327d412017-05-18 20:09:53 -0600181static int cros_ec_keyb_decode_fdt(struct udevice *dev,
182 struct cros_ec_keyb_priv *config)
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800183{
184 /*
185 * Get keyboard rows and columns - at present we are limited to
186 * 8 columns by the protocol (one byte per row scan)
187 */
Simon Glass8327d412017-05-18 20:09:53 -0600188 config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
189 config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800190 if (!config->key_rows || !config->key_cols ||
191 config->key_rows * config->key_cols / 8
192 > CROS_EC_KEYSCAN_COLS) {
193 debug("%s: Invalid key matrix size %d x %d\n", __func__,
194 config->key_rows, config->key_cols);
195 return -1;
196 }
Simon Glass8327d412017-05-18 20:09:53 -0600197 config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
198
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800199 return 0;
200}
201
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600202static int cros_ec_kbd_probe(struct udevice *dev)
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800203{
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600204 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
205 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
206 struct stdio_dev *sdev = &uc_priv->sdev;
207 struct input_config *input = &uc_priv->input;
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600208 int ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800209
Simon Glass8327d412017-05-18 20:09:53 -0600210 ret = cros_ec_keyb_decode_fdt(dev, priv);
211 if (ret) {
212 debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
213 return -EINVAL;
214 }
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600215 input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
216 ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols,
217 priv->ghost_filter);
218 if (ret) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800219 debug("%s: cannot init key matrix\n", __func__);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600220 return ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800221 }
Simon Glass8327d412017-05-18 20:09:53 -0600222 ret = key_matrix_decode_fdt(dev, &priv->matrix);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600223 if (ret) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800224 debug("%s: Could not decode key matrix from fdt\n", __func__);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600225 return ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800226 }
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600227 debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
228 priv->key_cols);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800229
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600230 priv->input = input;
231 input->dev = dev;
Simon Glassb1d7a182015-11-11 10:05:37 -0700232 input_add_tables(input, false);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600233 input->read_keys = cros_ec_kbc_check;
234 strcpy(sdev->name, "cros-ec-keyb");
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800235
236 /* Register the device. cros_ec_init_keyboard() will be called soon */
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600237 return input_stdio_register(sdev);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800238}
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600239
240static const struct keyboard_ops cros_ec_kbd_ops = {
241};
242
243static const struct udevice_id cros_ec_kbd_ids[] = {
244 { .compatible = "google,cros-ec-keyb" },
245 { }
246};
247
Walter Lozanoe3e24702020-06-25 01:10:04 -0300248U_BOOT_DRIVER(google_cros_ec_keyb) = {
249 .name = "google_cros_ec_keyb",
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600250 .id = UCLASS_KEYBOARD,
251 .of_match = cros_ec_kbd_ids,
252 .probe = cros_ec_kbd_probe,
253 .ops = &cros_ec_kbd_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700254 .priv_auto = sizeof(struct cros_ec_keyb_priv),
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600255};