ab8500_fg.c 84 KB

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