ac.c 7.6 KB

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