abx500_chargalg.c 54 KB

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