ac.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #ifdef CONFIG_ACPI_PROCFS_POWER
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #endif
  36. #include <linux/platform_device.h>
  37. #include <linux/power_supply.h>
  38. #include <acpi/acpi_bus.h>
  39. #include <acpi/acpi_drivers.h>
  40. #define PREFIX "ACPI: "
  41. #define ACPI_AC_CLASS "ac_adapter"
  42. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  43. #define ACPI_AC_FILE_STATE "state"
  44. #define ACPI_AC_NOTIFY_STATUS 0x80
  45. #define ACPI_AC_STATUS_OFFLINE 0x00
  46. #define ACPI_AC_STATUS_ONLINE 0x01
  47. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  48. #define _COMPONENT ACPI_AC_COMPONENT
  49. ACPI_MODULE_NAME("ac");
  50. MODULE_AUTHOR("Paul Diefenbaugh");
  51. MODULE_DESCRIPTION("ACPI AC Adapter Driver");
  52. MODULE_LICENSE("GPL");
  53. #ifdef CONFIG_ACPI_PROCFS_POWER
  54. extern struct proc_dir_entry *acpi_lock_ac_dir(void);
  55. extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
  56. static int acpi_ac_open_fs(struct inode *inode, struct file *file);
  57. #endif
  58. static int ac_sleep_before_get_state_ms;
  59. struct acpi_ac {
  60. struct power_supply charger;
  61. struct acpi_device *adev;
  62. struct platform_device *pdev;
  63. unsigned long long state;
  64. };
  65. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  66. #ifdef CONFIG_ACPI_PROCFS_POWER
  67. static const struct file_operations acpi_ac_fops = {
  68. .owner = THIS_MODULE,
  69. .open = acpi_ac_open_fs,
  70. .read = seq_read,
  71. .llseek = seq_lseek,
  72. .release = single_release,
  73. };
  74. #endif
  75. /* --------------------------------------------------------------------------
  76. AC Adapter Management
  77. -------------------------------------------------------------------------- */
  78. static int acpi_ac_get_state(struct acpi_ac *ac)
  79. {
  80. acpi_status status;
  81. status = acpi_evaluate_integer(ac->adev->handle, "_PSR", NULL,
  82. &ac->state);
  83. if (ACPI_FAILURE(status)) {
  84. ACPI_EXCEPTION((AE_INFO, status,
  85. "Error reading AC Adapter state"));
  86. ac->state = ACPI_AC_STATUS_UNKNOWN;
  87. return -ENODEV;
  88. }
  89. return 0;
  90. }
  91. /* --------------------------------------------------------------------------
  92. sysfs I/F
  93. -------------------------------------------------------------------------- */
  94. static int get_ac_property(struct power_supply *psy,
  95. enum power_supply_property psp,
  96. union power_supply_propval *val)
  97. {
  98. struct acpi_ac *ac = to_acpi_ac(psy);
  99. if (!ac)
  100. return -ENODEV;
  101. if (acpi_ac_get_state(ac))
  102. return -ENODEV;
  103. switch (psp) {
  104. case POWER_SUPPLY_PROP_ONLINE:
  105. val->intval = ac->state;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. return 0;
  111. }
  112. static enum power_supply_property ac_props[] = {
  113. POWER_SUPPLY_PROP_ONLINE,
  114. };
  115. #ifdef CONFIG_ACPI_PROCFS_POWER
  116. /* --------------------------------------------------------------------------
  117. FS Interface (/proc)
  118. -------------------------------------------------------------------------- */
  119. static struct proc_dir_entry *acpi_ac_dir;
  120. static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
  121. {
  122. struct acpi_ac *ac = seq->private;
  123. if (!ac)
  124. return 0;
  125. if (acpi_ac_get_state(ac)) {
  126. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  127. return 0;
  128. }
  129. seq_puts(seq, "state: ");
  130. switch (ac->state) {
  131. case ACPI_AC_STATUS_OFFLINE:
  132. seq_puts(seq, "off-line\n");
  133. break;
  134. case ACPI_AC_STATUS_ONLINE:
  135. seq_puts(seq, "on-line\n");
  136. break;
  137. default:
  138. seq_puts(seq, "unknown\n");
  139. break;
  140. }
  141. return 0;
  142. }
  143. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  144. {
  145. return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
  146. }
  147. static int acpi_ac_add_fs(struct acpi_ac *ac)
  148. {
  149. struct proc_dir_entry *entry = NULL;
  150. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
  151. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  152. if (!acpi_device_dir(ac->adev)) {
  153. acpi_device_dir(ac->adev) =
  154. proc_mkdir(acpi_device_bid(ac->adev), acpi_ac_dir);
  155. if (!acpi_device_dir(ac->adev))
  156. return -ENODEV;
  157. }
  158. /* 'state' [R] */
  159. entry = proc_create_data(ACPI_AC_FILE_STATE,
  160. S_IRUGO, acpi_device_dir(ac->adev),
  161. &acpi_ac_fops, ac);
  162. if (!entry)
  163. return -ENODEV;
  164. return 0;
  165. }
  166. static int acpi_ac_remove_fs(struct acpi_ac *ac)
  167. {
  168. if (acpi_device_dir(ac->adev)) {
  169. remove_proc_entry(ACPI_AC_FILE_STATE,
  170. acpi_device_dir(ac->adev));
  171. remove_proc_entry(acpi_device_bid(ac->adev), acpi_ac_dir);
  172. acpi_device_dir(ac->adev) = NULL;
  173. }
  174. return 0;
  175. }
  176. #endif
  177. /* --------------------------------------------------------------------------
  178. Driver Model
  179. -------------------------------------------------------------------------- */
  180. static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data)
  181. {
  182. struct acpi_ac *ac = data;
  183. if (!ac)
  184. return;
  185. switch (event) {
  186. default:
  187. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  188. "Unsupported event [0x%x]\n", event));
  189. case ACPI_AC_NOTIFY_STATUS:
  190. case ACPI_NOTIFY_BUS_CHECK:
  191. case ACPI_NOTIFY_DEVICE_CHECK:
  192. /*
  193. * A buggy BIOS may notify AC first and then sleep for
  194. * a specific time before doing actual operations in the
  195. * EC event handler (_Qxx). This will cause the AC state
  196. * reported by the ACPI event to be incorrect, so wait for a
  197. * specific time for the EC event handler to make progress.
  198. */
  199. if (ac_sleep_before_get_state_ms > 0)
  200. msleep(ac_sleep_before_get_state_ms);
  201. acpi_ac_get_state(ac);
  202. acpi_bus_generate_netlink_event(ac->adev->pnp.device_class,
  203. dev_name(&ac->pdev->dev),
  204. event, (u32) ac->state);
  205. acpi_notifier_call_chain(ac->adev, event, (u32) ac->state);
  206. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  207. }
  208. return;
  209. }
  210. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  211. {
  212. ac_sleep_before_get_state_ms = 1000;
  213. return 0;
  214. }
  215. static struct dmi_system_id ac_dmi_table[] = {
  216. {
  217. .callback = thinkpad_e530_quirk,
  218. .ident = "thinkpad e530",
  219. .matches = {
  220. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  221. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  222. },
  223. },
  224. {},
  225. };
  226. static int acpi_ac_probe(struct platform_device *pdev)
  227. {
  228. int result = 0;
  229. struct acpi_ac *ac = NULL;
  230. struct acpi_device *adev;
  231. if (!pdev)
  232. return -EINVAL;
  233. result = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
  234. if (result)
  235. return -ENODEV;
  236. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  237. if (!ac)
  238. return -ENOMEM;
  239. strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME);
  240. strcpy(acpi_device_class(adev), ACPI_AC_CLASS);
  241. ac->adev = adev;
  242. ac->pdev = pdev;
  243. platform_set_drvdata(pdev, ac);
  244. result = acpi_ac_get_state(ac);
  245. if (result)
  246. goto end;
  247. #ifdef CONFIG_ACPI_PROCFS_POWER
  248. result = acpi_ac_add_fs(ac);
  249. if (result)
  250. goto end;
  251. #endif
  252. ac->charger.name = acpi_device_bid(adev);
  253. ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
  254. ac->charger.properties = ac_props;
  255. ac->charger.num_properties = ARRAY_SIZE(ac_props);
  256. ac->charger.get_property = get_ac_property;
  257. result = power_supply_register(&pdev->dev, &ac->charger);
  258. if (result)
  259. goto end;
  260. result = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
  261. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler, ac);
  262. if (result) {
  263. power_supply_unregister(&ac->charger);
  264. goto end;
  265. }
  266. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  267. acpi_device_name(adev), acpi_device_bid(adev),
  268. ac->state ? "on-line" : "off-line");
  269. end:
  270. if (result) {
  271. #ifdef CONFIG_ACPI_PROCFS_POWER
  272. acpi_ac_remove_fs(ac);
  273. #endif
  274. kfree(ac);
  275. }
  276. dmi_check_system(ac_dmi_table);
  277. return result;
  278. }
  279. #ifdef CONFIG_PM_SLEEP
  280. static int acpi_ac_resume(struct device *dev)
  281. {
  282. struct acpi_ac *ac;
  283. unsigned old_state;
  284. if (!dev)
  285. return -EINVAL;
  286. ac = platform_get_drvdata(to_platform_device(dev));
  287. if (!ac)
  288. return -EINVAL;
  289. old_state = ac->state;
  290. if (acpi_ac_get_state(ac))
  291. return 0;
  292. if (old_state != ac->state)
  293. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  294. return 0;
  295. }
  296. #endif
  297. static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume);
  298. static int acpi_ac_remove(struct platform_device *pdev)
  299. {
  300. struct acpi_ac *ac;
  301. if (!pdev)
  302. return -EINVAL;
  303. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  304. ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler);
  305. ac = platform_get_drvdata(pdev);
  306. if (ac->charger.dev)
  307. power_supply_unregister(&ac->charger);
  308. #ifdef CONFIG_ACPI_PROCFS_POWER
  309. acpi_ac_remove_fs(ac);
  310. #endif
  311. kfree(ac);
  312. return 0;
  313. }
  314. static const struct acpi_device_id acpi_ac_match[] = {
  315. { "ACPI0003", 0 },
  316. { }
  317. };
  318. MODULE_DEVICE_TABLE(acpi, acpi_ac_match);
  319. static struct platform_driver acpi_ac_driver = {
  320. .probe = acpi_ac_probe,
  321. .remove = acpi_ac_remove,
  322. .driver = {
  323. .name = "acpi-ac",
  324. .owner = THIS_MODULE,
  325. .pm = &acpi_ac_pm_ops,
  326. .acpi_match_table = ACPI_PTR(acpi_ac_match),
  327. },
  328. };
  329. static int __init acpi_ac_init(void)
  330. {
  331. int result;
  332. if (acpi_disabled)
  333. return -ENODEV;
  334. #ifdef CONFIG_ACPI_PROCFS_POWER
  335. acpi_ac_dir = acpi_lock_ac_dir();
  336. if (!acpi_ac_dir)
  337. return -ENODEV;
  338. #endif
  339. result = platform_driver_register(&acpi_ac_driver);
  340. if (result < 0) {
  341. #ifdef CONFIG_ACPI_PROCFS_POWER
  342. acpi_unlock_ac_dir(acpi_ac_dir);
  343. #endif
  344. return -ENODEV;
  345. }
  346. return 0;
  347. }
  348. static void __exit acpi_ac_exit(void)
  349. {
  350. platform_driver_unregister(&acpi_ac_driver);
  351. #ifdef CONFIG_ACPI_PROCFS_POWER
  352. acpi_unlock_ac_dir(acpi_ac_dir);
  353. #endif
  354. }
  355. module_init(acpi_ac_init);
  356. module_exit(acpi_ac_exit);