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