battery.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /*
  2. * battery.c - ACPI Battery Driver (Revision: 2.0)
  3. *
  4. * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
  6. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  7. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/async.h>
  25. #include <linux/delay.h>
  26. #include <linux/dmi.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/list.h>
  30. #include <linux/module.h>
  31. #include <linux/mutex.h>
  32. #include <linux/slab.h>
  33. #include <linux/suspend.h>
  34. #include <linux/types.h>
  35. #include <asm/unaligned.h>
  36. #ifdef CONFIG_ACPI_PROCFS_POWER
  37. #include <linux/proc_fs.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/uaccess.h>
  40. #endif
  41. #include <linux/acpi.h>
  42. #include <linux/power_supply.h>
  43. #include <acpi/battery.h>
  44. #define PREFIX "ACPI: "
  45. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  46. #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
  47. ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
  48. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  49. /* Battery power unit: 0 means mW, 1 means mA */
  50. #define ACPI_BATTERY_POWER_UNIT_MA 1
  51. #define ACPI_BATTERY_STATE_DISCHARGING 0x1
  52. #define ACPI_BATTERY_STATE_CHARGING 0x2
  53. #define ACPI_BATTERY_STATE_CRITICAL 0x4
  54. #define _COMPONENT ACPI_BATTERY_COMPONENT
  55. ACPI_MODULE_NAME("battery");
  56. MODULE_AUTHOR("Paul Diefenbaugh");
  57. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  58. MODULE_DESCRIPTION("ACPI Battery Driver");
  59. MODULE_LICENSE("GPL");
  60. static async_cookie_t async_cookie;
  61. static bool battery_driver_registered;
  62. static int battery_bix_broken_package;
  63. static int battery_notification_delay_ms;
  64. static int battery_ac_is_broken;
  65. static int battery_check_pmic = 1;
  66. static unsigned int cache_time = 1000;
  67. module_param(cache_time, uint, 0644);
  68. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  69. #ifdef CONFIG_ACPI_PROCFS_POWER
  70. extern struct proc_dir_entry *acpi_lock_battery_dir(void);
  71. extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
  72. #endif
  73. static const struct acpi_device_id battery_device_ids[] = {
  74. {"PNP0C0A", 0},
  75. {"", 0},
  76. };
  77. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  78. /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
  79. static const char * const acpi_battery_blacklist[] = {
  80. "INT33F4", /* X-Powers AXP288 PMIC */
  81. };
  82. enum {
  83. ACPI_BATTERY_ALARM_PRESENT,
  84. ACPI_BATTERY_XINFO_PRESENT,
  85. ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
  86. /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
  87. switches between mWh and mAh depending on whether the system
  88. is running on battery or not. When mAh is the unit, most
  89. reported values are incorrect and need to be adjusted by
  90. 10000/design_voltage. Verified on x201, t410, t410s, and x220.
  91. Pre-2010 and 2012 models appear to always report in mWh and
  92. are thus unaffected (tested with t42, t61, t500, x200, x300,
  93. and x230). Also, in mid-2012 Lenovo issued a BIOS update for
  94. the 2011 models that fixes the issue (tested on x220 with a
  95. post-1.29 BIOS), but as of Nov. 2012, no such update is
  96. available for the 2010 models. */
  97. ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  98. /* for batteries reporting current capacity with design capacity
  99. * on a full charge, but showing degradation in full charge cap.
  100. */
  101. ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
  102. };
  103. struct acpi_battery {
  104. struct mutex lock;
  105. struct mutex sysfs_lock;
  106. struct power_supply *bat;
  107. struct power_supply_desc bat_desc;
  108. struct acpi_device *device;
  109. struct notifier_block pm_nb;
  110. struct list_head list;
  111. unsigned long update_time;
  112. int revision;
  113. int rate_now;
  114. int capacity_now;
  115. int voltage_now;
  116. int design_capacity;
  117. int full_charge_capacity;
  118. int technology;
  119. int design_voltage;
  120. int design_capacity_warning;
  121. int design_capacity_low;
  122. int cycle_count;
  123. int measurement_accuracy;
  124. int max_sampling_time;
  125. int min_sampling_time;
  126. int max_averaging_interval;
  127. int min_averaging_interval;
  128. int capacity_granularity_1;
  129. int capacity_granularity_2;
  130. int alarm;
  131. char model_number[32];
  132. char serial_number[32];
  133. char type[32];
  134. char oem_info[32];
  135. int state;
  136. int power_unit;
  137. unsigned long flags;
  138. };
  139. #define to_acpi_battery(x) power_supply_get_drvdata(x)
  140. static inline int acpi_battery_present(struct acpi_battery *battery)
  141. {
  142. return battery->device->status.battery_present;
  143. }
  144. static int acpi_battery_technology(struct acpi_battery *battery)
  145. {
  146. if (!strcasecmp("NiCd", battery->type))
  147. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  148. if (!strcasecmp("NiMH", battery->type))
  149. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  150. if (!strcasecmp("LION", battery->type))
  151. return POWER_SUPPLY_TECHNOLOGY_LION;
  152. if (!strncasecmp("LI-ION", battery->type, 6))
  153. return POWER_SUPPLY_TECHNOLOGY_LION;
  154. if (!strcasecmp("LiP", battery->type))
  155. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  156. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  157. }
  158. static int acpi_battery_get_state(struct acpi_battery *battery);
  159. static int acpi_battery_is_charged(struct acpi_battery *battery)
  160. {
  161. /* charging, discharging or critical low */
  162. if (battery->state != 0)
  163. return 0;
  164. /* battery not reporting charge */
  165. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
  166. battery->capacity_now == 0)
  167. return 0;
  168. /* good batteries update full_charge as the batteries degrade */
  169. if (battery->full_charge_capacity == battery->capacity_now)
  170. return 1;
  171. /* fallback to using design values for broken batteries */
  172. if (battery->design_capacity == battery->capacity_now)
  173. return 1;
  174. /* we don't do any sort of metric based on percentages */
  175. return 0;
  176. }
  177. static bool acpi_battery_is_degraded(struct acpi_battery *battery)
  178. {
  179. return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
  180. ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
  181. battery->full_charge_capacity < battery->design_capacity;
  182. }
  183. static int acpi_battery_handle_discharging(struct acpi_battery *battery)
  184. {
  185. /*
  186. * Some devices wrongly report discharging if the battery's charge level
  187. * was above the device's start charging threshold atm the AC adapter
  188. * was plugged in and the device thus did not start a new charge cycle.
  189. */
  190. if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
  191. battery->rate_now == 0)
  192. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  193. return POWER_SUPPLY_STATUS_DISCHARGING;
  194. }
  195. static int acpi_battery_get_property(struct power_supply *psy,
  196. enum power_supply_property psp,
  197. union power_supply_propval *val)
  198. {
  199. int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
  200. struct acpi_battery *battery = to_acpi_battery(psy);
  201. if (acpi_battery_present(battery)) {
  202. /* run battery update only if it is present */
  203. acpi_battery_get_state(battery);
  204. } else if (psp != POWER_SUPPLY_PROP_PRESENT)
  205. return -ENODEV;
  206. switch (psp) {
  207. case POWER_SUPPLY_PROP_STATUS:
  208. if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
  209. val->intval = acpi_battery_handle_discharging(battery);
  210. else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
  211. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  212. else if (acpi_battery_is_charged(battery))
  213. val->intval = POWER_SUPPLY_STATUS_FULL;
  214. else
  215. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  216. break;
  217. case POWER_SUPPLY_PROP_PRESENT:
  218. val->intval = acpi_battery_present(battery);
  219. break;
  220. case POWER_SUPPLY_PROP_TECHNOLOGY:
  221. val->intval = acpi_battery_technology(battery);
  222. break;
  223. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  224. val->intval = battery->cycle_count;
  225. break;
  226. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  227. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  228. ret = -ENODEV;
  229. else
  230. val->intval = battery->design_voltage * 1000;
  231. break;
  232. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  233. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  234. ret = -ENODEV;
  235. else
  236. val->intval = battery->voltage_now * 1000;
  237. break;
  238. case POWER_SUPPLY_PROP_CURRENT_NOW:
  239. case POWER_SUPPLY_PROP_POWER_NOW:
  240. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  241. ret = -ENODEV;
  242. else
  243. val->intval = battery->rate_now * 1000;
  244. break;
  245. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  246. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  247. if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
  248. ret = -ENODEV;
  249. else
  250. val->intval = battery->design_capacity * 1000;
  251. break;
  252. case POWER_SUPPLY_PROP_CHARGE_FULL:
  253. case POWER_SUPPLY_PROP_ENERGY_FULL:
  254. if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
  255. ret = -ENODEV;
  256. else
  257. val->intval = battery->full_charge_capacity * 1000;
  258. break;
  259. case POWER_SUPPLY_PROP_CHARGE_NOW:
  260. case POWER_SUPPLY_PROP_ENERGY_NOW:
  261. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  262. ret = -ENODEV;
  263. else
  264. val->intval = battery->capacity_now * 1000;
  265. break;
  266. case POWER_SUPPLY_PROP_CAPACITY:
  267. if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
  268. full_capacity = battery->full_charge_capacity;
  269. else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
  270. full_capacity = battery->design_capacity;
  271. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
  272. full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  273. ret = -ENODEV;
  274. else
  275. val->intval = battery->capacity_now * 100/
  276. full_capacity;
  277. break;
  278. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  279. if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
  280. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  281. else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
  282. (battery->capacity_now <= battery->alarm))
  283. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  284. else if (acpi_battery_is_charged(battery))
  285. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  286. else
  287. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  288. break;
  289. case POWER_SUPPLY_PROP_MODEL_NAME:
  290. val->strval = battery->model_number;
  291. break;
  292. case POWER_SUPPLY_PROP_MANUFACTURER:
  293. val->strval = battery->oem_info;
  294. break;
  295. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  296. val->strval = battery->serial_number;
  297. break;
  298. default:
  299. ret = -EINVAL;
  300. }
  301. return ret;
  302. }
  303. static enum power_supply_property charge_battery_props[] = {
  304. POWER_SUPPLY_PROP_STATUS,
  305. POWER_SUPPLY_PROP_PRESENT,
  306. POWER_SUPPLY_PROP_TECHNOLOGY,
  307. POWER_SUPPLY_PROP_CYCLE_COUNT,
  308. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  309. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  310. POWER_SUPPLY_PROP_CURRENT_NOW,
  311. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  312. POWER_SUPPLY_PROP_CHARGE_FULL,
  313. POWER_SUPPLY_PROP_CHARGE_NOW,
  314. POWER_SUPPLY_PROP_CAPACITY,
  315. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  316. POWER_SUPPLY_PROP_MODEL_NAME,
  317. POWER_SUPPLY_PROP_MANUFACTURER,
  318. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  319. };
  320. static enum power_supply_property charge_battery_full_cap_broken_props[] = {
  321. POWER_SUPPLY_PROP_STATUS,
  322. POWER_SUPPLY_PROP_PRESENT,
  323. POWER_SUPPLY_PROP_TECHNOLOGY,
  324. POWER_SUPPLY_PROP_CYCLE_COUNT,
  325. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  326. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  327. POWER_SUPPLY_PROP_CURRENT_NOW,
  328. POWER_SUPPLY_PROP_CHARGE_NOW,
  329. POWER_SUPPLY_PROP_MODEL_NAME,
  330. POWER_SUPPLY_PROP_MANUFACTURER,
  331. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  332. };
  333. static enum power_supply_property energy_battery_props[] = {
  334. POWER_SUPPLY_PROP_STATUS,
  335. POWER_SUPPLY_PROP_PRESENT,
  336. POWER_SUPPLY_PROP_TECHNOLOGY,
  337. POWER_SUPPLY_PROP_CYCLE_COUNT,
  338. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  339. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  340. POWER_SUPPLY_PROP_POWER_NOW,
  341. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  342. POWER_SUPPLY_PROP_ENERGY_FULL,
  343. POWER_SUPPLY_PROP_ENERGY_NOW,
  344. POWER_SUPPLY_PROP_CAPACITY,
  345. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  346. POWER_SUPPLY_PROP_MODEL_NAME,
  347. POWER_SUPPLY_PROP_MANUFACTURER,
  348. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  349. };
  350. static enum power_supply_property energy_battery_full_cap_broken_props[] = {
  351. POWER_SUPPLY_PROP_STATUS,
  352. POWER_SUPPLY_PROP_PRESENT,
  353. POWER_SUPPLY_PROP_TECHNOLOGY,
  354. POWER_SUPPLY_PROP_CYCLE_COUNT,
  355. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  356. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  357. POWER_SUPPLY_PROP_POWER_NOW,
  358. POWER_SUPPLY_PROP_ENERGY_NOW,
  359. POWER_SUPPLY_PROP_MODEL_NAME,
  360. POWER_SUPPLY_PROP_MANUFACTURER,
  361. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  362. };
  363. /* --------------------------------------------------------------------------
  364. Battery Management
  365. -------------------------------------------------------------------------- */
  366. struct acpi_offsets {
  367. size_t offset; /* offset inside struct acpi_sbs_battery */
  368. u8 mode; /* int or string? */
  369. };
  370. static const struct acpi_offsets state_offsets[] = {
  371. {offsetof(struct acpi_battery, state), 0},
  372. {offsetof(struct acpi_battery, rate_now), 0},
  373. {offsetof(struct acpi_battery, capacity_now), 0},
  374. {offsetof(struct acpi_battery, voltage_now), 0},
  375. };
  376. static const struct acpi_offsets info_offsets[] = {
  377. {offsetof(struct acpi_battery, power_unit), 0},
  378. {offsetof(struct acpi_battery, design_capacity), 0},
  379. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  380. {offsetof(struct acpi_battery, technology), 0},
  381. {offsetof(struct acpi_battery, design_voltage), 0},
  382. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  383. {offsetof(struct acpi_battery, design_capacity_low), 0},
  384. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  385. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  386. {offsetof(struct acpi_battery, model_number), 1},
  387. {offsetof(struct acpi_battery, serial_number), 1},
  388. {offsetof(struct acpi_battery, type), 1},
  389. {offsetof(struct acpi_battery, oem_info), 1},
  390. };
  391. static const struct acpi_offsets extended_info_offsets[] = {
  392. {offsetof(struct acpi_battery, revision), 0},
  393. {offsetof(struct acpi_battery, power_unit), 0},
  394. {offsetof(struct acpi_battery, design_capacity), 0},
  395. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  396. {offsetof(struct acpi_battery, technology), 0},
  397. {offsetof(struct acpi_battery, design_voltage), 0},
  398. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  399. {offsetof(struct acpi_battery, design_capacity_low), 0},
  400. {offsetof(struct acpi_battery, cycle_count), 0},
  401. {offsetof(struct acpi_battery, measurement_accuracy), 0},
  402. {offsetof(struct acpi_battery, max_sampling_time), 0},
  403. {offsetof(struct acpi_battery, min_sampling_time), 0},
  404. {offsetof(struct acpi_battery, max_averaging_interval), 0},
  405. {offsetof(struct acpi_battery, min_averaging_interval), 0},
  406. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  407. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  408. {offsetof(struct acpi_battery, model_number), 1},
  409. {offsetof(struct acpi_battery, serial_number), 1},
  410. {offsetof(struct acpi_battery, type), 1},
  411. {offsetof(struct acpi_battery, oem_info), 1},
  412. };
  413. static int extract_package(struct acpi_battery *battery,
  414. union acpi_object *package,
  415. const struct acpi_offsets *offsets, int num)
  416. {
  417. int i;
  418. union acpi_object *element;
  419. if (package->type != ACPI_TYPE_PACKAGE)
  420. return -EFAULT;
  421. for (i = 0; i < num; ++i) {
  422. if (package->package.count <= i)
  423. return -EFAULT;
  424. element = &package->package.elements[i];
  425. if (offsets[i].mode) {
  426. u8 *ptr = (u8 *)battery + offsets[i].offset;
  427. if (element->type == ACPI_TYPE_STRING ||
  428. element->type == ACPI_TYPE_BUFFER)
  429. strncpy(ptr, element->string.pointer, 32);
  430. else if (element->type == ACPI_TYPE_INTEGER) {
  431. strncpy(ptr, (u8 *)&element->integer.value,
  432. sizeof(u64));
  433. ptr[sizeof(u64)] = 0;
  434. } else
  435. *ptr = 0; /* don't have value */
  436. } else {
  437. int *x = (int *)((u8 *)battery + offsets[i].offset);
  438. *x = (element->type == ACPI_TYPE_INTEGER) ?
  439. element->integer.value : -1;
  440. }
  441. }
  442. return 0;
  443. }
  444. static int acpi_battery_get_status(struct acpi_battery *battery)
  445. {
  446. if (acpi_bus_get_status(battery->device)) {
  447. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  448. return -ENODEV;
  449. }
  450. return 0;
  451. }
  452. static int extract_battery_info(const int use_bix,
  453. struct acpi_battery *battery,
  454. const struct acpi_buffer *buffer)
  455. {
  456. int result = -EFAULT;
  457. if (use_bix && battery_bix_broken_package)
  458. result = extract_package(battery, buffer->pointer,
  459. extended_info_offsets + 1,
  460. ARRAY_SIZE(extended_info_offsets) - 1);
  461. else if (use_bix)
  462. result = extract_package(battery, buffer->pointer,
  463. extended_info_offsets,
  464. ARRAY_SIZE(extended_info_offsets));
  465. else
  466. result = extract_package(battery, buffer->pointer,
  467. info_offsets, ARRAY_SIZE(info_offsets));
  468. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
  469. battery->full_charge_capacity = battery->design_capacity;
  470. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
  471. battery->power_unit && battery->design_voltage) {
  472. battery->design_capacity = battery->design_capacity *
  473. 10000 / battery->design_voltage;
  474. battery->full_charge_capacity = battery->full_charge_capacity *
  475. 10000 / battery->design_voltage;
  476. battery->design_capacity_warning =
  477. battery->design_capacity_warning *
  478. 10000 / battery->design_voltage;
  479. /* Curiously, design_capacity_low, unlike the rest of them,
  480. is correct. */
  481. /* capacity_granularity_* equal 1 on the systems tested, so
  482. it's impossible to tell if they would need an adjustment
  483. or not if their values were higher. */
  484. }
  485. if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
  486. battery->capacity_now > battery->full_charge_capacity)
  487. battery->capacity_now = battery->full_charge_capacity;
  488. return result;
  489. }
  490. static int acpi_battery_get_info(struct acpi_battery *battery)
  491. {
  492. const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  493. int use_bix;
  494. int result = -ENODEV;
  495. if (!acpi_battery_present(battery))
  496. return 0;
  497. for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
  498. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  499. acpi_status status = AE_ERROR;
  500. mutex_lock(&battery->lock);
  501. status = acpi_evaluate_object(battery->device->handle,
  502. use_bix ? "_BIX":"_BIF",
  503. NULL, &buffer);
  504. mutex_unlock(&battery->lock);
  505. if (ACPI_FAILURE(status)) {
  506. ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
  507. use_bix ? "_BIX":"_BIF"));
  508. } else {
  509. result = extract_battery_info(use_bix,
  510. battery,
  511. &buffer);
  512. kfree(buffer.pointer);
  513. break;
  514. }
  515. }
  516. if (!result && !use_bix && xinfo)
  517. pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
  518. return result;
  519. }
  520. static int acpi_battery_get_state(struct acpi_battery *battery)
  521. {
  522. int result = 0;
  523. acpi_status status = 0;
  524. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  525. if (!acpi_battery_present(battery))
  526. return 0;
  527. if (battery->update_time &&
  528. time_before(jiffies, battery->update_time +
  529. msecs_to_jiffies(cache_time)))
  530. return 0;
  531. mutex_lock(&battery->lock);
  532. status = acpi_evaluate_object(battery->device->handle, "_BST",
  533. NULL, &buffer);
  534. mutex_unlock(&battery->lock);
  535. if (ACPI_FAILURE(status)) {
  536. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  537. return -ENODEV;
  538. }
  539. result = extract_package(battery, buffer.pointer,
  540. state_offsets, ARRAY_SIZE(state_offsets));
  541. battery->update_time = jiffies;
  542. kfree(buffer.pointer);
  543. /* For buggy DSDTs that report negative 16-bit values for either
  544. * charging or discharging current and/or report 0 as 65536
  545. * due to bad math.
  546. */
  547. if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
  548. battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
  549. (s16)(battery->rate_now) < 0) {
  550. battery->rate_now = abs((s16)battery->rate_now);
  551. pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
  552. }
  553. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
  554. && battery->capacity_now >= 0 && battery->capacity_now <= 100)
  555. battery->capacity_now = (battery->capacity_now *
  556. battery->full_charge_capacity) / 100;
  557. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
  558. battery->power_unit && battery->design_voltage) {
  559. battery->capacity_now = battery->capacity_now *
  560. 10000 / battery->design_voltage;
  561. }
  562. if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
  563. battery->capacity_now > battery->full_charge_capacity)
  564. battery->capacity_now = battery->full_charge_capacity;
  565. return result;
  566. }
  567. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  568. {
  569. acpi_status status = 0;
  570. if (!acpi_battery_present(battery) ||
  571. !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
  572. return -ENODEV;
  573. mutex_lock(&battery->lock);
  574. status = acpi_execute_simple_method(battery->device->handle, "_BTP",
  575. battery->alarm);
  576. mutex_unlock(&battery->lock);
  577. if (ACPI_FAILURE(status))
  578. return -ENODEV;
  579. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  580. return 0;
  581. }
  582. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  583. {
  584. /* See if alarms are supported, and if so, set default */
  585. if (!acpi_has_method(battery->device->handle, "_BTP")) {
  586. clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  587. return 0;
  588. }
  589. set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  590. if (!battery->alarm)
  591. battery->alarm = battery->design_capacity_warning;
  592. return acpi_battery_set_alarm(battery);
  593. }
  594. static ssize_t acpi_battery_alarm_show(struct device *dev,
  595. struct device_attribute *attr,
  596. char *buf)
  597. {
  598. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  599. return sprintf(buf, "%d\n", battery->alarm * 1000);
  600. }
  601. static ssize_t acpi_battery_alarm_store(struct device *dev,
  602. struct device_attribute *attr,
  603. const char *buf, size_t count)
  604. {
  605. unsigned long x;
  606. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  607. if (sscanf(buf, "%lu\n", &x) == 1)
  608. battery->alarm = x/1000;
  609. if (acpi_battery_present(battery))
  610. acpi_battery_set_alarm(battery);
  611. return count;
  612. }
  613. static const struct device_attribute alarm_attr = {
  614. .attr = {.name = "alarm", .mode = 0644},
  615. .show = acpi_battery_alarm_show,
  616. .store = acpi_battery_alarm_store,
  617. };
  618. /*
  619. * The Battery Hooking API
  620. *
  621. * This API is used inside other drivers that need to expose
  622. * platform-specific behaviour within the generic driver in a
  623. * generic way.
  624. *
  625. */
  626. static LIST_HEAD(acpi_battery_list);
  627. static LIST_HEAD(battery_hook_list);
  628. static DEFINE_MUTEX(hook_mutex);
  629. static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
  630. {
  631. struct acpi_battery *battery;
  632. /*
  633. * In order to remove a hook, we first need to
  634. * de-register all the batteries that are registered.
  635. */
  636. if (lock)
  637. mutex_lock(&hook_mutex);
  638. list_for_each_entry(battery, &acpi_battery_list, list) {
  639. hook->remove_battery(battery->bat);
  640. }
  641. list_del(&hook->list);
  642. if (lock)
  643. mutex_unlock(&hook_mutex);
  644. pr_info("extension unregistered: %s\n", hook->name);
  645. }
  646. void battery_hook_unregister(struct acpi_battery_hook *hook)
  647. {
  648. __battery_hook_unregister(hook, 1);
  649. }
  650. EXPORT_SYMBOL_GPL(battery_hook_unregister);
  651. void battery_hook_register(struct acpi_battery_hook *hook)
  652. {
  653. struct acpi_battery *battery;
  654. mutex_lock(&hook_mutex);
  655. INIT_LIST_HEAD(&hook->list);
  656. list_add(&hook->list, &battery_hook_list);
  657. /*
  658. * Now that the driver is registered, we need
  659. * to notify the hook that a battery is available
  660. * for each battery, so that the driver may add
  661. * its attributes.
  662. */
  663. list_for_each_entry(battery, &acpi_battery_list, list) {
  664. if (hook->add_battery(battery->bat)) {
  665. /*
  666. * If a add-battery returns non-zero,
  667. * the registration of the extension has failed,
  668. * and we will not add it to the list of loaded
  669. * hooks.
  670. */
  671. pr_err("extension failed to load: %s", hook->name);
  672. __battery_hook_unregister(hook, 0);
  673. goto end;
  674. }
  675. }
  676. pr_info("new extension: %s\n", hook->name);
  677. end:
  678. mutex_unlock(&hook_mutex);
  679. }
  680. EXPORT_SYMBOL_GPL(battery_hook_register);
  681. /*
  682. * This function gets called right after the battery sysfs
  683. * attributes have been added, so that the drivers that
  684. * define custom sysfs attributes can add their own.
  685. */
  686. static void battery_hook_add_battery(struct acpi_battery *battery)
  687. {
  688. struct acpi_battery_hook *hook_node, *tmp;
  689. mutex_lock(&hook_mutex);
  690. INIT_LIST_HEAD(&battery->list);
  691. list_add(&battery->list, &acpi_battery_list);
  692. /*
  693. * Since we added a new battery to the list, we need to
  694. * iterate over the hooks and call add_battery for each
  695. * hook that was registered. This usually happens
  696. * when a battery gets hotplugged or initialized
  697. * during the battery module initialization.
  698. */
  699. list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
  700. if (hook_node->add_battery(battery->bat)) {
  701. /*
  702. * The notification of the extensions has failed, to
  703. * prevent further errors we will unload the extension.
  704. */
  705. pr_err("error in extension, unloading: %s",
  706. hook_node->name);
  707. __battery_hook_unregister(hook_node, 0);
  708. }
  709. }
  710. mutex_unlock(&hook_mutex);
  711. }
  712. static void battery_hook_remove_battery(struct acpi_battery *battery)
  713. {
  714. struct acpi_battery_hook *hook;
  715. mutex_lock(&hook_mutex);
  716. /*
  717. * Before removing the hook, we need to remove all
  718. * custom attributes from the battery.
  719. */
  720. list_for_each_entry(hook, &battery_hook_list, list) {
  721. hook->remove_battery(battery->bat);
  722. }
  723. /* Then, just remove the battery from the list */
  724. list_del(&battery->list);
  725. mutex_unlock(&hook_mutex);
  726. }
  727. static void __exit battery_hook_exit(void)
  728. {
  729. struct acpi_battery_hook *hook;
  730. struct acpi_battery_hook *ptr;
  731. /*
  732. * At this point, the acpi_bus_unregister_driver()
  733. * has called remove for all batteries. We just
  734. * need to remove the hooks.
  735. */
  736. list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
  737. __battery_hook_unregister(hook, 1);
  738. }
  739. mutex_destroy(&hook_mutex);
  740. }
  741. static int sysfs_add_battery(struct acpi_battery *battery)
  742. {
  743. struct power_supply_config psy_cfg = { .drv_data = battery, };
  744. bool full_cap_broken = false;
  745. if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
  746. !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
  747. full_cap_broken = true;
  748. if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
  749. if (full_cap_broken) {
  750. battery->bat_desc.properties =
  751. charge_battery_full_cap_broken_props;
  752. battery->bat_desc.num_properties =
  753. ARRAY_SIZE(charge_battery_full_cap_broken_props);
  754. } else {
  755. battery->bat_desc.properties = charge_battery_props;
  756. battery->bat_desc.num_properties =
  757. ARRAY_SIZE(charge_battery_props);
  758. }
  759. } else {
  760. if (full_cap_broken) {
  761. battery->bat_desc.properties =
  762. energy_battery_full_cap_broken_props;
  763. battery->bat_desc.num_properties =
  764. ARRAY_SIZE(energy_battery_full_cap_broken_props);
  765. } else {
  766. battery->bat_desc.properties = energy_battery_props;
  767. battery->bat_desc.num_properties =
  768. ARRAY_SIZE(energy_battery_props);
  769. }
  770. }
  771. battery->bat_desc.name = acpi_device_bid(battery->device);
  772. battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  773. battery->bat_desc.get_property = acpi_battery_get_property;
  774. battery->bat = power_supply_register_no_ws(&battery->device->dev,
  775. &battery->bat_desc, &psy_cfg);
  776. if (IS_ERR(battery->bat)) {
  777. int result = PTR_ERR(battery->bat);
  778. battery->bat = NULL;
  779. return result;
  780. }
  781. battery_hook_add_battery(battery);
  782. return device_create_file(&battery->bat->dev, &alarm_attr);
  783. }
  784. static void sysfs_remove_battery(struct acpi_battery *battery)
  785. {
  786. mutex_lock(&battery->sysfs_lock);
  787. if (!battery->bat) {
  788. mutex_unlock(&battery->sysfs_lock);
  789. return;
  790. }
  791. battery_hook_remove_battery(battery);
  792. device_remove_file(&battery->bat->dev, &alarm_attr);
  793. power_supply_unregister(battery->bat);
  794. battery->bat = NULL;
  795. mutex_unlock(&battery->sysfs_lock);
  796. }
  797. static void find_battery(const struct dmi_header *dm, void *private)
  798. {
  799. struct acpi_battery *battery = (struct acpi_battery *)private;
  800. /* Note: the hardcoded offsets below have been extracted from
  801. the source code of dmidecode. */
  802. if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
  803. const u8 *dmi_data = (const u8 *)(dm + 1);
  804. int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
  805. if (dm->length >= 18)
  806. dmi_capacity *= dmi_data[17];
  807. if (battery->design_capacity * battery->design_voltage / 1000
  808. != dmi_capacity &&
  809. battery->design_capacity * 10 == dmi_capacity)
  810. set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  811. &battery->flags);
  812. }
  813. }
  814. /*
  815. * According to the ACPI spec, some kinds of primary batteries can
  816. * report percentage battery remaining capacity directly to OS.
  817. * In this case, it reports the Last Full Charged Capacity == 100
  818. * and BatteryPresentRate == 0xFFFFFFFF.
  819. *
  820. * Now we found some battery reports percentage remaining capacity
  821. * even if it's rechargeable.
  822. * https://bugzilla.kernel.org/show_bug.cgi?id=15979
  823. *
  824. * Handle this correctly so that they won't break userspace.
  825. */
  826. static void acpi_battery_quirks(struct acpi_battery *battery)
  827. {
  828. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
  829. return;
  830. if (battery->full_charge_capacity == 100 &&
  831. battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
  832. battery->capacity_now >= 0 && battery->capacity_now <= 100) {
  833. set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
  834. battery->full_charge_capacity = battery->design_capacity;
  835. battery->capacity_now = (battery->capacity_now *
  836. battery->full_charge_capacity) / 100;
  837. }
  838. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
  839. return;
  840. if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
  841. const char *s;
  842. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  843. if (s && !strncasecmp(s, "ThinkPad", 8)) {
  844. dmi_walk(find_battery, battery);
  845. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  846. &battery->flags) &&
  847. battery->design_voltage) {
  848. battery->design_capacity =
  849. battery->design_capacity *
  850. 10000 / battery->design_voltage;
  851. battery->full_charge_capacity =
  852. battery->full_charge_capacity *
  853. 10000 / battery->design_voltage;
  854. battery->design_capacity_warning =
  855. battery->design_capacity_warning *
  856. 10000 / battery->design_voltage;
  857. battery->capacity_now = battery->capacity_now *
  858. 10000 / battery->design_voltage;
  859. }
  860. }
  861. }
  862. if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
  863. return;
  864. if (acpi_battery_is_degraded(battery) &&
  865. battery->capacity_now > battery->full_charge_capacity) {
  866. set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
  867. battery->capacity_now = battery->full_charge_capacity;
  868. }
  869. }
  870. static int acpi_battery_update(struct acpi_battery *battery, bool resume)
  871. {
  872. int result = acpi_battery_get_status(battery);
  873. if (result)
  874. return result;
  875. if (!acpi_battery_present(battery)) {
  876. sysfs_remove_battery(battery);
  877. battery->update_time = 0;
  878. return 0;
  879. }
  880. if (resume)
  881. return 0;
  882. if (!battery->update_time) {
  883. result = acpi_battery_get_info(battery);
  884. if (result)
  885. return result;
  886. acpi_battery_init_alarm(battery);
  887. }
  888. result = acpi_battery_get_state(battery);
  889. if (result)
  890. return result;
  891. acpi_battery_quirks(battery);
  892. if (!battery->bat) {
  893. result = sysfs_add_battery(battery);
  894. if (result)
  895. return result;
  896. }
  897. /*
  898. * Wakeup the system if battery is critical low
  899. * or lower than the alarm level
  900. */
  901. if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
  902. (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
  903. (battery->capacity_now <= battery->alarm)))
  904. acpi_pm_wakeup_event(&battery->device->dev);
  905. return result;
  906. }
  907. static void acpi_battery_refresh(struct acpi_battery *battery)
  908. {
  909. int power_unit;
  910. if (!battery->bat)
  911. return;
  912. power_unit = battery->power_unit;
  913. acpi_battery_get_info(battery);
  914. if (power_unit == battery->power_unit)
  915. return;
  916. /* The battery has changed its reporting units. */
  917. sysfs_remove_battery(battery);
  918. sysfs_add_battery(battery);
  919. }
  920. /* --------------------------------------------------------------------------
  921. FS Interface (/proc)
  922. -------------------------------------------------------------------------- */
  923. #ifdef CONFIG_ACPI_PROCFS_POWER
  924. static struct proc_dir_entry *acpi_battery_dir;
  925. static const char *acpi_battery_units(const struct acpi_battery *battery)
  926. {
  927. return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
  928. "mA" : "mW";
  929. }
  930. static int acpi_battery_info_proc_show(struct seq_file *seq, void *offset)
  931. {
  932. struct acpi_battery *battery = seq->private;
  933. int result = acpi_battery_update(battery, false);
  934. if (result)
  935. goto end;
  936. seq_printf(seq, "present: %s\n",
  937. acpi_battery_present(battery) ? "yes" : "no");
  938. if (!acpi_battery_present(battery))
  939. goto end;
  940. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  941. seq_printf(seq, "design capacity: unknown\n");
  942. else
  943. seq_printf(seq, "design capacity: %d %sh\n",
  944. battery->design_capacity,
  945. acpi_battery_units(battery));
  946. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  947. seq_printf(seq, "last full capacity: unknown\n");
  948. else
  949. seq_printf(seq, "last full capacity: %d %sh\n",
  950. battery->full_charge_capacity,
  951. acpi_battery_units(battery));
  952. seq_printf(seq, "battery technology: %srechargeable\n",
  953. battery->technology ? "" : "non-");
  954. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  955. seq_printf(seq, "design voltage: unknown\n");
  956. else
  957. seq_printf(seq, "design voltage: %d mV\n",
  958. battery->design_voltage);
  959. seq_printf(seq, "design capacity warning: %d %sh\n",
  960. battery->design_capacity_warning,
  961. acpi_battery_units(battery));
  962. seq_printf(seq, "design capacity low: %d %sh\n",
  963. battery->design_capacity_low,
  964. acpi_battery_units(battery));
  965. seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
  966. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  967. battery->capacity_granularity_1,
  968. acpi_battery_units(battery));
  969. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  970. battery->capacity_granularity_2,
  971. acpi_battery_units(battery));
  972. seq_printf(seq, "model number: %s\n", battery->model_number);
  973. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  974. seq_printf(seq, "battery type: %s\n", battery->type);
  975. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  976. end:
  977. if (result)
  978. seq_printf(seq, "ERROR: Unable to read battery info\n");
  979. return result;
  980. }
  981. static int acpi_battery_state_proc_show(struct seq_file *seq, void *offset)
  982. {
  983. struct acpi_battery *battery = seq->private;
  984. int result = acpi_battery_update(battery, false);
  985. if (result)
  986. goto end;
  987. seq_printf(seq, "present: %s\n",
  988. acpi_battery_present(battery) ? "yes" : "no");
  989. if (!acpi_battery_present(battery))
  990. goto end;
  991. seq_printf(seq, "capacity state: %s\n",
  992. (battery->state & 0x04) ? "critical" : "ok");
  993. if ((battery->state & 0x01) && (battery->state & 0x02))
  994. seq_printf(seq,
  995. "charging state: charging/discharging\n");
  996. else if (battery->state & 0x01)
  997. seq_printf(seq, "charging state: discharging\n");
  998. else if (battery->state & 0x02)
  999. seq_printf(seq, "charging state: charging\n");
  1000. else
  1001. seq_printf(seq, "charging state: charged\n");
  1002. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  1003. seq_printf(seq, "present rate: unknown\n");
  1004. else
  1005. seq_printf(seq, "present rate: %d %s\n",
  1006. battery->rate_now, acpi_battery_units(battery));
  1007. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  1008. seq_printf(seq, "remaining capacity: unknown\n");
  1009. else
  1010. seq_printf(seq, "remaining capacity: %d %sh\n",
  1011. battery->capacity_now, acpi_battery_units(battery));
  1012. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  1013. seq_printf(seq, "present voltage: unknown\n");
  1014. else
  1015. seq_printf(seq, "present voltage: %d mV\n",
  1016. battery->voltage_now);
  1017. end:
  1018. if (result)
  1019. seq_printf(seq, "ERROR: Unable to read battery state\n");
  1020. return result;
  1021. }
  1022. static int acpi_battery_alarm_proc_show(struct seq_file *seq, void *offset)
  1023. {
  1024. struct acpi_battery *battery = seq->private;
  1025. int result = acpi_battery_update(battery, false);
  1026. if (result)
  1027. goto end;
  1028. if (!acpi_battery_present(battery)) {
  1029. seq_printf(seq, "present: no\n");
  1030. goto end;
  1031. }
  1032. seq_printf(seq, "alarm: ");
  1033. if (battery->alarm) {
  1034. seq_printf(seq, "%u %sh\n", battery->alarm,
  1035. acpi_battery_units(battery));
  1036. } else {
  1037. seq_printf(seq, "unsupported\n");
  1038. }
  1039. end:
  1040. if (result)
  1041. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  1042. return result;
  1043. }
  1044. static ssize_t acpi_battery_write_alarm(struct file *file,
  1045. const char __user * buffer,
  1046. size_t count, loff_t * ppos)
  1047. {
  1048. int result = 0;
  1049. char alarm_string[12] = { '\0' };
  1050. struct seq_file *m = file->private_data;
  1051. struct acpi_battery *battery = m->private;
  1052. if (!battery || (count > sizeof(alarm_string) - 1))
  1053. return -EINVAL;
  1054. if (!acpi_battery_present(battery)) {
  1055. result = -ENODEV;
  1056. goto end;
  1057. }
  1058. if (copy_from_user(alarm_string, buffer, count)) {
  1059. result = -EFAULT;
  1060. goto end;
  1061. }
  1062. alarm_string[count] = '\0';
  1063. if (kstrtoint(alarm_string, 0, &battery->alarm)) {
  1064. result = -EINVAL;
  1065. goto end;
  1066. }
  1067. result = acpi_battery_set_alarm(battery);
  1068. end:
  1069. if (result)
  1070. return result;
  1071. return count;
  1072. }
  1073. static int acpi_battery_alarm_proc_open(struct inode *inode, struct file *file)
  1074. {
  1075. return single_open(file, acpi_battery_alarm_proc_show, PDE_DATA(inode));
  1076. }
  1077. static const struct file_operations acpi_battery_alarm_fops = {
  1078. .owner = THIS_MODULE,
  1079. .open = acpi_battery_alarm_proc_open,
  1080. .read = seq_read,
  1081. .write = acpi_battery_write_alarm,
  1082. .llseek = seq_lseek,
  1083. .release = single_release,
  1084. };
  1085. static int acpi_battery_add_fs(struct acpi_device *device)
  1086. {
  1087. pr_warning(PREFIX "Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  1088. if (!acpi_device_dir(device)) {
  1089. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1090. acpi_battery_dir);
  1091. if (!acpi_device_dir(device))
  1092. return -ENODEV;
  1093. }
  1094. if (!proc_create_single_data("info", S_IRUGO, acpi_device_dir(device),
  1095. acpi_battery_info_proc_show, acpi_driver_data(device)))
  1096. return -ENODEV;
  1097. if (!proc_create_single_data("state", S_IRUGO, acpi_device_dir(device),
  1098. acpi_battery_state_proc_show, acpi_driver_data(device)))
  1099. return -ENODEV;
  1100. if (!proc_create_data("alarm", S_IFREG | S_IRUGO | S_IWUSR,
  1101. acpi_device_dir(device), &acpi_battery_alarm_fops,
  1102. acpi_driver_data(device)))
  1103. return -ENODEV;
  1104. return 0;
  1105. }
  1106. static void acpi_battery_remove_fs(struct acpi_device *device)
  1107. {
  1108. if (!acpi_device_dir(device))
  1109. return;
  1110. remove_proc_subtree(acpi_device_bid(device), acpi_battery_dir);
  1111. acpi_device_dir(device) = NULL;
  1112. }
  1113. #endif
  1114. /* --------------------------------------------------------------------------
  1115. Driver Interface
  1116. -------------------------------------------------------------------------- */
  1117. static void acpi_battery_notify(struct acpi_device *device, u32 event)
  1118. {
  1119. struct acpi_battery *battery = acpi_driver_data(device);
  1120. struct power_supply *old;
  1121. if (!battery)
  1122. return;
  1123. old = battery->bat;
  1124. /*
  1125. * On Acer Aspire V5-573G notifications are sometimes triggered too
  1126. * early. For example, when AC is unplugged and notification is
  1127. * triggered, battery state is still reported as "Full", and changes to
  1128. * "Discharging" only after short delay, without any notification.
  1129. */
  1130. if (battery_notification_delay_ms > 0)
  1131. msleep(battery_notification_delay_ms);
  1132. if (event == ACPI_BATTERY_NOTIFY_INFO)
  1133. acpi_battery_refresh(battery);
  1134. acpi_battery_update(battery, false);
  1135. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1136. dev_name(&device->dev), event,
  1137. acpi_battery_present(battery));
  1138. acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
  1139. /* acpi_battery_update could remove power_supply object */
  1140. if (old && battery->bat)
  1141. power_supply_changed(battery->bat);
  1142. }
  1143. static int battery_notify(struct notifier_block *nb,
  1144. unsigned long mode, void *_unused)
  1145. {
  1146. struct acpi_battery *battery = container_of(nb, struct acpi_battery,
  1147. pm_nb);
  1148. int result;
  1149. switch (mode) {
  1150. case PM_POST_HIBERNATION:
  1151. case PM_POST_SUSPEND:
  1152. if (!acpi_battery_present(battery))
  1153. return 0;
  1154. if (battery->bat) {
  1155. acpi_battery_refresh(battery);
  1156. } else {
  1157. result = acpi_battery_get_info(battery);
  1158. if (result)
  1159. return result;
  1160. result = sysfs_add_battery(battery);
  1161. if (result)
  1162. return result;
  1163. }
  1164. acpi_battery_init_alarm(battery);
  1165. acpi_battery_get_state(battery);
  1166. break;
  1167. }
  1168. return 0;
  1169. }
  1170. static int __init
  1171. battery_bix_broken_package_quirk(const struct dmi_system_id *d)
  1172. {
  1173. battery_bix_broken_package = 1;
  1174. return 0;
  1175. }
  1176. static int __init
  1177. battery_notification_delay_quirk(const struct dmi_system_id *d)
  1178. {
  1179. battery_notification_delay_ms = 1000;
  1180. return 0;
  1181. }
  1182. static int __init
  1183. battery_ac_is_broken_quirk(const struct dmi_system_id *d)
  1184. {
  1185. battery_ac_is_broken = 1;
  1186. return 0;
  1187. }
  1188. static int __init
  1189. battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
  1190. {
  1191. battery_check_pmic = 0;
  1192. return 0;
  1193. }
  1194. static const struct dmi_system_id bat_dmi_table[] __initconst = {
  1195. {
  1196. /* NEC LZ750/LS */
  1197. .callback = battery_bix_broken_package_quirk,
  1198. .matches = {
  1199. DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
  1200. DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
  1201. },
  1202. },
  1203. {
  1204. /* Acer Aspire V5-573G */
  1205. .callback = battery_notification_delay_quirk,
  1206. .matches = {
  1207. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  1208. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
  1209. },
  1210. },
  1211. {
  1212. /* Point of View mobii wintab p800w */
  1213. .callback = battery_ac_is_broken_quirk,
  1214. .matches = {
  1215. DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
  1216. DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
  1217. DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
  1218. /* Above matches are too generic, add bios-date match */
  1219. DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
  1220. },
  1221. },
  1222. {
  1223. /* ECS EF20EA */
  1224. .callback = battery_do_not_check_pmic_quirk,
  1225. .matches = {
  1226. DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
  1227. },
  1228. },
  1229. {
  1230. /* Lenovo Ideapad Miix 320 */
  1231. .callback = battery_do_not_check_pmic_quirk,
  1232. .matches = {
  1233. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1234. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
  1235. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
  1236. },
  1237. },
  1238. {},
  1239. };
  1240. /*
  1241. * Some machines'(E,G Lenovo Z480) ECs are not stable
  1242. * during boot up and this causes battery driver fails to be
  1243. * probed due to failure of getting battery information
  1244. * from EC sometimes. After several retries, the operation
  1245. * may work. So add retry code here and 20ms sleep between
  1246. * every retries.
  1247. */
  1248. static int acpi_battery_update_retry(struct acpi_battery *battery)
  1249. {
  1250. int retry, ret;
  1251. for (retry = 5; retry; retry--) {
  1252. ret = acpi_battery_update(battery, false);
  1253. if (!ret)
  1254. break;
  1255. msleep(20);
  1256. }
  1257. return ret;
  1258. }
  1259. static int acpi_battery_add(struct acpi_device *device)
  1260. {
  1261. int result = 0;
  1262. struct acpi_battery *battery = NULL;
  1263. if (!device)
  1264. return -EINVAL;
  1265. if (device->dep_unmet)
  1266. return -EPROBE_DEFER;
  1267. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  1268. if (!battery)
  1269. return -ENOMEM;
  1270. battery->device = device;
  1271. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  1272. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  1273. device->driver_data = battery;
  1274. mutex_init(&battery->lock);
  1275. mutex_init(&battery->sysfs_lock);
  1276. if (acpi_has_method(battery->device->handle, "_BIX"))
  1277. set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  1278. result = acpi_battery_update_retry(battery);
  1279. if (result)
  1280. goto fail;
  1281. #ifdef CONFIG_ACPI_PROCFS_POWER
  1282. result = acpi_battery_add_fs(device);
  1283. if (result) {
  1284. acpi_battery_remove_fs(device);
  1285. goto fail;
  1286. }
  1287. #endif
  1288. pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
  1289. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  1290. device->status.battery_present ? "present" : "absent");
  1291. battery->pm_nb.notifier_call = battery_notify;
  1292. register_pm_notifier(&battery->pm_nb);
  1293. device_init_wakeup(&device->dev, 1);
  1294. return result;
  1295. fail:
  1296. sysfs_remove_battery(battery);
  1297. mutex_destroy(&battery->lock);
  1298. mutex_destroy(&battery->sysfs_lock);
  1299. kfree(battery);
  1300. return result;
  1301. }
  1302. static int acpi_battery_remove(struct acpi_device *device)
  1303. {
  1304. struct acpi_battery *battery = NULL;
  1305. if (!device || !acpi_driver_data(device))
  1306. return -EINVAL;
  1307. device_init_wakeup(&device->dev, 0);
  1308. battery = acpi_driver_data(device);
  1309. unregister_pm_notifier(&battery->pm_nb);
  1310. #ifdef CONFIG_ACPI_PROCFS_POWER
  1311. acpi_battery_remove_fs(device);
  1312. #endif
  1313. sysfs_remove_battery(battery);
  1314. mutex_destroy(&battery->lock);
  1315. mutex_destroy(&battery->sysfs_lock);
  1316. kfree(battery);
  1317. return 0;
  1318. }
  1319. #ifdef CONFIG_PM_SLEEP
  1320. /* this is needed to learn about changes made in suspended state */
  1321. static int acpi_battery_resume(struct device *dev)
  1322. {
  1323. struct acpi_battery *battery;
  1324. if (!dev)
  1325. return -EINVAL;
  1326. battery = acpi_driver_data(to_acpi_device(dev));
  1327. if (!battery)
  1328. return -EINVAL;
  1329. battery->update_time = 0;
  1330. acpi_battery_update(battery, true);
  1331. return 0;
  1332. }
  1333. #else
  1334. #define acpi_battery_resume NULL
  1335. #endif
  1336. static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
  1337. static struct acpi_driver acpi_battery_driver = {
  1338. .name = "battery",
  1339. .class = ACPI_BATTERY_CLASS,
  1340. .ids = battery_device_ids,
  1341. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1342. .ops = {
  1343. .add = acpi_battery_add,
  1344. .remove = acpi_battery_remove,
  1345. .notify = acpi_battery_notify,
  1346. },
  1347. .drv.pm = &acpi_battery_pm,
  1348. };
  1349. static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
  1350. {
  1351. unsigned int i;
  1352. int result;
  1353. dmi_check_system(bat_dmi_table);
  1354. if (battery_check_pmic) {
  1355. for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
  1356. if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
  1357. pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
  1358. ": found native %s PMIC, not loading\n",
  1359. acpi_battery_blacklist[i]);
  1360. return;
  1361. }
  1362. }
  1363. #ifdef CONFIG_ACPI_PROCFS_POWER
  1364. acpi_battery_dir = acpi_lock_battery_dir();
  1365. if (!acpi_battery_dir)
  1366. return;
  1367. #endif
  1368. result = acpi_bus_register_driver(&acpi_battery_driver);
  1369. #ifdef CONFIG_ACPI_PROCFS_POWER
  1370. if (result < 0)
  1371. acpi_unlock_battery_dir(acpi_battery_dir);
  1372. #endif
  1373. battery_driver_registered = (result == 0);
  1374. }
  1375. static int __init acpi_battery_init(void)
  1376. {
  1377. if (acpi_disabled)
  1378. return -ENODEV;
  1379. async_cookie = async_schedule(acpi_battery_init_async, NULL);
  1380. return 0;
  1381. }
  1382. static void __exit acpi_battery_exit(void)
  1383. {
  1384. async_synchronize_cookie(async_cookie + 1);
  1385. if (battery_driver_registered) {
  1386. acpi_bus_unregister_driver(&acpi_battery_driver);
  1387. battery_hook_exit();
  1388. }
  1389. #ifdef CONFIG_ACPI_PROCFS_POWER
  1390. if (acpi_battery_dir)
  1391. acpi_unlock_battery_dir(acpi_battery_dir);
  1392. #endif
  1393. }
  1394. module_init(acpi_battery_init);
  1395. module_exit(acpi_battery_exit);