ab8500_fg.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2012
  3. *
  4. * Main and Back-up battery management driver.
  5. *
  6. * Note: Backup battery management is required in case of Li-Ion battery and not
  7. * for capacitive battery. HREF boards have capacitive battery and hence backup
  8. * battery management is not used and the supported code is available in this
  9. * driver.
  10. *
  11. * License Terms: GNU General Public License v2
  12. * Author:
  13. * Johan Palsson <johan.palsson@stericsson.com>
  14. * Karl Komierowski <karl.komierowski@stericsson.com>
  15. * Arun R Murthy <arun.murthy@stericsson.com>
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/kobject.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/time.h>
  27. #include <linux/of.h>
  28. #include <linux/completion.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/abx500.h>
  31. #include <linux/mfd/abx500/ab8500.h>
  32. #include <linux/mfd/abx500/ab8500-bm.h>
  33. #include <linux/mfd/abx500/ab8500-gpadc.h>
  34. #include <linux/kernel.h>
  35. #define MILLI_TO_MICRO 1000
  36. #define FG_LSB_IN_MA 1627
  37. #define QLSB_NANO_AMP_HOURS_X10 1129
  38. #define INS_CURR_TIMEOUT (3 * HZ)
  39. #define SEC_TO_SAMPLE(S) (S * 4)
  40. #define NBR_AVG_SAMPLES 20
  41. #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
  42. #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
  43. #define BATT_OK_MIN 2360 /* mV */
  44. #define BATT_OK_INCREMENT 50 /* mV */
  45. #define BATT_OK_MAX_NR_INCREMENTS 0xE
  46. /* FG constants */
  47. #define BATT_OVV 0x01
  48. #define interpolate(x, x1, y1, x2, y2) \
  49. ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
  50. #define to_ab8500_fg_device_info(x) container_of((x), \
  51. struct ab8500_fg, fg_psy);
  52. /**
  53. * struct ab8500_fg_interrupts - ab8500 fg interupts
  54. * @name: name of the interrupt
  55. * @isr function pointer to the isr
  56. */
  57. struct ab8500_fg_interrupts {
  58. char *name;
  59. irqreturn_t (*isr)(int irq, void *data);
  60. };
  61. enum ab8500_fg_discharge_state {
  62. AB8500_FG_DISCHARGE_INIT,
  63. AB8500_FG_DISCHARGE_INITMEASURING,
  64. AB8500_FG_DISCHARGE_INIT_RECOVERY,
  65. AB8500_FG_DISCHARGE_RECOVERY,
  66. AB8500_FG_DISCHARGE_READOUT_INIT,
  67. AB8500_FG_DISCHARGE_READOUT,
  68. AB8500_FG_DISCHARGE_WAKEUP,
  69. };
  70. static char *discharge_state[] = {
  71. "DISCHARGE_INIT",
  72. "DISCHARGE_INITMEASURING",
  73. "DISCHARGE_INIT_RECOVERY",
  74. "DISCHARGE_RECOVERY",
  75. "DISCHARGE_READOUT_INIT",
  76. "DISCHARGE_READOUT",
  77. "DISCHARGE_WAKEUP",
  78. };
  79. enum ab8500_fg_charge_state {
  80. AB8500_FG_CHARGE_INIT,
  81. AB8500_FG_CHARGE_READOUT,
  82. };
  83. static char *charge_state[] = {
  84. "CHARGE_INIT",
  85. "CHARGE_READOUT",
  86. };
  87. enum ab8500_fg_calibration_state {
  88. AB8500_FG_CALIB_INIT,
  89. AB8500_FG_CALIB_WAIT,
  90. AB8500_FG_CALIB_END,
  91. };
  92. struct ab8500_fg_avg_cap {
  93. int avg;
  94. int samples[NBR_AVG_SAMPLES];
  95. __kernel_time_t time_stamps[NBR_AVG_SAMPLES];
  96. int pos;
  97. int nbr_samples;
  98. int sum;
  99. };
  100. struct ab8500_fg_cap_scaling {
  101. bool enable;
  102. int cap_to_scale[2];
  103. int disable_cap_level;
  104. int scaled_cap;
  105. };
  106. struct ab8500_fg_battery_capacity {
  107. int max_mah_design;
  108. int max_mah;
  109. int mah;
  110. int permille;
  111. int level;
  112. int prev_mah;
  113. int prev_percent;
  114. int prev_level;
  115. int user_mah;
  116. struct ab8500_fg_cap_scaling cap_scale;
  117. };
  118. struct ab8500_fg_flags {
  119. bool fg_enabled;
  120. bool conv_done;
  121. bool charging;
  122. bool fully_charged;
  123. bool force_full;
  124. bool low_bat_delay;
  125. bool low_bat;
  126. bool bat_ovv;
  127. bool batt_unknown;
  128. bool calibrate;
  129. bool user_cap;
  130. bool batt_id_received;
  131. };
  132. struct inst_curr_result_list {
  133. struct list_head list;
  134. int *result;
  135. };
  136. /**
  137. * struct ab8500_fg - ab8500 FG device information
  138. * @dev: Pointer to the structure device
  139. * @node: a list of AB8500 FGs, hence prepared for reentrance
  140. * @irq holds the CCEOC interrupt number
  141. * @vbat: Battery voltage in mV
  142. * @vbat_nom: Nominal battery voltage in mV
  143. * @inst_curr: Instantenous battery current in mA
  144. * @avg_curr: Average battery current in mA
  145. * @bat_temp battery temperature
  146. * @fg_samples: Number of samples used in the FG accumulation
  147. * @accu_charge: Accumulated charge from the last conversion
  148. * @recovery_cnt: Counter for recovery mode
  149. * @high_curr_cnt: Counter for high current mode
  150. * @init_cnt: Counter for init mode
  151. * @low_bat_cnt Counter for number of consecutive low battery measures
  152. * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
  153. * @recovery_needed: Indicate if recovery is needed
  154. * @high_curr_mode: Indicate if we're in high current mode
  155. * @init_capacity: Indicate if initial capacity measuring should be done
  156. * @turn_off_fg: True if fg was off before current measurement
  157. * @calib_state State during offset calibration
  158. * @discharge_state: Current discharge state
  159. * @charge_state: Current charge state
  160. * @ab8500_fg_started Completion struct used for the instant current start
  161. * @ab8500_fg_complete Completion struct used for the instant current reading
  162. * @flags: Structure for information about events triggered
  163. * @bat_cap: Structure for battery capacity specific parameters
  164. * @avg_cap: Average capacity filter
  165. * @parent: Pointer to the struct ab8500
  166. * @gpadc: Pointer to the struct gpadc
  167. * @bm: Platform specific battery management information
  168. * @fg_psy: Structure that holds the FG specific battery properties
  169. * @fg_wq: Work queue for running the FG algorithm
  170. * @fg_periodic_work: Work to run the FG algorithm periodically
  171. * @fg_low_bat_work: Work to check low bat condition
  172. * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
  173. * @fg_work: Work to run the FG algorithm instantly
  174. * @fg_acc_cur_work: Work to read the FG accumulator
  175. * @fg_check_hw_failure_work: Work for checking HW state
  176. * @cc_lock: Mutex for locking the CC
  177. * @fg_kobject: Structure of type kobject
  178. */
  179. struct ab8500_fg {
  180. struct device *dev;
  181. struct list_head node;
  182. int irq;
  183. int vbat;
  184. int vbat_nom;
  185. int inst_curr;
  186. int avg_curr;
  187. int bat_temp;
  188. int fg_samples;
  189. int accu_charge;
  190. int recovery_cnt;
  191. int high_curr_cnt;
  192. int init_cnt;
  193. int low_bat_cnt;
  194. int nbr_cceoc_irq_cnt;
  195. bool recovery_needed;
  196. bool high_curr_mode;
  197. bool init_capacity;
  198. bool turn_off_fg;
  199. enum ab8500_fg_calibration_state calib_state;
  200. enum ab8500_fg_discharge_state discharge_state;
  201. enum ab8500_fg_charge_state charge_state;
  202. struct completion ab8500_fg_started;
  203. struct completion ab8500_fg_complete;
  204. struct ab8500_fg_flags flags;
  205. struct ab8500_fg_battery_capacity bat_cap;
  206. struct ab8500_fg_avg_cap avg_cap;
  207. struct ab8500 *parent;
  208. struct ab8500_gpadc *gpadc;
  209. struct abx500_bm_data *bm;
  210. struct power_supply fg_psy;
  211. struct workqueue_struct *fg_wq;
  212. struct delayed_work fg_periodic_work;
  213. struct delayed_work fg_low_bat_work;
  214. struct delayed_work fg_reinit_work;
  215. struct work_struct fg_work;
  216. struct work_struct fg_acc_cur_work;
  217. struct delayed_work fg_check_hw_failure_work;
  218. struct mutex cc_lock;
  219. struct kobject fg_kobject;
  220. };
  221. static LIST_HEAD(ab8500_fg_list);
  222. /**
  223. * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
  224. * (i.e. the first fuel gauge in the instance list)
  225. */
  226. struct ab8500_fg *ab8500_fg_get(void)
  227. {
  228. struct ab8500_fg *fg;
  229. if (list_empty(&ab8500_fg_list))
  230. return NULL;
  231. fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
  232. return fg;
  233. }
  234. /* Main battery properties */
  235. static enum power_supply_property ab8500_fg_props[] = {
  236. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  237. POWER_SUPPLY_PROP_CURRENT_NOW,
  238. POWER_SUPPLY_PROP_CURRENT_AVG,
  239. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  240. POWER_SUPPLY_PROP_ENERGY_FULL,
  241. POWER_SUPPLY_PROP_ENERGY_NOW,
  242. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  243. POWER_SUPPLY_PROP_CHARGE_FULL,
  244. POWER_SUPPLY_PROP_CHARGE_NOW,
  245. POWER_SUPPLY_PROP_CAPACITY,
  246. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  247. };
  248. /*
  249. * This array maps the raw hex value to lowbat voltage used by the AB8500
  250. * Values taken from the UM0836
  251. */
  252. static int ab8500_fg_lowbat_voltage_map[] = {
  253. 2300 ,
  254. 2325 ,
  255. 2350 ,
  256. 2375 ,
  257. 2400 ,
  258. 2425 ,
  259. 2450 ,
  260. 2475 ,
  261. 2500 ,
  262. 2525 ,
  263. 2550 ,
  264. 2575 ,
  265. 2600 ,
  266. 2625 ,
  267. 2650 ,
  268. 2675 ,
  269. 2700 ,
  270. 2725 ,
  271. 2750 ,
  272. 2775 ,
  273. 2800 ,
  274. 2825 ,
  275. 2850 ,
  276. 2875 ,
  277. 2900 ,
  278. 2925 ,
  279. 2950 ,
  280. 2975 ,
  281. 3000 ,
  282. 3025 ,
  283. 3050 ,
  284. 3075 ,
  285. 3100 ,
  286. 3125 ,
  287. 3150 ,
  288. 3175 ,
  289. 3200 ,
  290. 3225 ,
  291. 3250 ,
  292. 3275 ,
  293. 3300 ,
  294. 3325 ,
  295. 3350 ,
  296. 3375 ,
  297. 3400 ,
  298. 3425 ,
  299. 3450 ,
  300. 3475 ,
  301. 3500 ,
  302. 3525 ,
  303. 3550 ,
  304. 3575 ,
  305. 3600 ,
  306. 3625 ,
  307. 3650 ,
  308. 3675 ,
  309. 3700 ,
  310. 3725 ,
  311. 3750 ,
  312. 3775 ,
  313. 3800 ,
  314. 3825 ,
  315. 3850 ,
  316. 3850 ,
  317. };
  318. static u8 ab8500_volt_to_regval(int voltage)
  319. {
  320. int i;
  321. if (voltage < ab8500_fg_lowbat_voltage_map[0])
  322. return 0;
  323. for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
  324. if (voltage < ab8500_fg_lowbat_voltage_map[i])
  325. return (u8) i - 1;
  326. }
  327. /* If not captured above, return index of last element */
  328. return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
  329. }
  330. /**
  331. * ab8500_fg_is_low_curr() - Low or high current mode
  332. * @di: pointer to the ab8500_fg structure
  333. * @curr: the current to base or our decision on
  334. *
  335. * Low current mode if the current consumption is below a certain threshold
  336. */
  337. static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
  338. {
  339. /*
  340. * We want to know if we're in low current mode
  341. */
  342. if (curr > -di->bm->fg_params->high_curr_threshold)
  343. return true;
  344. else
  345. return false;
  346. }
  347. /**
  348. * ab8500_fg_add_cap_sample() - Add capacity to average filter
  349. * @di: pointer to the ab8500_fg structure
  350. * @sample: the capacity in mAh to add to the filter
  351. *
  352. * A capacity is added to the filter and a new mean capacity is calculated and
  353. * returned
  354. */
  355. static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
  356. {
  357. struct timespec ts;
  358. struct ab8500_fg_avg_cap *avg = &di->avg_cap;
  359. getnstimeofday(&ts);
  360. do {
  361. avg->sum += sample - avg->samples[avg->pos];
  362. avg->samples[avg->pos] = sample;
  363. avg->time_stamps[avg->pos] = ts.tv_sec;
  364. avg->pos++;
  365. if (avg->pos == NBR_AVG_SAMPLES)
  366. avg->pos = 0;
  367. if (avg->nbr_samples < NBR_AVG_SAMPLES)
  368. avg->nbr_samples++;
  369. /*
  370. * Check the time stamp for each sample. If too old,
  371. * replace with latest sample
  372. */
  373. } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
  374. avg->avg = avg->sum / avg->nbr_samples;
  375. return avg->avg;
  376. }
  377. /**
  378. * ab8500_fg_clear_cap_samples() - Clear average filter
  379. * @di: pointer to the ab8500_fg structure
  380. *
  381. * The capacity filter is is reset to zero.
  382. */
  383. static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
  384. {
  385. int i;
  386. struct ab8500_fg_avg_cap *avg = &di->avg_cap;
  387. avg->pos = 0;
  388. avg->nbr_samples = 0;
  389. avg->sum = 0;
  390. avg->avg = 0;
  391. for (i = 0; i < NBR_AVG_SAMPLES; i++) {
  392. avg->samples[i] = 0;
  393. avg->time_stamps[i] = 0;
  394. }
  395. }
  396. /**
  397. * ab8500_fg_fill_cap_sample() - Fill average filter
  398. * @di: pointer to the ab8500_fg structure
  399. * @sample: the capacity in mAh to fill the filter with
  400. *
  401. * The capacity filter is filled with a capacity in mAh
  402. */
  403. static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
  404. {
  405. int i;
  406. struct timespec ts;
  407. struct ab8500_fg_avg_cap *avg = &di->avg_cap;
  408. getnstimeofday(&ts);
  409. for (i = 0; i < NBR_AVG_SAMPLES; i++) {
  410. avg->samples[i] = sample;
  411. avg->time_stamps[i] = ts.tv_sec;
  412. }
  413. avg->pos = 0;
  414. avg->nbr_samples = NBR_AVG_SAMPLES;
  415. avg->sum = sample * NBR_AVG_SAMPLES;
  416. avg->avg = sample;
  417. }
  418. /**
  419. * ab8500_fg_coulomb_counter() - enable coulomb counter
  420. * @di: pointer to the ab8500_fg structure
  421. * @enable: enable/disable
  422. *
  423. * Enable/Disable coulomb counter.
  424. * On failure returns negative value.
  425. */
  426. static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
  427. {
  428. int ret = 0;
  429. mutex_lock(&di->cc_lock);
  430. if (enable) {
  431. /* To be able to reprogram the number of samples, we have to
  432. * first stop the CC and then enable it again */
  433. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  434. AB8500_RTC_CC_CONF_REG, 0x00);
  435. if (ret)
  436. goto cc_err;
  437. /* Program the samples */
  438. ret = abx500_set_register_interruptible(di->dev,
  439. AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
  440. di->fg_samples);
  441. if (ret)
  442. goto cc_err;
  443. /* Start the CC */
  444. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  445. AB8500_RTC_CC_CONF_REG,
  446. (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
  447. if (ret)
  448. goto cc_err;
  449. di->flags.fg_enabled = true;
  450. } else {
  451. /* Clear any pending read requests */
  452. ret = abx500_mask_and_set_register_interruptible(di->dev,
  453. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  454. (RESET_ACCU | READ_REQ), 0);
  455. if (ret)
  456. goto cc_err;
  457. ret = abx500_set_register_interruptible(di->dev,
  458. AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
  459. if (ret)
  460. goto cc_err;
  461. /* Stop the CC */
  462. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  463. AB8500_RTC_CC_CONF_REG, 0);
  464. if (ret)
  465. goto cc_err;
  466. di->flags.fg_enabled = false;
  467. }
  468. dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
  469. enable, di->fg_samples);
  470. mutex_unlock(&di->cc_lock);
  471. return ret;
  472. cc_err:
  473. dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
  474. mutex_unlock(&di->cc_lock);
  475. return ret;
  476. }
  477. /**
  478. * ab8500_fg_inst_curr_start() - start battery instantaneous current
  479. * @di: pointer to the ab8500_fg structure
  480. *
  481. * Returns 0 or error code
  482. * Note: This is part "one" and has to be called before
  483. * ab8500_fg_inst_curr_finalize()
  484. */
  485. int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
  486. {
  487. u8 reg_val;
  488. int ret;
  489. mutex_lock(&di->cc_lock);
  490. di->nbr_cceoc_irq_cnt = 0;
  491. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  492. AB8500_RTC_CC_CONF_REG, &reg_val);
  493. if (ret < 0)
  494. goto fail;
  495. if (!(reg_val & CC_PWR_UP_ENA)) {
  496. dev_dbg(di->dev, "%s Enable FG\n", __func__);
  497. di->turn_off_fg = true;
  498. /* Program the samples */
  499. ret = abx500_set_register_interruptible(di->dev,
  500. AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
  501. SEC_TO_SAMPLE(10));
  502. if (ret)
  503. goto fail;
  504. /* Start the CC */
  505. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  506. AB8500_RTC_CC_CONF_REG,
  507. (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
  508. if (ret)
  509. goto fail;
  510. } else {
  511. di->turn_off_fg = false;
  512. }
  513. /* Return and WFI */
  514. INIT_COMPLETION(di->ab8500_fg_started);
  515. INIT_COMPLETION(di->ab8500_fg_complete);
  516. enable_irq(di->irq);
  517. /* Note: cc_lock is still locked */
  518. return 0;
  519. fail:
  520. mutex_unlock(&di->cc_lock);
  521. return ret;
  522. }
  523. /**
  524. * ab8500_fg_inst_curr_started() - check if fg conversion has started
  525. * @di: pointer to the ab8500_fg structure
  526. *
  527. * Returns 1 if conversion started, 0 if still waiting
  528. */
  529. int ab8500_fg_inst_curr_started(struct ab8500_fg *di)
  530. {
  531. return completion_done(&di->ab8500_fg_started);
  532. }
  533. /**
  534. * ab8500_fg_inst_curr_done() - check if fg conversion is done
  535. * @di: pointer to the ab8500_fg structure
  536. *
  537. * Returns 1 if conversion done, 0 if still waiting
  538. */
  539. int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
  540. {
  541. return completion_done(&di->ab8500_fg_complete);
  542. }
  543. /**
  544. * ab8500_fg_inst_curr_finalize() - battery instantaneous current
  545. * @di: pointer to the ab8500_fg structure
  546. * @res: battery instantenous current(on success)
  547. *
  548. * Returns 0 or an error code
  549. * Note: This is part "two" and has to be called at earliest 250 ms
  550. * after ab8500_fg_inst_curr_start()
  551. */
  552. int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
  553. {
  554. u8 low, high;
  555. int val;
  556. int ret;
  557. int timeout;
  558. if (!completion_done(&di->ab8500_fg_complete)) {
  559. timeout = wait_for_completion_timeout(
  560. &di->ab8500_fg_complete,
  561. INS_CURR_TIMEOUT);
  562. dev_dbg(di->dev, "Finalize time: %d ms\n",
  563. ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
  564. if (!timeout) {
  565. ret = -ETIME;
  566. disable_irq(di->irq);
  567. di->nbr_cceoc_irq_cnt = 0;
  568. dev_err(di->dev, "completion timed out [%d]\n",
  569. __LINE__);
  570. goto fail;
  571. }
  572. }
  573. disable_irq(di->irq);
  574. di->nbr_cceoc_irq_cnt = 0;
  575. ret = abx500_mask_and_set_register_interruptible(di->dev,
  576. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  577. READ_REQ, READ_REQ);
  578. /* 100uS between read request and read is needed */
  579. usleep_range(100, 100);
  580. /* Read CC Sample conversion value Low and high */
  581. ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  582. AB8500_GASG_CC_SMPL_CNVL_REG, &low);
  583. if (ret < 0)
  584. goto fail;
  585. ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  586. AB8500_GASG_CC_SMPL_CNVH_REG, &high);
  587. if (ret < 0)
  588. goto fail;
  589. /*
  590. * negative value for Discharging
  591. * convert 2's compliment into decimal
  592. */
  593. if (high & 0x10)
  594. val = (low | (high << 8) | 0xFFFFE000);
  595. else
  596. val = (low | (high << 8));
  597. /*
  598. * Convert to unit value in mA
  599. * Full scale input voltage is
  600. * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
  601. * Given a 250ms conversion cycle time the LSB corresponds
  602. * to 112.9 nAh. Convert to current by dividing by the conversion
  603. * time in hours (250ms = 1 / (3600 * 4)h)
  604. * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
  605. */
  606. val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
  607. (1000 * di->bm->fg_res);
  608. if (di->turn_off_fg) {
  609. dev_dbg(di->dev, "%s Disable FG\n", __func__);
  610. /* Clear any pending read requests */
  611. ret = abx500_set_register_interruptible(di->dev,
  612. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
  613. if (ret)
  614. goto fail;
  615. /* Stop the CC */
  616. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  617. AB8500_RTC_CC_CONF_REG, 0);
  618. if (ret)
  619. goto fail;
  620. }
  621. mutex_unlock(&di->cc_lock);
  622. (*res) = val;
  623. return 0;
  624. fail:
  625. mutex_unlock(&di->cc_lock);
  626. return ret;
  627. }
  628. /**
  629. * ab8500_fg_inst_curr_blocking() - battery instantaneous current
  630. * @di: pointer to the ab8500_fg structure
  631. * @res: battery instantenous current(on success)
  632. *
  633. * Returns 0 else error code
  634. */
  635. int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
  636. {
  637. int ret;
  638. int timeout;
  639. int res = 0;
  640. ret = ab8500_fg_inst_curr_start(di);
  641. if (ret) {
  642. dev_err(di->dev, "Failed to initialize fg_inst\n");
  643. return 0;
  644. }
  645. /* Wait for CC to actually start */
  646. if (!completion_done(&di->ab8500_fg_started)) {
  647. timeout = wait_for_completion_timeout(
  648. &di->ab8500_fg_started,
  649. INS_CURR_TIMEOUT);
  650. dev_dbg(di->dev, "Start time: %d ms\n",
  651. ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
  652. if (!timeout) {
  653. ret = -ETIME;
  654. dev_err(di->dev, "completion timed out [%d]\n",
  655. __LINE__);
  656. goto fail;
  657. }
  658. }
  659. ret = ab8500_fg_inst_curr_finalize(di, &res);
  660. if (ret) {
  661. dev_err(di->dev, "Failed to finalize fg_inst\n");
  662. return 0;
  663. }
  664. dev_dbg(di->dev, "%s instant current: %d", __func__, res);
  665. return res;
  666. fail:
  667. disable_irq(di->irq);
  668. mutex_unlock(&di->cc_lock);
  669. return ret;
  670. }
  671. /**
  672. * ab8500_fg_acc_cur_work() - average battery current
  673. * @work: pointer to the work_struct structure
  674. *
  675. * Updated the average battery current obtained from the
  676. * coulomb counter.
  677. */
  678. static void ab8500_fg_acc_cur_work(struct work_struct *work)
  679. {
  680. int val;
  681. int ret;
  682. u8 low, med, high;
  683. struct ab8500_fg *di = container_of(work,
  684. struct ab8500_fg, fg_acc_cur_work);
  685. mutex_lock(&di->cc_lock);
  686. ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  687. AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
  688. if (ret)
  689. goto exit;
  690. ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  691. AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
  692. if (ret < 0)
  693. goto exit;
  694. ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  695. AB8500_GASG_CC_NCOV_ACCU_MED, &med);
  696. if (ret < 0)
  697. goto exit;
  698. ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
  699. AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
  700. if (ret < 0)
  701. goto exit;
  702. /* Check for sign bit in case of negative value, 2's compliment */
  703. if (high & 0x10)
  704. val = (low | (med << 8) | (high << 16) | 0xFFE00000);
  705. else
  706. val = (low | (med << 8) | (high << 16));
  707. /*
  708. * Convert to uAh
  709. * Given a 250ms conversion cycle time the LSB corresponds
  710. * to 112.9 nAh.
  711. * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
  712. */
  713. di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
  714. (100 * di->bm->fg_res);
  715. /*
  716. * Convert to unit value in mA
  717. * by dividing by the conversion
  718. * time in hours (= samples / (3600 * 4)h)
  719. * and multiply with 1000
  720. */
  721. di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
  722. (1000 * di->bm->fg_res * (di->fg_samples / 4));
  723. di->flags.conv_done = true;
  724. mutex_unlock(&di->cc_lock);
  725. queue_work(di->fg_wq, &di->fg_work);
  726. dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
  727. di->bm->fg_res, di->fg_samples, val, di->accu_charge);
  728. return;
  729. exit:
  730. dev_err(di->dev,
  731. "Failed to read or write gas gauge registers\n");
  732. mutex_unlock(&di->cc_lock);
  733. queue_work(di->fg_wq, &di->fg_work);
  734. }
  735. /**
  736. * ab8500_fg_bat_voltage() - get battery voltage
  737. * @di: pointer to the ab8500_fg structure
  738. *
  739. * Returns battery voltage(on success) else error code
  740. */
  741. static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
  742. {
  743. int vbat;
  744. static int prev;
  745. vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
  746. if (vbat < 0) {
  747. dev_err(di->dev,
  748. "%s gpadc conversion failed, using previous value\n",
  749. __func__);
  750. return prev;
  751. }
  752. prev = vbat;
  753. return vbat;
  754. }
  755. /**
  756. * ab8500_fg_volt_to_capacity() - Voltage based capacity
  757. * @di: pointer to the ab8500_fg structure
  758. * @voltage: The voltage to convert to a capacity
  759. *
  760. * Returns battery capacity in per mille based on voltage
  761. */
  762. static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
  763. {
  764. int i, tbl_size;
  765. struct abx500_v_to_cap *tbl;
  766. int cap = 0;
  767. tbl = di->bm->bat_type[di->bm->batt_id].v_to_cap_tbl,
  768. tbl_size = di->bm->bat_type[di->bm->batt_id].n_v_cap_tbl_elements;
  769. for (i = 0; i < tbl_size; ++i) {
  770. if (voltage > tbl[i].voltage)
  771. break;
  772. }
  773. if ((i > 0) && (i < tbl_size)) {
  774. cap = interpolate(voltage,
  775. tbl[i].voltage,
  776. tbl[i].capacity * 10,
  777. tbl[i-1].voltage,
  778. tbl[i-1].capacity * 10);
  779. } else if (i == 0) {
  780. cap = 1000;
  781. } else {
  782. cap = 0;
  783. }
  784. dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
  785. __func__, voltage, cap);
  786. return cap;
  787. }
  788. /**
  789. * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
  790. * @di: pointer to the ab8500_fg structure
  791. *
  792. * Returns battery capacity based on battery voltage that is not compensated
  793. * for the voltage drop due to the load
  794. */
  795. static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
  796. {
  797. di->vbat = ab8500_fg_bat_voltage(di);
  798. return ab8500_fg_volt_to_capacity(di, di->vbat);
  799. }
  800. /**
  801. * ab8500_fg_battery_resistance() - Returns the battery inner resistance
  802. * @di: pointer to the ab8500_fg structure
  803. *
  804. * Returns battery inner resistance added with the fuel gauge resistor value
  805. * to get the total resistance in the whole link from gnd to bat+ node.
  806. */
  807. static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
  808. {
  809. int i, tbl_size;
  810. struct batres_vs_temp *tbl;
  811. int resist = 0;
  812. tbl = di->bm->bat_type[di->bm->batt_id].batres_tbl;
  813. tbl_size = di->bm->bat_type[di->bm->batt_id].n_batres_tbl_elements;
  814. for (i = 0; i < tbl_size; ++i) {
  815. if (di->bat_temp / 10 > tbl[i].temp)
  816. break;
  817. }
  818. if ((i > 0) && (i < tbl_size)) {
  819. resist = interpolate(di->bat_temp / 10,
  820. tbl[i].temp,
  821. tbl[i].resist,
  822. tbl[i-1].temp,
  823. tbl[i-1].resist);
  824. } else if (i == 0) {
  825. resist = tbl[0].resist;
  826. } else {
  827. resist = tbl[tbl_size - 1].resist;
  828. }
  829. dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
  830. " fg resistance %d, total: %d (mOhm)\n",
  831. __func__, di->bat_temp, resist, di->bm->fg_res / 10,
  832. (di->bm->fg_res / 10) + resist);
  833. /* fg_res variable is in 0.1mOhm */
  834. resist += di->bm->fg_res / 10;
  835. return resist;
  836. }
  837. /**
  838. * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
  839. * @di: pointer to the ab8500_fg structure
  840. *
  841. * Returns battery capacity based on battery voltage that is load compensated
  842. * for the voltage drop
  843. */
  844. static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
  845. {
  846. int vbat_comp, res;
  847. int i = 0;
  848. int vbat = 0;
  849. ab8500_fg_inst_curr_start(di);
  850. do {
  851. vbat += ab8500_fg_bat_voltage(di);
  852. i++;
  853. usleep_range(5000, 6000);
  854. } while (!ab8500_fg_inst_curr_done(di));
  855. ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
  856. di->vbat = vbat / i;
  857. res = ab8500_fg_battery_resistance(di);
  858. /* Use Ohms law to get the load compensated voltage */
  859. vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
  860. dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
  861. "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
  862. __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
  863. return ab8500_fg_volt_to_capacity(di, vbat_comp);
  864. }
  865. /**
  866. * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
  867. * @di: pointer to the ab8500_fg structure
  868. * @cap_mah: capacity in mAh
  869. *
  870. * Converts capacity in mAh to capacity in permille
  871. */
  872. static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
  873. {
  874. return (cap_mah * 1000) / di->bat_cap.max_mah_design;
  875. }
  876. /**
  877. * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
  878. * @di: pointer to the ab8500_fg structure
  879. * @cap_pm: capacity in permille
  880. *
  881. * Converts capacity in permille to capacity in mAh
  882. */
  883. static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
  884. {
  885. return cap_pm * di->bat_cap.max_mah_design / 1000;
  886. }
  887. /**
  888. * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
  889. * @di: pointer to the ab8500_fg structure
  890. * @cap_mah: capacity in mAh
  891. *
  892. * Converts capacity in mAh to capacity in uWh
  893. */
  894. static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
  895. {
  896. u64 div_res;
  897. u32 div_rem;
  898. div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
  899. div_rem = do_div(div_res, 1000);
  900. /* Make sure to round upwards if necessary */
  901. if (div_rem >= 1000 / 2)
  902. div_res++;
  903. return (int) div_res;
  904. }
  905. /**
  906. * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
  907. * @di: pointer to the ab8500_fg structure
  908. *
  909. * Return the capacity in mAh based on previous calculated capcity and the FG
  910. * accumulator register value. The filter is filled with this capacity
  911. */
  912. static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
  913. {
  914. dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
  915. __func__,
  916. di->bat_cap.mah,
  917. di->accu_charge);
  918. /* Capacity should not be less than 0 */
  919. if (di->bat_cap.mah + di->accu_charge > 0)
  920. di->bat_cap.mah += di->accu_charge;
  921. else
  922. di->bat_cap.mah = 0;
  923. /*
  924. * We force capacity to 100% once when the algorithm
  925. * reports that it's full.
  926. */
  927. if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
  928. di->flags.force_full) {
  929. di->bat_cap.mah = di->bat_cap.max_mah_design;
  930. }
  931. ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
  932. di->bat_cap.permille =
  933. ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
  934. /* We need to update battery voltage and inst current when charging */
  935. di->vbat = ab8500_fg_bat_voltage(di);
  936. di->inst_curr = ab8500_fg_inst_curr_blocking(di);
  937. return di->bat_cap.mah;
  938. }
  939. /**
  940. * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
  941. * @di: pointer to the ab8500_fg structure
  942. * @comp: if voltage should be load compensated before capacity calc
  943. *
  944. * Return the capacity in mAh based on the battery voltage. The voltage can
  945. * either be load compensated or not. This value is added to the filter and a
  946. * new mean value is calculated and returned.
  947. */
  948. static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
  949. {
  950. int permille, mah;
  951. if (comp)
  952. permille = ab8500_fg_load_comp_volt_to_capacity(di);
  953. else
  954. permille = ab8500_fg_uncomp_volt_to_capacity(di);
  955. mah = ab8500_fg_convert_permille_to_mah(di, permille);
  956. di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
  957. di->bat_cap.permille =
  958. ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
  959. return di->bat_cap.mah;
  960. }
  961. /**
  962. * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
  963. * @di: pointer to the ab8500_fg structure
  964. *
  965. * Return the capacity in mAh based on previous calculated capcity and the FG
  966. * accumulator register value. This value is added to the filter and a
  967. * new mean value is calculated and returned.
  968. */
  969. static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
  970. {
  971. int permille_volt, permille;
  972. dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
  973. __func__,
  974. di->bat_cap.mah,
  975. di->accu_charge);
  976. /* Capacity should not be less than 0 */
  977. if (di->bat_cap.mah + di->accu_charge > 0)
  978. di->bat_cap.mah += di->accu_charge;
  979. else
  980. di->bat_cap.mah = 0;
  981. if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
  982. di->bat_cap.mah = di->bat_cap.max_mah_design;
  983. /*
  984. * Check against voltage based capacity. It can not be lower
  985. * than what the uncompensated voltage says
  986. */
  987. permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
  988. permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
  989. if (permille < permille_volt) {
  990. di->bat_cap.permille = permille_volt;
  991. di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
  992. di->bat_cap.permille);
  993. dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
  994. __func__,
  995. permille,
  996. permille_volt);
  997. ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
  998. } else {
  999. ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
  1000. di->bat_cap.permille =
  1001. ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
  1002. }
  1003. return di->bat_cap.mah;
  1004. }
  1005. /**
  1006. * ab8500_fg_capacity_level() - Get the battery capacity level
  1007. * @di: pointer to the ab8500_fg structure
  1008. *
  1009. * Get the battery capacity level based on the capacity in percent
  1010. */
  1011. static int ab8500_fg_capacity_level(struct ab8500_fg *di)
  1012. {
  1013. int ret, percent;
  1014. percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
  1015. if (percent <= di->bm->cap_levels->critical ||
  1016. di->flags.low_bat)
  1017. ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  1018. else if (percent <= di->bm->cap_levels->low)
  1019. ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  1020. else if (percent <= di->bm->cap_levels->normal)
  1021. ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  1022. else if (percent <= di->bm->cap_levels->high)
  1023. ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
  1024. else
  1025. ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  1026. return ret;
  1027. }
  1028. /**
  1029. * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
  1030. * @di: pointer to the ab8500_fg structure
  1031. *
  1032. * Calculates the capacity to be shown to upper layers. Scales the capacity
  1033. * to have 100% as a reference from the actual capacity upon removal of charger
  1034. * when charging is in maintenance mode.
  1035. */
  1036. static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di)
  1037. {
  1038. struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
  1039. int capacity = di->bat_cap.prev_percent;
  1040. if (!cs->enable)
  1041. return capacity;
  1042. /*
  1043. * As long as we are in fully charge mode scale the capacity
  1044. * to show 100%.
  1045. */
  1046. if (di->flags.fully_charged) {
  1047. cs->cap_to_scale[0] = 100;
  1048. cs->cap_to_scale[1] =
  1049. max(capacity, di->bm->fg_params->maint_thres);
  1050. dev_dbg(di->dev, "Scale cap with %d/%d\n",
  1051. cs->cap_to_scale[0], cs->cap_to_scale[1]);
  1052. }
  1053. /* Calculates the scaled capacity. */
  1054. if ((cs->cap_to_scale[0] != cs->cap_to_scale[1])
  1055. && (cs->cap_to_scale[1] > 0))
  1056. capacity = min(100,
  1057. DIV_ROUND_CLOSEST(di->bat_cap.prev_percent *
  1058. cs->cap_to_scale[0],
  1059. cs->cap_to_scale[1]));
  1060. if (di->flags.charging) {
  1061. if (capacity < cs->disable_cap_level) {
  1062. cs->disable_cap_level = capacity;
  1063. dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n",
  1064. cs->disable_cap_level);
  1065. } else if (!di->flags.fully_charged) {
  1066. if (di->bat_cap.prev_percent >=
  1067. cs->disable_cap_level) {
  1068. dev_dbg(di->dev, "Disabling scaled capacity\n");
  1069. cs->enable = false;
  1070. capacity = di->bat_cap.prev_percent;
  1071. } else {
  1072. dev_dbg(di->dev,
  1073. "Waiting in cap to level %d%%\n",
  1074. cs->disable_cap_level);
  1075. capacity = cs->disable_cap_level;
  1076. }
  1077. }
  1078. }
  1079. return capacity;
  1080. }
  1081. /**
  1082. * ab8500_fg_update_cap_scalers() - Capacity scaling
  1083. * @di: pointer to the ab8500_fg structure
  1084. *
  1085. * To be called when state change from charge<->discharge to update
  1086. * the capacity scalers.
  1087. */
  1088. static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di)
  1089. {
  1090. struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
  1091. if (!cs->enable)
  1092. return;
  1093. if (di->flags.charging) {
  1094. di->bat_cap.cap_scale.disable_cap_level =
  1095. di->bat_cap.cap_scale.scaled_cap;
  1096. dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n",
  1097. di->bat_cap.cap_scale.disable_cap_level);
  1098. } else {
  1099. if (cs->scaled_cap != 100) {
  1100. cs->cap_to_scale[0] = cs->scaled_cap;
  1101. cs->cap_to_scale[1] = di->bat_cap.prev_percent;
  1102. } else {
  1103. cs->cap_to_scale[0] = 100;
  1104. cs->cap_to_scale[1] =
  1105. max(di->bat_cap.prev_percent,
  1106. di->bm->fg_params->maint_thres);
  1107. }
  1108. dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n",
  1109. cs->cap_to_scale[0], cs->cap_to_scale[1]);
  1110. }
  1111. }
  1112. /**
  1113. * ab8500_fg_check_capacity_limits() - Check if capacity has changed
  1114. * @di: pointer to the ab8500_fg structure
  1115. * @init: capacity is allowed to go up in init mode
  1116. *
  1117. * Check if capacity or capacity limit has changed and notify the system
  1118. * about it using the power_supply framework
  1119. */
  1120. static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
  1121. {
  1122. bool changed = false;
  1123. int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
  1124. di->bat_cap.level = ab8500_fg_capacity_level(di);
  1125. if (di->bat_cap.level != di->bat_cap.prev_level) {
  1126. /*
  1127. * We do not allow reported capacity level to go up
  1128. * unless we're charging or if we're in init
  1129. */
  1130. if (!(!di->flags.charging && di->bat_cap.level >
  1131. di->bat_cap.prev_level) || init) {
  1132. dev_dbg(di->dev, "level changed from %d to %d\n",
  1133. di->bat_cap.prev_level,
  1134. di->bat_cap.level);
  1135. di->bat_cap.prev_level = di->bat_cap.level;
  1136. changed = true;
  1137. } else {
  1138. dev_dbg(di->dev, "level not allowed to go up "
  1139. "since no charger is connected: %d to %d\n",
  1140. di->bat_cap.prev_level,
  1141. di->bat_cap.level);
  1142. }
  1143. }
  1144. /*
  1145. * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
  1146. * shutdown
  1147. */
  1148. if (di->flags.low_bat) {
  1149. dev_dbg(di->dev, "Battery low, set capacity to 0\n");
  1150. di->bat_cap.prev_percent = 0;
  1151. di->bat_cap.permille = 0;
  1152. percent = 0;
  1153. di->bat_cap.prev_mah = 0;
  1154. di->bat_cap.mah = 0;
  1155. changed = true;
  1156. } else if (di->flags.fully_charged) {
  1157. /*
  1158. * We report 100% if algorithm reported fully charged
  1159. * and show 100% during maintenance charging (scaling).
  1160. */
  1161. if (di->flags.force_full) {
  1162. di->bat_cap.prev_percent = percent;
  1163. di->bat_cap.prev_mah = di->bat_cap.mah;
  1164. changed = true;
  1165. if (!di->bat_cap.cap_scale.enable &&
  1166. di->bm->capacity_scaling) {
  1167. di->bat_cap.cap_scale.enable = true;
  1168. di->bat_cap.cap_scale.cap_to_scale[0] = 100;
  1169. di->bat_cap.cap_scale.cap_to_scale[1] =
  1170. di->bat_cap.prev_percent;
  1171. di->bat_cap.cap_scale.disable_cap_level = 100;
  1172. }
  1173. } else if (di->bat_cap.prev_percent != percent) {
  1174. dev_dbg(di->dev,
  1175. "battery reported full "
  1176. "but capacity dropping: %d\n",
  1177. percent);
  1178. di->bat_cap.prev_percent = percent;
  1179. di->bat_cap.prev_mah = di->bat_cap.mah;
  1180. changed = true;
  1181. }
  1182. } else if (di->bat_cap.prev_percent != percent) {
  1183. if (percent == 0) {
  1184. /*
  1185. * We will not report 0% unless we've got
  1186. * the LOW_BAT IRQ, no matter what the FG
  1187. * algorithm says.
  1188. */
  1189. di->bat_cap.prev_percent = 1;
  1190. di->bat_cap.permille = 1;
  1191. di->bat_cap.prev_mah = 1;
  1192. di->bat_cap.mah = 1;
  1193. percent = 1;
  1194. changed = true;
  1195. } else if (!(!di->flags.charging &&
  1196. percent > di->bat_cap.prev_percent) || init) {
  1197. /*
  1198. * We do not allow reported capacity to go up
  1199. * unless we're charging or if we're in init
  1200. */
  1201. dev_dbg(di->dev,
  1202. "capacity changed from %d to %d (%d)\n",
  1203. di->bat_cap.prev_percent,
  1204. percent,
  1205. di->bat_cap.permille);
  1206. di->bat_cap.prev_percent = percent;
  1207. di->bat_cap.prev_mah = di->bat_cap.mah;
  1208. changed = true;
  1209. } else {
  1210. dev_dbg(di->dev, "capacity not allowed to go up since "
  1211. "no charger is connected: %d to %d (%d)\n",
  1212. di->bat_cap.prev_percent,
  1213. percent,
  1214. di->bat_cap.permille);
  1215. }
  1216. }
  1217. if (changed) {
  1218. if (di->bm->capacity_scaling) {
  1219. di->bat_cap.cap_scale.scaled_cap =
  1220. ab8500_fg_calculate_scaled_capacity(di);
  1221. dev_info(di->dev, "capacity=%d (%d)\n",
  1222. di->bat_cap.prev_percent,
  1223. di->bat_cap.cap_scale.scaled_cap);
  1224. }
  1225. power_supply_changed(&di->fg_psy);
  1226. if (di->flags.fully_charged && di->flags.force_full) {
  1227. dev_dbg(di->dev, "Battery full, notifying.\n");
  1228. di->flags.force_full = false;
  1229. sysfs_notify(&di->fg_kobject, NULL, "charge_full");
  1230. }
  1231. sysfs_notify(&di->fg_kobject, NULL, "charge_now");
  1232. }
  1233. }
  1234. static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
  1235. enum ab8500_fg_charge_state new_state)
  1236. {
  1237. dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
  1238. di->charge_state,
  1239. charge_state[di->charge_state],
  1240. new_state,
  1241. charge_state[new_state]);
  1242. di->charge_state = new_state;
  1243. }
  1244. static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
  1245. enum ab8500_fg_discharge_state new_state)
  1246. {
  1247. dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
  1248. di->discharge_state,
  1249. discharge_state[di->discharge_state],
  1250. new_state,
  1251. discharge_state[new_state]);
  1252. di->discharge_state = new_state;
  1253. }
  1254. /**
  1255. * ab8500_fg_algorithm_charging() - FG algorithm for when charging
  1256. * @di: pointer to the ab8500_fg structure
  1257. *
  1258. * Battery capacity calculation state machine for when we're charging
  1259. */
  1260. static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
  1261. {
  1262. /*
  1263. * If we change to discharge mode
  1264. * we should start with recovery
  1265. */
  1266. if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
  1267. ab8500_fg_discharge_state_to(di,
  1268. AB8500_FG_DISCHARGE_INIT_RECOVERY);
  1269. switch (di->charge_state) {
  1270. case AB8500_FG_CHARGE_INIT:
  1271. di->fg_samples = SEC_TO_SAMPLE(
  1272. di->bm->fg_params->accu_charging);
  1273. ab8500_fg_coulomb_counter(di, true);
  1274. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
  1275. break;
  1276. case AB8500_FG_CHARGE_READOUT:
  1277. /*
  1278. * Read the FG and calculate the new capacity
  1279. */
  1280. mutex_lock(&di->cc_lock);
  1281. if (!di->flags.conv_done && !di->flags.force_full) {
  1282. /* Wasn't the CC IRQ that got us here */
  1283. mutex_unlock(&di->cc_lock);
  1284. dev_dbg(di->dev, "%s CC conv not done\n",
  1285. __func__);
  1286. break;
  1287. }
  1288. di->flags.conv_done = false;
  1289. mutex_unlock(&di->cc_lock);
  1290. ab8500_fg_calc_cap_charging(di);
  1291. break;
  1292. default:
  1293. break;
  1294. }
  1295. /* Check capacity limits */
  1296. ab8500_fg_check_capacity_limits(di, false);
  1297. }
  1298. static void force_capacity(struct ab8500_fg *di)
  1299. {
  1300. int cap;
  1301. ab8500_fg_clear_cap_samples(di);
  1302. cap = di->bat_cap.user_mah;
  1303. if (cap > di->bat_cap.max_mah_design) {
  1304. dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
  1305. " %d\n", cap, di->bat_cap.max_mah_design);
  1306. cap = di->bat_cap.max_mah_design;
  1307. }
  1308. ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
  1309. di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
  1310. di->bat_cap.mah = cap;
  1311. ab8500_fg_check_capacity_limits(di, true);
  1312. }
  1313. static bool check_sysfs_capacity(struct ab8500_fg *di)
  1314. {
  1315. int cap, lower, upper;
  1316. int cap_permille;
  1317. cap = di->bat_cap.user_mah;
  1318. cap_permille = ab8500_fg_convert_mah_to_permille(di,
  1319. di->bat_cap.user_mah);
  1320. lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
  1321. upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
  1322. if (lower < 0)
  1323. lower = 0;
  1324. /* 1000 is permille, -> 100 percent */
  1325. if (upper > 1000)
  1326. upper = 1000;
  1327. dev_dbg(di->dev, "Capacity limits:"
  1328. " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
  1329. lower, cap_permille, upper, cap, di->bat_cap.mah);
  1330. /* If within limits, use the saved capacity and exit estimation...*/
  1331. if (cap_permille > lower && cap_permille < upper) {
  1332. dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
  1333. force_capacity(di);
  1334. return true;
  1335. }
  1336. dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
  1337. return false;
  1338. }
  1339. /**
  1340. * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
  1341. * @di: pointer to the ab8500_fg structure
  1342. *
  1343. * Battery capacity calculation state machine for when we're discharging
  1344. */
  1345. static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
  1346. {
  1347. int sleep_time;
  1348. /* If we change to charge mode we should start with init */
  1349. if (di->charge_state != AB8500_FG_CHARGE_INIT)
  1350. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  1351. switch (di->discharge_state) {
  1352. case AB8500_FG_DISCHARGE_INIT:
  1353. /* We use the FG IRQ to work on */
  1354. di->init_cnt = 0;
  1355. di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
  1356. ab8500_fg_coulomb_counter(di, true);
  1357. ab8500_fg_discharge_state_to(di,
  1358. AB8500_FG_DISCHARGE_INITMEASURING);
  1359. /* Intentional fallthrough */
  1360. case AB8500_FG_DISCHARGE_INITMEASURING:
  1361. /*
  1362. * Discard a number of samples during startup.
  1363. * After that, use compensated voltage for a few
  1364. * samples to get an initial capacity.
  1365. * Then go to READOUT
  1366. */
  1367. sleep_time = di->bm->fg_params->init_timer;
  1368. /* Discard the first [x] seconds */
  1369. if (di->init_cnt > di->bm->fg_params->init_discard_time) {
  1370. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1371. ab8500_fg_check_capacity_limits(di, true);
  1372. }
  1373. di->init_cnt += sleep_time;
  1374. if (di->init_cnt > di->bm->fg_params->init_total_time)
  1375. ab8500_fg_discharge_state_to(di,
  1376. AB8500_FG_DISCHARGE_READOUT_INIT);
  1377. break;
  1378. case AB8500_FG_DISCHARGE_INIT_RECOVERY:
  1379. di->recovery_cnt = 0;
  1380. di->recovery_needed = true;
  1381. ab8500_fg_discharge_state_to(di,
  1382. AB8500_FG_DISCHARGE_RECOVERY);
  1383. /* Intentional fallthrough */
  1384. case AB8500_FG_DISCHARGE_RECOVERY:
  1385. sleep_time = di->bm->fg_params->recovery_sleep_timer;
  1386. /*
  1387. * We should check the power consumption
  1388. * If low, go to READOUT (after x min) or
  1389. * RECOVERY_SLEEP if time left.
  1390. * If high, go to READOUT
  1391. */
  1392. di->inst_curr = ab8500_fg_inst_curr_blocking(di);
  1393. if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
  1394. if (di->recovery_cnt >
  1395. di->bm->fg_params->recovery_total_time) {
  1396. di->fg_samples = SEC_TO_SAMPLE(
  1397. di->bm->fg_params->accu_high_curr);
  1398. ab8500_fg_coulomb_counter(di, true);
  1399. ab8500_fg_discharge_state_to(di,
  1400. AB8500_FG_DISCHARGE_READOUT);
  1401. di->recovery_needed = false;
  1402. } else {
  1403. queue_delayed_work(di->fg_wq,
  1404. &di->fg_periodic_work,
  1405. sleep_time * HZ);
  1406. }
  1407. di->recovery_cnt += sleep_time;
  1408. } else {
  1409. di->fg_samples = SEC_TO_SAMPLE(
  1410. di->bm->fg_params->accu_high_curr);
  1411. ab8500_fg_coulomb_counter(di, true);
  1412. ab8500_fg_discharge_state_to(di,
  1413. AB8500_FG_DISCHARGE_READOUT);
  1414. }
  1415. break;
  1416. case AB8500_FG_DISCHARGE_READOUT_INIT:
  1417. di->fg_samples = SEC_TO_SAMPLE(
  1418. di->bm->fg_params->accu_high_curr);
  1419. ab8500_fg_coulomb_counter(di, true);
  1420. ab8500_fg_discharge_state_to(di,
  1421. AB8500_FG_DISCHARGE_READOUT);
  1422. break;
  1423. case AB8500_FG_DISCHARGE_READOUT:
  1424. di->inst_curr = ab8500_fg_inst_curr_blocking(di);
  1425. if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
  1426. /* Detect mode change */
  1427. if (di->high_curr_mode) {
  1428. di->high_curr_mode = false;
  1429. di->high_curr_cnt = 0;
  1430. }
  1431. if (di->recovery_needed) {
  1432. ab8500_fg_discharge_state_to(di,
  1433. AB8500_FG_DISCHARGE_INIT_RECOVERY);
  1434. queue_delayed_work(di->fg_wq,
  1435. &di->fg_periodic_work, 0);
  1436. break;
  1437. }
  1438. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1439. } else {
  1440. mutex_lock(&di->cc_lock);
  1441. if (!di->flags.conv_done) {
  1442. /* Wasn't the CC IRQ that got us here */
  1443. mutex_unlock(&di->cc_lock);
  1444. dev_dbg(di->dev, "%s CC conv not done\n",
  1445. __func__);
  1446. break;
  1447. }
  1448. di->flags.conv_done = false;
  1449. mutex_unlock(&di->cc_lock);
  1450. /* Detect mode change */
  1451. if (!di->high_curr_mode) {
  1452. di->high_curr_mode = true;
  1453. di->high_curr_cnt = 0;
  1454. }
  1455. di->high_curr_cnt +=
  1456. di->bm->fg_params->accu_high_curr;
  1457. if (di->high_curr_cnt >
  1458. di->bm->fg_params->high_curr_time)
  1459. di->recovery_needed = true;
  1460. ab8500_fg_calc_cap_discharge_fg(di);
  1461. }
  1462. ab8500_fg_check_capacity_limits(di, false);
  1463. break;
  1464. case AB8500_FG_DISCHARGE_WAKEUP:
  1465. ab8500_fg_coulomb_counter(di, true);
  1466. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1467. di->fg_samples = SEC_TO_SAMPLE(
  1468. di->bm->fg_params->accu_high_curr);
  1469. ab8500_fg_coulomb_counter(di, true);
  1470. ab8500_fg_discharge_state_to(di,
  1471. AB8500_FG_DISCHARGE_READOUT);
  1472. ab8500_fg_check_capacity_limits(di, false);
  1473. break;
  1474. default:
  1475. break;
  1476. }
  1477. }
  1478. /**
  1479. * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
  1480. * @di: pointer to the ab8500_fg structure
  1481. *
  1482. */
  1483. static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
  1484. {
  1485. int ret;
  1486. switch (di->calib_state) {
  1487. case AB8500_FG_CALIB_INIT:
  1488. dev_dbg(di->dev, "Calibration ongoing...\n");
  1489. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1490. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1491. CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
  1492. if (ret < 0)
  1493. goto err;
  1494. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1495. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1496. CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
  1497. if (ret < 0)
  1498. goto err;
  1499. di->calib_state = AB8500_FG_CALIB_WAIT;
  1500. break;
  1501. case AB8500_FG_CALIB_END:
  1502. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1503. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1504. CC_MUXOFFSET, CC_MUXOFFSET);
  1505. if (ret < 0)
  1506. goto err;
  1507. di->flags.calibrate = false;
  1508. dev_dbg(di->dev, "Calibration done...\n");
  1509. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1510. break;
  1511. case AB8500_FG_CALIB_WAIT:
  1512. dev_dbg(di->dev, "Calibration WFI\n");
  1513. default:
  1514. break;
  1515. }
  1516. return;
  1517. err:
  1518. /* Something went wrong, don't calibrate then */
  1519. dev_err(di->dev, "failed to calibrate the CC\n");
  1520. di->flags.calibrate = false;
  1521. di->calib_state = AB8500_FG_CALIB_INIT;
  1522. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1523. }
  1524. /**
  1525. * ab8500_fg_algorithm() - Entry point for the FG algorithm
  1526. * @di: pointer to the ab8500_fg structure
  1527. *
  1528. * Entry point for the battery capacity calculation state machine
  1529. */
  1530. static void ab8500_fg_algorithm(struct ab8500_fg *di)
  1531. {
  1532. if (di->flags.calibrate)
  1533. ab8500_fg_algorithm_calibrate(di);
  1534. else {
  1535. if (di->flags.charging)
  1536. ab8500_fg_algorithm_charging(di);
  1537. else
  1538. ab8500_fg_algorithm_discharging(di);
  1539. }
  1540. dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d "
  1541. "%d %d %d %d %d %d %d\n",
  1542. di->bat_cap.max_mah_design,
  1543. di->bat_cap.mah,
  1544. di->bat_cap.permille,
  1545. di->bat_cap.level,
  1546. di->bat_cap.prev_mah,
  1547. di->bat_cap.prev_percent,
  1548. di->bat_cap.prev_level,
  1549. di->vbat,
  1550. di->inst_curr,
  1551. di->avg_curr,
  1552. di->accu_charge,
  1553. di->flags.charging,
  1554. di->charge_state,
  1555. di->discharge_state,
  1556. di->high_curr_mode,
  1557. di->recovery_needed);
  1558. }
  1559. /**
  1560. * ab8500_fg_periodic_work() - Run the FG state machine periodically
  1561. * @work: pointer to the work_struct structure
  1562. *
  1563. * Work queue function for periodic work
  1564. */
  1565. static void ab8500_fg_periodic_work(struct work_struct *work)
  1566. {
  1567. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1568. fg_periodic_work.work);
  1569. if (di->init_capacity) {
  1570. /* Get an initial capacity calculation */
  1571. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1572. ab8500_fg_check_capacity_limits(di, true);
  1573. di->init_capacity = false;
  1574. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1575. } else if (di->flags.user_cap) {
  1576. if (check_sysfs_capacity(di)) {
  1577. ab8500_fg_check_capacity_limits(di, true);
  1578. if (di->flags.charging)
  1579. ab8500_fg_charge_state_to(di,
  1580. AB8500_FG_CHARGE_INIT);
  1581. else
  1582. ab8500_fg_discharge_state_to(di,
  1583. AB8500_FG_DISCHARGE_READOUT_INIT);
  1584. }
  1585. di->flags.user_cap = false;
  1586. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1587. } else
  1588. ab8500_fg_algorithm(di);
  1589. }
  1590. /**
  1591. * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
  1592. * @work: pointer to the work_struct structure
  1593. *
  1594. * Work queue function for checking the OVV_BAT condition
  1595. */
  1596. static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
  1597. {
  1598. int ret;
  1599. u8 reg_value;
  1600. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1601. fg_check_hw_failure_work.work);
  1602. /*
  1603. * If we have had a battery over-voltage situation,
  1604. * check ovv-bit to see if it should be reset.
  1605. */
  1606. ret = abx500_get_register_interruptible(di->dev,
  1607. AB8500_CHARGER, AB8500_CH_STAT_REG,
  1608. &reg_value);
  1609. if (ret < 0) {
  1610. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1611. return;
  1612. }
  1613. if ((reg_value & BATT_OVV) == BATT_OVV) {
  1614. if (!di->flags.bat_ovv) {
  1615. dev_dbg(di->dev, "Battery OVV\n");
  1616. di->flags.bat_ovv = true;
  1617. power_supply_changed(&di->fg_psy);
  1618. }
  1619. /* Not yet recovered from ovv, reschedule this test */
  1620. queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
  1621. HZ);
  1622. } else {
  1623. dev_dbg(di->dev, "Battery recovered from OVV\n");
  1624. di->flags.bat_ovv = false;
  1625. power_supply_changed(&di->fg_psy);
  1626. }
  1627. }
  1628. /**
  1629. * ab8500_fg_low_bat_work() - Check LOW_BAT condition
  1630. * @work: pointer to the work_struct structure
  1631. *
  1632. * Work queue function for checking the LOW_BAT condition
  1633. */
  1634. static void ab8500_fg_low_bat_work(struct work_struct *work)
  1635. {
  1636. int vbat;
  1637. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1638. fg_low_bat_work.work);
  1639. vbat = ab8500_fg_bat_voltage(di);
  1640. /* Check if LOW_BAT still fulfilled */
  1641. if (vbat < di->bm->fg_params->lowbat_threshold) {
  1642. /* Is it time to shut down? */
  1643. if (di->low_bat_cnt < 1) {
  1644. di->flags.low_bat = true;
  1645. dev_warn(di->dev, "Shut down pending...\n");
  1646. } else {
  1647. /*
  1648. * Else we need to re-schedule this check to be able to detect
  1649. * if the voltage increases again during charging or
  1650. * due to decreasing load.
  1651. */
  1652. di->low_bat_cnt--;
  1653. dev_warn(di->dev, "Battery voltage still LOW\n");
  1654. queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
  1655. round_jiffies(LOW_BAT_CHECK_INTERVAL));
  1656. }
  1657. } else {
  1658. di->flags.low_bat_delay = false;
  1659. di->low_bat_cnt = 10;
  1660. dev_warn(di->dev, "Battery voltage OK again\n");
  1661. }
  1662. /* This is needed to dispatch LOW_BAT */
  1663. ab8500_fg_check_capacity_limits(di, false);
  1664. }
  1665. /**
  1666. * ab8500_fg_battok_calc - calculate the bit pattern corresponding
  1667. * to the target voltage.
  1668. * @di: pointer to the ab8500_fg structure
  1669. * @target target voltage
  1670. *
  1671. * Returns bit pattern closest to the target voltage
  1672. * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
  1673. */
  1674. static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
  1675. {
  1676. if (target > BATT_OK_MIN +
  1677. (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
  1678. return BATT_OK_MAX_NR_INCREMENTS;
  1679. if (target < BATT_OK_MIN)
  1680. return 0;
  1681. return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
  1682. }
  1683. /**
  1684. * ab8500_fg_battok_init_hw_register - init battok levels
  1685. * @di: pointer to the ab8500_fg structure
  1686. *
  1687. */
  1688. static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
  1689. {
  1690. int selected;
  1691. int sel0;
  1692. int sel1;
  1693. int cbp_sel0;
  1694. int cbp_sel1;
  1695. int ret;
  1696. int new_val;
  1697. sel0 = di->bm->fg_params->battok_falling_th_sel0;
  1698. sel1 = di->bm->fg_params->battok_raising_th_sel1;
  1699. cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
  1700. cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
  1701. selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
  1702. if (selected != sel0)
  1703. dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
  1704. sel0, selected, cbp_sel0);
  1705. selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
  1706. if (selected != sel1)
  1707. dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
  1708. sel1, selected, cbp_sel1);
  1709. new_val = cbp_sel0 | (cbp_sel1 << 4);
  1710. dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
  1711. ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
  1712. AB8500_BATT_OK_REG, new_val);
  1713. return ret;
  1714. }
  1715. /**
  1716. * ab8500_fg_instant_work() - Run the FG state machine instantly
  1717. * @work: pointer to the work_struct structure
  1718. *
  1719. * Work queue function for instant work
  1720. */
  1721. static void ab8500_fg_instant_work(struct work_struct *work)
  1722. {
  1723. struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
  1724. ab8500_fg_algorithm(di);
  1725. }
  1726. /**
  1727. * ab8500_fg_cc_data_end_handler() - isr to get battery avg current.
  1728. * @irq: interrupt number
  1729. * @_di: pointer to the ab8500_fg structure
  1730. *
  1731. * Returns IRQ status(IRQ_HANDLED)
  1732. */
  1733. static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
  1734. {
  1735. struct ab8500_fg *di = _di;
  1736. if (!di->nbr_cceoc_irq_cnt) {
  1737. di->nbr_cceoc_irq_cnt++;
  1738. complete(&di->ab8500_fg_started);
  1739. } else {
  1740. di->nbr_cceoc_irq_cnt = 0;
  1741. complete(&di->ab8500_fg_complete);
  1742. }
  1743. return IRQ_HANDLED;
  1744. }
  1745. /**
  1746. * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
  1747. * @irq: interrupt number
  1748. * @_di: pointer to the ab8500_fg structure
  1749. *
  1750. * Returns IRQ status(IRQ_HANDLED)
  1751. */
  1752. static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
  1753. {
  1754. struct ab8500_fg *di = _di;
  1755. di->calib_state = AB8500_FG_CALIB_END;
  1756. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1757. return IRQ_HANDLED;
  1758. }
  1759. /**
  1760. * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
  1761. * @irq: interrupt number
  1762. * @_di: pointer to the ab8500_fg structure
  1763. *
  1764. * Returns IRQ status(IRQ_HANDLED)
  1765. */
  1766. static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
  1767. {
  1768. struct ab8500_fg *di = _di;
  1769. queue_work(di->fg_wq, &di->fg_acc_cur_work);
  1770. return IRQ_HANDLED;
  1771. }
  1772. /**
  1773. * ab8500_fg_batt_ovv_handler() - Battery OVV occured
  1774. * @irq: interrupt number
  1775. * @_di: pointer to the ab8500_fg structure
  1776. *
  1777. * Returns IRQ status(IRQ_HANDLED)
  1778. */
  1779. static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
  1780. {
  1781. struct ab8500_fg *di = _di;
  1782. dev_dbg(di->dev, "Battery OVV\n");
  1783. /* Schedule a new HW failure check */
  1784. queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
  1785. return IRQ_HANDLED;
  1786. }
  1787. /**
  1788. * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
  1789. * @irq: interrupt number
  1790. * @_di: pointer to the ab8500_fg structure
  1791. *
  1792. * Returns IRQ status(IRQ_HANDLED)
  1793. */
  1794. static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
  1795. {
  1796. struct ab8500_fg *di = _di;
  1797. /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
  1798. if (!di->flags.low_bat_delay) {
  1799. dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
  1800. di->flags.low_bat_delay = true;
  1801. /*
  1802. * Start a timer to check LOW_BAT again after some time
  1803. * This is done to avoid shutdown on single voltage dips
  1804. */
  1805. queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
  1806. round_jiffies(LOW_BAT_CHECK_INTERVAL));
  1807. }
  1808. return IRQ_HANDLED;
  1809. }
  1810. /**
  1811. * ab8500_fg_get_property() - get the fg properties
  1812. * @psy: pointer to the power_supply structure
  1813. * @psp: pointer to the power_supply_property structure
  1814. * @val: pointer to the power_supply_propval union
  1815. *
  1816. * This function gets called when an application tries to get the
  1817. * fg properties by reading the sysfs files.
  1818. * voltage_now: battery voltage
  1819. * current_now: battery instant current
  1820. * current_avg: battery average current
  1821. * charge_full_design: capacity where battery is considered full
  1822. * charge_now: battery capacity in nAh
  1823. * capacity: capacity in percent
  1824. * capacity_level: capacity level
  1825. *
  1826. * Returns error code in case of failure else 0 on success
  1827. */
  1828. static int ab8500_fg_get_property(struct power_supply *psy,
  1829. enum power_supply_property psp,
  1830. union power_supply_propval *val)
  1831. {
  1832. struct ab8500_fg *di;
  1833. di = to_ab8500_fg_device_info(psy);
  1834. /*
  1835. * If battery is identified as unknown and charging of unknown
  1836. * batteries is disabled, we always report 100% capacity and
  1837. * capacity level UNKNOWN, since we can't calculate
  1838. * remaining capacity
  1839. */
  1840. switch (psp) {
  1841. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1842. if (di->flags.bat_ovv)
  1843. val->intval = BATT_OVV_VALUE * 1000;
  1844. else
  1845. val->intval = di->vbat * 1000;
  1846. break;
  1847. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1848. val->intval = di->inst_curr * 1000;
  1849. break;
  1850. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1851. val->intval = di->avg_curr * 1000;
  1852. break;
  1853. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  1854. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1855. di->bat_cap.max_mah_design);
  1856. break;
  1857. case POWER_SUPPLY_PROP_ENERGY_FULL:
  1858. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1859. di->bat_cap.max_mah);
  1860. break;
  1861. case POWER_SUPPLY_PROP_ENERGY_NOW:
  1862. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1863. di->flags.batt_id_received)
  1864. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1865. di->bat_cap.max_mah);
  1866. else
  1867. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1868. di->bat_cap.prev_mah);
  1869. break;
  1870. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1871. val->intval = di->bat_cap.max_mah_design;
  1872. break;
  1873. case POWER_SUPPLY_PROP_CHARGE_FULL:
  1874. val->intval = di->bat_cap.max_mah;
  1875. break;
  1876. case POWER_SUPPLY_PROP_CHARGE_NOW:
  1877. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1878. di->flags.batt_id_received)
  1879. val->intval = di->bat_cap.max_mah;
  1880. else
  1881. val->intval = di->bat_cap.prev_mah;
  1882. break;
  1883. case POWER_SUPPLY_PROP_CAPACITY:
  1884. if (di->bm->capacity_scaling)
  1885. val->intval = di->bat_cap.cap_scale.scaled_cap;
  1886. else if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1887. di->flags.batt_id_received)
  1888. val->intval = 100;
  1889. else
  1890. val->intval = di->bat_cap.prev_percent;
  1891. break;
  1892. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  1893. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1894. di->flags.batt_id_received)
  1895. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  1896. else
  1897. val->intval = di->bat_cap.prev_level;
  1898. break;
  1899. default:
  1900. return -EINVAL;
  1901. }
  1902. return 0;
  1903. }
  1904. static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
  1905. {
  1906. struct power_supply *psy;
  1907. struct power_supply *ext;
  1908. struct ab8500_fg *di;
  1909. union power_supply_propval ret;
  1910. int i, j;
  1911. bool psy_found = false;
  1912. psy = (struct power_supply *)data;
  1913. ext = dev_get_drvdata(dev);
  1914. di = to_ab8500_fg_device_info(psy);
  1915. /*
  1916. * For all psy where the name of your driver
  1917. * appears in any supplied_to
  1918. */
  1919. for (i = 0; i < ext->num_supplicants; i++) {
  1920. if (!strcmp(ext->supplied_to[i], psy->name))
  1921. psy_found = true;
  1922. }
  1923. if (!psy_found)
  1924. return 0;
  1925. /* Go through all properties for the psy */
  1926. for (j = 0; j < ext->num_properties; j++) {
  1927. enum power_supply_property prop;
  1928. prop = ext->properties[j];
  1929. if (ext->get_property(ext, prop, &ret))
  1930. continue;
  1931. switch (prop) {
  1932. case POWER_SUPPLY_PROP_STATUS:
  1933. switch (ext->type) {
  1934. case POWER_SUPPLY_TYPE_BATTERY:
  1935. switch (ret.intval) {
  1936. case POWER_SUPPLY_STATUS_UNKNOWN:
  1937. case POWER_SUPPLY_STATUS_DISCHARGING:
  1938. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  1939. if (!di->flags.charging)
  1940. break;
  1941. di->flags.charging = false;
  1942. di->flags.fully_charged = false;
  1943. if (di->bm->capacity_scaling)
  1944. ab8500_fg_update_cap_scalers(di);
  1945. queue_work(di->fg_wq, &di->fg_work);
  1946. break;
  1947. case POWER_SUPPLY_STATUS_FULL:
  1948. if (di->flags.fully_charged)
  1949. break;
  1950. di->flags.fully_charged = true;
  1951. di->flags.force_full = true;
  1952. /* Save current capacity as maximum */
  1953. di->bat_cap.max_mah = di->bat_cap.mah;
  1954. queue_work(di->fg_wq, &di->fg_work);
  1955. break;
  1956. case POWER_SUPPLY_STATUS_CHARGING:
  1957. if (di->flags.charging &&
  1958. !di->flags.fully_charged)
  1959. break;
  1960. di->flags.charging = true;
  1961. di->flags.fully_charged = false;
  1962. if (di->bm->capacity_scaling)
  1963. ab8500_fg_update_cap_scalers(di);
  1964. queue_work(di->fg_wq, &di->fg_work);
  1965. break;
  1966. };
  1967. default:
  1968. break;
  1969. };
  1970. break;
  1971. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1972. switch (ext->type) {
  1973. case POWER_SUPPLY_TYPE_BATTERY:
  1974. if (!di->flags.batt_id_received &&
  1975. di->bm->batt_id != BATTERY_UNKNOWN) {
  1976. const struct abx500_battery_type *b;
  1977. b = &(di->bm->bat_type[di->bm->batt_id]);
  1978. di->flags.batt_id_received = true;
  1979. di->bat_cap.max_mah_design =
  1980. MILLI_TO_MICRO *
  1981. b->charge_full_design;
  1982. di->bat_cap.max_mah =
  1983. di->bat_cap.max_mah_design;
  1984. di->vbat_nom = b->nominal_voltage;
  1985. }
  1986. if (ret.intval)
  1987. di->flags.batt_unknown = false;
  1988. else
  1989. di->flags.batt_unknown = true;
  1990. break;
  1991. default:
  1992. break;
  1993. }
  1994. break;
  1995. case POWER_SUPPLY_PROP_TEMP:
  1996. switch (ext->type) {
  1997. case POWER_SUPPLY_TYPE_BATTERY:
  1998. if (di->flags.batt_id_received)
  1999. di->bat_temp = ret.intval;
  2000. break;
  2001. default:
  2002. break;
  2003. }
  2004. break;
  2005. default:
  2006. break;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. /**
  2012. * ab8500_fg_init_hw_registers() - Set up FG related registers
  2013. * @di: pointer to the ab8500_fg structure
  2014. *
  2015. * Set up battery OVV, low battery voltage registers
  2016. */
  2017. static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
  2018. {
  2019. int ret;
  2020. /* Set VBAT OVV threshold */
  2021. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2022. AB8500_CHARGER,
  2023. AB8500_BATT_OVV,
  2024. BATT_OVV_TH_4P75,
  2025. BATT_OVV_TH_4P75);
  2026. if (ret) {
  2027. dev_err(di->dev, "failed to set BATT_OVV\n");
  2028. goto out;
  2029. }
  2030. /* Enable VBAT OVV detection */
  2031. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2032. AB8500_CHARGER,
  2033. AB8500_BATT_OVV,
  2034. BATT_OVV_ENA,
  2035. BATT_OVV_ENA);
  2036. if (ret) {
  2037. dev_err(di->dev, "failed to enable BATT_OVV\n");
  2038. goto out;
  2039. }
  2040. /* Low Battery Voltage */
  2041. ret = abx500_set_register_interruptible(di->dev,
  2042. AB8500_SYS_CTRL2_BLOCK,
  2043. AB8500_LOW_BAT_REG,
  2044. ab8500_volt_to_regval(
  2045. di->bm->fg_params->lowbat_threshold) << 1 |
  2046. LOW_BAT_ENABLE);
  2047. if (ret) {
  2048. dev_err(di->dev, "%s write failed\n", __func__);
  2049. goto out;
  2050. }
  2051. /* Battery OK threshold */
  2052. ret = ab8500_fg_battok_init_hw_register(di);
  2053. if (ret) {
  2054. dev_err(di->dev, "BattOk init write failed.\n");
  2055. goto out;
  2056. }
  2057. out:
  2058. return ret;
  2059. }
  2060. /**
  2061. * ab8500_fg_external_power_changed() - callback for power supply changes
  2062. * @psy: pointer to the structure power_supply
  2063. *
  2064. * This function is the entry point of the pointer external_power_changed
  2065. * of the structure power_supply.
  2066. * This function gets executed when there is a change in any external power
  2067. * supply that this driver needs to be notified of.
  2068. */
  2069. static void ab8500_fg_external_power_changed(struct power_supply *psy)
  2070. {
  2071. struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
  2072. class_for_each_device(power_supply_class, NULL,
  2073. &di->fg_psy, ab8500_fg_get_ext_psy_data);
  2074. }
  2075. /**
  2076. * abab8500_fg_reinit_work() - work to reset the FG algorithm
  2077. * @work: pointer to the work_struct structure
  2078. *
  2079. * Used to reset the current battery capacity to be able to
  2080. * retrigger a new voltage base capacity calculation. For
  2081. * test and verification purpose.
  2082. */
  2083. static void ab8500_fg_reinit_work(struct work_struct *work)
  2084. {
  2085. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  2086. fg_reinit_work.work);
  2087. if (di->flags.calibrate == false) {
  2088. dev_dbg(di->dev, "Resetting FG state machine to init.\n");
  2089. ab8500_fg_clear_cap_samples(di);
  2090. ab8500_fg_calc_cap_discharge_voltage(di, true);
  2091. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  2092. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
  2093. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2094. } else {
  2095. dev_err(di->dev, "Residual offset calibration ongoing "
  2096. "retrying..\n");
  2097. /* Wait one second until next try*/
  2098. queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
  2099. round_jiffies(1));
  2100. }
  2101. }
  2102. /**
  2103. * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values
  2104. *
  2105. * This function can be used to force the FG algorithm to recalculate a new
  2106. * voltage based battery capacity.
  2107. */
  2108. void ab8500_fg_reinit(void)
  2109. {
  2110. struct ab8500_fg *di = ab8500_fg_get();
  2111. /* User won't be notified if a null pointer returned. */
  2112. if (di != NULL)
  2113. queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
  2114. }
  2115. /* Exposure to the sysfs interface */
  2116. struct ab8500_fg_sysfs_entry {
  2117. struct attribute attr;
  2118. ssize_t (*show)(struct ab8500_fg *, char *);
  2119. ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
  2120. };
  2121. static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
  2122. {
  2123. return sprintf(buf, "%d\n", di->bat_cap.max_mah);
  2124. }
  2125. static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
  2126. size_t count)
  2127. {
  2128. unsigned long charge_full;
  2129. ssize_t ret = -EINVAL;
  2130. ret = strict_strtoul(buf, 10, &charge_full);
  2131. dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
  2132. if (!ret) {
  2133. di->bat_cap.max_mah = (int) charge_full;
  2134. ret = count;
  2135. }
  2136. return ret;
  2137. }
  2138. static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
  2139. {
  2140. return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
  2141. }
  2142. static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
  2143. size_t count)
  2144. {
  2145. unsigned long charge_now;
  2146. ssize_t ret;
  2147. ret = strict_strtoul(buf, 10, &charge_now);
  2148. dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
  2149. ret, charge_now, di->bat_cap.prev_mah);
  2150. if (!ret) {
  2151. di->bat_cap.user_mah = (int) charge_now;
  2152. di->flags.user_cap = true;
  2153. ret = count;
  2154. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2155. }
  2156. return ret;
  2157. }
  2158. static struct ab8500_fg_sysfs_entry charge_full_attr =
  2159. __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
  2160. static struct ab8500_fg_sysfs_entry charge_now_attr =
  2161. __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
  2162. static ssize_t
  2163. ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
  2164. {
  2165. struct ab8500_fg_sysfs_entry *entry;
  2166. struct ab8500_fg *di;
  2167. entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
  2168. di = container_of(kobj, struct ab8500_fg, fg_kobject);
  2169. if (!entry->show)
  2170. return -EIO;
  2171. return entry->show(di, buf);
  2172. }
  2173. static ssize_t
  2174. ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
  2175. size_t count)
  2176. {
  2177. struct ab8500_fg_sysfs_entry *entry;
  2178. struct ab8500_fg *di;
  2179. entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
  2180. di = container_of(kobj, struct ab8500_fg, fg_kobject);
  2181. if (!entry->store)
  2182. return -EIO;
  2183. return entry->store(di, buf, count);
  2184. }
  2185. static const struct sysfs_ops ab8500_fg_sysfs_ops = {
  2186. .show = ab8500_fg_show,
  2187. .store = ab8500_fg_store,
  2188. };
  2189. static struct attribute *ab8500_fg_attrs[] = {
  2190. &charge_full_attr.attr,
  2191. &charge_now_attr.attr,
  2192. NULL,
  2193. };
  2194. static struct kobj_type ab8500_fg_ktype = {
  2195. .sysfs_ops = &ab8500_fg_sysfs_ops,
  2196. .default_attrs = ab8500_fg_attrs,
  2197. };
  2198. /**
  2199. * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
  2200. * @di: pointer to the struct ab8500_chargalg
  2201. *
  2202. * This function removes the entry in sysfs.
  2203. */
  2204. static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
  2205. {
  2206. kobject_del(&di->fg_kobject);
  2207. }
  2208. /**
  2209. * ab8500_chargalg_sysfs_init() - init of sysfs entry
  2210. * @di: pointer to the struct ab8500_chargalg
  2211. *
  2212. * This function adds an entry in sysfs.
  2213. * Returns error code in case of failure else 0(on success)
  2214. */
  2215. static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
  2216. {
  2217. int ret = 0;
  2218. ret = kobject_init_and_add(&di->fg_kobject,
  2219. &ab8500_fg_ktype,
  2220. NULL, "battery");
  2221. if (ret < 0)
  2222. dev_err(di->dev, "failed to create sysfs entry\n");
  2223. return ret;
  2224. }
  2225. /* Exposure to the sysfs interface <<END>> */
  2226. #if defined(CONFIG_PM)
  2227. static int ab8500_fg_resume(struct platform_device *pdev)
  2228. {
  2229. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2230. /*
  2231. * Change state if we're not charging. If we're charging we will wake
  2232. * up on the FG IRQ
  2233. */
  2234. if (!di->flags.charging) {
  2235. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
  2236. queue_work(di->fg_wq, &di->fg_work);
  2237. }
  2238. return 0;
  2239. }
  2240. static int ab8500_fg_suspend(struct platform_device *pdev,
  2241. pm_message_t state)
  2242. {
  2243. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2244. flush_delayed_work(&di->fg_periodic_work);
  2245. flush_work(&di->fg_work);
  2246. flush_work(&di->fg_acc_cur_work);
  2247. flush_delayed_work(&di->fg_reinit_work);
  2248. flush_delayed_work(&di->fg_low_bat_work);
  2249. flush_delayed_work(&di->fg_check_hw_failure_work);
  2250. /*
  2251. * If the FG is enabled we will disable it before going to suspend
  2252. * only if we're not charging
  2253. */
  2254. if (di->flags.fg_enabled && !di->flags.charging)
  2255. ab8500_fg_coulomb_counter(di, false);
  2256. return 0;
  2257. }
  2258. #else
  2259. #define ab8500_fg_suspend NULL
  2260. #define ab8500_fg_resume NULL
  2261. #endif
  2262. static int ab8500_fg_remove(struct platform_device *pdev)
  2263. {
  2264. int ret = 0;
  2265. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2266. list_del(&di->node);
  2267. /* Disable coulomb counter */
  2268. ret = ab8500_fg_coulomb_counter(di, false);
  2269. if (ret)
  2270. dev_err(di->dev, "failed to disable coulomb counter\n");
  2271. destroy_workqueue(di->fg_wq);
  2272. ab8500_fg_sysfs_exit(di);
  2273. flush_scheduled_work();
  2274. power_supply_unregister(&di->fg_psy);
  2275. platform_set_drvdata(pdev, NULL);
  2276. return ret;
  2277. }
  2278. /* ab8500 fg driver interrupts and their respective isr */
  2279. static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
  2280. {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
  2281. {"BATT_OVV", ab8500_fg_batt_ovv_handler},
  2282. {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
  2283. {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
  2284. {"CCEOC", ab8500_fg_cc_data_end_handler},
  2285. };
  2286. static char *supply_interface[] = {
  2287. "ab8500_chargalg",
  2288. "ab8500_usb",
  2289. };
  2290. static int ab8500_fg_probe(struct platform_device *pdev)
  2291. {
  2292. struct device_node *np = pdev->dev.of_node;
  2293. struct abx500_bm_data *plat = pdev->dev.platform_data;
  2294. struct ab8500_fg *di;
  2295. int i, irq;
  2296. int ret = 0;
  2297. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  2298. if (!di) {
  2299. dev_err(&pdev->dev, "%s no mem for ab8500_fg\n", __func__);
  2300. return -ENOMEM;
  2301. }
  2302. if (!plat) {
  2303. dev_err(&pdev->dev, "no battery management data supplied\n");
  2304. return -EINVAL;
  2305. }
  2306. di->bm = plat;
  2307. if (np) {
  2308. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  2309. if (ret) {
  2310. dev_err(&pdev->dev, "failed to get battery information\n");
  2311. return ret;
  2312. }
  2313. }
  2314. mutex_init(&di->cc_lock);
  2315. /* get parent data */
  2316. di->dev = &pdev->dev;
  2317. di->parent = dev_get_drvdata(pdev->dev.parent);
  2318. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  2319. di->fg_psy.name = "ab8500_fg";
  2320. di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  2321. di->fg_psy.properties = ab8500_fg_props;
  2322. di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
  2323. di->fg_psy.get_property = ab8500_fg_get_property;
  2324. di->fg_psy.supplied_to = supply_interface;
  2325. di->fg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
  2326. di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
  2327. di->bat_cap.max_mah_design = MILLI_TO_MICRO *
  2328. di->bm->bat_type[di->bm->batt_id].charge_full_design;
  2329. di->bat_cap.max_mah = di->bat_cap.max_mah_design;
  2330. di->vbat_nom = di->bm->bat_type[di->bm->batt_id].nominal_voltage;
  2331. di->init_capacity = true;
  2332. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  2333. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
  2334. /* Create a work queue for running the FG algorithm */
  2335. di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
  2336. if (di->fg_wq == NULL) {
  2337. dev_err(di->dev, "failed to create work queue\n");
  2338. return -ENOMEM;
  2339. }
  2340. /* Init work for running the fg algorithm instantly */
  2341. INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
  2342. /* Init work for getting the battery accumulated current */
  2343. INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
  2344. /* Init work for reinitialising the fg algorithm */
  2345. INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
  2346. ab8500_fg_reinit_work);
  2347. /* Work delayed Queue to run the state machine */
  2348. INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
  2349. ab8500_fg_periodic_work);
  2350. /* Work to check low battery condition */
  2351. INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
  2352. ab8500_fg_low_bat_work);
  2353. /* Init work for HW failure check */
  2354. INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
  2355. ab8500_fg_check_hw_failure_work);
  2356. /* Reset battery low voltage flag */
  2357. di->flags.low_bat = false;
  2358. /* Initialize low battery counter */
  2359. di->low_bat_cnt = 10;
  2360. /* Initialize OVV, and other registers */
  2361. ret = ab8500_fg_init_hw_registers(di);
  2362. if (ret) {
  2363. dev_err(di->dev, "failed to initialize registers\n");
  2364. goto free_inst_curr_wq;
  2365. }
  2366. /* Consider battery unknown until we're informed otherwise */
  2367. di->flags.batt_unknown = true;
  2368. di->flags.batt_id_received = false;
  2369. /* Register FG power supply class */
  2370. ret = power_supply_register(di->dev, &di->fg_psy);
  2371. if (ret) {
  2372. dev_err(di->dev, "failed to register FG psy\n");
  2373. goto free_inst_curr_wq;
  2374. }
  2375. di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
  2376. ab8500_fg_coulomb_counter(di, true);
  2377. /*
  2378. * Initialize completion used to notify completion and start
  2379. * of inst current
  2380. */
  2381. init_completion(&di->ab8500_fg_started);
  2382. init_completion(&di->ab8500_fg_complete);
  2383. /* Register interrupts */
  2384. for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
  2385. irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
  2386. ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
  2387. IRQF_SHARED | IRQF_NO_SUSPEND,
  2388. ab8500_fg_irq[i].name, di);
  2389. if (ret != 0) {
  2390. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  2391. , ab8500_fg_irq[i].name, irq, ret);
  2392. goto free_irq;
  2393. }
  2394. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  2395. ab8500_fg_irq[i].name, irq, ret);
  2396. }
  2397. di->irq = platform_get_irq_byname(pdev, "CCEOC");
  2398. disable_irq(di->irq);
  2399. di->nbr_cceoc_irq_cnt = 0;
  2400. platform_set_drvdata(pdev, di);
  2401. ret = ab8500_fg_sysfs_init(di);
  2402. if (ret) {
  2403. dev_err(di->dev, "failed to create sysfs entry\n");
  2404. goto free_irq;
  2405. }
  2406. /* Calibrate the fg first time */
  2407. di->flags.calibrate = true;
  2408. di->calib_state = AB8500_FG_CALIB_INIT;
  2409. /* Use room temp as default value until we get an update from driver. */
  2410. di->bat_temp = 210;
  2411. /* Run the FG algorithm */
  2412. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2413. list_add_tail(&di->node, &ab8500_fg_list);
  2414. return ret;
  2415. free_irq:
  2416. power_supply_unregister(&di->fg_psy);
  2417. /* We also have to free all successfully registered irqs */
  2418. for (i = i - 1; i >= 0; i--) {
  2419. irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
  2420. free_irq(irq, di);
  2421. }
  2422. free_inst_curr_wq:
  2423. destroy_workqueue(di->fg_wq);
  2424. return ret;
  2425. }
  2426. static const struct of_device_id ab8500_fg_match[] = {
  2427. { .compatible = "stericsson,ab8500-fg", },
  2428. { },
  2429. };
  2430. static struct platform_driver ab8500_fg_driver = {
  2431. .probe = ab8500_fg_probe,
  2432. .remove = ab8500_fg_remove,
  2433. .suspend = ab8500_fg_suspend,
  2434. .resume = ab8500_fg_resume,
  2435. .driver = {
  2436. .name = "ab8500-fg",
  2437. .owner = THIS_MODULE,
  2438. .of_match_table = ab8500_fg_match,
  2439. },
  2440. };
  2441. static int __init ab8500_fg_init(void)
  2442. {
  2443. return platform_driver_register(&ab8500_fg_driver);
  2444. }
  2445. static void __exit ab8500_fg_exit(void)
  2446. {
  2447. platform_driver_unregister(&ab8500_fg_driver);
  2448. }
  2449. subsys_initcall_sync(ab8500_fg_init);
  2450. module_exit(ab8500_fg_exit);
  2451. MODULE_LICENSE("GPL v2");
  2452. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  2453. MODULE_ALIAS("platform:ab8500-fg");
  2454. MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");