abx500_chargalg.c 55 KB

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