charger-manager.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  3. * MyungJoo Ham <myungjoo.ham@samsung.com>
  4. *
  5. * This driver enables to monitor battery health and control charger
  6. * during suspend-to-mem.
  7. * Charger manager depends on other devices. register this later than
  8. * the depending devices.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. **/
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/rtc.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power/charger-manager.h>
  23. #include <linux/regulator/consumer.h>
  24. /*
  25. * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
  26. * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
  27. * without any delays.
  28. */
  29. #define CM_JIFFIES_SMALL (2)
  30. /* If y is valid (> 0) and smaller than x, do x = y */
  31. #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
  32. /*
  33. * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
  34. * rtc alarm. It should be 2 or larger
  35. */
  36. #define CM_RTC_SMALL (2)
  37. #define UEVENT_BUF_SIZE 32
  38. static LIST_HEAD(cm_list);
  39. static DEFINE_MUTEX(cm_list_mtx);
  40. /* About in-suspend (suspend-again) monitoring */
  41. static struct rtc_device *rtc_dev;
  42. /*
  43. * Backup RTC alarm
  44. * Save the wakeup alarm before entering suspend-to-RAM
  45. */
  46. static struct rtc_wkalrm rtc_wkalarm_save;
  47. /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
  48. static unsigned long rtc_wkalarm_save_time;
  49. static bool cm_suspended;
  50. static bool cm_rtc_set;
  51. static unsigned long cm_suspend_duration_ms;
  52. /* About normal (not suspended) monitoring */
  53. static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
  54. static unsigned long next_polling; /* Next appointed polling time */
  55. static struct workqueue_struct *cm_wq; /* init at driver add */
  56. static struct delayed_work cm_monitor_work; /* init at driver add */
  57. /* Global charger-manager description */
  58. static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
  59. /**
  60. * is_batt_present - See if the battery presents in place.
  61. * @cm: the Charger Manager representing the battery.
  62. */
  63. static bool is_batt_present(struct charger_manager *cm)
  64. {
  65. union power_supply_propval val;
  66. bool present = false;
  67. int i, ret;
  68. switch (cm->desc->battery_present) {
  69. case CM_BATTERY_PRESENT:
  70. present = true;
  71. break;
  72. case CM_NO_BATTERY:
  73. break;
  74. case CM_FUEL_GAUGE:
  75. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  76. POWER_SUPPLY_PROP_PRESENT, &val);
  77. if (ret == 0 && val.intval)
  78. present = true;
  79. break;
  80. case CM_CHARGER_STAT:
  81. for (i = 0; cm->charger_stat[i]; i++) {
  82. ret = cm->charger_stat[i]->get_property(
  83. cm->charger_stat[i],
  84. POWER_SUPPLY_PROP_PRESENT, &val);
  85. if (ret == 0 && val.intval) {
  86. present = true;
  87. break;
  88. }
  89. }
  90. break;
  91. }
  92. return present;
  93. }
  94. /**
  95. * is_ext_pwr_online - See if an external power source is attached to charge
  96. * @cm: the Charger Manager representing the battery.
  97. *
  98. * Returns true if at least one of the chargers of the battery has an external
  99. * power source attached to charge the battery regardless of whether it is
  100. * actually charging or not.
  101. */
  102. static bool is_ext_pwr_online(struct charger_manager *cm)
  103. {
  104. union power_supply_propval val;
  105. bool online = false;
  106. int i, ret;
  107. /* If at least one of them has one, it's yes. */
  108. for (i = 0; cm->charger_stat[i]; i++) {
  109. ret = cm->charger_stat[i]->get_property(
  110. cm->charger_stat[i],
  111. POWER_SUPPLY_PROP_ONLINE, &val);
  112. if (ret == 0 && val.intval) {
  113. online = true;
  114. break;
  115. }
  116. }
  117. return online;
  118. }
  119. /**
  120. * get_batt_uV - Get the voltage level of the battery
  121. * @cm: the Charger Manager representing the battery.
  122. * @uV: the voltage level returned.
  123. *
  124. * Returns 0 if there is no error.
  125. * Returns a negative value on error.
  126. */
  127. static int get_batt_uV(struct charger_manager *cm, int *uV)
  128. {
  129. union power_supply_propval val;
  130. int ret;
  131. if (!cm->fuel_gauge)
  132. return -ENODEV;
  133. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  134. POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
  135. if (ret)
  136. return ret;
  137. *uV = val.intval;
  138. return 0;
  139. }
  140. /**
  141. * is_charging - Returns true if the battery is being charged.
  142. * @cm: the Charger Manager representing the battery.
  143. */
  144. static bool is_charging(struct charger_manager *cm)
  145. {
  146. int i, ret;
  147. bool charging = false;
  148. union power_supply_propval val;
  149. /* If there is no battery, it cannot be charged */
  150. if (!is_batt_present(cm))
  151. return false;
  152. /* If at least one of the charger is charging, return yes */
  153. for (i = 0; cm->charger_stat[i]; i++) {
  154. /* 1. The charger sholuld not be DISABLED */
  155. if (cm->emergency_stop)
  156. continue;
  157. if (!cm->charger_enabled)
  158. continue;
  159. /* 2. The charger should be online (ext-power) */
  160. ret = cm->charger_stat[i]->get_property(
  161. cm->charger_stat[i],
  162. POWER_SUPPLY_PROP_ONLINE, &val);
  163. if (ret) {
  164. dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
  165. cm->desc->psy_charger_stat[i]);
  166. continue;
  167. }
  168. if (val.intval == 0)
  169. continue;
  170. /*
  171. * 3. The charger should not be FULL, DISCHARGING,
  172. * or NOT_CHARGING.
  173. */
  174. ret = cm->charger_stat[i]->get_property(
  175. cm->charger_stat[i],
  176. POWER_SUPPLY_PROP_STATUS, &val);
  177. if (ret) {
  178. dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
  179. cm->desc->psy_charger_stat[i]);
  180. continue;
  181. }
  182. if (val.intval == POWER_SUPPLY_STATUS_FULL ||
  183. val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
  184. val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
  185. continue;
  186. /* Then, this is charging. */
  187. charging = true;
  188. break;
  189. }
  190. return charging;
  191. }
  192. /**
  193. * is_polling_required - Return true if need to continue polling for this CM.
  194. * @cm: the Charger Manager representing the battery.
  195. */
  196. static bool is_polling_required(struct charger_manager *cm)
  197. {
  198. switch (cm->desc->polling_mode) {
  199. case CM_POLL_DISABLE:
  200. return false;
  201. case CM_POLL_ALWAYS:
  202. return true;
  203. case CM_POLL_EXTERNAL_POWER_ONLY:
  204. return is_ext_pwr_online(cm);
  205. case CM_POLL_CHARGING_ONLY:
  206. return is_charging(cm);
  207. default:
  208. dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
  209. cm->desc->polling_mode);
  210. }
  211. return false;
  212. }
  213. /**
  214. * try_charger_enable - Enable/Disable chargers altogether
  215. * @cm: the Charger Manager representing the battery.
  216. * @enable: true: enable / false: disable
  217. *
  218. * Note that Charger Manager keeps the charger enabled regardless whether
  219. * the charger is charging or not (because battery is full or no external
  220. * power source exists) except when CM needs to disable chargers forcibly
  221. * bacause of emergency causes; when the battery is overheated or too cold.
  222. */
  223. static int try_charger_enable(struct charger_manager *cm, bool enable)
  224. {
  225. int err = 0, i;
  226. struct charger_desc *desc = cm->desc;
  227. /* Ignore if it's redundent command */
  228. if (enable == cm->charger_enabled)
  229. return 0;
  230. if (enable) {
  231. if (cm->emergency_stop)
  232. return -EAGAIN;
  233. err = regulator_bulk_enable(desc->num_charger_regulators,
  234. desc->charger_regulators);
  235. } else {
  236. /*
  237. * Abnormal battery state - Stop charging forcibly,
  238. * even if charger was enabled at the other places
  239. */
  240. err = regulator_bulk_disable(desc->num_charger_regulators,
  241. desc->charger_regulators);
  242. for (i = 0; i < desc->num_charger_regulators; i++) {
  243. if (regulator_is_enabled(
  244. desc->charger_regulators[i].consumer)) {
  245. regulator_force_disable(
  246. desc->charger_regulators[i].consumer);
  247. dev_warn(cm->dev,
  248. "Disable regulator(%s) forcibly.\n",
  249. desc->charger_regulators[i].supply);
  250. }
  251. }
  252. }
  253. if (!err)
  254. cm->charger_enabled = enable;
  255. return err;
  256. }
  257. /**
  258. * try_charger_restart - Restart charging.
  259. * @cm: the Charger Manager representing the battery.
  260. *
  261. * Restart charging by turning off and on the charger.
  262. */
  263. static int try_charger_restart(struct charger_manager *cm)
  264. {
  265. int err;
  266. if (cm->emergency_stop)
  267. return -EAGAIN;
  268. err = try_charger_enable(cm, false);
  269. if (err)
  270. return err;
  271. return try_charger_enable(cm, true);
  272. }
  273. /**
  274. * uevent_notify - Let users know something has changed.
  275. * @cm: the Charger Manager representing the battery.
  276. * @event: the event string.
  277. *
  278. * If @event is null, it implies that uevent_notify is called
  279. * by resume function. When called in the resume function, cm_suspended
  280. * should be already reset to false in order to let uevent_notify
  281. * notify the recent event during the suspend to users. While
  282. * suspended, uevent_notify does not notify users, but tracks
  283. * events so that uevent_notify can notify users later after resumed.
  284. */
  285. static void uevent_notify(struct charger_manager *cm, const char *event)
  286. {
  287. static char env_str[UEVENT_BUF_SIZE + 1] = "";
  288. static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
  289. if (cm_suspended) {
  290. /* Nothing in suspended-event buffer */
  291. if (env_str_save[0] == 0) {
  292. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  293. return; /* status not changed */
  294. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  295. return;
  296. }
  297. if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
  298. return; /* Duplicated. */
  299. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  300. return;
  301. }
  302. if (event == NULL) {
  303. /* No messages pending */
  304. if (!env_str_save[0])
  305. return;
  306. strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
  307. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  308. env_str_save[0] = 0;
  309. return;
  310. }
  311. /* status not changed */
  312. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  313. return;
  314. /* save the status and notify the update */
  315. strncpy(env_str, event, UEVENT_BUF_SIZE);
  316. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  317. dev_info(cm->dev, event);
  318. }
  319. /**
  320. * fullbatt_vchk - Check voltage drop some times after "FULL" event.
  321. * @work: the work_struct appointing the function
  322. *
  323. * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
  324. * charger_desc, Charger Manager checks voltage drop after the battery
  325. * "FULL" event. It checks whether the voltage has dropped more than
  326. * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
  327. */
  328. static void fullbatt_vchk(struct work_struct *work)
  329. {
  330. struct delayed_work *dwork = to_delayed_work(work);
  331. struct charger_manager *cm = container_of(dwork,
  332. struct charger_manager, fullbatt_vchk_work);
  333. struct charger_desc *desc = cm->desc;
  334. int batt_uV, err, diff;
  335. /* remove the appointment for fullbatt_vchk */
  336. cm->fullbatt_vchk_jiffies_at = 0;
  337. if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
  338. return;
  339. err = get_batt_uV(cm, &batt_uV);
  340. if (err) {
  341. dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
  342. return;
  343. }
  344. diff = cm->fullbatt_vchk_uV;
  345. diff -= batt_uV;
  346. dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
  347. if (diff > desc->fullbatt_vchkdrop_uV) {
  348. try_charger_restart(cm);
  349. uevent_notify(cm, "Recharge");
  350. }
  351. }
  352. /**
  353. * _cm_monitor - Monitor the temperature and return true for exceptions.
  354. * @cm: the Charger Manager representing the battery.
  355. *
  356. * Returns true if there is an event to notify for the battery.
  357. * (True if the status of "emergency_stop" changes)
  358. */
  359. static bool _cm_monitor(struct charger_manager *cm)
  360. {
  361. struct charger_desc *desc = cm->desc;
  362. int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
  363. dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
  364. cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
  365. /* It has been stopped or charging already */
  366. if (!!temp == !!cm->emergency_stop)
  367. return false;
  368. if (temp) {
  369. cm->emergency_stop = temp;
  370. if (!try_charger_enable(cm, false)) {
  371. if (temp > 0)
  372. uevent_notify(cm, "OVERHEAT");
  373. else
  374. uevent_notify(cm, "COLD");
  375. }
  376. } else {
  377. cm->emergency_stop = 0;
  378. if (!try_charger_enable(cm, true))
  379. uevent_notify(cm, "CHARGING");
  380. }
  381. return true;
  382. }
  383. /**
  384. * cm_monitor - Monitor every battery.
  385. *
  386. * Returns true if there is an event to notify from any of the batteries.
  387. * (True if the status of "emergency_stop" changes)
  388. */
  389. static bool cm_monitor(void)
  390. {
  391. bool stop = false;
  392. struct charger_manager *cm;
  393. mutex_lock(&cm_list_mtx);
  394. list_for_each_entry(cm, &cm_list, entry) {
  395. if (_cm_monitor(cm))
  396. stop = true;
  397. }
  398. mutex_unlock(&cm_list_mtx);
  399. return stop;
  400. }
  401. /**
  402. * _setup_polling - Setup the next instance of polling.
  403. * @work: work_struct of the function _setup_polling.
  404. */
  405. static void _setup_polling(struct work_struct *work)
  406. {
  407. unsigned long min = ULONG_MAX;
  408. struct charger_manager *cm;
  409. bool keep_polling = false;
  410. unsigned long _next_polling;
  411. mutex_lock(&cm_list_mtx);
  412. list_for_each_entry(cm, &cm_list, entry) {
  413. if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
  414. keep_polling = true;
  415. if (min > cm->desc->polling_interval_ms)
  416. min = cm->desc->polling_interval_ms;
  417. }
  418. }
  419. polling_jiffy = msecs_to_jiffies(min);
  420. if (polling_jiffy <= CM_JIFFIES_SMALL)
  421. polling_jiffy = CM_JIFFIES_SMALL + 1;
  422. if (!keep_polling)
  423. polling_jiffy = ULONG_MAX;
  424. if (polling_jiffy == ULONG_MAX)
  425. goto out;
  426. WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
  427. ". try it later. %s\n", __func__);
  428. _next_polling = jiffies + polling_jiffy;
  429. if (!delayed_work_pending(&cm_monitor_work) ||
  430. (delayed_work_pending(&cm_monitor_work) &&
  431. time_after(next_polling, _next_polling))) {
  432. cancel_delayed_work_sync(&cm_monitor_work);
  433. next_polling = jiffies + polling_jiffy;
  434. queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
  435. }
  436. out:
  437. mutex_unlock(&cm_list_mtx);
  438. }
  439. static DECLARE_WORK(setup_polling, _setup_polling);
  440. /**
  441. * cm_monitor_poller - The Monitor / Poller.
  442. * @work: work_struct of the function cm_monitor_poller
  443. *
  444. * During non-suspended state, cm_monitor_poller is used to poll and monitor
  445. * the batteries.
  446. */
  447. static void cm_monitor_poller(struct work_struct *work)
  448. {
  449. cm_monitor();
  450. schedule_work(&setup_polling);
  451. }
  452. static int charger_get_property(struct power_supply *psy,
  453. enum power_supply_property psp,
  454. union power_supply_propval *val)
  455. {
  456. struct charger_manager *cm = container_of(psy,
  457. struct charger_manager, charger_psy);
  458. struct charger_desc *desc = cm->desc;
  459. int ret = 0;
  460. int uV;
  461. switch (psp) {
  462. case POWER_SUPPLY_PROP_STATUS:
  463. if (is_charging(cm))
  464. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  465. else if (is_ext_pwr_online(cm))
  466. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  467. else
  468. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  469. break;
  470. case POWER_SUPPLY_PROP_HEALTH:
  471. if (cm->emergency_stop > 0)
  472. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  473. else if (cm->emergency_stop < 0)
  474. val->intval = POWER_SUPPLY_HEALTH_COLD;
  475. else
  476. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  477. break;
  478. case POWER_SUPPLY_PROP_PRESENT:
  479. if (is_batt_present(cm))
  480. val->intval = 1;
  481. else
  482. val->intval = 0;
  483. break;
  484. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  485. ret = get_batt_uV(cm, &val->intval);
  486. break;
  487. case POWER_SUPPLY_PROP_CURRENT_NOW:
  488. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  489. POWER_SUPPLY_PROP_CURRENT_NOW, val);
  490. break;
  491. case POWER_SUPPLY_PROP_TEMP:
  492. /* in thenth of centigrade */
  493. if (cm->last_temp_mC == INT_MIN)
  494. desc->temperature_out_of_range(&cm->last_temp_mC);
  495. val->intval = cm->last_temp_mC / 100;
  496. if (!desc->measure_battery_temp)
  497. ret = -ENODEV;
  498. break;
  499. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  500. /* in thenth of centigrade */
  501. if (cm->last_temp_mC == INT_MIN)
  502. desc->temperature_out_of_range(&cm->last_temp_mC);
  503. val->intval = cm->last_temp_mC / 100;
  504. if (desc->measure_battery_temp)
  505. ret = -ENODEV;
  506. break;
  507. case POWER_SUPPLY_PROP_CAPACITY:
  508. if (!cm->fuel_gauge) {
  509. ret = -ENODEV;
  510. break;
  511. }
  512. if (!is_batt_present(cm)) {
  513. /* There is no battery. Assume 100% */
  514. val->intval = 100;
  515. break;
  516. }
  517. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  518. POWER_SUPPLY_PROP_CAPACITY, val);
  519. if (ret)
  520. break;
  521. if (val->intval > 100) {
  522. val->intval = 100;
  523. break;
  524. }
  525. if (val->intval < 0)
  526. val->intval = 0;
  527. /* Do not adjust SOC when charging: voltage is overrated */
  528. if (is_charging(cm))
  529. break;
  530. /*
  531. * If the capacity value is inconsistent, calibrate it base on
  532. * the battery voltage values and the thresholds given as desc
  533. */
  534. ret = get_batt_uV(cm, &uV);
  535. if (ret) {
  536. /* Voltage information not available. No calibration */
  537. ret = 0;
  538. break;
  539. }
  540. if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  541. !is_charging(cm)) {
  542. val->intval = 100;
  543. break;
  544. }
  545. break;
  546. case POWER_SUPPLY_PROP_ONLINE:
  547. if (is_ext_pwr_online(cm))
  548. val->intval = 1;
  549. else
  550. val->intval = 0;
  551. break;
  552. case POWER_SUPPLY_PROP_CHARGE_FULL:
  553. if (cm->fuel_gauge) {
  554. if (cm->fuel_gauge->get_property(cm->fuel_gauge,
  555. POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
  556. break;
  557. }
  558. if (is_ext_pwr_online(cm)) {
  559. /* Not full if it's charging. */
  560. if (is_charging(cm)) {
  561. val->intval = 0;
  562. break;
  563. }
  564. /*
  565. * Full if it's powered but not charging andi
  566. * not forced stop by emergency
  567. */
  568. if (!cm->emergency_stop) {
  569. val->intval = 1;
  570. break;
  571. }
  572. }
  573. /* Full if it's over the fullbatt voltage */
  574. ret = get_batt_uV(cm, &uV);
  575. if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  576. !is_charging(cm)) {
  577. val->intval = 1;
  578. break;
  579. }
  580. /* Full if the cap is 100 */
  581. if (cm->fuel_gauge) {
  582. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  583. POWER_SUPPLY_PROP_CAPACITY, val);
  584. if (!ret && val->intval >= 100 && !is_charging(cm)) {
  585. val->intval = 1;
  586. break;
  587. }
  588. }
  589. val->intval = 0;
  590. ret = 0;
  591. break;
  592. case POWER_SUPPLY_PROP_CHARGE_NOW:
  593. if (is_charging(cm)) {
  594. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  595. POWER_SUPPLY_PROP_CHARGE_NOW,
  596. val);
  597. if (ret) {
  598. val->intval = 1;
  599. ret = 0;
  600. } else {
  601. /* If CHARGE_NOW is supplied, use it */
  602. val->intval = (val->intval > 0) ?
  603. val->intval : 1;
  604. }
  605. } else {
  606. val->intval = 0;
  607. }
  608. break;
  609. default:
  610. return -EINVAL;
  611. }
  612. return ret;
  613. }
  614. #define NUM_CHARGER_PSY_OPTIONAL (4)
  615. static enum power_supply_property default_charger_props[] = {
  616. /* Guaranteed to provide */
  617. POWER_SUPPLY_PROP_STATUS,
  618. POWER_SUPPLY_PROP_HEALTH,
  619. POWER_SUPPLY_PROP_PRESENT,
  620. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  621. POWER_SUPPLY_PROP_CAPACITY,
  622. POWER_SUPPLY_PROP_ONLINE,
  623. POWER_SUPPLY_PROP_CHARGE_FULL,
  624. /*
  625. * Optional properties are:
  626. * POWER_SUPPLY_PROP_CHARGE_NOW,
  627. * POWER_SUPPLY_PROP_CURRENT_NOW,
  628. * POWER_SUPPLY_PROP_TEMP, and
  629. * POWER_SUPPLY_PROP_TEMP_AMBIENT,
  630. */
  631. };
  632. static struct power_supply psy_default = {
  633. .name = "battery",
  634. .type = POWER_SUPPLY_TYPE_BATTERY,
  635. .properties = default_charger_props,
  636. .num_properties = ARRAY_SIZE(default_charger_props),
  637. .get_property = charger_get_property,
  638. };
  639. /**
  640. * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
  641. * for suspend_again.
  642. *
  643. * Returns true if the alarm is set for Charger Manager to use.
  644. * Returns false if
  645. * cm_setup_timer fails to set an alarm,
  646. * cm_setup_timer does not need to set an alarm for Charger Manager,
  647. * or an alarm previously configured is to be used.
  648. */
  649. static bool cm_setup_timer(void)
  650. {
  651. struct charger_manager *cm;
  652. unsigned int wakeup_ms = UINT_MAX;
  653. bool ret = false;
  654. mutex_lock(&cm_list_mtx);
  655. list_for_each_entry(cm, &cm_list, entry) {
  656. unsigned int fbchk_ms = 0;
  657. /* fullbatt_vchk is required. setup timer for that */
  658. if (cm->fullbatt_vchk_jiffies_at) {
  659. fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
  660. - jiffies);
  661. if (time_is_before_eq_jiffies(
  662. cm->fullbatt_vchk_jiffies_at) ||
  663. msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
  664. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  665. fbchk_ms = 0;
  666. }
  667. }
  668. CM_MIN_VALID(wakeup_ms, fbchk_ms);
  669. /* Skip if polling is not required for this CM */
  670. if (!is_polling_required(cm) && !cm->emergency_stop)
  671. continue;
  672. if (cm->desc->polling_interval_ms == 0)
  673. continue;
  674. CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
  675. }
  676. mutex_unlock(&cm_list_mtx);
  677. if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
  678. pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
  679. if (rtc_dev) {
  680. struct rtc_wkalrm tmp;
  681. unsigned long time, now;
  682. unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
  683. /*
  684. * Set alarm with the polling interval (wakeup_ms)
  685. * except when rtc_wkalarm_save comes first.
  686. * However, the alarm time should be NOW +
  687. * CM_RTC_SMALL or later.
  688. */
  689. tmp.enabled = 1;
  690. rtc_read_time(rtc_dev, &tmp.time);
  691. rtc_tm_to_time(&tmp.time, &now);
  692. if (add < CM_RTC_SMALL)
  693. add = CM_RTC_SMALL;
  694. time = now + add;
  695. ret = true;
  696. if (rtc_wkalarm_save.enabled &&
  697. rtc_wkalarm_save_time &&
  698. rtc_wkalarm_save_time < time) {
  699. if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
  700. time = now + CM_RTC_SMALL;
  701. else
  702. time = rtc_wkalarm_save_time;
  703. /* The timer is not appointed by CM */
  704. ret = false;
  705. }
  706. pr_info("Waking up after %lu secs.\n",
  707. time - now);
  708. rtc_time_to_tm(time, &tmp.time);
  709. rtc_set_alarm(rtc_dev, &tmp);
  710. cm_suspend_duration_ms += wakeup_ms;
  711. return ret;
  712. }
  713. }
  714. if (rtc_dev)
  715. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  716. return false;
  717. }
  718. static void _cm_fbchk_in_suspend(struct charger_manager *cm)
  719. {
  720. unsigned long jiffy_now = jiffies;
  721. if (!cm->fullbatt_vchk_jiffies_at)
  722. return;
  723. if (g_desc && g_desc->assume_timer_stops_in_suspend)
  724. jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
  725. /* Execute now if it's going to be executed not too long after */
  726. jiffy_now += CM_JIFFIES_SMALL;
  727. if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
  728. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  729. }
  730. /**
  731. * cm_suspend_again - Determine whether suspend again or not
  732. *
  733. * Returns true if the system should be suspended again
  734. * Returns false if the system should be woken up
  735. */
  736. bool cm_suspend_again(void)
  737. {
  738. struct charger_manager *cm;
  739. bool ret = false;
  740. if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
  741. !cm_rtc_set)
  742. return false;
  743. if (cm_monitor())
  744. goto out;
  745. ret = true;
  746. mutex_lock(&cm_list_mtx);
  747. list_for_each_entry(cm, &cm_list, entry) {
  748. _cm_fbchk_in_suspend(cm);
  749. if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
  750. cm->status_save_batt != is_batt_present(cm)) {
  751. ret = false;
  752. break;
  753. }
  754. }
  755. mutex_unlock(&cm_list_mtx);
  756. cm_rtc_set = cm_setup_timer();
  757. out:
  758. /* It's about the time when the non-CM appointed timer goes off */
  759. if (rtc_wkalarm_save.enabled) {
  760. unsigned long now;
  761. struct rtc_time tmp;
  762. rtc_read_time(rtc_dev, &tmp);
  763. rtc_tm_to_time(&tmp, &now);
  764. if (rtc_wkalarm_save_time &&
  765. now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
  766. return false;
  767. }
  768. return ret;
  769. }
  770. EXPORT_SYMBOL_GPL(cm_suspend_again);
  771. /**
  772. * setup_charger_manager - initialize charger_global_desc data
  773. * @gd: pointer to instance of charger_global_desc
  774. */
  775. int setup_charger_manager(struct charger_global_desc *gd)
  776. {
  777. if (!gd)
  778. return -EINVAL;
  779. if (rtc_dev)
  780. rtc_class_close(rtc_dev);
  781. rtc_dev = NULL;
  782. g_desc = NULL;
  783. if (!gd->rtc_only_wakeup) {
  784. pr_err("The callback rtc_only_wakeup is not given.\n");
  785. return -EINVAL;
  786. }
  787. if (gd->rtc_name) {
  788. rtc_dev = rtc_class_open(gd->rtc_name);
  789. if (IS_ERR_OR_NULL(rtc_dev)) {
  790. rtc_dev = NULL;
  791. /* Retry at probe. RTC may be not registered yet */
  792. }
  793. } else {
  794. pr_warn("No wakeup timer is given for charger manager."
  795. "In-suspend monitoring won't work.\n");
  796. }
  797. g_desc = gd;
  798. return 0;
  799. }
  800. EXPORT_SYMBOL_GPL(setup_charger_manager);
  801. static int charger_manager_probe(struct platform_device *pdev)
  802. {
  803. struct charger_desc *desc = dev_get_platdata(&pdev->dev);
  804. struct charger_manager *cm;
  805. int ret = 0, i = 0;
  806. union power_supply_propval val;
  807. if (g_desc && !rtc_dev && g_desc->rtc_name) {
  808. rtc_dev = rtc_class_open(g_desc->rtc_name);
  809. if (IS_ERR_OR_NULL(rtc_dev)) {
  810. rtc_dev = NULL;
  811. dev_err(&pdev->dev, "Cannot get RTC %s.\n",
  812. g_desc->rtc_name);
  813. ret = -ENODEV;
  814. goto err_alloc;
  815. }
  816. }
  817. if (!desc) {
  818. dev_err(&pdev->dev, "No platform data (desc) found.\n");
  819. ret = -ENODEV;
  820. goto err_alloc;
  821. }
  822. cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
  823. if (!cm) {
  824. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  825. ret = -ENOMEM;
  826. goto err_alloc;
  827. }
  828. /* Basic Values. Unspecified are Null or 0 */
  829. cm->dev = &pdev->dev;
  830. cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
  831. if (!cm->desc) {
  832. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  833. ret = -ENOMEM;
  834. goto err_alloc_desc;
  835. }
  836. memcpy(cm->desc, desc, sizeof(struct charger_desc));
  837. cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
  838. /*
  839. * The following two do not need to be errors.
  840. * Users may intentionally ignore those two features.
  841. */
  842. if (desc->fullbatt_uV == 0) {
  843. dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
  844. " as it is not supplied.");
  845. }
  846. if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
  847. dev_info(&pdev->dev, "Disabling full-battery voltage drop "
  848. "checking mechanism as it is not supplied.");
  849. desc->fullbatt_vchkdrop_ms = 0;
  850. desc->fullbatt_vchkdrop_uV = 0;
  851. }
  852. if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
  853. ret = -EINVAL;
  854. dev_err(&pdev->dev, "charger_regulators undefined.\n");
  855. goto err_no_charger;
  856. }
  857. if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
  858. dev_err(&pdev->dev, "No power supply defined.\n");
  859. ret = -EINVAL;
  860. goto err_no_charger_stat;
  861. }
  862. /* Counting index only */
  863. while (desc->psy_charger_stat[i])
  864. i++;
  865. cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
  866. GFP_KERNEL);
  867. if (!cm->charger_stat) {
  868. ret = -ENOMEM;
  869. goto err_no_charger_stat;
  870. }
  871. for (i = 0; desc->psy_charger_stat[i]; i++) {
  872. cm->charger_stat[i] = power_supply_get_by_name(
  873. desc->psy_charger_stat[i]);
  874. if (!cm->charger_stat[i]) {
  875. dev_err(&pdev->dev, "Cannot find power supply "
  876. "\"%s\"\n",
  877. desc->psy_charger_stat[i]);
  878. ret = -ENODEV;
  879. goto err_chg_stat;
  880. }
  881. }
  882. cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
  883. if (!cm->fuel_gauge) {
  884. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  885. desc->psy_fuel_gauge);
  886. ret = -ENODEV;
  887. goto err_chg_stat;
  888. }
  889. if (desc->polling_interval_ms == 0 ||
  890. msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
  891. dev_err(&pdev->dev, "polling_interval_ms is too small\n");
  892. ret = -EINVAL;
  893. goto err_chg_stat;
  894. }
  895. if (!desc->temperature_out_of_range) {
  896. dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
  897. ret = -EINVAL;
  898. goto err_chg_stat;
  899. }
  900. platform_set_drvdata(pdev, cm);
  901. memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
  902. if (!desc->psy_name) {
  903. strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
  904. } else {
  905. strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
  906. }
  907. cm->charger_psy.name = cm->psy_name_buf;
  908. /* Allocate for psy properties because they may vary */
  909. cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
  910. * (ARRAY_SIZE(default_charger_props) +
  911. NUM_CHARGER_PSY_OPTIONAL),
  912. GFP_KERNEL);
  913. if (!cm->charger_psy.properties) {
  914. dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
  915. ret = -ENOMEM;
  916. goto err_chg_stat;
  917. }
  918. memcpy(cm->charger_psy.properties, default_charger_props,
  919. sizeof(enum power_supply_property) *
  920. ARRAY_SIZE(default_charger_props));
  921. cm->charger_psy.num_properties = psy_default.num_properties;
  922. /* Find which optional psy-properties are available */
  923. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  924. POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
  925. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  926. POWER_SUPPLY_PROP_CHARGE_NOW;
  927. cm->charger_psy.num_properties++;
  928. }
  929. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  930. POWER_SUPPLY_PROP_CURRENT_NOW,
  931. &val)) {
  932. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  933. POWER_SUPPLY_PROP_CURRENT_NOW;
  934. cm->charger_psy.num_properties++;
  935. }
  936. if (desc->measure_battery_temp) {
  937. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  938. POWER_SUPPLY_PROP_TEMP;
  939. cm->charger_psy.num_properties++;
  940. } else {
  941. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  942. POWER_SUPPLY_PROP_TEMP_AMBIENT;
  943. cm->charger_psy.num_properties++;
  944. }
  945. INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
  946. ret = power_supply_register(NULL, &cm->charger_psy);
  947. if (ret) {
  948. dev_err(&pdev->dev, "Cannot register charger-manager with"
  949. " name \"%s\".\n", cm->charger_psy.name);
  950. goto err_register;
  951. }
  952. ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
  953. desc->charger_regulators);
  954. if (ret) {
  955. dev_err(&pdev->dev, "Cannot get charger regulators.\n");
  956. goto err_bulk_get;
  957. }
  958. ret = try_charger_enable(cm, true);
  959. if (ret) {
  960. dev_err(&pdev->dev, "Cannot enable charger regulators\n");
  961. goto err_chg_enable;
  962. }
  963. /* Add to the list */
  964. mutex_lock(&cm_list_mtx);
  965. list_add(&cm->entry, &cm_list);
  966. mutex_unlock(&cm_list_mtx);
  967. schedule_work(&setup_polling);
  968. return 0;
  969. err_chg_enable:
  970. regulator_bulk_free(desc->num_charger_regulators,
  971. desc->charger_regulators);
  972. err_bulk_get:
  973. power_supply_unregister(&cm->charger_psy);
  974. err_register:
  975. kfree(cm->charger_psy.properties);
  976. err_chg_stat:
  977. kfree(cm->charger_stat);
  978. err_no_charger_stat:
  979. err_no_charger:
  980. kfree(cm->desc);
  981. err_alloc_desc:
  982. kfree(cm);
  983. err_alloc:
  984. return ret;
  985. }
  986. static int __devexit charger_manager_remove(struct platform_device *pdev)
  987. {
  988. struct charger_manager *cm = platform_get_drvdata(pdev);
  989. struct charger_desc *desc = cm->desc;
  990. /* Remove from the list */
  991. mutex_lock(&cm_list_mtx);
  992. list_del(&cm->entry);
  993. mutex_unlock(&cm_list_mtx);
  994. if (work_pending(&setup_polling))
  995. cancel_work_sync(&setup_polling);
  996. if (delayed_work_pending(&cm_monitor_work))
  997. cancel_delayed_work_sync(&cm_monitor_work);
  998. regulator_bulk_free(desc->num_charger_regulators,
  999. desc->charger_regulators);
  1000. power_supply_unregister(&cm->charger_psy);
  1001. try_charger_enable(cm, false);
  1002. kfree(cm->charger_psy.properties);
  1003. kfree(cm->charger_stat);
  1004. kfree(cm->desc);
  1005. kfree(cm);
  1006. return 0;
  1007. }
  1008. static const struct platform_device_id charger_manager_id[] = {
  1009. { "charger-manager", 0 },
  1010. { },
  1011. };
  1012. MODULE_DEVICE_TABLE(platform, charger_manager_id);
  1013. static int cm_suspend_prepare(struct device *dev)
  1014. {
  1015. struct charger_manager *cm = dev_get_drvdata(dev);
  1016. if (!cm_suspended) {
  1017. if (rtc_dev) {
  1018. struct rtc_time tmp;
  1019. unsigned long now;
  1020. rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
  1021. rtc_read_time(rtc_dev, &tmp);
  1022. if (rtc_wkalarm_save.enabled) {
  1023. rtc_tm_to_time(&rtc_wkalarm_save.time,
  1024. &rtc_wkalarm_save_time);
  1025. rtc_tm_to_time(&tmp, &now);
  1026. if (now > rtc_wkalarm_save_time)
  1027. rtc_wkalarm_save_time = 0;
  1028. } else {
  1029. rtc_wkalarm_save_time = 0;
  1030. }
  1031. }
  1032. cm_suspended = true;
  1033. }
  1034. if (delayed_work_pending(&cm->fullbatt_vchk_work))
  1035. cancel_delayed_work(&cm->fullbatt_vchk_work);
  1036. cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
  1037. cm->status_save_batt = is_batt_present(cm);
  1038. if (!cm_rtc_set) {
  1039. cm_suspend_duration_ms = 0;
  1040. cm_rtc_set = cm_setup_timer();
  1041. }
  1042. return 0;
  1043. }
  1044. static void cm_suspend_complete(struct device *dev)
  1045. {
  1046. struct charger_manager *cm = dev_get_drvdata(dev);
  1047. if (cm_suspended) {
  1048. if (rtc_dev) {
  1049. struct rtc_wkalrm tmp;
  1050. rtc_read_alarm(rtc_dev, &tmp);
  1051. rtc_wkalarm_save.pending = tmp.pending;
  1052. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  1053. }
  1054. cm_suspended = false;
  1055. cm_rtc_set = false;
  1056. }
  1057. /* Re-enqueue delayed work (fullbatt_vchk_work) */
  1058. if (cm->fullbatt_vchk_jiffies_at) {
  1059. unsigned long delay = 0;
  1060. unsigned long now = jiffies + CM_JIFFIES_SMALL;
  1061. if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
  1062. delay = (unsigned long)((long)now
  1063. - (long)(cm->fullbatt_vchk_jiffies_at));
  1064. delay = jiffies_to_msecs(delay);
  1065. } else {
  1066. delay = 0;
  1067. }
  1068. /*
  1069. * Account for cm_suspend_duration_ms if
  1070. * assume_timer_stops_in_suspend is active
  1071. */
  1072. if (g_desc && g_desc->assume_timer_stops_in_suspend) {
  1073. if (delay > cm_suspend_duration_ms)
  1074. delay -= cm_suspend_duration_ms;
  1075. else
  1076. delay = 0;
  1077. }
  1078. queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  1079. msecs_to_jiffies(delay));
  1080. }
  1081. uevent_notify(cm, NULL);
  1082. }
  1083. static const struct dev_pm_ops charger_manager_pm = {
  1084. .prepare = cm_suspend_prepare,
  1085. .complete = cm_suspend_complete,
  1086. };
  1087. static struct platform_driver charger_manager_driver = {
  1088. .driver = {
  1089. .name = "charger-manager",
  1090. .owner = THIS_MODULE,
  1091. .pm = &charger_manager_pm,
  1092. },
  1093. .probe = charger_manager_probe,
  1094. .remove = __devexit_p(charger_manager_remove),
  1095. .id_table = charger_manager_id,
  1096. };
  1097. static int __init charger_manager_init(void)
  1098. {
  1099. cm_wq = create_freezable_workqueue("charger_manager");
  1100. INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
  1101. return platform_driver_register(&charger_manager_driver);
  1102. }
  1103. late_initcall(charger_manager_init);
  1104. static void __exit charger_manager_cleanup(void)
  1105. {
  1106. destroy_workqueue(cm_wq);
  1107. cm_wq = NULL;
  1108. platform_driver_unregister(&charger_manager_driver);
  1109. }
  1110. module_exit(charger_manager_cleanup);
  1111. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1112. MODULE_DESCRIPTION("Charger Manager");
  1113. MODULE_LICENSE("GPL");