]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/input/cros_ec_keyb.c
dm: tegra: Convert keyboard driver to driver model
[people/ms/u-boot.git] / drivers / input / cros_ec_keyb.c
CommitLineData
713cb680
HT
1/*
2 * Chromium OS Matrix Keyboard
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
713cb680 5 *
1a459660 6 * SPDX-License-Identifier: GPL-2.0+
713cb680
HT
7 */
8
9#include <common.h>
10#include <cros_ec.h>
e0dd81e3 11#include <errno.h>
713cb680
HT
12#include <fdtdec.h>
13#include <input.h>
14#include <key_matrix.h>
15#include <stdio_dev.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19enum {
20 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
93322749
SS
21 KBC_REPEAT_RATE_MS = 30,
22 KBC_REPEAT_DELAY_MS = 240,
713cb680
HT
23};
24
25static struct keyb {
26 struct cros_ec_dev *dev; /* The CROS_EC device */
27 struct input_config input; /* The input layer */
28 struct key_matrix matrix; /* The key matrix layer */
29 int key_rows; /* Number of keyboard rows */
30 int key_cols; /* Number of keyboard columns */
713cb680
HT
31 int ghost_filter; /* 1 to enable ghost filter, else 0 */
32 int inited; /* 1 if keyboard is ready */
33} config;
34
35
36/**
37 * Check the keyboard controller and return a list of key matrix positions
38 * for which a key is pressed
39 *
40 * @param config Keyboard config
41 * @param keys List of keys that we have detected
42 * @param max_count Maximum number of keys to return
e0dd81e3
SG
43 * @param samep Set to true if this scan repeats the last, else false
44 * @return number of pressed keys, 0 for none, -EIO on error
713cb680
HT
45 */
46static int check_for_keys(struct keyb *config,
e0dd81e3
SG
47 struct key_matrix_key *keys, int max_count,
48 bool *samep)
713cb680
HT
49{
50 struct key_matrix_key *key;
e0dd81e3
SG
51 static struct mbkp_keyscan last_scan;
52 static bool last_scan_valid;
713cb680
HT
53 struct mbkp_keyscan scan;
54 unsigned int row, col, bit, data;
55 int num_keys;
56
745009c4 57 if (cros_ec_scan_keyboard(config->dev->dev, &scan)) {
713cb680 58 debug("%s: keyboard scan failed\n", __func__);
e0dd81e3 59 return -EIO;
713cb680 60 }
e0dd81e3
SG
61 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
62
63 /*
64 * This is a bit odd. The EC has no way to tell us that it has run
65 * out of key scans. It just returns the same scan over and over
66 * again. So the only way to detect that we have run out is to detect
67 * that this scan is the same as the last.
68 */
69 last_scan_valid = true;
70 memcpy(&last_scan, &scan, sizeof(last_scan));
713cb680
HT
71
72 for (col = num_keys = bit = 0; col < config->matrix.num_cols;
73 col++) {
74 for (row = 0; row < config->matrix.num_rows; row++) {
75 unsigned int mask = 1 << (bit & 7);
76
77 data = scan.data[bit / 8];
78 if ((data & mask) && num_keys < max_count) {
79 key = keys + num_keys++;
80 key->row = row;
81 key->col = col;
82 key->valid = 1;
83 }
84 bit++;
85 }
86 }
87
88 return num_keys;
89}
90
91/**
92 * Test if keys are available to be read
93 *
94 * @return 0 if no keys available, 1 if keys are available
95 */
709ea543 96static int kbd_tstc(struct stdio_dev *dev)
713cb680
HT
97{
98 /* Just get input to do this for us */
99 return config.inited ? input_tstc(&config.input) : 0;
100}
101
102/**
103 * Read a key
104 *
105 * @return ASCII key code, or 0 if no key, or -1 if error
106 */
709ea543 107static int kbd_getc(struct stdio_dev *dev)
713cb680
HT
108{
109 /* Just get input to do this for us */
110 return config.inited ? input_getc(&config.input) : 0;
111}
112
113/**
114 * Check the keyboard, and send any keys that are pressed.
115 *
116 * This is called by input_tstc() and input_getc() when they need more
117 * characters
118 *
119 * @param input Input configuration
120 * @return 1, to indicate that we have something to look at
121 */
122int cros_ec_kbc_check(struct input_config *input)
123{
124 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
125 static int last_num_keys;
126 struct key_matrix_key keys[KBC_MAX_KEYS];
127 int keycodes[KBC_MAX_KEYS];
128 int num_keys, num_keycodes;
129 int irq_pending, sent;
e0dd81e3 130 bool same = false;
713cb680
HT
131
132 /*
133 * Loop until the EC has no more keyscan records, or we have
134 * received at least one character. This means we know that tstc()
135 * will always return non-zero if keys have been pressed.
136 *
137 * Without this loop, a key release (which generates no new ascii
138 * characters) will cause us to exit this function, and just tstc()
139 * may return 0 before all keys have been read from the EC.
140 */
141 do {
745009c4 142 irq_pending = cros_ec_interrupt_pending(config.dev->dev);
713cb680 143 if (irq_pending) {
e0dd81e3
SG
144 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS,
145 &same);
146 if (num_keys < 0)
147 return 0;
713cb680
HT
148 last_num_keys = num_keys;
149 memcpy(last_keys, keys, sizeof(keys));
150 } else {
151 /*
152 * EC doesn't want to be asked, so use keys from last
153 * time.
154 */
155 num_keys = last_num_keys;
156 memcpy(keys, last_keys, sizeof(keys));
157 }
158
159 if (num_keys < 0)
160 return -1;
161 num_keycodes = key_matrix_decode(&config.matrix, keys,
162 num_keys, keycodes, KBC_MAX_KEYS);
163 sent = input_send_keycodes(input, keycodes, num_keycodes);
e0dd81e3
SG
164
165 /*
166 * For those ECs without an interrupt, stop scanning when we
167 * see that the scan is the same as last time.
168 */
169 if ((irq_pending < 0) && same)
170 break;
713cb680
HT
171 } while (irq_pending && !sent);
172
173 return 1;
174}
175
176/**
177 * Decode MBKP keyboard details from the device tree
178 *
179 * @param blob Device tree blob
180 * @param node Node to decode from
181 * @param config Configuration data read from fdt
182 * @return 0 if ok, -1 on error
183 */
184static int cros_ec_keyb_decode_fdt(const void *blob, int node,
185 struct keyb *config)
186{
187 /*
188 * Get keyboard rows and columns - at present we are limited to
189 * 8 columns by the protocol (one byte per row scan)
190 */
93322749
SS
191 config->key_rows = fdtdec_get_int(blob, node, "keypad,num-rows", 0);
192 config->key_cols = fdtdec_get_int(blob, node, "keypad,num-columns", 0);
713cb680
HT
193 if (!config->key_rows || !config->key_cols ||
194 config->key_rows * config->key_cols / 8
195 > CROS_EC_KEYSCAN_COLS) {
196 debug("%s: Invalid key matrix size %d x %d\n", __func__,
197 config->key_rows, config->key_cols);
198 return -1;
199 }
713cb680 200 config->ghost_filter = fdtdec_get_bool(blob, node,
3fbb7871 201 "google,needs-ghost-filter");
713cb680
HT
202 return 0;
203}
204
205/**
206 * Set up the keyboard. This is called by the stdio device handler.
207 *
208 * We want to do this init when the keyboard is actually used rather than
209 * at start-up, since keyboard input may not currently be selected.
210 *
211 * @return 0 if ok, -1 on error
212 */
709ea543 213static int cros_ec_init_keyboard(struct stdio_dev *dev)
713cb680
HT
214{
215 const void *blob = gd->fdt_blob;
216 int node;
217
218 config.dev = board_get_cros_ec_dev();
219 if (!config.dev) {
220 debug("%s: no cros_ec device: cannot init keyboard\n",
221 __func__);
222 return -1;
223 }
224 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
225 if (node < 0) {
226 debug("%s: Node not found\n", __func__);
227 return -1;
228 }
229 if (cros_ec_keyb_decode_fdt(blob, node, &config))
230 return -1;
93322749
SS
231 input_set_delays(&config.input, KBC_REPEAT_DELAY_MS,
232 KBC_REPEAT_RATE_MS);
713cb680
HT
233 if (key_matrix_init(&config.matrix, config.key_rows,
234 config.key_cols, config.ghost_filter)) {
235 debug("%s: cannot init key matrix\n", __func__);
236 return -1;
237 }
238 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
239 debug("%s: Could not decode key matrix from fdt\n", __func__);
240 return -1;
241 }
242 config.inited = 1;
243 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
244 config.key_cols);
245
246 return 0;
247}
248
249int drv_keyboard_init(void)
250{
251 struct stdio_dev dev;
252
253 if (input_init(&config.input, 0)) {
254 debug("%s: Cannot set up input\n", __func__);
255 return -1;
256 }
257 config.input.read_keys = cros_ec_kbc_check;
66877b0f 258 input_add_tables(&config.input);
713cb680
HT
259
260 memset(&dev, '\0', sizeof(dev));
261 strcpy(dev.name, "cros-ec-keyb");
1caf934a 262 dev.flags = DEV_FLAGS_INPUT;
713cb680
HT
263 dev.getc = kbd_getc;
264 dev.tstc = kbd_tstc;
265 dev.start = cros_ec_init_keyboard;
266
267 /* Register the device. cros_ec_init_keyboard() will be called soon */
268 return input_stdio_register(&dev);
269}