ab8500_fg.c 84 KB

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