ab8500_fg.c 71 KB

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