bq27x00_battery.c 8.0 KB

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