bq27x00_battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /* If the system has several batteries we need a different name for each
  43. * of them...
  44. */
  45. static DEFINE_IDR(battery_id);
  46. static DEFINE_MUTEX(battery_mutex);
  47. struct bq27x00_device_info;
  48. struct bq27x00_access_methods {
  49. int (*read)(u8 reg, int *rt_value, int b_single,
  50. struct bq27x00_device_info *di);
  51. };
  52. enum bq27x00_chip { BQ27000, BQ27500 };
  53. struct bq27x00_device_info {
  54. struct device *dev;
  55. int id;
  56. struct bq27x00_access_methods *bus;
  57. struct power_supply bat;
  58. enum bq27x00_chip chip;
  59. struct i2c_client *client;
  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. };
  72. /*
  73. * Common code for BQ27x00 devices
  74. */
  75. static int bq27x00_read(u8 reg, int *rt_value, int b_single,
  76. struct bq27x00_device_info *di)
  77. {
  78. return di->bus->read(reg, rt_value, b_single, di);
  79. }
  80. /*
  81. * Return the battery temperature in tenths of degree Celsius
  82. * Or < 0 if something fails.
  83. */
  84. static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
  85. {
  86. int ret;
  87. int temp = 0;
  88. ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
  89. if (ret) {
  90. dev_err(di->dev, "error reading temperature\n");
  91. return ret;
  92. }
  93. if (di->chip == BQ27500)
  94. return temp - 2731;
  95. else
  96. return ((temp >> 2) - 273) * 10;
  97. }
  98. /*
  99. * Return the battery Voltage in milivolts
  100. * Or < 0 if something fails.
  101. */
  102. static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
  103. {
  104. int ret;
  105. int volt = 0;
  106. ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
  107. if (ret) {
  108. dev_err(di->dev, "error reading voltage\n");
  109. return ret;
  110. }
  111. return volt * 1000;
  112. }
  113. /*
  114. * Return the battery average current
  115. * Note that current can be negative signed as well
  116. * Or 0 if something fails.
  117. */
  118. static int bq27x00_battery_current(struct bq27x00_device_info *di)
  119. {
  120. int ret;
  121. int curr = 0;
  122. int flags = 0;
  123. ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
  124. if (ret) {
  125. dev_err(di->dev, "error reading current\n");
  126. return 0;
  127. }
  128. if (di->chip == BQ27500) {
  129. /* bq27500 returns signed value */
  130. curr = (int)(s16)curr;
  131. } else {
  132. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  133. if (ret < 0) {
  134. dev_err(di->dev, "error reading flags\n");
  135. return 0;
  136. }
  137. if (flags & BQ27000_FLAG_CHGS) {
  138. dev_dbg(di->dev, "negative current!\n");
  139. curr = -curr;
  140. }
  141. }
  142. return curr * 1000;
  143. }
  144. /*
  145. * Return the battery Relative State-of-Charge
  146. * Or < 0 if something fails.
  147. */
  148. static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
  149. {
  150. int ret;
  151. int rsoc = 0;
  152. if (di->chip == BQ27500)
  153. ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
  154. else
  155. ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
  156. if (ret) {
  157. dev_err(di->dev, "error reading relative State-of-Charge\n");
  158. return ret;
  159. }
  160. return rsoc;
  161. }
  162. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  163. union power_supply_propval *val)
  164. {
  165. int flags = 0;
  166. int status;
  167. int ret;
  168. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  169. if (ret < 0) {
  170. dev_err(di->dev, "error reading flags\n");
  171. return ret;
  172. }
  173. if (di->chip == BQ27500) {
  174. if (flags & BQ27500_FLAG_FC)
  175. status = POWER_SUPPLY_STATUS_FULL;
  176. else if (flags & BQ27500_FLAG_DSC)
  177. status = POWER_SUPPLY_STATUS_DISCHARGING;
  178. else
  179. status = POWER_SUPPLY_STATUS_CHARGING;
  180. } else {
  181. if (flags & BQ27000_FLAG_CHGS)
  182. status = POWER_SUPPLY_STATUS_CHARGING;
  183. else
  184. status = POWER_SUPPLY_STATUS_DISCHARGING;
  185. }
  186. val->intval = status;
  187. return 0;
  188. }
  189. /*
  190. * Read a time register.
  191. * Return < 0 if something fails.
  192. */
  193. static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
  194. union power_supply_propval *val)
  195. {
  196. int tval = 0;
  197. int ret;
  198. ret = bq27x00_read(reg, &tval, 0, di);
  199. if (ret) {
  200. dev_err(di->dev, "error reading register %02x\n", reg);
  201. return ret;
  202. }
  203. if (tval == 65535)
  204. return -ENODATA;
  205. val->intval = tval * 60;
  206. return 0;
  207. }
  208. #define to_bq27x00_device_info(x) container_of((x), \
  209. struct bq27x00_device_info, bat);
  210. static int bq27x00_battery_get_property(struct power_supply *psy,
  211. enum power_supply_property psp,
  212. union power_supply_propval *val)
  213. {
  214. int ret = 0;
  215. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  216. switch (psp) {
  217. case POWER_SUPPLY_PROP_STATUS:
  218. ret = bq27x00_battery_status(di, val);
  219. break;
  220. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  221. case POWER_SUPPLY_PROP_PRESENT:
  222. val->intval = bq27x00_battery_voltage(di);
  223. if (psp == POWER_SUPPLY_PROP_PRESENT)
  224. val->intval = val->intval <= 0 ? 0 : 1;
  225. break;
  226. case POWER_SUPPLY_PROP_CURRENT_NOW:
  227. val->intval = bq27x00_battery_current(di);
  228. break;
  229. case POWER_SUPPLY_PROP_CAPACITY:
  230. val->intval = bq27x00_battery_rsoc(di);
  231. break;
  232. case POWER_SUPPLY_PROP_TEMP:
  233. val->intval = bq27x00_battery_temperature(di);
  234. break;
  235. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  236. ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  237. break;
  238. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  239. ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  240. break;
  241. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  242. ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. return ret;
  248. }
  249. static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
  250. {
  251. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  252. di->bat.properties = bq27x00_battery_props;
  253. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  254. di->bat.get_property = bq27x00_battery_get_property;
  255. di->bat.external_power_changed = NULL;
  256. }
  257. /*
  258. * i2c specific code
  259. */
  260. static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
  261. struct bq27x00_device_info *di)
  262. {
  263. struct i2c_client *client = di->client;
  264. struct i2c_msg msg[1];
  265. unsigned char data[2];
  266. int err;
  267. if (!client->adapter)
  268. return -ENODEV;
  269. msg->addr = client->addr;
  270. msg->flags = 0;
  271. msg->len = 1;
  272. msg->buf = data;
  273. data[0] = reg;
  274. err = i2c_transfer(client->adapter, msg, 1);
  275. if (err >= 0) {
  276. if (!b_single)
  277. msg->len = 2;
  278. else
  279. msg->len = 1;
  280. msg->flags = I2C_M_RD;
  281. err = i2c_transfer(client->adapter, msg, 1);
  282. if (err >= 0) {
  283. if (!b_single)
  284. *rt_value = get_unaligned_le16(data);
  285. else
  286. *rt_value = data[0];
  287. return 0;
  288. }
  289. }
  290. return err;
  291. }
  292. static int bq27x00_battery_probe(struct i2c_client *client,
  293. const struct i2c_device_id *id)
  294. {
  295. char *name;
  296. struct bq27x00_device_info *di;
  297. struct bq27x00_access_methods *bus;
  298. int num;
  299. int retval = 0;
  300. /* Get new ID for the new battery device */
  301. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  302. if (retval == 0)
  303. return -ENOMEM;
  304. mutex_lock(&battery_mutex);
  305. retval = idr_get_new(&battery_id, client, &num);
  306. mutex_unlock(&battery_mutex);
  307. if (retval < 0)
  308. return retval;
  309. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  310. if (!name) {
  311. dev_err(&client->dev, "failed to allocate device name\n");
  312. retval = -ENOMEM;
  313. goto batt_failed_1;
  314. }
  315. di = kzalloc(sizeof(*di), GFP_KERNEL);
  316. if (!di) {
  317. dev_err(&client->dev, "failed to allocate device info data\n");
  318. retval = -ENOMEM;
  319. goto batt_failed_2;
  320. }
  321. di->id = num;
  322. di->chip = id->driver_data;
  323. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  324. if (!bus) {
  325. dev_err(&client->dev, "failed to allocate access method "
  326. "data\n");
  327. retval = -ENOMEM;
  328. goto batt_failed_3;
  329. }
  330. i2c_set_clientdata(client, di);
  331. di->dev = &client->dev;
  332. di->bat.name = name;
  333. bus->read = &bq27x00_read_i2c;
  334. di->bus = bus;
  335. di->client = client;
  336. bq27x00_powersupply_init(di);
  337. retval = power_supply_register(&client->dev, &di->bat);
  338. if (retval) {
  339. dev_err(&client->dev, "failed to register battery\n");
  340. goto batt_failed_4;
  341. }
  342. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  343. return 0;
  344. batt_failed_4:
  345. kfree(bus);
  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->bus);
  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. /*
  369. * Module stuff
  370. */
  371. static const struct i2c_device_id bq27x00_id[] = {
  372. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  373. { "bq27500", BQ27500 },
  374. {},
  375. };
  376. static struct i2c_driver bq27x00_battery_driver = {
  377. .driver = {
  378. .name = "bq27x00-battery",
  379. },
  380. .probe = bq27x00_battery_probe,
  381. .remove = bq27x00_battery_remove,
  382. .id_table = bq27x00_id,
  383. };
  384. static int __init bq27x00_battery_init(void)
  385. {
  386. int ret;
  387. ret = i2c_add_driver(&bq27x00_battery_driver);
  388. if (ret)
  389. printk(KERN_ERR "Unable to register BQ27x00 driver\n");
  390. return ret;
  391. }
  392. module_init(bq27x00_battery_init);
  393. static void __exit bq27x00_battery_exit(void)
  394. {
  395. i2c_del_driver(&bq27x00_battery_driver);
  396. }
  397. module_exit(bq27x00_battery_exit);
  398. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  399. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  400. MODULE_LICENSE("GPL");