ac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <linux/acpi.h>
  35. #define PREFIX "ACPI: "
  36. #define ACPI_AC_CLASS "ac_adapter"
  37. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  38. #define ACPI_AC_FILE_STATE "state"
  39. #define ACPI_AC_NOTIFY_STATUS 0x80
  40. #define ACPI_AC_STATUS_OFFLINE 0x00
  41. #define ACPI_AC_STATUS_ONLINE 0x01
  42. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  43. #define _COMPONENT ACPI_AC_COMPONENT
  44. ACPI_MODULE_NAME("ac");
  45. MODULE_AUTHOR("Paul Diefenbaugh");
  46. MODULE_DESCRIPTION("ACPI AC Adapter Driver");
  47. MODULE_LICENSE("GPL");
  48. static int ac_sleep_before_get_state_ms;
  49. struct acpi_ac {
  50. struct power_supply charger;
  51. struct platform_device *pdev;
  52. unsigned long long state;
  53. };
  54. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  55. /* --------------------------------------------------------------------------
  56. AC Adapter Management
  57. -------------------------------------------------------------------------- */
  58. static int acpi_ac_get_state(struct acpi_ac *ac)
  59. {
  60. acpi_status status;
  61. acpi_handle handle = ACPI_HANDLE(&ac->pdev->dev);
  62. status = acpi_evaluate_integer(handle, "_PSR", NULL,
  63. &ac->state);
  64. if (ACPI_FAILURE(status)) {
  65. ACPI_EXCEPTION((AE_INFO, status,
  66. "Error reading AC Adapter state"));
  67. ac->state = ACPI_AC_STATUS_UNKNOWN;
  68. return -ENODEV;
  69. }
  70. return 0;
  71. }
  72. /* --------------------------------------------------------------------------
  73. sysfs I/F
  74. -------------------------------------------------------------------------- */
  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 enum power_supply_property ac_props[] = {
  94. POWER_SUPPLY_PROP_ONLINE,
  95. };
  96. /* --------------------------------------------------------------------------
  97. Driver Model
  98. -------------------------------------------------------------------------- */
  99. static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data)
  100. {
  101. struct acpi_ac *ac = data;
  102. struct acpi_device *adev;
  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. adev = ACPI_COMPANION(&ac->pdev->dev);
  123. acpi_bus_generate_netlink_event(adev->pnp.device_class,
  124. dev_name(&ac->pdev->dev),
  125. event, (u32) ac->state);
  126. acpi_notifier_call_chain(adev, event, (u32) ac->state);
  127. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  128. }
  129. return;
  130. }
  131. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  132. {
  133. ac_sleep_before_get_state_ms = 1000;
  134. return 0;
  135. }
  136. static struct dmi_system_id ac_dmi_table[] = {
  137. {
  138. .callback = thinkpad_e530_quirk,
  139. .ident = "thinkpad e530",
  140. .matches = {
  141. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  142. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  143. },
  144. },
  145. {},
  146. };
  147. static int acpi_ac_probe(struct platform_device *pdev)
  148. {
  149. int result = 0;
  150. struct acpi_ac *ac = NULL;
  151. struct acpi_device *adev;
  152. if (!pdev)
  153. return -EINVAL;
  154. adev = ACPI_COMPANION(&pdev->dev);
  155. if (!adev)
  156. return -ENODEV;
  157. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  158. if (!ac)
  159. return -ENOMEM;
  160. strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME);
  161. strcpy(acpi_device_class(adev), ACPI_AC_CLASS);
  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_ALL_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. #else
  208. #define acpi_ac_resume NULL
  209. #endif
  210. static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume);
  211. static int acpi_ac_remove(struct platform_device *pdev)
  212. {
  213. struct acpi_ac *ac;
  214. if (!pdev)
  215. return -EINVAL;
  216. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  217. ACPI_ALL_NOTIFY, acpi_ac_notify_handler);
  218. ac = platform_get_drvdata(pdev);
  219. if (ac->charger.dev)
  220. power_supply_unregister(&ac->charger);
  221. kfree(ac);
  222. return 0;
  223. }
  224. static const struct acpi_device_id acpi_ac_match[] = {
  225. { "ACPI0003", 0 },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(acpi, acpi_ac_match);
  229. static struct platform_driver acpi_ac_driver = {
  230. .probe = acpi_ac_probe,
  231. .remove = acpi_ac_remove,
  232. .driver = {
  233. .name = "acpi-ac",
  234. .owner = THIS_MODULE,
  235. .pm = &acpi_ac_pm_ops,
  236. .acpi_match_table = ACPI_PTR(acpi_ac_match),
  237. },
  238. };
  239. static int __init acpi_ac_init(void)
  240. {
  241. int result;
  242. if (acpi_disabled)
  243. return -ENODEV;
  244. result = platform_driver_register(&acpi_ac_driver);
  245. if (result < 0)
  246. return -ENODEV;
  247. return 0;
  248. }
  249. static void __exit acpi_ac_exit(void)
  250. {
  251. platform_driver_unregister(&acpi_ac_driver);
  252. }
  253. module_init(acpi_ac_init);
  254. module_exit(acpi_ac_exit);