charger-manager.c 54 KB

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