ab8500_fg.c 83 KB

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