ab8500_fg.c 84 KB

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