abx500_chargalg.c 59 KB

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