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