bq27x00_battery.c 11 KB

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