charger-manager.c 40 KB

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