leds-clevo-mail.c 5.3 KB

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