bq27x00_battery.c 26 KB

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