charger-manager.c 36 KB

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