ds2760_battery.c 16 KB

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