bq27x00_battery.c 23 KB

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