ac.c 12 KB

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