bq27x00_battery.c 22 KB

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