ab8500_fg.c 67 KB

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