nvec_kbd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nvec_kbd: keyboard driver for a NVIDIA compliant embedded controller
  4. *
  5. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
  6. *
  7. * Authors: Pierre-Hugues Husson <phhusson@free.fr>
  8. * Marc Dietrich <marvin24@gmx.de>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/input.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include "nvec-keytable.h"
  16. #include "nvec.h"
  17. enum kbd_subcmds {
  18. CNFG_WAKE = 3,
  19. CNFG_WAKE_KEY_REPORTING,
  20. SET_LEDS = 0xed,
  21. ENABLE_KBD = 0xf4,
  22. DISABLE_KBD,
  23. };
  24. static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
  25. + ARRAY_SIZE(extcode_tab_us102)];
  26. struct nvec_keys {
  27. struct input_dev *input;
  28. struct notifier_block notifier;
  29. struct nvec_chip *nvec;
  30. bool caps_lock;
  31. };
  32. static struct nvec_keys keys_dev;
  33. static void nvec_kbd_toggle_led(void)
  34. {
  35. char buf[] = { NVEC_KBD, SET_LEDS, 0 };
  36. keys_dev.caps_lock = !keys_dev.caps_lock;
  37. if (keys_dev.caps_lock)
  38. /* should be BIT(0) only, firmware bug? */
  39. buf[2] = BIT(0) | BIT(1) | BIT(2);
  40. nvec_write_async(keys_dev.nvec, buf, sizeof(buf));
  41. }
  42. static int nvec_keys_notifier(struct notifier_block *nb,
  43. unsigned long event_type, void *data)
  44. {
  45. int code, state;
  46. unsigned char *msg = data;
  47. if (event_type == NVEC_KB_EVT) {
  48. int _size = (msg[0] & (3 << 5)) >> 5;
  49. /* power on/off button */
  50. if (_size == NVEC_VAR_SIZE)
  51. return NOTIFY_STOP;
  52. if (_size == NVEC_3BYTES)
  53. msg++;
  54. code = msg[1] & 0x7f;
  55. state = msg[1] & 0x80;
  56. if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
  57. nvec_kbd_toggle_led();
  58. input_report_key(keys_dev.input, code_tabs[_size][code],
  59. !state);
  60. input_sync(keys_dev.input);
  61. return NOTIFY_STOP;
  62. }
  63. return NOTIFY_DONE;
  64. }
  65. static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
  66. unsigned int code, int value)
  67. {
  68. struct nvec_chip *nvec = keys_dev.nvec;
  69. char buf[] = { NVEC_KBD, SET_LEDS, 0 };
  70. if (type == EV_REP)
  71. return 0;
  72. if (type != EV_LED)
  73. return -1;
  74. if (code != LED_CAPSL)
  75. return -1;
  76. buf[2] = !!value;
  77. nvec_write_async(nvec, buf, sizeof(buf));
  78. return 0;
  79. }
  80. static int nvec_kbd_probe(struct platform_device *pdev)
  81. {
  82. struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
  83. int i, j, err;
  84. struct input_dev *idev;
  85. char clear_leds[] = { NVEC_KBD, SET_LEDS, 0 },
  86. enable_kbd[] = { NVEC_KBD, ENABLE_KBD },
  87. cnfg_wake[] = { NVEC_KBD, CNFG_WAKE, true, true },
  88. cnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
  89. true };
  90. j = 0;
  91. for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
  92. keycodes[j++] = code_tab_102us[i];
  93. for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
  94. keycodes[j++] = extcode_tab_us102[i];
  95. idev = devm_input_allocate_device(&pdev->dev);
  96. if (!idev)
  97. return -ENOMEM;
  98. idev->name = "nvec keyboard";
  99. idev->phys = "nvec";
  100. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_LED);
  101. idev->ledbit[0] = BIT_MASK(LED_CAPSL);
  102. idev->event = nvec_kbd_event;
  103. idev->keycode = keycodes;
  104. idev->keycodesize = sizeof(unsigned char);
  105. idev->keycodemax = ARRAY_SIZE(keycodes);
  106. for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
  107. set_bit(keycodes[i], idev->keybit);
  108. clear_bit(0, idev->keybit);
  109. err = input_register_device(idev);
  110. if (err)
  111. return err;
  112. keys_dev.input = idev;
  113. keys_dev.notifier.notifier_call = nvec_keys_notifier;
  114. keys_dev.nvec = nvec;
  115. nvec_register_notifier(nvec, &keys_dev.notifier, 0);
  116. /* Enable keyboard */
  117. nvec_write_async(nvec, enable_kbd, 2);
  118. /* configures wake on special keys */
  119. nvec_write_async(nvec, cnfg_wake, 4);
  120. /* enable wake key reporting */
  121. nvec_write_async(nvec, cnfg_wake_key_reporting, 3);
  122. /* Disable caps lock LED */
  123. nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
  124. return 0;
  125. }
  126. static int nvec_kbd_remove(struct platform_device *pdev)
  127. {
  128. struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
  129. char disable_kbd[] = { NVEC_KBD, DISABLE_KBD },
  130. uncnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
  131. false };
  132. nvec_write_async(nvec, uncnfg_wake_key_reporting, 3);
  133. nvec_write_async(nvec, disable_kbd, 2);
  134. nvec_unregister_notifier(nvec, &keys_dev.notifier);
  135. return 0;
  136. }
  137. static struct platform_driver nvec_kbd_driver = {
  138. .probe = nvec_kbd_probe,
  139. .remove = nvec_kbd_remove,
  140. .driver = {
  141. .name = "nvec-kbd",
  142. },
  143. };
  144. module_platform_driver(nvec_kbd_driver);
  145. MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
  146. MODULE_DESCRIPTION("NVEC keyboard driver");
  147. MODULE_ALIAS("platform:nvec-kbd");
  148. MODULE_LICENSE("GPL");