ac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/dmi.h>
  27. #include <linux/delay.h>
  28. #ifdef CONFIG_ACPI_PROCFS_POWER
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #endif
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <linux/acpi.h>
  35. #include "battery.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 acpi_ac_add(struct acpi_device *device);
  50. static int acpi_ac_remove(struct acpi_device *device);
  51. static void acpi_ac_notify(struct acpi_device *device, u32 event);
  52. static const struct acpi_device_id ac_device_ids[] = {
  53. {"ACPI0003", 0},
  54. {"", 0},
  55. };
  56. MODULE_DEVICE_TABLE(acpi, ac_device_ids);
  57. #ifdef CONFIG_PM_SLEEP
  58. static int acpi_ac_resume(struct device *dev);
  59. #endif
  60. static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
  61. #ifdef CONFIG_ACPI_PROCFS_POWER
  62. extern struct proc_dir_entry *acpi_lock_ac_dir(void);
  63. extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
  64. static int acpi_ac_open_fs(struct inode *inode, struct file *file);
  65. #endif
  66. static int ac_sleep_before_get_state_ms;
  67. static struct acpi_driver acpi_ac_driver = {
  68. .name = "ac",
  69. .class = ACPI_AC_CLASS,
  70. .ids = ac_device_ids,
  71. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  72. .ops = {
  73. .add = acpi_ac_add,
  74. .remove = acpi_ac_remove,
  75. .notify = acpi_ac_notify,
  76. },
  77. .drv.pm = &acpi_ac_pm,
  78. };
  79. struct acpi_ac {
  80. struct power_supply *charger;
  81. struct power_supply_desc charger_desc;
  82. struct acpi_device * device;
  83. unsigned long long state;
  84. struct notifier_block battery_nb;
  85. };
  86. #define to_acpi_ac(x) power_supply_get_drvdata(x)
  87. #ifdef CONFIG_ACPI_PROCFS_POWER
  88. static const struct file_operations acpi_ac_fops = {
  89. .owner = THIS_MODULE,
  90. .open = acpi_ac_open_fs,
  91. .read = seq_read,
  92. .llseek = seq_lseek,
  93. .release = single_release,
  94. };
  95. #endif
  96. /* --------------------------------------------------------------------------
  97. AC Adapter Management
  98. -------------------------------------------------------------------------- */
  99. static int acpi_ac_get_state(struct acpi_ac *ac)
  100. {
  101. acpi_status status = AE_OK;
  102. if (!ac)
  103. return -EINVAL;
  104. status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
  105. &ac->state);
  106. if (ACPI_FAILURE(status)) {
  107. ACPI_EXCEPTION((AE_INFO, status,
  108. "Error reading AC Adapter state"));
  109. ac->state = ACPI_AC_STATUS_UNKNOWN;
  110. return -ENODEV;
  111. }
  112. return 0;
  113. }
  114. /* --------------------------------------------------------------------------
  115. sysfs I/F
  116. -------------------------------------------------------------------------- */
  117. static int get_ac_property(struct power_supply *psy,
  118. enum power_supply_property psp,
  119. union power_supply_propval *val)
  120. {
  121. struct acpi_ac *ac = to_acpi_ac(psy);
  122. if (!ac)
  123. return -ENODEV;
  124. if (acpi_ac_get_state(ac))
  125. return -ENODEV;
  126. switch (psp) {
  127. case POWER_SUPPLY_PROP_ONLINE:
  128. val->intval = ac->state;
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. return 0;
  134. }
  135. static enum power_supply_property ac_props[] = {
  136. POWER_SUPPLY_PROP_ONLINE,
  137. };
  138. #ifdef CONFIG_ACPI_PROCFS_POWER
  139. /* --------------------------------------------------------------------------
  140. FS Interface (/proc)
  141. -------------------------------------------------------------------------- */
  142. static struct proc_dir_entry *acpi_ac_dir;
  143. static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
  144. {
  145. struct acpi_ac *ac = seq->private;
  146. if (!ac)
  147. return 0;
  148. if (acpi_ac_get_state(ac)) {
  149. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  150. return 0;
  151. }
  152. seq_puts(seq, "state: ");
  153. switch (ac->state) {
  154. case ACPI_AC_STATUS_OFFLINE:
  155. seq_puts(seq, "off-line\n");
  156. break;
  157. case ACPI_AC_STATUS_ONLINE:
  158. seq_puts(seq, "on-line\n");
  159. break;
  160. default:
  161. seq_puts(seq, "unknown\n");
  162. break;
  163. }
  164. return 0;
  165. }
  166. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  167. {
  168. return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
  169. }
  170. static int acpi_ac_add_fs(struct acpi_ac *ac)
  171. {
  172. struct proc_dir_entry *entry = NULL;
  173. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
  174. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  175. if (!acpi_device_dir(ac->device)) {
  176. acpi_device_dir(ac->device) =
  177. proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir);
  178. if (!acpi_device_dir(ac->device))
  179. return -ENODEV;
  180. }
  181. /* 'state' [R] */
  182. entry = proc_create_data(ACPI_AC_FILE_STATE,
  183. S_IRUGO, acpi_device_dir(ac->device),
  184. &acpi_ac_fops, ac);
  185. if (!entry)
  186. return -ENODEV;
  187. return 0;
  188. }
  189. static int acpi_ac_remove_fs(struct acpi_ac *ac)
  190. {
  191. if (acpi_device_dir(ac->device)) {
  192. remove_proc_entry(ACPI_AC_FILE_STATE,
  193. acpi_device_dir(ac->device));
  194. remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir);
  195. acpi_device_dir(ac->device) = NULL;
  196. }
  197. return 0;
  198. }
  199. #endif
  200. /* --------------------------------------------------------------------------
  201. Driver Model
  202. -------------------------------------------------------------------------- */
  203. static void acpi_ac_notify(struct acpi_device *device, u32 event)
  204. {
  205. struct acpi_ac *ac = acpi_driver_data(device);
  206. if (!ac)
  207. return;
  208. switch (event) {
  209. default:
  210. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  211. "Unsupported event [0x%x]\n", event));
  212. case ACPI_AC_NOTIFY_STATUS:
  213. case ACPI_NOTIFY_BUS_CHECK:
  214. case ACPI_NOTIFY_DEVICE_CHECK:
  215. /*
  216. * A buggy BIOS may notify AC first and then sleep for
  217. * a specific time before doing actual operations in the
  218. * EC event handler (_Qxx). This will cause the AC state
  219. * reported by the ACPI event to be incorrect, so wait for a
  220. * specific time for the EC event handler to make progress.
  221. */
  222. if (ac_sleep_before_get_state_ms > 0)
  223. msleep(ac_sleep_before_get_state_ms);
  224. acpi_ac_get_state(ac);
  225. acpi_bus_generate_netlink_event(device->pnp.device_class,
  226. dev_name(&device->dev), event,
  227. (u32) ac->state);
  228. acpi_notifier_call_chain(device, event, (u32) ac->state);
  229. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  230. }
  231. return;
  232. }
  233. static int acpi_ac_battery_notify(struct notifier_block *nb,
  234. unsigned long action, void *data)
  235. {
  236. struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
  237. struct acpi_bus_event *event = (struct acpi_bus_event *)data;
  238. /*
  239. * On HP Pavilion dv6-6179er AC status notifications aren't triggered
  240. * when adapter is plugged/unplugged. However, battery status
  241. * notifcations are triggered when battery starts charging or
  242. * discharging. Re-reading AC status triggers lost AC notifications,
  243. * if AC status has changed.
  244. */
  245. if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
  246. event->type == ACPI_BATTERY_NOTIFY_STATUS)
  247. acpi_ac_get_state(ac);
  248. return NOTIFY_OK;
  249. }
  250. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  251. {
  252. ac_sleep_before_get_state_ms = 1000;
  253. return 0;
  254. }
  255. static const struct dmi_system_id ac_dmi_table[] = {
  256. {
  257. .callback = thinkpad_e530_quirk,
  258. .ident = "thinkpad e530",
  259. .matches = {
  260. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  261. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  262. },
  263. },
  264. {},
  265. };
  266. static int acpi_ac_add(struct acpi_device *device)
  267. {
  268. struct power_supply_config psy_cfg = {};
  269. int result = 0;
  270. struct acpi_ac *ac = NULL;
  271. if (!device)
  272. return -EINVAL;
  273. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  274. if (!ac)
  275. return -ENOMEM;
  276. ac->device = device;
  277. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  278. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  279. device->driver_data = ac;
  280. result = acpi_ac_get_state(ac);
  281. if (result)
  282. goto end;
  283. psy_cfg.drv_data = ac;
  284. ac->charger_desc.name = acpi_device_bid(device);
  285. #ifdef CONFIG_ACPI_PROCFS_POWER
  286. result = acpi_ac_add_fs(ac);
  287. if (result)
  288. goto end;
  289. #endif
  290. ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
  291. ac->charger_desc.properties = ac_props;
  292. ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
  293. ac->charger_desc.get_property = get_ac_property;
  294. ac->charger = power_supply_register(&ac->device->dev,
  295. &ac->charger_desc, &psy_cfg);
  296. if (IS_ERR(ac->charger)) {
  297. result = PTR_ERR(ac->charger);
  298. goto end;
  299. }
  300. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  301. acpi_device_name(device), acpi_device_bid(device),
  302. ac->state ? "on-line" : "off-line");
  303. ac->battery_nb.notifier_call = acpi_ac_battery_notify;
  304. register_acpi_notifier(&ac->battery_nb);
  305. end:
  306. if (result) {
  307. #ifdef CONFIG_ACPI_PROCFS_POWER
  308. acpi_ac_remove_fs(ac);
  309. #endif
  310. kfree(ac);
  311. }
  312. dmi_check_system(ac_dmi_table);
  313. return result;
  314. }
  315. #ifdef CONFIG_PM_SLEEP
  316. static int acpi_ac_resume(struct device *dev)
  317. {
  318. struct acpi_ac *ac;
  319. unsigned old_state;
  320. if (!dev)
  321. return -EINVAL;
  322. ac = acpi_driver_data(to_acpi_device(dev));
  323. if (!ac)
  324. return -EINVAL;
  325. old_state = ac->state;
  326. if (acpi_ac_get_state(ac))
  327. return 0;
  328. if (old_state != ac->state)
  329. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  330. return 0;
  331. }
  332. #else
  333. #define acpi_ac_resume NULL
  334. #endif
  335. static int acpi_ac_remove(struct acpi_device *device)
  336. {
  337. struct acpi_ac *ac = NULL;
  338. if (!device || !acpi_driver_data(device))
  339. return -EINVAL;
  340. ac = acpi_driver_data(device);
  341. power_supply_unregister(ac->charger);
  342. unregister_acpi_notifier(&ac->battery_nb);
  343. #ifdef CONFIG_ACPI_PROCFS_POWER
  344. acpi_ac_remove_fs(ac);
  345. #endif
  346. kfree(ac);
  347. return 0;
  348. }
  349. static int __init acpi_ac_init(void)
  350. {
  351. int result;
  352. if (acpi_disabled)
  353. return -ENODEV;
  354. #ifdef CONFIG_ACPI_PROCFS_POWER
  355. acpi_ac_dir = acpi_lock_ac_dir();
  356. if (!acpi_ac_dir)
  357. return -ENODEV;
  358. #endif
  359. result = acpi_bus_register_driver(&acpi_ac_driver);
  360. if (result < 0) {
  361. #ifdef CONFIG_ACPI_PROCFS_POWER
  362. acpi_unlock_ac_dir(acpi_ac_dir);
  363. #endif
  364. return -ENODEV;
  365. }
  366. return 0;
  367. }
  368. static void __exit acpi_ac_exit(void)
  369. {
  370. acpi_bus_unregister_driver(&acpi_ac_driver);
  371. #ifdef CONFIG_ACPI_PROCFS_POWER
  372. acpi_unlock_ac_dir(acpi_ac_dir);
  373. #endif
  374. }
  375. module_init(acpi_ac_init);
  376. module_exit(acpi_ac_exit);