ab8500_fg.c 84 KB

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