abx500_chargalg.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. switch (di->chg_info.charger_type) {
  555. case AC_CHG:
  556. dev_dbg(di->dev,
  557. "AC parameters: Vset %d, Ich %d\n", vset, iset);
  558. abx500_chargalg_usb_en(di, false, 0, 0);
  559. abx500_chargalg_ac_en(di, true, vset, iset);
  560. break;
  561. case USB_CHG:
  562. dev_dbg(di->dev,
  563. "USB parameters: Vset %d, Ich %d\n", vset, iset);
  564. abx500_chargalg_ac_en(di, false, 0, 0);
  565. abx500_chargalg_usb_en(di, true, vset, iset);
  566. break;
  567. default:
  568. dev_err(di->dev, "Unknown charger to charge from\n");
  569. break;
  570. }
  571. }
  572. /**
  573. * abx500_chargalg_check_temp() - Check battery temperature ranges
  574. * @di: pointer to the abx500_chargalg structure
  575. *
  576. * The battery temperature is checked against the predefined limits and the
  577. * charge state is changed accordingly
  578. */
  579. static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
  580. {
  581. if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
  582. di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
  583. /* Temp OK! */
  584. di->events.btemp_underover = false;
  585. di->events.btemp_lowhigh = false;
  586. di->t_hyst_norm = 0;
  587. di->t_hyst_lowhigh = 0;
  588. } else {
  589. if (((di->batt_data.temp >= di->bm->temp_high) &&
  590. (di->batt_data.temp <
  591. (di->bm->temp_over - di->t_hyst_lowhigh))) ||
  592. ((di->batt_data.temp >
  593. (di->bm->temp_under + di->t_hyst_lowhigh)) &&
  594. (di->batt_data.temp <= di->bm->temp_low))) {
  595. /* TEMP minor!!!!! */
  596. di->events.btemp_underover = false;
  597. di->events.btemp_lowhigh = true;
  598. di->t_hyst_norm = di->bm->temp_hysteresis;
  599. di->t_hyst_lowhigh = 0;
  600. } else if (di->batt_data.temp <= di->bm->temp_under ||
  601. di->batt_data.temp >= di->bm->temp_over) {
  602. /* TEMP major!!!!! */
  603. di->events.btemp_underover = true;
  604. di->events.btemp_lowhigh = false;
  605. di->t_hyst_norm = 0;
  606. di->t_hyst_lowhigh = di->bm->temp_hysteresis;
  607. } else {
  608. /* Within hysteresis */
  609. dev_dbg(di->dev, "Within hysteresis limit temp: %d "
  610. "hyst_lowhigh %d, hyst normal %d\n",
  611. di->batt_data.temp, di->t_hyst_lowhigh,
  612. di->t_hyst_norm);
  613. }
  614. }
  615. }
  616. /**
  617. * abx500_chargalg_check_charger_voltage() - Check charger voltage
  618. * @di: pointer to the abx500_chargalg structure
  619. *
  620. * Charger voltage is checked against maximum limit
  621. */
  622. static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
  623. {
  624. if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
  625. di->chg_info.usb_chg_ok = false;
  626. else
  627. di->chg_info.usb_chg_ok = true;
  628. if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
  629. di->chg_info.ac_chg_ok = false;
  630. else
  631. di->chg_info.ac_chg_ok = true;
  632. }
  633. /**
  634. * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
  635. * @di: pointer to the abx500_chargalg structure
  636. *
  637. * End-of-charge criteria is fulfilled when the battery voltage is above a
  638. * certain limit and the battery current is below a certain limit for a
  639. * predefined number of consecutive seconds. If true, the battery is full
  640. */
  641. static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
  642. {
  643. if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
  644. di->charge_state == STATE_NORMAL &&
  645. !di->maintenance_chg && (di->batt_data.volt >=
  646. di->bm->bat_type[di->bm->batt_id].termination_vol ||
  647. di->events.usb_cv_active || di->events.ac_cv_active) &&
  648. di->batt_data.avg_curr <
  649. di->bm->bat_type[di->bm->batt_id].termination_curr &&
  650. di->batt_data.avg_curr > 0) {
  651. if (++di->eoc_cnt >= EOC_COND_CNT) {
  652. di->eoc_cnt = 0;
  653. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  654. di->maintenance_chg = true;
  655. dev_dbg(di->dev, "EOC reached!\n");
  656. power_supply_changed(&di->chargalg_psy);
  657. } else {
  658. dev_dbg(di->dev,
  659. " EOC limit reached for the %d"
  660. " time, out of %d before EOC\n",
  661. di->eoc_cnt,
  662. EOC_COND_CNT);
  663. }
  664. } else {
  665. di->eoc_cnt = 0;
  666. }
  667. }
  668. static void init_maxim_chg_curr(struct abx500_chargalg *di)
  669. {
  670. di->ccm.original_iset =
  671. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
  672. di->ccm.current_iset =
  673. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
  674. di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
  675. di->ccm.max_current = di->bm->maxi->chg_curr;
  676. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  677. di->ccm.level = 0;
  678. }
  679. /**
  680. * abx500_chargalg_chg_curr_maxim - increases the charger current to
  681. * compensate for the system load
  682. * @di pointer to the abx500_chargalg structure
  683. *
  684. * This maximization function is used to raise the charger current to get the
  685. * battery current as close to the optimal value as possible. The battery
  686. * current during charging is affected by the system load
  687. */
  688. static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
  689. {
  690. int delta_i;
  691. if (!di->bm->maxi->ena_maxi)
  692. return MAXIM_RET_NOACTION;
  693. delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
  694. if (di->events.vbus_collapsed) {
  695. dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
  696. di->ccm.wait_cnt);
  697. if (di->ccm.wait_cnt == 0) {
  698. dev_dbg(di->dev, "lowering current\n");
  699. di->ccm.wait_cnt++;
  700. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  701. di->ccm.max_current =
  702. di->ccm.current_iset - di->ccm.test_delta_i;
  703. di->ccm.current_iset = di->ccm.max_current;
  704. di->ccm.level--;
  705. return MAXIM_RET_CHANGE;
  706. } else {
  707. dev_dbg(di->dev, "waiting\n");
  708. /* Let's go in here twice before lowering curr again */
  709. di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
  710. return MAXIM_RET_NOACTION;
  711. }
  712. }
  713. di->ccm.wait_cnt = 0;
  714. if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
  715. dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
  716. " (limit %dmA) (current iset: %dmA)!\n",
  717. di->batt_data.inst_curr, di->ccm.original_iset,
  718. di->ccm.current_iset);
  719. if (di->ccm.current_iset == di->ccm.original_iset)
  720. return MAXIM_RET_NOACTION;
  721. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  722. di->ccm.current_iset = di->ccm.original_iset;
  723. di->ccm.level = 0;
  724. return MAXIM_RET_IBAT_TOO_HIGH;
  725. }
  726. if (delta_i > di->ccm.test_delta_i &&
  727. (di->ccm.current_iset + di->ccm.test_delta_i) <
  728. di->ccm.max_current) {
  729. if (di->ccm.condition_cnt-- == 0) {
  730. /* Increse the iset with cco.test_delta_i */
  731. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  732. di->ccm.current_iset += di->ccm.test_delta_i;
  733. di->ccm.level++;
  734. dev_dbg(di->dev, " Maximization needed, increase"
  735. " with %d mA to %dmA (Optimal ibat: %d)"
  736. " Level %d\n",
  737. di->ccm.test_delta_i,
  738. di->ccm.current_iset,
  739. di->ccm.original_iset,
  740. di->ccm.level);
  741. return MAXIM_RET_CHANGE;
  742. } else {
  743. return MAXIM_RET_NOACTION;
  744. }
  745. } else {
  746. di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
  747. return MAXIM_RET_NOACTION;
  748. }
  749. }
  750. static void handle_maxim_chg_curr(struct abx500_chargalg *di)
  751. {
  752. enum maxim_ret ret;
  753. int result;
  754. ret = abx500_chargalg_chg_curr_maxim(di);
  755. switch (ret) {
  756. case MAXIM_RET_CHANGE:
  757. result = abx500_chargalg_update_chg_curr(di,
  758. di->ccm.current_iset);
  759. if (result)
  760. dev_err(di->dev, "failed to set chg curr\n");
  761. break;
  762. case MAXIM_RET_IBAT_TOO_HIGH:
  763. result = abx500_chargalg_update_chg_curr(di,
  764. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
  765. if (result)
  766. dev_err(di->dev, "failed to set chg curr\n");
  767. break;
  768. case MAXIM_RET_NOACTION:
  769. default:
  770. /* Do nothing..*/
  771. break;
  772. }
  773. }
  774. static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
  775. {
  776. struct power_supply *psy;
  777. struct power_supply *ext;
  778. struct abx500_chargalg *di;
  779. union power_supply_propval ret;
  780. int i, j;
  781. bool psy_found = false;
  782. bool capacity_updated = false;
  783. psy = (struct power_supply *)data;
  784. ext = dev_get_drvdata(dev);
  785. di = to_abx500_chargalg_device_info(psy);
  786. /* For all psy where the driver name appears in any supplied_to */
  787. for (i = 0; i < ext->num_supplicants; i++) {
  788. if (!strcmp(ext->supplied_to[i], psy->name))
  789. psy_found = true;
  790. }
  791. if (!psy_found)
  792. return 0;
  793. /*
  794. * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
  795. * property because of handling that sysfs entry on its own, this is
  796. * the place to get the battery capacity.
  797. */
  798. if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
  799. di->batt_data.percent = ret.intval;
  800. capacity_updated = true;
  801. }
  802. /* Go through all properties for the psy */
  803. for (j = 0; j < ext->num_properties; j++) {
  804. enum power_supply_property prop;
  805. prop = ext->properties[j];
  806. /* Initialize chargers if not already done */
  807. if (!di->ac_chg &&
  808. ext->type == POWER_SUPPLY_TYPE_MAINS)
  809. di->ac_chg = psy_to_ux500_charger(ext);
  810. else if (!di->usb_chg &&
  811. ext->type == POWER_SUPPLY_TYPE_USB)
  812. di->usb_chg = psy_to_ux500_charger(ext);
  813. if (ext->get_property(ext, prop, &ret))
  814. continue;
  815. switch (prop) {
  816. case POWER_SUPPLY_PROP_PRESENT:
  817. switch (ext->type) {
  818. case POWER_SUPPLY_TYPE_BATTERY:
  819. /* Battery present */
  820. if (ret.intval)
  821. di->events.batt_rem = false;
  822. /* Battery removed */
  823. else
  824. di->events.batt_rem = true;
  825. break;
  826. case POWER_SUPPLY_TYPE_MAINS:
  827. /* AC disconnected */
  828. if (!ret.intval &&
  829. (di->chg_info.conn_chg & AC_CHG)) {
  830. di->chg_info.prev_conn_chg =
  831. di->chg_info.conn_chg;
  832. di->chg_info.conn_chg &= ~AC_CHG;
  833. }
  834. /* AC connected */
  835. else if (ret.intval &&
  836. !(di->chg_info.conn_chg & AC_CHG)) {
  837. di->chg_info.prev_conn_chg =
  838. di->chg_info.conn_chg;
  839. di->chg_info.conn_chg |= AC_CHG;
  840. }
  841. break;
  842. case POWER_SUPPLY_TYPE_USB:
  843. /* USB disconnected */
  844. if (!ret.intval &&
  845. (di->chg_info.conn_chg & USB_CHG)) {
  846. di->chg_info.prev_conn_chg =
  847. di->chg_info.conn_chg;
  848. di->chg_info.conn_chg &= ~USB_CHG;
  849. }
  850. /* USB connected */
  851. else if (ret.intval &&
  852. !(di->chg_info.conn_chg & USB_CHG)) {
  853. di->chg_info.prev_conn_chg =
  854. di->chg_info.conn_chg;
  855. di->chg_info.conn_chg |= USB_CHG;
  856. }
  857. break;
  858. default:
  859. break;
  860. }
  861. break;
  862. case POWER_SUPPLY_PROP_ONLINE:
  863. switch (ext->type) {
  864. case POWER_SUPPLY_TYPE_BATTERY:
  865. break;
  866. case POWER_SUPPLY_TYPE_MAINS:
  867. /* AC offline */
  868. if (!ret.intval &&
  869. (di->chg_info.online_chg & AC_CHG)) {
  870. di->chg_info.prev_online_chg =
  871. di->chg_info.online_chg;
  872. di->chg_info.online_chg &= ~AC_CHG;
  873. }
  874. /* AC online */
  875. else if (ret.intval &&
  876. !(di->chg_info.online_chg & AC_CHG)) {
  877. di->chg_info.prev_online_chg =
  878. di->chg_info.online_chg;
  879. di->chg_info.online_chg |= AC_CHG;
  880. queue_delayed_work(di->chargalg_wq,
  881. &di->chargalg_wd_work, 0);
  882. }
  883. break;
  884. case POWER_SUPPLY_TYPE_USB:
  885. /* USB offline */
  886. if (!ret.intval &&
  887. (di->chg_info.online_chg & USB_CHG)) {
  888. di->chg_info.prev_online_chg =
  889. di->chg_info.online_chg;
  890. di->chg_info.online_chg &= ~USB_CHG;
  891. }
  892. /* USB online */
  893. else if (ret.intval &&
  894. !(di->chg_info.online_chg & USB_CHG)) {
  895. di->chg_info.prev_online_chg =
  896. di->chg_info.online_chg;
  897. di->chg_info.online_chg |= USB_CHG;
  898. queue_delayed_work(di->chargalg_wq,
  899. &di->chargalg_wd_work, 0);
  900. }
  901. break;
  902. default:
  903. break;
  904. }
  905. break;
  906. case POWER_SUPPLY_PROP_HEALTH:
  907. switch (ext->type) {
  908. case POWER_SUPPLY_TYPE_BATTERY:
  909. break;
  910. case POWER_SUPPLY_TYPE_MAINS:
  911. switch (ret.intval) {
  912. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  913. di->events.mainextchnotok = true;
  914. di->events.main_thermal_prot = false;
  915. di->events.main_ovv = false;
  916. di->events.ac_wd_expired = false;
  917. break;
  918. case POWER_SUPPLY_HEALTH_DEAD:
  919. di->events.ac_wd_expired = true;
  920. di->events.mainextchnotok = false;
  921. di->events.main_ovv = false;
  922. di->events.main_thermal_prot = false;
  923. break;
  924. case POWER_SUPPLY_HEALTH_COLD:
  925. case POWER_SUPPLY_HEALTH_OVERHEAT:
  926. di->events.main_thermal_prot = true;
  927. di->events.mainextchnotok = false;
  928. di->events.main_ovv = false;
  929. di->events.ac_wd_expired = false;
  930. break;
  931. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  932. di->events.main_ovv = true;
  933. di->events.mainextchnotok = false;
  934. di->events.main_thermal_prot = false;
  935. di->events.ac_wd_expired = false;
  936. break;
  937. case POWER_SUPPLY_HEALTH_GOOD:
  938. di->events.main_thermal_prot = false;
  939. di->events.mainextchnotok = false;
  940. di->events.main_ovv = false;
  941. di->events.ac_wd_expired = false;
  942. break;
  943. default:
  944. break;
  945. }
  946. break;
  947. case POWER_SUPPLY_TYPE_USB:
  948. switch (ret.intval) {
  949. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  950. di->events.usbchargernotok = true;
  951. di->events.usb_thermal_prot = false;
  952. di->events.vbus_ovv = false;
  953. di->events.usb_wd_expired = false;
  954. break;
  955. case POWER_SUPPLY_HEALTH_DEAD:
  956. di->events.usb_wd_expired = true;
  957. di->events.usbchargernotok = false;
  958. di->events.usb_thermal_prot = false;
  959. di->events.vbus_ovv = false;
  960. break;
  961. case POWER_SUPPLY_HEALTH_COLD:
  962. case POWER_SUPPLY_HEALTH_OVERHEAT:
  963. di->events.usb_thermal_prot = true;
  964. di->events.usbchargernotok = false;
  965. di->events.vbus_ovv = false;
  966. di->events.usb_wd_expired = false;
  967. break;
  968. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  969. di->events.vbus_ovv = true;
  970. di->events.usbchargernotok = false;
  971. di->events.usb_thermal_prot = false;
  972. di->events.usb_wd_expired = false;
  973. break;
  974. case POWER_SUPPLY_HEALTH_GOOD:
  975. di->events.usbchargernotok = false;
  976. di->events.usb_thermal_prot = false;
  977. di->events.vbus_ovv = false;
  978. di->events.usb_wd_expired = false;
  979. break;
  980. default:
  981. break;
  982. }
  983. default:
  984. break;
  985. }
  986. break;
  987. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  988. switch (ext->type) {
  989. case POWER_SUPPLY_TYPE_BATTERY:
  990. di->batt_data.volt = ret.intval / 1000;
  991. break;
  992. case POWER_SUPPLY_TYPE_MAINS:
  993. di->chg_info.ac_volt = ret.intval / 1000;
  994. break;
  995. case POWER_SUPPLY_TYPE_USB:
  996. di->chg_info.usb_volt = ret.intval / 1000;
  997. break;
  998. default:
  999. break;
  1000. }
  1001. break;
  1002. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  1003. switch (ext->type) {
  1004. case POWER_SUPPLY_TYPE_MAINS:
  1005. /* AVG is used to indicate when we are
  1006. * in CV mode */
  1007. if (ret.intval)
  1008. di->events.ac_cv_active = true;
  1009. else
  1010. di->events.ac_cv_active = false;
  1011. break;
  1012. case POWER_SUPPLY_TYPE_USB:
  1013. /* AVG is used to indicate when we are
  1014. * in CV mode */
  1015. if (ret.intval)
  1016. di->events.usb_cv_active = true;
  1017. else
  1018. di->events.usb_cv_active = false;
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. break;
  1024. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1025. switch (ext->type) {
  1026. case POWER_SUPPLY_TYPE_BATTERY:
  1027. if (ret.intval)
  1028. di->events.batt_unknown = false;
  1029. else
  1030. di->events.batt_unknown = true;
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. break;
  1036. case POWER_SUPPLY_PROP_TEMP:
  1037. di->batt_data.temp = ret.intval / 10;
  1038. break;
  1039. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1040. switch (ext->type) {
  1041. case POWER_SUPPLY_TYPE_MAINS:
  1042. di->chg_info.ac_curr =
  1043. ret.intval / 1000;
  1044. break;
  1045. case POWER_SUPPLY_TYPE_USB:
  1046. di->chg_info.usb_curr =
  1047. ret.intval / 1000;
  1048. break;
  1049. case POWER_SUPPLY_TYPE_BATTERY:
  1050. di->batt_data.inst_curr = ret.intval / 1000;
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. break;
  1056. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1057. switch (ext->type) {
  1058. case POWER_SUPPLY_TYPE_BATTERY:
  1059. di->batt_data.avg_curr = ret.intval / 1000;
  1060. break;
  1061. case POWER_SUPPLY_TYPE_USB:
  1062. if (ret.intval)
  1063. di->events.vbus_collapsed = true;
  1064. else
  1065. di->events.vbus_collapsed = false;
  1066. break;
  1067. default:
  1068. break;
  1069. }
  1070. break;
  1071. case POWER_SUPPLY_PROP_CAPACITY:
  1072. if (!capacity_updated)
  1073. di->batt_data.percent = ret.intval;
  1074. break;
  1075. default:
  1076. break;
  1077. }
  1078. }
  1079. return 0;
  1080. }
  1081. /**
  1082. * abx500_chargalg_external_power_changed() - callback for power supply changes
  1083. * @psy: pointer to the structure power_supply
  1084. *
  1085. * This function is the entry point of the pointer external_power_changed
  1086. * of the structure power_supply.
  1087. * This function gets executed when there is a change in any external power
  1088. * supply that this driver needs to be notified of.
  1089. */
  1090. static void abx500_chargalg_external_power_changed(struct power_supply *psy)
  1091. {
  1092. struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
  1093. /*
  1094. * Trigger execution of the algorithm instantly and read
  1095. * all power_supply properties there instead
  1096. */
  1097. queue_work(di->chargalg_wq, &di->chargalg_work);
  1098. }
  1099. /**
  1100. * abx500_chargalg_algorithm() - Main function for the algorithm
  1101. * @di: pointer to the abx500_chargalg structure
  1102. *
  1103. * This is the main control function for the charging algorithm.
  1104. * It is called periodically or when something happens that will
  1105. * trigger a state change
  1106. */
  1107. static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
  1108. {
  1109. int charger_status;
  1110. /* Collect data from all power_supply class devices */
  1111. class_for_each_device(power_supply_class, NULL,
  1112. &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
  1113. abx500_chargalg_end_of_charge(di);
  1114. abx500_chargalg_check_temp(di);
  1115. abx500_chargalg_check_charger_voltage(di);
  1116. charger_status = abx500_chargalg_check_charger_connection(di);
  1117. /*
  1118. * First check if we have a charger connected.
  1119. * Also we don't allow charging of unknown batteries if configured
  1120. * this way
  1121. */
  1122. if (!charger_status ||
  1123. (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
  1124. if (di->charge_state != STATE_HANDHELD) {
  1125. di->events.safety_timer_expired = false;
  1126. abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
  1127. }
  1128. }
  1129. /* If suspended, we should not continue checking the flags */
  1130. else if (di->charge_state == STATE_SUSPENDED_INIT ||
  1131. di->charge_state == STATE_SUSPENDED) {
  1132. /* We don't do anything here, just don,t continue */
  1133. }
  1134. /* Safety timer expiration */
  1135. else if (di->events.safety_timer_expired) {
  1136. if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
  1137. abx500_chargalg_state_to(di,
  1138. STATE_SAFETY_TIMER_EXPIRED_INIT);
  1139. }
  1140. /*
  1141. * Check if any interrupts has occured
  1142. * that will prevent us from charging
  1143. */
  1144. /* Battery removed */
  1145. else if (di->events.batt_rem) {
  1146. if (di->charge_state != STATE_BATT_REMOVED)
  1147. abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
  1148. }
  1149. /* Main or USB charger not ok. */
  1150. else if (di->events.mainextchnotok || di->events.usbchargernotok) {
  1151. /*
  1152. * If vbus_collapsed is set, we have to lower the charger
  1153. * current, which is done in the normal state below
  1154. */
  1155. if (di->charge_state != STATE_CHG_NOT_OK &&
  1156. !di->events.vbus_collapsed)
  1157. abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
  1158. }
  1159. /* VBUS, Main or VBAT OVV. */
  1160. else if (di->events.vbus_ovv ||
  1161. di->events.main_ovv ||
  1162. di->events.batt_ovv ||
  1163. !di->chg_info.usb_chg_ok ||
  1164. !di->chg_info.ac_chg_ok) {
  1165. if (di->charge_state != STATE_OVV_PROTECT)
  1166. abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
  1167. }
  1168. /* USB Thermal, stop charging */
  1169. else if (di->events.main_thermal_prot ||
  1170. di->events.usb_thermal_prot) {
  1171. if (di->charge_state != STATE_HW_TEMP_PROTECT)
  1172. abx500_chargalg_state_to(di,
  1173. STATE_HW_TEMP_PROTECT_INIT);
  1174. }
  1175. /* Battery temp over/under */
  1176. else if (di->events.btemp_underover) {
  1177. if (di->charge_state != STATE_TEMP_UNDEROVER)
  1178. abx500_chargalg_state_to(di,
  1179. STATE_TEMP_UNDEROVER_INIT);
  1180. }
  1181. /* Watchdog expired */
  1182. else if (di->events.ac_wd_expired ||
  1183. di->events.usb_wd_expired) {
  1184. if (di->charge_state != STATE_WD_EXPIRED)
  1185. abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
  1186. }
  1187. /* Battery temp high/low */
  1188. else if (di->events.btemp_lowhigh) {
  1189. if (di->charge_state != STATE_TEMP_LOWHIGH)
  1190. abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
  1191. }
  1192. dev_dbg(di->dev,
  1193. "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
  1194. "State %s Active_chg %d Chg_status %d AC %d USB %d "
  1195. "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
  1196. "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
  1197. di->batt_data.volt,
  1198. di->batt_data.avg_curr,
  1199. di->batt_data.inst_curr,
  1200. di->batt_data.temp,
  1201. di->batt_data.percent,
  1202. di->maintenance_chg,
  1203. states[di->charge_state],
  1204. di->chg_info.charger_type,
  1205. di->charge_status,
  1206. di->chg_info.conn_chg & AC_CHG,
  1207. di->chg_info.conn_chg & USB_CHG,
  1208. di->chg_info.online_chg & AC_CHG,
  1209. di->chg_info.online_chg & USB_CHG,
  1210. di->events.ac_cv_active,
  1211. di->events.usb_cv_active,
  1212. di->chg_info.ac_curr,
  1213. di->chg_info.usb_curr,
  1214. di->chg_info.ac_vset,
  1215. di->chg_info.ac_iset,
  1216. di->chg_info.usb_vset,
  1217. di->chg_info.usb_iset);
  1218. switch (di->charge_state) {
  1219. case STATE_HANDHELD_INIT:
  1220. abx500_chargalg_stop_charging(di);
  1221. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  1222. abx500_chargalg_state_to(di, STATE_HANDHELD);
  1223. /* Intentional fallthrough */
  1224. case STATE_HANDHELD:
  1225. break;
  1226. case STATE_SUSPENDED_INIT:
  1227. if (di->susp_status.ac_suspended)
  1228. abx500_chargalg_ac_en(di, false, 0, 0);
  1229. if (di->susp_status.usb_suspended)
  1230. abx500_chargalg_usb_en(di, false, 0, 0);
  1231. abx500_chargalg_stop_safety_timer(di);
  1232. abx500_chargalg_stop_maintenance_timer(di);
  1233. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1234. di->maintenance_chg = false;
  1235. abx500_chargalg_state_to(di, STATE_SUSPENDED);
  1236. power_supply_changed(&di->chargalg_psy);
  1237. /* Intentional fallthrough */
  1238. case STATE_SUSPENDED:
  1239. /* CHARGING is suspended */
  1240. break;
  1241. case STATE_BATT_REMOVED_INIT:
  1242. abx500_chargalg_stop_charging(di);
  1243. abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
  1244. /* Intentional fallthrough */
  1245. case STATE_BATT_REMOVED:
  1246. if (!di->events.batt_rem)
  1247. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1248. break;
  1249. case STATE_HW_TEMP_PROTECT_INIT:
  1250. abx500_chargalg_stop_charging(di);
  1251. abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
  1252. /* Intentional fallthrough */
  1253. case STATE_HW_TEMP_PROTECT:
  1254. if (!di->events.main_thermal_prot &&
  1255. !di->events.usb_thermal_prot)
  1256. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1257. break;
  1258. case STATE_OVV_PROTECT_INIT:
  1259. abx500_chargalg_stop_charging(di);
  1260. abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
  1261. /* Intentional fallthrough */
  1262. case STATE_OVV_PROTECT:
  1263. if (!di->events.vbus_ovv &&
  1264. !di->events.main_ovv &&
  1265. !di->events.batt_ovv &&
  1266. di->chg_info.usb_chg_ok &&
  1267. di->chg_info.ac_chg_ok)
  1268. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1269. break;
  1270. case STATE_CHG_NOT_OK_INIT:
  1271. abx500_chargalg_stop_charging(di);
  1272. abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
  1273. /* Intentional fallthrough */
  1274. case STATE_CHG_NOT_OK:
  1275. if (!di->events.mainextchnotok &&
  1276. !di->events.usbchargernotok)
  1277. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1278. break;
  1279. case STATE_SAFETY_TIMER_EXPIRED_INIT:
  1280. abx500_chargalg_stop_charging(di);
  1281. abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
  1282. /* Intentional fallthrough */
  1283. case STATE_SAFETY_TIMER_EXPIRED:
  1284. /* We exit this state when charger is removed */
  1285. break;
  1286. case STATE_NORMAL_INIT:
  1287. abx500_chargalg_start_charging(di,
  1288. di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
  1289. di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
  1290. abx500_chargalg_state_to(di, STATE_NORMAL);
  1291. abx500_chargalg_start_safety_timer(di);
  1292. abx500_chargalg_stop_maintenance_timer(di);
  1293. init_maxim_chg_curr(di);
  1294. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1295. di->eoc_cnt = 0;
  1296. di->maintenance_chg = false;
  1297. power_supply_changed(&di->chargalg_psy);
  1298. break;
  1299. case STATE_NORMAL:
  1300. handle_maxim_chg_curr(di);
  1301. if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
  1302. di->maintenance_chg) {
  1303. if (di->bm->no_maintenance)
  1304. abx500_chargalg_state_to(di,
  1305. STATE_WAIT_FOR_RECHARGE_INIT);
  1306. else
  1307. abx500_chargalg_state_to(di,
  1308. STATE_MAINTENANCE_A_INIT);
  1309. }
  1310. break;
  1311. /* This state will be used when the maintenance state is disabled */
  1312. case STATE_WAIT_FOR_RECHARGE_INIT:
  1313. abx500_chargalg_hold_charging(di);
  1314. abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
  1315. /* Intentional fallthrough */
  1316. case STATE_WAIT_FOR_RECHARGE:
  1317. if (di->batt_data.percent <=
  1318. di->bm->bat_type[di->bm->batt_id].
  1319. recharge_cap)
  1320. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1321. break;
  1322. case STATE_MAINTENANCE_A_INIT:
  1323. abx500_chargalg_stop_safety_timer(di);
  1324. abx500_chargalg_start_maintenance_timer(di,
  1325. di->bm->bat_type[
  1326. di->bm->batt_id].maint_a_chg_timer_h);
  1327. abx500_chargalg_start_charging(di,
  1328. di->bm->bat_type[
  1329. di->bm->batt_id].maint_a_vol_lvl,
  1330. di->bm->bat_type[
  1331. di->bm->batt_id].maint_a_cur_lvl);
  1332. abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
  1333. power_supply_changed(&di->chargalg_psy);
  1334. /* Intentional fallthrough*/
  1335. case STATE_MAINTENANCE_A:
  1336. if (di->events.maintenance_timer_expired) {
  1337. abx500_chargalg_stop_maintenance_timer(di);
  1338. abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
  1339. }
  1340. break;
  1341. case STATE_MAINTENANCE_B_INIT:
  1342. abx500_chargalg_start_maintenance_timer(di,
  1343. di->bm->bat_type[
  1344. di->bm->batt_id].maint_b_chg_timer_h);
  1345. abx500_chargalg_start_charging(di,
  1346. di->bm->bat_type[
  1347. di->bm->batt_id].maint_b_vol_lvl,
  1348. di->bm->bat_type[
  1349. di->bm->batt_id].maint_b_cur_lvl);
  1350. abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
  1351. power_supply_changed(&di->chargalg_psy);
  1352. /* Intentional fallthrough*/
  1353. case STATE_MAINTENANCE_B:
  1354. if (di->events.maintenance_timer_expired) {
  1355. abx500_chargalg_stop_maintenance_timer(di);
  1356. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1357. }
  1358. break;
  1359. case STATE_TEMP_LOWHIGH_INIT:
  1360. abx500_chargalg_start_charging(di,
  1361. di->bm->bat_type[
  1362. di->bm->batt_id].low_high_vol_lvl,
  1363. di->bm->bat_type[
  1364. di->bm->batt_id].low_high_cur_lvl);
  1365. abx500_chargalg_stop_maintenance_timer(di);
  1366. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  1367. abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
  1368. power_supply_changed(&di->chargalg_psy);
  1369. /* Intentional fallthrough */
  1370. case STATE_TEMP_LOWHIGH:
  1371. if (!di->events.btemp_lowhigh)
  1372. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1373. break;
  1374. case STATE_WD_EXPIRED_INIT:
  1375. abx500_chargalg_stop_charging(di);
  1376. abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
  1377. /* Intentional fallthrough */
  1378. case STATE_WD_EXPIRED:
  1379. if (!di->events.ac_wd_expired &&
  1380. !di->events.usb_wd_expired)
  1381. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1382. break;
  1383. case STATE_TEMP_UNDEROVER_INIT:
  1384. abx500_chargalg_stop_charging(di);
  1385. abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
  1386. /* Intentional fallthrough */
  1387. case STATE_TEMP_UNDEROVER:
  1388. if (!di->events.btemp_underover)
  1389. abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
  1390. break;
  1391. }
  1392. /* Start charging directly if the new state is a charge state */
  1393. if (di->charge_state == STATE_NORMAL_INIT ||
  1394. di->charge_state == STATE_MAINTENANCE_A_INIT ||
  1395. di->charge_state == STATE_MAINTENANCE_B_INIT)
  1396. queue_work(di->chargalg_wq, &di->chargalg_work);
  1397. }
  1398. /**
  1399. * abx500_chargalg_periodic_work() - Periodic work for the algorithm
  1400. * @work: pointer to the work_struct structure
  1401. *
  1402. * Work queue function for the charging algorithm
  1403. */
  1404. static void abx500_chargalg_periodic_work(struct work_struct *work)
  1405. {
  1406. struct abx500_chargalg *di = container_of(work,
  1407. struct abx500_chargalg, chargalg_periodic_work.work);
  1408. abx500_chargalg_algorithm(di);
  1409. /*
  1410. * If a charger is connected then the battery has to be monitored
  1411. * frequently, else the work can be delayed.
  1412. */
  1413. if (di->chg_info.conn_chg)
  1414. queue_delayed_work(di->chargalg_wq,
  1415. &di->chargalg_periodic_work,
  1416. di->bm->interval_charging * HZ);
  1417. else
  1418. queue_delayed_work(di->chargalg_wq,
  1419. &di->chargalg_periodic_work,
  1420. di->bm->interval_not_charging * HZ);
  1421. }
  1422. /**
  1423. * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
  1424. * @work: pointer to the work_struct structure
  1425. *
  1426. * Work queue function for kicking the charger watchdog
  1427. */
  1428. static void abx500_chargalg_wd_work(struct work_struct *work)
  1429. {
  1430. int ret;
  1431. struct abx500_chargalg *di = container_of(work,
  1432. struct abx500_chargalg, chargalg_wd_work.work);
  1433. dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
  1434. ret = abx500_chargalg_kick_watchdog(di);
  1435. if (ret < 0)
  1436. dev_err(di->dev, "failed to kick watchdog\n");
  1437. queue_delayed_work(di->chargalg_wq,
  1438. &di->chargalg_wd_work, CHG_WD_INTERVAL);
  1439. }
  1440. /**
  1441. * abx500_chargalg_work() - Work to run the charging algorithm instantly
  1442. * @work: pointer to the work_struct structure
  1443. *
  1444. * Work queue function for calling the charging algorithm
  1445. */
  1446. static void abx500_chargalg_work(struct work_struct *work)
  1447. {
  1448. struct abx500_chargalg *di = container_of(work,
  1449. struct abx500_chargalg, chargalg_work);
  1450. abx500_chargalg_algorithm(di);
  1451. }
  1452. /**
  1453. * abx500_chargalg_get_property() - get the chargalg properties
  1454. * @psy: pointer to the power_supply structure
  1455. * @psp: pointer to the power_supply_property structure
  1456. * @val: pointer to the power_supply_propval union
  1457. *
  1458. * This function gets called when an application tries to get the
  1459. * chargalg properties by reading the sysfs files.
  1460. * status: charging/discharging/full/unknown
  1461. * health: health of the battery
  1462. * Returns error code in case of failure else 0 on success
  1463. */
  1464. static int abx500_chargalg_get_property(struct power_supply *psy,
  1465. enum power_supply_property psp,
  1466. union power_supply_propval *val)
  1467. {
  1468. struct abx500_chargalg *di;
  1469. di = to_abx500_chargalg_device_info(psy);
  1470. switch (psp) {
  1471. case POWER_SUPPLY_PROP_STATUS:
  1472. val->intval = di->charge_status;
  1473. break;
  1474. case POWER_SUPPLY_PROP_HEALTH:
  1475. if (di->events.batt_ovv) {
  1476. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1477. } else if (di->events.btemp_underover) {
  1478. if (di->batt_data.temp <= di->bm->temp_under)
  1479. val->intval = POWER_SUPPLY_HEALTH_COLD;
  1480. else
  1481. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  1482. } else {
  1483. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  1484. }
  1485. break;
  1486. default:
  1487. return -EINVAL;
  1488. }
  1489. return 0;
  1490. }
  1491. /* Exposure to the sysfs interface */
  1492. /**
  1493. * abx500_chargalg_sysfs_charger() - sysfs store operations
  1494. * @kobj: pointer to the struct kobject
  1495. * @attr: pointer to the struct attribute
  1496. * @buf: buffer that holds the parameter passed from userspace
  1497. * @length: length of the parameter passed
  1498. *
  1499. * Returns length of the buffer(input taken from user space) on success
  1500. * else error code on failure
  1501. * The operation to be performed on passing the parameters from the user space.
  1502. */
  1503. static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
  1504. struct attribute *attr, const char *buf, size_t length)
  1505. {
  1506. struct abx500_chargalg *di = container_of(kobj,
  1507. struct abx500_chargalg, chargalg_kobject);
  1508. long int param;
  1509. int ac_usb;
  1510. int ret;
  1511. char entry = *attr->name;
  1512. switch (entry) {
  1513. case 'c':
  1514. ret = strict_strtol(buf, 10, &param);
  1515. if (ret < 0)
  1516. return ret;
  1517. ac_usb = param;
  1518. switch (ac_usb) {
  1519. case 0:
  1520. /* Disable charging */
  1521. di->susp_status.ac_suspended = true;
  1522. di->susp_status.usb_suspended = true;
  1523. di->susp_status.suspended_change = true;
  1524. /* Trigger a state change */
  1525. queue_work(di->chargalg_wq,
  1526. &di->chargalg_work);
  1527. break;
  1528. case 1:
  1529. /* Enable AC Charging */
  1530. di->susp_status.ac_suspended = false;
  1531. di->susp_status.suspended_change = true;
  1532. /* Trigger a state change */
  1533. queue_work(di->chargalg_wq,
  1534. &di->chargalg_work);
  1535. break;
  1536. case 2:
  1537. /* Enable USB charging */
  1538. di->susp_status.usb_suspended = false;
  1539. di->susp_status.suspended_change = true;
  1540. /* Trigger a state change */
  1541. queue_work(di->chargalg_wq,
  1542. &di->chargalg_work);
  1543. break;
  1544. default:
  1545. dev_info(di->dev, "Wrong input\n"
  1546. "Enter 0. Disable AC/USB Charging\n"
  1547. "1. Enable AC charging\n"
  1548. "2. Enable USB Charging\n");
  1549. };
  1550. break;
  1551. };
  1552. return strlen(buf);
  1553. }
  1554. static struct attribute abx500_chargalg_en_charger = \
  1555. {
  1556. .name = "chargalg",
  1557. .mode = S_IWUGO,
  1558. };
  1559. static struct attribute *abx500_chargalg_chg[] = {
  1560. &abx500_chargalg_en_charger,
  1561. NULL
  1562. };
  1563. static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
  1564. .store = abx500_chargalg_sysfs_charger,
  1565. };
  1566. static struct kobj_type abx500_chargalg_ktype = {
  1567. .sysfs_ops = &abx500_chargalg_sysfs_ops,
  1568. .default_attrs = abx500_chargalg_chg,
  1569. };
  1570. /**
  1571. * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
  1572. * @di: pointer to the struct abx500_chargalg
  1573. *
  1574. * This function removes the entry in sysfs.
  1575. */
  1576. static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
  1577. {
  1578. kobject_del(&di->chargalg_kobject);
  1579. }
  1580. /**
  1581. * abx500_chargalg_sysfs_init() - init of sysfs entry
  1582. * @di: pointer to the struct abx500_chargalg
  1583. *
  1584. * This function adds an entry in sysfs.
  1585. * Returns error code in case of failure else 0(on success)
  1586. */
  1587. static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
  1588. {
  1589. int ret = 0;
  1590. ret = kobject_init_and_add(&di->chargalg_kobject,
  1591. &abx500_chargalg_ktype,
  1592. NULL, "abx500_chargalg");
  1593. if (ret < 0)
  1594. dev_err(di->dev, "failed to create sysfs entry\n");
  1595. return ret;
  1596. }
  1597. /* Exposure to the sysfs interface <<END>> */
  1598. #if defined(CONFIG_PM)
  1599. static int abx500_chargalg_resume(struct platform_device *pdev)
  1600. {
  1601. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1602. /* Kick charger watchdog if charging (any charger online) */
  1603. if (di->chg_info.online_chg)
  1604. queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
  1605. /*
  1606. * Run the charging algorithm directly to be sure we don't
  1607. * do it too seldom
  1608. */
  1609. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1610. return 0;
  1611. }
  1612. static int abx500_chargalg_suspend(struct platform_device *pdev,
  1613. pm_message_t state)
  1614. {
  1615. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1616. if (di->chg_info.online_chg)
  1617. cancel_delayed_work_sync(&di->chargalg_wd_work);
  1618. cancel_delayed_work_sync(&di->chargalg_periodic_work);
  1619. return 0;
  1620. }
  1621. #else
  1622. #define abx500_chargalg_suspend NULL
  1623. #define abx500_chargalg_resume NULL
  1624. #endif
  1625. static int abx500_chargalg_remove(struct platform_device *pdev)
  1626. {
  1627. struct abx500_chargalg *di = platform_get_drvdata(pdev);
  1628. /* sysfs interface to enable/disbale charging from user space */
  1629. abx500_chargalg_sysfs_exit(di);
  1630. /* Delete the work queue */
  1631. destroy_workqueue(di->chargalg_wq);
  1632. flush_scheduled_work();
  1633. power_supply_unregister(&di->chargalg_psy);
  1634. platform_set_drvdata(pdev, NULL);
  1635. return 0;
  1636. }
  1637. static char *supply_interface[] = {
  1638. "ab8500_fg",
  1639. };
  1640. static int abx500_chargalg_probe(struct platform_device *pdev)
  1641. {
  1642. struct device_node *np = pdev->dev.of_node;
  1643. struct abx500_bm_data *plat = pdev->dev.platform_data;
  1644. struct abx500_chargalg *di;
  1645. int ret = 0;
  1646. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  1647. if (!di) {
  1648. dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
  1649. return -ENOMEM;
  1650. }
  1651. if (!plat) {
  1652. dev_err(&pdev->dev, "no battery management data supplied\n");
  1653. return -EINVAL;
  1654. }
  1655. di->bm = plat;
  1656. if (np) {
  1657. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  1658. if (ret) {
  1659. dev_err(&pdev->dev, "failed to get battery information\n");
  1660. return ret;
  1661. }
  1662. }
  1663. /* get device struct */
  1664. di->dev = &pdev->dev;
  1665. /* chargalg supply */
  1666. di->chargalg_psy.name = "abx500_chargalg";
  1667. di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  1668. di->chargalg_psy.properties = abx500_chargalg_props;
  1669. di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
  1670. di->chargalg_psy.get_property = abx500_chargalg_get_property;
  1671. di->chargalg_psy.supplied_to = supply_interface;
  1672. di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
  1673. di->chargalg_psy.external_power_changed =
  1674. abx500_chargalg_external_power_changed;
  1675. /* Initilialize safety timer */
  1676. init_timer(&di->safety_timer);
  1677. di->safety_timer.function = abx500_chargalg_safety_timer_expired;
  1678. di->safety_timer.data = (unsigned long) di;
  1679. /* Initilialize maintenance timer */
  1680. init_timer(&di->maintenance_timer);
  1681. di->maintenance_timer.function =
  1682. abx500_chargalg_maintenance_timer_expired;
  1683. di->maintenance_timer.data = (unsigned long) di;
  1684. /* Create a work queue for the chargalg */
  1685. di->chargalg_wq =
  1686. create_singlethread_workqueue("abx500_chargalg_wq");
  1687. if (di->chargalg_wq == NULL) {
  1688. dev_err(di->dev, "failed to create work queue\n");
  1689. return -ENOMEM;
  1690. }
  1691. /* Init work for chargalg */
  1692. INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
  1693. abx500_chargalg_periodic_work);
  1694. INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
  1695. abx500_chargalg_wd_work);
  1696. /* Init work for chargalg */
  1697. INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
  1698. /* To detect charger at startup */
  1699. di->chg_info.prev_conn_chg = -1;
  1700. /* Register chargalg power supply class */
  1701. ret = power_supply_register(di->dev, &di->chargalg_psy);
  1702. if (ret) {
  1703. dev_err(di->dev, "failed to register chargalg psy\n");
  1704. goto free_chargalg_wq;
  1705. }
  1706. platform_set_drvdata(pdev, di);
  1707. /* sysfs interface to enable/disable charging from user space */
  1708. ret = abx500_chargalg_sysfs_init(di);
  1709. if (ret) {
  1710. dev_err(di->dev, "failed to create sysfs entry\n");
  1711. goto free_psy;
  1712. }
  1713. /* Run the charging algorithm */
  1714. queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
  1715. dev_info(di->dev, "probe success\n");
  1716. return ret;
  1717. free_psy:
  1718. power_supply_unregister(&di->chargalg_psy);
  1719. free_chargalg_wq:
  1720. destroy_workqueue(di->chargalg_wq);
  1721. return ret;
  1722. }
  1723. static const struct of_device_id ab8500_chargalg_match[] = {
  1724. { .compatible = "stericsson,ab8500-chargalg", },
  1725. { },
  1726. };
  1727. static struct platform_driver abx500_chargalg_driver = {
  1728. .probe = abx500_chargalg_probe,
  1729. .remove = abx500_chargalg_remove,
  1730. .suspend = abx500_chargalg_suspend,
  1731. .resume = abx500_chargalg_resume,
  1732. .driver = {
  1733. .name = "ab8500-chargalg",
  1734. .owner = THIS_MODULE,
  1735. .of_match_table = ab8500_chargalg_match,
  1736. },
  1737. };
  1738. static int __init abx500_chargalg_init(void)
  1739. {
  1740. return platform_driver_register(&abx500_chargalg_driver);
  1741. }
  1742. static void __exit abx500_chargalg_exit(void)
  1743. {
  1744. platform_driver_unregister(&abx500_chargalg_driver);
  1745. }
  1746. module_init(abx500_chargalg_init);
  1747. module_exit(abx500_chargalg_exit);
  1748. MODULE_LICENSE("GPL v2");
  1749. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  1750. MODULE_ALIAS("platform:abx500-chargalg");
  1751. MODULE_DESCRIPTION("abx500 battery charging algorithm");