ab8500_fg.c 71 KB

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