ds2782_battery.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
  3. *
  4. * Copyright (C) 2009 Bluewater Systems Ltd
  5. *
  6. * Author: Ryan Mallon
  7. *
  8. * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/swab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/idr.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/slab.h>
  24. #include <linux/ds2782_battery.h>
  25. #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
  26. #define DS278x_REG_VOLT_MSB 0x0c
  27. #define DS278x_REG_TEMP_MSB 0x0a
  28. #define DS278x_REG_CURRENT_MSB 0x0e
  29. /* EEPROM Block */
  30. #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
  31. /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
  32. #define DS2782_CURRENT_UNITS 1563
  33. #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
  34. #define DS2786_CURRENT_UNITS 25
  35. struct ds278x_info;
  36. struct ds278x_battery_ops {
  37. int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
  38. int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
  39. int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
  40. };
  41. #define to_ds278x_info(x) container_of(x, struct ds278x_info, battery)
  42. struct ds278x_info {
  43. struct i2c_client *client;
  44. struct power_supply battery;
  45. struct ds278x_battery_ops *ops;
  46. int id;
  47. int rsns;
  48. };
  49. static DEFINE_IDR(battery_id);
  50. static DEFINE_MUTEX(battery_lock);
  51. static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
  52. {
  53. int ret;
  54. ret = i2c_smbus_read_byte_data(info->client, reg);
  55. if (ret < 0) {
  56. dev_err(&info->client->dev, "register read failed\n");
  57. return ret;
  58. }
  59. *val = ret;
  60. return 0;
  61. }
  62. static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
  63. s16 *val)
  64. {
  65. int ret;
  66. ret = swab16(i2c_smbus_read_word_data(info->client, reg_msb));
  67. if (ret < 0) {
  68. dev_err(&info->client->dev, "register read failed\n");
  69. return ret;
  70. }
  71. *val = ret;
  72. return 0;
  73. }
  74. static int ds278x_get_temp(struct ds278x_info *info, int *temp)
  75. {
  76. s16 raw;
  77. int err;
  78. /*
  79. * Temperature is measured in units of 0.125 degrees celcius, the
  80. * power_supply class measures temperature in tenths of degrees
  81. * celsius. The temperature value is stored as a 10 bit number, plus
  82. * sign in the upper bits of a 16 bit register.
  83. */
  84. err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
  85. if (err)
  86. return err;
  87. *temp = ((raw / 32) * 125) / 100;
  88. return 0;
  89. }
  90. static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
  91. {
  92. int sense_res;
  93. int err;
  94. u8 sense_res_raw;
  95. s16 raw;
  96. /*
  97. * The units of measurement for current are dependent on the value of
  98. * the sense resistor.
  99. */
  100. err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
  101. if (err)
  102. return err;
  103. if (sense_res_raw == 0) {
  104. dev_err(&info->client->dev, "sense resistor value is 0\n");
  105. return -ENXIO;
  106. }
  107. sense_res = 1000 / sense_res_raw;
  108. dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
  109. sense_res);
  110. err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
  111. if (err)
  112. return err;
  113. *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
  114. return 0;
  115. }
  116. static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
  117. {
  118. s16 raw;
  119. int err;
  120. /*
  121. * Voltage is measured in units of 4.88mV. The voltage is stored as
  122. * a 10-bit number plus sign, in the upper bits of a 16-bit register
  123. */
  124. err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
  125. if (err)
  126. return err;
  127. *voltage_uV = (raw / 32) * 4800;
  128. return 0;
  129. }
  130. static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
  131. {
  132. int err;
  133. u8 raw;
  134. err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
  135. if (err)
  136. return err;
  137. *capacity = raw;
  138. return 0;
  139. }
  140. static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
  141. {
  142. int err;
  143. s16 raw;
  144. err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
  145. if (err)
  146. return err;
  147. *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
  148. return 0;
  149. }
  150. static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
  151. {
  152. s16 raw;
  153. int err;
  154. /*
  155. * Voltage is measured in units of 1.22mV. The voltage is stored as
  156. * a 10-bit number plus sign, in the upper bits of a 16-bit register
  157. */
  158. err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
  159. if (err)
  160. return err;
  161. *voltage_uV = (raw / 8) * 1220;
  162. return 0;
  163. }
  164. static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
  165. {
  166. int err;
  167. u8 raw;
  168. err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
  169. if (err)
  170. return err;
  171. /* Relative capacity is displayed with resolution 0.5 % */
  172. *capacity = raw/2 ;
  173. return 0;
  174. }
  175. static int ds278x_get_status(struct ds278x_info *info, int *status)
  176. {
  177. int err;
  178. int current_uA;
  179. int capacity;
  180. err = info->ops->get_battery_current(info, &current_uA);
  181. if (err)
  182. return err;
  183. err = info->ops->get_battery_capacity(info, &capacity);
  184. if (err)
  185. return err;
  186. if (capacity == 100)
  187. *status = POWER_SUPPLY_STATUS_FULL;
  188. else if (current_uA == 0)
  189. *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  190. else if (current_uA < 0)
  191. *status = POWER_SUPPLY_STATUS_DISCHARGING;
  192. else
  193. *status = POWER_SUPPLY_STATUS_CHARGING;
  194. return 0;
  195. }
  196. static int ds278x_battery_get_property(struct power_supply *psy,
  197. enum power_supply_property prop,
  198. union power_supply_propval *val)
  199. {
  200. struct ds278x_info *info = to_ds278x_info(psy);
  201. int ret;
  202. switch (prop) {
  203. case POWER_SUPPLY_PROP_STATUS:
  204. ret = ds278x_get_status(info, &val->intval);
  205. break;
  206. case POWER_SUPPLY_PROP_CAPACITY:
  207. ret = info->ops->get_battery_capacity(info, &val->intval);
  208. break;
  209. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  210. ret = info->ops->get_battery_voltage(info, &val->intval);
  211. break;
  212. case POWER_SUPPLY_PROP_CURRENT_NOW:
  213. ret = info->ops->get_battery_current(info, &val->intval);
  214. break;
  215. case POWER_SUPPLY_PROP_TEMP:
  216. ret = ds278x_get_temp(info, &val->intval);
  217. break;
  218. default:
  219. ret = -EINVAL;
  220. }
  221. return ret;
  222. }
  223. static enum power_supply_property ds278x_battery_props[] = {
  224. POWER_SUPPLY_PROP_STATUS,
  225. POWER_SUPPLY_PROP_CAPACITY,
  226. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  227. POWER_SUPPLY_PROP_CURRENT_NOW,
  228. POWER_SUPPLY_PROP_TEMP,
  229. };
  230. static void ds278x_power_supply_init(struct power_supply *battery)
  231. {
  232. battery->type = POWER_SUPPLY_TYPE_BATTERY;
  233. battery->properties = ds278x_battery_props;
  234. battery->num_properties = ARRAY_SIZE(ds278x_battery_props);
  235. battery->get_property = ds278x_battery_get_property;
  236. battery->external_power_changed = NULL;
  237. }
  238. static int ds278x_battery_remove(struct i2c_client *client)
  239. {
  240. struct ds278x_info *info = i2c_get_clientdata(client);
  241. power_supply_unregister(&info->battery);
  242. kfree(info->battery.name);
  243. mutex_lock(&battery_lock);
  244. idr_remove(&battery_id, info->id);
  245. mutex_unlock(&battery_lock);
  246. kfree(info);
  247. return 0;
  248. }
  249. enum ds278x_num_id {
  250. DS2782 = 0,
  251. DS2786,
  252. };
  253. static struct ds278x_battery_ops ds278x_ops[] = {
  254. [DS2782] = {
  255. .get_battery_current = ds2782_get_current,
  256. .get_battery_voltage = ds2782_get_voltage,
  257. .get_battery_capacity = ds2782_get_capacity,
  258. },
  259. [DS2786] = {
  260. .get_battery_current = ds2786_get_current,
  261. .get_battery_voltage = ds2786_get_voltage,
  262. .get_battery_capacity = ds2786_get_capacity,
  263. }
  264. };
  265. static int ds278x_battery_probe(struct i2c_client *client,
  266. const struct i2c_device_id *id)
  267. {
  268. struct ds278x_platform_data *pdata = client->dev.platform_data;
  269. struct ds278x_info *info;
  270. int ret;
  271. int num;
  272. /*
  273. * ds2786 should have the sense resistor value set
  274. * in the platform data
  275. */
  276. if (id->driver_data == DS2786 && !pdata) {
  277. dev_err(&client->dev, "missing platform data for ds2786\n");
  278. return -EINVAL;
  279. }
  280. /* Get an ID for this battery */
  281. ret = idr_pre_get(&battery_id, GFP_KERNEL);
  282. if (ret == 0) {
  283. ret = -ENOMEM;
  284. goto fail_id;
  285. }
  286. mutex_lock(&battery_lock);
  287. ret = idr_get_new(&battery_id, client, &num);
  288. mutex_unlock(&battery_lock);
  289. if (ret < 0)
  290. goto fail_id;
  291. info = kzalloc(sizeof(*info), GFP_KERNEL);
  292. if (!info) {
  293. ret = -ENOMEM;
  294. goto fail_info;
  295. }
  296. info->battery.name = kasprintf(GFP_KERNEL, "%s-%d", client->name, num);
  297. if (!info->battery.name) {
  298. ret = -ENOMEM;
  299. goto fail_name;
  300. }
  301. if (id->driver_data == DS2786)
  302. info->rsns = pdata->rsns;
  303. i2c_set_clientdata(client, info);
  304. info->client = client;
  305. info->id = num;
  306. info->ops = &ds278x_ops[id->driver_data];
  307. ds278x_power_supply_init(&info->battery);
  308. ret = power_supply_register(&client->dev, &info->battery);
  309. if (ret) {
  310. dev_err(&client->dev, "failed to register battery\n");
  311. goto fail_register;
  312. }
  313. return 0;
  314. fail_register:
  315. kfree(info->battery.name);
  316. fail_name:
  317. kfree(info);
  318. fail_info:
  319. mutex_lock(&battery_lock);
  320. idr_remove(&battery_id, num);
  321. mutex_unlock(&battery_lock);
  322. fail_id:
  323. return ret;
  324. }
  325. static const struct i2c_device_id ds278x_id[] = {
  326. {"ds2782", DS2782},
  327. {"ds2786", DS2786},
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(i2c, ds278x_id);
  331. static struct i2c_driver ds278x_battery_driver = {
  332. .driver = {
  333. .name = "ds2782-battery",
  334. },
  335. .probe = ds278x_battery_probe,
  336. .remove = ds278x_battery_remove,
  337. .id_table = ds278x_id,
  338. };
  339. module_i2c_driver(ds278x_battery_driver);
  340. MODULE_AUTHOR("Ryan Mallon");
  341. MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
  342. MODULE_LICENSE("GPL");