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