charger-manager.c 46 KB

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