1 /*
2  * ChromeOS EC keyboard driver
3  *
4  * Copyright (C) 2012 Google, Inc
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * This driver uses the Chrome OS EC byte-level message-based protocol for
16  * communicating the keyboard state (which keys are pressed) from a keyboard EC
17  * to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
18  * but everything else (including deghosting) is done here.  The main
19  * motivation for this is to keep the EC firmware as simple as possible, since
20  * it cannot be easily upgraded and EC flash/IRAM space is relatively
21  * expensive.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/input.h>
27 #include <linux/kernel.h>
28 #include <linux/notifier.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
31 #include <linux/input/matrix_keypad.h>
32 #include <linux/mfd/cros_ec.h>
33 #include <linux/mfd/cros_ec_commands.h>
34 
35 /*
36  * @rows: Number of rows in the keypad
37  * @cols: Number of columns in the keypad
38  * @row_shift: log2 or number of rows, rounded up
39  * @keymap_data: Matrix keymap data used to convert to keyscan values
40  * @ghost_filter: true to enable the matrix key-ghosting filter
41  * @old_kb_state: bitmap of keys pressed last scan
42  * @dev: Device pointer
43  * @idev: Input device
44  * @ec: Top level ChromeOS device to use to talk to EC
45  * @event_notifier: interrupt event notifier for transport devices
46  */
47 struct cros_ec_keyb {
48 	unsigned int rows;
49 	unsigned int cols;
50 	int row_shift;
51 	const struct matrix_keymap_data *keymap_data;
52 	bool ghost_filter;
53 	uint8_t *old_kb_state;
54 
55 	struct device *dev;
56 	struct input_dev *idev;
57 	struct cros_ec_device *ec;
58 	struct notifier_block notifier;
59 };
60 
61 
62 static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb *ckdev,
63 					  uint8_t *buf, int row)
64 {
65 	int pressed_in_row = 0;
66 	int row_has_teeth = 0;
67 	int col, mask;
68 
69 	mask = 1 << row;
70 	for (col = 0; col < ckdev->cols; col++) {
71 		if (buf[col] & mask) {
72 			pressed_in_row++;
73 			row_has_teeth |= buf[col] & ~mask;
74 			if (pressed_in_row > 1 && row_has_teeth) {
75 				/* ghosting */
76 				dev_dbg(ckdev->dev,
77 					"ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
78 					row, col, pressed_in_row,
79 					row_has_teeth);
80 				return true;
81 			}
82 		}
83 	}
84 
85 	return false;
86 }
87 
88 /*
89  * Returns true when there is at least one combination of pressed keys that
90  * results in ghosting.
91  */
92 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
93 {
94 	int row;
95 
96 	/*
97 	 * Ghosting happens if for any pressed key X there are other keys
98 	 * pressed both in the same row and column of X as, for instance,
99 	 * in the following diagram:
100 	 *
101 	 * . . Y . g .
102 	 * . . . . . .
103 	 * . . . . . .
104 	 * . . X . Z .
105 	 *
106 	 * In this case only X, Y, and Z are pressed, but g appears to be
107 	 * pressed too (see Wikipedia).
108 	 *
109 	 * We can detect ghosting in a single pass (*) over the keyboard state
110 	 * by maintaining two arrays.  pressed_in_row counts how many pressed
111 	 * keys we have found in a row.  row_has_teeth is true if any of the
112 	 * pressed keys for this row has other pressed keys in its column.  If
113 	 * at any point of the scan we find that a row has multiple pressed
114 	 * keys, and at least one of them is at the intersection with a column
115 	 * with multiple pressed keys, we're sure there is ghosting.
116 	 * Conversely, if there is ghosting, we will detect such situation for
117 	 * at least one key during the pass.
118 	 *
119 	 * (*) This looks linear in the number of keys, but it's not.  We can
120 	 * cheat because the number of rows is small.
121 	 */
122 	for (row = 0; row < ckdev->rows; row++)
123 		if (cros_ec_keyb_row_has_ghosting(ckdev, buf, row))
124 			return true;
125 
126 	return false;
127 }
128 
129 /*
130  * Compares the new keyboard state to the old one and produces key
131  * press/release events accordingly.  The keyboard state is 13 bytes (one byte
132  * per column)
133  */
134 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
135 			 uint8_t *kb_state, int len)
136 {
137 	struct input_dev *idev = ckdev->idev;
138 	int col, row;
139 	int new_state;
140 	int old_state;
141 	int num_cols;
142 
143 	num_cols = len;
144 
145 	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
146 		/*
147 		 * Simple-minded solution: ignore this state. The obvious
148 		 * improvement is to only ignore changes to keys involved in
149 		 * the ghosting, but process the other changes.
150 		 */
151 		dev_dbg(ckdev->dev, "ghosting found\n");
152 		return;
153 	}
154 
155 	for (col = 0; col < ckdev->cols; col++) {
156 		for (row = 0; row < ckdev->rows; row++) {
157 			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
158 			const unsigned short *keycodes = idev->keycode;
159 
160 			new_state = kb_state[col] & (1 << row);
161 			old_state = ckdev->old_kb_state[col] & (1 << row);
162 			if (new_state != old_state) {
163 				dev_dbg(ckdev->dev,
164 					"changed: [r%d c%d]: byte %02x\n",
165 					row, col, new_state);
166 
167 				input_report_key(idev, keycodes[pos],
168 						 new_state);
169 			}
170 		}
171 		ckdev->old_kb_state[col] = kb_state[col];
172 	}
173 	input_sync(ckdev->idev);
174 }
175 
176 static int cros_ec_keyb_open(struct input_dev *dev)
177 {
178 	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
179 
180 	return blocking_notifier_chain_register(&ckdev->ec->event_notifier,
181 						&ckdev->notifier);
182 }
183 
184 static void cros_ec_keyb_close(struct input_dev *dev)
185 {
186 	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
187 
188 	blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
189 					   &ckdev->notifier);
190 }
191 
192 static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
193 {
194 	return ckdev->ec->command_recv(ckdev->ec, EC_CMD_MKBP_STATE,
195 					  kb_state, ckdev->cols);
196 }
197 
198 static int cros_ec_keyb_work(struct notifier_block *nb,
199 		     unsigned long state, void *_notify)
200 {
201 	int ret;
202 	struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
203 						    notifier);
204 	uint8_t kb_state[ckdev->cols];
205 
206 	ret = cros_ec_keyb_get_state(ckdev, kb_state);
207 	if (ret >= 0)
208 		cros_ec_keyb_process(ckdev, kb_state, ret);
209 
210 	return NOTIFY_DONE;
211 }
212 
213 static int cros_ec_keyb_probe(struct platform_device *pdev)
214 {
215 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
216 	struct device *dev = ec->dev;
217 	struct cros_ec_keyb *ckdev;
218 	struct input_dev *idev;
219 	struct device_node *np;
220 	int err;
221 
222 	np = pdev->dev.of_node;
223 	if (!np)
224 		return -ENODEV;
225 
226 	ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
227 	if (!ckdev)
228 		return -ENOMEM;
229 	err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
230 					    &ckdev->cols);
231 	if (err)
232 		return err;
233 	ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
234 	if (!ckdev->old_kb_state)
235 		return -ENOMEM;
236 
237 	idev = devm_input_allocate_device(&pdev->dev);
238 	if (!idev)
239 		return -ENOMEM;
240 
241 	ckdev->ec = ec;
242 	ckdev->notifier.notifier_call = cros_ec_keyb_work;
243 	ckdev->dev = dev;
244 	dev_set_drvdata(&pdev->dev, ckdev);
245 
246 	idev->name = ec->ec_name;
247 	idev->phys = ec->phys_name;
248 	__set_bit(EV_REP, idev->evbit);
249 
250 	idev->id.bustype = BUS_VIRTUAL;
251 	idev->id.version = 1;
252 	idev->id.product = 0;
253 	idev->dev.parent = &pdev->dev;
254 	idev->open = cros_ec_keyb_open;
255 	idev->close = cros_ec_keyb_close;
256 
257 	ckdev->ghost_filter = of_property_read_bool(np,
258 					"google,needs-ghost-filter");
259 
260 	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
261 					 NULL, idev);
262 	if (err) {
263 		dev_err(dev, "cannot build key matrix\n");
264 		return err;
265 	}
266 
267 	ckdev->row_shift = get_count_order(ckdev->cols);
268 
269 	input_set_capability(idev, EV_MSC, MSC_SCAN);
270 	input_set_drvdata(idev, ckdev);
271 	ckdev->idev = idev;
272 	err = input_register_device(ckdev->idev);
273 	if (err) {
274 		dev_err(dev, "cannot register input device\n");
275 		return err;
276 	}
277 
278 	return 0;
279 }
280 
281 #ifdef CONFIG_PM_SLEEP
282 /* Clear any keys in the buffer */
283 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
284 {
285 	uint8_t old_state[ckdev->cols];
286 	uint8_t new_state[ckdev->cols];
287 	unsigned long duration;
288 	int i, ret;
289 
290 	/*
291 	 * Keep reading until we see that the scan state does not change.
292 	 * That indicates that we are done.
293 	 *
294 	 * Assume that the EC keyscan buffer is at most 32 deep.
295 	 */
296 	duration = jiffies;
297 	ret = cros_ec_keyb_get_state(ckdev, new_state);
298 	for (i = 1; !ret && i < 32; i++) {
299 		memcpy(old_state, new_state, sizeof(old_state));
300 		ret = cros_ec_keyb_get_state(ckdev, new_state);
301 		if (0 == memcmp(old_state, new_state, sizeof(old_state)))
302 			break;
303 	}
304 	duration = jiffies - duration;
305 	dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
306 		jiffies_to_usecs(duration));
307 }
308 
309 static int cros_ec_keyb_resume(struct device *dev)
310 {
311 	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
312 
313 	/*
314 	 * When the EC is not a wake source, then it could not have caused the
315 	 * resume, so we clear the EC's key scan buffer. If the EC was a
316 	 * wake source (e.g. the lid is open and the user might press a key to
317 	 * wake) then the key scan buffer should be preserved.
318 	 */
319 	if (ckdev->ec->was_wake_device)
320 		cros_ec_keyb_clear_keyboard(ckdev);
321 
322 	return 0;
323 }
324 
325 #endif
326 
327 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
328 
329 static struct platform_driver cros_ec_keyb_driver = {
330 	.probe = cros_ec_keyb_probe,
331 	.driver = {
332 		.name = "cros-ec-keyb",
333 		.pm	= &cros_ec_keyb_pm_ops,
334 	},
335 };
336 
337 module_platform_driver(cros_ec_keyb_driver);
338 
339 MODULE_LICENSE("GPL");
340 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
341 MODULE_ALIAS("platform:cros-ec-keyb");
342