charger-manager.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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. for (i = 0 ; i < desc->num_charger_regulators ; i++)
  243. regulator_enable(desc->charger_regulators[i].consumer);
  244. } else {
  245. /*
  246. * Abnormal battery state - Stop charging forcibly,
  247. * even if charger was enabled at the other places
  248. */
  249. for (i = 0; i < desc->num_charger_regulators; i++) {
  250. if (regulator_is_enabled(
  251. desc->charger_regulators[i].consumer)) {
  252. regulator_force_disable(
  253. desc->charger_regulators[i].consumer);
  254. dev_warn(cm->dev,
  255. "Disable regulator(%s) forcibly.\n",
  256. desc->charger_regulators[i].regulator_name);
  257. }
  258. }
  259. }
  260. if (!err)
  261. cm->charger_enabled = enable;
  262. return err;
  263. }
  264. /**
  265. * try_charger_restart - Restart charging.
  266. * @cm: the Charger Manager representing the battery.
  267. *
  268. * Restart charging by turning off and on the charger.
  269. */
  270. static int try_charger_restart(struct charger_manager *cm)
  271. {
  272. int err;
  273. if (cm->emergency_stop)
  274. return -EAGAIN;
  275. err = try_charger_enable(cm, false);
  276. if (err)
  277. return err;
  278. return try_charger_enable(cm, true);
  279. }
  280. /**
  281. * uevent_notify - Let users know something has changed.
  282. * @cm: the Charger Manager representing the battery.
  283. * @event: the event string.
  284. *
  285. * If @event is null, it implies that uevent_notify is called
  286. * by resume function. When called in the resume function, cm_suspended
  287. * should be already reset to false in order to let uevent_notify
  288. * notify the recent event during the suspend to users. While
  289. * suspended, uevent_notify does not notify users, but tracks
  290. * events so that uevent_notify can notify users later after resumed.
  291. */
  292. static void uevent_notify(struct charger_manager *cm, const char *event)
  293. {
  294. static char env_str[UEVENT_BUF_SIZE + 1] = "";
  295. static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
  296. if (cm_suspended) {
  297. /* Nothing in suspended-event buffer */
  298. if (env_str_save[0] == 0) {
  299. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  300. return; /* status not changed */
  301. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  302. return;
  303. }
  304. if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
  305. return; /* Duplicated. */
  306. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  307. return;
  308. }
  309. if (event == NULL) {
  310. /* No messages pending */
  311. if (!env_str_save[0])
  312. return;
  313. strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
  314. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  315. env_str_save[0] = 0;
  316. return;
  317. }
  318. /* status not changed */
  319. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  320. return;
  321. /* save the status and notify the update */
  322. strncpy(env_str, event, UEVENT_BUF_SIZE);
  323. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  324. dev_info(cm->dev, event);
  325. }
  326. /**
  327. * fullbatt_vchk - Check voltage drop some times after "FULL" event.
  328. * @work: the work_struct appointing the function
  329. *
  330. * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
  331. * charger_desc, Charger Manager checks voltage drop after the battery
  332. * "FULL" event. It checks whether the voltage has dropped more than
  333. * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
  334. */
  335. static void fullbatt_vchk(struct work_struct *work)
  336. {
  337. struct delayed_work *dwork = to_delayed_work(work);
  338. struct charger_manager *cm = container_of(dwork,
  339. struct charger_manager, fullbatt_vchk_work);
  340. struct charger_desc *desc = cm->desc;
  341. int batt_uV, err, diff;
  342. /* remove the appointment for fullbatt_vchk */
  343. cm->fullbatt_vchk_jiffies_at = 0;
  344. if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
  345. return;
  346. err = get_batt_uV(cm, &batt_uV);
  347. if (err) {
  348. dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
  349. return;
  350. }
  351. diff = cm->fullbatt_vchk_uV;
  352. diff -= batt_uV;
  353. dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
  354. if (diff > desc->fullbatt_vchkdrop_uV) {
  355. try_charger_restart(cm);
  356. uevent_notify(cm, "Recharge");
  357. }
  358. }
  359. /**
  360. * _cm_monitor - Monitor the temperature and return true for exceptions.
  361. * @cm: the Charger Manager representing the battery.
  362. *
  363. * Returns true if there is an event to notify for the battery.
  364. * (True if the status of "emergency_stop" changes)
  365. */
  366. static bool _cm_monitor(struct charger_manager *cm)
  367. {
  368. struct charger_desc *desc = cm->desc;
  369. int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
  370. dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
  371. cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
  372. /* It has been stopped or charging already */
  373. if (!!temp == !!cm->emergency_stop)
  374. return false;
  375. if (temp) {
  376. cm->emergency_stop = temp;
  377. if (!try_charger_enable(cm, false)) {
  378. if (temp > 0)
  379. uevent_notify(cm, "OVERHEAT");
  380. else
  381. uevent_notify(cm, "COLD");
  382. }
  383. } else {
  384. cm->emergency_stop = 0;
  385. if (!try_charger_enable(cm, true))
  386. uevent_notify(cm, "CHARGING");
  387. }
  388. return true;
  389. }
  390. /**
  391. * cm_monitor - Monitor every battery.
  392. *
  393. * Returns true if there is an event to notify from any of the batteries.
  394. * (True if the status of "emergency_stop" changes)
  395. */
  396. static bool cm_monitor(void)
  397. {
  398. bool stop = false;
  399. struct charger_manager *cm;
  400. mutex_lock(&cm_list_mtx);
  401. list_for_each_entry(cm, &cm_list, entry) {
  402. if (_cm_monitor(cm))
  403. stop = true;
  404. }
  405. mutex_unlock(&cm_list_mtx);
  406. return stop;
  407. }
  408. /**
  409. * _setup_polling - Setup the next instance of polling.
  410. * @work: work_struct of the function _setup_polling.
  411. */
  412. static void _setup_polling(struct work_struct *work)
  413. {
  414. unsigned long min = ULONG_MAX;
  415. struct charger_manager *cm;
  416. bool keep_polling = false;
  417. unsigned long _next_polling;
  418. mutex_lock(&cm_list_mtx);
  419. list_for_each_entry(cm, &cm_list, entry) {
  420. if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
  421. keep_polling = true;
  422. if (min > cm->desc->polling_interval_ms)
  423. min = cm->desc->polling_interval_ms;
  424. }
  425. }
  426. polling_jiffy = msecs_to_jiffies(min);
  427. if (polling_jiffy <= CM_JIFFIES_SMALL)
  428. polling_jiffy = CM_JIFFIES_SMALL + 1;
  429. if (!keep_polling)
  430. polling_jiffy = ULONG_MAX;
  431. if (polling_jiffy == ULONG_MAX)
  432. goto out;
  433. WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
  434. ". try it later. %s\n", __func__);
  435. _next_polling = jiffies + polling_jiffy;
  436. if (!delayed_work_pending(&cm_monitor_work) ||
  437. (delayed_work_pending(&cm_monitor_work) &&
  438. time_after(next_polling, _next_polling))) {
  439. next_polling = jiffies + polling_jiffy;
  440. mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
  441. }
  442. out:
  443. mutex_unlock(&cm_list_mtx);
  444. }
  445. static DECLARE_WORK(setup_polling, _setup_polling);
  446. /**
  447. * cm_monitor_poller - The Monitor / Poller.
  448. * @work: work_struct of the function cm_monitor_poller
  449. *
  450. * During non-suspended state, cm_monitor_poller is used to poll and monitor
  451. * the batteries.
  452. */
  453. static void cm_monitor_poller(struct work_struct *work)
  454. {
  455. cm_monitor();
  456. schedule_work(&setup_polling);
  457. }
  458. /**
  459. * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
  460. * @cm: the Charger Manager representing the battery.
  461. */
  462. static void fullbatt_handler(struct charger_manager *cm)
  463. {
  464. struct charger_desc *desc = cm->desc;
  465. if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
  466. goto out;
  467. if (cm_suspended)
  468. device_set_wakeup_capable(cm->dev, true);
  469. mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  470. msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
  471. cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
  472. desc->fullbatt_vchkdrop_ms);
  473. if (cm->fullbatt_vchk_jiffies_at == 0)
  474. cm->fullbatt_vchk_jiffies_at = 1;
  475. out:
  476. dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
  477. uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
  478. }
  479. /**
  480. * battout_handler - Event handler for CM_EVENT_BATT_OUT
  481. * @cm: the Charger Manager representing the battery.
  482. */
  483. static void battout_handler(struct charger_manager *cm)
  484. {
  485. if (cm_suspended)
  486. device_set_wakeup_capable(cm->dev, true);
  487. if (!is_batt_present(cm)) {
  488. dev_emerg(cm->dev, "Battery Pulled Out!\n");
  489. uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
  490. } else {
  491. uevent_notify(cm, "Battery Reinserted?");
  492. }
  493. }
  494. /**
  495. * misc_event_handler - Handler for other evnets
  496. * @cm: the Charger Manager representing the battery.
  497. * @type: the Charger Manager representing the battery.
  498. */
  499. static void misc_event_handler(struct charger_manager *cm,
  500. enum cm_event_types type)
  501. {
  502. if (cm_suspended)
  503. device_set_wakeup_capable(cm->dev, true);
  504. if (!delayed_work_pending(&cm_monitor_work) &&
  505. is_polling_required(cm) && cm->desc->polling_interval_ms)
  506. schedule_work(&setup_polling);
  507. uevent_notify(cm, default_event_names[type]);
  508. }
  509. static int charger_get_property(struct power_supply *psy,
  510. enum power_supply_property psp,
  511. union power_supply_propval *val)
  512. {
  513. struct charger_manager *cm = container_of(psy,
  514. struct charger_manager, charger_psy);
  515. struct charger_desc *desc = cm->desc;
  516. int ret = 0;
  517. int uV;
  518. switch (psp) {
  519. case POWER_SUPPLY_PROP_STATUS:
  520. if (is_charging(cm))
  521. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  522. else if (is_ext_pwr_online(cm))
  523. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  524. else
  525. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  526. break;
  527. case POWER_SUPPLY_PROP_HEALTH:
  528. if (cm->emergency_stop > 0)
  529. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  530. else if (cm->emergency_stop < 0)
  531. val->intval = POWER_SUPPLY_HEALTH_COLD;
  532. else
  533. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  534. break;
  535. case POWER_SUPPLY_PROP_PRESENT:
  536. if (is_batt_present(cm))
  537. val->intval = 1;
  538. else
  539. val->intval = 0;
  540. break;
  541. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  542. ret = get_batt_uV(cm, &val->intval);
  543. break;
  544. case POWER_SUPPLY_PROP_CURRENT_NOW:
  545. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  546. POWER_SUPPLY_PROP_CURRENT_NOW, val);
  547. break;
  548. case POWER_SUPPLY_PROP_TEMP:
  549. /* in thenth of centigrade */
  550. if (cm->last_temp_mC == INT_MIN)
  551. desc->temperature_out_of_range(&cm->last_temp_mC);
  552. val->intval = cm->last_temp_mC / 100;
  553. if (!desc->measure_battery_temp)
  554. ret = -ENODEV;
  555. break;
  556. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  557. /* in thenth of centigrade */
  558. if (cm->last_temp_mC == INT_MIN)
  559. desc->temperature_out_of_range(&cm->last_temp_mC);
  560. val->intval = cm->last_temp_mC / 100;
  561. if (desc->measure_battery_temp)
  562. ret = -ENODEV;
  563. break;
  564. case POWER_SUPPLY_PROP_CAPACITY:
  565. if (!cm->fuel_gauge) {
  566. ret = -ENODEV;
  567. break;
  568. }
  569. if (!is_batt_present(cm)) {
  570. /* There is no battery. Assume 100% */
  571. val->intval = 100;
  572. break;
  573. }
  574. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  575. POWER_SUPPLY_PROP_CAPACITY, val);
  576. if (ret)
  577. break;
  578. if (val->intval > 100) {
  579. val->intval = 100;
  580. break;
  581. }
  582. if (val->intval < 0)
  583. val->intval = 0;
  584. /* Do not adjust SOC when charging: voltage is overrated */
  585. if (is_charging(cm))
  586. break;
  587. /*
  588. * If the capacity value is inconsistent, calibrate it base on
  589. * the battery voltage values and the thresholds given as desc
  590. */
  591. ret = get_batt_uV(cm, &uV);
  592. if (ret) {
  593. /* Voltage information not available. No calibration */
  594. ret = 0;
  595. break;
  596. }
  597. if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  598. !is_charging(cm)) {
  599. val->intval = 100;
  600. break;
  601. }
  602. break;
  603. case POWER_SUPPLY_PROP_ONLINE:
  604. if (is_ext_pwr_online(cm))
  605. val->intval = 1;
  606. else
  607. val->intval = 0;
  608. break;
  609. case POWER_SUPPLY_PROP_CHARGE_FULL:
  610. if (cm->fuel_gauge) {
  611. if (cm->fuel_gauge->get_property(cm->fuel_gauge,
  612. POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
  613. break;
  614. }
  615. if (is_ext_pwr_online(cm)) {
  616. /* Not full if it's charging. */
  617. if (is_charging(cm)) {
  618. val->intval = 0;
  619. break;
  620. }
  621. /*
  622. * Full if it's powered but not charging andi
  623. * not forced stop by emergency
  624. */
  625. if (!cm->emergency_stop) {
  626. val->intval = 1;
  627. break;
  628. }
  629. }
  630. /* Full if it's over the fullbatt voltage */
  631. ret = get_batt_uV(cm, &uV);
  632. if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  633. !is_charging(cm)) {
  634. val->intval = 1;
  635. break;
  636. }
  637. /* Full if the cap is 100 */
  638. if (cm->fuel_gauge) {
  639. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  640. POWER_SUPPLY_PROP_CAPACITY, val);
  641. if (!ret && val->intval >= 100 && !is_charging(cm)) {
  642. val->intval = 1;
  643. break;
  644. }
  645. }
  646. val->intval = 0;
  647. ret = 0;
  648. break;
  649. case POWER_SUPPLY_PROP_CHARGE_NOW:
  650. if (is_charging(cm)) {
  651. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  652. POWER_SUPPLY_PROP_CHARGE_NOW,
  653. val);
  654. if (ret) {
  655. val->intval = 1;
  656. ret = 0;
  657. } else {
  658. /* If CHARGE_NOW is supplied, use it */
  659. val->intval = (val->intval > 0) ?
  660. val->intval : 1;
  661. }
  662. } else {
  663. val->intval = 0;
  664. }
  665. break;
  666. default:
  667. return -EINVAL;
  668. }
  669. return ret;
  670. }
  671. #define NUM_CHARGER_PSY_OPTIONAL (4)
  672. static enum power_supply_property default_charger_props[] = {
  673. /* Guaranteed to provide */
  674. POWER_SUPPLY_PROP_STATUS,
  675. POWER_SUPPLY_PROP_HEALTH,
  676. POWER_SUPPLY_PROP_PRESENT,
  677. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  678. POWER_SUPPLY_PROP_CAPACITY,
  679. POWER_SUPPLY_PROP_ONLINE,
  680. POWER_SUPPLY_PROP_CHARGE_FULL,
  681. /*
  682. * Optional properties are:
  683. * POWER_SUPPLY_PROP_CHARGE_NOW,
  684. * POWER_SUPPLY_PROP_CURRENT_NOW,
  685. * POWER_SUPPLY_PROP_TEMP, and
  686. * POWER_SUPPLY_PROP_TEMP_AMBIENT,
  687. */
  688. };
  689. static struct power_supply psy_default = {
  690. .name = "battery",
  691. .type = POWER_SUPPLY_TYPE_BATTERY,
  692. .properties = default_charger_props,
  693. .num_properties = ARRAY_SIZE(default_charger_props),
  694. .get_property = charger_get_property,
  695. };
  696. /**
  697. * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
  698. * for suspend_again.
  699. *
  700. * Returns true if the alarm is set for Charger Manager to use.
  701. * Returns false if
  702. * cm_setup_timer fails to set an alarm,
  703. * cm_setup_timer does not need to set an alarm for Charger Manager,
  704. * or an alarm previously configured is to be used.
  705. */
  706. static bool cm_setup_timer(void)
  707. {
  708. struct charger_manager *cm;
  709. unsigned int wakeup_ms = UINT_MAX;
  710. bool ret = false;
  711. mutex_lock(&cm_list_mtx);
  712. list_for_each_entry(cm, &cm_list, entry) {
  713. unsigned int fbchk_ms = 0;
  714. /* fullbatt_vchk is required. setup timer for that */
  715. if (cm->fullbatt_vchk_jiffies_at) {
  716. fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
  717. - jiffies);
  718. if (time_is_before_eq_jiffies(
  719. cm->fullbatt_vchk_jiffies_at) ||
  720. msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
  721. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  722. fbchk_ms = 0;
  723. }
  724. }
  725. CM_MIN_VALID(wakeup_ms, fbchk_ms);
  726. /* Skip if polling is not required for this CM */
  727. if (!is_polling_required(cm) && !cm->emergency_stop)
  728. continue;
  729. if (cm->desc->polling_interval_ms == 0)
  730. continue;
  731. CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
  732. }
  733. mutex_unlock(&cm_list_mtx);
  734. if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
  735. pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
  736. if (rtc_dev) {
  737. struct rtc_wkalrm tmp;
  738. unsigned long time, now;
  739. unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
  740. /*
  741. * Set alarm with the polling interval (wakeup_ms)
  742. * except when rtc_wkalarm_save comes first.
  743. * However, the alarm time should be NOW +
  744. * CM_RTC_SMALL or later.
  745. */
  746. tmp.enabled = 1;
  747. rtc_read_time(rtc_dev, &tmp.time);
  748. rtc_tm_to_time(&tmp.time, &now);
  749. if (add < CM_RTC_SMALL)
  750. add = CM_RTC_SMALL;
  751. time = now + add;
  752. ret = true;
  753. if (rtc_wkalarm_save.enabled &&
  754. rtc_wkalarm_save_time &&
  755. rtc_wkalarm_save_time < time) {
  756. if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
  757. time = now + CM_RTC_SMALL;
  758. else
  759. time = rtc_wkalarm_save_time;
  760. /* The timer is not appointed by CM */
  761. ret = false;
  762. }
  763. pr_info("Waking up after %lu secs.\n",
  764. time - now);
  765. rtc_time_to_tm(time, &tmp.time);
  766. rtc_set_alarm(rtc_dev, &tmp);
  767. cm_suspend_duration_ms += wakeup_ms;
  768. return ret;
  769. }
  770. }
  771. if (rtc_dev)
  772. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  773. return false;
  774. }
  775. static void _cm_fbchk_in_suspend(struct charger_manager *cm)
  776. {
  777. unsigned long jiffy_now = jiffies;
  778. if (!cm->fullbatt_vchk_jiffies_at)
  779. return;
  780. if (g_desc && g_desc->assume_timer_stops_in_suspend)
  781. jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
  782. /* Execute now if it's going to be executed not too long after */
  783. jiffy_now += CM_JIFFIES_SMALL;
  784. if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
  785. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  786. }
  787. /**
  788. * cm_suspend_again - Determine whether suspend again or not
  789. *
  790. * Returns true if the system should be suspended again
  791. * Returns false if the system should be woken up
  792. */
  793. bool cm_suspend_again(void)
  794. {
  795. struct charger_manager *cm;
  796. bool ret = false;
  797. if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
  798. !cm_rtc_set)
  799. return false;
  800. if (cm_monitor())
  801. goto out;
  802. ret = true;
  803. mutex_lock(&cm_list_mtx);
  804. list_for_each_entry(cm, &cm_list, entry) {
  805. _cm_fbchk_in_suspend(cm);
  806. if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
  807. cm->status_save_batt != is_batt_present(cm)) {
  808. ret = false;
  809. break;
  810. }
  811. }
  812. mutex_unlock(&cm_list_mtx);
  813. cm_rtc_set = cm_setup_timer();
  814. out:
  815. /* It's about the time when the non-CM appointed timer goes off */
  816. if (rtc_wkalarm_save.enabled) {
  817. unsigned long now;
  818. struct rtc_time tmp;
  819. rtc_read_time(rtc_dev, &tmp);
  820. rtc_tm_to_time(&tmp, &now);
  821. if (rtc_wkalarm_save_time &&
  822. now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
  823. return false;
  824. }
  825. return ret;
  826. }
  827. EXPORT_SYMBOL_GPL(cm_suspend_again);
  828. /**
  829. * setup_charger_manager - initialize charger_global_desc data
  830. * @gd: pointer to instance of charger_global_desc
  831. */
  832. int setup_charger_manager(struct charger_global_desc *gd)
  833. {
  834. if (!gd)
  835. return -EINVAL;
  836. if (rtc_dev)
  837. rtc_class_close(rtc_dev);
  838. rtc_dev = NULL;
  839. g_desc = NULL;
  840. if (!gd->rtc_only_wakeup) {
  841. pr_err("The callback rtc_only_wakeup is not given.\n");
  842. return -EINVAL;
  843. }
  844. if (gd->rtc_name) {
  845. rtc_dev = rtc_class_open(gd->rtc_name);
  846. if (IS_ERR_OR_NULL(rtc_dev)) {
  847. rtc_dev = NULL;
  848. /* Retry at probe. RTC may be not registered yet */
  849. }
  850. } else {
  851. pr_warn("No wakeup timer is given for charger manager."
  852. "In-suspend monitoring won't work.\n");
  853. }
  854. g_desc = gd;
  855. return 0;
  856. }
  857. EXPORT_SYMBOL_GPL(setup_charger_manager);
  858. /**
  859. * charger_extcon_work - enable/diable charger according to the state
  860. * of charger cable
  861. *
  862. * @work: work_struct of the function charger_extcon_work.
  863. */
  864. static void charger_extcon_work(struct work_struct *work)
  865. {
  866. struct charger_cable *cable =
  867. container_of(work, struct charger_cable, wq);
  868. int ret;
  869. if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
  870. ret = regulator_set_current_limit(cable->charger->consumer,
  871. cable->min_uA, cable->max_uA);
  872. if (ret < 0) {
  873. pr_err("Cannot set current limit of %s (%s)\n",
  874. cable->charger->regulator_name, cable->name);
  875. return;
  876. }
  877. pr_info("Set current limit of %s : %duA ~ %duA\n",
  878. cable->charger->regulator_name,
  879. cable->min_uA, cable->max_uA);
  880. }
  881. try_charger_enable(cable->cm, cable->attached);
  882. }
  883. /**
  884. * charger_extcon_notifier - receive the state of charger cable
  885. * when registered cable is attached or detached.
  886. *
  887. * @self: the notifier block of the charger_extcon_notifier.
  888. * @event: the cable state.
  889. * @ptr: the data pointer of notifier block.
  890. */
  891. static int charger_extcon_notifier(struct notifier_block *self,
  892. unsigned long event, void *ptr)
  893. {
  894. struct charger_cable *cable =
  895. container_of(self, struct charger_cable, nb);
  896. cable->attached = event;
  897. schedule_work(&cable->wq);
  898. return NOTIFY_DONE;
  899. }
  900. /**
  901. * charger_extcon_init - register external connector to use it
  902. * as the charger cable
  903. *
  904. * @cm: the Charger Manager representing the battery.
  905. * @cable: the Charger cable representing the external connector.
  906. */
  907. static int charger_extcon_init(struct charger_manager *cm,
  908. struct charger_cable *cable)
  909. {
  910. int ret = 0;
  911. /*
  912. * Charger manager use Extcon framework to identify
  913. * the charger cable among various external connector
  914. * cable (e.g., TA, USB, MHL, Dock).
  915. */
  916. INIT_WORK(&cable->wq, charger_extcon_work);
  917. cable->nb.notifier_call = charger_extcon_notifier;
  918. ret = extcon_register_interest(&cable->extcon_dev,
  919. cable->extcon_name, cable->name, &cable->nb);
  920. if (ret < 0) {
  921. pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
  922. cable->extcon_name,
  923. cable->name);
  924. ret = -EINVAL;
  925. }
  926. return ret;
  927. }
  928. static int charger_manager_probe(struct platform_device *pdev)
  929. {
  930. struct charger_desc *desc = dev_get_platdata(&pdev->dev);
  931. struct charger_manager *cm;
  932. int ret = 0, i = 0;
  933. int j = 0;
  934. union power_supply_propval val;
  935. if (g_desc && !rtc_dev && g_desc->rtc_name) {
  936. rtc_dev = rtc_class_open(g_desc->rtc_name);
  937. if (IS_ERR_OR_NULL(rtc_dev)) {
  938. rtc_dev = NULL;
  939. dev_err(&pdev->dev, "Cannot get RTC %s.\n",
  940. g_desc->rtc_name);
  941. ret = -ENODEV;
  942. goto err_alloc;
  943. }
  944. }
  945. if (!desc) {
  946. dev_err(&pdev->dev, "No platform data (desc) found.\n");
  947. ret = -ENODEV;
  948. goto err_alloc;
  949. }
  950. cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
  951. if (!cm) {
  952. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  953. ret = -ENOMEM;
  954. goto err_alloc;
  955. }
  956. /* Basic Values. Unspecified are Null or 0 */
  957. cm->dev = &pdev->dev;
  958. cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
  959. if (!cm->desc) {
  960. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  961. ret = -ENOMEM;
  962. goto err_alloc_desc;
  963. }
  964. memcpy(cm->desc, desc, sizeof(struct charger_desc));
  965. cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
  966. /*
  967. * The following two do not need to be errors.
  968. * Users may intentionally ignore those two features.
  969. */
  970. if (desc->fullbatt_uV == 0) {
  971. dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
  972. " as it is not supplied.");
  973. }
  974. if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
  975. dev_info(&pdev->dev, "Disabling full-battery voltage drop "
  976. "checking mechanism as it is not supplied.");
  977. desc->fullbatt_vchkdrop_ms = 0;
  978. desc->fullbatt_vchkdrop_uV = 0;
  979. }
  980. if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
  981. ret = -EINVAL;
  982. dev_err(&pdev->dev, "charger_regulators undefined.\n");
  983. goto err_no_charger;
  984. }
  985. if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
  986. dev_err(&pdev->dev, "No power supply defined.\n");
  987. ret = -EINVAL;
  988. goto err_no_charger_stat;
  989. }
  990. /* Counting index only */
  991. while (desc->psy_charger_stat[i])
  992. i++;
  993. cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
  994. GFP_KERNEL);
  995. if (!cm->charger_stat) {
  996. ret = -ENOMEM;
  997. goto err_no_charger_stat;
  998. }
  999. for (i = 0; desc->psy_charger_stat[i]; i++) {
  1000. cm->charger_stat[i] = power_supply_get_by_name(
  1001. desc->psy_charger_stat[i]);
  1002. if (!cm->charger_stat[i]) {
  1003. dev_err(&pdev->dev, "Cannot find power supply "
  1004. "\"%s\"\n",
  1005. desc->psy_charger_stat[i]);
  1006. ret = -ENODEV;
  1007. goto err_chg_stat;
  1008. }
  1009. }
  1010. cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
  1011. if (!cm->fuel_gauge) {
  1012. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  1013. desc->psy_fuel_gauge);
  1014. ret = -ENODEV;
  1015. goto err_chg_stat;
  1016. }
  1017. if (desc->polling_interval_ms == 0 ||
  1018. msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
  1019. dev_err(&pdev->dev, "polling_interval_ms is too small\n");
  1020. ret = -EINVAL;
  1021. goto err_chg_stat;
  1022. }
  1023. if (!desc->temperature_out_of_range) {
  1024. dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
  1025. ret = -EINVAL;
  1026. goto err_chg_stat;
  1027. }
  1028. platform_set_drvdata(pdev, cm);
  1029. memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
  1030. if (!desc->psy_name) {
  1031. strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
  1032. } else {
  1033. strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
  1034. }
  1035. cm->charger_psy.name = cm->psy_name_buf;
  1036. /* Allocate for psy properties because they may vary */
  1037. cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
  1038. * (ARRAY_SIZE(default_charger_props) +
  1039. NUM_CHARGER_PSY_OPTIONAL),
  1040. GFP_KERNEL);
  1041. if (!cm->charger_psy.properties) {
  1042. dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
  1043. ret = -ENOMEM;
  1044. goto err_chg_stat;
  1045. }
  1046. memcpy(cm->charger_psy.properties, default_charger_props,
  1047. sizeof(enum power_supply_property) *
  1048. ARRAY_SIZE(default_charger_props));
  1049. cm->charger_psy.num_properties = psy_default.num_properties;
  1050. /* Find which optional psy-properties are available */
  1051. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  1052. POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
  1053. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1054. POWER_SUPPLY_PROP_CHARGE_NOW;
  1055. cm->charger_psy.num_properties++;
  1056. }
  1057. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  1058. POWER_SUPPLY_PROP_CURRENT_NOW,
  1059. &val)) {
  1060. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1061. POWER_SUPPLY_PROP_CURRENT_NOW;
  1062. cm->charger_psy.num_properties++;
  1063. }
  1064. if (desc->measure_battery_temp) {
  1065. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1066. POWER_SUPPLY_PROP_TEMP;
  1067. cm->charger_psy.num_properties++;
  1068. } else {
  1069. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1070. POWER_SUPPLY_PROP_TEMP_AMBIENT;
  1071. cm->charger_psy.num_properties++;
  1072. }
  1073. INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
  1074. ret = power_supply_register(NULL, &cm->charger_psy);
  1075. if (ret) {
  1076. dev_err(&pdev->dev, "Cannot register charger-manager with"
  1077. " name \"%s\".\n", cm->charger_psy.name);
  1078. goto err_register;
  1079. }
  1080. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  1081. struct charger_regulator *charger
  1082. = &desc->charger_regulators[i];
  1083. charger->consumer = regulator_get(&pdev->dev,
  1084. charger->regulator_name);
  1085. if (charger->consumer == NULL) {
  1086. dev_err(&pdev->dev, "Cannot find charger(%s)n",
  1087. charger->regulator_name);
  1088. ret = -EINVAL;
  1089. goto err_chg_get;
  1090. }
  1091. for (j = 0 ; j < charger->num_cables ; j++) {
  1092. struct charger_cable *cable = &charger->cables[j];
  1093. ret = charger_extcon_init(cm, cable);
  1094. if (ret < 0) {
  1095. dev_err(&pdev->dev, "Cannot find charger(%s)n",
  1096. charger->regulator_name);
  1097. goto err_extcon;
  1098. }
  1099. cable->charger = charger;
  1100. cable->cm = cm;
  1101. }
  1102. }
  1103. ret = try_charger_enable(cm, true);
  1104. if (ret) {
  1105. dev_err(&pdev->dev, "Cannot enable charger regulators\n");
  1106. goto err_chg_enable;
  1107. }
  1108. /* Add to the list */
  1109. mutex_lock(&cm_list_mtx);
  1110. list_add(&cm->entry, &cm_list);
  1111. mutex_unlock(&cm_list_mtx);
  1112. /*
  1113. * Charger-manager is capable of waking up the systme from sleep
  1114. * when event is happend through cm_notify_event()
  1115. */
  1116. device_init_wakeup(&pdev->dev, true);
  1117. device_set_wakeup_capable(&pdev->dev, false);
  1118. schedule_work(&setup_polling);
  1119. return 0;
  1120. err_chg_enable:
  1121. err_extcon:
  1122. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  1123. struct charger_regulator *charger
  1124. = &desc->charger_regulators[i];
  1125. for (j = 0 ; j < charger->num_cables ; j++) {
  1126. struct charger_cable *cable = &charger->cables[j];
  1127. extcon_unregister_interest(&cable->extcon_dev);
  1128. }
  1129. }
  1130. err_chg_get:
  1131. for (i = 0 ; i < desc->num_charger_regulators ; i++)
  1132. regulator_put(desc->charger_regulators[i].consumer);
  1133. power_supply_unregister(&cm->charger_psy);
  1134. err_register:
  1135. kfree(cm->charger_psy.properties);
  1136. err_chg_stat:
  1137. kfree(cm->charger_stat);
  1138. err_no_charger_stat:
  1139. err_no_charger:
  1140. kfree(cm->desc);
  1141. err_alloc_desc:
  1142. kfree(cm);
  1143. err_alloc:
  1144. return ret;
  1145. }
  1146. static int __devexit charger_manager_remove(struct platform_device *pdev)
  1147. {
  1148. struct charger_manager *cm = platform_get_drvdata(pdev);
  1149. struct charger_desc *desc = cm->desc;
  1150. int i = 0;
  1151. int j = 0;
  1152. /* Remove from the list */
  1153. mutex_lock(&cm_list_mtx);
  1154. list_del(&cm->entry);
  1155. mutex_unlock(&cm_list_mtx);
  1156. if (work_pending(&setup_polling))
  1157. cancel_work_sync(&setup_polling);
  1158. if (delayed_work_pending(&cm_monitor_work))
  1159. cancel_delayed_work_sync(&cm_monitor_work);
  1160. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  1161. struct charger_regulator *charger
  1162. = &desc->charger_regulators[i];
  1163. for (j = 0 ; j < charger->num_cables ; j++) {
  1164. struct charger_cable *cable = &charger->cables[j];
  1165. extcon_unregister_interest(&cable->extcon_dev);
  1166. }
  1167. }
  1168. for (i = 0 ; i < desc->num_charger_regulators ; i++)
  1169. regulator_put(desc->charger_regulators[i].consumer);
  1170. power_supply_unregister(&cm->charger_psy);
  1171. try_charger_enable(cm, false);
  1172. kfree(cm->charger_psy.properties);
  1173. kfree(cm->charger_stat);
  1174. kfree(cm->desc);
  1175. kfree(cm);
  1176. return 0;
  1177. }
  1178. static const struct platform_device_id charger_manager_id[] = {
  1179. { "charger-manager", 0 },
  1180. { },
  1181. };
  1182. MODULE_DEVICE_TABLE(platform, charger_manager_id);
  1183. static int cm_suspend_noirq(struct device *dev)
  1184. {
  1185. int ret = 0;
  1186. if (device_may_wakeup(dev)) {
  1187. device_set_wakeup_capable(dev, false);
  1188. ret = -EAGAIN;
  1189. }
  1190. return ret;
  1191. }
  1192. static int cm_suspend_prepare(struct device *dev)
  1193. {
  1194. struct charger_manager *cm = dev_get_drvdata(dev);
  1195. if (!cm_suspended) {
  1196. if (rtc_dev) {
  1197. struct rtc_time tmp;
  1198. unsigned long now;
  1199. rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
  1200. rtc_read_time(rtc_dev, &tmp);
  1201. if (rtc_wkalarm_save.enabled) {
  1202. rtc_tm_to_time(&rtc_wkalarm_save.time,
  1203. &rtc_wkalarm_save_time);
  1204. rtc_tm_to_time(&tmp, &now);
  1205. if (now > rtc_wkalarm_save_time)
  1206. rtc_wkalarm_save_time = 0;
  1207. } else {
  1208. rtc_wkalarm_save_time = 0;
  1209. }
  1210. }
  1211. cm_suspended = true;
  1212. }
  1213. if (delayed_work_pending(&cm->fullbatt_vchk_work))
  1214. cancel_delayed_work(&cm->fullbatt_vchk_work);
  1215. cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
  1216. cm->status_save_batt = is_batt_present(cm);
  1217. if (!cm_rtc_set) {
  1218. cm_suspend_duration_ms = 0;
  1219. cm_rtc_set = cm_setup_timer();
  1220. }
  1221. return 0;
  1222. }
  1223. static void cm_suspend_complete(struct device *dev)
  1224. {
  1225. struct charger_manager *cm = dev_get_drvdata(dev);
  1226. if (cm_suspended) {
  1227. if (rtc_dev) {
  1228. struct rtc_wkalrm tmp;
  1229. rtc_read_alarm(rtc_dev, &tmp);
  1230. rtc_wkalarm_save.pending = tmp.pending;
  1231. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  1232. }
  1233. cm_suspended = false;
  1234. cm_rtc_set = false;
  1235. }
  1236. /* Re-enqueue delayed work (fullbatt_vchk_work) */
  1237. if (cm->fullbatt_vchk_jiffies_at) {
  1238. unsigned long delay = 0;
  1239. unsigned long now = jiffies + CM_JIFFIES_SMALL;
  1240. if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
  1241. delay = (unsigned long)((long)now
  1242. - (long)(cm->fullbatt_vchk_jiffies_at));
  1243. delay = jiffies_to_msecs(delay);
  1244. } else {
  1245. delay = 0;
  1246. }
  1247. /*
  1248. * Account for cm_suspend_duration_ms if
  1249. * assume_timer_stops_in_suspend is active
  1250. */
  1251. if (g_desc && g_desc->assume_timer_stops_in_suspend) {
  1252. if (delay > cm_suspend_duration_ms)
  1253. delay -= cm_suspend_duration_ms;
  1254. else
  1255. delay = 0;
  1256. }
  1257. queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  1258. msecs_to_jiffies(delay));
  1259. }
  1260. device_set_wakeup_capable(cm->dev, false);
  1261. uevent_notify(cm, NULL);
  1262. }
  1263. static const struct dev_pm_ops charger_manager_pm = {
  1264. .prepare = cm_suspend_prepare,
  1265. .suspend_noirq = cm_suspend_noirq,
  1266. .complete = cm_suspend_complete,
  1267. };
  1268. static struct platform_driver charger_manager_driver = {
  1269. .driver = {
  1270. .name = "charger-manager",
  1271. .owner = THIS_MODULE,
  1272. .pm = &charger_manager_pm,
  1273. },
  1274. .probe = charger_manager_probe,
  1275. .remove = __devexit_p(charger_manager_remove),
  1276. .id_table = charger_manager_id,
  1277. };
  1278. static int __init charger_manager_init(void)
  1279. {
  1280. cm_wq = create_freezable_workqueue("charger_manager");
  1281. INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
  1282. return platform_driver_register(&charger_manager_driver);
  1283. }
  1284. late_initcall(charger_manager_init);
  1285. static void __exit charger_manager_cleanup(void)
  1286. {
  1287. destroy_workqueue(cm_wq);
  1288. cm_wq = NULL;
  1289. platform_driver_unregister(&charger_manager_driver);
  1290. }
  1291. module_exit(charger_manager_cleanup);
  1292. /**
  1293. * find_power_supply - find the associated power_supply of charger
  1294. * @cm: the Charger Manager representing the battery
  1295. * @psy: pointer to instance of charger's power_supply
  1296. */
  1297. static bool find_power_supply(struct charger_manager *cm,
  1298. struct power_supply *psy)
  1299. {
  1300. int i;
  1301. bool found = false;
  1302. for (i = 0; cm->charger_stat[i]; i++) {
  1303. if (psy == cm->charger_stat[i]) {
  1304. found = true;
  1305. break;
  1306. }
  1307. }
  1308. return found;
  1309. }
  1310. /**
  1311. * cm_notify_event - charger driver notify Charger Manager of charger event
  1312. * @psy: pointer to instance of charger's power_supply
  1313. * @type: type of charger event
  1314. * @msg: optional message passed to uevent_notify fuction
  1315. */
  1316. void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
  1317. char *msg)
  1318. {
  1319. struct charger_manager *cm;
  1320. bool found_power_supply = false;
  1321. if (psy == NULL)
  1322. return;
  1323. mutex_lock(&cm_list_mtx);
  1324. list_for_each_entry(cm, &cm_list, entry) {
  1325. found_power_supply = find_power_supply(cm, psy);
  1326. if (found_power_supply)
  1327. break;
  1328. }
  1329. mutex_unlock(&cm_list_mtx);
  1330. if (!found_power_supply)
  1331. return;
  1332. switch (type) {
  1333. case CM_EVENT_BATT_FULL:
  1334. fullbatt_handler(cm);
  1335. break;
  1336. case CM_EVENT_BATT_OUT:
  1337. battout_handler(cm);
  1338. break;
  1339. case CM_EVENT_BATT_IN:
  1340. case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
  1341. misc_event_handler(cm, type);
  1342. break;
  1343. case CM_EVENT_UNKNOWN:
  1344. case CM_EVENT_OTHERS:
  1345. uevent_notify(cm, msg ? msg : default_event_names[type]);
  1346. break;
  1347. default:
  1348. dev_err(cm->dev, "%s type not specified.\n", __func__);
  1349. break;
  1350. }
  1351. }
  1352. EXPORT_SYMBOL_GPL(cm_notify_event);
  1353. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1354. MODULE_DESCRIPTION("Charger Manager");
  1355. MODULE_LICENSE("GPL");