battery.c 39 KB

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