charger-manager.c 50 KB

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