bq27x00_battery.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. retval = bq27x00_powersupply_init(di);
  695. if (retval)
  696. goto batt_failed_3;
  697. i2c_set_clientdata(client, di);
  698. return 0;
  699. batt_failed_3:
  700. kfree(di);
  701. batt_failed_2:
  702. kfree(name);
  703. batt_failed_1:
  704. mutex_lock(&battery_mutex);
  705. idr_remove(&battery_id, num);
  706. mutex_unlock(&battery_mutex);
  707. return retval;
  708. }
  709. static int bq27x00_battery_remove(struct i2c_client *client)
  710. {
  711. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  712. bq27x00_powersupply_unregister(di);
  713. kfree(di->bat.name);
  714. mutex_lock(&battery_mutex);
  715. idr_remove(&battery_id, di->id);
  716. mutex_unlock(&battery_mutex);
  717. kfree(di);
  718. return 0;
  719. }
  720. static const struct i2c_device_id bq27x00_id[] = {
  721. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  722. { "bq27500", BQ27500 },
  723. { "bq27425", BQ27425 },
  724. {},
  725. };
  726. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  727. static struct i2c_driver bq27x00_battery_driver = {
  728. .driver = {
  729. .name = "bq27x00-battery",
  730. },
  731. .probe = bq27x00_battery_probe,
  732. .remove = bq27x00_battery_remove,
  733. .id_table = bq27x00_id,
  734. };
  735. static inline int bq27x00_battery_i2c_init(void)
  736. {
  737. int ret = i2c_add_driver(&bq27x00_battery_driver);
  738. if (ret)
  739. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  740. return ret;
  741. }
  742. static inline void bq27x00_battery_i2c_exit(void)
  743. {
  744. i2c_del_driver(&bq27x00_battery_driver);
  745. }
  746. #else
  747. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  748. static inline void bq27x00_battery_i2c_exit(void) {};
  749. #endif
  750. /* platform specific code */
  751. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  752. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  753. bool single)
  754. {
  755. struct device *dev = di->dev;
  756. struct bq27000_platform_data *pdata = dev->platform_data;
  757. unsigned int timeout = 3;
  758. int upper, lower;
  759. int temp;
  760. if (!single) {
  761. /* Make sure the value has not changed in between reading the
  762. * lower and the upper part */
  763. upper = pdata->read(dev, reg + 1);
  764. do {
  765. temp = upper;
  766. if (upper < 0)
  767. return upper;
  768. lower = pdata->read(dev, reg);
  769. if (lower < 0)
  770. return lower;
  771. upper = pdata->read(dev, reg + 1);
  772. } while (temp != upper && --timeout);
  773. if (timeout == 0)
  774. return -EIO;
  775. return (upper << 8) | lower;
  776. }
  777. return pdata->read(dev, reg);
  778. }
  779. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  780. {
  781. struct bq27x00_device_info *di;
  782. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  783. int ret;
  784. if (!pdata) {
  785. dev_err(&pdev->dev, "no platform_data supplied\n");
  786. return -EINVAL;
  787. }
  788. if (!pdata->read) {
  789. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  790. return -EINVAL;
  791. }
  792. di = kzalloc(sizeof(*di), GFP_KERNEL);
  793. if (!di) {
  794. dev_err(&pdev->dev, "failed to allocate device info data\n");
  795. return -ENOMEM;
  796. }
  797. platform_set_drvdata(pdev, di);
  798. di->dev = &pdev->dev;
  799. di->chip = BQ27000;
  800. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  801. di->bus.read = &bq27000_read_platform;
  802. ret = bq27x00_powersupply_init(di);
  803. if (ret)
  804. goto err_free;
  805. return 0;
  806. err_free:
  807. platform_set_drvdata(pdev, NULL);
  808. kfree(di);
  809. return ret;
  810. }
  811. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  812. {
  813. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  814. bq27x00_powersupply_unregister(di);
  815. platform_set_drvdata(pdev, NULL);
  816. kfree(di);
  817. return 0;
  818. }
  819. static struct platform_driver bq27000_battery_driver = {
  820. .probe = bq27000_battery_probe,
  821. .remove = bq27000_battery_remove,
  822. .driver = {
  823. .name = "bq27000-battery",
  824. .owner = THIS_MODULE,
  825. },
  826. };
  827. static inline int bq27x00_battery_platform_init(void)
  828. {
  829. int ret = platform_driver_register(&bq27000_battery_driver);
  830. if (ret)
  831. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  832. return ret;
  833. }
  834. static inline void bq27x00_battery_platform_exit(void)
  835. {
  836. platform_driver_unregister(&bq27000_battery_driver);
  837. }
  838. #else
  839. static inline int bq27x00_battery_platform_init(void) { return 0; }
  840. static inline void bq27x00_battery_platform_exit(void) {};
  841. #endif
  842. /*
  843. * Module stuff
  844. */
  845. static int __init bq27x00_battery_init(void)
  846. {
  847. int ret;
  848. ret = bq27x00_battery_i2c_init();
  849. if (ret)
  850. return ret;
  851. ret = bq27x00_battery_platform_init();
  852. if (ret)
  853. bq27x00_battery_i2c_exit();
  854. return ret;
  855. }
  856. module_init(bq27x00_battery_init);
  857. static void __exit bq27x00_battery_exit(void)
  858. {
  859. bq27x00_battery_platform_exit();
  860. bq27x00_battery_i2c_exit();
  861. }
  862. module_exit(bq27x00_battery_exit);
  863. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  864. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  865. MODULE_LICENSE("GPL");