bq27x00_battery.c 14 KB

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