ab8500_fg.c 83 KB

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