abx500_chargalg.c 61 KB

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