ab8500_fg.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2012
  3. *
  4. * Main and Back-up battery management driver.
  5. *
  6. * Note: Backup battery management is required in case of Li-Ion battery and not
  7. * for capacitive battery. HREF boards have capacitive battery and hence backup
  8. * battery management is not used and the supported code is available in this
  9. * driver.
  10. *
  11. * License Terms: GNU General Public License v2
  12. * Author:
  13. * Johan Palsson <johan.palsson@stericsson.com>
  14. * Karl Komierowski <karl.komierowski@stericsson.com>
  15. * Arun R Murthy <arun.murthy@stericsson.com>
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/kobject.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/time.h>
  27. #include <linux/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 1071
  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. * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542mA
  601. * Given a 250ms conversion cycle time the LSB corresponds
  602. * to 107.1 nAh. Convert to current by dividing by the conversion
  603. * time in hours (250ms = 1 / (3600 * 4)h)
  604. * 107.1nAh 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. const 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. const 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. percent = 1;
  1191. changed = true;
  1192. } else if (!(!di->flags.charging &&
  1193. percent > di->bat_cap.prev_percent) || init) {
  1194. /*
  1195. * We do not allow reported capacity to go up
  1196. * unless we're charging or if we're in init
  1197. */
  1198. dev_dbg(di->dev,
  1199. "capacity changed from %d to %d (%d)\n",
  1200. di->bat_cap.prev_percent,
  1201. percent,
  1202. di->bat_cap.permille);
  1203. di->bat_cap.prev_percent = percent;
  1204. di->bat_cap.prev_mah = di->bat_cap.mah;
  1205. changed = true;
  1206. } else {
  1207. dev_dbg(di->dev, "capacity not allowed to go up since "
  1208. "no charger is connected: %d to %d (%d)\n",
  1209. di->bat_cap.prev_percent,
  1210. percent,
  1211. di->bat_cap.permille);
  1212. }
  1213. }
  1214. if (changed) {
  1215. if (di->bm->capacity_scaling) {
  1216. di->bat_cap.cap_scale.scaled_cap =
  1217. ab8500_fg_calculate_scaled_capacity(di);
  1218. dev_info(di->dev, "capacity=%d (%d)\n",
  1219. di->bat_cap.prev_percent,
  1220. di->bat_cap.cap_scale.scaled_cap);
  1221. }
  1222. power_supply_changed(&di->fg_psy);
  1223. if (di->flags.fully_charged && di->flags.force_full) {
  1224. dev_dbg(di->dev, "Battery full, notifying.\n");
  1225. di->flags.force_full = false;
  1226. sysfs_notify(&di->fg_kobject, NULL, "charge_full");
  1227. }
  1228. sysfs_notify(&di->fg_kobject, NULL, "charge_now");
  1229. }
  1230. }
  1231. static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
  1232. enum ab8500_fg_charge_state new_state)
  1233. {
  1234. dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
  1235. di->charge_state,
  1236. charge_state[di->charge_state],
  1237. new_state,
  1238. charge_state[new_state]);
  1239. di->charge_state = new_state;
  1240. }
  1241. static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
  1242. enum ab8500_fg_discharge_state new_state)
  1243. {
  1244. dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
  1245. di->discharge_state,
  1246. discharge_state[di->discharge_state],
  1247. new_state,
  1248. discharge_state[new_state]);
  1249. di->discharge_state = new_state;
  1250. }
  1251. /**
  1252. * ab8500_fg_algorithm_charging() - FG algorithm for when charging
  1253. * @di: pointer to the ab8500_fg structure
  1254. *
  1255. * Battery capacity calculation state machine for when we're charging
  1256. */
  1257. static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
  1258. {
  1259. /*
  1260. * If we change to discharge mode
  1261. * we should start with recovery
  1262. */
  1263. if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
  1264. ab8500_fg_discharge_state_to(di,
  1265. AB8500_FG_DISCHARGE_INIT_RECOVERY);
  1266. switch (di->charge_state) {
  1267. case AB8500_FG_CHARGE_INIT:
  1268. di->fg_samples = SEC_TO_SAMPLE(
  1269. di->bm->fg_params->accu_charging);
  1270. ab8500_fg_coulomb_counter(di, true);
  1271. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
  1272. break;
  1273. case AB8500_FG_CHARGE_READOUT:
  1274. /*
  1275. * Read the FG and calculate the new capacity
  1276. */
  1277. mutex_lock(&di->cc_lock);
  1278. if (!di->flags.conv_done && !di->flags.force_full) {
  1279. /* Wasn't the CC IRQ that got us here */
  1280. mutex_unlock(&di->cc_lock);
  1281. dev_dbg(di->dev, "%s CC conv not done\n",
  1282. __func__);
  1283. break;
  1284. }
  1285. di->flags.conv_done = false;
  1286. mutex_unlock(&di->cc_lock);
  1287. ab8500_fg_calc_cap_charging(di);
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. /* Check capacity limits */
  1293. ab8500_fg_check_capacity_limits(di, false);
  1294. }
  1295. static void force_capacity(struct ab8500_fg *di)
  1296. {
  1297. int cap;
  1298. ab8500_fg_clear_cap_samples(di);
  1299. cap = di->bat_cap.user_mah;
  1300. if (cap > di->bat_cap.max_mah_design) {
  1301. dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
  1302. " %d\n", cap, di->bat_cap.max_mah_design);
  1303. cap = di->bat_cap.max_mah_design;
  1304. }
  1305. ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
  1306. di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
  1307. di->bat_cap.mah = cap;
  1308. ab8500_fg_check_capacity_limits(di, true);
  1309. }
  1310. static bool check_sysfs_capacity(struct ab8500_fg *di)
  1311. {
  1312. int cap, lower, upper;
  1313. int cap_permille;
  1314. cap = di->bat_cap.user_mah;
  1315. cap_permille = ab8500_fg_convert_mah_to_permille(di,
  1316. di->bat_cap.user_mah);
  1317. lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
  1318. upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
  1319. if (lower < 0)
  1320. lower = 0;
  1321. /* 1000 is permille, -> 100 percent */
  1322. if (upper > 1000)
  1323. upper = 1000;
  1324. dev_dbg(di->dev, "Capacity limits:"
  1325. " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
  1326. lower, cap_permille, upper, cap, di->bat_cap.mah);
  1327. /* If within limits, use the saved capacity and exit estimation...*/
  1328. if (cap_permille > lower && cap_permille < upper) {
  1329. dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
  1330. force_capacity(di);
  1331. return true;
  1332. }
  1333. dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
  1334. return false;
  1335. }
  1336. /**
  1337. * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
  1338. * @di: pointer to the ab8500_fg structure
  1339. *
  1340. * Battery capacity calculation state machine for when we're discharging
  1341. */
  1342. static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
  1343. {
  1344. int sleep_time;
  1345. /* If we change to charge mode we should start with init */
  1346. if (di->charge_state != AB8500_FG_CHARGE_INIT)
  1347. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  1348. switch (di->discharge_state) {
  1349. case AB8500_FG_DISCHARGE_INIT:
  1350. /* We use the FG IRQ to work on */
  1351. di->init_cnt = 0;
  1352. di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
  1353. ab8500_fg_coulomb_counter(di, true);
  1354. ab8500_fg_discharge_state_to(di,
  1355. AB8500_FG_DISCHARGE_INITMEASURING);
  1356. /* Intentional fallthrough */
  1357. case AB8500_FG_DISCHARGE_INITMEASURING:
  1358. /*
  1359. * Discard a number of samples during startup.
  1360. * After that, use compensated voltage for a few
  1361. * samples to get an initial capacity.
  1362. * Then go to READOUT
  1363. */
  1364. sleep_time = di->bm->fg_params->init_timer;
  1365. /* Discard the first [x] seconds */
  1366. if (di->init_cnt > di->bm->fg_params->init_discard_time) {
  1367. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1368. ab8500_fg_check_capacity_limits(di, true);
  1369. }
  1370. di->init_cnt += sleep_time;
  1371. if (di->init_cnt > di->bm->fg_params->init_total_time)
  1372. ab8500_fg_discharge_state_to(di,
  1373. AB8500_FG_DISCHARGE_READOUT_INIT);
  1374. break;
  1375. case AB8500_FG_DISCHARGE_INIT_RECOVERY:
  1376. di->recovery_cnt = 0;
  1377. di->recovery_needed = true;
  1378. ab8500_fg_discharge_state_to(di,
  1379. AB8500_FG_DISCHARGE_RECOVERY);
  1380. /* Intentional fallthrough */
  1381. case AB8500_FG_DISCHARGE_RECOVERY:
  1382. sleep_time = di->bm->fg_params->recovery_sleep_timer;
  1383. /*
  1384. * We should check the power consumption
  1385. * If low, go to READOUT (after x min) or
  1386. * RECOVERY_SLEEP if time left.
  1387. * If high, go to READOUT
  1388. */
  1389. di->inst_curr = ab8500_fg_inst_curr_blocking(di);
  1390. if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
  1391. if (di->recovery_cnt >
  1392. di->bm->fg_params->recovery_total_time) {
  1393. di->fg_samples = SEC_TO_SAMPLE(
  1394. di->bm->fg_params->accu_high_curr);
  1395. ab8500_fg_coulomb_counter(di, true);
  1396. ab8500_fg_discharge_state_to(di,
  1397. AB8500_FG_DISCHARGE_READOUT);
  1398. di->recovery_needed = false;
  1399. } else {
  1400. queue_delayed_work(di->fg_wq,
  1401. &di->fg_periodic_work,
  1402. sleep_time * HZ);
  1403. }
  1404. di->recovery_cnt += sleep_time;
  1405. } else {
  1406. di->fg_samples = SEC_TO_SAMPLE(
  1407. di->bm->fg_params->accu_high_curr);
  1408. ab8500_fg_coulomb_counter(di, true);
  1409. ab8500_fg_discharge_state_to(di,
  1410. AB8500_FG_DISCHARGE_READOUT);
  1411. }
  1412. break;
  1413. case AB8500_FG_DISCHARGE_READOUT_INIT:
  1414. di->fg_samples = SEC_TO_SAMPLE(
  1415. di->bm->fg_params->accu_high_curr);
  1416. ab8500_fg_coulomb_counter(di, true);
  1417. ab8500_fg_discharge_state_to(di,
  1418. AB8500_FG_DISCHARGE_READOUT);
  1419. break;
  1420. case AB8500_FG_DISCHARGE_READOUT:
  1421. di->inst_curr = ab8500_fg_inst_curr_blocking(di);
  1422. if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
  1423. /* Detect mode change */
  1424. if (di->high_curr_mode) {
  1425. di->high_curr_mode = false;
  1426. di->high_curr_cnt = 0;
  1427. }
  1428. if (di->recovery_needed) {
  1429. ab8500_fg_discharge_state_to(di,
  1430. AB8500_FG_DISCHARGE_INIT_RECOVERY);
  1431. queue_delayed_work(di->fg_wq,
  1432. &di->fg_periodic_work, 0);
  1433. break;
  1434. }
  1435. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1436. } else {
  1437. mutex_lock(&di->cc_lock);
  1438. if (!di->flags.conv_done) {
  1439. /* Wasn't the CC IRQ that got us here */
  1440. mutex_unlock(&di->cc_lock);
  1441. dev_dbg(di->dev, "%s CC conv not done\n",
  1442. __func__);
  1443. break;
  1444. }
  1445. di->flags.conv_done = false;
  1446. mutex_unlock(&di->cc_lock);
  1447. /* Detect mode change */
  1448. if (!di->high_curr_mode) {
  1449. di->high_curr_mode = true;
  1450. di->high_curr_cnt = 0;
  1451. }
  1452. di->high_curr_cnt +=
  1453. di->bm->fg_params->accu_high_curr;
  1454. if (di->high_curr_cnt >
  1455. di->bm->fg_params->high_curr_time)
  1456. di->recovery_needed = true;
  1457. ab8500_fg_calc_cap_discharge_fg(di);
  1458. }
  1459. ab8500_fg_check_capacity_limits(di, false);
  1460. break;
  1461. case AB8500_FG_DISCHARGE_WAKEUP:
  1462. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1463. di->fg_samples = SEC_TO_SAMPLE(
  1464. di->bm->fg_params->accu_high_curr);
  1465. ab8500_fg_coulomb_counter(di, true);
  1466. ab8500_fg_discharge_state_to(di,
  1467. AB8500_FG_DISCHARGE_READOUT);
  1468. ab8500_fg_check_capacity_limits(di, false);
  1469. break;
  1470. default:
  1471. break;
  1472. }
  1473. }
  1474. /**
  1475. * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
  1476. * @di: pointer to the ab8500_fg structure
  1477. *
  1478. */
  1479. static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
  1480. {
  1481. int ret;
  1482. switch (di->calib_state) {
  1483. case AB8500_FG_CALIB_INIT:
  1484. dev_dbg(di->dev, "Calibration ongoing...\n");
  1485. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1486. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1487. CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
  1488. if (ret < 0)
  1489. goto err;
  1490. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1491. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1492. CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
  1493. if (ret < 0)
  1494. goto err;
  1495. di->calib_state = AB8500_FG_CALIB_WAIT;
  1496. break;
  1497. case AB8500_FG_CALIB_END:
  1498. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1499. AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
  1500. CC_MUXOFFSET, CC_MUXOFFSET);
  1501. if (ret < 0)
  1502. goto err;
  1503. di->flags.calibrate = false;
  1504. dev_dbg(di->dev, "Calibration done...\n");
  1505. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1506. break;
  1507. case AB8500_FG_CALIB_WAIT:
  1508. dev_dbg(di->dev, "Calibration WFI\n");
  1509. default:
  1510. break;
  1511. }
  1512. return;
  1513. err:
  1514. /* Something went wrong, don't calibrate then */
  1515. dev_err(di->dev, "failed to calibrate the CC\n");
  1516. di->flags.calibrate = false;
  1517. di->calib_state = AB8500_FG_CALIB_INIT;
  1518. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1519. }
  1520. /**
  1521. * ab8500_fg_algorithm() - Entry point for the FG algorithm
  1522. * @di: pointer to the ab8500_fg structure
  1523. *
  1524. * Entry point for the battery capacity calculation state machine
  1525. */
  1526. static void ab8500_fg_algorithm(struct ab8500_fg *di)
  1527. {
  1528. if (di->flags.calibrate)
  1529. ab8500_fg_algorithm_calibrate(di);
  1530. else {
  1531. if (di->flags.charging)
  1532. ab8500_fg_algorithm_charging(di);
  1533. else
  1534. ab8500_fg_algorithm_discharging(di);
  1535. }
  1536. dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
  1537. "%d %d %d %d %d %d %d\n",
  1538. di->bat_cap.max_mah_design,
  1539. di->bat_cap.max_mah,
  1540. di->bat_cap.mah,
  1541. di->bat_cap.permille,
  1542. di->bat_cap.level,
  1543. di->bat_cap.prev_mah,
  1544. di->bat_cap.prev_percent,
  1545. di->bat_cap.prev_level,
  1546. di->vbat,
  1547. di->inst_curr,
  1548. di->avg_curr,
  1549. di->accu_charge,
  1550. di->flags.charging,
  1551. di->charge_state,
  1552. di->discharge_state,
  1553. di->high_curr_mode,
  1554. di->recovery_needed);
  1555. }
  1556. /**
  1557. * ab8500_fg_periodic_work() - Run the FG state machine periodically
  1558. * @work: pointer to the work_struct structure
  1559. *
  1560. * Work queue function for periodic work
  1561. */
  1562. static void ab8500_fg_periodic_work(struct work_struct *work)
  1563. {
  1564. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1565. fg_periodic_work.work);
  1566. if (di->init_capacity) {
  1567. /* Get an initial capacity calculation */
  1568. ab8500_fg_calc_cap_discharge_voltage(di, true);
  1569. ab8500_fg_check_capacity_limits(di, true);
  1570. di->init_capacity = false;
  1571. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1572. } else if (di->flags.user_cap) {
  1573. if (check_sysfs_capacity(di)) {
  1574. ab8500_fg_check_capacity_limits(di, true);
  1575. if (di->flags.charging)
  1576. ab8500_fg_charge_state_to(di,
  1577. AB8500_FG_CHARGE_INIT);
  1578. else
  1579. ab8500_fg_discharge_state_to(di,
  1580. AB8500_FG_DISCHARGE_READOUT_INIT);
  1581. }
  1582. di->flags.user_cap = false;
  1583. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1584. } else
  1585. ab8500_fg_algorithm(di);
  1586. }
  1587. /**
  1588. * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
  1589. * @work: pointer to the work_struct structure
  1590. *
  1591. * Work queue function for checking the OVV_BAT condition
  1592. */
  1593. static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
  1594. {
  1595. int ret;
  1596. u8 reg_value;
  1597. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1598. fg_check_hw_failure_work.work);
  1599. /*
  1600. * If we have had a battery over-voltage situation,
  1601. * check ovv-bit to see if it should be reset.
  1602. */
  1603. ret = abx500_get_register_interruptible(di->dev,
  1604. AB8500_CHARGER, AB8500_CH_STAT_REG,
  1605. &reg_value);
  1606. if (ret < 0) {
  1607. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1608. return;
  1609. }
  1610. if ((reg_value & BATT_OVV) == BATT_OVV) {
  1611. if (!di->flags.bat_ovv) {
  1612. dev_dbg(di->dev, "Battery OVV\n");
  1613. di->flags.bat_ovv = true;
  1614. power_supply_changed(&di->fg_psy);
  1615. }
  1616. /* Not yet recovered from ovv, reschedule this test */
  1617. queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
  1618. HZ);
  1619. } else {
  1620. dev_dbg(di->dev, "Battery recovered from OVV\n");
  1621. di->flags.bat_ovv = false;
  1622. power_supply_changed(&di->fg_psy);
  1623. }
  1624. }
  1625. /**
  1626. * ab8500_fg_low_bat_work() - Check LOW_BAT condition
  1627. * @work: pointer to the work_struct structure
  1628. *
  1629. * Work queue function for checking the LOW_BAT condition
  1630. */
  1631. static void ab8500_fg_low_bat_work(struct work_struct *work)
  1632. {
  1633. int vbat;
  1634. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  1635. fg_low_bat_work.work);
  1636. vbat = ab8500_fg_bat_voltage(di);
  1637. /* Check if LOW_BAT still fulfilled */
  1638. if (vbat < di->bm->fg_params->lowbat_threshold) {
  1639. /* Is it time to shut down? */
  1640. if (di->low_bat_cnt < 1) {
  1641. di->flags.low_bat = true;
  1642. dev_warn(di->dev, "Shut down pending...\n");
  1643. } else {
  1644. /*
  1645. * Else we need to re-schedule this check to be able to detect
  1646. * if the voltage increases again during charging or
  1647. * due to decreasing load.
  1648. */
  1649. di->low_bat_cnt--;
  1650. dev_warn(di->dev, "Battery voltage still LOW\n");
  1651. queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
  1652. round_jiffies(LOW_BAT_CHECK_INTERVAL));
  1653. }
  1654. } else {
  1655. di->flags.low_bat_delay = false;
  1656. di->low_bat_cnt = 10;
  1657. dev_warn(di->dev, "Battery voltage OK again\n");
  1658. }
  1659. /* This is needed to dispatch LOW_BAT */
  1660. ab8500_fg_check_capacity_limits(di, false);
  1661. }
  1662. /**
  1663. * ab8500_fg_battok_calc - calculate the bit pattern corresponding
  1664. * to the target voltage.
  1665. * @di: pointer to the ab8500_fg structure
  1666. * @target target voltage
  1667. *
  1668. * Returns bit pattern closest to the target voltage
  1669. * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
  1670. */
  1671. static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
  1672. {
  1673. if (target > BATT_OK_MIN +
  1674. (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
  1675. return BATT_OK_MAX_NR_INCREMENTS;
  1676. if (target < BATT_OK_MIN)
  1677. return 0;
  1678. return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
  1679. }
  1680. /**
  1681. * ab8500_fg_battok_init_hw_register - init battok levels
  1682. * @di: pointer to the ab8500_fg structure
  1683. *
  1684. */
  1685. static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
  1686. {
  1687. int selected;
  1688. int sel0;
  1689. int sel1;
  1690. int cbp_sel0;
  1691. int cbp_sel1;
  1692. int ret;
  1693. int new_val;
  1694. sel0 = di->bm->fg_params->battok_falling_th_sel0;
  1695. sel1 = di->bm->fg_params->battok_raising_th_sel1;
  1696. cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
  1697. cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
  1698. selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
  1699. if (selected != sel0)
  1700. dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
  1701. sel0, selected, cbp_sel0);
  1702. selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
  1703. if (selected != sel1)
  1704. dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
  1705. sel1, selected, cbp_sel1);
  1706. new_val = cbp_sel0 | (cbp_sel1 << 4);
  1707. dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
  1708. ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
  1709. AB8500_BATT_OK_REG, new_val);
  1710. return ret;
  1711. }
  1712. /**
  1713. * ab8500_fg_instant_work() - Run the FG state machine instantly
  1714. * @work: pointer to the work_struct structure
  1715. *
  1716. * Work queue function for instant work
  1717. */
  1718. static void ab8500_fg_instant_work(struct work_struct *work)
  1719. {
  1720. struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
  1721. ab8500_fg_algorithm(di);
  1722. }
  1723. /**
  1724. * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
  1725. * @irq: interrupt number
  1726. * @_di: pointer to the ab8500_fg structure
  1727. *
  1728. * Returns IRQ status(IRQ_HANDLED)
  1729. */
  1730. static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
  1731. {
  1732. struct ab8500_fg *di = _di;
  1733. if (!di->nbr_cceoc_irq_cnt) {
  1734. di->nbr_cceoc_irq_cnt++;
  1735. complete(&di->ab8500_fg_started);
  1736. } else {
  1737. di->nbr_cceoc_irq_cnt = 0;
  1738. complete(&di->ab8500_fg_complete);
  1739. }
  1740. return IRQ_HANDLED;
  1741. }
  1742. /**
  1743. * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
  1744. * @irq: interrupt number
  1745. * @_di: pointer to the ab8500_fg structure
  1746. *
  1747. * Returns IRQ status(IRQ_HANDLED)
  1748. */
  1749. static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
  1750. {
  1751. struct ab8500_fg *di = _di;
  1752. di->calib_state = AB8500_FG_CALIB_END;
  1753. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  1754. return IRQ_HANDLED;
  1755. }
  1756. /**
  1757. * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
  1758. * @irq: interrupt number
  1759. * @_di: pointer to the ab8500_fg structure
  1760. *
  1761. * Returns IRQ status(IRQ_HANDLED)
  1762. */
  1763. static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
  1764. {
  1765. struct ab8500_fg *di = _di;
  1766. queue_work(di->fg_wq, &di->fg_acc_cur_work);
  1767. return IRQ_HANDLED;
  1768. }
  1769. /**
  1770. * ab8500_fg_batt_ovv_handler() - Battery OVV occured
  1771. * @irq: interrupt number
  1772. * @_di: pointer to the ab8500_fg structure
  1773. *
  1774. * Returns IRQ status(IRQ_HANDLED)
  1775. */
  1776. static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
  1777. {
  1778. struct ab8500_fg *di = _di;
  1779. dev_dbg(di->dev, "Battery OVV\n");
  1780. /* Schedule a new HW failure check */
  1781. queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
  1782. return IRQ_HANDLED;
  1783. }
  1784. /**
  1785. * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
  1786. * @irq: interrupt number
  1787. * @_di: pointer to the ab8500_fg structure
  1788. *
  1789. * Returns IRQ status(IRQ_HANDLED)
  1790. */
  1791. static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
  1792. {
  1793. struct ab8500_fg *di = _di;
  1794. /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
  1795. if (!di->flags.low_bat_delay) {
  1796. dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
  1797. di->flags.low_bat_delay = true;
  1798. /*
  1799. * Start a timer to check LOW_BAT again after some time
  1800. * This is done to avoid shutdown on single voltage dips
  1801. */
  1802. queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
  1803. round_jiffies(LOW_BAT_CHECK_INTERVAL));
  1804. }
  1805. return IRQ_HANDLED;
  1806. }
  1807. /**
  1808. * ab8500_fg_get_property() - get the fg properties
  1809. * @psy: pointer to the power_supply structure
  1810. * @psp: pointer to the power_supply_property structure
  1811. * @val: pointer to the power_supply_propval union
  1812. *
  1813. * This function gets called when an application tries to get the
  1814. * fg properties by reading the sysfs files.
  1815. * voltage_now: battery voltage
  1816. * current_now: battery instant current
  1817. * current_avg: battery average current
  1818. * charge_full_design: capacity where battery is considered full
  1819. * charge_now: battery capacity in nAh
  1820. * capacity: capacity in percent
  1821. * capacity_level: capacity level
  1822. *
  1823. * Returns error code in case of failure else 0 on success
  1824. */
  1825. static int ab8500_fg_get_property(struct power_supply *psy,
  1826. enum power_supply_property psp,
  1827. union power_supply_propval *val)
  1828. {
  1829. struct ab8500_fg *di;
  1830. di = to_ab8500_fg_device_info(psy);
  1831. /*
  1832. * If battery is identified as unknown and charging of unknown
  1833. * batteries is disabled, we always report 100% capacity and
  1834. * capacity level UNKNOWN, since we can't calculate
  1835. * remaining capacity
  1836. */
  1837. switch (psp) {
  1838. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1839. if (di->flags.bat_ovv)
  1840. val->intval = BATT_OVV_VALUE * 1000;
  1841. else
  1842. val->intval = di->vbat * 1000;
  1843. break;
  1844. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1845. val->intval = di->inst_curr * 1000;
  1846. break;
  1847. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1848. val->intval = di->avg_curr * 1000;
  1849. break;
  1850. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  1851. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1852. di->bat_cap.max_mah_design);
  1853. break;
  1854. case POWER_SUPPLY_PROP_ENERGY_FULL:
  1855. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1856. di->bat_cap.max_mah);
  1857. break;
  1858. case POWER_SUPPLY_PROP_ENERGY_NOW:
  1859. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1860. di->flags.batt_id_received)
  1861. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1862. di->bat_cap.max_mah);
  1863. else
  1864. val->intval = ab8500_fg_convert_mah_to_uwh(di,
  1865. di->bat_cap.prev_mah);
  1866. break;
  1867. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1868. val->intval = di->bat_cap.max_mah_design;
  1869. break;
  1870. case POWER_SUPPLY_PROP_CHARGE_FULL:
  1871. val->intval = di->bat_cap.max_mah;
  1872. break;
  1873. case POWER_SUPPLY_PROP_CHARGE_NOW:
  1874. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1875. di->flags.batt_id_received)
  1876. val->intval = di->bat_cap.max_mah;
  1877. else
  1878. val->intval = di->bat_cap.prev_mah;
  1879. break;
  1880. case POWER_SUPPLY_PROP_CAPACITY:
  1881. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1882. di->flags.batt_id_received)
  1883. val->intval = 100;
  1884. else
  1885. val->intval = di->bat_cap.prev_percent;
  1886. break;
  1887. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  1888. if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
  1889. di->flags.batt_id_received)
  1890. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  1891. else
  1892. val->intval = di->bat_cap.prev_level;
  1893. break;
  1894. default:
  1895. return -EINVAL;
  1896. }
  1897. return 0;
  1898. }
  1899. static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
  1900. {
  1901. struct power_supply *psy;
  1902. struct power_supply *ext;
  1903. struct ab8500_fg *di;
  1904. union power_supply_propval ret;
  1905. int i, j;
  1906. bool psy_found = false;
  1907. psy = (struct power_supply *)data;
  1908. ext = dev_get_drvdata(dev);
  1909. di = to_ab8500_fg_device_info(psy);
  1910. /*
  1911. * For all psy where the name of your driver
  1912. * appears in any supplied_to
  1913. */
  1914. for (i = 0; i < ext->num_supplicants; i++) {
  1915. if (!strcmp(ext->supplied_to[i], psy->name))
  1916. psy_found = true;
  1917. }
  1918. if (!psy_found)
  1919. return 0;
  1920. /* Go through all properties for the psy */
  1921. for (j = 0; j < ext->num_properties; j++) {
  1922. enum power_supply_property prop;
  1923. prop = ext->properties[j];
  1924. if (ext->get_property(ext, prop, &ret))
  1925. continue;
  1926. switch (prop) {
  1927. case POWER_SUPPLY_PROP_STATUS:
  1928. switch (ext->type) {
  1929. case POWER_SUPPLY_TYPE_BATTERY:
  1930. switch (ret.intval) {
  1931. case POWER_SUPPLY_STATUS_UNKNOWN:
  1932. case POWER_SUPPLY_STATUS_DISCHARGING:
  1933. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  1934. if (!di->flags.charging)
  1935. break;
  1936. di->flags.charging = false;
  1937. di->flags.fully_charged = false;
  1938. if (di->bm->capacity_scaling)
  1939. ab8500_fg_update_cap_scalers(di);
  1940. queue_work(di->fg_wq, &di->fg_work);
  1941. break;
  1942. case POWER_SUPPLY_STATUS_FULL:
  1943. if (di->flags.fully_charged)
  1944. break;
  1945. di->flags.fully_charged = true;
  1946. di->flags.force_full = true;
  1947. /* Save current capacity as maximum */
  1948. di->bat_cap.max_mah = di->bat_cap.mah;
  1949. queue_work(di->fg_wq, &di->fg_work);
  1950. break;
  1951. case POWER_SUPPLY_STATUS_CHARGING:
  1952. if (di->flags.charging &&
  1953. !di->flags.fully_charged)
  1954. break;
  1955. di->flags.charging = true;
  1956. di->flags.fully_charged = false;
  1957. if (di->bm->capacity_scaling)
  1958. ab8500_fg_update_cap_scalers(di);
  1959. queue_work(di->fg_wq, &di->fg_work);
  1960. break;
  1961. };
  1962. default:
  1963. break;
  1964. };
  1965. break;
  1966. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1967. switch (ext->type) {
  1968. case POWER_SUPPLY_TYPE_BATTERY:
  1969. if (!di->flags.batt_id_received &&
  1970. di->bm->batt_id != BATTERY_UNKNOWN) {
  1971. const struct abx500_battery_type *b;
  1972. b = &(di->bm->bat_type[di->bm->batt_id]);
  1973. di->flags.batt_id_received = true;
  1974. di->bat_cap.max_mah_design =
  1975. MILLI_TO_MICRO *
  1976. b->charge_full_design;
  1977. di->bat_cap.max_mah =
  1978. di->bat_cap.max_mah_design;
  1979. di->vbat_nom = b->nominal_voltage;
  1980. }
  1981. if (ret.intval)
  1982. di->flags.batt_unknown = false;
  1983. else
  1984. di->flags.batt_unknown = true;
  1985. break;
  1986. default:
  1987. break;
  1988. }
  1989. break;
  1990. case POWER_SUPPLY_PROP_TEMP:
  1991. switch (ext->type) {
  1992. case POWER_SUPPLY_TYPE_BATTERY:
  1993. if (di->flags.batt_id_received)
  1994. di->bat_temp = ret.intval;
  1995. break;
  1996. default:
  1997. break;
  1998. }
  1999. break;
  2000. default:
  2001. break;
  2002. }
  2003. }
  2004. return 0;
  2005. }
  2006. /**
  2007. * ab8500_fg_init_hw_registers() - Set up FG related registers
  2008. * @di: pointer to the ab8500_fg structure
  2009. *
  2010. * Set up battery OVV, low battery voltage registers
  2011. */
  2012. static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
  2013. {
  2014. int ret;
  2015. /* Set VBAT OVV threshold */
  2016. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2017. AB8500_CHARGER,
  2018. AB8500_BATT_OVV,
  2019. BATT_OVV_TH_4P75,
  2020. BATT_OVV_TH_4P75);
  2021. if (ret) {
  2022. dev_err(di->dev, "failed to set BATT_OVV\n");
  2023. goto out;
  2024. }
  2025. /* Enable VBAT OVV detection */
  2026. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2027. AB8500_CHARGER,
  2028. AB8500_BATT_OVV,
  2029. BATT_OVV_ENA,
  2030. BATT_OVV_ENA);
  2031. if (ret) {
  2032. dev_err(di->dev, "failed to enable BATT_OVV\n");
  2033. goto out;
  2034. }
  2035. /* Low Battery Voltage */
  2036. ret = abx500_set_register_interruptible(di->dev,
  2037. AB8500_SYS_CTRL2_BLOCK,
  2038. AB8500_LOW_BAT_REG,
  2039. ab8500_volt_to_regval(
  2040. di->bm->fg_params->lowbat_threshold) << 1 |
  2041. LOW_BAT_ENABLE);
  2042. if (ret) {
  2043. dev_err(di->dev, "%s write failed\n", __func__);
  2044. goto out;
  2045. }
  2046. /* Battery OK threshold */
  2047. ret = ab8500_fg_battok_init_hw_register(di);
  2048. if (ret) {
  2049. dev_err(di->dev, "BattOk init write failed.\n");
  2050. goto out;
  2051. }
  2052. if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
  2053. abx500_get_chip_id(di->dev) >= AB8500_CUT2P0)
  2054. || is_ab8540(di->parent)) {
  2055. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2056. AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time);
  2057. if (ret) {
  2058. dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__);
  2059. goto out;
  2060. };
  2061. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2062. AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time);
  2063. if (ret) {
  2064. dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__);
  2065. goto out;
  2066. };
  2067. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2068. AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart);
  2069. if (ret) {
  2070. dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__);
  2071. goto out;
  2072. };
  2073. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2074. AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time);
  2075. if (ret) {
  2076. dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__);
  2077. goto out;
  2078. };
  2079. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2080. AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable);
  2081. if (ret) {
  2082. dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__);
  2083. goto out;
  2084. };
  2085. }
  2086. out:
  2087. return ret;
  2088. }
  2089. /**
  2090. * ab8500_fg_external_power_changed() - callback for power supply changes
  2091. * @psy: pointer to the structure power_supply
  2092. *
  2093. * This function is the entry point of the pointer external_power_changed
  2094. * of the structure power_supply.
  2095. * This function gets executed when there is a change in any external power
  2096. * supply that this driver needs to be notified of.
  2097. */
  2098. static void ab8500_fg_external_power_changed(struct power_supply *psy)
  2099. {
  2100. struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
  2101. class_for_each_device(power_supply_class, NULL,
  2102. &di->fg_psy, ab8500_fg_get_ext_psy_data);
  2103. }
  2104. /**
  2105. * abab8500_fg_reinit_work() - work to reset the FG algorithm
  2106. * @work: pointer to the work_struct structure
  2107. *
  2108. * Used to reset the current battery capacity to be able to
  2109. * retrigger a new voltage base capacity calculation. For
  2110. * test and verification purpose.
  2111. */
  2112. static void ab8500_fg_reinit_work(struct work_struct *work)
  2113. {
  2114. struct ab8500_fg *di = container_of(work, struct ab8500_fg,
  2115. fg_reinit_work.work);
  2116. if (di->flags.calibrate == false) {
  2117. dev_dbg(di->dev, "Resetting FG state machine to init.\n");
  2118. ab8500_fg_clear_cap_samples(di);
  2119. ab8500_fg_calc_cap_discharge_voltage(di, true);
  2120. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  2121. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
  2122. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2123. } else {
  2124. dev_err(di->dev, "Residual offset calibration ongoing "
  2125. "retrying..\n");
  2126. /* Wait one second until next try*/
  2127. queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
  2128. round_jiffies(1));
  2129. }
  2130. }
  2131. /**
  2132. * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values
  2133. *
  2134. * This function can be used to force the FG algorithm to recalculate a new
  2135. * voltage based battery capacity.
  2136. */
  2137. void ab8500_fg_reinit(void)
  2138. {
  2139. struct ab8500_fg *di = ab8500_fg_get();
  2140. /* User won't be notified if a null pointer returned. */
  2141. if (di != NULL)
  2142. queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
  2143. }
  2144. /* Exposure to the sysfs interface */
  2145. struct ab8500_fg_sysfs_entry {
  2146. struct attribute attr;
  2147. ssize_t (*show)(struct ab8500_fg *, char *);
  2148. ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
  2149. };
  2150. static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
  2151. {
  2152. return sprintf(buf, "%d\n", di->bat_cap.max_mah);
  2153. }
  2154. static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
  2155. size_t count)
  2156. {
  2157. unsigned long charge_full;
  2158. ssize_t ret = -EINVAL;
  2159. ret = strict_strtoul(buf, 10, &charge_full);
  2160. dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
  2161. if (!ret) {
  2162. di->bat_cap.max_mah = (int) charge_full;
  2163. ret = count;
  2164. }
  2165. return ret;
  2166. }
  2167. static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
  2168. {
  2169. return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
  2170. }
  2171. static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
  2172. size_t count)
  2173. {
  2174. unsigned long charge_now;
  2175. ssize_t ret;
  2176. ret = strict_strtoul(buf, 10, &charge_now);
  2177. dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
  2178. ret, charge_now, di->bat_cap.prev_mah);
  2179. if (!ret) {
  2180. di->bat_cap.user_mah = (int) charge_now;
  2181. di->flags.user_cap = true;
  2182. ret = count;
  2183. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2184. }
  2185. return ret;
  2186. }
  2187. static struct ab8500_fg_sysfs_entry charge_full_attr =
  2188. __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
  2189. static struct ab8500_fg_sysfs_entry charge_now_attr =
  2190. __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
  2191. static ssize_t
  2192. ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
  2193. {
  2194. struct ab8500_fg_sysfs_entry *entry;
  2195. struct ab8500_fg *di;
  2196. entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
  2197. di = container_of(kobj, struct ab8500_fg, fg_kobject);
  2198. if (!entry->show)
  2199. return -EIO;
  2200. return entry->show(di, buf);
  2201. }
  2202. static ssize_t
  2203. ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
  2204. size_t count)
  2205. {
  2206. struct ab8500_fg_sysfs_entry *entry;
  2207. struct ab8500_fg *di;
  2208. entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
  2209. di = container_of(kobj, struct ab8500_fg, fg_kobject);
  2210. if (!entry->store)
  2211. return -EIO;
  2212. return entry->store(di, buf, count);
  2213. }
  2214. static const struct sysfs_ops ab8500_fg_sysfs_ops = {
  2215. .show = ab8500_fg_show,
  2216. .store = ab8500_fg_store,
  2217. };
  2218. static struct attribute *ab8500_fg_attrs[] = {
  2219. &charge_full_attr.attr,
  2220. &charge_now_attr.attr,
  2221. NULL,
  2222. };
  2223. static struct kobj_type ab8500_fg_ktype = {
  2224. .sysfs_ops = &ab8500_fg_sysfs_ops,
  2225. .default_attrs = ab8500_fg_attrs,
  2226. };
  2227. /**
  2228. * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
  2229. * @di: pointer to the struct ab8500_chargalg
  2230. *
  2231. * This function removes the entry in sysfs.
  2232. */
  2233. static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
  2234. {
  2235. kobject_del(&di->fg_kobject);
  2236. }
  2237. /**
  2238. * ab8500_chargalg_sysfs_init() - init of sysfs entry
  2239. * @di: pointer to the struct ab8500_chargalg
  2240. *
  2241. * This function adds an entry in sysfs.
  2242. * Returns error code in case of failure else 0(on success)
  2243. */
  2244. static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
  2245. {
  2246. int ret = 0;
  2247. ret = kobject_init_and_add(&di->fg_kobject,
  2248. &ab8500_fg_ktype,
  2249. NULL, "battery");
  2250. if (ret < 0)
  2251. dev_err(di->dev, "failed to create sysfs entry\n");
  2252. return ret;
  2253. }
  2254. static ssize_t ab8505_powercut_flagtime_read(struct device *dev,
  2255. struct device_attribute *attr,
  2256. char *buf)
  2257. {
  2258. int ret;
  2259. u8 reg_value;
  2260. struct power_supply *psy = dev_get_drvdata(dev);
  2261. struct ab8500_fg *di;
  2262. di = to_ab8500_fg_device_info(psy);
  2263. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2264. AB8505_RTC_PCUT_FLAG_TIME_REG, &reg_value);
  2265. if (ret < 0) {
  2266. dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
  2267. goto fail;
  2268. }
  2269. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
  2270. fail:
  2271. return ret;
  2272. }
  2273. static ssize_t ab8505_powercut_flagtime_write(struct device *dev,
  2274. struct device_attribute *attr,
  2275. const char *buf, size_t count)
  2276. {
  2277. int ret;
  2278. long unsigned reg_value;
  2279. struct power_supply *psy = dev_get_drvdata(dev);
  2280. struct ab8500_fg *di;
  2281. di = to_ab8500_fg_device_info(psy);
  2282. reg_value = simple_strtoul(buf, NULL, 10);
  2283. if (reg_value > 0x7F) {
  2284. dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
  2285. goto fail;
  2286. }
  2287. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2288. AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value);
  2289. if (ret < 0)
  2290. dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
  2291. fail:
  2292. return count;
  2293. }
  2294. static ssize_t ab8505_powercut_maxtime_read(struct device *dev,
  2295. struct device_attribute *attr,
  2296. char *buf)
  2297. {
  2298. int ret;
  2299. u8 reg_value;
  2300. struct power_supply *psy = dev_get_drvdata(dev);
  2301. struct ab8500_fg *di;
  2302. di = to_ab8500_fg_device_info(psy);
  2303. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2304. AB8505_RTC_PCUT_MAX_TIME_REG, &reg_value);
  2305. if (ret < 0) {
  2306. dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
  2307. goto fail;
  2308. }
  2309. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
  2310. fail:
  2311. return ret;
  2312. }
  2313. static ssize_t ab8505_powercut_maxtime_write(struct device *dev,
  2314. struct device_attribute *attr,
  2315. const char *buf, size_t count)
  2316. {
  2317. int ret;
  2318. int reg_value;
  2319. struct power_supply *psy = dev_get_drvdata(dev);
  2320. struct ab8500_fg *di;
  2321. di = to_ab8500_fg_device_info(psy);
  2322. reg_value = simple_strtoul(buf, NULL, 10);
  2323. if (reg_value > 0x7F) {
  2324. dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
  2325. goto fail;
  2326. }
  2327. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2328. AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value);
  2329. if (ret < 0)
  2330. dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
  2331. fail:
  2332. return count;
  2333. }
  2334. static ssize_t ab8505_powercut_restart_read(struct device *dev,
  2335. struct device_attribute *attr,
  2336. char *buf)
  2337. {
  2338. int ret;
  2339. u8 reg_value;
  2340. struct power_supply *psy = dev_get_drvdata(dev);
  2341. struct ab8500_fg *di;
  2342. di = to_ab8500_fg_device_info(psy);
  2343. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2344. AB8505_RTC_PCUT_RESTART_REG, &reg_value);
  2345. if (ret < 0) {
  2346. dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
  2347. goto fail;
  2348. }
  2349. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF));
  2350. fail:
  2351. return ret;
  2352. }
  2353. static ssize_t ab8505_powercut_restart_write(struct device *dev,
  2354. struct device_attribute *attr,
  2355. const char *buf, size_t count)
  2356. {
  2357. int ret;
  2358. int reg_value;
  2359. struct power_supply *psy = dev_get_drvdata(dev);
  2360. struct ab8500_fg *di;
  2361. di = to_ab8500_fg_device_info(psy);
  2362. reg_value = simple_strtoul(buf, NULL, 10);
  2363. if (reg_value > 0xF) {
  2364. dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n");
  2365. goto fail;
  2366. }
  2367. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2368. AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value);
  2369. if (ret < 0)
  2370. dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
  2371. fail:
  2372. return count;
  2373. }
  2374. static ssize_t ab8505_powercut_timer_read(struct device *dev,
  2375. struct device_attribute *attr,
  2376. char *buf)
  2377. {
  2378. int ret;
  2379. u8 reg_value;
  2380. struct power_supply *psy = dev_get_drvdata(dev);
  2381. struct ab8500_fg *di;
  2382. di = to_ab8500_fg_device_info(psy);
  2383. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2384. AB8505_RTC_PCUT_TIME_REG, &reg_value);
  2385. if (ret < 0) {
  2386. dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
  2387. goto fail;
  2388. }
  2389. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
  2390. fail:
  2391. return ret;
  2392. }
  2393. static ssize_t ab8505_powercut_restart_counter_read(struct device *dev,
  2394. struct device_attribute *attr,
  2395. char *buf)
  2396. {
  2397. int ret;
  2398. u8 reg_value;
  2399. struct power_supply *psy = dev_get_drvdata(dev);
  2400. struct ab8500_fg *di;
  2401. di = to_ab8500_fg_device_info(psy);
  2402. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2403. AB8505_RTC_PCUT_RESTART_REG, &reg_value);
  2404. if (ret < 0) {
  2405. dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
  2406. goto fail;
  2407. }
  2408. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF0) >> 4);
  2409. fail:
  2410. return ret;
  2411. }
  2412. static ssize_t ab8505_powercut_read(struct device *dev,
  2413. struct device_attribute *attr,
  2414. char *buf)
  2415. {
  2416. int ret;
  2417. u8 reg_value;
  2418. struct power_supply *psy = dev_get_drvdata(dev);
  2419. struct ab8500_fg *di;
  2420. di = to_ab8500_fg_device_info(psy);
  2421. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2422. AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
  2423. if (ret < 0)
  2424. goto fail;
  2425. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x1));
  2426. fail:
  2427. return ret;
  2428. }
  2429. static ssize_t ab8505_powercut_write(struct device *dev,
  2430. struct device_attribute *attr,
  2431. const char *buf, size_t count)
  2432. {
  2433. int ret;
  2434. int reg_value;
  2435. struct power_supply *psy = dev_get_drvdata(dev);
  2436. struct ab8500_fg *di;
  2437. di = to_ab8500_fg_device_info(psy);
  2438. reg_value = simple_strtoul(buf, NULL, 10);
  2439. if (reg_value > 0x1) {
  2440. dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
  2441. goto fail;
  2442. }
  2443. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2444. AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value);
  2445. if (ret < 0)
  2446. dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
  2447. fail:
  2448. return count;
  2449. }
  2450. static ssize_t ab8505_powercut_flag_read(struct device *dev,
  2451. struct device_attribute *attr,
  2452. char *buf)
  2453. {
  2454. int ret;
  2455. u8 reg_value;
  2456. struct power_supply *psy = dev_get_drvdata(dev);
  2457. struct ab8500_fg *di;
  2458. di = to_ab8500_fg_device_info(psy);
  2459. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2460. AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
  2461. if (ret < 0) {
  2462. dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
  2463. goto fail;
  2464. }
  2465. return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x10) >> 4));
  2466. fail:
  2467. return ret;
  2468. }
  2469. static ssize_t ab8505_powercut_debounce_read(struct device *dev,
  2470. struct device_attribute *attr,
  2471. char *buf)
  2472. {
  2473. int ret;
  2474. u8 reg_value;
  2475. struct power_supply *psy = dev_get_drvdata(dev);
  2476. struct ab8500_fg *di;
  2477. di = to_ab8500_fg_device_info(psy);
  2478. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2479. AB8505_RTC_PCUT_DEBOUNCE_REG, &reg_value);
  2480. if (ret < 0) {
  2481. dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
  2482. goto fail;
  2483. }
  2484. return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7));
  2485. fail:
  2486. return ret;
  2487. }
  2488. static ssize_t ab8505_powercut_debounce_write(struct device *dev,
  2489. struct device_attribute *attr,
  2490. const char *buf, size_t count)
  2491. {
  2492. int ret;
  2493. int reg_value;
  2494. struct power_supply *psy = dev_get_drvdata(dev);
  2495. struct ab8500_fg *di;
  2496. di = to_ab8500_fg_device_info(psy);
  2497. reg_value = simple_strtoul(buf, NULL, 10);
  2498. if (reg_value > 0x7) {
  2499. dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
  2500. goto fail;
  2501. }
  2502. ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
  2503. AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value);
  2504. if (ret < 0)
  2505. dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
  2506. fail:
  2507. return count;
  2508. }
  2509. static ssize_t ab8505_powercut_enable_status_read(struct device *dev,
  2510. struct device_attribute *attr,
  2511. char *buf)
  2512. {
  2513. int ret;
  2514. u8 reg_value;
  2515. struct power_supply *psy = dev_get_drvdata(dev);
  2516. struct ab8500_fg *di;
  2517. di = to_ab8500_fg_device_info(psy);
  2518. ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
  2519. AB8505_RTC_PCUT_CTL_STATUS_REG, &reg_value);
  2520. if (ret < 0) {
  2521. dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
  2522. goto fail;
  2523. }
  2524. return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x20) >> 5));
  2525. fail:
  2526. return ret;
  2527. }
  2528. static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = {
  2529. __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP),
  2530. ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write),
  2531. __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP),
  2532. ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write),
  2533. __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP),
  2534. ab8505_powercut_restart_read, ab8505_powercut_restart_write),
  2535. __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL),
  2536. __ATTR(powercut_restart_counter, S_IRUGO,
  2537. ab8505_powercut_restart_counter_read, NULL),
  2538. __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
  2539. ab8505_powercut_read, ab8505_powercut_write),
  2540. __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL),
  2541. __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP),
  2542. ab8505_powercut_debounce_read, ab8505_powercut_debounce_write),
  2543. __ATTR(powercut_enable_status, S_IRUGO,
  2544. ab8505_powercut_enable_status_read, NULL),
  2545. };
  2546. static int ab8500_fg_sysfs_psy_create_attrs(struct device *dev)
  2547. {
  2548. unsigned int i, j;
  2549. struct power_supply *psy = dev_get_drvdata(dev);
  2550. struct ab8500_fg *di;
  2551. di = to_ab8500_fg_device_info(psy);
  2552. if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
  2553. abx500_get_chip_id(dev->parent) >= AB8500_CUT2P0)
  2554. || is_ab8540(di->parent)) {
  2555. for (j = 0; j < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); j++)
  2556. if (device_create_file(dev, &ab8505_fg_sysfs_psy_attrs[j]))
  2557. goto sysfs_psy_create_attrs_failed_ab8505;
  2558. }
  2559. return 0;
  2560. sysfs_psy_create_attrs_failed_ab8505:
  2561. dev_err(dev, "Failed creating sysfs psy attrs for ab8505.\n");
  2562. while (j--)
  2563. device_remove_file(dev, &ab8505_fg_sysfs_psy_attrs[i]);
  2564. return -EIO;
  2565. }
  2566. static void ab8500_fg_sysfs_psy_remove_attrs(struct device *dev)
  2567. {
  2568. unsigned int i;
  2569. struct power_supply *psy = dev_get_drvdata(dev);
  2570. struct ab8500_fg *di;
  2571. di = to_ab8500_fg_device_info(psy);
  2572. if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
  2573. abx500_get_chip_id(dev->parent) >= AB8500_CUT2P0)
  2574. || is_ab8540(di->parent)) {
  2575. for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
  2576. (void)device_remove_file(dev, &ab8505_fg_sysfs_psy_attrs[i]);
  2577. }
  2578. }
  2579. /* Exposure to the sysfs interface <<END>> */
  2580. #if defined(CONFIG_PM)
  2581. static int ab8500_fg_resume(struct platform_device *pdev)
  2582. {
  2583. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2584. /*
  2585. * Change state if we're not charging. If we're charging we will wake
  2586. * up on the FG IRQ
  2587. */
  2588. if (!di->flags.charging) {
  2589. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
  2590. queue_work(di->fg_wq, &di->fg_work);
  2591. }
  2592. return 0;
  2593. }
  2594. static int ab8500_fg_suspend(struct platform_device *pdev,
  2595. pm_message_t state)
  2596. {
  2597. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2598. flush_delayed_work(&di->fg_periodic_work);
  2599. flush_work(&di->fg_work);
  2600. flush_work(&di->fg_acc_cur_work);
  2601. flush_delayed_work(&di->fg_reinit_work);
  2602. flush_delayed_work(&di->fg_low_bat_work);
  2603. flush_delayed_work(&di->fg_check_hw_failure_work);
  2604. /*
  2605. * If the FG is enabled we will disable it before going to suspend
  2606. * only if we're not charging
  2607. */
  2608. if (di->flags.fg_enabled && !di->flags.charging)
  2609. ab8500_fg_coulomb_counter(di, false);
  2610. return 0;
  2611. }
  2612. #else
  2613. #define ab8500_fg_suspend NULL
  2614. #define ab8500_fg_resume NULL
  2615. #endif
  2616. static int ab8500_fg_remove(struct platform_device *pdev)
  2617. {
  2618. int ret = 0;
  2619. struct ab8500_fg *di = platform_get_drvdata(pdev);
  2620. list_del(&di->node);
  2621. /* Disable coulomb counter */
  2622. ret = ab8500_fg_coulomb_counter(di, false);
  2623. if (ret)
  2624. dev_err(di->dev, "failed to disable coulomb counter\n");
  2625. destroy_workqueue(di->fg_wq);
  2626. ab8500_fg_sysfs_exit(di);
  2627. flush_scheduled_work();
  2628. ab8500_fg_sysfs_psy_remove_attrs(di->fg_psy.dev);
  2629. power_supply_unregister(&di->fg_psy);
  2630. platform_set_drvdata(pdev, NULL);
  2631. return ret;
  2632. }
  2633. /* ab8500 fg driver interrupts and their respective isr */
  2634. static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
  2635. {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
  2636. {"BATT_OVV", ab8500_fg_batt_ovv_handler},
  2637. {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
  2638. {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
  2639. {"CCEOC", ab8500_fg_cc_data_end_handler},
  2640. };
  2641. static char *supply_interface[] = {
  2642. "ab8500_chargalg",
  2643. "ab8500_usb",
  2644. };
  2645. static int ab8500_fg_probe(struct platform_device *pdev)
  2646. {
  2647. struct device_node *np = pdev->dev.of_node;
  2648. struct abx500_bm_data *plat = pdev->dev.platform_data;
  2649. struct ab8500_fg *di;
  2650. int i, irq;
  2651. int ret = 0;
  2652. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  2653. if (!di) {
  2654. dev_err(&pdev->dev, "%s no mem for ab8500_fg\n", __func__);
  2655. return -ENOMEM;
  2656. }
  2657. if (!plat) {
  2658. dev_err(&pdev->dev, "no battery management data supplied\n");
  2659. return -EINVAL;
  2660. }
  2661. di->bm = plat;
  2662. if (np) {
  2663. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  2664. if (ret) {
  2665. dev_err(&pdev->dev, "failed to get battery information\n");
  2666. return ret;
  2667. }
  2668. }
  2669. mutex_init(&di->cc_lock);
  2670. /* get parent data */
  2671. di->dev = &pdev->dev;
  2672. di->parent = dev_get_drvdata(pdev->dev.parent);
  2673. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  2674. di->fg_psy.name = "ab8500_fg";
  2675. di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  2676. di->fg_psy.properties = ab8500_fg_props;
  2677. di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
  2678. di->fg_psy.get_property = ab8500_fg_get_property;
  2679. di->fg_psy.supplied_to = supply_interface;
  2680. di->fg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
  2681. di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
  2682. di->bat_cap.max_mah_design = MILLI_TO_MICRO *
  2683. di->bm->bat_type[di->bm->batt_id].charge_full_design;
  2684. di->bat_cap.max_mah = di->bat_cap.max_mah_design;
  2685. di->vbat_nom = di->bm->bat_type[di->bm->batt_id].nominal_voltage;
  2686. di->init_capacity = true;
  2687. ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
  2688. ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
  2689. /* Create a work queue for running the FG algorithm */
  2690. di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
  2691. if (di->fg_wq == NULL) {
  2692. dev_err(di->dev, "failed to create work queue\n");
  2693. return -ENOMEM;
  2694. }
  2695. /* Init work for running the fg algorithm instantly */
  2696. INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
  2697. /* Init work for getting the battery accumulated current */
  2698. INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
  2699. /* Init work for reinitialising the fg algorithm */
  2700. INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
  2701. ab8500_fg_reinit_work);
  2702. /* Work delayed Queue to run the state machine */
  2703. INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
  2704. ab8500_fg_periodic_work);
  2705. /* Work to check low battery condition */
  2706. INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
  2707. ab8500_fg_low_bat_work);
  2708. /* Init work for HW failure check */
  2709. INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
  2710. ab8500_fg_check_hw_failure_work);
  2711. /* Reset battery low voltage flag */
  2712. di->flags.low_bat = false;
  2713. /* Initialize low battery counter */
  2714. di->low_bat_cnt = 10;
  2715. /* Initialize OVV, and other registers */
  2716. ret = ab8500_fg_init_hw_registers(di);
  2717. if (ret) {
  2718. dev_err(di->dev, "failed to initialize registers\n");
  2719. goto free_inst_curr_wq;
  2720. }
  2721. /* Consider battery unknown until we're informed otherwise */
  2722. di->flags.batt_unknown = true;
  2723. di->flags.batt_id_received = false;
  2724. /* Register FG power supply class */
  2725. ret = power_supply_register(di->dev, &di->fg_psy);
  2726. if (ret) {
  2727. dev_err(di->dev, "failed to register FG psy\n");
  2728. goto free_inst_curr_wq;
  2729. }
  2730. di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
  2731. ab8500_fg_coulomb_counter(di, true);
  2732. /*
  2733. * Initialize completion used to notify completion and start
  2734. * of inst current
  2735. */
  2736. init_completion(&di->ab8500_fg_started);
  2737. init_completion(&di->ab8500_fg_complete);
  2738. /* Register interrupts */
  2739. for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
  2740. irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
  2741. ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
  2742. IRQF_SHARED | IRQF_NO_SUSPEND,
  2743. ab8500_fg_irq[i].name, di);
  2744. if (ret != 0) {
  2745. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  2746. , ab8500_fg_irq[i].name, irq, ret);
  2747. goto free_irq;
  2748. }
  2749. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  2750. ab8500_fg_irq[i].name, irq, ret);
  2751. }
  2752. di->irq = platform_get_irq_byname(pdev, "CCEOC");
  2753. disable_irq(di->irq);
  2754. di->nbr_cceoc_irq_cnt = 0;
  2755. platform_set_drvdata(pdev, di);
  2756. ret = ab8500_fg_sysfs_init(di);
  2757. if (ret) {
  2758. dev_err(di->dev, "failed to create sysfs entry\n");
  2759. goto free_irq;
  2760. }
  2761. ret = ab8500_fg_sysfs_psy_create_attrs(di->fg_psy.dev);
  2762. if (ret) {
  2763. dev_err(di->dev, "failed to create FG psy\n");
  2764. ab8500_fg_sysfs_exit(di);
  2765. goto free_irq;
  2766. }
  2767. /* Calibrate the fg first time */
  2768. di->flags.calibrate = true;
  2769. di->calib_state = AB8500_FG_CALIB_INIT;
  2770. /* Use room temp as default value until we get an update from driver. */
  2771. di->bat_temp = 210;
  2772. /* Run the FG algorithm */
  2773. queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
  2774. list_add_tail(&di->node, &ab8500_fg_list);
  2775. return ret;
  2776. free_irq:
  2777. power_supply_unregister(&di->fg_psy);
  2778. /* We also have to free all successfully registered irqs */
  2779. for (i = i - 1; i >= 0; i--) {
  2780. irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
  2781. free_irq(irq, di);
  2782. }
  2783. free_inst_curr_wq:
  2784. destroy_workqueue(di->fg_wq);
  2785. return ret;
  2786. }
  2787. static const struct of_device_id ab8500_fg_match[] = {
  2788. { .compatible = "stericsson,ab8500-fg", },
  2789. { },
  2790. };
  2791. static struct platform_driver ab8500_fg_driver = {
  2792. .probe = ab8500_fg_probe,
  2793. .remove = ab8500_fg_remove,
  2794. .suspend = ab8500_fg_suspend,
  2795. .resume = ab8500_fg_resume,
  2796. .driver = {
  2797. .name = "ab8500-fg",
  2798. .owner = THIS_MODULE,
  2799. .of_match_table = ab8500_fg_match,
  2800. },
  2801. };
  2802. static int __init ab8500_fg_init(void)
  2803. {
  2804. return platform_driver_register(&ab8500_fg_driver);
  2805. }
  2806. static void __exit ab8500_fg_exit(void)
  2807. {
  2808. platform_driver_unregister(&ab8500_fg_driver);
  2809. }
  2810. subsys_initcall_sync(ab8500_fg_init);
  2811. module_exit(ab8500_fg_exit);
  2812. MODULE_LICENSE("GPL v2");
  2813. MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
  2814. MODULE_ALIAS("platform:ab8500-fg");
  2815. MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");