abx500_chargalg.c 59 KB

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