1 // SPDX-License-Identifier: GPL-2.0+
3 * lg-laptop.c - LG Gram ACPI features and hotkeys Driver
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/acpi.h>
11 #include <linux/dmi.h>
12 #include <linux/input.h>
13 #include <linux/input/sparse-keymap.h>
14 #include <linux/kernel.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
20 #include <acpi/battery.h>
22 #define LED_DEVICE(_name, max, flag) struct led_classdev _name = { \
23 .name = __stringify(_name), \
24 .max_brightness = max, \
25 .brightness_set = _name##_set, \
26 .brightness_get = _name##_get, \
30 MODULE_AUTHOR("Matan Ziv-Av");
31 MODULE_DESCRIPTION("LG WMI Hotkey Driver");
32 MODULE_LICENSE("GPL");
34 #define WMI_EVENT_GUID0 "E4FB94F9-7F2B-4173-AD1A-CD1D95086248"
35 #define WMI_EVENT_GUID1 "023B133E-49D1-4E10-B313-698220140DC2"
36 #define WMI_EVENT_GUID2 "37BE1AC0-C3F2-4B1F-BFBE-8FDEAF2814D6"
37 #define WMI_EVENT_GUID3 "911BAD44-7DF8-4FBB-9319-BABA1C4B293B"
38 #define WMI_METHOD_WMAB "C3A72B38-D3EF-42D3-8CBB-D5A57049F66D"
39 #define WMI_METHOD_WMBB "2B4F501A-BD3C-4394-8DCF-00A7D2BC8210"
40 #define WMI_EVENT_GUID WMI_EVENT_GUID0
42 #define WMAB_METHOD "\\XINI.WMAB"
43 #define WMBB_METHOD "\\XINI.WMBB"
44 #define SB_GGOV_METHOD "\\_SB.GGOV"
45 #define GOV_TLED 0x2020008
48 #define WM_KEY_LIGHT 0x400
50 #define WM_FN_LOCK 0x407
51 #define WM_BATT_LIMIT 0x61
52 #define WM_READER_MODE 0xBF
53 #define WM_FAN_MODE 0x33
54 #define WMBB_USB_CHARGE 0x10B
55 #define WMBB_BATT_LIMIT 0x10C
57 #define PLATFORM_NAME "lg-laptop"
59 MODULE_ALIAS("wmi:" WMI_EVENT_GUID0);
60 MODULE_ALIAS("wmi:" WMI_EVENT_GUID1);
61 MODULE_ALIAS("wmi:" WMI_EVENT_GUID2);
62 MODULE_ALIAS("wmi:" WMI_EVENT_GUID3);
63 MODULE_ALIAS("wmi:" WMI_METHOD_WMAB);
64 MODULE_ALIAS("wmi:" WMI_METHOD_WMBB);
66 static struct platform_device *pf_device;
67 static struct input_dev *wmi_input_dev;
70 #define INIT_INPUT_WMI_0 0x01
71 #define INIT_INPUT_WMI_2 0x02
72 #define INIT_INPUT_ACPI 0x04
73 #define INIT_SPARSE_KEYMAP 0x80
75 static int battery_limit_use_wmbb;
76 static struct led_classdev kbd_backlight;
77 static enum led_brightness get_kbd_backlight_level(void);
79 static const struct key_entry wmi_keymap[] = {
80 {KE_KEY, 0x70, {KEY_F15} }, /* LG control panel (F1) */
81 {KE_KEY, 0x74, {KEY_F21} }, /* Touchpad toggle (F5) */
82 {KE_KEY, 0xf020000, {KEY_F14} }, /* Read mode (F9) */
83 {KE_KEY, 0x10000000, {KEY_F16} },/* Keyboard backlight (F8) - pressing
84 * this key both sends an event and
85 * changes backlight level.
87 {KE_KEY, 0x80, {KEY_RFKILL} },
91 static int ggov(u32 arg0)
93 union acpi_object args[1];
97 struct acpi_object_list arg;
98 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
101 args[0].type = ACPI_TYPE_INTEGER;
102 args[0].integer.value = arg0;
104 status = acpi_get_handle(NULL, (acpi_string) SB_GGOV_METHOD, &handle);
105 if (ACPI_FAILURE(status)) {
106 pr_err("Cannot get handle");
113 status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
114 if (ACPI_FAILURE(status)) {
115 acpi_handle_err(handle, "GGOV: call failed.\n");
120 if (r->type != ACPI_TYPE_INTEGER) {
125 res = r->integer.value;
131 static union acpi_object *lg_wmab(u32 method, u32 arg1, u32 arg2)
133 union acpi_object args[3];
136 struct acpi_object_list arg;
137 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
139 args[0].type = ACPI_TYPE_INTEGER;
140 args[0].integer.value = method;
141 args[1].type = ACPI_TYPE_INTEGER;
142 args[1].integer.value = arg1;
143 args[2].type = ACPI_TYPE_INTEGER;
144 args[2].integer.value = arg2;
146 status = acpi_get_handle(NULL, (acpi_string) WMAB_METHOD, &handle);
147 if (ACPI_FAILURE(status)) {
148 pr_err("Cannot get handle");
155 status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
156 if (ACPI_FAILURE(status)) {
157 acpi_handle_err(handle, "WMAB: call failed.\n");
161 return buffer.pointer;
164 static union acpi_object *lg_wmbb(u32 method_id, u32 arg1, u32 arg2)
166 union acpi_object args[3];
169 struct acpi_object_list arg;
170 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
173 *(u32 *)buf = method_id;
174 *(u32 *)(buf + 4) = arg1;
175 *(u32 *)(buf + 16) = arg2;
176 args[0].type = ACPI_TYPE_INTEGER;
177 args[0].integer.value = 0; /* ignored */
178 args[1].type = ACPI_TYPE_INTEGER;
179 args[1].integer.value = 1; /* Must be 1 or 2. Does not matter which */
180 args[2].type = ACPI_TYPE_BUFFER;
181 args[2].buffer.length = 32;
182 args[2].buffer.pointer = buf;
184 status = acpi_get_handle(NULL, (acpi_string)WMBB_METHOD, &handle);
185 if (ACPI_FAILURE(status)) {
186 pr_err("Cannot get handle");
193 status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
194 if (ACPI_FAILURE(status)) {
195 acpi_handle_err(handle, "WMAB: call failed.\n");
199 return (union acpi_object *)buffer.pointer;
202 static void wmi_notify(u32 value, void *context)
204 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
205 union acpi_object *obj;
207 long data = (long)context;
209 pr_debug("event guid %li\n", data);
210 status = wmi_get_event_data(value, &response);
211 if (ACPI_FAILURE(status)) {
212 pr_err("Bad event status 0x%x\n", status);
216 obj = (union acpi_object *)response.pointer;
220 if (obj->type == ACPI_TYPE_INTEGER) {
221 int eventcode = obj->integer.value;
222 struct key_entry *key;
224 if (eventcode == 0x10000000) {
225 led_classdev_notify_brightness_hw_changed(
226 &kbd_backlight, get_kbd_backlight_level());
228 key = sparse_keymap_entry_from_scancode(
229 wmi_input_dev, eventcode);
230 if (key && key->type == KE_KEY)
231 sparse_keymap_report_entry(wmi_input_dev,
236 pr_debug("Type: %i Eventcode: 0x%llx\n", obj->type,
238 kfree(response.pointer);
241 static void wmi_input_setup(void)
245 wmi_input_dev = input_allocate_device();
247 wmi_input_dev->name = "LG WMI hotkeys";
248 wmi_input_dev->phys = "wmi/input0";
249 wmi_input_dev->id.bustype = BUS_HOST;
251 if (sparse_keymap_setup(wmi_input_dev, wmi_keymap, NULL) ||
252 input_register_device(wmi_input_dev)) {
253 pr_info("Cannot initialize input device");
254 input_free_device(wmi_input_dev);
258 inited |= INIT_SPARSE_KEYMAP;
259 status = wmi_install_notify_handler(WMI_EVENT_GUID0, wmi_notify,
261 if (ACPI_SUCCESS(status))
262 inited |= INIT_INPUT_WMI_0;
264 status = wmi_install_notify_handler(WMI_EVENT_GUID2, wmi_notify,
266 if (ACPI_SUCCESS(status))
267 inited |= INIT_INPUT_WMI_2;
269 pr_info("Cannot allocate input device");
273 static void acpi_notify(struct acpi_device *device, u32 event)
275 struct key_entry *key;
277 acpi_handle_debug(device->handle, "notify: %d\n", event);
278 if (inited & INIT_SPARSE_KEYMAP) {
279 key = sparse_keymap_entry_from_scancode(wmi_input_dev, 0x80);
280 if (key && key->type == KE_KEY)
281 sparse_keymap_report_entry(wmi_input_dev, key, 1, true);
285 static ssize_t fan_mode_store(struct device *dev,
286 struct device_attribute *attr,
287 const char *buffer, size_t count)
290 union acpi_object *r;
294 ret = kstrtobool(buffer, &value);
298 r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
302 if (r->type != ACPI_TYPE_INTEGER) {
307 m = r->integer.value;
309 r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xffffff0f) | (value << 4));
311 r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xfffffff0) | value);
317 static ssize_t fan_mode_show(struct device *dev,
318 struct device_attribute *attr, char *buffer)
321 union acpi_object *r;
323 r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
327 if (r->type != ACPI_TYPE_INTEGER) {
332 status = r->integer.value & 0x01;
335 return sysfs_emit(buffer, "%d\n", status);
338 static ssize_t usb_charge_store(struct device *dev,
339 struct device_attribute *attr,
340 const char *buffer, size_t count)
343 union acpi_object *r;
346 ret = kstrtobool(buffer, &value);
350 r = lg_wmbb(WMBB_USB_CHARGE, WM_SET, value);
358 static ssize_t usb_charge_show(struct device *dev,
359 struct device_attribute *attr, char *buffer)
362 union acpi_object *r;
364 r = lg_wmbb(WMBB_USB_CHARGE, WM_GET, 0);
368 if (r->type != ACPI_TYPE_BUFFER) {
373 status = !!r->buffer.pointer[0x10];
377 return sysfs_emit(buffer, "%d\n", status);
380 static ssize_t reader_mode_store(struct device *dev,
381 struct device_attribute *attr,
382 const char *buffer, size_t count)
385 union acpi_object *r;
388 ret = kstrtobool(buffer, &value);
392 r = lg_wmab(WM_READER_MODE, WM_SET, value);
400 static ssize_t reader_mode_show(struct device *dev,
401 struct device_attribute *attr, char *buffer)
404 union acpi_object *r;
406 r = lg_wmab(WM_READER_MODE, WM_GET, 0);
410 if (r->type != ACPI_TYPE_INTEGER) {
415 status = !!r->integer.value;
419 return sysfs_emit(buffer, "%d\n", status);
422 static ssize_t fn_lock_store(struct device *dev,
423 struct device_attribute *attr,
424 const char *buffer, size_t count)
427 union acpi_object *r;
430 ret = kstrtobool(buffer, &value);
434 r = lg_wmab(WM_FN_LOCK, WM_SET, value);
442 static ssize_t fn_lock_show(struct device *dev,
443 struct device_attribute *attr, char *buffer)
446 union acpi_object *r;
448 r = lg_wmab(WM_FN_LOCK, WM_GET, 0);
452 if (r->type != ACPI_TYPE_BUFFER) {
457 status = !!r->buffer.pointer[0];
460 return sysfs_emit(buffer, "%d\n", status);
463 static ssize_t charge_control_end_threshold_store(struct device *dev,
464 struct device_attribute *attr,
465 const char *buf, size_t count)
470 ret = kstrtoul(buf, 10, &value);
474 if (value == 100 || value == 80) {
475 union acpi_object *r;
477 if (battery_limit_use_wmbb)
478 r = lg_wmbb(WMBB_BATT_LIMIT, WM_SET, value);
480 r = lg_wmab(WM_BATT_LIMIT, WM_SET, value);
491 static ssize_t charge_control_end_threshold_show(struct device *device,
492 struct device_attribute *attr,
496 union acpi_object *r;
498 if (battery_limit_use_wmbb) {
499 r = lg_wmbb(WMBB_BATT_LIMIT, WM_GET, 0);
503 if (r->type != ACPI_TYPE_BUFFER) {
508 status = r->buffer.pointer[0x10];
510 r = lg_wmab(WM_BATT_LIMIT, WM_GET, 0);
514 if (r->type != ACPI_TYPE_INTEGER) {
519 status = r->integer.value;
522 if (status != 80 && status != 100)
525 return sysfs_emit(buf, "%d\n", status);
528 static ssize_t battery_care_limit_show(struct device *dev,
529 struct device_attribute *attr,
532 return charge_control_end_threshold_show(dev, attr, buffer);
535 static ssize_t battery_care_limit_store(struct device *dev,
536 struct device_attribute *attr,
537 const char *buffer, size_t count)
539 return charge_control_end_threshold_store(dev, attr, buffer, count);
542 static DEVICE_ATTR_RW(fan_mode);
543 static DEVICE_ATTR_RW(usb_charge);
544 static DEVICE_ATTR_RW(reader_mode);
545 static DEVICE_ATTR_RW(fn_lock);
546 static DEVICE_ATTR_RW(charge_control_end_threshold);
547 static DEVICE_ATTR_RW(battery_care_limit);
549 static int lg_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
551 if (device_create_file(&battery->dev,
552 &dev_attr_charge_control_end_threshold))
558 static int lg_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
560 device_remove_file(&battery->dev,
561 &dev_attr_charge_control_end_threshold);
565 static struct acpi_battery_hook battery_hook = {
566 .add_battery = lg_battery_add,
567 .remove_battery = lg_battery_remove,
568 .name = "LG Battery Extension",
571 static struct attribute *dev_attributes[] = {
572 &dev_attr_fan_mode.attr,
573 &dev_attr_usb_charge.attr,
574 &dev_attr_reader_mode.attr,
575 &dev_attr_fn_lock.attr,
576 &dev_attr_battery_care_limit.attr,
580 static const struct attribute_group dev_attribute_group = {
581 .attrs = dev_attributes,
584 static void tpad_led_set(struct led_classdev *cdev,
585 enum led_brightness brightness)
587 union acpi_object *r;
589 r = lg_wmab(WM_TLED, WM_SET, brightness > LED_OFF);
593 static enum led_brightness tpad_led_get(struct led_classdev *cdev)
595 return ggov(GOV_TLED) > 0 ? LED_ON : LED_OFF;
598 static LED_DEVICE(tpad_led, 1, 0);
600 static void kbd_backlight_set(struct led_classdev *cdev,
601 enum led_brightness brightness)
604 union acpi_object *r;
607 if (brightness <= LED_OFF)
609 if (brightness >= LED_FULL)
611 r = lg_wmab(WM_KEY_LIGHT, WM_SET, val);
615 static enum led_brightness get_kbd_backlight_level(void)
617 union acpi_object *r;
620 r = lg_wmab(WM_KEY_LIGHT, WM_GET, 0);
625 if (r->type != ACPI_TYPE_BUFFER || r->buffer.pointer[1] != 0x05) {
630 switch (r->buffer.pointer[0] & 0x27) {
646 static enum led_brightness kbd_backlight_get(struct led_classdev *cdev)
648 return get_kbd_backlight_level();
651 static LED_DEVICE(kbd_backlight, 255, LED_BRIGHT_HW_CHANGED);
653 static void wmi_input_destroy(void)
655 if (inited & INIT_INPUT_WMI_2)
656 wmi_remove_notify_handler(WMI_EVENT_GUID2);
658 if (inited & INIT_INPUT_WMI_0)
659 wmi_remove_notify_handler(WMI_EVENT_GUID0);
661 if (inited & INIT_SPARSE_KEYMAP)
662 input_unregister_device(wmi_input_dev);
664 inited &= ~(INIT_INPUT_WMI_0 | INIT_INPUT_WMI_2 | INIT_SPARSE_KEYMAP);
667 static struct platform_driver pf_driver = {
669 .name = PLATFORM_NAME,
673 static int acpi_add(struct acpi_device *device)
682 ret = platform_driver_register(&pf_driver);
686 pf_device = platform_device_register_simple(PLATFORM_NAME,
689 if (IS_ERR(pf_device)) {
690 ret = PTR_ERR(pf_device);
692 pr_err("unable to register platform device\n");
693 goto out_platform_registered;
695 product = dmi_get_system_info(DMI_PRODUCT_NAME);
696 if (product && strlen(product) > 4)
697 switch (product[4]) {
699 if (strlen(product) > 5)
700 switch (product[5]) {
724 if (strlen(product) > 5)
725 switch (product[5]) {
739 pr_info("product: %s year: %d\n", product ?: "unknown", year);
742 battery_limit_use_wmbb = 1;
744 ret = sysfs_create_group(&pf_device->dev.kobj, &dev_attribute_group);
746 goto out_platform_device;
748 /* LEDs are optional */
749 led_classdev_register(&pf_device->dev, &kbd_backlight);
750 led_classdev_register(&pf_device->dev, &tpad_led);
753 battery_hook_register(&battery_hook);
758 platform_device_unregister(pf_device);
759 out_platform_registered:
760 platform_driver_unregister(&pf_driver);
764 static void acpi_remove(struct acpi_device *device)
766 sysfs_remove_group(&pf_device->dev.kobj, &dev_attribute_group);
768 led_classdev_unregister(&tpad_led);
769 led_classdev_unregister(&kbd_backlight);
771 battery_hook_unregister(&battery_hook);
773 platform_device_unregister(pf_device);
775 platform_driver_unregister(&pf_driver);
778 static const struct acpi_device_id device_ids[] = {
782 MODULE_DEVICE_TABLE(acpi, device_ids);
784 static struct acpi_driver acpi_driver = {
785 .name = "LG Gram Laptop Support",
786 .class = "lg-laptop",
790 .remove = acpi_remove,
791 .notify = acpi_notify,
793 .owner = THIS_MODULE,
796 static int __init acpi_init(void)
800 result = acpi_bus_register_driver(&acpi_driver);
802 pr_debug("Error registering driver\n");
809 static void __exit acpi_exit(void)
811 acpi_bus_unregister_driver(&acpi_driver);
814 module_init(acpi_init);
815 module_exit(acpi_exit);