ds2760_battery.c 14 KB

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