abx500_chargalg.c 57 KB

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