abx500_chargalg.c 54 KB

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