charger-manager.c 55 KB

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