This source file includes following definitions.
- cros_ec_keyb_has_ghosting
- cros_ec_keyb_process
- cros_ec_keyb_report_bs
- cros_ec_keyb_work
- cros_ec_keyb_compute_valid_keys
- cros_ec_keyb_info
- cros_ec_keyb_query_switches
- cros_ec_keyb_resume
- cros_ec_keyb_register_bs
- cros_ec_keyb_register_matrix
- cros_ec_keyb_probe
- cros_ec_keyb_remove
1
2
3
4
5
6
7
8
9
10
11
12
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
32
33
34
35
36
37
38
39
40
41
42
43
44 struct 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
64
65
66
67
68
69
70
71
72 struct cros_ec_bs_map {
73 unsigned int ev_type;
74 unsigned int code;
75 u8 bit;
76 bool inverted;
77 };
78
79
80 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
81
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
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
114
115
116 static 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
124
125
126
127
128
129
130
131
132
133
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
153
154
155
156 static 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
167
168
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
197
198
199
200
201
202
203
204
205 static 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
224 static 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 uint8_t mkbp_event_type = ckdev->ec->event_data.event_type &
230 EC_MKBP_EVENT_TYPE_MASK;
231 u32 val;
232 unsigned int ev_type;
233
234
235
236
237
238
239 if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
240 return NOTIFY_OK;
241
242 switch (mkbp_event_type) {
243 case EC_MKBP_EVENT_KEY_MATRIX:
244 pm_wakeup_event(ckdev->dev, 0);
245
246 if (ckdev->ec->event_size != ckdev->cols) {
247 dev_err(ckdev->dev,
248 "Discarded incomplete key matrix event.\n");
249 return NOTIFY_OK;
250 }
251
252 cros_ec_keyb_process(ckdev,
253 ckdev->ec->event_data.data.key_matrix,
254 ckdev->ec->event_size);
255 break;
256
257 case EC_MKBP_EVENT_SYSRQ:
258 pm_wakeup_event(ckdev->dev, 0);
259
260 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
261 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
262 handle_sysrq(val);
263 break;
264
265 case EC_MKBP_EVENT_BUTTON:
266 case EC_MKBP_EVENT_SWITCH:
267 pm_wakeup_event(ckdev->dev, 0);
268
269 if (mkbp_event_type == EC_MKBP_EVENT_BUTTON) {
270 val = get_unaligned_le32(
271 &ckdev->ec->event_data.data.buttons);
272 ev_type = EV_KEY;
273 } else {
274 val = get_unaligned_le32(
275 &ckdev->ec->event_data.data.switches);
276 ev_type = EV_SW;
277 }
278 cros_ec_keyb_report_bs(ckdev, ev_type, val);
279 break;
280
281 default:
282 return NOTIFY_DONE;
283 }
284
285 return NOTIFY_OK;
286 }
287
288
289
290
291
292 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
293 {
294 int row, col;
295 int row_shift = ckdev->row_shift;
296 unsigned short *keymap = ckdev->idev->keycode;
297 unsigned short code;
298
299 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
300
301 for (col = 0; col < ckdev->cols; col++) {
302 for (row = 0; row < ckdev->rows; row++) {
303 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
304 if (code && (code != KEY_BATTERY))
305 ckdev->valid_keys[col] |= 1 << row;
306 }
307 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
308 col, ckdev->valid_keys[col]);
309 }
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
330 enum ec_mkbp_info_type info_type,
331 enum ec_mkbp_event event_type,
332 union ec_response_get_next_data *result,
333 size_t result_size)
334 {
335 struct ec_params_mkbp_info *params;
336 struct cros_ec_command *msg;
337 int ret;
338
339 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
340 sizeof(*params)), GFP_KERNEL);
341 if (!msg)
342 return -ENOMEM;
343
344 msg->command = EC_CMD_MKBP_INFO;
345 msg->version = 1;
346 msg->outsize = sizeof(*params);
347 msg->insize = result_size;
348 params = (struct ec_params_mkbp_info *)msg->data;
349 params->info_type = info_type;
350 params->event_type = event_type;
351
352 ret = cros_ec_cmd_xfer(ec_dev, msg);
353 if (ret < 0) {
354 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
355 (int)info_type, (int)event_type, ret);
356 } else if (msg->result == EC_RES_INVALID_VERSION) {
357
358 memset(result, 0, result_size);
359 ret = 0;
360 } else if (msg->result != EC_RES_SUCCESS) {
361 dev_warn(ec_dev->dev, "Error getting info %d/%d: %d\n",
362 (int)info_type, (int)event_type, msg->result);
363 ret = -EPROTO;
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
381
382
383
384
385
386
387
388
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
410
411
412
413
414
415
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
429
430
431
432
433
434
435
436
437
438
439
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
472
473
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
520
521
522
523
524
525
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
535 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
536 if (err)
537 return err;
538
539 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
540 if (!ckdev->valid_keys)
541 return -ENOMEM;
542
543 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
544 if (!ckdev->old_kb_state)
545 return -ENOMEM;
546
547
548
549
550
551 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
552 if (!phys)
553 return -ENOMEM;
554
555 idev = devm_input_allocate_device(dev);
556 if (!idev)
557 return -ENOMEM;
558
559 idev->name = CROS_EC_DEV_NAME;
560 idev->phys = phys;
561 __set_bit(EV_REP, idev->evbit);
562
563 idev->id.bustype = BUS_VIRTUAL;
564 idev->id.version = 1;
565 idev->id.product = 0;
566 idev->dev.parent = dev;
567
568 ckdev->ghost_filter = of_property_read_bool(dev->of_node,
569 "google,needs-ghost-filter");
570
571 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
572 NULL, idev);
573 if (err) {
574 dev_err(dev, "cannot build key matrix\n");
575 return err;
576 }
577
578 ckdev->row_shift = get_count_order(ckdev->cols);
579
580 input_set_capability(idev, EV_MSC, MSC_SCAN);
581 input_set_drvdata(idev, ckdev);
582 ckdev->idev = idev;
583 cros_ec_keyb_compute_valid_keys(ckdev);
584
585 err = input_register_device(ckdev->idev);
586 if (err) {
587 dev_err(dev, "cannot register input device\n");
588 return err;
589 }
590
591 return 0;
592 }
593
594 static int cros_ec_keyb_probe(struct platform_device *pdev)
595 {
596 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
597 struct device *dev = &pdev->dev;
598 struct cros_ec_keyb *ckdev;
599 int err;
600
601 if (!dev->of_node)
602 return -ENODEV;
603
604 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
605 if (!ckdev)
606 return -ENOMEM;
607
608 ckdev->ec = ec;
609 ckdev->dev = dev;
610 dev_set_drvdata(dev, ckdev);
611
612 err = cros_ec_keyb_register_matrix(ckdev);
613 if (err) {
614 dev_err(dev, "cannot register matrix inputs: %d\n", err);
615 return err;
616 }
617
618 err = cros_ec_keyb_register_bs(ckdev);
619 if (err) {
620 dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
621 return err;
622 }
623
624 ckdev->notifier.notifier_call = cros_ec_keyb_work;
625 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
626 &ckdev->notifier);
627 if (err) {
628 dev_err(dev, "cannot register notifier: %d\n", err);
629 return err;
630 }
631
632 device_init_wakeup(ckdev->dev, true);
633 return 0;
634 }
635
636 static int cros_ec_keyb_remove(struct platform_device *pdev)
637 {
638 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
639
640 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
641 &ckdev->notifier);
642
643 return 0;
644 }
645
646 #ifdef CONFIG_OF
647 static const struct of_device_id cros_ec_keyb_of_match[] = {
648 { .compatible = "google,cros-ec-keyb" },
649 {},
650 };
651 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
652 #endif
653
654 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
655
656 static struct platform_driver cros_ec_keyb_driver = {
657 .probe = cros_ec_keyb_probe,
658 .remove = cros_ec_keyb_remove,
659 .driver = {
660 .name = "cros-ec-keyb",
661 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
662 .pm = &cros_ec_keyb_pm_ops,
663 },
664 };
665
666 module_platform_driver(cros_ec_keyb_driver);
667
668 MODULE_LICENSE("GPL v2");
669 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
670 MODULE_ALIAS("platform:cros-ec-keyb");