abx500_chargalg.c 54 KB

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