abx500_chargalg.c 55 KB

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