ds2760_battery.c 18 KB

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