bq27x00_battery.c 25 KB

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