bq27x00_battery.c 8.1 KB

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