blob: f512e99b976b17d610e1e9b6afbf372d12080168 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Sachin Kamat5c6f8442012-11-27 20:38:10 -08002#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Márton Némethcec035d2007-10-31 11:46:41 +01003
4#include <linux/module.h>
5
6#include <linux/platform_device.h>
7#include <linux/err.h>
8#include <linux/leds.h>
9
10#include <linux/io.h>
11#include <linux/dmi.h>
12
13#include <linux/i8042.h>
14
15#define CLEVO_MAIL_LED_OFF 0x0084
16#define CLEVO_MAIL_LED_BLINK_1HZ 0x008A
17#define CLEVO_MAIL_LED_BLINK_0_5HZ 0x0083
18
Németh Márton4d404fd2008-03-09 20:59:57 +000019MODULE_AUTHOR("Márton Németh <nm127@freemail.hu>");
Márton Némethcec035d2007-10-31 11:46:41 +010020MODULE_DESCRIPTION("Clevo mail LED driver");
21MODULE_LICENSE("GPL");
22
Jingoo Hanaad0f292014-02-27 23:26:08 -080023static bool nodetect;
Márton Némethcec035d2007-10-31 11:46:41 +010024module_param_named(nodetect, nodetect, bool, 0);
25MODULE_PARM_DESC(nodetect, "Skip DMI hardware detection");
26
27static struct platform_device *pdev;
28
29static int __init clevo_mail_led_dmi_callback(const struct dmi_system_id *id)
30{
Sachin Kamat5c6f8442012-11-27 20:38:10 -080031 pr_info("'%s' found\n", id->ident);
Márton Némethcec035d2007-10-31 11:46:41 +010032 return 1;
33}
34
35/*
Ondrej Zary96f09792012-08-02 05:04:46 +080036 * struct clevo_mail_led_dmi_table - List of known good models
Márton Némethcec035d2007-10-31 11:46:41 +010037 *
38 * Contains the known good models this driver is compatible with.
39 * When adding a new model try to be as strict as possible. This
40 * makes it possible to keep the false positives (the model is
41 * detected as working, but in reality it is not) as low as
42 * possible.
43 */
Christoph Hellwig6faadbb2017-09-14 11:59:30 +020044static const struct dmi_system_id clevo_mail_led_dmi_table[] __initconst = {
Márton Némethcec035d2007-10-31 11:46:41 +010045 {
46 .callback = clevo_mail_led_dmi_callback,
47 .ident = "Clevo D410J",
48 .matches = {
49 DMI_MATCH(DMI_SYS_VENDOR, "VIA"),
50 DMI_MATCH(DMI_PRODUCT_NAME, "K8N800"),
51 DMI_MATCH(DMI_PRODUCT_VERSION, "VT8204B")
52 }
53 },
54 {
55 .callback = clevo_mail_led_dmi_callback,
56 .ident = "Clevo M5x0N",
57 .matches = {
58 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
59 DMI_MATCH(DMI_PRODUCT_NAME, "M5x0N")
60 }
61 },
62 {
63 .callback = clevo_mail_led_dmi_callback,
Ondrej Zaryee539a92012-08-02 05:04:56 +080064 .ident = "Clevo M5x0V",
Márton Némethcec035d2007-10-31 11:46:41 +010065 .matches = {
66 DMI_MATCH(DMI_BOARD_VENDOR, "CLEVO Co. "),
67 DMI_MATCH(DMI_BOARD_NAME, "M5X0V "),
Márton Némethcec035d2007-10-31 11:46:41 +010068 DMI_MATCH(DMI_PRODUCT_VERSION, "VT6198")
69 }
70 },
71 {
72 .callback = clevo_mail_led_dmi_callback,
Mrton Nmethb3ba31f2008-03-09 20:47:59 +000073 .ident = "Clevo D400P",
74 .matches = {
75 DMI_MATCH(DMI_BOARD_VENDOR, "Clevo"),
76 DMI_MATCH(DMI_BOARD_NAME, "D400P"),
77 DMI_MATCH(DMI_BOARD_VERSION, "Rev.A"),
78 DMI_MATCH(DMI_PRODUCT_VERSION, "0106")
79 }
80 },
81 {
82 .callback = clevo_mail_led_dmi_callback,
Márton Némethcec035d2007-10-31 11:46:41 +010083 .ident = "Clevo D410V",
84 .matches = {
85 DMI_MATCH(DMI_BOARD_VENDOR, "Clevo, Co."),
86 DMI_MATCH(DMI_BOARD_NAME, "D400V/D470V"),
87 DMI_MATCH(DMI_BOARD_VERSION, "SS78B"),
88 DMI_MATCH(DMI_PRODUCT_VERSION, "Rev. A1")
89 }
90 },
91 { }
92};
Ondrej Zary96f09792012-08-02 05:04:46 +080093MODULE_DEVICE_TABLE(dmi, clevo_mail_led_dmi_table);
Márton Némethcec035d2007-10-31 11:46:41 +010094
95static void clevo_mail_led_set(struct led_classdev *led_cdev,
96 enum led_brightness value)
97{
Dmitry Torokhov181d6832009-09-16 01:06:43 -070098 i8042_lock_chip();
99
Márton Némethcec035d2007-10-31 11:46:41 +0100100 if (value == LED_OFF)
101 i8042_command(NULL, CLEVO_MAIL_LED_OFF);
102 else if (value <= LED_HALF)
103 i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
104 else
105 i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
106
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700107 i8042_unlock_chip();
108
Márton Némethcec035d2007-10-31 11:46:41 +0100109}
110
Márton Németh92e015c2007-10-31 15:09:05 +0100111static int clevo_mail_led_blink(struct led_classdev *led_cdev,
Németh Márton4d404fd2008-03-09 20:59:57 +0000112 unsigned long *delay_on,
113 unsigned long *delay_off)
Márton Németh92e015c2007-10-31 15:09:05 +0100114{
115 int status = -EINVAL;
116
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700117 i8042_lock_chip();
118
Márton Németh92e015c2007-10-31 15:09:05 +0100119 if (*delay_on == 0 /* ms */ && *delay_off == 0 /* ms */) {
120 /* Special case: the leds subsystem requested us to
121 * chose one user friendly blinking of the LED, and
122 * start it. Let's blink the led slowly (0.5Hz).
123 */
124 *delay_on = 1000; /* ms */
125 *delay_off = 1000; /* ms */
126 i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
127 status = 0;
128
129 } else if (*delay_on == 500 /* ms */ && *delay_off == 500 /* ms */) {
130 /* blink the led with 1Hz */
131 i8042_command(NULL, CLEVO_MAIL_LED_BLINK_1HZ);
132 status = 0;
133
134 } else if (*delay_on == 1000 /* ms */ && *delay_off == 1000 /* ms */) {
135 /* blink the led with 0.5Hz */
136 i8042_command(NULL, CLEVO_MAIL_LED_BLINK_0_5HZ);
137 status = 0;
138
139 } else {
Sachin Kamat5c6f8442012-11-27 20:38:10 -0800140 pr_debug("clevo_mail_led_blink(..., %lu, %lu),"
Márton Németh92e015c2007-10-31 15:09:05 +0100141 " returning -EINVAL (unsupported)\n",
142 *delay_on, *delay_off);
143 }
144
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700145 i8042_unlock_chip();
146
Márton Németh92e015c2007-10-31 15:09:05 +0100147 return status;
148}
149
Márton Némethcec035d2007-10-31 11:46:41 +0100150static struct led_classdev clevo_mail_led = {
Richard Purdie6c152be2007-10-31 15:00:07 +0100151 .name = "clevo::mail",
Márton Némethcec035d2007-10-31 11:46:41 +0100152 .brightness_set = clevo_mail_led_set,
Márton Németh92e015c2007-10-31 15:09:05 +0100153 .blink_set = clevo_mail_led_blink,
Richard Purdie859cb7f2009-01-08 17:55:03 +0000154 .flags = LED_CORE_SUSPENDRESUME,
Márton Némethcec035d2007-10-31 11:46:41 +0100155};
156
Jean Delvare0016db22014-03-13 10:56:24 -0700157static int __init clevo_mail_led_probe(struct platform_device *pdev)
Márton Némethcec035d2007-10-31 11:46:41 +0100158{
159 return led_classdev_register(&pdev->dev, &clevo_mail_led);
160}
161
162static int clevo_mail_led_remove(struct platform_device *pdev)
163{
164 led_classdev_unregister(&clevo_mail_led);
165 return 0;
166}
167
Márton Némethcec035d2007-10-31 11:46:41 +0100168static struct platform_driver clevo_mail_led_driver = {
Márton Némethcec035d2007-10-31 11:46:41 +0100169 .remove = clevo_mail_led_remove,
Márton Némethcec035d2007-10-31 11:46:41 +0100170 .driver = {
171 .name = KBUILD_MODNAME,
Márton Némethcec035d2007-10-31 11:46:41 +0100172 },
173};
174
175static int __init clevo_mail_led_init(void)
176{
177 int error = 0;
178 int count = 0;
179
180 /* Check with the help of DMI if we are running on supported hardware */
181 if (!nodetect) {
Ondrej Zary96f09792012-08-02 05:04:46 +0800182 count = dmi_check_system(clevo_mail_led_dmi_table);
Márton Némethcec035d2007-10-31 11:46:41 +0100183 } else {
184 count = 1;
Sachin Kamat5c6f8442012-11-27 20:38:10 -0800185 pr_err("Skipping DMI detection. "
Márton Némethcec035d2007-10-31 11:46:41 +0100186 "If the driver works on your hardware please "
187 "report model and the output of dmidecode in tracker "
188 "at http://sourceforge.net/projects/clevo-mailled/\n");
189 }
190
191 if (!count)
192 return -ENODEV;
193
194 pdev = platform_device_register_simple(KBUILD_MODNAME, -1, NULL, 0);
195 if (!IS_ERR(pdev)) {
196 error = platform_driver_probe(&clevo_mail_led_driver,
197 clevo_mail_led_probe);
198 if (error) {
Sachin Kamat5c6f8442012-11-27 20:38:10 -0800199 pr_err("Can't probe platform driver\n");
Márton Némethcec035d2007-10-31 11:46:41 +0100200 platform_device_unregister(pdev);
201 }
202 } else
203 error = PTR_ERR(pdev);
204
205 return error;
206}
207
208static void __exit clevo_mail_led_exit(void)
209{
210 platform_device_unregister(pdev);
211 platform_driver_unregister(&clevo_mail_led_driver);
212
213 clevo_mail_led_set(NULL, LED_OFF);
214}
215
216module_init(clevo_mail_led_init);
217module_exit(clevo_mail_led_exit);