ac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 platform_device *pdev;
  53. unsigned long long state;
  54. };
  55. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  56. /* --------------------------------------------------------------------------
  57. AC Adapter Management
  58. -------------------------------------------------------------------------- */
  59. static int acpi_ac_get_state(struct acpi_ac *ac)
  60. {
  61. acpi_status status;
  62. acpi_handle handle = ACPI_HANDLE(&ac->pdev->dev);
  63. status = acpi_evaluate_integer(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. struct acpi_device *adev;
  104. if (!ac)
  105. return;
  106. switch (event) {
  107. default:
  108. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  109. "Unsupported event [0x%x]\n", event));
  110. case ACPI_AC_NOTIFY_STATUS:
  111. case ACPI_NOTIFY_BUS_CHECK:
  112. case ACPI_NOTIFY_DEVICE_CHECK:
  113. /*
  114. * A buggy BIOS may notify AC first and then sleep for
  115. * a specific time before doing actual operations in the
  116. * EC event handler (_Qxx). This will cause the AC state
  117. * reported by the ACPI event to be incorrect, so wait for a
  118. * specific time for the EC event handler to make progress.
  119. */
  120. if (ac_sleep_before_get_state_ms > 0)
  121. msleep(ac_sleep_before_get_state_ms);
  122. acpi_ac_get_state(ac);
  123. adev = ACPI_COMPANION(&ac->pdev->dev);
  124. acpi_bus_generate_netlink_event(adev->pnp.device_class,
  125. dev_name(&ac->pdev->dev),
  126. event, (u32) ac->state);
  127. acpi_notifier_call_chain(adev, event, (u32) ac->state);
  128. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  129. }
  130. return;
  131. }
  132. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  133. {
  134. ac_sleep_before_get_state_ms = 1000;
  135. return 0;
  136. }
  137. static struct dmi_system_id ac_dmi_table[] = {
  138. {
  139. .callback = thinkpad_e530_quirk,
  140. .ident = "thinkpad e530",
  141. .matches = {
  142. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  143. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  144. },
  145. },
  146. {},
  147. };
  148. static int acpi_ac_probe(struct platform_device *pdev)
  149. {
  150. int result = 0;
  151. struct acpi_ac *ac = NULL;
  152. struct acpi_device *adev;
  153. if (!pdev)
  154. return -EINVAL;
  155. adev = ACPI_COMPANION(&pdev->dev);
  156. if (!adev)
  157. return -ENODEV;
  158. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  159. if (!ac)
  160. return -ENOMEM;
  161. strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME);
  162. strcpy(acpi_device_class(adev), ACPI_AC_CLASS);
  163. ac->pdev = pdev;
  164. platform_set_drvdata(pdev, ac);
  165. result = acpi_ac_get_state(ac);
  166. if (result)
  167. goto end;
  168. ac->charger.name = acpi_device_bid(adev);
  169. ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
  170. ac->charger.properties = ac_props;
  171. ac->charger.num_properties = ARRAY_SIZE(ac_props);
  172. ac->charger.get_property = get_ac_property;
  173. result = power_supply_register(&pdev->dev, &ac->charger);
  174. if (result)
  175. goto end;
  176. result = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
  177. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler, ac);
  178. if (result) {
  179. power_supply_unregister(&ac->charger);
  180. goto end;
  181. }
  182. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  183. acpi_device_name(adev), acpi_device_bid(adev),
  184. ac->state ? "on-line" : "off-line");
  185. end:
  186. if (result)
  187. kfree(ac);
  188. dmi_check_system(ac_dmi_table);
  189. return result;
  190. }
  191. #ifdef CONFIG_PM_SLEEP
  192. static int acpi_ac_resume(struct device *dev)
  193. {
  194. struct acpi_ac *ac;
  195. unsigned old_state;
  196. if (!dev)
  197. return -EINVAL;
  198. ac = platform_get_drvdata(to_platform_device(dev));
  199. if (!ac)
  200. return -EINVAL;
  201. old_state = ac->state;
  202. if (acpi_ac_get_state(ac))
  203. return 0;
  204. if (old_state != ac->state)
  205. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  206. return 0;
  207. }
  208. #endif
  209. static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume);
  210. static int acpi_ac_remove(struct platform_device *pdev)
  211. {
  212. struct acpi_ac *ac;
  213. if (!pdev)
  214. return -EINVAL;
  215. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  216. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler);
  217. ac = platform_get_drvdata(pdev);
  218. if (ac->charger.dev)
  219. power_supply_unregister(&ac->charger);
  220. kfree(ac);
  221. return 0;
  222. }
  223. static const struct acpi_device_id acpi_ac_match[] = {
  224. { "ACPI0003", 0 },
  225. { }
  226. };
  227. MODULE_DEVICE_TABLE(acpi, acpi_ac_match);
  228. static struct platform_driver acpi_ac_driver = {
  229. .probe = acpi_ac_probe,
  230. .remove = acpi_ac_remove,
  231. .driver = {
  232. .name = "acpi-ac",
  233. .owner = THIS_MODULE,
  234. .pm = &acpi_ac_pm_ops,
  235. .acpi_match_table = ACPI_PTR(acpi_ac_match),
  236. },
  237. };
  238. static int __init acpi_ac_init(void)
  239. {
  240. int result;
  241. if (acpi_disabled)
  242. return -ENODEV;
  243. result = platform_driver_register(&acpi_ac_driver);
  244. if (result < 0)
  245. return -ENODEV;
  246. return 0;
  247. }
  248. static void __exit acpi_ac_exit(void)
  249. {
  250. platform_driver_unregister(&acpi_ac_driver);
  251. }
  252. module_init(acpi_ac_init);
  253. module_exit(acpi_ac_exit);