ac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 int ac_check_pmic = 1;
  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. /* --------------------------------------------------------------------------
  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_add_fs(struct acpi_ac *ac)
  167. {
  168. struct proc_dir_entry *entry = NULL;
  169. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
  170. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  171. if (!acpi_device_dir(ac->device)) {
  172. acpi_device_dir(ac->device) =
  173. proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir);
  174. if (!acpi_device_dir(ac->device))
  175. return -ENODEV;
  176. }
  177. /* 'state' [R] */
  178. entry = proc_create_single_data(ACPI_AC_FILE_STATE, S_IRUGO,
  179. acpi_device_dir(ac->device), acpi_ac_seq_show, ac);
  180. if (!entry)
  181. return -ENODEV;
  182. return 0;
  183. }
  184. static int acpi_ac_remove_fs(struct acpi_ac *ac)
  185. {
  186. if (acpi_device_dir(ac->device)) {
  187. remove_proc_entry(ACPI_AC_FILE_STATE,
  188. acpi_device_dir(ac->device));
  189. remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir);
  190. acpi_device_dir(ac->device) = NULL;
  191. }
  192. return 0;
  193. }
  194. #endif
  195. /* --------------------------------------------------------------------------
  196. Driver Model
  197. -------------------------------------------------------------------------- */
  198. static void acpi_ac_notify(struct acpi_device *device, u32 event)
  199. {
  200. struct acpi_ac *ac = acpi_driver_data(device);
  201. if (!ac)
  202. return;
  203. switch (event) {
  204. default:
  205. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  206. "Unsupported event [0x%x]\n", event));
  207. /* fall through */
  208. case ACPI_AC_NOTIFY_STATUS:
  209. case ACPI_NOTIFY_BUS_CHECK:
  210. case ACPI_NOTIFY_DEVICE_CHECK:
  211. /*
  212. * A buggy BIOS may notify AC first and then sleep for
  213. * a specific time before doing actual operations in the
  214. * EC event handler (_Qxx). This will cause the AC state
  215. * reported by the ACPI event to be incorrect, so wait for a
  216. * specific time for the EC event handler to make progress.
  217. */
  218. if (ac_sleep_before_get_state_ms > 0)
  219. msleep(ac_sleep_before_get_state_ms);
  220. acpi_ac_get_state(ac);
  221. acpi_bus_generate_netlink_event(device->pnp.device_class,
  222. dev_name(&device->dev), event,
  223. (u32) ac->state);
  224. acpi_notifier_call_chain(device, event, (u32) ac->state);
  225. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  226. }
  227. return;
  228. }
  229. static int acpi_ac_battery_notify(struct notifier_block *nb,
  230. unsigned long action, void *data)
  231. {
  232. struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
  233. struct acpi_bus_event *event = (struct acpi_bus_event *)data;
  234. /*
  235. * On HP Pavilion dv6-6179er AC status notifications aren't triggered
  236. * when adapter is plugged/unplugged. However, battery status
  237. * notifcations are triggered when battery starts charging or
  238. * discharging. Re-reading AC status triggers lost AC notifications,
  239. * if AC status has changed.
  240. */
  241. if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
  242. event->type == ACPI_BATTERY_NOTIFY_STATUS)
  243. acpi_ac_get_state(ac);
  244. return NOTIFY_OK;
  245. }
  246. static int __init thinkpad_e530_quirk(const struct dmi_system_id *d)
  247. {
  248. ac_sleep_before_get_state_ms = 1000;
  249. return 0;
  250. }
  251. static int __init ac_do_not_check_pmic_quirk(const struct dmi_system_id *d)
  252. {
  253. ac_check_pmic = 0;
  254. return 0;
  255. }
  256. static const struct dmi_system_id ac_dmi_table[] __initconst = {
  257. {
  258. /* Thinkpad e530 */
  259. .callback = thinkpad_e530_quirk,
  260. .matches = {
  261. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  262. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  263. },
  264. },
  265. {
  266. /* ECS EF20EA */
  267. .callback = ac_do_not_check_pmic_quirk,
  268. .matches = {
  269. DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
  270. },
  271. },
  272. {
  273. /* Lenovo Ideapad Miix 320 */
  274. .callback = ac_do_not_check_pmic_quirk,
  275. .matches = {
  276. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  277. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
  278. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
  279. },
  280. },
  281. {},
  282. };
  283. static int acpi_ac_add(struct acpi_device *device)
  284. {
  285. struct power_supply_config psy_cfg = {};
  286. int result = 0;
  287. struct acpi_ac *ac = NULL;
  288. if (!device)
  289. return -EINVAL;
  290. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  291. if (!ac)
  292. return -ENOMEM;
  293. ac->device = device;
  294. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  295. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  296. device->driver_data = ac;
  297. result = acpi_ac_get_state(ac);
  298. if (result)
  299. goto end;
  300. psy_cfg.drv_data = ac;
  301. ac->charger_desc.name = acpi_device_bid(device);
  302. #ifdef CONFIG_ACPI_PROCFS_POWER
  303. result = acpi_ac_add_fs(ac);
  304. if (result)
  305. goto end;
  306. #endif
  307. ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
  308. ac->charger_desc.properties = ac_props;
  309. ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
  310. ac->charger_desc.get_property = get_ac_property;
  311. ac->charger = power_supply_register(&ac->device->dev,
  312. &ac->charger_desc, &psy_cfg);
  313. if (IS_ERR(ac->charger)) {
  314. result = PTR_ERR(ac->charger);
  315. goto end;
  316. }
  317. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  318. acpi_device_name(device), acpi_device_bid(device),
  319. ac->state ? "on-line" : "off-line");
  320. ac->battery_nb.notifier_call = acpi_ac_battery_notify;
  321. register_acpi_notifier(&ac->battery_nb);
  322. end:
  323. if (result) {
  324. #ifdef CONFIG_ACPI_PROCFS_POWER
  325. acpi_ac_remove_fs(ac);
  326. #endif
  327. kfree(ac);
  328. }
  329. return result;
  330. }
  331. #ifdef CONFIG_PM_SLEEP
  332. static int acpi_ac_resume(struct device *dev)
  333. {
  334. struct acpi_ac *ac;
  335. unsigned old_state;
  336. if (!dev)
  337. return -EINVAL;
  338. ac = acpi_driver_data(to_acpi_device(dev));
  339. if (!ac)
  340. return -EINVAL;
  341. old_state = ac->state;
  342. if (acpi_ac_get_state(ac))
  343. return 0;
  344. if (old_state != ac->state)
  345. kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
  346. return 0;
  347. }
  348. #else
  349. #define acpi_ac_resume NULL
  350. #endif
  351. static int acpi_ac_remove(struct acpi_device *device)
  352. {
  353. struct acpi_ac *ac = NULL;
  354. if (!device || !acpi_driver_data(device))
  355. return -EINVAL;
  356. ac = acpi_driver_data(device);
  357. power_supply_unregister(ac->charger);
  358. unregister_acpi_notifier(&ac->battery_nb);
  359. #ifdef CONFIG_ACPI_PROCFS_POWER
  360. acpi_ac_remove_fs(ac);
  361. #endif
  362. kfree(ac);
  363. return 0;
  364. }
  365. static int __init acpi_ac_init(void)
  366. {
  367. unsigned int i;
  368. int result;
  369. if (acpi_disabled)
  370. return -ENODEV;
  371. dmi_check_system(ac_dmi_table);
  372. if (ac_check_pmic) {
  373. for (i = 0; i < ARRAY_SIZE(acpi_ac_blacklist); i++)
  374. if (acpi_dev_present(acpi_ac_blacklist[i].hid, "1",
  375. acpi_ac_blacklist[i].hrv)) {
  376. pr_info(PREFIX "AC: found native %s PMIC, not loading\n",
  377. acpi_ac_blacklist[i].hid);
  378. return -ENODEV;
  379. }
  380. }
  381. #ifdef CONFIG_ACPI_PROCFS_POWER
  382. acpi_ac_dir = acpi_lock_ac_dir();
  383. if (!acpi_ac_dir)
  384. return -ENODEV;
  385. #endif
  386. result = acpi_bus_register_driver(&acpi_ac_driver);
  387. if (result < 0) {
  388. #ifdef CONFIG_ACPI_PROCFS_POWER
  389. acpi_unlock_ac_dir(acpi_ac_dir);
  390. #endif
  391. return -ENODEV;
  392. }
  393. return 0;
  394. }
  395. static void __exit acpi_ac_exit(void)
  396. {
  397. acpi_bus_unregister_driver(&acpi_ac_driver);
  398. #ifdef CONFIG_ACPI_PROCFS_POWER
  399. acpi_unlock_ac_dir(acpi_ac_dir);
  400. #endif
  401. }
  402. module_init(acpi_ac_init);
  403. module_exit(acpi_ac_exit);