ab8500_fg.c 84 KB

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