abx500_chargalg.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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;
  888. struct abx500_chargalg *di;
  889. union power_supply_propval ret;
  890. int i, j;
  891. bool psy_found = false;
  892. bool capacity_updated = false;
  893. psy = (struct power_supply *)data;
  894. ext = dev_get_drvdata(dev);
  895. di = power_supply_get_drvdata(psy);
  896. /* For all psy where the driver name appears in any supplied_to */
  897. for (i = 0; i < ext->num_supplicants; i++) {
  898. if (!strcmp(ext->supplied_to[i], psy->desc->name))
  899. psy_found = true;
  900. }
  901. if (!psy_found)
  902. return 0;
  903. /*
  904. * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
  905. * property because of handling that sysfs entry on its own, this is
  906. * the place to get the battery capacity.
  907. */
  908. if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
  909. di->batt_data.percent = ret.intval;
  910. capacity_updated = true;
  911. }
  912. /* Go through all properties for the psy */
  913. for (j = 0; j < ext->desc->num_properties; j++) {
  914. enum power_supply_property prop;
  915. prop = ext->desc->properties[j];
  916. /*
  917. * Initialize chargers if not already done.
  918. * The ab8500_charger*/
  919. if (!di->ac_chg &&
  920. ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
  921. di->ac_chg = psy_to_ux500_charger(ext);
  922. else if (!di->usb_chg &&
  923. ext->desc->type == POWER_SUPPLY_TYPE_USB)
  924. di->usb_chg = psy_to_ux500_charger(ext);
  925. if (power_supply_get_property(ext, prop, &ret))
  926. continue;
  927. switch (prop) {
  928. case POWER_SUPPLY_PROP_PRESENT:
  929. switch (ext->desc->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->desc->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->desc->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->desc->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->desc->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->desc->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->desc->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->desc->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 = power_supply_get_drvdata(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 = power_supply_get_drvdata(psy);
  1619. switch (psp) {
  1620. case POWER_SUPPLY_PROP_STATUS:
  1621. val->intval = di->charge_status;
  1622. break;
  1623. case POWER_SUPPLY_PROP_HEALTH:
  1624. if (di->events.batt_ovv) {
  1625. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1626. } else if (di->events.btemp_underover) {
  1627. if (di->batt_data.temp <= di->bm->temp_under)
  1628. val->intval = POWER_SUPPLY_HEALTH_COLD;
  1629. else
  1630. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  1631. } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
  1632. di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
  1633. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  1634. } else {
  1635. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  1636. }
  1637. break;
  1638. default:
  1639. return -EINVAL;
  1640. }
  1641. return 0;
  1642. }
  1643. /* Exposure to the sysfs interface */
  1644. static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
  1645. char *buf)
  1646. {
  1647. return sprintf(buf, "%d\n", di->curr_status.curr_step);
  1648. }
  1649. static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
  1650. const char *buf, size_t length)
  1651. {
  1652. long int param;
  1653. int ret;
  1654. ret = kstrtol(buf, 10, &param);
  1655. if (ret < 0)
  1656. return ret;
  1657. di->curr_status.curr_step = param;
  1658. if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
  1659. di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
  1660. di->curr_status.curr_step_change = true;
  1661. queue_work(di->chargalg_wq, &di->chargalg_work);
  1662. } else
  1663. dev_info(di->dev, "Wrong current step\n"
  1664. "Enter 0. Disable AC/USB Charging\n"
  1665. "1--100. Set AC/USB charging current step\n"
  1666. "100. Enable AC/USB Charging\n");
  1667. return strlen(buf);
  1668. }
  1669. static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
  1670. char *buf)
  1671. {
  1672. return sprintf(buf, "%d\n",
  1673. di->susp_status.ac_suspended &&
  1674. di->susp_status.usb_suspended);
  1675. }
  1676. static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
  1677. const char *buf, size_t length)
  1678. {
  1679. long int param;
  1680. int ac_usb;
  1681. int ret;
  1682. ret = kstrtol(buf, 10, &param);
  1683. if (ret < 0)
  1684. return ret;
  1685. ac_usb = param;
  1686. switch (ac_usb) {
  1687. case 0:
  1688. /* Disable charging */
  1689. di->susp_status.ac_suspended = true;
  1690. di->susp_status.usb_suspended = true;
  1691. di->susp_status.suspended_change = true;
  1692. /* Trigger a state change */
  1693. queue_work(di->chargalg_wq,
  1694. &di->chargalg_work);
  1695. break;
  1696. case 1:
  1697. /* Enable AC Charging */
  1698. di->susp_status.ac_suspended = false;
  1699. di->susp_status.suspended_change = true;
  1700. /* Trigger a state change */
  1701. queue_work(di->chargalg_wq,
  1702. &di->chargalg_work);
  1703. break;
  1704. case 2:
  1705. /* Enable USB charging */
  1706. di->susp_status.usb_suspended = false;
  1707. di->susp_status.suspended_change = true;
  1708. /* Trigger a state change */
  1709. queue_work(di->chargalg_wq,
  1710. &di->chargalg_work);
  1711. break;
  1712. default:
  1713. dev_info(di->dev, "Wrong input\n"
  1714. "Enter 0. Disable AC/USB Charging\n"
  1715. "1. Enable AC charging\n"
  1716. "2. Enable USB Charging\n");
  1717. };
  1718. return strlen(buf);
  1719. }
  1720. static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
  1721. __ATTR(chargalg, 0644, abx500_chargalg_en_show,
  1722. abx500_chargalg_en_store);
  1723. static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
  1724. __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
  1725. abx500_chargalg_curr_step_store);
  1726. static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
  1727. struct attribute *attr, char *buf)
  1728. {
  1729. struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
  1730. struct abx500_chargalg_sysfs_entry, attr);
  1731. struct abx500_chargalg *di = container_of(kobj,
  1732. struct abx500_chargalg, chargalg_kobject);
  1733. if (!entry->show)
  1734. return -EIO;
  1735. return entry->show(di, buf);
  1736. }
  1737. static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
  1738. struct attribute *attr, const char *buf, size_t length)
  1739. {
  1740. struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
  1741. struct abx500_chargalg_sysfs_entry, attr);
  1742. struct abx500_chargalg *di = container_of(kobj,
  1743. struct abx500_chargalg, chargalg_kobject);
  1744. if (!entry->store)
  1745. return -EIO;
  1746. return entry->store(di, buf, length);
  1747. }
  1748. static struct attribute *abx500_chargalg_chg[] = {
  1749. &abx500_chargalg_en_charger.attr,
  1750. &abx500_chargalg_curr_step.attr,
  1751. NULL,
  1752. };
  1753. static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
  1754. .show = abx500_chargalg_sysfs_show,
  1755. .store = abx500_chargalg_sysfs_charger,
  1756. };
  1757. static struct kobj_type abx500_chargalg_ktype = {
  1758. .sysfs_ops = &abx500_chargalg_sysfs_ops,
  1759. .default_attrs = abx500_chargalg_chg,
  1760. };
  1761. /**
  1762. * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
  1763. * @di: pointer to the struct abx500_chargalg
  1764. *
  1765. * This function removes the entry in sysfs.
  1766. */
  1767. static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
  1768. {
  1769. kobject_del(&di->chargalg_kobject);
  1770. }
  1771. /**
  1772. * abx500_chargalg_sysfs_init() - init of sysfs entry
  1773. * @di: pointer to the struct abx500_chargalg
  1774. *
  1775. * This function adds an entry in sysfs.
  1776. * Returns error code in case of failure else 0(on success)
  1777. */
  1778. static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
  1779. {
  1780. int ret = 0;
  1781. ret = kobject_init_and_add(&di->chargalg_kobject,
  1782. &abx500_chargalg_ktype,
  1783. NULL, "abx500_chargalg");
  1784. if (ret < 0)
  1785. dev_err(di->dev, "failed to create sysfs entry\n");
  1786. return ret;
  1787. }
  1788. /* Exposure to the sysfs interface <<END>> */
  1789. #if defined(CONFIG_PM)
  1790. static int abx500_chargalg_resume(struct platform_device *pdev)
  1791. {
  1792. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1793. /* Kick charger watchdog if charging (any charger online) */
  1794. if (di->chg_info.online_chg)
  1795. queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
  1796. /*
  1797. * Run the charging algorithm directly to be sure we don't
  1798. * do it too seldom
  1799. */
  1800. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1801. return 0;
  1802. }
  1803. static int abx500_chargalg_suspend(struct platform_device *pdev,
  1804. pm_message_t state)
  1805. {
  1806. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1807. if (di->chg_info.online_chg)
  1808. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1809. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1810. return 0;
  1811. }
  1812. #else
  1813. #define abx500_chargalg_suspend NULL
  1814. #define abx500_chargalg_resume NULL
  1815. #endif
  1816. static int abx500_chargalg_remove(struct platform_device *pdev)
  1817. {
  1818. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1819. /* sysfs interface to enable/disbale charging from user space */
  1820. abx500_chargalg_sysfs_exit(di);
  1821. hrtimer_cancel(&di->safety_timer);
  1822. hrtimer_cancel(&di->maintenance_timer);
  1823. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1824. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1825. cancel_work_sync(&di->chargalg_work);
  1826. /* Delete the work queue */
  1827. destroy_workqueue(di->chargalg_wq);
  1828. power_supply_unregister(di->chargalg_psy);
  1829. return 0;
  1830. }
  1831. static char *supply_interface[] = {
  1832. "ab8500_fg",
  1833. };
  1834. static const struct power_supply_desc abx500_chargalg_desc = {
  1835. .name = "abx500_chargalg",
  1836. .type = POWER_SUPPLY_TYPE_BATTERY,
  1837. .properties = abx500_chargalg_props,
  1838. .num_properties = ARRAY_SIZE(abx500_chargalg_props),
  1839. .get_property = abx500_chargalg_get_property,
  1840. .external_power_changed = abx500_chargalg_external_power_changed,
  1841. };
  1842. static int abx500_chargalg_probe(struct platform_device *pdev)
  1843. {
  1844. struct device_node *np = pdev->dev.of_node;
  1845. struct abx500_bm_data *plat = pdev->dev.platform_data;
  1846. struct power_supply_config psy_cfg = {};
  1847. struct abx500_chargalg *di;
  1848. int ret = 0;
  1849. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  1850. if (!di) {
  1851. dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
  1852. return -ENOMEM;
  1853. }
  1854. if (!plat) {
  1855. dev_err(&pdev->dev, "no battery management data supplied\n");
  1856. return -EINVAL;
  1857. }
  1858. di->bm = plat;
  1859. if (np) {
  1860. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  1861. if (ret) {
  1862. dev_err(&pdev->dev, "failed to get battery information\n");
  1863. return ret;
  1864. }
  1865. }
  1866. /* get device struct and parent */
  1867. di->dev = &pdev->dev;
  1868. di->parent = dev_get_drvdata(pdev->dev.parent);
  1869. psy_cfg.supplied_to = supply_interface;
  1870. psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  1871. psy_cfg.drv_data = di;
  1872. /* Initilialize safety timer */
  1873. hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  1874. di->safety_timer.function = abx500_chargalg_safety_timer_expired;
  1875. /* Initilialize maintenance timer */
  1876. hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  1877. di->maintenance_timer.function =
  1878. abx500_chargalg_maintenance_timer_expired;
  1879. /* Create a work queue for the chargalg */
  1880. di->chargalg_wq =
  1881. create_singlethread_workqueue("abx500_chargalg_wq");
  1882. if (di->chargalg_wq == NULL) {
  1883. dev_err(di->dev, "failed to create work queue\n");
  1884. return -ENOMEM;
  1885. }
  1886. /* Init work for chargalg */
  1887. INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
  1888. abx500_chargalg_periodic_work);
  1889. INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
  1890. abx500_chargalg_wd_work);
  1891. /* Init work for chargalg */
  1892. INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
  1893. /* To detect charger at startup */
  1894. di->chg_info.prev_conn_chg = -1;
  1895. /* Register chargalg power supply class */
  1896. di->chargalg_psy = power_supply_register(di->dev, &abx500_chargalg_desc,
  1897. &psy_cfg);
  1898. if (IS_ERR(di->chargalg_psy)) {
  1899. dev_err(di->dev, "failed to register chargalg psy\n");
  1900. ret = PTR_ERR(di->chargalg_psy);
  1901. goto free_chargalg_wq;
  1902. }
  1903. platform_set_drvdata(pdev, di);
  1904. /* sysfs interface to enable/disable charging from user space */
  1905. ret = abx500_chargalg_sysfs_init(di);
  1906. if (ret) {
  1907. dev_err(di->dev, "failed to create sysfs entry\n");
  1908. goto free_psy;
  1909. }
  1910. di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
  1911. /* Run the charging algorithm */
  1912. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1913. dev_info(di->dev, "probe success\n");
  1914. return ret;
  1915. free_psy:
  1916. power_supply_unregister(di->chargalg_psy);
  1917. free_chargalg_wq:
  1918. destroy_workqueue(di->chargalg_wq);
  1919. return ret;
  1920. }
  1921. static const struct of_device_id ab8500_chargalg_match[] = {
  1922. { .compatible = "stericsson,ab8500-chargalg", },
  1923. { },
  1924. };
  1925. static struct platform_driver abx500_chargalg_driver = {
  1926. .probe = abx500_chargalg_probe,
  1927. .remove = abx500_chargalg_remove,
  1928. .suspend = abx500_chargalg_suspend,
  1929. .resume = abx500_chargalg_resume,
  1930. .driver = {
  1931. .name = "ab8500-chargalg",
  1932. .of_match_table = ab8500_chargalg_match,
  1933. },
  1934. };
  1935. module_platform_driver(abx500_chargalg_driver);
  1936. MODULE_LICENSE("GPL v2");
  1937. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  1938. MODULE_ALIAS("platform:abx500-chargalg");
  1939. MODULE_DESCRIPTION("abx500 battery charging algorithm");