charger-manager.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  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, 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. of_node_put(child);
  1367. return ERR_PTR(-ENOMEM);
  1368. }
  1369. chg_regs->cables = cables;
  1370. for_each_child_of_node(child, _child) {
  1371. of_property_read_string(_child,
  1372. "cm-cable-name", &cables->name);
  1373. of_property_read_string(_child,
  1374. "cm-cable-extcon",
  1375. &cables->extcon_name);
  1376. of_property_read_u32(_child,
  1377. "cm-cable-min",
  1378. &cables->min_uA);
  1379. of_property_read_u32(_child,
  1380. "cm-cable-max",
  1381. &cables->max_uA);
  1382. cables++;
  1383. }
  1384. }
  1385. chg_regs++;
  1386. }
  1387. }
  1388. return desc;
  1389. }
  1390. static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
  1391. {
  1392. if (pdev->dev.of_node)
  1393. return of_cm_parse_desc(&pdev->dev);
  1394. return dev_get_platdata(&pdev->dev);
  1395. }
  1396. static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
  1397. {
  1398. cm_timer_set = false;
  1399. return ALARMTIMER_NORESTART;
  1400. }
  1401. static int charger_manager_probe(struct platform_device *pdev)
  1402. {
  1403. struct charger_desc *desc = cm_get_drv_data(pdev);
  1404. struct charger_manager *cm;
  1405. int ret = 0, i = 0;
  1406. int j = 0;
  1407. union power_supply_propval val;
  1408. struct power_supply *fuel_gauge;
  1409. struct power_supply_config psy_cfg = {};
  1410. if (IS_ERR(desc)) {
  1411. dev_err(&pdev->dev, "No platform data (desc) found\n");
  1412. return -ENODEV;
  1413. }
  1414. cm = devm_kzalloc(&pdev->dev,
  1415. sizeof(struct charger_manager), GFP_KERNEL);
  1416. if (!cm)
  1417. return -ENOMEM;
  1418. /* Basic Values. Unspecified are Null or 0 */
  1419. cm->dev = &pdev->dev;
  1420. cm->desc = desc;
  1421. psy_cfg.drv_data = cm;
  1422. /* Initialize alarm timer */
  1423. if (alarmtimer_get_rtcdev()) {
  1424. cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
  1425. alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
  1426. }
  1427. /*
  1428. * The following two do not need to be errors.
  1429. * Users may intentionally ignore those two features.
  1430. */
  1431. if (desc->fullbatt_uV == 0) {
  1432. dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
  1433. }
  1434. if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
  1435. dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
  1436. desc->fullbatt_vchkdrop_ms = 0;
  1437. desc->fullbatt_vchkdrop_uV = 0;
  1438. }
  1439. if (desc->fullbatt_soc == 0) {
  1440. dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
  1441. }
  1442. if (desc->fullbatt_full_capacity == 0) {
  1443. dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
  1444. }
  1445. if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
  1446. dev_err(&pdev->dev, "charger_regulators undefined\n");
  1447. return -EINVAL;
  1448. }
  1449. if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
  1450. dev_err(&pdev->dev, "No power supply defined\n");
  1451. return -EINVAL;
  1452. }
  1453. if (!desc->psy_fuel_gauge) {
  1454. dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
  1455. return -EINVAL;
  1456. }
  1457. /* Counting index only */
  1458. while (desc->psy_charger_stat[i])
  1459. i++;
  1460. /* Check if charger's supplies are present at probe */
  1461. for (i = 0; desc->psy_charger_stat[i]; i++) {
  1462. struct power_supply *psy;
  1463. psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
  1464. if (!psy) {
  1465. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  1466. desc->psy_charger_stat[i]);
  1467. return -ENODEV;
  1468. }
  1469. power_supply_put(psy);
  1470. }
  1471. if (desc->polling_interval_ms == 0 ||
  1472. msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
  1473. dev_err(&pdev->dev, "polling_interval_ms is too small\n");
  1474. return -EINVAL;
  1475. }
  1476. if (!desc->charging_max_duration_ms ||
  1477. !desc->discharging_max_duration_ms) {
  1478. dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
  1479. desc->charging_max_duration_ms = 0;
  1480. desc->discharging_max_duration_ms = 0;
  1481. }
  1482. platform_set_drvdata(pdev, cm);
  1483. memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default));
  1484. if (!desc->psy_name)
  1485. strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
  1486. else
  1487. strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
  1488. cm->charger_psy_desc.name = cm->psy_name_buf;
  1489. /* Allocate for psy properties because they may vary */
  1490. cm->charger_psy_desc.properties = devm_kzalloc(&pdev->dev,
  1491. sizeof(enum power_supply_property)
  1492. * (ARRAY_SIZE(default_charger_props) +
  1493. NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
  1494. if (!cm->charger_psy_desc.properties)
  1495. return -ENOMEM;
  1496. memcpy(cm->charger_psy_desc.properties, default_charger_props,
  1497. sizeof(enum power_supply_property) *
  1498. ARRAY_SIZE(default_charger_props));
  1499. cm->charger_psy_desc.num_properties = psy_default.num_properties;
  1500. /* Find which optional psy-properties are available */
  1501. fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
  1502. if (!fuel_gauge) {
  1503. dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
  1504. desc->psy_fuel_gauge);
  1505. return -ENODEV;
  1506. }
  1507. if (!power_supply_get_property(fuel_gauge,
  1508. POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
  1509. cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
  1510. POWER_SUPPLY_PROP_CHARGE_NOW;
  1511. cm->charger_psy_desc.num_properties++;
  1512. }
  1513. if (!power_supply_get_property(fuel_gauge,
  1514. POWER_SUPPLY_PROP_CURRENT_NOW,
  1515. &val)) {
  1516. cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
  1517. POWER_SUPPLY_PROP_CURRENT_NOW;
  1518. cm->charger_psy_desc.num_properties++;
  1519. }
  1520. ret = cm_init_thermal_data(cm, fuel_gauge);
  1521. if (ret) {
  1522. dev_err(&pdev->dev, "Failed to initialize thermal data\n");
  1523. cm->desc->measure_battery_temp = false;
  1524. }
  1525. power_supply_put(fuel_gauge);
  1526. INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
  1527. cm->charger_psy = power_supply_register(&pdev->dev,
  1528. &cm->charger_psy_desc,
  1529. &psy_cfg);
  1530. if (IS_ERR(cm->charger_psy)) {
  1531. dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
  1532. cm->charger_psy_desc.name);
  1533. return PTR_ERR(cm->charger_psy);
  1534. }
  1535. /* Register extcon device for charger cable */
  1536. ret = charger_manager_register_extcon(cm);
  1537. if (ret < 0) {
  1538. dev_err(&pdev->dev, "Cannot initialize extcon device\n");
  1539. goto err_reg_extcon;
  1540. }
  1541. /* Register sysfs entry for charger(regulator) */
  1542. ret = charger_manager_register_sysfs(cm);
  1543. if (ret < 0) {
  1544. dev_err(&pdev->dev,
  1545. "Cannot initialize sysfs entry of regulator\n");
  1546. goto err_reg_sysfs;
  1547. }
  1548. /* Add to the list */
  1549. mutex_lock(&cm_list_mtx);
  1550. list_add(&cm->entry, &cm_list);
  1551. mutex_unlock(&cm_list_mtx);
  1552. /*
  1553. * Charger-manager is capable of waking up the systme from sleep
  1554. * when event is happend through cm_notify_event()
  1555. */
  1556. device_init_wakeup(&pdev->dev, true);
  1557. device_set_wakeup_capable(&pdev->dev, false);
  1558. /*
  1559. * Charger-manager have to check the charging state right after
  1560. * tialization of charger-manager and then update current charging
  1561. * state.
  1562. */
  1563. cm_monitor();
  1564. schedule_work(&setup_polling);
  1565. return 0;
  1566. err_reg_sysfs:
  1567. for (i = 0; i < desc->num_charger_regulators; i++) {
  1568. struct charger_regulator *charger;
  1569. charger = &desc->charger_regulators[i];
  1570. sysfs_remove_group(&cm->charger_psy->dev.kobj,
  1571. &charger->attr_g);
  1572. }
  1573. err_reg_extcon:
  1574. for (i = 0; i < desc->num_charger_regulators; i++) {
  1575. struct charger_regulator *charger;
  1576. charger = &desc->charger_regulators[i];
  1577. for (j = 0; j < charger->num_cables; j++) {
  1578. struct charger_cable *cable = &charger->cables[j];
  1579. /* Remove notifier block if only edev exists */
  1580. if (cable->extcon_dev.edev)
  1581. extcon_unregister_interest(&cable->extcon_dev);
  1582. }
  1583. regulator_put(desc->charger_regulators[i].consumer);
  1584. }
  1585. power_supply_unregister(cm->charger_psy);
  1586. return ret;
  1587. }
  1588. static int charger_manager_remove(struct platform_device *pdev)
  1589. {
  1590. struct charger_manager *cm = platform_get_drvdata(pdev);
  1591. struct charger_desc *desc = cm->desc;
  1592. int i = 0;
  1593. int j = 0;
  1594. /* Remove from the list */
  1595. mutex_lock(&cm_list_mtx);
  1596. list_del(&cm->entry);
  1597. mutex_unlock(&cm_list_mtx);
  1598. cancel_work_sync(&setup_polling);
  1599. cancel_delayed_work_sync(&cm_monitor_work);
  1600. for (i = 0 ; i < desc->num_charger_regulators ; i++) {
  1601. struct charger_regulator *charger
  1602. = &desc->charger_regulators[i];
  1603. for (j = 0 ; j < charger->num_cables ; j++) {
  1604. struct charger_cable *cable = &charger->cables[j];
  1605. extcon_unregister_interest(&cable->extcon_dev);
  1606. }
  1607. }
  1608. for (i = 0 ; i < desc->num_charger_regulators ; i++)
  1609. regulator_put(desc->charger_regulators[i].consumer);
  1610. power_supply_unregister(cm->charger_psy);
  1611. try_charger_enable(cm, false);
  1612. return 0;
  1613. }
  1614. static const struct platform_device_id charger_manager_id[] = {
  1615. { "charger-manager", 0 },
  1616. { },
  1617. };
  1618. MODULE_DEVICE_TABLE(platform, charger_manager_id);
  1619. static int cm_suspend_noirq(struct device *dev)
  1620. {
  1621. int ret = 0;
  1622. if (device_may_wakeup(dev)) {
  1623. device_set_wakeup_capable(dev, false);
  1624. ret = -EAGAIN;
  1625. }
  1626. return ret;
  1627. }
  1628. static bool cm_need_to_awake(void)
  1629. {
  1630. struct charger_manager *cm;
  1631. if (cm_timer)
  1632. return false;
  1633. mutex_lock(&cm_list_mtx);
  1634. list_for_each_entry(cm, &cm_list, entry) {
  1635. if (is_charging(cm)) {
  1636. mutex_unlock(&cm_list_mtx);
  1637. return true;
  1638. }
  1639. }
  1640. mutex_unlock(&cm_list_mtx);
  1641. return false;
  1642. }
  1643. static int cm_suspend_prepare(struct device *dev)
  1644. {
  1645. struct charger_manager *cm = dev_get_drvdata(dev);
  1646. if (cm_need_to_awake())
  1647. return -EBUSY;
  1648. if (!cm_suspended)
  1649. cm_suspended = true;
  1650. cm_timer_set = cm_setup_timer();
  1651. if (cm_timer_set) {
  1652. cancel_work_sync(&setup_polling);
  1653. cancel_delayed_work_sync(&cm_monitor_work);
  1654. cancel_delayed_work(&cm->fullbatt_vchk_work);
  1655. }
  1656. return 0;
  1657. }
  1658. static void cm_suspend_complete(struct device *dev)
  1659. {
  1660. struct charger_manager *cm = dev_get_drvdata(dev);
  1661. if (cm_suspended)
  1662. cm_suspended = false;
  1663. if (cm_timer_set) {
  1664. ktime_t remain;
  1665. alarm_cancel(cm_timer);
  1666. cm_timer_set = false;
  1667. remain = alarm_expires_remaining(cm_timer);
  1668. cm_suspend_duration_ms -= ktime_to_ms(remain);
  1669. schedule_work(&setup_polling);
  1670. }
  1671. _cm_monitor(cm);
  1672. /* Re-enqueue delayed work (fullbatt_vchk_work) */
  1673. if (cm->fullbatt_vchk_jiffies_at) {
  1674. unsigned long delay = 0;
  1675. unsigned long now = jiffies + CM_JIFFIES_SMALL;
  1676. if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
  1677. delay = (unsigned long)((long)now
  1678. - (long)(cm->fullbatt_vchk_jiffies_at));
  1679. delay = jiffies_to_msecs(delay);
  1680. } else {
  1681. delay = 0;
  1682. }
  1683. /*
  1684. * Account for cm_suspend_duration_ms with assuming that
  1685. * timer stops in suspend.
  1686. */
  1687. if (delay > cm_suspend_duration_ms)
  1688. delay -= cm_suspend_duration_ms;
  1689. else
  1690. delay = 0;
  1691. queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
  1692. msecs_to_jiffies(delay));
  1693. }
  1694. device_set_wakeup_capable(cm->dev, false);
  1695. }
  1696. static const struct dev_pm_ops charger_manager_pm = {
  1697. .prepare = cm_suspend_prepare,
  1698. .suspend_noirq = cm_suspend_noirq,
  1699. .complete = cm_suspend_complete,
  1700. };
  1701. static struct platform_driver charger_manager_driver = {
  1702. .driver = {
  1703. .name = "charger-manager",
  1704. .pm = &charger_manager_pm,
  1705. .of_match_table = charger_manager_match,
  1706. },
  1707. .probe = charger_manager_probe,
  1708. .remove = charger_manager_remove,
  1709. .id_table = charger_manager_id,
  1710. };
  1711. static int __init charger_manager_init(void)
  1712. {
  1713. cm_wq = create_freezable_workqueue("charger_manager");
  1714. INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
  1715. return platform_driver_register(&charger_manager_driver);
  1716. }
  1717. late_initcall(charger_manager_init);
  1718. static void __exit charger_manager_cleanup(void)
  1719. {
  1720. destroy_workqueue(cm_wq);
  1721. cm_wq = NULL;
  1722. platform_driver_unregister(&charger_manager_driver);
  1723. }
  1724. module_exit(charger_manager_cleanup);
  1725. /**
  1726. * find_power_supply - find the associated power_supply of charger
  1727. * @cm: the Charger Manager representing the battery
  1728. * @psy: pointer to instance of charger's power_supply
  1729. */
  1730. static bool find_power_supply(struct charger_manager *cm,
  1731. struct power_supply *psy)
  1732. {
  1733. int i;
  1734. bool found = false;
  1735. for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
  1736. if (!strcmp(psy->desc->name, cm->desc->psy_charger_stat[i])) {
  1737. found = true;
  1738. break;
  1739. }
  1740. }
  1741. return found;
  1742. }
  1743. /**
  1744. * cm_notify_event - charger driver notify Charger Manager of charger event
  1745. * @psy: pointer to instance of charger's power_supply
  1746. * @type: type of charger event
  1747. * @msg: optional message passed to uevent_notify fuction
  1748. */
  1749. void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
  1750. char *msg)
  1751. {
  1752. struct charger_manager *cm;
  1753. bool found_power_supply = false;
  1754. if (psy == NULL)
  1755. return;
  1756. mutex_lock(&cm_list_mtx);
  1757. list_for_each_entry(cm, &cm_list, entry) {
  1758. found_power_supply = find_power_supply(cm, psy);
  1759. if (found_power_supply)
  1760. break;
  1761. }
  1762. mutex_unlock(&cm_list_mtx);
  1763. if (!found_power_supply)
  1764. return;
  1765. switch (type) {
  1766. case CM_EVENT_BATT_FULL:
  1767. fullbatt_handler(cm);
  1768. break;
  1769. case CM_EVENT_BATT_OUT:
  1770. battout_handler(cm);
  1771. break;
  1772. case CM_EVENT_BATT_IN:
  1773. case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
  1774. misc_event_handler(cm, type);
  1775. break;
  1776. case CM_EVENT_UNKNOWN:
  1777. case CM_EVENT_OTHERS:
  1778. uevent_notify(cm, msg ? msg : default_event_names[type]);
  1779. break;
  1780. default:
  1781. dev_err(cm->dev, "%s: type not specified\n", __func__);
  1782. break;
  1783. }
  1784. }
  1785. EXPORT_SYMBOL_GPL(cm_notify_event);
  1786. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1787. MODULE_DESCRIPTION("Charger Manager");
  1788. MODULE_LICENSE("GPL");