bq27x00_battery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. *
  8. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  9. *
  10. * This package is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/param.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/idr.h>
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <asm/unaligned.h>
  30. #include <linux/power/bq27x00_battery.h>
  31. #define DRIVER_VERSION "1.1.0"
  32. #define BQ27x00_REG_TEMP 0x06
  33. #define BQ27x00_REG_VOLT 0x08
  34. #define BQ27x00_REG_AI 0x14
  35. #define BQ27x00_REG_FLAGS 0x0A
  36. #define BQ27x00_REG_TTE 0x16
  37. #define BQ27x00_REG_TTF 0x18
  38. #define BQ27x00_REG_TTECP 0x26
  39. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  40. #define BQ27000_FLAG_CHGS BIT(7)
  41. #define BQ27500_REG_SOC 0x2c
  42. #define BQ27500_FLAG_DSC BIT(0)
  43. #define BQ27500_FLAG_FC BIT(9)
  44. #define BQ27000_RS 20 /* Resistor sense */
  45. struct bq27x00_device_info;
  46. struct bq27x00_access_methods {
  47. int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  48. };
  49. enum bq27x00_chip { BQ27000, BQ27500 };
  50. struct bq27x00_reg_cache {
  51. int temperature;
  52. int time_to_empty;
  53. int time_to_empty_avg;
  54. int time_to_full;
  55. int capacity;
  56. int flags;
  57. int current_now;
  58. };
  59. struct bq27x00_device_info {
  60. struct device *dev;
  61. int id;
  62. enum bq27x00_chip chip;
  63. struct bq27x00_reg_cache cache;
  64. unsigned long last_update;
  65. struct power_supply bat;
  66. struct bq27x00_access_methods bus;
  67. };
  68. static enum power_supply_property bq27x00_battery_props[] = {
  69. POWER_SUPPLY_PROP_STATUS,
  70. POWER_SUPPLY_PROP_PRESENT,
  71. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  72. POWER_SUPPLY_PROP_CURRENT_NOW,
  73. POWER_SUPPLY_PROP_CAPACITY,
  74. POWER_SUPPLY_PROP_TEMP,
  75. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  76. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  77. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  78. POWER_SUPPLY_PROP_TECHNOLOGY,
  79. };
  80. /*
  81. * Common code for BQ27x00 devices
  82. */
  83. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  84. bool single)
  85. {
  86. return di->bus.read(di, reg, single);
  87. }
  88. /*
  89. * Return the battery Relative State-of-Charge
  90. * Or < 0 if something fails.
  91. */
  92. static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
  93. {
  94. int rsoc;
  95. if (di->chip == BQ27500)
  96. rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
  97. else
  98. rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
  99. if (rsoc < 0)
  100. dev_err(di->dev, "error reading relative State-of-Charge\n");
  101. return rsoc;
  102. }
  103. /*
  104. * Read a time register.
  105. * Return < 0 if something fails.
  106. */
  107. static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
  108. {
  109. int tval;
  110. tval = bq27x00_read(di, reg, false);
  111. if (tval < 0) {
  112. dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
  113. return tval;
  114. }
  115. if (tval == 65535)
  116. return -ENODATA;
  117. return tval * 60;
  118. }
  119. static void bq27x00_update(struct bq27x00_device_info *di)
  120. {
  121. struct bq27x00_reg_cache cache = {0, };
  122. bool is_bq27500 = di->chip == BQ27500;
  123. cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
  124. if (cache.flags >= 0) {
  125. cache.capacity = bq27x00_battery_read_rsoc(di);
  126. cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
  127. cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
  128. cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
  129. cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
  130. if (!is_bq27500)
  131. cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
  132. }
  133. /* Ignore current_now which is a snapshot of the current battery state
  134. * and is likely to be different even between two consecutive reads */
  135. if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
  136. di->cache = cache;
  137. power_supply_changed(&di->bat);
  138. }
  139. di->last_update = jiffies;
  140. }
  141. /*
  142. * Return the battery temperature in tenths of degree Celsius
  143. * Or < 0 if something fails.
  144. */
  145. static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
  146. union power_supply_propval *val)
  147. {
  148. if (di->cache.temperature < 0)
  149. return di->cache.temperature;
  150. if (di->chip == BQ27500)
  151. val->intval = di->cache.temperature - 2731;
  152. else
  153. val->intval = ((di->cache.temperature * 5) - 5463) / 2;
  154. return 0;
  155. }
  156. /*
  157. * Return the battery average current
  158. * Note that current can be negative signed as well
  159. * Or 0 if something fails.
  160. */
  161. static int bq27x00_battery_current(struct bq27x00_device_info *di,
  162. union power_supply_propval *val)
  163. {
  164. int curr;
  165. if (di->chip == BQ27500)
  166. curr = bq27x00_read(di, BQ27x00_REG_AI, false);
  167. else
  168. curr = di->cache.current_now;
  169. if (curr < 0)
  170. return curr;
  171. if (di->chip == BQ27500) {
  172. /* bq27500 returns signed value */
  173. val->intval = (int)((s16)curr) * 1000;
  174. } else {
  175. if (di->cache.flags & BQ27000_FLAG_CHGS) {
  176. dev_dbg(di->dev, "negative current!\n");
  177. curr = -curr;
  178. }
  179. val->intval = curr * 3570 / BQ27000_RS;
  180. }
  181. return 0;
  182. }
  183. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  184. union power_supply_propval *val)
  185. {
  186. int status;
  187. if (di->chip == BQ27500) {
  188. if (di->cache.flags & BQ27500_FLAG_FC)
  189. status = POWER_SUPPLY_STATUS_FULL;
  190. else if (di->cache.flags & BQ27500_FLAG_DSC)
  191. status = POWER_SUPPLY_STATUS_DISCHARGING;
  192. else
  193. status = POWER_SUPPLY_STATUS_CHARGING;
  194. } else {
  195. if (di->cache.flags & BQ27000_FLAG_CHGS)
  196. status = POWER_SUPPLY_STATUS_CHARGING;
  197. else
  198. status = POWER_SUPPLY_STATUS_DISCHARGING;
  199. }
  200. val->intval = status;
  201. return 0;
  202. }
  203. /*
  204. * Return the battery Voltage in milivolts
  205. * Or < 0 if something fails.
  206. */
  207. static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
  208. union power_supply_propval *val)
  209. {
  210. int volt;
  211. volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
  212. if (volt < 0)
  213. return volt;
  214. val->intval = volt * 1000;
  215. return 0;
  216. }
  217. static int bq27x00_simple_value(int value,
  218. union power_supply_propval *val)
  219. {
  220. if (value < 0)
  221. return value;
  222. val->intval = value;
  223. return 0;
  224. }
  225. #define to_bq27x00_device_info(x) container_of((x), \
  226. struct bq27x00_device_info, bat);
  227. static int bq27x00_battery_get_property(struct power_supply *psy,
  228. enum power_supply_property psp,
  229. union power_supply_propval *val)
  230. {
  231. int ret = 0;
  232. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  233. if (time_is_before_jiffies(di->last_update + 5 * HZ))
  234. bq27x00_update(di);
  235. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  236. return -ENODEV;
  237. switch (psp) {
  238. case POWER_SUPPLY_PROP_STATUS:
  239. ret = bq27x00_battery_status(di, val);
  240. break;
  241. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  242. ret = bq27x00_battery_voltage(di, val);
  243. break;
  244. case POWER_SUPPLY_PROP_PRESENT:
  245. val->intval = di->cache.flags < 0 ? 0 : 1;
  246. break;
  247. case POWER_SUPPLY_PROP_CURRENT_NOW:
  248. ret = bq27x00_battery_current(di, val);
  249. break;
  250. case POWER_SUPPLY_PROP_CAPACITY:
  251. ret = bq27x00_simple_value(di->cache.capacity, val);
  252. break;
  253. case POWER_SUPPLY_PROP_TEMP:
  254. ret = bq27x00_battery_temperature(di, val);
  255. break;
  256. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  257. ret = bq27x00_simple_value(di->cache.time_to_empty, val);
  258. break;
  259. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  260. ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
  261. break;
  262. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  263. ret = bq27x00_simple_value(di->cache.time_to_full, val);
  264. break;
  265. case POWER_SUPPLY_PROP_TECHNOLOGY:
  266. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. return ret;
  272. }
  273. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  274. {
  275. int ret;
  276. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  277. di->bat.properties = bq27x00_battery_props;
  278. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  279. di->bat.get_property = bq27x00_battery_get_property;
  280. di->bat.external_power_changed = NULL;
  281. ret = power_supply_register(di->dev, &di->bat);
  282. if (ret) {
  283. dev_err(di->dev, "failed to register battery: %d\n", ret);
  284. return ret;
  285. }
  286. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  287. bq27x00_update(di);
  288. return 0;
  289. }
  290. /* i2c specific code */
  291. #ifdef CONFIG_BATTERY_BQ27X00_I2C
  292. /* If the system has several batteries we need a different name for each
  293. * of them...
  294. */
  295. static DEFINE_IDR(battery_id);
  296. static DEFINE_MUTEX(battery_mutex);
  297. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
  298. {
  299. struct i2c_client *client = to_i2c_client(di->dev);
  300. struct i2c_msg msg[1];
  301. unsigned char data[2];
  302. int ret;
  303. if (!client->adapter)
  304. return -ENODEV;
  305. msg->addr = client->addr;
  306. msg->flags = 0;
  307. msg->len = 1;
  308. msg->buf = data;
  309. data[0] = reg;
  310. ret = i2c_transfer(client->adapter, msg, 1);
  311. if (ret >= 0) {
  312. if (!single)
  313. msg->len = 2;
  314. else
  315. msg->len = 1;
  316. msg->flags = I2C_M_RD;
  317. ret = i2c_transfer(client->adapter, msg, 1);
  318. if (ret >= 0) {
  319. if (!single)
  320. ret = get_unaligned_le16(data);
  321. else
  322. ret = data[0];
  323. }
  324. }
  325. return ret;
  326. }
  327. static int bq27x00_battery_probe(struct i2c_client *client,
  328. const struct i2c_device_id *id)
  329. {
  330. char *name;
  331. struct bq27x00_device_info *di;
  332. int num;
  333. int retval = 0;
  334. /* Get new ID for the new battery device */
  335. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  336. if (retval == 0)
  337. return -ENOMEM;
  338. mutex_lock(&battery_mutex);
  339. retval = idr_get_new(&battery_id, client, &num);
  340. mutex_unlock(&battery_mutex);
  341. if (retval < 0)
  342. return retval;
  343. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  344. if (!name) {
  345. dev_err(&client->dev, "failed to allocate device name\n");
  346. retval = -ENOMEM;
  347. goto batt_failed_1;
  348. }
  349. di = kzalloc(sizeof(*di), GFP_KERNEL);
  350. if (!di) {
  351. dev_err(&client->dev, "failed to allocate device info data\n");
  352. retval = -ENOMEM;
  353. goto batt_failed_2;
  354. }
  355. di->id = num;
  356. di->dev = &client->dev;
  357. di->chip = id->driver_data;
  358. di->bat.name = name;
  359. di->bus.read = &bq27x00_read_i2c;
  360. if (bq27x00_powersupply_init(di))
  361. goto batt_failed_3;
  362. i2c_set_clientdata(client, di);
  363. return 0;
  364. batt_failed_3:
  365. kfree(di);
  366. batt_failed_2:
  367. kfree(name);
  368. batt_failed_1:
  369. mutex_lock(&battery_mutex);
  370. idr_remove(&battery_id, num);
  371. mutex_unlock(&battery_mutex);
  372. return retval;
  373. }
  374. static int bq27x00_battery_remove(struct i2c_client *client)
  375. {
  376. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  377. power_supply_unregister(&di->bat);
  378. kfree(di->bat.name);
  379. mutex_lock(&battery_mutex);
  380. idr_remove(&battery_id, di->id);
  381. mutex_unlock(&battery_mutex);
  382. kfree(di);
  383. return 0;
  384. }
  385. static const struct i2c_device_id bq27x00_id[] = {
  386. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  387. { "bq27500", BQ27500 },
  388. {},
  389. };
  390. static struct i2c_driver bq27x00_battery_driver = {
  391. .driver = {
  392. .name = "bq27x00-battery",
  393. },
  394. .probe = bq27x00_battery_probe,
  395. .remove = bq27x00_battery_remove,
  396. .id_table = bq27x00_id,
  397. };
  398. static inline int bq27x00_battery_i2c_init(void)
  399. {
  400. int ret = i2c_add_driver(&bq27x00_battery_driver);
  401. if (ret)
  402. printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
  403. return ret;
  404. }
  405. static inline void bq27x00_battery_i2c_exit(void)
  406. {
  407. i2c_del_driver(&bq27x00_battery_driver);
  408. }
  409. #else
  410. static inline int bq27x00_battery_i2c_init(void) { return 0; }
  411. static inline void bq27x00_battery_i2c_exit(void) {};
  412. #endif
  413. /* platform specific code */
  414. #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
  415. static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
  416. bool single)
  417. {
  418. struct device *dev = di->dev;
  419. struct bq27000_platform_data *pdata = dev->platform_data;
  420. unsigned int timeout = 3;
  421. int upper, lower;
  422. int temp;
  423. if (!single) {
  424. /* Make sure the value has not changed in between reading the
  425. * lower and the upper part */
  426. upper = pdata->read(dev, reg + 1);
  427. do {
  428. temp = upper;
  429. if (upper < 0)
  430. return upper;
  431. lower = pdata->read(dev, reg);
  432. if (lower < 0)
  433. return lower;
  434. upper = pdata->read(dev, reg + 1);
  435. } while (temp != upper && --timeout);
  436. if (timeout == 0)
  437. return -EIO;
  438. return (upper << 8) | lower;
  439. }
  440. return pdata->read(dev, reg);
  441. }
  442. static int __devinit bq27000_battery_probe(struct platform_device *pdev)
  443. {
  444. struct bq27x00_device_info *di;
  445. struct bq27000_platform_data *pdata = pdev->dev.platform_data;
  446. int ret;
  447. if (!pdata) {
  448. dev_err(&pdev->dev, "no platform_data supplied\n");
  449. return -EINVAL;
  450. }
  451. if (!pdata->read) {
  452. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  453. return -EINVAL;
  454. }
  455. di = kzalloc(sizeof(*di), GFP_KERNEL);
  456. if (!di) {
  457. dev_err(&pdev->dev, "failed to allocate device info data\n");
  458. return -ENOMEM;
  459. }
  460. platform_set_drvdata(pdev, di);
  461. di->dev = &pdev->dev;
  462. di->chip = BQ27000;
  463. di->bat.name = pdata->name ?: dev_name(&pdev->dev);
  464. di->bus.read = &bq27000_read_platform;
  465. ret = bq27x00_powersupply_init(di);
  466. if (ret)
  467. goto err_free;
  468. return 0;
  469. err_free:
  470. platform_set_drvdata(pdev, NULL);
  471. kfree(di);
  472. return ret;
  473. }
  474. static int __devexit bq27000_battery_remove(struct platform_device *pdev)
  475. {
  476. struct bq27x00_device_info *di = platform_get_drvdata(pdev);
  477. power_supply_unregister(&di->bat);
  478. platform_set_drvdata(pdev, NULL);
  479. kfree(di);
  480. return 0;
  481. }
  482. static struct platform_driver bq27000_battery_driver = {
  483. .probe = bq27000_battery_probe,
  484. .remove = __devexit_p(bq27000_battery_remove),
  485. .driver = {
  486. .name = "bq27000-battery",
  487. .owner = THIS_MODULE,
  488. },
  489. };
  490. static inline int bq27x00_battery_platform_init(void)
  491. {
  492. int ret = platform_driver_register(&bq27000_battery_driver);
  493. if (ret)
  494. printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
  495. return ret;
  496. }
  497. static inline void bq27x00_battery_platform_exit(void)
  498. {
  499. platform_driver_unregister(&bq27000_battery_driver);
  500. }
  501. #else
  502. static inline int bq27x00_battery_platform_init(void) { return 0; }
  503. static inline void bq27x00_battery_platform_exit(void) {};
  504. #endif
  505. /*
  506. * Module stuff
  507. */
  508. static int __init bq27x00_battery_init(void)
  509. {
  510. int ret;
  511. ret = bq27x00_battery_i2c_init();
  512. if (ret)
  513. return ret;
  514. ret = bq27x00_battery_platform_init();
  515. if (ret)
  516. bq27x00_battery_i2c_exit();
  517. return ret;
  518. }
  519. module_init(bq27x00_battery_init);
  520. static void __exit bq27x00_battery_exit(void)
  521. {
  522. bq27x00_battery_platform_exit();
  523. bq27x00_battery_i2c_exit();
  524. }
  525. module_exit(bq27x00_battery_exit);
  526. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  527. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  528. MODULE_LICENSE("GPL");