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