ds2760_battery.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. if (di->full_active_uAh == di->empty_uAh)
  156. di->rem_capacity = 0;
  157. else
  158. /* From Maxim Application Note 131: remaining capacity =
  159. * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
  160. di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
  161. (di->full_active_uAh - di->empty_uAh);
  162. if (di->rem_capacity < 0)
  163. di->rem_capacity = 0;
  164. if (di->rem_capacity > 100)
  165. di->rem_capacity = 100;
  166. if (di->current_uA)
  167. di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
  168. 3600L) / di->current_uA;
  169. else
  170. di->life_sec = 0;
  171. return 0;
  172. }
  173. static void ds2760_battery_update_status(struct ds2760_device_info *di)
  174. {
  175. int old_charge_status = di->charge_status;
  176. ds2760_battery_read_status(di);
  177. if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
  178. di->full_counter = 0;
  179. if (power_supply_am_i_supplied(&di->bat)) {
  180. if (di->current_uA > 10000) {
  181. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  182. di->full_counter = 0;
  183. } else if (di->current_uA < -5000) {
  184. if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
  185. dev_notice(di->dev, "not enough power to "
  186. "charge\n");
  187. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  188. di->full_counter = 0;
  189. } else if (di->current_uA < 10000 &&
  190. di->charge_status != POWER_SUPPLY_STATUS_FULL) {
  191. /* Don't consider the battery to be full unless
  192. * we've seen the current < 10 mA at least two
  193. * consecutive times. */
  194. di->full_counter++;
  195. if (di->full_counter < 2) {
  196. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  197. } else {
  198. unsigned char acr[2];
  199. int acr_val;
  200. /* acr is in units of 0.25 mAh */
  201. acr_val = di->full_active_uAh * 4L / 1000;
  202. acr[0] = acr_val >> 8;
  203. acr[1] = acr_val & 0xff;
  204. if (w1_ds2760_write(di->w1_dev, acr,
  205. DS2760_CURRENT_ACCUM_MSB, 2) < 2)
  206. dev_warn(di->dev,
  207. "ACR reset failed\n");
  208. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  209. }
  210. }
  211. } else {
  212. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  213. di->full_counter = 0;
  214. }
  215. if (di->charge_status != old_charge_status)
  216. power_supply_changed(&di->bat);
  217. }
  218. static void ds2760_battery_work(struct work_struct *work)
  219. {
  220. struct ds2760_device_info *di = container_of(work,
  221. struct ds2760_device_info, monitor_work.work);
  222. const int interval = HZ * 60;
  223. dev_dbg(di->dev, "%s\n", __func__);
  224. ds2760_battery_update_status(di);
  225. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
  226. }
  227. #define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
  228. bat);
  229. static void ds2760_battery_external_power_changed(struct power_supply *psy)
  230. {
  231. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  232. dev_dbg(di->dev, "%s\n", __func__);
  233. cancel_delayed_work(&di->monitor_work);
  234. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  235. }
  236. static int ds2760_battery_get_property(struct power_supply *psy,
  237. enum power_supply_property psp,
  238. union power_supply_propval *val)
  239. {
  240. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  241. switch (psp) {
  242. case POWER_SUPPLY_PROP_STATUS:
  243. val->intval = di->charge_status;
  244. return 0;
  245. default:
  246. break;
  247. }
  248. ds2760_battery_read_status(di);
  249. switch (psp) {
  250. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  251. val->intval = di->voltage_uV;
  252. break;
  253. case POWER_SUPPLY_PROP_CURRENT_NOW:
  254. val->intval = di->current_uA;
  255. break;
  256. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  257. val->intval = di->rated_capacity;
  258. break;
  259. case POWER_SUPPLY_PROP_CHARGE_FULL:
  260. val->intval = di->full_active_uAh;
  261. break;
  262. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  263. val->intval = di->empty_uAh;
  264. break;
  265. case POWER_SUPPLY_PROP_CHARGE_NOW:
  266. val->intval = di->accum_current_uAh;
  267. break;
  268. case POWER_SUPPLY_PROP_TEMP:
  269. val->intval = di->temp_C;
  270. break;
  271. default:
  272. return -EINVAL;
  273. }
  274. return 0;
  275. }
  276. static enum power_supply_property ds2760_battery_props[] = {
  277. POWER_SUPPLY_PROP_STATUS,
  278. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  279. POWER_SUPPLY_PROP_CURRENT_NOW,
  280. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  281. POWER_SUPPLY_PROP_CHARGE_FULL,
  282. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  283. POWER_SUPPLY_PROP_CHARGE_NOW,
  284. POWER_SUPPLY_PROP_TEMP,
  285. };
  286. static int ds2760_battery_probe(struct platform_device *pdev)
  287. {
  288. int retval = 0;
  289. struct ds2760_device_info *di;
  290. struct ds2760_platform_data *pdata;
  291. di = kzalloc(sizeof(*di), GFP_KERNEL);
  292. if (!di) {
  293. retval = -ENOMEM;
  294. goto di_alloc_failed;
  295. }
  296. platform_set_drvdata(pdev, di);
  297. pdata = pdev->dev.platform_data;
  298. di->dev = &pdev->dev;
  299. di->w1_dev = pdev->dev.parent;
  300. di->bat.name = dev_name(&pdev->dev);
  301. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  302. di->bat.properties = ds2760_battery_props;
  303. di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
  304. di->bat.get_property = ds2760_battery_get_property;
  305. di->bat.external_power_changed =
  306. ds2760_battery_external_power_changed;
  307. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  308. retval = power_supply_register(&pdev->dev, &di->bat);
  309. if (retval) {
  310. dev_err(di->dev, "failed to register battery\n");
  311. goto batt_failed;
  312. }
  313. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  314. di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
  315. if (!di->monitor_wqueue) {
  316. retval = -ESRCH;
  317. goto workqueue_failed;
  318. }
  319. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  320. goto success;
  321. workqueue_failed:
  322. power_supply_unregister(&di->bat);
  323. batt_failed:
  324. kfree(di);
  325. di_alloc_failed:
  326. success:
  327. return retval;
  328. }
  329. static int ds2760_battery_remove(struct platform_device *pdev)
  330. {
  331. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  332. cancel_rearming_delayed_workqueue(di->monitor_wqueue,
  333. &di->monitor_work);
  334. destroy_workqueue(di->monitor_wqueue);
  335. power_supply_unregister(&di->bat);
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int ds2760_battery_suspend(struct platform_device *pdev,
  340. pm_message_t state)
  341. {
  342. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  343. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  344. return 0;
  345. }
  346. static int ds2760_battery_resume(struct platform_device *pdev)
  347. {
  348. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  349. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  350. power_supply_changed(&di->bat);
  351. cancel_delayed_work(&di->monitor_work);
  352. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  353. return 0;
  354. }
  355. #else
  356. #define ds2760_battery_suspend NULL
  357. #define ds2760_battery_resume NULL
  358. #endif /* CONFIG_PM */
  359. MODULE_ALIAS("platform:ds2760-battery");
  360. static struct platform_driver ds2760_battery_driver = {
  361. .driver = {
  362. .name = "ds2760-battery",
  363. },
  364. .probe = ds2760_battery_probe,
  365. .remove = ds2760_battery_remove,
  366. .suspend = ds2760_battery_suspend,
  367. .resume = ds2760_battery_resume,
  368. };
  369. static int __init ds2760_battery_init(void)
  370. {
  371. return platform_driver_register(&ds2760_battery_driver);
  372. }
  373. static void __exit ds2760_battery_exit(void)
  374. {
  375. platform_driver_unregister(&ds2760_battery_driver);
  376. }
  377. module_init(ds2760_battery_init);
  378. module_exit(ds2760_battery_exit);
  379. MODULE_LICENSE("GPL");
  380. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  381. "Matt Reimer <mreimer@vpop.net>, "
  382. "Anton Vorontsov <cbou@mail.ru>");
  383. MODULE_DESCRIPTION("ds2760 battery driver");