ds2760_battery.c 18 KB

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