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/interrupt.h>
28 #include <linux/kernel.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  */
46 struct cros_ec_keyb {
47 	unsigned int rows;
48 	unsigned int cols;
49 	int row_shift;
50 	const struct matrix_keymap_data *keymap_data;
51 	bool ghost_filter;
52 	uint8_t *old_kb_state;
53 
54 	struct device *dev;
55 	struct input_dev *idev;
56 	struct cros_ec_device *ec;
57 };
58 
59 
60 static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb *ckdev,
61 					  uint8_t *buf, int row)
62 {
63 	int pressed_in_row = 0;
64 	int row_has_teeth = 0;
65 	int col, mask;
66 
67 	mask = 1 << row;
68 	for (col = 0; col < ckdev->cols; col++) {
69 		if (buf[col] & mask) {
70 			pressed_in_row++;
71 			row_has_teeth |= buf[col] & ~mask;
72 			if (pressed_in_row > 1 && row_has_teeth) {
73 				/* ghosting */
74 				dev_dbg(ckdev->dev,
75 					"ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
76 					row, col, pressed_in_row,
77 					row_has_teeth);
78 				return true;
79 			}
80 		}
81 	}
82 
83 	return false;
84 }
85 
86 /*
87  * Returns true when there is at least one combination of pressed keys that
88  * results in ghosting.
89  */
90 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
91 {
92 	int row;
93 
94 	/*
95 	 * Ghosting happens if for any pressed key X there are other keys
96 	 * pressed both in the same row and column of X as, for instance,
97 	 * in the following diagram:
98 	 *
99 	 * . . Y . g .
100 	 * . . . . . .
101 	 * . . . . . .
102 	 * . . X . Z .
103 	 *
104 	 * In this case only X, Y, and Z are pressed, but g appears to be
105 	 * pressed too (see Wikipedia).
106 	 *
107 	 * We can detect ghosting in a single pass (*) over the keyboard state
108 	 * by maintaining two arrays.  pressed_in_row counts how many pressed
109 	 * keys we have found in a row.  row_has_teeth is true if any of the
110 	 * pressed keys for this row has other pressed keys in its column.  If
111 	 * at any point of the scan we find that a row has multiple pressed
112 	 * keys, and at least one of them is at the intersection with a column
113 	 * with multiple pressed keys, we're sure there is ghosting.
114 	 * Conversely, if there is ghosting, we will detect such situation for
115 	 * at least one key during the pass.
116 	 *
117 	 * (*) This looks linear in the number of keys, but it's not.  We can
118 	 * cheat because the number of rows is small.
119 	 */
120 	for (row = 0; row < ckdev->rows; row++)
121 		if (cros_ec_keyb_row_has_ghosting(ckdev, buf, row))
122 			return true;
123 
124 	return false;
125 }
126 
127 /*
128  * Compares the new keyboard state to the old one and produces key
129  * press/release events accordingly.  The keyboard state is 13 bytes (one byte
130  * per column)
131  */
132 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
133 			 uint8_t *kb_state, int len)
134 {
135 	struct input_dev *idev = ckdev->idev;
136 	int col, row;
137 	int new_state;
138 	int old_state;
139 	int num_cols;
140 
141 	num_cols = len;
142 
143 	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
144 		/*
145 		 * Simple-minded solution: ignore this state. The obvious
146 		 * improvement is to only ignore changes to keys involved in
147 		 * the ghosting, but process the other changes.
148 		 */
149 		dev_dbg(ckdev->dev, "ghosting found\n");
150 		return;
151 	}
152 
153 	for (col = 0; col < ckdev->cols; col++) {
154 		for (row = 0; row < ckdev->rows; row++) {
155 			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
156 			const unsigned short *keycodes = idev->keycode;
157 
158 			new_state = kb_state[col] & (1 << row);
159 			old_state = ckdev->old_kb_state[col] & (1 << row);
160 			if (new_state != old_state) {
161 				dev_dbg(ckdev->dev,
162 					"changed: [r%d c%d]: byte %02x\n",
163 					row, col, new_state);
164 
165 				input_report_key(idev, keycodes[pos],
166 						 new_state);
167 			}
168 		}
169 		ckdev->old_kb_state[col] = kb_state[col];
170 	}
171 	input_sync(ckdev->idev);
172 }
173 
174 static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
175 {
176 	struct cros_ec_command msg = {
177 		.version = 0,
178 		.command = EC_CMD_MKBP_STATE,
179 		.outdata = NULL,
180 		.outsize = 0,
181 		.indata = kb_state,
182 		.insize = ckdev->cols,
183 	};
184 
185 	return ckdev->ec->cmd_xfer(ckdev->ec, &msg);
186 }
187 
188 static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
189 {
190 	struct cros_ec_keyb *ckdev = data;
191 	struct cros_ec_device *ec = ckdev->ec;
192 	int ret;
193 	uint8_t kb_state[ckdev->cols];
194 
195 	if (device_may_wakeup(ec->dev))
196 		pm_wakeup_event(ec->dev, 0);
197 
198 	ret = cros_ec_keyb_get_state(ckdev, kb_state);
199 	if (ret >= 0)
200 		cros_ec_keyb_process(ckdev, kb_state, ret);
201 	else
202 		dev_err(ec->dev, "failed to get keyboard state: %d\n", ret);
203 
204 	return IRQ_HANDLED;
205 }
206 
207 static int cros_ec_keyb_open(struct input_dev *dev)
208 {
209 	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
210 	struct cros_ec_device *ec = ckdev->ec;
211 
212 	return request_threaded_irq(ec->irq, NULL, cros_ec_keyb_irq,
213 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
214 					"cros_ec_keyb", ckdev);
215 }
216 
217 static void cros_ec_keyb_close(struct input_dev *dev)
218 {
219 	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
220 	struct cros_ec_device *ec = ckdev->ec;
221 
222 	free_irq(ec->irq, ckdev);
223 }
224 
225 static int cros_ec_keyb_probe(struct platform_device *pdev)
226 {
227 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
228 	struct device *dev = ec->dev;
229 	struct cros_ec_keyb *ckdev;
230 	struct input_dev *idev;
231 	struct device_node *np;
232 	int err;
233 
234 	np = pdev->dev.of_node;
235 	if (!np)
236 		return -ENODEV;
237 
238 	ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
239 	if (!ckdev)
240 		return -ENOMEM;
241 	err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
242 					    &ckdev->cols);
243 	if (err)
244 		return err;
245 	ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
246 	if (!ckdev->old_kb_state)
247 		return -ENOMEM;
248 
249 	idev = devm_input_allocate_device(&pdev->dev);
250 	if (!idev)
251 		return -ENOMEM;
252 
253 	if (!ec->irq) {
254 		dev_err(dev, "no EC IRQ specified\n");
255 		return -EINVAL;
256 	}
257 
258 	ckdev->ec = ec;
259 	ckdev->dev = dev;
260 	dev_set_drvdata(&pdev->dev, ckdev);
261 
262 	idev->name = ec->ec_name;
263 	idev->phys = ec->phys_name;
264 	__set_bit(EV_REP, idev->evbit);
265 
266 	idev->id.bustype = BUS_VIRTUAL;
267 	idev->id.version = 1;
268 	idev->id.product = 0;
269 	idev->dev.parent = &pdev->dev;
270 	idev->open = cros_ec_keyb_open;
271 	idev->close = cros_ec_keyb_close;
272 
273 	ckdev->ghost_filter = of_property_read_bool(np,
274 					"google,needs-ghost-filter");
275 
276 	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
277 					 NULL, idev);
278 	if (err) {
279 		dev_err(dev, "cannot build key matrix\n");
280 		return err;
281 	}
282 
283 	ckdev->row_shift = get_count_order(ckdev->cols);
284 
285 	input_set_capability(idev, EV_MSC, MSC_SCAN);
286 	input_set_drvdata(idev, ckdev);
287 	ckdev->idev = idev;
288 	err = input_register_device(ckdev->idev);
289 	if (err) {
290 		dev_err(dev, "cannot register input device\n");
291 		return err;
292 	}
293 
294 	return 0;
295 }
296 
297 #ifdef CONFIG_PM_SLEEP
298 /* Clear any keys in the buffer */
299 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
300 {
301 	uint8_t old_state[ckdev->cols];
302 	uint8_t new_state[ckdev->cols];
303 	unsigned long duration;
304 	int i, ret;
305 
306 	/*
307 	 * Keep reading until we see that the scan state does not change.
308 	 * That indicates that we are done.
309 	 *
310 	 * Assume that the EC keyscan buffer is at most 32 deep.
311 	 */
312 	duration = jiffies;
313 	ret = cros_ec_keyb_get_state(ckdev, new_state);
314 	for (i = 1; !ret && i < 32; i++) {
315 		memcpy(old_state, new_state, sizeof(old_state));
316 		ret = cros_ec_keyb_get_state(ckdev, new_state);
317 		if (0 == memcmp(old_state, new_state, sizeof(old_state)))
318 			break;
319 	}
320 	duration = jiffies - duration;
321 	dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
322 		jiffies_to_usecs(duration));
323 }
324 
325 static int cros_ec_keyb_resume(struct device *dev)
326 {
327 	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
328 
329 	/*
330 	 * When the EC is not a wake source, then it could not have caused the
331 	 * resume, so we clear the EC's key scan buffer. If the EC was a
332 	 * wake source (e.g. the lid is open and the user might press a key to
333 	 * wake) then the key scan buffer should be preserved.
334 	 */
335 	if (ckdev->ec->was_wake_device)
336 		cros_ec_keyb_clear_keyboard(ckdev);
337 
338 	return 0;
339 }
340 
341 #endif
342 
343 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
344 
345 static struct platform_driver cros_ec_keyb_driver = {
346 	.probe = cros_ec_keyb_probe,
347 	.driver = {
348 		.name = "cros-ec-keyb",
349 		.pm	= &cros_ec_keyb_pm_ops,
350 	},
351 };
352 
353 module_platform_driver(cros_ec_keyb_driver);
354 
355 MODULE_LICENSE("GPL");
356 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
357 MODULE_ALIAS("platform:cros-ec-keyb");
358