abx500_chargalg.c 59 KB

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