leds-clevo-mail.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/module.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/err.h>
  6. #include <linux/leds.h>
  7. #include <linux/io.h>
  8. #include <linux/dmi.h>
  9. #include <linux/i8042.h>
  10. #define CLEVO_MAIL_LED_OFF 0x0084
  11. #define CLEVO_MAIL_LED_BLINK_1HZ 0x008A
  12. #define CLEVO_MAIL_LED_BLINK_0_5HZ 0x0083
  13. MODULE_AUTHOR("Márton Németh <nm127@freemail.hu>");
  14. MODULE_DESCRIPTION("Clevo mail LED driver");
  15. MODULE_LICENSE("GPL");
  16. static bool nodetect;
  17. module_param_named(nodetect, nodetect, bool, 0);
  18. MODULE_PARM_DESC(nodetect, "Skip DMI hardware detection");
  19. static struct platform_device *pdev;
  20. static int __init clevo_mail_led_dmi_callback(const struct dmi_system_id *id)
  21. {
  22. pr_info("'%s' found\n", id->ident);
  23. return 1;
  24. }
  25. /*
  26. * struct clevo_mail_led_dmi_table - List of known good models
  27. *
  28. * Contains the known good models this driver is compatible with.
  29. * When adding a new model try to be as strict as possible. This
  30. * makes it possible to keep the false positives (the model is
  31. * detected as working, but in reality it is not) as low as
  32. * possible.
  33. */
  34. static const struct dmi_system_id clevo_mail_led_dmi_table[] __initconst = {
  35. {
  36. .callback = clevo_mail_led_dmi_callback,
  37. .ident = "Clevo D410J",
  38. .matches = {
  39. DMI_MATCH(DMI_SYS_VENDOR, "VIA"),
  40. DMI_MATCH(DMI_PRODUCT_NAME, "K8N800"),
  41. DMI_MATCH(DMI_PRODUCT_VERSION, "VT8204B")
  42. }
  43. },
  44. {
  45. .callback = clevo_mail_led_dmi_callback,
  46. .ident = "Clevo M5x0N",
  47. .matches = {
  48. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  49. DMI_MATCH(DMI_PRODUCT_NAME, "M5x0N")
  50. }
  51. },
  52. {
  53. .callback = clevo_mail_led_dmi_callback,
  54. .ident = "Clevo M5x0V",
  55. .matches = {
  56. DMI_MATCH(DMI_BOARD_VENDOR, "CLEVO Co. "),
  57. DMI_MATCH(DMI_BOARD_NAME, "M5X0V "),
  58. DMI_MATCH(DMI_PRODUCT_VERSION, "VT6198")
  59. }
  60. },
  61. {
  62. .callback = clevo_mail_led_dmi_callback,
  63. .ident = "Clevo D400P",
  64. .matches = {
  65. DMI_MATCH(DMI_BOARD_VENDOR, "Clevo"),
  66. DMI_MATCH(DMI_BOARD_NAME, "D400P"),
  67. DMI_MATCH(DMI_BOARD_VERSION, "Rev.A"),
  68. DMI_MATCH(DMI_PRODUCT_VERSION, "0106")
  69. }
  70. },
  71. {
  72. .callback = clevo_mail_led_dmi_callback,
  73. .ident = "Clevo D410V",
  74. .matches = {
  75. DMI_MATCH(DMI_BOARD_VENDOR, "Clevo, Co."),
  76. DMI_MATCH(DMI_BOARD_NAME, "D400V/D470V"),
  77. DMI_MATCH(DMI_BOARD_VERSION, "SS78B"),
  78. DMI_MATCH(DMI_PRODUCT_VERSION, "Rev. A1")
  79. }
  80. },
  81. { }
  82. };
  83. MODULE_DEVICE_TABLE(dmi, clevo_mail_led_dmi_table);
  84. static void clevo_mail_led_set(struct led_classdev *led_cdev,
  85. enum led_brightness value)
  86. {
  87. i8042_lock_chip();
  88. if (value == LED_OFF)
  89. i8042_command(NULL, CLEVO_MAIL_LED_OFF);
  90. else if (value <= LED_HALF)
  91. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  92. else
  93. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  94. i8042_unlock_chip();
  95. }
  96. static int clevo_mail_led_blink(struct led_classdev *led_cdev,
  97. unsigned long *delay_on,
  98. unsigned long *delay_off)
  99. {
  100. int status = -EINVAL;
  101. i8042_lock_chip();
  102. if (*delay_on == 0 /* ms */ && *delay_off == 0 /* ms */) {
  103. /* Special case: the leds subsystem requested us to
  104. * chose one user friendly blinking of the LED, and
  105. * start it. Let's blink the led slowly (0.5Hz).
  106. */
  107. *delay_on = 1000; /* ms */
  108. *delay_off = 1000; /* ms */
  109. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  110. status = 0;
  111. } else if (*delay_on == 500 /* ms */ && *delay_off == 500 /* ms */) {
  112. /* blink the led with 1Hz */
  113. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
  114. status = 0;
  115. } else if (*delay_on == 1000 /* ms */ && *delay_off == 1000 /* ms */) {
  116. /* blink the led with 0.5Hz */
  117. i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
  118. status = 0;
  119. } else {
  120. pr_debug("clevo_mail_led_blink(..., %lu, %lu),"
  121. " returning -EINVAL (unsupported)\n",
  122. *delay_on, *delay_off);
  123. }
  124. i8042_unlock_chip();
  125. return status;
  126. }
  127. static struct led_classdev clevo_mail_led = {
  128. .name = "clevo::mail",
  129. .brightness_set = clevo_mail_led_set,
  130. .blink_set = clevo_mail_led_blink,
  131. .flags = LED_CORE_SUSPENDRESUME,
  132. };
  133. static int __init clevo_mail_led_probe(struct platform_device *pdev)
  134. {
  135. return led_classdev_register(&pdev->dev, &clevo_mail_led);
  136. }
  137. static int clevo_mail_led_remove(struct platform_device *pdev)
  138. {
  139. led_classdev_unregister(&clevo_mail_led);
  140. return 0;
  141. }
  142. static struct platform_driver clevo_mail_led_driver = {
  143. .remove = clevo_mail_led_remove,
  144. .driver = {
  145. .name = KBUILD_MODNAME,
  146. },
  147. };
  148. static int __init clevo_mail_led_init(void)
  149. {
  150. int error = 0;
  151. int count = 0;
  152. /* Check with the help of DMI if we are running on supported hardware */
  153. if (!nodetect) {
  154. count = dmi_check_system(clevo_mail_led_dmi_table);
  155. } else {
  156. count = 1;
  157. pr_err("Skipping DMI detection. "
  158. "If the driver works on your hardware please "
  159. "report model and the output of dmidecode in tracker "
  160. "at http://sourceforge.net/projects/clevo-mailled/\n");
  161. }
  162. if (!count)
  163. return -ENODEV;
  164. pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
  165. if (!IS_ERR(pdev)) {
  166. error = platform_driver_probe(&clevo_mail_led_driver,
  167. clevo_mail_led_probe);
  168. if (error) {
  169. pr_err("Can't probe platform driver\n");
  170. platform_device_unregister(pdev);
  171. }
  172. } else
  173. error = PTR_ERR(pdev);
  174. return error;
  175. }
  176. static void __exit clevo_mail_led_exit(void)
  177. {
  178. platform_device_unregister(pdev);
  179. platform_driver_unregister(&clevo_mail_led_driver);
  180. clevo_mail_led_set(NULL, LED_OFF);
  181. }
  182. module_init(clevo_mail_led_init);
  183. module_exit(clevo_mail_led_exit);