blob: 8c1a25c73f772e9f3282b3b477d3467f11b6dd6b [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
8#include <common.h>
9#include <cros_ec.h>
Simon Glassaa8484f2015-10-18 21:17:17 -060010#include <dm.h>
Simon Glass041699f2014-02-27 13:26:05 -070011#include <errno.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080012#include <input.h>
Simon Glassaa8484f2015-10-18 21:17:17 -060013#include <keyboard.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080014#include <key_matrix.h>
15#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
40 * @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;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080049 struct mbkp_keyscan scan;
50 unsigned int row, col, bit, data;
51 int num_keys;
52
Simon Glassaa8484f2015-10-18 21:17:17 -060053 if (cros_ec_scan_keyboard(dev->parent, &scan)) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080054 debug("%s: keyboard scan failed\n", __func__);
Simon Glass041699f2014-02-27 13:26:05 -070055 return -EIO;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080056 }
Simon Glass041699f2014-02-27 13:26:05 -070057 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
58
59 /*
60 * This is a bit odd. The EC has no way to tell us that it has run
61 * out of key scans. It just returns the same scan over and over
62 * again. So the only way to detect that we have run out is to detect
63 * that this scan is the same as the last.
64 */
65 last_scan_valid = true;
66 memcpy(&last_scan, &scan, sizeof(last_scan));
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080067
Simon Glassaa8484f2015-10-18 21:17:17 -060068 for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080069 col++) {
Simon Glassaa8484f2015-10-18 21:17:17 -060070 for (row = 0; row < priv->matrix.num_rows; row++) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080071 unsigned int mask = 1 << (bit & 7);
72
73 data = scan.data[bit / 8];
74 if ((data & mask) && num_keys < max_count) {
75 key = keys + num_keys++;
76 key->row = row;
77 key->col = col;
78 key->valid = 1;
79 }
80 bit++;
81 }
82 }
83
84 return num_keys;
85}
86
87/**
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080088 * Check the keyboard, and send any keys that are pressed.
89 *
90 * This is called by input_tstc() and input_getc() when they need more
91 * characters
92 *
93 * @param input Input configuration
94 * @return 1, to indicate that we have something to look at
95 */
96int cros_ec_kbc_check(struct input_config *input)
97{
Simon Glassaa8484f2015-10-18 21:17:17 -060098 struct udevice *dev = input->dev;
99 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800100 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
101 static int last_num_keys;
102 struct key_matrix_key keys[KBC_MAX_KEYS];
103 int keycodes[KBC_MAX_KEYS];
104 int num_keys, num_keycodes;
105 int irq_pending, sent;
Simon Glass041699f2014-02-27 13:26:05 -0700106 bool same = false;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800107
108 /*
109 * Loop until the EC has no more keyscan records, or we have
110 * received at least one character. This means we know that tstc()
111 * will always return non-zero if keys have been pressed.
112 *
113 * Without this loop, a key release (which generates no new ascii
114 * characters) will cause us to exit this function, and just tstc()
115 * may return 0 before all keys have been read from the EC.
116 */
117 do {
Simon Glassaa8484f2015-10-18 21:17:17 -0600118 irq_pending = cros_ec_interrupt_pending(dev->parent);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800119 if (irq_pending) {
Simon Glassaa8484f2015-10-18 21:17:17 -0600120 num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
Simon Glass041699f2014-02-27 13:26:05 -0700121 &same);
122 if (num_keys < 0)
123 return 0;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800124 last_num_keys = num_keys;
125 memcpy(last_keys, keys, sizeof(keys));
126 } else {
127 /*
128 * EC doesn't want to be asked, so use keys from last
129 * time.
130 */
131 num_keys = last_num_keys;
132 memcpy(keys, last_keys, sizeof(keys));
133 }
134
135 if (num_keys < 0)
136 return -1;
Simon Glassaa8484f2015-10-18 21:17:17 -0600137 num_keycodes = key_matrix_decode(&priv->matrix, keys,
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800138 num_keys, keycodes, KBC_MAX_KEYS);
139 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glass041699f2014-02-27 13:26:05 -0700140
141 /*
142 * For those ECs without an interrupt, stop scanning when we
143 * see that the scan is the same as last time.
144 */
145 if ((irq_pending < 0) && same)
146 break;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800147 } while (irq_pending && !sent);
148
149 return 1;
150}
151
152/**
153 * Decode MBKP keyboard details from the device tree
154 *
155 * @param blob Device tree blob
156 * @param node Node to decode from
157 * @param config Configuration data read from fdt
158 * @return 0 if ok, -1 on error
159 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600160static int cros_ec_keyb_decode_fdt(struct udevice *dev,
161 struct cros_ec_keyb_priv *config)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800162{
163 /*
164 * Get keyboard rows and columns - at present we are limited to
165 * 8 columns by the protocol (one byte per row scan)
166 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600167 config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
168 config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800169 if (!config->key_rows || !config->key_cols ||
170 config->key_rows * config->key_cols / 8
171 > CROS_EC_KEYSCAN_COLS) {
172 debug("%s: Invalid key matrix size %d x %d\n", __func__,
173 config->key_rows, config->key_cols);
174 return -1;
175 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600176 config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
177
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800178 return 0;
179}
180
Simon Glassaa8484f2015-10-18 21:17:17 -0600181static int cros_ec_kbd_probe(struct udevice *dev)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800182{
Simon Glassaa8484f2015-10-18 21:17:17 -0600183 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
184 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
185 struct stdio_dev *sdev = &uc_priv->sdev;
186 struct input_config *input = &uc_priv->input;
Simon Glassaa8484f2015-10-18 21:17:17 -0600187 int ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800188
Simon Glassbf45b2e2017-05-18 20:09:53 -0600189 ret = cros_ec_keyb_decode_fdt(dev, priv);
190 if (ret) {
191 debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
192 return -EINVAL;
193 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600194 input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
195 ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols,
196 priv->ghost_filter);
197 if (ret) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800198 debug("%s: cannot init key matrix\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600199 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800200 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600201 ret = key_matrix_decode_fdt(dev, &priv->matrix);
Simon Glassaa8484f2015-10-18 21:17:17 -0600202 if (ret) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800203 debug("%s: Could not decode key matrix from fdt\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600204 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800205 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600206 debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
207 priv->key_cols);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800208
Simon Glassaa8484f2015-10-18 21:17:17 -0600209 priv->input = input;
210 input->dev = dev;
Simon Glasse6fa4852015-11-11 10:05:37 -0700211 input_add_tables(input, false);
Simon Glassaa8484f2015-10-18 21:17:17 -0600212 input->read_keys = cros_ec_kbc_check;
213 strcpy(sdev->name, "cros-ec-keyb");
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800214
Simon Glassaa8484f2015-10-18 21:17:17 -0600215 /* Register the device. cros_ec_init_keyboard() will be called soon */
216 return input_stdio_register(sdev);
217}
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800218
Simon Glassaa8484f2015-10-18 21:17:17 -0600219static const struct keyboard_ops cros_ec_kbd_ops = {
220};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800221
Simon Glassaa8484f2015-10-18 21:17:17 -0600222static const struct udevice_id cros_ec_kbd_ids[] = {
223 { .compatible = "google,cros-ec-keyb" },
224 { }
225};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800226
Simon Glassaa8484f2015-10-18 21:17:17 -0600227U_BOOT_DRIVER(cros_ec_kbd) = {
228 .name = "cros_ec_kbd",
229 .id = UCLASS_KEYBOARD,
230 .of_match = cros_ec_kbd_ids,
231 .probe = cros_ec_kbd_probe,
232 .ops = &cros_ec_kbd_ops,
233 .priv_auto_alloc_size = sizeof(struct cros_ec_keyb_priv),
234};