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