max77823_fuelgauge.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. /*
  2. * max77823_fuelgauge.c
  3. * Samsung MAX77823 Fuel Gauge Driver
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define DEBUG
  13. #include <linux/mfd/max77823.h>
  14. #include <linux/mfd/max77823-private.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. static enum power_supply_property max77823_fuelgauge_props[] = {
  19. POWER_SUPPLY_PROP_STATUS,
  20. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  21. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  22. POWER_SUPPLY_PROP_CURRENT_NOW,
  23. POWER_SUPPLY_PROP_CURRENT_AVG,
  24. POWER_SUPPLY_PROP_CHARGE_FULL,
  25. POWER_SUPPLY_PROP_ENERGY_NOW,
  26. POWER_SUPPLY_PROP_CAPACITY,
  27. POWER_SUPPLY_PROP_TEMP,
  28. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  29. };
  30. #ifdef CONFIG_FUELGAUGE_MAX77823_VOLTAGE_TRACKING
  31. static void max77823_init_regs(struct max77823_fuelgauge_data *fuelgauge)
  32. {
  33. u8 data[2];
  34. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_FILTERCFG, data) < 0)
  35. return;
  36. /* Clear average vcell (12 sec) */
  37. data[0] &= 0x8f;
  38. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_FILTERCFG, data);
  39. }
  40. static void max77823_get_version(struct max77823_fuelgauge_data *fuelgauge)
  41. {
  42. u8 data[2];
  43. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_VERSION, data) < 0)
  44. return;
  45. pr_debug("MAX77823 Fuel-Gauge Ver %d%d\n",
  46. data[0], data[1]);
  47. }
  48. static void max77823_alert_init(struct max77823_fuelgauge_data *fuelgauge)
  49. {
  50. u8 data[2];
  51. /* SALRT Threshold setting */
  52. data[0] = fuelgauge->pdata->fuel_alert_soc;
  53. data[1] = 0xff;
  54. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_SALRT_TH, data);
  55. /* VALRT Threshold setting */
  56. data[0] = 0x00;
  57. data[1] = 0xff;
  58. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_VALRT_TH, data);
  59. /* TALRT Threshold setting */
  60. data[0] = 0x80;
  61. data[1] = 0x7f;
  62. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_TALRT_TH, data);
  63. }
  64. static bool max77823_check_status(struct max77823_fuelgauge_data *fuelgauge)
  65. {
  66. u8 data[2];
  67. bool ret = false;
  68. /* check if Smn was generated */
  69. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_STATUS, data) < 0)
  70. return ret;
  71. pr_info("%s: status_reg(%02x%02x)\n",
  72. __func__, data[1], data[0]);
  73. /* minimum SOC threshold exceeded. */
  74. if (data[1] & (0x1 << 2))
  75. ret = true;
  76. /* clear status reg */
  77. if (!ret) {
  78. data[1] = 0;
  79. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_STATUS, data);
  80. msleep(200);
  81. }
  82. return ret;
  83. }
  84. static int max77823_set_temperature(struct max77823_fuelgauge_data *fuelgauge,
  85. int temperature)
  86. {
  87. u8 data[2];
  88. data[0] = 0;
  89. data[1] = temperature;
  90. max77823_write_reg(fuelgauge->i2c, MAX77823_REG_TEMPERATURE, data);
  91. pr_debug("%s: temperature to (%d)\n", __func__, temperature);
  92. return temperature;
  93. }
  94. static int max77823_get_temperature(struct max77823_fuelgauge_data *fuelgauge)
  95. {
  96. u8 data[2];
  97. s32 temperature = 0;
  98. if (max77823_read_reg(fuelgauge->i2c,
  99. MAX77823_REG_TEMPERATURE, data) < 0)
  100. return -ERANGE;
  101. /* data[] store 2's compliment format number */
  102. if (data[1] & (0x1 << 7)) {
  103. /* Negative */
  104. temperature = ((~(data[1])) & 0xFF) + 1;
  105. temperature *= (-1000);
  106. } else {
  107. temperature = data[1] & 0x7F;
  108. temperature *= 1000;
  109. temperature += data[0] * 39 / 10;
  110. }
  111. pr_debug("%s: temperature (%d)\n", __func__, temperature);
  112. return temperature;
  113. }
  114. /* soc should be 0.01% unit */
  115. static int max77823_get_soc(struct max77823_fuelgauge_data *fuelgauge)
  116. {
  117. u8 data[2];
  118. int soc;
  119. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_SOC_VF, data) < 0)
  120. return -EINVAL;
  121. soc = ((data[1] * 100) + (data[0] * 100 / 256));
  122. pr_debug("%s: raw capacity (%d)\n", __func__, soc);
  123. return min(soc, 10000);
  124. }
  125. static int max77823_get_vfocv(struct max77823_fuelgauge_data *fuelgauge)
  126. {
  127. u8 data[2];
  128. u32 vfocv = 0;
  129. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_VFOCV, data) < 0)
  130. return -EINVAL;
  131. vfocv = ((data[0] >> 3) + (data[1] << 5)) * 625 / 1000;
  132. pr_debug("%s: vfocv (%d)\n", __func__, vfocv);
  133. return vfocv;
  134. }
  135. static int max77823_get_vcell(struct max77823_fuelgauge_data *fuelgauge)
  136. {
  137. u8 data[2];
  138. u32 vcell = 0;
  139. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_VCELL, data) < 0)
  140. return -EINVAL;
  141. vcell = ((data[0] >> 3) + (data[1] << 5)) * 625 / 1000;
  142. pr_debug("%s: vcell (%d)\n", __func__, vcell);
  143. return vcell;
  144. }
  145. static int max77823_get_avgvcell(struct max77823_fuelgauge_data *fuelgauge)
  146. {
  147. u8 data[2];
  148. u32 avgvcell = 0;
  149. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_AVGVCELL, data) < 0)
  150. return -EINVAL;
  151. avgvcell = ((data[0] >> 3) + (data[1] << 5)) * 625 / 1000;
  152. pr_debug("%s: avgvcell (%d)\n", __func__, avgvcell);
  153. return avgvcell;
  154. }
  155. bool max77823_fg_init(struct max77823_fuelgauge_data *fuelgauge)
  156. {
  157. /* initialize fuel gauge registers */
  158. max77823_init_regs(fuelgauge);
  159. max77823_get_version(fuelgauge);
  160. return true;
  161. }
  162. bool max77823_fg_fuelalert_init(struct max77823_fuelgauge_data *fuelgauge,
  163. int soc)
  164. {
  165. u8 data[2];
  166. /* 1. Set max77823 alert configuration. */
  167. max77823_alert_init(fuelgauge);
  168. if (max77823_read_reg(fuelgauge->i2c, MAX77823_REG_CONFIG, data)
  169. < 0)
  170. return -1;
  171. /*Enable Alert (Aen = 1) */
  172. data[0] |= (0x1 << 2);
  173. max77823_write_reg(fuelgauge, MAX77823_REG_CONFIG, data);
  174. pr_debug("%s: config_reg(%02x%02x) irq(%d)\n",
  175. __func__, data[1], data[0], fuelgauge->pdata->fg_irq);
  176. return true;
  177. }
  178. bool max77823_fg_is_fuelalerted(struct max77823_fuelgauge_data *fuelgauge)
  179. {
  180. return max77823_check_status(fuelgauge);
  181. }
  182. bool max77823_fg_fuelalert_process(void *irq_data, bool is_fuel_alerted)
  183. {
  184. struct max77823_fuelgauge_data *fuelgauge = irq_data;
  185. u8 data[2];
  186. /* update SOC */
  187. /* max77823_get_soc(fuelgauge); */
  188. if (is_fuel_alerted) {
  189. if (max77823_read_reg(fuelgauge->i2c,
  190. MAX77823_REG_CONFIG, data) < 0)
  191. return false;
  192. data[1] |= (0x1 << 3);
  193. max77823_write_reg(fuelgauge->i2c,
  194. MAX77823_REG_CONFIG, data);
  195. pr_info("%s: Fuel-alert Alerted!! (%02x%02x)\n",
  196. __func__, data[1], data[0]);
  197. } else {
  198. if (max77823_read_reg(fuelgauge->i2c,
  199. MAX77823_REG_CONFIG, data)
  200. < 0)
  201. return false;
  202. data[1] &= (~(0x1 << 3));
  203. max77823_write_reg(fuelgauge->i2c,
  204. MAX77823_REG_CONFIG, data);
  205. pr_info("%s: Fuel-alert Released!! (%02x%02x)\n",
  206. __func__, data[1], data[0]);
  207. }
  208. max77823_read_reg(fuelgauge->i2c, MAX77823_REG_VCELL, data);
  209. pr_debug("%s: MAX77823_REG_VCELL(%02x%02x)\n",
  210. __func__, data[1], data[0]);
  211. max77823_read_reg(fuelgauge->i2c, MAX77823_REG_TEMPERATURE, data);
  212. pr_debug("%s: MAX77823_REG_TEMPERATURE(%02x%02x)\n",
  213. __func__, data[1], data[0]);
  214. max77823_read_reg(fuelgauge->i2c, MAX77823_REG_CONFIG, data);
  215. pr_debug("%s: MAX77823_REG_CONFIG(%02x%02x)\n",
  216. __func__, data[1], data[0]);
  217. max77823_read_reg(fuelgauge->i2c, MAX77823_REG_VFOCV, data);
  218. pr_debug("%s: MAX77823_REG_VFOCV(%02x%02x)\n",
  219. __func__, data[1], data[0]);
  220. max77823_read_reg(fuelgauge->i2c, MAX77823_REG_SOC_VF, data);
  221. pr_debug("%s: MAX77823_REG_SOC_VF(%02x%02x)\n",
  222. __func__, data[1], data[0]);
  223. pr_debug("%s: FUEL GAUGE IRQ (%d)\n",
  224. __func__,
  225. gpio_get_value(fuelgauge->pdata->fg_irq));
  226. return true;
  227. }
  228. bool max77823_fg_full_charged(struct max77823_fuelgauge_data *fuelgauge)
  229. {
  230. return true;
  231. }
  232. #endif
  233. #ifdef CONFIG_FUELGAUGE_MAX77823_COULOMB_COUNTING
  234. static void fg_test_print(struct max77823_fuelgauge_data *fuelgauge)
  235. {
  236. u8 data[2];
  237. u32 average_vcell;
  238. u16 w_data;
  239. u32 temp;
  240. u32 temp2;
  241. u16 reg_data;
  242. if (max77823_bulk_write(fuelgauge->i2c,
  243. AVR_VCELL_REG, 2, data) < 0) {
  244. pr_err("%s: Failed to read VCELL\n", __func__);
  245. return;
  246. }
  247. w_data = (data[1]<<8) | data[0];
  248. temp = (w_data & 0xFFF) * 78125;
  249. average_vcell = temp / 1000000;
  250. temp = ((w_data & 0xF000) >> 4) * 78125;
  251. temp2 = temp / 1000000;
  252. average_vcell += (temp2 << 4);
  253. pr_info("%s: AVG_VCELL(%d), data(0x%04x)\n", __func__,
  254. average_vcell, (data[1]<<8) | data[0]);
  255. reg_data = max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  256. pr_info("%s: FULLCAP(%d), data(0x%04x)\n", __func__,
  257. reg_data/2, reg_data);
  258. reg_data = max77823_read_word(fuelgauge->i2c, REMCAP_REP_REG);
  259. pr_info("%s: REMCAP_REP(%d), data(0x%04x)\n", __func__,
  260. reg_data/2, reg_data);
  261. reg_data = max77823_read_word(fuelgauge->i2c, REMCAP_MIX_REG);
  262. pr_info("%s: REMCAP_MIX(%d), data(0x%04x)\n", __func__,
  263. reg_data/2, reg_data);
  264. reg_data = max77823_read_word(fuelgauge->i2c, REMCAP_AV_REG);
  265. pr_info("%s: REMCAP_AV(%d), data(0x%04x)\n", __func__,
  266. reg_data/2, reg_data);
  267. }
  268. static void fg_periodic_read(struct max77823_fuelgauge_data *fuelgauge)
  269. {
  270. u8 reg;
  271. int i;
  272. int data[0x10];
  273. char *str = NULL;
  274. str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
  275. if (!str)
  276. return;
  277. for (i = 0; i < 16; i++) {
  278. for (reg = 0; reg < 0x10; reg++)
  279. data[reg] = max77823_read_word(fuelgauge->i2c, reg + i * 0x10);
  280. sprintf(str+strlen(str),
  281. "%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,",
  282. data[0x00], data[0x01], data[0x02], data[0x03],
  283. data[0x04], data[0x05], data[0x06], data[0x07]);
  284. sprintf(str+strlen(str),
  285. "%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,",
  286. data[0x08], data[0x09], data[0x0a], data[0x0b],
  287. data[0x0c], data[0x0d], data[0x0e], data[0x0f]);
  288. if (i == 4)
  289. i = 13;
  290. }
  291. pr_info("%s", str);
  292. kfree(str);
  293. }
  294. static int fg_read_vcell(struct max77823_fuelgauge_data *fuelgauge)
  295. {
  296. u8 data[2];
  297. u32 vcell;
  298. u16 w_data;
  299. u32 temp;
  300. u32 temp2;
  301. if (max77823_bulk_read(fuelgauge->i2c, VCELL_REG, 2, data) < 0) {
  302. pr_err("%s: Failed to read VCELL\n", __func__);
  303. return -1;
  304. }
  305. w_data = (data[1]<<8) | data[0];
  306. temp = (w_data & 0xFFF) * 78125;
  307. vcell = temp / 1000000;
  308. temp = ((w_data & 0xF000) >> 4) * 78125;
  309. temp2 = temp / 1000000;
  310. vcell += (temp2 << 4);
  311. if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
  312. pr_info("%s: VCELL(%d), data(0x%04x)\n",
  313. __func__, vcell, (data[1]<<8) | data[0]);
  314. return vcell;
  315. }
  316. static int fg_read_vfocv(struct max77823_fuelgauge_data *fuelgauge)
  317. {
  318. u8 data[2];
  319. u32 vfocv = 0;
  320. u16 w_data;
  321. u32 temp;
  322. u32 temp2;
  323. if (max77823_bulk_read(fuelgauge->i2c, VFOCV_REG, 2, data) < 0) {
  324. pr_err("%s: Failed to read VFOCV\n", __func__);
  325. return -1;
  326. }
  327. w_data = (data[1]<<8) | data[0];
  328. temp = (w_data & 0xFFF) * 78125;
  329. vfocv = temp / 1000000;
  330. temp = ((w_data & 0xF000) >> 4) * 78125;
  331. temp2 = temp / 1000000;
  332. vfocv += (temp2 << 4);
  333. return vfocv;
  334. }
  335. static int fg_read_avg_vcell(struct max77823_fuelgauge_data *fuelgauge)
  336. {
  337. u8 data[2];
  338. u32 avg_vcell = 0;
  339. u16 w_data;
  340. u32 temp;
  341. u32 temp2;
  342. if (max77823_bulk_read(fuelgauge->i2c, AVR_VCELL_REG,
  343. 2, data) < 0) {
  344. pr_err("%s: Failed to read AVG_VCELL\n", __func__);
  345. return -1;
  346. }
  347. w_data = (data[1]<<8) | data[0];
  348. temp = (w_data & 0xFFF) * 78125;
  349. avg_vcell = temp / 1000000;
  350. temp = ((w_data & 0xF000) >> 4) * 78125;
  351. temp2 = temp / 1000000;
  352. avg_vcell += (temp2 << 4);
  353. return avg_vcell;
  354. }
  355. static int fg_check_battery_present(struct max77823_fuelgauge_data *fuelgauge)
  356. {
  357. u8 status_data[2];
  358. int ret = 1;
  359. /* 1. Check Bst bit */
  360. if (max77823_bulk_read(fuelgauge->i2c, STATUS_REG,
  361. 2, status_data) < 0) {
  362. pr_err("%s: Failed to read STATUS_REG\n", __func__);
  363. return 0;
  364. }
  365. if (status_data[0] & (0x1 << 3)) {
  366. pr_info("%s: addr(0x01), data(0x%04x)\n", __func__,
  367. (status_data[1]<<8) | status_data[0]);
  368. pr_info("%s: battery is absent!!\n", __func__);
  369. ret = 0;
  370. }
  371. return ret;
  372. }
  373. static int fg_write_temp(struct max77823_fuelgauge_data *fuelgauge,
  374. int temperature)
  375. {
  376. u8 data[2];
  377. data[0] = (temperature%10) * 1000 / 39;
  378. data[1] = temperature / 10;
  379. max77823_bulk_read(fuelgauge->i2c, TEMPERATURE_REG,
  380. 2, data);
  381. pr_debug("%s: temperature to (%d, 0x%02x%02x)\n",
  382. __func__, temperature, data[1], data[0]);
  383. return temperature;
  384. }
  385. static int fg_read_temp(struct max77823_fuelgauge_data *fuelgauge)
  386. {
  387. u8 data[2] = {0, 0};
  388. int temper = 0;
  389. if (fg_check_battery_present(fuelgauge)) {
  390. if (max77823_bulk_read(fuelgauge->i2c,
  391. TEMPERATURE_REG, 2, data) < 0) {
  392. pr_err("%s: Failed to read TEMPERATURE_REG\n",
  393. __func__);
  394. return -1;
  395. }
  396. if (data[1]&(0x1 << 7)) {
  397. temper = ((~(data[1]))&0xFF)+1;
  398. temper *= (-1000);
  399. temper -= ((~((int)data[0]))+1) * 39 / 10;
  400. } else {
  401. temper = data[1] & 0x7f;
  402. temper *= 1000;
  403. temper += data[0] * 39 / 10;
  404. }
  405. } else
  406. temper = 20000;
  407. if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
  408. pr_info("%s: TEMPERATURE(%d), data(0x%04x)\n",
  409. __func__, temper, (data[1]<<8) | data[0]);
  410. return temper/100;
  411. }
  412. /* soc should be 0.1% unit */
  413. static int fg_read_vfsoc(struct max77823_fuelgauge_data *fuelgauge)
  414. {
  415. u8 data[2];
  416. int soc;
  417. if (max77823_bulk_read(fuelgauge->i2c, VFSOC_REG,
  418. 2, data) < 0) {
  419. pr_err("%s: Failed to read VFSOC\n", __func__);
  420. return -1;
  421. }
  422. soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
  423. return min(soc, 1000);
  424. }
  425. /* soc should be 0.1% unit */
  426. static int fg_read_avsoc(struct max77823_fuelgauge_data *fuelgauge)
  427. {
  428. u8 data[2];
  429. int soc;
  430. if (max77823_bulk_read(fuelgauge->i2c, SOCAV_REG,
  431. 2, data) < 0) {
  432. pr_err("%s: Failed to read AVSOC\n", __func__);
  433. return -1;
  434. }
  435. soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
  436. return min(soc, 1000);
  437. }
  438. /* soc should be 0.1% unit */
  439. static int fg_read_soc(struct max77823_fuelgauge_data *fuelgauge)
  440. {
  441. u8 data[2];
  442. int soc;
  443. if (max77823_bulk_read(fuelgauge->i2c, SOCREP_REG, 2, data) < 0) {
  444. pr_err("%s: Failed to read SOCREP\n", __func__);
  445. return -1;
  446. }
  447. soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
  448. pr_debug("%s: raw capacity (%d)\n", __func__, soc);
  449. if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
  450. pr_debug("%s: raw capacity (%d), data(0x%04x)\n",
  451. __func__, soc, (data[1]<<8) | data[0]);
  452. return min(soc, 1000);
  453. }
  454. /* soc should be 0.01% unit */
  455. static int fg_read_rawsoc(struct max77823_fuelgauge_data *fuelgauge)
  456. {
  457. u8 data[2];
  458. int soc;
  459. if (max77823_bulk_read(fuelgauge->i2c, SOCREP_REG,
  460. 2, data) < 0) {
  461. pr_err("%s: Failed to read SOCREP\n", __func__);
  462. return -1;
  463. }
  464. soc = (data[1] * 100) + (data[0] * 100 / 256);
  465. pr_debug("%s: raw capacity (0.01%%) (%d)\n",
  466. __func__, soc);
  467. if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
  468. pr_debug("%s: raw capacity (%d), data(0x%04x)\n",
  469. __func__, soc, (data[1]<<8) | data[0]);
  470. return min(soc, 10000);
  471. }
  472. static int fg_read_fullcap(struct max77823_fuelgauge_data *fuelgauge)
  473. {
  474. u8 data[2];
  475. int ret;
  476. if (max77823_bulk_read(fuelgauge->i2c, FULLCAP_REG,
  477. 2, data) < 0) {
  478. pr_err("%s: Failed to read FULLCAP\n", __func__);
  479. return -1;
  480. }
  481. ret = (data[1] << 8) + data[0];
  482. return ret;
  483. }
  484. static int fg_read_mixcap(struct max77823_fuelgauge_data *fuelgauge)
  485. {
  486. u8 data[2];
  487. int ret;
  488. if (max77823_bulk_read(fuelgauge->i2c, REMCAP_MIX_REG,
  489. 2, data) < 0) {
  490. pr_err("%s: Failed to read REMCAP_MIX_REG\n",
  491. __func__);
  492. return -1;
  493. }
  494. ret = (data[1] << 8) + data[0];
  495. return ret;
  496. }
  497. static int fg_read_avcap(struct max77823_fuelgauge_data *fuelgauge)
  498. {
  499. u8 data[2];
  500. int ret;
  501. if (max77823_bulk_read(fuelgauge->i2c, REMCAP_AV_REG,
  502. 2, data) < 0) {
  503. pr_err("%s: Failed to read REMCAP_AV_REG\n",
  504. __func__);
  505. return -1;
  506. }
  507. ret = (data[1] << 8) + data[0];
  508. return ret;
  509. }
  510. static int fg_read_repcap(struct max77823_fuelgauge_data *fuelgauge)
  511. {
  512. u8 data[2];
  513. int ret;
  514. if (max77823_bulk_read(fuelgauge->i2c, REMCAP_REP_REG,
  515. 2, data) < 0) {
  516. pr_err("%s: Failed to read REMCAP_REP_REG\n",
  517. __func__);
  518. return -1;
  519. }
  520. ret = (data[1] << 8) + data[0];
  521. return ret;
  522. }
  523. static int fg_read_current(struct max77823_fuelgauge_data *fuelgauge, int unit)
  524. {
  525. u8 data1[2], data2[2];
  526. u32 temp, sign;
  527. s32 i_current;
  528. s32 avg_current;
  529. if (max77823_bulk_read(fuelgauge->i2c, CURRENT_REG,
  530. 2, data1) < 0) {
  531. pr_err("%s: Failed to read CURRENT\n", __func__);
  532. return -1;
  533. }
  534. if (max77823_bulk_read(fuelgauge->i2c, AVG_CURRENT_REG,
  535. 2, data2) < 0) {
  536. pr_err("%s: Failed to read AVERAGE CURRENT\n", __func__);
  537. return -1;
  538. }
  539. temp = ((data1[1]<<8) | data1[0]) & 0xFFFF;
  540. if (temp & (0x1 << 15)) {
  541. sign = NEGATIVE;
  542. temp = (~temp & 0xFFFF) + 1;
  543. } else
  544. sign = POSITIVE;
  545. /* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
  546. switch (unit) {
  547. case SEC_BATTEY_CURRENT_UA:
  548. i_current = temp * 15625 / 100;
  549. break;
  550. case SEC_BATTEY_CURRENT_MA:
  551. default:
  552. i_current = temp * 15625 / 100000;
  553. }
  554. if (sign)
  555. i_current *= -1;
  556. temp = ((data2[1]<<8) | data2[0]) & 0xFFFF;
  557. if (temp & (0x1 << 15)) {
  558. sign = NEGATIVE;
  559. temp = (~temp & 0xFFFF) + 1;
  560. } else
  561. sign = POSITIVE;
  562. /* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
  563. avg_current = temp * 15625 / 100000;
  564. if (sign)
  565. avg_current *= -1;
  566. if (!(fuelgauge->info.pr_cnt++ % PRINT_COUNT)) {
  567. fg_test_print(fuelgauge);
  568. pr_info("%s: CURRENT(%dmA), AVG_CURRENT(%dmA)\n",
  569. __func__, i_current, avg_current);
  570. fuelgauge->info.pr_cnt = 1;
  571. /* Read max77823's all registers every 5 minute. */
  572. fg_periodic_read(fuelgauge);
  573. }
  574. return i_current;
  575. }
  576. static int fg_read_avg_current(struct max77823_fuelgauge_data *fuelgauge, int unit)
  577. {
  578. u8 data2[2];
  579. u32 temp, sign;
  580. s32 avg_current;
  581. if (max77823_bulk_read(fuelgauge->i2c, AVG_CURRENT_REG,
  582. 2, data2) < 0) {
  583. pr_err("%s: Failed to read AVERAGE CURRENT\n",
  584. __func__);
  585. return -1;
  586. }
  587. temp = ((data2[1]<<8) | data2[0]) & 0xFFFF;
  588. if (temp & (0x1 << 15)) {
  589. sign = NEGATIVE;
  590. temp = (~temp & 0xFFFF) + 1;
  591. } else
  592. sign = POSITIVE;
  593. /* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
  594. switch (unit) {
  595. case SEC_BATTEY_CURRENT_UA:
  596. avg_current = temp * 15625 / 100;
  597. break;
  598. case SEC_BATTEY_CURRENT_MA:
  599. default:
  600. avg_current = temp * 15625 / 100000;
  601. }
  602. if (sign)
  603. avg_current *= -1;
  604. return avg_current;
  605. }
  606. int fg_reset_soc(struct max77823_fuelgauge_data *fuelgauge)
  607. {
  608. u8 data[2];
  609. int vfocv, fullcap;
  610. /* delay for current stablization */
  611. msleep(500);
  612. pr_info("%s: Before quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n",
  613. __func__, fg_read_vcell(fuelgauge), fg_read_vfocv(fuelgauge),
  614. fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
  615. pr_info("%s: Before quick-start - current(%d), avg current(%d)\n",
  616. __func__, fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA),
  617. fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA));
  618. if (fuelgauge->pdata->check_jig_status ||
  619. !fuelgauge->pdata->check_jig_status()) {
  620. pr_info("%s : Return by No JIG_ON signal\n", __func__);
  621. return 0;
  622. }
  623. max77823_write_word(fuelgauge->i2c, CYCLES_REG, 0);
  624. if (max77823_bulk_read(fuelgauge->i2c, MISCCFG_REG,
  625. 2, data) < 0) {
  626. pr_err("%s: Failed to read MiscCFG\n", __func__);
  627. return -1;
  628. }
  629. data[1] |= (0x1 << 2);
  630. if (max77823_bulk_write(fuelgauge->i2c, MISCCFG_REG,
  631. 2, data) < 0) {
  632. pr_err("%s: Failed to write MiscCFG\n", __func__);
  633. return -1;
  634. }
  635. msleep(250);
  636. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  637. get_battery_data(fuelgauge).Capacity);
  638. msleep(500);
  639. pr_info("%s: After quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n",
  640. __func__, fg_read_vcell(fuelgauge), fg_read_vfocv(fuelgauge),
  641. fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
  642. pr_info("%s: After quick-start - current(%d), avg current(%d)\n",
  643. __func__, fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA),
  644. fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA));
  645. max77823_write_word(fuelgauge->i2c, CYCLES_REG, 0x00a0);
  646. /* P8 is not turned off by Quickstart @3.4V
  647. * (It's not a problem, depend on mode data)
  648. * Power off for factory test(File system, etc..) */
  649. vfocv = fg_read_vfocv(fuelgauge);
  650. if (vfocv < POWER_OFF_VOLTAGE_LOW_MARGIN) {
  651. pr_info("%s: Power off condition(%d)\n", __func__, vfocv);
  652. fullcap = max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  653. /* FullCAP * 0.009 */
  654. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  655. (u16)(fullcap * 9 / 1000));
  656. msleep(200);
  657. pr_info("%s: new soc=%d, vfocv=%d\n", __func__,
  658. fg_read_soc(fuelgauge), vfocv);
  659. }
  660. pr_info("%s: Additional step - VfOCV(%d), VfSOC(%d), RepSOC(%d)\n",
  661. __func__, fg_read_vfocv(fuelgauge),
  662. fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
  663. return 0;
  664. }
  665. int fg_reset_capacity_by_jig_connection(struct max77823_fuelgauge_data *fuelgauge)
  666. {
  667. pr_info("%s: DesignCap = Capacity - 1 (Jig Connection)\n", __func__);
  668. return max77823_write_word(fuelgauge->i2c, DESIGNCAP_REG,
  669. get_battery_data(fuelgauge).Capacity-1);
  670. }
  671. int fg_adjust_capacity(struct max77823_fuelgauge_data *fuelgauge)
  672. {
  673. u8 data[2];
  674. data[0] = 0;
  675. data[1] = 0;
  676. /* 1. Write RemCapREP(05h)=0; */
  677. if (max77823_bulk_write(fuelgauge->i2c, REMCAP_REP_REG,
  678. 2, data) < 0) {
  679. pr_err("%s: Failed to write RemCap_REP\n", __func__);
  680. return -1;
  681. }
  682. msleep(200);
  683. pr_info("%s: After adjust - RepSOC(%d)\n", __func__,
  684. fg_read_soc(fuelgauge));
  685. return 0;
  686. }
  687. void fg_low_batt_compensation(struct max77823_fuelgauge_data *fuelgauge,
  688. u32 level)
  689. {
  690. int read_val;
  691. u32 temp;
  692. pr_info("%s: Adjust SOCrep to %d!!\n", __func__, level);
  693. read_val = max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  694. /* RemCapREP (05h) = FullCap(10h) x 0.0090 */
  695. temp = read_val * (level*90) / 10000;
  696. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  697. (u16)temp);
  698. }
  699. static int fg_check_status_reg(struct max77823_fuelgauge_data *fuelgauge)
  700. {
  701. u8 status_data[2];
  702. int ret = 0;
  703. /* 1. Check Smn was generatedread */
  704. if (max77823_bulk_read(fuelgauge->i2c, STATUS_REG,
  705. 2, status_data) < 0) {
  706. pr_err("%s: Failed to read STATUS_REG\n", __func__);
  707. return -1;
  708. }
  709. pr_info("%s: addr(0x00), data(0x%04x)\n", __func__,
  710. (status_data[1]<<8) | status_data[0]);
  711. if (status_data[1] & (0x1 << 2))
  712. ret = 1;
  713. /* 2. clear Status reg */
  714. status_data[1] = 0;
  715. if (max77823_bulk_write(fuelgauge->i2c, STATUS_REG,
  716. 2, status_data) < 0) {
  717. pr_info("%s: Failed to write STATUS_REG\n", __func__);
  718. return -1;
  719. }
  720. return ret;
  721. }
  722. int get_fuelgauge_value(struct max77823_fuelgauge_data *fuelgauge, int data)
  723. {
  724. int ret;
  725. switch (data) {
  726. case FG_LEVEL:
  727. ret = fg_read_soc(fuelgauge);
  728. break;
  729. case FG_TEMPERATURE:
  730. ret = fg_read_temp(fuelgauge);
  731. break;
  732. case FG_VOLTAGE:
  733. ret = fg_read_vcell(fuelgauge);
  734. break;
  735. case FG_CURRENT:
  736. ret = fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA);
  737. break;
  738. case FG_CURRENT_AVG:
  739. ret = fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA);
  740. break;
  741. case FG_CHECK_STATUS:
  742. ret = fg_check_status_reg(fuelgauge);
  743. break;
  744. case FG_RAW_SOC:
  745. ret = fg_read_rawsoc(fuelgauge);
  746. break;
  747. case FG_VF_SOC:
  748. ret = fg_read_vfsoc(fuelgauge);
  749. break;
  750. case FG_AV_SOC:
  751. ret = fg_read_avsoc(fuelgauge);
  752. break;
  753. case FG_FULLCAP:
  754. ret = fg_read_fullcap(fuelgauge);
  755. break;
  756. case FG_MIXCAP:
  757. ret = fg_read_mixcap(fuelgauge);
  758. break;
  759. case FG_AVCAP:
  760. ret = fg_read_avcap(fuelgauge);
  761. break;
  762. case FG_REPCAP:
  763. ret = fg_read_repcap(fuelgauge);
  764. break;
  765. default:
  766. ret = -1;
  767. break;
  768. }
  769. return ret;
  770. }
  771. int fg_alert_init(struct max77823_fuelgauge_data *fuelgauge, int soc)
  772. {
  773. u8 misccgf_data[2];
  774. u8 salrt_data[2];
  775. u8 config_data[2];
  776. u8 valrt_data[2];
  777. u8 talrt_data[2];
  778. u16 read_data = 0;
  779. /* Using RepSOC */
  780. if (max77823_bulk_read(fuelgauge->i2c, MISCCFG_REG, 2,
  781. misccgf_data) < 0) {
  782. pr_err("%s: Failed to read MISCCFG_REG\n", __func__);
  783. return -1;
  784. }
  785. misccgf_data[0] = misccgf_data[0] & ~(0x03);
  786. if (max77823_bulk_write(fuelgauge->i2c, MISCCFG_REG,
  787. 2, misccgf_data) < 0) {
  788. pr_info("%s: Failed to write MISCCFG_REG\n", __func__);
  789. return -1;
  790. }
  791. /* SALRT Threshold setting */
  792. salrt_data[1] = 0xff;
  793. salrt_data[0] = soc;
  794. if (max77823_bulk_write(fuelgauge->i2c, SALRT_THRESHOLD_REG,
  795. 2, salrt_data) < 0) {
  796. pr_info("%s: Failed to write SALRT_THRESHOLD_REG\n", __func__);
  797. return -1;
  798. }
  799. /* Reset VALRT Threshold setting (disable) */
  800. valrt_data[1] = 0xFF;
  801. valrt_data[0] = 0x00;
  802. if (max77823_bulk_write(fuelgauge->i2c, VALRT_THRESHOLD_REG,
  803. 2, valrt_data) < 0) {
  804. pr_info("%s: Failed to write VALRT_THRESHOLD_REG\n", __func__);
  805. return -1;
  806. }
  807. read_data = max77823_read_word(fuelgauge->i2c, (u8)VALRT_THRESHOLD_REG);
  808. if (read_data != 0xff00)
  809. pr_err("%s: VALRT_THRESHOLD_REG is not valid (0x%x)\n",
  810. __func__, read_data);
  811. /* Reset TALRT Threshold setting (disable) */
  812. talrt_data[1] = 0x7F;
  813. talrt_data[0] = 0x80;
  814. if (max77823_bulk_write(fuelgauge->i2c, TALRT_THRESHOLD_REG,
  815. 2, talrt_data) < 0) {
  816. pr_info("%s: Failed to write TALRT_THRESHOLD_REG\n", __func__);
  817. return -1;
  818. }
  819. read_data = max77823_read_word(fuelgauge->i2c, (u8)TALRT_THRESHOLD_REG);
  820. if (read_data != 0x7f80)
  821. pr_err("%s: TALRT_THRESHOLD_REG is not valid (0x%x)\n",
  822. __func__, read_data);
  823. /*mdelay(100);*/
  824. /* Enable SOC alerts */
  825. if (max77823_bulk_read(fuelgauge->i2c, CONFIG_REG,
  826. 2, config_data) < 0) {
  827. pr_err("%s: Failed to read CONFIG_REG\n", __func__);
  828. return -1;
  829. }
  830. config_data[0] = config_data[0] | (0x1 << 2);
  831. if (max77823_bulk_write(fuelgauge->i2c, CONFIG_REG,
  832. 2, config_data) < 0) {
  833. pr_info("%s: Failed to write CONFIG_REG\n", __func__);
  834. return -1;
  835. }
  836. return 1;
  837. }
  838. void fg_fullcharged_compensation(struct max77823_fuelgauge_data *fuelgauge,
  839. u32 is_recharging, bool pre_update)
  840. {
  841. static int new_fullcap_data;
  842. pr_info("%s: is_recharging(%d), pre_update(%d)\n",
  843. __func__, is_recharging, pre_update);
  844. new_fullcap_data =
  845. max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  846. if (new_fullcap_data < 0)
  847. new_fullcap_data = get_battery_data(fuelgauge).Capacity;
  848. /* compare with initial capacity */
  849. if (new_fullcap_data >
  850. (get_battery_data(fuelgauge).Capacity * 110 / 100)) {
  851. pr_info("%s: [Case 1] capacity = 0x%04x, NewFullCap = 0x%04x\n",
  852. __func__, get_battery_data(fuelgauge).Capacity,
  853. new_fullcap_data);
  854. new_fullcap_data =
  855. (get_battery_data(fuelgauge).Capacity * 110) / 100;
  856. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  857. (u16)(new_fullcap_data));
  858. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  859. (u16)(new_fullcap_data));
  860. } else if (new_fullcap_data <
  861. (get_battery_data(fuelgauge).Capacity * 50 / 100)) {
  862. pr_info("%s: [Case 5] capacity = 0x%04x, NewFullCap = 0x%04x\n",
  863. __func__, get_battery_data(fuelgauge).Capacity,
  864. new_fullcap_data);
  865. new_fullcap_data =
  866. (get_battery_data(fuelgauge).Capacity * 50) / 100;
  867. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  868. (u16)(new_fullcap_data));
  869. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  870. (u16)(new_fullcap_data));
  871. } else {
  872. /* compare with previous capacity */
  873. if (new_fullcap_data >
  874. (fuelgauge->info.previous_fullcap * 110 / 100)) {
  875. pr_info("%s: [Case 2] previous_fullcap = 0x%04x, NewFullCap = 0x%04x\n",
  876. __func__, fuelgauge->info.previous_fullcap,
  877. new_fullcap_data);
  878. new_fullcap_data =
  879. (fuelgauge->info.previous_fullcap * 110) / 100;
  880. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  881. (u16)(new_fullcap_data));
  882. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  883. (u16)(new_fullcap_data));
  884. } else if (new_fullcap_data <
  885. (fuelgauge->info.previous_fullcap * 90 / 100)) {
  886. pr_info("%s: [Case 3] previous_fullcap = 0x%04x, NewFullCap = 0x%04x\n",
  887. __func__, fuelgauge->info.previous_fullcap,
  888. new_fullcap_data);
  889. new_fullcap_data =
  890. (fuelgauge->info.previous_fullcap * 90) / 100;
  891. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  892. (u16)(new_fullcap_data));
  893. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  894. (u16)(new_fullcap_data));
  895. } else {
  896. pr_info("%s: [Case 4] previous_fullcap = 0x%04x, NewFullCap = 0x%04x\n",
  897. __func__, fuelgauge->info.previous_fullcap,
  898. new_fullcap_data);
  899. }
  900. }
  901. /* 4. Write RepSOC(06h)=100%; */
  902. max77823_write_word(fuelgauge->i2c, SOCREP_REG, (u16)(0x64 << 8));
  903. /* 5. Write MixSOC(0Dh)=100%; */
  904. max77823_write_word(fuelgauge->i2c, SOCMIX_REG, (u16)(0x64 << 8));
  905. /* 6. Write AVSOC(0Eh)=100%; */
  906. max77823_write_word(fuelgauge->i2c, SOCAV_REG, (u16)(0x64 << 8));
  907. /* if pre_update case, skip updating PrevFullCAP value. */
  908. if (!pre_update)
  909. fuelgauge->info.previous_fullcap =
  910. max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  911. pr_info("%s: (A) FullCap = 0x%04x, RemCap = 0x%04x\n", __func__,
  912. max77823_read_word(fuelgauge->i2c, FULLCAP_REG),
  913. max77823_read_word(fuelgauge->i2c, REMCAP_REP_REG));
  914. fg_periodic_read(fuelgauge);
  915. }
  916. void fg_check_vf_fullcap_range(struct max77823_fuelgauge_data *fuelgauge)
  917. {
  918. static int new_vffullcap;
  919. bool is_vffullcap_changed = true;
  920. if (fuelgauge->pdata->check_jig_status &&
  921. fuelgauge->pdata->check_jig_status())
  922. fg_reset_capacity_by_jig_connection(fuelgauge);
  923. new_vffullcap = max77823_read_word(fuelgauge->i2c, FULLCAP_NOM_REG);
  924. if (new_vffullcap < 0)
  925. new_vffullcap = get_battery_data(fuelgauge).Capacity;
  926. /* compare with initial capacity */
  927. if (new_vffullcap >
  928. (get_battery_data(fuelgauge).Capacity * 110 / 100)) {
  929. pr_info("%s: [Case 1] capacity = 0x%04x, NewVfFullCap = 0x%04x\n",
  930. __func__, get_battery_data(fuelgauge).Capacity,
  931. new_vffullcap);
  932. new_vffullcap =
  933. (get_battery_data(fuelgauge).Capacity * 110) / 100;
  934. max77823_write_word(fuelgauge->i2c, DQACC_REG,
  935. (u16)(new_vffullcap / 4));
  936. max77823_write_word(fuelgauge->i2c, DPACC_REG, (u16)0x3200);
  937. } else if (new_vffullcap <
  938. (get_battery_data(fuelgauge).Capacity * 50 / 100)) {
  939. pr_info("%s: [Case 5] capacity = 0x%04x, NewVfFullCap = 0x%04x\n",
  940. __func__, get_battery_data(fuelgauge).Capacity,
  941. new_vffullcap);
  942. new_vffullcap =
  943. (get_battery_data(fuelgauge).Capacity * 50) / 100;
  944. max77823_write_word(fuelgauge->i2c, DQACC_REG,
  945. (u16)(new_vffullcap / 4));
  946. max77823_write_word(fuelgauge->i2c, DPACC_REG,
  947. (u16)0x3200);
  948. } else {
  949. /* compare with previous capacity */
  950. if (new_vffullcap >
  951. (fuelgauge->info.previous_vffullcap * 110 / 100)) {
  952. pr_info("%s: [Case 2] previous_vffullcap = 0x%04x, NewVfFullCap = 0x%04x\n",
  953. __func__, fuelgauge->info.previous_vffullcap,
  954. new_vffullcap);
  955. new_vffullcap =
  956. (fuelgauge->info.previous_vffullcap * 110) /
  957. 100;
  958. max77823_write_word(fuelgauge->i2c, DQACC_REG,
  959. (u16)(new_vffullcap / 4));
  960. max77823_write_word(fuelgauge->i2c, DPACC_REG,
  961. (u16)0x3200);
  962. } else if (new_vffullcap <
  963. (fuelgauge->info.previous_vffullcap * 90 / 100)) {
  964. pr_info("%s: [Case 3] previous_vffullcap = 0x%04x, NewVfFullCap = 0x%04x\n",
  965. __func__, fuelgauge->info.previous_vffullcap,
  966. new_vffullcap);
  967. new_vffullcap =
  968. (fuelgauge->info.previous_vffullcap * 90) / 100;
  969. max77823_write_word(fuelgauge->i2c, DQACC_REG,
  970. (u16)(new_vffullcap / 4));
  971. max77823_write_word(fuelgauge->i2c, DPACC_REG,
  972. (u16)0x3200);
  973. } else {
  974. pr_info("%s: [Case 4] previous_vffullcap = 0x%04x, NewVfFullCap = 0x%04x\n",
  975. __func__, fuelgauge->info.previous_vffullcap,
  976. new_vffullcap);
  977. is_vffullcap_changed = false;
  978. }
  979. }
  980. /* delay for register setting (dQacc, dPacc) */
  981. if (is_vffullcap_changed)
  982. msleep(300);
  983. fuelgauge->info.previous_vffullcap =
  984. max77823_read_word(fuelgauge->i2c, FULLCAP_NOM_REG);
  985. if (is_vffullcap_changed)
  986. pr_info("%s : VfFullCap(0x%04x), dQacc(0x%04x), dPacc(0x%04x)\n",
  987. __func__,
  988. max77823_read_word(fuelgauge->i2c, FULLCAP_NOM_REG),
  989. max77823_read_word(fuelgauge->i2c, DQACC_REG),
  990. max77823_read_word(fuelgauge->i2c, DPACC_REG));
  991. }
  992. void fg_set_full_charged(struct max77823_fuelgauge_data *fuelgauge)
  993. {
  994. pr_info("[FG_Set_Full] (B) FullCAP(%d), RemCAP(%d)\n",
  995. (max77823_read_word(fuelgauge->i2c, FULLCAP_REG)/2),
  996. (max77823_read_word(fuelgauge->i2c, REMCAP_REP_REG)/2));
  997. max77823_write_word(fuelgauge->i2c, FULLCAP_REG,
  998. (u16)max77823_read_word(fuelgauge->i2c, REMCAP_REP_REG));
  999. pr_info("[FG_Set_Full] (A) FullCAP(%d), RemCAP(%d)\n",
  1000. (max77823_read_word(fuelgauge->i2c, FULLCAP_REG)/2),
  1001. (max77823_read_word(fuelgauge->i2c, REMCAP_REP_REG)/2));
  1002. }
  1003. static void display_low_batt_comp_cnt(struct max77823_fuelgauge_data *fuelgauge)
  1004. {
  1005. pr_info("Check Array(%s): [%d, %d], [%d, %d], ",
  1006. get_battery_data(fuelgauge).type_str,
  1007. fuelgauge->info.low_batt_comp_cnt[0][0],
  1008. fuelgauge->info.low_batt_comp_cnt[0][1],
  1009. fuelgauge->info.low_batt_comp_cnt[1][0],
  1010. fuelgauge->info.low_batt_comp_cnt[1][1]);
  1011. pr_info("[%d, %d], [%d, %d], [%d, %d]\n",
  1012. fuelgauge->info.low_batt_comp_cnt[2][0],
  1013. fuelgauge->info.low_batt_comp_cnt[2][1],
  1014. fuelgauge->info.low_batt_comp_cnt[3][0],
  1015. fuelgauge->info.low_batt_comp_cnt[3][1],
  1016. fuelgauge->info.low_batt_comp_cnt[4][0],
  1017. fuelgauge->info.low_batt_comp_cnt[4][1]);
  1018. }
  1019. static void add_low_batt_comp_cnt(struct max77823_fuelgauge_data *fuelgauge,
  1020. int range, int level)
  1021. {
  1022. int i;
  1023. int j;
  1024. /* Increase the requested count value, and reset others. */
  1025. fuelgauge->info.low_batt_comp_cnt[range-1][level/2]++;
  1026. for (i = 0; i < LOW_BATT_COMP_RANGE_NUM; i++) {
  1027. for (j = 0; j < LOW_BATT_COMP_LEVEL_NUM; j++) {
  1028. if (i == range-1 && j == level/2)
  1029. continue;
  1030. else
  1031. fuelgauge->info.low_batt_comp_cnt[i][j] = 0;
  1032. }
  1033. }
  1034. }
  1035. void prevent_early_poweroff(struct max77823_fuelgauge_data *fuelgauge,
  1036. int vcell, int *fg_soc)
  1037. {
  1038. int soc = 0;
  1039. int read_val;
  1040. soc = fg_read_soc(fuelgauge);
  1041. /* No need to write REMCAP_REP in below normal cases */
  1042. if (soc > POWER_OFF_SOC_HIGH_MARGIN || vcell > get_battery_data(fuelgauge).low_battery_comp_voltage)
  1043. return;
  1044. pr_info("%s: soc=%d, vcell=%d\n", __func__, soc, vcell);
  1045. if (vcell > POWER_OFF_VOLTAGE_HIGH_MARGIN) {
  1046. read_val = max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  1047. /* FullCAP * 0.013 */
  1048. max77823_write_word(fuelgauge->i2c, REMCAP_REP_REG,
  1049. (u16)(read_val * 13 / 1000));
  1050. msleep(200);
  1051. *fg_soc = fg_read_soc(fuelgauge);
  1052. pr_info("%s: new soc=%d, vcell=%d\n", __func__, *fg_soc, vcell);
  1053. }
  1054. }
  1055. void reset_low_batt_comp_cnt(struct max77823_fuelgauge_data *fuelgauge)
  1056. {
  1057. memset(fuelgauge->info.low_batt_comp_cnt, 0,
  1058. sizeof(fuelgauge->info.low_batt_comp_cnt));
  1059. }
  1060. static int check_low_batt_comp_condition(
  1061. struct max77823_fuelgauge_data *fuelgauge,
  1062. int *nLevel)
  1063. {
  1064. int i;
  1065. int j;
  1066. int ret = 0;
  1067. for (i = 0; i < LOW_BATT_COMP_RANGE_NUM; i++) {
  1068. for (j = 0; j < LOW_BATT_COMP_LEVEL_NUM; j++) {
  1069. if (fuelgauge->info.low_batt_comp_cnt[i][j] >=
  1070. MAX_LOW_BATT_CHECK_CNT) {
  1071. display_low_batt_comp_cnt(fuelgauge);
  1072. ret = 1;
  1073. *nLevel = j*2 + 1;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. return ret;
  1079. }
  1080. static int get_low_batt_threshold(struct max77823_fuelgauge_data *fuelgauge,
  1081. int range, int nCurrent, int level)
  1082. {
  1083. int ret = 0;
  1084. ret = get_battery_data(fuelgauge).low_battery_table[range][OFFSET] +
  1085. ((nCurrent *
  1086. get_battery_data(fuelgauge).low_battery_table[range][SLOPE]) /
  1087. 1000);
  1088. return ret;
  1089. }
  1090. int low_batt_compensation(struct max77823_fuelgauge_data *fuelgauge,
  1091. int fg_soc, int fg_vcell, int fg_current)
  1092. {
  1093. int fg_avg_current = 0;
  1094. int fg_min_current = 0;
  1095. int new_level = 0;
  1096. int i, table_size;
  1097. /* Not charging, Under low battery comp voltage */
  1098. if (fg_vcell <= get_battery_data(fuelgauge).low_battery_comp_voltage) {
  1099. fg_avg_current = fg_read_avg_current(fuelgauge,
  1100. SEC_BATTEY_CURRENT_MA);
  1101. fg_min_current = min(fg_avg_current, fg_current);
  1102. table_size =
  1103. sizeof(get_battery_data(fuelgauge).low_battery_table) /
  1104. (sizeof(s16)*TABLE_MAX);
  1105. for (i = 1; i < CURRENT_RANGE_MAX_NUM; i++) {
  1106. if ((fg_min_current >= get_battery_data(fuelgauge).
  1107. low_battery_table[i-1][RANGE]) &&
  1108. (fg_min_current < get_battery_data(fuelgauge).
  1109. low_battery_table[i][RANGE])) {
  1110. if (fg_soc >= 10 && fg_vcell <
  1111. get_low_batt_threshold(fuelgauge,
  1112. i, fg_min_current, 1)) {
  1113. add_low_batt_comp_cnt(
  1114. fuelgauge, i, 1);
  1115. } else {
  1116. reset_low_batt_comp_cnt(fuelgauge);
  1117. }
  1118. }
  1119. }
  1120. if (check_low_batt_comp_condition(fuelgauge, &new_level)) {
  1121. fg_low_batt_compensation(fuelgauge, new_level);
  1122. reset_low_batt_comp_cnt(fuelgauge);
  1123. /* Do not update soc right after
  1124. * low battery compensation
  1125. * to prevent from powering-off suddenly
  1126. */
  1127. pr_info("%s: SOC is set to %d by low compensation!!\n",
  1128. __func__, fg_read_soc(fuelgauge));
  1129. }
  1130. }
  1131. /* Prevent power off over 3500mV */
  1132. prevent_early_poweroff(fuelgauge, fg_vcell, &fg_soc);
  1133. return fg_soc;
  1134. }
  1135. static bool is_booted_in_low_battery(struct max77823_fuelgauge_data *fuelgauge)
  1136. {
  1137. int fg_vcell = get_fuelgauge_value(fuelgauge, FG_VOLTAGE);
  1138. int fg_current = get_fuelgauge_value(fuelgauge, FG_CURRENT);
  1139. int threshold = 0;
  1140. threshold = 3300 + ((fg_current * 17) / 100);
  1141. if (fg_vcell <= threshold)
  1142. return true;
  1143. else
  1144. return false;
  1145. }
  1146. static bool fuelgauge_recovery_handler(struct max77823_fuelgauge_data *fuelgauge)
  1147. {
  1148. int current_soc;
  1149. int avsoc;
  1150. int temperature;
  1151. if (fuelgauge->info.soc >= LOW_BATTERY_SOC_REDUCE_UNIT) {
  1152. pr_err("%s: Reduce the Reported SOC by 1%%\n",
  1153. __func__);
  1154. current_soc =
  1155. get_fuelgauge_value(fuelgauge, FG_LEVEL) / 10;
  1156. if (current_soc) {
  1157. pr_info("%s: Returning to Normal discharge path\n",
  1158. __func__);
  1159. pr_info("%s: Actual SOC(%d) non-zero\n",
  1160. __func__, current_soc);
  1161. fuelgauge->info.is_low_batt_alarm = false;
  1162. } else {
  1163. temperature =
  1164. get_fuelgauge_value(fuelgauge, FG_TEMPERATURE);
  1165. avsoc =
  1166. get_fuelgauge_value(fuelgauge, FG_AV_SOC);
  1167. if ((fuelgauge->info.soc > avsoc) ||
  1168. (temperature < 0)) {
  1169. fuelgauge->info.soc -=
  1170. LOW_BATTERY_SOC_REDUCE_UNIT;
  1171. pr_err("%s: New Reduced RepSOC (%d)\n",
  1172. __func__, fuelgauge->info.soc);
  1173. } else
  1174. pr_info("%s: Waiting for recovery (AvSOC:%d)\n",
  1175. __func__, avsoc);
  1176. }
  1177. }
  1178. return fuelgauge->info.is_low_batt_alarm;
  1179. }
  1180. static int get_fuelgauge_soc(struct max77823_fuelgauge_data *fuelgauge)
  1181. {
  1182. union power_supply_propval value;
  1183. int fg_soc = 0;
  1184. int fg_vfsoc;
  1185. int fg_vcell;
  1186. int fg_current;
  1187. int avg_current;
  1188. ktime_t current_time;
  1189. struct timespec ts;
  1190. int fullcap_check_interval;
  1191. if (fuelgauge->info.is_low_batt_alarm)
  1192. if (fuelgauge_recovery_handler(fuelgauge)) {
  1193. fg_soc = fuelgauge->info.soc;
  1194. goto return_soc;
  1195. }
  1196. #if defined(ANDROID_ALARM_ACTIVATED)
  1197. current_time = alarm_get_elapsed_realtime();
  1198. ts = ktime_to_timespec(current_time);
  1199. #else
  1200. current_time = ktime_get_boottime();
  1201. ts = ktime_to_timespec(current_time);
  1202. #endif
  1203. /* check fullcap range */
  1204. fullcap_check_interval =
  1205. (ts.tv_sec - fuelgauge->info.fullcap_check_interval);
  1206. if (fullcap_check_interval >
  1207. VFFULLCAP_CHECK_INTERVAL) {
  1208. pr_info("%s: check fullcap range (interval:%d)\n",
  1209. __func__, fullcap_check_interval);
  1210. fg_check_vf_fullcap_range(fuelgauge);
  1211. fuelgauge->info.fullcap_check_interval = ts.tv_sec;
  1212. }
  1213. fg_soc = get_fuelgauge_value(fuelgauge, FG_LEVEL);
  1214. if (fg_soc < 0) {
  1215. pr_info("Can't read soc!!!");
  1216. fg_soc = fuelgauge->info.soc;
  1217. }
  1218. if (fuelgauge->info.low_batt_boot_flag) {
  1219. fg_soc = 0;
  1220. if (fuelgauge->pdata->check_cable_callback &&
  1221. fuelgauge->pdata->check_cable_callback() !=
  1222. POWER_SUPPLY_TYPE_BATTERY &&
  1223. !is_booted_in_low_battery(fuelgauge)) {
  1224. fg_adjust_capacity(fuelgauge);
  1225. fuelgauge->info.low_batt_boot_flag = 0;
  1226. }
  1227. if (fuelgauge->pdata->check_cable_callback &&
  1228. fuelgauge->pdata->check_cable_callback() ==
  1229. POWER_SUPPLY_TYPE_BATTERY)
  1230. fuelgauge->info.low_batt_boot_flag = 0;
  1231. }
  1232. fg_vcell = get_fuelgauge_value(fuelgauge, FG_VOLTAGE);
  1233. fg_current = get_fuelgauge_value(fuelgauge, FG_CURRENT);
  1234. avg_current = get_fuelgauge_value(fuelgauge, FG_CURRENT_AVG);
  1235. fg_vfsoc = get_fuelgauge_value(fuelgauge, FG_VF_SOC);
  1236. psy_do_property("battery", get,
  1237. POWER_SUPPLY_PROP_STATUS, value);
  1238. /* Algorithm for reducing time to fully charged (from MAXIM) */
  1239. if (value.intval != POWER_SUPPLY_STATUS_DISCHARGING &&
  1240. value.intval != POWER_SUPPLY_STATUS_FULL &&
  1241. fuelgauge->cable_type != POWER_SUPPLY_TYPE_USB &&
  1242. /* Skip when first check after boot up */
  1243. !fuelgauge->info.is_first_check &&
  1244. (fg_vfsoc > VFSOC_FOR_FULLCAP_LEARNING &&
  1245. (fg_current > LOW_CURRENT_FOR_FULLCAP_LEARNING &&
  1246. fg_current < HIGH_CURRENT_FOR_FULLCAP_LEARNING) &&
  1247. (avg_current > LOW_AVGCURRENT_FOR_FULLCAP_LEARNING &&
  1248. avg_current < HIGH_AVGCURRENT_FOR_FULLCAP_LEARNING))) {
  1249. if (fuelgauge->info.full_check_flag == 2) {
  1250. pr_info("%s: force fully charged SOC !! (%d)",
  1251. __func__, fuelgauge->info.full_check_flag);
  1252. fg_set_full_charged(fuelgauge);
  1253. fg_soc = get_fuelgauge_value(fuelgauge, FG_LEVEL);
  1254. } else if (fuelgauge->info.full_check_flag < 2)
  1255. pr_info("%s: full_check_flag (%d)",
  1256. __func__, fuelgauge->info.full_check_flag);
  1257. /* prevent overflow */
  1258. if (fuelgauge->info.full_check_flag++ > 10000)
  1259. fuelgauge->info.full_check_flag = 3;
  1260. } else
  1261. fuelgauge->info.full_check_flag = 0;
  1262. /* Checks vcell level and tries to compensate SOC if needed.*/
  1263. /* If jig cable is connected, then skip low batt compensation check. */
  1264. if (fuelgauge->pdata->check_jig_status &&
  1265. !fuelgauge->pdata->check_jig_status() &&
  1266. value.intval == POWER_SUPPLY_STATUS_DISCHARGING)
  1267. fg_soc = low_batt_compensation(
  1268. fuelgauge, fg_soc, fg_vcell, fg_current);
  1269. if (fuelgauge->info.is_first_check)
  1270. fuelgauge->info.is_first_check = false;
  1271. fuelgauge->info.soc = fg_soc;
  1272. return_soc:
  1273. pr_debug("%s: soc(%d), low_batt_alarm(%d)\n",
  1274. __func__, fuelgauge->info.soc,
  1275. fuelgauge->info.is_low_batt_alarm);
  1276. return fg_soc;
  1277. }
  1278. static void full_comp_work_handler(struct work_struct *work)
  1279. {
  1280. struct sec_fg_info *fg_info =
  1281. container_of(work, struct sec_fg_info, full_comp_work.work);
  1282. struct max77823_fuelgauge_data *fuelgauge =
  1283. container_of(fg_info, struct max77823_fuelgauge_data, info);
  1284. int avg_current;
  1285. union power_supply_propval value;
  1286. avg_current = get_fuelgauge_value(fuelgauge, FG_CURRENT_AVG);
  1287. psy_do_property("battery", get,
  1288. POWER_SUPPLY_PROP_STATUS, value);
  1289. if (avg_current >= 25) {
  1290. cancel_delayed_work(&fuelgauge->info.full_comp_work);
  1291. schedule_delayed_work(&fuelgauge->info.full_comp_work, 100);
  1292. } else {
  1293. pr_info("%s: full charge compensation start (avg_current %d)\n",
  1294. __func__, avg_current);
  1295. fg_fullcharged_compensation(fuelgauge,
  1296. (int)(value.intval ==
  1297. POWER_SUPPLY_STATUS_FULL), false);
  1298. }
  1299. }
  1300. static irqreturn_t max77823_jig_irq_thread(int irq, void *irq_data)
  1301. {
  1302. struct max77823_fuelgauge_data *fuelgauge = irq_data;
  1303. if (fuelgauge->pdata->check_jig_status &&
  1304. fuelgauge->pdata->check_jig_status())
  1305. fg_reset_capacity_by_jig_connection(fuelgauge);
  1306. else
  1307. pr_info("%s: jig removed\n", __func__);
  1308. return IRQ_HANDLED;
  1309. }
  1310. bool max77823_fg_init(struct max77823_fuelgauge_data *fuelgauge)
  1311. {
  1312. ktime_t current_time;
  1313. struct timespec ts;
  1314. u8 data[2] = {0, 0};
  1315. #if defined(ANDROID_ALARM_ACTIVATED)
  1316. current_time = alarm_get_elapsed_realtime();
  1317. ts = ktime_to_timespec(current_time);
  1318. #else
  1319. current_time = ktime_get_boottime();
  1320. ts = ktime_to_timespec(current_time);
  1321. #endif
  1322. fuelgauge->info.fullcap_check_interval = ts.tv_sec;
  1323. fuelgauge->info.is_low_batt_alarm = false;
  1324. fuelgauge->info.is_first_check = true;
  1325. /* Init parameters to prevent wrong compensation. */
  1326. fuelgauge->info.previous_fullcap =
  1327. max77823_read_word(fuelgauge->i2c, FULLCAP_REG);
  1328. fuelgauge->info.previous_vffullcap =
  1329. max77823_read_word(fuelgauge->i2c, FULLCAP_NOM_REG);
  1330. if (fuelgauge->pdata->check_cable_callback &&
  1331. (fuelgauge->pdata->check_cable_callback() !=
  1332. POWER_SUPPLY_TYPE_BATTERY) &&
  1333. is_booted_in_low_battery(fuelgauge))
  1334. fuelgauge->info.low_batt_boot_flag = 1;
  1335. if (fuelgauge->pdata->check_jig_status &&
  1336. fuelgauge->pdata->check_jig_status())
  1337. fg_reset_capacity_by_jig_connection(fuelgauge);
  1338. else {
  1339. if (fuelgauge->pdata->jig_irq) {
  1340. int ret;
  1341. ret = request_threaded_irq(fuelgauge->pdata->jig_irq,
  1342. NULL, max77823_jig_irq_thread,
  1343. fuelgauge->pdata->jig_irq_attr,
  1344. "jig-irq", fuelgauge);
  1345. if (ret) {
  1346. pr_info("%s: Failed to Reqeust IRQ\n",
  1347. __func__);
  1348. }
  1349. }
  1350. }
  1351. INIT_DELAYED_WORK(&fuelgauge->info.full_comp_work,
  1352. full_comp_work_handler);
  1353. /* NOT using FG for temperature */
  1354. if (fuelgauge->pdata->thermal_source != SEC_BATTERY_THERMAL_SOURCE_FG) {
  1355. data[0] = 0x00;
  1356. data[1] = 0x21;
  1357. max77823_bulk_write(fuelgauge->i2c, CONFIG_REG,
  1358. 2, data);
  1359. }
  1360. return true;
  1361. }
  1362. bool max77823_fg_fuelalert_init(struct max77823_fuelgauge_data *fuelgauge,
  1363. int soc)
  1364. {
  1365. if (fg_alert_init(fuelgauge, soc) > 0)
  1366. return true;
  1367. else
  1368. return false;
  1369. }
  1370. bool max77823_fg_is_fuelalerted(struct max77823_fuelgauge_data *fuelgauge)
  1371. {
  1372. if (get_fuelgauge_value(fuelgauge, FG_CHECK_STATUS) > 0)
  1373. return true;
  1374. else
  1375. return false;
  1376. }
  1377. bool max77823_fg_fuelalert_process(void *irq_data, bool is_fuel_alerted)
  1378. {
  1379. struct max77823_fuelgauge_data *fuelgauge =
  1380. (struct max77823_fuelgauge_data *)irq_data;
  1381. union power_supply_propval value;
  1382. int overcurrent_limit_in_soc;
  1383. int current_soc =
  1384. get_fuelgauge_value(fuelgauge, FG_LEVEL);
  1385. psy_do_property("battery", get,
  1386. POWER_SUPPLY_PROP_STATUS, value);
  1387. if (value.intval == POWER_SUPPLY_STATUS_CHARGING)
  1388. return true;
  1389. if (fuelgauge->info.soc <= STABLE_LOW_BATTERY_DIFF)
  1390. overcurrent_limit_in_soc = STABLE_LOW_BATTERY_DIFF_LOWBATT;
  1391. else
  1392. overcurrent_limit_in_soc = STABLE_LOW_BATTERY_DIFF;
  1393. if (((int)fuelgauge->info.soc - current_soc) >
  1394. overcurrent_limit_in_soc) {
  1395. pr_info("%s: Abnormal Current Consumption jump by %d units\n",
  1396. __func__, (((int)fuelgauge->info.soc - current_soc)));
  1397. pr_info("%s: Last Reported SOC (%d).\n",
  1398. __func__, fuelgauge->info.soc);
  1399. fuelgauge->info.is_low_batt_alarm = true;
  1400. if (fuelgauge->info.soc >=
  1401. LOW_BATTERY_SOC_REDUCE_UNIT)
  1402. return true;
  1403. }
  1404. if (value.intval ==
  1405. POWER_SUPPLY_STATUS_DISCHARGING) {
  1406. pr_err("Set battery level as 0, power off.\n");
  1407. fuelgauge->info.soc = 0;
  1408. value.intval = 0;
  1409. psy_do_property("battery", set,
  1410. POWER_SUPPLY_PROP_CAPACITY, value);
  1411. }
  1412. return true;
  1413. }
  1414. bool max77823_fg_full_charged(struct max77823_fuelgauge_data *fuelgauge)
  1415. {
  1416. union power_supply_propval value;
  1417. psy_do_property("battery", get,
  1418. POWER_SUPPLY_PROP_STATUS, value);
  1419. /* full charge compensation algorithm by MAXIM */
  1420. fg_fullcharged_compensation(fuelgauge,
  1421. (int)(value.intval == POWER_SUPPLY_STATUS_FULL), true);
  1422. cancel_delayed_work(&fuelgauge->info.full_comp_work);
  1423. schedule_delayed_work(&fuelgauge->info.full_comp_work, 100);
  1424. return false;
  1425. }
  1426. bool max77823_fg_reset(struct max77823_fuelgauge_data *fuelgauge)
  1427. {
  1428. if (!fg_reset_soc(fuelgauge))
  1429. return true;
  1430. else
  1431. return false;
  1432. }
  1433. #endif
  1434. static void max77823_fg_get_scaled_capacity(
  1435. struct max77823_fuelgauge_data *fuelgauge,
  1436. union power_supply_propval *val)
  1437. {
  1438. val->intval = (val->intval < fuelgauge->pdata->capacity_min) ?
  1439. 0 : ((val->intval - fuelgauge->pdata->capacity_min) * 1000 /
  1440. (fuelgauge->capacity_max - fuelgauge->pdata->capacity_min));
  1441. pr_debug("%s: scaled capacity (%d.%d)\n",
  1442. __func__, val->intval/10, val->intval%10);
  1443. }
  1444. /* capacity is integer */
  1445. static void max77823_fg_get_atomic_capacity(
  1446. struct max77823_fuelgauge_data *fuelgauge,
  1447. union power_supply_propval *val)
  1448. {
  1449. if (fuelgauge->pdata->capacity_calculation_type &
  1450. SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC) {
  1451. if (fuelgauge->capacity_old < val->intval)
  1452. val->intval = fuelgauge->capacity_old + 1;
  1453. else if (fuelgauge->capacity_old > val->intval)
  1454. val->intval = fuelgauge->capacity_old - 1;
  1455. }
  1456. /* keep SOC stable in abnormal status */
  1457. if (fuelgauge->pdata->capacity_calculation_type &
  1458. SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL) {
  1459. if (!fuelgauge->is_charging &&
  1460. fuelgauge->capacity_old < val->intval) {
  1461. pr_err("%s: capacity (old %d : new %d)\n",
  1462. __func__, fuelgauge->capacity_old, val->intval);
  1463. val->intval = fuelgauge->capacity_old;
  1464. }
  1465. }
  1466. /* updated old capacity */
  1467. fuelgauge->capacity_old = val->intval;
  1468. }
  1469. static int max77823_fg_calculate_dynamic_scale(
  1470. struct max77823_fuelgauge_data *fuelgauge)
  1471. {
  1472. union power_supply_propval raw_soc_val;
  1473. #ifdef CONFIG_FUELGAUGE_MAX77823_VOLTAGE_TRACKING
  1474. raw_soc_val.intval = max77823_get_soc(fuelgauge) / 10;
  1475. #else
  1476. raw_soc_val.intval = get_fuelgauge_value(fuelgauge,
  1477. FG_RAW_SOC);
  1478. #endif
  1479. if (raw_soc_val.intval <
  1480. fuelgauge->pdata->capacity_max -
  1481. fuelgauge->pdata->capacity_max_margin) {
  1482. fuelgauge->capacity_max =
  1483. fuelgauge->pdata->capacity_max -
  1484. fuelgauge->pdata->capacity_max_margin;
  1485. pr_debug("%s: capacity_max (%d)", __func__,
  1486. fuelgauge->capacity_max);
  1487. } else {
  1488. fuelgauge->capacity_max =
  1489. (raw_soc_val.intval >
  1490. fuelgauge->pdata->capacity_max +
  1491. fuelgauge->pdata->capacity_max_margin) ?
  1492. (fuelgauge->pdata->capacity_max +
  1493. fuelgauge->pdata->capacity_max_margin) :
  1494. raw_soc_val.intval;
  1495. pr_debug("%s: raw soc (%d)", __func__,
  1496. fuelgauge->capacity_max);
  1497. }
  1498. fuelgauge->capacity_max =
  1499. (fuelgauge->capacity_max * 99 / 100);
  1500. /* update capacity_old for sec_fg_get_atomic_capacity algorithm */
  1501. fuelgauge->capacity_old = 100;
  1502. pr_info("%s: %d is used for capacity_max\n",
  1503. __func__, fuelgauge->capacity_max);
  1504. return fuelgauge->capacity_max;
  1505. }
  1506. #ifdef CONFIG_FUELGAUGE_MAX77823_VOLTAGE_TRACKING
  1507. static int max77823_fg_get_property(strcut power_supply *psy,
  1508. enum power_supply_property psp,
  1509. union power_supply_propval *val)
  1510. {
  1511. struct max77823_fuelgauge_data *fuelgauge =
  1512. container_of(psy, struct max77823_fuelgauge_data, psy_fg);
  1513. switch (psp) {
  1514. /* Cell voltage (VCELL, mV) */
  1515. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1516. val->intval = max77823_get_vcell(fuelgauge);
  1517. break;
  1518. /* Additional Voltage Information (mV) */
  1519. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  1520. switch (val->intval) {
  1521. case SEC_BATTEY_VOLTAGE_AVERAGE:
  1522. val->intval = max77823_get_avgvcell(fuelgauge);
  1523. break;
  1524. case SEC_BATTEY_VOLTAGE_OCV:
  1525. val->intval = max77823_get_vfocv(fuelgauge);
  1526. break;
  1527. }
  1528. break;
  1529. /* Current (mA) */
  1530. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1531. val->intval = 0;
  1532. break;
  1533. /* Average Current (mA) */
  1534. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1535. val->intval = 0;
  1536. break;
  1537. /* SOC (%) */
  1538. case POWER_SUPPLY_PROP_CAPACITY:
  1539. if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RAW) {
  1540. val->intval = max77823_get_soc(fuelgauge);
  1541. } else {
  1542. val->intval = max77823_get_soc(fuelgauge) / 10;
  1543. if (fuelgauge->pdata->capacity_calculation_type &
  1544. (SEC_FUELGAUGE_CAPACITY_TYPE_SCALE |
  1545. SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE))
  1546. max77823_fg_get_scaled_capacity(fuelgauge, val);
  1547. /* capacity should be between 0% and 100%
  1548. * (0.1% degree)
  1549. */
  1550. if (val->intval > 1000)
  1551. val->intval = 1000;
  1552. if (val->intval < 0)
  1553. val->intval = 0;
  1554. /* get only integer part */
  1555. val->intval /= 10;
  1556. /* check whether doing the wake_unlock */
  1557. if ((val->intval > fuelgauge->pdata->fuel_alert_soc) &&
  1558. fuelgauge->is_fuel_alerted) {
  1559. wake_unlock(&fuelgauge->fuel_alert_wake_lock);
  1560. max77823_fg_fuelalert_init(fuelgauge,
  1561. fuelgauge->pdata->fuel_alert_soc);
  1562. }
  1563. /* (Only for atomic capacity)
  1564. * In initial time, capacity_old is 0.
  1565. * and in resume from sleep,
  1566. * capacity_old is too different from actual soc.
  1567. * should update capacity_old
  1568. * by val->intval in booting or resume.
  1569. */
  1570. if (fuelgauge->initial_update_of_soc) {
  1571. /* updated old capacity */
  1572. fuelgauge->capacity_old = val->intval;
  1573. fuelgauge->initial_update_of_soc = false;
  1574. break;
  1575. }
  1576. if (fuelgauge->pdata->capacity_calculation_type &
  1577. (SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC |
  1578. SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL))
  1579. max77823_fg_get_atomic_capacity(fuelgauge, val);
  1580. }
  1581. break;
  1582. /* Battery Temperature */
  1583. case POWER_SUPPLY_PROP_TEMP:
  1584. /* Target Temperature */
  1585. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  1586. val->intval = max77823_get_temperature(fuelgauge);
  1587. break;
  1588. default:
  1589. return false;
  1590. }
  1591. return true;
  1592. }
  1593. static int max77823_fg_set_property(struct power_supply *psy,
  1594. enum power_supply_property psp,
  1595. const union power_supply_propval *val)
  1596. {
  1597. struct max77823_fuelgauge_data *fuelgauge =
  1598. container_of(psy, struct max77823_fuelgauge_data, psy_fg);
  1599. switch (psp) {
  1600. case POWER_SUPPLY_PROP_STATUS:
  1601. if (val->intval == POWER_SUPPLY_STATUS_FULL)
  1602. max77823_fg_full_charged(fuelgauge);
  1603. break;
  1604. case POWER_SUPPLY_PROP_CHARGE_FULL:
  1605. if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
  1606. if (fuelgauge->pdata->capacity_calculation_type &
  1607. SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE)
  1608. max77823_fg_calculate_dynamic_scale(fuelgauge);
  1609. }
  1610. break;
  1611. case POWER_SUPPLY_PROP_ONLINE:
  1612. fuelgauge->cable_type = val->intval;
  1613. if (val->intval == POWER_SUPPLY_TYPE_BATTERY)
  1614. fuelgauge->is_charging = false;
  1615. else
  1616. fuelgauge->is_charging = true;
  1617. break;
  1618. /* Battery Temperature */
  1619. case POWER_SUPPLY_PROP_CAPACITY:
  1620. if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RESET) {
  1621. fuelgauge->initial_update_of_soc = true;
  1622. if (!max77823_fg_reset(fuelgauge))
  1623. return -EINVAL;
  1624. else
  1625. break;
  1626. }
  1627. /* Battery Temperature */
  1628. case POWER_SUPPLY_PROP_TEMP:
  1629. /* Target Temperature */
  1630. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  1631. max77823_set_temperature(fuelgauge, val->intval);
  1632. break;
  1633. default:
  1634. return false;
  1635. }
  1636. return true;
  1637. }
  1638. #endif
  1639. #ifdef CONFIG_FUELGAUGE_MAX77823_COULOMB_COUNTING
  1640. static int max77823_fg_get_property(struct power_supply *psy,
  1641. enum power_supply_property psp,
  1642. union power_supply_propval *val)
  1643. {
  1644. struct max77823_fuelgauge_data *fuelgauge =
  1645. container_of(psy, struct max77823_fuelgauge_data, psy_fg);
  1646. switch (psp) {
  1647. /* Cell voltage (VCELL, mV) */
  1648. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1649. val->intval = get_fuelgauge_value(fuelgauge, FG_VOLTAGE);
  1650. break;
  1651. /* Additional Voltage Information (mV) */
  1652. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  1653. switch (val->intval) {
  1654. case SEC_BATTEY_VOLTAGE_OCV:
  1655. val->intval = fg_read_vfocv(fuelgauge);
  1656. break;
  1657. case SEC_BATTEY_VOLTAGE_AVERAGE:
  1658. default:
  1659. val->intval = fg_read_avg_vcell(fuelgauge);
  1660. break;
  1661. }
  1662. break;
  1663. /* Current */
  1664. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1665. switch (val->intval) {
  1666. case SEC_BATTEY_CURRENT_UA:
  1667. val->intval =
  1668. fg_read_current(fuelgauge,
  1669. SEC_BATTEY_CURRENT_UA);
  1670. break;
  1671. case SEC_BATTEY_CURRENT_MA:
  1672. default:
  1673. val->intval = get_fuelgauge_value(fuelgauge,
  1674. FG_CURRENT);
  1675. break;
  1676. }
  1677. break;
  1678. /* Average Current */
  1679. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1680. switch (val->intval) {
  1681. case SEC_BATTEY_CURRENT_UA:
  1682. val->intval =
  1683. fg_read_avg_current(fuelgauge,
  1684. SEC_BATTEY_CURRENT_UA);
  1685. break;
  1686. case SEC_BATTEY_CURRENT_MA:
  1687. default:
  1688. val->intval =
  1689. get_fuelgauge_value(fuelgauge,
  1690. FG_CURRENT_AVG);
  1691. break;
  1692. }
  1693. break;
  1694. /* Full Capacity */
  1695. case POWER_SUPPLY_PROP_ENERGY_NOW:
  1696. switch (val->intval) {
  1697. case SEC_BATTEY_CAPACITY_DESIGNED:
  1698. val->intval = get_fuelgauge_value(fuelgauge,
  1699. FG_FULLCAP);
  1700. break;
  1701. case SEC_BATTEY_CAPACITY_ABSOLUTE:
  1702. val->intval = get_fuelgauge_value(fuelgauge,
  1703. FG_MIXCAP);
  1704. break;
  1705. case SEC_BATTEY_CAPACITY_TEMPERARY:
  1706. val->intval = get_fuelgauge_value(fuelgauge,
  1707. FG_AVCAP);
  1708. break;
  1709. case SEC_BATTEY_CAPACITY_CURRENT:
  1710. val->intval = get_fuelgauge_value(fuelgauge,
  1711. FG_REPCAP);
  1712. break;
  1713. }
  1714. break;
  1715. /* SOC (%) */
  1716. case POWER_SUPPLY_PROP_CAPACITY:
  1717. if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RAW) {
  1718. val->intval = get_fuelgauge_value(fuelgauge,
  1719. FG_RAW_SOC);
  1720. } else {
  1721. val->intval = get_fuelgauge_soc(fuelgauge);
  1722. if (fuelgauge->pdata->capacity_calculation_type &
  1723. (SEC_FUELGAUGE_CAPACITY_TYPE_SCALE |
  1724. SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE))
  1725. max77823_fg_get_scaled_capacity(fuelgauge, val);
  1726. /* capacity should be between 0% and 100%
  1727. * (0.1% degree)
  1728. */
  1729. if (val->intval > 1000)
  1730. val->intval = 1000;
  1731. if (val->intval < 0)
  1732. val->intval = 0;
  1733. /* get only integer part */
  1734. val->intval /= 10;
  1735. /* check whether doing the wake_unlock */
  1736. if ((val->intval > fuelgauge->pdata->fuel_alert_soc) &&
  1737. fuelgauge->is_fuel_alerted) {
  1738. wake_unlock(&fuelgauge->fuel_alert_wake_lock);
  1739. max77823_fg_fuelalert_init(fuelgauge,
  1740. fuelgauge->pdata->fuel_alert_soc);
  1741. }
  1742. /* (Only for atomic capacity)
  1743. * In initial time, capacity_old is 0.
  1744. * and in resume from sleep,
  1745. * capacity_old is too different from actual soc.
  1746. * should update capacity_old
  1747. * by val->intval in booting or resume.
  1748. */
  1749. if (fuelgauge->initial_update_of_soc) {
  1750. /* updated old capacity */
  1751. fuelgauge->capacity_old = val->intval;
  1752. fuelgauge->initial_update_of_soc = false;
  1753. break;
  1754. }
  1755. if (fuelgauge->pdata->capacity_calculation_type &
  1756. (SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC |
  1757. SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL))
  1758. max77823_fg_get_atomic_capacity(fuelgauge, val);
  1759. }
  1760. break;
  1761. /* Battery Temperature */
  1762. case POWER_SUPPLY_PROP_TEMP:
  1763. /* Target Temperature */
  1764. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  1765. val->intval = get_fuelgauge_value(fuelgauge,
  1766. FG_TEMPERATURE);
  1767. break;
  1768. default:
  1769. return -EINVAL;
  1770. }
  1771. return 0;
  1772. }
  1773. static int max77823_fg_set_property(struct power_supply *psy,
  1774. enum power_supply_property psp,
  1775. const union power_supply_propval *val)
  1776. {
  1777. struct max77823_fuelgauge_data *fuelgauge =
  1778. container_of(psy, struct max77823_fuelgauge_data, psy_fg);
  1779. switch (psp) {
  1780. case POWER_SUPPLY_PROP_STATUS:
  1781. if (val->intval == POWER_SUPPLY_STATUS_FULL)
  1782. max77823_fg_full_charged(fuelgauge);
  1783. break;
  1784. case POWER_SUPPLY_PROP_CHARGE_FULL:
  1785. if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
  1786. if (fuelgauge->pdata->capacity_calculation_type &
  1787. SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE)
  1788. max77823_fg_calculate_dynamic_scale(fuelgauge);
  1789. }
  1790. break;
  1791. case POWER_SUPPLY_PROP_ONLINE:
  1792. fuelgauge->cable_type = val->intval;
  1793. if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
  1794. fuelgauge->is_charging = false;
  1795. } else {
  1796. fuelgauge->is_charging = true;
  1797. if (fuelgauge->info.is_low_batt_alarm) {
  1798. pr_info("%s: Reset low_batt_alarm\n",
  1799. __func__);
  1800. fuelgauge->info.is_low_batt_alarm = false;
  1801. }
  1802. reset_low_batt_comp_cnt(fuelgauge);
  1803. }
  1804. break;
  1805. /* Battery Temperature */
  1806. case POWER_SUPPLY_PROP_CAPACITY:
  1807. if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RESET) {
  1808. fuelgauge->initial_update_of_soc = true;
  1809. if (!max77823_fg_reset(fuelgauge))
  1810. return -EINVAL;
  1811. else
  1812. break;
  1813. }
  1814. case POWER_SUPPLY_PROP_TEMP:
  1815. /* Target Temperature */
  1816. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  1817. fg_write_temp(fuelgauge, val->intval);
  1818. break;
  1819. case POWER_SUPPLY_PROP_ENERGY_NOW:
  1820. fg_reset_capacity_by_jig_connection(fuelgauge);
  1821. break;
  1822. default:
  1823. return -EINVAL;
  1824. }
  1825. return 0;
  1826. }
  1827. #endif
  1828. static void max77823_fg_isr_work(struct work_struct *work)
  1829. {
  1830. struct max77823_fuelgauge_data *fuelgauge =
  1831. container_of(work, struct max77823_fuelgauge_data, isr_work.work);
  1832. /* process for fuel gauge chip */
  1833. max77823_fg_fuelalert_process(fuelgauge, fuelgauge->is_fuel_alerted);
  1834. /* process for others */
  1835. if (fuelgauge->pdata->fuelalert_process != NULL)
  1836. fuelgauge->pdata->fuelalert_process(fuelgauge->is_fuel_alerted);
  1837. }
  1838. static irqreturn_t max77823_fg_irq_thread(int irq, void *irq_data)
  1839. {
  1840. struct max77823_fuelgauge_data *fuelgauge = irq_data;
  1841. bool fuel_alerted;
  1842. if (fuelgauge->pdata->fuel_alert_soc >= 0) {
  1843. fuel_alerted =
  1844. max77823_fg_is_fuelalerted(fuelgauge);
  1845. pr_info("%s: Fuel-alert %salerted!\n",
  1846. __func__, fuel_alerted ? "" : "NOT ");
  1847. if (fuel_alerted == fuelgauge->is_fuel_alerted) {
  1848. if (!fuelgauge->pdata->repeated_fuelalert) {
  1849. pr_debug("%s: Fuel-alert Repeated (%d)\n",
  1850. __func__, fuelgauge->is_fuel_alerted);
  1851. return IRQ_HANDLED;
  1852. }
  1853. }
  1854. if (fuel_alerted)
  1855. wake_lock(&fuelgauge->fuel_alert_wake_lock);
  1856. else
  1857. wake_unlock(&fuelgauge->fuel_alert_wake_lock);
  1858. schedule_delayed_work(&fuelgauge->isr_work, 0);
  1859. fuelgauge->is_fuel_alerted = fuel_alerted;
  1860. }
  1861. return IRQ_HANDLED;
  1862. }
  1863. static int max77823_fuelgauge_debugfs_show(struct seq_file *s, void *data)
  1864. {
  1865. struct max77823_fuelgauge_data *fuelgauge = s->private;
  1866. u8 reg;
  1867. u8 reg_data;
  1868. seq_printf(s, "MAX77823 FUELGAUGE IC :\n");
  1869. seq_printf(s, "===================\n");
  1870. for (reg = 0xB0; reg <= 0xC3; reg++) {
  1871. max77823_read_reg(fuelgauge->i2c, reg, &reg_data);
  1872. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  1873. }
  1874. seq_printf(s, "\n");
  1875. return 0;
  1876. }
  1877. static int max77823_fuelgauge_debugfs_open(struct inode *inode, struct file *file)
  1878. {
  1879. return single_open(file, max77823_fuelgauge_debugfs_show, inode->i_private);
  1880. }
  1881. static const struct file_operations max77823_fuelgauge_debugfs_fops = {
  1882. .open = max77823_fuelgauge_debugfs_open,
  1883. .read = seq_read,
  1884. .llseek = seq_lseek,
  1885. .release = single_release,
  1886. };
  1887. #ifdef CONFIG_OF
  1888. static int max77823_fuelgauge_parse_dt(struct max77823_fuelgauge_data *fuelgauge)
  1889. {
  1890. struct device_node *np = of_find_node_by_name(NULL, "max77823-fuelgauge");
  1891. sec_battery_platform_data_t *pdata = fuelgauge->pdata;
  1892. int ret;
  1893. /* reset, irq gpio info */
  1894. if (np == NULL) {
  1895. pr_err("%s np NULL\n", __func__);
  1896. return -1;
  1897. } else {
  1898. ret = of_property_read_u32(np, "fuelgauge,capacity_max",
  1899. &pdata->capacity_max);
  1900. if (ret < 0)
  1901. pr_err("%s error reading capacity_max %d\n", __func__, ret);
  1902. ret = of_property_read_u32(np, "fuelgauge,capacity_max_margin",
  1903. &pdata->capacity_max_margin);
  1904. if (ret < 0)
  1905. pr_err("%s error reading capacity_max_margin %d\n", __func__, ret);
  1906. ret = of_property_read_u32(np, "fuelgauge,capacity_min",
  1907. &pdata->capacity_min);
  1908. if (ret < 0)
  1909. pr_err("%s error reading capacity_min %d\n", __func__, ret);
  1910. ret = of_property_read_u32(np, "fuelgauge,capacity_calculation_type",
  1911. &pdata->capacity_calculation_type);
  1912. if (ret < 0)
  1913. pr_err("%s error reading capacity_calculation_type %d\n",
  1914. __func__, ret);
  1915. ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc",
  1916. &pdata->fuel_alert_soc);
  1917. if (ret < 0)
  1918. pr_err("%s error reading pdata->fuel_alert_soc %d\n",
  1919. __func__, ret);
  1920. pdata->repeated_fuelalert = of_property_read_bool(np,
  1921. "fuelgauge,repeated_fuelalert");
  1922. pdata->jig_irq = of_get_named_gpio(np, "fuelgauge,jig_gpio", 0);
  1923. if (pdata->jig_irq < 0)
  1924. pr_err("%s error reading jig_gpio = %d\n",
  1925. __func__,pdata->jig_irq);
  1926. else
  1927. pdata->jig_irq_attr = IRQF_TRIGGER_RISING;
  1928. pr_info("%s: fg_irq: %d, capacity_max: %d, "
  1929. "cpacity_max_margin: %d, capacity_min: %d,"
  1930. "calculation_type: 0x%x, fuel_alert_soc: %d,"
  1931. "repeated_fuelalert: %d, jig_irq : %d\n", __func__, pdata->fg_irq,
  1932. pdata->capacity_max, pdata->capacity_max_margin,
  1933. pdata->capacity_min, pdata->capacity_calculation_type,
  1934. pdata->fuel_alert_soc, pdata->repeated_fuelalert, pdata->jig_irq);
  1935. }
  1936. return 0;
  1937. }
  1938. #endif
  1939. static int __devinit max77823_fuelgauge_probe(struct platform_device *pdev)
  1940. {
  1941. struct max77823_dev *max77823 = dev_get_drvdata(pdev->dev.parent);
  1942. struct max77823_platform_data *pdata = dev_get_platdata(max77823->dev);
  1943. struct max77823_fuelgauge_data *fuelgauge;
  1944. int ret = 0;
  1945. union power_supply_propval raw_soc_val;
  1946. pr_info("%s: MAX77823 Fuelgauge Driver Loading\n", __func__);
  1947. fuelgauge = kzalloc(sizeof(*fuelgauge), GFP_KERNEL);
  1948. if (!fuelgauge)
  1949. return -ENOMEM;
  1950. pdata->fuelgauge_data = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  1951. if (!pdata->fuelgauge_data)
  1952. return -ENOMEM;
  1953. mutex_init(&fuelgauge->fg_lock);
  1954. fuelgauge->dev = &pdev->dev;
  1955. fuelgauge->pdata = pdata->fuelgauge_data;
  1956. fuelgauge->i2c = max77823->fuelgauge;
  1957. fuelgauge->max77823_pdata = pdata;
  1958. #if defined(CONFIG_OF)
  1959. ret = max77823_fuelgauge_parse_dt(fuelgauge);
  1960. if (ret < 0) {
  1961. pr_err("%s not found charger dt! ret[%d]\n",
  1962. __func__, ret);
  1963. }
  1964. #endif
  1965. board_fuelgauge_init(fuelgauge);
  1966. platform_set_drvdata(pdev, fuelgauge);
  1967. fuelgauge->psy_fg.name = "max77823-fuelgauge";
  1968. fuelgauge->psy_fg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  1969. fuelgauge->psy_fg.get_property = max77823_fg_get_property;
  1970. fuelgauge->psy_fg.set_property = max77823_fg_set_property;
  1971. fuelgauge->psy_fg.properties = max77823_fuelgauge_props;
  1972. fuelgauge->psy_fg.num_properties =
  1973. ARRAY_SIZE(max77823_fuelgauge_props);
  1974. fuelgauge->capacity_max = fuelgauge->pdata->capacity_max;
  1975. #ifdef CONFIG_FUELGAUGE_MAX77823_VOLTAGE_TRACKING
  1976. raw_soc_val.intval = max77823_get_soc(fuelgauge) / 10;
  1977. #else
  1978. raw_soc_val.intval = get_fuelgauge_value(fuelgauge, FG_RAW_SOC);
  1979. #endif
  1980. if(raw_soc_val.intval > fuelgauge->pdata->capacity_max)
  1981. max77823_fg_calculate_dynamic_scale(fuelgauge);
  1982. (void) debugfs_create_file("max77823-fuelgauge-regs",
  1983. S_IRUGO, NULL, (void *)fuelgauge, &max77823_fuelgauge_debugfs_fops);
  1984. if (!max77823_fg_init(fuelgauge)) {
  1985. pr_err("%s: Failed to Initialize Fuelgauge\n", __func__);
  1986. goto err_free;
  1987. }
  1988. ret = power_supply_register(&pdev->dev, &fuelgauge->psy_fg);
  1989. if (ret) {
  1990. pr_err("%s: Failed to Register psy_fg\n", __func__);
  1991. goto err_free;
  1992. }
  1993. if (fuelgauge->pdata->fg_irq) {
  1994. INIT_DELAYED_WORK(&fuelgauge->isr_work, max77823_fg_isr_work);
  1995. ret = request_threaded_irq(fuelgauge->fg_irq,
  1996. NULL, max77823_fg_irq_thread,
  1997. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1998. "fuelgauge-irq", fuelgauge);
  1999. if (ret) {
  2000. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  2001. goto err_supply_unreg;
  2002. }
  2003. ret = enable_irq_wake(fuelgauge->fg_irq);
  2004. if (ret < 0)
  2005. pr_err("%s: Failed to Enable Wakeup Source(%d)\n",
  2006. __func__, ret);
  2007. }
  2008. fuelgauge->is_fuel_alerted = false;
  2009. if (fuelgauge->pdata->fuel_alert_soc >= 0) {
  2010. if (max77823_fg_fuelalert_init(fuelgauge,
  2011. fuelgauge->pdata->fuel_alert_soc))
  2012. wake_lock_init(&fuelgauge->fuel_alert_wake_lock,
  2013. WAKE_LOCK_SUSPEND, "fuel_alerted");
  2014. else {
  2015. pr_err("%s: Failed to Initialize Fuel-alert\n",
  2016. __func__);
  2017. goto err_irq;
  2018. }
  2019. }
  2020. fuelgauge->initial_update_of_soc = true;
  2021. pr_info("%s: MAX77823 Fuelgauge Driver Loaded\n", __func__);
  2022. return 0;
  2023. err_irq:
  2024. if (fuelgauge->fg_irq)
  2025. free_irq(fuelgauge->fg_irq, fuelgauge);
  2026. wake_lock_destroy(&fuelgauge->fuel_alert_wake_lock);
  2027. err_supply_unreg:
  2028. power_supply_unregister(&fuelgauge->psy_fg);
  2029. err_free:
  2030. mutex_destroy(&fuelgauge->fg_lock);
  2031. kfree(fuelgauge);
  2032. return ret;
  2033. }
  2034. static int __devexit max77823_fuelgauge_remove(struct platform_device *pdev)
  2035. {
  2036. struct max77823_fuelgauge_data *fuelgauge =
  2037. platform_get_drvdata(pdev);
  2038. if (fuelgauge->pdata->fuel_alert_soc >= 0)
  2039. wake_lock_destroy(&fuelgauge->fuel_alert_wake_lock);
  2040. return 0;
  2041. }
  2042. static int max77823_fuelgauge_suspend(struct device *dev)
  2043. {
  2044. return 0;
  2045. }
  2046. static int max77823_fuelgauge_resume(struct device *dev)
  2047. {
  2048. struct max77823_fuelgauge_data *fuelgauge = dev_get_drvdata(dev);
  2049. fuelgauge->initial_update_of_soc = true;
  2050. return 0;
  2051. }
  2052. static void max77823_fuelgauge_shutdown(struct device *dev)
  2053. {
  2054. }
  2055. #if defined(CONFIG_OF)
  2056. static struct of_device_id max77823_fuelgauge_dt_ids[] = {
  2057. { .compatible = "samsung,max77823-fuelgauge" },
  2058. { }
  2059. };
  2060. MODULE_DEVICE_TABLE(of, max77823_fuelgauge_dt_ids);
  2061. #endif /* CONFIG_OF */
  2062. static SIMPLE_DEV_PM_OPS(max77823_fuelgauge_pm_ops, max77823_fuelgauge_suspend,
  2063. max77823_fuelgauge_resume);
  2064. static struct platform_driver max77823_fuelgauge_driver = {
  2065. .driver = {
  2066. .name = "max77823-fuelgauge",
  2067. .owner = THIS_MODULE,
  2068. #ifdef CONFIG_PM
  2069. .pm = &max77823_fuelgauge_pm_ops,
  2070. #endif
  2071. .shutdown = max77823_fuelgauge_shutdown,
  2072. #if defined(CONFIG_OF)
  2073. .of_match_table = max77823_fuelgauge_dt_ids,
  2074. #endif /* CONFIG_OF */
  2075. },
  2076. .probe = max77823_fuelgauge_probe,
  2077. .remove = __devexit_p(max77823_fuelgauge_remove),
  2078. };
  2079. static int __init max77823_fuelgauge_init(void)
  2080. {
  2081. pr_info("%s: \n", __func__);
  2082. return platform_driver_register(&max77823_fuelgauge_driver);
  2083. }
  2084. static void __exit max77823_fuelgauge_exit(void)
  2085. {
  2086. platform_driver_unregister(&max77823_fuelgauge_driver);
  2087. }
  2088. module_init(max77823_fuelgauge_init);
  2089. module_exit(max77823_fuelgauge_exit);
  2090. MODULE_DESCRIPTION("Samsung MAX778023 Fuel Gauge Driver");
  2091. MODULE_AUTHOR("Samsung Electronics");
  2092. MODULE_LICENSE("GPL");