ab8500_fg.c 84 KB

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