ab8500_fg.c 84 KB

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