bq27x00_battery.c 21 KB

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