charger-manager.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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. #include <linux/sysfs.h>
  25. static const char * const default_event_names[] = {
  26. [CM_EVENT_UNKNOWN] = "Unknown",
  27. [CM_EVENT_BATT_FULL] = "Battery Full",
  28. [CM_EVENT_BATT_IN] = "Battery Inserted",
  29. [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
  30. [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
  31. [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
  32. [CM_EVENT_OTHERS] = "Other battery events"
  33. };
  34. /*
  35. * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
  36. * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
  37. * without any delays.
  38. */
  39. #define CM_JIFFIES_SMALL (2)
  40. /* If y is valid (> 0) and smaller than x, do x = y */
  41. #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
  42. /*
  43. * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
  44. * rtc alarm. It should be 2 or larger
  45. */
  46. #define CM_RTC_SMALL (2)
  47. #define UEVENT_BUF_SIZE 32
  48. static LIST_HEAD(cm_list);
  49. static DEFINE_MUTEX(cm_list_mtx);
  50. /* About in-suspend (suspend-again) monitoring */
  51. static struct rtc_device *rtc_dev;
  52. /*
  53. * Backup RTC alarm
  54. * Save the wakeup alarm before entering suspend-to-RAM
  55. */
  56. static struct rtc_wkalrm rtc_wkalarm_save;
  57. /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
  58. static unsigned long rtc_wkalarm_save_time;
  59. static bool cm_suspended;
  60. static bool cm_rtc_set;
  61. static unsigned long cm_suspend_duration_ms;
  62. /* About normal (not suspended) monitoring */
  63. static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
  64. static unsigned long next_polling; /* Next appointed polling time */
  65. static struct workqueue_struct *cm_wq; /* init at driver add */
  66. static struct delayed_work cm_monitor_work; /* init at driver add */
  67. /* Global charger-manager description */
  68. static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
  69. /**
  70. * is_batt_present - See if the battery presents in place.
  71. * @cm: the Charger Manager representing the battery.
  72. */
  73. static bool is_batt_present(struct charger_manager *cm)
  74. {
  75. union power_supply_propval val;
  76. bool present = false;
  77. int i, ret;
  78. switch (cm->desc->battery_present) {
  79. case CM_BATTERY_PRESENT:
  80. present = true;
  81. break;
  82. case CM_NO_BATTERY:
  83. break;
  84. case CM_FUEL_GAUGE:
  85. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  86. POWER_SUPPLY_PROP_PRESENT, &val);
  87. if (ret == 0 && val.intval)
  88. present = true;
  89. break;
  90. case CM_CHARGER_STAT:
  91. for (i = 0; cm->charger_stat[i]; i++) {
  92. ret = cm->charger_stat[i]->get_property(
  93. cm->charger_stat[i],
  94. POWER_SUPPLY_PROP_PRESENT, &val);
  95. if (ret == 0 && val.intval) {
  96. present = true;
  97. break;
  98. }
  99. }
  100. break;
  101. }
  102. return present;
  103. }
  104. /**
  105. * is_ext_pwr_online - See if an external power source is attached to charge
  106. * @cm: the Charger Manager representing the battery.
  107. *
  108. * Returns true if at least one of the chargers of the battery has an external
  109. * power source attached to charge the battery regardless of whether it is
  110. * actually charging or not.
  111. */
  112. static bool is_ext_pwr_online(struct charger_manager *cm)
  113. {
  114. union power_supply_propval val;
  115. bool online = false;
  116. int i, ret;
  117. /* If at least one of them has one, it's yes. */
  118. for (i = 0; cm->charger_stat[i]; i++) {
  119. ret = cm->charger_stat[i]->get_property(
  120. cm->charger_stat[i],
  121. POWER_SUPPLY_PROP_ONLINE, &val);
  122. if (ret == 0 && val.intval) {
  123. online = true;
  124. break;
  125. }
  126. }
  127. return online;
  128. }
  129. /**
  130. * get_batt_uV - Get the voltage level of the battery
  131. * @cm: the Charger Manager representing the battery.
  132. * @uV: the voltage level returned.
  133. *
  134. * Returns 0 if there is no error.
  135. * Returns a negative value on error.
  136. */
  137. static int get_batt_uV(struct charger_manager *cm, int *uV)
  138. {
  139. union power_supply_propval val;
  140. int ret;
  141. if (!cm->fuel_gauge)
  142. return -ENODEV;
  143. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  144. POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
  145. if (ret)
  146. return ret;
  147. *uV = val.intval;
  148. return 0;
  149. }
  150. /**
  151. * is_charging - Returns true if the battery is being charged.
  152. * @cm: the Charger Manager representing the battery.
  153. */
  154. static bool is_charging(struct charger_manager *cm)
  155. {
  156. int i, ret;
  157. bool charging = false;
  158. union power_supply_propval val;
  159. /* If there is no battery, it cannot be charged */
  160. if (!is_batt_present(cm))
  161. return false;
  162. /* If at least one of the charger is charging, return yes */
  163. for (i = 0; cm->charger_stat[i]; i++) {
  164. /* 1. The charger sholuld not be DISABLED */
  165. if (cm->emergency_stop)
  166. continue;
  167. if (!cm->charger_enabled)
  168. continue;
  169. /* 2. The charger should be online (ext-power) */
  170. ret = cm->charger_stat[i]->get_property(
  171. cm->charger_stat[i],
  172. POWER_SUPPLY_PROP_ONLINE, &val);
  173. if (ret) {
  174. dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
  175. cm->desc->psy_charger_stat[i]);
  176. continue;
  177. }
  178. if (val.intval == 0)
  179. continue;
  180. /*
  181. * 3. The charger should not be FULL, DISCHARGING,
  182. * or NOT_CHARGING.
  183. */
  184. ret = cm->charger_stat[i]->get_property(
  185. cm->charger_stat[i],
  186. POWER_SUPPLY_PROP_STATUS, &val);
  187. if (ret) {
  188. dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
  189. cm->desc->psy_charger_stat[i]);
  190. continue;
  191. }
  192. if (val.intval == POWER_SUPPLY_STATUS_FULL ||
  193. val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
  194. val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
  195. continue;
  196. /* Then, this is charging. */
  197. charging = true;
  198. break;
  199. }
  200. return charging;
  201. }
  202. /**
  203. * is_full_charged - Returns true if the battery is fully charged.
  204. * @cm: the Charger Manager representing the battery.
  205. */
  206. static bool is_full_charged(struct charger_manager *cm)
  207. {
  208. struct charger_desc *desc = cm->desc;
  209. union power_supply_propval val;
  210. int ret = 0;
  211. int uV;
  212. /* If there is no battery, it cannot be charged */
  213. if (!is_batt_present(cm))
  214. return false;
  215. if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
  216. val.intval = 0;
  217. /* Not full if capacity of fuel gauge isn't full */
  218. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  219. POWER_SUPPLY_PROP_CHARGE_FULL, &val);
  220. if (!ret && val.intval > desc->fullbatt_full_capacity)
  221. return true;
  222. }
  223. /* Full, if it's over the fullbatt voltage */
  224. if (desc->fullbatt_uV > 0) {
  225. ret = get_batt_uV(cm, &uV);
  226. if (!ret && uV >= desc->fullbatt_uV)
  227. return true;
  228. }
  229. /* Full, if the capacity is more than fullbatt_soc */
  230. if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
  231. val.intval = 0;
  232. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  233. POWER_SUPPLY_PROP_CAPACITY, &val);
  234. if (!ret && val.intval >= desc->fullbatt_soc)
  235. return true;
  236. }
  237. return false;
  238. }
  239. /**
  240. * is_polling_required - Return true if need to continue polling for this CM.
  241. * @cm: the Charger Manager representing the battery.
  242. */
  243. static bool is_polling_required(struct charger_manager *cm)
  244. {
  245. switch (cm->desc->polling_mode) {
  246. case CM_POLL_DISABLE:
  247. return false;
  248. case CM_POLL_ALWAYS:
  249. return true;
  250. case CM_POLL_EXTERNAL_POWER_ONLY:
  251. return is_ext_pwr_online(cm);
  252. case CM_POLL_CHARGING_ONLY:
  253. return is_charging(cm);
  254. default:
  255. dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
  256. cm->desc->polling_mode);
  257. }
  258. return false;
  259. }
  260. /**
  261. * try_charger_enable - Enable/Disable chargers altogether
  262. * @cm: the Charger Manager representing the battery.
  263. * @enable: true: enable / false: disable
  264. *
  265. * Note that Charger Manager keeps the charger enabled regardless whether
  266. * the charger is charging or not (because battery is full or no external
  267. * power source exists) except when CM needs to disable chargers forcibly
  268. * bacause of emergency causes; when the battery is overheated or too cold.
  269. */
  270. static int try_charger_enable(struct charger_manager *cm, bool enable)
  271. {
  272. int err = 0, i;
  273. struct charger_desc *desc = cm->desc;
  274. /* Ignore if it's redundent command */
  275. if (enable == cm->charger_enabled)
  276. return 0;
  277. if (enable) {
  278. if (cm->emergency_stop)
  279. return -EAGAIN;
  280. /*
  281. * Save start time of charging to limit
  282. * maximum possible charging time.
  283. */
  284. cm->charging_start_time = ktime_to_ms(ktime_get());
  285. cm->charging_end_time = 0;
  286. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  287. if (desc->charger_regulators[i].externally_control)
  288. continue;
  289. err = regulator_enable(desc->charger_regulators[i].consumer);
  290. if (err < 0) {
  291. dev_warn(cm->dev,
  292. "Cannot enable %s regulator\n",
  293. desc->charger_regulators[i].regulator_name);
  294. }
  295. }
  296. } else {
  297. /*
  298. * Save end time of charging to maintain fully charged state
  299. * of battery after full-batt.
  300. */
  301. cm->charging_start_time = 0;
  302. cm->charging_end_time = ktime_to_ms(ktime_get());
  303. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  304. if (desc->charger_regulators[i].externally_control)
  305. continue;
  306. err = regulator_disable(desc->charger_regulators[i].consumer);
  307. if (err < 0) {
  308. dev_warn(cm->dev,
  309. "Cannot disable %s regulator\n",
  310. desc->charger_regulators[i].regulator_name);
  311. }
  312. }
  313. /*
  314. * Abnormal battery state - Stop charging forcibly,
  315. * even if charger was enabled at the other places
  316. */
  317. for (i = 0; i < desc->num_charger_regulators; i++) {
  318. if (regulator_is_enabled(
  319. desc->charger_regulators[i].consumer)) {
  320. regulator_force_disable(
  321. desc->charger_regulators[i].consumer);
  322. dev_warn(cm->dev,
  323. "Disable regulator(%s) forcibly.\n",
  324. desc->charger_regulators[i].regulator_name);
  325. }
  326. }
  327. }
  328. if (!err)
  329. cm->charger_enabled = enable;
  330. return err;
  331. }
  332. /**
  333. * try_charger_restart - Restart charging.
  334. * @cm: the Charger Manager representing the battery.
  335. *
  336. * Restart charging by turning off and on the charger.
  337. */
  338. static int try_charger_restart(struct charger_manager *cm)
  339. {
  340. int err;
  341. if (cm->emergency_stop)
  342. return -EAGAIN;
  343. err = try_charger_enable(cm, false);
  344. if (err)
  345. return err;
  346. return try_charger_enable(cm, true);
  347. }
  348. /**
  349. * uevent_notify - Let users know something has changed.
  350. * @cm: the Charger Manager representing the battery.
  351. * @event: the event string.
  352. *
  353. * If @event is null, it implies that uevent_notify is called
  354. * by resume function. When called in the resume function, cm_suspended
  355. * should be already reset to false in order to let uevent_notify
  356. * notify the recent event during the suspend to users. While
  357. * suspended, uevent_notify does not notify users, but tracks
  358. * events so that uevent_notify can notify users later after resumed.
  359. */
  360. static void uevent_notify(struct charger_manager *cm, const char *event)
  361. {
  362. static char env_str[UEVENT_BUF_SIZE + 1] = "";
  363. static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
  364. if (cm_suspended) {
  365. /* Nothing in suspended-event buffer */
  366. if (env_str_save[0] == 0) {
  367. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  368. return; /* status not changed */
  369. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  370. return;
  371. }
  372. if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
  373. return; /* Duplicated. */
  374. strncpy(env_str_save, event, UEVENT_BUF_SIZE);
  375. return;
  376. }
  377. if (event == NULL) {
  378. /* No messages pending */
  379. if (!env_str_save[0])
  380. return;
  381. strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
  382. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  383. env_str_save[0] = 0;
  384. return;
  385. }
  386. /* status not changed */
  387. if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
  388. return;
  389. /* save the status and notify the update */
  390. strncpy(env_str, event, UEVENT_BUF_SIZE);
  391. kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
  392. dev_info(cm->dev, event);
  393. }
  394. /**
  395. * fullbatt_vchk - Check voltage drop some times after "FULL" event.
  396. * @work: the work_struct appointing the function
  397. *
  398. * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
  399. * charger_desc, Charger Manager checks voltage drop after the battery
  400. * "FULL" event. It checks whether the voltage has dropped more than
  401. * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
  402. */
  403. static void fullbatt_vchk(struct work_struct *work)
  404. {
  405. struct delayed_work *dwork = to_delayed_work(work);
  406. struct charger_manager *cm = container_of(dwork,
  407. struct charger_manager, fullbatt_vchk_work);
  408. struct charger_desc *desc = cm->desc;
  409. int batt_uV, err, diff;
  410. /* remove the appointment for fullbatt_vchk */
  411. cm->fullbatt_vchk_jiffies_at = 0;
  412. if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
  413. return;
  414. err = get_batt_uV(cm, &batt_uV);
  415. if (err) {
  416. dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
  417. return;
  418. }
  419. diff = desc->fullbatt_uV - batt_uV;
  420. if (diff < 0)
  421. return;
  422. dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
  423. if (diff > desc->fullbatt_vchkdrop_uV) {
  424. try_charger_restart(cm);
  425. uevent_notify(cm, "Recharging");
  426. }
  427. }
  428. /**
  429. * check_charging_duration - Monitor charging/discharging duration
  430. * @cm: the Charger Manager representing the battery.
  431. *
  432. * If whole charging duration exceed 'charging_max_duration_ms',
  433. * cm stop charging to prevent overcharge/overheat. If discharging
  434. * duration exceed 'discharging _max_duration_ms', charger cable is
  435. * attached, after full-batt, cm start charging to maintain fully
  436. * charged state for battery.
  437. */
  438. static int check_charging_duration(struct charger_manager *cm)
  439. {
  440. struct charger_desc *desc = cm->desc;
  441. u64 curr = ktime_to_ms(ktime_get());
  442. u64 duration;
  443. int ret = false;
  444. if (!desc->charging_max_duration_ms &&
  445. !desc->discharging_max_duration_ms)
  446. return ret;
  447. if (cm->charger_enabled) {
  448. duration = curr - cm->charging_start_time;
  449. if (duration > desc->charging_max_duration_ms) {
  450. dev_info(cm->dev, "Charging duration exceed %lldms",
  451. desc->charging_max_duration_ms);
  452. uevent_notify(cm, "Discharging");
  453. try_charger_enable(cm, false);
  454. ret = true;
  455. }
  456. } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
  457. duration = curr - cm->charging_end_time;
  458. if (duration > desc->charging_max_duration_ms &&
  459. is_ext_pwr_online(cm)) {
  460. dev_info(cm->dev, "DisCharging duration exceed %lldms",
  461. desc->discharging_max_duration_ms);
  462. uevent_notify(cm, "Recharing");
  463. try_charger_enable(cm, true);
  464. ret = true;
  465. }
  466. }
  467. return ret;
  468. }
  469. /**
  470. * _cm_monitor - Monitor the temperature and return true for exceptions.
  471. * @cm: the Charger Manager representing the battery.
  472. *
  473. * Returns true if there is an event to notify for the battery.
  474. * (True if the status of "emergency_stop" changes)
  475. */
  476. static bool _cm_monitor(struct charger_manager *cm)
  477. {
  478. struct charger_desc *desc = cm->desc;
  479. int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
  480. dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
  481. cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
  482. /* It has been stopped already */
  483. if (temp && cm->emergency_stop)
  484. return false;
  485. /*
  486. * Check temperature whether overheat or cold.
  487. * If temperature is out of range normal state, stop charging.
  488. */
  489. if (temp) {
  490. cm->emergency_stop = temp;
  491. if (!try_charger_enable(cm, false)) {
  492. if (temp > 0)
  493. uevent_notify(cm, "OVERHEAT");
  494. else
  495. uevent_notify(cm, "COLD");
  496. }
  497. /*
  498. * Check whole charging duration and discharing duration
  499. * after full-batt.
  500. */
  501. } else if (!cm->emergency_stop && check_charging_duration(cm)) {
  502. dev_dbg(cm->dev,
  503. "Charging/Discharging duration is out of range");
  504. /*
  505. * Check dropped voltage of battery. If battery voltage is more
  506. * dropped than fullbatt_vchkdrop_uV after fully charged state,
  507. * charger-manager have to recharge battery.
  508. */
  509. } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
  510. !cm->charger_enabled) {
  511. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  512. /*
  513. * Check whether fully charged state to protect overcharge
  514. * if charger-manager is charging for battery.
  515. */
  516. } else if (!cm->emergency_stop && is_full_charged(cm) &&
  517. cm->charger_enabled) {
  518. dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
  519. uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
  520. try_charger_enable(cm, false);
  521. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  522. } else {
  523. cm->emergency_stop = 0;
  524. if (is_ext_pwr_online(cm)) {
  525. if (!try_charger_enable(cm, true))
  526. uevent_notify(cm, "CHARGING");
  527. }
  528. }
  529. return true;
  530. }
  531. /**
  532. * cm_monitor - Monitor every battery.
  533. *
  534. * Returns true if there is an event to notify from any of the batteries.
  535. * (True if the status of "emergency_stop" changes)
  536. */
  537. static bool cm_monitor(void)
  538. {
  539. bool stop = false;
  540. struct charger_manager *cm;
  541. mutex_lock(&cm_list_mtx);
  542. list_for_each_entry(cm, &cm_list, entry) {
  543. if (_cm_monitor(cm))
  544. stop = true;
  545. }
  546. mutex_unlock(&cm_list_mtx);
  547. return stop;
  548. }
  549. /**
  550. * _setup_polling - Setup the next instance of polling.
  551. * @work: work_struct of the function _setup_polling.
  552. */
  553. static void _setup_polling(struct work_struct *work)
  554. {
  555. unsigned long min = ULONG_MAX;
  556. struct charger_manager *cm;
  557. bool keep_polling = false;
  558. unsigned long _next_polling;
  559. mutex_lock(&cm_list_mtx);
  560. list_for_each_entry(cm, &cm_list, entry) {
  561. if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
  562. keep_polling = true;
  563. if (min > cm->desc->polling_interval_ms)
  564. min = cm->desc->polling_interval_ms;
  565. }
  566. }
  567. polling_jiffy = msecs_to_jiffies(min);
  568. if (polling_jiffy <= CM_JIFFIES_SMALL)
  569. polling_jiffy = CM_JIFFIES_SMALL + 1;
  570. if (!keep_polling)
  571. polling_jiffy = ULONG_MAX;
  572. if (polling_jiffy == ULONG_MAX)
  573. goto out;
  574. WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
  575. ". try it later. %s\n", __func__);
  576. /*
  577. * Use mod_delayed_work() iff the next polling interval should
  578. * occur before the currently scheduled one. If @cm_monitor_work
  579. * isn't active, the end result is the same, so no need to worry
  580. * about stale @next_polling.
  581. */
  582. _next_polling = jiffies + polling_jiffy;
  583. if (time_before(_next_polling, next_polling)) {
  584. mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
  585. next_polling = _next_polling;
  586. } else {
  587. if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
  588. next_polling = _next_polling;
  589. }
  590. out:
  591. mutex_unlock(&cm_list_mtx);
  592. }
  593. static DECLARE_WORK(setup_polling, _setup_polling);
  594. /**
  595. * cm_monitor_poller - The Monitor / Poller.
  596. * @work: work_struct of the function cm_monitor_poller
  597. *
  598. * During non-suspended state, cm_monitor_poller is used to poll and monitor
  599. * the batteries.
  600. */
  601. static void cm_monitor_poller(struct work_struct *work)
  602. {
  603. cm_monitor();
  604. schedule_work(&setup_polling);
  605. }
  606. /**
  607. * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
  608. * @cm: the Charger Manager representing the battery.
  609. */
  610. static void fullbatt_handler(struct charger_manager *cm)
  611. {
  612. struct charger_desc *desc = cm->desc;
  613. if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
  614. goto out;
  615. if (cm_suspended)
  616. device_set_wakeup_capable(cm->dev, true);
  617. mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  618. msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
  619. cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
  620. desc->fullbatt_vchkdrop_ms);
  621. if (cm->fullbatt_vchk_jiffies_at == 0)
  622. cm->fullbatt_vchk_jiffies_at = 1;
  623. out:
  624. dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
  625. uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
  626. }
  627. /**
  628. * battout_handler - Event handler for CM_EVENT_BATT_OUT
  629. * @cm: the Charger Manager representing the battery.
  630. */
  631. static void battout_handler(struct charger_manager *cm)
  632. {
  633. if (cm_suspended)
  634. device_set_wakeup_capable(cm->dev, true);
  635. if (!is_batt_present(cm)) {
  636. dev_emerg(cm->dev, "Battery Pulled Out!\n");
  637. uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
  638. } else {
  639. uevent_notify(cm, "Battery Reinserted?");
  640. }
  641. }
  642. /**
  643. * misc_event_handler - Handler for other evnets
  644. * @cm: the Charger Manager representing the battery.
  645. * @type: the Charger Manager representing the battery.
  646. */
  647. static void misc_event_handler(struct charger_manager *cm,
  648. enum cm_event_types type)
  649. {
  650. if (cm_suspended)
  651. device_set_wakeup_capable(cm->dev, true);
  652. if (is_polling_required(cm) && cm->desc->polling_interval_ms)
  653. schedule_work(&setup_polling);
  654. uevent_notify(cm, default_event_names[type]);
  655. }
  656. static int charger_get_property(struct power_supply *psy,
  657. enum power_supply_property psp,
  658. union power_supply_propval *val)
  659. {
  660. struct charger_manager *cm = container_of(psy,
  661. struct charger_manager, charger_psy);
  662. struct charger_desc *desc = cm->desc;
  663. int ret = 0;
  664. int uV;
  665. switch (psp) {
  666. case POWER_SUPPLY_PROP_STATUS:
  667. if (is_charging(cm))
  668. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  669. else if (is_ext_pwr_online(cm))
  670. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  671. else
  672. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  673. break;
  674. case POWER_SUPPLY_PROP_HEALTH:
  675. if (cm->emergency_stop > 0)
  676. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  677. else if (cm->emergency_stop < 0)
  678. val->intval = POWER_SUPPLY_HEALTH_COLD;
  679. else
  680. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  681. break;
  682. case POWER_SUPPLY_PROP_PRESENT:
  683. if (is_batt_present(cm))
  684. val->intval = 1;
  685. else
  686. val->intval = 0;
  687. break;
  688. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  689. ret = get_batt_uV(cm, &val->intval);
  690. break;
  691. case POWER_SUPPLY_PROP_CURRENT_NOW:
  692. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  693. POWER_SUPPLY_PROP_CURRENT_NOW, val);
  694. break;
  695. case POWER_SUPPLY_PROP_TEMP:
  696. /* in thenth of centigrade */
  697. if (cm->last_temp_mC == INT_MIN)
  698. desc->temperature_out_of_range(&cm->last_temp_mC);
  699. val->intval = cm->last_temp_mC / 100;
  700. if (!desc->measure_battery_temp)
  701. ret = -ENODEV;
  702. break;
  703. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  704. /* in thenth of centigrade */
  705. if (cm->last_temp_mC == INT_MIN)
  706. desc->temperature_out_of_range(&cm->last_temp_mC);
  707. val->intval = cm->last_temp_mC / 100;
  708. if (desc->measure_battery_temp)
  709. ret = -ENODEV;
  710. break;
  711. case POWER_SUPPLY_PROP_CAPACITY:
  712. if (!cm->fuel_gauge) {
  713. ret = -ENODEV;
  714. break;
  715. }
  716. if (!is_batt_present(cm)) {
  717. /* There is no battery. Assume 100% */
  718. val->intval = 100;
  719. break;
  720. }
  721. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  722. POWER_SUPPLY_PROP_CAPACITY, val);
  723. if (ret)
  724. break;
  725. if (val->intval > 100) {
  726. val->intval = 100;
  727. break;
  728. }
  729. if (val->intval < 0)
  730. val->intval = 0;
  731. /* Do not adjust SOC when charging: voltage is overrated */
  732. if (is_charging(cm))
  733. break;
  734. /*
  735. * If the capacity value is inconsistent, calibrate it base on
  736. * the battery voltage values and the thresholds given as desc
  737. */
  738. ret = get_batt_uV(cm, &uV);
  739. if (ret) {
  740. /* Voltage information not available. No calibration */
  741. ret = 0;
  742. break;
  743. }
  744. if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
  745. !is_charging(cm)) {
  746. val->intval = 100;
  747. break;
  748. }
  749. break;
  750. case POWER_SUPPLY_PROP_ONLINE:
  751. if (is_ext_pwr_online(cm))
  752. val->intval = 1;
  753. else
  754. val->intval = 0;
  755. break;
  756. case POWER_SUPPLY_PROP_CHARGE_FULL:
  757. if (is_full_charged(cm))
  758. val->intval = 1;
  759. else
  760. val->intval = 0;
  761. ret = 0;
  762. break;
  763. case POWER_SUPPLY_PROP_CHARGE_NOW:
  764. if (is_charging(cm)) {
  765. ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
  766. POWER_SUPPLY_PROP_CHARGE_NOW,
  767. val);
  768. if (ret) {
  769. val->intval = 1;
  770. ret = 0;
  771. } else {
  772. /* If CHARGE_NOW is supplied, use it */
  773. val->intval = (val->intval > 0) ?
  774. val->intval : 1;
  775. }
  776. } else {
  777. val->intval = 0;
  778. }
  779. break;
  780. default:
  781. return -EINVAL;
  782. }
  783. return ret;
  784. }
  785. #define NUM_CHARGER_PSY_OPTIONAL (4)
  786. static enum power_supply_property default_charger_props[] = {
  787. /* Guaranteed to provide */
  788. POWER_SUPPLY_PROP_STATUS,
  789. POWER_SUPPLY_PROP_HEALTH,
  790. POWER_SUPPLY_PROP_PRESENT,
  791. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  792. POWER_SUPPLY_PROP_CAPACITY,
  793. POWER_SUPPLY_PROP_ONLINE,
  794. POWER_SUPPLY_PROP_CHARGE_FULL,
  795. /*
  796. * Optional properties are:
  797. * POWER_SUPPLY_PROP_CHARGE_NOW,
  798. * POWER_SUPPLY_PROP_CURRENT_NOW,
  799. * POWER_SUPPLY_PROP_TEMP, and
  800. * POWER_SUPPLY_PROP_TEMP_AMBIENT,
  801. */
  802. };
  803. static struct power_supply psy_default = {
  804. .name = "battery",
  805. .type = POWER_SUPPLY_TYPE_BATTERY,
  806. .properties = default_charger_props,
  807. .num_properties = ARRAY_SIZE(default_charger_props),
  808. .get_property = charger_get_property,
  809. };
  810. /**
  811. * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
  812. * for suspend_again.
  813. *
  814. * Returns true if the alarm is set for Charger Manager to use.
  815. * Returns false if
  816. * cm_setup_timer fails to set an alarm,
  817. * cm_setup_timer does not need to set an alarm for Charger Manager,
  818. * or an alarm previously configured is to be used.
  819. */
  820. static bool cm_setup_timer(void)
  821. {
  822. struct charger_manager *cm;
  823. unsigned int wakeup_ms = UINT_MAX;
  824. bool ret = false;
  825. mutex_lock(&cm_list_mtx);
  826. list_for_each_entry(cm, &cm_list, entry) {
  827. unsigned int fbchk_ms = 0;
  828. /* fullbatt_vchk is required. setup timer for that */
  829. if (cm->fullbatt_vchk_jiffies_at) {
  830. fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
  831. - jiffies);
  832. if (time_is_before_eq_jiffies(
  833. cm->fullbatt_vchk_jiffies_at) ||
  834. msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
  835. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  836. fbchk_ms = 0;
  837. }
  838. }
  839. CM_MIN_VALID(wakeup_ms, fbchk_ms);
  840. /* Skip if polling is not required for this CM */
  841. if (!is_polling_required(cm) && !cm->emergency_stop)
  842. continue;
  843. if (cm->desc->polling_interval_ms == 0)
  844. continue;
  845. CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
  846. }
  847. mutex_unlock(&cm_list_mtx);
  848. if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
  849. pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
  850. if (rtc_dev) {
  851. struct rtc_wkalrm tmp;
  852. unsigned long time, now;
  853. unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
  854. /*
  855. * Set alarm with the polling interval (wakeup_ms)
  856. * except when rtc_wkalarm_save comes first.
  857. * However, the alarm time should be NOW +
  858. * CM_RTC_SMALL or later.
  859. */
  860. tmp.enabled = 1;
  861. rtc_read_time(rtc_dev, &tmp.time);
  862. rtc_tm_to_time(&tmp.time, &now);
  863. if (add < CM_RTC_SMALL)
  864. add = CM_RTC_SMALL;
  865. time = now + add;
  866. ret = true;
  867. if (rtc_wkalarm_save.enabled &&
  868. rtc_wkalarm_save_time &&
  869. rtc_wkalarm_save_time < time) {
  870. if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
  871. time = now + CM_RTC_SMALL;
  872. else
  873. time = rtc_wkalarm_save_time;
  874. /* The timer is not appointed by CM */
  875. ret = false;
  876. }
  877. pr_info("Waking up after %lu secs.\n",
  878. time - now);
  879. rtc_time_to_tm(time, &tmp.time);
  880. rtc_set_alarm(rtc_dev, &tmp);
  881. cm_suspend_duration_ms += wakeup_ms;
  882. return ret;
  883. }
  884. }
  885. if (rtc_dev)
  886. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  887. return false;
  888. }
  889. static void _cm_fbchk_in_suspend(struct charger_manager *cm)
  890. {
  891. unsigned long jiffy_now = jiffies;
  892. if (!cm->fullbatt_vchk_jiffies_at)
  893. return;
  894. if (g_desc && g_desc->assume_timer_stops_in_suspend)
  895. jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
  896. /* Execute now if it's going to be executed not too long after */
  897. jiffy_now += CM_JIFFIES_SMALL;
  898. if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
  899. fullbatt_vchk(&cm->fullbatt_vchk_work.work);
  900. }
  901. /**
  902. * cm_suspend_again - Determine whether suspend again or not
  903. *
  904. * Returns true if the system should be suspended again
  905. * Returns false if the system should be woken up
  906. */
  907. bool cm_suspend_again(void)
  908. {
  909. struct charger_manager *cm;
  910. bool ret = false;
  911. if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
  912. !cm_rtc_set)
  913. return false;
  914. if (cm_monitor())
  915. goto out;
  916. ret = true;
  917. mutex_lock(&cm_list_mtx);
  918. list_for_each_entry(cm, &cm_list, entry) {
  919. _cm_fbchk_in_suspend(cm);
  920. if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
  921. cm->status_save_batt != is_batt_present(cm)) {
  922. ret = false;
  923. break;
  924. }
  925. }
  926. mutex_unlock(&cm_list_mtx);
  927. cm_rtc_set = cm_setup_timer();
  928. out:
  929. /* It's about the time when the non-CM appointed timer goes off */
  930. if (rtc_wkalarm_save.enabled) {
  931. unsigned long now;
  932. struct rtc_time tmp;
  933. rtc_read_time(rtc_dev, &tmp);
  934. rtc_tm_to_time(&tmp, &now);
  935. if (rtc_wkalarm_save_time &&
  936. now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
  937. return false;
  938. }
  939. return ret;
  940. }
  941. EXPORT_SYMBOL_GPL(cm_suspend_again);
  942. /**
  943. * setup_charger_manager - initialize charger_global_desc data
  944. * @gd: pointer to instance of charger_global_desc
  945. */
  946. int setup_charger_manager(struct charger_global_desc *gd)
  947. {
  948. if (!gd)
  949. return -EINVAL;
  950. if (rtc_dev)
  951. rtc_class_close(rtc_dev);
  952. rtc_dev = NULL;
  953. g_desc = NULL;
  954. if (!gd->rtc_only_wakeup) {
  955. pr_err("The callback rtc_only_wakeup is not given.\n");
  956. return -EINVAL;
  957. }
  958. if (gd->rtc_name) {
  959. rtc_dev = rtc_class_open(gd->rtc_name);
  960. if (IS_ERR_OR_NULL(rtc_dev)) {
  961. rtc_dev = NULL;
  962. /* Retry at probe. RTC may be not registered yet */
  963. }
  964. } else {
  965. pr_warn("No wakeup timer is given for charger manager."
  966. "In-suspend monitoring won't work.\n");
  967. }
  968. g_desc = gd;
  969. return 0;
  970. }
  971. EXPORT_SYMBOL_GPL(setup_charger_manager);
  972. /**
  973. * charger_extcon_work - enable/diable charger according to the state
  974. * of charger cable
  975. *
  976. * @work: work_struct of the function charger_extcon_work.
  977. */
  978. static void charger_extcon_work(struct work_struct *work)
  979. {
  980. struct charger_cable *cable =
  981. container_of(work, struct charger_cable, wq);
  982. int ret;
  983. if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
  984. ret = regulator_set_current_limit(cable->charger->consumer,
  985. cable->min_uA, cable->max_uA);
  986. if (ret < 0) {
  987. pr_err("Cannot set current limit of %s (%s)\n",
  988. cable->charger->regulator_name, cable->name);
  989. return;
  990. }
  991. pr_info("Set current limit of %s : %duA ~ %duA\n",
  992. cable->charger->regulator_name,
  993. cable->min_uA, cable->max_uA);
  994. }
  995. try_charger_enable(cable->cm, cable->attached);
  996. }
  997. /**
  998. * charger_extcon_notifier - receive the state of charger cable
  999. * when registered cable is attached or detached.
  1000. *
  1001. * @self: the notifier block of the charger_extcon_notifier.
  1002. * @event: the cable state.
  1003. * @ptr: the data pointer of notifier block.
  1004. */
  1005. static int charger_extcon_notifier(struct notifier_block *self,
  1006. unsigned long event, void *ptr)
  1007. {
  1008. struct charger_cable *cable =
  1009. container_of(self, struct charger_cable, nb);
  1010. /*
  1011. * The newly state of charger cable.
  1012. * If cable is attached, cable->attached is true.
  1013. */
  1014. cable->attached = event;
  1015. /*
  1016. * Setup monitoring to check battery state
  1017. * when charger cable is attached.
  1018. */
  1019. if (cable->attached && is_polling_required(cable->cm)) {
  1020. cancel_work_sync(&setup_polling);
  1021. schedule_work(&setup_polling);
  1022. }
  1023. /*
  1024. * Setup work for controlling charger(regulator)
  1025. * according to charger cable.
  1026. */
  1027. schedule_work(&cable->wq);
  1028. return NOTIFY_DONE;
  1029. }
  1030. /**
  1031. * charger_extcon_init - register external connector to use it
  1032. * as the charger cable
  1033. *
  1034. * @cm: the Charger Manager representing the battery.
  1035. * @cable: the Charger cable representing the external connector.
  1036. */
  1037. static int charger_extcon_init(struct charger_manager *cm,
  1038. struct charger_cable *cable)
  1039. {
  1040. int ret = 0;
  1041. /*
  1042. * Charger manager use Extcon framework to identify
  1043. * the charger cable among various external connector
  1044. * cable (e.g., TA, USB, MHL, Dock).
  1045. */
  1046. INIT_WORK(&cable->wq, charger_extcon_work);
  1047. cable->nb.notifier_call = charger_extcon_notifier;
  1048. ret = extcon_register_interest(&cable->extcon_dev,
  1049. cable->extcon_name, cable->name, &cable->nb);
  1050. if (ret < 0) {
  1051. pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
  1052. cable->extcon_name,
  1053. cable->name);
  1054. ret = -EINVAL;
  1055. }
  1056. return ret;
  1057. }
  1058. /**
  1059. * charger_manager_register_extcon - Register extcon device to recevie state
  1060. * of charger cable.
  1061. * @cm: the Charger Manager representing the battery.
  1062. *
  1063. * This function support EXTCON(External Connector) subsystem to detect the
  1064. * state of charger cables for enabling or disabling charger(regulator) and
  1065. * select the charger cable for charging among a number of external cable
  1066. * according to policy of H/W board.
  1067. */
  1068. static int charger_manager_register_extcon(struct charger_manager *cm)
  1069. {
  1070. struct charger_desc *desc = cm->desc;
  1071. struct charger_regulator *charger;
  1072. int ret = 0;
  1073. int i;
  1074. int j;
  1075. for (i = 0; i < desc->num_charger_regulators; i++) {
  1076. charger = &desc->charger_regulators[i];
  1077. charger->consumer = regulator_get(cm->dev,
  1078. charger->regulator_name);
  1079. if (charger->consumer == NULL) {
  1080. dev_err(cm->dev, "Cannot find charger(%s)n",
  1081. charger->regulator_name);
  1082. ret = -EINVAL;
  1083. goto err;
  1084. }
  1085. charger->cm = cm;
  1086. for (j = 0; j < charger->num_cables; j++) {
  1087. struct charger_cable *cable = &charger->cables[j];
  1088. ret = charger_extcon_init(cm, cable);
  1089. if (ret < 0) {
  1090. dev_err(cm->dev, "Cannot initialize charger(%s)n",
  1091. charger->regulator_name);
  1092. goto err;
  1093. }
  1094. cable->charger = charger;
  1095. cable->cm = cm;
  1096. }
  1097. }
  1098. err:
  1099. return ret;
  1100. }
  1101. /* help function of sysfs node to control charger(regulator) */
  1102. static ssize_t charger_name_show(struct device *dev,
  1103. struct device_attribute *attr, char *buf)
  1104. {
  1105. struct charger_regulator *charger
  1106. = container_of(attr, struct charger_regulator, attr_name);
  1107. return sprintf(buf, "%s\n", charger->regulator_name);
  1108. }
  1109. static ssize_t charger_state_show(struct device *dev,
  1110. struct device_attribute *attr, char *buf)
  1111. {
  1112. struct charger_regulator *charger
  1113. = container_of(attr, struct charger_regulator, attr_state);
  1114. int state = 0;
  1115. if (!charger->externally_control)
  1116. state = regulator_is_enabled(charger->consumer);
  1117. return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
  1118. }
  1119. static ssize_t charger_externally_control_show(struct device *dev,
  1120. struct device_attribute *attr, char *buf)
  1121. {
  1122. struct charger_regulator *charger = container_of(attr,
  1123. struct charger_regulator, attr_externally_control);
  1124. return sprintf(buf, "%d\n", charger->externally_control);
  1125. }
  1126. static ssize_t charger_externally_control_store(struct device *dev,
  1127. struct device_attribute *attr, const char *buf,
  1128. size_t count)
  1129. {
  1130. struct charger_regulator *charger
  1131. = container_of(attr, struct charger_regulator,
  1132. attr_externally_control);
  1133. struct charger_manager *cm = charger->cm;
  1134. struct charger_desc *desc = cm->desc;
  1135. int i;
  1136. int ret;
  1137. int externally_control;
  1138. int chargers_externally_control = 1;
  1139. ret = sscanf(buf, "%d", &externally_control);
  1140. if (ret == 0) {
  1141. ret = -EINVAL;
  1142. return ret;
  1143. }
  1144. if (!externally_control) {
  1145. charger->externally_control = 0;
  1146. return count;
  1147. }
  1148. for (i = 0; i < desc->num_charger_regulators; i++) {
  1149. if (&desc->charger_regulators[i] != charger &&
  1150. !desc->charger_regulators[i].externally_control) {
  1151. /*
  1152. * At least, one charger is controlled by
  1153. * charger-manager
  1154. */
  1155. chargers_externally_control = 0;
  1156. break;
  1157. }
  1158. }
  1159. if (!chargers_externally_control) {
  1160. if (cm->charger_enabled) {
  1161. try_charger_enable(charger->cm, false);
  1162. charger->externally_control = externally_control;
  1163. try_charger_enable(charger->cm, true);
  1164. } else {
  1165. charger->externally_control = externally_control;
  1166. }
  1167. } else {
  1168. dev_warn(cm->dev,
  1169. "'%s' regulator should be controlled "
  1170. "in charger-manager because charger-manager "
  1171. "must need at least one charger for charging\n",
  1172. charger->regulator_name);
  1173. }
  1174. return count;
  1175. }
  1176. /**
  1177. * charger_manager_register_sysfs - Register sysfs entry for each charger
  1178. * @cm: the Charger Manager representing the battery.
  1179. *
  1180. * This function add sysfs entry for charger(regulator) to control charger from
  1181. * user-space. If some development board use one more chargers for charging
  1182. * but only need one charger on specific case which is dependent on user
  1183. * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
  1184. * class/power_supply/battery/charger.[index]/externally_control'. For example,
  1185. * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
  1186. * externally_control, this charger isn't controlled from charger-manager and
  1187. * always stay off state of regulator.
  1188. */
  1189. static int charger_manager_register_sysfs(struct charger_manager *cm)
  1190. {
  1191. struct charger_desc *desc = cm->desc;
  1192. struct charger_regulator *charger;
  1193. int chargers_externally_control = 1;
  1194. char buf[11];
  1195. char *str;
  1196. int ret = 0;
  1197. int i;
  1198. /* Create sysfs entry to control charger(regulator) */
  1199. for (i = 0; i < desc->num_charger_regulators; i++) {
  1200. charger = &desc->charger_regulators[i];
  1201. snprintf(buf, 10, "charger.%d", i);
  1202. str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
  1203. if (!str) {
  1204. dev_err(cm->dev, "Cannot allocate memory: %s\n",
  1205. charger->regulator_name);
  1206. ret = -ENOMEM;
  1207. goto err;
  1208. }
  1209. strcpy(str, buf);
  1210. charger->attrs[0] = &charger->attr_name.attr;
  1211. charger->attrs[1] = &charger->attr_state.attr;
  1212. charger->attrs[2] = &charger->attr_externally_control.attr;
  1213. charger->attrs[3] = NULL;
  1214. charger->attr_g.name = str;
  1215. charger->attr_g.attrs = charger->attrs;
  1216. sysfs_attr_init(&charger->attr_name.attr);
  1217. charger->attr_name.attr.name = "name";
  1218. charger->attr_name.attr.mode = 0444;
  1219. charger->attr_name.show = charger_name_show;
  1220. sysfs_attr_init(&charger->attr_state.attr);
  1221. charger->attr_state.attr.name = "state";
  1222. charger->attr_state.attr.mode = 0444;
  1223. charger->attr_state.show = charger_state_show;
  1224. sysfs_attr_init(&charger->attr_externally_control.attr);
  1225. charger->attr_externally_control.attr.name
  1226. = "externally_control";
  1227. charger->attr_externally_control.attr.mode = 0644;
  1228. charger->attr_externally_control.show
  1229. = charger_externally_control_show;
  1230. charger->attr_externally_control.store
  1231. = charger_externally_control_store;
  1232. if (!desc->charger_regulators[i].externally_control ||
  1233. !chargers_externally_control)
  1234. chargers_externally_control = 0;
  1235. dev_info(cm->dev, "'%s' regulator's externally_control"
  1236. "is %d\n", charger->regulator_name,
  1237. charger->externally_control);
  1238. ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
  1239. &charger->attr_g);
  1240. if (ret < 0) {
  1241. dev_err(cm->dev, "Cannot create sysfs entry"
  1242. "of %s regulator\n",
  1243. charger->regulator_name);
  1244. ret = -EINVAL;
  1245. goto err;
  1246. }
  1247. }
  1248. if (chargers_externally_control) {
  1249. dev_err(cm->dev, "Cannot register regulator because "
  1250. "charger-manager must need at least "
  1251. "one charger for charging battery\n");
  1252. ret = -EINVAL;
  1253. goto err;
  1254. }
  1255. err:
  1256. return ret;
  1257. }
  1258. static int charger_manager_probe(struct platform_device *pdev)
  1259. {
  1260. struct charger_desc *desc = dev_get_platdata(&pdev->dev);
  1261. struct charger_manager *cm;
  1262. int ret = 0, i = 0;
  1263. int j = 0;
  1264. union power_supply_propval val;
  1265. if (g_desc && !rtc_dev && g_desc->rtc_name) {
  1266. rtc_dev = rtc_class_open(g_desc->rtc_name);
  1267. if (IS_ERR_OR_NULL(rtc_dev)) {
  1268. rtc_dev = NULL;
  1269. dev_err(&pdev->dev, "Cannot get RTC %s.\n",
  1270. g_desc->rtc_name);
  1271. ret = -ENODEV;
  1272. goto err_alloc;
  1273. }
  1274. }
  1275. if (!desc) {
  1276. dev_err(&pdev->dev, "No platform data (desc) found.\n");
  1277. ret = -ENODEV;
  1278. goto err_alloc;
  1279. }
  1280. cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
  1281. if (!cm) {
  1282. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  1283. ret = -ENOMEM;
  1284. goto err_alloc;
  1285. }
  1286. /* Basic Values. Unspecified are Null or 0 */
  1287. cm->dev = &pdev->dev;
  1288. cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
  1289. if (!cm->desc) {
  1290. dev_err(&pdev->dev, "Cannot allocate memory.\n");
  1291. ret = -ENOMEM;
  1292. goto err_alloc_desc;
  1293. }
  1294. memcpy(cm->desc, desc, sizeof(struct charger_desc));
  1295. cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
  1296. /*
  1297. * The following two do not need to be errors.
  1298. * Users may intentionally ignore those two features.
  1299. */
  1300. if (desc->fullbatt_uV == 0) {
  1301. dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
  1302. " as it is not supplied.");
  1303. }
  1304. if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
  1305. dev_info(&pdev->dev, "Disabling full-battery voltage drop "
  1306. "checking mechanism as it is not supplied.");
  1307. desc->fullbatt_vchkdrop_ms = 0;
  1308. desc->fullbatt_vchkdrop_uV = 0;
  1309. }
  1310. if (desc->fullbatt_soc == 0) {
  1311. dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
  1312. " charge) threshold as it is not"
  1313. " supplied.");
  1314. }
  1315. if (desc->fullbatt_full_capacity == 0) {
  1316. dev_info(&pdev->dev, "Ignoring full-battery full capacity"
  1317. " threshold as it is not supplied.");
  1318. }
  1319. if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
  1320. ret = -EINVAL;
  1321. dev_err(&pdev->dev, "charger_regulators undefined.\n");
  1322. goto err_no_charger;
  1323. }
  1324. if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
  1325. dev_err(&pdev->dev, "No power supply defined.\n");
  1326. ret = -EINVAL;
  1327. goto err_no_charger_stat;
  1328. }
  1329. /* Counting index only */
  1330. while (desc->psy_charger_stat[i])
  1331. i++;
  1332. cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
  1333. GFP_KERNEL);
  1334. if (!cm->charger_stat) {
  1335. ret = -ENOMEM;
  1336. goto err_no_charger_stat;
  1337. }
  1338. for (i = 0; desc->psy_charger_stat[i]; i++) {
  1339. cm->charger_stat[i] = power_supply_get_by_name(
  1340. desc->psy_charger_stat[i]);
  1341. if (!cm->charger_stat[i]) {
  1342. dev_err(&pdev->dev, "Cannot find power supply "
  1343. "\"%s\"\n",
  1344. desc->psy_charger_stat[i]);
  1345. ret = -ENODEV;
  1346. goto err_chg_stat;
  1347. }
  1348. }
  1349. cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
  1350. if (!cm->fuel_gauge) {
  1351. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  1352. desc->psy_fuel_gauge);
  1353. ret = -ENODEV;
  1354. goto err_chg_stat;
  1355. }
  1356. if (desc->polling_interval_ms == 0 ||
  1357. msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
  1358. dev_err(&pdev->dev, "polling_interval_ms is too small\n");
  1359. ret = -EINVAL;
  1360. goto err_chg_stat;
  1361. }
  1362. if (!desc->temperature_out_of_range) {
  1363. dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
  1364. ret = -EINVAL;
  1365. goto err_chg_stat;
  1366. }
  1367. if (!desc->charging_max_duration_ms ||
  1368. !desc->discharging_max_duration_ms) {
  1369. dev_info(&pdev->dev, "Cannot limit charging duration "
  1370. "checking mechanism to prevent overcharge/overheat "
  1371. "and control discharging duration");
  1372. desc->charging_max_duration_ms = 0;
  1373. desc->discharging_max_duration_ms = 0;
  1374. }
  1375. platform_set_drvdata(pdev, cm);
  1376. memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
  1377. if (!desc->psy_name)
  1378. strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
  1379. else
  1380. strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
  1381. cm->charger_psy.name = cm->psy_name_buf;
  1382. /* Allocate for psy properties because they may vary */
  1383. cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
  1384. * (ARRAY_SIZE(default_charger_props) +
  1385. NUM_CHARGER_PSY_OPTIONAL),
  1386. GFP_KERNEL);
  1387. if (!cm->charger_psy.properties) {
  1388. dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
  1389. ret = -ENOMEM;
  1390. goto err_chg_stat;
  1391. }
  1392. memcpy(cm->charger_psy.properties, default_charger_props,
  1393. sizeof(enum power_supply_property) *
  1394. ARRAY_SIZE(default_charger_props));
  1395. cm->charger_psy.num_properties = psy_default.num_properties;
  1396. /* Find which optional psy-properties are available */
  1397. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  1398. POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
  1399. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1400. POWER_SUPPLY_PROP_CHARGE_NOW;
  1401. cm->charger_psy.num_properties++;
  1402. }
  1403. if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
  1404. POWER_SUPPLY_PROP_CURRENT_NOW,
  1405. &val)) {
  1406. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1407. POWER_SUPPLY_PROP_CURRENT_NOW;
  1408. cm->charger_psy.num_properties++;
  1409. }
  1410. if (desc->measure_battery_temp) {
  1411. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1412. POWER_SUPPLY_PROP_TEMP;
  1413. cm->charger_psy.num_properties++;
  1414. } else {
  1415. cm->charger_psy.properties[cm->charger_psy.num_properties] =
  1416. POWER_SUPPLY_PROP_TEMP_AMBIENT;
  1417. cm->charger_psy.num_properties++;
  1418. }
  1419. INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
  1420. ret = power_supply_register(NULL, &cm->charger_psy);
  1421. if (ret) {
  1422. dev_err(&pdev->dev, "Cannot register charger-manager with"
  1423. " name \"%s\".\n", cm->charger_psy.name);
  1424. goto err_register;
  1425. }
  1426. /* Register extcon device for charger cable */
  1427. ret = charger_manager_register_extcon(cm);
  1428. if (ret < 0) {
  1429. dev_err(&pdev->dev, "Cannot initialize extcon device\n");
  1430. goto err_reg_extcon;
  1431. }
  1432. /* Register sysfs entry for charger(regulator) */
  1433. ret = charger_manager_register_sysfs(cm);
  1434. if (ret < 0) {
  1435. dev_err(&pdev->dev,
  1436. "Cannot initialize sysfs entry of regulator\n");
  1437. goto err_reg_sysfs;
  1438. }
  1439. /* Add to the list */
  1440. mutex_lock(&cm_list_mtx);
  1441. list_add(&cm->entry, &cm_list);
  1442. mutex_unlock(&cm_list_mtx);
  1443. /*
  1444. * Charger-manager is capable of waking up the systme from sleep
  1445. * when event is happend through cm_notify_event()
  1446. */
  1447. device_init_wakeup(&pdev->dev, true);
  1448. device_set_wakeup_capable(&pdev->dev, false);
  1449. schedule_work(&setup_polling);
  1450. return 0;
  1451. err_reg_sysfs:
  1452. for (i = 0; i < desc->num_charger_regulators; i++) {
  1453. struct charger_regulator *charger;
  1454. charger = &desc->charger_regulators[i];
  1455. sysfs_remove_group(&cm->charger_psy.dev->kobj,
  1456. &charger->attr_g);
  1457. kfree(charger->attr_g.name);
  1458. }
  1459. err_reg_extcon:
  1460. for (i = 0; i < desc->num_charger_regulators; i++) {
  1461. struct charger_regulator *charger;
  1462. charger = &desc->charger_regulators[i];
  1463. for (j = 0; j < charger->num_cables; j++) {
  1464. struct charger_cable *cable = &charger->cables[j];
  1465. extcon_unregister_interest(&cable->extcon_dev);
  1466. }
  1467. regulator_put(desc->charger_regulators[i].consumer);
  1468. }
  1469. power_supply_unregister(&cm->charger_psy);
  1470. err_register:
  1471. kfree(cm->charger_psy.properties);
  1472. err_chg_stat:
  1473. kfree(cm->charger_stat);
  1474. err_no_charger_stat:
  1475. err_no_charger:
  1476. kfree(cm->desc);
  1477. err_alloc_desc:
  1478. kfree(cm);
  1479. err_alloc:
  1480. return ret;
  1481. }
  1482. static int charger_manager_remove(struct platform_device *pdev)
  1483. {
  1484. struct charger_manager *cm = platform_get_drvdata(pdev);
  1485. struct charger_desc *desc = cm->desc;
  1486. int i = 0;
  1487. int j = 0;
  1488. /* Remove from the list */
  1489. mutex_lock(&cm_list_mtx);
  1490. list_del(&cm->entry);
  1491. mutex_unlock(&cm_list_mtx);
  1492. cancel_work_sync(&setup_polling);
  1493. cancel_delayed_work_sync(&cm_monitor_work);
  1494. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  1495. struct charger_regulator *charger
  1496. = &desc->charger_regulators[i];
  1497. for (j = 0 ; j < charger->num_cables ; j++) {
  1498. struct charger_cable *cable = &charger->cables[j];
  1499. extcon_unregister_interest(&cable->extcon_dev);
  1500. }
  1501. }
  1502. for (i = 0 ; i < desc->num_charger_regulators ; i++)
  1503. regulator_put(desc->charger_regulators[i].consumer);
  1504. power_supply_unregister(&cm->charger_psy);
  1505. try_charger_enable(cm, false);
  1506. kfree(cm->charger_psy.properties);
  1507. kfree(cm->charger_stat);
  1508. kfree(cm->desc);
  1509. kfree(cm);
  1510. return 0;
  1511. }
  1512. static const struct platform_device_id charger_manager_id[] = {
  1513. { "charger-manager", 0 },
  1514. { },
  1515. };
  1516. MODULE_DEVICE_TABLE(platform, charger_manager_id);
  1517. static int cm_suspend_noirq(struct device *dev)
  1518. {
  1519. int ret = 0;
  1520. if (device_may_wakeup(dev)) {
  1521. device_set_wakeup_capable(dev, false);
  1522. ret = -EAGAIN;
  1523. }
  1524. return ret;
  1525. }
  1526. static int cm_suspend_prepare(struct device *dev)
  1527. {
  1528. struct charger_manager *cm = dev_get_drvdata(dev);
  1529. if (!cm_suspended) {
  1530. if (rtc_dev) {
  1531. struct rtc_time tmp;
  1532. unsigned long now;
  1533. rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
  1534. rtc_read_time(rtc_dev, &tmp);
  1535. if (rtc_wkalarm_save.enabled) {
  1536. rtc_tm_to_time(&rtc_wkalarm_save.time,
  1537. &rtc_wkalarm_save_time);
  1538. rtc_tm_to_time(&tmp, &now);
  1539. if (now > rtc_wkalarm_save_time)
  1540. rtc_wkalarm_save_time = 0;
  1541. } else {
  1542. rtc_wkalarm_save_time = 0;
  1543. }
  1544. }
  1545. cm_suspended = true;
  1546. }
  1547. cancel_delayed_work(&cm->fullbatt_vchk_work);
  1548. cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
  1549. cm->status_save_batt = is_batt_present(cm);
  1550. if (!cm_rtc_set) {
  1551. cm_suspend_duration_ms = 0;
  1552. cm_rtc_set = cm_setup_timer();
  1553. }
  1554. return 0;
  1555. }
  1556. static void cm_suspend_complete(struct device *dev)
  1557. {
  1558. struct charger_manager *cm = dev_get_drvdata(dev);
  1559. if (cm_suspended) {
  1560. if (rtc_dev) {
  1561. struct rtc_wkalrm tmp;
  1562. rtc_read_alarm(rtc_dev, &tmp);
  1563. rtc_wkalarm_save.pending = tmp.pending;
  1564. rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
  1565. }
  1566. cm_suspended = false;
  1567. cm_rtc_set = false;
  1568. }
  1569. /* Re-enqueue delayed work (fullbatt_vchk_work) */
  1570. if (cm->fullbatt_vchk_jiffies_at) {
  1571. unsigned long delay = 0;
  1572. unsigned long now = jiffies + CM_JIFFIES_SMALL;
  1573. if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
  1574. delay = (unsigned long)((long)now
  1575. - (long)(cm->fullbatt_vchk_jiffies_at));
  1576. delay = jiffies_to_msecs(delay);
  1577. } else {
  1578. delay = 0;
  1579. }
  1580. /*
  1581. * Account for cm_suspend_duration_ms if
  1582. * assume_timer_stops_in_suspend is active
  1583. */
  1584. if (g_desc && g_desc->assume_timer_stops_in_suspend) {
  1585. if (delay > cm_suspend_duration_ms)
  1586. delay -= cm_suspend_duration_ms;
  1587. else
  1588. delay = 0;
  1589. }
  1590. queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  1591. msecs_to_jiffies(delay));
  1592. }
  1593. device_set_wakeup_capable(cm->dev, false);
  1594. uevent_notify(cm, NULL);
  1595. }
  1596. static const struct dev_pm_ops charger_manager_pm = {
  1597. .prepare = cm_suspend_prepare,
  1598. .suspend_noirq = cm_suspend_noirq,
  1599. .complete = cm_suspend_complete,
  1600. };
  1601. static struct platform_driver charger_manager_driver = {
  1602. .driver = {
  1603. .name = "charger-manager",
  1604. .owner = THIS_MODULE,
  1605. .pm = &charger_manager_pm,
  1606. },
  1607. .probe = charger_manager_probe,
  1608. .remove = charger_manager_remove,
  1609. .id_table = charger_manager_id,
  1610. };
  1611. static int __init charger_manager_init(void)
  1612. {
  1613. cm_wq = create_freezable_workqueue("charger_manager");
  1614. INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
  1615. return platform_driver_register(&charger_manager_driver);
  1616. }
  1617. late_initcall(charger_manager_init);
  1618. static void __exit charger_manager_cleanup(void)
  1619. {
  1620. destroy_workqueue(cm_wq);
  1621. cm_wq = NULL;
  1622. platform_driver_unregister(&charger_manager_driver);
  1623. }
  1624. module_exit(charger_manager_cleanup);
  1625. /**
  1626. * find_power_supply - find the associated power_supply of charger
  1627. * @cm: the Charger Manager representing the battery
  1628. * @psy: pointer to instance of charger's power_supply
  1629. */
  1630. static bool find_power_supply(struct charger_manager *cm,
  1631. struct power_supply *psy)
  1632. {
  1633. int i;
  1634. bool found = false;
  1635. for (i = 0; cm->charger_stat[i]; i++) {
  1636. if (psy == cm->charger_stat[i]) {
  1637. found = true;
  1638. break;
  1639. }
  1640. }
  1641. return found;
  1642. }
  1643. /**
  1644. * cm_notify_event - charger driver notify Charger Manager of charger event
  1645. * @psy: pointer to instance of charger's power_supply
  1646. * @type: type of charger event
  1647. * @msg: optional message passed to uevent_notify fuction
  1648. */
  1649. void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
  1650. char *msg)
  1651. {
  1652. struct charger_manager *cm;
  1653. bool found_power_supply = false;
  1654. if (psy == NULL)
  1655. return;
  1656. mutex_lock(&cm_list_mtx);
  1657. list_for_each_entry(cm, &cm_list, entry) {
  1658. found_power_supply = find_power_supply(cm, psy);
  1659. if (found_power_supply)
  1660. break;
  1661. }
  1662. mutex_unlock(&cm_list_mtx);
  1663. if (!found_power_supply)
  1664. return;
  1665. switch (type) {
  1666. case CM_EVENT_BATT_FULL:
  1667. fullbatt_handler(cm);
  1668. break;
  1669. case CM_EVENT_BATT_OUT:
  1670. battout_handler(cm);
  1671. break;
  1672. case CM_EVENT_BATT_IN:
  1673. case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
  1674. misc_event_handler(cm, type);
  1675. break;
  1676. case CM_EVENT_UNKNOWN:
  1677. case CM_EVENT_OTHERS:
  1678. uevent_notify(cm, msg ? msg : default_event_names[type]);
  1679. break;
  1680. default:
  1681. dev_err(cm->dev, "%s type not specified.\n", __func__);
  1682. break;
  1683. }
  1684. }
  1685. EXPORT_SYMBOL_GPL(cm_notify_event);
  1686. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1687. MODULE_DESCRIPTION("Charger Manager");
  1688. MODULE_LICENSE("GPL");