ac.c 11 KB

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