ac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. */
  8. #define pr_fmt(fmt) "ACPI: AC: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/dmi.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/acpi.h>
  19. #include <acpi/battery.h>
  20. #define ACPI_AC_CLASS "ac_adapter"
  21. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  22. #define ACPI_AC_FILE_STATE "state"
  23. #define ACPI_AC_NOTIFY_STATUS 0x80
  24. #define ACPI_AC_STATUS_OFFLINE 0x00
  25. #define ACPI_AC_STATUS_ONLINE 0x01
  26. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  27. MODULE_AUTHOR("Paul Diefenbaugh");
  28. MODULE_DESCRIPTION("ACPI AC Adapter Driver");
  29. MODULE_LICENSE("GPL");
  30. static int acpi_ac_add(struct acpi_device *device);
  31. static int acpi_ac_remove(struct acpi_device *device);
  32. static void acpi_ac_notify(struct acpi_device *device, u32 event);
  33. static const struct acpi_device_id ac_device_ids[] = {
  34. {"ACPI0003", 0},
  35. {"", 0},
  36. };
  37. MODULE_DEVICE_TABLE(acpi, ac_device_ids);
  38. #ifdef CONFIG_PM_SLEEP
  39. static int acpi_ac_resume(struct device *dev);
  40. #endif
  41. static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
  42. static int ac_sleep_before_get_state_ms;
  43. static int ac_only;
  44. static struct acpi_driver acpi_ac_driver = {
  45. .name = "ac",
  46. .class = ACPI_AC_CLASS,
  47. .ids = ac_device_ids,
  48. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  49. .ops = {
  50. .add = acpi_ac_add,
  51. .remove = acpi_ac_remove,
  52. .notify = acpi_ac_notify,
  53. },
  54. .drv.pm = &acpi_ac_pm,
  55. };
  56. struct acpi_ac {
  57. struct power_supply *charger;
  58. struct power_supply_desc charger_desc;
  59. struct acpi_device *device;
  60. unsigned long long state;
  61. struct notifier_block battery_nb;
  62. };
  63. #define to_acpi_ac(x) power_supply_get_drvdata(x)
  64. /* AC Adapter Management */
  65. static int acpi_ac_get_state(struct acpi_ac *ac)
  66. {
  67. acpi_status status = AE_OK;
  68. if (!ac)
  69. return -EINVAL;
  70. if (ac_only) {
  71. ac->state = 1;
  72. return 0;
  73. }
  74. status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
  75. &ac->state);
  76. if (ACPI_FAILURE(status)) {
  77. acpi_handle_info(ac->device->handle,
  78. "Error reading AC Adapter state: %s\n",
  79. acpi_format_exception(status));
  80. ac->state = ACPI_AC_STATUS_UNKNOWN;
  81. return -ENODEV;
  82. }
  83. return 0;
  84. }
  85. /* sysfs I/F */
  86. static int get_ac_property(struct power_supply *psy,
  87. enum power_supply_property psp,
  88. union power_supply_propval *val)
  89. {
  90. struct acpi_ac *ac = to_acpi_ac(psy);
  91. if (!ac)
  92. return -ENODEV;
  93. if (acpi_ac_get_state(ac))
  94. return -ENODEV;
  95. switch (psp) {
  96. case POWER_SUPPLY_PROP_ONLINE:
  97. val->intval = ac->state;
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. return 0;
  103. }
  104. static enum power_supply_property ac_props[] = {
  105. POWER_SUPPLY_PROP_ONLINE,
  106. };
  107. /* Driver Model */
  108. static void acpi_ac_notify(struct acpi_device *device, u32 event)
  109. {
  110. struct acpi_ac *ac = acpi_driver_data(device);
  111. if (!ac)
  112. return;
  113. switch (event) {
  114. default:
  115. acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
  116. event);
  117. fallthrough;
  118. case ACPI_AC_NOTIFY_STATUS:
  119. case ACPI_NOTIFY_BUS_CHECK:
  120. case ACPI_NOTIFY_DEVICE_CHECK:
  121. /*
  122. * A buggy BIOS may notify AC first and then sleep for
  123. * a specific time before doing actual operations in the
  124. * EC event handler (_Qxx). This will cause the AC state
  125. * reported by the ACPI event to be incorrect, so wait for a
  126. * specific time for the EC event handler to make progress.
  127. */
  128. if (ac_sleep_before_get_state_ms > 0)
  129. msleep(ac_sleep_before_get_state_ms);
  130. acpi_ac_get_state(ac);
  131. acpi_bus_generate_netlink_event(device->pnp.device_class,
  132. dev_name(&device->dev), event,
  133. (u32) ac->state);
  134. acpi_notifier_call_chain(device, event, (u32) ac->state);
  135. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  136. }
  137. }
  138. static int acpi_ac_battery_notify(struct notifier_block *nb,
  139. unsigned long action, void *data)
  140. {
  141. struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
  142. struct acpi_bus_event *event = (struct acpi_bus_event *)data;
  143. /*
  144. * On HP Pavilion dv6-6179er AC status notifications aren't triggered
  145. * when adapter is plugged/unplugged. However, battery status
  146. * notifications are triggered when battery starts charging or
  147. * discharging. Re-reading AC status triggers lost AC notifications,
  148. * if AC status has changed.
  149. */
  150. if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
  151. event->type == ACPI_BATTERY_NOTIFY_STATUS)
  152. acpi_ac_get_state(ac);
  153. return NOTIFY_OK;
  154. }
  155. static int __init thinkpad_e530_quirk(const struct dmi_system_id *d)
  156. {
  157. ac_sleep_before_get_state_ms = 1000;
  158. return 0;
  159. }
  160. static int __init ac_only_quirk(const struct dmi_system_id *d)
  161. {
  162. ac_only = 1;
  163. return 0;
  164. }
  165. /* Please keep this list alphabetically sorted */
  166. static const struct dmi_system_id ac_dmi_table[] __initconst = {
  167. {
  168. /* Kodlix GK45 returning incorrect state */
  169. .callback = ac_only_quirk,
  170. .matches = {
  171. DMI_MATCH(DMI_PRODUCT_NAME, "GK45"),
  172. },
  173. },
  174. {
  175. /* Lenovo Thinkpad e530, see comment in acpi_ac_notify() */
  176. .callback = thinkpad_e530_quirk,
  177. .matches = {
  178. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  179. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  180. },
  181. },
  182. {},
  183. };
  184. static int acpi_ac_add(struct acpi_device *device)
  185. {
  186. struct power_supply_config psy_cfg = {};
  187. int result = 0;
  188. struct acpi_ac *ac = NULL;
  189. if (!device)
  190. return -EINVAL;
  191. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  192. if (!ac)
  193. return -ENOMEM;
  194. ac->device = device;
  195. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  196. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  197. device->driver_data = ac;
  198. result = acpi_ac_get_state(ac);
  199. if (result)
  200. goto end;
  201. psy_cfg.drv_data = ac;
  202. ac->charger_desc.name = acpi_device_bid(device);
  203. ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
  204. ac->charger_desc.properties = ac_props;
  205. ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
  206. ac->charger_desc.get_property = get_ac_property;
  207. ac->charger = power_supply_register(&ac->device->dev,
  208. &ac->charger_desc, &psy_cfg);
  209. if (IS_ERR(ac->charger)) {
  210. result = PTR_ERR(ac->charger);
  211. goto end;
  212. }
  213. pr_info("%s [%s] (%s)\n", acpi_device_name(device),
  214. acpi_device_bid(device), ac->state ? "on-line" : "off-line");
  215. ac->battery_nb.notifier_call = acpi_ac_battery_notify;
  216. register_acpi_notifier(&ac->battery_nb);
  217. end:
  218. if (result)
  219. kfree(ac);
  220. return result;
  221. }
  222. #ifdef CONFIG_PM_SLEEP
  223. static int acpi_ac_resume(struct device *dev)
  224. {
  225. struct acpi_ac *ac;
  226. unsigned int old_state;
  227. if (!dev)
  228. return -EINVAL;
  229. ac = acpi_driver_data(to_acpi_device(dev));
  230. if (!ac)
  231. return -EINVAL;
  232. old_state = ac->state;
  233. if (acpi_ac_get_state(ac))
  234. return 0;
  235. if (old_state != ac->state)
  236. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  237. return 0;
  238. }
  239. #else
  240. #define acpi_ac_resume NULL
  241. #endif
  242. static int acpi_ac_remove(struct acpi_device *device)
  243. {
  244. struct acpi_ac *ac = NULL;
  245. if (!device || !acpi_driver_data(device))
  246. return -EINVAL;
  247. ac = acpi_driver_data(device);
  248. power_supply_unregister(ac->charger);
  249. unregister_acpi_notifier(&ac->battery_nb);
  250. kfree(ac);
  251. return 0;
  252. }
  253. static int __init acpi_ac_init(void)
  254. {
  255. int result;
  256. if (acpi_disabled)
  257. return -ENODEV;
  258. if (acpi_quirk_skip_acpi_ac_and_battery())
  259. return -ENODEV;
  260. dmi_check_system(ac_dmi_table);
  261. result = acpi_bus_register_driver(&acpi_ac_driver);
  262. if (result < 0)
  263. return -ENODEV;
  264. return 0;
  265. }
  266. static void __exit acpi_ac_exit(void)
  267. {
  268. acpi_bus_unregister_driver(&acpi_ac_driver);
  269. }
  270. module_init(acpi_ac_init);
  271. module_exit(acpi_ac_exit);