charger-manager.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. /* Global charger-manager description */
  53. static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
  54. /**
  55. * is_batt_present - See if the battery presents in place.
  56. * @cm: the Charger Manager representing the battery.
  57. */
  58. static bool is_batt_present(struct charger_manager *cm)
  59. {
  60. union power_supply_propval val;
  61. bool present = false;
  62. int i, ret;
  63. switch (cm->desc->battery_present) {
  64. case CM_FUEL_GAUGE:
  65. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  66. POWER_SUPPLY_PROP_PRESENT, &val);
  67. if (ret == 0 && val.intval)
  68. present = true;
  69. break;
  70. case CM_CHARGER_STAT:
  71. for (i = 0; cm->charger_stat[i]; i++) {
  72. ret = cm->charger_stat[i]->get_property(
  73. cm->charger_stat[i],
  74. POWER_SUPPLY_PROP_PRESENT, &val);
  75. if (ret == 0 && val.intval) {
  76. present = true;
  77. break;
  78. }
  79. }
  80. break;
  81. }
  82. return present;
  83. }
  84. /**
  85. * is_ext_pwr_online - See if an external power source is attached to charge
  86. * @cm: the Charger Manager representing the battery.
  87. *
  88. * Returns true if at least one of the chargers of the battery has an external
  89. * power source attached to charge the battery regardless of whether it is
  90. * actually charging or not.
  91. */
  92. static bool is_ext_pwr_online(struct charger_manager *cm)
  93. {
  94. union power_supply_propval val;
  95. bool online = false;
  96. int i, ret;
  97. /* If at least one of them has one, it's yes. */
  98. for (i = 0; cm->charger_stat[i]; i++) {
  99. ret = cm->charger_stat[i]->get_property(
  100. cm->charger_stat[i],
  101. POWER_SUPPLY_PROP_ONLINE, &val);
  102. if (ret == 0 && val.intval) {
  103. online = true;
  104. break;
  105. }
  106. }
  107. return online;
  108. }
  109. /**
  110. * get_batt_uV - Get the voltage level of the battery
  111. * @cm: the Charger Manager representing the battery.
  112. * @uV: the voltage level returned.
  113. *
  114. * Returns 0 if there is no error.
  115. * Returns a negative value on error.
  116. */
  117. static int get_batt_uV(struct charger_manager *cm, int *uV)
  118. {
  119. union power_supply_propval val;
  120. int ret;
  121. if (!cm->fuel_gauge)
  122. return -ENODEV;
  123. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  124. POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
  125. if (ret)
  126. return ret;
  127. *uV = val.intval;
  128. return 0;
  129. }
  130. /**
  131. * is_charging - Returns true if the battery is being charged.
  132. * @cm: the Charger Manager representing the battery.
  133. */
  134. static bool is_charging(struct charger_manager *cm)
  135. {
  136. int i, ret;
  137. bool charging = false;
  138. union power_supply_propval val;
  139. /* If there is no battery, it cannot be charged */
  140. if (!is_batt_present(cm))
  141. return false;
  142. /* If at least one of the charger is charging, return yes */
  143. for (i = 0; cm->charger_stat[i]; i++) {
  144. /* 1. The charger sholuld not be DISABLED */
  145. if (cm->emergency_stop)
  146. continue;
  147. if (!cm->charger_enabled)
  148. continue;
  149. /* 2. The charger should be online (ext-power) */
  150. ret = cm->charger_stat[i]->get_property(
  151. cm->charger_stat[i],
  152. POWER_SUPPLY_PROP_ONLINE, &val);
  153. if (ret) {
  154. dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
  155. cm->desc->psy_charger_stat[i]);
  156. continue;
  157. }
  158. if (val.intval == 0)
  159. continue;
  160. /*
  161. * 3. The charger should not be FULL, DISCHARGING,
  162. * or NOT_CHARGING.
  163. */
  164. ret = cm->charger_stat[i]->get_property(
  165. cm->charger_stat[i],
  166. POWER_SUPPLY_PROP_STATUS, &val);
  167. if (ret) {
  168. dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
  169. cm->desc->psy_charger_stat[i]);
  170. continue;
  171. }
  172. if (val.intval == POWER_SUPPLY_STATUS_FULL ||
  173. val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
  174. val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
  175. continue;
  176. /* Then, this is charging. */
  177. charging = true;
  178. break;
  179. }
  180. return charging;
  181. }
  182. /**
  183. * is_polling_required - Return true if need to continue polling for this CM.
  184. * @cm: the Charger Manager representing the battery.
  185. */
  186. static bool is_polling_required(struct charger_manager *cm)
  187. {
  188. switch (cm->desc->polling_mode) {
  189. case CM_POLL_DISABLE:
  190. return false;
  191. case CM_POLL_ALWAYS:
  192. return true;
  193. case CM_POLL_EXTERNAL_POWER_ONLY:
  194. return is_ext_pwr_online(cm);
  195. case CM_POLL_CHARGING_ONLY:
  196. return is_charging(cm);
  197. default:
  198. dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
  199. cm->desc->polling_mode);
  200. }
  201. return false;
  202. }
  203. /**
  204. * try_charger_enable - Enable/Disable chargers altogether
  205. * @cm: the Charger Manager representing the battery.
  206. * @enable: true: enable / false: disable
  207. *
  208. * Note that Charger Manager keeps the charger enabled regardless whether
  209. * the charger is charging or not (because battery is full or no external
  210. * power source exists) except when CM needs to disable chargers forcibly
  211. * bacause of emergency causes; when the battery is overheated or too cold.
  212. */
  213. static int try_charger_enable(struct charger_manager *cm, bool enable)
  214. {
  215. int err = 0, i;
  216. struct charger_desc *desc = cm->desc;
  217. /* Ignore if it's redundent command */
  218. if (enable == cm->charger_enabled)
  219. return 0;
  220. if (enable) {
  221. if (cm->emergency_stop)
  222. return -EAGAIN;
  223. err = regulator_bulk_enable(desc->num_charger_regulators,
  224. desc->charger_regulators);
  225. } else {
  226. /*
  227. * Abnormal battery state - Stop charging forcibly,
  228. * even if charger was enabled at the other places
  229. */
  230. err = regulator_bulk_disable(desc->num_charger_regulators,
  231. desc->charger_regulators);
  232. for (i = 0; i < desc->num_charger_regulators; i++) {
  233. if (regulator_is_enabled(
  234. desc->charger_regulators[i].consumer)) {
  235. regulator_force_disable(
  236. desc->charger_regulators[i].consumer);
  237. dev_warn(cm->dev,
  238. "Disable regulator(%s) forcibly.\n",
  239. desc->charger_regulators[i].supply);
  240. }
  241. }
  242. }
  243. if (!err)
  244. cm->charger_enabled = enable;
  245. return err;
  246. }
  247. /**
  248. * uevent_notify - Let users know something has changed.
  249. * @cm: the Charger Manager representing the battery.
  250. * @event: the event string.
  251. *
  252. * If @event is null, it implies that uevent_notify is called
  253. * by resume function. When called in the resume function, cm_suspended
  254. * should be already reset to false in order to let uevent_notify
  255. * notify the recent event during the suspend to users. While
  256. * suspended, uevent_notify does not notify users, but tracks
  257. * events so that uevent_notify can notify users later after resumed.
  258. */
  259. static void uevent_notify(struct charger_manager *cm, const char *event)
  260. {
  261. static char env_str[UEVENT_BUF_SIZE + 1] = "";
  262. static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
  263. if (cm_suspended) {
  264. /* Nothing in suspended-event buffer */
  265. if (env_str_save[0] == 0) {
  266. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  267. return; /* status not changed */
  268. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  269. return;
  270. }
  271. if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
  272. return; /* Duplicated. */
  273. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  274. return;
  275. }
  276. if (event == NULL) {
  277. /* No messages pending */
  278. if (!env_str_save[0])
  279. return;
  280. strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
  281. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  282. env_str_save[0] = 0;
  283. return;
  284. }
  285. /* status not changed */
  286. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  287. return;
  288. /* save the status and notify the update */
  289. strncpy(env_str, event, UEVENT_BUF_SIZE);
  290. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  291. dev_info(cm->dev, "%s", event);
  292. }
  293. /**
  294. * _cm_monitor - Monitor the temperature and return true for exceptions.
  295. * @cm: the Charger Manager representing the battery.
  296. *
  297. * Returns true if there is an event to notify for the battery.
  298. * (True if the status of "emergency_stop" changes)
  299. */
  300. static bool _cm_monitor(struct charger_manager *cm)
  301. {
  302. struct charger_desc *desc = cm->desc;
  303. int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
  304. dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
  305. cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
  306. /* It has been stopped or charging already */
  307. if (!!temp == !!cm->emergency_stop)
  308. return false;
  309. if (temp) {
  310. cm->emergency_stop = temp;
  311. if (!try_charger_enable(cm, false)) {
  312. if (temp > 0)
  313. uevent_notify(cm, "OVERHEAT");
  314. else
  315. uevent_notify(cm, "COLD");
  316. }
  317. } else {
  318. cm->emergency_stop = 0;
  319. if (!try_charger_enable(cm, true))
  320. uevent_notify(cm, "CHARGING");
  321. }
  322. return true;
  323. }
  324. /**
  325. * cm_monitor - Monitor every battery.
  326. *
  327. * Returns true if there is an event to notify from any of the batteries.
  328. * (True if the status of "emergency_stop" changes)
  329. */
  330. static bool cm_monitor(void)
  331. {
  332. bool stop = false;
  333. struct charger_manager *cm;
  334. mutex_lock(&cm_list_mtx);
  335. list_for_each_entry(cm, &cm_list, entry) {
  336. if (_cm_monitor(cm))
  337. stop = true;
  338. }
  339. mutex_unlock(&cm_list_mtx);
  340. return stop;
  341. }
  342. static int charger_get_property(struct power_supply *psy,
  343. enum power_supply_property psp,
  344. union power_supply_propval *val)
  345. {
  346. struct charger_manager *cm = container_of(psy,
  347. struct charger_manager, charger_psy);
  348. struct charger_desc *desc = cm->desc;
  349. int ret = 0;
  350. int uV;
  351. switch (psp) {
  352. case POWER_SUPPLY_PROP_STATUS:
  353. if (is_charging(cm))
  354. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  355. else if (is_ext_pwr_online(cm))
  356. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  357. else
  358. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  359. break;
  360. case POWER_SUPPLY_PROP_HEALTH:
  361. if (cm->emergency_stop > 0)
  362. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  363. else if (cm->emergency_stop < 0)
  364. val->intval = POWER_SUPPLY_HEALTH_COLD;
  365. else
  366. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  367. break;
  368. case POWER_SUPPLY_PROP_PRESENT:
  369. if (is_batt_present(cm))
  370. val->intval = 1;
  371. else
  372. val->intval = 0;
  373. break;
  374. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  375. ret = get_batt_uV(cm, &val->intval);
  376. break;
  377. case POWER_SUPPLY_PROP_CURRENT_NOW:
  378. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  379. POWER_SUPPLY_PROP_CURRENT_NOW, val);
  380. break;
  381. case POWER_SUPPLY_PROP_TEMP:
  382. /* in thenth of centigrade */
  383. if (cm->last_temp_mC == INT_MIN)
  384. desc->temperature_out_of_range(&cm->last_temp_mC);
  385. val->intval = cm->last_temp_mC / 100;
  386. if (!desc->measure_battery_temp)
  387. ret = -ENODEV;
  388. break;
  389. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  390. /* in thenth of centigrade */
  391. if (cm->last_temp_mC == INT_MIN)
  392. desc->temperature_out_of_range(&cm->last_temp_mC);
  393. val->intval = cm->last_temp_mC / 100;
  394. if (desc->measure_battery_temp)
  395. ret = -ENODEV;
  396. break;
  397. case POWER_SUPPLY_PROP_CAPACITY:
  398. if (!cm->fuel_gauge) {
  399. ret = -ENODEV;
  400. break;
  401. }
  402. if (!is_batt_present(cm)) {
  403. /* There is no battery. Assume 100% */
  404. val->intval = 100;
  405. break;
  406. }
  407. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  408. POWER_SUPPLY_PROP_CAPACITY, val);
  409. if (ret)
  410. break;
  411. if (val->intval > 100) {
  412. val->intval = 100;
  413. break;
  414. }
  415. if (val->intval < 0)
  416. val->intval = 0;
  417. /* Do not adjust SOC when charging: voltage is overrated */
  418. if (is_charging(cm))
  419. break;
  420. /*
  421. * If the capacity value is inconsistent, calibrate it base on
  422. * the battery voltage values and the thresholds given as desc
  423. */
  424. ret = get_batt_uV(cm, &uV);
  425. if (ret) {
  426. /* Voltage information not available. No calibration */
  427. ret = 0;
  428. break;
  429. }
  430. if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  431. !is_charging(cm)) {
  432. val->intval = 100;
  433. break;
  434. }
  435. break;
  436. case POWER_SUPPLY_PROP_ONLINE:
  437. if (is_ext_pwr_online(cm))
  438. val->intval = 1;
  439. else
  440. val->intval = 0;
  441. break;
  442. case POWER_SUPPLY_PROP_CHARGE_FULL:
  443. if (cm->fuel_gauge) {
  444. if (cm->fuel_gauge->get_property(cm->fuel_gauge,
  445. POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
  446. break;
  447. }
  448. if (is_ext_pwr_online(cm)) {
  449. /* Not full if it's charging. */
  450. if (is_charging(cm)) {
  451. val->intval = 0;
  452. break;
  453. }
  454. /*
  455. * Full if it's powered but not charging andi
  456. * not forced stop by emergency
  457. */
  458. if (!cm->emergency_stop) {
  459. val->intval = 1;
  460. break;
  461. }
  462. }
  463. /* Full if it's over the fullbatt voltage */
  464. ret = get_batt_uV(cm, &uV);
  465. if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  466. !is_charging(cm)) {
  467. val->intval = 1;
  468. break;
  469. }
  470. /* Full if the cap is 100 */
  471. if (cm->fuel_gauge) {
  472. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  473. POWER_SUPPLY_PROP_CAPACITY, val);
  474. if (!ret && val->intval >= 100 && !is_charging(cm)) {
  475. val->intval = 1;
  476. break;
  477. }
  478. }
  479. val->intval = 0;
  480. ret = 0;
  481. break;
  482. case POWER_SUPPLY_PROP_CHARGE_NOW:
  483. if (is_charging(cm)) {
  484. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  485. POWER_SUPPLY_PROP_CHARGE_NOW,
  486. val);
  487. if (ret) {
  488. val->intval = 1;
  489. ret = 0;
  490. } else {
  491. /* If CHARGE_NOW is supplied, use it */
  492. val->intval = (val->intval > 0) ?
  493. val->intval : 1;
  494. }
  495. } else {
  496. val->intval = 0;
  497. }
  498. break;
  499. default:
  500. return -EINVAL;
  501. }
  502. return ret;
  503. }
  504. #define NUM_CHARGER_PSY_OPTIONAL (4)
  505. static enum power_supply_property default_charger_props[] = {
  506. /* Guaranteed to provide */
  507. POWER_SUPPLY_PROP_STATUS,
  508. POWER_SUPPLY_PROP_HEALTH,
  509. POWER_SUPPLY_PROP_PRESENT,
  510. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  511. POWER_SUPPLY_PROP_CAPACITY,
  512. POWER_SUPPLY_PROP_ONLINE,
  513. POWER_SUPPLY_PROP_CHARGE_FULL,
  514. /*
  515. * Optional properties are:
  516. * POWER_SUPPLY_PROP_CHARGE_NOW,
  517. * POWER_SUPPLY_PROP_CURRENT_NOW,
  518. * POWER_SUPPLY_PROP_TEMP, and
  519. * POWER_SUPPLY_PROP_TEMP_AMBIENT,
  520. */
  521. };
  522. static struct power_supply psy_default = {
  523. .name = "battery",
  524. .type = POWER_SUPPLY_TYPE_BATTERY,
  525. .properties = default_charger_props,
  526. .num_properties = ARRAY_SIZE(default_charger_props),
  527. .get_property = charger_get_property,
  528. };
  529. /**
  530. * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
  531. * for suspend_again.
  532. *
  533. * Returns true if the alarm is set for Charger Manager to use.
  534. * Returns false if
  535. * cm_setup_timer fails to set an alarm,
  536. * cm_setup_timer does not need to set an alarm for Charger Manager,
  537. * or an alarm previously configured is to be used.
  538. */
  539. static bool cm_setup_timer(void)
  540. {
  541. struct charger_manager *cm;
  542. unsigned int wakeup_ms = UINT_MAX;
  543. bool ret = false;
  544. mutex_lock(&cm_list_mtx);
  545. list_for_each_entry(cm, &cm_list, entry) {
  546. /* Skip if polling is not required for this CM */
  547. if (!is_polling_required(cm) && !cm->emergency_stop)
  548. continue;
  549. if (cm->desc->polling_interval_ms == 0)
  550. continue;
  551. CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
  552. }
  553. mutex_unlock(&cm_list_mtx);
  554. if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
  555. pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
  556. if (rtc_dev) {
  557. struct rtc_wkalrm tmp;
  558. unsigned long time, now;
  559. unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
  560. /*
  561. * Set alarm with the polling interval (wakeup_ms)
  562. * except when rtc_wkalarm_save comes first.
  563. * However, the alarm time should be NOW +
  564. * CM_RTC_SMALL or later.
  565. */
  566. tmp.enabled = 1;
  567. rtc_read_time(rtc_dev, &tmp.time);
  568. rtc_tm_to_time(&tmp.time, &now);
  569. if (add < CM_RTC_SMALL)
  570. add = CM_RTC_SMALL;
  571. time = now + add;
  572. ret = true;
  573. if (rtc_wkalarm_save.enabled &&
  574. rtc_wkalarm_save_time &&
  575. rtc_wkalarm_save_time < time) {
  576. if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
  577. time = now + CM_RTC_SMALL;
  578. else
  579. time = rtc_wkalarm_save_time;
  580. /* The timer is not appointed by CM */
  581. ret = false;
  582. }
  583. pr_info("Waking up after %lu secs.\n",
  584. time - now);
  585. rtc_time_to_tm(time, &tmp.time);
  586. rtc_set_alarm(rtc_dev, &tmp);
  587. cm_suspend_duration_ms += wakeup_ms;
  588. return ret;
  589. }
  590. }
  591. if (rtc_dev)
  592. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  593. return false;
  594. }
  595. /**
  596. * cm_suspend_again - Determine whether suspend again or not
  597. *
  598. * Returns true if the system should be suspended again
  599. * Returns false if the system should be woken up
  600. */
  601. bool cm_suspend_again(void)
  602. {
  603. struct charger_manager *cm;
  604. bool ret = false;
  605. if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
  606. !cm_rtc_set)
  607. return false;
  608. if (cm_monitor())
  609. goto out;
  610. ret = true;
  611. mutex_lock(&cm_list_mtx);
  612. list_for_each_entry(cm, &cm_list, entry) {
  613. if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
  614. cm->status_save_batt != is_batt_present(cm)) {
  615. ret = false;
  616. break;
  617. }
  618. }
  619. mutex_unlock(&cm_list_mtx);
  620. cm_rtc_set = cm_setup_timer();
  621. out:
  622. /* It's about the time when the non-CM appointed timer goes off */
  623. if (rtc_wkalarm_save.enabled) {
  624. unsigned long now;
  625. struct rtc_time tmp;
  626. rtc_read_time(rtc_dev, &tmp);
  627. rtc_tm_to_time(&tmp, &now);
  628. if (rtc_wkalarm_save_time &&
  629. now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
  630. return false;
  631. }
  632. return ret;
  633. }
  634. EXPORT_SYMBOL_GPL(cm_suspend_again);
  635. /**
  636. * setup_charger_manager - initialize charger_global_desc data
  637. * @gd: pointer to instance of charger_global_desc
  638. */
  639. int setup_charger_manager(struct charger_global_desc *gd)
  640. {
  641. if (!gd)
  642. return -EINVAL;
  643. if (rtc_dev)
  644. rtc_class_close(rtc_dev);
  645. rtc_dev = NULL;
  646. g_desc = NULL;
  647. if (!gd->rtc_only_wakeup) {
  648. pr_err("The callback rtc_only_wakeup is not given.\n");
  649. return -EINVAL;
  650. }
  651. if (gd->rtc_name) {
  652. rtc_dev = rtc_class_open(gd->rtc_name);
  653. if (IS_ERR_OR_NULL(rtc_dev)) {
  654. rtc_dev = NULL;
  655. /* Retry at probe. RTC may be not registered yet */
  656. }
  657. } else {
  658. pr_warn("No wakeup timer is given for charger manager."
  659. "In-suspend monitoring won't work.\n");
  660. }
  661. g_desc = gd;
  662. return 0;
  663. }
  664. EXPORT_SYMBOL_GPL(setup_charger_manager);
  665. static int charger_manager_probe(struct platform_device *pdev)
  666. {
  667. struct charger_desc *desc = dev_get_platdata(&pdev->dev);
  668. struct charger_manager *cm;
  669. int ret = 0, i = 0;
  670. union power_supply_propval val;
  671. if (g_desc && !rtc_dev && g_desc->rtc_name) {
  672. rtc_dev = rtc_class_open(g_desc->rtc_name);
  673. if (IS_ERR_OR_NULL(rtc_dev)) {
  674. rtc_dev = NULL;
  675. dev_err(&pdev->dev, "Cannot get RTC %s.\n",
  676. g_desc->rtc_name);
  677. ret = -ENODEV;
  678. goto err_alloc;
  679. }
  680. }
  681. if (!desc) {
  682. dev_err(&pdev->dev, "No platform data (desc) found.\n");
  683. ret = -ENODEV;
  684. goto err_alloc;
  685. }
  686. cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
  687. if (!cm) {
  688. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  689. ret = -ENOMEM;
  690. goto err_alloc;
  691. }
  692. /* Basic Values. Unspecified are Null or 0 */
  693. cm->dev = &pdev->dev;
  694. cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
  695. if (!cm->desc) {
  696. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  697. ret = -ENOMEM;
  698. goto err_alloc_desc;
  699. }
  700. memcpy(cm->desc, desc, sizeof(struct charger_desc));
  701. cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
  702. if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
  703. ret = -EINVAL;
  704. dev_err(&pdev->dev, "charger_regulators undefined.\n");
  705. goto err_no_charger;
  706. }
  707. if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
  708. dev_err(&pdev->dev, "No power supply defined.\n");
  709. ret = -EINVAL;
  710. goto err_no_charger_stat;
  711. }
  712. if (!desc->psy_fuel_gauge) {
  713. dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
  714. return -EINVAL;
  715. }
  716. /* Counting index only */
  717. while (desc->psy_charger_stat[i])
  718. i++;
  719. cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
  720. GFP_KERNEL);
  721. if (!cm->charger_stat) {
  722. ret = -ENOMEM;
  723. goto err_no_charger_stat;
  724. }
  725. for (i = 0; desc->psy_charger_stat[i]; i++) {
  726. cm->charger_stat[i] = power_supply_get_by_name(
  727. desc->psy_charger_stat[i]);
  728. if (!cm->charger_stat[i]) {
  729. dev_err(&pdev->dev, "Cannot find power supply "
  730. "\"%s\"\n",
  731. desc->psy_charger_stat[i]);
  732. ret = -ENODEV;
  733. goto err_chg_stat;
  734. }
  735. }
  736. cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
  737. if (!cm->fuel_gauge) {
  738. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  739. desc->psy_fuel_gauge);
  740. ret = -ENODEV;
  741. goto err_chg_stat;
  742. }
  743. if (desc->polling_interval_ms == 0 ||
  744. msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
  745. dev_err(&pdev->dev, "polling_interval_ms is too small\n");
  746. ret = -EINVAL;
  747. goto err_chg_stat;
  748. }
  749. if (!desc->temperature_out_of_range) {
  750. dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
  751. ret = -EINVAL;
  752. goto err_chg_stat;
  753. }
  754. platform_set_drvdata(pdev, cm);
  755. memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
  756. if (!desc->psy_name) {
  757. strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
  758. } else {
  759. strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
  760. }
  761. cm->charger_psy.name = cm->psy_name_buf;
  762. /* Allocate for psy properties because they may vary */
  763. cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
  764. * (ARRAY_SIZE(default_charger_props) +
  765. NUM_CHARGER_PSY_OPTIONAL),
  766. GFP_KERNEL);
  767. if (!cm->charger_psy.properties) {
  768. dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
  769. ret = -ENOMEM;
  770. goto err_chg_stat;
  771. }
  772. memcpy(cm->charger_psy.properties, default_charger_props,
  773. sizeof(enum power_supply_property) *
  774. ARRAY_SIZE(default_charger_props));
  775. cm->charger_psy.num_properties = psy_default.num_properties;
  776. /* Find which optional psy-properties are available */
  777. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  778. POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
  779. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  780. POWER_SUPPLY_PROP_CHARGE_NOW;
  781. cm->charger_psy.num_properties++;
  782. }
  783. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  784. POWER_SUPPLY_PROP_CURRENT_NOW,
  785. &val)) {
  786. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  787. POWER_SUPPLY_PROP_CURRENT_NOW;
  788. cm->charger_psy.num_properties++;
  789. }
  790. if (desc->measure_battery_temp) {
  791. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  792. POWER_SUPPLY_PROP_TEMP;
  793. cm->charger_psy.num_properties++;
  794. } else {
  795. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  796. POWER_SUPPLY_PROP_TEMP_AMBIENT;
  797. cm->charger_psy.num_properties++;
  798. }
  799. ret = power_supply_register(NULL, &cm->charger_psy);
  800. if (ret) {
  801. dev_err(&pdev->dev, "Cannot register charger-manager with"
  802. " name \"%s\".\n", cm->charger_psy.name);
  803. goto err_register;
  804. }
  805. ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
  806. desc->charger_regulators);
  807. if (ret) {
  808. dev_err(&pdev->dev, "Cannot get charger regulators.\n");
  809. goto err_bulk_get;
  810. }
  811. ret = try_charger_enable(cm, true);
  812. if (ret) {
  813. dev_err(&pdev->dev, "Cannot enable charger regulators\n");
  814. goto err_chg_enable;
  815. }
  816. /* Add to the list */
  817. mutex_lock(&cm_list_mtx);
  818. list_add(&cm->entry, &cm_list);
  819. mutex_unlock(&cm_list_mtx);
  820. return 0;
  821. err_chg_enable:
  822. regulator_bulk_free(desc->num_charger_regulators,
  823. desc->charger_regulators);
  824. err_bulk_get:
  825. power_supply_unregister(&cm->charger_psy);
  826. err_register:
  827. kfree(cm->charger_psy.properties);
  828. err_chg_stat:
  829. kfree(cm->charger_stat);
  830. err_no_charger_stat:
  831. err_no_charger:
  832. kfree(cm->desc);
  833. err_alloc_desc:
  834. kfree(cm);
  835. err_alloc:
  836. return ret;
  837. }
  838. static int __devexit charger_manager_remove(struct platform_device *pdev)
  839. {
  840. struct charger_manager *cm = platform_get_drvdata(pdev);
  841. struct charger_desc *desc = cm->desc;
  842. /* Remove from the list */
  843. mutex_lock(&cm_list_mtx);
  844. list_del(&cm->entry);
  845. mutex_unlock(&cm_list_mtx);
  846. regulator_bulk_free(desc->num_charger_regulators,
  847. desc->charger_regulators);
  848. power_supply_unregister(&cm->charger_psy);
  849. kfree(cm->charger_psy.properties);
  850. kfree(cm->charger_stat);
  851. kfree(cm->desc);
  852. kfree(cm);
  853. return 0;
  854. }
  855. static const struct platform_device_id charger_manager_id[] = {
  856. { "charger-manager", 0 },
  857. { },
  858. };
  859. MODULE_DEVICE_TABLE(platform, charger_manager_id);
  860. static int cm_suspend_prepare(struct device *dev)
  861. {
  862. struct charger_manager *cm = dev_get_drvdata(dev);
  863. if (!cm_suspended) {
  864. if (rtc_dev) {
  865. struct rtc_time tmp;
  866. unsigned long now;
  867. rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
  868. rtc_read_time(rtc_dev, &tmp);
  869. if (rtc_wkalarm_save.enabled) {
  870. rtc_tm_to_time(&rtc_wkalarm_save.time,
  871. &rtc_wkalarm_save_time);
  872. rtc_tm_to_time(&tmp, &now);
  873. if (now > rtc_wkalarm_save_time)
  874. rtc_wkalarm_save_time = 0;
  875. } else {
  876. rtc_wkalarm_save_time = 0;
  877. }
  878. }
  879. cm_suspended = true;
  880. }
  881. cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
  882. cm->status_save_batt = is_batt_present(cm);
  883. if (!cm_rtc_set) {
  884. cm_suspend_duration_ms = 0;
  885. cm_rtc_set = cm_setup_timer();
  886. }
  887. return 0;
  888. }
  889. static void cm_suspend_complete(struct device *dev)
  890. {
  891. struct charger_manager *cm = dev_get_drvdata(dev);
  892. if (cm_suspended) {
  893. if (rtc_dev) {
  894. struct rtc_wkalrm tmp;
  895. rtc_read_alarm(rtc_dev, &tmp);
  896. rtc_wkalarm_save.pending = tmp.pending;
  897. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  898. }
  899. cm_suspended = false;
  900. cm_rtc_set = false;
  901. }
  902. uevent_notify(cm, NULL);
  903. }
  904. static const struct dev_pm_ops charger_manager_pm = {
  905. .prepare = cm_suspend_prepare,
  906. .complete = cm_suspend_complete,
  907. };
  908. static struct platform_driver charger_manager_driver = {
  909. .driver = {
  910. .name = "charger-manager",
  911. .owner = THIS_MODULE,
  912. .pm = &charger_manager_pm,
  913. },
  914. .probe = charger_manager_probe,
  915. .remove = __devexit_p(charger_manager_remove),
  916. .id_table = charger_manager_id,
  917. };
  918. static int __init charger_manager_init(void)
  919. {
  920. return platform_driver_register(&charger_manager_driver);
  921. }
  922. late_initcall(charger_manager_init);
  923. static void __exit charger_manager_cleanup(void)
  924. {
  925. platform_driver_unregister(&charger_manager_driver);
  926. }
  927. module_exit(charger_manager_cleanup);
  928. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  929. MODULE_DESCRIPTION("Charger Manager");
  930. MODULE_LICENSE("GPL");