lg-laptop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/acpi.h>
  9. #include <linux/input.h>
  10. #include <linux/input/sparse-keymap.h>
  11. #include <linux/kernel.h>
  12. #include <linux/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/types.h>
  16. #define LED_DEVICE(_name, max) struct led_classdev _name = { \
  17. .name = __stringify(_name), \
  18. .max_brightness = max, \
  19. .brightness_set = _name##_set, \
  20. .brightness_get = _name##_get, \
  21. }
  22. MODULE_AUTHOR("Matan Ziv-Av");
  23. MODULE_DESCRIPTION("LG WMI Hotkey Driver");
  24. MODULE_LICENSE("GPL");
  25. #define WMI_EVENT_GUID0 "E4FB94F9-7F2B-4173-AD1A-CD1D95086248"
  26. #define WMI_EVENT_GUID1 "023B133E-49D1-4E10-B313-698220140DC2"
  27. #define WMI_EVENT_GUID2 "37BE1AC0-C3F2-4B1F-BFBE-8FDEAF2814D6"
  28. #define WMI_EVENT_GUID3 "911BAD44-7DF8-4FBB-9319-BABA1C4B293B"
  29. #define WMI_METHOD_WMAB "C3A72B38-D3EF-42D3-8CBB-D5A57049F66D"
  30. #define WMI_METHOD_WMBB "2B4F501A-BD3C-4394-8DCF-00A7D2BC8210"
  31. #define WMI_EVENT_GUID WMI_EVENT_GUID0
  32. #define WMAB_METHOD "\\XINI.WMAB"
  33. #define WMBB_METHOD "\\XINI.WMBB"
  34. #define SB_GGOV_METHOD "\\_SB.GGOV"
  35. #define GOV_TLED 0x2020008
  36. #define WM_GET 1
  37. #define WM_SET 2
  38. #define WM_KEY_LIGHT 0x400
  39. #define WM_TLED 0x404
  40. #define WM_FN_LOCK 0x407
  41. #define WM_BATT_LIMIT 0x61
  42. #define WM_READER_MODE 0xBF
  43. #define WM_FAN_MODE 0x33
  44. #define WMBB_USB_CHARGE 0x10B
  45. #define WMBB_BATT_LIMIT 0x10C
  46. #define PLATFORM_NAME "lg-laptop"
  47. MODULE_ALIAS("wmi:" WMI_EVENT_GUID0);
  48. MODULE_ALIAS("wmi:" WMI_EVENT_GUID1);
  49. MODULE_ALIAS("wmi:" WMI_EVENT_GUID2);
  50. MODULE_ALIAS("wmi:" WMI_EVENT_GUID3);
  51. MODULE_ALIAS("wmi:" WMI_METHOD_WMAB);
  52. MODULE_ALIAS("wmi:" WMI_METHOD_WMBB);
  53. MODULE_ALIAS("acpi*:LGEX0815:*");
  54. static struct platform_device *pf_device;
  55. static struct input_dev *wmi_input_dev;
  56. static u32 inited;
  57. #define INIT_INPUT_WMI_0 0x01
  58. #define INIT_INPUT_WMI_2 0x02
  59. #define INIT_INPUT_ACPI 0x04
  60. #define INIT_SPARSE_KEYMAP 0x80
  61. static const struct key_entry wmi_keymap[] = {
  62. {KE_KEY, 0x70, {KEY_F15} }, /* LG control panel (F1) */
  63. {KE_KEY, 0x74, {KEY_F13} }, /* Touchpad toggle (F5) */
  64. {KE_KEY, 0xf020000, {KEY_F14} }, /* Read mode (F9) */
  65. {KE_KEY, 0x10000000, {KEY_F16} },/* Keyboard backlight (F8) - pressing
  66. * this key both sends an event and
  67. * changes backlight level.
  68. */
  69. {KE_KEY, 0x80, {KEY_RFKILL} },
  70. {KE_END, 0}
  71. };
  72. static int ggov(u32 arg0)
  73. {
  74. union acpi_object args[1];
  75. union acpi_object *r;
  76. acpi_status status;
  77. acpi_handle handle;
  78. struct acpi_object_list arg;
  79. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  80. int res;
  81. args[0].type = ACPI_TYPE_INTEGER;
  82. args[0].integer.value = arg0;
  83. status = acpi_get_handle(NULL, (acpi_string) SB_GGOV_METHOD, &handle);
  84. if (ACPI_FAILURE(status)) {
  85. pr_err("Cannot get handle");
  86. return -ENODEV;
  87. }
  88. arg.count = 1;
  89. arg.pointer = args;
  90. status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
  91. if (ACPI_FAILURE(status)) {
  92. acpi_handle_err(handle, "GGOV: call failed.\n");
  93. return -EINVAL;
  94. }
  95. r = buffer.pointer;
  96. if (r->type != ACPI_TYPE_INTEGER) {
  97. kfree(r);
  98. return -EINVAL;
  99. }
  100. res = r->integer.value;
  101. kfree(r);
  102. return res;
  103. }
  104. static union acpi_object *lg_wmab(u32 method, u32 arg1, u32 arg2)
  105. {
  106. union acpi_object args[3];
  107. acpi_status status;
  108. acpi_handle handle;
  109. struct acpi_object_list arg;
  110. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  111. args[0].type = ACPI_TYPE_INTEGER;
  112. args[0].integer.value = method;
  113. args[1].type = ACPI_TYPE_INTEGER;
  114. args[1].integer.value = arg1;
  115. args[2].type = ACPI_TYPE_INTEGER;
  116. args[2].integer.value = arg2;
  117. status = acpi_get_handle(NULL, (acpi_string) WMAB_METHOD, &handle);
  118. if (ACPI_FAILURE(status)) {
  119. pr_err("Cannot get handle");
  120. return NULL;
  121. }
  122. arg.count = 3;
  123. arg.pointer = args;
  124. status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
  125. if (ACPI_FAILURE(status)) {
  126. acpi_handle_err(handle, "WMAB: call failed.\n");
  127. return NULL;
  128. }
  129. return buffer.pointer;
  130. }
  131. static union acpi_object *lg_wmbb(u32 method_id, 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. u8 buf[32];
  139. *(u32 *)buf = method_id;
  140. *(u32 *)(buf + 4) = arg1;
  141. *(u32 *)(buf + 16) = arg2;
  142. args[0].type = ACPI_TYPE_INTEGER;
  143. args[0].integer.value = 0; /* ignored */
  144. args[1].type = ACPI_TYPE_INTEGER;
  145. args[1].integer.value = 1; /* Must be 1 or 2. Does not matter which */
  146. args[2].type = ACPI_TYPE_BUFFER;
  147. args[2].buffer.length = 32;
  148. args[2].buffer.pointer = buf;
  149. status = acpi_get_handle(NULL, (acpi_string)WMBB_METHOD, &handle);
  150. if (ACPI_FAILURE(status)) {
  151. pr_err("Cannot get handle");
  152. return NULL;
  153. }
  154. arg.count = 3;
  155. arg.pointer = args;
  156. status = acpi_evaluate_object(handle, NULL, &arg, &buffer);
  157. if (ACPI_FAILURE(status)) {
  158. acpi_handle_err(handle, "WMAB: call failed.\n");
  159. return NULL;
  160. }
  161. return (union acpi_object *)buffer.pointer;
  162. }
  163. static void wmi_notify(u32 value, void *context)
  164. {
  165. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  166. union acpi_object *obj;
  167. acpi_status status;
  168. long data = (long)context;
  169. pr_debug("event guid %li\n", data);
  170. status = wmi_get_event_data(value, &response);
  171. if (ACPI_FAILURE(status)) {
  172. pr_err("Bad event status 0x%x\n", status);
  173. return;
  174. }
  175. obj = (union acpi_object *)response.pointer;
  176. if (!obj)
  177. return;
  178. if (obj->type == ACPI_TYPE_INTEGER) {
  179. int eventcode = obj->integer.value;
  180. struct key_entry *key;
  181. key =
  182. sparse_keymap_entry_from_scancode(wmi_input_dev, eventcode);
  183. if (key && key->type == KE_KEY)
  184. sparse_keymap_report_entry(wmi_input_dev, key, 1, true);
  185. }
  186. pr_debug("Type: %i Eventcode: 0x%llx\n", obj->type,
  187. obj->integer.value);
  188. kfree(response.pointer);
  189. }
  190. static void wmi_input_setup(void)
  191. {
  192. acpi_status status;
  193. wmi_input_dev = input_allocate_device();
  194. if (wmi_input_dev) {
  195. wmi_input_dev->name = "LG WMI hotkeys";
  196. wmi_input_dev->phys = "wmi/input0";
  197. wmi_input_dev->id.bustype = BUS_HOST;
  198. if (sparse_keymap_setup(wmi_input_dev, wmi_keymap, NULL) ||
  199. input_register_device(wmi_input_dev)) {
  200. pr_info("Cannot initialize input device");
  201. input_free_device(wmi_input_dev);
  202. return;
  203. }
  204. inited |= INIT_SPARSE_KEYMAP;
  205. status = wmi_install_notify_handler(WMI_EVENT_GUID0, wmi_notify,
  206. (void *)0);
  207. if (ACPI_SUCCESS(status))
  208. inited |= INIT_INPUT_WMI_0;
  209. status = wmi_install_notify_handler(WMI_EVENT_GUID2, wmi_notify,
  210. (void *)2);
  211. if (ACPI_SUCCESS(status))
  212. inited |= INIT_INPUT_WMI_2;
  213. } else {
  214. pr_info("Cannot allocate input device");
  215. }
  216. }
  217. static void acpi_notify(struct acpi_device *device, u32 event)
  218. {
  219. struct key_entry *key;
  220. acpi_handle_debug(device->handle, "notify: %d\n", event);
  221. if (inited & INIT_SPARSE_KEYMAP) {
  222. key = sparse_keymap_entry_from_scancode(wmi_input_dev, 0x80);
  223. if (key && key->type == KE_KEY)
  224. sparse_keymap_report_entry(wmi_input_dev, key, 1, true);
  225. }
  226. }
  227. static ssize_t fan_mode_store(struct device *dev,
  228. struct device_attribute *attr,
  229. const char *buffer, size_t count)
  230. {
  231. bool value;
  232. union acpi_object *r;
  233. u32 m;
  234. int ret;
  235. ret = kstrtobool(buffer, &value);
  236. if (ret)
  237. return ret;
  238. r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
  239. if (!r)
  240. return -EIO;
  241. if (r->type != ACPI_TYPE_INTEGER) {
  242. kfree(r);
  243. return -EIO;
  244. }
  245. m = r->integer.value;
  246. kfree(r);
  247. r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xffffff0f) | (value << 4));
  248. kfree(r);
  249. r = lg_wmab(WM_FAN_MODE, WM_SET, (m & 0xfffffff0) | value);
  250. kfree(r);
  251. return count;
  252. }
  253. static ssize_t fan_mode_show(struct device *dev,
  254. struct device_attribute *attr, char *buffer)
  255. {
  256. unsigned int status;
  257. union acpi_object *r;
  258. r = lg_wmab(WM_FAN_MODE, WM_GET, 0);
  259. if (!r)
  260. return -EIO;
  261. if (r->type != ACPI_TYPE_INTEGER) {
  262. kfree(r);
  263. return -EIO;
  264. }
  265. status = r->integer.value & 0x01;
  266. kfree(r);
  267. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  268. }
  269. static ssize_t usb_charge_store(struct device *dev,
  270. struct device_attribute *attr,
  271. const char *buffer, size_t count)
  272. {
  273. bool value;
  274. union acpi_object *r;
  275. int ret;
  276. ret = kstrtobool(buffer, &value);
  277. if (ret)
  278. return ret;
  279. r = lg_wmbb(WMBB_USB_CHARGE, WM_SET, value);
  280. if (!r)
  281. return -EIO;
  282. kfree(r);
  283. return count;
  284. }
  285. static ssize_t usb_charge_show(struct device *dev,
  286. struct device_attribute *attr, char *buffer)
  287. {
  288. unsigned int status;
  289. union acpi_object *r;
  290. r = lg_wmbb(WMBB_USB_CHARGE, WM_GET, 0);
  291. if (!r)
  292. return -EIO;
  293. if (r->type != ACPI_TYPE_BUFFER) {
  294. kfree(r);
  295. return -EIO;
  296. }
  297. status = !!r->buffer.pointer[0x10];
  298. kfree(r);
  299. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  300. }
  301. static ssize_t reader_mode_store(struct device *dev,
  302. struct device_attribute *attr,
  303. const char *buffer, size_t count)
  304. {
  305. bool value;
  306. union acpi_object *r;
  307. int ret;
  308. ret = kstrtobool(buffer, &value);
  309. if (ret)
  310. return ret;
  311. r = lg_wmab(WM_READER_MODE, WM_SET, value);
  312. if (!r)
  313. return -EIO;
  314. kfree(r);
  315. return count;
  316. }
  317. static ssize_t reader_mode_show(struct device *dev,
  318. struct device_attribute *attr, char *buffer)
  319. {
  320. unsigned int status;
  321. union acpi_object *r;
  322. r = lg_wmab(WM_READER_MODE, WM_GET, 0);
  323. if (!r)
  324. return -EIO;
  325. if (r->type != ACPI_TYPE_INTEGER) {
  326. kfree(r);
  327. return -EIO;
  328. }
  329. status = !!r->integer.value;
  330. kfree(r);
  331. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  332. }
  333. static ssize_t fn_lock_store(struct device *dev,
  334. struct device_attribute *attr,
  335. const char *buffer, size_t count)
  336. {
  337. bool value;
  338. union acpi_object *r;
  339. int ret;
  340. ret = kstrtobool(buffer, &value);
  341. if (ret)
  342. return ret;
  343. r = lg_wmab(WM_FN_LOCK, WM_SET, value);
  344. if (!r)
  345. return -EIO;
  346. kfree(r);
  347. return count;
  348. }
  349. static ssize_t fn_lock_show(struct device *dev,
  350. struct device_attribute *attr, char *buffer)
  351. {
  352. unsigned int status;
  353. union acpi_object *r;
  354. r = lg_wmab(WM_FN_LOCK, WM_GET, 0);
  355. if (!r)
  356. return -EIO;
  357. if (r->type != ACPI_TYPE_BUFFER) {
  358. kfree(r);
  359. return -EIO;
  360. }
  361. status = !!r->buffer.pointer[0];
  362. kfree(r);
  363. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  364. }
  365. static ssize_t battery_care_limit_store(struct device *dev,
  366. struct device_attribute *attr,
  367. const char *buffer, size_t count)
  368. {
  369. unsigned long value;
  370. int ret;
  371. ret = kstrtoul(buffer, 10, &value);
  372. if (ret)
  373. return ret;
  374. if (value == 100 || value == 80) {
  375. union acpi_object *r;
  376. r = lg_wmab(WM_BATT_LIMIT, WM_SET, value);
  377. if (!r)
  378. return -EIO;
  379. kfree(r);
  380. return count;
  381. }
  382. return -EINVAL;
  383. }
  384. static ssize_t battery_care_limit_show(struct device *dev,
  385. struct device_attribute *attr,
  386. char *buffer)
  387. {
  388. unsigned int status;
  389. union acpi_object *r;
  390. r = lg_wmab(WM_BATT_LIMIT, WM_GET, 0);
  391. if (!r)
  392. return -EIO;
  393. if (r->type != ACPI_TYPE_INTEGER) {
  394. kfree(r);
  395. return -EIO;
  396. }
  397. status = r->integer.value;
  398. kfree(r);
  399. if (status != 80 && status != 100)
  400. status = 0;
  401. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  402. }
  403. static DEVICE_ATTR_RW(fan_mode);
  404. static DEVICE_ATTR_RW(usb_charge);
  405. static DEVICE_ATTR_RW(reader_mode);
  406. static DEVICE_ATTR_RW(fn_lock);
  407. static DEVICE_ATTR_RW(battery_care_limit);
  408. static struct attribute *dev_attributes[] = {
  409. &dev_attr_fan_mode.attr,
  410. &dev_attr_usb_charge.attr,
  411. &dev_attr_reader_mode.attr,
  412. &dev_attr_fn_lock.attr,
  413. &dev_attr_battery_care_limit.attr,
  414. NULL
  415. };
  416. static const struct attribute_group dev_attribute_group = {
  417. .attrs = dev_attributes,
  418. };
  419. static void tpad_led_set(struct led_classdev *cdev,
  420. enum led_brightness brightness)
  421. {
  422. union acpi_object *r;
  423. r = lg_wmab(WM_TLED, WM_SET, brightness > LED_OFF);
  424. kfree(r);
  425. }
  426. static enum led_brightness tpad_led_get(struct led_classdev *cdev)
  427. {
  428. return ggov(GOV_TLED) > 0 ? LED_ON : LED_OFF;
  429. }
  430. static LED_DEVICE(tpad_led, 1);
  431. static void kbd_backlight_set(struct led_classdev *cdev,
  432. enum led_brightness brightness)
  433. {
  434. u32 val;
  435. union acpi_object *r;
  436. val = 0x22;
  437. if (brightness <= LED_OFF)
  438. val = 0;
  439. if (brightness >= LED_FULL)
  440. val = 0x24;
  441. r = lg_wmab(WM_KEY_LIGHT, WM_SET, val);
  442. kfree(r);
  443. }
  444. static enum led_brightness kbd_backlight_get(struct led_classdev *cdev)
  445. {
  446. union acpi_object *r;
  447. int val;
  448. r = lg_wmab(WM_KEY_LIGHT, WM_GET, 0);
  449. if (!r)
  450. return LED_OFF;
  451. if (r->type != ACPI_TYPE_BUFFER || r->buffer.pointer[1] != 0x05) {
  452. kfree(r);
  453. return LED_OFF;
  454. }
  455. switch (r->buffer.pointer[0] & 0x27) {
  456. case 0x24:
  457. val = LED_FULL;
  458. break;
  459. case 0x22:
  460. val = LED_HALF;
  461. break;
  462. default:
  463. val = LED_OFF;
  464. }
  465. kfree(r);
  466. return val;
  467. }
  468. static LED_DEVICE(kbd_backlight, 255);
  469. static void wmi_input_destroy(void)
  470. {
  471. if (inited & INIT_INPUT_WMI_2)
  472. wmi_remove_notify_handler(WMI_EVENT_GUID2);
  473. if (inited & INIT_INPUT_WMI_0)
  474. wmi_remove_notify_handler(WMI_EVENT_GUID0);
  475. if (inited & INIT_SPARSE_KEYMAP)
  476. input_unregister_device(wmi_input_dev);
  477. inited &= ~(INIT_INPUT_WMI_0 | INIT_INPUT_WMI_2 | INIT_SPARSE_KEYMAP);
  478. }
  479. static struct platform_driver pf_driver = {
  480. .driver = {
  481. .name = PLATFORM_NAME,
  482. }
  483. };
  484. static int acpi_add(struct acpi_device *device)
  485. {
  486. int ret;
  487. if (pf_device)
  488. return 0;
  489. ret = platform_driver_register(&pf_driver);
  490. if (ret)
  491. return ret;
  492. pf_device = platform_device_register_simple(PLATFORM_NAME,
  493. PLATFORM_DEVID_NONE,
  494. NULL, 0);
  495. if (IS_ERR(pf_device)) {
  496. ret = PTR_ERR(pf_device);
  497. pf_device = NULL;
  498. pr_err("unable to register platform device\n");
  499. goto out_platform_registered;
  500. }
  501. ret = sysfs_create_group(&pf_device->dev.kobj, &dev_attribute_group);
  502. if (ret)
  503. goto out_platform_device;
  504. /* LEDs are optional */
  505. led_classdev_register(&pf_device->dev, &kbd_backlight);
  506. led_classdev_register(&pf_device->dev, &tpad_led);
  507. wmi_input_setup();
  508. return 0;
  509. out_platform_device:
  510. platform_device_unregister(pf_device);
  511. out_platform_registered:
  512. platform_driver_unregister(&pf_driver);
  513. return ret;
  514. }
  515. static int acpi_remove(struct acpi_device *device)
  516. {
  517. sysfs_remove_group(&pf_device->dev.kobj, &dev_attribute_group);
  518. led_classdev_unregister(&tpad_led);
  519. led_classdev_unregister(&kbd_backlight);
  520. wmi_input_destroy();
  521. platform_device_unregister(pf_device);
  522. pf_device = NULL;
  523. platform_driver_unregister(&pf_driver);
  524. return 0;
  525. }
  526. static const struct acpi_device_id device_ids[] = {
  527. {"LGEX0815", 0},
  528. {"", 0}
  529. };
  530. MODULE_DEVICE_TABLE(acpi, device_ids);
  531. static struct acpi_driver acpi_driver = {
  532. .name = "LG Gram Laptop Support",
  533. .class = "lg-laptop",
  534. .ids = device_ids,
  535. .ops = {
  536. .add = acpi_add,
  537. .remove = acpi_remove,
  538. .notify = acpi_notify,
  539. },
  540. .owner = THIS_MODULE,
  541. };
  542. static int __init acpi_init(void)
  543. {
  544. int result;
  545. result = acpi_bus_register_driver(&acpi_driver);
  546. if (result < 0) {
  547. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error registering driver\n"));
  548. return -ENODEV;
  549. }
  550. return 0;
  551. }
  552. static void __exit acpi_exit(void)
  553. {
  554. acpi_bus_unregister_driver(&acpi_driver);
  555. }
  556. module_init(acpi_init);
  557. module_exit(acpi_exit);