bq27x00_battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 <asm/unaligned.h>
  28. #define DRIVER_VERSION "1.1.0"
  29. #define BQ27x00_REG_TEMP 0x06
  30. #define BQ27x00_REG_VOLT 0x08
  31. #define BQ27x00_REG_AI 0x14
  32. #define BQ27x00_REG_FLAGS 0x0A
  33. #define BQ27x00_REG_TTE 0x16
  34. #define BQ27x00_REG_TTF 0x18
  35. #define BQ27x00_REG_TTECP 0x26
  36. #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
  37. #define BQ27000_FLAG_CHGS BIT(7)
  38. #define BQ27500_REG_SOC 0x2c
  39. #define BQ27500_FLAG_DSC BIT(0)
  40. #define BQ27500_FLAG_FC BIT(9)
  41. /* If the system has several batteries we need a different name for each
  42. * of them...
  43. */
  44. static DEFINE_IDR(battery_id);
  45. static DEFINE_MUTEX(battery_mutex);
  46. struct bq27x00_device_info;
  47. struct bq27x00_access_methods {
  48. int (*read)(u8 reg, int *rt_value, int b_single,
  49. struct bq27x00_device_info *di);
  50. };
  51. enum bq27x00_chip { BQ27000, BQ27500 };
  52. struct bq27x00_device_info {
  53. struct device *dev;
  54. int id;
  55. struct bq27x00_access_methods *bus;
  56. struct power_supply bat;
  57. enum bq27x00_chip chip;
  58. struct i2c_client *client;
  59. };
  60. static enum power_supply_property bq27x00_battery_props[] = {
  61. POWER_SUPPLY_PROP_STATUS,
  62. POWER_SUPPLY_PROP_PRESENT,
  63. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  64. POWER_SUPPLY_PROP_CURRENT_NOW,
  65. POWER_SUPPLY_PROP_CAPACITY,
  66. POWER_SUPPLY_PROP_TEMP,
  67. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  68. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  69. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  70. };
  71. /*
  72. * Common code for BQ27x00 devices
  73. */
  74. static int bq27x00_read(u8 reg, int *rt_value, int b_single,
  75. struct bq27x00_device_info *di)
  76. {
  77. return di->bus->read(reg, rt_value, b_single, di);
  78. }
  79. /*
  80. * Return the battery temperature in tenths of degree Celsius
  81. * Or < 0 if something fails.
  82. */
  83. static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
  84. {
  85. int ret;
  86. int temp = 0;
  87. ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
  88. if (ret) {
  89. dev_err(di->dev, "error reading temperature\n");
  90. return ret;
  91. }
  92. if (di->chip == BQ27500)
  93. return temp - 2731;
  94. else
  95. return ((temp >> 2) - 273) * 10;
  96. }
  97. /*
  98. * Return the battery Voltage in milivolts
  99. * Or < 0 if something fails.
  100. */
  101. static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
  102. {
  103. int ret;
  104. int volt = 0;
  105. ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
  106. if (ret) {
  107. dev_err(di->dev, "error reading voltage\n");
  108. return ret;
  109. }
  110. return volt * 1000;
  111. }
  112. /*
  113. * Return the battery average current
  114. * Note that current can be negative signed as well
  115. * Or 0 if something fails.
  116. */
  117. static int bq27x00_battery_current(struct bq27x00_device_info *di)
  118. {
  119. int ret;
  120. int curr = 0;
  121. int flags = 0;
  122. ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
  123. if (ret) {
  124. dev_err(di->dev, "error reading current\n");
  125. return 0;
  126. }
  127. if (di->chip == BQ27500) {
  128. /* bq27500 returns signed value */
  129. curr = (int)(s16)curr;
  130. } else {
  131. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  132. if (ret < 0) {
  133. dev_err(di->dev, "error reading flags\n");
  134. return 0;
  135. }
  136. if (flags & BQ27000_FLAG_CHGS) {
  137. dev_dbg(di->dev, "negative current!\n");
  138. curr = -curr;
  139. }
  140. }
  141. return curr * 1000;
  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(BQ27500_REG_SOC, &rsoc, 0, di);
  153. else
  154. ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
  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(BQ27x00_REG_FLAGS, &flags, 0, di);
  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(reg, &tval, 0, di);
  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. switch (psp) {
  216. case POWER_SUPPLY_PROP_STATUS:
  217. ret = bq27x00_battery_status(di, val);
  218. break;
  219. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  220. case POWER_SUPPLY_PROP_PRESENT:
  221. val->intval = bq27x00_battery_voltage(di);
  222. if (psp == POWER_SUPPLY_PROP_PRESENT)
  223. val->intval = val->intval <= 0 ? 0 : 1;
  224. break;
  225. case POWER_SUPPLY_PROP_CURRENT_NOW:
  226. val->intval = bq27x00_battery_current(di);
  227. break;
  228. case POWER_SUPPLY_PROP_CAPACITY:
  229. val->intval = bq27x00_battery_rsoc(di);
  230. break;
  231. case POWER_SUPPLY_PROP_TEMP:
  232. val->intval = bq27x00_battery_temperature(di);
  233. break;
  234. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  235. ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  236. break;
  237. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  238. ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  239. break;
  240. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  241. ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. return ret;
  247. }
  248. static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
  249. {
  250. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  251. di->bat.properties = bq27x00_battery_props;
  252. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  253. di->bat.get_property = bq27x00_battery_get_property;
  254. di->bat.external_power_changed = NULL;
  255. }
  256. /*
  257. * i2c specific code
  258. */
  259. static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
  260. struct bq27x00_device_info *di)
  261. {
  262. struct i2c_client *client = di->client;
  263. struct i2c_msg msg[1];
  264. unsigned char data[2];
  265. int err;
  266. if (!client->adapter)
  267. return -ENODEV;
  268. msg->addr = client->addr;
  269. msg->flags = 0;
  270. msg->len = 1;
  271. msg->buf = data;
  272. data[0] = reg;
  273. err = i2c_transfer(client->adapter, msg, 1);
  274. if (err >= 0) {
  275. if (!b_single)
  276. msg->len = 2;
  277. else
  278. msg->len = 1;
  279. msg->flags = I2C_M_RD;
  280. err = i2c_transfer(client->adapter, msg, 1);
  281. if (err >= 0) {
  282. if (!b_single)
  283. *rt_value = get_unaligned_le16(data);
  284. else
  285. *rt_value = data[0];
  286. return 0;
  287. }
  288. }
  289. return err;
  290. }
  291. static int bq27x00_battery_probe(struct i2c_client *client,
  292. const struct i2c_device_id *id)
  293. {
  294. char *name;
  295. struct bq27x00_device_info *di;
  296. struct bq27x00_access_methods *bus;
  297. int num;
  298. int retval = 0;
  299. /* Get new ID for the new battery device */
  300. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  301. if (retval == 0)
  302. return -ENOMEM;
  303. mutex_lock(&battery_mutex);
  304. retval = idr_get_new(&battery_id, client, &num);
  305. mutex_unlock(&battery_mutex);
  306. if (retval < 0)
  307. return retval;
  308. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  309. if (!name) {
  310. dev_err(&client->dev, "failed to allocate device name\n");
  311. retval = -ENOMEM;
  312. goto batt_failed_1;
  313. }
  314. di = kzalloc(sizeof(*di), GFP_KERNEL);
  315. if (!di) {
  316. dev_err(&client->dev, "failed to allocate device info data\n");
  317. retval = -ENOMEM;
  318. goto batt_failed_2;
  319. }
  320. di->id = num;
  321. di->chip = id->driver_data;
  322. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  323. if (!bus) {
  324. dev_err(&client->dev, "failed to allocate access method "
  325. "data\n");
  326. retval = -ENOMEM;
  327. goto batt_failed_3;
  328. }
  329. i2c_set_clientdata(client, di);
  330. di->dev = &client->dev;
  331. di->bat.name = name;
  332. bus->read = &bq27x00_read_i2c;
  333. di->bus = bus;
  334. di->client = client;
  335. bq27x00_powersupply_init(di);
  336. retval = power_supply_register(&client->dev, &di->bat);
  337. if (retval) {
  338. dev_err(&client->dev, "failed to register battery\n");
  339. goto batt_failed_4;
  340. }
  341. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  342. return 0;
  343. batt_failed_4:
  344. kfree(bus);
  345. batt_failed_3:
  346. kfree(di);
  347. batt_failed_2:
  348. kfree(name);
  349. batt_failed_1:
  350. mutex_lock(&battery_mutex);
  351. idr_remove(&battery_id, num);
  352. mutex_unlock(&battery_mutex);
  353. return retval;
  354. }
  355. static int bq27x00_battery_remove(struct i2c_client *client)
  356. {
  357. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  358. power_supply_unregister(&di->bat);
  359. kfree(di->bat.name);
  360. mutex_lock(&battery_mutex);
  361. idr_remove(&battery_id, di->id);
  362. mutex_unlock(&battery_mutex);
  363. kfree(di);
  364. return 0;
  365. }
  366. /*
  367. * Module stuff
  368. */
  369. static const struct i2c_device_id bq27x00_id[] = {
  370. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  371. { "bq27500", BQ27500 },
  372. {},
  373. };
  374. static struct i2c_driver bq27x00_battery_driver = {
  375. .driver = {
  376. .name = "bq27x00-battery",
  377. },
  378. .probe = bq27x00_battery_probe,
  379. .remove = bq27x00_battery_remove,
  380. .id_table = bq27x00_id,
  381. };
  382. static int __init bq27x00_battery_init(void)
  383. {
  384. int ret;
  385. ret = i2c_add_driver(&bq27x00_battery_driver);
  386. if (ret)
  387. printk(KERN_ERR "Unable to register BQ27x00 driver\n");
  388. return ret;
  389. }
  390. module_init(bq27x00_battery_init);
  391. static void __exit bq27x00_battery_exit(void)
  392. {
  393. i2c_del_driver(&bq27x00_battery_driver);
  394. }
  395. module_exit(bq27x00_battery_exit);
  396. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  397. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  398. MODULE_LICENSE("GPL");