ab8500_fg.c 67 KB

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