ab8500_fg.c 83 KB

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