bq27x00_battery.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
  201. }
  202. /*
  203. * Return the battery Last measured discharge in µAh
  204. * Or < 0 if something fails.
  205. */
  206. static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
  207. {
  208. return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
  209. }
  210. /*
  211. * Return the battery Initial last measured discharge in µAh
  212. * Or < 0 if something fails.
  213. */
  214. static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
  215. {
  216. int ilmd;
  217. if (bq27xxx_is_chip_version_higher(di))
  218. ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
  219. else
  220. ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
  221. if (ilmd < 0) {
  222. dev_dbg(di->dev, "error reading initial last measured discharge\n");
  223. return ilmd;
  224. }
  225. if (bq27xxx_is_chip_version_higher(di))
  226. ilmd *= 1000;
  227. else
  228. ilmd = ilmd * 256 * 3570 / BQ27000_RS;
  229. return ilmd;
  230. }
  231. /*
  232. * Return the battery Available energy in µWh
  233. * Or < 0 if something fails.
  234. */
  235. static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
  236. {
  237. int ae;
  238. ae = bq27x00_read(di, BQ27x00_REG_AE, false);
  239. if (ae < 0) {
  240. dev_dbg(di->dev, "error reading available energy\n");
  241. return ae;
  242. }
  243. if (di->chip == BQ27500)
  244. ae *= 1000;
  245. else
  246. ae = ae * 29200 / BQ27000_RS;
  247. return ae;
  248. }
  249. /*
  250. * Return the battery temperature in tenths of degree Celsius
  251. * Or < 0 if something fails.
  252. */
  253. static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
  254. {
  255. int temp;
  256. temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
  257. if (temp < 0) {
  258. dev_err(di->dev, "error reading temperature\n");
  259. return temp;
  260. }
  261. if (bq27xxx_is_chip_version_higher(di))
  262. temp -= 2731;
  263. else
  264. temp = ((temp * 5) - 5463) / 2;
  265. return temp;
  266. }
  267. /*
  268. * Return the battery Cycle count total
  269. * Or < 0 if something fails.
  270. */
  271. static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
  272. {
  273. int cyct;
  274. cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
  275. if (cyct < 0)
  276. dev_err(di->dev, "error reading cycle count total\n");
  277. return cyct;
  278. }
  279. /*
  280. * Read a time register.
  281. * Return < 0 if something fails.
  282. */
  283. static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
  284. {
  285. int tval;
  286. tval = bq27x00_read(di, reg, false);
  287. if (tval < 0) {
  288. dev_dbg(di->dev, "error reading time register %02x: %d\n",
  289. reg, tval);
  290. return tval;
  291. }
  292. if (tval == 65535)
  293. return -ENODATA;
  294. return tval * 60;
  295. }
  296. /*
  297. * Read a power avg register.
  298. * Return < 0 if something fails.
  299. */
  300. static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
  301. {
  302. int tval;
  303. tval = bq27x00_read(di, reg, false);
  304. if (tval < 0) {
  305. dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
  306. reg, tval);
  307. return tval;
  308. }
  309. if (di->chip == BQ27500)
  310. return tval;
  311. else
  312. return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
  313. }
  314. /*
  315. * Read flag register.
  316. * Return < 0 if something fails.
  317. */
  318. static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
  319. {
  320. int tval;
  321. tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  322. if (tval < 0) {
  323. dev_err(di->dev, "error reading flag register:%d\n", tval);
  324. return tval;
  325. }
  326. if ((di->chip == BQ27500)) {
  327. if (tval & BQ27500_FLAG_SOCF)
  328. tval = POWER_SUPPLY_HEALTH_DEAD;
  329. else if (tval & BQ27500_FLAG_OTC)
  330. tval = POWER_SUPPLY_HEALTH_OVERHEAT;
  331. else
  332. tval = POWER_SUPPLY_HEALTH_GOOD;
  333. return tval;
  334. } else {
  335. if (tval & BQ27000_FLAG_EDV1)
  336. tval = POWER_SUPPLY_HEALTH_DEAD;
  337. else
  338. tval = POWER_SUPPLY_HEALTH_GOOD;
  339. return tval;
  340. }
  341. return -1;
  342. }
  343. static void bq27x00_update(struct bq27x00_device_info *di)
  344. {
  345. struct bq27x00_reg_cache cache = {0, };
  346. bool is_bq27500 = di->chip == BQ27500;
  347. bool is_bq27425 = di->chip == BQ27425;
  348. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
  349. if (cache.flags >= 0) {
  350. if (!is_bq27500 && !is_bq27425
  351. && (cache.flags & BQ27000_FLAG_CI)) {
  352. dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
  353. cache.capacity = -ENODATA;
  354. cache.energy = -ENODATA;
  355. cache.time_to_empty = -ENODATA;
  356. cache.time_to_empty_avg = -ENODATA;
  357. cache.time_to_full = -ENODATA;
  358. cache.charge_full = -ENODATA;
  359. cache.health = -ENODATA;
  360. } else {
  361. cache.capacity = bq27x00_battery_read_rsoc(di);
  362. if (!is_bq27425) {
  363. cache.energy = bq27x00_battery_read_energy(di);
  364. cache.time_to_empty =
  365. bq27x00_battery_read_time(di,
  366. BQ27x00_REG_TTE);
  367. cache.time_to_empty_avg =
  368. bq27x00_battery_read_time(di,
  369. BQ27x00_REG_TTECP);
  370. cache.time_to_full =
  371. bq27x00_battery_read_time(di,
  372. BQ27x00_REG_TTF);
  373. }
  374. cache.charge_full = bq27x00_battery_read_lmd(di);
  375. cache.health = bq27x00_battery_read_health(di);
  376. }
  377. cache.temperature = bq27x00_battery_read_temperature(di);
  378. if (!is_bq27425)
  379. cache.cycle_count = bq27x00_battery_read_cyct(di);
  380. cache.cycle_count = bq27x00_battery_read_cyct(di);
  381. cache.power_avg =
  382. bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
  383. /* We only have to read charge design full once */
  384. if (di->charge_design_full <= 0)
  385. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  386. }
  387. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
  388. di->cache = cache;
  389. power_supply_changed(&di->bat);
  390. }
  391. di->last_update = jiffies;
  392. }
  393. static void bq27x00_battery_poll(struct work_struct *work)
  394. {
  395. struct bq27x00_device_info *di =
  396. container_of(work, struct bq27x00_device_info, work.work);
  397. bq27x00_update(di);
  398. if (poll_interval > 0) {
  399. /* The timer does not have to be accurate. */
  400. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  401. schedule_delayed_work(&di->work, poll_interval * HZ);
  402. }
  403. }
  404. /*
  405. * Return the battery average current in µA
  406. * Note that current can be negative signed as well
  407. * Or 0 if something fails.
  408. */
  409. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  410. union power_supply_propval *val)
  411. {
  412. int curr;
  413. int flags;
  414. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  415. if (curr < 0) {
  416. dev_err(di->dev, "error reading current\n");
  417. return curr;
  418. }
  419. if (bq27xxx_is_chip_version_higher(di)) {
  420. /* bq27500 returns signed value */
  421. val->intval = (int)((s16)curr) * 1000;
  422. } else {
  423. flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  424. if (flags & BQ27000_FLAG_CHGS) {
  425. dev_dbg(di->dev, "negative current!\n");
  426. curr = -curr;
  427. }
  428. val->intval = curr * 3570 / BQ27000_RS;
  429. }
  430. return 0;
  431. }
  432. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  433. union power_supply_propval *val)
  434. {
  435. int status;
  436. if (bq27xxx_is_chip_version_higher(di)) {
  437. if (di->cache.flags & BQ27500_FLAG_FC)
  438. status = POWER_SUPPLY_STATUS_FULL;
  439. else if (di->cache.flags & BQ27500_FLAG_DSC)
  440. status = POWER_SUPPLY_STATUS_DISCHARGING;
  441. else
  442. status = POWER_SUPPLY_STATUS_CHARGING;
  443. } else {
  444. if (di->cache.flags & BQ27000_FLAG_FC)
  445. status = POWER_SUPPLY_STATUS_FULL;
  446. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  447. status = POWER_SUPPLY_STATUS_CHARGING;
  448. else if (power_supply_am_i_supplied(&di->bat))
  449. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  450. else
  451. status = POWER_SUPPLY_STATUS_DISCHARGING;
  452. }
  453. val->intval = status;
  454. return 0;
  455. }
  456. static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
  457. union power_supply_propval *val)
  458. {
  459. int level;
  460. if (bq27xxx_is_chip_version_higher(di)) {
  461. if (di->cache.flags & BQ27500_FLAG_FC)
  462. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  463. else if (di->cache.flags & BQ27500_FLAG_SOC1)
  464. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  465. else if (di->cache.flags & BQ27500_FLAG_SOCF)
  466. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  467. else
  468. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  469. } else {
  470. if (di->cache.flags & BQ27000_FLAG_FC)
  471. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  472. else if (di->cache.flags & BQ27000_FLAG_EDV1)
  473. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  474. else if (di->cache.flags & BQ27000_FLAG_EDVF)
  475. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  476. else
  477. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  478. }
  479. val->intval = level;
  480. return 0;
  481. }
  482. /*
  483. * Return the battery Voltage in millivolts
  484. * Or < 0 if something fails.
  485. */
  486. static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
  487. union power_supply_propval *val)
  488. {
  489. int volt;
  490. volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
  491. if (volt < 0) {
  492. dev_err(di->dev, "error reading voltage\n");
  493. return volt;
  494. }
  495. val->intval = volt * 1000;
  496. return 0;
  497. }
  498. static int bq27x00_simple_value(int value,
  499. union power_supply_propval *val)
  500. {
  501. if (value < 0)
  502. return value;
  503. val->intval = value;
  504. return 0;
  505. }
  506. #define to_bq27x00_device_info(x) container_of((x), \
  507. struct bq27x00_device_info, bat);
  508. static int bq27x00_battery_get_property(struct power_supply *psy,
  509. enum power_supply_property psp,
  510. union power_supply_propval *val)
  511. {
  512. int ret = 0;
  513. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  514. mutex_lock(&di->lock);
  515. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  516. cancel_delayed_work_sync(&di->work);
  517. bq27x00_battery_poll(&di->work.work);
  518. }
  519. mutex_unlock(&di->lock);
  520. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  521. return -ENODEV;
  522. switch (psp) {
  523. case POWER_SUPPLY_PROP_STATUS:
  524. ret = bq27x00_battery_status(di, val);
  525. break;
  526. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  527. ret = bq27x00_battery_voltage(di, val);
  528. break;
  529. case POWER_SUPPLY_PROP_PRESENT:
  530. val->intval = di->cache.flags < 0 ? 0 : 1;
  531. break;
  532. case POWER_SUPPLY_PROP_CURRENT_NOW:
  533. ret = bq27x00_battery_current(di, val);
  534. break;
  535. case POWER_SUPPLY_PROP_CAPACITY:
  536. ret = bq27x00_simple_value(di->cache.capacity, val);
  537. break;
  538. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  539. ret = bq27x00_battery_capacity_level(di, val);
  540. break;
  541. case POWER_SUPPLY_PROP_TEMP:
  542. ret = bq27x00_simple_value(di->cache.temperature, val);
  543. break;
  544. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  545. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  546. break;
  547. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  548. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  549. break;
  550. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  551. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  552. break;
  553. case POWER_SUPPLY_PROP_TECHNOLOGY:
  554. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  555. break;
  556. case POWER_SUPPLY_PROP_CHARGE_NOW:
  557. ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
  558. break;
  559. case POWER_SUPPLY_PROP_CHARGE_FULL:
  560. ret = bq27x00_simple_value(di->cache.charge_full, val);
  561. break;
  562. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  563. ret = bq27x00_simple_value(di->charge_design_full, val);
  564. break;
  565. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  566. ret = bq27x00_simple_value(di->cache.cycle_count, val);
  567. break;
  568. case POWER_SUPPLY_PROP_ENERGY_NOW:
  569. ret = bq27x00_simple_value(di->cache.energy, val);
  570. break;
  571. case POWER_SUPPLY_PROP_POWER_AVG:
  572. ret = bq27x00_simple_value(di->cache.power_avg, val);
  573. break;
  574. case POWER_SUPPLY_PROP_HEALTH:
  575. ret = bq27x00_simple_value(di->cache.health, val);
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. return ret;
  581. }
  582. static void bq27x00_external_power_changed(struct power_supply *psy)
  583. {
  584. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  585. cancel_delayed_work_sync(&di->work);
  586. schedule_delayed_work(&di->work, 0);
  587. }
  588. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  589. {
  590. int ret;
  591. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  592. di->chip = BQ27425;
  593. if (di->chip == BQ27425) {
  594. di->bat.properties = bq27425_battery_props;
  595. di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
  596. } else {
  597. di->bat.properties = bq27x00_battery_props;
  598. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  599. }
  600. di->bat.get_property = bq27x00_battery_get_property;
  601. di->bat.external_power_changed = bq27x00_external_power_changed;
  602. INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
  603. mutex_init(&di->lock);
  604. ret = power_supply_register(di->dev, &di->bat);
  605. if (ret) {
  606. dev_err(di->dev, "failed to register battery: %d\n", ret);
  607. return ret;
  608. }
  609. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  610. bq27x00_update(di);
  611. return 0;
  612. }
  613. static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
  614. {
  615. /*
  616. * power_supply_unregister call bq27x00_battery_get_property which
  617. * call bq27x00_battery_poll.
  618. * Make sure that bq27x00_battery_poll will not call
  619. * schedule_delayed_work again after unregister (which cause OOPS).
  620. */
  621. poll_interval = 0;
  622. cancel_delayed_work_sync(&di->work);
  623. power_supply_unregister(&di->bat);
  624. mutex_destroy(&di->lock);
  625. }
  626. /* i2c specific code */
  627. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  628. /* If the system has several batteries we need a different name for each
  629. * of them...
  630. */
  631. static DEFINE_IDR(battery_id);
  632. static DEFINE_MUTEX(battery_mutex);
  633. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  634. {
  635. struct i2c_client *client = to_i2c_client(di->dev);
  636. struct i2c_msg msg[2];
  637. unsigned char data[2];
  638. int ret;
  639. if (!client->adapter)
  640. return -ENODEV;
  641. msg[0].addr = client->addr;
  642. msg[0].flags = 0;
  643. msg[0].buf = &reg;
  644. msg[0].len = sizeof(reg);
  645. msg[1].addr = client->addr;
  646. msg[1].flags = I2C_M_RD;
  647. msg[1].buf = data;
  648. if (single)
  649. msg[1].len = 1;
  650. else
  651. msg[1].len = 2;
  652. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  653. if (ret < 0)
  654. return ret;
  655. if (!single)
  656. ret = get_unaligned_le16(data);
  657. else
  658. ret = data[0];
  659. return ret;
  660. }
  661. static int bq27x00_battery_probe(struct i2c_client *client,
  662. const struct i2c_device_id *id)
  663. {
  664. char *name;
  665. struct bq27x00_device_info *di;
  666. int num;
  667. int retval = 0;
  668. /* Get new ID for the new battery device */
  669. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  670. if (retval == 0)
  671. return -ENOMEM;
  672. mutex_lock(&battery_mutex);
  673. retval = idr_get_new(&battery_id, client, &num);
  674. mutex_unlock(&battery_mutex);
  675. if (retval < 0)
  676. return retval;
  677. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  678. if (!name) {
  679. dev_err(&client->dev, "failed to allocate device name\n");
  680. retval = -ENOMEM;
  681. goto batt_failed_1;
  682. }
  683. di = kzalloc(sizeof(*di), GFP_KERNEL);
  684. if (!di) {
  685. dev_err(&client->dev, "failed to allocate device info data\n");
  686. retval = -ENOMEM;
  687. goto batt_failed_2;
  688. }
  689. di->id = num;
  690. di->dev = &client->dev;
  691. di->chip = id->driver_data;
  692. di->bat.name = name;
  693. di->bus.read = &bq27x00_read_i2c;
  694. if (bq27x00_powersupply_init(di))
  695. goto batt_failed_3;
  696. i2c_set_clientdata(client, di);
  697. return 0;
  698. batt_failed_3:
  699. kfree(di);
  700. batt_failed_2:
  701. kfree(name);
  702. batt_failed_1:
  703. mutex_lock(&battery_mutex);
  704. idr_remove(&battery_id, num);
  705. mutex_unlock(&battery_mutex);
  706. return retval;
  707. }
  708. static int bq27x00_battery_remove(struct i2c_client *client)
  709. {
  710. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  711. bq27x00_powersupply_unregister(di);
  712. kfree(di->bat.name);
  713. mutex_lock(&battery_mutex);
  714. idr_remove(&battery_id, di->id);
  715. mutex_unlock(&battery_mutex);
  716. kfree(di);
  717. return 0;
  718. }
  719. static const struct i2c_device_id bq27x00_id[] = {
  720. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  721. { "bq27500", BQ27500 },
  722. { "bq27425", BQ27425 },
  723. {},
  724. };
  725. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  726. static struct i2c_driver bq27x00_battery_driver = {
  727. .driver = {
  728. .name = "bq27x00-battery",
  729. },
  730. .probe = bq27x00_battery_probe,
  731. .remove = bq27x00_battery_remove,
  732. .id_table = bq27x00_id,
  733. };
  734. static inline int bq27x00_battery_i2c_init(void)
  735. {
  736. int ret = i2c_add_driver(&bq27x00_battery_driver);
  737. if (ret)
  738. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  739. return ret;
  740. }
  741. static inline void bq27x00_battery_i2c_exit(void)
  742. {
  743. i2c_del_driver(&bq27x00_battery_driver);
  744. }
  745. #else
  746. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  747. static inline void bq27x00_battery_i2c_exit(void) {};
  748. #endif
  749. /* platform specific code */
  750. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  751. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  752. bool single)
  753. {
  754. struct device *dev = di->dev;
  755. struct bq27000_platform_data *pdata = dev->platform_data;
  756. unsigned int timeout = 3;
  757. int upper, lower;
  758. int temp;
  759. if (!single) {
  760. /* Make sure the value has not changed in between reading the
  761. * lower and the upper part */
  762. upper = pdata->read(dev, reg + 1);
  763. do {
  764. temp = upper;
  765. if (upper < 0)
  766. return upper;
  767. lower = pdata->read(dev, reg);
  768. if (lower < 0)
  769. return lower;
  770. upper = pdata->read(dev, reg + 1);
  771. } while (temp != upper && --timeout);
  772. if (timeout == 0)
  773. return -EIO;
  774. return (upper << 8) | lower;
  775. }
  776. return pdata->read(dev, reg);
  777. }
  778. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  779. {
  780. struct bq27x00_device_info *di;
  781. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  782. int ret;
  783. if (!pdata) {
  784. dev_err(&pdev->dev, "no platform_data supplied\n");
  785. return -EINVAL;
  786. }
  787. if (!pdata->read) {
  788. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  789. return -EINVAL;
  790. }
  791. di = kzalloc(sizeof(*di), GFP_KERNEL);
  792. if (!di) {
  793. dev_err(&pdev->dev, "failed to allocate device info data\n");
  794. return -ENOMEM;
  795. }
  796. platform_set_drvdata(pdev, di);
  797. di->dev = &pdev->dev;
  798. di->chip = BQ27000;
  799. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  800. di->bus.read = &bq27000_read_platform;
  801. ret = bq27x00_powersupply_init(di);
  802. if (ret)
  803. goto err_free;
  804. return 0;
  805. err_free:
  806. platform_set_drvdata(pdev, NULL);
  807. kfree(di);
  808. return ret;
  809. }
  810. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  811. {
  812. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  813. bq27x00_powersupply_unregister(di);
  814. platform_set_drvdata(pdev, NULL);
  815. kfree(di);
  816. return 0;
  817. }
  818. static struct platform_driver bq27000_battery_driver = {
  819. .probe = bq27000_battery_probe,
  820. .remove = __devexit_p(bq27000_battery_remove),
  821. .driver = {
  822. .name = "bq27000-battery",
  823. .owner = THIS_MODULE,
  824. },
  825. };
  826. static inline int bq27x00_battery_platform_init(void)
  827. {
  828. int ret = platform_driver_register(&bq27000_battery_driver);
  829. if (ret)
  830. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  831. return ret;
  832. }
  833. static inline void bq27x00_battery_platform_exit(void)
  834. {
  835. platform_driver_unregister(&bq27000_battery_driver);
  836. }
  837. #else
  838. static inline int bq27x00_battery_platform_init(void) { return 0; }
  839. static inline void bq27x00_battery_platform_exit(void) {};
  840. #endif
  841. /*
  842. * Module stuff
  843. */
  844. static int __init bq27x00_battery_init(void)
  845. {
  846. int ret;
  847. ret = bq27x00_battery_i2c_init();
  848. if (ret)
  849. return ret;
  850. ret = bq27x00_battery_platform_init();
  851. if (ret)
  852. bq27x00_battery_i2c_exit();
  853. return ret;
  854. }
  855. module_init(bq27x00_battery_init);
  856. static void __exit bq27x00_battery_exit(void)
  857. {
  858. bq27x00_battery_platform_exit();
  859. bq27x00_battery_i2c_exit();
  860. }
  861. module_exit(bq27x00_battery_exit);
  862. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  863. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  864. MODULE_LICENSE("GPL");