mt_battery_common.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516
  1. #include <linux/init.h> /* For init/exit macros */
  2. #include <linux/module.h> /* For MODULE_ marcros */
  3. #include <linux/fs.h>
  4. #include <linux/device.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/device.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/fs.h>
  11. #include <linux/cdev.h>
  12. #include <linux/delay.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/wait.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/poll.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/wakelock.h>
  23. #include <linux/time.h>
  24. #include <linux/mutex.h>
  25. #include <linux/kthread.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/of.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <mt-plat/mt_boot.h>
  34. #include "mt_charging.h"
  35. #include "mt_battery_custom_data.h"
  36. #include "mt_battery_common.h"
  37. #include "mt_battery_meter.h"
  38. #include <linux/irq.h>
  39. #include <linux/reboot.h>
  40. struct battery_common_data g_bat;
  41. /* Battery Notify */
  42. #define BATTERY_NOTIFY_CASE_0001_VCHARGER
  43. #define BATTERY_NOTIFY_CASE_0002_VBATTEMP
  44. /* #define BATTERY_NOTIFY_CASE_0003_ICHARGING */
  45. #define BATTERY_NOTIFY_CASE_0004_VBAT
  46. #define BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME
  47. /* Precise Tunning */
  48. #define BATTERY_AVERAGE_DATA_NUMBER 3
  49. #define BATTERY_AVERAGE_SIZE 30
  50. #define CUST_CAPACITY_OCV2CV_TRANSFORM
  51. /* ////////////////////////////////////////////////////////////////////////////// */
  52. /* Battery Logging Entry */
  53. /* ////////////////////////////////////////////////////////////////////////////// */
  54. int Enable_BATDRV_LOG = BAT_LOG_ERROR;
  55. char proc_bat_data[32];
  56. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  57. /* // Smart Battery Structure */
  58. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  59. struct PMU_ChargerStruct BMT_status;
  60. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  61. /* // Thermal related flags */
  62. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  63. /* 0:nothing, 1:enable batTT&chrTimer, 2:disable batTT&chrTimer, 3:enable batTT, disable chrTimer */
  64. int g_battery_thermal_throttling_flag = 3;
  65. int battery_cmd_thermal_test_mode = 0;
  66. int battery_cmd_thermal_test_mode_value = 0;
  67. int g_battery_tt_check_flag = 0; /* 0:default enable check batteryTT, 1:default disable check batteryTT */
  68. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  69. /* // Global Variable */
  70. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  71. #ifdef CONFIG_OF
  72. static const struct of_device_id mt_battery_common_id[] = {
  73. {.compatible = "mediatek,battery_common"},
  74. {},
  75. };
  76. MODULE_DEVICE_TABLE(of, mt_battery_common_id);
  77. #endif
  78. struct wake_lock battery_suspend_lock;
  79. unsigned int g_BatteryNotifyCode = 0x0000;
  80. unsigned int g_BN_TestMode = 0x0000;
  81. bool g_bat_init_flag = 0;
  82. bool g_call_state = CALL_IDLE;
  83. bool g_charging_full_reset_bat_meter = false;
  84. int g_platform_boot_mode = 0;
  85. static bool battery_meter_initilized;
  86. bool g_cmd_hold_charging = false;
  87. s32 g_custom_charging_current = -1;
  88. bool battery_suspended = false;
  89. bool g_refresh_ui_soc = false;
  90. static bool fg_battery_shutdown;
  91. struct mt_battery_charging_custom_data *p_bat_charging_data;
  92. static struct mt_battery_charging_custom_data default_charging_data = {
  93. .talking_recharge_voltage = 3800,
  94. .talking_sync_time = 60,
  95. /* Battery Temperature Protection */
  96. .max_discharge_temperature = 60,
  97. .min_discharge_temperature = -10,
  98. .max_charge_temperature = 50,
  99. .min_charge_temperature = 0,
  100. .err_charge_temperature = 0xFF,
  101. .use_avg_temperature = 1,
  102. /* Linear Charging Threshold */
  103. .v_pre2cc_thres = 3400, /* mV */
  104. .v_cc2topoff_thres = 4050,
  105. .recharging_voltage = 4110,
  106. .charging_full_current = 150, /* mA */
  107. /* CONFIG_USB_IF */
  108. .usb_charger_current_suspend = 0,
  109. .usb_charger_current_unconfigured = CHARGE_CURRENT_70_00_MA,
  110. .usb_charger_current_configured = CHARGE_CURRENT_500_00_MA,
  111. .usb_charger_current = CHARGE_CURRENT_500_00_MA,
  112. .ac_charger_current = 204800,
  113. .non_std_ac_charger_current = CHARGE_CURRENT_500_00_MA,
  114. .charging_host_charger_current = CHARGE_CURRENT_650_00_MA,
  115. .apple_0_5a_charger_current = CHARGE_CURRENT_500_00_MA,
  116. .apple_1_0a_charger_current = CHARGE_CURRENT_1000_00_MA,
  117. .apple_2_1a_charger_current = CHARGE_CURRENT_2000_00_MA,
  118. /* Charger error check */
  119. /* BAT_LOW_TEMP_PROTECT_ENABLE */
  120. .v_charger_enable = 0, /* 1:ON , 0:OFF */
  121. .v_charger_max = 6500, /* 6.5 V */
  122. .v_charger_min = 4400, /* 4.4 V */
  123. /* Tracking time */
  124. .onehundred_percent_tracking_time = 10, /* 10 second */
  125. .npercent_tracking_time = 20, /* 20 second */
  126. .sync_to_real_tracking_time = 30, /* 30 second */
  127. /* JEITA parameter */
  128. .cust_soc_jeita_sync_time = 30,
  129. .jeita_recharge_voltage = 4110, /* for linear charging */
  130. .jeita_temp_above_pos_60_cv_voltage = BATTERY_VOLT_04_000000_V,
  131. .jeita_temp_pos_45_to_pos_60_cv_voltage = BATTERY_VOLT_04_000000_V,
  132. .jeita_temp_pos_10_to_pos_45_cv_voltage = BATTERY_VOLT_04_200000_V,
  133. .jeita_temp_pos_0_to_pos_10_cv_voltage = BATTERY_VOLT_04_000000_V,
  134. .jeita_temp_neg_10_to_pos_0_cv_voltage = BATTERY_VOLT_04_000000_V,
  135. .jeita_temp_below_neg_10_cv_voltage = BATTERY_VOLT_04_000000_V,
  136. .temp_pos_60_threshold = 50,
  137. .temp_pos_60_thres_minus_x_degree = 47,
  138. .temp_pos_45_threshold = 45,
  139. .temp_pos_45_thres_minus_x_degree = 39,
  140. .temp_pos_10_threshold = 10,
  141. .temp_pos_10_thres_plus_x_degree = 16,
  142. .temp_pos_0_threshold = 0,
  143. .temp_pos_0_thres_plus_x_degree = 6,
  144. .temp_neg_10_threshold = 0,
  145. .temp_neg_10_thres_plus_x_degree = 0,
  146. /* For JEITA Linear Charging Only */
  147. .jeita_neg_10_to_pos_0_full_current = 120, /* mA */
  148. .jeita_temp_pos_45_to_pos_60_recharge_voltage = 4000,
  149. .jeita_temp_pos_10_to_pos_45_recharge_voltage = 4100,
  150. .jeita_temp_pos_0_to_pos_10_recharge_voltage = 4000,
  151. .jeita_temp_neg_10_to_pos_0_recharge_voltage = 3800,
  152. .jeita_temp_pos_45_to_pos_60_cc2topoff_threshold = 4050,
  153. .jeita_temp_pos_10_to_pos_45_cc2topoff_threshold = 4050,
  154. .jeita_temp_pos_0_to_pos_10_cc2topoff_threshold = 4050,
  155. .jeita_temp_neg_10_to_pos_0_cc2topoff_threshold = 3850,
  156. /* For Pump Express Plus */
  157. .ta_start_battery_soc = 1,
  158. .ta_stop_battery_soc = 95,
  159. .ta_ac_9v_input_current = CHARGE_CURRENT_1500_00_MA,
  160. .ta_ac_7v_input_current = CHARGE_CURRENT_1500_00_MA,
  161. .ta_ac_charging_current = CHARGE_CURRENT_2200_00_MA,
  162. .ta_9v_support = 1,
  163. };
  164. /* ////////////////////////////////////////////////////////////////////////////// */
  165. /* Integrate with NVRAM */
  166. /* ////////////////////////////////////////////////////////////////////////////// */
  167. #define ADC_CALI_DEVNAME "MT_pmic_adc_cali"
  168. #define TEST_ADC_CALI_PRINT _IO('k', 0)
  169. #define SET_ADC_CALI_Slop _IOW('k', 1, int)
  170. #define SET_ADC_CALI_Offset _IOW('k', 2, int)
  171. #define SET_ADC_CALI_Cal _IOW('k', 3, int)
  172. #define ADC_CHANNEL_READ _IOW('k', 4, int)
  173. #define BAT_STATUS_READ _IOW('k', 5, int)
  174. #define Set_Charger_Current _IOW('k', 6, int)
  175. /* add for meta tool----------------------------------------- */
  176. #define Get_META_BAT_VOL _IOW('k', 10, int)
  177. #define Get_META_BAT_SOC _IOW('k', 11, int)
  178. /* add for meta tool----------------------------------------- */
  179. static struct class *adc_cali_class;
  180. static int adc_cali_major;
  181. static dev_t adc_cali_devno;
  182. static struct cdev *adc_cali_cdev;
  183. int adc_cali_slop[14] = {
  184. 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000
  185. };
  186. int adc_cali_offset[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  187. int adc_cali_cal[1] = { 0 };
  188. int battery_in_data[1] = { 0 };
  189. int battery_out_data[1] = { 0 };
  190. int charging_level_data[1] = { 0 };
  191. bool g_ADC_Cali = false;
  192. bool g_ftm_battery_flag = false;
  193. static bool need_clear_current_window;
  194. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  195. /* // Thread related */
  196. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  197. #define BAT_MS_TO_NS(x) (x * 1000 * 1000)
  198. static atomic_t bat_thread_wakeup;
  199. static bool chr_wake_up_bat; /* charger in/out to wake up battery thread */
  200. static bool bat_meter_timeout;
  201. static DEFINE_MUTEX(bat_mutex);
  202. static DEFINE_MUTEX(charger_type_mutex);
  203. static DECLARE_WAIT_QUEUE_HEAD(bat_thread_wq);
  204. /* ////////////////////////////////////////////////////////////////////////////// */
  205. /* FOR ANDROID BATTERY SERVICE */
  206. /* ////////////////////////////////////////////////////////////////////////////// */
  207. struct ac_data {
  208. struct power_supply psy;
  209. int AC_ONLINE;
  210. };
  211. struct usb_data {
  212. struct power_supply psy;
  213. int USB_ONLINE;
  214. };
  215. struct battery_data {
  216. struct power_supply psy;
  217. int BAT_STATUS;
  218. int BAT_HEALTH;
  219. int BAT_PRESENT;
  220. int BAT_TECHNOLOGY;
  221. int BAT_CAPACITY;
  222. /* Add for Battery Service */
  223. int BAT_VOLTAGE_NOW;
  224. int BAT_VOLTAGE_AVG;
  225. int BAT_TEMP;
  226. /* Add for EM */
  227. int BAT_TemperatureR;
  228. int BAT_TempBattVoltage;
  229. int BAT_InstatVolt;
  230. int BAT_BatteryAverageCurrent;
  231. int BAT_BatterySenseVoltage;
  232. int BAT_ISenseVoltage;
  233. int BAT_ChargerVoltage;
  234. };
  235. static enum power_supply_property ac_props[] = {
  236. POWER_SUPPLY_PROP_ONLINE,
  237. };
  238. static enum power_supply_property usb_props[] = {
  239. POWER_SUPPLY_PROP_ONLINE,
  240. };
  241. static enum power_supply_property battery_props[] = {
  242. POWER_SUPPLY_PROP_STATUS,
  243. POWER_SUPPLY_PROP_HEALTH,
  244. POWER_SUPPLY_PROP_PRESENT,
  245. POWER_SUPPLY_PROP_TECHNOLOGY,
  246. POWER_SUPPLY_PROP_CAPACITY,
  247. /* Add for Battery Service */
  248. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  249. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  250. POWER_SUPPLY_PROP_TEMP,
  251. /* Add for EM
  252. POWER_SUPPLY_PROP_TemperatureR,
  253. POWER_SUPPLY_PROP_TempBattVoltage,
  254. POWER_SUPPLY_PROP_InstatVolt,
  255. POWER_SUPPLY_PROP_BatteryAverageCurrent,
  256. POWER_SUPPLY_PROP_BatterySenseVoltage,
  257. POWER_SUPPLY_PROP_ISenseVoltage,
  258. POWER_SUPPLY_PROP_ChargerVoltage,
  259. */
  260. };
  261. int read_tbat_value(void)
  262. {
  263. return BMT_status.temperature;
  264. }
  265. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  266. /* // PMIC PCHR Related APIs */
  267. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  268. bool upmu_is_chr_det(void)
  269. {
  270. #if defined(CONFIG_POWER_EXT)
  271. /* return true; */
  272. return bat_charger_get_detect_status();
  273. #else
  274. u32 tmp32;
  275. tmp32 = bat_charger_get_detect_status();
  276. if (tmp32 == 0)
  277. return false;
  278. if (mt_usb_is_device()) {
  279. battery_log(BAT_LOG_FULL, "[upmu_is_chr_det] Charger exist and USB is not host\n");
  280. return true;
  281. }
  282. battery_log(BAT_LOG_FULL, "[upmu_is_chr_det] Charger exist but USB is host\n");
  283. return false;
  284. #endif
  285. }
  286. EXPORT_SYMBOL(upmu_is_chr_det);
  287. static inline void _do_wake_up_bat_thread(void)
  288. {
  289. atomic_inc(&bat_thread_wakeup);
  290. wake_up(&bat_thread_wq);
  291. }
  292. /* for charger plug-in/out */
  293. void wake_up_bat(void)
  294. {
  295. pr_debug("%s:\n", __func__);
  296. chr_wake_up_bat = true;
  297. _do_wake_up_bat_thread();
  298. }
  299. EXPORT_SYMBOL(wake_up_bat);
  300. /* for meter update */
  301. static void wake_up_bat_update_meter(void)
  302. {
  303. pr_debug("%s:\n", __func__);
  304. bat_meter_timeout = true;
  305. _do_wake_up_bat_thread();
  306. }
  307. static ssize_t bat_log_write(struct file *filp, const char __user *buff, size_t len, loff_t *data)
  308. {
  309. if (copy_from_user(&proc_bat_data, buff, len)) {
  310. battery_log(BAT_LOG_FULL, "bat_log_write error.\n");
  311. return -EFAULT;
  312. }
  313. if (proc_bat_data[0] == '1') {
  314. battery_log(BAT_LOG_CRTI, "enable battery driver log system\n");
  315. Enable_BATDRV_LOG = 1;
  316. } else if (proc_bat_data[0] == '2') {
  317. battery_log(BAT_LOG_CRTI, "enable battery driver log system:2\n");
  318. Enable_BATDRV_LOG = 2;
  319. } else {
  320. battery_log(BAT_LOG_CRTI, "Disable battery driver log system\n");
  321. Enable_BATDRV_LOG = 0;
  322. }
  323. return len;
  324. }
  325. static const struct file_operations bat_proc_fops = {
  326. .write = bat_log_write,
  327. };
  328. int init_proc_log(void)
  329. {
  330. int ret = 0;
  331. proc_create("batdrv_log", 0644, NULL, &bat_proc_fops);
  332. battery_log(BAT_LOG_CRTI, "proc_create bat_proc_fops\n");
  333. return ret;
  334. }
  335. static int ac_get_property(struct power_supply *psy,
  336. enum power_supply_property psp, union power_supply_propval *val)
  337. {
  338. int ret = 0;
  339. struct ac_data *data = container_of(psy, struct ac_data, psy);
  340. switch (psp) {
  341. case POWER_SUPPLY_PROP_ONLINE:
  342. val->intval = data->AC_ONLINE;
  343. break;
  344. default:
  345. ret = -EINVAL;
  346. break;
  347. }
  348. return ret;
  349. }
  350. static int usb_get_property(struct power_supply *psy,
  351. enum power_supply_property psp, union power_supply_propval *val)
  352. {
  353. int ret = 0;
  354. struct usb_data *data = container_of(psy, struct usb_data, psy);
  355. switch (psp) {
  356. case POWER_SUPPLY_PROP_ONLINE:
  357. #if defined(CONFIG_POWER_EXT)
  358. /* #if 0 */
  359. data->USB_ONLINE = 1;
  360. val->intval = data->USB_ONLINE;
  361. #else
  362. val->intval = data->USB_ONLINE;
  363. #endif
  364. break;
  365. default:
  366. ret = -EINVAL;
  367. break;
  368. }
  369. return ret;
  370. }
  371. static int battery_get_property(struct power_supply *psy,
  372. enum power_supply_property psp, union power_supply_propval *val)
  373. {
  374. int ret = 0;
  375. struct battery_data *data = container_of(psy, struct battery_data, psy);
  376. switch (psp) {
  377. case POWER_SUPPLY_PROP_STATUS:
  378. val->intval = data->BAT_STATUS;
  379. break;
  380. case POWER_SUPPLY_PROP_HEALTH:
  381. val->intval = data->BAT_HEALTH;
  382. break;
  383. case POWER_SUPPLY_PROP_PRESENT:
  384. val->intval = data->BAT_PRESENT;
  385. break;
  386. case POWER_SUPPLY_PROP_TECHNOLOGY:
  387. val->intval = data->BAT_TECHNOLOGY;
  388. break;
  389. case POWER_SUPPLY_PROP_CAPACITY:
  390. val->intval = data->BAT_CAPACITY;
  391. break;
  392. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  393. val->intval = data->BAT_VOLTAGE_NOW;
  394. break;
  395. case POWER_SUPPLY_PROP_TEMP:
  396. val->intval = data->BAT_TEMP;
  397. break;
  398. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  399. val->intval = data->BAT_VOLTAGE_AVG;
  400. break;
  401. /*
  402. case POWER_SUPPLY_PROP_TemperatureR:
  403. val->intval = data->BAT_TemperatureR;
  404. break;
  405. case POWER_SUPPLY_PROP_TempBattVoltage:
  406. val->intval = data->BAT_TempBattVoltage;
  407. break;
  408. case POWER_SUPPLY_PROP_InstatVolt:
  409. val->intval = data->BAT_InstatVolt;
  410. break;
  411. case POWER_SUPPLY_PROP_BatteryAverageCurrent:
  412. val->intval = data->BAT_BatteryAverageCurrent;
  413. break;
  414. case POWER_SUPPLY_PROP_BatterySenseVoltage:
  415. val->intval = data->BAT_BatterySenseVoltage;
  416. break;
  417. case POWER_SUPPLY_PROP_ISenseVoltage:
  418. val->intval = data->BAT_ISenseVoltage;
  419. break;
  420. case POWER_SUPPLY_PROP_ChargerVoltage:
  421. val->intval = data->BAT_ChargerVoltage;
  422. break;
  423. */
  424. default:
  425. ret = -EINVAL;
  426. break;
  427. }
  428. return ret;
  429. }
  430. /* ac_data initialization */
  431. static struct ac_data ac_main = {
  432. .psy = {
  433. .name = "ac",
  434. .type = POWER_SUPPLY_TYPE_MAINS,
  435. .properties = ac_props,
  436. .num_properties = ARRAY_SIZE(ac_props),
  437. .get_property = ac_get_property,
  438. },
  439. .AC_ONLINE = 0,
  440. };
  441. /* usb_data initialization */
  442. static struct usb_data usb_main = {
  443. .psy = {
  444. .name = "usb",
  445. .type = POWER_SUPPLY_TYPE_USB,
  446. .properties = usb_props,
  447. .num_properties = ARRAY_SIZE(usb_props),
  448. .get_property = usb_get_property,
  449. },
  450. .USB_ONLINE = 0,
  451. };
  452. /* battery_data initialization */
  453. static struct battery_data battery_main = {
  454. .psy = {
  455. .name = "battery",
  456. .type = POWER_SUPPLY_TYPE_BATTERY,
  457. .properties = battery_props,
  458. .num_properties = ARRAY_SIZE(battery_props),
  459. .get_property = battery_get_property,
  460. },
  461. /* CC: modify to have a full power supply status */
  462. #if defined(CONFIG_POWER_EXT)
  463. .BAT_STATUS = POWER_SUPPLY_STATUS_FULL,
  464. .BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
  465. .BAT_PRESENT = 1,
  466. .BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
  467. .BAT_CAPACITY = 100,
  468. .BAT_VOLTAGE_NOW = 4200000,
  469. .BAT_VOLTAGE_AVG = 4200000,
  470. .BAT_TEMP = 22,
  471. #else
  472. .BAT_STATUS = POWER_SUPPLY_STATUS_NOT_CHARGING,
  473. .BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD,
  474. .BAT_PRESENT = 1,
  475. .BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION,
  476. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  477. .BAT_CAPACITY = -1,
  478. #else
  479. .BAT_CAPACITY = 50,
  480. #endif
  481. .BAT_VOLTAGE_NOW = 0,
  482. .BAT_VOLTAGE_AVG = 0,
  483. .BAT_TEMP = 0,
  484. #endif
  485. };
  486. #if !defined(CONFIG_POWER_EXT)
  487. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  488. /* // Create File For EM : ADC_Charger_Voltage */
  489. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  490. static ssize_t show_ADC_Charger_Voltage(struct device *dev, struct device_attribute *attr,
  491. char *buf)
  492. {
  493. battery_log(BAT_LOG_CRTI, "[EM] show_ADC_Charger_Voltage : %d\n", BMT_status.charger_vol);
  494. return sprintf(buf, "%d\n", BMT_status.charger_vol);
  495. }
  496. static ssize_t store_ADC_Charger_Voltage(struct device *dev, struct device_attribute *attr,
  497. const char *buf, size_t size)
  498. {
  499. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  500. return size;
  501. }
  502. static DEVICE_ATTR(ADC_Charger_Voltage, 0664, show_ADC_Charger_Voltage, store_ADC_Charger_Voltage);
  503. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  504. /* // Create File For EM : ADC_Channel_0_Slope */
  505. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  506. static ssize_t show_ADC_Channel_0_Slope(struct device *dev, struct device_attribute *attr,
  507. char *buf)
  508. {
  509. int ret_value = 1;
  510. ret_value = (*(adc_cali_slop + 0));
  511. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_0_Slope : %d\n", ret_value);
  512. return sprintf(buf, "%u\n", ret_value);
  513. }
  514. static ssize_t store_ADC_Channel_0_Slope(struct device *dev, struct device_attribute *attr,
  515. const char *buf, size_t size)
  516. {
  517. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  518. return size;
  519. }
  520. static DEVICE_ATTR(ADC_Channel_0_Slope, 0664, show_ADC_Channel_0_Slope, store_ADC_Channel_0_Slope);
  521. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  522. /* // Create File For EM : ADC_Channel_1_Slope */
  523. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  524. static ssize_t show_ADC_Channel_1_Slope(struct device *dev, struct device_attribute *attr,
  525. char *buf)
  526. {
  527. int ret_value = 1;
  528. ret_value = (*(adc_cali_slop + 1));
  529. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_1_Slope : %d\n", ret_value);
  530. return sprintf(buf, "%u\n", ret_value);
  531. }
  532. static ssize_t store_ADC_Channel_1_Slope(struct device *dev, struct device_attribute *attr,
  533. const char *buf, size_t size)
  534. {
  535. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  536. return size;
  537. }
  538. static DEVICE_ATTR(ADC_Channel_1_Slope, 0664, show_ADC_Channel_1_Slope, store_ADC_Channel_1_Slope);
  539. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  540. /* // Create File For EM : ADC_Channel_2_Slope */
  541. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  542. static ssize_t show_ADC_Channel_2_Slope(struct device *dev, struct device_attribute *attr,
  543. char *buf)
  544. {
  545. int ret_value = 1;
  546. ret_value = (*(adc_cali_slop + 2));
  547. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_2_Slope : %d\n", ret_value);
  548. return sprintf(buf, "%u\n", ret_value);
  549. }
  550. static ssize_t store_ADC_Channel_2_Slope(struct device *dev, struct device_attribute *attr,
  551. const char *buf, size_t size)
  552. {
  553. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  554. return size;
  555. }
  556. static DEVICE_ATTR(ADC_Channel_2_Slope, 0664, show_ADC_Channel_2_Slope, store_ADC_Channel_2_Slope);
  557. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  558. /* // Create File For EM : ADC_Channel_3_Slope */
  559. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  560. static ssize_t show_ADC_Channel_3_Slope(struct device *dev, struct device_attribute *attr,
  561. char *buf)
  562. {
  563. int ret_value = 1;
  564. ret_value = (*(adc_cali_slop + 3));
  565. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_3_Slope : %d\n", ret_value);
  566. return sprintf(buf, "%u\n", ret_value);
  567. }
  568. static ssize_t store_ADC_Channel_3_Slope(struct device *dev, struct device_attribute *attr,
  569. const char *buf, size_t size)
  570. {
  571. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  572. return size;
  573. }
  574. static DEVICE_ATTR(ADC_Channel_3_Slope, 0664, show_ADC_Channel_3_Slope, store_ADC_Channel_3_Slope);
  575. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  576. /* // Create File For EM : ADC_Channel_4_Slope */
  577. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  578. static ssize_t show_ADC_Channel_4_Slope(struct device *dev, struct device_attribute *attr,
  579. char *buf)
  580. {
  581. int ret_value = 1;
  582. ret_value = (*(adc_cali_slop + 4));
  583. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_4_Slope : %d\n", ret_value);
  584. return sprintf(buf, "%u\n", ret_value);
  585. }
  586. static ssize_t store_ADC_Channel_4_Slope(struct device *dev, struct device_attribute *attr,
  587. const char *buf, size_t size)
  588. {
  589. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  590. return size;
  591. }
  592. static DEVICE_ATTR(ADC_Channel_4_Slope, 0664, show_ADC_Channel_4_Slope, store_ADC_Channel_4_Slope);
  593. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  594. /* // Create File For EM : ADC_Channel_5_Slope */
  595. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  596. static ssize_t show_ADC_Channel_5_Slope(struct device *dev, struct device_attribute *attr,
  597. char *buf)
  598. {
  599. int ret_value = 1;
  600. ret_value = (*(adc_cali_slop + 5));
  601. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_5_Slope : %d\n", ret_value);
  602. return sprintf(buf, "%u\n", ret_value);
  603. }
  604. static ssize_t store_ADC_Channel_5_Slope(struct device *dev, struct device_attribute *attr,
  605. const char *buf, size_t size)
  606. {
  607. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  608. return size;
  609. }
  610. static DEVICE_ATTR(ADC_Channel_5_Slope, 0664, show_ADC_Channel_5_Slope, store_ADC_Channel_5_Slope);
  611. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  612. /* // Create File For EM : ADC_Channel_6_Slope */
  613. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  614. static ssize_t show_ADC_Channel_6_Slope(struct device *dev, struct device_attribute *attr,
  615. char *buf)
  616. {
  617. int ret_value = 1;
  618. ret_value = (*(adc_cali_slop + 6));
  619. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_6_Slope : %d\n", ret_value);
  620. return sprintf(buf, "%u\n", ret_value);
  621. }
  622. static ssize_t store_ADC_Channel_6_Slope(struct device *dev, struct device_attribute *attr,
  623. const char *buf, size_t size)
  624. {
  625. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  626. return size;
  627. }
  628. static DEVICE_ATTR(ADC_Channel_6_Slope, 0664, show_ADC_Channel_6_Slope, store_ADC_Channel_6_Slope);
  629. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  630. /* // Create File For EM : ADC_Channel_7_Slope */
  631. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  632. static ssize_t show_ADC_Channel_7_Slope(struct device *dev, struct device_attribute *attr,
  633. char *buf)
  634. {
  635. int ret_value = 1;
  636. ret_value = (*(adc_cali_slop + 7));
  637. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_7_Slope : %d\n", ret_value);
  638. return sprintf(buf, "%u\n", ret_value);
  639. }
  640. static ssize_t store_ADC_Channel_7_Slope(struct device *dev, struct device_attribute *attr,
  641. const char *buf, size_t size)
  642. {
  643. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  644. return size;
  645. }
  646. static DEVICE_ATTR(ADC_Channel_7_Slope, 0664, show_ADC_Channel_7_Slope, store_ADC_Channel_7_Slope);
  647. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  648. /* // Create File For EM : ADC_Channel_8_Slope */
  649. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  650. static ssize_t show_ADC_Channel_8_Slope(struct device *dev, struct device_attribute *attr,
  651. char *buf)
  652. {
  653. int ret_value = 1;
  654. ret_value = (*(adc_cali_slop + 8));
  655. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_8_Slope : %d\n", ret_value);
  656. return sprintf(buf, "%u\n", ret_value);
  657. }
  658. static ssize_t store_ADC_Channel_8_Slope(struct device *dev, struct device_attribute *attr,
  659. const char *buf, size_t size)
  660. {
  661. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  662. return size;
  663. }
  664. static DEVICE_ATTR(ADC_Channel_8_Slope, 0664, show_ADC_Channel_8_Slope, store_ADC_Channel_8_Slope);
  665. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  666. /* // Create File For EM : ADC_Channel_9_Slope */
  667. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  668. static ssize_t show_ADC_Channel_9_Slope(struct device *dev, struct device_attribute *attr,
  669. char *buf)
  670. {
  671. int ret_value = 1;
  672. ret_value = (*(adc_cali_slop + 9));
  673. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_9_Slope : %d\n", ret_value);
  674. return sprintf(buf, "%u\n", ret_value);
  675. }
  676. static ssize_t store_ADC_Channel_9_Slope(struct device *dev, struct device_attribute *attr,
  677. const char *buf, size_t size)
  678. {
  679. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  680. return size;
  681. }
  682. static DEVICE_ATTR(ADC_Channel_9_Slope, 0664, show_ADC_Channel_9_Slope, store_ADC_Channel_9_Slope);
  683. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  684. /* // Create File For EM : ADC_Channel_10_Slope */
  685. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  686. static ssize_t show_ADC_Channel_10_Slope(struct device *dev, struct device_attribute *attr,
  687. char *buf)
  688. {
  689. int ret_value = 1;
  690. ret_value = (*(adc_cali_slop + 10));
  691. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_10_Slope : %d\n", ret_value);
  692. return sprintf(buf, "%u\n", ret_value);
  693. }
  694. static ssize_t store_ADC_Channel_10_Slope(struct device *dev, struct device_attribute *attr,
  695. const char *buf, size_t size)
  696. {
  697. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  698. return size;
  699. }
  700. static DEVICE_ATTR(ADC_Channel_10_Slope, 0664, show_ADC_Channel_10_Slope,
  701. store_ADC_Channel_10_Slope);
  702. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  703. /* // Create File For EM : ADC_Channel_11_Slope */
  704. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  705. static ssize_t show_ADC_Channel_11_Slope(struct device *dev, struct device_attribute *attr,
  706. char *buf)
  707. {
  708. int ret_value = 1;
  709. ret_value = (*(adc_cali_slop + 11));
  710. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_11_Slope : %d\n", ret_value);
  711. return sprintf(buf, "%u\n", ret_value);
  712. }
  713. static ssize_t store_ADC_Channel_11_Slope(struct device *dev, struct device_attribute *attr,
  714. const char *buf, size_t size)
  715. {
  716. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  717. return size;
  718. }
  719. static DEVICE_ATTR(ADC_Channel_11_Slope, 0664, show_ADC_Channel_11_Slope,
  720. store_ADC_Channel_11_Slope);
  721. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  722. /* // Create File For EM : ADC_Channel_12_Slope */
  723. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  724. static ssize_t show_ADC_Channel_12_Slope(struct device *dev, struct device_attribute *attr,
  725. char *buf)
  726. {
  727. int ret_value = 1;
  728. ret_value = (*(adc_cali_slop + 12));
  729. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_12_Slope : %d\n", ret_value);
  730. return sprintf(buf, "%u\n", ret_value);
  731. }
  732. static ssize_t store_ADC_Channel_12_Slope(struct device *dev, struct device_attribute *attr,
  733. const char *buf, size_t size)
  734. {
  735. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  736. return size;
  737. }
  738. static DEVICE_ATTR(ADC_Channel_12_Slope, 0664, show_ADC_Channel_12_Slope,
  739. store_ADC_Channel_12_Slope);
  740. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  741. /* // Create File For EM : ADC_Channel_13_Slope */
  742. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  743. static ssize_t show_ADC_Channel_13_Slope(struct device *dev, struct device_attribute *attr,
  744. char *buf)
  745. {
  746. int ret_value = 1;
  747. ret_value = (*(adc_cali_slop + 13));
  748. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_13_Slope : %d\n", ret_value);
  749. return sprintf(buf, "%u\n", ret_value);
  750. }
  751. static ssize_t store_ADC_Channel_13_Slope(struct device *dev, struct device_attribute *attr,
  752. const char *buf, size_t size)
  753. {
  754. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  755. return size;
  756. }
  757. static DEVICE_ATTR(ADC_Channel_13_Slope, 0664, show_ADC_Channel_13_Slope,
  758. store_ADC_Channel_13_Slope);
  759. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  760. /* // Create File For EM : ADC_Channel_0_Offset */
  761. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  762. static ssize_t show_ADC_Channel_0_Offset(struct device *dev, struct device_attribute *attr,
  763. char *buf)
  764. {
  765. int ret_value = 1;
  766. ret_value = (*(adc_cali_offset + 0));
  767. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_0_Offset : %d\n", ret_value);
  768. return sprintf(buf, "%u\n", ret_value);
  769. }
  770. static ssize_t store_ADC_Channel_0_Offset(struct device *dev, struct device_attribute *attr,
  771. const char *buf, size_t size)
  772. {
  773. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  774. return size;
  775. }
  776. static DEVICE_ATTR(ADC_Channel_0_Offset, 0664, show_ADC_Channel_0_Offset,
  777. store_ADC_Channel_0_Offset);
  778. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  779. /* // Create File For EM : ADC_Channel_1_Offset */
  780. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  781. static ssize_t show_ADC_Channel_1_Offset(struct device *dev, struct device_attribute *attr,
  782. char *buf)
  783. {
  784. int ret_value = 1;
  785. ret_value = (*(adc_cali_offset + 1));
  786. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_1_Offset : %d\n", ret_value);
  787. return sprintf(buf, "%u\n", ret_value);
  788. }
  789. static ssize_t store_ADC_Channel_1_Offset(struct device *dev, struct device_attribute *attr,
  790. const char *buf, size_t size)
  791. {
  792. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  793. return size;
  794. }
  795. static DEVICE_ATTR(ADC_Channel_1_Offset, 0664, show_ADC_Channel_1_Offset,
  796. store_ADC_Channel_1_Offset);
  797. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  798. /* // Create File For EM : ADC_Channel_2_Offset */
  799. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  800. static ssize_t show_ADC_Channel_2_Offset(struct device *dev, struct device_attribute *attr,
  801. char *buf)
  802. {
  803. int ret_value = 1;
  804. ret_value = (*(adc_cali_offset + 2));
  805. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_2_Offset : %d\n", ret_value);
  806. return sprintf(buf, "%u\n", ret_value);
  807. }
  808. static ssize_t store_ADC_Channel_2_Offset(struct device *dev, struct device_attribute *attr,
  809. const char *buf, size_t size)
  810. {
  811. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  812. return size;
  813. }
  814. static DEVICE_ATTR(ADC_Channel_2_Offset, 0664, show_ADC_Channel_2_Offset,
  815. store_ADC_Channel_2_Offset);
  816. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  817. /* // Create File For EM : ADC_Channel_3_Offset */
  818. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  819. static ssize_t show_ADC_Channel_3_Offset(struct device *dev, struct device_attribute *attr,
  820. char *buf)
  821. {
  822. int ret_value = 1;
  823. ret_value = (*(adc_cali_offset + 3));
  824. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_3_Offset : %d\n", ret_value);
  825. return sprintf(buf, "%u\n", ret_value);
  826. }
  827. static ssize_t store_ADC_Channel_3_Offset(struct device *dev, struct device_attribute *attr,
  828. const char *buf, size_t size)
  829. {
  830. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  831. return size;
  832. }
  833. static DEVICE_ATTR(ADC_Channel_3_Offset, 0664, show_ADC_Channel_3_Offset,
  834. store_ADC_Channel_3_Offset);
  835. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  836. /* // Create File For EM : ADC_Channel_4_Offset */
  837. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  838. static ssize_t show_ADC_Channel_4_Offset(struct device *dev, struct device_attribute *attr,
  839. char *buf)
  840. {
  841. int ret_value = 1;
  842. ret_value = (*(adc_cali_offset + 4));
  843. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_4_Offset : %d\n", ret_value);
  844. return sprintf(buf, "%u\n", ret_value);
  845. }
  846. static ssize_t store_ADC_Channel_4_Offset(struct device *dev, struct device_attribute *attr,
  847. const char *buf, size_t size)
  848. {
  849. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  850. return size;
  851. }
  852. static DEVICE_ATTR(ADC_Channel_4_Offset, 0664, show_ADC_Channel_4_Offset,
  853. store_ADC_Channel_4_Offset);
  854. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  855. /* // Create File For EM : ADC_Channel_5_Offset */
  856. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  857. static ssize_t show_ADC_Channel_5_Offset(struct device *dev, struct device_attribute *attr,
  858. char *buf)
  859. {
  860. int ret_value = 1;
  861. ret_value = (*(adc_cali_offset + 5));
  862. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_5_Offset : %d\n", ret_value);
  863. return sprintf(buf, "%u\n", ret_value);
  864. }
  865. static ssize_t store_ADC_Channel_5_Offset(struct device *dev, struct device_attribute *attr,
  866. const char *buf, size_t size)
  867. {
  868. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  869. return size;
  870. }
  871. static DEVICE_ATTR(ADC_Channel_5_Offset, 0664, show_ADC_Channel_5_Offset,
  872. store_ADC_Channel_5_Offset);
  873. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  874. /* // Create File For EM : ADC_Channel_6_Offset */
  875. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  876. static ssize_t show_ADC_Channel_6_Offset(struct device *dev, struct device_attribute *attr,
  877. char *buf)
  878. {
  879. int ret_value = 1;
  880. ret_value = (*(adc_cali_offset + 6));
  881. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_6_Offset : %d\n", ret_value);
  882. return sprintf(buf, "%u\n", ret_value);
  883. }
  884. static ssize_t store_ADC_Channel_6_Offset(struct device *dev, struct device_attribute *attr,
  885. const char *buf, size_t size)
  886. {
  887. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  888. return size;
  889. }
  890. static DEVICE_ATTR(ADC_Channel_6_Offset, 0664, show_ADC_Channel_6_Offset,
  891. store_ADC_Channel_6_Offset);
  892. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  893. /* // Create File For EM : ADC_Channel_7_Offset */
  894. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  895. static ssize_t show_ADC_Channel_7_Offset(struct device *dev, struct device_attribute *attr,
  896. char *buf)
  897. {
  898. int ret_value = 1;
  899. ret_value = (*(adc_cali_offset + 7));
  900. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_7_Offset : %d\n", ret_value);
  901. return sprintf(buf, "%u\n", ret_value);
  902. }
  903. static ssize_t store_ADC_Channel_7_Offset(struct device *dev, struct device_attribute *attr,
  904. const char *buf, size_t size)
  905. {
  906. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  907. return size;
  908. }
  909. static DEVICE_ATTR(ADC_Channel_7_Offset, 0664, show_ADC_Channel_7_Offset,
  910. store_ADC_Channel_7_Offset);
  911. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  912. /* // Create File For EM : ADC_Channel_8_Offset */
  913. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  914. static ssize_t show_ADC_Channel_8_Offset(struct device *dev, struct device_attribute *attr,
  915. char *buf)
  916. {
  917. int ret_value = 1;
  918. ret_value = (*(adc_cali_offset + 8));
  919. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_8_Offset : %d\n", ret_value);
  920. return sprintf(buf, "%u\n", ret_value);
  921. }
  922. static ssize_t store_ADC_Channel_8_Offset(struct device *dev, struct device_attribute *attr,
  923. const char *buf, size_t size)
  924. {
  925. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  926. return size;
  927. }
  928. static DEVICE_ATTR(ADC_Channel_8_Offset, 0664, show_ADC_Channel_8_Offset,
  929. store_ADC_Channel_8_Offset);
  930. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  931. /* // Create File For EM : ADC_Channel_9_Offset */
  932. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  933. static ssize_t show_ADC_Channel_9_Offset(struct device *dev, struct device_attribute *attr,
  934. char *buf)
  935. {
  936. int ret_value = 1;
  937. ret_value = (*(adc_cali_offset + 9));
  938. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_9_Offset : %d\n", ret_value);
  939. return sprintf(buf, "%u\n", ret_value);
  940. }
  941. static ssize_t store_ADC_Channel_9_Offset(struct device *dev, struct device_attribute *attr,
  942. const char *buf, size_t size)
  943. {
  944. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  945. return size;
  946. }
  947. static DEVICE_ATTR(ADC_Channel_9_Offset, 0664, show_ADC_Channel_9_Offset,
  948. store_ADC_Channel_9_Offset);
  949. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  950. /* // Create File For EM : ADC_Channel_10_Offset */
  951. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  952. static ssize_t show_ADC_Channel_10_Offset(struct device *dev, struct device_attribute *attr,
  953. char *buf)
  954. {
  955. int ret_value = 1;
  956. ret_value = (*(adc_cali_offset + 10));
  957. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_10_Offset : %d\n", ret_value);
  958. return sprintf(buf, "%d\n", ret_value);
  959. }
  960. static ssize_t store_ADC_Channel_10_Offset(struct device *dev, struct device_attribute *attr,
  961. const char *buf, size_t size)
  962. {
  963. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  964. return size;
  965. }
  966. static DEVICE_ATTR(ADC_Channel_10_Offset, 0664, show_ADC_Channel_10_Offset,
  967. store_ADC_Channel_10_Offset);
  968. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  969. /* // Create File For EM : ADC_Channel_11_Offset */
  970. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  971. static ssize_t show_ADC_Channel_11_Offset(struct device *dev, struct device_attribute *attr,
  972. char *buf)
  973. {
  974. int ret_value = 1;
  975. ret_value = (*(adc_cali_offset + 11));
  976. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_11_Offset : %d\n", ret_value);
  977. return sprintf(buf, "%d\n", ret_value);
  978. }
  979. static ssize_t store_ADC_Channel_11_Offset(struct device *dev, struct device_attribute *attr,
  980. const char *buf, size_t size)
  981. {
  982. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  983. return size;
  984. }
  985. static DEVICE_ATTR(ADC_Channel_11_Offset, 0664, show_ADC_Channel_11_Offset,
  986. store_ADC_Channel_11_Offset);
  987. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  988. /* // Create File For EM : ADC_Channel_12_Offset */
  989. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  990. static ssize_t show_ADC_Channel_12_Offset(struct device *dev, struct device_attribute *attr,
  991. char *buf)
  992. {
  993. int ret_value = 1;
  994. ret_value = (*(adc_cali_offset + 12));
  995. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_12_Offset : %d\n", ret_value);
  996. return sprintf(buf, "%u\n", ret_value);
  997. }
  998. static ssize_t store_ADC_Channel_12_Offset(struct device *dev, struct device_attribute *attr,
  999. const char *buf, size_t size)
  1000. {
  1001. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1002. return size;
  1003. }
  1004. static DEVICE_ATTR(ADC_Channel_12_Offset, 0664, show_ADC_Channel_12_Offset,
  1005. store_ADC_Channel_12_Offset);
  1006. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1007. /* // Create File For EM : ADC_Channel_13_Offset */
  1008. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1009. static ssize_t show_ADC_Channel_13_Offset(struct device *dev, struct device_attribute *attr,
  1010. char *buf)
  1011. {
  1012. int ret_value = 1;
  1013. ret_value = (*(adc_cali_offset + 13));
  1014. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_13_Offset : %d\n", ret_value);
  1015. return sprintf(buf, "%u\n", ret_value);
  1016. }
  1017. static ssize_t store_ADC_Channel_13_Offset(struct device *dev, struct device_attribute *attr,
  1018. const char *buf, size_t size)
  1019. {
  1020. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1021. return size;
  1022. }
  1023. static DEVICE_ATTR(ADC_Channel_13_Offset, 0664, show_ADC_Channel_13_Offset,
  1024. store_ADC_Channel_13_Offset);
  1025. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1026. /* // Create File For EM : ADC_Channel_Is_Calibration */
  1027. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1028. static ssize_t show_ADC_Channel_Is_Calibration(struct device *dev, struct device_attribute *attr,
  1029. char *buf)
  1030. {
  1031. int ret_value = 2;
  1032. ret_value = g_ADC_Cali;
  1033. battery_log(BAT_LOG_CRTI, "[EM] ADC_Channel_Is_Calibration : %d\n", ret_value);
  1034. return sprintf(buf, "%u\n", ret_value);
  1035. }
  1036. static ssize_t store_ADC_Channel_Is_Calibration(struct device *dev, struct device_attribute *attr,
  1037. const char *buf, size_t size)
  1038. {
  1039. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1040. return size;
  1041. }
  1042. static DEVICE_ATTR(ADC_Channel_Is_Calibration, 0664, show_ADC_Channel_Is_Calibration,
  1043. store_ADC_Channel_Is_Calibration);
  1044. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1045. /* // Create File For EM : Power_On_Voltage */
  1046. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1047. static ssize_t show_Power_On_Voltage(struct device *dev, struct device_attribute *attr, char *buf)
  1048. {
  1049. int ret_value = 1;
  1050. ret_value = 3400;
  1051. battery_log(BAT_LOG_CRTI, "[EM] Power_On_Voltage : %d\n", ret_value);
  1052. return sprintf(buf, "%u\n", ret_value);
  1053. }
  1054. static ssize_t store_Power_On_Voltage(struct device *dev, struct device_attribute *attr,
  1055. const char *buf, size_t size)
  1056. {
  1057. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1058. return size;
  1059. }
  1060. static DEVICE_ATTR(Power_On_Voltage, 0664, show_Power_On_Voltage, store_Power_On_Voltage);
  1061. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1062. /* // Create File For EM : Power_Off_Voltage */
  1063. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1064. static ssize_t show_Power_Off_Voltage(struct device *dev, struct device_attribute *attr, char *buf)
  1065. {
  1066. int ret_value = 1;
  1067. ret_value = 3400;
  1068. battery_log(BAT_LOG_CRTI, "[EM] Power_Off_Voltage : %d\n", ret_value);
  1069. return sprintf(buf, "%u\n", ret_value);
  1070. }
  1071. static ssize_t store_Power_Off_Voltage(struct device *dev, struct device_attribute *attr,
  1072. const char *buf, size_t size)
  1073. {
  1074. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1075. return size;
  1076. }
  1077. static DEVICE_ATTR(Power_Off_Voltage, 0664, show_Power_Off_Voltage, store_Power_Off_Voltage);
  1078. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1079. /* // Create File For EM : Charger_TopOff_Value */
  1080. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1081. static ssize_t show_Charger_TopOff_Value(struct device *dev, struct device_attribute *attr,
  1082. char *buf)
  1083. {
  1084. int ret_value = 1;
  1085. ret_value = 4110;
  1086. battery_log(BAT_LOG_CRTI, "[EM] Charger_TopOff_Value : %d\n", ret_value);
  1087. return sprintf(buf, "%u\n", ret_value);
  1088. }
  1089. static ssize_t store_Charger_TopOff_Value(struct device *dev, struct device_attribute *attr,
  1090. const char *buf, size_t size)
  1091. {
  1092. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1093. return size;
  1094. }
  1095. static DEVICE_ATTR(Charger_TopOff_Value, 0664, show_Charger_TopOff_Value,
  1096. store_Charger_TopOff_Value);
  1097. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1098. /* // Create File For EM : FG_Battery_CurrentConsumption */
  1099. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1100. static ssize_t show_FG_Battery_CurrentConsumption(struct device *dev, struct device_attribute *attr,
  1101. char *buf)
  1102. {
  1103. int ret_value = 8888;
  1104. ret_value = battery_meter_get_battery_current();
  1105. battery_log(BAT_LOG_CRTI, "[EM] FG_Battery_CurrentConsumption : %d/10 mA\n", ret_value);
  1106. return sprintf(buf, "%u\n", ret_value);
  1107. }
  1108. static ssize_t store_FG_Battery_CurrentConsumption(struct device *dev,
  1109. struct device_attribute *attr, const char *buf,
  1110. size_t size)
  1111. {
  1112. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1113. return size;
  1114. }
  1115. static DEVICE_ATTR(FG_Battery_CurrentConsumption, 0664, show_FG_Battery_CurrentConsumption,
  1116. store_FG_Battery_CurrentConsumption);
  1117. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1118. /* // Create File For EM : FG_SW_CoulombCounter */
  1119. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1120. static ssize_t show_FG_SW_CoulombCounter(struct device *dev, struct device_attribute *attr,
  1121. char *buf)
  1122. {
  1123. s32 ret_value = 7777;
  1124. ret_value = battery_meter_get_car();
  1125. battery_log(BAT_LOG_CRTI, "[EM] FG_SW_CoulombCounter : %d\n", ret_value);
  1126. return sprintf(buf, "%u\n", ret_value);
  1127. }
  1128. static ssize_t store_FG_SW_CoulombCounter(struct device *dev, struct device_attribute *attr,
  1129. const char *buf, size_t size)
  1130. {
  1131. battery_log(BAT_LOG_CRTI, "[EM] Not Support Write Function\n");
  1132. return size;
  1133. }
  1134. static DEVICE_ATTR(FG_SW_CoulombCounter, 0664, show_FG_SW_CoulombCounter,
  1135. store_FG_SW_CoulombCounter);
  1136. static ssize_t show_Charging_CallState(struct device *dev, struct device_attribute *attr, char *buf)
  1137. {
  1138. battery_log(BAT_LOG_CRTI, "call state = %d\n", g_call_state);
  1139. return sprintf(buf, "%u\n", g_call_state);
  1140. }
  1141. static ssize_t store_Charging_CallState(struct device *dev, struct device_attribute *attr,
  1142. const char *buf, size_t size)
  1143. {
  1144. int ret, call_state;
  1145. ret = kstrtoint(buf, 0, &call_state);
  1146. if (ret) {
  1147. pr_err("wrong format!\n");
  1148. return size;
  1149. }
  1150. g_call_state = (call_state ? true : false);
  1151. battery_log(BAT_LOG_CRTI, "call state = %d\n", g_call_state);
  1152. return size;
  1153. }
  1154. static DEVICE_ATTR(Charging_CallState, 0664, show_Charging_CallState, store_Charging_CallState);
  1155. static ssize_t show_Charging_Enable(struct device *dev, struct device_attribute *attr, char *buf)
  1156. {
  1157. battery_log(BAT_LOG_CRTI, "hold charging = %d\n", g_cmd_hold_charging);
  1158. return sprintf(buf, "%u\n", !g_cmd_hold_charging);
  1159. }
  1160. static ssize_t store_Charging_Enable(struct device *dev, struct device_attribute *attr,
  1161. const char *buf, size_t size)
  1162. {
  1163. int ret, charging_enable = 1;
  1164. ret = kstrtoint(buf, 0, &charging_enable);
  1165. if (ret) {
  1166. pr_err("wrong format!\n");
  1167. return size;
  1168. }
  1169. if (charging_enable == 1)
  1170. g_cmd_hold_charging = false;
  1171. else if (charging_enable == 0)
  1172. g_cmd_hold_charging = true;
  1173. wake_up_bat_update_meter();
  1174. battery_log(BAT_LOG_CRTI, "hold charging = %d\n", g_cmd_hold_charging);
  1175. return size;
  1176. }
  1177. static DEVICE_ATTR(Charging_Enable, 0664, show_Charging_Enable, store_Charging_Enable);
  1178. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  1179. static ssize_t show_Pump_Express(struct device *dev, struct device_attribute *attr, char *buf)
  1180. {
  1181. int icount = 20; /* max debouncing time 20 * 0.2 sec */
  1182. if (true == ta_check_chr_type && STANDARD_CHARGER == BMT_status.charger_type &&
  1183. BMT_status.SOC >= p_bat_charging_data->ta_start_battery_soc &&
  1184. BMT_status.SOC < p_bat_charging_data->ta_stop_battery_soc) {
  1185. battery_log(BAT_LOG_CRTI, "[%s]Wait for PE detection\n", __func__);
  1186. do {
  1187. icount--;
  1188. msleep(200);
  1189. } while (icount && ta_check_chr_type);
  1190. }
  1191. battery_log(BAT_LOG_CRTI, "Pump express = %d\n", is_ta_connect);
  1192. return sprintf(buf, "%u\n", is_ta_connect);
  1193. }
  1194. static ssize_t store_Pump_Express(struct device *dev, struct device_attribute *attr,
  1195. const char *buf, size_t size)
  1196. {
  1197. int rv;
  1198. u32 value;
  1199. rv = kstrtouint(buf, 0, &value);
  1200. if (rv != 1)
  1201. return -EINVAL;
  1202. is_ta_connect = (value != 0) ? true : false;
  1203. battery_log(BAT_LOG_CRTI, "Pump express= %d\n", is_ta_connect);
  1204. return size;
  1205. }
  1206. static DEVICE_ATTR(Pump_Express, 0664, show_Pump_Express, store_Pump_Express);
  1207. #endif
  1208. static ssize_t show_Custom_Charging_Current(struct device *dev, struct device_attribute *attr,
  1209. char *buf)
  1210. {
  1211. battery_log(BAT_LOG_CRTI, "custom charging current = %d\n", g_custom_charging_current);
  1212. return sprintf(buf, "%d\n", g_custom_charging_current);
  1213. }
  1214. static ssize_t store_Custom_Charging_Current(struct device *dev, struct device_attribute *attr,
  1215. const char *buf, size_t size)
  1216. {
  1217. int ret, cur;
  1218. ret = kstrtoint(buf, 0, &cur);
  1219. if (ret) {
  1220. pr_err("wrong format!\n");
  1221. return size;
  1222. }
  1223. g_custom_charging_current = cur;
  1224. battery_log(BAT_LOG_CRTI, "custom charging current = %d\n", g_custom_charging_current);
  1225. wake_up_bat_update_meter();
  1226. return size;
  1227. }
  1228. static DEVICE_ATTR(Custom_Charging_Current, 0664, show_Custom_Charging_Current,
  1229. store_Custom_Charging_Current);
  1230. static void mt_battery_update_EM(struct battery_data *bat_data)
  1231. {
  1232. bat_data->BAT_CAPACITY = BMT_status.UI_SOC;
  1233. bat_data->BAT_TemperatureR = BMT_status.temperatureR; /* API */
  1234. bat_data->BAT_TempBattVoltage = BMT_status.temperatureV; /* API */
  1235. bat_data->BAT_InstatVolt = BMT_status.bat_vol; /* VBAT */
  1236. bat_data->BAT_BatteryAverageCurrent = BMT_status.ICharging;
  1237. bat_data->BAT_BatterySenseVoltage = BMT_status.bat_vol;
  1238. bat_data->BAT_ISenseVoltage = BMT_status.Vsense; /* API */
  1239. bat_data->BAT_ChargerVoltage = BMT_status.charger_vol;
  1240. if ((BMT_status.UI_SOC == 100) && BMT_status.charger_exist)
  1241. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_FULL;
  1242. #ifdef CONFIG_MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION
  1243. if (bat_data->BAT_CAPACITY <= 0)
  1244. bat_data->BAT_CAPACITY = 1;
  1245. battery_log(BAT_LOG_CRTI,
  1246. "BAT_CAPACITY=1, due to define MTK_DISABLE_POWER_ON_OFF_VOLTAGE_LIMITATION\r\n");
  1247. #endif
  1248. }
  1249. static bool mt_battery_100Percent_tracking_check(void)
  1250. {
  1251. bool resetBatteryMeter = false;
  1252. u32 cust_sync_time;
  1253. static u32 timer_counter;
  1254. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1255. cust_sync_time = p_bat_charging_data->cust_soc_jeita_sync_time;
  1256. if (timer_counter == 0)
  1257. timer_counter = (cust_sync_time / BAT_TASK_PERIOD);
  1258. #else
  1259. cust_sync_time = p_bat_charging_data->onehundred_percent_tracking_time;
  1260. if (timer_counter == 0)
  1261. timer_counter = (cust_sync_time / BAT_TASK_PERIOD);
  1262. #endif
  1263. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1264. if (g_temp_status != TEMP_POS_10_TO_POS_45 && g_temp_status != TEMP_POS_0_TO_POS_10) {
  1265. battery_log(BAT_LOG_FULL,
  1266. "Skip 100percent tracking due to not 4.2V full-charging.\n");
  1267. return false;
  1268. }
  1269. #endif
  1270. if (BMT_status.bat_full == true) { /* charging full first, UI tracking to 100% */
  1271. if (BMT_status.bat_in_recharging_state == true) {
  1272. if (BMT_status.UI_SOC >= 100)
  1273. BMT_status.UI_SOC = 100;
  1274. resetBatteryMeter = false;
  1275. } else if (BMT_status.UI_SOC >= 100) {
  1276. BMT_status.UI_SOC = 100;
  1277. if ((g_charging_full_reset_bat_meter == true)
  1278. && (BMT_status.bat_charging_state == CHR_BATFULL)) {
  1279. resetBatteryMeter = true;
  1280. g_charging_full_reset_bat_meter = false;
  1281. } else {
  1282. resetBatteryMeter = false;
  1283. }
  1284. } else {
  1285. /* increase UI percentage every xxs */
  1286. if (timer_counter >= (cust_sync_time / BAT_TASK_PERIOD)) {
  1287. timer_counter = 1;
  1288. BMT_status.UI_SOC++;
  1289. } else {
  1290. timer_counter++;
  1291. return resetBatteryMeter;
  1292. }
  1293. resetBatteryMeter = true;
  1294. }
  1295. battery_log(BAT_LOG_FULL,
  1296. "[Battery] mt_battery_100percent_tracking(), Charging full first UI(%d), reset(%d) \r\n",
  1297. BMT_status.UI_SOC, resetBatteryMeter);
  1298. } else {
  1299. /* charging is not full, UI keep 99% if reaching 100%, */
  1300. if (BMT_status.UI_SOC >= 99 && battery_meter_get_battery_current_sign()) {
  1301. BMT_status.UI_SOC = 99;
  1302. resetBatteryMeter = true;
  1303. battery_log(BAT_LOG_FULL,
  1304. "[Battery] mt_battery_100percent_tracking(), UI full first, keep (%d) \r\n",
  1305. BMT_status.UI_SOC);
  1306. }
  1307. timer_counter = (cust_sync_time / BAT_TASK_PERIOD);
  1308. }
  1309. return resetBatteryMeter;
  1310. }
  1311. static bool mt_battery_nPercent_tracking_check(void)
  1312. {
  1313. bool resetBatteryMeter = false;
  1314. #if defined(CONFIG_SOC_BY_HW_FG)
  1315. static u32 timer_counter;
  1316. if (timer_counter == 0)
  1317. timer_counter = (p_bat_charging_data->npercent_tracking_time / BAT_TASK_PERIOD);
  1318. if (BMT_status.nPrecent_UI_SOC_check_point == 0)
  1319. return false;
  1320. /* fuel gauge ZCV < 15%, but UI > 15%, 15% can be customized */
  1321. if ((BMT_status.ZCV <= BMT_status.nPercent_ZCV)
  1322. && (BMT_status.UI_SOC > BMT_status.nPrecent_UI_SOC_check_point)) {
  1323. if (timer_counter ==
  1324. (p_bat_charging_data->npercent_tracking_time / BAT_TASK_PERIOD)) {
  1325. BMT_status.UI_SOC--; /* every x sec decrease UI percentage */
  1326. timer_counter = 1;
  1327. } else {
  1328. timer_counter++;
  1329. return resetBatteryMeter;
  1330. }
  1331. resetBatteryMeter = true;
  1332. battery_log(BAT_LOG_CRTI,
  1333. "[Battery]mt_battery_nPercent_tracking_check(), ZCV(%d) <= BMT_status.nPercent_ZCV(%d), UI_SOC=%d., tracking UI_SOC=%d \r\n",
  1334. BMT_status.ZCV, BMT_status.nPercent_ZCV, BMT_status.UI_SOC,
  1335. BMT_status.nPrecent_UI_SOC_check_point);
  1336. } else if ((BMT_status.ZCV > BMT_status.nPercent_ZCV)
  1337. && (BMT_status.UI_SOC == BMT_status.nPrecent_UI_SOC_check_point)) {
  1338. /* UI less than 15 , but fuel gague is more than 15, hold UI 15% */
  1339. timer_counter = (p_bat_charging_data->npercent_tracking_time / BAT_TASK_PERIOD);
  1340. resetBatteryMeter = true;
  1341. battery_log(BAT_LOG_CRTI,
  1342. "[Battery]mt_battery_nPercent_tracking_check() ZCV(%d) > BMT_status.nPercent_ZCV(%d) and UI SOC (%d), then keep %d. \r\n",
  1343. BMT_status.ZCV, BMT_status.nPercent_ZCV, BMT_status.UI_SOC,
  1344. BMT_status.nPrecent_UI_SOC_check_point);
  1345. } else {
  1346. timer_counter = (p_bat_charging_data->npercent_tracking_time / BAT_TASK_PERIOD);
  1347. }
  1348. #endif
  1349. return resetBatteryMeter;
  1350. }
  1351. static bool mt_battery_0Percent_tracking_check(void)
  1352. {
  1353. bool resetBatteryMeter = true;
  1354. if (BMT_status.UI_SOC <= 0)
  1355. BMT_status.UI_SOC = 0;
  1356. else
  1357. BMT_status.UI_SOC--;
  1358. battery_log(BAT_LOG_CRTI,
  1359. "[Battery] mt_battery_0Percent_tracking_check(), VBAT < %d UI_SOC = (%d)\r\n",
  1360. SYSTEM_OFF_VOLTAGE, BMT_status.UI_SOC);
  1361. return resetBatteryMeter;
  1362. }
  1363. static void mt_battery_Sync_UI_Percentage_to_Real(void)
  1364. {
  1365. static u32 timer_counter;
  1366. if ((BMT_status.UI_SOC > BMT_status.SOC) && ((BMT_status.UI_SOC != 1))) {
  1367. /* reduce after xxs */
  1368. if (g_refresh_ui_soc
  1369. || timer_counter ==
  1370. (p_bat_charging_data->sync_to_real_tracking_time / BAT_TASK_PERIOD)) {
  1371. BMT_status.UI_SOC--;
  1372. timer_counter = 0;
  1373. g_refresh_ui_soc = false;
  1374. } else {
  1375. timer_counter++;
  1376. }
  1377. battery_log(BAT_LOG_CRTI,
  1378. "Sync UI percentage to Real one, BMT_status.UI_SOC=%d, BMT_status.SOC=%d, counter = %d\r\n",
  1379. BMT_status.UI_SOC, BMT_status.SOC, timer_counter);
  1380. } else {
  1381. timer_counter = 0;
  1382. if (BMT_status.UI_SOC == -1)
  1383. BMT_status.UI_SOC = BMT_status.SOC;
  1384. else if (BMT_status.charger_exist && BMT_status.bat_charging_state != CHR_ERROR) {
  1385. if (BMT_status.UI_SOC < BMT_status.SOC
  1386. && (BMT_status.SOC - BMT_status.UI_SOC > 1))
  1387. BMT_status.UI_SOC++;
  1388. else
  1389. BMT_status.UI_SOC = BMT_status.SOC;
  1390. }
  1391. }
  1392. if (BMT_status.UI_SOC <= 0) {
  1393. BMT_status.UI_SOC = 1;
  1394. battery_log(BAT_LOG_CRTI, "[Battery]UI_SOC get 0 first (%d)\r\n",
  1395. BMT_status.UI_SOC);
  1396. }
  1397. }
  1398. static void battery_update(struct battery_data *bat_data)
  1399. {
  1400. struct power_supply *bat_psy = &bat_data->psy;
  1401. bool resetBatteryMeter = false;
  1402. bat_data->BAT_TECHNOLOGY = POWER_SUPPLY_TECHNOLOGY_LION;
  1403. bat_data->BAT_HEALTH = POWER_SUPPLY_HEALTH_GOOD;
  1404. bat_data->BAT_VOLTAGE_AVG = BMT_status.bat_vol * 1000;
  1405. bat_data->BAT_VOLTAGE_NOW = BMT_status.bat_vol * 1000; /* voltage_now unit is microvolt */
  1406. bat_data->BAT_TEMP = BMT_status.temperature * 10;
  1407. bat_data->BAT_PRESENT = BMT_status.bat_exist;
  1408. if (BMT_status.charger_exist && (BMT_status.bat_charging_state != CHR_ERROR)
  1409. && !g_cmd_hold_charging) {
  1410. if (BMT_status.bat_exist) { /* charging */
  1411. if (BMT_status.bat_vol <= SYSTEM_OFF_VOLTAGE)
  1412. resetBatteryMeter = mt_battery_0Percent_tracking_check();
  1413. else
  1414. resetBatteryMeter = mt_battery_100Percent_tracking_check();
  1415. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_CHARGING;
  1416. } else { /* No Battery, Only Charger */
  1417. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_UNKNOWN;
  1418. BMT_status.UI_SOC = 0;
  1419. }
  1420. } else { /* Only Battery */
  1421. bat_data->BAT_STATUS = POWER_SUPPLY_STATUS_DISCHARGING;
  1422. if (BMT_status.bat_vol <= SYSTEM_OFF_VOLTAGE)
  1423. resetBatteryMeter = mt_battery_0Percent_tracking_check();
  1424. else
  1425. resetBatteryMeter = mt_battery_nPercent_tracking_check();
  1426. }
  1427. if (resetBatteryMeter == true)
  1428. battery_meter_reset(true);
  1429. else
  1430. mt_battery_Sync_UI_Percentage_to_Real();
  1431. battery_log(BAT_LOG_FULL, "UI_SOC=(%d), resetBatteryMeter=(%d)\n",
  1432. BMT_status.UI_SOC, resetBatteryMeter);
  1433. #ifdef CUST_CAPACITY_OCV2CV_TRANSFORM
  1434. /* We store capacity before loading compenstation in RTC */
  1435. if (battery_meter_get_battery_soc() <= 1)
  1436. set_rtc_spare_fg_value(1);
  1437. else
  1438. set_rtc_spare_fg_value(battery_meter_get_battery_soc()); /*use battery_soc */
  1439. #else
  1440. /* set RTC SOC to 1 to avoid SOC jump in charger boot. */
  1441. if (BMT_status.UI_SOC <= 1)
  1442. set_rtc_spare_fg_value(1);
  1443. else
  1444. set_rtc_spare_fg_value(BMT_status.UI_SOC);
  1445. #endif
  1446. battery_log(BAT_LOG_FULL, "RTC_SOC=(%d)\n", get_rtc_spare_fg_value());
  1447. mt_battery_update_EM(bat_data);
  1448. power_supply_changed(bat_psy);
  1449. }
  1450. static void ac_update(struct ac_data *ac_data)
  1451. {
  1452. static int ac_status = -1;
  1453. struct power_supply *ac_psy = &ac_data->psy;
  1454. if (BMT_status.charger_exist) {
  1455. if ((BMT_status.charger_type == NONSTANDARD_CHARGER) ||
  1456. (BMT_status.charger_type == STANDARD_CHARGER) ||
  1457. (BMT_status.charger_type == APPLE_1_0A_CHARGER) ||
  1458. (BMT_status.charger_type == APPLE_2_1A_CHARGER)) {
  1459. ac_data->AC_ONLINE = 1;
  1460. ac_psy->type = POWER_SUPPLY_TYPE_MAINS;
  1461. } else
  1462. ac_data->AC_ONLINE = 0;
  1463. } else {
  1464. ac_data->AC_ONLINE = 0;
  1465. }
  1466. if (ac_status != ac_data->AC_ONLINE) {
  1467. ac_status = ac_data->AC_ONLINE;
  1468. power_supply_changed(ac_psy);
  1469. }
  1470. }
  1471. static void usb_update(struct usb_data *usb_data)
  1472. {
  1473. static int usb_status = -1;
  1474. struct power_supply *usb_psy = &usb_data->psy;
  1475. if (BMT_status.charger_exist) {
  1476. if ((BMT_status.charger_type == STANDARD_HOST) ||
  1477. (BMT_status.charger_type == CHARGING_HOST)) {
  1478. usb_data->USB_ONLINE = 1;
  1479. usb_psy->type = POWER_SUPPLY_TYPE_USB;
  1480. } else
  1481. usb_data->USB_ONLINE = 0;
  1482. } else {
  1483. usb_data->USB_ONLINE = 0;
  1484. }
  1485. if (usb_status != usb_data->USB_ONLINE) {
  1486. usb_status = usb_data->USB_ONLINE;
  1487. power_supply_changed(usb_psy);
  1488. }
  1489. }
  1490. #endif
  1491. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1492. /* // Battery Temprature Parameters and functions */
  1493. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1494. bool pmic_chrdet_status(void)
  1495. {
  1496. if (upmu_is_chr_det() == true)
  1497. return true;
  1498. battery_log(BAT_LOG_CRTI, "[pmic_chrdet_status] No charger\r\n");
  1499. return false;
  1500. }
  1501. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1502. /* // Pulse Charging Algorithm */
  1503. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  1504. bool bat_is_charger_exist(void)
  1505. {
  1506. return bat_charger_get_detect_status();
  1507. }
  1508. bool bat_is_charging_full(void)
  1509. {
  1510. if ((BMT_status.bat_full == true) && (BMT_status.bat_in_recharging_state == false))
  1511. return true;
  1512. else
  1513. return false;
  1514. }
  1515. u32 bat_get_ui_percentage(void)
  1516. {
  1517. /* for plugging out charger in recharge phase, using SOC as UI_SOC */
  1518. if (chr_wake_up_bat == true)
  1519. return BMT_status.SOC;
  1520. else
  1521. return BMT_status.UI_SOC;
  1522. }
  1523. /* Full state --> recharge voltage --> full state */
  1524. u32 bat_is_recharging_phase(void)
  1525. {
  1526. if (BMT_status.bat_in_recharging_state || BMT_status.bat_full == true)
  1527. return true;
  1528. else
  1529. return false;
  1530. }
  1531. unsigned long BAT_Get_Battery_Voltage(int polling_mode)
  1532. {
  1533. unsigned long ret_val = 0;
  1534. #if defined(CONFIG_POWER_EXT)
  1535. ret_val = 4000;
  1536. #else
  1537. ret_val = battery_meter_get_battery_voltage();
  1538. #endif
  1539. return ret_val;
  1540. }
  1541. static void mt_battery_average_method_init(u32 *bufferdata, u32 data, s32 *sum)
  1542. {
  1543. u32 i;
  1544. static bool batteryBufferFirst = true;
  1545. static bool previous_charger_exist;
  1546. static bool previous_in_recharge_state;
  1547. static u8 index;
  1548. /* reset charging current window while plug in/out */
  1549. if (need_clear_current_window) {
  1550. if (BMT_status.charger_exist) {
  1551. if (previous_charger_exist == false) {
  1552. batteryBufferFirst = true;
  1553. previous_charger_exist = true;
  1554. if ((BMT_status.charger_type == STANDARD_CHARGER)
  1555. || (BMT_status.charger_type == APPLE_1_0A_CHARGER)
  1556. || (BMT_status.charger_type == APPLE_2_1A_CHARGER)
  1557. || (BMT_status.charger_type == CHARGING_HOST))
  1558. data = 650; /* mA */
  1559. else /* USB or non-stadanrd charger */
  1560. data = 450; /* mA */
  1561. } else if ((previous_in_recharge_state == false)
  1562. && (BMT_status.bat_in_recharging_state == true)) {
  1563. batteryBufferFirst = true;
  1564. if ((BMT_status.charger_type == STANDARD_CHARGER)
  1565. || (BMT_status.charger_type == APPLE_1_0A_CHARGER)
  1566. || (BMT_status.charger_type == APPLE_2_1A_CHARGER)
  1567. || (BMT_status.charger_type == CHARGING_HOST))
  1568. data = 650; /* mA */
  1569. else /* USB or non-stadanrd charger */
  1570. data = 450; /* mA */
  1571. }
  1572. previous_in_recharge_state = BMT_status.bat_in_recharging_state;
  1573. } else {
  1574. if (previous_charger_exist == true) {
  1575. batteryBufferFirst = true;
  1576. previous_charger_exist = false;
  1577. data = 0;
  1578. }
  1579. }
  1580. }
  1581. battery_log(BAT_LOG_FULL, "batteryBufferFirst =%d, data= (%d)\n", batteryBufferFirst, data);
  1582. if (batteryBufferFirst == true) {
  1583. for (i = 0; i < BATTERY_AVERAGE_SIZE; i++)
  1584. bufferdata[i] = data;
  1585. *sum = data * BATTERY_AVERAGE_SIZE;
  1586. }
  1587. index++;
  1588. if (index >= BATTERY_AVERAGE_DATA_NUMBER) {
  1589. index = BATTERY_AVERAGE_DATA_NUMBER;
  1590. batteryBufferFirst = false;
  1591. }
  1592. }
  1593. static u32 mt_battery_average_method(u32 *bufferdata, u32 data, s32 *sum, u8 batteryIndex)
  1594. {
  1595. u32 avgdata;
  1596. mt_battery_average_method_init(bufferdata, data, sum);
  1597. *sum -= bufferdata[batteryIndex];
  1598. *sum += data;
  1599. bufferdata[batteryIndex] = data;
  1600. avgdata = (*sum) / BATTERY_AVERAGE_SIZE;
  1601. battery_log(BAT_LOG_FULL, "bufferdata[%d]= (%d)\n", batteryIndex, bufferdata[batteryIndex]);
  1602. return avgdata;
  1603. }
  1604. static int filter_battery_temperature(int instant_temp)
  1605. {
  1606. int check_count;
  1607. /* recheck 3 times for critical temperature */
  1608. for (check_count = 0; check_count < 3; check_count++) {
  1609. if (instant_temp > p_bat_charging_data->max_discharge_temperature
  1610. || instant_temp < p_bat_charging_data->min_discharge_temperature) {
  1611. instant_temp = battery_meter_get_battery_temperature();
  1612. pr_warn("recheck battery temperature result: %d\n", instant_temp);
  1613. msleep(20);
  1614. continue;
  1615. }
  1616. }
  1617. return instant_temp;
  1618. }
  1619. void mt_battery_GetBatteryData(void)
  1620. {
  1621. u32 bat_vol, charger_vol, Vsense, ZCV;
  1622. s32 ICharging, temperature, temperatureR, temperatureV, SOC;
  1623. s32 avg_temperature;
  1624. static s32 bat_sum, icharging_sum, temperature_sum;
  1625. static s32 batteryVoltageBuffer[BATTERY_AVERAGE_SIZE];
  1626. static s32 batteryCurrentBuffer[BATTERY_AVERAGE_SIZE];
  1627. static s32 batteryTempBuffer[BATTERY_AVERAGE_SIZE];
  1628. static u8 batteryIndex;
  1629. static s32 previous_SOC = -1;
  1630. bat_vol = battery_meter_get_battery_voltage();
  1631. Vsense = battery_meter_get_VSense();
  1632. ICharging = battery_meter_get_charging_current();
  1633. charger_vol = battery_meter_get_charger_voltage();
  1634. temperature = battery_meter_get_battery_temperature();
  1635. temperatureV = battery_meter_get_tempV();
  1636. temperatureR = battery_meter_get_tempR(temperatureV);
  1637. if (bat_meter_timeout == true) {
  1638. SOC = battery_meter_get_battery_percentage();
  1639. bat_meter_timeout = false;
  1640. } else {
  1641. if (previous_SOC == -1)
  1642. SOC = battery_meter_get_battery_percentage();
  1643. else
  1644. SOC = previous_SOC;
  1645. }
  1646. ZCV = battery_meter_get_battery_zcv();
  1647. need_clear_current_window = true;
  1648. BMT_status.ICharging =
  1649. mt_battery_average_method(&batteryCurrentBuffer[0], ICharging, &icharging_sum,
  1650. batteryIndex);
  1651. need_clear_current_window = false;
  1652. #if 1
  1653. if (previous_SOC == -1 && bat_vol <= SYSTEM_OFF_VOLTAGE) {
  1654. battery_log(BAT_LOG_CRTI,
  1655. "battery voltage too low, use ZCV to init average data.\n");
  1656. BMT_status.bat_vol =
  1657. mt_battery_average_method(&batteryVoltageBuffer[0], ZCV, &bat_sum,
  1658. batteryIndex);
  1659. } else {
  1660. BMT_status.bat_vol =
  1661. mt_battery_average_method(&batteryVoltageBuffer[0], bat_vol, &bat_sum,
  1662. batteryIndex);
  1663. }
  1664. #else
  1665. BMT_status.bat_vol =
  1666. mt_battery_average_method(&batteryVoltageBuffer[0], bat_vol, &bat_sum, batteryIndex);
  1667. #endif
  1668. avg_temperature =
  1669. mt_battery_average_method(&batteryTempBuffer[0], temperature, &temperature_sum,
  1670. batteryIndex);
  1671. if (p_bat_charging_data->use_avg_temperature)
  1672. BMT_status.temperature = avg_temperature;
  1673. else
  1674. BMT_status.temperature = filter_battery_temperature(temperature);
  1675. if ((g_battery_thermal_throttling_flag == 1) || (g_battery_thermal_throttling_flag == 3)) {
  1676. if (battery_cmd_thermal_test_mode == 1) {
  1677. BMT_status.temperature = battery_cmd_thermal_test_mode_value;
  1678. battery_log(BAT_LOG_FULL,
  1679. "[Battery] In thermal_test_mode , Tbat=%d\n",
  1680. BMT_status.temperature);
  1681. }
  1682. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1683. if (BMT_status.temperature > p_bat_charging_data->max_discharge_temperature
  1684. || BMT_status.temperature < p_bat_charging_data->min_discharge_temperature) {
  1685. struct battery_data *bat_data = &battery_main;
  1686. struct power_supply *bat_psy = &bat_data->psy;
  1687. pr_warn("[Battery] instant Tbat(%d) out of range, power down device.\n",
  1688. BMT_status.temperature);
  1689. bat_data->BAT_CAPACITY = 0;
  1690. power_supply_changed(bat_psy);
  1691. /* can not power down due to charger exist, so need reset system */
  1692. if (BMT_status.charger_exist)
  1693. bat_charger_set_platform_reset();
  1694. else
  1695. orderly_poweroff(true);
  1696. }
  1697. #endif
  1698. }
  1699. BMT_status.Vsense = Vsense;
  1700. BMT_status.charger_vol = charger_vol;
  1701. BMT_status.temperatureV = temperatureV;
  1702. BMT_status.temperatureR = temperatureR;
  1703. BMT_status.SOC = SOC;
  1704. BMT_status.ZCV = ZCV;
  1705. #ifndef CUST_CAPACITY_OCV2CV_TRANSFORM
  1706. if (BMT_status.charger_exist == false) {
  1707. if (BMT_status.SOC > previous_SOC && previous_SOC >= 0)
  1708. BMT_status.SOC = previous_SOC;
  1709. }
  1710. #endif
  1711. previous_SOC = BMT_status.SOC;
  1712. batteryIndex++;
  1713. if (batteryIndex >= BATTERY_AVERAGE_SIZE)
  1714. batteryIndex = 0;
  1715. battery_log(BAT_LOG_CRTI,
  1716. "AvgVbat=(%d),bat_vol=(%d),AvgI=(%d),I=(%d),VChr=(%d),AvgT=(%d),T=(%d),pre_SOC=(%d),SOC=(%d),ZCV=(%d)\n",
  1717. BMT_status.bat_vol, bat_vol, BMT_status.ICharging, ICharging,
  1718. BMT_status.charger_vol, BMT_status.temperature, temperature,
  1719. previous_SOC, BMT_status.SOC, BMT_status.ZCV);
  1720. }
  1721. static int mt_battery_CheckBatteryTemp(void)
  1722. {
  1723. int status = PMU_STATUS_OK;
  1724. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1725. battery_log(BAT_LOG_CRTI, "[BATTERY] support JEITA, temperature=%d\n",
  1726. BMT_status.temperature);
  1727. if (do_jeita_state_machine() == PMU_STATUS_FAIL) {
  1728. battery_log(BAT_LOG_CRTI, "[BATTERY] JEITA : fail\n");
  1729. status = PMU_STATUS_FAIL;
  1730. }
  1731. #else
  1732. #ifdef CONFIG_BAT_LOW_TEMP_PROTECT_ENABLE
  1733. if ((BMT_status.temperature < p_bat_charging_data->min_charge_temperature)
  1734. || (BMT_status.temperature == p_bat_charging_data->err_charge_temperature)) {
  1735. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Under Temperature or NTC fail !!\n\r");
  1736. status = PMU_STATUS_FAIL;
  1737. }
  1738. #endif
  1739. if (BMT_status.temperature >= p_bat_charging_data->max_charge_temperature) {
  1740. battery_log(BAT_LOG_CRTI, "[BATTERY] Battery Over Temperature !!\n\r");
  1741. status = PMU_STATUS_FAIL;
  1742. }
  1743. #endif
  1744. return status;
  1745. }
  1746. static int mt_battery_CheckChargerVoltage(void)
  1747. {
  1748. int status = PMU_STATUS_OK;
  1749. if (BMT_status.charger_exist) {
  1750. if (p_bat_charging_data->v_charger_enable == 1) {
  1751. if (BMT_status.charger_vol <= p_bat_charging_data->v_charger_min) {
  1752. battery_log(BAT_LOG_CRTI, "[BATTERY]Charger under voltage!!\r\n");
  1753. BMT_status.bat_charging_state = CHR_ERROR;
  1754. status = PMU_STATUS_FAIL;
  1755. }
  1756. }
  1757. if (BMT_status.charger_vol >= p_bat_charging_data->v_charger_max) {
  1758. battery_log(BAT_LOG_CRTI, "[BATTERY]Charger over voltage !!\r\n");
  1759. BMT_status.charger_protect_status = charger_OVER_VOL;
  1760. BMT_status.bat_charging_state = CHR_ERROR;
  1761. status = PMU_STATUS_FAIL;
  1762. }
  1763. }
  1764. return status;
  1765. }
  1766. static int mt_battery_CheckChargingTime(void)
  1767. {
  1768. int status = PMU_STATUS_OK;
  1769. if ((g_battery_thermal_throttling_flag == 2) || (g_battery_thermal_throttling_flag == 3)) {
  1770. battery_log(BAT_LOG_CRTI,
  1771. "[TestMode] Disable Safety Timer. bat_tt_enable=%d, bat_thr_test_mode=%d, bat_thr_test_value=%d\n",
  1772. g_battery_thermal_throttling_flag,
  1773. battery_cmd_thermal_test_mode, battery_cmd_thermal_test_mode_value);
  1774. } else {
  1775. /* Charging OT */
  1776. if (BMT_status.total_charging_time >= MAX_CHARGING_TIME) {
  1777. battery_log(BAT_LOG_CRTI, "[BATTERY] Charging Over Time.\n");
  1778. status = PMU_STATUS_FAIL;
  1779. }
  1780. }
  1781. return status;
  1782. }
  1783. #if defined(CONFIG_STOP_CHARGING_IN_TAKLING)
  1784. static int mt_battery_CheckCallState(void)
  1785. {
  1786. int status = PMU_STATUS_OK;
  1787. if ((g_call_state == CALL_ACTIVE)
  1788. && (BMT_status.bat_vol > p_bat_charging_data->v_cc2topoff_thres))
  1789. status = PMU_STATUS_FAIL;
  1790. return status;
  1791. }
  1792. #endif
  1793. static void mt_battery_CheckBatteryStatus(void)
  1794. {
  1795. if (mt_battery_CheckBatteryTemp() != PMU_STATUS_OK) {
  1796. BMT_status.bat_charging_state = CHR_ERROR;
  1797. return;
  1798. }
  1799. if (mt_battery_CheckChargerVoltage() != PMU_STATUS_OK) {
  1800. BMT_status.bat_charging_state = CHR_ERROR;
  1801. return;
  1802. }
  1803. #if defined(CONFIG_STOP_CHARGING_IN_TAKLING)
  1804. if (mt_battery_CheckCallState() != PMU_STATUS_OK) {
  1805. BMT_status.bat_charging_state = CHR_HOLD;
  1806. return;
  1807. }
  1808. #endif
  1809. if (mt_battery_CheckChargingTime() != PMU_STATUS_OK) {
  1810. BMT_status.bat_charging_state = CHR_ERROR;
  1811. return;
  1812. }
  1813. if (g_cmd_hold_charging == true) {
  1814. BMT_status.bat_charging_state = CHR_CMD_HOLD;
  1815. return;
  1816. } else if (BMT_status.bat_charging_state == CHR_CMD_HOLD) {
  1817. BMT_status.bat_charging_state = CHR_PRE;
  1818. return;
  1819. }
  1820. }
  1821. static void mt_battery_notify_TatalChargingTime_check(void)
  1822. {
  1823. #if defined(BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME)
  1824. if ((g_battery_thermal_throttling_flag == 2) || (g_battery_thermal_throttling_flag == 3)) {
  1825. battery_log(BAT_LOG_CRTI, "[TestMode] Disable Safety Timer : no UI display\n");
  1826. } else {
  1827. if (BMT_status.total_charging_time >= MAX_CHARGING_TIME)
  1828. /* if(BMT_status.total_charging_time >= 60) //test */
  1829. {
  1830. g_BatteryNotifyCode |= 0x0010;
  1831. battery_log(BAT_LOG_CRTI, "[BATTERY] Charging Over Time\n");
  1832. } else {
  1833. g_BatteryNotifyCode &= ~(0x0010);
  1834. }
  1835. }
  1836. battery_log(BAT_LOG_FULL,
  1837. "[BATTERY] BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME (%x)\n",
  1838. g_BatteryNotifyCode);
  1839. #endif
  1840. }
  1841. static void mt_battery_notify_VBat_check(void)
  1842. {
  1843. #if defined(BATTERY_NOTIFY_CASE_0004_VBAT)
  1844. if (BMT_status.bat_vol > 4350)
  1845. /* if(BMT_status.bat_vol > 3800) //test */
  1846. {
  1847. g_BatteryNotifyCode |= 0x0008;
  1848. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_vlot(%d) > 4350mV\n", BMT_status.bat_vol);
  1849. } else {
  1850. g_BatteryNotifyCode &= ~(0x0008);
  1851. }
  1852. battery_log(BAT_LOG_FULL, "[BATTERY] BATTERY_NOTIFY_CASE_0004_VBAT (%x)\n",
  1853. g_BatteryNotifyCode);
  1854. #endif
  1855. }
  1856. static void mt_battery_notify_ICharging_check(void)
  1857. {
  1858. #if defined(BATTERY_NOTIFY_CASE_0003_ICHARGING)
  1859. if ((BMT_status.ICharging > 1000) && (BMT_status.total_charging_time > 300)) {
  1860. g_BatteryNotifyCode |= 0x0004;
  1861. battery_log(BAT_LOG_CRTI, "[BATTERY] I_charging(%ld) > 1000mA\n",
  1862. BMT_status.ICharging);
  1863. } else {
  1864. g_BatteryNotifyCode &= ~(0x0004);
  1865. }
  1866. battery_log(BAT_LOG_CRTI, "[BATTERY] BATTERY_NOTIFY_CASE_0003_ICHARGING (%x)\n",
  1867. g_BatteryNotifyCode);
  1868. #endif
  1869. }
  1870. static void mt_battery_notify_VBatTemp_check(void)
  1871. {
  1872. #if defined(BATTERY_NOTIFY_CASE_0002_VBATTEMP)
  1873. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1874. if ((BMT_status.temperature >= p_bat_charging_data->max_charge_temperature)
  1875. || (BMT_status.temperature < p_bat_charging_data->temp_neg_10_threshold)) {
  1876. #else
  1877. #ifdef CONFIG_BAT_LOW_TEMP_PROTECT_ENABLE
  1878. if ((BMT_status.temperature >= p_bat_charging_data->max_charge_temperature)
  1879. || (BMT_status.temperature < p_bat_charging_data->min_charge_temperature)) {
  1880. #else
  1881. if (BMT_status.temperature >= p_bat_charging_data->max_charge_temperature) {
  1882. #endif
  1883. #endif /* #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT) */
  1884. g_BatteryNotifyCode |= 0x0002;
  1885. battery_log(BAT_LOG_CRTI, "[BATTERY] bat_temp(%d) out of range\n",
  1886. BMT_status.temperature);
  1887. } else {
  1888. g_BatteryNotifyCode &= ~(0x0002);
  1889. }
  1890. battery_log(BAT_LOG_CRTI, "[BATTERY] BATTERY_NOTIFY_CASE_0002_VBATTEMP (%x)\n",
  1891. g_BatteryNotifyCode);
  1892. #endif
  1893. }
  1894. static void mt_battery_notify_VCharger_check(void)
  1895. {
  1896. #if defined(BATTERY_NOTIFY_CASE_0001_VCHARGER)
  1897. if (BMT_status.charger_vol > p_bat_charging_data->v_charger_max) {
  1898. g_BatteryNotifyCode |= 0x0001;
  1899. battery_log(BAT_LOG_CRTI, "[BATTERY] BMT_status.charger_vol(%d) > %d mV\n",
  1900. BMT_status.charger_vol, p_bat_charging_data->v_charger_max);
  1901. } else {
  1902. g_BatteryNotifyCode &= ~(0x0001);
  1903. }
  1904. battery_log(BAT_LOG_FULL, "[BATTERY] BATTERY_NOTIFY_CASE_0001_VCHARGER (%x)\n",
  1905. g_BatteryNotifyCode);
  1906. #endif
  1907. }
  1908. static void mt_battery_notify_UI_test(void)
  1909. {
  1910. if (g_BN_TestMode == 0x0001) {
  1911. g_BatteryNotifyCode = 0x0001;
  1912. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0001_VCHARGER\n");
  1913. } else if (g_BN_TestMode == 0x0002) {
  1914. g_BatteryNotifyCode = 0x0002;
  1915. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0002_VBATTEMP\n");
  1916. } else if (g_BN_TestMode == 0x0003) {
  1917. g_BatteryNotifyCode = 0x0004;
  1918. battery_log(BAT_LOG_CRTI,
  1919. "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0003_ICHARGING\n");
  1920. } else if (g_BN_TestMode == 0x0004) {
  1921. g_BatteryNotifyCode = 0x0008;
  1922. battery_log(BAT_LOG_CRTI, "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0004_VBAT\n");
  1923. } else if (g_BN_TestMode == 0x0005) {
  1924. g_BatteryNotifyCode = 0x0010;
  1925. battery_log(BAT_LOG_CRTI,
  1926. "[BATTERY_TestMode] BATTERY_NOTIFY_CASE_0005_TOTAL_CHARGINGTIME\n");
  1927. } else {
  1928. battery_log(BAT_LOG_CRTI, "[BATTERY] Unknown BN_TestMode Code : %x\n",
  1929. g_BN_TestMode);
  1930. }
  1931. }
  1932. void mt_battery_notify_check(void)
  1933. {
  1934. g_BatteryNotifyCode = 0x0000;
  1935. if (g_BN_TestMode == 0x0000) { /* for normal case */
  1936. battery_log(BAT_LOG_FULL, "[BATTERY] mt_battery_notify_check\n");
  1937. mt_battery_notify_VCharger_check();
  1938. mt_battery_notify_VBatTemp_check();
  1939. mt_battery_notify_ICharging_check();
  1940. mt_battery_notify_VBat_check();
  1941. mt_battery_notify_TatalChargingTime_check();
  1942. } else { /* for UI test */
  1943. mt_battery_notify_UI_test();
  1944. }
  1945. }
  1946. static void mt_battery_thermal_check(void)
  1947. {
  1948. if ((g_battery_thermal_throttling_flag == 1) || (g_battery_thermal_throttling_flag == 3)) {
  1949. if (battery_cmd_thermal_test_mode == 1) {
  1950. BMT_status.temperature = battery_cmd_thermal_test_mode_value;
  1951. battery_log(BAT_LOG_FULL,
  1952. "[Battery] In thermal_test_mode , Tbat=%d\n",
  1953. BMT_status.temperature);
  1954. }
  1955. #if defined(CONFIG_MTK_JEITA_STANDARD_SUPPORT)
  1956. if (BMT_status.temperature > p_bat_charging_data->max_discharge_temperature
  1957. || BMT_status.temperature < p_bat_charging_data->min_discharge_temperature) {
  1958. struct battery_data *bat_data = &battery_main;
  1959. struct power_supply *bat_psy = &bat_data->psy;
  1960. pr_warn("[Battery] Tbat(%d)out of range, power down device.\n",
  1961. BMT_status.temperature);
  1962. bat_data->BAT_CAPACITY = 0;
  1963. power_supply_changed(bat_psy);
  1964. /* can not power down due to charger exist, so need reset system */
  1965. if (BMT_status.charger_exist)
  1966. bat_charger_set_platform_reset();
  1967. else
  1968. orderly_poweroff(true);
  1969. }
  1970. #else
  1971. if (BMT_status.temperature >= p_bat_charging_data->max_discharge_temperature) {
  1972. #if defined(CONFIG_POWER_EXT)
  1973. battery_log(BAT_LOG_CRTI,
  1974. "[BATTERY] CONFIG_POWER_EXT, no update battery update power down.\n");
  1975. #else
  1976. {
  1977. if ((g_platform_boot_mode == META_BOOT)
  1978. || (g_platform_boot_mode == ADVMETA_BOOT)
  1979. || (g_platform_boot_mode == ATE_FACTORY_BOOT)) {
  1980. battery_log(BAT_LOG_FULL,
  1981. "[BATTERY] boot mode = %d, bypass temperature check\n",
  1982. g_platform_boot_mode);
  1983. } else {
  1984. struct battery_data *bat_data = &battery_main;
  1985. struct power_supply *bat_psy = &bat_data->psy;
  1986. battery_log(BAT_LOG_ERROR,
  1987. "[Battery] Tbat(%d)>=%d, system need power down.\n",
  1988. p_bat_charging_data->max_discharge_temperature,
  1989. BMT_status.temperature);
  1990. bat_data->BAT_CAPACITY = 0;
  1991. power_supply_changed(bat_psy);
  1992. /* can not power down due to charger exist, so need reset system */
  1993. if (BMT_status.charger_exist)
  1994. bat_charger_set_platform_reset();
  1995. else
  1996. orderly_poweroff(true);
  1997. }
  1998. }
  1999. #endif
  2000. }
  2001. #endif
  2002. }
  2003. }
  2004. int bat_charger_type_detection(void)
  2005. {
  2006. mutex_lock(&charger_type_mutex);
  2007. if (BMT_status.charger_type == CHARGER_UNKNOWN)
  2008. BMT_status.charger_type = bat_charger_get_charger_type();
  2009. mutex_unlock(&charger_type_mutex);
  2010. return BMT_status.charger_type;
  2011. }
  2012. static void mt_battery_charger_detect_check(void)
  2013. {
  2014. static bool fg_first_detect;
  2015. if (upmu_is_chr_det() == true) {
  2016. if (!BMT_status.charger_exist)
  2017. wake_lock(&battery_suspend_lock);
  2018. BMT_status.charger_exist = true;
  2019. /* re-detect once after 10s if it is non-standard type */
  2020. if (BMT_status.charger_type == NONSTANDARD_CHARGER && fg_first_detect) {
  2021. mutex_lock(&charger_type_mutex);
  2022. BMT_status.charger_type = bat_charger_get_charger_type();
  2023. mutex_unlock(&charger_type_mutex);
  2024. fg_first_detect = false;
  2025. if (BMT_status.charger_type != NONSTANDARD_CHARGER)
  2026. pr_warn("Update charger type to %d!\n", BMT_status.charger_type);
  2027. }
  2028. if (BMT_status.charger_type == CHARGER_UNKNOWN) {
  2029. bat_charger_type_detection();
  2030. if ((BMT_status.charger_type == STANDARD_HOST)
  2031. || (BMT_status.charger_type == CHARGING_HOST)) {
  2032. mt_usb_connect();
  2033. }
  2034. if (BMT_status.charger_type != CHARGER_UNKNOWN)
  2035. fg_first_detect = true;
  2036. }
  2037. battery_log(BAT_LOG_FULL, "[BAT_thread]Cable in, CHR_Type_num=%d\r\n",
  2038. BMT_status.charger_type);
  2039. } else {
  2040. if (BMT_status.charger_exist)
  2041. wake_lock_timeout(&battery_suspend_lock, HZ / 2);
  2042. fg_first_detect = false;
  2043. BMT_status.charger_exist = false;
  2044. BMT_status.charger_type = CHARGER_UNKNOWN;
  2045. BMT_status.bat_full = false;
  2046. BMT_status.bat_in_recharging_state = false;
  2047. BMT_status.bat_charging_state = CHR_PRE;
  2048. BMT_status.total_charging_time = 0;
  2049. BMT_status.PRE_charging_time = 0;
  2050. BMT_status.CC_charging_time = 0;
  2051. BMT_status.TOPOFF_charging_time = 0;
  2052. BMT_status.POSTFULL_charging_time = 0;
  2053. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2054. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2055. || g_platform_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2056. pr_warn
  2057. ("Unplug Charger/USB In Kernel Power Off Charging Mode! Shutdown OS!\r\n");
  2058. orderly_poweroff(true);
  2059. }
  2060. #endif
  2061. if (g_cmd_hold_charging) {
  2062. g_cmd_hold_charging = false;
  2063. bat_charger_enable_power_path(true);
  2064. }
  2065. battery_log(BAT_LOG_FULL, "[BAT_thread]Cable out \r\n");
  2066. mt_usb_disconnect();
  2067. }
  2068. }
  2069. static void mt_battery_update_status(void)
  2070. {
  2071. #if defined(CONFIG_POWER_EXT)
  2072. battery_log(BAT_LOG_CRTI, "[BATTERY] CONFIG_POWER_EXT, no update Android.\n");
  2073. #else
  2074. {
  2075. if (battery_meter_initilized == true)
  2076. battery_update(&battery_main);
  2077. ac_update(&ac_main);
  2078. usb_update(&usb_main);
  2079. }
  2080. #endif
  2081. }
  2082. static void do_chrdet_int_task(void)
  2083. {
  2084. if (g_bat_init_flag == true) {
  2085. if (upmu_is_chr_det() == true) {
  2086. pr_debug("[do_chrdet_int_task] charger exist!\n");
  2087. if (!BMT_status.charger_exist)
  2088. wake_lock(&battery_suspend_lock);
  2089. BMT_status.charger_exist = true;
  2090. #if defined(CONFIG_POWER_EXT)
  2091. bat_charger_type_detection();
  2092. mt_usb_connect();
  2093. battery_log(BAT_LOG_CRTI,
  2094. "[do_chrdet_int_task] call mt_usb_connect() in EVB\n");
  2095. #endif
  2096. } else {
  2097. pr_debug("[do_chrdet_int_task] charger NOT exist!\n");
  2098. if (BMT_status.charger_exist) {
  2099. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT)
  2100. wake_lock(&battery_suspend_lock);
  2101. else
  2102. wake_lock_timeout(&battery_suspend_lock, HZ / 2);
  2103. }
  2104. BMT_status.charger_exist = false;
  2105. BMT_status.charger_type = CHARGER_UNKNOWN;
  2106. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2107. if (g_platform_boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2108. || g_platform_boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2109. battery_log(BAT_LOG_CRTI,
  2110. "[pmic_thread_kthread] Unplug Charger/USB In Kernel Power Off Charging Mode! Shutdown OS!\r\n");
  2111. orderly_poweroff(true);
  2112. }
  2113. #endif
  2114. #if defined(CONFIG_POWER_EXT)
  2115. mt_usb_disconnect();
  2116. battery_log(BAT_LOG_CRTI,
  2117. "[do_chrdet_int_task] call mt_usb_disconnect() in EVB\n");
  2118. #endif
  2119. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2120. is_ta_connect = false;
  2121. ta_check_chr_type = true;
  2122. ta_cable_out_occur = true;
  2123. #endif
  2124. }
  2125. if (BMT_status.UI_SOC == 100 && BMT_status.charger_exist) {
  2126. BMT_status.bat_charging_state = CHR_BATFULL;
  2127. BMT_status.bat_full = true;
  2128. g_charging_full_reset_bat_meter = true;
  2129. }
  2130. mt_battery_update_status();
  2131. wake_up_bat();
  2132. } else {
  2133. battery_log(BAT_LOG_CRTI,
  2134. "[do_chrdet_int_task] battery thread not ready, will do after bettery init.\n");
  2135. }
  2136. }
  2137. irqreturn_t ops_chrdet_int_handler(int irq, void *dev_id)
  2138. {
  2139. pr_debug("[Power/Battery][chrdet_bat_int_handler]....\n");
  2140. do_chrdet_int_task();
  2141. return IRQ_HANDLED;
  2142. }
  2143. void BAT_thread(void)
  2144. {
  2145. if (battery_meter_initilized == false) {
  2146. battery_meter_initial(); /* move from battery_probe() to decrease booting time */
  2147. BMT_status.nPercent_ZCV = battery_meter_get_battery_nPercent_zcv();
  2148. battery_meter_initilized = true;
  2149. }
  2150. mt_battery_charger_detect_check();
  2151. if (fg_battery_shutdown)
  2152. return;
  2153. mt_battery_GetBatteryData();
  2154. if (fg_battery_shutdown)
  2155. return;
  2156. mt_battery_thermal_check();
  2157. mt_battery_notify_check();
  2158. if (BMT_status.charger_exist && !fg_battery_shutdown) {
  2159. mt_battery_CheckBatteryStatus();
  2160. mt_battery_charging_algorithm();
  2161. }
  2162. bat_charger_reset_watchdog_timer();
  2163. mt_battery_update_status();
  2164. }
  2165. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2166. /* // Internal API */
  2167. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2168. int bat_thread_kthread(void *x)
  2169. {
  2170. ktime_t ktime = ktime_set(3, 0); /* 10s, 10* 1000 ms */
  2171. /* Run on a process content */
  2172. while (!fg_battery_shutdown) {
  2173. int ret;
  2174. mutex_lock(&bat_mutex);
  2175. if (g_bat.init_done && !battery_suspended)
  2176. BAT_thread();
  2177. mutex_unlock(&bat_mutex);
  2178. ret = wait_event_hrtimeout(bat_thread_wq, atomic_read(&bat_thread_wakeup), ktime);
  2179. if (ret == -ETIME)
  2180. bat_meter_timeout = true;
  2181. else
  2182. atomic_dec(&bat_thread_wakeup);
  2183. pr_debug("%s: waking up: on %s; wake_flag=%d\n",
  2184. __func__, ret == -ETIME ? "timer" : "event",
  2185. atomic_read(&bat_thread_wakeup));
  2186. if (!fg_battery_shutdown)
  2187. ktime = ktime_set(BAT_TASK_PERIOD, 0); /* 10s, 10* 1000 ms */
  2188. if (chr_wake_up_bat == true) { /* for charger plug in/ out */
  2189. if (g_bat.init_done)
  2190. battery_meter_reset(false);
  2191. chr_wake_up_bat = false;
  2192. battery_log(BAT_LOG_CRTI,
  2193. "[BATTERY] Charger plug in/out, Call battery_meter_reset. (%d)\n",
  2194. BMT_status.UI_SOC);
  2195. }
  2196. }
  2197. mutex_lock(&bat_mutex);
  2198. g_bat.down = true;
  2199. mutex_unlock(&bat_mutex);
  2200. return 0;
  2201. }
  2202. /*
  2203. * This is charger interface to USB OTG code.
  2204. * If OTG is host, charger functionality, and charger interrupt
  2205. * must be disabled
  2206. * */
  2207. void bat_detect_set_usb_host_mode(bool usb_host_mode)
  2208. {
  2209. mutex_lock(&bat_mutex);
  2210. /* Don't change the charger event state
  2211. * if charger logic is not running */
  2212. if (g_bat.init_done) {
  2213. if (usb_host_mode && !g_bat.usb_host_mode)
  2214. disable_irq(g_bat.irq);
  2215. if (!usb_host_mode && g_bat.usb_host_mode)
  2216. enable_irq(g_bat.irq);
  2217. g_bat.usb_host_mode = usb_host_mode;
  2218. }
  2219. mutex_unlock(&bat_mutex);
  2220. }
  2221. EXPORT_SYMBOL(bat_detect_set_usb_host_mode);
  2222. static int bat_setup_charger_locked(void)
  2223. {
  2224. int ret = -EAGAIN;
  2225. if (g_bat.common_init_done && g_bat.charger && !g_bat.init_done) {
  2226. /* AP:
  2227. * Both common_battery and charger code are ready to go.
  2228. * Finalize init of common_battery.
  2229. */
  2230. g_platform_boot_mode = bat_charger_get_platform_boot_mode();
  2231. battery_log(BAT_LOG_CRTI, "[BAT_probe] g_platform_boot_mode = %d\n ",
  2232. g_platform_boot_mode);
  2233. /* AP:
  2234. * MTK implementation requires that BAT_thread() be called at least once
  2235. * before battery event is enabled.
  2236. * Although this should not be necessary, we maintain compatibility
  2237. * until rework is complete.
  2238. */
  2239. BAT_thread();
  2240. g_bat.init_done = true;
  2241. ret = irq_set_irq_wake(g_bat.irq, true);
  2242. if (ret)
  2243. pr_err("%s: irq_set_irq_wake err = %d\n", __func__, ret);
  2244. enable_irq(g_bat.irq);
  2245. pr_warn("%s: charger setup done\n", __func__);
  2246. }
  2247. /* if there is no external charger, we just enable detect irq */
  2248. #if defined(CONFIG_POWER_EXT) && defined(NO_EXTERNAL_CHARGER)
  2249. ret = irq_set_irq_wake(g_bat.irq, true);
  2250. if (ret)
  2251. pr_err("%s: irq_set_irq_wake err = %d\n", __func__, ret);
  2252. enable_irq(g_bat.irq);
  2253. pr_warn("%s: no charger. just enable detect irq.\n", __func__);
  2254. #endif
  2255. return ret;
  2256. }
  2257. int bat_charger_register(CHARGING_CONTROL ctrl)
  2258. {
  2259. int ret;
  2260. mutex_lock(&bat_mutex);
  2261. g_bat.charger = ctrl;
  2262. ret = bat_setup_charger_locked();
  2263. mutex_unlock(&bat_mutex);
  2264. return ret;
  2265. }
  2266. EXPORT_SYMBOL(bat_charger_register);
  2267. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2268. /* // fop API */
  2269. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2270. static long adc_cali_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2271. {
  2272. int *user_data_addr;
  2273. int *naram_data_addr;
  2274. int i = 0;
  2275. int ret = 0;
  2276. int adc_in_data[2] = { 1, 1 };
  2277. int adc_out_data[2] = { 1, 1 };
  2278. mutex_lock(&bat_mutex);
  2279. switch (cmd) {
  2280. case TEST_ADC_CALI_PRINT:
  2281. g_ADC_Cali = false;
  2282. break;
  2283. case SET_ADC_CALI_Slop:
  2284. naram_data_addr = (int *)arg;
  2285. ret = copy_from_user(adc_cali_slop, naram_data_addr, 36);
  2286. g_ADC_Cali = false; /* enable calibration after setting ADC_CALI_Cal */
  2287. /* Protection */
  2288. for (i = 0; i < 14; i++) {
  2289. if ((*(adc_cali_slop + i) == 0) || (*(adc_cali_slop + i) == 1))
  2290. *(adc_cali_slop + i) = 1000;
  2291. }
  2292. /*
  2293. for (i = 0; i < 14; i++)
  2294. battery_log(BAT_LOG_CRTI, "adc_cali_slop[%d] = %d\n", i,
  2295. *(adc_cali_slop + i));
  2296. battery_log(BAT_LOG_FULL,
  2297. "**** unlocked_ioctl : SET_ADC_CALI_Slop Done!\n");
  2298. */
  2299. break;
  2300. case SET_ADC_CALI_Offset:
  2301. naram_data_addr = (int *)arg;
  2302. ret = copy_from_user(adc_cali_offset, naram_data_addr, 36);
  2303. g_ADC_Cali = false; /* enable calibration after setting ADC_CALI_Cal */
  2304. /*
  2305. for (i = 0; i < 14; i++)
  2306. battery_log(BAT_LOG_CRTI, "adc_cali_offset[%d] = %d\n", i,
  2307. *(adc_cali_offset + i));
  2308. battery_log(BAT_LOG_FULL,
  2309. "**** unlocked_ioctl : SET_ADC_CALI_Offset Done!\n");
  2310. */
  2311. break;
  2312. case SET_ADC_CALI_Cal:
  2313. naram_data_addr = (int *)arg;
  2314. ret = copy_from_user(adc_cali_cal, naram_data_addr, 4);
  2315. g_ADC_Cali = true;
  2316. if (adc_cali_cal[0] == 1)
  2317. g_ADC_Cali = true;
  2318. else
  2319. g_ADC_Cali = false;
  2320. for (i = 0; i < 1; i++)
  2321. battery_log(BAT_LOG_CRTI, "adc_cali_cal[%d] = %d\n", i,
  2322. *(adc_cali_cal + i));
  2323. battery_log(BAT_LOG_FULL, "**** unlocked_ioctl : SET_ADC_CALI_Cal Done!\n");
  2324. break;
  2325. case ADC_CHANNEL_READ:
  2326. /* g_ADC_Cali = false; // 20100508 Infinity */
  2327. user_data_addr = (int *)arg;
  2328. ret = copy_from_user(adc_in_data, user_data_addr, 8); /* 2*int = 2*4 */
  2329. if (adc_in_data[0] == 0) /* I_SENSE */
  2330. adc_out_data[0] = battery_meter_get_VSense() * adc_in_data[1];
  2331. else if (adc_in_data[0] == 1) /* BAT_SENSE */
  2332. adc_out_data[0] = battery_meter_get_battery_voltage() * adc_in_data[1];
  2333. else if (adc_in_data[0] == 3) /* V_Charger */
  2334. adc_out_data[0] = battery_meter_get_charger_voltage() * adc_in_data[1];
  2335. else if (adc_in_data[0] == 30) /* V_Bat_temp magic number */
  2336. adc_out_data[0] = battery_meter_get_battery_temperature() * adc_in_data[1];
  2337. else if (adc_in_data[0] == 66) {
  2338. adc_out_data[0] = (battery_meter_get_battery_current()) / 10;
  2339. if (battery_meter_get_battery_current_sign() == true)
  2340. adc_out_data[0] = 0 - adc_out_data[0]; /* charging */
  2341. } else {
  2342. battery_log(BAT_LOG_FULL, "unknown channel(%d,%d)\n",
  2343. adc_in_data[0], adc_in_data[1]);
  2344. }
  2345. if (adc_out_data[0] < 0)
  2346. adc_out_data[1] = 1; /* failed */
  2347. else
  2348. adc_out_data[1] = 0; /* success */
  2349. if (adc_in_data[0] == 30)
  2350. adc_out_data[1] = 0; /* success */
  2351. if (adc_in_data[0] == 66)
  2352. adc_out_data[1] = 0; /* success */
  2353. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2354. battery_log(BAT_LOG_CRTI,
  2355. "**** unlocked_ioctl : Channel %d * %d times = %d\n",
  2356. adc_in_data[0], adc_in_data[1], adc_out_data[0]);
  2357. break;
  2358. case BAT_STATUS_READ:
  2359. user_data_addr = (int *)arg;
  2360. ret = copy_from_user(battery_in_data, user_data_addr, 4);
  2361. /* [0] is_CAL */
  2362. if (g_ADC_Cali)
  2363. battery_out_data[0] = 1;
  2364. else
  2365. battery_out_data[0] = 0;
  2366. ret = copy_to_user(user_data_addr, battery_out_data, 4);
  2367. battery_log(BAT_LOG_CRTI, "**** unlocked_ioctl : CAL:%d\n", battery_out_data[0]);
  2368. break;
  2369. case Set_Charger_Current: /* For Factory Mode */
  2370. user_data_addr = (int *)arg;
  2371. ret = copy_from_user(charging_level_data, user_data_addr, 4);
  2372. g_ftm_battery_flag = true;
  2373. if (charging_level_data[0] == 0)
  2374. charging_level_data[0] = CHARGE_CURRENT_70_00_MA;
  2375. else if (charging_level_data[0] == 1)
  2376. charging_level_data[0] = CHARGE_CURRENT_200_00_MA;
  2377. else if (charging_level_data[0] == 2)
  2378. charging_level_data[0] = CHARGE_CURRENT_400_00_MA;
  2379. else if (charging_level_data[0] == 3)
  2380. charging_level_data[0] = CHARGE_CURRENT_450_00_MA;
  2381. else if (charging_level_data[0] == 4)
  2382. charging_level_data[0] = CHARGE_CURRENT_550_00_MA;
  2383. else if (charging_level_data[0] == 5)
  2384. charging_level_data[0] = CHARGE_CURRENT_650_00_MA;
  2385. else if (charging_level_data[0] == 6)
  2386. charging_level_data[0] = CHARGE_CURRENT_700_00_MA;
  2387. else if (charging_level_data[0] == 7)
  2388. charging_level_data[0] = CHARGE_CURRENT_800_00_MA;
  2389. else if (charging_level_data[0] == 8)
  2390. charging_level_data[0] = CHARGE_CURRENT_900_00_MA;
  2391. else if (charging_level_data[0] == 9)
  2392. charging_level_data[0] = CHARGE_CURRENT_1000_00_MA;
  2393. else if (charging_level_data[0] == 10)
  2394. charging_level_data[0] = CHARGE_CURRENT_1100_00_MA;
  2395. else if (charging_level_data[0] == 11)
  2396. charging_level_data[0] = CHARGE_CURRENT_1200_00_MA;
  2397. else if (charging_level_data[0] == 12)
  2398. charging_level_data[0] = CHARGE_CURRENT_1300_00_MA;
  2399. else if (charging_level_data[0] == 13)
  2400. charging_level_data[0] = CHARGE_CURRENT_1400_00_MA;
  2401. else if (charging_level_data[0] == 14)
  2402. charging_level_data[0] = CHARGE_CURRENT_1500_00_MA;
  2403. else if (charging_level_data[0] == 15)
  2404. charging_level_data[0] = CHARGE_CURRENT_1600_00_MA;
  2405. else
  2406. charging_level_data[0] = CHARGE_CURRENT_450_00_MA;
  2407. wake_up_bat();
  2408. battery_log(BAT_LOG_CRTI, "**** unlocked_ioctl : set_Charger_Current:%d\n",
  2409. charging_level_data[0]);
  2410. break;
  2411. /* add for meta tool------------------------------- */
  2412. case Get_META_BAT_VOL:
  2413. user_data_addr = (int *)arg;
  2414. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2415. adc_out_data[0] = BMT_status.bat_vol;
  2416. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2417. break;
  2418. case Get_META_BAT_SOC:
  2419. user_data_addr = (int *)arg;
  2420. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2421. adc_out_data[0] = BMT_status.UI_SOC;
  2422. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2423. break;
  2424. /* add bing meta tool------------------------------- */
  2425. default:
  2426. g_ADC_Cali = false;
  2427. break;
  2428. }
  2429. mutex_unlock(&bat_mutex);
  2430. return 0;
  2431. }
  2432. static int adc_cali_open(struct inode *inode, struct file *file)
  2433. {
  2434. return 0;
  2435. }
  2436. static int adc_cali_release(struct inode *inode, struct file *file)
  2437. {
  2438. return 0;
  2439. }
  2440. static const struct file_operations adc_cali_fops = {
  2441. .owner = THIS_MODULE,
  2442. .unlocked_ioctl = adc_cali_ioctl,
  2443. .open = adc_cali_open,
  2444. .release = adc_cali_release,
  2445. };
  2446. void check_battery_exist(void)
  2447. {
  2448. #if defined(CONFIG_CONFIG_DIS_CHECK_BATTERY)
  2449. battery_log(BAT_LOG_FULL, "[BATTERY] Disable check battery exist.\n");
  2450. #else
  2451. u32 baton_count = 0;
  2452. u32 i;
  2453. for (i = 0; i < 3; i++)
  2454. baton_count += bat_charger_get_battery_status();
  2455. if (baton_count >= 3) {
  2456. if ((g_platform_boot_mode == META_BOOT) || (g_platform_boot_mode == ADVMETA_BOOT)
  2457. || (g_platform_boot_mode == ATE_FACTORY_BOOT)) {
  2458. battery_log(BAT_LOG_FULL,
  2459. "[BATTERY] boot mode = %d, bypass battery check\n",
  2460. g_platform_boot_mode);
  2461. } else {
  2462. battery_log(BAT_LOG_FULL,
  2463. "[BATTERY] Battery is not exist, power off FAN5405 and system (%d)\n",
  2464. baton_count);
  2465. bat_charger_enable(false);
  2466. bat_charger_set_platform_reset();
  2467. }
  2468. }
  2469. #endif
  2470. }
  2471. static void bat_parse_node(struct device_node *np, char *name, int *cust_val)
  2472. {
  2473. u32 val;
  2474. if (of_property_read_u32(np, name, &val) == 0) {
  2475. (*cust_val) = (int)val;
  2476. pr_debug("%s get %s :%d\n", __func__, name, *cust_val);
  2477. }
  2478. }
  2479. static void init_charging_data_from_dt(struct device_node *np)
  2480. {
  2481. bat_parse_node(np, "v_charger_max", &p_bat_charging_data->v_charger_max);
  2482. bat_parse_node(np, "v_charger_min", &p_bat_charging_data->v_charger_min);
  2483. bat_parse_node(np, "max_discharge_temperature", &p_bat_charging_data->max_discharge_temperature);
  2484. bat_parse_node(np, "min_discharge_temperature", &p_bat_charging_data->min_discharge_temperature);
  2485. bat_parse_node(np, "max_charge_temperature", &p_bat_charging_data->max_charge_temperature);
  2486. bat_parse_node(np, "min_charge_temperature", &p_bat_charging_data->min_charge_temperature);
  2487. bat_parse_node(np, "use_avg_temperature", &p_bat_charging_data->use_avg_temperature);
  2488. bat_parse_node(np, "usb_charger_current", &p_bat_charging_data->usb_charger_current);
  2489. bat_parse_node(np, "ac_charger_current", &p_bat_charging_data->ac_charger_current);
  2490. bat_parse_node(np, "non_std_ac_charger_current", &p_bat_charging_data->non_std_ac_charger_current);
  2491. bat_parse_node(np, "charging_host_charger_current", &p_bat_charging_data->charging_host_charger_current);
  2492. bat_parse_node(np, "apple_0_5a_charger_current", &p_bat_charging_data->apple_0_5a_charger_current);
  2493. bat_parse_node(np, "apple_1_0a_charger_current", &p_bat_charging_data->apple_1_0a_charger_current);
  2494. bat_parse_node(np, "apple_2_1a_charger_current", &p_bat_charging_data->apple_2_1a_charger_current);
  2495. bat_parse_node(np, "ta_start_battery_soc", &p_bat_charging_data->ta_start_battery_soc);
  2496. bat_parse_node(np, "ta_stop_battery_soc", &p_bat_charging_data->ta_stop_battery_soc);
  2497. bat_parse_node(np, "ta_ac_9v_input_current", &p_bat_charging_data->ta_ac_9v_input_current);
  2498. bat_parse_node(np, "ta_ac_7v_input_current", &p_bat_charging_data->ta_ac_7v_input_current);
  2499. bat_parse_node(np, "ta_ac_charging_current", &p_bat_charging_data->ta_ac_charging_current);
  2500. bat_parse_node(np, "ta_9v_support", &p_bat_charging_data->ta_9v_support);
  2501. bat_parse_node(np, "temp_pos_60_threshold", &p_bat_charging_data->temp_pos_60_threshold);
  2502. bat_parse_node(np, "temp_pos_60_thres_minus_x_degree", &p_bat_charging_data->temp_pos_60_thres_minus_x_degree);
  2503. bat_parse_node(np, "temp_pos_45_threshold", &p_bat_charging_data->temp_pos_45_threshold);
  2504. bat_parse_node(np, "temp_pos_45_thres_minus_x_degree", &p_bat_charging_data->temp_pos_45_thres_minus_x_degree);
  2505. bat_parse_node(np, "temp_pos_10_threshold", &p_bat_charging_data->temp_pos_10_threshold);
  2506. bat_parse_node(np, "temp_pos_10_thres_plus_x_degree", &p_bat_charging_data->temp_pos_10_thres_plus_x_degree);
  2507. bat_parse_node(np, "temp_pos_0_threshold ", &p_bat_charging_data->temp_pos_0_threshold);
  2508. bat_parse_node(np, "temp_pos_0_thres_plus_x_degree", &p_bat_charging_data->temp_pos_0_thres_plus_x_degree);
  2509. bat_parse_node(np, "temp_neg_10_threshold", &p_bat_charging_data->temp_neg_10_threshold);
  2510. bat_parse_node(np, "temp_neg_10_thres_plus_x_degree",
  2511. &p_bat_charging_data->temp_neg_10_thres_plus_x_degree);
  2512. bat_parse_node(np, "jeita_temp_above_pos_60_cv_voltage ",
  2513. &p_bat_charging_data->jeita_temp_above_pos_60_cv_voltage);
  2514. bat_parse_node(np, "jeita_temp_pos_45_to_pos_60_cv_voltage",
  2515. &p_bat_charging_data->jeita_temp_pos_45_to_pos_60_cv_voltage);
  2516. bat_parse_node(np, "jeita_temp_pos_10_to_pos_45_cv_voltage",
  2517. &p_bat_charging_data->jeita_temp_pos_10_to_pos_45_cv_voltage);
  2518. bat_parse_node(np, "jeita_temp_pos_0_to_pos_10_cv_voltage",
  2519. &p_bat_charging_data->jeita_temp_pos_0_to_pos_10_cv_voltage);
  2520. bat_parse_node(np, "jeita_temp_neg_10_to_pos_0_cv_voltage",
  2521. &p_bat_charging_data->jeita_temp_neg_10_to_pos_0_cv_voltage);
  2522. bat_parse_node(np, "jeita_temp_below_neg_10_cv_voltage",
  2523. &p_bat_charging_data->jeita_temp_below_neg_10_cv_voltage);
  2524. }
  2525. static int battery_probe(struct platform_device *pdev)
  2526. {
  2527. struct class_device *class_dev = NULL;
  2528. struct device *dev = &pdev->dev;
  2529. int ret = 0;
  2530. pr_debug("******** battery driver probe!! ********\n");
  2531. /* AP:
  2532. * Use PMIC events as interrupts through kernel IRQ API.
  2533. */
  2534. atomic_set(&bat_thread_wakeup, 0);
  2535. g_bat.irq = platform_get_irq(pdev, 0);
  2536. if (g_bat.irq <= 0)
  2537. return -EINVAL;
  2538. p_bat_charging_data = (struct mt_battery_charging_custom_data *)dev_get_platdata(dev);
  2539. if (!p_bat_charging_data) {
  2540. pr_err("%s: no platform data. replace with default settings.\n", __func__);
  2541. p_bat_charging_data = &default_charging_data;
  2542. /* populate property here */
  2543. init_charging_data_from_dt(pdev->dev.of_node);
  2544. }
  2545. irq_set_status_flags(g_bat.irq, IRQ_NOAUTOEN);
  2546. ret = request_threaded_irq(g_bat.irq, NULL,
  2547. ops_chrdet_int_handler,
  2548. IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "ops_mt6397_chrdet", pdev);
  2549. if (ret) {
  2550. pr_err("%s: request_threaded_irq err = %d\n", __func__, ret);
  2551. return ret;
  2552. }
  2553. /* Integrate with NVRAM */
  2554. ret = alloc_chrdev_region(&adc_cali_devno, 0, 1, ADC_CALI_DEVNAME);
  2555. if (ret)
  2556. battery_log(BAT_LOG_CRTI, "Error: Can't Get Major number for adc_cali\n");
  2557. adc_cali_cdev = cdev_alloc();
  2558. adc_cali_cdev->owner = THIS_MODULE;
  2559. adc_cali_cdev->ops = &adc_cali_fops;
  2560. ret = cdev_add(adc_cali_cdev, adc_cali_devno, 1);
  2561. if (ret)
  2562. battery_log(BAT_LOG_CRTI, "adc_cali Error: cdev_add\n");
  2563. adc_cali_major = MAJOR(adc_cali_devno);
  2564. adc_cali_class = class_create(THIS_MODULE, ADC_CALI_DEVNAME);
  2565. class_dev = (struct class_device *)device_create(adc_cali_class,
  2566. NULL,
  2567. adc_cali_devno, NULL, ADC_CALI_DEVNAME);
  2568. battery_log(BAT_LOG_CRTI, "[BAT_probe] adc_cali prepare : done !!\n ");
  2569. wake_lock_init(&battery_suspend_lock, WAKE_LOCK_SUSPEND, "battery suspend wakelock");
  2570. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2571. wake_lock_init(&TA_charger_suspend_lock, WAKE_LOCK_SUSPEND, "TA charger suspend wakelock");
  2572. #endif
  2573. /* Integrate with Android Battery Service */
  2574. ret = power_supply_register(dev, &ac_main.psy);
  2575. if (ret) {
  2576. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register AC Fail !!\n");
  2577. return ret;
  2578. }
  2579. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register AC Success !!\n");
  2580. ret = power_supply_register(dev, &usb_main.psy);
  2581. if (ret) {
  2582. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register USB Fail !!\n");
  2583. return ret;
  2584. }
  2585. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register USB Success !!\n");
  2586. ret = power_supply_register(dev, &battery_main.psy);
  2587. if (ret) {
  2588. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register Battery Fail !!\n");
  2589. return ret;
  2590. }
  2591. battery_log(BAT_LOG_CRTI, "[BAT_probe] power_supply_register Battery Success !!\n");
  2592. #if !defined(CONFIG_POWER_EXT)
  2593. /* For EM */
  2594. {
  2595. int ret_device_file = 0;
  2596. ret_device_file = device_create_file(dev, &dev_attr_ADC_Charger_Voltage);
  2597. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_0_Slope);
  2598. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_1_Slope);
  2599. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_2_Slope);
  2600. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_3_Slope);
  2601. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_4_Slope);
  2602. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_5_Slope);
  2603. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_6_Slope);
  2604. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_7_Slope);
  2605. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_8_Slope);
  2606. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_9_Slope);
  2607. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_10_Slope);
  2608. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_11_Slope);
  2609. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_12_Slope);
  2610. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_13_Slope);
  2611. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_0_Offset);
  2612. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_1_Offset);
  2613. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_2_Offset);
  2614. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_3_Offset);
  2615. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_4_Offset);
  2616. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_5_Offset);
  2617. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_6_Offset);
  2618. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_7_Offset);
  2619. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_8_Offset);
  2620. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_9_Offset);
  2621. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_10_Offset);
  2622. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_11_Offset);
  2623. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_12_Offset);
  2624. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_13_Offset);
  2625. ret_device_file = device_create_file(dev, &dev_attr_ADC_Channel_Is_Calibration);
  2626. ret_device_file = device_create_file(dev, &dev_attr_Power_On_Voltage);
  2627. ret_device_file = device_create_file(dev, &dev_attr_Power_Off_Voltage);
  2628. ret_device_file = device_create_file(dev, &dev_attr_Charger_TopOff_Value);
  2629. ret_device_file = device_create_file(dev, &dev_attr_FG_Battery_CurrentConsumption);
  2630. ret_device_file = device_create_file(dev, &dev_attr_FG_SW_CoulombCounter);
  2631. ret_device_file = device_create_file(dev, &dev_attr_Charging_CallState);
  2632. ret_device_file = device_create_file(dev, &dev_attr_Charging_Enable);
  2633. ret_device_file = device_create_file(dev, &dev_attr_Custom_Charging_Current);
  2634. #if defined(CONFIG_MTK_PUMP_EXPRESS_PLUS_SUPPORT)
  2635. ret_device_file = device_create_file(dev, &dev_attr_Pump_Express);
  2636. #endif
  2637. }
  2638. /* battery_meter_initial(); //move to mt_battery_GetBatteryData() to decrease booting time */
  2639. /* Initialization BMT Struct */
  2640. BMT_status.bat_exist = true; /* phone must have battery */
  2641. BMT_status.charger_exist = false; /* for default, no charger */
  2642. BMT_status.bat_vol = 0;
  2643. BMT_status.ICharging = 0;
  2644. BMT_status.temperature = 0;
  2645. BMT_status.charger_vol = 0;
  2646. BMT_status.total_charging_time = 0;
  2647. BMT_status.PRE_charging_time = 0;
  2648. BMT_status.CC_charging_time = 0;
  2649. BMT_status.TOPOFF_charging_time = 0;
  2650. BMT_status.POSTFULL_charging_time = 0;
  2651. BMT_status.SOC = 0;
  2652. BMT_status.UI_SOC = -1;
  2653. BMT_status.bat_charging_state = CHR_PRE;
  2654. BMT_status.bat_in_recharging_state = false;
  2655. BMT_status.bat_full = false;
  2656. BMT_status.nPercent_ZCV = 0;
  2657. BMT_status.nPrecent_UI_SOC_check_point = battery_meter_get_battery_nPercent_UI_SOC();
  2658. kthread_run(bat_thread_kthread, NULL, "bat_thread_kthread");
  2659. battery_log(BAT_LOG_CRTI, "[battery_probe] bat_thread_kthread Done\n");
  2660. /*LOG System Set */
  2661. init_proc_log();
  2662. #endif
  2663. g_bat_init_flag = true;
  2664. mutex_lock(&bat_mutex);
  2665. g_bat.common_init_done = true;
  2666. bat_setup_charger_locked();
  2667. mutex_unlock(&bat_mutex);
  2668. return 0;
  2669. }
  2670. static int battery_remove(struct platform_device *dev)
  2671. {
  2672. battery_log(BAT_LOG_CRTI, "******** battery driver remove!! ********\n");
  2673. return 0;
  2674. }
  2675. static void battery_shutdown(struct platform_device *pdev)
  2676. {
  2677. #if !defined(CONFIG_POWER_EXT)
  2678. int count = 0;
  2679. #endif
  2680. pr_debug("******** battery driver shutdown!! ********\n");
  2681. disable_irq(g_bat.irq);
  2682. mutex_lock(&bat_mutex);
  2683. fg_battery_shutdown = true;
  2684. wake_up_bat_update_meter();
  2685. #if !defined(CONFIG_POWER_EXT)
  2686. while (!g_bat.down && count < 5) {
  2687. mutex_unlock(&bat_mutex);
  2688. msleep(20);
  2689. count++;
  2690. mutex_lock(&bat_mutex);
  2691. }
  2692. if (!g_bat.down)
  2693. pr_err("%s: failed to terminate battery thread\n", __func__);
  2694. #endif
  2695. /* turn down interrupt thread and wakeup ability */
  2696. if (g_bat.init_done)
  2697. irq_set_irq_wake(g_bat.irq, false);
  2698. free_irq(g_bat.irq, pdev);
  2699. mutex_unlock(&bat_mutex);
  2700. }
  2701. static int battery_suspend(struct platform_device *dev, pm_message_t state)
  2702. {
  2703. disable_irq(g_bat.irq);
  2704. mutex_lock(&bat_mutex);
  2705. battery_suspended = true;
  2706. mutex_unlock(&bat_mutex);
  2707. return 0;
  2708. }
  2709. static int battery_resume(struct platform_device *dev)
  2710. {
  2711. battery_suspended = false;
  2712. g_refresh_ui_soc = true;
  2713. if (bat_charger_is_pcm_timer_trigger())
  2714. wake_up_bat_update_meter();
  2715. enable_irq(g_bat.irq);
  2716. return 0;
  2717. }
  2718. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2719. /* // Battery Notify API */
  2720. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2721. #if !defined(CONFIG_POWER_EXT)
  2722. static ssize_t show_BatteryNotify(struct device *dev, struct device_attribute *attr, char *buf)
  2723. {
  2724. battery_log(BAT_LOG_CRTI, "[Battery] show_BatteryNotify : %x\n", g_BatteryNotifyCode);
  2725. return sprintf(buf, "%u\n", g_BatteryNotifyCode);
  2726. }
  2727. static ssize_t store_BatteryNotify(struct device *dev, struct device_attribute *attr,
  2728. const char *buf, size_t size)
  2729. {
  2730. unsigned int reg_BatteryNotifyCode, ret;
  2731. if (buf != NULL && size != 0) {
  2732. ret = kstrtouint(buf, 0, &reg_BatteryNotifyCode);
  2733. if (ret) {
  2734. pr_err("wrong format!\n");
  2735. return size;
  2736. }
  2737. g_BatteryNotifyCode = reg_BatteryNotifyCode;
  2738. battery_log(BAT_LOG_CRTI, "[Battery] store code : %x\n", g_BatteryNotifyCode);
  2739. }
  2740. return size;
  2741. }
  2742. static DEVICE_ATTR(BatteryNotify, 0664, show_BatteryNotify, store_BatteryNotify);
  2743. static ssize_t show_BN_TestMode(struct device *dev, struct device_attribute *attr, char *buf)
  2744. {
  2745. battery_log(BAT_LOG_CRTI, "[Battery] show_BN_TestMode : %x\n", g_BN_TestMode);
  2746. return sprintf(buf, "%u\n", g_BN_TestMode);
  2747. }
  2748. static ssize_t store_BN_TestMode(struct device *dev, struct device_attribute *attr, const char *buf,
  2749. size_t size)
  2750. {
  2751. unsigned int reg_BN_TestMode, ret;
  2752. if (buf != NULL && size != 0) {
  2753. ret = kstrtouint(buf, 0, &reg_BN_TestMode);
  2754. if (ret) {
  2755. pr_err("wrong format!\n");
  2756. return size;
  2757. }
  2758. g_BN_TestMode = reg_BN_TestMode;
  2759. battery_log(BAT_LOG_CRTI, "[Battery] store g_BN_TestMode : %x\n", g_BN_TestMode);
  2760. }
  2761. return size;
  2762. }
  2763. static DEVICE_ATTR(BN_TestMode, 0664, show_BN_TestMode, store_BN_TestMode);
  2764. #endif
  2765. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2766. /* // platform_driver API */
  2767. /* ///////////////////////////////////////////////////////////////////////////////////////// */
  2768. #if 0
  2769. static int battery_cmd_read(char *buf, char **start, off_t off, int count, int *eof, void *data)
  2770. {
  2771. int len = 0;
  2772. char *p = buf;
  2773. p += sprintf(p,
  2774. "g_battery_thermal_throttling_flag=%d,\nbattery_cmd_thermal_test_mode=%d,\nbattery_cmd_thermal_test_mode_value=%d\n",
  2775. g_battery_thermal_throttling_flag, battery_cmd_thermal_test_mode,
  2776. battery_cmd_thermal_test_mode_value);
  2777. *start = buf + off;
  2778. len = p - buf;
  2779. if (len > off)
  2780. len -= off;
  2781. else
  2782. len = 0;
  2783. return len < count ? len : count;
  2784. }
  2785. #endif
  2786. static ssize_t battery_cmd_write(struct file *file, const char *buffer, size_t count, loff_t *data)
  2787. {
  2788. int len = 0, bat_tt_enable = 0, bat_thr_test_mode = 0, bat_thr_test_value = 0;
  2789. char desc[32];
  2790. len = (count < (sizeof(desc) - 1)) ? count : (sizeof(desc) - 1);
  2791. if (copy_from_user(desc, buffer, len))
  2792. return 0;
  2793. desc[len] = '\0';
  2794. if (sscanf(desc, "%d %d %d", &bat_tt_enable, &bat_thr_test_mode, &bat_thr_test_value) == 3) {
  2795. g_battery_thermal_throttling_flag = bat_tt_enable;
  2796. battery_cmd_thermal_test_mode = bat_thr_test_mode;
  2797. battery_cmd_thermal_test_mode_value = bat_thr_test_value;
  2798. battery_log(BAT_LOG_CRTI,
  2799. "bat_tt_enable=%d, bat_thr_test_mode=%d, bat_thr_test_value=%d\n",
  2800. g_battery_thermal_throttling_flag,
  2801. battery_cmd_thermal_test_mode, battery_cmd_thermal_test_mode_value);
  2802. return count;
  2803. }
  2804. battery_log(BAT_LOG_CRTI,
  2805. " bad argument, echo [bat_tt_enable] [bat_thr_test_mode] [bat_thr_test_value] > battery_cmd\n");
  2806. return -EINVAL;
  2807. }
  2808. static int proc_utilization_show(struct seq_file *m, void *v)
  2809. {
  2810. seq_printf(m,
  2811. "=> g_battery_thermal_throttling_flag=%d,\nbattery_cmd_thermal_test_mode=%d,\nbattery_cmd_thermal_test_mode_value=%d\n",
  2812. g_battery_thermal_throttling_flag, battery_cmd_thermal_test_mode,
  2813. battery_cmd_thermal_test_mode_value);
  2814. return 0;
  2815. }
  2816. static int proc_utilization_open(struct inode *inode, struct file *file)
  2817. {
  2818. return single_open(file, proc_utilization_show, NULL);
  2819. }
  2820. static const struct file_operations battery_cmd_proc_fops = {
  2821. .open = proc_utilization_open,
  2822. .read = seq_read,
  2823. .write = battery_cmd_write,
  2824. };
  2825. static int mt_batteryNotify_probe(struct platform_device *pdev)
  2826. {
  2827. #if defined(CONFIG_POWER_EXT)
  2828. #else
  2829. struct device *dev = &pdev->dev;
  2830. int ret_device_file = 0;
  2831. /* struct proc_dir_entry *entry = NULL; */
  2832. struct proc_dir_entry *battery_dir = NULL;
  2833. battery_log(BAT_LOG_CRTI, "******** mt_batteryNotify_probe!! ********\n");
  2834. ret_device_file = device_create_file(dev, &dev_attr_BatteryNotify);
  2835. ret_device_file = device_create_file(dev, &dev_attr_BN_TestMode);
  2836. battery_dir = proc_mkdir("mtk_battery_cmd", NULL);
  2837. if (!battery_dir) {
  2838. pr_err("[%s]: mkdir /proc/mtk_battery_cmd failed\n", __func__);
  2839. } else {
  2840. #if 1
  2841. proc_create("battery_cmd", S_IRUGO | S_IWUSR, battery_dir, &battery_cmd_proc_fops);
  2842. battery_log(BAT_LOG_CRTI, "proc_create battery_cmd_proc_fops\n");
  2843. #else
  2844. entry = create_proc_entry("battery_cmd", S_IRUGO | S_IWUSR, battery_dir);
  2845. if (entry) {
  2846. entry->read_proc = battery_cmd_read;
  2847. entry->write_proc = battery_cmd_write;
  2848. }
  2849. #endif
  2850. }
  2851. battery_log(BAT_LOG_CRTI, "******** mtk_battery_cmd!! ********\n");
  2852. #endif
  2853. return 0;
  2854. }
  2855. #if 0 /* move to board-common-battery.c */
  2856. struct platform_device battery_device = {
  2857. .name = "battery",
  2858. .id = -1,
  2859. };
  2860. #endif
  2861. static struct platform_driver battery_driver = {
  2862. .probe = battery_probe,
  2863. .remove = battery_remove,
  2864. .shutdown = battery_shutdown,
  2865. /* #ifdef CONFIG_PM */
  2866. .suspend = battery_suspend,
  2867. .resume = battery_resume,
  2868. /* #endif */
  2869. .driver = {
  2870. .name = "battery",
  2871. #ifdef CONFIG_OF
  2872. .of_match_table = of_match_ptr(mt_battery_common_id),
  2873. #endif
  2874. },
  2875. };
  2876. struct platform_device MT_batteryNotify_device = {
  2877. .name = "mt-battery",
  2878. .id = -1,
  2879. };
  2880. static struct platform_driver mt_batteryNotify_driver = {
  2881. .probe = mt_batteryNotify_probe,
  2882. .driver = {
  2883. .name = "mt-battery",
  2884. },
  2885. };
  2886. static int __init battery_init(void)
  2887. {
  2888. int ret;
  2889. #if 0 /* move to board-common-battery.c */
  2890. ret = platform_device_register(&battery_device);
  2891. if (ret) {
  2892. battery_log(BAT_LOG_CRTI,
  2893. "****[battery_driver] Unable to device register(%d)\n", ret);
  2894. return ret;
  2895. }
  2896. #endif
  2897. ret = platform_driver_register(&battery_driver);
  2898. if (ret) {
  2899. battery_log(BAT_LOG_CRTI,
  2900. "****[battery_driver] Unable to register driver (%d)\n", ret);
  2901. return ret;
  2902. }
  2903. /* battery notofy UI */
  2904. ret = platform_device_register(&MT_batteryNotify_device);
  2905. if (ret) {
  2906. battery_log(BAT_LOG_CRTI,
  2907. "****[mt_batteryNotify] Unable to device register(%d)\n", ret);
  2908. return ret;
  2909. }
  2910. ret = platform_driver_register(&mt_batteryNotify_driver);
  2911. if (ret) {
  2912. battery_log(BAT_LOG_CRTI,
  2913. "****[mt_batteryNotify] Unable to register driver (%d)\n", ret);
  2914. return ret;
  2915. }
  2916. battery_log(BAT_LOG_CRTI, "****[battery_driver] Initialization : DONE !!\n");
  2917. return 0;
  2918. }
  2919. static void __exit battery_exit(void)
  2920. {
  2921. }
  2922. /* move to late_initcall to ensure battery_meter probe first */
  2923. /* module_init(battery_init); */
  2924. late_initcall(battery_init);
  2925. module_exit(battery_exit);
  2926. MODULE_AUTHOR("Oscar Liu");
  2927. MODULE_DESCRIPTION("Battery Device Driver");
  2928. MODULE_LICENSE("GPL");