charger-manager.c 57 KB

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