nvec_kbd.c 4.6 KB

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