bq27x00_battery.c 8.1 KB

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