ds2760_battery.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  297. }
  298. static void ds2760_battery_set_charged_work(struct work_struct *work)
  299. {
  300. char bias;
  301. struct ds2760_device_info *di = container_of(work,
  302. struct ds2760_device_info, set_charged_work.work);
  303. dev_dbg(di->dev, "%s\n", __func__);
  304. ds2760_battery_read_status(di);
  305. /* When we get notified by external circuitry that the battery is
  306. * considered fully charged now, we know that there is no current
  307. * flow any more. However, the ds2760's internal current meter is
  308. * too inaccurate to rely on - spec say something ~15% failure.
  309. * Hence, we use the current offset bias register to compensate
  310. * that error.
  311. */
  312. if (!power_supply_am_i_supplied(&di->bat))
  313. return;
  314. bias = (signed char) di->current_raw +
  315. (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
  316. dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
  317. w1_ds2760_write(di->w1_dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
  318. w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  319. w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
  320. /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
  321. * value won't be read back by ds2760_battery_read_status() */
  322. di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
  323. }
  324. static void ds2760_battery_set_charged(struct power_supply *psy)
  325. {
  326. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  327. /* postpone the actual work by 20 secs. This is for debouncing GPIO
  328. * signals and to let the current value settle. See AN4188. */
  329. mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
  330. }
  331. static int ds2760_battery_get_property(struct power_supply *psy,
  332. enum power_supply_property psp,
  333. union power_supply_propval *val)
  334. {
  335. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  336. switch (psp) {
  337. case POWER_SUPPLY_PROP_STATUS:
  338. val->intval = di->charge_status;
  339. return 0;
  340. default:
  341. break;
  342. }
  343. ds2760_battery_read_status(di);
  344. switch (psp) {
  345. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  346. val->intval = di->voltage_uV;
  347. break;
  348. case POWER_SUPPLY_PROP_CURRENT_NOW:
  349. val->intval = di->current_uA;
  350. break;
  351. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  352. val->intval = di->rated_capacity;
  353. break;
  354. case POWER_SUPPLY_PROP_CHARGE_FULL:
  355. val->intval = di->full_active_uAh;
  356. break;
  357. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  358. val->intval = di->empty_uAh;
  359. break;
  360. case POWER_SUPPLY_PROP_CHARGE_NOW:
  361. val->intval = di->accum_current_uAh;
  362. break;
  363. case POWER_SUPPLY_PROP_TEMP:
  364. val->intval = di->temp_C;
  365. break;
  366. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  367. val->intval = di->life_sec;
  368. break;
  369. case POWER_SUPPLY_PROP_CAPACITY:
  370. val->intval = di->rem_capacity;
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. return 0;
  376. }
  377. static int ds2760_battery_set_property(struct power_supply *psy,
  378. enum power_supply_property psp,
  379. const union power_supply_propval *val)
  380. {
  381. struct ds2760_device_info *di = to_ds2760_device_info(psy);
  382. switch (psp) {
  383. case POWER_SUPPLY_PROP_CHARGE_FULL:
  384. /* the interface counts in uAh, convert the value */
  385. ds2760_battery_write_active_full(di, val->intval / 1000L);
  386. break;
  387. case POWER_SUPPLY_PROP_CHARGE_NOW:
  388. /* ds2760_battery_set_current_accum() does the conversion */
  389. ds2760_battery_set_current_accum(di, val->intval);
  390. break;
  391. default:
  392. return -EPERM;
  393. }
  394. return 0;
  395. }
  396. static int ds2760_battery_property_is_writeable(struct power_supply *psy,
  397. enum power_supply_property psp)
  398. {
  399. switch (psp) {
  400. case POWER_SUPPLY_PROP_CHARGE_FULL:
  401. case POWER_SUPPLY_PROP_CHARGE_NOW:
  402. return 1;
  403. default:
  404. break;
  405. }
  406. return 0;
  407. }
  408. static enum power_supply_property ds2760_battery_props[] = {
  409. POWER_SUPPLY_PROP_STATUS,
  410. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  411. POWER_SUPPLY_PROP_CURRENT_NOW,
  412. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  413. POWER_SUPPLY_PROP_CHARGE_FULL,
  414. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  415. POWER_SUPPLY_PROP_CHARGE_NOW,
  416. POWER_SUPPLY_PROP_TEMP,
  417. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  418. POWER_SUPPLY_PROP_CAPACITY,
  419. };
  420. static int ds2760_battery_probe(struct platform_device *pdev)
  421. {
  422. char status;
  423. int retval = 0;
  424. struct ds2760_device_info *di;
  425. di = kzalloc(sizeof(*di), GFP_KERNEL);
  426. if (!di) {
  427. retval = -ENOMEM;
  428. goto di_alloc_failed;
  429. }
  430. platform_set_drvdata(pdev, di);
  431. di->dev = &pdev->dev;
  432. di->w1_dev = pdev->dev.parent;
  433. di->bat.name = dev_name(&pdev->dev);
  434. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  435. di->bat.properties = ds2760_battery_props;
  436. di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
  437. di->bat.get_property = ds2760_battery_get_property;
  438. di->bat.set_property = ds2760_battery_set_property;
  439. di->bat.property_is_writeable =
  440. ds2760_battery_property_is_writeable;
  441. di->bat.set_charged = ds2760_battery_set_charged;
  442. di->bat.external_power_changed =
  443. ds2760_battery_external_power_changed;
  444. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  445. /* enable sleep mode feature */
  446. ds2760_battery_read_status(di);
  447. status = di->raw[DS2760_STATUS_REG];
  448. if (pmod_enabled)
  449. status |= DS2760_STATUS_PMOD;
  450. else
  451. status &= ~DS2760_STATUS_PMOD;
  452. ds2760_battery_write_status(di, status);
  453. /* set rated capacity from module param */
  454. if (rated_capacity)
  455. ds2760_battery_write_rated_capacity(di, rated_capacity);
  456. /* set current accumulator if given as parameter.
  457. * this should only be done for bootstrapping the value */
  458. if (current_accum)
  459. ds2760_battery_set_current_accum(di, current_accum);
  460. retval = power_supply_register(&pdev->dev, &di->bat);
  461. if (retval) {
  462. dev_err(di->dev, "failed to register battery\n");
  463. goto batt_failed;
  464. }
  465. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  466. INIT_DELAYED_WORK(&di->set_charged_work,
  467. ds2760_battery_set_charged_work);
  468. di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
  469. if (!di->monitor_wqueue) {
  470. retval = -ESRCH;
  471. goto workqueue_failed;
  472. }
  473. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  474. goto success;
  475. workqueue_failed:
  476. power_supply_unregister(&di->bat);
  477. batt_failed:
  478. kfree(di);
  479. di_alloc_failed:
  480. success:
  481. return retval;
  482. }
  483. static int ds2760_battery_remove(struct platform_device *pdev)
  484. {
  485. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  486. cancel_delayed_work_sync(&di->monitor_work);
  487. cancel_delayed_work_sync(&di->set_charged_work);
  488. destroy_workqueue(di->monitor_wqueue);
  489. power_supply_unregister(&di->bat);
  490. kfree(di);
  491. return 0;
  492. }
  493. #ifdef CONFIG_PM
  494. static int ds2760_battery_suspend(struct platform_device *pdev,
  495. pm_message_t state)
  496. {
  497. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  498. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  499. return 0;
  500. }
  501. static int ds2760_battery_resume(struct platform_device *pdev)
  502. {
  503. struct ds2760_device_info *di = platform_get_drvdata(pdev);
  504. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  505. power_supply_changed(&di->bat);
  506. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  507. return 0;
  508. }
  509. #else
  510. #define ds2760_battery_suspend NULL
  511. #define ds2760_battery_resume NULL
  512. #endif /* CONFIG_PM */
  513. MODULE_ALIAS("platform:ds2760-battery");
  514. static struct platform_driver ds2760_battery_driver = {
  515. .driver = {
  516. .name = "ds2760-battery",
  517. },
  518. .probe = ds2760_battery_probe,
  519. .remove = ds2760_battery_remove,
  520. .suspend = ds2760_battery_suspend,
  521. .resume = ds2760_battery_resume,
  522. };
  523. module_platform_driver(ds2760_battery_driver);
  524. MODULE_LICENSE("GPL");
  525. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  526. "Matt Reimer <mreimer@vpop.net>, "
  527. "Anton Vorontsov <cbou@mail.ru>");
  528. MODULE_DESCRIPTION("ds2760 battery driver");