bq27x00_battery.c 21 KB

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