ac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/dmi.h>
  31. #include <linux/delay.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <acpi/acpi_bus.h>
  35. #include <acpi/acpi_drivers.h>
  36. #define PREFIX "ACPI: "
  37. #define ACPI_AC_CLASS "ac_adapter"
  38. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  39. #define ACPI_AC_FILE_STATE "state"
  40. #define ACPI_AC_NOTIFY_STATUS 0x80
  41. #define ACPI_AC_STATUS_OFFLINE 0x00
  42. #define ACPI_AC_STATUS_ONLINE 0x01
  43. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  44. #define _COMPONENT ACPI_AC_COMPONENT
  45. ACPI_MODULE_NAME("ac");
  46. MODULE_AUTHOR("Paul Diefenbaugh");
  47. MODULE_DESCRIPTION("ACPI AC Adapter Driver");
  48. MODULE_LICENSE("GPL");
  49. static int ac_sleep_before_get_state_ms;
  50. struct acpi_ac {
  51. struct power_supply charger;
  52. struct acpi_device *adev;
  53. struct platform_device *pdev;
  54. unsigned long long state;
  55. };
  56. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  57. /* --------------------------------------------------------------------------
  58. AC Adapter Management
  59. -------------------------------------------------------------------------- */
  60. static int acpi_ac_get_state(struct acpi_ac *ac)
  61. {
  62. acpi_status status;
  63. status = acpi_evaluate_integer(ac->adev->handle, "_PSR", NULL,
  64. &ac->state);
  65. if (ACPI_FAILURE(status)) {
  66. ACPI_EXCEPTION((AE_INFO, status,
  67. "Error reading AC Adapter state"));
  68. ac->state = ACPI_AC_STATUS_UNKNOWN;
  69. return -ENODEV;
  70. }
  71. return 0;
  72. }
  73. /* --------------------------------------------------------------------------
  74. sysfs I/F
  75. -------------------------------------------------------------------------- */
  76. static int get_ac_property(struct power_supply *psy,
  77. enum power_supply_property psp,
  78. union power_supply_propval *val)
  79. {
  80. struct acpi_ac *ac = to_acpi_ac(psy);
  81. if (!ac)
  82. return -ENODEV;
  83. if (acpi_ac_get_state(ac))
  84. return -ENODEV;
  85. switch (psp) {
  86. case POWER_SUPPLY_PROP_ONLINE:
  87. val->intval = ac->state;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. return 0;
  93. }
  94. static enum power_supply_property ac_props[] = {
  95. POWER_SUPPLY_PROP_ONLINE,
  96. };
  97. /* --------------------------------------------------------------------------
  98. Driver Model
  99. -------------------------------------------------------------------------- */
  100. static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data)
  101. {
  102. struct acpi_ac *ac = data;
  103. if (!ac)
  104. return;
  105. switch (event) {
  106. default:
  107. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  108. "Unsupported event [0x%x]\n", event));
  109. case ACPI_AC_NOTIFY_STATUS:
  110. case ACPI_NOTIFY_BUS_CHECK:
  111. case ACPI_NOTIFY_DEVICE_CHECK:
  112. /*
  113. * A buggy BIOS may notify AC first and then sleep for
  114. * a specific time before doing actual operations in the
  115. * EC event handler (_Qxx). This will cause the AC state
  116. * reported by the ACPI event to be incorrect, so wait for a
  117. * specific time for the EC event handler to make progress.
  118. */
  119. if (ac_sleep_before_get_state_ms > 0)
  120. msleep(ac_sleep_before_get_state_ms);
  121. acpi_ac_get_state(ac);
  122. acpi_bus_generate_netlink_event(ac->adev->pnp.device_class,
  123. dev_name(&ac->pdev->dev),
  124. event, (u32) ac->state);
  125. acpi_notifier_call_chain(ac->adev, event, (u32) ac->state);
  126. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  127. }
  128. return;
  129. }
  130. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  131. {
  132. ac_sleep_before_get_state_ms = 1000;
  133. return 0;
  134. }
  135. static struct dmi_system_id ac_dmi_table[] = {
  136. {
  137. .callback = thinkpad_e530_quirk,
  138. .ident = "thinkpad e530",
  139. .matches = {
  140. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  141. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  142. },
  143. },
  144. {},
  145. };
  146. static int acpi_ac_probe(struct platform_device *pdev)
  147. {
  148. int result = 0;
  149. struct acpi_ac *ac = NULL;
  150. struct acpi_device *adev;
  151. if (!pdev)
  152. return -EINVAL;
  153. result = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
  154. if (result)
  155. return -ENODEV;
  156. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  157. if (!ac)
  158. return -ENOMEM;
  159. strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME);
  160. strcpy(acpi_device_class(adev), ACPI_AC_CLASS);
  161. ac->adev = adev;
  162. ac->pdev = pdev;
  163. platform_set_drvdata(pdev, ac);
  164. result = acpi_ac_get_state(ac);
  165. if (result)
  166. goto end;
  167. ac->charger.name = acpi_device_bid(adev);
  168. ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
  169. ac->charger.properties = ac_props;
  170. ac->charger.num_properties = ARRAY_SIZE(ac_props);
  171. ac->charger.get_property = get_ac_property;
  172. result = power_supply_register(&pdev->dev, &ac->charger);
  173. if (result)
  174. goto end;
  175. result = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
  176. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler, ac);
  177. if (result) {
  178. power_supply_unregister(&ac->charger);
  179. goto end;
  180. }
  181. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  182. acpi_device_name(adev), acpi_device_bid(adev),
  183. ac->state ? "on-line" : "off-line");
  184. end:
  185. if (result)
  186. kfree(ac);
  187. dmi_check_system(ac_dmi_table);
  188. return result;
  189. }
  190. #ifdef CONFIG_PM_SLEEP
  191. static int acpi_ac_resume(struct device *dev)
  192. {
  193. struct acpi_ac *ac;
  194. unsigned old_state;
  195. if (!dev)
  196. return -EINVAL;
  197. ac = platform_get_drvdata(to_platform_device(dev));
  198. if (!ac)
  199. return -EINVAL;
  200. old_state = ac->state;
  201. if (acpi_ac_get_state(ac))
  202. return 0;
  203. if (old_state != ac->state)
  204. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  205. return 0;
  206. }
  207. #endif
  208. static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume);
  209. static int acpi_ac_remove(struct platform_device *pdev)
  210. {
  211. struct acpi_ac *ac;
  212. if (!pdev)
  213. return -EINVAL;
  214. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  215. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler);
  216. ac = platform_get_drvdata(pdev);
  217. if (ac->charger.dev)
  218. power_supply_unregister(&ac->charger);
  219. kfree(ac);
  220. return 0;
  221. }
  222. static const struct acpi_device_id acpi_ac_match[] = {
  223. { "ACPI0003", 0 },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(acpi, acpi_ac_match);
  227. static struct platform_driver acpi_ac_driver = {
  228. .probe = acpi_ac_probe,
  229. .remove = acpi_ac_remove,
  230. .driver = {
  231. .name = "acpi-ac",
  232. .owner = THIS_MODULE,
  233. .pm = &acpi_ac_pm_ops,
  234. .acpi_match_table = ACPI_PTR(acpi_ac_match),
  235. },
  236. };
  237. static int __init acpi_ac_init(void)
  238. {
  239. int result;
  240. if (acpi_disabled)
  241. return -ENODEV;
  242. result = platform_driver_register(&acpi_ac_driver);
  243. if (result < 0)
  244. return -ENODEV;
  245. return 0;
  246. }
  247. static void __exit acpi_ac_exit(void)
  248. {
  249. platform_driver_unregister(&acpi_ac_driver);
  250. }
  251. module_init(acpi_ac_init);
  252. module_exit(acpi_ac_exit);