1 // SPDX-License-Identifier: GPL-2.0
3 * Topstar Laptop ACPI Extras driver
5 * Copyright (c) 2009 Herton Ronaldo Krzesinski <herton@mandriva.com.br>
6 * Copyright (c) 2018 Guillaume Douézan-Grard
8 * Implementation inspired by existing x86 platform drivers, in special
9 * asus/eepc/fujitsu-laptop, thanks to their authors.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/acpi.h>
19 #include <linux/dmi.h>
20 #include <linux/input.h>
21 #include <linux/input/sparse-keymap.h>
22 #include <linux/leds.h>
23 #include <linux/platform_device.h>
25 #define TOPSTAR_LAPTOP_CLASS "topstar"
27 struct topstar_laptop
{
28 struct acpi_device
*device
;
29 struct platform_device
*platform
;
30 struct input_dev
*input
;
31 struct led_classdev led
;
38 static enum led_brightness
topstar_led_get(struct led_classdev
*led
)
40 return led
->brightness
;
43 static int topstar_led_set(struct led_classdev
*led
,
44 enum led_brightness state
)
46 struct topstar_laptop
*topstar
= container_of(led
,
47 struct topstar_laptop
, led
);
49 struct acpi_object_list params
;
50 union acpi_object in_obj
;
51 unsigned long long int ret
;
55 params
.pointer
= &in_obj
;
56 in_obj
.type
= ACPI_TYPE_INTEGER
;
57 in_obj
.integer
.value
= 0x83;
60 * Topstar ACPI returns 0x30001 when the LED is ON and 0x30000 when it
63 status
= acpi_evaluate_integer(topstar
->device
->handle
,
64 "GETX", ¶ms
, &ret
);
65 if (ACPI_FAILURE(status
))
69 * FNCX(0x83) toggles the LED (more precisely, it is supposed to
70 * act as an hardware switch and disconnect the WLAN adapter but
71 * it seems to be faulty on some models like the Topstar U931
74 if ((ret
== 0x30001 && state
== LED_OFF
)
75 || (ret
== 0x30000 && state
!= LED_OFF
)) {
76 status
= acpi_execute_simple_method(topstar
->device
->handle
,
78 if (ACPI_FAILURE(status
))
85 static int topstar_led_init(struct topstar_laptop
*topstar
)
87 topstar
->led
= (struct led_classdev
) {
88 .default_trigger
= "rfkill0",
89 .brightness_get
= topstar_led_get
,
90 .brightness_set_blocking
= topstar_led_set
,
91 .name
= TOPSTAR_LAPTOP_CLASS
"::wlan",
94 return led_classdev_register(&topstar
->platform
->dev
, &topstar
->led
);
97 static void topstar_led_exit(struct topstar_laptop
*topstar
)
99 led_classdev_unregister(&topstar
->led
);
106 static const struct key_entry topstar_keymap
[] = {
107 { KE_KEY
, 0x80, { KEY_BRIGHTNESSUP
} },
108 { KE_KEY
, 0x81, { KEY_BRIGHTNESSDOWN
} },
109 { KE_KEY
, 0x83, { KEY_VOLUMEUP
} },
110 { KE_KEY
, 0x84, { KEY_VOLUMEDOWN
} },
111 { KE_KEY
, 0x85, { KEY_MUTE
} },
112 { KE_KEY
, 0x86, { KEY_SWITCHVIDEOMODE
} },
113 { KE_KEY
, 0x87, { KEY_F13
} }, /* touchpad enable/disable key */
114 { KE_KEY
, 0x88, { KEY_WLAN
} },
115 { KE_KEY
, 0x8a, { KEY_WWW
} },
116 { KE_KEY
, 0x8b, { KEY_MAIL
} },
117 { KE_KEY
, 0x8c, { KEY_MEDIA
} },
119 /* Known non hotkey events don't handled or that we don't care yet */
120 { KE_IGNORE
, 0x82, }, /* backlight event */
121 { KE_IGNORE
, 0x8e, },
122 { KE_IGNORE
, 0x8f, },
123 { KE_IGNORE
, 0x90, },
126 * 'G key' generate two event codes, convert to only
127 * one event/key code for now, consider replacing by
128 * a switch (3G switch - SW_3G?)
130 { KE_KEY
, 0x96, { KEY_F14
} },
131 { KE_KEY
, 0x97, { KEY_F14
} },
136 static void topstar_input_notify(struct topstar_laptop
*topstar
, int event
)
138 if (!sparse_keymap_report_event(topstar
->input
, event
, 1, true))
139 pr_info("unknown event = 0x%02x\n", event
);
142 static int topstar_input_init(struct topstar_laptop
*topstar
)
144 struct input_dev
*input
;
147 input
= input_allocate_device();
151 input
->name
= "Topstar Laptop extra buttons";
152 input
->phys
= TOPSTAR_LAPTOP_CLASS
"/input0";
153 input
->id
.bustype
= BUS_HOST
;
154 input
->dev
.parent
= &topstar
->platform
->dev
;
156 err
= sparse_keymap_setup(input
, topstar_keymap
, NULL
);
158 pr_err("Unable to setup input device keymap\n");
162 err
= input_register_device(input
);
164 pr_err("Unable to register input device\n");
168 topstar
->input
= input
;
172 input_free_device(input
);
176 static void topstar_input_exit(struct topstar_laptop
*topstar
)
178 input_unregister_device(topstar
->input
);
185 static struct platform_driver topstar_platform_driver
= {
187 .name
= TOPSTAR_LAPTOP_CLASS
,
191 static int topstar_platform_init(struct topstar_laptop
*topstar
)
195 topstar
->platform
= platform_device_alloc(TOPSTAR_LAPTOP_CLASS
, -1);
196 if (!topstar
->platform
)
199 platform_set_drvdata(topstar
->platform
, topstar
);
201 err
= platform_device_add(topstar
->platform
);
208 platform_device_put(topstar
->platform
);
212 static void topstar_platform_exit(struct topstar_laptop
*topstar
)
214 platform_device_unregister(topstar
->platform
);
221 static int topstar_acpi_fncx_switch(struct acpi_device
*device
, bool state
)
224 u64 arg
= state
? 0x86 : 0x87;
226 status
= acpi_execute_simple_method(device
->handle
, "FNCX", arg
);
227 if (ACPI_FAILURE(status
)) {
228 pr_err("Unable to switch FNCX notifications\n");
235 static void topstar_acpi_notify(struct acpi_device
*device
, u32 event
)
237 struct topstar_laptop
*topstar
= acpi_driver_data(device
);
238 static bool dup_evnt
[2];
241 /* 0x83 and 0x84 key events comes duplicated... */
242 if (event
== 0x83 || event
== 0x84) {
243 dup
= &dup_evnt
[event
- 0x83];
251 topstar_input_notify(topstar
, event
);
254 static int topstar_acpi_init(struct topstar_laptop
*topstar
)
256 return topstar_acpi_fncx_switch(topstar
->device
, true);
259 static void topstar_acpi_exit(struct topstar_laptop
*topstar
)
261 topstar_acpi_fncx_switch(topstar
->device
, false);
265 * Enable software-based WLAN LED control on systems with defective
268 static bool led_workaround
;
270 static int dmi_led_workaround(const struct dmi_system_id
*id
)
272 led_workaround
= true;
276 static const struct dmi_system_id topstar_dmi_ids
[] = {
278 .callback
= dmi_led_workaround
,
279 .ident
= "Topstar U931/RVP7",
281 DMI_MATCH(DMI_BOARD_NAME
, "U931"),
282 DMI_MATCH(DMI_BOARD_VERSION
, "RVP7"),
288 static int topstar_acpi_add(struct acpi_device
*device
)
290 struct topstar_laptop
*topstar
;
293 dmi_check_system(topstar_dmi_ids
);
295 topstar
= kzalloc(sizeof(struct topstar_laptop
), GFP_KERNEL
);
299 strcpy(acpi_device_name(device
), "Topstar TPSACPI");
300 strcpy(acpi_device_class(device
), TOPSTAR_LAPTOP_CLASS
);
301 device
->driver_data
= topstar
;
302 topstar
->device
= device
;
304 err
= topstar_acpi_init(topstar
);
308 err
= topstar_platform_init(topstar
);
312 err
= topstar_input_init(topstar
);
314 goto err_platform_exit
;
316 if (led_workaround
) {
317 err
= topstar_led_init(topstar
);
325 topstar_input_exit(topstar
);
327 topstar_platform_exit(topstar
);
329 topstar_acpi_exit(topstar
);
335 static int topstar_acpi_remove(struct acpi_device
*device
)
337 struct topstar_laptop
*topstar
= acpi_driver_data(device
);
340 topstar_led_exit(topstar
);
342 topstar_input_exit(topstar
);
343 topstar_platform_exit(topstar
);
344 topstar_acpi_exit(topstar
);
350 static const struct acpi_device_id topstar_device_ids
[] = {
355 MODULE_DEVICE_TABLE(acpi
, topstar_device_ids
);
357 static struct acpi_driver topstar_acpi_driver
= {
358 .name
= "Topstar laptop ACPI driver",
359 .class = TOPSTAR_LAPTOP_CLASS
,
360 .ids
= topstar_device_ids
,
362 .add
= topstar_acpi_add
,
363 .remove
= topstar_acpi_remove
,
364 .notify
= topstar_acpi_notify
,
368 static int __init
topstar_laptop_init(void)
372 ret
= platform_driver_register(&topstar_platform_driver
);
376 ret
= acpi_bus_register_driver(&topstar_acpi_driver
);
378 goto err_driver_unreg
;
380 pr_info("ACPI extras driver loaded\n");
384 platform_driver_unregister(&topstar_platform_driver
);
388 static void __exit
topstar_laptop_exit(void)
390 acpi_bus_unregister_driver(&topstar_acpi_driver
);
391 platform_driver_unregister(&topstar_platform_driver
);
394 module_init(topstar_laptop_init
);
395 module_exit(topstar_laptop_exit
);
397 MODULE_AUTHOR("Herton Ronaldo Krzesinski");
398 MODULE_AUTHOR("Guillaume Douézan-Grard");
399 MODULE_DESCRIPTION("Topstar Laptop ACPI Extras driver");
400 MODULE_LICENSE("GPL");