smb1357_charger.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573
  1. /*
  2. * smb1357_charger.c
  3. * Samsung SMB1357 Charger Driver
  4. *
  5. * Copyright (C) 2014 Samsung Electronics
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #define DEBUG
  14. #include <linux/battery/sec_battery.h>
  15. #include <linux/battery/sec_charger.h>
  16. #include <linux/battery/charger/smb1357_charger.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/gpio.h>
  21. #include <linux/qpnp/pin.h>
  22. #include <linux/qpnp/qpnp-adc.h>
  23. #ifdef CONFIG_USB_HOST_NOTIFY
  24. #include <linux/host_notify.h>
  25. #endif
  26. #include <mach/sec_debug.h>
  27. #define SIOP_INPUT_LIMIT_CURRENT 1200
  28. #define SIOP_CHARGING_LIMIT_CURRENT 1000
  29. #define CHG_THERM_REDUCE_MAX_TEMP 650
  30. #define CHG_THERM_INPUT_LIMIT_CURRENT 1800
  31. #define CHG_THERM_CHARGING_LIMIT_CURRENT 2400
  32. #define SECOND_TERMINATION_CURRENT 4
  33. #if defined(CONFIG_ARCH_MSM8974PRO)
  34. #define CHG_THEM_THEM_CHANNEL LR_MUX8_PU1_AMUX_THM4
  35. #else
  36. #define CHG_THEM_THEM_CHANNEL LR_MUX8_PU2_AMUX_THM4
  37. #endif
  38. #if defined(CONFIG_MACH_KLTE_JPN) || defined(CONFIG_MACH_KACTIVELTE_DCM) || defined(CONFIG_MACH_CHAGALL_KDI)
  39. #define DT_NODE_NAME "charger"
  40. #define DEV_CHG_NAME "sec-charger"
  41. #define ENABLE_MAX77804K_CHG_IRQ
  42. #define SMB_OTG_NOTIFY_OVERCURRENT
  43. #define OVP_MSM_GPIO_CONTROL
  44. #endif
  45. #if defined(CONFIG_SEC_TRLTE_JPN) || defined(CONFIG_SEC_TBLTE_JPN)
  46. #define DT_NODE_NAME "smb1357charger"
  47. #define DEV_CHG_NAME "smb1357-charger"
  48. #define ENABLE_EXTERNAL_APSD
  49. #define POWER_SUPPLY_TYPE_POGODOCK POWER_SUPPLY_TYPE_MAX
  50. #endif
  51. #if defined(CONFIG_MACH_KACTIVELTE_DCM)
  52. #define ENABLE_SHUTDOWN_MODE
  53. #endif
  54. #if !defined(CONFIG_MACH_CHAGALL_KDI)
  55. #define ENABLE_CHG_THERM_LIMIT
  56. #endif
  57. #define REDUCE_TMM_CHG
  58. //#define ENABLE_CHG_THERM_LIMIT
  59. //#define ENABLE_DCIN_5VOR9V
  60. //#define ENABLE_SYSON_TRIGGER
  61. //#define ENABLE_SMBCHG_BATT_DET
  62. //#define ENABLE_SHUTDOWN_MODE
  63. //#define USE_SYSFS_CURRENT_CTRL
  64. //#define TIMER_FORCE_BLOCK
  65. //#define CHECK_VF_BY_IRQ
  66. //#define USE_DEBUG_WORK
  67. #define CS21_REVISION 0x25
  68. #if defined(REDUCE_TMM_CHG)
  69. #define TMM_SIOP_LVL 3
  70. #define TMM_CHG_CURRENT 300
  71. #endif
  72. #if defined(TIMER_FORCE_BLOCK)
  73. #define FORCE_BLOCK_MAX (10 * 60 / 30)
  74. #define FORCE_BLOCK_MIN (5 * 60 / 30)
  75. static int force_block;
  76. static int force_block_count;
  77. #endif
  78. extern int sec_otg_notify(int event);
  79. extern unsigned int sec_dbg_level;
  80. #define smb_dbg(dev, fmt, arg...) \
  81. do { \
  82. if (sec_dbg_level != KERNEL_SEC_DEBUG_LEVEL_LOW) \
  83. dev_info(dev, fmt, ##arg); \
  84. } while(0)
  85. #define smb_info(dev, fmt, arg...) \
  86. do { \
  87. dev_info(dev, fmt, ##arg); \
  88. } while(0)
  89. struct smb1357_charger_data{
  90. struct power_supply *psy_bat;
  91. struct power_supply psy_pogo;
  92. struct sec_charger_info *charger;
  93. struct delayed_work init_work;
  94. struct delayed_work debug_work;
  95. struct delayed_work hvdcp_det_work;
  96. struct delayed_work pogo_det_work;
  97. struct delayed_work syson_trigger_work;
  98. struct delayed_work detbat_work;
  99. struct regulator *chg_vf_1p8v;
  100. struct wake_lock chg_wake_lock;
  101. struct wake_lock chgisr_wake_lock;
  102. struct mutex mutex;
  103. u8 revision;
  104. u8 hvdcp_det_count;
  105. u8 pogo_det_count;
  106. u8 pogo_status;
  107. u8 syson_trigger_onoff;
  108. int siop_level;
  109. int pogo_det_gpio;
  110. int shdn_gpio;
  111. int chg_limit;
  112. int chg_high_temp;
  113. int chg_high_temp_recovery;
  114. int ovp_gpio_en;
  115. int chg_vf_det;
  116. int detbat_irq;
  117. int usbin_cable_type;
  118. u8 pogo_2nd_charging;
  119. u8 sysfs_mode;
  120. u32 sysfs_curin;
  121. u32 sysfs_curout;
  122. };
  123. #if defined(ENABLE_MAX77804K_CHG_IRQ)
  124. static int max77804k_tiny_irq_batp;
  125. struct max77804k_tiny_charger_data{
  126. struct max77804k_dev *max77804k;
  127. struct delayed_work tiny_init_work;
  128. };
  129. #endif
  130. static struct qpnp_pin_cfg smb1357_pm_mpp_digin_config = {
  131. .mode = 0, /* QPNP_PIN_MODE_DIG_IN */
  132. .output_type = 0, /* QPNP_PIN_OUT_BUF_CMOS */
  133. .invert = 0, /* QPNP_PIN_INVERT_DISABLE */
  134. .pull = 1, /* PM8XXX_MPP_BI_PULLUP_OPEN */
  135. .vin_sel = 2,
  136. .out_strength = 3, /* QPNP_PIN_OUT_STRENGTH_HIGH */
  137. .src_sel = 0, /* QPNP_PIN_SEL_FUNC_CONSTANT */
  138. .master_en = 1, /* QPNP_PIN_MASTER_ENABLE */
  139. };
  140. static struct qpnp_pin_cfg smb1357_pm_mpp_ain_config = {
  141. .mode = 4, /* QPNP_PIN_MODE_AIN */
  142. .output_type = 0, /* QPNP_PIN_OUT_BUF_CMOS */
  143. .invert = 0, /* QPNP_PIN_INVERT_DISABLE */
  144. .pull = 1, /* PM8XXX_MPP_BI_PULLUP_OPEN */
  145. .vin_sel = 2,
  146. .ain_route = 3, /* QPNP_PIN_AIN_AMUX_CH8 */
  147. .out_strength = 3, /* QPNP_PIN_OUT_STRENGTH_HIGH */
  148. .src_sel = 0, /* QPNP_PIN_SEL_FUNC_CONSTANT */
  149. .master_en = 1, /* QPNP_PIN_MASTER_ENABLE */
  150. };
  151. static enum power_supply_property pogo_power_props[] = {
  152. POWER_SUPPLY_PROP_ONLINE,
  153. };
  154. static char *pogo_supply_list[] = {
  155. "battery",
  156. };
  157. static int chg_fast_cur_table[] = {
  158. 300, // 0x00
  159. 400,
  160. 450,
  161. 475,
  162. 500,
  163. 550,
  164. 600,
  165. 650,
  166. 700, // 0x08
  167. 900,
  168. 950,
  169. 1000,
  170. 1100,
  171. 1200,
  172. 1400,
  173. 1450,
  174. 1500, // 0x10
  175. 1600,
  176. 1800,
  177. 1850,
  178. 1880,
  179. 1910,
  180. 1930,
  181. 1950,
  182. 1970, // 0x18
  183. 2000,
  184. 2050,
  185. 2100,
  186. 2300,
  187. 2400,
  188. 2500,
  189. 3000 // 0x1F
  190. };
  191. static int chg_pre_cur_table[] = {
  192. 100,
  193. 150,
  194. 200,
  195. 250
  196. };
  197. static u32 chg_temp_table_size;
  198. const static sec_bat_adc_table_data_t chg_temp_table[] = {
  199. {25844, 900},
  200. {26113, 850},
  201. {26595, 800},
  202. {26896, 750},
  203. {27238, 700},
  204. {27679, 650},
  205. {28193, 600},
  206. {28768, 550},
  207. {29412, 500},
  208. {30169, 450},
  209. {31008, 400},
  210. {31917, 350},
  211. {32901, 300},
  212. {33955, 250},
  213. {35039, 200},
  214. {36132, 150},
  215. {37183, 100},
  216. {38158, 50},
  217. {39041, 0},
  218. {39856, -50},
  219. {40561, -100},
  220. {41146, -150},
  221. {41622, -200},
  222. };
  223. enum {
  224. CUR_MODE = 6,
  225. CUR_SET,
  226. USE_SYSFS_CUR = 0xff,
  227. };
  228. static void ovp_gpio_enable(int gpio, int value)
  229. {
  230. #if defined(OVP_MSM_GPIO_CONTROL)
  231. // msmgpio.
  232. gpio_set_value(gpio, value);
  233. #else
  234. // gpio expander.
  235. gpio_direction_output(gpio, value);
  236. #endif
  237. return;
  238. }
  239. static int smb1357_charger_i2c_write(struct i2c_client *client,
  240. int reg, u8 *buf)
  241. {
  242. int ret;
  243. ret = i2c_smbus_write_byte_data(client, reg, *buf);
  244. if (ret < 0)
  245. dev_err(&client->dev, "%s: Error(%d)buf %d\n", __func__, ret,*buf);
  246. return ret;
  247. }
  248. static int smb1357_charger_i2c_read(struct i2c_client *client,
  249. int reg, u8 *buf)
  250. {
  251. int ret;
  252. ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf);
  253. if (ret < 0)
  254. dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
  255. return ret;
  256. }
  257. static int smb1357_charger_masked_write_reg(struct i2c_client *client,
  258. u8 reg, u8 mask, u8 val)
  259. {
  260. int ret;
  261. u8 buf;
  262. ret = smb1357_charger_i2c_read(client, reg, &buf);
  263. if (ret < 0) {
  264. dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
  265. goto error;
  266. }
  267. buf &= ~mask;
  268. buf |= val & mask;
  269. ret = smb1357_charger_i2c_write(client, reg, &buf);
  270. if (ret < 0) {
  271. dev_err(&client->dev, "%s: error - write(%d)\n", __func__, ret);
  272. goto error;
  273. }
  274. error:
  275. return ret;
  276. }
  277. static void smb1357_read_regs(struct i2c_client *client, char *str)
  278. {
  279. u8 data = 0;
  280. u32 addr = 0;
  281. for (addr = 0; addr <= 0x1f; addr++) {
  282. smb1357_charger_i2c_read(client, addr, &data);
  283. sprintf(str+strlen(str), "0x%x, ", data);
  284. }
  285. /* "#" considered as new line in application */
  286. sprintf(str+strlen(str), "#");
  287. for (addr = 0x40; addr <= 0x57; addr++) {
  288. smb1357_charger_i2c_read(client, addr, &data);
  289. sprintf(str+strlen(str), "0x%x, ", data);
  290. }
  291. }
  292. static u8 smb1357_check_product_id(struct i2c_client * client)
  293. {
  294. struct smb1357_charger_data *smb1357data =
  295. dev_get_drvdata(&client->dev);
  296. u8 id = 0x00;
  297. int ret;
  298. smb1357_charger_i2c_read(client, STATUS_9_REG, &id);
  299. smb1357data->revision = id;
  300. dev_dbg(&client->dev,"%s : Revision (0x%2x)\n", __func__, id);
  301. ret = smb1357_charger_i2c_read(client, SMB1357_PRODUCT_ID, &id);
  302. if (ret < 0) {
  303. dev_err(&client->dev,"%s : ID Read FAILED..[%d]\n",
  304. __func__, ret);
  305. id = 0xFF;
  306. }
  307. else {
  308. dev_info(&client->dev,"%s : charger ID %s\n",
  309. __func__, id ? "SMB1356" : "SMB1357");
  310. }
  311. return id;
  312. }
  313. static void smb1357_write_enable(struct i2c_client * client, u8 enable)
  314. {
  315. u8 buf;
  316. if (enable == true) buf = ALLOW_VOLATILE_BIT;
  317. else buf = 0x00;
  318. smb1357_charger_masked_write_reg(client, CMD_I2C_REG,
  319. ALLOW_VOLATILE_BIT, buf);
  320. dev_dbg(&client->dev,"%s : %s\n", __func__, enable ? "EN":"DISABLE");
  321. return;
  322. }
  323. static void smb1357_set_APSD(struct i2c_client *client, u8 enable)
  324. {
  325. msleep(20);
  326. smb1357_charger_masked_write_reg(client, CFG_11_REG,
  327. AUTO_DET_SRC_EN_BIT, AUTO_DET_SRC_EN_BIT);
  328. if (enable) {
  329. smb1357_charger_masked_write_reg(client, CMD_INPUT_LIMIT,
  330. USE_REGISTER_FOR_CURRENT, 0x00);
  331. }
  332. else {
  333. smb1357_charger_masked_write_reg(client, CMD_INPUT_LIMIT,
  334. USE_REGISTER_FOR_CURRENT, USE_REGISTER_FOR_CURRENT);
  335. }
  336. dev_dbg(&client->dev,"%s : %s\n",
  337. __func__, enable ? "EN":"DISABLE");
  338. return;
  339. }
  340. static int smb1357_check_vf_status(struct i2c_client *client)
  341. {
  342. struct smb1357_charger_data *smb1357data =
  343. dev_get_drvdata(&client->dev);
  344. int vf_removed = 0;
  345. int ret;
  346. if (smb1357data->chg_vf_det){
  347. if (regulator_enable(smb1357data->chg_vf_1p8v)) {
  348. msleep(10);
  349. qpnp_pin_config(smb1357data->chg_vf_det,
  350. &smb1357_pm_mpp_digin_config);
  351. /* VF_DEF is HIGH - batt removed. */
  352. vf_removed = gpio_get_value(smb1357data->chg_vf_det);
  353. qpnp_pin_config(smb1357data->chg_vf_det, &smb1357_pm_mpp_ain_config);
  354. regulator_disable(smb1357data->chg_vf_1p8v);
  355. msleep(10);
  356. }
  357. }
  358. if (vf_removed == 1) ret = false;
  359. else ret = true;
  360. dev_info(&client->dev, "%s batt status %s\n",
  361. __func__, ret ? "OK":"Removed");
  362. return ret;
  363. }
  364. static int smb1357_cvt_adc_to_temp(struct i2c_client *client, int adc)
  365. {
  366. int temp = 0;
  367. int high = 0;
  368. int mid = 0;
  369. int low = 0;
  370. /* check min,max temp */
  371. if (chg_temp_table[0].adc >= adc){
  372. temp = chg_temp_table[0].data;
  373. goto out;
  374. }
  375. else if (chg_temp_table[chg_temp_table_size-1].adc <= adc){
  376. temp = chg_temp_table[chg_temp_table_size-1].data;
  377. goto out;
  378. }
  379. high = chg_temp_table_size - 1;
  380. while (low <= high) {
  381. mid = (low + high) / 2;
  382. if (chg_temp_table[mid].adc > adc)
  383. high = mid - 1;
  384. else if (chg_temp_table[mid].adc < adc)
  385. low = mid + 1;
  386. else {
  387. temp = chg_temp_table[mid].data;
  388. goto out;
  389. }
  390. }
  391. temp = chg_temp_table[high].data;
  392. temp += ((chg_temp_table[low].data - chg_temp_table[high].data) *
  393. (adc - chg_temp_table[high].adc)) /
  394. (chg_temp_table[low].adc - chg_temp_table[high].adc);
  395. out:
  396. dev_info(&client->dev,"%s adc [%d] temp [ %3d]\n",
  397. __func__, adc, temp);
  398. return temp;
  399. }
  400. static int smb1357_get_therm_temp(struct i2c_client *client)
  401. {
  402. struct qpnp_vadc_result results;
  403. int temp = 0;
  404. int err = 0;
  405. err = qpnp_vadc_read(NULL, CHG_THEM_THEM_CHANNEL, &results);
  406. if (err < 0) {
  407. dev_err(&client->dev,"%s : therm read fail rc = %d\n",
  408. __func__, err);
  409. temp = 300;
  410. }
  411. else {
  412. temp = smb1357_cvt_adc_to_temp(client, results.adc_code);
  413. }
  414. return temp;
  415. }
  416. static void smb1357_set_STAT_conf(struct i2c_client *client)
  417. {
  418. u8 buf;
  419. smb1357_charger_masked_write_reg(client, CFG_17_REG,
  420. CHG_STAT_DISABLE_BIT | \
  421. CHG_STAT_ACTIVE_HIGH_BIT | \
  422. CHG_STAT_SRC_BIT | \
  423. CHG_STAT_IRQ_ONLY_BIT, 0x00);
  424. smb1357_charger_i2c_read(client, CFG_17_REG, &buf);
  425. dev_dbg(&client->dev,"%s : default [0x%x]\n",
  426. __func__, buf);
  427. return;
  428. }
  429. static void smb1357_set_VSYS(struct i2c_client *client)
  430. {
  431. /* min vsys voltage : 3600 (0x20) */
  432. smb1357_charger_masked_write_reg(client,CFG_4_REG,
  433. CHG_MIN_VSYS_MASK, 0x20);
  434. dev_dbg(&client->dev,"%s\n",__func__);
  435. return;
  436. }
  437. static void smb1357_lowbatt_threshold(struct i2c_client *client)
  438. {
  439. /* low batt threshold : 3250 (0x0c) */
  440. smb1357_charger_masked_write_reg(client, CFG_13_REG,
  441. LOW_BATT_THRESHOLD_MASK, 0x0C);
  442. dev_dbg(&client->dev,"%s\n",__func__);
  443. return;
  444. }
  445. static void smb1357_usb_current_limit(struct i2c_client *client,
  446. u8 usb_type, u8 usb_mode)
  447. {
  448. /* otg batt under voltage : 3100 */
  449. smb1357_charger_masked_write_reg(client, CFG_4_REG,
  450. OTG_BATT_UV_MASK , 0x08);
  451. /* USB TYPE : 0 - USB2.0 : 1 - USB3.0 */
  452. smb1357_charger_masked_write_reg(client, CFG_5_REG,
  453. USB_2_3_BIT, usb_type);
  454. /* USB Mode : 0 - USB500 : 1 - USBAC : 2 - USB100 */
  455. smb1357_charger_masked_write_reg(client, CMD_INPUT_LIMIT,
  456. USB_100_500_AC_MASK, usb_mode);
  457. dev_info(&client->dev,"%s type : %d mode %d\n",
  458. __func__, usb_type, usb_mode);
  459. return;
  460. }
  461. static void smb1357_temp_compensation(struct i2c_client *client)
  462. {
  463. /* hot/cold soft vfloat comp disable. */
  464. smb1357_charger_masked_write_reg(client, CFG_1A_REG,
  465. HOT_SOFT_VFLOAT_COMP_EN_BIT | COLD_SOFT_VFLOAT_COMP_EN_BIT,
  466. 0x00);
  467. dev_dbg(&client->dev,"%s\n",__func__);
  468. return;
  469. }
  470. static void smb1357_dcin_adapter(struct i2c_client *client, u8 data)
  471. {
  472. #ifdef CONFIG_SEC_FACTORY
  473. data = DCIN_5VOR9V;
  474. #endif
  475. /* DCIN ADAPTER : 5V or(to) 9V */
  476. smb1357_charger_masked_write_reg(client, CFG_A_REG,
  477. DCIN_ADAPTER_MASK, data);
  478. msleep(50);
  479. dev_dbg(&client->dev,"%s, data = 0x%x\n",__func__, data);
  480. return;
  481. }
  482. static void smb1357_syson_trigger_work(struct work_struct *work)
  483. {
  484. struct smb1357_charger_data *smb1357data =
  485. container_of(work, struct smb1357_charger_data,syson_trigger_work.work);
  486. struct sec_charger_info *charger = smb1357data->charger;
  487. struct i2c_client *client = charger->client;
  488. cancel_delayed_work(&smb1357data->syson_trigger_work);
  489. #ifdef ENABLE_SYSON_TRIGGER
  490. // TODO. En/Disable VBUS sensing.
  491. if (true) {
  492. extern void dwc3_msm_config_vbus_sensing(bool enable);
  493. dwc3_msm_config_vbus_sensing(smb1357data->syson_trigger_onoff);
  494. }
  495. #endif
  496. dev_dbg(&client->dev,"%s, on = [%d]\n",
  497. __func__,smb1357data->syson_trigger_onoff);
  498. return;
  499. }
  500. static void smb1357_syson_sensing(struct i2c_client *client,
  501. u32 delay, bool onoff)
  502. {
  503. struct smb1357_charger_data *smb1357data =
  504. i2c_get_clientdata(client);
  505. smb1357data->syson_trigger_onoff = onoff;
  506. #ifdef ENABLE_SYSON_TRIGGER
  507. cancel_delayed_work(&smb1357data->syson_trigger_work);
  508. schedule_delayed_work(&smb1357data->syson_trigger_work,
  509. msecs_to_jiffies(delay));
  510. #endif
  511. dev_dbg(&client->dev,"%s, delay[%d] on = [%d]\n",
  512. __func__, delay, onoff);
  513. return;
  514. }
  515. static void smb1357_dcin_config_hv_lv(struct i2c_client *client, u8 mode)
  516. {
  517. u8 data_e = 0xff;
  518. #ifdef ENABLE_DCIN_5VOR9V
  519. if (mode) {
  520. smb1357_dcin_adapter(client, DCIN_5VOR9V);
  521. msleep(10);
  522. }
  523. smb1357_charger_i2c_read(client, IRQ_E_REG, &data_e);
  524. if (data_e & IRQ_E_DC_OV_BIT) {
  525. /* dcin 9v only */
  526. smb1357_dcin_adapter(client, DCIN_9VONLY);
  527. smb1357_syson_sensing(client, 100, false);
  528. }
  529. else if (data_e & IRQ_E_DC_UV_BIT) {
  530. /* dcin 5v or 9v */
  531. smb1357_dcin_adapter(client, DCIN_5VOR9V);
  532. }
  533. #else
  534. smb1357_dcin_adapter(client, DCIN_5VONLY);
  535. #endif
  536. msleep(10);
  537. dev_info(&client->dev,"%s, dcin mode %d, OV_UV 0x%x\n",
  538. __func__, mode, data_e);
  539. return;
  540. }
  541. static u8 smb1357_dcin_input_voltage(struct i2c_client *client)
  542. {
  543. u8 status_8;
  544. u8 ret;
  545. smb1357_charger_i2c_read(client, STATUS_8_REG, &status_8);
  546. if (status_8 & DCIN_HV)
  547. ret = DCIN_9V;
  548. else if (status_8 & DCIN_LV)
  549. ret = DCIN_5V;
  550. else
  551. ret = DCIN_NONE;
  552. return ret;
  553. }
  554. static void smb1357_clear_irq_status(struct i2c_client *client)
  555. {
  556. u8 buf;
  557. u8 reg = IRQ_A_REG;
  558. do{
  559. smb1357_charger_i2c_read(client, reg, &buf);
  560. }while(++reg <= IRQ_G_REG);
  561. dev_dbg(&client->dev,"%s\n",__func__);
  562. return;
  563. }
  564. static void smb1357_enable_irq(struct i2c_client *client,
  565. u8 reg, u8 mask, u8 buf)
  566. {
  567. if(reg < IRQ_CFG_REG && reg > IRQ3_CFG_REG)
  568. {
  569. dev_dbg(&client->dev,"%s, reg error 0x%2x\n",__func__,reg);
  570. goto out;
  571. }
  572. smb1357_charger_masked_write_reg(client, reg, mask, buf);
  573. dev_dbg(&client->dev,"%s, reg 0x%2x\n",__func__,reg);
  574. out:
  575. return;
  576. }
  577. static void smb1357_enable_charging(struct i2c_client *client, u8 enable)
  578. {
  579. u8 buf;
  580. if(enable == true) buf = CMD_CHG_EN_VAL;
  581. else buf = CMD_CHG_DIS_VAL;
  582. smb1357_charger_masked_write_reg(client, CMD_CHG_REG,
  583. CMD_CHG_EN_MASK, buf);
  584. dev_info(&client->dev,"%s, enable = %d\n",__func__, enable);
  585. return;
  586. }
  587. static void smb1357_enable_otg(struct i2c_client *client, u8 enable)
  588. {
  589. smb1357_charger_masked_write_reg(client, CMD_CHG_REG,
  590. OTG_EN, enable);
  591. dev_info(&client->dev,"%s, enable = %d\n",__func__, enable);
  592. return;
  593. }
  594. static void smb1357_chgin_shutdown(struct i2c_client *client, u8 enable)
  595. {
  596. if (enable == true){
  597. smb1357_charger_masked_write_reg(client, CMD_INPUT_LIMIT,
  598. USB_SHUTDOWN_BIT|DC_SHUTDOWN_BIT,
  599. USB_SHUTDOWN_BIT|DC_SHUTDOWN_BIT);
  600. }
  601. else {
  602. smb1357_charger_masked_write_reg(client, CMD_INPUT_LIMIT,
  603. USB_SHUTDOWN_BIT|DC_SHUTDOWN_BIT,
  604. 0x00);
  605. }
  606. dev_dbg(&client->dev,"%s, enable = %d\n",__func__, enable);
  607. return;
  608. }
  609. static void smb1357_enable_batt_irq(struct i2c_client *client, u8 enable)
  610. {
  611. if (enable == true){
  612. /* enable battery missing algoritm */
  613. smb1357_charger_masked_write_reg(client, CFG_19_REG,
  614. BATT_MISSING_ALGO_BIT|BATT_MISSING_BMD_BIT | \
  615. BATT_MISSING_PLUG_BIT|BATT_MISSING_POLLING_BIT,
  616. BATT_MISSING_ALGO_BIT|BATT_MISSING_BMD_BIT | \
  617. BATT_MISSING_PLUG_BIT|BATT_MISSING_POLLING_BIT);
  618. }
  619. else
  620. {
  621. /* disable battery missing algoritm */
  622. smb1357_charger_masked_write_reg(client, CFG_19_REG,
  623. BATT_MISSING_ALGO_BIT|BATT_MISSING_BMD_BIT | \
  624. BATT_MISSING_PLUG_BIT|BATT_MISSING_POLLING_BIT,
  625. 0x00);
  626. }
  627. /* set battery irq */
  628. smb1357_charger_masked_write_reg(client, IRQ2_CFG_REG,
  629. (IRQ2_BATT_MISSING_BIT|IRQ2_VBAT_LOW_BIT),
  630. (enable << 1 | enable));
  631. dev_dbg(&client->dev,"%s, enable = %d\n",__func__, enable);
  632. return;
  633. }
  634. #if defined(ENABLE_SMBCHG_BATT_DET)
  635. static u8 smb1357_get_batt_status(struct i2c_client *client)
  636. {
  637. u8 data;
  638. smb1357_charger_i2c_read(client, IRQ_B_REG, &data);
  639. dev_dbg(&client->dev,
  640. "%s: irq B [ 0x%x ]\n", __func__, data);
  641. /* false : battery missing, true : battery using */
  642. if ((data & IRQ_B_BATT_TERMINAL_BIT)||
  643. (data & IRQ_B_BATT_MISSING_BIT)||
  644. (data & IRQ_B_VBAT_LOW_BIT)) {
  645. data = false;
  646. dev_dbg(&client->dev,"%s, batt = %s\n",
  647. __func__, data ? "using":"missing");
  648. }
  649. else
  650. data = true;
  651. return data;
  652. }
  653. #endif
  654. static u8 smb1357_get_charger_type(struct i2c_client *client)
  655. {
  656. u8 hvdcp_type;
  657. u8 chg_type;
  658. u8 port;
  659. smb1357_charger_i2c_read(client, STATUS_5_REG, &chg_type);
  660. dev_dbg(&client->dev,"%s, chgtype = 0x%x\n", __func__, chg_type);
  661. if (chg_type & CDP_BIT) {
  662. port = CDP;
  663. }
  664. else if (chg_type & DCP_BIT) {
  665. smb1357_charger_i2c_read(client, STATUS_7_REG, &hvdcp_type);
  666. if (hvdcp_type & HVDCP_SEL_12V_BIT)
  667. port = HVDCP_12V;
  668. else if (hvdcp_type & HVDCP_SEL_9V_BIT)
  669. port = HVDCP_9V;
  670. else if (hvdcp_type & HVDCP_SEL_5V_BIT)
  671. port = HVDCP_5V;
  672. else
  673. port = DCP;
  674. dev_dbg(&client->dev,"%s, hvdcp = %d\n", __func__, hvdcp_type);
  675. }
  676. else if (chg_type & OTHER_BIT) {
  677. port = OCP;
  678. }
  679. else if (chg_type & SDP_BIT) {
  680. port = SDP;
  681. }
  682. else
  683. port = SDP;
  684. dev_info(&client->dev,"%s, port = %d\n", __func__, port);
  685. return port;
  686. }
  687. static u8 smb1357_get_float_voltage_data(
  688. int float_voltage)
  689. {
  690. u8 data;
  691. if (float_voltage < 3500)
  692. data = 0;
  693. else if(float_voltage <= 4340)
  694. data = (float_voltage - 3500) / 20;
  695. else if(float_voltage == 4350)
  696. data = 0x2B; /* (4340 -3500)/20 + 1 */
  697. else if(float_voltage <= 4500)
  698. data = (float_voltage - 3500) / 20 + 1;
  699. else
  700. data = 0x2B;
  701. return data;
  702. }
  703. static void smb1357_set_vfloat(struct i2c_client *client,int f_vol)
  704. {
  705. u8 data;
  706. data = smb1357_get_float_voltage_data(f_vol);
  707. smb1357_charger_masked_write_reg(client, VFLOAT_REG,
  708. FLOAT_MASK, data);
  709. return;
  710. }
  711. static u8 smb1357_get_current_data(struct i2c_client *client, int cur)
  712. {
  713. struct smb1357_charger_data *smb1357data =
  714. dev_get_drvdata(&client->dev);
  715. u8 data = 0;
  716. int max_reg = (int)(sizeof(chg_fast_cur_table)/sizeof(int));
  717. do{
  718. if (cur <= chg_fast_cur_table[data]){
  719. break;
  720. }
  721. }while(++data <= max_reg);
  722. if (smb1357data->revision >= CS21_REVISION) {
  723. /* check register value */
  724. if ((data == 0x0F) || (data == 0x16))
  725. data -= 0x01;
  726. /* fixed current on cs21 sample */
  727. if (cur == 2700) data = 0x0F;
  728. else if (cur == 2800) data = 0x16;
  729. }
  730. if(data >= max_reg) data = 0x00;
  731. dev_dbg(&client->dev,"%s : current [%d]: reg [0x%x]!\n",
  732. __func__, cur, data);
  733. return data;
  734. }
  735. static u8 smb1357_get_term_current_limit_data(
  736. int termination_current)
  737. {
  738. u8 data;
  739. if (termination_current <= 50)
  740. data = 0x01;
  741. else if (termination_current <= 100)
  742. data = 0x02;
  743. else if (termination_current <= 150)
  744. data = 0x03;
  745. else if (termination_current <= 200)
  746. data = 0x04;
  747. else if (termination_current <= 250)
  748. data = 0x05;
  749. else if (termination_current <= 300)
  750. data = 0x00;
  751. else if (termination_current <= 500)
  752. data = 0x06;
  753. else if (termination_current <= 600)
  754. data = 0x07;
  755. else
  756. data = 0x07; /* set input current limit as maximum */
  757. data = data << 3;
  758. return data;
  759. }
  760. static void smb1357_set_cur_termination(struct i2c_client *client, u8 enable)
  761. {
  762. u8 data;
  763. if(enable == true) data = 0x00;
  764. else data = 0x08;
  765. smb1357_charger_masked_write_reg(client, CFG_14_REG,
  766. DISABLE_CURRENT_TERM_BIT, data);
  767. dev_dbg(&client->dev,"%s : %s\n",
  768. __func__, enable ? "EN":"DISABLE");
  769. return;
  770. }
  771. static void smb1357_set_auto_recharge(struct i2c_client *client, u8 enable)
  772. {
  773. u8 data;
  774. if(enable == true) data = 0x00;
  775. else data = 0x04;
  776. /* recharge voltage threshold : VFLT - 100mv */
  777. smb1357_charger_masked_write_reg(client, CFG_5_REG,
  778. RECHARGE_200MV_BIT, 0x00);
  779. /* automatic recharge */
  780. smb1357_charger_masked_write_reg(client, CFG_14_REG,
  781. DISABLE_AUTO_RECHARGE_BIT, data);
  782. dev_dbg(&client->dev,"%s : %s\n",
  783. __func__, enable ? "EN":"DISABLE");
  784. return;
  785. }
  786. static void smb1357_set_charging_current(
  787. struct i2c_client *client, int charging_current)
  788. {
  789. u8 data;
  790. data = smb1357_get_current_data(client, charging_current);
  791. smb1357_charger_masked_write_reg(client, CFG_1C_REG,
  792. FAST_CHARGE_CURRENT_MASK, data);
  793. dev_info(&client->dev,"%s : chg_cur = [%4d]data :(0x%02x)\n",
  794. __func__, charging_current, data);
  795. return;
  796. }
  797. static void smb1357_set_input_current_limit(
  798. struct i2c_client *client, int input_current_limit)
  799. {
  800. u8 data;
  801. /* disable AICL */
  802. smb1357_charger_masked_write_reg(client, CFG_D_REG,
  803. USBIN_AICL_BIT, 0x00);
  804. smb1357_charger_masked_write_reg(client, USBIN_DCIN_CFG_REG,
  805. AICL_5V_THRESHOLD_BIT, AICL_5V_THRESHOLD_BIT);
  806. /* Input current limit */
  807. data = smb1357_get_current_data(client, input_current_limit);
  808. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  809. USBIN_INPUT_MASK, data);
  810. smb1357_charger_masked_write_reg(client, CFG_A_REG,
  811. DCIN_INPUT_MASK, data);
  812. msleep(50);
  813. /* enable AICL */
  814. smb1357_charger_masked_write_reg(client, CFG_D_REG,
  815. USBIN_AICL_BIT, USBIN_AICL_BIT);
  816. dev_dbg(&client->dev,
  817. "%s : data : (0x%02x)\n", __func__, data);
  818. return;
  819. }
  820. static int smb1357_status_charging_current( struct i2c_client *client)
  821. {
  822. struct smb1357_charger_data *smb1357data =
  823. dev_get_drvdata(&client->dev);
  824. u8 data = 0;
  825. u8 status = 0;
  826. u32 cur = 0;
  827. smb1357_charger_i2c_read(client,STATUS_3_REG, &data);
  828. smb1357_charger_i2c_read(client,STATUS_4_REG, &status);
  829. if (status & BATT_DONE_STATUS)
  830. status = BATT_FAST_CHG_VAL;
  831. else
  832. status = (status & CHG_TYPE_MASK) >> 1;
  833. switch(status){
  834. case BATT_PRE_CHG_VAL:
  835. data &= (data&PRE_CHG_CUR_COMP_MASK) >> 5;
  836. cur = chg_pre_cur_table[data];
  837. break;
  838. case BATT_FAST_CHG_VAL:
  839. case BATT_TAPER_CHG_VAL:
  840. data &= FAST_CHG_CUR_COMP_MASK;
  841. cur = chg_fast_cur_table[data];
  842. if (smb1357data->revision >= CS21_REVISION) {
  843. if (data == 0x0F) cur = 2700;
  844. else if (data == 0x16) cur = 2800;
  845. }
  846. break;
  847. case BATT_NOT_CHG_VAL:
  848. default:
  849. cur = 0;
  850. break;
  851. }
  852. dev_info(&client->dev,
  853. "%s : data : (0x%02x), cur = [%d] status = [0x%x]\n",
  854. __func__, data, cur, status);
  855. return cur;
  856. }
  857. static void smb1357_irq_cfg(struct i2c_client * client)
  858. {
  859. smb1357_enable_irq(client, IRQ_CFG_REG, 0x0F,
  860. IRQ_USBIN_UV_BIT);
  861. smb1357_enable_irq(client, IRQ2_CFG_REG, 0xFF,
  862. IRQ2_POWER_OK_BIT);
  863. smb1357_enable_irq(client, IRQ3_CFG_REG, 0xFF,
  864. IRQ3_DCIN_OV_BIT|IRQ3_DCIN_UV_BIT);
  865. }
  866. static void smb1357_hw_init(struct i2c_client * client)
  867. {
  868. /* move to bootlaoder for lowbatt booting */
  869. /* APSD disable */
  870. /* smb1357_set_APSD(client, false); */
  871. /* USBIN ADAPTER : 9V only */
  872. /* smb1357_charger_masked_write_reg(client, CFG_C_REG, */
  873. /* USBIN_ADAPTER_MASK, 0x60); */
  874. /* DCIN ADAPTER : 5V to(or) 9V */
  875. smb1357_dcin_config_hv_lv(client, true);
  876. /* DCIN Disable AICL */
  877. smb1357_charger_masked_write_reg(client, CFG_B_REG,
  878. DCIN_AICL_BIT, 0x00);
  879. /* USBIN Input Current : 1800mA */
  880. smb1357_set_input_current_limit(client, 1800);
  881. /* PRE CHG CUR : 250mA */
  882. smb1357_charger_masked_write_reg(client, CFG_1C_REG,
  883. PRE_CHG_CUR_MASK, 0x60);
  884. /* Fast CHG CUR : 2500mA */
  885. smb1357_set_charging_current(client, 2500);
  886. /* irq active low */
  887. smb1357_set_STAT_conf(client);
  888. /* Config Default irq */
  889. smb1357_irq_cfg(client);
  890. /* Clear Irq */
  891. smb1357_clear_irq_status(client);
  892. /* Enable batt irq */
  893. smb1357_enable_batt_irq(client, false);
  894. dev_info(&client->dev,"%s\n",__func__);
  895. return;
  896. }
  897. static void smb1357_set_default_data(struct i2c_client *client)
  898. {
  899. u8 data;
  900. u8 data_f;
  901. /* Set the registers to the default configuration */
  902. smb1357_charger_i2c_read(client, IRQ_F_REG, &data_f);
  903. if ((data_f & IRQ_F_POWER_OK_BIT) == false) {
  904. /* Sometimes It didn't detcted TA_HV */
  905. /* USBIN ADAPTER : 9V */
  906. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  907. USBIN_ADAPTER_MASK, 0x60);
  908. msleep(20);
  909. /* USBIN ADAPTER : 5v or 9v */
  910. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  911. USBIN_ADAPTER_MASK, 0x20);
  912. /* APSD */
  913. smb1357_set_APSD(client, false);
  914. }
  915. /* Disable Automatic Recharge */
  916. smb1357_set_auto_recharge(client, false);
  917. /* usb mode */
  918. smb1357_usb_current_limit(client, USB3_VAL, USB_500_VAL);
  919. /* AICL */
  920. data = 0x04;
  921. smb1357_charger_i2c_write(client, CFG_D_REG, &data);
  922. /* DCIN Disable AICL */
  923. smb1357_charger_masked_write_reg(client, CFG_B_REG,
  924. DCIN_AICL_BIT, 0x00);
  925. /* DCIN 5V to(or) 9V */
  926. smb1357_dcin_config_hv_lv(client, true);
  927. /* config irq */
  928. smb1357_irq_cfg(client);
  929. /* Clear Irq */
  930. smb1357_clear_irq_status(client);
  931. dev_dbg(&client->dev,"%s, PA 0x%x\n", __func__, data_f);
  932. return;
  933. }
  934. static int smb1357_get_charging_status(struct i2c_client *client)
  935. {
  936. struct smb1357_charger_data *smb1357data =
  937. i2c_get_clientdata(client);
  938. int status = POWER_SUPPLY_STATUS_UNKNOWN;
  939. u8 status_1 = 0;
  940. u8 status_4 = 0;
  941. /* need delay to update charger status */
  942. msleep(10);
  943. smb1357_charger_i2c_read(client, STATUS_1_REG, &status_1);
  944. smb1357_charger_i2c_read(client, STATUS_4_REG, &status_4);
  945. dev_info(&client->dev,"chg status:s1[0x%2x],s4[0x%2x]\n",
  946. status_1, status_4);
  947. /* At least one charge cycle terminated,
  948. * Charge current < Termination Current
  949. */
  950. if ((smb1357data->hvdcp_det_count == 0) &&
  951. (status_4 & BATT_DONE_STATUS)) {
  952. /* top-off by full charging */
  953. status = POWER_SUPPLY_STATUS_FULL;
  954. goto charging_status_end;
  955. }
  956. /* Is Charge enabled ? */
  957. if (status_4 & CHG_EN_BIT) {
  958. /* CHG Status [2:1] : no Chg (0b00)
  959. * not charging
  960. */
  961. if (!(status_4 & CHG_TYPE_MASK)) {
  962. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  963. goto charging_status_end;
  964. } else {
  965. status = POWER_SUPPLY_STATUS_CHARGING;
  966. goto charging_status_end;
  967. }
  968. } else
  969. status = POWER_SUPPLY_STATUS_DISCHARGING;
  970. charging_status_end:
  971. return (int)status;
  972. }
  973. static int smb1357_get_charging_health(struct i2c_client *client)
  974. {
  975. struct smb1357_charger_data *smb1357data =
  976. dev_get_drvdata(&client->dev);
  977. struct sec_charger_info *charger = smb1357data->charger;
  978. int health = POWER_SUPPLY_HEALTH_GOOD;
  979. u8 status_1;
  980. u8 status_4;
  981. u8 status_8;
  982. u8 irq_e;
  983. u8 chg_en;
  984. u8 chg_cur;
  985. u8 gpio = false;
  986. smb1357_charger_i2c_read(client, STATUS_1_REG, &status_1);
  987. smb1357_charger_i2c_read(client, STATUS_4_REG, &status_4);
  988. smb1357_charger_i2c_read(client, STATUS_8_REG, &status_8);
  989. smb1357_charger_i2c_read(client, IRQ_E_REG, &irq_e);
  990. smb1357_charger_i2c_read(client, CMD_CHG_REG, &chg_en);
  991. smb1357_charger_i2c_read(client, CFG_1C_REG, &chg_cur);
  992. if (smb1357data->pogo_det_gpio) {
  993. gpio = gpio_get_value(smb1357data->pogo_det_gpio);
  994. dev_info(&client->dev,"health: pogo gpio %d\n",gpio);
  995. }
  996. if (charger->is_charging == true) {
  997. #if defined(CHECK_VF_BY_IRQ)
  998. handle_nested_irq(smb1357data->detbat_irq);
  999. #endif
  1000. if (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK) {
  1001. if (irq_e & IRQ_E_DC_UV_BIT) {
  1002. health = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  1003. if (gpio == true) {
  1004. dev_info(&client->dev,
  1005. "health: need to check pogo event cnt %d\n",
  1006. smb1357data->pogo_det_count);
  1007. if ((smb1357data->pogo_det_count == 0)&&
  1008. (smb1357data->pogo_status != DCIN_NONE)) {
  1009. cancel_delayed_work(&charger->isr_work);
  1010. schedule_delayed_work(&charger->isr_work,
  1011. msecs_to_jiffies(0));
  1012. }
  1013. }
  1014. }
  1015. else if (irq_e & IRQ_E_DC_OV_BIT) {
  1016. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1017. }
  1018. }
  1019. else {
  1020. if (irq_e & IRQ_E_USB_UV_BIT)
  1021. health = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  1022. else if (irq_e & IRQ_E_USB_OV_BIT)
  1023. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1024. }
  1025. if ((smb1357data->siop_level >= 100) &&
  1026. ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1027. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1028. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK))) {
  1029. #if defined(ENABLE_CHG_THERM_LIMIT)
  1030. int temp = 0;
  1031. temp = smb1357_get_therm_temp(client);
  1032. if (smb1357data->chg_limit == false) {
  1033. if (smb1357data->chg_high_temp < temp){
  1034. smb1357data->chg_limit = true;
  1035. if (smb1357data->sysfs_mode == USE_SYSFS_CUR){
  1036. smb1357_set_input_current_limit(client,
  1037. smb1357data->sysfs_curin);
  1038. smb1357_set_charging_current(client,
  1039. smb1357data->sysfs_curout);
  1040. }
  1041. else {
  1042. smb1357_set_input_current_limit(client,
  1043. CHG_THERM_INPUT_LIMIT_CURRENT);
  1044. smb1357_set_charging_current(client,
  1045. CHG_THERM_CHARGING_LIMIT_CURRENT);
  1046. }
  1047. dev_info(&client->dev,"%s chg set limit temp %d\n",
  1048. __func__, temp);
  1049. }
  1050. }
  1051. if (smb1357data->chg_limit == true) {
  1052. if (smb1357data->chg_high_temp_recovery > temp) {
  1053. smb1357data->chg_limit = false;
  1054. smb1357_set_input_current_limit(client,
  1055. charger->pdata->charging_current
  1056. [charger->cable_type].input_current_limit);
  1057. smb1357_set_charging_current(client,
  1058. charger->pdata->charging_current
  1059. [charger->cable_type].fast_charging_current);
  1060. dev_info(&client->dev,"%s chg limit free temp %d\n",
  1061. __func__, temp);
  1062. }
  1063. else if (CHG_THERM_REDUCE_MAX_TEMP < temp)
  1064. {
  1065. smb1357_set_input_current_limit(client,
  1066. SIOP_INPUT_LIMIT_CURRENT);
  1067. smb1357_set_charging_current(client,
  1068. SIOP_CHARGING_LIMIT_CURRENT);
  1069. dev_info(&client->dev,
  1070. "%s WARNING!! OVER THE MAX TEMP\n", __func__);
  1071. }
  1072. }
  1073. #endif
  1074. }
  1075. #if defined(TIMER_FORCE_BLOCK)
  1076. if ((charger->is_charging == true) &&
  1077. ((charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1078. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK))) {
  1079. if ((force_block_count >= FORCE_BLOCK_MAX) &&
  1080. force_block == false) {
  1081. force_block = true;
  1082. force_block_count = 0;
  1083. smb1357_set_charging_current(client,2100);
  1084. }
  1085. else if((force_block_count >= FORCE_BLOCK_MIN)&&
  1086. force_block == true) {
  1087. force_block = false;
  1088. force_block_count = 0;
  1089. smb1357_set_charging_current(client,3000);
  1090. }
  1091. force_block_count++;
  1092. dev_dbg(&client->dev,"%s timer check %d, block %d\n",
  1093. __func__, force_block_count, force_block);
  1094. }
  1095. else {
  1096. force_block = false;
  1097. force_block_count = 0;
  1098. }
  1099. #endif
  1100. }
  1101. if (status_1 & 0x08){
  1102. if (smb1357data->ovp_gpio_en) {
  1103. ovp_gpio_enable(smb1357data->ovp_gpio_en, true);
  1104. msleep(500);
  1105. ovp_gpio_enable(smb1357data->ovp_gpio_en, false);
  1106. }
  1107. dev_dbg(&client->dev,"USBIN suspended %s\n",__func__);
  1108. }
  1109. dev_info(&client->dev,"health:S1[0x%2x]S4[0x%2x]S8[0x%2x]",
  1110. status_1, status_4, status_8);
  1111. dev_info(&client->dev,"health:irqE[0x%2x]en[0x%2x]cur[0x%2x]\n",
  1112. irq_e, chg_en, chg_cur);
  1113. return (int)health;
  1114. }
  1115. static void smb1357_enter_suspend(struct i2c_client *client)
  1116. {
  1117. pr_info("%s: ENTER SUSPEND\n", __func__);
  1118. smb1357_set_input_current_limit(client, 0x00);
  1119. smb1357_set_charging_current(client, 0x00);
  1120. smb1357_enable_charging(client, false);
  1121. smb1357_enable_otg(client, false);
  1122. /* When the battery removed. It will be shutdown the device.
  1123. * Set min input current
  1124. * Disable Charging.
  1125. */
  1126. if (smb1357_check_vf_status(client) == false) {
  1127. smb1357_chgin_shutdown(client, true);
  1128. msleep(100);
  1129. }
  1130. else {
  1131. smb1357_chgin_shutdown(client, false);
  1132. }
  1133. }
  1134. static void smb1357_charger_function_control(
  1135. struct i2c_client *client)
  1136. {
  1137. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1138. struct sec_charger_info *charger = smb1357data->charger;
  1139. union power_supply_propval val;
  1140. u8 usb_type, usb_mode, term, status, data_f;
  1141. union power_supply_propval input_value;
  1142. if (charger->charging_current < 0) {
  1143. dev_dbg(&client->dev,
  1144. "%s : OTG is activated. Ignore command!\n", __func__);
  1145. return;
  1146. }
  1147. /* [STEP - 1] ================================================
  1148. * Volatile write permission(bit 6) - allow(1)
  1149. */
  1150. smb1357_write_enable(charger->client, true);
  1151. psy_do_property("battery", get,
  1152. POWER_SUPPLY_PROP_HEALTH, input_value);
  1153. if (input_value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
  1154. pr_info("[SMB1357] Unspec_failure, charger suspend\n");
  1155. smb1357_enter_suspend(client);
  1156. }
  1157. else if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
  1158. cancel_delayed_work(&smb1357data->hvdcp_det_work);
  1159. /* Charger Disabled */
  1160. smb1357_enable_charging(client, false);
  1161. pr_info("[SMB1357] Set the registers to the default configuration\n");
  1162. smb1357_set_default_data(client);
  1163. smb1357_charger_i2c_read(client, IRQ_F_REG, &data_f);
  1164. if ((data_f & IRQ_F_POWER_OK_BIT) == IRQ_F_POWER_OK_BIT) {
  1165. /* Check USBIN/DCIN HV/LV Port */
  1166. smb1357_charger_i2c_read(client, STATUS_8_REG, &status);
  1167. if (((status & (USBIN_HV | USBIN_LV)) == false) &&
  1168. ((status & (DCIN_HV | DCIN_LV)) != false) &&
  1169. (smb1357data->pogo_2nd_charging == false)) {
  1170. /* Removed USBIN , Connected DCIN, Check TOP-OFF */
  1171. if (smb1357data->pogo_det_gpio) {
  1172. wake_lock(&smb1357data->chg_wake_lock);
  1173. cancel_delayed_work(&smb1357data->pogo_det_work);
  1174. smb1357data->pogo_det_count = 3;
  1175. schedule_delayed_work(&smb1357data->pogo_det_work,
  1176. msecs_to_jiffies(200));
  1177. dev_dbg(&client->dev,"%s, send pogo detection\n",__func__);
  1178. }
  1179. }
  1180. dev_dbg(&client->dev,"%s : s8 0x%x 2nd = %d, pogo gpio %d\n",
  1181. __func__, status, smb1357data->pogo_2nd_charging,
  1182. gpio_get_value(smb1357data->pogo_det_gpio));
  1183. }
  1184. }
  1185. else {
  1186. int full_check_type;
  1187. psy_do_property("battery", get, POWER_SUPPLY_PROP_CHARGE_NOW, val);
  1188. if (val.intval == SEC_BATTERY_CHARGING_1ST)
  1189. full_check_type = charger->pdata->full_check_type;
  1190. else
  1191. full_check_type = charger->pdata->full_check_type_2nd;
  1192. smb1357_chgin_shutdown(client, false);
  1193. /* [STEP - 2] ================================================
  1194. * USB 5/1(9/1.5) Mode(bit 1) - USB1/USB1.5(0), USB5/USB9(1)
  1195. * USB/HC Mode(bit 0) - USB5/1 or USB9/1.5 Mode(0)
  1196. * High-Current Mode(1)
  1197. */
  1198. switch (charger->cable_type) {
  1199. case POWER_SUPPLY_TYPE_USB_CDP:
  1200. case POWER_SUPPLY_TYPE_MISC:
  1201. case POWER_SUPPLY_TYPE_WIRELESS:
  1202. case POWER_SUPPLY_TYPE_CARDOCK:
  1203. case POWER_SUPPLY_TYPE_UARTOFF:
  1204. case POWER_SUPPLY_TYPE_MHL_900:
  1205. case POWER_SUPPLY_TYPE_MHL_1500:
  1206. case POWER_SUPPLY_TYPE_LAN_HUB:
  1207. case POWER_SUPPLY_TYPE_SMART_NOTG:
  1208. /* High-current mode */
  1209. usb_type = USB3_VAL;
  1210. usb_mode = USB_AC_VAL;
  1211. break;
  1212. case POWER_SUPPLY_TYPE_UNKNOWN:
  1213. case POWER_SUPPLY_TYPE_MAINS:
  1214. case POWER_SUPPLY_TYPE_HV_MAINS:
  1215. case POWER_SUPPLY_TYPE_POGODOCK:
  1216. case POWER_SUPPLY_TYPE_UPS:
  1217. case POWER_SUPPLY_TYPE_USB:
  1218. case POWER_SUPPLY_TYPE_USB_DCP:
  1219. case POWER_SUPPLY_TYPE_USB_ACA:
  1220. case POWER_SUPPLY_TYPE_MHL_500:
  1221. case POWER_SUPPLY_TYPE_MHL_USB:
  1222. case POWER_SUPPLY_TYPE_SMART_OTG:
  1223. case POWER_SUPPLY_TYPE_POWER_SHARING:
  1224. /* USB5 */
  1225. usb_type = USB2_VAL;
  1226. usb_mode = USB_500_VAL;
  1227. break;
  1228. default:
  1229. /* USB1 */
  1230. usb_type = USB2_VAL;
  1231. usb_mode = USB_100_VAL;
  1232. break;
  1233. }
  1234. smb1357_usb_current_limit(client, usb_type, usb_mode);
  1235. /* [STEP 3] Charge Current(0x00) ===============================
  1236. * Set pre-charge current(bit 7:5) - 250mA(011)
  1237. * Set fast charge current(bit 4:0)
  1238. */
  1239. switch (full_check_type) {
  1240. case SEC_BATTERY_FULLCHARGED_CHGGPIO:
  1241. case SEC_BATTERY_FULLCHARGED_CHGINT:
  1242. case SEC_BATTERY_FULLCHARGED_CHGPSY:
  1243. case SEC_BATTERY_FULLCHARGED_TIME:
  1244. smb1357_set_cur_termination(client, false);
  1245. if (val.intval == SEC_BATTERY_CHARGING_1ST) {
  1246. dev_info(&client->dev,
  1247. "1st %s : termination current (%dmA)\n",
  1248. __func__, charger->pdata->charging_current[
  1249. charger->cable_type].full_check_current_1st);
  1250. term = smb1357_get_term_current_limit_data(
  1251. charger->pdata->charging_current[
  1252. charger->cable_type].full_check_current_1st);
  1253. smb1357data->pogo_2nd_charging = false;
  1254. } else {
  1255. if (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK)
  1256. smb1357data->pogo_2nd_charging = true;
  1257. #ifndef SECOND_TERMINATION_CURRENT
  1258. dev_info(&client->dev,
  1259. "2st %s : termination current (%dmA)\n",
  1260. __func__, charger->pdata->charging_current[
  1261. charger->cable_type].full_check_current_2nd);
  1262. term = smb1357_get_term_current_limit_data(
  1263. charger->pdata->charging_current[
  1264. charger->cable_type].full_check_current_2nd);
  1265. #else
  1266. /* Disable Charging */
  1267. smb1357_enable_charging(client, false);
  1268. dev_info(&client->dev,
  1269. "2st %s : termination current (%dmA)\n",
  1270. __func__, charger->pdata->charging_current[
  1271. charger->cable_type].full_check_current_1st
  1272. / SECOND_TERMINATION_CURRENT);
  1273. term = smb1357_get_term_current_limit_data(
  1274. charger->pdata->charging_current[
  1275. charger->cable_type].full_check_current_1st
  1276. / SECOND_TERMINATION_CURRENT);
  1277. #endif
  1278. }
  1279. /* termination level */
  1280. smb1357_charger_masked_write_reg(client, CFG_3_REG,
  1281. CHG_ITERM_MASK, term);
  1282. /* enable termination */
  1283. smb1357_set_cur_termination(client, true);
  1284. break;
  1285. }
  1286. /* Set charging current */
  1287. smb1357_set_charging_current(client, charger->charging_current);
  1288. /* [STEP - 4] ===============================================
  1289. * Set Input Current limit : default 1600mA
  1290. * */
  1291. dev_info(&client->dev, "%s : input current (%dmA)\n",
  1292. __func__, charger->pdata->charging_current
  1293. [charger->cable_type].input_current_limit);
  1294. if ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1295. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1296. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK)) {
  1297. smb1357_set_input_current_limit(client,
  1298. (smb1357data->siop_level < 100) ?
  1299. SIOP_CHARGING_LIMIT_CURRENT :
  1300. charger->pdata->charging_current
  1301. [charger->cable_type].input_current_limit);
  1302. }
  1303. else {
  1304. smb1357_set_input_current_limit(client,
  1305. charger->pdata->charging_current
  1306. [charger->cable_type].input_current_limit);
  1307. }
  1308. /* [STEP - 5] =================================================
  1309. * Disable Pin Control, Enable CMD control
  1310. * USB5/1/HC input State(bit0) - Dual-state input(1)
  1311. */
  1312. smb1357_charger_masked_write_reg(client, CFG_E_REG,
  1313. USB_CTRL_BY_PIN_BIT|USB_DUAL_STATE_BIT,
  1314. 0x00|USB_DUAL_STATE_BIT);
  1315. /* [STEP - 6] =================================================
  1316. * AICL - Enalbed(1)
  1317. */
  1318. if (charger->pdata->chg_functions_setting &
  1319. SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT) {
  1320. /* disable AICL */
  1321. smb1357_charger_masked_write_reg(client, CFG_D_REG,
  1322. USBIN_AICL_BIT, 0x00);
  1323. }
  1324. else {
  1325. /* disable AICL */
  1326. smb1357_charger_masked_write_reg(client, CFG_D_REG,
  1327. USBIN_AICL_BIT, 0x00);
  1328. msleep(50);
  1329. /* enable AICL */
  1330. smb1357_charger_masked_write_reg(client, CFG_D_REG,
  1331. USBIN_AICL_BIT, USBIN_AICL_BIT);
  1332. }
  1333. /* [STEP - 7] =================================================
  1334. * Float Voltage(bit 5:0)
  1335. */
  1336. smb1357_set_vfloat(client, charger->pdata->chg_float_voltage);
  1337. /* [STEP - 8] =================================================
  1338. * Charge control
  1339. * Automatic Recharge disable,
  1340. * Current Termination disable,
  1341. * APSD enable
  1342. */
  1343. if (charger->cable_type == POWER_SUPPLY_TYPE_MAINS) {
  1344. /* Sometimes It didn't detected TA_HV.
  1345. * Don't move below code to other point.
  1346. */
  1347. u8 hvdcp_type = 0;
  1348. smb1357_charger_masked_write_reg(client, CFG_E_REG,
  1349. HVDCP_ADAPTER_MASK|HVDCP_ENABLE_BIT,
  1350. HVDCP_ADAPTER_9V|HVDCP_ENABLE_BIT);
  1351. msleep(100);
  1352. smb1357_charger_i2c_read(client, STATUS_7_REG, &hvdcp_type);
  1353. if (hvdcp_type == 0x00) {
  1354. /* USBIN ADAPTER : 9V only */
  1355. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  1356. USBIN_ADAPTER_MASK, 0x60);
  1357. }
  1358. }
  1359. if ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1360. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1361. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK)) {
  1362. smb1357_set_APSD(client, true);
  1363. }
  1364. else {
  1365. smb1357_set_APSD(client, false);
  1366. }
  1367. /* USBIN ADAPTER : 5V or 9V */
  1368. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  1369. USBIN_ADAPTER_MASK, 0x20);
  1370. smb1357_set_auto_recharge(client, true);
  1371. /* [STEP - 9] =================================================
  1372. * STAT active low(bit 1),
  1373. */
  1374. smb1357_set_STAT_conf(client);
  1375. /* [STEP - 10] =================================================
  1376. * Mininum System Voltage(bit 6) - 3.75v
  1377. */
  1378. smb1357_set_VSYS(client);
  1379. /* [STEP - 11] ================================================
  1380. * Low-Battery/SYSOK Voltage threshold - 3.25V
  1381. */
  1382. smb1357_lowbatt_threshold(client);
  1383. /* [STEP - 12] ================================================
  1384. * Hard/Soft Limit Cell temp monitor
  1385. */
  1386. smb1357_temp_compensation(client);
  1387. /* [STEP - 13] ================================================
  1388. * STATUS ingerrupt - Clear
  1389. */
  1390. smb1357_clear_irq_status(client);
  1391. /* [STEP - 14] ================================================
  1392. * Interrupt - Configure.
  1393. */
  1394. smb1357_irq_cfg(client);
  1395. /* Enable batt irq */
  1396. smb1357_enable_batt_irq(client, false);
  1397. /* [STEP - 15] ================================================
  1398. * Volatile write permission(bit 6) - allowed(1)
  1399. * Charging Enable(bit 1) - Enabled(1)
  1400. */
  1401. if (charger->charging_current > 0)
  1402. smb1357_enable_charging(client, true);
  1403. else
  1404. smb1357_enable_charging(client, false);
  1405. /* Start HVDCP Detect WorkQueue */
  1406. if (charger->cable_type == POWER_SUPPLY_TYPE_MAINS){
  1407. smb1357data->hvdcp_det_count = 3;
  1408. cancel_delayed_work(&smb1357data->hvdcp_det_work);
  1409. schedule_delayed_work(&smb1357data->hvdcp_det_work,
  1410. msecs_to_jiffies(3000));
  1411. }
  1412. else if (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK) {
  1413. /* DCIN enable AICL */
  1414. smb1357_charger_masked_write_reg(client, CFG_B_REG,
  1415. DCIN_AICL_BIT, DCIN_AICL_BIT);
  1416. }
  1417. else {
  1418. cancel_delayed_work(&smb1357data->hvdcp_det_work);
  1419. /* DCIN enable AICL */
  1420. smb1357_charger_masked_write_reg(client, CFG_B_REG,
  1421. DCIN_AICL_BIT, 0x00);
  1422. }
  1423. }
  1424. dev_info(&client->dev,"%s : %d\n", __func__,__LINE__);
  1425. }
  1426. static void smb1357_charger_otg_control(
  1427. struct i2c_client *client)
  1428. {
  1429. struct smb1357_charger_data *smb1357data =
  1430. i2c_get_clientdata(client);
  1431. struct sec_charger_info *charger = smb1357data->charger;
  1432. union power_supply_propval value;
  1433. u8 enable = false;
  1434. switch (charger->cable_type) {
  1435. case POWER_SUPPLY_TYPE_POWER_SHARING:
  1436. psy_do_property("ps", get,
  1437. POWER_SUPPLY_PROP_STATUS, value);
  1438. if (value.intval)
  1439. enable = true;
  1440. dev_info(&client->dev, "%s : PS %s\n",
  1441. __func__, ((enable) ? "Enable":"Disable"));
  1442. case POWER_SUPPLY_TYPE_OTG:
  1443. /* Enable OTG */
  1444. #if defined(ENABLE_SHUTDOWN_MODE)
  1445. if (smb1357data->shdn_gpio) {
  1446. gpio_set_value(smb1357data->shdn_gpio, true);
  1447. msleep(10);
  1448. }
  1449. #endif
  1450. /* OTG output current limit : 250mA */
  1451. smb1357_charger_masked_write_reg(client, USBIN_DCIN_CFG_REG,
  1452. OTG_CURRENT_LIMIT_MASK, 0x00);
  1453. /* OTG Under Voltage */
  1454. smb1357_usb_current_limit(client, USB3_VAL, USB_500_VAL);
  1455. /* OTG Over Current IRQ */
  1456. smb1357_enable_irq(client, IRQ_CFG_REG,
  1457. IRQ_OTG_OV_CURRENT_BIT, IRQ_OTG_OV_CURRENT_BIT);
  1458. /* OTG output current limit : 1000mA */
  1459. #if defined(CONFIG_SEC_FACTORY)
  1460. smb1357_charger_masked_write_reg(client, USBIN_DCIN_CFG_REG,
  1461. OTG_CURRENT_LIMIT_MASK, 0x04);
  1462. #else
  1463. smb1357_charger_masked_write_reg(client, USBIN_DCIN_CFG_REG,
  1464. OTG_CURRENT_LIMIT_MASK, OTG_CURRENT_LIMIT_MASK);
  1465. #endif
  1466. if (charger->cable_type == POWER_SUPPLY_TYPE_OTG) {
  1467. enable = true;
  1468. }
  1469. /* Enable otg */
  1470. if (smb1357data->revision < CS21_REVISION) {
  1471. smb1357_enable_otg(client, enable);
  1472. smb1357_enable_otg(client, false);
  1473. }
  1474. smb1357_enable_otg(client, enable);
  1475. break;
  1476. default:
  1477. /* Disable OTG */
  1478. /* OTG Over Current IRQ */
  1479. smb1357_enable_irq(client, IRQ_CFG_REG,
  1480. IRQ_OTG_OV_CURRENT_BIT, 0x00);
  1481. /* disable otg */
  1482. if (smb1357data->revision < CS21_REVISION) {
  1483. smb1357_enable_otg(client, false);
  1484. }
  1485. smb1357_enable_otg(client, false);
  1486. if (smb1357data->revision < CS21_REVISION) {
  1487. if (smb1357data->ovp_gpio_en) {
  1488. ovp_gpio_enable(smb1357data->ovp_gpio_en, true);
  1489. msleep(30);
  1490. ovp_gpio_enable(smb1357data->ovp_gpio_en, false);
  1491. }
  1492. }
  1493. break;
  1494. }
  1495. dev_info(&client->dev, "%s : OTG [%s] \n",__func__,
  1496. ((enable) ? "ON":"OFF"));
  1497. return;
  1498. }
  1499. void smb1357_charger_shutdown(struct i2c_client *client)
  1500. {
  1501. pr_info("%s: smb1357 Charging Disabled\n", __func__);
  1502. smb1357_charger_masked_write_reg(client, CFG_E_REG,
  1503. HVDCP_ADAPTER_MASK, HVDCP_ADAPTER_5V);
  1504. return;
  1505. }
  1506. static int smb1357_debugfs_show(struct seq_file *s, void *data)
  1507. {
  1508. struct sec_charger_info *charger = s->private;
  1509. u8 reg;
  1510. u8 reg_data;
  1511. seq_printf(s, "SMB CHARGER IC :\n");
  1512. seq_printf(s, "==================\n");
  1513. for (reg = 0x00; reg <= 0x1F; reg++) {
  1514. smb1357_charger_i2c_read(charger->client, reg, &reg_data);
  1515. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  1516. }
  1517. for (reg = 0x40; reg <= 0x57; reg++) {
  1518. smb1357_charger_i2c_read(charger->client, reg, &reg_data);
  1519. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  1520. }
  1521. seq_printf(s, "\n");
  1522. return 0;
  1523. }
  1524. static int smb1357_debugfs_open(struct inode *inode, struct file *file)
  1525. {
  1526. return single_open(file, smb1357_debugfs_show, inode->i_private);
  1527. }
  1528. static const struct file_operations smb1357_debugfs_fops = {
  1529. .open = smb1357_debugfs_open,
  1530. .read = seq_read,
  1531. .llseek = seq_lseek,
  1532. .release = single_release,
  1533. };
  1534. bool smb1357_hal_chg_init(struct i2c_client *client)
  1535. {
  1536. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1537. struct sec_charger_info *charger = smb1357data->charger;
  1538. dev_info(&client->dev,
  1539. "%s: SMB1357 Charger init(Start)!!\n", __func__);
  1540. /* sys-kernel-debug-smb1357_regs */
  1541. (void) debugfs_create_file("smb1357_regs",
  1542. S_IRUGO, NULL, (void *)charger, &smb1357_debugfs_fops);
  1543. return true;
  1544. }
  1545. bool smb1357_hal_chg_suspend(struct i2c_client *client)
  1546. {
  1547. #if defined(ENABLE_SHUTDOWN_MODE)
  1548. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1549. struct sec_charger_info *charger = smb1357data->charger;
  1550. if (charger->is_charging == false) {
  1551. if (smb1357data->shdn_gpio) {
  1552. gpio_set_value(smb1357data->shdn_gpio, false);
  1553. }
  1554. }
  1555. #endif
  1556. return true;
  1557. }
  1558. bool smb1357_hal_chg_resume(struct i2c_client *client)
  1559. {
  1560. #if defined(ENABLE_SHUTDOWN_MODE)
  1561. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1562. struct sec_charger_info *charger = smb1357data->charger;
  1563. if (charger->is_charging == false) {
  1564. if (smb1357data->shdn_gpio) {
  1565. gpio_set_value(smb1357data->shdn_gpio, true);
  1566. }
  1567. }
  1568. #endif
  1569. return true;
  1570. }
  1571. bool smb1357_hal_chg_get_property(struct i2c_client *client,
  1572. enum power_supply_property psp,
  1573. union power_supply_propval *val)
  1574. {
  1575. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1576. struct sec_charger_info *charger = smb1357data->charger;
  1577. u8 port;
  1578. switch (psp) {
  1579. case POWER_SUPPLY_PROP_ONLINE:
  1580. val->intval = charger->cable_type;
  1581. if ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1582. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS)) {
  1583. port = smb1357_get_charger_type(charger->client);
  1584. switch(port){
  1585. case HVDCP_A:
  1586. case HVDCP_12V:
  1587. case HVDCP_9V:
  1588. case HVDCP_5V:
  1589. val->intval = POWER_SUPPLY_TYPE_HV_MAINS;
  1590. break;
  1591. default:
  1592. break;
  1593. }
  1594. }
  1595. else if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
  1596. u8 data_f;
  1597. smb1357_charger_i2c_read(client, IRQ_F_REG, &data_f);
  1598. if ((data_f & IRQ_F_POWER_OK_BIT) == IRQ_F_POWER_OK_BIT) {
  1599. val->intval = POWER_SUPPLY_TYPE_MAINS;
  1600. }
  1601. dev_info(&client->dev,
  1602. "%s:data_f 0x%x, val = %d!\n", __func__, data_f, val->intval);
  1603. }
  1604. break;
  1605. case POWER_SUPPLY_PROP_STATUS:
  1606. val->intval = smb1357_get_charging_status(client);
  1607. break;
  1608. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  1609. if (charger->is_charging)
  1610. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  1611. else
  1612. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  1613. break;
  1614. case POWER_SUPPLY_PROP_HEALTH:
  1615. val->intval = smb1357_get_charging_health(client);
  1616. break;
  1617. case POWER_SUPPLY_PROP_PRESENT:
  1618. /* check removed battery */
  1619. val->intval = smb1357_check_vf_status(client);
  1620. break;
  1621. /* calculated input current limit value */
  1622. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1623. case POWER_SUPPLY_PROP_CURRENT_AVG: /* charging current */
  1624. if (charger->charging_current) {
  1625. val->intval = smb1357_status_charging_current(client);
  1626. } else
  1627. val->intval = 0;
  1628. dev_dbg(&client->dev,
  1629. "%s : set-current(%dmA), current now(%dmA)\n",
  1630. __func__, charger->charging_current, val->intval);
  1631. break;
  1632. #if defined(CONFIG_BATTERY_SWELLING)
  1633. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  1634. smb1357_charger_i2c_read(client, VFLOAT_REG, &port);
  1635. val->intval = port;
  1636. pr_info("%s: Float voltage : 0x%x\n", __func__, val->intval);
  1637. break;
  1638. #endif
  1639. default:
  1640. return false;
  1641. }
  1642. return true;
  1643. }
  1644. bool smb1357_hal_chg_set_property(struct i2c_client *client,
  1645. enum power_supply_property psp,
  1646. const union power_supply_propval *val)
  1647. {
  1648. struct smb1357_charger_data *smb1357data = i2c_get_clientdata(client);
  1649. struct sec_charger_info *charger = smb1357data->charger;
  1650. bool ret = true;
  1651. mutex_lock(&smb1357data->mutex);
  1652. switch (psp) {
  1653. /* val->intval : type */
  1654. case POWER_SUPPLY_PROP_ONLINE:
  1655. dev_dbg(&client->dev,
  1656. "%s : cable type %d!\n", __func__, val->intval);
  1657. if (val->intval != POWER_SUPPLY_TYPE_POGODOCK) {
  1658. smb1357data->usbin_cable_type = val->intval;
  1659. dev_dbg(&client->dev,
  1660. "%s : usbin cable type %d!\n", __func__, val->intval);
  1661. }
  1662. if (charger->charging_current < 0) {
  1663. smb1357_charger_otg_control(client);
  1664. }
  1665. else if (charger->charging_current > 0) {
  1666. smb1357_charger_function_control(client);
  1667. }
  1668. else {
  1669. smb1357_charger_function_control(client);
  1670. smb1357_charger_otg_control(client);
  1671. }
  1672. break;
  1673. case POWER_SUPPLY_PROP_CURRENT_MAX: /* input current limit set */
  1674. /* calculated input current limit value */
  1675. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1676. smb1357_set_input_current_limit(client, val->intval);
  1677. break;
  1678. /* val->intval : charging current */
  1679. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1680. smb1357_set_charging_current(client, val->intval);
  1681. break;
  1682. default:
  1683. ret = false;
  1684. break;
  1685. }
  1686. mutex_unlock(&smb1357data->mutex);
  1687. return ret;
  1688. }
  1689. ssize_t smb1357_hal_chg_show_attrs(struct device *dev,
  1690. const ptrdiff_t offset, char *buf)
  1691. {
  1692. struct power_supply *psy = dev_get_drvdata(dev);
  1693. struct sec_charger_info *chg =
  1694. container_of(psy, struct sec_charger_info, psy_chg);
  1695. int i = 0;
  1696. char *str = NULL;
  1697. switch (offset) {
  1698. case CHG_REG:
  1699. i += scnprintf(buf + i, PAGE_SIZE - i, "%x\n",
  1700. chg->reg_addr);
  1701. break;
  1702. case CHG_DATA:
  1703. i += scnprintf(buf + i, PAGE_SIZE - i, "%x\n",
  1704. chg->reg_data);
  1705. break;
  1706. case CHG_REGS:
  1707. str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
  1708. if (!str)
  1709. return -ENOMEM;
  1710. smb1357_read_regs(chg->client, str);
  1711. i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
  1712. str);
  1713. kfree(str);
  1714. break;
  1715. default:
  1716. i = -EINVAL;
  1717. break;
  1718. }
  1719. return i;
  1720. }
  1721. ssize_t smb1357_hal_chg_store_attrs(struct device *dev,
  1722. const ptrdiff_t offset,
  1723. const char *buf, size_t count)
  1724. {
  1725. struct power_supply *psy = dev_get_drvdata(dev);
  1726. struct sec_charger_info *chg =
  1727. container_of(psy, struct sec_charger_info, psy_chg);
  1728. int ret = 0;
  1729. u8 data = 0;
  1730. int x = 0;
  1731. switch (offset) {
  1732. case CHG_REG:
  1733. if (sscanf(buf, "%x\n", &x) == 1) {
  1734. chg->reg_addr = x;
  1735. smb1357_charger_i2c_read(chg->client,
  1736. chg->reg_addr, &data);
  1737. chg->reg_data = data;
  1738. dev_dbg(dev, "%s: (read) addr = 0x%x, data = 0x%x\n",
  1739. __func__, chg->reg_addr, chg->reg_data);
  1740. ret = count;
  1741. }
  1742. break;
  1743. case CHG_DATA:
  1744. if (sscanf(buf, "%x\n", &x) == 1) {
  1745. data = (u8)x;
  1746. dev_dbg(dev, "%s: (write) addr = 0x%x, data = 0x%x\n",
  1747. __func__, chg->reg_addr, data);
  1748. smb1357_charger_i2c_write(chg->client,
  1749. chg->reg_addr, &data);
  1750. ret = count;
  1751. }
  1752. break;
  1753. default:
  1754. ret = -EINVAL;
  1755. break;
  1756. }
  1757. return ret;
  1758. }
  1759. static struct device_attribute smb1357_charger_attrs[] = {
  1760. SMB1357_CHARGER_ATTR(reg),
  1761. SMB1357_CHARGER_ATTR(data),
  1762. SMB1357_CHARGER_ATTR(regs),
  1763. POGO_ATTR(pogo),
  1764. CHIP_ID_CHECK_ATTR(chip_id),
  1765. CHG_THERM_ATTR(chg_therm),
  1766. CHG_THERM_ADC_ATTR(chg_therm_adc),
  1767. CHG_CURRENT_ATTR(cur_mode),
  1768. CHG_CURRENT_ATTR(cur_set),
  1769. };
  1770. static enum power_supply_property smb1357_charger_props[] = {
  1771. POWER_SUPPLY_PROP_STATUS,
  1772. POWER_SUPPLY_PROP_PRESENT,
  1773. POWER_SUPPLY_PROP_CHARGE_TYPE,
  1774. POWER_SUPPLY_PROP_HEALTH,
  1775. POWER_SUPPLY_PROP_ONLINE,
  1776. POWER_SUPPLY_PROP_CURRENT_MAX,
  1777. POWER_SUPPLY_PROP_CURRENT_AVG,
  1778. POWER_SUPPLY_PROP_CURRENT_NOW,
  1779. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  1780. #if defined(CONFIG_BATTERY_SWELLING)
  1781. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  1782. #endif
  1783. };
  1784. static int smb1357_chg_get_property(struct power_supply *psy,
  1785. enum power_supply_property psp,
  1786. union power_supply_propval *val)
  1787. {
  1788. struct sec_charger_info *charger =
  1789. container_of(psy, struct sec_charger_info, psy_chg);
  1790. switch (psp) {
  1791. case POWER_SUPPLY_PROP_CURRENT_MAX: /* input current limit set */
  1792. val->intval = charger->charging_current_max;
  1793. break;
  1794. #if defined(CONFIG_BATTERY_SWELLING)
  1795. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  1796. #endif
  1797. case POWER_SUPPLY_PROP_PRESENT:
  1798. case POWER_SUPPLY_PROP_ONLINE:
  1799. case POWER_SUPPLY_PROP_STATUS:
  1800. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  1801. case POWER_SUPPLY_PROP_HEALTH:
  1802. case POWER_SUPPLY_PROP_CURRENT_AVG: /* charging current */
  1803. /* calculated input current limit value */
  1804. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1805. if (!smb1357_hal_chg_get_property(charger->client, psp, val))
  1806. return -EINVAL;
  1807. break;
  1808. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1809. break;
  1810. default:
  1811. return -EINVAL;
  1812. }
  1813. return 0;
  1814. }
  1815. static int smb1357_chg_set_property(struct power_supply *psy,
  1816. enum power_supply_property psp,
  1817. const union power_supply_propval *val)
  1818. {
  1819. struct sec_charger_info *charger =
  1820. container_of(psy, struct sec_charger_info, psy_chg);
  1821. struct smb1357_charger_data *smb1357data =
  1822. dev_get_drvdata(&charger->client->dev);
  1823. union power_supply_propval input_value;
  1824. union power_supply_propval charging_value;
  1825. int set_charging_current, set_charging_current_max;
  1826. const int usb_charging_current = charger->pdata->charging_current[
  1827. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  1828. #if defined(CONFIG_BATTERY_SWELLING)
  1829. u8 reg_data;
  1830. #endif
  1831. switch (psp) {
  1832. case POWER_SUPPLY_PROP_STATUS:
  1833. charger->status = val->intval;
  1834. break;
  1835. /* val->intval : type */
  1836. case POWER_SUPPLY_PROP_ONLINE:
  1837. /* change current cable type */
  1838. charger->cable_type = val->intval;
  1839. set_charging_current =
  1840. charger->pdata->charging_current[
  1841. charger->cable_type].fast_charging_current;
  1842. set_charging_current_max =
  1843. charger->pdata->charging_current[
  1844. charger->cable_type].input_current_limit;
  1845. if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY){
  1846. charger->is_charging = false;
  1847. }
  1848. else {
  1849. charger->is_charging = true;
  1850. /* current setting */
  1851. if ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1852. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1853. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK)) {
  1854. /* charging current after compensation of siop */
  1855. if (smb1357data->siop_level == 0) {
  1856. set_charging_current = usb_charging_current;
  1857. }
  1858. else {
  1859. set_charging_current =
  1860. (set_charging_current * smb1357data->siop_level) / 100;
  1861. }
  1862. if ((set_charging_current > 0) &&
  1863. (set_charging_current < usb_charging_current)){
  1864. set_charging_current = usb_charging_current;
  1865. }
  1866. /* input current after compenation of siop */
  1867. if (smb1357data->siop_level < 100) {
  1868. if (set_charging_current_max >
  1869. SIOP_INPUT_LIMIT_CURRENT) {
  1870. set_charging_current_max =
  1871. SIOP_INPUT_LIMIT_CURRENT;
  1872. }
  1873. if (set_charging_current >
  1874. SIOP_CHARGING_LIMIT_CURRENT) {
  1875. set_charging_current =
  1876. SIOP_CHARGING_LIMIT_CURRENT;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. /* charging current after compensation of siop */
  1882. charger->charging_current = set_charging_current;
  1883. /* input current after compenation of siop */
  1884. charger->charging_current_max = set_charging_current_max;
  1885. dev_info(&charger->client->dev,"%s siop= %d,chg_cur %d, in_cur %d\n",
  1886. __func__, smb1357data->siop_level, charger->charging_current,
  1887. set_charging_current);
  1888. if (!smb1357_hal_chg_set_property(charger->client, psp, val))
  1889. return -EINVAL;
  1890. break;
  1891. /* val->intval : input current limit set */
  1892. case POWER_SUPPLY_PROP_CURRENT_MAX:
  1893. charger->charging_current_max = val->intval;
  1894. /* to control charging current,
  1895. * use input current limit and set charging current as much as possible
  1896. * so we only control input current limit to control charge current
  1897. */
  1898. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1899. if (!smb1357_hal_chg_set_property(charger->client, psp, val))
  1900. return -EINVAL;
  1901. break;
  1902. /* val->intval : charging current */
  1903. case POWER_SUPPLY_PROP_CURRENT_AVG:
  1904. charger->charging_current = val->intval;
  1905. if (!smb1357_hal_chg_set_property(charger->client, psp, val))
  1906. return -EINVAL;
  1907. break;
  1908. /* val->intval : SIOP level (%)
  1909. * SIOP charging current setting
  1910. */
  1911. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1912. /* change val as charging current by SIOP level
  1913. * do NOT change initial charging current setting
  1914. */
  1915. smb1357data->siop_level = val->intval;
  1916. if (charger->is_charging == true) {
  1917. input_value.intval =
  1918. charger->pdata->charging_current[
  1919. charger->cable_type].input_current_limit;
  1920. charging_value.intval =
  1921. charger->pdata->charging_current[
  1922. charger->cable_type].fast_charging_current;
  1923. if ((charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
  1924. (charger->cable_type == POWER_SUPPLY_TYPE_HV_MAINS) ||
  1925. (charger->cable_type == POWER_SUPPLY_TYPE_POGODOCK)) {
  1926. #if defined(REDUCE_TMM_CHG)
  1927. if (smb1357data->siop_level == TMM_SIOP_LVL) {
  1928. /* enforce only 5V charging */
  1929. smb1357_charger_masked_write_reg(charger->client, CFG_E_REG,
  1930. HVDCP_ADAPTER_MASK, HVDCP_ADAPTER_5V);
  1931. /* no change of input limit current
  1932. * set charging current */
  1933. charging_value.intval = TMM_CHG_CURRENT;
  1934. }
  1935. else {
  1936. /* enable 9V charging */
  1937. smb1357_charger_masked_write_reg(charger->client, CFG_E_REG,
  1938. HVDCP_ADAPTER_MASK, HVDCP_ADAPTER_9V);
  1939. if (smb1357data->siop_level < 100) {
  1940. input_value.intval = SIOP_INPUT_LIMIT_CURRENT;
  1941. }
  1942. if (smb1357data->siop_level == 0) {
  1943. charging_value.intval = usb_charging_current;
  1944. }
  1945. else {
  1946. charging_value.intval =
  1947. (charging_value.intval * smb1357data->siop_level) / 100;
  1948. }
  1949. /* charging current should be over than USB charging current */
  1950. if ((charging_value.intval > 0) &&
  1951. (charging_value.intval < usb_charging_current)){
  1952. charging_value.intval = usb_charging_current;
  1953. }
  1954. if ((smb1357data->siop_level < 100) &&
  1955. (charging_value.intval > SIOP_CHARGING_LIMIT_CURRENT)){
  1956. charging_value.intval = SIOP_CHARGING_LIMIT_CURRENT;
  1957. }
  1958. }
  1959. #else
  1960. if (smb1357data->siop_level < 100){
  1961. input_value.intval = SIOP_INPUT_LIMIT_CURRENT;
  1962. }
  1963. if (smb1357data->siop_level == 0) {
  1964. charging_value.intval = usb_charging_current;
  1965. }
  1966. else {
  1967. charging_value.intval =
  1968. (charging_value.intval * smb1357data->siop_level) / 100;
  1969. }
  1970. /* charging current should be over than USB charging current */
  1971. if ((charging_value.intval > 0) &&
  1972. (charging_value.intval < usb_charging_current)){
  1973. charging_value.intval = usb_charging_current;
  1974. }
  1975. if ((smb1357data->siop_level < 100) &&
  1976. (charging_value.intval > SIOP_CHARGING_LIMIT_CURRENT)){
  1977. charging_value.intval = SIOP_CHARGING_LIMIT_CURRENT;
  1978. }
  1979. #endif
  1980. }
  1981. /* input current after compenation of siop */
  1982. charger->charging_current_max = input_value.intval;
  1983. /* charging current after compensation of siop */
  1984. charger->charging_current = charging_value.intval;
  1985. dev_info(&charger->client->dev,"%s siop= %d,chg_cur %d, in_cur %d\n",
  1986. __func__, smb1357data->siop_level, charger->charging_current,
  1987. charger->charging_current_max);
  1988. /* set input current limit */
  1989. if (!smb1357_hal_chg_set_property(charger->client,
  1990. POWER_SUPPLY_PROP_CURRENT_NOW, &input_value))
  1991. return -EINVAL;
  1992. /* set charging current as new value */
  1993. if (!smb1357_hal_chg_set_property(charger->client,
  1994. POWER_SUPPLY_PROP_CURRENT_AVG, &charging_value))
  1995. return -EINVAL;
  1996. }
  1997. break;
  1998. #if defined(CONFIG_BATTERY_SWELLING)
  1999. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  2000. pr_info("%s: float voltage(%d)\n", __func__, val->intval);
  2001. smb1357_set_vfloat(charger->client, val->intval);
  2002. smb1357_charger_i2c_read(charger->client, VFLOAT_REG, &reg_data);
  2003. pr_info("%s: Float voltage set to : 0x%x\n", __func__, reg_data);
  2004. break;
  2005. #endif
  2006. default:
  2007. return -EINVAL;
  2008. }
  2009. return 0;
  2010. }
  2011. static bool smb1357_is_pogo_event(struct i2c_client *client)
  2012. {
  2013. struct smb1357_charger_data *smb1357data =
  2014. i2c_get_clientdata(client);
  2015. u8 data_f, data_e, status;
  2016. bool ret = false;
  2017. u8 gpio = false;
  2018. msleep(1);
  2019. /* Check Inserted Pogo */
  2020. smb1357_charger_i2c_read(client, IRQ_F_REG, &data_f);
  2021. /* Check Removed Pogo */
  2022. smb1357_charger_i2c_read(client, IRQ_E_REG, &data_e);
  2023. /* Check DCIN HV/LV Port */
  2024. smb1357_charger_i2c_read(client, STATUS_8_REG, &status);
  2025. if (((data_f & IRQ_F_POWER_OK_BIT) && ((status & (DCIN_HV|DCIN_LV)) != 0)
  2026. && (smb1357data->pogo_status == DCIN_NONE)) ||
  2027. ((data_e & IRQ_E_DC_UV_BIT) && ((status & (DCIN_HV|DCIN_LV)) == 0)
  2028. &&(smb1357data->pogo_status != DCIN_NONE))) {
  2029. ret = true;
  2030. }
  2031. if (smb1357data->pogo_det_gpio) {
  2032. gpio = gpio_get_value(smb1357data->pogo_det_gpio);
  2033. if ((smb1357data->pogo_status != DCIN_NONE) && (gpio == true))
  2034. {
  2035. ret = true;
  2036. dev_info(&client->dev,"%s, pogo status[%d],gpio[%d]",
  2037. __func__, smb1357data->pogo_status, gpio);
  2038. }
  2039. }
  2040. dev_info(&client->dev,
  2041. "%s, f[0x%x] e[0x%x] st[0x%x] gpio[%d]ret[%s]\n",
  2042. __func__, data_f, data_e, status,smb1357data->pogo_status,
  2043. ret ? "POGO":"NONE");
  2044. return ret;
  2045. }
  2046. static void smb1357_chg_isr_work(struct work_struct *work)
  2047. {
  2048. struct sec_charger_info *charger =
  2049. container_of(work, struct sec_charger_info, isr_work.work);
  2050. struct i2c_client *client = charger->client;
  2051. struct smb1357_charger_data *smb1357data =
  2052. i2c_get_clientdata(client);
  2053. u8 data_f = 0;
  2054. dev_info(&charger->client->dev,
  2055. "%s: Charger Interrupt\n", __func__);
  2056. wake_lock(&smb1357data->chgisr_wake_lock);
  2057. if (smb1357_is_pogo_event(client)) {
  2058. if (smb1357data->pogo_det_gpio) {
  2059. wake_lock(&smb1357data->chg_wake_lock);
  2060. smb1357_syson_sensing(client, 10, true);
  2061. cancel_delayed_work(&smb1357data->pogo_det_work);
  2062. smb1357data->pogo_det_count = 3;
  2063. schedule_delayed_work(&smb1357data->pogo_det_work,
  2064. msecs_to_jiffies(200));
  2065. }
  2066. }
  2067. if (smb1357data->pogo_status == DCIN_5V) {
  2068. smb1357_dcin_config_hv_lv(client,false);
  2069. }
  2070. smb1357_charger_i2c_read(client, IRQ_F_REG, &data_f);
  2071. if (data_f & IRQ_F_OTG_OV_CUR_BIT) {
  2072. #ifdef SMB_OTG_NOTIFY_OVERCURRENT
  2073. sec_otg_notify(HNOTIFY_OVERCURRENT);
  2074. #endif
  2075. dev_info(&client->dev,"%s, otg over current\n",__func__);
  2076. }
  2077. /* Clear Irq */
  2078. smb1357_clear_irq_status(client);
  2079. wake_unlock(&smb1357data->chgisr_wake_lock);
  2080. return;
  2081. }
  2082. static irqreturn_t smb1357_chg_irq_thread(int irq, void *irq_data)
  2083. {
  2084. struct sec_charger_info *charger = irq_data;
  2085. cancel_delayed_work(&charger->isr_work);
  2086. schedule_delayed_work(&charger->isr_work, msecs_to_jiffies(0));
  2087. return IRQ_HANDLED;
  2088. }
  2089. #if defined(ENABLE_MAX77804K_CHG_IRQ)
  2090. static void smb1357_detbat_work(struct work_struct *work)
  2091. {
  2092. struct smb1357_charger_data *smb1357data =
  2093. container_of(work, struct smb1357_charger_data, detbat_work.work);
  2094. struct sec_charger_info *charger = smb1357data->charger;
  2095. struct i2c_client *client = charger->client;
  2096. union power_supply_propval value;
  2097. #if defined(ENABLE_SMBCHG_BATT_DET)
  2098. value.intval = smb1357_get_batt_status(client);
  2099. #else
  2100. value.intval = smb1357_check_vf_status(client);
  2101. #endif
  2102. psy_do_property("battery", set,
  2103. POWER_SUPPLY_PROP_PRESENT, value);
  2104. dev_dbg(&client->dev, "%s:\n", __func__);
  2105. return;
  2106. }
  2107. static irqreturn_t smb1357_detbat_irq_thread(int irq, void *irq_data)
  2108. {
  2109. struct sec_charger_info *charger = irq_data;
  2110. struct smb1357_charger_data *smb1357data =
  2111. dev_get_drvdata(&charger->client->dev);
  2112. schedule_delayed_work(&smb1357data->detbat_work, msecs_to_jiffies(0));
  2113. return IRQ_HANDLED;
  2114. }
  2115. #endif
  2116. static int smb1357_chg_create_attrs(struct device *dev)
  2117. {
  2118. int i, rc;
  2119. for (i = 0; i < ARRAY_SIZE(smb1357_charger_attrs); i++) {
  2120. rc = device_create_file(dev, &smb1357_charger_attrs[i]);
  2121. if (rc)
  2122. goto create_attrs_failed;
  2123. }
  2124. goto create_attrs_succeed;
  2125. create_attrs_failed:
  2126. dev_err(dev, "%s: failed (%d)\n", __func__, rc);
  2127. while (i--)
  2128. device_remove_file(dev, &smb1357_charger_attrs[i]);
  2129. create_attrs_succeed:
  2130. return rc;
  2131. }
  2132. ssize_t smb1357_chg_show_attrs(struct device *dev,
  2133. struct device_attribute *attr, char *buf)
  2134. {
  2135. const ptrdiff_t offset = attr - smb1357_charger_attrs;
  2136. int i = 0;
  2137. switch (offset) {
  2138. case CHG_REG:
  2139. case CHG_DATA:
  2140. case CHG_REGS:
  2141. i = smb1357_hal_chg_show_attrs(dev, offset, buf);
  2142. break;
  2143. default:
  2144. i = -EINVAL;
  2145. break;
  2146. }
  2147. return i;
  2148. }
  2149. ssize_t smb1357_chg_store_attrs(struct device *dev,
  2150. struct device_attribute *attr,
  2151. const char *buf, size_t count)
  2152. {
  2153. const ptrdiff_t offset = attr - smb1357_charger_attrs;
  2154. int ret = 0;
  2155. switch (offset) {
  2156. case CHG_REG:
  2157. case CHG_DATA:
  2158. ret = smb1357_hal_chg_store_attrs(dev, offset, buf, count);
  2159. break;
  2160. default:
  2161. ret = -EINVAL;
  2162. break;
  2163. }
  2164. return ret;
  2165. }
  2166. ssize_t chg_current_store_attrs(struct device *dev,
  2167. struct device_attribute *attr,
  2168. const char *buf, size_t count)
  2169. {
  2170. struct power_supply *psy = dev_get_drvdata(dev);
  2171. struct sec_charger_info *charger =
  2172. container_of(psy, struct sec_charger_info, psy_chg);
  2173. struct smb1357_charger_data *smb1357data =
  2174. dev_get_drvdata(&charger->client->dev);
  2175. const ptrdiff_t offset = attr - smb1357_charger_attrs;
  2176. int ret = 0;
  2177. int mode;
  2178. int cur = 0;
  2179. switch (offset) {
  2180. case CUR_MODE:
  2181. if (sscanf(buf, "%d\n", &mode) == 1) {
  2182. smb1357data->sysfs_mode = (u8)mode;
  2183. ret = count;
  2184. }
  2185. break;
  2186. case CUR_SET:
  2187. if (sscanf(buf, "%d\n", &cur) == 1) {
  2188. switch(smb1357data->sysfs_mode) {
  2189. case 1:
  2190. smb1357data->sysfs_curin = (u32)cur;
  2191. break;
  2192. case 2:
  2193. smb1357data->sysfs_curout = (u32)cur;
  2194. break;
  2195. #if defined(USE_SYSFS_CURRENT_CTRL)
  2196. case 11:
  2197. charger->pdata->charging_current[
  2198. POWER_SUPPLY_TYPE_MAINS].input_current_limit = (u32)cur;
  2199. break;
  2200. case 12:
  2201. charger->pdata->charging_current[
  2202. POWER_SUPPLY_TYPE_MAINS].fast_charging_current = (u32)cur;
  2203. break;
  2204. case 21:
  2205. charger->pdata->charging_current[
  2206. POWER_SUPPLY_TYPE_HV_MAINS].input_current_limit = (u32)cur;
  2207. break;
  2208. case 22:
  2209. charger->pdata->charging_current[
  2210. POWER_SUPPLY_TYPE_HV_MAINS].fast_charging_current = (u32)cur;
  2211. break;
  2212. #endif
  2213. default:
  2214. break;
  2215. }
  2216. ret = count;
  2217. }
  2218. break;
  2219. default:
  2220. ret = -EINVAL;
  2221. break;
  2222. }
  2223. dev_info(&charger->client->dev,
  2224. "%s: mode [%d], cur[%d]\n", __func__,
  2225. smb1357data->sysfs_mode,(u32)cur);
  2226. return ret;
  2227. }
  2228. ssize_t chg_current_show_attrs(struct device *dev,
  2229. struct device_attribute *attr, char *buf)
  2230. {
  2231. struct power_supply *psy = dev_get_drvdata(dev);
  2232. struct sec_charger_info *charger =
  2233. container_of(psy, struct sec_charger_info, psy_chg);
  2234. struct smb1357_charger_data *smb1357data =
  2235. dev_get_drvdata(&charger->client->dev);
  2236. const ptrdiff_t offset = attr - smb1357_charger_attrs;
  2237. int ret = 0;
  2238. u32 curin = 0;
  2239. u32 curout = 0;
  2240. switch (offset) {
  2241. case CUR_MODE:
  2242. ret=sprintf(buf,"%d\n",smb1357data->sysfs_mode);
  2243. break;
  2244. case CUR_SET:
  2245. switch(smb1357data->sysfs_mode) {
  2246. case 1:
  2247. case 2:
  2248. curin = smb1357data->sysfs_curin;
  2249. curout = smb1357data->sysfs_curout;
  2250. ret=sprintf(buf,"mode[%d] in: %d, out: %d\n",
  2251. smb1357data->sysfs_mode, curin, curout);
  2252. break;
  2253. case 11:
  2254. case 12:
  2255. curin = charger->pdata->charging_current[
  2256. POWER_SUPPLY_TYPE_MAINS].input_current_limit;
  2257. curout = charger->pdata->charging_current[
  2258. POWER_SUPPLY_TYPE_MAINS].fast_charging_current;
  2259. ret=sprintf(buf,"mode[%d] in: %d, out: %d\n",
  2260. smb1357data->sysfs_mode,curin, curout);
  2261. break;
  2262. case 21:
  2263. case 22:
  2264. curin = charger->pdata->charging_current[
  2265. POWER_SUPPLY_TYPE_HV_MAINS].input_current_limit;
  2266. curout = charger->pdata->charging_current[
  2267. POWER_SUPPLY_TYPE_HV_MAINS].fast_charging_current;
  2268. ret=sprintf(buf,"mode[%d] in: %d, out: %d\n",
  2269. smb1357data->sysfs_mode,curin, curout);
  2270. break;
  2271. case USE_SYSFS_CUR:
  2272. ret=sprintf(buf,"Use SYSFS CUR!!!\n");
  2273. break;
  2274. default:
  2275. ret=sprintf(buf,"%s\n","cur_mode Error");
  2276. break;
  2277. }
  2278. break;
  2279. default:
  2280. ret = -EINVAL;
  2281. break;
  2282. }
  2283. dev_info(&charger->client->dev,
  2284. "%s: mode [%d], cur in[%d]cur out[%d]\n", __func__,
  2285. smb1357data->sysfs_mode, curin, curout);
  2286. return ret;
  2287. }
  2288. #ifdef CONFIG_OF
  2289. static int smb1357_charger_read_u32_index_dt(const struct device_node *np,
  2290. const char *propname,
  2291. u32 index, u32 *out_value)
  2292. {
  2293. struct property *prop = of_find_property(np, propname, NULL);
  2294. u32 len = (index + 1) * sizeof(*out_value);
  2295. if (!prop)
  2296. return (-EINVAL);
  2297. if (!prop->value)
  2298. return (-ENODATA);
  2299. if (len > prop->length)
  2300. return (-EOVERFLOW);
  2301. *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
  2302. return 0;
  2303. }
  2304. static int smb1357_charger_parse_dt(struct smb1357_charger_data *smb1357data)
  2305. {
  2306. struct device_node *np = of_find_node_by_name(NULL, DT_NODE_NAME);
  2307. struct sec_charger_info *charger = smb1357data->charger;
  2308. sec_battery_platform_data_t *pdata = charger->pdata;
  2309. int ret = 0;
  2310. int i, len;
  2311. const u32 *p;
  2312. if (np == NULL) {
  2313. pr_err("%s np NULL\n", __func__);
  2314. return -1;
  2315. } else {
  2316. ret = of_get_named_gpio(np, "battery,pogo_det_gpio",0);
  2317. if (ret < 0)
  2318. pr_err("%s: pogo det gpio (%d)\n", __func__,ret);
  2319. else
  2320. smb1357data->pogo_det_gpio = ret;
  2321. ret = of_get_named_gpio(np, "battery,shdn_gpio",0);
  2322. if (ret < 0)
  2323. pr_err("%s: shutdown gpio (%d)\n", __func__,ret);
  2324. else
  2325. smb1357data->shdn_gpio = ret;
  2326. ret = of_get_named_gpio(np, "battery,ovp_gpio_en",0);
  2327. if (ret < 0)
  2328. pr_err("%s: ovp gpio en (%d)\n", __func__,ret);
  2329. else
  2330. smb1357data->ovp_gpio_en = ret;
  2331. ret = of_get_named_gpio(np, "battery,chg_vf_det",0);
  2332. if (ret < 0)
  2333. pr_err("%s: vf det (%d)\n", __func__,ret);
  2334. else
  2335. smb1357data->chg_vf_det = ret;
  2336. ret = of_property_read_u32(np, "battery,chg_high_temp",
  2337. &smb1357data->chg_high_temp);
  2338. if (ret < 0)
  2339. pr_err("%s: chg_high_temp read failed (%d)\n", __func__,ret);
  2340. ret = of_property_read_u32(np, "battery,chg_high_temp_recovery",
  2341. &smb1357data->chg_high_temp_recovery);
  2342. if (ret < 0)
  2343. pr_err("%s: chg_high_temp_recovery read failed (%d)\n",
  2344. __func__,ret);
  2345. smb1357data->chg_vf_1p8v =
  2346. regulator_get(&charger->client->dev, "max77826_ldo6");
  2347. if (IS_ERR(smb1357data->chg_vf_1p8v)){
  2348. pr_err("%s: chg_vf_1p8v regulator error\n",__func__);
  2349. smb1357data->chg_vf_1p8v = NULL;
  2350. }
  2351. ret = of_property_read_u32(np, "battery,detbat_irq",
  2352. &smb1357data->detbat_irq);
  2353. if (ret < 0)
  2354. pr_err("%s: chg_irq read failed (%d)\n", __func__,ret);
  2355. ret = of_get_named_gpio(np, "battery,chg_irq", 0);
  2356. if (ret < 0)
  2357. pr_err("%s: chg_irq read failed (%d)\n", __func__,ret);
  2358. else{
  2359. gpio_request(ret, "smbcharger_irq");
  2360. pdata->chg_irq = gpio_to_irq(ret);
  2361. }
  2362. ret = of_property_read_u32(np, "battery,chg_irq_attr",
  2363. (unsigned int *)&pdata->chg_irq_attr);
  2364. if (ret < 0)
  2365. pr_err("%s: chg_irq_attr read failed (%d)\n", __func__, ret);
  2366. ret = of_property_read_u32(np, "battery,chg_float_voltage",
  2367. &pdata->chg_float_voltage);
  2368. if (ret < 0)
  2369. pr_err("%s: chg_float_voltage read failed (%d)\n", __func__, ret);
  2370. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  2371. &pdata->ovp_uvlo_check_type);
  2372. if (ret < 0)
  2373. pr_err("%s: ovp_uvlo_check_type read failed (%d)\n", __func__, ret);
  2374. ret = of_property_read_u32(np, "battery,full_check_type",
  2375. &pdata->full_check_type);
  2376. if (ret < 0)
  2377. pr_err("%s: full_check_type read failed (%d)\n", __func__, ret);
  2378. ret = of_property_read_u32(np, "battery,full_check_type_2nd",
  2379. &pdata->full_check_type_2nd);
  2380. if (ret < 0)
  2381. pr_err("%s: full_check_type_2nd read failed (%d)\n", __func__, ret);
  2382. p = of_get_property(np, "battery,input_current_limit", &len);
  2383. len = len / sizeof(u32);
  2384. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  2385. GFP_KERNEL);
  2386. for(i = 0; i < len; i++) {
  2387. ret = smb1357_charger_read_u32_index_dt(np,
  2388. "battery,input_current_limit", i,
  2389. &pdata->charging_current[i].input_current_limit);
  2390. ret = smb1357_charger_read_u32_index_dt(np,
  2391. "battery,fast_charging_current", i,
  2392. &pdata->charging_current[i].fast_charging_current);
  2393. ret = smb1357_charger_read_u32_index_dt(np,
  2394. "battery,full_check_current_1st", i,
  2395. &pdata->charging_current[i].full_check_current_1st);
  2396. ret = smb1357_charger_read_u32_index_dt(np,
  2397. "battery,full_check_current_2nd", i,
  2398. &pdata->charging_current[i].full_check_current_2nd);
  2399. }
  2400. }
  2401. return ret;
  2402. }
  2403. #else
  2404. static int smb1357_charger_parse_dt(struct max77803_charger_data *charger)
  2405. {
  2406. return 0;
  2407. }
  2408. #endif
  2409. static void smb1357_charger_hvdcp_det_work(struct work_struct *work)
  2410. {
  2411. struct smb1357_charger_data *smb1357data =
  2412. container_of(work, struct smb1357_charger_data,hvdcp_det_work.work);
  2413. struct sec_charger_info *charger = smb1357data->charger;
  2414. struct i2c_client *client = charger->client;
  2415. union power_supply_propval value;
  2416. if (charger->is_charging == true) {
  2417. smb1357_hal_chg_get_property(client,
  2418. POWER_SUPPLY_PROP_ONLINE, &value);
  2419. if ((value.intval == POWER_SUPPLY_TYPE_HV_MAINS) &&
  2420. (charger->cable_type == POWER_SUPPLY_TYPE_MAINS)){
  2421. psy_do_property("battery",set,
  2422. POWER_SUPPLY_PROP_ONLINE, value);
  2423. msleep(50);
  2424. smb1357data->hvdcp_det_count = 0;
  2425. }
  2426. else {
  2427. if (smb1357data->hvdcp_det_count > 0){
  2428. smb1357data->hvdcp_det_count--;
  2429. cancel_delayed_work(&smb1357data->hvdcp_det_work);
  2430. schedule_delayed_work(&smb1357data->hvdcp_det_work,
  2431. msecs_to_jiffies(1000));
  2432. }
  2433. }
  2434. }
  2435. dev_dbg(&client->dev,"%s, is_charging [%d], cable type = [%d]\n",
  2436. __func__, charger->is_charging, charger->cable_type);
  2437. return;
  2438. }
  2439. ssize_t chip_id_check_show_attrs(struct device *dev,
  2440. struct device_attribute *attr, char *buf)
  2441. {
  2442. struct power_supply *psy = dev_get_drvdata(dev);
  2443. struct sec_charger_info *charger =
  2444. container_of(psy, struct sec_charger_info, psy_chg);
  2445. u8 id;
  2446. id = smb1357_check_product_id(charger->client);
  2447. return snprintf(buf, 9, "%s\n",
  2448. (id == 0xff) ? "Unknown": id ? "SMB1356":"SMB1357");
  2449. }
  2450. ssize_t pogo_chg_show_attrs(struct device *dev,
  2451. struct device_attribute *attr, char *buf)
  2452. {
  2453. struct power_supply *psy = dev_get_drvdata(dev);
  2454. struct sec_charger_info *charger =
  2455. container_of(psy, struct sec_charger_info, psy_chg);
  2456. struct smb1357_charger_data *smb1357data =
  2457. dev_get_drvdata(&charger->client->dev);
  2458. dev_info(&charger->client->dev,
  2459. "%s: pogo status [ %d ]\n",
  2460. __func__, smb1357data->pogo_status);
  2461. return snprintf(buf, 3, "%d\n", smb1357data->pogo_status);
  2462. }
  2463. ssize_t chg_therm_show_attrs(struct device *dev,
  2464. struct device_attribute *attr, char *buf)
  2465. {
  2466. struct power_supply *psy = dev_get_drvdata(dev);
  2467. struct sec_charger_info *charger =
  2468. container_of(psy, struct sec_charger_info, psy_chg);
  2469. int temp;
  2470. temp = smb1357_get_therm_temp(charger->client);
  2471. dev_info(&charger->client->dev,
  2472. "%s: chg_threm [ %d ]\n", __func__, temp);
  2473. return snprintf(buf, 6, "%4d\n",temp);
  2474. }
  2475. ssize_t chg_therm_store_attrs(struct device *dev,
  2476. struct device_attribute *attr,
  2477. const char *buf, size_t count)
  2478. {
  2479. struct power_supply *psy = dev_get_drvdata(dev);
  2480. struct sec_charger_info *charger =
  2481. container_of(psy, struct sec_charger_info, psy_chg);
  2482. struct smb1357_charger_data *smb1357data =
  2483. dev_get_drvdata(&charger->client->dev);
  2484. int ret = 0;
  2485. int temp;
  2486. if (sscanf(buf, "%d\n", &temp) == 1) {
  2487. smb1357data->chg_high_temp = temp;
  2488. smb1357data->chg_high_temp_recovery = temp - 100;
  2489. ret = count;
  2490. }
  2491. dev_info(&charger->client->dev,
  2492. "%s: high [%d], recovery [%d]\n", __func__,
  2493. smb1357data->chg_high_temp,smb1357data->chg_high_temp_recovery);
  2494. return ret;
  2495. }
  2496. ssize_t chg_therm_adc_show_attrs(struct device *dev,
  2497. struct device_attribute *attr, char *buf)
  2498. {
  2499. struct power_supply *psy = dev_get_drvdata(dev);
  2500. struct sec_charger_info *charger =
  2501. container_of(psy, struct sec_charger_info, psy_chg);
  2502. struct qpnp_vadc_result results;
  2503. int temp_adc = -1;
  2504. int err = 0;
  2505. err = qpnp_vadc_read(NULL, CHG_THEM_THEM_CHANNEL, &results);
  2506. if (err < 0) {
  2507. dev_err(&charger->client->dev,"%s : therm read fail rc = %d\n",
  2508. __func__, err);
  2509. }
  2510. else {
  2511. temp_adc = results.adc_code;
  2512. }
  2513. dev_info(&charger->client->dev,
  2514. "%s: chg_threm_adc [ %d ]\n", __func__, temp_adc);
  2515. return snprintf(buf, 8, "%d\n",temp_adc);
  2516. }
  2517. static int sec_pogo_get_property(struct power_supply *psy,
  2518. enum power_supply_property psp,
  2519. union power_supply_propval *val)
  2520. {
  2521. struct smb1357_charger_data *smb1357data =
  2522. container_of(psy, struct smb1357_charger_data, psy_pogo);
  2523. if (psp != POWER_SUPPLY_PROP_ONLINE)
  2524. return -EINVAL;
  2525. if (smb1357data->pogo_status != DCIN_NONE)
  2526. val->intval = 1;
  2527. else
  2528. val->intval = 0;
  2529. dev_info(&smb1357data->charger->client->dev,
  2530. "%s: pogo status [ %d ]\n",
  2531. __func__, smb1357data->pogo_status);
  2532. return 0;
  2533. }
  2534. static void smb1357_gpio_init(struct smb1357_charger_data *smb1357data)
  2535. {
  2536. int ret;
  2537. if(smb1357data->pogo_det_gpio) {
  2538. gpio_tlmm_config(GPIO_CFG(smb1357data->pogo_det_gpio,
  2539. 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,GPIO_CFG_2MA),
  2540. GPIO_CFG_ENABLE);
  2541. gpio_request(smb1357data->pogo_det_gpio, "pogo_det_gpio");
  2542. }
  2543. if(smb1357data->shdn_gpio) {
  2544. gpio_tlmm_config(GPIO_CFG(smb1357data->shdn_gpio,
  2545. 1, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,GPIO_CFG_2MA),
  2546. GPIO_CFG_ENABLE);
  2547. gpio_request(smb1357data->shdn_gpio, "shdn_gpio");
  2548. #if defined(ENABLE_SHUTDOWN_MODE)
  2549. gpio_set_value(smb1357data->shdn_gpio, true);
  2550. #else
  2551. gpio_set_value(smb1357data->shdn_gpio, false);
  2552. #endif
  2553. msleep(10);
  2554. }
  2555. if (smb1357data->ovp_gpio_en) {
  2556. gpio_tlmm_config(GPIO_CFG(smb1357data->ovp_gpio_en,
  2557. 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA),
  2558. GPIO_CFG_ENABLE);
  2559. ret = gpio_request(smb1357data->ovp_gpio_en, "ovp_gpio_en");
  2560. if (ret) {
  2561. pr_err("ERROR ovp_gpio_en request failed.\n");
  2562. }
  2563. else {
  2564. ovp_gpio_enable(smb1357data->ovp_gpio_en, false);
  2565. }
  2566. }
  2567. if (smb1357data->chg_vf_1p8v) {
  2568. ret = regulator_set_voltage(smb1357data->chg_vf_1p8v,
  2569. 1800000, 1800000);
  2570. if (unlikely(ret < 0)){
  2571. pr_err("ERROR chg_vf_1p8v set voltage failed.\n");
  2572. }
  2573. else {
  2574. if (regulator_is_enabled(smb1357data->chg_vf_1p8v)){
  2575. ret = regulator_disable(smb1357data->chg_vf_1p8v);
  2576. }
  2577. }
  2578. }
  2579. return;
  2580. }
  2581. static void smb1357_charger_pogo_det_work(struct work_struct *work)
  2582. {
  2583. struct smb1357_charger_data *smb1357data =
  2584. container_of(work, struct smb1357_charger_data,pogo_det_work.work);
  2585. struct sec_charger_info *charger = smb1357data->charger;
  2586. struct i2c_client *client = charger->client;
  2587. u8 gpio;
  2588. union power_supply_propval val;
  2589. cancel_delayed_work(&smb1357data->pogo_det_work);
  2590. gpio = gpio_get_value(smb1357data->pogo_det_gpio);
  2591. if (gpio == false) {
  2592. smb1357data->pogo_det_count = 0;
  2593. smb1357_syson_sensing(client, 4000, false);
  2594. smb1357_dcin_config_hv_lv(client, true);
  2595. smb1357data->pogo_status =
  2596. smb1357_dcin_input_voltage(client);
  2597. dev_dbg(&client->dev," %s, pogo detect, %dv usbin %d\n",
  2598. __func__,smb1357data->pogo_status,
  2599. smb1357data->usbin_cable_type);
  2600. if (smb1357data->usbin_cable_type == POWER_SUPPLY_TYPE_BATTERY) {
  2601. val.intval = POWER_SUPPLY_TYPE_POGODOCK;
  2602. psy_do_property("battery",set,
  2603. POWER_SUPPLY_PROP_ONLINE, val);
  2604. msleep(100);
  2605. }
  2606. else {
  2607. /* Connected USBIN port, Just update UI */
  2608. if (smb1357data->psy_bat) {
  2609. power_supply_changed(smb1357data->psy_bat);
  2610. dev_dbg(&client->dev,"%s, Just update UI\n",
  2611. __func__);
  2612. }
  2613. }
  2614. wake_unlock(&smb1357data->chg_wake_lock);
  2615. }
  2616. else {
  2617. if (smb1357data->pogo_det_count > 0) {
  2618. smb1357data->pogo_det_count--;
  2619. schedule_delayed_work(&smb1357data->pogo_det_work,
  2620. msecs_to_jiffies(50));
  2621. }
  2622. else {
  2623. dev_dbg(&client->dev," %s, pogo removed, %dv \n",
  2624. __func__,smb1357data->pogo_status);
  2625. smb1357_syson_sensing(client, 10, false);
  2626. smb1357_dcin_config_hv_lv(client, true);
  2627. smb1357data->pogo_status = DCIN_NONE;
  2628. if (smb1357data->usbin_cable_type == POWER_SUPPLY_TYPE_BATTERY) {
  2629. val.intval = POWER_SUPPLY_TYPE_BATTERY;
  2630. psy_do_property("battery",set,
  2631. POWER_SUPPLY_PROP_ONLINE, val);
  2632. msleep(100);
  2633. }
  2634. else {
  2635. /* Connected USBIN port, Just update UI */
  2636. if (smb1357data->psy_bat) {
  2637. power_supply_changed(smb1357data->psy_bat);
  2638. dev_dbg(&client->dev,"%s,Just update UI\n",
  2639. __func__);
  2640. }
  2641. }
  2642. wake_unlock(&smb1357data->chg_wake_lock);
  2643. }
  2644. }
  2645. dev_dbg(&client->dev," %s, det [ %d ], mode [ %d v]",
  2646. __func__, gpio, smb1357data->pogo_status);
  2647. }
  2648. static void smb1357_charger_init_work(struct work_struct *work)
  2649. {
  2650. struct smb1357_charger_data *smb1357data =
  2651. container_of(work, struct smb1357_charger_data,init_work.work);
  2652. struct i2c_client *client = smb1357data->charger->client;
  2653. struct sec_battery_info *battery;
  2654. int ret = 0;
  2655. smb1357data->psy_bat = power_supply_get_by_name("battery");
  2656. if (smb1357data->psy_bat != NULL) {
  2657. battery =
  2658. container_of(smb1357data->psy_bat, struct sec_battery_info, psy_bat);
  2659. smb1357data->psy_pogo.name = "pogo",
  2660. smb1357data->psy_pogo.type = POWER_SUPPLY_TYPE_POGODOCK,
  2661. smb1357data->psy_pogo.supplied_to = pogo_supply_list,
  2662. smb1357data->psy_pogo.num_supplicants = ARRAY_SIZE(pogo_supply_list);
  2663. smb1357data->psy_pogo.properties = pogo_power_props,
  2664. smb1357data->psy_pogo.num_properties = ARRAY_SIZE(pogo_power_props),
  2665. smb1357data->psy_pogo.get_property = sec_pogo_get_property;
  2666. ret = power_supply_register(battery->dev, &smb1357data->psy_pogo);
  2667. if (ret) {
  2668. dev_err(battery->dev,
  2669. "%s: Failed to Register psy_pogo\n", __func__);
  2670. }
  2671. }
  2672. smb1357_dcin_config_hv_lv(client, false);
  2673. if (smb1357_is_pogo_event(client)) {
  2674. if (smb1357data->pogo_det_gpio) {
  2675. wake_lock(&smb1357data->chg_wake_lock);
  2676. cancel_delayed_work(&smb1357data->pogo_det_work);
  2677. smb1357data->pogo_det_count = 3;
  2678. schedule_delayed_work(&smb1357data->pogo_det_work,
  2679. msecs_to_jiffies(200));
  2680. }
  2681. }
  2682. #if defined(ENABLE_MAX77804K_CHG_IRQ)
  2683. if (max77804k_tiny_irq_batp > 0) {
  2684. smb1357data->detbat_irq = max77804k_tiny_irq_batp;
  2685. if (smb1357data->detbat_irq){
  2686. INIT_DELAYED_WORK(
  2687. &smb1357data->detbat_work, smb1357_detbat_work);
  2688. ret = request_threaded_irq(smb1357data->detbat_irq,
  2689. NULL, smb1357_detbat_irq_thread, IRQF_TRIGGER_FALLING,
  2690. "detbat-irq", smb1357data->charger);
  2691. if (ret) {
  2692. dev_err(&client->dev,
  2693. "%s: Failed to Reqeust DETBAT IRQ\n", __func__);
  2694. }
  2695. }
  2696. }
  2697. dev_dbg(&client->dev,"%s batp %d\n", __func__, max77804k_tiny_irq_batp);
  2698. #endif
  2699. return;
  2700. }
  2701. #if defined(USE_DEBUG_WORK)
  2702. static void smb1357_charger_debug_work(struct work_struct *work)
  2703. {
  2704. struct smb1357_charger_data *smb1357data =
  2705. container_of(work, struct smb1357_charger_data,debug_work.work);
  2706. struct i2c_client *client = smb1357data->charger->client;
  2707. u8 buf_a = 0;
  2708. u8 buf_b = 0;
  2709. u8 addr = 0;
  2710. for (addr = 0; addr <= 0x1f; addr++)
  2711. {
  2712. smb1357_charger_i2c_read(client, addr, &buf_a);
  2713. smb1357_charger_i2c_read(client, addr+0x40, &buf_b);
  2714. dev_dbg(&client->dev,"[reg 0x%2x : 0x%2x], [reg 0x%2x : 0x%2x]\n",
  2715. addr, buf_a, addr+0x40, buf_b);
  2716. }
  2717. dev_dbg(&client->dev,"============================================\n");
  2718. schedule_delayed_work(&smb1357data->debug_work, msecs_to_jiffies(10000));
  2719. return;
  2720. }
  2721. #endif
  2722. #if defined(ENABLE_EXTERNAL_APSD)
  2723. extern void smb1357_charger_external_apsd(u8 enable)
  2724. {
  2725. struct power_supply *psy;
  2726. struct sec_charger_info *charger;
  2727. struct smb1357_charger_data *smb1357data;
  2728. struct i2c_client *client;
  2729. u8 apsd_reg = 0x00;
  2730. psy = power_supply_get_by_name("smb1357-charger");
  2731. if (psy == NULL) {
  2732. pr_err("%s smb1357 loading failed.\n", __func__);
  2733. goto out;
  2734. }
  2735. charger = container_of(psy, struct sec_charger_info, psy_chg);
  2736. smb1357data = dev_get_drvdata(&charger->client->dev);
  2737. client = smb1357data->charger->client;
  2738. smb1357_charger_i2c_read(client, CFG_11_REG, &apsd_reg);
  2739. apsd_reg &= AUTO_DET_SRC_EN_BIT;
  2740. if (apsd_reg == enable) {
  2741. pr_err("%s apsd_reg = [0x%x].\n", __func__, apsd_reg);
  2742. goto shdn;
  2743. }
  2744. if (enable) {
  2745. smb1357_write_enable(client, true);
  2746. msleep(10);
  2747. /* USBIN ADAPTER : 9V only */
  2748. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  2749. USBIN_ADAPTER_MASK, 0x60);
  2750. smb1357_set_APSD(client, true);
  2751. /* USBIN ADAPTER : 5V or 9V */
  2752. smb1357_charger_masked_write_reg(client, CFG_C_REG,
  2753. USBIN_ADAPTER_MASK, 0x20);
  2754. }
  2755. dev_dbg(&client->dev,"%s enable [%d]\n", __func__, enable);
  2756. shdn:
  2757. smb1357_chgin_shutdown(client, true);
  2758. out:
  2759. return;
  2760. }
  2761. #endif
  2762. static int smb1357_charger_probe(
  2763. struct i2c_client *client,
  2764. const struct i2c_device_id *id)
  2765. {
  2766. struct i2c_adapter *adapter =
  2767. to_i2c_adapter(client->dev.parent);
  2768. struct smb1357_charger_data *smb1357data;
  2769. struct sec_charger_info *charger;
  2770. int ret = 0;
  2771. dev_info(&client->dev,
  2772. "%s: SMB1357 Charger Driver Loading\n", __func__);
  2773. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  2774. return -EIO;
  2775. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  2776. if (!charger)
  2777. return -ENOMEM;
  2778. smb1357data = kzalloc(sizeof(*smb1357data), GFP_KERNEL);
  2779. if (!smb1357data){
  2780. ret = -ENOMEM;
  2781. goto err_free2;
  2782. }
  2783. smb1357data->charger = charger;
  2784. smb1357data->chg_limit = false;
  2785. smb1357data->pogo_status = DCIN_NONE;
  2786. smb1357data->usbin_cable_type = POWER_SUPPLY_TYPE_BATTERY;
  2787. smb1357data->charger->cable_type = POWER_SUPPLY_TYPE_BATTERY;
  2788. mutex_init(&smb1357data->mutex);
  2789. chg_temp_table_size =
  2790. sizeof(chg_temp_table)/sizeof(sec_bat_adc_table_data_t);
  2791. smb1357data->siop_level = 100;
  2792. charger->client = client;
  2793. if (client->dev.of_node) {
  2794. void * pdata = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  2795. if (!pdata)
  2796. goto err_free1;
  2797. charger->pdata = pdata;
  2798. if (smb1357_charger_parse_dt(smb1357data))
  2799. dev_err(&client->dev,
  2800. "%s : Failed to get charger dt\n", __func__);
  2801. } else
  2802. charger->pdata = client->dev.platform_data;
  2803. i2c_set_clientdata(client, smb1357data);
  2804. charger->psy_chg.name = DEV_CHG_NAME;
  2805. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  2806. charger->psy_chg.get_property = smb1357_chg_get_property;
  2807. charger->psy_chg.set_property = smb1357_chg_set_property;
  2808. charger->psy_chg.properties = smb1357_charger_props;
  2809. charger->psy_chg.num_properties = ARRAY_SIZE(smb1357_charger_props);
  2810. if (charger->pdata->chg_gpio_init) {
  2811. if (!charger->pdata->chg_gpio_init()) {
  2812. dev_err(&client->dev,
  2813. "%s: Failed to Initialize GPIO\n", __func__);
  2814. goto err_free;
  2815. }
  2816. }
  2817. smb1357_check_product_id(charger->client);
  2818. /* volatile access */
  2819. smb1357_write_enable(charger->client, true);
  2820. msleep(10);
  2821. smb1357_gpio_init(smb1357data);
  2822. smb1357_hw_init(charger->client);
  2823. if (!smb1357_hal_chg_init(charger->client)) {
  2824. dev_err(&client->dev,
  2825. "%s: Failed to Initialize Charger\n", __func__);
  2826. goto err_free;
  2827. }
  2828. ret = power_supply_register(&client->dev, &charger->psy_chg);
  2829. if (ret) {
  2830. dev_err(&client->dev,
  2831. "%s: Failed to Register psy_chg\n", __func__);
  2832. goto err_free;
  2833. }
  2834. if (charger->pdata->chg_irq) {
  2835. INIT_DELAYED_WORK(&charger->isr_work, smb1357_chg_isr_work);
  2836. ret = request_threaded_irq(charger->pdata->chg_irq,
  2837. NULL, smb1357_chg_irq_thread,
  2838. charger->pdata->chg_irq_attr,
  2839. "smbcharger_irq", charger);
  2840. if (ret) {
  2841. dev_err(&client->dev,
  2842. "%s: Failed to Reqeust IRQ\n", __func__);
  2843. goto err_supply_unreg;
  2844. }
  2845. ret = enable_irq_wake(charger->pdata->chg_irq);
  2846. if (ret < 0)
  2847. dev_err(&client->dev,
  2848. "%s: Failed to Enable Wakeup Source(%d)\n",
  2849. __func__, ret);
  2850. }
  2851. ret = smb1357_chg_create_attrs(charger->psy_chg.dev);
  2852. if (ret) {
  2853. dev_err(&client->dev,
  2854. "%s : Failed to create_attrs\n", __func__);
  2855. goto err_req_irq;
  2856. }
  2857. wake_lock_init(&smb1357data->chg_wake_lock, WAKE_LOCK_SUSPEND,
  2858. "chg-wakelock");
  2859. wake_lock_init(&smb1357data->chgisr_wake_lock, WAKE_LOCK_SUSPEND,
  2860. "chgisr-wakelock");
  2861. INIT_DELAYED_WORK(&smb1357data->hvdcp_det_work,
  2862. smb1357_charger_hvdcp_det_work);
  2863. INIT_DELAYED_WORK(&smb1357data->pogo_det_work,
  2864. smb1357_charger_pogo_det_work);
  2865. INIT_DELAYED_WORK(&smb1357data->syson_trigger_work,
  2866. smb1357_syson_trigger_work);
  2867. INIT_DELAYED_WORK(&smb1357data->init_work,
  2868. smb1357_charger_init_work);
  2869. schedule_delayed_work(&smb1357data->init_work, msecs_to_jiffies(3000));
  2870. #if defined(USE_DEBUG_WORK)
  2871. INIT_DELAYED_WORK(&smb1357data->debug_work, smb1357_charger_debug_work);
  2872. schedule_delayed_work(&smb1357data->debug_work, msecs_to_jiffies(5000));
  2873. #endif
  2874. #if defined(TIMER_FORCE_BLOCK)
  2875. force_block = false;
  2876. force_block_count = 0;
  2877. #endif
  2878. dev_dbg(&client->dev,
  2879. "%s: SMB1357 Charger Driver Loaded\n", __func__);
  2880. return 0;
  2881. err_req_irq:
  2882. if (charger->pdata->chg_irq)
  2883. free_irq(charger->pdata->chg_irq, charger);
  2884. err_supply_unreg:
  2885. power_supply_unregister(&charger->psy_chg);
  2886. err_free:
  2887. kfree(charger->pdata);
  2888. err_free1:
  2889. mutex_destroy(&smb1357data->mutex);
  2890. kfree(smb1357data);
  2891. err_free2:
  2892. kfree(charger);
  2893. return ret;
  2894. }
  2895. static int smb1357_charger_remove(struct i2c_client *client)
  2896. {
  2897. return 0;
  2898. }
  2899. static int smb1357_charger_suspend(struct device *dev)
  2900. {
  2901. struct smb1357_charger_data *smb1357data = dev_get_drvdata(dev);
  2902. struct sec_charger_info *charger = smb1357data->charger;
  2903. if (!smb1357_hal_chg_suspend(charger->client))
  2904. dev_err(&charger->client->dev,
  2905. "%s: Failed to Suspend Charger\n", __func__);
  2906. dev_info(&charger->client->dev,"%s: stop \n", __func__);
  2907. return 0;
  2908. }
  2909. static int smb1357_charger_resume(struct device *dev)
  2910. {
  2911. struct smb1357_charger_data *smb1357data = dev_get_drvdata(dev);
  2912. struct sec_charger_info *charger = smb1357data->charger;
  2913. dev_info(&charger->client->dev,"%s: start\n", __func__);
  2914. if (!smb1357_hal_chg_resume(charger->client))
  2915. dev_err(&charger->client->dev,
  2916. "%s: Failed to Resume Charger\n", __func__);
  2917. return 0;
  2918. }
  2919. static const struct dev_pm_ops smb1357_charger_pm_ops = {
  2920. .suspend = smb1357_charger_suspend,
  2921. .resume = smb1357_charger_resume,
  2922. };
  2923. static const struct i2c_device_id smb1357_charger_id[] = {
  2924. {"smb1357-charger", 0},
  2925. {}
  2926. };
  2927. MODULE_DEVICE_TABLE(i2c, smb1357_charger_id);
  2928. static struct of_device_id smb1357_i2c_match_table[] = {
  2929. { .compatible = "smb1357-charger,i2c", },
  2930. { },
  2931. };
  2932. MODULE_DEVICE_TABLE(i2c, smb1357_i2c_match_table);
  2933. static struct i2c_driver smb1357_charger_driver = {
  2934. .driver = {
  2935. .name = "smb1357-charger",
  2936. .owner = THIS_MODULE,
  2937. .of_match_table = smb1357_i2c_match_table,
  2938. #ifdef CONFIG_PM
  2939. .pm = &smb1357_charger_pm_ops,
  2940. #endif
  2941. },
  2942. .probe = smb1357_charger_probe,
  2943. .remove = smb1357_charger_remove,
  2944. .shutdown = smb1357_charger_shutdown,
  2945. .id_table = smb1357_charger_id,
  2946. };
  2947. static int __init smb1357_charger_init(void)
  2948. {
  2949. return i2c_add_driver(&smb1357_charger_driver);
  2950. }
  2951. static void __exit smb1357_charger_exit(void)
  2952. {
  2953. i2c_del_driver(&smb1357_charger_driver);
  2954. }
  2955. module_init(smb1357_charger_init);
  2956. module_exit(smb1357_charger_exit);
  2957. #if defined(ENABLE_MAX77804K_CHG_IRQ)
  2958. static void max77804k_tiny_init_work(struct work_struct *work)
  2959. {
  2960. struct max77804k_tiny_charger_data *max77804k_tiny_charger =
  2961. container_of(work, struct max77804k_tiny_charger_data,
  2962. tiny_init_work.work);
  2963. u8 reg_data;
  2964. #ifdef CONFIG_SEC_FACTORY
  2965. reg_data = 0xFF;
  2966. max77804k_update_reg(max77804k_tiny_charger->max77804k->i2c,
  2967. MAX77804K_CHG_REG_CHG_INT_MASK, reg_data, 0xFF);
  2968. #else
  2969. reg_data = 0x00;
  2970. max77804k_update_reg(max77804k_tiny_charger->max77804k->i2c,
  2971. MAX77804K_CHG_REG_CHG_INT_MASK, reg_data, 0x04);
  2972. #endif
  2973. pr_info("%s\n",__func__);
  2974. return;
  2975. }
  2976. static int max77804k_tiny_charger_probe(struct platform_device *pdev)
  2977. {
  2978. struct max77804k_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  2979. struct max77804k_tiny_charger_data *max77804k_tiny_charger;
  2980. int ret = 0;
  2981. pr_info("%s : MAX77804K Tiny Charger Driver\n",__func__);
  2982. max77804k_tiny_charger =
  2983. kzalloc(sizeof(*max77804k_tiny_charger), GFP_KERNEL);
  2984. if (!max77804k_tiny_charger){
  2985. ret = -ENOMEM;
  2986. goto err_free;
  2987. }
  2988. max77804k_tiny_charger->max77804k = iodev;
  2989. platform_set_drvdata(pdev, max77804k_tiny_charger);
  2990. INIT_DELAYED_WORK(&max77804k_tiny_charger->tiny_init_work,
  2991. max77804k_tiny_init_work);
  2992. #ifndef CONFIG_SEC_FACTORY
  2993. if (max77804k_tiny_charger->max77804k->irq_base) {
  2994. max77804k_tiny_irq_batp =
  2995. max77804k_tiny_charger->max77804k->irq_base + MAX77804K_CHG_IRQ_BATP_I;
  2996. }
  2997. else
  2998. #endif
  2999. max77804k_tiny_irq_batp = 0;
  3000. schedule_delayed_work(&max77804k_tiny_charger->tiny_init_work,
  3001. msecs_to_jiffies(3000));
  3002. err_free:
  3003. return ret;
  3004. }
  3005. static void max77804k_tiny_charger_shutdown(struct device *dev)
  3006. {
  3007. struct max77804k_tiny_charger_data *max77804k_tiny_charger =
  3008. dev_get_drvdata(dev);
  3009. u8 reg_data;
  3010. pr_info("%s MAX77804K Tiny Driver Shutdown\n",__func__);
  3011. reg_data = 0xff;
  3012. max77804k_write_reg(max77804k_tiny_charger->max77804k->i2c,
  3013. MAX77804K_CHG_REG_CHG_INT_MASK, reg_data);
  3014. return;
  3015. }
  3016. static int max77804k_tiny_charger_remove(struct platform_device *pdev)
  3017. {
  3018. struct max77804k_tiny_charger_data *max77804k_tiny_charger =
  3019. platform_get_drvdata(pdev);
  3020. pr_info("%s \n",__func__);
  3021. if(max77804k_tiny_charger)
  3022. kfree(max77804k_tiny_charger);
  3023. return 0;
  3024. }
  3025. static struct platform_driver max77804k_tiny_charger_driver = {
  3026. .driver = {
  3027. .name = "max77804k-charger",
  3028. .owner = THIS_MODULE,
  3029. .shutdown = max77804k_tiny_charger_shutdown,
  3030. },
  3031. .probe = max77804k_tiny_charger_probe,
  3032. .remove = max77804k_tiny_charger_remove,
  3033. };
  3034. static int __init max77804k_tiny_charger_init(void)
  3035. {
  3036. pr_info("func:%s\n", __func__);
  3037. return platform_driver_register(&max77804k_tiny_charger_driver);
  3038. }
  3039. static void __exit max77804k_tiny_charger_exit(void)
  3040. {
  3041. pr_info("func:%s\n", __func__);
  3042. platform_driver_unregister(&max77804k_tiny_charger_driver);
  3043. }
  3044. module_init(max77804k_tiny_charger_init);
  3045. module_exit(max77804k_tiny_charger_exit);
  3046. #endif
  3047. MODULE_DESCRIPTION("Samsung SMB1357 Charger Driver");
  3048. MODULE_AUTHOR("Samsung Electronics");
  3049. MODULE_LICENSE("GPL");