bq27x00_battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 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_DSC BIT(0)
  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_FC BIT(9)
  62. #define BQ27000_RS 20 /* Resistor sense */
  63. struct bq27x00_device_info;
  64. struct bq27x00_access_methods {
  65. int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  66. };
  67. enum bq27x00_chip { BQ27000, BQ27500 };
  68. struct bq27x00_reg_cache {
  69. int temperature;
  70. int time_to_empty;
  71. int time_to_empty_avg;
  72. int time_to_full;
  73. int charge_full;
  74. int cycle_count;
  75. int capacity;
  76. int energy;
  77. int flags;
  78. };
  79. struct bq27x00_device_info {
  80. struct device *dev;
  81. int id;
  82. enum bq27x00_chip chip;
  83. struct bq27x00_reg_cache cache;
  84. int charge_design_full;
  85. unsigned long last_update;
  86. struct delayed_work work;
  87. struct power_supply bat;
  88. struct bq27x00_access_methods bus;
  89. struct mutex lock;
  90. };
  91. static enum power_supply_property bq27x00_battery_props[] = {
  92. POWER_SUPPLY_PROP_STATUS,
  93. POWER_SUPPLY_PROP_PRESENT,
  94. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  95. POWER_SUPPLY_PROP_CURRENT_NOW,
  96. POWER_SUPPLY_PROP_CAPACITY,
  97. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  98. POWER_SUPPLY_PROP_TEMP,
  99. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  100. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  101. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  102. POWER_SUPPLY_PROP_TECHNOLOGY,
  103. POWER_SUPPLY_PROP_CHARGE_FULL,
  104. POWER_SUPPLY_PROP_CHARGE_NOW,
  105. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  106. POWER_SUPPLY_PROP_CYCLE_COUNT,
  107. POWER_SUPPLY_PROP_ENERGY_NOW,
  108. };
  109. static unsigned int poll_interval = 360;
  110. module_param(poll_interval, uint, 0644);
  111. MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
  112. "0 disables polling");
  113. /*
  114. * Common code for BQ27x00 devices
  115. */
  116. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  117. bool single)
  118. {
  119. return di->bus.read(di, reg, single);
  120. }
  121. /*
  122. * Return the battery Relative State-of-Charge
  123. * Or < 0 if something fails.
  124. */
  125. static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
  126. {
  127. int rsoc;
  128. if (di->chip == BQ27500)
  129. rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
  130. else
  131. rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
  132. if (rsoc < 0)
  133. dev_dbg(di->dev, "error reading relative State-of-Charge\n");
  134. return rsoc;
  135. }
  136. /*
  137. * Return a battery charge value in µAh
  138. * Or < 0 if something fails.
  139. */
  140. static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
  141. {
  142. int charge;
  143. charge = bq27x00_read(di, reg, false);
  144. if (charge < 0) {
  145. dev_dbg(di->dev, "error reading charge register %02x: %d\n",
  146. reg, charge);
  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_dbg(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_dbg(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_dbg(di->dev, "error reading time register %02x: %d\n",
  250. reg, tval);
  251. return tval;
  252. }
  253. if (tval == 65535)
  254. return -ENODATA;
  255. return tval * 60;
  256. }
  257. static void bq27x00_update(struct bq27x00_device_info *di)
  258. {
  259. struct bq27x00_reg_cache cache = {0, };
  260. bool is_bq27500 = di->chip == BQ27500;
  261. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
  262. if (cache.flags >= 0) {
  263. if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
  264. dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
  265. cache.capacity = -ENODATA;
  266. cache.energy = -ENODATA;
  267. cache.time_to_empty = -ENODATA;
  268. cache.time_to_empty_avg = -ENODATA;
  269. cache.time_to_full = -ENODATA;
  270. cache.charge_full = -ENODATA;
  271. } else {
  272. cache.capacity = bq27x00_battery_read_rsoc(di);
  273. cache.energy = bq27x00_battery_read_energy(di);
  274. cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
  275. cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
  276. cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
  277. cache.charge_full = bq27x00_battery_read_lmd(di);
  278. }
  279. cache.temperature = bq27x00_battery_read_temperature(di);
  280. cache.cycle_count = bq27x00_battery_read_cyct(di);
  281. /* We only have to read charge design full once */
  282. if (di->charge_design_full <= 0)
  283. di->charge_design_full = bq27x00_battery_read_ilmd(di);
  284. }
  285. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
  286. di->cache = cache;
  287. power_supply_changed(&di->bat);
  288. }
  289. di->last_update = jiffies;
  290. }
  291. static void bq27x00_battery_poll(struct work_struct *work)
  292. {
  293. struct bq27x00_device_info *di =
  294. container_of(work, struct bq27x00_device_info, work.work);
  295. bq27x00_update(di);
  296. if (poll_interval > 0) {
  297. /* The timer does not have to be accurate. */
  298. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  299. schedule_delayed_work(&di->work, poll_interval * HZ);
  300. }
  301. }
  302. /*
  303. * Return the battery average current in µA
  304. * Note that current can be negative signed as well
  305. * Or 0 if something fails.
  306. */
  307. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  308. union power_supply_propval *val)
  309. {
  310. int curr;
  311. int flags;
  312. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  313. if (curr < 0) {
  314. dev_err(di->dev, "error reading current\n");
  315. return curr;
  316. }
  317. if (di->chip == BQ27500) {
  318. /* bq27500 returns signed value */
  319. val->intval = (int)((s16)curr) * 1000;
  320. } else {
  321. flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
  322. if (flags & BQ27000_FLAG_CHGS) {
  323. dev_dbg(di->dev, "negative current!\n");
  324. curr = -curr;
  325. }
  326. val->intval = curr * 3570 / BQ27000_RS;
  327. }
  328. return 0;
  329. }
  330. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  331. union power_supply_propval *val)
  332. {
  333. int status;
  334. if (di->chip == BQ27500) {
  335. if (di->cache.flags & BQ27500_FLAG_FC)
  336. status = POWER_SUPPLY_STATUS_FULL;
  337. else if (di->cache.flags & BQ27500_FLAG_DSC)
  338. status = POWER_SUPPLY_STATUS_DISCHARGING;
  339. else
  340. status = POWER_SUPPLY_STATUS_CHARGING;
  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. dev_err(di->dev, "error reading voltage\n");
  391. return volt;
  392. }
  393. val->intval = volt * 1000;
  394. return 0;
  395. }
  396. static int bq27x00_simple_value(int value,
  397. union power_supply_propval *val)
  398. {
  399. if (value < 0)
  400. return value;
  401. val->intval = value;
  402. return 0;
  403. }
  404. #define to_bq27x00_device_info(x) container_of((x), \
  405. struct bq27x00_device_info, bat);
  406. static int bq27x00_battery_get_property(struct power_supply *psy,
  407. enum power_supply_property psp,
  408. union power_supply_propval *val)
  409. {
  410. int ret = 0;
  411. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  412. mutex_lock(&di->lock);
  413. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  414. cancel_delayed_work_sync(&di->work);
  415. bq27x00_battery_poll(&di->work.work);
  416. }
  417. mutex_unlock(&di->lock);
  418. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  419. return -ENODEV;
  420. switch (psp) {
  421. case POWER_SUPPLY_PROP_STATUS:
  422. ret = bq27x00_battery_status(di, val);
  423. break;
  424. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  425. ret = bq27x00_battery_voltage(di, val);
  426. break;
  427. case POWER_SUPPLY_PROP_PRESENT:
  428. val->intval = di->cache.flags < 0 ? 0 : 1;
  429. break;
  430. case POWER_SUPPLY_PROP_CURRENT_NOW:
  431. ret = bq27x00_battery_current(di, val);
  432. break;
  433. case POWER_SUPPLY_PROP_CAPACITY:
  434. ret = bq27x00_simple_value(di->cache.capacity, val);
  435. break;
  436. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  437. ret = bq27x00_battery_capacity_level(di, val);
  438. break;
  439. case POWER_SUPPLY_PROP_TEMP:
  440. ret = bq27x00_simple_value(di->cache.temperature, val);
  441. break;
  442. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  443. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  444. break;
  445. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  446. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  447. break;
  448. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  449. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  450. break;
  451. case POWER_SUPPLY_PROP_TECHNOLOGY:
  452. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  453. break;
  454. case POWER_SUPPLY_PROP_CHARGE_NOW:
  455. ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
  456. break;
  457. case POWER_SUPPLY_PROP_CHARGE_FULL:
  458. ret = bq27x00_simple_value(di->cache.charge_full, val);
  459. break;
  460. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  461. ret = bq27x00_simple_value(di->charge_design_full, val);
  462. break;
  463. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  464. ret = bq27x00_simple_value(di->cache.cycle_count, val);
  465. break;
  466. case POWER_SUPPLY_PROP_ENERGY_NOW:
  467. ret = bq27x00_simple_value(di->cache.energy, val);
  468. break;
  469. default:
  470. return -EINVAL;
  471. }
  472. return ret;
  473. }
  474. static void bq27x00_external_power_changed(struct power_supply *psy)
  475. {
  476. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  477. cancel_delayed_work_sync(&di->work);
  478. schedule_delayed_work(&di->work, 0);
  479. }
  480. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  481. {
  482. int ret;
  483. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  484. di->bat.properties = bq27x00_battery_props;
  485. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  486. di->bat.get_property = bq27x00_battery_get_property;
  487. di->bat.external_power_changed = bq27x00_external_power_changed;
  488. INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
  489. mutex_init(&di->lock);
  490. ret = power_supply_register(di->dev, &di->bat);
  491. if (ret) {
  492. dev_err(di->dev, "failed to register battery: %d\n", ret);
  493. return ret;
  494. }
  495. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  496. bq27x00_update(di);
  497. return 0;
  498. }
  499. static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
  500. {
  501. /*
  502. * power_supply_unregister call bq27x00_battery_get_property which
  503. * call bq27x00_battery_poll.
  504. * Make sure that bq27x00_battery_poll will not call
  505. * schedule_delayed_work again after unregister (which cause OOPS).
  506. */
  507. poll_interval = 0;
  508. cancel_delayed_work_sync(&di->work);
  509. power_supply_unregister(&di->bat);
  510. mutex_destroy(&di->lock);
  511. }
  512. /* i2c specific code */
  513. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  514. /* If the system has several batteries we need a different name for each
  515. * of them...
  516. */
  517. static DEFINE_IDR(battery_id);
  518. static DEFINE_MUTEX(battery_mutex);
  519. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  520. {
  521. struct i2c_client *client = to_i2c_client(di->dev);
  522. struct i2c_msg msg[2];
  523. unsigned char data[2];
  524. int ret;
  525. if (!client->adapter)
  526. return -ENODEV;
  527. msg[0].addr = client->addr;
  528. msg[0].flags = 0;
  529. msg[0].buf = &reg;
  530. msg[0].len = sizeof(reg);
  531. msg[1].addr = client->addr;
  532. msg[1].flags = I2C_M_RD;
  533. msg[1].buf = data;
  534. if (single)
  535. msg[1].len = 1;
  536. else
  537. msg[1].len = 2;
  538. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  539. if (ret < 0)
  540. return ret;
  541. if (!single)
  542. ret = get_unaligned_le16(data);
  543. else
  544. ret = data[0];
  545. return ret;
  546. }
  547. static int bq27x00_battery_probe(struct i2c_client *client,
  548. const struct i2c_device_id *id)
  549. {
  550. char *name;
  551. struct bq27x00_device_info *di;
  552. int num;
  553. int retval = 0;
  554. /* Get new ID for the new battery device */
  555. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  556. if (retval == 0)
  557. return -ENOMEM;
  558. mutex_lock(&battery_mutex);
  559. retval = idr_get_new(&battery_id, client, &num);
  560. mutex_unlock(&battery_mutex);
  561. if (retval < 0)
  562. return retval;
  563. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  564. if (!name) {
  565. dev_err(&client->dev, "failed to allocate device name\n");
  566. retval = -ENOMEM;
  567. goto batt_failed_1;
  568. }
  569. di = kzalloc(sizeof(*di), GFP_KERNEL);
  570. if (!di) {
  571. dev_err(&client->dev, "failed to allocate device info data\n");
  572. retval = -ENOMEM;
  573. goto batt_failed_2;
  574. }
  575. di->id = num;
  576. di->dev = &client->dev;
  577. di->chip = id->driver_data;
  578. di->bat.name = name;
  579. di->bus.read = &bq27x00_read_i2c;
  580. if (bq27x00_powersupply_init(di))
  581. goto batt_failed_3;
  582. i2c_set_clientdata(client, di);
  583. return 0;
  584. batt_failed_3:
  585. kfree(di);
  586. batt_failed_2:
  587. kfree(name);
  588. batt_failed_1:
  589. mutex_lock(&battery_mutex);
  590. idr_remove(&battery_id, num);
  591. mutex_unlock(&battery_mutex);
  592. return retval;
  593. }
  594. static int bq27x00_battery_remove(struct i2c_client *client)
  595. {
  596. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  597. bq27x00_powersupply_unregister(di);
  598. kfree(di->bat.name);
  599. mutex_lock(&battery_mutex);
  600. idr_remove(&battery_id, di->id);
  601. mutex_unlock(&battery_mutex);
  602. kfree(di);
  603. return 0;
  604. }
  605. static const struct i2c_device_id bq27x00_id[] = {
  606. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  607. { "bq27500", BQ27500 },
  608. {},
  609. };
  610. MODULE_DEVICE_TABLE(i2c, bq27x00_id);
  611. static struct i2c_driver bq27x00_battery_driver = {
  612. .driver = {
  613. .name = "bq27x00-battery",
  614. },
  615. .probe = bq27x00_battery_probe,
  616. .remove = bq27x00_battery_remove,
  617. .id_table = bq27x00_id,
  618. };
  619. static inline int bq27x00_battery_i2c_init(void)
  620. {
  621. int ret = i2c_add_driver(&bq27x00_battery_driver);
  622. if (ret)
  623. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  624. return ret;
  625. }
  626. static inline void bq27x00_battery_i2c_exit(void)
  627. {
  628. i2c_del_driver(&bq27x00_battery_driver);
  629. }
  630. #else
  631. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  632. static inline void bq27x00_battery_i2c_exit(void) {};
  633. #endif
  634. /* platform specific code */
  635. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  636. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  637. bool single)
  638. {
  639. struct device *dev = di->dev;
  640. struct bq27000_platform_data *pdata = dev->platform_data;
  641. unsigned int timeout = 3;
  642. int upper, lower;
  643. int temp;
  644. if (!single) {
  645. /* Make sure the value has not changed in between reading the
  646. * lower and the upper part */
  647. upper = pdata->read(dev, reg + 1);
  648. do {
  649. temp = upper;
  650. if (upper < 0)
  651. return upper;
  652. lower = pdata->read(dev, reg);
  653. if (lower < 0)
  654. return lower;
  655. upper = pdata->read(dev, reg + 1);
  656. } while (temp != upper && --timeout);
  657. if (timeout == 0)
  658. return -EIO;
  659. return (upper << 8) | lower;
  660. }
  661. return pdata->read(dev, reg);
  662. }
  663. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  664. {
  665. struct bq27x00_device_info *di;
  666. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  667. int ret;
  668. if (!pdata) {
  669. dev_err(&pdev->dev, "no platform_data supplied\n");
  670. return -EINVAL;
  671. }
  672. if (!pdata->read) {
  673. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  674. return -EINVAL;
  675. }
  676. di = kzalloc(sizeof(*di), GFP_KERNEL);
  677. if (!di) {
  678. dev_err(&pdev->dev, "failed to allocate device info data\n");
  679. return -ENOMEM;
  680. }
  681. platform_set_drvdata(pdev, di);
  682. di->dev = &pdev->dev;
  683. di->chip = BQ27000;
  684. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  685. di->bus.read = &bq27000_read_platform;
  686. ret = bq27x00_powersupply_init(di);
  687. if (ret)
  688. goto err_free;
  689. return 0;
  690. err_free:
  691. platform_set_drvdata(pdev, NULL);
  692. kfree(di);
  693. return ret;
  694. }
  695. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  696. {
  697. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  698. bq27x00_powersupply_unregister(di);
  699. platform_set_drvdata(pdev, NULL);
  700. kfree(di);
  701. return 0;
  702. }
  703. static struct platform_driver bq27000_battery_driver = {
  704. .probe = bq27000_battery_probe,
  705. .remove = __devexit_p(bq27000_battery_remove),
  706. .driver = {
  707. .name = "bq27000-battery",
  708. .owner = THIS_MODULE,
  709. },
  710. };
  711. static inline int bq27x00_battery_platform_init(void)
  712. {
  713. int ret = platform_driver_register(&bq27000_battery_driver);
  714. if (ret)
  715. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  716. return ret;
  717. }
  718. static inline void bq27x00_battery_platform_exit(void)
  719. {
  720. platform_driver_unregister(&bq27000_battery_driver);
  721. }
  722. #else
  723. static inline int bq27x00_battery_platform_init(void) { return 0; }
  724. static inline void bq27x00_battery_platform_exit(void) {};
  725. #endif
  726. /*
  727. * Module stuff
  728. */
  729. static int __init bq27x00_battery_init(void)
  730. {
  731. int ret;
  732. ret = bq27x00_battery_i2c_init();
  733. if (ret)
  734. return ret;
  735. ret = bq27x00_battery_platform_init();
  736. if (ret)
  737. bq27x00_battery_i2c_exit();
  738. return ret;
  739. }
  740. module_init(bq27x00_battery_init);
  741. static void __exit bq27x00_battery_exit(void)
  742. {
  743. bq27x00_battery_platform_exit();
  744. bq27x00_battery_i2c_exit();
  745. }
  746. module_exit(bq27x00_battery_exit);
  747. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  748. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  749. MODULE_LICENSE("GPL");