ds2760_battery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Driver for batteries with DS2760 chips inside.
  3. *
  4. * Copyright © 2007 Anton Vorontsov
  5. * 2004-2007 Matt Reimer
  6. * 2004 Szabolcs Gyurko
  7. *
  8. * Use consistent with the GNU GPL is permitted,
  9. * provided that this copyright notice is
  10. * preserved in its entirety in all copies and derived works.
  11. *
  12. * Author: Anton Vorontsov <cbou@mail.ru>
  13. * February 2007
  14. *
  15. * Matt Reimer <mreimer@vpop.net>
  16. * April 2004, 2005, 2007
  17. *
  18. * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
  19. * September 2004
  20. */
  21. #include <linux/module.h>
  22. #include <linux/param.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/pm.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/power_supply.h>
  28. #include "../w1/w1.h"
  29. #include "../w1/slaves/w1_ds2760.h"
  30. struct ds2760_device_info {
  31. struct device *dev;
  32. /* DS2760 data, valid after calling ds2760_battery_read_status() */
  33. unsigned long update_time; /* jiffies when data read */
  34. char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
  35. int voltage_raw; /* units of 4.88 mV */
  36. int voltage_uV; /* units of µV */
  37. int current_raw; /* units of 0.625 mA */
  38. int current_uA; /* units of µA */
  39. int accum_current_raw; /* units of 0.25 mAh */
  40. int accum_current_uAh; /* units of µAh */
  41. int temp_raw; /* units of 0.125 °C */
  42. int temp_C; /* units of 0.1 °C */
  43. int rated_capacity; /* units of µAh */
  44. int rem_capacity; /* percentage */
  45. int full_active_uAh; /* units of µAh */
  46. int empty_uAh; /* units of µAh */
  47. int life_sec; /* units of seconds */
  48. int charge_status; /* POWER_SUPPLY_STATUS_* */
  49. int full_counter;
  50. struct power_supply bat;
  51. struct device *w1_dev;
  52. struct workqueue_struct *monitor_wqueue;
  53. struct delayed_work monitor_work;
  54. };
  55. static unsigned int cache_time = 1000;
  56. module_param(cache_time, uint, 0644);
  57. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  58. /* Some batteries have their rated capacity stored a N * 10 mAh, while
  59. * others use an index into this table. */
  60. static int rated_capacities[] = {
  61. 0,
  62. 920, /* Samsung */
  63. 920, /* BYD */
  64. 920, /* Lishen */
  65. 920, /* NEC */
  66. 1440, /* Samsung */
  67. 1440, /* BYD */
  68. 1440, /* Lishen */
  69. 1440, /* NEC */
  70. 2880, /* Samsung */
  71. 2880, /* BYD */
  72. 2880, /* Lishen */
  73. 2880 /* NEC */
  74. };
  75. /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
  76. * temp is in Celsius */
  77. static int battery_interpolate(int array[], int temp)
  78. {
  79. int index, dt;
  80. if (temp <= 0)
  81. return array[0];
  82. if (temp >= 40)
  83. return array[4];
  84. index = temp / 10;
  85. dt = temp % 10;
  86. return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
  87. }
  88. static int ds2760_battery_read_status(struct ds2760_device_info *di)
  89. {
  90. int ret, i, start, count, scale[5];
  91. if (di->update_time && time_before(jiffies, di->update_time +
  92. msecs_to_jiffies(cache_time)))
  93. return 0;
  94. /* The first time we read the entire contents of SRAM/EEPROM,
  95. * but after that we just read the interesting bits that change. */
  96. if (di->update_time == 0) {
  97. start = 0;
  98. count = DS2760_DATA_SIZE;
  99. } else {
  100. start = DS2760_VOLTAGE_MSB;
  101. count = DS2760_TEMP_LSB - start + 1;
  102. }
  103. ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
  104. if (ret != count) {
  105. dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
  106. di->w1_dev);
  107. return 1;
  108. }
  109. di->update_time = jiffies;
  110. /* DS2760 reports voltage in units of 4.88mV, but the battery class
  111. * reports in units of uV, so convert by multiplying by 4880. */
  112. di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
  113. (di->raw[DS2760_VOLTAGE_LSB] >> 5);
  114. di->voltage_uV = di->voltage_raw * 4880;
  115. /* DS2760 reports current in signed units of 0.625mA, but the battery
  116. * class reports in units of µA, so convert by multiplying by 625. */
  117. di->current_raw =
  118. (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
  119. (di->raw[DS2760_CURRENT_LSB] >> 3);
  120. di->current_uA = di->current_raw * 625;
  121. /* DS2760 reports accumulated current in signed units of 0.25mAh. */
  122. di->accum_current_raw =
  123. (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
  124. di->raw[DS2760_CURRENT_ACCUM_LSB];
  125. di->accum_current_uAh = di->accum_current_raw * 250;
  126. /* DS2760 reports temperature in signed units of 0.125°C, but the
  127. * battery class reports in units of 1/10 °C, so we convert by
  128. * multiplying by .125 * 10 = 1.25. */
  129. di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
  130. (di->raw[DS2760_TEMP_LSB] >> 5);
  131. di->temp_C = di->temp_raw + (di->temp_raw / 4);
  132. /* At least some battery monitors (e.g. HP iPAQ) store the battery's
  133. * maximum rated capacity. */
  134. if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
  135. di->rated_capacity = rated_capacities[
  136. (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
  137. else
  138. di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
  139. di->rated_capacity *= 1000; /* convert to µAh */
  140. /* Calculate the full level at the present temperature. */
  141. di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
  142. di->raw[DS2760_ACTIVE_FULL + 1];
  143. scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
  144. di->raw[DS2760_ACTIVE_FULL + 1];
  145. for (i = 1; i < 5; i++)
  146. scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
  147. di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
  148. di->full_active_uAh *= 1000; /* convert to µAh */
  149. /* Calculate the empty level at the present temperature. */
  150. scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
  151. for (i = 3; i >= 0; i--)
  152. scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
  153. di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
  154. di->empty_uAh *= 1000; /* convert to µAh */
  155. /* From Maxim Application Note 131: remaining capacity =
  156. * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
  157. di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
  158. (di->full_active_uAh - di->empty_uAh);
  159. if (di->rem_capacity < 0)
  160. di->rem_capacity = 0;
  161. if (di->rem_capacity > 100)
  162. di->rem_capacity = 100;
  163. if (di->current_uA)
  164. di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
  165. 3600L) / di->current_uA;
  166. else
  167. di->life_sec = 0;
  168. return 0;
  169. }
  170. static void ds2760_battery_update_status(struct ds2760_device_info *di)
  171. {
  172. int old_charge_status = di->charge_status;
  173. ds2760_battery_read_status(di);
  174. if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
  175. di->full_counter = 0;
  176. if (power_supply_am_i_supplied(&di->bat)) {
  177. if (di->current_uA > 10000) {
  178. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  179. di->full_counter = 0;
  180. } else if (di->current_uA < -5000) {
  181. if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
  182. dev_notice(di->dev, "not enough power to "
  183. "charge\n");
  184. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  185. di->full_counter = 0;
  186. } else if (di->current_uA < 10000 &&
  187. di->charge_status != POWER_SUPPLY_STATUS_FULL) {
  188. /* Don't consider the battery to be full unless
  189. * we've seen the current < 10 mA at least two
  190. * consecutive times. */
  191. di->full_counter++;
  192. if (di->full_counter < 2) {
  193. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  194. } else {
  195. unsigned char acr[2];
  196. int acr_val;
  197. /* acr is in units of 0.25 mAh */
  198. acr_val = di->full_active_uAh * 4L / 1000;
  199. acr[0] = acr_val >> 8;
  200. acr[1] = acr_val & 0xff;
  201. if (w1_ds2760_write(di->w1_dev, acr,
  202. DS2760_CURRENT_ACCUM_MSB, 2) < 2)
  203. dev_warn(di->dev,
  204. "ACR reset failed\n");
  205. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  206. }
  207. }
  208. } else {
  209. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  210. di->full_counter = 0;
  211. }
  212. if (di->charge_status != old_charge_status)
  213. power_supply_changed(&di->bat);
  214. }
  215. static void ds2760_battery_work(struct work_struct *work)
  216. {
  217. struct ds2760_device_info *di = container_of(work,
  218. struct ds2760_device_info, monitor_work.work);
  219. const int interval = HZ * 60;
  220. dev_dbg(di->dev, "%s\n", __func__);
  221. ds2760_battery_update_status(di);
  222. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
  223. }
  224. #define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
  225. bat);
  226. static void ds2760_battery_external_power_changed(struct power_supply *psy)
  227. {
  228. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  229. dev_dbg(di->dev, "%s\n", __func__);
  230. cancel_delayed_work(&di->monitor_work);
  231. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  232. }
  233. static int ds2760_battery_get_property(struct power_supply *psy,
  234. enum power_supply_property psp,
  235. union power_supply_propval *val)
  236. {
  237. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  238. switch (psp) {
  239. case POWER_SUPPLY_PROP_STATUS:
  240. val->intval = di->charge_status;
  241. return 0;
  242. default:
  243. break;
  244. }
  245. ds2760_battery_read_status(di);
  246. switch (psp) {
  247. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  248. val->intval = di->voltage_uV;
  249. break;
  250. case POWER_SUPPLY_PROP_CURRENT_NOW:
  251. val->intval = di->current_uA;
  252. break;
  253. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  254. val->intval = di->rated_capacity;
  255. break;
  256. case POWER_SUPPLY_PROP_CHARGE_FULL:
  257. val->intval = di->full_active_uAh;
  258. break;
  259. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  260. val->intval = di->empty_uAh;
  261. break;
  262. case POWER_SUPPLY_PROP_CHARGE_NOW:
  263. val->intval = di->accum_current_uAh;
  264. break;
  265. case POWER_SUPPLY_PROP_TEMP:
  266. val->intval = di->temp_C;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. static enum power_supply_property ds2760_battery_props[] = {
  274. POWER_SUPPLY_PROP_STATUS,
  275. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  276. POWER_SUPPLY_PROP_CURRENT_NOW,
  277. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  278. POWER_SUPPLY_PROP_CHARGE_FULL,
  279. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  280. POWER_SUPPLY_PROP_CHARGE_NOW,
  281. POWER_SUPPLY_PROP_TEMP,
  282. };
  283. static int ds2760_battery_probe(struct platform_device *pdev)
  284. {
  285. int retval = 0;
  286. struct ds2760_device_info *di;
  287. struct ds2760_platform_data *pdata;
  288. di = kzalloc(sizeof(*di), GFP_KERNEL);
  289. if (!di) {
  290. retval = -ENOMEM;
  291. goto di_alloc_failed;
  292. }
  293. platform_set_drvdata(pdev, di);
  294. pdata = pdev->dev.platform_data;
  295. di->dev = &pdev->dev;
  296. di->w1_dev = pdev->dev.parent;
  297. di->bat.name = pdev->dev.bus_id;
  298. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  299. di->bat.properties = ds2760_battery_props;
  300. di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
  301. di->bat.get_property = ds2760_battery_get_property;
  302. di->bat.external_power_changed =
  303. ds2760_battery_external_power_changed;
  304. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  305. retval = power_supply_register(&pdev->dev, &di->bat);
  306. if (retval) {
  307. dev_err(di->dev, "failed to register battery\n");
  308. goto batt_failed;
  309. }
  310. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  311. di->monitor_wqueue = create_singlethread_workqueue(pdev->dev.bus_id);
  312. if (!di->monitor_wqueue) {
  313. retval = -ESRCH;
  314. goto workqueue_failed;
  315. }
  316. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  317. goto success;
  318. workqueue_failed:
  319. power_supply_unregister(&di->bat);
  320. batt_failed:
  321. kfree(di);
  322. di_alloc_failed:
  323. success:
  324. return retval;
  325. }
  326. static int ds2760_battery_remove(struct platform_device *pdev)
  327. {
  328. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  329. cancel_rearming_delayed_workqueue(di->monitor_wqueue,
  330. &di->monitor_work);
  331. destroy_workqueue(di->monitor_wqueue);
  332. power_supply_unregister(&di->bat);
  333. return 0;
  334. }
  335. #ifdef CONFIG_PM
  336. static int ds2760_battery_suspend(struct platform_device *pdev,
  337. pm_message_t state)
  338. {
  339. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  340. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  341. return 0;
  342. }
  343. static int ds2760_battery_resume(struct platform_device *pdev)
  344. {
  345. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  346. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  347. power_supply_changed(&di->bat);
  348. cancel_delayed_work(&di->monitor_work);
  349. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  350. return 0;
  351. }
  352. #else
  353. #define ds2760_battery_suspend NULL
  354. #define ds2760_battery_resume NULL
  355. #endif /* CONFIG_PM */
  356. MODULE_ALIAS("platform:ds2760-battery");
  357. static struct platform_driver ds2760_battery_driver = {
  358. .driver = {
  359. .name = "ds2760-battery",
  360. },
  361. .probe = ds2760_battery_probe,
  362. .remove = ds2760_battery_remove,
  363. .suspend = ds2760_battery_suspend,
  364. .resume = ds2760_battery_resume,
  365. };
  366. static int __init ds2760_battery_init(void)
  367. {
  368. return platform_driver_register(&ds2760_battery_driver);
  369. }
  370. static void __exit ds2760_battery_exit(void)
  371. {
  372. platform_driver_unregister(&ds2760_battery_driver);
  373. }
  374. module_init(ds2760_battery_init);
  375. module_exit(ds2760_battery_exit);
  376. MODULE_LICENSE("GPL");
  377. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  378. "Matt Reimer <mreimer@vpop.net>, "
  379. "Anton Vorontsov <cbou@mail.ru>");
  380. MODULE_DESCRIPTION("ds2760 battery driver");