ds2760_battery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. static unsigned int pmod_enabled;
  59. module_param(pmod_enabled, bool, 0644);
  60. MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
  61. static unsigned int rated_capacity;
  62. module_param(rated_capacity, uint, 0644);
  63. MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
  64. /* Some batteries have their rated capacity stored a N * 10 mAh, while
  65. * others use an index into this table. */
  66. static int rated_capacities[] = {
  67. 0,
  68. 920, /* Samsung */
  69. 920, /* BYD */
  70. 920, /* Lishen */
  71. 920, /* NEC */
  72. 1440, /* Samsung */
  73. 1440, /* BYD */
  74. 1440, /* Lishen */
  75. 1440, /* NEC */
  76. 2880, /* Samsung */
  77. 2880, /* BYD */
  78. 2880, /* Lishen */
  79. 2880 /* NEC */
  80. };
  81. /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
  82. * temp is in Celsius */
  83. static int battery_interpolate(int array[], int temp)
  84. {
  85. int index, dt;
  86. if (temp <= 0)
  87. return array[0];
  88. if (temp >= 40)
  89. return array[4];
  90. index = temp / 10;
  91. dt = temp % 10;
  92. return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
  93. }
  94. static int ds2760_battery_read_status(struct ds2760_device_info *di)
  95. {
  96. int ret, i, start, count, scale[5];
  97. if (di->update_time && time_before(jiffies, di->update_time +
  98. msecs_to_jiffies(cache_time)))
  99. return 0;
  100. /* The first time we read the entire contents of SRAM/EEPROM,
  101. * but after that we just read the interesting bits that change. */
  102. if (di->update_time == 0) {
  103. start = 0;
  104. count = DS2760_DATA_SIZE;
  105. } else {
  106. start = DS2760_VOLTAGE_MSB;
  107. count = DS2760_TEMP_LSB - start + 1;
  108. }
  109. ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
  110. if (ret != count) {
  111. dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
  112. di->w1_dev);
  113. return 1;
  114. }
  115. di->update_time = jiffies;
  116. /* DS2760 reports voltage in units of 4.88mV, but the battery class
  117. * reports in units of uV, so convert by multiplying by 4880. */
  118. di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
  119. (di->raw[DS2760_VOLTAGE_LSB] >> 5);
  120. di->voltage_uV = di->voltage_raw * 4880;
  121. /* DS2760 reports current in signed units of 0.625mA, but the battery
  122. * class reports in units of µA, so convert by multiplying by 625. */
  123. di->current_raw =
  124. (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
  125. (di->raw[DS2760_CURRENT_LSB] >> 3);
  126. di->current_uA = di->current_raw * 625;
  127. /* DS2760 reports accumulated current in signed units of 0.25mAh. */
  128. di->accum_current_raw =
  129. (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
  130. di->raw[DS2760_CURRENT_ACCUM_LSB];
  131. di->accum_current_uAh = di->accum_current_raw * 250;
  132. /* DS2760 reports temperature in signed units of 0.125°C, but the
  133. * battery class reports in units of 1/10 °C, so we convert by
  134. * multiplying by .125 * 10 = 1.25. */
  135. di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
  136. (di->raw[DS2760_TEMP_LSB] >> 5);
  137. di->temp_C = di->temp_raw + (di->temp_raw / 4);
  138. /* At least some battery monitors (e.g. HP iPAQ) store the battery's
  139. * maximum rated capacity. */
  140. if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
  141. di->rated_capacity = rated_capacities[
  142. (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
  143. else
  144. di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
  145. di->rated_capacity *= 1000; /* convert to µAh */
  146. /* Calculate the full level at the present temperature. */
  147. di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
  148. di->raw[DS2760_ACTIVE_FULL + 1];
  149. scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
  150. di->raw[DS2760_ACTIVE_FULL + 1];
  151. for (i = 1; i < 5; i++)
  152. scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
  153. di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
  154. di->full_active_uAh *= 1000; /* convert to µAh */
  155. /* Calculate the empty level at the present temperature. */
  156. scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
  157. for (i = 3; i >= 0; i--)
  158. scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
  159. di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
  160. di->empty_uAh *= 1000; /* convert to µAh */
  161. if (di->full_active_uAh == di->empty_uAh)
  162. di->rem_capacity = 0;
  163. else
  164. /* From Maxim Application Note 131: remaining capacity =
  165. * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
  166. di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
  167. (di->full_active_uAh - di->empty_uAh);
  168. if (di->rem_capacity < 0)
  169. di->rem_capacity = 0;
  170. if (di->rem_capacity > 100)
  171. di->rem_capacity = 100;
  172. if (di->current_uA)
  173. di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
  174. 3600L) / di->current_uA;
  175. else
  176. di->life_sec = 0;
  177. return 0;
  178. }
  179. static void ds2760_battery_update_status(struct ds2760_device_info *di)
  180. {
  181. int old_charge_status = di->charge_status;
  182. ds2760_battery_read_status(di);
  183. if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
  184. di->full_counter = 0;
  185. if (power_supply_am_i_supplied(&di->bat)) {
  186. if (di->current_uA > 10000) {
  187. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  188. di->full_counter = 0;
  189. } else if (di->current_uA < -5000) {
  190. if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
  191. dev_notice(di->dev, "not enough power to "
  192. "charge\n");
  193. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  194. di->full_counter = 0;
  195. } else if (di->current_uA < 10000 &&
  196. di->charge_status != POWER_SUPPLY_STATUS_FULL) {
  197. /* Don't consider the battery to be full unless
  198. * we've seen the current < 10 mA at least two
  199. * consecutive times. */
  200. di->full_counter++;
  201. if (di->full_counter < 2) {
  202. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  203. } else {
  204. unsigned char acr[2];
  205. int acr_val;
  206. /* acr is in units of 0.25 mAh */
  207. acr_val = di->full_active_uAh * 4L / 1000;
  208. acr[0] = acr_val >> 8;
  209. acr[1] = acr_val & 0xff;
  210. if (w1_ds2760_write(di->w1_dev, acr,
  211. DS2760_CURRENT_ACCUM_MSB, 2) < 2)
  212. dev_warn(di->dev,
  213. "ACR reset failed\n");
  214. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  215. }
  216. }
  217. } else {
  218. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  219. di->full_counter = 0;
  220. }
  221. if (di->charge_status != old_charge_status)
  222. power_supply_changed(&di->bat);
  223. }
  224. static void ds2760_battery_write_status(struct ds2760_device_info *di,
  225. char status)
  226. {
  227. if (status == di->raw[DS2760_STATUS_REG])
  228. return;
  229. w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
  230. w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  231. w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  232. }
  233. static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
  234. unsigned char rated_capacity)
  235. {
  236. if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
  237. return;
  238. w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
  239. w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  240. w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  241. }
  242. static void ds2760_battery_work(struct work_struct *work)
  243. {
  244. struct ds2760_device_info *di = container_of(work,
  245. struct ds2760_device_info, monitor_work.work);
  246. const int interval = HZ * 60;
  247. dev_dbg(di->dev, "%s\n", __func__);
  248. ds2760_battery_update_status(di);
  249. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
  250. }
  251. #define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
  252. bat);
  253. static void ds2760_battery_external_power_changed(struct power_supply *psy)
  254. {
  255. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  256. dev_dbg(di->dev, "%s\n", __func__);
  257. cancel_delayed_work(&di->monitor_work);
  258. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  259. }
  260. static int ds2760_battery_get_property(struct power_supply *psy,
  261. enum power_supply_property psp,
  262. union power_supply_propval *val)
  263. {
  264. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  265. switch (psp) {
  266. case POWER_SUPPLY_PROP_STATUS:
  267. val->intval = di->charge_status;
  268. return 0;
  269. default:
  270. break;
  271. }
  272. ds2760_battery_read_status(di);
  273. switch (psp) {
  274. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  275. val->intval = di->voltage_uV;
  276. break;
  277. case POWER_SUPPLY_PROP_CURRENT_NOW:
  278. val->intval = di->current_uA;
  279. break;
  280. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  281. val->intval = di->rated_capacity;
  282. break;
  283. case POWER_SUPPLY_PROP_CHARGE_FULL:
  284. val->intval = di->full_active_uAh;
  285. break;
  286. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  287. val->intval = di->empty_uAh;
  288. break;
  289. case POWER_SUPPLY_PROP_CHARGE_NOW:
  290. val->intval = di->accum_current_uAh;
  291. break;
  292. case POWER_SUPPLY_PROP_TEMP:
  293. val->intval = di->temp_C;
  294. break;
  295. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  296. val->intval = di->life_sec;
  297. break;
  298. case POWER_SUPPLY_PROP_CAPACITY:
  299. val->intval = di->rem_capacity;
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. static enum power_supply_property ds2760_battery_props[] = {
  307. POWER_SUPPLY_PROP_STATUS,
  308. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  309. POWER_SUPPLY_PROP_CURRENT_NOW,
  310. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  311. POWER_SUPPLY_PROP_CHARGE_FULL,
  312. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  313. POWER_SUPPLY_PROP_CHARGE_NOW,
  314. POWER_SUPPLY_PROP_TEMP,
  315. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  316. POWER_SUPPLY_PROP_CAPACITY,
  317. };
  318. static int ds2760_battery_probe(struct platform_device *pdev)
  319. {
  320. char status;
  321. int retval = 0;
  322. struct ds2760_device_info *di;
  323. di = kzalloc(sizeof(*di), GFP_KERNEL);
  324. if (!di) {
  325. retval = -ENOMEM;
  326. goto di_alloc_failed;
  327. }
  328. platform_set_drvdata(pdev, di);
  329. di->dev = &pdev->dev;
  330. di->w1_dev = pdev->dev.parent;
  331. di->bat.name = dev_name(&pdev->dev);
  332. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  333. di->bat.properties = ds2760_battery_props;
  334. di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
  335. di->bat.get_property = ds2760_battery_get_property;
  336. di->bat.external_power_changed =
  337. ds2760_battery_external_power_changed;
  338. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  339. /* enable sleep mode feature */
  340. ds2760_battery_read_status(di);
  341. status = di->raw[DS2760_STATUS_REG];
  342. if (pmod_enabled)
  343. status |= DS2760_STATUS_PMOD;
  344. else
  345. status &= ~DS2760_STATUS_PMOD;
  346. ds2760_battery_write_status(di, status);
  347. /* set rated capacity from module param */
  348. if (rated_capacity)
  349. ds2760_battery_write_rated_capacity(di, rated_capacity);
  350. retval = power_supply_register(&pdev->dev, &di->bat);
  351. if (retval) {
  352. dev_err(di->dev, "failed to register battery\n");
  353. goto batt_failed;
  354. }
  355. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  356. di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
  357. if (!di->monitor_wqueue) {
  358. retval = -ESRCH;
  359. goto workqueue_failed;
  360. }
  361. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  362. goto success;
  363. workqueue_failed:
  364. power_supply_unregister(&di->bat);
  365. batt_failed:
  366. kfree(di);
  367. di_alloc_failed:
  368. success:
  369. return retval;
  370. }
  371. static int ds2760_battery_remove(struct platform_device *pdev)
  372. {
  373. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  374. cancel_rearming_delayed_workqueue(di->monitor_wqueue,
  375. &di->monitor_work);
  376. destroy_workqueue(di->monitor_wqueue);
  377. power_supply_unregister(&di->bat);
  378. return 0;
  379. }
  380. #ifdef CONFIG_PM
  381. static int ds2760_battery_suspend(struct platform_device *pdev,
  382. pm_message_t state)
  383. {
  384. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  385. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  386. return 0;
  387. }
  388. static int ds2760_battery_resume(struct platform_device *pdev)
  389. {
  390. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  391. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  392. power_supply_changed(&di->bat);
  393. cancel_delayed_work(&di->monitor_work);
  394. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  395. return 0;
  396. }
  397. #else
  398. #define ds2760_battery_suspend NULL
  399. #define ds2760_battery_resume NULL
  400. #endif /* CONFIG_PM */
  401. MODULE_ALIAS("platform:ds2760-battery");
  402. static struct platform_driver ds2760_battery_driver = {
  403. .driver = {
  404. .name = "ds2760-battery",
  405. },
  406. .probe = ds2760_battery_probe,
  407. .remove = ds2760_battery_remove,
  408. .suspend = ds2760_battery_suspend,
  409. .resume = ds2760_battery_resume,
  410. };
  411. static int __init ds2760_battery_init(void)
  412. {
  413. return platform_driver_register(&ds2760_battery_driver);
  414. }
  415. static void __exit ds2760_battery_exit(void)
  416. {
  417. platform_driver_unregister(&ds2760_battery_driver);
  418. }
  419. module_init(ds2760_battery_init);
  420. module_exit(ds2760_battery_exit);
  421. MODULE_LICENSE("GPL");
  422. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  423. "Matt Reimer <mreimer@vpop.net>, "
  424. "Anton Vorontsov <cbou@mail.ru>");
  425. MODULE_DESCRIPTION("ds2760 battery driver");