ab8500_fg.c 83 KB

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