bq27x00_battery.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * BQ27x00 battery driver
  3. *
  4. * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
  6. * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
  7. * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
  8. *
  9. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  10. *
  11. * This package is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * Datasheets:
  20. * http://focus.ti.com/docs/prod/folders/print/bq27000.html
  21. * http://focus.ti.com/docs/prod/folders/print/bq27500.html
  22. * http://www.ti.com/product/bq27425-g1
  23. * http://www.ti.com/product/BQ27742-G1
  24. * http://www.ti.com/product/BQ27510-G3
  25. */
  26. #include <linux/device.h>
  27. #include <linux/module.h>
  28. #include <linux/param.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/delay.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <linux/idr.h>
  35. #include <linux/i2c.h>
  36. #include <linux/slab.h>
  37. #include <asm/unaligned.h>
  38. #include <linux/power/bq27x00_battery.h>
  39. #define DRIVER_VERSION "1.2.0"
  40. #define BQ27x00_REG_TEMP 0x06
  41. #define BQ27x00_REG_VOLT 0x08
  42. #define BQ27x00_REG_AI 0x14
  43. #define BQ27x00_REG_FLAGS 0x0A
  44. #define BQ27x00_REG_TTE 0x16
  45. #define BQ27x00_REG_TTF 0x18
  46. #define BQ27x00_REG_TTECP 0x26
  47. #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
  48. #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
  49. #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
  50. #define BQ27x00_REG_AE 0x22 /* Available energy */
  51. #define BQ27x00_POWER_AVG 0x24
  52. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  53. #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
  54. #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
  55. #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
  56. #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
  57. #define BQ27000_FLAG_FC BIT(5)
  58. #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
  59. #define BQ27500_REG_SOC 0x2C
  60. #define BQ27500_REG_DCAP 0x3C /* Design capacity */
  61. #define BQ27500_FLAG_DSC BIT(0)
  62. #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
  63. #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
  64. #define BQ27500_FLAG_FC BIT(9)
  65. #define BQ27500_FLAG_OTC BIT(15)
  66. #define BQ27742_POWER_AVG 0x76
  67. #define BQ27510_REG_SOC 0x20
  68. #define BQ27510_REG_DCAP 0x2E /* Design capacity */
  69. #define BQ27510_REG_CYCT 0x1E /* Cycle count total */
  70. /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
  71. #define BQ27425_REG_OFFSET 0x04
  72. #define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
  73. #define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
  74. #define BQ27000_RS 20 /* Resistor sense */
  75. #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
  76. struct bq27x00_device_info;
  77. struct bq27x00_access_methods {
  78. int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  79. };
  80. enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742, BQ27510};
  81. struct bq27x00_reg_cache {
  82. int temperature;
  83. int time_to_empty;
  84. int time_to_empty_avg;
  85. int time_to_full;
  86. int charge_full;
  87. int cycle_count;
  88. int capacity;
  89. int energy;
  90. int flags;
  91. int power_avg;
  92. int health;
  93. };
  94. struct bq27x00_device_info {
  95. struct device *dev;
  96. int id;
  97. enum bq27x00_chip chip;
  98. struct bq27x00_reg_cache cache;
  99. int charge_design_full;
  100. unsigned long last_update;
  101. struct delayed_work work;
  102. struct power_supply *bat;
  103. struct bq27x00_access_methods bus;
  104. struct mutex lock;
  105. };
  106. static enum power_supply_property bq27x00_battery_props[] = {
  107. POWER_SUPPLY_PROP_STATUS,
  108. POWER_SUPPLY_PROP_PRESENT,
  109. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  110. POWER_SUPPLY_PROP_CURRENT_NOW,
  111. POWER_SUPPLY_PROP_CAPACITY,
  112. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  113. POWER_SUPPLY_PROP_TEMP,
  114. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  115. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  116. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  117. POWER_SUPPLY_PROP_TECHNOLOGY,
  118. POWER_SUPPLY_PROP_CHARGE_FULL,
  119. POWER_SUPPLY_PROP_CHARGE_NOW,
  120. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  121. POWER_SUPPLY_PROP_CYCLE_COUNT,
  122. POWER_SUPPLY_PROP_ENERGY_NOW,
  123. POWER_SUPPLY_PROP_POWER_AVG,
  124. POWER_SUPPLY_PROP_HEALTH,
  125. };
  126. static enum power_supply_property bq27425_battery_props[] = {
  127. POWER_SUPPLY_PROP_STATUS,
  128. POWER_SUPPLY_PROP_PRESENT,
  129. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  130. POWER_SUPPLY_PROP_CURRENT_NOW,
  131. POWER_SUPPLY_PROP_CAPACITY,
  132. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  133. POWER_SUPPLY_PROP_TEMP,
  134. POWER_SUPPLY_PROP_TECHNOLOGY,
  135. POWER_SUPPLY_PROP_CHARGE_FULL,
  136. POWER_SUPPLY_PROP_CHARGE_NOW,
  137. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  138. };
  139. static enum power_supply_property bq27742_battery_props[] = {
  140. POWER_SUPPLY_PROP_STATUS,
  141. POWER_SUPPLY_PROP_PRESENT,
  142. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  143. POWER_SUPPLY_PROP_CURRENT_NOW,
  144. POWER_SUPPLY_PROP_CAPACITY,
  145. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  146. POWER_SUPPLY_PROP_TEMP,
  147. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  148. POWER_SUPPLY_PROP_TECHNOLOGY,
  149. POWER_SUPPLY_PROP_CHARGE_FULL,
  150. POWER_SUPPLY_PROP_CHARGE_NOW,
  151. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  152. POWER_SUPPLY_PROP_CYCLE_COUNT,
  153. POWER_SUPPLY_PROP_POWER_AVG,
  154. POWER_SUPPLY_PROP_HEALTH,
  155. };
  156. static enum power_supply_property bq27510_battery_props[] = {
  157. POWER_SUPPLY_PROP_STATUS,
  158. POWER_SUPPLY_PROP_PRESENT,
  159. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  160. POWER_SUPPLY_PROP_CURRENT_NOW,
  161. POWER_SUPPLY_PROP_CAPACITY,
  162. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  163. POWER_SUPPLY_PROP_TEMP,
  164. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  165. POWER_SUPPLY_PROP_TECHNOLOGY,
  166. POWER_SUPPLY_PROP_CHARGE_FULL,
  167. POWER_SUPPLY_PROP_CHARGE_NOW,
  168. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  169. POWER_SUPPLY_PROP_CYCLE_COUNT,
  170. POWER_SUPPLY_PROP_POWER_AVG,
  171. POWER_SUPPLY_PROP_HEALTH,
  172. };
  173. static unsigned int poll_interval = 360;
  174. module_param(poll_interval, uint, 0644);
  175. MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
  176. "0 disables polling");
  177. /*
  178. * Common code for BQ27x00 devices
  179. */
  180. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  181. bool single)
  182. {
  183. if (di->chip == BQ27425)
  184. return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
  185. return di->bus.read(di, reg, single);
  186. }
  187. /*
  188. * Higher versions of the chip like BQ27425 and BQ27500
  189. * differ from BQ27000 and BQ27200 in calculation of certain
  190. * parameters. Hence we need to check for the chip type.
  191. */
  192. static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
  193. {
  194. if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742
  195. || di->chip == BQ27510)
  196. return true;
  197. return false;
  198. }
  199. /*
  200. * Return the battery Relative State-of-Charge
  201. * Or < 0 if something fails.
  202. */
  203. static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
  204. {
  205. int rsoc;
  206. if (di->chip == BQ27500 || di->chip == BQ27742)
  207. rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
  208. else if (di->chip == BQ27510)
  209. rsoc = bq27x00_read(di, BQ27510_REG_SOC, false);
  210. else if (di->chip == BQ27425)
  211. rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
  212. else
  213. rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
  214. if (rsoc < 0)
  215. dev_dbg(di->dev, "error reading relative State-of-Charge\n");
  216. return rsoc;
  217. }
  218. /*
  219. * Return a battery charge value in µAh
  220. * Or < 0 if something fails.
  221. */
  222. static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
  223. {
  224. int charge;
  225. charge = bq27x00_read(di, reg, false);
  226. if (charge < 0) {
  227. dev_dbg(di->dev, "error reading charge register %02x: %d\n",
  228. reg, charge);
  229. return charge;
  230. }
  231. if (bq27xxx_is_chip_version_higher(di))
  232. charge *= 1000;
  233. else
  234. charge = charge * 3570 / BQ27000_RS;
  235. return charge;
  236. }
  237. /*
  238. * Return the battery Nominal available capaciy in µAh
  239. * Or < 0 if something fails.
  240. */
  241. static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
  242. {
  243. int flags;
  244. bool is_bq27500 = di->chip == BQ27500;
  245. bool is_bq27742 = di->chip == BQ27742;
  246. bool is_higher = bq27xxx_is_chip_version_higher(di);
  247. bool flags_1b = !(is_bq27500 || is_bq27742);
  248. flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
  249. if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
  250. return -ENODATA;
  251. return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
  252. }
  253. /*
  254. * Return the battery Last measured discharge in µAh
  255. * Or < 0 if something fails.
  256. */
  257. static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
  258. {
  259. return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
  260. }
  261. /*
  262. * Return the battery Initial last measured discharge in µAh
  263. * Or < 0 if something fails.
  264. */
  265. static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
  266. {
  267. int ilmd;
  268. if (bq27xxx_is_chip_version_higher(di)) {
  269. if (di->chip == BQ27425)
  270. ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false);
  271. else if (di->chip == BQ27510)
  272. ilmd = bq27x00_read(di, BQ27510_REG_DCAP, false);
  273. else
  274. ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
  275. } else
  276. ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
  277. if (ilmd < 0) {
  278. dev_dbg(di->dev, "error reading initial last measured discharge\n");
  279. return ilmd;
  280. }
  281. if (bq27xxx_is_chip_version_higher(di))
  282. ilmd *= 1000;
  283. else
  284. ilmd = ilmd * 256 * 3570 / BQ27000_RS;
  285. return ilmd;
  286. }
  287. /*
  288. * Return the battery Available energy in µWh
  289. * Or < 0 if something fails.
  290. */
  291. static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
  292. {
  293. int ae;
  294. ae = bq27x00_read(di, BQ27x00_REG_AE, false);
  295. if (ae < 0) {
  296. dev_dbg(di->dev, "error reading available energy\n");
  297. return ae;
  298. }
  299. if (di->chip == BQ27500)
  300. ae *= 1000;
  301. else
  302. ae = ae * 29200 / BQ27000_RS;
  303. return ae;
  304. }
  305. /*
  306. * Return the battery temperature in tenths of degree Kelvin
  307. * Or < 0 if something fails.
  308. */
  309. static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
  310. {
  311. int temp;
  312. temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
  313. if (temp < 0) {
  314. dev_err(di->dev, "error reading temperature\n");
  315. return temp;
  316. }
  317. if (!bq27xxx_is_chip_version_higher(di))
  318. temp = 5 * temp / 2;
  319. return temp;
  320. }
  321. /*
  322. * Return the battery Cycle count total
  323. * Or < 0 if something fails.
  324. */
  325. static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
  326. {
  327. int cyct;
  328. if (di->chip == BQ27510)
  329. cyct = bq27x00_read(di, BQ27510_REG_CYCT, false);
  330. else
  331. cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
  332. if (cyct < 0)
  333. dev_err(di->dev, "error reading cycle count total\n");
  334. return cyct;
  335. }
  336. /*
  337. * Read a time register.
  338. * Return < 0 if something fails.
  339. */
  340. static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
  341. {
  342. int tval;
  343. tval = bq27x00_read(di, reg, false);
  344. if (tval < 0) {
  345. dev_dbg(di->dev, "error reading time register %02x: %d\n",
  346. reg, tval);
  347. return tval;
  348. }
  349. if (tval == 65535)
  350. return -ENODATA;
  351. return tval * 60;
  352. }
  353. /*
  354. * Read a power avg register.
  355. * Return < 0 if something fails.
  356. */
  357. static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
  358. {
  359. int tval;
  360. tval = bq27x00_read(di, reg, false);
  361. if (tval < 0) {
  362. dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
  363. reg, tval);
  364. return tval;
  365. }
  366. if (di->chip == BQ27500)
  367. return tval;
  368. else
  369. return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
  370. }
  371. /*
  372. * Read flag register.
  373. * Return < 0 if something fails.
  374. */
  375. static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
  376. {
  377. int tval;
  378. tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  379. if (tval < 0) {
  380. dev_err(di->dev, "error reading flag register:%d\n", tval);
  381. return tval;
  382. }
  383. if ((di->chip == BQ27500)) {
  384. if (tval & BQ27500_FLAG_SOCF)
  385. tval = POWER_SUPPLY_HEALTH_DEAD;
  386. else if (tval & BQ27500_FLAG_OTC)
  387. tval = POWER_SUPPLY_HEALTH_OVERHEAT;
  388. else
  389. tval = POWER_SUPPLY_HEALTH_GOOD;
  390. return tval;
  391. } else if (di->chip == BQ27510) {
  392. if (tval & BQ27500_FLAG_OTC)
  393. return POWER_SUPPLY_HEALTH_OVERHEAT;
  394. return POWER_SUPPLY_HEALTH_GOOD;
  395. } else {
  396. if (tval & BQ27000_FLAG_EDV1)
  397. tval = POWER_SUPPLY_HEALTH_DEAD;
  398. else
  399. tval = POWER_SUPPLY_HEALTH_GOOD;
  400. return tval;
  401. }
  402. return -1;
  403. }
  404. static void bq27x00_update(struct bq27x00_device_info *di)
  405. {
  406. struct bq27x00_reg_cache cache = {0, };
  407. bool is_bq27500 = di->chip == BQ27500;
  408. bool is_bq27510 = di->chip == BQ27510;
  409. bool is_bq27425 = di->chip == BQ27425;
  410. bool is_bq27742 = di->chip == BQ27742;
  411. bool flags_1b = !(is_bq27500 || is_bq27742);
  412. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
  413. if ((cache.flags & 0xff) == 0xff)
  414. /* read error */
  415. cache.flags = -1;
  416. if (cache.flags >= 0) {
  417. if (!is_bq27500 && !is_bq27425 && !is_bq27742 && !is_bq27510
  418. && (cache.flags & BQ27000_FLAG_CI)) {
  419. dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
  420. cache.capacity = -ENODATA;
  421. cache.energy = -ENODATA;
  422. cache.time_to_empty = -ENODATA;
  423. cache.time_to_empty_avg = -ENODATA;
  424. cache.time_to_full = -ENODATA;
  425. cache.charge_full = -ENODATA;
  426. cache.health = -ENODATA;
  427. } else {
  428. cache.capacity = bq27x00_battery_read_rsoc(di);
  429. if (is_bq27742 || is_bq27510)
  430. cache.time_to_empty =
  431. bq27x00_battery_read_time(di,
  432. BQ27x00_REG_TTE);
  433. else if (!is_bq27425) {
  434. cache.energy = bq27x00_battery_read_energy(di);
  435. cache.time_to_empty =
  436. bq27x00_battery_read_time(di,
  437. BQ27x00_REG_TTE);
  438. cache.time_to_empty_avg =
  439. bq27x00_battery_read_time(di,
  440. BQ27x00_REG_TTECP);
  441. cache.time_to_full =
  442. bq27x00_battery_read_time(di,
  443. BQ27x00_REG_TTF);
  444. }
  445. cache.charge_full = bq27x00_battery_read_lmd(di);
  446. cache.health = bq27x00_battery_read_health(di);
  447. }
  448. cache.temperature = bq27x00_battery_read_temperature(di);
  449. if (!is_bq27425)
  450. cache.cycle_count = bq27x00_battery_read_cyct(di);
  451. if (is_bq27742)
  452. cache.power_avg =
  453. bq27x00_battery_read_pwr_avg(di,
  454. BQ27742_POWER_AVG);
  455. else
  456. cache.power_avg =
  457. bq27x00_battery_read_pwr_avg(di,
  458. BQ27x00_POWER_AVG);
  459. /* We only have to read charge design full once */
  460. if (di->charge_design_full <= 0)
  461. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  462. }
  463. if (di->cache.capacity != cache.capacity)
  464. power_supply_changed(di->bat);
  465. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
  466. di->cache = cache;
  467. di->last_update = jiffies;
  468. }
  469. static void bq27x00_battery_poll(struct work_struct *work)
  470. {
  471. struct bq27x00_device_info *di =
  472. container_of(work, struct bq27x00_device_info, work.work);
  473. bq27x00_update(di);
  474. if (poll_interval > 0) {
  475. /* The timer does not have to be accurate. */
  476. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  477. schedule_delayed_work(&di->work, poll_interval * HZ);
  478. }
  479. }
  480. /*
  481. * Return the battery average current in µA
  482. * Note that current can be negative signed as well
  483. * Or 0 if something fails.
  484. */
  485. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  486. union power_supply_propval *val)
  487. {
  488. int curr;
  489. int flags;
  490. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  491. if (curr < 0) {
  492. dev_err(di->dev, "error reading current\n");
  493. return curr;
  494. }
  495. if (bq27xxx_is_chip_version_higher(di)) {
  496. /* bq27500 returns signed value */
  497. val->intval = (int)((s16)curr) * 1000;
  498. } else {
  499. flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  500. if (flags & BQ27000_FLAG_CHGS) {
  501. dev_dbg(di->dev, "negative current!\n");
  502. curr = -curr;
  503. }
  504. val->intval = curr * 3570 / BQ27000_RS;
  505. }
  506. return 0;
  507. }
  508. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  509. union power_supply_propval *val)
  510. {
  511. int status;
  512. if (bq27xxx_is_chip_version_higher(di)) {
  513. if (di->cache.flags & BQ27500_FLAG_FC)
  514. status = POWER_SUPPLY_STATUS_FULL;
  515. else if (di->cache.flags & BQ27500_FLAG_DSC)
  516. status = POWER_SUPPLY_STATUS_DISCHARGING;
  517. else
  518. status = POWER_SUPPLY_STATUS_CHARGING;
  519. } else {
  520. if (di->cache.flags & BQ27000_FLAG_FC)
  521. status = POWER_SUPPLY_STATUS_FULL;
  522. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  523. status = POWER_SUPPLY_STATUS_CHARGING;
  524. else if (power_supply_am_i_supplied(di->bat))
  525. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  526. else
  527. status = POWER_SUPPLY_STATUS_DISCHARGING;
  528. }
  529. val->intval = status;
  530. return 0;
  531. }
  532. static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
  533. union power_supply_propval *val)
  534. {
  535. int level;
  536. if (bq27xxx_is_chip_version_higher(di)) {
  537. if (di->cache.flags & BQ27500_FLAG_FC)
  538. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  539. else if (di->cache.flags & BQ27500_FLAG_SOC1)
  540. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  541. else if (di->cache.flags & BQ27500_FLAG_SOCF)
  542. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  543. else
  544. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  545. } else {
  546. if (di->cache.flags & BQ27000_FLAG_FC)
  547. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  548. else if (di->cache.flags & BQ27000_FLAG_EDV1)
  549. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  550. else if (di->cache.flags & BQ27000_FLAG_EDVF)
  551. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  552. else
  553. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  554. }
  555. val->intval = level;
  556. return 0;
  557. }
  558. /*
  559. * Return the battery Voltage in millivolts
  560. * Or < 0 if something fails.
  561. */
  562. static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
  563. union power_supply_propval *val)
  564. {
  565. int volt;
  566. volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
  567. if (volt < 0) {
  568. dev_err(di->dev, "error reading voltage\n");
  569. return volt;
  570. }
  571. val->intval = volt * 1000;
  572. return 0;
  573. }
  574. static int bq27x00_simple_value(int value,
  575. union power_supply_propval *val)
  576. {
  577. if (value < 0)
  578. return value;
  579. val->intval = value;
  580. return 0;
  581. }
  582. static int bq27x00_battery_get_property(struct power_supply *psy,
  583. enum power_supply_property psp,
  584. union power_supply_propval *val)
  585. {
  586. int ret = 0;
  587. struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
  588. mutex_lock(&di->lock);
  589. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  590. cancel_delayed_work_sync(&di->work);
  591. bq27x00_battery_poll(&di->work.work);
  592. }
  593. mutex_unlock(&di->lock);
  594. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  595. return -ENODEV;
  596. switch (psp) {
  597. case POWER_SUPPLY_PROP_STATUS:
  598. ret = bq27x00_battery_status(di, val);
  599. break;
  600. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  601. ret = bq27x00_battery_voltage(di, val);
  602. break;
  603. case POWER_SUPPLY_PROP_PRESENT:
  604. val->intval = di->cache.flags < 0 ? 0 : 1;
  605. break;
  606. case POWER_SUPPLY_PROP_CURRENT_NOW:
  607. ret = bq27x00_battery_current(di, val);
  608. break;
  609. case POWER_SUPPLY_PROP_CAPACITY:
  610. ret = bq27x00_simple_value(di->cache.capacity, val);
  611. break;
  612. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  613. ret = bq27x00_battery_capacity_level(di, val);
  614. break;
  615. case POWER_SUPPLY_PROP_TEMP:
  616. ret = bq27x00_simple_value(di->cache.temperature, val);
  617. if (ret == 0)
  618. val->intval -= 2731;
  619. break;
  620. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  621. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  622. break;
  623. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  624. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  625. break;
  626. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  627. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  628. break;
  629. case POWER_SUPPLY_PROP_TECHNOLOGY:
  630. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  631. break;
  632. case POWER_SUPPLY_PROP_CHARGE_NOW:
  633. ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
  634. break;
  635. case POWER_SUPPLY_PROP_CHARGE_FULL:
  636. ret = bq27x00_simple_value(di->cache.charge_full, val);
  637. break;
  638. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  639. ret = bq27x00_simple_value(di->charge_design_full, val);
  640. break;
  641. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  642. ret = bq27x00_simple_value(di->cache.cycle_count, val);
  643. break;
  644. case POWER_SUPPLY_PROP_ENERGY_NOW:
  645. ret = bq27x00_simple_value(di->cache.energy, val);
  646. break;
  647. case POWER_SUPPLY_PROP_POWER_AVG:
  648. ret = bq27x00_simple_value(di->cache.power_avg, val);
  649. break;
  650. case POWER_SUPPLY_PROP_HEALTH:
  651. ret = bq27x00_simple_value(di->cache.health, val);
  652. break;
  653. default:
  654. return -EINVAL;
  655. }
  656. return ret;
  657. }
  658. static void bq27x00_external_power_changed(struct power_supply *psy)
  659. {
  660. struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
  661. cancel_delayed_work_sync(&di->work);
  662. schedule_delayed_work(&di->work, 0);
  663. }
  664. static int bq27x00_powersupply_init(struct bq27x00_device_info *di,
  665. const char *name)
  666. {
  667. int ret;
  668. struct power_supply_desc *psy_desc;
  669. struct power_supply_config psy_cfg = { .drv_data = di, };
  670. psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
  671. if (!psy_desc)
  672. return -ENOMEM;
  673. psy_desc->name = name;
  674. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  675. if (di->chip == BQ27425) {
  676. psy_desc->properties = bq27425_battery_props;
  677. psy_desc->num_properties = ARRAY_SIZE(bq27425_battery_props);
  678. } else if (di->chip == BQ27742) {
  679. psy_desc->properties = bq27742_battery_props;
  680. psy_desc->num_properties = ARRAY_SIZE(bq27742_battery_props);
  681. } else if (di->chip == BQ27510) {
  682. psy_desc->properties = bq27510_battery_props;
  683. psy_desc->num_properties = ARRAY_SIZE(bq27510_battery_props);
  684. } else {
  685. psy_desc->properties = bq27x00_battery_props;
  686. psy_desc->num_properties = ARRAY_SIZE(bq27x00_battery_props);
  687. }
  688. psy_desc->get_property = bq27x00_battery_get_property;
  689. psy_desc->external_power_changed = bq27x00_external_power_changed;
  690. INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
  691. mutex_init(&di->lock);
  692. di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
  693. if (IS_ERR(di->bat)) {
  694. ret = PTR_ERR(di->bat);
  695. dev_err(di->dev, "failed to register battery: %d\n", ret);
  696. return ret;
  697. }
  698. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  699. bq27x00_update(di);
  700. return 0;
  701. }
  702. static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
  703. {
  704. /*
  705. * power_supply_unregister call bq27x00_battery_get_property which
  706. * call bq27x00_battery_poll.
  707. * Make sure that bq27x00_battery_poll will not call
  708. * schedule_delayed_work again after unregister (which cause OOPS).
  709. */
  710. poll_interval = 0;
  711. cancel_delayed_work_sync(&di->work);
  712. power_supply_unregister(di->bat);
  713. mutex_destroy(&di->lock);
  714. }
  715. /* i2c specific code */
  716. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  717. /* If the system has several batteries we need a different name for each
  718. * of them...
  719. */
  720. static DEFINE_IDR(battery_id);
  721. static DEFINE_MUTEX(battery_mutex);
  722. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  723. {
  724. struct i2c_client *client = to_i2c_client(di->dev);
  725. struct i2c_msg msg[2];
  726. unsigned char data[2];
  727. int ret;
  728. if (!client->adapter)
  729. return -ENODEV;
  730. msg[0].addr = client->addr;
  731. msg[0].flags = 0;
  732. msg[0].buf = &reg;
  733. msg[0].len = sizeof(reg);
  734. msg[1].addr = client->addr;
  735. msg[1].flags = I2C_M_RD;
  736. msg[1].buf = data;
  737. if (single)
  738. msg[1].len = 1;
  739. else
  740. msg[1].len = 2;
  741. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  742. if (ret < 0)
  743. return ret;
  744. if (!single)
  745. ret = get_unaligned_le16(data);
  746. else
  747. ret = data[0];
  748. return ret;
  749. }
  750. static int bq27x00_battery_probe(struct i2c_client *client,
  751. const struct i2c_device_id *id)
  752. {
  753. char *name;
  754. struct bq27x00_device_info *di;
  755. int num;
  756. int retval = 0;
  757. /* Get new ID for the new battery device */
  758. mutex_lock(&battery_mutex);
  759. num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
  760. mutex_unlock(&battery_mutex);
  761. if (num < 0)
  762. return num;
  763. name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
  764. if (!name) {
  765. dev_err(&client->dev, "failed to allocate device name\n");
  766. retval = -ENOMEM;
  767. goto batt_failed;
  768. }
  769. di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
  770. if (!di) {
  771. dev_err(&client->dev, "failed to allocate device info data\n");
  772. retval = -ENOMEM;
  773. goto batt_failed;
  774. }
  775. di->id = num;
  776. di->dev = &client->dev;
  777. di->chip = id->driver_data;
  778. di->bus.read = &bq27x00_read_i2c;
  779. retval = bq27x00_powersupply_init(di, name);
  780. if (retval)
  781. goto batt_failed;
  782. i2c_set_clientdata(client, di);
  783. return 0;
  784. batt_failed:
  785. mutex_lock(&battery_mutex);
  786. idr_remove(&battery_id, num);
  787. mutex_unlock(&battery_mutex);
  788. return retval;
  789. }
  790. static int bq27x00_battery_remove(struct i2c_client *client)
  791. {
  792. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  793. bq27x00_powersupply_unregister(di);
  794. mutex_lock(&battery_mutex);
  795. idr_remove(&battery_id, di->id);
  796. mutex_unlock(&battery_mutex);
  797. return 0;
  798. }
  799. static const struct i2c_device_id bq27x00_id[] = {
  800. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  801. { "bq27500", BQ27500 },
  802. { "bq27425", BQ27425 },
  803. { "bq27742", BQ27742 },
  804. { "bq27510", BQ27510 },
  805. {},
  806. };
  807. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  808. static struct i2c_driver bq27x00_battery_driver = {
  809. .driver = {
  810. .name = "bq27x00-battery",
  811. },
  812. .probe = bq27x00_battery_probe,
  813. .remove = bq27x00_battery_remove,
  814. .id_table = bq27x00_id,
  815. };
  816. static inline int bq27x00_battery_i2c_init(void)
  817. {
  818. int ret = i2c_add_driver(&bq27x00_battery_driver);
  819. if (ret)
  820. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  821. return ret;
  822. }
  823. static inline void bq27x00_battery_i2c_exit(void)
  824. {
  825. i2c_del_driver(&bq27x00_battery_driver);
  826. }
  827. #else
  828. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  829. static inline void bq27x00_battery_i2c_exit(void) {};
  830. #endif
  831. /* platform specific code */
  832. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  833. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  834. bool single)
  835. {
  836. struct device *dev = di->dev;
  837. struct bq27000_platform_data *pdata = dev->platform_data;
  838. unsigned int timeout = 3;
  839. int upper, lower;
  840. int temp;
  841. if (!single) {
  842. /* Make sure the value has not changed in between reading the
  843. * lower and the upper part */
  844. upper = pdata->read(dev, reg + 1);
  845. do {
  846. temp = upper;
  847. if (upper < 0)
  848. return upper;
  849. lower = pdata->read(dev, reg);
  850. if (lower < 0)
  851. return lower;
  852. upper = pdata->read(dev, reg + 1);
  853. } while (temp != upper && --timeout);
  854. if (timeout == 0)
  855. return -EIO;
  856. return (upper << 8) | lower;
  857. }
  858. return pdata->read(dev, reg);
  859. }
  860. static int bq27000_battery_probe(struct platform_device *pdev)
  861. {
  862. struct bq27x00_device_info *di;
  863. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  864. const char *name;
  865. if (!pdata) {
  866. dev_err(&pdev->dev, "no platform_data supplied\n");
  867. return -EINVAL;
  868. }
  869. if (!pdata->read) {
  870. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  871. return -EINVAL;
  872. }
  873. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  874. if (!di) {
  875. dev_err(&pdev->dev, "failed to allocate device info data\n");
  876. return -ENOMEM;
  877. }
  878. platform_set_drvdata(pdev, di);
  879. di->dev = &pdev->dev;
  880. di->chip = BQ27000;
  881. name = pdata->name ?: dev_name(&pdev->dev);
  882. di->bus.read = &bq27000_read_platform;
  883. return bq27x00_powersupply_init(di, name);
  884. }
  885. static int bq27000_battery_remove(struct platform_device *pdev)
  886. {
  887. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  888. bq27x00_powersupply_unregister(di);
  889. return 0;
  890. }
  891. static struct platform_driver bq27000_battery_driver = {
  892. .probe = bq27000_battery_probe,
  893. .remove = bq27000_battery_remove,
  894. .driver = {
  895. .name = "bq27000-battery",
  896. },
  897. };
  898. static inline int bq27x00_battery_platform_init(void)
  899. {
  900. int ret = platform_driver_register(&bq27000_battery_driver);
  901. if (ret)
  902. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  903. return ret;
  904. }
  905. static inline void bq27x00_battery_platform_exit(void)
  906. {
  907. platform_driver_unregister(&bq27000_battery_driver);
  908. }
  909. #else
  910. static inline int bq27x00_battery_platform_init(void) { return 0; }
  911. static inline void bq27x00_battery_platform_exit(void) {};
  912. #endif
  913. /*
  914. * Module stuff
  915. */
  916. static int __init bq27x00_battery_init(void)
  917. {
  918. int ret;
  919. ret = bq27x00_battery_i2c_init();
  920. if (ret)
  921. return ret;
  922. ret = bq27x00_battery_platform_init();
  923. if (ret)
  924. bq27x00_battery_i2c_exit();
  925. return ret;
  926. }
  927. module_init(bq27x00_battery_init);
  928. static void __exit bq27x00_battery_exit(void)
  929. {
  930. bq27x00_battery_platform_exit();
  931. bq27x00_battery_i2c_exit();
  932. }
  933. module_exit(bq27x00_battery_exit);
  934. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  935. MODULE_ALIAS("platform:bq27000-battery");
  936. #endif
  937. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  938. MODULE_ALIAS("i2c:bq27000-battery");
  939. #endif
  940. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  941. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  942. MODULE_LICENSE("GPL");