charger-manager.c 54 KB

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