bq27x00_battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. *
  7. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  8. *
  9. * This package is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/param.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/idr.h>
  26. #include <linux/i2c.h>
  27. #include <linux/slab.h>
  28. #include <asm/unaligned.h>
  29. #define DRIVER_VERSION "1.1.0"
  30. #define BQ27x00_REG_TEMP 0x06
  31. #define BQ27x00_REG_VOLT 0x08
  32. #define BQ27x00_REG_AI 0x14
  33. #define BQ27x00_REG_FLAGS 0x0A
  34. #define BQ27x00_REG_TTE 0x16
  35. #define BQ27x00_REG_TTF 0x18
  36. #define BQ27x00_REG_TTECP 0x26
  37. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  38. #define BQ27000_FLAG_CHGS BIT(7)
  39. #define BQ27500_REG_SOC 0x2c
  40. #define BQ27500_FLAG_DSC BIT(0)
  41. #define BQ27500_FLAG_FC BIT(9)
  42. #define BQ27000_RS 20 /* Resistor sense */
  43. /* If the system has several batteries we need a different name for each
  44. * of them...
  45. */
  46. static DEFINE_IDR(battery_id);
  47. static DEFINE_MUTEX(battery_mutex);
  48. struct bq27x00_device_info;
  49. struct bq27x00_access_methods {
  50. int (*read)(struct bq27x00_device_info *, u8 reg, int *rt_value,
  51. bool single);
  52. };
  53. enum bq27x00_chip { BQ27000, BQ27500 };
  54. struct bq27x00_device_info {
  55. struct device *dev;
  56. int id;
  57. enum bq27x00_chip chip;
  58. struct power_supply bat;
  59. struct bq27x00_access_methods bus;
  60. };
  61. static enum power_supply_property bq27x00_battery_props[] = {
  62. POWER_SUPPLY_PROP_STATUS,
  63. POWER_SUPPLY_PROP_PRESENT,
  64. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  65. POWER_SUPPLY_PROP_CURRENT_NOW,
  66. POWER_SUPPLY_PROP_CAPACITY,
  67. POWER_SUPPLY_PROP_TEMP,
  68. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  69. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  70. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  71. POWER_SUPPLY_PROP_TECHNOLOGY,
  72. };
  73. /*
  74. * Common code for BQ27x00 devices
  75. */
  76. static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
  77. int *rt_value, bool single)
  78. {
  79. return di->bus.read(di, reg, rt_value, single);
  80. }
  81. /*
  82. * Return the battery temperature in tenths of degree Celsius
  83. * Or < 0 if something fails.
  84. */
  85. static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
  86. {
  87. int ret;
  88. int temp = 0;
  89. ret = bq27x00_read(di, BQ27x00_REG_TEMP, &temp, false);
  90. if (ret) {
  91. dev_err(di->dev, "error reading temperature\n");
  92. return ret;
  93. }
  94. if (di->chip == BQ27500)
  95. return temp - 2731;
  96. else
  97. return ((temp * 5) - 5463) / 2;
  98. }
  99. /*
  100. * Return the battery Voltage in milivolts
  101. * Or < 0 if something fails.
  102. */
  103. static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
  104. {
  105. int ret;
  106. int volt = 0;
  107. ret = bq27x00_read(di, BQ27x00_REG_VOLT, &volt, false);
  108. if (ret) {
  109. dev_err(di->dev, "error reading voltage\n");
  110. return ret;
  111. }
  112. return volt * 1000;
  113. }
  114. /*
  115. * Return the battery average current
  116. * Note that current can be negative signed as well
  117. * Or 0 if something fails.
  118. */
  119. static int bq27x00_battery_current(struct bq27x00_device_info *di)
  120. {
  121. int ret;
  122. int curr = 0;
  123. int flags = 0;
  124. ret = bq27x00_read(di, BQ27x00_REG_AI, &curr, false);
  125. if (ret) {
  126. dev_err(di->dev, "error reading current\n");
  127. return 0;
  128. }
  129. if (di->chip == BQ27500) {
  130. /* bq27500 returns signed value */
  131. curr = (int)((s16)curr) * 1000;
  132. } else {
  133. ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
  134. if (ret < 0) {
  135. dev_err(di->dev, "error reading flags\n");
  136. return 0;
  137. }
  138. if (flags & BQ27000_FLAG_CHGS) {
  139. dev_dbg(di->dev, "negative current!\n");
  140. curr = -curr;
  141. }
  142. curr = curr * 3570 / BQ27000_RS;
  143. }
  144. return curr;
  145. }
  146. /*
  147. * Return the battery Relative State-of-Charge
  148. * Or < 0 if something fails.
  149. */
  150. static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
  151. {
  152. int ret;
  153. int rsoc = 0;
  154. if (di->chip == BQ27500)
  155. ret = bq27x00_read(di, BQ27500_REG_SOC, &rsoc, false);
  156. else
  157. ret = bq27x00_read(di, BQ27000_REG_RSOC, &rsoc, true);
  158. if (ret) {
  159. dev_err(di->dev, "error reading relative State-of-Charge\n");
  160. return ret;
  161. }
  162. return rsoc;
  163. }
  164. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  165. union power_supply_propval *val)
  166. {
  167. int flags = 0;
  168. int status;
  169. int ret;
  170. ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
  171. if (ret < 0) {
  172. dev_err(di->dev, "error reading flags\n");
  173. return ret;
  174. }
  175. if (di->chip == BQ27500) {
  176. if (flags & BQ27500_FLAG_FC)
  177. status = POWER_SUPPLY_STATUS_FULL;
  178. else if (flags & BQ27500_FLAG_DSC)
  179. status = POWER_SUPPLY_STATUS_DISCHARGING;
  180. else
  181. status = POWER_SUPPLY_STATUS_CHARGING;
  182. } else {
  183. if (flags & BQ27000_FLAG_CHGS)
  184. status = POWER_SUPPLY_STATUS_CHARGING;
  185. else
  186. status = POWER_SUPPLY_STATUS_DISCHARGING;
  187. }
  188. val->intval = status;
  189. return 0;
  190. }
  191. /*
  192. * Read a time register.
  193. * Return < 0 if something fails.
  194. */
  195. static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
  196. union power_supply_propval *val)
  197. {
  198. int tval = 0;
  199. int ret;
  200. ret = bq27x00_read(di, reg, &tval, false);
  201. if (ret) {
  202. dev_err(di->dev, "error reading register %02x\n", reg);
  203. return ret;
  204. }
  205. if (tval == 65535)
  206. return -ENODATA;
  207. val->intval = tval * 60;
  208. return 0;
  209. }
  210. #define to_bq27x00_device_info(x) container_of((x), \
  211. struct bq27x00_device_info, bat);
  212. static int bq27x00_battery_get_property(struct power_supply *psy,
  213. enum power_supply_property psp,
  214. union power_supply_propval *val)
  215. {
  216. int ret = 0;
  217. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  218. int voltage = bq27x00_battery_voltage(di);
  219. if (psp != POWER_SUPPLY_PROP_PRESENT && voltage <= 0)
  220. return -ENODEV;
  221. switch (psp) {
  222. case POWER_SUPPLY_PROP_STATUS:
  223. ret = bq27x00_battery_status(di, val);
  224. break;
  225. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  226. val->intval = voltage;
  227. break;
  228. case POWER_SUPPLY_PROP_PRESENT:
  229. if (psp == POWER_SUPPLY_PROP_PRESENT)
  230. val->intval = voltage <= 0 ? 0 : 1;
  231. break;
  232. case POWER_SUPPLY_PROP_CURRENT_NOW:
  233. val->intval = bq27x00_battery_current(di);
  234. break;
  235. case POWER_SUPPLY_PROP_CAPACITY:
  236. val->intval = bq27x00_battery_rsoc(di);
  237. break;
  238. case POWER_SUPPLY_PROP_TEMP:
  239. val->intval = bq27x00_battery_temperature(di);
  240. break;
  241. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  242. ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  243. break;
  244. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  245. ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  246. break;
  247. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  248. ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  249. break;
  250. case POWER_SUPPLY_PROP_TECHNOLOGY:
  251. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. return ret;
  257. }
  258. static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
  259. {
  260. int ret;
  261. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  262. di->bat.properties = bq27x00_battery_props;
  263. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  264. di->bat.get_property = bq27x00_battery_get_property;
  265. di->bat.external_power_changed = NULL;
  266. ret = power_supply_register(di->dev, &di->bat);
  267. if (ret) {
  268. dev_err(di->dev, "failed to register battery: %d\n", ret);
  269. return ret;
  270. }
  271. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  272. return 0;
  273. }
  274. /*
  275. * i2c specific code
  276. */
  277. static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg,
  278. int *rt_value, bool single)
  279. {
  280. struct i2c_client *client = to_i2c_client(di->dev);
  281. struct i2c_msg msg[1];
  282. unsigned char data[2];
  283. int err;
  284. if (!client->adapter)
  285. return -ENODEV;
  286. msg->addr = client->addr;
  287. msg->flags = 0;
  288. msg->len = 1;
  289. msg->buf = data;
  290. data[0] = reg;
  291. err = i2c_transfer(client->adapter, msg, 1);
  292. if (err >= 0) {
  293. if (!single)
  294. msg->len = 2;
  295. else
  296. msg->len = 1;
  297. msg->flags = I2C_M_RD;
  298. err = i2c_transfer(client->adapter, msg, 1);
  299. if (err >= 0) {
  300. if (!single)
  301. *rt_value = get_unaligned_le16(data);
  302. else
  303. *rt_value = data[0];
  304. return 0;
  305. }
  306. }
  307. return err;
  308. }
  309. static int bq27x00_battery_probe(struct i2c_client *client,
  310. const struct i2c_device_id *id)
  311. {
  312. char *name;
  313. struct bq27x00_device_info *di;
  314. int num;
  315. int retval = 0;
  316. /* Get new ID for the new battery device */
  317. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  318. if (retval == 0)
  319. return -ENOMEM;
  320. mutex_lock(&battery_mutex);
  321. retval = idr_get_new(&battery_id, client, &num);
  322. mutex_unlock(&battery_mutex);
  323. if (retval < 0)
  324. return retval;
  325. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  326. if (!name) {
  327. dev_err(&client->dev, "failed to allocate device name\n");
  328. retval = -ENOMEM;
  329. goto batt_failed_1;
  330. }
  331. di = kzalloc(sizeof(*di), GFP_KERNEL);
  332. if (!di) {
  333. dev_err(&client->dev, "failed to allocate device info data\n");
  334. retval = -ENOMEM;
  335. goto batt_failed_2;
  336. }
  337. di->id = num;
  338. di->dev = &client->dev;
  339. di->chip = id->driver_data;
  340. di->bat.name = name;
  341. di->bus.read = &bq27x00_read_i2c;
  342. if (bq27x00_powersupply_init(di))
  343. goto batt_failed_3;
  344. i2c_set_clientdata(client, di);
  345. return 0;
  346. batt_failed_3:
  347. kfree(di);
  348. batt_failed_2:
  349. kfree(name);
  350. batt_failed_1:
  351. mutex_lock(&battery_mutex);
  352. idr_remove(&battery_id, num);
  353. mutex_unlock(&battery_mutex);
  354. return retval;
  355. }
  356. static int bq27x00_battery_remove(struct i2c_client *client)
  357. {
  358. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  359. power_supply_unregister(&di->bat);
  360. kfree(di->bat.name);
  361. mutex_lock(&battery_mutex);
  362. idr_remove(&battery_id, di->id);
  363. mutex_unlock(&battery_mutex);
  364. kfree(di);
  365. return 0;
  366. }
  367. /*
  368. * Module stuff
  369. */
  370. static const struct i2c_device_id bq27x00_id[] = {
  371. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  372. { "bq27500", BQ27500 },
  373. {},
  374. };
  375. static struct i2c_driver bq27x00_battery_driver = {
  376. .driver = {
  377. .name = "bq27x00-battery",
  378. },
  379. .probe = bq27x00_battery_probe,
  380. .remove = bq27x00_battery_remove,
  381. .id_table = bq27x00_id,
  382. };
  383. static int __init bq27x00_battery_init(void)
  384. {
  385. int ret;
  386. ret = i2c_add_driver(&bq27x00_battery_driver);
  387. if (ret)
  388. printk(KERN_ERR "Unable to register BQ27x00 driver\n");
  389. return ret;
  390. }
  391. module_init(bq27x00_battery_init);
  392. static void __exit bq27x00_battery_exit(void)
  393. {
  394. i2c_del_driver(&bq27x00_battery_driver);
  395. }
  396. module_exit(bq27x00_battery_exit);
  397. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  398. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  399. MODULE_LICENSE("GPL");