1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC keyboard driver
3 //
4 // Copyright (C) 2012 Google, Inc.
5 //
6 // This driver uses the ChromeOS EC byte-level message-based protocol for
7 // communicating the keyboard state (which keys are pressed) from a keyboard EC
8 // to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
9 // but everything else (including deghosting) is done here.  The main
10 // motivation for this is to keep the EC firmware as simple as possible, since
11 // it cannot be easily upgraded and EC flash/IRAM space is relatively
12 // expensive.
13 
14 #include <linux/module.h>
15 #include <linux/bitops.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/input/vivaldi-fmap.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/notifier.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/sysrq.h>
25 #include <linux/input/matrix_keypad.h>
26 #include <linux/platform_data/cros_ec_commands.h>
27 #include <linux/platform_data/cros_ec_proto.h>
28 
29 #include <asm/unaligned.h>
30 
31 /**
32  * struct cros_ec_keyb - Structure representing EC keyboard device
33  *
34  * @rows: Number of rows in the keypad
35  * @cols: Number of columns in the keypad
36  * @row_shift: log2 or number of rows, rounded up
37  * @keymap_data: Matrix keymap data used to convert to keyscan values
38  * @ghost_filter: true to enable the matrix key-ghosting filter
39  * @valid_keys: bitmap of existing keys for each matrix column
40  * @old_kb_state: bitmap of keys pressed last scan
41  * @dev: Device pointer
42  * @ec: Top level ChromeOS device to use to talk to EC
43  * @idev: The input device for the matrix keys.
44  * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
45  * @notifier: interrupt event notifier for transport devices
46  * @vdata: vivaldi function row data
47  */
48 struct cros_ec_keyb {
49 	unsigned int rows;
50 	unsigned int cols;
51 	int row_shift;
52 	const struct matrix_keymap_data *keymap_data;
53 	bool ghost_filter;
54 	uint8_t *valid_keys;
55 	uint8_t *old_kb_state;
56 
57 	struct device *dev;
58 	struct cros_ec_device *ec;
59 
60 	struct input_dev *idev;
61 	struct input_dev *bs_idev;
62 	struct notifier_block notifier;
63 
64 	struct vivaldi_data vdata;
65 };
66 
67 /**
68  * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch
69  *	bitmap #defines
70  *
71  * @ev_type: The type of the input event to generate (e.g., EV_KEY).
72  * @code: A linux keycode
73  * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
74  * @inverted: If the #define and EV_SW have opposite meanings, this is true.
75  *            Only applicable to switches.
76  */
77 struct cros_ec_bs_map {
78 	unsigned int ev_type;
79 	unsigned int code;
80 	u8 bit;
81 	bool inverted;
82 };
83 
84 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
85 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
86 	/* Buttons */
87 	{
88 		.ev_type	= EV_KEY,
89 		.code		= KEY_POWER,
90 		.bit		= EC_MKBP_POWER_BUTTON,
91 	},
92 	{
93 		.ev_type	= EV_KEY,
94 		.code		= KEY_VOLUMEUP,
95 		.bit		= EC_MKBP_VOL_UP,
96 	},
97 	{
98 		.ev_type	= EV_KEY,
99 		.code		= KEY_VOLUMEDOWN,
100 		.bit		= EC_MKBP_VOL_DOWN,
101 	},
102 
103 	/* Switches */
104 	{
105 		.ev_type	= EV_SW,
106 		.code		= SW_LID,
107 		.bit		= EC_MKBP_LID_OPEN,
108 		.inverted	= true,
109 	},
110 	{
111 		.ev_type	= EV_SW,
112 		.code		= SW_TABLET_MODE,
113 		.bit		= EC_MKBP_TABLET_MODE,
114 	},
115 };
116 
117 /*
118  * Returns true when there is at least one combination of pressed keys that
119  * results in ghosting.
120  */
121 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
122 {
123 	int col1, col2, buf1, buf2;
124 	struct device *dev = ckdev->dev;
125 	uint8_t *valid_keys = ckdev->valid_keys;
126 
127 	/*
128 	 * Ghosting happens if for any pressed key X there are other keys
129 	 * pressed both in the same row and column of X as, for instance,
130 	 * in the following diagram:
131 	 *
132 	 * . . Y . g .
133 	 * . . . . . .
134 	 * . . . . . .
135 	 * . . X . Z .
136 	 *
137 	 * In this case only X, Y, and Z are pressed, but g appears to be
138 	 * pressed too (see Wikipedia).
139 	 */
140 	for (col1 = 0; col1 < ckdev->cols; col1++) {
141 		buf1 = buf[col1] & valid_keys[col1];
142 		for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
143 			buf2 = buf[col2] & valid_keys[col2];
144 			if (hweight8(buf1 & buf2) > 1) {
145 				dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
146 					col1, buf1, col2, buf2);
147 				return true;
148 			}
149 		}
150 	}
151 
152 	return false;
153 }
154 
155 
156 /*
157  * Compares the new keyboard state to the old one and produces key
158  * press/release events accordingly.  The keyboard state is 13 bytes (one byte
159  * per column)
160  */
161 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
162 			 uint8_t *kb_state, int len)
163 {
164 	struct input_dev *idev = ckdev->idev;
165 	int col, row;
166 	int new_state;
167 	int old_state;
168 
169 	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
170 		/*
171 		 * Simple-minded solution: ignore this state. The obvious
172 		 * improvement is to only ignore changes to keys involved in
173 		 * the ghosting, but process the other changes.
174 		 */
175 		dev_dbg(ckdev->dev, "ghosting found\n");
176 		return;
177 	}
178 
179 	for (col = 0; col < ckdev->cols; col++) {
180 		for (row = 0; row < ckdev->rows; row++) {
181 			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
182 			const unsigned short *keycodes = idev->keycode;
183 
184 			new_state = kb_state[col] & (1 << row);
185 			old_state = ckdev->old_kb_state[col] & (1 << row);
186 			if (new_state != old_state) {
187 				dev_dbg(ckdev->dev,
188 					"changed: [r%d c%d]: byte %02x\n",
189 					row, col, new_state);
190 
191 				input_event(idev, EV_MSC, MSC_SCAN, pos);
192 				input_report_key(idev, keycodes[pos],
193 						 new_state);
194 			}
195 		}
196 		ckdev->old_kb_state[col] = kb_state[col];
197 	}
198 	input_sync(ckdev->idev);
199 }
200 
201 /**
202  * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
203  *
204  * This takes a bitmap of buttons or switches from the EC and reports events,
205  * syncing at the end.
206  *
207  * @ckdev: The keyboard device.
208  * @ev_type: The input event type (e.g., EV_KEY).
209  * @mask: A bitmap of buttons from the EC.
210  */
211 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
212 				   unsigned int ev_type, u32 mask)
213 
214 {
215 	struct input_dev *idev = ckdev->bs_idev;
216 	int i;
217 
218 	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
219 		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
220 
221 		if (map->ev_type != ev_type)
222 			continue;
223 
224 		input_event(idev, ev_type, map->code,
225 			    !!(mask & BIT(map->bit)) ^ map->inverted);
226 	}
227 	input_sync(idev);
228 }
229 
230 static int cros_ec_keyb_work(struct notifier_block *nb,
231 			     unsigned long queued_during_suspend, void *_notify)
232 {
233 	struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
234 						  notifier);
235 	u32 val;
236 	unsigned int ev_type;
237 
238 	/*
239 	 * If not wake enabled, discard key state changes during
240 	 * suspend. Switches will be re-checked in
241 	 * cros_ec_keyb_resume() to be sure nothing is lost.
242 	 */
243 	if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
244 		return NOTIFY_OK;
245 
246 	switch (ckdev->ec->event_data.event_type) {
247 	case EC_MKBP_EVENT_KEY_MATRIX:
248 		pm_wakeup_event(ckdev->dev, 0);
249 
250 		if (ckdev->ec->event_size != ckdev->cols) {
251 			dev_err(ckdev->dev,
252 				"Discarded incomplete key matrix event.\n");
253 			return NOTIFY_OK;
254 		}
255 
256 		cros_ec_keyb_process(ckdev,
257 				     ckdev->ec->event_data.data.key_matrix,
258 				     ckdev->ec->event_size);
259 		break;
260 
261 	case EC_MKBP_EVENT_SYSRQ:
262 		pm_wakeup_event(ckdev->dev, 0);
263 
264 		val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
265 		dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
266 		handle_sysrq(val);
267 		break;
268 
269 	case EC_MKBP_EVENT_BUTTON:
270 	case EC_MKBP_EVENT_SWITCH:
271 		pm_wakeup_event(ckdev->dev, 0);
272 
273 		if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
274 			val = get_unaligned_le32(
275 					&ckdev->ec->event_data.data.buttons);
276 			ev_type = EV_KEY;
277 		} else {
278 			val = get_unaligned_le32(
279 					&ckdev->ec->event_data.data.switches);
280 			ev_type = EV_SW;
281 		}
282 		cros_ec_keyb_report_bs(ckdev, ev_type, val);
283 		break;
284 
285 	default:
286 		return NOTIFY_DONE;
287 	}
288 
289 	return NOTIFY_OK;
290 }
291 
292 /*
293  * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
294  * ghosting logic to ignore NULL or virtual keys.
295  */
296 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
297 {
298 	int row, col;
299 	int row_shift = ckdev->row_shift;
300 	unsigned short *keymap = ckdev->idev->keycode;
301 	unsigned short code;
302 
303 	BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
304 
305 	for (col = 0; col < ckdev->cols; col++) {
306 		for (row = 0; row < ckdev->rows; row++) {
307 			code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
308 			if (code && (code != KEY_BATTERY))
309 				ckdev->valid_keys[col] |= 1 << row;
310 		}
311 		dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
312 			col, ckdev->valid_keys[col]);
313 	}
314 }
315 
316 /**
317  * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
318  *
319  * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
320  * unmarshalling and different version nonsense into something simple.
321  *
322  * @ec_dev: The EC device
323  * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
324  * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH.  Actually
325  *              in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
326  *              EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
327  * @result: Where we'll store the result; a union
328  * @result_size: The size of the result.  Expected to be the size of one of
329  *               the elements in the union.
330  *
331  * Returns 0 if no error or -error upon error.
332  */
333 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
334 			     enum ec_mkbp_info_type info_type,
335 			     enum ec_mkbp_event event_type,
336 			     union ec_response_get_next_data *result,
337 			     size_t result_size)
338 {
339 	struct ec_params_mkbp_info *params;
340 	struct cros_ec_command *msg;
341 	int ret;
342 
343 	msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
344 					   sizeof(*params)), GFP_KERNEL);
345 	if (!msg)
346 		return -ENOMEM;
347 
348 	msg->command = EC_CMD_MKBP_INFO;
349 	msg->version = 1;
350 	msg->outsize = sizeof(*params);
351 	msg->insize = result_size;
352 	params = (struct ec_params_mkbp_info *)msg->data;
353 	params->info_type = info_type;
354 	params->event_type = event_type;
355 
356 	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
357 	if (ret == -ENOPROTOOPT) {
358 		/* With older ECs we just return 0 for everything */
359 		memset(result, 0, result_size);
360 		ret = 0;
361 	} else if (ret < 0) {
362 		dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
363 			 (int)info_type, (int)event_type, ret);
364 	} else if (ret != result_size) {
365 		dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
366 			 (int)info_type, (int)event_type,
367 			 ret, result_size);
368 		ret = -EPROTO;
369 	} else {
370 		memcpy(result, msg->data, result_size);
371 		ret = 0;
372 	}
373 
374 	kfree(msg);
375 
376 	return ret;
377 }
378 
379 /**
380  * cros_ec_keyb_query_switches - Query the state of switches and report
381  *
382  * This will ask the EC about the current state of switches and report to the
383  * kernel.  Note that we don't query for buttons because they are more
384  * transitory and we'll get an update on the next release / press.
385  *
386  * @ckdev: The keyboard device
387  *
388  * Returns 0 if no error or -error upon error.
389  */
390 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
391 {
392 	struct cros_ec_device *ec_dev = ckdev->ec;
393 	union ec_response_get_next_data event_data = {};
394 	int ret;
395 
396 	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
397 				EC_MKBP_EVENT_SWITCH, &event_data,
398 				sizeof(event_data.switches));
399 	if (ret)
400 		return ret;
401 
402 	cros_ec_keyb_report_bs(ckdev, EV_SW,
403 			       get_unaligned_le32(&event_data.switches));
404 
405 	return 0;
406 }
407 
408 /**
409  * cros_ec_keyb_resume - Resume the keyboard
410  *
411  * We use the resume notification as a chance to query the EC for switches.
412  *
413  * @dev: The keyboard device
414  *
415  * Returns 0 if no error or -error upon error.
416  */
417 static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
418 {
419 	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
420 
421 	if (ckdev->bs_idev)
422 		return cros_ec_keyb_query_switches(ckdev);
423 
424 	return 0;
425 }
426 
427 /**
428  * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
429  *
430  * Handles all the bits of the keyboard driver related to non-matrix buttons
431  * and switches, including asking the EC about which are present and telling
432  * the kernel to expect them.
433  *
434  * If this device has no support for buttons and switches we'll return no error
435  * but the ckdev->bs_idev will remain NULL when this function exits.
436  *
437  * @ckdev: The keyboard device
438  *
439  * Returns 0 if no error or -error upon error.
440  */
441 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
442 {
443 	struct cros_ec_device *ec_dev = ckdev->ec;
444 	struct device *dev = ckdev->dev;
445 	struct input_dev *idev;
446 	union ec_response_get_next_data event_data = {};
447 	const char *phys;
448 	u32 buttons;
449 	u32 switches;
450 	int ret;
451 	int i;
452 
453 	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
454 				EC_MKBP_EVENT_BUTTON, &event_data,
455 				sizeof(event_data.buttons));
456 	if (ret)
457 		return ret;
458 	buttons = get_unaligned_le32(&event_data.buttons);
459 
460 	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
461 				EC_MKBP_EVENT_SWITCH, &event_data,
462 				sizeof(event_data.switches));
463 	if (ret)
464 		return ret;
465 	switches = get_unaligned_le32(&event_data.switches);
466 
467 	if (!buttons && !switches)
468 		return 0;
469 
470 	/*
471 	 * We call the non-matrix buttons/switches 'input1', if present.
472 	 * Allocate phys before input dev, to ensure correct tear-down
473 	 * ordering.
474 	 */
475 	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
476 	if (!phys)
477 		return -ENOMEM;
478 
479 	idev = devm_input_allocate_device(dev);
480 	if (!idev)
481 		return -ENOMEM;
482 
483 	idev->name = "cros_ec_buttons";
484 	idev->phys = phys;
485 	__set_bit(EV_REP, idev->evbit);
486 
487 	idev->id.bustype = BUS_VIRTUAL;
488 	idev->id.version = 1;
489 	idev->id.product = 0;
490 	idev->dev.parent = dev;
491 
492 	input_set_drvdata(idev, ckdev);
493 	ckdev->bs_idev = idev;
494 
495 	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
496 		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
497 
498 		if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
499 		    (map->ev_type == EV_SW && (switches & BIT(map->bit))))
500 			input_set_capability(idev, map->ev_type, map->code);
501 	}
502 
503 	ret = cros_ec_keyb_query_switches(ckdev);
504 	if (ret) {
505 		dev_err(dev, "cannot query switches\n");
506 		return ret;
507 	}
508 
509 	ret = input_register_device(ckdev->bs_idev);
510 	if (ret) {
511 		dev_err(dev, "cannot register input device\n");
512 		return ret;
513 	}
514 
515 	return 0;
516 }
517 
518 /**
519  * cros_ec_keyb_register_bs - Register matrix keys
520  *
521  * Handles all the bits of the keyboard driver related to matrix keys.
522  *
523  * @ckdev: The keyboard device
524  *
525  * Returns 0 if no error or -error upon error.
526  */
527 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
528 {
529 	struct cros_ec_device *ec_dev = ckdev->ec;
530 	struct device *dev = ckdev->dev;
531 	struct input_dev *idev;
532 	const char *phys;
533 	int err;
534 	struct property *prop;
535 	const __be32 *p;
536 	u32 *physmap;
537 	u32 key_pos;
538 	unsigned int row, col, scancode, n_physmap;
539 
540 	err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
541 	if (err)
542 		return err;
543 
544 	ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
545 	if (!ckdev->valid_keys)
546 		return -ENOMEM;
547 
548 	ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
549 	if (!ckdev->old_kb_state)
550 		return -ENOMEM;
551 
552 	/*
553 	 * We call the keyboard matrix 'input0'. Allocate phys before input
554 	 * dev, to ensure correct tear-down ordering.
555 	 */
556 	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
557 	if (!phys)
558 		return -ENOMEM;
559 
560 	idev = devm_input_allocate_device(dev);
561 	if (!idev)
562 		return -ENOMEM;
563 
564 	idev->name = CROS_EC_DEV_NAME;
565 	idev->phys = phys;
566 	__set_bit(EV_REP, idev->evbit);
567 
568 	idev->id.bustype = BUS_VIRTUAL;
569 	idev->id.version = 1;
570 	idev->id.product = 0;
571 	idev->dev.parent = dev;
572 
573 	ckdev->ghost_filter = of_property_read_bool(dev->of_node,
574 					"google,needs-ghost-filter");
575 
576 	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
577 					 NULL, idev);
578 	if (err) {
579 		dev_err(dev, "cannot build key matrix\n");
580 		return err;
581 	}
582 
583 	ckdev->row_shift = get_count_order(ckdev->cols);
584 
585 	input_set_capability(idev, EV_MSC, MSC_SCAN);
586 	input_set_drvdata(idev, ckdev);
587 	ckdev->idev = idev;
588 	cros_ec_keyb_compute_valid_keys(ckdev);
589 
590 	physmap = ckdev->vdata.function_row_physmap;
591 	n_physmap = 0;
592 	of_property_for_each_u32(dev->of_node, "function-row-physmap",
593 				 prop, p, key_pos) {
594 		if (n_physmap == VIVALDI_MAX_FUNCTION_ROW_KEYS) {
595 			dev_warn(dev, "Only support up to %d top row keys\n",
596 				 VIVALDI_MAX_FUNCTION_ROW_KEYS);
597 			break;
598 		}
599 		row = KEY_ROW(key_pos);
600 		col = KEY_COL(key_pos);
601 		scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
602 		physmap[n_physmap++] = scancode;
603 	}
604 	ckdev->vdata.num_function_row_keys = n_physmap;
605 
606 	err = input_register_device(ckdev->idev);
607 	if (err) {
608 		dev_err(dev, "cannot register input device\n");
609 		return err;
610 	}
611 
612 	return 0;
613 }
614 
615 static ssize_t function_row_physmap_show(struct device *dev,
616 					 struct device_attribute *attr,
617 					 char *buf)
618 {
619 	const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
620 	const struct vivaldi_data *data = &ckdev->vdata;
621 
622 	return vivaldi_function_row_physmap_show(data, buf);
623 }
624 
625 static DEVICE_ATTR_RO(function_row_physmap);
626 
627 static struct attribute *cros_ec_keyb_attrs[] = {
628 	&dev_attr_function_row_physmap.attr,
629 	NULL,
630 };
631 
632 static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
633 					    struct attribute *attr,
634 					    int n)
635 {
636 	struct device *dev = kobj_to_dev(kobj);
637 	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
638 
639 	if (attr == &dev_attr_function_row_physmap.attr &&
640 	    !ckdev->vdata.num_function_row_keys)
641 		return 0;
642 
643 	return attr->mode;
644 }
645 
646 static const struct attribute_group cros_ec_keyb_attr_group = {
647 	.is_visible = cros_ec_keyb_attr_is_visible,
648 	.attrs = cros_ec_keyb_attrs,
649 };
650 
651 
652 static int cros_ec_keyb_probe(struct platform_device *pdev)
653 {
654 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
655 	struct device *dev = &pdev->dev;
656 	struct cros_ec_keyb *ckdev;
657 	int err;
658 
659 	if (!dev->of_node)
660 		return -ENODEV;
661 
662 	ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
663 	if (!ckdev)
664 		return -ENOMEM;
665 
666 	ckdev->ec = ec;
667 	ckdev->dev = dev;
668 	dev_set_drvdata(dev, ckdev);
669 
670 	err = cros_ec_keyb_register_matrix(ckdev);
671 	if (err) {
672 		dev_err(dev, "cannot register matrix inputs: %d\n", err);
673 		return err;
674 	}
675 
676 	err = cros_ec_keyb_register_bs(ckdev);
677 	if (err) {
678 		dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
679 		return err;
680 	}
681 
682 	err = devm_device_add_group(dev, &cros_ec_keyb_attr_group);
683 	if (err) {
684 		dev_err(dev, "failed to create attributes. err=%d\n", err);
685 		return err;
686 	}
687 
688 	ckdev->notifier.notifier_call = cros_ec_keyb_work;
689 	err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
690 					       &ckdev->notifier);
691 	if (err) {
692 		dev_err(dev, "cannot register notifier: %d\n", err);
693 		return err;
694 	}
695 
696 	device_init_wakeup(ckdev->dev, true);
697 	return 0;
698 }
699 
700 static int cros_ec_keyb_remove(struct platform_device *pdev)
701 {
702 	struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
703 
704 	blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
705 					   &ckdev->notifier);
706 
707 	return 0;
708 }
709 
710 #ifdef CONFIG_OF
711 static const struct of_device_id cros_ec_keyb_of_match[] = {
712 	{ .compatible = "google,cros-ec-keyb" },
713 	{},
714 };
715 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
716 #endif
717 
718 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
719 
720 static struct platform_driver cros_ec_keyb_driver = {
721 	.probe = cros_ec_keyb_probe,
722 	.remove = cros_ec_keyb_remove,
723 	.driver = {
724 		.name = "cros-ec-keyb",
725 		.of_match_table = of_match_ptr(cros_ec_keyb_of_match),
726 		.pm = &cros_ec_keyb_pm_ops,
727 	},
728 };
729 
730 module_platform_driver(cros_ec_keyb_driver);
731 
732 MODULE_LICENSE("GPL v2");
733 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
734 MODULE_ALIAS("platform:cros-ec-keyb");
735