ds2760_battery.c 18 KB

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