ds2782_battery.c 11 KB

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