ab8500_fg.c 71 KB

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