charger-manager.c 49 KB

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