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. /* 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. POWER_SUPPLY_PROP_TECHNOLOGY,
  72. };
  73. /*
  74. * Common code for BQ27x00 devices
  75. */
  76. static int bq27x00_read(u8 reg, int *rt_value, int b_single,
  77. struct bq27x00_device_info *di)
  78. {
  79. return di->bus->read(reg, rt_value, b_single, di);
  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(BQ27x00_REG_TEMP, &temp, 0, di);
  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 >> 2) - 273) * 10;
  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(BQ27x00_REG_VOLT, &volt, 0, di);
  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(BQ27x00_REG_AI, &curr, 0, di);
  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;
  132. } else {
  133. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  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. }
  143. return curr * 1000;
  144. }
  145. /*
  146. * Return the battery Relative State-of-Charge
  147. * Or < 0 if something fails.
  148. */
  149. static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
  150. {
  151. int ret;
  152. int rsoc = 0;
  153. if (di->chip == BQ27500)
  154. ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
  155. else
  156. ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
  157. if (ret) {
  158. dev_err(di->dev, "error reading relative State-of-Charge\n");
  159. return ret;
  160. }
  161. return rsoc;
  162. }
  163. static int bq27x00_battery_status(struct bq27x00_device_info *di,
  164. union power_supply_propval *val)
  165. {
  166. int flags = 0;
  167. int status;
  168. int ret;
  169. ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
  170. if (ret < 0) {
  171. dev_err(di->dev, "error reading flags\n");
  172. return ret;
  173. }
  174. if (di->chip == BQ27500) {
  175. if (flags & BQ27500_FLAG_FC)
  176. status = POWER_SUPPLY_STATUS_FULL;
  177. else if (flags & BQ27500_FLAG_DSC)
  178. status = POWER_SUPPLY_STATUS_DISCHARGING;
  179. else
  180. status = POWER_SUPPLY_STATUS_CHARGING;
  181. } else {
  182. if (flags & BQ27000_FLAG_CHGS)
  183. status = POWER_SUPPLY_STATUS_CHARGING;
  184. else
  185. status = POWER_SUPPLY_STATUS_DISCHARGING;
  186. }
  187. val->intval = status;
  188. return 0;
  189. }
  190. /*
  191. * Read a time register.
  192. * Return < 0 if something fails.
  193. */
  194. static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
  195. union power_supply_propval *val)
  196. {
  197. int tval = 0;
  198. int ret;
  199. ret = bq27x00_read(reg, &tval, 0, di);
  200. if (ret) {
  201. dev_err(di->dev, "error reading register %02x\n", reg);
  202. return ret;
  203. }
  204. if (tval == 65535)
  205. return -ENODATA;
  206. val->intval = tval * 60;
  207. return 0;
  208. }
  209. #define to_bq27x00_device_info(x) container_of((x), \
  210. struct bq27x00_device_info, bat);
  211. static int bq27x00_battery_get_property(struct power_supply *psy,
  212. enum power_supply_property psp,
  213. union power_supply_propval *val)
  214. {
  215. int ret = 0;
  216. struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
  217. switch (psp) {
  218. case POWER_SUPPLY_PROP_STATUS:
  219. ret = bq27x00_battery_status(di, val);
  220. break;
  221. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  222. case POWER_SUPPLY_PROP_PRESENT:
  223. val->intval = bq27x00_battery_voltage(di);
  224. if (psp == POWER_SUPPLY_PROP_PRESENT)
  225. val->intval = val->intval <= 0 ? 0 : 1;
  226. break;
  227. case POWER_SUPPLY_PROP_CURRENT_NOW:
  228. val->intval = bq27x00_battery_current(di);
  229. break;
  230. case POWER_SUPPLY_PROP_CAPACITY:
  231. val->intval = bq27x00_battery_rsoc(di);
  232. break;
  233. case POWER_SUPPLY_PROP_TEMP:
  234. val->intval = bq27x00_battery_temperature(di);
  235. break;
  236. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  237. ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
  238. break;
  239. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  240. ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
  241. break;
  242. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  243. ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
  244. break;
  245. case POWER_SUPPLY_PROP_TECHNOLOGY:
  246. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. return ret;
  252. }
  253. static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
  254. {
  255. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  256. di->bat.properties = bq27x00_battery_props;
  257. di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
  258. di->bat.get_property = bq27x00_battery_get_property;
  259. di->bat.external_power_changed = NULL;
  260. }
  261. /*
  262. * i2c specific code
  263. */
  264. static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
  265. struct bq27x00_device_info *di)
  266. {
  267. struct i2c_client *client = di->client;
  268. struct i2c_msg msg[1];
  269. unsigned char data[2];
  270. int err;
  271. if (!client->adapter)
  272. return -ENODEV;
  273. msg->addr = client->addr;
  274. msg->flags = 0;
  275. msg->len = 1;
  276. msg->buf = data;
  277. data[0] = reg;
  278. err = i2c_transfer(client->adapter, msg, 1);
  279. if (err >= 0) {
  280. if (!b_single)
  281. msg->len = 2;
  282. else
  283. msg->len = 1;
  284. msg->flags = I2C_M_RD;
  285. err = i2c_transfer(client->adapter, msg, 1);
  286. if (err >= 0) {
  287. if (!b_single)
  288. *rt_value = get_unaligned_le16(data);
  289. else
  290. *rt_value = data[0];
  291. return 0;
  292. }
  293. }
  294. return err;
  295. }
  296. static int bq27x00_battery_probe(struct i2c_client *client,
  297. const struct i2c_device_id *id)
  298. {
  299. char *name;
  300. struct bq27x00_device_info *di;
  301. struct bq27x00_access_methods *bus;
  302. int num;
  303. int retval = 0;
  304. /* Get new ID for the new battery device */
  305. retval = idr_pre_get(&battery_id, GFP_KERNEL);
  306. if (retval == 0)
  307. return -ENOMEM;
  308. mutex_lock(&battery_mutex);
  309. retval = idr_get_new(&battery_id, client, &num);
  310. mutex_unlock(&battery_mutex);
  311. if (retval < 0)
  312. return retval;
  313. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  314. if (!name) {
  315. dev_err(&client->dev, "failed to allocate device name\n");
  316. retval = -ENOMEM;
  317. goto batt_failed_1;
  318. }
  319. di = kzalloc(sizeof(*di), GFP_KERNEL);
  320. if (!di) {
  321. dev_err(&client->dev, "failed to allocate device info data\n");
  322. retval = -ENOMEM;
  323. goto batt_failed_2;
  324. }
  325. di->id = num;
  326. di->chip = id->driver_data;
  327. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  328. if (!bus) {
  329. dev_err(&client->dev, "failed to allocate access method "
  330. "data\n");
  331. retval = -ENOMEM;
  332. goto batt_failed_3;
  333. }
  334. i2c_set_clientdata(client, di);
  335. di->dev = &client->dev;
  336. di->bat.name = name;
  337. bus->read = &bq27x00_read_i2c;
  338. di->bus = bus;
  339. di->client = client;
  340. bq27x00_powersupply_init(di);
  341. retval = power_supply_register(&client->dev, &di->bat);
  342. if (retval) {
  343. dev_err(&client->dev, "failed to register battery\n");
  344. goto batt_failed_4;
  345. }
  346. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  347. return 0;
  348. batt_failed_4:
  349. kfree(bus);
  350. batt_failed_3:
  351. kfree(di);
  352. batt_failed_2:
  353. kfree(name);
  354. batt_failed_1:
  355. mutex_lock(&battery_mutex);
  356. idr_remove(&battery_id, num);
  357. mutex_unlock(&battery_mutex);
  358. return retval;
  359. }
  360. static int bq27x00_battery_remove(struct i2c_client *client)
  361. {
  362. struct bq27x00_device_info *di = i2c_get_clientdata(client);
  363. power_supply_unregister(&di->bat);
  364. kfree(di->bus);
  365. kfree(di->bat.name);
  366. mutex_lock(&battery_mutex);
  367. idr_remove(&battery_id, di->id);
  368. mutex_unlock(&battery_mutex);
  369. kfree(di);
  370. return 0;
  371. }
  372. /*
  373. * Module stuff
  374. */
  375. static const struct i2c_device_id bq27x00_id[] = {
  376. { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
  377. { "bq27500", BQ27500 },
  378. {},
  379. };
  380. static struct i2c_driver bq27x00_battery_driver = {
  381. .driver = {
  382. .name = "bq27x00-battery",
  383. },
  384. .probe = bq27x00_battery_probe,
  385. .remove = bq27x00_battery_remove,
  386. .id_table = bq27x00_id,
  387. };
  388. static int __init bq27x00_battery_init(void)
  389. {
  390. int ret;
  391. ret = i2c_add_driver(&bq27x00_battery_driver);
  392. if (ret)
  393. printk(KERN_ERR "Unable to register BQ27x00 driver\n");
  394. return ret;
  395. }
  396. module_init(bq27x00_battery_init);
  397. static void __exit bq27x00_battery_exit(void)
  398. {
  399. i2c_del_driver(&bq27x00_battery_driver);
  400. }
  401. module_exit(bq27x00_battery_exit);
  402. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  403. MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
  404. MODULE_LICENSE("GPL");