blob: 6f12ec8b0ac438cc4bf83bb2bd07185bf2e3bfbb [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;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080050 struct mbkp_keyscan scan;
51 unsigned int row, col, bit, data;
52 int num_keys;
53
Simon Glass1fa4bfd2015-10-18 21:17:17 -060054 if (cros_ec_scan_keyboard(dev->parent, &scan)) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +080055 debug("%s: keyboard scan failed\n", __func__);
Simon Glasse0dd81e2014-02-27 13:26:05 -070056 return -EIO;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080057 }
Simon Glasse0dd81e2014-02-27 13:26:05 -070058 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
59
60 /*
61 * This is a bit odd. The EC has no way to tell us that it has run
62 * out of key scans. It just returns the same scan over and over
63 * again. So the only way to detect that we have run out is to detect
64 * that this scan is the same as the last.
65 */
66 last_scan_valid = true;
67 memcpy(&last_scan, &scan, sizeof(last_scan));
Hung-ying Tyan713cb682013-05-15 18:27:32 +080068
Simon Glass1fa4bfd2015-10-18 21:17:17 -060069 for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
Hung-ying Tyan713cb682013-05-15 18:27:32 +080070 col++) {
Simon Glass1fa4bfd2015-10-18 21:17:17 -060071 for (row = 0; row < priv->matrix.num_rows; row++) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +080072 unsigned int mask = 1 << (bit & 7);
73
74 data = scan.data[bit / 8];
75 if ((data & mask) && num_keys < max_count) {
76 key = keys + num_keys++;
77 key->row = row;
78 key->col = col;
79 key->valid = 1;
80 }
81 bit++;
82 }
83 }
84
85 return num_keys;
86}
87
88/**
Hung-ying Tyan713cb682013-05-15 18:27:32 +080089 * Check the keyboard, and send any keys that are pressed.
90 *
91 * This is called by input_tstc() and input_getc() when they need more
92 * characters
93 *
94 * @param input Input configuration
95 * @return 1, to indicate that we have something to look at
96 */
97int cros_ec_kbc_check(struct input_config *input)
98{
Simon Glass1fa4bfd2015-10-18 21:17:17 -060099 struct udevice *dev = input->dev;
100 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800101 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
102 static int last_num_keys;
103 struct key_matrix_key keys[KBC_MAX_KEYS];
104 int keycodes[KBC_MAX_KEYS];
105 int num_keys, num_keycodes;
106 int irq_pending, sent;
Simon Glasse0dd81e2014-02-27 13:26:05 -0700107 bool same = false;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800108
109 /*
110 * Loop until the EC has no more keyscan records, or we have
111 * received at least one character. This means we know that tstc()
112 * will always return non-zero if keys have been pressed.
113 *
114 * Without this loop, a key release (which generates no new ascii
115 * characters) will cause us to exit this function, and just tstc()
116 * may return 0 before all keys have been read from the EC.
117 */
118 do {
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600119 irq_pending = cros_ec_interrupt_pending(dev->parent);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800120 if (irq_pending) {
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600121 num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
Simon Glasse0dd81e2014-02-27 13:26:05 -0700122 &same);
123 if (num_keys < 0)
124 return 0;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800125 last_num_keys = num_keys;
126 memcpy(last_keys, keys, sizeof(keys));
127 } else {
128 /*
129 * EC doesn't want to be asked, so use keys from last
130 * time.
131 */
132 num_keys = last_num_keys;
133 memcpy(keys, last_keys, sizeof(keys));
134 }
135
136 if (num_keys < 0)
137 return -1;
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600138 num_keycodes = key_matrix_decode(&priv->matrix, keys,
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800139 num_keys, keycodes, KBC_MAX_KEYS);
140 sent = input_send_keycodes(input, keycodes, num_keycodes);
Simon Glasse0dd81e2014-02-27 13:26:05 -0700141
142 /*
143 * For those ECs without an interrupt, stop scanning when we
144 * see that the scan is the same as last time.
145 */
146 if ((irq_pending < 0) && same)
147 break;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800148 } while (irq_pending && !sent);
149
150 return 1;
151}
152
153/**
154 * Decode MBKP keyboard details from the device tree
155 *
156 * @param blob Device tree blob
157 * @param node Node to decode from
158 * @param config Configuration data read from fdt
159 * @return 0 if ok, -1 on error
160 */
Simon Glass8327d412017-05-18 20:09:53 -0600161static int cros_ec_keyb_decode_fdt(struct udevice *dev,
162 struct cros_ec_keyb_priv *config)
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800163{
164 /*
165 * Get keyboard rows and columns - at present we are limited to
166 * 8 columns by the protocol (one byte per row scan)
167 */
Simon Glass8327d412017-05-18 20:09:53 -0600168 config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
169 config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800170 if (!config->key_rows || !config->key_cols ||
171 config->key_rows * config->key_cols / 8
172 > CROS_EC_KEYSCAN_COLS) {
173 debug("%s: Invalid key matrix size %d x %d\n", __func__,
174 config->key_rows, config->key_cols);
175 return -1;
176 }
Simon Glass8327d412017-05-18 20:09:53 -0600177 config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
178
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800179 return 0;
180}
181
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600182static int cros_ec_kbd_probe(struct udevice *dev)
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800183{
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600184 struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
185 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
186 struct stdio_dev *sdev = &uc_priv->sdev;
187 struct input_config *input = &uc_priv->input;
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600188 int ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800189
Simon Glass8327d412017-05-18 20:09:53 -0600190 ret = cros_ec_keyb_decode_fdt(dev, priv);
191 if (ret) {
192 debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
193 return -EINVAL;
194 }
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600195 input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
196 ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols,
197 priv->ghost_filter);
198 if (ret) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800199 debug("%s: cannot init key matrix\n", __func__);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600200 return ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800201 }
Simon Glass8327d412017-05-18 20:09:53 -0600202 ret = key_matrix_decode_fdt(dev, &priv->matrix);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600203 if (ret) {
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800204 debug("%s: Could not decode key matrix from fdt\n", __func__);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600205 return ret;
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800206 }
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600207 debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
208 priv->key_cols);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800209
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600210 priv->input = input;
211 input->dev = dev;
Simon Glassb1d7a182015-11-11 10:05:37 -0700212 input_add_tables(input, false);
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600213 input->read_keys = cros_ec_kbc_check;
214 strcpy(sdev->name, "cros-ec-keyb");
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800215
216 /* Register the device. cros_ec_init_keyboard() will be called soon */
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600217 return input_stdio_register(sdev);
Hung-ying Tyan713cb682013-05-15 18:27:32 +0800218}
Simon Glass1fa4bfd2015-10-18 21:17:17 -0600219
220static const struct keyboard_ops cros_ec_kbd_ops = {
221};
222
223static const struct udevice_id cros_ec_kbd_ids[] = {
224 { .compatible = "google,cros-ec-keyb" },
225 { }
226};
227
228U_BOOT_DRIVER(cros_ec_kbd) = {
229 .name = "cros_ec_kbd",
230 .id = UCLASS_KEYBOARD,
231 .of_match = cros_ec_kbd_ids,
232 .probe = cros_ec_kbd_probe,
233 .ops = &cros_ec_kbd_ops,
234 .priv_auto_alloc_size = sizeof(struct cros_ec_keyb_priv),
235};