ac.c 11 KB

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