ab8500_fg.c 84 KB

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