bq27x00_battery.c 8.5 KB

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