ab8500_chargalg.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2012
  4. * Copyright (c) 2012 Sony Mobile Communications AB
  5. *
  6. * Charging algorithm driver for AB8500
  7. *
  8. * Authors:
  9. * Johan Palsson <[email protected]>
  10. * Karl Komierowski <[email protected]>
  11. * Arun R Murthy <[email protected]>
  12. * Author: Imre Sunyi <[email protected]>
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/component.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/completion.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/kobject.h>
  27. #include <linux/of.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/abx500.h>
  30. #include <linux/mfd/abx500/ab8500.h>
  31. #include <linux/notifier.h>
  32. #include "ab8500-bm.h"
  33. #include "ab8500-chargalg.h"
  34. /* Watchdog kick interval */
  35. #define CHG_WD_INTERVAL (6 * HZ)
  36. /* End-of-charge criteria counter */
  37. #define EOC_COND_CNT 10
  38. /* One hour expressed in seconds */
  39. #define ONE_HOUR_IN_SECONDS 3600
  40. /* Five minutes expressed in seconds */
  41. #define FIVE_MINUTES_IN_SECONDS 300
  42. /*
  43. * This is the battery capacity limit that will trigger a new
  44. * full charging cycle in the case where maintenance charging
  45. * has been disabled
  46. */
  47. #define AB8500_RECHARGE_CAP 95
  48. enum ab8500_chargers {
  49. NO_CHG,
  50. AC_CHG,
  51. USB_CHG,
  52. };
  53. struct ab8500_chargalg_charger_info {
  54. enum ab8500_chargers conn_chg;
  55. enum ab8500_chargers prev_conn_chg;
  56. enum ab8500_chargers online_chg;
  57. enum ab8500_chargers prev_online_chg;
  58. enum ab8500_chargers charger_type;
  59. bool usb_chg_ok;
  60. bool ac_chg_ok;
  61. int usb_volt_uv;
  62. int usb_curr_ua;
  63. int ac_volt_uv;
  64. int ac_curr_ua;
  65. int usb_vset_uv;
  66. int usb_iset_ua;
  67. int ac_vset_uv;
  68. int ac_iset_ua;
  69. };
  70. struct ab8500_chargalg_battery_data {
  71. int temp;
  72. int volt_uv;
  73. int avg_curr_ua;
  74. int inst_curr_ua;
  75. int percent;
  76. };
  77. enum ab8500_chargalg_states {
  78. STATE_HANDHELD_INIT,
  79. STATE_HANDHELD,
  80. STATE_CHG_NOT_OK_INIT,
  81. STATE_CHG_NOT_OK,
  82. STATE_HW_TEMP_PROTECT_INIT,
  83. STATE_HW_TEMP_PROTECT,
  84. STATE_NORMAL_INIT,
  85. STATE_NORMAL,
  86. STATE_WAIT_FOR_RECHARGE_INIT,
  87. STATE_WAIT_FOR_RECHARGE,
  88. STATE_MAINTENANCE_A_INIT,
  89. STATE_MAINTENANCE_A,
  90. STATE_MAINTENANCE_B_INIT,
  91. STATE_MAINTENANCE_B,
  92. STATE_TEMP_UNDEROVER_INIT,
  93. STATE_TEMP_UNDEROVER,
  94. STATE_TEMP_LOWHIGH_INIT,
  95. STATE_TEMP_LOWHIGH,
  96. STATE_OVV_PROTECT_INIT,
  97. STATE_OVV_PROTECT,
  98. STATE_SAFETY_TIMER_EXPIRED_INIT,
  99. STATE_SAFETY_TIMER_EXPIRED,
  100. STATE_BATT_REMOVED_INIT,
  101. STATE_BATT_REMOVED,
  102. STATE_WD_EXPIRED_INIT,
  103. STATE_WD_EXPIRED,
  104. };
  105. static const char * const states[] = {
  106. "HANDHELD_INIT",
  107. "HANDHELD",
  108. "CHG_NOT_OK_INIT",
  109. "CHG_NOT_OK",
  110. "HW_TEMP_PROTECT_INIT",
  111. "HW_TEMP_PROTECT",
  112. "NORMAL_INIT",
  113. "NORMAL",
  114. "WAIT_FOR_RECHARGE_INIT",
  115. "WAIT_FOR_RECHARGE",
  116. "MAINTENANCE_A_INIT",
  117. "MAINTENANCE_A",
  118. "MAINTENANCE_B_INIT",
  119. "MAINTENANCE_B",
  120. "TEMP_UNDEROVER_INIT",
  121. "TEMP_UNDEROVER",
  122. "TEMP_LOWHIGH_INIT",
  123. "TEMP_LOWHIGH",
  124. "OVV_PROTECT_INIT",
  125. "OVV_PROTECT",
  126. "SAFETY_TIMER_EXPIRED_INIT",
  127. "SAFETY_TIMER_EXPIRED",
  128. "BATT_REMOVED_INIT",
  129. "BATT_REMOVED",
  130. "WD_EXPIRED_INIT",
  131. "WD_EXPIRED",
  132. };
  133. struct ab8500_chargalg_events {
  134. bool batt_unknown;
  135. bool mainextchnotok;
  136. bool batt_ovv;
  137. bool batt_rem;
  138. bool btemp_underover;
  139. bool btemp_low;
  140. bool btemp_high;
  141. bool main_thermal_prot;
  142. bool usb_thermal_prot;
  143. bool main_ovv;
  144. bool vbus_ovv;
  145. bool usbchargernotok;
  146. bool safety_timer_expired;
  147. bool maintenance_timer_expired;
  148. bool ac_wd_expired;
  149. bool usb_wd_expired;
  150. bool ac_cv_active;
  151. bool usb_cv_active;
  152. bool vbus_collapsed;
  153. };
  154. /**
  155. * struct ab8500_charge_curr_maximization - Charger maximization parameters
  156. * @original_iset_ua: the non optimized/maximised charger current
  157. * @current_iset_ua: the charging current used at this moment
  158. * @condition_cnt: number of iterations needed before a new charger current
  159. is set
  160. * @max_current_ua: maximum charger current
  161. * @wait_cnt: to avoid too fast current step down in case of charger
  162. * voltage collapse, we insert this delay between step
  163. * down
  164. * @level: tells in how many steps the charging current has been
  165. increased
  166. */
  167. struct ab8500_charge_curr_maximization {
  168. int original_iset_ua;
  169. int current_iset_ua;
  170. int condition_cnt;
  171. int max_current_ua;
  172. int wait_cnt;
  173. u8 level;
  174. };
  175. enum maxim_ret {
  176. MAXIM_RET_NOACTION,
  177. MAXIM_RET_CHANGE,
  178. MAXIM_RET_IBAT_TOO_HIGH,
  179. };
  180. /**
  181. * struct ab8500_chargalg - ab8500 Charging algorithm device information
  182. * @dev: pointer to the structure device
  183. * @charge_status: battery operating status
  184. * @eoc_cnt: counter used to determine end-of_charge
  185. * @maintenance_chg: indicate if maintenance charge is active
  186. * @t_hyst_norm temperature hysteresis when the temperature has been
  187. * over or under normal limits
  188. * @t_hyst_lowhigh temperature hysteresis when the temperature has been
  189. * over or under the high or low limits
  190. * @charge_state: current state of the charging algorithm
  191. * @ccm charging current maximization parameters
  192. * @chg_info: information about connected charger types
  193. * @batt_data: data of the battery
  194. * @bm: Platform specific battery management information
  195. * @parent: pointer to the struct ab8500
  196. * @chargalg_psy: structure that holds the battery properties exposed by
  197. * the charging algorithm
  198. * @events: structure for information about events triggered
  199. * @chargalg_wq: work queue for running the charging algorithm
  200. * @chargalg_periodic_work: work to run the charging algorithm periodically
  201. * @chargalg_wd_work: work to kick the charger watchdog periodically
  202. * @chargalg_work: work to run the charging algorithm instantly
  203. * @safety_timer: charging safety timer
  204. * @maintenance_timer: maintenance charging timer
  205. * @chargalg_kobject: structure of type kobject
  206. */
  207. struct ab8500_chargalg {
  208. struct device *dev;
  209. int charge_status;
  210. int eoc_cnt;
  211. bool maintenance_chg;
  212. int t_hyst_norm;
  213. int t_hyst_lowhigh;
  214. enum ab8500_chargalg_states charge_state;
  215. struct ab8500_charge_curr_maximization ccm;
  216. struct ab8500_chargalg_charger_info chg_info;
  217. struct ab8500_chargalg_battery_data batt_data;
  218. struct ab8500 *parent;
  219. struct ab8500_bm_data *bm;
  220. struct power_supply *chargalg_psy;
  221. struct ux500_charger *ac_chg;
  222. struct ux500_charger *usb_chg;
  223. struct ab8500_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 hrtimer safety_timer;
  229. struct hrtimer maintenance_timer;
  230. struct kobject chargalg_kobject;
  231. };
  232. /* Main battery properties */
  233. static enum power_supply_property ab8500_chargalg_props[] = {
  234. POWER_SUPPLY_PROP_STATUS,
  235. POWER_SUPPLY_PROP_HEALTH,
  236. };
  237. /**
  238. * ab8500_chargalg_safety_timer_expired() - Expiration of the safety timer
  239. * @timer: pointer to the hrtimer structure
  240. *
  241. * This function gets called when the safety timer for the charger
  242. * expires
  243. */
  244. static enum hrtimer_restart
  245. ab8500_chargalg_safety_timer_expired(struct hrtimer *timer)
  246. {
  247. struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
  248. safety_timer);
  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. return HRTIMER_NORESTART;
  254. }
  255. /**
  256. * ab8500_chargalg_maintenance_timer_expired() - Expiration of
  257. * the maintenance timer
  258. * @timer: pointer to the timer structure
  259. *
  260. * This function gets called when the maintenance timer
  261. * expires
  262. */
  263. static enum hrtimer_restart
  264. ab8500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
  265. {
  266. struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
  267. maintenance_timer);
  268. dev_dbg(di->dev, "Maintenance timer expired\n");
  269. di->events.maintenance_timer_expired = true;
  270. /* Trigger execution of the algorithm instantly */
  271. queue_work(di->chargalg_wq, &di->chargalg_work);
  272. return HRTIMER_NORESTART;
  273. }
  274. /**
  275. * ab8500_chargalg_state_to() - Change charge state
  276. * @di: pointer to the ab8500_chargalg structure
  277. *
  278. * This function gets called when a charge state change should occur
  279. */
  280. static void ab8500_chargalg_state_to(struct ab8500_chargalg *di,
  281. enum ab8500_chargalg_states state)
  282. {
  283. dev_dbg(di->dev,
  284. "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
  285. di->charge_state == state ? "NO" : "YES",
  286. di->charge_state,
  287. states[di->charge_state],
  288. state,
  289. states[state]);
  290. di->charge_state = state;
  291. }
  292. static int ab8500_chargalg_check_charger_enable(struct ab8500_chargalg *di)
  293. {
  294. struct power_supply_battery_info *bi = di->bm->bi;
  295. switch (di->charge_state) {
  296. case STATE_NORMAL:
  297. case STATE_MAINTENANCE_A:
  298. case STATE_MAINTENANCE_B:
  299. break;
  300. default:
  301. return 0;
  302. }
  303. if (di->chg_info.charger_type & USB_CHG) {
  304. return di->usb_chg->ops.check_enable(di->usb_chg,
  305. bi->constant_charge_voltage_max_uv,
  306. bi->constant_charge_current_max_ua);
  307. } else if (di->chg_info.charger_type & AC_CHG) {
  308. return di->ac_chg->ops.check_enable(di->ac_chg,
  309. bi->constant_charge_voltage_max_uv,
  310. bi->constant_charge_current_max_ua);
  311. }
  312. return 0;
  313. }
  314. /**
  315. * ab8500_chargalg_check_charger_connection() - Check charger connection change
  316. * @di: pointer to the ab8500_chargalg structure
  317. *
  318. * This function will check if there is a change in the charger connection
  319. * and change charge state accordingly. AC has precedence over USB.
  320. */
  321. static int ab8500_chargalg_check_charger_connection(struct ab8500_chargalg *di)
  322. {
  323. if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg) {
  324. /* Charger state changed since last update */
  325. if (di->chg_info.conn_chg & AC_CHG) {
  326. dev_info(di->dev, "Charging source is AC\n");
  327. if (di->chg_info.charger_type != AC_CHG) {
  328. di->chg_info.charger_type = AC_CHG;
  329. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  330. }
  331. } else if (di->chg_info.conn_chg & USB_CHG) {
  332. dev_info(di->dev, "Charging source is USB\n");
  333. di->chg_info.charger_type = USB_CHG;
  334. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  335. } else {
  336. dev_dbg(di->dev, "Charging source is OFF\n");
  337. di->chg_info.charger_type = NO_CHG;
  338. ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
  339. }
  340. di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
  341. }
  342. return di->chg_info.conn_chg;
  343. }
  344. /**
  345. * ab8500_chargalg_start_safety_timer() - Start charging safety timer
  346. * @di: pointer to the ab8500_chargalg structure
  347. *
  348. * The safety timer is used to avoid overcharging of old or bad batteries.
  349. * There are different timers for AC and USB
  350. */
  351. static void ab8500_chargalg_start_safety_timer(struct ab8500_chargalg *di)
  352. {
  353. /* Charger-dependent expiration time in hours*/
  354. int timer_expiration = 0;
  355. switch (di->chg_info.charger_type) {
  356. case AC_CHG:
  357. timer_expiration = di->bm->main_safety_tmr_h;
  358. break;
  359. case USB_CHG:
  360. timer_expiration = di->bm->usb_safety_tmr_h;
  361. break;
  362. default:
  363. dev_err(di->dev, "Unknown charger to charge from\n");
  364. break;
  365. }
  366. di->events.safety_timer_expired = false;
  367. hrtimer_set_expires_range(&di->safety_timer,
  368. ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
  369. ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
  370. hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
  371. }
  372. /**
  373. * ab8500_chargalg_stop_safety_timer() - Stop charging safety timer
  374. * @di: pointer to the ab8500_chargalg structure
  375. *
  376. * The safety timer is stopped whenever the NORMAL state is exited
  377. */
  378. static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di)
  379. {
  380. if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
  381. di->events.safety_timer_expired = false;
  382. }
  383. /**
  384. * ab8500_chargalg_start_maintenance_timer() - Start charging maintenance timer
  385. * @di: pointer to the ab8500_chargalg structure
  386. * @duration: duration of the maintenance timer in minutes
  387. *
  388. * The maintenance timer is used to maintain the charge in the battery once
  389. * the battery is considered full. These timers are chosen to match the
  390. * discharge curve of the battery
  391. */
  392. static void ab8500_chargalg_start_maintenance_timer(struct ab8500_chargalg *di,
  393. int duration)
  394. {
  395. /* Set a timer in minutes with a 30 second range */
  396. hrtimer_set_expires_range(&di->maintenance_timer,
  397. ktime_set(duration * 60, 0),
  398. ktime_set(30, 0));
  399. di->events.maintenance_timer_expired = false;
  400. hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
  401. }
  402. /**
  403. * ab8500_chargalg_stop_maintenance_timer() - Stop maintenance timer
  404. * @di: pointer to the ab8500_chargalg structure
  405. *
  406. * The maintenance timer is stopped whenever maintenance ends or when another
  407. * state is entered
  408. */
  409. static void ab8500_chargalg_stop_maintenance_timer(struct ab8500_chargalg *di)
  410. {
  411. if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
  412. di->events.maintenance_timer_expired = false;
  413. }
  414. /**
  415. * ab8500_chargalg_kick_watchdog() - Kick charger watchdog
  416. * @di: pointer to the ab8500_chargalg structure
  417. *
  418. * The charger watchdog have to be kicked periodically whenever the charger is
  419. * on, else the ABB will reset the system
  420. */
  421. static int ab8500_chargalg_kick_watchdog(struct ab8500_chargalg *di)
  422. {
  423. /* Check if charger exists and kick watchdog if charging */
  424. if (di->ac_chg && di->ac_chg->ops.kick_wd &&
  425. di->chg_info.online_chg & AC_CHG) {
  426. return di->ac_chg->ops.kick_wd(di->ac_chg);
  427. } else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
  428. di->chg_info.online_chg & USB_CHG)
  429. return di->usb_chg->ops.kick_wd(di->usb_chg);
  430. return -ENXIO;
  431. }
  432. /**
  433. * ab8500_chargalg_ac_en() - Turn on/off the AC charger
  434. * @di: pointer to the ab8500_chargalg structure
  435. * @enable: charger on/off
  436. * @vset_uv: requested charger output voltage in microvolt
  437. * @iset_ua: requested charger output current in microampere
  438. *
  439. * The AC charger will be turned on/off with the requested charge voltage and
  440. * current
  441. */
  442. static int ab8500_chargalg_ac_en(struct ab8500_chargalg *di, int enable,
  443. int vset_uv, int iset_ua)
  444. {
  445. if (!di->ac_chg || !di->ac_chg->ops.enable)
  446. return -ENXIO;
  447. /* Select maximum of what both the charger and the battery supports */
  448. if (di->ac_chg->max_out_volt_uv)
  449. vset_uv = min(vset_uv, di->ac_chg->max_out_volt_uv);
  450. if (di->ac_chg->max_out_curr_ua)
  451. iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua);
  452. di->chg_info.ac_iset_ua = iset_ua;
  453. di->chg_info.ac_vset_uv = vset_uv;
  454. return di->ac_chg->ops.enable(di->ac_chg, enable, vset_uv, iset_ua);
  455. }
  456. /**
  457. * ab8500_chargalg_usb_en() - Turn on/off the USB charger
  458. * @di: pointer to the ab8500_chargalg structure
  459. * @enable: charger on/off
  460. * @vset_uv: requested charger output voltage in microvolt
  461. * @iset_ua: requested charger output current in microampere
  462. *
  463. * The USB charger will be turned on/off with the requested charge voltage and
  464. * current
  465. */
  466. static int ab8500_chargalg_usb_en(struct ab8500_chargalg *di, int enable,
  467. int vset_uv, int iset_ua)
  468. {
  469. if (!di->usb_chg || !di->usb_chg->ops.enable)
  470. return -ENXIO;
  471. /* Select maximum of what both the charger and the battery supports */
  472. if (di->usb_chg->max_out_volt_uv)
  473. vset_uv = min(vset_uv, di->usb_chg->max_out_volt_uv);
  474. if (di->usb_chg->max_out_curr_ua)
  475. iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua);
  476. di->chg_info.usb_iset_ua = iset_ua;
  477. di->chg_info.usb_vset_uv = vset_uv;
  478. return di->usb_chg->ops.enable(di->usb_chg, enable, vset_uv, iset_ua);
  479. }
  480. /**
  481. * ab8500_chargalg_update_chg_curr() - Update charger current
  482. * @di: pointer to the ab8500_chargalg structure
  483. * @iset_ua: requested charger output current in microampere
  484. *
  485. * The charger output current will be updated for the charger
  486. * that is currently in use
  487. */
  488. static int ab8500_chargalg_update_chg_curr(struct ab8500_chargalg *di,
  489. int iset_ua)
  490. {
  491. /* Check if charger exists and update current if charging */
  492. if (di->ac_chg && di->ac_chg->ops.update_curr &&
  493. di->chg_info.charger_type & AC_CHG) {
  494. /*
  495. * Select maximum of what both the charger
  496. * and the battery supports
  497. */
  498. if (di->ac_chg->max_out_curr_ua)
  499. iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua);
  500. di->chg_info.ac_iset_ua = iset_ua;
  501. return di->ac_chg->ops.update_curr(di->ac_chg, iset_ua);
  502. } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
  503. di->chg_info.charger_type & USB_CHG) {
  504. /*
  505. * Select maximum of what both the charger
  506. * and the battery supports
  507. */
  508. if (di->usb_chg->max_out_curr_ua)
  509. iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua);
  510. di->chg_info.usb_iset_ua = iset_ua;
  511. return di->usb_chg->ops.update_curr(di->usb_chg, iset_ua);
  512. }
  513. return -ENXIO;
  514. }
  515. /**
  516. * ab8500_chargalg_stop_charging() - Stop charging
  517. * @di: pointer to the ab8500_chargalg structure
  518. *
  519. * This function is called from any state where charging should be stopped.
  520. * All charging is disabled and all status parameters and timers are changed
  521. * accordingly
  522. */
  523. static void ab8500_chargalg_stop_charging(struct ab8500_chargalg *di)
  524. {
  525. ab8500_chargalg_ac_en(di, false, 0, 0);
  526. ab8500_chargalg_usb_en(di, false, 0, 0);
  527. ab8500_chargalg_stop_safety_timer(di);
  528. ab8500_chargalg_stop_maintenance_timer(di);
  529. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  530. di->maintenance_chg = false;
  531. cancel_delayed_work(&di->chargalg_wd_work);
  532. power_supply_changed(di->chargalg_psy);
  533. }
  534. /**
  535. * ab8500_chargalg_hold_charging() - Pauses charging
  536. * @di: pointer to the ab8500_chargalg structure
  537. *
  538. * This function is called in the case where maintenance charging has been
  539. * disabled and instead a battery voltage mode is entered to check when the
  540. * battery voltage has reached a certain recharge voltage
  541. */
  542. static void ab8500_chargalg_hold_charging(struct ab8500_chargalg *di)
  543. {
  544. ab8500_chargalg_ac_en(di, false, 0, 0);
  545. ab8500_chargalg_usb_en(di, false, 0, 0);
  546. ab8500_chargalg_stop_safety_timer(di);
  547. ab8500_chargalg_stop_maintenance_timer(di);
  548. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  549. di->maintenance_chg = false;
  550. cancel_delayed_work(&di->chargalg_wd_work);
  551. power_supply_changed(di->chargalg_psy);
  552. }
  553. /**
  554. * ab8500_chargalg_start_charging() - Start the charger
  555. * @di: pointer to the ab8500_chargalg structure
  556. * @vset_uv: requested charger output voltage in microvolt
  557. * @iset_ua: requested charger output current in microampere
  558. *
  559. * A charger will be enabled depending on the requested charger type that was
  560. * detected previously.
  561. */
  562. static void ab8500_chargalg_start_charging(struct ab8500_chargalg *di,
  563. int vset_uv, int iset_ua)
  564. {
  565. switch (di->chg_info.charger_type) {
  566. case AC_CHG:
  567. dev_dbg(di->dev,
  568. "AC parameters: Vset %d, Ich %d\n", vset_uv, iset_ua);
  569. ab8500_chargalg_usb_en(di, false, 0, 0);
  570. ab8500_chargalg_ac_en(di, true, vset_uv, iset_ua);
  571. break;
  572. case USB_CHG:
  573. dev_dbg(di->dev,
  574. "USB parameters: Vset %d, Ich %d\n", vset_uv, iset_ua);
  575. ab8500_chargalg_ac_en(di, false, 0, 0);
  576. ab8500_chargalg_usb_en(di, true, vset_uv, iset_ua);
  577. break;
  578. default:
  579. dev_err(di->dev, "Unknown charger to charge from\n");
  580. break;
  581. }
  582. }
  583. /**
  584. * ab8500_chargalg_check_temp() - Check battery temperature ranges
  585. * @di: pointer to the ab8500_chargalg structure
  586. *
  587. * The battery temperature is checked against the predefined limits and the
  588. * charge state is changed accordingly
  589. */
  590. static void ab8500_chargalg_check_temp(struct ab8500_chargalg *di)
  591. {
  592. struct power_supply_battery_info *bi = di->bm->bi;
  593. if (di->batt_data.temp > (bi->temp_alert_min + di->t_hyst_norm) &&
  594. di->batt_data.temp < (bi->temp_alert_max - di->t_hyst_norm)) {
  595. /* Temp OK! */
  596. di->events.btemp_underover = false;
  597. di->events.btemp_low = false;
  598. di->events.btemp_high = false;
  599. di->t_hyst_norm = 0;
  600. di->t_hyst_lowhigh = 0;
  601. } else {
  602. if ((di->batt_data.temp >= bi->temp_alert_max) &&
  603. (di->batt_data.temp < (bi->temp_max - di->t_hyst_lowhigh))) {
  604. /* Alert zone for high temperature */
  605. di->events.btemp_underover = false;
  606. di->events.btemp_high = true;
  607. di->t_hyst_norm = di->bm->temp_hysteresis;
  608. di->t_hyst_lowhigh = 0;
  609. } else if ((di->batt_data.temp > (bi->temp_min + di->t_hyst_lowhigh)) &&
  610. (di->batt_data.temp <= bi->temp_alert_min)) {
  611. /* Alert zone for low temperature */
  612. di->events.btemp_underover = false;
  613. di->events.btemp_low = true;
  614. di->t_hyst_norm = di->bm->temp_hysteresis;
  615. di->t_hyst_lowhigh = 0;
  616. } else if (di->batt_data.temp <= bi->temp_min ||
  617. di->batt_data.temp >= bi->temp_max) {
  618. /* TEMP major!!!!! */
  619. di->events.btemp_underover = true;
  620. di->events.btemp_low = false;
  621. di->events.btemp_high = false;
  622. di->t_hyst_norm = 0;
  623. di->t_hyst_lowhigh = di->bm->temp_hysteresis;
  624. } else {
  625. /* Within hysteresis */
  626. dev_dbg(di->dev, "Within hysteresis limit temp: %d "
  627. "hyst_lowhigh %d, hyst normal %d\n",
  628. di->batt_data.temp, di->t_hyst_lowhigh,
  629. di->t_hyst_norm);
  630. }
  631. }
  632. }
  633. /**
  634. * ab8500_chargalg_check_charger_voltage() - Check charger voltage
  635. * @di: pointer to the ab8500_chargalg structure
  636. *
  637. * Charger voltage is checked against maximum limit
  638. */
  639. static void ab8500_chargalg_check_charger_voltage(struct ab8500_chargalg *di)
  640. {
  641. if (di->chg_info.usb_volt_uv > di->bm->chg_params->usb_volt_max_uv)
  642. di->chg_info.usb_chg_ok = false;
  643. else
  644. di->chg_info.usb_chg_ok = true;
  645. if (di->chg_info.ac_volt_uv > di->bm->chg_params->ac_volt_max_uv)
  646. di->chg_info.ac_chg_ok = false;
  647. else
  648. di->chg_info.ac_chg_ok = true;
  649. }
  650. /**
  651. * ab8500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
  652. * @di: pointer to the ab8500_chargalg structure
  653. *
  654. * End-of-charge criteria is fulfilled when the battery voltage is above a
  655. * certain limit and the battery current is below a certain limit for a
  656. * predefined number of consecutive seconds. If true, the battery is full
  657. */
  658. static void ab8500_chargalg_end_of_charge(struct ab8500_chargalg *di)
  659. {
  660. if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
  661. di->charge_state == STATE_NORMAL &&
  662. !di->maintenance_chg && (di->batt_data.volt_uv >=
  663. di->bm->bi->voltage_max_design_uv ||
  664. di->events.usb_cv_active || di->events.ac_cv_active) &&
  665. di->batt_data.avg_curr_ua <
  666. di->bm->bi->charge_term_current_ua &&
  667. di->batt_data.avg_curr_ua > 0) {
  668. if (++di->eoc_cnt >= EOC_COND_CNT) {
  669. di->eoc_cnt = 0;
  670. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  671. di->maintenance_chg = true;
  672. dev_dbg(di->dev, "EOC reached!\n");
  673. power_supply_changed(di->chargalg_psy);
  674. } else {
  675. dev_dbg(di->dev,
  676. " EOC limit reached for the %d"
  677. " time, out of %d before EOC\n",
  678. di->eoc_cnt,
  679. EOC_COND_CNT);
  680. }
  681. } else {
  682. di->eoc_cnt = 0;
  683. }
  684. }
  685. static void init_maxim_chg_curr(struct ab8500_chargalg *di)
  686. {
  687. struct power_supply_battery_info *bi = di->bm->bi;
  688. di->ccm.original_iset_ua = bi->constant_charge_current_max_ua;
  689. di->ccm.current_iset_ua = bi->constant_charge_current_max_ua;
  690. di->ccm.max_current_ua = di->bm->maxi->chg_curr_ua;
  691. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  692. di->ccm.level = 0;
  693. }
  694. /**
  695. * ab8500_chargalg_chg_curr_maxim - increases the charger current to
  696. * compensate for the system load
  697. * @di pointer to the ab8500_chargalg structure
  698. *
  699. * This maximization function is used to raise the charger current to get the
  700. * battery current as close to the optimal value as possible. The battery
  701. * current during charging is affected by the system load
  702. */
  703. static enum maxim_ret ab8500_chargalg_chg_curr_maxim(struct ab8500_chargalg *di)
  704. {
  705. if (!di->bm->maxi->ena_maxi)
  706. return MAXIM_RET_NOACTION;
  707. if (di->events.vbus_collapsed) {
  708. dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
  709. di->ccm.wait_cnt);
  710. if (di->ccm.wait_cnt == 0) {
  711. dev_dbg(di->dev, "lowering current\n");
  712. di->ccm.wait_cnt++;
  713. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  714. di->ccm.max_current_ua = di->ccm.current_iset_ua;
  715. di->ccm.current_iset_ua = di->ccm.max_current_ua;
  716. di->ccm.level--;
  717. return MAXIM_RET_CHANGE;
  718. } else {
  719. dev_dbg(di->dev, "waiting\n");
  720. /* Let's go in here twice before lowering curr again */
  721. di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
  722. return MAXIM_RET_NOACTION;
  723. }
  724. }
  725. di->ccm.wait_cnt = 0;
  726. if (di->batt_data.inst_curr_ua > di->ccm.original_iset_ua) {
  727. dev_dbg(di->dev, " Maximization Ibat (%duA) too high"
  728. " (limit %duA) (current iset: %duA)!\n",
  729. di->batt_data.inst_curr_ua, di->ccm.original_iset_ua,
  730. di->ccm.current_iset_ua);
  731. if (di->ccm.current_iset_ua == di->ccm.original_iset_ua)
  732. return MAXIM_RET_NOACTION;
  733. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  734. di->ccm.current_iset_ua = di->ccm.original_iset_ua;
  735. di->ccm.level = 0;
  736. return MAXIM_RET_IBAT_TOO_HIGH;
  737. }
  738. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  739. return MAXIM_RET_NOACTION;
  740. }
  741. static void handle_maxim_chg_curr(struct ab8500_chargalg *di)
  742. {
  743. struct power_supply_battery_info *bi = di->bm->bi;
  744. enum maxim_ret ret;
  745. int result;
  746. ret = ab8500_chargalg_chg_curr_maxim(di);
  747. switch (ret) {
  748. case MAXIM_RET_CHANGE:
  749. result = ab8500_chargalg_update_chg_curr(di,
  750. di->ccm.current_iset_ua);
  751. if (result)
  752. dev_err(di->dev, "failed to set chg curr\n");
  753. break;
  754. case MAXIM_RET_IBAT_TOO_HIGH:
  755. result = ab8500_chargalg_update_chg_curr(di,
  756. bi->constant_charge_current_max_ua);
  757. if (result)
  758. dev_err(di->dev, "failed to set chg curr\n");
  759. break;
  760. case MAXIM_RET_NOACTION:
  761. default:
  762. /* Do nothing..*/
  763. break;
  764. }
  765. }
  766. static int ab8500_chargalg_get_ext_psy_data(struct device *dev, void *data)
  767. {
  768. struct power_supply *psy;
  769. struct power_supply *ext = dev_get_drvdata(dev);
  770. const char **supplicants = (const char **)ext->supplied_to;
  771. struct ab8500_chargalg *di;
  772. union power_supply_propval ret;
  773. int j;
  774. bool capacity_updated = false;
  775. psy = (struct power_supply *)data;
  776. di = power_supply_get_drvdata(psy);
  777. /* For all psy where the driver name appears in any supplied_to */
  778. j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
  779. if (j < 0)
  780. return 0;
  781. /*
  782. * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
  783. * property because of handling that sysfs entry on its own, this is
  784. * the place to get the battery capacity.
  785. */
  786. if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
  787. di->batt_data.percent = ret.intval;
  788. capacity_updated = true;
  789. }
  790. /* Go through all properties for the psy */
  791. for (j = 0; j < ext->desc->num_properties; j++) {
  792. enum power_supply_property prop;
  793. prop = ext->desc->properties[j];
  794. /*
  795. * Initialize chargers if not already done.
  796. * The ab8500_charger*/
  797. if (!di->ac_chg &&
  798. ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
  799. di->ac_chg = psy_to_ux500_charger(ext);
  800. else if (!di->usb_chg &&
  801. ext->desc->type == POWER_SUPPLY_TYPE_USB)
  802. di->usb_chg = psy_to_ux500_charger(ext);
  803. if (power_supply_get_property(ext, prop, &ret))
  804. continue;
  805. switch (prop) {
  806. case POWER_SUPPLY_PROP_PRESENT:
  807. switch (ext->desc->type) {
  808. case POWER_SUPPLY_TYPE_BATTERY:
  809. /* Battery present */
  810. if (ret.intval)
  811. di->events.batt_rem = false;
  812. /* Battery removed */
  813. else
  814. di->events.batt_rem = true;
  815. break;
  816. case POWER_SUPPLY_TYPE_MAINS:
  817. /* AC disconnected */
  818. if (!ret.intval &&
  819. (di->chg_info.conn_chg & AC_CHG)) {
  820. di->chg_info.prev_conn_chg =
  821. di->chg_info.conn_chg;
  822. di->chg_info.conn_chg &= ~AC_CHG;
  823. }
  824. /* AC connected */
  825. else if (ret.intval &&
  826. !(di->chg_info.conn_chg & AC_CHG)) {
  827. di->chg_info.prev_conn_chg =
  828. di->chg_info.conn_chg;
  829. di->chg_info.conn_chg |= AC_CHG;
  830. }
  831. break;
  832. case POWER_SUPPLY_TYPE_USB:
  833. /* USB disconnected */
  834. if (!ret.intval &&
  835. (di->chg_info.conn_chg & USB_CHG)) {
  836. di->chg_info.prev_conn_chg =
  837. di->chg_info.conn_chg;
  838. di->chg_info.conn_chg &= ~USB_CHG;
  839. }
  840. /* USB connected */
  841. else if (ret.intval &&
  842. !(di->chg_info.conn_chg & USB_CHG)) {
  843. di->chg_info.prev_conn_chg =
  844. di->chg_info.conn_chg;
  845. di->chg_info.conn_chg |= USB_CHG;
  846. }
  847. break;
  848. default:
  849. break;
  850. }
  851. break;
  852. case POWER_SUPPLY_PROP_ONLINE:
  853. switch (ext->desc->type) {
  854. case POWER_SUPPLY_TYPE_BATTERY:
  855. break;
  856. case POWER_SUPPLY_TYPE_MAINS:
  857. /* AC offline */
  858. if (!ret.intval &&
  859. (di->chg_info.online_chg & AC_CHG)) {
  860. di->chg_info.prev_online_chg =
  861. di->chg_info.online_chg;
  862. di->chg_info.online_chg &= ~AC_CHG;
  863. }
  864. /* AC online */
  865. else if (ret.intval &&
  866. !(di->chg_info.online_chg & AC_CHG)) {
  867. di->chg_info.prev_online_chg =
  868. di->chg_info.online_chg;
  869. di->chg_info.online_chg |= AC_CHG;
  870. queue_delayed_work(di->chargalg_wq,
  871. &di->chargalg_wd_work, 0);
  872. }
  873. break;
  874. case POWER_SUPPLY_TYPE_USB:
  875. /* USB offline */
  876. if (!ret.intval &&
  877. (di->chg_info.online_chg & USB_CHG)) {
  878. di->chg_info.prev_online_chg =
  879. di->chg_info.online_chg;
  880. di->chg_info.online_chg &= ~USB_CHG;
  881. }
  882. /* USB online */
  883. else if (ret.intval &&
  884. !(di->chg_info.online_chg & USB_CHG)) {
  885. di->chg_info.prev_online_chg =
  886. di->chg_info.online_chg;
  887. di->chg_info.online_chg |= USB_CHG;
  888. queue_delayed_work(di->chargalg_wq,
  889. &di->chargalg_wd_work, 0);
  890. }
  891. break;
  892. default:
  893. break;
  894. }
  895. break;
  896. case POWER_SUPPLY_PROP_HEALTH:
  897. switch (ext->desc->type) {
  898. case POWER_SUPPLY_TYPE_BATTERY:
  899. break;
  900. case POWER_SUPPLY_TYPE_MAINS:
  901. switch (ret.intval) {
  902. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  903. di->events.mainextchnotok = true;
  904. di->events.main_thermal_prot = false;
  905. di->events.main_ovv = false;
  906. di->events.ac_wd_expired = false;
  907. break;
  908. case POWER_SUPPLY_HEALTH_DEAD:
  909. di->events.ac_wd_expired = true;
  910. di->events.mainextchnotok = false;
  911. di->events.main_ovv = false;
  912. di->events.main_thermal_prot = false;
  913. break;
  914. case POWER_SUPPLY_HEALTH_COLD:
  915. case POWER_SUPPLY_HEALTH_OVERHEAT:
  916. di->events.main_thermal_prot = true;
  917. di->events.mainextchnotok = false;
  918. di->events.main_ovv = false;
  919. di->events.ac_wd_expired = false;
  920. break;
  921. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  922. di->events.main_ovv = true;
  923. di->events.mainextchnotok = false;
  924. di->events.main_thermal_prot = false;
  925. di->events.ac_wd_expired = false;
  926. break;
  927. case POWER_SUPPLY_HEALTH_GOOD:
  928. di->events.main_thermal_prot = false;
  929. di->events.mainextchnotok = false;
  930. di->events.main_ovv = false;
  931. di->events.ac_wd_expired = false;
  932. break;
  933. default:
  934. break;
  935. }
  936. break;
  937. case POWER_SUPPLY_TYPE_USB:
  938. switch (ret.intval) {
  939. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  940. di->events.usbchargernotok = true;
  941. di->events.usb_thermal_prot = false;
  942. di->events.vbus_ovv = false;
  943. di->events.usb_wd_expired = false;
  944. break;
  945. case POWER_SUPPLY_HEALTH_DEAD:
  946. di->events.usb_wd_expired = true;
  947. di->events.usbchargernotok = false;
  948. di->events.usb_thermal_prot = false;
  949. di->events.vbus_ovv = false;
  950. break;
  951. case POWER_SUPPLY_HEALTH_COLD:
  952. case POWER_SUPPLY_HEALTH_OVERHEAT:
  953. di->events.usb_thermal_prot = true;
  954. di->events.usbchargernotok = false;
  955. di->events.vbus_ovv = false;
  956. di->events.usb_wd_expired = false;
  957. break;
  958. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  959. di->events.vbus_ovv = true;
  960. di->events.usbchargernotok = false;
  961. di->events.usb_thermal_prot = false;
  962. di->events.usb_wd_expired = false;
  963. break;
  964. case POWER_SUPPLY_HEALTH_GOOD:
  965. di->events.usbchargernotok = false;
  966. di->events.usb_thermal_prot = false;
  967. di->events.vbus_ovv = false;
  968. di->events.usb_wd_expired = false;
  969. break;
  970. default:
  971. break;
  972. }
  973. break;
  974. default:
  975. break;
  976. }
  977. break;
  978. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  979. switch (ext->desc->type) {
  980. case POWER_SUPPLY_TYPE_BATTERY:
  981. di->batt_data.volt_uv = ret.intval;
  982. break;
  983. case POWER_SUPPLY_TYPE_MAINS:
  984. di->chg_info.ac_volt_uv = ret.intval;
  985. break;
  986. case POWER_SUPPLY_TYPE_USB:
  987. di->chg_info.usb_volt_uv = ret.intval;
  988. break;
  989. default:
  990. break;
  991. }
  992. break;
  993. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  994. switch (ext->desc->type) {
  995. case POWER_SUPPLY_TYPE_MAINS:
  996. /* AVG is used to indicate when we are
  997. * in CV mode */
  998. if (ret.intval)
  999. di->events.ac_cv_active = true;
  1000. else
  1001. di->events.ac_cv_active = false;
  1002. break;
  1003. case POWER_SUPPLY_TYPE_USB:
  1004. /* AVG is used to indicate when we are
  1005. * in CV mode */
  1006. if (ret.intval)
  1007. di->events.usb_cv_active = true;
  1008. else
  1009. di->events.usb_cv_active = false;
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. break;
  1015. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1016. switch (ext->desc->type) {
  1017. case POWER_SUPPLY_TYPE_BATTERY:
  1018. if (ret.intval)
  1019. di->events.batt_unknown = false;
  1020. else
  1021. di->events.batt_unknown = true;
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. break;
  1027. case POWER_SUPPLY_PROP_TEMP:
  1028. di->batt_data.temp = ret.intval / 10;
  1029. break;
  1030. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1031. switch (ext->desc->type) {
  1032. case POWER_SUPPLY_TYPE_MAINS:
  1033. di->chg_info.ac_curr_ua = ret.intval;
  1034. break;
  1035. case POWER_SUPPLY_TYPE_USB:
  1036. di->chg_info.usb_curr_ua = ret.intval;
  1037. break;
  1038. case POWER_SUPPLY_TYPE_BATTERY:
  1039. di->batt_data.inst_curr_ua = ret.intval;
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. break;
  1045. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1046. switch (ext->desc->type) {
  1047. case POWER_SUPPLY_TYPE_BATTERY:
  1048. di->batt_data.avg_curr_ua = ret.intval;
  1049. break;
  1050. case POWER_SUPPLY_TYPE_USB:
  1051. if (ret.intval)
  1052. di->events.vbus_collapsed = true;
  1053. else
  1054. di->events.vbus_collapsed = false;
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. break;
  1060. case POWER_SUPPLY_PROP_CAPACITY:
  1061. if (!capacity_updated)
  1062. di->batt_data.percent = ret.intval;
  1063. break;
  1064. default:
  1065. break;
  1066. }
  1067. }
  1068. return 0;
  1069. }
  1070. /**
  1071. * ab8500_chargalg_external_power_changed() - callback for power supply changes
  1072. * @psy: pointer to the structure power_supply
  1073. *
  1074. * This function is the entry point of the pointer external_power_changed
  1075. * of the structure power_supply.
  1076. * This function gets executed when there is a change in any external power
  1077. * supply that this driver needs to be notified of.
  1078. */
  1079. static void ab8500_chargalg_external_power_changed(struct power_supply *psy)
  1080. {
  1081. struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
  1082. /*
  1083. * Trigger execution of the algorithm instantly and read
  1084. * all power_supply properties there instead
  1085. */
  1086. if (di->chargalg_wq)
  1087. queue_work(di->chargalg_wq, &di->chargalg_work);
  1088. }
  1089. /**
  1090. * ab8500_chargalg_time_to_restart() - time to restart CC/CV charging?
  1091. * @di: charging algorithm state
  1092. *
  1093. * This checks if the voltage or capacity of the battery has fallen so
  1094. * low that we need to restart the CC/CV charge cycle.
  1095. */
  1096. static bool ab8500_chargalg_time_to_restart(struct ab8500_chargalg *di)
  1097. {
  1098. struct power_supply_battery_info *bi = di->bm->bi;
  1099. /* Sanity check - these need to have some reasonable values */
  1100. if (!di->batt_data.volt_uv || !di->batt_data.percent)
  1101. return false;
  1102. /* Some batteries tell us at which voltage we should restart charging */
  1103. if (bi->charge_restart_voltage_uv > 0) {
  1104. if (di->batt_data.volt_uv <= bi->charge_restart_voltage_uv)
  1105. return true;
  1106. /* Else we restart as we reach a certain capacity */
  1107. } else {
  1108. if (di->batt_data.percent <= AB8500_RECHARGE_CAP)
  1109. return true;
  1110. }
  1111. return false;
  1112. }
  1113. /**
  1114. * ab8500_chargalg_algorithm() - Main function for the algorithm
  1115. * @di: pointer to the ab8500_chargalg structure
  1116. *
  1117. * This is the main control function for the charging algorithm.
  1118. * It is called periodically or when something happens that will
  1119. * trigger a state change
  1120. */
  1121. static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
  1122. {
  1123. struct power_supply_battery_info *bi = di->bm->bi;
  1124. struct power_supply_maintenance_charge_table *mt;
  1125. int charger_status;
  1126. int ret;
  1127. /* Collect data from all power_supply class devices */
  1128. class_for_each_device(power_supply_class, NULL,
  1129. di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
  1130. ab8500_chargalg_end_of_charge(di);
  1131. ab8500_chargalg_check_temp(di);
  1132. ab8500_chargalg_check_charger_voltage(di);
  1133. charger_status = ab8500_chargalg_check_charger_connection(di);
  1134. if (is_ab8500(di->parent)) {
  1135. ret = ab8500_chargalg_check_charger_enable(di);
  1136. if (ret < 0)
  1137. dev_err(di->dev, "Checking charger is enabled error"
  1138. ": Returned Value %d\n", ret);
  1139. }
  1140. /*
  1141. * First check if we have a charger connected.
  1142. * Also we don't allow charging of unknown batteries if configured
  1143. * this way
  1144. */
  1145. if (!charger_status ||
  1146. (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
  1147. if (di->charge_state != STATE_HANDHELD) {
  1148. di->events.safety_timer_expired = false;
  1149. ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
  1150. }
  1151. }
  1152. /* Safety timer expiration */
  1153. else if (di->events.safety_timer_expired) {
  1154. if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
  1155. ab8500_chargalg_state_to(di,
  1156. STATE_SAFETY_TIMER_EXPIRED_INIT);
  1157. }
  1158. /*
  1159. * Check if any interrupts has occurred
  1160. * that will prevent us from charging
  1161. */
  1162. /* Battery removed */
  1163. else if (di->events.batt_rem) {
  1164. if (di->charge_state != STATE_BATT_REMOVED)
  1165. ab8500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
  1166. }
  1167. /* Main or USB charger not ok. */
  1168. else if (di->events.mainextchnotok || di->events.usbchargernotok) {
  1169. /*
  1170. * If vbus_collapsed is set, we have to lower the charger
  1171. * current, which is done in the normal state below
  1172. */
  1173. if (di->charge_state != STATE_CHG_NOT_OK &&
  1174. !di->events.vbus_collapsed)
  1175. ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
  1176. }
  1177. /* VBUS, Main or VBAT OVV. */
  1178. else if (di->events.vbus_ovv ||
  1179. di->events.main_ovv ||
  1180. di->events.batt_ovv ||
  1181. !di->chg_info.usb_chg_ok ||
  1182. !di->chg_info.ac_chg_ok) {
  1183. if (di->charge_state != STATE_OVV_PROTECT)
  1184. ab8500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
  1185. }
  1186. /* USB Thermal, stop charging */
  1187. else if (di->events.main_thermal_prot ||
  1188. di->events.usb_thermal_prot) {
  1189. if (di->charge_state != STATE_HW_TEMP_PROTECT)
  1190. ab8500_chargalg_state_to(di,
  1191. STATE_HW_TEMP_PROTECT_INIT);
  1192. }
  1193. /* Battery temp over/under */
  1194. else if (di->events.btemp_underover) {
  1195. if (di->charge_state != STATE_TEMP_UNDEROVER)
  1196. ab8500_chargalg_state_to(di,
  1197. STATE_TEMP_UNDEROVER_INIT);
  1198. }
  1199. /* Watchdog expired */
  1200. else if (di->events.ac_wd_expired ||
  1201. di->events.usb_wd_expired) {
  1202. if (di->charge_state != STATE_WD_EXPIRED)
  1203. ab8500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
  1204. }
  1205. /* Battery temp high/low */
  1206. else if (di->events.btemp_low || di->events.btemp_high) {
  1207. if (di->charge_state != STATE_TEMP_LOWHIGH)
  1208. ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
  1209. }
  1210. dev_dbg(di->dev,
  1211. "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
  1212. "State %s Active_chg %d Chg_status %d AC %d USB %d "
  1213. "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
  1214. "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
  1215. di->batt_data.volt_uv,
  1216. di->batt_data.avg_curr_ua,
  1217. di->batt_data.inst_curr_ua,
  1218. di->batt_data.temp,
  1219. di->batt_data.percent,
  1220. di->maintenance_chg,
  1221. states[di->charge_state],
  1222. di->chg_info.charger_type,
  1223. di->charge_status,
  1224. di->chg_info.conn_chg & AC_CHG,
  1225. di->chg_info.conn_chg & USB_CHG,
  1226. di->chg_info.online_chg & AC_CHG,
  1227. di->chg_info.online_chg & USB_CHG,
  1228. di->events.ac_cv_active,
  1229. di->events.usb_cv_active,
  1230. di->chg_info.ac_curr_ua,
  1231. di->chg_info.usb_curr_ua,
  1232. di->chg_info.ac_vset_uv,
  1233. di->chg_info.ac_iset_ua,
  1234. di->chg_info.usb_vset_uv,
  1235. di->chg_info.usb_iset_ua);
  1236. switch (di->charge_state) {
  1237. case STATE_HANDHELD_INIT:
  1238. ab8500_chargalg_stop_charging(di);
  1239. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  1240. ab8500_chargalg_state_to(di, STATE_HANDHELD);
  1241. fallthrough;
  1242. case STATE_HANDHELD:
  1243. break;
  1244. case STATE_BATT_REMOVED_INIT:
  1245. ab8500_chargalg_stop_charging(di);
  1246. ab8500_chargalg_state_to(di, STATE_BATT_REMOVED);
  1247. fallthrough;
  1248. case STATE_BATT_REMOVED:
  1249. if (!di->events.batt_rem)
  1250. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1251. break;
  1252. case STATE_HW_TEMP_PROTECT_INIT:
  1253. ab8500_chargalg_stop_charging(di);
  1254. ab8500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
  1255. fallthrough;
  1256. case STATE_HW_TEMP_PROTECT:
  1257. if (!di->events.main_thermal_prot &&
  1258. !di->events.usb_thermal_prot)
  1259. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1260. break;
  1261. case STATE_OVV_PROTECT_INIT:
  1262. ab8500_chargalg_stop_charging(di);
  1263. ab8500_chargalg_state_to(di, STATE_OVV_PROTECT);
  1264. fallthrough;
  1265. case STATE_OVV_PROTECT:
  1266. if (!di->events.vbus_ovv &&
  1267. !di->events.main_ovv &&
  1268. !di->events.batt_ovv &&
  1269. di->chg_info.usb_chg_ok &&
  1270. di->chg_info.ac_chg_ok)
  1271. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1272. break;
  1273. case STATE_CHG_NOT_OK_INIT:
  1274. ab8500_chargalg_stop_charging(di);
  1275. ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK);
  1276. fallthrough;
  1277. case STATE_CHG_NOT_OK:
  1278. if (!di->events.mainextchnotok &&
  1279. !di->events.usbchargernotok)
  1280. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1281. break;
  1282. case STATE_SAFETY_TIMER_EXPIRED_INIT:
  1283. ab8500_chargalg_stop_charging(di);
  1284. ab8500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
  1285. fallthrough;
  1286. case STATE_SAFETY_TIMER_EXPIRED:
  1287. /* We exit this state when charger is removed */
  1288. break;
  1289. case STATE_NORMAL_INIT:
  1290. if (bi->constant_charge_current_max_ua == 0)
  1291. /* "charging" with 0 uA */
  1292. ab8500_chargalg_stop_charging(di);
  1293. else {
  1294. ab8500_chargalg_start_charging(di,
  1295. bi->constant_charge_voltage_max_uv,
  1296. bi->constant_charge_current_max_ua);
  1297. }
  1298. ab8500_chargalg_state_to(di, STATE_NORMAL);
  1299. ab8500_chargalg_start_safety_timer(di);
  1300. ab8500_chargalg_stop_maintenance_timer(di);
  1301. init_maxim_chg_curr(di);
  1302. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1303. di->eoc_cnt = 0;
  1304. di->maintenance_chg = false;
  1305. power_supply_changed(di->chargalg_psy);
  1306. break;
  1307. case STATE_NORMAL:
  1308. handle_maxim_chg_curr(di);
  1309. if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
  1310. di->maintenance_chg) {
  1311. /*
  1312. * The battery is fully charged, check if we support
  1313. * maintenance charging else go back to waiting for
  1314. * the recharge voltage limit.
  1315. */
  1316. if (!power_supply_supports_maintenance_charging(bi))
  1317. ab8500_chargalg_state_to(di,
  1318. STATE_WAIT_FOR_RECHARGE_INIT);
  1319. else
  1320. ab8500_chargalg_state_to(di,
  1321. STATE_MAINTENANCE_A_INIT);
  1322. }
  1323. break;
  1324. /* This state will be used when the maintenance state is disabled */
  1325. case STATE_WAIT_FOR_RECHARGE_INIT:
  1326. ab8500_chargalg_hold_charging(di);
  1327. ab8500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
  1328. fallthrough;
  1329. case STATE_WAIT_FOR_RECHARGE:
  1330. if (ab8500_chargalg_time_to_restart(di))
  1331. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1332. break;
  1333. case STATE_MAINTENANCE_A_INIT:
  1334. mt = power_supply_get_maintenance_charging_setting(bi, 0);
  1335. if (!mt) {
  1336. /* No maintenance A state, go back to normal */
  1337. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1338. power_supply_changed(di->chargalg_psy);
  1339. break;
  1340. }
  1341. ab8500_chargalg_stop_safety_timer(di);
  1342. ab8500_chargalg_start_maintenance_timer(di,
  1343. mt->charge_safety_timer_minutes);
  1344. ab8500_chargalg_start_charging(di,
  1345. mt->charge_voltage_max_uv,
  1346. mt->charge_current_max_ua);
  1347. ab8500_chargalg_state_to(di, STATE_MAINTENANCE_A);
  1348. power_supply_changed(di->chargalg_psy);
  1349. fallthrough;
  1350. case STATE_MAINTENANCE_A:
  1351. if (di->events.maintenance_timer_expired) {
  1352. ab8500_chargalg_stop_maintenance_timer(di);
  1353. ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
  1354. }
  1355. /*
  1356. * This happens if the voltage drops too quickly during
  1357. * maintenance charging, especially in older batteries.
  1358. */
  1359. if (ab8500_chargalg_time_to_restart(di)) {
  1360. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1361. dev_info(di->dev, "restarted charging from maintenance state A - battery getting old?\n");
  1362. }
  1363. break;
  1364. case STATE_MAINTENANCE_B_INIT:
  1365. mt = power_supply_get_maintenance_charging_setting(bi, 1);
  1366. if (!mt) {
  1367. /* No maintenance B state, go back to normal */
  1368. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1369. power_supply_changed(di->chargalg_psy);
  1370. break;
  1371. }
  1372. ab8500_chargalg_start_maintenance_timer(di,
  1373. mt->charge_safety_timer_minutes);
  1374. ab8500_chargalg_start_charging(di,
  1375. mt->charge_voltage_max_uv,
  1376. mt->charge_current_max_ua);
  1377. ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B);
  1378. power_supply_changed(di->chargalg_psy);
  1379. fallthrough;
  1380. case STATE_MAINTENANCE_B:
  1381. if (di->events.maintenance_timer_expired) {
  1382. ab8500_chargalg_stop_maintenance_timer(di);
  1383. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1384. }
  1385. /*
  1386. * This happens if the voltage drops too quickly during
  1387. * maintenance charging, especially in older batteries.
  1388. */
  1389. if (ab8500_chargalg_time_to_restart(di)) {
  1390. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1391. dev_info(di->dev, "restarted charging from maintenance state B - battery getting old?\n");
  1392. }
  1393. break;
  1394. case STATE_TEMP_LOWHIGH_INIT:
  1395. if (di->events.btemp_low) {
  1396. ab8500_chargalg_start_charging(di,
  1397. bi->alert_low_temp_charge_voltage_uv,
  1398. bi->alert_low_temp_charge_current_ua);
  1399. } else if (di->events.btemp_high) {
  1400. ab8500_chargalg_start_charging(di,
  1401. bi->alert_high_temp_charge_voltage_uv,
  1402. bi->alert_high_temp_charge_current_ua);
  1403. } else {
  1404. dev_err(di->dev, "neither low or high temp event occurred\n");
  1405. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1406. break;
  1407. }
  1408. ab8500_chargalg_stop_maintenance_timer(di);
  1409. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1410. ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
  1411. power_supply_changed(di->chargalg_psy);
  1412. fallthrough;
  1413. case STATE_TEMP_LOWHIGH:
  1414. if (!di->events.btemp_low && !di->events.btemp_high)
  1415. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1416. break;
  1417. case STATE_WD_EXPIRED_INIT:
  1418. ab8500_chargalg_stop_charging(di);
  1419. ab8500_chargalg_state_to(di, STATE_WD_EXPIRED);
  1420. fallthrough;
  1421. case STATE_WD_EXPIRED:
  1422. if (!di->events.ac_wd_expired &&
  1423. !di->events.usb_wd_expired)
  1424. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1425. break;
  1426. case STATE_TEMP_UNDEROVER_INIT:
  1427. ab8500_chargalg_stop_charging(di);
  1428. ab8500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
  1429. fallthrough;
  1430. case STATE_TEMP_UNDEROVER:
  1431. if (!di->events.btemp_underover)
  1432. ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1433. break;
  1434. }
  1435. /* Start charging directly if the new state is a charge state */
  1436. if (di->charge_state == STATE_NORMAL_INIT ||
  1437. di->charge_state == STATE_MAINTENANCE_A_INIT ||
  1438. di->charge_state == STATE_MAINTENANCE_B_INIT)
  1439. queue_work(di->chargalg_wq, &di->chargalg_work);
  1440. }
  1441. /**
  1442. * ab8500_chargalg_periodic_work() - Periodic work for the algorithm
  1443. * @work: pointer to the work_struct structure
  1444. *
  1445. * Work queue function for the charging algorithm
  1446. */
  1447. static void ab8500_chargalg_periodic_work(struct work_struct *work)
  1448. {
  1449. struct ab8500_chargalg *di = container_of(work,
  1450. struct ab8500_chargalg, chargalg_periodic_work.work);
  1451. ab8500_chargalg_algorithm(di);
  1452. /*
  1453. * If a charger is connected then the battery has to be monitored
  1454. * frequently, else the work can be delayed.
  1455. */
  1456. if (di->chg_info.conn_chg)
  1457. queue_delayed_work(di->chargalg_wq,
  1458. &di->chargalg_periodic_work,
  1459. di->bm->interval_charging * HZ);
  1460. else
  1461. queue_delayed_work(di->chargalg_wq,
  1462. &di->chargalg_periodic_work,
  1463. di->bm->interval_not_charging * HZ);
  1464. }
  1465. /**
  1466. * ab8500_chargalg_wd_work() - periodic work to kick the charger watchdog
  1467. * @work: pointer to the work_struct structure
  1468. *
  1469. * Work queue function for kicking the charger watchdog
  1470. */
  1471. static void ab8500_chargalg_wd_work(struct work_struct *work)
  1472. {
  1473. int ret;
  1474. struct ab8500_chargalg *di = container_of(work,
  1475. struct ab8500_chargalg, chargalg_wd_work.work);
  1476. ret = ab8500_chargalg_kick_watchdog(di);
  1477. if (ret < 0)
  1478. dev_err(di->dev, "failed to kick watchdog\n");
  1479. queue_delayed_work(di->chargalg_wq,
  1480. &di->chargalg_wd_work, CHG_WD_INTERVAL);
  1481. }
  1482. /**
  1483. * ab8500_chargalg_work() - Work to run the charging algorithm instantly
  1484. * @work: pointer to the work_struct structure
  1485. *
  1486. * Work queue function for calling the charging algorithm
  1487. */
  1488. static void ab8500_chargalg_work(struct work_struct *work)
  1489. {
  1490. struct ab8500_chargalg *di = container_of(work,
  1491. struct ab8500_chargalg, chargalg_work);
  1492. ab8500_chargalg_algorithm(di);
  1493. }
  1494. /**
  1495. * ab8500_chargalg_get_property() - get the chargalg properties
  1496. * @psy: pointer to the power_supply structure
  1497. * @psp: pointer to the power_supply_property structure
  1498. * @val: pointer to the power_supply_propval union
  1499. *
  1500. * This function gets called when an application tries to get the
  1501. * chargalg properties by reading the sysfs files.
  1502. * status: charging/discharging/full/unknown
  1503. * health: health of the battery
  1504. * Returns error code in case of failure else 0 on success
  1505. */
  1506. static int ab8500_chargalg_get_property(struct power_supply *psy,
  1507. enum power_supply_property psp,
  1508. union power_supply_propval *val)
  1509. {
  1510. struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
  1511. switch (psp) {
  1512. case POWER_SUPPLY_PROP_STATUS:
  1513. val->intval = di->charge_status;
  1514. break;
  1515. case POWER_SUPPLY_PROP_HEALTH:
  1516. if (di->events.batt_ovv) {
  1517. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1518. } else if (di->events.btemp_underover) {
  1519. if (di->batt_data.temp <= di->bm->bi->temp_min)
  1520. val->intval = POWER_SUPPLY_HEALTH_COLD;
  1521. else
  1522. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  1523. } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
  1524. di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
  1525. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  1526. } else {
  1527. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  1528. }
  1529. break;
  1530. default:
  1531. return -EINVAL;
  1532. }
  1533. return 0;
  1534. }
  1535. static int __maybe_unused ab8500_chargalg_resume(struct device *dev)
  1536. {
  1537. struct ab8500_chargalg *di = dev_get_drvdata(dev);
  1538. /* Kick charger watchdog if charging (any charger online) */
  1539. if (di->chg_info.online_chg)
  1540. queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
  1541. /*
  1542. * Run the charging algorithm directly to be sure we don't
  1543. * do it too seldom
  1544. */
  1545. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1546. return 0;
  1547. }
  1548. static int __maybe_unused ab8500_chargalg_suspend(struct device *dev)
  1549. {
  1550. struct ab8500_chargalg *di = dev_get_drvdata(dev);
  1551. if (di->chg_info.online_chg)
  1552. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1553. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1554. return 0;
  1555. }
  1556. static char *supply_interface[] = {
  1557. "ab8500_fg",
  1558. };
  1559. static const struct power_supply_desc ab8500_chargalg_desc = {
  1560. .name = "ab8500_chargalg",
  1561. .type = POWER_SUPPLY_TYPE_UNKNOWN,
  1562. .properties = ab8500_chargalg_props,
  1563. .num_properties = ARRAY_SIZE(ab8500_chargalg_props),
  1564. .get_property = ab8500_chargalg_get_property,
  1565. .external_power_changed = ab8500_chargalg_external_power_changed,
  1566. };
  1567. static int ab8500_chargalg_bind(struct device *dev, struct device *master,
  1568. void *data)
  1569. {
  1570. struct ab8500_chargalg *di = dev_get_drvdata(dev);
  1571. /* Create a work queue for the chargalg */
  1572. di->chargalg_wq = alloc_ordered_workqueue("ab8500_chargalg_wq",
  1573. WQ_MEM_RECLAIM);
  1574. if (di->chargalg_wq == NULL) {
  1575. dev_err(di->dev, "failed to create work queue\n");
  1576. return -ENOMEM;
  1577. }
  1578. /* Run the charging algorithm */
  1579. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1580. return 0;
  1581. }
  1582. static void ab8500_chargalg_unbind(struct device *dev, struct device *master,
  1583. void *data)
  1584. {
  1585. struct ab8500_chargalg *di = dev_get_drvdata(dev);
  1586. /* Stop all timers and work */
  1587. hrtimer_cancel(&di->safety_timer);
  1588. hrtimer_cancel(&di->maintenance_timer);
  1589. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1590. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1591. cancel_work_sync(&di->chargalg_work);
  1592. /* Delete the work queue */
  1593. destroy_workqueue(di->chargalg_wq);
  1594. }
  1595. static const struct component_ops ab8500_chargalg_component_ops = {
  1596. .bind = ab8500_chargalg_bind,
  1597. .unbind = ab8500_chargalg_unbind,
  1598. };
  1599. static int ab8500_chargalg_probe(struct platform_device *pdev)
  1600. {
  1601. struct device *dev = &pdev->dev;
  1602. struct power_supply_config psy_cfg = {};
  1603. struct ab8500_chargalg *di;
  1604. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  1605. if (!di)
  1606. return -ENOMEM;
  1607. di->bm = &ab8500_bm_data;
  1608. /* get device struct and parent */
  1609. di->dev = dev;
  1610. di->parent = dev_get_drvdata(pdev->dev.parent);
  1611. psy_cfg.supplied_to = supply_interface;
  1612. psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  1613. psy_cfg.drv_data = di;
  1614. /* Initilialize safety timer */
  1615. hrtimer_init(&di->safety_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1616. di->safety_timer.function = ab8500_chargalg_safety_timer_expired;
  1617. /* Initilialize maintenance timer */
  1618. hrtimer_init(&di->maintenance_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1619. di->maintenance_timer.function =
  1620. ab8500_chargalg_maintenance_timer_expired;
  1621. /* Init work for chargalg */
  1622. INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
  1623. ab8500_chargalg_periodic_work);
  1624. INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
  1625. ab8500_chargalg_wd_work);
  1626. /* Init work for chargalg */
  1627. INIT_WORK(&di->chargalg_work, ab8500_chargalg_work);
  1628. /* To detect charger at startup */
  1629. di->chg_info.prev_conn_chg = -1;
  1630. /* Register chargalg power supply class */
  1631. di->chargalg_psy = devm_power_supply_register(di->dev,
  1632. &ab8500_chargalg_desc,
  1633. &psy_cfg);
  1634. if (IS_ERR(di->chargalg_psy)) {
  1635. dev_err(di->dev, "failed to register chargalg psy\n");
  1636. return PTR_ERR(di->chargalg_psy);
  1637. }
  1638. platform_set_drvdata(pdev, di);
  1639. dev_info(di->dev, "probe success\n");
  1640. return component_add(dev, &ab8500_chargalg_component_ops);
  1641. }
  1642. static int ab8500_chargalg_remove(struct platform_device *pdev)
  1643. {
  1644. component_del(&pdev->dev, &ab8500_chargalg_component_ops);
  1645. return 0;
  1646. }
  1647. static SIMPLE_DEV_PM_OPS(ab8500_chargalg_pm_ops, ab8500_chargalg_suspend, ab8500_chargalg_resume);
  1648. static const struct of_device_id ab8500_chargalg_match[] = {
  1649. { .compatible = "stericsson,ab8500-chargalg", },
  1650. { },
  1651. };
  1652. struct platform_driver ab8500_chargalg_driver = {
  1653. .probe = ab8500_chargalg_probe,
  1654. .remove = ab8500_chargalg_remove,
  1655. .driver = {
  1656. .name = "ab8500_chargalg",
  1657. .of_match_table = ab8500_chargalg_match,
  1658. .pm = &ab8500_chargalg_pm_ops,
  1659. },
  1660. };
  1661. MODULE_LICENSE("GPL v2");
  1662. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  1663. MODULE_ALIAS("platform:ab8500-chargalg");
  1664. MODULE_DESCRIPTION("ab8500 battery charging algorithm");