bq27x00_battery.c 25 KB

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