blob: 6fa35a63ddcf139a5c6d35af38203987c3e29f7f [file] [log] [blame]
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08001/*
2 * Chromium OS Matrix Keyboard
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +08007 */
8
9#include <common.h>
10#include <cros_ec.h>
Simon Glassaa8484f2015-10-18 21:17:17 -060011#include <dm.h>
Simon Glass041699f2014-02-27 13:26:05 -070012#include <errno.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080013#include <input.h>
Simon Glassaa8484f2015-10-18 21:17:17 -060014#include <keyboard.h>
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080015#include <key_matrix.h>
16#include <stdio_dev.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20enum {
21 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
Sjoerd Simonsd2f895f2014-11-27 16:34:08 +010022 KBC_REPEAT_RATE_MS = 30,
23 KBC_REPEAT_DELAY_MS = 240,
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080024};
25
Simon Glassaa8484f2015-10-18 21:17:17 -060026struct cros_ec_keyb_priv {
27 struct input_config *input; /* The input layer */
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080028 struct key_matrix matrix; /* The key matrix layer */
29 int key_rows; /* Number of keyboard rows */
30 int key_cols; /* Number of keyboard columns */
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080031 int ghost_filter; /* 1 to enable ghost filter, else 0 */
Simon Glassaa8484f2015-10-18 21:17:17 -060032};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080033
34
35/**
36 * Check the keyboard controller and return a list of key matrix positions
37 * for which a key is pressed
38 *
Simon Glassaa8484f2015-10-18 21:17:17 -060039 * @param dev Keyboard device
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080040 * @param keys List of keys that we have detected
41 * @param max_count Maximum number of keys to return
Simon Glass041699f2014-02-27 13:26:05 -070042 * @param samep Set to true if this scan repeats the last, else false
43 * @return number of pressed keys, 0 for none, -EIO on error
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080044 */
Simon Glassaa8484f2015-10-18 21:17:17 -060045static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys,
46 int max_count, bool *samep)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080047{
Simon Glassaa8484f2015-10-18 21:17:17 -060048 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080049 struct key_matrix_key *key;
Simon Glass041699f2014-02-27 13:26:05 -070050 static struct mbkp_keyscan last_scan;
51 static bool last_scan_valid;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080052 struct mbkp_keyscan scan;
53 unsigned int row, col, bit, data;
54 int num_keys;
55
Simon Glassaa8484f2015-10-18 21:17:17 -060056 if (cros_ec_scan_keyboard(dev->parent, &scan)) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080057 debug("%s: keyboard scan failed\n", __func__);
Simon Glass041699f2014-02-27 13:26:05 -070058 return -EIO;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080059 }
Simon Glass041699f2014-02-27 13:26:05 -070060 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
61
62 /*
63 * This is a bit odd. The EC has no way to tell us that it has run
64 * out of key scans. It just returns the same scan over and over
65 * again. So the only way to detect that we have run out is to detect
66 * that this scan is the same as the last.
67 */
68 last_scan_valid = true;
69 memcpy(&last_scan, &scan, sizeof(last_scan));
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080070
Simon Glassaa8484f2015-10-18 21:17:17 -060071 for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080072 col++) {
Simon Glassaa8484f2015-10-18 21:17:17 -060073 for (row = 0; row < priv->matrix.num_rows; row++) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080074 unsigned int mask = 1 << (bit & 7);
75
76 data = scan.data[bit / 8];
77 if ((data & mask) && num_keys < max_count) {
78 key = keys + num_keys++;
79 key->row = row;
80 key->col = col;
81 key->valid = 1;
82 }
83 bit++;
84 }
85 }
86
87 return num_keys;
88}
89
90/**
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080091 * Check the keyboard, and send any keys that are pressed.
92 *
93 * This is called by input_tstc() and input_getc() when they need more
94 * characters
95 *
96 * @param input Input configuration
97 * @return 1, to indicate that we have something to look at
98 */
99int cros_ec_kbc_check(struct input_config *input)
100{
Simon Glassaa8484f2015-10-18 21:17:17 -0600101 struct udevice *dev = input->dev;
102 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800103 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
104 static int last_num_keys;
105 struct key_matrix_key keys[KBC_MAX_KEYS];
106 int keycodes[KBC_MAX_KEYS];
107 int num_keys, num_keycodes;
108 int irq_pending, sent;
Simon Glass041699f2014-02-27 13:26:05 -0700109 bool same = false;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800110
111 /*
112 * Loop until the EC has no more keyscan records, or we have
113 * received at least one character. This means we know that tstc()
114 * will always return non-zero if keys have been pressed.
115 *
116 * Without this loop, a key release (which generates no new ascii
117 * characters) will cause us to exit this function, and just tstc()
118 * may return 0 before all keys have been read from the EC.
119 */
120 do {
Simon Glassaa8484f2015-10-18 21:17:17 -0600121 irq_pending = cros_ec_interrupt_pending(dev->parent);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800122 if (irq_pending) {
Simon Glassaa8484f2015-10-18 21:17:17 -0600123 num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
Simon Glass041699f2014-02-27 13:26:05 -0700124 &same);
125 if (num_keys < 0)
126 return 0;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800127 last_num_keys = num_keys;
128 memcpy(last_keys, keys, sizeof(keys));
129 } else {
130 /*
131 * EC doesn't want to be asked, so use keys from last
132 * time.
133 */
134 num_keys = last_num_keys;
135 memcpy(keys, last_keys, sizeof(keys));
136 }
137
138 if (num_keys < 0)
139 return -1;
Simon Glassaa8484f2015-10-18 21:17:17 -0600140 num_keycodes = key_matrix_decode(&priv->matrix, keys,
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800141 num_keys, keycodes, KBC_MAX_KEYS);
142 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glass041699f2014-02-27 13:26:05 -0700143
144 /*
145 * For those ECs without an interrupt, stop scanning when we
146 * see that the scan is the same as last time.
147 */
148 if ((irq_pending < 0) && same)
149 break;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800150 } while (irq_pending && !sent);
151
152 return 1;
153}
154
155/**
156 * Decode MBKP keyboard details from the device tree
157 *
158 * @param blob Device tree blob
159 * @param node Node to decode from
160 * @param config Configuration data read from fdt
161 * @return 0 if ok, -1 on error
162 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600163static int cros_ec_keyb_decode_fdt(struct udevice *dev,
164 struct cros_ec_keyb_priv *config)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800165{
166 /*
167 * Get keyboard rows and columns - at present we are limited to
168 * 8 columns by the protocol (one byte per row scan)
169 */
Simon Glassbf45b2e2017-05-18 20:09:53 -0600170 config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
171 config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800172 if (!config->key_rows || !config->key_cols ||
173 config->key_rows * config->key_cols / 8
174 > CROS_EC_KEYSCAN_COLS) {
175 debug("%s: Invalid key matrix size %d x %d\n", __func__,
176 config->key_rows, config->key_cols);
177 return -1;
178 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600179 config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
180
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800181 return 0;
182}
183
Simon Glassaa8484f2015-10-18 21:17:17 -0600184static int cros_ec_kbd_probe(struct udevice *dev)
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800185{
Simon Glassaa8484f2015-10-18 21:17:17 -0600186 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
187 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
188 struct stdio_dev *sdev = &uc_priv->sdev;
189 struct input_config *input = &uc_priv->input;
Simon Glassaa8484f2015-10-18 21:17:17 -0600190 int ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800191
Simon Glassbf45b2e2017-05-18 20:09:53 -0600192 ret = cros_ec_keyb_decode_fdt(dev, priv);
193 if (ret) {
194 debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
195 return -EINVAL;
196 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600197 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 Tyan4a48bcf2013-05-15 18:27:32 +0800201 debug("%s: cannot init key matrix\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600202 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800203 }
Simon Glassbf45b2e2017-05-18 20:09:53 -0600204 ret = key_matrix_decode_fdt(dev, &priv->matrix);
Simon Glassaa8484f2015-10-18 21:17:17 -0600205 if (ret) {
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800206 debug("%s: Could not decode key matrix from fdt\n", __func__);
Simon Glassaa8484f2015-10-18 21:17:17 -0600207 return ret;
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800208 }
Simon Glassaa8484f2015-10-18 21:17:17 -0600209 debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
210 priv->key_cols);
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800211
Simon Glassaa8484f2015-10-18 21:17:17 -0600212 priv->input = input;
213 input->dev = dev;
Simon Glasse6fa4852015-11-11 10:05:37 -0700214 input_add_tables(input, false);
Simon Glassaa8484f2015-10-18 21:17:17 -0600215 input->read_keys = cros_ec_kbc_check;
216 strcpy(sdev->name, "cros-ec-keyb");
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800217
Simon Glassaa8484f2015-10-18 21:17:17 -0600218 /* Register the device. cros_ec_init_keyboard() will be called soon */
219 return input_stdio_register(sdev);
220}
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800221
Simon Glassaa8484f2015-10-18 21:17:17 -0600222static const struct keyboard_ops cros_ec_kbd_ops = {
223};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800224
Simon Glassaa8484f2015-10-18 21:17:17 -0600225static const struct udevice_id cros_ec_kbd_ids[] = {
226 { .compatible = "google,cros-ec-keyb" },
227 { }
228};
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +0800229
Simon Glassaa8484f2015-10-18 21:17:17 -0600230U_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};