ds2760_battery.c 18 KB

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