ds2782_battery.c 11 KB

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