abx500_chargalg.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2012
  3. * Copyright (c) 2012 Sony Mobile Communications AB
  4. *
  5. * Charging algorithm driver for abx500 variants
  6. *
  7. * License Terms: GNU General Public License v2
  8. * Authors:
  9. * Johan Palsson <johan.palsson@stericsson.com>
  10. * Karl Komierowski <karl.komierowski@stericsson.com>
  11. * Arun R Murthy <arun.murthy@stericsson.com>
  12. * Author: Imre Sunyi <imre.sunyi@sonymobile.com>
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/completion.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/kobject.h>
  26. #include <linux/of.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/abx500.h>
  29. #include <linux/mfd/abx500/ab8500.h>
  30. #include <linux/mfd/abx500/ux500_chargalg.h>
  31. #include <linux/mfd/abx500/ab8500-bm.h>
  32. #include <linux/notifier.h>
  33. /* Watchdog kick interval */
  34. #define CHG_WD_INTERVAL (6 * HZ)
  35. /* End-of-charge criteria counter */
  36. #define EOC_COND_CNT 10
  37. /* One hour expressed in seconds */
  38. #define ONE_HOUR_IN_SECONDS 3600
  39. /* Five minutes expressed in seconds */
  40. #define FIVE_MINUTES_IN_SECONDS 300
  41. /* Plus margin for the low battery threshold */
  42. #define BAT_PLUS_MARGIN (100)
  43. #define CHARGALG_CURR_STEP_LOW 0
  44. #define CHARGALG_CURR_STEP_HIGH 100
  45. #define to_abx500_chargalg_device_info(x) container_of((x), \
  46. struct abx500_chargalg, chargalg_psy);
  47. enum abx500_chargers {
  48. NO_CHG,
  49. AC_CHG,
  50. USB_CHG,
  51. };
  52. struct abx500_chargalg_charger_info {
  53. enum abx500_chargers conn_chg;
  54. enum abx500_chargers prev_conn_chg;
  55. enum abx500_chargers online_chg;
  56. enum abx500_chargers prev_online_chg;
  57. enum abx500_chargers charger_type;
  58. bool usb_chg_ok;
  59. bool ac_chg_ok;
  60. int usb_volt;
  61. int usb_curr;
  62. int ac_volt;
  63. int ac_curr;
  64. int usb_vset;
  65. int usb_iset;
  66. int ac_vset;
  67. int ac_iset;
  68. };
  69. struct abx500_chargalg_suspension_status {
  70. bool suspended_change;
  71. bool ac_suspended;
  72. bool usb_suspended;
  73. };
  74. struct abx500_chargalg_current_step_status {
  75. bool curr_step_change;
  76. int curr_step;
  77. };
  78. struct abx500_chargalg_battery_data {
  79. int temp;
  80. int volt;
  81. int avg_curr;
  82. int inst_curr;
  83. int percent;
  84. };
  85. enum abx500_chargalg_states {
  86. STATE_HANDHELD_INIT,
  87. STATE_HANDHELD,
  88. STATE_CHG_NOT_OK_INIT,
  89. STATE_CHG_NOT_OK,
  90. STATE_HW_TEMP_PROTECT_INIT,
  91. STATE_HW_TEMP_PROTECT,
  92. STATE_NORMAL_INIT,
  93. STATE_USB_PP_PRE_CHARGE,
  94. STATE_NORMAL,
  95. STATE_WAIT_FOR_RECHARGE_INIT,
  96. STATE_WAIT_FOR_RECHARGE,
  97. STATE_MAINTENANCE_A_INIT,
  98. STATE_MAINTENANCE_A,
  99. STATE_MAINTENANCE_B_INIT,
  100. STATE_MAINTENANCE_B,
  101. STATE_TEMP_UNDEROVER_INIT,
  102. STATE_TEMP_UNDEROVER,
  103. STATE_TEMP_LOWHIGH_INIT,
  104. STATE_TEMP_LOWHIGH,
  105. STATE_SUSPENDED_INIT,
  106. STATE_SUSPENDED,
  107. STATE_OVV_PROTECT_INIT,
  108. STATE_OVV_PROTECT,
  109. STATE_SAFETY_TIMER_EXPIRED_INIT,
  110. STATE_SAFETY_TIMER_EXPIRED,
  111. STATE_BATT_REMOVED_INIT,
  112. STATE_BATT_REMOVED,
  113. STATE_WD_EXPIRED_INIT,
  114. STATE_WD_EXPIRED,
  115. };
  116. static const char *states[] = {
  117. "HANDHELD_INIT",
  118. "HANDHELD",
  119. "CHG_NOT_OK_INIT",
  120. "CHG_NOT_OK",
  121. "HW_TEMP_PROTECT_INIT",
  122. "HW_TEMP_PROTECT",
  123. "NORMAL_INIT",
  124. "USB_PP_PRE_CHARGE",
  125. "NORMAL",
  126. "WAIT_FOR_RECHARGE_INIT",
  127. "WAIT_FOR_RECHARGE",
  128. "MAINTENANCE_A_INIT",
  129. "MAINTENANCE_A",
  130. "MAINTENANCE_B_INIT",
  131. "MAINTENANCE_B",
  132. "TEMP_UNDEROVER_INIT",
  133. "TEMP_UNDEROVER",
  134. "TEMP_LOWHIGH_INIT",
  135. "TEMP_LOWHIGH",
  136. "SUSPENDED_INIT",
  137. "SUSPENDED",
  138. "OVV_PROTECT_INIT",
  139. "OVV_PROTECT",
  140. "SAFETY_TIMER_EXPIRED_INIT",
  141. "SAFETY_TIMER_EXPIRED",
  142. "BATT_REMOVED_INIT",
  143. "BATT_REMOVED",
  144. "WD_EXPIRED_INIT",
  145. "WD_EXPIRED",
  146. };
  147. struct abx500_chargalg_events {
  148. bool batt_unknown;
  149. bool mainextchnotok;
  150. bool batt_ovv;
  151. bool batt_rem;
  152. bool btemp_underover;
  153. bool btemp_lowhigh;
  154. bool main_thermal_prot;
  155. bool usb_thermal_prot;
  156. bool main_ovv;
  157. bool vbus_ovv;
  158. bool usbchargernotok;
  159. bool safety_timer_expired;
  160. bool maintenance_timer_expired;
  161. bool ac_wd_expired;
  162. bool usb_wd_expired;
  163. bool ac_cv_active;
  164. bool usb_cv_active;
  165. bool vbus_collapsed;
  166. };
  167. /**
  168. * struct abx500_charge_curr_maximization - Charger maximization parameters
  169. * @original_iset: the non optimized/maximised charger current
  170. * @current_iset: the charging current used at this moment
  171. * @test_delta_i: the delta between the current we want to charge and the
  172. current that is really going into the battery
  173. * @condition_cnt: number of iterations needed before a new charger current
  174. is set
  175. * @max_current: maximum charger current
  176. * @wait_cnt: to avoid too fast current step down in case of charger
  177. * voltage collapse, we insert this delay between step
  178. * down
  179. * @level: tells in how many steps the charging current has been
  180. increased
  181. */
  182. struct abx500_charge_curr_maximization {
  183. int original_iset;
  184. int current_iset;
  185. int test_delta_i;
  186. int condition_cnt;
  187. int max_current;
  188. int wait_cnt;
  189. u8 level;
  190. };
  191. enum maxim_ret {
  192. MAXIM_RET_NOACTION,
  193. MAXIM_RET_CHANGE,
  194. MAXIM_RET_IBAT_TOO_HIGH,
  195. };
  196. /**
  197. * struct abx500_chargalg - abx500 Charging algorithm device information
  198. * @dev: pointer to the structure device
  199. * @charge_status: battery operating status
  200. * @eoc_cnt: counter used to determine end-of_charge
  201. * @maintenance_chg: indicate if maintenance charge is active
  202. * @t_hyst_norm temperature hysteresis when the temperature has been
  203. * over or under normal limits
  204. * @t_hyst_lowhigh temperature hysteresis when the temperature has been
  205. * over or under the high or low limits
  206. * @charge_state: current state of the charging algorithm
  207. * @ccm charging current maximization parameters
  208. * @chg_info: information about connected charger types
  209. * @batt_data: data of the battery
  210. * @susp_status: current charger suspension status
  211. * @bm: Platform specific battery management information
  212. * @curr_status: Current step status for over-current protection
  213. * @parent: pointer to the struct abx500
  214. * @chargalg_psy: structure that holds the battery properties exposed by
  215. * the charging algorithm
  216. * @events: structure for information about events triggered
  217. * @chargalg_wq: work queue for running the charging algorithm
  218. * @chargalg_periodic_work: work to run the charging algorithm periodically
  219. * @chargalg_wd_work: work to kick the charger watchdog periodically
  220. * @chargalg_work: work to run the charging algorithm instantly
  221. * @safety_timer: charging safety timer
  222. * @maintenance_timer: maintenance charging timer
  223. * @chargalg_kobject: structure of type kobject
  224. */
  225. struct abx500_chargalg {
  226. struct device *dev;
  227. int charge_status;
  228. int eoc_cnt;
  229. bool maintenance_chg;
  230. int t_hyst_norm;
  231. int t_hyst_lowhigh;
  232. enum abx500_chargalg_states charge_state;
  233. struct abx500_charge_curr_maximization ccm;
  234. struct abx500_chargalg_charger_info chg_info;
  235. struct abx500_chargalg_battery_data batt_data;
  236. struct abx500_chargalg_suspension_status susp_status;
  237. struct ab8500 *parent;
  238. struct abx500_chargalg_current_step_status curr_status;
  239. struct abx500_bm_data *bm;
  240. struct power_supply chargalg_psy;
  241. struct ux500_charger *ac_chg;
  242. struct ux500_charger *usb_chg;
  243. struct abx500_chargalg_events events;
  244. struct workqueue_struct *chargalg_wq;
  245. struct delayed_work chargalg_periodic_work;
  246. struct delayed_work chargalg_wd_work;
  247. struct work_struct chargalg_work;
  248. struct hrtimer safety_timer;
  249. struct hrtimer maintenance_timer;
  250. struct kobject chargalg_kobject;
  251. };
  252. /*External charger prepare notifier*/
  253. BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
  254. /* Main battery properties */
  255. static enum power_supply_property abx500_chargalg_props[] = {
  256. POWER_SUPPLY_PROP_STATUS,
  257. POWER_SUPPLY_PROP_HEALTH,
  258. };
  259. struct abx500_chargalg_sysfs_entry {
  260. struct attribute attr;
  261. ssize_t (*show)(struct abx500_chargalg *, char *);
  262. ssize_t (*store)(struct abx500_chargalg *, const char *, size_t);
  263. };
  264. /**
  265. * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
  266. * @timer: pointer to the hrtimer structure
  267. *
  268. * This function gets called when the safety timer for the charger
  269. * expires
  270. */
  271. static enum hrtimer_restart
  272. abx500_chargalg_safety_timer_expired(struct hrtimer *timer)
  273. {
  274. struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
  275. safety_timer);
  276. dev_err(di->dev, "Safety timer expired\n");
  277. di->events.safety_timer_expired = true;
  278. /* Trigger execution of the algorithm instantly */
  279. queue_work(di->chargalg_wq, &di->chargalg_work);
  280. return HRTIMER_NORESTART;
  281. }
  282. /**
  283. * abx500_chargalg_maintenance_timer_expired() - Expiration of
  284. * the maintenance timer
  285. * @timer: pointer to the timer structure
  286. *
  287. * This function gets called when the maintenence timer
  288. * expires
  289. */
  290. static enum hrtimer_restart
  291. abx500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
  292. {
  293. struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
  294. maintenance_timer);
  295. dev_dbg(di->dev, "Maintenance timer expired\n");
  296. di->events.maintenance_timer_expired = true;
  297. /* Trigger execution of the algorithm instantly */
  298. queue_work(di->chargalg_wq, &di->chargalg_work);
  299. return HRTIMER_NORESTART;
  300. }
  301. /**
  302. * abx500_chargalg_state_to() - Change charge state
  303. * @di: pointer to the abx500_chargalg structure
  304. *
  305. * This function gets called when a charge state change should occur
  306. */
  307. static void abx500_chargalg_state_to(struct abx500_chargalg *di,
  308. enum abx500_chargalg_states state)
  309. {
  310. dev_dbg(di->dev,
  311. "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
  312. di->charge_state == state ? "NO" : "YES",
  313. di->charge_state,
  314. states[di->charge_state],
  315. state,
  316. states[state]);
  317. di->charge_state = state;
  318. }
  319. static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
  320. {
  321. switch (di->charge_state) {
  322. case STATE_NORMAL:
  323. case STATE_MAINTENANCE_A:
  324. case STATE_MAINTENANCE_B:
  325. break;
  326. default:
  327. return 0;
  328. }
  329. if (di->chg_info.charger_type & USB_CHG) {
  330. return di->usb_chg->ops.check_enable(di->usb_chg,
  331. di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
  332. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
  333. } else if ((di->chg_info.charger_type & AC_CHG) &&
  334. !(di->ac_chg->external)) {
  335. return di->ac_chg->ops.check_enable(di->ac_chg,
  336. di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
  337. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
  338. }
  339. return 0;
  340. }
  341. /**
  342. * abx500_chargalg_check_charger_connection() - Check charger connection change
  343. * @di: pointer to the abx500_chargalg structure
  344. *
  345. * This function will check if there is a change in the charger connection
  346. * and change charge state accordingly. AC has precedence over USB.
  347. */
  348. static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
  349. {
  350. if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
  351. di->susp_status.suspended_change) {
  352. /*
  353. * Charger state changed or suspension
  354. * has changed since last update
  355. */
  356. if ((di->chg_info.conn_chg & AC_CHG) &&
  357. !di->susp_status.ac_suspended) {
  358. dev_dbg(di->dev, "Charging source is AC\n");
  359. if (di->chg_info.charger_type != AC_CHG) {
  360. di->chg_info.charger_type = AC_CHG;
  361. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  362. }
  363. } else if ((di->chg_info.conn_chg & USB_CHG) &&
  364. !di->susp_status.usb_suspended) {
  365. dev_dbg(di->dev, "Charging source is USB\n");
  366. di->chg_info.charger_type = USB_CHG;
  367. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  368. } else if (di->chg_info.conn_chg &&
  369. (di->susp_status.ac_suspended ||
  370. di->susp_status.usb_suspended)) {
  371. dev_dbg(di->dev, "Charging is suspended\n");
  372. di->chg_info.charger_type = NO_CHG;
  373. abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
  374. } else {
  375. dev_dbg(di->dev, "Charging source is OFF\n");
  376. di->chg_info.charger_type = NO_CHG;
  377. abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
  378. }
  379. di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
  380. di->susp_status.suspended_change = false;
  381. }
  382. return di->chg_info.conn_chg;
  383. }
  384. /**
  385. * abx500_chargalg_check_current_step_status() - Check charging current
  386. * step status.
  387. * @di: pointer to the abx500_chargalg structure
  388. *
  389. * This function will check if there is a change in the charging current step
  390. * and change charge state accordingly.
  391. */
  392. static void abx500_chargalg_check_current_step_status
  393. (struct abx500_chargalg *di)
  394. {
  395. if (di->curr_status.curr_step_change)
  396. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  397. di->curr_status.curr_step_change = false;
  398. }
  399. /**
  400. * abx500_chargalg_start_safety_timer() - Start charging safety timer
  401. * @di: pointer to the abx500_chargalg structure
  402. *
  403. * The safety timer is used to avoid overcharging of old or bad batteries.
  404. * There are different timers for AC and USB
  405. */
  406. static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
  407. {
  408. /* Charger-dependent expiration time in hours*/
  409. int timer_expiration = 0;
  410. switch (di->chg_info.charger_type) {
  411. case AC_CHG:
  412. timer_expiration = di->bm->main_safety_tmr_h;
  413. break;
  414. case USB_CHG:
  415. timer_expiration = di->bm->usb_safety_tmr_h;
  416. break;
  417. default:
  418. dev_err(di->dev, "Unknown charger to charge from\n");
  419. break;
  420. }
  421. di->events.safety_timer_expired = false;
  422. hrtimer_set_expires_range(&di->safety_timer,
  423. ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
  424. ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
  425. hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
  426. }
  427. /**
  428. * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
  429. * @di: pointer to the abx500_chargalg structure
  430. *
  431. * The safety timer is stopped whenever the NORMAL state is exited
  432. */
  433. static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
  434. {
  435. if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
  436. di->events.safety_timer_expired = false;
  437. }
  438. /**
  439. * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
  440. * @di: pointer to the abx500_chargalg structure
  441. * @duration: duration of ther maintenance timer in hours
  442. *
  443. * The maintenance timer is used to maintain the charge in the battery once
  444. * the battery is considered full. These timers are chosen to match the
  445. * discharge curve of the battery
  446. */
  447. static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
  448. int duration)
  449. {
  450. hrtimer_set_expires_range(&di->maintenance_timer,
  451. ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
  452. ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
  453. di->events.maintenance_timer_expired = false;
  454. hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
  455. }
  456. /**
  457. * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
  458. * @di: pointer to the abx500_chargalg structure
  459. *
  460. * The maintenance timer is stopped whenever maintenance ends or when another
  461. * state is entered
  462. */
  463. static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
  464. {
  465. if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
  466. di->events.maintenance_timer_expired = false;
  467. }
  468. /**
  469. * abx500_chargalg_kick_watchdog() - Kick charger watchdog
  470. * @di: pointer to the abx500_chargalg structure
  471. *
  472. * The charger watchdog have to be kicked periodically whenever the charger is
  473. * on, else the ABB will reset the system
  474. */
  475. static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
  476. {
  477. /* Check if charger exists and kick watchdog if charging */
  478. if (di->ac_chg && di->ac_chg->ops.kick_wd &&
  479. di->chg_info.online_chg & AC_CHG) {
  480. /*
  481. * If AB charger watchdog expired, pm2xxx charging
  482. * gets disabled. To be safe, kick both AB charger watchdog
  483. * and pm2xxx watchdog.
  484. */
  485. if (di->ac_chg->external &&
  486. di->usb_chg && di->usb_chg->ops.kick_wd)
  487. di->usb_chg->ops.kick_wd(di->usb_chg);
  488. return di->ac_chg->ops.kick_wd(di->ac_chg);
  489. }
  490. else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
  491. di->chg_info.online_chg & USB_CHG)
  492. return di->usb_chg->ops.kick_wd(di->usb_chg);
  493. return -ENXIO;
  494. }
  495. /**
  496. * abx500_chargalg_ac_en() - Turn on/off the AC charger
  497. * @di: pointer to the abx500_chargalg structure
  498. * @enable: charger on/off
  499. * @vset: requested charger output voltage
  500. * @iset: requested charger output current
  501. *
  502. * The AC charger will be turned on/off with the requested charge voltage and
  503. * current
  504. */
  505. static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
  506. int vset, int iset)
  507. {
  508. static int abx500_chargalg_ex_ac_enable_toggle;
  509. if (!di->ac_chg || !di->ac_chg->ops.enable)
  510. return -ENXIO;
  511. /* Select maximum of what both the charger and the battery supports */
  512. if (di->ac_chg->max_out_volt)
  513. vset = min(vset, di->ac_chg->max_out_volt);
  514. if (di->ac_chg->max_out_curr)
  515. iset = min(iset, di->ac_chg->max_out_curr);
  516. di->chg_info.ac_iset = iset;
  517. di->chg_info.ac_vset = vset;
  518. /* Enable external charger */
  519. if (enable && di->ac_chg->external &&
  520. !abx500_chargalg_ex_ac_enable_toggle) {
  521. blocking_notifier_call_chain(&charger_notifier_list,
  522. 0, di->dev);
  523. abx500_chargalg_ex_ac_enable_toggle++;
  524. }
  525. return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
  526. }
  527. /**
  528. * abx500_chargalg_usb_en() - Turn on/off the USB charger
  529. * @di: pointer to the abx500_chargalg structure
  530. * @enable: charger on/off
  531. * @vset: requested charger output voltage
  532. * @iset: requested charger output current
  533. *
  534. * The USB charger will be turned on/off with the requested charge voltage and
  535. * current
  536. */
  537. static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
  538. int vset, int iset)
  539. {
  540. if (!di->usb_chg || !di->usb_chg->ops.enable)
  541. return -ENXIO;
  542. /* Select maximum of what both the charger and the battery supports */
  543. if (di->usb_chg->max_out_volt)
  544. vset = min(vset, di->usb_chg->max_out_volt);
  545. if (di->usb_chg->max_out_curr)
  546. iset = min(iset, di->usb_chg->max_out_curr);
  547. di->chg_info.usb_iset = iset;
  548. di->chg_info.usb_vset = vset;
  549. return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
  550. }
  551. /**
  552. * ab8540_chargalg_usb_pp_en() - Enable/ disable USB power path
  553. * @di: pointer to the abx500_chargalg structure
  554. * @enable: power path enable/disable
  555. *
  556. * The USB power path will be enable/ disable
  557. */
  558. static int ab8540_chargalg_usb_pp_en(struct abx500_chargalg *di, bool enable)
  559. {
  560. if (!di->usb_chg || !di->usb_chg->ops.pp_enable)
  561. return -ENXIO;
  562. return di->usb_chg->ops.pp_enable(di->usb_chg, enable);
  563. }
  564. /**
  565. * ab8540_chargalg_usb_pre_chg_en() - Enable/ disable USB pre-charge
  566. * @di: pointer to the abx500_chargalg structure
  567. * @enable: USB pre-charge enable/disable
  568. *
  569. * The USB USB pre-charge will be enable/ disable
  570. */
  571. static int ab8540_chargalg_usb_pre_chg_en(struct abx500_chargalg *di,
  572. bool enable)
  573. {
  574. if (!di->usb_chg || !di->usb_chg->ops.pre_chg_enable)
  575. return -ENXIO;
  576. return di->usb_chg->ops.pre_chg_enable(di->usb_chg, enable);
  577. }
  578. /**
  579. * abx500_chargalg_update_chg_curr() - Update charger current
  580. * @di: pointer to the abx500_chargalg structure
  581. * @iset: requested charger output current
  582. *
  583. * The charger output current will be updated for the charger
  584. * that is currently in use
  585. */
  586. static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
  587. int iset)
  588. {
  589. /* Check if charger exists and update current if charging */
  590. if (di->ac_chg && di->ac_chg->ops.update_curr &&
  591. di->chg_info.charger_type & AC_CHG) {
  592. /*
  593. * Select maximum of what both the charger
  594. * and the battery supports
  595. */
  596. if (di->ac_chg->max_out_curr)
  597. iset = min(iset, di->ac_chg->max_out_curr);
  598. di->chg_info.ac_iset = iset;
  599. return di->ac_chg->ops.update_curr(di->ac_chg, iset);
  600. } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
  601. di->chg_info.charger_type & USB_CHG) {
  602. /*
  603. * Select maximum of what both the charger
  604. * and the battery supports
  605. */
  606. if (di->usb_chg->max_out_curr)
  607. iset = min(iset, di->usb_chg->max_out_curr);
  608. di->chg_info.usb_iset = iset;
  609. return di->usb_chg->ops.update_curr(di->usb_chg, iset);
  610. }
  611. return -ENXIO;
  612. }
  613. /**
  614. * abx500_chargalg_stop_charging() - Stop charging
  615. * @di: pointer to the abx500_chargalg structure
  616. *
  617. * This function is called from any state where charging should be stopped.
  618. * All charging is disabled and all status parameters and timers are changed
  619. * accordingly
  620. */
  621. static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
  622. {
  623. abx500_chargalg_ac_en(di, false, 0, 0);
  624. abx500_chargalg_usb_en(di, false, 0, 0);
  625. abx500_chargalg_stop_safety_timer(di);
  626. abx500_chargalg_stop_maintenance_timer(di);
  627. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  628. di->maintenance_chg = false;
  629. cancel_delayed_work(&di->chargalg_wd_work);
  630. power_supply_changed(&di->chargalg_psy);
  631. }
  632. /**
  633. * abx500_chargalg_hold_charging() - Pauses charging
  634. * @di: pointer to the abx500_chargalg structure
  635. *
  636. * This function is called in the case where maintenance charging has been
  637. * disabled and instead a battery voltage mode is entered to check when the
  638. * battery voltage has reached a certain recharge voltage
  639. */
  640. static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
  641. {
  642. abx500_chargalg_ac_en(di, false, 0, 0);
  643. abx500_chargalg_usb_en(di, false, 0, 0);
  644. abx500_chargalg_stop_safety_timer(di);
  645. abx500_chargalg_stop_maintenance_timer(di);
  646. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  647. di->maintenance_chg = false;
  648. cancel_delayed_work(&di->chargalg_wd_work);
  649. power_supply_changed(&di->chargalg_psy);
  650. }
  651. /**
  652. * abx500_chargalg_start_charging() - Start the charger
  653. * @di: pointer to the abx500_chargalg structure
  654. * @vset: requested charger output voltage
  655. * @iset: requested charger output current
  656. *
  657. * A charger will be enabled depending on the requested charger type that was
  658. * detected previously.
  659. */
  660. static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
  661. int vset, int iset)
  662. {
  663. switch (di->chg_info.charger_type) {
  664. case AC_CHG:
  665. dev_dbg(di->dev,
  666. "AC parameters: Vset %d, Ich %d\n", vset, iset);
  667. abx500_chargalg_usb_en(di, false, 0, 0);
  668. abx500_chargalg_ac_en(di, true, vset, iset);
  669. break;
  670. case USB_CHG:
  671. dev_dbg(di->dev,
  672. "USB parameters: Vset %d, Ich %d\n", vset, iset);
  673. abx500_chargalg_ac_en(di, false, 0, 0);
  674. abx500_chargalg_usb_en(di, true, vset, iset);
  675. break;
  676. default:
  677. dev_err(di->dev, "Unknown charger to charge from\n");
  678. break;
  679. }
  680. }
  681. /**
  682. * abx500_chargalg_check_temp() - Check battery temperature ranges
  683. * @di: pointer to the abx500_chargalg structure
  684. *
  685. * The battery temperature is checked against the predefined limits and the
  686. * charge state is changed accordingly
  687. */
  688. static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
  689. {
  690. if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
  691. di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
  692. /* Temp OK! */
  693. di->events.btemp_underover = false;
  694. di->events.btemp_lowhigh = false;
  695. di->t_hyst_norm = 0;
  696. di->t_hyst_lowhigh = 0;
  697. } else {
  698. if (((di->batt_data.temp >= di->bm->temp_high) &&
  699. (di->batt_data.temp <
  700. (di->bm->temp_over - di->t_hyst_lowhigh))) ||
  701. ((di->batt_data.temp >
  702. (di->bm->temp_under + di->t_hyst_lowhigh)) &&
  703. (di->batt_data.temp <= di->bm->temp_low))) {
  704. /* TEMP minor!!!!! */
  705. di->events.btemp_underover = false;
  706. di->events.btemp_lowhigh = true;
  707. di->t_hyst_norm = di->bm->temp_hysteresis;
  708. di->t_hyst_lowhigh = 0;
  709. } else if (di->batt_data.temp <= di->bm->temp_under ||
  710. di->batt_data.temp >= di->bm->temp_over) {
  711. /* TEMP major!!!!! */
  712. di->events.btemp_underover = true;
  713. di->events.btemp_lowhigh = false;
  714. di->t_hyst_norm = 0;
  715. di->t_hyst_lowhigh = di->bm->temp_hysteresis;
  716. } else {
  717. /* Within hysteresis */
  718. dev_dbg(di->dev, "Within hysteresis limit temp: %d "
  719. "hyst_lowhigh %d, hyst normal %d\n",
  720. di->batt_data.temp, di->t_hyst_lowhigh,
  721. di->t_hyst_norm);
  722. }
  723. }
  724. }
  725. /**
  726. * abx500_chargalg_check_charger_voltage() - Check charger voltage
  727. * @di: pointer to the abx500_chargalg structure
  728. *
  729. * Charger voltage is checked against maximum limit
  730. */
  731. static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
  732. {
  733. if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
  734. di->chg_info.usb_chg_ok = false;
  735. else
  736. di->chg_info.usb_chg_ok = true;
  737. if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
  738. di->chg_info.ac_chg_ok = false;
  739. else
  740. di->chg_info.ac_chg_ok = true;
  741. }
  742. /**
  743. * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
  744. * @di: pointer to the abx500_chargalg structure
  745. *
  746. * End-of-charge criteria is fulfilled when the battery voltage is above a
  747. * certain limit and the battery current is below a certain limit for a
  748. * predefined number of consecutive seconds. If true, the battery is full
  749. */
  750. static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
  751. {
  752. if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
  753. di->charge_state == STATE_NORMAL &&
  754. !di->maintenance_chg && (di->batt_data.volt >=
  755. di->bm->bat_type[di->bm->batt_id].termination_vol ||
  756. di->events.usb_cv_active || di->events.ac_cv_active) &&
  757. di->batt_data.avg_curr <
  758. di->bm->bat_type[di->bm->batt_id].termination_curr &&
  759. di->batt_data.avg_curr > 0) {
  760. if (++di->eoc_cnt >= EOC_COND_CNT) {
  761. di->eoc_cnt = 0;
  762. if ((di->chg_info.charger_type & USB_CHG) &&
  763. (di->usb_chg->power_path))
  764. ab8540_chargalg_usb_pp_en(di, true);
  765. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  766. di->maintenance_chg = true;
  767. dev_dbg(di->dev, "EOC reached!\n");
  768. power_supply_changed(&di->chargalg_psy);
  769. } else {
  770. dev_dbg(di->dev,
  771. " EOC limit reached for the %d"
  772. " time, out of %d before EOC\n",
  773. di->eoc_cnt,
  774. EOC_COND_CNT);
  775. }
  776. } else {
  777. di->eoc_cnt = 0;
  778. }
  779. }
  780. static void init_maxim_chg_curr(struct abx500_chargalg *di)
  781. {
  782. di->ccm.original_iset =
  783. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
  784. di->ccm.current_iset =
  785. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
  786. di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
  787. di->ccm.max_current = di->bm->maxi->chg_curr;
  788. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  789. di->ccm.level = 0;
  790. }
  791. /**
  792. * abx500_chargalg_chg_curr_maxim - increases the charger current to
  793. * compensate for the system load
  794. * @di pointer to the abx500_chargalg structure
  795. *
  796. * This maximization function is used to raise the charger current to get the
  797. * battery current as close to the optimal value as possible. The battery
  798. * current during charging is affected by the system load
  799. */
  800. static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
  801. {
  802. int delta_i;
  803. if (!di->bm->maxi->ena_maxi)
  804. return MAXIM_RET_NOACTION;
  805. delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
  806. if (di->events.vbus_collapsed) {
  807. dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
  808. di->ccm.wait_cnt);
  809. if (di->ccm.wait_cnt == 0) {
  810. dev_dbg(di->dev, "lowering current\n");
  811. di->ccm.wait_cnt++;
  812. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  813. di->ccm.max_current =
  814. di->ccm.current_iset - di->ccm.test_delta_i;
  815. di->ccm.current_iset = di->ccm.max_current;
  816. di->ccm.level--;
  817. return MAXIM_RET_CHANGE;
  818. } else {
  819. dev_dbg(di->dev, "waiting\n");
  820. /* Let's go in here twice before lowering curr again */
  821. di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
  822. return MAXIM_RET_NOACTION;
  823. }
  824. }
  825. di->ccm.wait_cnt = 0;
  826. if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
  827. dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
  828. " (limit %dmA) (current iset: %dmA)!\n",
  829. di->batt_data.inst_curr, di->ccm.original_iset,
  830. di->ccm.current_iset);
  831. if (di->ccm.current_iset == di->ccm.original_iset)
  832. return MAXIM_RET_NOACTION;
  833. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  834. di->ccm.current_iset = di->ccm.original_iset;
  835. di->ccm.level = 0;
  836. return MAXIM_RET_IBAT_TOO_HIGH;
  837. }
  838. if (delta_i > di->ccm.test_delta_i &&
  839. (di->ccm.current_iset + di->ccm.test_delta_i) <
  840. di->ccm.max_current) {
  841. if (di->ccm.condition_cnt-- == 0) {
  842. /* Increse the iset with cco.test_delta_i */
  843. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  844. di->ccm.current_iset += di->ccm.test_delta_i;
  845. di->ccm.level++;
  846. dev_dbg(di->dev, " Maximization needed, increase"
  847. " with %d mA to %dmA (Optimal ibat: %d)"
  848. " Level %d\n",
  849. di->ccm.test_delta_i,
  850. di->ccm.current_iset,
  851. di->ccm.original_iset,
  852. di->ccm.level);
  853. return MAXIM_RET_CHANGE;
  854. } else {
  855. return MAXIM_RET_NOACTION;
  856. }
  857. } else {
  858. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  859. return MAXIM_RET_NOACTION;
  860. }
  861. }
  862. static void handle_maxim_chg_curr(struct abx500_chargalg *di)
  863. {
  864. enum maxim_ret ret;
  865. int result;
  866. ret = abx500_chargalg_chg_curr_maxim(di);
  867. switch (ret) {
  868. case MAXIM_RET_CHANGE:
  869. result = abx500_chargalg_update_chg_curr(di,
  870. di->ccm.current_iset);
  871. if (result)
  872. dev_err(di->dev, "failed to set chg curr\n");
  873. break;
  874. case MAXIM_RET_IBAT_TOO_HIGH:
  875. result = abx500_chargalg_update_chg_curr(di,
  876. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
  877. if (result)
  878. dev_err(di->dev, "failed to set chg curr\n");
  879. break;
  880. case MAXIM_RET_NOACTION:
  881. default:
  882. /* Do nothing..*/
  883. break;
  884. }
  885. }
  886. static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
  887. {
  888. struct power_supply *psy;
  889. struct power_supply *ext;
  890. struct abx500_chargalg *di;
  891. union power_supply_propval ret;
  892. int i, j;
  893. bool psy_found = false;
  894. bool capacity_updated = false;
  895. psy = (struct power_supply *)data;
  896. ext = dev_get_drvdata(dev);
  897. di = to_abx500_chargalg_device_info(psy);
  898. /* For all psy where the driver name appears in any supplied_to */
  899. for (i = 0; i < ext->num_supplicants; i++) {
  900. if (!strcmp(ext->supplied_to[i], psy->name))
  901. psy_found = true;
  902. }
  903. if (!psy_found)
  904. return 0;
  905. /*
  906. * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
  907. * property because of handling that sysfs entry on its own, this is
  908. * the place to get the battery capacity.
  909. */
  910. if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
  911. di->batt_data.percent = ret.intval;
  912. capacity_updated = true;
  913. }
  914. /* Go through all properties for the psy */
  915. for (j = 0; j < ext->num_properties; j++) {
  916. enum power_supply_property prop;
  917. prop = ext->properties[j];
  918. /* Initialize chargers if not already done */
  919. if (!di->ac_chg &&
  920. ext->type == POWER_SUPPLY_TYPE_MAINS)
  921. di->ac_chg = psy_to_ux500_charger(ext);
  922. else if (!di->usb_chg &&
  923. ext->type == POWER_SUPPLY_TYPE_USB)
  924. di->usb_chg = psy_to_ux500_charger(ext);
  925. if (ext->get_property(ext, prop, &ret))
  926. continue;
  927. switch (prop) {
  928. case POWER_SUPPLY_PROP_PRESENT:
  929. switch (ext->type) {
  930. case POWER_SUPPLY_TYPE_BATTERY:
  931. /* Battery present */
  932. if (ret.intval)
  933. di->events.batt_rem = false;
  934. /* Battery removed */
  935. else
  936. di->events.batt_rem = true;
  937. break;
  938. case POWER_SUPPLY_TYPE_MAINS:
  939. /* AC disconnected */
  940. if (!ret.intval &&
  941. (di->chg_info.conn_chg & AC_CHG)) {
  942. di->chg_info.prev_conn_chg =
  943. di->chg_info.conn_chg;
  944. di->chg_info.conn_chg &= ~AC_CHG;
  945. }
  946. /* AC connected */
  947. else if (ret.intval &&
  948. !(di->chg_info.conn_chg & AC_CHG)) {
  949. di->chg_info.prev_conn_chg =
  950. di->chg_info.conn_chg;
  951. di->chg_info.conn_chg |= AC_CHG;
  952. }
  953. break;
  954. case POWER_SUPPLY_TYPE_USB:
  955. /* USB disconnected */
  956. if (!ret.intval &&
  957. (di->chg_info.conn_chg & USB_CHG)) {
  958. di->chg_info.prev_conn_chg =
  959. di->chg_info.conn_chg;
  960. di->chg_info.conn_chg &= ~USB_CHG;
  961. }
  962. /* USB connected */
  963. else if (ret.intval &&
  964. !(di->chg_info.conn_chg & USB_CHG)) {
  965. di->chg_info.prev_conn_chg =
  966. di->chg_info.conn_chg;
  967. di->chg_info.conn_chg |= USB_CHG;
  968. }
  969. break;
  970. default:
  971. break;
  972. }
  973. break;
  974. case POWER_SUPPLY_PROP_ONLINE:
  975. switch (ext->type) {
  976. case POWER_SUPPLY_TYPE_BATTERY:
  977. break;
  978. case POWER_SUPPLY_TYPE_MAINS:
  979. /* AC offline */
  980. if (!ret.intval &&
  981. (di->chg_info.online_chg & AC_CHG)) {
  982. di->chg_info.prev_online_chg =
  983. di->chg_info.online_chg;
  984. di->chg_info.online_chg &= ~AC_CHG;
  985. }
  986. /* AC online */
  987. else if (ret.intval &&
  988. !(di->chg_info.online_chg & AC_CHG)) {
  989. di->chg_info.prev_online_chg =
  990. di->chg_info.online_chg;
  991. di->chg_info.online_chg |= AC_CHG;
  992. queue_delayed_work(di->chargalg_wq,
  993. &di->chargalg_wd_work, 0);
  994. }
  995. break;
  996. case POWER_SUPPLY_TYPE_USB:
  997. /* USB offline */
  998. if (!ret.intval &&
  999. (di->chg_info.online_chg & USB_CHG)) {
  1000. di->chg_info.prev_online_chg =
  1001. di->chg_info.online_chg;
  1002. di->chg_info.online_chg &= ~USB_CHG;
  1003. }
  1004. /* USB online */
  1005. else if (ret.intval &&
  1006. !(di->chg_info.online_chg & USB_CHG)) {
  1007. di->chg_info.prev_online_chg =
  1008. di->chg_info.online_chg;
  1009. di->chg_info.online_chg |= USB_CHG;
  1010. queue_delayed_work(di->chargalg_wq,
  1011. &di->chargalg_wd_work, 0);
  1012. }
  1013. break;
  1014. default:
  1015. break;
  1016. }
  1017. break;
  1018. case POWER_SUPPLY_PROP_HEALTH:
  1019. switch (ext->type) {
  1020. case POWER_SUPPLY_TYPE_BATTERY:
  1021. break;
  1022. case POWER_SUPPLY_TYPE_MAINS:
  1023. switch (ret.intval) {
  1024. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  1025. di->events.mainextchnotok = true;
  1026. di->events.main_thermal_prot = false;
  1027. di->events.main_ovv = false;
  1028. di->events.ac_wd_expired = false;
  1029. break;
  1030. case POWER_SUPPLY_HEALTH_DEAD:
  1031. di->events.ac_wd_expired = true;
  1032. di->events.mainextchnotok = false;
  1033. di->events.main_ovv = false;
  1034. di->events.main_thermal_prot = false;
  1035. break;
  1036. case POWER_SUPPLY_HEALTH_COLD:
  1037. case POWER_SUPPLY_HEALTH_OVERHEAT:
  1038. di->events.main_thermal_prot = true;
  1039. di->events.mainextchnotok = false;
  1040. di->events.main_ovv = false;
  1041. di->events.ac_wd_expired = false;
  1042. break;
  1043. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  1044. di->events.main_ovv = true;
  1045. di->events.mainextchnotok = false;
  1046. di->events.main_thermal_prot = false;
  1047. di->events.ac_wd_expired = false;
  1048. break;
  1049. case POWER_SUPPLY_HEALTH_GOOD:
  1050. di->events.main_thermal_prot = false;
  1051. di->events.mainextchnotok = false;
  1052. di->events.main_ovv = false;
  1053. di->events.ac_wd_expired = false;
  1054. break;
  1055. default:
  1056. break;
  1057. }
  1058. break;
  1059. case POWER_SUPPLY_TYPE_USB:
  1060. switch (ret.intval) {
  1061. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  1062. di->events.usbchargernotok = true;
  1063. di->events.usb_thermal_prot = false;
  1064. di->events.vbus_ovv = false;
  1065. di->events.usb_wd_expired = false;
  1066. break;
  1067. case POWER_SUPPLY_HEALTH_DEAD:
  1068. di->events.usb_wd_expired = true;
  1069. di->events.usbchargernotok = false;
  1070. di->events.usb_thermal_prot = false;
  1071. di->events.vbus_ovv = false;
  1072. break;
  1073. case POWER_SUPPLY_HEALTH_COLD:
  1074. case POWER_SUPPLY_HEALTH_OVERHEAT:
  1075. di->events.usb_thermal_prot = true;
  1076. di->events.usbchargernotok = false;
  1077. di->events.vbus_ovv = false;
  1078. di->events.usb_wd_expired = false;
  1079. break;
  1080. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  1081. di->events.vbus_ovv = true;
  1082. di->events.usbchargernotok = false;
  1083. di->events.usb_thermal_prot = false;
  1084. di->events.usb_wd_expired = false;
  1085. break;
  1086. case POWER_SUPPLY_HEALTH_GOOD:
  1087. di->events.usbchargernotok = false;
  1088. di->events.usb_thermal_prot = false;
  1089. di->events.vbus_ovv = false;
  1090. di->events.usb_wd_expired = false;
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. default:
  1096. break;
  1097. }
  1098. break;
  1099. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1100. switch (ext->type) {
  1101. case POWER_SUPPLY_TYPE_BATTERY:
  1102. di->batt_data.volt = ret.intval / 1000;
  1103. break;
  1104. case POWER_SUPPLY_TYPE_MAINS:
  1105. di->chg_info.ac_volt = ret.intval / 1000;
  1106. break;
  1107. case POWER_SUPPLY_TYPE_USB:
  1108. di->chg_info.usb_volt = ret.intval / 1000;
  1109. break;
  1110. default:
  1111. break;
  1112. }
  1113. break;
  1114. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  1115. switch (ext->type) {
  1116. case POWER_SUPPLY_TYPE_MAINS:
  1117. /* AVG is used to indicate when we are
  1118. * in CV mode */
  1119. if (ret.intval)
  1120. di->events.ac_cv_active = true;
  1121. else
  1122. di->events.ac_cv_active = false;
  1123. break;
  1124. case POWER_SUPPLY_TYPE_USB:
  1125. /* AVG is used to indicate when we are
  1126. * in CV mode */
  1127. if (ret.intval)
  1128. di->events.usb_cv_active = true;
  1129. else
  1130. di->events.usb_cv_active = false;
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. break;
  1136. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1137. switch (ext->type) {
  1138. case POWER_SUPPLY_TYPE_BATTERY:
  1139. if (ret.intval)
  1140. di->events.batt_unknown = false;
  1141. else
  1142. di->events.batt_unknown = true;
  1143. break;
  1144. default:
  1145. break;
  1146. }
  1147. break;
  1148. case POWER_SUPPLY_PROP_TEMP:
  1149. di->batt_data.temp = ret.intval / 10;
  1150. break;
  1151. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1152. switch (ext->type) {
  1153. case POWER_SUPPLY_TYPE_MAINS:
  1154. di->chg_info.ac_curr =
  1155. ret.intval / 1000;
  1156. break;
  1157. case POWER_SUPPLY_TYPE_USB:
  1158. di->chg_info.usb_curr =
  1159. ret.intval / 1000;
  1160. break;
  1161. case POWER_SUPPLY_TYPE_BATTERY:
  1162. di->batt_data.inst_curr = ret.intval / 1000;
  1163. break;
  1164. default:
  1165. break;
  1166. }
  1167. break;
  1168. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1169. switch (ext->type) {
  1170. case POWER_SUPPLY_TYPE_BATTERY:
  1171. di->batt_data.avg_curr = ret.intval / 1000;
  1172. break;
  1173. case POWER_SUPPLY_TYPE_USB:
  1174. if (ret.intval)
  1175. di->events.vbus_collapsed = true;
  1176. else
  1177. di->events.vbus_collapsed = false;
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. break;
  1183. case POWER_SUPPLY_PROP_CAPACITY:
  1184. if (!capacity_updated)
  1185. di->batt_data.percent = ret.intval;
  1186. break;
  1187. default:
  1188. break;
  1189. }
  1190. }
  1191. return 0;
  1192. }
  1193. /**
  1194. * abx500_chargalg_external_power_changed() - callback for power supply changes
  1195. * @psy: pointer to the structure power_supply
  1196. *
  1197. * This function is the entry point of the pointer external_power_changed
  1198. * of the structure power_supply.
  1199. * This function gets executed when there is a change in any external power
  1200. * supply that this driver needs to be notified of.
  1201. */
  1202. static void abx500_chargalg_external_power_changed(struct power_supply *psy)
  1203. {
  1204. struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
  1205. /*
  1206. * Trigger execution of the algorithm instantly and read
  1207. * all power_supply properties there instead
  1208. */
  1209. queue_work(di->chargalg_wq, &di->chargalg_work);
  1210. }
  1211. /**
  1212. * abx500_chargalg_algorithm() - Main function for the algorithm
  1213. * @di: pointer to the abx500_chargalg structure
  1214. *
  1215. * This is the main control function for the charging algorithm.
  1216. * It is called periodically or when something happens that will
  1217. * trigger a state change
  1218. */
  1219. static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
  1220. {
  1221. int charger_status;
  1222. int ret;
  1223. int curr_step_lvl;
  1224. /* Collect data from all power_supply class devices */
  1225. class_for_each_device(power_supply_class, NULL,
  1226. &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
  1227. abx500_chargalg_end_of_charge(di);
  1228. abx500_chargalg_check_temp(di);
  1229. abx500_chargalg_check_charger_voltage(di);
  1230. charger_status = abx500_chargalg_check_charger_connection(di);
  1231. abx500_chargalg_check_current_step_status(di);
  1232. if (is_ab8500(di->parent)) {
  1233. ret = abx500_chargalg_check_charger_enable(di);
  1234. if (ret < 0)
  1235. dev_err(di->dev, "Checking charger is enabled error"
  1236. ": Returned Value %d\n", ret);
  1237. }
  1238. /*
  1239. * First check if we have a charger connected.
  1240. * Also we don't allow charging of unknown batteries if configured
  1241. * this way
  1242. */
  1243. if (!charger_status ||
  1244. (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
  1245. if (di->charge_state != STATE_HANDHELD) {
  1246. di->events.safety_timer_expired = false;
  1247. abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
  1248. }
  1249. }
  1250. /* If suspended, we should not continue checking the flags */
  1251. else if (di->charge_state == STATE_SUSPENDED_INIT ||
  1252. di->charge_state == STATE_SUSPENDED) {
  1253. /* We don't do anything here, just don,t continue */
  1254. }
  1255. /* Safety timer expiration */
  1256. else if (di->events.safety_timer_expired) {
  1257. if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
  1258. abx500_chargalg_state_to(di,
  1259. STATE_SAFETY_TIMER_EXPIRED_INIT);
  1260. }
  1261. /*
  1262. * Check if any interrupts has occured
  1263. * that will prevent us from charging
  1264. */
  1265. /* Battery removed */
  1266. else if (di->events.batt_rem) {
  1267. if (di->charge_state != STATE_BATT_REMOVED)
  1268. abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
  1269. }
  1270. /* Main or USB charger not ok. */
  1271. else if (di->events.mainextchnotok || di->events.usbchargernotok) {
  1272. /*
  1273. * If vbus_collapsed is set, we have to lower the charger
  1274. * current, which is done in the normal state below
  1275. */
  1276. if (di->charge_state != STATE_CHG_NOT_OK &&
  1277. !di->events.vbus_collapsed)
  1278. abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
  1279. }
  1280. /* VBUS, Main or VBAT OVV. */
  1281. else if (di->events.vbus_ovv ||
  1282. di->events.main_ovv ||
  1283. di->events.batt_ovv ||
  1284. !di->chg_info.usb_chg_ok ||
  1285. !di->chg_info.ac_chg_ok) {
  1286. if (di->charge_state != STATE_OVV_PROTECT)
  1287. abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
  1288. }
  1289. /* USB Thermal, stop charging */
  1290. else if (di->events.main_thermal_prot ||
  1291. di->events.usb_thermal_prot) {
  1292. if (di->charge_state != STATE_HW_TEMP_PROTECT)
  1293. abx500_chargalg_state_to(di,
  1294. STATE_HW_TEMP_PROTECT_INIT);
  1295. }
  1296. /* Battery temp over/under */
  1297. else if (di->events.btemp_underover) {
  1298. if (di->charge_state != STATE_TEMP_UNDEROVER)
  1299. abx500_chargalg_state_to(di,
  1300. STATE_TEMP_UNDEROVER_INIT);
  1301. }
  1302. /* Watchdog expired */
  1303. else if (di->events.ac_wd_expired ||
  1304. di->events.usb_wd_expired) {
  1305. if (di->charge_state != STATE_WD_EXPIRED)
  1306. abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
  1307. }
  1308. /* Battery temp high/low */
  1309. else if (di->events.btemp_lowhigh) {
  1310. if (di->charge_state != STATE_TEMP_LOWHIGH)
  1311. abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
  1312. }
  1313. dev_dbg(di->dev,
  1314. "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
  1315. "State %s Active_chg %d Chg_status %d AC %d USB %d "
  1316. "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
  1317. "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
  1318. di->batt_data.volt,
  1319. di->batt_data.avg_curr,
  1320. di->batt_data.inst_curr,
  1321. di->batt_data.temp,
  1322. di->batt_data.percent,
  1323. di->maintenance_chg,
  1324. states[di->charge_state],
  1325. di->chg_info.charger_type,
  1326. di->charge_status,
  1327. di->chg_info.conn_chg & AC_CHG,
  1328. di->chg_info.conn_chg & USB_CHG,
  1329. di->chg_info.online_chg & AC_CHG,
  1330. di->chg_info.online_chg & USB_CHG,
  1331. di->events.ac_cv_active,
  1332. di->events.usb_cv_active,
  1333. di->chg_info.ac_curr,
  1334. di->chg_info.usb_curr,
  1335. di->chg_info.ac_vset,
  1336. di->chg_info.ac_iset,
  1337. di->chg_info.usb_vset,
  1338. di->chg_info.usb_iset);
  1339. switch (di->charge_state) {
  1340. case STATE_HANDHELD_INIT:
  1341. abx500_chargalg_stop_charging(di);
  1342. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  1343. abx500_chargalg_state_to(di, STATE_HANDHELD);
  1344. /* Intentional fallthrough */
  1345. case STATE_HANDHELD:
  1346. break;
  1347. case STATE_SUSPENDED_INIT:
  1348. if (di->susp_status.ac_suspended)
  1349. abx500_chargalg_ac_en(di, false, 0, 0);
  1350. if (di->susp_status.usb_suspended)
  1351. abx500_chargalg_usb_en(di, false, 0, 0);
  1352. abx500_chargalg_stop_safety_timer(di);
  1353. abx500_chargalg_stop_maintenance_timer(di);
  1354. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1355. di->maintenance_chg = false;
  1356. abx500_chargalg_state_to(di, STATE_SUSPENDED);
  1357. power_supply_changed(&di->chargalg_psy);
  1358. /* Intentional fallthrough */
  1359. case STATE_SUSPENDED:
  1360. /* CHARGING is suspended */
  1361. break;
  1362. case STATE_BATT_REMOVED_INIT:
  1363. abx500_chargalg_stop_charging(di);
  1364. abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
  1365. /* Intentional fallthrough */
  1366. case STATE_BATT_REMOVED:
  1367. if (!di->events.batt_rem)
  1368. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1369. break;
  1370. case STATE_HW_TEMP_PROTECT_INIT:
  1371. abx500_chargalg_stop_charging(di);
  1372. abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
  1373. /* Intentional fallthrough */
  1374. case STATE_HW_TEMP_PROTECT:
  1375. if (!di->events.main_thermal_prot &&
  1376. !di->events.usb_thermal_prot)
  1377. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1378. break;
  1379. case STATE_OVV_PROTECT_INIT:
  1380. abx500_chargalg_stop_charging(di);
  1381. abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
  1382. /* Intentional fallthrough */
  1383. case STATE_OVV_PROTECT:
  1384. if (!di->events.vbus_ovv &&
  1385. !di->events.main_ovv &&
  1386. !di->events.batt_ovv &&
  1387. di->chg_info.usb_chg_ok &&
  1388. di->chg_info.ac_chg_ok)
  1389. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1390. break;
  1391. case STATE_CHG_NOT_OK_INIT:
  1392. abx500_chargalg_stop_charging(di);
  1393. abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
  1394. /* Intentional fallthrough */
  1395. case STATE_CHG_NOT_OK:
  1396. if (!di->events.mainextchnotok &&
  1397. !di->events.usbchargernotok)
  1398. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1399. break;
  1400. case STATE_SAFETY_TIMER_EXPIRED_INIT:
  1401. abx500_chargalg_stop_charging(di);
  1402. abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
  1403. /* Intentional fallthrough */
  1404. case STATE_SAFETY_TIMER_EXPIRED:
  1405. /* We exit this state when charger is removed */
  1406. break;
  1407. case STATE_NORMAL_INIT:
  1408. if ((di->chg_info.charger_type & USB_CHG) &&
  1409. di->usb_chg->power_path) {
  1410. if (di->batt_data.volt >
  1411. (di->bm->fg_params->lowbat_threshold +
  1412. BAT_PLUS_MARGIN)) {
  1413. ab8540_chargalg_usb_pre_chg_en(di, false);
  1414. ab8540_chargalg_usb_pp_en(di, false);
  1415. } else {
  1416. ab8540_chargalg_usb_pp_en(di, true);
  1417. ab8540_chargalg_usb_pre_chg_en(di, true);
  1418. abx500_chargalg_state_to(di,
  1419. STATE_USB_PP_PRE_CHARGE);
  1420. break;
  1421. }
  1422. }
  1423. if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
  1424. abx500_chargalg_stop_charging(di);
  1425. else {
  1426. curr_step_lvl = di->bm->bat_type[
  1427. di->bm->batt_id].normal_cur_lvl
  1428. * di->curr_status.curr_step
  1429. / CHARGALG_CURR_STEP_HIGH;
  1430. abx500_chargalg_start_charging(di,
  1431. di->bm->bat_type[di->bm->batt_id]
  1432. .normal_vol_lvl, curr_step_lvl);
  1433. }
  1434. abx500_chargalg_state_to(di, STATE_NORMAL);
  1435. abx500_chargalg_start_safety_timer(di);
  1436. abx500_chargalg_stop_maintenance_timer(di);
  1437. init_maxim_chg_curr(di);
  1438. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1439. di->eoc_cnt = 0;
  1440. di->maintenance_chg = false;
  1441. power_supply_changed(&di->chargalg_psy);
  1442. break;
  1443. case STATE_USB_PP_PRE_CHARGE:
  1444. if (di->batt_data.volt >
  1445. (di->bm->fg_params->lowbat_threshold +
  1446. BAT_PLUS_MARGIN))
  1447. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1448. break;
  1449. case STATE_NORMAL:
  1450. handle_maxim_chg_curr(di);
  1451. if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
  1452. di->maintenance_chg) {
  1453. if (di->bm->no_maintenance)
  1454. abx500_chargalg_state_to(di,
  1455. STATE_WAIT_FOR_RECHARGE_INIT);
  1456. else
  1457. abx500_chargalg_state_to(di,
  1458. STATE_MAINTENANCE_A_INIT);
  1459. }
  1460. break;
  1461. /* This state will be used when the maintenance state is disabled */
  1462. case STATE_WAIT_FOR_RECHARGE_INIT:
  1463. abx500_chargalg_hold_charging(di);
  1464. abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
  1465. /* Intentional fallthrough */
  1466. case STATE_WAIT_FOR_RECHARGE:
  1467. if (di->batt_data.percent <=
  1468. di->bm->bat_type[di->bm->batt_id].
  1469. recharge_cap)
  1470. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1471. break;
  1472. case STATE_MAINTENANCE_A_INIT:
  1473. abx500_chargalg_stop_safety_timer(di);
  1474. abx500_chargalg_start_maintenance_timer(di,
  1475. di->bm->bat_type[
  1476. di->bm->batt_id].maint_a_chg_timer_h);
  1477. abx500_chargalg_start_charging(di,
  1478. di->bm->bat_type[
  1479. di->bm->batt_id].maint_a_vol_lvl,
  1480. di->bm->bat_type[
  1481. di->bm->batt_id].maint_a_cur_lvl);
  1482. abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
  1483. power_supply_changed(&di->chargalg_psy);
  1484. /* Intentional fallthrough*/
  1485. case STATE_MAINTENANCE_A:
  1486. if (di->events.maintenance_timer_expired) {
  1487. abx500_chargalg_stop_maintenance_timer(di);
  1488. abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
  1489. }
  1490. break;
  1491. case STATE_MAINTENANCE_B_INIT:
  1492. abx500_chargalg_start_maintenance_timer(di,
  1493. di->bm->bat_type[
  1494. di->bm->batt_id].maint_b_chg_timer_h);
  1495. abx500_chargalg_start_charging(di,
  1496. di->bm->bat_type[
  1497. di->bm->batt_id].maint_b_vol_lvl,
  1498. di->bm->bat_type[
  1499. di->bm->batt_id].maint_b_cur_lvl);
  1500. abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
  1501. power_supply_changed(&di->chargalg_psy);
  1502. /* Intentional fallthrough*/
  1503. case STATE_MAINTENANCE_B:
  1504. if (di->events.maintenance_timer_expired) {
  1505. abx500_chargalg_stop_maintenance_timer(di);
  1506. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1507. }
  1508. break;
  1509. case STATE_TEMP_LOWHIGH_INIT:
  1510. abx500_chargalg_start_charging(di,
  1511. di->bm->bat_type[
  1512. di->bm->batt_id].low_high_vol_lvl,
  1513. di->bm->bat_type[
  1514. di->bm->batt_id].low_high_cur_lvl);
  1515. abx500_chargalg_stop_maintenance_timer(di);
  1516. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1517. abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
  1518. power_supply_changed(&di->chargalg_psy);
  1519. /* Intentional fallthrough */
  1520. case STATE_TEMP_LOWHIGH:
  1521. if (!di->events.btemp_lowhigh)
  1522. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1523. break;
  1524. case STATE_WD_EXPIRED_INIT:
  1525. abx500_chargalg_stop_charging(di);
  1526. abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
  1527. /* Intentional fallthrough */
  1528. case STATE_WD_EXPIRED:
  1529. if (!di->events.ac_wd_expired &&
  1530. !di->events.usb_wd_expired)
  1531. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1532. break;
  1533. case STATE_TEMP_UNDEROVER_INIT:
  1534. abx500_chargalg_stop_charging(di);
  1535. abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
  1536. /* Intentional fallthrough */
  1537. case STATE_TEMP_UNDEROVER:
  1538. if (!di->events.btemp_underover)
  1539. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1540. break;
  1541. }
  1542. /* Start charging directly if the new state is a charge state */
  1543. if (di->charge_state == STATE_NORMAL_INIT ||
  1544. di->charge_state == STATE_MAINTENANCE_A_INIT ||
  1545. di->charge_state == STATE_MAINTENANCE_B_INIT)
  1546. queue_work(di->chargalg_wq, &di->chargalg_work);
  1547. }
  1548. /**
  1549. * abx500_chargalg_periodic_work() - Periodic work for the algorithm
  1550. * @work: pointer to the work_struct structure
  1551. *
  1552. * Work queue function for the charging algorithm
  1553. */
  1554. static void abx500_chargalg_periodic_work(struct work_struct *work)
  1555. {
  1556. struct abx500_chargalg *di = container_of(work,
  1557. struct abx500_chargalg, chargalg_periodic_work.work);
  1558. abx500_chargalg_algorithm(di);
  1559. /*
  1560. * If a charger is connected then the battery has to be monitored
  1561. * frequently, else the work can be delayed.
  1562. */
  1563. if (di->chg_info.conn_chg)
  1564. queue_delayed_work(di->chargalg_wq,
  1565. &di->chargalg_periodic_work,
  1566. di->bm->interval_charging * HZ);
  1567. else
  1568. queue_delayed_work(di->chargalg_wq,
  1569. &di->chargalg_periodic_work,
  1570. di->bm->interval_not_charging * HZ);
  1571. }
  1572. /**
  1573. * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
  1574. * @work: pointer to the work_struct structure
  1575. *
  1576. * Work queue function for kicking the charger watchdog
  1577. */
  1578. static void abx500_chargalg_wd_work(struct work_struct *work)
  1579. {
  1580. int ret;
  1581. struct abx500_chargalg *di = container_of(work,
  1582. struct abx500_chargalg, chargalg_wd_work.work);
  1583. dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
  1584. ret = abx500_chargalg_kick_watchdog(di);
  1585. if (ret < 0)
  1586. dev_err(di->dev, "failed to kick watchdog\n");
  1587. queue_delayed_work(di->chargalg_wq,
  1588. &di->chargalg_wd_work, CHG_WD_INTERVAL);
  1589. }
  1590. /**
  1591. * abx500_chargalg_work() - Work to run the charging algorithm instantly
  1592. * @work: pointer to the work_struct structure
  1593. *
  1594. * Work queue function for calling the charging algorithm
  1595. */
  1596. static void abx500_chargalg_work(struct work_struct *work)
  1597. {
  1598. struct abx500_chargalg *di = container_of(work,
  1599. struct abx500_chargalg, chargalg_work);
  1600. abx500_chargalg_algorithm(di);
  1601. }
  1602. /**
  1603. * abx500_chargalg_get_property() - get the chargalg properties
  1604. * @psy: pointer to the power_supply structure
  1605. * @psp: pointer to the power_supply_property structure
  1606. * @val: pointer to the power_supply_propval union
  1607. *
  1608. * This function gets called when an application tries to get the
  1609. * chargalg properties by reading the sysfs files.
  1610. * status: charging/discharging/full/unknown
  1611. * health: health of the battery
  1612. * Returns error code in case of failure else 0 on success
  1613. */
  1614. static int abx500_chargalg_get_property(struct power_supply *psy,
  1615. enum power_supply_property psp,
  1616. union power_supply_propval *val)
  1617. {
  1618. struct abx500_chargalg *di;
  1619. di = to_abx500_chargalg_device_info(psy);
  1620. switch (psp) {
  1621. case POWER_SUPPLY_PROP_STATUS:
  1622. val->intval = di->charge_status;
  1623. break;
  1624. case POWER_SUPPLY_PROP_HEALTH:
  1625. if (di->events.batt_ovv) {
  1626. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1627. } else if (di->events.btemp_underover) {
  1628. if (di->batt_data.temp <= di->bm->temp_under)
  1629. val->intval = POWER_SUPPLY_HEALTH_COLD;
  1630. else
  1631. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  1632. } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
  1633. di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
  1634. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  1635. } else {
  1636. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  1637. }
  1638. break;
  1639. default:
  1640. return -EINVAL;
  1641. }
  1642. return 0;
  1643. }
  1644. /* Exposure to the sysfs interface */
  1645. static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
  1646. char *buf)
  1647. {
  1648. return sprintf(buf, "%d\n", di->curr_status.curr_step);
  1649. }
  1650. static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
  1651. const char *buf, size_t length)
  1652. {
  1653. long int param;
  1654. int ret;
  1655. ret = kstrtol(buf, 10, &param);
  1656. if (ret < 0)
  1657. return ret;
  1658. di->curr_status.curr_step = param;
  1659. if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
  1660. di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
  1661. di->curr_status.curr_step_change = true;
  1662. queue_work(di->chargalg_wq, &di->chargalg_work);
  1663. } else
  1664. dev_info(di->dev, "Wrong current step\n"
  1665. "Enter 0. Disable AC/USB Charging\n"
  1666. "1--100. Set AC/USB charging current step\n"
  1667. "100. Enable AC/USB Charging\n");
  1668. return strlen(buf);
  1669. }
  1670. static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
  1671. char *buf)
  1672. {
  1673. return sprintf(buf, "%d\n",
  1674. di->susp_status.ac_suspended &&
  1675. di->susp_status.usb_suspended);
  1676. }
  1677. static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
  1678. const char *buf, size_t length)
  1679. {
  1680. long int param;
  1681. int ac_usb;
  1682. int ret;
  1683. ret = kstrtol(buf, 10, &param);
  1684. if (ret < 0)
  1685. return ret;
  1686. ac_usb = param;
  1687. switch (ac_usb) {
  1688. case 0:
  1689. /* Disable charging */
  1690. di->susp_status.ac_suspended = true;
  1691. di->susp_status.usb_suspended = true;
  1692. di->susp_status.suspended_change = true;
  1693. /* Trigger a state change */
  1694. queue_work(di->chargalg_wq,
  1695. &di->chargalg_work);
  1696. break;
  1697. case 1:
  1698. /* Enable AC Charging */
  1699. di->susp_status.ac_suspended = false;
  1700. di->susp_status.suspended_change = true;
  1701. /* Trigger a state change */
  1702. queue_work(di->chargalg_wq,
  1703. &di->chargalg_work);
  1704. break;
  1705. case 2:
  1706. /* Enable USB charging */
  1707. di->susp_status.usb_suspended = false;
  1708. di->susp_status.suspended_change = true;
  1709. /* Trigger a state change */
  1710. queue_work(di->chargalg_wq,
  1711. &di->chargalg_work);
  1712. break;
  1713. default:
  1714. dev_info(di->dev, "Wrong input\n"
  1715. "Enter 0. Disable AC/USB Charging\n"
  1716. "1. Enable AC charging\n"
  1717. "2. Enable USB Charging\n");
  1718. };
  1719. return strlen(buf);
  1720. }
  1721. static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
  1722. __ATTR(chargalg, 0644, abx500_chargalg_en_show,
  1723. abx500_chargalg_en_store);
  1724. static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
  1725. __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
  1726. abx500_chargalg_curr_step_store);
  1727. static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
  1728. struct attribute *attr, char *buf)
  1729. {
  1730. struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
  1731. struct abx500_chargalg_sysfs_entry, attr);
  1732. struct abx500_chargalg *di = container_of(kobj,
  1733. struct abx500_chargalg, chargalg_kobject);
  1734. if (!entry->show)
  1735. return -EIO;
  1736. return entry->show(di, buf);
  1737. }
  1738. static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
  1739. struct attribute *attr, const char *buf, size_t length)
  1740. {
  1741. struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
  1742. struct abx500_chargalg_sysfs_entry, attr);
  1743. struct abx500_chargalg *di = container_of(kobj,
  1744. struct abx500_chargalg, chargalg_kobject);
  1745. if (!entry->store)
  1746. return -EIO;
  1747. return entry->store(di, buf, length);
  1748. }
  1749. static struct attribute *abx500_chargalg_chg[] = {
  1750. &abx500_chargalg_en_charger.attr,
  1751. &abx500_chargalg_curr_step.attr,
  1752. NULL,
  1753. };
  1754. static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
  1755. .show = abx500_chargalg_sysfs_show,
  1756. .store = abx500_chargalg_sysfs_charger,
  1757. };
  1758. static struct kobj_type abx500_chargalg_ktype = {
  1759. .sysfs_ops = &abx500_chargalg_sysfs_ops,
  1760. .default_attrs = abx500_chargalg_chg,
  1761. };
  1762. /**
  1763. * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
  1764. * @di: pointer to the struct abx500_chargalg
  1765. *
  1766. * This function removes the entry in sysfs.
  1767. */
  1768. static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
  1769. {
  1770. kobject_del(&di->chargalg_kobject);
  1771. }
  1772. /**
  1773. * abx500_chargalg_sysfs_init() - init of sysfs entry
  1774. * @di: pointer to the struct abx500_chargalg
  1775. *
  1776. * This function adds an entry in sysfs.
  1777. * Returns error code in case of failure else 0(on success)
  1778. */
  1779. static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
  1780. {
  1781. int ret = 0;
  1782. ret = kobject_init_and_add(&di->chargalg_kobject,
  1783. &abx500_chargalg_ktype,
  1784. NULL, "abx500_chargalg");
  1785. if (ret < 0)
  1786. dev_err(di->dev, "failed to create sysfs entry\n");
  1787. return ret;
  1788. }
  1789. /* Exposure to the sysfs interface <<END>> */
  1790. #if defined(CONFIG_PM)
  1791. static int abx500_chargalg_resume(struct platform_device *pdev)
  1792. {
  1793. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1794. /* Kick charger watchdog if charging (any charger online) */
  1795. if (di->chg_info.online_chg)
  1796. queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
  1797. /*
  1798. * Run the charging algorithm directly to be sure we don't
  1799. * do it too seldom
  1800. */
  1801. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1802. return 0;
  1803. }
  1804. static int abx500_chargalg_suspend(struct platform_device *pdev,
  1805. pm_message_t state)
  1806. {
  1807. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1808. if (di->chg_info.online_chg)
  1809. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1810. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1811. return 0;
  1812. }
  1813. #else
  1814. #define abx500_chargalg_suspend NULL
  1815. #define abx500_chargalg_resume NULL
  1816. #endif
  1817. static int abx500_chargalg_remove(struct platform_device *pdev)
  1818. {
  1819. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1820. /* sysfs interface to enable/disbale charging from user space */
  1821. abx500_chargalg_sysfs_exit(di);
  1822. hrtimer_cancel(&di->safety_timer);
  1823. hrtimer_cancel(&di->maintenance_timer);
  1824. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1825. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1826. cancel_work_sync(&di->chargalg_work);
  1827. /* Delete the work queue */
  1828. destroy_workqueue(di->chargalg_wq);
  1829. power_supply_unregister(&di->chargalg_psy);
  1830. return 0;
  1831. }
  1832. static char *supply_interface[] = {
  1833. "ab8500_fg",
  1834. };
  1835. static int abx500_chargalg_probe(struct platform_device *pdev)
  1836. {
  1837. struct device_node *np = pdev->dev.of_node;
  1838. struct abx500_bm_data *plat = pdev->dev.platform_data;
  1839. struct abx500_chargalg *di;
  1840. int ret = 0;
  1841. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  1842. if (!di) {
  1843. dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
  1844. return -ENOMEM;
  1845. }
  1846. if (!plat) {
  1847. dev_err(&pdev->dev, "no battery management data supplied\n");
  1848. return -EINVAL;
  1849. }
  1850. di->bm = plat;
  1851. if (np) {
  1852. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  1853. if (ret) {
  1854. dev_err(&pdev->dev, "failed to get battery information\n");
  1855. return ret;
  1856. }
  1857. }
  1858. /* get device struct and parent */
  1859. di->dev = &pdev->dev;
  1860. di->parent = dev_get_drvdata(pdev->dev.parent);
  1861. /* chargalg supply */
  1862. di->chargalg_psy.name = "abx500_chargalg";
  1863. di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  1864. di->chargalg_psy.properties = abx500_chargalg_props;
  1865. di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
  1866. di->chargalg_psy.get_property = abx500_chargalg_get_property;
  1867. di->chargalg_psy.supplied_to = supply_interface;
  1868. di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
  1869. di->chargalg_psy.external_power_changed =
  1870. abx500_chargalg_external_power_changed;
  1871. /* Initilialize safety timer */
  1872. hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  1873. di->safety_timer.function = abx500_chargalg_safety_timer_expired;
  1874. /* Initilialize maintenance timer */
  1875. hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  1876. di->maintenance_timer.function =
  1877. abx500_chargalg_maintenance_timer_expired;
  1878. /* Create a work queue for the chargalg */
  1879. di->chargalg_wq =
  1880. create_singlethread_workqueue("abx500_chargalg_wq");
  1881. if (di->chargalg_wq == NULL) {
  1882. dev_err(di->dev, "failed to create work queue\n");
  1883. return -ENOMEM;
  1884. }
  1885. /* Init work for chargalg */
  1886. INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
  1887. abx500_chargalg_periodic_work);
  1888. INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
  1889. abx500_chargalg_wd_work);
  1890. /* Init work for chargalg */
  1891. INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
  1892. /* To detect charger at startup */
  1893. di->chg_info.prev_conn_chg = -1;
  1894. /* Register chargalg power supply class */
  1895. ret = power_supply_register(di->dev, &di->chargalg_psy);
  1896. if (ret) {
  1897. dev_err(di->dev, "failed to register chargalg psy\n");
  1898. goto free_chargalg_wq;
  1899. }
  1900. platform_set_drvdata(pdev, di);
  1901. /* sysfs interface to enable/disable charging from user space */
  1902. ret = abx500_chargalg_sysfs_init(di);
  1903. if (ret) {
  1904. dev_err(di->dev, "failed to create sysfs entry\n");
  1905. goto free_psy;
  1906. }
  1907. di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
  1908. /* Run the charging algorithm */
  1909. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1910. dev_info(di->dev, "probe success\n");
  1911. return ret;
  1912. free_psy:
  1913. power_supply_unregister(&di->chargalg_psy);
  1914. free_chargalg_wq:
  1915. destroy_workqueue(di->chargalg_wq);
  1916. return ret;
  1917. }
  1918. static const struct of_device_id ab8500_chargalg_match[] = {
  1919. { .compatible = "stericsson,ab8500-chargalg", },
  1920. { },
  1921. };
  1922. static struct platform_driver abx500_chargalg_driver = {
  1923. .probe = abx500_chargalg_probe,
  1924. .remove = abx500_chargalg_remove,
  1925. .suspend = abx500_chargalg_suspend,
  1926. .resume = abx500_chargalg_resume,
  1927. .driver = {
  1928. .name = "ab8500-chargalg",
  1929. .owner = THIS_MODULE,
  1930. .of_match_table = ab8500_chargalg_match,
  1931. },
  1932. };
  1933. module_platform_driver(abx500_chargalg_driver);
  1934. MODULE_LICENSE("GPL v2");
  1935. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  1936. MODULE_ALIAS("platform:abx500-chargalg");
  1937. MODULE_DESCRIPTION("abx500 battery charging algorithm");