Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * lg-laptop.c - LG Gram ACPI features and hotkeys Driver
  4 *
  5 * Copyright (C) 2018 Matan Ziv-Av <matan@svgalib.org>
  6 */
  7
  8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9
 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>
 19
 20#include <acpi/battery.h>
 21
 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,          \
 27	.flags = flag,                          \
 28}
 29
 30MODULE_AUTHOR("Matan Ziv-Av");
 31MODULE_DESCRIPTION("LG WMI Hotkey Driver");
 32MODULE_LICENSE("GPL");
 33
 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
 41
 42#define WMAB_METHOD     "\\XINI.WMAB"
 43#define WMBB_METHOD     "\\XINI.WMBB"
 44#define SB_GGOV_METHOD  "\\_SB.GGOV"
 45#define GOV_TLED        0x2020008
 46#define WM_GET          1
 47#define WM_SET          2
 48#define WM_KEY_LIGHT    0x400
 49#define WM_TLED         0x404
 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
 56
 57#define PLATFORM_NAME   "lg-laptop"
 58
 59MODULE_ALIAS("wmi:" WMI_EVENT_GUID0);
 60MODULE_ALIAS("wmi:" WMI_EVENT_GUID1);
 61MODULE_ALIAS("wmi:" WMI_EVENT_GUID2);
 62MODULE_ALIAS("wmi:" WMI_EVENT_GUID3);
 63MODULE_ALIAS("wmi:" WMI_METHOD_WMAB);
 64MODULE_ALIAS("wmi:" WMI_METHOD_WMBB);
 
 65
 66static struct platform_device *pf_device;
 67static struct input_dev *wmi_input_dev;
 68
 69static u32 inited;
 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
 74
 75static int battery_limit_use_wmbb;
 76static struct led_classdev kbd_backlight;
 77static enum led_brightness get_kbd_backlight_level(void);
 78
 79static 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.
 86					  */
 87	{KE_KEY, 0x80, {KEY_RFKILL} },
 88	{KE_END, 0}
 89};
 90
 91static int ggov(u32 arg0)
 92{
 93	union acpi_object args[1];
 94	union acpi_object *r;
 95	acpi_status status;
 96	acpi_handle handle;
 97	struct acpi_object_list arg;
 98	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 99	int res;
100
101	args[0].type = ACPI_TYPE_INTEGER;
102	args[0].integer.value = arg0;
103
104	status = acpi_get_handle(NULL, (acpi_string) SB_GGOV_METHOD, &handle);
105	if (ACPI_FAILURE(status)) {
106		pr_err("Cannot get handle");
107		return -ENODEV;
108	}
109
110	arg.count = 1;
111	arg.pointer = args;
112
113	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
114	if (ACPI_FAILURE(status)) {
115		acpi_handle_err(handle, "GGOV: call failed.\n");
116		return -EINVAL;
117	}
118
119	r = buffer.pointer;
120	if (r->type != ACPI_TYPE_INTEGER) {
121		kfree(r);
122		return -EINVAL;
123	}
124
125	res = r->integer.value;
126	kfree(r);
127
128	return res;
129}
130
131static union acpi_object *lg_wmab(u32 method, u32 arg1, u32 arg2)
132{
133	union acpi_object args[3];
134	acpi_status status;
135	acpi_handle handle;
136	struct acpi_object_list arg;
137	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
138
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;
145
146	status = acpi_get_handle(NULL, (acpi_string) WMAB_METHOD, &handle);
147	if (ACPI_FAILURE(status)) {
148		pr_err("Cannot get handle");
149		return NULL;
150	}
151
152	arg.count = 3;
153	arg.pointer = args;
154
155	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
156	if (ACPI_FAILURE(status)) {
157		acpi_handle_err(handle, "WMAB: call failed.\n");
158		return NULL;
159	}
160
161	return buffer.pointer;
162}
163
164static union acpi_object *lg_wmbb(u32 method_id, u32 arg1, u32 arg2)
165{
166	union acpi_object args[3];
167	acpi_status status;
168	acpi_handle handle;
169	struct acpi_object_list arg;
170	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
171	u8 buf[32];
172
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;
183
184	status = acpi_get_handle(NULL, (acpi_string)WMBB_METHOD, &handle);
185	if (ACPI_FAILURE(status)) {
186		pr_err("Cannot get handle");
187		return NULL;
188	}
189
190	arg.count = 3;
191	arg.pointer = args;
192
193	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
194	if (ACPI_FAILURE(status)) {
195		acpi_handle_err(handle, "WMAB: call failed.\n");
196		return NULL;
197	}
198
199	return (union acpi_object *)buffer.pointer;
200}
201
202static void wmi_notify(u32 value, void *context)
203{
204	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
205	union acpi_object *obj;
206	acpi_status status;
207	long data = (long)context;
208
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);
213		return;
214	}
215
216	obj = (union acpi_object *)response.pointer;
217	if (!obj)
218		return;
219
220	if (obj->type == ACPI_TYPE_INTEGER) {
221		int eventcode = obj->integer.value;
222		struct key_entry *key;
223
224		if (eventcode == 0x10000000) {
225			led_classdev_notify_brightness_hw_changed(
226				&kbd_backlight, get_kbd_backlight_level());
227		} else {
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,
232							   key, 1, true);
233		}
234	}
235
236	pr_debug("Type: %i    Eventcode: 0x%llx\n", obj->type,
237		 obj->integer.value);
238	kfree(response.pointer);
239}
240
241static void wmi_input_setup(void)
242{
243	acpi_status status;
244
245	wmi_input_dev = input_allocate_device();
246	if (wmi_input_dev) {
247		wmi_input_dev->name = "LG WMI hotkeys";
248		wmi_input_dev->phys = "wmi/input0";
249		wmi_input_dev->id.bustype = BUS_HOST;
250
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);
255			return;
256		}
257
258		inited |= INIT_SPARSE_KEYMAP;
259		status = wmi_install_notify_handler(WMI_EVENT_GUID0, wmi_notify,
260						    (void *)0);
261		if (ACPI_SUCCESS(status))
262			inited |= INIT_INPUT_WMI_0;
263
264		status = wmi_install_notify_handler(WMI_EVENT_GUID2, wmi_notify,
265						    (void *)2);
266		if (ACPI_SUCCESS(status))
267			inited |= INIT_INPUT_WMI_2;
268	} else {
269		pr_info("Cannot allocate input device");
270	}
271}
272
273static void acpi_notify(struct acpi_device *device, u32 event)
274{
275	struct key_entry *key;
276
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);
282	}
283}
284
285static ssize_t fan_mode_store(struct device *dev,
286			      struct device_attribute *attr,
287			      const char *buffer, size_t count)
288{
289	bool value;
290	union acpi_object *r;
291	u32 m;
292	int ret;
293
294	ret = kstrtobool(buffer, &value);
295	if (ret)
296		return ret;
297
298	r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
299	if (!r)
300		return -EIO;
301
302	if (r->type != ACPI_TYPE_INTEGER) {
303		kfree(r);
304		return -EIO;
305	}
306
307	m = r->integer.value;
308	kfree(r);
309	r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xffffff0f) | (value << 4));
310	kfree(r);
311	r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xfffffff0) | value);
312	kfree(r);
313
314	return count;
315}
316
317static ssize_t fan_mode_show(struct device *dev,
318			     struct device_attribute *attr, char *buffer)
319{
320	unsigned int status;
321	union acpi_object *r;
322
323	r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
324	if (!r)
325		return -EIO;
326
327	if (r->type != ACPI_TYPE_INTEGER) {
328		kfree(r);
329		return -EIO;
330	}
331
332	status = r->integer.value & 0x01;
333	kfree(r);
334
335	return sysfs_emit(buffer, "%d\n", status);
336}
337
338static ssize_t usb_charge_store(struct device *dev,
339				struct device_attribute *attr,
340				const char *buffer, size_t count)
341{
342	bool value;
343	union acpi_object *r;
344	int ret;
345
346	ret = kstrtobool(buffer, &value);
347	if (ret)
348		return ret;
349
350	r = lg_wmbb(WMBB_USB_CHARGE, WM_SET, value);
351	if (!r)
352		return -EIO;
353
354	kfree(r);
355	return count;
356}
357
358static ssize_t usb_charge_show(struct device *dev,
359			       struct device_attribute *attr, char *buffer)
360{
361	unsigned int status;
362	union acpi_object *r;
363
364	r = lg_wmbb(WMBB_USB_CHARGE, WM_GET, 0);
365	if (!r)
366		return -EIO;
367
368	if (r->type != ACPI_TYPE_BUFFER) {
369		kfree(r);
370		return -EIO;
371	}
372
373	status = !!r->buffer.pointer[0x10];
374
375	kfree(r);
376
377	return sysfs_emit(buffer, "%d\n", status);
378}
379
380static ssize_t reader_mode_store(struct device *dev,
381				 struct device_attribute *attr,
382				 const char *buffer, size_t count)
383{
384	bool value;
385	union acpi_object *r;
386	int ret;
387
388	ret = kstrtobool(buffer, &value);
389	if (ret)
390		return ret;
391
392	r = lg_wmab(WM_READER_MODE, WM_SET, value);
393	if (!r)
394		return -EIO;
395
396	kfree(r);
397	return count;
398}
399
400static ssize_t reader_mode_show(struct device *dev,
401				struct device_attribute *attr, char *buffer)
402{
403	unsigned int status;
404	union acpi_object *r;
405
406	r = lg_wmab(WM_READER_MODE, WM_GET, 0);
407	if (!r)
408		return -EIO;
409
410	if (r->type != ACPI_TYPE_INTEGER) {
411		kfree(r);
412		return -EIO;
413	}
414
415	status = !!r->integer.value;
416
417	kfree(r);
418
419	return sysfs_emit(buffer, "%d\n", status);
420}
421
422static ssize_t fn_lock_store(struct device *dev,
423			     struct device_attribute *attr,
424			     const char *buffer, size_t count)
425{
426	bool value;
427	union acpi_object *r;
428	int ret;
429
430	ret = kstrtobool(buffer, &value);
431	if (ret)
432		return ret;
433
434	r = lg_wmab(WM_FN_LOCK, WM_SET, value);
435	if (!r)
436		return -EIO;
437
438	kfree(r);
439	return count;
440}
441
442static ssize_t fn_lock_show(struct device *dev,
443			    struct device_attribute *attr, char *buffer)
444{
445	unsigned int status;
446	union acpi_object *r;
447
448	r = lg_wmab(WM_FN_LOCK, WM_GET, 0);
449	if (!r)
450		return -EIO;
451
452	if (r->type != ACPI_TYPE_BUFFER) {
453		kfree(r);
454		return -EIO;
455	}
456
457	status = !!r->buffer.pointer[0];
458	kfree(r);
459
460	return sysfs_emit(buffer, "%d\n", status);
461}
462
463static ssize_t charge_control_end_threshold_store(struct device *dev,
464						  struct device_attribute *attr,
465						  const char *buf, size_t count)
466{
467	unsigned long value;
468	int ret;
469
470	ret = kstrtoul(buf, 10, &value);
471	if (ret)
472		return ret;
473
474	if (value == 100 || value == 80) {
475		union acpi_object *r;
476
477		if (battery_limit_use_wmbb)
478			r = lg_wmbb(WMBB_BATT_LIMIT, WM_SET, value);
479		else
480			r = lg_wmab(WM_BATT_LIMIT, WM_SET, value);
481		if (!r)
482			return -EIO;
483
484		kfree(r);
485		return count;
486	}
487
488	return -EINVAL;
489}
490
491static ssize_t charge_control_end_threshold_show(struct device *device,
492						 struct device_attribute *attr,
493						 char *buf)
494{
495	unsigned int status;
496	union acpi_object *r;
497
498	if (battery_limit_use_wmbb) {
499		r = lg_wmbb(WMBB_BATT_LIMIT, WM_GET, 0);
500		if (!r)
501			return -EIO;
502
503		if (r->type != ACPI_TYPE_BUFFER) {
504			kfree(r);
505			return -EIO;
506		}
507
508		status = r->buffer.pointer[0x10];
509	} else {
510		r = lg_wmab(WM_BATT_LIMIT, WM_GET, 0);
511		if (!r)
512			return -EIO;
513
514		if (r->type != ACPI_TYPE_INTEGER) {
515			kfree(r);
516			return -EIO;
517		}
518
519		status = r->integer.value;
 
 
520	}
 
 
521	kfree(r);
522	if (status != 80 && status != 100)
523		status = 0;
524
525	return sysfs_emit(buf, "%d\n", status);
526}
527
528static ssize_t battery_care_limit_show(struct device *dev,
529				       struct device_attribute *attr,
530				       char *buffer)
531{
532	return charge_control_end_threshold_show(dev, attr, buffer);
533}
534
535static ssize_t battery_care_limit_store(struct device *dev,
536					struct device_attribute *attr,
537					const char *buffer, size_t count)
538{
539	return charge_control_end_threshold_store(dev, attr, buffer, count);
540}
541
542static DEVICE_ATTR_RW(fan_mode);
543static DEVICE_ATTR_RW(usb_charge);
544static DEVICE_ATTR_RW(reader_mode);
545static DEVICE_ATTR_RW(fn_lock);
546static DEVICE_ATTR_RW(charge_control_end_threshold);
547static DEVICE_ATTR_RW(battery_care_limit);
548
549static int lg_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
550{
551	if (device_create_file(&battery->dev,
552			       &dev_attr_charge_control_end_threshold))
553		return -ENODEV;
554
555	return 0;
556}
557
558static int lg_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
559{
560	device_remove_file(&battery->dev,
561			   &dev_attr_charge_control_end_threshold);
562	return 0;
563}
564
565static struct acpi_battery_hook battery_hook = {
566	.add_battery = lg_battery_add,
567	.remove_battery = lg_battery_remove,
568	.name = "LG Battery Extension",
569};
570
571static 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,
577	NULL
578};
579
580static const struct attribute_group dev_attribute_group = {
581	.attrs = dev_attributes,
582};
583
584static void tpad_led_set(struct led_classdev *cdev,
585			 enum led_brightness brightness)
586{
587	union acpi_object *r;
588
589	r = lg_wmab(WM_TLED, WM_SET, brightness > LED_OFF);
590	kfree(r);
591}
592
593static enum led_brightness tpad_led_get(struct led_classdev *cdev)
594{
595	return ggov(GOV_TLED) > 0 ? LED_ON : LED_OFF;
596}
597
598static LED_DEVICE(tpad_led, 1, 0);
599
600static void kbd_backlight_set(struct led_classdev *cdev,
601			      enum led_brightness brightness)
602{
603	u32 val;
604	union acpi_object *r;
605
606	val = 0x22;
607	if (brightness <= LED_OFF)
608		val = 0;
609	if (brightness >= LED_FULL)
610		val = 0x24;
611	r = lg_wmab(WM_KEY_LIGHT, WM_SET, val);
612	kfree(r);
613}
614
615static enum led_brightness get_kbd_backlight_level(void)
616{
617	union acpi_object *r;
618	int val;
619
620	r = lg_wmab(WM_KEY_LIGHT, WM_GET, 0);
621
622	if (!r)
623		return LED_OFF;
624
625	if (r->type != ACPI_TYPE_BUFFER || r->buffer.pointer[1] != 0x05) {
626		kfree(r);
627		return LED_OFF;
628	}
629
630	switch (r->buffer.pointer[0] & 0x27) {
631	case 0x24:
632		val = LED_FULL;
633		break;
634	case 0x22:
635		val = LED_HALF;
636		break;
637	default:
638		val = LED_OFF;
639	}
640
641	kfree(r);
642
643	return val;
644}
645
646static enum led_brightness kbd_backlight_get(struct led_classdev *cdev)
647{
648	return get_kbd_backlight_level();
649}
650
651static LED_DEVICE(kbd_backlight, 255, LED_BRIGHT_HW_CHANGED);
652
653static void wmi_input_destroy(void)
654{
655	if (inited & INIT_INPUT_WMI_2)
656		wmi_remove_notify_handler(WMI_EVENT_GUID2);
657
658	if (inited & INIT_INPUT_WMI_0)
659		wmi_remove_notify_handler(WMI_EVENT_GUID0);
660
661	if (inited & INIT_SPARSE_KEYMAP)
662		input_unregister_device(wmi_input_dev);
663
664	inited &= ~(INIT_INPUT_WMI_0 | INIT_INPUT_WMI_2 | INIT_SPARSE_KEYMAP);
665}
666
667static struct platform_driver pf_driver = {
668	.driver = {
669		   .name = PLATFORM_NAME,
670	}
671};
672
673static int acpi_add(struct acpi_device *device)
674{
675	int ret;
676	const char *product;
677	int year = 2017;
678
679	if (pf_device)
680		return 0;
681
682	ret = platform_driver_register(&pf_driver);
683	if (ret)
684		return ret;
685
686	pf_device = platform_device_register_simple(PLATFORM_NAME,
687						    PLATFORM_DEVID_NONE,
688						    NULL, 0);
689	if (IS_ERR(pf_device)) {
690		ret = PTR_ERR(pf_device);
691		pf_device = NULL;
692		pr_err("unable to register platform device\n");
693		goto out_platform_registered;
694	}
695	product = dmi_get_system_info(DMI_PRODUCT_NAME);
696	if (product && strlen(product) > 4)
697		switch (product[4]) {
698		case '5':
699			if (strlen(product) > 5)
700				switch (product[5]) {
701				case 'N':
702					year = 2021;
703					break;
704				case '0':
705					year = 2016;
706					break;
707				default:
708					year = 2022;
709				}
710			break;
711		case '6':
712			year = 2016;
713			break;
714		case '7':
715			year = 2017;
716			break;
717		case '8':
718			year = 2018;
719			break;
720		case '9':
721			year = 2019;
722			break;
723		case '0':
724			if (strlen(product) > 5)
725				switch (product[5]) {
726				case 'N':
727					year = 2020;
728					break;
729				case 'P':
730					year = 2021;
731					break;
732				default:
733					year = 2022;
734				}
735			break;
736		default:
737			year = 2019;
738		}
739	pr_info("product: %s  year: %d\n", product, year);
740
741	if (year >= 2019)
742		battery_limit_use_wmbb = 1;
743
744	ret = sysfs_create_group(&pf_device->dev.kobj, &dev_attribute_group);
745	if (ret)
746		goto out_platform_device;
747
748	/* LEDs are optional */
749	led_classdev_register(&pf_device->dev, &kbd_backlight);
750	led_classdev_register(&pf_device->dev, &tpad_led);
 
 
751
752	wmi_input_setup();
753	battery_hook_register(&battery_hook);
754
755	return 0;
756
757out_platform_device:
758	platform_device_unregister(pf_device);
759out_platform_registered:
760	platform_driver_unregister(&pf_driver);
761	return ret;
762}
763
764static void acpi_remove(struct acpi_device *device)
765{
766	sysfs_remove_group(&pf_device->dev.kobj, &dev_attribute_group);
 
 
767
768	led_classdev_unregister(&tpad_led);
769	led_classdev_unregister(&kbd_backlight);
770
771	battery_hook_unregister(&battery_hook);
772	wmi_input_destroy();
773	platform_device_unregister(pf_device);
774	pf_device = NULL;
775	platform_driver_unregister(&pf_driver);
 
 
776}
777
778static const struct acpi_device_id device_ids[] = {
779	{"LGEX0815", 0},
780	{"", 0}
781};
782MODULE_DEVICE_TABLE(acpi, device_ids);
783
784static struct acpi_driver acpi_driver = {
785	.name = "LG Gram Laptop Support",
786	.class = "lg-laptop",
787	.ids = device_ids,
788	.ops = {
789		.add = acpi_add,
790		.remove = acpi_remove,
791		.notify = acpi_notify,
792		},
793	.owner = THIS_MODULE,
794};
795
796static int __init acpi_init(void)
797{
798	int result;
799
800	result = acpi_bus_register_driver(&acpi_driver);
801	if (result < 0) {
802		pr_debug("Error registering driver\n");
803		return -ENODEV;
804	}
805
806	return 0;
807}
808
809static void __exit acpi_exit(void)
810{
811	acpi_bus_unregister_driver(&acpi_driver);
812}
813
814module_init(acpi_init);
815module_exit(acpi_exit);
v5.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * lg-laptop.c - LG Gram ACPI features and hotkeys Driver
  4 *
  5 * Copyright (C) 2018 Matan Ziv-Av <matan@svgalib.org>
  6 */
  7
  8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9
 10#include <linux/acpi.h>
 
 11#include <linux/input.h>
 12#include <linux/input/sparse-keymap.h>
 13#include <linux/kernel.h>
 14#include <linux/leds.h>
 15#include <linux/module.h>
 16#include <linux/platform_device.h>
 17#include <linux/types.h>
 18
 19#define LED_DEVICE(_name, max) struct led_classdev _name = { \
 
 
 20	.name           = __stringify(_name),   \
 21	.max_brightness = max,                  \
 22	.brightness_set = _name##_set,          \
 23	.brightness_get = _name##_get,          \
 
 24}
 25
 26MODULE_AUTHOR("Matan Ziv-Av");
 27MODULE_DESCRIPTION("LG WMI Hotkey Driver");
 28MODULE_LICENSE("GPL");
 29
 30#define WMI_EVENT_GUID0	"E4FB94F9-7F2B-4173-AD1A-CD1D95086248"
 31#define WMI_EVENT_GUID1	"023B133E-49D1-4E10-B313-698220140DC2"
 32#define WMI_EVENT_GUID2	"37BE1AC0-C3F2-4B1F-BFBE-8FDEAF2814D6"
 33#define WMI_EVENT_GUID3	"911BAD44-7DF8-4FBB-9319-BABA1C4B293B"
 34#define WMI_METHOD_WMAB "C3A72B38-D3EF-42D3-8CBB-D5A57049F66D"
 35#define WMI_METHOD_WMBB "2B4F501A-BD3C-4394-8DCF-00A7D2BC8210"
 36#define WMI_EVENT_GUID  WMI_EVENT_GUID0
 37
 38#define WMAB_METHOD     "\\XINI.WMAB"
 39#define WMBB_METHOD     "\\XINI.WMBB"
 40#define SB_GGOV_METHOD  "\\_SB.GGOV"
 41#define GOV_TLED        0x2020008
 42#define WM_GET          1
 43#define WM_SET          2
 44#define WM_KEY_LIGHT    0x400
 45#define WM_TLED         0x404
 46#define WM_FN_LOCK      0x407
 47#define WM_BATT_LIMIT   0x61
 48#define WM_READER_MODE  0xBF
 49#define WM_FAN_MODE	0x33
 50#define WMBB_USB_CHARGE 0x10B
 51#define WMBB_BATT_LIMIT 0x10C
 52
 53#define PLATFORM_NAME   "lg-laptop"
 54
 55MODULE_ALIAS("wmi:" WMI_EVENT_GUID0);
 56MODULE_ALIAS("wmi:" WMI_EVENT_GUID1);
 57MODULE_ALIAS("wmi:" WMI_EVENT_GUID2);
 58MODULE_ALIAS("wmi:" WMI_EVENT_GUID3);
 59MODULE_ALIAS("wmi:" WMI_METHOD_WMAB);
 60MODULE_ALIAS("wmi:" WMI_METHOD_WMBB);
 61MODULE_ALIAS("acpi*:LGEX0815:*");
 62
 63static struct platform_device *pf_device;
 64static struct input_dev *wmi_input_dev;
 65
 66static u32 inited;
 67#define INIT_INPUT_WMI_0        0x01
 68#define INIT_INPUT_WMI_2        0x02
 69#define INIT_INPUT_ACPI         0x04
 70#define INIT_TPAD_LED           0x08
 71#define INIT_KBD_LED            0x10
 72#define INIT_SPARSE_KEYMAP        0x80
 
 
 73
 74static const struct key_entry wmi_keymap[] = {
 75	{KE_KEY, 0x70, {KEY_F15} },	 /* LG control panel (F1) */
 76	{KE_KEY, 0x74, {KEY_F13} },	 /* Touchpad toggle (F5) */
 77	{KE_KEY, 0xf020000, {KEY_F14} }, /* Read mode (F9) */
 78	{KE_KEY, 0x10000000, {KEY_F16} },/* Keyboard backlight (F8) - pressing
 79					  * this key both sends an event and
 80					  * changes backlight level.
 81					  */
 82	{KE_KEY, 0x80, {KEY_RFKILL} },
 83	{KE_END, 0}
 84};
 85
 86static int ggov(u32 arg0)
 87{
 88	union acpi_object args[1];
 89	union acpi_object *r;
 90	acpi_status status;
 91	acpi_handle handle;
 92	struct acpi_object_list arg;
 93	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 94	int res;
 95
 96	args[0].type = ACPI_TYPE_INTEGER;
 97	args[0].integer.value = arg0;
 98
 99	status = acpi_get_handle(NULL, (acpi_string) SB_GGOV_METHOD, &handle);
100	if (ACPI_FAILURE(status)) {
101		pr_err("Cannot get handle");
102		return -ENODEV;
103	}
104
105	arg.count = 1;
106	arg.pointer = args;
107
108	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
109	if (ACPI_FAILURE(status)) {
110		acpi_handle_err(handle, "GGOV: call failed.\n");
111		return -EINVAL;
112	}
113
114	r = buffer.pointer;
115	if (r->type != ACPI_TYPE_INTEGER) {
116		kfree(r);
117		return -EINVAL;
118	}
119
120	res = r->integer.value;
121	kfree(r);
122
123	return res;
124}
125
126static union acpi_object *lg_wmab(u32 method, u32 arg1, u32 arg2)
127{
128	union acpi_object args[3];
129	acpi_status status;
130	acpi_handle handle;
131	struct acpi_object_list arg;
132	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
133
134	args[0].type = ACPI_TYPE_INTEGER;
135	args[0].integer.value = method;
136	args[1].type = ACPI_TYPE_INTEGER;
137	args[1].integer.value = arg1;
138	args[2].type = ACPI_TYPE_INTEGER;
139	args[2].integer.value = arg2;
140
141	status = acpi_get_handle(NULL, (acpi_string) WMAB_METHOD, &handle);
142	if (ACPI_FAILURE(status)) {
143		pr_err("Cannot get handle");
144		return NULL;
145	}
146
147	arg.count = 3;
148	arg.pointer = args;
149
150	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
151	if (ACPI_FAILURE(status)) {
152		acpi_handle_err(handle, "WMAB: call failed.\n");
153		return NULL;
154	}
155
156	return buffer.pointer;
157}
158
159static union acpi_object *lg_wmbb(u32 method_id, u32 arg1, u32 arg2)
160{
161	union acpi_object args[3];
162	acpi_status status;
163	acpi_handle handle;
164	struct acpi_object_list arg;
165	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
166	u8 buf[32];
167
168	*(u32 *)buf = method_id;
169	*(u32 *)(buf + 4) = arg1;
170	*(u32 *)(buf + 16) = arg2;
171	args[0].type = ACPI_TYPE_INTEGER;
172	args[0].integer.value = 0; /* ignored */
173	args[1].type = ACPI_TYPE_INTEGER;
174	args[1].integer.value = 1; /* Must be 1 or 2. Does not matter which */
175	args[2].type = ACPI_TYPE_BUFFER;
176	args[2].buffer.length = 32;
177	args[2].buffer.pointer = buf;
178
179	status = acpi_get_handle(NULL, (acpi_string)WMBB_METHOD, &handle);
180	if (ACPI_FAILURE(status)) {
181		pr_err("Cannot get handle");
182		return NULL;
183	}
184
185	arg.count = 3;
186	arg.pointer = args;
187
188	status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
189	if (ACPI_FAILURE(status)) {
190		acpi_handle_err(handle, "WMAB: call failed.\n");
191		return NULL;
192	}
193
194	return (union acpi_object *)buffer.pointer;
195}
196
197static void wmi_notify(u32 value, void *context)
198{
199	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
200	union acpi_object *obj;
201	acpi_status status;
202	long data = (long)context;
203
204	pr_debug("event guid %li\n", data);
205	status = wmi_get_event_data(value, &response);
206	if (ACPI_FAILURE(status)) {
207		pr_err("Bad event status 0x%x\n", status);
208		return;
209	}
210
211	obj = (union acpi_object *)response.pointer;
212	if (!obj)
213		return;
214
215	if (obj->type == ACPI_TYPE_INTEGER) {
216		int eventcode = obj->integer.value;
217		struct key_entry *key;
218
219		key =
220		    sparse_keymap_entry_from_scancode(wmi_input_dev, eventcode);
221		if (key && key->type == KE_KEY)
222			sparse_keymap_report_entry(wmi_input_dev, key, 1, true);
 
 
 
 
 
 
223	}
224
225	pr_debug("Type: %i    Eventcode: 0x%llx\n", obj->type,
226		 obj->integer.value);
227	kfree(response.pointer);
228}
229
230static void wmi_input_setup(void)
231{
232	acpi_status status;
233
234	wmi_input_dev = input_allocate_device();
235	if (wmi_input_dev) {
236		wmi_input_dev->name = "LG WMI hotkeys";
237		wmi_input_dev->phys = "wmi/input0";
238		wmi_input_dev->id.bustype = BUS_HOST;
239
240		if (sparse_keymap_setup(wmi_input_dev, wmi_keymap, NULL) ||
241		    input_register_device(wmi_input_dev)) {
242			pr_info("Cannot initialize input device");
243			input_free_device(wmi_input_dev);
244			return;
245		}
246
247		inited |= INIT_SPARSE_KEYMAP;
248		status = wmi_install_notify_handler(WMI_EVENT_GUID0, wmi_notify,
249						    (void *)0);
250		if (ACPI_SUCCESS(status))
251			inited |= INIT_INPUT_WMI_0;
252
253		status = wmi_install_notify_handler(WMI_EVENT_GUID2, wmi_notify,
254						    (void *)2);
255		if (ACPI_SUCCESS(status))
256			inited |= INIT_INPUT_WMI_2;
257	} else {
258		pr_info("Cannot allocate input device");
259	}
260}
261
262static void acpi_notify(struct acpi_device *device, u32 event)
263{
264	struct key_entry *key;
265
266	acpi_handle_debug(device->handle, "notify: %d\n", event);
267	if (inited & INIT_SPARSE_KEYMAP) {
268		key = sparse_keymap_entry_from_scancode(wmi_input_dev, 0x80);
269		if (key && key->type == KE_KEY)
270			sparse_keymap_report_entry(wmi_input_dev, key, 1, true);
271	}
272}
273
274static ssize_t fan_mode_store(struct device *dev,
275			      struct device_attribute *attr,
276			      const char *buffer, size_t count)
277{
278	bool value;
279	union acpi_object *r;
280	u32 m;
281	int ret;
282
283	ret = kstrtobool(buffer, &value);
284	if (ret)
285		return ret;
286
287	r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
288	if (!r)
289		return -EIO;
290
291	if (r->type != ACPI_TYPE_INTEGER) {
292		kfree(r);
293		return -EIO;
294	}
295
296	m = r->integer.value;
297	kfree(r);
298	r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xffffff0f) | (value << 4));
299	kfree(r);
300	r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xfffffff0) | value);
301	kfree(r);
302
303	return count;
304}
305
306static ssize_t fan_mode_show(struct device *dev,
307			     struct device_attribute *attr, char *buffer)
308{
309	unsigned int status;
310	union acpi_object *r;
311
312	r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
313	if (!r)
314		return -EIO;
315
316	if (r->type != ACPI_TYPE_INTEGER) {
317		kfree(r);
318		return -EIO;
319	}
320
321	status = r->integer.value & 0x01;
322	kfree(r);
323
324	return snprintf(buffer, PAGE_SIZE, "%d\n", status);
325}
326
327static ssize_t usb_charge_store(struct device *dev,
328				struct device_attribute *attr,
329				const char *buffer, size_t count)
330{
331	bool value;
332	union acpi_object *r;
333	int ret;
334
335	ret = kstrtobool(buffer, &value);
336	if (ret)
337		return ret;
338
339	r = lg_wmbb(WMBB_USB_CHARGE, WM_SET, value);
340	if (!r)
341		return -EIO;
342
343	kfree(r);
344	return count;
345}
346
347static ssize_t usb_charge_show(struct device *dev,
348			       struct device_attribute *attr, char *buffer)
349{
350	unsigned int status;
351	union acpi_object *r;
352
353	r = lg_wmbb(WMBB_USB_CHARGE, WM_GET, 0);
354	if (!r)
355		return -EIO;
356
357	if (r->type != ACPI_TYPE_BUFFER) {
358		kfree(r);
359		return -EIO;
360	}
361
362	status = !!r->buffer.pointer[0x10];
363
364	kfree(r);
365
366	return snprintf(buffer, PAGE_SIZE, "%d\n", status);
367}
368
369static ssize_t reader_mode_store(struct device *dev,
370				 struct device_attribute *attr,
371				 const char *buffer, size_t count)
372{
373	bool value;
374	union acpi_object *r;
375	int ret;
376
377	ret = kstrtobool(buffer, &value);
378	if (ret)
379		return ret;
380
381	r = lg_wmab(WM_READER_MODE, WM_SET, value);
382	if (!r)
383		return -EIO;
384
385	kfree(r);
386	return count;
387}
388
389static ssize_t reader_mode_show(struct device *dev,
390				struct device_attribute *attr, char *buffer)
391{
392	unsigned int status;
393	union acpi_object *r;
394
395	r = lg_wmab(WM_READER_MODE, WM_GET, 0);
396	if (!r)
397		return -EIO;
398
399	if (r->type != ACPI_TYPE_INTEGER) {
400		kfree(r);
401		return -EIO;
402	}
403
404	status = !!r->integer.value;
405
406	kfree(r);
407
408	return snprintf(buffer, PAGE_SIZE, "%d\n", status);
409}
410
411static ssize_t fn_lock_store(struct device *dev,
412			     struct device_attribute *attr,
413			     const char *buffer, size_t count)
414{
415	bool value;
416	union acpi_object *r;
417	int ret;
418
419	ret = kstrtobool(buffer, &value);
420	if (ret)
421		return ret;
422
423	r = lg_wmab(WM_FN_LOCK, WM_SET, value);
424	if (!r)
425		return -EIO;
426
427	kfree(r);
428	return count;
429}
430
431static ssize_t fn_lock_show(struct device *dev,
432			    struct device_attribute *attr, char *buffer)
433{
434	unsigned int status;
435	union acpi_object *r;
436
437	r = lg_wmab(WM_FN_LOCK, WM_GET, 0);
438	if (!r)
439		return -EIO;
440
441	if (r->type != ACPI_TYPE_BUFFER) {
442		kfree(r);
443		return -EIO;
444	}
445
446	status = !!r->buffer.pointer[0];
447	kfree(r);
448
449	return snprintf(buffer, PAGE_SIZE, "%d\n", status);
450}
451
452static ssize_t battery_care_limit_store(struct device *dev,
453					struct device_attribute *attr,
454					const char *buffer, size_t count)
455{
456	unsigned long value;
457	int ret;
458
459	ret = kstrtoul(buffer, 10, &value);
460	if (ret)
461		return ret;
462
463	if (value == 100 || value == 80) {
464		union acpi_object *r;
465
466		r = lg_wmab(WM_BATT_LIMIT, WM_SET, value);
 
 
 
467		if (!r)
468			return -EIO;
469
470		kfree(r);
471		return count;
472	}
473
474	return -EINVAL;
475}
476
477static ssize_t battery_care_limit_show(struct device *dev,
478				       struct device_attribute *attr,
479				       char *buffer)
480{
481	unsigned int status;
482	union acpi_object *r;
483
484	r = lg_wmab(WM_BATT_LIMIT, WM_GET, 0);
485	if (!r)
486		return -EIO;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487
488	if (r->type != ACPI_TYPE_INTEGER) {
489		kfree(r);
490		return -EIO;
491	}
492
493	status = r->integer.value;
494	kfree(r);
495	if (status != 80 && status != 100)
496		status = 0;
497
498	return snprintf(buffer, PAGE_SIZE, "%d\n", status);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499}
500
501static DEVICE_ATTR_RW(fan_mode);
502static DEVICE_ATTR_RW(usb_charge);
503static DEVICE_ATTR_RW(reader_mode);
504static DEVICE_ATTR_RW(fn_lock);
 
505static DEVICE_ATTR_RW(battery_care_limit);
506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507static struct attribute *dev_attributes[] = {
508	&dev_attr_fan_mode.attr,
509	&dev_attr_usb_charge.attr,
510	&dev_attr_reader_mode.attr,
511	&dev_attr_fn_lock.attr,
512	&dev_attr_battery_care_limit.attr,
513	NULL
514};
515
516static const struct attribute_group dev_attribute_group = {
517	.attrs = dev_attributes,
518};
519
520static void tpad_led_set(struct led_classdev *cdev,
521			 enum led_brightness brightness)
522{
523	union acpi_object *r;
524
525	r = lg_wmab(WM_TLED, WM_SET, brightness > LED_OFF);
526	kfree(r);
527}
528
529static enum led_brightness tpad_led_get(struct led_classdev *cdev)
530{
531	return ggov(GOV_TLED) > 0 ? LED_ON : LED_OFF;
532}
533
534static LED_DEVICE(tpad_led, 1);
535
536static void kbd_backlight_set(struct led_classdev *cdev,
537			      enum led_brightness brightness)
538{
539	u32 val;
540	union acpi_object *r;
541
542	val = 0x22;
543	if (brightness <= LED_OFF)
544		val = 0;
545	if (brightness >= LED_FULL)
546		val = 0x24;
547	r = lg_wmab(WM_KEY_LIGHT, WM_SET, val);
548	kfree(r);
549}
550
551static enum led_brightness kbd_backlight_get(struct led_classdev *cdev)
552{
553	union acpi_object *r;
554	int val;
555
556	r = lg_wmab(WM_KEY_LIGHT, WM_GET, 0);
557
558	if (!r)
559		return LED_OFF;
560
561	if (r->type != ACPI_TYPE_BUFFER || r->buffer.pointer[1] != 0x05) {
562		kfree(r);
563		return LED_OFF;
564	}
565
566	switch (r->buffer.pointer[0] & 0x27) {
567	case 0x24:
568		val = LED_FULL;
569		break;
570	case 0x22:
571		val = LED_HALF;
572		break;
573	default:
574		val = LED_OFF;
575	}
576
577	kfree(r);
578
579	return val;
580}
581
582static LED_DEVICE(kbd_backlight, 255);
 
 
 
 
 
583
584static void wmi_input_destroy(void)
585{
586	if (inited & INIT_INPUT_WMI_2)
587		wmi_remove_notify_handler(WMI_EVENT_GUID2);
588
589	if (inited & INIT_INPUT_WMI_0)
590		wmi_remove_notify_handler(WMI_EVENT_GUID0);
591
592	if (inited & INIT_SPARSE_KEYMAP)
593		input_unregister_device(wmi_input_dev);
594
595	inited &= ~(INIT_INPUT_WMI_0 | INIT_INPUT_WMI_2 | INIT_SPARSE_KEYMAP);
596}
597
598static struct platform_driver pf_driver = {
599	.driver = {
600		   .name = PLATFORM_NAME,
601	}
602};
603
604static int acpi_add(struct acpi_device *device)
605{
606	int ret;
 
 
607
608	if (pf_device)
609		return 0;
610
611	ret = platform_driver_register(&pf_driver);
612	if (ret)
613		return ret;
614
615	pf_device = platform_device_register_simple(PLATFORM_NAME,
616						    PLATFORM_DEVID_NONE,
617						    NULL, 0);
618	if (IS_ERR(pf_device)) {
619		ret = PTR_ERR(pf_device);
620		pf_device = NULL;
621		pr_err("unable to register platform device\n");
622		goto out_platform_registered;
623	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
625	ret = sysfs_create_group(&pf_device->dev.kobj, &dev_attribute_group);
626	if (ret)
627		goto out_platform_device;
628
629	if (!led_classdev_register(&pf_device->dev, &kbd_backlight))
630		inited |= INIT_KBD_LED;
631
632	if (!led_classdev_register(&pf_device->dev, &tpad_led))
633		inited |= INIT_TPAD_LED;
634
635	wmi_input_setup();
 
636
637	return 0;
638
639out_platform_device:
640	platform_device_unregister(pf_device);
641out_platform_registered:
642	platform_driver_unregister(&pf_driver);
643	return ret;
644}
645
646static int acpi_remove(struct acpi_device *device)
647{
648	sysfs_remove_group(&pf_device->dev.kobj, &dev_attribute_group);
649	if (inited & INIT_KBD_LED)
650		led_classdev_unregister(&kbd_backlight);
651
652	if (inited & INIT_TPAD_LED)
653		led_classdev_unregister(&tpad_led);
654
 
655	wmi_input_destroy();
656	platform_device_unregister(pf_device);
657	pf_device = NULL;
658	platform_driver_unregister(&pf_driver);
659
660	return 0;
661}
662
663static const struct acpi_device_id device_ids[] = {
664	{"LGEX0815", 0},
665	{"", 0}
666};
667MODULE_DEVICE_TABLE(acpi, device_ids);
668
669static struct acpi_driver acpi_driver = {
670	.name = "LG Gram Laptop Support",
671	.class = "lg-laptop",
672	.ids = device_ids,
673	.ops = {
674		.add = acpi_add,
675		.remove = acpi_remove,
676		.notify = acpi_notify,
677		},
678	.owner = THIS_MODULE,
679};
680
681static int __init acpi_init(void)
682{
683	int result;
684
685	result = acpi_bus_register_driver(&acpi_driver);
686	if (result < 0) {
687		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error registering driver\n"));
688		return -ENODEV;
689	}
690
691	return 0;
692}
693
694static void __exit acpi_exit(void)
695{
696	acpi_bus_unregister_driver(&acpi_driver);
697}
698
699module_init(acpi_init);
700module_exit(acpi_exit);